]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Scsi/ScsiBusDxe/ScsiBus.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Scsi / ScsiBusDxe / ScsiBus.c
index 0802b617268ffc4441097882a0c528e8d4f1596c..d6c965def30b37d3eaf1b927ef29361da7ddcc58 100644 (file)
@@ -2,22 +2,16 @@
   SCSI Bus driver that layers on every SCSI Pass Thru and\r
   Extended SCSI Pass Thru protocol in the system.\r
 \r
   SCSI Bus driver that layers on every SCSI Pass Thru and\r
   Extended SCSI Pass Thru protocol in the system.\r
 \r
-Copyright (c) 2006 - 2015, 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
+Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1985 - 2022, American Megatrends International LLC.<BR>\r
 \r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 \r
 **/\r
 \r
-\r
 #include "ScsiBus.h"\r
 \r
 #include "ScsiBus.h"\r
 \r
-\r
-EFI_DRIVER_BINDING_PROTOCOL gSCSIBusDriverBinding = {\r
+EFI_DRIVER_BINDING_PROTOCOL  gSCSIBusDriverBinding = {\r
   SCSIBusDriverBindingSupported,\r
   SCSIBusDriverBindingStart,\r
   SCSIBusDriverBindingStop,\r
   SCSIBusDriverBindingSupported,\r
   SCSIBusDriverBindingStart,\r
   SCSIBusDriverBindingStop,\r
@@ -86,8 +80,8 @@ NotifyFunction (
 **/\r
 VOID *\r
 AllocateAlignedBuffer (\r
 **/\r
 VOID *\r
 AllocateAlignedBuffer (\r
-  IN SCSI_IO_DEV              *ScsiIoDevice,\r
-  IN UINTN                    BufferSize\r
+  IN SCSI_IO_DEV  *ScsiIoDevice,\r
+  IN UINTN        BufferSize\r
   )\r
 {\r
   return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), ScsiIoDevice->ScsiIo.IoAlign);\r
   )\r
 {\r
   return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), ScsiIoDevice->ScsiIo.IoAlign);\r
@@ -105,8 +99,8 @@ AllocateAlignedBuffer (
 **/\r
 VOID\r
 FreeAlignedBuffer (\r
 **/\r
 VOID\r
 FreeAlignedBuffer (\r
-  IN VOID                     *Buffer,\r
-  IN UINTN                    BufferSize\r
+  IN VOID   *Buffer,\r
+  IN UINTN  BufferSize\r
   )\r
 {\r
   if (Buffer != NULL) {\r
   )\r
 {\r
   if (Buffer != NULL) {\r
@@ -126,12 +120,12 @@ FreeAlignedBuffer (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-InitializeScsiBus(\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
+InitializeScsiBus (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Install driver model protocol(s).\r
 \r
   //\r
   // Install driver model protocol(s).\r
@@ -149,7 +143,6 @@ InitializeScsiBus(
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Test to see if this driver supports ControllerHandle.\r
 \r
 /**\r
   Test to see if this driver supports ControllerHandle.\r
 \r
@@ -177,18 +170,18 @@ SCSIBusDriverBindingSupported (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
-  EFI_STATUS                      Status;\r
-  EFI_SCSI_PASS_THRU_PROTOCOL     *PassThru;\r
-  EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtPassThru;\r
-  UINT64                          Lun;\r
-  UINT8                           *TargetId;\r
-  SCSI_TARGET_ID                  ScsiTargetId;\r
+  EFI_STATUS                       Status;\r
+  EFI_SCSI_PASS_THRU_PROTOCOL      *PassThru;\r
+  EFI_EXT_SCSI_PASS_THRU_PROTOCOL  *ExtPassThru;\r
+  UINT64                           Lun;\r
+  UINT8                            *TargetId;\r
+  SCSI_TARGET_ID                   ScsiTargetId;\r
 \r
   TargetId = &ScsiTargetId.ScsiId.ExtScsi[0];\r
   SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);\r
 \r
   //\r
 \r
   TargetId = &ScsiTargetId.ScsiId.ExtScsi[0];\r
   SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);\r
 \r
   //\r
-  // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as \r
+  // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as\r
   // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly\r
   // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.\r
   //\r
   // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly\r
   // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.\r
   //\r
@@ -203,7 +196,7 @@ SCSIBusDriverBindingSupported (
 \r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
 \r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
-  } else if (!EFI_ERROR(Status)) {\r
+  } else if (!EFI_ERROR (Status)) {\r
     //\r
     // Check if RemainingDevicePath is NULL or the End of Device Path Node,\r
     // if yes, return EFI_SUCCESS.\r
     //\r
     // Check if RemainingDevicePath is NULL or the End of Device Path Node,\r
     // if yes, return EFI_SUCCESS.\r
@@ -217,7 +210,7 @@ SCSIBusDriverBindingSupported (
              &gEfiExtScsiPassThruProtocolGuid,\r
              This->DriverBindingHandle,\r
              Controller\r
              &gEfiExtScsiPassThruProtocolGuid,\r
              This->DriverBindingHandle,\r
              Controller\r
-             );      \r
+             );\r
       return EFI_SUCCESS;\r
     } else {\r
       //\r
       return EFI_SUCCESS;\r
     } else {\r
       //\r
@@ -232,15 +225,15 @@ SCSIBusDriverBindingSupported (
              &gEfiExtScsiPassThruProtocolGuid,\r
              This->DriverBindingHandle,\r
              Controller\r
              &gEfiExtScsiPassThruProtocolGuid,\r
              This->DriverBindingHandle,\r
              Controller\r
-             );      \r
-      if (!EFI_ERROR(Status)) {\r
+             );\r
+      if (!EFI_ERROR (Status)) {\r
         return EFI_SUCCESS;\r
       }\r
     }\r
   }\r
 \r
   //\r
         return EFI_SUCCESS;\r
       }\r
     }\r
   }\r
 \r
   //\r
-  // Come here in 2 condition: \r
+  // Come here in 2 condition:\r
   // 1. ExtPassThru doesn't exist.\r
   // 2. ExtPassThru exists but RemainingDevicePath is invalid.\r
   //\r
   // 1. ExtPassThru doesn't exist.\r
   // 2. ExtPassThru exists but RemainingDevicePath is invalid.\r
   //\r
@@ -252,22 +245,22 @@ SCSIBusDriverBindingSupported (
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
-  \r
+\r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
   }\r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
   }\r
-  \r
+\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
   // Test RemainingDevicePath is valid or not.\r
   //\r
   if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
     Status = PassThru->GetTargetLun (PassThru, RemainingDevicePath, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
   }\r
   //\r
   // Test RemainingDevicePath is valid or not.\r
   //\r
   if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
     Status = PassThru->GetTargetLun (PassThru, RemainingDevicePath, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
   }\r
-  \r
+\r
   gBS->CloseProtocol (\r
          Controller,\r
          &gEfiScsiPassThruProtocolGuid,\r
   gBS->CloseProtocol (\r
          Controller,\r
          &gEfiScsiPassThruProtocolGuid,\r
@@ -277,7 +270,6 @@ SCSIBusDriverBindingSupported (
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Start this driver on ControllerHandle.\r
 \r
 /**\r
   Start this driver on ControllerHandle.\r
 \r
@@ -305,34 +297,34 @@ SCSIBusDriverBindingStart (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
-  UINT64                                Lun;\r
-  UINT8                                 *TargetId;\r
-  BOOLEAN                               ScanOtherPuns;\r
-  BOOLEAN                               FromFirstTarget;\r
-  BOOLEAN                               ExtScsiSupport;\r
-  EFI_STATUS                            Status;\r
-  EFI_STATUS                            DevicePathStatus;\r
-  EFI_STATUS                            PassThruStatus;\r
-  SCSI_BUS_DEVICE                       *ScsiBusDev;\r
-  SCSI_TARGET_ID                        ScsiTargetId;\r
-  EFI_DEVICE_PATH_PROTOCOL              *ParentDevicePath;\r
-  EFI_SCSI_PASS_THRU_PROTOCOL           *ScsiInterface;\r
-  EFI_EXT_SCSI_PASS_THRU_PROTOCOL       *ExtScsiInterface;\r
-  EFI_SCSI_BUS_PROTOCOL                 *BusIdentify;\r
+  UINT64                           Lun;\r
+  UINT8                            *TargetId;\r
+  BOOLEAN                          ScanOtherPuns;\r
+  BOOLEAN                          FromFirstTarget;\r
+  BOOLEAN                          ExtScsiSupport;\r
+  EFI_STATUS                       Status;\r
+  EFI_STATUS                       DevicePathStatus;\r
+  EFI_STATUS                       PassThruStatus;\r
+  SCSI_BUS_DEVICE                  *ScsiBusDev;\r
+  SCSI_TARGET_ID                   ScsiTargetId;\r
+  EFI_DEVICE_PATH_PROTOCOL         *ParentDevicePath;\r
+  EFI_SCSI_PASS_THRU_PROTOCOL      *ScsiInterface;\r
+  EFI_EXT_SCSI_PASS_THRU_PROTOCOL  *ExtScsiInterface;\r
+  EFI_SCSI_BUS_PROTOCOL            *BusIdentify;\r
 \r
   TargetId        = NULL;\r
   ScanOtherPuns   = TRUE;\r
   FromFirstTarget = FALSE;\r
   ExtScsiSupport  = FALSE;\r
   PassThruStatus  = EFI_SUCCESS;\r
 \r
   TargetId        = NULL;\r
   ScanOtherPuns   = TRUE;\r
   FromFirstTarget = FALSE;\r
   ExtScsiSupport  = FALSE;\r
   PassThruStatus  = EFI_SUCCESS;\r
-  \r
+\r
   TargetId = &ScsiTargetId.ScsiId.ExtScsi[0];\r
   SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);\r
   TargetId = &ScsiTargetId.ScsiId.ExtScsi[0];\r
   SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);\r
-  \r
+\r
   DevicePathStatus = gBS->OpenProtocol (\r
                             Controller,\r
                             &gEfiDevicePathProtocolGuid,\r
   DevicePathStatus = gBS->OpenProtocol (\r
                             Controller,\r
                             &gEfiDevicePathProtocolGuid,\r
-                            (VOID **) &ParentDevicePath,\r
+                            (VOID **)&ParentDevicePath,\r
                             This->DriverBindingHandle,\r
                             Controller,\r
                             EFI_OPEN_PROTOCOL_BY_DRIVER\r
                             This->DriverBindingHandle,\r
                             Controller,\r
                             EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -348,17 +340,17 @@ SCSIBusDriverBindingStart (
     EFI_PROGRESS_CODE,\r
     (EFI_IO_BUS_SCSI | EFI_IOB_PC_INIT),\r
     ParentDevicePath\r
     EFI_PROGRESS_CODE,\r
     (EFI_IO_BUS_SCSI | EFI_IOB_PC_INIT),\r
     ParentDevicePath\r
-    );  \r
+    );\r
 \r
   //\r
 \r
   //\r
-  // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as \r
+  // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as\r
   // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly\r
   // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.\r
   //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiExtScsiPassThruProtocolGuid,\r
   // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly\r
   // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.\r
   //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiExtScsiPassThruProtocolGuid,\r
-                  (VOID **) &ExtScsiInterface,\r
+                  (VOID **)&ExtScsiInterface,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -366,11 +358,11 @@ SCSIBusDriverBindingStart (
   //\r
   // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.\r
   //\r
   //\r
   // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.\r
   //\r
-  if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {\r
+  if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiScsiPassThruProtocolGuid,\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiScsiPassThruProtocolGuid,\r
-                    (VOID **) &ScsiInterface,\r
+                    (VOID **)&ScsiInterface,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -379,58 +371,60 @@ SCSIBusDriverBindingStart (
     // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.\r
     //\r
     if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
     // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.\r
     //\r
     if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
-      if (!EFI_ERROR(DevicePathStatus)) {\r
+      if (!EFI_ERROR (DevicePathStatus)) {\r
         gBS->CloseProtocol (\r
                Controller,\r
                &gEfiDevicePathProtocolGuid,\r
                This->DriverBindingHandle,\r
                Controller\r
                );\r
         gBS->CloseProtocol (\r
                Controller,\r
                &gEfiDevicePathProtocolGuid,\r
                This->DriverBindingHandle,\r
                Controller\r
                );\r
-      } \r
+      }\r
+\r
       return Status;\r
       return Status;\r
-    } \r
+    }\r
   } else {\r
     //\r
   } else {\r
     //\r
-    // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol \r
-    // with BY_DRIVER if it is also present on the handle. The intent is to prevent \r
+    // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol\r
+    // with BY_DRIVER if it is also present on the handle. The intent is to prevent\r
     // another SCSI Bus Driver to work on the same host handle.\r
     //\r
     ExtScsiSupport = TRUE;\r
     PassThruStatus = gBS->OpenProtocol (\r
                             Controller,\r
                             &gEfiScsiPassThruProtocolGuid,\r
     // another SCSI Bus Driver to work on the same host handle.\r
     //\r
     ExtScsiSupport = TRUE;\r
     PassThruStatus = gBS->OpenProtocol (\r
                             Controller,\r
                             &gEfiScsiPassThruProtocolGuid,\r
-                            (VOID **) &ScsiInterface,\r
+                            (VOID **)&ScsiInterface,\r
                             This->DriverBindingHandle,\r
                             Controller,\r
                             EFI_OPEN_PROTOCOL_BY_DRIVER\r
                             );\r
   }\r
                             This->DriverBindingHandle,\r
                             Controller,\r
                             EFI_OPEN_PROTOCOL_BY_DRIVER\r
                             );\r
   }\r
-    \r
+\r
   if (Status != EFI_ALREADY_STARTED) {\r
     //\r
     // Go through here means either ExtPassThru or PassThru Protocol is successfully opened\r
     // on this handle for this time. Then construct Host controller private data.\r
     //\r
     ScsiBusDev = NULL;\r
   if (Status != EFI_ALREADY_STARTED) {\r
     //\r
     // Go through here means either ExtPassThru or PassThru Protocol is successfully opened\r
     // on this handle for this time. Then construct Host controller private data.\r
     //\r
     ScsiBusDev = NULL;\r
-    ScsiBusDev = AllocateZeroPool(sizeof(SCSI_BUS_DEVICE));\r
+    ScsiBusDev = AllocateZeroPool (sizeof (SCSI_BUS_DEVICE));\r
     if (ScsiBusDev == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto ErrorExit;\r
     }\r
     if (ScsiBusDev == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto ErrorExit;\r
     }\r
-    ScsiBusDev->Signature        = SCSI_BUS_DEVICE_SIGNATURE;\r
-    ScsiBusDev->ExtScsiSupport   = ExtScsiSupport;\r
-    ScsiBusDev->DevicePath       = ParentDevicePath;\r
+\r
+    ScsiBusDev->Signature      = SCSI_BUS_DEVICE_SIGNATURE;\r
+    ScsiBusDev->ExtScsiSupport = ExtScsiSupport;\r
+    ScsiBusDev->DevicePath     = ParentDevicePath;\r
     if (ScsiBusDev->ExtScsiSupport) {\r
       ScsiBusDev->ExtScsiInterface = ExtScsiInterface;\r
     } else {\r
     if (ScsiBusDev->ExtScsiSupport) {\r
       ScsiBusDev->ExtScsiInterface = ExtScsiInterface;\r
     } else {\r
-      ScsiBusDev->ScsiInterface    = ScsiInterface;    \r
+      ScsiBusDev->ScsiInterface = ScsiInterface;\r
     }\r
 \r
     //\r
     // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be\r
     // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru\r
     // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.\r
     }\r
 \r
     //\r
     // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be\r
     // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru\r
     // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.\r
-    // \r
+    //\r
     Status = gBS->InstallProtocolInterface (\r
                     &Controller,\r
                     &gEfiCallerIdGuid,\r
     Status = gBS->InstallProtocolInterface (\r
                     &Controller,\r
                     &gEfiCallerIdGuid,\r
@@ -448,7 +442,7 @@ SCSIBusDriverBindingStart (
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiCallerIdGuid,\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiCallerIdGuid,\r
-                    (VOID **) &BusIdentify,\r
+                    (VOID **)&BusIdentify,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -457,6 +451,7 @@ SCSIBusDriverBindingStart (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
+\r
     ScsiBusDev = SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify);\r
   }\r
 \r
     ScsiBusDev = SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify);\r
   }\r
 \r
@@ -469,20 +464,20 @@ SCSIBusDriverBindingStart (
     ParentDevicePath\r
     );\r
 \r
     ParentDevicePath\r
     );\r
 \r
-  Lun  = 0;\r
+  Lun = 0;\r
   if (RemainingDevicePath == NULL) {\r
     //\r
   if (RemainingDevicePath == NULL) {\r
     //\r
-    // If RemainingDevicePath is NULL, \r
+    // If RemainingDevicePath is NULL,\r
     // must enumerate all SCSI devices anyway\r
     //\r
     FromFirstTarget = TRUE;\r
   } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
     //\r
     // must enumerate all SCSI devices anyway\r
     //\r
     FromFirstTarget = TRUE;\r
   } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
     //\r
-    // If RemainingDevicePath isn't the End of Device Path Node, \r
+    // If RemainingDevicePath isn't the End of Device Path Node,\r
     // only scan the specified device by RemainingDevicePath\r
     //\r
     if (ScsiBusDev->ExtScsiSupport) {\r
     // only scan the specified device by RemainingDevicePath\r
     //\r
     if (ScsiBusDev->ExtScsiSupport) {\r
-      Status = ScsiBusDev->ExtScsiInterface->GetTargetLun (ScsiBusDev->ExtScsiInterface, RemainingDevicePath, &TargetId, &Lun);  \r
+      Status = ScsiBusDev->ExtScsiInterface->GetTargetLun (ScsiBusDev->ExtScsiInterface, RemainingDevicePath, &TargetId, &Lun);\r
     } else {\r
       Status = ScsiBusDev->ScsiInterface->GetTargetLun (ScsiBusDev->ScsiInterface, RemainingDevicePath, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
     }\r
     } else {\r
       Status = ScsiBusDev->ScsiInterface->GetTargetLun (ScsiBusDev->ScsiInterface, RemainingDevicePath, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
     }\r
@@ -493,12 +488,12 @@ SCSIBusDriverBindingStart (
   } else {\r
     //\r
     // If RemainingDevicePath is the End of Device Path Node,\r
   } else {\r
     //\r
     // If RemainingDevicePath is the End of Device Path Node,\r
-    // skip enumerate any device and return EFI_SUCESSS\r
-    // \r
+    // skip enumerate any device and return EFI_SUCCESS\r
+    //\r
     ScanOtherPuns = FALSE;\r
   }\r
 \r
     ScanOtherPuns = FALSE;\r
   }\r
 \r
-  while(ScanOtherPuns) {\r
+  while (ScanOtherPuns) {\r
     if (FromFirstTarget) {\r
       //\r
       // Remaining Device Path is NULL, scan all the possible Puns in the\r
     if (FromFirstTarget) {\r
       //\r
       // Remaining Device Path is NULL, scan all the possible Puns in the\r
@@ -509,6 +504,7 @@ SCSIBusDriverBindingStart (
       } else {\r
         Status = ScsiBusDev->ScsiInterface->GetNextDevice (ScsiBusDev->ScsiInterface, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
       }\r
       } else {\r
         Status = ScsiBusDev->ScsiInterface->GetNextDevice (ScsiBusDev->ScsiInterface, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
       }\r
+\r
       if (EFI_ERROR (Status)) {\r
         //\r
         // no legal Pun and Lun found any more\r
       if (EFI_ERROR (Status)) {\r
         //\r
         // no legal Pun and Lun found any more\r
@@ -518,6 +514,7 @@ SCSIBusDriverBindingStart (
     } else {\r
       ScanOtherPuns = FALSE;\r
     }\r
     } else {\r
       ScanOtherPuns = FALSE;\r
     }\r
+\r
     //\r
     // Avoid creating handle for the host adapter.\r
     //\r
     //\r
     // Avoid creating handle for the host adapter.\r
     //\r
@@ -530,20 +527,25 @@ SCSIBusDriverBindingStart (
         continue;\r
       }\r
     }\r
         continue;\r
       }\r
     }\r
+\r
     //\r
     // Scan for the scsi device, if it attaches to the scsi bus,\r
     // then create handle and install scsi i/o protocol.\r
     //\r
     Status = ScsiScanCreateDevice (This, Controller, &ScsiTargetId, Lun, ScsiBusDev);\r
     //\r
     // Scan for the scsi device, if it attaches to the scsi bus,\r
     // then create handle and install scsi i/o protocol.\r
     //\r
     Status = ScsiScanCreateDevice (This, Controller, &ScsiTargetId, Lun, ScsiBusDev);\r
+    if (Status == EFI_OUT_OF_RESOURCES) {\r
+      goto ErrorExit;\r
+    }\r
   }\r
   }\r
+\r
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
-  \r
+\r
   if (ScsiBusDev != NULL) {\r
     FreePool (ScsiBusDev);\r
   }\r
   if (ScsiBusDev != NULL) {\r
     FreePool (ScsiBusDev);\r
   }\r
-  \r
+\r
   if (ExtScsiSupport) {\r
     gBS->CloseProtocol (\r
            Controller,\r
   if (ExtScsiSupport) {\r
     gBS->CloseProtocol (\r
            Controller,\r
@@ -567,6 +569,7 @@ ErrorExit:
            Controller\r
            );\r
   }\r
            Controller\r
            );\r
   }\r
+\r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
@@ -578,7 +581,7 @@ ErrorExit:
   restrictions for this service. DisconnectController() must follow these\r
   calling restrictions. If any other agent wishes to call Stop() it must also\r
   follow these calling restrictions.\r
   restrictions for this service. DisconnectController() must follow these\r
   calling restrictions. If any other agent wishes to call Stop() it must also\r
   follow these calling restrictions.\r
-  \r
+\r
   @param  This              Protocol instance pointer.\r
   @param  ControllerHandle  Handle of device to stop driver on\r
   @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of\r
   @param  This              Protocol instance pointer.\r
   @param  ControllerHandle  Handle of device to stop driver on\r
   @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of\r
@@ -592,20 +595,20 @@ ErrorExit:
 EFI_STATUS\r
 EFIAPI\r
 SCSIBusDriverBindingStop (\r
 EFI_STATUS\r
 EFIAPI\r
 SCSIBusDriverBindingStop (\r
-  IN  EFI_DRIVER_BINDING_PROTOCOL     *This,\r
-  IN  EFI_HANDLE                      Controller,\r
-  IN  UINTN                           NumberOfChildren,\r
-  IN  EFI_HANDLE                      *ChildHandleBuffer\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   Controller,\r
+  IN  UINTN                        NumberOfChildren,\r
+  IN  EFI_HANDLE                   *ChildHandleBuffer\r
   )\r
 {\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  BOOLEAN                     AllChildrenStopped;\r
-  UINTN                       Index;\r
-  EFI_SCSI_IO_PROTOCOL        *ScsiIo;\r
-  SCSI_IO_DEV                 *ScsiIoDevice;\r
-  VOID                        *ScsiPassThru;\r
-  EFI_SCSI_BUS_PROTOCOL       *Scsidentifier;\r
-  SCSI_BUS_DEVICE             *ScsiBusDev;\r
+  EFI_STATUS             Status;\r
+  BOOLEAN                AllChildrenStopped;\r
+  UINTN                  Index;\r
+  EFI_SCSI_IO_PROTOCOL   *ScsiIo;\r
+  SCSI_IO_DEV            *ScsiIoDevice;\r
+  VOID                   *ScsiPassThru;\r
+  EFI_SCSI_BUS_PROTOCOL  *Scsidentifier;\r
+  SCSI_BUS_DEVICE        *ScsiBusDev;\r
 \r
   if (NumberOfChildren == 0) {\r
     //\r
 \r
   if (NumberOfChildren == 0) {\r
     //\r
@@ -614,7 +617,7 @@ SCSIBusDriverBindingStop (
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiCallerIdGuid,\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiCallerIdGuid,\r
-                    (VOID **) &Scsidentifier,\r
+                    (VOID **)&Scsidentifier,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -650,7 +653,7 @@ SCSIBusDriverBindingStop (
              );\r
       //\r
       // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.\r
              );\r
       //\r
       // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.\r
-      // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle. \r
+      // Its intent is to prevent another SCSI Bus Driver from working on the same host handle.\r
       // So Stop() needs to try to close PassThru if present here.\r
       //\r
       gBS->CloseProtocol (\r
       // So Stop() needs to try to close PassThru if present here.\r
       //\r
       gBS->CloseProtocol (\r
@@ -681,11 +684,10 @@ SCSIBusDriverBindingStop (
   AllChildrenStopped = TRUE;\r
 \r
   for (Index = 0; Index < NumberOfChildren; Index++) {\r
   AllChildrenStopped = TRUE;\r
 \r
   for (Index = 0; Index < NumberOfChildren; Index++) {\r
-\r
     Status = gBS->OpenProtocol (\r
                     ChildHandleBuffer[Index],\r
                     &gEfiScsiIoProtocolGuid,\r
     Status = gBS->OpenProtocol (\r
                     ChildHandleBuffer[Index],\r
                     &gEfiScsiIoProtocolGuid,\r
-                    (VOID **) &ScsiIo,\r
+                    (VOID **)&ScsiIo,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -706,7 +708,6 @@ SCSIBusDriverBindingStop (
                       This->DriverBindingHandle,\r
                       ChildHandleBuffer[Index]\r
                       );\r
                       This->DriverBindingHandle,\r
                       ChildHandleBuffer[Index]\r
                       );\r
-\r
     } else {\r
       Status = gBS->CloseProtocol (\r
                       Controller,\r
     } else {\r
       Status = gBS->CloseProtocol (\r
                       Controller,\r
@@ -757,37 +758,35 @@ SCSIBusDriverBindingStop (
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Retrieves the device type information of the SCSI Controller.\r
 \r
   @param  This          Protocol instance pointer.\r
   @param  DeviceType    A pointer to the device type information retrieved from\r
 /**\r
   Retrieves the device type information of the SCSI Controller.\r
 \r
   @param  This          Protocol instance pointer.\r
   @param  DeviceType    A pointer to the device type information retrieved from\r
-                        the SCSI Controller. \r
+                        the SCSI Controller.\r
 \r
   @retval EFI_SUCCESS             Retrieves the device type information successfully.\r
   @retval EFI_INVALID_PARAMETER   The DeviceType is NULL.\r
 \r
   @retval EFI_SUCCESS             Retrieves the device type information successfully.\r
   @retval EFI_INVALID_PARAMETER   The DeviceType is NULL.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiGetDeviceType (\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiGetDeviceType (\r
-  IN  EFI_SCSI_IO_PROTOCOL     *This,\r
-  OUT UINT8                    *DeviceType\r
+  IN  EFI_SCSI_IO_PROTOCOL  *This,\r
+  OUT UINT8                 *DeviceType\r
   )\r
 {\r
   )\r
 {\r
-  SCSI_IO_DEV *ScsiIoDevice;\r
+  SCSI_IO_DEV  *ScsiIoDevice;\r
 \r
   if (DeviceType == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 \r
   if (DeviceType == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  ScsiIoDevice  = SCSI_IO_DEV_FROM_THIS (This);\r
-  *DeviceType   = ScsiIoDevice->ScsiDeviceType;\r
+  ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
+  *DeviceType  = ScsiIoDevice->ScsiDeviceType;\r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Retrieves the device location in the SCSI channel.\r
 \r
 /**\r
   Retrieves the device location in the SCSI channel.\r
 \r
@@ -804,22 +803,22 @@ ScsiGetDeviceType (
 EFI_STATUS\r
 EFIAPI\r
 ScsiGetDeviceLocation (\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiGetDeviceLocation (\r
-  IN  EFI_SCSI_IO_PROTOCOL    *This,\r
-  IN OUT UINT8                **Target,\r
-  OUT UINT64                  *Lun\r
+  IN  EFI_SCSI_IO_PROTOCOL  *This,\r
+  IN OUT UINT8              **Target,\r
+  OUT UINT64                *Lun\r
   )\r
 {\r
   )\r
 {\r
-  SCSI_IO_DEV *ScsiIoDevice;\r
+  SCSI_IO_DEV  *ScsiIoDevice;\r
 \r
 \r
-  if (Target == NULL || Lun == NULL) {\r
+  if ((Target == NULL) || (Lun == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
 \r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
 \r
-  CopyMem (*Target,&ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
+  CopyMem (*Target, &ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
 \r
 \r
-  *Lun         = ScsiIoDevice->Lun;\r
+  *Lun = ScsiIoDevice->Lun;\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -833,16 +832,16 @@ ScsiGetDeviceLocation (
   @retval  EFI_DEVICE_ERROR  Errors encountered when resetting the SCSI bus.\r
   @retval  EFI_UNSUPPORTED   The bus reset operation is not supported by the\r
                              SCSI Host Controller.\r
   @retval  EFI_DEVICE_ERROR  Errors encountered when resetting the SCSI bus.\r
   @retval  EFI_UNSUPPORTED   The bus reset operation is not supported by the\r
                              SCSI Host Controller.\r
-  @retval  EFI_TIMEOUT       A timeout occurred while attempting to reset \r
+  @retval  EFI_TIMEOUT       A timeout occurred while attempting to reset\r
                              the SCSI bus.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiResetBus (\r
                              the SCSI bus.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiResetBus (\r
-  IN  EFI_SCSI_IO_PROTOCOL     *This\r
+  IN  EFI_SCSI_IO_PROTOCOL  *This\r
   )\r
 {\r
   )\r
 {\r
-  SCSI_IO_DEV *ScsiIoDevice;\r
+  SCSI_IO_DEV  *ScsiIoDevice;\r
 \r
   ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
 \r
 \r
   ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
 \r
@@ -855,14 +854,13 @@ ScsiResetBus (
     ScsiIoDevice->ScsiBusDeviceData->DevicePath\r
     );\r
 \r
     ScsiIoDevice->ScsiBusDeviceData->DevicePath\r
     );\r
 \r
-  if (ScsiIoDevice->ExtScsiSupport){\r
+  if (ScsiIoDevice->ExtScsiSupport) {\r
     return ScsiIoDevice->ExtScsiPassThru->ResetChannel (ScsiIoDevice->ExtScsiPassThru);\r
   } else {\r
     return ScsiIoDevice->ScsiPassThru->ResetChannel (ScsiIoDevice->ScsiPassThru);\r
   }\r
 }\r
 \r
     return ScsiIoDevice->ExtScsiPassThru->ResetChannel (ScsiIoDevice->ExtScsiPassThru);\r
   } else {\r
     return ScsiIoDevice->ScsiPassThru->ResetChannel (ScsiIoDevice->ScsiPassThru);\r
   }\r
 }\r
 \r
-\r
 /**\r
   Resets the SCSI Controller that the device handle specifies.\r
 \r
 /**\r
   Resets the SCSI Controller that the device handle specifies.\r
 \r
@@ -877,7 +875,7 @@ ScsiResetBus (
 EFI_STATUS\r
 EFIAPI\r
 ScsiResetDevice (\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiResetDevice (\r
-  IN  EFI_SCSI_IO_PROTOCOL     *This\r
+  IN  EFI_SCSI_IO_PROTOCOL  *This\r
   )\r
 {\r
   SCSI_IO_DEV  *ScsiIoDevice;\r
   )\r
 {\r
   SCSI_IO_DEV  *ScsiIoDevice;\r
@@ -893,79 +891,77 @@ ScsiResetDevice (
     (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_RESET),\r
     ScsiIoDevice->ScsiBusDeviceData->DevicePath\r
     );\r
     (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_RESET),\r
     ScsiIoDevice->ScsiBusDeviceData->DevicePath\r
     );\r
-  \r
-  CopyMem (Target,&ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
 \r
 \r
+  CopyMem (Target, &ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
 \r
   if (ScsiIoDevice->ExtScsiSupport) {\r
     return ScsiIoDevice->ExtScsiPassThru->ResetTargetLun (\r
 \r
   if (ScsiIoDevice->ExtScsiSupport) {\r
     return ScsiIoDevice->ExtScsiPassThru->ResetTargetLun (\r
-                                        ScsiIoDevice->ExtScsiPassThru,\r
-                                        Target,\r
-                                        ScsiIoDevice->Lun\r
-                                          );\r
+                                            ScsiIoDevice->ExtScsiPassThru,\r
+                                            Target,\r
+                                            ScsiIoDevice->Lun\r
+                                            );\r
   } else {\r
     return ScsiIoDevice->ScsiPassThru->ResetTarget (\r
   } else {\r
     return ScsiIoDevice->ScsiPassThru->ResetTarget (\r
-                                          ScsiIoDevice->ScsiPassThru,\r
-                                          ScsiIoDevice->Pun.ScsiId.Scsi,\r
-                                          ScsiIoDevice->Lun\r
-                                            );\r
+                                         ScsiIoDevice->ScsiPassThru,\r
+                                         ScsiIoDevice->Pun.ScsiId.Scsi,\r
+                                         ScsiIoDevice->Lun\r
+                                         );\r
   }\r
 }\r
 \r
   }\r
 }\r
 \r
-\r
 /**\r
   Sends a SCSI Request Packet to the SCSI Controller for execution.\r
 \r
   @param  This            Protocol instance pointer.\r
 /**\r
   Sends a SCSI Request Packet to the SCSI Controller for execution.\r
 \r
   @param  This            Protocol instance pointer.\r
-  @param  CommandPacket   The SCSI request packet to send to the SCSI \r
+  @param  CommandPacket   The SCSI request packet to send to the SCSI\r
                           Controller specified by the device handle.\r
   @param  Event           If the SCSI bus where the SCSI device is attached\r
                           Controller specified by the device handle.\r
   @param  Event           If the SCSI bus where the SCSI device is attached\r
-                          does not support non-blocking I/O, then Event is \r
-                          ignored, and blocking I/O is performed.  \r
+                          does not support non-blocking I/O, then Event is\r
+                          ignored, and blocking I/O is performed.\r
                           If Event is NULL, then blocking I/O is performed.\r
                           If Event is NULL, then blocking I/O is performed.\r
-                          If Event is not NULL and non-blocking I/O is \r
+                          If Event is not NULL and non-blocking I/O is\r
                           supported, then non-blocking I/O is performed,\r
                           and Event will be signaled when the SCSI Request\r
                           Packet completes.\r
 \r
                           supported, then non-blocking I/O is performed,\r
                           and Event will be signaled when the SCSI Request\r
                           Packet completes.\r
 \r
-  @retval EFI_SUCCESS         The SCSI Request Packet was sent by the host \r
-                              successfully, and TransferLength bytes were \r
-                              transferred to/from DataBuffer.See \r
-                              HostAdapterStatus, TargetStatus, \r
+  @retval EFI_SUCCESS         The SCSI Request Packet was sent by the host\r
+                              successfully, and TransferLength bytes were\r
+                              transferred to/from DataBuffer.See\r
+                              HostAdapterStatus, TargetStatus,\r
                               SenseDataLength, and SenseData in that order\r
                               for additional status information.\r
                               SenseDataLength, and SenseData in that order\r
                               for additional status information.\r
-  @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed, \r
+  @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,\r
                               but the entire DataBuffer could not be transferred.\r
                               The actual number of bytes transferred is returned\r
                               but the entire DataBuffer could not be transferred.\r
                               The actual number of bytes transferred is returned\r
-                              in TransferLength. See HostAdapterStatus, \r
-                              TargetStatus, SenseDataLength, and SenseData in \r
+                              in TransferLength. See HostAdapterStatus,\r
+                              TargetStatus, SenseDataLength, and SenseData in\r
                               that order for additional status information.\r
                               that order for additional status information.\r
-  @retval EFI_NOT_READY       The SCSI Request Packet could not be sent because \r
-                              there are too many SCSI Command Packets already \r
+  @retval EFI_NOT_READY       The SCSI Request Packet could not be sent because\r
+                              there are too many SCSI Command Packets already\r
                               queued.The caller may retry again later.\r
                               queued.The caller may retry again later.\r
-  @retval EFI_DEVICE_ERROR    A device error occurred while attempting to send \r
-                              the SCSI Request Packet. See HostAdapterStatus, \r
-                              TargetStatus, SenseDataLength, and SenseData in \r
+  @retval EFI_DEVICE_ERROR    A device error occurred while attempting to send\r
+                              the SCSI Request Packet. See HostAdapterStatus,\r
+                              TargetStatus, SenseDataLength, and SenseData in\r
                               that order for additional status information.\r
                               that order for additional status information.\r
-  @retval EFI_INVALID_PARAMETER  The contents of CommandPacket are invalid.  \r
-                                 The SCSI Request Packet was not sent, so no \r
+  @retval EFI_INVALID_PARAMETER  The contents of CommandPacket are invalid.\r
+                                 The SCSI Request Packet was not sent, so no\r
                                  additional status information is available.\r
   @retval EFI_UNSUPPORTED     The command described by the SCSI Request Packet\r
                                  additional status information is available.\r
   @retval EFI_UNSUPPORTED     The command described by the SCSI Request Packet\r
-                              is not supported by the SCSI initiator(i.e., SCSI \r
+                              is not supported by the SCSI initiator(i.e., SCSI\r
                               Host Controller). The SCSI Request Packet was not\r
                               Host Controller). The SCSI Request Packet was not\r
-                              sent, so no additional status information is \r
+                              sent, so no additional status information is\r
                               available.\r
                               available.\r
-  @retval EFI_TIMEOUT         A timeout occurred while waiting for the SCSI \r
+  @retval EFI_TIMEOUT         A timeout occurred while waiting for the SCSI\r
                               Request Packet to execute. See HostAdapterStatus,\r
                               Request Packet to execute. See HostAdapterStatus,\r
-                              TargetStatus, SenseDataLength, and SenseData in \r
+                              TargetStatus, SenseDataLength, and SenseData in\r
                               that order for additional status information.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiExecuteSCSICommand (\r
                               that order for additional status information.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiExecuteSCSICommand (\r
-  IN     EFI_SCSI_IO_PROTOCOL                     *This,\r
-  IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET          *Packet,\r
-  IN     EFI_EVENT                                Event  OPTIONAL\r
+  IN     EFI_SCSI_IO_PROTOCOL             *This,\r
+  IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET  *Packet,\r
+  IN     EFI_EVENT                        Event  OPTIONAL\r
   )\r
 {\r
   SCSI_IO_DEV                                 *ScsiIoDevice;\r
   )\r
 {\r
   SCSI_IO_DEV                                 *ScsiIoDevice;\r
@@ -973,19 +969,19 @@ ScsiExecuteSCSICommand (
   UINT8                                       Target[TARGET_MAX_BYTES];\r
   EFI_EVENT                                   PacketEvent;\r
   EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET  *ExtRequestPacket;\r
   UINT8                                       Target[TARGET_MAX_BYTES];\r
   EFI_EVENT                                   PacketEvent;\r
   EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET  *ExtRequestPacket;\r
-  SCSI_EVENT_DATA                             EventData;                                     \r
+  SCSI_EVENT_DATA                             EventData;\r
 \r
   PacketEvent = NULL;\r
 \r
   PacketEvent = NULL;\r
-  \r
+\r
   if (Packet == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if (Packet == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  ScsiIoDevice  = SCSI_IO_DEV_FROM_THIS (This);\r
-  CopyMem (Target,&ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
+  ScsiIoDevice = SCSI_IO_DEV_FROM_THIS (This);\r
+  CopyMem (Target, &ScsiIoDevice->Pun, TARGET_MAX_BYTES);\r
 \r
   if (ScsiIoDevice->ExtScsiSupport) {\r
 \r
   if (ScsiIoDevice->ExtScsiSupport) {\r
-    ExtRequestPacket = (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *) Packet;\r
+    ExtRequestPacket = (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *)Packet;\r
 \r
     if (((ScsiIoDevice->ExtScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO) != 0) && (Event !=  NULL)) {\r
       Status = ScsiIoDevice->ExtScsiPassThru->PassThru (\r
 \r
     if (((ScsiIoDevice->ExtScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO) != 0) && (Event !=  NULL)) {\r
       Status = ScsiIoDevice->ExtScsiPassThru->PassThru (\r
@@ -1007,7 +1003,7 @@ ScsiExecuteSCSICommand (
                                                 ExtRequestPacket,\r
                                                 NULL\r
                                                 );\r
                                                 ExtRequestPacket,\r
                                                 NULL\r
                                                 );\r
-      if ((!EFI_ERROR(Status)) && (Event != NULL)) {\r
+      if ((!EFI_ERROR (Status)) && (Event != NULL)) {\r
         //\r
         // Signal Event to tell caller to pick up the SCSI IO packet if the\r
         // PassThru() succeeds.\r
         //\r
         // Signal Event to tell caller to pick up the SCSI IO packet if the\r
         // PassThru() succeeds.\r
@@ -1016,8 +1012,7 @@ ScsiExecuteSCSICommand (
       }\r
     }\r
   } else {\r
       }\r
     }\r
   } else {\r
-\r
-    mWorkingBuffer = AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));\r
+    mWorkingBuffer = AllocatePool (sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));\r
 \r
     if (mWorkingBuffer == NULL) {\r
       return EFI_DEVICE_ERROR;\r
 \r
     if (mWorkingBuffer == NULL) {\r
       return EFI_DEVICE_ERROR;\r
@@ -1026,67 +1021,66 @@ ScsiExecuteSCSICommand (
     //\r
     // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.\r
     //\r
     //\r
     // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.\r
     //\r
-    Status = ScsiioToPassThruPacket(Packet, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET*)mWorkingBuffer);\r
-    if (EFI_ERROR(Status)) {\r
-      FreePool(mWorkingBuffer);\r
+    Status = ScsiioToPassThruPacket (Packet, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *)mWorkingBuffer);\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (mWorkingBuffer);\r
       return Status;\r
     }\r
 \r
     if (((ScsiIoDevice->ScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO) != 0) && (Event !=  NULL)) {\r
       return Status;\r
     }\r
 \r
     if (((ScsiIoDevice->ScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO) != 0) && (Event !=  NULL)) {\r
-      EventData.Data1 = (VOID*)Packet;\r
+      EventData.Data1 = (VOID *)Packet;\r
       EventData.Data2 = Event;\r
       //\r
       // Create Event\r
       //\r
       Status = gBS->CreateEvent (\r
       EventData.Data2 = Event;\r
       //\r
       // Create Event\r
       //\r
       Status = gBS->CreateEvent (\r
-                       EVT_NOTIFY_SIGNAL,\r
-                       TPL_NOTIFY,\r
-                       NotifyFunction,\r
-                       &EventData,\r
-                       &PacketEvent\r
-                       );\r
-      if (EFI_ERROR(Status)) {\r
-        FreePool(mWorkingBuffer);\r
+                      EVT_NOTIFY_SIGNAL,\r
+                      TPL_NOTIFY,\r
+                      NotifyFunction,\r
+                      &EventData,\r
+                      &PacketEvent\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (mWorkingBuffer);\r
         return Status;\r
       }\r
 \r
       Status = ScsiIoDevice->ScsiPassThru->PassThru (\r
         return Status;\r
       }\r
 \r
       Status = ScsiIoDevice->ScsiPassThru->PassThru (\r
-                                          ScsiIoDevice->ScsiPassThru,\r
-                                          ScsiIoDevice->Pun.ScsiId.Scsi,\r
-                                          ScsiIoDevice->Lun,\r
-                                          mWorkingBuffer,\r
-                                          PacketEvent\r
-                                          );\r
+                                             ScsiIoDevice->ScsiPassThru,\r
+                                             ScsiIoDevice->Pun.ScsiId.Scsi,\r
+                                             ScsiIoDevice->Lun,\r
+                                             mWorkingBuffer,\r
+                                             PacketEvent\r
+                                             );\r
 \r
 \r
-      if (EFI_ERROR(Status)) {\r
-        FreePool(mWorkingBuffer);\r
-        gBS->CloseEvent(PacketEvent);\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (mWorkingBuffer);\r
+        gBS->CloseEvent (PacketEvent);\r
         return Status;\r
       }\r
         return Status;\r
       }\r
-\r
     } else {\r
       //\r
       // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert\r
       // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.\r
       //\r
       Status = ScsiIoDevice->ScsiPassThru->PassThru (\r
     } else {\r
       //\r
       // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert\r
       // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.\r
       //\r
       Status = ScsiIoDevice->ScsiPassThru->PassThru (\r
-                                          ScsiIoDevice->ScsiPassThru,\r
-                                          ScsiIoDevice->Pun.ScsiId.Scsi,\r
-                                          ScsiIoDevice->Lun,\r
-                                          mWorkingBuffer,\r
-                                          NULL\r
-                                          );\r
-      if (EFI_ERROR(Status)) {\r
-        FreePool(mWorkingBuffer);\r
+                                             ScsiIoDevice->ScsiPassThru,\r
+                                             ScsiIoDevice->Pun.ScsiId.Scsi,\r
+                                             ScsiIoDevice->Lun,\r
+                                             mWorkingBuffer,\r
+                                             NULL\r
+                                             );\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (mWorkingBuffer);\r
         return Status;\r
       }\r
 \r
         return Status;\r
       }\r
 \r
-      PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET*)mWorkingBuffer,Packet);\r
+      PassThruToScsiioPacket ((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *)mWorkingBuffer, Packet);\r
       //\r
       // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,\r
       // free mWorkingBuffer.\r
       //\r
       //\r
       // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,\r
       // free mWorkingBuffer.\r
       //\r
-      FreePool(mWorkingBuffer);\r
+      FreePool (mWorkingBuffer);\r
 \r
       //\r
       // Signal Event to tell caller to pick up the SCSI IO Packet.\r
 \r
       //\r
       // Signal Event to tell caller to pick up the SCSI IO Packet.\r
@@ -1096,32 +1090,33 @@ ScsiExecuteSCSICommand (
       }\r
     }\r
   }\r
       }\r
     }\r
   }\r
+\r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.\r
 \r
   @param  This           Protocol instance pointer\r
   @param  Controller     Controller handle\r
 /**\r
   Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.\r
 \r
   @param  This           Protocol instance pointer\r
   @param  Controller     Controller handle\r
-  @param  TargetId       Tartget to be scanned\r
+  @param  TargetId       Target to be scanned\r
   @param  Lun            The Lun of the SCSI device on the SCSI channel.\r
   @param  ScsiBusDev     The pointer of SCSI_BUS_DEVICE\r
 \r
   @retval EFI_SUCCESS           Successfully to discover the device and attach\r
                                 ScsiIoProtocol to it.\r
   @param  Lun            The Lun of the SCSI device on the SCSI channel.\r
   @param  ScsiBusDev     The pointer of SCSI_BUS_DEVICE\r
 \r
   @retval EFI_SUCCESS           Successfully to discover the device and attach\r
                                 ScsiIoProtocol to it.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to discover the device.\r
+  @retval EFI_NOT_FOUND         Fail to discover the device.\r
+  @retval EFI_OUT_OF_RESOURCES  Fail to allocate memory resources.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiScanCreateDevice (\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 ScsiScanCreateDevice (\r
-  IN     EFI_DRIVER_BINDING_PROTOCOL   *This,\r
-  IN     EFI_HANDLE                    Controller,\r
-  IN     SCSI_TARGET_ID                *TargetId,\r
-  IN     UINT64                        Lun,\r
-  IN OUT SCSI_BUS_DEVICE               *ScsiBusDev\r
+  IN     EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN     EFI_HANDLE                   Controller,\r
+  IN     SCSI_TARGET_ID               *TargetId,\r
+  IN     UINT64                       Lun,\r
+  IN OUT SCSI_BUS_DEVICE              *ScsiBusDev\r
   )\r
 {\r
   EFI_STATUS                Status;\r
   )\r
 {\r
   EFI_STATUS                Status;\r
@@ -1129,7 +1124,7 @@ ScsiScanCreateDevice (
   EFI_DEVICE_PATH_PROTOCOL  *ScsiDevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *RemainingDevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *ScsiDevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *RemainingDevicePath;\r
-  EFI_HANDLE                 DeviceHandle;\r
+  EFI_HANDLE                DeviceHandle;\r
 \r
   DevicePath          = NULL;\r
   RemainingDevicePath = NULL;\r
 \r
   DevicePath          = NULL;\r
   RemainingDevicePath = NULL;\r
@@ -1139,7 +1134,7 @@ ScsiScanCreateDevice (
   //\r
   // Build Device Path\r
   //\r
   //\r
   // Build Device Path\r
   //\r
-  if (ScsiBusDev->ExtScsiSupport){\r
+  if (ScsiBusDev->ExtScsiSupport) {\r
     Status = ScsiBusDev->ExtScsiInterface->BuildDevicePath (\r
                                              ScsiBusDev->ExtScsiInterface,\r
                                              &TargetId->ScsiId.ExtScsi[0],\r
     Status = ScsiBusDev->ExtScsiInterface->BuildDevicePath (\r
                                              ScsiBusDev->ExtScsiInterface,\r
                                              &TargetId->ScsiId.ExtScsi[0],\r
@@ -1155,7 +1150,7 @@ ScsiScanCreateDevice (
                                           );\r
   }\r
 \r
                                           );\r
   }\r
 \r
-  if (EFI_ERROR(Status)) {\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
     return Status;\r
   }\r
 \r
@@ -1169,10 +1164,10 @@ ScsiScanCreateDevice (
     goto ErrorExit;\r
   }\r
 \r
     goto ErrorExit;\r
   }\r
 \r
-  DeviceHandle = NULL;\r
+  DeviceHandle        = NULL;\r
   RemainingDevicePath = DevicePath;\r
   RemainingDevicePath = DevicePath;\r
-  Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);\r
-  if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {\r
+  Status              = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);\r
+  if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd (RemainingDevicePath)) {\r
     //\r
     // The device has been started, directly return to fast boot.\r
     //\r
     //\r
     // The device has been started, directly return to fast boot.\r
     //\r
@@ -1186,20 +1181,19 @@ ScsiScanCreateDevice (
     goto ErrorExit;\r
   }\r
 \r
     goto ErrorExit;\r
   }\r
 \r
-  ScsiIoDevice->Signature                 = SCSI_IO_DEV_SIGNATURE;\r
-  ScsiIoDevice->ScsiBusDeviceData         = ScsiBusDev;\r
-  CopyMem(&ScsiIoDevice->Pun, TargetId, TARGET_MAX_BYTES);\r
-  ScsiIoDevice->Lun                       = Lun;\r
+  ScsiIoDevice->Signature         = SCSI_IO_DEV_SIGNATURE;\r
+  ScsiIoDevice->ScsiBusDeviceData = ScsiBusDev;\r
+  CopyMem (&ScsiIoDevice->Pun, TargetId, TARGET_MAX_BYTES);\r
+  ScsiIoDevice->Lun = Lun;\r
 \r
   if (ScsiBusDev->ExtScsiSupport) {\r
 \r
   if (ScsiBusDev->ExtScsiSupport) {\r
-    ScsiIoDevice->ExtScsiPassThru         = ScsiBusDev->ExtScsiInterface;\r
-    ScsiIoDevice->ExtScsiSupport          = TRUE;\r
-    ScsiIoDevice->ScsiIo.IoAlign          = ScsiIoDevice->ExtScsiPassThru->Mode->IoAlign;\r
-\r
+    ScsiIoDevice->ExtScsiPassThru = ScsiBusDev->ExtScsiInterface;\r
+    ScsiIoDevice->ExtScsiSupport  = TRUE;\r
+    ScsiIoDevice->ScsiIo.IoAlign  = ScsiIoDevice->ExtScsiPassThru->Mode->IoAlign;\r
   } else {\r
   } else {\r
-    ScsiIoDevice->ScsiPassThru            = ScsiBusDev->ScsiInterface;\r
-    ScsiIoDevice->ExtScsiSupport          = FALSE;\r
-    ScsiIoDevice->ScsiIo.IoAlign          = ScsiIoDevice->ScsiPassThru->Mode->IoAlign;\r
+    ScsiIoDevice->ScsiPassThru   = ScsiBusDev->ScsiInterface;\r
+    ScsiIoDevice->ExtScsiSupport = FALSE;\r
+    ScsiIoDevice->ScsiIo.IoAlign = ScsiIoDevice->ScsiPassThru->Mode->IoAlign;\r
   }\r
 \r
   ScsiIoDevice->ScsiIo.GetDeviceType      = ScsiGetDeviceType;\r
   }\r
 \r
   ScsiIoDevice->ScsiIo.GetDeviceType      = ScsiGetDeviceType;\r
@@ -1217,8 +1211,8 @@ ScsiScanCreateDevice (
     ScsiBusDev->DevicePath\r
     );\r
 \r
     ScsiBusDev->DevicePath\r
     );\r
 \r
-  if (!DiscoverScsiDevice (ScsiIoDevice)) {\r
-    Status = EFI_OUT_OF_RESOURCES;\r
+  Status = DiscoverScsiDevice (ScsiIoDevice);\r
+  if (EFI_ERROR (Status)) {\r
     goto ErrorExit;\r
   }\r
 \r
     goto ErrorExit;\r
   }\r
 \r
@@ -1239,26 +1233,27 @@ ScsiScanCreateDevice (
       gBS->OpenProtocol (\r
              Controller,\r
              &gEfiExtScsiPassThruProtocolGuid,\r
       gBS->OpenProtocol (\r
              Controller,\r
              &gEfiExtScsiPassThruProtocolGuid,\r
-             (VOID **) &(ScsiBusDev->ExtScsiInterface),\r
+             (VOID **)&(ScsiBusDev->ExtScsiInterface),\r
              This->DriverBindingHandle,\r
              ScsiIoDevice->Handle,\r
              EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
              );\r
              This->DriverBindingHandle,\r
              ScsiIoDevice->Handle,\r
              EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
              );\r
-     } else {\r
+    } else {\r
       gBS->OpenProtocol (\r
              Controller,\r
              &gEfiScsiPassThruProtocolGuid,\r
       gBS->OpenProtocol (\r
              Controller,\r
              &gEfiScsiPassThruProtocolGuid,\r
-             (VOID **) &(ScsiBusDev->ScsiInterface),\r
+             (VOID **)&(ScsiBusDev->ScsiInterface),\r
              This->DriverBindingHandle,\r
              ScsiIoDevice->Handle,\r
              EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
              );\r
              This->DriverBindingHandle,\r
              ScsiIoDevice->Handle,\r
              EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
              );\r
-     }\r
+    }\r
   }\r
   }\r
+\r
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
-  \r
+\r
   //\r
   // The memory space for ScsiDevicePath is allocated in\r
   // ScsiPassThru->BuildDevicePath() function; It is no longer used\r
   //\r
   // The memory space for ScsiDevicePath is allocated in\r
   // ScsiPassThru->BuildDevicePath() function; It is no longer used\r
@@ -1277,37 +1272,47 @@ ErrorExit:
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Discovery SCSI Device\r
 \r
   @param  ScsiIoDevice    The pointer of SCSI_IO_DEV\r
 \r
 /**\r
   Discovery SCSI Device\r
 \r
   @param  ScsiIoDevice    The pointer of SCSI_IO_DEV\r
 \r
-  @retval  TRUE   Find SCSI Device and verify it.\r
-  @retval  FALSE  Unable to find SCSI Device.\r
+  @retval EFI_SUCCESS           Find SCSI Device and verify it.\r
+  @retval EFI_NOT_FOUND         Unable to find SCSI Device.\r
+  @retval EFI_OUT_OF_RESOURCES  Fail to allocate memory resources.\r
 \r
 **/\r
 \r
 **/\r
-BOOLEAN\r
+EFI_STATUS\r
 DiscoverScsiDevice (\r
 DiscoverScsiDevice (\r
-  IN OUT  SCSI_IO_DEV   *ScsiIoDevice\r
+  IN OUT  SCSI_IO_DEV  *ScsiIoDevice\r
   )\r
 {\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  UINT32                InquiryDataLength;\r
-  UINT8                 SenseDataLength;\r
-  UINT8                 HostAdapterStatus;\r
-  UINT8                 TargetStatus;\r
-  EFI_SCSI_INQUIRY_DATA *InquiryData;\r
-  UINT8                 MaxRetry;\r
-  UINT8                 Index;\r
-  BOOLEAN               ScsiDeviceFound;\r
+  EFI_STATUS             Status;\r
+  UINT32                 InquiryDataLength;\r
+  UINT8                  SenseDataLength;\r
+  UINT8                  HostAdapterStatus;\r
+  UINT8                  TargetStatus;\r
+  EFI_SCSI_INQUIRY_DATA  *InquiryData;\r
+  EFI_SCSI_SENSE_DATA    *SenseData;\r
+  UINT8                  MaxRetry;\r
+  UINT8                  Index;\r
 \r
   HostAdapterStatus = 0;\r
   TargetStatus      = 0;\r
 \r
   HostAdapterStatus = 0;\r
   TargetStatus      = 0;\r
+  SenseData         = NULL;\r
 \r
   InquiryData = AllocateAlignedBuffer (ScsiIoDevice, sizeof (EFI_SCSI_INQUIRY_DATA));\r
   if (InquiryData == NULL) {\r
 \r
   InquiryData = AllocateAlignedBuffer (ScsiIoDevice, sizeof (EFI_SCSI_INQUIRY_DATA));\r
   if (InquiryData == NULL) {\r
-    ScsiDeviceFound = FALSE;\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  SenseData = AllocateAlignedBuffer (\r
+                ScsiIoDevice,\r
+                sizeof (EFI_SCSI_SENSE_DATA)\r
+                );\r
+  if (SenseData == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
 \r
     goto Done;\r
   }\r
 \r
@@ -1315,55 +1320,62 @@ DiscoverScsiDevice (
   // Using Inquiry command to scan for the device\r
   //\r
   InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA);\r
   // Using Inquiry command to scan for the device\r
   //\r
   InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA);\r
-  SenseDataLength   = 0;\r
+  SenseDataLength   = sizeof (EFI_SCSI_SENSE_DATA);\r
   ZeroMem (InquiryData, InquiryDataLength);\r
   ZeroMem (InquiryData, InquiryDataLength);\r
+  ZeroMem (SenseData, SenseDataLength);\r
 \r
   MaxRetry = 2;\r
   for (Index = 0; Index < MaxRetry; Index++) {\r
     Status = ScsiInquiryCommand (\r
 \r
   MaxRetry = 2;\r
   for (Index = 0; Index < MaxRetry; Index++) {\r
     Status = ScsiInquiryCommand (\r
-              &ScsiIoDevice->ScsiIo,\r
-              SCSI_BUS_TIMEOUT,\r
-              NULL,\r
-              &SenseDataLength,\r
-              &HostAdapterStatus,\r
-              &TargetStatus,\r
-              (VOID *) InquiryData,\r
-              &InquiryDataLength,\r
-              FALSE\r
-              );\r
+               &ScsiIoDevice->ScsiIo,\r
+               SCSI_BUS_TIMEOUT,\r
+               SenseData,\r
+               &SenseDataLength,\r
+               &HostAdapterStatus,\r
+               &TargetStatus,\r
+               (VOID *)InquiryData,\r
+               &InquiryDataLength,\r
+               FALSE\r
+               );\r
     if (!EFI_ERROR (Status)) {\r
     if (!EFI_ERROR (Status)) {\r
+      if ((HostAdapterStatus == EFI_SCSI_IO_STATUS_HOST_ADAPTER_OK) &&\r
+          (TargetStatus == EFI_SCSI_IO_STATUS_TARGET_CHECK_CONDITION) &&\r
+          (SenseData->Error_Code == 0x70) &&\r
+          (SenseData->Sense_Key == EFI_SCSI_SK_ILLEGAL_REQUEST))\r
+      {\r
+        Status = EFI_NOT_FOUND;\r
+        goto Done;\r
+      }\r
+\r
       break;\r
       break;\r
-    } else if ((Status == EFI_BAD_BUFFER_SIZE) || \r
-               (Status == EFI_INVALID_PARAMETER) ||\r
-               (Status == EFI_UNSUPPORTED)) {\r
-      ScsiDeviceFound = FALSE;\r
+    }\r
+\r
+    if ((Status == EFI_BAD_BUFFER_SIZE) ||\r
+        (Status == EFI_INVALID_PARAMETER) ||\r
+        (Status == EFI_UNSUPPORTED))\r
+    {\r
+      Status = EFI_NOT_FOUND;\r
       goto Done;\r
     }\r
   }\r
 \r
   if (Index == MaxRetry) {\r
       goto Done;\r
     }\r
   }\r
 \r
   if (Index == MaxRetry) {\r
-    ScsiDeviceFound = FALSE;\r
+    Status = EFI_NOT_FOUND;\r
     goto Done;\r
   }\r
     goto Done;\r
   }\r
-  \r
+\r
   //\r
   // Retrieved inquiry data successfully\r
   //\r
   //\r
   // Retrieved inquiry data successfully\r
   //\r
-  if ((InquiryData->Peripheral_Qualifier != 0) &&\r
-      (InquiryData->Peripheral_Qualifier != 3)) {\r
-    ScsiDeviceFound = FALSE;\r
+  if (InquiryData->Peripheral_Qualifier != 0) {\r
+    Status = EFI_NOT_FOUND;\r
     goto Done;\r
   }\r
 \r
     goto Done;\r
   }\r
 \r
-  if (InquiryData->Peripheral_Qualifier == 3) {\r
-    if (InquiryData->Peripheral_Type != 0x1f) {\r
-      ScsiDeviceFound = FALSE;\r
-      goto Done;\r
-    }\r
-  }\r
-\r
-  if (0x1e >= InquiryData->Peripheral_Type && InquiryData->Peripheral_Type >= 0xa) {\r
-    ScsiDeviceFound = FALSE;\r
+  if ((InquiryData->Peripheral_Type >= EFI_SCSI_TYPE_RESERVED_LOW) &&\r
+      (InquiryData->Peripheral_Type <= EFI_SCSI_TYPE_RESERVED_HIGH))\r
+  {\r
+    Status = EFI_NOT_FOUND;\r
     goto Done;\r
   }\r
 \r
     goto Done;\r
   }\r
 \r
@@ -1378,18 +1390,18 @@ DiscoverScsiDevice (
     //\r
     // ANSI-approved version\r
     //\r
     //\r
     // ANSI-approved version\r
     //\r
-    ScsiIoDevice->ScsiVersion = (UINT8) (InquiryData->Version & 0x07);\r
+    ScsiIoDevice->ScsiVersion = (UINT8)(InquiryData->Version & 0x07);\r
   }\r
 \r
   }\r
 \r
-  ScsiDeviceFound = TRUE;\r
+  Status = EFI_SUCCESS;\r
 \r
 Done:\r
 \r
 Done:\r
+  FreeAlignedBuffer (SenseData, sizeof (EFI_SCSI_SENSE_DATA));\r
   FreeAlignedBuffer (InquiryData, sizeof (EFI_SCSI_INQUIRY_DATA));\r
 \r
   FreeAlignedBuffer (InquiryData, sizeof (EFI_SCSI_INQUIRY_DATA));\r
 \r
-  return ScsiDeviceFound;\r
+  return Status;\r
 }\r
 \r
 }\r
 \r
-\r
 /**\r
   Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.\r
 \r
 /**\r
   Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.\r
 \r
@@ -1405,7 +1417,7 @@ ScsiioToPassThruPacket (
   )\r
 {\r
   //\r
   )\r
 {\r
   //\r
-  //EFI 1.10 doesn't support Bi-Direction Command.\r
+  // EFI 1.10 doesn't support Bi-Direction Command.\r
   //\r
   if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL) {\r
     return EFI_UNSUPPORTED;\r
   //\r
   if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL) {\r
     return EFI_UNSUPPORTED;\r
@@ -1423,16 +1435,16 @@ ScsiioToPassThruPacket (
   CommandPacket->SenseDataLength   = Packet->SenseDataLength;\r
 \r
   if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_READ) {\r
   CommandPacket->SenseDataLength   = Packet->SenseDataLength;\r
 \r
   if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_READ) {\r
-    CommandPacket->DataBuffer = Packet->InDataBuffer;\r
+    CommandPacket->DataBuffer     = Packet->InDataBuffer;\r
     CommandPacket->TransferLength = Packet->InTransferLength;\r
   } else if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_WRITE) {\r
     CommandPacket->TransferLength = Packet->InTransferLength;\r
   } else if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_WRITE) {\r
-    CommandPacket->DataBuffer = Packet->OutDataBuffer;\r
+    CommandPacket->DataBuffer     = Packet->OutDataBuffer;\r
     CommandPacket->TransferLength = Packet->OutTransferLength;\r
   }\r
     CommandPacket->TransferLength = Packet->OutTransferLength;\r
   }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.\r
 \r
 /**\r
   Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.\r
 \r
@@ -1457,10 +1469,10 @@ PassThruToScsiioPacket (
   Packet->SenseDataLength   = ScsiPacket->SenseDataLength;\r
 \r
   if (ScsiPacket->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_READ) {\r
   Packet->SenseDataLength   = ScsiPacket->SenseDataLength;\r
 \r
   if (ScsiPacket->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_READ) {\r
-    Packet->InDataBuffer = ScsiPacket->DataBuffer;\r
+    Packet->InDataBuffer     = ScsiPacket->DataBuffer;\r
     Packet->InTransferLength = ScsiPacket->TransferLength;\r
   } else if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_WRITE) {\r
     Packet->InTransferLength = ScsiPacket->TransferLength;\r
   } else if (Packet->DataDirection == EFI_SCSI_IO_DATA_DIRECTION_WRITE) {\r
-    Packet->OutDataBuffer = ScsiPacket->DataBuffer;\r
+    Packet->OutDataBuffer     = ScsiPacket->DataBuffer;\r
     Packet->OutTransferLength = ScsiPacket->TransferLength;\r
   }\r
 \r
     Packet->OutTransferLength = ScsiPacket->TransferLength;\r
   }\r
 \r
@@ -1482,31 +1494,30 @@ NotifyFunction (
   IN  VOID       *Context\r
   )\r
 {\r
   IN  VOID       *Context\r
   )\r
 {\r
-  EFI_SCSI_IO_SCSI_REQUEST_PACKET          *Packet;\r
-  EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET   *ScsiPacket;\r
-  EFI_EVENT                                CallerEvent;\r
-  SCSI_EVENT_DATA                          *PassData;\r
+  EFI_SCSI_IO_SCSI_REQUEST_PACKET         *Packet;\r
+  EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET  *ScsiPacket;\r
+  EFI_EVENT                               CallerEvent;\r
+  SCSI_EVENT_DATA                         *PassData;\r
 \r
 \r
-  PassData = (SCSI_EVENT_DATA*)Context;\r
-  Packet  = (EFI_SCSI_IO_SCSI_REQUEST_PACKET *)PassData->Data1;\r
-  ScsiPacket =  (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET*)mWorkingBuffer;\r
+  PassData   = (SCSI_EVENT_DATA *)Context;\r
+  Packet     = (EFI_SCSI_IO_SCSI_REQUEST_PACKET *)PassData->Data1;\r
+  ScsiPacket =  (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *)mWorkingBuffer;\r
 \r
   //\r
   // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.\r
   //\r
 \r
   //\r
   // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.\r
   //\r
-  PassThruToScsiioPacket(ScsiPacket, Packet);\r
+  PassThruToScsiioPacket (ScsiPacket, Packet);\r
 \r
   //\r
   // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,\r
   // free mWorkingBuffer.\r
   //\r
 \r
   //\r
   // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,\r
   // free mWorkingBuffer.\r
   //\r
-  gBS->FreePool(mWorkingBuffer);\r
+  gBS->FreePool (mWorkingBuffer);\r
 \r
   //\r
   // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.\r
   //\r
   CallerEvent = PassData->Data2;\r
 \r
   //\r
   // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.\r
   //\r
   CallerEvent = PassData->Data2;\r
-  gBS->CloseEvent(Event);\r
-  gBS->SignalEvent(CallerEvent);\r
+  gBS->CloseEvent (Event);\r
+  gBS->SignalEvent (CallerEvent);\r
 }\r
 }\r
-\r