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 - 2010, 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 SCSI_TARGET_ID ScsiTargetId
;
152 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
153 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
156 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
157 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
158 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
160 Status
= gBS
->OpenProtocol (
162 &gEfiExtScsiPassThruProtocolGuid
,
163 (VOID
**)&ExtPassThru
,
164 This
->DriverBindingHandle
,
166 EFI_OPEN_PROTOCOL_BY_DRIVER
169 if (Status
== EFI_ALREADY_STARTED
) {
171 } else if (!EFI_ERROR(Status
)) {
173 // Check if RemainingDevicePath is NULL or the End of Device Path Node,
174 // if yes, return EFI_SUCCESS.
176 if ((RemainingDevicePath
== NULL
) || IsDevicePathEnd (RemainingDevicePath
)) {
180 // If RemainingDevicePath isn't the End of Device Path Node, check its validation
182 Status
= ExtPassThru
->GetTargetLun (ExtPassThru
, RemainingDevicePath
, &TargetId
, &Lun
);
184 // Close protocol regardless of RemainingDevicePath validation
188 &gEfiExtScsiPassThruProtocolGuid
,
189 This
->DriverBindingHandle
,
192 if (!EFI_ERROR(Status
)) {
199 // Come here in 2 condition:
200 // 1. ExtPassThru doesn't exist.
201 // 2. ExtPassThru exists but RemainingDevicePath is invalid.
203 Status
= gBS
->OpenProtocol (
205 &gEfiScsiPassThruProtocolGuid
,
207 This
->DriverBindingHandle
,
209 EFI_OPEN_PROTOCOL_BY_DRIVER
212 if (Status
== EFI_ALREADY_STARTED
) {
216 if (EFI_ERROR (Status
)) {
221 // Test RemainingDevicePath is valid or not.
223 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
224 Status
= PassThru
->GetTargetLun (PassThru
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
229 &gEfiScsiPassThruProtocolGuid
,
230 This
->DriverBindingHandle
,
238 Start this driver on ControllerHandle.
240 This service is called by the EFI boot service ConnectController(). In order
241 to make drivers as small as possible, there are a few calling restrictions for
242 this service. ConnectController() must follow these calling restrictions. If
243 any other agent wishes to call Start() it must also follow these calling
246 @param This Protocol instance pointer.
247 @param ControllerHandle Handle of device to bind driver to
248 @param RemainingDevicePath Optional parameter use to pick a specific child
251 @retval EFI_SUCCESS This driver is added to ControllerHandle
252 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
253 @retval other This driver does not support this device
258 SCSIBusDriverBindingStart (
259 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
260 IN EFI_HANDLE Controller
,
261 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
266 BOOLEAN ScanOtherPuns
;
267 BOOLEAN FromFirstTarget
;
268 BOOLEAN ExtScsiSupport
;
270 EFI_STATUS DevicePathStatus
;
271 EFI_STATUS PassThruStatus
;
272 SCSI_BUS_DEVICE
*ScsiBusDev
;
273 SCSI_TARGET_ID ScsiTargetId
;
274 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
275 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiInterface
;
276 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiInterface
;
277 EFI_SCSI_BUS_PROTOCOL
*BusIdentify
;
280 ScanOtherPuns
= TRUE
;
281 FromFirstTarget
= FALSE
;
282 ExtScsiSupport
= FALSE
;
283 PassThruStatus
= EFI_SUCCESS
;
285 TargetId
= &ScsiTargetId
.ScsiId
.ExtScsi
[0];
286 SetMem (TargetId
, TARGET_MAX_BYTES
, 0xFF);
288 DevicePathStatus
= gBS
->OpenProtocol (
290 &gEfiDevicePathProtocolGuid
,
291 (VOID
**) &ParentDevicePath
,
292 This
->DriverBindingHandle
,
294 EFI_OPEN_PROTOCOL_BY_DRIVER
296 if (EFI_ERROR (DevicePathStatus
) && (DevicePathStatus
!= EFI_ALREADY_STARTED
)) {
297 return DevicePathStatus
;
301 // To keep backward compatibility, UEFI ExtPassThru Protocol is supported as well as
302 // EFI PassThru Protocol. From priority perspective, ExtPassThru Protocol is firstly
303 // tried to open on host controller handle. If fails, then PassThru Protocol is tried instead.
305 Status
= gBS
->OpenProtocol (
307 &gEfiExtScsiPassThruProtocolGuid
,
308 (VOID
**) &ExtScsiInterface
,
309 This
->DriverBindingHandle
,
311 EFI_OPEN_PROTOCOL_BY_DRIVER
314 // Fail to open UEFI ExtendPassThru Protocol, then try to open EFI PassThru Protocol instead.
316 if (EFI_ERROR(Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
317 Status
= gBS
->OpenProtocol (
319 &gEfiScsiPassThruProtocolGuid
,
320 (VOID
**) &ScsiInterface
,
321 This
->DriverBindingHandle
,
323 EFI_OPEN_PROTOCOL_BY_DRIVER
326 // Fail to open EFI PassThru Protocol, Close the DevicePathProtocol if it is opened by this time.
328 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
329 if (!EFI_ERROR(DevicePathStatus
)) {
332 &gEfiDevicePathProtocolGuid
,
333 This
->DriverBindingHandle
,
341 // Succeed to open ExtPassThru Protocol, and meanwhile open PassThru Protocol
342 // with BY_DRIVER if it is also present on the handle. The intent is to prevent
343 // another SCSI Bus Driver to work on the same host handle.
345 ExtScsiSupport
= TRUE
;
346 PassThruStatus
= gBS
->OpenProtocol (
348 &gEfiScsiPassThruProtocolGuid
,
349 (VOID
**) &ScsiInterface
,
350 This
->DriverBindingHandle
,
352 EFI_OPEN_PROTOCOL_BY_DRIVER
356 if (Status
!= EFI_ALREADY_STARTED
) {
358 // Go through here means either ExtPassThru or PassThru Protocol is successfully opened
359 // on this handle for this time. Then construct Host controller private data.
362 ScsiBusDev
= AllocateZeroPool(sizeof(SCSI_BUS_DEVICE
));
363 if (ScsiBusDev
== NULL
) {
364 Status
= EFI_OUT_OF_RESOURCES
;
367 ScsiBusDev
->Signature
= SCSI_BUS_DEVICE_SIGNATURE
;
368 ScsiBusDev
->ExtScsiSupport
= ExtScsiSupport
;
369 ScsiBusDev
->DevicePath
= ParentDevicePath
;
370 if (ScsiBusDev
->ExtScsiSupport
) {
371 ScsiBusDev
->ExtScsiInterface
= ExtScsiInterface
;
373 ScsiBusDev
->ScsiInterface
= ScsiInterface
;
377 // Install EFI_SCSI_BUS_PROTOCOL to the controller handle, So ScsiBusDev could be
378 // retrieved on this controller handle. With ScsiBusDev, we can know which PassThru
379 // Protocol is present on the handle, UEFI ExtPassThru Protocol or EFI PassThru Protocol.
381 Status
= gBS
->InstallProtocolInterface (
383 &mScsiBusProtocolGuid
,
384 EFI_NATIVE_INTERFACE
,
385 &ScsiBusDev
->BusIdentify
387 if (EFI_ERROR (Status
)) {
392 // Go through here means Start() is re-invoked again, nothing special is required to do except
393 // picking up Host controller private information.
395 Status
= gBS
->OpenProtocol (
397 &mScsiBusProtocolGuid
,
398 (VOID
**) &BusIdentify
,
399 This
->DriverBindingHandle
,
401 EFI_OPEN_PROTOCOL_GET_PROTOCOL
404 if (EFI_ERROR (Status
)) {
407 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (BusIdentify
);
411 if (RemainingDevicePath
== NULL
) {
413 // If RemainingDevicePath is NULL,
414 // must enumerate all SCSI devices anyway
416 FromFirstTarget
= TRUE
;
417 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
419 // If RemainingDevicePath isn't the End of Device Path Node,
420 // only scan the specified device by RemainingDevicePath
422 if (ScsiBusDev
->ExtScsiSupport
) {
423 ScsiBusDev
->ExtScsiInterface
->GetTargetLun (ScsiBusDev
->ExtScsiInterface
, RemainingDevicePath
, &TargetId
, &Lun
);
425 ScsiBusDev
->ScsiInterface
->GetTargetLun (ScsiBusDev
->ScsiInterface
, RemainingDevicePath
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
430 // If RemainingDevicePath is the End of Device Path Node,
431 // skip enumerate any device and return EFI_SUCESSS
433 ScanOtherPuns
= FALSE
;
436 while(ScanOtherPuns
) {
437 if (FromFirstTarget
) {
439 // Remaining Device Path is NULL, scan all the possible Puns in the
442 if (ScsiBusDev
->ExtScsiSupport
) {
443 Status
= ScsiBusDev
->ExtScsiInterface
->GetNextTargetLun (ScsiBusDev
->ExtScsiInterface
, &TargetId
, &Lun
);
445 Status
= ScsiBusDev
->ScsiInterface
->GetNextDevice (ScsiBusDev
->ScsiInterface
, &ScsiTargetId
.ScsiId
.Scsi
, &Lun
);
447 if (EFI_ERROR (Status
)) {
449 // no legal Pun and Lun found any more
454 ScanOtherPuns
= FALSE
;
457 // Avoid creating handle for the host adapter.
459 if (ScsiBusDev
->ExtScsiSupport
) {
460 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ExtScsiInterface
->Mode
->AdapterId
) {
464 if ((ScsiTargetId
.ScsiId
.Scsi
) == ScsiBusDev
->ScsiInterface
->Mode
->AdapterId
) {
469 // Scan for the scsi device, if it attaches to the scsi bus,
470 // then create handle and install scsi i/o protocol.
472 Status
= ScsiScanCreateDevice (This
, Controller
, &ScsiTargetId
, Lun
, ScsiBusDev
);
478 if (ScsiBusDev
!= NULL
) {
479 FreePool (ScsiBusDev
);
482 if (ExtScsiSupport
) {
485 &gEfiExtScsiPassThruProtocolGuid
,
486 This
->DriverBindingHandle
,
489 if (!EFI_ERROR (PassThruStatus
)) {
492 &gEfiScsiPassThruProtocolGuid
,
493 This
->DriverBindingHandle
,
500 &gEfiScsiPassThruProtocolGuid
,
501 This
->DriverBindingHandle
,
509 Stop this driver on ControllerHandle.
511 This service is called by the EFI boot service DisconnectController().
512 In order to make drivers as small as possible, there are a few calling
513 restrictions for this service. DisconnectController() must follow these
514 calling restrictions. If any other agent wishes to call Stop() it must also
515 follow these calling restrictions.
517 @param This Protocol instance pointer.
518 @param ControllerHandle Handle of device to stop driver on
519 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
520 children is zero stop the entire bus driver.
521 @param ChildHandleBuffer List of Child Handles to Stop.
523 @retval EFI_SUCCESS This driver is removed ControllerHandle
524 @retval other This driver was not removed from this device
529 SCSIBusDriverBindingStop (
530 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
531 IN EFI_HANDLE Controller
,
532 IN UINTN NumberOfChildren
,
533 IN EFI_HANDLE
*ChildHandleBuffer
537 BOOLEAN AllChildrenStopped
;
539 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
540 SCSI_IO_DEV
*ScsiIoDevice
;
542 EFI_SCSI_BUS_PROTOCOL
*Scsidentifier
;
543 SCSI_BUS_DEVICE
*ScsiBusDev
;
545 if (NumberOfChildren
== 0) {
547 // Get the SCSI_BUS_DEVICE
549 Status
= gBS
->OpenProtocol (
551 &mScsiBusProtocolGuid
,
552 (VOID
**) &Scsidentifier
,
553 This
->DriverBindingHandle
,
555 EFI_OPEN_PROTOCOL_GET_PROTOCOL
558 if (EFI_ERROR (Status
)) {
559 return EFI_DEVICE_ERROR
;
562 ScsiBusDev
= SCSI_BUS_CONTROLLER_DEVICE_FROM_THIS (Scsidentifier
);
565 // Uninstall SCSI Bus Protocol
567 gBS
->UninstallProtocolInterface (
569 &mScsiBusProtocolGuid
,
570 &ScsiBusDev
->BusIdentify
574 // Close the bus driver
576 if (ScsiBusDev
->ExtScsiSupport
) {
578 // Close ExtPassThru Protocol from this controller handle
582 &gEfiExtScsiPassThruProtocolGuid
,
583 This
->DriverBindingHandle
,
587 // When Start() succeeds to open ExtPassThru, it always tries to open PassThru BY_DRIVER.
588 // Its intent is to prevent another SCSI Bus Driver from woking on the same host handle.
589 // So Stop() needs to try to close PassThru if present here.
593 &gEfiScsiPassThruProtocolGuid
,
594 This
->DriverBindingHandle
,
600 &gEfiScsiPassThruProtocolGuid
,
601 This
->DriverBindingHandle
,
608 &gEfiDevicePathProtocolGuid
,
609 This
->DriverBindingHandle
,
612 FreePool (ScsiBusDev
);
616 AllChildrenStopped
= TRUE
;
618 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
620 Status
= gBS
->OpenProtocol (
621 ChildHandleBuffer
[Index
],
622 &gEfiScsiIoProtocolGuid
,
624 This
->DriverBindingHandle
,
626 EFI_OPEN_PROTOCOL_GET_PROTOCOL
628 if (EFI_ERROR (Status
)) {
629 AllChildrenStopped
= FALSE
;
633 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (ScsiIo
);
635 // Close the child handle
637 if (ScsiIoDevice
->ExtScsiSupport
) {
638 Status
= gBS
->CloseProtocol (
640 &gEfiExtScsiPassThruProtocolGuid
,
641 This
->DriverBindingHandle
,
642 ChildHandleBuffer
[Index
]
646 Status
= gBS
->CloseProtocol (
648 &gEfiScsiPassThruProtocolGuid
,
649 This
->DriverBindingHandle
,
650 ChildHandleBuffer
[Index
]
654 Status
= gBS
->UninstallMultipleProtocolInterfaces (
655 ChildHandleBuffer
[Index
],
656 &gEfiDevicePathProtocolGuid
,
657 ScsiIoDevice
->DevicePath
,
658 &gEfiScsiIoProtocolGuid
,
659 &ScsiIoDevice
->ScsiIo
,
662 if (EFI_ERROR (Status
)) {
663 AllChildrenStopped
= FALSE
;
664 if (ScsiIoDevice
->ExtScsiSupport
) {
667 &gEfiExtScsiPassThruProtocolGuid
,
669 This
->DriverBindingHandle
,
670 ChildHandleBuffer
[Index
],
671 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
676 &gEfiScsiPassThruProtocolGuid
,
678 This
->DriverBindingHandle
,
679 ChildHandleBuffer
[Index
],
680 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
684 FreePool (ScsiIoDevice
);
688 if (!AllChildrenStopped
) {
689 return EFI_DEVICE_ERROR
;
697 Retrieves the device type information of the SCSI Controller.
699 @param This Protocol instance pointer.
700 @param DeviceType A pointer to the device type information retrieved from
703 @retval EFI_SUCCESS Retrieves the device type information successfully.
704 @retval EFI_INVALID_PARAMETER The DeviceType is NULL.
710 IN EFI_SCSI_IO_PROTOCOL
*This
,
711 OUT UINT8
*DeviceType
714 SCSI_IO_DEV
*ScsiIoDevice
;
716 if (DeviceType
== NULL
) {
717 return EFI_INVALID_PARAMETER
;
720 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
721 *DeviceType
= ScsiIoDevice
->ScsiDeviceType
;
727 Retrieves the device location in the SCSI channel.
729 @param This Protocol instance pointer.
730 @param Target A pointer to the Target ID of a SCSI device
732 @param Lun A pointer to the LUN of the SCSI device on
735 @retval EFI_SUCCESS Retrieves the device location successfully.
736 @retval EFI_INVALID_PARAMETER The Target or Lun is NULL.
741 ScsiGetDeviceLocation (
742 IN EFI_SCSI_IO_PROTOCOL
*This
,
743 IN OUT UINT8
**Target
,
747 SCSI_IO_DEV
*ScsiIoDevice
;
749 if (Target
== NULL
|| Lun
== NULL
) {
750 return EFI_INVALID_PARAMETER
;
753 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
755 CopyMem (*Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
757 *Lun
= ScsiIoDevice
->Lun
;
763 Resets the SCSI Bus that the SCSI Controller is attached to.
765 @param This Protocol instance pointer.
767 @retval EFI_SUCCESS The SCSI bus is reset successfully.
768 @retval EFI_DEVICE_ERROR Errors encountered when resetting the SCSI bus.
769 @retval EFI_UNSUPPORTED The bus reset operation is not supported by the
770 SCSI Host Controller.
771 @retval EFI_TIMEOUT A timeout occurred while attempting to reset
777 IN EFI_SCSI_IO_PROTOCOL
*This
780 SCSI_IO_DEV
*ScsiIoDevice
;
782 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
784 if (ScsiIoDevice
->ExtScsiSupport
){
785 return ScsiIoDevice
->ExtScsiPassThru
->ResetChannel (ScsiIoDevice
->ExtScsiPassThru
);
787 return ScsiIoDevice
->ScsiPassThru
->ResetChannel (ScsiIoDevice
->ScsiPassThru
);
793 Resets the SCSI Controller that the device handle specifies.
795 @param This Protocol instance pointer.
797 @retval EFI_SUCCESS Reset the SCSI controller successfully.
798 @retval EFI_DEVICE_ERROR Errors are encountered when resetting the SCSI Controller.
799 @retval EFI_UNSUPPORTED The SCSI bus does not support a device reset operation.
800 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the
806 IN EFI_SCSI_IO_PROTOCOL
*This
809 SCSI_IO_DEV
*ScsiIoDevice
;
810 UINT8 Target
[TARGET_MAX_BYTES
];
812 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
813 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
816 if (ScsiIoDevice
->ExtScsiSupport
) {
817 return ScsiIoDevice
->ExtScsiPassThru
->ResetTargetLun (
818 ScsiIoDevice
->ExtScsiPassThru
,
823 return ScsiIoDevice
->ScsiPassThru
->ResetTarget (
824 ScsiIoDevice
->ScsiPassThru
,
825 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
833 Sends a SCSI Request Packet to the SCSI Controller for execution.
835 @param This Protocol instance pointer.
836 @param CommandPacket The SCSI request packet to send to the SCSI
837 Controller specified by the device handle.
838 @param Event If the SCSI bus where the SCSI device is attached
839 does not support non-blocking I/O, then Event is
840 ignored, and blocking I/O is performed.
841 If Event is NULL, then blocking I/O is performed.
842 If Event is not NULL and non-blocking I/O is
843 supported, then non-blocking I/O is performed,
844 and Event will be signaled when the SCSI Request
847 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host
848 successfully, and TransferLength bytes were
849 transferred to/from DataBuffer.See
850 HostAdapterStatus, TargetStatus,
851 SenseDataLength, and SenseData in that order
852 for additional status information.
853 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was executed,
854 but the entire DataBuffer could not be transferred.
855 The actual number of bytes transferred is returned
856 in TransferLength. See HostAdapterStatus,
857 TargetStatus, SenseDataLength, and SenseData in
858 that order for additional status information.
859 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because
860 there are too many SCSI Command Packets already
861 queued.The caller may retry again later.
862 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send
863 the SCSI Request Packet. See HostAdapterStatus,
864 TargetStatus, SenseDataLength, and SenseData in
865 that order for additional status information.
866 @retval EFI_INVALID_PARAMETER The contents of CommandPacket are invalid.
867 The SCSI Request Packet was not sent, so no
868 additional status information is available.
869 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet
870 is not supported by the SCSI initiator(i.e., SCSI
871 Host Controller). The SCSI Request Packet was not
872 sent, so no additional status information is
874 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI
875 Request Packet to execute. See HostAdapterStatus,
876 TargetStatus, SenseDataLength, and SenseData in
877 that order for additional status information.
881 ScsiExecuteSCSICommand (
882 IN EFI_SCSI_IO_PROTOCOL
*This
,
883 IN OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
884 IN EFI_EVENT Event OPTIONAL
887 SCSI_IO_DEV
*ScsiIoDevice
;
889 UINT8 Target
[TARGET_MAX_BYTES
];
890 EFI_EVENT PacketEvent
;
891 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ExtRequestPacket
;
892 SCSI_EVENT_DATA EventData
;
896 if (Packet
== NULL
) {
897 return EFI_INVALID_PARAMETER
;
900 ScsiIoDevice
= SCSI_IO_DEV_FROM_THIS (This
);
901 CopyMem (Target
,&ScsiIoDevice
->Pun
, TARGET_MAX_BYTES
);
903 if (ScsiIoDevice
->ExtScsiSupport
) {
904 ExtRequestPacket
= (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*) Packet
;
905 Status
= ScsiIoDevice
->ExtScsiPassThru
->PassThru (
906 ScsiIoDevice
->ExtScsiPassThru
,
914 mWorkingBuffer
= AllocatePool (sizeof(EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
916 if (mWorkingBuffer
== NULL
) {
917 return EFI_DEVICE_ERROR
;
921 // Convert package into EFI1.0, EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET.
923 Status
= ScsiioToPassThruPacket(Packet
, (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
);
924 if (EFI_ERROR(Status
)) {
925 FreePool(mWorkingBuffer
);
929 if (((ScsiIoDevice
->ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
) != 0) && (Event
!= NULL
)) {
930 EventData
.Data1
= (VOID
*)Packet
;
931 EventData
.Data2
= Event
;
935 Status
= gBS
->CreateEvent (
942 if (EFI_ERROR(Status
)) {
943 FreePool(mWorkingBuffer
);
947 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
948 ScsiIoDevice
->ScsiPassThru
,
949 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
955 if (EFI_ERROR(Status
)) {
956 FreePool(mWorkingBuffer
);
957 gBS
->CloseEvent(PacketEvent
);
963 // If there's no event or SCSI Device doesn't support NON-BLOCKING, just convert
964 // EFI1.0 PassThru packet back to UEFI2.0 SCSI IO Packet.
966 Status
= ScsiIoDevice
->ScsiPassThru
->PassThru (
967 ScsiIoDevice
->ScsiPassThru
,
968 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
973 if (EFI_ERROR(Status
)) {
974 FreePool(mWorkingBuffer
);
978 PassThruToScsiioPacket((EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
,Packet
);
980 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
981 // free mWorkingBuffer.
983 FreePool(mWorkingBuffer
);
991 Scan SCSI Bus to discover the device, and attach ScsiIoProtocol to it.
993 @param This Protocol instance pointer
994 @param Controller Controller handle
995 @param TargetId Tartget to be scanned
996 @param Lun The Lun of the SCSI device on the SCSI channel.
997 @param ScsiBusDev The pointer of SCSI_BUS_DEVICE
999 @retval EFI_SUCCESS Successfully to discover the device and attach
1000 ScsiIoProtocol to it.
1001 @retval EFI_OUT_OF_RESOURCES Fail to discover the device.
1006 ScsiScanCreateDevice (
1007 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1008 IN EFI_HANDLE Controller
,
1009 IN SCSI_TARGET_ID
*TargetId
,
1011 IN OUT SCSI_BUS_DEVICE
*ScsiBusDev
1015 SCSI_IO_DEV
*ScsiIoDevice
;
1016 EFI_DEVICE_PATH_PROTOCOL
*ScsiDevicePath
;
1018 ScsiIoDevice
= AllocateZeroPool (sizeof (SCSI_IO_DEV
));
1019 if (ScsiIoDevice
== NULL
) {
1020 return EFI_OUT_OF_RESOURCES
;
1023 ScsiIoDevice
->Signature
= SCSI_IO_DEV_SIGNATURE
;
1024 CopyMem(&ScsiIoDevice
->Pun
, TargetId
, TARGET_MAX_BYTES
);
1025 ScsiIoDevice
->Lun
= Lun
;
1027 if (ScsiBusDev
->ExtScsiSupport
) {
1028 ScsiIoDevice
->ExtScsiPassThru
= ScsiBusDev
->ExtScsiInterface
;
1029 ScsiIoDevice
->ExtScsiSupport
= TRUE
;
1030 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ExtScsiPassThru
->Mode
->IoAlign
;
1033 ScsiIoDevice
->ScsiPassThru
= ScsiBusDev
->ScsiInterface
;
1034 ScsiIoDevice
->ExtScsiSupport
= FALSE
;
1035 ScsiIoDevice
->ScsiIo
.IoAlign
= ScsiIoDevice
->ScsiPassThru
->Mode
->IoAlign
;
1038 ScsiIoDevice
->ScsiIo
.GetDeviceType
= ScsiGetDeviceType
;
1039 ScsiIoDevice
->ScsiIo
.GetDeviceLocation
= ScsiGetDeviceLocation
;
1040 ScsiIoDevice
->ScsiIo
.ResetBus
= ScsiResetBus
;
1041 ScsiIoDevice
->ScsiIo
.ResetDevice
= ScsiResetDevice
;
1042 ScsiIoDevice
->ScsiIo
.ExecuteScsiCommand
= ScsiExecuteSCSICommand
;
1045 if (!DiscoverScsiDevice (ScsiIoDevice
)) {
1046 FreePool (ScsiIoDevice
);
1047 return EFI_OUT_OF_RESOURCES
;
1053 ScsiDevicePath
= NULL
;
1054 if (ScsiIoDevice
->ExtScsiSupport
){
1055 Status
= ScsiIoDevice
->ExtScsiPassThru
->BuildDevicePath (
1056 ScsiIoDevice
->ExtScsiPassThru
,
1057 &ScsiIoDevice
->Pun
.ScsiId
.ExtScsi
[0],
1062 Status
= ScsiIoDevice
->ScsiPassThru
->BuildDevicePath (
1063 ScsiIoDevice
->ScsiPassThru
,
1064 ScsiIoDevice
->Pun
.ScsiId
.Scsi
,
1070 if (EFI_ERROR(Status
)) {
1071 FreePool (ScsiIoDevice
);
1075 ScsiIoDevice
->DevicePath
= AppendDevicePathNode (
1076 ScsiBusDev
->DevicePath
,
1080 // The memory space for ScsiDevicePath is allocated in
1081 // ScsiPassThru->BuildDevicePath() function; It is no longer used
1082 // after EfiAppendDevicePathNode,so free the memory it occupies.
1084 FreePool (ScsiDevicePath
);
1086 if (ScsiIoDevice
->DevicePath
== NULL
) {
1087 FreePool (ScsiIoDevice
);
1088 return EFI_OUT_OF_RESOURCES
;
1091 Status
= gBS
->InstallMultipleProtocolInterfaces (
1092 &ScsiIoDevice
->Handle
,
1093 &gEfiDevicePathProtocolGuid
,
1094 ScsiIoDevice
->DevicePath
,
1095 &gEfiScsiIoProtocolGuid
,
1096 &ScsiIoDevice
->ScsiIo
,
1099 if (EFI_ERROR (Status
)) {
1100 FreePool (ScsiIoDevice
->DevicePath
);
1101 FreePool (ScsiIoDevice
);
1102 return EFI_OUT_OF_RESOURCES
;
1104 if (ScsiBusDev
->ExtScsiSupport
) {
1107 &gEfiExtScsiPassThruProtocolGuid
,
1108 (VOID
**) &(ScsiBusDev
->ExtScsiInterface
),
1109 This
->DriverBindingHandle
,
1110 ScsiIoDevice
->Handle
,
1111 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1116 &gEfiScsiPassThruProtocolGuid
,
1117 (VOID
**) &(ScsiBusDev
->ScsiInterface
),
1118 This
->DriverBindingHandle
,
1119 ScsiIoDevice
->Handle
,
1120 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1129 Discovery SCSI Device
1131 @param ScsiIoDevice The pointer of SCSI_IO_DEV
1133 @retval TRUE Find SCSI Device and verify it.
1134 @retval FALSE Unable to find SCSI Device.
1138 DiscoverScsiDevice (
1139 IN OUT SCSI_IO_DEV
*ScsiIoDevice
1143 UINT32 InquiryDataLength
;
1144 UINT8 SenseDataLength
;
1145 UINT8 HostAdapterStatus
;
1147 EFI_SCSI_SENSE_DATA SenseData
;
1148 EFI_SCSI_INQUIRY_DATA InquiryData
;
1150 HostAdapterStatus
= 0;
1153 // Using Inquiry command to scan for the device
1155 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
1156 SenseDataLength
= sizeof (EFI_SCSI_SENSE_DATA
);
1158 Status
= ScsiInquiryCommand (
1159 &ScsiIoDevice
->ScsiIo
,
1160 EFI_TIMER_PERIOD_SECONDS (1),
1161 (VOID
*) &SenseData
,
1165 (VOID
*) &InquiryData
,
1169 if (EFI_ERROR (Status
) && Status
!= EFI_BAD_BUFFER_SIZE
) {
1173 // Retrieved inquiry data successfully
1175 if ((InquiryData
.Peripheral_Qualifier
!= 0) &&
1176 (InquiryData
.Peripheral_Qualifier
!= 3)) {
1180 if (InquiryData
.Peripheral_Qualifier
== 3) {
1181 if (InquiryData
.Peripheral_Type
!= 0x1f) {
1186 if (0x1e >= InquiryData
.Peripheral_Type
&& InquiryData
.Peripheral_Type
>= 0xa) {
1191 // valid device type and peripheral qualifier combination.
1193 ScsiIoDevice
->ScsiDeviceType
= InquiryData
.Peripheral_Type
;
1194 ScsiIoDevice
->RemovableDevice
= InquiryData
.Rmb
;
1195 if (InquiryData
.Version
== 0) {
1196 ScsiIoDevice
->ScsiVersion
= 0;
1199 // ANSI-approved version
1201 ScsiIoDevice
->ScsiVersion
= (UINT8
) (InquiryData
.Version
& 0x03);
1209 Convert EFI_SCSI_IO_SCSI_REQUEST_PACKET packet to EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet.
1211 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1212 @param CommandPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1217 ScsiioToPassThruPacket (
1218 IN EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
,
1219 OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*CommandPacket
1223 //EFI 1.10 doesn't support Bi-Direction Command.
1225 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_BIDIRECTIONAL
) {
1226 return EFI_UNSUPPORTED
;
1229 ZeroMem (CommandPacket
, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1231 CommandPacket
->Timeout
= Packet
->Timeout
;
1232 CommandPacket
->Cdb
= Packet
->Cdb
;
1233 CommandPacket
->CdbLength
= Packet
->CdbLength
;
1234 CommandPacket
->DataDirection
= Packet
->DataDirection
;
1235 CommandPacket
->HostAdapterStatus
= Packet
->HostAdapterStatus
;
1236 CommandPacket
->TargetStatus
= Packet
->TargetStatus
;
1237 CommandPacket
->SenseData
= Packet
->SenseData
;
1238 CommandPacket
->SenseDataLength
= Packet
->SenseDataLength
;
1240 if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1241 CommandPacket
->DataBuffer
= Packet
->InDataBuffer
;
1242 CommandPacket
->TransferLength
= Packet
->InTransferLength
;
1243 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1244 CommandPacket
->DataBuffer
= Packet
->OutDataBuffer
;
1245 CommandPacket
->TransferLength
= Packet
->OutTransferLength
;
1252 Convert EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET packet to EFI_SCSI_IO_SCSI_REQUEST_PACKET packet.
1254 @param ScsiPacket The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1255 @param Packet The pointer of EFI_SCSI_IO_SCSI_REQUEST_PACKET
1260 PassThruToScsiioPacket (
1261 IN EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
,
1262 OUT EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
1265 Packet
->Timeout
= ScsiPacket
->Timeout
;
1266 Packet
->Cdb
= ScsiPacket
->Cdb
;
1267 Packet
->CdbLength
= ScsiPacket
->CdbLength
;
1268 Packet
->DataDirection
= ScsiPacket
->DataDirection
;
1269 Packet
->HostAdapterStatus
= ScsiPacket
->HostAdapterStatus
;
1270 Packet
->TargetStatus
= ScsiPacket
->TargetStatus
;
1271 Packet
->SenseData
= ScsiPacket
->SenseData
;
1272 Packet
->SenseDataLength
= ScsiPacket
->SenseDataLength
;
1274 if (ScsiPacket
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_READ
) {
1275 Packet
->InDataBuffer
= ScsiPacket
->DataBuffer
;
1276 Packet
->InTransferLength
= ScsiPacket
->TransferLength
;
1277 } else if (Packet
->DataDirection
== EFI_SCSI_IO_DATA_DIRECTION_WRITE
) {
1278 Packet
->OutDataBuffer
= ScsiPacket
->DataBuffer
;
1279 Packet
->OutTransferLength
= ScsiPacket
->TransferLength
;
1286 Notify Function in which convert EFI1.0 PassThru Packet back to UEF2.0
1289 @param Event The instance of EFI_EVENT.
1290 @param Context The parameter passed in.
1300 EFI_SCSI_IO_SCSI_REQUEST_PACKET
*Packet
;
1301 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*ScsiPacket
;
1302 EFI_EVENT CallerEvent
;
1303 SCSI_EVENT_DATA
*PassData
;
1305 PassData
= (SCSI_EVENT_DATA
*)Context
;
1306 Packet
= (EFI_SCSI_IO_SCSI_REQUEST_PACKET
*)PassData
->Data1
;
1307 ScsiPacket
= (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*)mWorkingBuffer
;
1310 // Convert EFI1.0 PassThru packet to UEFI2.0 SCSI IO Packet.
1312 PassThruToScsiioPacket(ScsiPacket
, Packet
);
1315 // After converting EFI1.0 PassThru Packet back to UEFI2.0 SCSI IO Packet,
1316 // free mWorkingBuffer.
1318 gBS
->FreePool(mWorkingBuffer
);
1321 // Signal Event to tell caller to pick up UEFI2.0 SCSI IO Packet.
1323 CallerEvent
= PassData
->Data2
;
1324 gBS
->CloseEvent(Event
);
1325 gBS
->SignalEvent(CallerEvent
);