2 This file implements ATA_PASSTHRU_PROCTOCOL and EXT_SCSI_PASSTHRU_PROTOCOL interfaces
3 for managed ATA controllers.
5 Copyright (c) 2010 - 2014, 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, // OriginalAttributes
99 0, // PreviousPortMultiplier
100 0, // PreviousTargetId
103 { // NonBlocking TaskList
109 ATAPI_DEVICE_PATH mAtapiDevicePathTemplate
= {
111 MESSAGING_DEVICE_PATH
,
114 (UINT8
) (sizeof (ATAPI_DEVICE_PATH
)),
115 (UINT8
) ((sizeof (ATAPI_DEVICE_PATH
)) >> 8)
123 SATA_DEVICE_PATH mSataDevicePathTemplate
= {
125 MESSAGING_DEVICE_PATH
,
128 (UINT8
) (sizeof (SATA_DEVICE_PATH
)),
129 (UINT8
) ((sizeof (SATA_DEVICE_PATH
)) >> 8)
137 UINT8 mScsiId
[TARGET_MAX_BYTES
] = {
138 0xFF, 0xFF, 0xFF, 0xFF,
139 0xFF, 0xFF, 0xFF, 0xFF,
140 0xFF, 0xFF, 0xFF, 0xFF,
141 0xFF, 0xFF, 0xFF, 0xFF
145 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
146 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
147 and the non-blocking I/O functionality is optional.
149 @param[in] Port The port number of the ATA device to send the command.
150 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
151 If there is no port multiplier, then specify 0.
152 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
153 and PortMultiplierPort.
154 @param[in] Instance Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE.
155 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
156 used by non-blocking mode.
158 @retval EFI_SUCCESS The ATA command was sent by the host. For
159 bi-directional commands, InTransferLength bytes
160 were transferred from InDataBuffer. For
161 write and bi-directional commands, OutTransferLength
162 bytes were transferred by OutDataBuffer.
163 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
164 of bytes that could be transferred is returned
165 in InTransferLength. For write and bi-directional
166 commands, OutTransferLength bytes were transferred
168 @retval EFI_NOT_READY The ATA command could not be sent because
169 there are too many ATA commands already
170 queued. The caller may retry again later.
171 @retval EFI_DEVICE_ERROR A device error occurred while attempting
172 to send the ATA command.
173 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents
174 of Acb are invalid. The ATA command was
175 not sent, so no additional status information
181 AtaPassThruPassThruExecute (
183 IN UINT16 PortMultiplierPort
,
184 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
185 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
186 IN ATA_NONBLOCK_TASK
*Task OPTIONAL
189 EFI_ATA_PASS_THRU_CMD_PROTOCOL Protocol
;
190 EFI_ATA_HC_WORK_MODE Mode
;
193 Protocol
= Packet
->Protocol
;
195 Mode
= Instance
->Mode
;
199 // Reassign IDE mode io port registers' base addresses
201 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
203 if (EFI_ERROR (Status
)) {
208 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
209 Status
= AtaNonDataCommandIn (
211 &Instance
->IdeRegisters
[Port
],
218 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
219 Status
= AtaPioDataInOut (
221 &Instance
->IdeRegisters
[Port
],
222 Packet
->InDataBuffer
,
223 Packet
->InTransferLength
,
231 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
232 Status
= AtaPioDataInOut (
234 &Instance
->IdeRegisters
[Port
],
235 Packet
->OutDataBuffer
,
236 Packet
->OutTransferLength
,
244 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
245 Status
= AtaUdmaInOut (
247 &Instance
->IdeRegisters
[Port
],
249 Packet
->InDataBuffer
,
250 Packet
->InTransferLength
,
257 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
258 Status
= AtaUdmaInOut (
260 &Instance
->IdeRegisters
[Port
],
262 Packet
->OutDataBuffer
,
263 Packet
->OutTransferLength
,
271 return EFI_UNSUPPORTED
;
274 case EfiAtaAhciMode
:
276 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
277 Status
= AhciNonDataTransfer (
279 &Instance
->AhciRegisters
,
281 (UINT8
)PortMultiplierPort
,
290 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
291 Status
= AhciPioTransfer (
293 &Instance
->AhciRegisters
,
295 (UINT8
)PortMultiplierPort
,
301 Packet
->InDataBuffer
,
302 Packet
->InTransferLength
,
307 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
308 Status
= AhciPioTransfer (
310 &Instance
->AhciRegisters
,
312 (UINT8
)PortMultiplierPort
,
318 Packet
->OutDataBuffer
,
319 Packet
->OutTransferLength
,
324 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
:
325 Status
= AhciDmaTransfer (
327 &Instance
->AhciRegisters
,
329 (UINT8
)PortMultiplierPort
,
335 Packet
->InDataBuffer
,
336 Packet
->InTransferLength
,
341 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
:
342 Status
= AhciDmaTransfer (
344 &Instance
->AhciRegisters
,
346 (UINT8
)PortMultiplierPort
,
352 Packet
->OutDataBuffer
,
353 Packet
->OutTransferLength
,
359 return EFI_UNSUPPORTED
;
364 Status
= EFI_DEVICE_ERROR
;
372 Call back function when the timer event is signaled.
374 @param[in] Event The Event this notify function registered to.
375 @param[in] Context Pointer to the context data registered to the
381 AsyncNonBlockingTransferRoutine (
387 LIST_ENTRY
*EntryHeader
;
388 ATA_NONBLOCK_TASK
*Task
;
390 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
392 Instance
= (ATA_ATAPI_PASS_THRU_INSTANCE
*) Context
;
393 EntryHeader
= &Instance
->NonBlockingTaskList
;
395 // Get the Taks from the Taks List and execute it, until there is
396 // no task in the list or the device is busy with task (EFI_NOT_READY).
399 if (!IsListEmpty (EntryHeader
)) {
400 Entry
= GetFirstNode (EntryHeader
);
401 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (Entry
);
406 Status
= AtaPassThruPassThruExecute (
408 Task
->PortMultiplier
,
415 // If the data transfer meet a error, remove all tasks in the list since these tasks are
416 // associated with one task from Ata Bus and signal the event with error status.
418 if ((Status
!= EFI_NOT_READY
) && (Status
!= EFI_SUCCESS
)) {
419 DestroyAsynTaskList (Instance
, TRUE
);
424 // For Non blocking mode, the Status of EFI_NOT_READY means the operation
425 // is not finished yet. Otherwise the operation is successful.
427 if (Status
== EFI_NOT_READY
) {
430 RemoveEntryList (&Task
->Link
);
431 gBS
->SignalEvent (Task
->Event
);
438 The Entry Point of module.
440 @param[in] ImageHandle The firmware allocated handle for the EFI image.
441 @param[in] SystemTable A pointer to the EFI System Table.
443 @retval EFI_SUCCESS The entry point is executed successfully.
444 @retval other Some error occurs when executing this entry point.
449 InitializeAtaAtapiPassThru (
450 IN EFI_HANDLE ImageHandle
,
451 IN EFI_SYSTEM_TABLE
*SystemTable
457 // Install driver model protocol(s).
459 Status
= EfiLibInstallDriverBindingComponentName2 (
462 &gAtaAtapiPassThruDriverBinding
,
464 &gAtaAtapiPassThruComponentName
,
465 &gAtaAtapiPassThruComponentName2
467 ASSERT_EFI_ERROR (Status
);
473 Tests to see if this driver supports a given controller. If a child device is provided,
474 it further tests to see if this driver supports creating a handle for the specified child device.
476 This function checks to see if the driver specified by This supports the device specified by
477 ControllerHandle. Drivers will typically use the device path attached to
478 ControllerHandle and/or the services from the bus I/O abstraction attached to
479 ControllerHandle to determine if the driver supports ControllerHandle. This function
480 may be called many times during platform initialization. In order to reduce boot times, the tests
481 performed by this function must be very small, and take as little time as possible to execute. This
482 function must not change the state of any hardware devices, and this function must be aware that the
483 device specified by ControllerHandle may already be managed by the same driver or a
484 different driver. This function must match its calls to AllocatePages() with FreePages(),
485 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
486 Because ControllerHandle may have been previously started by the same driver, if a protocol is
487 already in the opened state, then it must not be closed with CloseProtocol(). This is required
488 to guarantee the state of ControllerHandle is not modified by this function.
490 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
491 @param[in] ControllerHandle The handle of the controller to test. This handle
492 must support a protocol interface that supplies
493 an I/O abstraction to the driver.
494 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
495 parameter is ignored by device drivers, and is optional for bus
496 drivers. For bus drivers, if this parameter is not NULL, then
497 the bus driver must determine if the bus controller specified
498 by ControllerHandle and the child controller specified
499 by RemainingDevicePath are both supported by this
502 @retval EFI_SUCCESS The device specified by ControllerHandle and
503 RemainingDevicePath is supported by the driver specified by This.
504 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
505 RemainingDevicePath is already being managed by the driver
507 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
508 RemainingDevicePath is already being managed by a different
509 driver or an application that requires exclusive access.
510 Currently not implemented.
511 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
512 RemainingDevicePath is not supported by the driver specified by This.
516 AtaAtapiPassThruSupported (
517 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
518 IN EFI_HANDLE Controller
,
519 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
523 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
524 EFI_PCI_IO_PROTOCOL
*PciIo
;
526 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
529 // SATA Controller is a device driver, and should ingore the
530 // "RemainingDevicePath" according to UEFI spec
532 Status
= gBS
->OpenProtocol (
534 &gEfiDevicePathProtocolGuid
,
535 (VOID
*) &ParentDevicePath
,
536 This
->DriverBindingHandle
,
538 EFI_OPEN_PROTOCOL_BY_DRIVER
540 if (EFI_ERROR (Status
)) {
542 // EFI_ALREADY_STARTED is also an error
547 // Close the protocol because we don't use it here
551 &gEfiDevicePathProtocolGuid
,
552 This
->DriverBindingHandle
,
556 Status
= gBS
->OpenProtocol (
558 &gEfiIdeControllerInitProtocolGuid
,
559 (VOID
**) &IdeControllerInit
,
560 This
->DriverBindingHandle
,
562 EFI_OPEN_PROTOCOL_BY_DRIVER
565 if (EFI_ERROR (Status
)) {
567 // EFI_ALREADY_STARTED is also an error
573 // Close the I/O Abstraction(s) used to perform the supported test
577 &gEfiIdeControllerInitProtocolGuid
,
578 This
->DriverBindingHandle
,
583 // Now test the EfiPciIoProtocol
585 Status
= gBS
->OpenProtocol (
587 &gEfiPciIoProtocolGuid
,
589 This
->DriverBindingHandle
,
591 EFI_OPEN_PROTOCOL_GET_PROTOCOL
593 if (EFI_ERROR (Status
)) {
597 // Now further check the PCI header: Base class (offset 0x0B) and
598 // Sub Class (offset 0x0A). This controller should be an ATA controller
600 Status
= PciIo
->Pci
.Read (
603 PCI_CLASSCODE_OFFSET
,
604 sizeof (PciData
.Hdr
.ClassCode
),
605 PciData
.Hdr
.ClassCode
607 if (EFI_ERROR (Status
)) {
608 return EFI_UNSUPPORTED
;
611 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
615 return EFI_UNSUPPORTED
;
619 Starts a device controller or a bus controller.
621 The Start() function is designed to be invoked from the EFI boot service ConnectController().
622 As a result, much of the error checking on the parameters to Start() has been moved into this
623 common boot service. It is legal to call Start() from other locations,
624 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
625 1. ControllerHandle must be a valid EFI_HANDLE.
626 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
627 EFI_DEVICE_PATH_PROTOCOL.
628 3. Prior to calling Start(), the Supported() function for the driver specified by This must
629 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
631 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
632 @param[in] ControllerHandle The handle of the controller to start. This handle
633 must support a protocol interface that supplies
634 an I/O abstraction to the driver.
635 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
636 parameter is ignored by device drivers, and is optional for bus
637 drivers. For a bus driver, if this parameter is NULL, then handles
638 for all the children of Controller are created by this driver.
639 If this parameter is not NULL and the first Device Path Node is
640 not the End of Device Path Node, then only the handle for the
641 child device specified by the first Device Path Node of
642 RemainingDevicePath is created by this driver.
643 If the first Device Path Node of RemainingDevicePath is
644 the End of Device Path Node, no child handle is created by this
647 @retval EFI_SUCCESS The device was started.
648 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
649 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
650 @retval Others The driver failded to start the device.
655 AtaAtapiPassThruStart (
656 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
657 IN EFI_HANDLE Controller
,
658 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
662 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeControllerInit
;
663 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
664 EFI_PCI_IO_PROTOCOL
*PciIo
;
666 UINT64 OriginalPciAttributes
;
668 Status
= EFI_SUCCESS
;
669 IdeControllerInit
= NULL
;
671 OriginalPciAttributes
= 0;
673 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Start== Controller = %x\n", Controller
));
675 Status
= gBS
->OpenProtocol (
677 &gEfiIdeControllerInitProtocolGuid
,
678 (VOID
**) &IdeControllerInit
,
679 This
->DriverBindingHandle
,
681 EFI_OPEN_PROTOCOL_BY_DRIVER
684 if (EFI_ERROR (Status
)) {
685 DEBUG ((EFI_D_ERROR
, "Open Ide_Controller_Init Error, Status=%r", Status
));
689 Status
= gBS
->OpenProtocol (
691 &gEfiPciIoProtocolGuid
,
693 This
->DriverBindingHandle
,
695 EFI_OPEN_PROTOCOL_GET_PROTOCOL
697 if (EFI_ERROR (Status
)) {
698 DEBUG ((EFI_D_ERROR
, "Get Pci_Io Protocol Error, Status=%r", Status
));
702 Status
= PciIo
->Attributes (
704 EfiPciIoAttributeOperationGet
,
706 &OriginalPciAttributes
709 if (EFI_ERROR (Status
)) {
713 Status
= PciIo
->Attributes (
715 EfiPciIoAttributeOperationSupported
,
719 if (!EFI_ERROR (Status
)) {
720 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
721 Status
= PciIo
->Attributes (
723 EfiPciIoAttributeOperationEnable
,
729 if (EFI_ERROR (Status
)) {
734 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
736 Instance
= AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE
), &gAtaAtapiPassThruInstanceTemplate
);
737 if (Instance
== NULL
) {
741 Instance
->ControllerHandle
= Controller
;
742 Instance
->IdeControllerInit
= IdeControllerInit
;
743 Instance
->PciIo
= PciIo
;
744 Instance
->OriginalPciAttributes
= OriginalPciAttributes
;
745 Instance
->AtaPassThru
.Mode
= &Instance
->AtaPassThruMode
;
746 Instance
->ExtScsiPassThru
.Mode
= &Instance
->ExtScsiPassThruMode
;
747 InitializeListHead(&Instance
->DeviceList
);
748 InitializeListHead(&Instance
->NonBlockingTaskList
);
750 Instance
->TimerEvent
= NULL
;
752 Status
= gBS
->CreateEvent (
753 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
755 AsyncNonBlockingTransferRoutine
,
757 &Instance
->TimerEvent
759 if (EFI_ERROR (Status
)) {
766 Status
= gBS
->SetTimer (Instance
->TimerEvent
, TimerPeriodic
, 10000);
767 if (EFI_ERROR (Status
)) {
772 // Enumerate all inserted ATA devices.
774 Status
= EnumerateAttachedDevice (Instance
);
775 if (EFI_ERROR (Status
)) {
779 Status
= gBS
->InstallMultipleProtocolInterfaces (
781 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
782 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
785 ASSERT_EFI_ERROR (Status
);
790 if (IdeControllerInit
!= NULL
) {
793 &gEfiIdeControllerInitProtocolGuid
,
794 This
->DriverBindingHandle
,
799 if ((Instance
!= NULL
) && (Instance
->TimerEvent
!= NULL
)) {
800 gBS
->CloseEvent (Instance
->TimerEvent
);
804 // Remove all inserted ATA devices.
806 DestroyDeviceInfoList(Instance
);
808 if (Instance
!= NULL
) {
811 return EFI_UNSUPPORTED
;
815 Stops a device controller or a bus controller.
817 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
818 As a result, much of the error checking on the parameters to Stop() has been moved
819 into this common boot service. It is legal to call Stop() from other locations,
820 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
821 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
822 same driver's Start() function.
823 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
824 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
825 Start() function, and the Start() function must have called OpenProtocol() on
826 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
828 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
829 @param[in] ControllerHandle A handle to the device being stopped. The handle must
830 support a bus specific I/O protocol for the driver
831 to use to stop the device.
832 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
833 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
834 if NumberOfChildren is 0.
836 @retval EFI_SUCCESS The device was stopped.
837 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
842 AtaAtapiPassThruStop (
843 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
844 IN EFI_HANDLE Controller
,
845 IN UINTN NumberOfChildren
,
846 IN EFI_HANDLE
*ChildHandleBuffer
850 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
851 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
852 EFI_PCI_IO_PROTOCOL
*PciIo
;
853 EFI_AHCI_REGISTERS
*AhciRegisters
;
856 DEBUG ((EFI_D_INFO
, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller
));
858 Status
= gBS
->OpenProtocol (
860 &gEfiAtaPassThruProtocolGuid
,
861 (VOID
**) &AtaPassThru
,
862 This
->DriverBindingHandle
,
864 EFI_OPEN_PROTOCOL_GET_PROTOCOL
867 if (EFI_ERROR (Status
)) {
868 return EFI_DEVICE_ERROR
;
871 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru
);
873 Status
= gBS
->UninstallMultipleProtocolInterfaces (
875 &gEfiAtaPassThruProtocolGuid
, &(Instance
->AtaPassThru
),
876 &gEfiExtScsiPassThruProtocolGuid
, &(Instance
->ExtScsiPassThru
),
880 if (EFI_ERROR (Status
)) {
881 return EFI_DEVICE_ERROR
;
885 // Close protocols opened by AtaAtapiPassThru controller driver
889 &gEfiIdeControllerInitProtocolGuid
,
890 This
->DriverBindingHandle
,
895 // Close Non-Blocking timer and free Task list.
897 if (Instance
->TimerEvent
!= NULL
) {
898 gBS
->CloseEvent (Instance
->TimerEvent
);
899 Instance
->TimerEvent
= NULL
;
901 DestroyAsynTaskList (Instance
, FALSE
);
903 // Free allocated resource
905 DestroyDeviceInfoList (Instance
);
908 // If the current working mode is AHCI mode, then pre-allocated resource
909 // for AHCI initialization should be released.
911 PciIo
= Instance
->PciIo
;
913 if (Instance
->Mode
== EfiAtaAhciMode
) {
914 AhciRegisters
= &Instance
->AhciRegisters
;
917 AhciRegisters
->MapCommandTable
921 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandTableSize
),
922 AhciRegisters
->AhciCommandTable
926 AhciRegisters
->MapCmdList
930 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxCommandListSize
),
931 AhciRegisters
->AhciCmdList
935 AhciRegisters
->MapRFis
939 EFI_SIZE_TO_PAGES ((UINTN
) AhciRegisters
->MaxReceiveFisSize
),
940 AhciRegisters
->AhciRFis
945 // Disable this ATA host controller.
947 Status
= PciIo
->Attributes (
949 EfiPciIoAttributeOperationSupported
,
953 if (!EFI_ERROR (Status
)) {
954 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
957 EfiPciIoAttributeOperationDisable
,
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 0.
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
);
1007 if ((DeviceInfo
->Type
== DeviceType
) &&
1008 (Port
== DeviceInfo
->Port
) &&
1009 (PortMultiplier
== DeviceInfo
->PortMultiplier
)) {
1013 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1020 Allocate device info data structure to contain device info.
1021 And insert the data structure to the tail of device list for tracing.
1023 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1024 @param[in] Port The port number of the ATA device to send the command.
1025 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1026 If there is no port multiplier, then specify 0.
1027 @param[in] DeviceType The device type of the ATA device.
1028 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1030 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1031 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1036 CreateNewDeviceInfo (
1037 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1039 IN UINT16 PortMultiplier
,
1040 IN EFI_ATA_DEVICE_TYPE DeviceType
,
1041 IN EFI_IDENTIFY_DATA
*IdentifyData
1044 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1046 DeviceInfo
= AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO
));
1048 if (DeviceInfo
== NULL
) {
1049 return EFI_OUT_OF_RESOURCES
;
1052 DeviceInfo
->Signature
= ATA_ATAPI_DEVICE_SIGNATURE
;
1053 DeviceInfo
->Port
= Port
;
1054 DeviceInfo
->PortMultiplier
= PortMultiplier
;
1055 DeviceInfo
->Type
= DeviceType
;
1057 if (IdentifyData
!= NULL
) {
1058 DeviceInfo
->IdentifyData
= AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA
), IdentifyData
);
1059 if (DeviceInfo
->IdentifyData
== NULL
) {
1060 FreePool (DeviceInfo
);
1061 return EFI_OUT_OF_RESOURCES
;
1065 InsertTailList (&Instance
->DeviceList
, &DeviceInfo
->Link
);
1071 Destroy all attached ATA devices info.
1073 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1078 DestroyDeviceInfoList (
1079 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1082 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1085 Node
= GetFirstNode (&Instance
->DeviceList
);
1086 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1087 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1089 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1091 RemoveEntryList (&DeviceInfo
->Link
);
1092 if (DeviceInfo
->IdentifyData
!= NULL
) {
1093 FreePool (DeviceInfo
->IdentifyData
);
1095 FreePool (DeviceInfo
);
1100 Destroy all pending non blocking tasks.
1102 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1103 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1109 DestroyAsynTaskList (
1110 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
,
1111 IN BOOLEAN IsSigEvent
1115 LIST_ENTRY
*DelEntry
;
1116 ATA_NONBLOCK_TASK
*Task
;
1119 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1120 if (!IsListEmpty (&Instance
->NonBlockingTaskList
)) {
1122 // Free the Subtask list.
1124 for (Entry
= (&Instance
->NonBlockingTaskList
)->ForwardLink
;
1125 Entry
!= (&Instance
->NonBlockingTaskList
);
1128 Entry
= Entry
->ForwardLink
;
1129 Task
= ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry
);
1131 RemoveEntryList (DelEntry
);
1133 Task
->Packet
->Asb
->AtaStatus
= 0x01;
1134 gBS
->SignalEvent (Task
->Event
);
1139 gBS
->RestoreTPL (OldTpl
);
1143 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1145 The function is designed to enumerate all attached ATA devices.
1147 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1149 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1150 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1155 EnumerateAttachedDevice (
1156 IN ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
1163 Status
= EFI_SUCCESS
;
1165 Status
= Instance
->PciIo
->Pci
.Read (
1168 PCI_CLASSCODE_OFFSET
,
1169 sizeof (PciData
.Hdr
.ClassCode
),
1170 PciData
.Hdr
.ClassCode
1172 ASSERT_EFI_ERROR (Status
);
1174 ClassCode
= PciData
.Hdr
.ClassCode
[1];
1176 switch (ClassCode
) {
1177 case PCI_CLASS_MASS_STORAGE_IDE
:
1179 // The ATA controller is working at IDE mode
1181 Instance
->Mode
= EfiAtaIdeMode
;
1183 Status
= IdeModeInitialization (Instance
);
1184 if (EFI_ERROR (Status
)) {
1185 Status
= EFI_DEVICE_ERROR
;
1189 case PCI_CLASS_MASS_STORAGE_SATADPA
:
1191 // The ATA controller is working at AHCI mode
1193 Instance
->Mode
= EfiAtaAhciMode
;
1195 Status
= AhciModeInitialization (Instance
);
1197 if (EFI_ERROR (Status
)) {
1198 Status
= EFI_DEVICE_ERROR
;
1204 Status
= EFI_UNSUPPORTED
;
1212 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1213 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1214 and the non-blocking I/O functionality is optional.
1216 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1217 @param[in] Port The port number of the ATA device to send the command.
1218 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1219 If there is no port multiplier, then specify 0.
1220 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1221 and PortMultiplierPort.
1222 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1223 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1224 Event is not NULL and non blocking I/O is supported, then non-blocking
1225 I/O is performed, and Event will be signaled when the ATA command completes.
1227 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1228 InTransferLength bytes were transferred from InDataBuffer. For write and
1229 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1230 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1231 is returned in InTransferLength. For write and bi-directional commands,
1232 OutTransferLength bytes were transferred by OutDataBuffer.
1233 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1234 already queued. The caller may retry again later.
1235 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1236 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1237 command was not sent, so no additional status information is available.
1242 AtaPassThruPassThru (
1243 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1245 IN UINT16 PortMultiplierPort
,
1246 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
,
1247 IN EFI_EVENT Event OPTIONAL
1250 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1252 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1253 EFI_IDENTIFY_DATA
*IdentifyData
;
1255 UINT32 MaxSectorCount
;
1256 ATA_NONBLOCK_TASK
*Task
;
1259 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1261 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1262 return EFI_INVALID_PARAMETER
;
1265 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1266 return EFI_INVALID_PARAMETER
;
1269 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->Asb
, This
->Mode
->IoAlign
)) {
1270 return EFI_INVALID_PARAMETER
;
1273 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1276 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeCdrom
);
1278 return EFI_INVALID_PARAMETER
;
1283 // convert the transfer length from sector count to byte.
1285 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1286 (Packet
->InTransferLength
!= 0)) {
1287 Packet
->InTransferLength
= Packet
->InTransferLength
* 0x200;
1291 // convert the transfer length from sector count to byte.
1293 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
1294 (Packet
->OutTransferLength
!= 0)) {
1295 Packet
->OutTransferLength
= Packet
->OutTransferLength
* 0x200;
1299 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1300 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1301 // If bit10 is one, it means the ata device support 48-bit addressing.
1303 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1304 IdentifyData
= DeviceInfo
->IdentifyData
;
1305 MaxSectorCount
= 0x100;
1306 if ((IdentifyData
->AtaData
.command_set_supported_83
& (BIT10
| BIT15
| BIT14
)) == 0x4400) {
1307 Capacity
= *((UINT64
*)IdentifyData
->AtaData
.maximum_lba_for_48bit_addressing
);
1308 if (Capacity
> 0xFFFFFFF) {
1310 // Capacity exceeds 120GB. 48-bit addressing is really needed
1311 // In this case, the max sector count is 0x10000
1313 MaxSectorCount
= 0x10000;
1318 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1319 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1322 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* 0x200)) ||
1323 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* 0x200))) {
1324 return EFI_BAD_BUFFER_SIZE
;
1328 // For non-blocking mode, queue the Task into the list.
1330 if (Event
!= NULL
) {
1331 Task
= AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK
));
1333 return EFI_OUT_OF_RESOURCES
;
1336 Task
->Signature
= ATA_NONBLOCKING_TASK_SIGNATURE
;
1338 Task
->PortMultiplier
= PortMultiplierPort
;
1339 Task
->Packet
= Packet
;
1340 Task
->Event
= Event
;
1341 Task
->IsStart
= FALSE
;
1342 Task
->RetryTimes
= DivU64x32(Packet
->Timeout
, 1000) + 1;
1343 if (Packet
->Timeout
== 0) {
1344 Task
->InfiniteWait
= TRUE
;
1346 Task
->InfiniteWait
= FALSE
;
1349 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1350 InsertTailList (&Instance
->NonBlockingTaskList
, &Task
->Link
);
1351 gBS
->RestoreTPL (OldTpl
);
1355 return AtaPassThruPassThruExecute (
1366 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1367 These can either be the list of ports where ATA devices are actually present or the
1368 list of legal port numbers for the ATA controller. Regardless, the caller of this
1369 function must probe the port number returned to see if an ATA device is actually
1370 present at that location on the ATA controller.
1372 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1373 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1374 in Port and EFI_SUCCESS is returned.
1376 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1377 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1378 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1379 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1381 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1384 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1385 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1386 On output, a pointer to the next port number on the ATA
1387 controller. An input value of 0xFFFF retrieves the first port
1388 number on the ATA controller.
1390 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1391 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1392 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1398 AtaPassThruGetNextPort (
1399 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1403 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1405 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1407 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1410 return EFI_INVALID_PARAMETER
;
1413 if (*Port
== 0xFFFF) {
1415 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1417 Node
= GetFirstNode (&Instance
->DeviceList
);
1419 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1420 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1422 if (DeviceInfo
->Type
== EfiIdeHarddisk
) {
1423 *Port
= DeviceInfo
->Port
;
1427 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1430 return EFI_NOT_FOUND
;
1431 } else if (*Port
== Instance
->PreviousPort
) {
1432 Node
= GetFirstNode (&Instance
->DeviceList
);
1434 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1435 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1437 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1438 (DeviceInfo
->Port
> *Port
)){
1439 *Port
= DeviceInfo
->Port
;
1443 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1446 return EFI_NOT_FOUND
;
1449 // Port is not equal to 0xFFFF and also not equal to previous return value
1451 return EFI_INVALID_PARAMETER
;
1456 // Update the PreviousPort and PreviousPortMultiplier.
1458 Instance
->PreviousPort
= *Port
;
1464 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1465 controller. These can either be the list of port multiplier ports where ATA devices are actually
1466 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1467 function must probe the port number and port multiplier port number returned to see if an ATA
1468 device is actually present.
1470 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1471 present on a port of an ATA controller.
1473 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1474 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1475 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1478 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1479 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1480 EFI_SUCCESS is returned.
1482 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1483 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1486 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1487 the ATA controller, then EFI_NOT_FOUND is returned.
1489 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1490 @param[in] Port The port number present on the ATA controller.
1491 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1492 ATA device present on the ATA controller.
1493 If on input a PortMultiplierPort of 0xFFFF is specified,
1494 then the port multiplier port number of the first ATA device
1495 is returned. On output, a pointer to the port multiplier port
1496 number of the next ATA device present on an ATA controller.
1498 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1499 of the ATA controller was returned in PortMultiplierPort.
1500 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1501 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1502 returned on a previous call to GetNextDevice().
1507 AtaPassThruGetNextDevice (
1508 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1510 IN OUT UINT16
*PortMultiplierPort
1513 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1515 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1517 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1519 if (PortMultiplierPort
== NULL
) {
1520 return EFI_INVALID_PARAMETER
;
1523 if (*PortMultiplierPort
== 0xFFFF) {
1525 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1527 Node
= GetFirstNode (&Instance
->DeviceList
);
1529 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1530 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1532 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1533 (DeviceInfo
->Port
== Port
)){
1534 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1538 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1541 return EFI_NOT_FOUND
;
1542 } else if (*PortMultiplierPort
== Instance
->PreviousPortMultiplier
) {
1543 Node
= GetFirstNode (&Instance
->DeviceList
);
1545 while (!IsNull (&Instance
->DeviceList
, Node
)) {
1546 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
1548 if ((DeviceInfo
->Type
== EfiIdeHarddisk
) &&
1549 (DeviceInfo
->Port
== Port
) &&
1550 (DeviceInfo
->PortMultiplier
> *PortMultiplierPort
)){
1551 *PortMultiplierPort
= DeviceInfo
->PortMultiplier
;
1555 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
1558 return EFI_NOT_FOUND
;
1561 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1563 return EFI_INVALID_PARAMETER
;
1568 // Update the PreviousPort and PreviousPortMultiplier.
1570 Instance
->PreviousPortMultiplier
= *PortMultiplierPort
;
1576 Used to allocate and build a device path node for an ATA device on an ATA controller.
1578 The BuildDevicePath() function allocates and builds a single device node for the ATA
1579 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1580 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1581 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1582 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1584 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1585 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1586 and EFI_SUCCESS is returned.
1588 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1589 @param[in] Port Port specifies the port number of the ATA device for which a
1590 device path node is to be allocated and built.
1591 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1592 device path node is to be allocated and built. If there is no
1593 port multiplier, then specify 0.
1594 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1595 device specified by Port and PortMultiplierPort. This function
1596 is responsible for allocating the buffer DevicePath with the
1597 boot service AllocatePool(). It is the caller's responsibility
1598 to free DevicePath when the caller is finished with DevicePath.
1599 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1600 Port and PortMultiplierPort was allocated and returned in DevicePath.
1601 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1602 exist on the ATA controller.
1603 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1604 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1609 AtaPassThruBuildDevicePath (
1610 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1612 IN UINT16 PortMultiplierPort
,
1613 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1616 EFI_DEV_PATH
*DevicePathNode
;
1617 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1620 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1623 // Validate parameters passed in.
1625 if (DevicePath
== NULL
) {
1626 return EFI_INVALID_PARAMETER
;
1629 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1631 return EFI_NOT_FOUND
;
1634 if (Instance
->Mode
== EfiAtaIdeMode
) {
1635 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
1636 if (DevicePathNode
== NULL
) {
1637 return EFI_OUT_OF_RESOURCES
;
1639 DevicePathNode
->Atapi
.PrimarySecondary
= (UINT8
) Port
;
1640 DevicePathNode
->Atapi
.SlaveMaster
= (UINT8
) PortMultiplierPort
;
1641 DevicePathNode
->Atapi
.Lun
= 0;
1643 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
1644 if (DevicePathNode
== NULL
) {
1645 return EFI_OUT_OF_RESOURCES
;
1648 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
1649 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplierPort
;
1650 DevicePathNode
->Sata
.Lun
= 0;
1653 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
1659 Used to translate a device path node to a port number and port multiplier port number.
1661 The GetDevice() function determines the port and port multiplier port number associated with
1662 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1663 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1664 DevicePath into a port number and port multiplier port number.
1666 If this translation is successful, then that port number and port multiplier port number are returned
1667 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1669 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1671 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1672 EFI_UNSUPPORTED is returned.
1674 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1675 a valid translation from DevicePath to a port number and port multiplier port number, then
1676 EFI_NOT_FOUND is returned.
1678 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1679 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1681 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1682 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1683 on the ATA controller.
1685 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1686 port number, and they were returned in Port and PortMultiplierPort.
1687 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1688 @retval EFI_INVALID_PARAMETER Port is NULL.
1689 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1690 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1691 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1692 port number does not exist.
1696 AtaPassThruGetDevice (
1697 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1698 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1700 OUT UINT16
*PortMultiplierPort
1703 EFI_DEV_PATH
*DevicePathNode
;
1704 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1707 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1710 // Validate parameters passed in.
1712 if (DevicePath
== NULL
|| Port
== NULL
|| PortMultiplierPort
== NULL
) {
1713 return EFI_INVALID_PARAMETER
;
1717 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1719 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1720 ((DevicePath
->SubType
!= MSG_SATA_DP
) &&
1721 (DevicePath
->SubType
!= MSG_ATAPI_DP
)) ||
1722 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
1723 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
1724 return EFI_UNSUPPORTED
;
1727 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
1729 if (Instance
->Mode
== EfiAtaIdeMode
) {
1730 *Port
= DevicePathNode
->Atapi
.PrimarySecondary
;
1731 *PortMultiplierPort
= DevicePathNode
->Atapi
.SlaveMaster
;
1733 *Port
= DevicePathNode
->Sata
.HBAPortNumber
;
1734 *PortMultiplierPort
= DevicePathNode
->Sata
.PortMultiplierPortNumber
;
1737 Node
= SearchDeviceInfoList(Instance
, *Port
, *PortMultiplierPort
, EfiIdeHarddisk
);
1740 return EFI_NOT_FOUND
;
1747 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1748 connected to the port.
1750 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1751 resets all the ATA devices connected to that port. If this ATA controller does not support
1752 a reset port operation, then EFI_UNSUPPORTED is returned.
1754 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1757 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1759 If the port reset operation is completed, then EFI_SUCCESS is returned.
1761 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1762 @param[in] Port The port number on the ATA controller.
1764 @retval EFI_SUCCESS The ATA controller port was reset.
1765 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1766 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1767 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1772 AtaPassThruResetPort (
1773 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1778 // Return success directly then upper layer driver could think reset port operation is done.
1784 Resets an ATA device that is connected to an ATA controller.
1786 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1787 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1790 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1791 EFI_INVALID_PARAMETER is returned.
1793 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1796 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1799 If the device reset operation is completed, then EFI_SUCCESS is returned.
1801 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1802 @param[in] Port Port represents the port number of the ATA device to be reset.
1803 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1804 If there is no port multiplier, then specify 0.
1805 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1806 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1807 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1808 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1809 specified by Port and PortMultiplierPort.
1810 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1811 specified by Port and PortMultiplierPort.
1816 AtaPassThruResetDevice (
1817 IN EFI_ATA_PASS_THRU_PROTOCOL
*This
,
1819 IN UINT16 PortMultiplierPort
1822 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1825 Instance
= ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1827 Node
= SearchDeviceInfoList (Instance
, Port
, PortMultiplierPort
, EfiIdeHarddisk
);
1830 return EFI_INVALID_PARAMETER
;
1834 // Return success directly then upper layer driver could think reset device operation is done.
1840 Sumbit ATAPI request sense command.
1842 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1843 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1844 the id of the SCSI device to send the SCSI Request Packet. Each
1845 transport driver may choose to utilize a subset of this size to suit the needs
1846 of transport target representation. For example, a Fibre Channel driver
1847 may use only 8 bytes (WWN) to represent an FC target.
1848 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1849 @param[in] SenseData A pointer to store sense data.
1850 @param[in] SenseDataLength The sense data length.
1851 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1853 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1854 @retval EFI_DEVICE_ERROR The device failed to send data.
1859 AtaPacketRequestSense (
1860 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1864 IN UINT8 SenseDataLength
,
1868 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet
;
1872 ZeroMem (&Packet
, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
));
1875 Cdb
[0] = ATA_CMD_REQUEST_SENSE
;
1876 Cdb
[4] = SenseDataLength
;
1878 Packet
.Timeout
= Timeout
;
1880 Packet
.CdbLength
= 12;
1881 Packet
.DataDirection
= EFI_EXT_SCSI_DATA_DIRECTION_READ
;
1882 Packet
.InDataBuffer
= SenseData
;
1883 Packet
.InTransferLength
= SenseDataLength
;
1885 Status
= ExtScsiPassThruPassThru (This
, Target
, Lun
, &Packet
, NULL
);
1891 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1892 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1893 nonblocking I/O functionality is optional.
1895 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1896 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1897 the id of the SCSI device to send the SCSI Request Packet. Each
1898 transport driver may choose to utilize a subset of this size to suit the needs
1899 of transport target representation. For example, a Fibre Channel driver
1900 may use only 8 bytes (WWN) to represent an FC target.
1901 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1902 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1903 specified by Target and Lun.
1904 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1905 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1906 Event is not NULL and non blocking I/O is supported, then
1907 nonblocking I/O is performed, and Event will be signaled when the
1908 SCSI Request Packet completes.
1910 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1911 commands, InTransferLength bytes were transferred from
1912 InDataBuffer. For write and bi-directional commands,
1913 OutTransferLength bytes were transferred by
1915 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
1916 could be transferred is returned in InTransferLength. For write
1917 and bi-directional commands, OutTransferLength bytes were
1918 transferred by OutDataBuffer.
1919 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
1920 SCSI Request Packets already queued. The caller may retry again later.
1921 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1923 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
1924 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
1925 by the host adapter. This includes the case of Bi-directional SCSI
1926 commands not supported by the implementation. The SCSI Request
1927 Packet was not sent, so no additional status information is available.
1928 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1933 ExtScsiPassThruPassThru (
1934 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
1937 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1938 IN EFI_EVENT Event OPTIONAL
1942 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
1944 UINT8 PortMultiplier
;
1945 EFI_ATA_HC_WORK_MODE Mode
;
1947 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
1949 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
1951 EFI_STATUS SenseStatus
;
1954 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
1956 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
1957 return EFI_INVALID_PARAMETER
;
1961 // Don't support variable length CDB
1963 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
1964 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
1965 return EFI_INVALID_PARAMETER
;
1968 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
1969 return EFI_INVALID_PARAMETER
;
1972 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
1973 return EFI_INVALID_PARAMETER
;
1976 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
1977 return EFI_INVALID_PARAMETER
;
1980 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
1981 return EFI_INVALID_PARAMETER
;
1985 // For ATAPI device, doesn't support multiple LUN device.
1988 return EFI_INVALID_PARAMETER
;
1992 // The layout of Target array:
1993 // ________________________________________________________________________
1994 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
1995 // |_____________________|_____________________|_____|______________________|
1996 // | | The port multiplier | | |
1997 // | The port number | port number | N/A | N/A |
1998 // |_____________________|_____________________|_____|______________________|
2000 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2003 PortMultiplier
= Target
[1];
2005 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2007 return EFI_INVALID_PARAMETER
;
2010 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2013 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2014 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2015 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2017 if (*((UINT8
*)Packet
->Cdb
) == ATA_CMD_IDENTIFY_DEVICE
) {
2018 CopyMem (Packet
->InDataBuffer
, DeviceInfo
->IdentifyData
, sizeof (EFI_IDENTIFY_DATA
));
2020 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2022 Packet
->SenseDataLength
= 0;
2026 Mode
= Instance
->Mode
;
2030 // Reassign IDE mode io port registers' base addresses
2032 Status
= GetIdeRegisterIoAddr (Instance
->PciIo
, Instance
->IdeRegisters
);
2034 if (EFI_ERROR (Status
)) {
2038 Status
= AtaPacketCommandExecute (Instance
->PciIo
, &Instance
->IdeRegisters
[Port
], Port
, PortMultiplier
, Packet
);
2040 case EfiAtaAhciMode
:
2041 Status
= AhciPacketCommandExecute (Instance
->PciIo
, &Instance
->AhciRegisters
, Port
, PortMultiplier
, Packet
);
2044 Status
= EFI_DEVICE_ERROR
;
2049 // If the cmd doesn't get executed correctly, then check sense data.
2051 if (EFI_ERROR (Status
) && (Packet
->SenseDataLength
!= 0) && (*((UINT8
*)Packet
->Cdb
) != ATA_CMD_REQUEST_SENSE
)) {
2052 PtrSenseData
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)), This
->Mode
->IoAlign
);
2053 if (PtrSenseData
== NULL
) {
2054 return EFI_DEVICE_ERROR
;
2057 for (SenseReq
= TRUE
; SenseReq
;) {
2058 SenseStatus
= AtaPacketRequestSense (
2063 sizeof (EFI_SCSI_SENSE_DATA
),
2066 if (EFI_ERROR (SenseStatus
)) {
2070 CopyMem ((UINT8
*)Packet
->SenseData
+ SenseDataLen
, PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
2071 SenseDataLen
+= sizeof (EFI_SCSI_SENSE_DATA
);
2074 // no more sense key or number of sense keys exceeds predefined,
2077 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
2078 (SenseDataLen
+ sizeof (EFI_SCSI_SENSE_DATA
) > Packet
->SenseDataLength
)) {
2082 FreeAlignedPages (PtrSenseData
, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA
)));
2085 // Update the SenseDataLength field to the data length received.
2087 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2092 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2093 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2094 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2095 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2098 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2099 @param Target On input, a pointer to the Target ID (an array of size
2100 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2101 On output, a pointer to the Target ID (an array of
2102 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2103 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2104 Target array retrieves the Target ID of the first SCSI device present on a
2106 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2107 channel. On output, a pointer to the LUN of the next SCSI device present
2110 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2111 channel was returned in Target and Lun.
2112 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2113 not returned on a previous call to GetNextTargetLun().
2114 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2119 ExtScsiPassThruGetNextTargetLun (
2120 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2121 IN OUT UINT8
**Target
,
2125 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2127 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2131 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2133 if (Target
== NULL
|| Lun
== NULL
) {
2134 return EFI_INVALID_PARAMETER
;
2137 if (*Target
== NULL
) {
2138 return EFI_INVALID_PARAMETER
;
2142 Target16
= (UINT16
*)*Target
;
2144 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2146 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2147 // So the higher bytes in Target array should be 0xFF.
2149 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2150 return EFI_INVALID_PARAMETER
;
2154 // When Target is not all 0xFF's, compare 2 least significant bytes with
2155 // previous target id to see if it is returned by previous call.
2157 if ((*Target16
!= Instance
->PreviousTargetId
) ||
2158 (*Lun
!= Instance
->PreviousLun
)) {
2159 return EFI_INVALID_PARAMETER
;
2163 // Traverse the whole device list to find the next cdrom closed to
2164 // the device signified by Target[0] and Target[1].
2166 // Note that we here use a tricky way to find the next cdrom :
2167 // All ata devices are detected and inserted into the device list
2170 Node
= GetFirstNode (&Instance
->DeviceList
);
2172 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2173 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2175 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2176 ((Target8
[0] < DeviceInfo
->Port
) ||
2177 ((Target8
[0] == DeviceInfo
->Port
) &&
2178 (Target8
[1] < DeviceInfo
->PortMultiplier
)))) {
2179 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2180 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2184 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2187 return EFI_NOT_FOUND
;
2190 // If the array is all 0xFF's, start to traverse the device list from the beginning
2192 Node
= GetFirstNode (&Instance
->DeviceList
);
2193 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2194 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2196 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2197 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2198 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2202 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2205 return EFI_NOT_FOUND
;
2212 // Update the PreviousTargetId.
2214 Instance
->PreviousTargetId
= *Target16
;
2215 Instance
->PreviousLun
= *Lun
;
2221 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2223 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2224 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2225 Target ID of the SCSI device for which a device path node is to be
2226 allocated and built. Transport drivers may chose to utilize a subset of
2227 this size to suit the representation of targets. For example, a Fibre
2228 Channel driver may use only 8 bytes (WWN) in the array to represent a
2230 @param Lun The LUN of the SCSI device for which a device path node is to be
2231 allocated and built.
2232 @param DevicePath A pointer to a single device path node that describes the SCSI device
2233 specified by Target and Lun. This function is responsible for
2234 allocating the buffer DevicePath with the boot service
2235 AllocatePool(). It is the caller's responsibility to free
2236 DevicePath when the caller is finished with DevicePath.
2238 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2239 Target and Lun was allocated and returned in
2241 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2242 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2243 on the SCSI channel.
2244 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2249 ExtScsiPassThruBuildDevicePath (
2250 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2253 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
2256 EFI_DEV_PATH
*DevicePathNode
;
2257 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2259 UINT8 PortMultiplier
;
2261 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2264 PortMultiplier
= Target
[1];
2267 // Validate parameters passed in.
2269 if (DevicePath
== NULL
) {
2270 return EFI_INVALID_PARAMETER
;
2274 // can not build device path for the SCSI Host Controller.
2277 return EFI_NOT_FOUND
;
2280 if (SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
) == NULL
) {
2281 return EFI_NOT_FOUND
;
2284 if (Instance
->Mode
== EfiAtaIdeMode
) {
2285 DevicePathNode
= AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH
), &mAtapiDevicePathTemplate
);
2286 if (DevicePathNode
== NULL
) {
2287 return EFI_OUT_OF_RESOURCES
;
2290 DevicePathNode
->Atapi
.PrimarySecondary
= Port
;
2291 DevicePathNode
->Atapi
.SlaveMaster
= PortMultiplier
;
2292 DevicePathNode
->Atapi
.Lun
= (UINT16
) Lun
;
2294 DevicePathNode
= AllocateCopyPool (sizeof (SATA_DEVICE_PATH
), &mSataDevicePathTemplate
);
2295 if (DevicePathNode
== NULL
) {
2296 return EFI_OUT_OF_RESOURCES
;
2299 DevicePathNode
->Sata
.HBAPortNumber
= Port
;
2300 DevicePathNode
->Sata
.PortMultiplierPortNumber
= PortMultiplier
;
2301 DevicePathNode
->Sata
.Lun
= (UINT16
) Lun
;
2304 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
2310 Used to translate a device path node to a Target ID and LUN.
2312 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2313 @param DevicePath A pointer to a single device path node that describes the SCSI device
2314 on the SCSI channel.
2315 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2316 on the SCSI channel.
2317 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2319 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2320 LUN, and they were returned in Target and Lun.
2321 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2322 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2324 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2330 ExtScsiPassThruGetTargetLun (
2331 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2332 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2337 EFI_DEV_PATH
*DevicePathNode
;
2338 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2341 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2344 // Validate parameters passed in.
2346 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
2347 return EFI_INVALID_PARAMETER
;
2350 if (*Target
== NULL
) {
2351 return EFI_INVALID_PARAMETER
;
2354 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2356 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
2357 ((DevicePath
->SubType
!= MSG_ATAPI_DP
) &&
2358 (DevicePath
->SubType
!= MSG_SATA_DP
)) ||
2359 ((DevicePathNodeLength(DevicePath
) != sizeof(ATAPI_DEVICE_PATH
)) &&
2360 (DevicePathNodeLength(DevicePath
) != sizeof(SATA_DEVICE_PATH
)))) {
2361 return EFI_UNSUPPORTED
;
2364 SetMem (*Target
, TARGET_MAX_BYTES
, 0xFF);
2366 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
2368 if (Instance
->Mode
== EfiAtaIdeMode
) {
2369 (*Target
)[0] = (UINT8
) DevicePathNode
->Atapi
.PrimarySecondary
;
2370 (*Target
)[1] = (UINT8
) DevicePathNode
->Atapi
.SlaveMaster
;
2371 *Lun
= (UINT8
) DevicePathNode
->Atapi
.Lun
;
2373 (*Target
)[0] = (UINT8
) DevicePathNode
->Sata
.HBAPortNumber
;
2374 (*Target
)[1] = (UINT8
) DevicePathNode
->Sata
.PortMultiplierPortNumber
;
2375 *Lun
= (UINT8
) DevicePathNode
->Sata
.Lun
;
2378 Node
= SearchDeviceInfoList(Instance
, (*Target
)[0], (*Target
)[1], EfiIdeCdrom
);
2381 return EFI_NOT_FOUND
;
2385 return EFI_NOT_FOUND
;
2392 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2394 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2396 @retval EFI_SUCCESS The SCSI channel was reset.
2397 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2398 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2399 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2404 ExtScsiPassThruResetChannel (
2405 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
2409 // Return success directly then upper layer driver could think reset channel operation is done.
2415 Resets a SCSI logical unit that is connected to a SCSI channel.
2417 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2418 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2419 target port ID of the SCSI device containing the SCSI logical unit to
2420 reset. Transport drivers may chose to utilize a subset of this array to suit
2421 the representation of their targets.
2422 @param Lun The LUN of the SCSI device to reset.
2424 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2425 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2426 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2427 specified by Target and Lun.
2428 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2429 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2430 specified by Target and Lun.
2435 ExtScsiPassThruResetTargetLun (
2436 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2441 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2444 UINT8 PortMultiplier
;
2446 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2448 // For ATAPI device, doesn't support multiple LUN device.
2451 return EFI_INVALID_PARAMETER
;
2454 // The layout of Target array:
2455 // ________________________________________________________________________
2456 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2457 // |_____________________|_____________________|_____|______________________|
2458 // | | The port multiplier | | |
2459 // | The port number | port number | N/A | N/A |
2460 // |_____________________|_____________________|_____|______________________|
2462 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2465 PortMultiplier
= Target
[1];
2467 Node
= SearchDeviceInfoList(Instance
, Port
, PortMultiplier
, EfiIdeCdrom
);
2469 return EFI_INVALID_PARAMETER
;
2473 // Return success directly then upper layer driver could think reset target LUN operation is done.
2479 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2480 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2481 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2482 see if a SCSI device is actually present at that location on the SCSI channel.
2484 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2485 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2486 On output, a pointer to the Target ID (an array of
2487 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2488 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2489 Target array retrieves the Target ID of the first SCSI device present on a
2492 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2493 channel was returned in Target.
2494 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2495 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2496 returned on a previous call to GetNextTarget().
2497 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2502 ExtScsiPassThruGetNextTarget (
2503 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
2504 IN OUT UINT8
**Target
2507 ATA_ATAPI_PASS_THRU_INSTANCE
*Instance
;
2509 EFI_ATA_DEVICE_INFO
*DeviceInfo
;
2513 Instance
= EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
2515 if (Target
== NULL
|| *Target
== NULL
) {
2516 return EFI_INVALID_PARAMETER
;
2520 Target16
= (UINT16
*)*Target
;
2522 if (CompareMem(Target8
, mScsiId
, TARGET_MAX_BYTES
) != 0) {
2524 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2525 // So the higher bytes in Target array should be 0xFF.
2527 if (CompareMem (&Target8
[2], &mScsiId
[2], TARGET_MAX_BYTES
- 2) != 0) {
2528 return EFI_INVALID_PARAMETER
;
2532 // When Target is not all 0xFF's, compare 2 least significant bytes with
2533 // previous target id to see if it is returned by previous call.
2535 if (*Target16
!= Instance
->PreviousTargetId
) {
2536 return EFI_INVALID_PARAMETER
;
2540 // Traverse the whole device list to find the next cdrom closed to
2541 // the device signified by Target[0] and Target[1].
2543 // Note that we here use a tricky way to find the next cdrom :
2544 // All ata devices are detected and inserted into the device list
2547 Node
= GetFirstNode (&Instance
->DeviceList
);
2548 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2549 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2551 if ((DeviceInfo
->Type
== EfiIdeCdrom
) &&
2552 ((Target8
[0] < DeviceInfo
->Port
) ||
2553 ((Target8
[0] == DeviceInfo
->Port
) &&
2554 (Target8
[1] < DeviceInfo
->PortMultiplier
)))) {
2555 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2556 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2560 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2563 return EFI_NOT_FOUND
;
2566 // If the array is all 0xFF's, start to traverse the device list from the beginning
2568 Node
= GetFirstNode (&Instance
->DeviceList
);
2570 while (!IsNull (&Instance
->DeviceList
, Node
)) {
2571 DeviceInfo
= ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node
);
2573 if (DeviceInfo
->Type
== EfiIdeCdrom
) {
2574 Target8
[0] = (UINT8
)DeviceInfo
->Port
;
2575 Target8
[1] = (UINT8
)DeviceInfo
->PortMultiplier
;
2579 Node
= GetNextNode (&Instance
->DeviceList
, Node
);
2582 return EFI_NOT_FOUND
;
2587 // Update the PreviousTargetId.
2589 Instance
->PreviousTargetId
= *Target16
;