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_init_rss_key - Initialize wx RSS key
86 * Allocates and initializes the RSS key if it is not allocated.
88 static inline int ngbe_init_rss_key(struct wx *wx)
93 rss_key = kzalloc(WX_RSS_KEY_SIZE, GFP_KERNEL);
94 if (unlikely(!rss_key))
97 netdev_rss_key_fill(rss_key, WX_RSS_KEY_SIZE);
98 wx->rss_key = rss_key;
105 * ngbe_sw_init - Initialize general software structures
106 * @wx: board private structure to initialize
108 static int ngbe_sw_init(struct wx *wx)
110 struct pci_dev *pdev = wx->pdev;
114 wx->mac.num_rar_entries = NGBE_RAR_ENTRIES;
115 wx->mac.max_rx_queues = NGBE_MAX_RX_QUEUES;
116 wx->mac.max_tx_queues = NGBE_MAX_TX_QUEUES;
117 wx->mac.mcft_size = NGBE_MC_TBL_SIZE;
118 wx->mac.vft_size = NGBE_SP_VFT_TBL_SIZE;
119 wx->mac.rx_pb_size = NGBE_RX_PB_SIZE;
120 wx->mac.tx_pb_size = NGBE_TDB_PB_SZ;
122 /* PCI config space info */
123 err = wx_sw_init(wx);
125 wx_err(wx, "read of internal subsystem device id failed\n");
129 /* mac type, phy type , oem type */
130 ngbe_init_type_code(wx);
132 /* Set common capability flags and settings */
133 wx->max_q_vectors = NGBE_MAX_MSIX_VECTORS;
134 err = wx_get_pcie_msix_counts(wx, &msix_count, NGBE_MAX_MSIX_VECTORS);
136 dev_err(&pdev->dev, "Do not support MSI-X\n");
137 wx->mac.max_msix_vectors = msix_count;
139 if (ngbe_init_rss_key(wx))
142 /* enable itr by default in dynamic mode */
143 wx->rx_itr_setting = 1;
144 wx->tx_itr_setting = 1;
146 /* set default ring sizes */
147 wx->tx_ring_count = NGBE_DEFAULT_TXD;
148 wx->rx_ring_count = NGBE_DEFAULT_RXD;
150 /* set default work limits */
151 wx->tx_work_limit = NGBE_DEFAULT_TX_WORK;
152 wx->rx_work_limit = NGBE_DEFAULT_RX_WORK;
158 * ngbe_irq_enable - Enable default interrupt generation settings
159 * @wx: board private structure
160 * @queues: enable all queues interrupts
162 static void ngbe_irq_enable(struct wx *wx, bool queues)
166 /* enable misc interrupt */
167 mask = NGBE_PX_MISC_IEN_MASK;
169 wr32(wx, WX_GPIO_DDR, WX_GPIO_DDR_0);
170 wr32(wx, WX_GPIO_INTEN, WX_GPIO_INTEN_0 | WX_GPIO_INTEN_1);
171 wr32(wx, WX_GPIO_INTTYPE_LEVEL, 0x0);
172 wr32(wx, WX_GPIO_POLARITY, wx->gpio_ctrl ? 0 : 0x3);
174 wr32(wx, WX_PX_MISC_IEN, mask);
178 wx_intr_enable(wx, NGBE_INTR_ALL);
180 wx_intr_enable(wx, NGBE_INTR_MISC(wx));
184 * ngbe_intr - msi/legacy mode Interrupt Handler
185 * @irq: interrupt number
186 * @data: pointer to a network interface device structure
188 static irqreturn_t ngbe_intr(int __always_unused irq, void *data)
190 struct wx_q_vector *q_vector;
191 struct wx *wx = data;
192 struct pci_dev *pdev;
195 q_vector = wx->q_vector[0];
198 eicr = wx_misc_isb(wx, WX_ISB_VEC0);
200 /* shared interrupt alert!
201 * the interrupt that we masked before the EICR read.
203 if (netif_running(wx->netdev))
204 ngbe_irq_enable(wx, true);
205 return IRQ_NONE; /* Not our interrupt */
207 wx->isb_mem[WX_ISB_VEC0] = 0;
208 if (!(pdev->msi_enabled))
209 wr32(wx, WX_PX_INTA, 1);
211 wx->isb_mem[WX_ISB_MISC] = 0;
212 /* would disable interrupts here but it is auto disabled */
213 napi_schedule_irqoff(&q_vector->napi);
215 if (netif_running(wx->netdev))
216 ngbe_irq_enable(wx, false);
221 static irqreturn_t ngbe_msix_other(int __always_unused irq, void *data)
223 struct wx *wx = data;
225 /* re-enable the original interrupt state, no lsc, no queues */
226 if (netif_running(wx->netdev))
227 ngbe_irq_enable(wx, false);
233 * ngbe_request_msix_irqs - Initialize MSI-X interrupts
234 * @wx: board private structure
236 * ngbe_request_msix_irqs allocates MSI-X vectors and requests
237 * interrupts from the kernel.
239 static int ngbe_request_msix_irqs(struct wx *wx)
241 struct net_device *netdev = wx->netdev;
244 for (vector = 0; vector < wx->num_q_vectors; vector++) {
245 struct wx_q_vector *q_vector = wx->q_vector[vector];
246 struct msix_entry *entry = &wx->msix_entries[vector];
248 if (q_vector->tx.ring && q_vector->rx.ring)
249 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
250 "%s-TxRx-%d", netdev->name, entry->entry);
252 /* skip this unused q_vector */
255 err = request_irq(entry->vector, wx_msix_clean_rings, 0,
256 q_vector->name, q_vector);
258 wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
259 q_vector->name, err);
260 goto free_queue_irqs;
264 err = request_irq(wx->msix_entries[vector].vector,
265 ngbe_msix_other, 0, netdev->name, wx);
268 wx_err(wx, "request_irq for msix_other failed: %d\n", err);
269 goto free_queue_irqs;
277 free_irq(wx->msix_entries[vector].vector,
278 wx->q_vector[vector]);
280 wx_reset_interrupt_capability(wx);
285 * ngbe_request_irq - initialize interrupts
286 * @wx: board private structure
288 * Attempts to configure interrupts using the best available
289 * capabilities of the hardware and kernel.
291 static int ngbe_request_irq(struct wx *wx)
293 struct net_device *netdev = wx->netdev;
294 struct pci_dev *pdev = wx->pdev;
297 if (pdev->msix_enabled)
298 err = ngbe_request_msix_irqs(wx);
299 else if (pdev->msi_enabled)
300 err = request_irq(pdev->irq, ngbe_intr, 0,
303 err = request_irq(pdev->irq, ngbe_intr, IRQF_SHARED,
307 wx_err(wx, "request_irq failed, Error %d\n", err);
312 static void ngbe_disable_device(struct wx *wx)
314 struct net_device *netdev = wx->netdev;
317 /* disable all enabled rx queues */
318 for (i = 0; i < wx->num_rx_queues; i++)
319 /* this call also flushes the previous write */
320 wx_disable_rx_queue(wx, wx->rx_ring[i]);
321 /* disable receives */
323 wx_napi_disable_all(wx);
324 netif_tx_stop_all_queues(netdev);
325 netif_tx_disable(netdev);
327 ngbe_sfp_modules_txrx_powerctl(wx, false);
329 /* disable transmits in the hardware now that interrupts are off */
330 for (i = 0; i < wx->num_tx_queues; i++) {
331 u8 reg_idx = wx->tx_ring[i]->reg_idx;
333 wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
339 static void ngbe_down(struct wx *wx)
341 phy_stop(wx->phydev);
342 ngbe_disable_device(wx);
343 wx_clean_all_tx_rings(wx);
344 wx_clean_all_rx_rings(wx);
347 static void ngbe_up(struct wx *wx)
349 wx_configure_vectors(wx);
351 /* make sure to complete pre-operations */
352 smp_mb__before_atomic();
353 wx_napi_enable_all(wx);
354 /* enable transmits */
355 netif_tx_start_all_queues(wx->netdev);
357 /* clear any pending interrupts, may auto mask */
358 rd32(wx, WX_PX_IC(0));
359 rd32(wx, WX_PX_MISC_IC);
360 ngbe_irq_enable(wx, true);
362 ngbe_sfp_modules_txrx_powerctl(wx, true);
364 phy_start(wx->phydev);
368 * ngbe_open - Called when a network interface is made active
369 * @netdev: network interface device structure
371 * Returns 0 on success, negative value on failure
373 * The open entry point is called when a network interface is made
374 * active by the system (IFF_UP).
376 static int ngbe_open(struct net_device *netdev)
378 struct wx *wx = netdev_priv(netdev);
381 wx_control_hw(wx, true);
383 err = wx_setup_resources(wx);
389 err = ngbe_request_irq(wx);
391 goto err_free_resources;
393 err = ngbe_phy_connect(wx);
397 err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
401 err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
409 phy_disconnect(wx->phydev);
413 wx_free_resources(wx);
418 * ngbe_close - Disables a network interface
419 * @netdev: network interface device structure
421 * Returns 0, this is not allowed to fail
423 * The close entry point is called when an interface is de-activated
424 * by the OS. The hardware is still under the drivers control, but
425 * needs to be disabled. A global MAC reset is issued to stop the
426 * hardware, and all transmit and receive resources are freed.
428 static int ngbe_close(struct net_device *netdev)
430 struct wx *wx = netdev_priv(netdev);
434 wx_free_resources(wx);
435 phy_disconnect(wx->phydev);
436 wx_control_hw(wx, false);
441 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
443 struct wx *wx = pci_get_drvdata(pdev);
444 struct net_device *netdev;
449 netif_device_detach(netdev);
451 if (netif_running(netdev))
453 wx_clear_interrupt_scheme(wx);
457 wx_set_rx_mode(netdev);
459 wr32(wx, NGBE_PSR_WKUP_CTL, wufc);
461 wr32(wx, NGBE_PSR_WKUP_CTL, 0);
463 pci_wake_from_d3(pdev, !!wufc);
464 *enable_wake = !!wufc;
465 wx_control_hw(wx, false);
467 pci_disable_device(pdev);
470 static void ngbe_shutdown(struct pci_dev *pdev)
472 struct wx *wx = pci_get_drvdata(pdev);
477 ngbe_dev_shutdown(pdev, &wake);
479 if (system_state == SYSTEM_POWER_OFF) {
480 pci_wake_from_d3(pdev, wake);
481 pci_set_power_state(pdev, PCI_D3hot);
485 static const struct net_device_ops ngbe_netdev_ops = {
486 .ndo_open = ngbe_open,
487 .ndo_stop = ngbe_close,
488 .ndo_change_mtu = wx_change_mtu,
489 .ndo_start_xmit = wx_xmit_frame,
490 .ndo_set_rx_mode = wx_set_rx_mode,
491 .ndo_set_features = wx_set_features,
492 .ndo_validate_addr = eth_validate_addr,
493 .ndo_set_mac_address = wx_set_mac,
494 .ndo_get_stats64 = wx_get_stats64,
495 .ndo_vlan_rx_add_vid = wx_vlan_rx_add_vid,
496 .ndo_vlan_rx_kill_vid = wx_vlan_rx_kill_vid,
500 * ngbe_probe - Device Initialization Routine
501 * @pdev: PCI device information struct
502 * @ent: entry in ngbe_pci_tbl
504 * Returns 0 on success, negative on failure
506 * ngbe_probe initializes an wx identified by a pci_dev structure.
507 * The OS initialization, configuring of the wx private structure,
508 * and a hardware reset occur.
510 static int ngbe_probe(struct pci_dev *pdev,
511 const struct pci_device_id __always_unused *ent)
513 struct net_device *netdev;
514 u32 e2rom_cksum_cap = 0;
515 struct wx *wx = NULL;
516 static int func_nums;
522 err = pci_enable_device_mem(pdev);
526 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
529 "No usable DMA configuration, aborting\n");
530 goto err_pci_disable_dev;
533 err = pci_request_selected_regions(pdev,
534 pci_select_bars(pdev, IORESOURCE_MEM),
538 "pci_request_selected_regions failed %d\n", err);
539 goto err_pci_disable_dev;
542 pci_set_master(pdev);
544 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
550 goto err_pci_release_regions;
553 SET_NETDEV_DEV(netdev, &pdev->dev);
555 wx = netdev_priv(netdev);
558 wx->msg_enable = BIT(3) - 1;
560 wx->hw_addr = devm_ioremap(&pdev->dev,
561 pci_resource_start(pdev, 0),
562 pci_resource_len(pdev, 0));
565 goto err_pci_release_regions;
568 wx->driver_name = ngbe_driver_name;
569 ngbe_set_ethtool_ops(netdev);
570 netdev->netdev_ops = &ngbe_netdev_ops;
572 netdev->features = NETIF_F_SG | NETIF_F_IP_CSUM |
573 NETIF_F_TSO | NETIF_F_TSO6 |
574 NETIF_F_RXHASH | NETIF_F_RXCSUM;
575 netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_TSO_MANGLEID;
576 netdev->vlan_features |= netdev->features;
577 netdev->features |= NETIF_F_IPV6_CSUM | NETIF_F_VLAN_FEATURES;
578 /* copy netdev features into list of user selectable features */
579 netdev->hw_features |= netdev->features | NETIF_F_RXALL;
580 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
581 netdev->features |= NETIF_F_HIGHDMA;
582 netdev->hw_features |= NETIF_F_GRO;
583 netdev->features |= NETIF_F_GRO;
585 netdev->priv_flags |= IFF_UNICAST_FLT;
586 netdev->priv_flags |= IFF_SUPP_NOFCS;
588 netdev->min_mtu = ETH_MIN_MTU;
589 netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
590 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
592 wx->bd_number = func_nums;
593 /* setup the private structure */
594 err = ngbe_sw_init(wx);
596 goto err_free_mac_table;
598 /* check if flash load is done after hw power up */
599 err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PERST);
601 goto err_free_mac_table;
602 err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PWRRST);
604 goto err_free_mac_table;
606 err = wx_mng_present(wx);
608 dev_err(&pdev->dev, "Management capability is not present\n");
609 goto err_free_mac_table;
612 err = ngbe_reset_hw(wx);
614 dev_err(&pdev->dev, "HW Init failed: %d\n", err);
615 goto err_free_mac_table;
618 if (wx->bus.func == 0) {
619 wr32(wx, NGBE_CALSUM_CAP_STATUS, 0x0);
620 wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
622 e2rom_cksum_cap = rd32(wx, NGBE_CALSUM_CAP_STATUS);
623 saved_ver = rd32(wx, NGBE_EEPROM_VERSION_STORE_REG);
626 wx_init_eeprom_params(wx);
627 if (wx->bus.func == 0 || e2rom_cksum_cap == 0) {
628 /* make sure the EEPROM is ready */
629 err = ngbe_eeprom_chksum_hostif(wx);
631 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
633 goto err_free_mac_table;
638 if (wx->wol_hw_supported)
639 wx->wol = NGBE_PSR_WKUP_CTL_MAG;
641 netdev->wol_enabled = !!(wx->wol);
642 wr32(wx, NGBE_PSR_WKUP_CTL, wx->wol);
643 device_set_wakeup_enable(&pdev->dev, wx->wol);
645 /* Save off EEPROM version number and Option Rom version which
646 * together make a unique identify for the eeprom
649 etrack_id = saved_ver;
651 wx_read_ee_hostif(wx,
652 wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H,
654 etrack_id = e2rom_ver << 16;
655 wx_read_ee_hostif(wx,
656 wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L,
658 etrack_id |= e2rom_ver;
659 wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
661 snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
662 "0x%08x", etrack_id);
664 eth_hw_addr_set(netdev, wx->mac.perm_addr);
665 wx_mac_set_default_filter(wx, wx->mac.perm_addr);
667 err = wx_init_interrupt_scheme(wx);
669 goto err_free_mac_table;
671 /* phy Interface Configuration */
672 err = ngbe_mdio_init(wx);
674 goto err_clear_interrupt_scheme;
676 err = register_netdev(netdev);
680 pci_set_drvdata(pdev, wx);
685 wx_control_hw(wx, false);
686 err_clear_interrupt_scheme:
687 wx_clear_interrupt_scheme(wx);
689 kfree(wx->mac_table);
690 err_pci_release_regions:
691 pci_release_selected_regions(pdev,
692 pci_select_bars(pdev, IORESOURCE_MEM));
694 pci_disable_device(pdev);
699 * ngbe_remove - Device Removal Routine
700 * @pdev: PCI device information struct
702 * ngbe_remove is called by the PCI subsystem to alert the driver
703 * that it should release a PCI device. The could be caused by a
704 * Hot-Plug event, or because the driver is going to be removed from
707 static void ngbe_remove(struct pci_dev *pdev)
709 struct wx *wx = pci_get_drvdata(pdev);
710 struct net_device *netdev;
713 unregister_netdev(netdev);
714 pci_release_selected_regions(pdev,
715 pci_select_bars(pdev, IORESOURCE_MEM));
717 kfree(wx->mac_table);
718 wx_clear_interrupt_scheme(wx);
720 pci_disable_device(pdev);
723 static int ngbe_suspend(struct pci_dev *pdev, pm_message_t state)
727 ngbe_dev_shutdown(pdev, &wake);
728 device_set_wakeup_enable(&pdev->dev, wake);
733 static int ngbe_resume(struct pci_dev *pdev)
735 struct net_device *netdev;
739 wx = pci_get_drvdata(pdev);
742 err = pci_enable_device_mem(pdev);
744 wx_err(wx, "Cannot enable PCI device from suspend\n");
747 pci_set_master(pdev);
748 device_wakeup_disable(&pdev->dev);
752 err = wx_init_interrupt_scheme(wx);
753 if (!err && netif_running(netdev))
754 err = ngbe_open(netdev);
756 netif_device_attach(netdev);
762 static struct pci_driver ngbe_driver = {
763 .name = ngbe_driver_name,
764 .id_table = ngbe_pci_tbl,
766 .remove = ngbe_remove,
767 .suspend = ngbe_suspend,
768 .resume = ngbe_resume,
769 .shutdown = ngbe_shutdown,
772 module_pci_driver(ngbe_driver);
774 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
775 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
776 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
777 MODULE_LICENSE("GPL");