1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 - 2022 Beijing WangXun Technology Co., Ltd. */
4 #include <linux/types.h>
5 #include <linux/module.h>
7 #include <linux/netdevice.h>
8 #include <linux/string.h>
9 #include <linux/etherdevice.h>
11 #include <linux/phy.h>
12 #include <linux/if_vlan.h>
14 #include "../libwx/wx_type.h"
15 #include "../libwx/wx_hw.h"
16 #include "../libwx/wx_lib.h"
17 #include "ngbe_type.h"
18 #include "ngbe_mdio.h"
20 #include "ngbe_ethtool.h"
22 char ngbe_driver_name[] = "ngbe";
24 /* ngbe_pci_tbl - PCI Device ID Table
26 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
27 * Class, Class Mask, private data (not used) }
29 static const struct pci_device_id ngbe_pci_tbl[] = {
30 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL_W), 0},
31 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2), 0},
32 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2S), 0},
33 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4), 0},
34 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4S), 0},
35 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2), 0},
36 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2S), 0},
37 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4), 0},
38 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4S), 0},
39 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860LC), 0},
40 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1), 0},
41 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1L), 0},
42 /* required last entry */
47 * ngbe_init_type_code - Initialize the shared code
48 * @wx: pointer to hardware structure
50 static void ngbe_init_type_code(struct wx *wx)
52 int wol_mask = 0, ncsi_mask = 0;
53 u16 type_mask = 0, val;
55 wx->mac.type = wx_mac_em;
56 type_mask = (u16)(wx->subsystem_device_id & NGBE_OEM_MASK);
57 ncsi_mask = wx->subsystem_device_id & NGBE_NCSI_MASK;
58 wol_mask = wx->subsystem_device_id & NGBE_WOL_MASK;
60 val = rd32(wx, WX_CFG_PORT_ST);
61 wx->mac_type = (val & BIT(7)) >> 7 ?
65 wx->wol_hw_supported = (wol_mask == NGBE_WOL_SUP) ? 1 : 0;
66 wx->ncsi_enabled = (ncsi_mask == NGBE_NCSI_MASK ||
67 type_mask == NGBE_SUBID_OCP_CARD) ? 1 : 0;
70 case NGBE_SUBID_LY_YT8521S_SFP:
71 case NGBE_SUBID_LY_M88E1512_SFP:
72 case NGBE_SUBID_YT8521S_SFP_GPIO:
73 case NGBE_SUBID_INTERNAL_YT8521S_SFP_GPIO:
83 * ngbe_sw_init - Initialize general software structures
84 * @wx: board private structure to initialize
86 static int ngbe_sw_init(struct wx *wx)
88 struct pci_dev *pdev = wx->pdev;
92 wx->mac.num_rar_entries = NGBE_RAR_ENTRIES;
93 wx->mac.max_rx_queues = NGBE_MAX_RX_QUEUES;
94 wx->mac.max_tx_queues = NGBE_MAX_TX_QUEUES;
95 wx->mac.mcft_size = NGBE_MC_TBL_SIZE;
96 wx->mac.vft_size = NGBE_SP_VFT_TBL_SIZE;
97 wx->mac.rx_pb_size = NGBE_RX_PB_SIZE;
98 wx->mac.tx_pb_size = NGBE_TDB_PB_SZ;
100 /* PCI config space info */
101 err = wx_sw_init(wx);
105 /* mac type, phy type , oem type */
106 ngbe_init_type_code(wx);
108 /* Set common capability flags and settings */
109 wx->max_q_vectors = NGBE_MAX_MSIX_VECTORS;
110 err = wx_get_pcie_msix_counts(wx, &msix_count, NGBE_MAX_MSIX_VECTORS);
112 dev_err(&pdev->dev, "Do not support MSI-X\n");
113 wx->mac.max_msix_vectors = msix_count;
115 wx->ring_feature[RING_F_RSS].limit = min_t(int, NGBE_MAX_RSS_INDICES,
117 wx->rss_enabled = true;
119 /* enable itr by default in dynamic mode */
120 wx->rx_itr_setting = 1;
121 wx->tx_itr_setting = 1;
123 /* set default ring sizes */
124 wx->tx_ring_count = NGBE_DEFAULT_TXD;
125 wx->rx_ring_count = NGBE_DEFAULT_RXD;
127 /* set default work limits */
128 wx->tx_work_limit = NGBE_DEFAULT_TX_WORK;
129 wx->rx_work_limit = NGBE_DEFAULT_RX_WORK;
135 * ngbe_irq_enable - Enable default interrupt generation settings
136 * @wx: board private structure
137 * @queues: enable all queues interrupts
139 static void ngbe_irq_enable(struct wx *wx, bool queues)
143 /* enable misc interrupt */
144 mask = NGBE_PX_MISC_IEN_MASK;
146 wr32(wx, WX_GPIO_DDR, WX_GPIO_DDR_0);
147 wr32(wx, WX_GPIO_INTEN, WX_GPIO_INTEN_0 | WX_GPIO_INTEN_1);
148 wr32(wx, WX_GPIO_INTTYPE_LEVEL, 0x0);
149 wr32(wx, WX_GPIO_POLARITY, wx->gpio_ctrl ? 0 : 0x3);
151 wr32(wx, WX_PX_MISC_IEN, mask);
155 wx_intr_enable(wx, NGBE_INTR_ALL);
157 wx_intr_enable(wx, NGBE_INTR_MISC);
161 * ngbe_intr - msi/legacy mode Interrupt Handler
162 * @irq: interrupt number
163 * @data: pointer to a network interface device structure
165 static irqreturn_t ngbe_intr(int __always_unused irq, void *data)
167 struct wx_q_vector *q_vector;
168 struct wx *wx = data;
169 struct pci_dev *pdev;
172 q_vector = wx->q_vector[0];
175 eicr = wx_misc_isb(wx, WX_ISB_VEC0);
177 /* shared interrupt alert!
178 * the interrupt that we masked before the EICR read.
180 if (netif_running(wx->netdev))
181 ngbe_irq_enable(wx, true);
182 return IRQ_NONE; /* Not our interrupt */
184 wx->isb_mem[WX_ISB_VEC0] = 0;
185 if (!(pdev->msi_enabled))
186 wr32(wx, WX_PX_INTA, 1);
188 wx->isb_mem[WX_ISB_MISC] = 0;
189 /* would disable interrupts here but it is auto disabled */
190 napi_schedule_irqoff(&q_vector->napi);
192 if (netif_running(wx->netdev))
193 ngbe_irq_enable(wx, false);
198 static irqreturn_t ngbe_msix_other(int __always_unused irq, void *data)
200 struct wx *wx = data;
202 /* re-enable the original interrupt state, no lsc, no queues */
203 if (netif_running(wx->netdev))
204 ngbe_irq_enable(wx, false);
210 * ngbe_request_msix_irqs - Initialize MSI-X interrupts
211 * @wx: board private structure
213 * ngbe_request_msix_irqs allocates MSI-X vectors and requests
214 * interrupts from the kernel.
216 static int ngbe_request_msix_irqs(struct wx *wx)
218 struct net_device *netdev = wx->netdev;
221 for (vector = 0; vector < wx->num_q_vectors; vector++) {
222 struct wx_q_vector *q_vector = wx->q_vector[vector];
223 struct msix_entry *entry = &wx->msix_q_entries[vector];
225 if (q_vector->tx.ring && q_vector->rx.ring)
226 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
227 "%s-TxRx-%d", netdev->name, entry->entry);
229 /* skip this unused q_vector */
232 err = request_irq(entry->vector, wx_msix_clean_rings, 0,
233 q_vector->name, q_vector);
235 wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
236 q_vector->name, err);
237 goto free_queue_irqs;
241 err = request_irq(wx->msix_entry->vector,
242 ngbe_msix_other, 0, netdev->name, wx);
245 wx_err(wx, "request_irq for msix_other failed: %d\n", err);
246 goto free_queue_irqs;
254 free_irq(wx->msix_q_entries[vector].vector,
255 wx->q_vector[vector]);
257 wx_reset_interrupt_capability(wx);
262 * ngbe_request_irq - initialize interrupts
263 * @wx: board private structure
265 * Attempts to configure interrupts using the best available
266 * capabilities of the hardware and kernel.
268 static int ngbe_request_irq(struct wx *wx)
270 struct net_device *netdev = wx->netdev;
271 struct pci_dev *pdev = wx->pdev;
274 if (pdev->msix_enabled)
275 err = ngbe_request_msix_irqs(wx);
276 else if (pdev->msi_enabled)
277 err = request_irq(pdev->irq, ngbe_intr, 0,
280 err = request_irq(pdev->irq, ngbe_intr, IRQF_SHARED,
284 wx_err(wx, "request_irq failed, Error %d\n", err);
289 static void ngbe_disable_device(struct wx *wx)
291 struct net_device *netdev = wx->netdev;
294 /* disable all enabled rx queues */
295 for (i = 0; i < wx->num_rx_queues; i++)
296 /* this call also flushes the previous write */
297 wx_disable_rx_queue(wx, wx->rx_ring[i]);
298 /* disable receives */
300 wx_napi_disable_all(wx);
301 netif_tx_stop_all_queues(netdev);
302 netif_tx_disable(netdev);
304 ngbe_sfp_modules_txrx_powerctl(wx, false);
306 /* disable transmits in the hardware now that interrupts are off */
307 for (i = 0; i < wx->num_tx_queues; i++) {
308 u8 reg_idx = wx->tx_ring[i]->reg_idx;
310 wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
316 void ngbe_down(struct wx *wx)
318 phylink_stop(wx->phylink);
319 ngbe_disable_device(wx);
320 wx_clean_all_tx_rings(wx);
321 wx_clean_all_rx_rings(wx);
324 void ngbe_up(struct wx *wx)
326 wx_configure_vectors(wx);
328 /* make sure to complete pre-operations */
329 smp_mb__before_atomic();
330 wx_napi_enable_all(wx);
331 /* enable transmits */
332 netif_tx_start_all_queues(wx->netdev);
334 /* clear any pending interrupts, may auto mask */
335 rd32(wx, WX_PX_IC(0));
336 rd32(wx, WX_PX_MISC_IC);
337 ngbe_irq_enable(wx, true);
339 ngbe_sfp_modules_txrx_powerctl(wx, true);
341 phylink_start(wx->phylink);
345 * ngbe_open - Called when a network interface is made active
346 * @netdev: network interface device structure
348 * Returns 0 on success, negative value on failure
350 * The open entry point is called when a network interface is made
351 * active by the system (IFF_UP).
353 static int ngbe_open(struct net_device *netdev)
355 struct wx *wx = netdev_priv(netdev);
358 wx_control_hw(wx, true);
360 err = wx_setup_resources(wx);
366 err = ngbe_request_irq(wx);
368 goto err_free_resources;
370 err = phylink_connect_phy(wx->phylink, wx->phydev);
374 err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
378 err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
386 phylink_disconnect_phy(wx->phylink);
390 wx_free_resources(wx);
395 * ngbe_close - Disables a network interface
396 * @netdev: network interface device structure
398 * Returns 0, this is not allowed to fail
400 * The close entry point is called when an interface is de-activated
401 * by the OS. The hardware is still under the drivers control, but
402 * needs to be disabled. A global MAC reset is issued to stop the
403 * hardware, and all transmit and receive resources are freed.
405 static int ngbe_close(struct net_device *netdev)
407 struct wx *wx = netdev_priv(netdev);
411 wx_free_resources(wx);
412 phylink_disconnect_phy(wx->phylink);
413 wx_control_hw(wx, false);
418 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
420 struct wx *wx = pci_get_drvdata(pdev);
421 struct net_device *netdev;
426 netif_device_detach(netdev);
428 if (netif_running(netdev))
430 wx_clear_interrupt_scheme(wx);
434 wx_set_rx_mode(netdev);
436 wr32(wx, NGBE_PSR_WKUP_CTL, wufc);
438 wr32(wx, NGBE_PSR_WKUP_CTL, 0);
440 pci_wake_from_d3(pdev, !!wufc);
441 *enable_wake = !!wufc;
442 wx_control_hw(wx, false);
444 pci_disable_device(pdev);
447 static void ngbe_shutdown(struct pci_dev *pdev)
449 struct wx *wx = pci_get_drvdata(pdev);
454 ngbe_dev_shutdown(pdev, &wake);
456 if (system_state == SYSTEM_POWER_OFF) {
457 pci_wake_from_d3(pdev, wake);
458 pci_set_power_state(pdev, PCI_D3hot);
463 * ngbe_setup_tc - routine to configure net_device for multiple traffic
466 * @dev: net device to configure
467 * @tc: number of traffic classes to enable
469 int ngbe_setup_tc(struct net_device *dev, u8 tc)
471 struct wx *wx = netdev_priv(dev);
473 /* Hardware has to reinitialize queues and interrupts to
474 * match packet buffer alignment. Unfortunately, the
475 * hardware is not flexible enough to do this dynamically.
477 if (netif_running(dev))
480 wx_clear_interrupt_scheme(wx);
483 netdev_set_num_tc(dev, tc);
485 netdev_reset_tc(dev);
487 wx_init_interrupt_scheme(wx);
489 if (netif_running(dev))
495 static const struct net_device_ops ngbe_netdev_ops = {
496 .ndo_open = ngbe_open,
497 .ndo_stop = ngbe_close,
498 .ndo_change_mtu = wx_change_mtu,
499 .ndo_start_xmit = wx_xmit_frame,
500 .ndo_set_rx_mode = wx_set_rx_mode,
501 .ndo_set_features = wx_set_features,
502 .ndo_validate_addr = eth_validate_addr,
503 .ndo_set_mac_address = wx_set_mac,
504 .ndo_get_stats64 = wx_get_stats64,
505 .ndo_vlan_rx_add_vid = wx_vlan_rx_add_vid,
506 .ndo_vlan_rx_kill_vid = wx_vlan_rx_kill_vid,
510 * ngbe_probe - Device Initialization Routine
511 * @pdev: PCI device information struct
512 * @ent: entry in ngbe_pci_tbl
514 * Returns 0 on success, negative on failure
516 * ngbe_probe initializes an wx identified by a pci_dev structure.
517 * The OS initialization, configuring of the wx private structure,
518 * and a hardware reset occur.
520 static int ngbe_probe(struct pci_dev *pdev,
521 const struct pci_device_id __always_unused *ent)
523 struct net_device *netdev;
524 u32 e2rom_cksum_cap = 0;
525 struct wx *wx = NULL;
526 static int func_nums;
532 err = pci_enable_device_mem(pdev);
536 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
539 "No usable DMA configuration, aborting\n");
540 goto err_pci_disable_dev;
543 err = pci_request_selected_regions(pdev,
544 pci_select_bars(pdev, IORESOURCE_MEM),
548 "pci_request_selected_regions failed %d\n", err);
549 goto err_pci_disable_dev;
552 pci_set_master(pdev);
554 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
560 goto err_pci_release_regions;
563 SET_NETDEV_DEV(netdev, &pdev->dev);
565 wx = netdev_priv(netdev);
568 wx->msg_enable = BIT(3) - 1;
570 wx->hw_addr = devm_ioremap(&pdev->dev,
571 pci_resource_start(pdev, 0),
572 pci_resource_len(pdev, 0));
575 goto err_pci_release_regions;
578 wx->driver_name = ngbe_driver_name;
579 ngbe_set_ethtool_ops(netdev);
580 netdev->netdev_ops = &ngbe_netdev_ops;
582 netdev->features = NETIF_F_SG | NETIF_F_IP_CSUM |
583 NETIF_F_TSO | NETIF_F_TSO6 |
584 NETIF_F_RXHASH | NETIF_F_RXCSUM;
585 netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_TSO_MANGLEID;
586 netdev->vlan_features |= netdev->features;
587 netdev->features |= NETIF_F_IPV6_CSUM | NETIF_F_VLAN_FEATURES;
588 /* copy netdev features into list of user selectable features */
589 netdev->hw_features |= netdev->features | NETIF_F_RXALL;
590 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
591 netdev->features |= NETIF_F_HIGHDMA;
592 netdev->hw_features |= NETIF_F_GRO;
593 netdev->features |= NETIF_F_GRO;
595 netdev->priv_flags |= IFF_UNICAST_FLT;
596 netdev->priv_flags |= IFF_SUPP_NOFCS;
597 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
599 netdev->min_mtu = ETH_MIN_MTU;
600 netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
601 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
603 wx->bd_number = func_nums;
604 /* setup the private structure */
605 err = ngbe_sw_init(wx);
607 goto err_free_mac_table;
609 /* check if flash load is done after hw power up */
610 err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PERST);
612 goto err_free_mac_table;
613 err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PWRRST);
615 goto err_free_mac_table;
617 err = wx_mng_present(wx);
619 dev_err(&pdev->dev, "Management capability is not present\n");
620 goto err_free_mac_table;
623 err = ngbe_reset_hw(wx);
625 dev_err(&pdev->dev, "HW Init failed: %d\n", err);
626 goto err_free_mac_table;
629 if (wx->bus.func == 0) {
630 wr32(wx, NGBE_CALSUM_CAP_STATUS, 0x0);
631 wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
633 e2rom_cksum_cap = rd32(wx, NGBE_CALSUM_CAP_STATUS);
634 saved_ver = rd32(wx, NGBE_EEPROM_VERSION_STORE_REG);
637 wx_init_eeprom_params(wx);
638 if (wx->bus.func == 0 || e2rom_cksum_cap == 0) {
639 /* make sure the EEPROM is ready */
640 err = ngbe_eeprom_chksum_hostif(wx);
642 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
644 goto err_free_mac_table;
649 if (wx->wol_hw_supported)
650 wx->wol = NGBE_PSR_WKUP_CTL_MAG;
652 netdev->wol_enabled = !!(wx->wol);
653 wr32(wx, NGBE_PSR_WKUP_CTL, wx->wol);
654 device_set_wakeup_enable(&pdev->dev, wx->wol);
656 /* Save off EEPROM version number and Option Rom version which
657 * together make a unique identify for the eeprom
660 etrack_id = saved_ver;
662 wx_read_ee_hostif(wx,
663 wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H,
665 etrack_id = e2rom_ver << 16;
666 wx_read_ee_hostif(wx,
667 wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L,
669 etrack_id |= e2rom_ver;
670 wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
672 snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
673 "0x%08x", etrack_id);
675 eth_hw_addr_set(netdev, wx->mac.perm_addr);
676 wx_mac_set_default_filter(wx, wx->mac.perm_addr);
678 err = wx_init_interrupt_scheme(wx);
680 goto err_free_mac_table;
682 /* phy Interface Configuration */
683 err = ngbe_mdio_init(wx);
685 goto err_clear_interrupt_scheme;
687 err = register_netdev(netdev);
691 pci_set_drvdata(pdev, wx);
696 phylink_destroy(wx->phylink);
697 wx_control_hw(wx, false);
698 err_clear_interrupt_scheme:
699 wx_clear_interrupt_scheme(wx);
701 kfree(wx->mac_table);
702 err_pci_release_regions:
703 pci_release_selected_regions(pdev,
704 pci_select_bars(pdev, IORESOURCE_MEM));
706 pci_disable_device(pdev);
711 * ngbe_remove - Device Removal Routine
712 * @pdev: PCI device information struct
714 * ngbe_remove is called by the PCI subsystem to alert the driver
715 * that it should release a PCI device. The could be caused by a
716 * Hot-Plug event, or because the driver is going to be removed from
719 static void ngbe_remove(struct pci_dev *pdev)
721 struct wx *wx = pci_get_drvdata(pdev);
722 struct net_device *netdev;
725 unregister_netdev(netdev);
726 phylink_destroy(wx->phylink);
727 pci_release_selected_regions(pdev,
728 pci_select_bars(pdev, IORESOURCE_MEM));
731 kfree(wx->mac_table);
732 wx_clear_interrupt_scheme(wx);
734 pci_disable_device(pdev);
737 static int ngbe_suspend(struct pci_dev *pdev, pm_message_t state)
741 ngbe_dev_shutdown(pdev, &wake);
742 device_set_wakeup_enable(&pdev->dev, wake);
747 static int ngbe_resume(struct pci_dev *pdev)
749 struct net_device *netdev;
753 wx = pci_get_drvdata(pdev);
756 err = pci_enable_device_mem(pdev);
758 wx_err(wx, "Cannot enable PCI device from suspend\n");
761 pci_set_master(pdev);
762 device_wakeup_disable(&pdev->dev);
766 err = wx_init_interrupt_scheme(wx);
767 if (!err && netif_running(netdev))
768 err = ngbe_open(netdev);
770 netif_device_attach(netdev);
776 static struct pci_driver ngbe_driver = {
777 .name = ngbe_driver_name,
778 .id_table = ngbe_pci_tbl,
780 .remove = ngbe_remove,
781 .suspend = ngbe_suspend,
782 .resume = ngbe_resume,
783 .shutdown = ngbe_shutdown,
786 module_pci_driver(ngbe_driver);
788 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
789 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
790 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
791 MODULE_LICENSE("GPL");