[PATCH] drivers/net/*: fix polling loops

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

small bug in drivers/net/sis190.c - "i" never reached 1000

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

Index: linux-kj/drivers/net/bnx2.c
===================================================================
--- linux-kj.orig/drivers/net/bnx2.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/net/bnx2.c	2006-01-18 16:46:01.000000000 +0300
@@ -4055,9 +4055,9 @@ bnx2_test_link(struct bnx2 *bp)
 static int
 bnx2_test_intr(struct bnx2 *bp)
 {
-	int i;
 	u32 val;
 	u16 status_idx;
+	unsigned long end_time;
 
 	if (!netif_running(bp->dev))
 		return -ENODEV;
@@ -4069,7 +4069,8 @@ bnx2_test_intr(struct bnx2 *bp)
 	REG_WR(bp, BNX2_HC_COMMAND, val | BNX2_HC_COMMAND_COAL_NOW);
 	REG_RD(bp, BNX2_HC_COMMAND);
 
-	for (i = 0; i < 10; i++) {
+	end_time = jiffies + msecs_to_jiffies(100);
+	while (time_before(jiffies, end_time)) {
 		if ((REG_RD(bp, BNX2_PCICFG_INT_ACK_CMD) & 0xffff) !=
 			status_idx) {
 
@@ -4078,7 +4079,7 @@ bnx2_test_intr(struct bnx2 *bp)
 
 		msleep_interruptible(10);
 	}
-	if (i < 10)
+	if (time_before(jiffies, end_time))
 		return 0;
 
 	return -ENODEV;
Index: linux-kj/drivers/net/pcmcia/xirc2ps_cs.c
===================================================================
--- linux-kj.orig/drivers/net/pcmcia/xirc2ps_cs.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/net/pcmcia/xirc2ps_cs.c	2006-01-18 16:46:01.000000000 +0300
@@ -1797,7 +1797,7 @@ init_mii(struct net_device *dev)
     local_info_t *local = netdev_priv(dev);
     kio_addr_t ioaddr = dev->base_addr;
     unsigned control, status, linkpartner;
-    int i;
+	unsigned long end_time;
 
     if (if_port == 4 || if_port == 1) { /* force 100BaseT or 10BaseT */
 	dev->if_port = if_port;
