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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Traverse the attached ATA devices list to find out the device with given Port
22 and PortMultiplierPort.
24 @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
26 @param[in] Port The port number of the ATA device.
27 @param[in] PortMultiplierPort The port multiplier port number of the ATA device.
29 @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device
33 PEI_AHCI_ATA_DEVICE_DATA
*
35 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
,
37 IN UINT16 PortMultiplierPort
40 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
43 Node
= GetFirstNode (&Private
->DeviceList
);
44 while (!IsNull (&Private
->DeviceList
, Node
)) {
45 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
47 if ((DeviceData
->Port
== Port
) &&
48 (DeviceData
->PortMultiplier
== PortMultiplierPort
)) {
52 Node
= GetNextNode (&Private
->DeviceList
, Node
);
59 Sends an ATA command to an ATA device that is attached to the ATA controller.
61 @param[in] Private Pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA.
62 @param[in] Port The port number of the ATA device.
63 @param[in] PortMultiplierPort The port multiplier port number of the ATA
65 @param[in] FisIndex The index of the FIS.
66 @param[in,out] Packet A pointer to the ATA command to send to
67 the ATA device specified by Port and
70 @retval EFI_SUCCESS The ATA command was sent by the host. For
71 bi-directional commands, InTransferLength bytes
72 were transferred from InDataBuffer. For write
73 and bi-directional commands, OutTransferLength
74 bytes were transferred by OutDataBuffer.
75 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
76 of bytes that could be transferred is returned
77 in InTransferLength. For write and bi-directional
78 commands, OutTransferLength bytes were transferred
80 @retval EFI_NOT_READY The ATA command could not be sent because there
81 are too many ATA commands already queued. The
82 caller may retry again later.
83 @retval EFI_DEVICE_ERROR A device error occurred while attempting to
85 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of
86 Acb are invalid. The ATA command was not sent,
87 so no additional status information is available.
92 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
,
94 IN UINT16 PortMultiplierPort
,
96 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
101 switch (Packet
->Protocol
) {
102 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA
:
103 Status
= AhciNonDataTransfer (
106 (UINT8
) PortMultiplierPort
,
113 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
:
114 Status
= AhciPioTransfer (
117 (UINT8
) PortMultiplierPort
,
122 Packet
->InDataBuffer
,
123 Packet
->InTransferLength
,
127 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
:
128 Status
= AhciPioTransfer (
131 (UINT8
) PortMultiplierPort
,
136 Packet
->OutDataBuffer
,
137 Packet
->OutTransferLength
,
142 return EFI_UNSUPPORTED
;
149 Sends an ATA command to an ATA device that is attached to the ATA controller.
151 @param[in] This The PPI instance pointer.
152 @param[in] Port The port number of the ATA device to send
154 @param[in] PortMultiplierPort The port multiplier port number of the ATA
155 device to send the command.
156 If there is no port multiplier, then specify
158 @param[in,out] Packet A pointer to the ATA command to send to
159 the ATA device specified by Port and
162 @retval EFI_SUCCESS The ATA command was sent by the host. For
163 bi-directional commands, InTransferLength bytes
164 were transferred from InDataBuffer. For write
165 and bi-directional commands, OutTransferLength
166 bytes were transferred by OutDataBuffer.
167 @retval EFI_NOT_FOUND The specified ATA device is not found.
168 @retval EFI_INVALID_PARAMETER The contents of Acb are invalid. The ATA command
169 was not sent, so no additional status information
171 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
172 of bytes that could be transferred is returned
173 in InTransferLength. For write and bi-directional
174 commands, OutTransferLength bytes were transferred
176 @retval EFI_NOT_READY The ATA command could not be sent because there
177 are too many ATA commands already queued. The
178 caller may retry again later.
179 @retval EFI_DEVICE_ERROR A device error occurred while attempting to
180 send the ATA command.
185 AhciAtaPassThruPassThru (
186 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
188 IN UINT16 PortMultiplierPort
,
189 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
193 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
194 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
195 UINT32 MaxSectorCount
;
198 if (This
== NULL
|| Packet
== NULL
) {
199 return EFI_INVALID_PARAMETER
;
202 IoAlign
= This
->Mode
->IoAlign
;
203 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->InDataBuffer
, IoAlign
)) {
204 return EFI_INVALID_PARAMETER
;
207 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->OutDataBuffer
, IoAlign
)) {
208 return EFI_INVALID_PARAMETER
;
211 if ((IoAlign
> 1) && !IS_ALIGNED (Packet
->Asb
, IoAlign
)) {
212 return EFI_INVALID_PARAMETER
;
215 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
216 DeviceData
= SearchDeviceByPort (Private
, Port
, PortMultiplierPort
);
217 if (DeviceData
== NULL
) {
218 return EFI_NOT_FOUND
;
221 MaxSectorCount
= mMaxTransferBlockNumber
[DeviceData
->Lba48Bit
];
222 BlockSize
= DeviceData
->Media
.BlockSize
;
225 // Convert the transfer length from sector count to byte.
227 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
228 (Packet
->InTransferLength
!= 0)) {
229 Packet
->InTransferLength
= Packet
->InTransferLength
* BlockSize
;
233 // Convert the transfer length from sector count to byte.
235 if (((Packet
->Length
& EFI_ATA_PASS_THRU_LENGTH_BYTES
) == 0) &&
236 (Packet
->OutTransferLength
!= 0)) {
237 Packet
->OutTransferLength
= Packet
->OutTransferLength
* BlockSize
;
241 // If the data buffer described by InDataBuffer/OutDataBuffer and
242 // InTransferLength/OutTransferLength is too big to be transferred in a single
243 // command, then no data is transferred and EFI_BAD_BUFFER_SIZE is returned.
245 if (((Packet
->InTransferLength
!= 0) && (Packet
->InTransferLength
> MaxSectorCount
* BlockSize
)) ||
246 ((Packet
->OutTransferLength
!= 0) && (Packet
->OutTransferLength
> MaxSectorCount
* BlockSize
))) {
247 return EFI_BAD_BUFFER_SIZE
;
250 return AhciPassThruExecute (
253 DeviceData
->PortMultiplier
,
254 DeviceData
->FisIndex
,
260 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
261 These can either be the list of ports where ATA devices are actually present or the
262 list of legal port numbers for the ATA controller. Regardless, the caller of this
263 function must probe the port number returned to see if an ATA device is actually
264 present at that location on the ATA controller.
266 The GetNextPort() function retrieves the port number on an ATA controller. If on
267 input Port is 0xFFFF, then the port number of the first port on the ATA controller
268 is returned in Port and EFI_SUCCESS is returned.
270 If Port is a port number that was returned on a previous call to GetNextPort(),
271 then the port number of the next port on the ATA controller is returned in Port,
272 and EFI_SUCCESS is returned. If Port is not 0xFFFF and Port was not returned on
273 a previous call to GetNextPort(), then EFI_INVALID_PARAMETER is returned.
275 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND
278 @param[in] This The PPI instance pointer.
279 @param[in,out] Port On input, a pointer to the port number on the ATA controller.
280 On output, a pointer to the next port number on the ATA
281 controller. An input value of 0xFFFF retrieves the first
282 port number on the ATA controller.
284 @retval EFI_SUCCESS The next port number on the ATA controller was
286 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
287 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned
288 on a previous call to GetNextPort().
293 AhciAtaPassThruGetNextPort (
294 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
298 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
299 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
302 if (This
== NULL
|| Port
== NULL
) {
303 return EFI_INVALID_PARAMETER
;
306 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
308 if (*Port
== 0xFFFF) {
310 // If the Port is all 0xFF's, start to traverse the device list from the
313 Node
= GetFirstNode (&Private
->DeviceList
);
314 if (!IsNull (&Private
->DeviceList
, Node
)) {
315 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
317 *Port
= DeviceData
->Port
;
321 return EFI_NOT_FOUND
;
322 } else if (*Port
== Private
->PreviousPort
) {
323 Node
= GetFirstNode (&Private
->DeviceList
);
325 while (!IsNull (&Private
->DeviceList
, Node
)) {
326 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
328 if (DeviceData
->Port
> *Port
){
329 *Port
= DeviceData
->Port
;
333 Node
= GetNextNode (&Private
->DeviceList
, Node
);
336 return EFI_NOT_FOUND
;
339 // Port is not equal to all 0xFF's and not equal to previous return value.
341 return EFI_INVALID_PARAMETER
;
346 // Update the PreviousPort.
348 Private
->PreviousPort
= *Port
;
354 Used to retrieve the list of legal port multiplier port numbers for ATA devices
355 on a port of an ATA controller. These can either be the list of port multiplier
356 ports where ATA devices are actually present on port or the list of legal port
357 multiplier ports on that port. Regardless, the caller of this function must probe
358 the port number and port multiplier port number returned to see if an ATA device
361 The GetNextDevice() function retrieves the port multiplier port number of an ATA
362 device present on a port of an ATA controller.
364 If PortMultiplierPort points to a port multiplier port number value that was
365 returned on a previous call to GetNextDevice(), then the port multiplier port
366 number of the next ATA device on the port of the ATA controller is returned in
367 PortMultiplierPort, and EFI_SUCCESS is returned.
369 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number
370 of the first ATA device on port of the ATA controller is returned in PortMultiplierPort
371 and EFI_SUCCESS is returned.
373 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
374 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
377 If PortMultiplierPort is the port multiplier port number of the last ATA device
378 on the port of the ATA controller, then EFI_NOT_FOUND is returned.
380 @param[in] This The PPI instance pointer.
381 @param[in] Port The port number present on the ATA controller.
382 @param[in,out] PortMultiplierPort On input, a pointer to the port multiplier
383 port number of an ATA device present on the
384 ATA controller. If on input a PortMultiplierPort
385 of 0xFFFF is specified, then the port multiplier
386 port number of the first ATA device is returned.
387 On output, a pointer to the port multiplier port
388 number of the next ATA device present on an ATA
391 @retval EFI_SUCCESS The port multiplier port number of the next ATA
392 device on the port of the ATA controller was
393 returned in PortMultiplierPort.
394 @retval EFI_NOT_FOUND There are no more ATA devices on this port of
396 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort
397 was not returned on a previous call to GetNextDevice().
402 AhciAtaPassThruGetNextDevice (
403 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
405 IN OUT UINT16
*PortMultiplierPort
408 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
409 PEI_AHCI_ATA_DEVICE_DATA
*DeviceData
;
412 if (This
== NULL
|| PortMultiplierPort
== NULL
) {
413 return EFI_INVALID_PARAMETER
;
416 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
418 if (Private
->PreviousPortMultiplier
== 0xFFFF) {
420 // If a device is directly attached on a port, previous call to this
421 // function will return the value 0xFFFF for PortMultiplierPort. In
422 // this case, there should be no more device on the port multiplier.
424 Private
->PreviousPortMultiplier
= 0;
425 return EFI_NOT_FOUND
;
428 if (*PortMultiplierPort
== Private
->PreviousPortMultiplier
) {
429 Node
= GetFirstNode (&Private
->DeviceList
);
431 while (!IsNull (&Private
->DeviceList
, Node
)) {
432 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
434 if ((DeviceData
->Port
== Port
) &&
435 (DeviceData
->PortMultiplier
> *PortMultiplierPort
)){
436 *PortMultiplierPort
= DeviceData
->PortMultiplier
;
440 Node
= GetNextNode (&Private
->DeviceList
, Node
);
443 return EFI_NOT_FOUND
;
444 } else if (*PortMultiplierPort
== 0xFFFF) {
446 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list
447 // from the beginning.
449 Node
= GetFirstNode (&Private
->DeviceList
);
451 while (!IsNull (&Private
->DeviceList
, Node
)) {
452 DeviceData
= AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node
);
454 if (DeviceData
->Port
== Port
){
455 *PortMultiplierPort
= DeviceData
->PortMultiplier
;
459 Node
= GetNextNode (&Private
->DeviceList
, Node
);
462 return EFI_NOT_FOUND
;
465 // PortMultiplierPort is not equal to all 0xFF's and not equal to previous
468 return EFI_INVALID_PARAMETER
;
473 // Update the PreviousPortMultiplier.
475 Private
->PreviousPortMultiplier
= *PortMultiplierPort
;
481 Gets the device path information of the underlying ATA host controller.
483 @param[in] This The PPI instance pointer.
484 @param[out] DevicePathLength The length of the device path in bytes specified
486 @param[out] DevicePath The device path of the underlying ATA host controller.
487 This field re-uses EFI Device Path Protocol as
488 defined by Section 10.2 EFI Device Path Protocol
489 of UEFI 2.7 Specification.
491 @retval EFI_SUCCESS The device path of the ATA host controller has
492 been successfully returned.
493 @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.
494 @retval EFI_OUT_OF_RESOURCES Not enough resource to return the device path.
499 AhciAtaPassThruGetDevicePath (
500 IN EDKII_PEI_ATA_PASS_THRU_PPI
*This
,
501 OUT UINTN
*DevicePathLength
,
502 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
505 PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
;
507 if (This
== NULL
|| DevicePathLength
== NULL
|| DevicePath
== NULL
) {
508 return EFI_INVALID_PARAMETER
;
511 Private
= GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This
);
513 *DevicePathLength
= Private
->DevicePathLength
;
514 *DevicePath
= AllocateCopyPool (Private
->DevicePathLength
, Private
->DevicePath
);
515 if (*DevicePath
== NULL
) {
516 *DevicePathLength
= 0;
517 return EFI_OUT_OF_RESOURCES
;