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