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