File Index
Files and directories
Sign in
FILE

wx_sriov.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/wangxun/libwx/wx_sriov.c C
  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_hw.h"
  9#include "wx_mbx.h"
 10#include "wx_sriov.h"
 11
 12static void wx_vf_configuration(struct pci_dev *pdev, int event_mask)
 13{
 14	bool enable = !!WX_VF_ENABLE_CHECK(event_mask);
 15	struct wx *wx = pci_get_drvdata(pdev);
 16	u32 vfn = WX_VF_NUM_GET(event_mask);
 17
 18	if (enable)
 19		eth_zero_addr(wx->vfinfo[vfn].vf_mac_addr);
 20}
 21
 22static int wx_alloc_vf_macvlans(struct wx *wx, u8 num_vfs)
 23{
 24	struct vf_macvlans *mv_list;
 25	int num_vf_macvlans, i;
 26
 27	/* Initialize list of VF macvlans */
 28	INIT_LIST_HEAD(&wx->vf_mvs.mvlist);
 29
 30	num_vf_macvlans = wx->mac.num_rar_entries -
 31			  (WX_MAX_PF_MACVLANS + 1 + num_vfs);
 32	if (!num_vf_macvlans)
 33		return -EINVAL;
 34
 35	mv_list = kcalloc(num_vf_macvlans, sizeof(struct vf_macvlans),
 36			  GFP_KERNEL);
 37	if (!mv_list)
 38		return -ENOMEM;
 39
 40	for (i = 0; i < num_vf_macvlans; i++) {
 41		mv_list[i].vf = -1;
 42		mv_list[i].free = true;
 43		list_add(&mv_list[i].mvlist, &wx->vf_mvs.mvlist);
 44	}
 45	wx->mv_list = mv_list;
 46
 47	return 0;
 48}
 49
 50static void wx_sriov_clear_data(struct wx *wx)
 51{
 52	/* set num VFs to 0 to prevent access to vfinfo */
 53	wx->num_vfs = 0;
 54
 55	/* free VF control structures */
 56	kfree(wx->vfinfo);
 57	wx->vfinfo = NULL;
 58
 59	/* free macvlan list */
 60	kfree(wx->mv_list);
 61	wx->mv_list = NULL;
 62
 63	/* set default pool back to 0 */
 64	wr32m(wx, WX_PSR_VM_CTL, WX_PSR_VM_CTL_POOL_MASK, 0);
 65	wx->ring_feature[RING_F_VMDQ].offset = 0;
 66
 67	clear_bit(WX_FLAG_IRQ_VECTOR_SHARED, wx->flags);
 68	clear_bit(WX_FLAG_SRIOV_ENABLED, wx->flags);
 69	/* Disable VMDq flag so device will be set in NM mode */
 70	if (wx->ring_feature[RING_F_VMDQ].limit == 1)
 71		clear_bit(WX_FLAG_VMDQ_ENABLED, wx->flags);
 72}
 73
 74static int __wx_enable_sriov(struct wx *wx, u8 num_vfs)
 75{
 76	int i, ret = 0;
 77	u32 value = 0;
 78
 79	set_bit(WX_FLAG_SRIOV_ENABLED, wx->flags);
 80	dev_info(&wx->pdev->dev, "SR-IOV enabled with %d VFs\n", num_vfs);
 81
 82	if (num_vfs == 7 && wx->mac.type == wx_mac_em)
 83		set_bit(WX_FLAG_IRQ_VECTOR_SHARED, wx->flags);
 84
 85	/* Enable VMDq flag so device will be set in VM mode */
 86	set_bit(WX_FLAG_VMDQ_ENABLED, wx->flags);
 87	if (!wx->ring_feature[RING_F_VMDQ].limit)
 88		wx->ring_feature[RING_F_VMDQ].limit = 1;
 89	wx->ring_feature[RING_F_VMDQ].offset = num_vfs;
 90
 91	wx->vfinfo = kcalloc(num_vfs, sizeof(struct vf_data_storage),
 92			     GFP_KERNEL);
 93	if (!wx->vfinfo)
 94		return -ENOMEM;
 95
 96	ret = wx_alloc_vf_macvlans(wx, num_vfs);
 97	if (ret)
 98		return ret;
 99
100	/* Initialize default switching mode VEB */
101	wr32m(wx, WX_PSR_CTL, WX_PSR_CTL_SW_EN, WX_PSR_CTL_SW_EN);
102
103	for (i = 0; i < num_vfs; i++) {
104		/* enable spoof checking for all VFs */
105		wx->vfinfo[i].spoofchk_enabled = true;
106		wx->vfinfo[i].link_enable = true;
107		/* untrust all VFs */
108		wx->vfinfo[i].trusted = false;
109		/* set the default xcast mode */
110		wx->vfinfo[i].xcast_mode = WXVF_XCAST_MODE_NONE;
111	}
112
113	if (!test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
114		value = WX_CFG_PORT_CTL_NUM_VT_8;
115	} else {
116		if (num_vfs < 32)
117			value = WX_CFG_PORT_CTL_NUM_VT_32;
118		else
119			value = WX_CFG_PORT_CTL_NUM_VT_64;
120	}
121	wr32m(wx, WX_CFG_PORT_CTL,
122	      WX_CFG_PORT_CTL_NUM_VT_MASK,
123	      value);
124
125	/* Disable RSC when in SR-IOV mode */
126	clear_bit(WX_FLAG_RSC_CAPABLE, wx->flags);
127	clear_bit(WX_FLAG_RSC_ENABLED, wx->flags);
128
129	return ret;
130}
131
132static void wx_sriov_reinit(struct wx *wx)
133{
134	rtnl_lock();
135	wx->setup_tc(wx->netdev, netdev_get_num_tc(wx->netdev));
136	rtnl_unlock();
137}
138
139void wx_disable_sriov(struct wx *wx)
140{
141	if (!pci_vfs_assigned(wx->pdev))
142		pci_disable_sriov(wx->pdev);
143	else
144		wx_err(wx, "Unloading driver while VFs are assigned.\n");
145
146	/* clear flags and free allloced data */
147	wx_sriov_clear_data(wx);
148}
149EXPORT_SYMBOL(wx_disable_sriov);
150
151static int wx_pci_sriov_enable(struct pci_dev *dev,
152			       int num_vfs)
153{
154	struct wx *wx = pci_get_drvdata(dev);
155	int err = 0, i;
156
157	if (netif_is_rxfh_configured(wx->netdev)) {
158		wx_err(wx, "Cannot enable SR-IOV while RXFH is configured\n");
159		wx_err(wx, "Run 'ethtool -X <if> default' to reset RSS table\n");
160		return -EBUSY;
161	}
162
163	err = __wx_enable_sriov(wx, num_vfs);
164	if (err)
165		return err;
166
167	wx->num_vfs = num_vfs;
168	for (i = 0; i < wx->num_vfs; i++)
169		wx_vf_configuration(dev, (i | WX_VF_ENABLE));
170
171	/* reset before enabling SRIOV to avoid mailbox issues */
172	wx_sriov_reinit(wx);
173
174	err = pci_enable_sriov(dev, num_vfs);
175	if (err) {
176		wx_err(wx, "Failed to enable PCI sriov: %d\n", err);
177		goto err_out;
178	}
179
180	return num_vfs;
181err_out:
182	wx_sriov_clear_data(wx);
183	return err;
184}
185
186static int wx_pci_sriov_disable(struct pci_dev *dev)
187{
188	struct wx *wx = pci_get_drvdata(dev);
189
190	if (netif_is_rxfh_configured(wx->netdev)) {
191		wx_err(wx, "Cannot disable SR-IOV while RXFH is configured\n");
192		wx_err(wx, "Run 'ethtool -X <if> default' to reset RSS table\n");
193		return -EBUSY;
194	}
195
196	wx_disable_sriov(wx);
197	wx_sriov_reinit(wx);
198
199	return 0;
200}
201
202int wx_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
203{
204	struct wx *wx = pci_get_drvdata(pdev);
205	int err;
206
207	if (!num_vfs) {
208		if (!pci_vfs_assigned(pdev))
209			return wx_pci_sriov_disable(pdev);
210
211		wx_err(wx, "can't free VFs because some are assigned to VMs.\n");
212		return -EBUSY;
213	}
214
215	err = wx_pci_sriov_enable(pdev, num_vfs);
216	if (err)
217		return err;
218
219	return num_vfs;
220}
221EXPORT_SYMBOL(wx_pci_sriov_configure);
222
223static int wx_set_vf_mac(struct wx *wx, u16 vf, const u8 *mac_addr)
224{
225	u8 hw_addr[ETH_ALEN];
226	int ret = 0;
227
228	ether_addr_copy(hw_addr, mac_addr);
229	wx_del_mac_filter(wx, wx->vfinfo[vf].vf_mac_addr, vf);
230	ret = wx_add_mac_filter(wx, hw_addr, vf);
231	if (ret >= 0)
232		ether_addr_copy(wx->vfinfo[vf].vf_mac_addr, mac_addr);
233	else
234		eth_zero_addr(wx->vfinfo[vf].vf_mac_addr);
235
236	return ret;
237}
238
239static void wx_set_vmolr(struct wx *wx, u16 vf, bool aupe)
240{
241	u32 vmolr = rd32(wx, WX_PSR_VM_L2CTL(vf));
242
243	vmolr |=  WX_PSR_VM_L2CTL_BAM;
244	if (aupe)
245		vmolr |= WX_PSR_VM_L2CTL_AUPE;
246	else
247		vmolr &= ~WX_PSR_VM_L2CTL_AUPE;
248	wr32(wx, WX_PSR_VM_L2CTL(vf), vmolr);
249}
250
251static void wx_set_vmvir(struct wx *wx, u16 vid, u16 qos, u16 vf)
252{
253	u32 vmvir = vid | (qos << VLAN_PRIO_SHIFT) |
254		    WX_TDM_VLAN_INS_VLANA_DEFAULT;
255
256	wr32(wx, WX_TDM_VLAN_INS(vf), vmvir);
257}
258
259static int wx_set_vf_vlan(struct wx *wx, int add, int vid, u16 vf)
260{
261	if (!vid && !add)
262		return 0;
263
264	return wx_set_vfta(wx, vid, vf, (bool)add);
265}
266
267static void wx_set_vlan_anti_spoofing(struct wx *wx, bool enable, int vf)
268{
269	u32 index = WX_VF_REG_OFFSET(vf), vf_bit = WX_VF_IND_SHIFT(vf);
270	u32 pfvfspoof;
271
272	pfvfspoof = rd32(wx, WX_TDM_VLAN_AS(index));
273	if (enable)
274		pfvfspoof |= BIT(vf_bit);
275	else
276		pfvfspoof &= ~BIT(vf_bit);
277	wr32(wx, WX_TDM_VLAN_AS(index), pfvfspoof);
278}
279
280static void wx_write_qde(struct wx *wx, u32 vf, u32 qde)
281{
282	struct wx_ring_feature *vmdq = &wx->ring_feature[RING_F_VMDQ];
283	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
284	u32 reg = 0, n = vf * q_per_pool / 32;
285	u32 i = vf * q_per_pool;
286
287	reg = rd32(wx, WX_RDM_PF_QDE(n));
288	for (i = (vf * q_per_pool - n * 32);
289	     i < ((vf + 1) * q_per_pool - n * 32);
290	     i++) {
291		if (qde == 1)
292			reg |= qde << i;
293		else
294			reg &= qde << i;
295	}
296
297	wr32(wx, WX_RDM_PF_QDE(n), reg);
298}
299
300static void wx_clear_vmvir(struct wx *wx, u32 vf)
301{
302	wr32(wx, WX_TDM_VLAN_INS(vf), 0);
303}
304
305static void wx_ping_vf(struct wx *wx, int vf)
306{
307	u32 ping = WX_PF_CONTROL_MSG;
308
309	if (wx->vfinfo[vf].clear_to_send)
310		ping |= WX_VT_MSGTYPE_CTS;
311	wx_write_mbx_pf(wx, &ping, 1, vf);
312}
313
314static void wx_set_vf_rx_tx(struct wx *wx, int vf)
315{
316	u32 index = WX_VF_REG_OFFSET(vf), vf_bit = WX_VF_IND_SHIFT(vf);
317	u32 reg_cur_tx, reg_cur_rx, reg_req_tx, reg_req_rx;
318
319	reg_cur_tx = rd32(wx, WX_TDM_VF_TE(index));
320	reg_cur_rx = rd32(wx, WX_RDM_VF_RE(index));
321
322	if (wx->vfinfo[vf].link_enable) {
323		reg_req_tx = reg_cur_tx | BIT(vf_bit);
324		reg_req_rx = reg_cur_rx | BIT(vf_bit);
325		/* Enable particular VF */
326		if (reg_cur_tx != reg_req_tx)
327			wr32(wx, WX_TDM_VF_TE(index), reg_req_tx);
328		if (reg_cur_rx != reg_req_rx)
329			wr32(wx, WX_RDM_VF_RE(index), reg_req_rx);
330	} else {
331		reg_req_tx = BIT(vf_bit);
332		reg_req_rx = BIT(vf_bit);
333		/* Disable particular VF */
334		if (reg_cur_tx & reg_req_tx)
335			wr32(wx, WX_TDM_VFTE_CLR(index), reg_req_tx);
336		if (reg_cur_rx & reg_req_rx)
337			wr32(wx, WX_RDM_VFRE_CLR(index), reg_req_rx);
338	}
339}
340
341static int wx_get_vf_queues(struct wx *wx, u32 *msgbuf, u32 vf)
342{
343	struct wx_ring_feature *vmdq = &wx->ring_feature[RING_F_VMDQ];
344	unsigned int default_tc = 0;
345
346	msgbuf[WX_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
347	msgbuf[WX_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
348
349	if (wx->vfinfo[vf].pf_vlan || wx->vfinfo[vf].pf_qos)
350		msgbuf[WX_VF_TRANS_VLAN] = 1;
351	else
352		msgbuf[WX_VF_TRANS_VLAN] = 0;
353
354	/* notify VF of default queue */
355	msgbuf[WX_VF_DEF_QUEUE] = default_tc;
356
357	return 0;
358}
359
360static void wx_vf_reset_event(struct wx *wx, u16 vf)
361{
362	struct vf_data_storage *vfinfo = &wx->vfinfo[vf];
363	u8 num_tcs = netdev_get_num_tc(wx->netdev);
364
365	/* add PF assigned VLAN */
366	wx_set_vf_vlan(wx, true, vfinfo->pf_vlan, vf);
367
368	/* reset offloads to defaults */
369	wx_set_vmolr(wx, vf, !vfinfo->pf_vlan);
370
371	/* set outgoing tags for VFs */
372	if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
373		wx_clear_vmvir(wx, vf);
374	} else {
375		if (vfinfo->pf_qos || !num_tcs)
376			wx_set_vmvir(wx, vfinfo->pf_vlan,
377				     vfinfo->pf_qos, vf);
378		else
379			wx_set_vmvir(wx, vfinfo->pf_vlan,
380				     wx->default_up, vf);
381	}
382
383	/* reset multicast table array for vf */
384	wx->vfinfo[vf].num_vf_mc_hashes = 0;
385
386	/* Flush and reset the mta with the new values */
387	wx_set_rx_mode(wx->netdev);
388
389	wx_del_mac_filter(wx, wx->vfinfo[vf].vf_mac_addr, vf);
390	/* reset VF api back to unknown */
391	wx->vfinfo[vf].vf_api = wx_mbox_api_null;
392}
393
394static void wx_vf_reset_msg(struct wx *wx, u16 vf)
395{
396	const u8 *vf_mac = wx->vfinfo[vf].vf_mac_addr;
397	struct net_device *dev = wx->netdev;
398	u32 msgbuf[5] = {0, 0, 0, 0, 0};
399	u8 *addr = (u8 *)(&msgbuf[1]);
400	u32 reg = 0, index, vf_bit;
401	int pf_max_frame;
402
403	/* reset the filters for the device */
404	wx_vf_reset_event(wx, vf);
405
406	/* set vf mac address */
407	if (!is_zero_ether_addr(vf_mac))
408		wx_set_vf_mac(wx, vf, vf_mac);
409
410	index = WX_VF_REG_OFFSET(vf);
411	vf_bit = WX_VF_IND_SHIFT(vf);
412
413	/* force drop enable for all VF Rx queues */
414	wx_write_qde(wx, vf, 1);
415
416	/* set transmit and receive for vf */
417	wx_set_vf_rx_tx(wx, vf);
418
419	pf_max_frame = dev->mtu + ETH_HLEN;
420
421	if (pf_max_frame > ETH_FRAME_LEN)
422		reg = BIT(vf_bit);
423	wr32(wx, WX_RDM_VFRE_CLR(index), reg);
424
425	/* enable VF mailbox for further messages */
426	wx->vfinfo[vf].clear_to_send = true;
427
428	/* reply to reset with ack and vf mac address */
429	msgbuf[0] = WX_VF_RESET;
430	if (!is_zero_ether_addr(vf_mac)) {
431		msgbuf[0] |= WX_VT_MSGTYPE_ACK;
432		memcpy(addr, vf_mac, ETH_ALEN);
433	} else {
434		msgbuf[0] |= WX_VT_MSGTYPE_NACK;
435		wx_err(wx, "VF %d has no MAC address assigned", vf);
436	}
437
438	msgbuf[3] = wx->mac.mc_filter_type;
439	wx_write_mbx_pf(wx, msgbuf, WX_VF_PERMADDR_MSG_LEN, vf);
440}
441
442static int wx_set_vf_mac_addr(struct wx *wx, u32 *msgbuf, u16 vf)
443{
444	const u8 *new_mac = ((u8 *)(&msgbuf[1]));
445	int ret;
446
447	if (!is_valid_ether_addr(new_mac)) {
448		wx_err(wx, "VF %d attempted to set invalid mac\n", vf);
449		return -EINVAL;
450	}
451
452	if (wx->vfinfo[vf].pf_set_mac &&
453	    memcmp(wx->vfinfo[vf].vf_mac_addr, new_mac, ETH_ALEN)) {
454		wx_err(wx,
455		       "VF %d attempt to set a MAC but it already had a MAC.",
456		       vf);
457		return -EBUSY;
458	}
459
460	ret = wx_set_vf_mac(wx, vf, new_mac);
461	if (ret < 0)
462		return ret;
463
464	return 0;
465}
466
467static void wx_set_vf_multicasts(struct wx *wx, u32 *msgbuf, u32 vf)
468{
469	struct vf_data_storage *vfinfo = &wx->vfinfo[vf];
470	u16 entries = (msgbuf[0] & WX_VT_MSGINFO_MASK)
471		      >> WX_VT_MSGINFO_SHIFT;
472	u32 vmolr = rd32(wx, WX_PSR_VM_L2CTL(vf));
473	u32 vector_bit, vector_reg, mta_reg, i;
474	u16 *hash_list = (u16 *)&msgbuf[1];
475
476	/* only so many hash values supported */
477	entries = min_t(u16, entries, WX_MAX_VF_MC_ENTRIES);
478	vfinfo->num_vf_mc_hashes = entries;
479
480	for (i = 0; i < entries; i++)
481		vfinfo->vf_mc_hashes[i] = hash_list[i];
482
483	for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
484		vector_reg = WX_PSR_MC_TBL_REG(vfinfo->vf_mc_hashes[i]);
485		vector_bit = WX_PSR_MC_TBL_BIT(vfinfo->vf_mc_hashes[i]);
486		mta_reg = wx->mac.mta_shadow[vector_reg];
487		mta_reg |= BIT(vector_bit);
488		wx->mac.mta_shadow[vector_reg] = mta_reg;
489		wr32(wx, WX_PSR_MC_TBL(vector_reg), mta_reg);
490	}
491	vmolr |= WX_PSR_VM_L2CTL_ROMPE;
492	wr32(wx, WX_PSR_VM_L2CTL(vf), vmolr);
493}
494
495static void wx_set_vf_lpe(struct wx *wx, u32 max_frame, u32 vf)
496{
497	u32 index, vf_bit, vfre;
498	u32 max_frs, reg_val;
499
500	/* determine VF receive enable location */
501	index = WX_VF_REG_OFFSET(vf);
502	vf_bit = WX_VF_IND_SHIFT(vf);
503
504	vfre = rd32(wx, WX_RDM_VF_RE(index));
505	vfre |= BIT(vf_bit);
506	wr32(wx, WX_RDM_VF_RE(index), vfre);
507
508	/* pull current max frame size from hardware */
509	max_frs = DIV_ROUND_UP(max_frame, 1024);
510	reg_val = rd32(wx, WX_MAC_WDG_TIMEOUT) & WX_MAC_WDG_TIMEOUT_WTO_MASK;
511	if (max_frs > (reg_val + WX_MAC_WDG_TIMEOUT_WTO_DELTA))
512		wr32(wx, WX_MAC_WDG_TIMEOUT,
513		     max_frs - WX_MAC_WDG_TIMEOUT_WTO_DELTA);
514}
515
516static int wx_find_vlvf_entry(struct wx *wx, u32 vlan)
517{
518	int regindex;
519	u32 vlvf;
520
521	/* short cut the special case */
522	if (vlan == 0)
523		return 0;
524
525	/* Search for the vlan id in the VLVF entries */
526	for (regindex = 1; regindex < WX_PSR_VLAN_SWC_ENTRIES; regindex++) {
527		wr32(wx, WX_PSR_VLAN_SWC_IDX, regindex);
528		vlvf = rd32(wx, WX_PSR_VLAN_SWC);
529		if ((vlvf & VLAN_VID_MASK) == vlan)
530			break;
531	}
532
533	/* Return a negative value if not found */
534	if (regindex >= WX_PSR_VLAN_SWC_ENTRIES)
535		regindex = -EINVAL;
536
537	return regindex;
538}
539
540static int wx_set_vf_macvlan(struct wx *wx,
541			     u16 vf, int index, unsigned char *mac_addr)
542{
543	struct vf_macvlans *entry;
544	struct list_head *pos;
545	int retval = 0;
546
547	if (index <= 1) {
548		list_for_each(pos, &wx->vf_mvs.mvlist) {
549			entry = list_entry(pos, struct vf_macvlans, mvlist);
550			if (entry->vf == vf) {
551				entry->vf = -1;
552				entry->free = true;
553				entry->is_macvlan = false;
554				wx_del_mac_filter(wx, entry->vf_macvlan, vf);
555			}
556		}
557	}
558
559	if (!index)
560		return 0;
561
562	entry = NULL;
563	list_for_each(pos, &wx->vf_mvs.mvlist) {
564		entry = list_entry(pos, struct vf_macvlans, mvlist);
565		if (entry->free)
566			break;
567	}
568
569	if (!entry || !entry->free)
570		return -ENOSPC;
571
572	retval = wx_add_mac_filter(wx, mac_addr, vf);
573	if (retval >= 0) {
574		entry->free = false;
575		entry->is_macvlan = true;
576		entry->vf = vf;
577		memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
578	}
579
580	return retval;
581}
582
583static int wx_set_vf_vlan_msg(struct wx *wx, u32 *msgbuf, u16 vf)
584{
585	int add = (msgbuf[0] & WX_VT_MSGINFO_MASK) >> WX_VT_MSGINFO_SHIFT;
586	int vid = (msgbuf[1] & WX_PSR_VLAN_SWC_VLANID_MASK);
587	int ret;
588
589	if (add)
590		wx->vfinfo[vf].vlan_count++;
591	else if (wx->vfinfo[vf].vlan_count)
592		wx->vfinfo[vf].vlan_count--;
593
594	/* in case of promiscuous mode any VLAN filter set for a VF must
595	 * also have the PF pool added to it.
596	 */
597	if (add && wx->netdev->flags & IFF_PROMISC)
598		wx_set_vf_vlan(wx, add, vid, VMDQ_P(0));
599
600	ret = wx_set_vf_vlan(wx, add, vid, vf);
601	if (!ret && wx->vfinfo[vf].spoofchk_enabled)
602		wx_set_vlan_anti_spoofing(wx, true, vf);
603
604	/* Go through all the checks to see if the VLAN filter should
605	 * be wiped completely.
606	 */
607	if (!add && wx->netdev->flags & IFF_PROMISC) {
608		u32 bits = 0, vlvf;
609		int reg_ndx;
610
611		reg_ndx = wx_find_vlvf_entry(wx, vid);
612		if (reg_ndx < 0)
613			return -ENOSPC;
614		wr32(wx, WX_PSR_VLAN_SWC_IDX, reg_ndx);
615		vlvf = rd32(wx, WX_PSR_VLAN_SWC);
616		/* See if any other pools are set for this VLAN filter
617		 * entry other than the PF.
618		 */
619		if (VMDQ_P(0) < 32) {
620			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_L);
621			bits &= ~BIT(VMDQ_P(0));
622			if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags))
623				bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_H);
624		} else {
625			if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags))
626				bits = rd32(wx, WX_PSR_VLAN_SWC_VM_H);
627			bits &= ~BIT(VMDQ_P(0) % 32);
628			bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_L);
629		}
630		/* If the filter was removed then ensure PF pool bit
631		 * is cleared if the PF only added itself to the pool
632		 * because the PF is in promiscuous mode.
633		 */
634		if ((vlvf & VLAN_VID_MASK) == vid && !bits)
635			wx_set_vf_vlan(wx, add, vid, VMDQ_P(0));
636	}
637
638	return 0;
639}
640
641static int wx_set_vf_macvlan_msg(struct wx *wx, u32 *msgbuf, u16 vf)
642{
643	int index = (msgbuf[0] & WX_VT_MSGINFO_MASK) >>
644		    WX_VT_MSGINFO_SHIFT;
645	u8 *new_mac = ((u8 *)(&msgbuf[1]));
646	int err;
647
648	if (wx->vfinfo[vf].pf_set_mac && index > 0) {
649		wx_err(wx, "VF %d request MACVLAN filter but is denied\n", vf);
650		return -EINVAL;
651	}
652
653	/* An non-zero index indicates the VF is setting a filter */
654	if (index) {
655		if (!is_valid_ether_addr(new_mac)) {
656			wx_err(wx, "VF %d attempted to set invalid mac\n", vf);
657			return -EINVAL;
658		}
659		/* If the VF is allowed to set MAC filters then turn off
660		 * anti-spoofing to avoid false positives.
661		 */
662		if (wx->vfinfo[vf].spoofchk_enabled)
663			wx_set_vf_spoofchk(wx->netdev, vf, false);
664	}
665
666	err = wx_set_vf_macvlan(wx, vf, index, new_mac);
667	if (err == -ENOSPC)
668		wx_err(wx,
669		       "VF %d request MACVLAN filter but there is no space\n",
670		       vf);
671	if (err < 0)
672		return err;
673
674	return 0;
675}
676
677static int wx_negotiate_vf_api(struct wx *wx, u32 *msgbuf, u32 vf)
678{
679	int api = msgbuf[1];
680
681	switch (api) {
682	case wx_mbox_api_13:
683		wx->vfinfo[vf].vf_api = api;
684		return 0;
685	default:
686		wx_err(wx, "VF %d requested invalid api version %u\n", vf, api);
687		return -EINVAL;
688	}
689}
690
691static int wx_get_vf_link_state(struct wx *wx, u32 *msgbuf, u32 vf)
692{
693	msgbuf[1] = wx->vfinfo[vf].link_enable;
694
695	return 0;
696}
697
698static int wx_get_fw_version(struct wx *wx, u32 *msgbuf, u32 vf)
699{
700	unsigned long fw_version = 0ULL;
701	int ret = 0;
702
703	ret = kstrtoul(wx->eeprom_id, 16, &fw_version);
704	if (ret)
705		return -EOPNOTSUPP;
706	msgbuf[1] = fw_version;
707
708	return 0;
709}
710
711static int wx_update_vf_xcast_mode(struct wx *wx, u32 *msgbuf, u32 vf)
712{
713	int xcast_mode = msgbuf[1];
714	u32 vmolr, disable, enable;
715
716	if (wx->vfinfo[vf].xcast_mode == xcast_mode)
717		return 0;
718
719	switch (xcast_mode) {
720	case WXVF_XCAST_MODE_NONE:
721		disable = WX_PSR_VM_L2CTL_BAM | WX_PSR_VM_L2CTL_ROMPE |
722			  WX_PSR_VM_L2CTL_MPE | WX_PSR_VM_L2CTL_UPE |
723			  WX_PSR_VM_L2CTL_VPE;
724		enable = 0;
725		break;
726	case WXVF_XCAST_MODE_MULTI:
727		disable = WX_PSR_VM_L2CTL_MPE | WX_PSR_VM_L2CTL_UPE |
728			  WX_PSR_VM_L2CTL_VPE;
729		enable = WX_PSR_VM_L2CTL_BAM | WX_PSR_VM_L2CTL_ROMPE;
730		break;
731	case WXVF_XCAST_MODE_ALLMULTI:
732		disable = WX_PSR_VM_L2CTL_UPE | WX_PSR_VM_L2CTL_VPE;
733		enable = WX_PSR_VM_L2CTL_BAM | WX_PSR_VM_L2CTL_ROMPE |
734			 WX_PSR_VM_L2CTL_MPE;
735		break;
736	case WXVF_XCAST_MODE_PROMISC:
737		disable = 0;
738		enable = WX_PSR_VM_L2CTL_BAM | WX_PSR_VM_L2CTL_ROMPE |
739			 WX_PSR_VM_L2CTL_MPE | WX_PSR_VM_L2CTL_UPE |
740			 WX_PSR_VM_L2CTL_VPE;
741		break;
742	default:
743		return -EOPNOTSUPP;
744	}
745
746	vmolr = rd32(wx, WX_PSR_VM_L2CTL(vf));
747	vmolr &= ~disable;
748	vmolr |= enable;
749	wr32(wx, WX_PSR_VM_L2CTL(vf), vmolr);
750
751	wx->vfinfo[vf].xcast_mode = xcast_mode;
752	msgbuf[1] = xcast_mode;
753
754	return 0;
755}
756
757static void wx_rcv_msg_from_vf(struct wx *wx, u16 vf)
758{
759	u16 mbx_size = WX_VXMAILBOX_SIZE;
760	u32 msgbuf[WX_VXMAILBOX_SIZE];
761	int retval;
762
763	retval = wx_read_mbx_pf(wx, msgbuf, mbx_size, vf);
764	if (retval) {
765		wx_err(wx, "Error receiving message from VF\n");
766		return;
767	}
768
769	/* this is a message we already processed, do nothing */
770	if (msgbuf[0] & (WX_VT_MSGTYPE_ACK | WX_VT_MSGTYPE_NACK))
771		return;
772
773	if (msgbuf[0] == WX_VF_RESET) {
774		wx_vf_reset_msg(wx, vf);
775		return;
776	}
777
778	/* until the vf completes a virtual function reset it should not be
779	 * allowed to start any configuration.
780	 */
781	if (!wx->vfinfo[vf].clear_to_send) {
782		msgbuf[0] |= WX_VT_MSGTYPE_NACK;
783		wx_write_mbx_pf(wx, msgbuf, 1, vf);
784		return;
785	}
786
787	switch ((msgbuf[0] & U16_MAX)) {
788	case WX_VF_SET_MAC_ADDR:
789		retval = wx_set_vf_mac_addr(wx, msgbuf, vf);
790		break;
791	case WX_VF_SET_MULTICAST:
792		wx_set_vf_multicasts(wx, msgbuf, vf);
793		retval = 0;
794		break;
795	case WX_VF_SET_VLAN:
796		retval = wx_set_vf_vlan_msg(wx, msgbuf, vf);
797		break;
798	case WX_VF_SET_LPE:
799		wx_set_vf_lpe(wx, msgbuf[1], vf);
800		retval = 0;
801		break;
802	case WX_VF_SET_MACVLAN:
803		retval = wx_set_vf_macvlan_msg(wx, msgbuf, vf);
804		break;
805	case WX_VF_API_NEGOTIATE:
806		retval = wx_negotiate_vf_api(wx, msgbuf, vf);
807		break;
808	case WX_VF_GET_QUEUES:
809		retval = wx_get_vf_queues(wx, msgbuf, vf);
810		break;
811	case WX_VF_GET_LINK_STATE:
812		retval = wx_get_vf_link_state(wx, msgbuf, vf);
813		break;
814	case WX_VF_GET_FW_VERSION:
815		retval = wx_get_fw_version(wx, msgbuf, vf);
816		break;
817	case WX_VF_UPDATE_XCAST_MODE:
818		retval = wx_update_vf_xcast_mode(wx, msgbuf, vf);
819		break;
820	case WX_VF_BACKUP:
821		break;
822	default:
823		wx_err(wx, "Unhandled Msg %8.8x\n", msgbuf[0]);
824		break;
825	}
826
827	/* notify the VF of the results of what it sent us */
828	if (retval)
829		msgbuf[0] |= WX_VT_MSGTYPE_NACK;
830	else
831		msgbuf[0] |= WX_VT_MSGTYPE_ACK;
832
833	msgbuf[0] |= WX_VT_MSGTYPE_CTS;
834
835	wx_write_mbx_pf(wx, msgbuf, mbx_size, vf);
836}
837
838static void wx_rcv_ack_from_vf(struct wx *wx, u16 vf)
839{
840	u32 msg = WX_VT_MSGTYPE_NACK;
841
842	/* if device isn't clear to send it shouldn't be reading either */
843	if (!wx->vfinfo[vf].clear_to_send)
844		wx_write_mbx_pf(wx, &msg, 1, vf);
845}
846
847void wx_msg_task(struct wx *wx)
848{
849	u16 vf;
850
851	for (vf = 0; vf < wx->num_vfs; vf++) {
852		/* process any reset requests */
853		if (!wx_check_for_rst_pf(wx, vf))
854			wx_vf_reset_event(wx, vf);
855
856		/* process any messages pending */
857		if (!wx_check_for_msg_pf(wx, vf))
858			wx_rcv_msg_from_vf(wx, vf);
859
860		/* process any acks */
861		if (!wx_check_for_ack_pf(wx, vf))
862			wx_rcv_ack_from_vf(wx, vf);
863	}
864}
865EXPORT_SYMBOL(wx_msg_task);
866
867void wx_disable_vf_rx_tx(struct wx *wx)
868{
869	wr32(wx, WX_TDM_VFTE_CLR(0), U32_MAX);
870	wr32(wx, WX_RDM_VFRE_CLR(0), U32_MAX);
871	if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
872		wr32(wx, WX_TDM_VFTE_CLR(1), U32_MAX);
873		wr32(wx, WX_RDM_VFRE_CLR(1), U32_MAX);
874	}
875}
876EXPORT_SYMBOL(wx_disable_vf_rx_tx);
877
878void wx_ping_all_vfs_with_link_status(struct wx *wx, bool link_up)
879{
880	u32 msgbuf[2] = {0, 0};
881	u16 i;
882
883	if (!wx->num_vfs)
884		return;
885	msgbuf[0] = WX_PF_NOFITY_VF_LINK_STATUS | WX_PF_CONTROL_MSG;
886	if (link_up)
887		msgbuf[1] = FIELD_PREP(GENMASK(31, 1), wx->speed) | link_up;
888	if (wx->notify_down)
889		msgbuf[1] |= WX_PF_NOFITY_VF_NET_NOT_RUNNING;
890	for (i = 0; i < wx->num_vfs; i++) {
891		if (wx->vfinfo[i].clear_to_send)
892			msgbuf[0] |= WX_VT_MSGTYPE_CTS;
893		wx_write_mbx_pf(wx, msgbuf, 2, i);
894	}
895}
896EXPORT_SYMBOL(wx_ping_all_vfs_with_link_status);
897
898static void wx_set_vf_link_state(struct wx *wx, int vf, int state)
899{
900	wx->vfinfo[vf].link_state = state;
901	switch (state) {
902	case IFLA_VF_LINK_STATE_AUTO:
903		if (netif_running(wx->netdev))
904			wx->vfinfo[vf].link_enable = true;
905		else
906			wx->vfinfo[vf].link_enable = false;
907		break;
908	case IFLA_VF_LINK_STATE_ENABLE:
909		wx->vfinfo[vf].link_enable = true;
910		break;
911	case IFLA_VF_LINK_STATE_DISABLE:
912		wx->vfinfo[vf].link_enable = false;
913		break;
914	}
915	/* restart the VF */
916	wx->vfinfo[vf].clear_to_send = false;
917	wx_ping_vf(wx, vf);
918
919	wx_set_vf_rx_tx(wx, vf);
920}
921
922void wx_set_all_vfs(struct wx *wx)
923{
924	int i;
925
926	for (i = 0; i < wx->num_vfs; i++)
927		wx_set_vf_link_state(wx, i, wx->vfinfo[i].link_state);
928}
929EXPORT_SYMBOL(wx_set_all_vfs);
~/ Read-only file preview.