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