2 SCSI Bus driver that layers on every SCSI Pass Thru and
3 Extended SCSI Pass Thru protocol in the system.
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 EFI_DRIVER_BINDING_PROTOCOL gSCSIBusDriverBinding
= {
21 SCSIBusDriverBindingSupported
,
22 SCSIBusDriverBindingStart
,
23 SCSIBusDriverBindingStop
,
32 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
34 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
35 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
40 ScsiioToPassThruPacket (
41 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
42 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
46 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
48 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
49 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
54 PassThruToScsiioPacket (
55 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
56 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
60 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
63 @param Event The instance of EFI_EVENT.
64 @param Context The parameter passed in.
75 Allocates an aligned buffer for SCSI device.
77 This function allocates an aligned buffer for the SCSI device to perform
78 SCSI pass through operations. The alignment requirement is from SCSI pass
81 @param ScsiIoDevice The SCSI child device involved for the operation.
82 @param BufferSize The request buffer size.
84 @return A pointer to the aligned buffer or NULL if the allocation fails.
88 AllocateAlignedBuffer (
89 IN SCSI_IO_DEV
*ScsiIoDevice
,
93 return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), ScsiIoDevice
->ScsiIo
.IoAlign
);
97 Frees an aligned buffer for SCSI device.
99 This function frees an aligned buffer for the SCSI device to perform
100 SCSI pass through operations.
102 @param Buffer The aligned buffer to be freed.
103 @param BufferSize The request buffer size.
112 if (Buffer
!= NULL
) {
113 FreeAlignedPages (Buffer
, EFI_SIZE_TO_PAGES (BufferSize
));
118 The user Entry Point for module ScsiBus. The user code starts with this function.
120 @param ImageHandle The firmware allocated handle for the EFI image.
121 @param SystemTable A pointer to the EFI System Table.
123 @retval EFI_SUCCESS The entry point is executed successfully.
124 @retval other Some error occurs when executing this entry point.
130 IN EFI_HANDLE ImageHandle
,
131 IN EFI_SYSTEM_TABLE
*SystemTable
137 // Install driver model protocol(s).
139 Status
= EfiLibInstallDriverBindingComponentName2 (
142 &gSCSIBusDriverBinding
,
144 &gScsiBusComponentName
,
145 &gScsiBusComponentName2
147 ASSERT_EFI_ERROR (Status
);
154 Test to see if this driver supports ControllerHandle.
156 This service is called by the EFI boot service ConnectController(). In order
157 to make drivers as small as possible, there are a few calling restrictions for
158 this service. ConnectController() must follow these calling restrictions. If
159 any other agent wishes to call Supported() it must also follow these calling
162 @param This Protocol instance pointer.
163 @param ControllerHandle Handle of device to test
164 @param RemainingDevicePath Optional parameter use to pick a specific child
167 @retval EFI_SUCCESS This driver supports this device
168 @retval EFI_ALREADY_STARTED This driver is already running on this device
169 @retval other This driver does not support this device
174 SCSIBusDriverBindingSupported (
175 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
176 IN EFI_HANDLE Controller
,
177 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
181 EFI_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
182 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtPassThru
;
185 SCSI_TARGET_ID ScsiTargetId
;
187 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
188 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
191 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
192 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
193 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
195 Status
= gBS
->OpenProtocol (
197 &gEfiExtScsiPassThruProtocolGuid
,
198 (VOID
**)&ExtPassThru
,
199 This
->DriverBindingHandle
,
201 EFI_OPEN_PROTOCOL_BY_DRIVER
204 if (Status
== EFI_ALREADY_STARTED
) {
206 } else if (!EFI_ERROR(Status
)) {
208 // Check if RemainingDevicePath is NULL or the End of Device Path Node,
209 // if yes, return EFI_SUCCESS.
211 if ((RemainingDevicePath
== NULL
) || IsDevicePathEnd (RemainingDevicePath
)) {
213 // Close protocol regardless of RemainingDevicePath validation
217 &gEfiExtScsiPassThruProtocolGuid
,
218 This
->DriverBindingHandle
,
224 // If RemainingDevicePath isn't the End of Device Path Node, check its validation
226 Status
= ExtPassThru
->GetTargetLun (ExtPassThru
, RemainingDevicePath
, &TargetId
, &Lun
);
228 // Close protocol regardless of RemainingDevicePath validation
232 &gEfiExtScsiPassThruProtocolGuid
,
233 This
->DriverBindingHandle
,
236 if (!EFI_ERROR(Status
)) {
243 // Come here in 2 condition:
244 // 1. ExtPassThru doesn't exist.
245 // 2. ExtPassThru exists but RemainingDevicePath is invalid.
247 Status
= gBS
->OpenProtocol (
249 &gEfiScsiPassThruProtocolGuid
,
251 This
->DriverBindingHandle
,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
256 if (Status
== EFI_ALREADY_STARTED
) {
260 if (EFI_ERROR (Status
)) {
265 // Test RemainingDevicePath is valid or not.
267 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
268 Status
= PassThru
->GetTargetLun (PassThru
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
273 &gEfiScsiPassThruProtocolGuid
,
274 This
->DriverBindingHandle
,
282 Start this driver on ControllerHandle.
284 This service is called by the EFI boot service ConnectController(). In order
285 to make drivers as small as possible, there are a few calling restrictions for
286 this service. ConnectController() must follow these calling restrictions. If
287 any other agent wishes to call Start() it must also follow these calling
290 @param This Protocol instance pointer.
291 @param ControllerHandle Handle of device to bind driver to
292 @param RemainingDevicePath Optional parameter use to pick a specific child
295 @retval EFI_SUCCESS This driver is added to ControllerHandle
296 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
297 @retval other This driver does not support this device
302 SCSIBusDriverBindingStart (
303 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
304 IN EFI_HANDLE Controller
,
305 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
310 BOOLEAN ScanOtherPuns
;
311 BOOLEAN FromFirstTarget
;
312 BOOLEAN ExtScsiSupport
;
314 EFI_STATUS DevicePathStatus
;
315 EFI_STATUS PassThruStatus
;
316 SCSI_BUS_DEVICE
*ScsiBusDev
;
317 SCSI_TARGET_ID ScsiTargetId
;
318 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
319 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiInterface
;
320 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiInterface
;
321 EFI_SCSI_BUS_PROTOCOL
*BusIdentify
;
324 ScanOtherPuns
= TRUE
;
325 FromFirstTarget
= FALSE
;
326 ExtScsiSupport
= FALSE
;
327 PassThruStatus
= EFI_SUCCESS
;
329 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
330 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
332 DevicePathStatus
= gBS
->OpenProtocol (
334 &gEfiDevicePathProtocolGuid
,
335 (VOID
**) &ParentDevicePath
,
336 This
->DriverBindingHandle
,
338 EFI_OPEN_PROTOCOL_BY_DRIVER
340 if (EFI_ERROR (DevicePathStatus
) && (DevicePathStatus
!= EFI_ALREADY_STARTED
)) {
341 return DevicePathStatus
;
345 // Report Status Code to indicate SCSI bus starts
347 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
349 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_INIT
),
354 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
355 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
356 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
358 Status
= gBS
->OpenProtocol (
360 &gEfiExtScsiPassThruProtocolGuid
,
361 (VOID
**) &ExtScsiInterface
,
362 This
->DriverBindingHandle
,
364 EFI_OPEN_PROTOCOL_BY_DRIVER
367 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
369 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
370 Status
= gBS
->OpenProtocol (
372 &gEfiScsiPassThruProtocolGuid
,
373 (VOID
**) &ScsiInterface
,
374 This
->DriverBindingHandle
,
376 EFI_OPEN_PROTOCOL_BY_DRIVER
379 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
381 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
382 if (!EFI_ERROR(DevicePathStatus
)) {
385 &gEfiDevicePathProtocolGuid
,
386 This
->DriverBindingHandle
,
394 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
395 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
396 // another SCSI Bus Driver to work on the same host handle.
398 ExtScsiSupport
= TRUE
;
399 PassThruStatus
= gBS
->OpenProtocol (
401 &gEfiScsiPassThruProtocolGuid
,
402 (VOID
**) &ScsiInterface
,
403 This
->DriverBindingHandle
,
405 EFI_OPEN_PROTOCOL_BY_DRIVER
409 if (Status
!= EFI_ALREADY_STARTED
) {
411 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
412 // on this handle for this time. Then construct Host controller private data.
415 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
416 if (ScsiBusDev
== NULL
) {
417 Status
= EFI_OUT_OF_RESOURCES
;
420 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
421 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
422 ScsiBusDev
->DevicePath
= ParentDevicePath
;
423 if (ScsiBusDev
->ExtScsiSupport
) {
424 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
426 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
430 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
431 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
432 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
434 Status
= gBS
->InstallProtocolInterface (
437 EFI_NATIVE_INTERFACE
,
438 &ScsiBusDev
->BusIdentify
440 if (EFI_ERROR (Status
)) {
445 // Go through here means Start() is re-invoked again, nothing special is required to do except
446 // picking up Host controller private information.
448 Status
= gBS
->OpenProtocol (
451 (VOID
**) &BusIdentify
,
452 This
->DriverBindingHandle
,
454 EFI_OPEN_PROTOCOL_GET_PROTOCOL
457 if (EFI_ERROR (Status
)) {
460 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
464 // Report Status Code to indicate detecting devices on bus
466 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
468 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_DETECT
),
473 if (RemainingDevicePath
== NULL
) {
475 // If RemainingDevicePath is NULL,
476 // must enumerate all SCSI devices anyway
478 FromFirstTarget
= TRUE
;
479 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
481 // If RemainingDevicePath isn't the End of Device Path Node,
482 // only scan the specified device by RemainingDevicePath
484 if (ScsiBusDev
->ExtScsiSupport
) {
485 Status
= ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
487 Status
= ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
490 if (EFI_ERROR (Status
)) {
495 // If RemainingDevicePath is the End of Device Path Node,
496 // skip enumerate any device and return EFI_SUCESSS
498 ScanOtherPuns
= FALSE
;
501 while(ScanOtherPuns
) {
502 if (FromFirstTarget
) {
504 // Remaining Device Path is NULL, scan all the possible Puns in the
507 if (ScsiBusDev
->ExtScsiSupport
) {
508 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
510 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
512 if (EFI_ERROR (Status
)) {
514 // no legal Pun and Lun found any more
519 ScanOtherPuns
= FALSE
;
522 // Avoid creating handle for the host adapter.
524 if (ScsiBusDev
->ExtScsiSupport
) {
525 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
529 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
534 // Scan for the scsi device, if it attaches to the scsi bus,
535 // then create handle and install scsi i/o protocol.
537 Status
= ScsiScanCreateDevice (This
, Controller
, &ScsiTargetId
, Lun
, ScsiBusDev
);
543 if (ScsiBusDev
!= NULL
) {
544 FreePool (ScsiBusDev
);
547 if (ExtScsiSupport
) {
550 &gEfiExtScsiPassThruProtocolGuid
,
551 This
->DriverBindingHandle
,
554 if (!EFI_ERROR (PassThruStatus
)) {
557 &gEfiScsiPassThruProtocolGuid
,
558 This
->DriverBindingHandle
,
565 &gEfiScsiPassThruProtocolGuid
,
566 This
->DriverBindingHandle
,
574 Stop this driver on ControllerHandle.
576 This service is called by the EFI boot service DisconnectController().
577 In order to make drivers as small as possible, there are a few calling
578 restrictions for this service. DisconnectController() must follow these
579 calling restrictions. If any other agent wishes to call Stop() it must also
580 follow these calling restrictions.
582 @param This Protocol instance pointer.
583 @param ControllerHandle Handle of device to stop driver on
584 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
585 children is zero stop the entire bus driver.
586 @param ChildHandleBuffer List of Child Handles to Stop.
588 @retval EFI_SUCCESS This driver is removed ControllerHandle
589 @retval other This driver was not removed from this device
594 SCSIBusDriverBindingStop (
595 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
596 IN EFI_HANDLE Controller
,
597 IN UINTN NumberOfChildren
,
598 IN EFI_HANDLE
*ChildHandleBuffer
602 BOOLEAN AllChildrenStopped
;
604 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
605 SCSI_IO_DEV
*ScsiIoDevice
;
607 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
608 SCSI_BUS_DEVICE
*ScsiBusDev
;
610 if (NumberOfChildren
== 0) {
612 // Get the SCSI_BUS_DEVICE
614 Status
= gBS
->OpenProtocol (
617 (VOID
**) &Scsidentifier
,
618 This
->DriverBindingHandle
,
620 EFI_OPEN_PROTOCOL_GET_PROTOCOL
623 if (EFI_ERROR (Status
)) {
624 return EFI_DEVICE_ERROR
;
627 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
630 // Uninstall SCSI Bus Protocol
632 gBS
->UninstallProtocolInterface (
635 &ScsiBusDev
->BusIdentify
639 // Close the bus driver
641 if (ScsiBusDev
->ExtScsiSupport
) {
643 // Close ExtPassThru Protocol from this controller handle
647 &gEfiExtScsiPassThruProtocolGuid
,
648 This
->DriverBindingHandle
,
652 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
653 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
654 // So Stop() needs to try to close PassThru if present here.
658 &gEfiScsiPassThruProtocolGuid
,
659 This
->DriverBindingHandle
,
665 &gEfiScsiPassThruProtocolGuid
,
666 This
->DriverBindingHandle
,
673 &gEfiDevicePathProtocolGuid
,
674 This
->DriverBindingHandle
,
677 FreePool (ScsiBusDev
);
681 AllChildrenStopped
= TRUE
;
683 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
685 Status
= gBS
->OpenProtocol (
686 ChildHandleBuffer
[Index
],
687 &gEfiScsiIoProtocolGuid
,
689 This
->DriverBindingHandle
,
691 EFI_OPEN_PROTOCOL_GET_PROTOCOL
693 if (EFI_ERROR (Status
)) {
694 AllChildrenStopped
= FALSE
;
698 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
700 // Close the child handle
702 if (ScsiIoDevice
->ExtScsiSupport
) {
703 Status
= gBS
->CloseProtocol (
705 &gEfiExtScsiPassThruProtocolGuid
,
706 This
->DriverBindingHandle
,
707 ChildHandleBuffer
[Index
]
711 Status
= gBS
->CloseProtocol (
713 &gEfiScsiPassThruProtocolGuid
,
714 This
->DriverBindingHandle
,
715 ChildHandleBuffer
[Index
]
719 Status
= gBS
->UninstallMultipleProtocolInterfaces (
720 ChildHandleBuffer
[Index
],
721 &gEfiDevicePathProtocolGuid
,
722 ScsiIoDevice
->DevicePath
,
723 &gEfiScsiIoProtocolGuid
,
724 &ScsiIoDevice
->ScsiIo
,
727 if (EFI_ERROR (Status
)) {
728 AllChildrenStopped
= FALSE
;
729 if (ScsiIoDevice
->ExtScsiSupport
) {
732 &gEfiExtScsiPassThruProtocolGuid
,
734 This
->DriverBindingHandle
,
735 ChildHandleBuffer
[Index
],
736 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
741 &gEfiScsiPassThruProtocolGuid
,
743 This
->DriverBindingHandle
,
744 ChildHandleBuffer
[Index
],
745 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
749 FreePool (ScsiIoDevice
);
753 if (!AllChildrenStopped
) {
754 return EFI_DEVICE_ERROR
;
762 Retrieves the device type information of the SCSI Controller.
764 @param This Protocol instance pointer.
765 @param DeviceType A pointer to the device type information retrieved from
768 @retval EFI_SUCCESS Retrieves the device type information successfully.
769 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
775 IN EFI_SCSI_IO_PROTOCOL
*This
,
776 OUT UINT8
*DeviceType
779 SCSI_IO_DEV
*ScsiIoDevice
;
781 if (DeviceType
== NULL
) {
782 return EFI_INVALID_PARAMETER
;
785 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
786 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
792 Retrieves the device location in the SCSI channel.
794 @param This Protocol instance pointer.
795 @param Target A pointer to the Target ID of a SCSI device
797 @param Lun A pointer to the LUN of the SCSI device on
800 @retval EFI_SUCCESS Retrieves the device location successfully.
801 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
806 ScsiGetDeviceLocation (
807 IN EFI_SCSI_IO_PROTOCOL
*This
,
808 IN OUT UINT8
**Target
,
812 SCSI_IO_DEV
*ScsiIoDevice
;
814 if (Target
== NULL
|| Lun
== NULL
) {
815 return EFI_INVALID_PARAMETER
;
818 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
820 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
822 *Lun
= ScsiIoDevice
->Lun
;
828 Resets the SCSI Bus that the SCSI Controller is attached to.
830 @param This Protocol instance pointer.
832 @retval EFI_SUCCESS The SCSI bus is reset successfully.
833 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
834 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
835 SCSI Host Controller.
836 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
842 IN EFI_SCSI_IO_PROTOCOL
*This
845 SCSI_IO_DEV
*ScsiIoDevice
;
847 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
850 // Report Status Code to indicate reset happens
852 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
854 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
855 ScsiIoDevice
->ScsiBusDeviceData
->DevicePath
858 if (ScsiIoDevice
->ExtScsiSupport
){
859 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
861 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
867 Resets the SCSI Controller that the device handle specifies.
869 @param This Protocol instance pointer.
871 @retval EFI_SUCCESS Reset the SCSI controller successfully.
872 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
873 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
874 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
880 IN EFI_SCSI_IO_PROTOCOL
*This
883 SCSI_IO_DEV
*ScsiIoDevice
;
884 UINT8 Target
[TARGET_MAX_BYTES
];
886 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
889 // Report Status Code to indicate reset happens
891 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
893 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
894 ScsiIoDevice
->ScsiBusDeviceData
->DevicePath
897 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
900 if (ScsiIoDevice
->ExtScsiSupport
) {
901 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
902 ScsiIoDevice
->ExtScsiPassThru
,
907 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
908 ScsiIoDevice
->ScsiPassThru
,
909 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
917 Sends a SCSI Request Packet to the SCSI Controller for execution.
919 @param This Protocol instance pointer.
920 @param CommandPacket The SCSI request packet to send to the SCSI
921 Controller specified by the device handle.
922 @param Event If the SCSI bus where the SCSI device is attached
923 does not support non-blocking I/O, then Event is
924 ignored, and blocking I/O is performed.
925 If Event is NULL, then blocking I/O is performed.
926 If Event is not NULL and non-blocking I/O is
927 supported, then non-blocking I/O is performed,
928 and Event will be signaled when the SCSI Request
931 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
932 successfully, and TransferLength bytes were
933 transferred to/from DataBuffer.See
934 HostAdapterStatus, TargetStatus,
935 SenseDataLength, and SenseData in that order
936 for additional status information.
937 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
938 but the entire DataBuffer could not be transferred.
939 The actual number of bytes transferred is returned
940 in TransferLength. See HostAdapterStatus,
941 TargetStatus, SenseDataLength, and SenseData in
942 that order for additional status information.
943 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
944 there are too many SCSI Command Packets already
945 queued.The caller may retry again later.
946 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
947 the SCSI Request Packet. See HostAdapterStatus,
948 TargetStatus, SenseDataLength, and SenseData in
949 that order for additional status information.
950 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
951 The SCSI Request Packet was not sent, so no
952 additional status information is available.
953 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
954 is not supported by the SCSI initiator(i.e., SCSI
955 Host Controller). The SCSI Request Packet was not
956 sent, so no additional status information is
958 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
959 Request Packet to execute. See HostAdapterStatus,
960 TargetStatus, SenseDataLength, and SenseData in
961 that order for additional status information.
965 ScsiExecuteSCSICommand (
966 IN EFI_SCSI_IO_PROTOCOL
*This
,
967 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
968 IN EFI_EVENT Event OPTIONAL
971 SCSI_IO_DEV
*ScsiIoDevice
;
973 UINT8 Target
[TARGET_MAX_BYTES
];
974 EFI_EVENT PacketEvent
;
975 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
976 SCSI_EVENT_DATA EventData
;
980 if (Packet
== NULL
) {
981 return EFI_INVALID_PARAMETER
;
984 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
985 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
987 if (ScsiIoDevice
->ExtScsiSupport
) {
988 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
990 if (((ScsiIoDevice
->ExtScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
991 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
992 ScsiIoDevice
->ExtScsiPassThru
,
1000 // If there's no event or the SCSI Device doesn't support NON-BLOCKING,
1001 // let the 'Event' parameter for PassThru() be NULL.
1003 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
1004 ScsiIoDevice
->ExtScsiPassThru
,
1010 if ((!EFI_ERROR(Status
)) && (Event
!= NULL
)) {
1012 // Signal Event to tell caller to pick up the SCSI IO packet if the
1013 // PassThru() succeeds.
1015 gBS
->SignalEvent (Event
);
1020 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1022 if (mWorkingBuffer
== NULL
) {
1023 return EFI_DEVICE_ERROR
;
1027 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
1029 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
1030 if (EFI_ERROR(Status
)) {
1031 FreePool(mWorkingBuffer
);
1035 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
1036 EventData
.Data1
= (VOID
*)Packet
;
1037 EventData
.Data2
= Event
;
1041 Status
= gBS
->CreateEvent (
1048 if (EFI_ERROR(Status
)) {
1049 FreePool(mWorkingBuffer
);
1053 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
1054 ScsiIoDevice
->ScsiPassThru
,
1055 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1061 if (EFI_ERROR(Status
)) {
1062 FreePool(mWorkingBuffer
);
1063 gBS
->CloseEvent(PacketEvent
);
1069 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
1070 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
1072 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
1073 ScsiIoDevice
->ScsiPassThru
,
1074 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1079 if (EFI_ERROR(Status
)) {
1080 FreePool(mWorkingBuffer
);
1084 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
1086 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1087 // free mWorkingBuffer.
1089 FreePool(mWorkingBuffer
);
1092 // Signal Event to tell caller to pick up the SCSI IO Packet.
1094 if (Event
!= NULL
) {
1095 gBS
->SignalEvent (Event
);
1104 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
1106 @param This Protocol instance pointer
1107 @param Controller Controller handle
1108 @param TargetId Tartget to be scanned
1109 @param Lun The Lun of the SCSI device on the SCSI channel.
1110 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
1112 @retval EFI_SUCCESS Successfully to discover the device and attach
1113 ScsiIoProtocol to it.
1114 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
1119 ScsiScanCreateDevice (
1120 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1121 IN EFI_HANDLE Controller
,
1122 IN SCSI_TARGET_ID
*TargetId
,
1124 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
1128 SCSI_IO_DEV
*ScsiIoDevice
;
1129 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
1130 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1131 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1132 EFI_HANDLE DeviceHandle
;
1135 RemainingDevicePath
= NULL
;
1136 ScsiDevicePath
= NULL
;
1137 ScsiIoDevice
= NULL
;
1140 // Build Device Path
1142 if (ScsiBusDev
->ExtScsiSupport
){
1143 Status
= ScsiBusDev
->ExtScsiInterface
->BuildDevicePath (
1144 ScsiBusDev
->ExtScsiInterface
,
1145 &TargetId
->ScsiId
.ExtScsi
[0],
1150 Status
= ScsiBusDev
->ScsiInterface
->BuildDevicePath (
1151 ScsiBusDev
->ScsiInterface
,
1152 TargetId
->ScsiId
.Scsi
,
1158 if (EFI_ERROR(Status
)) {
1162 DevicePath
= AppendDevicePathNode (
1163 ScsiBusDev
->DevicePath
,
1167 if (DevicePath
== NULL
) {
1168 Status
= EFI_OUT_OF_RESOURCES
;
1172 DeviceHandle
= NULL
;
1173 RemainingDevicePath
= DevicePath
;
1174 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
1175 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(RemainingDevicePath
)) {
1177 // The device has been started, directly return to fast boot.
1179 Status
= EFI_ALREADY_STARTED
;
1183 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
1184 if (ScsiIoDevice
== NULL
) {
1185 Status
= EFI_OUT_OF_RESOURCES
;
1189 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
1190 ScsiIoDevice
->ScsiBusDeviceData
= ScsiBusDev
;
1191 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
1192 ScsiIoDevice
->Lun
= Lun
;
1194 if (ScsiBusDev
->ExtScsiSupport
) {
1195 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
1196 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
1197 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
1200 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
1201 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
1202 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
1205 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
1206 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
1207 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
1208 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
1209 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1212 // Report Status Code here since the new SCSI device will be discovered
1214 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1216 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_ENABLE
),
1217 ScsiBusDev
->DevicePath
1220 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1221 Status
= EFI_OUT_OF_RESOURCES
;
1225 ScsiIoDevice
->DevicePath
= DevicePath
;
1227 Status
= gBS
->InstallMultipleProtocolInterfaces (
1228 &ScsiIoDevice
->Handle
,
1229 &gEfiDevicePathProtocolGuid
,
1230 ScsiIoDevice
->DevicePath
,
1231 &gEfiScsiIoProtocolGuid
,
1232 &ScsiIoDevice
->ScsiIo
,
1235 if (EFI_ERROR (Status
)) {
1238 if (ScsiBusDev
->ExtScsiSupport
) {
1241 &gEfiExtScsiPassThruProtocolGuid
,
1242 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1243 This
->DriverBindingHandle
,
1244 ScsiIoDevice
->Handle
,
1245 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1250 &gEfiScsiPassThruProtocolGuid
,
1251 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1252 This
->DriverBindingHandle
,
1253 ScsiIoDevice
->Handle
,
1254 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1263 // The memory space for ScsiDevicePath is allocated in
1264 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1265 // after AppendDevicePathNode,so free the memory it occupies.
1267 FreePool (ScsiDevicePath
);
1269 if (DevicePath
!= NULL
) {
1270 FreePool (DevicePath
);
1273 if (ScsiIoDevice
!= NULL
) {
1274 FreePool (ScsiIoDevice
);
1282 Discovery SCSI Device
1284 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1286 @retval TRUE Find SCSI Device and verify it.
1287 @retval FALSE Unable to find SCSI Device.
1291 DiscoverScsiDevice (
1292 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1296 UINT32 InquiryDataLength
;
1297 UINT8 SenseDataLength
;
1298 UINT8 HostAdapterStatus
;
1300 EFI_SCSI_INQUIRY_DATA
*InquiryData
;
1303 BOOLEAN ScsiDeviceFound
;
1305 HostAdapterStatus
= 0;
1308 InquiryData
= AllocateAlignedBuffer (ScsiIoDevice
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1309 if (InquiryData
== NULL
) {
1310 ScsiDeviceFound
= FALSE
;
1315 // Using Inquiry command to scan for the device
1317 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1318 SenseDataLength
= 0;
1319 ZeroMem (InquiryData
, InquiryDataLength
);
1322 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1323 Status
= ScsiInquiryCommand (
1324 &ScsiIoDevice
->ScsiIo
,
1330 (VOID
*) InquiryData
,
1334 if (!EFI_ERROR (Status
)) {
1336 } else if ((Status
== EFI_BAD_BUFFER_SIZE
) ||
1337 (Status
== EFI_INVALID_PARAMETER
) ||
1338 (Status
== EFI_UNSUPPORTED
)) {
1339 ScsiDeviceFound
= FALSE
;
1344 if (Index
== MaxRetry
) {
1345 ScsiDeviceFound
= FALSE
;
1350 // Retrieved inquiry data successfully
1352 if (InquiryData
->Peripheral_Qualifier
!= 0) {
1353 ScsiDeviceFound
= FALSE
;
1357 if (0x1e >= InquiryData
->Peripheral_Type
&& InquiryData
->Peripheral_Type
>= 0xa) {
1358 ScsiDeviceFound
= FALSE
;
1363 // valid device type and peripheral qualifier combination.
1365 ScsiIoDevice
->ScsiDeviceType
= InquiryData
->Peripheral_Type
;
1366 ScsiIoDevice
->RemovableDevice
= InquiryData
->Rmb
;
1367 if (InquiryData
->Version
== 0) {
1368 ScsiIoDevice
->ScsiVersion
= 0;
1371 // ANSI-approved version
1373 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
->Version
& 0x07);
1376 ScsiDeviceFound
= TRUE
;
1379 FreeAlignedBuffer (InquiryData
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1381 return ScsiDeviceFound
;
1386 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1388 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1389 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1394 ScsiioToPassThruPacket (
1395 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1396 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1400 //EFI 1.10 doesn't support Bi-Direction Command.
1402 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1403 return EFI_UNSUPPORTED
;
1406 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1408 CommandPacket
->Timeout
= Packet
->Timeout
;
1409 CommandPacket
->Cdb
= Packet
->Cdb
;
1410 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1411 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1412 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1413 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1414 CommandPacket
->SenseData
= Packet
->SenseData
;
1415 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1417 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1418 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1419 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1420 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1421 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1422 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1429 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1431 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1432 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1437 PassThruToScsiioPacket (
1438 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1439 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1442 Packet
->Timeout
= ScsiPacket
->Timeout
;
1443 Packet
->Cdb
= ScsiPacket
->Cdb
;
1444 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1445 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1446 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1447 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1448 Packet
->SenseData
= ScsiPacket
->SenseData
;
1449 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1451 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1452 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1453 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1454 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1455 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1456 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1463 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1466 @param Event The instance of EFI_EVENT.
1467 @param Context The parameter passed in.
1477 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1478 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1479 EFI_EVENT CallerEvent
;
1480 SCSI_EVENT_DATA
*PassData
;
1482 PassData
= (SCSI_EVENT_DATA
*)Context
;
1483 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1484 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1487 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1489 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1492 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1493 // free mWorkingBuffer.
1495 gBS
->FreePool(mWorkingBuffer
);
1498 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1500 CallerEvent
= PassData
->Data2
;
1501 gBS
->CloseEvent(Event
);
1502 gBS
->SignalEvent(CallerEvent
);