/* This function is used to poll for the DRQ bit clear in the Status Register. DRQ is cleared when the device is finished transferring data. So this function is called after data transfer is finished. */
EFI_STATUS EFIAPI DRQClear(IN EFI_PCI_IO_PROTOCOL *PciIo, IN EFI_IDE_REGISTERS *IdeRegisters, IN UINT64 Timeout)
/* This function is used to poll for the DRQ bit clear in the Status Register. DRQ is cleared when the device is finished transferring data. So this function is called after data transfer is finished. */
EFI_STATUS EFIAPI DRQClear(IN EFI_PCI_IO_PROTOCOL *PciIo, IN EFI_IDE_REGISTERS *IdeRegisters, IN UINT64 Timeout)
{
  UINT64   Delay;
  UINT8    StatusRegister;
  BOOLEAN  InfiniteWait;
  ASSERT (PciIo != NULL);
  ASSERT (IdeRegisters != NULL);
  if (Timeout == 0) {
    InfiniteWait = TRUE;
  } else {
    InfiniteWait = FALSE;
  }
  Delay = DivU64x32 (Timeout, 1000) + 1;
  do {
    StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus);
    if ((StatusRegister & ATA_STSREG_BSY) == 0) {
      if ((StatusRegister & ATA_STSREG_DRQ) == ATA_STSREG_DRQ) {
        return EFI_DEVICE_ERROR;
      } else {
        return EFI_SUCCESS;
      }
    }
    MicroSecondDelay (100);
    Delay--;
  } while (InfiniteWait || (Delay > 0));
  return EFI_TIMEOUT;
}