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