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