1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2015 - 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>
10 #include <linux/etherdevice.h>
13 #include "../libwx/wx_type.h"
14 #include "../libwx/wx_lib.h"
15 #include "../libwx/wx_hw.h"
16 #include "txgbe_type.h"
18 #include "txgbe_ethtool.h"
20 char txgbe_driver_name[] = "txgbe";
22 /* txgbe_pci_tbl - PCI Device ID Table
24 * Wildcard entries (PCI_ANY_ID) should come last
25 * Last entry must be all 0s
27 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
28 * Class, Class Mask, private data (not used) }
30 static const struct pci_device_id txgbe_pci_tbl[] = {
31 { PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_SP1000), 0},
32 { PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_WX1820), 0},
33 /* required last entry */
37 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
39 static void txgbe_check_minimum_link(struct wx *wx)
44 pcie_print_link_status(pdev);
48 * txgbe_enumerate_functions - Get the number of ports this device has
51 * This function enumerates the phsyical functions co-located on a single slot,
52 * in order to determine how many ports a device has. This is most useful in
53 * determining the required GT/s of PCIe bandwidth necessary for optimal
56 static int txgbe_enumerate_functions(struct wx *wx)
58 struct pci_dev *entry, *pdev = wx->pdev;
61 list_for_each_entry(entry, &pdev->bus->devices, bus_list) {
62 /* When the devices on the bus don't all match our device ID,
63 * we can't reliably determine the correct number of
64 * functions. This can occur if a function has been direct
65 * attached to a virtual machine using VT-d.
67 if (entry->vendor != pdev->vendor ||
68 entry->device != pdev->device)
78 * txgbe_irq_enable - Enable default interrupt generation settings
79 * @wx: pointer to private structure
80 * @queues: enable irqs for queues
82 static void txgbe_irq_enable(struct wx *wx, bool queues)
84 /* unmask interrupt */
85 wx_intr_enable(wx, TXGBE_INTR_MISC(wx));
87 wx_intr_enable(wx, TXGBE_INTR_QALL(wx));
91 * txgbe_intr - msi/legacy mode Interrupt Handler
92 * @irq: interrupt number
93 * @data: pointer to a network interface device structure
95 static irqreturn_t txgbe_intr(int __always_unused irq, void *data)
97 struct wx_q_vector *q_vector;
102 q_vector = wx->q_vector[0];
105 eicr = wx_misc_isb(wx, WX_ISB_VEC0);
107 /* shared interrupt alert!
108 * the interrupt that we masked before the ICR read.
110 if (netif_running(wx->netdev))
111 txgbe_irq_enable(wx, true);
112 return IRQ_NONE; /* Not our interrupt */
114 wx->isb_mem[WX_ISB_VEC0] = 0;
115 if (!(pdev->msi_enabled))
116 wr32(wx, WX_PX_INTA, 1);
118 wx->isb_mem[WX_ISB_MISC] = 0;
119 /* would disable interrupts here but it is auto disabled */
120 napi_schedule_irqoff(&q_vector->napi);
122 /* re-enable link(maybe) and non-queue interrupts, no flush.
123 * txgbe_poll will re-enable the queue interrupts
125 if (netif_running(wx->netdev))
126 txgbe_irq_enable(wx, false);
131 static irqreturn_t txgbe_msix_other(int __always_unused irq, void *data)
133 struct wx *wx = data;
135 /* re-enable the original interrupt state */
136 if (netif_running(wx->netdev))
137 txgbe_irq_enable(wx, false);
143 * txgbe_request_msix_irqs - Initialize MSI-X interrupts
144 * @wx: board private structure
146 * Allocate MSI-X vectors and request interrupts from the kernel.
148 static int txgbe_request_msix_irqs(struct wx *wx)
150 struct net_device *netdev = wx->netdev;
153 for (vector = 0; vector < wx->num_q_vectors; vector++) {
154 struct wx_q_vector *q_vector = wx->q_vector[vector];
155 struct msix_entry *entry = &wx->msix_entries[vector];
157 if (q_vector->tx.ring && q_vector->rx.ring)
158 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
159 "%s-TxRx-%d", netdev->name, entry->entry);
161 /* skip this unused q_vector */
164 err = request_irq(entry->vector, wx_msix_clean_rings, 0,
165 q_vector->name, q_vector);
167 wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
168 q_vector->name, err);
169 goto free_queue_irqs;
173 err = request_irq(wx->msix_entries[vector].vector,
174 txgbe_msix_other, 0, netdev->name, wx);
176 wx_err(wx, "request_irq for msix_other failed: %d\n", err);
177 goto free_queue_irqs;
185 free_irq(wx->msix_entries[vector].vector,
186 wx->q_vector[vector]);
188 wx_reset_interrupt_capability(wx);
193 * txgbe_request_irq - initialize interrupts
194 * @wx: board private structure
196 * Attempt to configure interrupts using the best available
197 * capabilities of the hardware and kernel.
199 static int txgbe_request_irq(struct wx *wx)
201 struct net_device *netdev = wx->netdev;
202 struct pci_dev *pdev = wx->pdev;
205 if (pdev->msix_enabled)
206 err = txgbe_request_msix_irqs(wx);
207 else if (pdev->msi_enabled)
208 err = request_irq(wx->pdev->irq, &txgbe_intr, 0,
211 err = request_irq(wx->pdev->irq, &txgbe_intr, IRQF_SHARED,
215 wx_err(wx, "request_irq failed, Error %d\n", err);
220 static void txgbe_up_complete(struct wx *wx)
224 wx_control_hw(wx, true);
225 wx_configure_vectors(wx);
227 /* make sure to complete pre-operations */
228 smp_mb__before_atomic();
229 wx_napi_enable_all(wx);
231 /* clear any pending interrupts, may auto mask */
233 rd32(wx, WX_PX_MISC_IC);
234 txgbe_irq_enable(wx, true);
236 /* Configure MAC Rx and Tx when link is up */
237 reg = rd32(wx, WX_MAC_RX_CFG);
238 wr32(wx, WX_MAC_RX_CFG, reg);
239 wr32(wx, WX_MAC_PKT_FLT, WX_MAC_PKT_FLT_PR);
240 reg = rd32(wx, WX_MAC_WDG_TIMEOUT);
241 wr32(wx, WX_MAC_WDG_TIMEOUT, reg);
242 reg = rd32(wx, WX_MAC_TX_CFG);
243 wr32(wx, WX_MAC_TX_CFG, (reg & ~WX_MAC_TX_CFG_SPEED_MASK) | WX_MAC_TX_CFG_SPEED_10G);
245 /* enable transmits */
246 netif_tx_start_all_queues(wx->netdev);
247 netif_carrier_on(wx->netdev);
250 static void txgbe_reset(struct wx *wx)
252 struct net_device *netdev = wx->netdev;
253 u8 old_addr[ETH_ALEN];
256 err = txgbe_reset_hw(wx);
258 wx_err(wx, "Hardware Error: %d\n", err);
260 /* do not flush user set addresses */
261 memcpy(old_addr, &wx->mac_table[0].addr, netdev->addr_len);
262 wx_flush_sw_mac_table(wx);
263 wx_mac_set_default_filter(wx, old_addr);
266 static void txgbe_disable_device(struct wx *wx)
268 struct net_device *netdev = wx->netdev;
271 wx_disable_pcie_master(wx);
272 /* disable receives */
275 /* disable all enabled rx queues */
276 for (i = 0; i < wx->num_rx_queues; i++)
277 /* this call also flushes the previous write */
278 wx_disable_rx_queue(wx, wx->rx_ring[i]);
280 netif_tx_stop_all_queues(netdev);
281 netif_carrier_off(netdev);
282 netif_tx_disable(netdev);
285 wx_napi_disable_all(wx);
287 if (wx->bus.func < 2)
288 wr32m(wx, TXGBE_MIS_PRB_CTL, TXGBE_MIS_PRB_CTL_LAN_UP(wx->bus.func), 0);
290 wx_err(wx, "%s: invalid bus lan id %d\n",
291 __func__, wx->bus.func);
293 if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
294 ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) {
295 /* disable mac transmiter */
296 wr32m(wx, WX_MAC_TX_CFG, WX_MAC_TX_CFG_TE, 0);
299 /* disable transmits in the hardware now that interrupts are off */
300 for (i = 0; i < wx->num_tx_queues; i++) {
301 u8 reg_idx = wx->tx_ring[i]->reg_idx;
303 wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
306 /* Disable the Tx DMA engine */
307 wr32m(wx, WX_TDM_CTL, WX_TDM_CTL_TE, 0);
310 static void txgbe_down(struct wx *wx)
312 txgbe_disable_device(wx);
315 wx_clean_all_tx_rings(wx);
316 wx_clean_all_rx_rings(wx);
320 * txgbe_sw_init - Initialize general software structures (struct wx)
321 * @wx: board private structure to initialize
323 static int txgbe_sw_init(struct wx *wx)
328 wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
329 wx->mac.max_tx_queues = TXGBE_SP_MAX_TX_QUEUES;
330 wx->mac.max_rx_queues = TXGBE_SP_MAX_RX_QUEUES;
331 wx->mac.mcft_size = TXGBE_SP_MC_TBL_SIZE;
332 wx->mac.rx_pb_size = TXGBE_SP_RX_PB_SIZE;
333 wx->mac.tx_pb_size = TXGBE_SP_TDB_PB_SZ;
335 /* PCI config space info */
336 err = wx_sw_init(wx);
338 wx_err(wx, "read of internal subsystem device id failed\n");
342 switch (wx->device_id) {
343 case TXGBE_DEV_ID_SP1000:
344 case TXGBE_DEV_ID_WX1820:
345 wx->mac.type = wx_mac_sp;
348 wx->mac.type = wx_mac_unknown;
352 /* Set common capability flags and settings */
353 wx->max_q_vectors = TXGBE_MAX_MSIX_VECTORS;
354 err = wx_get_pcie_msix_counts(wx, &msix_count, TXGBE_MAX_MSIX_VECTORS);
356 wx_err(wx, "Do not support MSI-X\n");
357 wx->mac.max_msix_vectors = msix_count;
359 /* enable itr by default in dynamic mode */
360 wx->rx_itr_setting = 1;
361 wx->tx_itr_setting = 1;
363 /* set default ring sizes */
364 wx->tx_ring_count = TXGBE_DEFAULT_TXD;
365 wx->rx_ring_count = TXGBE_DEFAULT_RXD;
367 /* set default work limits */
368 wx->tx_work_limit = TXGBE_DEFAULT_TX_WORK;
369 wx->rx_work_limit = TXGBE_DEFAULT_RX_WORK;
375 * txgbe_open - Called when a network interface is made active
376 * @netdev: network interface device structure
378 * Returns 0 on success, negative value on failure
380 * The open entry point is called when a network interface is made
381 * active by the system (IFF_UP).
383 static int txgbe_open(struct net_device *netdev)
385 struct wx *wx = netdev_priv(netdev);
388 err = wx_setup_resources(wx);
394 err = txgbe_request_irq(wx);
398 /* Notify the stack of the actual queue counts. */
399 err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
403 err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
407 txgbe_up_complete(wx);
414 wx_free_isb_resources(wx);
422 * txgbe_close_suspend - actions necessary to both suspend and close flows
423 * @wx: the private wx struct
425 * This function should contain the necessary work common to both suspending
426 * and closing of the device.
428 static void txgbe_close_suspend(struct wx *wx)
430 txgbe_disable_device(wx);
431 wx_free_resources(wx);
435 * txgbe_close - Disables a network interface
436 * @netdev: network interface device structure
438 * Returns 0, this is not allowed to fail
440 * The close entry point is called when an interface is de-activated
441 * by the OS. The hardware is still under the drivers control, but
442 * needs to be disabled. A global MAC reset is issued to stop the
443 * hardware, and all transmit and receive resources are freed.
445 static int txgbe_close(struct net_device *netdev)
447 struct wx *wx = netdev_priv(netdev);
451 wx_free_resources(wx);
452 wx_control_hw(wx, false);
457 static void txgbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
459 struct wx *wx = pci_get_drvdata(pdev);
460 struct net_device *netdev;
463 netif_device_detach(netdev);
466 if (netif_running(netdev))
467 txgbe_close_suspend(wx);
470 wx_control_hw(wx, false);
472 pci_disable_device(pdev);
475 static void txgbe_shutdown(struct pci_dev *pdev)
479 txgbe_dev_shutdown(pdev, &wake);
481 if (system_state == SYSTEM_POWER_OFF) {
482 pci_wake_from_d3(pdev, wake);
483 pci_set_power_state(pdev, PCI_D3hot);
487 static const struct net_device_ops txgbe_netdev_ops = {
488 .ndo_open = txgbe_open,
489 .ndo_stop = txgbe_close,
490 .ndo_start_xmit = wx_xmit_frame,
491 .ndo_set_rx_mode = wx_set_rx_mode,
492 .ndo_validate_addr = eth_validate_addr,
493 .ndo_set_mac_address = wx_set_mac,
494 .ndo_get_stats64 = wx_get_stats64,
498 * txgbe_probe - Device Initialization Routine
499 * @pdev: PCI device information struct
500 * @ent: entry in txgbe_pci_tbl
502 * Returns 0 on success, negative on failure
504 * txgbe_probe initializes an adapter identified by a pci_dev structure.
505 * The OS initialization, configuring of the wx private structure,
506 * and a hardware reset occur.
508 static int txgbe_probe(struct pci_dev *pdev,
509 const struct pci_device_id __always_unused *ent)
511 struct net_device *netdev;
512 int err, expected_gts;
513 struct wx *wx = NULL;
515 u16 eeprom_verh = 0, eeprom_verl = 0, offset = 0;
516 u16 eeprom_cfg_blkh = 0, eeprom_cfg_blkl = 0;
517 u16 build = 0, major = 0, patch = 0;
518 u8 part_str[TXGBE_PBANUM_LENGTH];
521 err = pci_enable_device_mem(pdev);
525 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
528 "No usable DMA configuration, aborting\n");
529 goto err_pci_disable_dev;
532 err = pci_request_selected_regions(pdev,
533 pci_select_bars(pdev, IORESOURCE_MEM),
537 "pci_request_selected_regions failed 0x%x\n", err);
538 goto err_pci_disable_dev;
541 pci_enable_pcie_error_reporting(pdev);
542 pci_set_master(pdev);
544 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
547 TXGBE_MAX_RX_QUEUES);
550 goto err_pci_release_regions;
553 SET_NETDEV_DEV(netdev, &pdev->dev);
555 wx = netdev_priv(netdev);
559 wx->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
561 wx->hw_addr = devm_ioremap(&pdev->dev,
562 pci_resource_start(pdev, 0),
563 pci_resource_len(pdev, 0));
566 goto err_pci_release_regions;
569 wx->driver_name = txgbe_driver_name;
570 txgbe_set_ethtool_ops(netdev);
571 netdev->netdev_ops = &txgbe_netdev_ops;
573 /* setup the private structure */
574 err = txgbe_sw_init(wx);
576 goto err_free_mac_table;
578 /* check if flash load is done after hw power up */
579 err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PERST);
581 goto err_free_mac_table;
582 err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PWRRST);
584 goto err_free_mac_table;
586 err = wx_mng_present(wx);
588 dev_err(&pdev->dev, "Management capability is not present\n");
589 goto err_free_mac_table;
592 err = txgbe_reset_hw(wx);
594 dev_err(&pdev->dev, "HW Init failed: %d\n", err);
595 goto err_free_mac_table;
598 netdev->features |= NETIF_F_HIGHDMA;
599 netdev->features = NETIF_F_SG;
601 /* copy netdev features into list of user selectable features */
602 netdev->hw_features |= netdev->features | NETIF_F_RXALL;
604 netdev->priv_flags |= IFF_UNICAST_FLT;
605 netdev->priv_flags |= IFF_SUPP_NOFCS;
607 netdev->min_mtu = ETH_MIN_MTU;
608 netdev->max_mtu = TXGBE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN);
610 /* make sure the EEPROM is good */
611 err = txgbe_validate_eeprom_checksum(wx, NULL);
613 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
614 wr32(wx, WX_MIS_RST, WX_MIS_RST_SW_RST);
616 goto err_free_mac_table;
619 eth_hw_addr_set(netdev, wx->mac.perm_addr);
620 wx_mac_set_default_filter(wx, wx->mac.perm_addr);
622 err = wx_init_interrupt_scheme(wx);
624 goto err_free_mac_table;
626 /* Save off EEPROM version number and Option Rom version which
627 * together make a unique identify for the eeprom
629 wx_read_ee_hostif(wx,
630 wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_H,
632 wx_read_ee_hostif(wx,
633 wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_L,
635 etrack_id = (eeprom_verh << 16) | eeprom_verl;
637 wx_read_ee_hostif(wx,
638 wx->eeprom.sw_region_offset + TXGBE_ISCSI_BOOT_CONFIG,
641 /* Make sure offset to SCSI block is valid */
642 if (!(offset == 0x0) && !(offset == 0xffff)) {
643 wx_read_ee_hostif(wx, offset + 0x84, &eeprom_cfg_blkh);
644 wx_read_ee_hostif(wx, offset + 0x83, &eeprom_cfg_blkl);
646 /* Only display Option Rom if exist */
647 if (eeprom_cfg_blkl && eeprom_cfg_blkh) {
648 major = eeprom_cfg_blkl >> 8;
649 build = (eeprom_cfg_blkl << 8) | (eeprom_cfg_blkh >> 8);
650 patch = eeprom_cfg_blkh & 0x00ff;
652 snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
653 "0x%08x, %d.%d.%d", etrack_id, major, build,
656 snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
657 "0x%08x", etrack_id);
660 snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
661 "0x%08x", etrack_id);
664 err = register_netdev(netdev);
668 pci_set_drvdata(pdev, wx);
670 netif_tx_stop_all_queues(netdev);
672 /* calculate the expected PCIe bandwidth required for optimal
673 * performance. Note that some older parts will never have enough
674 * bandwidth due to being older generation PCIe parts. We clamp these
675 * parts to ensure that no warning is displayed, as this could confuse
678 expected_gts = txgbe_enumerate_functions(wx) * 10;
680 /* don't check link if we failed to enumerate functions */
681 if (expected_gts > 0)
682 txgbe_check_minimum_link(wx);
684 dev_warn(&pdev->dev, "Failed to enumerate PF devices.\n");
686 /* First try to read PBA as a string */
687 err = txgbe_read_pba_string(wx, part_str, TXGBE_PBANUM_LENGTH);
689 strncpy(part_str, "Unknown", TXGBE_PBANUM_LENGTH);
691 netif_info(wx, probe, netdev, "%pM\n", netdev->dev_addr);
696 wx_clear_interrupt_scheme(wx);
697 wx_control_hw(wx, false);
699 kfree(wx->mac_table);
700 err_pci_release_regions:
701 pci_disable_pcie_error_reporting(pdev);
702 pci_release_selected_regions(pdev,
703 pci_select_bars(pdev, IORESOURCE_MEM));
705 pci_disable_device(pdev);
710 * txgbe_remove - Device Removal Routine
711 * @pdev: PCI device information struct
713 * txgbe_remove is called by the PCI subsystem to alert the driver
714 * that it should release a PCI device. The could be caused by a
715 * Hot-Plug event, or because the driver is going to be removed from
718 static void txgbe_remove(struct pci_dev *pdev)
720 struct wx *wx = pci_get_drvdata(pdev);
721 struct net_device *netdev;
724 unregister_netdev(netdev);
726 pci_release_selected_regions(pdev,
727 pci_select_bars(pdev, IORESOURCE_MEM));
729 kfree(wx->mac_table);
730 wx_clear_interrupt_scheme(wx);
732 pci_disable_pcie_error_reporting(pdev);
734 pci_disable_device(pdev);
737 static struct pci_driver txgbe_driver = {
738 .name = txgbe_driver_name,
739 .id_table = txgbe_pci_tbl,
740 .probe = txgbe_probe,
741 .remove = txgbe_remove,
742 .shutdown = txgbe_shutdown,
745 module_pci_driver(txgbe_driver);
747 MODULE_DEVICE_TABLE(pci, txgbe_pci_tbl);
748 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>");
749 MODULE_DESCRIPTION("WangXun(R) 10 Gigabit PCI Express Network Driver");
750 MODULE_LICENSE("GPL");