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 - 2008, Intel Corporation. <BR>
6 All rights reserved. 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
,
31 // The ScsiBusProtocol is just used to locate ScsiBusDev
32 // structure in the SCSIBusDriverBindingStop(). Then we can
33 // Close all opened protocols and release this structure.
35 EFI_GUID mScsiBusProtocolGuid
= EFI_SCSI_BUS_PROTOCOL_GUID
;
40 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
42 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
43 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
48 ScsiioToPassThruPacket (
49 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
50 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
54 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
56 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
57 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
62 PassThruToScsiioPacket (
63 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
64 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
68 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
71 @param Event The instance of EFI_EVENT.
72 @param Context The parameter passed in.
83 The user Entry Point for module ScsiBus. The user code starts with this function.
85 @param ImageHandle The firmware allocated handle for the EFI image.
86 @param SystemTable A pointer to the EFI System Table.
88 @retval EFI_SUCCESS The entry point is executed successfully.
89 @retval other Some error occurs when executing this entry point.
95 IN EFI_HANDLE ImageHandle
,
96 IN EFI_SYSTEM_TABLE
*SystemTable
102 // Install driver model protocol(s).
104 Status
= EfiLibInstallDriverBindingComponentName2 (
107 &gSCSIBusDriverBinding
,
109 &gScsiBusComponentName
,
110 &gScsiBusComponentName2
112 ASSERT_EFI_ERROR (Status
);
119 Test to see if this driver supports ControllerHandle.
121 This service is called by the EFI boot service ConnectController(). In order
122 to make drivers as small as possible, there are a few calling restrictions for
123 this service. ConnectController() must follow these calling restrictions. If
124 any other agent wishes to call Supported() it must also follow these calling
127 @param This Protocol instance pointer.
128 @param ControllerHandle Handle of device to test
129 @param RemainingDevicePath Optional parameter use to pick a specific child
132 @retval EFI_SUCCESS This driver supports this device
133 @retval EFI_ALREADY_STARTED This driver is already running on this device
134 @retval other This driver does not support this device
139 SCSIBusDriverBindingSupported (
140 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
141 IN EFI_HANDLE Controller
,
142 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
146 EFI_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
147 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtPassThru
;
149 // Check for the existence of Extended SCSI Pass Thru Protocol and SCSI Pass Thru Protocol
151 Status
= gBS
->OpenProtocol (
153 &gEfiExtScsiPassThruProtocolGuid
,
154 (VOID
**)&ExtPassThru
,
155 This
->DriverBindingHandle
,
157 EFI_OPEN_PROTOCOL_BY_DRIVER
160 if (Status
== EFI_ALREADY_STARTED
) {
164 if (EFI_ERROR (Status
)) {
165 Status
= gBS
->OpenProtocol (
167 &gEfiScsiPassThruProtocolGuid
,
169 This
->DriverBindingHandle
,
171 EFI_OPEN_PROTOCOL_BY_DRIVER
174 if (Status
== EFI_ALREADY_STARTED
) {
178 if (EFI_ERROR (Status
)) {
184 &gEfiScsiPassThruProtocolGuid
,
185 This
->DriverBindingHandle
,
193 &gEfiExtScsiPassThruProtocolGuid
,
194 This
->DriverBindingHandle
,
203 Start this driver on ControllerHandle.
205 This service is called by the EFI boot service ConnectController(). In order
206 to make drivers as small as possible, there are a few calling restrictions for
207 this service. ConnectController() must follow these calling restrictions. If
208 any other agent wishes to call Start() it must also follow these calling
211 @param This Protocol instance pointer.
212 @param ControllerHandle Handle of device to bind driver to
213 @param RemainingDevicePath Optional parameter use to pick a specific child
216 @retval EFI_SUCCESS This driver is added to ControllerHandle
217 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
218 @retval other This driver does not support this device
223 SCSIBusDriverBindingStart (
224 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
225 IN EFI_HANDLE Controller
,
226 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
231 BOOLEAN ScanOtherPuns
;
232 BOOLEAN FromFirstTarget
;
233 BOOLEAN ExtScsiSupport
;
235 EFI_STATUS DevicePathStatus
;
236 EFI_STATUS PassThruStatus
;
237 SCSI_BUS_DEVICE
*ScsiBusDev
;
238 SCSI_TARGET_ID
*ScsiTargetId
;
239 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
240 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiInterface
;
241 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiInterface
;
242 EFI_SCSI_BUS_PROTOCOL
*BusIdentify
;
246 ScanOtherPuns
= TRUE
;
247 FromFirstTarget
= FALSE
;
248 ExtScsiSupport
= FALSE
;
249 PassThruStatus
= EFI_SUCCESS
;
251 ScsiTargetId
= AllocateZeroPool(sizeof(SCSI_TARGET_ID
));
252 if (ScsiTargetId
== NULL
) {
253 return EFI_OUT_OF_RESOURCES
;
256 TargetId
= &ScsiTargetId
->ScsiId
.ExtScsi
[0];
258 DevicePathStatus
= gBS
->OpenProtocol (
260 &gEfiDevicePathProtocolGuid
,
261 (VOID
**) &ParentDevicePath
,
262 This
->DriverBindingHandle
,
264 EFI_OPEN_PROTOCOL_BY_DRIVER
266 if (EFI_ERROR (DevicePathStatus
) && (DevicePathStatus
!= EFI_ALREADY_STARTED
)) {
267 return DevicePathStatus
;
271 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
272 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
273 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
275 Status
= gBS
->OpenProtocol (
277 &gEfiExtScsiPassThruProtocolGuid
,
278 (VOID
**) &ExtScsiInterface
,
279 This
->DriverBindingHandle
,
281 EFI_OPEN_PROTOCOL_BY_DRIVER
284 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
286 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
287 Status
= gBS
->OpenProtocol (
289 &gEfiScsiPassThruProtocolGuid
,
290 (VOID
**) &ScsiInterface
,
291 This
->DriverBindingHandle
,
293 EFI_OPEN_PROTOCOL_BY_DRIVER
296 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
298 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
299 if (!EFI_ERROR(DevicePathStatus
)) {
302 &gEfiDevicePathProtocolGuid
,
303 This
->DriverBindingHandle
,
311 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
312 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
313 // another SCSI Bus Driver to work on the same host handle.
315 ExtScsiSupport
= TRUE
;
316 PassThruStatus
= gBS
->OpenProtocol (
318 &gEfiScsiPassThruProtocolGuid
,
319 (VOID
**) &ScsiInterface
,
320 This
->DriverBindingHandle
,
322 EFI_OPEN_PROTOCOL_BY_DRIVER
326 if (Status
!= EFI_ALREADY_STARTED
) {
328 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
329 // on this handle for this time. Then construct Host controller private data.
332 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
333 if (ScsiBusDev
== NULL
) {
334 Status
= EFI_OUT_OF_RESOURCES
;
337 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
338 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
339 ScsiBusDev
->DevicePath
= ParentDevicePath
;
340 if (ScsiBusDev
->ExtScsiSupport
) {
341 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
343 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
347 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
348 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
349 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
351 Status
= gBS
->InstallProtocolInterface (
353 &mScsiBusProtocolGuid
,
354 EFI_NATIVE_INTERFACE
,
355 &ScsiBusDev
->BusIdentify
357 if (EFI_ERROR (Status
)) {
362 // Go through here means Start() is re-invoked again, nothing special is required to do except
363 // picking up Host controller private information.
365 Status
= gBS
->OpenProtocol (
367 &mScsiBusProtocolGuid
,
368 (VOID
**) &BusIdentify
,
369 This
->DriverBindingHandle
,
371 EFI_OPEN_PROTOCOL_GET_PROTOCOL
374 if (EFI_ERROR (Status
)) {
377 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
380 if (RemainingDevicePath
== NULL
) {
381 SetMem (ScsiTargetId
, TARGET_MAX_BYTES
,0xFF);
383 FromFirstTarget
= TRUE
;
385 if (ScsiBusDev
->ExtScsiSupport
) {
386 ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
388 ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
->ScsiId
.Scsi
, &Lun
);
392 while(ScanOtherPuns
) {
393 if (FromFirstTarget
) {
395 // Remaining Device Path is NULL, scan all the possible Puns in the
398 if (ScsiBusDev
->ExtScsiSupport
) {
399 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
401 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
->ScsiId
.Scsi
, &Lun
);
403 if (EFI_ERROR (Status
)) {
405 // no legal Pun and Lun found any more
410 ScanOtherPuns
= FALSE
;
413 // Avoid creating handle for the host adapter.
415 if (ScsiBusDev
->ExtScsiSupport
) {
416 if ((ScsiTargetId
->ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
420 if ((ScsiTargetId
->ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
425 // Scan for the scsi device, if it attaches to the scsi bus,
426 // then create handle and install scsi i/o protocol.
428 Status
= ScsiScanCreateDevice (This
, Controller
, ScsiTargetId
, Lun
, ScsiBusDev
);
430 FreePool (ScsiTargetId
);
435 if (ScsiBusDev
!= NULL
) {
436 FreePool (ScsiBusDev
);
439 if (ExtScsiSupport
) {
442 &gEfiExtScsiPassThruProtocolGuid
,
443 This
->DriverBindingHandle
,
446 if (!EFI_ERROR (PassThruStatus
)) {
449 &gEfiScsiPassThruProtocolGuid
,
450 This
->DriverBindingHandle
,
457 &gEfiScsiPassThruProtocolGuid
,
458 This
->DriverBindingHandle
,
466 Stop this driver on ControllerHandle.
468 This service is called by the EFI boot service DisconnectController().
469 In order to make drivers as small as possible, there are a few calling
470 restrictions for this service. DisconnectController() must follow these
471 calling restrictions. If any other agent wishes to call Stop() it must also
472 follow these calling restrictions.
474 @param This Protocol instance pointer.
475 @param ControllerHandle Handle of device to stop driver on
476 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
477 children is zero stop the entire bus driver.
478 @param ChildHandleBuffer List of Child Handles to Stop.
480 @retval EFI_SUCCESS This driver is removed ControllerHandle
481 @retval other This driver was not removed from this device
486 SCSIBusDriverBindingStop (
487 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
488 IN EFI_HANDLE Controller
,
489 IN UINTN NumberOfChildren
,
490 IN EFI_HANDLE
*ChildHandleBuffer
494 BOOLEAN AllChildrenStopped
;
496 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
497 SCSI_IO_DEV
*ScsiIoDevice
;
499 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
500 SCSI_BUS_DEVICE
*ScsiBusDev
;
502 if (NumberOfChildren
== 0) {
504 // Get the SCSI_BUS_DEVICE
506 Status
= gBS
->OpenProtocol (
508 &mScsiBusProtocolGuid
,
509 (VOID
**) &Scsidentifier
,
510 This
->DriverBindingHandle
,
512 EFI_OPEN_PROTOCOL_GET_PROTOCOL
515 if (EFI_ERROR (Status
)) {
516 return EFI_DEVICE_ERROR
;
519 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
522 // Uninstall SCSI Bus Protocol
524 gBS
->UninstallProtocolInterface (
526 &mScsiBusProtocolGuid
,
527 &ScsiBusDev
->BusIdentify
531 // Close the bus driver
533 if (ScsiBusDev
->ExtScsiSupport
) {
535 // Close ExtPassThru Protocol from this controller handle
539 &gEfiExtScsiPassThruProtocolGuid
,
540 This
->DriverBindingHandle
,
544 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
545 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
546 // So Stop() needs to try to close PassThru if present here.
550 &gEfiScsiPassThruProtocolGuid
,
551 This
->DriverBindingHandle
,
557 &gEfiScsiPassThruProtocolGuid
,
558 This
->DriverBindingHandle
,
565 &gEfiDevicePathProtocolGuid
,
566 This
->DriverBindingHandle
,
569 FreePool (ScsiBusDev
);
573 AllChildrenStopped
= TRUE
;
575 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
577 Status
= gBS
->OpenProtocol (
578 ChildHandleBuffer
[Index
],
579 &gEfiScsiIoProtocolGuid
,
581 This
->DriverBindingHandle
,
583 EFI_OPEN_PROTOCOL_GET_PROTOCOL
585 if (EFI_ERROR (Status
)) {
586 AllChildrenStopped
= FALSE
;
590 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
592 // Close the child handle
594 if (ScsiIoDevice
->ExtScsiSupport
) {
595 Status
= gBS
->CloseProtocol (
597 &gEfiExtScsiPassThruProtocolGuid
,
598 This
->DriverBindingHandle
,
599 ChildHandleBuffer
[Index
]
603 Status
= gBS
->CloseProtocol (
605 &gEfiScsiPassThruProtocolGuid
,
606 This
->DriverBindingHandle
,
607 ChildHandleBuffer
[Index
]
611 Status
= gBS
->UninstallMultipleProtocolInterfaces (
612 ChildHandleBuffer
[Index
],
613 &gEfiDevicePathProtocolGuid
,
614 ScsiIoDevice
->DevicePath
,
615 &gEfiScsiIoProtocolGuid
,
616 &ScsiIoDevice
->ScsiIo
,
619 if (EFI_ERROR (Status
)) {
620 AllChildrenStopped
= FALSE
;
621 if (ScsiIoDevice
->ExtScsiSupport
) {
624 &gEfiExtScsiPassThruProtocolGuid
,
626 This
->DriverBindingHandle
,
627 ChildHandleBuffer
[Index
],
628 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
633 &gEfiScsiPassThruProtocolGuid
,
635 This
->DriverBindingHandle
,
636 ChildHandleBuffer
[Index
],
637 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
641 FreePool (ScsiIoDevice
);
645 if (!AllChildrenStopped
) {
646 return EFI_DEVICE_ERROR
;
654 Retrieves the device type information of the SCSI Controller.
656 @param This Protocol instance pointer.
657 @param DeviceType A pointer to the device type information retrieved from
660 @retval EFI_SUCCESS Retrieves the device type information successfully.
661 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
667 IN EFI_SCSI_IO_PROTOCOL
*This
,
668 OUT UINT8
*DeviceType
671 SCSI_IO_DEV
*ScsiIoDevice
;
673 if (DeviceType
== NULL
) {
674 return EFI_INVALID_PARAMETER
;
677 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
678 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
684 Retrieves the device location in the SCSI channel.
686 @param This Protocol instance pointer.
687 @param Target A pointer to the Target ID of a SCSI device
689 @param Lun A pointer to the LUN of the SCSI device on
692 @retval EFI_SUCCESS Retrieves the device location successfully.
693 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
698 ScsiGetDeviceLocation (
699 IN EFI_SCSI_IO_PROTOCOL
*This
,
700 IN OUT UINT8
**Target
,
704 SCSI_IO_DEV
*ScsiIoDevice
;
706 if (Target
== NULL
|| Lun
== NULL
) {
707 return EFI_INVALID_PARAMETER
;
710 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
712 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
714 *Lun
= ScsiIoDevice
->Lun
;
720 Resets the SCSI Bus that the SCSI Controller is attached to.
722 @param This Protocol instance pointer.
724 @retval EFI_SUCCESS The SCSI bus is reset successfully.
725 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
726 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
727 SCSI Host Controller.
728 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
734 IN EFI_SCSI_IO_PROTOCOL
*This
737 SCSI_IO_DEV
*ScsiIoDevice
;
739 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
741 if (ScsiIoDevice
->ExtScsiSupport
){
742 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
744 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
750 Resets the SCSI Controller that the device handle specifies.
752 @param This Protocol instance pointer.
754 @retval EFI_SUCCESS Reset the SCSI controller successfully.
755 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
756 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
757 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
763 IN EFI_SCSI_IO_PROTOCOL
*This
766 SCSI_IO_DEV
*ScsiIoDevice
;
767 UINT8 Target
[TARGET_MAX_BYTES
];
769 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
770 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
773 if (ScsiIoDevice
->ExtScsiSupport
) {
774 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
775 ScsiIoDevice
->ExtScsiPassThru
,
780 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
781 ScsiIoDevice
->ScsiPassThru
,
782 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
790 Sends a SCSI Request Packet to the SCSI Controller for execution.
792 @param This Protocol instance pointer.
793 @param CommandPacket The SCSI request packet to send to the SCSI
794 Controller specified by the device handle.
795 @param Event If the SCSI bus where the SCSI device is attached
796 does not support non-blocking I/O, then Event is
797 ignored, and blocking I/O is performed.
798 If Event is NULL, then blocking I/O is performed.
799 If Event is not NULL and non-blocking I/O is
800 supported, then non-blocking I/O is performed,
801 and Event will be signaled when the SCSI Request
804 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
805 successfully, and TransferLength bytes were
806 transferred to/from DataBuffer.See
807 HostAdapterStatus, TargetStatus,
808 SenseDataLength, and SenseData in that order
809 for additional status information.
810 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
811 but the entire DataBuffer could not be transferred.
812 The actual number of bytes transferred is returned
813 in TransferLength. See HostAdapterStatus,
814 TargetStatus, SenseDataLength, and SenseData in
815 that order for additional status information.
816 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
817 there are too many SCSI Command Packets already
818 queued.The caller may retry again later.
819 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
820 the SCSI Request Packet. See HostAdapterStatus,
821 TargetStatus, SenseDataLength, and SenseData in
822 that order for additional status information.
823 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
824 The SCSI Request Packet was not sent, so no
825 additional status information is available.
826 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
827 is not supported by the SCSI initiator(i.e., SCSI
828 Host Controller). The SCSI Request Packet was not
829 sent, so no additional status information is
831 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
832 Request Packet to execute. See HostAdapterStatus,
833 TargetStatus, SenseDataLength, and SenseData in
834 that order for additional status information.
838 ScsiExecuteSCSICommand (
839 IN EFI_SCSI_IO_PROTOCOL
*This
,
840 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
841 IN EFI_EVENT Event OPTIONAL
844 SCSI_IO_DEV
*ScsiIoDevice
;
846 UINT8 Target
[TARGET_MAX_BYTES
];
847 EFI_EVENT PacketEvent
;
848 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
849 SCSI_EVENT_DATA EventData
;
853 if (Packet
== NULL
) {
854 return EFI_INVALID_PARAMETER
;
857 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
858 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
860 if (ScsiIoDevice
->ExtScsiSupport
) {
861 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
862 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
863 ScsiIoDevice
->ExtScsiPassThru
,
871 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
873 if (mWorkingBuffer
== NULL
) {
874 return EFI_DEVICE_ERROR
;
878 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
880 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
881 if (EFI_ERROR(Status
)) {
882 FreePool(mWorkingBuffer
);
886 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
887 EventData
.Data1
= (VOID
*)Packet
;
888 EventData
.Data2
= Event
;
892 Status
= gBS
->CreateEvent (
899 if (EFI_ERROR(Status
)) {
900 FreePool(mWorkingBuffer
);
904 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
905 ScsiIoDevice
->ScsiPassThru
,
906 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
912 if (EFI_ERROR(Status
)) {
913 FreePool(mWorkingBuffer
);
914 gBS
->CloseEvent(PacketEvent
);
920 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
921 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
923 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
924 ScsiIoDevice
->ScsiPassThru
,
925 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
930 if (EFI_ERROR(Status
)) {
931 FreePool(mWorkingBuffer
);
935 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
937 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
938 // free mWorkingBuffer.
940 FreePool(mWorkingBuffer
);
948 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
950 @param This Protocol instance pointer
951 @param Controller Controller handle
952 @param TargetId Tartget to be scanned
953 @param Lun The Lun of the SCSI device on the SCSI channel.
954 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
956 @retval EFI_SUCCESS Successfully to discover the device and attach
957 ScsiIoProtocol to it.
958 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
963 ScsiScanCreateDevice (
964 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
965 IN EFI_HANDLE Controller
,
966 IN SCSI_TARGET_ID
*TargetId
,
968 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
972 SCSI_IO_DEV
*ScsiIoDevice
;
973 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
975 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
976 if (ScsiIoDevice
== NULL
) {
977 return EFI_OUT_OF_RESOURCES
;
980 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
981 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
982 ScsiIoDevice
->Lun
= Lun
;
984 if (ScsiBusDev
->ExtScsiSupport
) {
985 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
986 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
987 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
990 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
991 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
992 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
995 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
996 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
997 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
998 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
999 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1002 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1003 FreePool (ScsiIoDevice
);
1004 return EFI_OUT_OF_RESOURCES
;
1010 ScsiDevicePath
= NULL
;
1011 if (ScsiIoDevice
->ExtScsiSupport
){
1012 Status
= ScsiIoDevice
->ExtScsiPassThru
->BuildDevicePath (
1013 ScsiIoDevice
->ExtScsiPassThru
,
1014 &ScsiIoDevice
->Pun
.ScsiId
.ExtScsi
[0],
1019 Status
= ScsiIoDevice
->ScsiPassThru
->BuildDevicePath (
1020 ScsiIoDevice
->ScsiPassThru
,
1021 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1027 if (Status
== EFI_OUT_OF_RESOURCES
) {
1028 FreePool (ScsiIoDevice
);
1032 ScsiIoDevice
->DevicePath
= AppendDevicePathNode (
1033 ScsiBusDev
->DevicePath
,
1037 // The memory space for ScsiDevicePath is allocated in
1038 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1039 // after EfiAppendDevicePathNode,so free the memory it occupies.
1041 FreePool (ScsiDevicePath
);
1043 if (ScsiIoDevice
->DevicePath
== NULL
) {
1044 FreePool (ScsiIoDevice
);
1045 return EFI_OUT_OF_RESOURCES
;
1048 Status
= gBS
->InstallMultipleProtocolInterfaces (
1049 &ScsiIoDevice
->Handle
,
1050 &gEfiDevicePathProtocolGuid
,
1051 ScsiIoDevice
->DevicePath
,
1052 &gEfiScsiIoProtocolGuid
,
1053 &ScsiIoDevice
->ScsiIo
,
1056 if (EFI_ERROR (Status
)) {
1057 FreePool (ScsiIoDevice
->DevicePath
);
1058 FreePool (ScsiIoDevice
);
1059 return EFI_OUT_OF_RESOURCES
;
1061 if (ScsiBusDev
->ExtScsiSupport
) {
1064 &gEfiExtScsiPassThruProtocolGuid
,
1065 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1066 This
->DriverBindingHandle
,
1067 ScsiIoDevice
->Handle
,
1068 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1073 &gEfiScsiPassThruProtocolGuid
,
1074 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1075 This
->DriverBindingHandle
,
1076 ScsiIoDevice
->Handle
,
1077 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1086 Discovery SCSI Device
1088 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1090 @retval TRUE Find SCSI Device and verify it.
1091 @retval FALSE Unable to find SCSI Device.
1095 DiscoverScsiDevice (
1096 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1100 UINT32 InquiryDataLength
;
1101 UINT8 SenseDataLength
;
1102 UINT8 HostAdapterStatus
;
1104 EFI_SCSI_SENSE_DATA SenseData
;
1105 EFI_SCSI_INQUIRY_DATA InquiryData
;
1107 HostAdapterStatus
= 0;
1110 // Using Inquiry command to scan for the device
1112 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1113 SenseDataLength
= sizeof (EFI_SCSI_SENSE_DATA
);
1115 Status
= ScsiInquiryCommand (
1116 &ScsiIoDevice
->ScsiIo
,
1117 EFI_TIMER_PERIOD_SECONDS (1),
1118 (VOID
*) &SenseData
,
1122 (VOID
*) &InquiryData
,
1126 if (EFI_ERROR (Status
)) {
1130 // Retrieved inquiry data successfully
1132 if ((InquiryData
.Peripheral_Qualifier
!= 0) &&
1133 (InquiryData
.Peripheral_Qualifier
!= 3)) {
1137 if (InquiryData
.Peripheral_Qualifier
== 3) {
1138 if (InquiryData
.Peripheral_Type
!= 0x1f) {
1143 if (0x1e >= InquiryData
.Peripheral_Type
&& InquiryData
.Peripheral_Type
>= 0xa) {
1148 // valid device type and peripheral qualifier combination.
1150 ScsiIoDevice
->ScsiDeviceType
= InquiryData
.Peripheral_Type
;
1151 ScsiIoDevice
->RemovableDevice
= InquiryData
.Rmb
;
1152 if (InquiryData
.Version
== 0) {
1153 ScsiIoDevice
->ScsiVersion
= 0;
1156 // ANSI-approved version
1158 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
.Version
& 0x03);
1166 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1168 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1169 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1174 ScsiioToPassThruPacket (
1175 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1176 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1180 //EFI 1.10 doesn't support Bi-Direction Command.
1182 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1183 return EFI_UNSUPPORTED
;
1186 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1188 CommandPacket
->Timeout
= Packet
->Timeout
;
1189 CommandPacket
->Cdb
= Packet
->Cdb
;
1190 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1191 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1192 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1193 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1194 CommandPacket
->SenseData
= Packet
->SenseData
;
1195 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1197 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1198 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1199 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1200 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1201 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1202 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1209 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1211 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1212 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1217 PassThruToScsiioPacket (
1218 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1219 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1222 Packet
->Timeout
= ScsiPacket
->Timeout
;
1223 Packet
->Cdb
= ScsiPacket
->Cdb
;
1224 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1225 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1226 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1227 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1228 Packet
->SenseData
= ScsiPacket
->SenseData
;
1229 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1231 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1232 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1233 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1234 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1235 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1236 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1243 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1246 @param Event The instance of EFI_EVENT.
1247 @param Context The parameter passed in.
1257 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1258 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1259 EFI_EVENT CallerEvent
;
1260 SCSI_EVENT_DATA
*PassData
;
1262 PassData
= (SCSI_EVENT_DATA
*)Context
;
1263 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1264 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1267 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1269 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1272 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1273 // free mWorkingBuffer.
1275 gBS
->FreePool(mWorkingBuffer
);
1278 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1280 CallerEvent
= PassData
->Data2
;
1281 gBS
->CloseEvent(Event
);
1282 gBS
->SignalEvent(CallerEvent
);