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 - 2009, 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
;
150 if (RemainingDevicePath
!= NULL
) {
151 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
153 // Check if RemainingDevicePath is the End of Device Path Node,
154 // if yes, go on checking other conditions
156 if (!IsDevicePathEnd (Node
)) {
158 // If RemainingDevicePath isn't the End of Device Path Node,
159 // check its validation
161 if (Node
->DevPath
.Type
!= MESSAGING_DEVICE_PATH
||
162 Node
->DevPath
.SubType
!= MSG_SCSI_DP
||
163 DevicePathNodeLength(&Node
->DevPath
) != sizeof(ATAPI_DEVICE_PATH
)) {
164 return EFI_UNSUPPORTED
;
170 // Check for the existence of Extended SCSI Pass Thru Protocol and SCSI Pass Thru Protocol
172 Status
= gBS
->OpenProtocol (
174 &gEfiExtScsiPassThruProtocolGuid
,
175 (VOID
**)&ExtPassThru
,
176 This
->DriverBindingHandle
,
178 EFI_OPEN_PROTOCOL_BY_DRIVER
181 if (Status
== EFI_ALREADY_STARTED
) {
185 if (EFI_ERROR (Status
)) {
186 Status
= gBS
->OpenProtocol (
188 &gEfiScsiPassThruProtocolGuid
,
190 This
->DriverBindingHandle
,
192 EFI_OPEN_PROTOCOL_BY_DRIVER
195 if (Status
== EFI_ALREADY_STARTED
) {
199 if (EFI_ERROR (Status
)) {
205 &gEfiScsiPassThruProtocolGuid
,
206 This
->DriverBindingHandle
,
213 // Close the I/O Abstraction(s) used to perform the supported test
217 &gEfiExtScsiPassThruProtocolGuid
,
218 This
->DriverBindingHandle
,
227 Start this driver on ControllerHandle.
229 This service is called by the EFI boot service ConnectController(). In order
230 to make drivers as small as possible, there are a few calling restrictions for
231 this service. ConnectController() must follow these calling restrictions. If
232 any other agent wishes to call Start() it must also follow these calling
235 @param This Protocol instance pointer.
236 @param ControllerHandle Handle of device to bind driver to
237 @param RemainingDevicePath Optional parameter use to pick a specific child
240 @retval EFI_SUCCESS This driver is added to ControllerHandle
241 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
242 @retval other This driver does not support this device
247 SCSIBusDriverBindingStart (
248 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
249 IN EFI_HANDLE Controller
,
250 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
255 BOOLEAN ScanOtherPuns
;
256 BOOLEAN FromFirstTarget
;
257 BOOLEAN ExtScsiSupport
;
259 EFI_STATUS DevicePathStatus
;
260 EFI_STATUS PassThruStatus
;
261 SCSI_BUS_DEVICE
*ScsiBusDev
;
262 SCSI_TARGET_ID
*ScsiTargetId
;
263 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
264 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiInterface
;
265 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiInterface
;
266 EFI_SCSI_BUS_PROTOCOL
*BusIdentify
;
270 ScanOtherPuns
= TRUE
;
271 FromFirstTarget
= FALSE
;
272 ExtScsiSupport
= FALSE
;
273 PassThruStatus
= EFI_SUCCESS
;
275 ScsiTargetId
= AllocateZeroPool(sizeof(SCSI_TARGET_ID
));
276 if (ScsiTargetId
== NULL
) {
277 return EFI_OUT_OF_RESOURCES
;
280 TargetId
= &ScsiTargetId
->ScsiId
.ExtScsi
[0];
282 DevicePathStatus
= gBS
->OpenProtocol (
284 &gEfiDevicePathProtocolGuid
,
285 (VOID
**) &ParentDevicePath
,
286 This
->DriverBindingHandle
,
288 EFI_OPEN_PROTOCOL_BY_DRIVER
290 if (EFI_ERROR (DevicePathStatus
) && (DevicePathStatus
!= EFI_ALREADY_STARTED
)) {
291 return DevicePathStatus
;
295 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
296 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
297 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
299 Status
= gBS
->OpenProtocol (
301 &gEfiExtScsiPassThruProtocolGuid
,
302 (VOID
**) &ExtScsiInterface
,
303 This
->DriverBindingHandle
,
305 EFI_OPEN_PROTOCOL_BY_DRIVER
308 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
310 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
311 Status
= gBS
->OpenProtocol (
313 &gEfiScsiPassThruProtocolGuid
,
314 (VOID
**) &ScsiInterface
,
315 This
->DriverBindingHandle
,
317 EFI_OPEN_PROTOCOL_BY_DRIVER
320 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
322 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
323 if (!EFI_ERROR(DevicePathStatus
)) {
326 &gEfiDevicePathProtocolGuid
,
327 This
->DriverBindingHandle
,
335 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
336 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
337 // another SCSI Bus Driver to work on the same host handle.
339 ExtScsiSupport
= TRUE
;
340 PassThruStatus
= gBS
->OpenProtocol (
342 &gEfiScsiPassThruProtocolGuid
,
343 (VOID
**) &ScsiInterface
,
344 This
->DriverBindingHandle
,
346 EFI_OPEN_PROTOCOL_BY_DRIVER
350 if (Status
!= EFI_ALREADY_STARTED
) {
352 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
353 // on this handle for this time. Then construct Host controller private data.
356 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
357 if (ScsiBusDev
== NULL
) {
358 Status
= EFI_OUT_OF_RESOURCES
;
361 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
362 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
363 ScsiBusDev
->DevicePath
= ParentDevicePath
;
364 if (ScsiBusDev
->ExtScsiSupport
) {
365 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
367 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
371 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
372 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
373 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
375 Status
= gBS
->InstallProtocolInterface (
377 &mScsiBusProtocolGuid
,
378 EFI_NATIVE_INTERFACE
,
379 &ScsiBusDev
->BusIdentify
381 if (EFI_ERROR (Status
)) {
386 // Go through here means Start() is re-invoked again, nothing special is required to do except
387 // picking up Host controller private information.
389 Status
= gBS
->OpenProtocol (
391 &mScsiBusProtocolGuid
,
392 (VOID
**) &BusIdentify
,
393 This
->DriverBindingHandle
,
395 EFI_OPEN_PROTOCOL_GET_PROTOCOL
398 if (EFI_ERROR (Status
)) {
401 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
405 if (RemainingDevicePath
== NULL
) {
407 // If RemainingDevicePath is NULL,
408 // must enumerate all SCSI devices anyway
410 SetMem (ScsiTargetId
, TARGET_MAX_BYTES
,0xFF);
411 FromFirstTarget
= TRUE
;
413 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
415 // If RemainingDevicePath isn't the End of Device Path Node,
416 // only scan the specified device by RemainingDevicePath
418 if (ScsiBusDev
->ExtScsiSupport
) {
419 ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
421 ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
->ScsiId
.Scsi
, &Lun
);
426 // If RemainingDevicePath is the End of Device Path Node,
427 // skip enumerate any device and return EFI_SUCESSS
429 ScanOtherPuns
= FALSE
;
432 while(ScanOtherPuns
) {
433 if (FromFirstTarget
) {
435 // Remaining Device Path is NULL, scan all the possible Puns in the
438 if (ScsiBusDev
->ExtScsiSupport
) {
439 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
441 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
->ScsiId
.Scsi
, &Lun
);
443 if (EFI_ERROR (Status
)) {
445 // no legal Pun and Lun found any more
450 ScanOtherPuns
= FALSE
;
453 // Avoid creating handle for the host adapter.
455 if (ScsiBusDev
->ExtScsiSupport
) {
456 if ((ScsiTargetId
->ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
460 if ((ScsiTargetId
->ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
465 // Scan for the scsi device, if it attaches to the scsi bus,
466 // then create handle and install scsi i/o protocol.
468 Status
= ScsiScanCreateDevice (This
, Controller
, ScsiTargetId
, Lun
, ScsiBusDev
);
470 FreePool (ScsiTargetId
);
475 if (ScsiBusDev
!= NULL
) {
476 FreePool (ScsiBusDev
);
479 if (ExtScsiSupport
) {
482 &gEfiExtScsiPassThruProtocolGuid
,
483 This
->DriverBindingHandle
,
486 if (!EFI_ERROR (PassThruStatus
)) {
489 &gEfiScsiPassThruProtocolGuid
,
490 This
->DriverBindingHandle
,
497 &gEfiScsiPassThruProtocolGuid
,
498 This
->DriverBindingHandle
,
506 Stop this driver on ControllerHandle.
508 This service is called by the EFI boot service DisconnectController().
509 In order to make drivers as small as possible, there are a few calling
510 restrictions for this service. DisconnectController() must follow these
511 calling restrictions. If any other agent wishes to call Stop() it must also
512 follow these calling restrictions.
514 @param This Protocol instance pointer.
515 @param ControllerHandle Handle of device to stop driver on
516 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
517 children is zero stop the entire bus driver.
518 @param ChildHandleBuffer List of Child Handles to Stop.
520 @retval EFI_SUCCESS This driver is removed ControllerHandle
521 @retval other This driver was not removed from this device
526 SCSIBusDriverBindingStop (
527 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
528 IN EFI_HANDLE Controller
,
529 IN UINTN NumberOfChildren
,
530 IN EFI_HANDLE
*ChildHandleBuffer
534 BOOLEAN AllChildrenStopped
;
536 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
537 SCSI_IO_DEV
*ScsiIoDevice
;
539 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
540 SCSI_BUS_DEVICE
*ScsiBusDev
;
542 if (NumberOfChildren
== 0) {
544 // Get the SCSI_BUS_DEVICE
546 Status
= gBS
->OpenProtocol (
548 &mScsiBusProtocolGuid
,
549 (VOID
**) &Scsidentifier
,
550 This
->DriverBindingHandle
,
552 EFI_OPEN_PROTOCOL_GET_PROTOCOL
555 if (EFI_ERROR (Status
)) {
556 return EFI_DEVICE_ERROR
;
559 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
562 // Uninstall SCSI Bus Protocol
564 gBS
->UninstallProtocolInterface (
566 &mScsiBusProtocolGuid
,
567 &ScsiBusDev
->BusIdentify
571 // Close the bus driver
573 if (ScsiBusDev
->ExtScsiSupport
) {
575 // Close ExtPassThru Protocol from this controller handle
579 &gEfiExtScsiPassThruProtocolGuid
,
580 This
->DriverBindingHandle
,
584 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
585 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
586 // So Stop() needs to try to close PassThru if present here.
590 &gEfiScsiPassThruProtocolGuid
,
591 This
->DriverBindingHandle
,
597 &gEfiScsiPassThruProtocolGuid
,
598 This
->DriverBindingHandle
,
605 &gEfiDevicePathProtocolGuid
,
606 This
->DriverBindingHandle
,
609 FreePool (ScsiBusDev
);
613 AllChildrenStopped
= TRUE
;
615 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
617 Status
= gBS
->OpenProtocol (
618 ChildHandleBuffer
[Index
],
619 &gEfiScsiIoProtocolGuid
,
621 This
->DriverBindingHandle
,
623 EFI_OPEN_PROTOCOL_GET_PROTOCOL
625 if (EFI_ERROR (Status
)) {
626 AllChildrenStopped
= FALSE
;
630 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
632 // Close the child handle
634 if (ScsiIoDevice
->ExtScsiSupport
) {
635 Status
= gBS
->CloseProtocol (
637 &gEfiExtScsiPassThruProtocolGuid
,
638 This
->DriverBindingHandle
,
639 ChildHandleBuffer
[Index
]
643 Status
= gBS
->CloseProtocol (
645 &gEfiScsiPassThruProtocolGuid
,
646 This
->DriverBindingHandle
,
647 ChildHandleBuffer
[Index
]
651 Status
= gBS
->UninstallMultipleProtocolInterfaces (
652 ChildHandleBuffer
[Index
],
653 &gEfiDevicePathProtocolGuid
,
654 ScsiIoDevice
->DevicePath
,
655 &gEfiScsiIoProtocolGuid
,
656 &ScsiIoDevice
->ScsiIo
,
659 if (EFI_ERROR (Status
)) {
660 AllChildrenStopped
= FALSE
;
661 if (ScsiIoDevice
->ExtScsiSupport
) {
664 &gEfiExtScsiPassThruProtocolGuid
,
666 This
->DriverBindingHandle
,
667 ChildHandleBuffer
[Index
],
668 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
673 &gEfiScsiPassThruProtocolGuid
,
675 This
->DriverBindingHandle
,
676 ChildHandleBuffer
[Index
],
677 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
681 FreePool (ScsiIoDevice
);
685 if (!AllChildrenStopped
) {
686 return EFI_DEVICE_ERROR
;
694 Retrieves the device type information of the SCSI Controller.
696 @param This Protocol instance pointer.
697 @param DeviceType A pointer to the device type information retrieved from
700 @retval EFI_SUCCESS Retrieves the device type information successfully.
701 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
707 IN EFI_SCSI_IO_PROTOCOL
*This
,
708 OUT UINT8
*DeviceType
711 SCSI_IO_DEV
*ScsiIoDevice
;
713 if (DeviceType
== NULL
) {
714 return EFI_INVALID_PARAMETER
;
717 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
718 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
724 Retrieves the device location in the SCSI channel.
726 @param This Protocol instance pointer.
727 @param Target A pointer to the Target ID of a SCSI device
729 @param Lun A pointer to the LUN of the SCSI device on
732 @retval EFI_SUCCESS Retrieves the device location successfully.
733 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
738 ScsiGetDeviceLocation (
739 IN EFI_SCSI_IO_PROTOCOL
*This
,
740 IN OUT UINT8
**Target
,
744 SCSI_IO_DEV
*ScsiIoDevice
;
746 if (Target
== NULL
|| Lun
== NULL
) {
747 return EFI_INVALID_PARAMETER
;
750 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
752 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
754 *Lun
= ScsiIoDevice
->Lun
;
760 Resets the SCSI Bus that the SCSI Controller is attached to.
762 @param This Protocol instance pointer.
764 @retval EFI_SUCCESS The SCSI bus is reset successfully.
765 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
766 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
767 SCSI Host Controller.
768 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
774 IN EFI_SCSI_IO_PROTOCOL
*This
777 SCSI_IO_DEV
*ScsiIoDevice
;
779 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
781 if (ScsiIoDevice
->ExtScsiSupport
){
782 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
784 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
790 Resets the SCSI Controller that the device handle specifies.
792 @param This Protocol instance pointer.
794 @retval EFI_SUCCESS Reset the SCSI controller successfully.
795 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
796 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
797 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
803 IN EFI_SCSI_IO_PROTOCOL
*This
806 SCSI_IO_DEV
*ScsiIoDevice
;
807 UINT8 Target
[TARGET_MAX_BYTES
];
809 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
810 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
813 if (ScsiIoDevice
->ExtScsiSupport
) {
814 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
815 ScsiIoDevice
->ExtScsiPassThru
,
820 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
821 ScsiIoDevice
->ScsiPassThru
,
822 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
830 Sends a SCSI Request Packet to the SCSI Controller for execution.
832 @param This Protocol instance pointer.
833 @param CommandPacket The SCSI request packet to send to the SCSI
834 Controller specified by the device handle.
835 @param Event If the SCSI bus where the SCSI device is attached
836 does not support non-blocking I/O, then Event is
837 ignored, and blocking I/O is performed.
838 If Event is NULL, then blocking I/O is performed.
839 If Event is not NULL and non-blocking I/O is
840 supported, then non-blocking I/O is performed,
841 and Event will be signaled when the SCSI Request
844 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
845 successfully, and TransferLength bytes were
846 transferred to/from DataBuffer.See
847 HostAdapterStatus, TargetStatus,
848 SenseDataLength, and SenseData in that order
849 for additional status information.
850 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
851 but the entire DataBuffer could not be transferred.
852 The actual number of bytes transferred is returned
853 in TransferLength. See HostAdapterStatus,
854 TargetStatus, SenseDataLength, and SenseData in
855 that order for additional status information.
856 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
857 there are too many SCSI Command Packets already
858 queued.The caller may retry again later.
859 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
860 the SCSI Request Packet. See HostAdapterStatus,
861 TargetStatus, SenseDataLength, and SenseData in
862 that order for additional status information.
863 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
864 The SCSI Request Packet was not sent, so no
865 additional status information is available.
866 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
867 is not supported by the SCSI initiator(i.e., SCSI
868 Host Controller). The SCSI Request Packet was not
869 sent, so no additional status information is
871 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
872 Request Packet to execute. See HostAdapterStatus,
873 TargetStatus, SenseDataLength, and SenseData in
874 that order for additional status information.
878 ScsiExecuteSCSICommand (
879 IN EFI_SCSI_IO_PROTOCOL
*This
,
880 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
881 IN EFI_EVENT Event OPTIONAL
884 SCSI_IO_DEV
*ScsiIoDevice
;
886 UINT8 Target
[TARGET_MAX_BYTES
];
887 EFI_EVENT PacketEvent
;
888 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
889 SCSI_EVENT_DATA EventData
;
893 if (Packet
== NULL
) {
894 return EFI_INVALID_PARAMETER
;
897 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
898 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
900 if (ScsiIoDevice
->ExtScsiSupport
) {
901 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
902 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
903 ScsiIoDevice
->ExtScsiPassThru
,
911 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
913 if (mWorkingBuffer
== NULL
) {
914 return EFI_DEVICE_ERROR
;
918 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
920 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
921 if (EFI_ERROR(Status
)) {
922 FreePool(mWorkingBuffer
);
926 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
927 EventData
.Data1
= (VOID
*)Packet
;
928 EventData
.Data2
= Event
;
932 Status
= gBS
->CreateEvent (
939 if (EFI_ERROR(Status
)) {
940 FreePool(mWorkingBuffer
);
944 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
945 ScsiIoDevice
->ScsiPassThru
,
946 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
952 if (EFI_ERROR(Status
)) {
953 FreePool(mWorkingBuffer
);
954 gBS
->CloseEvent(PacketEvent
);
960 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
961 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
963 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
964 ScsiIoDevice
->ScsiPassThru
,
965 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
970 if (EFI_ERROR(Status
)) {
971 FreePool(mWorkingBuffer
);
975 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
977 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
978 // free mWorkingBuffer.
980 FreePool(mWorkingBuffer
);
988 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
990 @param This Protocol instance pointer
991 @param Controller Controller handle
992 @param TargetId Tartget to be scanned
993 @param Lun The Lun of the SCSI device on the SCSI channel.
994 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
996 @retval EFI_SUCCESS Successfully to discover the device and attach
997 ScsiIoProtocol to it.
998 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
1003 ScsiScanCreateDevice (
1004 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1005 IN EFI_HANDLE Controller
,
1006 IN SCSI_TARGET_ID
*TargetId
,
1008 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
1012 SCSI_IO_DEV
*ScsiIoDevice
;
1013 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
1015 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
1016 if (ScsiIoDevice
== NULL
) {
1017 return EFI_OUT_OF_RESOURCES
;
1020 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
1021 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
1022 ScsiIoDevice
->Lun
= Lun
;
1024 if (ScsiBusDev
->ExtScsiSupport
) {
1025 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
1026 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
1027 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
1030 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
1031 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
1032 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
1035 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
1036 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
1037 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
1038 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
1039 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1042 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1043 FreePool (ScsiIoDevice
);
1044 return EFI_OUT_OF_RESOURCES
;
1050 ScsiDevicePath
= NULL
;
1051 if (ScsiIoDevice
->ExtScsiSupport
){
1052 Status
= ScsiIoDevice
->ExtScsiPassThru
->BuildDevicePath (
1053 ScsiIoDevice
->ExtScsiPassThru
,
1054 &ScsiIoDevice
->Pun
.ScsiId
.ExtScsi
[0],
1059 Status
= ScsiIoDevice
->ScsiPassThru
->BuildDevicePath (
1060 ScsiIoDevice
->ScsiPassThru
,
1061 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1067 if (EFI_ERROR(Status
)) {
1068 FreePool (ScsiIoDevice
);
1072 ScsiIoDevice
->DevicePath
= AppendDevicePathNode (
1073 ScsiBusDev
->DevicePath
,
1077 // The memory space for ScsiDevicePath is allocated in
1078 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1079 // after EfiAppendDevicePathNode,so free the memory it occupies.
1081 FreePool (ScsiDevicePath
);
1083 if (ScsiIoDevice
->DevicePath
== NULL
) {
1084 FreePool (ScsiIoDevice
);
1085 return EFI_OUT_OF_RESOURCES
;
1088 Status
= gBS
->InstallMultipleProtocolInterfaces (
1089 &ScsiIoDevice
->Handle
,
1090 &gEfiDevicePathProtocolGuid
,
1091 ScsiIoDevice
->DevicePath
,
1092 &gEfiScsiIoProtocolGuid
,
1093 &ScsiIoDevice
->ScsiIo
,
1096 if (EFI_ERROR (Status
)) {
1097 FreePool (ScsiIoDevice
->DevicePath
);
1098 FreePool (ScsiIoDevice
);
1099 return EFI_OUT_OF_RESOURCES
;
1101 if (ScsiBusDev
->ExtScsiSupport
) {
1104 &gEfiExtScsiPassThruProtocolGuid
,
1105 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1106 This
->DriverBindingHandle
,
1107 ScsiIoDevice
->Handle
,
1108 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1113 &gEfiScsiPassThruProtocolGuid
,
1114 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1115 This
->DriverBindingHandle
,
1116 ScsiIoDevice
->Handle
,
1117 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1126 Discovery SCSI Device
1128 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1130 @retval TRUE Find SCSI Device and verify it.
1131 @retval FALSE Unable to find SCSI Device.
1135 DiscoverScsiDevice (
1136 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1140 UINT32 InquiryDataLength
;
1141 UINT8 SenseDataLength
;
1142 UINT8 HostAdapterStatus
;
1144 EFI_SCSI_SENSE_DATA SenseData
;
1145 EFI_SCSI_INQUIRY_DATA InquiryData
;
1147 HostAdapterStatus
= 0;
1150 // Using Inquiry command to scan for the device
1152 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1153 SenseDataLength
= sizeof (EFI_SCSI_SENSE_DATA
);
1155 Status
= ScsiInquiryCommand (
1156 &ScsiIoDevice
->ScsiIo
,
1157 EFI_TIMER_PERIOD_SECONDS (1),
1158 (VOID
*) &SenseData
,
1162 (VOID
*) &InquiryData
,
1166 if (EFI_ERROR (Status
) && Status
!= EFI_BAD_BUFFER_SIZE
) {
1170 // Retrieved inquiry data successfully
1172 if ((InquiryData
.Peripheral_Qualifier
!= 0) &&
1173 (InquiryData
.Peripheral_Qualifier
!= 3)) {
1177 if (InquiryData
.Peripheral_Qualifier
== 3) {
1178 if (InquiryData
.Peripheral_Type
!= 0x1f) {
1183 if (0x1e >= InquiryData
.Peripheral_Type
&& InquiryData
.Peripheral_Type
>= 0xa) {
1188 // valid device type and peripheral qualifier combination.
1190 ScsiIoDevice
->ScsiDeviceType
= InquiryData
.Peripheral_Type
;
1191 ScsiIoDevice
->RemovableDevice
= InquiryData
.Rmb
;
1192 if (InquiryData
.Version
== 0) {
1193 ScsiIoDevice
->ScsiVersion
= 0;
1196 // ANSI-approved version
1198 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
.Version
& 0x03);
1206 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1208 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1209 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1214 ScsiioToPassThruPacket (
1215 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1216 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1220 //EFI 1.10 doesn't support Bi-Direction Command.
1222 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1223 return EFI_UNSUPPORTED
;
1226 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1228 CommandPacket
->Timeout
= Packet
->Timeout
;
1229 CommandPacket
->Cdb
= Packet
->Cdb
;
1230 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1231 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1232 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1233 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1234 CommandPacket
->SenseData
= Packet
->SenseData
;
1235 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1237 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1238 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1239 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1240 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1241 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1242 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1249 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1251 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1252 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1257 PassThruToScsiioPacket (
1258 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1259 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1262 Packet
->Timeout
= ScsiPacket
->Timeout
;
1263 Packet
->Cdb
= ScsiPacket
->Cdb
;
1264 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1265 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1266 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1267 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1268 Packet
->SenseData
= ScsiPacket
->SenseData
;
1269 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1271 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1272 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1273 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1274 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1275 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1276 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1283 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1286 @param Event The instance of EFI_EVENT.
1287 @param Context The parameter passed in.
1297 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1298 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1299 EFI_EVENT CallerEvent
;
1300 SCSI_EVENT_DATA
*PassData
;
1302 PassData
= (SCSI_EVENT_DATA
*)Context
;
1303 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1304 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1307 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1309 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1312 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1313 // free mWorkingBuffer.
1315 gBS
->FreePool(mWorkingBuffer
);
1318 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1320 CallerEvent
= PassData
->Data2
;
1321 gBS
->CloseEvent(Event
);
1322 gBS
->SignalEvent(CallerEvent
);