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