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 Bus Master DMA on the device when exiting the boot services.
485 @param[in] Event Event for which this notification function is being
487 @param[in] Context Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE that
492 AtaPassThruExitBootServices (
497 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
498 EFI_PCI_IO_PROTOCOL
*PciIo
;
500 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
503 PciIo
= Instance
->PciIo
;
507 EfiPciIoAttributeOperationDisable
,
508 Instance
->EnabledPciAttributes
& EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
,
514 Tests to see if this driver supports a given controller. If a child device is provided,
515 it further tests to see if this driver supports creating a handle for the specified child device.
517 This function checks to see if the driver specified by This supports the device specified by
518 ControllerHandle. Drivers will typically use the device path attached to
519 ControllerHandle and/or the services from the bus I/O abstraction attached to
520 ControllerHandle to determine if the driver supports ControllerHandle. This function
521 may be called many times during platform initialization. In order to reduce boot times, the tests
522 performed by this function must be very small, and take as little time as possible to execute. This
523 function must not change the state of any hardware devices, and this function must be aware that the
524 device specified by ControllerHandle may already be managed by the same driver or a
525 different driver. This function must match its calls to AllocatePages() with FreePages(),
526 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
527 Because ControllerHandle may have been previously started by the same driver, if a protocol is
528 already in the opened state, then it must not be closed with CloseProtocol(). This is required
529 to guarantee the state of ControllerHandle is not modified by this function.
531 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
532 @param[in] ControllerHandle The handle of the controller to test. This handle
533 must support a protocol interface that supplies
534 an I/O abstraction to the driver.
535 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
536 parameter is ignored by device drivers, and is optional for bus
537 drivers. For bus drivers, if this parameter is not NULL, then
538 the bus driver must determine if the bus controller specified
539 by ControllerHandle and the child controller specified
540 by RemainingDevicePath are both supported by this
543 @retval EFI_SUCCESS The device specified by ControllerHandle and
544 RemainingDevicePath is supported by the driver specified by This.
545 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
546 RemainingDevicePath is already being managed by the driver
548 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
549 RemainingDevicePath is already being managed by a different
550 driver or an application that requires exclusive access.
551 Currently not implemented.
552 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
553 RemainingDevicePath is not supported by the driver specified by This.
557 AtaAtapiPassThruSupported (
558 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
559 IN EFI_HANDLE Controller
,
560 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
564 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
565 EFI_PCI_IO_PROTOCOL
*PciIo
;
567 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
570 // SATA Controller is a device driver, and should ingore the
571 // "RemainingDevicePath" according to UEFI spec
573 Status
= gBS
->OpenProtocol (
575 &gEfiDevicePathProtocolGuid
,
576 (VOID
*) &ParentDevicePath
,
577 This
->DriverBindingHandle
,
579 EFI_OPEN_PROTOCOL_BY_DRIVER
581 if (EFI_ERROR (Status
)) {
583 // EFI_ALREADY_STARTED is also an error
588 // Close the protocol because we don't use it here
592 &gEfiDevicePathProtocolGuid
,
593 This
->DriverBindingHandle
,
597 Status
= gBS
->OpenProtocol (
599 &gEfiIdeControllerInitProtocolGuid
,
600 (VOID
**) &IdeControllerInit
,
601 This
->DriverBindingHandle
,
603 EFI_OPEN_PROTOCOL_BY_DRIVER
606 if (EFI_ERROR (Status
)) {
608 // EFI_ALREADY_STARTED is also an error
614 // Close the I/O Abstraction(s) used to perform the supported test
618 &gEfiIdeControllerInitProtocolGuid
,
619 This
->DriverBindingHandle
,
624 // Now test the EfiPciIoProtocol
626 Status
= gBS
->OpenProtocol (
628 &gEfiPciIoProtocolGuid
,
630 This
->DriverBindingHandle
,
632 EFI_OPEN_PROTOCOL_GET_PROTOCOL
634 if (EFI_ERROR (Status
)) {
638 // Now further check the PCI header: Base class (offset 0x0B) and
639 // Sub Class (offset 0x0A). This controller should be an ATA controller
641 Status
= PciIo
->Pci
.Read (
644 PCI_CLASSCODE_OFFSET
,
645 sizeof (PciData
.Hdr
.ClassCode
),
646 PciData
.Hdr
.ClassCode
648 if (EFI_ERROR (Status
)) {
649 return EFI_UNSUPPORTED
;
652 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
656 return EFI_UNSUPPORTED
;
660 Starts a device controller or a bus controller.
662 The Start() function is designed to be invoked from the EFI boot service ConnectController().
663 As a result, much of the error checking on the parameters to Start() has been moved into this
664 common boot service. It is legal to call Start() from other locations,
665 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
666 1. ControllerHandle must be a valid EFI_HANDLE.
667 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
668 EFI_DEVICE_PATH_PROTOCOL.
669 3. Prior to calling Start(), the Supported() function for the driver specified by This must
670 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
672 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
673 @param[in] ControllerHandle The handle of the controller to start. This handle
674 must support a protocol interface that supplies
675 an I/O abstraction to the driver.
676 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
677 parameter is ignored by device drivers, and is optional for bus
678 drivers. For a bus driver, if this parameter is NULL, then handles
679 for all the children of Controller are created by this driver.
680 If this parameter is not NULL and the first Device Path Node is
681 not the End of Device Path Node, then only the handle for the
682 child device specified by the first Device Path Node of
683 RemainingDevicePath is created by this driver.
684 If the first Device Path Node of RemainingDevicePath is
685 the End of Device Path Node, no child handle is created by this
688 @retval EFI_SUCCESS The device was started.
689 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
690 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
691 @retval Others The driver failded to start the device.
696 AtaAtapiPassThruStart (
697 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
698 IN EFI_HANDLE Controller
,
699 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
703 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
704 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
705 EFI_PCI_IO_PROTOCOL
*PciIo
;
706 UINT64 EnabledPciAttributes
;
707 UINT64 OriginalPciAttributes
;
709 Status
= EFI_SUCCESS
;
710 IdeControllerInit
= NULL
;
712 OriginalPciAttributes
= 0;
714 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Start== Controller = %x\n", Controller
));
716 Status
= gBS
->OpenProtocol (
718 &gEfiIdeControllerInitProtocolGuid
,
719 (VOID
**) &IdeControllerInit
,
720 This
->DriverBindingHandle
,
722 EFI_OPEN_PROTOCOL_BY_DRIVER
725 if (EFI_ERROR (Status
)) {
726 DEBUG ((EFI_D_ERROR
, "Open Ide_Controller_Init Error, Status=%r", Status
));
730 Status
= gBS
->OpenProtocol (
732 &gEfiPciIoProtocolGuid
,
734 This
->DriverBindingHandle
,
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
738 if (EFI_ERROR (Status
)) {
739 DEBUG ((EFI_D_ERROR
, "Get Pci_Io Protocol Error, Status=%r", Status
));
743 Status
= PciIo
->Attributes (
745 EfiPciIoAttributeOperationGet
,
747 &OriginalPciAttributes
750 if (EFI_ERROR (Status
)) {
754 Status
= PciIo
->Attributes (
756 EfiPciIoAttributeOperationSupported
,
758 &EnabledPciAttributes
760 if (!EFI_ERROR (Status
)) {
761 EnabledPciAttributes
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
762 Status
= PciIo
->Attributes (
764 EfiPciIoAttributeOperationEnable
,
765 EnabledPciAttributes
,
770 if (EFI_ERROR (Status
)) {
775 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
777 Instance
= AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE
), &gAtaAtapiPassThruInstanceTemplate
);
778 if (Instance
== NULL
) {
782 Instance
->ControllerHandle
= Controller
;
783 Instance
->IdeControllerInit
= IdeControllerInit
;
784 Instance
->PciIo
= PciIo
;
785 Instance
->EnabledPciAttributes
= EnabledPciAttributes
;
786 Instance
->OriginalPciAttributes
= OriginalPciAttributes
;
787 Instance
->AtaPassThru
.Mode
= &Instance
->AtaPassThruMode
;
788 Instance
->ExtScsiPassThru
.Mode
= &Instance
->ExtScsiPassThruMode
;
789 InitializeListHead(&Instance
->DeviceList
);
790 InitializeListHead(&Instance
->NonBlockingTaskList
);
792 Status
= gBS
->CreateEvent (
793 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
795 AtaPassThruExitBootServices
,
797 &Instance
->ExitBootEvent
799 if (EFI_ERROR (Status
)) {
803 Instance
->TimerEvent
= NULL
;
805 Status
= gBS
->CreateEvent (
806 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
808 AsyncNonBlockingTransferRoutine
,
810 &Instance
->TimerEvent
812 if (EFI_ERROR (Status
)) {
819 Status
= gBS
->SetTimer (Instance
->TimerEvent
, TimerPeriodic
, 10000);
820 if (EFI_ERROR (Status
)) {
825 // Enumerate all inserted ATA devices.
827 Status
= EnumerateAttachedDevice (Instance
);
828 if (EFI_ERROR (Status
)) {
832 Status
= gBS
->InstallMultipleProtocolInterfaces (
834 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
835 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
838 ASSERT_EFI_ERROR (Status
);
843 if (IdeControllerInit
!= NULL
) {
846 &gEfiIdeControllerInitProtocolGuid
,
847 This
->DriverBindingHandle
,
852 if ((Instance
!= NULL
) && (Instance
->TimerEvent
!= NULL
)) {
853 gBS
->CloseEvent (Instance
->TimerEvent
);
856 if ((Instance
!= NULL
) && (Instance
->ExitBootEvent
!= NULL
)) {
857 gBS
->CloseEvent (Instance
->ExitBootEvent
);
861 // Remove all inserted ATA devices.
863 DestroyDeviceInfoList(Instance
);
865 if (Instance
!= NULL
) {
868 return EFI_UNSUPPORTED
;
872 Stops a device controller or a bus controller.
874 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
875 As a result, much of the error checking on the parameters to Stop() has been moved
876 into this common boot service. It is legal to call Stop() from other locations,
877 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
878 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
879 same driver's Start() function.
880 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
881 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
882 Start() function, and the Start() function must have called OpenProtocol() on
883 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
885 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
886 @param[in] ControllerHandle A handle to the device being stopped. The handle must
887 support a bus specific I/O protocol for the driver
888 to use to stop the device.
889 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
890 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
891 if NumberOfChildren is 0.
893 @retval EFI_SUCCESS The device was stopped.
894 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
899 AtaAtapiPassThruStop (
900 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
901 IN EFI_HANDLE Controller
,
902 IN UINTN NumberOfChildren
,
903 IN EFI_HANDLE
*ChildHandleBuffer
907 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
908 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
909 EFI_PCI_IO_PROTOCOL
*PciIo
;
910 EFI_AHCI_REGISTERS
*AhciRegisters
;
912 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller
));
914 Status
= gBS
->OpenProtocol (
916 &gEfiAtaPassThruProtocolGuid
,
917 (VOID
**) &AtaPassThru
,
918 This
->DriverBindingHandle
,
920 EFI_OPEN_PROTOCOL_GET_PROTOCOL
923 if (EFI_ERROR (Status
)) {
924 return EFI_DEVICE_ERROR
;
927 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru
);
929 Status
= gBS
->UninstallMultipleProtocolInterfaces (
931 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
932 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
936 if (EFI_ERROR (Status
)) {
937 return EFI_DEVICE_ERROR
;
941 // Close protocols opened by AtaAtapiPassThru controller driver
945 &gEfiIdeControllerInitProtocolGuid
,
946 This
->DriverBindingHandle
,
951 // Close Non-Blocking timer and free Task list.
953 if (Instance
->TimerEvent
!= NULL
) {
954 gBS
->CloseEvent (Instance
->TimerEvent
);
955 Instance
->TimerEvent
= NULL
;
957 DestroyAsynTaskList (Instance
, FALSE
);
960 // Close event signaled at gBS->ExitBootServices().
962 if (Instance
->ExitBootEvent
!= NULL
) {
963 gBS
->CloseEvent (Instance
->ExitBootEvent
);
964 Instance
->ExitBootEvent
= NULL
;
968 // Free allocated resource
970 DestroyDeviceInfoList (Instance
);
972 PciIo
= Instance
->PciIo
;
975 // Disable this ATA host controller.
979 EfiPciIoAttributeOperationDisable
,
980 Instance
->EnabledPciAttributes
,
985 // If the current working mode is AHCI mode, then pre-allocated resource
986 // for AHCI initialization should be released.
988 if (Instance
->Mode
== EfiAtaAhciMode
) {
989 AhciRegisters
= &Instance
->AhciRegisters
;
992 AhciRegisters
->MapCommandTable
996 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandTableSize
),
997 AhciRegisters
->AhciCommandTable
1001 AhciRegisters
->MapCmdList
1005 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandListSize
),
1006 AhciRegisters
->AhciCmdList
1010 AhciRegisters
->MapRFis
1014 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxReceiveFisSize
),
1015 AhciRegisters
->AhciRFis
1020 // Restore original PCI attributes
1022 Status
= PciIo
->Attributes (
1024 EfiPciIoAttributeOperationSet
,
1025 Instance
->OriginalPciAttributes
,
1028 ASSERT_EFI_ERROR (Status
);
1030 FreePool (Instance
);
1036 Traverse the attached ATA devices list to find out the device to access.
1038 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1039 @param[in] Port The port number of the ATA device to send the command.
1040 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1041 If there is no port multiplier, then specify 0xFFFF.
1042 @param[in] DeviceType The device type of the ATA device.
1044 @retval The pointer to the data structure of the device info to access.
1049 SearchDeviceInfoList (
1050 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1052 IN UINT16 PortMultiplier
,
1053 IN EFI_ATA_DEVICE_TYPE DeviceType
1056 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1059 Node
= GetFirstNode (&Instance
->DeviceList
);
1060 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1061 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1064 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
1065 // the PortMultiplier information. If the CD-ROM is directly attached
1066 // on a SATA port, the PortMultiplier should be translated from 0xFF
1067 // to 0xFFFF according to the UEFI spec.
1069 if ((Instance
->Mode
== EfiAtaAhciMode
) &&
1070 (DeviceInfo
->Type
== EfiIdeCdrom
) &&
1071 (PortMultiplier
== 0xFF)) {
1072 PortMultiplier
= 0xFFFF;
1075 if ((DeviceInfo
->Type
== DeviceType
) &&
1076 (Port
== DeviceInfo
->Port
) &&
1077 (PortMultiplier
== DeviceInfo
->PortMultiplier
)) {
1081 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1088 Allocate device info data structure to contain device info.
1089 And insert the data structure to the tail of device list for tracing.
1091 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1092 @param[in] Port The port number of the ATA device to send the command.
1093 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1094 If there is no port multiplier, then specify 0xFFFF.
1095 @param[in] DeviceType The device type of the ATA device.
1096 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1098 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1099 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1104 CreateNewDeviceInfo (
1105 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1107 IN UINT16 PortMultiplier
,
1108 IN EFI_ATA_DEVICE_TYPE DeviceType
,
1109 IN EFI_IDENTIFY_DATA
*IdentifyData
1112 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1114 DeviceInfo
= AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO
));
1116 if (DeviceInfo
== NULL
) {
1117 return EFI_OUT_OF_RESOURCES
;
1120 DeviceInfo
->Signature
= ATA_ATAPI_DEVICE_SIGNATURE
;
1121 DeviceInfo
->Port
= Port
;
1122 DeviceInfo
->PortMultiplier
= PortMultiplier
;
1123 DeviceInfo
->Type
= DeviceType
;
1125 if (IdentifyData
!= NULL
) {
1126 DeviceInfo
->IdentifyData
= AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA
), IdentifyData
);
1127 if (DeviceInfo
->IdentifyData
== NULL
) {
1128 FreePool (DeviceInfo
);
1129 return EFI_OUT_OF_RESOURCES
;
1133 InsertTailList (&Instance
->DeviceList
, &DeviceInfo
->Link
);
1139 Destroy all attached ATA devices info.
1141 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1146 DestroyDeviceInfoList (
1147 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1150 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1153 Node
= GetFirstNode (&Instance
->DeviceList
);
1154 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1155 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1157 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1159 RemoveEntryList (&DeviceInfo
->Link
);
1160 if (DeviceInfo
->IdentifyData
!= NULL
) {
1161 FreePool (DeviceInfo
->IdentifyData
);
1163 FreePool (DeviceInfo
);
1168 Destroy all pending non blocking tasks.
1170 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1171 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1177 DestroyAsynTaskList (
1178 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1179 IN BOOLEAN IsSigEvent
1183 LIST_ENTRY
*DelEntry
;
1184 ATA_NONBLOCK_TASK
*Task
;
1187 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1188 if (!IsListEmpty (&Instance
->NonBlockingTaskList
)) {
1190 // Free the Subtask list.
1192 for (Entry
= (&Instance
->NonBlockingTaskList
)->ForwardLink
;
1193 Entry
!= (&Instance
->NonBlockingTaskList
);
1196 Entry
= Entry
->ForwardLink
;
1197 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry
);
1199 RemoveEntryList (DelEntry
);
1201 Task
->Packet
->Asb
->AtaStatus
= 0x01;
1202 gBS
->SignalEvent (Task
->Event
);
1207 gBS
->RestoreTPL (OldTpl
);
1211 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1213 The function is designed to enumerate all attached ATA devices.
1215 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1217 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1218 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1223 EnumerateAttachedDevice (
1224 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1231 Status
= EFI_SUCCESS
;
1233 Status
= Instance
->PciIo
->Pci
.Read (
1236 PCI_CLASSCODE_OFFSET
,
1237 sizeof (PciData
.Hdr
.ClassCode
),
1238 PciData
.Hdr
.ClassCode
1240 ASSERT_EFI_ERROR (Status
);
1242 ClassCode
= PciData
.Hdr
.ClassCode
[1];
1244 switch (ClassCode
) {
1245 case PCI_CLASS_MASS_STORAGE_IDE
:
1247 // The ATA controller is working at IDE mode
1249 Instance
->Mode
= EfiAtaIdeMode
;
1251 Status
= IdeModeInitialization (Instance
);
1252 if (EFI_ERROR (Status
)) {
1253 Status
= EFI_DEVICE_ERROR
;
1257 case PCI_CLASS_MASS_STORAGE_SATADPA
:
1259 // The ATA controller is working at AHCI mode
1261 Instance
->Mode
= EfiAtaAhciMode
;
1263 Status
= AhciModeInitialization (Instance
);
1265 if (EFI_ERROR (Status
)) {
1266 Status
= EFI_DEVICE_ERROR
;
1272 Status
= EFI_UNSUPPORTED
;
1280 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1281 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1282 and the non-blocking I/O functionality is optional.
1284 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1285 @param[in] Port The port number of the ATA device to send the command.
1286 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1287 If there is no port multiplier, then specify 0xFFFF.
1288 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1289 and PortMultiplierPort.
1290 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1291 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1292 Event is not NULL and non blocking I/O is supported, then non-blocking
1293 I/O is performed, and Event will be signaled when the ATA command completes.
1295 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1296 InTransferLength bytes were transferred from InDataBuffer. For write and
1297 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1298 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1299 is returned in InTransferLength. For write and bi-directional commands,
1300 OutTransferLength bytes were transferred by OutDataBuffer.
1301 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1302 already queued. The caller may retry again later.
1303 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1304 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1305 command was not sent, so no additional status information is available.
1310 AtaPassThruPassThru (
1311 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1313 IN UINT16 PortMultiplierPort
,
1314 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
1315 IN EFI_EVENT Event OPTIONAL
1318 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1320 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1321 EFI_IDENTIFY_DATA
*IdentifyData
;
1323 UINT32 MaxSectorCount
;
1324 ATA_NONBLOCK_TASK
*Task
;
1328 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1330 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1331 return EFI_INVALID_PARAMETER
;
1334 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1335 return EFI_INVALID_PARAMETER
;
1338 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->Asb
, This
->Mode
->IoAlign
)) {
1339 return EFI_INVALID_PARAMETER
;
1342 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1345 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeCdrom
);
1347 return EFI_INVALID_PARAMETER
;
1352 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1353 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1354 // If bit10 is one, it means the ata device support 48-bit addressing.
1356 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1357 IdentifyData
= DeviceInfo
->IdentifyData
;
1358 MaxSectorCount
= 0x100;
1359 if ((IdentifyData
->AtaData
.command_set_supported_83
& (BIT10
| BIT15
| BIT14
)) == 0x4400) {
1360 Capacity
= *((UINT64
*)IdentifyData
->AtaData
.maximum_lba_for_48bit_addressing
);
1361 if (Capacity
> 0xFFFFFFF) {
1363 // Capacity exceeds 120GB. 48-bit addressing is really needed
1364 // In this case, the max sector count is 0x10000
1366 MaxSectorCount
= 0x10000;
1371 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& (BIT14
| BIT15
)) == BIT14
) {
1373 // Check logical block size
1375 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& BIT12
) != 0) {
1376 BlockSize
= (UINT32
) (((IdentifyData
->AtaData
.logic_sector_size_hi
<< 16) | IdentifyData
->AtaData
.logic_sector_size_lo
) * sizeof (UINT16
));
1381 // convert the transfer length from sector count to byte.
1383 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1384 (Packet
->InTransferLength
!= 0)) {
1385 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
1389 // convert the transfer length from sector count to byte.
1391 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1392 (Packet
->OutTransferLength
!= 0)) {
1393 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
1397 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1398 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1401 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
1402 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
))) {
1403 return EFI_BAD_BUFFER_SIZE
;
1407 // For non-blocking mode, queue the Task into the list.
1409 if (Event
!= NULL
) {
1410 Task
= AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK
));
1412 return EFI_OUT_OF_RESOURCES
;
1415 Task
->Signature
= ATA_NONBLOCKING_TASK_SIGNATURE
;
1417 Task
->PortMultiplier
= PortMultiplierPort
;
1418 Task
->Packet
= Packet
;
1419 Task
->Event
= Event
;
1420 Task
->IsStart
= FALSE
;
1421 Task
->RetryTimes
= DivU64x32(Packet
->Timeout
, 1000) + 1;
1422 if (Packet
->Timeout
== 0) {
1423 Task
->InfiniteWait
= TRUE
;
1425 Task
->InfiniteWait
= FALSE
;
1428 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1429 InsertTailList (&Instance
->NonBlockingTaskList
, &Task
->Link
);
1430 gBS
->RestoreTPL (OldTpl
);
1434 return AtaPassThruPassThruExecute (
1445 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1446 These can either be the list of ports where ATA devices are actually present or the
1447 list of legal port numbers for the ATA controller. Regardless, the caller of this
1448 function must probe the port number returned to see if an ATA device is actually
1449 present at that location on the ATA controller.
1451 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1452 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1453 in Port and EFI_SUCCESS is returned.
1455 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1456 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1457 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1458 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1460 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1463 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1464 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1465 On output, a pointer to the next port number on the ATA
1466 controller. An input value of 0xFFFF retrieves the first port
1467 number on the ATA controller.
1469 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1470 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1471 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1477 AtaPassThruGetNextPort (
1478 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1482 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1484 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1486 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1489 return EFI_INVALID_PARAMETER
;
1492 if (*Port
== 0xFFFF) {
1494 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1496 Node
= GetFirstNode (&Instance
->DeviceList
);
1498 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1499 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1501 if (DeviceInfo
->Type
== EfiIdeHarddisk
) {
1502 *Port
= DeviceInfo
->Port
;
1506 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1509 return EFI_NOT_FOUND
;
1510 } else if (*Port
== Instance
->PreviousPort
) {
1511 Node
= GetFirstNode (&Instance
->DeviceList
);
1513 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1514 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1516 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1517 (DeviceInfo
->Port
> *Port
)){
1518 *Port
= DeviceInfo
->Port
;
1522 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1525 return EFI_NOT_FOUND
;
1528 // Port is not equal to 0xFFFF and also not equal to previous return value
1530 return EFI_INVALID_PARAMETER
;
1535 // Update the PreviousPort and PreviousPortMultiplier.
1537 Instance
->PreviousPort
= *Port
;
1543 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1544 controller. These can either be the list of port multiplier ports where ATA devices are actually
1545 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1546 function must probe the port number and port multiplier port number returned to see if an ATA
1547 device is actually present.
1549 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1550 present on a port of an ATA controller.
1552 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1553 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1554 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1557 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1558 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1559 EFI_SUCCESS is returned.
1561 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1562 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1565 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1566 the ATA controller, then EFI_NOT_FOUND is returned.
1568 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1569 @param[in] Port The port number present on the ATA controller.
1570 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1571 ATA device present on the ATA controller.
1572 If on input a PortMultiplierPort of 0xFFFF is specified,
1573 then the port multiplier port number of the first ATA device
1574 is returned. On output, a pointer to the port multiplier port
1575 number of the next ATA device present on an ATA controller.
1577 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1578 of the ATA controller was returned in PortMultiplierPort.
1579 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1580 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1581 returned on a previous call to GetNextDevice().
1586 AtaPassThruGetNextDevice (
1587 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1589 IN OUT UINT16
*PortMultiplierPort
1592 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1594 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1596 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1598 if (PortMultiplierPort
== NULL
) {
1599 return EFI_INVALID_PARAMETER
;
1602 if (Instance
->PreviousPortMultiplier
== 0xFFFF) {
1604 // If a device is directly attached on a port, previous call to this
1605 // function will return the value 0xFFFF for PortMultiplierPort. In
1606 // this case, there should be no more device on the port multiplier.
1608 Instance
->PreviousPortMultiplier
= 0;
1609 return EFI_NOT_FOUND
;
1612 if (*PortMultiplierPort
== Instance
->PreviousPortMultiplier
) {
1613 Node
= GetFirstNode (&Instance
->DeviceList
);
1615 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1616 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1618 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1619 (DeviceInfo
->Port
== Port
) &&
1620 (DeviceInfo
->PortMultiplier
> *PortMultiplierPort
)){
1621 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1625 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1628 return EFI_NOT_FOUND
;
1629 } else if (*PortMultiplierPort
== 0xFFFF) {
1631 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1633 Node
= GetFirstNode (&Instance
->DeviceList
);
1635 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1636 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1638 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1639 (DeviceInfo
->Port
== Port
)){
1640 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1644 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1647 return EFI_NOT_FOUND
;
1650 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1652 return EFI_INVALID_PARAMETER
;
1657 // Update the PreviousPort and PreviousPortMultiplier.
1659 Instance
->PreviousPortMultiplier
= *PortMultiplierPort
;
1665 Used to allocate and build a device path node for an ATA device on an ATA controller.
1667 The BuildDevicePath() function allocates and builds a single device node for the ATA
1668 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1669 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1670 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1671 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1673 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1674 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1675 and EFI_SUCCESS is returned.
1677 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1678 @param[in] Port Port specifies the port number of the ATA device for which a
1679 device path node is to be allocated and built.
1680 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1681 device path node is to be allocated and built. If there is no
1682 port multiplier, then specify 0xFFFF.
1683 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1684 device specified by Port and PortMultiplierPort. This function
1685 is responsible for allocating the buffer DevicePath with the
1686 boot service AllocatePool(). It is the caller's responsibility
1687 to free DevicePath when the caller is finished with DevicePath.
1688 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1689 Port and PortMultiplierPort was allocated and returned in DevicePath.
1690 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1691 exist on the ATA controller.
1692 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1693 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1698 AtaPassThruBuildDevicePath (
1699 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1701 IN UINT16 PortMultiplierPort
,
1702 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1705 EFI_DEV_PATH
*DevicePathNode
;
1706 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1709 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1712 // Validate parameters passed in.
1714 if (DevicePath
== NULL
) {
1715 return EFI_INVALID_PARAMETER
;
1718 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1720 return EFI_NOT_FOUND
;
1723 if (Instance
->Mode
== EfiAtaIdeMode
) {
1724 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
1725 if (DevicePathNode
== NULL
) {
1726 return EFI_OUT_OF_RESOURCES
;
1728 DevicePathNode
->Atapi
.PrimarySecondary
= (UINT8
) Port
;
1729 DevicePathNode
->Atapi
.SlaveMaster
= (UINT8
) PortMultiplierPort
;
1730 DevicePathNode
->Atapi
.Lun
= 0;
1732 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
1733 if (DevicePathNode
== NULL
) {
1734 return EFI_OUT_OF_RESOURCES
;
1737 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
1738 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplierPort
;
1739 DevicePathNode
->Sata
.Lun
= 0;
1742 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
1748 Used to translate a device path node to a port number and port multiplier port number.
1750 The GetDevice() function determines the port and port multiplier port number associated with
1751 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1752 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1753 DevicePath into a port number and port multiplier port number.
1755 If this translation is successful, then that port number and port multiplier port number are returned
1756 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1758 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1760 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1761 EFI_UNSUPPORTED is returned.
1763 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1764 a valid translation from DevicePath to a port number and port multiplier port number, then
1765 EFI_NOT_FOUND is returned.
1767 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1768 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1770 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1771 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1772 on the ATA controller.
1774 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1775 port number, and they were returned in Port and PortMultiplierPort.
1776 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1777 @retval EFI_INVALID_PARAMETER Port is NULL.
1778 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1779 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1780 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1781 port number does not exist.
1785 AtaPassThruGetDevice (
1786 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1787 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1789 OUT UINT16
*PortMultiplierPort
1792 EFI_DEV_PATH
*DevicePathNode
;
1793 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1796 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1799 // Validate parameters passed in.
1801 if (DevicePath
== NULL
|| Port
== NULL
|| PortMultiplierPort
== NULL
) {
1802 return EFI_INVALID_PARAMETER
;
1806 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1808 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1809 ((DevicePath
->SubType
!= MSG_SATA_DP
) &&
1810 (DevicePath
->SubType
!= MSG_ATAPI_DP
)) ||
1811 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
1812 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
1813 return EFI_UNSUPPORTED
;
1816 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
1818 if (Instance
->Mode
== EfiAtaIdeMode
) {
1819 *Port
= DevicePathNode
->Atapi
.PrimarySecondary
;
1820 *PortMultiplierPort
= DevicePathNode
->Atapi
.SlaveMaster
;
1822 *Port
= DevicePathNode
->Sata
.HBAPortNumber
;
1823 *PortMultiplierPort
= DevicePathNode
->Sata
.PortMultiplierPortNumber
;
1826 Node
= SearchDeviceInfoList(Instance
, *Port
, *PortMultiplierPort
, EfiIdeHarddisk
);
1829 return EFI_NOT_FOUND
;
1836 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1837 connected to the port.
1839 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1840 resets all the ATA devices connected to that port. If this ATA controller does not support
1841 a reset port operation, then EFI_UNSUPPORTED is returned.
1843 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1846 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1848 If the port reset operation is completed, then EFI_SUCCESS is returned.
1850 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1851 @param[in] Port The port number on the ATA controller.
1853 @retval EFI_SUCCESS The ATA controller port was reset.
1854 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1855 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1856 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1861 AtaPassThruResetPort (
1862 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1867 // Return success directly then upper layer driver could think reset port operation is done.
1873 Resets an ATA device that is connected to an ATA controller.
1875 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1876 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1879 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1880 EFI_INVALID_PARAMETER is returned.
1882 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1885 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1888 If the device reset operation is completed, then EFI_SUCCESS is returned.
1890 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1891 @param[in] Port Port represents the port number of the ATA device to be reset.
1892 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1893 If there is no port multiplier, then specify 0xFFFF.
1894 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1895 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1896 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1897 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1898 specified by Port and PortMultiplierPort.
1899 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1900 specified by Port and PortMultiplierPort.
1905 AtaPassThruResetDevice (
1906 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1908 IN UINT16 PortMultiplierPort
1911 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1914 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1916 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1919 return EFI_INVALID_PARAMETER
;
1923 // Return success directly then upper layer driver could think reset device operation is done.
1929 Submit ATAPI request sense command.
1931 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1932 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1933 the id of the SCSI device to send the SCSI Request Packet. Each
1934 transport driver may choose to utilize a subset of this size to suit the needs
1935 of transport target representation. For example, a Fibre Channel driver
1936 may use only 8 bytes (WWN) to represent an FC target.
1937 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1938 @param[in] SenseData A pointer to store sense data.
1939 @param[in] SenseDataLength The sense data length.
1940 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1942 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1943 @retval EFI_DEVICE_ERROR The device failed to send data.
1948 AtaPacketRequestSense (
1949 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1953 IN UINT8 SenseDataLength
,
1957 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet
;
1961 ZeroMem (&Packet
, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1964 Cdb
[0] = ATA_CMD_REQUEST_SENSE
;
1965 Cdb
[4] = SenseDataLength
;
1967 Packet
.Timeout
= Timeout
;
1969 Packet
.CdbLength
= 12;
1970 Packet
.DataDirection
= EFI_EXT_SCSI_DATA_DIRECTION_READ
;
1971 Packet
.InDataBuffer
= SenseData
;
1972 Packet
.InTransferLength
= SenseDataLength
;
1974 Status
= ExtScsiPassThruPassThru (This
, Target
, Lun
, &Packet
, NULL
);
1980 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1981 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1982 nonblocking I/O functionality is optional.
1984 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1985 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1986 the id of the SCSI device to send the SCSI Request Packet. Each
1987 transport driver may choose to utilize a subset of this size to suit the needs
1988 of transport target representation. For example, a Fibre Channel driver
1989 may use only 8 bytes (WWN) to represent an FC target.
1990 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1991 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1992 specified by Target and Lun.
1993 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1994 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1995 Event is not NULL and non blocking I/O is supported, then
1996 nonblocking I/O is performed, and Event will be signaled when the
1997 SCSI Request Packet completes.
1999 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
2000 commands, InTransferLength bytes were transferred from
2001 InDataBuffer. For write and bi-directional commands,
2002 OutTransferLength bytes were transferred by
2004 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
2005 could be transferred is returned in InTransferLength. For write
2006 and bi-directional commands, OutTransferLength bytes were
2007 transferred by OutDataBuffer.
2008 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
2009 SCSI Request Packets already queued. The caller may retry again later.
2010 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
2012 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
2013 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
2014 by the host adapter. This includes the case of Bi-directional SCSI
2015 commands not supported by the implementation. The SCSI Request
2016 Packet was not sent, so no additional status information is available.
2017 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
2022 ExtScsiPassThruPassThru (
2023 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2026 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
2027 IN EFI_EVENT Event OPTIONAL
2031 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2033 UINT8 PortMultiplier
;
2034 EFI_ATA_HC_WORK_MODE Mode
;
2036 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2038 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
2040 EFI_STATUS SenseStatus
;
2043 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2045 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
2046 return EFI_INVALID_PARAMETER
;
2050 // Don't support variable length CDB
2052 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
2053 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
2054 return EFI_INVALID_PARAMETER
;
2057 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
2058 return EFI_INVALID_PARAMETER
;
2061 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
2062 return EFI_INVALID_PARAMETER
;
2065 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
2066 return EFI_INVALID_PARAMETER
;
2069 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
2070 return EFI_INVALID_PARAMETER
;
2074 // For ATAPI device, doesn't support multiple LUN device.
2077 return EFI_INVALID_PARAMETER
;
2081 // The layout of Target array:
2082 // ________________________________________________________________________
2083 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2084 // |_____________________|_____________________|_____|______________________|
2085 // | | The port multiplier | | |
2086 // | The port number | port number | N/A | N/A |
2087 // |_____________________|_____________________|_____|______________________|
2089 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2092 PortMultiplier
= Target
[1];
2094 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2096 return EFI_INVALID_PARAMETER
;
2099 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2102 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2103 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2104 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2106 if (*((UINT8
*)Packet
->Cdb
) == ATA_CMD_IDENTIFY_DEVICE
) {
2107 CopyMem (Packet
->InDataBuffer
, DeviceInfo
->IdentifyData
, sizeof (EFI_IDENTIFY_DATA
));
2109 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2111 Packet
->SenseDataLength
= 0;
2115 Mode
= Instance
->Mode
;
2119 // Reassign IDE mode io port registers' base addresses
2121 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
2123 if (EFI_ERROR (Status
)) {
2127 Status
= AtaPacketCommandExecute (Instance
->PciIo
, &Instance
->IdeRegisters
[Port
], Port
, PortMultiplier
, Packet
);
2129 case EfiAtaAhciMode
:
2130 if (PortMultiplier
== 0xFF) {
2132 // If there is no port multiplier, the PortMultiplier will be 0xFF
2133 // Here, we convert its value to 0 to follow the AHCI spec.
2137 Status
= AhciPacketCommandExecute (Instance
->PciIo
, &Instance
->AhciRegisters
, Port
, PortMultiplier
, Packet
);
2140 Status
= EFI_DEVICE_ERROR
;
2145 // If the cmd doesn't get executed correctly, then check sense data.
2147 if (EFI_ERROR (Status
) && (Packet
->SenseDataLength
!= 0) && (*((UINT8
*)Packet
->Cdb
) != ATA_CMD_REQUEST_SENSE
)) {
2148 PtrSenseData
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)), This
->Mode
->IoAlign
);
2149 if (PtrSenseData
== NULL
) {
2150 return EFI_DEVICE_ERROR
;
2153 for (SenseReq
= TRUE
; SenseReq
;) {
2154 SenseStatus
= AtaPacketRequestSense (
2159 sizeof (EFI_SCSI_SENSE_DATA
),
2162 if (EFI_ERROR (SenseStatus
)) {
2166 CopyMem ((UINT8
*)Packet
->SenseData
+ SenseDataLen
, PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
2167 SenseDataLen
+= sizeof (EFI_SCSI_SENSE_DATA
);
2170 // no more sense key or number of sense keys exceeds predefined,
2173 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
2174 (SenseDataLen
+ sizeof (EFI_SCSI_SENSE_DATA
) > Packet
->SenseDataLength
)) {
2178 FreeAlignedPages (PtrSenseData
, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)));
2181 // Update the SenseDataLength field to the data length received.
2183 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2188 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2189 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2190 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2191 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2194 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2195 @param Target On input, a pointer to the Target ID (an array of size
2196 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2197 On output, a pointer to the Target ID (an array of
2198 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2199 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2200 Target array retrieves the Target ID of the first SCSI device present on a
2202 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2203 channel. On output, a pointer to the LUN of the next SCSI device present
2206 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2207 channel was returned in Target and Lun.
2208 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2209 not returned on a previous call to GetNextTargetLun().
2210 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2215 ExtScsiPassThruGetNextTargetLun (
2216 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2217 IN OUT UINT8
**Target
,
2221 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2223 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2227 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2229 if (Target
== NULL
|| Lun
== NULL
) {
2230 return EFI_INVALID_PARAMETER
;
2233 if (*Target
== NULL
) {
2234 return EFI_INVALID_PARAMETER
;
2238 Target16
= (UINT16
*)*Target
;
2240 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2242 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2243 // So the higher bytes in Target array should be 0xFF.
2245 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2246 return EFI_INVALID_PARAMETER
;
2250 // When Target is not all 0xFF's, compare 2 least significant bytes with
2251 // previous target id to see if it is returned by previous call.
2253 if ((*Target16
!= Instance
->PreviousTargetId
) ||
2254 (*Lun
!= Instance
->PreviousLun
)) {
2255 return EFI_INVALID_PARAMETER
;
2259 // Traverse the whole device list to find the next cdrom closed to
2260 // the device signified by Target[0] and Target[1].
2262 // Note that we here use a tricky way to find the next cdrom :
2263 // All ata devices are detected and inserted into the device list
2266 Node
= GetFirstNode (&Instance
->DeviceList
);
2268 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2269 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2271 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2272 ((Target8
[0] < DeviceInfo
->Port
) ||
2273 ((Target8
[0] == DeviceInfo
->Port
) &&
2274 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2275 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2276 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2280 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2283 return EFI_NOT_FOUND
;
2286 // If the array is all 0xFF's, start to traverse the device list from the beginning
2288 Node
= GetFirstNode (&Instance
->DeviceList
);
2289 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2290 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2292 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2293 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2294 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2298 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2301 return EFI_NOT_FOUND
;
2308 // Update the PreviousTargetId.
2310 Instance
->PreviousTargetId
= *Target16
;
2311 Instance
->PreviousLun
= *Lun
;
2317 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2319 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2320 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2321 Target ID of the SCSI device for which a device path node is to be
2322 allocated and built. Transport drivers may chose to utilize a subset of
2323 this size to suit the representation of targets. For example, a Fibre
2324 Channel driver may use only 8 bytes (WWN) in the array to represent a
2326 @param Lun The LUN of the SCSI device for which a device path node is to be
2327 allocated and built.
2328 @param DevicePath A pointer to a single device path node that describes the SCSI device
2329 specified by Target and Lun. This function is responsible for
2330 allocating the buffer DevicePath with the boot service
2331 AllocatePool(). It is the caller's responsibility to free
2332 DevicePath when the caller is finished with DevicePath.
2334 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2335 Target and Lun was allocated and returned in
2337 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2338 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2339 on the SCSI channel.
2340 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2345 ExtScsiPassThruBuildDevicePath (
2346 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2349 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
2352 EFI_DEV_PATH
*DevicePathNode
;
2353 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2355 UINT8 PortMultiplier
;
2357 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2360 PortMultiplier
= Target
[1];
2363 // Validate parameters passed in.
2365 if (DevicePath
== NULL
) {
2366 return EFI_INVALID_PARAMETER
;
2370 // can not build device path for the SCSI Host Controller.
2373 return EFI_NOT_FOUND
;
2376 if (SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
) == NULL
) {
2377 return EFI_NOT_FOUND
;
2380 if (Instance
->Mode
== EfiAtaIdeMode
) {
2381 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
2382 if (DevicePathNode
== NULL
) {
2383 return EFI_OUT_OF_RESOURCES
;
2386 DevicePathNode
->Atapi
.PrimarySecondary
= Port
;
2387 DevicePathNode
->Atapi
.SlaveMaster
= PortMultiplier
;
2388 DevicePathNode
->Atapi
.Lun
= (UINT16
) Lun
;
2390 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
2391 if (DevicePathNode
== NULL
) {
2392 return EFI_OUT_OF_RESOURCES
;
2395 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
2397 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
2398 // the PortMultiplier information. If the CD-ROM is directly attached
2399 // on a SATA port, the PortMultiplier should be translated from 0xFF
2400 // to 0xFFFF according to the UEFI spec.
2402 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplier
== 0xFF ? 0xFFFF : PortMultiplier
;
2403 DevicePathNode
->Sata
.Lun
= (UINT16
) Lun
;
2406 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
2412 Used to translate a device path node to a Target ID and LUN.
2414 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2415 @param DevicePath A pointer to a single device path node that describes the SCSI device
2416 on the SCSI channel.
2417 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2418 on the SCSI channel.
2419 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2421 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2422 LUN, and they were returned in Target and Lun.
2423 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2424 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2426 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2432 ExtScsiPassThruGetTargetLun (
2433 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2434 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2439 EFI_DEV_PATH
*DevicePathNode
;
2440 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2443 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2446 // Validate parameters passed in.
2448 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
2449 return EFI_INVALID_PARAMETER
;
2452 if (*Target
== NULL
) {
2453 return EFI_INVALID_PARAMETER
;
2456 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2458 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
2459 ((DevicePath
->SubType
!= MSG_ATAPI_DP
) &&
2460 (DevicePath
->SubType
!= MSG_SATA_DP
)) ||
2461 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
2462 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
2463 return EFI_UNSUPPORTED
;
2466 SetMem (*Target
, TARGET_MAX_BYTES
, 0xFF);
2468 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
2470 if (Instance
->Mode
== EfiAtaIdeMode
) {
2471 (*Target
)[0] = (UINT8
) DevicePathNode
->Atapi
.PrimarySecondary
;
2472 (*Target
)[1] = (UINT8
) DevicePathNode
->Atapi
.SlaveMaster
;
2473 *Lun
= (UINT8
) DevicePathNode
->Atapi
.Lun
;
2475 (*Target
)[0] = (UINT8
) DevicePathNode
->Sata
.HBAPortNumber
;
2476 (*Target
)[1] = (UINT8
) DevicePathNode
->Sata
.PortMultiplierPortNumber
;
2477 *Lun
= (UINT8
) DevicePathNode
->Sata
.Lun
;
2480 Node
= SearchDeviceInfoList(Instance
, (*Target
)[0], (*Target
)[1], EfiIdeCdrom
);
2483 return EFI_NOT_FOUND
;
2487 return EFI_NOT_FOUND
;
2494 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2496 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2498 @retval EFI_SUCCESS The SCSI channel was reset.
2499 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2500 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2501 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2506 ExtScsiPassThruResetChannel (
2507 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
2511 // Return success directly then upper layer driver could think reset channel operation is done.
2517 Resets a SCSI logical unit that is connected to a SCSI channel.
2519 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2520 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2521 target port ID of the SCSI device containing the SCSI logical unit to
2522 reset. Transport drivers may chose to utilize a subset of this array to suit
2523 the representation of their targets.
2524 @param Lun The LUN of the SCSI device to reset.
2526 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2527 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2528 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2529 specified by Target and Lun.
2530 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2531 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2532 specified by Target and Lun.
2537 ExtScsiPassThruResetTargetLun (
2538 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2543 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2546 UINT8 PortMultiplier
;
2548 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2550 // For ATAPI device, doesn't support multiple LUN device.
2553 return EFI_INVALID_PARAMETER
;
2556 // The layout of Target array:
2557 // ________________________________________________________________________
2558 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2559 // |_____________________|_____________________|_____|______________________|
2560 // | | The port multiplier | | |
2561 // | The port number | port number | N/A | N/A |
2562 // |_____________________|_____________________|_____|______________________|
2564 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2567 PortMultiplier
= Target
[1];
2569 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2571 return EFI_INVALID_PARAMETER
;
2575 // Return success directly then upper layer driver could think reset target LUN operation is done.
2581 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2582 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2583 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2584 see if a SCSI device is actually present at that location on the SCSI channel.
2586 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2587 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2588 On output, a pointer to the Target ID (an array of
2589 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2590 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2591 Target array retrieves the Target ID of the first SCSI device present on a
2594 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2595 channel was returned in Target.
2596 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2597 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2598 returned on a previous call to GetNextTarget().
2599 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2604 ExtScsiPassThruGetNextTarget (
2605 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2606 IN OUT UINT8
**Target
2609 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2611 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2615 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2617 if (Target
== NULL
|| *Target
== NULL
) {
2618 return EFI_INVALID_PARAMETER
;
2622 Target16
= (UINT16
*)*Target
;
2624 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2626 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2627 // So the higher bytes in Target array should be 0xFF.
2629 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2630 return EFI_INVALID_PARAMETER
;
2634 // When Target is not all 0xFF's, compare 2 least significant bytes with
2635 // previous target id to see if it is returned by previous call.
2637 if (*Target16
!= Instance
->PreviousTargetId
) {
2638 return EFI_INVALID_PARAMETER
;
2642 // Traverse the whole device list to find the next cdrom closed to
2643 // the device signified by Target[0] and Target[1].
2645 // Note that we here use a tricky way to find the next cdrom :
2646 // All ata devices are detected and inserted into the device list
2649 Node
= GetFirstNode (&Instance
->DeviceList
);
2650 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2651 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2653 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2654 ((Target8
[0] < DeviceInfo
->Port
) ||
2655 ((Target8
[0] == DeviceInfo
->Port
) &&
2656 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2657 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2658 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2662 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2665 return EFI_NOT_FOUND
;
2668 // If the array is all 0xFF's, start to traverse the device list from the beginning
2670 Node
= GetFirstNode (&Instance
->DeviceList
);
2672 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2673 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2675 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2676 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2677 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2681 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2684 return EFI_NOT_FOUND
;
2689 // Update the PreviousTargetId.
2691 Instance
->PreviousTargetId
= *Target16
;