]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c
1. Changed device detect method to use ATA/ATAPI device signature;
[mirror_edk2.git] / EdkModulePkg / Bus / Pci / IdeBus / Dxe / ata.c
index cb184a497df5daad7bd301599b10204c3cd9fdc1..a08c79cadee5d082a4346d253542760942e20175 100644 (file)
@@ -1,12 +1,12 @@
 /** @file\r
-  Copyright (c) 2006, Intel Corporation                                                         \r
-  All rights reserved. This program and the accompanying materials                          \r
-  are licensed and made available under the terms and conditions of the BSD License         \r
-  which accompanies this distribution.  The full text of the license may be found at        \r
-  http://opensource.org/licenses/bsd-license.php                                            \r
+  Copyright (c) 2006, Intel Corporation\r
+  All rights reserved. This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
 \r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
   @par Revision Reference:\r
   2002-6: Add Atapi6 enhancement, support >120GB hard disk, including\r
@@ -64,11 +64,11 @@ AtaPioDataOutExt (
   Sends out an ATA Identify Command to the specified device.\r
 \r
   This function is called by DiscoverIdeDevice() during its device\r
-  identification. It sends out the ATA Identify Command to the \r
-  specified device. Only ATA device responses to this command. If \r
-  the command succeeds, it returns the Identify data structure which \r
-  contains information about the device. This function extracts the \r
-  information it needs to fill the IDE_BLK_IO_DEV data structure, \r
+  identification. It sends out the ATA Identify Command to the\r
+  specified device. Only ATA device responses to this command. If\r
+  the command succeeds, it returns the Identify data structure which\r
+  contains information about the device. This function extracts the\r
+  information it needs to fill the IDE_BLK_IO_DEV data structure,\r
   including device type, media block size, media capacity, and etc.\r
 \r
   @param[in] *IdeDev\r
@@ -76,7 +76,7 @@ AtaPioDataOutExt (
   to record all the information of the IDE device.\r
 \r
   @retval EFI_SUCCESS Identify ATA device successfully.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or\r
   device is not ATA device.\r
 \r
@@ -203,13 +203,13 @@ ATAIdentify (
 \r
   @retval  EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one\r
   and 48-bit addressing must be used\r
-  \r
+\r
   @retval  EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but\r
   the capacity is below 120G, 48bit addressing is not\r
   needed\r
 \r
   @note\r
-  This function must be called after DEVICE_IDENTITY command has been \r
+  This function must be called after DEVICE_IDENTITY command has been\r
   successfully returned\r
 \r
 **/\r
