xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_sriov.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
3 
4 #include <linux/types.h>
5 #include <linux/module.h>
6 #include <linux/pci.h>
7 #include <linux/netdevice.h>
8 #include <linux/vmalloc.h>
9 #include <linux/string.h>
10 #include <linux/in.h>
11 #include <linux/ip.h>
12 #include <linux/tcp.h>
13 #include <linux/ipv6.h>
14 #include <linux/if_bridge.h>
15 #ifdef NETIF_F_HW_VLAN_CTAG_TX
16 #include <linux/if_vlan.h>
17 #endif
18 
19 #include "ixgbe.h"
20 #include "ixgbe_type.h"
21 #include "ixgbe_mbx.h"
22 #include "ixgbe_sriov.h"
23 
24 #ifdef CONFIG_PCI_IOV
ixgbe_alloc_vf_macvlans(struct ixgbe_adapter * adapter,unsigned int num_vfs)25 static inline void ixgbe_alloc_vf_macvlans(struct ixgbe_adapter *adapter,
26 					   unsigned int num_vfs)
27 {
28 	struct ixgbe_hw *hw = &adapter->hw;
29 	struct vf_macvlans *mv_list;
30 	int num_vf_macvlans, i;
31 
32 	/* Initialize list of VF macvlans */
33 	INIT_LIST_HEAD(&adapter->vf_mvs.l);
34 
35 	num_vf_macvlans = hw->mac.num_rar_entries -
36 			  (IXGBE_MAX_PF_MACVLANS + 1 + num_vfs);
37 	if (!num_vf_macvlans)
38 		return;
39 
40 	mv_list = kzalloc_objs(struct vf_macvlans, num_vf_macvlans);
41 	if (mv_list) {
42 		for (i = 0; i < num_vf_macvlans; i++) {
43 			mv_list[i].vf = -1;
44 			mv_list[i].free = true;
45 			list_add(&mv_list[i].l, &adapter->vf_mvs.l);
46 		}
47 		adapter->mv_list = mv_list;
48 	}
49 }
50 
__ixgbe_enable_sriov(struct ixgbe_adapter * adapter,unsigned int num_vfs)51 static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
52 				unsigned int num_vfs)
53 {
54 	struct ixgbe_hw *hw = &adapter->hw;
55 	int i;
56 
57 	if (adapter->xdp_prog) {
58 		e_warn(probe, "SRIOV is not supported with XDP\n");
59 		return -EINVAL;
60 	}
61 
62 	/* Enable VMDq flag so device will be set in VM mode */
63 	adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED |
64 			  IXGBE_FLAG_VMDQ_ENABLED;
65 
66 	/* Allocate memory for per VF control structures */
67 	adapter->vfinfo = kzalloc_objs(struct vf_data_storage, num_vfs);
68 	if (!adapter->vfinfo)
69 		return -ENOMEM;
70 
71 	adapter->num_vfs = num_vfs;
72 
73 	ixgbe_alloc_vf_macvlans(adapter, num_vfs);
74 	adapter->ring_feature[RING_F_VMDQ].offset = num_vfs;
75 
76 	/* Initialize default switching mode VEB */
77 	IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
78 	adapter->bridge_mode = BRIDGE_MODE_VEB;
79 
80 	/* limit traffic classes based on VFs enabled */
81 	if ((adapter->hw.mac.type == ixgbe_mac_82599EB) && (num_vfs < 16)) {
82 		adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
83 		adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
84 	} else if (num_vfs < 32) {
85 		adapter->dcb_cfg.num_tcs.pg_tcs = 4;
86 		adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
87 	} else {
88 		adapter->dcb_cfg.num_tcs.pg_tcs = 1;
89 		adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
90 	}
91 
92 	/* Disable RSC when in SR-IOV mode */
93 	adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
94 			     IXGBE_FLAG2_RSC_ENABLED);
95 
96 	for (i = 0; i < num_vfs; i++) {
97 		/* enable spoof checking for all VFs */
98 		adapter->vfinfo[i].spoofchk_enabled = true;
99 		adapter->vfinfo[i].link_enable = true;
100 
101 		/* We support VF RSS querying only for 82599 and x540
102 		 * devices at the moment. These devices share RSS
103 		 * indirection table and RSS hash key with PF therefore
104 		 * we want to disable the querying by default.
105 		 */
106 		adapter->vfinfo[i].rss_query_enabled = false;
107 
108 		/* Untrust all VFs */
109 		adapter->vfinfo[i].trusted = false;
110 
111 		/* set the default xcast mode */
112 		adapter->vfinfo[i].xcast_mode = IXGBEVF_XCAST_MODE_NONE;
113 	}
114 
115 	e_info(probe, "SR-IOV enabled with %d VFs\n", num_vfs);
116 	return 0;
117 }
118 
119 /**
120  * ixgbe_get_vfs - Find and take references to all vf devices
121  * @adapter: Pointer to adapter struct
122  */
ixgbe_get_vfs(struct ixgbe_adapter * adapter)123 static void ixgbe_get_vfs(struct ixgbe_adapter *adapter)
124 {
125 	struct pci_dev *pdev = adapter->pdev;
126 	u16 vendor = pdev->vendor;
127 	struct pci_dev *vfdev;
128 	int vf = 0;
129 	u16 vf_id;
130 	int pos;
131 
132 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
133 	if (!pos)
134 		return;
135 	pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_id);
136 
137 	vfdev = pci_get_device(vendor, vf_id, NULL);
138 	for (; vfdev; vfdev = pci_get_device(vendor, vf_id, vfdev)) {
139 		if (!vfdev->is_virtfn)
140 			continue;
141 		if (vfdev->physfn != pdev)
142 			continue;
143 		if (vf >= adapter->num_vfs)
144 			continue;
145 		pci_dev_get(vfdev);
146 		adapter->vfinfo[vf].vfdev = vfdev;
147 		++vf;
148 	}
149 }
150 
151 /* Note this function is called when the user wants to enable SR-IOV
152  * VFs using the now deprecated module parameter
153  */
ixgbe_enable_sriov(struct ixgbe_adapter * adapter,unsigned int max_vfs)154 void ixgbe_enable_sriov(struct ixgbe_adapter *adapter, unsigned int max_vfs)
155 {
156 	int pre_existing_vfs = 0;
157 	unsigned int num_vfs;
158 
159 	pre_existing_vfs = pci_num_vf(adapter->pdev);
160 	if (!pre_existing_vfs && !max_vfs)
161 		return;
162 
163 	/* If there are pre-existing VFs then we have to force
164 	 * use of that many - over ride any module parameter value.
165 	 * This may result from the user unloading the PF driver
166 	 * while VFs were assigned to guest VMs or because the VFs
167 	 * have been created via the new PCI SR-IOV sysfs interface.
168 	 */
169 	if (pre_existing_vfs) {
170 		num_vfs = pre_existing_vfs;
171 		dev_warn(&adapter->pdev->dev,
172 			 "Virtual Functions already enabled for this device - Please reload all VF drivers to avoid spoofed packet errors\n");
173 	} else {
174 		int err;
175 		/*
176 		 * The 82599 supports up to 64 VFs per physical function
177 		 * but this implementation limits allocation to 63 so that
178 		 * basic networking resources are still available to the
179 		 * physical function.  If the user requests greater than
180 		 * 63 VFs then it is an error - reset to default of zero.
181 		 */
182 		num_vfs = min_t(unsigned int, max_vfs, IXGBE_MAX_VFS_DRV_LIMIT);
183 
184 		err = pci_enable_sriov(adapter->pdev, num_vfs);
185 		if (err) {
186 			e_err(probe, "Failed to enable PCI sriov: %d\n", err);
187 			return;
188 		}
189 	}
190 
191 	if (!__ixgbe_enable_sriov(adapter, num_vfs)) {
192 		ixgbe_get_vfs(adapter);
193 		return;
194 	}
195 
196 	/* If we have gotten to this point then there is no memory available
197 	 * to manage the VF devices - print message and bail.
198 	 */
199 	e_err(probe, "Unable to allocate memory for VF Data Storage - "
200 	      "SRIOV disabled\n");
201 	ixgbe_disable_sriov(adapter);
202 }
203 
204 #endif /* #ifdef CONFIG_PCI_IOV */
ixgbe_disable_sriov(struct ixgbe_adapter * adapter)205 int ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
206 {
207 	unsigned int num_vfs = adapter->num_vfs, vf;
208 	struct ixgbe_hw *hw = &adapter->hw;
209 	unsigned long flags;
210 	int rss;
211 
212 	spin_lock_irqsave(&adapter->vfs_lock, flags);
213 	/* set num VFs to 0 to prevent access to vfinfo */
214 	adapter->num_vfs = 0;
215 	spin_unlock_irqrestore(&adapter->vfs_lock, flags);
216 
217 	/* put the reference to all of the vf devices */
218 	for (vf = 0; vf < num_vfs; ++vf) {
219 		struct pci_dev *vfdev = adapter->vfinfo[vf].vfdev;
220 
221 		if (!vfdev)
222 			continue;
223 		adapter->vfinfo[vf].vfdev = NULL;
224 		pci_dev_put(vfdev);
225 	}
226 
227 	/* free VF control structures */
228 	kfree(adapter->vfinfo);
229 	adapter->vfinfo = NULL;
230 
231 	/* free macvlan list */
232 	kfree(adapter->mv_list);
233 	adapter->mv_list = NULL;
234 
235 	/* if SR-IOV is already disabled then there is nothing to do */
236 	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
237 		return 0;
238 
239 	if (hw->mac.ops.disable_mdd)
240 		hw->mac.ops.disable_mdd(hw);
241 
242 #ifdef CONFIG_PCI_IOV
243 	/*
244 	 * If our VFs are assigned we cannot shut down SR-IOV
245 	 * without causing issues, so just leave the hardware
246 	 * available but disabled
247 	 */
248 	if (pci_vfs_assigned(adapter->pdev)) {
249 		e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
250 		return -EPERM;
251 	}
252 	/* disable iov and allow time for transactions to clear */
253 	pci_disable_sriov(adapter->pdev);
254 #endif
255 
256 	/* Disable VMDq flag so device will be set in VM mode */
257 	if (bitmap_weight(adapter->fwd_bitmask, adapter->num_rx_pools) == 1) {
258 		adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
259 		adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
260 		rss = min_t(int, ixgbe_max_rss_indices(adapter),
261 			    num_online_cpus());
262 	} else {
263 		rss = min_t(int, IXGBE_MAX_L2A_QUEUES, num_online_cpus());
264 	}
265 
266 	adapter->ring_feature[RING_F_VMDQ].offset = 0;
267 	adapter->ring_feature[RING_F_RSS].limit = rss;
268 
269 	/* take a breather then clean up driver data */
270 	msleep(100);
271 	return 0;
272 }
273 
ixgbe_pci_sriov_enable(struct pci_dev * dev,int num_vfs)274 static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs)
275 {
276 #ifdef CONFIG_PCI_IOV
277 	struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
278 	int pre_existing_vfs = pci_num_vf(dev);
279 	int err = 0, num_rx_pools, i, limit;
280 	u8 num_tc;
281 
282 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
283 		err = ixgbe_disable_sriov(adapter);
284 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
285 		return num_vfs;
286 
287 	if (err)
288 		return err;
289 
290 	/* While the SR-IOV capability structure reports total VFs to be 64,
291 	 * we limit the actual number allocated as below based on two factors.
292 	 *    Num_TCs	MAX_VFs
293 	 *	1	  63
294 	 *	<=4	  31
295 	 *	>4	  15
296 	 * First, we reserve some transmit/receive resources for the PF.
297 	 * Second, VMDQ also uses the same pools that SR-IOV does. We need to
298 	 * account for this, so that we don't accidentally allocate more VFs
299 	 * than we have available pools. The PCI bus driver already checks for
300 	 * other values out of range.
301 	 */
302 	num_tc = adapter->hw_tcs;
303 	num_rx_pools = bitmap_weight(adapter->fwd_bitmask,
304 				     adapter->num_rx_pools);
305 	limit = (num_tc > 4) ? IXGBE_MAX_VFS_8TC :
306 		(num_tc > 1) ? IXGBE_MAX_VFS_4TC : IXGBE_MAX_VFS_1TC;
307 
308 	if (num_vfs > (limit - num_rx_pools)) {
309 		e_dev_err("Currently configured with %d TCs, and %d offloaded macvlans. Creating more than %d VFs is not allowed\n",
310 			  num_tc, num_rx_pools - 1, limit - num_rx_pools);
311 		return -EPERM;
312 	}
313 
314 	err = __ixgbe_enable_sriov(adapter, num_vfs);
315 	if (err)
316 		return  err;
317 
318 	for (i = 0; i < num_vfs; i++)
319 		ixgbe_vf_configuration(dev, (i | 0x10000000));
320 
321 	/* reset before enabling SRIOV to avoid mailbox issues */
322 	ixgbe_sriov_reinit(adapter);
323 
324 	err = pci_enable_sriov(dev, num_vfs);
325 	if (err) {
326 		e_dev_warn("Failed to enable PCI sriov: %d\n", err);
327 		return err;
328 	}
329 	ixgbe_get_vfs(adapter);
330 
331 	return num_vfs;
332 #else
333 	return 0;
334 #endif
335 }
336 
ixgbe_pci_sriov_disable(struct pci_dev * dev)337 static int ixgbe_pci_sriov_disable(struct pci_dev *dev)
338 {
339 	struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
340 	int err;
341 #ifdef CONFIG_PCI_IOV
342 	u32 current_flags = adapter->flags;
343 	int prev_num_vf = pci_num_vf(dev);
344 #endif
345 
346 	err = ixgbe_disable_sriov(adapter);
347 
348 	/* Only reinit if no error and state changed */
349 #ifdef CONFIG_PCI_IOV
350 	if (!err && (current_flags != adapter->flags ||
351 		     prev_num_vf != pci_num_vf(dev)))
352 		ixgbe_sriov_reinit(adapter);
353 #endif
354 
355 	return err;
356 }
357 
ixgbe_pci_sriov_configure(struct pci_dev * dev,int num_vfs)358 int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
359 {
360 	if (num_vfs == 0)
361 		return ixgbe_pci_sriov_disable(dev);
362 	else
363 		return ixgbe_pci_sriov_enable(dev, num_vfs);
364 }
365 
ixgbe_set_vf_multicasts(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)366 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
367 				   u32 *msgbuf, u32 vf)
368 {
369 	int entries = FIELD_GET(IXGBE_VT_MSGINFO_MASK, msgbuf[0]);
370 	u16 *hash_list = (u16 *)&msgbuf[1];
371 	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
372 	struct ixgbe_hw *hw = &adapter->hw;
373 	int i;
374 	u32 vector_bit;
375 	u32 vector_reg;
376 	u32 mta_reg;
377 	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
378 
379 	/* only so many hash values supported */
380 	entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
381 
382 	/*
383 	 * salt away the number of multi cast addresses assigned
384 	 * to this VF for later use to restore when the PF multi cast
385 	 * list changes
386 	 */
387 	vfinfo->num_vf_mc_hashes = entries;
388 
389 	/*
390 	 * VFs are limited to using the MTA hash table for their multicast
391 	 * addresses
392 	 */
393 	for (i = 0; i < entries; i++) {
394 		vfinfo->vf_mc_hashes[i] = hash_list[i];
395 	}
396 
397 	for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
398 		vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
399 		vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
400 		mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
401 		mta_reg |= BIT(vector_bit);
402 		IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
403 	}
404 	vmolr |= IXGBE_VMOLR_ROMPE;
405 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
406 
407 	return 0;
408 }
409 
410 #ifdef CONFIG_PCI_IOV
ixgbe_restore_vf_multicasts(struct ixgbe_adapter * adapter)411 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
412 {
413 	struct ixgbe_hw *hw = &adapter->hw;
414 	struct vf_data_storage *vfinfo;
415 	int i, j;
416 	u32 vector_bit;
417 	u32 vector_reg;
418 	u32 mta_reg;
419 
420 	for (i = 0; i < adapter->num_vfs; i++) {
421 		u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(i));
422 		vfinfo = &adapter->vfinfo[i];
423 		for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
424 			hw->addr_ctrl.mta_in_use++;
425 			vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
426 			vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
427 			mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
428 			mta_reg |= BIT(vector_bit);
429 			IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
430 		}
431 
432 		if (vfinfo->num_vf_mc_hashes)
433 			vmolr |= IXGBE_VMOLR_ROMPE;
434 		else
435 			vmolr &= ~IXGBE_VMOLR_ROMPE;
436 		IXGBE_WRITE_REG(hw, IXGBE_VMOLR(i), vmolr);
437 	}
438 
439 	/* Restore any VF macvlans */
440 	ixgbe_full_sync_mac_table(adapter);
441 }
442 #endif
443 
ixgbe_set_vf_vlan(struct ixgbe_adapter * adapter,int add,int vid,u32 vf)444 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
445 			     u32 vf)
446 {
447 	struct ixgbe_hw *hw = &adapter->hw;
448 	int err;
449 
450 	/* If VLAN overlaps with one the PF is currently monitoring make
451 	 * sure that we are able to allocate a VLVF entry.  This may be
452 	 * redundant but it guarantees PF will maintain visibility to
453 	 * the VLAN.
454 	 */
455 	if (add && test_bit(vid, adapter->active_vlans)) {
456 		err = hw->mac.ops.set_vfta(hw, vid, VMDQ_P(0), true, false);
457 		if (err)
458 			return err;
459 	}
460 
461 	err = hw->mac.ops.set_vfta(hw, vid, vf, !!add, false);
462 
463 	if (add && !err)
464 		return err;
465 
466 	/* If we failed to add the VF VLAN or we are removing the VF VLAN
467 	 * we may need to drop the PF pool bit in order to allow us to free
468 	 * up the VLVF resources.
469 	 */
470 	if (test_bit(vid, adapter->active_vlans) ||
471 	    (adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
472 		ixgbe_update_pf_promisc_vlvf(adapter, vid);
473 
474 	return err;
475 }
476 
ixgbe_set_vf_lpe(struct ixgbe_adapter * adapter,u32 max_frame,u32 vf)477 static int ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 max_frame, u32 vf)
478 {
479 	struct ixgbe_hw *hw = &adapter->hw;
480 	u32 max_frs;
481 
482 	if (max_frame < ETH_MIN_MTU || max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
483 		e_err(drv, "VF max_frame %d out of range\n", max_frame);
484 		return -EINVAL;
485 	}
486 
487 	/*
488 	 * For 82599EB we have to keep all PFs and VFs operating with
489 	 * the same max_frame value in order to avoid sending an oversize
490 	 * frame to a VF.  In order to guarantee this is handled correctly
491 	 * for all cases we have several special exceptions to take into
492 	 * account before we can enable the VF for receive
493 	 */
494 	if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
495 		struct net_device *dev = adapter->netdev;
496 		int pf_max_frame = dev->mtu + ETH_HLEN;
497 		u32 reg_offset, vf_shift, vfre;
498 		int err = 0;
499 
500 #ifdef CONFIG_FCOE
501 		if (dev->fcoe_mtu)
502 			pf_max_frame = max_t(int, pf_max_frame,
503 					     IXGBE_FCOE_JUMBO_FRAME_SIZE);
504 
505 #endif /* CONFIG_FCOE */
506 		switch (adapter->vfinfo[vf].vf_api) {
507 		case ixgbe_mbox_api_11:
508 		case ixgbe_mbox_api_12:
509 		case ixgbe_mbox_api_13:
510 		case ixgbe_mbox_api_14:
511 		case ixgbe_mbox_api_16:
512 		case ixgbe_mbox_api_17:
513 			/* Version 1.1 supports jumbo frames on VFs if PF has
514 			 * jumbo frames enabled which means legacy VFs are
515 			 * disabled
516 			 */
517 			if (pf_max_frame > ETH_FRAME_LEN)
518 				break;
519 			fallthrough;
520 		default:
521 			/* If the PF or VF are running w/ jumbo frames enabled
522 			 * we need to shut down the VF Rx path as we cannot
523 			 * support jumbo frames on legacy VFs
524 			 */
525 			if ((pf_max_frame > ETH_FRAME_LEN) ||
526 			    (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
527 				err = -EINVAL;
528 			break;
529 		}
530 
531 		/* determine VF receive enable location */
532 		vf_shift = vf % 32;
533 		reg_offset = vf / 32;
534 
535 		/* enable or disable receive depending on error */
536 		vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
537 		if (err)
538 			vfre &= ~BIT(vf_shift);
539 		else
540 			vfre |= BIT(vf_shift);
541 		IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);
542 
543 		if (err) {
544 			e_err(drv, "VF max_frame %d out of range\n", max_frame);
545 			return err;
546 		}
547 	}
548 
549 	/* pull current max frame size from hardware */
550 	max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
551 	max_frs &= IXGBE_MHADD_MFS_MASK;
552 	max_frs >>= IXGBE_MHADD_MFS_SHIFT;
553 
554 	if (max_frs < max_frame) {
555 		max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
556 		IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
557 	}
558 
559 	e_info(hw, "VF requests change max MTU to %d\n", max_frame);
560 
561 	return 0;
562 }
563 
ixgbe_set_vmolr(struct ixgbe_hw * hw,u32 vf,bool aupe)564 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
565 {
566 	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
567 	vmolr |= IXGBE_VMOLR_BAM;
568 	if (aupe)
569 		vmolr |= IXGBE_VMOLR_AUPE;
570 	else
571 		vmolr &= ~IXGBE_VMOLR_AUPE;
572 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
573 }
574 
ixgbe_clear_vmvir(struct ixgbe_adapter * adapter,u32 vf)575 static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf)
576 {
577 	struct ixgbe_hw *hw = &adapter->hw;
578 
579 	IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
580 }
581 
ixgbe_clear_vf_vlans(struct ixgbe_adapter * adapter,u32 vf)582 static void ixgbe_clear_vf_vlans(struct ixgbe_adapter *adapter, u32 vf)
583 {
584 	struct ixgbe_hw *hw = &adapter->hw;
585 	u32 vlvfb_mask, pool_mask, i;
586 
587 	/* create mask for VF and other pools */
588 	pool_mask = ~BIT(VMDQ_P(0) % 32);
589 	vlvfb_mask = BIT(vf % 32);
590 
591 	/* post increment loop, covers VLVF_ENTRIES - 1 to 0 */
592 	for (i = IXGBE_VLVF_ENTRIES; i--;) {
593 		u32 bits[2], vlvfb, vid, vfta, vlvf;
594 		u32 word = i * 2 + vf / 32;
595 		u32 mask;
596 
597 		vlvfb = IXGBE_READ_REG(hw, IXGBE_VLVFB(word));
598 
599 		/* if our bit isn't set we can skip it */
600 		if (!(vlvfb & vlvfb_mask))
601 			continue;
602 
603 		/* clear our bit from vlvfb */
604 		vlvfb ^= vlvfb_mask;
605 
606 		/* create 64b mask to chedk to see if we should clear VLVF */
607 		bits[word % 2] = vlvfb;
608 		bits[~word % 2] = IXGBE_READ_REG(hw, IXGBE_VLVFB(word ^ 1));
609 
610 		/* if other pools are present, just remove ourselves */
611 		if (bits[(VMDQ_P(0) / 32) ^ 1] ||
612 		    (bits[VMDQ_P(0) / 32] & pool_mask))
613 			goto update_vlvfb;
614 
615 		/* if PF is present, leave VFTA */
616 		if (bits[0] || bits[1])
617 			goto update_vlvf;
618 
619 		/* if we cannot determine VLAN just remove ourselves */
620 		vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(i));
621 		if (!vlvf)
622 			goto update_vlvfb;
623 
624 		vid = vlvf & VLAN_VID_MASK;
625 		mask = BIT(vid % 32);
626 
627 		/* clear bit from VFTA */
628 		vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid / 32));
629 		if (vfta & mask)
630 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid / 32), vfta ^ mask);
631 update_vlvf:
632 		/* clear POOL selection enable */
633 		IXGBE_WRITE_REG(hw, IXGBE_VLVF(i), 0);
634 
635 		if (!(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
636 			vlvfb = 0;
637 update_vlvfb:
638 		/* clear pool bits */
639 		IXGBE_WRITE_REG(hw, IXGBE_VLVFB(word), vlvfb);
640 	}
641 }
642 
ixgbe_set_vf_macvlan(struct ixgbe_adapter * adapter,int vf,int index,unsigned char * mac_addr)643 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
644 				int vf, int index, unsigned char *mac_addr)
645 {
646 	struct vf_macvlans *entry;
647 	bool found = false;
648 	int retval = 0;
649 
650 	if (index <= 1) {
651 		list_for_each_entry(entry, &adapter->vf_mvs.l, l) {
652 			if (entry->vf == vf) {
653 				entry->vf = -1;
654 				entry->free = true;
655 				entry->is_macvlan = false;
656 				ixgbe_del_mac_filter(adapter,
657 						     entry->vf_macvlan, vf);
658 			}
659 		}
660 	}
661 
662 	/*
663 	 * If index was zero then we were asked to clear the uc list
664 	 * for the VF.  We're done.
665 	 */
666 	if (!index)
667 		return 0;
668 
669 	list_for_each_entry(entry, &adapter->vf_mvs.l, l) {
670 		if (entry->free) {
671 			found = true;
672 			break;
673 		}
674 	}
675 
676 	/*
677 	 * If we traversed the entire list and didn't find a free entry
678 	 * then we're out of space on the RAR table.  It's also possible
679 	 * for the &adapter->vf_mvs.l list to be empty because the original
680 	 * memory allocation for the list failed, which is not fatal but does
681 	 * mean we can't support VF requests for MACVLAN because we couldn't
682 	 * allocate memory for the list management required.
683 	 */
684 	if (!found)
685 		return -ENOSPC;
686 
687 	retval = ixgbe_add_mac_filter(adapter, mac_addr, vf);
688 	if (retval < 0)
689 		return retval;
690 
691 	entry->free = false;
692 	entry->is_macvlan = true;
693 	entry->vf = vf;
694 	memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
695 
696 	return 0;
697 }
698 
ixgbe_vf_reset_event(struct ixgbe_adapter * adapter,u32 vf)699 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
700 {
701 	struct ixgbe_hw *hw = &adapter->hw;
702 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
703 	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
704 	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
705 	u8 num_tcs = adapter->hw_tcs;
706 	u32 reg_val;
707 	u32 queue;
708 
709 	/* remove VLAN filters belonging to this VF */
710 	ixgbe_clear_vf_vlans(adapter, vf);
711 
712 	/* add back PF assigned VLAN or VLAN 0 */
713 	ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf);
714 
715 	/* reset offloads to defaults */
716 	ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan);
717 
718 	/* set outgoing tags for VFs */
719 	if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
720 		ixgbe_clear_vmvir(adapter, vf);
721 	} else {
722 		if (vfinfo->pf_qos || !num_tcs)
723 			ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
724 					vfinfo->pf_qos, vf);
725 		else
726 			ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
727 					adapter->default_up, vf);
728 
729 		if (vfinfo->spoofchk_enabled) {
730 			hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
731 			hw->mac.ops.set_mac_anti_spoofing(hw, true, vf);
732 		}
733 	}
734 
735 	/* reset multicast table array for vf */
736 	adapter->vfinfo[vf].num_vf_mc_hashes = 0;
737 
738 	/* clear any ipsec table info */
739 	ixgbe_ipsec_vf_clear(adapter, vf);
740 
741 	/* Flush and reset the mta with the new values */
742 	ixgbe_set_rx_mode(adapter->netdev);
743 
744 	ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
745 	ixgbe_set_vf_macvlan(adapter, vf, 0, NULL);
746 
747 	/* reset VF api back to unknown */
748 	adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
749 
750 	/* Restart each queue for given VF */
751 	for (queue = 0; queue < q_per_pool; queue++) {
752 		unsigned int reg_idx = (vf * q_per_pool) + queue;
753 
754 		reg_val = IXGBE_READ_REG(hw, IXGBE_PVFTXDCTL(reg_idx));
755 
756 		/* Re-enabling only configured queues */
757 		if (reg_val) {
758 			reg_val |= IXGBE_TXDCTL_ENABLE;
759 			IXGBE_WRITE_REG(hw, IXGBE_PVFTXDCTL(reg_idx), reg_val);
760 			reg_val &= ~IXGBE_TXDCTL_ENABLE;
761 			IXGBE_WRITE_REG(hw, IXGBE_PVFTXDCTL(reg_idx), reg_val);
762 		}
763 	}
764 
765 	IXGBE_WRITE_FLUSH(hw);
766 }
767 
ixgbe_vf_clear_mbx(struct ixgbe_adapter * adapter,u32 vf)768 static void ixgbe_vf_clear_mbx(struct ixgbe_adapter *adapter, u32 vf)
769 {
770 	struct ixgbe_hw *hw = &adapter->hw;
771 	u32 word;
772 
773 	/* Clear VF's mailbox memory */
774 	for (word = 0; word < IXGBE_VFMAILBOX_SIZE; word++)
775 		IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf), word, 0);
776 
777 	IXGBE_WRITE_FLUSH(hw);
778 }
779 
ixgbe_set_vf_mac(struct ixgbe_adapter * adapter,int vf,unsigned char * mac_addr)780 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
781 			    int vf, unsigned char *mac_addr)
782 {
783 	int retval;
784 
785 	ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
786 	retval = ixgbe_add_mac_filter(adapter, mac_addr, vf);
787 	if (retval >= 0)
788 		memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr,
789 		       ETH_ALEN);
790 	else
791 		eth_zero_addr(adapter->vfinfo[vf].vf_mac_addresses);
792 
793 	return retval;
794 }
795 
ixgbe_vf_configuration(struct pci_dev * pdev,unsigned int event_mask)796 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
797 {
798 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
799 	unsigned int vfn = (event_mask & 0x3f);
800 
801 	bool enable = ((event_mask & 0x10000000U) != 0);
802 
803 	if (enable)
804 		eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses);
805 
806 	return 0;
807 }
808 
ixgbe_write_qde(struct ixgbe_adapter * adapter,u32 vf,u32 qde)809 static inline void ixgbe_write_qde(struct ixgbe_adapter *adapter, u32 vf,
810 				   u32 qde)
811 {
812 	struct ixgbe_hw *hw = &adapter->hw;
813 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
814 	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
815 	int i;
816 
817 	for (i = vf * q_per_pool; i < ((vf + 1) * q_per_pool); i++) {
818 		u32 reg;
819 
820 		/* flush previous write */
821 		IXGBE_WRITE_FLUSH(hw);
822 
823 		/* indicate to hardware that we want to set drop enable */
824 		reg = IXGBE_QDE_WRITE | qde;
825 		reg |= i <<  IXGBE_QDE_IDX_SHIFT;
826 		IXGBE_WRITE_REG(hw, IXGBE_QDE, reg);
827 	}
828 }
829 
830 /**
831  * ixgbe_set_vf_rx_tx - Set VF rx tx
832  * @adapter: Pointer to adapter struct
833  * @vf: VF identifier
834  *
835  * Set or reset correct transmit and receive for vf
836  **/
ixgbe_set_vf_rx_tx(struct ixgbe_adapter * adapter,int vf)837 static void ixgbe_set_vf_rx_tx(struct ixgbe_adapter *adapter, int vf)
838 {
839 	u32 reg_cur_tx, reg_cur_rx, reg_req_tx, reg_req_rx;
840 	struct ixgbe_hw *hw = &adapter->hw;
841 	u32 reg_offset, vf_shift;
842 
843 	vf_shift = vf % 32;
844 	reg_offset = vf / 32;
845 
846 	reg_cur_tx = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
847 	reg_cur_rx = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
848 
849 	if (adapter->vfinfo[vf].link_enable) {
850 		reg_req_tx = reg_cur_tx | 1 << vf_shift;
851 		reg_req_rx = reg_cur_rx | 1 << vf_shift;
852 	} else {
853 		reg_req_tx = reg_cur_tx & ~(1 << vf_shift);
854 		reg_req_rx = reg_cur_rx & ~(1 << vf_shift);
855 	}
856 
857 	/* The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
858 	 * For more info take a look at ixgbe_set_vf_lpe
859 	 */
860 	if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
861 		struct net_device *dev = adapter->netdev;
862 		int pf_max_frame = dev->mtu + ETH_HLEN;
863 
864 #if IS_ENABLED(CONFIG_FCOE)
865 		if (dev->fcoe_mtu)
866 			pf_max_frame = max_t(int, pf_max_frame,
867 					     IXGBE_FCOE_JUMBO_FRAME_SIZE);
868 #endif /* CONFIG_FCOE */
869 
870 		if (pf_max_frame > ETH_FRAME_LEN)
871 			reg_req_rx = reg_cur_rx & ~(1 << vf_shift);
872 	}
873 
874 	/* Enable/Disable particular VF */
875 	if (reg_cur_tx != reg_req_tx)
876 		IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg_req_tx);
877 	if (reg_cur_rx != reg_req_rx)
878 		IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg_req_rx);
879 }
880 
ixgbe_vf_reset_msg(struct ixgbe_adapter * adapter,u32 vf)881 static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
882 {
883 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
884 	struct ixgbe_hw *hw = &adapter->hw;
885 	unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
886 	u32 reg, reg_offset, vf_shift;
887 	u32 msgbuf[4] = {0, 0, 0, 0};
888 	u8 *addr = (u8 *)(&msgbuf[1]);
889 	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
890 	int i;
891 
892 	e_info(probe, "VF Reset msg received from vf %d\n", vf);
893 
894 	/* reset the filters for the device */
895 	ixgbe_vf_reset_event(adapter, vf);
896 
897 	ixgbe_vf_clear_mbx(adapter, vf);
898 
899 	/* set vf mac address */
900 	if (!is_zero_ether_addr(vf_mac))
901 		ixgbe_set_vf_mac(adapter, vf, vf_mac);
902 
903 	vf_shift = vf % 32;
904 	reg_offset = vf / 32;
905 
906 	/* force drop enable for all VF Rx queues */
907 	reg = IXGBE_QDE_ENABLE;
908 	if (adapter->vfinfo[vf].pf_vlan)
909 		reg |= IXGBE_QDE_HIDE_VLAN;
910 
911 	ixgbe_write_qde(adapter, vf, reg);
912 
913 	ixgbe_set_vf_rx_tx(adapter, vf);
914 
915 	/* enable VF mailbox for further messages */
916 	adapter->vfinfo[vf].clear_to_send = true;
917 
918 	/* Enable counting of spoofed packets in the SSVPC register */
919 	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
920 	reg |= BIT(vf_shift);
921 	IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
922 
923 	/*
924 	 * Reset the VFs TDWBAL and TDWBAH registers
925 	 * which are not cleared by an FLR
926 	 */
927 	for (i = 0; i < q_per_pool; i++) {
928 		IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBAHn(q_per_pool, vf, i), 0);
929 		IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBALn(q_per_pool, vf, i), 0);
930 	}
931 
932 	/* reply to reset with ack and vf mac address */
933 	msgbuf[0] = IXGBE_VF_RESET;
934 	if (!is_zero_ether_addr(vf_mac) && adapter->vfinfo[vf].pf_set_mac) {
935 		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
936 		memcpy(addr, vf_mac, ETH_ALEN);
937 	} else {
938 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
939 	}
940 
941 	/*
942 	 * Piggyback the multicast filter type so VF can compute the
943 	 * correct vectors
944 	 */
945 	msgbuf[3] = hw->mac.mc_filter_type;
946 	ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
947 
948 	return 0;
949 }
950 
ixgbe_set_vf_mac_addr(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)951 static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
952 				 u32 *msgbuf, u32 vf)
953 {
954 	u8 *new_mac = ((u8 *)(&msgbuf[1]));
955 
956 	if (!is_valid_ether_addr(new_mac)) {
957 		e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
958 		return -1;
959 	}
960 
961 	if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted &&
962 	    !ether_addr_equal(adapter->vfinfo[vf].vf_mac_addresses, new_mac)) {
963 		e_warn(drv,
964 		       "VF %d attempted to override administratively set MAC address\n"
965 		       "Reload the VF driver to resume operations\n",
966 		       vf);
967 		return -1;
968 	}
969 
970 	return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0;
971 }
972 
ixgbe_set_vf_vlan_msg(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)973 static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
974 				 u32 *msgbuf, u32 vf)
975 {
976 	u32 add = FIELD_GET(IXGBE_VT_MSGINFO_MASK, msgbuf[0]);
977 	u32 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
978 	u8 tcs = adapter->hw_tcs;
979 
980 	if (adapter->vfinfo[vf].pf_vlan || tcs) {
981 		e_warn(drv,
982 		       "VF %d attempted to override administratively set VLAN configuration\n"
983 		       "Reload the VF driver to resume operations\n",
984 		       vf);
985 		return -1;
986 	}
987 
988 	/* VLAN 0 is a special case, don't allow it to be removed */
989 	if (!vid && !add)
990 		return 0;
991 
992 	return ixgbe_set_vf_vlan(adapter, add, vid, vf);
993 }
994 
ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)995 static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
996 				    u32 *msgbuf, u32 vf)
997 {
998 	u8 *new_mac = ((u8 *)(&msgbuf[1]));
999 	int index = FIELD_GET(IXGBE_VT_MSGINFO_MASK, msgbuf[0]);
1000 	int err;
1001 
1002 	if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted &&
1003 	    index > 0) {
1004 		e_warn(drv,
1005 		       "VF %d requested MACVLAN filter but is administratively denied\n",
1006 		       vf);
1007 		return -1;
1008 	}
1009 
1010 	/* An non-zero index indicates the VF is setting a filter */
1011 	if (index) {
1012 		if (!is_valid_ether_addr(new_mac)) {
1013 			e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
1014 			return -1;
1015 		}
1016 
1017 		/*
1018 		 * If the VF is allowed to set MAC filters then turn off
1019 		 * anti-spoofing to avoid false positives.
1020 		 */
1021 		if (adapter->vfinfo[vf].spoofchk_enabled) {
1022 			struct ixgbe_hw *hw = &adapter->hw;
1023 
1024 			hw->mac.ops.set_mac_anti_spoofing(hw, false, vf);
1025 			hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
1026 		}
1027 	}
1028 
1029 	err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
1030 	if (err == -ENOSPC)
1031 		e_warn(drv,
1032 		       "VF %d has requested a MACVLAN filter but there is no space for it\n",
1033 		       vf);
1034 
1035 	return err < 0;
1036 }
1037 
ixgbe_negotiate_vf_api(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1038 static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
1039 				  u32 *msgbuf, u32 vf)
1040 {
1041 	int api = msgbuf[1];
1042 
1043 	switch (api) {
1044 	case ixgbe_mbox_api_10:
1045 	case ixgbe_mbox_api_11:
1046 	case ixgbe_mbox_api_12:
1047 	case ixgbe_mbox_api_13:
1048 	case ixgbe_mbox_api_14:
1049 	case ixgbe_mbox_api_16:
1050 	case ixgbe_mbox_api_17:
1051 		adapter->vfinfo[vf].vf_api = api;
1052 		return 0;
1053 	default:
1054 		break;
1055 	}
1056 
1057 	e_dbg(drv, "VF %d requested unsupported api version %u\n", vf, api);
1058 
1059 	return -1;
1060 }
1061 
ixgbe_get_vf_queues(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1062 static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter,
1063 			       u32 *msgbuf, u32 vf)
1064 {
1065 	struct net_device *dev = adapter->netdev;
1066 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
1067 	unsigned int default_tc = 0;
1068 	u8 num_tcs = adapter->hw_tcs;
1069 
1070 	/* verify the PF is supporting the correct APIs */
1071 	switch (adapter->vfinfo[vf].vf_api) {
1072 	case ixgbe_mbox_api_20:
1073 	case ixgbe_mbox_api_11:
1074 	case ixgbe_mbox_api_12:
1075 	case ixgbe_mbox_api_13:
1076 	case ixgbe_mbox_api_14:
1077 	case ixgbe_mbox_api_16:
1078 	case ixgbe_mbox_api_17:
1079 		break;
1080 	default:
1081 		return -1;
1082 	}
1083 
1084 	/* only allow 1 Tx queue for bandwidth limiting */
1085 	msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
1086 	msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
1087 
1088 	/* if TCs > 1 determine which TC belongs to default user priority */
1089 	if (num_tcs > 1)
1090 		default_tc = netdev_get_prio_tc_map(dev, adapter->default_up);
1091 
1092 	/* notify VF of need for VLAN tag stripping, and correct queue */
1093 	if (num_tcs)
1094 		msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
1095 	else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos)
1096 		msgbuf[IXGBE_VF_TRANS_VLAN] = 1;
1097 	else
1098 		msgbuf[IXGBE_VF_TRANS_VLAN] = 0;
1099 
1100 	/* notify VF of default queue */
1101 	msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc;
1102 
1103 	return 0;
1104 }
1105 
ixgbe_get_vf_reta(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1106 static int ixgbe_get_vf_reta(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
1107 {
1108 	u32 i, j;
1109 	u32 *out_buf = &msgbuf[1];
1110 	const u8 *reta = adapter->rss_indir_tbl;
1111 	u32 reta_size = ixgbe_rss_indir_tbl_entries(adapter);
1112 
1113 	/* Check if operation is permitted */
1114 	if (!adapter->vfinfo[vf].rss_query_enabled)
1115 		return -EPERM;
1116 
1117 	/* verify the PF is supporting the correct API */
1118 	switch (adapter->vfinfo[vf].vf_api) {
1119 	case ixgbe_mbox_api_17:
1120 	case ixgbe_mbox_api_16:
1121 	case ixgbe_mbox_api_14:
1122 	case ixgbe_mbox_api_13:
1123 	case ixgbe_mbox_api_12:
1124 		break;
1125 	default:
1126 		return -EOPNOTSUPP;
1127 	}
1128 
1129 	/* This mailbox command is supported (required) only for 82599 and x540
1130 	 * VFs which support up to 4 RSS queues. Therefore we will compress the
1131 	 * RETA by saving only 2 bits from each entry. This way we will be able
1132 	 * to transfer the whole RETA in a single mailbox operation.
1133 	 */
1134 	for (i = 0; i < reta_size / 16; i++) {
1135 		out_buf[i] = 0;
1136 		for (j = 0; j < 16; j++)
1137 			out_buf[i] |= (u32)(reta[16 * i + j] & 0x3) << (2 * j);
1138 	}
1139 
1140 	return 0;
1141 }
1142 
ixgbe_get_vf_rss_key(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1143 static int ixgbe_get_vf_rss_key(struct ixgbe_adapter *adapter,
1144 				u32 *msgbuf, u32 vf)
1145 {
1146 	u32 *rss_key = &msgbuf[1];
1147 
1148 	/* Check if the operation is permitted */
1149 	if (!adapter->vfinfo[vf].rss_query_enabled)
1150 		return -EPERM;
1151 
1152 	/* verify the PF is supporting the correct API */
1153 	switch (adapter->vfinfo[vf].vf_api) {
1154 	case ixgbe_mbox_api_17:
1155 	case ixgbe_mbox_api_16:
1156 	case ixgbe_mbox_api_14:
1157 	case ixgbe_mbox_api_13:
1158 	case ixgbe_mbox_api_12:
1159 		break;
1160 	default:
1161 		return -EOPNOTSUPP;
1162 	}
1163 
1164 	memcpy(rss_key, adapter->rss_key, IXGBE_RSS_KEY_SIZE);
1165 
1166 	return 0;
1167 }
1168 
ixgbe_update_vf_xcast_mode(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1169 static int ixgbe_update_vf_xcast_mode(struct ixgbe_adapter *adapter,
1170 				      u32 *msgbuf, u32 vf)
1171 {
1172 	struct ixgbe_hw *hw = &adapter->hw;
1173 	int xcast_mode = msgbuf[1];
1174 	u32 vmolr, fctrl, disable, enable;
1175 
1176 	/* verify the PF is supporting the correct APIs */
1177 	switch (adapter->vfinfo[vf].vf_api) {
1178 	case ixgbe_mbox_api_12:
1179 		/* promisc introduced in 1.3 version */
1180 		if (xcast_mode == IXGBEVF_XCAST_MODE_PROMISC)
1181 			return -EOPNOTSUPP;
1182 		fallthrough;
1183 	case ixgbe_mbox_api_13:
1184 	case ixgbe_mbox_api_14:
1185 	case ixgbe_mbox_api_16:
1186 	case ixgbe_mbox_api_17:
1187 		break;
1188 	default:
1189 		return -EOPNOTSUPP;
1190 	}
1191 
1192 	if (xcast_mode > IXGBEVF_XCAST_MODE_MULTI &&
1193 	    !adapter->vfinfo[vf].trusted) {
1194 		xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
1195 	}
1196 
1197 	if (adapter->vfinfo[vf].xcast_mode == xcast_mode)
1198 		goto out;
1199 
1200 	switch (xcast_mode) {
1201 	case IXGBEVF_XCAST_MODE_NONE:
1202 		disable = IXGBE_VMOLR_ROMPE |
1203 			  IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
1204 		enable = IXGBE_VMOLR_BAM;
1205 		break;
1206 	case IXGBEVF_XCAST_MODE_MULTI:
1207 		disable = IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
1208 		enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE;
1209 		break;
1210 	case IXGBEVF_XCAST_MODE_ALLMULTI:
1211 		disable = IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
1212 		enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
1213 		break;
1214 	case IXGBEVF_XCAST_MODE_PROMISC:
1215 		if (hw->mac.type <= ixgbe_mac_82599EB)
1216 			return -EOPNOTSUPP;
1217 
1218 		fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1219 		if (!(fctrl & IXGBE_FCTRL_UPE)) {
1220 			/* VF promisc requires PF in promisc */
1221 			e_warn(drv,
1222 			       "Enabling VF promisc requires PF in promisc\n");
1223 			return -EPERM;
1224 		}
1225 
1226 		disable = IXGBE_VMOLR_VPE;
1227 		enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE |
1228 			 IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE;
1229 		break;
1230 	default:
1231 		return -EOPNOTSUPP;
1232 	}
1233 
1234 	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
1235 	vmolr &= ~disable;
1236 	vmolr |= enable;
1237 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
1238 
1239 	adapter->vfinfo[vf].xcast_mode = xcast_mode;
1240 
1241 out:
1242 	msgbuf[1] = xcast_mode;
1243 
1244 	return 0;
1245 }
1246 
ixgbe_get_vf_link_state(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1247 static int ixgbe_get_vf_link_state(struct ixgbe_adapter *adapter,
1248 				   u32 *msgbuf, u32 vf)
1249 {
1250 	u32 *link_state = &msgbuf[1];
1251 
1252 	/* verify the PF is supporting the correct API */
1253 	switch (adapter->vfinfo[vf].vf_api) {
1254 	case ixgbe_mbox_api_12:
1255 	case ixgbe_mbox_api_13:
1256 	case ixgbe_mbox_api_14:
1257 	case ixgbe_mbox_api_16:
1258 	case ixgbe_mbox_api_17:
1259 		break;
1260 	default:
1261 		return -EOPNOTSUPP;
1262 	}
1263 
1264 	*link_state = adapter->vfinfo[vf].link_enable;
1265 
1266 	return 0;
1267 }
1268 
1269 /**
1270  * ixgbe_send_vf_link_status - send link status data to VF
1271  * @adapter: pointer to adapter struct
1272  * @msgbuf: pointer to message buffers
1273  * @vf: VF identifier
1274  *
1275  * Reply for IXGBE_VF_GET_PF_LINK_STATE mbox command sending link status data.
1276  *
1277  * Return: 0 on success or -EOPNOTSUPP when operation is not supported.
1278  */
ixgbe_send_vf_link_status(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1279 static int ixgbe_send_vf_link_status(struct ixgbe_adapter *adapter,
1280 				     u32 *msgbuf, u32 vf)
1281 {
1282 	struct ixgbe_hw *hw = &adapter->hw;
1283 
1284 	switch (adapter->vfinfo[vf].vf_api) {
1285 	case ixgbe_mbox_api_16:
1286 	case ixgbe_mbox_api_17:
1287 		if (hw->mac.type != ixgbe_mac_e610)
1288 			return -EOPNOTSUPP;
1289 		break;
1290 	default:
1291 		return -EOPNOTSUPP;
1292 	}
1293 	/* Simply provide stored values as watchdog & link status events take
1294 	 * care of its freshness.
1295 	 */
1296 	msgbuf[1] = adapter->link_speed;
1297 	msgbuf[2] = adapter->link_up;
1298 
1299 	return 0;
1300 }
1301 
1302 /**
1303  * ixgbe_negotiate_vf_features -  negotiate supported features with VF driver
1304  * @adapter: pointer to adapter struct
1305  * @msgbuf: pointer to message buffers
1306  * @vf: VF identifier
1307  *
1308  * Return: 0 on success or -EOPNOTSUPP when operation is not supported.
1309  */
ixgbe_negotiate_vf_features(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1310 static int ixgbe_negotiate_vf_features(struct ixgbe_adapter *adapter,
1311 				       u32 *msgbuf, u32 vf)
1312 {
1313 	u32 features = msgbuf[1];
1314 
1315 	switch (adapter->vfinfo[vf].vf_api) {
1316 	case ixgbe_mbox_api_17:
1317 		break;
1318 	default:
1319 		return -EOPNOTSUPP;
1320 	}
1321 
1322 	features &= IXGBE_SUPPORTED_FEATURES;
1323 	msgbuf[1] = features;
1324 
1325 	return 0;
1326 }
1327 
ixgbe_rcv_msg_from_vf(struct ixgbe_adapter * adapter,u32 vf)1328 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
1329 {
1330 	u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
1331 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
1332 	struct ixgbe_hw *hw = &adapter->hw;
1333 	int retval;
1334 
1335 	retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
1336 
1337 	if (retval) {
1338 		pr_err("Error receiving message from VF\n");
1339 		return retval;
1340 	}
1341 
1342 	/* this is a message we already processed, do nothing */
1343 	if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
1344 		return 0;
1345 
1346 	/* flush the ack before we write any messages back */
1347 	IXGBE_WRITE_FLUSH(hw);
1348 
1349 	if (msgbuf[0] == IXGBE_VF_RESET)
1350 		return ixgbe_vf_reset_msg(adapter, vf);
1351 
1352 	/*
1353 	 * until the vf completes a virtual function reset it should not be
1354 	 * allowed to start any configuration.
1355 	 */
1356 	if (!adapter->vfinfo[vf].clear_to_send) {
1357 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
1358 		ixgbe_write_mbx(hw, msgbuf, 1, vf);
1359 		return 0;
1360 	}
1361 
1362 	switch ((msgbuf[0] & 0xFFFF)) {
1363 	case IXGBE_VF_SET_MAC_ADDR:
1364 		retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
1365 		break;
1366 	case IXGBE_VF_SET_MULTICAST:
1367 		retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
1368 		break;
1369 	case IXGBE_VF_SET_VLAN:
1370 		retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
1371 		break;
1372 	case IXGBE_VF_SET_LPE:
1373 		retval = ixgbe_set_vf_lpe(adapter, msgbuf[1], vf);
1374 		break;
1375 	case IXGBE_VF_SET_MACVLAN:
1376 		retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
1377 		break;
1378 	case IXGBE_VF_API_NEGOTIATE:
1379 		retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
1380 		break;
1381 	case IXGBE_VF_GET_QUEUES:
1382 		retval = ixgbe_get_vf_queues(adapter, msgbuf, vf);
1383 		break;
1384 	case IXGBE_VF_GET_RETA:
1385 		retval = ixgbe_get_vf_reta(adapter, msgbuf, vf);
1386 		break;
1387 	case IXGBE_VF_GET_RSS_KEY:
1388 		retval = ixgbe_get_vf_rss_key(adapter, msgbuf, vf);
1389 		break;
1390 	case IXGBE_VF_UPDATE_XCAST_MODE:
1391 		retval = ixgbe_update_vf_xcast_mode(adapter, msgbuf, vf);
1392 		break;
1393 	case IXGBE_VF_GET_LINK_STATE:
1394 		retval = ixgbe_get_vf_link_state(adapter, msgbuf, vf);
1395 		break;
1396 	case IXGBE_VF_IPSEC_ADD:
1397 		retval = ixgbe_ipsec_vf_add_sa(adapter, msgbuf, vf);
1398 		break;
1399 	case IXGBE_VF_IPSEC_DEL:
1400 		retval = ixgbe_ipsec_vf_del_sa(adapter, msgbuf, vf);
1401 		break;
1402 	case IXGBE_VF_GET_PF_LINK_STATE:
1403 		retval = ixgbe_send_vf_link_status(adapter, msgbuf, vf);
1404 		break;
1405 	case IXGBE_VF_FEATURES_NEGOTIATE:
1406 		retval = ixgbe_negotiate_vf_features(adapter, msgbuf, vf);
1407 		break;
1408 	default:
1409 		e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
1410 		retval = -EIO;
1411 		break;
1412 	}
1413 
1414 	/* notify the VF of the results of what it sent us */
1415 	if (retval)
1416 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
1417 	else
1418 		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
1419 
1420 	msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
1421 
1422 	ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
1423 
1424 	return retval;
1425 }
1426 
ixgbe_rcv_ack_from_vf(struct ixgbe_adapter * adapter,u32 vf)1427 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
1428 {
1429 	struct ixgbe_hw *hw = &adapter->hw;
1430 	u32 msg = IXGBE_VT_MSGTYPE_NACK;
1431 
1432 	/* if device isn't clear to send it shouldn't be reading either */
1433 	if (!adapter->vfinfo[vf].clear_to_send)
1434 		ixgbe_write_mbx(hw, &msg, 1, vf);
1435 }
1436 
1437 /**
1438  * ixgbe_check_mdd_event - check for MDD event on all VFs
1439  * @adapter: pointer to ixgbe adapter
1440  *
1441  * Return: true if there is a VF on which MDD event occurred, false otherwise.
1442  */
ixgbe_check_mdd_event(struct ixgbe_adapter * adapter)1443 bool ixgbe_check_mdd_event(struct ixgbe_adapter *adapter)
1444 {
1445 	struct ixgbe_hw *hw = &adapter->hw;
1446 	DECLARE_BITMAP(vf_bitmap, 64);
1447 	bool ret = false;
1448 	int i;
1449 
1450 	if (!hw->mac.ops.handle_mdd)
1451 		return false;
1452 
1453 	/* Did we have a malicious event */
1454 	bitmap_zero(vf_bitmap, 64);
1455 	hw->mac.ops.handle_mdd(hw, vf_bitmap);
1456 
1457 	/* Log any blocked queues and release lock */
1458 	for_each_set_bit(i, vf_bitmap, 64) {
1459 		dev_warn(&adapter->pdev->dev,
1460 			 "Malicious event on VF %d tx:%x rx:%x\n", i,
1461 			 IXGBE_READ_REG(hw, IXGBE_LVMMC_TX),
1462 			 IXGBE_READ_REG(hw, IXGBE_LVMMC_RX));
1463 
1464 		if (hw->mac.ops.restore_mdd_vf) {
1465 			u32 ping;
1466 
1467 			hw->mac.ops.restore_mdd_vf(hw, i);
1468 
1469 			/* get the VF to rebuild its queues */
1470 			adapter->vfinfo[i].clear_to_send = 0;
1471 			ping = IXGBE_PF_CONTROL_MSG |
1472 			       IXGBE_VT_MSGTYPE_CTS;
1473 			ixgbe_write_mbx(hw, &ping, 1, i);
1474 		}
1475 
1476 		ret = true;
1477 	}
1478 
1479 	return ret;
1480 }
1481 
ixgbe_msg_task(struct ixgbe_adapter * adapter)1482 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
1483 {
1484 	struct ixgbe_hw *hw = &adapter->hw;
1485 	unsigned long flags;
1486 	u32 vf;
1487 
1488 	ixgbe_check_mdd_event(adapter);
1489 
1490 	spin_lock_irqsave(&adapter->vfs_lock, flags);
1491 	for (vf = 0; vf < adapter->num_vfs; vf++) {
1492 		/* process any reset requests */
1493 		if (!ixgbe_check_for_rst(hw, vf))
1494 			ixgbe_vf_reset_event(adapter, vf);
1495 
1496 		/* process any messages pending */
1497 		if (!ixgbe_check_for_msg(hw, vf))
1498 			ixgbe_rcv_msg_from_vf(adapter, vf);
1499 
1500 		/* process any acks */
1501 		if (!ixgbe_check_for_ack(hw, vf))
1502 			ixgbe_rcv_ack_from_vf(adapter, vf);
1503 	}
1504 	spin_unlock_irqrestore(&adapter->vfs_lock, flags);
1505 }
1506 
ixgbe_ping_vf(struct ixgbe_adapter * adapter,int vf)1507 static inline void ixgbe_ping_vf(struct ixgbe_adapter *adapter, int vf)
1508 {
1509 	struct ixgbe_hw *hw = &adapter->hw;
1510 	u32 ping;
1511 
1512 	ping = IXGBE_PF_CONTROL_MSG;
1513 	if (adapter->vfinfo[vf].clear_to_send)
1514 		ping |= IXGBE_VT_MSGTYPE_CTS;
1515 	ixgbe_write_mbx(hw, &ping, 1, vf);
1516 }
1517 
ixgbe_ping_all_vfs(struct ixgbe_adapter * adapter)1518 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
1519 {
1520 	struct ixgbe_hw *hw = &adapter->hw;
1521 	u32 ping;
1522 	int i;
1523 
1524 	for (i = 0 ; i < adapter->num_vfs; i++) {
1525 		ping = IXGBE_PF_CONTROL_MSG;
1526 		if (adapter->vfinfo[i].clear_to_send)
1527 			ping |= IXGBE_VT_MSGTYPE_CTS;
1528 		ixgbe_write_mbx(hw, &ping, 1, i);
1529 	}
1530 }
1531 
1532 /**
1533  * ixgbe_set_all_vfs - update vfs queues
1534  * @adapter: Pointer to adapter struct
1535  *
1536  * Update setting transmit and receive queues for all vfs
1537  **/
ixgbe_set_all_vfs(struct ixgbe_adapter * adapter)1538 void ixgbe_set_all_vfs(struct ixgbe_adapter *adapter)
1539 {
1540 	int i;
1541 
1542 	for (i = 0 ; i < adapter->num_vfs; i++)
1543 		ixgbe_set_vf_link_state(adapter, i,
1544 					adapter->vfinfo[i].link_state);
1545 }
1546 
ixgbe_ndo_set_vf_mac(struct net_device * netdev,int vf,u8 * mac)1547 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1548 {
1549 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1550 	int retval;
1551 
1552 	if (vf >= adapter->num_vfs)
1553 		return -EINVAL;
1554 
1555 	if (is_valid_ether_addr(mac)) {
1556 		dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n",
1557 			 mac, vf);
1558 		dev_info(&adapter->pdev->dev, "Reload the VF driver to make this change effective.");
1559 
1560 		retval = ixgbe_set_vf_mac(adapter, vf, mac);
1561 		if (retval >= 0) {
1562 			adapter->vfinfo[vf].pf_set_mac = true;
1563 
1564 			if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1565 				dev_warn(&adapter->pdev->dev, "The VF MAC address has been set, but the PF device is not up.\n");
1566 				dev_warn(&adapter->pdev->dev, "Bring the PF device up before attempting to use the VF device.\n");
1567 			}
1568 		} else {
1569 			dev_warn(&adapter->pdev->dev, "The VF MAC address was NOT set due to invalid or duplicate MAC address.\n");
1570 		}
1571 	} else if (is_zero_ether_addr(mac)) {
1572 		unsigned char *vf_mac_addr =
1573 					   adapter->vfinfo[vf].vf_mac_addresses;
1574 
1575 		/* nothing to do */
1576 		if (is_zero_ether_addr(vf_mac_addr))
1577 			return 0;
1578 
1579 		dev_info(&adapter->pdev->dev, "removing MAC on VF %d\n", vf);
1580 
1581 		retval = ixgbe_del_mac_filter(adapter, vf_mac_addr, vf);
1582 		if (retval >= 0) {
1583 			adapter->vfinfo[vf].pf_set_mac = false;
1584 			memcpy(vf_mac_addr, mac, ETH_ALEN);
1585 		} else {
1586 			dev_warn(&adapter->pdev->dev, "Could NOT remove the VF MAC address.\n");
1587 		}
1588 	} else {
1589 		retval = -EINVAL;
1590 	}
1591 
1592 	return retval;
1593 }
1594 
ixgbe_enable_port_vlan(struct ixgbe_adapter * adapter,int vf,u16 vlan,u8 qos)1595 static int ixgbe_enable_port_vlan(struct ixgbe_adapter *adapter, int vf,
1596 				  u16 vlan, u8 qos)
1597 {
1598 	struct ixgbe_hw *hw = &adapter->hw;
1599 	int err;
1600 
1601 	err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
1602 	if (err)
1603 		goto out;
1604 
1605 	/* Revoke tagless access via VLAN 0 */
1606 	ixgbe_set_vf_vlan(adapter, false, 0, vf);
1607 
1608 	ixgbe_set_vmvir(adapter, vlan, qos, vf);
1609 	ixgbe_set_vmolr(hw, vf, false);
1610 
1611 	/* enable hide vlan on X550 */
1612 	if (hw->mac.type >= ixgbe_mac_X550)
1613 		ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE |
1614 				IXGBE_QDE_HIDE_VLAN);
1615 
1616 	adapter->vfinfo[vf].pf_vlan = vlan;
1617 	adapter->vfinfo[vf].pf_qos = qos;
1618 	dev_info(&adapter->pdev->dev,
1619 		 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
1620 	if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1621 		dev_warn(&adapter->pdev->dev,
1622 			 "The VF VLAN has been set, but the PF device is not up.\n");
1623 		dev_warn(&adapter->pdev->dev,
1624 			 "Bring the PF device up before attempting to use the VF device.\n");
1625 	}
1626 
1627 out:
1628 	return err;
1629 }
1630 
ixgbe_disable_port_vlan(struct ixgbe_adapter * adapter,int vf)1631 static int ixgbe_disable_port_vlan(struct ixgbe_adapter *adapter, int vf)
1632 {
1633 	struct ixgbe_hw *hw = &adapter->hw;
1634 	int err;
1635 
1636 	err = ixgbe_set_vf_vlan(adapter, false,
1637 				adapter->vfinfo[vf].pf_vlan, vf);
1638 	/* Restore tagless access via VLAN 0 */
1639 	ixgbe_set_vf_vlan(adapter, true, 0, vf);
1640 	ixgbe_clear_vmvir(adapter, vf);
1641 	ixgbe_set_vmolr(hw, vf, true);
1642 
1643 	/* disable hide VLAN on X550 */
1644 	if (hw->mac.type >= ixgbe_mac_X550)
1645 		ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE);
1646 
1647 	adapter->vfinfo[vf].pf_vlan = 0;
1648 	adapter->vfinfo[vf].pf_qos = 0;
1649 
1650 	return err;
1651 }
1652 
ixgbe_ndo_set_vf_vlan(struct net_device * netdev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)1653 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
1654 			  u8 qos, __be16 vlan_proto)
1655 {
1656 	int err = 0;
1657 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1658 
1659 	if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
1660 		return -EINVAL;
1661 	if (vlan_proto != htons(ETH_P_8021Q))
1662 		return -EPROTONOSUPPORT;
1663 	if (vlan || qos) {
1664 		/* Check if there is already a port VLAN set, if so
1665 		 * we have to delete the old one first before we
1666 		 * can set the new one.  The usage model had
1667 		 * previously assumed the user would delete the
1668 		 * old port VLAN before setting a new one but this
1669 		 * is not necessarily the case.
1670 		 */
1671 		if (adapter->vfinfo[vf].pf_vlan)
1672 			err = ixgbe_disable_port_vlan(adapter, vf);
1673 		if (err)
1674 			goto out;
1675 		err = ixgbe_enable_port_vlan(adapter, vf, vlan, qos);
1676 	} else {
1677 		err = ixgbe_disable_port_vlan(adapter, vf);
1678 	}
1679 
1680 out:
1681 	return err;
1682 }
1683 
ixgbe_link_mbps(struct ixgbe_adapter * adapter)1684 int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
1685 {
1686 	switch (adapter->link_speed) {
1687 	case IXGBE_LINK_SPEED_100_FULL:
1688 		return 100;
1689 	case IXGBE_LINK_SPEED_1GB_FULL:
1690 		return 1000;
1691 	case IXGBE_LINK_SPEED_10GB_FULL:
1692 		return 10000;
1693 	default:
1694 		return 0;
1695 	}
1696 }
1697 
ixgbe_set_vf_rate_limit(struct ixgbe_adapter * adapter,int vf)1698 static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
1699 {
1700 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
1701 	struct ixgbe_hw *hw = &adapter->hw;
1702 	u32 bcnrc_val = 0;
1703 	u16 queue, queues_per_pool;
1704 	u16 tx_rate = adapter->vfinfo[vf].tx_rate;
1705 
1706 	if (tx_rate) {
1707 		/* start with base link speed value */
1708 		bcnrc_val = adapter->vf_rate_link_speed;
1709 
1710 		/* Calculate the rate factor values to set */
1711 		bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
1712 		bcnrc_val /= tx_rate;
1713 
1714 		/* clear everything but the rate factor */
1715 		bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
1716 			     IXGBE_RTTBCNRC_RF_DEC_MASK;
1717 
1718 		/* enable the rate scheduler */
1719 		bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
1720 	}
1721 
1722 	/*
1723 	 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
1724 	 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
1725 	 * and 0x004 otherwise.
1726 	 */
1727 	switch (hw->mac.type) {
1728 	case ixgbe_mac_82599EB:
1729 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
1730 		break;
1731 	case ixgbe_mac_X540:
1732 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
1733 		break;
1734 	default:
1735 		break;
1736 	}
1737 
1738 	/* determine how many queues per pool based on VMDq mask */
1739 	queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
1740 
1741 	/* write value for all Tx queues belonging to VF */
1742 	for (queue = 0; queue < queues_per_pool; queue++) {
1743 		unsigned int reg_idx = (vf * queues_per_pool) + queue;
1744 
1745 		IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
1746 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
1747 	}
1748 }
1749 
ixgbe_check_vf_rate_limit(struct ixgbe_adapter * adapter)1750 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
1751 {
1752 	int i;
1753 
1754 	/* VF Tx rate limit was not set */
1755 	if (!adapter->vf_rate_link_speed)
1756 		return;
1757 
1758 	if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
1759 		adapter->vf_rate_link_speed = 0;
1760 		dev_info(&adapter->pdev->dev,
1761 			 "Link speed has been changed. VF Transmit rate is disabled\n");
1762 	}
1763 
1764 	for (i = 0; i < adapter->num_vfs; i++) {
1765 		if (!adapter->vf_rate_link_speed)
1766 			adapter->vfinfo[i].tx_rate = 0;
1767 
1768 		ixgbe_set_vf_rate_limit(adapter, i);
1769 	}
1770 }
1771 
ixgbe_ndo_set_vf_bw(struct net_device * netdev,int vf,int min_tx_rate,int max_tx_rate)1772 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int min_tx_rate,
1773 			int max_tx_rate)
1774 {
1775 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1776 	int link_speed;
1777 
1778 	/* verify VF is active */
1779 	if (vf >= adapter->num_vfs)
1780 		return -EINVAL;
1781 
1782 	/* verify link is up */
1783 	if (!adapter->link_up)
1784 		return -EINVAL;
1785 
1786 	/* verify we are linked at 10Gbps */
1787 	link_speed = ixgbe_link_mbps(adapter);
1788 	if (link_speed != 10000)
1789 		return -EINVAL;
1790 
1791 	if (min_tx_rate)
1792 		return -EINVAL;
1793 
1794 	/* rate limit cannot be less than 10Mbs or greater than link speed */
1795 	if (max_tx_rate && ((max_tx_rate <= 10) || (max_tx_rate > link_speed)))
1796 		return -EINVAL;
1797 
1798 	/* store values */
1799 	adapter->vf_rate_link_speed = link_speed;
1800 	adapter->vfinfo[vf].tx_rate = max_tx_rate;
1801 
1802 	/* update hardware configuration */
1803 	ixgbe_set_vf_rate_limit(adapter, vf);
1804 
1805 	return 0;
1806 }
1807 
ixgbe_ndo_set_vf_spoofchk(struct net_device * netdev,int vf,bool setting)1808 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
1809 {
1810 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1811 	struct ixgbe_hw *hw = &adapter->hw;
1812 
1813 	if (vf >= adapter->num_vfs)
1814 		return -EINVAL;
1815 
1816 	adapter->vfinfo[vf].spoofchk_enabled = setting;
1817 
1818 	/* configure MAC spoofing */
1819 	hw->mac.ops.set_mac_anti_spoofing(hw, setting, vf);
1820 
1821 	/* configure VLAN spoofing */
1822 	hw->mac.ops.set_vlan_anti_spoofing(hw, setting, vf);
1823 
1824 	/* Ensure LLDP and FC is set for Ethertype Antispoofing if we will be
1825 	 * calling set_ethertype_anti_spoofing for each VF in loop below
1826 	 */
1827 	if (hw->mac.ops.set_ethertype_anti_spoofing) {
1828 		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_LLDP),
1829 				(IXGBE_ETQF_FILTER_EN    |
1830 				 IXGBE_ETQF_TX_ANTISPOOF |
1831 				 ETH_P_LLDP));
1832 
1833 		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FC),
1834 				(IXGBE_ETQF_FILTER_EN |
1835 				 IXGBE_ETQF_TX_ANTISPOOF |
1836 				 ETH_P_PAUSE));
1837 
1838 		hw->mac.ops.set_ethertype_anti_spoofing(hw, setting, vf);
1839 	}
1840 
1841 	return 0;
1842 }
1843 
1844 /**
1845  * ixgbe_set_vf_link_state - Set link state
1846  * @adapter: Pointer to adapter struct
1847  * @vf: VF identifier
1848  * @state: required link state
1849  *
1850  * Set a link force state on/off a single vf
1851  **/
ixgbe_set_vf_link_state(struct ixgbe_adapter * adapter,int vf,int state)1852 void ixgbe_set_vf_link_state(struct ixgbe_adapter *adapter, int vf, int state)
1853 {
1854 	adapter->vfinfo[vf].link_state = state;
1855 
1856 	switch (state) {
1857 	case IFLA_VF_LINK_STATE_AUTO:
1858 		if (test_bit(__IXGBE_DOWN, &adapter->state))
1859 			adapter->vfinfo[vf].link_enable = false;
1860 		else
1861 			adapter->vfinfo[vf].link_enable = true;
1862 		break;
1863 	case IFLA_VF_LINK_STATE_ENABLE:
1864 		adapter->vfinfo[vf].link_enable = true;
1865 		break;
1866 	case IFLA_VF_LINK_STATE_DISABLE:
1867 		adapter->vfinfo[vf].link_enable = false;
1868 		break;
1869 	}
1870 
1871 	ixgbe_set_vf_rx_tx(adapter, vf);
1872 
1873 	/* restart the VF */
1874 	adapter->vfinfo[vf].clear_to_send = false;
1875 	ixgbe_ping_vf(adapter, vf);
1876 }
1877 
1878 /**
1879  * ixgbe_ndo_set_vf_link_state - Set link state
1880  * @netdev: network interface device structure
1881  * @vf: VF identifier
1882  * @state: required link state
1883  *
1884  * Set the link state of a specified VF, regardless of physical link state
1885  **/
ixgbe_ndo_set_vf_link_state(struct net_device * netdev,int vf,int state)1886 int ixgbe_ndo_set_vf_link_state(struct net_device *netdev, int vf, int state)
1887 {
1888 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1889 	int ret = 0;
1890 
1891 	if (vf < 0 || vf >= adapter->num_vfs) {
1892 		dev_err(&adapter->pdev->dev,
1893 			"NDO set VF link - invalid VF identifier %d\n", vf);
1894 		return -EINVAL;
1895 	}
1896 
1897 	switch (state) {
1898 	case IFLA_VF_LINK_STATE_ENABLE:
1899 		dev_info(&adapter->pdev->dev,
1900 			 "NDO set VF %d link state %d - not supported\n",
1901 			vf, state);
1902 		break;
1903 	case IFLA_VF_LINK_STATE_DISABLE:
1904 		dev_info(&adapter->pdev->dev,
1905 			 "NDO set VF %d link state disable\n", vf);
1906 		ixgbe_set_vf_link_state(adapter, vf, state);
1907 		break;
1908 	case IFLA_VF_LINK_STATE_AUTO:
1909 		dev_info(&adapter->pdev->dev,
1910 			 "NDO set VF %d link state auto\n", vf);
1911 		ixgbe_set_vf_link_state(adapter, vf, state);
1912 		break;
1913 	default:
1914 		dev_err(&adapter->pdev->dev,
1915 			"NDO set VF %d - invalid link state %d\n", vf, state);
1916 		ret = -EINVAL;
1917 	}
1918 
1919 	return ret;
1920 }
1921 
ixgbe_ndo_set_vf_rss_query_en(struct net_device * netdev,int vf,bool setting)1922 int ixgbe_ndo_set_vf_rss_query_en(struct net_device *netdev, int vf,
1923 				  bool setting)
1924 {
1925 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1926 
1927 	/* This operation is currently supported only for 82599 and x540
1928 	 * devices.
1929 	 */
1930 	if (adapter->hw.mac.type < ixgbe_mac_82599EB ||
1931 	    adapter->hw.mac.type >= ixgbe_mac_X550)
1932 		return -EOPNOTSUPP;
1933 
1934 	if (vf >= adapter->num_vfs)
1935 		return -EINVAL;
1936 
1937 	adapter->vfinfo[vf].rss_query_enabled = setting;
1938 
1939 	return 0;
1940 }
1941 
ixgbe_ndo_set_vf_trust(struct net_device * netdev,int vf,bool setting)1942 int ixgbe_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
1943 {
1944 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1945 
1946 	if (vf >= adapter->num_vfs)
1947 		return -EINVAL;
1948 
1949 	/* nothing to do */
1950 	if (adapter->vfinfo[vf].trusted == setting)
1951 		return 0;
1952 
1953 	adapter->vfinfo[vf].trusted = setting;
1954 
1955 	/* reset VF to reconfigure features */
1956 	adapter->vfinfo[vf].clear_to_send = false;
1957 	ixgbe_ping_vf(adapter, vf);
1958 
1959 	e_info(drv, "VF %u is %strusted\n", vf, setting ? "" : "not ");
1960 
1961 	return 0;
1962 }
1963 
ixgbe_ndo_get_vf_config(struct net_device * netdev,int vf,struct ifla_vf_info * ivi)1964 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
1965 			    int vf, struct ifla_vf_info *ivi)
1966 {
1967 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(netdev);
1968 	if (vf >= adapter->num_vfs)
1969 		return -EINVAL;
1970 	ivi->vf = vf;
1971 	memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
1972 	ivi->max_tx_rate = adapter->vfinfo[vf].tx_rate;
1973 	ivi->min_tx_rate = 0;
1974 	ivi->vlan = adapter->vfinfo[vf].pf_vlan;
1975 	ivi->qos = adapter->vfinfo[vf].pf_qos;
1976 	ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
1977 	ivi->rss_query_en = adapter->vfinfo[vf].rss_query_enabled;
1978 	ivi->trusted = adapter->vfinfo[vf].trusted;
1979 	ivi->linkstate = adapter->vfinfo[vf].link_state;
1980 	return 0;
1981 }
1982