X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FAta%2FAtaAtapiPassThru%2FAhciMode.c;h=4d01c1dd7fca2c70d97250e9bace7840135e4d09;hp=950b5da03882ccfec156d042b082653e04c6c79f;hb=6052a15f4a4297b430cf03f2456e51f8d3bb4598;hpb=cffd21712f5c487e995ac9d38222e9e5b72f494e diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c index 950b5da038..4d01c1dd7f 100644 --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c @@ -1,7 +1,8 @@ /** @file The file for AHCI mode of ATA host controller. - Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -156,9 +157,16 @@ AhciWaitMmioSet ( ) { UINT32 Value; - UINT32 Delay; + UINT64 Delay; + BOOLEAN InfiniteWait; - Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1); + if (Timeout == 0) { + InfiniteWait = TRUE; + } else { + InfiniteWait = FALSE; + } + + Delay = DivU64x32 (Timeout, 1000) + 1; do { // @@ -177,7 +185,7 @@ AhciWaitMmioSet ( Delay--; - } while (Delay > 0); + } while (InfiniteWait || (Delay > 0)); return EFI_TIMEOUT; } @@ -204,9 +212,16 @@ AhciWaitMemSet ( ) { UINT32 Value; - UINT32 Delay; + UINT64 Delay; + BOOLEAN InfiniteWait; - Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1); + if (Timeout == 0) { + InfiniteWait = TRUE; + } else { + InfiniteWait = FALSE; + } + + Delay = DivU64x32 (Timeout, 1000) + 1; do { // @@ -231,7 +246,7 @@ AhciWaitMemSet ( Delay--; - } while (Delay > 0); + } while (InfiniteWait || (Delay > 0)); return EFI_TIMEOUT; } @@ -242,7 +257,8 @@ AhciWaitMemSet ( @param[in] Address The memory address to test. @param[in] MaskValue The mask value of memory. @param[in] TestValue The test value of memory. - @param[in, out] RetryTimes The retry times value for waitting memory set. If 0, then just try once. + @param[in, out] Task Optional. Pointer to the ATA_NONBLOCK_TASK used by + non-blocking mode. If NULL, then just try once. @retval EFI_NOTREADY The memory is not set. @retval EFI_TIMEOUT The memory setting retry times out. @@ -255,13 +271,13 @@ AhciCheckMemSet ( IN UINTN Address, IN UINT32 MaskValue, IN UINT32 TestValue, - IN OUT UINTN *RetryTimes OPTIONAL + IN OUT ATA_NONBLOCK_TASK *Task ) { UINT32 Value; - if (RetryTimes != NULL) { - (*RetryTimes)--; + if (Task != NULL) { + Task->RetryTimes--; } Value = *(volatile UINT32 *) Address; @@ -271,7 +287,7 @@ AhciCheckMemSet ( return EFI_SUCCESS; } - if ((RetryTimes != NULL) && (*RetryTimes == 0)) { + if ((Task != NULL) && !Task->InfiniteWait && (Task->RetryTimes == 0)) { return EFI_TIMEOUT; } else { return EFI_NOT_READY; @@ -355,6 +371,7 @@ AhciClearPortStatus ( in the Status Register, the Error Register's value is also be dumped. @param PciIo The PCI IO protocol instance. + @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. @@ -363,24 +380,42 @@ VOID EFIAPI AhciDumpPortStatus ( IN EFI_PCI_IO_PROTOCOL *PciIo, + IN EFI_AHCI_REGISTERS *AhciRegisters, IN UINT8 Port, IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock ) { - UINT32 Offset; + UINTN Offset; UINT32 Data; + UINTN FisBaseAddr; + EFI_STATUS Status; ASSERT (PciIo != NULL); - Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD; - Data = AhciReadReg (PciIo, Offset); - if (AtaStatusBlock != NULL) { ZeroMem (AtaStatusBlock, sizeof (EFI_ATA_STATUS_BLOCK)); - AtaStatusBlock->AtaStatus = (UINT8)Data; - if ((AtaStatusBlock->AtaStatus & BIT0) != 0) { - AtaStatusBlock->AtaError = (UINT8)(Data >> 8); + FisBaseAddr = (UINTN)AhciRegisters->AhciRFis + Port * sizeof (EFI_AHCI_RECEIVED_FIS); + Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET; + + Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_REGISTER_D2H, NULL); + if (!EFI_ERROR (Status)) { + // + // If D2H FIS is received, update StatusBlock with its content. + // + CopyMem (AtaStatusBlock, (UINT8 *)Offset, sizeof (EFI_ATA_STATUS_BLOCK)); + } else { + // + // If D2H FIS is not received, only update Status & Error field through PxTFD + // as there is no other way to get the content of the Shadow Register Block. + // + Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD; + Data = AhciReadReg (PciIo, (UINT32)Offset); + + AtaStatusBlock->AtaStatus = (UINT8)Data; + if ((AtaStatusBlock->AtaStatus & BIT0) != 0) { + AtaStatusBlock->AtaError = (UINT8)(Data >> 8); + } } } } @@ -411,13 +446,7 @@ AhciEnableFisReceive ( Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD; AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_FRE); - return AhciWaitMmioSet ( - PciIo, - Offset, - EFI_AHCI_PORT_CMD_FR, - EFI_AHCI_PORT_CMD_FR, - Timeout - ); + return EFI_SUCCESS; } /** @@ -517,7 +546,7 @@ AhciBuildCommand ( // // Filling the PRDT // - PrdtNumber = (DataLength + EFI_AHCI_MAX_DATA_PER_PRDT - 1) / EFI_AHCI_MAX_DATA_PER_PRDT; + PrdtNumber = (UINT32)DivU64x32 (((UINT64)DataLength + EFI_AHCI_MAX_DATA_PER_PRDT - 1), EFI_AHCI_MAX_DATA_PER_PRDT); // // According to AHCI 1.3 spec, a PRDT entry can point to a maximum 4MB data block. @@ -683,11 +712,20 @@ AhciPioTransfer ( VOID *Map; UINTN MapLength; EFI_PCI_IO_PROTOCOL_OPERATION Flag; - UINT32 Delay; + UINT64 Delay; EFI_AHCI_COMMAND_FIS CFis; EFI_AHCI_COMMAND_LIST CmdList; UINT32 PortTfd; UINT32 PrdCount; + BOOLEAN InfiniteWait; + BOOLEAN PioFisReceived; + BOOLEAN D2hFisReceived; + + if (Timeout == 0) { + InfiniteWait = TRUE; + } else { + InfiniteWait = FALSE; + } if (Read) { Flag = EfiPciIoOperationBusMasterWrite; @@ -756,17 +794,33 @@ AhciPioTransfer ( // Wait device sends the PIO setup fis before data transfer // Status = EFI_TIMEOUT; - Delay = (UINT32) (DivU64x32 (Timeout, 1000) + 1); + Delay = DivU64x32 (Timeout, 1000) + 1; do { + PioFisReceived = FALSE; + D2hFisReceived = FALSE; Offset = FisBaseAddr + EFI_AHCI_PIO_FIS_OFFSET; - - Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_PIO_SETUP, 0); + Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_PIO_SETUP, NULL); + if (!EFI_ERROR (Status)) { + PioFisReceived = TRUE; + } + // + // According to SATA 2.6 spec section 11.7, D2h FIS means an error encountered. + // But Qemu and Marvel 9230 sata controller may just receive a D2h FIS from device + // after the transaction is finished successfully. + // To get better device compatibilities, we further check if the PxTFD's ERR bit is set. + // By this way, we can know if there is a real error happened. + // + Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET; + Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_REGISTER_D2H, NULL); if (!EFI_ERROR (Status)) { + D2hFisReceived = TRUE; + } + + if (PioFisReceived || D2hFisReceived) { Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD; PortTfd = AhciReadReg (PciIo, (UINT32) Offset); // // PxTFD will be updated if there is a D2H or SetupFIS received. - // For PIO IN transfer, D2H means a device error. Therefore we only need to check the TFD after receiving a SetupFIS. // if ((PortTfd & EFI_AHCI_PORT_TFD_ERR) != 0) { Status = EFI_DEVICE_ERROR; @@ -775,24 +829,21 @@ AhciPioTransfer ( PrdCount = *(volatile UINT32 *) (&(AhciRegisters->AhciCmdList[0].AhciCmdPrdbc)); if (PrdCount == DataCount) { + Status = EFI_SUCCESS; break; } } - Offset = FisBaseAddr + EFI_AHCI_D2H_FIS_OFFSET; - Status = AhciCheckMemSet (Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_REGISTER_D2H, 0); - if (!EFI_ERROR (Status)) { - Status = EFI_DEVICE_ERROR; - break; - } - // // Stall for 100 microseconds. // MicroSecondDelay(100); Delay--; - } while (Delay > 0); + if (Delay == 0) { + Status = EFI_TIMEOUT; + } + } while (InfiniteWait || (Delay > 0)); } else { // // Wait for D2H Fis is received @@ -834,7 +885,7 @@ Exit: Map ); - AhciDumpPortStatus (PciIo, Port, AtaStatusBlock); + AhciDumpPortStatus (PciIo, AhciRegisters, Port, AtaStatusBlock); return Status; } @@ -924,7 +975,6 @@ AhciDmaTransfer ( // if (Task != NULL) { Task->IsStart = TRUE; - Task->RetryTimes = (UINT32) (DivU64x32(Timeout, 1000) + 1); } if (Read) { Flag = EfiPciIoOperationBusMasterWrite; @@ -1000,7 +1050,7 @@ AhciDmaTransfer ( Offset, EFI_AHCI_FIS_TYPE_MASK, EFI_AHCI_FIS_REGISTER_D2H, - (UINTN *) (&Task->RetryTimes) + Task ); } else { Status = AhciWaitMemSet ( @@ -1054,7 +1104,7 @@ Exit: } } - AhciDumpPortStatus (PciIo, Port, AtaStatusBlock); + AhciDumpPortStatus (PciIo, AhciRegisters, Port, AtaStatusBlock); return Status; } @@ -1170,7 +1220,7 @@ Exit: Timeout ); - AhciDumpPortStatus (PciIo, Port, AtaStatusBlock); + AhciDumpPortStatus (PciIo, AhciRegisters, Port, AtaStatusBlock); return Status; } @@ -1304,10 +1354,6 @@ AhciStartCommand ( // // Setting the command // - Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SACT; - AhciAndReg (PciIo, Offset, 0); - AhciOrReg (PciIo, Offset, CmdSlotBit); - Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CI; AhciAndReg (PciIo, Offset, 0); AhciOrReg (PciIo, Offset, CmdSlotBit); @@ -1375,6 +1421,7 @@ AhciPortReset ( ); if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Port %d COMRESET failed: %r\n", Port, Status)); return Status; } @@ -1402,14 +1449,21 @@ AhciReset ( IN UINT64 Timeout ) { - UINT32 Delay; + UINT64 Delay; UINT32 Value; - AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE); + // + // Make sure that GHC.AE bit is set before accessing any AHCI registers. + // + Value = AhciReadReg(PciIo, EFI_AHCI_GHC_OFFSET); + + if ((Value & EFI_AHCI_GHC_ENABLE) == 0) { + AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE); + } AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_RESET); - Delay = (UINT32) (DivU64x32(Timeout, 1000) + 1); + Delay = DivU64x32(Timeout, 1000) + 1; do { Value = AhciReadReg(PciIo, EFI_AHCI_GHC_OFFSET); @@ -1439,7 +1493,7 @@ AhciReset ( @param PciIo The PCI IO protocol instance. @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. - @param PortMultiplier The timeout value of stop. + @param PortMultiplier The port multiplier port number. @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. @retval EFI_SUCCESS Successfully get the return status of S.M.A.R.T command execution. @@ -1537,7 +1591,7 @@ AhciAtaSmartReturnStatusCheck ( @param PciIo The PCI IO protocol instance. @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. - @param PortMultiplier The timeout value of stop. + @param PortMultiplier The port multiplier port number. @param IdentifyData A pointer to data buffer which is used to contain IDENTIFY data. @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. @@ -1653,7 +1707,7 @@ AhciAtaSmartSupport ( @param PciIo The PCI IO protocol instance. @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. - @param PortMultiplier The timeout value of stop. + @param PortMultiplier The port multiplier port number. @param Buffer The data buffer to store IDENTIFY PACKET data. @retval EFI_DEVICE_ERROR The cmd abort with error occurs. @@ -1711,7 +1765,7 @@ AhciIdentify ( @param PciIo The PCI IO protocol instance. @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. - @param PortMultiplier The timeout value of stop. + @param PortMultiplier The port multiplier port number. @param Buffer The data buffer to store IDENTIFY PACKET data. @retval EFI_DEVICE_ERROR The cmd abort with error occurs. @@ -1769,7 +1823,7 @@ AhciIdentifyPacket ( @param PciIo The PCI IO protocol instance. @param AhciRegisters The pointer to the EFI_AHCI_REGISTERS. @param Port The number of port. - @param PortMultiplier The timeout value of stop. + @param PortMultiplier The port multiplier port number. @param Feature The data to send Feature register. @param FeatureSpecificData The specific data for SET FEATURE cmd. @@ -2194,6 +2248,7 @@ AhciModeInitialization ( EFI_ATA_COLLECTIVE_MODE *SupportedModes; EFI_ATA_TRANSFER_MODE TransferMode; UINT32 PhyDetectDelay; + UINT32 Value; if (Instance == NULL) { return EFI_INVALID_PARAMETER; @@ -2209,14 +2264,36 @@ AhciModeInitialization ( } // - // Enable AE before accessing any AHCI registers + // Collect AHCI controller information // - AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE); + Capability = AhciReadReg (PciIo, EFI_AHCI_CAPABILITY_OFFSET); // - // Collect AHCI controller information + // Make sure that GHC.AE bit is set before accessing any AHCI registers. // - Capability = AhciReadReg(PciIo, EFI_AHCI_CAPABILITY_OFFSET); + Value = AhciReadReg(PciIo, EFI_AHCI_GHC_OFFSET); + + if ((Value & EFI_AHCI_GHC_ENABLE) == 0) { + AhciOrReg (PciIo, EFI_AHCI_GHC_OFFSET, EFI_AHCI_GHC_ENABLE); + } + + // + // Enable 64-bit DMA support in the PCI layer if this controller + // supports it. + // + if ((Capability & EFI_AHCI_CAP_S64A) != 0) { + Status = PciIo->Attributes ( + PciIo, + EfiPciIoAttributeOperationEnable, + EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_WARN, + "AhciModeInitialization: failed to enable 64-bit DMA on 64-bit capable controller (%r)\n", + Status)); + } + } // // Get the number of command slots per port supported by this HBA. @@ -2297,16 +2374,6 @@ AhciModeInitialization ( // Offset = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_CMD; AhciOrReg (PciIo, Offset, EFI_AHCI_PORT_CMD_FRE); - Status = AhciWaitMmioSet ( - PciIo, - Offset, - EFI_AHCI_PORT_CMD_FR, - EFI_AHCI_PORT_CMD_FR, - EFI_AHCI_PORT_CMD_FR_CLEAR_TIMEOUT - ); - if (EFI_ERROR (Status)) { - continue; - } // // Wait no longer than 10 ms to wait the Phy to detect the presence of a device. @@ -2356,6 +2423,7 @@ AhciModeInitialization ( } while (PhyDetectDelay > 0); if (PhyDetectDelay == 0) { + DEBUG ((EFI_D_ERROR, "Port %d Device presence detected but phy not ready (TFD=0x%X)\n", Port, Data)); continue; } @@ -2465,7 +2533,7 @@ AhciModeInitialization ( // // Found a ATA or ATAPI device, add it into the device list. // - CreateNewDeviceInfo (Instance, Port, 0, DeviceType, &Buffer); + CreateNewDeviceInfo (Instance, Port, 0xFFFF, DeviceType, &Buffer); if (DeviceType == EfiIdeHarddisk) { REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_PERIPHERAL_FIXED_MEDIA | EFI_P_PC_ENABLE)); }