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