xref: /linux/drivers/net/ethernet/jme.c (revision b45e0c30bc58fb6fcaa42f1d1d813cefb8ab4117)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
4  *
5  * Copyright 2008 JMicron Technology Corporation
6  * http://www.jmicron.com/
7  * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
8  *
9  * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
10  */
11 
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/pci-aspm.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ethtool.h>
21 #include <linux/mii.h>
22 #include <linux/crc32.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/ipv6.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/if_vlan.h>
31 #include <linux/slab.h>
32 #include <net/ip6_checksum.h>
33 #include "jme.h"
34 
35 static int force_pseudohp = -1;
36 static int no_pseudohp = -1;
37 static int no_extplug = -1;
38 module_param(force_pseudohp, int, 0);
39 MODULE_PARM_DESC(force_pseudohp,
40 	"Enable pseudo hot-plug feature manually by driver instead of BIOS.");
41 module_param(no_pseudohp, int, 0);
42 MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
43 module_param(no_extplug, int, 0);
44 MODULE_PARM_DESC(no_extplug,
45 	"Do not use external plug signal for pseudo hot-plug.");
46 
47 static int
48 jme_mdio_read(struct net_device *netdev, int phy, int reg)
49 {
50 	struct jme_adapter *jme = netdev_priv(netdev);
51 	int i, val, again = (reg == MII_BMSR) ? 1 : 0;
52 
53 read_again:
54 	jwrite32(jme, JME_SMI, SMI_OP_REQ |
55 				smi_phy_addr(phy) |
56 				smi_reg_addr(reg));
57 
58 	wmb();
59 	for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
60 		udelay(20);
61 		val = jread32(jme, JME_SMI);
62 		if ((val & SMI_OP_REQ) == 0)
63 			break;
64 	}
65 
66 	if (i == 0) {
67 		pr_err("phy(%d) read timeout : %d\n", phy, reg);
68 		return 0;
69 	}
70 
71 	if (again--)
72 		goto read_again;
73 
74 	return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
75 }
76 
77 static void
78 jme_mdio_write(struct net_device *netdev,
79 				int phy, int reg, int val)
80 {
81 	struct jme_adapter *jme = netdev_priv(netdev);
82 	int i;
83 
84 	jwrite32(jme, JME_SMI, SMI_OP_WRITE | SMI_OP_REQ |
85 		((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
86 		smi_phy_addr(phy) | smi_reg_addr(reg));
87 
88 	wmb();
89 	for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
90 		udelay(20);
91 		if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
92 			break;
93 	}
94 
95 	if (i == 0)
96 		pr_err("phy(%d) write timeout : %d\n", phy, reg);
97 }
98 
99 static inline void
100 jme_reset_phy_processor(struct jme_adapter *jme)
101 {
102 	u32 val;
103 
104 	jme_mdio_write(jme->dev,
105 			jme->mii_if.phy_id,
106 			MII_ADVERTISE, ADVERTISE_ALL |
107 			ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
108 
109 	if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
110 		jme_mdio_write(jme->dev,
111 				jme->mii_if.phy_id,
112 				MII_CTRL1000,
113 				ADVERTISE_1000FULL | ADVERTISE_1000HALF);
114 
115 	val = jme_mdio_read(jme->dev,
116 				jme->mii_if.phy_id,
117 				MII_BMCR);
118 
119 	jme_mdio_write(jme->dev,
120 			jme->mii_if.phy_id,
121 			MII_BMCR, val | BMCR_RESET);
122 }
123 
124 static void
125 jme_setup_wakeup_frame(struct jme_adapter *jme,
126 		       const u32 *mask, u32 crc, int fnr)
127 {
128 	int i;
129 
130 	/*
131 	 * Setup CRC pattern
132 	 */
133 	jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
134 	wmb();
135 	jwrite32(jme, JME_WFODP, crc);
136 	wmb();
137 
138 	/*
139 	 * Setup Mask
140 	 */
141 	for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
142 		jwrite32(jme, JME_WFOI,
143 				((i << WFOI_MASK_SHIFT) & WFOI_MASK_SEL) |
144 				(fnr & WFOI_FRAME_SEL));
145 		wmb();
146 		jwrite32(jme, JME_WFODP, mask[i]);
147 		wmb();
148 	}
149 }
150 
151 static inline void
152 jme_mac_rxclk_off(struct jme_adapter *jme)
153 {
154 	jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
155 	jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
156 }
157 
158 static inline void
159 jme_mac_rxclk_on(struct jme_adapter *jme)
160 {
161 	jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
162 	jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
163 }
164 
165 static inline void
166 jme_mac_txclk_off(struct jme_adapter *jme)
167 {
168 	jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
169 	jwrite32f(jme, JME_GHC, jme->reg_ghc);
170 }
171 
172 static inline void
173 jme_mac_txclk_on(struct jme_adapter *jme)
174 {
175 	u32 speed = jme->reg_ghc & GHC_SPEED;
176 	if (speed == GHC_SPEED_1000M)
177 		jme->reg_ghc |= GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
178 	else
179 		jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
180 	jwrite32f(jme, JME_GHC, jme->reg_ghc);
181 }
182 
183 static inline void
184 jme_reset_ghc_speed(struct jme_adapter *jme)
185 {
186 	jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
187 	jwrite32f(jme, JME_GHC, jme->reg_ghc);
188 }
189 
190 static inline void
191 jme_reset_250A2_workaround(struct jme_adapter *jme)
192 {
193 	jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
194 			     GPREG1_RSSPATCH);
195 	jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
196 }
197 
198 static inline void
199 jme_assert_ghc_reset(struct jme_adapter *jme)
200 {
201 	jme->reg_ghc |= GHC_SWRST;
202 	jwrite32f(jme, JME_GHC, jme->reg_ghc);
203 }
204 
205 static inline void
206 jme_clear_ghc_reset(struct jme_adapter *jme)
207 {
208 	jme->reg_ghc &= ~GHC_SWRST;
209 	jwrite32f(jme, JME_GHC, jme->reg_ghc);
210 }
211 
212 static void
213 jme_reset_mac_processor(struct jme_adapter *jme)
214 {
215 	static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
216 	u32 crc = 0xCDCDCDCD;
217 	u32 gpreg0;
218 	int i;
219 
220 	jme_reset_ghc_speed(jme);
221 	jme_reset_250A2_workaround(jme);
222 
223 	jme_mac_rxclk_on(jme);
224 	jme_mac_txclk_on(jme);
225 	udelay(1);
226 	jme_assert_ghc_reset(jme);
227 	udelay(1);
228 	jme_mac_rxclk_off(jme);
229 	jme_mac_txclk_off(jme);
230 	udelay(1);
231 	jme_clear_ghc_reset(jme);
232 	udelay(1);
233 	jme_mac_rxclk_on(jme);
234 	jme_mac_txclk_on(jme);
235 	udelay(1);
236 	jme_mac_rxclk_off(jme);
237 	jme_mac_txclk_off(jme);
238 
239 	jwrite32(jme, JME_RXDBA_LO, 0x00000000);
240 	jwrite32(jme, JME_RXDBA_HI, 0x00000000);
241 	jwrite32(jme, JME_RXQDC, 0x00000000);
242 	jwrite32(jme, JME_RXNDA, 0x00000000);
243 	jwrite32(jme, JME_TXDBA_LO, 0x00000000);
244 	jwrite32(jme, JME_TXDBA_HI, 0x00000000);
245 	jwrite32(jme, JME_TXQDC, 0x00000000);
246 	jwrite32(jme, JME_TXNDA, 0x00000000);
247 
248 	jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
249 	jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
250 	for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
251 		jme_setup_wakeup_frame(jme, mask, crc, i);
252 	if (jme->fpgaver)
253 		gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
254 	else
255 		gpreg0 = GPREG0_DEFAULT;
256 	jwrite32(jme, JME_GPREG0, gpreg0);
257 }
258 
259 static inline void
260 jme_clear_pm_enable_wol(struct jme_adapter *jme)
261 {
262 	jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
263 }
264 
265 static inline void
266 jme_clear_pm_disable_wol(struct jme_adapter *jme)
267 {
268 	jwrite32(jme, JME_PMCS, PMCS_STMASK);
269 }
270 
271 static int
272 jme_reload_eeprom(struct jme_adapter *jme)
273 {
274 	u32 val;
275 	int i;
276 
277 	val = jread32(jme, JME_SMBCSR);
278 
279 	if (val & SMBCSR_EEPROMD) {
280 		val |= SMBCSR_CNACK;
281 		jwrite32(jme, JME_SMBCSR, val);
282 		val |= SMBCSR_RELOAD;
283 		jwrite32(jme, JME_SMBCSR, val);
284 		mdelay(12);
285 
286 		for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
287 			mdelay(1);
288 			if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
289 				break;
290 		}
291 
292 		if (i == 0) {
293 			pr_err("eeprom reload timeout\n");
294 			return -EIO;
295 		}
296 	}
297 
298 	return 0;
299 }
300 
301 static void
302 jme_load_macaddr(struct net_device *netdev)
303 {
304 	struct jme_adapter *jme = netdev_priv(netdev);
305 	unsigned char macaddr[ETH_ALEN];
306 	u32 val;
307 
308 	spin_lock_bh(&jme->macaddr_lock);
309 	val = jread32(jme, JME_RXUMA_LO);
310 	macaddr[0] = (val >>  0) & 0xFF;
311 	macaddr[1] = (val >>  8) & 0xFF;
312 	macaddr[2] = (val >> 16) & 0xFF;
313 	macaddr[3] = (val >> 24) & 0xFF;
314 	val = jread32(jme, JME_RXUMA_HI);
315 	macaddr[4] = (val >>  0) & 0xFF;
316 	macaddr[5] = (val >>  8) & 0xFF;
317 	memcpy(netdev->dev_addr, macaddr, ETH_ALEN);
318 	spin_unlock_bh(&jme->macaddr_lock);
319 }
320 
321 static inline void
322 jme_set_rx_pcc(struct jme_adapter *jme, int p)
323 {
324 	switch (p) {
325 	case PCC_OFF:
326 		jwrite32(jme, JME_PCCRX0,
327 			((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
328 			((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
329 		break;
330 	case PCC_P1:
331 		jwrite32(jme, JME_PCCRX0,
332 			((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
333 			((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
334 		break;
335 	case PCC_P2:
336 		jwrite32(jme, JME_PCCRX0,
337 			((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
338 			((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
339 		break;
340 	case PCC_P3:
341 		jwrite32(jme, JME_PCCRX0,
342 			((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
343 			((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
344 		break;
345 	default:
346 		break;
347 	}
348 	wmb();
349 
350 	if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
351 		netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
352 }
353 
354 static void
355 jme_start_irq(struct jme_adapter *jme)
356 {
357 	register struct dynpcc_info *dpi = &(jme->dpi);
358 
359 	jme_set_rx_pcc(jme, PCC_P1);
360 	dpi->cur		= PCC_P1;
361 	dpi->attempt		= PCC_P1;
362 	dpi->cnt		= 0;
363 
364 	jwrite32(jme, JME_PCCTX,
365 			((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
366 			((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
367 			PCCTXQ0_EN
368 		);
369 
370 	/*
371 	 * Enable Interrupts
372 	 */
373 	jwrite32(jme, JME_IENS, INTR_ENABLE);
374 }
375 
376 static inline void
377 jme_stop_irq(struct jme_adapter *jme)
378 {
379 	/*
380 	 * Disable Interrupts
381 	 */
382 	jwrite32f(jme, JME_IENC, INTR_ENABLE);
383 }
384 
385 static u32
386 jme_linkstat_from_phy(struct jme_adapter *jme)
387 {
388 	u32 phylink, bmsr;
389 
390 	phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
391 	bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
392 	if (bmsr & BMSR_ANCOMP)
393 		phylink |= PHY_LINK_AUTONEG_COMPLETE;
394 
395 	return phylink;
396 }
397 
398 static inline void
399 jme_set_phyfifo_5level(struct jme_adapter *jme)
400 {
401 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
402 }
403 
404 static inline void
405 jme_set_phyfifo_8level(struct jme_adapter *jme)
406 {
407 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
408 }
409 
410 static int
411 jme_check_link(struct net_device *netdev, int testonly)
412 {
413 	struct jme_adapter *jme = netdev_priv(netdev);
414 	u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
415 	char linkmsg[64];
416 	int rc = 0;
417 
418 	linkmsg[0] = '\0';
419 
420 	if (jme->fpgaver)
421 		phylink = jme_linkstat_from_phy(jme);
422 	else
423 		phylink = jread32(jme, JME_PHY_LINK);
424 
425 	if (phylink & PHY_LINK_UP) {
426 		if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
427 			/*
428 			 * If we did not enable AN
429 			 * Speed/Duplex Info should be obtained from SMI
430 			 */
431 			phylink = PHY_LINK_UP;
432 
433 			bmcr = jme_mdio_read(jme->dev,
434 						jme->mii_if.phy_id,
435 						MII_BMCR);
436 
437 			phylink |= ((bmcr & BMCR_SPEED1000) &&
438 					(bmcr & BMCR_SPEED100) == 0) ?
439 					PHY_LINK_SPEED_1000M :
440 					(bmcr & BMCR_SPEED100) ?
441 					PHY_LINK_SPEED_100M :
442 					PHY_LINK_SPEED_10M;
443 
444 			phylink |= (bmcr & BMCR_FULLDPLX) ?
445 					 PHY_LINK_DUPLEX : 0;
446 
447 			strcat(linkmsg, "Forced: ");
448 		} else {
449 			/*
450 			 * Keep polling for speed/duplex resolve complete
451 			 */
452 			while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
453 				--cnt) {
454 
455 				udelay(1);
456 
457 				if (jme->fpgaver)
458 					phylink = jme_linkstat_from_phy(jme);
459 				else
460 					phylink = jread32(jme, JME_PHY_LINK);
461 			}
462 			if (!cnt)
463 				pr_err("Waiting speed resolve timeout\n");
464 
465 			strcat(linkmsg, "ANed: ");
466 		}
467 
468 		if (jme->phylink == phylink) {
469 			rc = 1;
470 			goto out;
471 		}
472 		if (testonly)
473 			goto out;
474 
475 		jme->phylink = phylink;
476 
477 		/*
478 		 * The speed/duplex setting of jme->reg_ghc already cleared
479 		 * by jme_reset_mac_processor()
480 		 */
481 		switch (phylink & PHY_LINK_SPEED_MASK) {
482 		case PHY_LINK_SPEED_10M:
483 			jme->reg_ghc |= GHC_SPEED_10M;
484 			strcat(linkmsg, "10 Mbps, ");
485 			break;
486 		case PHY_LINK_SPEED_100M:
487 			jme->reg_ghc |= GHC_SPEED_100M;
488 			strcat(linkmsg, "100 Mbps, ");
489 			break;
490 		case PHY_LINK_SPEED_1000M:
491 			jme->reg_ghc |= GHC_SPEED_1000M;
492 			strcat(linkmsg, "1000 Mbps, ");
493 			break;
494 		default:
495 			break;
496 		}
497 
498 		if (phylink & PHY_LINK_DUPLEX) {
499 			jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
500 			jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
501 			jme->reg_ghc |= GHC_DPX;
502 		} else {
503 			jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
504 						TXMCS_BACKOFF |
505 						TXMCS_CARRIERSENSE |
506 						TXMCS_COLLISION);
507 			jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
508 		}
509 
510 		jwrite32(jme, JME_GHC, jme->reg_ghc);
511 
512 		if (is_buggy250(jme->pdev->device, jme->chiprev)) {
513 			jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
514 					     GPREG1_RSSPATCH);
515 			if (!(phylink & PHY_LINK_DUPLEX))
516 				jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
517 			switch (phylink & PHY_LINK_SPEED_MASK) {
518 			case PHY_LINK_SPEED_10M:
519 				jme_set_phyfifo_8level(jme);
520 				jme->reg_gpreg1 |= GPREG1_RSSPATCH;
521 				break;
522 			case PHY_LINK_SPEED_100M:
523 				jme_set_phyfifo_5level(jme);
524 				jme->reg_gpreg1 |= GPREG1_RSSPATCH;
525 				break;
526 			case PHY_LINK_SPEED_1000M:
527 				jme_set_phyfifo_8level(jme);
528 				break;
529 			default:
530 				break;
531 			}
532 		}
533 		jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
534 
535 		strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
536 					"Full-Duplex, " :
537 					"Half-Duplex, ");
538 		strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
539 					"MDI-X" :
540 					"MDI");
541 		netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
542 		netif_carrier_on(netdev);
543 	} else {
544 		if (testonly)
545 			goto out;
546 
547 		netif_info(jme, link, jme->dev, "Link is down\n");
548 		jme->phylink = 0;
549 		netif_carrier_off(netdev);
550 	}
551 
552 out:
553 	return rc;
554 }
555 
556 static int
557 jme_setup_tx_resources(struct jme_adapter *jme)
558 {
559 	struct jme_ring *txring = &(jme->txring[0]);
560 
561 	txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
562 				   TX_RING_ALLOC_SIZE(jme->tx_ring_size),
563 				   &(txring->dmaalloc),
564 				   GFP_ATOMIC);
565 
566 	if (!txring->alloc)
567 		goto err_set_null;
568 
569 	/*
570 	 * 16 Bytes align
571 	 */
572 	txring->desc		= (void *)ALIGN((unsigned long)(txring->alloc),
573 						RING_DESC_ALIGN);
574 	txring->dma		= ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
575 	txring->next_to_use	= 0;
576 	atomic_set(&txring->next_to_clean, 0);
577 	atomic_set(&txring->nr_free, jme->tx_ring_size);
578 
579 	txring->bufinf		= kcalloc(jme->tx_ring_size,
580 						sizeof(struct jme_buffer_info),
581 						GFP_ATOMIC);
582 	if (unlikely(!(txring->bufinf)))
583 		goto err_free_txring;
584 
585 	return 0;
586 
587 err_free_txring:
588 	dma_free_coherent(&(jme->pdev->dev),
589 			  TX_RING_ALLOC_SIZE(jme->tx_ring_size),
590 			  txring->alloc,
591 			  txring->dmaalloc);
592 
593 err_set_null:
594 	txring->desc = NULL;
595 	txring->dmaalloc = 0;
596 	txring->dma = 0;
597 	txring->bufinf = NULL;
598 
599 	return -ENOMEM;
600 }
601 
602 static void
603 jme_free_tx_resources(struct jme_adapter *jme)
604 {
605 	int i;
606 	struct jme_ring *txring = &(jme->txring[0]);
607 	struct jme_buffer_info *txbi;
608 
609 	if (txring->alloc) {
610 		if (txring->bufinf) {
611 			for (i = 0 ; i < jme->tx_ring_size ; ++i) {
612 				txbi = txring->bufinf + i;
613 				if (txbi->skb) {
614 					dev_kfree_skb(txbi->skb);
615 					txbi->skb = NULL;
616 				}
617 				txbi->mapping		= 0;
618 				txbi->len		= 0;
619 				txbi->nr_desc		= 0;
620 				txbi->start_xmit	= 0;
621 			}
622 			kfree(txring->bufinf);
623 		}
624 
625 		dma_free_coherent(&(jme->pdev->dev),
626 				  TX_RING_ALLOC_SIZE(jme->tx_ring_size),
627 				  txring->alloc,
628 				  txring->dmaalloc);
629 
630 		txring->alloc		= NULL;
631 		txring->desc		= NULL;
632 		txring->dmaalloc	= 0;
633 		txring->dma		= 0;
634 		txring->bufinf		= NULL;
635 	}
636 	txring->next_to_use	= 0;
637 	atomic_set(&txring->next_to_clean, 0);
638 	atomic_set(&txring->nr_free, 0);
639 }
640 
641 static inline void
642 jme_enable_tx_engine(struct jme_adapter *jme)
643 {
644 	/*
645 	 * Select Queue 0
646 	 */
647 	jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
648 	wmb();
649 
650 	/*
651 	 * Setup TX Queue 0 DMA Bass Address
652 	 */
653 	jwrite32(jme, JME_TXDBA_LO, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
654 	jwrite32(jme, JME_TXDBA_HI, (__u64)(jme->txring[0].dma) >> 32);
655 	jwrite32(jme, JME_TXNDA, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
656 
657 	/*
658 	 * Setup TX Descptor Count
659 	 */
660 	jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
661 
662 	/*
663 	 * Enable TX Engine
664 	 */
665 	wmb();
666 	jwrite32f(jme, JME_TXCS, jme->reg_txcs |
667 				TXCS_SELECT_QUEUE0 |
668 				TXCS_ENABLE);
669 
670 	/*
671 	 * Start clock for TX MAC Processor
672 	 */
673 	jme_mac_txclk_on(jme);
674 }
675 
676 static inline void
677 jme_disable_tx_engine(struct jme_adapter *jme)
678 {
679 	int i;
680 	u32 val;
681 
682 	/*
683 	 * Disable TX Engine
684 	 */
685 	jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
686 	wmb();
687 
688 	val = jread32(jme, JME_TXCS);
689 	for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
690 		mdelay(1);
691 		val = jread32(jme, JME_TXCS);
692 		rmb();
693 	}
694 
695 	if (!i)
696 		pr_err("Disable TX engine timeout\n");
697 
698 	/*
699 	 * Stop clock for TX MAC Processor
700 	 */
701 	jme_mac_txclk_off(jme);
702 }
703 
704 static void
705 jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
706 {
707 	struct jme_ring *rxring = &(jme->rxring[0]);
708 	register struct rxdesc *rxdesc = rxring->desc;
709 	struct jme_buffer_info *rxbi = rxring->bufinf;
710 	rxdesc += i;
711 	rxbi += i;
712 
713 	rxdesc->dw[0] = 0;
714 	rxdesc->dw[1] = 0;
715 	rxdesc->desc1.bufaddrh	= cpu_to_le32((__u64)rxbi->mapping >> 32);
716 	rxdesc->desc1.bufaddrl	= cpu_to_le32(
717 					(__u64)rxbi->mapping & 0xFFFFFFFFUL);
718 	rxdesc->desc1.datalen	= cpu_to_le16(rxbi->len);
719 	if (jme->dev->features & NETIF_F_HIGHDMA)
720 		rxdesc->desc1.flags = RXFLAG_64BIT;
721 	wmb();
722 	rxdesc->desc1.flags	|= RXFLAG_OWN | RXFLAG_INT;
723 }
724 
725 static int
726 jme_make_new_rx_buf(struct jme_adapter *jme, int i)
727 {
728 	struct jme_ring *rxring = &(jme->rxring[0]);
729 	struct jme_buffer_info *rxbi = rxring->bufinf + i;
730 	struct sk_buff *skb;
731 	dma_addr_t mapping;
732 
733 	skb = netdev_alloc_skb(jme->dev,
734 		jme->dev->mtu + RX_EXTRA_LEN);
735 	if (unlikely(!skb))
736 		return -ENOMEM;
737 
738 	mapping = pci_map_page(jme->pdev, virt_to_page(skb->data),
739 			       offset_in_page(skb->data), skb_tailroom(skb),
740 			       PCI_DMA_FROMDEVICE);
741 	if (unlikely(pci_dma_mapping_error(jme->pdev, mapping))) {
742 		dev_kfree_skb(skb);
743 		return -ENOMEM;
744 	}
745 
746 	if (likely(rxbi->mapping))
747 		pci_unmap_page(jme->pdev, rxbi->mapping,
748 			       rxbi->len, PCI_DMA_FROMDEVICE);
749 
750 	rxbi->skb = skb;
751 	rxbi->len = skb_tailroom(skb);
752 	rxbi->mapping = mapping;
753 	return 0;
754 }
755 
756 static void
757 jme_free_rx_buf(struct jme_adapter *jme, int i)
758 {
759 	struct jme_ring *rxring = &(jme->rxring[0]);
760 	struct jme_buffer_info *rxbi = rxring->bufinf;
761 	rxbi += i;
762 
763 	if (rxbi->skb) {
764 		pci_unmap_page(jme->pdev,
765 				 rxbi->mapping,
766 				 rxbi->len,
767 				 PCI_DMA_FROMDEVICE);
768 		dev_kfree_skb(rxbi->skb);
769 		rxbi->skb = NULL;
770 		rxbi->mapping = 0;
771 		rxbi->len = 0;
772 	}
773 }
774 
775 static void
776 jme_free_rx_resources(struct jme_adapter *jme)
777 {
778 	int i;
779 	struct jme_ring *rxring = &(jme->rxring[0]);
780 
781 	if (rxring->alloc) {
782 		if (rxring->bufinf) {
783 			for (i = 0 ; i < jme->rx_ring_size ; ++i)
784 				jme_free_rx_buf(jme, i);
785 			kfree(rxring->bufinf);
786 		}
787 
788 		dma_free_coherent(&(jme->pdev->dev),
789 				  RX_RING_ALLOC_SIZE(jme->rx_ring_size),
790 				  rxring->alloc,
791 				  rxring->dmaalloc);
792 		rxring->alloc    = NULL;
793 		rxring->desc     = NULL;
794 		rxring->dmaalloc = 0;
795 		rxring->dma      = 0;
796 		rxring->bufinf   = NULL;
797 	}
798 	rxring->next_to_use   = 0;
799 	atomic_set(&rxring->next_to_clean, 0);
800 }
801 
802 static int
803 jme_setup_rx_resources(struct jme_adapter *jme)
804 {
805 	int i;
806 	struct jme_ring *rxring = &(jme->rxring[0]);
807 
808 	rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
809 				   RX_RING_ALLOC_SIZE(jme->rx_ring_size),
810 				   &(rxring->dmaalloc),
811 				   GFP_ATOMIC);
812 	if (!rxring->alloc)
813 		goto err_set_null;
814 
815 	/*
816 	 * 16 Bytes align
817 	 */
818 	rxring->desc		= (void *)ALIGN((unsigned long)(rxring->alloc),
819 						RING_DESC_ALIGN);
820 	rxring->dma		= ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
821 	rxring->next_to_use	= 0;
822 	atomic_set(&rxring->next_to_clean, 0);
823 
824 	rxring->bufinf		= kcalloc(jme->rx_ring_size,
825 						sizeof(struct jme_buffer_info),
826 						GFP_ATOMIC);
827 	if (unlikely(!(rxring->bufinf)))
828 		goto err_free_rxring;
829 
830 	/*
831 	 * Initiallize Receive Descriptors
832 	 */
833 	for (i = 0 ; i < jme->rx_ring_size ; ++i) {
834 		if (unlikely(jme_make_new_rx_buf(jme, i))) {
835 			jme_free_rx_resources(jme);
836 			return -ENOMEM;
837 		}
838 
839 		jme_set_clean_rxdesc(jme, i);
840 	}
841 
842 	return 0;
843 
844 err_free_rxring:
845 	dma_free_coherent(&(jme->pdev->dev),
846 			  RX_RING_ALLOC_SIZE(jme->rx_ring_size),
847 			  rxring->alloc,
848 			  rxring->dmaalloc);
849 err_set_null:
850 	rxring->desc = NULL;
851 	rxring->dmaalloc = 0;
852 	rxring->dma = 0;
853 	rxring->bufinf = NULL;
854 
855 	return -ENOMEM;
856 }
857 
858 static inline void
859 jme_enable_rx_engine(struct jme_adapter *jme)
860 {
861 	/*
862 	 * Select Queue 0
863 	 */
864 	jwrite32(jme, JME_RXCS, jme->reg_rxcs |
865 				RXCS_QUEUESEL_Q0);
866 	wmb();
867 
868 	/*
869 	 * Setup RX DMA Bass Address
870 	 */
871 	jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
872 	jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
873 	jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
874 
875 	/*
876 	 * Setup RX Descriptor Count
877 	 */
878 	jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
879 
880 	/*
881 	 * Setup Unicast Filter
882 	 */
883 	jme_set_unicastaddr(jme->dev);
884 	jme_set_multi(jme->dev);
885 
886 	/*
887 	 * Enable RX Engine
888 	 */
889 	wmb();
890 	jwrite32f(jme, JME_RXCS, jme->reg_rxcs |
891 				RXCS_QUEUESEL_Q0 |
892 				RXCS_ENABLE |
893 				RXCS_QST);
894 
895 	/*
896 	 * Start clock for RX MAC Processor
897 	 */
898 	jme_mac_rxclk_on(jme);
899 }
900 
901 static inline void
902 jme_restart_rx_engine(struct jme_adapter *jme)
903 {
904 	/*
905 	 * Start RX Engine
906 	 */
907 	jwrite32(jme, JME_RXCS, jme->reg_rxcs |
908 				RXCS_QUEUESEL_Q0 |
909 				RXCS_ENABLE |
910 				RXCS_QST);
911 }
912 
913 static inline void
914 jme_disable_rx_engine(struct jme_adapter *jme)
915 {
916 	int i;
917 	u32 val;
918 
919 	/*
920 	 * Disable RX Engine
921 	 */
922 	jwrite32(jme, JME_RXCS, jme->reg_rxcs);
923 	wmb();
924 
925 	val = jread32(jme, JME_RXCS);
926 	for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
927 		mdelay(1);
928 		val = jread32(jme, JME_RXCS);
929 		rmb();
930 	}
931 
932 	if (!i)
933 		pr_err("Disable RX engine timeout\n");
934 
935 	/*
936 	 * Stop clock for RX MAC Processor
937 	 */
938 	jme_mac_rxclk_off(jme);
939 }
940 
941 static u16
942 jme_udpsum(struct sk_buff *skb)
943 {
944 	u16 csum = 0xFFFFu;
945 
946 	if (skb->len < (ETH_HLEN + sizeof(struct iphdr)))
947 		return csum;
948 	if (skb->protocol != htons(ETH_P_IP))
949 		return csum;
950 	skb_set_network_header(skb, ETH_HLEN);
951 	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
952 	    (skb->len < (ETH_HLEN +
953 			(ip_hdr(skb)->ihl << 2) +
954 			sizeof(struct udphdr)))) {
955 		skb_reset_network_header(skb);
956 		return csum;
957 	}
958 	skb_set_transport_header(skb,
959 			ETH_HLEN + (ip_hdr(skb)->ihl << 2));
960 	csum = udp_hdr(skb)->check;
961 	skb_reset_transport_header(skb);
962 	skb_reset_network_header(skb);
963 
964 	return csum;
965 }
966 
967 static int
968 jme_rxsum_ok(struct jme_adapter *jme, u16 flags, struct sk_buff *skb)
969 {
970 	if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
971 		return false;
972 
973 	if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
974 			== RXWBFLAG_TCPON)) {
975 		if (flags & RXWBFLAG_IPV4)
976 			netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
977 		return false;
978 	}
979 
980 	if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
981 			== RXWBFLAG_UDPON) && jme_udpsum(skb)) {
982 		if (flags & RXWBFLAG_IPV4)
983 			netif_err(jme, rx_err, jme->dev, "UDP Checksum error\n");
984 		return false;
985 	}
986 
987 	if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
988 			== RXWBFLAG_IPV4)) {
989 		netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error\n");
990 		return false;
991 	}
992 
993 	return true;
994 }
995 
996 static void
997 jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
998 {
999 	struct jme_ring *rxring = &(jme->rxring[0]);
1000 	struct rxdesc *rxdesc = rxring->desc;
1001 	struct jme_buffer_info *rxbi = rxring->bufinf;
1002 	struct sk_buff *skb;
1003 	int framesize;
1004 
1005 	rxdesc += idx;
1006 	rxbi += idx;
1007 
1008 	skb = rxbi->skb;
1009 	pci_dma_sync_single_for_cpu(jme->pdev,
1010 					rxbi->mapping,
1011 					rxbi->len,
1012 					PCI_DMA_FROMDEVICE);
1013 
1014 	if (unlikely(jme_make_new_rx_buf(jme, idx))) {
1015 		pci_dma_sync_single_for_device(jme->pdev,
1016 						rxbi->mapping,
1017 						rxbi->len,
1018 						PCI_DMA_FROMDEVICE);
1019 
1020 		++(NET_STAT(jme).rx_dropped);
1021 	} else {
1022 		framesize = le16_to_cpu(rxdesc->descwb.framesize)
1023 				- RX_PREPAD_SIZE;
1024 
1025 		skb_reserve(skb, RX_PREPAD_SIZE);
1026 		skb_put(skb, framesize);
1027 		skb->protocol = eth_type_trans(skb, jme->dev);
1028 
1029 		if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags), skb))
1030 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1031 		else
1032 			skb_checksum_none_assert(skb);
1033 
1034 		if (rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_TAGON)) {
1035 			u16 vid = le16_to_cpu(rxdesc->descwb.vlan);
1036 
1037 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1038 			NET_STAT(jme).rx_bytes += 4;
1039 		}
1040 		jme->jme_rx(skb);
1041 
1042 		if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_DEST)) ==
1043 		    cpu_to_le16(RXWBFLAG_DEST_MUL))
1044 			++(NET_STAT(jme).multicast);
1045 
1046 		NET_STAT(jme).rx_bytes += framesize;
1047 		++(NET_STAT(jme).rx_packets);
1048 	}
1049 
1050 	jme_set_clean_rxdesc(jme, idx);
1051 
1052 }
1053 
1054 static int
1055 jme_process_receive(struct jme_adapter *jme, int limit)
1056 {
1057 	struct jme_ring *rxring = &(jme->rxring[0]);
1058 	struct rxdesc *rxdesc;
1059 	int i, j, ccnt, desccnt, mask = jme->rx_ring_mask;
1060 
1061 	if (unlikely(!atomic_dec_and_test(&jme->rx_cleaning)))
1062 		goto out_inc;
1063 
1064 	if (unlikely(atomic_read(&jme->link_changing) != 1))
1065 		goto out_inc;
1066 
1067 	if (unlikely(!netif_carrier_ok(jme->dev)))
1068 		goto out_inc;
1069 
1070 	i = atomic_read(&rxring->next_to_clean);
1071 	while (limit > 0) {
1072 		rxdesc = rxring->desc;
1073 		rxdesc += i;
1074 
1075 		if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_OWN)) ||
1076 		!(rxdesc->descwb.desccnt & RXWBDCNT_WBCPL))
1077 			goto out;
1078 		--limit;
1079 
1080 		rmb();
1081 		desccnt = rxdesc->descwb.desccnt & RXWBDCNT_DCNT;
1082 
1083 		if (unlikely(desccnt > 1 ||
1084 		rxdesc->descwb.errstat & RXWBERR_ALLERR)) {
1085 
1086 			if (rxdesc->descwb.errstat & RXWBERR_CRCERR)
1087 				++(NET_STAT(jme).rx_crc_errors);
1088 			else if (rxdesc->descwb.errstat & RXWBERR_OVERUN)
1089 				++(NET_STAT(jme).rx_fifo_errors);
1090 			else
1091 				++(NET_STAT(jme).rx_errors);
1092 
1093 			if (desccnt > 1)
1094 				limit -= desccnt - 1;
1095 
1096 			for (j = i, ccnt = desccnt ; ccnt-- ; ) {
1097 				jme_set_clean_rxdesc(jme, j);
1098 				j = (j + 1) & (mask);
1099 			}
1100 
1101 		} else {
1102 			jme_alloc_and_feed_skb(jme, i);
1103 		}
1104 
1105 		i = (i + desccnt) & (mask);
1106 	}
1107 
1108 out:
1109 	atomic_set(&rxring->next_to_clean, i);
1110 
1111 out_inc:
1112 	atomic_inc(&jme->rx_cleaning);
1113 
1114 	return limit > 0 ? limit : 0;
1115 
1116 }
1117 
1118 static void
1119 jme_attempt_pcc(struct dynpcc_info *dpi, int atmp)
1120 {
1121 	if (likely(atmp == dpi->cur)) {
1122 		dpi->cnt = 0;
1123 		return;
1124 	}
1125 
1126 	if (dpi->attempt == atmp) {
1127 		++(dpi->cnt);
1128 	} else {
1129 		dpi->attempt = atmp;
1130 		dpi->cnt = 0;
1131 	}
1132 
1133 }
1134 
1135 static void
1136 jme_dynamic_pcc(struct jme_adapter *jme)
1137 {
1138 	register struct dynpcc_info *dpi = &(jme->dpi);
1139 
1140 	if ((NET_STAT(jme).rx_bytes - dpi->last_bytes) > PCC_P3_THRESHOLD)
1141 		jme_attempt_pcc(dpi, PCC_P3);
1142 	else if ((NET_STAT(jme).rx_packets - dpi->last_pkts) > PCC_P2_THRESHOLD ||
1143 		 dpi->intr_cnt > PCC_INTR_THRESHOLD)
1144 		jme_attempt_pcc(dpi, PCC_P2);
1145 	else
1146 		jme_attempt_pcc(dpi, PCC_P1);
1147 
1148 	if (unlikely(dpi->attempt != dpi->cur && dpi->cnt > 5)) {
1149 		if (dpi->attempt < dpi->cur)
1150 			tasklet_schedule(&jme->rxclean_task);
1151 		jme_set_rx_pcc(jme, dpi->attempt);
1152 		dpi->cur = dpi->attempt;
1153 		dpi->cnt = 0;
1154 	}
1155 }
1156 
1157 static void
1158 jme_start_pcc_timer(struct jme_adapter *jme)
1159 {
1160 	struct dynpcc_info *dpi = &(jme->dpi);
1161 	dpi->last_bytes		= NET_STAT(jme).rx_bytes;
1162 	dpi->last_pkts		= NET_STAT(jme).rx_packets;
1163 	dpi->intr_cnt		= 0;
1164 	jwrite32(jme, JME_TMCSR,
1165 		TMCSR_EN | ((0xFFFFFF - PCC_INTERVAL_US) & TMCSR_CNT));
1166 }
1167 
1168 static inline void
1169 jme_stop_pcc_timer(struct jme_adapter *jme)
1170 {
1171 	jwrite32(jme, JME_TMCSR, 0);
1172 }
1173 
1174 static void
1175 jme_shutdown_nic(struct jme_adapter *jme)
1176 {
1177 	u32 phylink;
1178 
1179 	phylink = jme_linkstat_from_phy(jme);
1180 
1181 	if (!(phylink & PHY_LINK_UP)) {
1182 		/*
1183 		 * Disable all interrupt before issue timer
1184 		 */
1185 		jme_stop_irq(jme);
1186 		jwrite32(jme, JME_TIMER2, TMCSR_EN | 0xFFFFFE);
1187 	}
1188 }
1189 
1190 static void
1191 jme_pcc_tasklet(unsigned long arg)
1192 {
1193 	struct jme_adapter *jme = (struct jme_adapter *)arg;
1194 	struct net_device *netdev = jme->dev;
1195 
1196 	if (unlikely(test_bit(JME_FLAG_SHUTDOWN, &jme->flags))) {
1197 		jme_shutdown_nic(jme);
1198 		return;
1199 	}
1200 
1201 	if (unlikely(!netif_carrier_ok(netdev) ||
1202 		(atomic_read(&jme->link_changing) != 1)
1203 	)) {
1204 		jme_stop_pcc_timer(jme);
1205 		return;
1206 	}
1207 
1208 	if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
1209 		jme_dynamic_pcc(jme);
1210 
1211 	jme_start_pcc_timer(jme);
1212 }
1213 
1214 static inline void
1215 jme_polling_mode(struct jme_adapter *jme)
1216 {
1217 	jme_set_rx_pcc(jme, PCC_OFF);
1218 }
1219 
1220 static inline void
1221 jme_interrupt_mode(struct jme_adapter *jme)
1222 {
1223 	jme_set_rx_pcc(jme, PCC_P1);
1224 }
1225 
1226 static inline int
1227 jme_pseudo_hotplug_enabled(struct jme_adapter *jme)
1228 {
1229 	u32 apmc;
1230 	apmc = jread32(jme, JME_APMC);
1231 	return apmc & JME_APMC_PSEUDO_HP_EN;
1232 }
1233 
1234 static void
1235 jme_start_shutdown_timer(struct jme_adapter *jme)
1236 {
1237 	u32 apmc;
1238 
1239 	apmc = jread32(jme, JME_APMC) | JME_APMC_PCIE_SD_EN;
1240 	apmc &= ~JME_APMC_EPIEN_CTRL;
1241 	if (!no_extplug) {
1242 		jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_EN);
1243 		wmb();
1244 	}
1245 	jwrite32f(jme, JME_APMC, apmc);
1246 
1247 	jwrite32f(jme, JME_TIMER2, 0);
1248 	set_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1249 	jwrite32(jme, JME_TMCSR,
1250 		TMCSR_EN | ((0xFFFFFF - APMC_PHP_SHUTDOWN_DELAY) & TMCSR_CNT));
1251 }
1252 
1253 static void
1254 jme_stop_shutdown_timer(struct jme_adapter *jme)
1255 {
1256 	u32 apmc;
1257 
1258 	jwrite32f(jme, JME_TMCSR, 0);
1259 	jwrite32f(jme, JME_TIMER2, 0);
1260 	clear_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1261 
1262 	apmc = jread32(jme, JME_APMC);
1263 	apmc &= ~(JME_APMC_PCIE_SD_EN | JME_APMC_EPIEN_CTRL);
1264 	jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_DIS);
1265 	wmb();
1266 	jwrite32f(jme, JME_APMC, apmc);
1267 }
1268 
1269 static void
1270 jme_link_change_tasklet(unsigned long arg)
1271 {
1272 	struct jme_adapter *jme = (struct jme_adapter *)arg;
1273 	struct net_device *netdev = jme->dev;
1274 	int rc;
1275 
1276 	while (!atomic_dec_and_test(&jme->link_changing)) {
1277 		atomic_inc(&jme->link_changing);
1278 		netif_info(jme, intr, jme->dev, "Get link change lock failed\n");
1279 		while (atomic_read(&jme->link_changing) != 1)
1280 			netif_info(jme, intr, jme->dev, "Waiting link change lock\n");
1281 	}
1282 
1283 	if (jme_check_link(netdev, 1) && jme->old_mtu == netdev->mtu)
1284 		goto out;
1285 
1286 	jme->old_mtu = netdev->mtu;
1287 	netif_stop_queue(netdev);
1288 	if (jme_pseudo_hotplug_enabled(jme))
1289 		jme_stop_shutdown_timer(jme);
1290 
1291 	jme_stop_pcc_timer(jme);
1292 	tasklet_disable(&jme->txclean_task);
1293 	tasklet_disable(&jme->rxclean_task);
1294 	tasklet_disable(&jme->rxempty_task);
1295 
1296 	if (netif_carrier_ok(netdev)) {
1297 		jme_disable_rx_engine(jme);
1298 		jme_disable_tx_engine(jme);
1299 		jme_reset_mac_processor(jme);
1300 		jme_free_rx_resources(jme);
1301 		jme_free_tx_resources(jme);
1302 
1303 		if (test_bit(JME_FLAG_POLL, &jme->flags))
1304 			jme_polling_mode(jme);
1305 
1306 		netif_carrier_off(netdev);
1307 	}
1308 
1309 	jme_check_link(netdev, 0);
1310 	if (netif_carrier_ok(netdev)) {
1311 		rc = jme_setup_rx_resources(jme);
1312 		if (rc) {
1313 			pr_err("Allocating resources for RX error, Device STOPPED!\n");
1314 			goto out_enable_tasklet;
1315 		}
1316 
1317 		rc = jme_setup_tx_resources(jme);
1318 		if (rc) {
1319 			pr_err("Allocating resources for TX error, Device STOPPED!\n");
1320 			goto err_out_free_rx_resources;
1321 		}
1322 
1323 		jme_enable_rx_engine(jme);
1324 		jme_enable_tx_engine(jme);
1325 
1326 		netif_start_queue(netdev);
1327 
1328 		if (test_bit(JME_FLAG_POLL, &jme->flags))
1329 			jme_interrupt_mode(jme);
1330 
1331 		jme_start_pcc_timer(jme);
1332 	} else if (jme_pseudo_hotplug_enabled(jme)) {
1333 		jme_start_shutdown_timer(jme);
1334 	}
1335 
1336 	goto out_enable_tasklet;
1337 
1338 err_out_free_rx_resources:
1339 	jme_free_rx_resources(jme);
1340 out_enable_tasklet:
1341 	tasklet_enable(&jme->txclean_task);
1342 	tasklet_enable(&jme->rxclean_task);
1343 	tasklet_enable(&jme->rxempty_task);
1344 out:
1345 	atomic_inc(&jme->link_changing);
1346 }
1347 
1348 static void
1349 jme_rx_clean_tasklet(unsigned long arg)
1350 {
1351 	struct jme_adapter *jme = (struct jme_adapter *)arg;
1352 	struct dynpcc_info *dpi = &(jme->dpi);
1353 
1354 	jme_process_receive(jme, jme->rx_ring_size);
1355 	++(dpi->intr_cnt);
1356 
1357 }
1358 
1359 static int
1360 jme_poll(JME_NAPI_HOLDER(holder), JME_NAPI_WEIGHT(budget))
1361 {
1362 	struct jme_adapter *jme = jme_napi_priv(holder);
1363 	int rest;
1364 
1365 	rest = jme_process_receive(jme, JME_NAPI_WEIGHT_VAL(budget));
1366 
1367 	while (atomic_read(&jme->rx_empty) > 0) {
1368 		atomic_dec(&jme->rx_empty);
1369 		++(NET_STAT(jme).rx_dropped);
1370 		jme_restart_rx_engine(jme);
1371 	}
1372 	atomic_inc(&jme->rx_empty);
1373 
1374 	if (rest) {
1375 		JME_RX_COMPLETE(netdev, holder);
1376 		jme_interrupt_mode(jme);
1377 	}
1378 
1379 	JME_NAPI_WEIGHT_SET(budget, rest);
1380 	return JME_NAPI_WEIGHT_VAL(budget) - rest;
1381 }
1382 
1383 static void
1384 jme_rx_empty_tasklet(unsigned long arg)
1385 {
1386 	struct jme_adapter *jme = (struct jme_adapter *)arg;
1387 
1388 	if (unlikely(atomic_read(&jme->link_changing) != 1))
1389 		return;
1390 
1391 	if (unlikely(!netif_carrier_ok(jme->dev)))
1392 		return;
1393 
1394 	netif_info(jme, rx_status, jme->dev, "RX Queue Full!\n");
1395 
1396 	jme_rx_clean_tasklet(arg);
1397 
1398 	while (atomic_read(&jme->rx_empty) > 0) {
1399 		atomic_dec(&jme->rx_empty);
1400 		++(NET_STAT(jme).rx_dropped);
1401 		jme_restart_rx_engine(jme);
1402 	}
1403 	atomic_inc(&jme->rx_empty);
1404 }
1405 
1406 static void
1407 jme_wake_queue_if_stopped(struct jme_adapter *jme)
1408 {
1409 	struct jme_ring *txring = &(jme->txring[0]);
1410 
1411 	smp_wmb();
1412 	if (unlikely(netif_queue_stopped(jme->dev) &&
1413 	atomic_read(&txring->nr_free) >= (jme->tx_wake_threshold))) {
1414 		netif_info(jme, tx_done, jme->dev, "TX Queue Waked\n");
1415 		netif_wake_queue(jme->dev);
1416 	}
1417 
1418 }
1419 
1420 static void
1421 jme_tx_clean_tasklet(unsigned long arg)
1422 {
1423 	struct jme_adapter *jme = (struct jme_adapter *)arg;
1424 	struct jme_ring *txring = &(jme->txring[0]);
1425 	struct txdesc *txdesc = txring->desc;
1426 	struct jme_buffer_info *txbi = txring->bufinf, *ctxbi, *ttxbi;
1427 	int i, j, cnt = 0, max, err, mask;
1428 
1429 	tx_dbg(jme, "Into txclean\n");
1430 
1431 	if (unlikely(!atomic_dec_and_test(&jme->tx_cleaning)))
1432 		goto out;
1433 
1434 	if (unlikely(atomic_read(&jme->link_changing) != 1))
1435 		goto out;
1436 
1437 	if (unlikely(!netif_carrier_ok(jme->dev)))
1438 		goto out;
1439 
1440 	max = jme->tx_ring_size - atomic_read(&txring->nr_free);
1441 	mask = jme->tx_ring_mask;
1442 
1443 	for (i = atomic_read(&txring->next_to_clean) ; cnt < max ; ) {
1444 
1445 		ctxbi = txbi + i;
1446 
1447 		if (likely(ctxbi->skb &&
1448 		!(txdesc[i].descwb.flags & TXWBFLAG_OWN))) {
1449 
1450 			tx_dbg(jme, "txclean: %d+%d@%lu\n",
1451 			       i, ctxbi->nr_desc, jiffies);
1452 
1453 			err = txdesc[i].descwb.flags & TXWBFLAG_ALLERR;
1454 
1455 			for (j = 1 ; j < ctxbi->nr_desc ; ++j) {
1456 				ttxbi = txbi + ((i + j) & (mask));
1457 				txdesc[(i + j) & (mask)].dw[0] = 0;
1458 
1459 				pci_unmap_page(jme->pdev,
1460 						 ttxbi->mapping,
1461 						 ttxbi->len,
1462 						 PCI_DMA_TODEVICE);
1463 
1464 				ttxbi->mapping = 0;
1465 				ttxbi->len = 0;
1466 			}
1467 
1468 			dev_kfree_skb(ctxbi->skb);
1469 
1470 			cnt += ctxbi->nr_desc;
1471 
1472 			if (unlikely(err)) {
1473 				++(NET_STAT(jme).tx_carrier_errors);
1474 			} else {
1475 				++(NET_STAT(jme).tx_packets);
1476 				NET_STAT(jme).tx_bytes += ctxbi->len;
1477 			}
1478 
1479 			ctxbi->skb = NULL;
1480 			ctxbi->len = 0;
1481 			ctxbi->start_xmit = 0;
1482 
1483 		} else {
1484 			break;
1485 		}
1486 
1487 		i = (i + ctxbi->nr_desc) & mask;
1488 
1489 		ctxbi->nr_desc = 0;
1490 	}
1491 
1492 	tx_dbg(jme, "txclean: done %d@%lu\n", i, jiffies);
1493 	atomic_set(&txring->next_to_clean, i);
1494 	atomic_add(cnt, &txring->nr_free);
1495 
1496 	jme_wake_queue_if_stopped(jme);
1497 
1498 out:
1499 	atomic_inc(&jme->tx_cleaning);
1500 }
1501 
1502 static void
1503 jme_intr_msi(struct jme_adapter *jme, u32 intrstat)
1504 {
1505 	/*
1506 	 * Disable interrupt
1507 	 */
1508 	jwrite32f(jme, JME_IENC, INTR_ENABLE);
1509 
1510 	if (intrstat & (INTR_LINKCH | INTR_SWINTR)) {
1511 		/*
1512 		 * Link change event is critical
1513 		 * all other events are ignored
1514 		 */
1515 		jwrite32(jme, JME_IEVE, intrstat);
1516 		tasklet_schedule(&jme->linkch_task);
1517 		goto out_reenable;
1518 	}
1519 
1520 	if (intrstat & INTR_TMINTR) {
1521 		jwrite32(jme, JME_IEVE, INTR_TMINTR);
1522 		tasklet_schedule(&jme->pcc_task);
1523 	}
1524 
1525 	if (intrstat & (INTR_PCCTXTO | INTR_PCCTX)) {
1526 		jwrite32(jme, JME_IEVE, INTR_PCCTXTO | INTR_PCCTX | INTR_TX0);
1527 		tasklet_schedule(&jme->txclean_task);
1528 	}
1529 
1530 	if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1531 		jwrite32(jme, JME_IEVE, (intrstat & (INTR_PCCRX0TO |
1532 						     INTR_PCCRX0 |
1533 						     INTR_RX0EMP)) |
1534 					INTR_RX0);
1535 	}
1536 
1537 	if (test_bit(JME_FLAG_POLL, &jme->flags)) {
1538 		if (intrstat & INTR_RX0EMP)
1539 			atomic_inc(&jme->rx_empty);
1540 
1541 		if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1542 			if (likely(JME_RX_SCHEDULE_PREP(jme))) {
1543 				jme_polling_mode(jme);
1544 				JME_RX_SCHEDULE(jme);
1545 			}
1546 		}
1547 	} else {
1548 		if (intrstat & INTR_RX0EMP) {
1549 			atomic_inc(&jme->rx_empty);
1550 			tasklet_hi_schedule(&jme->rxempty_task);
1551 		} else if (intrstat & (INTR_PCCRX0TO | INTR_PCCRX0)) {
1552 			tasklet_hi_schedule(&jme->rxclean_task);
1553 		}
1554 	}
1555 
1556 out_reenable:
1557 	/*
1558 	 * Re-enable interrupt
1559 	 */
1560 	jwrite32f(jme, JME_IENS, INTR_ENABLE);
1561 }
1562 
1563 static irqreturn_t
1564 jme_intr(int irq, void *dev_id)
1565 {
1566 	struct net_device *netdev = dev_id;
1567 	struct jme_adapter *jme = netdev_priv(netdev);
1568 	u32 intrstat;
1569 
1570 	intrstat = jread32(jme, JME_IEVE);
1571 
1572 	/*
1573 	 * Check if it's really an interrupt for us
1574 	 */
1575 	if (unlikely((intrstat & INTR_ENABLE) == 0))
1576 		return IRQ_NONE;
1577 
1578 	/*
1579 	 * Check if the device still exist
1580 	 */
1581 	if (unlikely(intrstat == ~((typeof(intrstat))0)))
1582 		return IRQ_NONE;
1583 
1584 	jme_intr_msi(jme, intrstat);
1585 
1586 	return IRQ_HANDLED;
1587 }
1588 
1589 static irqreturn_t
1590 jme_msi(int irq, void *dev_id)
1591 {
1592 	struct net_device *netdev = dev_id;
1593 	struct jme_adapter *jme = netdev_priv(netdev);
1594 	u32 intrstat;
1595 
1596 	intrstat = jread32(jme, JME_IEVE);
1597 
1598 	jme_intr_msi(jme, intrstat);
1599 
1600 	return IRQ_HANDLED;
1601 }
1602 
1603 static void
1604 jme_reset_link(struct jme_adapter *jme)
1605 {
1606 	jwrite32(jme, JME_TMCSR, TMCSR_SWIT);
1607 }
1608 
1609 static void
1610 jme_restart_an(struct jme_adapter *jme)
1611 {
1612 	u32 bmcr;
1613 
1614 	spin_lock_bh(&jme->phy_lock);
1615 	bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1616 	bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
1617 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1618 	spin_unlock_bh(&jme->phy_lock);
1619 }
1620 
1621 static int
1622 jme_request_irq(struct jme_adapter *jme)
1623 {
1624 	int rc;
1625 	struct net_device *netdev = jme->dev;
1626 	irq_handler_t handler = jme_intr;
1627 	int irq_flags = IRQF_SHARED;
1628 
1629 	if (!pci_enable_msi(jme->pdev)) {
1630 		set_bit(JME_FLAG_MSI, &jme->flags);
1631 		handler = jme_msi;
1632 		irq_flags = 0;
1633 	}
1634 
1635 	rc = request_irq(jme->pdev->irq, handler, irq_flags, netdev->name,
1636 			  netdev);
1637 	if (rc) {
1638 		netdev_err(netdev,
1639 			   "Unable to request %s interrupt (return: %d)\n",
1640 			   test_bit(JME_FLAG_MSI, &jme->flags) ? "MSI" : "INTx",
1641 			   rc);
1642 
1643 		if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1644 			pci_disable_msi(jme->pdev);
1645 			clear_bit(JME_FLAG_MSI, &jme->flags);
1646 		}
1647 	} else {
1648 		netdev->irq = jme->pdev->irq;
1649 	}
1650 
1651 	return rc;
1652 }
1653 
1654 static void
1655 jme_free_irq(struct jme_adapter *jme)
1656 {
1657 	free_irq(jme->pdev->irq, jme->dev);
1658 	if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1659 		pci_disable_msi(jme->pdev);
1660 		clear_bit(JME_FLAG_MSI, &jme->flags);
1661 		jme->dev->irq = jme->pdev->irq;
1662 	}
1663 }
1664 
1665 static inline void
1666 jme_new_phy_on(struct jme_adapter *jme)
1667 {
1668 	u32 reg;
1669 
1670 	reg = jread32(jme, JME_PHY_PWR);
1671 	reg &= ~(PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1672 		 PHY_PWR_DWN2 | PHY_PWR_CLKSEL);
1673 	jwrite32(jme, JME_PHY_PWR, reg);
1674 
1675 	pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1676 	reg &= ~PE1_GPREG0_PBG;
1677 	reg |= PE1_GPREG0_ENBG;
1678 	pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1679 }
1680 
1681 static inline void
1682 jme_new_phy_off(struct jme_adapter *jme)
1683 {
1684 	u32 reg;
1685 
1686 	reg = jread32(jme, JME_PHY_PWR);
1687 	reg |= PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1688 	       PHY_PWR_DWN2 | PHY_PWR_CLKSEL;
1689 	jwrite32(jme, JME_PHY_PWR, reg);
1690 
1691 	pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1692 	reg &= ~PE1_GPREG0_PBG;
1693 	reg |= PE1_GPREG0_PDD3COLD;
1694 	pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1695 }
1696 
1697 static inline void
1698 jme_phy_on(struct jme_adapter *jme)
1699 {
1700 	u32 bmcr;
1701 
1702 	bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1703 	bmcr &= ~BMCR_PDOWN;
1704 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1705 
1706 	if (new_phy_power_ctrl(jme->chip_main_rev))
1707 		jme_new_phy_on(jme);
1708 }
1709 
1710 static inline void
1711 jme_phy_off(struct jme_adapter *jme)
1712 {
1713 	u32 bmcr;
1714 
1715 	bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1716 	bmcr |= BMCR_PDOWN;
1717 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1718 
1719 	if (new_phy_power_ctrl(jme->chip_main_rev))
1720 		jme_new_phy_off(jme);
1721 }
1722 
1723 static int
1724 jme_phy_specreg_read(struct jme_adapter *jme, u32 specreg)
1725 {
1726 	u32 phy_addr;
1727 
1728 	phy_addr = JM_PHY_SPEC_REG_READ | specreg;
1729 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1730 			phy_addr);
1731 	return jme_mdio_read(jme->dev, jme->mii_if.phy_id,
1732 			JM_PHY_SPEC_DATA_REG);
1733 }
1734 
1735 static void
1736 jme_phy_specreg_write(struct jme_adapter *jme, u32 ext_reg, u32 phy_data)
1737 {
1738 	u32 phy_addr;
1739 
1740 	phy_addr = JM_PHY_SPEC_REG_WRITE | ext_reg;
1741 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_DATA_REG,
1742 			phy_data);
1743 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1744 			phy_addr);
1745 }
1746 
1747 static int
1748 jme_phy_calibration(struct jme_adapter *jme)
1749 {
1750 	u32 ctrl1000, phy_data;
1751 
1752 	jme_phy_off(jme);
1753 	jme_phy_on(jme);
1754 	/*  Enabel PHY test mode 1 */
1755 	ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1756 	ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1757 	ctrl1000 |= PHY_GAD_TEST_MODE_1;
1758 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1759 
1760 	phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1761 	phy_data &= ~JM_PHY_EXT_COMM_2_CALI_MODE_0;
1762 	phy_data |= JM_PHY_EXT_COMM_2_CALI_LATCH |
1763 			JM_PHY_EXT_COMM_2_CALI_ENABLE;
1764 	jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1765 	msleep(20);
1766 	phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1767 	phy_data &= ~(JM_PHY_EXT_COMM_2_CALI_ENABLE |
1768 			JM_PHY_EXT_COMM_2_CALI_MODE_0 |
1769 			JM_PHY_EXT_COMM_2_CALI_LATCH);
1770 	jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1771 
1772 	/*  Disable PHY test mode */
1773 	ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1774 	ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1775 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1776 	return 0;
1777 }
1778 
1779 static int
1780 jme_phy_setEA(struct jme_adapter *jme)
1781 {
1782 	u32 phy_comm0 = 0, phy_comm1 = 0;
1783 	u8 nic_ctrl;
1784 
1785 	pci_read_config_byte(jme->pdev, PCI_PRIV_SHARE_NICCTRL, &nic_ctrl);
1786 	if ((nic_ctrl & 0x3) == JME_FLAG_PHYEA_ENABLE)
1787 		return 0;
1788 
1789 	switch (jme->pdev->device) {
1790 	case PCI_DEVICE_ID_JMICRON_JMC250:
1791 		if (((jme->chip_main_rev == 5) &&
1792 			((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1793 			(jme->chip_sub_rev == 3))) ||
1794 			(jme->chip_main_rev >= 6)) {
1795 			phy_comm0 = 0x008A;
1796 			phy_comm1 = 0x4109;
1797 		}
1798 		if ((jme->chip_main_rev == 3) &&
1799 			((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1800 			phy_comm0 = 0xE088;
1801 		break;
1802 	case PCI_DEVICE_ID_JMICRON_JMC260:
1803 		if (((jme->chip_main_rev == 5) &&
1804 			((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1805 			(jme->chip_sub_rev == 3))) ||
1806 			(jme->chip_main_rev >= 6)) {
1807 			phy_comm0 = 0x008A;
1808 			phy_comm1 = 0x4109;
1809 		}
1810 		if ((jme->chip_main_rev == 3) &&
1811 			((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1812 			phy_comm0 = 0xE088;
1813 		if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 0))
1814 			phy_comm0 = 0x608A;
1815 		if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 2))
1816 			phy_comm0 = 0x408A;
1817 		break;
1818 	default:
1819 		return -ENODEV;
1820 	}
1821 	if (phy_comm0)
1822 		jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_0_REG, phy_comm0);
1823 	if (phy_comm1)
1824 		jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_1_REG, phy_comm1);
1825 
1826 	return 0;
1827 }
1828 
1829 static int
1830 jme_open(struct net_device *netdev)
1831 {
1832 	struct jme_adapter *jme = netdev_priv(netdev);
1833 	int rc;
1834 
1835 	jme_clear_pm_disable_wol(jme);
1836 	JME_NAPI_ENABLE(jme);
1837 
1838 	tasklet_init(&jme->linkch_task, jme_link_change_tasklet,
1839 		     (unsigned long) jme);
1840 	tasklet_init(&jme->txclean_task, jme_tx_clean_tasklet,
1841 		     (unsigned long) jme);
1842 	tasklet_init(&jme->rxclean_task, jme_rx_clean_tasklet,
1843 		     (unsigned long) jme);
1844 	tasklet_init(&jme->rxempty_task, jme_rx_empty_tasklet,
1845 		     (unsigned long) jme);
1846 
1847 	rc = jme_request_irq(jme);
1848 	if (rc)
1849 		goto err_out;
1850 
1851 	jme_start_irq(jme);
1852 
1853 	jme_phy_on(jme);
1854 	if (test_bit(JME_FLAG_SSET, &jme->flags))
1855 		jme_set_link_ksettings(netdev, &jme->old_cmd);
1856 	else
1857 		jme_reset_phy_processor(jme);
1858 	jme_phy_calibration(jme);
1859 	jme_phy_setEA(jme);
1860 	jme_reset_link(jme);
1861 
1862 	return 0;
1863 
1864 err_out:
1865 	netif_stop_queue(netdev);
1866 	netif_carrier_off(netdev);
1867 	return rc;
1868 }
1869 
1870 static void
1871 jme_set_100m_half(struct jme_adapter *jme)
1872 {
1873 	u32 bmcr, tmp;
1874 
1875 	jme_phy_on(jme);
1876 	bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1877 	tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
1878 		       BMCR_SPEED1000 | BMCR_FULLDPLX);
1879 	tmp |= BMCR_SPEED100;
1880 
1881 	if (bmcr != tmp)
1882 		jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, tmp);
1883 
1884 	if (jme->fpgaver)
1885 		jwrite32(jme, JME_GHC, GHC_SPEED_100M | GHC_LINK_POLL);
1886 	else
1887 		jwrite32(jme, JME_GHC, GHC_SPEED_100M);
1888 }
1889 
1890 #define JME_WAIT_LINK_TIME 2000 /* 2000ms */
1891 static void
1892 jme_wait_link(struct jme_adapter *jme)
1893 {
1894 	u32 phylink, to = JME_WAIT_LINK_TIME;
1895 
1896 	msleep(1000);
1897 	phylink = jme_linkstat_from_phy(jme);
1898 	while (!(phylink & PHY_LINK_UP) && (to -= 10) > 0) {
1899 		usleep_range(10000, 11000);
1900 		phylink = jme_linkstat_from_phy(jme);
1901 	}
1902 }
1903 
1904 static void
1905 jme_powersave_phy(struct jme_adapter *jme)
1906 {
1907 	if (jme->reg_pmcs && device_may_wakeup(&jme->pdev->dev)) {
1908 		jme_set_100m_half(jme);
1909 		if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
1910 			jme_wait_link(jme);
1911 		jme_clear_pm_enable_wol(jme);
1912 	} else {
1913 		jme_phy_off(jme);
1914 	}
1915 }
1916 
1917 static int
1918 jme_close(struct net_device *netdev)
1919 {
1920 	struct jme_adapter *jme = netdev_priv(netdev);
1921 
1922 	netif_stop_queue(netdev);
1923 	netif_carrier_off(netdev);
1924 
1925 	jme_stop_irq(jme);
1926 	jme_free_irq(jme);
1927 
1928 	JME_NAPI_DISABLE(jme);
1929 
1930 	tasklet_kill(&jme->linkch_task);
1931 	tasklet_kill(&jme->txclean_task);
1932 	tasklet_kill(&jme->rxclean_task);
1933 	tasklet_kill(&jme->rxempty_task);
1934 
1935 	jme_disable_rx_engine(jme);
1936 	jme_disable_tx_engine(jme);
1937 	jme_reset_mac_processor(jme);
1938 	jme_free_rx_resources(jme);
1939 	jme_free_tx_resources(jme);
1940 	jme->phylink = 0;
1941 	jme_phy_off(jme);
1942 
1943 	return 0;
1944 }
1945 
1946 static int
1947 jme_alloc_txdesc(struct jme_adapter *jme,
1948 			struct sk_buff *skb)
1949 {
1950 	struct jme_ring *txring = &(jme->txring[0]);
1951 	int idx, nr_alloc, mask = jme->tx_ring_mask;
1952 
1953 	idx = txring->next_to_use;
1954 	nr_alloc = skb_shinfo(skb)->nr_frags + 2;
1955 
1956 	if (unlikely(atomic_read(&txring->nr_free) < nr_alloc))
1957 		return -1;
1958 
1959 	atomic_sub(nr_alloc, &txring->nr_free);
1960 
1961 	txring->next_to_use = (txring->next_to_use + nr_alloc) & mask;
1962 
1963 	return idx;
1964 }
1965 
1966 static int
1967 jme_fill_tx_map(struct pci_dev *pdev,
1968 		struct txdesc *txdesc,
1969 		struct jme_buffer_info *txbi,
1970 		struct page *page,
1971 		u32 page_offset,
1972 		u32 len,
1973 		bool hidma)
1974 {
1975 	dma_addr_t dmaaddr;
1976 
1977 	dmaaddr = pci_map_page(pdev,
1978 				page,
1979 				page_offset,
1980 				len,
1981 				PCI_DMA_TODEVICE);
1982 
1983 	if (unlikely(pci_dma_mapping_error(pdev, dmaaddr)))
1984 		return -EINVAL;
1985 
1986 	pci_dma_sync_single_for_device(pdev,
1987 				       dmaaddr,
1988 				       len,
1989 				       PCI_DMA_TODEVICE);
1990 
1991 	txdesc->dw[0] = 0;
1992 	txdesc->dw[1] = 0;
1993 	txdesc->desc2.flags	= TXFLAG_OWN;
1994 	txdesc->desc2.flags	|= (hidma) ? TXFLAG_64BIT : 0;
1995 	txdesc->desc2.datalen	= cpu_to_le16(len);
1996 	txdesc->desc2.bufaddrh	= cpu_to_le32((__u64)dmaaddr >> 32);
1997 	txdesc->desc2.bufaddrl	= cpu_to_le32(
1998 					(__u64)dmaaddr & 0xFFFFFFFFUL);
1999 
2000 	txbi->mapping = dmaaddr;
2001 	txbi->len = len;
2002 	return 0;
2003 }
2004 
2005 static void jme_drop_tx_map(struct jme_adapter *jme, int startidx, int count)
2006 {
2007 	struct jme_ring *txring = &(jme->txring[0]);
2008 	struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
2009 	int mask = jme->tx_ring_mask;
2010 	int j;
2011 
2012 	for (j = 0 ; j < count ; j++) {
2013 		ctxbi = txbi + ((startidx + j + 2) & (mask));
2014 		pci_unmap_page(jme->pdev,
2015 				ctxbi->mapping,
2016 				ctxbi->len,
2017 				PCI_DMA_TODEVICE);
2018 
2019 		ctxbi->mapping = 0;
2020 		ctxbi->len = 0;
2021 	}
2022 }
2023 
2024 static int
2025 jme_map_tx_skb(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2026 {
2027 	struct jme_ring *txring = &(jme->txring[0]);
2028 	struct txdesc *txdesc = txring->desc, *ctxdesc;
2029 	struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
2030 	bool hidma = jme->dev->features & NETIF_F_HIGHDMA;
2031 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
2032 	int mask = jme->tx_ring_mask;
2033 	u32 len;
2034 	int ret = 0;
2035 
2036 	for (i = 0 ; i < nr_frags ; ++i) {
2037 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2038 
2039 		ctxdesc = txdesc + ((idx + i + 2) & (mask));
2040 		ctxbi = txbi + ((idx + i + 2) & (mask));
2041 
2042 		ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi,
2043 				      skb_frag_page(frag), skb_frag_off(frag),
2044 				      skb_frag_size(frag), hidma);
2045 		if (ret) {
2046 			jme_drop_tx_map(jme, idx, i);
2047 			goto out;
2048 		}
2049 	}
2050 
2051 	len = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
2052 	ctxdesc = txdesc + ((idx + 1) & (mask));
2053 	ctxbi = txbi + ((idx + 1) & (mask));
2054 	ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi, virt_to_page(skb->data),
2055 			offset_in_page(skb->data), len, hidma);
2056 	if (ret)
2057 		jme_drop_tx_map(jme, idx, i);
2058 
2059 out:
2060 	return ret;
2061 
2062 }
2063 
2064 
2065 static int
2066 jme_tx_tso(struct sk_buff *skb, __le16 *mss, u8 *flags)
2067 {
2068 	*mss = cpu_to_le16(skb_shinfo(skb)->gso_size << TXDESC_MSS_SHIFT);
2069 	if (*mss) {
2070 		*flags |= TXFLAG_LSEN;
2071 
2072 		if (skb->protocol == htons(ETH_P_IP)) {
2073 			struct iphdr *iph = ip_hdr(skb);
2074 
2075 			iph->check = 0;
2076 			tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2077 								iph->daddr, 0,
2078 								IPPROTO_TCP,
2079 								0);
2080 		} else {
2081 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
2082 
2083 			tcp_hdr(skb)->check = ~csum_ipv6_magic(&ip6h->saddr,
2084 								&ip6h->daddr, 0,
2085 								IPPROTO_TCP,
2086 								0);
2087 		}
2088 
2089 		return 0;
2090 	}
2091 
2092 	return 1;
2093 }
2094 
2095 static void
2096 jme_tx_csum(struct jme_adapter *jme, struct sk_buff *skb, u8 *flags)
2097 {
2098 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2099 		u8 ip_proto;
2100 
2101 		switch (skb->protocol) {
2102 		case htons(ETH_P_IP):
2103 			ip_proto = ip_hdr(skb)->protocol;
2104 			break;
2105 		case htons(ETH_P_IPV6):
2106 			ip_proto = ipv6_hdr(skb)->nexthdr;
2107 			break;
2108 		default:
2109 			ip_proto = 0;
2110 			break;
2111 		}
2112 
2113 		switch (ip_proto) {
2114 		case IPPROTO_TCP:
2115 			*flags |= TXFLAG_TCPCS;
2116 			break;
2117 		case IPPROTO_UDP:
2118 			*flags |= TXFLAG_UDPCS;
2119 			break;
2120 		default:
2121 			netif_err(jme, tx_err, jme->dev, "Error upper layer protocol\n");
2122 			break;
2123 		}
2124 	}
2125 }
2126 
2127 static inline void
2128 jme_tx_vlan(struct sk_buff *skb, __le16 *vlan, u8 *flags)
2129 {
2130 	if (skb_vlan_tag_present(skb)) {
2131 		*flags |= TXFLAG_TAGON;
2132 		*vlan = cpu_to_le16(skb_vlan_tag_get(skb));
2133 	}
2134 }
2135 
2136 static int
2137 jme_fill_tx_desc(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2138 {
2139 	struct jme_ring *txring = &(jme->txring[0]);
2140 	struct txdesc *txdesc;
2141 	struct jme_buffer_info *txbi;
2142 	u8 flags;
2143 	int ret = 0;
2144 
2145 	txdesc = (struct txdesc *)txring->desc + idx;
2146 	txbi = txring->bufinf + idx;
2147 
2148 	txdesc->dw[0] = 0;
2149 	txdesc->dw[1] = 0;
2150 	txdesc->dw[2] = 0;
2151 	txdesc->dw[3] = 0;
2152 	txdesc->desc1.pktsize = cpu_to_le16(skb->len);
2153 	/*
2154 	 * Set OWN bit at final.
2155 	 * When kernel transmit faster than NIC.
2156 	 * And NIC trying to send this descriptor before we tell
2157 	 * it to start sending this TX queue.
2158 	 * Other fields are already filled correctly.
2159 	 */
2160 	wmb();
2161 	flags = TXFLAG_OWN | TXFLAG_INT;
2162 	/*
2163 	 * Set checksum flags while not tso
2164 	 */
2165 	if (jme_tx_tso(skb, &txdesc->desc1.mss, &flags))
2166 		jme_tx_csum(jme, skb, &flags);
2167 	jme_tx_vlan(skb, &txdesc->desc1.vlan, &flags);
2168 	ret = jme_map_tx_skb(jme, skb, idx);
2169 	if (ret)
2170 		return ret;
2171 
2172 	txdesc->desc1.flags = flags;
2173 	/*
2174 	 * Set tx buffer info after telling NIC to send
2175 	 * For better tx_clean timing
2176 	 */
2177 	wmb();
2178 	txbi->nr_desc = skb_shinfo(skb)->nr_frags + 2;
2179 	txbi->skb = skb;
2180 	txbi->len = skb->len;
2181 	txbi->start_xmit = jiffies;
2182 	if (!txbi->start_xmit)
2183 		txbi->start_xmit = (0UL-1);
2184 
2185 	return 0;
2186 }
2187 
2188 static void
2189 jme_stop_queue_if_full(struct jme_adapter *jme)
2190 {
2191 	struct jme_ring *txring = &(jme->txring[0]);
2192 	struct jme_buffer_info *txbi = txring->bufinf;
2193 	int idx = atomic_read(&txring->next_to_clean);
2194 
2195 	txbi += idx;
2196 
2197 	smp_wmb();
2198 	if (unlikely(atomic_read(&txring->nr_free) < (MAX_SKB_FRAGS+2))) {
2199 		netif_stop_queue(jme->dev);
2200 		netif_info(jme, tx_queued, jme->dev, "TX Queue Paused\n");
2201 		smp_wmb();
2202 		if (atomic_read(&txring->nr_free)
2203 			>= (jme->tx_wake_threshold)) {
2204 			netif_wake_queue(jme->dev);
2205 			netif_info(jme, tx_queued, jme->dev, "TX Queue Fast Waked\n");
2206 		}
2207 	}
2208 
2209 	if (unlikely(txbi->start_xmit &&
2210 			(jiffies - txbi->start_xmit) >= TX_TIMEOUT &&
2211 			txbi->skb)) {
2212 		netif_stop_queue(jme->dev);
2213 		netif_info(jme, tx_queued, jme->dev,
2214 			   "TX Queue Stopped %d@%lu\n", idx, jiffies);
2215 	}
2216 }
2217 
2218 /*
2219  * This function is already protected by netif_tx_lock()
2220  */
2221 
2222 static netdev_tx_t
2223 jme_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2224 {
2225 	struct jme_adapter *jme = netdev_priv(netdev);
2226 	int idx;
2227 
2228 	if (unlikely(skb_is_gso(skb) && skb_cow_head(skb, 0))) {
2229 		dev_kfree_skb_any(skb);
2230 		++(NET_STAT(jme).tx_dropped);
2231 		return NETDEV_TX_OK;
2232 	}
2233 
2234 	idx = jme_alloc_txdesc(jme, skb);
2235 
2236 	if (unlikely(idx < 0)) {
2237 		netif_stop_queue(netdev);
2238 		netif_err(jme, tx_err, jme->dev,
2239 			  "BUG! Tx ring full when queue awake!\n");
2240 
2241 		return NETDEV_TX_BUSY;
2242 	}
2243 
2244 	if (jme_fill_tx_desc(jme, skb, idx))
2245 		return NETDEV_TX_OK;
2246 
2247 	jwrite32(jme, JME_TXCS, jme->reg_txcs |
2248 				TXCS_SELECT_QUEUE0 |
2249 				TXCS_QUEUE0S |
2250 				TXCS_ENABLE);
2251 
2252 	tx_dbg(jme, "xmit: %d+%d@%lu\n",
2253 	       idx, skb_shinfo(skb)->nr_frags + 2, jiffies);
2254 	jme_stop_queue_if_full(jme);
2255 
2256 	return NETDEV_TX_OK;
2257 }
2258 
2259 static void
2260 jme_set_unicastaddr(struct net_device *netdev)
2261 {
2262 	struct jme_adapter *jme = netdev_priv(netdev);
2263 	u32 val;
2264 
2265 	val = (netdev->dev_addr[3] & 0xff) << 24 |
2266 	      (netdev->dev_addr[2] & 0xff) << 16 |
2267 	      (netdev->dev_addr[1] & 0xff) <<  8 |
2268 	      (netdev->dev_addr[0] & 0xff);
2269 	jwrite32(jme, JME_RXUMA_LO, val);
2270 	val = (netdev->dev_addr[5] & 0xff) << 8 |
2271 	      (netdev->dev_addr[4] & 0xff);
2272 	jwrite32(jme, JME_RXUMA_HI, val);
2273 }
2274 
2275 static int
2276 jme_set_macaddr(struct net_device *netdev, void *p)
2277 {
2278 	struct jme_adapter *jme = netdev_priv(netdev);
2279 	struct sockaddr *addr = p;
2280 
2281 	if (netif_running(netdev))
2282 		return -EBUSY;
2283 
2284 	spin_lock_bh(&jme->macaddr_lock);
2285 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2286 	jme_set_unicastaddr(netdev);
2287 	spin_unlock_bh(&jme->macaddr_lock);
2288 
2289 	return 0;
2290 }
2291 
2292 static void
2293 jme_set_multi(struct net_device *netdev)
2294 {
2295 	struct jme_adapter *jme = netdev_priv(netdev);
2296 	u32 mc_hash[2] = {};
2297 
2298 	spin_lock_bh(&jme->rxmcs_lock);
2299 
2300 	jme->reg_rxmcs |= RXMCS_BRDFRAME | RXMCS_UNIFRAME;
2301 
2302 	if (netdev->flags & IFF_PROMISC) {
2303 		jme->reg_rxmcs |= RXMCS_ALLFRAME;
2304 	} else if (netdev->flags & IFF_ALLMULTI) {
2305 		jme->reg_rxmcs |= RXMCS_ALLMULFRAME;
2306 	} else if (netdev->flags & IFF_MULTICAST) {
2307 		struct netdev_hw_addr *ha;
2308 		int bit_nr;
2309 
2310 		jme->reg_rxmcs |= RXMCS_MULFRAME | RXMCS_MULFILTERED;
2311 		netdev_for_each_mc_addr(ha, netdev) {
2312 			bit_nr = ether_crc(ETH_ALEN, ha->addr) & 0x3F;
2313 			mc_hash[bit_nr >> 5] |= 1 << (bit_nr & 0x1F);
2314 		}
2315 
2316 		jwrite32(jme, JME_RXMCHT_LO, mc_hash[0]);
2317 		jwrite32(jme, JME_RXMCHT_HI, mc_hash[1]);
2318 	}
2319 
2320 	wmb();
2321 	jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2322 
2323 	spin_unlock_bh(&jme->rxmcs_lock);
2324 }
2325 
2326 static int
2327 jme_change_mtu(struct net_device *netdev, int new_mtu)
2328 {
2329 	struct jme_adapter *jme = netdev_priv(netdev);
2330 
2331 	netdev->mtu = new_mtu;
2332 	netdev_update_features(netdev);
2333 
2334 	jme_restart_rx_engine(jme);
2335 	jme_reset_link(jme);
2336 
2337 	return 0;
2338 }
2339 
2340 static void
2341 jme_tx_timeout(struct net_device *netdev)
2342 {
2343 	struct jme_adapter *jme = netdev_priv(netdev);
2344 
2345 	jme->phylink = 0;
2346 	jme_reset_phy_processor(jme);
2347 	if (test_bit(JME_FLAG_SSET, &jme->flags))
2348 		jme_set_link_ksettings(netdev, &jme->old_cmd);
2349 
2350 	/*
2351 	 * Force to Reset the link again
2352 	 */
2353 	jme_reset_link(jme);
2354 }
2355 
2356 static void
2357 jme_get_drvinfo(struct net_device *netdev,
2358 		     struct ethtool_drvinfo *info)
2359 {
2360 	struct jme_adapter *jme = netdev_priv(netdev);
2361 
2362 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2363 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2364 	strlcpy(info->bus_info, pci_name(jme->pdev), sizeof(info->bus_info));
2365 }
2366 
2367 static int
2368 jme_get_regs_len(struct net_device *netdev)
2369 {
2370 	return JME_REG_LEN;
2371 }
2372 
2373 static void
2374 mmapio_memcpy(struct jme_adapter *jme, u32 *p, u32 reg, int len)
2375 {
2376 	int i;
2377 
2378 	for (i = 0 ; i < len ; i += 4)
2379 		p[i >> 2] = jread32(jme, reg + i);
2380 }
2381 
2382 static void
2383 mdio_memcpy(struct jme_adapter *jme, u32 *p, int reg_nr)
2384 {
2385 	int i;
2386 	u16 *p16 = (u16 *)p;
2387 
2388 	for (i = 0 ; i < reg_nr ; ++i)
2389 		p16[i] = jme_mdio_read(jme->dev, jme->mii_if.phy_id, i);
2390 }
2391 
2392 static void
2393 jme_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
2394 {
2395 	struct jme_adapter *jme = netdev_priv(netdev);
2396 	u32 *p32 = (u32 *)p;
2397 
2398 	memset(p, 0xFF, JME_REG_LEN);
2399 
2400 	regs->version = 1;
2401 	mmapio_memcpy(jme, p32, JME_MAC, JME_MAC_LEN);
2402 
2403 	p32 += 0x100 >> 2;
2404 	mmapio_memcpy(jme, p32, JME_PHY, JME_PHY_LEN);
2405 
2406 	p32 += 0x100 >> 2;
2407 	mmapio_memcpy(jme, p32, JME_MISC, JME_MISC_LEN);
2408 
2409 	p32 += 0x100 >> 2;
2410 	mmapio_memcpy(jme, p32, JME_RSS, JME_RSS_LEN);
2411 
2412 	p32 += 0x100 >> 2;
2413 	mdio_memcpy(jme, p32, JME_PHY_REG_NR);
2414 }
2415 
2416 static int
2417 jme_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2418 {
2419 	struct jme_adapter *jme = netdev_priv(netdev);
2420 
2421 	ecmd->tx_coalesce_usecs = PCC_TX_TO;
2422 	ecmd->tx_max_coalesced_frames = PCC_TX_CNT;
2423 
2424 	if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2425 		ecmd->use_adaptive_rx_coalesce = false;
2426 		ecmd->rx_coalesce_usecs = 0;
2427 		ecmd->rx_max_coalesced_frames = 0;
2428 		return 0;
2429 	}
2430 
2431 	ecmd->use_adaptive_rx_coalesce = true;
2432 
2433 	switch (jme->dpi.cur) {
2434 	case PCC_P1:
2435 		ecmd->rx_coalesce_usecs = PCC_P1_TO;
2436 		ecmd->rx_max_coalesced_frames = PCC_P1_CNT;
2437 		break;
2438 	case PCC_P2:
2439 		ecmd->rx_coalesce_usecs = PCC_P2_TO;
2440 		ecmd->rx_max_coalesced_frames = PCC_P2_CNT;
2441 		break;
2442 	case PCC_P3:
2443 		ecmd->rx_coalesce_usecs = PCC_P3_TO;
2444 		ecmd->rx_max_coalesced_frames = PCC_P3_CNT;
2445 		break;
2446 	default:
2447 		break;
2448 	}
2449 
2450 	return 0;
2451 }
2452 
2453 static int
2454 jme_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2455 {
2456 	struct jme_adapter *jme = netdev_priv(netdev);
2457 	struct dynpcc_info *dpi = &(jme->dpi);
2458 
2459 	if (netif_running(netdev))
2460 		return -EBUSY;
2461 
2462 	if (ecmd->use_adaptive_rx_coalesce &&
2463 	    test_bit(JME_FLAG_POLL, &jme->flags)) {
2464 		clear_bit(JME_FLAG_POLL, &jme->flags);
2465 		jme->jme_rx = netif_rx;
2466 		dpi->cur		= PCC_P1;
2467 		dpi->attempt		= PCC_P1;
2468 		dpi->cnt		= 0;
2469 		jme_set_rx_pcc(jme, PCC_P1);
2470 		jme_interrupt_mode(jme);
2471 	} else if (!(ecmd->use_adaptive_rx_coalesce) &&
2472 		   !(test_bit(JME_FLAG_POLL, &jme->flags))) {
2473 		set_bit(JME_FLAG_POLL, &jme->flags);
2474 		jme->jme_rx = netif_receive_skb;
2475 		jme_interrupt_mode(jme);
2476 	}
2477 
2478 	return 0;
2479 }
2480 
2481 static void
2482 jme_get_pauseparam(struct net_device *netdev,
2483 			struct ethtool_pauseparam *ecmd)
2484 {
2485 	struct jme_adapter *jme = netdev_priv(netdev);
2486 	u32 val;
2487 
2488 	ecmd->tx_pause = (jme->reg_txpfc & TXPFC_PF_EN) != 0;
2489 	ecmd->rx_pause = (jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0;
2490 
2491 	spin_lock_bh(&jme->phy_lock);
2492 	val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2493 	spin_unlock_bh(&jme->phy_lock);
2494 
2495 	ecmd->autoneg =
2496 		(val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0;
2497 }
2498 
2499 static int
2500 jme_set_pauseparam(struct net_device *netdev,
2501 			struct ethtool_pauseparam *ecmd)
2502 {
2503 	struct jme_adapter *jme = netdev_priv(netdev);
2504 	u32 val;
2505 
2506 	if (((jme->reg_txpfc & TXPFC_PF_EN) != 0) ^
2507 		(ecmd->tx_pause != 0)) {
2508 
2509 		if (ecmd->tx_pause)
2510 			jme->reg_txpfc |= TXPFC_PF_EN;
2511 		else
2512 			jme->reg_txpfc &= ~TXPFC_PF_EN;
2513 
2514 		jwrite32(jme, JME_TXPFC, jme->reg_txpfc);
2515 	}
2516 
2517 	spin_lock_bh(&jme->rxmcs_lock);
2518 	if (((jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0) ^
2519 		(ecmd->rx_pause != 0)) {
2520 
2521 		if (ecmd->rx_pause)
2522 			jme->reg_rxmcs |= RXMCS_FLOWCTRL;
2523 		else
2524 			jme->reg_rxmcs &= ~RXMCS_FLOWCTRL;
2525 
2526 		jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2527 	}
2528 	spin_unlock_bh(&jme->rxmcs_lock);
2529 
2530 	spin_lock_bh(&jme->phy_lock);
2531 	val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2532 	if (((val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0) ^
2533 		(ecmd->autoneg != 0)) {
2534 
2535 		if (ecmd->autoneg)
2536 			val |= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2537 		else
2538 			val &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2539 
2540 		jme_mdio_write(jme->dev, jme->mii_if.phy_id,
2541 				MII_ADVERTISE, val);
2542 	}
2543 	spin_unlock_bh(&jme->phy_lock);
2544 
2545 	return 0;
2546 }
2547 
2548 static void
2549 jme_get_wol(struct net_device *netdev,
2550 		struct ethtool_wolinfo *wol)
2551 {
2552 	struct jme_adapter *jme = netdev_priv(netdev);
2553 
2554 	wol->supported = WAKE_MAGIC | WAKE_PHY;
2555 
2556 	wol->wolopts = 0;
2557 
2558 	if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
2559 		wol->wolopts |= WAKE_PHY;
2560 
2561 	if (jme->reg_pmcs & PMCS_MFEN)
2562 		wol->wolopts |= WAKE_MAGIC;
2563 
2564 }
2565 
2566 static int
2567 jme_set_wol(struct net_device *netdev,
2568 		struct ethtool_wolinfo *wol)
2569 {
2570 	struct jme_adapter *jme = netdev_priv(netdev);
2571 
2572 	if (wol->wolopts & (WAKE_MAGICSECURE |
2573 				WAKE_UCAST |
2574 				WAKE_MCAST |
2575 				WAKE_BCAST |
2576 				WAKE_ARP))
2577 		return -EOPNOTSUPP;
2578 
2579 	jme->reg_pmcs = 0;
2580 
2581 	if (wol->wolopts & WAKE_PHY)
2582 		jme->reg_pmcs |= PMCS_LFEN | PMCS_LREN;
2583 
2584 	if (wol->wolopts & WAKE_MAGIC)
2585 		jme->reg_pmcs |= PMCS_MFEN;
2586 
2587 	return 0;
2588 }
2589 
2590 static int
2591 jme_get_link_ksettings(struct net_device *netdev,
2592 		       struct ethtool_link_ksettings *cmd)
2593 {
2594 	struct jme_adapter *jme = netdev_priv(netdev);
2595 
2596 	spin_lock_bh(&jme->phy_lock);
2597 	mii_ethtool_get_link_ksettings(&jme->mii_if, cmd);
2598 	spin_unlock_bh(&jme->phy_lock);
2599 	return 0;
2600 }
2601 
2602 static int
2603 jme_set_link_ksettings(struct net_device *netdev,
2604 		       const struct ethtool_link_ksettings *cmd)
2605 {
2606 	struct jme_adapter *jme = netdev_priv(netdev);
2607 	int rc, fdc = 0;
2608 
2609 	if (cmd->base.speed == SPEED_1000 &&
2610 	    cmd->base.autoneg != AUTONEG_ENABLE)
2611 		return -EINVAL;
2612 
2613 	/*
2614 	 * Check If user changed duplex only while force_media.
2615 	 * Hardware would not generate link change interrupt.
2616 	 */
2617 	if (jme->mii_if.force_media &&
2618 	    cmd->base.autoneg != AUTONEG_ENABLE &&
2619 	    (jme->mii_if.full_duplex != cmd->base.duplex))
2620 		fdc = 1;
2621 
2622 	spin_lock_bh(&jme->phy_lock);
2623 	rc = mii_ethtool_set_link_ksettings(&jme->mii_if, cmd);
2624 	spin_unlock_bh(&jme->phy_lock);
2625 
2626 	if (!rc) {
2627 		if (fdc)
2628 			jme_reset_link(jme);
2629 		jme->old_cmd = *cmd;
2630 		set_bit(JME_FLAG_SSET, &jme->flags);
2631 	}
2632 
2633 	return rc;
2634 }
2635 
2636 static int
2637 jme_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2638 {
2639 	int rc;
2640 	struct jme_adapter *jme = netdev_priv(netdev);
2641 	struct mii_ioctl_data *mii_data = if_mii(rq);
2642 	unsigned int duplex_chg;
2643 
2644 	if (cmd == SIOCSMIIREG) {
2645 		u16 val = mii_data->val_in;
2646 		if (!(val & (BMCR_RESET|BMCR_ANENABLE)) &&
2647 		    (val & BMCR_SPEED1000))
2648 			return -EINVAL;
2649 	}
2650 
2651 	spin_lock_bh(&jme->phy_lock);
2652 	rc = generic_mii_ioctl(&jme->mii_if, mii_data, cmd, &duplex_chg);
2653 	spin_unlock_bh(&jme->phy_lock);
2654 
2655 	if (!rc && (cmd == SIOCSMIIREG)) {
2656 		if (duplex_chg)
2657 			jme_reset_link(jme);
2658 		jme_get_link_ksettings(netdev, &jme->old_cmd);
2659 		set_bit(JME_FLAG_SSET, &jme->flags);
2660 	}
2661 
2662 	return rc;
2663 }
2664 
2665 static u32
2666 jme_get_link(struct net_device *netdev)
2667 {
2668 	struct jme_adapter *jme = netdev_priv(netdev);
2669 	return jread32(jme, JME_PHY_LINK) & PHY_LINK_UP;
2670 }
2671 
2672 static u32
2673 jme_get_msglevel(struct net_device *netdev)
2674 {
2675 	struct jme_adapter *jme = netdev_priv(netdev);
2676 	return jme->msg_enable;
2677 }
2678 
2679 static void
2680 jme_set_msglevel(struct net_device *netdev, u32 value)
2681 {
2682 	struct jme_adapter *jme = netdev_priv(netdev);
2683 	jme->msg_enable = value;
2684 }
2685 
2686 static netdev_features_t
2687 jme_fix_features(struct net_device *netdev, netdev_features_t features)
2688 {
2689 	if (netdev->mtu > 1900)
2690 		features &= ~(NETIF_F_ALL_TSO | NETIF_F_CSUM_MASK);
2691 	return features;
2692 }
2693 
2694 static int
2695 jme_set_features(struct net_device *netdev, netdev_features_t features)
2696 {
2697 	struct jme_adapter *jme = netdev_priv(netdev);
2698 
2699 	spin_lock_bh(&jme->rxmcs_lock);
2700 	if (features & NETIF_F_RXCSUM)
2701 		jme->reg_rxmcs |= RXMCS_CHECKSUM;
2702 	else
2703 		jme->reg_rxmcs &= ~RXMCS_CHECKSUM;
2704 	jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2705 	spin_unlock_bh(&jme->rxmcs_lock);
2706 
2707 	return 0;
2708 }
2709 
2710 #ifdef CONFIG_NET_POLL_CONTROLLER
2711 static void jme_netpoll(struct net_device *dev)
2712 {
2713 	unsigned long flags;
2714 
2715 	local_irq_save(flags);
2716 	jme_intr(dev->irq, dev);
2717 	local_irq_restore(flags);
2718 }
2719 #endif
2720 
2721 static int
2722 jme_nway_reset(struct net_device *netdev)
2723 {
2724 	struct jme_adapter *jme = netdev_priv(netdev);
2725 	jme_restart_an(jme);
2726 	return 0;
2727 }
2728 
2729 static u8
2730 jme_smb_read(struct jme_adapter *jme, unsigned int addr)
2731 {
2732 	u32 val;
2733 	int to;
2734 
2735 	val = jread32(jme, JME_SMBCSR);
2736 	to = JME_SMB_BUSY_TIMEOUT;
2737 	while ((val & SMBCSR_BUSY) && --to) {
2738 		msleep(1);
2739 		val = jread32(jme, JME_SMBCSR);
2740 	}
2741 	if (!to) {
2742 		netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2743 		return 0xFF;
2744 	}
2745 
2746 	jwrite32(jme, JME_SMBINTF,
2747 		((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2748 		SMBINTF_HWRWN_READ |
2749 		SMBINTF_HWCMD);
2750 
2751 	val = jread32(jme, JME_SMBINTF);
2752 	to = JME_SMB_BUSY_TIMEOUT;
2753 	while ((val & SMBINTF_HWCMD) && --to) {
2754 		msleep(1);
2755 		val = jread32(jme, JME_SMBINTF);
2756 	}
2757 	if (!to) {
2758 		netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2759 		return 0xFF;
2760 	}
2761 
2762 	return (val & SMBINTF_HWDATR) >> SMBINTF_HWDATR_SHIFT;
2763 }
2764 
2765 static void
2766 jme_smb_write(struct jme_adapter *jme, unsigned int addr, u8 data)
2767 {
2768 	u32 val;
2769 	int to;
2770 
2771 	val = jread32(jme, JME_SMBCSR);
2772 	to = JME_SMB_BUSY_TIMEOUT;
2773 	while ((val & SMBCSR_BUSY) && --to) {
2774 		msleep(1);
2775 		val = jread32(jme, JME_SMBCSR);
2776 	}
2777 	if (!to) {
2778 		netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2779 		return;
2780 	}
2781 
2782 	jwrite32(jme, JME_SMBINTF,
2783 		((data << SMBINTF_HWDATW_SHIFT) & SMBINTF_HWDATW) |
2784 		((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2785 		SMBINTF_HWRWN_WRITE |
2786 		SMBINTF_HWCMD);
2787 
2788 	val = jread32(jme, JME_SMBINTF);
2789 	to = JME_SMB_BUSY_TIMEOUT;
2790 	while ((val & SMBINTF_HWCMD) && --to) {
2791 		msleep(1);
2792 		val = jread32(jme, JME_SMBINTF);
2793 	}
2794 	if (!to) {
2795 		netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2796 		return;
2797 	}
2798 
2799 	mdelay(2);
2800 }
2801 
2802 static int
2803 jme_get_eeprom_len(struct net_device *netdev)
2804 {
2805 	struct jme_adapter *jme = netdev_priv(netdev);
2806 	u32 val;
2807 	val = jread32(jme, JME_SMBCSR);
2808 	return (val & SMBCSR_EEPROMD) ? JME_SMB_LEN : 0;
2809 }
2810 
2811 static int
2812 jme_get_eeprom(struct net_device *netdev,
2813 		struct ethtool_eeprom *eeprom, u8 *data)
2814 {
2815 	struct jme_adapter *jme = netdev_priv(netdev);
2816 	int i, offset = eeprom->offset, len = eeprom->len;
2817 
2818 	/*
2819 	 * ethtool will check the boundary for us
2820 	 */
2821 	eeprom->magic = JME_EEPROM_MAGIC;
2822 	for (i = 0 ; i < len ; ++i)
2823 		data[i] = jme_smb_read(jme, i + offset);
2824 
2825 	return 0;
2826 }
2827 
2828 static int
2829 jme_set_eeprom(struct net_device *netdev,
2830 		struct ethtool_eeprom *eeprom, u8 *data)
2831 {
2832 	struct jme_adapter *jme = netdev_priv(netdev);
2833 	int i, offset = eeprom->offset, len = eeprom->len;
2834 
2835 	if (eeprom->magic != JME_EEPROM_MAGIC)
2836 		return -EINVAL;
2837 
2838 	/*
2839 	 * ethtool will check the boundary for us
2840 	 */
2841 	for (i = 0 ; i < len ; ++i)
2842 		jme_smb_write(jme, i + offset, data[i]);
2843 
2844 	return 0;
2845 }
2846 
2847 static const struct ethtool_ops jme_ethtool_ops = {
2848 	.get_drvinfo            = jme_get_drvinfo,
2849 	.get_regs_len		= jme_get_regs_len,
2850 	.get_regs		= jme_get_regs,
2851 	.get_coalesce		= jme_get_coalesce,
2852 	.set_coalesce		= jme_set_coalesce,
2853 	.get_pauseparam		= jme_get_pauseparam,
2854 	.set_pauseparam		= jme_set_pauseparam,
2855 	.get_wol		= jme_get_wol,
2856 	.set_wol		= jme_set_wol,
2857 	.get_link		= jme_get_link,
2858 	.get_msglevel           = jme_get_msglevel,
2859 	.set_msglevel           = jme_set_msglevel,
2860 	.nway_reset             = jme_nway_reset,
2861 	.get_eeprom_len		= jme_get_eeprom_len,
2862 	.get_eeprom		= jme_get_eeprom,
2863 	.set_eeprom		= jme_set_eeprom,
2864 	.get_link_ksettings	= jme_get_link_ksettings,
2865 	.set_link_ksettings	= jme_set_link_ksettings,
2866 };
2867 
2868 static int
2869 jme_pci_dma64(struct pci_dev *pdev)
2870 {
2871 	if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
2872 	    !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
2873 		if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2874 			return 1;
2875 
2876 	if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
2877 	    !pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2878 		if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40)))
2879 			return 1;
2880 
2881 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
2882 		if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2883 			return 0;
2884 
2885 	return -1;
2886 }
2887 
2888 static inline void
2889 jme_phy_init(struct jme_adapter *jme)
2890 {
2891 	u16 reg26;
2892 
2893 	reg26 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 26);
2894 	jme_mdio_write(jme->dev, jme->mii_if.phy_id, 26, reg26 | 0x1000);
2895 }
2896 
2897 static inline void
2898 jme_check_hw_ver(struct jme_adapter *jme)
2899 {
2900 	u32 chipmode;
2901 
2902 	chipmode = jread32(jme, JME_CHIPMODE);
2903 
2904 	jme->fpgaver = (chipmode & CM_FPGAVER_MASK) >> CM_FPGAVER_SHIFT;
2905 	jme->chiprev = (chipmode & CM_CHIPREV_MASK) >> CM_CHIPREV_SHIFT;
2906 	jme->chip_main_rev = jme->chiprev & 0xF;
2907 	jme->chip_sub_rev = (jme->chiprev >> 4) & 0xF;
2908 }
2909 
2910 static const struct net_device_ops jme_netdev_ops = {
2911 	.ndo_open		= jme_open,
2912 	.ndo_stop		= jme_close,
2913 	.ndo_validate_addr	= eth_validate_addr,
2914 	.ndo_do_ioctl		= jme_ioctl,
2915 	.ndo_start_xmit		= jme_start_xmit,
2916 	.ndo_set_mac_address	= jme_set_macaddr,
2917 	.ndo_set_rx_mode	= jme_set_multi,
2918 	.ndo_change_mtu		= jme_change_mtu,
2919 	.ndo_tx_timeout		= jme_tx_timeout,
2920 	.ndo_fix_features       = jme_fix_features,
2921 	.ndo_set_features       = jme_set_features,
2922 #ifdef CONFIG_NET_POLL_CONTROLLER
2923 	.ndo_poll_controller	= jme_netpoll,
2924 #endif
2925 };
2926 
2927 static int
2928 jme_init_one(struct pci_dev *pdev,
2929 	     const struct pci_device_id *ent)
2930 {
2931 	int rc = 0, using_dac, i;
2932 	struct net_device *netdev;
2933 	struct jme_adapter *jme;
2934 	u16 bmcr, bmsr;
2935 	u32 apmc;
2936 
2937 	/*
2938 	 * set up PCI device basics
2939 	 */
2940 	pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
2941 			       PCIE_LINK_STATE_CLKPM);
2942 
2943 	rc = pci_enable_device(pdev);
2944 	if (rc) {
2945 		pr_err("Cannot enable PCI device\n");
2946 		goto err_out;
2947 	}
2948 
2949 	using_dac = jme_pci_dma64(pdev);
2950 	if (using_dac < 0) {
2951 		pr_err("Cannot set PCI DMA Mask\n");
2952 		rc = -EIO;
2953 		goto err_out_disable_pdev;
2954 	}
2955 
2956 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2957 		pr_err("No PCI resource region found\n");
2958 		rc = -ENOMEM;
2959 		goto err_out_disable_pdev;
2960 	}
2961 
2962 	rc = pci_request_regions(pdev, DRV_NAME);
2963 	if (rc) {
2964 		pr_err("Cannot obtain PCI resource region\n");
2965 		goto err_out_disable_pdev;
2966 	}
2967 
2968 	pci_set_master(pdev);
2969 
2970 	/*
2971 	 * alloc and init net device
2972 	 */
2973 	netdev = alloc_etherdev(sizeof(*jme));
2974 	if (!netdev) {
2975 		rc = -ENOMEM;
2976 		goto err_out_release_regions;
2977 	}
2978 	netdev->netdev_ops = &jme_netdev_ops;
2979 	netdev->ethtool_ops		= &jme_ethtool_ops;
2980 	netdev->watchdog_timeo		= TX_TIMEOUT;
2981 	netdev->hw_features		=	NETIF_F_IP_CSUM |
2982 						NETIF_F_IPV6_CSUM |
2983 						NETIF_F_SG |
2984 						NETIF_F_TSO |
2985 						NETIF_F_TSO6 |
2986 						NETIF_F_RXCSUM;
2987 	netdev->features		=	NETIF_F_IP_CSUM |
2988 						NETIF_F_IPV6_CSUM |
2989 						NETIF_F_SG |
2990 						NETIF_F_TSO |
2991 						NETIF_F_TSO6 |
2992 						NETIF_F_HW_VLAN_CTAG_TX |
2993 						NETIF_F_HW_VLAN_CTAG_RX;
2994 	if (using_dac)
2995 		netdev->features	|=	NETIF_F_HIGHDMA;
2996 
2997 	/* MTU range: 1280 - 9202*/
2998 	netdev->min_mtu = IPV6_MIN_MTU;
2999 	netdev->max_mtu = MAX_ETHERNET_JUMBO_PACKET_SIZE - ETH_HLEN;
3000 
3001 	SET_NETDEV_DEV(netdev, &pdev->dev);
3002 	pci_set_drvdata(pdev, netdev);
3003 
3004 	/*
3005 	 * init adapter info
3006 	 */
3007 	jme = netdev_priv(netdev);
3008 	jme->pdev = pdev;
3009 	jme->dev = netdev;
3010 	jme->jme_rx = netif_rx;
3011 	jme->old_mtu = netdev->mtu = 1500;
3012 	jme->phylink = 0;
3013 	jme->tx_ring_size = 1 << 10;
3014 	jme->tx_ring_mask = jme->tx_ring_size - 1;
3015 	jme->tx_wake_threshold = 1 << 9;
3016 	jme->rx_ring_size = 1 << 9;
3017 	jme->rx_ring_mask = jme->rx_ring_size - 1;
3018 	jme->msg_enable = JME_DEF_MSG_ENABLE;
3019 	jme->regs = ioremap(pci_resource_start(pdev, 0),
3020 			     pci_resource_len(pdev, 0));
3021 	if (!(jme->regs)) {
3022 		pr_err("Mapping PCI resource region error\n");
3023 		rc = -ENOMEM;
3024 		goto err_out_free_netdev;
3025 	}
3026 
3027 	if (no_pseudohp) {
3028 		apmc = jread32(jme, JME_APMC) & ~JME_APMC_PSEUDO_HP_EN;
3029 		jwrite32(jme, JME_APMC, apmc);
3030 	} else if (force_pseudohp) {
3031 		apmc = jread32(jme, JME_APMC) | JME_APMC_PSEUDO_HP_EN;
3032 		jwrite32(jme, JME_APMC, apmc);
3033 	}
3034 
3035 	NETIF_NAPI_SET(netdev, &jme->napi, jme_poll, NAPI_POLL_WEIGHT)
3036 
3037 	spin_lock_init(&jme->phy_lock);
3038 	spin_lock_init(&jme->macaddr_lock);
3039 	spin_lock_init(&jme->rxmcs_lock);
3040 
3041 	atomic_set(&jme->link_changing, 1);
3042 	atomic_set(&jme->rx_cleaning, 1);
3043 	atomic_set(&jme->tx_cleaning, 1);
3044 	atomic_set(&jme->rx_empty, 1);
3045 
3046 	tasklet_init(&jme->pcc_task,
3047 		     jme_pcc_tasklet,
3048 		     (unsigned long) jme);
3049 	jme->dpi.cur = PCC_P1;
3050 
3051 	jme->reg_ghc = 0;
3052 	jme->reg_rxcs = RXCS_DEFAULT;
3053 	jme->reg_rxmcs = RXMCS_DEFAULT;
3054 	jme->reg_txpfc = 0;
3055 	jme->reg_pmcs = PMCS_MFEN;
3056 	jme->reg_gpreg1 = GPREG1_DEFAULT;
3057 
3058 	if (jme->reg_rxmcs & RXMCS_CHECKSUM)
3059 		netdev->features |= NETIF_F_RXCSUM;
3060 
3061 	/*
3062 	 * Get Max Read Req Size from PCI Config Space
3063 	 */
3064 	pci_read_config_byte(pdev, PCI_DCSR_MRRS, &jme->mrrs);
3065 	jme->mrrs &= PCI_DCSR_MRRS_MASK;
3066 	switch (jme->mrrs) {
3067 	case MRRS_128B:
3068 		jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_128B;
3069 		break;
3070 	case MRRS_256B:
3071 		jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_256B;
3072 		break;
3073 	default:
3074 		jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_512B;
3075 		break;
3076 	}
3077 
3078 	/*
3079 	 * Must check before reset_mac_processor
3080 	 */
3081 	jme_check_hw_ver(jme);
3082 	jme->mii_if.dev = netdev;
3083 	if (jme->fpgaver) {
3084 		jme->mii_if.phy_id = 0;
3085 		for (i = 1 ; i < 32 ; ++i) {
3086 			bmcr = jme_mdio_read(netdev, i, MII_BMCR);
3087 			bmsr = jme_mdio_read(netdev, i, MII_BMSR);
3088 			if (bmcr != 0xFFFFU && (bmcr != 0 || bmsr != 0)) {
3089 				jme->mii_if.phy_id = i;
3090 				break;
3091 			}
3092 		}
3093 
3094 		if (!jme->mii_if.phy_id) {
3095 			rc = -EIO;
3096 			pr_err("Can not find phy_id\n");
3097 			goto err_out_unmap;
3098 		}
3099 
3100 		jme->reg_ghc |= GHC_LINK_POLL;
3101 	} else {
3102 		jme->mii_if.phy_id = 1;
3103 	}
3104 	if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
3105 		jme->mii_if.supports_gmii = true;
3106 	else
3107 		jme->mii_if.supports_gmii = false;
3108 	jme->mii_if.phy_id_mask = 0x1F;
3109 	jme->mii_if.reg_num_mask = 0x1F;
3110 	jme->mii_if.mdio_read = jme_mdio_read;
3111 	jme->mii_if.mdio_write = jme_mdio_write;
3112 
3113 	jme_clear_pm_disable_wol(jme);
3114 	device_init_wakeup(&pdev->dev, true);
3115 
3116 	jme_set_phyfifo_5level(jme);
3117 	jme->pcirev = pdev->revision;
3118 	if (!jme->fpgaver)
3119 		jme_phy_init(jme);
3120 	jme_phy_off(jme);
3121 
3122 	/*
3123 	 * Reset MAC processor and reload EEPROM for MAC Address
3124 	 */
3125 	jme_reset_mac_processor(jme);
3126 	rc = jme_reload_eeprom(jme);
3127 	if (rc) {
3128 		pr_err("Reload eeprom for reading MAC Address error\n");
3129 		goto err_out_unmap;
3130 	}
3131 	jme_load_macaddr(netdev);
3132 
3133 	/*
3134 	 * Tell stack that we are not ready to work until open()
3135 	 */
3136 	netif_carrier_off(netdev);
3137 
3138 	rc = register_netdev(netdev);
3139 	if (rc) {
3140 		pr_err("Cannot register net device\n");
3141 		goto err_out_unmap;
3142 	}
3143 
3144 	netif_info(jme, probe, jme->dev, "%s%s chiprev:%x pcirev:%x macaddr:%pM\n",
3145 		   (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250) ?
3146 		   "JMC250 Gigabit Ethernet" :
3147 		   (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC260) ?
3148 		   "JMC260 Fast Ethernet" : "Unknown",
3149 		   (jme->fpgaver != 0) ? " (FPGA)" : "",
3150 		   (jme->fpgaver != 0) ? jme->fpgaver : jme->chiprev,
3151 		   jme->pcirev, netdev->dev_addr);
3152 
3153 	return 0;
3154 
3155 err_out_unmap:
3156 	iounmap(jme->regs);
3157 err_out_free_netdev:
3158 	free_netdev(netdev);
3159 err_out_release_regions:
3160 	pci_release_regions(pdev);
3161 err_out_disable_pdev:
3162 	pci_disable_device(pdev);
3163 err_out:
3164 	return rc;
3165 }
3166 
3167 static void
3168 jme_remove_one(struct pci_dev *pdev)
3169 {
3170 	struct net_device *netdev = pci_get_drvdata(pdev);
3171 	struct jme_adapter *jme = netdev_priv(netdev);
3172 
3173 	unregister_netdev(netdev);
3174 	iounmap(jme->regs);
3175 	free_netdev(netdev);
3176 	pci_release_regions(pdev);
3177 	pci_disable_device(pdev);
3178 
3179 }
3180 
3181 static void
3182 jme_shutdown(struct pci_dev *pdev)
3183 {
3184 	struct net_device *netdev = pci_get_drvdata(pdev);
3185 	struct jme_adapter *jme = netdev_priv(netdev);
3186 
3187 	jme_powersave_phy(jme);
3188 	pci_pme_active(pdev, true);
3189 }
3190 
3191 #ifdef CONFIG_PM_SLEEP
3192 static int
3193 jme_suspend(struct device *dev)
3194 {
3195 	struct net_device *netdev = dev_get_drvdata(dev);
3196 	struct jme_adapter *jme = netdev_priv(netdev);
3197 
3198 	if (!netif_running(netdev))
3199 		return 0;
3200 
3201 	atomic_dec(&jme->link_changing);
3202 
3203 	netif_device_detach(netdev);
3204 	netif_stop_queue(netdev);
3205 	jme_stop_irq(jme);
3206 
3207 	tasklet_disable(&jme->txclean_task);
3208 	tasklet_disable(&jme->rxclean_task);
3209 	tasklet_disable(&jme->rxempty_task);
3210 
3211 	if (netif_carrier_ok(netdev)) {
3212 		if (test_bit(JME_FLAG_POLL, &jme->flags))
3213 			jme_polling_mode(jme);
3214 
3215 		jme_stop_pcc_timer(jme);
3216 		jme_disable_rx_engine(jme);
3217 		jme_disable_tx_engine(jme);
3218 		jme_reset_mac_processor(jme);
3219 		jme_free_rx_resources(jme);
3220 		jme_free_tx_resources(jme);
3221 		netif_carrier_off(netdev);
3222 		jme->phylink = 0;
3223 	}
3224 
3225 	tasklet_enable(&jme->txclean_task);
3226 	tasklet_enable(&jme->rxclean_task);
3227 	tasklet_enable(&jme->rxempty_task);
3228 
3229 	jme_powersave_phy(jme);
3230 
3231 	return 0;
3232 }
3233 
3234 static int
3235 jme_resume(struct device *dev)
3236 {
3237 	struct net_device *netdev = dev_get_drvdata(dev);
3238 	struct jme_adapter *jme = netdev_priv(netdev);
3239 
3240 	if (!netif_running(netdev))
3241 		return 0;
3242 
3243 	jme_clear_pm_disable_wol(jme);
3244 	jme_phy_on(jme);
3245 	if (test_bit(JME_FLAG_SSET, &jme->flags))
3246 		jme_set_link_ksettings(netdev, &jme->old_cmd);
3247 	else
3248 		jme_reset_phy_processor(jme);
3249 	jme_phy_calibration(jme);
3250 	jme_phy_setEA(jme);
3251 	netif_device_attach(netdev);
3252 
3253 	atomic_inc(&jme->link_changing);
3254 
3255 	jme_reset_link(jme);
3256 
3257 	jme_start_irq(jme);
3258 
3259 	return 0;
3260 }
3261 
3262 static SIMPLE_DEV_PM_OPS(jme_pm_ops, jme_suspend, jme_resume);
3263 #define JME_PM_OPS (&jme_pm_ops)
3264 
3265 #else
3266 
3267 #define JME_PM_OPS NULL
3268 #endif
3269 
3270 static const struct pci_device_id jme_pci_tbl[] = {
3271 	{ PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC250) },
3272 	{ PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC260) },
3273 	{ }
3274 };
3275 
3276 static struct pci_driver jme_driver = {
3277 	.name           = DRV_NAME,
3278 	.id_table       = jme_pci_tbl,
3279 	.probe          = jme_init_one,
3280 	.remove         = jme_remove_one,
3281 	.shutdown       = jme_shutdown,
3282 	.driver.pm	= JME_PM_OPS,
3283 };
3284 
3285 static int __init
3286 jme_init_module(void)
3287 {
3288 	pr_info("JMicron JMC2XX ethernet driver version %s\n", DRV_VERSION);
3289 	return pci_register_driver(&jme_driver);
3290 }
3291 
3292 static void __exit
3293 jme_cleanup_module(void)
3294 {
3295 	pci_unregister_driver(&jme_driver);
3296 }
3297 
3298 module_init(jme_init_module);
3299 module_exit(jme_cleanup_module);
3300 
3301 MODULE_AUTHOR("Guo-Fu Tseng <cooldavid@cooldavid.org>");
3302 MODULE_DESCRIPTION("JMicron JMC2x0 PCI Express Ethernet driver");
3303 MODULE_LICENSE("GPL");
3304 MODULE_VERSION(DRV_VERSION);
3305 MODULE_DEVICE_TABLE(pci, jme_pci_tbl);
3306