]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AhciPei / AhciPeiPassThru.c
1 /** @file
2 The AhciPei driver is used to manage ATA hard disk device working under AHCI
3 mode at PEI phase.
4
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "AhciPei.h"
12
13 /**
14 Traverse the attached ATA devices list to find out the device with given Port
15 and PortMultiplierPort.
16
17 @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
18 instance.
19 @param[in] Port The port number of the ATA device.
20 @param[in] PortMultiplierPort The port multiplier port number of the ATA device.
21
22 @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device
23 info to access.
24
25 **/
26 PEI_AHCI_ATA_DEVICE_DATA *
27 SearchDeviceByPort (
28 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,
29 IN UINT16 Port,
30 IN UINT16 PortMultiplierPort
31 )
32 {
33 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
34 LIST_ENTRY *Node;
35
36 Node = GetFirstNode (&Private->DeviceList);
37 while (!IsNull (&Private->DeviceList, Node)) {
38 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
39
40 if ((DeviceData->Port == Port) &&
41 (DeviceData->PortMultiplier == PortMultiplierPort))
42 {
43 return DeviceData;
44 }
45
46 Node = GetNextNode (&Private->DeviceList, Node);
47 }
48
49 return NULL;
50 }
51
52 /**
53 Sends an ATA command to an ATA device that is attached to the ATA controller.
54
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
58 device.
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
62 PortMultiplierPort.
63
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
73 by OutDataBuffer.
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
78 send the ATA command.
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.
82
83 **/
84 EFI_STATUS
85 AhciPassThruExecute (
86 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,
87 IN UINT16 Port,
88 IN UINT16 PortMultiplierPort,
89 IN UINT8 FisIndex,
90 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet
91 )
92 {
93 EFI_STATUS Status;
94
95 switch (Packet->Protocol) {
96 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:
97 Status = AhciNonDataTransfer (
98 Private,
99 (UINT8)Port,
100 (UINT8)PortMultiplierPort,
101 FisIndex,
102 Packet->Acb,
103 Packet->Asb,
104 Packet->Timeout
105 );
106 break;
107 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:
108 Status = AhciPioTransfer (
109 Private,
110 (UINT8)Port,
111 (UINT8)PortMultiplierPort,
112 FisIndex,
113 TRUE,
114 Packet->Acb,
115 Packet->Asb,
116 Packet->InDataBuffer,
117 Packet->InTransferLength,
118 Packet->Timeout
119 );
120 break;
121 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:
122 Status = AhciPioTransfer (
123 Private,
124 (UINT8)Port,
125 (UINT8)PortMultiplierPort,
126 FisIndex,
127 FALSE,
128 Packet->Acb,
129 Packet->Asb,
130 Packet->OutDataBuffer,
131 Packet->OutTransferLength,
132 Packet->Timeout
133 );
134 break;
135 default:
136 return EFI_UNSUPPORTED;
137 }
138
139 return Status;
140 }
141
142 /**
143 Sends an ATA command to an ATA device that is attached to the ATA controller.
144
145 @param[in] This The PPI instance pointer.
146 @param[in] Port The port number of the ATA device to send
147 the command.
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
151 0xFFFF.
152 @param[in,out] Packet A pointer to the ATA command to send to
153 the ATA device specified by Port and
154 PortMultiplierPort.
155
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
164 is available.
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
169 by OutDataBuffer.
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.
175
176 **/
177 EFI_STATUS
178 EFIAPI
179 AhciAtaPassThruPassThru (
180 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
181 IN UINT16 Port,
182 IN UINT16 PortMultiplierPort,
183 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet
184 )
185 {
186 UINT32 IoAlign;
187 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
188 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
189 UINT32 MaxSectorCount;
190 UINT32 BlockSize;
191
192 if ((This == NULL) || (Packet == NULL)) {
193 return EFI_INVALID_PARAMETER;
194 }
195
196 IoAlign = This->Mode->IoAlign;
197 if ((IoAlign > 1) && !IS_ALIGNED (Packet->InDataBuffer, IoAlign)) {
198 return EFI_INVALID_PARAMETER;
199 }
200
201 if ((IoAlign > 1) && !IS_ALIGNED (Packet->OutDataBuffer, IoAlign)) {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) {
206 return EFI_INVALID_PARAMETER;
207 }
208
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;
213 }
214
215 MaxSectorCount = mMaxTransferBlockNumber[DeviceData->Lba48Bit];
216 BlockSize = DeviceData->Media.BlockSize;
217
218 //
219 // Convert the transfer length from sector count to byte.
220 //
221 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
222 (Packet->InTransferLength != 0))
223 {
224 Packet->InTransferLength = Packet->InTransferLength * BlockSize;
225 }
226
227 //
228 // Convert the transfer length from sector count to byte.
229 //
230 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
231 (Packet->OutTransferLength != 0))
232 {
233 Packet->OutTransferLength = Packet->OutTransferLength * BlockSize;
234 }
235
236 //
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.
240 //
241 if (((Packet->InTransferLength != 0) && (Packet->InTransferLength > MaxSectorCount * BlockSize)) ||
242 ((Packet->OutTransferLength != 0) && (Packet->OutTransferLength > MaxSectorCount * BlockSize)))
243 {
244 return EFI_BAD_BUFFER_SIZE;
245 }
246
247 return AhciPassThruExecute (
248 Private,
249 DeviceData->Port,
250 DeviceData->PortMultiplier,
251 DeviceData->FisIndex,
252 Packet
253 );
254 }
255
256 /**
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.
262
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.
266
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.
271
272 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND
273 is returned.
274
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.
280
281 @retval EFI_SUCCESS The next port number on the ATA controller was
282 returned in Port.
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().
286
287 **/
288 EFI_STATUS
289 EFIAPI
290 AhciAtaPassThruGetNextPort (
291 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
292 IN OUT UINT16 *Port
293 )
294 {
295 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
296 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
297 LIST_ENTRY *Node;
298
299 if ((This == NULL) || (Port == NULL)) {
300 return EFI_INVALID_PARAMETER;
301 }
302
303 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
304
305 if (*Port == 0xFFFF) {
306 //
307 // If the Port is all 0xFF's, start to traverse the device list from the
308 // beginning.
309 //
310 Node = GetFirstNode (&Private->DeviceList);
311 if (!IsNull (&Private->DeviceList, Node)) {
312 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
313
314 *Port = DeviceData->Port;
315 goto Exit;
316 }
317
318 return EFI_NOT_FOUND;
319 } else if (*Port == Private->PreviousPort) {
320 Node = GetFirstNode (&Private->DeviceList);
321
322 while (!IsNull (&Private->DeviceList, Node)) {
323 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
324
325 if (DeviceData->Port > *Port) {
326 *Port = DeviceData->Port;
327 goto Exit;
328 }
329
330 Node = GetNextNode (&Private->DeviceList, Node);
331 }
332
333 return EFI_NOT_FOUND;
334 } else {
335 //
336 // Port is not equal to all 0xFF's and not equal to previous return value.
337 //
338 return EFI_INVALID_PARAMETER;
339 }
340
341 Exit:
342 //
343 // Update the PreviousPort.
344 //
345 Private->PreviousPort = *Port;
346
347 return EFI_SUCCESS;
348 }
349
350 /**
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
356 is actually present.
357
358 The GetNextDevice() function retrieves the port multiplier port number of an ATA
359 device present on a port of an ATA controller.
360
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.
365
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.
369
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
372 is returned.
373
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.
376
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
386 controller.
387
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
392 the ATA controller.
393 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort
394 was not returned on a previous call to GetNextDevice().
395
396 **/
397 EFI_STATUS
398 EFIAPI
399 AhciAtaPassThruGetNextDevice (
400 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
401 IN UINT16 Port,
402 IN OUT UINT16 *PortMultiplierPort
403 )
404 {
405 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
406 PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
407 LIST_ENTRY *Node;
408
409 if ((This == NULL) || (PortMultiplierPort == NULL)) {
410 return EFI_INVALID_PARAMETER;
411 }
412
413 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
414
415 if (Private->PreviousPortMultiplier == 0xFFFF) {
416 //
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.
420 //
421 Private->PreviousPortMultiplier = 0;
422 return EFI_NOT_FOUND;
423 }
424
425 if (*PortMultiplierPort == Private->PreviousPortMultiplier) {
426 Node = GetFirstNode (&Private->DeviceList);
427
428 while (!IsNull (&Private->DeviceList, Node)) {
429 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
430
431 if ((DeviceData->Port == Port) &&
432 (DeviceData->PortMultiplier > *PortMultiplierPort))
433 {
434 *PortMultiplierPort = DeviceData->PortMultiplier;
435 goto Exit;
436 }
437
438 Node = GetNextNode (&Private->DeviceList, Node);
439 }
440
441 return EFI_NOT_FOUND;
442 } else if (*PortMultiplierPort == 0xFFFF) {
443 //
444 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list
445 // from the beginning.
446 //
447 Node = GetFirstNode (&Private->DeviceList);
448
449 while (!IsNull (&Private->DeviceList, Node)) {
450 DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
451
452 if (DeviceData->Port == Port) {
453 *PortMultiplierPort = DeviceData->PortMultiplier;
454 goto Exit;
455 }
456
457 Node = GetNextNode (&Private->DeviceList, Node);
458 }
459
460 return EFI_NOT_FOUND;
461 } else {
462 //
463 // PortMultiplierPort is not equal to all 0xFF's and not equal to previous
464 // return value.
465 //
466 return EFI_INVALID_PARAMETER;
467 }
468
469 Exit:
470 //
471 // Update the PreviousPortMultiplier.
472 //
473 Private->PreviousPortMultiplier = *PortMultiplierPort;
474
475 return EFI_SUCCESS;
476 }
477
478 /**
479 Gets the device path information of the underlying ATA host controller.
480
481 @param[in] This The PPI instance pointer.
482 @param[out] DevicePathLength The length of the device path in bytes specified
483 by DevicePath.
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.
488
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.
493
494 **/
495 EFI_STATUS
496 EFIAPI
497 AhciAtaPassThruGetDevicePath (
498 IN EDKII_PEI_ATA_PASS_THRU_PPI *This,
499 OUT UINTN *DevicePathLength,
500 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
501 )
502 {
503 PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
504
505 if ((This == NULL) || (DevicePathLength == NULL) || (DevicePath == NULL)) {
506 return EFI_INVALID_PARAMETER;
507 }
508
509 Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_PASS_THRU (This);
510
511 *DevicePathLength = Private->DevicePathLength;
512 *DevicePath = AllocateCopyPool (Private->DevicePathLength, Private->DevicePath);
513 if (*DevicePath == NULL) {
514 *DevicePathLength = 0;
515 return EFI_OUT_OF_RESOURCES;
516 }
517
518 return EFI_SUCCESS;
519 }