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 "ngbevf_type.h"
18
19 /* ngbevf_pci_tbl - PCI Device ID Table
20 *
21 * Wildcard entries (PCI_ANY_ID) should come last
22 * Last entry must be all 0s
23 *
24 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
25 * Class, Class Mask, private data (not used) }
26 */
27 static const struct pci_device_id ngbevf_pci_tbl[] = {
28 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL_W), 0},
29 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860A2), 0},
30 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860A2S), 0},
31 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860A4), 0},
32 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860A4S), 0},
33 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL2), 0},
34 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL2S), 0},
35 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL4), 0},
36 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL4S), 0},
37 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860NCSI), 0},
38 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860A1), 0},
39 { PCI_VDEVICE(WANGXUN, NGBEVF_DEV_ID_EM_WX1860AL1), 0},
40 /* required last entry */
41 { .device = 0 }
42 };
43
44 static const struct net_device_ops ngbevf_netdev_ops = {
45 .ndo_open = wxvf_open,
46 .ndo_stop = wxvf_close,
47 .ndo_start_xmit = wx_xmit_frame,
48 .ndo_validate_addr = eth_validate_addr,
49 .ndo_set_mac_address = wx_set_mac_vf,
50 };
51
ngbevf_set_num_queues(struct wx * wx)52 static void ngbevf_set_num_queues(struct wx *wx)
53 {
54 /* Start with base case */
55 wx->num_rx_queues = 1;
56 wx->num_tx_queues = 1;
57 }
58
ngbevf_sw_init(struct wx * wx)59 static int ngbevf_sw_init(struct wx *wx)
60 {
61 struct net_device *netdev = wx->netdev;
62 struct pci_dev *pdev = wx->pdev;
63 int err;
64
65 /* Initialize pcie info and common capability flags */
66 err = wx_sw_init(wx);
67 if (err < 0)
68 goto err_wx_sw_init;
69
70 /* Initialize the mailbox */
71 err = wx_init_mbx_params_vf(wx);
72 if (err)
73 goto err_init_mbx_params;
74
75 /* Initialize the device type */
76 wx->mac.type = wx_mac_em;
77 wx->mac.max_msix_vectors = NGBEVF_MAX_MSIX_VECTORS;
78 /* lock to protect mailbox accesses */
79 spin_lock_init(&wx->mbx.mbx_lock);
80
81 err = wx_reset_hw_vf(wx);
82 if (err) {
83 wx_err(wx, "PF still in reset state. Is the PF interface up?\n");
84 goto err_reset_hw;
85 }
86 wx_init_hw_vf(wx);
87 wx_negotiate_api_vf(wx);
88 if (is_zero_ether_addr(wx->mac.addr))
89 dev_info(&pdev->dev,
90 "MAC address not assigned by administrator.\n");
91 eth_hw_addr_set(netdev, wx->mac.addr);
92
93 if (!is_valid_ether_addr(netdev->dev_addr)) {
94 dev_info(&pdev->dev, "Assigning random MAC address\n");
95 eth_hw_addr_random(netdev);
96 ether_addr_copy(wx->mac.addr, netdev->dev_addr);
97 ether_addr_copy(wx->mac.perm_addr, netdev->dev_addr);
98 }
99
100 wx->mac.max_tx_queues = NGBEVF_MAX_TX_QUEUES;
101 wx->mac.max_rx_queues = NGBEVF_MAX_RX_QUEUES;
102 /* Enable dynamic interrupt throttling rates */
103 wx->rx_itr_setting = 1;
104 wx->tx_itr_setting = 1;
105 /* set default ring sizes */
106 wx->tx_ring_count = NGBEVF_DEFAULT_TXD;
107 wx->rx_ring_count = NGBEVF_DEFAULT_RXD;
108 /* set default work limits */
109 wx->tx_work_limit = NGBEVF_DEFAULT_TX_WORK;
110 wx->rx_work_limit = NGBEVF_DEFAULT_RX_WORK;
111 wx->set_num_queues = ngbevf_set_num_queues;
112
113 return 0;
114 err_reset_hw:
115 kfree(wx->vfinfo);
116 err_init_mbx_params:
117 kfree(wx->rss_key);
118 kfree(wx->mac_table);
119 err_wx_sw_init:
120 return err;
121 }
122
123 /**
124 * ngbevf_probe - Device Initialization Routine
125 * @pdev: PCI device information struct
126 * @ent: entry in ngbevf_pci_tbl
127 *
128 * Return: return 0 on success, negative on failure
129 *
130 * ngbevf_probe initializes an adapter identified by a pci_dev structure.
131 * The OS initialization, configuring of the adapter private structure,
132 * and a hardware reset occur.
133 **/
ngbevf_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)134 static int ngbevf_probe(struct pci_dev *pdev,
135 const struct pci_device_id __always_unused *ent)
136 {
137 struct net_device *netdev;
138 struct wx *wx = NULL;
139 int err;
140
141 err = pci_enable_device_mem(pdev);
142 if (err)
143 return err;
144
145 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
146 if (err) {
147 dev_err(&pdev->dev,
148 "No usable DMA configuration, aborting\n");
149 goto err_pci_disable_dev;
150 }
151
152 err = pci_request_selected_regions(pdev,
153 pci_select_bars(pdev, IORESOURCE_MEM),
154 dev_driver_string(&pdev->dev));
155 if (err) {
156 dev_err(&pdev->dev,
157 "pci_request_selected_regions failed 0x%x\n", err);
158 goto err_pci_disable_dev;
159 }
160
161 pci_set_master(pdev);
162
163 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
164 sizeof(struct wx),
165 NGBEVF_MAX_TX_QUEUES,
166 NGBEVF_MAX_RX_QUEUES);
167 if (!netdev) {
168 err = -ENOMEM;
169 goto err_pci_release_regions;
170 }
171
172 SET_NETDEV_DEV(netdev, &pdev->dev);
173
174 wx = netdev_priv(netdev);
175 wx->netdev = netdev;
176 wx->pdev = pdev;
177
178 wx->msg_enable = netif_msg_init(-1, NETIF_MSG_DRV |
179 NETIF_MSG_PROBE | NETIF_MSG_LINK);
180 wx->hw_addr = devm_ioremap(&pdev->dev,
181 pci_resource_start(pdev, 0),
182 pci_resource_len(pdev, 0));
183 if (!wx->hw_addr) {
184 err = -EIO;
185 goto err_pci_release_regions;
186 }
187
188 netdev->netdev_ops = &ngbevf_netdev_ops;
189
190 /* setup the private structure */
191 err = ngbevf_sw_init(wx);
192 if (err)
193 goto err_pci_release_regions;
194
195 netdev->features |= NETIF_F_HIGHDMA;
196
197 eth_hw_addr_set(netdev, wx->mac.perm_addr);
198 ether_addr_copy(netdev->perm_addr, wx->mac.addr);
199
200 wxvf_init_service(wx);
201 err = wx_init_interrupt_scheme(wx);
202 if (err)
203 goto err_free_sw_init;
204
205 err = register_netdev(netdev);
206 if (err)
207 goto err_register;
208
209 pci_set_drvdata(pdev, wx);
210 netif_tx_stop_all_queues(netdev);
211
212 return 0;
213
214 err_register:
215 wx_clear_interrupt_scheme(wx);
216 err_free_sw_init:
217 timer_delete_sync(&wx->service_timer);
218 cancel_work_sync(&wx->service_task);
219 kfree(wx->vfinfo);
220 kfree(wx->rss_key);
221 kfree(wx->mac_table);
222 err_pci_release_regions:
223 pci_release_selected_regions(pdev,
224 pci_select_bars(pdev, IORESOURCE_MEM));
225 err_pci_disable_dev:
226 pci_disable_device(pdev);
227 return err;
228 }
229
230 /**
231 * ngbevf_remove - Device Removal Routine
232 * @pdev: PCI device information struct
233 *
234 * ngbevf_remove is called by the PCI subsystem to alert the driver
235 * that it should release a PCI device. The could be caused by a
236 * Hot-Plug event, or because the driver is going to be removed from
237 * memory.
238 **/
ngbevf_remove(struct pci_dev * pdev)239 static void ngbevf_remove(struct pci_dev *pdev)
240 {
241 wxvf_remove(pdev);
242 }
243
244 static DEFINE_SIMPLE_DEV_PM_OPS(ngbevf_pm_ops, wxvf_suspend, wxvf_resume);
245
246 static struct pci_driver ngbevf_driver = {
247 .name = KBUILD_MODNAME,
248 .id_table = ngbevf_pci_tbl,
249 .probe = ngbevf_probe,
250 .remove = ngbevf_remove,
251 .shutdown = wxvf_shutdown,
252 /* Power Management Hooks */
253 .driver.pm = pm_sleep_ptr(&ngbevf_pm_ops)
254 };
255
256 module_pci_driver(ngbevf_driver);
257
258 MODULE_DEVICE_TABLE(pci, ngbevf_pci_tbl);
259 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>");
260 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
261 MODULE_LICENSE("GPL");
262