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