2 This file implements ATA_PASSTHRU_PROCTOCOL and EXT_SCSI_PASSTHRU_PROTOCOL interfaces
3 for managed ATA controllers.
5 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "AtaAtapiPassThru.h"
19 // EFI_DRIVER_BINDING_PROTOCOL instance
21 EFI_DRIVER_BINDING_PROTOCOL gAtaAtapiPassThruDriverBinding
= {
22 AtaAtapiPassThruSupported
,
23 AtaAtapiPassThruStart
,
30 ATA_ATAPI_PASS_THRU_INSTANCE gAtaAtapiPassThruInstanceTemplate
= {
31 ATA_ATAPI_PASS_THRU_SIGNATURE
,
32 0, // Controller Handle
33 NULL
, // PciIo Protocol
34 NULL
, // IdeControllerInit Protocol
37 // According to UEFI2.3 spec Section 12.10, Drivers for non-RAID ATA controllers should set
38 // both EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL and EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL
40 // Note that the driver doesn't support AtaPassThru non blocking I/O.
42 EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL
| EFI_ATA_PASS_THRU_ATTRIBUTES_NONBLOCKIO
,
51 AtaPassThruGetNextPort
,
52 AtaPassThruGetNextDevice
,
53 AtaPassThruBuildDevicePath
,
56 AtaPassThruResetDevice
58 { // ExtScsiPassThruMode
64 // According to UEFI2.3 spec Section 14.7, Drivers for non-RAID SCSI controllers should set
65 // both EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL and EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
67 // Note that the driver doesn't support ExtScsiPassThru non blocking I/O.
69 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
,
77 ExtScsiPassThruPassThru
,
78 ExtScsiPassThruGetNextTargetLun
,
79 ExtScsiPassThruBuildDevicePath
,
80 ExtScsiPassThruGetTargetLun
,
81 ExtScsiPassThruResetChannel
,
82 ExtScsiPassThruResetTargetLun
,
83 ExtScsiPassThruGetNextTarget
85 EfiAtaUnknownMode
, // Work Mode
97 0, // EnabledPciAttributes
98 0, // OriginalAttributes
100 0, // PreviousPortMultiplier
101 0, // PreviousTargetId
104 { // NonBlocking TaskList
108 NULL
, // ExitBootEvent
111 ATAPI_DEVICE_PATH mAtapiDevicePathTemplate
= {
113 MESSAGING_DEVICE_PATH
,
116 (UINT8
) (sizeof (ATAPI_DEVICE_PATH
)),
117 (UINT8
) ((sizeof (ATAPI_DEVICE_PATH
)) >> 8)
125 SATA_DEVICE_PATH mSataDevicePathTemplate
= {
127 MESSAGING_DEVICE_PATH
,
130 (UINT8
) (sizeof (SATA_DEVICE_PATH
)),
131 (UINT8
) ((sizeof (SATA_DEVICE_PATH
)) >> 8)
139 UINT8 mScsiId
[TARGET_MAX_BYTES
] = {
140 0xFF, 0xFF, 0xFF, 0xFF,
141 0xFF, 0xFF, 0xFF, 0xFF,
142 0xFF, 0xFF, 0xFF, 0xFF,
143 0xFF, 0xFF, 0xFF, 0xFF
147 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
148 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
149 and the non-blocking I/O functionality is optional.
151 @param[in] Port The port number of the ATA device to send the command.
152 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
153 If there is no port multiplier, then specify 0xFFFF.
154 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
155 and PortMultiplierPort.
156 @param[in] Instance Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE.
157 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
158 used by non-blocking mode.
160 @retval EFI_SUCCESS The ATA command was sent by the host. For
161 bi-directional commands, InTransferLength bytes
162 were transferred from InDataBuffer. For
163 write and bi-directional commands, OutTransferLength
164 bytes were transferred by OutDataBuffer.
165 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
166 of bytes that could be transferred is returned
167 in InTransferLength. For write and bi-directional
168 commands, OutTransferLength bytes were transferred
170 @retval EFI_NOT_READY The ATA command could not be sent because
171 there are too many ATA commands already
172 queued. The caller may retry again later.
173 @retval EFI_DEVICE_ERROR A device error occurred while attempting
174 to send the ATA command.
175 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents
176 of Acb are invalid. The ATA command was
177 not sent, so no additional status information
183 AtaPassThruPassThruExecute (
185 IN UINT16 PortMultiplierPort
,
186 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
187 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
188 IN ATA_NONBLOCK_TASK
*Task OPTIONAL
191 EFI_ATA_PASS_THRU_CMD_PROTOCOL Protocol
;
192 EFI_ATA_HC_WORK_MODE Mode
;
195 Protocol
= Packet
->Protocol
;
197 Mode
= Instance
->Mode
;
201 // Reassign IDE mode io port registers' base addresses
203 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
205 if (EFI_ERROR (Status
)) {
210 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
211 Status
= AtaNonDataCommandIn (
213 &Instance
->IdeRegisters
[Port
],
220 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
221 Status
= AtaPioDataInOut (
223 &Instance
->IdeRegisters
[Port
],
224 Packet
->InDataBuffer
,
225 Packet
->InTransferLength
,
233 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
234 Status
= AtaPioDataInOut (
236 &Instance
->IdeRegisters
[Port
],
237 Packet
->OutDataBuffer
,
238 Packet
->OutTransferLength
,
246 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
247 Status
= AtaUdmaInOut (
249 &Instance
->IdeRegisters
[Port
],
251 Packet
->InDataBuffer
,
252 Packet
->InTransferLength
,
259 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
260 Status
= AtaUdmaInOut (
262 &Instance
->IdeRegisters
[Port
],
264 Packet
->OutDataBuffer
,
265 Packet
->OutTransferLength
,
273 return EFI_UNSUPPORTED
;
276 case EfiAtaAhciMode
:
277 if (PortMultiplierPort
== 0xFFFF) {
279 // If there is no port multiplier, PortMultiplierPort will be 0xFFFF
280 // according to UEFI spec. Here, we convert its value to 0 to follow
283 PortMultiplierPort
= 0;
286 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
287 Status
= AhciNonDataTransfer (
289 &Instance
->AhciRegisters
,
291 (UINT8
)PortMultiplierPort
,
300 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
301 Status
= AhciPioTransfer (
303 &Instance
->AhciRegisters
,
305 (UINT8
)PortMultiplierPort
,
311 Packet
->InDataBuffer
,
312 Packet
->InTransferLength
,
317 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
318 Status
= AhciPioTransfer (
320 &Instance
->AhciRegisters
,
322 (UINT8
)PortMultiplierPort
,
328 Packet
->OutDataBuffer
,
329 Packet
->OutTransferLength
,
334 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
335 Status
= AhciDmaTransfer (
337 &Instance
->AhciRegisters
,
339 (UINT8
)PortMultiplierPort
,
345 Packet
->InDataBuffer
,
346 Packet
->InTransferLength
,
351 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
352 Status
= AhciDmaTransfer (
354 &Instance
->AhciRegisters
,
356 (UINT8
)PortMultiplierPort
,
362 Packet
->OutDataBuffer
,
363 Packet
->OutTransferLength
,
369 return EFI_UNSUPPORTED
;
374 Status
= EFI_DEVICE_ERROR
;
382 Call back function when the timer event is signaled.
384 @param[in] Event The Event this notify function registered to.
385 @param[in] Context Pointer to the context data registered to the
391 AsyncNonBlockingTransferRoutine (
397 LIST_ENTRY
*EntryHeader
;
398 ATA_NONBLOCK_TASK
*Task
;
400 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
402 Instance
= (ATA_ATAPI_PASS_THRU_INSTANCE
*) Context
;
403 EntryHeader
= &Instance
->NonBlockingTaskList
;
405 // Get the Taks from the Taks List and execute it, until there is
406 // no task in the list or the device is busy with task (EFI_NOT_READY).
409 if (!IsListEmpty (EntryHeader
)) {
410 Entry
= GetFirstNode (EntryHeader
);
411 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (Entry
);
416 Status
= AtaPassThruPassThruExecute (
418 Task
->PortMultiplier
,
425 // If the data transfer meet a error, remove all tasks in the list since these tasks are
426 // associated with one task from Ata Bus and signal the event with error status.
428 if ((Status
!= EFI_NOT_READY
) && (Status
!= EFI_SUCCESS
)) {
429 DestroyAsynTaskList (Instance
, TRUE
);
434 // For Non blocking mode, the Status of EFI_NOT_READY means the operation
435 // is not finished yet. Otherwise the operation is successful.
437 if (Status
== EFI_NOT_READY
) {
440 RemoveEntryList (&Task
->Link
);
441 gBS
->SignalEvent (Task
->Event
);
448 The Entry Point of module.
450 @param[in] ImageHandle The firmware allocated handle for the EFI image.
451 @param[in] SystemTable A pointer to the EFI System Table.
453 @retval EFI_SUCCESS The entry point is executed successfully.
454 @retval other Some error occurs when executing this entry point.
459 InitializeAtaAtapiPassThru (
460 IN EFI_HANDLE ImageHandle
,
461 IN EFI_SYSTEM_TABLE
*SystemTable
467 // Install driver model protocol(s).
469 Status
= EfiLibInstallDriverBindingComponentName2 (
472 &gAtaAtapiPassThruDriverBinding
,
474 &gAtaAtapiPassThruComponentName
,
475 &gAtaAtapiPassThruComponentName2
477 ASSERT_EFI_ERROR (Status
);
483 Disable the device (especially Bus Master DMA) when exiting the boot
486 @param[in] Event Event for which this notification function is being
488 @param[in] Context Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE that
493 AtaPassThruExitBootServices (
498 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
499 EFI_PCI_IO_PROTOCOL
*PciIo
;
501 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
504 PciIo
= Instance
->PciIo
;
508 EfiPciIoAttributeOperationDisable
,
509 Instance
->EnabledPciAttributes
,
515 Tests to see if this driver supports a given controller. If a child device is provided,
516 it further tests to see if this driver supports creating a handle for the specified child device.
518 This function checks to see if the driver specified by This supports the device specified by
519 ControllerHandle. Drivers will typically use the device path attached to
520 ControllerHandle and/or the services from the bus I/O abstraction attached to
521 ControllerHandle to determine if the driver supports ControllerHandle. This function
522 may be called many times during platform initialization. In order to reduce boot times, the tests
523 performed by this function must be very small, and take as little time as possible to execute. This
524 function must not change the state of any hardware devices, and this function must be aware that the
525 device specified by ControllerHandle may already be managed by the same driver or a
526 different driver. This function must match its calls to AllocatePages() with FreePages(),
527 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
528 Because ControllerHandle may have been previously started by the same driver, if a protocol is
529 already in the opened state, then it must not be closed with CloseProtocol(). This is required
530 to guarantee the state of ControllerHandle is not modified by this function.
532 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
533 @param[in] ControllerHandle The handle of the controller to test. This handle
534 must support a protocol interface that supplies
535 an I/O abstraction to the driver.
536 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
537 parameter is ignored by device drivers, and is optional for bus
538 drivers. For bus drivers, if this parameter is not NULL, then
539 the bus driver must determine if the bus controller specified
540 by ControllerHandle and the child controller specified
541 by RemainingDevicePath are both supported by this
544 @retval EFI_SUCCESS The device specified by ControllerHandle and
545 RemainingDevicePath is supported by the driver specified by This.
546 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
547 RemainingDevicePath is already being managed by the driver
549 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
550 RemainingDevicePath is already being managed by a different
551 driver or an application that requires exclusive access.
552 Currently not implemented.
553 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
554 RemainingDevicePath is not supported by the driver specified by This.
558 AtaAtapiPassThruSupported (
559 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
560 IN EFI_HANDLE Controller
,
561 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
565 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
566 EFI_PCI_IO_PROTOCOL
*PciIo
;
568 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
571 // SATA Controller is a device driver, and should ingore the
572 // "RemainingDevicePath" according to UEFI spec
574 Status
= gBS
->OpenProtocol (
576 &gEfiDevicePathProtocolGuid
,
577 (VOID
*) &ParentDevicePath
,
578 This
->DriverBindingHandle
,
580 EFI_OPEN_PROTOCOL_BY_DRIVER
582 if (EFI_ERROR (Status
)) {
584 // EFI_ALREADY_STARTED is also an error
589 // Close the protocol because we don't use it here
593 &gEfiDevicePathProtocolGuid
,
594 This
->DriverBindingHandle
,
598 Status
= gBS
->OpenProtocol (
600 &gEfiIdeControllerInitProtocolGuid
,
601 (VOID
**) &IdeControllerInit
,
602 This
->DriverBindingHandle
,
604 EFI_OPEN_PROTOCOL_BY_DRIVER
607 if (EFI_ERROR (Status
)) {
609 // EFI_ALREADY_STARTED is also an error
615 // Close the I/O Abstraction(s) used to perform the supported test
619 &gEfiIdeControllerInitProtocolGuid
,
620 This
->DriverBindingHandle
,
625 // Now test the EfiPciIoProtocol
627 Status
= gBS
->OpenProtocol (
629 &gEfiPciIoProtocolGuid
,
631 This
->DriverBindingHandle
,
633 EFI_OPEN_PROTOCOL_GET_PROTOCOL
635 if (EFI_ERROR (Status
)) {
639 // Now further check the PCI header: Base class (offset 0x0B) and
640 // Sub Class (offset 0x0A). This controller should be an ATA controller
642 Status
= PciIo
->Pci
.Read (
645 PCI_CLASSCODE_OFFSET
,
646 sizeof (PciData
.Hdr
.ClassCode
),
647 PciData
.Hdr
.ClassCode
649 if (EFI_ERROR (Status
)) {
650 return EFI_UNSUPPORTED
;
653 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
657 return EFI_UNSUPPORTED
;
661 Starts a device controller or a bus controller.
663 The Start() function is designed to be invoked from the EFI boot service ConnectController().
664 As a result, much of the error checking on the parameters to Start() has been moved into this
665 common boot service. It is legal to call Start() from other locations,
666 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
667 1. ControllerHandle must be a valid EFI_HANDLE.
668 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
669 EFI_DEVICE_PATH_PROTOCOL.
670 3. Prior to calling Start(), the Supported() function for the driver specified by This must
671 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
673 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
674 @param[in] ControllerHandle The handle of the controller to start. This handle
675 must support a protocol interface that supplies
676 an I/O abstraction to the driver.
677 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
678 parameter is ignored by device drivers, and is optional for bus
679 drivers. For a bus driver, if this parameter is NULL, then handles
680 for all the children of Controller are created by this driver.
681 If this parameter is not NULL and the first Device Path Node is
682 not the End of Device Path Node, then only the handle for the
683 child device specified by the first Device Path Node of
684 RemainingDevicePath is created by this driver.
685 If the first Device Path Node of RemainingDevicePath is
686 the End of Device Path Node, no child handle is created by this
689 @retval EFI_SUCCESS The device was started.
690 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
691 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
692 @retval Others The driver failded to start the device.
697 AtaAtapiPassThruStart (
698 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
699 IN EFI_HANDLE Controller
,
700 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
704 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
705 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
706 EFI_PCI_IO_PROTOCOL
*PciIo
;
707 UINT64 EnabledPciAttributes
;
708 UINT64 OriginalPciAttributes
;
710 Status
= EFI_SUCCESS
;
711 IdeControllerInit
= NULL
;
713 OriginalPciAttributes
= 0;
715 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Start== Controller = %x\n", Controller
));
717 Status
= gBS
->OpenProtocol (
719 &gEfiIdeControllerInitProtocolGuid
,
720 (VOID
**) &IdeControllerInit
,
721 This
->DriverBindingHandle
,
723 EFI_OPEN_PROTOCOL_BY_DRIVER
726 if (EFI_ERROR (Status
)) {
727 DEBUG ((EFI_D_ERROR
, "Open Ide_Controller_Init Error, Status=%r", Status
));
731 Status
= gBS
->OpenProtocol (
733 &gEfiPciIoProtocolGuid
,
735 This
->DriverBindingHandle
,
737 EFI_OPEN_PROTOCOL_GET_PROTOCOL
739 if (EFI_ERROR (Status
)) {
740 DEBUG ((EFI_D_ERROR
, "Get Pci_Io Protocol Error, Status=%r", Status
));
744 Status
= PciIo
->Attributes (
746 EfiPciIoAttributeOperationGet
,
748 &OriginalPciAttributes
751 if (EFI_ERROR (Status
)) {
755 Status
= PciIo
->Attributes (
757 EfiPciIoAttributeOperationSupported
,
759 &EnabledPciAttributes
761 if (!EFI_ERROR (Status
)) {
762 EnabledPciAttributes
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
763 Status
= PciIo
->Attributes (
765 EfiPciIoAttributeOperationEnable
,
766 EnabledPciAttributes
,
771 if (EFI_ERROR (Status
)) {
776 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
778 Instance
= AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE
), &gAtaAtapiPassThruInstanceTemplate
);
779 if (Instance
== NULL
) {
783 Instance
->ControllerHandle
= Controller
;
784 Instance
->IdeControllerInit
= IdeControllerInit
;
785 Instance
->PciIo
= PciIo
;
786 Instance
->EnabledPciAttributes
= EnabledPciAttributes
;
787 Instance
->OriginalPciAttributes
= OriginalPciAttributes
;
788 Instance
->AtaPassThru
.Mode
= &Instance
->AtaPassThruMode
;
789 Instance
->ExtScsiPassThru
.Mode
= &Instance
->ExtScsiPassThruMode
;
790 InitializeListHead(&Instance
->DeviceList
);
791 InitializeListHead(&Instance
->NonBlockingTaskList
);
793 Status
= gBS
->CreateEvent (
794 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
796 AtaPassThruExitBootServices
,
798 &Instance
->ExitBootEvent
800 if (EFI_ERROR (Status
)) {
804 Instance
->TimerEvent
= NULL
;
806 Status
= gBS
->CreateEvent (
807 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
809 AsyncNonBlockingTransferRoutine
,
811 &Instance
->TimerEvent
813 if (EFI_ERROR (Status
)) {
820 Status
= gBS
->SetTimer (Instance
->TimerEvent
, TimerPeriodic
, 10000);
821 if (EFI_ERROR (Status
)) {
826 // Enumerate all inserted ATA devices.
828 Status
= EnumerateAttachedDevice (Instance
);
829 if (EFI_ERROR (Status
)) {
833 Status
= gBS
->InstallMultipleProtocolInterfaces (
835 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
836 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
839 ASSERT_EFI_ERROR (Status
);
844 if (IdeControllerInit
!= NULL
) {
847 &gEfiIdeControllerInitProtocolGuid
,
848 This
->DriverBindingHandle
,
853 if ((Instance
!= NULL
) && (Instance
->TimerEvent
!= NULL
)) {
854 gBS
->CloseEvent (Instance
->TimerEvent
);
857 if ((Instance
!= NULL
) && (Instance
->ExitBootEvent
!= NULL
)) {
858 gBS
->CloseEvent (Instance
->ExitBootEvent
);
862 // Remove all inserted ATA devices.
864 DestroyDeviceInfoList(Instance
);
866 if (Instance
!= NULL
) {
869 return EFI_UNSUPPORTED
;
873 Stops a device controller or a bus controller.
875 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
876 As a result, much of the error checking on the parameters to Stop() has been moved
877 into this common boot service. It is legal to call Stop() from other locations,
878 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
879 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
880 same driver's Start() function.
881 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
882 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
883 Start() function, and the Start() function must have called OpenProtocol() on
884 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
886 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
887 @param[in] ControllerHandle A handle to the device being stopped. The handle must
888 support a bus specific I/O protocol for the driver
889 to use to stop the device.
890 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
891 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
892 if NumberOfChildren is 0.
894 @retval EFI_SUCCESS The device was stopped.
895 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
900 AtaAtapiPassThruStop (
901 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
902 IN EFI_HANDLE Controller
,
903 IN UINTN NumberOfChildren
,
904 IN EFI_HANDLE
*ChildHandleBuffer
908 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
909 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
910 EFI_PCI_IO_PROTOCOL
*PciIo
;
911 EFI_AHCI_REGISTERS
*AhciRegisters
;
913 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller
));
915 Status
= gBS
->OpenProtocol (
917 &gEfiAtaPassThruProtocolGuid
,
918 (VOID
**) &AtaPassThru
,
919 This
->DriverBindingHandle
,
921 EFI_OPEN_PROTOCOL_GET_PROTOCOL
924 if (EFI_ERROR (Status
)) {
925 return EFI_DEVICE_ERROR
;
928 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru
);
930 Status
= gBS
->UninstallMultipleProtocolInterfaces (
932 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
933 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
937 if (EFI_ERROR (Status
)) {
938 return EFI_DEVICE_ERROR
;
942 // Close protocols opened by AtaAtapiPassThru controller driver
946 &gEfiIdeControllerInitProtocolGuid
,
947 This
->DriverBindingHandle
,
952 // Close Non-Blocking timer and free Task list.
954 if (Instance
->TimerEvent
!= NULL
) {
955 gBS
->CloseEvent (Instance
->TimerEvent
);
956 Instance
->TimerEvent
= NULL
;
958 DestroyAsynTaskList (Instance
, FALSE
);
961 // Close event signaled at gBS->ExitBootServices().
963 if (Instance
->ExitBootEvent
!= NULL
) {
964 gBS
->CloseEvent (Instance
->ExitBootEvent
);
965 Instance
->ExitBootEvent
= NULL
;
969 // Free allocated resource
971 DestroyDeviceInfoList (Instance
);
973 PciIo
= Instance
->PciIo
;
976 // Disable this ATA host controller.
980 EfiPciIoAttributeOperationDisable
,
981 Instance
->EnabledPciAttributes
,
986 // If the current working mode is AHCI mode, then pre-allocated resource
987 // for AHCI initialization should be released.
989 if (Instance
->Mode
== EfiAtaAhciMode
) {
990 AhciRegisters
= &Instance
->AhciRegisters
;
993 AhciRegisters
->MapCommandTable
997 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandTableSize
),
998 AhciRegisters
->AhciCommandTable
1002 AhciRegisters
->MapCmdList
1006 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandListSize
),
1007 AhciRegisters
->AhciCmdList
1011 AhciRegisters
->MapRFis
1015 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxReceiveFisSize
),
1016 AhciRegisters
->AhciRFis
1021 // Restore original PCI attributes
1023 Status
= PciIo
->Attributes (
1025 EfiPciIoAttributeOperationSet
,
1026 Instance
->OriginalPciAttributes
,
1029 ASSERT_EFI_ERROR (Status
);
1031 FreePool (Instance
);
1037 Traverse the attached ATA devices list to find out the device to access.
1039 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1040 @param[in] Port The port number of the ATA device to send the command.
1041 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1042 If there is no port multiplier, then specify 0xFFFF.
1043 @param[in] DeviceType The device type of the ATA device.
1045 @retval The pointer to the data structure of the device info to access.
1050 SearchDeviceInfoList (
1051 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1053 IN UINT16 PortMultiplier
,
1054 IN EFI_ATA_DEVICE_TYPE DeviceType
1057 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1060 Node
= GetFirstNode (&Instance
->DeviceList
);
1061 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1062 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1065 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
1066 // the PortMultiplier information. If the CD-ROM is directly attached
1067 // on a SATA port, the PortMultiplier should be translated from 0xFF
1068 // to 0xFFFF according to the UEFI spec.
1070 if ((Instance
->Mode
== EfiAtaAhciMode
) &&
1071 (DeviceInfo
->Type
== EfiIdeCdrom
) &&
1072 (PortMultiplier
== 0xFF)) {
1073 PortMultiplier
= 0xFFFF;
1076 if ((DeviceInfo
->Type
== DeviceType
) &&
1077 (Port
== DeviceInfo
->Port
) &&
1078 (PortMultiplier
== DeviceInfo
->PortMultiplier
)) {
1082 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1089 Allocate device info data structure to contain device info.
1090 And insert the data structure to the tail of device list for tracing.
1092 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1093 @param[in] Port The port number of the ATA device to send the command.
1094 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1095 If there is no port multiplier, then specify 0xFFFF.
1096 @param[in] DeviceType The device type of the ATA device.
1097 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1099 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1100 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1105 CreateNewDeviceInfo (
1106 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1108 IN UINT16 PortMultiplier
,
1109 IN EFI_ATA_DEVICE_TYPE DeviceType
,
1110 IN EFI_IDENTIFY_DATA
*IdentifyData
1113 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1115 DeviceInfo
= AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO
));
1117 if (DeviceInfo
== NULL
) {
1118 return EFI_OUT_OF_RESOURCES
;
1121 DeviceInfo
->Signature
= ATA_ATAPI_DEVICE_SIGNATURE
;
1122 DeviceInfo
->Port
= Port
;
1123 DeviceInfo
->PortMultiplier
= PortMultiplier
;
1124 DeviceInfo
->Type
= DeviceType
;
1126 if (IdentifyData
!= NULL
) {
1127 DeviceInfo
->IdentifyData
= AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA
), IdentifyData
);
1128 if (DeviceInfo
->IdentifyData
== NULL
) {
1129 FreePool (DeviceInfo
);
1130 return EFI_OUT_OF_RESOURCES
;
1134 InsertTailList (&Instance
->DeviceList
, &DeviceInfo
->Link
);
1140 Destroy all attached ATA devices info.
1142 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1147 DestroyDeviceInfoList (
1148 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1151 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1154 Node
= GetFirstNode (&Instance
->DeviceList
);
1155 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1156 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1158 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1160 RemoveEntryList (&DeviceInfo
->Link
);
1161 if (DeviceInfo
->IdentifyData
!= NULL
) {
1162 FreePool (DeviceInfo
->IdentifyData
);
1164 FreePool (DeviceInfo
);
1169 Destroy all pending non blocking tasks.
1171 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1172 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1178 DestroyAsynTaskList (
1179 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1180 IN BOOLEAN IsSigEvent
1184 LIST_ENTRY
*DelEntry
;
1185 ATA_NONBLOCK_TASK
*Task
;
1188 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1189 if (!IsListEmpty (&Instance
->NonBlockingTaskList
)) {
1191 // Free the Subtask list.
1193 for (Entry
= (&Instance
->NonBlockingTaskList
)->ForwardLink
;
1194 Entry
!= (&Instance
->NonBlockingTaskList
);
1197 Entry
= Entry
->ForwardLink
;
1198 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry
);
1200 RemoveEntryList (DelEntry
);
1202 Task
->Packet
->Asb
->AtaStatus
= 0x01;
1203 gBS
->SignalEvent (Task
->Event
);
1208 gBS
->RestoreTPL (OldTpl
);
1212 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1214 The function is designed to enumerate all attached ATA devices.
1216 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1218 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1219 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1224 EnumerateAttachedDevice (
1225 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1232 Status
= EFI_SUCCESS
;
1234 Status
= Instance
->PciIo
->Pci
.Read (
1237 PCI_CLASSCODE_OFFSET
,
1238 sizeof (PciData
.Hdr
.ClassCode
),
1239 PciData
.Hdr
.ClassCode
1241 ASSERT_EFI_ERROR (Status
);
1243 ClassCode
= PciData
.Hdr
.ClassCode
[1];
1245 switch (ClassCode
) {
1246 case PCI_CLASS_MASS_STORAGE_IDE
:
1248 // The ATA controller is working at IDE mode
1250 Instance
->Mode
= EfiAtaIdeMode
;
1252 Status
= IdeModeInitialization (Instance
);
1253 if (EFI_ERROR (Status
)) {
1254 Status
= EFI_DEVICE_ERROR
;
1258 case PCI_CLASS_MASS_STORAGE_SATADPA
:
1260 // The ATA controller is working at AHCI mode
1262 Instance
->Mode
= EfiAtaAhciMode
;
1264 Status
= AhciModeInitialization (Instance
);
1266 if (EFI_ERROR (Status
)) {
1267 Status
= EFI_DEVICE_ERROR
;
1273 Status
= EFI_UNSUPPORTED
;
1281 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1282 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1283 and the non-blocking I/O functionality is optional.
1285 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1286 @param[in] Port The port number of the ATA device to send the command.
1287 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1288 If there is no port multiplier, then specify 0xFFFF.
1289 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1290 and PortMultiplierPort.
1291 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1292 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1293 Event is not NULL and non blocking I/O is supported, then non-blocking
1294 I/O is performed, and Event will be signaled when the ATA command completes.
1296 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1297 InTransferLength bytes were transferred from InDataBuffer. For write and
1298 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1299 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1300 is returned in InTransferLength. For write and bi-directional commands,
1301 OutTransferLength bytes were transferred by OutDataBuffer.
1302 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1303 already queued. The caller may retry again later.
1304 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1305 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1306 command was not sent, so no additional status information is available.
1311 AtaPassThruPassThru (
1312 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1314 IN UINT16 PortMultiplierPort
,
1315 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
1316 IN EFI_EVENT Event OPTIONAL
1319 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1321 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1322 EFI_IDENTIFY_DATA
*IdentifyData
;
1324 UINT32 MaxSectorCount
;
1325 ATA_NONBLOCK_TASK
*Task
;
1329 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1331 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1332 return EFI_INVALID_PARAMETER
;
1335 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1336 return EFI_INVALID_PARAMETER
;
1339 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->Asb
, This
->Mode
->IoAlign
)) {
1340 return EFI_INVALID_PARAMETER
;
1343 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1346 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeCdrom
);
1348 return EFI_INVALID_PARAMETER
;
1353 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1354 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1355 // If bit10 is one, it means the ata device support 48-bit addressing.
1357 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1358 IdentifyData
= DeviceInfo
->IdentifyData
;
1359 MaxSectorCount
= 0x100;
1360 if ((IdentifyData
->AtaData
.command_set_supported_83
& (BIT10
| BIT15
| BIT14
)) == 0x4400) {
1361 Capacity
= *((UINT64
*)IdentifyData
->AtaData
.maximum_lba_for_48bit_addressing
);
1362 if (Capacity
> 0xFFFFFFF) {
1364 // Capacity exceeds 120GB. 48-bit addressing is really needed
1365 // In this case, the max sector count is 0x10000
1367 MaxSectorCount
= 0x10000;
1372 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& (BIT14
| BIT15
)) == BIT14
) {
1374 // Check logical block size
1376 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& BIT12
) != 0) {
1377 BlockSize
= (UINT32
) (((IdentifyData
->AtaData
.logic_sector_size_hi
<< 16) | IdentifyData
->AtaData
.logic_sector_size_lo
) * sizeof (UINT16
));
1382 // convert the transfer length from sector count to byte.
1384 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1385 (Packet
->InTransferLength
!= 0)) {
1386 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
1390 // convert the transfer length from sector count to byte.
1392 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1393 (Packet
->OutTransferLength
!= 0)) {
1394 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
1398 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1399 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1402 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
1403 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
))) {
1404 return EFI_BAD_BUFFER_SIZE
;
1408 // For non-blocking mode, queue the Task into the list.
1410 if (Event
!= NULL
) {
1411 Task
= AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK
));
1413 return EFI_OUT_OF_RESOURCES
;
1416 Task
->Signature
= ATA_NONBLOCKING_TASK_SIGNATURE
;
1418 Task
->PortMultiplier
= PortMultiplierPort
;
1419 Task
->Packet
= Packet
;
1420 Task
->Event
= Event
;
1421 Task
->IsStart
= FALSE
;
1422 Task
->RetryTimes
= DivU64x32(Packet
->Timeout
, 1000) + 1;
1423 if (Packet
->Timeout
== 0) {
1424 Task
->InfiniteWait
= TRUE
;
1426 Task
->InfiniteWait
= FALSE
;
1429 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1430 InsertTailList (&Instance
->NonBlockingTaskList
, &Task
->Link
);
1431 gBS
->RestoreTPL (OldTpl
);
1435 return AtaPassThruPassThruExecute (
1446 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1447 These can either be the list of ports where ATA devices are actually present or the
1448 list of legal port numbers for the ATA controller. Regardless, the caller of this
1449 function must probe the port number returned to see if an ATA device is actually
1450 present at that location on the ATA controller.
1452 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1453 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1454 in Port and EFI_SUCCESS is returned.
1456 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1457 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1458 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1459 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1461 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1464 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1465 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1466 On output, a pointer to the next port number on the ATA
1467 controller. An input value of 0xFFFF retrieves the first port
1468 number on the ATA controller.
1470 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1471 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1472 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1478 AtaPassThruGetNextPort (
1479 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1483 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1485 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1487 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1490 return EFI_INVALID_PARAMETER
;
1493 if (*Port
== 0xFFFF) {
1495 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1497 Node
= GetFirstNode (&Instance
->DeviceList
);
1499 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1500 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1502 if (DeviceInfo
->Type
== EfiIdeHarddisk
) {
1503 *Port
= DeviceInfo
->Port
;
1507 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1510 return EFI_NOT_FOUND
;
1511 } else if (*Port
== Instance
->PreviousPort
) {
1512 Node
= GetFirstNode (&Instance
->DeviceList
);
1514 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1515 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1517 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1518 (DeviceInfo
->Port
> *Port
)){
1519 *Port
= DeviceInfo
->Port
;
1523 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1526 return EFI_NOT_FOUND
;
1529 // Port is not equal to 0xFFFF and also not equal to previous return value
1531 return EFI_INVALID_PARAMETER
;
1536 // Update the PreviousPort and PreviousPortMultiplier.
1538 Instance
->PreviousPort
= *Port
;
1544 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1545 controller. These can either be the list of port multiplier ports where ATA devices are actually
1546 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1547 function must probe the port number and port multiplier port number returned to see if an ATA
1548 device is actually present.
1550 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1551 present on a port of an ATA controller.
1553 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1554 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1555 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1558 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1559 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1560 EFI_SUCCESS is returned.
1562 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1563 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1566 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1567 the ATA controller, then EFI_NOT_FOUND is returned.
1569 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1570 @param[in] Port The port number present on the ATA controller.
1571 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1572 ATA device present on the ATA controller.
1573 If on input a PortMultiplierPort of 0xFFFF is specified,
1574 then the port multiplier port number of the first ATA device
1575 is returned. On output, a pointer to the port multiplier port
1576 number of the next ATA device present on an ATA controller.
1578 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1579 of the ATA controller was returned in PortMultiplierPort.
1580 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1581 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1582 returned on a previous call to GetNextDevice().
1587 AtaPassThruGetNextDevice (
1588 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1590 IN OUT UINT16
*PortMultiplierPort
1593 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1595 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1597 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1599 if (PortMultiplierPort
== NULL
) {
1600 return EFI_INVALID_PARAMETER
;
1603 if (Instance
->PreviousPortMultiplier
== 0xFFFF) {
1605 // If a device is directly attached on a port, previous call to this
1606 // function will return the value 0xFFFF for PortMultiplierPort. In
1607 // this case, there should be no more device on the port multiplier.
1609 Instance
->PreviousPortMultiplier
= 0;
1610 return EFI_NOT_FOUND
;
1613 if (*PortMultiplierPort
== Instance
->PreviousPortMultiplier
) {
1614 Node
= GetFirstNode (&Instance
->DeviceList
);
1616 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1617 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1619 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1620 (DeviceInfo
->Port
== Port
) &&
1621 (DeviceInfo
->PortMultiplier
> *PortMultiplierPort
)){
1622 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1626 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1629 return EFI_NOT_FOUND
;
1630 } else if (*PortMultiplierPort
== 0xFFFF) {
1632 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1634 Node
= GetFirstNode (&Instance
->DeviceList
);
1636 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1637 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1639 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1640 (DeviceInfo
->Port
== Port
)){
1641 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1645 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1648 return EFI_NOT_FOUND
;
1651 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1653 return EFI_INVALID_PARAMETER
;
1658 // Update the PreviousPort and PreviousPortMultiplier.
1660 Instance
->PreviousPortMultiplier
= *PortMultiplierPort
;
1666 Used to allocate and build a device path node for an ATA device on an ATA controller.
1668 The BuildDevicePath() function allocates and builds a single device node for the ATA
1669 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1670 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1671 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1672 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1674 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1675 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1676 and EFI_SUCCESS is returned.
1678 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1679 @param[in] Port Port specifies the port number of the ATA device for which a
1680 device path node is to be allocated and built.
1681 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1682 device path node is to be allocated and built. If there is no
1683 port multiplier, then specify 0xFFFF.
1684 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1685 device specified by Port and PortMultiplierPort. This function
1686 is responsible for allocating the buffer DevicePath with the
1687 boot service AllocatePool(). It is the caller's responsibility
1688 to free DevicePath when the caller is finished with DevicePath.
1689 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1690 Port and PortMultiplierPort was allocated and returned in DevicePath.
1691 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1692 exist on the ATA controller.
1693 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1694 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1699 AtaPassThruBuildDevicePath (
1700 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1702 IN UINT16 PortMultiplierPort
,
1703 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1706 EFI_DEV_PATH
*DevicePathNode
;
1707 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1710 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1713 // Validate parameters passed in.
1715 if (DevicePath
== NULL
) {
1716 return EFI_INVALID_PARAMETER
;
1719 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1721 return EFI_NOT_FOUND
;
1724 if (Instance
->Mode
== EfiAtaIdeMode
) {
1725 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
1726 if (DevicePathNode
== NULL
) {
1727 return EFI_OUT_OF_RESOURCES
;
1729 DevicePathNode
->Atapi
.PrimarySecondary
= (UINT8
) Port
;
1730 DevicePathNode
->Atapi
.SlaveMaster
= (UINT8
) PortMultiplierPort
;
1731 DevicePathNode
->Atapi
.Lun
= 0;
1733 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
1734 if (DevicePathNode
== NULL
) {
1735 return EFI_OUT_OF_RESOURCES
;
1738 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
1739 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplierPort
;
1740 DevicePathNode
->Sata
.Lun
= 0;
1743 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
1749 Used to translate a device path node to a port number and port multiplier port number.
1751 The GetDevice() function determines the port and port multiplier port number associated with
1752 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1753 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1754 DevicePath into a port number and port multiplier port number.
1756 If this translation is successful, then that port number and port multiplier port number are returned
1757 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1759 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1761 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1762 EFI_UNSUPPORTED is returned.
1764 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1765 a valid translation from DevicePath to a port number and port multiplier port number, then
1766 EFI_NOT_FOUND is returned.
1768 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1769 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1771 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1772 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1773 on the ATA controller.
1775 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1776 port number, and they were returned in Port and PortMultiplierPort.
1777 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1778 @retval EFI_INVALID_PARAMETER Port is NULL.
1779 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1780 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1781 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1782 port number does not exist.
1786 AtaPassThruGetDevice (
1787 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1788 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1790 OUT UINT16
*PortMultiplierPort
1793 EFI_DEV_PATH
*DevicePathNode
;
1794 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1797 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1800 // Validate parameters passed in.
1802 if (DevicePath
== NULL
|| Port
== NULL
|| PortMultiplierPort
== NULL
) {
1803 return EFI_INVALID_PARAMETER
;
1807 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1809 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1810 ((DevicePath
->SubType
!= MSG_SATA_DP
) &&
1811 (DevicePath
->SubType
!= MSG_ATAPI_DP
)) ||
1812 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
1813 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
1814 return EFI_UNSUPPORTED
;
1817 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
1819 if (Instance
->Mode
== EfiAtaIdeMode
) {
1820 *Port
= DevicePathNode
->Atapi
.PrimarySecondary
;
1821 *PortMultiplierPort
= DevicePathNode
->Atapi
.SlaveMaster
;
1823 *Port
= DevicePathNode
->Sata
.HBAPortNumber
;
1824 *PortMultiplierPort
= DevicePathNode
->Sata
.PortMultiplierPortNumber
;
1827 Node
= SearchDeviceInfoList(Instance
, *Port
, *PortMultiplierPort
, EfiIdeHarddisk
);
1830 return EFI_NOT_FOUND
;
1837 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1838 connected to the port.
1840 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1841 resets all the ATA devices connected to that port. If this ATA controller does not support
1842 a reset port operation, then EFI_UNSUPPORTED is returned.
1844 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1847 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1849 If the port reset operation is completed, then EFI_SUCCESS is returned.
1851 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1852 @param[in] Port The port number on the ATA controller.
1854 @retval EFI_SUCCESS The ATA controller port was reset.
1855 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1856 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1857 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1862 AtaPassThruResetPort (
1863 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1868 // Return success directly then upper layer driver could think reset port operation is done.
1874 Resets an ATA device that is connected to an ATA controller.
1876 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1877 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1880 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1881 EFI_INVALID_PARAMETER is returned.
1883 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1886 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1889 If the device reset operation is completed, then EFI_SUCCESS is returned.
1891 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1892 @param[in] Port Port represents the port number of the ATA device to be reset.
1893 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1894 If there is no port multiplier, then specify 0xFFFF.
1895 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1896 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1897 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1898 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1899 specified by Port and PortMultiplierPort.
1900 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1901 specified by Port and PortMultiplierPort.
1906 AtaPassThruResetDevice (
1907 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1909 IN UINT16 PortMultiplierPort
1912 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1915 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1917 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1920 return EFI_INVALID_PARAMETER
;
1924 // Return success directly then upper layer driver could think reset device operation is done.
1930 Submit ATAPI request sense command.
1932 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1933 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1934 the id of the SCSI device to send the SCSI Request Packet. Each
1935 transport driver may choose to utilize a subset of this size to suit the needs
1936 of transport target representation. For example, a Fibre Channel driver
1937 may use only 8 bytes (WWN) to represent an FC target.
1938 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1939 @param[in] SenseData A pointer to store sense data.
1940 @param[in] SenseDataLength The sense data length.
1941 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1943 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1944 @retval EFI_DEVICE_ERROR The device failed to send data.
1949 AtaPacketRequestSense (
1950 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1954 IN UINT8 SenseDataLength
,
1958 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet
;
1962 ZeroMem (&Packet
, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1965 Cdb
[0] = ATA_CMD_REQUEST_SENSE
;
1966 Cdb
[4] = SenseDataLength
;
1968 Packet
.Timeout
= Timeout
;
1970 Packet
.CdbLength
= 12;
1971 Packet
.DataDirection
= EFI_EXT_SCSI_DATA_DIRECTION_READ
;
1972 Packet
.InDataBuffer
= SenseData
;
1973 Packet
.InTransferLength
= SenseDataLength
;
1975 Status
= ExtScsiPassThruPassThru (This
, Target
, Lun
, &Packet
, NULL
);
1981 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1982 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1983 nonblocking I/O functionality is optional.
1985 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1986 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1987 the id of the SCSI device to send the SCSI Request Packet. Each
1988 transport driver may choose to utilize a subset of this size to suit the needs
1989 of transport target representation. For example, a Fibre Channel driver
1990 may use only 8 bytes (WWN) to represent an FC target.
1991 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1992 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1993 specified by Target and Lun.
1994 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1995 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1996 Event is not NULL and non blocking I/O is supported, then
1997 nonblocking I/O is performed, and Event will be signaled when the
1998 SCSI Request Packet completes.
2000 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
2001 commands, InTransferLength bytes were transferred from
2002 InDataBuffer. For write and bi-directional commands,
2003 OutTransferLength bytes were transferred by
2005 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
2006 could be transferred is returned in InTransferLength. For write
2007 and bi-directional commands, OutTransferLength bytes were
2008 transferred by OutDataBuffer.
2009 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
2010 SCSI Request Packets already queued. The caller may retry again later.
2011 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
2013 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
2014 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
2015 by the host adapter. This includes the case of Bi-directional SCSI
2016 commands not supported by the implementation. The SCSI Request
2017 Packet was not sent, so no additional status information is available.
2018 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
2023 ExtScsiPassThruPassThru (
2024 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2027 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
2028 IN EFI_EVENT Event OPTIONAL
2032 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2034 UINT8 PortMultiplier
;
2035 EFI_ATA_HC_WORK_MODE Mode
;
2037 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2039 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
2041 EFI_STATUS SenseStatus
;
2044 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2046 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
2047 return EFI_INVALID_PARAMETER
;
2051 // Don't support variable length CDB
2053 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
2054 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
2055 return EFI_INVALID_PARAMETER
;
2058 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
2059 return EFI_INVALID_PARAMETER
;
2062 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
2063 return EFI_INVALID_PARAMETER
;
2066 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
2067 return EFI_INVALID_PARAMETER
;
2070 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
2071 return EFI_INVALID_PARAMETER
;
2075 // For ATAPI device, doesn't support multiple LUN device.
2078 return EFI_INVALID_PARAMETER
;
2082 // The layout of Target array:
2083 // ________________________________________________________________________
2084 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2085 // |_____________________|_____________________|_____|______________________|
2086 // | | The port multiplier | | |
2087 // | The port number | port number | N/A | N/A |
2088 // |_____________________|_____________________|_____|______________________|
2090 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2093 PortMultiplier
= Target
[1];
2095 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2097 return EFI_INVALID_PARAMETER
;
2100 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2103 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2104 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2105 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2107 if (*((UINT8
*)Packet
->Cdb
) == ATA_CMD_IDENTIFY_DEVICE
) {
2108 CopyMem (Packet
->InDataBuffer
, DeviceInfo
->IdentifyData
, sizeof (EFI_IDENTIFY_DATA
));
2110 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2112 Packet
->SenseDataLength
= 0;
2116 Mode
= Instance
->Mode
;
2120 // Reassign IDE mode io port registers' base addresses
2122 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
2124 if (EFI_ERROR (Status
)) {
2128 Status
= AtaPacketCommandExecute (Instance
->PciIo
, &Instance
->IdeRegisters
[Port
], Port
, PortMultiplier
, Packet
);
2130 case EfiAtaAhciMode
:
2131 if (PortMultiplier
== 0xFF) {
2133 // If there is no port multiplier, the PortMultiplier will be 0xFF
2134 // Here, we convert its value to 0 to follow the AHCI spec.
2138 Status
= AhciPacketCommandExecute (Instance
->PciIo
, &Instance
->AhciRegisters
, Port
, PortMultiplier
, Packet
);
2141 Status
= EFI_DEVICE_ERROR
;
2146 // If the cmd doesn't get executed correctly, then check sense data.
2148 if (EFI_ERROR (Status
) && (Packet
->SenseDataLength
!= 0) && (*((UINT8
*)Packet
->Cdb
) != ATA_CMD_REQUEST_SENSE
)) {
2149 PtrSenseData
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)), This
->Mode
->IoAlign
);
2150 if (PtrSenseData
== NULL
) {
2151 return EFI_DEVICE_ERROR
;
2154 for (SenseReq
= TRUE
; SenseReq
;) {
2155 SenseStatus
= AtaPacketRequestSense (
2160 sizeof (EFI_SCSI_SENSE_DATA
),
2163 if (EFI_ERROR (SenseStatus
)) {
2167 CopyMem ((UINT8
*)Packet
->SenseData
+ SenseDataLen
, PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
2168 SenseDataLen
+= sizeof (EFI_SCSI_SENSE_DATA
);
2171 // no more sense key or number of sense keys exceeds predefined,
2174 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
2175 (SenseDataLen
+ sizeof (EFI_SCSI_SENSE_DATA
) > Packet
->SenseDataLength
)) {
2179 FreeAlignedPages (PtrSenseData
, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)));
2182 // Update the SenseDataLength field to the data length received.
2184 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2189 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2190 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2191 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2192 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2195 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2196 @param Target On input, a pointer to the Target ID (an array of size
2197 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2198 On output, a pointer to the Target ID (an array of
2199 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2200 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2201 Target array retrieves the Target ID of the first SCSI device present on a
2203 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2204 channel. On output, a pointer to the LUN of the next SCSI device present
2207 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2208 channel was returned in Target and Lun.
2209 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2210 not returned on a previous call to GetNextTargetLun().
2211 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2216 ExtScsiPassThruGetNextTargetLun (
2217 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2218 IN OUT UINT8
**Target
,
2222 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2224 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2228 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2230 if (Target
== NULL
|| Lun
== NULL
) {
2231 return EFI_INVALID_PARAMETER
;
2234 if (*Target
== NULL
) {
2235 return EFI_INVALID_PARAMETER
;
2239 Target16
= (UINT16
*)*Target
;
2241 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2243 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2244 // So the higher bytes in Target array should be 0xFF.
2246 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2247 return EFI_INVALID_PARAMETER
;
2251 // When Target is not all 0xFF's, compare 2 least significant bytes with
2252 // previous target id to see if it is returned by previous call.
2254 if ((*Target16
!= Instance
->PreviousTargetId
) ||
2255 (*Lun
!= Instance
->PreviousLun
)) {
2256 return EFI_INVALID_PARAMETER
;
2260 // Traverse the whole device list to find the next cdrom closed to
2261 // the device signified by Target[0] and Target[1].
2263 // Note that we here use a tricky way to find the next cdrom :
2264 // All ata devices are detected and inserted into the device list
2267 Node
= GetFirstNode (&Instance
->DeviceList
);
2269 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2270 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2272 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2273 ((Target8
[0] < DeviceInfo
->Port
) ||
2274 ((Target8
[0] == DeviceInfo
->Port
) &&
2275 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2276 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2277 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2281 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2284 return EFI_NOT_FOUND
;
2287 // If the array is all 0xFF's, start to traverse the device list from the beginning
2289 Node
= GetFirstNode (&Instance
->DeviceList
);
2290 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2291 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2293 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2294 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2295 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2299 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2302 return EFI_NOT_FOUND
;
2309 // Update the PreviousTargetId.
2311 Instance
->PreviousTargetId
= *Target16
;
2312 Instance
->PreviousLun
= *Lun
;
2318 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2320 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2321 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2322 Target ID of the SCSI device for which a device path node is to be
2323 allocated and built. Transport drivers may chose to utilize a subset of
2324 this size to suit the representation of targets. For example, a Fibre
2325 Channel driver may use only 8 bytes (WWN) in the array to represent a
2327 @param Lun The LUN of the SCSI device for which a device path node is to be
2328 allocated and built.
2329 @param DevicePath A pointer to a single device path node that describes the SCSI device
2330 specified by Target and Lun. This function is responsible for
2331 allocating the buffer DevicePath with the boot service
2332 AllocatePool(). It is the caller's responsibility to free
2333 DevicePath when the caller is finished with DevicePath.
2335 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2336 Target and Lun was allocated and returned in
2338 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2339 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2340 on the SCSI channel.
2341 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2346 ExtScsiPassThruBuildDevicePath (
2347 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2350 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
2353 EFI_DEV_PATH
*DevicePathNode
;
2354 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2356 UINT8 PortMultiplier
;
2358 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2361 PortMultiplier
= Target
[1];
2364 // Validate parameters passed in.
2366 if (DevicePath
== NULL
) {
2367 return EFI_INVALID_PARAMETER
;
2371 // can not build device path for the SCSI Host Controller.
2374 return EFI_NOT_FOUND
;
2377 if (SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
) == NULL
) {
2378 return EFI_NOT_FOUND
;
2381 if (Instance
->Mode
== EfiAtaIdeMode
) {
2382 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
2383 if (DevicePathNode
== NULL
) {
2384 return EFI_OUT_OF_RESOURCES
;
2387 DevicePathNode
->Atapi
.PrimarySecondary
= Port
;
2388 DevicePathNode
->Atapi
.SlaveMaster
= PortMultiplier
;
2389 DevicePathNode
->Atapi
.Lun
= (UINT16
) Lun
;
2391 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
2392 if (DevicePathNode
== NULL
) {
2393 return EFI_OUT_OF_RESOURCES
;
2396 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
2398 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
2399 // the PortMultiplier information. If the CD-ROM is directly attached
2400 // on a SATA port, the PortMultiplier should be translated from 0xFF
2401 // to 0xFFFF according to the UEFI spec.
2403 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplier
== 0xFF ? 0xFFFF : PortMultiplier
;
2404 DevicePathNode
->Sata
.Lun
= (UINT16
) Lun
;
2407 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
2413 Used to translate a device path node to a Target ID and LUN.
2415 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2416 @param DevicePath A pointer to a single device path node that describes the SCSI device
2417 on the SCSI channel.
2418 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2419 on the SCSI channel.
2420 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2422 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2423 LUN, and they were returned in Target and Lun.
2424 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2425 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2427 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2433 ExtScsiPassThruGetTargetLun (
2434 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2435 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2440 EFI_DEV_PATH
*DevicePathNode
;
2441 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2444 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2447 // Validate parameters passed in.
2449 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
2450 return EFI_INVALID_PARAMETER
;
2453 if (*Target
== NULL
) {
2454 return EFI_INVALID_PARAMETER
;
2457 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2459 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
2460 ((DevicePath
->SubType
!= MSG_ATAPI_DP
) &&
2461 (DevicePath
->SubType
!= MSG_SATA_DP
)) ||
2462 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
2463 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
2464 return EFI_UNSUPPORTED
;
2467 SetMem (*Target
, TARGET_MAX_BYTES
, 0xFF);
2469 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
2471 if (Instance
->Mode
== EfiAtaIdeMode
) {
2472 (*Target
)[0] = (UINT8
) DevicePathNode
->Atapi
.PrimarySecondary
;
2473 (*Target
)[1] = (UINT8
) DevicePathNode
->Atapi
.SlaveMaster
;
2474 *Lun
= (UINT8
) DevicePathNode
->Atapi
.Lun
;
2476 (*Target
)[0] = (UINT8
) DevicePathNode
->Sata
.HBAPortNumber
;
2477 (*Target
)[1] = (UINT8
) DevicePathNode
->Sata
.PortMultiplierPortNumber
;
2478 *Lun
= (UINT8
) DevicePathNode
->Sata
.Lun
;
2481 Node
= SearchDeviceInfoList(Instance
, (*Target
)[0], (*Target
)[1], EfiIdeCdrom
);
2484 return EFI_NOT_FOUND
;
2488 return EFI_NOT_FOUND
;
2495 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2497 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2499 @retval EFI_SUCCESS The SCSI channel was reset.
2500 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2501 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2502 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2507 ExtScsiPassThruResetChannel (
2508 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
2512 // Return success directly then upper layer driver could think reset channel operation is done.
2518 Resets a SCSI logical unit that is connected to a SCSI channel.
2520 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2521 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2522 target port ID of the SCSI device containing the SCSI logical unit to
2523 reset. Transport drivers may chose to utilize a subset of this array to suit
2524 the representation of their targets.
2525 @param Lun The LUN of the SCSI device to reset.
2527 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2528 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2529 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2530 specified by Target and Lun.
2531 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2532 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2533 specified by Target and Lun.
2538 ExtScsiPassThruResetTargetLun (
2539 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2544 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2547 UINT8 PortMultiplier
;
2549 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2551 // For ATAPI device, doesn't support multiple LUN device.
2554 return EFI_INVALID_PARAMETER
;
2557 // The layout of Target array:
2558 // ________________________________________________________________________
2559 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2560 // |_____________________|_____________________|_____|______________________|
2561 // | | The port multiplier | | |
2562 // | The port number | port number | N/A | N/A |
2563 // |_____________________|_____________________|_____|______________________|
2565 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2568 PortMultiplier
= Target
[1];
2570 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2572 return EFI_INVALID_PARAMETER
;
2576 // Return success directly then upper layer driver could think reset target LUN operation is done.
2582 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2583 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2584 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2585 see if a SCSI device is actually present at that location on the SCSI channel.
2587 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2588 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2589 On output, a pointer to the Target ID (an array of
2590 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2591 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2592 Target array retrieves the Target ID of the first SCSI device present on a
2595 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2596 channel was returned in Target.
2597 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2598 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2599 returned on a previous call to GetNextTarget().
2600 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2605 ExtScsiPassThruGetNextTarget (
2606 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2607 IN OUT UINT8
**Target
2610 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2612 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2616 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2618 if (Target
== NULL
|| *Target
== NULL
) {
2619 return EFI_INVALID_PARAMETER
;
2623 Target16
= (UINT16
*)*Target
;
2625 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2627 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2628 // So the higher bytes in Target array should be 0xFF.
2630 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2631 return EFI_INVALID_PARAMETER
;
2635 // When Target is not all 0xFF's, compare 2 least significant bytes with
2636 // previous target id to see if it is returned by previous call.
2638 if (*Target16
!= Instance
->PreviousTargetId
) {
2639 return EFI_INVALID_PARAMETER
;
2643 // Traverse the whole device list to find the next cdrom closed to
2644 // the device signified by Target[0] and Target[1].
2646 // Note that we here use a tricky way to find the next cdrom :
2647 // All ata devices are detected and inserted into the device list
2650 Node
= GetFirstNode (&Instance
->DeviceList
);
2651 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2652 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2654 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2655 ((Target8
[0] < DeviceInfo
->Port
) ||
2656 ((Target8
[0] == DeviceInfo
->Port
) &&
2657 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2658 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2659 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2663 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2666 return EFI_NOT_FOUND
;
2669 // If the array is all 0xFF's, start to traverse the device list from the beginning
2671 Node
= GetFirstNode (&Instance
->DeviceList
);
2673 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2674 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2676 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2677 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2678 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2682 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2685 return EFI_NOT_FOUND
;
2690 // Update the PreviousTargetId.
2692 Instance
->PreviousTargetId
= *Target16
;