@@ -274,7 +274,7 @@ AtaAtapi6Identify (
 \r
 /**\r
   This function is called by ATAIdentify() or ATAPIIdentify()\r
-  to print device's module name. \r
+  to print device's module name.\r
 \r
   @param[in] *IdeDev\r
   pointer pointing to IDE_BLK_IO_DEV data structure, used\r
@@ -295,7 +295,7 @@ PrintAtaModuleName (
 }\r
 \r
 /**\r
-  This function is used to send out ATA commands conforms to the \r
+  This function is used to send out ATA commands conforms to the\r
   PIO Data In Protocol.\r
 \r
   @param[in] *IdeDev\r
@@ -328,7 +328,7 @@ PrintAtaModuleName (
 \r
   @retval EFI_SUCCESS send out the ATA command and device send required\r
   data successfully.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR command sent failed.\r
 \r
 **/\r
@@ -462,7 +462,7 @@ AtaPioDataIn (
 }\r
 \r
 /**\r
-  This function is used to send out ATA commands conforms to the \r
+  This function is used to send out ATA commands conforms to the\r
   PIO Data Out Protocol.\r
 \r
   @param *IdeDev\r
@@ -480,7 +480,7 @@ AtaPioDataIn (
 \r
   @retval EFI_SUCCESS send out the ATA command and device received required\r
   data successfully.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR command sent failed.\r
 \r
 **/\r
@@ -571,7 +571,7 @@ AtaPioDataOut (
   WordCount = 0;\r
 \r
   while (WordCount < ByteCount / 2) {\r
-    \r
+\r
     //\r
     // DRQReady2-- read Alternate Status Register to determine the DRQ bit\r
     // data transfer can be performed only when DRQ is ready.\r
@@ -595,7 +595,7 @@ AtaPioDataOut (
     //\r
     // perform a series of write without check DRQ ready\r
     //\r
-    \r
+\r
     IDEWritePortWMultiple (\r
       IdeDev->PciIo,\r
       IdeDev->IoPort->Data,\r
@@ -613,7 +613,7 @@ AtaPioDataOut (
 }\r
 \r
 /**\r
-  This function is used to analyze the Status Register and print out \r
+  This function is used to analyze the Status Register and print out\r
   some debug information and if there is ERR bit set in the Status\r
   Register, the Error Register's value is also be parsed and print out.\r
 \r
@@ -729,10 +729,10 @@ CheckErrorStatus (
   to record all the information of the IDE device.\r
 \r
   @param[in] *DataBuffer\r
-  A pointer to the destination buffer for the data. \r
+  A pointer to the destination buffer for the data.\r
 \r
   @param[in] Lba\r
-  The starting logical block address to read from \r
+  The starting logical block address to read from\r
   on the device media.\r
 \r
   @param[in] NumberOfBlocks\r
@@ -770,7 +770,7 @@ AtaReadSectors (
   //\r
   AtaCommand      = READ_SECTORS_CMD;\r
 \r
-  \r
+\r
   BlocksRemaining = NumberOfBlocks;\r
 \r
   Lba32           = (UINT32) Lba;\r
@@ -778,7 +778,7 @@ AtaReadSectors (
   Status          = EFI_SUCCESS;\r
 \r
   while (BlocksRemaining > 0) {\r
-    \r
+\r
     //\r
     // in ATA-3 spec, LBA is in 28 bit width\r
     //\r
@@ -791,7 +791,7 @@ AtaReadSectors (
     Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);\r
 \r
     if (BlocksRemaining >= 0x100) {\r
-      \r
+\r
       //\r
       //  SectorCount8 is sent to Sector Count register, 0x00 means 256\r
       //  sectors to be read\r
@@ -847,10 +847,10 @@ AtaReadSectors (
   to record all the information of the IDE device.\r
 \r
   @param[in] *BufferData\r
-  A pointer to the source buffer for the data. \r
+  A pointer to the source buffer for the data.\r
 \r
   @param[in] Lba\r
-  The starting logical block address to write onto \r
+  The starting logical block address to write onto\r
   the device media.\r
 \r
   @param[in] NumberOfBlocks\r
@@ -902,7 +902,7 @@ AtaWriteSectors (
     Lba3  = (UINT8) ((Lba32 >> 24) & 0x0f);\r
 \r
     if (BlocksRemaining >= 0x100) {\r
-      \r
+\r
       //\r
       //  SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors\r
       //  to be written\r
@@ -945,8 +945,8 @@ AtaWriteSectors (
 \r
 /**\r
   This function is used to implement the Soft Reset on the specified\r
-  device. But, the ATA Soft Reset mechanism is so strong a reset method \r
-  that it will force resetting on both devices connected to the \r
+  device. But, the ATA Soft Reset mechanism is so strong a reset method\r
+  that it will force resetting on both devices connected to the\r
   same cable.\r
 \r
   It is called by IdeBlkIoReset(), a interface function of Block\r
@@ -988,6 +988,10 @@ AtaSoftReset (
 \r
   IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
 \r
+  //\r
+  // SRST should assert for at least 5 us, we use 10 us for\r
+  // better compatibility\r
+  //\r
   gBS->Stall (10);\r
 \r
   //\r
@@ -996,6 +1000,11 @@ AtaSoftReset (
   DeviceControl = 0;\r
   IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
 \r
+  //\r
+  // Wait for at least 2 ms to check BSY status, we use 10 ms\r
+  // for better compatibility\r
+  //\r
+  gBS->Stall(10000);\r
   //\r
   // slave device needs at most 31s to clear BSY\r
   //\r
@@ -1017,7 +1026,7 @@ AtaSoftReset (
   The media id that the read request is for.\r
 \r
   @param[in] LBA\r
-  The starting logical block address to read from \r
+  The starting logical block address to read from\r
   on the device.\r
 \r
   @param[in] BufferSize\r
@@ -1025,7 +1034,7 @@ AtaSoftReset (
   multiple of the intrinsic block size of the device.\r
 \r
   @param[out] *Buffer\r
-  A pointer to the destination buffer for the data. \r
+  A pointer to the destination buffer for the data.\r
   The caller is responsible for either having implicit\r
   or explicit ownership of the memory that data is read into.\r
 \r
@@ -1033,11 +1042,11 @@ AtaSoftReset (
   @retval EFI_DEVICE_ERROR  Read Blocks failed.\r
   @retval EFI_NO_MEDIA      There is no media in the device.\r
   @retval EFI_MEDIA_CHANGE  The MediaId is not for the current media.\r
-  \r
+\r
   @retval EFI_BAD_BUFFER_SIZE\r
   The BufferSize parameter is not a multiple of the\r
   intrinsic block size of the device.\r
-  \r
+\r
   @retval EFI_INVALID_PARAMETER\r
   The read request contains LBAs that are not valid,\r
   or the data buffer is not valid.\r
@@ -1114,13 +1123,9 @@ AtaBlkIoReadBlocks (
   } else {\r
     //\r
     // For ATA-3 compatible device, use ATA-3 read block mechanism\r
-    // Notice DMA operation can only handle 32bit address\r
     //\r
-    if ((UINTN) Buffer <= 0xFFFFFFFF) {\r
-      Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);\r
-    }\r
-\r
-    if (EFI_ERROR (Status) || ((UINTN) Buffer > 0xFFFFFFFF)) {\r
+    Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);\r
+    if (EFI_ERROR (Status)) {\r
       Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);\r
     }\r
   }\r
@@ -1145,7 +1150,7 @@ AtaBlkIoReadBlocks (
   The media id that the write request is for.\r
 \r
   @param[in] LBA\r
-  The starting logical block address to write onto \r
+  The starting logical block address to write onto\r
   the device.\r
 \r
   @param[in] BufferSize\r
@@ -1153,20 +1158,20 @@ AtaBlkIoReadBlocks (
   multiple of the intrinsic block size of the device.\r
 \r
   @param[out] *Buffer\r
-  A pointer to the source buffer for the data. \r
+  A pointer to the source buffer for the data.\r
   The caller is responsible for either having implicit\r
-  or explicit ownership of the memory that data is \r
+  or explicit ownership of the memory that data is\r
   written from.\r
 \r
   @retval EFI_SUCCESS       Write Blocks successfully.\r
   @retval EFI_DEVICE_ERROR  Write Blocks failed.\r
   @retval EFI_NO_MEDIA      There is no media in the device.\r
   @retval EFI_MEDIA_CHANGE  The MediaId is not for the current media.\r
-  \r
+\r
   @retval EFI_BAD_BUFFER_SIZE\r
   The BufferSize parameter is not a multiple of the\r
   intrinsic block size of the device.\r
-  \r
+\r
   @retval EFI_INVALID_PARAMETER\r
   The write request contains LBAs that are not valid,\r
   or the data buffer is not valid.\r
@@ -1241,7 +1246,7 @@ AtaBlkIoWriteBlocks (
     // For ATA-3 compatible device, use ATA-3 write block mechanism\r
     //\r
     Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);\r
-    if (EFI_ERROR (Status) || ((UINTN) Buffer > 0xFFFFFFFF)) {\r
+    if (EFI_ERROR (Status)) {\r
       Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);\r
     }\r
   }\r
@@ -1256,15 +1261,15 @@ AtaBlkIoWriteBlocks (
 \r
 /**\r
   This function is called by the AtaBlkIoReadBlocks() to perform\r
-  reading from media in block unit. The function has been enhanced to \r
+  reading from media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev\r
   pointer pointing to IDE_BLK_IO_DEV data structure, used\r
   to record all the information of the IDE device.\r
 \r
-  @param[in] *DataBuffer    A pointer to the destination buffer for the data. \r
-  @param[in] StartLba       The starting logical block address to read from \r
+  @param[in] *DataBuffer    A pointer to the destination buffer for the data.\r
+  @param[in] StartLba       The starting logical block address to read from\r
   on the device media.\r
   @param[in] NumberOfBlocks The number of transfer data blocks.\r
 \r
@@ -1339,7 +1344,7 @@ AtaReadSectorsExt (
 \r
 /**\r
   This function is called by the AtaBlkIoWriteBlocks() to perform\r
-  writing onto media in block unit. The function has been enhanced to \r
+  writing onto media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev\r
@@ -1347,10 +1352,10 @@ AtaReadSectorsExt (
   to record all the information of the IDE device.\r
 \r
   @param[in] *DataBuffer\r
-  A pointer to the source buffer for the data. \r
+  A pointer to the source buffer for the data.\r
 \r
   @param[in] Lba\r
-  The starting logical block address to write onto \r
+  The starting logical block address to write onto\r
   the device media.\r
 \r
   @param[in] NumberOfBlocks\r
@@ -1427,14 +1432,14 @@ AtaWriteSectorsExt (
 }\r
 \r
 /**\r
-  This function is used to send out ATA commands conforms to the \r
+  This function is used to send out ATA commands conforms to the\r
   PIO Data In Protocol, supporting ATA/ATAPI-6 standard\r
 \r
   Comparing with ATA-3 data in protocol, we have two differents here:<BR>\r
   1. Do NOT wait for DRQ clear before sending command into IDE device.(the\r
   wait will frequently fail... cause writing function return error)\r
 \r
-  2. Do NOT wait for DRQ clear after all data readed.(the wait greatly \r
+  2. Do NOT wait for DRQ clear after all data readed.(the wait greatly\r
   slow down writing performance by 100 times!)\r
 \r
   @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used\r
@@ -1448,7 +1453,7 @@ AtaWriteSectorsExt (
 \r
   @retval EFI_SUCCESS send out the ATA command and device send required\r
   data successfully.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR command sent failed.\r
 \r
 **/\r
@@ -1544,7 +1549,7 @@ AtaPioDataInExt (
   // According to PIO data in protocol, host can perform a series of reads to\r
   // the data register after each time device set DRQ ready;\r
   //\r
-  \r
+\r
   //\r
   // 256 words\r
   //\r
@@ -1592,14 +1597,14 @@ AtaPioDataInExt (
 }\r
 \r
 /**\r
-  This function is used to send out ATA commands conforms to the \r
+  This function is used to send out ATA commands conforms to the\r
   PIO Data Out Protocol, supporting ATA/ATAPI-6 standard\r
 \r
   Comparing with ATA-3 data out protocol, we have two differents here:<BR>\r
   1. Do NOT wait for DRQ clear before sending command into IDE device.(the\r
   wait will frequently fail... cause writing function return error)\r
 \r
-  2. Do NOT wait for DRQ clear after all data readed.(the wait greatly \r
+  2. Do NOT wait for DRQ clear after all data readed.(the wait greatly\r
   slow down writing performance by 100 times!)\r
 \r
   @param[in] *IdeDev\r
@@ -1614,7 +1619,7 @@ AtaPioDataInExt (
 \r
   @retval EFI_SUCCESS send out the ATA command and device receive required\r
   data successfully.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR command sent failed.\r
 \r
 **/\r
@@ -1661,7 +1666,7 @@ AtaPioDataOutExt (
   if (EFI_ERROR (Status)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
-     \r
+\r
   //\r
   // Fill feature register if needed\r
   //\r
@@ -2090,7 +2095,7 @@ AtaCommandIssue (
   //\r
   // Fill the start LBA registers, which are also two-byte FIFO\r
   //\r
-  \r
+\r
   IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0);\r
   IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1);\r
   IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2);\r
@@ -2110,15 +2115,15 @@ AtaCommandIssue (
 \r
 /**\r
   This function is called by the AtaBlkIoReadBlocks() to perform\r
-  reading from media in block unit. The function has been enhanced to \r
+  reading from media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used\r
   to record all the information of the IDE device.\r
 \r
-  @param[in] *DataBuffer A pointer to the destination buffer for the data. \r
+  @param[in] *DataBuffer A pointer to the destination buffer for the data.\r
 \r
-  @param[in] StartLba The starting logical block address to read from \r
+  @param[in] StartLba The starting logical block address to read from\r
   on the device media.\r
 \r
   @param[in] NumberOfBlocks The number of transfer data blocks.\r
@@ -2139,21 +2144,26 @@ AtaUdmaReadExt (
   IN  UINTN           NumberOfBlocks\r
   )\r
 {\r
-  IDE_DMA_PRD *PrdAddr;\r
-  IDE_DMA_PRD *UsedPrdAddr;\r
-  IDE_DMA_PRD *TempPrdAddr;\r
-  UINT8       RegisterValue;\r
-  UINT8       Device;\r
-  UINT64      IoPortForBmic;\r
-  UINT64      IoPortForBmis;\r
-  UINT64      IoPortForBmid;\r
-  EFI_STATUS  Status;\r
-  UINTN       PrdTableNum;\r
-  UINTN       ByteCount;\r
-  UINTN       ByteAvailable;\r
-  UINT8       *PrdBuffer;\r
-  UINTN       RemainBlockNum;\r
-  UINT8       DeviceControl;\r
+  IDE_DMA_PRD                *PrdAddr;\r
+  IDE_DMA_PRD                *UsedPrdAddr;\r
+  IDE_DMA_PRD                *TempPrdAddr;\r
+  UINT8                      RegisterValue;\r
+  UINT8                      Device;\r
+  UINT64                     IoPortForBmic;\r
+  UINT64                     IoPortForBmis;\r
+  UINT64                     IoPortForBmid;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      PrdTableNum;\r
+  UINTN                      ByteCount;\r
+  UINTN                      ByteAvailable;\r
+  UINT8                      *PrdBuffer;\r
+  UINTN                      RemainBlockNum;\r
+  UINT8                      DeviceControl;\r
+  UINT32                     Count;\r
+  UINTN                      PageCount;\r
+  VOID                       *Map;\r
+  EFI_PHYSICAL_ADDRESS       MemPage;\r
+  EFI_PHYSICAL_ADDRESS       DeviceAddress;\r
 \r
   //\r
   // Channel and device differential. Select device.\r
@@ -2207,7 +2217,20 @@ AtaUdmaReadExt (
     //\r
     // Build PRD table\r
     //\r
-    PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD)));\r
+    MemPage = 0xFFFFFFFF;\r
+    PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));\r
+    Status = gBS->AllocatePages (\r
+                    AllocateMaxAddress,\r
+                    EfiBootServicesData,\r
+                    PageCount,\r
+                    &MemPage\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));\r
+\r
+    PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);\r
 \r
     //\r
     // To make sure PRD is allocated in one 64K page\r
@@ -2225,7 +2248,19 @@ AtaUdmaReadExt (
     //\r
     // Build the PRD table\r
     //\r
-    PrdBuffer   = DataBuffer;\r
+    Status = IdeDev->PciIo->Map (\r
+                       IdeDev->PciIo,\r
+                       EfiPciIoOperationBusMasterWrite,\r
+                       DataBuffer,\r
+                       &ByteCount,\r
+                       &DeviceAddress,\r
+                       &Map\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePages (MemPage, PageCount);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    PrdBuffer   = (VOID *) ((UINTN) DeviceAddress);\r
     TempPrdAddr = UsedPrdAddr;\r
     while (TRUE) {\r
 \r
@@ -2245,7 +2280,7 @@ AtaUdmaReadExt (
       PrdBuffer += ByteAvailable;\r
       TempPrdAddr++;\r
     }\r
-  \r
+\r
     //\r
     // Set the base address to BMID register\r
     //\r
@@ -2308,15 +2343,16 @@ AtaUdmaReadExt (
     // Issue READ DMA EXT command\r
     //\r
     Status = AtaCommandIssueExt (\r
-              IdeDev,\r
-              READ_DMA_EXT_CMD,\r
-              Device,\r
-              0,\r
-              (UINT16) NumberOfBlocks,\r
-              StartLba\r
-              );\r
+               IdeDev,\r
+               READ_DMA_EXT_CMD,\r
+               Device,\r
+               0,\r
+               (UINT16) NumberOfBlocks,\r
+               StartLba\r
+               );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (PrdAddr);\r
+      gBS->FreePages (MemPage, PageCount);\r
+      IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
       return EFI_DEVICE_ERROR;\r
     }\r
 \r
@@ -2345,7 +2381,11 @@ AtaUdmaReadExt (
 \r
     //\r
     // Check the INTERRUPT and ERROR bit of BMIS\r
+    // Max transfer number of sectors for one command is 65536(32Mbyte),\r
+    // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).\r
+    // So set the variable Count to 2000, for about 2 second timeout time.\r
     //\r
+    Count = 2000;\r
     while (TRUE) {\r
 \r
       IdeDev->PciIo->Io.Read (\r
@@ -2356,21 +2396,49 @@ AtaUdmaReadExt (
                           1,\r
                           &RegisterValue\r
                           );\r
-      if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) {\r
-        if (RegisterValue & BMIS_ERROR) {\r
-          gBS->FreePool (PrdAddr);\r
+      if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {\r
+        if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {\r
+          //\r
+          // Clear START bit of BMIC register before return EFI_DEVICE_ERROR\r
+          //\r
+          IdeDev->PciIo->Io.Read (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+\r
+          RegisterValue &= ~((UINT8)BMIC_START);\r
+\r
+          IdeDev->PciIo->Io.Write (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+          gBS->FreePages (MemPage, PageCount);\r
+          IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
           return EFI_DEVICE_ERROR;\r
         }\r
         break;\r
       }\r
 \r
       gBS->Stall (1000);\r
+      Count --;\r
     }\r
 \r
-    gBS->FreePool (PrdAddr);\r
-\r
+    gBS->FreePages (MemPage, PageCount);\r
+    IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
     //\r
-    // Set START bit of BMIC register\r
+    // Read Status Register of IDE device to clear interrupt\r
+    //\r
+    RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);\r
+    //\r
+    // Clear START bit of BMIC register\r
     //\r
     IdeDev->PciIo->Io.Read (\r
                         IdeDev->PciIo,\r
@@ -2400,20 +2468,27 @@ AtaUdmaReadExt (
     StartLba += NumberOfBlocks;\r
   }\r
 \r
+  //\r
+  // Disable interrupt of Select device\r
+  //\r
+  IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);\r
+  DeviceControl |= IEN_L;\r
+  IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   This function is called by the AtaBlkIoReadBlocks() to perform\r
-  reading from media in block unit. The function has been enhanced to \r
+  reading from media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev\r
   pointer pointing to IDE_BLK_IO_DEV data structure, used\r
   to record all the information of the IDE device.\r
 \r
-  @param[in] *DataBuffer    A pointer to the destination buffer for the data. \r
-  @param[in] StartLba       The starting logical block address to read from \r
+  @param[in] *DataBuffer    A pointer to the destination buffer for the data.\r
+  @param[in] StartLba       The starting logical block address to read from\r
   on the device media.\r
   @param[in] NumberOfBlocks The number of transfer data blocks.\r
 \r
@@ -2433,21 +2508,26 @@ AtaUdmaRead (
   IN  UINTN           NumberOfBlocks\r
   )\r
 {\r
-  IDE_DMA_PRD *PrdAddr;\r
-  IDE_DMA_PRD *UsedPrdAddr;\r
-  IDE_DMA_PRD *TempPrdAddr;\r
-  UINT8       RegisterValue;\r
-  UINT8       Device;\r
-  UINT64      IoPortForBmic;\r
-  UINT64      IoPortForBmis;\r
-  UINT64      IoPortForBmid;\r
-  EFI_STATUS  Status;\r
-  UINTN       PrdTableNum;\r
-  UINTN       ByteCount;\r
-  UINTN       ByteAvailable;\r
-  UINT8       *PrdBuffer;\r
-  UINTN       RemainBlockNum;\r
-  UINT8       DeviceControl;\r
+  IDE_DMA_PRD                *PrdAddr;\r
+  IDE_DMA_PRD                *UsedPrdAddr;\r
+  IDE_DMA_PRD                *TempPrdAddr;\r
+  UINT8                      RegisterValue;\r
+  UINT8                      Device;\r
+  UINT64                     IoPortForBmic;\r
+  UINT64                     IoPortForBmis;\r
+  UINT64                     IoPortForBmid;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      PrdTableNum;\r
+  UINTN                      ByteCount;\r
+  UINTN                      ByteAvailable;\r
+  UINT8                      *PrdBuffer;\r
+  UINTN                      RemainBlockNum;\r
+  UINT8                      DeviceControl;\r
+  UINT32                     Count;\r
+  UINTN                      PageCount;\r
+  VOID                       *Map;\r
+  EFI_PHYSICAL_ADDRESS       MemPage;\r
+  EFI_PHYSICAL_ADDRESS       DeviceAddress;\r
 \r
   //\r
   // Channel and device differential\r
@@ -2501,7 +2581,20 @@ AtaUdmaRead (
     //\r
     // Build PRD table\r
     //\r
-    PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD)));\r
+    MemPage = 0xFFFFFFFF;\r
+    PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));\r
+    Status = gBS->AllocatePages (\r
+                    AllocateMaxAddress,\r
+                    EfiBootServicesData,\r
+                    PageCount,\r
+                    &MemPage\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));\r
+\r
+    PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);\r
     //\r
     // To make sure PRD is allocated in one 64K page\r
     //\r
@@ -2518,7 +2611,19 @@ AtaUdmaRead (
     //\r
     // Build the PRD table\r
     //\r
-    PrdBuffer   = DataBuffer;\r
+    Status = IdeDev->PciIo->Map (\r
+                       IdeDev->PciIo,\r
+                       EfiPciIoOperationBusMasterWrite,\r
+                       DataBuffer,\r
+                       &ByteCount,\r
+                       &DeviceAddress,\r
+                       &Map\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePages (MemPage, PageCount);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    PrdBuffer   = (UINT8 *) ((UINTN) DeviceAddress);\r
     TempPrdAddr = UsedPrdAddr;\r
     while (TRUE) {\r
 \r
@@ -2601,15 +2706,16 @@ AtaUdmaRead (
     // Issue READ DMA command\r
     //\r
     Status = AtaCommandIssue (\r
-              IdeDev,\r
-              READ_DMA_CMD,\r
-              Device,\r
-              0,\r
-              (UINT16) NumberOfBlocks,\r
-              StartLba\r
-              );\r
+               IdeDev,\r
+               READ_DMA_CMD,\r
+               Device,\r
+               0,\r
+               (UINT16) NumberOfBlocks,\r
+               StartLba\r
+               );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (PrdAddr);\r
+      gBS->FreePages (MemPage, PageCount);\r
+      IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
       return EFI_DEVICE_ERROR;\r
     }\r
 \r
@@ -2638,7 +2744,11 @@ AtaUdmaRead (
 \r
     //\r
     // Check the INTERRUPT and ERROR bit of BMIS\r
+    // Max transfer number of sectors for one command is 65536(32Mbyte),\r
+    // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).\r
+    // So set the variable Count to 2000, for about 2 second timeout time.\r
     //\r
+    Count = 2000;\r
     while (TRUE) {\r
 \r
       IdeDev->PciIo->Io.Read (\r
@@ -2649,21 +2759,49 @@ AtaUdmaRead (
                           1,\r
                           &RegisterValue\r
                           );\r
-      if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) {\r
-        if (RegisterValue & BMIS_ERROR) {\r
-          gBS->FreePool (PrdAddr);\r
+      if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {\r
+        if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {\r
+          //\r
+          // Clear START bit of BMIC register before return EFI_DEVICE_ERROR\r
+          //\r
+          IdeDev->PciIo->Io.Read (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+\r
+          RegisterValue &= ~((UINT8)BMIC_START);\r
+\r
+          IdeDev->PciIo->Io.Write (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+          gBS->FreePages (MemPage, PageCount);\r
+          IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
           return EFI_DEVICE_ERROR;\r
         }\r
         break;\r
       }\r
 \r
       gBS->Stall (1000);\r
+      Count --;\r
     }\r
 \r
-    gBS->FreePool (PrdAddr);\r
-\r
+    gBS->FreePages (MemPage, PageCount);\r
+    IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
     //\r
-    // Set START bit of BMIC register\r
+    // Read Status Register of IDE device to clear interrupt\r
+    //\r
+    RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);\r
+    //\r
+    // Clear START bit of BMIC register\r
     //\r
     IdeDev->PciIo->Io.Read (\r
                         IdeDev->PciIo,\r
@@ -2693,20 +2831,27 @@ AtaUdmaRead (
     StartLba += NumberOfBlocks;\r
   }\r
 \r
+  //\r
+  // Disable interrupt of Select device\r
+  //\r
+  IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);\r
+  DeviceControl |= IEN_L;\r
+  IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   This function is called by the AtaBlkIoWriteBlocks() to perform\r
-  writing to media in block unit. The function has been enhanced to \r
+  writing to media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used\r
   to record all the information of the IDE device.\r
 \r
-  @param[in] *DataBuffer A pointer to the source buffer for the data. \r
+  @param[in] *DataBuffer A pointer to the source buffer for the data.\r
 \r
-  @param[in] StartLba The starting logical block address to write to \r
+  @param[in] StartLba The starting logical block address to write to\r
   on the device media.\r
 \r
   @param[in] NumberOfBlocks The number of transfer data blocks.\r
@@ -2726,21 +2871,26 @@ AtaUdmaWriteExt (
   IN  UINTN           NumberOfBlocks\r
   )\r
 {\r
-  IDE_DMA_PRD *PrdAddr;\r
-  IDE_DMA_PRD *UsedPrdAddr;\r
-  IDE_DMA_PRD *TempPrdAddr;\r
-  UINT8       RegisterValue;\r
-  UINT8       Device;\r
-  UINT64      IoPortForBmic;\r
-  UINT64      IoPortForBmis;\r
-  UINT64      IoPortForBmid;\r
-  EFI_STATUS  Status;\r
-  UINTN       PrdTableNum;\r
-  UINTN       ByteCount;\r
-  UINTN       ByteAvailable;\r
-  UINT8       *PrdBuffer;\r
-  UINTN       RemainBlockNum;\r
-  UINT8       DeviceControl;\r
+  IDE_DMA_PRD                *PrdAddr;\r
+  IDE_DMA_PRD                *UsedPrdAddr;\r
+  IDE_DMA_PRD                *TempPrdAddr;\r
+  UINT8                      RegisterValue;\r
+  UINT8                      Device;\r
+  UINT64                     IoPortForBmic;\r
+  UINT64                     IoPortForBmis;\r
+  UINT64                     IoPortForBmid;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      PrdTableNum;\r
+  UINTN                      ByteCount;\r
+  UINTN                      ByteAvailable;\r
+  UINT8                      *PrdBuffer;\r
+  UINTN                      RemainBlockNum;\r
+  UINT8                      DeviceControl;\r
+  UINT32                     Count;\r
+  UINTN                      PageCount;\r
+  VOID                       *Map;\r
+  EFI_PHYSICAL_ADDRESS       MemPage;\r
+  EFI_PHYSICAL_ADDRESS       DeviceAddress;\r
 \r
   //\r
   // Channel and device differential\r
@@ -2794,7 +2944,20 @@ AtaUdmaWriteExt (
     //\r
     // Build PRD table\r
     //\r
-    PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD)));\r
+    MemPage = 0xFFFFFFFF;\r
+    PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));\r
+    Status = gBS->AllocatePages (\r
+                    AllocateMaxAddress,\r
+                    EfiBootServicesData,\r
+                    PageCount,\r
+                    &MemPage\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));\r
+\r
+    PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);\r
     //\r
     // To make sure PRD is allocated in one 64K page\r
     //\r
@@ -2811,7 +2974,19 @@ AtaUdmaWriteExt (
     //\r
     // Build the PRD table\r
     //\r
-    PrdBuffer   = DataBuffer;\r
+    Status = IdeDev->PciIo->Map (\r
+                       IdeDev->PciIo,\r
+                       EfiPciIoOperationBusMasterRead,\r
+                       DataBuffer,\r
+                       &ByteCount,\r
+                       &DeviceAddress,\r
+                       &Map\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePages (MemPage, PageCount);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    PrdBuffer   = (UINT8 *) ((UINTN) DeviceAddress);\r
     TempPrdAddr = UsedPrdAddr;\r
     while (TRUE) {\r
 \r
@@ -2831,7 +3006,7 @@ AtaUdmaWriteExt (
       PrdBuffer += ByteAvailable;\r
       TempPrdAddr++;\r
     }\r
-  \r
+\r
     //\r
     // Set the base address to BMID register\r
     //\r
@@ -2896,15 +3071,16 @@ AtaUdmaWriteExt (
     // Issue WRITE DMA EXT command\r
     //\r
     Status = AtaCommandIssueExt (\r
-              IdeDev,\r
-              WRITE_DMA_EXT_CMD,\r
-              Device,\r
-              0,\r
-              (UINT16) NumberOfBlocks,\r
-              StartLba\r
-              );\r
+               IdeDev,\r
+               WRITE_DMA_EXT_CMD,\r
+               Device,\r
+               0,\r
+               (UINT16) NumberOfBlocks,\r
+               StartLba\r
+               );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (PrdAddr);\r
+      gBS->FreePages (MemPage, PageCount);\r
+      IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
       return EFI_DEVICE_ERROR;\r
     }\r
 \r
@@ -2933,7 +3109,11 @@ AtaUdmaWriteExt (
 \r
     //\r
     // Check the INTERRUPT and ERROR bit of BMIS\r
+    // Max transfer number of sectors for one command is 65536(32Mbyte),\r
+    // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).\r
+    // So set the variable Count to 2000, for about 2 second timeout time.\r
     //\r
+    Count = 2000;\r
     while (TRUE) {\r
 \r
       IdeDev->PciIo->Io.Read (\r
@@ -2944,21 +3124,49 @@ AtaUdmaWriteExt (
                           1,\r
                           &RegisterValue\r
                           );\r
-      if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) {\r
-        if (RegisterValue & BMIS_ERROR) {\r
-          gBS->FreePool (PrdAddr);\r
+      if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {\r
+        if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {\r
+          //\r
+          // Clear START bit of BMIC register before return EFI_DEVICE_ERROR\r
+          //\r
+          IdeDev->PciIo->Io.Read (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+\r
+          RegisterValue &= ~((UINT8)BMIC_START);\r
+\r
+          IdeDev->PciIo->Io.Write (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+          gBS->FreePages (MemPage, PageCount);\r
+          IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
           return EFI_DEVICE_ERROR;\r
         }\r
         break;\r
       }\r
 \r
       gBS->Stall (1000);\r
+      Count --;\r
     }\r
 \r
-    gBS->FreePool (PrdAddr);\r
-\r
+    gBS->FreePages (MemPage, PageCount);\r
+    IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
     //\r
-    // Set START bit of BMIC register\r
+    // Read Status Register of IDE device to clear interrupt\r
+    //\r
+    RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);\r
+    //\r
+    // Clear START bit of BMIC register\r
     //\r
     IdeDev->PciIo->Io.Read (\r
                         IdeDev->PciIo,\r
@@ -2984,12 +3192,19 @@ AtaUdmaWriteExt (
     StartLba += NumberOfBlocks;\r
   }\r
 \r
+  //\r
+  // Disable interrupt of Select device\r
+  //\r
+  IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);\r
+  DeviceControl |= IEN_L;\r
+  IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   This function is called by the AtaBlkIoWriteBlocks() to perform\r
-  writing to media in block unit. The function has been enhanced to \r
+  writing to media in block unit. The function has been enhanced to\r
   support >120GB access and transfer at most 65536 blocks per command\r
 \r
   @param[in] *IdeDev\r
@@ -2997,10 +3212,10 @@ AtaUdmaWriteExt (
   to record all the information of the IDE device.\r
 \r
   @param[in] *DataBuffer\r
-  A pointer to the source buffer for the data. \r
+  A pointer to the source buffer for the data.\r
 \r
   @param[in] StartLba\r
-  The starting logical block address to write to \r
+  The starting logical block address to write to\r
   on the device media.\r
 \r
   @param[in] NumberOfBlocks\r
@@ -3021,21 +3236,26 @@ AtaUdmaWrite (
   IN  UINTN           NumberOfBlocks\r
   )\r
 {\r
-  IDE_DMA_PRD *PrdAddr;\r
-  IDE_DMA_PRD *UsedPrdAddr;\r
-  IDE_DMA_PRD *TempPrdAddr;\r
-  UINT8       RegisterValue;\r
-  UINT8       Device;\r
-  UINT64      IoPortForBmic;\r
-  UINT64      IoPortForBmis;\r
-  UINT64      IoPortForBmid;\r
-  EFI_STATUS  Status;\r
-  UINTN       PrdTableNum;\r
-  UINTN       ByteCount;\r
-  UINTN       ByteAvailable;\r
-  UINT8       *PrdBuffer;\r
-  UINTN       RemainBlockNum;\r
-  UINT8       DeviceControl;\r
+  IDE_DMA_PRD                *PrdAddr;\r
+  IDE_DMA_PRD                *UsedPrdAddr;\r
+  IDE_DMA_PRD                *TempPrdAddr;\r
+  UINT8                      RegisterValue;\r
+  UINT8                      Device;\r
+  UINT64                     IoPortForBmic;\r
+  UINT64                     IoPortForBmis;\r
+  UINT64                     IoPortForBmid;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      PrdTableNum;\r
+  UINTN                      ByteCount;\r
+  UINTN                      ByteAvailable;\r
+  UINT8                      *PrdBuffer;\r
+  UINTN                      RemainBlockNum;\r
+  UINT8                      DeviceControl;\r
+  UINT32                     Count;\r
+  UINTN                      PageCount;\r
+  VOID                       *Map;\r
+  EFI_PHYSICAL_ADDRESS       MemPage;\r
+  EFI_PHYSICAL_ADDRESS       DeviceAddress;\r
 \r
   //\r
   // Channel and device differential\r
@@ -3089,7 +3309,20 @@ AtaUdmaWrite (
     //\r
     // Build PRD table\r
     //\r
-    PrdAddr = (IDE_DMA_PRD *) AllocateZeroPool ((2 * PrdTableNum * sizeof (IDE_DMA_PRD)));\r
+    MemPage = 0xFFFFFFFF;\r
+    PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));\r
+    Status = gBS->AllocatePages (\r
+                    AllocateMaxAddress,\r
+                    EfiBootServicesData,\r
+                    PageCount,\r
+                    &MemPage\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));\r
+\r
+    PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);\r
 \r
     //\r
     // To make sure PRD is allocated in one 64K page\r
@@ -3107,7 +3340,19 @@ AtaUdmaWrite (
     //\r
     // Build the PRD table\r
     //\r
-    PrdBuffer   = DataBuffer;\r
+    Status = IdeDev->PciIo->Map (\r
+                       IdeDev->PciIo,\r
+                       EfiPciIoOperationBusMasterRead,\r
+                       DataBuffer,\r
+                       &ByteCount,\r
+                       &DeviceAddress,\r
+                       &Map\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePages (MemPage, PageCount);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    PrdBuffer   = (UINT8 *) ((UINTN) DeviceAddress);\r
     TempPrdAddr = UsedPrdAddr;\r
     while (TRUE) {\r
 \r
@@ -3127,7 +3372,7 @@ AtaUdmaWrite (
       PrdBuffer += ByteAvailable;\r
       TempPrdAddr++;\r
     }\r
-  \r
+\r
     //\r
     // Set the base address to BMID register\r
     //\r
@@ -3192,15 +3437,16 @@ AtaUdmaWrite (
     // Issue WRITE DMA command\r
     //\r
     Status = AtaCommandIssue (\r
-              IdeDev,\r
-              WRITE_DMA_CMD,\r
-              Device,\r
-              0,\r
-              (UINT16) NumberOfBlocks,\r
-              StartLba\r
-              );\r
+               IdeDev,\r
+               WRITE_DMA_CMD,\r
+               Device,\r
+               0,\r
+               (UINT16) NumberOfBlocks,\r
+               StartLba\r
+               );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (PrdAddr);\r
+      gBS->FreePages (MemPage, PageCount);\r
+      IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
       return EFI_DEVICE_ERROR;\r
     }\r
 \r
@@ -3229,7 +3475,11 @@ AtaUdmaWrite (
 \r
     //\r
     // Check the INTERRUPT and ERROR bit of BMIS\r
+    // Max transfer number of sectors for one command is 65536(32Mbyte),\r
+    // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).\r
+    // So set the variable Count to 2000, for about 2 second timeout time.\r
     //\r
+    Count = 2000;\r
     while (TRUE) {\r
 \r
       IdeDev->PciIo->Io.Read (\r
@@ -3240,21 +3490,50 @@ AtaUdmaWrite (
                           1,\r
                           &RegisterValue\r
                           );\r
-      if (RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) {\r
-        if (RegisterValue & BMIS_ERROR) {\r
-          gBS->FreePool (PrdAddr);\r
+      if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {\r
+        if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {\r
+          //\r
+          // Clear START bit of BMIC register before return EFI_DEVICE_ERROR\r
+          //\r
+          IdeDev->PciIo->Io.Read (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+\r
+          RegisterValue &= ~((UINT8)BMIC_START);\r
+\r
+          IdeDev->PciIo->Io.Write (\r
+                              IdeDev->PciIo,\r
+                              EfiPciIoWidthUint8,\r
+                              EFI_PCI_IO_PASS_THROUGH_BAR,\r
+                              IoPortForBmic,\r
+                              1,\r
+                              &RegisterValue\r
+                              );\r
+          gBS->FreePages (MemPage, PageCount);\r
+          IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
           return EFI_DEVICE_ERROR;\r
         }\r
         break;\r
       }\r
 \r
       gBS->Stall (1000);\r
+      Count --;\r
     }\r
 \r
-    gBS->FreePool (PrdAddr);\r
+    gBS->FreePages (MemPage, PageCount);\r
+    IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);\r
 \r
     //\r
-    // Set START bit of BMIC register\r
+    // Read Status Register of IDE device to clear interrupt\r
+    //\r
+    RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);\r
+    //\r
+    // Clear START bit of BMIC register\r
     //\r
     IdeDev->PciIo->Io.Read (\r
                         IdeDev->PciIo,\r
@@ -3280,5 +3559,12 @@ AtaUdmaWrite (
     StartLba += NumberOfBlocks;\r
   }\r
 \r
+  //\r
+  // Disable interrupt of Select device\r
+  //\r
+  IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);\r
+  DeviceControl |= IEN_L;\r
+  IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);\r
+\r
   return EFI_SUCCESS;\r
 }\r