From a33b996095f7c18eed1e9cf6053c17a115de3fca Mon Sep 17 00:00:00 2001
From: sos <sos@FreeBSD.org>
Date: Sat, 24 Jul 2004 19:03:28 +0000
Subject: [PATCH] Refine the wait for ATAPI_RESET. Properly wait for not busy
 and introduce a timeout for devices not setting busy (as they should). Leave
 a printf in there that states how long the wait was, as I'd like to get an
 idea of the variations here. The time needed seems also to be affected by
 whether a medium is present or not.

---
 sys/dev/ata/ata-lowlevel.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/sys/dev/ata/ata-lowlevel.c b/sys/dev/ata/ata-lowlevel.c
index e1635d7827d1..29a95ad2e5b6 100644
--- a/sys/dev/ata/ata-lowlevel.c
+++ b/sys/dev/ata/ata-lowlevel.c
@@ -110,8 +110,16 @@ ata_generic_transaction(struct ata_request *request)
 
 	    /* device reset doesn't interrupt */
 	    if (request->u.ata.command == ATA_ATAPI_RESET) {
-		DELAY(10);
-		request->status = ATA_IDX_INB(ch, ATA_STATUS);
+		int timeout = 1000000;
+		do {
+		    DELAY(10);
+		    request->status = ATA_IDX_INB(ch, ATA_STATUS);
+		} while (request->status & ATA_S_BUSY && timeout--);
+		if (timeout)
+		    printf("ATAPI_RESET time = %dus\n", (1000000-timeout)*10);
+		else
+		    printf("ATAPI_RESET timeout\n");
+
 		if (request->status & ATA_S_ERROR) {
 		    request->error = ATA_IDX_INB(ch, ATA_ERROR);
 		    //request->result = EIO;