FILE
txgbevf_main.c
File preview with syntax highlighting.
linux-6.19.14/drivers/net/ethernet/wangxun/txgbevf/txgbevf_main.c
1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2015 - 2025 Beijing WangXun Technology Co., Ltd. */ 3 4#include <linux/types.h> 5#include <linux/module.h> 6#include <linux/pci.h> 7#include <linux/netdevice.h> 8#include <linux/string.h> 9#include <linux/etherdevice.h> 10 11#include "../libwx/wx_type.h" 12#include "../libwx/wx_hw.h" 13#include "../libwx/wx_lib.h" 14#include "../libwx/wx_mbx.h" 15#include "../libwx/wx_vf.h" 16#include "../libwx/wx_vf_common.h" 17#include "../libwx/wx_ethtool.h" 18#include "txgbevf_type.h" 19 20/* txgbevf_pci_tbl - PCI Device ID Table 21 * 22 * Wildcard entries (PCI_ANY_ID) should come last 23 * Last entry must be all 0s 24 * 25 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 26 * Class, Class Mask, private data (not used) } 27 */ 28static const struct pci_device_id txgbevf_pci_tbl[] = { 29 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_SP1000), 0}, 30 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_WX1820), 0}, 31 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML500F), 0}, 32 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML510F), 0}, 33 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML5024), 0}, 34 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML5124), 0}, 35 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML503F), 0}, 36 { PCI_VDEVICE(WANGXUN, TXGBEVF_DEV_ID_AML513F), 0}, 37 /* required last entry */ 38 { .device = 0 } 39}; 40 41static const struct net_device_ops txgbevf_netdev_ops = { 42 .ndo_open = wxvf_open, 43 .ndo_stop = wxvf_close, 44 .ndo_start_xmit = wx_xmit_frame, 45 .ndo_validate_addr = eth_validate_addr, 46 .ndo_set_mac_address = wx_set_mac_vf, 47}; 48 49static void txgbevf_set_num_queues(struct wx *wx) 50{ 51 u32 def_q = 0, num_tcs = 0; 52 u16 rss, queue; 53 int ret = 0; 54 55 /* Start with base case */ 56 wx->num_rx_queues = 1; 57 wx->num_tx_queues = 1; 58 59 spin_lock_bh(&wx->mbx.mbx_lock); 60 /* fetch queue configuration from the PF */ 61 ret = wx_get_queues_vf(wx, &num_tcs, &def_q); 62 spin_unlock_bh(&wx->mbx.mbx_lock); 63 64 if (ret) 65 return; 66 67 /* we need as many queues as traffic classes */ 68 if (num_tcs > 1) { 69 wx->num_rx_queues = num_tcs; 70 } else { 71 rss = min_t(u16, num_online_cpus(), TXGBEVF_MAX_RSS_NUM); 72 queue = min_t(u16, wx->mac.max_rx_queues, wx->mac.max_tx_queues); 73 rss = min_t(u16, queue, rss); 74 75 if (wx->vfinfo->vf_api >= wx_mbox_api_13) { 76 wx->num_rx_queues = rss; 77 wx->num_tx_queues = rss; 78 } 79 } 80} 81 82static void txgbevf_init_type_code(struct wx *wx) 83{ 84 switch (wx->device_id) { 85 case TXGBEVF_DEV_ID_SP1000: 86 case TXGBEVF_DEV_ID_WX1820: 87 wx->mac.type = wx_mac_sp; 88 break; 89 case TXGBEVF_DEV_ID_AML500F: 90 case TXGBEVF_DEV_ID_AML510F: 91 case TXGBEVF_DEV_ID_AML5024: 92 case TXGBEVF_DEV_ID_AML5124: 93 case TXGBEVF_DEV_ID_AML503F: 94 case TXGBEVF_DEV_ID_AML513F: 95 wx->mac.type = wx_mac_aml; 96 break; 97 default: 98 wx->mac.type = wx_mac_unknown; 99 break; 100 } 101} 102 103static int txgbevf_sw_init(struct wx *wx) 104{ 105 struct net_device *netdev = wx->netdev; 106 struct pci_dev *pdev = wx->pdev; 107 int err; 108 109 /* Initialize pcie info and common capability flags */ 110 err = wx_sw_init(wx); 111 if (err < 0) 112 goto err_wx_sw_init; 113 114 /* Initialize the mailbox */ 115 err = wx_init_mbx_params_vf(wx); 116 if (err) 117 goto err_init_mbx_params; 118 119 /* max q_vectors */ 120 wx->mac.max_msix_vectors = TXGBEVF_MAX_MSIX_VECTORS; 121 /* Initialize the device type */ 122 txgbevf_init_type_code(wx); 123 /* lock to protect mailbox accesses */ 124 spin_lock_init(&wx->mbx.mbx_lock); 125 126 err = wx_reset_hw_vf(wx); 127 if (err) { 128 wx_err(wx, "PF still in reset state. Is the PF interface up?\n"); 129 goto err_reset_hw; 130 } 131 wx_init_hw_vf(wx); 132 wx_negotiate_api_vf(wx); 133 if (is_zero_ether_addr(wx->mac.addr)) 134 dev_info(&pdev->dev, 135 "MAC address not assigned by administrator.\n"); 136 eth_hw_addr_set(netdev, wx->mac.addr); 137 138 if (!is_valid_ether_addr(netdev->dev_addr)) { 139 dev_info(&pdev->dev, "Assigning random MAC address\n"); 140 eth_hw_addr_random(netdev); 141 ether_addr_copy(wx->mac.addr, netdev->dev_addr); 142 ether_addr_copy(wx->mac.perm_addr, netdev->dev_addr); 143 } 144 145 wx->mac.max_tx_queues = TXGBEVF_MAX_TX_QUEUES; 146 wx->mac.max_rx_queues = TXGBEVF_MAX_RX_QUEUES; 147 /* Enable dynamic interrupt throttling rates */ 148 wx->adaptive_itr = true; 149 wx->rx_itr_setting = 1; 150 wx->tx_itr_setting = 1; 151 /* set default ring sizes */ 152 wx->tx_ring_count = TXGBEVF_DEFAULT_TXD; 153 wx->rx_ring_count = TXGBEVF_DEFAULT_RXD; 154 /* set default work limits */ 155 wx->tx_work_limit = TXGBEVF_DEFAULT_TX_WORK; 156 wx->rx_work_limit = TXGBEVF_DEFAULT_RX_WORK; 157 158 wx->set_num_queues = txgbevf_set_num_queues; 159 160 switch (wx->mac.type) { 161 case wx_mac_sp: 162 break; 163 case wx_mac_aml: 164 case wx_mac_aml40: 165 set_bit(WX_FLAG_RX_MERGE_ENABLED, wx->flags); 166 set_bit(WX_FLAG_TXHEAD_WB_ENABLED, wx->flags); 167 break; 168 default: 169 break; 170 } 171 172 return 0; 173err_reset_hw: 174 kfree(wx->vfinfo); 175err_init_mbx_params: 176 kfree(wx->rss_key); 177 kfree(wx->mac_table); 178err_wx_sw_init: 179 return err; 180} 181 182/** 183 * txgbevf_probe - Device Initialization Routine 184 * @pdev: PCI device information struct 185 * @ent: entry in txgbevf_pci_tbl 186 * 187 * Return: return 0 on success, negative on failure 188 * 189 * txgbevf_probe initializes an adapter identified by a pci_dev structure. 190 * The OS initialization, configuring of the adapter private structure, 191 * and a hardware reset occur. 192 **/ 193static int txgbevf_probe(struct pci_dev *pdev, 194 const struct pci_device_id __always_unused *ent) 195{ 196 struct net_device *netdev; 197 struct wx *wx = NULL; 198 int err; 199 200 err = pci_enable_device_mem(pdev); 201 if (err) 202 return err; 203 204 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 205 if (err) { 206 dev_err(&pdev->dev, 207 "No usable DMA configuration, aborting\n"); 208 goto err_pci_disable_dev; 209 } 210 211 err = pci_request_selected_regions(pdev, 212 pci_select_bars(pdev, IORESOURCE_MEM), 213 dev_driver_string(&pdev->dev)); 214 if (err) { 215 dev_err(&pdev->dev, 216 "pci_request_selected_regions failed 0x%x\n", err); 217 goto err_pci_disable_dev; 218 } 219 220 pci_set_master(pdev); 221 222 netdev = devm_alloc_etherdev_mqs(&pdev->dev, 223 sizeof(struct wx), 224 TXGBEVF_MAX_TX_QUEUES, 225 TXGBEVF_MAX_RX_QUEUES); 226 if (!netdev) { 227 err = -ENOMEM; 228 goto err_pci_release_regions; 229 } 230 231 SET_NETDEV_DEV(netdev, &pdev->dev); 232 233 wx = netdev_priv(netdev); 234 wx->netdev = netdev; 235 wx->pdev = pdev; 236 237 wx->msg_enable = netif_msg_init(-1, NETIF_MSG_DRV | 238 NETIF_MSG_PROBE | NETIF_MSG_LINK); 239 wx->hw_addr = devm_ioremap(&pdev->dev, 240 pci_resource_start(pdev, 0), 241 pci_resource_len(pdev, 0)); 242 if (!wx->hw_addr) { 243 err = -EIO; 244 goto err_pci_release_regions; 245 } 246 247 wx->b4_addr = devm_ioremap(&pdev->dev, 248 pci_resource_start(pdev, 4), 249 pci_resource_len(pdev, 4)); 250 if (!wx->b4_addr) { 251 err = -EIO; 252 goto err_pci_release_regions; 253 } 254 255 wx->driver_name = KBUILD_MODNAME; 256 wx_set_ethtool_ops_vf(netdev); 257 netdev->netdev_ops = &txgbevf_netdev_ops; 258 259 /* setup the private structure */ 260 err = txgbevf_sw_init(wx); 261 if (err) 262 goto err_pci_release_regions; 263 264 netdev->features |= NETIF_F_HIGHDMA; 265 266 eth_hw_addr_set(netdev, wx->mac.perm_addr); 267 ether_addr_copy(netdev->perm_addr, wx->mac.addr); 268 269 wxvf_init_service(wx); 270 err = wx_init_interrupt_scheme(wx); 271 if (err) 272 goto err_free_sw_init; 273 274 wx_get_fw_version_vf(wx); 275 err = register_netdev(netdev); 276 if (err) 277 goto err_register; 278 279 pci_set_drvdata(pdev, wx); 280 netif_tx_stop_all_queues(netdev); 281 282 return 0; 283 284err_register: 285 wx_clear_interrupt_scheme(wx); 286err_free_sw_init: 287 timer_delete_sync(&wx->service_timer); 288 cancel_work_sync(&wx->service_task); 289 kfree(wx->vfinfo); 290 kfree(wx->rss_key); 291 kfree(wx->mac_table); 292err_pci_release_regions: 293 pci_release_selected_regions(pdev, 294 pci_select_bars(pdev, IORESOURCE_MEM)); 295err_pci_disable_dev: 296 pci_disable_device(pdev); 297 return err; 298} 299 300/** 301 * txgbevf_remove - Device Removal Routine 302 * @pdev: PCI device information struct 303 * 304 * txgbevf_remove is called by the PCI subsystem to alert the driver 305 * that it should release a PCI device. The could be caused by a 306 * Hot-Plug event, or because the driver is going to be removed from 307 * memory. 308 **/ 309static void txgbevf_remove(struct pci_dev *pdev) 310{ 311 wxvf_remove(pdev); 312} 313 314static DEFINE_SIMPLE_DEV_PM_OPS(txgbevf_pm_ops, wxvf_suspend, wxvf_resume); 315 316static struct pci_driver txgbevf_driver = { 317 .name = KBUILD_MODNAME, 318 .id_table = txgbevf_pci_tbl, 319 .probe = txgbevf_probe, 320 .remove = txgbevf_remove, 321 .shutdown = wxvf_shutdown, 322 /* Power Management Hooks */ 323 .driver.pm = pm_sleep_ptr(&txgbevf_pm_ops) 324}; 325 326module_pci_driver(txgbevf_driver); 327 328MODULE_DEVICE_TABLE(pci, txgbevf_pci_tbl); 329MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>"); 330MODULE_DESCRIPTION("WangXun(R) 10/25/40 Gigabit Virtual Function Network Driver"); 331MODULE_LICENSE("GPL");
~/
Read-only file preview.