blob: 9c3ab00643bdfb9b6824dc906d75a36df796c897 [file] [log] [blame]
Thomas Gleixner8fe76f52019-05-28 09:57:22 -07001// SPDX-License-Identifier: GPL-2.0-only
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002/*
3 * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
4 *
5 * Copyright 2008 JMicron Technology Corporation
6 * http://www.jmicron.com/
Guo-Fu Tsenge47dfcd2010-10-18 14:10:44 +00007 * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
Guo-Fu Tseng95252232008-09-16 01:00:11 +08008 *
9 * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
Guo-Fu Tseng95252232008-09-16 01:00:11 +080010 */
11
Joe Perches49d70c42010-09-04 22:21:05 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Guo-Fu Tseng95252232008-09-16 01:00:11 +080014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/pci.h>
Kevin Baradonaac94532012-10-10 10:50:49 +000017#include <linux/pci-aspm.h>
Guo-Fu Tseng95252232008-09-16 01:00:11 +080018#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/ethtool.h>
21#include <linux/mii.h>
22#include <linux/crc32.h>
23#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/ipv6.h>
28#include <linux/tcp.h>
29#include <linux/udp.h>
30#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070032#include <net/ip6_checksum.h>
Guo-Fu Tseng95252232008-09-16 01:00:11 +080033#include "jme.h"
34
35static int force_pseudohp = -1;
36static int no_pseudohp = -1;
37static int no_extplug = -1;
38module_param(force_pseudohp, int, 0);
39MODULE_PARM_DESC(force_pseudohp,
40 "Enable pseudo hot-plug feature manually by driver instead of BIOS.");
41module_param(no_pseudohp, int, 0);
42MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
43module_param(no_extplug, int, 0);
44MODULE_PARM_DESC(no_extplug,
45 "Do not use external plug signal for pseudo hot-plug.");
46
47static int
48jme_mdio_read(struct net_device *netdev, int phy, int reg)
49{
50 struct jme_adapter *jme = netdev_priv(netdev);
51 int i, val, again = (reg == MII_BMSR) ? 1 : 0;
52
53read_again:
54 jwrite32(jme, JME_SMI, SMI_OP_REQ |
55 smi_phy_addr(phy) |
56 smi_reg_addr(reg));
57
58 wmb();
59 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
60 udelay(20);
61 val = jread32(jme, JME_SMI);
62 if ((val & SMI_OP_REQ) == 0)
63 break;
64 }
65
66 if (i == 0) {
Joe Perches49d70c42010-09-04 22:21:05 +000067 pr_err("phy(%d) read timeout : %d\n", phy, reg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +080068 return 0;
69 }
70
71 if (again--)
72 goto read_again;
73
74 return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
75}
76
77static void
78jme_mdio_write(struct net_device *netdev,
79 int phy, int reg, int val)
80{
81 struct jme_adapter *jme = netdev_priv(netdev);
82 int i;
83
84 jwrite32(jme, JME_SMI, SMI_OP_WRITE | SMI_OP_REQ |
85 ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
86 smi_phy_addr(phy) | smi_reg_addr(reg));
87
88 wmb();
89 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
90 udelay(20);
91 if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
92 break;
93 }
94
95 if (i == 0)
Joe Perches49d70c42010-09-04 22:21:05 +000096 pr_err("phy(%d) write timeout : %d\n", phy, reg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +080097}
98
99static inline void
100jme_reset_phy_processor(struct jme_adapter *jme)
101{
102 u32 val;
103
104 jme_mdio_write(jme->dev,
105 jme->mii_if.phy_id,
106 MII_ADVERTISE, ADVERTISE_ALL |
107 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
108
109 if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
110 jme_mdio_write(jme->dev,
111 jme->mii_if.phy_id,
112 MII_CTRL1000,
113 ADVERTISE_1000FULL | ADVERTISE_1000HALF);
114
115 val = jme_mdio_read(jme->dev,
116 jme->mii_if.phy_id,
117 MII_BMCR);
118
119 jme_mdio_write(jme->dev,
120 jme->mii_if.phy_id,
121 MII_BMCR, val | BMCR_RESET);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800122}
123
124static void
125jme_setup_wakeup_frame(struct jme_adapter *jme,
Joe Perchesb6bc7652010-12-21 02:16:08 -0800126 const u32 *mask, u32 crc, int fnr)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800127{
128 int i;
129
130 /*
131 * Setup CRC pattern
132 */
133 jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
134 wmb();
135 jwrite32(jme, JME_WFODP, crc);
136 wmb();
137
138 /*
139 * Setup Mask
140 */
141 for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
142 jwrite32(jme, JME_WFOI,
143 ((i << WFOI_MASK_SHIFT) & WFOI_MASK_SEL) |
144 (fnr & WFOI_FRAME_SEL));
145 wmb();
146 jwrite32(jme, JME_WFODP, mask[i]);
147 wmb();
148 }
149}
150
151static inline void
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000152jme_mac_rxclk_off(struct jme_adapter *jme)
153{
154 jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
155 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
156}
157
158static inline void
159jme_mac_rxclk_on(struct jme_adapter *jme)
160{
161 jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
162 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
163}
164
165static inline void
166jme_mac_txclk_off(struct jme_adapter *jme)
167{
168 jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
169 jwrite32f(jme, JME_GHC, jme->reg_ghc);
170}
171
172static inline void
173jme_mac_txclk_on(struct jme_adapter *jme)
174{
175 u32 speed = jme->reg_ghc & GHC_SPEED;
176 if (speed == GHC_SPEED_1000M)
177 jme->reg_ghc |= GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
178 else
179 jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
180 jwrite32f(jme, JME_GHC, jme->reg_ghc);
181}
182
183static inline void
184jme_reset_ghc_speed(struct jme_adapter *jme)
185{
186 jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
187 jwrite32f(jme, JME_GHC, jme->reg_ghc);
188}
189
190static inline void
191jme_reset_250A2_workaround(struct jme_adapter *jme)
192{
193 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
194 GPREG1_RSSPATCH);
195 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
196}
197
198static inline void
199jme_assert_ghc_reset(struct jme_adapter *jme)
200{
201 jme->reg_ghc |= GHC_SWRST;
202 jwrite32f(jme, JME_GHC, jme->reg_ghc);
203}
204
205static inline void
206jme_clear_ghc_reset(struct jme_adapter *jme)
207{
208 jme->reg_ghc &= ~GHC_SWRST;
209 jwrite32f(jme, JME_GHC, jme->reg_ghc);
210}
211
Denys Vlasenkoea019642016-04-08 20:39:47 +0200212static void
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800213jme_reset_mac_processor(struct jme_adapter *jme)
214{
Joe Perchesb6bc7652010-12-21 02:16:08 -0800215 static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800216 u32 crc = 0xCDCDCDCD;
217 u32 gpreg0;
218 int i;
219
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000220 jme_reset_ghc_speed(jme);
221 jme_reset_250A2_workaround(jme);
222
223 jme_mac_rxclk_on(jme);
224 jme_mac_txclk_on(jme);
225 udelay(1);
226 jme_assert_ghc_reset(jme);
227 udelay(1);
228 jme_mac_rxclk_off(jme);
229 jme_mac_txclk_off(jme);
230 udelay(1);
231 jme_clear_ghc_reset(jme);
232 udelay(1);
233 jme_mac_rxclk_on(jme);
234 jme_mac_txclk_on(jme);
235 udelay(1);
236 jme_mac_rxclk_off(jme);
237 jme_mac_txclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800238
239 jwrite32(jme, JME_RXDBA_LO, 0x00000000);
240 jwrite32(jme, JME_RXDBA_HI, 0x00000000);
241 jwrite32(jme, JME_RXQDC, 0x00000000);
242 jwrite32(jme, JME_RXNDA, 0x00000000);
243 jwrite32(jme, JME_TXDBA_LO, 0x00000000);
244 jwrite32(jme, JME_TXDBA_HI, 0x00000000);
245 jwrite32(jme, JME_TXQDC, 0x00000000);
246 jwrite32(jme, JME_TXNDA, 0x00000000);
247
248 jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
249 jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
250 for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
251 jme_setup_wakeup_frame(jme, mask, crc, i);
252 if (jme->fpgaver)
253 gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
254 else
255 gpreg0 = GPREG0_DEFAULT;
256 jwrite32(jme, JME_GPREG0, gpreg0);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800257}
258
259static inline void
Guo-Fu Tseng0772a992016-03-05 08:11:55 +0800260jme_clear_pm_enable_wol(struct jme_adapter *jme)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800261{
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +0000262 jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800263}
264
Guo-Fu Tseng0772a992016-03-05 08:11:55 +0800265static inline void
266jme_clear_pm_disable_wol(struct jme_adapter *jme)
267{
268 jwrite32(jme, JME_PMCS, PMCS_STMASK);
269}
270
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800271static int
272jme_reload_eeprom(struct jme_adapter *jme)
273{
274 u32 val;
275 int i;
276
277 val = jread32(jme, JME_SMBCSR);
278
279 if (val & SMBCSR_EEPROMD) {
280 val |= SMBCSR_CNACK;
281 jwrite32(jme, JME_SMBCSR, val);
282 val |= SMBCSR_RELOAD;
283 jwrite32(jme, JME_SMBCSR, val);
284 mdelay(12);
285
286 for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
287 mdelay(1);
288 if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
289 break;
290 }
291
292 if (i == 0) {
Joe Perches49d70c42010-09-04 22:21:05 +0000293 pr_err("eeprom reload timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800294 return -EIO;
295 }
296 }
297
298 return 0;
299}
300
301static void
302jme_load_macaddr(struct net_device *netdev)
303{
304 struct jme_adapter *jme = netdev_priv(netdev);
Joe Perchesd458cdf2013-10-01 19:04:40 -0700305 unsigned char macaddr[ETH_ALEN];
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800306 u32 val;
307
308 spin_lock_bh(&jme->macaddr_lock);
309 val = jread32(jme, JME_RXUMA_LO);
310 macaddr[0] = (val >> 0) & 0xFF;
311 macaddr[1] = (val >> 8) & 0xFF;
312 macaddr[2] = (val >> 16) & 0xFF;
313 macaddr[3] = (val >> 24) & 0xFF;
314 val = jread32(jme, JME_RXUMA_HI);
315 macaddr[4] = (val >> 0) & 0xFF;
316 macaddr[5] = (val >> 8) & 0xFF;
Joe Perchesd458cdf2013-10-01 19:04:40 -0700317 memcpy(netdev->dev_addr, macaddr, ETH_ALEN);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800318 spin_unlock_bh(&jme->macaddr_lock);
319}
320
321static inline void
322jme_set_rx_pcc(struct jme_adapter *jme, int p)
323{
324 switch (p) {
325 case PCC_OFF:
326 jwrite32(jme, JME_PCCRX0,
327 ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
328 ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
329 break;
330 case PCC_P1:
331 jwrite32(jme, JME_PCCRX0,
332 ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
333 ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
334 break;
335 case PCC_P2:
336 jwrite32(jme, JME_PCCRX0,
337 ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
338 ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
339 break;
340 case PCC_P3:
341 jwrite32(jme, JME_PCCRX0,
342 ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
343 ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
344 break;
345 default:
346 break;
347 }
348 wmb();
349
350 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
Joe Perchesf8502ce2010-02-09 11:49:51 +0000351 netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800352}
353
354static void
355jme_start_irq(struct jme_adapter *jme)
356{
357 register struct dynpcc_info *dpi = &(jme->dpi);
358
359 jme_set_rx_pcc(jme, PCC_P1);
360 dpi->cur = PCC_P1;
361 dpi->attempt = PCC_P1;
362 dpi->cnt = 0;
363
364 jwrite32(jme, JME_PCCTX,
365 ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
366 ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
367 PCCTXQ0_EN
368 );
369
370 /*
371 * Enable Interrupts
372 */
373 jwrite32(jme, JME_IENS, INTR_ENABLE);
374}
375
376static inline void
377jme_stop_irq(struct jme_adapter *jme)
378{
379 /*
380 * Disable Interrupts
381 */
382 jwrite32f(jme, JME_IENC, INTR_ENABLE);
383}
384
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800385static u32
386jme_linkstat_from_phy(struct jme_adapter *jme)
387{
388 u32 phylink, bmsr;
389
390 phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
391 bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
392 if (bmsr & BMSR_ANCOMP)
393 phylink |= PHY_LINK_AUTONEG_COMPLETE;
394
395 return phylink;
396}
397
398static inline void
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000399jme_set_phyfifo_5level(struct jme_adapter *jme)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800400{
401 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
402}
403
404static inline void
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000405jme_set_phyfifo_8level(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, 0x0000);
408}
409
410static int
411jme_check_link(struct net_device *netdev, int testonly)
412{
413 struct jme_adapter *jme = netdev_priv(netdev);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000414 u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800415 char linkmsg[64];
416 int rc = 0;
417
418 linkmsg[0] = '\0';
419
420 if (jme->fpgaver)
421 phylink = jme_linkstat_from_phy(jme);
422 else
423 phylink = jread32(jme, JME_PHY_LINK);
424
425 if (phylink & PHY_LINK_UP) {
426 if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
427 /*
428 * If we did not enable AN
429 * Speed/Duplex Info should be obtained from SMI
430 */
431 phylink = PHY_LINK_UP;
432
433 bmcr = jme_mdio_read(jme->dev,
434 jme->mii_if.phy_id,
435 MII_BMCR);
436
437 phylink |= ((bmcr & BMCR_SPEED1000) &&
438 (bmcr & BMCR_SPEED100) == 0) ?
439 PHY_LINK_SPEED_1000M :
440 (bmcr & BMCR_SPEED100) ?
441 PHY_LINK_SPEED_100M :
442 PHY_LINK_SPEED_10M;
443
444 phylink |= (bmcr & BMCR_FULLDPLX) ?
445 PHY_LINK_DUPLEX : 0;
446
447 strcat(linkmsg, "Forced: ");
448 } else {
449 /*
450 * Keep polling for speed/duplex resolve complete
451 */
452 while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
453 --cnt) {
454
455 udelay(1);
456
457 if (jme->fpgaver)
458 phylink = jme_linkstat_from_phy(jme);
459 else
460 phylink = jread32(jme, JME_PHY_LINK);
461 }
462 if (!cnt)
Joe Perches49d70c42010-09-04 22:21:05 +0000463 pr_err("Waiting speed resolve timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800464
465 strcat(linkmsg, "ANed: ");
466 }
467
468 if (jme->phylink == phylink) {
469 rc = 1;
470 goto out;
471 }
472 if (testonly)
473 goto out;
474
475 jme->phylink = phylink;
476
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000477 /*
478 * The speed/duplex setting of jme->reg_ghc already cleared
479 * by jme_reset_mac_processor()
480 */
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800481 switch (phylink & PHY_LINK_SPEED_MASK) {
482 case PHY_LINK_SPEED_10M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000483 jme->reg_ghc |= GHC_SPEED_10M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800484 strcat(linkmsg, "10 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800485 break;
486 case PHY_LINK_SPEED_100M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000487 jme->reg_ghc |= GHC_SPEED_100M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800488 strcat(linkmsg, "100 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800489 break;
490 case PHY_LINK_SPEED_1000M:
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000491 jme->reg_ghc |= GHC_SPEED_1000M;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800492 strcat(linkmsg, "1000 Mbps, ");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800493 break;
494 default:
495 break;
496 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800497
498 if (phylink & PHY_LINK_DUPLEX) {
499 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
Guo-Fu Tseng3903c022011-02-13 18:27:38 +0000500 jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000501 jme->reg_ghc |= GHC_DPX;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800502 } else {
503 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
504 TXMCS_BACKOFF |
505 TXMCS_CARRIERSENSE |
506 TXMCS_COLLISION);
Guo-Fu Tseng3903c022011-02-13 18:27:38 +0000507 jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800508 }
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700509
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000510 jwrite32(jme, JME_GHC, jme->reg_ghc);
511
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700512 if (is_buggy250(jme->pdev->device, jme->chiprev)) {
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000513 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
514 GPREG1_RSSPATCH);
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700515 if (!(phylink & PHY_LINK_DUPLEX))
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000516 jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700517 switch (phylink & PHY_LINK_SPEED_MASK) {
518 case PHY_LINK_SPEED_10M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000519 jme_set_phyfifo_8level(jme);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000520 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700521 break;
522 case PHY_LINK_SPEED_100M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000523 jme_set_phyfifo_5level(jme);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000524 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700525 break;
526 case PHY_LINK_SPEED_1000M:
Guo-Fu Tseng51754572011-02-13 18:27:37 +0000527 jme_set_phyfifo_8level(jme);
Guo-Fu Tsenga821ebe2008-10-08 19:48:58 -0700528 break;
529 default:
530 break;
531 }
532 }
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000533 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800534
akeemting4f40bf42008-12-03 21:19:16 -0800535 strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
536 "Full-Duplex, " :
537 "Half-Duplex, ");
538 strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
539 "MDI-X" :
540 "MDI");
Joe Perches49d70c42010-09-04 22:21:05 +0000541 netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800542 netif_carrier_on(netdev);
543 } else {
544 if (testonly)
545 goto out;
546
Joe Perches49d70c42010-09-04 22:21:05 +0000547 netif_info(jme, link, jme->dev, "Link is down\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800548 jme->phylink = 0;
549 netif_carrier_off(netdev);
550 }
551
552out:
553 return rc;
554}
555
556static int
557jme_setup_tx_resources(struct jme_adapter *jme)
558{
559 struct jme_ring *txring = &(jme->txring[0]);
560
561 txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
562 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
563 &(txring->dmaalloc),
564 GFP_ATOMIC);
565
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000566 if (!txring->alloc)
567 goto err_set_null;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800568
569 /*
570 * 16 Bytes align
571 */
572 txring->desc = (void *)ALIGN((unsigned long)(txring->alloc),
573 RING_DESC_ALIGN);
574 txring->dma = ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
575 txring->next_to_use = 0;
576 atomic_set(&txring->next_to_clean, 0);
577 atomic_set(&txring->nr_free, jme->tx_ring_size);
578
Kees Cook6396bb22018-06-12 14:03:40 -0700579 txring->bufinf = kcalloc(jme->tx_ring_size,
580 sizeof(struct jme_buffer_info),
581 GFP_ATOMIC);
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000582 if (unlikely(!(txring->bufinf)))
583 goto err_free_txring;
584
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800585 return 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000586
587err_free_txring:
588 dma_free_coherent(&(jme->pdev->dev),
589 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
590 txring->alloc,
591 txring->dmaalloc);
592
593err_set_null:
594 txring->desc = NULL;
595 txring->dmaalloc = 0;
596 txring->dma = 0;
597 txring->bufinf = NULL;
598
599 return -ENOMEM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800600}
601
602static void
603jme_free_tx_resources(struct jme_adapter *jme)
604{
605 int i;
606 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000607 struct jme_buffer_info *txbi;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800608
609 if (txring->alloc) {
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000610 if (txring->bufinf) {
611 for (i = 0 ; i < jme->tx_ring_size ; ++i) {
612 txbi = txring->bufinf + i;
613 if (txbi->skb) {
614 dev_kfree_skb(txbi->skb);
615 txbi->skb = NULL;
616 }
617 txbi->mapping = 0;
618 txbi->len = 0;
619 txbi->nr_desc = 0;
620 txbi->start_xmit = 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800621 }
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000622 kfree(txring->bufinf);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800623 }
624
625 dma_free_coherent(&(jme->pdev->dev),
626 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
627 txring->alloc,
628 txring->dmaalloc);
629
630 txring->alloc = NULL;
631 txring->desc = NULL;
632 txring->dmaalloc = 0;
633 txring->dma = 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000634 txring->bufinf = NULL;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800635 }
636 txring->next_to_use = 0;
637 atomic_set(&txring->next_to_clean, 0);
638 atomic_set(&txring->nr_free, 0);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800639}
640
641static inline void
642jme_enable_tx_engine(struct jme_adapter *jme)
643{
644 /*
645 * Select Queue 0
646 */
647 jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
648 wmb();
649
650 /*
651 * Setup TX Queue 0 DMA Bass Address
652 */
653 jwrite32(jme, JME_TXDBA_LO, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
654 jwrite32(jme, JME_TXDBA_HI, (__u64)(jme->txring[0].dma) >> 32);
655 jwrite32(jme, JME_TXNDA, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
656
657 /*
658 * Setup TX Descptor Count
659 */
660 jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
661
662 /*
663 * Enable TX Engine
664 */
665 wmb();
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000666 jwrite32f(jme, JME_TXCS, jme->reg_txcs |
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800667 TXCS_SELECT_QUEUE0 |
668 TXCS_ENABLE);
669
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000670 /*
671 * Start clock for TX MAC Processor
672 */
673 jme_mac_txclk_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800674}
675
676static inline void
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800677jme_disable_tx_engine(struct jme_adapter *jme)
678{
679 int i;
680 u32 val;
681
682 /*
683 * Disable TX Engine
684 */
685 jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
686 wmb();
687
688 val = jread32(jme, JME_TXCS);
689 for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
690 mdelay(1);
691 val = jread32(jme, JME_TXCS);
692 rmb();
693 }
694
695 if (!i)
Joe Perches49d70c42010-09-04 22:21:05 +0000696 pr_err("Disable TX engine timeout\n");
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000697
698 /*
699 * Stop clock for TX MAC Processor
700 */
701 jme_mac_txclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800702}
703
704static void
705jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
706{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000707 struct jme_ring *rxring = &(jme->rxring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800708 register struct rxdesc *rxdesc = rxring->desc;
709 struct jme_buffer_info *rxbi = rxring->bufinf;
710 rxdesc += i;
711 rxbi += i;
712
713 rxdesc->dw[0] = 0;
714 rxdesc->dw[1] = 0;
715 rxdesc->desc1.bufaddrh = cpu_to_le32((__u64)rxbi->mapping >> 32);
716 rxdesc->desc1.bufaddrl = cpu_to_le32(
717 (__u64)rxbi->mapping & 0xFFFFFFFFUL);
718 rxdesc->desc1.datalen = cpu_to_le16(rxbi->len);
719 if (jme->dev->features & NETIF_F_HIGHDMA)
720 rxdesc->desc1.flags = RXFLAG_64BIT;
721 wmb();
722 rxdesc->desc1.flags |= RXFLAG_OWN | RXFLAG_INT;
723}
724
725static int
726jme_make_new_rx_buf(struct jme_adapter *jme, int i)
727{
728 struct jme_ring *rxring = &(jme->rxring[0]);
729 struct jme_buffer_info *rxbi = rxring->bufinf + i;
730 struct sk_buff *skb;
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000731 dma_addr_t mapping;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800732
733 skb = netdev_alloc_skb(jme->dev,
734 jme->dev->mtu + RX_EXTRA_LEN);
735 if (unlikely(!skb))
736 return -ENOMEM;
737
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000738 mapping = pci_map_page(jme->pdev, virt_to_page(skb->data),
739 offset_in_page(skb->data), skb_tailroom(skb),
740 PCI_DMA_FROMDEVICE);
741 if (unlikely(pci_dma_mapping_error(jme->pdev, mapping))) {
742 dev_kfree_skb(skb);
743 return -ENOMEM;
744 }
745
746 if (likely(rxbi->mapping))
747 pci_unmap_page(jme->pdev, rxbi->mapping,
748 rxbi->len, PCI_DMA_FROMDEVICE);
749
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800750 rxbi->skb = skb;
751 rxbi->len = skb_tailroom(skb);
Guo-Fu Tseng94c5b412011-07-20 16:57:36 +0000752 rxbi->mapping = mapping;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800753 return 0;
754}
755
756static void
757jme_free_rx_buf(struct jme_adapter *jme, int i)
758{
759 struct jme_ring *rxring = &(jme->rxring[0]);
760 struct jme_buffer_info *rxbi = rxring->bufinf;
761 rxbi += i;
762
763 if (rxbi->skb) {
764 pci_unmap_page(jme->pdev,
765 rxbi->mapping,
766 rxbi->len,
767 PCI_DMA_FROMDEVICE);
768 dev_kfree_skb(rxbi->skb);
769 rxbi->skb = NULL;
770 rxbi->mapping = 0;
771 rxbi->len = 0;
772 }
773}
774
775static void
776jme_free_rx_resources(struct jme_adapter *jme)
777{
778 int i;
779 struct jme_ring *rxring = &(jme->rxring[0]);
780
781 if (rxring->alloc) {
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000782 if (rxring->bufinf) {
783 for (i = 0 ; i < jme->rx_ring_size ; ++i)
784 jme_free_rx_buf(jme, i);
785 kfree(rxring->bufinf);
786 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800787
788 dma_free_coherent(&(jme->pdev->dev),
789 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
790 rxring->alloc,
791 rxring->dmaalloc);
792 rxring->alloc = NULL;
793 rxring->desc = NULL;
794 rxring->dmaalloc = 0;
795 rxring->dma = 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000796 rxring->bufinf = NULL;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800797 }
798 rxring->next_to_use = 0;
799 atomic_set(&rxring->next_to_clean, 0);
800}
801
802static int
803jme_setup_rx_resources(struct jme_adapter *jme)
804{
805 int i;
806 struct jme_ring *rxring = &(jme->rxring[0]);
807
808 rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
809 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
810 &(rxring->dmaalloc),
811 GFP_ATOMIC);
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000812 if (!rxring->alloc)
813 goto err_set_null;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800814
815 /*
816 * 16 Bytes align
817 */
818 rxring->desc = (void *)ALIGN((unsigned long)(rxring->alloc),
819 RING_DESC_ALIGN);
820 rxring->dma = ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
821 rxring->next_to_use = 0;
822 atomic_set(&rxring->next_to_clean, 0);
823
Kees Cook6396bb22018-06-12 14:03:40 -0700824 rxring->bufinf = kcalloc(jme->rx_ring_size,
825 sizeof(struct jme_buffer_info),
826 GFP_ATOMIC);
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000827 if (unlikely(!(rxring->bufinf)))
828 goto err_free_rxring;
829
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800830 /*
831 * Initiallize Receive Descriptors
832 */
833 for (i = 0 ; i < jme->rx_ring_size ; ++i) {
834 if (unlikely(jme_make_new_rx_buf(jme, i))) {
835 jme_free_rx_resources(jme);
836 return -ENOMEM;
837 }
838
839 jme_set_clean_rxdesc(jme, i);
840 }
841
842 return 0;
Guo-Fu Tseng47bd10d2009-07-06 04:39:46 +0000843
844err_free_rxring:
845 dma_free_coherent(&(jme->pdev->dev),
846 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
847 rxring->alloc,
848 rxring->dmaalloc);
849err_set_null:
850 rxring->desc = NULL;
851 rxring->dmaalloc = 0;
852 rxring->dma = 0;
853 rxring->bufinf = NULL;
854
855 return -ENOMEM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800856}
857
858static inline void
859jme_enable_rx_engine(struct jme_adapter *jme)
860{
861 /*
862 * Select Queue 0
863 */
864 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
865 RXCS_QUEUESEL_Q0);
866 wmb();
867
868 /*
869 * Setup RX DMA Bass Address
870 */
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000871 jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800872 jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +0000873 jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800874
875 /*
876 * Setup RX Descriptor Count
877 */
878 jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
879
880 /*
881 * Setup Unicast Filter
882 */
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +0000883 jme_set_unicastaddr(jme->dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800884 jme_set_multi(jme->dev);
885
886 /*
887 * Enable RX Engine
888 */
889 wmb();
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000890 jwrite32f(jme, JME_RXCS, jme->reg_rxcs |
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800891 RXCS_QUEUESEL_Q0 |
892 RXCS_ENABLE |
893 RXCS_QST);
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000894
895 /*
896 * Start clock for RX MAC Processor
897 */
898 jme_mac_rxclk_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800899}
900
901static inline void
902jme_restart_rx_engine(struct jme_adapter *jme)
903{
904 /*
905 * Start RX Engine
906 */
907 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
908 RXCS_QUEUESEL_Q0 |
909 RXCS_ENABLE |
910 RXCS_QST);
911}
912
913static inline void
914jme_disable_rx_engine(struct jme_adapter *jme)
915{
916 int i;
917 u32 val;
918
919 /*
920 * Disable RX Engine
921 */
922 jwrite32(jme, JME_RXCS, jme->reg_rxcs);
923 wmb();
924
925 val = jread32(jme, JME_RXCS);
926 for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
927 mdelay(1);
928 val = jread32(jme, JME_RXCS);
929 rmb();
930 }
931
932 if (!i)
Joe Perches49d70c42010-09-04 22:21:05 +0000933 pr_err("Disable RX engine timeout\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800934
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +0000935 /*
936 * Stop clock for RX MAC Processor
937 */
938 jme_mac_rxclk_off(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800939}
940
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +0000941static u16
942jme_udpsum(struct sk_buff *skb)
943{
944 u16 csum = 0xFFFFu;
945
946 if (skb->len < (ETH_HLEN + sizeof(struct iphdr)))
947 return csum;
948 if (skb->protocol != htons(ETH_P_IP))
949 return csum;
950 skb_set_network_header(skb, ETH_HLEN);
951 if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
952 (skb->len < (ETH_HLEN +
953 (ip_hdr(skb)->ihl << 2) +
954 sizeof(struct udphdr)))) {
955 skb_reset_network_header(skb);
956 return csum;
957 }
958 skb_set_transport_header(skb,
959 ETH_HLEN + (ip_hdr(skb)->ihl << 2));
960 csum = udp_hdr(skb)->check;
961 skb_reset_transport_header(skb);
962 skb_reset_network_header(skb);
963
964 return csum;
965}
966
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800967static int
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +0000968jme_rxsum_ok(struct jme_adapter *jme, u16 flags, struct sk_buff *skb)
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800969{
970 if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
971 return false;
972
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000973 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
974 == RXWBFLAG_TCPON)) {
975 if (flags & RXWBFLAG_IPV4)
Joe Perchesf8502ce2010-02-09 11:49:51 +0000976 netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000977 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800978 }
979
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000980 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +0000981 == RXWBFLAG_UDPON) && jme_udpsum(skb)) {
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000982 if (flags & RXWBFLAG_IPV4)
Joe Perches49d70c42010-09-04 22:21:05 +0000983 netif_err(jme, rx_err, jme->dev, "UDP Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000984 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800985 }
986
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000987 if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
988 == RXWBFLAG_IPV4)) {
Joe Perches49d70c42010-09-04 22:21:05 +0000989 netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error\n");
Guo-Fu Tsengce7d70a2009-07-06 04:41:22 +0000990 return false;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800991 }
992
993 return true;
Guo-Fu Tseng95252232008-09-16 01:00:11 +0800994}
995
996static void
997jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
998{
999 struct jme_ring *rxring = &(jme->rxring[0]);
1000 struct rxdesc *rxdesc = rxring->desc;
1001 struct jme_buffer_info *rxbi = rxring->bufinf;
1002 struct sk_buff *skb;
1003 int framesize;
1004
1005 rxdesc += idx;
1006 rxbi += idx;
1007
1008 skb = rxbi->skb;
1009 pci_dma_sync_single_for_cpu(jme->pdev,
1010 rxbi->mapping,
1011 rxbi->len,
1012 PCI_DMA_FROMDEVICE);
1013
1014 if (unlikely(jme_make_new_rx_buf(jme, idx))) {
1015 pci_dma_sync_single_for_device(jme->pdev,
1016 rxbi->mapping,
1017 rxbi->len,
1018 PCI_DMA_FROMDEVICE);
1019
1020 ++(NET_STAT(jme).rx_dropped);
1021 } else {
1022 framesize = le16_to_cpu(rxdesc->descwb.framesize)
1023 - RX_PREPAD_SIZE;
1024
1025 skb_reserve(skb, RX_PREPAD_SIZE);
1026 skb_put(skb, framesize);
1027 skb->protocol = eth_type_trans(skb, jme->dev);
1028
Guo-Fu Tsengc00cd822011-02-13 19:27:17 +00001029 if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags), skb))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001030 skb->ip_summed = CHECKSUM_UNNECESSARY;
1031 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001032 skb_checksum_none_assert(skb);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001033
Harvey Harrison31c221c2008-11-19 15:50:59 -08001034 if (rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_TAGON)) {
Jiri Pirko5043f50572011-07-20 04:54:21 +00001035 u16 vid = le16_to_cpu(rxdesc->descwb.vlan);
1036
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001037 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Jiri Pirko5043f50572011-07-20 04:54:21 +00001038 NET_STAT(jme).rx_bytes += 4;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001039 }
Jiri Pirko5043f50572011-07-20 04:54:21 +00001040 jme->jme_rx(skb);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001041
Harvey Harrison31c221c2008-11-19 15:50:59 -08001042 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_DEST)) ==
1043 cpu_to_le16(RXWBFLAG_DEST_MUL))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001044 ++(NET_STAT(jme).multicast);
1045
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001046 NET_STAT(jme).rx_bytes += framesize;
1047 ++(NET_STAT(jme).rx_packets);
1048 }
1049
1050 jme_set_clean_rxdesc(jme, idx);
1051
1052}
1053
1054static int
1055jme_process_receive(struct jme_adapter *jme, int limit)
1056{
1057 struct jme_ring *rxring = &(jme->rxring[0]);
Colin Ian Kingf14d2442018-02-01 16:58:42 +00001058 struct rxdesc *rxdesc;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001059 int i, j, ccnt, desccnt, mask = jme->rx_ring_mask;
1060
1061 if (unlikely(!atomic_dec_and_test(&jme->rx_cleaning)))
1062 goto out_inc;
1063
1064 if (unlikely(atomic_read(&jme->link_changing) != 1))
1065 goto out_inc;
1066
1067 if (unlikely(!netif_carrier_ok(jme->dev)))
1068 goto out_inc;
1069
1070 i = atomic_read(&rxring->next_to_clean);
Roel Kluin858b9ce2009-03-04 00:11:42 -08001071 while (limit > 0) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001072 rxdesc = rxring->desc;
1073 rxdesc += i;
1074
Harvey Harrison31c221c2008-11-19 15:50:59 -08001075 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_OWN)) ||
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001076 !(rxdesc->descwb.desccnt & RXWBDCNT_WBCPL))
1077 goto out;
Roel Kluin858b9ce2009-03-04 00:11:42 -08001078 --limit;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001079
Guo-Fu Tsengea192aa2010-10-18 14:10:42 +00001080 rmb();
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001081 desccnt = rxdesc->descwb.desccnt & RXWBDCNT_DCNT;
1082
1083 if (unlikely(desccnt > 1 ||
1084 rxdesc->descwb.errstat & RXWBERR_ALLERR)) {
1085
1086 if (rxdesc->descwb.errstat & RXWBERR_CRCERR)
1087 ++(NET_STAT(jme).rx_crc_errors);
1088 else if (rxdesc->descwb.errstat & RXWBERR_OVERUN)
1089 ++(NET_STAT(jme).rx_fifo_errors);
1090 else
1091 ++(NET_STAT(jme).rx_errors);
1092
1093 if (desccnt > 1)
1094 limit -= desccnt - 1;
1095
1096 for (j = i, ccnt = desccnt ; ccnt-- ; ) {
1097 jme_set_clean_rxdesc(jme, j);
1098 j = (j + 1) & (mask);
1099 }
1100
1101 } else {
1102 jme_alloc_and_feed_skb(jme, i);
1103 }
1104
1105 i = (i + desccnt) & (mask);
1106 }
1107
1108out:
1109 atomic_set(&rxring->next_to_clean, i);
1110
1111out_inc:
1112 atomic_inc(&jme->rx_cleaning);
1113
1114 return limit > 0 ? limit : 0;
1115
1116}
1117
1118static void
1119jme_attempt_pcc(struct dynpcc_info *dpi, int atmp)
1120{
1121 if (likely(atmp == dpi->cur)) {
1122 dpi->cnt = 0;
1123 return;
1124 }
1125
1126 if (dpi->attempt == atmp) {
1127 ++(dpi->cnt);
1128 } else {
1129 dpi->attempt = atmp;
1130 dpi->cnt = 0;
1131 }
1132
1133}
1134
1135static void
1136jme_dynamic_pcc(struct jme_adapter *jme)
1137{
1138 register struct dynpcc_info *dpi = &(jme->dpi);
1139
1140 if ((NET_STAT(jme).rx_bytes - dpi->last_bytes) > PCC_P3_THRESHOLD)
1141 jme_attempt_pcc(dpi, PCC_P3);
Joe Perches8e95a202009-12-03 07:58:21 +00001142 else if ((NET_STAT(jme).rx_packets - dpi->last_pkts) > PCC_P2_THRESHOLD ||
1143 dpi->intr_cnt > PCC_INTR_THRESHOLD)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001144 jme_attempt_pcc(dpi, PCC_P2);
1145 else
1146 jme_attempt_pcc(dpi, PCC_P1);
1147
1148 if (unlikely(dpi->attempt != dpi->cur && dpi->cnt > 5)) {
1149 if (dpi->attempt < dpi->cur)
1150 tasklet_schedule(&jme->rxclean_task);
1151 jme_set_rx_pcc(jme, dpi->attempt);
1152 dpi->cur = dpi->attempt;
1153 dpi->cnt = 0;
1154 }
1155}
1156
1157static void
1158jme_start_pcc_timer(struct jme_adapter *jme)
1159{
1160 struct dynpcc_info *dpi = &(jme->dpi);
1161 dpi->last_bytes = NET_STAT(jme).rx_bytes;
1162 dpi->last_pkts = NET_STAT(jme).rx_packets;
1163 dpi->intr_cnt = 0;
1164 jwrite32(jme, JME_TMCSR,
1165 TMCSR_EN | ((0xFFFFFF - PCC_INTERVAL_US) & TMCSR_CNT));
1166}
1167
1168static inline void
1169jme_stop_pcc_timer(struct jme_adapter *jme)
1170{
1171 jwrite32(jme, JME_TMCSR, 0);
1172}
1173
1174static void
1175jme_shutdown_nic(struct jme_adapter *jme)
1176{
1177 u32 phylink;
1178
1179 phylink = jme_linkstat_from_phy(jme);
1180
1181 if (!(phylink & PHY_LINK_UP)) {
1182 /*
1183 * Disable all interrupt before issue timer
1184 */
1185 jme_stop_irq(jme);
1186 jwrite32(jme, JME_TIMER2, TMCSR_EN | 0xFFFFFE);
1187 }
1188}
1189
1190static void
1191jme_pcc_tasklet(unsigned long arg)
1192{
1193 struct jme_adapter *jme = (struct jme_adapter *)arg;
1194 struct net_device *netdev = jme->dev;
1195
1196 if (unlikely(test_bit(JME_FLAG_SHUTDOWN, &jme->flags))) {
1197 jme_shutdown_nic(jme);
1198 return;
1199 }
1200
1201 if (unlikely(!netif_carrier_ok(netdev) ||
1202 (atomic_read(&jme->link_changing) != 1)
1203 )) {
1204 jme_stop_pcc_timer(jme);
1205 return;
1206 }
1207
1208 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
1209 jme_dynamic_pcc(jme);
1210
1211 jme_start_pcc_timer(jme);
1212}
1213
1214static inline void
1215jme_polling_mode(struct jme_adapter *jme)
1216{
1217 jme_set_rx_pcc(jme, PCC_OFF);
1218}
1219
1220static inline void
1221jme_interrupt_mode(struct jme_adapter *jme)
1222{
1223 jme_set_rx_pcc(jme, PCC_P1);
1224}
1225
1226static inline int
1227jme_pseudo_hotplug_enabled(struct jme_adapter *jme)
1228{
1229 u32 apmc;
1230 apmc = jread32(jme, JME_APMC);
1231 return apmc & JME_APMC_PSEUDO_HP_EN;
1232}
1233
1234static void
1235jme_start_shutdown_timer(struct jme_adapter *jme)
1236{
1237 u32 apmc;
1238
1239 apmc = jread32(jme, JME_APMC) | JME_APMC_PCIE_SD_EN;
1240 apmc &= ~JME_APMC_EPIEN_CTRL;
1241 if (!no_extplug) {
1242 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_EN);
1243 wmb();
1244 }
1245 jwrite32f(jme, JME_APMC, apmc);
1246
1247 jwrite32f(jme, JME_TIMER2, 0);
1248 set_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1249 jwrite32(jme, JME_TMCSR,
1250 TMCSR_EN | ((0xFFFFFF - APMC_PHP_SHUTDOWN_DELAY) & TMCSR_CNT));
1251}
1252
1253static void
1254jme_stop_shutdown_timer(struct jme_adapter *jme)
1255{
1256 u32 apmc;
1257
1258 jwrite32f(jme, JME_TMCSR, 0);
1259 jwrite32f(jme, JME_TIMER2, 0);
1260 clear_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1261
1262 apmc = jread32(jme, JME_APMC);
1263 apmc &= ~(JME_APMC_PCIE_SD_EN | JME_APMC_EPIEN_CTRL);
1264 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_DIS);
1265 wmb();
1266 jwrite32f(jme, JME_APMC, apmc);
1267}
1268
1269static void
1270jme_link_change_tasklet(unsigned long arg)
1271{
1272 struct jme_adapter *jme = (struct jme_adapter *)arg;
1273 struct net_device *netdev = jme->dev;
1274 int rc;
1275
1276 while (!atomic_dec_and_test(&jme->link_changing)) {
1277 atomic_inc(&jme->link_changing);
Joe Perches49d70c42010-09-04 22:21:05 +00001278 netif_info(jme, intr, jme->dev, "Get link change lock failed\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001279 while (atomic_read(&jme->link_changing) != 1)
Joe Perches49d70c42010-09-04 22:21:05 +00001280 netif_info(jme, intr, jme->dev, "Waiting link change lock\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001281 }
1282
1283 if (jme_check_link(netdev, 1) && jme->old_mtu == netdev->mtu)
1284 goto out;
1285
1286 jme->old_mtu = netdev->mtu;
1287 netif_stop_queue(netdev);
1288 if (jme_pseudo_hotplug_enabled(jme))
1289 jme_stop_shutdown_timer(jme);
1290
1291 jme_stop_pcc_timer(jme);
1292 tasklet_disable(&jme->txclean_task);
1293 tasklet_disable(&jme->rxclean_task);
1294 tasklet_disable(&jme->rxempty_task);
1295
1296 if (netif_carrier_ok(netdev)) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001297 jme_disable_rx_engine(jme);
1298 jme_disable_tx_engine(jme);
1299 jme_reset_mac_processor(jme);
1300 jme_free_rx_resources(jme);
1301 jme_free_tx_resources(jme);
1302
1303 if (test_bit(JME_FLAG_POLL, &jme->flags))
1304 jme_polling_mode(jme);
1305
1306 netif_carrier_off(netdev);
1307 }
1308
1309 jme_check_link(netdev, 0);
1310 if (netif_carrier_ok(netdev)) {
1311 rc = jme_setup_rx_resources(jme);
1312 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001313 pr_err("Allocating resources for RX error, Device STOPPED!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001314 goto out_enable_tasklet;
1315 }
1316
1317 rc = jme_setup_tx_resources(jme);
1318 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001319 pr_err("Allocating resources for TX error, Device STOPPED!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001320 goto err_out_free_rx_resources;
1321 }
1322
1323 jme_enable_rx_engine(jme);
1324 jme_enable_tx_engine(jme);
1325
1326 netif_start_queue(netdev);
1327
1328 if (test_bit(JME_FLAG_POLL, &jme->flags))
1329 jme_interrupt_mode(jme);
1330
1331 jme_start_pcc_timer(jme);
1332 } else if (jme_pseudo_hotplug_enabled(jme)) {
1333 jme_start_shutdown_timer(jme);
1334 }
1335
1336 goto out_enable_tasklet;
1337
1338err_out_free_rx_resources:
1339 jme_free_rx_resources(jme);
1340out_enable_tasklet:
1341 tasklet_enable(&jme->txclean_task);
Quentin Lambert06f66522014-12-12 13:35:52 +01001342 tasklet_enable(&jme->rxclean_task);
1343 tasklet_enable(&jme->rxempty_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001344out:
1345 atomic_inc(&jme->link_changing);
1346}
1347
1348static void
1349jme_rx_clean_tasklet(unsigned long arg)
1350{
1351 struct jme_adapter *jme = (struct jme_adapter *)arg;
1352 struct dynpcc_info *dpi = &(jme->dpi);
1353
1354 jme_process_receive(jme, jme->rx_ring_size);
1355 ++(dpi->intr_cnt);
1356
1357}
1358
1359static int
1360jme_poll(JME_NAPI_HOLDER(holder), JME_NAPI_WEIGHT(budget))
1361{
1362 struct jme_adapter *jme = jme_napi_priv(holder);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001363 int rest;
1364
1365 rest = jme_process_receive(jme, JME_NAPI_WEIGHT_VAL(budget));
1366
1367 while (atomic_read(&jme->rx_empty) > 0) {
1368 atomic_dec(&jme->rx_empty);
1369 ++(NET_STAT(jme).rx_dropped);
1370 jme_restart_rx_engine(jme);
1371 }
1372 atomic_inc(&jme->rx_empty);
1373
1374 if (rest) {
1375 JME_RX_COMPLETE(netdev, holder);
1376 jme_interrupt_mode(jme);
1377 }
1378
1379 JME_NAPI_WEIGHT_SET(budget, rest);
1380 return JME_NAPI_WEIGHT_VAL(budget) - rest;
1381}
1382
1383static void
1384jme_rx_empty_tasklet(unsigned long arg)
1385{
1386 struct jme_adapter *jme = (struct jme_adapter *)arg;
1387
1388 if (unlikely(atomic_read(&jme->link_changing) != 1))
1389 return;
1390
1391 if (unlikely(!netif_carrier_ok(jme->dev)))
1392 return;
1393
Joe Perchesf8502ce2010-02-09 11:49:51 +00001394 netif_info(jme, rx_status, jme->dev, "RX Queue Full!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001395
1396 jme_rx_clean_tasklet(arg);
1397
1398 while (atomic_read(&jme->rx_empty) > 0) {
1399 atomic_dec(&jme->rx_empty);
1400 ++(NET_STAT(jme).rx_dropped);
1401 jme_restart_rx_engine(jme);
1402 }
1403 atomic_inc(&jme->rx_empty);
1404}
1405
1406static void
1407jme_wake_queue_if_stopped(struct jme_adapter *jme)
1408{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00001409 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001410
1411 smp_wmb();
1412 if (unlikely(netif_queue_stopped(jme->dev) &&
1413 atomic_read(&txring->nr_free) >= (jme->tx_wake_threshold))) {
Joe Perches49d70c42010-09-04 22:21:05 +00001414 netif_info(jme, tx_done, jme->dev, "TX Queue Waked\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001415 netif_wake_queue(jme->dev);
1416 }
1417
1418}
1419
1420static void
1421jme_tx_clean_tasklet(unsigned long arg)
1422{
1423 struct jme_adapter *jme = (struct jme_adapter *)arg;
1424 struct jme_ring *txring = &(jme->txring[0]);
1425 struct txdesc *txdesc = txring->desc;
1426 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi, *ttxbi;
1427 int i, j, cnt = 0, max, err, mask;
1428
Joe Perches49d70c42010-09-04 22:21:05 +00001429 tx_dbg(jme, "Into txclean\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001430
1431 if (unlikely(!atomic_dec_and_test(&jme->tx_cleaning)))
1432 goto out;
1433
1434 if (unlikely(atomic_read(&jme->link_changing) != 1))
1435 goto out;
1436
1437 if (unlikely(!netif_carrier_ok(jme->dev)))
1438 goto out;
1439
1440 max = jme->tx_ring_size - atomic_read(&txring->nr_free);
1441 mask = jme->tx_ring_mask;
1442
1443 for (i = atomic_read(&txring->next_to_clean) ; cnt < max ; ) {
1444
1445 ctxbi = txbi + i;
1446
1447 if (likely(ctxbi->skb &&
1448 !(txdesc[i].descwb.flags & TXWBFLAG_OWN))) {
1449
1450 tx_dbg(jme, "txclean: %d+%d@%lu\n",
Joe Perches49d70c42010-09-04 22:21:05 +00001451 i, ctxbi->nr_desc, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001452
1453 err = txdesc[i].descwb.flags & TXWBFLAG_ALLERR;
1454
1455 for (j = 1 ; j < ctxbi->nr_desc ; ++j) {
1456 ttxbi = txbi + ((i + j) & (mask));
1457 txdesc[(i + j) & (mask)].dw[0] = 0;
1458
1459 pci_unmap_page(jme->pdev,
1460 ttxbi->mapping,
1461 ttxbi->len,
1462 PCI_DMA_TODEVICE);
1463
1464 ttxbi->mapping = 0;
1465 ttxbi->len = 0;
1466 }
1467
1468 dev_kfree_skb(ctxbi->skb);
1469
1470 cnt += ctxbi->nr_desc;
1471
1472 if (unlikely(err)) {
1473 ++(NET_STAT(jme).tx_carrier_errors);
1474 } else {
1475 ++(NET_STAT(jme).tx_packets);
1476 NET_STAT(jme).tx_bytes += ctxbi->len;
1477 }
1478
1479 ctxbi->skb = NULL;
1480 ctxbi->len = 0;
1481 ctxbi->start_xmit = 0;
1482
1483 } else {
1484 break;
1485 }
1486
1487 i = (i + ctxbi->nr_desc) & mask;
1488
1489 ctxbi->nr_desc = 0;
1490 }
1491
Joe Perches49d70c42010-09-04 22:21:05 +00001492 tx_dbg(jme, "txclean: done %d@%lu\n", i, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001493 atomic_set(&txring->next_to_clean, i);
1494 atomic_add(cnt, &txring->nr_free);
1495
1496 jme_wake_queue_if_stopped(jme);
1497
1498out:
1499 atomic_inc(&jme->tx_cleaning);
1500}
1501
1502static void
1503jme_intr_msi(struct jme_adapter *jme, u32 intrstat)
1504{
1505 /*
1506 * Disable interrupt
1507 */
1508 jwrite32f(jme, JME_IENC, INTR_ENABLE);
1509
1510 if (intrstat & (INTR_LINKCH | INTR_SWINTR)) {
1511 /*
1512 * Link change event is critical
1513 * all other events are ignored
1514 */
1515 jwrite32(jme, JME_IEVE, intrstat);
1516 tasklet_schedule(&jme->linkch_task);
1517 goto out_reenable;
1518 }
1519
1520 if (intrstat & INTR_TMINTR) {
1521 jwrite32(jme, JME_IEVE, INTR_TMINTR);
1522 tasklet_schedule(&jme->pcc_task);
1523 }
1524
1525 if (intrstat & (INTR_PCCTXTO | INTR_PCCTX)) {
1526 jwrite32(jme, JME_IEVE, INTR_PCCTXTO | INTR_PCCTX | INTR_TX0);
1527 tasklet_schedule(&jme->txclean_task);
1528 }
1529
1530 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1531 jwrite32(jme, JME_IEVE, (intrstat & (INTR_PCCRX0TO |
1532 INTR_PCCRX0 |
1533 INTR_RX0EMP)) |
1534 INTR_RX0);
1535 }
1536
1537 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
1538 if (intrstat & INTR_RX0EMP)
1539 atomic_inc(&jme->rx_empty);
1540
1541 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1542 if (likely(JME_RX_SCHEDULE_PREP(jme))) {
1543 jme_polling_mode(jme);
1544 JME_RX_SCHEDULE(jme);
1545 }
1546 }
1547 } else {
1548 if (intrstat & INTR_RX0EMP) {
1549 atomic_inc(&jme->rx_empty);
1550 tasklet_hi_schedule(&jme->rxempty_task);
1551 } else if (intrstat & (INTR_PCCRX0TO | INTR_PCCRX0)) {
1552 tasklet_hi_schedule(&jme->rxclean_task);
1553 }
1554 }
1555
1556out_reenable:
1557 /*
1558 * Re-enable interrupt
1559 */
1560 jwrite32f(jme, JME_IENS, INTR_ENABLE);
1561}
1562
1563static irqreturn_t
1564jme_intr(int irq, void *dev_id)
1565{
1566 struct net_device *netdev = dev_id;
1567 struct jme_adapter *jme = netdev_priv(netdev);
1568 u32 intrstat;
1569
1570 intrstat = jread32(jme, JME_IEVE);
1571
1572 /*
1573 * Check if it's really an interrupt for us
1574 */
akeemting576b5222008-10-08 19:50:03 -07001575 if (unlikely((intrstat & INTR_ENABLE) == 0))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001576 return IRQ_NONE;
1577
1578 /*
1579 * Check if the device still exist
1580 */
1581 if (unlikely(intrstat == ~((typeof(intrstat))0)))
1582 return IRQ_NONE;
1583
1584 jme_intr_msi(jme, intrstat);
1585
1586 return IRQ_HANDLED;
1587}
1588
1589static irqreturn_t
1590jme_msi(int irq, void *dev_id)
1591{
1592 struct net_device *netdev = dev_id;
1593 struct jme_adapter *jme = netdev_priv(netdev);
1594 u32 intrstat;
1595
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00001596 intrstat = jread32(jme, JME_IEVE);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001597
1598 jme_intr_msi(jme, intrstat);
1599
1600 return IRQ_HANDLED;
1601}
1602
1603static void
1604jme_reset_link(struct jme_adapter *jme)
1605{
1606 jwrite32(jme, JME_TMCSR, TMCSR_SWIT);
1607}
1608
1609static void
1610jme_restart_an(struct jme_adapter *jme)
1611{
1612 u32 bmcr;
1613
1614 spin_lock_bh(&jme->phy_lock);
1615 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1616 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
1617 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1618 spin_unlock_bh(&jme->phy_lock);
1619}
1620
1621static int
1622jme_request_irq(struct jme_adapter *jme)
1623{
1624 int rc;
1625 struct net_device *netdev = jme->dev;
1626 irq_handler_t handler = jme_intr;
1627 int irq_flags = IRQF_SHARED;
1628
1629 if (!pci_enable_msi(jme->pdev)) {
1630 set_bit(JME_FLAG_MSI, &jme->flags);
1631 handler = jme_msi;
1632 irq_flags = 0;
1633 }
1634
1635 rc = request_irq(jme->pdev->irq, handler, irq_flags, netdev->name,
1636 netdev);
1637 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00001638 netdev_err(netdev,
1639 "Unable to request %s interrupt (return: %d)\n",
1640 test_bit(JME_FLAG_MSI, &jme->flags) ? "MSI" : "INTx",
1641 rc);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001642
1643 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1644 pci_disable_msi(jme->pdev);
1645 clear_bit(JME_FLAG_MSI, &jme->flags);
1646 }
1647 } else {
1648 netdev->irq = jme->pdev->irq;
1649 }
1650
1651 return rc;
1652}
1653
1654static void
1655jme_free_irq(struct jme_adapter *jme)
1656{
1657 free_irq(jme->pdev->irq, jme->dev);
1658 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1659 pci_disable_msi(jme->pdev);
1660 clear_bit(JME_FLAG_MSI, &jme->flags);
1661 jme->dev->irq = jme->pdev->irq;
1662 }
1663}
1664
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001665static inline void
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001666jme_new_phy_on(struct jme_adapter *jme)
1667{
1668 u32 reg;
1669
1670 reg = jread32(jme, JME_PHY_PWR);
1671 reg &= ~(PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1672 PHY_PWR_DWN2 | PHY_PWR_CLKSEL);
1673 jwrite32(jme, JME_PHY_PWR, reg);
1674
1675 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1676 reg &= ~PE1_GPREG0_PBG;
1677 reg |= PE1_GPREG0_ENBG;
1678 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1679}
1680
1681static inline void
1682jme_new_phy_off(struct jme_adapter *jme)
1683{
1684 u32 reg;
1685
1686 reg = jread32(jme, JME_PHY_PWR);
1687 reg |= PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1688 PHY_PWR_DWN2 | PHY_PWR_CLKSEL;
1689 jwrite32(jme, JME_PHY_PWR, reg);
1690
1691 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
1692 reg &= ~PE1_GPREG0_PBG;
1693 reg |= PE1_GPREG0_PDD3COLD;
1694 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1695}
1696
1697static inline void
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001698jme_phy_on(struct jme_adapter *jme)
1699{
1700 u32 bmcr;
1701
1702 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1703 bmcr &= ~BMCR_PDOWN;
1704 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001705
1706 if (new_phy_power_ctrl(jme->chip_main_rev))
1707 jme_new_phy_on(jme);
1708}
1709
1710static inline void
1711jme_phy_off(struct jme_adapter *jme)
1712{
1713 u32 bmcr;
1714
1715 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1716 bmcr |= BMCR_PDOWN;
1717 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1718
1719 if (new_phy_power_ctrl(jme->chip_main_rev))
1720 jme_new_phy_off(jme);
Guo-Fu Tsengc8a86842010-10-18 14:10:40 +00001721}
1722
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001723static int
Aries Leec4860ba2011-11-21 10:20:42 +00001724jme_phy_specreg_read(struct jme_adapter *jme, u32 specreg)
1725{
1726 u32 phy_addr;
1727
1728 phy_addr = JM_PHY_SPEC_REG_READ | specreg;
1729 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1730 phy_addr);
1731 return jme_mdio_read(jme->dev, jme->mii_if.phy_id,
1732 JM_PHY_SPEC_DATA_REG);
1733}
1734
1735static void
1736jme_phy_specreg_write(struct jme_adapter *jme, u32 ext_reg, u32 phy_data)
1737{
1738 u32 phy_addr;
1739
1740 phy_addr = JM_PHY_SPEC_REG_WRITE | ext_reg;
1741 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_DATA_REG,
1742 phy_data);
1743 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1744 phy_addr);
1745}
1746
1747static int
1748jme_phy_calibration(struct jme_adapter *jme)
1749{
1750 u32 ctrl1000, phy_data;
1751
1752 jme_phy_off(jme);
1753 jme_phy_on(jme);
1754 /* Enabel PHY test mode 1 */
1755 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1756 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1757 ctrl1000 |= PHY_GAD_TEST_MODE_1;
1758 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1759
1760 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1761 phy_data &= ~JM_PHY_EXT_COMM_2_CALI_MODE_0;
1762 phy_data |= JM_PHY_EXT_COMM_2_CALI_LATCH |
1763 JM_PHY_EXT_COMM_2_CALI_ENABLE;
1764 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1765 msleep(20);
1766 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1767 phy_data &= ~(JM_PHY_EXT_COMM_2_CALI_ENABLE |
1768 JM_PHY_EXT_COMM_2_CALI_MODE_0 |
1769 JM_PHY_EXT_COMM_2_CALI_LATCH);
1770 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1771
1772 /* Disable PHY test mode */
1773 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1774 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1775 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1776 return 0;
1777}
1778
1779static int
1780jme_phy_setEA(struct jme_adapter *jme)
1781{
1782 u32 phy_comm0 = 0, phy_comm1 = 0;
1783 u8 nic_ctrl;
1784
1785 pci_read_config_byte(jme->pdev, PCI_PRIV_SHARE_NICCTRL, &nic_ctrl);
1786 if ((nic_ctrl & 0x3) == JME_FLAG_PHYEA_ENABLE)
1787 return 0;
1788
1789 switch (jme->pdev->device) {
1790 case PCI_DEVICE_ID_JMICRON_JMC250:
1791 if (((jme->chip_main_rev == 5) &&
1792 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1793 (jme->chip_sub_rev == 3))) ||
1794 (jme->chip_main_rev >= 6)) {
1795 phy_comm0 = 0x008A;
1796 phy_comm1 = 0x4109;
1797 }
1798 if ((jme->chip_main_rev == 3) &&
1799 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1800 phy_comm0 = 0xE088;
1801 break;
1802 case PCI_DEVICE_ID_JMICRON_JMC260:
1803 if (((jme->chip_main_rev == 5) &&
1804 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1805 (jme->chip_sub_rev == 3))) ||
1806 (jme->chip_main_rev >= 6)) {
1807 phy_comm0 = 0x008A;
1808 phy_comm1 = 0x4109;
1809 }
1810 if ((jme->chip_main_rev == 3) &&
1811 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1812 phy_comm0 = 0xE088;
1813 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 0))
1814 phy_comm0 = 0x608A;
1815 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 2))
1816 phy_comm0 = 0x408A;
1817 break;
1818 default:
1819 return -ENODEV;
1820 }
1821 if (phy_comm0)
1822 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_0_REG, phy_comm0);
1823 if (phy_comm1)
1824 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_1_REG, phy_comm1);
1825
1826 return 0;
1827}
1828
1829static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001830jme_open(struct net_device *netdev)
1831{
1832 struct jme_adapter *jme = netdev_priv(netdev);
1833 int rc;
1834
Guo-Fu Tseng0772a992016-03-05 08:11:55 +08001835 jme_clear_pm_disable_wol(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001836 JME_NAPI_ENABLE(jme);
1837
Xiaotian Feng71c6c832012-11-13 19:47:36 +00001838 tasklet_init(&jme->linkch_task, jme_link_change_tasklet,
1839 (unsigned long) jme);
1840 tasklet_init(&jme->txclean_task, jme_tx_clean_tasklet,
1841 (unsigned long) jme);
1842 tasklet_init(&jme->rxclean_task, jme_rx_clean_tasklet,
1843 (unsigned long) jme);
1844 tasklet_init(&jme->rxempty_task, jme_rx_empty_tasklet,
1845 (unsigned long) jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001846
1847 rc = jme_request_irq(jme);
1848 if (rc)
1849 goto err_out;
1850
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001851 jme_start_irq(jme);
1852
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001853 jme_phy_on(jme);
1854 if (test_bit(JME_FLAG_SSET, &jme->flags))
Philippe Reynesc5238382017-01-15 23:18:21 +01001855 jme_set_link_ksettings(netdev, &jme->old_cmd);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00001856 else
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001857 jme_reset_phy_processor(jme);
Aries Leec4860ba2011-11-21 10:20:42 +00001858 jme_phy_calibration(jme);
1859 jme_phy_setEA(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001860 jme_reset_link(jme);
1861
1862 return 0;
1863
1864err_out:
1865 netif_stop_queue(netdev);
1866 netif_carrier_off(netdev);
1867 return rc;
1868}
1869
1870static void
1871jme_set_100m_half(struct jme_adapter *jme)
1872{
1873 u32 bmcr, tmp;
1874
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001875 jme_phy_on(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001876 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1877 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
1878 BMCR_SPEED1000 | BMCR_FULLDPLX);
1879 tmp |= BMCR_SPEED100;
1880
1881 if (bmcr != tmp)
1882 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, tmp);
1883
1884 if (jme->fpgaver)
1885 jwrite32(jme, JME_GHC, GHC_SPEED_100M | GHC_LINK_POLL);
1886 else
1887 jwrite32(jme, JME_GHC, GHC_SPEED_100M);
1888}
1889
1890#define JME_WAIT_LINK_TIME 2000 /* 2000ms */
1891static void
1892jme_wait_link(struct jme_adapter *jme)
1893{
1894 u32 phylink, to = JME_WAIT_LINK_TIME;
1895
Jia-Ju Baid818c592018-07-27 16:25:07 +08001896 msleep(1000);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001897 phylink = jme_linkstat_from_phy(jme);
1898 while (!(phylink & PHY_LINK_UP) && (to -= 10) > 0) {
Jia-Ju Baid818c592018-07-27 16:25:07 +08001899 usleep_range(10000, 11000);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001900 phylink = jme_linkstat_from_phy(jme);
1901 }
1902}
1903
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001904static void
1905jme_powersave_phy(struct jme_adapter *jme)
1906{
Guo-Fu Tseng81422e62016-03-05 08:11:56 +08001907 if (jme->reg_pmcs && device_may_wakeup(&jme->pdev->dev)) {
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001908 jme_set_100m_half(jme);
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001909 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
1910 jme_wait_link(jme);
Guo-Fu Tseng0772a992016-03-05 08:11:55 +08001911 jme_clear_pm_enable_wol(jme);
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07001912 } else {
1913 jme_phy_off(jme);
1914 }
1915}
1916
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001917static int
1918jme_close(struct net_device *netdev)
1919{
1920 struct jme_adapter *jme = netdev_priv(netdev);
1921
1922 netif_stop_queue(netdev);
1923 netif_carrier_off(netdev);
1924
1925 jme_stop_irq(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001926 jme_free_irq(jme);
1927
1928 JME_NAPI_DISABLE(jme);
1929
Xiaotian Feng175c0df2012-10-31 00:29:57 +00001930 tasklet_kill(&jme->linkch_task);
1931 tasklet_kill(&jme->txclean_task);
1932 tasklet_kill(&jme->rxclean_task);
1933 tasklet_kill(&jme->rxempty_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001934
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001935 jme_disable_rx_engine(jme);
1936 jme_disable_tx_engine(jme);
1937 jme_reset_mac_processor(jme);
1938 jme_free_rx_resources(jme);
1939 jme_free_tx_resources(jme);
1940 jme->phylink = 0;
1941 jme_phy_off(jme);
1942
1943 return 0;
1944}
1945
1946static int
1947jme_alloc_txdesc(struct jme_adapter *jme,
1948 struct sk_buff *skb)
1949{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00001950 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001951 int idx, nr_alloc, mask = jme->tx_ring_mask;
1952
1953 idx = txring->next_to_use;
1954 nr_alloc = skb_shinfo(skb)->nr_frags + 2;
1955
1956 if (unlikely(atomic_read(&txring->nr_free) < nr_alloc))
1957 return -1;
1958
1959 atomic_sub(nr_alloc, &txring->nr_free);
1960
1961 txring->next_to_use = (txring->next_to_use + nr_alloc) & mask;
1962
1963 return idx;
1964}
1965
Neil Horman76a691d2014-05-05 14:51:47 -04001966static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001967jme_fill_tx_map(struct pci_dev *pdev,
1968 struct txdesc *txdesc,
1969 struct jme_buffer_info *txbi,
1970 struct page *page,
1971 u32 page_offset,
1972 u32 len,
Michał Mirosław3ad9b352011-11-16 14:05:33 +00001973 bool hidma)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001974{
1975 dma_addr_t dmaaddr;
1976
1977 dmaaddr = pci_map_page(pdev,
1978 page,
1979 page_offset,
1980 len,
1981 PCI_DMA_TODEVICE);
1982
Neil Horman76a691d2014-05-05 14:51:47 -04001983 if (unlikely(pci_dma_mapping_error(pdev, dmaaddr)))
1984 return -EINVAL;
1985
Guo-Fu Tseng95252232008-09-16 01:00:11 +08001986 pci_dma_sync_single_for_device(pdev,
1987 dmaaddr,
1988 len,
1989 PCI_DMA_TODEVICE);
1990
1991 txdesc->dw[0] = 0;
1992 txdesc->dw[1] = 0;
1993 txdesc->desc2.flags = TXFLAG_OWN;
1994 txdesc->desc2.flags |= (hidma) ? TXFLAG_64BIT : 0;
1995 txdesc->desc2.datalen = cpu_to_le16(len);
1996 txdesc->desc2.bufaddrh = cpu_to_le32((__u64)dmaaddr >> 32);
1997 txdesc->desc2.bufaddrl = cpu_to_le32(
1998 (__u64)dmaaddr & 0xFFFFFFFFUL);
1999
2000 txbi->mapping = dmaaddr;
2001 txbi->len = len;
Neil Horman76a691d2014-05-05 14:51:47 -04002002 return 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002003}
2004
Neil Hormanc4b16062014-05-12 10:38:18 -04002005static void jme_drop_tx_map(struct jme_adapter *jme, int startidx, int count)
Neil Horman76a691d2014-05-05 14:51:47 -04002006{
2007 struct jme_ring *txring = &(jme->txring[0]);
2008 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
2009 int mask = jme->tx_ring_mask;
2010 int j;
2011
Neil Hormanc4b16062014-05-12 10:38:18 -04002012 for (j = 0 ; j < count ; j++) {
Neil Horman76a691d2014-05-05 14:51:47 -04002013 ctxbi = txbi + ((startidx + j + 2) & (mask));
2014 pci_unmap_page(jme->pdev,
2015 ctxbi->mapping,
2016 ctxbi->len,
2017 PCI_DMA_TODEVICE);
2018
Colin Ian Kingfb709502019-01-17 00:03:26 +00002019 ctxbi->mapping = 0;
2020 ctxbi->len = 0;
Neil Horman76a691d2014-05-05 14:51:47 -04002021 }
Neil Horman76a691d2014-05-05 14:51:47 -04002022}
2023
2024static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002025jme_map_tx_skb(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2026{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002027 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002028 struct txdesc *txdesc = txring->desc, *ctxdesc;
2029 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
Michał Mirosław3ad9b352011-11-16 14:05:33 +00002030 bool hidma = jme->dev->features & NETIF_F_HIGHDMA;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002031 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2032 int mask = jme->tx_ring_mask;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002033 u32 len;
Neil Horman76a691d2014-05-05 14:51:47 -04002034 int ret = 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002035
2036 for (i = 0 ; i < nr_frags ; ++i) {
Matthew Wilcox (Oracle)d7840972019-07-22 20:08:25 -07002037 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2038
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002039 ctxdesc = txdesc + ((idx + i + 2) & (mask));
2040 ctxbi = txbi + ((idx + i + 2) & (mask));
2041
Neil Horman76a691d2014-05-05 14:51:47 -04002042 ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi,
Ian Campbellf3273582011-08-31 00:46:54 +00002043 skb_frag_page(frag),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002044 frag->page_offset, skb_frag_size(frag), hidma);
Neil Horman76a691d2014-05-05 14:51:47 -04002045 if (ret) {
Neil Hormanc4b16062014-05-12 10:38:18 -04002046 jme_drop_tx_map(jme, idx, i);
Neil Horman76a691d2014-05-05 14:51:47 -04002047 goto out;
2048 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002049 }
2050
2051 len = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
2052 ctxdesc = txdesc + ((idx + 1) & (mask));
2053 ctxbi = txbi + ((idx + 1) & (mask));
Neil Horman76a691d2014-05-05 14:51:47 -04002054 ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi, virt_to_page(skb->data),
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002055 offset_in_page(skb->data), len, hidma);
Neil Horman76a691d2014-05-05 14:51:47 -04002056 if (ret)
Neil Hormanc4b16062014-05-12 10:38:18 -04002057 jme_drop_tx_map(jme, idx, i);
Neil Horman76a691d2014-05-05 14:51:47 -04002058
2059out:
2060 return ret;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002061
2062}
2063
Neil Horman76a691d2014-05-05 14:51:47 -04002064
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002065static int
Harvey Harrison31c221c2008-11-19 15:50:59 -08002066jme_tx_tso(struct sk_buff *skb, __le16 *mss, u8 *flags)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002067{
Harvey Harrison31c221c2008-11-19 15:50:59 -08002068 *mss = cpu_to_le16(skb_shinfo(skb)->gso_size << TXDESC_MSS_SHIFT);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002069 if (*mss) {
2070 *flags |= TXFLAG_LSEN;
2071
2072 if (skb->protocol == htons(ETH_P_IP)) {
2073 struct iphdr *iph = ip_hdr(skb);
2074
2075 iph->check = 0;
2076 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2077 iph->daddr, 0,
2078 IPPROTO_TCP,
2079 0);
2080 } else {
2081 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2082
2083 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ip6h->saddr,
2084 &ip6h->daddr, 0,
2085 IPPROTO_TCP,
2086 0);
2087 }
2088
2089 return 0;
2090 }
2091
2092 return 1;
2093}
2094
2095static void
2096jme_tx_csum(struct jme_adapter *jme, struct sk_buff *skb, u8 *flags)
2097{
2098 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2099 u8 ip_proto;
2100
2101 switch (skb->protocol) {
2102 case htons(ETH_P_IP):
2103 ip_proto = ip_hdr(skb)->protocol;
2104 break;
2105 case htons(ETH_P_IPV6):
2106 ip_proto = ipv6_hdr(skb)->nexthdr;
2107 break;
2108 default:
2109 ip_proto = 0;
2110 break;
2111 }
2112
2113 switch (ip_proto) {
2114 case IPPROTO_TCP:
2115 *flags |= TXFLAG_TCPCS;
2116 break;
2117 case IPPROTO_UDP:
2118 *flags |= TXFLAG_UDPCS;
2119 break;
2120 default:
Joe Perches49d70c42010-09-04 22:21:05 +00002121 netif_err(jme, tx_err, jme->dev, "Error upper layer protocol\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002122 break;
2123 }
2124 }
2125}
2126
2127static inline void
Harvey Harrison31c221c2008-11-19 15:50:59 -08002128jme_tx_vlan(struct sk_buff *skb, __le16 *vlan, u8 *flags)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002129{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002130 if (skb_vlan_tag_present(skb)) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002131 *flags |= TXFLAG_TAGON;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002132 *vlan = cpu_to_le16(skb_vlan_tag_get(skb));
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002133 }
2134}
2135
2136static int
Guo-Fu Tseng7f7fd2d2009-02-27 17:57:01 +00002137jme_fill_tx_desc(struct jme_adapter *jme, struct sk_buff *skb, int idx)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002138{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002139 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002140 struct txdesc *txdesc;
2141 struct jme_buffer_info *txbi;
2142 u8 flags;
Neil Horman76a691d2014-05-05 14:51:47 -04002143 int ret = 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002144
2145 txdesc = (struct txdesc *)txring->desc + idx;
2146 txbi = txring->bufinf + idx;
2147
2148 txdesc->dw[0] = 0;
2149 txdesc->dw[1] = 0;
2150 txdesc->dw[2] = 0;
2151 txdesc->dw[3] = 0;
2152 txdesc->desc1.pktsize = cpu_to_le16(skb->len);
2153 /*
2154 * Set OWN bit at final.
2155 * When kernel transmit faster than NIC.
2156 * And NIC trying to send this descriptor before we tell
2157 * it to start sending this TX queue.
2158 * Other fields are already filled correctly.
2159 */
2160 wmb();
2161 flags = TXFLAG_OWN | TXFLAG_INT;
2162 /*
2163 * Set checksum flags while not tso
2164 */
2165 if (jme_tx_tso(skb, &txdesc->desc1.mss, &flags))
2166 jme_tx_csum(jme, skb, &flags);
2167 jme_tx_vlan(skb, &txdesc->desc1.vlan, &flags);
Neil Horman76a691d2014-05-05 14:51:47 -04002168 ret = jme_map_tx_skb(jme, skb, idx);
2169 if (ret)
2170 return ret;
2171
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002172 txdesc->desc1.flags = flags;
2173 /*
2174 * Set tx buffer info after telling NIC to send
2175 * For better tx_clean timing
2176 */
2177 wmb();
2178 txbi->nr_desc = skb_shinfo(skb)->nr_frags + 2;
2179 txbi->skb = skb;
2180 txbi->len = skb->len;
2181 txbi->start_xmit = jiffies;
2182 if (!txbi->start_xmit)
2183 txbi->start_xmit = (0UL-1);
2184
2185 return 0;
2186}
2187
2188static void
2189jme_stop_queue_if_full(struct jme_adapter *jme)
2190{
Guo-Fu Tsengeacf69a2009-07-06 04:36:30 +00002191 struct jme_ring *txring = &(jme->txring[0]);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002192 struct jme_buffer_info *txbi = txring->bufinf;
2193 int idx = atomic_read(&txring->next_to_clean);
2194
2195 txbi += idx;
2196
2197 smp_wmb();
2198 if (unlikely(atomic_read(&txring->nr_free) < (MAX_SKB_FRAGS+2))) {
2199 netif_stop_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002200 netif_info(jme, tx_queued, jme->dev, "TX Queue Paused\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002201 smp_wmb();
2202 if (atomic_read(&txring->nr_free)
2203 >= (jme->tx_wake_threshold)) {
2204 netif_wake_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002205 netif_info(jme, tx_queued, jme->dev, "TX Queue Fast Waked\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002206 }
2207 }
2208
2209 if (unlikely(txbi->start_xmit &&
2210 (jiffies - txbi->start_xmit) >= TX_TIMEOUT &&
2211 txbi->skb)) {
2212 netif_stop_queue(jme->dev);
Joe Perches49d70c42010-09-04 22:21:05 +00002213 netif_info(jme, tx_queued, jme->dev,
2214 "TX Queue Stopped %d@%lu\n", idx, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002215 }
2216}
2217
2218/*
2219 * This function is already protected by netif_tx_lock()
2220 */
2221
Stephen Hemminger613573252009-08-31 19:50:58 +00002222static netdev_tx_t
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002223jme_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2224{
2225 struct jme_adapter *jme = netdev_priv(netdev);
2226 int idx;
2227
françois romieuba4e6d12014-03-29 12:26:26 +01002228 if (unlikely(skb_is_gso(skb) && skb_cow_head(skb, 0))) {
2229 dev_kfree_skb_any(skb);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002230 ++(NET_STAT(jme).tx_dropped);
2231 return NETDEV_TX_OK;
2232 }
2233
2234 idx = jme_alloc_txdesc(jme, skb);
2235
2236 if (unlikely(idx < 0)) {
2237 netif_stop_queue(netdev);
Joe Perches49d70c42010-09-04 22:21:05 +00002238 netif_err(jme, tx_err, jme->dev,
2239 "BUG! Tx ring full when queue awake!\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002240
2241 return NETDEV_TX_BUSY;
2242 }
2243
Neil Horman76a691d2014-05-05 14:51:47 -04002244 if (jme_fill_tx_desc(jme, skb, idx))
Neil Hormanc4b16062014-05-12 10:38:18 -04002245 return NETDEV_TX_OK;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002246
2247 jwrite32(jme, JME_TXCS, jme->reg_txcs |
2248 TXCS_SELECT_QUEUE0 |
2249 TXCS_QUEUE0S |
2250 TXCS_ENABLE);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002251
Joe Perches49d70c42010-09-04 22:21:05 +00002252 tx_dbg(jme, "xmit: %d+%d@%lu\n",
2253 idx, skb_shinfo(skb)->nr_frags + 2, jiffies);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002254 jme_stop_queue_if_full(jme);
2255
2256 return NETDEV_TX_OK;
2257}
2258
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +00002259static void
2260jme_set_unicastaddr(struct net_device *netdev)
2261{
2262 struct jme_adapter *jme = netdev_priv(netdev);
2263 u32 val;
2264
2265 val = (netdev->dev_addr[3] & 0xff) << 24 |
2266 (netdev->dev_addr[2] & 0xff) << 16 |
2267 (netdev->dev_addr[1] & 0xff) << 8 |
2268 (netdev->dev_addr[0] & 0xff);
2269 jwrite32(jme, JME_RXUMA_LO, val);
2270 val = (netdev->dev_addr[5] & 0xff) << 8 |
2271 (netdev->dev_addr[4] & 0xff);
2272 jwrite32(jme, JME_RXUMA_HI, val);
2273}
2274
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002275static int
2276jme_set_macaddr(struct net_device *netdev, void *p)
2277{
2278 struct jme_adapter *jme = netdev_priv(netdev);
2279 struct sockaddr *addr = p;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002280
2281 if (netif_running(netdev))
2282 return -EBUSY;
2283
2284 spin_lock_bh(&jme->macaddr_lock);
2285 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Guo-Fu Tseng8b53aba2011-02-13 18:27:40 +00002286 jme_set_unicastaddr(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002287 spin_unlock_bh(&jme->macaddr_lock);
2288
2289 return 0;
2290}
2291
2292static void
2293jme_set_multi(struct net_device *netdev)
2294{
2295 struct jme_adapter *jme = netdev_priv(netdev);
2296 u32 mc_hash[2] = {};
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002297
2298 spin_lock_bh(&jme->rxmcs_lock);
2299
2300 jme->reg_rxmcs |= RXMCS_BRDFRAME | RXMCS_UNIFRAME;
2301
2302 if (netdev->flags & IFF_PROMISC) {
2303 jme->reg_rxmcs |= RXMCS_ALLFRAME;
2304 } else if (netdev->flags & IFF_ALLMULTI) {
2305 jme->reg_rxmcs |= RXMCS_ALLMULFRAME;
2306 } else if (netdev->flags & IFF_MULTICAST) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002307 struct netdev_hw_addr *ha;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002308 int bit_nr;
2309
2310 jme->reg_rxmcs |= RXMCS_MULFRAME | RXMCS_MULFILTERED;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002311 netdev_for_each_mc_addr(ha, netdev) {
2312 bit_nr = ether_crc(ETH_ALEN, ha->addr) & 0x3F;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002313 mc_hash[bit_nr >> 5] |= 1 << (bit_nr & 0x1F);
2314 }
2315
2316 jwrite32(jme, JME_RXMCHT_LO, mc_hash[0]);
2317 jwrite32(jme, JME_RXMCHT_HI, mc_hash[1]);
2318 }
2319
2320 wmb();
2321 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2322
2323 spin_unlock_bh(&jme->rxmcs_lock);
2324}
2325
2326static int
2327jme_change_mtu(struct net_device *netdev, int new_mtu)
2328{
2329 struct jme_adapter *jme = netdev_priv(netdev);
2330
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002331 netdev->mtu = new_mtu;
Michał Mirosławd7b57652011-03-31 01:01:35 +00002332 netdev_update_features(netdev);
2333
Guo-Fu Tsengba9adbe2012-02-22 08:58:10 +00002334 jme_restart_rx_engine(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002335 jme_reset_link(jme);
2336
2337 return 0;
2338}
2339
2340static void
2341jme_tx_timeout(struct net_device *netdev)
2342{
2343 struct jme_adapter *jme = netdev_priv(netdev);
2344
2345 jme->phylink = 0;
2346 jme_reset_phy_processor(jme);
2347 if (test_bit(JME_FLAG_SSET, &jme->flags))
Philippe Reynesc5238382017-01-15 23:18:21 +01002348 jme_set_link_ksettings(netdev, &jme->old_cmd);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002349
2350 /*
2351 * Force to Reset the link again
2352 */
2353 jme_reset_link(jme);
2354}
2355
2356static void
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002357jme_get_drvinfo(struct net_device *netdev,
2358 struct ethtool_drvinfo *info)
2359{
2360 struct jme_adapter *jme = netdev_priv(netdev);
2361
Rick Jones23020ab2011-11-09 09:58:07 +00002362 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2363 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2364 strlcpy(info->bus_info, pci_name(jme->pdev), sizeof(info->bus_info));
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002365}
2366
2367static int
2368jme_get_regs_len(struct net_device *netdev)
2369{
2370 return JME_REG_LEN;
2371}
2372
2373static void
2374mmapio_memcpy(struct jme_adapter *jme, u32 *p, u32 reg, int len)
2375{
2376 int i;
2377
2378 for (i = 0 ; i < len ; i += 4)
2379 p[i >> 2] = jread32(jme, reg + i);
2380}
2381
2382static void
2383mdio_memcpy(struct jme_adapter *jme, u32 *p, int reg_nr)
2384{
2385 int i;
2386 u16 *p16 = (u16 *)p;
2387
2388 for (i = 0 ; i < reg_nr ; ++i)
2389 p16[i] = jme_mdio_read(jme->dev, jme->mii_if.phy_id, i);
2390}
2391
2392static void
2393jme_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
2394{
2395 struct jme_adapter *jme = netdev_priv(netdev);
2396 u32 *p32 = (u32 *)p;
2397
2398 memset(p, 0xFF, JME_REG_LEN);
2399
2400 regs->version = 1;
2401 mmapio_memcpy(jme, p32, JME_MAC, JME_MAC_LEN);
2402
2403 p32 += 0x100 >> 2;
2404 mmapio_memcpy(jme, p32, JME_PHY, JME_PHY_LEN);
2405
2406 p32 += 0x100 >> 2;
2407 mmapio_memcpy(jme, p32, JME_MISC, JME_MISC_LEN);
2408
2409 p32 += 0x100 >> 2;
2410 mmapio_memcpy(jme, p32, JME_RSS, JME_RSS_LEN);
2411
2412 p32 += 0x100 >> 2;
2413 mdio_memcpy(jme, p32, JME_PHY_REG_NR);
2414}
2415
2416static int
2417jme_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2418{
2419 struct jme_adapter *jme = netdev_priv(netdev);
2420
2421 ecmd->tx_coalesce_usecs = PCC_TX_TO;
2422 ecmd->tx_max_coalesced_frames = PCC_TX_CNT;
2423
2424 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2425 ecmd->use_adaptive_rx_coalesce = false;
2426 ecmd->rx_coalesce_usecs = 0;
2427 ecmd->rx_max_coalesced_frames = 0;
2428 return 0;
2429 }
2430
2431 ecmd->use_adaptive_rx_coalesce = true;
2432
2433 switch (jme->dpi.cur) {
2434 case PCC_P1:
2435 ecmd->rx_coalesce_usecs = PCC_P1_TO;
2436 ecmd->rx_max_coalesced_frames = PCC_P1_CNT;
2437 break;
2438 case PCC_P2:
2439 ecmd->rx_coalesce_usecs = PCC_P2_TO;
2440 ecmd->rx_max_coalesced_frames = PCC_P2_CNT;
2441 break;
2442 case PCC_P3:
2443 ecmd->rx_coalesce_usecs = PCC_P3_TO;
2444 ecmd->rx_max_coalesced_frames = PCC_P3_CNT;
2445 break;
2446 default:
2447 break;
2448 }
2449
2450 return 0;
2451}
2452
2453static int
2454jme_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
2455{
2456 struct jme_adapter *jme = netdev_priv(netdev);
2457 struct dynpcc_info *dpi = &(jme->dpi);
2458
2459 if (netif_running(netdev))
2460 return -EBUSY;
2461
Joe Perches8e95a202009-12-03 07:58:21 +00002462 if (ecmd->use_adaptive_rx_coalesce &&
2463 test_bit(JME_FLAG_POLL, &jme->flags)) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002464 clear_bit(JME_FLAG_POLL, &jme->flags);
2465 jme->jme_rx = netif_rx;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002466 dpi->cur = PCC_P1;
2467 dpi->attempt = PCC_P1;
2468 dpi->cnt = 0;
2469 jme_set_rx_pcc(jme, PCC_P1);
2470 jme_interrupt_mode(jme);
Joe Perches8e95a202009-12-03 07:58:21 +00002471 } else if (!(ecmd->use_adaptive_rx_coalesce) &&
2472 !(test_bit(JME_FLAG_POLL, &jme->flags))) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002473 set_bit(JME_FLAG_POLL, &jme->flags);
2474 jme->jme_rx = netif_receive_skb;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002475 jme_interrupt_mode(jme);
2476 }
2477
2478 return 0;
2479}
2480
2481static void
2482jme_get_pauseparam(struct net_device *netdev,
2483 struct ethtool_pauseparam *ecmd)
2484{
2485 struct jme_adapter *jme = netdev_priv(netdev);
2486 u32 val;
2487
2488 ecmd->tx_pause = (jme->reg_txpfc & TXPFC_PF_EN) != 0;
2489 ecmd->rx_pause = (jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0;
2490
2491 spin_lock_bh(&jme->phy_lock);
2492 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2493 spin_unlock_bh(&jme->phy_lock);
2494
2495 ecmd->autoneg =
2496 (val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0;
2497}
2498
2499static int
2500jme_set_pauseparam(struct net_device *netdev,
2501 struct ethtool_pauseparam *ecmd)
2502{
2503 struct jme_adapter *jme = netdev_priv(netdev);
2504 u32 val;
2505
2506 if (((jme->reg_txpfc & TXPFC_PF_EN) != 0) ^
2507 (ecmd->tx_pause != 0)) {
2508
2509 if (ecmd->tx_pause)
2510 jme->reg_txpfc |= TXPFC_PF_EN;
2511 else
2512 jme->reg_txpfc &= ~TXPFC_PF_EN;
2513
2514 jwrite32(jme, JME_TXPFC, jme->reg_txpfc);
2515 }
2516
2517 spin_lock_bh(&jme->rxmcs_lock);
2518 if (((jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0) ^
2519 (ecmd->rx_pause != 0)) {
2520
2521 if (ecmd->rx_pause)
2522 jme->reg_rxmcs |= RXMCS_FLOWCTRL;
2523 else
2524 jme->reg_rxmcs &= ~RXMCS_FLOWCTRL;
2525
2526 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2527 }
2528 spin_unlock_bh(&jme->rxmcs_lock);
2529
2530 spin_lock_bh(&jme->phy_lock);
2531 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2532 if (((val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0) ^
2533 (ecmd->autoneg != 0)) {
2534
2535 if (ecmd->autoneg)
2536 val |= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2537 else
2538 val &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2539
2540 jme_mdio_write(jme->dev, jme->mii_if.phy_id,
2541 MII_ADVERTISE, val);
2542 }
2543 spin_unlock_bh(&jme->phy_lock);
2544
2545 return 0;
2546}
2547
2548static void
2549jme_get_wol(struct net_device *netdev,
2550 struct ethtool_wolinfo *wol)
2551{
2552 struct jme_adapter *jme = netdev_priv(netdev);
2553
2554 wol->supported = WAKE_MAGIC | WAKE_PHY;
2555
2556 wol->wolopts = 0;
2557
2558 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
2559 wol->wolopts |= WAKE_PHY;
2560
2561 if (jme->reg_pmcs & PMCS_MFEN)
2562 wol->wolopts |= WAKE_MAGIC;
2563
2564}
2565
2566static int
2567jme_set_wol(struct net_device *netdev,
2568 struct ethtool_wolinfo *wol)
2569{
2570 struct jme_adapter *jme = netdev_priv(netdev);
2571
2572 if (wol->wolopts & (WAKE_MAGICSECURE |
2573 WAKE_UCAST |
2574 WAKE_MCAST |
2575 WAKE_BCAST |
2576 WAKE_ARP))
2577 return -EOPNOTSUPP;
2578
2579 jme->reg_pmcs = 0;
2580
2581 if (wol->wolopts & WAKE_PHY)
2582 jme->reg_pmcs |= PMCS_LFEN | PMCS_LREN;
2583
2584 if (wol->wolopts & WAKE_MAGIC)
2585 jme->reg_pmcs |= PMCS_MFEN;
2586
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002587 return 0;
2588}
2589
2590static int
Philippe Reynesc5238382017-01-15 23:18:21 +01002591jme_get_link_ksettings(struct net_device *netdev,
2592 struct ethtool_link_ksettings *cmd)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002593{
2594 struct jme_adapter *jme = netdev_priv(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002595
2596 spin_lock_bh(&jme->phy_lock);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002597 mii_ethtool_get_link_ksettings(&jme->mii_if, cmd);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002598 spin_unlock_bh(&jme->phy_lock);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002599 return 0;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002600}
2601
2602static int
Philippe Reynesc5238382017-01-15 23:18:21 +01002603jme_set_link_ksettings(struct net_device *netdev,
2604 const struct ethtool_link_ksettings *cmd)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002605{
2606 struct jme_adapter *jme = netdev_priv(netdev);
2607 int rc, fdc = 0;
2608
Philippe Reynesc5238382017-01-15 23:18:21 +01002609 if (cmd->base.speed == SPEED_1000 &&
2610 cmd->base.autoneg != AUTONEG_ENABLE)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002611 return -EINVAL;
2612
Guo-Fu Tseng3ee94012010-10-18 14:10:41 +00002613 /*
2614 * Check If user changed duplex only while force_media.
2615 * Hardware would not generate link change interrupt.
2616 */
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002617 if (jme->mii_if.force_media &&
Philippe Reynesc5238382017-01-15 23:18:21 +01002618 cmd->base.autoneg != AUTONEG_ENABLE &&
2619 (jme->mii_if.full_duplex != cmd->base.duplex))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002620 fdc = 1;
2621
2622 spin_lock_bh(&jme->phy_lock);
Philippe Reynesc5238382017-01-15 23:18:21 +01002623 rc = mii_ethtool_set_link_ksettings(&jme->mii_if, cmd);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002624 spin_unlock_bh(&jme->phy_lock);
2625
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002626 if (!rc) {
Guo-Fu Tseng3ee94012010-10-18 14:10:41 +00002627 if (fdc)
2628 jme_reset_link(jme);
Philippe Reynesc5238382017-01-15 23:18:21 +01002629 jme->old_cmd = *cmd;
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00002630 set_bit(JME_FLAG_SSET, &jme->flags);
2631 }
2632
2633 return rc;
2634}
2635
2636static int
2637jme_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2638{
2639 int rc;
2640 struct jme_adapter *jme = netdev_priv(netdev);
2641 struct mii_ioctl_data *mii_data = if_mii(rq);
2642 unsigned int duplex_chg;
2643
2644 if (cmd == SIOCSMIIREG) {
2645 u16 val = mii_data->val_in;
2646 if (!(val & (BMCR_RESET|BMCR_ANENABLE)) &&
2647 (val & BMCR_SPEED1000))
2648 return -EINVAL;
2649 }
2650
2651 spin_lock_bh(&jme->phy_lock);
2652 rc = generic_mii_ioctl(&jme->mii_if, mii_data, cmd, &duplex_chg);
2653 spin_unlock_bh(&jme->phy_lock);
2654
2655 if (!rc && (cmd == SIOCSMIIREG)) {
2656 if (duplex_chg)
2657 jme_reset_link(jme);
Philippe Reynesc5238382017-01-15 23:18:21 +01002658 jme_get_link_ksettings(netdev, &jme->old_cmd);
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00002659 set_bit(JME_FLAG_SSET, &jme->flags);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002660 }
2661
2662 return rc;
2663}
2664
2665static u32
2666jme_get_link(struct net_device *netdev)
2667{
2668 struct jme_adapter *jme = netdev_priv(netdev);
2669 return jread32(jme, JME_PHY_LINK) & PHY_LINK_UP;
2670}
2671
2672static u32
2673jme_get_msglevel(struct net_device *netdev)
2674{
2675 struct jme_adapter *jme = netdev_priv(netdev);
2676 return jme->msg_enable;
2677}
2678
2679static void
2680jme_set_msglevel(struct net_device *netdev, u32 value)
2681{
2682 struct jme_adapter *jme = netdev_priv(netdev);
2683 jme->msg_enable = value;
2684}
2685
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002686static netdev_features_t
2687jme_fix_features(struct net_device *netdev, netdev_features_t features)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002688{
Michał Mirosławd7b57652011-03-31 01:01:35 +00002689 if (netdev->mtu > 1900)
Tom Herberta1882222015-12-14 11:19:43 -08002690 features &= ~(NETIF_F_ALL_TSO | NETIF_F_CSUM_MASK);
Michał Mirosławd7b57652011-03-31 01:01:35 +00002691 return features;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002692}
2693
2694static int
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002695jme_set_features(struct net_device *netdev, netdev_features_t features)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002696{
2697 struct jme_adapter *jme = netdev_priv(netdev);
2698
2699 spin_lock_bh(&jme->rxmcs_lock);
Michał Mirosławd7b57652011-03-31 01:01:35 +00002700 if (features & NETIF_F_RXCSUM)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002701 jme->reg_rxmcs |= RXMCS_CHECKSUM;
2702 else
2703 jme->reg_rxmcs &= ~RXMCS_CHECKSUM;
2704 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2705 spin_unlock_bh(&jme->rxmcs_lock);
2706
2707 return 0;
2708}
2709
Peter Wued36d7b2012-07-17 06:29:34 +00002710#ifdef CONFIG_NET_POLL_CONTROLLER
2711static void jme_netpoll(struct net_device *dev)
2712{
2713 unsigned long flags;
2714
2715 local_irq_save(flags);
2716 jme_intr(dev->irq, dev);
2717 local_irq_restore(flags);
2718}
2719#endif
2720
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002721static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002722jme_nway_reset(struct net_device *netdev)
2723{
2724 struct jme_adapter *jme = netdev_priv(netdev);
2725 jme_restart_an(jme);
2726 return 0;
2727}
2728
2729static u8
2730jme_smb_read(struct jme_adapter *jme, unsigned int addr)
2731{
2732 u32 val;
2733 int to;
2734
2735 val = jread32(jme, JME_SMBCSR);
2736 to = JME_SMB_BUSY_TIMEOUT;
2737 while ((val & SMBCSR_BUSY) && --to) {
2738 msleep(1);
2739 val = jread32(jme, JME_SMBCSR);
2740 }
2741 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002742 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002743 return 0xFF;
2744 }
2745
2746 jwrite32(jme, JME_SMBINTF,
2747 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2748 SMBINTF_HWRWN_READ |
2749 SMBINTF_HWCMD);
2750
2751 val = jread32(jme, JME_SMBINTF);
2752 to = JME_SMB_BUSY_TIMEOUT;
2753 while ((val & SMBINTF_HWCMD) && --to) {
2754 msleep(1);
2755 val = jread32(jme, JME_SMBINTF);
2756 }
2757 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002758 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002759 return 0xFF;
2760 }
2761
2762 return (val & SMBINTF_HWDATR) >> SMBINTF_HWDATR_SHIFT;
2763}
2764
2765static void
2766jme_smb_write(struct jme_adapter *jme, unsigned int addr, u8 data)
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;
2780 }
2781
2782 jwrite32(jme, JME_SMBINTF,
2783 ((data << SMBINTF_HWDATW_SHIFT) & SMBINTF_HWDATW) |
2784 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2785 SMBINTF_HWRWN_WRITE |
2786 SMBINTF_HWCMD);
2787
2788 val = jread32(jme, JME_SMBINTF);
2789 to = JME_SMB_BUSY_TIMEOUT;
2790 while ((val & SMBINTF_HWCMD) && --to) {
2791 msleep(1);
2792 val = jread32(jme, JME_SMBINTF);
2793 }
2794 if (!to) {
Joe Perches49d70c42010-09-04 22:21:05 +00002795 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002796 return;
2797 }
2798
2799 mdelay(2);
2800}
2801
2802static int
2803jme_get_eeprom_len(struct net_device *netdev)
2804{
2805 struct jme_adapter *jme = netdev_priv(netdev);
2806 u32 val;
2807 val = jread32(jme, JME_SMBCSR);
2808 return (val & SMBCSR_EEPROMD) ? JME_SMB_LEN : 0;
2809}
2810
2811static int
2812jme_get_eeprom(struct net_device *netdev,
2813 struct ethtool_eeprom *eeprom, u8 *data)
2814{
2815 struct jme_adapter *jme = netdev_priv(netdev);
2816 int i, offset = eeprom->offset, len = eeprom->len;
2817
2818 /*
2819 * ethtool will check the boundary for us
2820 */
2821 eeprom->magic = JME_EEPROM_MAGIC;
2822 for (i = 0 ; i < len ; ++i)
2823 data[i] = jme_smb_read(jme, i + offset);
2824
2825 return 0;
2826}
2827
2828static int
2829jme_set_eeprom(struct net_device *netdev,
2830 struct ethtool_eeprom *eeprom, u8 *data)
2831{
2832 struct jme_adapter *jme = netdev_priv(netdev);
2833 int i, offset = eeprom->offset, len = eeprom->len;
2834
2835 if (eeprom->magic != JME_EEPROM_MAGIC)
2836 return -EINVAL;
2837
2838 /*
2839 * ethtool will check the boundary for us
2840 */
2841 for (i = 0 ; i < len ; ++i)
2842 jme_smb_write(jme, i + offset, data[i]);
2843
2844 return 0;
2845}
2846
2847static const struct ethtool_ops jme_ethtool_ops = {
2848 .get_drvinfo = jme_get_drvinfo,
2849 .get_regs_len = jme_get_regs_len,
2850 .get_regs = jme_get_regs,
2851 .get_coalesce = jme_get_coalesce,
2852 .set_coalesce = jme_set_coalesce,
2853 .get_pauseparam = jme_get_pauseparam,
2854 .set_pauseparam = jme_set_pauseparam,
2855 .get_wol = jme_get_wol,
2856 .set_wol = jme_set_wol,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002857 .get_link = jme_get_link,
2858 .get_msglevel = jme_get_msglevel,
2859 .set_msglevel = jme_set_msglevel,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002860 .nway_reset = jme_nway_reset,
2861 .get_eeprom_len = jme_get_eeprom_len,
2862 .get_eeprom = jme_get_eeprom,
2863 .set_eeprom = jme_set_eeprom,
Philippe Reynesc5238382017-01-15 23:18:21 +01002864 .get_link_ksettings = jme_get_link_ksettings,
2865 .set_link_ksettings = jme_set_link_ksettings,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002866};
2867
2868static int
2869jme_pci_dma64(struct pci_dev *pdev)
2870{
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002871 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
Yang Hongyange9304382009-04-13 14:40:14 -07002872 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
2873 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002874 return 1;
2875
2876 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
Yang Hongyange9304382009-04-13 14:40:14 -07002877 !pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2878 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40)))
Guo-Fu Tseng814c01d2009-02-27 17:59:44 +00002879 return 1;
2880
Yang Hongyang284901a2009-04-06 19:01:15 -07002881 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
2882 if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002883 return 0;
2884
2885 return -1;
2886}
2887
2888static inline void
2889jme_phy_init(struct jme_adapter *jme)
2890{
2891 u16 reg26;
2892
2893 reg26 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 26);
2894 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 26, reg26 | 0x1000);
2895}
2896
2897static inline void
2898jme_check_hw_ver(struct jme_adapter *jme)
2899{
2900 u32 chipmode;
2901
2902 chipmode = jread32(jme, JME_CHIPMODE);
2903
2904 jme->fpgaver = (chipmode & CM_FPGAVER_MASK) >> CM_FPGAVER_SHIFT;
2905 jme->chiprev = (chipmode & CM_CHIPREV_MASK) >> CM_CHIPREV_SHIFT;
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00002906 jme->chip_main_rev = jme->chiprev & 0xF;
2907 jme->chip_sub_rev = (jme->chiprev >> 4) & 0xF;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002908}
2909
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002910static const struct net_device_ops jme_netdev_ops = {
2911 .ndo_open = jme_open,
2912 .ndo_stop = jme_close,
2913 .ndo_validate_addr = eth_validate_addr,
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00002914 .ndo_do_ioctl = jme_ioctl,
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002915 .ndo_start_xmit = jme_start_xmit,
2916 .ndo_set_mac_address = jme_set_macaddr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002917 .ndo_set_rx_mode = jme_set_multi,
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002918 .ndo_change_mtu = jme_change_mtu,
2919 .ndo_tx_timeout = jme_tx_timeout,
Michał Mirosławd7b57652011-03-31 01:01:35 +00002920 .ndo_fix_features = jme_fix_features,
2921 .ndo_set_features = jme_set_features,
Peter Wued36d7b2012-07-17 06:29:34 +00002922#ifdef CONFIG_NET_POLL_CONTROLLER
2923 .ndo_poll_controller = jme_netpoll,
2924#endif
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002925};
2926
Bill Pembertonaf375572012-12-03 09:23:53 -05002927static int
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002928jme_init_one(struct pci_dev *pdev,
2929 const struct pci_device_id *ent)
2930{
2931 int rc = 0, using_dac, i;
2932 struct net_device *netdev;
2933 struct jme_adapter *jme;
2934 u16 bmcr, bmsr;
2935 u32 apmc;
2936
2937 /*
2938 * set up PCI device basics
2939 */
Kevin Baradonaac94532012-10-10 10:50:49 +00002940 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
2941 PCIE_LINK_STATE_CLKPM);
2942
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002943 rc = pci_enable_device(pdev);
2944 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00002945 pr_err("Cannot enable PCI device\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002946 goto err_out;
2947 }
2948
2949 using_dac = jme_pci_dma64(pdev);
2950 if (using_dac < 0) {
Joe Perches49d70c42010-09-04 22:21:05 +00002951 pr_err("Cannot set PCI DMA Mask\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002952 rc = -EIO;
2953 goto err_out_disable_pdev;
2954 }
2955
2956 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
Joe Perches49d70c42010-09-04 22:21:05 +00002957 pr_err("No PCI resource region found\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002958 rc = -ENOMEM;
2959 goto err_out_disable_pdev;
2960 }
2961
2962 rc = pci_request_regions(pdev, DRV_NAME);
2963 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00002964 pr_err("Cannot obtain PCI resource region\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002965 goto err_out_disable_pdev;
2966 }
2967
2968 pci_set_master(pdev);
2969
2970 /*
2971 * alloc and init net device
2972 */
2973 netdev = alloc_etherdev(sizeof(*jme));
2974 if (!netdev) {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002975 rc = -ENOMEM;
2976 goto err_out_release_regions;
2977 }
Stephen Hemmingere48714b2008-11-21 17:28:33 -08002978 netdev->netdev_ops = &jme_netdev_ops;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002979 netdev->ethtool_ops = &jme_ethtool_ops;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002980 netdev->watchdog_timeo = TX_TIMEOUT;
Michał Mirosławd7b57652011-03-31 01:01:35 +00002981 netdev->hw_features = NETIF_F_IP_CSUM |
2982 NETIF_F_IPV6_CSUM |
2983 NETIF_F_SG |
2984 NETIF_F_TSO |
2985 NETIF_F_TSO6 |
2986 NETIF_F_RXCSUM;
Michał Mirosław79032642010-11-30 06:38:00 +00002987 netdev->features = NETIF_F_IP_CSUM |
2988 NETIF_F_IPV6_CSUM |
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002989 NETIF_F_SG |
2990 NETIF_F_TSO |
2991 NETIF_F_TSO6 |
Patrick McHardyf6469682013-04-19 02:04:27 +00002992 NETIF_F_HW_VLAN_CTAG_TX |
2993 NETIF_F_HW_VLAN_CTAG_RX;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08002994 if (using_dac)
2995 netdev->features |= NETIF_F_HIGHDMA;
2996
Jarod Wilsond894be52016-10-20 13:55:16 -04002997 /* MTU range: 1280 - 9202*/
2998 netdev->min_mtu = IPV6_MIN_MTU;
2999 netdev->max_mtu = MAX_ETHERNET_JUMBO_PACKET_SIZE - ETH_HLEN;
3000
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003001 SET_NETDEV_DEV(netdev, &pdev->dev);
3002 pci_set_drvdata(pdev, netdev);
3003
3004 /*
3005 * init adapter info
3006 */
3007 jme = netdev_priv(netdev);
3008 jme->pdev = pdev;
3009 jme->dev = netdev;
3010 jme->jme_rx = netif_rx;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003011 jme->old_mtu = netdev->mtu = 1500;
3012 jme->phylink = 0;
3013 jme->tx_ring_size = 1 << 10;
3014 jme->tx_ring_mask = jme->tx_ring_size - 1;
3015 jme->tx_wake_threshold = 1 << 9;
3016 jme->rx_ring_size = 1 << 9;
3017 jme->rx_ring_mask = jme->rx_ring_size - 1;
3018 jme->msg_enable = JME_DEF_MSG_ENABLE;
3019 jme->regs = ioremap(pci_resource_start(pdev, 0),
3020 pci_resource_len(pdev, 0));
3021 if (!(jme->regs)) {
Joe Perches49d70c42010-09-04 22:21:05 +00003022 pr_err("Mapping PCI resource region error\n");
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003023 rc = -ENOMEM;
3024 goto err_out_free_netdev;
3025 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003026
3027 if (no_pseudohp) {
3028 apmc = jread32(jme, JME_APMC) & ~JME_APMC_PSEUDO_HP_EN;
3029 jwrite32(jme, JME_APMC, apmc);
3030 } else if (force_pseudohp) {
3031 apmc = jread32(jme, JME_APMC) | JME_APMC_PSEUDO_HP_EN;
3032 jwrite32(jme, JME_APMC, apmc);
3033 }
3034
Michal Schmidt449cfcc2013-08-23 15:40:53 +02003035 NETIF_NAPI_SET(netdev, &jme->napi, jme_poll, NAPI_POLL_WEIGHT)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003036
3037 spin_lock_init(&jme->phy_lock);
3038 spin_lock_init(&jme->macaddr_lock);
3039 spin_lock_init(&jme->rxmcs_lock);
3040
3041 atomic_set(&jme->link_changing, 1);
3042 atomic_set(&jme->rx_cleaning, 1);
3043 atomic_set(&jme->tx_cleaning, 1);
3044 atomic_set(&jme->rx_empty, 1);
3045
3046 tasklet_init(&jme->pcc_task,
Joe Perches164165d2009-11-19 09:30:10 +00003047 jme_pcc_tasklet,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003048 (unsigned long) jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003049 jme->dpi.cur = PCC_P1;
3050
3051 jme->reg_ghc = 0;
3052 jme->reg_rxcs = RXCS_DEFAULT;
3053 jme->reg_rxmcs = RXMCS_DEFAULT;
3054 jme->reg_txpfc = 0;
3055 jme->reg_pmcs = PMCS_MFEN;
Guo-Fu Tseng854a2e72011-02-13 18:27:39 +00003056 jme->reg_gpreg1 = GPREG1_DEFAULT;
Michał Mirosławd7b57652011-03-31 01:01:35 +00003057
3058 if (jme->reg_rxmcs & RXMCS_CHECKSUM)
3059 netdev->features |= NETIF_F_RXCSUM;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003060
3061 /*
3062 * Get Max Read Req Size from PCI Config Space
3063 */
3064 pci_read_config_byte(pdev, PCI_DCSR_MRRS, &jme->mrrs);
3065 jme->mrrs &= PCI_DCSR_MRRS_MASK;
3066 switch (jme->mrrs) {
3067 case MRRS_128B:
3068 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_128B;
3069 break;
3070 case MRRS_256B:
3071 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_256B;
3072 break;
3073 default:
3074 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_512B;
3075 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003076 }
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003077
3078 /*
3079 * Must check before reset_mac_processor
3080 */
3081 jme_check_hw_ver(jme);
3082 jme->mii_if.dev = netdev;
3083 if (jme->fpgaver) {
3084 jme->mii_if.phy_id = 0;
3085 for (i = 1 ; i < 32 ; ++i) {
3086 bmcr = jme_mdio_read(netdev, i, MII_BMCR);
3087 bmsr = jme_mdio_read(netdev, i, MII_BMSR);
3088 if (bmcr != 0xFFFFU && (bmcr != 0 || bmsr != 0)) {
3089 jme->mii_if.phy_id = i;
3090 break;
3091 }
3092 }
3093
3094 if (!jme->mii_if.phy_id) {
3095 rc = -EIO;
Joe Perches49d70c42010-09-04 22:21:05 +00003096 pr_err("Can not find phy_id\n");
3097 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003098 }
3099
3100 jme->reg_ghc |= GHC_LINK_POLL;
3101 } else {
3102 jme->mii_if.phy_id = 1;
3103 }
3104 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
3105 jme->mii_if.supports_gmii = true;
3106 else
3107 jme->mii_if.supports_gmii = false;
Guo-Fu Tseng334fbbb2010-10-18 14:10:43 +00003108 jme->mii_if.phy_id_mask = 0x1F;
3109 jme->mii_if.reg_num_mask = 0x1F;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003110 jme->mii_if.mdio_read = jme_mdio_read;
3111 jme->mii_if.mdio_write = jme_mdio_write;
3112
Guo-Fu Tseng0772a992016-03-05 08:11:55 +08003113 jme_clear_pm_disable_wol(jme);
Guo-Fu Tseng81422e62016-03-05 08:11:56 +08003114 device_init_wakeup(&pdev->dev, true);
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003115
Guo-Fu Tseng51754572011-02-13 18:27:37 +00003116 jme_set_phyfifo_5level(jme);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08003117 jme->pcirev = pdev->revision;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003118 if (!jme->fpgaver)
3119 jme_phy_init(jme);
3120 jme_phy_off(jme);
3121
3122 /*
3123 * Reset MAC processor and reload EEPROM for MAC Address
3124 */
3125 jme_reset_mac_processor(jme);
3126 rc = jme_reload_eeprom(jme);
3127 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00003128 pr_err("Reload eeprom for reading MAC Address error\n");
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00003129 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003130 }
3131 jme_load_macaddr(netdev);
3132
3133 /*
3134 * Tell stack that we are not ready to work until open()
3135 */
3136 netif_carrier_off(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003137
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003138 rc = register_netdev(netdev);
3139 if (rc) {
Joe Perches49d70c42010-09-04 22:21:05 +00003140 pr_err("Cannot register net device\n");
Guo-Fu Tsengd1dfa1d2009-07-06 04:40:38 +00003141 goto err_out_unmap;
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003142 }
3143
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00003144 netif_info(jme, probe, jme->dev, "%s%s chiprev:%x pcirev:%x macaddr:%pM\n",
Joe Perchesf8502ce2010-02-09 11:49:51 +00003145 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250) ?
3146 "JMC250 Gigabit Ethernet" :
3147 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC260) ?
3148 "JMC260 Fast Ethernet" : "Unknown",
3149 (jme->fpgaver != 0) ? " (FPGA)" : "",
3150 (jme->fpgaver != 0) ? jme->fpgaver : jme->chiprev,
Guo-Fu Tseng19d96012011-02-13 18:27:34 +00003151 jme->pcirev, netdev->dev_addr);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003152
3153 return 0;
3154
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003155err_out_unmap:
3156 iounmap(jme->regs);
3157err_out_free_netdev:
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003158 free_netdev(netdev);
3159err_out_release_regions:
3160 pci_release_regions(pdev);
3161err_out_disable_pdev:
3162 pci_disable_device(pdev);
3163err_out:
3164 return rc;
3165}
3166
Bill Pembertonaf375572012-12-03 09:23:53 -05003167static void
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003168jme_remove_one(struct pci_dev *pdev)
3169{
3170 struct net_device *netdev = pci_get_drvdata(pdev);
3171 struct jme_adapter *jme = netdev_priv(netdev);
3172
3173 unregister_netdev(netdev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003174 iounmap(jme->regs);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003175 free_netdev(netdev);
3176 pci_release_regions(pdev);
3177 pci_disable_device(pdev);
3178
3179}
3180
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003181static void
3182jme_shutdown(struct pci_dev *pdev)
3183{
3184 struct net_device *netdev = pci_get_drvdata(pdev);
3185 struct jme_adapter *jme = netdev_priv(netdev);
3186
3187 jme_powersave_phy(jme);
3188 pci_pme_active(pdev, true);
3189}
3190
Guo-Fu Tsengaab6fb82011-06-27 19:23:34 +00003191#ifdef CONFIG_PM_SLEEP
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003192static int
3193jme_suspend(struct device *dev)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003194{
Chuhong Yuanc9b6c562019-07-23 22:16:42 +08003195 struct net_device *netdev = dev_get_drvdata(dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003196 struct jme_adapter *jme = netdev_priv(netdev);
3197
David S. Miller1805b2f2011-10-24 18:18:09 -04003198 if (!netif_running(netdev))
3199 return 0;
3200
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003201 atomic_dec(&jme->link_changing);
3202
3203 netif_device_detach(netdev);
3204 netif_stop_queue(netdev);
3205 jme_stop_irq(jme);
3206
3207 tasklet_disable(&jme->txclean_task);
3208 tasklet_disable(&jme->rxclean_task);
3209 tasklet_disable(&jme->rxempty_task);
3210
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003211 if (netif_carrier_ok(netdev)) {
3212 if (test_bit(JME_FLAG_POLL, &jme->flags))
3213 jme_polling_mode(jme);
3214
3215 jme_stop_pcc_timer(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003216 jme_disable_rx_engine(jme);
3217 jme_disable_tx_engine(jme);
3218 jme_reset_mac_processor(jme);
3219 jme_free_rx_resources(jme);
3220 jme_free_tx_resources(jme);
3221 netif_carrier_off(netdev);
3222 jme->phylink = 0;
3223 }
3224
3225 tasklet_enable(&jme->txclean_task);
Quentin Lambert06f66522014-12-12 13:35:52 +01003226 tasklet_enable(&jme->rxclean_task);
3227 tasklet_enable(&jme->rxempty_task);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003228
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003229 jme_powersave_phy(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003230
3231 return 0;
3232}
3233
Guo-Fu Tsengbc057e02011-06-28 01:38:30 +00003234static int
3235jme_resume(struct device *dev)
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003236{
Chuhong Yuanc9b6c562019-07-23 22:16:42 +08003237 struct net_device *netdev = dev_get_drvdata(dev);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003238 struct jme_adapter *jme = netdev_priv(netdev);
3239
David S. Miller1805b2f2011-10-24 18:18:09 -04003240 if (!netif_running(netdev))
3241 return 0;
3242
Guo-Fu Tseng0772a992016-03-05 08:11:55 +08003243 jme_clear_pm_disable_wol(jme);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00003244 jme_phy_on(jme);
3245 if (test_bit(JME_FLAG_SSET, &jme->flags))
Philippe Reynesc5238382017-01-15 23:18:21 +01003246 jme_set_link_ksettings(netdev, &jme->old_cmd);
Guo-Fu Tseng4872b112011-02-13 18:27:35 +00003247 else
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003248 jme_reset_phy_processor(jme);
Aries Leec4860ba2011-11-21 10:20:42 +00003249 jme_phy_calibration(jme);
3250 jme_phy_setEA(jme);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003251 netif_device_attach(netdev);
3252
3253 atomic_inc(&jme->link_changing);
3254
3255 jme_reset_link(jme);
3256
Diego Violaee50c132016-02-23 12:04:04 -03003257 jme_start_irq(jme);
3258
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003259 return 0;
3260}
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003261
3262static SIMPLE_DEV_PM_OPS(jme_pm_ops, jme_suspend, jme_resume);
3263#define JME_PM_OPS (&jme_pm_ops)
3264
3265#else
3266
3267#define JME_PM_OPS NULL
David S. Miller724f8802008-10-08 19:54:31 -07003268#endif
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003269
Benoit Taine9baa3c32014-08-08 15:56:03 +02003270static const struct pci_device_id jme_pci_tbl[] = {
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003271 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC250) },
3272 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC260) },
3273 { }
3274};
3275
3276static struct pci_driver jme_driver = {
3277 .name = DRV_NAME,
3278 .id_table = jme_pci_tbl,
3279 .probe = jme_init_one,
Bill Pembertonaf375572012-12-03 09:23:53 -05003280 .remove = jme_remove_one,
Guo-Fu Tseng1c557812010-10-24 16:18:25 -07003281 .shutdown = jme_shutdown,
Rafael J. Wysockif4e5bd42011-03-26 01:34:06 +00003282 .driver.pm = JME_PM_OPS,
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003283};
3284
3285static int __init
3286jme_init_module(void)
3287{
Joe Perches49d70c42010-09-04 22:21:05 +00003288 pr_info("JMicron JMC2XX ethernet driver version %s\n", DRV_VERSION);
Guo-Fu Tseng95252232008-09-16 01:00:11 +08003289 return pci_register_driver(&jme_driver);
3290}
3291
3292static void __exit
3293jme_cleanup_module(void)
3294{
3295 pci_unregister_driver(&jme_driver);
3296}
3297
3298module_init(jme_init_module);
3299module_exit(jme_cleanup_module);
3300
3301MODULE_AUTHOR("Guo-Fu Tseng <cooldavid@cooldavid.org>");
3302MODULE_DESCRIPTION("JMicron JMC2x0 PCI Express Ethernet driver");
3303MODULE_LICENSE("GPL");
3304MODULE_VERSION(DRV_VERSION);
3305MODULE_DEVICE_TABLE(pci, jme_pci_tbl);