X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FAta%2FAtaAtapiPassThru%2FIdeMode.c;h=0604a9d4c3cbd60774a25615cb2925b9ce90091a;hp=d3eb940839e7035442c7ebd46545d6aab210893a;hb=8536cc4b37e12d11af9432d6e73e3c8c84c68b79;hpb=a41b5272cdffba5fd9c4734d4c06c8682186d822 diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c index d3eb940839..0604a9d4c3 100644 --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c @@ -1,7 +1,7 @@ /** @file Header file for AHCI mode of ATA host controller. - Copyright (c) 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
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 @@ -17,7 +17,7 @@ /** read a one-byte data from a IDE port. - @param PciIo The PCI IO protocol instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port The IDE Port number @return the one-byte data read from IDE port @@ -51,7 +51,7 @@ IdeReadPortB ( /** write a 1-byte data to a specific IDE port. - @param PciIo The PCI IO protocol instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port The IDE port to be writen @param Data The data to write to the port **/ @@ -81,7 +81,7 @@ IdeWritePortB ( /** write a 1-word data to a specific IDE port. - @param PciIo PCI IO protocol instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port The IDE port to be writen @param Data The data to write to the port **/ @@ -111,7 +111,7 @@ IdeWritePortW ( /** write a 2-word data to a specific IDE port. - @param PciIo PCI IO protocol instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port The IDE port to be writen @param Data The data to write to the port **/ @@ -143,7 +143,7 @@ IdeWritePortDW ( Call the IO abstraction once to do the complete read, not one word at a time - @param PciIo Pointer to the EFI_PCI_IO instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port IO port to read @param Count No. of UINT16's to read @param Buffer Pointer to the data buffer for read @@ -180,7 +180,7 @@ IdeWritePortWMultiple ( Call the IO abstraction once to do the complete read, not one word at a time - @param PciIo Pointer to the EFI_PCI_IO instance + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure @param Port IO port to read @param Count Number of UINT16's to read @param Buffer Pointer to the data buffer for read @@ -217,7 +217,7 @@ IdeReadPortWMultiple ( some debug information and if there is ERR bit set in the Status Register, the Error Register's value is also be parsed and print out. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. @@ -294,11 +294,10 @@ DumpAllIdeRegisters ( } /** - This function is used to analyze the Status Register and print out - some debug information and if there is ERR bit set in the Status - Register, the Error Register's value is also be parsed and print out. + This function is used to analyze the Status Register at the condition that BSY is zero. + if there is ERR bit set in the Status Register, then return error. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. @retval EFI_SUCCESS No err information in the Status Register. @@ -312,7 +311,6 @@ CheckStatusRegister ( IN EFI_IDE_REGISTERS *IdeRegisters ) { - EFI_STATUS Status; UINT8 StatusRegister; ASSERT (PciIo != NULL); @@ -320,13 +318,14 @@ CheckStatusRegister ( StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus); - if ((StatusRegister & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) { - Status = EFI_SUCCESS; - } else { - Status = EFI_DEVICE_ERROR; + if ((StatusRegister & ATA_STSREG_BSY) == 0) { + if ((StatusRegister & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) { + return EFI_SUCCESS; + } else { + return EFI_DEVICE_ERROR; + } } - - return Status; + return EFI_SUCCESS; } /** @@ -334,9 +333,9 @@ CheckStatusRegister ( Register. DRQ is cleared when the device is finished transferring data. So this function is called after data transfer is finished. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRQ bit clear within the time out. @@ -356,7 +355,6 @@ DRQClear ( { UINT32 Delay; UINT8 StatusRegister; - UINT8 ErrorRegister; ASSERT (PciIo != NULL); ASSERT (IdeRegisters != NULL); @@ -366,22 +364,18 @@ DRQClear ( StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus); // - // wait for BSY == 0 and DRQ == 0 + // Wait for BSY == 0, then judge if DRQ is clear // - if ((StatusRegister & (ATA_STSREG_DRQ | ATA_STSREG_BSY)) == 0) { - break; - } - - if ((StatusRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((StatusRegister & ATA_STSREG_BSY) == 0) { + if ((StatusRegister & ATA_STSREG_DRQ) == ATA_STSREG_DRQ) { + return EFI_DEVICE_ERROR; + } else { + return EFI_SUCCESS; } } // - // Stall for 100 microseconds. + // Stall for 100 microseconds. // MicroSecondDelay (100); @@ -389,11 +383,7 @@ DRQClear ( } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** This function is used to poll for the DRQ bit clear in the Alternate @@ -401,9 +391,9 @@ DRQClear ( transferring data. So this function is called after data transfer is finished. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRQ bit clear within the time out. @@ -421,7 +411,6 @@ DRQClear2 ( { UINT32 Delay; UINT8 AltRegister; - UINT8 ErrorRegister; ASSERT (PciIo != NULL); ASSERT (IdeRegisters != NULL); @@ -431,17 +420,13 @@ DRQClear2 ( AltRegister = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); // - // wait for BSY == 0 and DRQ == 0 + // Wait for BSY == 0, then judge if DRQ is clear // - if ((AltRegister & (ATA_STSREG_DRQ | ATA_STSREG_BSY)) == 0) { - break; - } - - if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((AltRegister & ATA_STSREG_BSY) == 0) { + if ((AltRegister & ATA_STSREG_DRQ) == ATA_STSREG_DRQ) { + return EFI_DEVICE_ERROR; + } else { + return EFI_SUCCESS; } } @@ -454,11 +439,7 @@ DRQClear2 ( } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -468,9 +449,9 @@ DRQClear2 ( is called after the command is sent to the device and before required data is transferred. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRQ bit set within the time out. @retval EFI_TIMEOUT DRQ bit not set within the time out. @@ -497,22 +478,27 @@ DRQReady ( Delay = (UINT32) (DivU64x32(Timeout, 1000) + 1); do { // - // read Status Register will clear interrupt + // Read Status Register will clear interrupt // StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus); // - // BSY==0,DRQ==1 + // Wait for BSY == 0, then judge if DRQ is clear or ERR is set // - if ((StatusRegister & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) { - break; - } + if ((StatusRegister & ATA_STSREG_BSY) == 0) { + if ((StatusRegister & ATA_STSREG_ERR) == ATA_STSREG_ERR) { + ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - if ((StatusRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); + if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { + return EFI_ABORTED; + } + return EFI_DEVICE_ERROR; + } - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((StatusRegister & ATA_STSREG_DRQ) == ATA_STSREG_DRQ) { + return EFI_SUCCESS; + } else { + return EFI_NOT_READY; } } @@ -524,20 +510,16 @@ DRQReady ( Delay--; } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** This function is used to poll for the DRQ bit set in the Alternate Status Register. DRQ is set when the device is ready to transfer data. So this function is called after the command is sent to the device and before required data is transferred. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRQ bit set within the time out. @retval EFI_TIMEOUT DRQ bit not set within the time out. @@ -564,21 +546,26 @@ DRQReady2 ( do { // - // Read Alternate Status Register will not clear interrupt status + // Read Alternate Status Register will not clear interrupt status // AltRegister = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); // - // BSY == 0 , DRQ == 1 + // Wait for BSY == 0, then judge if DRQ is clear or ERR is set // - if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) { - break; - } + if ((AltRegister & ATA_STSREG_BSY) == 0) { + if ((AltRegister & ATA_STSREG_ERR) == ATA_STSREG_ERR) { + ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); + if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { + return EFI_ABORTED; + } + return EFI_DEVICE_ERROR; + } - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((AltRegister & ATA_STSREG_DRQ) == ATA_STSREG_DRQ) { + return EFI_SUCCESS; + } else { + return EFI_NOT_READY; } } @@ -590,11 +577,7 @@ DRQReady2 ( Delay--; } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -602,9 +585,9 @@ DRQReady2 ( bit is set when the device is ready to accept command. Most ATA commands must be sent after DRDY set except the ATAPI Packet Command. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRDY bit set within the time out. @retval EFI_TIMEOUT DRDY bit not set within the time out. @@ -630,17 +613,22 @@ DRDYReady ( do { StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus); // - // BSY == 0 , DRDY == 1 + // Wait for BSY == 0, then judge if DRDY is set or ERR is set // - if ((StatusRegister & (ATA_STSREG_DRDY | ATA_STSREG_BSY)) == ATA_STSREG_DRDY) { - break; - } + if ((StatusRegister & ATA_STSREG_BSY) == 0) { + if ((StatusRegister & ATA_STSREG_ERR) == ATA_STSREG_ERR) { + ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - if ((StatusRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); + if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { + return EFI_ABORTED; + } + return EFI_DEVICE_ERROR; + } - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((StatusRegister & ATA_STSREG_DRDY) == ATA_STSREG_DRDY) { + return EFI_SUCCESS; + } else { + return EFI_DEVICE_ERROR; } } @@ -652,11 +640,7 @@ DRDYReady ( Delay--; } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -664,9 +648,9 @@ DRDYReady ( DRDY bit is set when the device is ready to accept command. Most ATA commands must be sent after DRDY set except the ATAPI Packet Command. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS DRDY bit set within the time out. @retval EFI_TIMEOUT DRDY bit not set within the time out. @@ -693,17 +677,22 @@ DRDYReady2 ( do { AltRegister = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); // - // BSY == 0 , DRDY == 1 + // Wait for BSY == 0, then judge if DRDY is set or ERR is set // - if ((AltRegister & (ATA_STSREG_DRDY | ATA_STSREG_BSY)) == ATA_STSREG_DRDY) { - break; - } + if ((AltRegister & ATA_STSREG_BSY) == 0) { + if ((AltRegister & ATA_STSREG_ERR) == ATA_STSREG_ERR) { + ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); - if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) { - ErrorRegister = IdeReadPortB (PciIo, IdeRegisters->ErrOrFeature); + if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { + return EFI_ABORTED; + } + return EFI_DEVICE_ERROR; + } - if ((ErrorRegister & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) { - return EFI_ABORTED; + if ((AltRegister & ATA_STSREG_DRDY) == ATA_STSREG_DRDY) { + return EFI_SUCCESS; + } else { + return EFI_DEVICE_ERROR; } } @@ -715,11 +704,7 @@ DRDYReady2 ( Delay--; } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -728,7 +713,7 @@ DRDYReady2 ( @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS BSY bit clear within the time out. @retval EFI_TIMEOUT BSY bit not clear within the time out. @@ -754,7 +739,7 @@ WaitForBSYClear ( StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus); if ((StatusRegister & ATA_STSREG_BSY) == 0x00) { - break; + return EFI_SUCCESS; } // @@ -766,11 +751,7 @@ WaitForBSYClear ( } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -779,7 +760,7 @@ WaitForBSYClear ( @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS BSY bit clear within the time out. @retval EFI_TIMEOUT BSY bit not clear within the time out. @@ -805,7 +786,7 @@ WaitForBSYClear2 ( AltStatusRegister = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); if ((AltStatusRegister & ATA_STSREG_BSY) == 0x00) { - break; + return EFI_SUCCESS; } // @@ -817,11 +798,7 @@ WaitForBSYClear2 ( } while (Delay > 0); - if (Delay == 0) { - return EFI_TIMEOUT; - } - - return EFI_SUCCESS; + return EFI_TIMEOUT; } /** @@ -960,7 +937,7 @@ GetIdeRegisterIoAddr ( IdeRegisters[EfiIdeSecondary].Head = (UINT16) (CommandBlockBaseAddr + 0x06); IdeRegisters[EfiIdeSecondary].CmdOrStatus = (UINT16) (CommandBlockBaseAddr + 0x07); IdeRegisters[EfiIdeSecondary].AltOrDev = ControlBlockBaseAddr; - IdeRegisters[EfiIdeSecondary].BusMasterBaseAddr = BusMasterBaseAddr + 0x8; + IdeRegisters[EfiIdeSecondary].BusMasterBaseAddr = (UINT16) (BusMasterBaseAddr + 0x8); return EFI_SUCCESS; } @@ -978,7 +955,7 @@ GetIdeRegisterIoAddr ( @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS Soft reset completes successfully. @retval EFI_DEVICE_ERROR Any step during the reset process is failed. @@ -1038,7 +1015,7 @@ AtaSoftReset ( @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. @param AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. - @param Timeout The time to complete the command. + @param Timeout The time to complete the command, uses 100ns as a unit. @retval EFI_SUCCESS Reading succeed @retval EFI_DEVICE_ERROR Error executing commands on this device. @@ -1123,16 +1100,20 @@ AtaIssueCommand ( /** This function is used to send out ATA commands conforms to the PIO Data In Protocol. - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. - @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Buffer A pointer to the source buffer for the data. - @param ByteCount The length of the data. - @param Read Flag used to determine the data transfer direction. - Read equals 1, means data transferred from device to host; - Read equals 0, means data transferred from host to device. - @param AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. - @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. - @param Timeout The time to complete the command. + @param[in] PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data + structure. + @param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. + @param[in, out] Buffer A pointer to the source buffer for the data. + @param[in] ByteCount The length of the data. + @param[in] Read Flag used to determine the data transfer direction. + Read equals 1, means data transferred from device + to host;Read equals 0, means data transferred + from host to device. + @param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. + @param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. + @param[in] Timeout The time to complete the command, uses 100ns as a unit. + @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK + used by non-blocking mode. @retval EFI_SUCCESS send out the ATA command and device send required data successfully. @retval EFI_DEVICE_ERROR command sent failed. @@ -1148,7 +1129,8 @@ AtaPioDataInOut ( IN BOOLEAN Read, IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock, IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock, - IN UINT64 Timeout + IN UINT64 Timeout, + IN ATA_NONBLOCK_TASK *Task ) { UINTN WordCount; @@ -1248,17 +1230,24 @@ Exit: // DumpAllIdeRegisters (PciIo, IdeRegisters, AtaStatusBlock); + // + // Not support the Non-blocking now,just do the blocking process. + // return Status; } /** Send ATA command into device with NON_DATA protocol - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. - @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. - @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. - @param Timeout The time to complete the command. + @param[in] PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE + data structure. + @param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. + @param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data + structure. + @param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. + @param[in] Timeout The time to complete the command, uses 100ns as a unit. + @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK + used by non-blocking mode. @retval EFI_SUCCESS Reading succeed @retval EFI_ABORTED Command failed @@ -1272,7 +1261,8 @@ AtaNonDataCommandIn ( IN EFI_IDE_REGISTERS *IdeRegisters, IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock, IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock, - IN UINT64 Timeout + IN UINT64 Timeout, + IN ATA_NONBLOCK_TASK *Task ) { EFI_STATUS Status; @@ -1310,24 +1300,137 @@ Exit: // Dump All Ide registers to ATA_STATUS_BLOCK // DumpAllIdeRegisters (PciIo, IdeRegisters, AtaStatusBlock); + + // + // Not support the Non-blocking now,just do the blocking process. + // + return Status; +} + +/** + Wait for memory to be set. + + @param[in] PciIo The PCI IO protocol instance. + @param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. + + @retval EFI_DEVICE_ERROR The memory is not set. + @retval EFI_TIMEOUT The memory setting is time out. + @retval EFI_SUCCESS The memory is correct set. + +**/ +EFI_STATUS +AtaUdmStatusWait ( + IN EFI_PCI_IO_PROTOCOL *PciIo, + IN EFI_IDE_REGISTERS *IdeRegisters + ) +{ + UINT8 RegisterValue; + EFI_STATUS Status; + UINT16 IoPortForBmis; + UINT64 Timeout; + + Timeout = 2000; + + while (TRUE) { + Status = CheckStatusRegister (PciIo, IdeRegisters); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + break; + } + + IoPortForBmis = (UINT16) (IdeRegisters->BusMasterBaseAddr + BMIS_OFFSET); + RegisterValue = IdeReadPortB (PciIo, IoPortForBmis); + if (((RegisterValue & BMIS_ERROR) != 0) || (Timeout == 0)) { + DEBUG ((EFI_D_ERROR, "ATA UDMA operation fails\n")); + Status = EFI_DEVICE_ERROR; + break; + } + + if ((RegisterValue & BMIS_INTERRUPT) != 0) { + Status = EFI_SUCCESS; + break; + } + // + // Stall for 1 milliseconds. + // + MicroSecondDelay (1000); + Timeout--; + } return Status; } +/** + Check if the memory to be set. + + @param[in] PciIo The PCI IO protocol instance. + @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK + used by non-blocking mode. + @param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. + + @retval EFI_DEVICE_ERROR The memory setting met a issue. + @retval EFI_NOT_READY The memory is not set. + @retval EFI_TIMEOUT The memory setting is time out. + @retval EFI_SUCCESS The memory is correct set. + +**/ +EFI_STATUS +AtaUdmStatusCheck ( + IN EFI_PCI_IO_PROTOCOL *PciIo, + IN ATA_NONBLOCK_TASK *Task, + IN EFI_IDE_REGISTERS *IdeRegisters + ) +{ + UINT8 RegisterValue; + UINT16 IoPortForBmis; + EFI_STATUS Status; + + Task->RetryTimes--; + + Status = CheckStatusRegister (PciIo, IdeRegisters); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + IoPortForBmis = (UINT16) (IdeRegisters->BusMasterBaseAddr + BMIS_OFFSET); + RegisterValue = IdeReadPortB (PciIo, IoPortForBmis); + + if ((RegisterValue & BMIS_ERROR) != 0) { + DEBUG ((EFI_D_ERROR, "ATA UDMA operation fails\n")); + return EFI_DEVICE_ERROR; + } + + if ((RegisterValue & BMIS_INTERRUPT) != 0) { + return EFI_SUCCESS; + } + + if (Task->RetryTimes == 0) { + return EFI_TIMEOUT; + } else { + // + // The memory is not set. + // + return EFI_NOT_READY; + } +} /** Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt). - @param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. - @param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. - @param Read Flag used to determine the data transfer direction. - Read equals 1, means data transferred from device to host; - Read equals 0, means data transferred from host to device. - @param DataBuffer A pointer to the source buffer for the data. - @param DataLength The length of the data. - @param AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. - @param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. - @param Timeout The time to complete the command. + @param[in] Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data + structure. + @param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure. + @param[in] Read Flag used to determine the data transfer + direction. Read equals 1, means data transferred + from device to host;Read equals 0, means data + transferred from host to device. + @param[in] DataBuffer A pointer to the source buffer for the data. + @param[in] DataLength The length of the data. + @param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure. + @param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure. + @param[in] Timeout The time to complete the command, uses 100ns as a unit. + @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK + used by non-blocking mode. @retval EFI_SUCCESS the operation is successful. @retval EFI_OUT_OF_RESOURCES Build PRD table failed @@ -1338,14 +1441,15 @@ Exit: EFI_STATUS EFIAPI AtaUdmaInOut ( - IN EFI_PCI_IO_PROTOCOL *PciIo, - IN EFI_IDE_REGISTERS *IdeRegisters, - IN BOOLEAN Read, - IN VOID *DataBuffer, - IN UINT64 DataLength, - IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock, - IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock, - IN UINT64 Timeout + IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance, + IN EFI_IDE_REGISTERS *IdeRegisters, + IN BOOLEAN Read, + IN VOID *DataBuffer, + IN UINT64 DataLength, + IN EFI_ATA_COMMAND_BLOCK *AtaCommandBlock, + IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock, + IN UINT64 Timeout, + IN ATA_NONBLOCK_TASK *Task ) { EFI_STATUS Status; @@ -1353,18 +1457,17 @@ AtaUdmaInOut ( UINT16 IoPortForBmis; UINT16 IoPortForBmid; - UINT8 RegisterValue; - - EFI_ATA_DMA_PRD *PrdBaseAddr; - UINTN PrdTableNum; UINTN PrdTableSize; EFI_PHYSICAL_ADDRESS PrdTableMapAddr; VOID *PrdTableMap; + EFI_ATA_DMA_PRD *PrdBaseAddr; + EFI_ATA_DMA_PRD *TempPrdBaseAddr; + UINTN PrdTableNum; + UINT8 RegisterValue; UINTN PageCount; UINTN ByteCount; UINTN ByteRemaining; - UINT8 DeviceControl; VOID *BufferMap; @@ -1372,251 +1475,303 @@ AtaUdmaInOut ( EFI_PCI_IO_PROTOCOL_OPERATION PciIoOperation; UINT8 DeviceHead; - UINT8 AtaCommand; + EFI_PCI_IO_PROTOCOL *PciIo; + EFI_TPL OldTpl; - Status = EFI_SUCCESS; - PrdBaseAddr = NULL; - if ((PciIo == NULL) || (IdeRegisters == NULL) || (DataBuffer == NULL) || (AtaCommandBlock == NULL)) { - return EFI_INVALID_PARAMETER; - } + Status = EFI_SUCCESS; + PrdBaseAddr = NULL; + PrdTableMap = NULL; + BufferMap = NULL; + PageCount = 0; + PciIo = Instance->PciIo; - // - // The data buffer should be even alignment - // - if (((UINTN)DataBuffer & 0x1) != 0) { + if ((PciIo == NULL) || (IdeRegisters == NULL) || (DataBuffer == NULL) || (AtaCommandBlock == NULL)) { return EFI_INVALID_PARAMETER; } // - // Calculate the number of PRD entry. - // Every entry in PRD table can specify a 64K memory region. + // Before starting the Blocking BlockIO operation, push to finish all non-blocking + // BlockIO tasks. + // Delay 1ms to simulate the blocking time out checking. // - PrdTableNum = (UINTN)(RShiftU64(DataLength, 16) + 1); + while ((Task == NULL) && (!IsListEmpty (&Instance->NonBlockingTaskList))) { + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + AsyncNonBlockingTransferRoutine (NULL, Instance); + gBS->RestoreTPL (OldTpl); + // + // Stall for 1 milliseconds. + // + MicroSecondDelay (1000); + } // - // Make sure that the memory region of PRD table is not cross 64K boundary + // The data buffer should be even alignment // - PrdTableSize = PrdTableNum * sizeof (EFI_ATA_DMA_PRD); - if (PrdTableSize > 0x10000) { + if (((UINTN)DataBuffer & 0x1) != 0) { return EFI_INVALID_PARAMETER; } // - // Allocate buffer for PRD table initialization. + // Set relevant IO Port address. // - PageCount = EFI_SIZE_TO_PAGES (PrdTableSize); - Status = PciIo->AllocateBuffer ( - PciIo, - AllocateAnyPages, - EfiBootServicesData, - PageCount, - &PrdBaseAddr, - 0 - ); - if (EFI_ERROR (Status)) { - return EFI_OUT_OF_RESOURCES; - } - - ByteCount = EFI_PAGES_TO_SIZE (PageCount); - Status = PciIo->Map ( - PciIo, - EfiPciIoOperationBusMasterCommonBuffer, - PrdBaseAddr, - &ByteCount, - &PrdTableMapAddr, - &PrdTableMap - ); - if (EFI_ERROR (Status) || (ByteCount != EFI_PAGES_TO_SIZE (PageCount))) { - // - // If the data length actually mapped is not equal to the requested amount, - // it means the DMA operation may be broken into several discontinuous smaller chunks. - // Can't handle this case. - // - PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); - return EFI_OUT_OF_RESOURCES; - } + IoPortForBmic = (UINT16) (IdeRegisters->BusMasterBaseAddr + BMIC_OFFSET); + IoPortForBmis = (UINT16) (IdeRegisters->BusMasterBaseAddr + BMIS_OFFSET); + IoPortForBmid = (UINT16) (IdeRegisters->BusMasterBaseAddr + BMID_OFFSET); - ZeroMem ((VOID *) ((UINTN) PrdBaseAddr), ByteCount); - - // - // Map the host address of DataBuffer to DMA master address. // - if (Read) { - PciIoOperation = EfiPciIoOperationBusMasterWrite; - } else { - PciIoOperation = EfiPciIoOperationBusMasterRead; - } + // For Blocking mode, start the command. + // For non-blocking mode, when the command is not started, start it, otherwise + // go to check the status. + // + if (((Task != NULL) && (!Task->IsStart)) || (Task == NULL)) { + // + // Calculate the number of PRD entry. + // Every entry in PRD table can specify a 64K memory region. + // + PrdTableNum = (UINTN)(RShiftU64(DataLength, 16) + 1); - ByteCount = DataLength; - Status = PciIo->Map ( - PciIo, - PciIoOperation, - DataBuffer, - &ByteCount, - &BufferMapAddress, - &BufferMap - ); - if (EFI_ERROR (Status) || (ByteCount != DataLength)) { - PciIo->Unmap (PciIo, PrdTableMap); - PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); - return EFI_OUT_OF_RESOURCES; - } + // + // Make sure that the memory region of PRD table is not cross 64K boundary + // + PrdTableSize = PrdTableNum * sizeof (EFI_ATA_DMA_PRD); + if (PrdTableSize > 0x10000) { + return EFI_INVALID_PARAMETER; + } - // - // According to Ata spec, it requires the buffer address and size to be even. - // - ASSERT ((BufferMapAddress & 0x1) == 0); - ASSERT ((ByteCount & 0x1) == 0); + // + // Allocate buffer for PRD table initialization. + // + PageCount = EFI_SIZE_TO_PAGES (PrdTableSize); + Status = PciIo->AllocateBuffer ( + PciIo, + AllocateAnyPages, + EfiBootServicesData, + PageCount, + (VOID **)&PrdBaseAddr, + 0 + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } - // - // Fill the PRD table with appropriate bus master address of data buffer and data length. - // - ByteRemaining = ByteCount; - while (ByteRemaining != 0) { - if (ByteRemaining <= 0x10000) { - PrdBaseAddr->RegionBaseAddr = (UINT32) ((UINTN) BufferMapAddress); - PrdBaseAddr->ByteCount = (UINT16) ByteRemaining; - PrdBaseAddr->EndOfTable = 0x8000; - break; + ByteCount = EFI_PAGES_TO_SIZE (PageCount); + Status = PciIo->Map ( + PciIo, + EfiPciIoOperationBusMasterCommonBuffer, + PrdBaseAddr, + &ByteCount, + &PrdTableMapAddr, + &PrdTableMap + ); + if (EFI_ERROR (Status) || (ByteCount != EFI_PAGES_TO_SIZE (PageCount))) { + // + // If the data length actually mapped is not equal to the requested amount, + // it means the DMA operation may be broken into several discontinuous smaller chunks. + // Can't handle this case. + // + PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); + return EFI_OUT_OF_RESOURCES; } - PrdBaseAddr->RegionBaseAddr = (UINT32) ((UINTN) BufferMapAddress); - PrdBaseAddr->ByteCount = (UINT16) 0x0; + ZeroMem ((VOID *) ((UINTN) PrdBaseAddr), ByteCount); - ByteRemaining -= 0x10000; - BufferMapAddress += 0x10000; - PrdBaseAddr++; - } + // + // Map the host address of DataBuffer to DMA master address. + // + if (Read) { + PciIoOperation = EfiPciIoOperationBusMasterWrite; + } else { + PciIoOperation = EfiPciIoOperationBusMasterRead; + } - // - // Start to enable the DMA operation - // - DeviceHead = AtaCommandBlock->AtaDeviceHead; - AtaCommand = AtaCommandBlock->AtaCommand; + ByteCount = (UINTN)DataLength; + Status = PciIo->Map ( + PciIo, + PciIoOperation, + DataBuffer, + &ByteCount, + &BufferMapAddress, + &BufferMap + ); + if (EFI_ERROR (Status) || (ByteCount != DataLength)) { + PciIo->Unmap (PciIo, PrdTableMap); + PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); + return EFI_OUT_OF_RESOURCES; + } + + // + // According to Ata spec, it requires the buffer address and size to be even. + // + ASSERT ((BufferMapAddress & 0x1) == 0); + ASSERT ((ByteCount & 0x1) == 0); - IdeWritePortB (PciIo, IdeRegisters->Head, (UINT8)(0xe0 | DeviceHead)); + // + // Fill the PRD table with appropriate bus master address of data buffer and data length. + // + ByteRemaining = ByteCount; + TempPrdBaseAddr = PrdBaseAddr; + while (ByteRemaining != 0) { + if (ByteRemaining <= 0x10000) { + TempPrdBaseAddr->RegionBaseAddr = (UINT32) ((UINTN) BufferMapAddress); + TempPrdBaseAddr->ByteCount = (UINT16) ByteRemaining; + TempPrdBaseAddr->EndOfTable = 0x8000; + break; + } - // - // Enable interrupt to support UDMA - // - DeviceControl = 0; - IdeWritePortB (PciIo, IdeRegisters->AltOrDev, DeviceControl); + TempPrdBaseAddr->RegionBaseAddr = (UINT32) ((UINTN) BufferMapAddress); + TempPrdBaseAddr->ByteCount = (UINT16) 0x0; - IoPortForBmic = IdeRegisters->BusMasterBaseAddr + BMIC_OFFSET; - IoPortForBmis = IdeRegisters->BusMasterBaseAddr + BMIS_OFFSET; - IoPortForBmid = IdeRegisters->BusMasterBaseAddr + BMID_OFFSET; + ByteRemaining -= 0x10000; + BufferMapAddress += 0x10000; + TempPrdBaseAddr++; + } - // - // Read BMIS register and clear ERROR and INTR bit - // - RegisterValue = IdeReadPortB(PciIo, IoPortForBmis); - RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); - IdeWritePortB(PciIo, IoPortForBmis, RegisterValue); + // + // Start to enable the DMA operation + // + DeviceHead = AtaCommandBlock->AtaDeviceHead; - // - // Set the base address to BMID register - // - IdeWritePortDW(PciIo, IoPortForBmid, (UINT32)PrdTableMapAddr); + IdeWritePortB (PciIo, IdeRegisters->Head, (UINT8)(0xe0 | DeviceHead)); - // - // Set BMIC register to identify the operation direction - // - RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); - if (Read) { - RegisterValue |= BMIC_NREAD; - } else { - RegisterValue &= ~((UINT8) BMIC_NREAD); - } - IdeWritePortB(PciIo, IoPortForBmic, RegisterValue); + // + // Enable interrupt to support UDMA + // + DeviceControl = 0; + IdeWritePortB (PciIo, IdeRegisters->AltOrDev, DeviceControl); - // - // Issue ATA command - // - Status = AtaIssueCommand (PciIo, IdeRegisters, AtaCommandBlock, Timeout); + // + // Read BMIS register and clear ERROR and INTR bit + // + RegisterValue = IdeReadPortB(PciIo, IoPortForBmis); + RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); + IdeWritePortB (PciIo, IoPortForBmis, RegisterValue); - if (EFI_ERROR (Status)) { - Status = EFI_DEVICE_ERROR; - goto Exit; - } + // + // Set the base address to BMID register + // + IdeWritePortDW (PciIo, IoPortForBmid, (UINT32)PrdTableMapAddr); - // - // Set START bit of BMIC register - // - RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); - RegisterValue |= BMIC_START; - IdeWritePortB(PciIo, IoPortForBmic, RegisterValue); + // + // Set BMIC register to identify the operation direction + // + RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); + if (Read) { + RegisterValue |= BMIC_NREAD; + } else { + RegisterValue &= ~((UINT8) BMIC_NREAD); + } + IdeWritePortB (PciIo, IoPortForBmic, RegisterValue); - // - // Check the INTERRUPT and ERROR bit of BMIS - // Max transfer number of sectors for one command is 65536(32Mbyte), - // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps). - // So set the variable Count to 2000, for about 2 second Timeout time. - // - Timeout = 2000; - while (TRUE) { - RegisterValue = IdeReadPortB(PciIo, IoPortForBmis); + // + // Issue ATA command + // + Status = AtaIssueCommand (PciIo, IdeRegisters, AtaCommandBlock, Timeout); - if (((RegisterValue & BMIS_ERROR) != 0) || (Timeout == 0)) { - DEBUG ((EFI_D_ERROR, "ATA UDMA operation fails\n")); + if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; - break; + goto Exit; } - if ((RegisterValue & BMIS_INTERRUPT) != 0) { - Status = EFI_SUCCESS; - break; + Status = CheckStatusRegister (PciIo, IdeRegisters); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto Exit; } // - // Stall for 1 milliseconds. + // Set START bit of BMIC register // - MicroSecondDelay (1000); - Timeout--; + RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); + RegisterValue |= BMIC_START; + IdeWritePortB(PciIo, IoPortForBmic, RegisterValue); + + if (Task != NULL) { + // + // Max transfer number of sectors for one command is 65536(32Mbyte), + // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps). + // So set the variable Count to 2000, for about 2 second Timeout time. + // + Task->RetryTimes = 2000; + Task->Map = BufferMap; + Task->TableMap = PrdTableMap; + Task->MapBaseAddress = PrdBaseAddr; + Task->PageCount = PageCount; + Task->IsStart = TRUE; + } } // - // Read BMIS register and clear ERROR and INTR bit + // Check the INTERRUPT and ERROR bit of BMIS + // Max transfer number of sectors for one command is 65536(32Mbyte), + // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps). + // So set the variable Count to 2000, for about 2 second Timeout time. // - RegisterValue = IdeReadPortB(PciIo, IoPortForBmis); - RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); - IdeWritePortB(PciIo, IoPortForBmis, RegisterValue); + if (Task != NULL) { + Status = AtaUdmStatusCheck (PciIo, Task, IdeRegisters); + } else { + Status = AtaUdmStatusWait (PciIo, IdeRegisters); + } // - // Read Status Register of IDE device to clear interrupt + // For blocking mode, clear registers and free buffers. + // For non blocking mode, when the related registers have been set or time + // out, or a error has been happened, it needs to clear the register and free + // buffer. // - RegisterValue = IdeReadPortB(PciIo, IdeRegisters->CmdOrStatus); + if ((Task == NULL) || Status != EFI_NOT_READY) { + // + // Read BMIS register and clear ERROR and INTR bit + // + RegisterValue = IdeReadPortB (PciIo, IoPortForBmis); + RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR); + IdeWritePortB (PciIo, IoPortForBmis, RegisterValue); - // - // Clear START bit of BMIC register - // - RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); - RegisterValue &= ~((UINT8) BMIC_START); - IdeWritePortB(PciIo, IoPortForBmic, RegisterValue); + // + // Read Status Register of IDE device to clear interrupt + // + RegisterValue = IdeReadPortB(PciIo, IdeRegisters->CmdOrStatus); - // - // Disable interrupt of Select device - // - DeviceControl = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); - DeviceControl |= ATA_CTLREG_IEN_L; - IdeWritePortB (PciIo, IdeRegisters->AltOrDev, DeviceControl); - // - // Stall for 10 milliseconds. - // - MicroSecondDelay (10000); + // + // Clear START bit of BMIC register + // + RegisterValue = IdeReadPortB(PciIo, IoPortForBmic); + RegisterValue &= ~((UINT8) BMIC_START); + IdeWritePortB (PciIo, IoPortForBmic, RegisterValue); + + // + // Disable interrupt of Select device + // + DeviceControl = IdeReadPortB (PciIo, IdeRegisters->AltOrDev); + DeviceControl |= ATA_CTLREG_IEN_L; + IdeWritePortB (PciIo, IdeRegisters->AltOrDev, DeviceControl); + // + // Stall for 10 milliseconds. + // + MicroSecondDelay (10000); + + } Exit: // // Free all allocated resource // - PciIo->Unmap (PciIo, PrdTableMap); - PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); - PciIo->Unmap (PciIo, BufferMap); - - // - // Dump All Ide registers to ATA_STATUS_BLOCK - // - DumpAllIdeRegisters (PciIo, IdeRegisters, AtaStatusBlock); + if ((Task == NULL) || Status != EFI_NOT_READY) { + if (Task != NULL) { + PciIo->Unmap (PciIo, Task->TableMap); + PciIo->FreeBuffer (PciIo, Task->PageCount, Task->MapBaseAddress); + PciIo->Unmap (PciIo, Task->Map); + } else { + PciIo->Unmap (PciIo, PrdTableMap); + PciIo->FreeBuffer (PciIo, PageCount, PrdBaseAddr); + PciIo->Unmap (PciIo, BufferMap); + } + // + // Dump All Ide registers to ATA_STATUS_BLOCK + // + DumpAllIdeRegisters (PciIo, IdeRegisters, AtaStatusBlock); + } + return Status; } @@ -1672,7 +1827,8 @@ AtaPacketReadPendingData ( @param Read Flag used to determine the data transfer direction. Read equals 1, means data transferred from device to host; Read equals 0, means data transferred from host to device. - @param Timeout Timeout value for wait DRQ ready before each data stream's transfer. + @param Timeout Timeout value for wait DRQ ready before each data stream's transfer + , uses 100ns as a unit. @retval EFI_SUCCESS data is transferred successfully. @retval EFI_DEVICE_ERROR the device failed to transfer data. @@ -1715,8 +1871,7 @@ AtaPacketReadWrite ( // Status = DRQReady2 (PciIo, IdeRegisters, Timeout); if (EFI_ERROR (Status)) { - CheckStatusRegister (PciIo, IdeRegisters); - return EFI_DEVICE_ERROR; + return CheckStatusRegister (PciIo, IdeRegisters); } // @@ -1777,7 +1932,7 @@ AtaPacketReadWrite ( // // After data transfer is completed, normally, DRQ bit should clear. // - Status = DRQClear2 (PciIo, IdeRegisters, Timeout); + Status = DRQClear (PciIo, IdeRegisters, Timeout); if (EFI_ERROR (Status)) { return EFI_DEVICE_ERROR; } @@ -1795,7 +1950,7 @@ AtaPacketReadWrite ( @param[in] Device The device number of device. @param[in] SenseData A pointer to store sense data. @param[in] SenseDataLength The sense data length. - @param[in] Timeout The timeout value to execute this cmd. + @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit. @retval EFI_SUCCESS Send out the ATAPI packet command successfully. @retval EFI_DEVICE_ERROR The device failed to send data. @@ -1890,7 +2045,7 @@ AtaPacketCommandExecute ( // AtaCommandBlock.AtaCylinderLow = (UINT8) (ATAPI_MAX_BYTE_COUNT & 0x00ff); AtaCommandBlock.AtaCylinderHigh = (UINT8) (ATAPI_MAX_BYTE_COUNT >> 8); - AtaCommandBlock.AtaDeviceHead = Device << 0x4; + AtaCommandBlock.AtaDeviceHead = (UINT8) (Device << 0x4); AtaCommandBlock.AtaCommand = ATA_CMD_PACKET; IdeWritePortB (PciIo, IdeRegisters->Head, (UINT8)(0xe0 | (Device << 0x4))); @@ -2022,7 +2177,8 @@ SetDeviceTransferMode ( &Instance->IdeRegisters[Channel], &AtaCommandBlock, AtaStatusBlock, - ATA_ATAPI_TIMEOUT + ATA_ATAPI_TIMEOUT, + NULL ); return Status; @@ -2050,7 +2206,6 @@ SetDriveParameters ( IN UINT8 Device, IN EFI_ATA_DRIVE_PARMS *DriveParameters, IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock - ) { EFI_STATUS Status; @@ -2070,7 +2225,8 @@ SetDriveParameters ( &Instance->IdeRegisters[Channel], &AtaCommandBlock, AtaStatusBlock, - ATA_ATAPI_TIMEOUT + ATA_ATAPI_TIMEOUT, + NULL ); // @@ -2085,12 +2241,181 @@ SetDriveParameters ( &Instance->IdeRegisters[Channel], &AtaCommandBlock, AtaStatusBlock, - ATA_ATAPI_TIMEOUT + ATA_ATAPI_TIMEOUT, + NULL ); return Status; } +/** + Send SMART Return Status command to check if the execution of SMART cmd is successful or not. + + @param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param Channel The channel number of device. + @param Device The device number of device. + @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. + @retval Others Fail to get return status data. + +**/ +EFI_STATUS +EFIAPI +IdeAtaSmartReturnStatusCheck ( + IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance, + IN UINT8 Channel, + IN UINT8 Device, + IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock + ) +{ + EFI_STATUS Status; + EFI_ATA_COMMAND_BLOCK AtaCommandBlock; + UINT8 LBAMid; + UINT8 LBAHigh; + + ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK)); + + AtaCommandBlock.AtaCommand = ATA_CMD_SMART; + AtaCommandBlock.AtaFeatures = ATA_SMART_RETURN_STATUS; + AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F; + AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2; + AtaCommandBlock.AtaDeviceHead = (UINT8) ((Device << 0x4) | 0xe0); + + // + // Send S.M.A.R.T Read Return Status command to device + // + Status = AtaNonDataCommandIn ( + Instance->PciIo, + &Instance->IdeRegisters[Channel], + &AtaCommandBlock, + AtaStatusBlock, + ATA_ATAPI_TIMEOUT, + NULL + ); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + LBAMid = IdeReadPortB (Instance->PciIo, Instance->IdeRegisters[Channel].CylinderLsb); + LBAHigh = IdeReadPortB (Instance->PciIo, Instance->IdeRegisters[Channel].CylinderMsb); + + if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) { + // + // The threshold exceeded condition is not detected by the device + // + DEBUG ((EFI_D_INFO, "The S.M.A.R.T threshold exceeded condition is not detected\n")); + + } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) { + // + // The threshold exceeded condition is detected by the device + // + DEBUG ((EFI_D_INFO, "The S.M.A.R.T threshold exceeded condition is detected\n")); + } + + return EFI_SUCCESS; +} + +/** + Enable SMART command of the disk if supported. + + @param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure. + @param Channel The channel number of device. + @param Device The device number of device. + @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. + +**/ +VOID +EFIAPI +IdeAtaSmartSupport ( + IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance, + IN UINT8 Channel, + IN UINT8 Device, + IN EFI_IDENTIFY_DATA *IdentifyData, + IN OUT EFI_ATA_STATUS_BLOCK *AtaStatusBlock + ) +{ + EFI_STATUS Status; + EFI_ATA_COMMAND_BLOCK AtaCommandBlock; + + // + // Detect if the device supports S.M.A.R.T. + // + if ((IdentifyData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) { + // + // S.M.A.R.T is not supported by the device + // + DEBUG ((EFI_D_INFO, "S.M.A.R.T feature is not supported at [%a] channel [%a] device!\n", + (Channel == 1) ? "secondary" : "primary", (Device == 1) ? "slave" : "master")); + } else { + // + // Check if the feature is enabled. If not, then enable S.M.A.R.T. + // + if ((IdentifyData->AtaData.command_set_feature_enb_85 & 0x0001) != 0x0001) { + + ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK)); + + AtaCommandBlock.AtaCommand = ATA_CMD_SMART; + AtaCommandBlock.AtaFeatures = ATA_SMART_ENABLE_OPERATION; + AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F; + AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2; + AtaCommandBlock.AtaDeviceHead = (UINT8) ((Device << 0x4) | 0xe0); + + // + // Send S.M.A.R.T Enable command to device + // + Status = AtaNonDataCommandIn ( + Instance->PciIo, + &Instance->IdeRegisters[Channel], + &AtaCommandBlock, + AtaStatusBlock, + ATA_ATAPI_TIMEOUT, + NULL + ); + + if (!EFI_ERROR (Status)) { + // + // Send S.M.A.R.T AutoSave command to device + // + ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK)); + + AtaCommandBlock.AtaCommand = ATA_CMD_SMART; + AtaCommandBlock.AtaFeatures = 0xD2; + AtaCommandBlock.AtaSectorCount = 0xF1; + AtaCommandBlock.AtaCylinderLow = ATA_CONSTANT_4F; + AtaCommandBlock.AtaCylinderHigh = ATA_CONSTANT_C2; + AtaCommandBlock.AtaDeviceHead = (UINT8) ((Device << 0x4) | 0xe0); + + Status = AtaNonDataCommandIn ( + Instance->PciIo, + &Instance->IdeRegisters[Channel], + &AtaCommandBlock, + AtaStatusBlock, + ATA_ATAPI_TIMEOUT, + NULL + ); + if (!EFI_ERROR (Status)) { + Status = IdeAtaSmartReturnStatusCheck ( + Instance, + Channel, + Device, + AtaStatusBlock + ); + } + } + } + + DEBUG ((EFI_D_INFO, "Enabled S.M.A.R.T feature at [%a] channel [%a] device!\n", + (Channel == 1) ? "secondary" : "primary", (Device == 1) ? "slave" : "master")); + + } + + return ; +} + + /** Sends out an ATA Identify Command to the specified device. @@ -2111,6 +2436,7 @@ SetDriveParameters ( @retval EFI_SUCCESS Identify ATA device successfully. @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or device is not ATA device. @retval EFI_OUT_OF_RESOURCES Allocate memory failed. + **/ EFI_STATUS EFIAPI @@ -2123,10 +2449,10 @@ AtaIdentify ( ) { EFI_STATUS Status; - EFI_ATA_COMMAND_BLOCK AtaCommandBlock; + EFI_ATA_COMMAND_BLOCK AtaCommandBlock; ZeroMem (&AtaCommandBlock, sizeof (EFI_ATA_COMMAND_BLOCK)); - + AtaCommandBlock.AtaCommand = ATA_CMD_IDENTIFY_DRIVE; AtaCommandBlock.AtaDeviceHead = (UINT8)(Device << 0x4); @@ -2138,7 +2464,8 @@ AtaIdentify ( TRUE, &AtaCommandBlock, AtaStatusBlock, - ATA_ATAPI_TIMEOUT + ATA_ATAPI_TIMEOUT, + NULL ); return Status; @@ -2205,7 +2532,8 @@ AtaIdentifyPacket ( TRUE, &AtaCommandBlock, AtaStatusBlock, - ATA_ATAPI_TIMEOUT + ATA_ATAPI_TIMEOUT, + NULL ); return Status; @@ -2247,7 +2575,7 @@ DetectAndConfigIdeDevice ( UINT8 LBAMidReg; UINT8 LBAHighReg; EFI_ATA_DEVICE_TYPE DeviceType; - EFI_IDE_DEVICE IdeDevice; + UINT8 IdeDevice; EFI_IDE_REGISTERS *IdeRegisters; EFI_IDENTIFY_DATA Buffer; @@ -2262,7 +2590,7 @@ DetectAndConfigIdeDevice ( IdeInit = Instance->IdeControllerInit; PciIo = Instance->PciIo; - for (IdeDevice = 0; IdeDevice < EfiIdeMaxDevice; IdeDevice++) { + for (IdeDevice = 0; IdeDevice < EfiIdeMaxDevice; IdeDevice++) { // // Send ATA Device Execut Diagnostic command. // This command should work no matter DRDY is ready or not @@ -2309,6 +2637,8 @@ DetectAndConfigIdeDevice ( // if identifying ata device is failure, then try to send identify packet cmd. // if (EFI_ERROR (Status)) { + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_PERIPHERAL_FIXED_MEDIA | EFI_P_EC_NOT_DETECTED)); + DeviceType = EfiIdeCdrom; Status = AtaIdentifyPacket (Instance, IdeChannel, IdeDevice, &Buffer, NULL); } @@ -2320,7 +2650,7 @@ DetectAndConfigIdeDevice ( if (EFI_ERROR (Status)) { DeviceType = EfiIdeHarddisk; Status = AtaIdentify (Instance, IdeChannel, IdeDevice, &Buffer, NULL); - } + } } if (EFI_ERROR (Status)) { @@ -2328,11 +2658,23 @@ DetectAndConfigIdeDevice ( // No device is found at this port // continue; - } - + } + DEBUG ((EFI_D_INFO, "[%a] channel [%a] [%a] device\n", (IdeChannel == 1) ? "secondary" : "primary ", (IdeDevice == 1) ? "slave " : "master", DeviceType == EfiIdeCdrom ? "cdrom " : "harddisk")); + // + // If the device is a hard disk, then try to enable S.M.A.R.T feature + // + if (DeviceType == EfiIdeHarddisk) { + IdeAtaSmartSupport ( + Instance, + IdeChannel, + IdeDevice, + &Buffer, + NULL + ); + } // // Submit identify data to IDE controller init driver @@ -2372,7 +2714,7 @@ DetectAndConfigIdeDevice ( continue; } } - + // // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't // be set together. Only one DMA mode can be set to a device. If setting @@ -2383,7 +2725,7 @@ DetectAndConfigIdeDevice ( TransferMode.ModeCategory = EFI_ATA_MODE_UDMA; TransferMode.ModeNumber = (UINT8) (SupportedModes->UdmaMode.Mode); Status = SetDeviceTransferMode (Instance, IdeChannel, IdeDevice, &TransferMode, NULL); - + if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "Set transfer Mode Fail, Status = %r\n", Status)); continue; @@ -2392,13 +2734,13 @@ DetectAndConfigIdeDevice ( TransferMode.ModeCategory = EFI_ATA_MODE_MDMA; TransferMode.ModeNumber = (UINT8) SupportedModes->MultiWordDmaMode.Mode; Status = SetDeviceTransferMode (Instance, IdeChannel, IdeDevice, &TransferMode, NULL); - + if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "Set transfer Mode Fail, Status = %r\n", Status)); continue; } } - + // // Set Parameters for the device: // 1) Init @@ -2409,12 +2751,12 @@ DetectAndConfigIdeDevice ( // Init driver parameters // DriveParameters.Sector = (UINT8) ((ATA5_IDENTIFY_DATA *)(&Buffer.AtaData))->sectors_per_track; - DriveParameters.Heads = (UINT8) ((ATA5_IDENTIFY_DATA *)(&Buffer.AtaData))->heads - 1; + DriveParameters.Heads = (UINT8) (((ATA5_IDENTIFY_DATA *)(&Buffer.AtaData))->heads - 1); DriveParameters.MultipleSector = (UINT8) ((ATA5_IDENTIFY_DATA *)(&Buffer.AtaData))->multi_sector_cmd_max_sct_cnt; - + Status = SetDriveParameters (Instance, IdeChannel, IdeDevice, &DriveParameters, NULL); } - + // // Set IDE controller Timing Blocks in the PCI Configuration Space // @@ -2428,6 +2770,10 @@ DetectAndConfigIdeDevice ( if (EFI_ERROR (Status)) { continue; } + + if (DeviceType == EfiIdeHarddisk) { + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_PERIPHERAL_FIXED_MEDIA | EFI_P_PC_ENABLE)); + } } return EFI_SUCCESS; } @@ -2447,7 +2793,6 @@ IdeModeInitialization ( IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance ) { - BOOLEAN EnumAll; EFI_STATUS Status; EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeInit; EFI_PCI_IO_PROTOCOL *PciIo; @@ -2458,7 +2803,6 @@ IdeModeInitialization ( IdeInit = Instance->IdeControllerInit; PciIo = Instance->PciIo; - EnumAll = IdeInit->EnumAll; Channel = IdeInit->ChannelCount; //