xref: /linux/drivers/net/ethernet/wangxun/libwx/wx_vf_common.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2015 - 2025 Beijing WangXun Technology Co., Ltd. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/pci.h>
6 
7 #include "wx_type.h"
8 #include "wx_mbx.h"
9 #include "wx_lib.h"
10 #include "wx_vf.h"
11 #include "wx_vf_lib.h"
12 #include "wx_vf_common.h"
13 
wxvf_suspend(struct device * dev_d)14 int wxvf_suspend(struct device *dev_d)
15 {
16 	struct pci_dev *pdev = to_pci_dev(dev_d);
17 	struct wx *wx = pci_get_drvdata(pdev);
18 
19 	netif_device_detach(wx->netdev);
20 	wx_clear_interrupt_scheme(wx);
21 	pci_disable_device(pdev);
22 
23 	return 0;
24 }
25 EXPORT_SYMBOL(wxvf_suspend);
26 
wxvf_shutdown(struct pci_dev * pdev)27 void wxvf_shutdown(struct pci_dev *pdev)
28 {
29 	wxvf_suspend(&pdev->dev);
30 }
31 EXPORT_SYMBOL(wxvf_shutdown);
32 
wxvf_resume(struct device * dev_d)33 int wxvf_resume(struct device *dev_d)
34 {
35 	struct pci_dev *pdev = to_pci_dev(dev_d);
36 	struct wx *wx = pci_get_drvdata(pdev);
37 
38 	pci_set_master(pdev);
39 	wx_init_interrupt_scheme(wx);
40 	netif_device_attach(wx->netdev);
41 
42 	return 0;
43 }
44 EXPORT_SYMBOL(wxvf_resume);
45 
wxvf_remove(struct pci_dev * pdev)46 void wxvf_remove(struct pci_dev *pdev)
47 {
48 	struct wx *wx = pci_get_drvdata(pdev);
49 	struct net_device *netdev;
50 
51 	netdev = wx->netdev;
52 	unregister_netdev(netdev);
53 	timer_shutdown_sync(&wx->service_timer);
54 	cancel_work_sync(&wx->service_task);
55 	kfree(wx->vfinfo);
56 	kfree(wx->rss_key);
57 	kfree(wx->mac_table);
58 	wx_clear_interrupt_scheme(wx);
59 	pci_release_selected_regions(pdev,
60 				     pci_select_bars(pdev, IORESOURCE_MEM));
61 	pci_disable_device(pdev);
62 }
63 EXPORT_SYMBOL(wxvf_remove);
64 
wx_msix_misc_vf(int __always_unused irq,void * data)65 static irqreturn_t wx_msix_misc_vf(int __always_unused irq, void *data)
66 {
67 	struct wx *wx = data;
68 
69 	set_bit(WX_FLAG_NEED_UPDATE_LINK, wx->flags);
70 	/* Clear the interrupt */
71 	if (!test_bit(WX_STATE_DOWN, wx->state))
72 		wr32(wx, WX_VXIMC, wx->eims_other);
73 
74 	return IRQ_HANDLED;
75 }
76 
wx_request_msix_irqs_vf(struct wx * wx)77 int wx_request_msix_irqs_vf(struct wx *wx)
78 {
79 	struct net_device *netdev = wx->netdev;
80 	int vector, err;
81 
82 	for (vector = 0; vector < wx->num_q_vectors; vector++) {
83 		struct wx_q_vector *q_vector = wx->q_vector[vector];
84 		struct msix_entry *entry = &wx->msix_q_entries[vector];
85 
86 		if (q_vector->tx.ring && q_vector->rx.ring)
87 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
88 				 "%s-TxRx-%d", netdev->name, entry->entry);
89 		else
90 			/* skip this unused q_vector */
91 			continue;
92 
93 		err = request_irq(entry->vector, wx_msix_clean_rings, 0,
94 				  q_vector->name, q_vector);
95 		if (err) {
96 			wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
97 			       q_vector->name, err);
98 			goto free_queue_irqs;
99 		}
100 	}
101 
102 	err = request_irq(wx->msix_entry->vector, wx_msix_misc_vf,
103 			  0, netdev->name, wx);
104 	if (err) {
105 		wx_err(wx, "request_irq for msix_other failed: %d\n", err);
106 		goto free_queue_irqs;
107 	}
108 
109 	return 0;
110 
111 free_queue_irqs:
112 	while (vector) {
113 		vector--;
114 		free_irq(wx->msix_q_entries[vector].vector,
115 			 wx->q_vector[vector]);
116 	}
117 	wx_reset_interrupt_capability(wx);
118 	return err;
119 }
120 EXPORT_SYMBOL(wx_request_msix_irqs_vf);
121 
wx_negotiate_api_vf(struct wx * wx)122 void wx_negotiate_api_vf(struct wx *wx)
123 {
124 	int api[] = {
125 		     wx_mbox_api_13,
126 		     wx_mbox_api_null};
127 	int err = 0, idx = 0;
128 
129 	spin_lock_bh(&wx->mbx.mbx_lock);
130 	while (api[idx] != wx_mbox_api_null) {
131 		err = wx_negotiate_api_version(wx, api[idx]);
132 		if (!err)
133 			break;
134 		idx++;
135 	}
136 	spin_unlock_bh(&wx->mbx.mbx_lock);
137 }
138 EXPORT_SYMBOL(wx_negotiate_api_vf);
139 
wx_reset_vf(struct wx * wx)140 void wx_reset_vf(struct wx *wx)
141 {
142 	struct net_device *netdev = wx->netdev;
143 	int ret = 0;
144 
145 	ret = wx_reset_hw_vf(wx);
146 	if (!ret)
147 		wx_init_hw_vf(wx);
148 	wx_negotiate_api_vf(wx);
149 	if (is_valid_ether_addr(wx->mac.addr)) {
150 		eth_hw_addr_set(netdev, wx->mac.addr);
151 		ether_addr_copy(netdev->perm_addr, wx->mac.addr);
152 	}
153 }
154 EXPORT_SYMBOL(wx_reset_vf);
155 
wx_set_rx_mode_vf(struct net_device * netdev)156 void wx_set_rx_mode_vf(struct net_device *netdev)
157 {
158 	struct wx *wx = netdev_priv(netdev);
159 	unsigned int flags = netdev->flags;
160 	int xcast_mode;
161 
162 	xcast_mode = (flags & IFF_ALLMULTI) ? WXVF_XCAST_MODE_ALLMULTI :
163 		     (flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
164 		     WXVF_XCAST_MODE_MULTI : WXVF_XCAST_MODE_NONE;
165 	/* request the most inclusive mode we need */
166 	if (flags & IFF_PROMISC)
167 		xcast_mode = WXVF_XCAST_MODE_PROMISC;
168 	else if (flags & IFF_ALLMULTI)
169 		xcast_mode = WXVF_XCAST_MODE_ALLMULTI;
170 	else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
171 		xcast_mode = WXVF_XCAST_MODE_MULTI;
172 	else
173 		xcast_mode = WXVF_XCAST_MODE_NONE;
174 
175 	spin_lock_bh(&wx->mbx.mbx_lock);
176 	wx_update_xcast_mode_vf(wx, xcast_mode);
177 	wx_update_mc_addr_list_vf(wx, netdev);
178 	wx_write_uc_addr_list_vf(netdev);
179 	spin_unlock_bh(&wx->mbx.mbx_lock);
180 }
181 EXPORT_SYMBOL(wx_set_rx_mode_vf);
182 
183 /**
184  * wx_configure_rx_vf - Configure Receive Unit after Reset
185  * @wx: board private structure
186  *
187  * Configure the Rx unit of the MAC after a reset.
188  **/
wx_configure_rx_vf(struct wx * wx)189 static void wx_configure_rx_vf(struct wx *wx)
190 {
191 	struct net_device *netdev = wx->netdev;
192 	int i, ret;
193 
194 	wx_setup_psrtype_vf(wx);
195 	wx_setup_vfmrqc_vf(wx);
196 
197 	spin_lock_bh(&wx->mbx.mbx_lock);
198 	ret = wx_rlpml_set_vf(wx,
199 			      netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
200 	spin_unlock_bh(&wx->mbx.mbx_lock);
201 	if (ret)
202 		wx_dbg(wx, "Failed to set MTU at %d\n", netdev->mtu);
203 
204 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
205 	 * the Base and Length of the Rx Descriptor Ring
206 	 */
207 	for (i = 0; i < wx->num_rx_queues; i++) {
208 		struct wx_ring *rx_ring = wx->rx_ring[i];
209 #ifdef HAVE_SWIOTLB_SKIP_CPU_SYNC
210 		wx_set_rx_buffer_len_vf(wx, rx_ring);
211 #endif
212 		wx_configure_rx_ring_vf(wx, rx_ring);
213 	}
214 }
215 
wx_configure_vf(struct wx * wx)216 void wx_configure_vf(struct wx *wx)
217 {
218 	wx_set_rx_mode_vf(wx->netdev);
219 	wx_configure_tx_vf(wx);
220 	wx_configure_rx_vf(wx);
221 }
222 EXPORT_SYMBOL(wx_configure_vf);
223 
wx_set_mac_vf(struct net_device * netdev,void * p)224 int wx_set_mac_vf(struct net_device *netdev, void *p)
225 {
226 	struct wx *wx = netdev_priv(netdev);
227 	struct sockaddr *addr = p;
228 	int ret;
229 
230 	ret = eth_prepare_mac_addr_change(netdev, addr);
231 	if (ret)
232 		return ret;
233 
234 	spin_lock_bh(&wx->mbx.mbx_lock);
235 	ret = wx_set_rar_vf(wx, 1, (u8 *)addr->sa_data, 1);
236 	spin_unlock_bh(&wx->mbx.mbx_lock);
237 
238 	if (ret)
239 		return -EPERM;
240 
241 	memcpy(wx->mac.addr, addr->sa_data, netdev->addr_len);
242 	memcpy(wx->mac.perm_addr, addr->sa_data, netdev->addr_len);
243 	eth_hw_addr_set(netdev, addr->sa_data);
244 
245 	return 0;
246 }
247 EXPORT_SYMBOL(wx_set_mac_vf);
248 
wxvf_watchdog_update_link(struct wx * wx)249 void wxvf_watchdog_update_link(struct wx *wx)
250 {
251 	int err;
252 
253 	if (!test_bit(WX_FLAG_NEED_UPDATE_LINK, wx->flags))
254 		return;
255 
256 	spin_lock_bh(&wx->mbx.mbx_lock);
257 	err = wx_check_mac_link_vf(wx);
258 	spin_unlock_bh(&wx->mbx.mbx_lock);
259 	if (err) {
260 		wx->link = false;
261 		set_bit(WX_FLAG_NEED_DO_RESET, wx->flags);
262 	}
263 	clear_bit(WX_FLAG_NEED_UPDATE_LINK, wx->flags);
264 }
265 EXPORT_SYMBOL(wxvf_watchdog_update_link);
266 
wxvf_irq_enable(struct wx * wx)267 static void wxvf_irq_enable(struct wx *wx)
268 {
269 	wr32(wx, WX_VXIMC, wx->eims_enable_mask);
270 }
271 
wxvf_up_complete(struct wx * wx)272 void wxvf_up_complete(struct wx *wx)
273 {
274 	/* Always set the carrier off */
275 	netif_carrier_off(wx->netdev);
276 	mod_timer(&wx->service_timer, jiffies + HZ);
277 	set_bit(WX_FLAG_NEED_UPDATE_LINK, wx->flags);
278 
279 	wx_configure_msix_vf(wx);
280 	smp_mb__before_atomic();
281 	clear_bit(WX_STATE_DOWN, wx->state);
282 	wx_napi_enable_all(wx);
283 
284 	/* clear any pending interrupts, may auto mask */
285 	wr32(wx, WX_VXICR, U32_MAX);
286 	wxvf_irq_enable(wx);
287 	/* enable transmits */
288 	netif_tx_start_all_queues(wx->netdev);
289 }
290 
wxvf_open(struct net_device * netdev)291 int wxvf_open(struct net_device *netdev)
292 {
293 	struct wx *wx = netdev_priv(netdev);
294 	int err;
295 
296 	err = wx_setup_resources(wx);
297 	if (err)
298 		goto err_reset;
299 	wx_configure_vf(wx);
300 
301 	err = wx_request_msix_irqs_vf(wx);
302 	if (err)
303 		goto err_free_resources;
304 
305 	/* Notify the stack of the actual queue counts. */
306 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
307 	if (err)
308 		goto err_free_irq;
309 
310 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
311 	if (err)
312 		goto err_free_irq;
313 
314 	wxvf_up_complete(wx);
315 
316 	return 0;
317 err_free_irq:
318 	wx_free_irq(wx);
319 err_free_resources:
320 	wx_free_resources(wx);
321 err_reset:
322 	wx_reset_vf(wx);
323 	return err;
324 }
325 EXPORT_SYMBOL(wxvf_open);
326 
wxvf_down(struct wx * wx)327 void wxvf_down(struct wx *wx)
328 {
329 	struct net_device *netdev = wx->netdev;
330 
331 	if (test_and_set_bit(WX_STATE_DOWN, wx->state))
332 		return;
333 
334 	timer_delete_sync(&wx->service_timer);
335 	netif_tx_stop_all_queues(netdev);
336 	netif_tx_disable(netdev);
337 	netif_carrier_off(netdev);
338 	wx_napi_disable_all(wx);
339 	wx_reset_vf(wx);
340 
341 	wx_clean_all_tx_rings(wx);
342 	wx_clean_all_rx_rings(wx);
343 }
344 
wxvf_reinit_locked(struct wx * wx)345 static void wxvf_reinit_locked(struct wx *wx)
346 {
347 	mutex_lock(&wx->reset_lock);
348 	set_bit(WX_STATE_RESETTING, wx->state);
349 
350 	wxvf_down(wx);
351 	wx_free_irq(wx);
352 	wx_configure_vf(wx);
353 	wx_request_msix_irqs_vf(wx);
354 	wxvf_up_complete(wx);
355 	clear_bit(WX_STATE_RESETTING, wx->state);
356 	mutex_unlock(&wx->reset_lock);
357 }
358 
wxvf_reset_subtask(struct wx * wx)359 static void wxvf_reset_subtask(struct wx *wx)
360 {
361 	if (!test_bit(WX_FLAG_NEED_DO_RESET, wx->flags))
362 		return;
363 	clear_bit(WX_FLAG_NEED_DO_RESET, wx->flags);
364 
365 	rtnl_lock();
366 	if (test_bit(WX_STATE_RESETTING, wx->state) ||
367 	    test_bit(WX_STATE_DOWN, wx->state)) {
368 		rtnl_unlock();
369 		return;
370 	}
371 	wxvf_reinit_locked(wx);
372 	rtnl_unlock();
373 }
374 
wxvf_close(struct net_device * netdev)375 int wxvf_close(struct net_device *netdev)
376 {
377 	struct wx *wx = netdev_priv(netdev);
378 
379 	wxvf_down(wx);
380 	wx_free_irq(wx);
381 	wx_free_resources(wx);
382 
383 	return 0;
384 }
385 EXPORT_SYMBOL(wxvf_close);
386 
wxvf_link_config_subtask(struct wx * wx)387 static void wxvf_link_config_subtask(struct wx *wx)
388 {
389 	struct net_device *netdev = wx->netdev;
390 
391 	wxvf_watchdog_update_link(wx);
392 	if (wx->link) {
393 		if (netif_carrier_ok(netdev))
394 			return;
395 		netif_carrier_on(netdev);
396 		netdev_info(netdev, "Link is Up - %s\n",
397 			    phy_speed_to_str(wx->speed));
398 	} else {
399 		if (!netif_carrier_ok(netdev))
400 			return;
401 		netif_carrier_off(netdev);
402 		netdev_info(netdev, "Link is Down\n");
403 	}
404 }
405 
wxvf_service_task(struct work_struct * work)406 static void wxvf_service_task(struct work_struct *work)
407 {
408 	struct wx *wx = container_of(work, struct wx, service_task);
409 
410 	wxvf_link_config_subtask(wx);
411 	wxvf_reset_subtask(wx);
412 	wx_service_event_complete(wx);
413 }
414 
wxvf_init_service(struct wx * wx)415 void wxvf_init_service(struct wx *wx)
416 {
417 	timer_setup(&wx->service_timer, wx_service_timer, 0);
418 	INIT_WORK(&wx->service_task, wxvf_service_task);
419 	clear_bit(WX_STATE_SERVICE_SCHED, wx->state);
420 }
421 EXPORT_SYMBOL(wxvf_init_service);
422