]> bbs.cooldavid.org Git - jme.git/blob - jme.c
e8afe85cf9a3a0a19640bad2d9ae0d7d003580a2
[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
108 static inline void
109 jme_reset_phy_processor(struct jme_adapter *jme)
110 {
111         u32 val;
112
113         jme_mdio_write(jme->dev,
114                         jme->mii_if.phy_id,
115                         MII_ADVERTISE, ADVERTISE_ALL |
116                         ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
117
118         if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
119                 jme_mdio_write(jme->dev,
120                                 jme->mii_if.phy_id,
121                                 MII_CTRL1000,
122                                 ADVERTISE_1000FULL | ADVERTISE_1000HALF);
123
124         val = jme_mdio_read(jme->dev,
125                                 jme->mii_if.phy_id,
126                                 MII_BMCR);
127
128         jme_mdio_write(jme->dev,
129                         jme->mii_if.phy_id,
130                         MII_BMCR, val | BMCR_RESET);
131 }
132
133 static void
134 jme_setup_wakeup_frame(struct jme_adapter *jme,
135                 u32 *mask, u32 crc, int fnr)
136 {
137         int i;
138
139         /*
140          * Setup CRC pattern
141          */
142         jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
143         wmb();
144         jwrite32(jme, JME_WFODP, crc);
145         wmb();
146
147         /*
148          * Setup Mask
149          */
150         for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
151                 jwrite32(jme, JME_WFOI,
152                                 ((i << WFOI_MASK_SHIFT) & WFOI_MASK_SEL) |
153                                 (fnr & WFOI_FRAME_SEL));
154                 wmb();
155                 jwrite32(jme, JME_WFODP, mask[i]);
156                 wmb();
157         }
158 }
159
160 static inline void
161 jme_reset_mac_processor(struct jme_adapter *jme)
162 {
163         u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
164         u32 crc = 0xCDCDCDCD;
165         u32 gpreg0;
166         int i;
167
168         jwrite32(jme, JME_GHC, jme->reg_ghc | GHC_SWRST);
169         udelay(2);
170         jwrite32(jme, JME_GHC, jme->reg_ghc);
171
172         jwrite32(jme, JME_RXDBA_LO, 0x00000000);
173         jwrite32(jme, JME_RXDBA_HI, 0x00000000);
174         jwrite32(jme, JME_RXQDC, 0x00000000);
175         jwrite32(jme, JME_RXNDA, 0x00000000);
176         jwrite32(jme, JME_TXDBA_LO, 0x00000000);
177         jwrite32(jme, JME_TXDBA_HI, 0x00000000);
178         jwrite32(jme, JME_TXQDC, 0x00000000);
179         jwrite32(jme, JME_TXNDA, 0x00000000);
180
181         jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
182         jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
183         for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
184                 jme_setup_wakeup_frame(jme, mask, crc, i);
185         if (jme->fpgaver)
186                 gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
187         else
188                 gpreg0 = GPREG0_DEFAULT;
189         jwrite32(jme, JME_GPREG0, gpreg0);
190         jwrite32(jme, JME_GPREG1, GPREG1_DEFAULT);
191 }
192
193 static inline void
194 jme_reset_ghc_speed(struct jme_adapter *jme)
195 {
196         jme->reg_ghc &= ~(GHC_SPEED_1000M | GHC_DPX);
197         jwrite32(jme, JME_GHC, jme->reg_ghc);
198 }
199
200 static inline void
201 jme_clear_pm(struct jme_adapter *jme)
202 {
203         jwrite32(jme, JME_PMCS, 0xFFFF0000 | jme->reg_pmcs);
204         pci_set_power_state(jme->pdev, PCI_D0);
205         pci_enable_wake(jme->pdev, PCI_D0, false);
206 }
207
208 static int
209 jme_reload_eeprom(struct jme_adapter *jme)
210 {
211         u32 val;
212         int i;
213
214         val = jread32(jme, JME_SMBCSR);
215
216         if (val & SMBCSR_EEPROMD) {
217                 val |= SMBCSR_CNACK;
218                 jwrite32(jme, JME_SMBCSR, val);
219                 val |= SMBCSR_RELOAD;
220                 jwrite32(jme, JME_SMBCSR, val);
221                 mdelay(12);
222
223                 for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
224                         mdelay(1);
225                         if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
226                                 break;
227                 }
228
229                 if (i == 0) {
230                         jeprintk(jme->pdev, "eeprom reload timeout\n");
231                         return -EIO;
232                 }
233         }
234
235         return 0;
236 }
237
238 static void
239 jme_load_macaddr(struct net_device *netdev)
240 {
241         struct jme_adapter *jme = netdev_priv(netdev);
242         unsigned char macaddr[6];
243         u32 val;
244
245         spin_lock_bh(&jme->macaddr_lock);
246         val = jread32(jme, JME_RXUMA_LO);
247         macaddr[0] = (val >>  0) & 0xFF;
248         macaddr[1] = (val >>  8) & 0xFF;
249         macaddr[2] = (val >> 16) & 0xFF;
250         macaddr[3] = (val >> 24) & 0xFF;
251         val = jread32(jme, JME_RXUMA_HI);
252         macaddr[4] = (val >>  0) & 0xFF;
253         macaddr[5] = (val >>  8) & 0xFF;
254         memcpy(netdev->dev_addr, macaddr, 6);
255         spin_unlock_bh(&jme->macaddr_lock);
256 }
257
258 static inline void
259 jme_set_rx_pcc(struct jme_adapter *jme, int p)
260 {
261         switch (p) {
262         case PCC_OFF:
263                 jwrite32(jme, JME_PCCRX0,
264                         ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
265                         ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
266                 break;
267         case PCC_P1:
268                 jwrite32(jme, JME_PCCRX0,
269                         ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
270                         ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
271                 break;
272         case PCC_P2:
273                 jwrite32(jme, JME_PCCRX0,
274                         ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
275                         ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
276                 break;
277         case PCC_P3:
278                 jwrite32(jme, JME_PCCRX0,
279                         ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
280                         ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
281                 break;
282         default:
283                 break;
284         }
285         wmb();
286
287         if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
288 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
289                 msg_rx_status(jme, "Switched to PCC_P%d\n", p);
290 #else
291                 netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
292 #endif
293 }
294
295 static void
296 jme_start_irq(struct jme_adapter *jme)
297 {
298         register struct dynpcc_info *dpi = &(jme->dpi);
299
300         jme_set_rx_pcc(jme, PCC_P1);
301         dpi->cur                = PCC_P1;
302         dpi->attempt            = PCC_P1;
303         dpi->cnt                = 0;
304
305         jwrite32(jme, JME_PCCTX,
306                         ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
307                         ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
308                         PCCTXQ0_EN
309                 );
310
311         /*
312          * Enable Interrupts
313          */
314         jwrite32(jme, JME_IENS, INTR_ENABLE);
315 }
316
317 static inline void
318 jme_stop_irq(struct jme_adapter *jme)
319 {
320         /*
321          * Disable Interrupts
322          */
323         jwrite32f(jme, JME_IENC, INTR_ENABLE);
324 }
325
326 static u32
327 jme_linkstat_from_phy(struct jme_adapter *jme)
328 {
329         u32 phylink, bmsr;
330
331         phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
332         bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
333         if (bmsr & BMSR_ANCOMP)
334                 phylink |= PHY_LINK_AUTONEG_COMPLETE;
335
336         return phylink;
337 }
338
339 static inline void
340 jme_set_phyfifoa(struct jme_adapter *jme)
341 {
342         jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
343 }
344
345 static inline void
346 jme_set_phyfifob(struct jme_adapter *jme)
347 {
348         jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
349 }
350
351 static int
352 jme_check_link(struct net_device *netdev, int testonly)
353 {
354         struct jme_adapter *jme = netdev_priv(netdev);
355         u32 phylink, ghc, cnt = JME_SPDRSV_TIMEOUT, bmcr, gpreg1;
356         char linkmsg[64];
357         int rc = 0;
358
359         linkmsg[0] = '\0';
360
361         if (jme->fpgaver)
362                 phylink = jme_linkstat_from_phy(jme);
363         else
364                 phylink = jread32(jme, JME_PHY_LINK);
365
366         if (phylink & PHY_LINK_UP) {
367                 if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
368                         /*
369                          * If we did not enable AN
370                          * Speed/Duplex Info should be obtained from SMI
371                          */
372                         phylink = PHY_LINK_UP;
373
374                         bmcr = jme_mdio_read(jme->dev,
375                                                 jme->mii_if.phy_id,
376                                                 MII_BMCR);
377
378                         phylink |= ((bmcr & BMCR_SPEED1000) &&
379                                         (bmcr & BMCR_SPEED100) == 0) ?
380                                         PHY_LINK_SPEED_1000M :
381                                         (bmcr & BMCR_SPEED100) ?
382                                         PHY_LINK_SPEED_100M :
383                                         PHY_LINK_SPEED_10M;
384
385                         phylink |= (bmcr & BMCR_FULLDPLX) ?
386                                          PHY_LINK_DUPLEX : 0;
387
388                         strcat(linkmsg, "Forced: ");
389                 } else {
390                         /*
391                          * Keep polling for speed/duplex resolve complete
392                          */
393                         while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
394                                 --cnt) {
395
396                                 udelay(1);
397
398                                 if (jme->fpgaver)
399                                         phylink = jme_linkstat_from_phy(jme);
400                                 else
401                                         phylink = jread32(jme, JME_PHY_LINK);
402                         }
403                         if (!cnt)
404                                 jeprintk(jme->pdev,
405                                         "Waiting speed resolve timeout.\n");
406
407                         strcat(linkmsg, "ANed: ");
408                 }
409
410                 if (jme->phylink == phylink) {
411                         rc = 1;
412                         goto out;
413                 }
414                 if (testonly)
415                         goto out;
416
417                 jme->phylink = phylink;
418
419                 ghc = jme->reg_ghc & ~(GHC_SPEED | GHC_DPX |
420                                 GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE |
421                                 GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY);
422                 switch (phylink & PHY_LINK_SPEED_MASK) {
423                 case PHY_LINK_SPEED_10M:
424                         ghc |= GHC_SPEED_10M |
425                                 GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
426                         strcat(linkmsg, "10 Mbps, ");
427                         break;
428                 case PHY_LINK_SPEED_100M:
429                         ghc |= GHC_SPEED_100M |
430                                 GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
431                         strcat(linkmsg, "100 Mbps, ");
432                         break;
433                 case PHY_LINK_SPEED_1000M:
434                         ghc |= GHC_SPEED_1000M |
435                                 GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
436                         strcat(linkmsg, "1000 Mbps, ");
437                         break;
438                 default:
439                         break;
440                 }
441
442                 if (phylink & PHY_LINK_DUPLEX) {
443                         jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
444                         ghc |= GHC_DPX;
445                 } else {
446                         jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
447                                                 TXMCS_BACKOFF |
448                                                 TXMCS_CARRIERSENSE |
449                                                 TXMCS_COLLISION);
450                         jwrite32(jme, JME_TXTRHD, TXTRHD_TXPEN |
451                                 ((0x2000 << TXTRHD_TXP_SHIFT) & TXTRHD_TXP) |
452                                 TXTRHD_TXREN |
453                                 ((8 << TXTRHD_TXRL_SHIFT) & TXTRHD_TXRL));
454                 }
455
456                 gpreg1 = GPREG1_DEFAULT;
457                 if (is_buggy250(jme->pdev->device, jme->chiprev)) {
458                         if (!(phylink & PHY_LINK_DUPLEX))
459                                 gpreg1 |= GPREG1_HALFMODEPATCH;
460                         switch (phylink & PHY_LINK_SPEED_MASK) {
461                         case PHY_LINK_SPEED_10M:
462                                 jme_set_phyfifoa(jme);
463                                 gpreg1 |= GPREG1_RSSPATCH;
464                                 break;
465                         case PHY_LINK_SPEED_100M:
466                                 jme_set_phyfifob(jme);
467                                 gpreg1 |= GPREG1_RSSPATCH;
468                                 break;
469                         case PHY_LINK_SPEED_1000M:
470                                 jme_set_phyfifoa(jme);
471                                 break;
472                         default:
473                                 break;
474                         }
475                 }
476
477                 jwrite32(jme, JME_GPREG1, gpreg1);
478                 jwrite32(jme, JME_GHC, ghc);
479                 jme->reg_ghc = ghc;
480
481                 strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
482                                         "Full-Duplex, " :
483                                         "Half-Duplex, ");
484                 strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
485                                         "MDI-X" :
486                                         "MDI");
487 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
488                 msg_link(jme, "Link is up at %s.\n", linkmsg);
489 #else
490                 netif_info(jme, link, jme->dev, "Link is up at %s.\n", linkmsg);
491 #endif
492                 netif_carrier_on(netdev);
493         } else {
494                 if (testonly)
495                         goto out;
496
497 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
498                 msg_link(jme, "Link is down.\n");
499 #else
500                 netif_info(jme, link, jme->dev, "Link is down.\n");
501 #endif
502                 jme->phylink = 0;
503                 netif_carrier_off(netdev);
504         }
505
506 out:
507         return rc;
508 }
509
510 static int
511 jme_setup_tx_resources(struct jme_adapter *jme)
512 {
513         struct jme_ring *txring = &(jme->txring[0]);
514
515         txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
516                                    TX_RING_ALLOC_SIZE(jme->tx_ring_size),
517                                    &(txring->dmaalloc),
518                                    GFP_ATOMIC);
519
520         if (!txring->alloc)
521                 goto err_set_null;
522
523         /*
524          * 16 Bytes align
525          */
526         txring->desc            = (void *)ALIGN((unsigned long)(txring->alloc),
527                                                 RING_DESC_ALIGN);
528         txring->dma             = ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
529         txring->next_to_use     = 0;
530         atomic_set(&txring->next_to_clean, 0);
531         atomic_set(&txring->nr_free, jme->tx_ring_size);
532
533         txring->bufinf          = kmalloc(sizeof(struct jme_buffer_info) *
534                                         jme->tx_ring_size, GFP_ATOMIC);
535         if (unlikely(!(txring->bufinf)))
536                 goto err_free_txring;
537
538         /*
539          * Initialize Transmit Descriptors
540          */
541         memset(txring->alloc, 0, TX_RING_ALLOC_SIZE(jme->tx_ring_size));
542         memset(txring->bufinf, 0,
543                 sizeof(struct jme_buffer_info) * jme->tx_ring_size);
544
545         return 0;
546
547 err_free_txring:
548         dma_free_coherent(&(jme->pdev->dev),
549                           TX_RING_ALLOC_SIZE(jme->tx_ring_size),
550                           txring->alloc,
551                           txring->dmaalloc);
552
553 err_set_null:
554         txring->desc = NULL;
555         txring->dmaalloc = 0;
556         txring->dma = 0;
557         txring->bufinf = NULL;
558
559         return -ENOMEM;
560 }
561
562 static void
563 jme_free_tx_resources(struct jme_adapter *jme)
564 {
565         int i;
566         struct jme_ring *txring = &(jme->txring[0]);
567         struct jme_buffer_info *txbi;
568
569         if (txring->alloc) {
570                 if (txring->bufinf) {
571                         for (i = 0 ; i < jme->tx_ring_size ; ++i) {
572                                 txbi = txring->bufinf + i;
573                                 if (txbi->skb) {
574                                         dev_kfree_skb(txbi->skb);
575                                         txbi->skb = NULL;
576                                 }
577                                 txbi->mapping           = 0;
578                                 txbi->len               = 0;
579                                 txbi->nr_desc           = 0;
580                                 txbi->start_xmit        = 0;
581                         }
582                         kfree(txring->bufinf);
583                 }
584
585                 dma_free_coherent(&(jme->pdev->dev),
586                                   TX_RING_ALLOC_SIZE(jme->tx_ring_size),
587                                   txring->alloc,
588                                   txring->dmaalloc);
589
590                 txring->alloc           = NULL;
591                 txring->desc            = NULL;
592                 txring->dmaalloc        = 0;
593                 txring->dma             = 0;
594                 txring->bufinf          = NULL;
595         }
596         txring->next_to_use     = 0;
597         atomic_set(&txring->next_to_clean, 0);
598         atomic_set(&txring->nr_free, 0);
599 }
600
601 static inline void
602 jme_enable_tx_engine(struct jme_adapter *jme)
603 {
604         /*
605          * Select Queue 0
606          */
607         jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
608         wmb();
609
610         /*
611          * Setup TX Queue 0 DMA Bass Address
612          */
613         jwrite32(jme, JME_TXDBA_LO, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
614         jwrite32(jme, JME_TXDBA_HI, (__u64)(jme->txring[0].dma) >> 32);
615         jwrite32(jme, JME_TXNDA, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
616
617         /*
618          * Setup TX Descptor Count
619          */
620         jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
621
622         /*
623          * Enable TX Engine
624          */
625         wmb();
626         jwrite32(jme, JME_TXCS, jme->reg_txcs |
627                                 TXCS_SELECT_QUEUE0 |
628                                 TXCS_ENABLE);
629
630 }
631
632 static inline void
633 jme_restart_tx_engine(struct jme_adapter *jme)
634 {
635         /*
636          * Restart TX Engine
637          */
638         jwrite32(jme, JME_TXCS, jme->reg_txcs |
639                                 TXCS_SELECT_QUEUE0 |
640                                 TXCS_ENABLE);
641 }
642
643 static inline void
644 jme_disable_tx_engine(struct jme_adapter *jme)
645 {
646         int i;
647         u32 val;
648
649         /*
650          * Disable TX Engine
651          */
652         jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
653         wmb();
654
655         val = jread32(jme, JME_TXCS);
656         for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
657                 mdelay(1);
658                 val = jread32(jme, JME_TXCS);
659                 rmb();
660         }
661
662         if (!i)
663                 jeprintk(jme->pdev, "Disable TX engine timeout.\n");
664 }
665
666 static void
667 jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
668 {
669         struct jme_ring *rxring = &(jme->rxring[0]);
670         register struct rxdesc *rxdesc = rxring->desc;
671         struct jme_buffer_info *rxbi = rxring->bufinf;
672         rxdesc += i;
673         rxbi += i;
674
675         rxdesc->dw[0] = 0;
676         rxdesc->dw[1] = 0;
677         rxdesc->desc1.bufaddrh  = cpu_to_le32((__u64)rxbi->mapping >> 32);
678         rxdesc->desc1.bufaddrl  = cpu_to_le32(
679                                         (__u64)rxbi->mapping & 0xFFFFFFFFUL);
680         rxdesc->desc1.datalen   = cpu_to_le16(rxbi->len);
681         if (jme->dev->features & NETIF_F_HIGHDMA)
682                 rxdesc->desc1.flags = RXFLAG_64BIT;
683         wmb();
684         rxdesc->desc1.flags     |= RXFLAG_OWN | RXFLAG_INT;
685 }
686
687 static int
688 jme_make_new_rx_buf(struct jme_adapter *jme, int i)
689 {
690         struct jme_ring *rxring = &(jme->rxring[0]);
691         struct jme_buffer_info *rxbi = rxring->bufinf + i;
692         struct sk_buff *skb;
693
694         skb = netdev_alloc_skb(jme->dev,
695                 jme->dev->mtu + RX_EXTRA_LEN);
696         if (unlikely(!skb))
697                 return -ENOMEM;
698 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
699         skb->dev = jme->dev;
700 #endif
701
702         rxbi->skb = skb;
703         rxbi->len = skb_tailroom(skb);
704         rxbi->mapping = pci_map_page(jme->pdev,
705                                         virt_to_page(skb->data),
706                                         offset_in_page(skb->data),
707                                         rxbi->len,
708                                         PCI_DMA_FROMDEVICE);
709
710         return 0;
711 }
712
713 static void
714 jme_free_rx_buf(struct jme_adapter *jme, int i)
715 {
716         struct jme_ring *rxring = &(jme->rxring[0]);
717         struct jme_buffer_info *rxbi = rxring->bufinf;
718         rxbi += i;
719
720         if (rxbi->skb) {
721                 pci_unmap_page(jme->pdev,
722                                  rxbi->mapping,
723                                  rxbi->len,
724                                  PCI_DMA_FROMDEVICE);
725                 dev_kfree_skb(rxbi->skb);
726                 rxbi->skb = NULL;
727                 rxbi->mapping = 0;
728                 rxbi->len = 0;
729         }
730 }
731
732 static void
733 jme_free_rx_resources(struct jme_adapter *jme)
734 {
735         int i;
736         struct jme_ring *rxring = &(jme->rxring[0]);
737
738         if (rxring->alloc) {
739                 if (rxring->bufinf) {
740                         for (i = 0 ; i < jme->rx_ring_size ; ++i)
741                                 jme_free_rx_buf(jme, i);
742                         kfree(rxring->bufinf);
743                 }
744
745                 dma_free_coherent(&(jme->pdev->dev),
746                                   RX_RING_ALLOC_SIZE(jme->rx_ring_size),
747                                   rxring->alloc,
748                                   rxring->dmaalloc);
749                 rxring->alloc    = NULL;
750                 rxring->desc     = NULL;
751                 rxring->dmaalloc = 0;
752                 rxring->dma      = 0;
753                 rxring->bufinf   = NULL;
754         }
755         rxring->next_to_use   = 0;
756         atomic_set(&rxring->next_to_clean, 0);
757 }
758
759 static int
760 jme_setup_rx_resources(struct jme_adapter *jme)
761 {
762         int i;
763         struct jme_ring *rxring = &(jme->rxring[0]);
764
765         rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
766                                    RX_RING_ALLOC_SIZE(jme->rx_ring_size),
767                                    &(rxring->dmaalloc),
768                                    GFP_ATOMIC);
769         if (!rxring->alloc)
770                 goto err_set_null;
771
772         /*
773          * 16 Bytes align
774          */
775         rxring->desc            = (void *)ALIGN((unsigned long)(rxring->alloc),
776                                                 RING_DESC_ALIGN);
777         rxring->dma             = ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
778         rxring->next_to_use     = 0;
779         atomic_set(&rxring->next_to_clean, 0);
780
781         rxring->bufinf          = kmalloc(sizeof(struct jme_buffer_info) *
782                                         jme->rx_ring_size, GFP_ATOMIC);
783         if (unlikely(!(rxring->bufinf)))
784                 goto err_free_rxring;
785
786         /*
787          * Initiallize Receive Descriptors
788          */
789         memset(rxring->bufinf, 0,
790                 sizeof(struct jme_buffer_info) * jme->rx_ring_size);
791         for (i = 0 ; i < jme->rx_ring_size ; ++i) {
792                 if (unlikely(jme_make_new_rx_buf(jme, i))) {
793                         jme_free_rx_resources(jme);
794                         return -ENOMEM;
795                 }
796
797                 jme_set_clean_rxdesc(jme, i);
798         }
799
800         return 0;
801
802 err_free_rxring:
803         dma_free_coherent(&(jme->pdev->dev),
804                           RX_RING_ALLOC_SIZE(jme->rx_ring_size),
805                           rxring->alloc,
806                           rxring->dmaalloc);
807 err_set_null:
808         rxring->desc = NULL;
809         rxring->dmaalloc = 0;
810         rxring->dma = 0;
811         rxring->bufinf = NULL;
812
813         return -ENOMEM;
814 }
815
816 static inline void
817 jme_enable_rx_engine(struct jme_adapter *jme)
818 {
819         /*
820          * Select Queue 0
821          */
822         jwrite32(jme, JME_RXCS, jme->reg_rxcs |
823                                 RXCS_QUEUESEL_Q0);
824         wmb();
825
826         /*
827          * Setup RX DMA Bass Address
828          */
829         jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
830         jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
831         jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
832
833         /*
834          * Setup RX Descriptor Count
835          */
836         jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
837
838         /*
839          * Setup Unicast Filter
840          */
841         jme_set_multi(jme->dev);
842
843         /*
844          * Enable RX Engine
845          */
846         wmb();
847         jwrite32(jme, JME_RXCS, jme->reg_rxcs |
848                                 RXCS_QUEUESEL_Q0 |
849                                 RXCS_ENABLE |
850                                 RXCS_QST);
851 }
852
853 static inline void
854 jme_restart_rx_engine(struct jme_adapter *jme)
855 {
856         /*
857          * Start RX Engine
858          */
859         jwrite32(jme, JME_RXCS, jme->reg_rxcs |
860                                 RXCS_QUEUESEL_Q0 |
861                                 RXCS_ENABLE |
862                                 RXCS_QST);
863 }
864
865 static inline void
866 jme_disable_rx_engine(struct jme_adapter *jme)
867 {
868         int i;
869         u32 val;
870
871         /*
872          * Disable RX Engine
873          */
874         jwrite32(jme, JME_RXCS, jme->reg_rxcs);
875         wmb();
876
877         val = jread32(jme, JME_RXCS);
878         for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
879                 mdelay(1);
880                 val = jread32(jme, JME_RXCS);
881                 rmb();
882         }
883
884         if (!i)
885                 jeprintk(jme->pdev, "Disable RX engine timeout.\n");
886
887 }
888
889 static int
890 jme_rxsum_ok(struct jme_adapter *jme, u16 flags)
891 {
892         if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
893                 return false;
894
895         if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
896                         == RXWBFLAG_TCPON)) {
897                 if (flags & RXWBFLAG_IPV4)
898 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
899                         msg_rx_err(jme, "TCP Checksum error\n");
900 #else
901                         netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
902 #endif
903                 return false;
904         }
905
906         if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
907                         == RXWBFLAG_UDPON)) {
908                 if (flags & RXWBFLAG_IPV4)
909 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
910                         msg_rx_err(jme, "UDP Checksum error.\n");
911 #else
912                         netif_err(jme, rx_err, jme->dev, "UDP Checksum error.\n");
913 #endif
914                 return false;
915         }
916
917         if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
918                         == RXWBFLAG_IPV4)) {
919 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,33)
920                 msg_rx_err(jme, "IPv4 Checksum error.\n");
921 #else
922                 netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error.\n");
923 #endif
924                 return false;
925         }
926
927         return true;
928 }
929
930 static void
931 jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
932 {
933         struct jme_ring *rxring = &(jme->rxring[0]);
934         struct rxdesc *rxdesc = rxring->desc;
935         struct jme_buffer_info *rxbi = rxring->bufinf;
936         struct sk_buff *skb;
937         int framesize;
938
939         rxdesc += idx;
940         rxbi += idx;
941
942         skb = rxbi->skb;
943         pci_dma_sync_single_for_cpu(jme->pdev,
944                                         rxbi->mapping,
945                                         rxbi->len,
946                                         PCI_DMA_FROMDEVICE);
947
948         if (unlikely(jme_make_new_rx_buf(jme, idx))) {
949                 pci_dma_sync_single_for_device(jme->pdev,
950                                                 rxbi->mapping,
951                                                 rxbi->len,
952                                                 PCI_DMA_FROMDEVICE);
953
954                 ++(NET_STAT(jme).rx_dropped);
955         } else {
956                 framesize = le16_to_cpu(rxdesc->descwb.framesize)
957                                 - RX_PREPAD_SIZE;
958
959                 skb_reserve(skb, RX_PREPAD_SIZE);
960                 skb_put(skb, framesize);
961                 skb->protocol = eth_type_trans(skb, jme->dev);
962
963                 if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags)))
964                         skb->ip_summed = CHECKSUM_UNNECESSARY;
965                 else
966 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35)
967                         skb->ip_summed = CHECKSUM_NONE;
968 #else
969                         skb_checksum_none_assert(skb);
970 #endif
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