]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Scsi/ScsiBusDxe/ScsiBus.c
when remaining device path is not NULL, check if it's the device managed by bus driver.
[mirror_edk2.git] / MdeModulePkg / Bus / Scsi / ScsiBusDxe / ScsiBus.c
index 654e5c042cf38423b5a4be8f66533011e3b8ddf0..eaa4658e35813e8c0f670d8e095ff8bc07802345 100644 (file)
@@ -2,7 +2,7 @@
   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 - 2008, Intel Corporation. <BR>\r
+Copyright (c) 2006 - 2010, Intel Corporation. <BR>\r
 All rights reserved. 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
@@ -142,11 +142,20 @@ SCSIBusDriverBindingSupported (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  EFI_SCSI_PASS_THRU_PROTOCOL *PassThru;\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
-  // Check for the existence of Extended SCSI Pass Thru Protocol and SCSI Pass Thru Protocol\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
@@ -159,43 +168,78 @@ SCSIBusDriverBindingSupported (
 \r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
-  }\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    Status = gBS->OpenProtocol (\r
-                    Controller,\r
-                    &gEfiScsiPassThruProtocolGuid,\r
-                    (VOID **)&PassThru,\r
-                    This->DriverBindingHandle,\r
-                    Controller,\r
-                    EFI_OPEN_PROTOCOL_BY_DRIVER\r
-                    );\r
-\r
-    if (Status == EFI_ALREADY_STARTED) {\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
+    if ((RemainingDevicePath == NULL) || IsDevicePathEnd (RemainingDevicePath)) {\r
+      //\r
+      // Close protocol regardless of RemainingDevicePath validation\r
+      //\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiExtScsiPassThruProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );      \r
       return EFI_SUCCESS;\r
+    } else {\r
+      //\r
+      // If RemainingDevicePath isn't the End of Device Path Node, check its validation\r
+      //\r
+      Status = ExtPassThru->GetTargetLun (ExtPassThru, RemainingDevicePath, &TargetId, &Lun);\r
+      //\r
+      // Close protocol regardless of RemainingDevicePath validation\r
+      //\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiExtScsiPassThruProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );      \r
+      if (!EFI_ERROR(Status)) {\r
+        return EFI_SUCCESS;\r
+      }\r
     }\r
+  }\r
 \r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    gBS->CloseProtocol (\r
-      Controller,\r
-      &gEfiScsiPassThruProtocolGuid,\r
-      This->DriverBindingHandle,\r
-      Controller\r
-      );\r
+  //\r
+  // Come here in 2 condition: \r
+  // 1. ExtPassThru doesn't exist.\r
+  // 2. ExtPassThru exists but RemainingDevicePath is invalid.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gEfiScsiPassThruProtocolGuid,\r
+                  (VOID **)&PassThru,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  \r
+  if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
   }\r
-\r
+  \r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\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
   gBS->CloseProtocol (\r
-    Controller,\r
-    &gEfiExtScsiPassThruProtocolGuid,\r
-    This->DriverBindingHandle,\r
-    Controller\r
-    );\r
-\r
-  return EFI_SUCCESS;\r
+         Controller,\r
+         &gEfiScsiPassThruProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
+  return Status;\r
 }\r
 \r
 \r
