[PATCH] drivers/message/fusion/*: fix polling loops

From: Marcin Slusarz <marcin.slusarz@gmail.com>

Signed-off-by: Marcin Slusarz <marcin.slusarz@gmail.com>
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>

Index: linux-kj/drivers/message/fusion/mptbase.c
===================================================================
--- linux-kj.orig/drivers/message/fusion/mptbase.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/message/fusion/mptbase.c	2006-01-18 16:45:59.000000000 +0300
@@ -2941,6 +2941,7 @@ mpt_downloadboot(MPT_ADAPTER *ioc, MpiFw
 	u32			 fwSize;
 	u32			 diag0val;
 	int			 count;
+	unsigned long		 end_time;
 	u32			*ptrFw;
 	u32			 diagRwData;
 	u32			 nextImage;
@@ -2969,7 +2970,9 @@ mpt_downloadboot(MPT_ADAPTER *ioc, MpiFw
 	diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
 	CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val | MPI_DIAG_RESET_ADAPTER);
 
-	for (count = 0; count < 30; count ++) {
+	count = 0;
+	end_time = jiffies + msecs_to_jiffies(3000);
+	while (time_before(jiffies, end_time)) {
 		diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
 		if (!(diag0val & MPI_DIAG_RESET_ADAPTER)) {
 			ddlprintk((MYIOC_s_INFO_FMT "RESET_ADAPTER cleared, count=%d\n",
@@ -2982,9 +2985,10 @@ mpt_downloadboot(MPT_ADAPTER *ioc, MpiFw
 		} else {
 			mdelay (100);
 		}
+		++count;
 	}
 
-	if ( count == 30 ) {
+	if (diag0val & MPI_DIAG_RESET_ADAPTER) {
 		ddlprintk((MYIOC_s_INFO_FMT "downloadboot failed! "
 		"Unable to get MPI_DIAG_DRWE mode, diag0val=%x\n",
 		ioc->name, diag0val));
@@ -3099,7 +3103,9 @@ mpt_downloadboot(MPT_ADAPTER *ioc, MpiFw
 		}
 	}
 
-	for (count=0; count<HZ*20; count++) {
+	count = 0;
+	end_time = jiffies + msecs_to_jiffies(HZ * 200);
+	while (time_before(jiffies, end_time)) {
 		if ((ioc_state = mpt_GetIocState(ioc, 0)) & MPI_IOC_STATE_READY) {
 			ddlprintk((MYIOC_s_INFO_FMT "downloadboot successful! (count=%d) IocState=%x\n",
 					ioc->name, count, ioc_state));
@@ -3120,6 +3126,7 @@ mpt_downloadboot(MPT_ADAPTER *ioc, MpiFw
 		} else {
 			mdelay (10);
 		}
+		++count;
 	}
 	ddlprintk((MYIOC_s_INFO_FMT "downloadboot failed! IocState=%x\n",
 		ioc->name, ioc_state));
@@ -3157,7 +3164,8 @@ KickStart(MPT_ADAPTER *ioc, int force, i
 {
 	int hard_reset_done = 0;
 	u32 ioc_state=0;
-	int cnt,cntdn;
+	int cnt;
+	unsigned long end_time;
 
 	dinitprintk((KERN_WARNING MYNAM ": KickStarting %s!\n", ioc->name));
 	if (ioc->bus_type == SPI) {
@@ -3180,8 +3188,9 @@ KickStart(MPT_ADAPTER *ioc, int force, i
 	dinitprintk((MYIOC_s_INFO_FMT "Diagnostic reset successful!\n",
 			ioc->name));
 
-	cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 2;	/* 2 seconds */
-	for (cnt=0; cnt<cntdn; cnt++) {
+	cnt = 0;
+	end_time = jiffies + msecs_to_jiffies(2000);
+	while (time_before(jiffies, end_time)) {
 		ioc_state = mpt_GetIocState(ioc, 1);
 		if ((ioc_state == MPI_IOC_STATE_READY) || (ioc_state == MPI_IOC_STATE_OPERATIONAL)) {
 			dinitprintk((MYIOC_s_INFO_FMT "KickStart successful! (cnt=%d)\n",
@@ -3193,6 +3202,7 @@ KickStart(MPT_ADAPTER *ioc, int force, i
 		} else {
 			mdelay (10);
 		}
+		++cnt;
 	}
 
 	printk(MYIOC_s_ERR_FMT "Failed to come READY after reset! IocState=%x\n",
@@ -3229,6 +3239,7 @@ mpt_diag_reset(MPT_ADAPTER *ioc, int ign
 #ifdef MPT_DEBUG
 	u32 diag1val = 0;
 #endif
+	unsigned long end_time;
 
 	/* Clear any existing interrupts */
 	CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
@@ -3247,6 +3258,7 @@ mpt_diag_reset(MPT_ADAPTER *ioc, int ign
 	 * or if the reset history is 0
 	 */
 	if (ignore || !(diag0val & MPI_DIAG_RESET_HISTORY)) {
+		end_time = jiffies + msecs_to_jiffies(2000);
 		while ((diag0val & MPI_DIAG_DRWE) == 0) {
 			/* Write magic sequence to WriteSequence register
 			 * Loop until in diagnostic mode
@@ -3265,12 +3277,10 @@ mpt_diag_reset(MPT_ADAPTER *ioc, int ign
 				mdelay (100);
 			}
 
-			count++;
-			if (count > 20) {
+			if (time_after(jiffies, end_time)) {
 				printk(MYIOC_s_ERR_FMT "Enable Diagnostic mode FAILED! (%02xh)\n",
 						ioc->name, diag0val);
 				return -2;
-
 			}
 
 			diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
@@ -3331,7 +3341,8 @@ mpt_diag_reset(MPT_ADAPTER *ioc, int ign
 			 * IOC will be left unusable. This is a fatal error
 			 * case.  _diag_reset will return < 0
 			 */
-			for (count = 0; count < 30; count ++) {
+			end_time = jiffies + msecs_to_jiffies(30000);
+			while (time_before(jiffies, end_time)) {
 				diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
 				if (!(diag0val & MPI_DIAG_RESET_ADAPTER)) {
 					break;
@@ -3357,7 +3368,8 @@ mpt_diag_reset(MPT_ADAPTER *ioc, int ign
 			 * If fail, no error will check again
 			 * with calling program.
 			 */
-			for (count = 0; count < 60; count ++) {
+			end_time = jiffies + msecs_to_jiffies(60000);
+			while (time_before(jiffies, end_time)) {
 				doorbell = CHIPREG_READ32(&ioc->chip->Doorbell);
 				doorbell &= MPI_IOC_STATE_MASK;
 
Index: linux-kj/drivers/message/fusion/mptscsih.c
===================================================================
--- linux-kj.orig/drivers/message/fusion/mptscsih.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/message/fusion/mptscsih.c	2006-01-18 16:45:59.000000000 +0300
@@ -1968,10 +1968,11 @@ static int
 mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
 {
 	unsigned long  flags;
-	int            loop_count = 4 * 10;  /* Wait 10 seconds */
 	int            status = FAILED;
+	unsigned long  end_time;
 
-	do {
+	end_time = jiffies + msecs_to_jiffies(10000); /* Wait 10 seconds */
+	while (time_before(jiffies, end_time)) {
 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
 		if (hd->tmState == TM_STATE_NONE) {
 			hd->tmState = TM_STATE_IN_PROGRESS;
@@ -1982,7 +1983,7 @@ mptscsih_tm_pending_wait(MPT_SCSI_HOST *
 		}
 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
 		msleep(250);
-	} while (--loop_count);
+	}
 
 	return status;
 }
@@ -1998,10 +1999,11 @@ static int
 mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
 {
 	unsigned long  flags;
-	int            loop_count = 4 * timeout;
 	int            status = FAILED;
+	unsigned long  end_time;
 
-	do {
+	end_time = jiffies + msecs_to_jiffies(timeout * 1000);
+	while (time_before(jiffies, end_time)) {
 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
 		if(hd->tmPending == 0) {
 			status = SUCCESS;
@@ -2010,7 +2012,7 @@ mptscsih_tm_wait_for_completion(MPT_SCSI
 		}
 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
 		msleep_interruptible(250);
-	} while (--loop_count);
+	}
 
 	return status;
 }
