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 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
14 EFI_DRIVER_BINDING_PROTOCOL gSCSIBusDriverBinding
= {
15 SCSIBusDriverBindingSupported
,
16 SCSIBusDriverBindingStart
,
17 SCSIBusDriverBindingStop
,
26 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
28 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
29 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
34 ScsiioToPassThruPacket (
35 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
36 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
40 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
42 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
43 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
48 PassThruToScsiioPacket (
49 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
50 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
54 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
57 @param Event The instance of EFI_EVENT.
58 @param Context The parameter passed in.
69 Allocates an aligned buffer for SCSI device.
71 This function allocates an aligned buffer for the SCSI device to perform
72 SCSI pass through operations. The alignment requirement is from SCSI pass
75 @param ScsiIoDevice The SCSI child device involved for the operation.
76 @param BufferSize The request buffer size.
78 @return A pointer to the aligned buffer or NULL if the allocation fails.
82 AllocateAlignedBuffer (
83 IN SCSI_IO_DEV
*ScsiIoDevice
,
87 return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), ScsiIoDevice
->ScsiIo
.IoAlign
);
91 Frees an aligned buffer for SCSI device.
93 This function frees an aligned buffer for the SCSI device to perform
94 SCSI pass through operations.
96 @param Buffer The aligned buffer to be freed.
97 @param BufferSize The request buffer size.
106 if (Buffer
!= NULL
) {
107 FreeAlignedPages (Buffer
, EFI_SIZE_TO_PAGES (BufferSize
));
112 The user Entry Point for module ScsiBus. The user code starts with this function.
114 @param ImageHandle The firmware allocated handle for the EFI image.
115 @param SystemTable A pointer to the EFI System Table.
117 @retval EFI_SUCCESS The entry point is executed successfully.
118 @retval other Some error occurs when executing this entry point.
124 IN EFI_HANDLE ImageHandle
,
125 IN EFI_SYSTEM_TABLE
*SystemTable
131 // Install driver model protocol(s).
133 Status
= EfiLibInstallDriverBindingComponentName2 (
136 &gSCSIBusDriverBinding
,
138 &gScsiBusComponentName
,
139 &gScsiBusComponentName2
141 ASSERT_EFI_ERROR (Status
);
148 Test to see if this driver supports ControllerHandle.
150 This service is called by the EFI boot service ConnectController(). In order
151 to make drivers as small as possible, there are a few calling restrictions for
152 this service. ConnectController() must follow these calling restrictions. If
153 any other agent wishes to call Supported() it must also follow these calling
156 @param This Protocol instance pointer.
157 @param ControllerHandle Handle of device to test
158 @param RemainingDevicePath Optional parameter use to pick a specific child
161 @retval EFI_SUCCESS This driver supports this device
162 @retval EFI_ALREADY_STARTED This driver is already running on this device
163 @retval other This driver does not support this device
168 SCSIBusDriverBindingSupported (
169 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
170 IN EFI_HANDLE Controller
,
171 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
175 EFI_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
176 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtPassThru
;
179 SCSI_TARGET_ID ScsiTargetId
;
181 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
182 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
185 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
186 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
187 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
189 Status
= gBS
->OpenProtocol (
191 &gEfiExtScsiPassThruProtocolGuid
,
192 (VOID
**)&ExtPassThru
,
193 This
->DriverBindingHandle
,
195 EFI_OPEN_PROTOCOL_BY_DRIVER
198 if (Status
== EFI_ALREADY_STARTED
) {
200 } else if (!EFI_ERROR(Status
)) {
202 // Check if RemainingDevicePath is NULL or the End of Device Path Node,
203 // if yes, return EFI_SUCCESS.
205 if ((RemainingDevicePath
== NULL
) || IsDevicePathEnd (RemainingDevicePath
)) {
207 // Close protocol regardless of RemainingDevicePath validation
211 &gEfiExtScsiPassThruProtocolGuid
,
212 This
->DriverBindingHandle
,
218 // If RemainingDevicePath isn't the End of Device Path Node, check its validation
220 Status
= ExtPassThru
->GetTargetLun (ExtPassThru
, RemainingDevicePath
, &TargetId
, &Lun
);
222 // Close protocol regardless of RemainingDevicePath validation
226 &gEfiExtScsiPassThruProtocolGuid
,
227 This
->DriverBindingHandle
,
230 if (!EFI_ERROR(Status
)) {
237 // Come here in 2 condition:
238 // 1. ExtPassThru doesn't exist.
239 // 2. ExtPassThru exists but RemainingDevicePath is invalid.
241 Status
= gBS
->OpenProtocol (
243 &gEfiScsiPassThruProtocolGuid
,
245 This
->DriverBindingHandle
,
247 EFI_OPEN_PROTOCOL_BY_DRIVER
250 if (Status
== EFI_ALREADY_STARTED
) {
254 if (EFI_ERROR (Status
)) {
259 // Test RemainingDevicePath is valid or not.
261 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
262 Status
= PassThru
->GetTargetLun (PassThru
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
267 &gEfiScsiPassThruProtocolGuid
,
268 This
->DriverBindingHandle
,
276 Start this driver on ControllerHandle.
278 This service is called by the EFI boot service ConnectController(). In order
279 to make drivers as small as possible, there are a few calling restrictions for
280 this service. ConnectController() must follow these calling restrictions. If
281 any other agent wishes to call Start() it must also follow these calling
284 @param This Protocol instance pointer.
285 @param ControllerHandle Handle of device to bind driver to
286 @param RemainingDevicePath Optional parameter use to pick a specific child
289 @retval EFI_SUCCESS This driver is added to ControllerHandle
290 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
291 @retval other This driver does not support this device
296 SCSIBusDriverBindingStart (
297 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
298 IN EFI_HANDLE Controller
,
299 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
304 BOOLEAN ScanOtherPuns
;
305 BOOLEAN FromFirstTarget
;
306 BOOLEAN ExtScsiSupport
;
308 EFI_STATUS DevicePathStatus
;
309 EFI_STATUS PassThruStatus
;
310 SCSI_BUS_DEVICE
*ScsiBusDev
;
311 SCSI_TARGET_ID ScsiTargetId
;
312 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
313 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiInterface
;
314 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiInterface
;
315 EFI_SCSI_BUS_PROTOCOL
*BusIdentify
;
318 ScanOtherPuns
= TRUE
;
319 FromFirstTarget
= FALSE
;
320 ExtScsiSupport
= FALSE
;
321 PassThruStatus
= EFI_SUCCESS
;
323 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
324 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
326 DevicePathStatus
= gBS
->OpenProtocol (
328 &gEfiDevicePathProtocolGuid
,
329 (VOID
**) &ParentDevicePath
,
330 This
->DriverBindingHandle
,
332 EFI_OPEN_PROTOCOL_BY_DRIVER
334 if (EFI_ERROR (DevicePathStatus
) && (DevicePathStatus
!= EFI_ALREADY_STARTED
)) {
335 return DevicePathStatus
;
339 // Report Status Code to indicate SCSI bus starts
341 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
343 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_INIT
),
348 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
349 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
350 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
352 Status
= gBS
->OpenProtocol (
354 &gEfiExtScsiPassThruProtocolGuid
,
355 (VOID
**) &ExtScsiInterface
,
356 This
->DriverBindingHandle
,
358 EFI_OPEN_PROTOCOL_BY_DRIVER
361 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
363 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
364 Status
= gBS
->OpenProtocol (
366 &gEfiScsiPassThruProtocolGuid
,
367 (VOID
**) &ScsiInterface
,
368 This
->DriverBindingHandle
,
370 EFI_OPEN_PROTOCOL_BY_DRIVER
373 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
375 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
376 if (!EFI_ERROR(DevicePathStatus
)) {
379 &gEfiDevicePathProtocolGuid
,
380 This
->DriverBindingHandle
,
388 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
389 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
390 // another SCSI Bus Driver to work on the same host handle.
392 ExtScsiSupport
= TRUE
;
393 PassThruStatus
= gBS
->OpenProtocol (
395 &gEfiScsiPassThruProtocolGuid
,
396 (VOID
**) &ScsiInterface
,
397 This
->DriverBindingHandle
,
399 EFI_OPEN_PROTOCOL_BY_DRIVER
403 if (Status
!= EFI_ALREADY_STARTED
) {
405 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
406 // on this handle for this time. Then construct Host controller private data.
409 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
410 if (ScsiBusDev
== NULL
) {
411 Status
= EFI_OUT_OF_RESOURCES
;
414 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
415 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
416 ScsiBusDev
->DevicePath
= ParentDevicePath
;
417 if (ScsiBusDev
->ExtScsiSupport
) {
418 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
420 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
424 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
425 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
426 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
428 Status
= gBS
->InstallProtocolInterface (
431 EFI_NATIVE_INTERFACE
,
432 &ScsiBusDev
->BusIdentify
434 if (EFI_ERROR (Status
)) {
439 // Go through here means Start() is re-invoked again, nothing special is required to do except
440 // picking up Host controller private information.
442 Status
= gBS
->OpenProtocol (
445 (VOID
**) &BusIdentify
,
446 This
->DriverBindingHandle
,
448 EFI_OPEN_PROTOCOL_GET_PROTOCOL
451 if (EFI_ERROR (Status
)) {
454 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
458 // Report Status Code to indicate detecting devices on bus
460 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
462 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_DETECT
),
467 if (RemainingDevicePath
== NULL
) {
469 // If RemainingDevicePath is NULL,
470 // must enumerate all SCSI devices anyway
472 FromFirstTarget
= TRUE
;
473 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
475 // If RemainingDevicePath isn't the End of Device Path Node,
476 // only scan the specified device by RemainingDevicePath
478 if (ScsiBusDev
->ExtScsiSupport
) {
479 Status
= ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
481 Status
= ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
484 if (EFI_ERROR (Status
)) {
489 // If RemainingDevicePath is the End of Device Path Node,
490 // skip enumerate any device and return EFI_SUCESSS
492 ScanOtherPuns
= FALSE
;
495 while(ScanOtherPuns
) {
496 if (FromFirstTarget
) {
498 // Remaining Device Path is NULL, scan all the possible Puns in the
501 if (ScsiBusDev
->ExtScsiSupport
) {
502 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
504 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
506 if (EFI_ERROR (Status
)) {
508 // no legal Pun and Lun found any more
513 ScanOtherPuns
= FALSE
;
516 // Avoid creating handle for the host adapter.
518 if (ScsiBusDev
->ExtScsiSupport
) {
519 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
523 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
528 // Scan for the scsi device, if it attaches to the scsi bus,
529 // then create handle and install scsi i/o protocol.
531 Status
= ScsiScanCreateDevice (This
, Controller
, &ScsiTargetId
, Lun
, ScsiBusDev
);
537 if (ScsiBusDev
!= NULL
) {
538 FreePool (ScsiBusDev
);
541 if (ExtScsiSupport
) {
544 &gEfiExtScsiPassThruProtocolGuid
,
545 This
->DriverBindingHandle
,
548 if (!EFI_ERROR (PassThruStatus
)) {
551 &gEfiScsiPassThruProtocolGuid
,
552 This
->DriverBindingHandle
,
559 &gEfiScsiPassThruProtocolGuid
,
560 This
->DriverBindingHandle
,
568 Stop this driver on ControllerHandle.
570 This service is called by the EFI boot service DisconnectController().
571 In order to make drivers as small as possible, there are a few calling
572 restrictions for this service. DisconnectController() must follow these
573 calling restrictions. If any other agent wishes to call Stop() it must also
574 follow these calling restrictions.
576 @param This Protocol instance pointer.
577 @param ControllerHandle Handle of device to stop driver on
578 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
579 children is zero stop the entire bus driver.
580 @param ChildHandleBuffer List of Child Handles to Stop.
582 @retval EFI_SUCCESS This driver is removed ControllerHandle
583 @retval other This driver was not removed from this device
588 SCSIBusDriverBindingStop (
589 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
590 IN EFI_HANDLE Controller
,
591 IN UINTN NumberOfChildren
,
592 IN EFI_HANDLE
*ChildHandleBuffer
596 BOOLEAN AllChildrenStopped
;
598 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
599 SCSI_IO_DEV
*ScsiIoDevice
;
601 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
602 SCSI_BUS_DEVICE
*ScsiBusDev
;
604 if (NumberOfChildren
== 0) {
606 // Get the SCSI_BUS_DEVICE
608 Status
= gBS
->OpenProtocol (
611 (VOID
**) &Scsidentifier
,
612 This
->DriverBindingHandle
,
614 EFI_OPEN_PROTOCOL_GET_PROTOCOL
617 if (EFI_ERROR (Status
)) {
618 return EFI_DEVICE_ERROR
;
621 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
624 // Uninstall SCSI Bus Protocol
626 gBS
->UninstallProtocolInterface (
629 &ScsiBusDev
->BusIdentify
633 // Close the bus driver
635 if (ScsiBusDev
->ExtScsiSupport
) {
637 // Close ExtPassThru Protocol from this controller handle
641 &gEfiExtScsiPassThruProtocolGuid
,
642 This
->DriverBindingHandle
,
646 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
647 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
648 // So Stop() needs to try to close PassThru if present here.
652 &gEfiScsiPassThruProtocolGuid
,
653 This
->DriverBindingHandle
,
659 &gEfiScsiPassThruProtocolGuid
,
660 This
->DriverBindingHandle
,
667 &gEfiDevicePathProtocolGuid
,
668 This
->DriverBindingHandle
,
671 FreePool (ScsiBusDev
);
675 AllChildrenStopped
= TRUE
;
677 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
679 Status
= gBS
->OpenProtocol (
680 ChildHandleBuffer
[Index
],
681 &gEfiScsiIoProtocolGuid
,
683 This
->DriverBindingHandle
,
685 EFI_OPEN_PROTOCOL_GET_PROTOCOL
687 if (EFI_ERROR (Status
)) {
688 AllChildrenStopped
= FALSE
;
692 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
694 // Close the child handle
696 if (ScsiIoDevice
->ExtScsiSupport
) {
697 Status
= gBS
->CloseProtocol (
699 &gEfiExtScsiPassThruProtocolGuid
,
700 This
->DriverBindingHandle
,
701 ChildHandleBuffer
[Index
]
705 Status
= gBS
->CloseProtocol (
707 &gEfiScsiPassThruProtocolGuid
,
708 This
->DriverBindingHandle
,
709 ChildHandleBuffer
[Index
]
713 Status
= gBS
->UninstallMultipleProtocolInterfaces (
714 ChildHandleBuffer
[Index
],
715 &gEfiDevicePathProtocolGuid
,
716 ScsiIoDevice
->DevicePath
,
717 &gEfiScsiIoProtocolGuid
,
718 &ScsiIoDevice
->ScsiIo
,
721 if (EFI_ERROR (Status
)) {
722 AllChildrenStopped
= FALSE
;
723 if (ScsiIoDevice
->ExtScsiSupport
) {
726 &gEfiExtScsiPassThruProtocolGuid
,
728 This
->DriverBindingHandle
,
729 ChildHandleBuffer
[Index
],
730 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
735 &gEfiScsiPassThruProtocolGuid
,
737 This
->DriverBindingHandle
,
738 ChildHandleBuffer
[Index
],
739 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
743 FreePool (ScsiIoDevice
);
747 if (!AllChildrenStopped
) {
748 return EFI_DEVICE_ERROR
;
756 Retrieves the device type information of the SCSI Controller.
758 @param This Protocol instance pointer.
759 @param DeviceType A pointer to the device type information retrieved from
762 @retval EFI_SUCCESS Retrieves the device type information successfully.
763 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
769 IN EFI_SCSI_IO_PROTOCOL
*This
,
770 OUT UINT8
*DeviceType
773 SCSI_IO_DEV
*ScsiIoDevice
;
775 if (DeviceType
== NULL
) {
776 return EFI_INVALID_PARAMETER
;
779 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
780 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
786 Retrieves the device location in the SCSI channel.
788 @param This Protocol instance pointer.
789 @param Target A pointer to the Target ID of a SCSI device
791 @param Lun A pointer to the LUN of the SCSI device on
794 @retval EFI_SUCCESS Retrieves the device location successfully.
795 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
800 ScsiGetDeviceLocation (
801 IN EFI_SCSI_IO_PROTOCOL
*This
,
802 IN OUT UINT8
**Target
,
806 SCSI_IO_DEV
*ScsiIoDevice
;
808 if (Target
== NULL
|| Lun
== NULL
) {
809 return EFI_INVALID_PARAMETER
;
812 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
814 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
816 *Lun
= ScsiIoDevice
->Lun
;
822 Resets the SCSI Bus that the SCSI Controller is attached to.
824 @param This Protocol instance pointer.
826 @retval EFI_SUCCESS The SCSI bus is reset successfully.
827 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
828 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
829 SCSI Host Controller.
830 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
836 IN EFI_SCSI_IO_PROTOCOL
*This
839 SCSI_IO_DEV
*ScsiIoDevice
;
841 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
844 // Report Status Code to indicate reset happens
846 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
848 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
849 ScsiIoDevice
->ScsiBusDeviceData
->DevicePath
852 if (ScsiIoDevice
->ExtScsiSupport
){
853 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
855 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
861 Resets the SCSI Controller that the device handle specifies.
863 @param This Protocol instance pointer.
865 @retval EFI_SUCCESS Reset the SCSI controller successfully.
866 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
867 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
868 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
874 IN EFI_SCSI_IO_PROTOCOL
*This
877 SCSI_IO_DEV
*ScsiIoDevice
;
878 UINT8 Target
[TARGET_MAX_BYTES
];
880 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
883 // Report Status Code to indicate reset happens
885 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
887 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
888 ScsiIoDevice
->ScsiBusDeviceData
->DevicePath
891 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
894 if (ScsiIoDevice
->ExtScsiSupport
) {
895 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
896 ScsiIoDevice
->ExtScsiPassThru
,
901 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
902 ScsiIoDevice
->ScsiPassThru
,
903 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
911 Sends a SCSI Request Packet to the SCSI Controller for execution.
913 @param This Protocol instance pointer.
914 @param CommandPacket The SCSI request packet to send to the SCSI
915 Controller specified by the device handle.
916 @param Event If the SCSI bus where the SCSI device is attached
917 does not support non-blocking I/O, then Event is
918 ignored, and blocking I/O is performed.
919 If Event is NULL, then blocking I/O is performed.
920 If Event is not NULL and non-blocking I/O is
921 supported, then non-blocking I/O is performed,
922 and Event will be signaled when the SCSI Request
925 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
926 successfully, and TransferLength bytes were
927 transferred to/from DataBuffer.See
928 HostAdapterStatus, TargetStatus,
929 SenseDataLength, and SenseData in that order
930 for additional status information.
931 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
932 but the entire DataBuffer could not be transferred.
933 The actual number of bytes transferred is returned
934 in TransferLength. See HostAdapterStatus,
935 TargetStatus, SenseDataLength, and SenseData in
936 that order for additional status information.
937 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
938 there are too many SCSI Command Packets already
939 queued.The caller may retry again later.
940 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
941 the SCSI Request Packet. See HostAdapterStatus,
942 TargetStatus, SenseDataLength, and SenseData in
943 that order for additional status information.
944 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
945 The SCSI Request Packet was not sent, so no
946 additional status information is available.
947 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
948 is not supported by the SCSI initiator(i.e., SCSI
949 Host Controller). The SCSI Request Packet was not
950 sent, so no additional status information is
952 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
953 Request Packet to execute. See HostAdapterStatus,
954 TargetStatus, SenseDataLength, and SenseData in
955 that order for additional status information.
959 ScsiExecuteSCSICommand (
960 IN EFI_SCSI_IO_PROTOCOL
*This
,
961 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
962 IN EFI_EVENT Event OPTIONAL
965 SCSI_IO_DEV
*ScsiIoDevice
;
967 UINT8 Target
[TARGET_MAX_BYTES
];
968 EFI_EVENT PacketEvent
;
969 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
970 SCSI_EVENT_DATA EventData
;
974 if (Packet
== NULL
) {
975 return EFI_INVALID_PARAMETER
;
978 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
979 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
981 if (ScsiIoDevice
->ExtScsiSupport
) {
982 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
984 if (((ScsiIoDevice
->ExtScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
985 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
986 ScsiIoDevice
->ExtScsiPassThru
,
994 // If there's no event or the SCSI Device doesn't support NON-BLOCKING,
995 // let the 'Event' parameter for PassThru() be NULL.
997 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
998 ScsiIoDevice
->ExtScsiPassThru
,
1004 if ((!EFI_ERROR(Status
)) && (Event
!= NULL
)) {
1006 // Signal Event to tell caller to pick up the SCSI IO packet if the
1007 // PassThru() succeeds.
1009 gBS
->SignalEvent (Event
);
1014 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1016 if (mWorkingBuffer
== NULL
) {
1017 return EFI_DEVICE_ERROR
;
1021 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
1023 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
1024 if (EFI_ERROR(Status
)) {
1025 FreePool(mWorkingBuffer
);
1029 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
1030 EventData
.Data1
= (VOID
*)Packet
;
1031 EventData
.Data2
= Event
;
1035 Status
= gBS
->CreateEvent (
1042 if (EFI_ERROR(Status
)) {
1043 FreePool(mWorkingBuffer
);
1047 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
1048 ScsiIoDevice
->ScsiPassThru
,
1049 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1055 if (EFI_ERROR(Status
)) {
1056 FreePool(mWorkingBuffer
);
1057 gBS
->CloseEvent(PacketEvent
);
1063 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
1064 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
1066 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
1067 ScsiIoDevice
->ScsiPassThru
,
1068 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1073 if (EFI_ERROR(Status
)) {
1074 FreePool(mWorkingBuffer
);
1078 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
1080 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1081 // free mWorkingBuffer.
1083 FreePool(mWorkingBuffer
);
1086 // Signal Event to tell caller to pick up the SCSI IO Packet.
1088 if (Event
!= NULL
) {
1089 gBS
->SignalEvent (Event
);
1098 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
1100 @param This Protocol instance pointer
1101 @param Controller Controller handle
1102 @param TargetId Tartget to be scanned
1103 @param Lun The Lun of the SCSI device on the SCSI channel.
1104 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
1106 @retval EFI_SUCCESS Successfully to discover the device and attach
1107 ScsiIoProtocol to it.
1108 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
1113 ScsiScanCreateDevice (
1114 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1115 IN EFI_HANDLE Controller
,
1116 IN SCSI_TARGET_ID
*TargetId
,
1118 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
1122 SCSI_IO_DEV
*ScsiIoDevice
;
1123 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
1124 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1125 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1126 EFI_HANDLE DeviceHandle
;
1129 RemainingDevicePath
= NULL
;
1130 ScsiDevicePath
= NULL
;
1131 ScsiIoDevice
= NULL
;
1134 // Build Device Path
1136 if (ScsiBusDev
->ExtScsiSupport
){
1137 Status
= ScsiBusDev
->ExtScsiInterface
->BuildDevicePath (
1138 ScsiBusDev
->ExtScsiInterface
,
1139 &TargetId
->ScsiId
.ExtScsi
[0],
1144 Status
= ScsiBusDev
->ScsiInterface
->BuildDevicePath (
1145 ScsiBusDev
->ScsiInterface
,
1146 TargetId
->ScsiId
.Scsi
,
1152 if (EFI_ERROR(Status
)) {
1156 DevicePath
= AppendDevicePathNode (
1157 ScsiBusDev
->DevicePath
,
1161 if (DevicePath
== NULL
) {
1162 Status
= EFI_OUT_OF_RESOURCES
;
1166 DeviceHandle
= NULL
;
1167 RemainingDevicePath
= DevicePath
;
1168 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
1169 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(RemainingDevicePath
)) {
1171 // The device has been started, directly return to fast boot.
1173 Status
= EFI_ALREADY_STARTED
;
1177 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
1178 if (ScsiIoDevice
== NULL
) {
1179 Status
= EFI_OUT_OF_RESOURCES
;
1183 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
1184 ScsiIoDevice
->ScsiBusDeviceData
= ScsiBusDev
;
1185 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
1186 ScsiIoDevice
->Lun
= Lun
;
1188 if (ScsiBusDev
->ExtScsiSupport
) {
1189 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
1190 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
1191 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
1194 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
1195 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
1196 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
1199 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
1200 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
1201 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
1202 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
1203 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1206 // Report Status Code here since the new SCSI device will be discovered
1208 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1210 (EFI_IO_BUS_SCSI
| EFI_IOB_PC_ENABLE
),
1211 ScsiBusDev
->DevicePath
1214 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1215 Status
= EFI_OUT_OF_RESOURCES
;
1219 ScsiIoDevice
->DevicePath
= DevicePath
;
1221 Status
= gBS
->InstallMultipleProtocolInterfaces (
1222 &ScsiIoDevice
->Handle
,
1223 &gEfiDevicePathProtocolGuid
,
1224 ScsiIoDevice
->DevicePath
,
1225 &gEfiScsiIoProtocolGuid
,
1226 &ScsiIoDevice
->ScsiIo
,
1229 if (EFI_ERROR (Status
)) {
1232 if (ScsiBusDev
->ExtScsiSupport
) {
1235 &gEfiExtScsiPassThruProtocolGuid
,
1236 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1237 This
->DriverBindingHandle
,
1238 ScsiIoDevice
->Handle
,
1239 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1244 &gEfiScsiPassThruProtocolGuid
,
1245 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1246 This
->DriverBindingHandle
,
1247 ScsiIoDevice
->Handle
,
1248 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1257 // The memory space for ScsiDevicePath is allocated in
1258 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1259 // after AppendDevicePathNode,so free the memory it occupies.
1261 FreePool (ScsiDevicePath
);
1263 if (DevicePath
!= NULL
) {
1264 FreePool (DevicePath
);
1267 if (ScsiIoDevice
!= NULL
) {
1268 FreePool (ScsiIoDevice
);
1276 Discovery SCSI Device
1278 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1280 @retval TRUE Find SCSI Device and verify it.
1281 @retval FALSE Unable to find SCSI Device.
1285 DiscoverScsiDevice (
1286 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1290 UINT32 InquiryDataLength
;
1291 UINT8 SenseDataLength
;
1292 UINT8 HostAdapterStatus
;
1294 EFI_SCSI_INQUIRY_DATA
*InquiryData
;
1295 EFI_SCSI_SENSE_DATA
*SenseData
;
1298 BOOLEAN ScsiDeviceFound
;
1300 HostAdapterStatus
= 0;
1304 InquiryData
= AllocateAlignedBuffer (ScsiIoDevice
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1305 if (InquiryData
== NULL
) {
1306 ScsiDeviceFound
= FALSE
;
1310 SenseData
= AllocateAlignedBuffer (
1312 sizeof (EFI_SCSI_SENSE_DATA
)
1314 if (SenseData
== NULL
) {
1315 ScsiDeviceFound
= FALSE
;
1320 // Using Inquiry command to scan for the device
1322 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1323 SenseDataLength
= sizeof (EFI_SCSI_SENSE_DATA
);
1324 ZeroMem (InquiryData
, InquiryDataLength
);
1325 ZeroMem (SenseData
, SenseDataLength
);
1328 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1329 Status
= ScsiInquiryCommand (
1330 &ScsiIoDevice
->ScsiIo
,
1336 (VOID
*) InquiryData
,
1340 if (!EFI_ERROR (Status
)) {
1341 if ((HostAdapterStatus
== EFI_SCSI_IO_STATUS_HOST_ADAPTER_OK
) &&
1342 (TargetStatus
== EFI_SCSI_IO_STATUS_TARGET_CHECK_CONDITION
) &&
1343 (SenseData
->Error_Code
== 0x70) &&
1344 (SenseData
->Sense_Key
== EFI_SCSI_SK_ILLEGAL_REQUEST
)) {
1345 ScsiDeviceFound
= FALSE
;
1350 if ((Status
== EFI_BAD_BUFFER_SIZE
) ||
1351 (Status
== EFI_INVALID_PARAMETER
) ||
1352 (Status
== EFI_UNSUPPORTED
)) {
1353 ScsiDeviceFound
= FALSE
;
1358 if (Index
== MaxRetry
) {
1359 ScsiDeviceFound
= FALSE
;
1364 // Retrieved inquiry data successfully
1366 if (InquiryData
->Peripheral_Qualifier
!= 0) {
1367 ScsiDeviceFound
= FALSE
;
1371 if (0x1e >= InquiryData
->Peripheral_Type
&& InquiryData
->Peripheral_Type
>= 0xa) {
1372 ScsiDeviceFound
= FALSE
;
1377 // valid device type and peripheral qualifier combination.
1379 ScsiIoDevice
->ScsiDeviceType
= InquiryData
->Peripheral_Type
;
1380 ScsiIoDevice
->RemovableDevice
= InquiryData
->Rmb
;
1381 if (InquiryData
->Version
== 0) {
1382 ScsiIoDevice
->ScsiVersion
= 0;
1385 // ANSI-approved version
1387 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
->Version
& 0x07);
1390 ScsiDeviceFound
= TRUE
;
1393 FreeAlignedBuffer (SenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
1394 FreeAlignedBuffer (InquiryData
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1396 return ScsiDeviceFound
;
1401 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1403 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1404 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1409 ScsiioToPassThruPacket (
1410 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1411 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1415 //EFI 1.10 doesn't support Bi-Direction Command.
1417 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1418 return EFI_UNSUPPORTED
;
1421 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1423 CommandPacket
->Timeout
= Packet
->Timeout
;
1424 CommandPacket
->Cdb
= Packet
->Cdb
;
1425 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1426 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1427 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1428 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1429 CommandPacket
->SenseData
= Packet
->SenseData
;
1430 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1432 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1433 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1434 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1435 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1436 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1437 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1444 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1446 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1447 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1452 PassThruToScsiioPacket (
1453 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1454 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1457 Packet
->Timeout
= ScsiPacket
->Timeout
;
1458 Packet
->Cdb
= ScsiPacket
->Cdb
;
1459 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1460 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1461 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1462 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1463 Packet
->SenseData
= ScsiPacket
->SenseData
;
1464 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1466 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1467 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1468 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1469 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1470 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1471 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1478 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1481 @param Event The instance of EFI_EVENT.
1482 @param Context The parameter passed in.
1492 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1493 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1494 EFI_EVENT CallerEvent
;
1495 SCSI_EVENT_DATA
*PassData
;
1497 PassData
= (SCSI_EVENT_DATA
*)Context
;
1498 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1499 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1502 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1504 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1507 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1508 // free mWorkingBuffer.
1510 gBS
->FreePool(mWorkingBuffer
);
1513 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1515 CallerEvent
= PassData
->Data2
;
1516 gBS
->CloseEvent(Event
);
1517 gBS
->SignalEvent(CallerEvent
);