@@ -235,25 +279,20 @@ SCSIBusDriverBindingStart (
   EFI_STATUS                            DevicePathStatus;\r
   EFI_STATUS                            PassThruStatus;\r
   SCSI_BUS_DEVICE                       *ScsiBusDev;\r
-  SCSI_TARGET_ID                        *ScsiTargetId;\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
-  ScsiTargetId    = NULL;\r
   ScanOtherPuns   = TRUE;\r
   FromFirstTarget = FALSE;\r
   ExtScsiSupport  = FALSE;\r
   PassThruStatus  = EFI_SUCCESS;\r
   \r
-  ScsiTargetId = AllocateZeroPool(sizeof(SCSI_TARGET_ID));\r
-  if (ScsiTargetId == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  TargetId = &ScsiTargetId->ScsiId.ExtScsi[0];\r
+  TargetId = &ScsiTargetId.ScsiId.ExtScsi[0];\r
+  SetMem (TargetId, TARGET_MAX_BYTES, 0xFF);\r
   \r
   DevicePathStatus = gBS->OpenProtocol (\r
                             Controller,\r
@@ -377,16 +416,33 @@ SCSIBusDriverBindingStart (
     ScsiBusDev = SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify);\r
   }\r
 \r
+  Lun  = 0;\r
   if (RemainingDevicePath == NULL) {\r
-    SetMem (ScsiTargetId, TARGET_MAX_BYTES,0xFF);\r
-    Lun  = 0;\r
+    //\r
+    // If RemainingDevicePath is NULL, \r
+    // must enumerate all SCSI devices anyway\r
+    //\r
     FromFirstTarget = TRUE;\r
-  } else {\r
+  } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
+    //\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
-      ScsiBusDev->ExtScsiInterface->GetTargetLun (ScsiBusDev->ExtScsiInterface, RemainingDevicePath, &TargetId, &Lun);  \r
+      Status = ScsiBusDev->ExtScsiInterface->GetTargetLun (ScsiBusDev->ExtScsiInterface, RemainingDevicePath, &TargetId, &Lun);  \r
     } else {\r
-      ScsiBusDev->ScsiInterface->GetTargetLun (ScsiBusDev->ScsiInterface, RemainingDevicePath, &ScsiTargetId->ScsiId.Scsi, &Lun);\r
+      Status = ScsiBusDev->ScsiInterface->GetTargetLun (ScsiBusDev->ScsiInterface, RemainingDevicePath, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
     }\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  } else {\r
+    //\r
+    // If RemainingDevicePath is the End of Device Path Node,\r
+    // skip enumerate any device and return EFI_SUCESSS\r
+    // \r
+    ScanOtherPuns = FALSE;\r
   }\r
 \r
   while(ScanOtherPuns) {\r
@@ -398,7 +454,7 @@ SCSIBusDriverBindingStart (
       if (ScsiBusDev->ExtScsiSupport) {\r
         Status = ScsiBusDev->ExtScsiInterface->GetNextTargetLun (ScsiBusDev->ExtScsiInterface, &TargetId, &Lun);\r
       } else {\r
-        Status = ScsiBusDev->ScsiInterface->GetNextDevice (ScsiBusDev->ScsiInterface, &ScsiTargetId->ScsiId.Scsi, &Lun);\r
+        Status = ScsiBusDev->ScsiInterface->GetNextDevice (ScsiBusDev->ScsiInterface, &ScsiTargetId.ScsiId.Scsi, &Lun);\r
       }\r
       if (EFI_ERROR (Status)) {\r
         //\r
@@ -413,11 +469,11 @@ SCSIBusDriverBindingStart (
     // Avoid creating handle for the host adapter.\r
     //\r
     if (ScsiBusDev->ExtScsiSupport) {\r
-      if ((ScsiTargetId->ScsiId.Scsi) == ScsiBusDev->ExtScsiInterface->Mode->AdapterId) {\r
+      if ((ScsiTargetId.ScsiId.Scsi) == ScsiBusDev->ExtScsiInterface->Mode->AdapterId) {\r
         continue;\r
       }\r
     } else {\r
-      if ((ScsiTargetId->ScsiId.Scsi) == ScsiBusDev->ScsiInterface->Mode->AdapterId) {\r
+      if ((ScsiTargetId.ScsiId.Scsi) == ScsiBusDev->ScsiInterface->Mode->AdapterId) {\r
         continue;\r
       }\r
     }\r
@@ -425,9 +481,8 @@ SCSIBusDriverBindingStart (
     // 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
+    Status = ScsiScanCreateDevice (This, Controller, &ScsiTargetId, Lun, ScsiBusDev);\r
   }\r
-  FreePool (ScsiTargetId);\r
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
@@ -531,12 +586,26 @@ SCSIBusDriverBindingStop (
     // Close the bus driver\r
     //\r
     if (ScsiBusDev->ExtScsiSupport) {\r
+      //\r
+      // Close ExtPassThru Protocol from this controller handle\r
+      //\r
       gBS->CloseProtocol (\r
              Controller,\r
              &gEfiExtScsiPassThruProtocolGuid,\r
              This->DriverBindingHandle,\r
              Controller\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
+      // So Stop() needs to try to close PassThru if present here.\r
+      //\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiScsiPassThruProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
     } else {\r
       gBS->CloseProtocol (\r
              Controller,\r
@@ -1001,10 +1070,6 @@ ScsiScanCreateDevice (
                                           ScsiIoDevice->Lun,\r
                                           &ScsiDevicePath\r
                                           );\r
-    if (Status == EFI_OUT_OF_RESOURCES) {\r
-      FreePool (ScsiIoDevice);\r
-      return Status;\r
-    }\r
   } else {\r
     Status = ScsiIoDevice->ScsiPassThru->BuildDevicePath (\r
                                           ScsiIoDevice->ScsiPassThru,\r
@@ -1012,10 +1077,11 @@ ScsiScanCreateDevice (
                                           ScsiIoDevice->Lun,\r
                                           &ScsiDevicePath\r
                                           );\r
-    if (Status == EFI_OUT_OF_RESOURCES) {\r
-      FreePool (ScsiIoDevice);\r
-      return Status;\r
-    }\r
+  }\r
+\r
+  if (EFI_ERROR(Status)) {\r
+    FreePool (ScsiIoDevice);\r
+    return Status;\r
   }\r
 \r
   ScsiIoDevice->DevicePath = AppendDevicePathNode (\r
@@ -1043,6 +1109,7 @@ ScsiScanCreateDevice (
                   NULL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (ScsiIoDevice->DevicePath);\r
     FreePool (ScsiIoDevice);\r
     return EFI_OUT_OF_RESOURCES;\r
   } else {\r
@@ -1111,7 +1178,7 @@ DiscoverScsiDevice (
             &InquiryDataLength,\r
             FALSE\r
             );\r
-  if (EFI_ERROR (Status)) {\r
+  if (EFI_ERROR (Status) && Status != EFI_BAD_BUFFER_SIZE) {\r
     return FALSE;\r
   }\r
   //\r
@@ -1136,7 +1203,7 @@ DiscoverScsiDevice (
   // valid device type and peripheral qualifier combination.\r
   //\r
   ScsiIoDevice->ScsiDeviceType  = InquiryData.Peripheral_Type;\r
-  ScsiIoDevice->RemovableDevice = InquiryData.RMB;\r
+  ScsiIoDevice->RemovableDevice = InquiryData.Rmb;\r
   if (InquiryData.Version == 0) {\r
     ScsiIoDevice->ScsiVersion = 0;\r
   } else {\r