blob: c911d883c27e6fbd922eab405aa2457db25be782 [file] [log] [blame]
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001/*
2 * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
3 *
4 * Copyright 2008 JMicron Technology Corporation
5 * http://www.jmicron.com/
Guo-Fu Tsenge47dfcd2010-10-18 14:10:44 +00006 * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
Guo-Fu Tseng95252232008-09-16 01:00:11 +08007 *
8 * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
Joe Perches49d70c42010-09-04 22:21:05 +000025#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
Guo-Fu Tseng95252232008-09-16 01:00:11 +080027#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/pci.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/ethtool.h>
33#include <linux/mii.h>
34#include <linux/crc32.h>
35#include <linux/delay.h>
36#include <linux/spinlock.h>
37#include <linux/in.h>
38#include <linux/ip.h>
39#include <linux/ipv6.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070044#include <net/ip6_checksum.h>
Guo-Fu Tseng95252232008-09-16 01:00:11 +080045#include "jme.h"
46
47static int force_pseudohp = -1;
48static int no_pseudohp = -1;
49static int no_extplug = -1;
50module_param(force_pseudohp, int, 0);
51MODULE_PARM_DESC(force_pseudohp,
52 "Enable pseudo hot-plug feature manually by driver instead of BIOS.");
53module_param(no_pseudohp, int, 0);
54MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
55module_param(no_extplug, int, 0);
56MODULE_PARM_DESC(no_extplug,
57 "Do not use external plug signal for pseudo hot-plug.");
58
59static int
60jme_mdio_read(struct net_device *netdev, int phy, int reg)
61{
62 struct jme_adapter *jme = netdev_priv(netdev);
63 int i, val, again = (reg == MII_BMSR) ? 1 : 0;
64
65read_again:
66 jwrite32(jme, JME_SMI, SMI_OP_REQ |
67 smi_phy_addr(phy) |
68 smi_reg_addr(reg));
69
70 wmb();
71 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
72 udelay(20);
73 val = jread32(jme, JME_SMI);
74 if ((val & SMI_OP_REQ) == 0)
75 break;
76 }
77
78 if (i == 0) {
Joe Perches49d70c42010-09-04 22:21:05 +000079 pr_err("phy(%d) read timeout : %d\n", phy, reg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +080080 return 0;
81 }
82
83 if (again--)
84 goto read_again;
85
86 return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
87}
88
89static void
90jme_mdio_write(struct net_device *netdev,
91 int phy, int reg, int val)
92{
93 struct jme_adapter *jme = netdev_priv(netdev);
94 int i;
95
96 jwrite32(jme, JME_SMI, SMI_OP_WRITE | SMI_OP_REQ |
97 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
98 smi_phy_addr(phy) | smi_reg_addr(reg));
99
100 wmb();
101 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
102 udelay(20);
103 if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
104 break;
105 }
106
107 if (i == 0)
Joe Perches49d70c42010-09-04 22:21:05 +0000108 pr_err("phy(%d) write timeout : %d\n", phy, reg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800109}
110
111static inline void
112jme_reset_phy_processor(struct jme_adapter *jme)
113{
114 u32 val;
115
116 jme_mdio_write(jme->dev,
117 jme->mii_if.phy_id,
118 MII_ADVERTISE, ADVERTISE_ALL |
119 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
120
121 if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
122 jme_mdio_write(jme->dev,
123 jme->mii_if.phy_id,
124 MII_CTRL1000,
125 ADVERTISE_1000FULL | ADVERTISE_1000HALF);
126
127 val = jme_mdio_read(jme->dev,
128 jme->mii_if.phy_id,
129 MII_BMCR);
130
131 jme_mdio_write(jme->dev,
132 jme->mii_if.phy_id,
133 MII_BMCR, val | BMCR_RESET);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800134}
135
136static void
137jme_setup_wakeup_frame(struct jme_adapter *jme,
Joe Perchesb6bc7652010-12-21 02:16:08 -0800138 const u32 *mask, u32 crc, int fnr)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800139{
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 */
153 for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
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}
162
163static inline void
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000164jme_mac_rxclk_off(struct jme_adapter *jme)
165{
166 jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
167 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
168}
169
170static inline void
171jme_mac_rxclk_on(struct jme_adapter *jme)
172{
173 jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
174 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
175}
176
177static inline void
178jme_mac_txclk_off(struct jme_adapter *jme)
179{
180 jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
181 jwrite32f(jme, JME_GHC, jme->reg_ghc);
182}
183
184static inline void
185jme_mac_txclk_on(struct jme_adapter *jme)
186{
187 u32 speed = jme->reg_ghc & GHC_SPEED;
188 if (speed == GHC_SPEED_1000M)
189 jme->reg_ghc |= GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
190 else
191 jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
192 jwrite32f(jme, JME_GHC, jme->reg_ghc);
193}
194
195static inline void
196jme_reset_ghc_speed(struct jme_adapter *jme)
197{
198 jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
199 jwrite32f(jme, JME_GHC, jme->reg_ghc);
200}
201
202static inline void
203jme_reset_250A2_workaround(struct jme_adapter *jme)
204{
205 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
206 GPREG1_RSSPATCH);
207 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
208}
209
210static inline void
211jme_assert_ghc_reset(struct jme_adapter *jme)
212{
213 jme->reg_ghc |= GHC_SWRST;
214 jwrite32f(jme, JME_GHC, jme->reg_ghc);
215}
216
217static inline void
218jme_clear_ghc_reset(struct jme_adapter *jme)
219{
220 jme->reg_ghc &= ~GHC_SWRST;
221 jwrite32f(jme, JME_GHC, jme->reg_ghc);
222}
223
224static inline void
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800225jme_reset_mac_processor(struct jme_adapter *jme)
226{
Joe Perchesb6bc7652010-12-21 02:16:08 -0800227 static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800228 u32 crc = 0xCDCDCDCD;
229 u32 gpreg0;
230 int i;
231
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000232 jme_reset_ghc_speed(jme);
233 jme_reset_250A2_workaround(jme);
234
235 jme_mac_rxclk_on(jme);
236 jme_mac_txclk_on(jme);
237 udelay(1);
238 jme_assert_ghc_reset(jme);
239 udelay(1);
240 jme_mac_rxclk_off(jme);
241 jme_mac_txclk_off(jme);
242 udelay(1);
243 jme_clear_ghc_reset(jme);
244 udelay(1);
245 jme_mac_rxclk_on(jme);
246 jme_mac_txclk_on(jme);
247 udelay(1);
248 jme_mac_rxclk_off(jme);
249 jme_mac_txclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800250
251 jwrite32(jme, JME_RXDBA_LO, 0x00000000);
252 jwrite32(jme, JME_RXDBA_HI, 0x00000000);
253 jwrite32(jme, JME_RXQDC, 0x00000000);
254 jwrite32(jme, JME_RXNDA, 0x00000000);
255 jwrite32(jme, JME_TXDBA_LO, 0x00000000);
256 jwrite32(jme, JME_TXDBA_HI, 0x00000000);
257 jwrite32(jme, JME_TXQDC, 0x00000000);
258 jwrite32(jme, JME_TXNDA, 0x00000000);
259
260 jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
261 jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
262 for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
263 jme_setup_wakeup_frame(jme, mask, crc, i);
264 if (jme->fpgaver)
265 gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
266 else
267 gpreg0 = GPREG0_DEFAULT;
268 jwrite32(jme, JME_GPREG0, gpreg0);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800269}
270
271static inline void
272jme_clear_pm(struct jme_adapter *jme)
273{
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +0000274 jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800275}
276
277static int
278jme_reload_eeprom(struct jme_adapter *jme)
279{
280 u32 val;
281 int i;
282
283 val = jread32(jme, JME_SMBCSR);
284
285 if (val & SMBCSR_EEPROMD) {
286 val |= SMBCSR_CNACK;
287 jwrite32(jme, JME_SMBCSR, val);
288 val |= SMBCSR_RELOAD;
289 jwrite32(jme, JME_SMBCSR, val);
290 mdelay(12);
291
292 for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
293 mdelay(1);
294 if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
295 break;
296 }
297
298 if (i == 0) {
Joe Perches49d70c42010-09-04 22:21:05 +0000299 pr_err("eeprom reload timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800300 return -EIO;
301 }
302 }
303
304 return 0;
305}
306
307static void
308jme_load_macaddr(struct net_device *netdev)
309{
310 struct jme_adapter *jme = netdev_priv(netdev);
311 unsigned char macaddr[6];
312 u32 val;
313
314 spin_lock_bh(&jme->macaddr_lock);
315 val = jread32(jme, JME_RXUMA_LO);
316 macaddr[0] = (val >> 0) & 0xFF;
317 macaddr[1] = (val >> 8) & 0xFF;
318 macaddr[2] = (val >> 16) & 0xFF;
319 macaddr[3] = (val >> 24) & 0xFF;
320 val = jread32(jme, JME_RXUMA_HI);
321 macaddr[4] = (val >> 0) & 0xFF;
322 macaddr[5] = (val >> 8) & 0xFF;
323 memcpy(netdev->dev_addr, macaddr, 6);
324 spin_unlock_bh(&jme->macaddr_lock);
325}
326
327static inline void
328jme_set_rx_pcc(struct jme_adapter *jme, int p)
329{
330 switch (p) {
331 case PCC_OFF:
332 jwrite32(jme, JME_PCCRX0,
333 ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
334 ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
335 break;
336 case PCC_P1:
337 jwrite32(jme, JME_PCCRX0,
338 ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
339 ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
340 break;
341 case PCC_P2:
342 jwrite32(jme, JME_PCCRX0,
343 ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
344 ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
345 break;
346 case PCC_P3:
347 jwrite32(jme, JME_PCCRX0,
348 ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
349 ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
350 break;
351 default:
352 break;
353 }
354 wmb();
355
356 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
Joe Perchesf8502ce2010-02-09 11:49:51 +0000357 netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800358}
359
360static void
361jme_start_irq(struct jme_adapter *jme)
362{
363 register struct dynpcc_info *dpi = &(jme->dpi);
364
365 jme_set_rx_pcc(jme, PCC_P1);
366 dpi->cur = PCC_P1;
367 dpi->attempt = PCC_P1;
368 dpi->cnt = 0;
369
370 jwrite32(jme, JME_PCCTX,
371 ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
372 ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
373 PCCTXQ0_EN
374 );
375
376 /*
377 * Enable Interrupts
378 */
379 jwrite32(jme, JME_IENS, INTR_ENABLE);
380}
381
382static inline void
383jme_stop_irq(struct jme_adapter *jme)
384{
385 /*
386 * Disable Interrupts
387 */
388 jwrite32f(jme, JME_IENC, INTR_ENABLE);
389}
390
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800391static u32
392jme_linkstat_from_phy(struct jme_adapter *jme)
393{
394 u32 phylink, bmsr;
395
396 phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
397 bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
398 if (bmsr & BMSR_ANCOMP)
399 phylink |= PHY_LINK_AUTONEG_COMPLETE;
400
401 return phylink;
402}
403
404static inline void
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000405jme_set_phyfifo_5level(struct jme_adapter *jme)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800406{
407 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
408}
409
410static inline void
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000411jme_set_phyfifo_8level(struct jme_adapter *jme)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800412{
413 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
414}
415
416static int
417jme_check_link(struct net_device *netdev, int testonly)
418{
419 struct jme_adapter *jme = netdev_priv(netdev);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000420 u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800421 char linkmsg[64];
422 int rc = 0;
423
424 linkmsg[0] = '\0';
425
426 if (jme->fpgaver)
427 phylink = jme_linkstat_from_phy(jme);
428 else
429 phylink = jread32(jme, JME_PHY_LINK);
430
431 if (phylink & PHY_LINK_UP) {
432 if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
433 /*
434 * If we did not enable AN
435 * Speed/Duplex Info should be obtained from SMI
436 */
437 phylink = PHY_LINK_UP;
438
439 bmcr = jme_mdio_read(jme->dev,
440 jme->mii_if.phy_id,
441 MII_BMCR);
442
443 phylink |= ((bmcr & BMCR_SPEED1000) &&
444 (bmcr & BMCR_SPEED100) == 0) ?
445 PHY_LINK_SPEED_1000M :
446 (bmcr & BMCR_SPEED100) ?
447 PHY_LINK_SPEED_100M :
448 PHY_LINK_SPEED_10M;
449
450 phylink |= (bmcr & BMCR_FULLDPLX) ?
451 PHY_LINK_DUPLEX : 0;
452
453 strcat(linkmsg, "Forced: ");
454 } else {
455 /*
456 * Keep polling for speed/duplex resolve complete
457 */
458 while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
459 --cnt) {
460
461 udelay(1);
462
463 if (jme->fpgaver)
464 phylink = jme_linkstat_from_phy(jme);
465 else
466 phylink = jread32(jme, JME_PHY_LINK);
467 }
468 if (!cnt)
Joe Perches49d70c42010-09-04 22:21:05 +0000469 pr_err("Waiting speed resolve timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800470
471 strcat(linkmsg, "ANed: ");
472 }
473
474 if (jme->phylink == phylink) {
475 rc = 1;
476 goto out;
477 }
478 if (testonly)
479 goto out;
480
481 jme->phylink = phylink;
482
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000483 /*
484 * The speed/duplex setting of jme->reg_ghc already cleared
485 * by jme_reset_mac_processor()
486 */
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800487 switch (phylink & PHY_LINK_SPEED_MASK) {
488 case PHY_LINK_SPEED_10M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000489 jme->reg_ghc |= GHC_SPEED_10M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800490 strcat(linkmsg, "10 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800491 break;
492 case PHY_LINK_SPEED_100M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000493 jme->reg_ghc |= GHC_SPEED_100M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800494 strcat(linkmsg, "100 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800495 break;
496 case PHY_LINK_SPEED_1000M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000497 jme->reg_ghc |= GHC_SPEED_1000M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800498 strcat(linkmsg, "1000 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800499 break;
500 default:
501 break;
502 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800503
504 if (phylink & PHY_LINK_DUPLEX) {
505 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
Guo-Fu Tseng3903c022011-02-13 18:27:38 +0000506 jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000507 jme->reg_ghc |= GHC_DPX;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800508 } else {
509 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
510 TXMCS_BACKOFF |
511 TXMCS_CARRIERSENSE |
512 TXMCS_COLLISION);
Guo-Fu Tseng3903c022011-02-13 18:27:38 +0000513 jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800514 }
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700515
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000516 jwrite32(jme, JME_GHC, jme->reg_ghc);
517
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700518 if (is_buggy250(jme->pdev->device, jme->chiprev)) {
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000519 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
520 GPREG1_RSSPATCH);
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700521 if (!(phylink & PHY_LINK_DUPLEX))
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000522 jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700523 switch (phylink & PHY_LINK_SPEED_MASK) {
524 case PHY_LINK_SPEED_10M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000525 jme_set_phyfifo_8level(jme);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000526 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700527 break;
528 case PHY_LINK_SPEED_100M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000529 jme_set_phyfifo_5level(jme);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000530 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700531 break;
532 case PHY_LINK_SPEED_1000M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000533 jme_set_phyfifo_8level(jme);
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700534 break;
535 default:
536 break;
537 }
538 }
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000539 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800540
akeemting4f40bf42008-12-03 21:19:16 -0800541 strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
542 "Full-Duplex, " :
543 "Half-Duplex, ");
544 strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
545 "MDI-X" :
546 "MDI");
Joe Perches49d70c42010-09-04 22:21:05 +0000547 netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800548 netif_carrier_on(netdev);
549 } else {
550 if (testonly)
551 goto out;
552
Joe Perches49d70c42010-09-04 22:21:05 +0000553 netif_info(jme, link, jme->dev, "Link is down\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800554 jme->phylink = 0;
555 netif_carrier_off(netdev);
556 }
557
558out:
559 return rc;
560}
561
562static int
563jme_setup_tx_resources(struct jme_adapter *jme)
564{
565 struct jme_ring *txring = &(jme->txring[0]);
566
567 txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
568 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
569 &(txring->dmaalloc),
570 GFP_ATOMIC);
571
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000572 if (!txring->alloc)
573 goto err_set_null;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800574
575 /*
576 * 16 Bytes align
577 */
578 txring->desc = (void *)ALIGN((unsigned long)(txring->alloc),
579 RING_DESC_ALIGN);
580 txring->dma = ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
581 txring->next_to_use = 0;
582 atomic_set(&txring->next_to_clean, 0);
583 atomic_set(&txring->nr_free, jme->tx_ring_size);
584
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000585 txring->bufinf = kmalloc(sizeof(struct jme_buffer_info) *
586 jme->tx_ring_size, GFP_ATOMIC);
587 if (unlikely(!(txring->bufinf)))
588 goto err_free_txring;
589
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800590 /*
591 * Initialize Transmit Descriptors
592 */
593 memset(txring->alloc, 0, TX_RING_ALLOC_SIZE(jme->tx_ring_size));
594 memset(txring->bufinf, 0,
595 sizeof(struct jme_buffer_info) * jme->tx_ring_size);
596
597 return 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000598
599err_free_txring:
600 dma_free_coherent(&(jme->pdev->dev),
601 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
602 txring->alloc,
603 txring->dmaalloc);
604
605err_set_null:
606 txring->desc = NULL;
607 txring->dmaalloc = 0;
608 txring->dma = 0;
609 txring->bufinf = NULL;
610
611 return -ENOMEM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800612}
613
614static void
615jme_free_tx_resources(struct jme_adapter *jme)
616{
617 int i;
618 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000619 struct jme_buffer_info *txbi;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800620
621 if (txring->alloc) {
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000622 if (txring->bufinf) {
623 for (i = 0 ; i < jme->tx_ring_size ; ++i) {
624 txbi = txring->bufinf + i;
625 if (txbi->skb) {
626 dev_kfree_skb(txbi->skb);
627 txbi->skb = NULL;
628 }
629 txbi->mapping = 0;
630 txbi->len = 0;
631 txbi->nr_desc = 0;
632 txbi->start_xmit = 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800633 }
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000634 kfree(txring->bufinf);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800635 }
636
637 dma_free_coherent(&(jme->pdev->dev),
638 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
639 txring->alloc,
640 txring->dmaalloc);
641
642 txring->alloc = NULL;
643 txring->desc = NULL;
644 txring->dmaalloc = 0;
645 txring->dma = 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000646 txring->bufinf = NULL;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800647 }
648 txring->next_to_use = 0;
649 atomic_set(&txring->next_to_clean, 0);
650 atomic_set(&txring->nr_free, 0);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800651}
652
653static inline void
654jme_enable_tx_engine(struct jme_adapter *jme)
655{
656 /*
657 * Select Queue 0
658 */
659 jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
660 wmb();
661
662 /*
663 * Setup TX Queue 0 DMA Bass Address
664 */
665 jwrite32(jme, JME_TXDBA_LO, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
666 jwrite32(jme, JME_TXDBA_HI, (__u64)(jme->txring[0].dma) >> 32);
667 jwrite32(jme, JME_TXNDA, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
668
669 /*
670 * Setup TX Descptor Count
671 */
672 jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
673
674 /*
675 * Enable TX Engine
676 */
677 wmb();
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000678 jwrite32f(jme, JME_TXCS, jme->reg_txcs |
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800679 TXCS_SELECT_QUEUE0 |
680 TXCS_ENABLE);
681
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000682 /*
683 * Start clock for TX MAC Processor
684 */
685 jme_mac_txclk_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800686}
687
688static inline void
689jme_restart_tx_engine(struct jme_adapter *jme)
690{
691 /*
692 * Restart TX Engine
693 */
694 jwrite32(jme, JME_TXCS, jme->reg_txcs |
695 TXCS_SELECT_QUEUE0 |
696 TXCS_ENABLE);
697}
698
699static inline void
700jme_disable_tx_engine(struct jme_adapter *jme)
701{
702 int i;
703 u32 val;
704
705 /*
706 * Disable TX Engine
707 */
708 jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
709 wmb();
710
711 val = jread32(jme, JME_TXCS);
712 for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
713 mdelay(1);
714 val = jread32(jme, JME_TXCS);
715 rmb();
716 }
717
718 if (!i)
Joe Perches49d70c42010-09-04 22:21:05 +0000719 pr_err("Disable TX engine timeout\n");
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000720
721 /*
722 * Stop clock for TX MAC Processor
723 */
724 jme_mac_txclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800725}
726
727static void
728jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
729{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000730 struct jme_ring *rxring = &(jme->rxring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800731 register struct rxdesc *rxdesc = rxring->desc;
732 struct jme_buffer_info *rxbi = rxring->bufinf;
733 rxdesc += i;
734 rxbi += i;
735
736 rxdesc->dw[0] = 0;
737 rxdesc->dw[1] = 0;
738 rxdesc->desc1.bufaddrh = cpu_to_le32((__u64)rxbi->mapping >> 32);
739 rxdesc->desc1.bufaddrl = cpu_to_le32(
740 (__u64)rxbi->mapping & 0xFFFFFFFFUL);
741 rxdesc->desc1.datalen = cpu_to_le16(rxbi->len);
742 if (jme->dev->features & NETIF_F_HIGHDMA)
743 rxdesc->desc1.flags = RXFLAG_64BIT;
744 wmb();
745 rxdesc->desc1.flags |= RXFLAG_OWN | RXFLAG_INT;
746}
747
748static int
749jme_make_new_rx_buf(struct jme_adapter *jme, int i)
750{
751 struct jme_ring *rxring = &(jme->rxring[0]);
752 struct jme_buffer_info *rxbi = rxring->bufinf + i;
753 struct sk_buff *skb;
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000754 dma_addr_t mapping;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800755
756 skb = netdev_alloc_skb(jme->dev,
757 jme->dev->mtu + RX_EXTRA_LEN);
758 if (unlikely(!skb))
759 return -ENOMEM;
760
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000761 mapping = pci_map_page(jme->pdev, virt_to_page(skb->data),
762 offset_in_page(skb->data), skb_tailroom(skb),
763 PCI_DMA_FROMDEVICE);
764 if (unlikely(pci_dma_mapping_error(jme->pdev, mapping))) {
765 dev_kfree_skb(skb);
766 return -ENOMEM;
767 }
768
769 if (likely(rxbi->mapping))
770 pci_unmap_page(jme->pdev, rxbi->mapping,
771 rxbi->len, PCI_DMA_FROMDEVICE);
772
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800773 rxbi->skb = skb;
774 rxbi->len = skb_tailroom(skb);
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000775 rxbi->mapping = mapping;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800776 return 0;
777}
778
779static void
780jme_free_rx_buf(struct jme_adapter *jme, int i)
781{
782 struct jme_ring *rxring = &(jme->rxring[0]);
783 struct jme_buffer_info *rxbi = rxring->bufinf;
784 rxbi += i;
785
786 if (rxbi->skb) {
787 pci_unmap_page(jme->pdev,
788 rxbi->mapping,
789 rxbi->len,
790 PCI_DMA_FROMDEVICE);
791 dev_kfree_skb(rxbi->skb);
792 rxbi->skb = NULL;
793 rxbi->mapping = 0;
794 rxbi->len = 0;
795 }
796}
797
798static void
799jme_free_rx_resources(struct jme_adapter *jme)
800{
801 int i;
802 struct jme_ring *rxring = &(jme->rxring[0]);
803
804 if (rxring->alloc) {
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000805 if (rxring->bufinf) {
806 for (i = 0 ; i < jme->rx_ring_size ; ++i)
807 jme_free_rx_buf(jme, i);
808 kfree(rxring->bufinf);
809 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800810
811 dma_free_coherent(&(jme->pdev->dev),
812 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
813 rxring->alloc,
814 rxring->dmaalloc);
815 rxring->alloc = NULL;
816 rxring->desc = NULL;
817 rxring->dmaalloc = 0;
818 rxring->dma = 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000819 rxring->bufinf = NULL;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800820 }
821 rxring->next_to_use = 0;
822 atomic_set(&rxring->next_to_clean, 0);
823}
824
825static int
826jme_setup_rx_resources(struct jme_adapter *jme)
827{
828 int i;
829 struct jme_ring *rxring = &(jme->rxring[0]);
830
831 rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
832 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
833 &(rxring->dmaalloc),
834 GFP_ATOMIC);
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000835 if (!rxring->alloc)
836 goto err_set_null;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800837
838 /*
839 * 16 Bytes align
840 */
841 rxring->desc = (void *)ALIGN((unsigned long)(rxring->alloc),
842 RING_DESC_ALIGN);
843 rxring->dma = ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
844 rxring->next_to_use = 0;
845 atomic_set(&rxring->next_to_clean, 0);
846
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000847 rxring->bufinf = kmalloc(sizeof(struct jme_buffer_info) *
848 jme->rx_ring_size, GFP_ATOMIC);
849 if (unlikely(!(rxring->bufinf)))
850 goto err_free_rxring;
851
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800852 /*
853 * Initiallize Receive Descriptors
854 */
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000855 memset(rxring->bufinf, 0,
856 sizeof(struct jme_buffer_info) * jme->rx_ring_size);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800857 for (i = 0 ; i < jme->rx_ring_size ; ++i) {
858 if (unlikely(jme_make_new_rx_buf(jme, i))) {
859 jme_free_rx_resources(jme);
860 return -ENOMEM;
861 }
862
863 jme_set_clean_rxdesc(jme, i);
864 }
865
866 return 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000867
868err_free_rxring:
869 dma_free_coherent(&(jme->pdev->dev),
870 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
871 rxring->alloc,
872 rxring->dmaalloc);
873err_set_null:
874 rxring->desc = NULL;
875 rxring->dmaalloc = 0;
876 rxring->dma = 0;
877 rxring->bufinf = NULL;
878
879 return -ENOMEM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800880}
881
882static inline void
883jme_enable_rx_engine(struct jme_adapter *jme)
884{
885 /*
886 * Select Queue 0
887 */
888 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
889 RXCS_QUEUESEL_Q0);
890 wmb();
891
892 /*
893 * Setup RX DMA Bass Address
894 */
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000895 jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800896 jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000897 jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800898
899 /*
900 * Setup RX Descriptor Count
901 */
902 jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
903
904 /*
905 * Setup Unicast Filter
906 */
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +0000907 jme_set_unicastaddr(jme->dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800908 jme_set_multi(jme->dev);
909
910 /*
911 * Enable RX Engine
912 */
913 wmb();
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000914 jwrite32f(jme, JME_RXCS, jme->reg_rxcs |
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800915 RXCS_QUEUESEL_Q0 |
916 RXCS_ENABLE |
917 RXCS_QST);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000918
919 /*
920 * Start clock for RX MAC Processor
921 */
922 jme_mac_rxclk_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800923}
924
925static inline void
926jme_restart_rx_engine(struct jme_adapter *jme)
927{
928 /*
929 * Start RX Engine
930 */
931 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
932 RXCS_QUEUESEL_Q0 |
933 RXCS_ENABLE |
934 RXCS_QST);
935}
936
937static inline void
938jme_disable_rx_engine(struct jme_adapter *jme)
939{
940 int i;
941 u32 val;
942
943 /*
944 * Disable RX Engine
945 */
946 jwrite32(jme, JME_RXCS, jme->reg_rxcs);
947 wmb();
948
949 val = jread32(jme, JME_RXCS);
950 for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
951 mdelay(1);
952 val = jread32(jme, JME_RXCS);
953 rmb();
954 }
955
956 if (!i)
Joe Perches49d70c42010-09-04 22:21:05 +0000957 pr_err("Disable RX engine timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800958
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000959 /*
960 * Stop clock for RX MAC Processor
961 */
962 jme_mac_rxclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800963}
964
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +0000965static u16
966jme_udpsum(struct sk_buff *skb)
967{
968 u16 csum = 0xFFFFu;
969
970 if (skb->len < (ETH_HLEN + sizeof(struct iphdr)))
971 return csum;
972 if (skb->protocol != htons(ETH_P_IP))
973 return csum;
974 skb_set_network_header(skb, ETH_HLEN);
975 if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
976 (skb->len < (ETH_HLEN +
977 (ip_hdr(skb)->ihl << 2) +
978 sizeof(struct udphdr)))) {
979 skb_reset_network_header(skb);
980 return csum;
981 }
982 skb_set_transport_header(skb,
983 ETH_HLEN + (ip_hdr(skb)->ihl << 2));
984 csum = udp_hdr(skb)->check;
985 skb_reset_transport_header(skb);
986 skb_reset_network_header(skb);
987
988 return csum;
989}
990
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800991static int
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +0000992jme_rxsum_ok(struct jme_adapter *jme, u16 flags, struct sk_buff *skb)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800993{
994 if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
995 return false;
996
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000997 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
998 == RXWBFLAG_TCPON)) {
999 if (flags & RXWBFLAG_IPV4)
Joe Perchesf8502ce2010-02-09 11:49:51 +00001000 netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001001 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001002 }
1003
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001004 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +00001005 == RXWBFLAG_UDPON) && jme_udpsum(skb)) {
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001006 if (flags & RXWBFLAG_IPV4)
Joe Perches49d70c42010-09-04 22:21:05 +00001007 netif_err(jme, rx_err, jme->dev, "UDP Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001008 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001009 }
1010
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001011 if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
1012 == RXWBFLAG_IPV4)) {
Joe Perches49d70c42010-09-04 22:21:05 +00001013 netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +00001014 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001015 }
1016
1017 return true;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001018}
1019
1020static void
1021jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
1022{
1023 struct jme_ring *rxring = &(jme->rxring[0]);
1024 struct rxdesc *rxdesc = rxring->desc;
1025 struct jme_buffer_info *rxbi = rxring->bufinf;
1026 struct sk_buff *skb;
1027 int framesize;
1028
1029 rxdesc += idx;
1030 rxbi += idx;
1031
1032 skb = rxbi->skb;
1033 pci_dma_sync_single_for_cpu(jme->pdev,
1034 rxbi->mapping,
1035 rxbi->len,
1036 PCI_DMA_FROMDEVICE);
1037
1038 if (unlikely(jme_make_new_rx_buf(jme, idx))) {
1039 pci_dma_sync_single_for_device(jme->pdev,
1040 rxbi->mapping,
1041 rxbi->len,
1042 PCI_DMA_FROMDEVICE);
1043
1044 ++(NET_STAT(jme).rx_dropped);
1045 } else {
1046 framesize = le16_to_cpu(rxdesc->descwb.framesize)
1047 - RX_PREPAD_SIZE;
1048
1049 skb_reserve(skb, RX_PREPAD_SIZE);
1050 skb_put(skb, framesize);
1051 skb->protocol = eth_type_trans(skb, jme->dev);
1052
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +00001053 if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags), skb))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001054 skb->ip_summed = CHECKSUM_UNNECESSARY;
1055 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001056 skb_checksum_none_assert(skb);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001057
Harvey Harrison31c221c2008-11-19 15:50:59 -08001058 if (rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_TAGON)) {
Jiri Pirko5043f50572011-07-20 04:54:21 +00001059 u16 vid = le16_to_cpu(rxdesc->descwb.vlan);
1060
1061 __vlan_hwaccel_put_tag(skb, vid);
1062 NET_STAT(jme).rx_bytes += 4;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001063 }
Jiri Pirko5043f50572011-07-20 04:54:21 +00001064 jme->jme_rx(skb);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001065
Harvey Harrison31c221c2008-11-19 15:50:59 -08001066 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_DEST)) ==
1067 cpu_to_le16(RXWBFLAG_DEST_MUL))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001068 ++(NET_STAT(jme).multicast);
1069
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001070 NET_STAT(jme).rx_bytes += framesize;
1071 ++(NET_STAT(jme).rx_packets);
1072 }
1073
1074 jme_set_clean_rxdesc(jme, idx);
1075
1076}
1077
1078static int
1079jme_process_receive(struct jme_adapter *jme, int limit)
1080{
1081 struct jme_ring *rxring = &(jme->rxring[0]);
1082 struct rxdesc *rxdesc = rxring->desc;
1083 int i, j, ccnt, desccnt, mask = jme->rx_ring_mask;
1084
1085 if (unlikely(!atomic_dec_and_test(&jme->rx_cleaning)))
1086 goto out_inc;
1087
1088 if (unlikely(atomic_read(&jme->link_changing) != 1))
1089 goto out_inc;
1090
1091 if (unlikely(!netif_carrier_ok(jme->dev)))
1092 goto out_inc;
1093
1094 i = atomic_read(&rxring->next_to_clean);
Roel Kluin858b9ce2009-03-04 00:11:42 -08001095 while (limit > 0) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001096 rxdesc = rxring->desc;
1097 rxdesc += i;
1098
Harvey Harrison31c221c2008-11-19 15:50:59 -08001099 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_OWN)) ||
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001100 !(rxdesc->descwb.desccnt & RXWBDCNT_WBCPL))
1101 goto out;
Roel Kluin858b9ce2009-03-04 00:11:42 -08001102 --limit;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001103
Guo-Fu Tsengea192aa2010-10-18 14:10:42 +00001104 rmb();
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001105 desccnt = rxdesc->descwb.desccnt & RXWBDCNT_DCNT;
1106
1107 if (unlikely(desccnt > 1 ||
1108 rxdesc->descwb.errstat & RXWBERR_ALLERR)) {
1109
1110 if (rxdesc->descwb.errstat & RXWBERR_CRCERR)
1111 ++(NET_STAT(jme).rx_crc_errors);
1112 else if (rxdesc->descwb.errstat & RXWBERR_OVERUN)
1113 ++(NET_STAT(jme).rx_fifo_errors);
1114 else
1115 ++(NET_STAT(jme).rx_errors);
1116
1117 if (desccnt > 1)
1118 limit -= desccnt - 1;
1119
1120 for (j = i, ccnt = desccnt ; ccnt-- ; ) {
1121 jme_set_clean_rxdesc(jme, j);
1122 j = (j + 1) & (mask);
1123 }
1124
1125 } else {
1126 jme_alloc_and_feed_skb(jme, i);
1127 }
1128
1129 i = (i + desccnt) & (mask);
1130 }
1131
1132out:
1133 atomic_set(&rxring->next_to_clean, i);
1134
1135out_inc:
1136 atomic_inc(&jme->rx_cleaning);
1137
1138 return limit > 0 ? limit : 0;
1139
1140}
1141
1142static void
1143jme_attempt_pcc(struct dynpcc_info *dpi, int atmp)
1144{
1145 if (likely(atmp == dpi->cur)) {
1146 dpi->cnt = 0;
1147 return;
1148 }
1149
1150 if (dpi->attempt == atmp) {
1151 ++(dpi->cnt);
1152 } else {
1153 dpi->attempt = atmp;
1154 dpi->cnt = 0;
1155 }
1156
1157}
1158
1159static void
1160jme_dynamic_pcc(struct jme_adapter *jme)
1161{
1162 register struct dynpcc_info *dpi = &(jme->dpi);
1163
1164 if ((NET_STAT(jme).rx_bytes - dpi->last_bytes) > PCC_P3_THRESHOLD)
1165 jme_attempt_pcc(dpi, PCC_P3);
Joe Perches8e95a202009-12-03 07:58:21 +00001166 else if ((NET_STAT(jme).rx_packets - dpi->last_pkts) > PCC_P2_THRESHOLD ||
1167 dpi->intr_cnt > PCC_INTR_THRESHOLD)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001168 jme_attempt_pcc(dpi, PCC_P2);
1169 else
1170 jme_attempt_pcc(dpi, PCC_P1);
1171
1172 if (unlikely(dpi->attempt != dpi->cur && dpi->cnt > 5)) {
1173 if (dpi->attempt < dpi->cur)
1174 tasklet_schedule(&jme->rxclean_task);
1175 jme_set_rx_pcc(jme, dpi->attempt);
1176 dpi->cur = dpi->attempt;
1177 dpi->cnt = 0;
1178 }
1179}
1180
1181static void
1182jme_start_pcc_timer(struct jme_adapter *jme)
1183{
1184 struct dynpcc_info *dpi = &(jme->dpi);
1185 dpi->last_bytes = NET_STAT(jme).rx_bytes;
1186 dpi->last_pkts = NET_STAT(jme).rx_packets;
1187 dpi->intr_cnt = 0;
1188 jwrite32(jme, JME_TMCSR,
1189 TMCSR_EN | ((0xFFFFFF - PCC_INTERVAL_US) & TMCSR_CNT));
1190}
1191
1192static inline void
1193jme_stop_pcc_timer(struct jme_adapter *jme)
1194{
1195 jwrite32(jme, JME_TMCSR, 0);
1196}
1197
1198static void
1199jme_shutdown_nic(struct jme_adapter *jme)
1200{
1201 u32 phylink;
1202
1203 phylink = jme_linkstat_from_phy(jme);
1204
1205 if (!(phylink & PHY_LINK_UP)) {
1206 /*
1207 * Disable all interrupt before issue timer
1208 */
1209 jme_stop_irq(jme);
1210 jwrite32(jme, JME_TIMER2, TMCSR_EN | 0xFFFFFE);
1211 }
1212}
1213
1214static void
1215jme_pcc_tasklet(unsigned long arg)
1216{
1217 struct jme_adapter *jme = (struct jme_adapter *)arg;
1218 struct net_device *netdev = jme->dev;
1219
1220 if (unlikely(test_bit(JME_FLAG_SHUTDOWN, &jme->flags))) {
1221 jme_shutdown_nic(jme);
1222 return;
1223 }
1224
1225 if (unlikely(!netif_carrier_ok(netdev) ||
1226 (atomic_read(&jme->link_changing) != 1)
1227 )) {
1228 jme_stop_pcc_timer(jme);
1229 return;
1230 }
1231
1232 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
1233 jme_dynamic_pcc(jme);
1234
1235 jme_start_pcc_timer(jme);
1236}
1237
1238static inline void
1239jme_polling_mode(struct jme_adapter *jme)
1240{
1241 jme_set_rx_pcc(jme, PCC_OFF);
1242}
1243
1244static inline void
1245jme_interrupt_mode(struct jme_adapter *jme)
1246{
1247 jme_set_rx_pcc(jme, PCC_P1);
1248}
1249
1250static inline int
1251jme_pseudo_hotplug_enabled(struct jme_adapter *jme)
1252{
1253 u32 apmc;
1254 apmc = jread32(jme, JME_APMC);
1255 return apmc & JME_APMC_PSEUDO_HP_EN;
1256}
1257
1258static void
1259jme_start_shutdown_timer(struct jme_adapter *jme)
1260{
1261 u32 apmc;
1262
1263 apmc = jread32(jme, JME_APMC) | JME_APMC_PCIE_SD_EN;
1264 apmc &= ~JME_APMC_EPIEN_CTRL;
1265 if (!no_extplug) {
1266 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_EN);
1267 wmb();
1268 }
1269 jwrite32f(jme, JME_APMC, apmc);
1270
1271 jwrite32f(jme, JME_TIMER2, 0);
1272 set_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1273 jwrite32(jme, JME_TMCSR,
1274 TMCSR_EN | ((0xFFFFFF - APMC_PHP_SHUTDOWN_DELAY) & TMCSR_CNT));
1275}
1276
1277static void
1278jme_stop_shutdown_timer(struct jme_adapter *jme)
1279{
1280 u32 apmc;
1281
1282 jwrite32f(jme, JME_TMCSR, 0);
1283 jwrite32f(jme, JME_TIMER2, 0);
1284 clear_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1285
1286 apmc = jread32(jme, JME_APMC);
1287 apmc &= ~(JME_APMC_PCIE_SD_EN | JME_APMC_EPIEN_CTRL);
1288 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_DIS);
1289 wmb();
1290 jwrite32f(jme, JME_APMC, apmc);
1291}
1292
1293static void
1294jme_link_change_tasklet(unsigned long arg)
1295{
1296 struct jme_adapter *jme = (struct jme_adapter *)arg;
1297 struct net_device *netdev = jme->dev;
1298 int rc;
1299
1300 while (!atomic_dec_and_test(&jme->link_changing)) {
1301 atomic_inc(&jme->link_changing);
Joe Perches49d70c42010-09-04 22:21:05 +00001302 netif_info(jme, intr, jme->dev, "Get link change lock failed\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001303 while (atomic_read(&jme->link_changing) != 1)
Joe Perches49d70c42010-09-04 22:21:05 +00001304 netif_info(jme, intr, jme->dev, "Waiting link change lock\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001305 }
1306
1307 if (jme_check_link(netdev, 1) && jme->old_mtu == netdev->mtu)
1308 goto out;
1309
1310 jme->old_mtu = netdev->mtu;
1311 netif_stop_queue(netdev);
1312 if (jme_pseudo_hotplug_enabled(jme))
1313 jme_stop_shutdown_timer(jme);
1314
1315 jme_stop_pcc_timer(jme);
1316 tasklet_disable(&jme->txclean_task);
1317 tasklet_disable(&jme->rxclean_task);
1318 tasklet_disable(&jme->rxempty_task);
1319
1320 if (netif_carrier_ok(netdev)) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001321 jme_disable_rx_engine(jme);
1322 jme_disable_tx_engine(jme);
1323 jme_reset_mac_processor(jme);
1324 jme_free_rx_resources(jme);
1325 jme_free_tx_resources(jme);
1326
1327 if (test_bit(JME_FLAG_POLL, &jme->flags))
1328 jme_polling_mode(jme);
1329
1330 netif_carrier_off(netdev);
1331 }
1332
1333 jme_check_link(netdev, 0);
1334 if (netif_carrier_ok(netdev)) {
1335 rc = jme_setup_rx_resources(jme);
1336 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001337 pr_err("Allocating resources for RX error, Device STOPPED!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001338 goto out_enable_tasklet;
1339 }
1340
1341 rc = jme_setup_tx_resources(jme);
1342 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001343 pr_err("Allocating resources for TX error, Device STOPPED!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001344 goto err_out_free_rx_resources;
1345 }
1346
1347 jme_enable_rx_engine(jme);
1348 jme_enable_tx_engine(jme);
1349
1350 netif_start_queue(netdev);
1351
1352 if (test_bit(JME_FLAG_POLL, &jme->flags))
1353 jme_interrupt_mode(jme);
1354
1355 jme_start_pcc_timer(jme);
1356 } else if (jme_pseudo_hotplug_enabled(jme)) {
1357 jme_start_shutdown_timer(jme);
1358 }
1359
1360 goto out_enable_tasklet;
1361
1362err_out_free_rx_resources:
1363 jme_free_rx_resources(jme);
1364out_enable_tasklet:
1365 tasklet_enable(&jme->txclean_task);
1366 tasklet_hi_enable(&jme->rxclean_task);
1367 tasklet_hi_enable(&jme->rxempty_task);
1368out:
1369 atomic_inc(&jme->link_changing);
1370}
1371
1372static void
1373jme_rx_clean_tasklet(unsigned long arg)
1374{
1375 struct jme_adapter *jme = (struct jme_adapter *)arg;
1376 struct dynpcc_info *dpi = &(jme->dpi);
1377
1378 jme_process_receive(jme, jme->rx_ring_size);
1379 ++(dpi->intr_cnt);
1380
1381}
1382
1383static int
1384jme_poll(JME_NAPI_HOLDER(holder), JME_NAPI_WEIGHT(budget))
1385{
1386 struct jme_adapter *jme = jme_napi_priv(holder);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001387 int rest;
1388
1389 rest = jme_process_receive(jme, JME_NAPI_WEIGHT_VAL(budget));
1390
1391 while (atomic_read(&jme->rx_empty) > 0) {
1392 atomic_dec(&jme->rx_empty);
1393 ++(NET_STAT(jme).rx_dropped);
1394 jme_restart_rx_engine(jme);
1395 }
1396 atomic_inc(&jme->rx_empty);
1397
1398 if (rest) {
1399 JME_RX_COMPLETE(netdev, holder);
1400 jme_interrupt_mode(jme);
1401 }
1402
1403 JME_NAPI_WEIGHT_SET(budget, rest);
1404 return JME_NAPI_WEIGHT_VAL(budget) - rest;
1405}
1406
1407static void
1408jme_rx_empty_tasklet(unsigned long arg)
1409{
1410 struct jme_adapter *jme = (struct jme_adapter *)arg;
1411
1412 if (unlikely(atomic_read(&jme->link_changing) != 1))
1413 return;
1414
1415 if (unlikely(!netif_carrier_ok(jme->dev)))
1416 return;
1417
Joe Perchesf8502ce2010-02-09 11:49:51 +00001418 netif_info(jme, rx_status, jme->dev, "RX Queue Full!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001419
1420 jme_rx_clean_tasklet(arg);
1421
1422 while (atomic_read(&jme->rx_empty) > 0) {
1423 atomic_dec(&jme->rx_empty);
1424 ++(NET_STAT(jme).rx_dropped);
1425 jme_restart_rx_engine(jme);
1426 }
1427 atomic_inc(&jme->rx_empty);
1428}
1429
1430static void
1431jme_wake_queue_if_stopped(struct jme_adapter *jme)
1432{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00001433 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001434
1435 smp_wmb();
1436 if (unlikely(netif_queue_stopped(jme->dev) &&
1437 atomic_read(&txring->nr_free) >= (jme->tx_wake_threshold))) {
Joe Perches49d70c42010-09-04 22:21:05 +00001438 netif_info(jme, tx_done, jme->dev, "TX Queue Waked\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001439 netif_wake_queue(jme->dev);
1440 }
1441
1442}
1443
1444static void
1445jme_tx_clean_tasklet(unsigned long arg)
1446{
1447 struct jme_adapter *jme = (struct jme_adapter *)arg;
1448 struct jme_ring *txring = &(jme->txring[0]);
1449 struct txdesc *txdesc = txring->desc;
1450 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi, *ttxbi;
1451 int i, j, cnt = 0, max, err, mask;
1452
Joe Perches49d70c42010-09-04 22:21:05 +00001453 tx_dbg(jme, "Into txclean\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001454
1455 if (unlikely(!atomic_dec_and_test(&jme->tx_cleaning)))
1456 goto out;
1457
1458 if (unlikely(atomic_read(&jme->link_changing) != 1))
1459 goto out;
1460
1461 if (unlikely(!netif_carrier_ok(jme->dev)))
1462 goto out;
1463
1464 max = jme->tx_ring_size - atomic_read(&txring->nr_free);
1465 mask = jme->tx_ring_mask;
1466
1467 for (i = atomic_read(&txring->next_to_clean) ; cnt < max ; ) {
1468
1469 ctxbi = txbi + i;
1470
1471 if (likely(ctxbi->skb &&
1472 !(txdesc[i].descwb.flags & TXWBFLAG_OWN))) {
1473
1474 tx_dbg(jme, "txclean: %d+%d@%lu\n",
Joe Perches49d70c42010-09-04 22:21:05 +00001475 i, ctxbi->nr_desc, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001476
1477 err = txdesc[i].descwb.flags & TXWBFLAG_ALLERR;
1478
1479 for (j = 1 ; j < ctxbi->nr_desc ; ++j) {
1480 ttxbi = txbi + ((i + j) & (mask));
1481 txdesc[(i + j) & (mask)].dw[0] = 0;
1482
1483 pci_unmap_page(jme->pdev,
1484 ttxbi->mapping,
1485 ttxbi->len,
1486 PCI_DMA_TODEVICE);
1487
1488 ttxbi->mapping = 0;
1489 ttxbi->len = 0;
1490 }
1491
1492 dev_kfree_skb(ctxbi->skb);
1493
1494 cnt += ctxbi->nr_desc;
1495
1496 if (unlikely(err)) {
1497 ++(NET_STAT(jme).tx_carrier_errors);
1498 } else {
1499 ++(NET_STAT(jme).tx_packets);
1500 NET_STAT(jme).tx_bytes += ctxbi->len;
1501 }
1502
1503 ctxbi->skb = NULL;
1504 ctxbi->len = 0;
1505 ctxbi->start_xmit = 0;
1506
1507 } else {
1508 break;
1509 }
1510
1511 i = (i + ctxbi->nr_desc) & mask;
1512
1513 ctxbi->nr_desc = 0;
1514 }
1515
Joe Perches49d70c42010-09-04 22:21:05 +00001516 tx_dbg(jme, "txclean: done %d@%lu\n", i, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001517 atomic_set(&txring->next_to_clean, i);
1518 atomic_add(cnt, &txring->nr_free);
1519
1520 jme_wake_queue_if_stopped(jme);
1521
1522out:
1523 atomic_inc(&jme->tx_cleaning);
1524}
1525
1526static void
1527jme_intr_msi(struct jme_adapter *jme, u32 intrstat)
1528{
1529 /*
1530 * Disable interrupt
1531 */
1532 jwrite32f(jme, JME_IENC, INTR_ENABLE);
1533
1534 if (intrstat & (INTR_LINKCH | INTR_SWINTR)) {
1535 /*
1536 * Link change event is critical
1537 * all other events are ignored
1538 */
1539 jwrite32(jme, JME_IEVE, intrstat);
1540 tasklet_schedule(&jme->linkch_task);
1541 goto out_reenable;
1542 }
1543
1544 if (intrstat & INTR_TMINTR) {
1545 jwrite32(jme, JME_IEVE, INTR_TMINTR);
1546 tasklet_schedule(&jme->pcc_task);
1547 }
1548
1549 if (intrstat & (INTR_PCCTXTO | INTR_PCCTX)) {
1550 jwrite32(jme, JME_IEVE, INTR_PCCTXTO | INTR_PCCTX | INTR_TX0);
1551 tasklet_schedule(&jme->txclean_task);
1552 }
1553
1554 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1555 jwrite32(jme, JME_IEVE, (intrstat & (INTR_PCCRX0TO |
1556 INTR_PCCRX0 |
1557 INTR_RX0EMP)) |
1558 INTR_RX0);
1559 }
1560
1561 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
1562 if (intrstat & INTR_RX0EMP)
1563 atomic_inc(&jme->rx_empty);
1564
1565 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1566 if (likely(JME_RX_SCHEDULE_PREP(jme))) {
1567 jme_polling_mode(jme);
1568 JME_RX_SCHEDULE(jme);
1569 }
1570 }
1571 } else {
1572 if (intrstat & INTR_RX0EMP) {
1573 atomic_inc(&jme->rx_empty);
1574 tasklet_hi_schedule(&jme->rxempty_task);
1575 } else if (intrstat & (INTR_PCCRX0TO | INTR_PCCRX0)) {
1576 tasklet_hi_schedule(&jme->rxclean_task);
1577 }
1578 }
1579
1580out_reenable:
1581 /*
1582 * Re-enable interrupt
1583 */
1584 jwrite32f(jme, JME_IENS, INTR_ENABLE);
1585}
1586
1587static irqreturn_t
1588jme_intr(int irq, void *dev_id)
1589{
1590 struct net_device *netdev = dev_id;
1591 struct jme_adapter *jme = netdev_priv(netdev);
1592 u32 intrstat;
1593
1594 intrstat = jread32(jme, JME_IEVE);
1595
1596 /*
1597 * Check if it's really an interrupt for us
1598 */
akeemting576b5222008-10-08 19:50:03 -07001599 if (unlikely((intrstat & INTR_ENABLE) == 0))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001600 return IRQ_NONE;
1601
1602 /*
1603 * Check if the device still exist
1604 */
1605 if (unlikely(intrstat == ~((typeof(intrstat))0)))
1606 return IRQ_NONE;
1607
1608 jme_intr_msi(jme, intrstat);
1609
1610 return IRQ_HANDLED;
1611}
1612
1613static irqreturn_t
1614jme_msi(int irq, void *dev_id)
1615{
1616 struct net_device *netdev = dev_id;
1617 struct jme_adapter *jme = netdev_priv(netdev);
1618 u32 intrstat;
1619
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00001620 intrstat = jread32(jme, JME_IEVE);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001621
1622 jme_intr_msi(jme, intrstat);
1623
1624 return IRQ_HANDLED;
1625}
1626
1627static void
1628jme_reset_link(struct jme_adapter *jme)
1629{
1630 jwrite32(jme, JME_TMCSR, TMCSR_SWIT);
1631}
1632
1633static void
1634jme_restart_an(struct jme_adapter *jme)
1635{
1636 u32 bmcr;
1637
1638 spin_lock_bh(&jme->phy_lock);
1639 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1640 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
1641 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1642 spin_unlock_bh(&jme->phy_lock);
1643}
1644
1645static int
1646jme_request_irq(struct jme_adapter *jme)
1647{
1648 int rc;
1649 struct net_device *netdev = jme->dev;
1650 irq_handler_t handler = jme_intr;
1651 int irq_flags = IRQF_SHARED;
1652
1653 if (!pci_enable_msi(jme->pdev)) {
1654 set_bit(JME_FLAG_MSI, &jme->flags);
1655 handler = jme_msi;
1656 irq_flags = 0;
1657 }
1658
1659 rc = request_irq(jme->pdev->irq, handler, irq_flags, netdev->name,
1660 netdev);
1661 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001662 netdev_err(netdev,
1663 "Unable to request %s interrupt (return: %d)\n",
1664 test_bit(JME_FLAG_MSI, &jme->flags) ? "MSI" : "INTx",
1665 rc);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001666
1667 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1668 pci_disable_msi(jme->pdev);
1669 clear_bit(JME_FLAG_MSI, &jme->flags);
1670 }
1671 } else {
1672 netdev->irq = jme->pdev->irq;
1673 }
1674
1675 return rc;
1676}
1677
1678static void
1679jme_free_irq(struct jme_adapter *jme)
1680{
1681 free_irq(jme->pdev->irq, jme->dev);
1682 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1683 pci_disable_msi(jme->pdev);
1684 clear_bit(JME_FLAG_MSI, &jme->flags);
1685 jme->dev->irq = jme->pdev->irq;
1686 }
1687}
1688
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001689static inline void
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001690jme_new_phy_on(struct jme_adapter *jme)
1691{
1692 u32 reg;
1693
1694 reg = jread32(jme, JME_PHY_PWR);
1695 reg &= ~(PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1696 PHY_PWR_DWN2 | PHY_PWR_CLKSEL);
1697 jwrite32(jme, JME_PHY_PWR, reg);
1698
1699 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1700 reg &= ~PE1_GPREG0_PBG;
1701 reg |= PE1_GPREG0_ENBG;
1702 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1703}
1704
1705static inline void
1706jme_new_phy_off(struct jme_adapter *jme)
1707{
1708 u32 reg;
1709
1710 reg = jread32(jme, JME_PHY_PWR);
1711 reg |= PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1712 PHY_PWR_DWN2 | PHY_PWR_CLKSEL;
1713 jwrite32(jme, JME_PHY_PWR, reg);
1714
1715 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1716 reg &= ~PE1_GPREG0_PBG;
1717 reg |= PE1_GPREG0_PDD3COLD;
1718 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1719}
1720
1721static inline void
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001722jme_phy_on(struct jme_adapter *jme)
1723{
1724 u32 bmcr;
1725
1726 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1727 bmcr &= ~BMCR_PDOWN;
1728 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001729
1730 if (new_phy_power_ctrl(jme->chip_main_rev))
1731 jme_new_phy_on(jme);
1732}
1733
1734static inline void
1735jme_phy_off(struct jme_adapter *jme)
1736{
1737 u32 bmcr;
1738
1739 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1740 bmcr |= BMCR_PDOWN;
1741 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1742
1743 if (new_phy_power_ctrl(jme->chip_main_rev))
1744 jme_new_phy_off(jme);
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001745}
1746
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001747static int
Aries Leec4860ba2011-11-21 10:20:42 +00001748jme_phy_specreg_read(struct jme_adapter *jme, u32 specreg)
1749{
1750 u32 phy_addr;
1751
1752 phy_addr = JM_PHY_SPEC_REG_READ | specreg;
1753 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1754 phy_addr);
1755 return jme_mdio_read(jme->dev, jme->mii_if.phy_id,
1756 JM_PHY_SPEC_DATA_REG);
1757}
1758
1759static void
1760jme_phy_specreg_write(struct jme_adapter *jme, u32 ext_reg, u32 phy_data)
1761{
1762 u32 phy_addr;
1763
1764 phy_addr = JM_PHY_SPEC_REG_WRITE | ext_reg;
1765 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_DATA_REG,
1766 phy_data);
1767 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1768 phy_addr);
1769}
1770
1771static int
1772jme_phy_calibration(struct jme_adapter *jme)
1773{
1774 u32 ctrl1000, phy_data;
1775
1776 jme_phy_off(jme);
1777 jme_phy_on(jme);
1778 /* Enabel PHY test mode 1 */
1779 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1780 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1781 ctrl1000 |= PHY_GAD_TEST_MODE_1;
1782 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1783
1784 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1785 phy_data &= ~JM_PHY_EXT_COMM_2_CALI_MODE_0;
1786 phy_data |= JM_PHY_EXT_COMM_2_CALI_LATCH |
1787 JM_PHY_EXT_COMM_2_CALI_ENABLE;
1788 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1789 msleep(20);
1790 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1791 phy_data &= ~(JM_PHY_EXT_COMM_2_CALI_ENABLE |
1792 JM_PHY_EXT_COMM_2_CALI_MODE_0 |
1793 JM_PHY_EXT_COMM_2_CALI_LATCH);
1794 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1795
1796 /* Disable PHY test mode */
1797 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1798 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1799 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1800 return 0;
1801}
1802
1803static int
1804jme_phy_setEA(struct jme_adapter *jme)
1805{
1806 u32 phy_comm0 = 0, phy_comm1 = 0;
1807 u8 nic_ctrl;
1808
1809 pci_read_config_byte(jme->pdev, PCI_PRIV_SHARE_NICCTRL, &nic_ctrl);
1810 if ((nic_ctrl & 0x3) == JME_FLAG_PHYEA_ENABLE)
1811 return 0;
1812
1813 switch (jme->pdev->device) {
1814 case PCI_DEVICE_ID_JMICRON_JMC250:
1815 if (((jme->chip_main_rev == 5) &&
1816 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1817 (jme->chip_sub_rev == 3))) ||
1818 (jme->chip_main_rev >= 6)) {
1819 phy_comm0 = 0x008A;
1820 phy_comm1 = 0x4109;
1821 }
1822 if ((jme->chip_main_rev == 3) &&
1823 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1824 phy_comm0 = 0xE088;
1825 break;
1826 case PCI_DEVICE_ID_JMICRON_JMC260:
1827 if (((jme->chip_main_rev == 5) &&
1828 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1829 (jme->chip_sub_rev == 3))) ||
1830 (jme->chip_main_rev >= 6)) {
1831 phy_comm0 = 0x008A;
1832 phy_comm1 = 0x4109;
1833 }
1834 if ((jme->chip_main_rev == 3) &&
1835 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1836 phy_comm0 = 0xE088;
1837 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 0))
1838 phy_comm0 = 0x608A;
1839 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 2))
1840 phy_comm0 = 0x408A;
1841 break;
1842 default:
1843 return -ENODEV;
1844 }
1845 if (phy_comm0)
1846 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_0_REG, phy_comm0);
1847 if (phy_comm1)
1848 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_1_REG, phy_comm1);
1849
1850 return 0;
1851}
1852
1853static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001854jme_open(struct net_device *netdev)
1855{
1856 struct jme_adapter *jme = netdev_priv(netdev);
1857 int rc;
1858
1859 jme_clear_pm(jme);
1860 JME_NAPI_ENABLE(jme);
1861
Guo-Fu Tseng38ed0c22009-07-06 04:37:52 +00001862 tasklet_enable(&jme->linkch_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001863 tasklet_enable(&jme->txclean_task);
1864 tasklet_hi_enable(&jme->rxclean_task);
1865 tasklet_hi_enable(&jme->rxempty_task);
1866
1867 rc = jme_request_irq(jme);
1868 if (rc)
1869 goto err_out;
1870
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001871 jme_start_irq(jme);
1872
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001873 jme_phy_on(jme);
1874 if (test_bit(JME_FLAG_SSET, &jme->flags))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001875 jme_set_settings(netdev, &jme->old_ecmd);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001876 else
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001877 jme_reset_phy_processor(jme);
Aries Leec4860ba2011-11-21 10:20:42 +00001878 jme_phy_calibration(jme);
1879 jme_phy_setEA(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001880 jme_reset_link(jme);
1881
1882 return 0;
1883
1884err_out:
1885 netif_stop_queue(netdev);
1886 netif_carrier_off(netdev);
1887 return rc;
1888}
1889
1890static void
1891jme_set_100m_half(struct jme_adapter *jme)
1892{
1893 u32 bmcr, tmp;
1894
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001895 jme_phy_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001896 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1897 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
1898 BMCR_SPEED1000 | BMCR_FULLDPLX);
1899 tmp |= BMCR_SPEED100;
1900
1901 if (bmcr != tmp)
1902 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, tmp);
1903
1904 if (jme->fpgaver)
1905 jwrite32(jme, JME_GHC, GHC_SPEED_100M | GHC_LINK_POLL);
1906 else
1907 jwrite32(jme, JME_GHC, GHC_SPEED_100M);
1908}
1909
1910#define JME_WAIT_LINK_TIME 2000 /* 2000ms */
1911static void
1912jme_wait_link(struct jme_adapter *jme)
1913{
1914 u32 phylink, to = JME_WAIT_LINK_TIME;
1915
1916 mdelay(1000);
1917 phylink = jme_linkstat_from_phy(jme);
1918 while (!(phylink & PHY_LINK_UP) && (to -= 10) > 0) {
1919 mdelay(10);
1920 phylink = jme_linkstat_from_phy(jme);
1921 }
1922}
1923
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001924static void
1925jme_powersave_phy(struct jme_adapter *jme)
1926{
1927 if (jme->reg_pmcs) {
1928 jme_set_100m_half(jme);
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001929 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
1930 jme_wait_link(jme);
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00001931 jme_clear_pm(jme);
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001932 } else {
1933 jme_phy_off(jme);
1934 }
1935}
1936
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001937static int
1938jme_close(struct net_device *netdev)
1939{
1940 struct jme_adapter *jme = netdev_priv(netdev);
1941
1942 netif_stop_queue(netdev);
1943 netif_carrier_off(netdev);
1944
1945 jme_stop_irq(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001946 jme_free_irq(jme);
1947
1948 JME_NAPI_DISABLE(jme);
1949
Guo-Fu Tseng38ed0c22009-07-06 04:37:52 +00001950 tasklet_disable(&jme->linkch_task);
1951 tasklet_disable(&jme->txclean_task);
1952 tasklet_disable(&jme->rxclean_task);
1953 tasklet_disable(&jme->rxempty_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001954
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001955 jme_disable_rx_engine(jme);
1956 jme_disable_tx_engine(jme);
1957 jme_reset_mac_processor(jme);
1958 jme_free_rx_resources(jme);
1959 jme_free_tx_resources(jme);
1960 jme->phylink = 0;
1961 jme_phy_off(jme);
1962
1963 return 0;
1964}
1965
1966static int
1967jme_alloc_txdesc(struct jme_adapter *jme,
1968 struct sk_buff *skb)
1969{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00001970 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001971 int idx, nr_alloc, mask = jme->tx_ring_mask;
1972
1973 idx = txring->next_to_use;
1974 nr_alloc = skb_shinfo(skb)->nr_frags + 2;
1975
1976 if (unlikely(atomic_read(&txring->nr_free) < nr_alloc))
1977 return -1;
1978
1979 atomic_sub(nr_alloc, &txring->nr_free);
1980
1981 txring->next_to_use = (txring->next_to_use + nr_alloc) & mask;
1982
1983 return idx;
1984}
1985
1986static void
1987jme_fill_tx_map(struct pci_dev *pdev,
1988 struct txdesc *txdesc,
1989 struct jme_buffer_info *txbi,
1990 struct page *page,
1991 u32 page_offset,
1992 u32 len,
Michał Mirosław3ad9b352011-11-16 14:05:33 +00001993 bool hidma)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001994{
1995 dma_addr_t dmaaddr;
1996
1997 dmaaddr = pci_map_page(pdev,
1998 page,
1999 page_offset,
2000 len,
2001 PCI_DMA_TODEVICE);
2002
2003 pci_dma_sync_single_for_device(pdev,
2004 dmaaddr,
2005 len,
2006 PCI_DMA_TODEVICE);
2007
2008 txdesc->dw[0] = 0;
2009 txdesc->dw[1] = 0;
2010 txdesc->desc2.flags = TXFLAG_OWN;
2011 txdesc->desc2.flags |= (hidma) ? TXFLAG_64BIT : 0;
2012 txdesc->desc2.datalen = cpu_to_le16(len);
2013 txdesc->desc2.bufaddrh = cpu_to_le32((__u64)dmaaddr >> 32);
2014 txdesc->desc2.bufaddrl = cpu_to_le32(
2015 (__u64)dmaaddr & 0xFFFFFFFFUL);
2016
2017 txbi->mapping = dmaaddr;
2018 txbi->len = len;
2019}
2020
2021static void
2022jme_map_tx_skb(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2023{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002024 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002025 struct txdesc *txdesc = txring->desc, *ctxdesc;
2026 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
Michał Mirosław3ad9b352011-11-16 14:05:33 +00002027 bool hidma = jme->dev->features & NETIF_F_HIGHDMA;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002028 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2029 int mask = jme->tx_ring_mask;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002030 const struct skb_frag_struct *frag;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002031 u32 len;
2032
2033 for (i = 0 ; i < nr_frags ; ++i) {
2034 frag = &skb_shinfo(skb)->frags[i];
2035 ctxdesc = txdesc + ((idx + i + 2) & (mask));
2036 ctxbi = txbi + ((idx + i + 2) & (mask));
2037
Ian Campbellf3273582011-08-31 00:46:54 +00002038 jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi,
2039 skb_frag_page(frag),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002040 frag->page_offset, skb_frag_size(frag), hidma);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002041 }
2042
2043 len = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
2044 ctxdesc = txdesc + ((idx + 1) & (mask));
2045 ctxbi = txbi + ((idx + 1) & (mask));
2046 jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi, virt_to_page(skb->data),
2047 offset_in_page(skb->data), len, hidma);
2048
2049}
2050
2051static int
2052jme_expand_header(struct jme_adapter *jme, struct sk_buff *skb)
2053{
2054 if (unlikely(skb_shinfo(skb)->gso_size &&
2055 skb_header_cloned(skb) &&
2056 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
2057 dev_kfree_skb(skb);
2058 return -1;
2059 }
2060
2061 return 0;
2062}
2063
2064static int
Harvey Harrison31c221c2008-11-19 15:50:59 -08002065jme_tx_tso(struct sk_buff *skb, __le16 *mss, u8 *flags)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002066{
Harvey Harrison31c221c2008-11-19 15:50:59 -08002067 *mss = cpu_to_le16(skb_shinfo(skb)->gso_size << TXDESC_MSS_SHIFT);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002068 if (*mss) {
2069 *flags |= TXFLAG_LSEN;
2070
2071 if (skb->protocol == htons(ETH_P_IP)) {
2072 struct iphdr *iph = ip_hdr(skb);
2073
2074 iph->check = 0;
2075 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2076 iph->daddr, 0,
2077 IPPROTO_TCP,
2078 0);
2079 } else {
2080 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2081
2082 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ip6h->saddr,
2083 &ip6h->daddr, 0,
2084 IPPROTO_TCP,
2085 0);
2086 }
2087
2088 return 0;
2089 }
2090
2091 return 1;
2092}
2093
2094static void
2095jme_tx_csum(struct jme_adapter *jme, struct sk_buff *skb, u8 *flags)
2096{
2097 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2098 u8 ip_proto;
2099
2100 switch (skb->protocol) {
2101 case htons(ETH_P_IP):
2102 ip_proto = ip_hdr(skb)->protocol;
2103 break;
2104 case htons(ETH_P_IPV6):
2105 ip_proto = ipv6_hdr(skb)->nexthdr;
2106 break;
2107 default:
2108 ip_proto = 0;
2109 break;
2110 }
2111
2112 switch (ip_proto) {
2113 case IPPROTO_TCP:
2114 *flags |= TXFLAG_TCPCS;
2115 break;
2116 case IPPROTO_UDP:
2117 *flags |= TXFLAG_UDPCS;
2118 break;
2119 default:
Joe Perches49d70c42010-09-04 22:21:05 +00002120 netif_err(jme, tx_err, jme->dev, "Error upper layer protocol\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002121 break;
2122 }
2123 }
2124}
2125
2126static inline void
Harvey Harrison31c221c2008-11-19 15:50:59 -08002127jme_tx_vlan(struct sk_buff *skb, __le16 *vlan, u8 *flags)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002128{
2129 if (vlan_tx_tag_present(skb)) {
2130 *flags |= TXFLAG_TAGON;
Harvey Harrison31c221c2008-11-19 15:50:59 -08002131 *vlan = cpu_to_le16(vlan_tx_tag_get(skb));
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002132 }
2133}
2134
2135static int
Guo-Fu Tseng7f7fd2d2009-02-27 17:57:01 +00002136jme_fill_tx_desc(struct jme_adapter *jme, struct sk_buff *skb, int idx)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002137{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002138 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002139 struct txdesc *txdesc;
2140 struct jme_buffer_info *txbi;
2141 u8 flags;
2142
2143 txdesc = (struct txdesc *)txring->desc + idx;
2144 txbi = txring->bufinf + idx;
2145
2146 txdesc->dw[0] = 0;
2147 txdesc->dw[1] = 0;
2148 txdesc->dw[2] = 0;
2149 txdesc->dw[3] = 0;
2150 txdesc->desc1.pktsize = cpu_to_le16(skb->len);
2151 /*
2152 * Set OWN bit at final.
2153 * When kernel transmit faster than NIC.
2154 * And NIC trying to send this descriptor before we tell
2155 * it to start sending this TX queue.
2156 * Other fields are already filled correctly.
2157 */
2158 wmb();
2159 flags = TXFLAG_OWN | TXFLAG_INT;
2160 /*
2161 * Set checksum flags while not tso
2162 */
2163 if (jme_tx_tso(skb, &txdesc->desc1.mss, &flags))
2164 jme_tx_csum(jme, skb, &flags);
2165 jme_tx_vlan(skb, &txdesc->desc1.vlan, &flags);
Guo-Fu Tseng7f7fd2d2009-02-27 17:57:01 +00002166 jme_map_tx_skb(jme, skb, idx);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002167 txdesc->desc1.flags = flags;
2168 /*
2169 * Set tx buffer info after telling NIC to send
2170 * For better tx_clean timing
2171 */
2172 wmb();
2173 txbi->nr_desc = skb_shinfo(skb)->nr_frags + 2;
2174 txbi->skb = skb;
2175 txbi->len = skb->len;
2176 txbi->start_xmit = jiffies;
2177 if (!txbi->start_xmit)
2178 txbi->start_xmit = (0UL-1);
2179
2180 return 0;
2181}
2182
2183static void
2184jme_stop_queue_if_full(struct jme_adapter *jme)
2185{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002186 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002187 struct jme_buffer_info *txbi = txring->bufinf;
2188 int idx = atomic_read(&txring->next_to_clean);
2189
2190 txbi += idx;
2191
2192 smp_wmb();
2193 if (unlikely(atomic_read(&txring->nr_free) < (MAX_SKB_FRAGS+2))) {
2194 netif_stop_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002195 netif_info(jme, tx_queued, jme->dev, "TX Queue Paused\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002196 smp_wmb();
2197 if (atomic_read(&txring->nr_free)
2198 >= (jme->tx_wake_threshold)) {
2199 netif_wake_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002200 netif_info(jme, tx_queued, jme->dev, "TX Queue Fast Waked\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002201 }
2202 }
2203
2204 if (unlikely(txbi->start_xmit &&
2205 (jiffies - txbi->start_xmit) >= TX_TIMEOUT &&
2206 txbi->skb)) {
2207 netif_stop_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002208 netif_info(jme, tx_queued, jme->dev,
2209 "TX Queue Stopped %d@%lu\n", idx, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002210 }
2211}
2212
2213/*
2214 * This function is already protected by netif_tx_lock()
2215 */
2216
Stephen Hemminger613573252009-08-31 19:50:58 +00002217static netdev_tx_t
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002218jme_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2219{
2220 struct jme_adapter *jme = netdev_priv(netdev);
2221 int idx;
2222
2223 if (unlikely(jme_expand_header(jme, skb))) {
2224 ++(NET_STAT(jme).tx_dropped);
2225 return NETDEV_TX_OK;
2226 }
2227
2228 idx = jme_alloc_txdesc(jme, skb);
2229
2230 if (unlikely(idx < 0)) {
2231 netif_stop_queue(netdev);
Joe Perches49d70c42010-09-04 22:21:05 +00002232 netif_err(jme, tx_err, jme->dev,
2233 "BUG! Tx ring full when queue awake!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002234
2235 return NETDEV_TX_BUSY;
2236 }
2237
Guo-Fu Tseng7f7fd2d2009-02-27 17:57:01 +00002238 jme_fill_tx_desc(jme, skb, idx);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002239
2240 jwrite32(jme, JME_TXCS, jme->reg_txcs |
2241 TXCS_SELECT_QUEUE0 |
2242 TXCS_QUEUE0S |
2243 TXCS_ENABLE);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002244
Joe Perches49d70c42010-09-04 22:21:05 +00002245 tx_dbg(jme, "xmit: %d+%d@%lu\n",
2246 idx, skb_shinfo(skb)->nr_frags + 2, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002247 jme_stop_queue_if_full(jme);
2248
2249 return NETDEV_TX_OK;
2250}
2251
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +00002252static void
2253jme_set_unicastaddr(struct net_device *netdev)
2254{
2255 struct jme_adapter *jme = netdev_priv(netdev);
2256 u32 val;
2257
2258 val = (netdev->dev_addr[3] & 0xff) << 24 |
2259 (netdev->dev_addr[2] & 0xff) << 16 |
2260 (netdev->dev_addr[1] & 0xff) << 8 |
2261 (netdev->dev_addr[0] & 0xff);
2262 jwrite32(jme, JME_RXUMA_LO, val);
2263 val = (netdev->dev_addr[5] & 0xff) << 8 |
2264 (netdev->dev_addr[4] & 0xff);
2265 jwrite32(jme, JME_RXUMA_HI, val);
2266}
2267
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002268static int
2269jme_set_macaddr(struct net_device *netdev, void *p)
2270{
2271 struct jme_adapter *jme = netdev_priv(netdev);
2272 struct sockaddr *addr = p;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002273
2274 if (netif_running(netdev))
2275 return -EBUSY;
2276
2277 spin_lock_bh(&jme->macaddr_lock);
2278 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +00002279 jme_set_unicastaddr(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002280 spin_unlock_bh(&jme->macaddr_lock);
2281
2282 return 0;
2283}
2284
2285static void
2286jme_set_multi(struct net_device *netdev)
2287{
2288 struct jme_adapter *jme = netdev_priv(netdev);
2289 u32 mc_hash[2] = {};
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002290
2291 spin_lock_bh(&jme->rxmcs_lock);
2292
2293 jme->reg_rxmcs |= RXMCS_BRDFRAME | RXMCS_UNIFRAME;
2294
2295 if (netdev->flags & IFF_PROMISC) {
2296 jme->reg_rxmcs |= RXMCS_ALLFRAME;
2297 } else if (netdev->flags & IFF_ALLMULTI) {
2298 jme->reg_rxmcs |= RXMCS_ALLMULFRAME;
2299 } else if (netdev->flags & IFF_MULTICAST) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002300 struct netdev_hw_addr *ha;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002301 int bit_nr;
2302
2303 jme->reg_rxmcs |= RXMCS_MULFRAME | RXMCS_MULFILTERED;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002304 netdev_for_each_mc_addr(ha, netdev) {
2305 bit_nr = ether_crc(ETH_ALEN, ha->addr) & 0x3F;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002306 mc_hash[bit_nr >> 5] |= 1 << (bit_nr & 0x1F);
2307 }
2308
2309 jwrite32(jme, JME_RXMCHT_LO, mc_hash[0]);
2310 jwrite32(jme, JME_RXMCHT_HI, mc_hash[1]);
2311 }
2312
2313 wmb();
2314 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2315
2316 spin_unlock_bh(&jme->rxmcs_lock);
2317}
2318
2319static int
2320jme_change_mtu(struct net_device *netdev, int new_mtu)
2321{
2322 struct jme_adapter *jme = netdev_priv(netdev);
2323
2324 if (new_mtu == jme->old_mtu)
2325 return 0;
2326
2327 if (((new_mtu + ETH_HLEN) > MAX_ETHERNET_JUMBO_PACKET_SIZE) ||
2328 ((new_mtu) < IPV6_MIN_MTU))
2329 return -EINVAL;
2330
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002331
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002332 netdev->mtu = new_mtu;
Michał Mirosławd7b57652011-03-31 01:01:35 +00002333 netdev_update_features(netdev);
2334
Guo-Fu Tsengba9adbe2012-02-22 08:58:10 +00002335 jme_restart_rx_engine(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002336 jme_reset_link(jme);
2337
2338 return 0;
2339}
2340
2341static void
2342jme_tx_timeout(struct net_device *netdev)
2343{
2344 struct jme_adapter *jme = netdev_priv(netdev);
2345
2346 jme->phylink = 0;
2347 jme_reset_phy_processor(jme);
2348 if (test_bit(JME_FLAG_SSET, &jme->flags))
2349 jme_set_settings(netdev, &jme->old_ecmd);
2350
2351 /*
2352 * Force to Reset the link again
2353 */
2354 jme_reset_link(jme);
2355}
2356
Guo-Fu Tsengbf5e5362010-03-17 00:09:30 +00002357static inline void jme_pause_rx(struct jme_adapter *jme)
2358{
2359 atomic_dec(&jme->link_changing);
2360
2361 jme_set_rx_pcc(jme, PCC_OFF);
2362 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2363 JME_NAPI_DISABLE(jme);
2364 } else {
2365 tasklet_disable(&jme->rxclean_task);
2366 tasklet_disable(&jme->rxempty_task);
2367 }
2368}
2369
2370static inline void jme_resume_rx(struct jme_adapter *jme)
2371{
2372 struct dynpcc_info *dpi = &(jme->dpi);
2373
2374 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2375 JME_NAPI_ENABLE(jme);
2376 } else {
2377 tasklet_hi_enable(&jme->rxclean_task);
2378 tasklet_hi_enable(&jme->rxempty_task);
2379 }
2380 dpi->cur = PCC_P1;
2381 dpi->attempt = PCC_P1;
2382 dpi->cnt = 0;
2383 jme_set_rx_pcc(jme, PCC_P1);
2384
2385 atomic_inc(&jme->link_changing);
2386}
2387
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002388static void
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002389jme_get_drvinfo(struct net_device *netdev,
2390 struct ethtool_drvinfo *info)
2391{
2392 struct jme_adapter *jme = netdev_priv(netdev);
2393
Rick Jones23020ab2011-11-09 09:58:07 +00002394 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2395 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2396 strlcpy(info->bus_info, pci_name(jme->pdev), sizeof(info->bus_info));
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002397}
2398
2399static int
2400jme_get_regs_len(struct net_device *netdev)
2401{
2402 return JME_REG_LEN;
2403}
2404
2405static void
2406mmapio_memcpy(struct jme_adapter *jme, u32 *p, u32 reg, int len)
2407{
2408 int i;
2409
2410 for (i = 0 ; i < len ; i += 4)
2411 p[i >> 2] = jread32(jme, reg + i);
2412}
2413
2414static void
2415mdio_memcpy(struct jme_adapter *jme, u32 *p, int reg_nr)
2416{
2417 int i;
2418 u16 *p16 = (u16 *)p;
2419
2420 for (i = 0 ; i < reg_nr ; ++i)
2421 p16[i] = jme_mdio_read(jme->dev, jme->mii_if.phy_id, i);
2422}
2423
2424static void
2425jme_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
2426{
2427 struct jme_adapter *jme = netdev_priv(netdev);
2428 u32 *p32 = (u32 *)p;
2429
2430 memset(p, 0xFF, JME_REG_LEN);
2431
2432 regs->version = 1;
2433 mmapio_memcpy(jme, p32, JME_MAC, JME_MAC_LEN);
2434
2435 p32 += 0x100 >> 2;
2436 mmapio_memcpy(jme, p32, JME_PHY, JME_PHY_LEN);
2437
2438 p32 += 0x100 >> 2;
2439 mmapio_memcpy(jme, p32, JME_MISC, JME_MISC_LEN);
2440
2441 p32 += 0x100 >> 2;
2442 mmapio_memcpy(jme, p32, JME_RSS, JME_RSS_LEN);
2443
2444 p32 += 0x100 >> 2;
2445 mdio_memcpy(jme, p32, JME_PHY_REG_NR);
2446}
2447
2448static int
2449jme_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2450{
2451 struct jme_adapter *jme = netdev_priv(netdev);
2452
2453 ecmd->tx_coalesce_usecs = PCC_TX_TO;
2454 ecmd->tx_max_coalesced_frames = PCC_TX_CNT;
2455
2456 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2457 ecmd->use_adaptive_rx_coalesce = false;
2458 ecmd->rx_coalesce_usecs = 0;
2459 ecmd->rx_max_coalesced_frames = 0;
2460 return 0;
2461 }
2462
2463 ecmd->use_adaptive_rx_coalesce = true;
2464
2465 switch (jme->dpi.cur) {
2466 case PCC_P1:
2467 ecmd->rx_coalesce_usecs = PCC_P1_TO;
2468 ecmd->rx_max_coalesced_frames = PCC_P1_CNT;
2469 break;
2470 case PCC_P2:
2471 ecmd->rx_coalesce_usecs = PCC_P2_TO;
2472 ecmd->rx_max_coalesced_frames = PCC_P2_CNT;
2473 break;
2474 case PCC_P3:
2475 ecmd->rx_coalesce_usecs = PCC_P3_TO;
2476 ecmd->rx_max_coalesced_frames = PCC_P3_CNT;
2477 break;
2478 default:
2479 break;
2480 }
2481
2482 return 0;
2483}
2484
2485static int
2486jme_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2487{
2488 struct jme_adapter *jme = netdev_priv(netdev);
2489 struct dynpcc_info *dpi = &(jme->dpi);
2490
2491 if (netif_running(netdev))
2492 return -EBUSY;
2493
Joe Perches8e95a202009-12-03 07:58:21 +00002494 if (ecmd->use_adaptive_rx_coalesce &&
2495 test_bit(JME_FLAG_POLL, &jme->flags)) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002496 clear_bit(JME_FLAG_POLL, &jme->flags);
2497 jme->jme_rx = netif_rx;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002498 dpi->cur = PCC_P1;
2499 dpi->attempt = PCC_P1;
2500 dpi->cnt = 0;
2501 jme_set_rx_pcc(jme, PCC_P1);
2502 jme_interrupt_mode(jme);
Joe Perches8e95a202009-12-03 07:58:21 +00002503 } else if (!(ecmd->use_adaptive_rx_coalesce) &&
2504 !(test_bit(JME_FLAG_POLL, &jme->flags))) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002505 set_bit(JME_FLAG_POLL, &jme->flags);
2506 jme->jme_rx = netif_receive_skb;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002507 jme_interrupt_mode(jme);
2508 }
2509
2510 return 0;
2511}
2512
2513static void
2514jme_get_pauseparam(struct net_device *netdev,
2515 struct ethtool_pauseparam *ecmd)
2516{
2517 struct jme_adapter *jme = netdev_priv(netdev);
2518 u32 val;
2519
2520 ecmd->tx_pause = (jme->reg_txpfc & TXPFC_PF_EN) != 0;
2521 ecmd->rx_pause = (jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0;
2522
2523 spin_lock_bh(&jme->phy_lock);
2524 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2525 spin_unlock_bh(&jme->phy_lock);
2526
2527 ecmd->autoneg =
2528 (val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0;
2529}
2530
2531static int
2532jme_set_pauseparam(struct net_device *netdev,
2533 struct ethtool_pauseparam *ecmd)
2534{
2535 struct jme_adapter *jme = netdev_priv(netdev);
2536 u32 val;
2537
2538 if (((jme->reg_txpfc & TXPFC_PF_EN) != 0) ^
2539 (ecmd->tx_pause != 0)) {
2540
2541 if (ecmd->tx_pause)
2542 jme->reg_txpfc |= TXPFC_PF_EN;
2543 else
2544 jme->reg_txpfc &= ~TXPFC_PF_EN;
2545
2546 jwrite32(jme, JME_TXPFC, jme->reg_txpfc);
2547 }
2548
2549 spin_lock_bh(&jme->rxmcs_lock);
2550 if (((jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0) ^
2551 (ecmd->rx_pause != 0)) {
2552
2553 if (ecmd->rx_pause)
2554 jme->reg_rxmcs |= RXMCS_FLOWCTRL;
2555 else
2556 jme->reg_rxmcs &= ~RXMCS_FLOWCTRL;
2557
2558 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2559 }
2560 spin_unlock_bh(&jme->rxmcs_lock);
2561
2562 spin_lock_bh(&jme->phy_lock);
2563 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2564 if (((val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0) ^
2565 (ecmd->autoneg != 0)) {
2566
2567 if (ecmd->autoneg)
2568 val |= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2569 else
2570 val &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2571
2572 jme_mdio_write(jme->dev, jme->mii_if.phy_id,
2573 MII_ADVERTISE, val);
2574 }
2575 spin_unlock_bh(&jme->phy_lock);
2576
2577 return 0;
2578}
2579
2580static void
2581jme_get_wol(struct net_device *netdev,
2582 struct ethtool_wolinfo *wol)
2583{
2584 struct jme_adapter *jme = netdev_priv(netdev);
2585
2586 wol->supported = WAKE_MAGIC | WAKE_PHY;
2587
2588 wol->wolopts = 0;
2589
2590 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
2591 wol->wolopts |= WAKE_PHY;
2592
2593 if (jme->reg_pmcs & PMCS_MFEN)
2594 wol->wolopts |= WAKE_MAGIC;
2595
2596}
2597
2598static int
2599jme_set_wol(struct net_device *netdev,
2600 struct ethtool_wolinfo *wol)
2601{
2602 struct jme_adapter *jme = netdev_priv(netdev);
2603
2604 if (wol->wolopts & (WAKE_MAGICSECURE |
2605 WAKE_UCAST |
2606 WAKE_MCAST |
2607 WAKE_BCAST |
2608 WAKE_ARP))
2609 return -EOPNOTSUPP;
2610
2611 jme->reg_pmcs = 0;
2612
2613 if (wol->wolopts & WAKE_PHY)
2614 jme->reg_pmcs |= PMCS_LFEN | PMCS_LREN;
2615
2616 if (wol->wolopts & WAKE_MAGIC)
2617 jme->reg_pmcs |= PMCS_MFEN;
2618
2619 jwrite32(jme, JME_PMCS, jme->reg_pmcs);
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00002620 device_set_wakeup_enable(&jme->pdev->dev, !!(jme->reg_pmcs));
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00002621
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002622 return 0;
2623}
2624
2625static int
2626jme_get_settings(struct net_device *netdev,
2627 struct ethtool_cmd *ecmd)
2628{
2629 struct jme_adapter *jme = netdev_priv(netdev);
2630 int rc;
2631
2632 spin_lock_bh(&jme->phy_lock);
2633 rc = mii_ethtool_gset(&(jme->mii_if), ecmd);
2634 spin_unlock_bh(&jme->phy_lock);
2635 return rc;
2636}
2637
2638static int
2639jme_set_settings(struct net_device *netdev,
2640 struct ethtool_cmd *ecmd)
2641{
2642 struct jme_adapter *jme = netdev_priv(netdev);
2643 int rc, fdc = 0;
2644
David Decotigny25db0332011-04-27 18:32:39 +00002645 if (ethtool_cmd_speed(ecmd) == SPEED_1000
2646 && ecmd->autoneg != AUTONEG_ENABLE)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002647 return -EINVAL;
2648
Guo-Fu Tseng3ee94012010-10-18 14:10:41 +00002649 /*
2650 * Check If user changed duplex only while force_media.
2651 * Hardware would not generate link change interrupt.
2652 */
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002653 if (jme->mii_if.force_media &&
2654 ecmd->autoneg != AUTONEG_ENABLE &&
2655 (jme->mii_if.full_duplex != ecmd->duplex))
2656 fdc = 1;
2657
2658 spin_lock_bh(&jme->phy_lock);
2659 rc = mii_ethtool_sset(&(jme->mii_if), ecmd);
2660 spin_unlock_bh(&jme->phy_lock);
2661
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002662 if (!rc) {
Guo-Fu Tseng3ee94012010-10-18 14:10:41 +00002663 if (fdc)
2664 jme_reset_link(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002665 jme->old_ecmd = *ecmd;
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00002666 set_bit(JME_FLAG_SSET, &jme->flags);
2667 }
2668
2669 return rc;
2670}
2671
2672static int
2673jme_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2674{
2675 int rc;
2676 struct jme_adapter *jme = netdev_priv(netdev);
2677 struct mii_ioctl_data *mii_data = if_mii(rq);
2678 unsigned int duplex_chg;
2679
2680 if (cmd == SIOCSMIIREG) {
2681 u16 val = mii_data->val_in;
2682 if (!(val & (BMCR_RESET|BMCR_ANENABLE)) &&
2683 (val & BMCR_SPEED1000))
2684 return -EINVAL;
2685 }
2686
2687 spin_lock_bh(&jme->phy_lock);
2688 rc = generic_mii_ioctl(&jme->mii_if, mii_data, cmd, &duplex_chg);
2689 spin_unlock_bh(&jme->phy_lock);
2690
2691 if (!rc && (cmd == SIOCSMIIREG)) {
2692 if (duplex_chg)
2693 jme_reset_link(jme);
2694 jme_get_settings(netdev, &jme->old_ecmd);
2695 set_bit(JME_FLAG_SSET, &jme->flags);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002696 }
2697
2698 return rc;
2699}
2700
2701static u32
2702jme_get_link(struct net_device *netdev)
2703{
2704 struct jme_adapter *jme = netdev_priv(netdev);
2705 return jread32(jme, JME_PHY_LINK) & PHY_LINK_UP;
2706}
2707
2708static u32
2709jme_get_msglevel(struct net_device *netdev)
2710{
2711 struct jme_adapter *jme = netdev_priv(netdev);
2712 return jme->msg_enable;
2713}
2714
2715static void
2716jme_set_msglevel(struct net_device *netdev, u32 value)
2717{
2718 struct jme_adapter *jme = netdev_priv(netdev);
2719 jme->msg_enable = value;
2720}
2721
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002722static netdev_features_t
2723jme_fix_features(struct net_device *netdev, netdev_features_t features)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002724{
Michał Mirosławd7b57652011-03-31 01:01:35 +00002725 if (netdev->mtu > 1900)
2726 features &= ~(NETIF_F_ALL_TSO | NETIF_F_ALL_CSUM);
2727 return features;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002728}
2729
2730static int
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002731jme_set_features(struct net_device *netdev, netdev_features_t features)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002732{
2733 struct jme_adapter *jme = netdev_priv(netdev);
2734
2735 spin_lock_bh(&jme->rxmcs_lock);
Michał Mirosławd7b57652011-03-31 01:01:35 +00002736 if (features & NETIF_F_RXCSUM)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002737 jme->reg_rxmcs |= RXMCS_CHECKSUM;
2738 else
2739 jme->reg_rxmcs &= ~RXMCS_CHECKSUM;
2740 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2741 spin_unlock_bh(&jme->rxmcs_lock);
2742
2743 return 0;
2744}
2745
Peter Wued36d7b2012-07-17 06:29:34 +00002746#ifdef CONFIG_NET_POLL_CONTROLLER
2747static void jme_netpoll(struct net_device *dev)
2748{
2749 unsigned long flags;
2750
2751 local_irq_save(flags);
2752 jme_intr(dev->irq, dev);
2753 local_irq_restore(flags);
2754}
2755#endif
2756
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002757static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002758jme_nway_reset(struct net_device *netdev)
2759{
2760 struct jme_adapter *jme = netdev_priv(netdev);
2761 jme_restart_an(jme);
2762 return 0;
2763}
2764
2765static u8
2766jme_smb_read(struct jme_adapter *jme, unsigned int addr)
2767{
2768 u32 val;
2769 int to;
2770
2771 val = jread32(jme, JME_SMBCSR);
2772 to = JME_SMB_BUSY_TIMEOUT;
2773 while ((val & SMBCSR_BUSY) && --to) {
2774 msleep(1);
2775 val = jread32(jme, JME_SMBCSR);
2776 }
2777 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002778 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002779 return 0xFF;
2780 }
2781
2782 jwrite32(jme, JME_SMBINTF,
2783 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2784 SMBINTF_HWRWN_READ |
2785 SMBINTF_HWCMD);
2786
2787 val = jread32(jme, JME_SMBINTF);
2788 to = JME_SMB_BUSY_TIMEOUT;
2789 while ((val & SMBINTF_HWCMD) && --to) {
2790 msleep(1);
2791 val = jread32(jme, JME_SMBINTF);
2792 }
2793 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002794 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002795 return 0xFF;
2796 }
2797
2798 return (val & SMBINTF_HWDATR) >> SMBINTF_HWDATR_SHIFT;
2799}
2800
2801static void
2802jme_smb_write(struct jme_adapter *jme, unsigned int addr, u8 data)
2803{
2804 u32 val;
2805 int to;
2806
2807 val = jread32(jme, JME_SMBCSR);
2808 to = JME_SMB_BUSY_TIMEOUT;
2809 while ((val & SMBCSR_BUSY) && --to) {
2810 msleep(1);
2811 val = jread32(jme, JME_SMBCSR);
2812 }
2813 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002814 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002815 return;
2816 }
2817
2818 jwrite32(jme, JME_SMBINTF,
2819 ((data << SMBINTF_HWDATW_SHIFT) & SMBINTF_HWDATW) |
2820 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2821 SMBINTF_HWRWN_WRITE |
2822 SMBINTF_HWCMD);
2823
2824 val = jread32(jme, JME_SMBINTF);
2825 to = JME_SMB_BUSY_TIMEOUT;
2826 while ((val & SMBINTF_HWCMD) && --to) {
2827 msleep(1);
2828 val = jread32(jme, JME_SMBINTF);
2829 }
2830 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002831 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002832 return;
2833 }
2834
2835 mdelay(2);
2836}
2837
2838static int
2839jme_get_eeprom_len(struct net_device *netdev)
2840{
2841 struct jme_adapter *jme = netdev_priv(netdev);
2842 u32 val;
2843 val = jread32(jme, JME_SMBCSR);
2844 return (val & SMBCSR_EEPROMD) ? JME_SMB_LEN : 0;
2845}
2846
2847static int
2848jme_get_eeprom(struct net_device *netdev,
2849 struct ethtool_eeprom *eeprom, u8 *data)
2850{
2851 struct jme_adapter *jme = netdev_priv(netdev);
2852 int i, offset = eeprom->offset, len = eeprom->len;
2853
2854 /*
2855 * ethtool will check the boundary for us
2856 */
2857 eeprom->magic = JME_EEPROM_MAGIC;
2858 for (i = 0 ; i < len ; ++i)
2859 data[i] = jme_smb_read(jme, i + offset);
2860
2861 return 0;
2862}
2863
2864static int
2865jme_set_eeprom(struct net_device *netdev,
2866 struct ethtool_eeprom *eeprom, u8 *data)
2867{
2868 struct jme_adapter *jme = netdev_priv(netdev);
2869 int i, offset = eeprom->offset, len = eeprom->len;
2870
2871 if (eeprom->magic != JME_EEPROM_MAGIC)
2872 return -EINVAL;
2873
2874 /*
2875 * ethtool will check the boundary for us
2876 */
2877 for (i = 0 ; i < len ; ++i)
2878 jme_smb_write(jme, i + offset, data[i]);
2879
2880 return 0;
2881}
2882
2883static const struct ethtool_ops jme_ethtool_ops = {
2884 .get_drvinfo = jme_get_drvinfo,
2885 .get_regs_len = jme_get_regs_len,
2886 .get_regs = jme_get_regs,
2887 .get_coalesce = jme_get_coalesce,
2888 .set_coalesce = jme_set_coalesce,
2889 .get_pauseparam = jme_get_pauseparam,
2890 .set_pauseparam = jme_set_pauseparam,
2891 .get_wol = jme_get_wol,
2892 .set_wol = jme_set_wol,
2893 .get_settings = jme_get_settings,
2894 .set_settings = jme_set_settings,
2895 .get_link = jme_get_link,
2896 .get_msglevel = jme_get_msglevel,
2897 .set_msglevel = jme_set_msglevel,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002898 .nway_reset = jme_nway_reset,
2899 .get_eeprom_len = jme_get_eeprom_len,
2900 .get_eeprom = jme_get_eeprom,
2901 .set_eeprom = jme_set_eeprom,
2902};
2903
2904static int
2905jme_pci_dma64(struct pci_dev *pdev)
2906{
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002907 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
Yang Hongyange9304382009-04-13 14:40:14 -07002908 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
2909 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002910 return 1;
2911
2912 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
Yang Hongyange9304382009-04-13 14:40:14 -07002913 !pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2914 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40)))
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002915 return 1;
2916
Yang Hongyang284901a2009-04-06 19:01:15 -07002917 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
2918 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002919 return 0;
2920
2921 return -1;
2922}
2923
2924static inline void
2925jme_phy_init(struct jme_adapter *jme)
2926{
2927 u16 reg26;
2928
2929 reg26 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 26);
2930 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 26, reg26 | 0x1000);
2931}
2932
2933static inline void
2934jme_check_hw_ver(struct jme_adapter *jme)
2935{
2936 u32 chipmode;
2937
2938 chipmode = jread32(jme, JME_CHIPMODE);
2939
2940 jme->fpgaver = (chipmode & CM_FPGAVER_MASK) >> CM_FPGAVER_SHIFT;
2941 jme->chiprev = (chipmode & CM_CHIPREV_MASK) >> CM_CHIPREV_SHIFT;
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00002942 jme->chip_main_rev = jme->chiprev & 0xF;
2943 jme->chip_sub_rev = (jme->chiprev >> 4) & 0xF;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002944}
2945
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002946static const struct net_device_ops jme_netdev_ops = {
2947 .ndo_open = jme_open,
2948 .ndo_stop = jme_close,
2949 .ndo_validate_addr = eth_validate_addr,
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00002950 .ndo_do_ioctl = jme_ioctl,
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002951 .ndo_start_xmit = jme_start_xmit,
2952 .ndo_set_mac_address = jme_set_macaddr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002953 .ndo_set_rx_mode = jme_set_multi,
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002954 .ndo_change_mtu = jme_change_mtu,
2955 .ndo_tx_timeout = jme_tx_timeout,
Michał Mirosławd7b57652011-03-31 01:01:35 +00002956 .ndo_fix_features = jme_fix_features,
2957 .ndo_set_features = jme_set_features,
Peter Wued36d7b2012-07-17 06:29:34 +00002958#ifdef CONFIG_NET_POLL_CONTROLLER
2959 .ndo_poll_controller = jme_netpoll,
2960#endif
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002961};
2962
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002963static int __devinit
2964jme_init_one(struct pci_dev *pdev,
2965 const struct pci_device_id *ent)
2966{
2967 int rc = 0, using_dac, i;
2968 struct net_device *netdev;
2969 struct jme_adapter *jme;
2970 u16 bmcr, bmsr;
2971 u32 apmc;
2972
2973 /*
2974 * set up PCI device basics
2975 */
2976 rc = pci_enable_device(pdev);
2977 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00002978 pr_err("Cannot enable PCI device\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002979 goto err_out;
2980 }
2981
2982 using_dac = jme_pci_dma64(pdev);
2983 if (using_dac < 0) {
Joe Perches49d70c42010-09-04 22:21:05 +00002984 pr_err("Cannot set PCI DMA Mask\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002985 rc = -EIO;
2986 goto err_out_disable_pdev;
2987 }
2988
2989 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
Joe Perches49d70c42010-09-04 22:21:05 +00002990 pr_err("No PCI resource region found\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002991 rc = -ENOMEM;
2992 goto err_out_disable_pdev;
2993 }
2994
2995 rc = pci_request_regions(pdev, DRV_NAME);
2996 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00002997 pr_err("Cannot obtain PCI resource region\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002998 goto err_out_disable_pdev;
2999 }
3000
3001 pci_set_master(pdev);
3002
3003 /*
3004 * alloc and init net device
3005 */
3006 netdev = alloc_etherdev(sizeof(*jme));
3007 if (!netdev) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003008 rc = -ENOMEM;
3009 goto err_out_release_regions;
3010 }
Stephen Hemmingere48714b2008-11-21 17:28:33 -08003011 netdev->netdev_ops = &jme_netdev_ops;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003012 netdev->ethtool_ops = &jme_ethtool_ops;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003013 netdev->watchdog_timeo = TX_TIMEOUT;
Michał Mirosławd7b57652011-03-31 01:01:35 +00003014 netdev->hw_features = NETIF_F_IP_CSUM |
3015 NETIF_F_IPV6_CSUM |
3016 NETIF_F_SG |
3017 NETIF_F_TSO |
3018 NETIF_F_TSO6 |
3019 NETIF_F_RXCSUM;
Michał Mirosław79032642010-11-30 06:38:00 +00003020 netdev->features = NETIF_F_IP_CSUM |
3021 NETIF_F_IPV6_CSUM |
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003022 NETIF_F_SG |
3023 NETIF_F_TSO |
3024 NETIF_F_TSO6 |
3025 NETIF_F_HW_VLAN_TX |
3026 NETIF_F_HW_VLAN_RX;
3027 if (using_dac)
3028 netdev->features |= NETIF_F_HIGHDMA;
3029
3030 SET_NETDEV_DEV(netdev, &pdev->dev);
3031 pci_set_drvdata(pdev, netdev);
3032
3033 /*
3034 * init adapter info
3035 */
3036 jme = netdev_priv(netdev);
3037 jme->pdev = pdev;
3038 jme->dev = netdev;
3039 jme->jme_rx = netif_rx;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003040 jme->old_mtu = netdev->mtu = 1500;
3041 jme->phylink = 0;
3042 jme->tx_ring_size = 1 << 10;
3043 jme->tx_ring_mask = jme->tx_ring_size - 1;
3044 jme->tx_wake_threshold = 1 << 9;
3045 jme->rx_ring_size = 1 << 9;
3046 jme->rx_ring_mask = jme->rx_ring_size - 1;
3047 jme->msg_enable = JME_DEF_MSG_ENABLE;
3048 jme->regs = ioremap(pci_resource_start(pdev, 0),
3049 pci_resource_len(pdev, 0));
3050 if (!(jme->regs)) {
Joe Perches49d70c42010-09-04 22:21:05 +00003051 pr_err("Mapping PCI resource region error\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003052 rc = -ENOMEM;
3053 goto err_out_free_netdev;
3054 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003055
3056 if (no_pseudohp) {
3057 apmc = jread32(jme, JME_APMC) & ~JME_APMC_PSEUDO_HP_EN;
3058 jwrite32(jme, JME_APMC, apmc);
3059 } else if (force_pseudohp) {
3060 apmc = jread32(jme, JME_APMC) | JME_APMC_PSEUDO_HP_EN;
3061 jwrite32(jme, JME_APMC, apmc);
3062 }
3063
3064 NETIF_NAPI_SET(netdev, &jme->napi, jme_poll, jme->rx_ring_size >> 2)
3065
3066 spin_lock_init(&jme->phy_lock);
3067 spin_lock_init(&jme->macaddr_lock);
3068 spin_lock_init(&jme->rxmcs_lock);
3069
3070 atomic_set(&jme->link_changing, 1);
3071 atomic_set(&jme->rx_cleaning, 1);
3072 atomic_set(&jme->tx_cleaning, 1);
3073 atomic_set(&jme->rx_empty, 1);
3074
3075 tasklet_init(&jme->pcc_task,
Joe Perches164165d2009-11-19 09:30:10 +00003076 jme_pcc_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003077 (unsigned long) jme);
3078 tasklet_init(&jme->linkch_task,
Joe Perches164165d2009-11-19 09:30:10 +00003079 jme_link_change_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003080 (unsigned long) jme);
3081 tasklet_init(&jme->txclean_task,
Joe Perches164165d2009-11-19 09:30:10 +00003082 jme_tx_clean_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003083 (unsigned long) jme);
3084 tasklet_init(&jme->rxclean_task,
Joe Perches164165d2009-11-19 09:30:10 +00003085 jme_rx_clean_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003086 (unsigned long) jme);
3087 tasklet_init(&jme->rxempty_task,
Joe Perches164165d2009-11-19 09:30:10 +00003088 jme_rx_empty_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003089 (unsigned long) jme);
Guo-Fu Tseng38ed0c22009-07-06 04:37:52 +00003090 tasklet_disable_nosync(&jme->linkch_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003091 tasklet_disable_nosync(&jme->txclean_task);
3092 tasklet_disable_nosync(&jme->rxclean_task);
3093 tasklet_disable_nosync(&jme->rxempty_task);
3094 jme->dpi.cur = PCC_P1;
3095
3096 jme->reg_ghc = 0;
3097 jme->reg_rxcs = RXCS_DEFAULT;
3098 jme->reg_rxmcs = RXMCS_DEFAULT;
3099 jme->reg_txpfc = 0;
3100 jme->reg_pmcs = PMCS_MFEN;
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +00003101 jme->reg_gpreg1 = GPREG1_DEFAULT;
Michał Mirosławd7b57652011-03-31 01:01:35 +00003102
3103 if (jme->reg_rxmcs & RXMCS_CHECKSUM)
3104 netdev->features |= NETIF_F_RXCSUM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003105
3106 /*
3107 * Get Max Read Req Size from PCI Config Space
3108 */
3109 pci_read_config_byte(pdev, PCI_DCSR_MRRS, &jme->mrrs);
3110 jme->mrrs &= PCI_DCSR_MRRS_MASK;
3111 switch (jme->mrrs) {
3112 case MRRS_128B:
3113 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_128B;
3114 break;
3115 case MRRS_256B:
3116 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_256B;
3117 break;
3118 default:
3119 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_512B;
3120 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003121 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003122
3123 /*
3124 * Must check before reset_mac_processor
3125 */
3126 jme_check_hw_ver(jme);
3127 jme->mii_if.dev = netdev;
3128 if (jme->fpgaver) {
3129 jme->mii_if.phy_id = 0;
3130 for (i = 1 ; i < 32 ; ++i) {
3131 bmcr = jme_mdio_read(netdev, i, MII_BMCR);
3132 bmsr = jme_mdio_read(netdev, i, MII_BMSR);
3133 if (bmcr != 0xFFFFU && (bmcr != 0 || bmsr != 0)) {
3134 jme->mii_if.phy_id = i;
3135 break;
3136 }
3137 }
3138
3139 if (!jme->mii_if.phy_id) {
3140 rc = -EIO;
Joe Perches49d70c42010-09-04 22:21:05 +00003141 pr_err("Can not find phy_id\n");
3142 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003143 }
3144
3145 jme->reg_ghc |= GHC_LINK_POLL;
3146 } else {
3147 jme->mii_if.phy_id = 1;
3148 }
3149 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
3150 jme->mii_if.supports_gmii = true;
3151 else
3152 jme->mii_if.supports_gmii = false;
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00003153 jme->mii_if.phy_id_mask = 0x1F;
3154 jme->mii_if.reg_num_mask = 0x1F;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003155 jme->mii_if.mdio_read = jme_mdio_read;
3156 jme->mii_if.mdio_write = jme_mdio_write;
3157
3158 jme_clear_pm(jme);
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003159 pci_set_power_state(jme->pdev, PCI_D0);
3160 device_set_wakeup_enable(&pdev->dev, true);
3161
Guo-Fu Tseng51754572011-02-13 18:27:37 +00003162 jme_set_phyfifo_5level(jme);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08003163 jme->pcirev = pdev->revision;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003164 if (!jme->fpgaver)
3165 jme_phy_init(jme);
3166 jme_phy_off(jme);
3167
3168 /*
3169 * Reset MAC processor and reload EEPROM for MAC Address
3170 */
3171 jme_reset_mac_processor(jme);
3172 rc = jme_reload_eeprom(jme);
3173 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00003174 pr_err("Reload eeprom for reading MAC Address error\n");
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00003175 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003176 }
3177 jme_load_macaddr(netdev);
3178
3179 /*
3180 * Tell stack that we are not ready to work until open()
3181 */
3182 netif_carrier_off(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003183
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003184 rc = register_netdev(netdev);
3185 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00003186 pr_err("Cannot register net device\n");
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00003187 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003188 }
3189
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00003190 netif_info(jme, probe, jme->dev, "%s%s chiprev:%x pcirev:%x macaddr:%pM\n",
Joe Perchesf8502ce2010-02-09 11:49:51 +00003191 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250) ?
3192 "JMC250 Gigabit Ethernet" :
3193 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC260) ?
3194 "JMC260 Fast Ethernet" : "Unknown",
3195 (jme->fpgaver != 0) ? " (FPGA)" : "",
3196 (jme->fpgaver != 0) ? jme->fpgaver : jme->chiprev,
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00003197 jme->pcirev, netdev->dev_addr);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003198
3199 return 0;
3200
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003201err_out_unmap:
3202 iounmap(jme->regs);
3203err_out_free_netdev:
3204 pci_set_drvdata(pdev, NULL);
3205 free_netdev(netdev);
3206err_out_release_regions:
3207 pci_release_regions(pdev);
3208err_out_disable_pdev:
3209 pci_disable_device(pdev);
3210err_out:
3211 return rc;
3212}
3213
3214static void __devexit
3215jme_remove_one(struct pci_dev *pdev)
3216{
3217 struct net_device *netdev = pci_get_drvdata(pdev);
3218 struct jme_adapter *jme = netdev_priv(netdev);
3219
3220 unregister_netdev(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003221 iounmap(jme->regs);
3222 pci_set_drvdata(pdev, NULL);
3223 free_netdev(netdev);
3224 pci_release_regions(pdev);
3225 pci_disable_device(pdev);
3226
3227}
3228
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003229static void
3230jme_shutdown(struct pci_dev *pdev)
3231{
3232 struct net_device *netdev = pci_get_drvdata(pdev);
3233 struct jme_adapter *jme = netdev_priv(netdev);
3234
3235 jme_powersave_phy(jme);
3236 pci_pme_active(pdev, true);
3237}
3238
Guo-Fu Tsengaab6fb82011-06-27 19:23:34 +00003239#ifdef CONFIG_PM_SLEEP
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003240static int
3241jme_suspend(struct device *dev)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003242{
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003243 struct pci_dev *pdev = to_pci_dev(dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003244 struct net_device *netdev = pci_get_drvdata(pdev);
3245 struct jme_adapter *jme = netdev_priv(netdev);
3246
David S. Miller1805b2f2011-10-24 18:18:09 -04003247 if (!netif_running(netdev))
3248 return 0;
3249
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003250 atomic_dec(&jme->link_changing);
3251
3252 netif_device_detach(netdev);
3253 netif_stop_queue(netdev);
3254 jme_stop_irq(jme);
3255
3256 tasklet_disable(&jme->txclean_task);
3257 tasklet_disable(&jme->rxclean_task);
3258 tasklet_disable(&jme->rxempty_task);
3259
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003260 if (netif_carrier_ok(netdev)) {
3261 if (test_bit(JME_FLAG_POLL, &jme->flags))
3262 jme_polling_mode(jme);
3263
3264 jme_stop_pcc_timer(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003265 jme_disable_rx_engine(jme);
3266 jme_disable_tx_engine(jme);
3267 jme_reset_mac_processor(jme);
3268 jme_free_rx_resources(jme);
3269 jme_free_tx_resources(jme);
3270 netif_carrier_off(netdev);
3271 jme->phylink = 0;
3272 }
3273
3274 tasklet_enable(&jme->txclean_task);
3275 tasklet_hi_enable(&jme->rxclean_task);
3276 tasklet_hi_enable(&jme->rxempty_task);
3277
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003278 jme_powersave_phy(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003279
3280 return 0;
3281}
3282
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003283static int
3284jme_resume(struct device *dev)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003285{
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003286 struct pci_dev *pdev = to_pci_dev(dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003287 struct net_device *netdev = pci_get_drvdata(pdev);
3288 struct jme_adapter *jme = netdev_priv(netdev);
3289
David S. Miller1805b2f2011-10-24 18:18:09 -04003290 if (!netif_running(netdev))
3291 return 0;
3292
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003293 jme_clear_pm(jme);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00003294 jme_phy_on(jme);
3295 if (test_bit(JME_FLAG_SSET, &jme->flags))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003296 jme_set_settings(netdev, &jme->old_ecmd);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00003297 else
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003298 jme_reset_phy_processor(jme);
Aries Leec4860ba2011-11-21 10:20:42 +00003299 jme_phy_calibration(jme);
3300 jme_phy_setEA(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003301 jme_start_irq(jme);
3302 netif_device_attach(netdev);
3303
3304 atomic_inc(&jme->link_changing);
3305
3306 jme_reset_link(jme);
3307
3308 return 0;
3309}
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003310
3311static SIMPLE_DEV_PM_OPS(jme_pm_ops, jme_suspend, jme_resume);
3312#define JME_PM_OPS (&jme_pm_ops)
3313
3314#else
3315
3316#define JME_PM_OPS NULL
David S. Miller724f8802008-10-08 19:54:31 -07003317#endif
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003318
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00003319static DEFINE_PCI_DEVICE_TABLE(jme_pci_tbl) = {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003320 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC250) },
3321 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC260) },
3322 { }
3323};
3324
3325static struct pci_driver jme_driver = {
3326 .name = DRV_NAME,
3327 .id_table = jme_pci_tbl,
3328 .probe = jme_init_one,
3329 .remove = __devexit_p(jme_remove_one),
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003330 .shutdown = jme_shutdown,
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003331 .driver.pm = JME_PM_OPS,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003332};
3333
3334static int __init
3335jme_init_module(void)
3336{
Joe Perches49d70c42010-09-04 22:21:05 +00003337 pr_info("JMicron JMC2XX ethernet driver version %s\n", DRV_VERSION);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003338 return pci_register_driver(&jme_driver);
3339}
3340
3341static void __exit
3342jme_cleanup_module(void)
3343{
3344 pci_unregister_driver(&jme_driver);
3345}
3346
3347module_init(jme_init_module);
3348module_exit(jme_cleanup_module);
3349
3350MODULE_AUTHOR("Guo-Fu Tseng <cooldavid@cooldavid.org>");
3351MODULE_DESCRIPTION("JMicron JMC2x0 PCI Express Ethernet driver");
3352MODULE_LICENSE("GPL");
3353MODULE_VERSION(DRV_VERSION);
3354MODULE_DEVICE_TABLE(pci, jme_pci_tbl);