From a98f11c5b9663638bca1fd7518a783c33c420039 Mon Sep 17 00:00:00 2001 From: jtang12 Date: Fri, 13 Oct 2006 05:51:59 +0000 Subject: [PATCH] 1. Changed device detect method to use ATA/ATAPI device signature; 2. Enhancements to better support SATA CDROM; 3. Fixed UDMA operation for buffer above 4G memory issue; 4. Fixed maximal block setting for ATAPI read sector operation to comply with spec; 5. Some minor fixes. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1739 6f19259b-4bc3-4df7-8a09-765794883524 --- EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c | 690 +++++++++++++------ EdkModulePkg/Bus/Pci/IdeBus/Dxe/atapi.c | 795 ++++++++-------------- EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.c | 361 +++++----- EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.h | 112 ++- EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.c | 23 +- EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.h | 9 + EdkModulePkg/Bus/Pci/IdeBus/Dxe/idedata.h | 15 + 7 files changed, 1061 insertions(+), 944 deletions(-) diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c index cb184a497d..a08c79cade 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c @@ -1,12 +1,12 @@ /** @file - Copyright (c) 2006, 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 - http://opensource.org/licenses/bsd-license.php + Copyright (c) 2006, 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 + http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @par Revision Reference: 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including @@ -64,11 +64,11 @@ AtaPioDataOutExt ( Sends out an ATA Identify Command to the specified device. This function is called by DiscoverIdeDevice() during its device - identification. It sends out the ATA Identify Command to the - specified device. Only ATA device responses to this command. If - the command succeeds, it returns the Identify data structure which - contains information about the device. This function extracts the - information it needs to fill the IDE_BLK_IO_DEV data structure, + identification. It sends out the ATA Identify Command to the + specified device. Only ATA device responses to this command. If + the command succeeds, it returns the Identify data structure which + contains information about the device. This function extracts the + information it needs to fill the IDE_BLK_IO_DEV data structure, including device type, media block size, media capacity, and etc. @param[in] *IdeDev @@ -76,7 +76,7 @@ AtaPioDataOutExt ( to record all the information of the IDE device. @retval EFI_SUCCESS Identify ATA device successfully. - + @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or device is not ATA device. @@ -203,13 +203,13 @@ ATAIdentify ( @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one and 48-bit addressing must be used - + @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but the capacity is below 120G, 48bit addressing is not needed @note - This function must be called after DEVICE_IDENTITY command has been + This function must be called after DEVICE_IDENTITY command has been successfully returned **/ @@ -274,7 +274,7 @@ AtaAtapi6Identify ( /** This function is called by ATAIdentify() or ATAPIIdentify() - to print device's module name. + to print device's module name. @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used @@ -295,7 +295,7 @@ PrintAtaModuleName ( } /** - This function is used to send out ATA commands conforms to the + This function is used to send out ATA commands conforms to the PIO Data In Protocol. @param[in] *IdeDev @@ -328,7 +328,7 @@ PrintAtaModuleName ( @retval EFI_SUCCESS send out the ATA command and device send required data successfully. - + @retval EFI_DEVICE_ERROR command sent failed. **/ @@ -462,7 +462,7 @@ AtaPioDataIn ( } /** - This function is used to send out ATA commands conforms to the + This function is used to send out ATA commands conforms to the PIO Data Out Protocol. @param *IdeDev @@ -480,7 +480,7 @@ AtaPioDataIn ( @retval EFI_SUCCESS send out the ATA command and device received required data successfully. - + @retval EFI_DEVICE_ERROR command sent failed. **/ @@ -571,7 +571,7 @@ AtaPioDataOut ( WordCount = 0; while (WordCount < ByteCount / 2) { - + // // DRQReady2-- read Alternate Status Register to determine the DRQ bit // data transfer can be performed only when DRQ is ready. @@ -595,7 +595,7 @@ AtaPioDataOut ( // // perform a series of write without check DRQ ready // - + IDEWritePortWMultiple ( IdeDev->PciIo, IdeDev->IoPort->Data, @@ -613,7 +613,7 @@ AtaPioDataOut ( } /** - This function is used to analyze the Status Register and print out + 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. @@ -729,10 +729,10 @@ CheckErrorStatus ( to record all the information of the IDE device. @param[in] *DataBuffer - A pointer to the destination buffer for the data. + A pointer to the destination buffer for the data. @param[in] Lba - The starting logical block address to read from + The starting logical block address to read from on the device media. @param[in] NumberOfBlocks @@ -770,7 +770,7 @@ AtaReadSectors ( // AtaCommand = READ_SECTORS_CMD; - + BlocksRemaining = NumberOfBlocks; Lba32 = (UINT32) Lba; @@ -778,7 +778,7 @@ AtaReadSectors ( Status = EFI_SUCCESS; while (BlocksRemaining > 0) { - + // // in ATA-3 spec, LBA is in 28 bit width // @@ -791,7 +791,7 @@ AtaReadSectors ( Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f); if (BlocksRemaining >= 0x100) { - + // // SectorCount8 is sent to Sector Count register, 0x00 means 256 // sectors to be read @@ -847,10 +847,10 @@ AtaReadSectors ( to record all the information of the IDE device. @param[in] *BufferData - A pointer to the source buffer for the data. + A pointer to the source buffer for the data. @param[in] Lba - The starting logical block address to write onto + The starting logical block address to write onto the device media. @param[in] NumberOfBlocks @@ -902,7 +902,7 @@ AtaWriteSectors ( Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f); if (BlocksRemaining >= 0x100) { - + // // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors // to be written @@ -945,8 +945,8 @@ AtaWriteSectors ( /** This function is used to implement the Soft Reset on the specified - device. But, the ATA Soft Reset mechanism is so strong a reset method - that it will force resetting on both devices connected to the + device. But, the ATA Soft Reset mechanism is so strong a reset method + that it will force resetting on both devices connected to the same cable. It is called by IdeBlkIoReset(), a interface function of Block @@ -988,6 +988,10 @@ AtaSoftReset ( IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + // + // SRST should assert for at least 5 us, we use 10 us for + // better compatibility + // gBS->Stall (10); // @@ -996,6 +1000,11 @@ AtaSoftReset ( DeviceControl = 0; IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + // + // Wait for at least 2 ms to check BSY status, we use 10 ms + // for better compatibility + // + gBS->Stall(10000); // // slave device needs at most 31s to clear BSY // @@ -1017,7 +1026,7 @@ AtaSoftReset ( The media id that the read request is for. @param[in] LBA - The starting logical block address to read from + The starting logical block address to read from on the device. @param[in] BufferSize @@ -1025,7 +1034,7 @@ AtaSoftReset ( multiple of the intrinsic block size of the device. @param[out] *Buffer - A pointer to the destination buffer for the data. + A pointer to the destination buffer for the data. The caller is responsible for either having implicit or explicit ownership of the memory that data is read into. @@ -1033,11 +1042,11 @@ AtaSoftReset ( @retval EFI_DEVICE_ERROR Read Blocks failed. @retval EFI_NO_MEDIA There is no media in the device. @retval EFI_MEDIA_CHANGE The MediaId is not for the current media. - + @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device. - + @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid, or the data buffer is not valid. @@ -1114,13 +1123,9 @@ AtaBlkIoReadBlocks ( } else { // // For ATA-3 compatible device, use ATA-3 read block mechanism - // Notice DMA operation can only handle 32bit address // - if ((UINTN) Buffer <= 0xFFFFFFFF) { - Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); - } - - if (EFI_ERROR (Status) || ((UINTN) Buffer > 0xFFFFFFFF)) { + Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); + if (EFI_ERROR (Status)) { Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); } } @@ -1145,7 +1150,7 @@ AtaBlkIoReadBlocks ( The media id that the write request is for. @param[in] LBA - The starting logical block address to write onto + The starting logical block address to write onto the device. @param[in] BufferSize @@ -1153,20 +1158,20 @@ AtaBlkIoReadBlocks ( multiple of the intrinsic block size of the device. @param[out] *Buffer - A pointer to the source buffer for the data. + A pointer to the source buffer for the data. The caller is responsible for either having implicit - or explicit ownership of the memory that data is + or explicit ownership of the memory that data is written from. @retval EFI_SUCCESS Write Blocks successfully. @retval EFI_DEVICE_ERROR Write Blocks failed. @retval EFI_NO_MEDIA There is no media in the device. @retval EFI_MEDIA_CHANGE The MediaId is not for the current media. - + @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device. - + @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid, or the data buffer is not valid. @@ -1241,7 +1246,7 @@ AtaBlkIoWriteBlocks ( // For ATA-3 compatible device, use ATA-3 write block mechanism // Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); - if (EFI_ERROR (Status) || ((UINTN) Buffer > 0xFFFFFFFF)) { + if (EFI_ERROR (Status)) { Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks); } } @@ -1256,15 +1261,15 @@ AtaBlkIoWriteBlocks ( /** This function is called by the AtaBlkIoReadBlocks() to perform - reading from media in block unit. The function has been enhanced to + reading from media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record all the information of the IDE device. - @param[in] *DataBuffer A pointer to the destination buffer for the data. - @param[in] StartLba The starting logical block address to read from + @param[in] *DataBuffer A pointer to the destination buffer for the data. + @param[in] StartLba The starting logical block address to read from on the device media. @param[in] NumberOfBlocks The number of transfer data blocks. @@ -1339,7 +1344,7 @@ AtaReadSectorsExt ( /** This function is called by the AtaBlkIoWriteBlocks() to perform - writing onto media in block unit. The function has been enhanced to + writing onto media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev @@ -1347,10 +1352,10 @@ AtaReadSectorsExt ( to record all the information of the IDE device. @param[in] *DataBuffer - A pointer to the source buffer for the data. + A pointer to the source buffer for the data. @param[in] Lba - The starting logical block address to write onto + The starting logical block address to write onto the device media. @param[in] NumberOfBlocks @@ -1427,14 +1432,14 @@ AtaWriteSectorsExt ( } /** - This function is used to send out ATA commands conforms to the + This function is used to send out ATA commands conforms to the PIO Data In Protocol, supporting ATA/ATAPI-6 standard Comparing with ATA-3 data in protocol, we have two differents here:
1. Do NOT wait for DRQ clear before sending command into IDE device.(the wait will frequently fail... cause writing function return error) - 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly + 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly slow down writing performance by 100 times!) @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used @@ -1448,7 +1453,7 @@ AtaWriteSectorsExt ( @retval EFI_SUCCESS send out the ATA command and device send required data successfully. - + @retval EFI_DEVICE_ERROR command sent failed. **/ @@ -1544,7 +1549,7 @@ AtaPioDataInExt ( // According to PIO data in protocol, host can perform a series of reads to // the data register after each time device set DRQ ready; // - + // // 256 words // @@ -1592,14 +1597,14 @@ AtaPioDataInExt ( } /** - This function is used to send out ATA commands conforms to the + This function is used to send out ATA commands conforms to the PIO Data Out Protocol, supporting ATA/ATAPI-6 standard Comparing with ATA-3 data out protocol, we have two differents here:
1. Do NOT wait for DRQ clear before sending command into IDE device.(the wait will frequently fail... cause writing function return error) - 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly + 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly slow down writing performance by 100 times!) @param[in] *IdeDev @@ -1614,7 +1619,7 @@ AtaPioDataInExt ( @retval EFI_SUCCESS send out the ATA command and device receive required data successfully. - + @retval EFI_DEVICE_ERROR command sent failed. **/ @@ -1661,7 +1666,7 @@ AtaPioDataOutExt ( if (EFI_ERROR (Status)) { return EFI_DEVICE_ERROR; } - + // // Fill feature register if needed // @@ -2090,7 +2095,7 @@ AtaCommandIssue ( // // Fill the start LBA registers, which are also two-byte FIFO // - + IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0); IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1); IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2); @@ -2110,15 +2115,15 @@ AtaCommandIssue ( /** This function is called by the AtaBlkIoReadBlocks() to perform - reading from media in block unit. The function has been enhanced to + reading from media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record all the information of the IDE device. - @param[in] *DataBuffer A pointer to the destination buffer for the data. + @param[in] *DataBuffer A pointer to the destination buffer for the data. - @param[in] StartLba The starting logical block address to read from + @param[in] StartLba The starting logical block address to read from on the device media. @param[in] NumberOfBlocks The number of transfer data blocks. @@ -2139,21 +2144,26 @@ AtaUdmaReadExt ( IN UINTN NumberOfBlocks ) { - IDE_DMA_PRD *PrdAddr; - IDE_DMA_PRD *UsedPrdAddr; - IDE_DMA_PRD *TempPrdAddr; - UINT8 RegisterValue; - UINT8 Device; - UINT64 IoPortForBmic; - UINT64 IoPortForBmis; - UINT64 IoPortForBmid; - EFI_STATUS Status; - UINTN PrdTableNum; - UINTN ByteCount; - UINTN ByteAvailable; - UINT8 *PrdBuffer; - UINTN RemainBlockNum; - UINT8 DeviceControl; + IDE_DMA_PRD *PrdAddr; + IDE_DMA_PRD *UsedPrdAddr; + IDE_DMA_PRD *TempPrdAddr; + UINT8 RegisterValue; + UINT8 Device; + UINT64 IoPortForBmic; + UINT64 IoPortForBmis; + UINT64 IoPortForBmid; + EFI_STATUS Status; + UINTN PrdTableNum; + UINTN ByteCount; + UINTN ByteAvailable; + UINT8 *PrdBuffer; + UINTN RemainBlockNum; + UINT8 DeviceControl; + UINT32 Count; + UINTN PageCount; + VOID *Map; + EFI_PHYSICAL_ADDRESS MemPage; + EFI_PHYSICAL_ADDRESS DeviceAddress; // // Channel and device differential. Select device. @@ -2207,7 +2217,20 @@ AtaUdmaReadExt ( // // Build PRD table // - PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD))); + MemPage = 0xFFFFFFFF; + PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD)); + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + PageCount, + &MemPage + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount)); + + PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage); // // To make sure PRD is allocated in one 64K page @@ -2225,7 +2248,19 @@ AtaUdmaReadExt ( // // Build the PRD table // - PrdBuffer = DataBuffer; + Status = IdeDev->PciIo->Map ( + IdeDev->PciIo, + EfiPciIoOperationBusMasterWrite, + DataBuffer, + &ByteCount, + &DeviceAddress, + &Map + ); + if (EFI_ERROR (Status)) { + gBS->FreePages (MemPage, PageCount); + return EFI_OUT_OF_RESOURCES; + } + PrdBuffer = (VOID *) ((UINTN) DeviceAddress); TempPrdAddr = UsedPrdAddr; while (TRUE) { @@ -2245,7 +2280,7 @@ AtaUdmaReadExt ( PrdBuffer += ByteAvailable; TempPrdAddr++; } - + // // Set the base address to BMID register // @@ -2308,15 +2343,16 @@ AtaUdmaReadExt ( // Issue READ DMA EXT command // Status = AtaCommandIssueExt ( - IdeDev, - READ_DMA_EXT_CMD, - Device, - 0, - (UINT16) NumberOfBlocks, - StartLba - ); + IdeDev, + READ_DMA_EXT_CMD, + Device, + 0, + (UINT16) NumberOfBlocks, + StartLba + ); if (EFI_ERROR (Status)) { - gBS->FreePool (PrdAddr); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } @@ -2345,7 +2381,11 @@ AtaUdmaReadExt ( // // 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. // + Count = 2000; while (TRUE) { IdeDev->PciIo->Io.Read ( @@ -2356,21 +2396,49 @@ AtaUdmaReadExt ( 1, &RegisterValue ); - if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) { - if (RegisterValue & BMIS_ERROR) { - gBS->FreePool (PrdAddr); + if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) { + if ((RegisterValue & BMIS_ERROR) || (Count == 0)) { + // + // Clear START bit of BMIC register before return EFI_DEVICE_ERROR + // + IdeDev->PciIo->Io.Read ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + + RegisterValue &= ~((UINT8)BMIC_START); + + IdeDev->PciIo->Io.Write ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } break; } gBS->Stall (1000); + Count --; } - gBS->FreePool (PrdAddr); - + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); // - // Set START bit of BMIC register + // Read Status Register of IDE device to clear interrupt + // + RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status); + // + // Clear START bit of BMIC register // IdeDev->PciIo->Io.Read ( IdeDev->PciIo, @@ -2400,20 +2468,27 @@ AtaUdmaReadExt ( StartLba += NumberOfBlocks; } + // + // Disable interrupt of Select device + // + IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl); + DeviceControl |= IEN_L; + IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + return EFI_SUCCESS; } /** This function is called by the AtaBlkIoReadBlocks() to perform - reading from media in block unit. The function has been enhanced to + reading from media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record all the information of the IDE device. - @param[in] *DataBuffer A pointer to the destination buffer for the data. - @param[in] StartLba The starting logical block address to read from + @param[in] *DataBuffer A pointer to the destination buffer for the data. + @param[in] StartLba The starting logical block address to read from on the device media. @param[in] NumberOfBlocks The number of transfer data blocks. @@ -2433,21 +2508,26 @@ AtaUdmaRead ( IN UINTN NumberOfBlocks ) { - IDE_DMA_PRD *PrdAddr; - IDE_DMA_PRD *UsedPrdAddr; - IDE_DMA_PRD *TempPrdAddr; - UINT8 RegisterValue; - UINT8 Device; - UINT64 IoPortForBmic; - UINT64 IoPortForBmis; - UINT64 IoPortForBmid; - EFI_STATUS Status; - UINTN PrdTableNum; - UINTN ByteCount; - UINTN ByteAvailable; - UINT8 *PrdBuffer; - UINTN RemainBlockNum; - UINT8 DeviceControl; + IDE_DMA_PRD *PrdAddr; + IDE_DMA_PRD *UsedPrdAddr; + IDE_DMA_PRD *TempPrdAddr; + UINT8 RegisterValue; + UINT8 Device; + UINT64 IoPortForBmic; + UINT64 IoPortForBmis; + UINT64 IoPortForBmid; + EFI_STATUS Status; + UINTN PrdTableNum; + UINTN ByteCount; + UINTN ByteAvailable; + UINT8 *PrdBuffer; + UINTN RemainBlockNum; + UINT8 DeviceControl; + UINT32 Count; + UINTN PageCount; + VOID *Map; + EFI_PHYSICAL_ADDRESS MemPage; + EFI_PHYSICAL_ADDRESS DeviceAddress; // // Channel and device differential @@ -2501,7 +2581,20 @@ AtaUdmaRead ( // // Build PRD table // - PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD))); + MemPage = 0xFFFFFFFF; + PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD)); + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + PageCount, + &MemPage + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount)); + + PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage); // // To make sure PRD is allocated in one 64K page // @@ -2518,7 +2611,19 @@ AtaUdmaRead ( // // Build the PRD table // - PrdBuffer = DataBuffer; + Status = IdeDev->PciIo->Map ( + IdeDev->PciIo, + EfiPciIoOperationBusMasterWrite, + DataBuffer, + &ByteCount, + &DeviceAddress, + &Map + ); + if (EFI_ERROR (Status)) { + gBS->FreePages (MemPage, PageCount); + return EFI_OUT_OF_RESOURCES; + } + PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress); TempPrdAddr = UsedPrdAddr; while (TRUE) { @@ -2601,15 +2706,16 @@ AtaUdmaRead ( // Issue READ DMA command // Status = AtaCommandIssue ( - IdeDev, - READ_DMA_CMD, - Device, - 0, - (UINT16) NumberOfBlocks, - StartLba - ); + IdeDev, + READ_DMA_CMD, + Device, + 0, + (UINT16) NumberOfBlocks, + StartLba + ); if (EFI_ERROR (Status)) { - gBS->FreePool (PrdAddr); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } @@ -2638,7 +2744,11 @@ AtaUdmaRead ( // // 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. // + Count = 2000; while (TRUE) { IdeDev->PciIo->Io.Read ( @@ -2649,21 +2759,49 @@ AtaUdmaRead ( 1, &RegisterValue ); - if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) { - if (RegisterValue & BMIS_ERROR) { - gBS->FreePool (PrdAddr); + if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) { + if ((RegisterValue & BMIS_ERROR) || (Count == 0)) { + // + // Clear START bit of BMIC register before return EFI_DEVICE_ERROR + // + IdeDev->PciIo->Io.Read ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + + RegisterValue &= ~((UINT8)BMIC_START); + + IdeDev->PciIo->Io.Write ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } break; } gBS->Stall (1000); + Count --; } - gBS->FreePool (PrdAddr); - + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); // - // Set START bit of BMIC register + // Read Status Register of IDE device to clear interrupt + // + RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status); + // + // Clear START bit of BMIC register // IdeDev->PciIo->Io.Read ( IdeDev->PciIo, @@ -2693,20 +2831,27 @@ AtaUdmaRead ( StartLba += NumberOfBlocks; } + // + // Disable interrupt of Select device + // + IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl); + DeviceControl |= IEN_L; + IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + return EFI_SUCCESS; } /** This function is called by the AtaBlkIoWriteBlocks() to perform - writing to media in block unit. The function has been enhanced to + writing to media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record all the information of the IDE device. - @param[in] *DataBuffer A pointer to the source buffer for the data. + @param[in] *DataBuffer A pointer to the source buffer for the data. - @param[in] StartLba The starting logical block address to write to + @param[in] StartLba The starting logical block address to write to on the device media. @param[in] NumberOfBlocks The number of transfer data blocks. @@ -2726,21 +2871,26 @@ AtaUdmaWriteExt ( IN UINTN NumberOfBlocks ) { - IDE_DMA_PRD *PrdAddr; - IDE_DMA_PRD *UsedPrdAddr; - IDE_DMA_PRD *TempPrdAddr; - UINT8 RegisterValue; - UINT8 Device; - UINT64 IoPortForBmic; - UINT64 IoPortForBmis; - UINT64 IoPortForBmid; - EFI_STATUS Status; - UINTN PrdTableNum; - UINTN ByteCount; - UINTN ByteAvailable; - UINT8 *PrdBuffer; - UINTN RemainBlockNum; - UINT8 DeviceControl; + IDE_DMA_PRD *PrdAddr; + IDE_DMA_PRD *UsedPrdAddr; + IDE_DMA_PRD *TempPrdAddr; + UINT8 RegisterValue; + UINT8 Device; + UINT64 IoPortForBmic; + UINT64 IoPortForBmis; + UINT64 IoPortForBmid; + EFI_STATUS Status; + UINTN PrdTableNum; + UINTN ByteCount; + UINTN ByteAvailable; + UINT8 *PrdBuffer; + UINTN RemainBlockNum; + UINT8 DeviceControl; + UINT32 Count; + UINTN PageCount; + VOID *Map; + EFI_PHYSICAL_ADDRESS MemPage; + EFI_PHYSICAL_ADDRESS DeviceAddress; // // Channel and device differential @@ -2794,7 +2944,20 @@ AtaUdmaWriteExt ( // // Build PRD table // - PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD))); + MemPage = 0xFFFFFFFF; + PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD)); + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + PageCount, + &MemPage + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount)); + + PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage); // // To make sure PRD is allocated in one 64K page // @@ -2811,7 +2974,19 @@ AtaUdmaWriteExt ( // // Build the PRD table // - PrdBuffer = DataBuffer; + Status = IdeDev->PciIo->Map ( + IdeDev->PciIo, + EfiPciIoOperationBusMasterRead, + DataBuffer, + &ByteCount, + &DeviceAddress, + &Map + ); + if (EFI_ERROR (Status)) { + gBS->FreePages (MemPage, PageCount); + return EFI_OUT_OF_RESOURCES; + } + PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress); TempPrdAddr = UsedPrdAddr; while (TRUE) { @@ -2831,7 +3006,7 @@ AtaUdmaWriteExt ( PrdBuffer += ByteAvailable; TempPrdAddr++; } - + // // Set the base address to BMID register // @@ -2896,15 +3071,16 @@ AtaUdmaWriteExt ( // Issue WRITE DMA EXT command // Status = AtaCommandIssueExt ( - IdeDev, - WRITE_DMA_EXT_CMD, - Device, - 0, - (UINT16) NumberOfBlocks, - StartLba - ); + IdeDev, + WRITE_DMA_EXT_CMD, + Device, + 0, + (UINT16) NumberOfBlocks, + StartLba + ); if (EFI_ERROR (Status)) { - gBS->FreePool (PrdAddr); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } @@ -2933,7 +3109,11 @@ AtaUdmaWriteExt ( // // 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. // + Count = 2000; while (TRUE) { IdeDev->PciIo->Io.Read ( @@ -2944,21 +3124,49 @@ AtaUdmaWriteExt ( 1, &RegisterValue ); - if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) { - if (RegisterValue & BMIS_ERROR) { - gBS->FreePool (PrdAddr); + if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) { + if ((RegisterValue & BMIS_ERROR) || (Count == 0)) { + // + // Clear START bit of BMIC register before return EFI_DEVICE_ERROR + // + IdeDev->PciIo->Io.Read ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + + RegisterValue &= ~((UINT8)BMIC_START); + + IdeDev->PciIo->Io.Write ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } break; } gBS->Stall (1000); + Count --; } - gBS->FreePool (PrdAddr); - + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); // - // Set START bit of BMIC register + // Read Status Register of IDE device to clear interrupt + // + RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status); + // + // Clear START bit of BMIC register // IdeDev->PciIo->Io.Read ( IdeDev->PciIo, @@ -2984,12 +3192,19 @@ AtaUdmaWriteExt ( StartLba += NumberOfBlocks; } + // + // Disable interrupt of Select device + // + IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl); + DeviceControl |= IEN_L; + IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + return EFI_SUCCESS; } /** This function is called by the AtaBlkIoWriteBlocks() to perform - writing to media in block unit. The function has been enhanced to + writing to media in block unit. The function has been enhanced to support >120GB access and transfer at most 65536 blocks per command @param[in] *IdeDev @@ -2997,10 +3212,10 @@ AtaUdmaWriteExt ( to record all the information of the IDE device. @param[in] *DataBuffer - A pointer to the source buffer for the data. + A pointer to the source buffer for the data. @param[in] StartLba - The starting logical block address to write to + The starting logical block address to write to on the device media. @param[in] NumberOfBlocks @@ -3021,21 +3236,26 @@ AtaUdmaWrite ( IN UINTN NumberOfBlocks ) { - IDE_DMA_PRD *PrdAddr; - IDE_DMA_PRD *UsedPrdAddr; - IDE_DMA_PRD *TempPrdAddr; - UINT8 RegisterValue; - UINT8 Device; - UINT64 IoPortForBmic; - UINT64 IoPortForBmis; - UINT64 IoPortForBmid; - EFI_STATUS Status; - UINTN PrdTableNum; - UINTN ByteCount; - UINTN ByteAvailable; - UINT8 *PrdBuffer; - UINTN RemainBlockNum; - UINT8 DeviceControl; + IDE_DMA_PRD *PrdAddr; + IDE_DMA_PRD *UsedPrdAddr; + IDE_DMA_PRD *TempPrdAddr; + UINT8 RegisterValue; + UINT8 Device; + UINT64 IoPortForBmic; + UINT64 IoPortForBmis; + UINT64 IoPortForBmid; + EFI_STATUS Status; + UINTN PrdTableNum; + UINTN ByteCount; + UINTN ByteAvailable; + UINT8 *PrdBuffer; + UINTN RemainBlockNum; + UINT8 DeviceControl; + UINT32 Count; + UINTN PageCount; + VOID *Map; + EFI_PHYSICAL_ADDRESS MemPage; + EFI_PHYSICAL_ADDRESS DeviceAddress; // // Channel and device differential @@ -3089,7 +3309,20 @@ AtaUdmaWrite ( // // Build PRD table // - PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD))); + MemPage = 0xFFFFFFFF; + PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD)); + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + PageCount, + &MemPage + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount)); + + PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage); // // To make sure PRD is allocated in one 64K page @@ -3107,7 +3340,19 @@ AtaUdmaWrite ( // // Build the PRD table // - PrdBuffer = DataBuffer; + Status = IdeDev->PciIo->Map ( + IdeDev->PciIo, + EfiPciIoOperationBusMasterRead, + DataBuffer, + &ByteCount, + &DeviceAddress, + &Map + ); + if (EFI_ERROR (Status)) { + gBS->FreePages (MemPage, PageCount); + return EFI_OUT_OF_RESOURCES; + } + PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress); TempPrdAddr = UsedPrdAddr; while (TRUE) { @@ -3127,7 +3372,7 @@ AtaUdmaWrite ( PrdBuffer += ByteAvailable; TempPrdAddr++; } - + // // Set the base address to BMID register // @@ -3192,15 +3437,16 @@ AtaUdmaWrite ( // Issue WRITE DMA command // Status = AtaCommandIssue ( - IdeDev, - WRITE_DMA_CMD, - Device, - 0, - (UINT16) NumberOfBlocks, - StartLba - ); + IdeDev, + WRITE_DMA_CMD, + Device, + 0, + (UINT16) NumberOfBlocks, + StartLba + ); if (EFI_ERROR (Status)) { - gBS->FreePool (PrdAddr); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } @@ -3229,7 +3475,11 @@ AtaUdmaWrite ( // // 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. // + Count = 2000; while (TRUE) { IdeDev->PciIo->Io.Read ( @@ -3240,21 +3490,50 @@ AtaUdmaWrite ( 1, &RegisterValue ); - if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) { - if (RegisterValue & BMIS_ERROR) { - gBS->FreePool (PrdAddr); + if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) { + if ((RegisterValue & BMIS_ERROR) || (Count == 0)) { + // + // Clear START bit of BMIC register before return EFI_DEVICE_ERROR + // + IdeDev->PciIo->Io.Read ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + + RegisterValue &= ~((UINT8)BMIC_START); + + IdeDev->PciIo->Io.Write ( + IdeDev->PciIo, + EfiPciIoWidthUint8, + EFI_PCI_IO_PASS_THROUGH_BAR, + IoPortForBmic, + 1, + &RegisterValue + ); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); return EFI_DEVICE_ERROR; } break; } gBS->Stall (1000); + Count --; } - gBS->FreePool (PrdAddr); + gBS->FreePages (MemPage, PageCount); + IdeDev->PciIo->Unmap (IdeDev->PciIo, Map); // - // Set START bit of BMIC register + // Read Status Register of IDE device to clear interrupt + // + RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status); + // + // Clear START bit of BMIC register // IdeDev->PciIo->Io.Read ( IdeDev->PciIo, @@ -3280,5 +3559,12 @@ AtaUdmaWrite ( StartLba += NumberOfBlocks; } + // + // Disable interrupt of Select device + // + IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl); + DeviceControl |= IEN_L; + IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl); + return EFI_SUCCESS; } diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/atapi.c b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/atapi.c index 7043306642..a54acf6994 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/atapi.c +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/atapi.c @@ -783,6 +783,15 @@ PioReadWriteData ( PtrBuffer += WordCount; ActualWordCount += WordCount; } + + if (Read) { + // + // In the case where the drive wants to send more data than we need to read, + // the DRQ bit will be set and cause delays from DRQClear2(). + // We need to read data from the drive until it clears DRQ so we can move on. + // + AtapiReadPendingData (IdeDev); + } // // After data transfer is completed, normally, DRQ bit should clear. @@ -802,25 +811,25 @@ PioReadWriteData ( Sends out ATAPI Test Unit Ready Packet Command to the specified device to find out whether device is accessible. - @param[in] *IdeDev - pointer pointing to IDE_BLK_IO_DEV data structure, used - to record all the information of the IDE device. + @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used + to record all the information of the IDE device. + @param[in] *SenseCount Sense count for this packet command - @retval EFI_SUCCESS - device is accessible. - - @retval EFI_DEVICE_ERROR - device is not accessible. + @retval EFI_SUCCESS Device is accessible. + @retval EFI_DEVICE_ERROR Device is not accessible. **/ EFI_STATUS AtapiTestUnitReady ( - IN IDE_BLK_IO_DEV *IdeDev + IN IDE_BLK_IO_DEV *IdeDev, + OUT UINTN *SenseCount ) { ATAPI_PACKET_COMMAND Packet; EFI_STATUS Status; + *SenseCount = 0; + // // fill command packet // @@ -831,7 +840,17 @@ AtapiTestUnitReady ( // send command packet // Status = AtapiPacketCommandIn (IdeDev, &Packet, NULL, 0, ATAPITIMEOUT); - return Status; + if (EFI_ERROR (Status)) { + return Status; + } + + Status = AtapiRequestSense (IdeDev, SenseCount); + if (EFI_ERROR (Status)) { + *SenseCount = 0; + return Status; + } + + return EFI_SUCCESS; } /** @@ -927,7 +946,7 @@ AtapiRequestSense ( // // Ptr is word-based pointer // - Ptr += sizeof (REQUEST_SENSE_DATA) / 2; + Ptr += (sizeof (REQUEST_SENSE_DATA) + 1) >> 1; } else { // @@ -951,30 +970,28 @@ AtapiRequestSense ( if the Read Capacity Command failed, the Sense data must be requested and be analyzed to determine if the Read Capacity Command should retry. - @param[in] *IdeDev - pointer pointing to IDE_BLK_IO_DEV data structure, used - to record all the information of the IDE device. + @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used + to record all the information of the IDE device. + @param[in] SenseCount Sense count for this packet command - @retval EFI_SUCCESS - Read Capacity Command finally completes successfully. - - @retval EFI_DEVICE_ERROR - Read Capacity Command failed because of device error. + @retval EFI_SUCCESS Read Capacity Command finally completes successfully. + @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error. - @note - parameter "IdeDev" will be updated in this function. + @note Parameter "IdeDev" will be updated in this function. TODO: EFI_NOT_READY - add return value to function comment **/ EFI_STATUS AtapiReadCapacity ( - IN IDE_BLK_IO_DEV *IdeDev + IN IDE_BLK_IO_DEV *IdeDev, + OUT UINTN *SenseCount ) { // // status returned by Read Capacity Packet Command // EFI_STATUS Status; + EFI_STATUS SenseStatus; ATAPI_PACKET_COMMAND Packet; // @@ -983,6 +1000,8 @@ AtapiReadCapacity ( READ_CAPACITY_DATA Data; READ_FORMAT_CAPACITY_DATA FormatData; + *SenseCount = 0; + ZeroMem (&Data, sizeof (Data)); ZeroMem (&FormatData, sizeof (FormatData)); @@ -991,12 +1010,12 @@ AtapiReadCapacity ( ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND)); Packet.Inquiry.opcode = READ_CAPACITY; Status = AtapiPacketCommandIn ( - IdeDev, - &Packet, - (UINT16 *) &Data, - sizeof (READ_CAPACITY_DATA), - ATAPITIMEOUT - ); + IdeDev, + &Packet, + (UINT16 *) &Data, + sizeof (READ_CAPACITY_DATA), + ATAPITIMEOUT + ); } else { // @@ -1006,81 +1025,91 @@ AtapiReadCapacity ( Packet.ReadFormatCapacity.opcode = READ_FORMAT_CAPACITY; Packet.ReadFormatCapacity.allocation_length_lo = 12; Status = AtapiPacketCommandIn ( - IdeDev, - &Packet, - (UINT16 *) &FormatData, - sizeof (READ_FORMAT_CAPACITY_DATA), - ATAPITIMEOUT - ); + IdeDev, + &Packet, + (UINT16 *) &FormatData, + sizeof (READ_FORMAT_CAPACITY_DATA), + ATAPITIMEOUT + ); } - if (!EFI_ERROR (Status)) { - - if (IdeDev->Type == IdeCdRom) { - - IdeDev->BlkIo.Media->LastBlock = (Data.LastLba3 << 24) | - (Data.LastLba2 << 16) | - (Data.LastLba1 << 8) | - Data.LastLba0; - - if (IdeDev->BlkIo.Media->LastBlock != 0) { - - IdeDev->BlkIo.Media->BlockSize = (Data.BlockSize3 << 24) | - (Data.BlockSize2 << 16) | - (Data.BlockSize1 << 8) | - Data.BlockSize0; + if (Status == EFI_TIMEOUT) { + *SenseCount = 0; + return Status; + } - IdeDev->BlkIo.Media->MediaPresent = TRUE; - } else { - IdeDev->BlkIo.Media->MediaPresent = FALSE; - return EFI_DEVICE_ERROR; - } + SenseStatus = AtapiRequestSense (IdeDev, SenseCount); - IdeDev->BlkIo.Media->ReadOnly = TRUE; + if (!EFI_ERROR (SenseStatus)) { - // - // Because the user data portion in the sector of the Data CD supported - // is always 0x800 - // - IdeDev->BlkIo.Media->BlockSize = 0x800; - } + if (!EFI_ERROR (Status)) { - if (IdeDev->Type == IdeMagnetic) { + if (IdeDev->Type == IdeCdRom) { - if (FormatData.DesCode == 3) { - IdeDev->BlkIo.Media->MediaPresent = FALSE; - IdeDev->BlkIo.Media->LastBlock = 0; - } else { + IdeDev->BlkIo.Media->LastBlock = (Data.LastLba3 << 24) | + (Data.LastLba2 << 16) | + (Data.LastLba1 << 8) | + Data.LastLba0; - IdeDev->BlkIo.Media->LastBlock = (FormatData.LastLba3 << 24) | - (FormatData.LastLba2 << 16) | - (FormatData.LastLba1 << 8) | - FormatData.LastLba0; if (IdeDev->BlkIo.Media->LastBlock != 0) { - IdeDev->BlkIo.Media->LastBlock--; - IdeDev->BlkIo.Media->BlockSize = (FormatData.BlockSize2 << 16) | - (FormatData.BlockSize1 << 8) | - FormatData.BlockSize0; + IdeDev->BlkIo.Media->BlockSize = (Data.BlockSize3 << 24) | + (Data.BlockSize2 << 16) | + (Data.BlockSize1 << 8) | + Data.BlockSize0; IdeDev->BlkIo.Media->MediaPresent = TRUE; } else { IdeDev->BlkIo.Media->MediaPresent = FALSE; - // - // Return EFI_NOT_READY operation succeeds but returned capacity is 0 - // - return EFI_NOT_READY; + return EFI_DEVICE_ERROR; } - IdeDev->BlkIo.Media->BlockSize = 0x200; + IdeDev->BlkIo.Media->ReadOnly = TRUE; + + // + // Because the user data portion in the sector of the Data CD supported + // is always 0x800 + // + IdeDev->BlkIo.Media->BlockSize = 0x800; + } + + if (IdeDev->Type == IdeMagnetic) { + + if (FormatData.DesCode == 3) { + IdeDev->BlkIo.Media->MediaPresent = FALSE; + IdeDev->BlkIo.Media->LastBlock = 0; + } else { + + IdeDev->BlkIo.Media->LastBlock = (FormatData.LastLba3 << 24) | + (FormatData.LastLba2 << 16) | + (FormatData.LastLba1 << 8) | + FormatData.LastLba0; + if (IdeDev->BlkIo.Media->LastBlock != 0) { + IdeDev->BlkIo.Media->LastBlock--; + + IdeDev->BlkIo.Media->BlockSize = (FormatData.BlockSize2 << 16) | + (FormatData.BlockSize1 << 8) | + FormatData.BlockSize0; + + IdeDev->BlkIo.Media->MediaPresent = TRUE; + } else { + IdeDev->BlkIo.Media->MediaPresent = FALSE; + // + // Return EFI_NOT_READY operation succeeds but returned capacity is 0 + // + return EFI_NOT_READY; + } + + IdeDev->BlkIo.Media->BlockSize = 0x200; + } } } return EFI_SUCCESS; } else { - + *SenseCount = 0; return EFI_DEVICE_ERROR; } } @@ -1119,240 +1148,158 @@ AtapiDetectMedia ( OUT BOOLEAN *MediaChange ) { - EFI_STATUS Status; - EFI_STATUS ReadCapacityStatus; - EFI_BLOCK_IO_MEDIA OldMediaInfo; - UINTN SenseCounts; - UINTN RetryIndex; - UINTN RetryTimes; - UINTN MaximumRetryTimes; - UINTN ReadyWaitFactor; - BOOLEAN NeedRetry; - // - // a flag used to determine whether need to perform Read Capacity command. - // - BOOLEAN NeedReadCapacity; - BOOLEAN WriteProtected; + EFI_STATUS Status; + EFI_STATUS CleanStateStatus; + EFI_BLOCK_IO_MEDIA OldMediaInfo; + UINTN RetryTimes; + UINTN RetryNotReady; + UINTN SenseCount; + SENSE_RESULT SResult; + BOOLEAN WriteProtected; + CopyMem (&OldMediaInfo, IdeDev->BlkIo.Media, sizeof (EFI_BLOCK_IO_MEDIA)); + *MediaChange = FALSE; // - // init + // Retry for SenseDeviceNotReadyNeedRetry. + // Each retry takes 1s and we limit the upper boundary to + // 120 times about 2 min. // - CopyMem (&OldMediaInfo, IdeDev->BlkIo.Media, sizeof (OldMediaInfo)); - // OldMediaInfo = *(IdeDev->BlkIo.Media); - *MediaChange = FALSE; - ReadCapacityStatus = EFI_DEVICE_ERROR; + RetryNotReady = 120; // - // if there is no media, or media is not changed, - // the request sense command will detect faster than read capacity command. - // read capacity command can be bypassed, thus improve performance. + // Do Test Unit Ready // - + DoTUR: // - // Test Unit Ready command is used to detect whether device is accessible, - // the device will produce corresponding Sense data. + // Retry 5 times // - for (RetryIndex = 0; RetryIndex < 2; RetryIndex++) { - - Status = AtapiTestUnitReady (IdeDev); - if (!EFI_ERROR (Status)) { - // - // skip the loop if test unit command succeeds. - // - break; - } + RetryTimes = 5; + while (RetryTimes != 0) { - Status = AtapiSoftReset (IdeDev); + Status = AtapiTestUnitReady (IdeDev, &SenseCount); if (EFI_ERROR (Status)) { - AtaSoftReset (IdeDev); - } - } - - SenseCounts = 0; - NeedReadCapacity = TRUE; - - // - // at most retry 5 times - // - MaximumRetryTimes = 5; - RetryTimes = 1; - - for (RetryIndex = 0; - (RetryIndex < RetryTimes) && (RetryIndex < MaximumRetryTimes); - RetryIndex++) { - - Status = AtapiRequestSense (IdeDev, &SenseCounts); - - if (!EFI_ERROR (Status)) { // - // if first time there is no Sense Key, no need to read capacity any more + // Test Unit Ready error without sense data. + // For some devices, this means there's extra data + // that has not been read, so we read these extra + // data out before going on. // - if (!HaveSenseKey (IdeDev->SenseData, SenseCounts) && - (IdeDev->BlkIo.Media->MediaPresent)) { - - if (RetryIndex == 0) { - NeedReadCapacity = FALSE; - } - - } else { + CleanStateStatus = AtapiReadPendingData (IdeDev); + if (EFI_ERROR (CleanStateStatus)) { // - // No Media + // Busy wait failed, try again // - if (IsNoMedia (IdeDev->SenseData, SenseCounts)) { - NeedReadCapacity = FALSE; - IdeDev->BlkIo.Media->MediaPresent = FALSE; - IdeDev->BlkIo.Media->LastBlock = 0; + RetryTimes--; + } + // + // Try again without counting down RetryTimes + // + continue; + } else { + + ParseSenseData (IdeDev, SenseCount, &SResult); + + switch (SResult) { + case SenseNoSenseKey: + if (IdeDev->BlkIo.Media->MediaPresent) { + goto Done; } else { // - // Media Changed - // - if (IsMediaChange (IdeDev->SenseData, SenseCounts)) { - NeedReadCapacity = TRUE; - IdeDev->BlkIo.Media->MediaId++; - } - // - // Media Error + // Media present but the internal structure need refreshed. + // Try Read Capacity // - if (IsMediaError (IdeDev->SenseData, SenseCounts)) { - return EFI_DEVICE_ERROR; - } + goto DoRC; + } + break; + + case SenseDeviceNotReadyNeedRetry: + if (--RetryNotReady == 0) { + return EFI_DEVICE_ERROR; } + gBS->Stall (1000 * STALL_1_MILLI_SECOND); + continue; + break; + + case SenseNoMedia: + IdeDev->BlkIo.Media->MediaPresent = FALSE; + IdeDev->BlkIo.Media->LastBlock = 0; + goto Done; + break; + + case SenseDeviceNotReadyNoRetry: + case SenseMediaError: + return EFI_DEVICE_ERROR; + + case SenseMediaChange: + IdeDev->BlkIo.Media->MediaId++; + goto DoRC; + break; + + default: + RetryTimes--; + break; } - } else { - // - // retry once more, if request sense command met errors. - // - RetryTimes++; } } - if (NeedReadCapacity) { - // - // at most retry 5 times - // - MaximumRetryTimes = 5; - // - // initial retry twice - // - RetryTimes = 2; - ReadyWaitFactor = 2; - - for (RetryIndex = 0; - (RetryIndex < RetryTimes) && (RetryIndex < MaximumRetryTimes); - RetryIndex++) { + return EFI_DEVICE_ERROR; - ReadCapacityStatus = AtapiReadCapacity (IdeDev); + // + // Do Read Capacity + // + DoRC: + RetryTimes = 5; - SenseCounts = 0; + while (RetryTimes != 0) { - if (!EFI_ERROR (ReadCapacityStatus)) { - // - // Read Capacity succeeded - // - break; + Status = AtapiReadCapacity (IdeDev, &SenseCount); + if (EFI_ERROR (Status)) { + RetryTimes--; + continue; } else { - if (ReadCapacityStatus == EFI_NOT_READY) { - // - // If device not ready, wait here... waiting time increases by retry - // times. - // - gBS->Stall (ReadyWaitFactor * 2000 * STALL_1_MILLI_SECOND); - ReadyWaitFactor++; - // - // retry once more - // - RetryTimes++; - continue; - } - - // - // Other errors returned, requery sense data - // - Status = AtapiRequestSense (IdeDev, &SenseCounts); - - // - // If Request Sense data failed, reset the device and retry. - // - if (EFI_ERROR (Status)) { + ParseSenseData (IdeDev, SenseCount, &SResult); - Status = AtapiSoftReset (IdeDev); + switch (SResult) { + case SenseNoSenseKey: + goto Done; + break; + case SenseDeviceNotReadyNeedRetry: // - // if ATAPI soft reset fail, - // use stronger reset mechanism -- ATA soft reset. - // - if (EFI_ERROR (Status)) { - AtaSoftReset (IdeDev); - } - // - // retry once more + // We use Test Unit Ready to retry which + // is faster. // - RetryTimes++; - continue; - } - - // - // No Media - // - if (IsNoMedia (IdeDev->SenseData, SenseCounts)) { + goto DoTUR; + break; + case SenseNoMedia: IdeDev->BlkIo.Media->MediaPresent = FALSE; IdeDev->BlkIo.Media->LastBlock = 0; - return EFI_NO_MEDIA; - } + goto Done; + break; - if (IsMediaError (IdeDev->SenseData, SenseCounts)) { + case SenseDeviceNotReadyNoRetry: + case SenseMediaError: return EFI_DEVICE_ERROR; - } - - // - // Media Changed - // - if (IsMediaChange (IdeDev->SenseData, SenseCounts)) { + + case SenseMediaChange: IdeDev->BlkIo.Media->MediaId++; - } + continue; + break; - if (!IsDriveReady (IdeDev->SenseData, SenseCounts, &NeedRetry)) { - - // - // Drive not ready: if NeedRetry, then retry once more; - // else return error - // - if (NeedRetry) { - // - // Stall 1 second to wait for drive becoming ready - // - gBS->Stall (1000 * STALL_1_MILLI_SECOND); - // - // reset retry variable to zero, - // to make it retry for "drive in progress of becoming ready". - // - RetryIndex = 0; - continue; - } else { - AtapiSoftReset (IdeDev); - return EFI_DEVICE_ERROR; - } + default: + RetryTimes--; + break; } - // - // if read capacity fail not for above reasons, retry once more - // - RetryTimes++; } - - } - - // - // tell whether the readcapacity process is successful or not in the end - // - if (EFI_ERROR (ReadCapacityStatus)) { - return EFI_DEVICE_ERROR; } - } + return EFI_DEVICE_ERROR; + + Done: // // the following code is to check the write-protected for LS120 media // @@ -1423,8 +1370,11 @@ AtapiDetectMedia ( ); } - return EFI_SUCCESS; - + if (IdeDev->BlkIo.Media->MediaPresent) { + return EFI_SUCCESS; + } else { + return EFI_NO_MEDIA; + } } /** @@ -1490,7 +1440,7 @@ AtapiReadSectors ( // // limit the data bytes that can be transferred by one Read(10) Command // - MaxBlock = (UINT16) (65536 / BlockSize); + MaxBlock = 65535; BlocksRemaining = NumberOfBlocks; @@ -2037,259 +1987,114 @@ AtapiBlkIoWriteBlocks ( } -// -// The following functions are a set of helper functions, -// which are used to parse sense key returned by the device. -// - /** - TODO: Add function description + This function is used to parse sense data. Only the first + sense data is honoured. - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description + @param[in] IdeDev Indicates the calling context. + @param[in] SenseCount Count of sense data. + @param[out] Result The parsed result. - TODO: add return values + @retval EFI_SUCCESS Successfully parsed. + @retval EFI_INVALID_PARAMETER Count of sense data is zero. **/ -BOOLEAN -IsNoMedia ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts +EFI_STATUS +ParseSenseData ( + IN IDE_BLK_IO_DEV *IdeDev, + IN UINTN SenseCount, + OUT SENSE_RESULT *Result ) { - REQUEST_SENSE_DATA *SensePointer; - UINTN Index; - BOOLEAN NoMedia; - - NoMedia = FALSE; - SensePointer = SenseData; - - for (Index = 0; Index < SenseCounts; Index++) { - // - // Sense Key is SK_NOT_READY (0x2), - // Additional Sense Code is ASC_NO_MEDIA (0x3A) - // - if ((SensePointer->sense_key == SK_NOT_READY) && - (SensePointer->addnl_sense_code == ASC_NO_MEDIA)) { - - NoMedia = TRUE; - } + REQUEST_SENSE_DATA *SenseData; - SensePointer++; + if (SenseCount == 0) { + return EFI_INVALID_PARAMETER; } - return NoMedia; -} - -/** - Test if the device meets a media error after media changed - - @param[in] *SenseData - pointer pointing to ATAPI device sense data list. - @param[in] SenseCounts - sense data number of the list - - @retval TRUE Device meets a media error - @retval FALSE No media error - -**/ -BOOLEAN -IsMediaError ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts - ) -{ - REQUEST_SENSE_DATA *SensePointer; - UINTN Index; - BOOLEAN IsError; - - IsError = FALSE; - SensePointer = SenseData; - - for (Index = 0; Index < SenseCounts; Index++) { - - switch (SensePointer->sense_key) { - - case SK_MEDIUM_ERROR: - // - // Sense Key is SK_MEDIUM_ERROR (0x3) - // - switch (SensePointer->addnl_sense_code) { - case ASC_MEDIA_ERR1: - case ASC_MEDIA_ERR2: - case ASC_MEDIA_ERR3: - case ASC_MEDIA_ERR4: - IsError = TRUE; - break; - - default: - break; - } + // + // Only use the first sense data + // + SenseData = IdeDev->SenseData; + *Result = SenseOtherSense; + switch (SenseData->sense_key) { + case SK_NO_SENSE: + *Result = SenseNoSenseKey; + break; + case SK_NOT_READY: + switch (SenseData->addnl_sense_code) { + case ASC_NO_MEDIA: + *Result = SenseNoMedia; break; - - case SK_NOT_READY: - // - // Sense Key is SK_NOT_READY (0x2) - // - switch (SensePointer->addnl_sense_code) { - // - // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6) - // - case ASC_MEDIA_UPSIDE_DOWN: - IsError = TRUE; - break; - - default: - break; - } + case ASC_MEDIA_UPSIDE_DOWN: + *Result = SenseMediaError; break; - - default: + case ASC_NOT_READY: + if (SenseData->addnl_sense_code_qualifier == ASCQ_IN_PROGRESS) { + *Result = SenseDeviceNotReadyNeedRetry; + } else { + *Result = SenseDeviceNotReadyNoRetry; + } break; } - - SensePointer++; - } - - return IsError; -} - -/** - TODO: Add function description - - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - - TODO: add return values - -**/ -BOOLEAN -IsMediaChange ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts - ) -{ - REQUEST_SENSE_DATA *SensePointer; - UINTN Index; - BOOLEAN IsMediaChange; - - IsMediaChange = FALSE; - SensePointer = SenseData; - - for (Index = 0; Index < SenseCounts; Index++) { - // - // Sense Key is SK_UNIT_ATTENTION (0x6) - // - if ((SensePointer->sense_key == SK_UNIT_ATTENTION) && - (SensePointer->addnl_sense_code == ASC_MEDIA_CHANGE)) { - - IsMediaChange = TRUE; + break; + case SK_UNIT_ATTENTION: + if (SenseData->addnl_sense_code == ASC_MEDIA_CHANGE) { + *Result = SenseMediaChange; } - - SensePointer++; - } - - return IsMediaChange; -} - -/** - TODO: Add function description - - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - @param NeedRetry TODO: add argument description - - TODO: add return values - -**/ -BOOLEAN -IsDriveReady ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts, - OUT BOOLEAN *NeedRetry - ) -{ - REQUEST_SENSE_DATA *SensePointer; - UINTN Index; - BOOLEAN IsReady; - - IsReady = TRUE; - *NeedRetry = FALSE; - SensePointer = SenseData; - - for (Index = 0; Index < SenseCounts; Index++) { - - switch (SensePointer->sense_key) { - - case SK_NOT_READY: - // - // Sense Key is SK_NOT_READY (0x2) - // - switch (SensePointer->addnl_sense_code) { - case ASC_NOT_READY: - // - // Additional Sense Code is ASC_NOT_READY (0x4) - // - switch (SensePointer->addnl_sense_code_qualifier) { - case ASCQ_IN_PROGRESS: - // - // Additional Sense Code Qualifier is ASCQ_IN_PROGRESS (0x1) - // - IsReady = FALSE; - *NeedRetry = TRUE; - break; - - default: - IsReady = FALSE; - *NeedRetry = FALSE; - break; - } - break; - - default: - break; - } - break; - - default: + break; + case SK_MEDIUM_ERROR: + switch (SenseData->addnl_sense_code) { + case ASC_MEDIA_ERR1: + case ASC_MEDIA_ERR2: + case ASC_MEDIA_ERR3: + case ASC_MEDIA_ERR4: + *Result = SenseMediaError; break; } - - SensePointer++; + break; + default: + break; } - return IsReady; + return EFI_SUCCESS; } /** - TODO: Add function description + This function reads the pending data in the device. - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description + @param[in] IdeDev Indicates the calling context. - TODO: add return values + @retval EFI_SUCCESS Successfully read. + @retval EFI_NOT_READY The BSY is set avoiding reading. **/ -BOOLEAN -HaveSenseKey ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts +EFI_STATUS +AtapiReadPendingData ( + IN IDE_BLK_IO_DEV *IdeDev ) { - BOOLEAN Have; + UINT8 AltRegister; + UINT16 TempWordBuffer; - Have = TRUE; - - // - // if first sense key in the Sense Data Array is SK_NO_SENSE, - // it indicates there is no more sense key in the Sense Data Array. - // - if (SenseData->sense_key == SK_NO_SENSE) { - Have = FALSE; + AltRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.AltStatus); + if ((AltRegister & BSY) == BSY) { + return EFI_NOT_READY; } - - return Have; + if ((AltRegister & (BSY | DRQ)) == DRQ) { + TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus); + while ((TempWordBuffer & (BSY | DRQ)) == DRQ) { + IDEReadPortWMultiple ( + IdeDev->PciIo, + IdeDev->IoPort->Data, + 1, + &TempWordBuffer + ); + TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus); + } + } + return EFI_SUCCESS; } /** diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.c b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.c index d9562c9c1f..0b28c57a03 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.c +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.c @@ -12,8 +12,11 @@ #include "idebus.h" -BOOLEAN SlaveDeviceExist = FALSE; -BOOLEAN MasterDeviceExist = FALSE; +BOOLEAN ChannelDeviceDetected = FALSE; +BOOLEAN SlaveDeviceExist = FALSE; +UINT8 SlaveDeviceType = INVALID_DEVICE_TYPE; +BOOLEAN MasterDeviceExist = FALSE; +UINT8 MasterDeviceType = INVALID_DEVICE_TYPE; /** TODO: Add function description @@ -464,57 +467,6 @@ ReassignIdeResources ( return EFI_SUCCESS; } -/** - Read SATA registers to detect SATA disks - - @param IdeDev The BLK_IO private data which specifies the IDE device - -**/ -EFI_STATUS -CheckPowerMode ( - IDE_BLK_IO_DEV *IdeDev - ) -// TODO: EFI_NOT_FOUND - add return value to function comment -// TODO: EFI_SUCCESS - add return value to function comment -// TODO: EFI_NOT_FOUND - add return value to function comment -{ - UINT8 ErrorRegister; - EFI_STATUS Status; - - IDEWritePortB ( - IdeDev->PciIo, - IdeDev->IoPort->Head, - (UINT8) ((IdeDev->Device << 4) | 0xe0) - ); - - // - // Wait 31 seconds for BSY clear. BSY should be in clear state if there exists - // a device (initial state). Normally, BSY is also in clear state if there is - // no device - // - Status = WaitForBSYClear (IdeDev, 31000); - if (EFI_ERROR (Status)) { - return EFI_NOT_FOUND; - } - - // - // select device, read error register - // - IDEWritePortB ( - IdeDev->PciIo, - IdeDev->IoPort->Head, - (UINT8) ((IdeDev->Device << 4) | 0xe0) - ); - Status = DRDYReady (IdeDev, 200); - - ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); - if ((ErrorRegister == 0x01) || (ErrorRegister == 0x81)) { - return EFI_SUCCESS; - } else { - return EFI_NOT_FOUND; - } -} - // // DiscoverIdeDevice // @@ -533,44 +485,67 @@ DiscoverIdeDevice ( // TODO: EFI_SUCCESS - add return value to function comment { EFI_STATUS Status; - BOOLEAN SataFlag; - - SataFlag = FALSE; - // - // This extra detection is for SATA disks - // - Status = CheckPowerMode (IdeDev); - if (Status == EFI_SUCCESS) { - SataFlag = TRUE; - } // // If a channel has not been checked, check it now. Then set it to "checked" state // After this step, all devices in this channel have been checked. // - Status = DetectIDEController (IdeDev); - - if ((EFI_ERROR (Status)) && !SataFlag) { - return EFI_NOT_FOUND; + if (ChannelDeviceDetected == FALSE) { + Status = DetectIDEController (IdeDev); + if (EFI_ERROR (Status)) { + return EFI_NOT_FOUND; + } } + Status = EFI_NOT_FOUND; + // - // Device exists. test if it is an ATA device + // Device exists. test if it is an ATA device. + // Prefer the result from DetectIDEController, + // if failed, try another device type to handle + // devices that not follow the spec. // - Status = ATAIdentify (IdeDev); - if (EFI_ERROR (Status)) { - // - // if not ATA device, test if it is an ATAPI device - // - Status = ATAPIIdentify (IdeDev); - if (EFI_ERROR (Status)) { - // - // if not ATAPI device either, return error. - // - return EFI_NOT_FOUND; + if ((IdeDev->Device == IdeMaster) && (MasterDeviceExist)) { + if (MasterDeviceType == ATA_DEVICE_TYPE) { + Status = ATAIdentify (IdeDev); + if (EFI_ERROR (Status)) { + Status = ATAPIIdentify (IdeDev); + if (!EFI_ERROR (Status)) { + MasterDeviceType = ATAPI_DEVICE_TYPE; + } + } + } else { + Status = ATAPIIdentify (IdeDev); + if (EFI_ERROR (Status)) { + Status = ATAIdentify (IdeDev); + if (!EFI_ERROR (Status)) { + MasterDeviceType = ATA_DEVICE_TYPE; + } + } } } - + if ((IdeDev->Device == IdeSlave) && (SlaveDeviceExist)) { + if (SlaveDeviceType == ATA_DEVICE_TYPE) { + Status = ATAIdentify (IdeDev); + if (EFI_ERROR (Status)) { + Status = ATAPIIdentify (IdeDev); + if (!EFI_ERROR (Status)) { + SlaveDeviceType = ATAPI_DEVICE_TYPE; + } + } + } else { + Status = ATAPIIdentify (IdeDev); + if (EFI_ERROR (Status)) { + Status = ATAIdentify (IdeDev); + if (!EFI_ERROR (Status)) { + SlaveDeviceType = ATA_DEVICE_TYPE; + } + } + } + } + if (EFI_ERROR (Status)) { + return EFI_NOT_FOUND; + } // // Init Block I/O interface // @@ -595,6 +570,26 @@ DiscoverIdeDevice ( return EFI_SUCCESS; } +/** + This interface is used to initialize all state data related to the detection of one + channel. + + @retval EFI_SUCCESS Completed Successfully. + +**/ +EFI_STATUS +InitializeIDEChannelData ( + VOID + ) +{ + ChannelDeviceDetected = FALSE; + MasterDeviceExist = FALSE; + MasterDeviceType = 0xff; + SlaveDeviceExist = FALSE; + SlaveDeviceType = 0xff; + return EFI_SUCCESS; +} + /** This function is called by DiscoverIdeDevice(). It is used for detect whether the IDE device exists in the specified Channel as the specified @@ -633,31 +628,12 @@ DetectIDEController ( ) { EFI_STATUS Status; - UINT8 ErrorReg; - UINT8 StatusReg; + UINT8 SectorCountReg; + UINT8 LBALowReg; + UINT8 LBAMidReg; + UINT8 LBAHighReg; UINT8 InitStatusReg; - EFI_STATUS DeviceStatus; - - // - // Slave device has been detected with master device. - // - if ((IdeDev->Device) == 1) { - if (SlaveDeviceExist) { - // - // If master not exists but slave exists, slave have to wait a while - // - if (!MasterDeviceExist) { - // - // if single slave can't be detected, add delay 4s here. - // - gBS->Stall (4000000); - } - - return EFI_SUCCESS; - } else { - return EFI_NOT_FOUND; - } - } + UINT8 StatusReg; // // Select slave device @@ -675,7 +651,7 @@ DetectIDEController ( InitStatusReg = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status); // - // Select master back + // Select Master back // IDEWritePortB ( IdeDev->PciIo, @@ -683,6 +659,7 @@ DetectIDEController ( (UINT8) ((0 << 4) | 0xe0) ); gBS->Stall (100); + // // Send ATA Device Execut Diagnostic command. // This command should work no matter DRDY is ready or not @@ -690,88 +667,124 @@ DetectIDEController ( IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, 0x90); Status = WaitForBSYClear (IdeDev, 3500); - - ErrorReg = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); - + if (EFI_ERROR (Status)) { + DEBUG((EFI_D_ERROR, "New detecting method: Send Execute Diagnostic Command: WaitForBSYClear: Status: %d\n", Status)); + return Status; + } // - // Master Error register is 0x01. D0 passed, D1 passed or not present. - // Master Error register is 0x81. D0 passed, D1 failed. Return. - // Master Error register is other value. D0 failed, D1 passed or not present.. + // Read device signature // - if (ErrorReg == 0x01) { - MasterDeviceExist = TRUE; - DeviceStatus = EFI_SUCCESS; - } else if (ErrorReg == 0x81) { - + // + // Select Master + // + IDEWritePortB ( + IdeDev->PciIo, + IdeDev->IoPort->Head, + (UINT8) ((0 << 4) | 0xe0) + ); + gBS->Stall (100); + SectorCountReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->SectorCount + ); + LBALowReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->SectorNumber + ); + LBAMidReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->CylinderLsb + ); + LBAHighReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->CylinderMsb + ); + if ((SectorCountReg == 0x1) && + (LBALowReg == 0x1) && + (LBAMidReg == 0x0) && + (LBAHighReg == 0x0)) { MasterDeviceExist = TRUE; - DeviceStatus = EFI_SUCCESS; - SlaveDeviceExist = FALSE; - - return DeviceStatus; + MasterDeviceType = ATA_DEVICE_TYPE; } else { - MasterDeviceExist = FALSE; - DeviceStatus = EFI_NOT_FOUND; + if ((LBAMidReg == 0x14) && + (LBAHighReg == 0xeb)) { + MasterDeviceExist = TRUE; + MasterDeviceType = ATAPI_DEVICE_TYPE; + } } // - // Master Error register is not 0x81, Go on check Slave - // - - // - // Stall 20ms to wait for slave device ready if master device not exists + // For some Hard Drive, it takes some time to get + // the right signature when operating in single slave mode. + // We stall 20ms to work around this. // if (!MasterDeviceExist) { gBS->Stall (20000); } // - // select slave + // Select Slave // IDEWritePortB ( IdeDev->PciIo, IdeDev->IoPort->Head, (UINT8) ((1 << 4) | 0xe0) ); - - gBS->Stall (300); - ErrorReg = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error); - - // - // Slave Error register is not 0x01, D1 failed. Return. - // - if (ErrorReg != 0x01) { - SlaveDeviceExist = FALSE; - return DeviceStatus; + gBS->Stall (100); + SectorCountReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->SectorCount + ); + LBALowReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->SectorNumber + ); + LBAMidReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->CylinderLsb + ); + LBAHighReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->CylinderMsb + ); + StatusReg = IDEReadPortB ( + IdeDev->PciIo, + IdeDev->IoPort->Reg.Status + ); + if ((SectorCountReg == 0x1) && + (LBALowReg == 0x1) && + (LBAMidReg == 0x0) && + (LBAHighReg == 0x0)) { + SlaveDeviceExist = TRUE; + SlaveDeviceType = ATA_DEVICE_TYPE; + } else { + if ((LBAMidReg == 0x14) && + (LBAHighReg == 0xeb)) { + SlaveDeviceExist = TRUE; + SlaveDeviceType = ATAPI_DEVICE_TYPE; + } } - StatusReg = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status); - // - // Most ATAPI devices don't set DRDY bit, so test with a slow but accurate - // "ATAPI TEST UNIT READY" command + // When single master is plugged, slave device + // will be wrongly detected. Here's the workaround + // for ATA devices by detecting DRY bit in status + // register. + // NOTE: This workaround doesn't apply to ATAPI. // - if (((StatusReg & DRDY) == 0) && ((InitStatusReg & DRDY) == 0)) { - Status = AtapiTestUnitReady (IdeDev); - - // - // Still fail, Slave doesn't exist. - // - if (EFI_ERROR (Status)) { - SlaveDeviceExist = FALSE; - return DeviceStatus; - } + if (MasterDeviceExist && SlaveDeviceExist && + (StatusReg & DRDY) == 0 && + (InitStatusReg & DRDY) == 0 && + MasterDeviceType == SlaveDeviceType && + SlaveDeviceType != ATAPI_DEVICE_TYPE) { + SlaveDeviceExist = FALSE; } // - // Error reg is 0x01 and DRDY is ready, - // or ATAPI test unit ready success, - // or init Slave status DRDY is ready - // Slave exists. + // Indicate this channel has been detected // - SlaveDeviceExist = TRUE; - - return DeviceStatus; - + ChannelDeviceDetected = TRUE; + return EFI_SUCCESS; } /** @@ -1251,7 +1264,7 @@ DRDYReady ( } } - gBS->Stall (15); + gBS->Stall (30); Delay--; } while (Delay); @@ -1432,6 +1445,11 @@ ReleaseIdeResources ( gBS->FreePool (IdeBlkIoDevice->DevicePath); } + if (IdeBlkIoDevice->ExitBootServiceEvent != NULL) { + gBS->CloseEvent (IdeBlkIoDevice->ExitBootServiceEvent); + IdeBlkIoDevice->ExitBootServiceEvent = NULL; + } + gBS->FreePool (IdeBlkIoDevice); IdeBlkIoDevice = NULL; @@ -1553,8 +1571,14 @@ AtaNonDataCommandIn ( // // Wait for command completion + // For ATA_SMART_CMD, we may need more timeout to let device + // adjust internal states. // - Status = WaitForBSYClear (IdeDev, ATATIMEOUT); + if (AtaCommand == ATA_SMART_CMD) { + Status = WaitForBSYClear (IdeDev, ATASMARTTIMEOUT); + } else { + Status = WaitForBSYClear (IdeDev, ATATIMEOUT); + } if (EFI_ERROR (Status)) { return EFI_DEVICE_ERROR; } @@ -1716,12 +1740,11 @@ SetDriveParameters ( // // Send Init drive parameters // - Status = AtaPioDataIn ( + Status = AtaNonDataCommandIn ( IdeDev, - NULL, - 0, INIT_DRIVE_PARAM_CMD, (UINT8) (DeviceSelect + DriveParameters->Heads), + 0, DriveParameters->Sector, 0, 0, @@ -1731,18 +1754,16 @@ SetDriveParameters ( // // Send Set Multiple parameters // - Status = AtaPioDataIn ( + Status = AtaNonDataCommandIn ( IdeDev, - NULL, - 0, SET_MULTIPLE_MODE_CMD, DeviceSelect, + 0, DriveParameters->MultipleSector, 0, 0, 0 ); - return Status; } @@ -1769,13 +1790,13 @@ EnableInterrupt ( return EFI_SUCCESS; } + /** Clear pending IDE interrupt before OS loader/kernel take control of the IDE device. @param[in] Event Pointer to this event @param[in] Context Event hanlder private data - **/ VOID EFIAPI diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.h b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.h index bb9c460d63..b83a82a9fb 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.h +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/ide.h @@ -1,15 +1,15 @@ /** @file - Header file for IDE Bus Driver, containing the helper functions' + Header file for IDE Bus Driver, containing the helper functions' entire prototype. - Copyright (c) 2006, 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 - http://opensource.org/licenses/bsd-license.php + Copyright (c) 2006, 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 + http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @par Revision Reference: 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including @@ -198,6 +198,19 @@ DiscoverIdeDevice ( ) ; +/** + This interface is used to initialize all state data related to the + detection of one channel. + + @retval EFI_SUCCESS Completed successfully. + +**/ +EFI_STATUS +InitializeIDEChannelData ( + VOID + ) +; + /** TODO: Add function description @@ -661,6 +674,7 @@ PioReadWriteData ( /** TODO: Add function description + @param IdeDev TODO: add argument description @param IdeDev TODO: add argument description TODO: add return values @@ -668,7 +682,8 @@ PioReadWriteData ( **/ EFI_STATUS AtapiTestUnitReady ( - IN IDE_BLK_IO_DEV *IdeDev + IN IDE_BLK_IO_DEV *IdeDev, + OUT UINTN *SenseCount ) ; @@ -691,6 +706,7 @@ AtapiRequestSense ( /** TODO: Add function description + @param IdeDev TODO: add argument description @param IdeDev TODO: add argument description TODO: add return values @@ -698,7 +714,8 @@ AtapiRequestSense ( **/ EFI_STATUS AtapiReadCapacity ( - IN IDE_BLK_IO_DEV *IdeDev + IN IDE_BLK_IO_DEV *IdeDev, + OUT UINTN *SenseCount ) ; @@ -819,82 +836,32 @@ AtapiBlkIoWriteBlocks ( /** TODO: Add function description - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - - TODO: add return values - -**/ -BOOLEAN -IsNoMedia ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts - ) -; - -/** - TODO: Add function description - - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - - TODO: add return values - -**/ -BOOLEAN -IsMediaError ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts - ) -; - -/** - TODO: Add function description - - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - - TODO: add return values - -**/ -BOOLEAN -IsMediaChange ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts - ) -; - -/** - TODO: Add function description - - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description - @param NeedRetry TODO: add argument description + @param IdeDev TODO: add argument description + @param SenseCount TODO: add argument description + @param Result TODO: add argument description TODO: add return values **/ -BOOLEAN -IsDriveReady ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts, - OUT BOOLEAN *NeedRetry +EFI_STATUS +ParseSenseData ( + IN IDE_BLK_IO_DEV *IdeDev, + IN UINTN SenseCount, + OUT SENSE_RESULT *Result ) ; /** TODO: Add function description - @param SenseData TODO: add argument description - @param SenseCounts TODO: add argument description + @param IdeDev TODO: add argument description TODO: add return values **/ -BOOLEAN -HaveSenseKey ( - IN REQUEST_SENSE_DATA *SenseData, - IN UINTN SenseCounts +EFI_STATUS +AtapiReadPendingData ( + IN IDE_BLK_IO_DEV *IdeDev ) ; @@ -1304,6 +1271,7 @@ EnableInterrupt ( IN IDE_BLK_IO_DEV *IdeDev ) ; + /** Clear pending IDE interrupt before OS loader/kernel take control of the IDE device. diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.c b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.c index 45baa9ec87..83dd5510e4 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.c +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.c @@ -183,7 +183,6 @@ IDEBusDriverBindingStart ( UINTN DataSize; UINT32 Attributes; IDE_BUS_DRIVER_PRIVATE_DATA *IdeBusDriverPrivateData; - EFI_EVENT Event; // // Local variables declaration for IdeControllerInit support @@ -417,6 +416,12 @@ IDEBusDriverBindingStart ( EfiIdeBusBeforeDevicePresenceDetection, IdeChannel ); + + // + // Prepare to detect IDE device of this channel + // + InitializeIDEChannelData (); + // // -- 1st inner loop --- Master/Slave ------------ Step14 // @@ -488,6 +493,15 @@ IDEBusDriverBindingStart ( IdeBlkIoDevicePtr->IdeBusDriverPrivateData = IdeBusDriverPrivateData; IdeBlkIoDevicePtr->IoPort->BusMasterBaseAddr = IdeRegsBaseAddr[IdeChannel].BusMasterBaseAddr; + // + // Report Status code: is about to detect IDE drive + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_PROGRESS_CODE, + (EFI_IO_BUS_ATA_ATAPI | EFI_P_PC_PRESENCE_DETECT), + IdeBlkIoDevicePtr->DevicePath + ); + // // Discover device, now! // @@ -523,7 +537,6 @@ IDEBusDriverBindingStart ( // Submit identify data to IDE controller init driver // CopyMem (&IdentifyData, IdeBlkIoDevicePtr->pIdData, sizeof (IdentifyData)); - // IdentifyData = *IdeBlkIoDevicePtr->pIdData; IdeBusDriverPrivateData->DeviceFound[IdeChannel * 2 + IdeDevice] = TRUE; IdeInit->SubmitData (IdeInit, IdeChannel, IdeDevice, &IdentifyData); } else { @@ -706,10 +719,9 @@ IDEBusDriverBindingStart ( EFI_TPL_NOTIFY, ClearInterrupt, IdeBlkIoDevicePtr, - &Event + &IdeBlkIoDevicePtr->ExitBootServiceEvent ); - // // end of 2nd inner loop ---- // @@ -1028,7 +1040,8 @@ IDEBlkIoReset ( // // for ATA device, using ATA reset method // - if (IdeBlkIoDevice->Type == IdeHardDisk) { + if (IdeBlkIoDevice->Type == IdeHardDisk || + IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) { return AtaSoftReset (IdeBlkIoDevice); } diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.h b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.h index d1721b956f..e49e7623e5 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.h +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idebus.h @@ -28,6 +28,10 @@ #define MAX_IDE_CHANNELS 2 #define MAX_IDE_DRIVES 2 +#define INVALID_DEVICE_TYPE 0xff +#define ATA_DEVICE_TYPE 0x00 +#define ATAPI_DEVICE_TYPE 0x01 + typedef struct { BOOLEAN HaveScannedDevice[MAX_IDE_DEVICE]; BOOLEAN DeviceFound[MAX_IDE_DEVICE]; @@ -67,6 +71,11 @@ typedef struct { UINT8 SenseDataNumber; UINT8 *Cache; + // + // ExitBootService Event, it is used to clear pending IDE interrupt + // + EFI_EVENT ExitBootServiceEvent; + EFI_UNICODE_STRING_TABLE *ControllerNameTable; } IDE_BLK_IO_DEV; diff --git a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idedata.h b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idedata.h index 55ae217d44..ec088dcfa6 100644 --- a/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idedata.h +++ b/EdkModulePkg/Bus/Pci/IdeBus/Dxe/idedata.h @@ -76,6 +76,16 @@ typedef enum { IdeUnknown } IDE_DEVICE_TYPE; +typedef enum { + SenseNoSenseKey, + SenseDeviceNotReadyNoRetry, + SenseDeviceNotReadyNeedRetry, + SenseNoMedia, + SenseMediaChange, + SenseMediaError, + SenseOtherSense +} SENSE_RESULT; + // // IDE Registers // @@ -234,6 +244,11 @@ typedef struct { // #define ATAPILONGTIMEOUT 5000 +// +// 10 seconds +// +#define ATASMARTTIMEOUT 10000 + // // ATA Commands Code // -- 2.39.2