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 - 2011, 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 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
346 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
347 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
349 Status
= gBS
->OpenProtocol (
351 &gEfiExtScsiPassThruProtocolGuid
,
352 (VOID
**) &ExtScsiInterface
,
353 This
->DriverBindingHandle
,
355 EFI_OPEN_PROTOCOL_BY_DRIVER
358 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
360 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
361 Status
= gBS
->OpenProtocol (
363 &gEfiScsiPassThruProtocolGuid
,
364 (VOID
**) &ScsiInterface
,
365 This
->DriverBindingHandle
,
367 EFI_OPEN_PROTOCOL_BY_DRIVER
370 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
372 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
373 if (!EFI_ERROR(DevicePathStatus
)) {
376 &gEfiDevicePathProtocolGuid
,
377 This
->DriverBindingHandle
,
385 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
386 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
387 // another SCSI Bus Driver to work on the same host handle.
389 ExtScsiSupport
= TRUE
;
390 PassThruStatus
= gBS
->OpenProtocol (
392 &gEfiScsiPassThruProtocolGuid
,
393 (VOID
**) &ScsiInterface
,
394 This
->DriverBindingHandle
,
396 EFI_OPEN_PROTOCOL_BY_DRIVER
400 if (Status
!= EFI_ALREADY_STARTED
) {
402 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
403 // on this handle for this time. Then construct Host controller private data.
406 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
407 if (ScsiBusDev
== NULL
) {
408 Status
= EFI_OUT_OF_RESOURCES
;
411 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
412 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
413 ScsiBusDev
->DevicePath
= ParentDevicePath
;
414 if (ScsiBusDev
->ExtScsiSupport
) {
415 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
417 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
421 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
422 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
423 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
425 Status
= gBS
->InstallProtocolInterface (
428 EFI_NATIVE_INTERFACE
,
429 &ScsiBusDev
->BusIdentify
431 if (EFI_ERROR (Status
)) {
436 // Go through here means Start() is re-invoked again, nothing special is required to do except
437 // picking up Host controller private information.
439 Status
= gBS
->OpenProtocol (
442 (VOID
**) &BusIdentify
,
443 This
->DriverBindingHandle
,
445 EFI_OPEN_PROTOCOL_GET_PROTOCOL
448 if (EFI_ERROR (Status
)) {
451 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
455 if (RemainingDevicePath
== NULL
) {
457 // If RemainingDevicePath is NULL,
458 // must enumerate all SCSI devices anyway
460 FromFirstTarget
= TRUE
;
461 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
463 // If RemainingDevicePath isn't the End of Device Path Node,
464 // only scan the specified device by RemainingDevicePath
466 if (ScsiBusDev
->ExtScsiSupport
) {
467 Status
= ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
469 Status
= ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
472 if (EFI_ERROR (Status
)) {
477 // If RemainingDevicePath is the End of Device Path Node,
478 // skip enumerate any device and return EFI_SUCESSS
480 ScanOtherPuns
= FALSE
;
483 while(ScanOtherPuns
) {
484 if (FromFirstTarget
) {
486 // Remaining Device Path is NULL, scan all the possible Puns in the
489 if (ScsiBusDev
->ExtScsiSupport
) {
490 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
492 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
494 if (EFI_ERROR (Status
)) {
496 // no legal Pun and Lun found any more
501 ScanOtherPuns
= FALSE
;
504 // Avoid creating handle for the host adapter.
506 if (ScsiBusDev
->ExtScsiSupport
) {
507 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
511 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
516 // Scan for the scsi device, if it attaches to the scsi bus,
517 // then create handle and install scsi i/o protocol.
519 Status
= ScsiScanCreateDevice (This
, Controller
, &ScsiTargetId
, Lun
, ScsiBusDev
);
525 if (ScsiBusDev
!= NULL
) {
526 FreePool (ScsiBusDev
);
529 if (ExtScsiSupport
) {
532 &gEfiExtScsiPassThruProtocolGuid
,
533 This
->DriverBindingHandle
,
536 if (!EFI_ERROR (PassThruStatus
)) {
539 &gEfiScsiPassThruProtocolGuid
,
540 This
->DriverBindingHandle
,
547 &gEfiScsiPassThruProtocolGuid
,
548 This
->DriverBindingHandle
,
556 Stop this driver on ControllerHandle.
558 This service is called by the EFI boot service DisconnectController().
559 In order to make drivers as small as possible, there are a few calling
560 restrictions for this service. DisconnectController() must follow these
561 calling restrictions. If any other agent wishes to call Stop() it must also
562 follow these calling restrictions.
564 @param This Protocol instance pointer.
565 @param ControllerHandle Handle of device to stop driver on
566 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
567 children is zero stop the entire bus driver.
568 @param ChildHandleBuffer List of Child Handles to Stop.
570 @retval EFI_SUCCESS This driver is removed ControllerHandle
571 @retval other This driver was not removed from this device
576 SCSIBusDriverBindingStop (
577 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
578 IN EFI_HANDLE Controller
,
579 IN UINTN NumberOfChildren
,
580 IN EFI_HANDLE
*ChildHandleBuffer
584 BOOLEAN AllChildrenStopped
;
586 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
587 SCSI_IO_DEV
*ScsiIoDevice
;
589 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
590 SCSI_BUS_DEVICE
*ScsiBusDev
;
592 if (NumberOfChildren
== 0) {
594 // Get the SCSI_BUS_DEVICE
596 Status
= gBS
->OpenProtocol (
599 (VOID
**) &Scsidentifier
,
600 This
->DriverBindingHandle
,
602 EFI_OPEN_PROTOCOL_GET_PROTOCOL
605 if (EFI_ERROR (Status
)) {
606 return EFI_DEVICE_ERROR
;
609 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
612 // Uninstall SCSI Bus Protocol
614 gBS
->UninstallProtocolInterface (
617 &ScsiBusDev
->BusIdentify
621 // Close the bus driver
623 if (ScsiBusDev
->ExtScsiSupport
) {
625 // Close ExtPassThru Protocol from this controller handle
629 &gEfiExtScsiPassThruProtocolGuid
,
630 This
->DriverBindingHandle
,
634 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
635 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
636 // So Stop() needs to try to close PassThru if present here.
640 &gEfiScsiPassThruProtocolGuid
,
641 This
->DriverBindingHandle
,
647 &gEfiScsiPassThruProtocolGuid
,
648 This
->DriverBindingHandle
,
655 &gEfiDevicePathProtocolGuid
,
656 This
->DriverBindingHandle
,
659 FreePool (ScsiBusDev
);
663 AllChildrenStopped
= TRUE
;
665 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
667 Status
= gBS
->OpenProtocol (
668 ChildHandleBuffer
[Index
],
669 &gEfiScsiIoProtocolGuid
,
671 This
->DriverBindingHandle
,
673 EFI_OPEN_PROTOCOL_GET_PROTOCOL
675 if (EFI_ERROR (Status
)) {
676 AllChildrenStopped
= FALSE
;
680 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
682 // Close the child handle
684 if (ScsiIoDevice
->ExtScsiSupport
) {
685 Status
= gBS
->CloseProtocol (
687 &gEfiExtScsiPassThruProtocolGuid
,
688 This
->DriverBindingHandle
,
689 ChildHandleBuffer
[Index
]
693 Status
= gBS
->CloseProtocol (
695 &gEfiScsiPassThruProtocolGuid
,
696 This
->DriverBindingHandle
,
697 ChildHandleBuffer
[Index
]
701 Status
= gBS
->UninstallMultipleProtocolInterfaces (
702 ChildHandleBuffer
[Index
],
703 &gEfiDevicePathProtocolGuid
,
704 ScsiIoDevice
->DevicePath
,
705 &gEfiScsiIoProtocolGuid
,
706 &ScsiIoDevice
->ScsiIo
,
709 if (EFI_ERROR (Status
)) {
710 AllChildrenStopped
= FALSE
;
711 if (ScsiIoDevice
->ExtScsiSupport
) {
714 &gEfiExtScsiPassThruProtocolGuid
,
716 This
->DriverBindingHandle
,
717 ChildHandleBuffer
[Index
],
718 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
723 &gEfiScsiPassThruProtocolGuid
,
725 This
->DriverBindingHandle
,
726 ChildHandleBuffer
[Index
],
727 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
731 FreePool (ScsiIoDevice
);
735 if (!AllChildrenStopped
) {
736 return EFI_DEVICE_ERROR
;
744 Retrieves the device type information of the SCSI Controller.
746 @param This Protocol instance pointer.
747 @param DeviceType A pointer to the device type information retrieved from
750 @retval EFI_SUCCESS Retrieves the device type information successfully.
751 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
757 IN EFI_SCSI_IO_PROTOCOL
*This
,
758 OUT UINT8
*DeviceType
761 SCSI_IO_DEV
*ScsiIoDevice
;
763 if (DeviceType
== NULL
) {
764 return EFI_INVALID_PARAMETER
;
767 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
768 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
774 Retrieves the device location in the SCSI channel.
776 @param This Protocol instance pointer.
777 @param Target A pointer to the Target ID of a SCSI device
779 @param Lun A pointer to the LUN of the SCSI device on
782 @retval EFI_SUCCESS Retrieves the device location successfully.
783 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
788 ScsiGetDeviceLocation (
789 IN EFI_SCSI_IO_PROTOCOL
*This
,
790 IN OUT UINT8
**Target
,
794 SCSI_IO_DEV
*ScsiIoDevice
;
796 if (Target
== NULL
|| Lun
== NULL
) {
797 return EFI_INVALID_PARAMETER
;
800 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
802 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
804 *Lun
= ScsiIoDevice
->Lun
;
810 Resets the SCSI Bus that the SCSI Controller is attached to.
812 @param This Protocol instance pointer.
814 @retval EFI_SUCCESS The SCSI bus is reset successfully.
815 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
816 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
817 SCSI Host Controller.
818 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
824 IN EFI_SCSI_IO_PROTOCOL
*This
827 SCSI_IO_DEV
*ScsiIoDevice
;
829 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
831 if (ScsiIoDevice
->ExtScsiSupport
){
832 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
834 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
840 Resets the SCSI Controller that the device handle specifies.
842 @param This Protocol instance pointer.
844 @retval EFI_SUCCESS Reset the SCSI controller successfully.
845 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
846 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
847 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
853 IN EFI_SCSI_IO_PROTOCOL
*This
856 SCSI_IO_DEV
*ScsiIoDevice
;
857 UINT8 Target
[TARGET_MAX_BYTES
];
859 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
860 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
863 if (ScsiIoDevice
->ExtScsiSupport
) {
864 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
865 ScsiIoDevice
->ExtScsiPassThru
,
870 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
871 ScsiIoDevice
->ScsiPassThru
,
872 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
880 Sends a SCSI Request Packet to the SCSI Controller for execution.
882 @param This Protocol instance pointer.
883 @param CommandPacket The SCSI request packet to send to the SCSI
884 Controller specified by the device handle.
885 @param Event If the SCSI bus where the SCSI device is attached
886 does not support non-blocking I/O, then Event is
887 ignored, and blocking I/O is performed.
888 If Event is NULL, then blocking I/O is performed.
889 If Event is not NULL and non-blocking I/O is
890 supported, then non-blocking I/O is performed,
891 and Event will be signaled when the SCSI Request
894 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
895 successfully, and TransferLength bytes were
896 transferred to/from DataBuffer.See
897 HostAdapterStatus, TargetStatus,
898 SenseDataLength, and SenseData in that order
899 for additional status information.
900 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
901 but the entire DataBuffer could not be transferred.
902 The actual number of bytes transferred is returned
903 in TransferLength. See HostAdapterStatus,
904 TargetStatus, SenseDataLength, and SenseData in
905 that order for additional status information.
906 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
907 there are too many SCSI Command Packets already
908 queued.The caller may retry again later.
909 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
910 the SCSI Request Packet. See HostAdapterStatus,
911 TargetStatus, SenseDataLength, and SenseData in
912 that order for additional status information.
913 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
914 The SCSI Request Packet was not sent, so no
915 additional status information is available.
916 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
917 is not supported by the SCSI initiator(i.e., SCSI
918 Host Controller). The SCSI Request Packet was not
919 sent, so no additional status information is
921 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
922 Request Packet to execute. See HostAdapterStatus,
923 TargetStatus, SenseDataLength, and SenseData in
924 that order for additional status information.
928 ScsiExecuteSCSICommand (
929 IN EFI_SCSI_IO_PROTOCOL
*This
,
930 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
931 IN EFI_EVENT Event OPTIONAL
934 SCSI_IO_DEV
*ScsiIoDevice
;
936 UINT8 Target
[TARGET_MAX_BYTES
];
937 EFI_EVENT PacketEvent
;
938 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
939 SCSI_EVENT_DATA EventData
;
943 if (Packet
== NULL
) {
944 return EFI_INVALID_PARAMETER
;
947 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
948 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
950 if (ScsiIoDevice
->ExtScsiSupport
) {
951 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
952 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
953 ScsiIoDevice
->ExtScsiPassThru
,
961 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
963 if (mWorkingBuffer
== NULL
) {
964 return EFI_DEVICE_ERROR
;
968 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
970 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
971 if (EFI_ERROR(Status
)) {
972 FreePool(mWorkingBuffer
);
976 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
977 EventData
.Data1
= (VOID
*)Packet
;
978 EventData
.Data2
= Event
;
982 Status
= gBS
->CreateEvent (
989 if (EFI_ERROR(Status
)) {
990 FreePool(mWorkingBuffer
);
994 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
995 ScsiIoDevice
->ScsiPassThru
,
996 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1002 if (EFI_ERROR(Status
)) {
1003 FreePool(mWorkingBuffer
);
1004 gBS
->CloseEvent(PacketEvent
);
1010 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
1011 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
1013 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
1014 ScsiIoDevice
->ScsiPassThru
,
1015 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1020 if (EFI_ERROR(Status
)) {
1021 FreePool(mWorkingBuffer
);
1025 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
1027 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1028 // free mWorkingBuffer.
1030 FreePool(mWorkingBuffer
);
1038 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
1040 @param This Protocol instance pointer
1041 @param Controller Controller handle
1042 @param TargetId Tartget to be scanned
1043 @param Lun The Lun of the SCSI device on the SCSI channel.
1044 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
1046 @retval EFI_SUCCESS Successfully to discover the device and attach
1047 ScsiIoProtocol to it.
1048 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
1053 ScsiScanCreateDevice (
1054 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1055 IN EFI_HANDLE Controller
,
1056 IN SCSI_TARGET_ID
*TargetId
,
1058 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
1062 SCSI_IO_DEV
*ScsiIoDevice
;
1063 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
1064 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1065 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1066 EFI_HANDLE DeviceHandle
;
1069 RemainingDevicePath
= NULL
;
1070 ScsiDevicePath
= NULL
;
1071 ScsiIoDevice
= NULL
;
1074 // Build Device Path
1076 if (ScsiBusDev
->ExtScsiSupport
){
1077 Status
= ScsiBusDev
->ExtScsiInterface
->BuildDevicePath (
1078 ScsiBusDev
->ExtScsiInterface
,
1079 &TargetId
->ScsiId
.ExtScsi
[0],
1084 Status
= ScsiBusDev
->ScsiInterface
->BuildDevicePath (
1085 ScsiBusDev
->ScsiInterface
,
1086 TargetId
->ScsiId
.Scsi
,
1092 if (EFI_ERROR(Status
)) {
1096 DevicePath
= AppendDevicePathNode (
1097 ScsiBusDev
->DevicePath
,
1101 if (DevicePath
== NULL
) {
1102 Status
= EFI_OUT_OF_RESOURCES
;
1106 DeviceHandle
= NULL
;
1107 RemainingDevicePath
= DevicePath
;
1108 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
1109 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(RemainingDevicePath
)) {
1111 // The device has been started, directly return to fast boot.
1113 Status
= EFI_ALREADY_STARTED
;
1117 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
1118 if (ScsiIoDevice
== NULL
) {
1119 Status
= EFI_OUT_OF_RESOURCES
;
1123 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
1124 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
1125 ScsiIoDevice
->Lun
= Lun
;
1127 if (ScsiBusDev
->ExtScsiSupport
) {
1128 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
1129 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
1130 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
1133 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
1134 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
1135 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
1138 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
1139 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
1140 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
1141 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
1142 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1144 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1145 Status
= EFI_OUT_OF_RESOURCES
;
1149 ScsiIoDevice
->DevicePath
= DevicePath
;
1151 Status
= gBS
->InstallMultipleProtocolInterfaces (
1152 &ScsiIoDevice
->Handle
,
1153 &gEfiDevicePathProtocolGuid
,
1154 ScsiIoDevice
->DevicePath
,
1155 &gEfiScsiIoProtocolGuid
,
1156 &ScsiIoDevice
->ScsiIo
,
1159 if (EFI_ERROR (Status
)) {
1162 if (ScsiBusDev
->ExtScsiSupport
) {
1165 &gEfiExtScsiPassThruProtocolGuid
,
1166 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1167 This
->DriverBindingHandle
,
1168 ScsiIoDevice
->Handle
,
1169 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1174 &gEfiScsiPassThruProtocolGuid
,
1175 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1176 This
->DriverBindingHandle
,
1177 ScsiIoDevice
->Handle
,
1178 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1187 // The memory space for ScsiDevicePath is allocated in
1188 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1189 // after AppendDevicePathNode,so free the memory it occupies.
1191 FreePool (ScsiDevicePath
);
1193 if (DevicePath
!= NULL
) {
1194 FreePool (DevicePath
);
1197 if (ScsiIoDevice
!= NULL
) {
1198 FreePool (ScsiIoDevice
);
1206 Discovery SCSI Device
1208 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1210 @retval TRUE Find SCSI Device and verify it.
1211 @retval FALSE Unable to find SCSI Device.
1215 DiscoverScsiDevice (
1216 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1220 UINT32 InquiryDataLength
;
1221 UINT8 SenseDataLength
;
1222 UINT8 HostAdapterStatus
;
1224 EFI_SCSI_INQUIRY_DATA
*InquiryData
;
1227 BOOLEAN ScsiDeviceFound
;
1229 HostAdapterStatus
= 0;
1232 InquiryData
= AllocateAlignedBuffer (ScsiIoDevice
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1233 if (InquiryData
== NULL
) {
1234 ScsiDeviceFound
= FALSE
;
1239 // Using Inquiry command to scan for the device
1241 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1242 SenseDataLength
= 0;
1243 ZeroMem (InquiryData
, InquiryDataLength
);
1246 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1247 Status
= ScsiInquiryCommand (
1248 &ScsiIoDevice
->ScsiIo
,
1249 EFI_TIMER_PERIOD_SECONDS (1),
1254 (VOID
*) InquiryData
,
1258 if (!EFI_ERROR (Status
)) {
1260 } else if ((Status
== EFI_BAD_BUFFER_SIZE
) ||
1261 (Status
== EFI_INVALID_PARAMETER
) ||
1262 (Status
== EFI_UNSUPPORTED
)) {
1263 ScsiDeviceFound
= FALSE
;
1268 if (Index
== MaxRetry
) {
1269 ScsiDeviceFound
= FALSE
;
1274 // Retrieved inquiry data successfully
1276 if ((InquiryData
->Peripheral_Qualifier
!= 0) &&
1277 (InquiryData
->Peripheral_Qualifier
!= 3)) {
1278 ScsiDeviceFound
= FALSE
;
1282 if (InquiryData
->Peripheral_Qualifier
== 3) {
1283 if (InquiryData
->Peripheral_Type
!= 0x1f) {
1284 ScsiDeviceFound
= FALSE
;
1289 if (0x1e >= InquiryData
->Peripheral_Type
&& InquiryData
->Peripheral_Type
>= 0xa) {
1290 ScsiDeviceFound
= FALSE
;
1295 // valid device type and peripheral qualifier combination.
1297 ScsiIoDevice
->ScsiDeviceType
= InquiryData
->Peripheral_Type
;
1298 ScsiIoDevice
->RemovableDevice
= InquiryData
->Rmb
;
1299 if (InquiryData
->Version
== 0) {
1300 ScsiIoDevice
->ScsiVersion
= 0;
1303 // ANSI-approved version
1305 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
->Version
& 0x07);
1308 ScsiDeviceFound
= TRUE
;
1311 FreeAlignedBuffer (InquiryData
, sizeof (EFI_SCSI_INQUIRY_DATA
));
1313 return ScsiDeviceFound
;
1318 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1320 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1321 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1326 ScsiioToPassThruPacket (
1327 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1328 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1332 //EFI 1.10 doesn't support Bi-Direction Command.
1334 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1335 return EFI_UNSUPPORTED
;
1338 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1340 CommandPacket
->Timeout
= Packet
->Timeout
;
1341 CommandPacket
->Cdb
= Packet
->Cdb
;
1342 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1343 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1344 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1345 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1346 CommandPacket
->SenseData
= Packet
->SenseData
;
1347 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1349 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1350 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1351 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1352 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1353 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1354 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1361 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1363 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1364 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1369 PassThruToScsiioPacket (
1370 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1371 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1374 Packet
->Timeout
= ScsiPacket
->Timeout
;
1375 Packet
->Cdb
= ScsiPacket
->Cdb
;
1376 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1377 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1378 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1379 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1380 Packet
->SenseData
= ScsiPacket
->SenseData
;
1381 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1383 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1384 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1385 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1386 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1387 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1388 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1395 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1398 @param Event The instance of EFI_EVENT.
1399 @param Context The parameter passed in.
1409 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1410 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1411 EFI_EVENT CallerEvent
;
1412 SCSI_EVENT_DATA
*PassData
;
1414 PassData
= (SCSI_EVENT_DATA
*)Context
;
1415 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1416 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1419 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1421 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1424 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1425 // free mWorkingBuffer.
1427 gBS
->FreePool(mWorkingBuffer
);
1430 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1432 CallerEvent
= PassData
->Data2
;
1433 gBS
->CloseEvent(Event
);
1434 gBS
->SignalEvent(CallerEvent
);