2 The AhciPei driver is used to manage ATA hard disk device working under AHCI
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Traverse the attached ATA devices list to find out the device with given Port
15 and PortMultiplierPort.
17 @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
19 @param[in] Port The port number of the ATA device.
20 @param[in] PortMultiplierPort The port multiplier port number of the ATA device.
22 @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device
26 PEI_AHCI_ATA_DEVICE_DATA
*
28 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
,
30 IN UINT16 PortMultiplierPort
33 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
36 Node
= GetFirstNode (&Private
->DeviceList
);
37 while (!IsNull (&Private
->DeviceList
, Node
)) {
38 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
40 if ((DeviceData
->Port
== Port
) &&
41 (DeviceData
->PortMultiplier
== PortMultiplierPort
))
46 Node
= GetNextNode (&Private
->DeviceList
, Node
);
53 Sends an ATA command to an ATA device that is attached to the ATA controller.
55 @param[in] Private Pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA.
56 @param[in] Port The port number of the ATA device.
57 @param[in] PortMultiplierPort The port multiplier port number of the ATA
59 @param[in] FisIndex The index of the FIS.
60 @param[in,out] Packet A pointer to the ATA command to send to
61 the ATA device specified by Port and
64 @retval EFI_SUCCESS The ATA command was sent by the host. For
65 bi-directional commands, InTransferLength bytes
66 were transferred from InDataBuffer. For write
67 and bi-directional commands, OutTransferLength
68 bytes were transferred by OutDataBuffer.
69 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
70 of bytes that could be transferred is returned
71 in InTransferLength. For write and bi-directional
72 commands, OutTransferLength bytes were transferred
74 @retval EFI_NOT_READY The ATA command could not be sent because there
75 are too many ATA commands already queued. The
76 caller may retry again later.
77 @retval EFI_DEVICE_ERROR A device error occurred while attempting to
79 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of
80 Acb are invalid. The ATA command was not sent,
81 so no additional status information is available.
86 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
,
88 IN UINT16 PortMultiplierPort
,
90 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
95 switch (Packet
->Protocol
) {
96 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
97 Status
= AhciNonDataTransfer (
100 (UINT8
)PortMultiplierPort
,
107 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
108 Status
= AhciPioTransfer (
111 (UINT8
)PortMultiplierPort
,
116 Packet
->InDataBuffer
,
117 Packet
->InTransferLength
,
121 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
122 Status
= AhciPioTransfer (
125 (UINT8
)PortMultiplierPort
,
130 Packet
->OutDataBuffer
,
131 Packet
->OutTransferLength
,
136 return EFI_UNSUPPORTED
;
143 Sends an ATA command to an ATA device that is attached to the ATA controller.
145 @param[in] This The PPI instance pointer.
146 @param[in] Port The port number of the ATA device to send
148 @param[in] PortMultiplierPort The port multiplier port number of the ATA
149 device to send the command.
150 If there is no port multiplier, then specify
152 @param[in,out] Packet A pointer to the ATA command to send to
153 the ATA device specified by Port and
156 @retval EFI_SUCCESS The ATA command was sent by the host. For
157 bi-directional commands, InTransferLength bytes
158 were transferred from InDataBuffer. For write
159 and bi-directional commands, OutTransferLength
160 bytes were transferred by OutDataBuffer.
161 @retval EFI_NOT_FOUND The specified ATA device is not found.
162 @retval EFI_INVALID_PARAMETER The contents of Acb are invalid. The ATA command
163 was not sent, so no additional status information
165 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
166 of bytes that could be transferred is returned
167 in InTransferLength. For write and bi-directional
168 commands, OutTransferLength bytes were transferred
170 @retval EFI_NOT_READY The ATA command could not be sent because there
171 are too many ATA commands already queued. The
172 caller may retry again later.
173 @retval EFI_DEVICE_ERROR A device error occurred while attempting to
174 send the ATA command.
179 AhciAtaPassThruPassThru (
180 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
182 IN UINT16 PortMultiplierPort
,
183 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
187 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
188 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
189 UINT32 MaxSectorCount
;
192 if ((This
== NULL
) || (Packet
== NULL
)) {
193 return EFI_INVALID_PARAMETER
;
196 IoAlign
= This
->Mode
->IoAlign
;
197 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->InDataBuffer
, IoAlign
)) {
198 return EFI_INVALID_PARAMETER
;
201 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->OutDataBuffer
, IoAlign
)) {
202 return EFI_INVALID_PARAMETER
;
205 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->Asb
, IoAlign
)) {
206 return EFI_INVALID_PARAMETER
;
209 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
210 DeviceData
= SearchDeviceByPort (Private
, Port
, PortMultiplierPort
);
211 if (DeviceData
== NULL
) {
212 return EFI_NOT_FOUND
;
215 MaxSectorCount
= mMaxTransferBlockNumber
[DeviceData
->Lba48Bit
];
216 BlockSize
= DeviceData
->Media
.BlockSize
;
219 // Convert the transfer length from sector count to byte.
221 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
222 (Packet
->InTransferLength
!= 0))
224 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
228 // Convert the transfer length from sector count to byte.
230 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
231 (Packet
->OutTransferLength
!= 0))
233 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
237 // If the data buffer described by InDataBuffer/OutDataBuffer and
238 // InTransferLength/OutTransferLength is too big to be transferred in a single
239 // command, then no data is transferred and EFI_BAD_BUFFER_SIZE is returned.
241 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
242 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
)))
244 return EFI_BAD_BUFFER_SIZE
;
247 return AhciPassThruExecute (
250 DeviceData
->PortMultiplier
,
251 DeviceData
->FisIndex
,
257 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
258 These can either be the list of ports where ATA devices are actually present or the
259 list of legal port numbers for the ATA controller. Regardless, the caller of this
260 function must probe the port number returned to see if an ATA device is actually
261 present at that location on the ATA controller.
263 The GetNextPort() function retrieves the port number on an ATA controller. If on
264 input Port is 0xFFFF, then the port number of the first port on the ATA controller
265 is returned in Port and EFI_SUCCESS is returned.
267 If Port is a port number that was returned on a previous call to GetNextPort(),
268 then the port number of the next port on the ATA controller is returned in Port,
269 and EFI_SUCCESS is returned. If Port is not 0xFFFF and Port was not returned on
270 a previous call to GetNextPort(), then EFI_INVALID_PARAMETER is returned.
272 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND
275 @param[in] This The PPI instance pointer.
276 @param[in,out] Port On input, a pointer to the port number on the ATA controller.
277 On output, a pointer to the next port number on the ATA
278 controller. An input value of 0xFFFF retrieves the first
279 port number on the ATA controller.
281 @retval EFI_SUCCESS The next port number on the ATA controller was
283 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
284 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned
285 on a previous call to GetNextPort().
290 AhciAtaPassThruGetNextPort (
291 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
295 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
296 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
299 if ((This
== NULL
) || (Port
== NULL
)) {
300 return EFI_INVALID_PARAMETER
;
303 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
305 if (*Port
== 0xFFFF) {
307 // If the Port is all 0xFF's, start to traverse the device list from the
310 Node
= GetFirstNode (&Private
->DeviceList
);
311 if (!IsNull (&Private
->DeviceList
, Node
)) {
312 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
314 *Port
= DeviceData
->Port
;
318 return EFI_NOT_FOUND
;
319 } else if (*Port
== Private
->PreviousPort
) {
320 Node
= GetFirstNode (&Private
->DeviceList
);
322 while (!IsNull (&Private
->DeviceList
, Node
)) {
323 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
325 if (DeviceData
->Port
> *Port
) {
326 *Port
= DeviceData
->Port
;
330 Node
= GetNextNode (&Private
->DeviceList
, Node
);
333 return EFI_NOT_FOUND
;
336 // Port is not equal to all 0xFF's and not equal to previous return value.
338 return EFI_INVALID_PARAMETER
;
343 // Update the PreviousPort.
345 Private
->PreviousPort
= *Port
;
351 Used to retrieve the list of legal port multiplier port numbers for ATA devices
352 on a port of an ATA controller. These can either be the list of port multiplier
353 ports where ATA devices are actually present on port or the list of legal port
354 multiplier ports on that port. Regardless, the caller of this function must probe
355 the port number and port multiplier port number returned to see if an ATA device
358 The GetNextDevice() function retrieves the port multiplier port number of an ATA
359 device present on a port of an ATA controller.
361 If PortMultiplierPort points to a port multiplier port number value that was
362 returned on a previous call to GetNextDevice(), then the port multiplier port
363 number of the next ATA device on the port of the ATA controller is returned in
364 PortMultiplierPort, and EFI_SUCCESS is returned.
366 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number
367 of the first ATA device on port of the ATA controller is returned in PortMultiplierPort
368 and EFI_SUCCESS is returned.
370 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
371 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
374 If PortMultiplierPort is the port multiplier port number of the last ATA device
375 on the port of the ATA controller, then EFI_NOT_FOUND is returned.
377 @param[in] This The PPI instance pointer.
378 @param[in] Port The port number present on the ATA controller.
379 @param[in,out] PortMultiplierPort On input, a pointer to the port multiplier
380 port number of an ATA device present on the
381 ATA controller. If on input a PortMultiplierPort
382 of 0xFFFF is specified, then the port multiplier
383 port number of the first ATA device is returned.
384 On output, a pointer to the port multiplier port
385 number of the next ATA device present on an ATA
388 @retval EFI_SUCCESS The port multiplier port number of the next ATA
389 device on the port of the ATA controller was
390 returned in PortMultiplierPort.
391 @retval EFI_NOT_FOUND There are no more ATA devices on this port of
393 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort
394 was not returned on a previous call to GetNextDevice().
399 AhciAtaPassThruGetNextDevice (
400 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
402 IN OUT UINT16
*PortMultiplierPort
405 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
406 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
409 if ((This
== NULL
) || (PortMultiplierPort
== NULL
)) {
410 return EFI_INVALID_PARAMETER
;
413 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
415 if (Private
->PreviousPortMultiplier
== 0xFFFF) {
417 // If a device is directly attached on a port, previous call to this
418 // function will return the value 0xFFFF for PortMultiplierPort. In
419 // this case, there should be no more device on the port multiplier.
421 Private
->PreviousPortMultiplier
= 0;
422 return EFI_NOT_FOUND
;
425 if (*PortMultiplierPort
== Private
->PreviousPortMultiplier
) {
426 Node
= GetFirstNode (&Private
->DeviceList
);
428 while (!IsNull (&Private
->DeviceList
, Node
)) {
429 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
431 if ((DeviceData
->Port
== Port
) &&
432 (DeviceData
->PortMultiplier
> *PortMultiplierPort
))
434 *PortMultiplierPort
= DeviceData
->PortMultiplier
;
438 Node
= GetNextNode (&Private
->DeviceList
, Node
);
441 return EFI_NOT_FOUND
;
442 } else if (*PortMultiplierPort
== 0xFFFF) {
444 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list
445 // from the beginning.
447 Node
= GetFirstNode (&Private
->DeviceList
);
449 while (!IsNull (&Private
->DeviceList
, Node
)) {
450 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
452 if (DeviceData
->Port
== Port
) {
453 *PortMultiplierPort
= DeviceData
->PortMultiplier
;
457 Node
= GetNextNode (&Private
->DeviceList
, Node
);
460 return EFI_NOT_FOUND
;
463 // PortMultiplierPort is not equal to all 0xFF's and not equal to previous
466 return EFI_INVALID_PARAMETER
;
471 // Update the PreviousPortMultiplier.
473 Private
->PreviousPortMultiplier
= *PortMultiplierPort
;
479 Gets the device path information of the underlying ATA host controller.
481 @param[in] This The PPI instance pointer.
482 @param[out] DevicePathLength The length of the device path in bytes specified
484 @param[out] DevicePath The device path of the underlying ATA host controller.
485 This field re-uses EFI Device Path Protocol as
486 defined by Section 10.2 EFI Device Path Protocol
487 of UEFI 2.7 Specification.
489 @retval EFI_SUCCESS The device path of the ATA host controller has
490 been successfully returned.
491 @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.
492 @retval EFI_OUT_OF_RESOURCES Not enough resource to return the device path.
497 AhciAtaPassThruGetDevicePath (
498 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
499 OUT UINTN
*DevicePathLength
,
500 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
503 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
505 if ((This
== NULL
) || (DevicePathLength
== NULL
) || (DevicePath
== NULL
)) {
506 return EFI_INVALID_PARAMETER
;
509 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
511 *DevicePathLength
= Private
->DevicePathLength
;
512 *DevicePath
= AllocateCopyPool (Private
->DevicePathLength
, Private
->DevicePath
);
513 if (*DevicePath
== NULL
) {
514 *DevicePathLength
= 0;
515 return EFI_OUT_OF_RESOURCES
;