]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AhciPei / AhciPeiPassThru.c
CommitLineData
87bc3f19
HW
1/** @file\r
2 The AhciPei driver is used to manage ATA hard disk device working under AHCI\r
3 mode at PEI phase.\r
4\r
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
6\r
9d510e61 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
87bc3f19
HW
8\r
9**/\r
10\r
11#include "AhciPei.h"\r
12\r
13/**\r
14 Traverse the attached ATA devices list to find out the device with given Port\r
15 and PortMultiplierPort.\r
16\r
17 @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA\r
18 instance.\r
19 @param[in] Port The port number of the ATA device.\r
20 @param[in] PortMultiplierPort The port multiplier port number of the ATA device.\r
21\r
22 @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device\r
23 info to access.\r
24\r
25**/\r
26PEI_AHCI_ATA_DEVICE_DATA *\r
27SearchDeviceByPort (\r
1436aea4
MK
28 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,\r
29 IN UINT16 Port,\r
30 IN UINT16 PortMultiplierPort\r
87bc3f19
HW
31 )\r
32{\r
1436aea4
MK
33 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;\r
34 LIST_ENTRY *Node;\r
87bc3f19
HW
35\r
36 Node = GetFirstNode (&Private->DeviceList);\r
37 while (!IsNull (&Private->DeviceList, Node)) {\r
38 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);\r
39\r
40 if ((DeviceData->Port == Port) &&\r
1436aea4
MK
41 (DeviceData->PortMultiplier == PortMultiplierPort))\r
42 {\r
87bc3f19
HW
43 return DeviceData;\r
44 }\r
45\r
46 Node = GetNextNode (&Private->DeviceList, Node);\r
47 }\r
48\r
49 return NULL;\r
50}\r
51\r
52/**\r
53 Sends an ATA command to an ATA device that is attached to the ATA controller.\r
54\r
55 @param[in] Private Pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA.\r
56 @param[in] Port The port number of the ATA device.\r
57 @param[in] PortMultiplierPort The port multiplier port number of the ATA\r
58 device.\r
59 @param[in] FisIndex The index of the FIS.\r
60 @param[in,out] Packet A pointer to the ATA command to send to\r
61 the ATA device specified by Port and\r
62 PortMultiplierPort.\r
63\r
64 @retval EFI_SUCCESS The ATA command was sent by the host. For\r
65 bi-directional commands, InTransferLength bytes\r
66 were transferred from InDataBuffer. For write\r
67 and bi-directional commands, OutTransferLength\r
68 bytes were transferred by OutDataBuffer.\r
69 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number\r
70 of bytes that could be transferred is returned\r
71 in InTransferLength. For write and bi-directional\r
72 commands, OutTransferLength bytes were transferred\r
73 by OutDataBuffer.\r
74 @retval EFI_NOT_READY The ATA command could not be sent because there\r
75 are too many ATA commands already queued. The\r
76 caller may retry again later.\r
77 @retval EFI_DEVICE_ERROR A device error occurred while attempting to\r
78 send the ATA command.\r
79 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of\r
80 Acb are invalid. The ATA command was not sent,\r
81 so no additional status information is available.\r
82\r
83**/\r
84EFI_STATUS\r
85AhciPassThruExecute (\r
1436aea4
MK
86 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,\r
87 IN UINT16 Port,\r
88 IN UINT16 PortMultiplierPort,\r
89 IN UINT8 FisIndex,\r
90 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet\r
87bc3f19
HW
91 )\r
92{\r
1436aea4 93 EFI_STATUS Status;\r
87bc3f19
HW
94\r
95 switch (Packet->Protocol) {\r
96 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:\r
97 Status = AhciNonDataTransfer (\r
98 Private,\r
1436aea4
MK
99 (UINT8)Port,\r
100 (UINT8)PortMultiplierPort,\r
87bc3f19
HW
101 FisIndex,\r
102 Packet->Acb,\r
103 Packet->Asb,\r
104 Packet->Timeout\r
105 );\r
106 break;\r
107 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:\r
108 Status = AhciPioTransfer (\r
109 Private,\r
1436aea4
MK
110 (UINT8)Port,\r
111 (UINT8)PortMultiplierPort,\r
87bc3f19
HW
112 FisIndex,\r
113 TRUE,\r
114 Packet->Acb,\r
115 Packet->Asb,\r
116 Packet->InDataBuffer,\r
117 Packet->InTransferLength,\r
118 Packet->Timeout\r
119 );\r
120 break;\r
121 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:\r
122 Status = AhciPioTransfer (\r
123 Private,\r
1436aea4
MK
124 (UINT8)Port,\r
125 (UINT8)PortMultiplierPort,\r
87bc3f19
HW
126 FisIndex,\r
127 FALSE,\r
128 Packet->Acb,\r
129 Packet->Asb,\r
130 Packet->OutDataBuffer,\r
131 Packet->OutTransferLength,\r
132 Packet->Timeout\r
133 );\r
134 break;\r
135 default:\r
136 return EFI_UNSUPPORTED;\r
137 }\r
138\r
139 return Status;\r
140}\r
141\r
142/**\r
143 Sends an ATA command to an ATA device that is attached to the ATA controller.\r
144\r
145 @param[in] This The PPI instance pointer.\r
146 @param[in] Port The port number of the ATA device to send\r
147 the command.\r
148 @param[in] PortMultiplierPort The port multiplier port number of the ATA\r
149 device to send the command.\r
150 If there is no port multiplier, then specify\r
151 0xFFFF.\r
152 @param[in,out] Packet A pointer to the ATA command to send to\r
153 the ATA device specified by Port and\r
154 PortMultiplierPort.\r
155\r
156 @retval EFI_SUCCESS The ATA command was sent by the host. For\r
157 bi-directional commands, InTransferLength bytes\r
158 were transferred from InDataBuffer. For write\r
159 and bi-directional commands, OutTransferLength\r
160 bytes were transferred by OutDataBuffer.\r
161 @retval EFI_NOT_FOUND The specified ATA device is not found.\r
162 @retval EFI_INVALID_PARAMETER The contents of Acb are invalid. The ATA command\r
163 was not sent, so no additional status information\r
164 is available.\r
165 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number\r
166 of bytes that could be transferred is returned\r
167 in InTransferLength. For write and bi-directional\r
168 commands, OutTransferLength bytes were transferred\r
169 by OutDataBuffer.\r
170 @retval EFI_NOT_READY The ATA command could not be sent because there\r
171 are too many ATA commands already queued. The\r
172 caller may retry again later.\r
173 @retval EFI_DEVICE_ERROR A device error occurred while attempting to\r
174 send the ATA command.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179AhciAtaPassThruPassThru (\r
1436aea4
MK
180 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,\r
181 IN UINT16 Port,\r
182 IN UINT16 PortMultiplierPort,\r
183 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet\r
87bc3f19
HW
184 )\r
185{\r
1436aea4
MK
186 UINT32 IoAlign;\r
187 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;\r
188 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;\r
189 UINT32 MaxSectorCount;\r
190 UINT32 BlockSize;\r
87bc3f19 191\r
1436aea4 192 if ((This == NULL) || (Packet == NULL)) {\r
87bc3f19
HW
193 return EFI_INVALID_PARAMETER;\r
194 }\r
195\r
196 IoAlign = This->Mode->IoAlign;\r
197 if ((IoAlign > 1) && !IS_ALIGNED (Packet->InDataBuffer, IoAlign)) {\r
198 return EFI_INVALID_PARAMETER;\r
199 }\r
200\r
201 if ((IoAlign > 1) && !IS_ALIGNED (Packet->OutDataBuffer, IoAlign)) {\r
202 return EFI_INVALID_PARAMETER;\r
203 }\r
204\r
205 if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) {\r
206 return EFI_INVALID_PARAMETER;\r
207 }\r
208\r
1436aea4 209 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);\r
87bc3f19
HW
210 DeviceData = SearchDeviceByPort (Private, Port, PortMultiplierPort);\r
211 if (DeviceData == NULL) {\r
212 return EFI_NOT_FOUND;\r
213 }\r
214\r
215 MaxSectorCount = mMaxTransferBlockNumber[DeviceData->Lba48Bit];\r
216 BlockSize = DeviceData->Media.BlockSize;\r
217\r
218 //\r
219 // Convert the transfer length from sector count to byte.\r
220 //\r
221 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&\r
1436aea4
MK
222 (Packet->InTransferLength != 0))\r
223 {\r
87bc3f19
HW
224 Packet->InTransferLength = Packet->InTransferLength * BlockSize;\r
225 }\r
226\r
227 //\r
228 // Convert the transfer length from sector count to byte.\r
229 //\r
230 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&\r
1436aea4
MK
231 (Packet->OutTransferLength != 0))\r
232 {\r
87bc3f19
HW
233 Packet->OutTransferLength = Packet->OutTransferLength * BlockSize;\r
234 }\r
235\r
236 //\r
237 // If the data buffer described by InDataBuffer/OutDataBuffer and\r
238 // InTransferLength/OutTransferLength is too big to be transferred in a single\r
239 // command, then no data is transferred and EFI_BAD_BUFFER_SIZE is returned.\r
240 //\r
241 if (((Packet->InTransferLength != 0) && (Packet->InTransferLength > MaxSectorCount * BlockSize)) ||\r
1436aea4
MK
242 ((Packet->OutTransferLength != 0) && (Packet->OutTransferLength > MaxSectorCount * BlockSize)))\r
243 {\r
87bc3f19
HW
244 return EFI_BAD_BUFFER_SIZE;\r
245 }\r
246\r
247 return AhciPassThruExecute (\r
248 Private,\r
249 DeviceData->Port,\r
250 DeviceData->PortMultiplier,\r
251 DeviceData->FisIndex,\r
252 Packet\r
253 );\r
254}\r
255\r
256/**\r
257 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.\r
258 These can either be the list of ports where ATA devices are actually present or the\r
259 list of legal port numbers for the ATA controller. Regardless, the caller of this\r
260 function must probe the port number returned to see if an ATA device is actually\r
261 present at that location on the ATA controller.\r
262\r
263 The GetNextPort() function retrieves the port number on an ATA controller. If on\r
264 input Port is 0xFFFF, then the port number of the first port on the ATA controller\r
265 is returned in Port and EFI_SUCCESS is returned.\r
266\r
267 If Port is a port number that was returned on a previous call to GetNextPort(),\r
268 then the port number of the next port on the ATA controller is returned in Port,\r
269 and EFI_SUCCESS is returned. If Port is not 0xFFFF and Port was not returned on\r
270 a previous call to GetNextPort(), then EFI_INVALID_PARAMETER is returned.\r
271\r
272 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND\r
273 is returned.\r
274\r
275 @param[in] This The PPI instance pointer.\r
276 @param[in,out] Port On input, a pointer to the port number on the ATA controller.\r
277 On output, a pointer to the next port number on the ATA\r
278 controller. An input value of 0xFFFF retrieves the first\r
279 port number on the ATA controller.\r
280\r
281 @retval EFI_SUCCESS The next port number on the ATA controller was\r
282 returned in Port.\r
283 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.\r
284 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned\r
285 on a previous call to GetNextPort().\r
286\r
287**/\r
288EFI_STATUS\r
289EFIAPI\r
290AhciAtaPassThruGetNextPort (\r
1436aea4
MK
291 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,\r
292 IN OUT UINT16 *Port\r
87bc3f19
HW
293 )\r
294{\r
1436aea4
MK
295 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;\r
296 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;\r
297 LIST_ENTRY *Node;\r
87bc3f19 298\r
1436aea4 299 if ((This == NULL) || (Port == NULL)) {\r
87bc3f19
HW
300 return EFI_INVALID_PARAMETER;\r
301 }\r
302\r
303 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);\r
304\r
305 if (*Port == 0xFFFF) {\r
306 //\r
307 // If the Port is all 0xFF's, start to traverse the device list from the\r
308 // beginning.\r
309 //\r
310 Node = GetFirstNode (&Private->DeviceList);\r
311 if (!IsNull (&Private->DeviceList, Node)) {\r
312 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);\r
313\r
314 *Port = DeviceData->Port;\r
315 goto Exit;\r
316 }\r
317\r
318 return EFI_NOT_FOUND;\r
319 } else if (*Port == Private->PreviousPort) {\r
320 Node = GetFirstNode (&Private->DeviceList);\r
321\r
322 while (!IsNull (&Private->DeviceList, Node)) {\r
323 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);\r
324\r
1436aea4 325 if (DeviceData->Port > *Port) {\r
87bc3f19
HW
326 *Port = DeviceData->Port;\r
327 goto Exit;\r
328 }\r
329\r
330 Node = GetNextNode (&Private->DeviceList, Node);\r
331 }\r
332\r
333 return EFI_NOT_FOUND;\r
334 } else {\r
335 //\r
336 // Port is not equal to all 0xFF's and not equal to previous return value.\r
337 //\r
338 return EFI_INVALID_PARAMETER;\r
339 }\r
340\r
341Exit:\r
342 //\r
343 // Update the PreviousPort.\r
344 //\r
345 Private->PreviousPort = *Port;\r
346\r
347 return EFI_SUCCESS;\r
348}\r
349\r
350/**\r
351 Used to retrieve the list of legal port multiplier port numbers for ATA devices\r
352 on a port of an ATA controller. These can either be the list of port multiplier\r
353 ports where ATA devices are actually present on port or the list of legal port\r
354 multiplier ports on that port. Regardless, the caller of this function must probe\r
355 the port number and port multiplier port number returned to see if an ATA device\r
356 is actually present.\r
357\r
358 The GetNextDevice() function retrieves the port multiplier port number of an ATA\r
359 device present on a port of an ATA controller.\r
360\r
361 If PortMultiplierPort points to a port multiplier port number value that was\r
362 returned on a previous call to GetNextDevice(), then the port multiplier port\r
363 number of the next ATA device on the port of the ATA controller is returned in\r
364 PortMultiplierPort, and EFI_SUCCESS is returned.\r
365\r
366 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number\r
367 of the first ATA device on port of the ATA controller is returned in PortMultiplierPort\r
368 and EFI_SUCCESS is returned.\r
369\r
370 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort\r
371 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER\r
372 is returned.\r
373\r
374 If PortMultiplierPort is the port multiplier port number of the last ATA device\r
375 on the port of the ATA controller, then EFI_NOT_FOUND is returned.\r
376\r
377 @param[in] This The PPI instance pointer.\r
378 @param[in] Port The port number present on the ATA controller.\r
379 @param[in,out] PortMultiplierPort On input, a pointer to the port multiplier\r
380 port number of an ATA device present on the\r
381 ATA controller. If on input a PortMultiplierPort\r
382 of 0xFFFF is specified, then the port multiplier\r
383 port number of the first ATA device is returned.\r
384 On output, a pointer to the port multiplier port\r
385 number of the next ATA device present on an ATA\r
386 controller.\r
387\r
388 @retval EFI_SUCCESS The port multiplier port number of the next ATA\r
389 device on the port of the ATA controller was\r
390 returned in PortMultiplierPort.\r
391 @retval EFI_NOT_FOUND There are no more ATA devices on this port of\r
392 the ATA controller.\r
393 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort\r
394 was not returned on a previous call to GetNextDevice().\r
395\r
396**/\r
397EFI_STATUS\r
398EFIAPI\r
399AhciAtaPassThruGetNextDevice (\r
1436aea4
MK
400 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,\r
401 IN UINT16 Port,\r
402 IN OUT UINT16 *PortMultiplierPort\r
87bc3f19
HW
403 )\r
404{\r
1436aea4
MK
405 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;\r
406 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;\r
407 LIST_ENTRY *Node;\r
87bc3f19 408\r
1436aea4 409 if ((This == NULL) || (PortMultiplierPort == NULL)) {\r
87bc3f19
HW
410 return EFI_INVALID_PARAMETER;\r
411 }\r
412\r
413 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);\r
414\r
415 if (Private->PreviousPortMultiplier == 0xFFFF) {\r
416 //\r
417 // If a device is directly attached on a port, previous call to this\r
418 // function will return the value 0xFFFF for PortMultiplierPort. In\r
419 // this case, there should be no more device on the port multiplier.\r
420 //\r
421 Private->PreviousPortMultiplier = 0;\r
422 return EFI_NOT_FOUND;\r
423 }\r
424\r
425 if (*PortMultiplierPort == Private->PreviousPortMultiplier) {\r
426 Node = GetFirstNode (&Private->DeviceList);\r
427\r
428 while (!IsNull (&Private->DeviceList, Node)) {\r
429 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);\r
430\r
431 if ((DeviceData->Port == Port) &&\r
1436aea4
MK
432 (DeviceData->PortMultiplier > *PortMultiplierPort))\r
433 {\r
87bc3f19
HW
434 *PortMultiplierPort = DeviceData->PortMultiplier;\r
435 goto Exit;\r
436 }\r
437\r
438 Node = GetNextNode (&Private->DeviceList, Node);\r
439 }\r
440\r
441 return EFI_NOT_FOUND;\r
442 } else if (*PortMultiplierPort == 0xFFFF) {\r
443 //\r
444 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list\r
445 // from the beginning.\r
446 //\r
447 Node = GetFirstNode (&Private->DeviceList);\r
448\r
449 while (!IsNull (&Private->DeviceList, Node)) {\r
450 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);\r
451\r
1436aea4 452 if (DeviceData->Port == Port) {\r
87bc3f19
HW
453 *PortMultiplierPort = DeviceData->PortMultiplier;\r
454 goto Exit;\r
455 }\r
456\r
457 Node = GetNextNode (&Private->DeviceList, Node);\r
458 }\r
459\r
460 return EFI_NOT_FOUND;\r
461 } else {\r
462 //\r
463 // PortMultiplierPort is not equal to all 0xFF's and not equal to previous\r
464 // return value.\r
465 //\r
466 return EFI_INVALID_PARAMETER;\r
467 }\r
468\r
469Exit:\r
470 //\r
471 // Update the PreviousPortMultiplier.\r
472 //\r
473 Private->PreviousPortMultiplier = *PortMultiplierPort;\r
474\r
475 return EFI_SUCCESS;\r
476}\r
477\r
478/**\r
479 Gets the device path information of the underlying ATA host controller.\r
480\r
481 @param[in] This The PPI instance pointer.\r
482 @param[out] DevicePathLength The length of the device path in bytes specified\r
483 by DevicePath.\r
484 @param[out] DevicePath The device path of the underlying ATA host controller.\r
485 This field re-uses EFI Device Path Protocol as\r
486 defined by Section 10.2 EFI Device Path Protocol\r
487 of UEFI 2.7 Specification.\r
488\r
489 @retval EFI_SUCCESS The device path of the ATA host controller has\r
490 been successfully returned.\r
491 @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.\r
492 @retval EFI_OUT_OF_RESOURCES Not enough resource to return the device path.\r
493\r
494**/\r
495EFI_STATUS\r
496EFIAPI\r
497AhciAtaPassThruGetDevicePath (\r
1436aea4
MK
498 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,\r
499 OUT UINTN *DevicePathLength,\r
500 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
87bc3f19
HW
501 )\r
502{\r
1436aea4 503 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;\r
87bc3f19 504\r
1436aea4 505 if ((This == NULL) || (DevicePathLength == NULL) || (DevicePath == NULL)) {\r
87bc3f19
HW
506 return EFI_INVALID_PARAMETER;\r
507 }\r
508\r
509 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);\r
510\r
511 *DevicePathLength = Private->DevicePathLength;\r
512 *DevicePath = AllocateCopyPool (Private->DevicePathLength, Private->DevicePath);\r
513 if (*DevicePath == NULL) {\r
514 *DevicePathLength = 0;\r
515 return EFI_OUT_OF_RESOURCES;\r
516 }\r
517\r
518 return EFI_SUCCESS;\r
519}\r