]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c
MdeModulePkg: ATA performance tuning.
[mirror_edk2.git] / MdeModulePkg / Bus / Scsi / ScsiDiskDxe / ScsiDisk.c
index 4b835fc27fb97de0fc5af8791dbc8bdc128bec55..0e8aa34ad63c61e6dc26d370db253d7b29c0cc9c 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   SCSI disk driver that layers on every SCSI IO protocol in the system.\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -166,6 +166,9 @@ ScsiDiskDriverBindingStart (
   UINT8                 Index;\r
   UINT8                 MaxRetry;\r
   BOOLEAN               NeedRetry;\r
+  BOOLEAN               MustReadCapacity;\r
+\r
+  MustReadCapacity = TRUE;\r
 \r
   ScsiDiskDevice = (SCSI_DISK_DEV *) AllocateZeroPool (sizeof (SCSI_DISK_DEV));\r
   if (ScsiDiskDevice == NULL) {\r
@@ -187,6 +190,7 @@ ScsiDiskDriverBindingStart (
 \r
   ScsiDiskDevice->Signature         = SCSI_DISK_DEV_SIGNATURE;\r
   ScsiDiskDevice->ScsiIo            = ScsiIo;\r
+  ScsiDiskDevice->BlkIo.Revision    = EFI_BLOCK_IO_PROTOCOL_REVISION3;\r
   ScsiDiskDevice->BlkIo.Media       = &ScsiDiskDevice->BlkIoMedia;\r
   ScsiDiskDevice->BlkIo.Reset       = ScsiDiskReset;\r
   ScsiDiskDevice->BlkIo.ReadBlocks  = ScsiDiskReadBlocks;\r
@@ -198,10 +202,12 @@ ScsiDiskDriverBindingStart (
   switch (ScsiDiskDevice->DeviceType) {\r
   case EFI_SCSI_TYPE_DISK:\r
     ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200;\r
+    MustReadCapacity = TRUE;\r
     break;\r
 \r
   case EFI_SCSI_TYPE_CDROM:\r
     ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800;\r
+    MustReadCapacity = FALSE;\r
     break;\r
   }\r
   //\r
@@ -248,7 +254,7 @@ ScsiDiskDriverBindingStart (
   // The second parameter "TRUE" means must\r
   // retrieve media capacity\r
   //\r
-  Status = ScsiDiskDetectMedia (ScsiDiskDevice, TRUE, &Temp);\r
+  Status = ScsiDiskDetectMedia (ScsiDiskDevice, MustReadCapacity, &Temp);\r
   if (!EFI_ERROR (Status)) {\r
     //\r
     // Determine if Block IO should be produced on this controller handle\r
@@ -398,12 +404,22 @@ ScsiDiskReset (
 \r
   Status          = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);\r
 \r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto Done;\r
+  }\r
+\r
   if (!ExtendedVerification) {\r
     goto Done;\r
   }\r
 \r
   Status = ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);\r
 \r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto Done;\r
+  }\r
+\r
 Done:\r
   gBS->RestoreTPL (OldTpl);\r
   return Status;\r
@@ -444,17 +460,8 @@ ScsiDiskReadBlocks (
   BOOLEAN             MediaChange;\r
   EFI_TPL             OldTpl;\r
 \r
-  MediaChange = FALSE;\r
-  if (Buffer == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (BufferSize == 0) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
-\r
+  MediaChange    = FALSE;\r
+  OldTpl         = gBS->RaiseTPL (TPL_CALLBACK);\r
   ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (This);\r
 \r
   if (!IS_DEVICE_FIXED(ScsiDiskDevice)) {\r
@@ -492,6 +499,16 @@ ScsiDiskReadBlocks (
     goto Done;\r
   }\r
 \r
+  if (Buffer == NULL) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
+  }\r
+\r
+  if (BufferSize == 0) {\r
+    Status = EFI_SUCCESS;\r
+    goto Done;\r
+  }\r
+\r
   if (BufferSize % BlockSize != 0) {\r
     Status = EFI_BAD_BUFFER_SIZE;\r
     goto Done;\r
@@ -559,17 +576,8 @@ ScsiDiskWriteBlocks (
   BOOLEAN             MediaChange;\r
   EFI_TPL             OldTpl;\r
 \r
-  MediaChange = FALSE;\r
-  if (Buffer == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (BufferSize == 0) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
-\r
+  MediaChange    = FALSE;\r
+  OldTpl         = gBS->RaiseTPL (TPL_CALLBACK);\r
   ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (This);\r
 \r
   if (!IS_DEVICE_FIXED(ScsiDiskDevice)) {\r
@@ -607,6 +615,16 @@ ScsiDiskWriteBlocks (
     goto Done;\r
   }\r
 \r
+  if (BufferSize == 0) {\r
+    Status = EFI_SUCCESS;\r
+    goto Done;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
+  }\r
+\r
   if (BufferSize % BlockSize != 0) {\r
     Status = EFI_BAD_BUFFER_SIZE;\r
     goto Done;\r
@@ -679,26 +697,50 @@ ScsiDiskDetectMedia (
   )\r
 {\r
   EFI_STATUS          Status;\r
-  EFI_STATUS          ReadCapacityStatus;\r
   EFI_SCSI_SENSE_DATA *SenseData;\r
   UINTN               NumberOfSenseKeys;\r
   BOOLEAN             NeedRetry;\r
   BOOLEAN             NeedReadCapacity;\r
-  UINT8               Index;\r
+  UINT8               Retry;\r
   UINT8               MaxRetry;\r
   EFI_BLOCK_IO_MEDIA  OldMedia;\r
   UINTN               Action;\r
+  EFI_EVENT           TimeoutEvt;\r
 \r
   Status              = EFI_SUCCESS;\r
-  ReadCapacityStatus  = EFI_SUCCESS;\r
   SenseData           = NULL;\r
   NumberOfSenseKeys   = 0;\r
+  Retry               = 0;\r
+  MaxRetry            = 3;\r
+  Action              = ACTION_NO_ACTION;\r
   NeedReadCapacity    = FALSE;\r
-  CopyMem (&OldMedia, ScsiDiskDevice->BlkIo.Media, sizeof (OldMedia));\r
   *MediaChange        = FALSE;\r
-  MaxRetry            = 3;\r
+  TimeoutEvt          = NULL;\r
 \r
-  for (Index = 0; Index < MaxRetry; Index++) {\r
+  CopyMem (&OldMedia, ScsiDiskDevice->BlkIo.Media, sizeof (OldMedia));\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER,\r
+                  TPL_CALLBACK,\r
+                  NULL,\r
+                  NULL,\r
+                  &TimeoutEvt\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(120));\r
+  if (EFI_ERROR (Status)) {\r
+    goto EXIT;\r
+  }\r
+\r
+  //\r
+  // Sending Test_Unit cmd to poll device status.\r
+  // If the sense data shows the drive is not ready or reset before, we need poll the device status again.\r
+  // We limit the upper boundary to 120 seconds.\r
+  //\r
+  while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {\r
     Status = ScsiDiskTestUnitReady (\r
               ScsiDiskDevice,\r
               &NeedRetry,\r
@@ -706,34 +748,36 @@ ScsiDiskDetectMedia (
               &NumberOfSenseKeys\r
               );\r
     if (!EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
-\r
-    if (!NeedRetry) {\r
-      return Status;\r
+      Status = DetectMediaParsingSenseKeys (\r
+                 ScsiDiskDevice,\r
+                 SenseData,\r
+                 NumberOfSenseKeys,\r
+                 &Action\r
+                 );\r
+      if (EFI_ERROR (Status)) {\r
+        goto EXIT;\r
+      } else if (Action == ACTION_RETRY_COMMAND_LATER) {\r
+        continue;\r
+      } else {\r
+        break;\r
+      }\r
+    } else {\r
+      Retry++;\r
+      if (!NeedRetry || (Retry >= MaxRetry)) {\r
+        goto EXIT;\r
+      }\r
     }\r
   }\r
 \r
-  if ((Index == MaxRetry) && EFI_ERROR (Status)) {\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  Status = DetectMediaParsingSenseKeys (\r
-            ScsiDiskDevice,\r
-            SenseData,\r
-            NumberOfSenseKeys,\r
-            &Action\r
-            );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto EXIT;\r
   }\r
+\r
   //\r
   // ACTION_NO_ACTION: need not read capacity\r
   // other action code: need read capacity\r
   //\r
-  if (Action == ACTION_NO_ACTION) {\r
-    NeedReadCapacity = FALSE;\r
-  } else {\r
+  if (Action == ACTION_READ_CAPACITY) {\r
     NeedReadCapacity = TRUE;\r
   }\r
 \r
@@ -745,63 +789,45 @@ ScsiDiskDetectMedia (
     //\r
     // retrieve media information\r
     //\r
-    MaxRetry = 3;\r
-    for (Index = 0; Index < MaxRetry; Index++) {\r
-\r
-      ReadCapacityStatus = ScsiDiskReadCapacity (\r
-                            ScsiDiskDevice,\r
-                            &NeedRetry,\r
-                            &SenseData,\r
-                            &NumberOfSenseKeys\r
-                            );\r
-      if (EFI_ERROR (ReadCapacityStatus) && !NeedRetry) {\r
-        return EFI_DEVICE_ERROR;\r
-      }\r
-      //\r
-      // analyze sense key to action\r
-      //\r
-      Status = DetectMediaParsingSenseKeys (\r
-                ScsiDiskDevice,\r
-                SenseData,\r
-                NumberOfSenseKeys,\r
-                &Action\r
-                );\r
-      //\r
-      // if Status is error, it may indicate crisis error,\r
-      // so return without retry.\r
-      //\r
-      if (EFI_ERROR (Status)) {\r
-        return Status;\r
-      }\r
-\r
-      switch (Action) {\r
-      case ACTION_NO_ACTION:\r
-        //\r
-        // no retry\r
-        //\r
-        Index = MaxRetry;\r
-        break;\r
-\r
-      case ACTION_RETRY_COMMAND_LATER:\r
-        //\r
-        // retry the ReadCapacity later and continuously, until the condition\r
-        // no longer emerges.\r
-        // stall time is 100000us, or say 0.1 second.\r
-        //\r
-        gBS->Stall (100000);\r
-        Index = 0;\r
-        break;\r
-\r
-      default:\r
+    for (Retry = 0; Retry < MaxRetry; Retry++) {\r
+      Status = ScsiDiskReadCapacity (\r
+                 ScsiDiskDevice,\r
+                 &NeedRetry,\r
+                 &SenseData,\r
+                 &NumberOfSenseKeys\r
+                 );\r
+      if (!EFI_ERROR (Status)) {\r
         //\r
-        // other cases, just retry the command\r
+        // analyze sense key to action\r
         //\r
-        break;\r
+        Status = DetectMediaParsingSenseKeys (\r
+                   ScsiDiskDevice,\r
+                   SenseData,\r
+                   NumberOfSenseKeys,\r
+                   &Action\r
+                   );\r
+        if (EFI_ERROR (Status)) {\r
+          //\r
+          // if Status is error, it may indicate crisis error,\r
+          // so return without retry.\r
+          //\r
+          goto EXIT;\r
+        } else if (Action == ACTION_RETRY_COMMAND_LATER) {\r
+          Retry = 0;\r
+          continue;\r
+        } else {\r
+          break;\r
+        }\r
+      } else {   \r
+        Retry++;\r
+        if (!NeedRetry || (Retry >= MaxRetry)) {\r
+          goto EXIT;\r
+        }\r
       }\r
     }\r
 \r
-    if ((Index == MaxRetry) && EFI_ERROR (ReadCapacityStatus)) {\r
-      return EFI_DEVICE_ERROR;\r
+    if (EFI_ERROR (Status)) {\r
+      goto EXIT;\r
     }\r
   }\r
 \r
@@ -843,7 +869,11 @@ ScsiDiskDetectMedia (
     *MediaChange = TRUE;\r
   }\r
 \r
-  return EFI_SUCCESS;\r
+EXIT:\r
+  if (TimeoutEvt != NULL) {\r
+    gBS->CloseEvent (TimeoutEvt);\r
+  }\r
+  return Status;\r
 }\r
 \r
 \r
@@ -863,15 +893,18 @@ ScsiDiskInquiryDevice (
      OUT  BOOLEAN         *NeedRetry\r
   )\r
 {\r
-  UINT32              InquiryDataLength;\r
-  UINT8               SenseDataLength;\r
-  UINT8               HostAdapterStatus;\r
-  UINT8               TargetStatus;\r
-  EFI_SCSI_SENSE_DATA *SenseDataArray;\r
-  UINTN               NumberOfSenseKeys;\r
-  EFI_STATUS          Status;\r
-  UINT8               MaxRetry;\r
-  UINT8               Index;\r
+  UINT32                                InquiryDataLength;\r
+  UINT8                                 SenseDataLength;\r
+  UINT8                                 HostAdapterStatus;\r
+  UINT8                                 TargetStatus;\r
+  EFI_SCSI_SENSE_DATA                   *SenseDataArray;\r
+  UINTN                                 NumberOfSenseKeys;\r
+  EFI_STATUS                            Status;\r
+  UINT8                                 MaxRetry;\r
+  UINT8                                 Index;\r
+  EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE SupportedVpdPages;\r
+  EFI_SCSI_BLOCK_LIMITS_VPD_PAGE        BlockLimits;\r
+  UINTN                                 PageLength;\r
 \r
   InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA);\r
   SenseDataLength   = 0;\r
@@ -891,27 +924,86 @@ ScsiDiskInquiryDevice (
     // no need to check HostAdapterStatus and TargetStatus\r
     //\r
   if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) {\r
-     ParseInquiryData (ScsiDiskDevice);\r
-     return EFI_SUCCESS;\r
\r
-   } else if (Status == EFI_NOT_READY) {\r
-     *NeedRetry = TRUE;\r
-     return EFI_DEVICE_ERROR;\r
\r
-   } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {\r
-     *NeedRetry = FALSE;\r
-     return EFI_DEVICE_ERROR;\r
-   }\r
-   //\r
-   // go ahead to check HostAdapterStatus and TargetStatus\r
-   // (EFI_TIMEOUT, EFI_DEVICE_ERROR)\r
-   //\r
+    ParseInquiryData (ScsiDiskDevice);\r
+\r
+    if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_DISK) {\r
+      //\r
+      // Check whether the device supports Block Limits VPD page (0xB0)\r
+      //\r
+      ZeroMem (&SupportedVpdPages, sizeof (SupportedVpdPages));\r
+      InquiryDataLength = sizeof (SupportedVpdPages);\r
+      SenseDataLength   = 0;\r
+      Status = ScsiInquiryCommandEx (\r
+                 ScsiDiskDevice->ScsiIo,\r
+                 EFI_TIMER_PERIOD_SECONDS (1),\r
+                 NULL,\r
+                 &SenseDataLength,\r
+                 &HostAdapterStatus,\r
+                 &TargetStatus,\r
+                 (VOID *) &SupportedVpdPages,\r
+                 &InquiryDataLength,\r
+                 TRUE,\r
+                 EFI_SCSI_PAGE_CODE_SUPPORTED_VPD\r
+                 );\r
+      if (!EFI_ERROR (Status)) {\r
+        PageLength = (SupportedVpdPages.PageLength2 << 8)\r
+                   |  SupportedVpdPages.PageLength1;\r
+        for (Index = 0; Index < PageLength; Index++) {\r
+          if (SupportedVpdPages.SupportedVpdPageList[Index] == EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD) {\r
+            break;\r
+          }\r
+        }\r
+\r
+        //\r
+        // Query the Block Limits VPD page\r
+        //\r
+        if (Index < PageLength) {\r
+          ZeroMem (&BlockLimits, sizeof (BlockLimits));\r
+          InquiryDataLength = sizeof (BlockLimits);\r
+          SenseDataLength   = 0;\r
+          Status = ScsiInquiryCommandEx (\r
+                     ScsiDiskDevice->ScsiIo,\r
+                     EFI_TIMER_PERIOD_SECONDS (1),\r
+                     NULL,\r
+                     &SenseDataLength,\r
+                     &HostAdapterStatus,\r
+                     &TargetStatus,\r
+                     (VOID *) &BlockLimits,\r
+                     &InquiryDataLength,\r
+                     TRUE,\r
+                     EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD\r
+                     );\r
+          if (!EFI_ERROR (Status)) {\r
+            ScsiDiskDevice->BlkIo.Media->OptimalTransferLengthGranularity = \r
+              (BlockLimits.OptimalTransferLengthGranularity2 << 8) |\r
+               BlockLimits.OptimalTransferLengthGranularity1;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  if (!EFI_ERROR (Status)) {\r
+    return EFI_SUCCESS;\r
+\r
+  } else if (Status == EFI_NOT_READY) {\r
+    *NeedRetry = TRUE;\r
+    return EFI_DEVICE_ERROR;\r
  \r
-   Status = CheckHostAdapterStatus (HostAdapterStatus);\r
-   if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {\r
-     *NeedRetry = TRUE;\r
-     return EFI_DEVICE_ERROR;\r
-   } else if (Status == EFI_DEVICE_ERROR) {\r
+  } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {\r
+    *NeedRetry = FALSE;\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  //\r
+  // go ahead to check HostAdapterStatus and TargetStatus\r
+  // (EFI_TIMEOUT, EFI_DEVICE_ERROR)\r
+  //\r
+\r
+  Status = CheckHostAdapterStatus (HostAdapterStatus);\r
+  if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {\r
+    *NeedRetry = TRUE;\r
+    return EFI_DEVICE_ERROR;\r
+  } else if (Status == EFI_DEVICE_ERROR) {\r
       //\r
       // reset the scsi channel\r
       //\r
@@ -1106,7 +1198,9 @@ DetectMediaParsingSenseKeys (
   *Action = ACTION_READ_CAPACITY;\r
 \r
   if (NumberOfSenseKeys == 0) {\r
-    *Action = ACTION_NO_ACTION;\r
+    if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {\r
+      *Action = ACTION_NO_ACTION;\r
+    }\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -1114,7 +1208,9 @@ DetectMediaParsingSenseKeys (
     //\r
     // No Sense Key returned from last submitted command\r
     //\r
-    *Action = ACTION_NO_ACTION;\r
+    if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {\r
+      *Action = ACTION_NO_ACTION;\r
+    }\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -1130,13 +1226,20 @@ DetectMediaParsingSenseKeys (
     return EFI_SUCCESS;\r
   }\r
 \r
+  if (ScsiDiskIsResetBefore (SenseData, NumberOfSenseKeys)) {\r
+    *Action = ACTION_RETRY_COMMAND_LATER;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if (ScsiDiskIsMediaError (SenseData, NumberOfSenseKeys)) {\r
     ScsiDiskDevice->BlkIo.Media->MediaPresent = FALSE;\r
     ScsiDiskDevice->BlkIo.Media->LastBlock    = 0;\r
+    *Action = ACTION_NO_ACTION;\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
   if (ScsiDiskIsHardwareError (SenseData, NumberOfSenseKeys)) {\r
+    *Action = ACTION_NO_ACTION;\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -1145,7 +1248,7 @@ DetectMediaParsingSenseKeys (
       *Action = ACTION_RETRY_COMMAND_LATER;\r
       return EFI_SUCCESS;\r
     }\r
-\r
+    *Action = ACTION_NO_ACTION;\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -1162,7 +1265,7 @@ DetectMediaParsingSenseKeys (
   @param  NumberOfSenseKeys  The number of sense key\r
 \r
   @retval EFI_DEVICE_ERROR   Indicates that error occurs\r
-  @retval EFI_SUCCESS        Successfully to read capacity\r
+  @retval EFI_SUCCESS        Successfully to read capacity or sense data is received.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1180,7 +1283,6 @@ ScsiDiskReadCapacity (
   UINT8                         Index;\r
   UINT8                         MaxRetry;\r
   UINT8                         SenseDataLength;\r
-  UINT8                         ScsiVersion;\r
   UINT32                        DataLength10;\r
   UINT32                        DataLength16;\r
   EFI_SCSI_DISK_CAPACITY_DATA   CapacityData10;\r
@@ -1195,40 +1297,47 @@ ScsiDiskReadCapacity (
 \r
   *NumberOfSenseKeys  = 0;\r
   *NeedRetry          = FALSE;\r
-  ScsiVersion         = (UINT8)(ScsiDiskDevice->InquiryData.Version & 0x03);\r
 \r
-  if (ScsiVersion < SCSI_COMMAND_VERSION_3) {\r
+  //\r
+  // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh, \r
+  // 16 byte command should be used to access large hard disk >2TB\r
+  //\r
+  CommandStatus = ScsiReadCapacityCommand (\r
+                    ScsiDiskDevice->ScsiIo,\r
+                    EFI_TIMER_PERIOD_SECONDS(1),\r
+                    NULL,\r
+                    &SenseDataLength,\r
+                    &HostAdapterStatus,\r
+                    &TargetStatus,\r
+                    (VOID *) &CapacityData10,\r
+                    &DataLength10,\r
+                    FALSE\r
+                    );\r
+\r
+  ScsiDiskDevice->Cdb16Byte = FALSE;\r
+  if ((!EFI_ERROR (CommandStatus)) && (CapacityData10.LastLba3 == 0xff) && (CapacityData10.LastLba2 == 0xff) &&\r
+      (CapacityData10.LastLba1 == 0xff) && (CapacityData10.LastLba0 == 0xff)) {\r
+    //\r
+    // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB\r
     //\r
-    // submit Read Capacity(10) Command. in this call,not request sense data\r
+    ScsiDiskDevice->Cdb16Byte = TRUE;\r
     //\r
-    CommandStatus = ScsiReadCapacityCommand (\r
+    // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock\r
+    // and LowestAlignedLba\r
+    //\r
+    CommandStatus = ScsiReadCapacity16Command (\r
                       ScsiDiskDevice->ScsiIo,\r
-                      EFI_TIMER_PERIOD_SECONDS(1),\r
+                      EFI_TIMER_PERIOD_SECONDS (1),\r
                       NULL,\r
                       &SenseDataLength,\r
                       &HostAdapterStatus,\r
                       &TargetStatus,\r
-                      (VOID *) &CapacityData10,\r
-                      &DataLength10,\r
+                      (VOID *) &CapacityData16,\r
+                      &DataLength16,\r
                       FALSE\r
                       );\r
-    } else {\r
-      //\r
-      // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock\r
-      // and LowestAlignedLba\r
-      //\r
-      CommandStatus = ScsiReadCapacity16Command (\r
-                        ScsiDiskDevice->ScsiIo,\r
-                        EFI_TIMER_PERIOD_SECONDS (1),\r
-                        NULL,\r
-                        &SenseDataLength,\r
-                        &HostAdapterStatus,\r
-                        &TargetStatus,\r
-                        (VOID *) &CapacityData16,\r
-                        &DataLength16,\r
-                        FALSE\r
-                        );\r
-    }\r
+  }\r
+\r
     //\r
     // no need to check HostAdapterStatus and TargetStatus\r
     //\r
@@ -1293,8 +1402,7 @@ ScsiDiskReadCapacity (
               TRUE\r
               );\r
     if (!EFI_ERROR (Status)) {\r
-      *NeedRetry = TRUE;\r
-      return EFI_DEVICE_ERROR;\r
+      return EFI_SUCCESS;\r
     }\r
 \r
     if (!*NeedRetry) {\r
@@ -1423,7 +1531,7 @@ ScsiDiskRequestSenseKeys (
   UINT8               TargetStatus;\r
 \r
   FallStatus      = EFI_SUCCESS;\r
-  SenseDataLength = sizeof (EFI_SCSI_SENSE_DATA);\r
+  SenseDataLength = (UINT8) sizeof (EFI_SCSI_SENSE_DATA);\r
 \r
   ZeroMem (\r
     ScsiDiskDevice->SenseData,\r
@@ -1502,15 +1610,9 @@ GetMediaInfo (
   IN     EFI_SCSI_DISK_CAPACITY_DATA16  *Capacity16\r
   )\r
 {\r
-  UINT8       ScsiVersion;\r
   UINT8       *Ptr;\r
 \r
-  ScsiVersion    = (UINT8)(ScsiDiskDevice->InquiryData.Version & 0x03);\r
-  ScsiDiskDevice->BlkIo.Media->LowestAlignedLba               = 0;\r
-  ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock  = 1;\r
-  \r
-\r
-  if (ScsiVersion < SCSI_COMMAND_VERSION_3) {\r
+  if (!ScsiDiskDevice->Cdb16Byte) {\r
     ScsiDiskDevice->BlkIo.Media->LastBlock =  (Capacity10->LastLba3 << 24) |\r
                                               (Capacity10->LastLba2 << 16) |\r
                                               (Capacity10->LastLba1 << 8)  |\r
@@ -1520,9 +1622,9 @@ GetMediaInfo (
                                              (Capacity10->BlockSize2 << 16) | \r
                                              (Capacity10->BlockSize1 << 8)  |\r
                                               Capacity10->BlockSize0;\r
-    ScsiDiskDevice->BlkIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION;      \r
+    ScsiDiskDevice->BlkIo.Media->LowestAlignedLba               = 0;\r
+    ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock  = 0;\r
   } else {\r
-\r
     Ptr = (UINT8*)&ScsiDiskDevice->BlkIo.Media->LastBlock;\r
     *Ptr++ = Capacity16->LastLba0;\r
     *Ptr++ = Capacity16->LastLba1;\r
@@ -1532,18 +1634,17 @@ GetMediaInfo (
     *Ptr++ = Capacity16->LastLba5;\r
     *Ptr++ = Capacity16->LastLba6;\r
     *Ptr   = Capacity16->LastLba7;\r
-  \r
+\r
     ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity16->BlockSize3 << 24) |\r
                                              (Capacity16->BlockSize2 << 16) | \r
                                              (Capacity16->BlockSize1 << 8)  |\r
                                               Capacity16->BlockSize0;\r
 \r
-    ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = (Capacity16->LowestAlignLogic2 << 8)|(Capacity16->LowestAlignLogic1);\r
-    ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock  = Capacity16->LogicPerPhysical;\r
-    ScsiDiskDevice->BlkIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;  \r
+    ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = (Capacity16->LowestAlignLogic2 << 8) |\r
+                                                     Capacity16->LowestAlignLogic1;\r
+    ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock  = (1 << Capacity16->LogicPerPhysical);\r
   }\r
 \r
-\r
   ScsiDiskDevice->BlkIo.Media->MediaPresent = TRUE;\r
   \r
   if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_DISK) {\r
@@ -1591,7 +1692,6 @@ ScsiDiskReadSectors (
   )\r
 {\r
   UINTN               BlocksRemaining;\r
-  UINT32              Lba32;\r
   UINT8               *PtrBuffer;\r
   UINT32              BlockSize;\r
   UINT32              ByteCount;\r
@@ -1612,21 +1712,27 @@ ScsiDiskReadSectors (
 \r
   BlocksRemaining   = NumberOfBlocks;\r
   BlockSize         = ScsiDiskDevice->BlkIo.Media->BlockSize;\r
+  \r
   //\r
-  // limit the data bytes that can be transferred by one Read(10) Command\r
+  // limit the data bytes that can be transferred by one Read(10) or Read(16) Command\r
   //\r
-  MaxBlock  = 65536;\r
+  if (!ScsiDiskDevice->Cdb16Byte) {\r
+    MaxBlock         = 0xFFFF;\r
+  } else {\r
+    MaxBlock         = 0xFFFFFFFF;\r
+  }\r
 \r
   PtrBuffer = Buffer;\r
-  Lba32     = (UINT32) Lba;\r
 \r
   while (BlocksRemaining > 0) {\r
 \r
     if (BlocksRemaining <= MaxBlock) {\r
-\r
-      SectorCount = (UINT16) BlocksRemaining;\r
+      if (!ScsiDiskDevice->Cdb16Byte) {\r
+        SectorCount = (UINT16) BlocksRemaining;\r
+      } else {\r
+        SectorCount = (UINT32) BlocksRemaining;\r
+      }\r
     } else {\r
-\r
       SectorCount = MaxBlock;\r
     }\r
 \r
@@ -1635,18 +1741,31 @@ ScsiDiskReadSectors (
 \r
     MaxRetry  = 2;\r
     for (Index = 0; Index < MaxRetry; Index++) {\r
-\r
-      Status = ScsiDiskRead10 (\r
-                ScsiDiskDevice,\r
-                &NeedRetry,\r
-                &SenseData,\r
-                &NumberOfSenseKeys,\r
-                Timeout,\r
-                PtrBuffer,\r
-                &ByteCount,\r
-                Lba32,\r
-                SectorCount\r
-                );\r
+      if (!ScsiDiskDevice->Cdb16Byte) {\r
+        Status = ScsiDiskRead10 (\r
+                  ScsiDiskDevice,\r
+                  &NeedRetry,\r
+                  &SenseData,\r
+                  &NumberOfSenseKeys,\r
+                  Timeout,\r
+                  PtrBuffer,\r
+                  &ByteCount,\r
+                  (UINT32) Lba,\r
+                  SectorCount\r
+                  );\r
+      } else {\r
+        Status = ScsiDiskRead16 (\r
+                  ScsiDiskDevice,\r
+                  &NeedRetry,\r
+                  &SenseData,\r
+                  &NumberOfSenseKeys,\r
+                  Timeout,\r
+                  PtrBuffer,\r
+                  &ByteCount,\r
+                  Lba,\r
+                  SectorCount\r
+                  );\r
+      }\r
       if (!EFI_ERROR (Status)) {\r
         break;\r
       }\r
@@ -1666,7 +1785,7 @@ ScsiDiskReadSectors (
     //\r
     SectorCount = ByteCount / BlockSize;\r
 \r
-    Lba32 += SectorCount;\r
+    Lba += SectorCount;\r
     PtrBuffer = PtrBuffer + SectorCount * BlockSize;\r
     BlocksRemaining -= SectorCount;\r
   }\r
@@ -1695,7 +1814,6 @@ ScsiDiskWriteSectors (
   )\r
 {\r
   UINTN               BlocksRemaining;\r
-  UINT32              Lba32;\r
   UINT8               *PtrBuffer;\r
   UINT32              BlockSize;\r
   UINT32              ByteCount;\r
@@ -1716,21 +1834,27 @@ ScsiDiskWriteSectors (
 \r
   BlocksRemaining   = NumberOfBlocks;\r
   BlockSize         = ScsiDiskDevice->BlkIo.Media->BlockSize;\r
+\r
   //\r
-  // limit the data bytes that can be transferred by one Write(10) Command\r
+  // limit the data bytes that can be transferred by one Read(10) or Read(16) Command\r
   //\r
-  MaxBlock  = 65536;\r
+  if (!ScsiDiskDevice->Cdb16Byte) {\r
+    MaxBlock         = 0xFFFF;\r
+  } else {\r
+    MaxBlock         = 0xFFFFFFFF;\r
+  }\r
 \r
   PtrBuffer = Buffer;\r
-  Lba32     = (UINT32) Lba;\r
 \r
   while (BlocksRemaining > 0) {\r
 \r
     if (BlocksRemaining <= MaxBlock) {\r
-\r
-      SectorCount = (UINT16) BlocksRemaining;\r
+      if (!ScsiDiskDevice->Cdb16Byte) {\r
+        SectorCount = (UINT16) BlocksRemaining;\r
+      } else {\r
+        SectorCount = (UINT32) BlocksRemaining;\r
+      }\r
     } else {\r
-\r
       SectorCount = MaxBlock;\r
     }\r
 \r
@@ -1738,17 +1862,31 @@ ScsiDiskWriteSectors (
     Timeout   = EFI_TIMER_PERIOD_SECONDS (2);\r
     MaxRetry  = 2;\r
     for (Index = 0; Index < MaxRetry; Index++) {\r
-      Status = ScsiDiskWrite10 (\r
-                ScsiDiskDevice,\r
-                &NeedRetry,\r
-                &SenseData,\r
-                &NumberOfSenseKeys,\r
-                Timeout,\r
-                PtrBuffer,\r
-                &ByteCount,\r
-                Lba32,\r
-                SectorCount\r
-                );\r
+      if (!ScsiDiskDevice->Cdb16Byte) {\r
+        Status = ScsiDiskWrite10 (\r
+                  ScsiDiskDevice,\r
+                  &NeedRetry,\r
+                  &SenseData,\r
+                  &NumberOfSenseKeys,\r
+                  Timeout,\r
+                  PtrBuffer,\r
+                  &ByteCount,\r
+                  (UINT32) Lba,\r
+                  SectorCount\r
+                  );\r
+      } else {\r
+        Status = ScsiDiskWrite16 (\r
+                  ScsiDiskDevice,\r
+                  &NeedRetry,\r
+                  &SenseData,\r
+                  &NumberOfSenseKeys,\r
+                  Timeout,\r
+                  PtrBuffer,\r
+                  &ByteCount,\r
+                  Lba,\r
+                  SectorCount\r
+                  );         \r
+        }\r
       if (!EFI_ERROR (Status)) {\r
         break;\r
       }\r
@@ -1766,7 +1904,7 @@ ScsiDiskWriteSectors (
     //\r
     SectorCount = ByteCount / BlockSize;\r
 \r
-    Lba32 += SectorCount;\r
+    Lba += SectorCount;\r
     PtrBuffer = PtrBuffer + SectorCount * BlockSize;\r
     BlocksRemaining -= SectorCount;\r
   }\r
@@ -1776,7 +1914,7 @@ ScsiDiskWriteSectors (
 \r
 \r
 /**\r
-  Submit Read command.\r
+  Submit Read(10) command.\r
 \r
   @param  ScsiDiskDevice     The pointer of ScsiDiskDevice\r
   @param  NeedRetry          The pointer of flag indicates if needs retry if error happens\r
@@ -1828,7 +1966,7 @@ ScsiDiskRead10 (
 \r
 \r
 /**\r
-  Submit Write Command.\r
+  Submit Write(10) Command.\r
 \r
   @param  ScsiDiskDevice     The pointer of ScsiDiskDevice\r
   @param  NeedRetry          The pointer of flag indicates if needs retry if error happens\r
@@ -1880,6 +2018,111 @@ ScsiDiskWrite10 (
 }\r
 \r
 \r
+/**\r
+  Submit Read(16) command.\r
+\r
+  @param  ScsiDiskDevice     The pointer of ScsiDiskDevice\r
+  @param  NeedRetry          The pointer of flag indicates if needs retry if error happens\r
+  @param  SenseDataArray     NOT used yet in this function\r
+  @param  NumberOfSenseKeys  The number of sense key\r
+  @param  Timeout            The time to complete the command\r
+  @param  DataBuffer         The buffer to fill with the read out data\r
+  @param  DataLength         The length of buffer\r
+  @param  StartLba           The start logic block address\r
+  @param  SectorSize         The size of sector\r
+\r
+  @return  EFI_STATUS is returned by calling ScsiRead10Command().\r
+**/\r
+EFI_STATUS\r
+ScsiDiskRead16 (\r
+  IN     SCSI_DISK_DEV         *ScsiDiskDevice,\r
+     OUT BOOLEAN               *NeedRetry,\r
+     OUT EFI_SCSI_SENSE_DATA   **SenseDataArray,   OPTIONAL\r
+     OUT UINTN                 *NumberOfSenseKeys,\r
+  IN     UINT64                Timeout,\r
+     OUT UINT8                 *DataBuffer,\r
+  IN OUT UINT32                *DataLength,\r
+  IN     UINT64                StartLba,\r
+  IN     UINT32                SectorSize\r
+  )\r
+{\r
+  UINT8       SenseDataLength;\r
+  EFI_STATUS  Status;\r
+  UINT8       HostAdapterStatus;\r
+  UINT8       TargetStatus;\r
+\r
+  *NeedRetry          = FALSE;\r
+  *NumberOfSenseKeys  = 0;\r
+  SenseDataLength     = 0;\r
+  Status = ScsiRead16Command (\r
+            ScsiDiskDevice->ScsiIo,\r
+            Timeout,\r
+            NULL,\r
+            &SenseDataLength,\r
+            &HostAdapterStatus,\r
+            &TargetStatus,\r
+            DataBuffer,\r
+            DataLength,\r
+            StartLba,\r
+            SectorSize\r
+            );\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Submit Write(16) Command.\r
+\r
+  @param  ScsiDiskDevice     The pointer of ScsiDiskDevice\r
+  @param  NeedRetry          The pointer of flag indicates if needs retry if error happens\r
+  @param  SenseDataArray     NOT used yet in this function\r
+  @param  NumberOfSenseKeys  The number of sense key\r
+  @param  Timeout            The time to complete the command\r
+  @param  DataBuffer         The buffer to fill with the read out data\r
+  @param  DataLength         The length of buffer\r
+  @param  StartLba           The start logic block address\r
+  @param  SectorSize         The size of sector\r
+\r
+  @return  EFI_STATUS is returned by calling ScsiWrite10Command().\r
+\r
+**/\r
+EFI_STATUS\r
+ScsiDiskWrite16 (\r
+  IN     SCSI_DISK_DEV         *ScsiDiskDevice,\r
+     OUT BOOLEAN               *NeedRetry,\r
+     OUT EFI_SCSI_SENSE_DATA   **SenseDataArray,   OPTIONAL\r
+     OUT UINTN                 *NumberOfSenseKeys,\r
+  IN     UINT64                Timeout,\r
+  IN     UINT8                 *DataBuffer,\r
+  IN OUT UINT32                *DataLength,\r
+  IN     UINT64                StartLba,\r
+  IN     UINT32                SectorSize\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       SenseDataLength;\r
+  UINT8       HostAdapterStatus;\r
+  UINT8       TargetStatus;\r
+\r
+  *NeedRetry          = FALSE;\r
+  *NumberOfSenseKeys  = 0;\r
+  SenseDataLength     = 0;\r
+  Status = ScsiWrite16Command (\r
+            ScsiDiskDevice->ScsiIo,\r
+            Timeout,\r
+            NULL,\r
+            &SenseDataLength,\r
+            &HostAdapterStatus,\r
+            &TargetStatus,\r
+            DataBuffer,\r
+            DataLength,\r
+            StartLba,\r
+            SectorSize\r
+            );\r
+  return Status;\r
+}\r
+\r
+\r
 /**\r
   Check sense key to find if media presents.\r
 \r
@@ -2372,9 +2615,9 @@ GetParentProtocol (
   This function is used by the IDE bus driver to get inquiry data.  Data format\r
   of Identify data is defined by the Interface GUID.\r
 \r
-  @param[in]     This              Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
-  @param[in,out] InquiryData       Pointer to a buffer for the inquiry data.\r
-  @param[in,out] InquiryDataSize   Pointer to the value for the inquiry data size.\r
+  @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
+  @param[in, out] InquiryData       Pointer to a buffer for the inquiry data.\r
+  @param[in, out] InquiryDataSize   Pointer to the value for the inquiry data size.\r
 \r
   @retval EFI_SUCCESS            The command was accepted without any errors.\r
   @retval EFI_NOT_FOUND          Device does not support this data class \r
@@ -2411,10 +2654,10 @@ ScsiDiskInfoInquiry (
   This function is used by the IDE bus driver to get identify data.  Data format\r
   of Identify data is defined by the Interface GUID.\r
 \r
-  @param[in]     This               Pointer to the EFI_DISK_INFO_PROTOCOL \r
+  @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL \r
                                     instance.\r
-  @param[in,out] IdentifyData       Pointer to a buffer for the identify data.\r
-  @param[in,out] IdentifyDataSize   Pointer to the value for the identify data\r
+  @param[in, out] IdentifyData      Pointer to a buffer for the identify data.\r
+  @param[in, out] IdentifyDataSize  Pointer to the value for the identify data\r
                                     size.\r
 \r
   @retval EFI_SUCCESS            The command was accepted without any errors.\r
@@ -2458,10 +2701,10 @@ ScsiDiskInfoIdentify (
   This function is used by the IDE bus driver to get sense data. \r
   Data format of Sense data is defined by the Interface GUID.\r
 \r
-  @param[in]     This              Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
-  @param[in,out] SenseData         Pointer to the SenseData.\r
-  @param[in,out] SenseDataSize     Size of SenseData in bytes.\r
-  @param[out]    SenseDataNumber   Pointer to the value for the sense data size.\r
+  @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
+  @param[in, out] SenseData         Pointer to the SenseData.\r
+  @param[in, out] SenseDataSize     Size of SenseData in bytes.\r
+  @param[out]     SenseDataNumber   Pointer to the value for the sense data size.\r
 \r
   @retval EFI_SUCCESS            The command was accepted without any errors.\r
   @retval EFI_NOT_FOUND          Device does not support this data class.\r
@@ -2548,7 +2791,7 @@ AtapiIdentifyDevice (
   Cdb[0] = ATA_CMD_IDENTIFY_DEVICE;\r
   CommandPacket.Timeout = EFI_TIMER_PERIOD_SECONDS (1);\r
   CommandPacket.Cdb = Cdb;\r
-  CommandPacket.CdbLength = sizeof (Cdb);\r
+  CommandPacket.CdbLength = (UINT8) sizeof (Cdb);\r
   CommandPacket.InDataBuffer = &ScsiDiskDevice->IdentifyData;\r
   CommandPacket.InTransferLength = sizeof (ScsiDiskDevice->IdentifyData);\r
 \r