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