]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c
Add new adding ATA related status code in PI 1.3 to definition and ATA modules.
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaAtapiPassThru / AhciMode.c
1 /** @file
2 The file for AHCI mode of ATA host controller.
3
4 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "AtaAtapiPassThru.h"
16
17 /**
18 Read AHCI Operation register.
19
20 @param PciIo The PCI IO protocol instance.
21 @param Offset The operation register offset.
22
23 @return The register content read.
24
25 **/
26 UINT32
27 EFIAPI
28 AhciReadReg (
29 IN EFI_PCI_IO_PROTOCOL *PciIo,
30 IN UINT32 Offset
31 )
32 {
33 UINT32 Data;
34
35 ASSERT (PciIo != NULL);
36
37 Data = 0;
38
39 PciIo->Mem.Read (
40 PciIo,
41 EfiPciIoWidthUint32,
42 EFI_AHCI_BAR_INDEX,
43 (UINT64) Offset,
44 1,
45 &Data
46 );
47
48 return Data;
49 }
50
51 /**
52 Write AHCI Operation register.
53
54 @param PciIo The PCI IO protocol instance.
55 @param Offset The operation register offset.
56 @param Data The data used to write down.
57
58 **/
59 VOID
60 EFIAPI
61 AhciWriteReg (
62 IN EFI_PCI_IO_PROTOCOL *PciIo,
63 IN UINT32 Offset,
64 IN UINT32 Data
65 )
66 {
67 ASSERT (PciIo != NULL);
68
69 PciIo->Mem.Write (
70 PciIo,
71 EfiPciIoWidthUint32,
72 EFI_AHCI_BAR_INDEX,
73 (UINT64) Offset,
74 1,
75 &Data
76 );
77
78 return ;
79 }
80
81 /**
82 Do AND operation with the value of AHCI Operation register.
83
84 @param PciIo The PCI IO protocol instance.
85 @param Offset The operation register offset.
86 @param AndData The data used to do AND operation.
87
88 **/
89 VOID
90 EFIAPI
91 AhciAndReg (
92 IN EFI_PCI_IO_PROTOCOL *PciIo,
93 IN UINT32 Offset,
94 IN UINT32 AndData
95 )
96 {
97 UINT32 Data;
98
99 ASSERT (PciIo != NULL);
100
101 Data = AhciReadReg (PciIo, Offset);
102
103 Data &= AndData;
104
105 AhciWriteReg (PciIo, Offset, Data);
106 }
107
108 /**
109 Do OR operation with the value of AHCI Operation register.
110
111 @param PciIo The PCI IO protocol instance.
112 @param Offset The operation register offset.
113 @param OrData The data used to do OR operation.
114
115 **/
116 VOID
117 EFIAPI
118 AhciOrReg (
119 IN EFI_PCI_IO_PROTOCOL *PciIo,
120 IN UINT32 Offset,
121 IN UINT32 OrData
122 )
123 {
124 UINT32 Data;
125
126 ASSERT (PciIo != NULL);
127
128 Data = AhciReadReg (PciIo, Offset);
129
130 Data |= OrData;
131
132 AhciWriteReg (PciIo, Offset, Data);
133 }
134
135 /**
136 Wait for the value of the specified MMIO register set to the test value.
137
138 @param PciIo The PCI IO protocol instance.
139 @param Offset The MMIO address to test.
140 @param MaskValue The mask value of memory.
141 @param TestValue The test value of memory.
142 @param Timeout The time out value for wait memory set, uses 100ns as a unit.
143
144 @retval EFI_TIMEOUT The MMIO setting is time out.
145 @retval EFI_SUCCESS The MMIO is correct set.
146
147 **/
148 EFI_STATUS
149 EFIAPI
150 AhciWaitMmioSet (
151 IN EFI_PCI_IO_PROTOCOL *PciIo,
152 IN UINTN Offset,
153 IN UINT32 MaskValue,
154 IN UINT32 TestValue,
155 IN UINT64 Timeout
156 )
157 {
158 UINT32 Value;
159 UINT32 Delay;
160
161 Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1);
162
163 do {
164 //
165 // Access PCI MMIO space to see if the value is the tested one.
166 //
167 Value = AhciReadReg (PciIo, (UINT32) Offset) & MaskValue;
168
169 if (Value == TestValue) {
170 return EFI_SUCCESS;
171 }
172
173 //
174 // Stall for 100 microseconds.
175 //
176 MicroSecondDelay (100);
177
178 Delay--;
179
180 } while (Delay > 0);
181
182 return EFI_TIMEOUT;
183 }
184
185 /**
186 Wait for the value of the specified system memory set to the test value.
187
188 @param Address The system memory address to test.
189 @param MaskValue The mask value of memory.
190 @param TestValue The test value of memory.
191 @param Timeout The time out value for wait memory set, uses 100ns as a unit.
192
193 @retval EFI_TIMEOUT The system memory setting is time out.
194 @retval EFI_SUCCESS The system memory is correct set.
195
196 **/
197 EFI_STATUS
198 EFIAPI
199 AhciWaitMemSet (
200 IN EFI_PHYSICAL_ADDRESS Address,
201 IN UINT32 MaskValue,
202 IN UINT32 TestValue,
203 IN UINT64 Timeout
204 )
205 {
206 UINT32 Value;
207 UINT32 Delay;
208
209 Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1);
210
211 do {
212 //
213 // Access sytem memory to see if the value is the tested one.
214 //
215 // The system memory pointed by Address will be updated by the
216 // SATA Host Controller, "volatile" is introduced to prevent
217 // compiler from optimizing the access to the memory address
218 // to only read once.
219 //
220 Value = *(volatile UINT32 *) (UINTN) Address;
221 Value &= MaskValue;
222
223 if (Value == TestValue) {
224 return EFI_SUCCESS;
225 }
226
227 //
228 // Stall for 100 microseconds.
229 //
230 MicroSecondDelay (100);
231
232 Delay--;
233
234 } while (Delay > 0);
235
236 return EFI_TIMEOUT;
237 }
238
239 /**
240 Check the memory status to the test value.
241
242 @param[in] Address The memory address to test.
243 @param[in] MaskValue The mask value of memory.
244 @param[in] TestValue The test value of memory.
245 @param[in, out] RetryTimes The retry times value for waitting memory set. If 0, then just try once.
246
247 @retval EFI_NOTREADY The memory is not set.
248 @retval EFI_TIMEOUT The memory setting retry times out.
249 @retval EFI_SUCCESS The memory is correct set.
250
251 **/
252 EFI_STATUS
253 EFIAPI
254 AhciCheckMemSet (
255 IN UINTN Address,
256 IN UINT32 MaskValue,
257 IN UINT32 TestValue,
258 IN OUT UINTN *RetryTimes OPTIONAL
259 )
260 {
261 UINT32 Value;
262
263 if (RetryTimes != NULL) {
264 (*RetryTimes)--;
265 }
266
267 Value = *(volatile UINT32 *) Address;
268 Value &= MaskValue;
269
270 if (Value == TestValue) {
271 return EFI_SUCCESS;
272 }
273
274 if ((RetryTimes != NULL) && (*RetryTimes == 0)) {
275 return EFI_TIMEOUT;
276 } else {
277 return EFI_NOT_READY;
278 }
279 }
280
281 /**
282 Check if the device is still on port. It also checks if the AHCI controller
283 supports the address and data count will be transferred.
284
285 @param PciIo The PCI IO protocol instance.
286 @param Port The number of port.
287
288 @retval EFI_SUCCESS The device is attached to port and the transfer data is
289 supported by AHCI controller.
290 @retval EFI_UNSUPPORTED The transfer address and count is not supported by AHCI
291 controller.
292 @retval EFI_NOT_READY The physical communication between AHCI controller and device
293 is not ready.
294
295 **/
296 EFI_STATUS
297 EFIAPI
298 AhciCheckDeviceStatus (
299 IN EFI_PCI_IO_PROTOCOL *PciIo,
300 IN UINT8 Port
301 )
302 {
303 UINT32 Data;
304 UINT32 Offset;
305
306 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SSTS;
307
308 Data = AhciReadReg (PciIo, Offset) & EFI_AHCI_PORT_SSTS_DET_MASK;
309
310 if (Data == EFI_AHCI_PORT_SSTS_DET_PCE) {
311 return EFI_SUCCESS;
312 }
313
314 return EFI_NOT_READY;
315 }
316
317 /**
318
319 Clear the port interrupt and error status. It will also clear
320 HBA interrupt status.
321
322 @param PciIo The PCI IO protocol instance.
323 @param Port The number of port.
324
325 **/
326 VOID
327 EFIAPI
328 AhciClearPortStatus (
329 IN EFI_PCI_IO_PROTOCOL *PciIo,
330 IN UINT8 Port
331 )
332 {
333 UINT32 Offset;
334
335 //
336 // Clear any error status
337 //
338 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SERR;
339 AhciWriteReg (PciIo, Offset, AhciReadReg (PciIo, Offset));
340
341 //
342 // Clear any port interrupt status
343 //
344 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_IS;
345 AhciWriteReg (PciIo, Offset, AhciReadReg (PciIo, Offset));
346
347 //
348 // Clear any HBA interrupt status
349 //
350 AhciWriteReg (PciIo, EFI_AHCI_IS_OFFSET, AhciReadReg (PciIo, EFI_AHCI_IS_OFFSET));
351 }
352
353 /**
354 This function is used to dump the Status Registers and if there is ERR bit set
355 in the Status Register, the Error Register's value is also be dumped.
356
357 @param PciIo The PCI IO protocol instance.
358 @param Port The number of port.
359 @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
360
361 **/
362 VOID
363 EFIAPI
364 AhciDumpPortStatus (
365 IN EFI_PCI_IO_PROTOCOL *PciIo,
366 IN UINT8 Port,
367 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock
368 )
369 {
370 UINT32 Offset;
371 UINT32 Data;
372
373 ASSERT (PciIo != NULL);
374
375 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
376 Data = AhciReadReg (PciIo, Offset);
377
378 if (AtaStatusBlock != NULL) {
379 ZeroMem (AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK));
380
381 AtaStatusBlock->AtaStatus = (UINT8)Data;
382 if ((AtaStatusBlock->AtaStatus & BIT0) != 0) {
383 AtaStatusBlock->AtaError = (UINT8)(Data >> 8);
384 }
385 }
386 }
387
388
389 /**
390 Enable the FIS running for giving port.
391
392 @param PciIo The PCI IO protocol instance.
393 @param Port The number of port.
394 @param Timeout The timeout value of enabling FIS, uses 100ns as a unit.
395
396 @retval EFI_DEVICE_ERROR The FIS enable setting fails.
397 @retval EFI_TIMEOUT The FIS enable setting is time out.
398 @retval EFI_SUCCESS The FIS enable successfully.
399
400 **/
401 EFI_STATUS
402 EFIAPI
403 AhciEnableFisReceive (
404 IN EFI_PCI_IO_PROTOCOL *PciIo,
405 IN UINT8 Port,
406 IN UINT64 Timeout
407 )
408 {
409 UINT32 Offset;
410
411 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
412 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_FRE);
413
414 return AhciWaitMmioSet (
415 PciIo,
416 Offset,
417 EFI_AHCI_PORT_CMD_FR,
418 EFI_AHCI_PORT_CMD_FR,
419 Timeout
420 );
421 }
422
423 /**
424 Disable the FIS running for giving port.
425
426 @param PciIo The PCI IO protocol instance.
427 @param Port The number of port.
428 @param Timeout The timeout value of disabling FIS, uses 100ns as a unit.
429
430 @retval EFI_DEVICE_ERROR The FIS disable setting fails.
431 @retval EFI_TIMEOUT The FIS disable setting is time out.
432 @retval EFI_UNSUPPORTED The port is in running state.
433 @retval EFI_SUCCESS The FIS disable successfully.
434
435 **/
436 EFI_STATUS
437 EFIAPI
438 AhciDisableFisReceive (
439 IN EFI_PCI_IO_PROTOCOL *PciIo,
440 IN UINT8 Port,
441 IN UINT64 Timeout
442 )
443 {
444 UINT32 Offset;
445 UINT32 Data;
446
447 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
448 Data = AhciReadReg (PciIo, Offset);
449
450 //
451 // Before disabling Fis receive, the DMA engine of the port should NOT be in running status.
452 //
453 if ((Data & (EFI_AHCI_PORT_CMD_ST | EFI_AHCI_PORT_CMD_CR)) != 0) {
454 return EFI_UNSUPPORTED;
455 }
456
457 //
458 // Check if the Fis receive DMA engine for the port is running.
459 //
460 if ((Data & EFI_AHCI_PORT_CMD_FR) != EFI_AHCI_PORT_CMD_FR) {
461 return EFI_SUCCESS;
462 }
463
464 AhciAndReg (PciIo, Offset, (UINT32)~(EFI_AHCI_PORT_CMD_FRE));
465
466 return AhciWaitMmioSet (
467 PciIo,
468 Offset,
469 EFI_AHCI_PORT_CMD_FR,
470 0,
471 Timeout
472 );
473 }
474
475
476
477 /**
478 Build the command list, command table and prepare the fis receiver.
479
480 @param PciIo The PCI IO protocol instance.
481 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
482 @param Port The number of port.
483 @param PortMultiplier The timeout value of stop.
484 @param CommandFis The control fis will be used for the transfer.
485 @param CommandList The command list will be used for the transfer.
486 @param AtapiCommand The atapi command will be used for the transfer.
487 @param AtapiCommandLength The length of the atapi command.
488 @param CommandSlotNumber The command slot will be used for the transfer.
489 @param DataPhysicalAddr The pointer to the data buffer pci bus master address.
490 @param DataLength The data count to be transferred.
491
492 **/
493 VOID
494 EFIAPI
495 AhciBuildCommand (
496 IN EFI_PCI_IO_PROTOCOL *PciIo,
497 IN EFI_AHCI_REGISTERS *AhciRegisters,
498 IN UINT8 Port,
499 IN UINT8 PortMultiplier,
500 IN EFI_AHCI_COMMAND_FIS *CommandFis,
501 IN EFI_AHCI_COMMAND_LIST *CommandList,
502 IN EFI_AHCI_ATAPI_COMMAND *AtapiCommand OPTIONAL,
503 IN UINT8 AtapiCommandLength,
504 IN UINT8 CommandSlotNumber,
505 IN OUT VOID *DataPhysicalAddr,
506 IN UINT32 DataLength
507 )
508 {
509 UINT64 BaseAddr;
510 UINT32 PrdtNumber;
511 UINT32 PrdtIndex;
512 UINTN RemainedData;
513 UINTN MemAddr;
514 DATA_64 Data64;
515 UINT32 Offset;
516
517 //
518 // Filling the PRDT
519 //
520 PrdtNumber = (DataLength + EFI_AHCI_MAX_DATA_PER_PRDT - 1) / EFI_AHCI_MAX_DATA_PER_PRDT;
521
522 //
523 // According to AHCI 1.3 spec, a PRDT entry can point to a maximum 4MB data block.
524 // It also limits that the maximum amount of the PRDT entry in the command table
525 // is 65535.
526 //
527 ASSERT (PrdtNumber <= 65535);
528
529 Data64.Uint64 = (UINTN) (AhciRegisters->AhciRFis) + sizeof (EFI_AHCI_RECEIVED_FIS) * Port;
530
531 BaseAddr = Data64.Uint64;
532
533 ZeroMem ((VOID *)((UINTN) BaseAddr), sizeof (EFI_AHCI_RECEIVED_FIS));
534
535 ZeroMem (AhciRegisters->AhciCommandTable, sizeof (EFI_AHCI_COMMAND_TABLE));
536
537 CommandFis->AhciCFisPmNum = PortMultiplier;
538
539 CopyMem (&AhciRegisters->AhciCommandTable->CommandFis, CommandFis, sizeof (EFI_AHCI_COMMAND_FIS));
540
541 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
542 if (AtapiCommand != NULL) {
543 CopyMem (
544 &AhciRegisters->AhciCommandTable->AtapiCmd,
545 AtapiCommand,
546 AtapiCommandLength
547 );
548
549 CommandList->AhciCmdA = 1;
550 CommandList->AhciCmdP = 1;
551
552 AhciOrReg (PciIo, Offset, (EFI_AHCI_PORT_CMD_DLAE | EFI_AHCI_PORT_CMD_ATAPI));
553 } else {
554 AhciAndReg (PciIo, Offset, (UINT32)~(EFI_AHCI_PORT_CMD_DLAE | EFI_AHCI_PORT_CMD_ATAPI));
555 }
556
557 RemainedData = (UINTN) DataLength;
558 MemAddr = (UINTN) DataPhysicalAddr;
559 CommandList->AhciCmdPrdtl = PrdtNumber;
560
561 for (PrdtIndex = 0; PrdtIndex < PrdtNumber; PrdtIndex++) {
562 if (RemainedData < EFI_AHCI_MAX_DATA_PER_PRDT) {
563 AhciRegisters->AhciCommandTable->PrdtTable[PrdtIndex].AhciPrdtDbc = (UINT32)RemainedData - 1;
564 } else {
565 AhciRegisters->AhciCommandTable->PrdtTable[PrdtIndex].AhciPrdtDbc = EFI_AHCI_MAX_DATA_PER_PRDT - 1;
566 }
567
568 Data64.Uint64 = (UINT64)MemAddr;
569 AhciRegisters->AhciCommandTable->PrdtTable[PrdtIndex].AhciPrdtDba = Data64.Uint32.Lower32;
570 AhciRegisters->AhciCommandTable->PrdtTable[PrdtIndex].AhciPrdtDbau = Data64.Uint32.Upper32;
571 RemainedData -= EFI_AHCI_MAX_DATA_PER_PRDT;
572 MemAddr += EFI_AHCI_MAX_DATA_PER_PRDT;
573 }
574
575 //
576 // Set the last PRDT to Interrupt On Complete
577 //
578 if (PrdtNumber > 0) {
579 AhciRegisters->AhciCommandTable->PrdtTable[PrdtNumber - 1].AhciPrdtIoc = 1;
580 }
581
582 CopyMem (
583 (VOID *) ((UINTN) AhciRegisters->AhciCmdList + (UINTN) CommandSlotNumber * sizeof (EFI_AHCI_COMMAND_LIST)),
584 CommandList,
585 sizeof (EFI_AHCI_COMMAND_LIST)
586 );
587
588 Data64.Uint64 = (UINT64)(UINTN) AhciRegisters->AhciCommandTablePciAddr;
589 AhciRegisters->AhciCmdList[CommandSlotNumber].AhciCmdCtba = Data64.Uint32.Lower32;
590 AhciRegisters->AhciCmdList[CommandSlotNumber].AhciCmdCtbau = Data64.Uint32.Upper32;
591 AhciRegisters->AhciCmdList[CommandSlotNumber].AhciCmdPmp = PortMultiplier;
592
593 }
594
595 /**
596 Buid a command FIS.
597
598 @param CmdFis A pointer to the EFI_AHCI_COMMAND_FIS data structure.
599 @param AtaCommandBlock A pointer to the AhciBuildCommandFis data structure.
600
601 **/
602 VOID
603 EFIAPI
604 AhciBuildCommandFis (
605 IN OUT EFI_AHCI_COMMAND_FIS *CmdFis,
606 IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock
607 )
608 {
609 ZeroMem (CmdFis, sizeof (EFI_AHCI_COMMAND_FIS));
610
611 CmdFis->AhciCFisType = EFI_AHCI_FIS_REGISTER_H2D;
612 //
613 // Indicator it's a command
614 //
615 CmdFis->AhciCFisCmdInd = 0x1;
616 CmdFis->AhciCFisCmd = AtaCommandBlock->AtaCommand;
617
618 CmdFis->AhciCFisFeature = AtaCommandBlock->AtaFeatures;
619 CmdFis->AhciCFisFeatureExp = AtaCommandBlock->AtaFeaturesExp;
620
621 CmdFis->AhciCFisSecNum = AtaCommandBlock->AtaSectorNumber;
622 CmdFis->AhciCFisSecNumExp = AtaCommandBlock->AtaSectorNumberExp;
623
624 CmdFis->AhciCFisClyLow = AtaCommandBlock->AtaCylinderLow;
625 CmdFis->AhciCFisClyLowExp = AtaCommandBlock->AtaCylinderLowExp;
626
627 CmdFis->AhciCFisClyHigh = AtaCommandBlock->AtaCylinderHigh;
628 CmdFis->AhciCFisClyHighExp = AtaCommandBlock->AtaCylinderHighExp;
629
630 CmdFis->AhciCFisSecCount = AtaCommandBlock->AtaSectorCount;
631 CmdFis->AhciCFisSecCountExp = AtaCommandBlock->AtaSectorCountExp;
632
633 CmdFis->AhciCFisDevHead = (UINT8) (AtaCommandBlock->AtaDeviceHead | 0xE0);
634 }
635
636 /**
637 Start a PIO data transfer on specific port.
638
639 @param[in] PciIo The PCI IO protocol instance.
640 @param[in] AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
641 @param[in] Port The number of port.
642 @param[in] PortMultiplier The timeout value of stop.
643 @param[in] AtapiCommand The atapi command will be used for the
644 transfer.
645 @param[in] AtapiCommandLength The length of the atapi command.
646 @param[in] Read The transfer direction.
647 @param[in] AtaCommandBlock The EFI_ATA_COMMAND_BLOCK data.
648 @param[in, out] AtaStatusBlock The EFI_ATA_STATUS_BLOCK data.
649 @param[in, out] MemoryAddr The pointer to the data buffer.
650 @param[in] DataCount The data count to be transferred.
651 @param[in] Timeout The timeout value of non data transfer, uses 100ns as a unit.
652 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
653 used by non-blocking mode.
654
655 @retval EFI_DEVICE_ERROR The PIO data transfer abort with error occurs.
656 @retval EFI_TIMEOUT The operation is time out.
657 @retval EFI_UNSUPPORTED The device is not ready for transfer.
658 @retval EFI_SUCCESS The PIO data transfer executes successfully.
659
660 **/
661 EFI_STATUS
662 EFIAPI
663 AhciPioTransfer (
664 IN EFI_PCI_IO_PROTOCOL *PciIo,
665 IN EFI_AHCI_REGISTERS *AhciRegisters,
666 IN UINT8 Port,
667 IN UINT8 PortMultiplier,
668 IN EFI_AHCI_ATAPI_COMMAND *AtapiCommand OPTIONAL,
669 IN UINT8 AtapiCommandLength,
670 IN BOOLEAN Read,
671 IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock,
672 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock,
673 IN OUT VOID *MemoryAddr,
674 IN UINT32 DataCount,
675 IN UINT64 Timeout,
676 IN ATA_NONBLOCK_TASK *Task
677 )
678 {
679 EFI_STATUS Status;
680 UINTN FisBaseAddr;
681 UINTN Offset;
682 EFI_PHYSICAL_ADDRESS PhyAddr;
683 VOID *Map;
684 UINTN MapLength;
685 EFI_PCI_IO_PROTOCOL_OPERATION Flag;
686 UINT32 Delay;
687 EFI_AHCI_COMMAND_FIS CFis;
688 EFI_AHCI_COMMAND_LIST CmdList;
689 UINT32 PortTfd;
690 UINT32 PrdCount;
691
692 if (Read) {
693 Flag = EfiPciIoOperationBusMasterWrite;
694 } else {
695 Flag = EfiPciIoOperationBusMasterRead;
696 }
697
698 //
699 // construct command list and command table with pci bus address
700 //
701 MapLength = DataCount;
702 Status = PciIo->Map (
703 PciIo,
704 Flag,
705 MemoryAddr,
706 &MapLength,
707 &PhyAddr,
708 &Map
709 );
710
711 if (EFI_ERROR (Status) || (DataCount != MapLength)) {
712 return EFI_BAD_BUFFER_SIZE;
713 }
714
715 //
716 // Package read needed
717 //
718 AhciBuildCommandFis (&CFis, AtaCommandBlock);
719
720 ZeroMem (&CmdList, sizeof (EFI_AHCI_COMMAND_LIST));
721
722 CmdList.AhciCmdCfl = EFI_AHCI_FIS_REGISTER_H2D_LENGTH / 4;
723 CmdList.AhciCmdW = Read ? 0 : 1;
724
725 AhciBuildCommand (
726 PciIo,
727 AhciRegisters,
728 Port,
729 PortMultiplier,
730 &CFis,
731 &CmdList,
732 AtapiCommand,
733 AtapiCommandLength,
734 0,
735 (VOID *)(UINTN)PhyAddr,
736 DataCount
737 );
738
739 Status = AhciStartCommand (
740 PciIo,
741 Port,
742 0,
743 Timeout
744 );
745 if (EFI_ERROR (Status)) {
746 goto Exit;
747 }
748
749 //
750 // Check the status and wait the driver sending data
751 //
752 FisBaseAddr = (UINTN)AhciRegisters->AhciRFis + Port * sizeof (EFI_AHCI_RECEIVED_FIS);
753
754 if (Read && (AtapiCommand == 0)) {
755 //
756 // Wait device sends the PIO setup fis before data transfer
757 //
758 Status = EFI_TIMEOUT;
759 Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1);
760 do {
761 Offset = FisBaseAddr + EFI_AHCI_PIO_FIS_OFFSET;
762
763 Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_PIO_SETUP, 0);
764 if (!EFI_ERROR (Status)) {
765 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
766 PortTfd = AhciReadReg (PciIo, (UINT32) Offset);
767 //
768 // PxTFD will be updated if there is a D2H or SetupFIS received.
769 // For PIO IN transfer, D2H means a device error. Therefore we only need to check the TFD after receiving a SetupFIS.
770 //
771 if ((PortTfd & EFI_AHCI_PORT_TFD_ERR) != 0) {
772 Status = EFI_DEVICE_ERROR;
773 break;
774 }
775
776 PrdCount = *(volatile UINT32 *) (&(AhciRegisters->AhciCmdList[0].AhciCmdPrdbc));
777 if (PrdCount == DataCount) {
778 break;
779 }
780 }
781
782 Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET;
783 Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_REGISTER_D2H, 0);
784 if (!EFI_ERROR (Status)) {
785 Status = EFI_DEVICE_ERROR;
786 break;
787 }
788
789 //
790 // Stall for 100 microseconds.
791 //
792 MicroSecondDelay(100);
793
794 Delay--;
795 } while (Delay > 0);
796 } else {
797 //
798 // Wait for D2H Fis is received
799 //
800 Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET;
801 Status = AhciWaitMemSet (
802 Offset,
803 EFI_AHCI_FIS_TYPE_MASK,
804 EFI_AHCI_FIS_REGISTER_D2H,
805 Timeout
806 );
807
808 if (EFI_ERROR (Status)) {
809 goto Exit;
810 }
811
812 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
813 PortTfd = AhciReadReg (PciIo, (UINT32) Offset);
814 if ((PortTfd & EFI_AHCI_PORT_TFD_ERR) != 0) {
815 Status = EFI_DEVICE_ERROR;
816 }
817 }
818
819 Exit:
820 AhciStopCommand (
821 PciIo,
822 Port,
823 Timeout
824 );
825
826 AhciDisableFisReceive (
827 PciIo,
828 Port,
829 Timeout
830 );
831
832 PciIo->Unmap (
833 PciIo,
834 Map
835 );
836
837 AhciDumpPortStatus (PciIo, Port, AtaStatusBlock);
838
839 return Status;
840 }
841
842 /**
843 Start a DMA data transfer on specific port
844
845 @param[in] Instance The ATA_ATAPI_PASS_THRU_INSTANCE protocol instance.
846 @param[in] AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
847 @param[in] Port The number of port.
848 @param[in] PortMultiplier The timeout value of stop.
849 @param[in] AtapiCommand The atapi command will be used for the
850 transfer.
851 @param[in] AtapiCommandLength The length of the atapi command.
852 @param[in] Read The transfer direction.
853 @param[in] AtaCommandBlock The EFI_ATA_COMMAND_BLOCK data.
854 @param[in, out] AtaStatusBlock The EFI_ATA_STATUS_BLOCK data.
855 @param[in, out] MemoryAddr The pointer to the data buffer.
856 @param[in] DataCount The data count to be transferred.
857 @param[in] Timeout The timeout value of non data transfer, uses 100ns as a unit.
858 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
859 used by non-blocking mode.
860
861 @retval EFI_DEVICE_ERROR The DMA data transfer abort with error occurs.
862 @retval EFI_TIMEOUT The operation is time out.
863 @retval EFI_UNSUPPORTED The device is not ready for transfer.
864 @retval EFI_SUCCESS The DMA data transfer executes successfully.
865
866 **/
867 EFI_STATUS
868 EFIAPI
869 AhciDmaTransfer (
870 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,
871 IN EFI_AHCI_REGISTERS *AhciRegisters,
872 IN UINT8 Port,
873 IN UINT8 PortMultiplier,
874 IN EFI_AHCI_ATAPI_COMMAND *AtapiCommand OPTIONAL,
875 IN UINT8 AtapiCommandLength,
876 IN BOOLEAN Read,
877 IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock,
878 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock,
879 IN OUT VOID *MemoryAddr,
880 IN UINT32 DataCount,
881 IN UINT64 Timeout,
882 IN ATA_NONBLOCK_TASK *Task
883 )
884 {
885 EFI_STATUS Status;
886 UINTN Offset;
887 EFI_PHYSICAL_ADDRESS PhyAddr;
888 VOID *Map;
889 UINTN MapLength;
890 EFI_PCI_IO_PROTOCOL_OPERATION Flag;
891 EFI_AHCI_COMMAND_FIS CFis;
892 EFI_AHCI_COMMAND_LIST CmdList;
893 UINTN FisBaseAddr;
894 UINT32 PortTfd;
895
896 EFI_PCI_IO_PROTOCOL *PciIo;
897 EFI_TPL OldTpl;
898
899 Map = NULL;
900 PciIo = Instance->PciIo;
901
902 if (PciIo == NULL) {
903 return EFI_INVALID_PARAMETER;
904 }
905
906 //
907 // Before starting the Blocking BlockIO operation, push to finish all non-blocking
908 // BlockIO tasks.
909 // Delay 100us to simulate the blocking time out checking.
910 //
911 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
912 while ((Task == NULL) && (!IsListEmpty (&Instance->NonBlockingTaskList))) {
913 AsyncNonBlockingTransferRoutine (NULL, Instance);
914 //
915 // Stall for 100us.
916 //
917 MicroSecondDelay (100);
918 }
919 gBS->RestoreTPL (OldTpl);
920
921 if ((Task == NULL) || ((Task != NULL) && (!Task->IsStart))) {
922 //
923 // Mark the Task to indicate that it has been started.
924 //
925 if (Task != NULL) {
926 Task->IsStart = TRUE;
927 Task->RetryTimes = (UINT32) (DivU64x32(Timeout, 1000) + 1);
928 }
929 if (Read) {
930 Flag = EfiPciIoOperationBusMasterWrite;
931 } else {
932 Flag = EfiPciIoOperationBusMasterRead;
933 }
934
935 //
936 // Construct command list and command table with pci bus address.
937 //
938 MapLength = DataCount;
939 Status = PciIo->Map (
940 PciIo,
941 Flag,
942 MemoryAddr,
943 &MapLength,
944 &PhyAddr,
945 &Map
946 );
947
948 if (EFI_ERROR (Status) || (DataCount != MapLength)) {
949 return EFI_BAD_BUFFER_SIZE;
950 }
951
952 if (Task != NULL) {
953 Task->Map = Map;
954 }
955 //
956 // Package read needed
957 //
958 AhciBuildCommandFis (&CFis, AtaCommandBlock);
959
960 ZeroMem (&CmdList, sizeof (EFI_AHCI_COMMAND_LIST));
961
962 CmdList.AhciCmdCfl = EFI_AHCI_FIS_REGISTER_H2D_LENGTH / 4;
963 CmdList.AhciCmdW = Read ? 0 : 1;
964
965 AhciBuildCommand (
966 PciIo,
967 AhciRegisters,
968 Port,
969 PortMultiplier,
970 &CFis,
971 &CmdList,
972 AtapiCommand,
973 AtapiCommandLength,
974 0,
975 (VOID *)(UINTN)PhyAddr,
976 DataCount
977 );
978
979 Status = AhciStartCommand (
980 PciIo,
981 Port,
982 0,
983 Timeout
984 );
985 if (EFI_ERROR (Status)) {
986 goto Exit;
987 }
988 }
989
990 //
991 // Wait for command compelte
992 //
993 FisBaseAddr = (UINTN)AhciRegisters->AhciRFis + Port * sizeof (EFI_AHCI_RECEIVED_FIS);
994 Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET;
995 if (Task != NULL) {
996 //
997 // For Non-blocking
998 //
999 Status = AhciCheckMemSet (
1000 Offset,
1001 EFI_AHCI_FIS_TYPE_MASK,
1002 EFI_AHCI_FIS_REGISTER_D2H,
1003 (UINTN *) (&Task->RetryTimes)
1004 );
1005 } else {
1006 Status = AhciWaitMemSet (
1007 Offset,
1008 EFI_AHCI_FIS_TYPE_MASK,
1009 EFI_AHCI_FIS_REGISTER_D2H,
1010 Timeout
1011 );
1012 }
1013
1014 if (EFI_ERROR (Status)) {
1015 goto Exit;
1016 }
1017
1018 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
1019 PortTfd = AhciReadReg (PciIo, (UINT32) Offset);
1020 if ((PortTfd & EFI_AHCI_PORT_TFD_ERR) != 0) {
1021 Status = EFI_DEVICE_ERROR;
1022 }
1023
1024 Exit:
1025 //
1026 // For Blocking mode, the command should be stopped, the Fis should be disabled
1027 // and the PciIo should be unmapped.
1028 // For non-blocking mode, only when a error is happened (if the return status is
1029 // EFI_NOT_READY that means the command doesn't finished, try again.), first do the
1030 // context cleanup, then set the packet's Asb status.
1031 //
1032 if (Task == NULL ||
1033 ((Task != NULL) && (Status != EFI_NOT_READY))
1034 ) {
1035 AhciStopCommand (
1036 PciIo,
1037 Port,
1038 Timeout
1039 );
1040
1041 AhciDisableFisReceive (
1042 PciIo,
1043 Port,
1044 Timeout
1045 );
1046
1047 PciIo->Unmap (
1048 PciIo,
1049 (Task != NULL) ? Task->Map : Map
1050 );
1051
1052 if (Task != NULL) {
1053 Task->Packet->Asb->AtaStatus = 0x01;
1054 }
1055 }
1056
1057 AhciDumpPortStatus (PciIo, Port, AtaStatusBlock);
1058 return Status;
1059 }
1060
1061 /**
1062 Start a non data transfer on specific port.
1063
1064 @param[in] PciIo The PCI IO protocol instance.
1065 @param[in] AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1066 @param[in] Port The number of port.
1067 @param[in] PortMultiplier The timeout value of stop.
1068 @param[in] AtapiCommand The atapi command will be used for the
1069 transfer.
1070 @param[in] AtapiCommandLength The length of the atapi command.
1071 @param[in] AtaCommandBlock The EFI_ATA_COMMAND_BLOCK data.
1072 @param[in, out] AtaStatusBlock The EFI_ATA_STATUS_BLOCK data.
1073 @param[in] Timeout The timeout value of non data transfer, uses 100ns as a unit.
1074 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
1075 used by non-blocking mode.
1076
1077 @retval EFI_DEVICE_ERROR The non data transfer abort with error occurs.
1078 @retval EFI_TIMEOUT The operation is time out.
1079 @retval EFI_UNSUPPORTED The device is not ready for transfer.
1080 @retval EFI_SUCCESS The non data transfer executes successfully.
1081
1082 **/
1083 EFI_STATUS
1084 EFIAPI
1085 AhciNonDataTransfer (
1086 IN EFI_PCI_IO_PROTOCOL *PciIo,
1087 IN EFI_AHCI_REGISTERS *AhciRegisters,
1088 IN UINT8 Port,
1089 IN UINT8 PortMultiplier,
1090 IN EFI_AHCI_ATAPI_COMMAND *AtapiCommand OPTIONAL,
1091 IN UINT8 AtapiCommandLength,
1092 IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock,
1093 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock,
1094 IN UINT64 Timeout,
1095 IN ATA_NONBLOCK_TASK *Task
1096 )
1097 {
1098 EFI_STATUS Status;
1099 UINTN FisBaseAddr;
1100 UINTN Offset;
1101 UINT32 PortTfd;
1102 EFI_AHCI_COMMAND_FIS CFis;
1103 EFI_AHCI_COMMAND_LIST CmdList;
1104
1105 //
1106 // Package read needed
1107 //
1108 AhciBuildCommandFis (&CFis, AtaCommandBlock);
1109
1110 ZeroMem (&CmdList, sizeof (EFI_AHCI_COMMAND_LIST));
1111
1112 CmdList.AhciCmdCfl = EFI_AHCI_FIS_REGISTER_H2D_LENGTH / 4;
1113
1114 AhciBuildCommand (
1115 PciIo,
1116 AhciRegisters,
1117 Port,
1118 PortMultiplier,
1119 &CFis,
1120 &CmdList,
1121 AtapiCommand,
1122 AtapiCommandLength,
1123 0,
1124 NULL,
1125 0
1126 );
1127
1128 Status = AhciStartCommand (
1129 PciIo,
1130 Port,
1131 0,
1132 Timeout
1133 );
1134 if (EFI_ERROR (Status)) {
1135 goto Exit;
1136 }
1137
1138 //
1139 // Wait device sends the Response Fis
1140 //
1141 FisBaseAddr = (UINTN)AhciRegisters->AhciRFis + Port * sizeof (EFI_AHCI_RECEIVED_FIS);
1142 Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET;
1143 Status = AhciWaitMemSet (
1144 Offset,
1145 EFI_AHCI_FIS_TYPE_MASK,
1146 EFI_AHCI_FIS_REGISTER_D2H,
1147 Timeout
1148 );
1149
1150 if (EFI_ERROR (Status)) {
1151 goto Exit;
1152 }
1153
1154 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
1155 PortTfd = AhciReadReg (PciIo, (UINT32) Offset);
1156 if ((PortTfd & EFI_AHCI_PORT_TFD_ERR) != 0) {
1157 Status = EFI_DEVICE_ERROR;
1158 }
1159
1160 Exit:
1161 AhciStopCommand (
1162 PciIo,
1163 Port,
1164 Timeout
1165 );
1166
1167 AhciDisableFisReceive (
1168 PciIo,
1169 Port,
1170 Timeout
1171 );
1172
1173 AhciDumpPortStatus (PciIo, Port, AtaStatusBlock);
1174
1175 return Status;
1176 }
1177
1178 /**
1179 Stop command running for giving port
1180
1181 @param PciIo The PCI IO protocol instance.
1182 @param Port The number of port.
1183 @param Timeout The timeout value of stop, uses 100ns as a unit.
1184
1185 @retval EFI_DEVICE_ERROR The command stop unsuccessfully.
1186 @retval EFI_TIMEOUT The operation is time out.
1187 @retval EFI_SUCCESS The command stop successfully.
1188
1189 **/
1190 EFI_STATUS
1191 EFIAPI
1192 AhciStopCommand (
1193 IN EFI_PCI_IO_PROTOCOL *PciIo,
1194 IN UINT8 Port,
1195 IN UINT64 Timeout
1196 )
1197 {
1198 UINT32 Offset;
1199 UINT32 Data;
1200
1201 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
1202 Data = AhciReadReg (PciIo, Offset);
1203
1204 if ((Data & (EFI_AHCI_PORT_CMD_ST | EFI_AHCI_PORT_CMD_CR)) == 0) {
1205 return EFI_SUCCESS;
1206 }
1207
1208 if ((Data & EFI_AHCI_PORT_CMD_ST) != 0) {
1209 AhciAndReg (PciIo, Offset, (UINT32)~(EFI_AHCI_PORT_CMD_ST));
1210 }
1211
1212 return AhciWaitMmioSet (
1213 PciIo,
1214 Offset,
1215 EFI_AHCI_PORT_CMD_CR,
1216 0,
1217 Timeout
1218 );
1219 }
1220
1221 /**
1222 Start command for give slot on specific port.
1223
1224 @param PciIo The PCI IO protocol instance.
1225 @param Port The number of port.
1226 @param CommandSlot The number of Command Slot.
1227 @param Timeout The timeout value of start, uses 100ns as a unit.
1228
1229 @retval EFI_DEVICE_ERROR The command start unsuccessfully.
1230 @retval EFI_TIMEOUT The operation is time out.
1231 @retval EFI_SUCCESS The command start successfully.
1232
1233 **/
1234 EFI_STATUS
1235 EFIAPI
1236 AhciStartCommand (
1237 IN EFI_PCI_IO_PROTOCOL *PciIo,
1238 IN UINT8 Port,
1239 IN UINT8 CommandSlot,
1240 IN UINT64 Timeout
1241 )
1242 {
1243 UINT32 CmdSlotBit;
1244 EFI_STATUS Status;
1245 UINT32 PortStatus;
1246 UINT32 StartCmd;
1247 UINT32 PortTfd;
1248 UINT32 Offset;
1249 UINT32 Capability;
1250
1251 //
1252 // Collect AHCI controller information
1253 //
1254 Capability = AhciReadReg(PciIo, EFI_AHCI_CAPABILITY_OFFSET);
1255
1256 CmdSlotBit = (UINT32) (1 << CommandSlot);
1257
1258 AhciClearPortStatus (
1259 PciIo,
1260 Port
1261 );
1262
1263 Status = AhciEnableFisReceive (
1264 PciIo,
1265 Port,
1266 Timeout
1267 );
1268
1269 if (EFI_ERROR (Status)) {
1270 return Status;
1271 }
1272
1273 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
1274 PortStatus = AhciReadReg (PciIo, Offset);
1275
1276 StartCmd = 0;
1277 if ((PortStatus & EFI_AHCI_PORT_CMD_ALPE) != 0) {
1278 StartCmd = AhciReadReg (PciIo, Offset);
1279 StartCmd &= ~EFI_AHCI_PORT_CMD_ICC_MASK;
1280 StartCmd |= EFI_AHCI_PORT_CMD_ACTIVE;
1281 }
1282
1283 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
1284 PortTfd = AhciReadReg (PciIo, Offset);
1285
1286 if ((PortTfd & (EFI_AHCI_PORT_TFD_BSY | EFI_AHCI_PORT_TFD_DRQ)) != 0) {
1287 if ((Capability & BIT24) != 0) {
1288 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
1289 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_CLO);
1290
1291 AhciWaitMmioSet (
1292 PciIo,
1293 Offset,
1294 EFI_AHCI_PORT_CMD_CLO,
1295 0,
1296 Timeout
1297 );
1298 }
1299 }
1300
1301 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
1302 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_ST | StartCmd);
1303
1304 //
1305 // Setting the command
1306 //
1307 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SACT;
1308 AhciAndReg (PciIo, Offset, 0);
1309 AhciOrReg (PciIo, Offset, CmdSlotBit);
1310
1311 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CI;
1312 AhciAndReg (PciIo, Offset, 0);
1313 AhciOrReg (PciIo, Offset, CmdSlotBit);
1314
1315 return EFI_SUCCESS;
1316 }
1317
1318 /**
1319 Do AHCI port reset.
1320
1321 @param PciIo The PCI IO protocol instance.
1322 @param Port The number of port.
1323 @param Timeout The timeout value of reset, uses 100ns as a unit.
1324
1325 @retval EFI_DEVICE_ERROR The port reset unsuccessfully
1326 @retval EFI_TIMEOUT The reset operation is time out.
1327 @retval EFI_SUCCESS The port reset successfully.
1328
1329 **/
1330 EFI_STATUS
1331 EFIAPI
1332 AhciPortReset (
1333 IN EFI_PCI_IO_PROTOCOL *PciIo,
1334 IN UINT8 Port,
1335 IN UINT64 Timeout
1336 )
1337 {
1338 EFI_STATUS Status;
1339 UINT32 Offset;
1340
1341 AhciClearPortStatus (PciIo, Port);
1342
1343 AhciStopCommand (PciIo, Port, Timeout);
1344
1345 AhciDisableFisReceive (PciIo, Port, Timeout);
1346
1347 AhciEnableFisReceive (PciIo, Port, Timeout);
1348
1349 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SCTL;
1350
1351 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_SCTL_DET_INIT);
1352
1353 //
1354 // wait 5 millisecond before de-assert DET
1355 //
1356 MicroSecondDelay (5000);
1357
1358 AhciAndReg (PciIo, Offset, (UINT32)EFI_AHCI_PORT_SCTL_MASK);
1359
1360 //
1361 // wait 5 millisecond before de-assert DET
1362 //
1363 MicroSecondDelay (5000);
1364
1365 //
1366 // Wait for communication to be re-established
1367 //
1368 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SSTS;
1369 Status = AhciWaitMmioSet (
1370 PciIo,
1371 Offset,
1372 EFI_AHCI_PORT_SSTS_DET_MASK,
1373 EFI_AHCI_PORT_SSTS_DET_PCE,
1374 Timeout
1375 );
1376
1377 if (EFI_ERROR (Status)) {
1378 return Status;
1379 }
1380
1381 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SERR;
1382 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_ERR_CLEAR);
1383
1384 return EFI_SUCCESS;
1385 }
1386
1387 /**
1388 Do AHCI HBA reset.
1389
1390 @param PciIo The PCI IO protocol instance.
1391 @param Timeout The timeout value of reset, uses 100ns as a unit.
1392
1393 @retval EFI_DEVICE_ERROR AHCI controller is failed to complete hardware reset.
1394 @retval EFI_TIMEOUT The reset operation is time out.
1395 @retval EFI_SUCCESS AHCI controller is reset successfully.
1396
1397 **/
1398 EFI_STATUS
1399 EFIAPI
1400 AhciReset (
1401 IN EFI_PCI_IO_PROTOCOL *PciIo,
1402 IN UINT64 Timeout
1403 )
1404 {
1405 UINT32 Delay;
1406 UINT32 Value;
1407
1408 AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE);
1409
1410 AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_RESET);
1411
1412 Delay = (UINT32) (DivU64x32(Timeout, 1000) + 1);
1413
1414 do {
1415 Value = AhciReadReg(PciIo, EFI_AHCI_GHC_OFFSET);
1416
1417 if ((Value & EFI_AHCI_GHC_RESET) == 0) {
1418 break;
1419 }
1420
1421 //
1422 // Stall for 100 microseconds.
1423 //
1424 MicroSecondDelay(100);
1425
1426 Delay--;
1427 } while (Delay > 0);
1428
1429 if (Delay == 0) {
1430 return EFI_TIMEOUT;
1431 }
1432
1433 return EFI_SUCCESS;
1434 }
1435
1436 /**
1437 Send SMART Return Status command to check if the execution of SMART cmd is successful or not.
1438
1439 @param PciIo The PCI IO protocol instance.
1440 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1441 @param Port The number of port.
1442 @param PortMultiplier The timeout value of stop.
1443 @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
1444
1445 @retval EFI_SUCCESS Successfully get the return status of S.M.A.R.T command execution.
1446 @retval Others Fail to get return status data.
1447
1448 **/
1449 EFI_STATUS
1450 EFIAPI
1451 AhciAtaSmartReturnStatusCheck (
1452 IN EFI_PCI_IO_PROTOCOL *PciIo,
1453 IN EFI_AHCI_REGISTERS *AhciRegisters,
1454 IN UINT8 Port,
1455 IN UINT8 PortMultiplier,
1456 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock
1457 )
1458 {
1459 EFI_STATUS Status;
1460 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1461 UINT8 LBAMid;
1462 UINT8 LBAHigh;
1463 UINTN FisBaseAddr;
1464 UINT32 Value;
1465
1466 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1467
1468 AtaCommandBlock.AtaCommand = ATA_CMD_SMART;
1469 AtaCommandBlock.AtaFeatures = ATA_SMART_RETURN_STATUS;
1470 AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F;
1471 AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2;
1472
1473 //
1474 // Send S.M.A.R.T Read Return Status command to device
1475 //
1476 Status = AhciNonDataTransfer (
1477 PciIo,
1478 AhciRegisters,
1479 (UINT8)Port,
1480 (UINT8)PortMultiplier,
1481 NULL,
1482 0,
1483 &AtaCommandBlock,
1484 AtaStatusBlock,
1485 ATA_ATAPI_TIMEOUT,
1486 NULL
1487 );
1488
1489 if (EFI_ERROR (Status)) {
1490 REPORT_STATUS_CODE (
1491 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1492 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED)
1493 );
1494 return EFI_DEVICE_ERROR;
1495 }
1496
1497 REPORT_STATUS_CODE (
1498 EFI_PROGRESS_CODE,
1499 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE)
1500 );
1501
1502 FisBaseAddr = (UINTN)AhciRegisters->AhciRFis + Port * sizeof (EFI_AHCI_RECEIVED_FIS);
1503
1504 Value = *(UINT32 *) (FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET);
1505
1506 if ((Value & EFI_AHCI_FIS_TYPE_MASK) == EFI_AHCI_FIS_REGISTER_D2H) {
1507 LBAMid = ((UINT8 *)(UINTN)(FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET))[5];
1508 LBAHigh = ((UINT8 *)(UINTN)(FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET))[6];
1509
1510 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) {
1511 //
1512 // The threshold exceeded condition is not detected by the device
1513 //
1514 DEBUG ((EFI_D_INFO, "The S.M.A.R.T threshold exceeded condition is not detected\n"));
1515 REPORT_STATUS_CODE (
1516 EFI_PROGRESS_CODE,
1517 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD)
1518 );
1519 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) {
1520 //
1521 // The threshold exceeded condition is detected by the device
1522 //
1523 DEBUG ((EFI_D_INFO, "The S.M.A.R.T threshold exceeded condition is detected\n"));
1524 REPORT_STATUS_CODE (
1525 EFI_PROGRESS_CODE,
1526 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD)
1527 );
1528 }
1529 }
1530
1531 return EFI_SUCCESS;
1532 }
1533
1534 /**
1535 Enable SMART command of the disk if supported.
1536
1537 @param PciIo The PCI IO protocol instance.
1538 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1539 @param Port The number of port.
1540 @param PortMultiplier The timeout value of stop.
1541 @param IdentifyData A pointer to data buffer which is used to contain IDENTIFY data.
1542 @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
1543
1544 **/
1545 VOID
1546 EFIAPI
1547 AhciAtaSmartSupport (
1548 IN EFI_PCI_IO_PROTOCOL *PciIo,
1549 IN EFI_AHCI_REGISTERS *AhciRegisters,
1550 IN UINT8 Port,
1551 IN UINT8 PortMultiplier,
1552 IN EFI_IDENTIFY_DATA *IdentifyData,
1553 IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock
1554 )
1555 {
1556 EFI_STATUS Status;
1557 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1558
1559 //
1560 // Detect if the device supports S.M.A.R.T.
1561 //
1562 if ((IdentifyData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) {
1563 //
1564 // S.M.A.R.T is not supported by the device
1565 //
1566 DEBUG ((EFI_D_INFO, "S.M.A.R.T feature is not supported at port [%d] PortMultiplier [%d]!\n",
1567 Port, PortMultiplier));
1568 REPORT_STATUS_CODE (
1569 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1570 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED)
1571 );
1572 } else {
1573 //
1574 // Check if the feature is enabled. If not, then enable S.M.A.R.T.
1575 //
1576 if ((IdentifyData->AtaData.command_set_feature_enb_85 & 0x0001) != 0x0001) {
1577
1578 REPORT_STATUS_CODE (
1579 EFI_PROGRESS_CODE,
1580 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLE)
1581 );
1582
1583 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1584
1585 AtaCommandBlock.AtaCommand = ATA_CMD_SMART;
1586 AtaCommandBlock.AtaFeatures = ATA_SMART_ENABLE_OPERATION;
1587 AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F;
1588 AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2;
1589
1590 //
1591 // Send S.M.A.R.T Enable command to device
1592 //
1593 Status = AhciNonDataTransfer (
1594 PciIo,
1595 AhciRegisters,
1596 (UINT8)Port,
1597 (UINT8)PortMultiplier,
1598 NULL,
1599 0,
1600 &AtaCommandBlock,
1601 AtaStatusBlock,
1602 ATA_ATAPI_TIMEOUT,
1603 NULL
1604 );
1605
1606
1607 if (!EFI_ERROR (Status)) {
1608 //
1609 // Send S.M.A.R.T AutoSave command to device
1610 //
1611 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1612
1613 AtaCommandBlock.AtaCommand = ATA_CMD_SMART;
1614 AtaCommandBlock.AtaFeatures = 0xD2;
1615 AtaCommandBlock.AtaSectorCount = 0xF1;
1616 AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F;
1617 AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2;
1618
1619 Status = AhciNonDataTransfer (
1620 PciIo,
1621 AhciRegisters,
1622 (UINT8)Port,
1623 (UINT8)PortMultiplier,
1624 NULL,
1625 0,
1626 &AtaCommandBlock,
1627 AtaStatusBlock,
1628 ATA_ATAPI_TIMEOUT,
1629 NULL
1630 );
1631
1632 if (!EFI_ERROR (Status)) {
1633 Status = AhciAtaSmartReturnStatusCheck (
1634 PciIo,
1635 AhciRegisters,
1636 (UINT8)Port,
1637 (UINT8)PortMultiplier,
1638 AtaStatusBlock
1639 );
1640 }
1641 }
1642 }
1643 DEBUG ((EFI_D_INFO, "Enabled S.M.A.R.T feature at port [%d] PortMultiplier [%d]!\n",
1644 Port, PortMultiplier));
1645 }
1646
1647 return ;
1648 }
1649
1650 /**
1651 Send Buffer cmd to specific device.
1652
1653 @param PciIo The PCI IO protocol instance.
1654 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1655 @param Port The number of port.
1656 @param PortMultiplier The timeout value of stop.
1657 @param Buffer The data buffer to store IDENTIFY PACKET data.
1658
1659 @retval EFI_DEVICE_ERROR The cmd abort with error occurs.
1660 @retval EFI_TIMEOUT The operation is time out.
1661 @retval EFI_UNSUPPORTED The device is not ready for executing.
1662 @retval EFI_SUCCESS The cmd executes successfully.
1663
1664 **/
1665 EFI_STATUS
1666 EFIAPI
1667 AhciIdentify (
1668 IN EFI_PCI_IO_PROTOCOL *PciIo,
1669 IN EFI_AHCI_REGISTERS *AhciRegisters,
1670 IN UINT8 Port,
1671 IN UINT8 PortMultiplier,
1672 IN OUT EFI_IDENTIFY_DATA *Buffer
1673 )
1674 {
1675 EFI_STATUS Status;
1676 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1677 EFI_ATA_STATUS_BLOCK AtaStatusBlock;
1678
1679 if (PciIo == NULL || AhciRegisters == NULL || Buffer == NULL) {
1680 return EFI_INVALID_PARAMETER;
1681 }
1682
1683 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1684 ZeroMem (&AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK));
1685
1686 AtaCommandBlock.AtaCommand = ATA_CMD_IDENTIFY_DRIVE;
1687 AtaCommandBlock.AtaSectorCount = 1;
1688
1689 Status = AhciPioTransfer (
1690 PciIo,
1691 AhciRegisters,
1692 Port,
1693 PortMultiplier,
1694 NULL,
1695 0,
1696 TRUE,
1697 &AtaCommandBlock,
1698 &AtaStatusBlock,
1699 Buffer,
1700 sizeof (EFI_IDENTIFY_DATA),
1701 ATA_ATAPI_TIMEOUT,
1702 NULL
1703 );
1704
1705 return Status;
1706 }
1707
1708 /**
1709 Send Buffer cmd to specific device.
1710
1711 @param PciIo The PCI IO protocol instance.
1712 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1713 @param Port The number of port.
1714 @param PortMultiplier The timeout value of stop.
1715 @param Buffer The data buffer to store IDENTIFY PACKET data.
1716
1717 @retval EFI_DEVICE_ERROR The cmd abort with error occurs.
1718 @retval EFI_TIMEOUT The operation is time out.
1719 @retval EFI_UNSUPPORTED The device is not ready for executing.
1720 @retval EFI_SUCCESS The cmd executes successfully.
1721
1722 **/
1723 EFI_STATUS
1724 EFIAPI
1725 AhciIdentifyPacket (
1726 IN EFI_PCI_IO_PROTOCOL *PciIo,
1727 IN EFI_AHCI_REGISTERS *AhciRegisters,
1728 IN UINT8 Port,
1729 IN UINT8 PortMultiplier,
1730 IN OUT EFI_IDENTIFY_DATA *Buffer
1731 )
1732 {
1733 EFI_STATUS Status;
1734 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1735 EFI_ATA_STATUS_BLOCK AtaStatusBlock;
1736
1737 if (PciIo == NULL || AhciRegisters == NULL) {
1738 return EFI_INVALID_PARAMETER;
1739 }
1740
1741 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1742 ZeroMem (&AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK));
1743
1744 AtaCommandBlock.AtaCommand = ATA_CMD_IDENTIFY_DEVICE;
1745 AtaCommandBlock.AtaSectorCount = 1;
1746
1747 Status = AhciPioTransfer (
1748 PciIo,
1749 AhciRegisters,
1750 Port,
1751 PortMultiplier,
1752 NULL,
1753 0,
1754 TRUE,
1755 &AtaCommandBlock,
1756 &AtaStatusBlock,
1757 Buffer,
1758 sizeof (EFI_IDENTIFY_DATA),
1759 ATA_ATAPI_TIMEOUT,
1760 NULL
1761 );
1762
1763 return Status;
1764 }
1765
1766 /**
1767 Send SET FEATURE cmd on specific device.
1768
1769 @param PciIo The PCI IO protocol instance.
1770 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1771 @param Port The number of port.
1772 @param PortMultiplier The timeout value of stop.
1773 @param Feature The data to send Feature register.
1774 @param FeatureSpecificData The specific data for SET FEATURE cmd.
1775
1776 @retval EFI_DEVICE_ERROR The cmd abort with error occurs.
1777 @retval EFI_TIMEOUT The operation is time out.
1778 @retval EFI_UNSUPPORTED The device is not ready for executing.
1779 @retval EFI_SUCCESS The cmd executes successfully.
1780
1781 **/
1782 EFI_STATUS
1783 EFIAPI
1784 AhciDeviceSetFeature (
1785 IN EFI_PCI_IO_PROTOCOL *PciIo,
1786 IN EFI_AHCI_REGISTERS *AhciRegisters,
1787 IN UINT8 Port,
1788 IN UINT8 PortMultiplier,
1789 IN UINT16 Feature,
1790 IN UINT32 FeatureSpecificData
1791 )
1792 {
1793 EFI_STATUS Status;
1794 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1795 EFI_ATA_STATUS_BLOCK AtaStatusBlock;
1796
1797 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1798 ZeroMem (&AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK));
1799
1800 AtaCommandBlock.AtaCommand = ATA_CMD_SET_FEATURES;
1801 AtaCommandBlock.AtaFeatures = (UINT8) Feature;
1802 AtaCommandBlock.AtaFeaturesExp = (UINT8) (Feature >> 8);
1803 AtaCommandBlock.AtaSectorCount = (UINT8) FeatureSpecificData;
1804 AtaCommandBlock.AtaSectorNumber = (UINT8) (FeatureSpecificData >> 8);
1805 AtaCommandBlock.AtaCylinderLow = (UINT8) (FeatureSpecificData >> 16);
1806 AtaCommandBlock.AtaCylinderHigh = (UINT8) (FeatureSpecificData >> 24);
1807
1808 Status = AhciNonDataTransfer (
1809 PciIo,
1810 AhciRegisters,
1811 (UINT8)Port,
1812 (UINT8)PortMultiplier,
1813 NULL,
1814 0,
1815 &AtaCommandBlock,
1816 &AtaStatusBlock,
1817 ATA_ATAPI_TIMEOUT,
1818 NULL
1819 );
1820
1821 return Status;
1822 }
1823
1824 /**
1825 This function is used to send out ATAPI commands conforms to the Packet Command
1826 with PIO Protocol.
1827
1828 @param PciIo The PCI IO protocol instance.
1829 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1830 @param Port The number of port.
1831 @param PortMultiplier The number of port multiplier.
1832 @param Packet A pointer to EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET structure.
1833
1834 @retval EFI_SUCCESS send out the ATAPI packet command successfully
1835 and device sends data successfully.
1836 @retval EFI_DEVICE_ERROR the device failed to send data.
1837
1838 **/
1839 EFI_STATUS
1840 EFIAPI
1841 AhciPacketCommandExecute (
1842 IN EFI_PCI_IO_PROTOCOL *PciIo,
1843 IN EFI_AHCI_REGISTERS *AhciRegisters,
1844 IN UINT8 Port,
1845 IN UINT8 PortMultiplier,
1846 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1847 )
1848 {
1849 EFI_STATUS Status;
1850 VOID *Buffer;
1851 UINT32 Length;
1852 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;
1853 EFI_ATA_STATUS_BLOCK AtaStatusBlock;
1854 BOOLEAN Read;
1855
1856 if (Packet == NULL || Packet->Cdb == NULL) {
1857 return EFI_INVALID_PARAMETER;
1858 }
1859
1860 ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK));
1861 ZeroMem (&AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK));
1862 AtaCommandBlock.AtaCommand = ATA_CMD_PACKET;
1863 //
1864 // No OVL; No DMA
1865 //
1866 AtaCommandBlock.AtaFeatures = 0x00;
1867 //
1868 // set the transfersize to ATAPI_MAX_BYTE_COUNT to let the device
1869 // determine how many data should be transferred.
1870 //
1871 AtaCommandBlock.AtaCylinderLow = (UINT8) (ATAPI_MAX_BYTE_COUNT & 0x00ff);
1872 AtaCommandBlock.AtaCylinderHigh = (UINT8) (ATAPI_MAX_BYTE_COUNT >> 8);
1873
1874 if (Packet->DataDirection == EFI_EXT_SCSI_DATA_DIRECTION_READ) {
1875 Buffer = Packet->InDataBuffer;
1876 Length = Packet->InTransferLength;
1877 Read = TRUE;
1878 } else {
1879 Buffer = Packet->OutDataBuffer;
1880 Length = Packet->OutTransferLength;
1881 Read = FALSE;
1882 }
1883
1884 if (Length == 0) {
1885 Status = AhciNonDataTransfer (
1886 PciIo,
1887 AhciRegisters,
1888 Port,
1889 PortMultiplier,
1890 Packet->Cdb,
1891 Packet->CdbLength,
1892 &AtaCommandBlock,
1893 &AtaStatusBlock,
1894 Packet->Timeout,
1895 NULL
1896 );
1897 } else {
1898 Status = AhciPioTransfer (
1899 PciIo,
1900 AhciRegisters,
1901 Port,
1902 PortMultiplier,
1903 Packet->Cdb,
1904 Packet->CdbLength,
1905 Read,
1906 &AtaCommandBlock,
1907 &AtaStatusBlock,
1908 Buffer,
1909 Length,
1910 Packet->Timeout,
1911 NULL
1912 );
1913 }
1914 return Status;
1915 }
1916
1917 /**
1918 Allocate transfer-related data struct which is used at AHCI mode.
1919
1920 @param PciIo The PCI IO protocol instance.
1921 @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS.
1922
1923 **/
1924 EFI_STATUS
1925 EFIAPI
1926 AhciCreateTransferDescriptor (
1927 IN EFI_PCI_IO_PROTOCOL *PciIo,
1928 IN OUT EFI_AHCI_REGISTERS *AhciRegisters
1929 )
1930 {
1931 EFI_STATUS Status;
1932 UINTN Bytes;
1933 VOID *Buffer;
1934
1935 UINT32 Capability;
1936 UINT32 PortImplementBitMap;
1937 UINT8 MaxPortNumber;
1938 UINT8 MaxCommandSlotNumber;
1939 BOOLEAN Support64Bit;
1940 UINT64 MaxReceiveFisSize;
1941 UINT64 MaxCommandListSize;
1942 UINT64 MaxCommandTableSize;
1943 EFI_PHYSICAL_ADDRESS AhciRFisPciAddr;
1944 EFI_PHYSICAL_ADDRESS AhciCmdListPciAddr;
1945 EFI_PHYSICAL_ADDRESS AhciCommandTablePciAddr;
1946
1947 Buffer = NULL;
1948 //
1949 // Collect AHCI controller information
1950 //
1951 Capability = AhciReadReg(PciIo, EFI_AHCI_CAPABILITY_OFFSET);
1952 //
1953 // Get the number of command slots per port supported by this HBA.
1954 //
1955 MaxCommandSlotNumber = (UINT8) (((Capability & 0x1F00) >> 8) + 1);
1956 Support64Bit = (BOOLEAN) (((Capability & BIT31) != 0) ? TRUE : FALSE);
1957
1958 PortImplementBitMap = AhciReadReg(PciIo, EFI_AHCI_PI_OFFSET);
1959 //
1960 // Get the highest bit of implemented ports which decides how many bytes are allocated for recived FIS.
1961 //
1962 MaxPortNumber = (UINT8)(UINTN)(HighBitSet32(PortImplementBitMap) + 1);
1963 if (MaxPortNumber == 0) {
1964 return EFI_DEVICE_ERROR;
1965 }
1966
1967 MaxReceiveFisSize = MaxPortNumber * sizeof (EFI_AHCI_RECEIVED_FIS);
1968 Status = PciIo->AllocateBuffer (
1969 PciIo,
1970 AllocateAnyPages,
1971 EfiBootServicesData,
1972 EFI_SIZE_TO_PAGES ((UINTN) MaxReceiveFisSize),
1973 &Buffer,
1974 0
1975 );
1976
1977 if (EFI_ERROR (Status)) {
1978 return EFI_OUT_OF_RESOURCES;
1979 }
1980
1981 ZeroMem (Buffer, (UINTN)MaxReceiveFisSize);
1982
1983 AhciRegisters->AhciRFis = Buffer;
1984 AhciRegisters->MaxReceiveFisSize = MaxReceiveFisSize;
1985 Bytes = (UINTN)MaxReceiveFisSize;
1986
1987 Status = PciIo->Map (
1988 PciIo,
1989 EfiPciIoOperationBusMasterCommonBuffer,
1990 Buffer,
1991 &Bytes,
1992 &AhciRFisPciAddr,
1993 &AhciRegisters->MapRFis
1994 );
1995
1996 if (EFI_ERROR (Status) || (Bytes != MaxReceiveFisSize)) {
1997 //
1998 // Map error or unable to map the whole RFis buffer into a contiguous region.
1999 //
2000 Status = EFI_OUT_OF_RESOURCES;
2001 goto Error6;
2002 }
2003
2004 if ((!Support64Bit) && (AhciRFisPciAddr > 0x100000000ULL)) {
2005 //
2006 // The AHCI HBA doesn't support 64bit addressing, so should not get a >4G pci bus master address.
2007 //
2008 Status = EFI_DEVICE_ERROR;
2009 goto Error5;
2010 }
2011 AhciRegisters->AhciRFisPciAddr = (EFI_AHCI_RECEIVED_FIS *)(UINTN)AhciRFisPciAddr;
2012
2013 //
2014 // Allocate memory for command list
2015 // Note that the implemenation is a single task model which only use a command list for all ports.
2016 //
2017 Buffer = NULL;
2018 MaxCommandListSize = MaxCommandSlotNumber * sizeof (EFI_AHCI_COMMAND_LIST);
2019 Status = PciIo->AllocateBuffer (
2020 PciIo,
2021 AllocateAnyPages,
2022 EfiBootServicesData,
2023 EFI_SIZE_TO_PAGES ((UINTN) MaxCommandListSize),
2024 &Buffer,
2025 0
2026 );
2027
2028 if (EFI_ERROR (Status)) {
2029 //
2030 // Free mapped resource.
2031 //
2032 Status = EFI_OUT_OF_RESOURCES;
2033 goto Error5;
2034 }
2035
2036 ZeroMem (Buffer, (UINTN)MaxCommandListSize);
2037
2038 AhciRegisters->AhciCmdList = Buffer;
2039 AhciRegisters->MaxCommandListSize = MaxCommandListSize;
2040 Bytes = (UINTN)MaxCommandListSize;
2041
2042 Status = PciIo->Map (
2043 PciIo,
2044 EfiPciIoOperationBusMasterCommonBuffer,
2045 Buffer,
2046 &Bytes,
2047 &AhciCmdListPciAddr,
2048 &AhciRegisters->MapCmdList
2049 );
2050
2051 if (EFI_ERROR (Status) || (Bytes != MaxCommandListSize)) {
2052 //
2053 // Map error or unable to map the whole cmd list buffer into a contiguous region.
2054 //
2055 Status = EFI_OUT_OF_RESOURCES;
2056 goto Error4;
2057 }
2058
2059 if ((!Support64Bit) && (AhciCmdListPciAddr > 0x100000000ULL)) {
2060 //
2061 // The AHCI HBA doesn't support 64bit addressing, so should not get a >4G pci bus master address.
2062 //
2063 Status = EFI_DEVICE_ERROR;
2064 goto Error3;
2065 }
2066 AhciRegisters->AhciCmdListPciAddr = (EFI_AHCI_COMMAND_LIST *)(UINTN)AhciCmdListPciAddr;
2067
2068 //
2069 // Allocate memory for command table
2070 // According to AHCI 1.3 spec, a PRD table can contain maximum 65535 entries.
2071 //
2072 Buffer = NULL;
2073 MaxCommandTableSize = sizeof (EFI_AHCI_COMMAND_TABLE);
2074
2075 Status = PciIo->AllocateBuffer (
2076 PciIo,
2077 AllocateAnyPages,
2078 EfiBootServicesData,
2079 EFI_SIZE_TO_PAGES ((UINTN) MaxCommandTableSize),
2080 &Buffer,
2081 0
2082 );
2083
2084 if (EFI_ERROR (Status)) {
2085 //
2086 // Free mapped resource.
2087 //
2088 Status = EFI_OUT_OF_RESOURCES;
2089 goto Error3;
2090 }
2091
2092 ZeroMem (Buffer, (UINTN)MaxCommandTableSize);
2093
2094 AhciRegisters->AhciCommandTable = Buffer;
2095 AhciRegisters->MaxCommandTableSize = MaxCommandTableSize;
2096 Bytes = (UINTN)MaxCommandTableSize;
2097
2098 Status = PciIo->Map (
2099 PciIo,
2100 EfiPciIoOperationBusMasterCommonBuffer,
2101 Buffer,
2102 &Bytes,
2103 &AhciCommandTablePciAddr,
2104 &AhciRegisters->MapCommandTable
2105 );
2106
2107 if (EFI_ERROR (Status) || (Bytes != MaxCommandTableSize)) {
2108 //
2109 // Map error or unable to map the whole cmd list buffer into a contiguous region.
2110 //
2111 Status = EFI_OUT_OF_RESOURCES;
2112 goto Error2;
2113 }
2114
2115 if ((!Support64Bit) && (AhciCommandTablePciAddr > 0x100000000ULL)) {
2116 //
2117 // The AHCI HBA doesn't support 64bit addressing, so should not get a >4G pci bus master address.
2118 //
2119 Status = EFI_DEVICE_ERROR;
2120 goto Error1;
2121 }
2122 AhciRegisters->AhciCommandTablePciAddr = (EFI_AHCI_COMMAND_TABLE *)(UINTN)AhciCommandTablePciAddr;
2123
2124 return EFI_SUCCESS;
2125 //
2126 // Map error or unable to map the whole CmdList buffer into a contiguous region.
2127 //
2128 Error1:
2129 PciIo->Unmap (
2130 PciIo,
2131 AhciRegisters->MapCommandTable
2132 );
2133 Error2:
2134 PciIo->FreeBuffer (
2135 PciIo,
2136 EFI_SIZE_TO_PAGES ((UINTN) MaxCommandTableSize),
2137 AhciRegisters->AhciCommandTable
2138 );
2139 Error3:
2140 PciIo->Unmap (
2141 PciIo,
2142 AhciRegisters->MapCmdList
2143 );
2144 Error4:
2145 PciIo->FreeBuffer (
2146 PciIo,
2147 EFI_SIZE_TO_PAGES ((UINTN) MaxCommandListSize),
2148 AhciRegisters->AhciCmdList
2149 );
2150 Error5:
2151 PciIo->Unmap (
2152 PciIo,
2153 AhciRegisters->MapRFis
2154 );
2155 Error6:
2156 PciIo->FreeBuffer (
2157 PciIo,
2158 EFI_SIZE_TO_PAGES ((UINTN) MaxReceiveFisSize),
2159 AhciRegisters->AhciRFis
2160 );
2161
2162 return Status;
2163 }
2164
2165 /**
2166 Initialize ATA host controller at AHCI mode.
2167
2168 The function is designed to initialize ATA host controller.
2169
2170 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
2171
2172 **/
2173 EFI_STATUS
2174 EFIAPI
2175 AhciModeInitialization (
2176 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance
2177 )
2178 {
2179 EFI_STATUS Status;
2180 EFI_PCI_IO_PROTOCOL *PciIo;
2181 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeInit;
2182 UINT32 Capability;
2183 UINT8 MaxPortNumber;
2184 UINT32 PortImplementBitMap;
2185
2186 EFI_AHCI_REGISTERS *AhciRegisters;
2187
2188 UINT8 Port;
2189 DATA_64 Data64;
2190 UINT32 Offset;
2191 UINT32 Data;
2192 EFI_IDENTIFY_DATA Buffer;
2193 EFI_ATA_DEVICE_TYPE DeviceType;
2194 EFI_ATA_COLLECTIVE_MODE *SupportedModes;
2195 EFI_ATA_TRANSFER_MODE TransferMode;
2196 UINT32 PhyDetectDelay;
2197
2198 if (Instance == NULL) {
2199 return EFI_INVALID_PARAMETER;
2200 }
2201
2202 PciIo = Instance->PciIo;
2203 IdeInit = Instance->IdeControllerInit;
2204
2205 Status = AhciReset (PciIo, EFI_AHCI_BUS_RESET_TIMEOUT);
2206
2207 if (EFI_ERROR (Status)) {
2208 return EFI_DEVICE_ERROR;
2209 }
2210
2211 //
2212 // Enable AE before accessing any AHCI registers
2213 //
2214 AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE);
2215
2216 //
2217 // Collect AHCI controller information
2218 //
2219 Capability = AhciReadReg(PciIo, EFI_AHCI_CAPABILITY_OFFSET);
2220
2221 //
2222 // Get the number of command slots per port supported by this HBA.
2223 //
2224 MaxPortNumber = (UINT8) ((Capability & 0x1F) + 1);
2225
2226 //
2227 // Get the bit map of those ports exposed by this HBA.
2228 // It indicates which ports that the HBA supports are available for software to use.
2229 //
2230 PortImplementBitMap = AhciReadReg(PciIo, EFI_AHCI_PI_OFFSET);
2231
2232 AhciRegisters = &Instance->AhciRegisters;
2233 Status = AhciCreateTransferDescriptor (PciIo, AhciRegisters);
2234
2235 if (EFI_ERROR (Status)) {
2236 return EFI_OUT_OF_RESOURCES;
2237 }
2238
2239 for (Port = 0; Port < EFI_AHCI_MAX_PORTS; Port ++) {
2240 if ((PortImplementBitMap & (BIT0 << Port)) != 0) {
2241 //
2242 // According to AHCI spec, MaxPortNumber should be equal or greater than the number of implemented ports.
2243 //
2244 if ((MaxPortNumber--) == 0) {
2245 //
2246 // Should never be here.
2247 //
2248 ASSERT (FALSE);
2249 return EFI_SUCCESS;
2250 }
2251
2252 IdeInit->NotifyPhase (IdeInit, EfiIdeBeforeChannelEnumeration, Port);
2253
2254 //
2255 // Initialize FIS Base Address Register and Command List Base Address Register for use.
2256 //
2257 Data64.Uint64 = (UINTN) (AhciRegisters->AhciRFisPciAddr) + sizeof (EFI_AHCI_RECEIVED_FIS) * Port;
2258 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_FB;
2259 AhciWriteReg (PciIo, Offset, Data64.Uint32.Lower32);
2260 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_FBU;
2261 AhciWriteReg (PciIo, Offset, Data64.Uint32.Upper32);
2262
2263 Data64.Uint64 = (UINTN) (AhciRegisters->AhciCmdListPciAddr);
2264 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CLB;
2265 AhciWriteReg (PciIo, Offset, Data64.Uint32.Lower32);
2266 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CLBU;
2267 AhciWriteReg (PciIo, Offset, Data64.Uint32.Upper32);
2268
2269 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
2270 Data = AhciReadReg (PciIo, Offset);
2271 if ((Data & EFI_AHCI_PORT_CMD_CPD) != 0) {
2272 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_POD);
2273 }
2274
2275 if ((Capability & EFI_AHCI_CAP_SSS) != 0) {
2276 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_SUD);
2277 }
2278
2279 //
2280 // Disable aggressive power management.
2281 //
2282 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SCTL;
2283 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_SCTL_IPM_INIT);
2284 //
2285 // Disable the reporting of the corresponding interrupt to system software.
2286 //
2287 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_IE;
2288 AhciAndReg (PciIo, Offset, 0);
2289
2290 //
2291 // Now inform the IDE Controller Init Module.
2292 //
2293 IdeInit->NotifyPhase (IdeInit, EfiIdeBusBeforeDevicePresenceDetection, Port);
2294
2295 //
2296 // Enable FIS Receive DMA engine for the first D2H FIS.
2297 //
2298 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
2299 AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_FRE);
2300 Status = AhciWaitMmioSet (
2301 PciIo,
2302 Offset,
2303 EFI_AHCI_PORT_CMD_FR,
2304 EFI_AHCI_PORT_CMD_FR,
2305 EFI_AHCI_PORT_CMD_FR_CLEAR_TIMEOUT
2306 );
2307 if (EFI_ERROR (Status)) {
2308 continue;
2309 }
2310
2311 //
2312 // Wait no longer than 10 ms to wait the Phy to detect the presence of a device.
2313 // It's the requirment from SATA1.0a spec section 5.2.
2314 //
2315 PhyDetectDelay = EFI_AHCI_BUS_PHY_DETECT_TIMEOUT;
2316 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SSTS;
2317 do {
2318 Data = AhciReadReg (PciIo, Offset) & EFI_AHCI_PORT_SSTS_DET_MASK;
2319 if ((Data == EFI_AHCI_PORT_SSTS_DET_PCE) || (Data == EFI_AHCI_PORT_SSTS_DET)) {
2320 break;
2321 }
2322
2323 MicroSecondDelay (1000);
2324 PhyDetectDelay--;
2325 } while (PhyDetectDelay > 0);
2326
2327 if (PhyDetectDelay == 0) {
2328 //
2329 // No device detected at this port.
2330 // Clear PxCMD.SUD for those ports at which there are no device present.
2331 //
2332 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD;
2333 AhciAndReg (PciIo, Offset, (UINT32) ~(EFI_AHCI_PORT_CMD_SUD));
2334 continue;
2335 }
2336
2337 //
2338 // According to SATA1.0a spec section 5.2, we need to wait for PxTFD.BSY and PxTFD.DRQ
2339 // and PxTFD.ERR to be zero. The maximum wait time is 16s which is defined at ATA spec.
2340 //
2341 PhyDetectDelay = 16 * 1000;
2342 do {
2343 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SERR;
2344 if (AhciReadReg(PciIo, Offset) != 0) {
2345 AhciWriteReg (PciIo, Offset, AhciReadReg(PciIo, Offset));
2346 }
2347 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;
2348
2349 Data = AhciReadReg (PciIo, Offset) & EFI_AHCI_PORT_TFD_MASK;
2350 if (Data == 0) {
2351 break;
2352 }
2353
2354 MicroSecondDelay (1000);
2355 PhyDetectDelay--;
2356 } while (PhyDetectDelay > 0);
2357
2358 if (PhyDetectDelay == 0) {
2359 continue;
2360 }
2361
2362 //
2363 // When the first D2H register FIS is received, the content of PxSIG register is updated.
2364 //
2365 Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SIG;
2366 Status = AhciWaitMmioSet (
2367 PciIo,
2368 Offset,
2369 0x0000FFFF,
2370 0x00000101,
2371 EFI_TIMER_PERIOD_SECONDS(16)
2372 );
2373 if (EFI_ERROR (Status)) {
2374 continue;
2375 }
2376
2377 Data = AhciReadReg (PciIo, Offset);
2378 if ((Data & EFI_AHCI_ATAPI_SIG_MASK) == EFI_AHCI_ATAPI_DEVICE_SIG) {
2379 Status = AhciIdentifyPacket (PciIo, AhciRegisters, Port, 0, &Buffer);
2380
2381 if (EFI_ERROR (Status)) {
2382 continue;
2383 }
2384
2385 DeviceType = EfiIdeCdrom;
2386 } else if ((Data & EFI_AHCI_ATAPI_SIG_MASK) == EFI_AHCI_ATA_DEVICE_SIG) {
2387 Status = AhciIdentify (PciIo, AhciRegisters, Port, 0, &Buffer);
2388
2389 if (EFI_ERROR (Status)) {
2390 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_PERIPHERAL_FIXED_MEDIA | EFI_P_EC_NOT_DETECTED));
2391 continue;
2392 }
2393
2394 DeviceType = EfiIdeHarddisk;
2395 } else {
2396 continue;
2397 }
2398 DEBUG ((EFI_D_INFO, "port [%d] port mulitplier [%d] has a [%a]\n",
2399 Port, 0, DeviceType == EfiIdeCdrom ? "cdrom" : "harddisk"));
2400
2401 //
2402 // If the device is a hard disk, then try to enable S.M.A.R.T feature
2403 //
2404 if ((DeviceType == EfiIdeHarddisk) && PcdGetBool (PcdAtaSmartEnable)) {
2405 AhciAtaSmartSupport (
2406 PciIo,
2407 AhciRegisters,
2408 Port,
2409 0,
2410 &Buffer,
2411 NULL
2412 );
2413 }
2414
2415 //
2416 // Submit identify data to IDE controller init driver
2417 //
2418 IdeInit->SubmitData (IdeInit, Port, 0, &Buffer);
2419
2420 //
2421 // Now start to config ide device parameter and transfer mode.
2422 //
2423 Status = IdeInit->CalculateMode (
2424 IdeInit,
2425 Port,
2426 0,
2427 &SupportedModes
2428 );
2429 if (EFI_ERROR (Status)) {
2430 DEBUG ((EFI_D_ERROR, "Calculate Mode Fail, Status = %r\n", Status));
2431 continue;
2432 }
2433
2434 //
2435 // Set best supported PIO mode on this IDE device
2436 //
2437 if (SupportedModes->PioMode.Mode <= EfiAtaPioMode2) {
2438 TransferMode.ModeCategory = EFI_ATA_MODE_DEFAULT_PIO;
2439 } else {
2440 TransferMode.ModeCategory = EFI_ATA_MODE_FLOW_PIO;
2441 }
2442
2443 TransferMode.ModeNumber = (UINT8) (SupportedModes->PioMode.Mode);
2444
2445 //
2446 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
2447 // be set together. Only one DMA mode can be set to a device. If setting
2448 // DMA mode operation fails, we can continue moving on because we only use
2449 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
2450 //
2451 if (SupportedModes->UdmaMode.Valid) {
2452 TransferMode.ModeCategory = EFI_ATA_MODE_UDMA;
2453 TransferMode.ModeNumber = (UINT8) (SupportedModes->UdmaMode.Mode);
2454 } else if (SupportedModes->MultiWordDmaMode.Valid) {
2455 TransferMode.ModeCategory = EFI_ATA_MODE_MDMA;
2456 TransferMode.ModeNumber = (UINT8) SupportedModes->MultiWordDmaMode.Mode;
2457 }
2458
2459 Status = AhciDeviceSetFeature (PciIo, AhciRegisters, Port, 0, 0x03, (UINT32)(*(UINT8 *)&TransferMode));
2460 if (EFI_ERROR (Status)) {
2461 DEBUG ((EFI_D_ERROR, "Set transfer Mode Fail, Status = %r\n", Status));
2462 continue;
2463 }
2464
2465 //
2466 // Found a ATA or ATAPI device, add it into the device list.
2467 //
2468 CreateNewDeviceInfo (Instance, Port, 0, DeviceType, &Buffer);
2469 if (DeviceType == EfiIdeHarddisk) {
2470 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_PERIPHERAL_FIXED_MEDIA | EFI_P_PC_ENABLE));
2471 }
2472 }
2473 }
2474
2475 return EFI_SUCCESS;
2476 }
2477