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
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
146 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
147 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
148 and the non-blocking I/O functionality is optional.
150 @param[in] Port The port number of the ATA device to send the command.
151 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
152 If there is no port multiplier, then specify 0xFFFF.
153 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
154 and PortMultiplierPort.
155 @param[in] Instance Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE.
156 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
157 used by non-blocking mode.
159 @retval EFI_SUCCESS The ATA command was sent by the host. For
160 bi-directional commands, InTransferLength bytes
161 were transferred from InDataBuffer. For
162 write and bi-directional commands, OutTransferLength
163 bytes were transferred by OutDataBuffer.
164 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
165 of bytes that could be transferred is returned
166 in InTransferLength. For write and bi-directional
167 commands, OutTransferLength bytes were transferred
169 @retval EFI_NOT_READY The ATA command could not be sent because
170 there are too many ATA commands already
171 queued. The caller may retry again later.
172 @retval EFI_DEVICE_ERROR A device error occurred while attempting
173 to send the ATA command.
174 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents
175 of Acb are invalid. The ATA command was
176 not sent, so no additional status information
182 AtaPassThruPassThruExecute (
184 IN UINT16 PortMultiplierPort
,
185 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
186 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
187 IN ATA_NONBLOCK_TASK
*Task OPTIONAL
190 EFI_ATA_PASS_THRU_CMD_PROTOCOL Protocol
;
191 EFI_ATA_HC_WORK_MODE Mode
;
194 Protocol
= Packet
->Protocol
;
196 Mode
= Instance
->Mode
;
200 // Reassign IDE mode io port registers' base addresses
202 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
204 if (EFI_ERROR (Status
)) {
209 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
210 Status
= AtaNonDataCommandIn (
212 &Instance
->IdeRegisters
[Port
],
219 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
220 Status
= AtaPioDataInOut (
222 &Instance
->IdeRegisters
[Port
],
223 Packet
->InDataBuffer
,
224 Packet
->InTransferLength
,
232 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
233 Status
= AtaPioDataInOut (
235 &Instance
->IdeRegisters
[Port
],
236 Packet
->OutDataBuffer
,
237 Packet
->OutTransferLength
,
245 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
246 Status
= AtaUdmaInOut (
248 &Instance
->IdeRegisters
[Port
],
250 Packet
->InDataBuffer
,
251 Packet
->InTransferLength
,
258 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
259 Status
= AtaUdmaInOut (
261 &Instance
->IdeRegisters
[Port
],
263 Packet
->OutDataBuffer
,
264 Packet
->OutTransferLength
,
272 return EFI_UNSUPPORTED
;
275 case EfiAtaAhciMode
:
276 if (PortMultiplierPort
== 0xFFFF) {
278 // If there is no port multiplier, PortMultiplierPort will be 0xFFFF
279 // according to UEFI spec. Here, we convert its value to 0 to follow
282 PortMultiplierPort
= 0;
285 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
286 Status
= AhciNonDataTransfer (
288 &Instance
->AhciRegisters
,
290 (UINT8
)PortMultiplierPort
,
299 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
300 Status
= AhciPioTransfer (
302 &Instance
->AhciRegisters
,
304 (UINT8
)PortMultiplierPort
,
310 Packet
->InDataBuffer
,
311 Packet
->InTransferLength
,
316 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
317 Status
= AhciPioTransfer (
319 &Instance
->AhciRegisters
,
321 (UINT8
)PortMultiplierPort
,
327 Packet
->OutDataBuffer
,
328 Packet
->OutTransferLength
,
333 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
334 Status
= AhciDmaTransfer (
336 &Instance
->AhciRegisters
,
338 (UINT8
)PortMultiplierPort
,
344 Packet
->InDataBuffer
,
345 Packet
->InTransferLength
,
350 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
351 Status
= AhciDmaTransfer (
353 &Instance
->AhciRegisters
,
355 (UINT8
)PortMultiplierPort
,
361 Packet
->OutDataBuffer
,
362 Packet
->OutTransferLength
,
368 return EFI_UNSUPPORTED
;
373 Status
= EFI_DEVICE_ERROR
;
381 Call back function when the timer event is signaled.
383 @param[in] Event The Event this notify function registered to.
384 @param[in] Context Pointer to the context data registered to the
390 AsyncNonBlockingTransferRoutine (
396 LIST_ENTRY
*EntryHeader
;
397 ATA_NONBLOCK_TASK
*Task
;
399 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
401 Instance
= (ATA_ATAPI_PASS_THRU_INSTANCE
*) Context
;
402 EntryHeader
= &Instance
->NonBlockingTaskList
;
404 // Get the Taks from the Taks List and execute it, until there is
405 // no task in the list or the device is busy with task (EFI_NOT_READY).
408 if (!IsListEmpty (EntryHeader
)) {
409 Entry
= GetFirstNode (EntryHeader
);
410 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (Entry
);
415 Status
= AtaPassThruPassThruExecute (
417 Task
->PortMultiplier
,
424 // If the data transfer meet a error, remove all tasks in the list since these tasks are
425 // associated with one task from Ata Bus and signal the event with error status.
427 if ((Status
!= EFI_NOT_READY
) && (Status
!= EFI_SUCCESS
)) {
428 DestroyAsynTaskList (Instance
, TRUE
);
433 // For Non blocking mode, the Status of EFI_NOT_READY means the operation
434 // is not finished yet. Otherwise the operation is successful.
436 if (Status
== EFI_NOT_READY
) {
439 RemoveEntryList (&Task
->Link
);
440 gBS
->SignalEvent (Task
->Event
);
447 The Entry Point of module.
449 @param[in] ImageHandle The firmware allocated handle for the EFI image.
450 @param[in] SystemTable A pointer to the EFI System Table.
452 @retval EFI_SUCCESS The entry point is executed successfully.
453 @retval other Some error occurs when executing this entry point.
458 InitializeAtaAtapiPassThru (
459 IN EFI_HANDLE ImageHandle
,
460 IN EFI_SYSTEM_TABLE
*SystemTable
466 // Install driver model protocol(s).
468 Status
= EfiLibInstallDriverBindingComponentName2 (
471 &gAtaAtapiPassThruDriverBinding
,
473 &gAtaAtapiPassThruComponentName
,
474 &gAtaAtapiPassThruComponentName2
476 ASSERT_EFI_ERROR (Status
);
482 Tests to see if this driver supports a given controller. If a child device is provided,
483 it further tests to see if this driver supports creating a handle for the specified child device.
485 This function checks to see if the driver specified by This supports the device specified by
486 ControllerHandle. Drivers will typically use the device path attached to
487 ControllerHandle and/or the services from the bus I/O abstraction attached to
488 ControllerHandle to determine if the driver supports ControllerHandle. This function
489 may be called many times during platform initialization. In order to reduce boot times, the tests
490 performed by this function must be very small, and take as little time as possible to execute. This
491 function must not change the state of any hardware devices, and this function must be aware that the
492 device specified by ControllerHandle may already be managed by the same driver or a
493 different driver. This function must match its calls to AllocatePages() with FreePages(),
494 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
495 Because ControllerHandle may have been previously started by the same driver, if a protocol is
496 already in the opened state, then it must not be closed with CloseProtocol(). This is required
497 to guarantee the state of ControllerHandle is not modified by this function.
499 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
500 @param[in] ControllerHandle The handle of the controller to test. This handle
501 must support a protocol interface that supplies
502 an I/O abstraction to the driver.
503 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
504 parameter is ignored by device drivers, and is optional for bus
505 drivers. For bus drivers, if this parameter is not NULL, then
506 the bus driver must determine if the bus controller specified
507 by ControllerHandle and the child controller specified
508 by RemainingDevicePath are both supported by this
511 @retval EFI_SUCCESS The device specified by ControllerHandle and
512 RemainingDevicePath is supported by the driver specified by This.
513 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
514 RemainingDevicePath is already being managed by the driver
516 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
517 RemainingDevicePath is already being managed by a different
518 driver or an application that requires exclusive access.
519 Currently not implemented.
520 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
521 RemainingDevicePath is not supported by the driver specified by This.
525 AtaAtapiPassThruSupported (
526 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
527 IN EFI_HANDLE Controller
,
528 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
532 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
533 EFI_PCI_IO_PROTOCOL
*PciIo
;
535 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
538 // SATA Controller is a device driver, and should ingore the
539 // "RemainingDevicePath" according to UEFI spec
541 Status
= gBS
->OpenProtocol (
543 &gEfiDevicePathProtocolGuid
,
544 (VOID
*) &ParentDevicePath
,
545 This
->DriverBindingHandle
,
547 EFI_OPEN_PROTOCOL_BY_DRIVER
549 if (EFI_ERROR (Status
)) {
551 // EFI_ALREADY_STARTED is also an error
556 // Close the protocol because we don't use it here
560 &gEfiDevicePathProtocolGuid
,
561 This
->DriverBindingHandle
,
565 Status
= gBS
->OpenProtocol (
567 &gEfiIdeControllerInitProtocolGuid
,
568 (VOID
**) &IdeControllerInit
,
569 This
->DriverBindingHandle
,
571 EFI_OPEN_PROTOCOL_BY_DRIVER
574 if (EFI_ERROR (Status
)) {
576 // EFI_ALREADY_STARTED is also an error
582 // Close the I/O Abstraction(s) used to perform the supported test
586 &gEfiIdeControllerInitProtocolGuid
,
587 This
->DriverBindingHandle
,
592 // Now test the EfiPciIoProtocol
594 Status
= gBS
->OpenProtocol (
596 &gEfiPciIoProtocolGuid
,
598 This
->DriverBindingHandle
,
600 EFI_OPEN_PROTOCOL_GET_PROTOCOL
602 if (EFI_ERROR (Status
)) {
606 // Now further check the PCI header: Base class (offset 0x0B) and
607 // Sub Class (offset 0x0A). This controller should be an ATA controller
609 Status
= PciIo
->Pci
.Read (
612 PCI_CLASSCODE_OFFSET
,
613 sizeof (PciData
.Hdr
.ClassCode
),
614 PciData
.Hdr
.ClassCode
616 if (EFI_ERROR (Status
)) {
617 return EFI_UNSUPPORTED
;
620 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
624 return EFI_UNSUPPORTED
;
628 Starts a device controller or a bus controller.
630 The Start() function is designed to be invoked from the EFI boot service ConnectController().
631 As a result, much of the error checking on the parameters to Start() has been moved into this
632 common boot service. It is legal to call Start() from other locations,
633 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
634 1. ControllerHandle must be a valid EFI_HANDLE.
635 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
636 EFI_DEVICE_PATH_PROTOCOL.
637 3. Prior to calling Start(), the Supported() function for the driver specified by This must
638 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
640 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
641 @param[in] ControllerHandle The handle of the controller to start. This handle
642 must support a protocol interface that supplies
643 an I/O abstraction to the driver.
644 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
645 parameter is ignored by device drivers, and is optional for bus
646 drivers. For a bus driver, if this parameter is NULL, then handles
647 for all the children of Controller are created by this driver.
648 If this parameter is not NULL and the first Device Path Node is
649 not the End of Device Path Node, then only the handle for the
650 child device specified by the first Device Path Node of
651 RemainingDevicePath is created by this driver.
652 If the first Device Path Node of RemainingDevicePath is
653 the End of Device Path Node, no child handle is created by this
656 @retval EFI_SUCCESS The device was started.
657 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
658 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
659 @retval Others The driver failded to start the device.
664 AtaAtapiPassThruStart (
665 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
666 IN EFI_HANDLE Controller
,
667 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
671 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
672 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
673 EFI_PCI_IO_PROTOCOL
*PciIo
;
674 UINT64 EnabledPciAttributes
;
675 UINT64 OriginalPciAttributes
;
677 Status
= EFI_SUCCESS
;
678 IdeControllerInit
= NULL
;
680 OriginalPciAttributes
= 0;
682 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Start== Controller = %x\n", Controller
));
684 Status
= gBS
->OpenProtocol (
686 &gEfiIdeControllerInitProtocolGuid
,
687 (VOID
**) &IdeControllerInit
,
688 This
->DriverBindingHandle
,
690 EFI_OPEN_PROTOCOL_BY_DRIVER
693 if (EFI_ERROR (Status
)) {
694 DEBUG ((EFI_D_ERROR
, "Open Ide_Controller_Init Error, Status=%r", Status
));
698 Status
= gBS
->OpenProtocol (
700 &gEfiPciIoProtocolGuid
,
702 This
->DriverBindingHandle
,
704 EFI_OPEN_PROTOCOL_GET_PROTOCOL
706 if (EFI_ERROR (Status
)) {
707 DEBUG ((EFI_D_ERROR
, "Get Pci_Io Protocol Error, Status=%r", Status
));
711 Status
= PciIo
->Attributes (
713 EfiPciIoAttributeOperationGet
,
715 &OriginalPciAttributes
718 if (EFI_ERROR (Status
)) {
722 Status
= PciIo
->Attributes (
724 EfiPciIoAttributeOperationSupported
,
726 &EnabledPciAttributes
728 if (!EFI_ERROR (Status
)) {
729 EnabledPciAttributes
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
730 Status
= PciIo
->Attributes (
732 EfiPciIoAttributeOperationEnable
,
733 EnabledPciAttributes
,
738 if (EFI_ERROR (Status
)) {
743 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
745 Instance
= AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE
), &gAtaAtapiPassThruInstanceTemplate
);
746 if (Instance
== NULL
) {
750 Instance
->ControllerHandle
= Controller
;
751 Instance
->IdeControllerInit
= IdeControllerInit
;
752 Instance
->PciIo
= PciIo
;
753 Instance
->EnabledPciAttributes
= EnabledPciAttributes
;
754 Instance
->OriginalPciAttributes
= OriginalPciAttributes
;
755 Instance
->AtaPassThru
.Mode
= &Instance
->AtaPassThruMode
;
756 Instance
->ExtScsiPassThru
.Mode
= &Instance
->ExtScsiPassThruMode
;
757 InitializeListHead(&Instance
->DeviceList
);
758 InitializeListHead(&Instance
->NonBlockingTaskList
);
760 Instance
->TimerEvent
= NULL
;
762 Status
= gBS
->CreateEvent (
763 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
765 AsyncNonBlockingTransferRoutine
,
767 &Instance
->TimerEvent
769 if (EFI_ERROR (Status
)) {
776 Status
= gBS
->SetTimer (Instance
->TimerEvent
, TimerPeriodic
, 10000);
777 if (EFI_ERROR (Status
)) {
782 // Enumerate all inserted ATA devices.
784 Status
= EnumerateAttachedDevice (Instance
);
785 if (EFI_ERROR (Status
)) {
789 Status
= gBS
->InstallMultipleProtocolInterfaces (
791 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
792 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
795 ASSERT_EFI_ERROR (Status
);
800 if (IdeControllerInit
!= NULL
) {
803 &gEfiIdeControllerInitProtocolGuid
,
804 This
->DriverBindingHandle
,
809 if ((Instance
!= NULL
) && (Instance
->TimerEvent
!= NULL
)) {
810 gBS
->CloseEvent (Instance
->TimerEvent
);
814 // Remove all inserted ATA devices.
816 DestroyDeviceInfoList(Instance
);
818 if (Instance
!= NULL
) {
821 return EFI_UNSUPPORTED
;
825 Stops a device controller or a bus controller.
827 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
828 As a result, much of the error checking on the parameters to Stop() has been moved
829 into this common boot service. It is legal to call Stop() from other locations,
830 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
831 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
832 same driver's Start() function.
833 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
834 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
835 Start() function, and the Start() function must have called OpenProtocol() on
836 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
838 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
839 @param[in] ControllerHandle A handle to the device being stopped. The handle must
840 support a bus specific I/O protocol for the driver
841 to use to stop the device.
842 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
843 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
844 if NumberOfChildren is 0.
846 @retval EFI_SUCCESS The device was stopped.
847 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
852 AtaAtapiPassThruStop (
853 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
854 IN EFI_HANDLE Controller
,
855 IN UINTN NumberOfChildren
,
856 IN EFI_HANDLE
*ChildHandleBuffer
860 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
861 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
862 EFI_PCI_IO_PROTOCOL
*PciIo
;
863 EFI_AHCI_REGISTERS
*AhciRegisters
;
865 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller
));
867 Status
= gBS
->OpenProtocol (
869 &gEfiAtaPassThruProtocolGuid
,
870 (VOID
**) &AtaPassThru
,
871 This
->DriverBindingHandle
,
873 EFI_OPEN_PROTOCOL_GET_PROTOCOL
876 if (EFI_ERROR (Status
)) {
877 return EFI_DEVICE_ERROR
;
880 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru
);
882 Status
= gBS
->UninstallMultipleProtocolInterfaces (
884 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
885 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
889 if (EFI_ERROR (Status
)) {
890 return EFI_DEVICE_ERROR
;
894 // Close protocols opened by AtaAtapiPassThru controller driver
898 &gEfiIdeControllerInitProtocolGuid
,
899 This
->DriverBindingHandle
,
904 // Close Non-Blocking timer and free Task list.
906 if (Instance
->TimerEvent
!= NULL
) {
907 gBS
->CloseEvent (Instance
->TimerEvent
);
908 Instance
->TimerEvent
= NULL
;
910 DestroyAsynTaskList (Instance
, FALSE
);
912 // Free allocated resource
914 DestroyDeviceInfoList (Instance
);
917 // If the current working mode is AHCI mode, then pre-allocated resource
918 // for AHCI initialization should be released.
920 PciIo
= Instance
->PciIo
;
922 if (Instance
->Mode
== EfiAtaAhciMode
) {
923 AhciRegisters
= &Instance
->AhciRegisters
;
926 AhciRegisters
->MapCommandTable
930 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandTableSize
),
931 AhciRegisters
->AhciCommandTable
935 AhciRegisters
->MapCmdList
939 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandListSize
),
940 AhciRegisters
->AhciCmdList
944 AhciRegisters
->MapRFis
948 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxReceiveFisSize
),
949 AhciRegisters
->AhciRFis
954 // Disable this ATA host controller.
958 EfiPciIoAttributeOperationDisable
,
959 Instance
->EnabledPciAttributes
,
964 // Restore original PCI attributes
966 Status
= PciIo
->Attributes (
968 EfiPciIoAttributeOperationSet
,
969 Instance
->OriginalPciAttributes
,
972 ASSERT_EFI_ERROR (Status
);
980 Traverse the attached ATA devices list to find out the device to access.
982 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
983 @param[in] Port The port number of the ATA device to send the command.
984 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
985 If there is no port multiplier, then specify 0xFFFF.
986 @param[in] DeviceType The device type of the ATA device.
988 @retval The pointer to the data structure of the device info to access.
993 SearchDeviceInfoList (
994 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
996 IN UINT16 PortMultiplier
,
997 IN EFI_ATA_DEVICE_TYPE DeviceType
1000 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1003 Node
= GetFirstNode (&Instance
->DeviceList
);
1004 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1005 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1008 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
1009 // the PortMultiplier information. If the CD-ROM is directly attached
1010 // on a SATA port, the PortMultiplier should be translated from 0xFF
1011 // to 0xFFFF according to the UEFI spec.
1013 if ((Instance
->Mode
== EfiAtaAhciMode
) &&
1014 (DeviceInfo
->Type
== EfiIdeCdrom
) &&
1015 (PortMultiplier
== 0xFF)) {
1016 PortMultiplier
= 0xFFFF;
1019 if ((DeviceInfo
->Type
== DeviceType
) &&
1020 (Port
== DeviceInfo
->Port
) &&
1021 (PortMultiplier
== DeviceInfo
->PortMultiplier
)) {
1025 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1032 Allocate device info data structure to contain device info.
1033 And insert the data structure to the tail of device list for tracing.
1035 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1036 @param[in] Port The port number of the ATA device to send the command.
1037 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1038 If there is no port multiplier, then specify 0xFFFF.
1039 @param[in] DeviceType The device type of the ATA device.
1040 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1042 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1043 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1048 CreateNewDeviceInfo (
1049 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1051 IN UINT16 PortMultiplier
,
1052 IN EFI_ATA_DEVICE_TYPE DeviceType
,
1053 IN EFI_IDENTIFY_DATA
*IdentifyData
1056 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1058 DeviceInfo
= AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO
));
1060 if (DeviceInfo
== NULL
) {
1061 return EFI_OUT_OF_RESOURCES
;
1064 DeviceInfo
->Signature
= ATA_ATAPI_DEVICE_SIGNATURE
;
1065 DeviceInfo
->Port
= Port
;
1066 DeviceInfo
->PortMultiplier
= PortMultiplier
;
1067 DeviceInfo
->Type
= DeviceType
;
1069 if (IdentifyData
!= NULL
) {
1070 DeviceInfo
->IdentifyData
= AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA
), IdentifyData
);
1071 if (DeviceInfo
->IdentifyData
== NULL
) {
1072 FreePool (DeviceInfo
);
1073 return EFI_OUT_OF_RESOURCES
;
1077 InsertTailList (&Instance
->DeviceList
, &DeviceInfo
->Link
);
1083 Destroy all attached ATA devices info.
1085 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1090 DestroyDeviceInfoList (
1091 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1094 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1097 Node
= GetFirstNode (&Instance
->DeviceList
);
1098 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1099 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1101 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1103 RemoveEntryList (&DeviceInfo
->Link
);
1104 if (DeviceInfo
->IdentifyData
!= NULL
) {
1105 FreePool (DeviceInfo
->IdentifyData
);
1107 FreePool (DeviceInfo
);
1112 Destroy all pending non blocking tasks.
1114 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1115 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1121 DestroyAsynTaskList (
1122 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1123 IN BOOLEAN IsSigEvent
1127 LIST_ENTRY
*DelEntry
;
1128 ATA_NONBLOCK_TASK
*Task
;
1131 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1132 if (!IsListEmpty (&Instance
->NonBlockingTaskList
)) {
1134 // Free the Subtask list.
1136 for (Entry
= (&Instance
->NonBlockingTaskList
)->ForwardLink
;
1137 Entry
!= (&Instance
->NonBlockingTaskList
);
1140 Entry
= Entry
->ForwardLink
;
1141 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry
);
1143 RemoveEntryList (DelEntry
);
1145 Task
->Packet
->Asb
->AtaStatus
= 0x01;
1146 gBS
->SignalEvent (Task
->Event
);
1151 gBS
->RestoreTPL (OldTpl
);
1155 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1157 The function is designed to enumerate all attached ATA devices.
1159 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1161 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1162 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1167 EnumerateAttachedDevice (
1168 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1175 Status
= EFI_SUCCESS
;
1177 Status
= Instance
->PciIo
->Pci
.Read (
1180 PCI_CLASSCODE_OFFSET
,
1181 sizeof (PciData
.Hdr
.ClassCode
),
1182 PciData
.Hdr
.ClassCode
1184 ASSERT_EFI_ERROR (Status
);
1186 ClassCode
= PciData
.Hdr
.ClassCode
[1];
1188 switch (ClassCode
) {
1189 case PCI_CLASS_MASS_STORAGE_IDE
:
1191 // The ATA controller is working at IDE mode
1193 Instance
->Mode
= EfiAtaIdeMode
;
1195 Status
= IdeModeInitialization (Instance
);
1196 if (EFI_ERROR (Status
)) {
1197 Status
= EFI_DEVICE_ERROR
;
1201 case PCI_CLASS_MASS_STORAGE_SATADPA
:
1203 // The ATA controller is working at AHCI mode
1205 Instance
->Mode
= EfiAtaAhciMode
;
1207 Status
= AhciModeInitialization (Instance
);
1209 if (EFI_ERROR (Status
)) {
1210 Status
= EFI_DEVICE_ERROR
;
1216 Status
= EFI_UNSUPPORTED
;
1224 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1225 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1226 and the non-blocking I/O functionality is optional.
1228 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1229 @param[in] Port The port number of the ATA device to send the command.
1230 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1231 If there is no port multiplier, then specify 0xFFFF.
1232 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1233 and PortMultiplierPort.
1234 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1235 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1236 Event is not NULL and non blocking I/O is supported, then non-blocking
1237 I/O is performed, and Event will be signaled when the ATA command completes.
1239 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1240 InTransferLength bytes were transferred from InDataBuffer. For write and
1241 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1242 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1243 is returned in InTransferLength. For write and bi-directional commands,
1244 OutTransferLength bytes were transferred by OutDataBuffer.
1245 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1246 already queued. The caller may retry again later.
1247 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1248 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1249 command was not sent, so no additional status information is available.
1254 AtaPassThruPassThru (
1255 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1257 IN UINT16 PortMultiplierPort
,
1258 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
1259 IN EFI_EVENT Event OPTIONAL
1262 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1264 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1265 EFI_IDENTIFY_DATA
*IdentifyData
;
1267 UINT32 MaxSectorCount
;
1268 ATA_NONBLOCK_TASK
*Task
;
1272 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1274 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1275 return EFI_INVALID_PARAMETER
;
1278 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1279 return EFI_INVALID_PARAMETER
;
1282 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->Asb
, This
->Mode
->IoAlign
)) {
1283 return EFI_INVALID_PARAMETER
;
1286 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1289 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeCdrom
);
1291 return EFI_INVALID_PARAMETER
;
1296 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1297 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1298 // If bit10 is one, it means the ata device support 48-bit addressing.
1300 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1301 IdentifyData
= DeviceInfo
->IdentifyData
;
1302 MaxSectorCount
= 0x100;
1303 if ((IdentifyData
->AtaData
.command_set_supported_83
& (BIT10
| BIT15
| BIT14
)) == 0x4400) {
1304 Capacity
= *((UINT64
*)IdentifyData
->AtaData
.maximum_lba_for_48bit_addressing
);
1305 if (Capacity
> 0xFFFFFFF) {
1307 // Capacity exceeds 120GB. 48-bit addressing is really needed
1308 // In this case, the max sector count is 0x10000
1310 MaxSectorCount
= 0x10000;
1315 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& (BIT14
| BIT15
)) == BIT14
) {
1317 // Check logical block size
1319 if ((IdentifyData
->AtaData
.phy_logic_sector_support
& BIT12
) != 0) {
1320 BlockSize
= (UINT32
) (((IdentifyData
->AtaData
.logic_sector_size_hi
<< 16) | IdentifyData
->AtaData
.logic_sector_size_lo
) * sizeof (UINT16
));
1325 // convert the transfer length from sector count to byte.
1327 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1328 (Packet
->InTransferLength
!= 0)) {
1329 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
1333 // convert the transfer length from sector count to byte.
1335 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1336 (Packet
->OutTransferLength
!= 0)) {
1337 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
1341 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1342 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1345 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
1346 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
))) {
1347 return EFI_BAD_BUFFER_SIZE
;
1351 // For non-blocking mode, queue the Task into the list.
1353 if (Event
!= NULL
) {
1354 Task
= AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK
));
1356 return EFI_OUT_OF_RESOURCES
;
1359 Task
->Signature
= ATA_NONBLOCKING_TASK_SIGNATURE
;
1361 Task
->PortMultiplier
= PortMultiplierPort
;
1362 Task
->Packet
= Packet
;
1363 Task
->Event
= Event
;
1364 Task
->IsStart
= FALSE
;
1365 Task
->RetryTimes
= DivU64x32(Packet
->Timeout
, 1000) + 1;
1366 if (Packet
->Timeout
== 0) {
1367 Task
->InfiniteWait
= TRUE
;
1369 Task
->InfiniteWait
= FALSE
;
1372 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1373 InsertTailList (&Instance
->NonBlockingTaskList
, &Task
->Link
);
1374 gBS
->RestoreTPL (OldTpl
);
1378 return AtaPassThruPassThruExecute (
1389 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1390 These can either be the list of ports where ATA devices are actually present or the
1391 list of legal port numbers for the ATA controller. Regardless, the caller of this
1392 function must probe the port number returned to see if an ATA device is actually
1393 present at that location on the ATA controller.
1395 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1396 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1397 in Port and EFI_SUCCESS is returned.
1399 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1400 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1401 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1402 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1404 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1407 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1408 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1409 On output, a pointer to the next port number on the ATA
1410 controller. An input value of 0xFFFF retrieves the first port
1411 number on the ATA controller.
1413 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1414 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1415 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1421 AtaPassThruGetNextPort (
1422 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1426 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1428 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1430 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1433 return EFI_INVALID_PARAMETER
;
1436 if (*Port
== 0xFFFF) {
1438 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1440 Node
= GetFirstNode (&Instance
->DeviceList
);
1442 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1443 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1445 if (DeviceInfo
->Type
== EfiIdeHarddisk
) {
1446 *Port
= DeviceInfo
->Port
;
1450 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1453 return EFI_NOT_FOUND
;
1454 } else if (*Port
== Instance
->PreviousPort
) {
1455 Node
= GetFirstNode (&Instance
->DeviceList
);
1457 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1458 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1460 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1461 (DeviceInfo
->Port
> *Port
)){
1462 *Port
= DeviceInfo
->Port
;
1466 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1469 return EFI_NOT_FOUND
;
1472 // Port is not equal to 0xFFFF and also not equal to previous return value
1474 return EFI_INVALID_PARAMETER
;
1479 // Update the PreviousPort and PreviousPortMultiplier.
1481 Instance
->PreviousPort
= *Port
;
1487 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1488 controller. These can either be the list of port multiplier ports where ATA devices are actually
1489 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1490 function must probe the port number and port multiplier port number returned to see if an ATA
1491 device is actually present.
1493 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1494 present on a port of an ATA controller.
1496 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1497 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1498 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1501 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1502 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1503 EFI_SUCCESS is returned.
1505 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1506 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1509 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1510 the ATA controller, then EFI_NOT_FOUND is returned.
1512 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1513 @param[in] Port The port number present on the ATA controller.
1514 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1515 ATA device present on the ATA controller.
1516 If on input a PortMultiplierPort of 0xFFFF is specified,
1517 then the port multiplier port number of the first ATA device
1518 is returned. On output, a pointer to the port multiplier port
1519 number of the next ATA device present on an ATA controller.
1521 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1522 of the ATA controller was returned in PortMultiplierPort.
1523 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1524 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1525 returned on a previous call to GetNextDevice().
1530 AtaPassThruGetNextDevice (
1531 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1533 IN OUT UINT16
*PortMultiplierPort
1536 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1538 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1540 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1542 if (PortMultiplierPort
== NULL
) {
1543 return EFI_INVALID_PARAMETER
;
1546 if (Instance
->PreviousPortMultiplier
== 0xFFFF) {
1548 // If a device is directly attached on a port, previous call to this
1549 // function will return the value 0xFFFF for PortMultiplierPort. In
1550 // this case, there should be no more device on the port multiplier.
1552 Instance
->PreviousPortMultiplier
= 0;
1553 return EFI_NOT_FOUND
;
1556 if (*PortMultiplierPort
== Instance
->PreviousPortMultiplier
) {
1557 Node
= GetFirstNode (&Instance
->DeviceList
);
1559 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1560 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1562 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1563 (DeviceInfo
->Port
== Port
) &&
1564 (DeviceInfo
->PortMultiplier
> *PortMultiplierPort
)){
1565 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1569 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1572 return EFI_NOT_FOUND
;
1573 } else if (*PortMultiplierPort
== 0xFFFF) {
1575 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1577 Node
= GetFirstNode (&Instance
->DeviceList
);
1579 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1580 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1582 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1583 (DeviceInfo
->Port
== Port
)){
1584 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1588 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1591 return EFI_NOT_FOUND
;
1594 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1596 return EFI_INVALID_PARAMETER
;
1601 // Update the PreviousPort and PreviousPortMultiplier.
1603 Instance
->PreviousPortMultiplier
= *PortMultiplierPort
;
1609 Used to allocate and build a device path node for an ATA device on an ATA controller.
1611 The BuildDevicePath() function allocates and builds a single device node for the ATA
1612 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1613 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1614 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1615 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1617 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1618 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1619 and EFI_SUCCESS is returned.
1621 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1622 @param[in] Port Port specifies the port number of the ATA device for which a
1623 device path node is to be allocated and built.
1624 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1625 device path node is to be allocated and built. If there is no
1626 port multiplier, then specify 0xFFFF.
1627 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1628 device specified by Port and PortMultiplierPort. This function
1629 is responsible for allocating the buffer DevicePath with the
1630 boot service AllocatePool(). It is the caller's responsibility
1631 to free DevicePath when the caller is finished with DevicePath.
1632 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1633 Port and PortMultiplierPort was allocated and returned in DevicePath.
1634 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1635 exist on the ATA controller.
1636 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1637 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1642 AtaPassThruBuildDevicePath (
1643 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1645 IN UINT16 PortMultiplierPort
,
1646 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1649 EFI_DEV_PATH
*DevicePathNode
;
1650 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1653 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1656 // Validate parameters passed in.
1658 if (DevicePath
== NULL
) {
1659 return EFI_INVALID_PARAMETER
;
1662 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1664 return EFI_NOT_FOUND
;
1667 if (Instance
->Mode
== EfiAtaIdeMode
) {
1668 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
1669 if (DevicePathNode
== NULL
) {
1670 return EFI_OUT_OF_RESOURCES
;
1672 DevicePathNode
->Atapi
.PrimarySecondary
= (UINT8
) Port
;
1673 DevicePathNode
->Atapi
.SlaveMaster
= (UINT8
) PortMultiplierPort
;
1674 DevicePathNode
->Atapi
.Lun
= 0;
1676 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
1677 if (DevicePathNode
== NULL
) {
1678 return EFI_OUT_OF_RESOURCES
;
1681 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
1682 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplierPort
;
1683 DevicePathNode
->Sata
.Lun
= 0;
1686 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
1692 Used to translate a device path node to a port number and port multiplier port number.
1694 The GetDevice() function determines the port and port multiplier port number associated with
1695 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1696 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1697 DevicePath into a port number and port multiplier port number.
1699 If this translation is successful, then that port number and port multiplier port number are returned
1700 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1702 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1704 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1705 EFI_UNSUPPORTED is returned.
1707 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1708 a valid translation from DevicePath to a port number and port multiplier port number, then
1709 EFI_NOT_FOUND is returned.
1711 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1712 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1714 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1715 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1716 on the ATA controller.
1718 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1719 port number, and they were returned in Port and PortMultiplierPort.
1720 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1721 @retval EFI_INVALID_PARAMETER Port is NULL.
1722 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1723 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1724 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1725 port number does not exist.
1729 AtaPassThruGetDevice (
1730 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1731 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1733 OUT UINT16
*PortMultiplierPort
1736 EFI_DEV_PATH
*DevicePathNode
;
1737 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1740 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1743 // Validate parameters passed in.
1745 if (DevicePath
== NULL
|| Port
== NULL
|| PortMultiplierPort
== NULL
) {
1746 return EFI_INVALID_PARAMETER
;
1750 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1752 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1753 ((DevicePath
->SubType
!= MSG_SATA_DP
) &&
1754 (DevicePath
->SubType
!= MSG_ATAPI_DP
)) ||
1755 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
1756 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
1757 return EFI_UNSUPPORTED
;
1760 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
1762 if (Instance
->Mode
== EfiAtaIdeMode
) {
1763 *Port
= DevicePathNode
->Atapi
.PrimarySecondary
;
1764 *PortMultiplierPort
= DevicePathNode
->Atapi
.SlaveMaster
;
1766 *Port
= DevicePathNode
->Sata
.HBAPortNumber
;
1767 *PortMultiplierPort
= DevicePathNode
->Sata
.PortMultiplierPortNumber
;
1770 Node
= SearchDeviceInfoList(Instance
, *Port
, *PortMultiplierPort
, EfiIdeHarddisk
);
1773 return EFI_NOT_FOUND
;
1780 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1781 connected to the port.
1783 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1784 resets all the ATA devices connected to that port. If this ATA controller does not support
1785 a reset port operation, then EFI_UNSUPPORTED is returned.
1787 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1790 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1792 If the port reset operation is completed, then EFI_SUCCESS is returned.
1794 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1795 @param[in] Port The port number on the ATA controller.
1797 @retval EFI_SUCCESS The ATA controller port was reset.
1798 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1799 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1800 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1805 AtaPassThruResetPort (
1806 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1811 // Return success directly then upper layer driver could think reset port operation is done.
1817 Resets an ATA device that is connected to an ATA controller.
1819 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1820 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1823 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1824 EFI_INVALID_PARAMETER is returned.
1826 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1829 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1832 If the device reset operation is completed, then EFI_SUCCESS is returned.
1834 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1835 @param[in] Port Port represents the port number of the ATA device to be reset.
1836 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1837 If there is no port multiplier, then specify 0xFFFF.
1838 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1839 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1840 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1841 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1842 specified by Port and PortMultiplierPort.
1843 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1844 specified by Port and PortMultiplierPort.
1849 AtaPassThruResetDevice (
1850 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1852 IN UINT16 PortMultiplierPort
1855 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1858 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1860 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1863 return EFI_INVALID_PARAMETER
;
1867 // Return success directly then upper layer driver could think reset device operation is done.
1873 Submit ATAPI request sense command.
1875 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1876 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1877 the id of the SCSI device to send the SCSI Request Packet. Each
1878 transport driver may choose to utilize a subset of this size to suit the needs
1879 of transport target representation. For example, a Fibre Channel driver
1880 may use only 8 bytes (WWN) to represent an FC target.
1881 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1882 @param[in] SenseData A pointer to store sense data.
1883 @param[in] SenseDataLength The sense data length.
1884 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1886 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1887 @retval EFI_DEVICE_ERROR The device failed to send data.
1892 AtaPacketRequestSense (
1893 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1897 IN UINT8 SenseDataLength
,
1901 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet
;
1905 ZeroMem (&Packet
, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1908 Cdb
[0] = ATA_CMD_REQUEST_SENSE
;
1909 Cdb
[4] = SenseDataLength
;
1911 Packet
.Timeout
= Timeout
;
1913 Packet
.CdbLength
= 12;
1914 Packet
.DataDirection
= EFI_EXT_SCSI_DATA_DIRECTION_READ
;
1915 Packet
.InDataBuffer
= SenseData
;
1916 Packet
.InTransferLength
= SenseDataLength
;
1918 Status
= ExtScsiPassThruPassThru (This
, Target
, Lun
, &Packet
, NULL
);
1924 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1925 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1926 nonblocking I/O functionality is optional.
1928 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1929 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1930 the id of the SCSI device to send the SCSI Request Packet. Each
1931 transport driver may choose to utilize a subset of this size to suit the needs
1932 of transport target representation. For example, a Fibre Channel driver
1933 may use only 8 bytes (WWN) to represent an FC target.
1934 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1935 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1936 specified by Target and Lun.
1937 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1938 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1939 Event is not NULL and non blocking I/O is supported, then
1940 nonblocking I/O is performed, and Event will be signaled when the
1941 SCSI Request Packet completes.
1943 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1944 commands, InTransferLength bytes were transferred from
1945 InDataBuffer. For write and bi-directional commands,
1946 OutTransferLength bytes were transferred by
1948 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
1949 could be transferred is returned in InTransferLength. For write
1950 and bi-directional commands, OutTransferLength bytes were
1951 transferred by OutDataBuffer.
1952 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1953 SCSI Request Packets already queued. The caller may retry again later.
1954 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1956 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
1957 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
1958 by the host adapter. This includes the case of Bi-directional SCSI
1959 commands not supported by the implementation. The SCSI Request
1960 Packet was not sent, so no additional status information is available.
1961 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1966 ExtScsiPassThruPassThru (
1967 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1970 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1971 IN EFI_EVENT Event OPTIONAL
1975 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1977 UINT8 PortMultiplier
;
1978 EFI_ATA_HC_WORK_MODE Mode
;
1980 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1982 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
1984 EFI_STATUS SenseStatus
;
1987 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1989 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
1990 return EFI_INVALID_PARAMETER
;
1994 // Don't support variable length CDB
1996 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
1997 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
1998 return EFI_INVALID_PARAMETER
;
2001 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
2002 return EFI_INVALID_PARAMETER
;
2005 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
2006 return EFI_INVALID_PARAMETER
;
2009 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
2010 return EFI_INVALID_PARAMETER
;
2013 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
2014 return EFI_INVALID_PARAMETER
;
2018 // For ATAPI device, doesn't support multiple LUN device.
2021 return EFI_INVALID_PARAMETER
;
2025 // The layout of Target array:
2026 // ________________________________________________________________________
2027 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2028 // |_____________________|_____________________|_____|______________________|
2029 // | | The port multiplier | | |
2030 // | The port number | port number | N/A | N/A |
2031 // |_____________________|_____________________|_____|______________________|
2033 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2036 PortMultiplier
= Target
[1];
2038 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2040 return EFI_INVALID_PARAMETER
;
2043 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2046 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2047 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2048 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2050 if (*((UINT8
*)Packet
->Cdb
) == ATA_CMD_IDENTIFY_DEVICE
) {
2051 CopyMem (Packet
->InDataBuffer
, DeviceInfo
->IdentifyData
, sizeof (EFI_IDENTIFY_DATA
));
2053 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2055 Packet
->SenseDataLength
= 0;
2059 Mode
= Instance
->Mode
;
2063 // Reassign IDE mode io port registers' base addresses
2065 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
2067 if (EFI_ERROR (Status
)) {
2071 Status
= AtaPacketCommandExecute (Instance
->PciIo
, &Instance
->IdeRegisters
[Port
], Port
, PortMultiplier
, Packet
);
2073 case EfiAtaAhciMode
:
2074 if (PortMultiplier
== 0xFF) {
2076 // If there is no port multiplier, the PortMultiplier will be 0xFF
2077 // Here, we convert its value to 0 to follow the AHCI spec.
2081 Status
= AhciPacketCommandExecute (Instance
->PciIo
, &Instance
->AhciRegisters
, Port
, PortMultiplier
, Packet
);
2084 Status
= EFI_DEVICE_ERROR
;
2089 // If the cmd doesn't get executed correctly, then check sense data.
2091 if (EFI_ERROR (Status
) && (Packet
->SenseDataLength
!= 0) && (*((UINT8
*)Packet
->Cdb
) != ATA_CMD_REQUEST_SENSE
)) {
2092 PtrSenseData
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)), This
->Mode
->IoAlign
);
2093 if (PtrSenseData
== NULL
) {
2094 return EFI_DEVICE_ERROR
;
2097 for (SenseReq
= TRUE
; SenseReq
;) {
2098 SenseStatus
= AtaPacketRequestSense (
2103 sizeof (EFI_SCSI_SENSE_DATA
),
2106 if (EFI_ERROR (SenseStatus
)) {
2110 CopyMem ((UINT8
*)Packet
->SenseData
+ SenseDataLen
, PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
2111 SenseDataLen
+= sizeof (EFI_SCSI_SENSE_DATA
);
2114 // no more sense key or number of sense keys exceeds predefined,
2117 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
2118 (SenseDataLen
+ sizeof (EFI_SCSI_SENSE_DATA
) > Packet
->SenseDataLength
)) {
2122 FreeAlignedPages (PtrSenseData
, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)));
2125 // Update the SenseDataLength field to the data length received.
2127 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2132 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2133 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2134 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2135 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2138 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2139 @param Target On input, a pointer to the Target ID (an array of size
2140 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2141 On output, a pointer to the Target ID (an array of
2142 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2143 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2144 Target array retrieves the Target ID of the first SCSI device present on a
2146 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2147 channel. On output, a pointer to the LUN of the next SCSI device present
2150 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2151 channel was returned in Target and Lun.
2152 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2153 not returned on a previous call to GetNextTargetLun().
2154 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2159 ExtScsiPassThruGetNextTargetLun (
2160 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2161 IN OUT UINT8
**Target
,
2165 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2167 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2171 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2173 if (Target
== NULL
|| Lun
== NULL
) {
2174 return EFI_INVALID_PARAMETER
;
2177 if (*Target
== NULL
) {
2178 return EFI_INVALID_PARAMETER
;
2182 Target16
= (UINT16
*)*Target
;
2184 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2186 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2187 // So the higher bytes in Target array should be 0xFF.
2189 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2190 return EFI_INVALID_PARAMETER
;
2194 // When Target is not all 0xFF's, compare 2 least significant bytes with
2195 // previous target id to see if it is returned by previous call.
2197 if ((*Target16
!= Instance
->PreviousTargetId
) ||
2198 (*Lun
!= Instance
->PreviousLun
)) {
2199 return EFI_INVALID_PARAMETER
;
2203 // Traverse the whole device list to find the next cdrom closed to
2204 // the device signified by Target[0] and Target[1].
2206 // Note that we here use a tricky way to find the next cdrom :
2207 // All ata devices are detected and inserted into the device list
2210 Node
= GetFirstNode (&Instance
->DeviceList
);
2212 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2213 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2215 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2216 ((Target8
[0] < DeviceInfo
->Port
) ||
2217 ((Target8
[0] == DeviceInfo
->Port
) &&
2218 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2219 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2220 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2224 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2227 return EFI_NOT_FOUND
;
2230 // If the array is all 0xFF's, start to traverse the device list from the beginning
2232 Node
= GetFirstNode (&Instance
->DeviceList
);
2233 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2234 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2236 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2237 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2238 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2242 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2245 return EFI_NOT_FOUND
;
2252 // Update the PreviousTargetId.
2254 Instance
->PreviousTargetId
= *Target16
;
2255 Instance
->PreviousLun
= *Lun
;
2261 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2263 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2264 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2265 Target ID of the SCSI device for which a device path node is to be
2266 allocated and built. Transport drivers may chose to utilize a subset of
2267 this size to suit the representation of targets. For example, a Fibre
2268 Channel driver may use only 8 bytes (WWN) in the array to represent a
2270 @param Lun The LUN of the SCSI device for which a device path node is to be
2271 allocated and built.
2272 @param DevicePath A pointer to a single device path node that describes the SCSI device
2273 specified by Target and Lun. This function is responsible for
2274 allocating the buffer DevicePath with the boot service
2275 AllocatePool(). It is the caller's responsibility to free
2276 DevicePath when the caller is finished with DevicePath.
2278 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2279 Target and Lun was allocated and returned in
2281 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2282 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2283 on the SCSI channel.
2284 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2289 ExtScsiPassThruBuildDevicePath (
2290 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2293 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
2296 EFI_DEV_PATH
*DevicePathNode
;
2297 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2299 UINT8 PortMultiplier
;
2301 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2304 PortMultiplier
= Target
[1];
2307 // Validate parameters passed in.
2309 if (DevicePath
== NULL
) {
2310 return EFI_INVALID_PARAMETER
;
2314 // can not build device path for the SCSI Host Controller.
2317 return EFI_NOT_FOUND
;
2320 if (SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
) == NULL
) {
2321 return EFI_NOT_FOUND
;
2324 if (Instance
->Mode
== EfiAtaIdeMode
) {
2325 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
2326 if (DevicePathNode
== NULL
) {
2327 return EFI_OUT_OF_RESOURCES
;
2330 DevicePathNode
->Atapi
.PrimarySecondary
= Port
;
2331 DevicePathNode
->Atapi
.SlaveMaster
= PortMultiplier
;
2332 DevicePathNode
->Atapi
.Lun
= (UINT16
) Lun
;
2334 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
2335 if (DevicePathNode
== NULL
) {
2336 return EFI_OUT_OF_RESOURCES
;
2339 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
2341 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
2342 // the PortMultiplier information. If the CD-ROM is directly attached
2343 // on a SATA port, the PortMultiplier should be translated from 0xFF
2344 // to 0xFFFF according to the UEFI spec.
2346 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplier
== 0xFF ? 0xFFFF : PortMultiplier
;
2347 DevicePathNode
->Sata
.Lun
= (UINT16
) Lun
;
2350 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
2356 Used to translate a device path node to a Target ID and LUN.
2358 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2359 @param DevicePath A pointer to a single device path node that describes the SCSI device
2360 on the SCSI channel.
2361 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2362 on the SCSI channel.
2363 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2365 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2366 LUN, and they were returned in Target and Lun.
2367 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2368 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2370 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2376 ExtScsiPassThruGetTargetLun (
2377 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2378 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2383 EFI_DEV_PATH
*DevicePathNode
;
2384 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2387 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2390 // Validate parameters passed in.
2392 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
2393 return EFI_INVALID_PARAMETER
;
2396 if (*Target
== NULL
) {
2397 return EFI_INVALID_PARAMETER
;
2400 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2402 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
2403 ((DevicePath
->SubType
!= MSG_ATAPI_DP
) &&
2404 (DevicePath
->SubType
!= MSG_SATA_DP
)) ||
2405 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
2406 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
2407 return EFI_UNSUPPORTED
;
2410 SetMem (*Target
, TARGET_MAX_BYTES
, 0xFF);
2412 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
2414 if (Instance
->Mode
== EfiAtaIdeMode
) {
2415 (*Target
)[0] = (UINT8
) DevicePathNode
->Atapi
.PrimarySecondary
;
2416 (*Target
)[1] = (UINT8
) DevicePathNode
->Atapi
.SlaveMaster
;
2417 *Lun
= (UINT8
) DevicePathNode
->Atapi
.Lun
;
2419 (*Target
)[0] = (UINT8
) DevicePathNode
->Sata
.HBAPortNumber
;
2420 (*Target
)[1] = (UINT8
) DevicePathNode
->Sata
.PortMultiplierPortNumber
;
2421 *Lun
= (UINT8
) DevicePathNode
->Sata
.Lun
;
2424 Node
= SearchDeviceInfoList(Instance
, (*Target
)[0], (*Target
)[1], EfiIdeCdrom
);
2427 return EFI_NOT_FOUND
;
2431 return EFI_NOT_FOUND
;
2438 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2440 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2442 @retval EFI_SUCCESS The SCSI channel was reset.
2443 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2444 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2445 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2450 ExtScsiPassThruResetChannel (
2451 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
2455 // Return success directly then upper layer driver could think reset channel operation is done.
2461 Resets a SCSI logical unit that is connected to a SCSI channel.
2463 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2464 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2465 target port ID of the SCSI device containing the SCSI logical unit to
2466 reset. Transport drivers may chose to utilize a subset of this array to suit
2467 the representation of their targets.
2468 @param Lun The LUN of the SCSI device to reset.
2470 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2471 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2472 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2473 specified by Target and Lun.
2474 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2475 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2476 specified by Target and Lun.
2481 ExtScsiPassThruResetTargetLun (
2482 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2487 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2490 UINT8 PortMultiplier
;
2492 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2494 // For ATAPI device, doesn't support multiple LUN device.
2497 return EFI_INVALID_PARAMETER
;
2500 // The layout of Target array:
2501 // ________________________________________________________________________
2502 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2503 // |_____________________|_____________________|_____|______________________|
2504 // | | The port multiplier | | |
2505 // | The port number | port number | N/A | N/A |
2506 // |_____________________|_____________________|_____|______________________|
2508 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2511 PortMultiplier
= Target
[1];
2513 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2515 return EFI_INVALID_PARAMETER
;
2519 // Return success directly then upper layer driver could think reset target LUN operation is done.
2525 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2526 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2527 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2528 see if a SCSI device is actually present at that location on the SCSI channel.
2530 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2531 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2532 On output, a pointer to the Target ID (an array of
2533 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2534 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2535 Target array retrieves the Target ID of the first SCSI device present on a
2538 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2539 channel was returned in Target.
2540 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2541 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2542 returned on a previous call to GetNextTarget().
2543 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2548 ExtScsiPassThruGetNextTarget (
2549 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2550 IN OUT UINT8
**Target
2553 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2555 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2559 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2561 if (Target
== NULL
|| *Target
== NULL
) {
2562 return EFI_INVALID_PARAMETER
;
2566 Target16
= (UINT16
*)*Target
;
2568 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2570 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2571 // So the higher bytes in Target array should be 0xFF.
2573 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2574 return EFI_INVALID_PARAMETER
;
2578 // When Target is not all 0xFF's, compare 2 least significant bytes with
2579 // previous target id to see if it is returned by previous call.
2581 if (*Target16
!= Instance
->PreviousTargetId
) {
2582 return EFI_INVALID_PARAMETER
;
2586 // Traverse the whole device list to find the next cdrom closed to
2587 // the device signified by Target[0] and Target[1].
2589 // Note that we here use a tricky way to find the next cdrom :
2590 // All ata devices are detected and inserted into the device list
2593 Node
= GetFirstNode (&Instance
->DeviceList
);
2594 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2595 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2597 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2598 ((Target8
[0] < DeviceInfo
->Port
) ||
2599 ((Target8
[0] == DeviceInfo
->Port
) &&
2600 (Target8
[1] < (UINT8
)DeviceInfo
->PortMultiplier
)))) {
2601 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2602 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2606 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2609 return EFI_NOT_FOUND
;
2612 // If the array is all 0xFF's, start to traverse the device list from the beginning
2614 Node
= GetFirstNode (&Instance
->DeviceList
);
2616 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2617 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2619 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2620 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2621 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2625 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2628 return EFI_NOT_FOUND
;
2633 // Update the PreviousTargetId.
2635 Instance
->PreviousTargetId
= *Target16
;