1 // SPDX-License-Identifier: GPL-2.0-only
3 * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
5 * Copyright 2008 JMicron Technology Corporation
6 * https://www.jmicron.com/
7 * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
9 * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/mii.h>
21 #include <linux/crc32.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
26 #include <linux/ipv6.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/if_vlan.h>
30 #include <linux/slab.h>
31 #include <linux/jiffies.h>
32 #include <net/ip6_checksum.h>
35 static int force_pseudohp = -1;
36 static int no_pseudohp = -1;
37 static int no_extplug = -1;
38 module_param(force_pseudohp, int, 0);
39 MODULE_PARM_DESC(force_pseudohp,
40 "Enable pseudo hot-plug feature manually by driver instead of BIOS.");
41 module_param(no_pseudohp, int, 0);
42 MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
43 module_param(no_extplug, int, 0);
44 MODULE_PARM_DESC(no_extplug,
45 "Do not use external plug signal for pseudo hot-plug.");
48 jme_mdio_read(struct net_device *netdev, int phy, int reg)
50 struct jme_adapter *jme = netdev_priv(netdev);
51 int i, val, again = (reg == MII_BMSR) ? 1 : 0;
54 jwrite32(jme, JME_SMI, SMI_OP_REQ |
59 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
61 val = jread32(jme, JME_SMI);
62 if ((val & SMI_OP_REQ) == 0)
67 pr_err("phy(%d) read timeout : %d\n", phy, reg);
74 return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
78 jme_mdio_write(struct net_device *netdev,
79 int phy, int reg, int val)
81 struct jme_adapter *jme = netdev_priv(netdev);
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));
89 for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
91 if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
96 pr_err("phy(%d) write timeout : %d\n", phy, reg);
100 jme_reset_phy_processor(struct jme_adapter *jme)
104 jme_mdio_write(jme->dev,
106 MII_ADVERTISE, ADVERTISE_ALL |
107 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
109 if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
110 jme_mdio_write(jme->dev,
113 ADVERTISE_1000FULL | ADVERTISE_1000HALF);
115 val = jme_mdio_read(jme->dev,
119 jme_mdio_write(jme->dev,
121 MII_BMCR, val | BMCR_RESET);
125 jme_setup_wakeup_frame(struct jme_adapter *jme,
126 const u32 *mask, u32 crc, int fnr)
133 jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
135 jwrite32(jme, JME_WFODP, crc);
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));
146 jwrite32(jme, JME_WFODP, mask[i]);
152 jme_mac_rxclk_off(struct jme_adapter *jme)
154 jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
155 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
159 jme_mac_rxclk_on(struct jme_adapter *jme)
161 jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
162 jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
166 jme_mac_txclk_off(struct jme_adapter *jme)
168 jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
169 jwrite32f(jme, JME_GHC, jme->reg_ghc);
173 jme_mac_txclk_on(struct jme_adapter *jme)
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;
179 jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
180 jwrite32f(jme, JME_GHC, jme->reg_ghc);
184 jme_reset_ghc_speed(struct jme_adapter *jme)
186 jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
187 jwrite32f(jme, JME_GHC, jme->reg_ghc);
191 jme_reset_250A2_workaround(struct jme_adapter *jme)
193 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
195 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
199 jme_assert_ghc_reset(struct jme_adapter *jme)
201 jme->reg_ghc |= GHC_SWRST;
202 jwrite32f(jme, JME_GHC, jme->reg_ghc);
206 jme_clear_ghc_reset(struct jme_adapter *jme)
208 jme->reg_ghc &= ~GHC_SWRST;
209 jwrite32f(jme, JME_GHC, jme->reg_ghc);
213 jme_reset_mac_processor(struct jme_adapter *jme)
215 static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
216 u32 crc = 0xCDCDCDCD;
220 jme_reset_ghc_speed(jme);
221 jme_reset_250A2_workaround(jme);
223 jme_mac_rxclk_on(jme);
224 jme_mac_txclk_on(jme);
226 jme_assert_ghc_reset(jme);
228 jme_mac_rxclk_off(jme);
229 jme_mac_txclk_off(jme);
231 jme_clear_ghc_reset(jme);
233 jme_mac_rxclk_on(jme);
234 jme_mac_txclk_on(jme);
236 jme_mac_rxclk_off(jme);
237 jme_mac_txclk_off(jme);
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);
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);
253 gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
255 gpreg0 = GPREG0_DEFAULT;
256 jwrite32(jme, JME_GPREG0, gpreg0);
260 jme_clear_pm_enable_wol(struct jme_adapter *jme)
262 jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
266 jme_clear_pm_disable_wol(struct jme_adapter *jme)
268 jwrite32(jme, JME_PMCS, PMCS_STMASK);
272 jme_reload_eeprom(struct jme_adapter *jme)
277 val = jread32(jme, JME_SMBCSR);
279 if (val & SMBCSR_EEPROMD) {
281 jwrite32(jme, JME_SMBCSR, val);
282 val |= SMBCSR_RELOAD;
283 jwrite32(jme, JME_SMBCSR, val);
286 for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
288 if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
293 pr_err("eeprom reload timeout\n");
302 jme_load_macaddr(struct net_device *netdev)
304 struct jme_adapter *jme = netdev_priv(netdev);
305 unsigned char macaddr[ETH_ALEN];
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;
317 eth_hw_addr_set(netdev, macaddr);
318 spin_unlock_bh(&jme->macaddr_lock);
322 jme_set_rx_pcc(struct jme_adapter *jme, int p)
326 jwrite32(jme, JME_PCCRX0,
327 ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
328 ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
331 jwrite32(jme, JME_PCCRX0,
332 ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
333 ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
336 jwrite32(jme, JME_PCCRX0,
337 ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
338 ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
341 jwrite32(jme, JME_PCCRX0,
342 ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
343 ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
350 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
351 netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
355 jme_start_irq(struct jme_adapter *jme)
357 register struct dynpcc_info *dpi = &(jme->dpi);
359 jme_set_rx_pcc(jme, PCC_P1);
361 dpi->attempt = PCC_P1;
364 jwrite32(jme, JME_PCCTX,
365 ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
366 ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
373 jwrite32(jme, JME_IENS, INTR_ENABLE);
377 jme_stop_irq(struct jme_adapter *jme)
382 jwrite32f(jme, JME_IENC, INTR_ENABLE);
386 jme_linkstat_from_phy(struct jme_adapter *jme)
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;
399 jme_set_phyfifo_5level(struct jme_adapter *jme)
401 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
405 jme_set_phyfifo_8level(struct jme_adapter *jme)
407 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
411 jme_check_link(struct net_device *netdev, int testonly)
413 struct jme_adapter *jme = netdev_priv(netdev);
414 u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
421 phylink = jme_linkstat_from_phy(jme);
423 phylink = jread32(jme, JME_PHY_LINK);
425 if (phylink & PHY_LINK_UP) {
426 if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
428 * If we did not enable AN
429 * Speed/Duplex Info should be obtained from SMI
431 phylink = PHY_LINK_UP;
433 bmcr = jme_mdio_read(jme->dev,
437 phylink |= ((bmcr & BMCR_SPEED1000) &&
438 (bmcr & BMCR_SPEED100) == 0) ?
439 PHY_LINK_SPEED_1000M :
440 (bmcr & BMCR_SPEED100) ?
441 PHY_LINK_SPEED_100M :
444 phylink |= (bmcr & BMCR_FULLDPLX) ?
447 strcat(linkmsg, "Forced: ");
450 * Keep polling for speed/duplex resolve complete
452 while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
458 phylink = jme_linkstat_from_phy(jme);
460 phylink = jread32(jme, JME_PHY_LINK);
463 pr_err("Waiting speed resolve timeout\n");
465 strcat(linkmsg, "ANed: ");
468 if (jme->phylink == phylink) {
475 jme->phylink = phylink;
478 * The speed/duplex setting of jme->reg_ghc already cleared
479 * by jme_reset_mac_processor()
481 switch (phylink & PHY_LINK_SPEED_MASK) {
482 case PHY_LINK_SPEED_10M:
483 jme->reg_ghc |= GHC_SPEED_10M;
484 strcat(linkmsg, "10 Mbps, ");
486 case PHY_LINK_SPEED_100M:
487 jme->reg_ghc |= GHC_SPEED_100M;
488 strcat(linkmsg, "100 Mbps, ");
490 case PHY_LINK_SPEED_1000M:
491 jme->reg_ghc |= GHC_SPEED_1000M;
492 strcat(linkmsg, "1000 Mbps, ");
498 if (phylink & PHY_LINK_DUPLEX) {
499 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
500 jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
501 jme->reg_ghc |= GHC_DPX;
503 jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
507 jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
510 jwrite32(jme, JME_GHC, jme->reg_ghc);
512 if (is_buggy250(jme->pdev->device, jme->chiprev)) {
513 jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
515 if (!(phylink & PHY_LINK_DUPLEX))
516 jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
517 switch (phylink & PHY_LINK_SPEED_MASK) {
518 case PHY_LINK_SPEED_10M:
519 jme_set_phyfifo_8level(jme);
520 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
522 case PHY_LINK_SPEED_100M:
523 jme_set_phyfifo_5level(jme);
524 jme->reg_gpreg1 |= GPREG1_RSSPATCH;
526 case PHY_LINK_SPEED_1000M:
527 jme_set_phyfifo_8level(jme);
533 jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
535 strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
538 strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
541 netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
542 netif_carrier_on(netdev);
547 netif_info(jme, link, jme->dev, "Link is down\n");
549 netif_carrier_off(netdev);
557 jme_setup_tx_resources(struct jme_adapter *jme)
559 struct jme_ring *txring = &(jme->txring[0]);
561 txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
562 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
572 txring->desc = (void *)ALIGN((unsigned long)(txring->alloc),
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);
579 txring->bufinf = kcalloc(jme->tx_ring_size,
580 sizeof(struct jme_buffer_info),
582 if (unlikely(!(txring->bufinf)))
583 goto err_free_txring;
588 dma_free_coherent(&(jme->pdev->dev),
589 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
595 txring->dmaalloc = 0;
597 txring->bufinf = NULL;
603 jme_free_tx_resources(struct jme_adapter *jme)
606 struct jme_ring *txring = &(jme->txring[0]);
607 struct jme_buffer_info *txbi;
610 if (txring->bufinf) {
611 for (i = 0 ; i < jme->tx_ring_size ; ++i) {
612 txbi = txring->bufinf + i;
614 dev_kfree_skb(txbi->skb);
620 txbi->start_xmit = 0;
622 kfree(txring->bufinf);
625 dma_free_coherent(&(jme->pdev->dev),
626 TX_RING_ALLOC_SIZE(jme->tx_ring_size),
630 txring->alloc = NULL;
632 txring->dmaalloc = 0;
634 txring->bufinf = NULL;
636 txring->next_to_use = 0;
637 atomic_set(&txring->next_to_clean, 0);
638 atomic_set(&txring->nr_free, 0);
642 jme_enable_tx_engine(struct jme_adapter *jme)
647 jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
651 * Setup TX Queue 0 DMA Bass Address
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);
658 * Setup TX Descptor Count
660 jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
666 jwrite32f(jme, JME_TXCS, jme->reg_txcs |
671 * Start clock for TX MAC Processor
673 jme_mac_txclk_on(jme);
677 jme_disable_tx_engine(struct jme_adapter *jme)
685 jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
688 val = jread32(jme, JME_TXCS);
689 for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
691 val = jread32(jme, JME_TXCS);
696 pr_err("Disable TX engine timeout\n");
699 * Stop clock for TX MAC Processor
701 jme_mac_txclk_off(jme);
705 jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
707 struct jme_ring *rxring = &(jme->rxring[0]);
708 register struct rxdesc *rxdesc = rxring->desc;
709 struct jme_buffer_info *rxbi = rxring->bufinf;
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;
722 rxdesc->desc1.flags |= RXFLAG_OWN | RXFLAG_INT;
726 jme_make_new_rx_buf(struct jme_adapter *jme, int i)
728 struct jme_ring *rxring = &(jme->rxring[0]);
729 struct jme_buffer_info *rxbi = rxring->bufinf + i;
733 skb = netdev_alloc_skb(jme->dev,
734 jme->dev->mtu + RX_EXTRA_LEN);
738 mapping = dma_map_page(&jme->pdev->dev, virt_to_page(skb->data),
739 offset_in_page(skb->data), skb_tailroom(skb),
741 if (unlikely(dma_mapping_error(&jme->pdev->dev, mapping))) {
746 if (likely(rxbi->mapping))
747 dma_unmap_page(&jme->pdev->dev, rxbi->mapping, rxbi->len,
751 rxbi->len = skb_tailroom(skb);
752 rxbi->mapping = mapping;
757 jme_free_rx_buf(struct jme_adapter *jme, int i)
759 struct jme_ring *rxring = &(jme->rxring[0]);
760 struct jme_buffer_info *rxbi = rxring->bufinf;
764 dma_unmap_page(&jme->pdev->dev, rxbi->mapping, rxbi->len,
766 dev_kfree_skb(rxbi->skb);
774 jme_free_rx_resources(struct jme_adapter *jme)
777 struct jme_ring *rxring = &(jme->rxring[0]);
780 if (rxring->bufinf) {
781 for (i = 0 ; i < jme->rx_ring_size ; ++i)
782 jme_free_rx_buf(jme, i);
783 kfree(rxring->bufinf);
786 dma_free_coherent(&(jme->pdev->dev),
787 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
790 rxring->alloc = NULL;
792 rxring->dmaalloc = 0;
794 rxring->bufinf = NULL;
796 rxring->next_to_use = 0;
797 atomic_set(&rxring->next_to_clean, 0);
801 jme_setup_rx_resources(struct jme_adapter *jme)
804 struct jme_ring *rxring = &(jme->rxring[0]);
806 rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
807 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
816 rxring->desc = (void *)ALIGN((unsigned long)(rxring->alloc),
818 rxring->dma = ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
819 rxring->next_to_use = 0;
820 atomic_set(&rxring->next_to_clean, 0);
822 rxring->bufinf = kcalloc(jme->rx_ring_size,
823 sizeof(struct jme_buffer_info),
825 if (unlikely(!(rxring->bufinf)))
826 goto err_free_rxring;
829 * Initiallize Receive Descriptors
831 for (i = 0 ; i < jme->rx_ring_size ; ++i) {
832 if (unlikely(jme_make_new_rx_buf(jme, i))) {
833 jme_free_rx_resources(jme);
837 jme_set_clean_rxdesc(jme, i);
843 dma_free_coherent(&(jme->pdev->dev),
844 RX_RING_ALLOC_SIZE(jme->rx_ring_size),
849 rxring->dmaalloc = 0;
851 rxring->bufinf = NULL;
857 jme_enable_rx_engine(struct jme_adapter *jme)
862 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
867 * Setup RX DMA Bass Address
869 jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
870 jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
871 jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
874 * Setup RX Descriptor Count
876 jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
879 * Setup Unicast Filter
881 jme_set_unicastaddr(jme->dev);
882 jme_set_multi(jme->dev);
888 jwrite32f(jme, JME_RXCS, jme->reg_rxcs |
894 * Start clock for RX MAC Processor
896 jme_mac_rxclk_on(jme);
900 jme_restart_rx_engine(struct jme_adapter *jme)
905 jwrite32(jme, JME_RXCS, jme->reg_rxcs |
912 jme_disable_rx_engine(struct jme_adapter *jme)
920 jwrite32(jme, JME_RXCS, jme->reg_rxcs);
923 val = jread32(jme, JME_RXCS);
924 for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
926 val = jread32(jme, JME_RXCS);
931 pr_err("Disable RX engine timeout\n");
934 * Stop clock for RX MAC Processor
936 jme_mac_rxclk_off(jme);
940 jme_udpsum(struct sk_buff *skb)
944 if (skb->len < (ETH_HLEN + sizeof(struct iphdr)))
946 if (skb->protocol != htons(ETH_P_IP))
948 skb_set_network_header(skb, ETH_HLEN);
949 if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
950 (skb->len < (ETH_HLEN +
951 (ip_hdr(skb)->ihl << 2) +
952 sizeof(struct udphdr)))) {
953 skb_reset_network_header(skb);
956 skb_set_transport_header(skb,
957 ETH_HLEN + (ip_hdr(skb)->ihl << 2));
958 csum = udp_hdr(skb)->check;
959 skb_reset_transport_header(skb);
960 skb_reset_network_header(skb);
966 jme_rxsum_ok(struct jme_adapter *jme, u16 flags, struct sk_buff *skb)
968 if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
971 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
972 == RXWBFLAG_TCPON)) {
973 if (flags & RXWBFLAG_IPV4)
974 netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
978 if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
979 == RXWBFLAG_UDPON) && jme_udpsum(skb)) {
980 if (flags & RXWBFLAG_IPV4)
981 netif_err(jme, rx_err, jme->dev, "UDP Checksum error\n");
985 if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
987 netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error\n");
995 jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
997 struct jme_ring *rxring = &(jme->rxring[0]);
998 struct rxdesc *rxdesc = rxring->desc;
999 struct jme_buffer_info *rxbi = rxring->bufinf;
1000 struct sk_buff *skb;
1007 dma_sync_single_for_cpu(&jme->pdev->dev, rxbi->mapping, rxbi->len,
1010 if (unlikely(jme_make_new_rx_buf(jme, idx))) {
1011 dma_sync_single_for_device(&jme->pdev->dev, rxbi->mapping,
1012 rxbi->len, DMA_FROM_DEVICE);
1014 ++(NET_STAT(jme).rx_dropped);
1016 framesize = le16_to_cpu(rxdesc->descwb.framesize)
1019 skb_reserve(skb, RX_PREPAD_SIZE);
1020 skb_put(skb, framesize);
1021 skb->protocol = eth_type_trans(skb, jme->dev);
1023 if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags), skb))
1024 skb->ip_summed = CHECKSUM_UNNECESSARY;
1026 skb_checksum_none_assert(skb);
1028 if (rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_TAGON)) {
1029 u16 vid = le16_to_cpu(rxdesc->descwb.vlan);
1031 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1032 NET_STAT(jme).rx_bytes += 4;
1036 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_DEST)) ==
1037 cpu_to_le16(RXWBFLAG_DEST_MUL))
1038 ++(NET_STAT(jme).multicast);
1040 NET_STAT(jme).rx_bytes += framesize;
1041 ++(NET_STAT(jme).rx_packets);
1044 jme_set_clean_rxdesc(jme, idx);
1049 jme_process_receive(struct jme_adapter *jme, int limit)
1051 struct jme_ring *rxring = &(jme->rxring[0]);
1052 struct rxdesc *rxdesc;
1053 int i, j, ccnt, desccnt, mask = jme->rx_ring_mask;
1055 if (unlikely(!atomic_dec_and_test(&jme->rx_cleaning)))
1058 if (unlikely(atomic_read(&jme->link_changing) != 1))
1061 if (unlikely(!netif_carrier_ok(jme->dev)))
1064 i = atomic_read(&rxring->next_to_clean);
1066 rxdesc = rxring->desc;
1069 if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_OWN)) ||
1070 !(rxdesc->descwb.desccnt & RXWBDCNT_WBCPL))
1075 desccnt = rxdesc->descwb.desccnt & RXWBDCNT_DCNT;
1077 if (unlikely(desccnt > 1 ||
1078 rxdesc->descwb.errstat & RXWBERR_ALLERR)) {
1080 if (rxdesc->descwb.errstat & RXWBERR_CRCERR)
1081 ++(NET_STAT(jme).rx_crc_errors);
1082 else if (rxdesc->descwb.errstat & RXWBERR_OVERUN)
1083 ++(NET_STAT(jme).rx_fifo_errors);
1085 ++(NET_STAT(jme).rx_errors);
1088 limit -= desccnt - 1;
1090 for (j = i, ccnt = desccnt ; ccnt-- ; ) {
1091 jme_set_clean_rxdesc(jme, j);
1092 j = (j + 1) & (mask);
1096 jme_alloc_and_feed_skb(jme, i);
1099 i = (i + desccnt) & (mask);
1103 atomic_set(&rxring->next_to_clean, i);
1106 atomic_inc(&jme->rx_cleaning);
1108 return limit > 0 ? limit : 0;
1113 jme_attempt_pcc(struct dynpcc_info *dpi, int atmp)
1115 if (likely(atmp == dpi->cur)) {
1120 if (dpi->attempt == atmp) {
1123 dpi->attempt = atmp;
1130 jme_dynamic_pcc(struct jme_adapter *jme)
1132 register struct dynpcc_info *dpi = &(jme->dpi);
1134 if ((NET_STAT(jme).rx_bytes - dpi->last_bytes) > PCC_P3_THRESHOLD)
1135 jme_attempt_pcc(dpi, PCC_P3);
1136 else if ((NET_STAT(jme).rx_packets - dpi->last_pkts) > PCC_P2_THRESHOLD ||
1137 dpi->intr_cnt > PCC_INTR_THRESHOLD)
1138 jme_attempt_pcc(dpi, PCC_P2);
1140 jme_attempt_pcc(dpi, PCC_P1);
1142 if (unlikely(dpi->attempt != dpi->cur && dpi->cnt > 5)) {
1143 if (dpi->attempt < dpi->cur)
1144 tasklet_schedule(&jme->rxclean_task);
1145 jme_set_rx_pcc(jme, dpi->attempt);
1146 dpi->cur = dpi->attempt;
1152 jme_start_pcc_timer(struct jme_adapter *jme)
1154 struct dynpcc_info *dpi = &(jme->dpi);
1155 dpi->last_bytes = NET_STAT(jme).rx_bytes;
1156 dpi->last_pkts = NET_STAT(jme).rx_packets;
1158 jwrite32(jme, JME_TMCSR,
1159 TMCSR_EN | ((0xFFFFFF - PCC_INTERVAL_US) & TMCSR_CNT));
1163 jme_stop_pcc_timer(struct jme_adapter *jme)
1165 jwrite32(jme, JME_TMCSR, 0);
1169 jme_shutdown_nic(struct jme_adapter *jme)
1173 phylink = jme_linkstat_from_phy(jme);
1175 if (!(phylink & PHY_LINK_UP)) {
1177 * Disable all interrupt before issue timer
1180 jwrite32(jme, JME_TIMER2, TMCSR_EN | 0xFFFFFE);
1185 jme_pcc_tasklet(struct tasklet_struct *t)
1187 struct jme_adapter *jme = from_tasklet(jme, t, pcc_task);
1188 struct net_device *netdev = jme->dev;
1190 if (unlikely(test_bit(JME_FLAG_SHUTDOWN, &jme->flags))) {
1191 jme_shutdown_nic(jme);
1195 if (unlikely(!netif_carrier_ok(netdev) ||
1196 (atomic_read(&jme->link_changing) != 1)
1198 jme_stop_pcc_timer(jme);
1202 if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
1203 jme_dynamic_pcc(jme);
1205 jme_start_pcc_timer(jme);
1209 jme_polling_mode(struct jme_adapter *jme)
1211 jme_set_rx_pcc(jme, PCC_OFF);
1215 jme_interrupt_mode(struct jme_adapter *jme)
1217 jme_set_rx_pcc(jme, PCC_P1);
1221 jme_pseudo_hotplug_enabled(struct jme_adapter *jme)
1224 apmc = jread32(jme, JME_APMC);
1225 return apmc & JME_APMC_PSEUDO_HP_EN;
1229 jme_start_shutdown_timer(struct jme_adapter *jme)
1233 apmc = jread32(jme, JME_APMC) | JME_APMC_PCIE_SD_EN;
1234 apmc &= ~JME_APMC_EPIEN_CTRL;
1236 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_EN);
1239 jwrite32f(jme, JME_APMC, apmc);
1241 jwrite32f(jme, JME_TIMER2, 0);
1242 set_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1243 jwrite32(jme, JME_TMCSR,
1244 TMCSR_EN | ((0xFFFFFF - APMC_PHP_SHUTDOWN_DELAY) & TMCSR_CNT));
1248 jme_stop_shutdown_timer(struct jme_adapter *jme)
1252 jwrite32f(jme, JME_TMCSR, 0);
1253 jwrite32f(jme, JME_TIMER2, 0);
1254 clear_bit(JME_FLAG_SHUTDOWN, &jme->flags);
1256 apmc = jread32(jme, JME_APMC);
1257 apmc &= ~(JME_APMC_PCIE_SD_EN | JME_APMC_EPIEN_CTRL);
1258 jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_DIS);
1260 jwrite32f(jme, JME_APMC, apmc);
1263 static void jme_link_change_work(struct work_struct *work)
1265 struct jme_adapter *jme = container_of(work, struct jme_adapter, linkch_task);
1266 struct net_device *netdev = jme->dev;
1269 while (!atomic_dec_and_test(&jme->link_changing)) {
1270 atomic_inc(&jme->link_changing);
1271 netif_info(jme, intr, jme->dev, "Get link change lock failed\n");
1272 while (atomic_read(&jme->link_changing) != 1)
1273 netif_info(jme, intr, jme->dev, "Waiting link change lock\n");
1276 if (jme_check_link(netdev, 1) && jme->old_mtu == netdev->mtu)
1279 jme->old_mtu = netdev->mtu;
1280 netif_stop_queue(netdev);
1281 if (jme_pseudo_hotplug_enabled(jme))
1282 jme_stop_shutdown_timer(jme);
1284 jme_stop_pcc_timer(jme);
1285 tasklet_disable(&jme->txclean_task);
1286 tasklet_disable(&jme->rxclean_task);
1287 tasklet_disable(&jme->rxempty_task);
1289 if (netif_carrier_ok(netdev)) {
1290 jme_disable_rx_engine(jme);
1291 jme_disable_tx_engine(jme);
1292 jme_reset_mac_processor(jme);
1293 jme_free_rx_resources(jme);
1294 jme_free_tx_resources(jme);
1296 if (test_bit(JME_FLAG_POLL, &jme->flags))
1297 jme_polling_mode(jme);
1299 netif_carrier_off(netdev);
1302 jme_check_link(netdev, 0);
1303 if (netif_carrier_ok(netdev)) {
1304 rc = jme_setup_rx_resources(jme);
1306 pr_err("Allocating resources for RX error, Device STOPPED!\n");
1307 goto out_enable_tasklet;
1310 rc = jme_setup_tx_resources(jme);
1312 pr_err("Allocating resources for TX error, Device STOPPED!\n");
1313 goto err_out_free_rx_resources;
1316 jme_enable_rx_engine(jme);
1317 jme_enable_tx_engine(jme);
1319 netif_start_queue(netdev);
1321 if (test_bit(JME_FLAG_POLL, &jme->flags))
1322 jme_interrupt_mode(jme);
1324 jme_start_pcc_timer(jme);
1325 } else if (jme_pseudo_hotplug_enabled(jme)) {
1326 jme_start_shutdown_timer(jme);
1329 goto out_enable_tasklet;
1331 err_out_free_rx_resources:
1332 jme_free_rx_resources(jme);
1334 tasklet_enable(&jme->txclean_task);
1335 tasklet_enable(&jme->rxclean_task);
1336 tasklet_enable(&jme->rxempty_task);
1338 atomic_inc(&jme->link_changing);
1342 jme_rx_clean_tasklet(struct tasklet_struct *t)
1344 struct jme_adapter *jme = from_tasklet(jme, t, rxclean_task);
1345 struct dynpcc_info *dpi = &(jme->dpi);
1347 jme_process_receive(jme, jme->rx_ring_size);
1353 jme_poll(JME_NAPI_HOLDER(holder), JME_NAPI_WEIGHT(budget))
1355 struct jme_adapter *jme = jme_napi_priv(holder);
1358 rest = jme_process_receive(jme, JME_NAPI_WEIGHT_VAL(budget));
1360 while (atomic_read(&jme->rx_empty) > 0) {
1361 atomic_dec(&jme->rx_empty);
1362 ++(NET_STAT(jme).rx_dropped);
1363 jme_restart_rx_engine(jme);
1365 atomic_inc(&jme->rx_empty);
1368 JME_RX_COMPLETE(netdev, holder);
1369 jme_interrupt_mode(jme);
1372 JME_NAPI_WEIGHT_SET(budget, rest);
1373 return JME_NAPI_WEIGHT_VAL(budget) - rest;
1377 jme_rx_empty_tasklet(struct tasklet_struct *t)
1379 struct jme_adapter *jme = from_tasklet(jme, t, rxempty_task);
1381 if (unlikely(atomic_read(&jme->link_changing) != 1))
1384 if (unlikely(!netif_carrier_ok(jme->dev)))
1387 netif_info(jme, rx_status, jme->dev, "RX Queue Full!\n");
1389 jme_rx_clean_tasklet(&jme->rxclean_task);
1391 while (atomic_read(&jme->rx_empty) > 0) {
1392 atomic_dec(&jme->rx_empty);
1393 ++(NET_STAT(jme).rx_dropped);
1394 jme_restart_rx_engine(jme);
1396 atomic_inc(&jme->rx_empty);
1400 jme_wake_queue_if_stopped(struct jme_adapter *jme)
1402 struct jme_ring *txring = &(jme->txring[0]);
1405 if (unlikely(netif_queue_stopped(jme->dev) &&
1406 atomic_read(&txring->nr_free) >= (jme->tx_wake_threshold))) {
1407 netif_info(jme, tx_done, jme->dev, "TX Queue Waked\n");
1408 netif_wake_queue(jme->dev);
1413 static void jme_tx_clean_tasklet(struct tasklet_struct *t)
1415 struct jme_adapter *jme = from_tasklet(jme, t, txclean_task);
1416 struct jme_ring *txring = &(jme->txring[0]);
1417 struct txdesc *txdesc = txring->desc;
1418 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi, *ttxbi;
1419 int i, j, cnt = 0, max, err, mask;
1421 tx_dbg(jme, "Into txclean\n");
1423 if (unlikely(!atomic_dec_and_test(&jme->tx_cleaning)))
1426 if (unlikely(atomic_read(&jme->link_changing) != 1))
1429 if (unlikely(!netif_carrier_ok(jme->dev)))
1432 max = jme->tx_ring_size - atomic_read(&txring->nr_free);
1433 mask = jme->tx_ring_mask;
1435 for (i = atomic_read(&txring->next_to_clean) ; cnt < max ; ) {
1439 if (likely(ctxbi->skb &&
1440 !(txdesc[i].descwb.flags & TXWBFLAG_OWN))) {
1442 tx_dbg(jme, "txclean: %d+%d@%lu\n",
1443 i, ctxbi->nr_desc, jiffies);
1445 err = txdesc[i].descwb.flags & TXWBFLAG_ALLERR;
1447 for (j = 1 ; j < ctxbi->nr_desc ; ++j) {
1448 ttxbi = txbi + ((i + j) & (mask));
1449 txdesc[(i + j) & (mask)].dw[0] = 0;
1451 dma_unmap_page(&jme->pdev->dev,
1452 ttxbi->mapping, ttxbi->len,
1459 dev_kfree_skb(ctxbi->skb);
1461 cnt += ctxbi->nr_desc;
1463 if (unlikely(err)) {
1464 ++(NET_STAT(jme).tx_carrier_errors);
1466 ++(NET_STAT(jme).tx_packets);
1467 NET_STAT(jme).tx_bytes += ctxbi->len;
1472 ctxbi->start_xmit = 0;
1478 i = (i + ctxbi->nr_desc) & mask;
1483 tx_dbg(jme, "txclean: done %d@%lu\n", i, jiffies);
1484 atomic_set(&txring->next_to_clean, i);
1485 atomic_add(cnt, &txring->nr_free);
1487 jme_wake_queue_if_stopped(jme);
1490 atomic_inc(&jme->tx_cleaning);
1494 jme_intr_msi(struct jme_adapter *jme, u32 intrstat)
1499 jwrite32f(jme, JME_IENC, INTR_ENABLE);
1501 if (intrstat & (INTR_LINKCH | INTR_SWINTR)) {
1503 * Link change event is critical
1504 * all other events are ignored
1506 jwrite32(jme, JME_IEVE, intrstat);
1507 schedule_work(&jme->linkch_task);
1511 if (intrstat & INTR_TMINTR) {
1512 jwrite32(jme, JME_IEVE, INTR_TMINTR);
1513 tasklet_schedule(&jme->pcc_task);
1516 if (intrstat & (INTR_PCCTXTO | INTR_PCCTX)) {
1517 jwrite32(jme, JME_IEVE, INTR_PCCTXTO | INTR_PCCTX | INTR_TX0);
1518 tasklet_schedule(&jme->txclean_task);
1521 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1522 jwrite32(jme, JME_IEVE, (intrstat & (INTR_PCCRX0TO |
1528 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
1529 if (intrstat & INTR_RX0EMP)
1530 atomic_inc(&jme->rx_empty);
1532 if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
1533 if (likely(JME_RX_SCHEDULE_PREP(jme))) {
1534 jme_polling_mode(jme);
1535 JME_RX_SCHEDULE(jme);
1539 if (intrstat & INTR_RX0EMP) {
1540 atomic_inc(&jme->rx_empty);
1541 tasklet_hi_schedule(&jme->rxempty_task);
1542 } else if (intrstat & (INTR_PCCRX0TO | INTR_PCCRX0)) {
1543 tasklet_hi_schedule(&jme->rxclean_task);
1549 * Re-enable interrupt
1551 jwrite32f(jme, JME_IENS, INTR_ENABLE);
1555 jme_intr(int irq, void *dev_id)
1557 struct net_device *netdev = dev_id;
1558 struct jme_adapter *jme = netdev_priv(netdev);
1561 intrstat = jread32(jme, JME_IEVE);
1564 * Check if it's really an interrupt for us
1566 if (unlikely((intrstat & INTR_ENABLE) == 0))
1570 * Check if the device still exist
1572 if (unlikely(intrstat == ~((typeof(intrstat))0)))
1575 jme_intr_msi(jme, intrstat);
1581 jme_msi(int irq, void *dev_id)
1583 struct net_device *netdev = dev_id;
1584 struct jme_adapter *jme = netdev_priv(netdev);
1587 intrstat = jread32(jme, JME_IEVE);
1589 jme_intr_msi(jme, intrstat);
1595 jme_reset_link(struct jme_adapter *jme)
1597 jwrite32(jme, JME_TMCSR, TMCSR_SWIT);
1601 jme_restart_an(struct jme_adapter *jme)
1605 spin_lock_bh(&jme->phy_lock);
1606 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1607 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
1608 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1609 spin_unlock_bh(&jme->phy_lock);
1613 jme_request_irq(struct jme_adapter *jme)
1616 struct net_device *netdev = jme->dev;
1617 irq_handler_t handler = jme_intr;
1618 int irq_flags = IRQF_SHARED;
1620 if (!pci_enable_msi(jme->pdev)) {
1621 set_bit(JME_FLAG_MSI, &jme->flags);
1626 rc = request_irq(jme->pdev->irq, handler, irq_flags, netdev->name,
1630 "Unable to request %s interrupt (return: %d)\n",
1631 test_bit(JME_FLAG_MSI, &jme->flags) ? "MSI" : "INTx",
1634 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1635 pci_disable_msi(jme->pdev);
1636 clear_bit(JME_FLAG_MSI, &jme->flags);
1639 netdev->irq = jme->pdev->irq;
1646 jme_free_irq(struct jme_adapter *jme)
1648 free_irq(jme->pdev->irq, jme->dev);
1649 if (test_bit(JME_FLAG_MSI, &jme->flags)) {
1650 pci_disable_msi(jme->pdev);
1651 clear_bit(JME_FLAG_MSI, &jme->flags);
1652 jme->dev->irq = jme->pdev->irq;
1657 jme_new_phy_on(struct jme_adapter *jme)
1661 reg = jread32(jme, JME_PHY_PWR);
1662 reg &= ~(PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1663 PHY_PWR_DWN2 | PHY_PWR_CLKSEL);
1664 jwrite32(jme, JME_PHY_PWR, reg);
1666 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, ®);
1667 reg &= ~PE1_GPREG0_PBG;
1668 reg |= PE1_GPREG0_ENBG;
1669 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1673 jme_new_phy_off(struct jme_adapter *jme)
1677 reg = jread32(jme, JME_PHY_PWR);
1678 reg |= PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
1679 PHY_PWR_DWN2 | PHY_PWR_CLKSEL;
1680 jwrite32(jme, JME_PHY_PWR, reg);
1682 pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, ®);
1683 reg &= ~PE1_GPREG0_PBG;
1684 reg |= PE1_GPREG0_PDD3COLD;
1685 pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
1689 jme_phy_on(struct jme_adapter *jme)
1693 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1694 bmcr &= ~BMCR_PDOWN;
1695 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1697 if (new_phy_power_ctrl(jme->chip_main_rev))
1698 jme_new_phy_on(jme);
1702 jme_phy_off(struct jme_adapter *jme)
1706 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1708 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
1710 if (new_phy_power_ctrl(jme->chip_main_rev))
1711 jme_new_phy_off(jme);
1715 jme_phy_specreg_read(struct jme_adapter *jme, u32 specreg)
1719 phy_addr = JM_PHY_SPEC_REG_READ | specreg;
1720 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1722 return jme_mdio_read(jme->dev, jme->mii_if.phy_id,
1723 JM_PHY_SPEC_DATA_REG);
1727 jme_phy_specreg_write(struct jme_adapter *jme, u32 ext_reg, u32 phy_data)
1731 phy_addr = JM_PHY_SPEC_REG_WRITE | ext_reg;
1732 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_DATA_REG,
1734 jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
1739 jme_phy_calibration(struct jme_adapter *jme)
1741 u32 ctrl1000, phy_data;
1745 /* Enabel PHY test mode 1 */
1746 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1747 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1748 ctrl1000 |= PHY_GAD_TEST_MODE_1;
1749 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1751 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1752 phy_data &= ~JM_PHY_EXT_COMM_2_CALI_MODE_0;
1753 phy_data |= JM_PHY_EXT_COMM_2_CALI_LATCH |
1754 JM_PHY_EXT_COMM_2_CALI_ENABLE;
1755 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1757 phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
1758 phy_data &= ~(JM_PHY_EXT_COMM_2_CALI_ENABLE |
1759 JM_PHY_EXT_COMM_2_CALI_MODE_0 |
1760 JM_PHY_EXT_COMM_2_CALI_LATCH);
1761 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
1763 /* Disable PHY test mode */
1764 ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
1765 ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
1766 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
1771 jme_phy_setEA(struct jme_adapter *jme)
1773 u32 phy_comm0 = 0, phy_comm1 = 0;
1776 pci_read_config_byte(jme->pdev, PCI_PRIV_SHARE_NICCTRL, &nic_ctrl);
1777 if ((nic_ctrl & 0x3) == JME_FLAG_PHYEA_ENABLE)
1780 switch (jme->pdev->device) {
1781 case PCI_DEVICE_ID_JMICRON_JMC250:
1782 if (((jme->chip_main_rev == 5) &&
1783 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1784 (jme->chip_sub_rev == 3))) ||
1785 (jme->chip_main_rev >= 6)) {
1789 if ((jme->chip_main_rev == 3) &&
1790 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1793 case PCI_DEVICE_ID_JMICRON_JMC260:
1794 if (((jme->chip_main_rev == 5) &&
1795 ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
1796 (jme->chip_sub_rev == 3))) ||
1797 (jme->chip_main_rev >= 6)) {
1801 if ((jme->chip_main_rev == 3) &&
1802 ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
1804 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 0))
1806 if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 2))
1813 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_0_REG, phy_comm0);
1815 jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_1_REG, phy_comm1);
1821 jme_open(struct net_device *netdev)
1823 struct jme_adapter *jme = netdev_priv(netdev);
1826 jme_clear_pm_disable_wol(jme);
1827 JME_NAPI_ENABLE(jme);
1829 tasklet_setup(&jme->txclean_task, jme_tx_clean_tasklet);
1830 tasklet_setup(&jme->rxclean_task, jme_rx_clean_tasklet);
1831 tasklet_setup(&jme->rxempty_task, jme_rx_empty_tasklet);
1833 rc = jme_request_irq(jme);
1840 if (test_bit(JME_FLAG_SSET, &jme->flags))
1841 jme_set_link_ksettings(netdev, &jme->old_cmd);
1843 jme_reset_phy_processor(jme);
1844 jme_phy_calibration(jme);
1846 jme_reset_link(jme);
1851 netif_stop_queue(netdev);
1852 netif_carrier_off(netdev);
1857 jme_set_100m_half(struct jme_adapter *jme)
1862 bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
1863 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
1864 BMCR_SPEED1000 | BMCR_FULLDPLX);
1865 tmp |= BMCR_SPEED100;
1868 jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, tmp);
1871 jwrite32(jme, JME_GHC, GHC_SPEED_100M | GHC_LINK_POLL);
1873 jwrite32(jme, JME_GHC, GHC_SPEED_100M);
1876 #define JME_WAIT_LINK_TIME 2000 /* 2000ms */
1878 jme_wait_link(struct jme_adapter *jme)
1880 u32 phylink, to = JME_WAIT_LINK_TIME;
1883 phylink = jme_linkstat_from_phy(jme);
1884 while (!(phylink & PHY_LINK_UP) && (to -= 10) > 0) {
1885 usleep_range(10000, 11000);
1886 phylink = jme_linkstat_from_phy(jme);
1891 jme_powersave_phy(struct jme_adapter *jme)
1893 if (jme->reg_pmcs && device_may_wakeup(&jme->pdev->dev)) {
1894 jme_set_100m_half(jme);
1895 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
1897 jme_clear_pm_enable_wol(jme);
1904 jme_close(struct net_device *netdev)
1906 struct jme_adapter *jme = netdev_priv(netdev);
1908 netif_stop_queue(netdev);
1909 netif_carrier_off(netdev);
1914 JME_NAPI_DISABLE(jme);
1916 cancel_work_sync(&jme->linkch_task);
1917 tasklet_kill(&jme->txclean_task);
1918 tasklet_kill(&jme->rxclean_task);
1919 tasklet_kill(&jme->rxempty_task);
1921 jme_disable_rx_engine(jme);
1922 jme_disable_tx_engine(jme);
1923 jme_reset_mac_processor(jme);
1924 jme_free_rx_resources(jme);
1925 jme_free_tx_resources(jme);
1933 jme_alloc_txdesc(struct jme_adapter *jme,
1934 struct sk_buff *skb)
1936 struct jme_ring *txring = &(jme->txring[0]);
1937 int idx, nr_alloc, mask = jme->tx_ring_mask;
1939 idx = txring->next_to_use;
1940 nr_alloc = skb_shinfo(skb)->nr_frags + 2;
1942 if (unlikely(atomic_read(&txring->nr_free) < nr_alloc))
1945 atomic_sub(nr_alloc, &txring->nr_free);
1947 txring->next_to_use = (txring->next_to_use + nr_alloc) & mask;
1953 jme_fill_tx_map(struct pci_dev *pdev,
1954 struct txdesc *txdesc,
1955 struct jme_buffer_info *txbi,
1963 dmaaddr = dma_map_page(&pdev->dev, page, page_offset, len,
1966 if (unlikely(dma_mapping_error(&pdev->dev, dmaaddr)))
1969 dma_sync_single_for_device(&pdev->dev, dmaaddr, len, DMA_TO_DEVICE);
1973 txdesc->desc2.flags = TXFLAG_OWN;
1974 txdesc->desc2.flags |= (hidma) ? TXFLAG_64BIT : 0;
1975 txdesc->desc2.datalen = cpu_to_le16(len);
1976 txdesc->desc2.bufaddrh = cpu_to_le32((__u64)dmaaddr >> 32);
1977 txdesc->desc2.bufaddrl = cpu_to_le32(
1978 (__u64)dmaaddr & 0xFFFFFFFFUL);
1980 txbi->mapping = dmaaddr;
1985 static void jme_drop_tx_map(struct jme_adapter *jme, int startidx, int count)
1987 struct jme_ring *txring = &(jme->txring[0]);
1988 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
1989 int mask = jme->tx_ring_mask;
1992 for (j = 0 ; j < count ; j++) {
1993 ctxbi = txbi + ((startidx + j + 2) & (mask));
1994 dma_unmap_page(&jme->pdev->dev, ctxbi->mapping, ctxbi->len,
2003 jme_map_tx_skb(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2005 struct jme_ring *txring = &(jme->txring[0]);
2006 struct txdesc *txdesc = txring->desc, *ctxdesc;
2007 struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
2008 bool hidma = jme->dev->features & NETIF_F_HIGHDMA;
2009 int i, nr_frags = skb_shinfo(skb)->nr_frags;
2010 int mask = jme->tx_ring_mask;
2014 for (i = 0 ; i < nr_frags ; ++i) {
2015 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2017 ctxdesc = txdesc + ((idx + i + 2) & (mask));
2018 ctxbi = txbi + ((idx + i + 2) & (mask));
2020 ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi,
2021 skb_frag_page(frag), skb_frag_off(frag),
2022 skb_frag_size(frag), hidma);
2024 jme_drop_tx_map(jme, idx, i);
2029 len = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
2030 ctxdesc = txdesc + ((idx + 1) & (mask));
2031 ctxbi = txbi + ((idx + 1) & (mask));
2032 ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi, virt_to_page(skb->data),
2033 offset_in_page(skb->data), len, hidma);
2035 jme_drop_tx_map(jme, idx, i);
2044 jme_tx_tso(struct sk_buff *skb, __le16 *mss, u8 *flags)
2046 *mss = cpu_to_le16(skb_shinfo(skb)->gso_size << TXDESC_MSS_SHIFT);
2048 *flags |= TXFLAG_LSEN;
2050 if (skb->protocol == htons(ETH_P_IP)) {
2051 struct iphdr *iph = ip_hdr(skb);
2054 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2059 tcp_v6_gso_csum_prep(skb);
2069 jme_tx_csum(struct jme_adapter *jme, struct sk_buff *skb, u8 *flags)
2071 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2074 switch (skb->protocol) {
2075 case htons(ETH_P_IP):
2076 ip_proto = ip_hdr(skb)->protocol;
2078 case htons(ETH_P_IPV6):
2079 ip_proto = ipv6_hdr(skb)->nexthdr;
2088 *flags |= TXFLAG_TCPCS;
2091 *flags |= TXFLAG_UDPCS;
2094 netif_err(jme, tx_err, jme->dev, "Error upper layer protocol\n");
2101 jme_tx_vlan(struct sk_buff *skb, __le16 *vlan, u8 *flags)
2103 if (skb_vlan_tag_present(skb)) {
2104 *flags |= TXFLAG_TAGON;
2105 *vlan = cpu_to_le16(skb_vlan_tag_get(skb));
2110 jme_fill_tx_desc(struct jme_adapter *jme, struct sk_buff *skb, int idx)
2112 struct jme_ring *txring = &(jme->txring[0]);
2113 struct txdesc *txdesc;
2114 struct jme_buffer_info *txbi;
2118 txdesc = (struct txdesc *)txring->desc + idx;
2119 txbi = txring->bufinf + idx;
2125 txdesc->desc1.pktsize = cpu_to_le16(skb->len);
2127 * Set OWN bit at final.
2128 * When kernel transmit faster than NIC.
2129 * And NIC trying to send this descriptor before we tell
2130 * it to start sending this TX queue.
2131 * Other fields are already filled correctly.
2134 flags = TXFLAG_OWN | TXFLAG_INT;
2136 * Set checksum flags while not tso
2138 if (jme_tx_tso(skb, &txdesc->desc1.mss, &flags))
2139 jme_tx_csum(jme, skb, &flags);
2140 jme_tx_vlan(skb, &txdesc->desc1.vlan, &flags);
2141 ret = jme_map_tx_skb(jme, skb, idx);
2145 txdesc->desc1.flags = flags;
2147 * Set tx buffer info after telling NIC to send
2148 * For better tx_clean timing
2151 txbi->nr_desc = skb_shinfo(skb)->nr_frags + 2;
2153 txbi->len = skb->len;
2154 txbi->start_xmit = jiffies;
2155 if (!txbi->start_xmit)
2156 txbi->start_xmit = (0UL-1);
2162 jme_stop_queue_if_full(struct jme_adapter *jme)
2164 struct jme_ring *txring = &(jme->txring[0]);
2165 struct jme_buffer_info *txbi = txring->bufinf;
2166 int idx = atomic_read(&txring->next_to_clean);
2171 if (unlikely(atomic_read(&txring->nr_free) < (MAX_SKB_FRAGS+2))) {
2172 netif_stop_queue(jme->dev);
2173 netif_info(jme, tx_queued, jme->dev, "TX Queue Paused\n");
2175 if (atomic_read(&txring->nr_free)
2176 >= (jme->tx_wake_threshold)) {
2177 netif_wake_queue(jme->dev);
2178 netif_info(jme, tx_queued, jme->dev, "TX Queue Fast Waked\n");
2182 if (unlikely(txbi->start_xmit &&
2183 time_is_before_eq_jiffies(txbi->start_xmit + TX_TIMEOUT) &&
2185 netif_stop_queue(jme->dev);
2186 netif_info(jme, tx_queued, jme->dev,
2187 "TX Queue Stopped %d@%lu\n", idx, jiffies);
2192 * This function is already protected by netif_tx_lock()
2196 jme_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2198 struct jme_adapter *jme = netdev_priv(netdev);
2201 if (unlikely(skb_is_gso(skb) && skb_cow_head(skb, 0))) {
2202 dev_kfree_skb_any(skb);
2203 ++(NET_STAT(jme).tx_dropped);
2204 return NETDEV_TX_OK;
2207 idx = jme_alloc_txdesc(jme, skb);
2209 if (unlikely(idx < 0)) {
2210 netif_stop_queue(netdev);
2211 netif_err(jme, tx_err, jme->dev,
2212 "BUG! Tx ring full when queue awake!\n");
2214 return NETDEV_TX_BUSY;
2217 if (jme_fill_tx_desc(jme, skb, idx))
2218 return NETDEV_TX_OK;
2220 jwrite32(jme, JME_TXCS, jme->reg_txcs |
2221 TXCS_SELECT_QUEUE0 |
2225 tx_dbg(jme, "xmit: %d+%d@%lu\n",
2226 idx, skb_shinfo(skb)->nr_frags + 2, jiffies);
2227 jme_stop_queue_if_full(jme);
2229 return NETDEV_TX_OK;
2233 jme_set_unicastaddr(struct net_device *netdev)
2235 struct jme_adapter *jme = netdev_priv(netdev);
2238 val = (netdev->dev_addr[3] & 0xff) << 24 |
2239 (netdev->dev_addr[2] & 0xff) << 16 |
2240 (netdev->dev_addr[1] & 0xff) << 8 |
2241 (netdev->dev_addr[0] & 0xff);
2242 jwrite32(jme, JME_RXUMA_LO, val);
2243 val = (netdev->dev_addr[5] & 0xff) << 8 |
2244 (netdev->dev_addr[4] & 0xff);
2245 jwrite32(jme, JME_RXUMA_HI, val);
2249 jme_set_macaddr(struct net_device *netdev, void *p)
2251 struct jme_adapter *jme = netdev_priv(netdev);
2252 struct sockaddr *addr = p;
2254 if (netif_running(netdev))
2257 spin_lock_bh(&jme->macaddr_lock);
2258 eth_hw_addr_set(netdev, addr->sa_data);
2259 jme_set_unicastaddr(netdev);
2260 spin_unlock_bh(&jme->macaddr_lock);
2266 jme_set_multi(struct net_device *netdev)
2268 struct jme_adapter *jme = netdev_priv(netdev);
2269 u32 mc_hash[2] = {};
2271 spin_lock_bh(&jme->rxmcs_lock);
2273 jme->reg_rxmcs |= RXMCS_BRDFRAME | RXMCS_UNIFRAME;
2275 if (netdev->flags & IFF_PROMISC) {
2276 jme->reg_rxmcs |= RXMCS_ALLFRAME;
2277 } else if (netdev->flags & IFF_ALLMULTI) {
2278 jme->reg_rxmcs |= RXMCS_ALLMULFRAME;
2279 } else if (netdev->flags & IFF_MULTICAST) {
2280 struct netdev_hw_addr *ha;
2283 jme->reg_rxmcs |= RXMCS_MULFRAME | RXMCS_MULFILTERED;
2284 netdev_for_each_mc_addr(ha, netdev) {
2285 bit_nr = ether_crc(ETH_ALEN, ha->addr) & 0x3F;
2286 mc_hash[bit_nr >> 5] |= 1 << (bit_nr & 0x1F);
2289 jwrite32(jme, JME_RXMCHT_LO, mc_hash[0]);
2290 jwrite32(jme, JME_RXMCHT_HI, mc_hash[1]);
2294 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2296 spin_unlock_bh(&jme->rxmcs_lock);
2300 jme_change_mtu(struct net_device *netdev, int new_mtu)
2302 struct jme_adapter *jme = netdev_priv(netdev);
2304 netdev->mtu = new_mtu;
2305 netdev_update_features(netdev);
2307 jme_restart_rx_engine(jme);
2308 jme_reset_link(jme);
2314 jme_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2316 struct jme_adapter *jme = netdev_priv(netdev);
2319 jme_reset_phy_processor(jme);
2320 if (test_bit(JME_FLAG_SSET, &jme->flags))
2321 jme_set_link_ksettings(netdev, &jme->old_cmd);
2324 * Force to Reset the link again
2326 jme_reset_link(jme);
2330 jme_get_drvinfo(struct net_device *netdev,
2331 struct ethtool_drvinfo *info)
2333 struct jme_adapter *jme = netdev_priv(netdev);
2335 strscpy(info->driver, DRV_NAME, sizeof(info->driver));
2336 strscpy(info->version, DRV_VERSION, sizeof(info->version));
2337 strscpy(info->bus_info, pci_name(jme->pdev), sizeof(info->bus_info));
2341 jme_get_regs_len(struct net_device *netdev)
2347 mmapio_memcpy(struct jme_adapter *jme, u32 *p, u32 reg, int len)
2351 for (i = 0 ; i < len ; i += 4)
2352 p[i >> 2] = jread32(jme, reg + i);
2356 mdio_memcpy(struct jme_adapter *jme, u32 *p, int reg_nr)
2359 u16 *p16 = (u16 *)p;
2361 for (i = 0 ; i < reg_nr ; ++i)
2362 p16[i] = jme_mdio_read(jme->dev, jme->mii_if.phy_id, i);
2366 jme_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
2368 struct jme_adapter *jme = netdev_priv(netdev);
2369 u32 *p32 = (u32 *)p;
2371 memset(p, 0xFF, JME_REG_LEN);
2374 mmapio_memcpy(jme, p32, JME_MAC, JME_MAC_LEN);
2377 mmapio_memcpy(jme, p32, JME_PHY, JME_PHY_LEN);
2380 mmapio_memcpy(jme, p32, JME_MISC, JME_MISC_LEN);
2383 mmapio_memcpy(jme, p32, JME_RSS, JME_RSS_LEN);
2386 mdio_memcpy(jme, p32, JME_PHY_REG_NR);
2389 static int jme_get_coalesce(struct net_device *netdev,
2390 struct ethtool_coalesce *ecmd,
2391 struct kernel_ethtool_coalesce *kernel_coal,
2392 struct netlink_ext_ack *extack)
2394 struct jme_adapter *jme = netdev_priv(netdev);
2396 ecmd->tx_coalesce_usecs = PCC_TX_TO;
2397 ecmd->tx_max_coalesced_frames = PCC_TX_CNT;
2399 if (test_bit(JME_FLAG_POLL, &jme->flags)) {
2400 ecmd->use_adaptive_rx_coalesce = false;
2401 ecmd->rx_coalesce_usecs = 0;
2402 ecmd->rx_max_coalesced_frames = 0;
2406 ecmd->use_adaptive_rx_coalesce = true;
2408 switch (jme->dpi.cur) {
2410 ecmd->rx_coalesce_usecs = PCC_P1_TO;
2411 ecmd->rx_max_coalesced_frames = PCC_P1_CNT;
2414 ecmd->rx_coalesce_usecs = PCC_P2_TO;
2415 ecmd->rx_max_coalesced_frames = PCC_P2_CNT;
2418 ecmd->rx_coalesce_usecs = PCC_P3_TO;
2419 ecmd->rx_max_coalesced_frames = PCC_P3_CNT;
2428 static int jme_set_coalesce(struct net_device *netdev,
2429 struct ethtool_coalesce *ecmd,
2430 struct kernel_ethtool_coalesce *kernel_coal,
2431 struct netlink_ext_ack *extack)
2433 struct jme_adapter *jme = netdev_priv(netdev);
2434 struct dynpcc_info *dpi = &(jme->dpi);
2436 if (netif_running(netdev))
2439 if (ecmd->use_adaptive_rx_coalesce &&
2440 test_bit(JME_FLAG_POLL, &jme->flags)) {
2441 clear_bit(JME_FLAG_POLL, &jme->flags);
2442 jme->jme_rx = netif_rx;
2444 dpi->attempt = PCC_P1;
2446 jme_set_rx_pcc(jme, PCC_P1);
2447 jme_interrupt_mode(jme);
2448 } else if (!(ecmd->use_adaptive_rx_coalesce) &&
2449 !(test_bit(JME_FLAG_POLL, &jme->flags))) {
2450 set_bit(JME_FLAG_POLL, &jme->flags);
2451 jme->jme_rx = netif_receive_skb;
2452 jme_interrupt_mode(jme);
2459 jme_get_pauseparam(struct net_device *netdev,
2460 struct ethtool_pauseparam *ecmd)
2462 struct jme_adapter *jme = netdev_priv(netdev);
2465 ecmd->tx_pause = (jme->reg_txpfc & TXPFC_PF_EN) != 0;
2466 ecmd->rx_pause = (jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0;
2468 spin_lock_bh(&jme->phy_lock);
2469 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2470 spin_unlock_bh(&jme->phy_lock);
2473 (val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0;
2477 jme_set_pauseparam(struct net_device *netdev,
2478 struct ethtool_pauseparam *ecmd)
2480 struct jme_adapter *jme = netdev_priv(netdev);
2483 if (((jme->reg_txpfc & TXPFC_PF_EN) != 0) ^
2484 (ecmd->tx_pause != 0)) {
2487 jme->reg_txpfc |= TXPFC_PF_EN;
2489 jme->reg_txpfc &= ~TXPFC_PF_EN;
2491 jwrite32(jme, JME_TXPFC, jme->reg_txpfc);
2494 spin_lock_bh(&jme->rxmcs_lock);
2495 if (((jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0) ^
2496 (ecmd->rx_pause != 0)) {
2499 jme->reg_rxmcs |= RXMCS_FLOWCTRL;
2501 jme->reg_rxmcs &= ~RXMCS_FLOWCTRL;
2503 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2505 spin_unlock_bh(&jme->rxmcs_lock);
2507 spin_lock_bh(&jme->phy_lock);
2508 val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
2509 if (((val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0) ^
2510 (ecmd->autoneg != 0)) {
2513 val |= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2515 val &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2517 jme_mdio_write(jme->dev, jme->mii_if.phy_id,
2518 MII_ADVERTISE, val);
2520 spin_unlock_bh(&jme->phy_lock);
2526 jme_get_wol(struct net_device *netdev,
2527 struct ethtool_wolinfo *wol)
2529 struct jme_adapter *jme = netdev_priv(netdev);
2531 wol->supported = WAKE_MAGIC | WAKE_PHY;
2535 if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
2536 wol->wolopts |= WAKE_PHY;
2538 if (jme->reg_pmcs & PMCS_MFEN)
2539 wol->wolopts |= WAKE_MAGIC;
2544 jme_set_wol(struct net_device *netdev,
2545 struct ethtool_wolinfo *wol)
2547 struct jme_adapter *jme = netdev_priv(netdev);
2549 if (wol->wolopts & (WAKE_MAGICSECURE |
2558 if (wol->wolopts & WAKE_PHY)
2559 jme->reg_pmcs |= PMCS_LFEN | PMCS_LREN;
2561 if (wol->wolopts & WAKE_MAGIC)
2562 jme->reg_pmcs |= PMCS_MFEN;
2568 jme_get_link_ksettings(struct net_device *netdev,
2569 struct ethtool_link_ksettings *cmd)
2571 struct jme_adapter *jme = netdev_priv(netdev);
2573 spin_lock_bh(&jme->phy_lock);
2574 mii_ethtool_get_link_ksettings(&jme->mii_if, cmd);
2575 spin_unlock_bh(&jme->phy_lock);
2580 jme_set_link_ksettings(struct net_device *netdev,
2581 const struct ethtool_link_ksettings *cmd)
2583 struct jme_adapter *jme = netdev_priv(netdev);
2586 if (cmd->base.speed == SPEED_1000 &&
2587 cmd->base.autoneg != AUTONEG_ENABLE)
2591 * Check If user changed duplex only while force_media.
2592 * Hardware would not generate link change interrupt.
2594 if (jme->mii_if.force_media &&
2595 cmd->base.autoneg != AUTONEG_ENABLE &&
2596 (jme->mii_if.full_duplex != cmd->base.duplex))
2599 spin_lock_bh(&jme->phy_lock);
2600 rc = mii_ethtool_set_link_ksettings(&jme->mii_if, cmd);
2601 spin_unlock_bh(&jme->phy_lock);
2605 jme_reset_link(jme);
2606 jme->old_cmd = *cmd;
2607 set_bit(JME_FLAG_SSET, &jme->flags);
2614 jme_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2617 struct jme_adapter *jme = netdev_priv(netdev);
2618 struct mii_ioctl_data *mii_data = if_mii(rq);
2619 unsigned int duplex_chg;
2621 if (cmd == SIOCSMIIREG) {
2622 u16 val = mii_data->val_in;
2623 if (!(val & (BMCR_RESET|BMCR_ANENABLE)) &&
2624 (val & BMCR_SPEED1000))
2628 spin_lock_bh(&jme->phy_lock);
2629 rc = generic_mii_ioctl(&jme->mii_if, mii_data, cmd, &duplex_chg);
2630 spin_unlock_bh(&jme->phy_lock);
2632 if (!rc && (cmd == SIOCSMIIREG)) {
2634 jme_reset_link(jme);
2635 jme_get_link_ksettings(netdev, &jme->old_cmd);
2636 set_bit(JME_FLAG_SSET, &jme->flags);
2643 jme_get_link(struct net_device *netdev)
2645 struct jme_adapter *jme = netdev_priv(netdev);
2646 return jread32(jme, JME_PHY_LINK) & PHY_LINK_UP;
2650 jme_get_msglevel(struct net_device *netdev)
2652 struct jme_adapter *jme = netdev_priv(netdev);
2653 return jme->msg_enable;
2657 jme_set_msglevel(struct net_device *netdev, u32 value)
2659 struct jme_adapter *jme = netdev_priv(netdev);
2660 jme->msg_enable = value;
2663 static netdev_features_t
2664 jme_fix_features(struct net_device *netdev, netdev_features_t features)
2666 if (netdev->mtu > 1900)
2667 features &= ~(NETIF_F_ALL_TSO | NETIF_F_CSUM_MASK);
2672 jme_set_features(struct net_device *netdev, netdev_features_t features)
2674 struct jme_adapter *jme = netdev_priv(netdev);
2676 spin_lock_bh(&jme->rxmcs_lock);
2677 if (features & NETIF_F_RXCSUM)
2678 jme->reg_rxmcs |= RXMCS_CHECKSUM;
2680 jme->reg_rxmcs &= ~RXMCS_CHECKSUM;
2681 jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
2682 spin_unlock_bh(&jme->rxmcs_lock);
2687 #ifdef CONFIG_NET_POLL_CONTROLLER
2688 static void jme_netpoll(struct net_device *dev)
2690 unsigned long flags;
2692 local_irq_save(flags);
2693 jme_intr(dev->irq, dev);
2694 local_irq_restore(flags);
2699 jme_nway_reset(struct net_device *netdev)
2701 struct jme_adapter *jme = netdev_priv(netdev);
2702 jme_restart_an(jme);
2707 jme_smb_read(struct jme_adapter *jme, unsigned int addr)
2712 val = jread32(jme, JME_SMBCSR);
2713 to = JME_SMB_BUSY_TIMEOUT;
2714 while ((val & SMBCSR_BUSY) && --to) {
2716 val = jread32(jme, JME_SMBCSR);
2719 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2723 jwrite32(jme, JME_SMBINTF,
2724 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2725 SMBINTF_HWRWN_READ |
2728 val = jread32(jme, JME_SMBINTF);
2729 to = JME_SMB_BUSY_TIMEOUT;
2730 while ((val & SMBINTF_HWCMD) && --to) {
2732 val = jread32(jme, JME_SMBINTF);
2735 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2739 return (val & SMBINTF_HWDATR) >> SMBINTF_HWDATR_SHIFT;
2743 jme_smb_write(struct jme_adapter *jme, unsigned int addr, u8 data)
2748 val = jread32(jme, JME_SMBCSR);
2749 to = JME_SMB_BUSY_TIMEOUT;
2750 while ((val & SMBCSR_BUSY) && --to) {
2752 val = jread32(jme, JME_SMBCSR);
2755 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2759 jwrite32(jme, JME_SMBINTF,
2760 ((data << SMBINTF_HWDATW_SHIFT) & SMBINTF_HWDATW) |
2761 ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
2762 SMBINTF_HWRWN_WRITE |
2765 val = jread32(jme, JME_SMBINTF);
2766 to = JME_SMB_BUSY_TIMEOUT;
2767 while ((val & SMBINTF_HWCMD) && --to) {
2769 val = jread32(jme, JME_SMBINTF);
2772 netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
2780 jme_get_eeprom_len(struct net_device *netdev)
2782 struct jme_adapter *jme = netdev_priv(netdev);
2784 val = jread32(jme, JME_SMBCSR);
2785 return (val & SMBCSR_EEPROMD) ? JME_SMB_LEN : 0;
2789 jme_get_eeprom(struct net_device *netdev,
2790 struct ethtool_eeprom *eeprom, u8 *data)
2792 struct jme_adapter *jme = netdev_priv(netdev);
2793 int i, offset = eeprom->offset, len = eeprom->len;
2796 * ethtool will check the boundary for us
2798 eeprom->magic = JME_EEPROM_MAGIC;
2799 for (i = 0 ; i < len ; ++i)
2800 data[i] = jme_smb_read(jme, i + offset);
2806 jme_set_eeprom(struct net_device *netdev,
2807 struct ethtool_eeprom *eeprom, u8 *data)
2809 struct jme_adapter *jme = netdev_priv(netdev);
2810 int i, offset = eeprom->offset, len = eeprom->len;
2812 if (eeprom->magic != JME_EEPROM_MAGIC)
2816 * ethtool will check the boundary for us
2818 for (i = 0 ; i < len ; ++i)
2819 jme_smb_write(jme, i + offset, data[i]);
2824 static const struct ethtool_ops jme_ethtool_ops = {
2825 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2826 ETHTOOL_COALESCE_MAX_FRAMES |
2827 ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
2828 .get_drvinfo = jme_get_drvinfo,
2829 .get_regs_len = jme_get_regs_len,
2830 .get_regs = jme_get_regs,
2831 .get_coalesce = jme_get_coalesce,
2832 .set_coalesce = jme_set_coalesce,
2833 .get_pauseparam = jme_get_pauseparam,
2834 .set_pauseparam = jme_set_pauseparam,
2835 .get_wol = jme_get_wol,
2836 .set_wol = jme_set_wol,
2837 .get_link = jme_get_link,
2838 .get_msglevel = jme_get_msglevel,
2839 .set_msglevel = jme_set_msglevel,
2840 .nway_reset = jme_nway_reset,
2841 .get_eeprom_len = jme_get_eeprom_len,
2842 .get_eeprom = jme_get_eeprom,
2843 .set_eeprom = jme_set_eeprom,
2844 .get_link_ksettings = jme_get_link_ksettings,
2845 .set_link_ksettings = jme_set_link_ksettings,
2849 jme_pci_dma64(struct pci_dev *pdev)
2851 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
2852 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
2855 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
2856 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)))
2859 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
2866 jme_phy_init(struct jme_adapter *jme)
2870 reg26 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 26);
2871 jme_mdio_write(jme->dev, jme->mii_if.phy_id, 26, reg26 | 0x1000);
2875 jme_check_hw_ver(struct jme_adapter *jme)
2879 chipmode = jread32(jme, JME_CHIPMODE);
2881 jme->fpgaver = (chipmode & CM_FPGAVER_MASK) >> CM_FPGAVER_SHIFT;
2882 jme->chiprev = (chipmode & CM_CHIPREV_MASK) >> CM_CHIPREV_SHIFT;
2883 jme->chip_main_rev = jme->chiprev & 0xF;
2884 jme->chip_sub_rev = (jme->chiprev >> 4) & 0xF;
2887 static const struct net_device_ops jme_netdev_ops = {
2888 .ndo_open = jme_open,
2889 .ndo_stop = jme_close,
2890 .ndo_validate_addr = eth_validate_addr,
2891 .ndo_eth_ioctl = jme_ioctl,
2892 .ndo_start_xmit = jme_start_xmit,
2893 .ndo_set_mac_address = jme_set_macaddr,
2894 .ndo_set_rx_mode = jme_set_multi,
2895 .ndo_change_mtu = jme_change_mtu,
2896 .ndo_tx_timeout = jme_tx_timeout,
2897 .ndo_fix_features = jme_fix_features,
2898 .ndo_set_features = jme_set_features,
2899 #ifdef CONFIG_NET_POLL_CONTROLLER
2900 .ndo_poll_controller = jme_netpoll,
2905 jme_init_one(struct pci_dev *pdev,
2906 const struct pci_device_id *ent)
2908 int rc = 0, using_dac, i;
2909 struct net_device *netdev;
2910 struct jme_adapter *jme;
2915 * set up PCI device basics
2917 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
2918 PCIE_LINK_STATE_CLKPM);
2920 rc = pci_enable_device(pdev);
2922 pr_err("Cannot enable PCI device\n");
2926 using_dac = jme_pci_dma64(pdev);
2927 if (using_dac < 0) {
2928 pr_err("Cannot set PCI DMA Mask\n");
2930 goto err_out_disable_pdev;
2933 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2934 pr_err("No PCI resource region found\n");
2936 goto err_out_disable_pdev;
2939 rc = pci_request_regions(pdev, DRV_NAME);
2941 pr_err("Cannot obtain PCI resource region\n");
2942 goto err_out_disable_pdev;
2945 pci_set_master(pdev);
2948 * alloc and init net device
2950 netdev = alloc_etherdev(sizeof(*jme));
2953 goto err_out_release_regions;
2955 netdev->netdev_ops = &jme_netdev_ops;
2956 netdev->ethtool_ops = &jme_ethtool_ops;
2957 netdev->watchdog_timeo = TX_TIMEOUT;
2958 netdev->hw_features = NETIF_F_IP_CSUM |
2964 netdev->features = NETIF_F_IP_CSUM |
2969 NETIF_F_HW_VLAN_CTAG_TX |
2970 NETIF_F_HW_VLAN_CTAG_RX;
2972 netdev->features |= NETIF_F_HIGHDMA;
2974 /* MTU range: 1280 - 9202*/
2975 netdev->min_mtu = IPV6_MIN_MTU;
2976 netdev->max_mtu = MAX_ETHERNET_JUMBO_PACKET_SIZE - ETH_HLEN;
2978 SET_NETDEV_DEV(netdev, &pdev->dev);
2979 pci_set_drvdata(pdev, netdev);
2984 jme = netdev_priv(netdev);
2987 jme->jme_rx = netif_rx;
2988 jme->old_mtu = netdev->mtu = 1500;
2990 jme->tx_ring_size = 1 << 10;
2991 jme->tx_ring_mask = jme->tx_ring_size - 1;
2992 jme->tx_wake_threshold = 1 << 9;
2993 jme->rx_ring_size = 1 << 9;
2994 jme->rx_ring_mask = jme->rx_ring_size - 1;
2995 jme->msg_enable = JME_DEF_MSG_ENABLE;
2996 jme->regs = ioremap(pci_resource_start(pdev, 0),
2997 pci_resource_len(pdev, 0));
2999 pr_err("Mapping PCI resource region error\n");
3001 goto err_out_free_netdev;
3005 apmc = jread32(jme, JME_APMC) & ~JME_APMC_PSEUDO_HP_EN;
3006 jwrite32(jme, JME_APMC, apmc);
3007 } else if (force_pseudohp) {
3008 apmc = jread32(jme, JME_APMC) | JME_APMC_PSEUDO_HP_EN;
3009 jwrite32(jme, JME_APMC, apmc);
3012 netif_napi_add(netdev, &jme->napi, jme_poll);
3014 spin_lock_init(&jme->phy_lock);
3015 spin_lock_init(&jme->macaddr_lock);
3016 spin_lock_init(&jme->rxmcs_lock);
3018 atomic_set(&jme->link_changing, 1);
3019 atomic_set(&jme->rx_cleaning, 1);
3020 atomic_set(&jme->tx_cleaning, 1);
3021 atomic_set(&jme->rx_empty, 1);
3023 tasklet_setup(&jme->pcc_task, jme_pcc_tasklet);
3024 INIT_WORK(&jme->linkch_task, jme_link_change_work);
3025 jme->dpi.cur = PCC_P1;
3028 jme->reg_rxcs = RXCS_DEFAULT;
3029 jme->reg_rxmcs = RXMCS_DEFAULT;
3031 jme->reg_pmcs = PMCS_MFEN;
3032 jme->reg_gpreg1 = GPREG1_DEFAULT;
3034 if (jme->reg_rxmcs & RXMCS_CHECKSUM)
3035 netdev->features |= NETIF_F_RXCSUM;
3038 * Get Max Read Req Size from PCI Config Space
3040 pci_read_config_byte(pdev, PCI_DCSR_MRRS, &jme->mrrs);
3041 jme->mrrs &= PCI_DCSR_MRRS_MASK;
3042 switch (jme->mrrs) {
3044 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_128B;
3047 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_256B;
3050 jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_512B;
3055 * Must check before reset_mac_processor
3057 jme_check_hw_ver(jme);
3058 jme->mii_if.dev = netdev;
3060 jme->mii_if.phy_id = 0;
3061 for (i = 1 ; i < 32 ; ++i) {
3062 bmcr = jme_mdio_read(netdev, i, MII_BMCR);
3063 bmsr = jme_mdio_read(netdev, i, MII_BMSR);
3064 if (bmcr != 0xFFFFU && (bmcr != 0 || bmsr != 0)) {
3065 jme->mii_if.phy_id = i;
3070 if (!jme->mii_if.phy_id) {
3072 pr_err("Can not find phy_id\n");
3076 jme->reg_ghc |= GHC_LINK_POLL;
3078 jme->mii_if.phy_id = 1;
3080 if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
3081 jme->mii_if.supports_gmii = true;
3083 jme->mii_if.supports_gmii = false;
3084 jme->mii_if.phy_id_mask = 0x1F;
3085 jme->mii_if.reg_num_mask = 0x1F;
3086 jme->mii_if.mdio_read = jme_mdio_read;
3087 jme->mii_if.mdio_write = jme_mdio_write;
3089 jme_clear_pm_disable_wol(jme);
3090 device_init_wakeup(&pdev->dev, true);
3092 jme_set_phyfifo_5level(jme);
3093 jme->pcirev = pdev->revision;
3099 * Reset MAC processor and reload EEPROM for MAC Address
3101 jme_reset_mac_processor(jme);
3102 rc = jme_reload_eeprom(jme);
3104 pr_err("Reload eeprom for reading MAC Address error\n");
3107 jme_load_macaddr(netdev);
3110 * Tell stack that we are not ready to work until open()
3112 netif_carrier_off(netdev);
3114 rc = register_netdev(netdev);
3116 pr_err("Cannot register net device\n");
3120 netif_info(jme, probe, jme->dev, "%s%s chiprev:%x pcirev:%x macaddr:%pM\n",
3121 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250) ?
3122 "JMC250 Gigabit Ethernet" :
3123 (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC260) ?
3124 "JMC260 Fast Ethernet" : "Unknown",
3125 (jme->fpgaver != 0) ? " (FPGA)" : "",
3126 (jme->fpgaver != 0) ? jme->fpgaver : jme->chiprev,
3127 jme->pcirev, netdev->dev_addr);
3133 err_out_free_netdev:
3134 free_netdev(netdev);
3135 err_out_release_regions:
3136 pci_release_regions(pdev);
3137 err_out_disable_pdev:
3138 pci_disable_device(pdev);
3144 jme_remove_one(struct pci_dev *pdev)
3146 struct net_device *netdev = pci_get_drvdata(pdev);
3147 struct jme_adapter *jme = netdev_priv(netdev);
3149 unregister_netdev(netdev);
3151 free_netdev(netdev);
3152 pci_release_regions(pdev);
3153 pci_disable_device(pdev);
3158 jme_shutdown(struct pci_dev *pdev)
3160 struct net_device *netdev = pci_get_drvdata(pdev);
3161 struct jme_adapter *jme = netdev_priv(netdev);
3163 jme_powersave_phy(jme);
3164 pci_pme_active(pdev, true);
3167 #ifdef CONFIG_PM_SLEEP
3169 jme_suspend(struct device *dev)
3171 struct net_device *netdev = dev_get_drvdata(dev);
3172 struct jme_adapter *jme = netdev_priv(netdev);
3174 if (!netif_running(netdev))
3177 atomic_dec(&jme->link_changing);
3179 netif_device_detach(netdev);
3180 netif_stop_queue(netdev);
3183 tasklet_disable(&jme->txclean_task);
3184 tasklet_disable(&jme->rxclean_task);
3185 tasklet_disable(&jme->rxempty_task);
3187 if (netif_carrier_ok(netdev)) {
3188 if (test_bit(JME_FLAG_POLL, &jme->flags))
3189 jme_polling_mode(jme);
3191 jme_stop_pcc_timer(jme);
3192 jme_disable_rx_engine(jme);
3193 jme_disable_tx_engine(jme);
3194 jme_reset_mac_processor(jme);
3195 jme_free_rx_resources(jme);
3196 jme_free_tx_resources(jme);
3197 netif_carrier_off(netdev);
3201 tasklet_enable(&jme->txclean_task);
3202 tasklet_enable(&jme->rxclean_task);
3203 tasklet_enable(&jme->rxempty_task);
3205 jme_powersave_phy(jme);
3211 jme_resume(struct device *dev)
3213 struct net_device *netdev = dev_get_drvdata(dev);
3214 struct jme_adapter *jme = netdev_priv(netdev);
3216 if (!netif_running(netdev))
3219 jme_clear_pm_disable_wol(jme);
3221 if (test_bit(JME_FLAG_SSET, &jme->flags))
3222 jme_set_link_ksettings(netdev, &jme->old_cmd);
3224 jme_reset_phy_processor(jme);
3225 jme_phy_calibration(jme);
3227 netif_device_attach(netdev);
3229 atomic_inc(&jme->link_changing);
3231 jme_reset_link(jme);
3238 static SIMPLE_DEV_PM_OPS(jme_pm_ops, jme_suspend, jme_resume);
3239 #define JME_PM_OPS (&jme_pm_ops)
3243 #define JME_PM_OPS NULL
3246 static const struct pci_device_id jme_pci_tbl[] = {
3247 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC250) },
3248 { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC260) },
3252 static struct pci_driver jme_driver = {
3254 .id_table = jme_pci_tbl,
3255 .probe = jme_init_one,
3256 .remove = jme_remove_one,
3257 .shutdown = jme_shutdown,
3258 .driver.pm = JME_PM_OPS,
3262 jme_init_module(void)
3264 pr_info("JMicron JMC2XX ethernet driver version %s\n", DRV_VERSION);
3265 return pci_register_driver(&jme_driver);
3269 jme_cleanup_module(void)
3271 pci_unregister_driver(&jme_driver);
3274 module_init(jme_init_module);
3275 module_exit(jme_cleanup_module);
3277 MODULE_AUTHOR("Guo-Fu Tseng <cooldavid@cooldavid.org>");
3278 MODULE_DESCRIPTION("JMicron JMC2x0 PCI Express Ethernet driver");
3279 MODULE_LICENSE("GPL");
3280 MODULE_VERSION(DRV_VERSION);
3281 MODULE_DEVICE_TABLE(pci, jme_pci_tbl);