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