@@ -1831,13 +1831,13 @@ init_mii(struct net_device *dev)
     if (local->probe_port) {
 	/* according to the DP83840A specs the auto negotiation process
 	 * may take up to 3.5 sec, so we use this also for our ML6692
-	 * Fixme: Better to use a timer here!
 	 */
-	for (i=0; i < 35; i++) {
-	    msleep(100);	 /* wait 100 msec */
-	    status = mii_rd(ioaddr,  0, 1);
-	    if ((status & 0x0020) && (status & 0x0004))
-		break;
+	end_time = jiffies + msecs_to_jiffies(3500);
+	while (time_before(jiffies, end_time)) {
+		msleep(100);	 /* wait 100 msec */
+		status = mii_rd(ioaddr,  0, 1);
+		if ((status & 0x0020) && (status & 0x0004))
+			break;
 	}
 
 	if (!(status & 0x0020)) {
Index: linux-kj/drivers/net/s2io.c
===================================================================
--- linux-kj.orig/drivers/net/s2io.c	2006-01-18 16:42:19.000000000 +0300
+++ linux-kj/drivers/net/s2io.c	2006-01-18 16:46:01.000000000 +0300
@@ -835,13 +835,13 @@ static int init_nic(struct s2io_nic *nic
 	struct net_device *dev = nic->dev;
 	register u64 val64 = 0;
 	void __iomem *add;
-	u32 time;
 	int i, j;
 	mac_info_t *mac_control;
 	struct config_param *config;
 	int mdio_cnt = 0, dtx_cnt = 0;
 	unsigned long long mem_share;
 	int mem_size;
+	unsigned long end_time;
 
 	mac_control = &nic->mac_control;
 	config = &nic->config;
@@ -1357,19 +1357,18 @@ static int init_nic(struct s2io_nic *nic
 	 * We wait for a maximum of 500ms for the operation to complete,
 	 * if it's not complete by then we return error.
 	 */
-	time = 0;
+	end_time = jiffies + msecs_to_jiffies(500);
 	while (TRUE) {
 		val64 = readq(&bar0->tti_command_mem);
 		if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
 			break;
 		}
-		if (time > 10) {
+		if (time_after(jiffies, end_time)) {
 			DBG_PRINT(ERR_DBG, "%s: TTI init Failed\n",
 				  dev->name);
 			return -1;
 		}
 		msleep(50);
-		time++;
 	}
 
 	if (nic->config.bimodal) {
@@ -1385,19 +1384,18 @@ static int init_nic(struct s2io_nic *nic
 		 * We wait for a maximum of 500ms for the operation to complete,
 		 * if it's not complete by then we return error.
 		*/
-			time = 0;
+			end_time = jiffies + msecs_to_jiffies(500);
 			while (TRUE) {
 				val64 = readq(&bar0->tti_command_mem);
 				if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
 					break;
 				}
-				if (time > 10) {
+				if (time_after(jiffies, end_time)) {
 					DBG_PRINT(ERR_DBG,
 						"%s: TTI init Failed\n",
 					dev->name);
 					return -1;
 				}
-				time++;
 				msleep(50);
 			}
 		}
@@ -1442,18 +1440,17 @@ static int init_nic(struct s2io_nic *nic
 			 * for the operation to complete, if it's not complete
 			 * by then we return error.
 			 */
-			time = 0;
+			end_time = jiffies + msecs_to_jiffies(500);
 			while (TRUE) {
 				val64 = readq(&bar0->rti_command_mem);
 				if (!(val64 & RTI_CMD_MEM_STROBE_NEW_CMD)) {
 					break;
 				}
-				if (time > 10) {
+				if (time_after(jiffies, end_time)) {
 					DBG_PRINT(ERR_DBG, "%s: RTI init Failed\n",
 						  dev->name);
 					return -1;
 				}
-				time++;
 				msleep(50);
 			}
 		}
@@ -2825,8 +2822,9 @@ static void alarm_intr_handler(struct s2
 static int wait_for_cmd_complete(nic_t * sp)
 {
 	XENA_dev_config_t __iomem *bar0 = sp->bar0;
-	int ret = FAILURE, cnt = 0;
+	int ret = FAILURE;
 	u64 val64;
+	unsigned long end_time = jiffies + msecs_to_jiffies(500);
 
 	while (TRUE) {
 		val64 = readq(&bar0->rmac_addr_cmd_mem);
@@ -2835,7 +2833,7 @@ static int wait_for_cmd_complete(nic_t *
 			break;
 		}
 		msleep(50);
-		if (cnt++ > 10)
+		if (time_after(jiffies, end_time))
 			break;
 	}
 
@@ -4823,13 +4821,15 @@ static int s2io_eeprom_test(nic_t * sp, 
 static int s2io_bist_test(nic_t * sp, uint64_t * data)
 {
 	u8 bist = 0;
-	int cnt = 0, ret = -1;
+	int ret = -1;
+	unsigned long end_time;
 
 	pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
 	bist |= PCI_BIST_START;
 	pci_write_config_word(sp->pdev, PCI_BIST, bist);
 
-	while (cnt < 20) {
+	end_time = jiffies + msecs_to_jiffies(2000);
+	while (time_before(jiffies, end_time)) {
 		pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
 		if (!(bist & PCI_BIST_START)) {
 			*data = (bist & PCI_BIST_CODE_MASK);
@@ -4837,7 +4837,6 @@ static int s2io_bist_test(nic_t * sp, ui
 			break;
 		}
 		msleep(100);
-		cnt++;
 	}
 
 	return ret;
@@ -4885,7 +4884,8 @@ static int s2io_rldram_test(nic_t * sp, 
 {
 	XENA_dev_config_t __iomem *bar0 = sp->bar0;
 	u64 val64;
-	int cnt, iteration = 0, test_fail = 0;
+	int iteration = 0, test_fail = 0;
+	unsigned long end_time;
 
 	val64 = readq(&bar0->adapter_control);
 	val64 &= ~ADAPTER_ECC_EN;
@@ -4928,27 +4928,29 @@ static int s2io_rldram_test(nic_t * sp, 
 		    	MC_RLDRAM_TEST_GO;
 		SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_test_ctrl, LF);
 
-		for (cnt = 0; cnt < 5; cnt++) {
+		end_time = jiffies + msecs_to_jiffies(1000);
+		while (time_before(jiffies, end_time)) {
 			val64 = readq(&bar0->mc_rldram_test_ctrl);
 			if (val64 & MC_RLDRAM_TEST_DONE)
 				break;
 			msleep(200);
 		}
 
-		if (cnt == 5)
+		if (time_after_eq(jiffies, end_time))
 			break;
 
 		val64 = MC_RLDRAM_TEST_MODE | MC_RLDRAM_TEST_GO;
 		SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_test_ctrl, LF);
 
-		for (cnt = 0; cnt < 5; cnt++) {
+		end_time = jiffies + msecs_to_jiffies(2500);
+		while (time_before(jiffies, end_time)) {
 			val64 = readq(&bar0->mc_rldram_test_ctrl);
 			if (val64 & MC_RLDRAM_TEST_DONE)
 				break;
 			msleep(500);
 		}
 
-		if (cnt == 5)
+		if (time_after_eq(jiffies, end_time))
 			break;
 
 		val64 = readq(&bar0->mc_rldram_test_ctrl);
@@ -5411,10 +5413,10 @@ static void s2io_set_link(unsigned long 
 
 static void s2io_card_down(nic_t * sp)
 {
-	int cnt = 0;
 	XENA_dev_config_t __iomem *bar0 = sp->bar0;
 	unsigned long flags;
 	register u64 val64 = 0;
+	unsigned long end_time;
 
 	del_timer_sync(&sp->alarm_timer);
 	/* If s2io_set_link task is executing, wait till it completes. */
@@ -5430,6 +5432,7 @@ static void s2io_card_down(nic_t * sp)
 	tasklet_kill(&sp->task);
 
 	/* Check if the device is Quiescent and then Reset the NIC */
+	end_time = jiffies + msecs_to_jiffies(500);
 	do {
 		val64 = readq(&bar0->adapter_status);
 		if (verify_xena_quiescence(sp, val64, sp->device_enabled_once)) {
@@ -5437,8 +5440,7 @@ static void s2io_card_down(nic_t * sp)
 		}
 
 		msleep(50);
-		cnt++;
-		if (cnt == 10) {
+		if (time_after(jiffies, end_time)) {
 			DBG_PRINT(ERR_DBG,
 				  "s2io_close:Device not Quiescent ");
 			DBG_PRINT(ERR_DBG, "adaper status reads 0x%llx\n",
@@ -5449,13 +5451,12 @@ static void s2io_card_down(nic_t * sp)
 	s2io_reset(sp);
 
 	/* Waiting till all Interrupt handlers are complete */
-	cnt = 0;
-	do {
+	end_time = jiffies + msecs_to_jiffies(50);
+	while (time_before(jiffies, end_time)) {
 		msleep(10);
 		if (!atomic_read(&sp->isr_cnt))
 			break;
-		cnt++;
-	} while(cnt < 5);
+	}
 
 	spin_lock_irqsave(&sp->tx_lock, flags);
 	/* Free all Tx buffers */
Index: linux-kj/drivers/net/sis190.c
===================================================================
--- linux-kj.orig/drivers/net/sis190.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/net/sis190.c	2006-01-18 16:46:01.000000000 +0300
@@ -370,19 +370,20 @@ static int multicast_filter_limit = 32;
 
 static void __mdio_cmd(void __iomem *ioaddr, u32 ctl)
 {
-	unsigned int i;
+	unsigned long end_time;
 
 	SIS_W32(GMIIControl, ctl);
 
 	msleep(1);
 
-	for (i = 0; i < 100; i++) {
+	end_time = jiffies + msecs_to_jiffies(100);
+	while (time_before(jiffies, end_time)) {
 		if (!(SIS_R32(GMIIControl) & EhnMIInotDone))
 			break;
 		msleep(1);
 	}
 
-	if (i > 999)
+	if (time_after_eq(jiffies, end_time))
 		printk(KERN_ERR PFX "PHY command failed !\n");
 }
 
@@ -424,14 +425,15 @@ static u16 mdio_read_latched(void __iome
 static u16 __devinit sis190_read_eeprom(void __iomem *ioaddr, u32 reg)
 {
 	u16 data = 0xffff;
-	unsigned int i;
+	unsigned long end_time;
 
 	if (!(SIS_R32(ROMControl) & 0x0002))
 		return 0;
 
 	SIS_W32(ROMInterface, EEREQ | EEROP | (reg << 10));
 
-	for (i = 0; i < 200; i++) {
+	end_time = jiffies + msecs_to_jiffies(200);
+	while (time_before(jiffies, end_time)) {
 		if (!(SIS_R32(ROMInterface) & EEREQ)) {
 			data = (SIS_R32(ROMInterface) & 0xffff0000) >> 16;
 			break;
Index: linux-kj/drivers/net/skge.c
===================================================================
--- linux-kj.orig/drivers/net/skge.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/net/skge.c	2006-01-18 16:46:01.000000000 +0300
@@ -670,21 +670,21 @@ static void skge_led(struct skge_port *s
 static int skge_phys_id(struct net_device *dev, u32 data)
 {
 	struct skge_port *skge = netdev_priv(dev);
-	unsigned long ms;
+	unsigned long end_time;
 	enum led_mode mode = LED_MODE_TST;
 
 	if (!data || data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ))
-		ms = jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT / HZ) * 1000;
+		end_time = jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT / HZ) * 1000;
 	else
-		ms = data * 1000;
+		end_time = data * 1000;
 
-	while (ms > 0) {
+	end_time = jiffies + msecs_to_jiffies(end_time);
+	while (time_before(jiffies, end_time)) {
 		skge_led(skge, mode);
 		mode ^= LED_MODE_TST;
 
 		if (msleep_interruptible(BLINK_MS))
 			break;
-		ms -= BLINK_MS;
 	}
 
 	/* back to regular LED state */
Index: linux-kj/drivers/net/tg3.c
===================================================================
--- linux-kj.orig/drivers/net/tg3.c	2006-01-18 16:42:19.000000000 +0300
+++ linux-kj/drivers/net/tg3.c	2006-01-18 16:46:01.000000000 +0300
@@ -6283,7 +6283,8 @@ static void tg3_timer(unsigned long __op
 static int tg3_test_interrupt(struct tg3 *tp)
 {
 	struct net_device *dev = tp->dev;
-	int err, i;
+	int err;
+	unsigned long end_time;
 	u32 int_mbox = 0;
 
 	if (!netif_running(dev))
@@ -6304,7 +6305,8 @@ static int tg3_test_interrupt(struct tg3
 	tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
 	       HOSTCC_MODE_NOW);
 
-	for (i = 0; i < 5; i++) {
+	end_time = jiffies + msecs_to_jiffies(50);
+	while (time_before(jiffies, end_time)) {
 		int_mbox = tr32_mailbox(MAILBOX_INTERRUPT_0 +
 					TG3_64BIT_REG_LOW);
 		if (int_mbox != 0)
Index: linux-kj/drivers/net/wan/cycx_drv.c
===================================================================
--- linux-kj.orig/drivers/net/wan/cycx_drv.c	2006-01-18 02:21:27.000000000 +0300
+++ linux-kj/drivers/net/wan/cycx_drv.c	2006-01-18 16:46:01.000000000 +0300
@@ -383,7 +383,7 @@ static int cycx_code_boot(void __iomem *
  * o start adapter up */
 static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len)
 {
-	int i, j;
+	unsigned long end_time, end_time2;
 	struct cycx_fw_header *img_hdr;
 	u8 *reset_image,
 	   *data_image,
@@ -448,7 +448,8 @@ static int load_cyc2x(struct cycx_hw *hw
 			 cfm->descr[0] ? cfm->descr : "unknown firmware",
 			 cfm->info.codeid);
 
-	for (i = 0 ; i < 5 ; i++) {
+	end_time = jiffies + msecs_to_jiffies(20000);
+	while (time_before(jiffies, end_time)) {
 		/* Reset Cyclom hardware */
 		if (!reset_cyc2x(hw->dpmbase)) {
 			printk(KERN_ERR "%s: dpm problem or board not found\n",
@@ -462,7 +463,8 @@ static int load_cyc2x(struct cycx_hw *hw
 		writew(GEN_POWER_ON, pt_cycld);
 		msleep_interruptible(1 * 1000);
 
-		for (j = 0 ; j < 3 ; j++)
+		end_time2 = jiffies + msecs_to_jiffies(3000);
+		while (time_before(jiffies, end_time2))
 			if (!readw(pt_cycld))
 				goto reset_loaded;
 			else
