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