2 This file implements ATA_PASSTHRU_PROCTOCOL and EXT_SCSI_PASSTHRU_PROTOCOL interfaces
3 for managed ATA controllers.
5 Copyright (c) 2010 - 2018, 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
110 ATAPI_DEVICE_PATH mAtapiDevicePathTemplate
= {
112 MESSAGING_DEVICE_PATH
,
115 (UINT8
) (sizeof (ATAPI_DEVICE_PATH
)),
116 (UINT8
) ((sizeof (ATAPI_DEVICE_PATH
)) >> 8)
124 SATA_DEVICE_PATH mSataDevicePathTemplate
= {
126 MESSAGING_DEVICE_PATH
,
129 (UINT8
) (sizeof (SATA_DEVICE_PATH
)),
130 (UINT8
) ((sizeof (SATA_DEVICE_PATH
)) >> 8)
138 UINT8 mScsiId
[TARGET_MAX_BYTES
] = {
139 0xFF, 0xFF, 0xFF, 0xFF,
140 0xFF, 0xFF, 0xFF, 0xFF,
141 0xFF, 0xFF, 0xFF, 0xFF,
142 0xFF, 0xFF, 0xFF, 0xFF
145 EDKII_ATA_ATAPI_POLICY_PROTOCOL
*mAtaAtapiPolicy
;
146 EDKII_ATA_ATAPI_POLICY_PROTOCOL mDefaultAtaAtapiPolicy
= {
147 EDKII_ATA_ATAPI_POLICY_VERSION
,
149 0, // DeviceSleepEnable
150 0, // AggressiveDeviceSleepEnable
155 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
156 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
157 and the non-blocking I/O functionality is optional.
159 @param[in] Port The port number of the ATA device to send the command.
160 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
161 If there is no port multiplier, then specify 0xFFFF.
162 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
163 and PortMultiplierPort.
164 @param[in] Instance Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE.
165 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
166 used by non-blocking mode.
168 @retval EFI_SUCCESS The ATA command was sent by the host. For
169 bi-directional commands, InTransferLength bytes
170 were transferred from InDataBuffer. For
171 write and bi-directional commands, OutTransferLength
172 bytes were transferred by OutDataBuffer.
173 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
174 of bytes that could be transferred is returned
175 in InTransferLength. For write and bi-directional
176 commands, OutTransferLength bytes were transferred
178 @retval EFI_NOT_READY The ATA command could not be sent because
179 there are too many ATA commands already
180 queued. The caller may retry again later.
181 @retval EFI_DEVICE_ERROR A device error occurred while attempting
182 to send the ATA command.
183 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents
184 of Acb are invalid. The ATA command was
185 not sent, so no additional status information
191 AtaPassThruPassThruExecute (
193 IN UINT16 PortMultiplierPort
,
194 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
195 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
196 IN ATA_NONBLOCK_TASK
*Task OPTIONAL
199 EFI_ATA_PASS_THRU_CMD_PROTOCOL Protocol
;
200 EFI_ATA_HC_WORK_MODE Mode
;
203 Protocol
= Packet
->Protocol
;
205 Mode
= Instance
->Mode
;
209 // Reassign IDE mode io port registers' base addresses
211 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
213 if (EFI_ERROR (Status
)) {
218 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
219 Status
= AtaNonDataCommandIn (
221 &Instance
->IdeRegisters
[Port
],
228 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
229 Status
= AtaPioDataInOut (
231 &Instance
->IdeRegisters
[Port
],
232 Packet
->InDataBuffer
,
233 Packet
->InTransferLength
,
241 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
242 Status
= AtaPioDataInOut (
244 &Instance
->IdeRegisters
[Port
],
245 Packet
->OutDataBuffer
,
246 Packet
->OutTransferLength
,
254 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
255 Status
= AtaUdmaInOut (
257 &Instance
->IdeRegisters
[Port
],
259 Packet
->InDataBuffer
,
260 Packet
->InTransferLength
,
267 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
268 Status
= AtaUdmaInOut (
270 &Instance
->IdeRegisters
[Port
],
272 Packet
->OutDataBuffer
,
273 Packet
->OutTransferLength
,
281 return EFI_UNSUPPORTED
;
284 case EfiAtaAhciMode
:
285 if (PortMultiplierPort
== 0xFFFF) {
287 // If there is no port multiplier, PortMultiplierPort will be 0xFFFF
288 // according to UEFI spec. Here, we convert its value to 0 to follow
291 PortMultiplierPort
= 0;
294 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
295 Status
= AhciNonDataTransfer (
297 &Instance
->AhciRegisters
,
299 (UINT8
)PortMultiplierPort
,
308 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
309 Status
= AhciPioTransfer (
311 &Instance
->AhciRegisters
,
313 (UINT8
)PortMultiplierPort
,
319 Packet
->InDataBuffer
,
320 Packet
->InTransferLength
,
325 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
326 Status
= AhciPioTransfer (
328 &Instance
->AhciRegisters
,
330 (UINT8
)PortMultiplierPort
,
336 Packet
->OutDataBuffer
,
337 Packet
->OutTransferLength
,
342 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
343 Status
= AhciDmaTransfer (
345 &Instance
->AhciRegisters
,
347 (UINT8
)PortMultiplierPort
,
353 Packet
->InDataBuffer
,
354 Packet
->InTransferLength
,
359 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
360 Status
= AhciDmaTransfer (
362 &Instance
->AhciRegisters
,
364 (UINT8
)PortMultiplierPort
,
370 Packet
->OutDataBuffer
,
371 Packet
->OutTransferLength
,
377 return EFI_UNSUPPORTED
;
382 Status
= EFI_DEVICE_ERROR
;
390 Call back function when the timer event is signaled.
392 @param[in] Event The Event this notify function registered to.
393 @param[in] Context Pointer to the context data registered to the
399 AsyncNonBlockingTransferRoutine (
405 LIST_ENTRY
*EntryHeader
;
406 ATA_NONBLOCK_TASK
*Task
;
408 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
410 Instance
= (ATA_ATAPI_PASS_THRU_INSTANCE
*) Context
;
411 EntryHeader
= &Instance
->NonBlockingTaskList
;
413 // Get the Taks from the Taks List and execute it, until there is
414 // no task in the list or the device is busy with task (EFI_NOT_READY).
417 if (!IsListEmpty (EntryHeader
)) {
418 Entry
= GetFirstNode (EntryHeader
);
419 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (Entry
);
424 Status
= AtaPassThruPassThruExecute (
426 Task
->PortMultiplier
,
433 // If the data transfer meet a error, remove all tasks in the list since these tasks are
434 // associated with one task from Ata Bus and signal the event with error status.
436 if ((Status
!= EFI_NOT_READY
) && (Status
!= EFI_SUCCESS
)) {
437 DestroyAsynTaskList (Instance
, TRUE
);
442 // For Non blocking mode, the Status of EFI_NOT_READY means the operation
443 // is not finished yet. Otherwise the operation is successful.
445 if (Status
== EFI_NOT_READY
) {
448 RemoveEntryList (&Task
->Link
);
449 gBS
->SignalEvent (Task
->Event
);
456 The Entry Point of module.
458 @param[in] ImageHandle The firmware allocated handle for the EFI image.
459 @param[in] SystemTable A pointer to the EFI System Table.
461 @retval EFI_SUCCESS The entry point is executed successfully.
462 @retval other Some error occurs when executing this entry point.
467 InitializeAtaAtapiPassThru (
468 IN EFI_HANDLE ImageHandle
,
469 IN EFI_SYSTEM_TABLE
*SystemTable
475 // Install driver model protocol(s).
477 Status
= EfiLibInstallDriverBindingComponentName2 (
480 &gAtaAtapiPassThruDriverBinding
,
482 &gAtaAtapiPassThruComponentName
,
483 &gAtaAtapiPassThruComponentName2
485 ASSERT_EFI_ERROR (Status
);
491 Tests to see if this driver supports a given controller. If a child device is provided,
492 it further tests to see if this driver supports creating a handle for the specified child device.
494 This function checks to see if the driver specified by This supports the device specified by
495 ControllerHandle. Drivers will typically use the device path attached to
496 ControllerHandle and/or the services from the bus I/O abstraction attached to
497 ControllerHandle to determine if the driver supports ControllerHandle. This function
498 may be called many times during platform initialization. In order to reduce boot times, the tests
499 performed by this function must be very small, and take as little time as possible to execute. This
500 function must not change the state of any hardware devices, and this function must be aware that the
501 device specified by ControllerHandle may already be managed by the same driver or a
502 different driver. This function must match its calls to AllocatePages() with FreePages(),
503 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
504 Because ControllerHandle may have been previously started by the same driver, if a protocol is
505 already in the opened state, then it must not be closed with CloseProtocol(). This is required
506 to guarantee the state of ControllerHandle is not modified by this function.
508 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
509 @param[in] ControllerHandle The handle of the controller to test. This handle
510 must support a protocol interface that supplies
511 an I/O abstraction to the driver.
512 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
513 parameter is ignored by device drivers, and is optional for bus
514 drivers. For bus drivers, if this parameter is not NULL, then
515 the bus driver must determine if the bus controller specified
516 by ControllerHandle and the child controller specified
517 by RemainingDevicePath are both supported by this
520 @retval EFI_SUCCESS The device specified by ControllerHandle and
521 RemainingDevicePath is supported by the driver specified by This.
522 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
523 RemainingDevicePath is already being managed by the driver
525 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
526 RemainingDevicePath is already being managed by a different
527 driver or an application that requires exclusive access.
528 Currently not implemented.
529 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
530 RemainingDevicePath is not supported by the driver specified by This.
534 AtaAtapiPassThruSupported (
535 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
536 IN EFI_HANDLE Controller
,
537 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
541 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
542 EFI_PCI_IO_PROTOCOL
*PciIo
;
544 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
547 // SATA Controller is a device driver, and should ingore the
548 // "RemainingDevicePath" according to UEFI spec
550 Status
= gBS
->OpenProtocol (
552 &gEfiDevicePathProtocolGuid
,
553 (VOID
*) &ParentDevicePath
,
554 This
->DriverBindingHandle
,
556 EFI_OPEN_PROTOCOL_BY_DRIVER
558 if (EFI_ERROR (Status
)) {
560 // EFI_ALREADY_STARTED is also an error
565 // Close the protocol because we don't use it here
569 &gEfiDevicePathProtocolGuid
,
570 This
->DriverBindingHandle
,
574 Status
= gBS
->OpenProtocol (
576 &gEfiIdeControllerInitProtocolGuid
,
577 (VOID
**) &IdeControllerInit
,
578 This
->DriverBindingHandle
,
580 EFI_OPEN_PROTOCOL_BY_DRIVER
583 if (EFI_ERROR (Status
)) {
585 // EFI_ALREADY_STARTED is also an error
591 // Close the I/O Abstraction(s) used to perform the supported test
595 &gEfiIdeControllerInitProtocolGuid
,
596 This
->DriverBindingHandle
,
601 // Now test the EfiPciIoProtocol
603 Status
= gBS
->OpenProtocol (
605 &gEfiPciIoProtocolGuid
,
607 This
->DriverBindingHandle
,
609 EFI_OPEN_PROTOCOL_GET_PROTOCOL
611 if (EFI_ERROR (Status
)) {
615 // Now further check the PCI header: Base class (offset 0x0B) and
616 // Sub Class (offset 0x0A). This controller should be an ATA controller
618 Status
= PciIo
->Pci
.Read (
621 PCI_CLASSCODE_OFFSET
,
622 sizeof (PciData
.Hdr
.ClassCode
),
623 PciData
.Hdr
.ClassCode
625 if (EFI_ERROR (Status
)) {
626 return EFI_UNSUPPORTED
;
629 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
633 return EFI_UNSUPPORTED
;
637 Starts a device controller or a bus controller.
639 The Start() function is designed to be invoked from the EFI boot service ConnectController().
640 As a result, much of the error checking on the parameters to Start() has been moved into this
641 common boot service. It is legal to call Start() from other locations,
642 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
643 1. ControllerHandle must be a valid EFI_HANDLE.
644 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
645 EFI_DEVICE_PATH_PROTOCOL.
646 3. Prior to calling Start(), the Supported() function for the driver specified by This must
647 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
649 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
650 @param[in] ControllerHandle The handle of the controller to start. This handle
651 must support a protocol interface that supplies
652 an I/O abstraction to the driver.
653 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
654 parameter is ignored by device drivers, and is optional for bus
655 drivers. For a bus driver, if this parameter is NULL, then handles
656 for all the children of Controller are created by this driver.
657 If this parameter is not NULL and the first Device Path Node is
658 not the End of Device Path Node, then only the handle for the
659 child device specified by the first Device Path Node of
660 RemainingDevicePath is created by this driver.
661 If the first Device Path Node of RemainingDevicePath is
662 the End of Device Path Node, no child handle is created by this
665 @retval EFI_SUCCESS The device was started.
666 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
667 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
668 @retval Others The driver failded to start the device.
673 AtaAtapiPassThruStart (
674 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
675 IN EFI_HANDLE Controller
,
676 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
680 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
681 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
682 EFI_PCI_IO_PROTOCOL
*PciIo
;
683 UINT64 EnabledPciAttributes
;
684 UINT64 OriginalPciAttributes
;
686 Status
= EFI_SUCCESS
;
687 IdeControllerInit
= NULL
;
689 OriginalPciAttributes
= 0;
691 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Start== Controller = %x\n", Controller
));
693 Status
= gBS
->OpenProtocol (
695 &gEfiIdeControllerInitProtocolGuid
,
696 (VOID
**) &IdeControllerInit
,
697 This
->DriverBindingHandle
,
699 EFI_OPEN_PROTOCOL_BY_DRIVER
702 if (EFI_ERROR (Status
)) {
703 DEBUG ((EFI_D_ERROR
, "Open Ide_Controller_Init Error, Status=%r", Status
));
707 Status
= gBS
->OpenProtocol (
709 &gEfiPciIoProtocolGuid
,
711 This
->DriverBindingHandle
,
713 EFI_OPEN_PROTOCOL_GET_PROTOCOL
715 if (EFI_ERROR (Status
)) {
716 DEBUG ((EFI_D_ERROR
, "Get Pci_Io Protocol Error, Status=%r", Status
));
720 Status
= PciIo
->Attributes (
722 EfiPciIoAttributeOperationGet
,
724 &OriginalPciAttributes
727 if (EFI_ERROR (Status
)) {
731 Status
= PciIo
->Attributes (
733 EfiPciIoAttributeOperationSupported
,
735 &EnabledPciAttributes
737 if (!EFI_ERROR (Status
)) {
738 EnabledPciAttributes
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
739 Status
= PciIo
->Attributes (
741 EfiPciIoAttributeOperationEnable
,
742 EnabledPciAttributes
,
747 if (EFI_ERROR (Status
)) {
751 Status
= gBS
->LocateProtocol (&gEdkiiAtaAtapiPolicyProtocolGuid
, NULL
, (VOID
**)&mAtaAtapiPolicy
);
752 if (EFI_ERROR (Status
)) {
754 // If there is no AtaAtapiPolicy exposed, use the default policy.
756 mAtaAtapiPolicy
= &mDefaultAtaAtapiPolicy
;
760 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
762 Instance
= AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE
), &gAtaAtapiPassThruInstanceTemplate
);
763 if (Instance
== NULL
) {
767 Instance
->ControllerHandle
= Controller
;
768 Instance
->IdeControllerInit
= IdeControllerInit
;
769 Instance
->PciIo
= PciIo
;
770 Instance
->EnabledPciAttributes
= EnabledPciAttributes
;
771 Instance
->OriginalPciAttributes
= OriginalPciAttributes
;
772 Instance
->AtaPassThru
.Mode
= &Instance
->AtaPassThruMode
;
773 Instance
->ExtScsiPassThru
.Mode
= &Instance
->ExtScsiPassThruMode
;
774 InitializeListHead(&Instance
->DeviceList
);
775 InitializeListHead(&Instance
->NonBlockingTaskList
);
777 Instance
->TimerEvent
= NULL
;
779 Status
= gBS
->CreateEvent (
780 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
782 AsyncNonBlockingTransferRoutine
,
784 &Instance
->TimerEvent
786 if (EFI_ERROR (Status
)) {
793 Status
= gBS
->SetTimer (Instance
->TimerEvent
, TimerPeriodic
, 10000);
794 if (EFI_ERROR (Status
)) {
799 // Enumerate all inserted ATA devices.
801 Status
= EnumerateAttachedDevice (Instance
);
802 if (EFI_ERROR (Status
)) {
806 Status
= gBS
->InstallMultipleProtocolInterfaces (
808 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
809 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
812 ASSERT_EFI_ERROR (Status
);
817 if (IdeControllerInit
!= NULL
) {
820 &gEfiIdeControllerInitProtocolGuid
,
821 This
->DriverBindingHandle
,
826 if ((Instance
!= NULL
) && (Instance
->TimerEvent
!= NULL
)) {
827 gBS
->CloseEvent (Instance
->TimerEvent
);
830 if (Instance
!= NULL
) {
832 // Remove all inserted ATA devices.
834 DestroyDeviceInfoList (Instance
);
837 return EFI_UNSUPPORTED
;
841 Stops a device controller or a bus controller.
843 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
844 As a result, much of the error checking on the parameters to Stop() has been moved
845 into this common boot service. It is legal to call Stop() from other locations,
846 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
847 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
848 same driver's Start() function.
849 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
850 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
851 Start() function, and the Start() function must have called OpenProtocol() on
852 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
854 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
855 @param[in] ControllerHandle A handle to the device being stopped. The handle must
856 support a bus specific I/O protocol for the driver
857 to use to stop the device.
858 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
859 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
860 if NumberOfChildren is 0.
862 @retval EFI_SUCCESS The device was stopped.
863 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
868 AtaAtapiPassThruStop (
869 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
870 IN EFI_HANDLE Controller
,
871 IN UINTN NumberOfChildren
,
872 IN EFI_HANDLE
*ChildHandleBuffer
876 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
877 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
878 EFI_PCI_IO_PROTOCOL
*PciIo
;
879 EFI_AHCI_REGISTERS
*AhciRegisters
;
881 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller
));
883 Status
= gBS
->OpenProtocol (
885 &gEfiAtaPassThruProtocolGuid
,
886 (VOID
**) &AtaPassThru
,
887 This
->DriverBindingHandle
,
889 EFI_OPEN_PROTOCOL_GET_PROTOCOL
892 if (EFI_ERROR (Status
)) {
893 return EFI_DEVICE_ERROR
;
896 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru
);
898 Status
= gBS
->UninstallMultipleProtocolInterfaces (
900 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
901 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
905 if (EFI_ERROR (Status
)) {
906 return EFI_DEVICE_ERROR
;
910 // Close protocols opened by AtaAtapiPassThru controller driver
914 &gEfiIdeControllerInitProtocolGuid
,
915 This
->DriverBindingHandle
,
920 // Close Non-Blocking timer and free Task list.
922 if (Instance
->TimerEvent
!= NULL
) {
923 gBS
->CloseEvent (Instance
->TimerEvent
);
924 Instance
->TimerEvent
= NULL
;
926 DestroyAsynTaskList (Instance
, FALSE
);
928 // Free allocated resource
930 DestroyDeviceInfoList (Instance
);
932 PciIo
= Instance
->PciIo
;
935 // Disable this ATA host controller.
939 EfiPciIoAttributeOperationDisable
,
940 Instance
->EnabledPciAttributes
,
945 // If the current working mode is AHCI mode, then pre-allocated resource
946 // for AHCI initialization should be released.
948 if (Instance
->Mode
== EfiAtaAhciMode
) {
949 AhciRegisters
= &Instance
->AhciRegisters
;
952 AhciRegisters
->MapCommandTable
956 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandTableSize
),
957 AhciRegisters
->AhciCommandTable
961 AhciRegisters
->MapCmdList
965 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandListSize
),
966 AhciRegisters
->AhciCmdList
970 AhciRegisters
->MapRFis
974 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxReceiveFisSize
),
975 AhciRegisters
->AhciRFis
980 // Restore original PCI attributes
982 Status
= PciIo
->Attributes (
984 EfiPciIoAttributeOperationSet
,
985 Instance
->OriginalPciAttributes
,
988 ASSERT_EFI_ERROR (Status
);
996 Traverse the attached ATA devices list to find out the device to access.
998 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
999 @param[in] Port The port number of the ATA device to send the command.
1000 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1001 If there is no port multiplier, then specify 0xFFFF.
1002 @param[in] DeviceType The device type of the ATA device.
1004 @retval The pointer to the data structure of the device info to access.
1009 SearchDeviceInfoList (
1010 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1012 IN UINT16 PortMultiplier
,
1013 IN EFI_ATA_DEVICE_TYPE DeviceType
1016 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1019 Node
= GetFirstNode (&Instance
->DeviceList
);
1020 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1021 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1024 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
1025 // the PortMultiplier information. If the CD-ROM is directly attached
1026 // on a SATA port, the PortMultiplier should be translated from 0xFF
1027 // to 0xFFFF according to the UEFI spec.
1029 if ((Instance
->Mode
== EfiAtaAhciMode
) &&
1030 (DeviceInfo
->Type
== EfiIdeCdrom
) &&
1031 (PortMultiplier
== 0xFF)) {
1032 PortMultiplier
= 0xFFFF;
1035 if ((DeviceInfo
->Type
== DeviceType
) &&
1036 (Port
== DeviceInfo
->Port
) &&
1037 (PortMultiplier
== DeviceInfo
->PortMultiplier
)) {
1041 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1048 Allocate device info data structure to contain device info.
1049 And insert the data structure to the tail of device list for tracing.
1051 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1052 @param[in] Port The port number of the ATA device to send the command.
1053 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1054 If there is no port multiplier, then specify 0xFFFF.
1055 @param[in] DeviceType The device type of the ATA device.
1056 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1058 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1059 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1064 CreateNewDeviceInfo (
1065 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1067 IN UINT16 PortMultiplier
,
1068 IN EFI_ATA_DEVICE_TYPE DeviceType
,
1069 IN EFI_IDENTIFY_DATA
*IdentifyData
1072 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1074 DeviceInfo
= AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO
));
1076 if (DeviceInfo
== NULL
) {
1077 return EFI_OUT_OF_RESOURCES
;
1080 DeviceInfo
->Signature
= ATA_ATAPI_DEVICE_SIGNATURE
;
1081 DeviceInfo
->Port
= Port
;
1082 DeviceInfo
->PortMultiplier
= PortMultiplier
;
1083 DeviceInfo
->Type
= DeviceType
;
1085 if (IdentifyData
!= NULL
) {
1086 DeviceInfo
->IdentifyData
= AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA
), IdentifyData
);
1087 if (DeviceInfo
->IdentifyData
== NULL
) {
1088 FreePool (DeviceInfo
);
1089 return EFI_OUT_OF_RESOURCES
;
1093 InsertTailList (&Instance
->DeviceList
, &DeviceInfo
->Link
);
1099 Destroy all attached ATA devices info.
1101 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1106 DestroyDeviceInfoList (
1107 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1110 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1113 Node
= GetFirstNode (&Instance
->DeviceList
);
1114 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1115 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1117 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1119 RemoveEntryList (&DeviceInfo
->Link
);
1120 if (DeviceInfo
->IdentifyData
!= NULL
) {
1121 FreePool (DeviceInfo
->IdentifyData
);
1123 FreePool (DeviceInfo
);
1128 Destroy all pending non blocking tasks.
1130 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1131 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1137 DestroyAsynTaskList (
1138 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1139 IN BOOLEAN IsSigEvent
1143 LIST_ENTRY
*DelEntry
;
1144 ATA_NONBLOCK_TASK
*Task
;
1147 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1148 if (!IsListEmpty (&Instance
->NonBlockingTaskList
)) {
1150 // Free the Subtask list.
1152 for (Entry
= (&Instance
->NonBlockingTaskList
)->ForwardLink
;
1153 Entry
!= (&Instance
->NonBlockingTaskList
);
1156 Entry
= Entry
->ForwardLink
;
1157 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry
);
1159 RemoveEntryList (DelEntry
);
1161 Task
->Packet
->Asb
->AtaStatus
= 0x01;
1162 gBS
->SignalEvent (Task
->Event
);
1167 gBS
->RestoreTPL (OldTpl
);
1171 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1173 The function is designed to enumerate all attached ATA devices.
1175 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1177 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1178 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1183 EnumerateAttachedDevice (
1184 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1191 Status
= EFI_SUCCESS
;
1193 Status
= Instance
->PciIo
->Pci
.Read (
1196 PCI_CLASSCODE_OFFSET
,
1197 sizeof (PciData
.Hdr
.ClassCode
),
1198 PciData
.Hdr
.ClassCode
1200 ASSERT_EFI_ERROR (Status
);
1202 ClassCode
= PciData
.Hdr
.ClassCode
[1];
1204 switch (ClassCode
) {
1205 case PCI_CLASS_MASS_STORAGE_IDE
:
1207 // The ATA controller is working at IDE mode
1209 Instance
->Mode
= EfiAtaIdeMode
;
1211 Status
= IdeModeInitialization (Instance
);
1212 if (EFI_ERROR (Status
)) {
1213 Status
= EFI_DEVICE_ERROR
;
1217 case PCI_CLASS_MASS_STORAGE_SATADPA
:
1219 // The ATA controller is working at AHCI mode
1221 Instance
->Mode
= EfiAtaAhciMode
;
1223 Status
= AhciModeInitialization (Instance
);
1225 if (EFI_ERROR (Status
)) {
1226 Status
= EFI_DEVICE_ERROR
;
1232 Status
= EFI_UNSUPPORTED
;
1240 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1241 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1242 and the non-blocking I/O functionality is optional.
1244 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1245 @param[in] Port The port number of the ATA device to send the command.
1246 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1247 If there is no port multiplier, then specify 0xFFFF.
1248 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1249 and PortMultiplierPort.
1250 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1251 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1252 Event is not NULL and non blocking I/O is supported, then non-blocking
1253 I/O is performed, and Event will be signaled when the ATA command completes.
1255 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1256 InTransferLength bytes were transferred from InDataBuffer. For write and
1257 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1258 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1259 is returned in InTransferLength. For write and bi-directional commands,
1260 OutTransferLength bytes were transferred by OutDataBuffer.
1261 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1262 already queued. The caller may retry again later.
1263 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1264 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1265 command was not sent, so no additional status information is available.
1270 AtaPassThruPassThru (
1271 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1273 IN UINT16 PortMultiplierPort
,
1274 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
1275 IN EFI_EVENT Event OPTIONAL
1278 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1280 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1281 EFI_IDENTIFY_DATA
*IdentifyData
;
1283 UINT32 MaxSectorCount
;
1284 ATA_NONBLOCK_TASK
*Task
;
1288 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1290 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1291 return EFI_INVALID_PARAMETER
;
1294 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1295 return EFI_INVALID_PARAMETER
;
1298 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->Asb
, This
->Mode
->IoAlign
)) {
1299 return EFI_INVALID_PARAMETER
;
1302 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1305 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeCdrom
);
1307 return EFI_INVALID_PARAMETER
;
1312 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1313 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1314 // If bit10 is one, it means the ata device support 48-bit addressing.
1316 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1317 IdentifyData
= DeviceInfo
->IdentifyData
;
1318 MaxSectorCount
= 0x100;
1319 if ((IdentifyData
->AtaData
.command_set_supported_83
& (BIT10
| BIT15
| BIT14
)) == 0x4400) {
1320 Capacity
= *((UINT64
*)IdentifyData
->AtaData
.maximum_lba_for_48bit_addressing
);
1321 if (Capacity
> 0xFFFFFFF) {
1323 // Capacity exceeds 120GB. 48-bit addressing is really needed
1324 // In this case, the max sector count is 0x10000
1326 MaxSectorCount
= 0x10000;
1331 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& (BIT14
| BIT15
)) == BIT14
) {
1333 // Check logical block size
1335 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& BIT12
) != 0) {
1336 BlockSize
= (UINT32
) (((IdentifyData
->AtaData
.logic_sector_size_hi
<< 16) | IdentifyData
->AtaData
.logic_sector_size_lo
) * sizeof (UINT16
));
1341 // convert the transfer length from sector count to byte.
1343 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1344 (Packet
->InTransferLength
!= 0)) {
1345 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
1349 // convert the transfer length from sector count to byte.
1351 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1352 (Packet
->OutTransferLength
!= 0)) {
1353 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
1357 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1358 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1361 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
1362 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
))) {
1363 return EFI_BAD_BUFFER_SIZE
;
1367 // For non-blocking mode, queue the Task into the list.
1369 if (Event
!= NULL
) {
1370 Task
= AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK
));
1372 return EFI_OUT_OF_RESOURCES
;
1375 Task
->Signature
= ATA_NONBLOCKING_TASK_SIGNATURE
;
1377 Task
->PortMultiplier
= PortMultiplierPort
;
1378 Task
->Packet
= Packet
;
1379 Task
->Event
= Event
;
1380 Task
->IsStart
= FALSE
;
1381 Task
->RetryTimes
= DivU64x32(Packet
->Timeout
, 1000) + 1;
1382 if (Packet
->Timeout
== 0) {
1383 Task
->InfiniteWait
= TRUE
;
1385 Task
->InfiniteWait
= FALSE
;
1388 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1389 InsertTailList (&Instance
->NonBlockingTaskList
, &Task
->Link
);
1390 gBS
->RestoreTPL (OldTpl
);
1394 return AtaPassThruPassThruExecute (
1405 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1406 These can either be the list of ports where ATA devices are actually present or the
1407 list of legal port numbers for the ATA controller. Regardless, the caller of this
1408 function must probe the port number returned to see if an ATA device is actually
1409 present at that location on the ATA controller.
1411 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1412 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1413 in Port and EFI_SUCCESS is returned.
1415 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1416 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1417 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1418 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1420 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1423 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1424 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1425 On output, a pointer to the next port number on the ATA
1426 controller. An input value of 0xFFFF retrieves the first port
1427 number on the ATA controller.
1429 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1430 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1431 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1437 AtaPassThruGetNextPort (
1438 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1442 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1444 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1446 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1449 return EFI_INVALID_PARAMETER
;
1452 if (*Port
== 0xFFFF) {
1454 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1456 Node
= GetFirstNode (&Instance
->DeviceList
);
1458 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1459 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1461 if (DeviceInfo
->Type
== EfiIdeHarddisk
) {
1462 *Port
= DeviceInfo
->Port
;
1466 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1469 return EFI_NOT_FOUND
;
1470 } else if (*Port
== Instance
->PreviousPort
) {
1471 Node
= GetFirstNode (&Instance
->DeviceList
);
1473 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1474 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1476 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1477 (DeviceInfo
->Port
> *Port
)){
1478 *Port
= DeviceInfo
->Port
;
1482 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1485 return EFI_NOT_FOUND
;
1488 // Port is not equal to 0xFFFF and also not equal to previous return value
1490 return EFI_INVALID_PARAMETER
;
1495 // Update the PreviousPort and PreviousPortMultiplier.
1497 Instance
->PreviousPort
= *Port
;
1503 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1504 controller. These can either be the list of port multiplier ports where ATA devices are actually
1505 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1506 function must probe the port number and port multiplier port number returned to see if an ATA
1507 device is actually present.
1509 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1510 present on a port of an ATA controller.
1512 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1513 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1514 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1517 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1518 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1519 EFI_SUCCESS is returned.
1521 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1522 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1525 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1526 the ATA controller, then EFI_NOT_FOUND is returned.
1528 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1529 @param[in] Port The port number present on the ATA controller.
1530 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1531 ATA device present on the ATA controller.
1532 If on input a PortMultiplierPort of 0xFFFF is specified,
1533 then the port multiplier port number of the first ATA device
1534 is returned. On output, a pointer to the port multiplier port
1535 number of the next ATA device present on an ATA controller.
1537 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1538 of the ATA controller was returned in PortMultiplierPort.
1539 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1540 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1541 returned on a previous call to GetNextDevice().
1546 AtaPassThruGetNextDevice (
1547 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1549 IN OUT UINT16
*PortMultiplierPort
1552 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1554 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1556 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1558 if (PortMultiplierPort
== NULL
) {
1559 return EFI_INVALID_PARAMETER
;
1562 if (Instance
->PreviousPortMultiplier
== 0xFFFF) {
1564 // If a device is directly attached on a port, previous call to this
1565 // function will return the value 0xFFFF for PortMultiplierPort. In
1566 // this case, there should be no more device on the port multiplier.
1568 Instance
->PreviousPortMultiplier
= 0;
1569 return EFI_NOT_FOUND
;
1572 if (*PortMultiplierPort
== Instance
->PreviousPortMultiplier
) {
1573 Node
= GetFirstNode (&Instance
->DeviceList
);
1575 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1576 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1578 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1579 (DeviceInfo
->Port
== Port
) &&
1580 (DeviceInfo
->PortMultiplier
> *PortMultiplierPort
)){
1581 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1585 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1588 return EFI_NOT_FOUND
;
1589 } else if (*PortMultiplierPort
== 0xFFFF) {
1591 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1593 Node
= GetFirstNode (&Instance
->DeviceList
);
1595 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1596 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1598 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1599 (DeviceInfo
->Port
== Port
)){
1600 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1604 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1607 return EFI_NOT_FOUND
;
1610 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1612 return EFI_INVALID_PARAMETER
;
1617 // Update the PreviousPort and PreviousPortMultiplier.
1619 Instance
->PreviousPortMultiplier
= *PortMultiplierPort
;
1625 Used to allocate and build a device path node for an ATA device on an ATA controller.
1627 The BuildDevicePath() function allocates and builds a single device node for the ATA
1628 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1629 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1630 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1631 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1633 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1634 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1635 and EFI_SUCCESS is returned.
1637 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1638 @param[in] Port Port specifies the port number of the ATA device for which a
1639 device path node is to be allocated and built.
1640 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1641 device path node is to be allocated and built. If there is no
1642 port multiplier, then specify 0xFFFF.
1643 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1644 device specified by Port and PortMultiplierPort. This function
1645 is responsible for allocating the buffer DevicePath with the
1646 boot service AllocatePool(). It is the caller's responsibility
1647 to free DevicePath when the caller is finished with DevicePath.
1648 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1649 Port and PortMultiplierPort was allocated and returned in DevicePath.
1650 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1651 exist on the ATA controller.
1652 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1653 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1658 AtaPassThruBuildDevicePath (
1659 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1661 IN UINT16 PortMultiplierPort
,
1662 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1665 EFI_DEV_PATH
*DevicePathNode
;
1666 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1669 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1672 // Validate parameters passed in.
1674 if (DevicePath
== NULL
) {
1675 return EFI_INVALID_PARAMETER
;
1678 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1680 return EFI_NOT_FOUND
;
1683 if (Instance
->Mode
== EfiAtaIdeMode
) {
1684 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
1685 if (DevicePathNode
== NULL
) {
1686 return EFI_OUT_OF_RESOURCES
;
1688 DevicePathNode
->Atapi
.PrimarySecondary
= (UINT8
) Port
;
1689 DevicePathNode
->Atapi
.SlaveMaster
= (UINT8
) PortMultiplierPort
;
1690 DevicePathNode
->Atapi
.Lun
= 0;
1692 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
1693 if (DevicePathNode
== NULL
) {
1694 return EFI_OUT_OF_RESOURCES
;
1697 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
1698 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplierPort
;
1699 DevicePathNode
->Sata
.Lun
= 0;
1702 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
1708 Used to translate a device path node to a port number and port multiplier port number.
1710 The GetDevice() function determines the port and port multiplier port number associated with
1711 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1712 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1713 DevicePath into a port number and port multiplier port number.
1715 If this translation is successful, then that port number and port multiplier port number are returned
1716 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1718 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1720 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1721 EFI_UNSUPPORTED is returned.
1723 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1724 a valid translation from DevicePath to a port number and port multiplier port number, then
1725 EFI_NOT_FOUND is returned.
1727 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1728 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1730 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1731 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1732 on the ATA controller.
1734 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1735 port number, and they were returned in Port and PortMultiplierPort.
1736 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1737 @retval EFI_INVALID_PARAMETER Port is NULL.
1738 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1739 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1740 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1741 port number does not exist.
1745 AtaPassThruGetDevice (
1746 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1747 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1749 OUT UINT16
*PortMultiplierPort
1752 EFI_DEV_PATH
*DevicePathNode
;
1753 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1756 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1759 // Validate parameters passed in.
1761 if (DevicePath
== NULL
|| Port
== NULL
|| PortMultiplierPort
== NULL
) {
1762 return EFI_INVALID_PARAMETER
;
1766 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1768 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1769 ((DevicePath
->SubType
!= MSG_SATA_DP
) &&
1770 (DevicePath
->SubType
!= MSG_ATAPI_DP
)) ||
1771 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
1772 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
1773 return EFI_UNSUPPORTED
;
1776 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
1778 if (Instance
->Mode
== EfiAtaIdeMode
) {
1779 *Port
= DevicePathNode
->Atapi
.PrimarySecondary
;
1780 *PortMultiplierPort
= DevicePathNode
->Atapi
.SlaveMaster
;
1782 *Port
= DevicePathNode
->Sata
.HBAPortNumber
;
1783 *PortMultiplierPort
= DevicePathNode
->Sata
.PortMultiplierPortNumber
;
1786 Node
= SearchDeviceInfoList(Instance
, *Port
, *PortMultiplierPort
, EfiIdeHarddisk
);
1789 return EFI_NOT_FOUND
;
1796 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1797 connected to the port.
1799 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1800 resets all the ATA devices connected to that port. If this ATA controller does not support
1801 a reset port operation, then EFI_UNSUPPORTED is returned.
1803 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1806 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1808 If the port reset operation is completed, then EFI_SUCCESS is returned.
1810 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1811 @param[in] Port The port number on the ATA controller.
1813 @retval EFI_SUCCESS The ATA controller port was reset.
1814 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1815 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1816 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1821 AtaPassThruResetPort (
1822 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1827 // Return success directly then upper layer driver could think reset port operation is done.
1833 Resets an ATA device that is connected to an ATA controller.
1835 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1836 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1839 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1840 EFI_INVALID_PARAMETER is returned.
1842 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1845 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1848 If the device 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 Port represents the port number of the ATA device to be reset.
1852 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1853 If there is no port multiplier, then specify 0xFFFF.
1854 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1855 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1856 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1857 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1858 specified by Port and PortMultiplierPort.
1859 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1860 specified by Port and PortMultiplierPort.
1865 AtaPassThruResetDevice (
1866 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1868 IN UINT16 PortMultiplierPort
1871 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1874 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1876 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1879 return EFI_INVALID_PARAMETER
;
1883 // Return success directly then upper layer driver could think reset device operation is done.
1889 Submit ATAPI request sense command.
1891 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1892 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1893 the id of the SCSI device to send the SCSI Request Packet. Each
1894 transport driver may choose to utilize a subset of this size to suit the needs
1895 of transport target representation. For example, a Fibre Channel driver
1896 may use only 8 bytes (WWN) to represent an FC target.
1897 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1898 @param[in] SenseData A pointer to store sense data.
1899 @param[in] SenseDataLength The sense data length.
1900 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1902 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1903 @retval EFI_DEVICE_ERROR The device failed to send data.
1908 AtaPacketRequestSense (
1909 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1913 IN UINT8 SenseDataLength
,
1917 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet
;
1921 ZeroMem (&Packet
, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1924 Cdb
[0] = ATA_CMD_REQUEST_SENSE
;
1925 Cdb
[4] = SenseDataLength
;
1927 Packet
.Timeout
= Timeout
;
1929 Packet
.CdbLength
= 12;
1930 Packet
.DataDirection
= EFI_EXT_SCSI_DATA_DIRECTION_READ
;
1931 Packet
.InDataBuffer
= SenseData
;
1932 Packet
.InTransferLength
= SenseDataLength
;
1934 Status
= ExtScsiPassThruPassThru (This
, Target
, Lun
, &Packet
, NULL
);
1940 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1941 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1942 nonblocking I/O functionality is optional.
1944 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1945 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1946 the id of the SCSI device to send the SCSI Request Packet. Each
1947 transport driver may choose to utilize a subset of this size to suit the needs
1948 of transport target representation. For example, a Fibre Channel driver
1949 may use only 8 bytes (WWN) to represent an FC target.
1950 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1951 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1952 specified by Target and Lun.
1953 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1954 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1955 Event is not NULL and non blocking I/O is supported, then
1956 nonblocking I/O is performed, and Event will be signaled when the
1957 SCSI Request Packet completes.
1959 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1960 commands, InTransferLength bytes were transferred from
1961 InDataBuffer. For write and bi-directional commands,
1962 OutTransferLength bytes were transferred by
1964 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
1965 could be transferred is returned in InTransferLength. For write
1966 and bi-directional commands, OutTransferLength bytes were
1967 transferred by OutDataBuffer.
1968 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1969 SCSI Request Packets already queued. The caller may retry again later.
1970 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1972 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
1973 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
1974 by the host adapter. This includes the case of Bi-directional SCSI
1975 commands not supported by the implementation. The SCSI Request
1976 Packet was not sent, so no additional status information is available.
1977 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1982 ExtScsiPassThruPassThru (
1983 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1986 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1987 IN EFI_EVENT Event OPTIONAL
1991 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1993 UINT8 PortMultiplier
;
1994 EFI_ATA_HC_WORK_MODE Mode
;
1996 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1998 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
2000 EFI_STATUS SenseStatus
;
2003 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2005 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
2006 return EFI_INVALID_PARAMETER
;
2010 // Don't support variable length CDB
2012 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
2013 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
2014 return EFI_INVALID_PARAMETER
;
2017 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
2018 return EFI_INVALID_PARAMETER
;
2021 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
2022 return EFI_INVALID_PARAMETER
;
2025 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
2026 return EFI_INVALID_PARAMETER
;
2029 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
2030 return EFI_INVALID_PARAMETER
;
2034 // For ATAPI device, doesn't support multiple LUN device.
2037 return EFI_INVALID_PARAMETER
;
2041 // The layout of Target array:
2042 // ________________________________________________________________________
2043 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2044 // |_____________________|_____________________|_____|______________________|
2045 // | | The port multiplier | | |
2046 // | The port number | port number | N/A | N/A |
2047 // |_____________________|_____________________|_____|______________________|
2049 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2052 PortMultiplier
= Target
[1];
2054 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2056 return EFI_INVALID_PARAMETER
;
2059 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2062 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2063 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2064 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2066 if (*((UINT8
*)Packet
->Cdb
) == ATA_CMD_IDENTIFY_DEVICE
) {
2067 CopyMem (Packet
->InDataBuffer
, DeviceInfo
->IdentifyData
, sizeof (EFI_IDENTIFY_DATA
));
2069 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2071 Packet
->SenseDataLength
= 0;
2075 Mode
= Instance
->Mode
;
2079 // Reassign IDE mode io port registers' base addresses
2081 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
2083 if (EFI_ERROR (Status
)) {
2087 Status
= AtaPacketCommandExecute (Instance
->PciIo
, &Instance
->IdeRegisters
[Port
], Port
, PortMultiplier
, Packet
);
2089 case EfiAtaAhciMode
:
2090 if (PortMultiplier
== 0xFF) {
2092 // If there is no port multiplier, the PortMultiplier will be 0xFF
2093 // Here, we convert its value to 0 to follow the AHCI spec.
2097 Status
= AhciPacketCommandExecute (Instance
->PciIo
, &Instance
->AhciRegisters
, Port
, PortMultiplier
, Packet
);
2100 Status
= EFI_DEVICE_ERROR
;
2105 // If the cmd doesn't get executed correctly, then check sense data.
2107 if (EFI_ERROR (Status
) && (Packet
->SenseDataLength
!= 0) && (*((UINT8
*)Packet
->Cdb
) != ATA_CMD_REQUEST_SENSE
)) {
2108 PtrSenseData
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)), This
->Mode
->IoAlign
);
2109 if (PtrSenseData
== NULL
) {
2110 return EFI_DEVICE_ERROR
;
2113 for (SenseReq
= TRUE
; SenseReq
;) {
2114 SenseStatus
= AtaPacketRequestSense (
2119 sizeof (EFI_SCSI_SENSE_DATA
),
2122 if (EFI_ERROR (SenseStatus
)) {
2126 CopyMem ((UINT8
*)Packet
->SenseData
+ SenseDataLen
, PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
2127 SenseDataLen
+= sizeof (EFI_SCSI_SENSE_DATA
);
2130 // no more sense key or number of sense keys exceeds predefined,
2133 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
2134 (SenseDataLen
+ sizeof (EFI_SCSI_SENSE_DATA
) > Packet
->SenseDataLength
)) {
2138 FreeAlignedPages (PtrSenseData
, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)));
2141 // Update the SenseDataLength field to the data length received.
2143 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2148 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2149 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2150 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2151 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2154 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2155 @param Target On input, a pointer to the Target ID (an array of size
2156 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2157 On output, a pointer to the Target ID (an array of
2158 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2159 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2160 Target array retrieves the Target ID of the first SCSI device present on a
2162 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2163 channel. On output, a pointer to the LUN of the next SCSI device present
2166 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2167 channel was returned in Target and Lun.
2168 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2169 not returned on a previous call to GetNextTargetLun().
2170 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2175 ExtScsiPassThruGetNextTargetLun (
2176 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2177 IN OUT UINT8
**Target
,
2181 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2183 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2187 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2189 if (Target
== NULL
|| Lun
== NULL
) {
2190 return EFI_INVALID_PARAMETER
;
2193 if (*Target
== NULL
) {
2194 return EFI_INVALID_PARAMETER
;
2198 Target16
= (UINT16
*)*Target
;
2200 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2202 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2203 // So the higher bytes in Target array should be 0xFF.
2205 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2206 return EFI_INVALID_PARAMETER
;
2210 // When Target is not all 0xFF's, compare 2 least significant bytes with
2211 // previous target id to see if it is returned by previous call.
2213 if ((*Target16
!= Instance
->PreviousTargetId
) ||
2214 (*Lun
!= Instance
->PreviousLun
)) {
2215 return EFI_INVALID_PARAMETER
;
2219 // Traverse the whole device list to find the next cdrom closed to
2220 // the device signified by Target[0] and Target[1].
2222 // Note that we here use a tricky way to find the next cdrom :
2223 // All ata devices are detected and inserted into the device list
2226 Node
= GetFirstNode (&Instance
->DeviceList
);
2228 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2229 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2231 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2232 ((Target8
[0] < DeviceInfo
->Port
) ||
2233 ((Target8
[0] == DeviceInfo
->Port
) &&
2234 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2235 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2236 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2240 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2243 return EFI_NOT_FOUND
;
2246 // If the array is all 0xFF's, start to traverse the device list from the beginning
2248 Node
= GetFirstNode (&Instance
->DeviceList
);
2249 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2250 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2252 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2253 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2254 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2258 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2261 return EFI_NOT_FOUND
;
2268 // Update the PreviousTargetId.
2270 Instance
->PreviousTargetId
= *Target16
;
2271 Instance
->PreviousLun
= *Lun
;
2277 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2279 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2280 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2281 Target ID of the SCSI device for which a device path node is to be
2282 allocated and built. Transport drivers may chose to utilize a subset of
2283 this size to suit the representation of targets. For example, a Fibre
2284 Channel driver may use only 8 bytes (WWN) in the array to represent a
2286 @param Lun The LUN of the SCSI device for which a device path node is to be
2287 allocated and built.
2288 @param DevicePath A pointer to a single device path node that describes the SCSI device
2289 specified by Target and Lun. This function is responsible for
2290 allocating the buffer DevicePath with the boot service
2291 AllocatePool(). It is the caller's responsibility to free
2292 DevicePath when the caller is finished with DevicePath.
2294 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2295 Target and Lun was allocated and returned in
2297 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2298 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2299 on the SCSI channel.
2300 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2305 ExtScsiPassThruBuildDevicePath (
2306 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2309 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
2312 EFI_DEV_PATH
*DevicePathNode
;
2313 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2315 UINT8 PortMultiplier
;
2317 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2320 PortMultiplier
= Target
[1];
2323 // Validate parameters passed in.
2325 if (DevicePath
== NULL
) {
2326 return EFI_INVALID_PARAMETER
;
2330 // can not build device path for the SCSI Host Controller.
2333 return EFI_NOT_FOUND
;
2336 if (SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
) == NULL
) {
2337 return EFI_NOT_FOUND
;
2340 if (Instance
->Mode
== EfiAtaIdeMode
) {
2341 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
2342 if (DevicePathNode
== NULL
) {
2343 return EFI_OUT_OF_RESOURCES
;
2346 DevicePathNode
->Atapi
.PrimarySecondary
= Port
;
2347 DevicePathNode
->Atapi
.SlaveMaster
= PortMultiplier
;
2348 DevicePathNode
->Atapi
.Lun
= (UINT16
) Lun
;
2350 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
2351 if (DevicePathNode
== NULL
) {
2352 return EFI_OUT_OF_RESOURCES
;
2355 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
2357 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
2358 // the PortMultiplier information. If the CD-ROM is directly attached
2359 // on a SATA port, the PortMultiplier should be translated from 0xFF
2360 // to 0xFFFF according to the UEFI spec.
2362 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplier
== 0xFF ? 0xFFFF : PortMultiplier
;
2363 DevicePathNode
->Sata
.Lun
= (UINT16
) Lun
;
2366 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
2372 Used to translate a device path node to a Target ID and LUN.
2374 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2375 @param DevicePath A pointer to a single device path node that describes the SCSI device
2376 on the SCSI channel.
2377 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2378 on the SCSI channel.
2379 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2381 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2382 LUN, and they were returned in Target and Lun.
2383 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2384 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2386 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2392 ExtScsiPassThruGetTargetLun (
2393 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2394 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2399 EFI_DEV_PATH
*DevicePathNode
;
2400 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2403 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2406 // Validate parameters passed in.
2408 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
2409 return EFI_INVALID_PARAMETER
;
2412 if (*Target
== NULL
) {
2413 return EFI_INVALID_PARAMETER
;
2416 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2418 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
2419 ((DevicePath
->SubType
!= MSG_ATAPI_DP
) &&
2420 (DevicePath
->SubType
!= MSG_SATA_DP
)) ||
2421 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
2422 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
2423 return EFI_UNSUPPORTED
;
2426 SetMem (*Target
, TARGET_MAX_BYTES
, 0xFF);
2428 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
2430 if (Instance
->Mode
== EfiAtaIdeMode
) {
2431 (*Target
)[0] = (UINT8
) DevicePathNode
->Atapi
.PrimarySecondary
;
2432 (*Target
)[1] = (UINT8
) DevicePathNode
->Atapi
.SlaveMaster
;
2433 *Lun
= (UINT8
) DevicePathNode
->Atapi
.Lun
;
2435 (*Target
)[0] = (UINT8
) DevicePathNode
->Sata
.HBAPortNumber
;
2436 (*Target
)[1] = (UINT8
) DevicePathNode
->Sata
.PortMultiplierPortNumber
;
2437 *Lun
= (UINT8
) DevicePathNode
->Sata
.Lun
;
2440 Node
= SearchDeviceInfoList(Instance
, (*Target
)[0], (*Target
)[1], EfiIdeCdrom
);
2443 return EFI_NOT_FOUND
;
2447 return EFI_NOT_FOUND
;
2454 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2456 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2458 @retval EFI_SUCCESS The SCSI channel was reset.
2459 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2460 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2461 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2466 ExtScsiPassThruResetChannel (
2467 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
2471 // Return success directly then upper layer driver could think reset channel operation is done.
2477 Resets a SCSI logical unit that is connected to a SCSI channel.
2479 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2480 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2481 target port ID of the SCSI device containing the SCSI logical unit to
2482 reset. Transport drivers may chose to utilize a subset of this array to suit
2483 the representation of their targets.
2484 @param Lun The LUN of the SCSI device to reset.
2486 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2487 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2488 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2489 specified by Target and Lun.
2490 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2491 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2492 specified by Target and Lun.
2497 ExtScsiPassThruResetTargetLun (
2498 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2503 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2506 UINT8 PortMultiplier
;
2508 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2510 // For ATAPI device, doesn't support multiple LUN device.
2513 return EFI_INVALID_PARAMETER
;
2516 // The layout of Target array:
2517 // ________________________________________________________________________
2518 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2519 // |_____________________|_____________________|_____|______________________|
2520 // | | The port multiplier | | |
2521 // | The port number | port number | N/A | N/A |
2522 // |_____________________|_____________________|_____|______________________|
2524 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2527 PortMultiplier
= Target
[1];
2529 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2531 return EFI_INVALID_PARAMETER
;
2535 // Return success directly then upper layer driver could think reset target LUN operation is done.
2541 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2542 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2543 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2544 see if a SCSI device is actually present at that location on the SCSI channel.
2546 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2547 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2548 On output, a pointer to the Target ID (an array of
2549 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2550 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2551 Target array retrieves the Target ID of the first SCSI device present on a
2554 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2555 channel was returned in Target.
2556 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2557 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2558 returned on a previous call to GetNextTarget().
2559 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2564 ExtScsiPassThruGetNextTarget (
2565 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2566 IN OUT UINT8
**Target
2569 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2571 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2575 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2577 if (Target
== NULL
|| *Target
== NULL
) {
2578 return EFI_INVALID_PARAMETER
;
2582 Target16
= (UINT16
*)*Target
;
2584 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2586 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2587 // So the higher bytes in Target array should be 0xFF.
2589 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2590 return EFI_INVALID_PARAMETER
;
2594 // When Target is not all 0xFF's, compare 2 least significant bytes with
2595 // previous target id to see if it is returned by previous call.
2597 if (*Target16
!= Instance
->PreviousTargetId
) {
2598 return EFI_INVALID_PARAMETER
;
2602 // Traverse the whole device list to find the next cdrom closed to
2603 // the device signified by Target[0] and Target[1].
2605 // Note that we here use a tricky way to find the next cdrom :
2606 // All ata devices are detected and inserted into the device list
2609 Node
= GetFirstNode (&Instance
->DeviceList
);
2610 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2611 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2613 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2614 ((Target8
[0] < DeviceInfo
->Port
) ||
2615 ((Target8
[0] == DeviceInfo
->Port
) &&
2616 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2617 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2618 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2622 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2625 return EFI_NOT_FOUND
;
2628 // If the array is all 0xFF's, start to traverse the device list from the beginning
2630 Node
= GetFirstNode (&Instance
->DeviceList
);
2632 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2633 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2635 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2636 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2637 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2641 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2644 return EFI_NOT_FOUND
;
2649 // Update the PreviousTargetId.
2651 Instance
->PreviousTargetId
= *Target16
;