xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_sriov.c (revision 72503791edffe516848d0f01d377fa9cd0711970)
1 /*******************************************************************************
2 
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2012 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   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 
26 *******************************************************************************/
27 
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/netdevice.h>
32 #include <linux/vmalloc.h>
33 #include <linux/string.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/tcp.h>
37 #include <linux/ipv6.h>
38 #ifdef NETIF_F_HW_VLAN_TX
39 #include <linux/if_vlan.h>
40 #endif
41 
42 #include "ixgbe.h"
43 #include "ixgbe_type.h"
44 #include "ixgbe_sriov.h"
45 
46 #ifdef CONFIG_PCI_IOV
47 void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
48 			 const struct ixgbe_info *ii)
49 {
50 	struct ixgbe_hw *hw = &adapter->hw;
51 	int num_vf_macvlans, i;
52 	struct vf_macvlans *mv_list;
53 	int pre_existing_vfs = 0;
54 
55 	pre_existing_vfs = pci_num_vf(adapter->pdev);
56 	if (!pre_existing_vfs && !adapter->num_vfs)
57 		return;
58 
59 	/* If there are pre-existing VFs then we have to force
60 	 * use of that many because they were not deleted the last
61 	 * time someone removed the PF driver.  That would have
62 	 * been because they were allocated to guest VMs and can't
63 	 * be removed.  Go ahead and just re-enable the old amount.
64 	 * If the user wants to change the number of VFs they can
65 	 * use ethtool while making sure no VFs are allocated to
66 	 * guest VMs... i.e. the right way.
67 	 */
68 	if (pre_existing_vfs) {
69 		adapter->num_vfs = pre_existing_vfs;
70 		dev_warn(&adapter->pdev->dev, "Virtual Functions already "
71 			 "enabled for this device - Please reload all "
72 			 "VF drivers to avoid spoofed packet errors\n");
73 	} else {
74 		int err;
75 		/*
76 		 * The 82599 supports up to 64 VFs per physical function
77 		 * but this implementation limits allocation to 63 so that
78 		 * basic networking resources are still available to the
79 		 * physical function.  If the user requests greater thn
80 		 * 63 VFs then it is an error - reset to default of zero.
81 		 */
82 		adapter->num_vfs = min_t(unsigned int, adapter->num_vfs, 63);
83 
84 		err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
85 		if (err) {
86 			e_err(probe, "Failed to enable PCI sriov: %d\n", err);
87 			adapter->num_vfs = 0;
88 			return;
89 		}
90 	}
91 
92 	adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
93 	e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
94 
95 	/* Enable VMDq flag so device will be set in VM mode */
96 	adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED;
97 	if (!adapter->ring_feature[RING_F_VMDQ].limit)
98 		adapter->ring_feature[RING_F_VMDQ].limit = 1;
99 	adapter->ring_feature[RING_F_VMDQ].offset = adapter->num_vfs;
100 
101 	num_vf_macvlans = hw->mac.num_rar_entries -
102 	(IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
103 
104 	adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
105 					     sizeof(struct vf_macvlans),
106 					     GFP_KERNEL);
107 	if (mv_list) {
108 		/* Initialize list of VF macvlans */
109 		INIT_LIST_HEAD(&adapter->vf_mvs.l);
110 		for (i = 0; i < num_vf_macvlans; i++) {
111 			mv_list->vf = -1;
112 			mv_list->free = true;
113 			mv_list->rar_entry = hw->mac.num_rar_entries -
114 				(i + adapter->num_vfs + 1);
115 			list_add(&mv_list->l, &adapter->vf_mvs.l);
116 			mv_list++;
117 		}
118 	}
119 
120 	/* If call to enable VFs succeeded then allocate memory
121 	 * for per VF control structures.
122 	 */
123 	adapter->vfinfo =
124 		kcalloc(adapter->num_vfs,
125 			sizeof(struct vf_data_storage), GFP_KERNEL);
126 	if (adapter->vfinfo) {
127 		/* Now that we're sure SR-IOV is enabled
128 		 * and memory allocated set up the mailbox parameters
129 		 */
130 		ixgbe_init_mbx_params_pf(hw);
131 		memcpy(&hw->mbx.ops, ii->mbx_ops, sizeof(hw->mbx.ops));
132 
133 		/* limit trafffic classes based on VFs enabled */
134 		if ((adapter->hw.mac.type == ixgbe_mac_82599EB) &&
135 		    (adapter->num_vfs < 16)) {
136 			adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
137 			adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
138 		} else if (adapter->num_vfs < 32) {
139 			adapter->dcb_cfg.num_tcs.pg_tcs = 4;
140 			adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
141 		} else {
142 			adapter->dcb_cfg.num_tcs.pg_tcs = 1;
143 			adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
144 		}
145 
146 		/* We do not support RSS w/ SR-IOV */
147 		adapter->ring_feature[RING_F_RSS].limit = 1;
148 
149 		/* Disable RSC when in SR-IOV mode */
150 		adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
151 				     IXGBE_FLAG2_RSC_ENABLED);
152 
153 #ifdef IXGBE_FCOE
154 		/*
155 		 * When SR-IOV is enabled 82599 cannot support jumbo frames
156 		 * so we must disable FCoE because we cannot support FCoE MTU.
157 		 */
158 		if (adapter->hw.mac.type == ixgbe_mac_82599EB)
159 			adapter->flags &= ~(IXGBE_FLAG_FCOE_ENABLED |
160 					    IXGBE_FLAG_FCOE_CAPABLE);
161 #endif
162 
163 		/* enable spoof checking for all VFs */
164 		for (i = 0; i < adapter->num_vfs; i++)
165 			adapter->vfinfo[i].spoofchk_enabled = true;
166 		return;
167 	}
168 
169 	/* Oh oh */
170 	e_err(probe, "Unable to allocate memory for VF Data Storage - "
171 	      "SRIOV disabled\n");
172 	ixgbe_disable_sriov(adapter);
173 }
174 
175 static bool ixgbe_vfs_are_assigned(struct ixgbe_adapter *adapter)
176 {
177 	struct pci_dev *pdev = adapter->pdev;
178 	struct pci_dev *vfdev;
179 	int dev_id;
180 
181 	switch (adapter->hw.mac.type) {
182 	case ixgbe_mac_82599EB:
183 		dev_id = IXGBE_DEV_ID_82599_VF;
184 		break;
185 	case ixgbe_mac_X540:
186 		dev_id = IXGBE_DEV_ID_X540_VF;
187 		break;
188 	default:
189 		return false;
190 	}
191 
192 	/* loop through all the VFs to see if we own any that are assigned */
193 	vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
194 	while (vfdev) {
195 		/* if we don't own it we don't care */
196 		if (vfdev->is_virtfn && vfdev->physfn == pdev) {
197 			/* if it is assigned we cannot release it */
198 			if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
199 				return true;
200 		}
201 
202 		vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
203 	}
204 
205 	return false;
206 }
207 
208 #endif /* #ifdef CONFIG_PCI_IOV */
209 void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
210 {
211 	struct ixgbe_hw *hw = &adapter->hw;
212 	u32 gpie;
213 	u32 vmdctl;
214 
215 	/* set num VFs to 0 to prevent access to vfinfo */
216 	adapter->num_vfs = 0;
217 
218 	/* free VF control structures */
219 	kfree(adapter->vfinfo);
220 	adapter->vfinfo = NULL;
221 
222 	/* free macvlan list */
223 	kfree(adapter->mv_list);
224 	adapter->mv_list = NULL;
225 
226 	/* if SR-IOV is already disabled then there is nothing to do */
227 	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
228 		return;
229 
230 #ifdef CONFIG_PCI_IOV
231 	/*
232 	 * If our VFs are assigned we cannot shut down SR-IOV
233 	 * without causing issues, so just leave the hardware
234 	 * available but disabled
235 	 */
236 	if (ixgbe_vfs_are_assigned(adapter)) {
237 		e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
238 		return;
239 	}
240 	/* disable iov and allow time for transactions to clear */
241 	pci_disable_sriov(adapter->pdev);
242 #endif
243 
244 	/* turn off device IOV mode */
245 	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
246 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
247 	gpie &= ~IXGBE_GPIE_VTMODE_MASK;
248 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
249 
250 	/* set default pool back to 0 */
251 	vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
252 	vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
253 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
254 	IXGBE_WRITE_FLUSH(hw);
255 
256 	/* Disable VMDq flag so device will be set in VM mode */
257 	if (adapter->ring_feature[RING_F_VMDQ].limit == 1)
258 		adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
259 	adapter->ring_feature[RING_F_VMDQ].offset = 0;
260 
261 	/* take a breather then clean up driver data */
262 	msleep(100);
263 
264 	adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
265 }
266 
267 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
268 				   int entries, u16 *hash_list, u32 vf)
269 {
270 	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
271 	struct ixgbe_hw *hw = &adapter->hw;
272 	int i;
273 	u32 vector_bit;
274 	u32 vector_reg;
275 	u32 mta_reg;
276 
277 	/* only so many hash values supported */
278 	entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
279 
280 	/*
281 	 * salt away the number of multi cast addresses assigned
282 	 * to this VF for later use to restore when the PF multi cast
283 	 * list changes
284 	 */
285 	vfinfo->num_vf_mc_hashes = entries;
286 
287 	/*
288 	 * VFs are limited to using the MTA hash table for their multicast
289 	 * addresses
290 	 */
291 	for (i = 0; i < entries; i++) {
292 		vfinfo->vf_mc_hashes[i] = hash_list[i];
293 	}
294 
295 	for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
296 		vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
297 		vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
298 		mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
299 		mta_reg |= (1 << vector_bit);
300 		IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
301 	}
302 
303 	return 0;
304 }
305 
306 static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
307 {
308 	struct ixgbe_hw *hw = &adapter->hw;
309 	struct list_head *pos;
310 	struct vf_macvlans *entry;
311 
312 	list_for_each(pos, &adapter->vf_mvs.l) {
313 		entry = list_entry(pos, struct vf_macvlans, l);
314 		if (!entry->free)
315 			hw->mac.ops.set_rar(hw, entry->rar_entry,
316 					    entry->vf_macvlan,
317 					    entry->vf, IXGBE_RAH_AV);
318 	}
319 }
320 
321 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
322 {
323 	struct ixgbe_hw *hw = &adapter->hw;
324 	struct vf_data_storage *vfinfo;
325 	int i, j;
326 	u32 vector_bit;
327 	u32 vector_reg;
328 	u32 mta_reg;
329 
330 	for (i = 0; i < adapter->num_vfs; i++) {
331 		vfinfo = &adapter->vfinfo[i];
332 		for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
333 			hw->addr_ctrl.mta_in_use++;
334 			vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
335 			vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
336 			mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
337 			mta_reg |= (1 << vector_bit);
338 			IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
339 		}
340 	}
341 
342 	/* Restore any VF macvlans */
343 	ixgbe_restore_vf_macvlans(adapter);
344 }
345 
346 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
347 			     u32 vf)
348 {
349 	/* VLAN 0 is a special case, don't allow it to be removed */
350 	if (!vid && !add)
351 		return 0;
352 
353 	return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
354 }
355 
356 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
357 {
358 	struct ixgbe_hw *hw = &adapter->hw;
359 	int new_mtu = msgbuf[1];
360 	u32 max_frs;
361 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
362 
363 	/* Only X540 supports jumbo frames in IOV mode */
364 	if (adapter->hw.mac.type != ixgbe_mac_X540)
365 		return;
366 
367 	/* MTU < 68 is an error and causes problems on some kernels */
368 	if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
369 		e_err(drv, "VF mtu %d out of range\n", new_mtu);
370 		return;
371 	}
372 
373 	max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
374 		   IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
375 	if (max_frs < new_mtu) {
376 		max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
377 		IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
378 	}
379 
380 	e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
381 }
382 
383 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
384 {
385 	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
386 	vmolr |= (IXGBE_VMOLR_ROMPE |
387 		  IXGBE_VMOLR_BAM);
388 	if (aupe)
389 		vmolr |= IXGBE_VMOLR_AUPE;
390 	else
391 		vmolr &= ~IXGBE_VMOLR_AUPE;
392 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
393 }
394 
395 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
396 {
397 	struct ixgbe_hw *hw = &adapter->hw;
398 
399 	if (vid)
400 		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
401 				(vid | IXGBE_VMVIR_VLANA_DEFAULT));
402 	else
403 		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
404 }
405 
406 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
407 {
408 	struct ixgbe_hw *hw = &adapter->hw;
409 	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
410 
411 	/* reset offloads to defaults */
412 	if (adapter->vfinfo[vf].pf_vlan) {
413 		ixgbe_set_vf_vlan(adapter, true,
414 				  adapter->vfinfo[vf].pf_vlan, vf);
415 		ixgbe_set_vmvir(adapter,
416 				(adapter->vfinfo[vf].pf_vlan |
417 				 (adapter->vfinfo[vf].pf_qos <<
418 				  VLAN_PRIO_SHIFT)), vf);
419 		ixgbe_set_vmolr(hw, vf, false);
420 	} else {
421 		ixgbe_set_vf_vlan(adapter, true, 0, vf);
422 		ixgbe_set_vmvir(adapter, 0, vf);
423 		ixgbe_set_vmolr(hw, vf, true);
424 	}
425 
426 	/* reset multicast table array for vf */
427 	adapter->vfinfo[vf].num_vf_mc_hashes = 0;
428 
429 	/* Flush and reset the mta with the new values */
430 	ixgbe_set_rx_mode(adapter->netdev);
431 
432 	hw->mac.ops.clear_rar(hw, rar_entry);
433 }
434 
435 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
436 			    int vf, unsigned char *mac_addr)
437 {
438 	struct ixgbe_hw *hw = &adapter->hw;
439 	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
440 
441 	memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
442 	hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
443 
444 	return 0;
445 }
446 
447 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
448 				int vf, int index, unsigned char *mac_addr)
449 {
450 	struct ixgbe_hw *hw = &adapter->hw;
451 	struct list_head *pos;
452 	struct vf_macvlans *entry;
453 
454 	if (index <= 1) {
455 		list_for_each(pos, &adapter->vf_mvs.l) {
456 			entry = list_entry(pos, struct vf_macvlans, l);
457 			if (entry->vf == vf) {
458 				entry->vf = -1;
459 				entry->free = true;
460 				entry->is_macvlan = false;
461 				hw->mac.ops.clear_rar(hw, entry->rar_entry);
462 			}
463 		}
464 	}
465 
466 	/*
467 	 * If index was zero then we were asked to clear the uc list
468 	 * for the VF.  We're done.
469 	 */
470 	if (!index)
471 		return 0;
472 
473 	entry = NULL;
474 
475 	list_for_each(pos, &adapter->vf_mvs.l) {
476 		entry = list_entry(pos, struct vf_macvlans, l);
477 		if (entry->free)
478 			break;
479 	}
480 
481 	/*
482 	 * If we traversed the entire list and didn't find a free entry
483 	 * then we're out of space on the RAR table.  Also entry may
484 	 * be NULL because the original memory allocation for the list
485 	 * failed, which is not fatal but does mean we can't support
486 	 * VF requests for MACVLAN because we couldn't allocate
487 	 * memory for the list management required.
488 	 */
489 	if (!entry || !entry->free)
490 		return -ENOSPC;
491 
492 	entry->free = false;
493 	entry->is_macvlan = true;
494 	entry->vf = vf;
495 	memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
496 
497 	hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
498 
499 	return 0;
500 }
501 
502 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
503 {
504 	unsigned char vf_mac_addr[6];
505 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
506 	unsigned int vfn = (event_mask & 0x3f);
507 
508 	bool enable = ((event_mask & 0x10000000U) != 0);
509 
510 	if (enable) {
511 		eth_random_addr(vf_mac_addr);
512 		e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
513 		       vfn, vf_mac_addr);
514 		/*
515 		 * Store away the VF "permananet" MAC address, it will ask
516 		 * for it later.
517 		 */
518 		memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
519 	}
520 
521 	return 0;
522 }
523 
524 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
525 {
526 	struct ixgbe_hw *hw = &adapter->hw;
527 	u32 reg;
528 	u32 reg_offset, vf_shift;
529 
530 	vf_shift = vf % 32;
531 	reg_offset = vf / 32;
532 
533 	/* enable transmit and receive for vf */
534 	reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
535 	reg |= (reg | (1 << vf_shift));
536 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
537 
538 	reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
539 	reg |= (reg | (1 << vf_shift));
540 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
541 
542 	/* Enable counting of spoofed packets in the SSVPC register */
543 	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
544 	reg |= (1 << vf_shift);
545 	IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
546 
547 	ixgbe_vf_reset_event(adapter, vf);
548 }
549 
550 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
551 {
552 	u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
553 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
554 	struct ixgbe_hw *hw = &adapter->hw;
555 	s32 retval;
556 	int entries;
557 	u16 *hash_list;
558 	int add, vid, index;
559 	u8 *new_mac;
560 
561 	retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
562 
563 	if (retval) {
564 		pr_err("Error receiving message from VF\n");
565 		return retval;
566 	}
567 
568 	/* this is a message we already processed, do nothing */
569 	if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
570 		return retval;
571 
572 	/* flush the ack before we write any messages back */
573 	IXGBE_WRITE_FLUSH(hw);
574 
575 	/*
576 	 * until the vf completes a virtual function reset it should not be
577 	 * allowed to start any configuration.
578 	 */
579 
580 	if (msgbuf[0] == IXGBE_VF_RESET) {
581 		unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
582 		new_mac = (u8 *)(&msgbuf[1]);
583 		e_info(probe, "VF Reset msg received from vf %d\n", vf);
584 		adapter->vfinfo[vf].clear_to_send = false;
585 		ixgbe_vf_reset_msg(adapter, vf);
586 		adapter->vfinfo[vf].clear_to_send = true;
587 
588 		if (is_valid_ether_addr(new_mac) &&
589 		    !adapter->vfinfo[vf].pf_set_mac)
590 			ixgbe_set_vf_mac(adapter, vf, vf_mac);
591 		else
592 			ixgbe_set_vf_mac(adapter,
593 				 vf, adapter->vfinfo[vf].vf_mac_addresses);
594 
595 		/* reply to reset with ack and vf mac address */
596 		msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
597 		memcpy(new_mac, vf_mac, ETH_ALEN);
598 		/*
599 		 * Piggyback the multicast filter type so VF can compute the
600 		 * correct vectors
601 		 */
602 		msgbuf[3] = hw->mac.mc_filter_type;
603 		ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
604 
605 		return retval;
606 	}
607 
608 	if (!adapter->vfinfo[vf].clear_to_send) {
609 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
610 		ixgbe_write_mbx(hw, msgbuf, 1, vf);
611 		return retval;
612 	}
613 
614 	switch ((msgbuf[0] & 0xFFFF)) {
615 	case IXGBE_VF_SET_MAC_ADDR:
616 		new_mac = ((u8 *)(&msgbuf[1]));
617 		if (is_valid_ether_addr(new_mac) &&
618 		    !adapter->vfinfo[vf].pf_set_mac) {
619 			ixgbe_set_vf_mac(adapter, vf, new_mac);
620 		} else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
621 				  new_mac, ETH_ALEN)) {
622 			e_warn(drv, "VF %d attempted to override "
623 			       "administratively set MAC address\nReload "
624 			       "the VF driver to resume operations\n", vf);
625 			retval = -1;
626 		}
627 		break;
628 	case IXGBE_VF_SET_MULTICAST:
629 		entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
630 		          >> IXGBE_VT_MSGINFO_SHIFT;
631 		hash_list = (u16 *)&msgbuf[1];
632 		retval = ixgbe_set_vf_multicasts(adapter, entries,
633 		                                 hash_list, vf);
634 		break;
635 	case IXGBE_VF_SET_LPE:
636 		ixgbe_set_vf_lpe(adapter, msgbuf);
637 		break;
638 	case IXGBE_VF_SET_VLAN:
639 		add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
640 		      >> IXGBE_VT_MSGINFO_SHIFT;
641 		vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
642 		if (adapter->vfinfo[vf].pf_vlan) {
643 			e_warn(drv, "VF %d attempted to override "
644 			       "administratively set VLAN configuration\n"
645 			       "Reload the VF driver to resume operations\n",
646 			       vf);
647 			retval = -1;
648 		} else {
649 			if (add)
650 				adapter->vfinfo[vf].vlan_count++;
651 			else if (adapter->vfinfo[vf].vlan_count)
652 				adapter->vfinfo[vf].vlan_count--;
653 			retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
654 			if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
655 				hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
656 		}
657 		break;
658 	case IXGBE_VF_SET_MACVLAN:
659 		index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
660 			IXGBE_VT_MSGINFO_SHIFT;
661 		if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
662 			e_warn(drv, "VF %d requested MACVLAN filter but is "
663 				    "administratively denied\n", vf);
664 			retval = -1;
665 			break;
666 		}
667 		/*
668 		 * If the VF is allowed to set MAC filters then turn off
669 		 * anti-spoofing to avoid false positives.  An index
670 		 * greater than 0 will indicate the VF is setting a
671 		 * macvlan MAC filter.
672 		 */
673 		if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
674 			ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
675 		retval = ixgbe_set_vf_macvlan(adapter, vf, index,
676 					      (unsigned char *)(&msgbuf[1]));
677 		if (retval == -ENOSPC)
678 			e_warn(drv, "VF %d has requested a MACVLAN filter "
679 				    "but there is no space for it\n", vf);
680 		break;
681 	default:
682 		e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
683 		retval = IXGBE_ERR_MBX;
684 		break;
685 	}
686 
687 	/* notify the VF of the results of what it sent us */
688 	if (retval)
689 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
690 	else
691 		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
692 
693 	msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
694 
695 	ixgbe_write_mbx(hw, msgbuf, 1, vf);
696 
697 	return retval;
698 }
699 
700 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
701 {
702 	struct ixgbe_hw *hw = &adapter->hw;
703 	u32 msg = IXGBE_VT_MSGTYPE_NACK;
704 
705 	/* if device isn't clear to send it shouldn't be reading either */
706 	if (!adapter->vfinfo[vf].clear_to_send)
707 		ixgbe_write_mbx(hw, &msg, 1, vf);
708 }
709 
710 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
711 {
712 	struct ixgbe_hw *hw = &adapter->hw;
713 	u32 vf;
714 
715 	for (vf = 0; vf < adapter->num_vfs; vf++) {
716 		/* process any reset requests */
717 		if (!ixgbe_check_for_rst(hw, vf))
718 			ixgbe_vf_reset_event(adapter, vf);
719 
720 		/* process any messages pending */
721 		if (!ixgbe_check_for_msg(hw, vf))
722 			ixgbe_rcv_msg_from_vf(adapter, vf);
723 
724 		/* process any acks */
725 		if (!ixgbe_check_for_ack(hw, vf))
726 			ixgbe_rcv_ack_from_vf(adapter, vf);
727 	}
728 }
729 
730 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
731 {
732 	struct ixgbe_hw *hw = &adapter->hw;
733 
734 	/* disable transmit and receive for all vfs */
735 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
736 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
737 
738 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
739 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
740 }
741 
742 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
743 {
744 	struct ixgbe_hw *hw = &adapter->hw;
745 	u32 ping;
746 	int i;
747 
748 	for (i = 0 ; i < adapter->num_vfs; i++) {
749 		ping = IXGBE_PF_CONTROL_MSG;
750 		if (adapter->vfinfo[i].clear_to_send)
751 			ping |= IXGBE_VT_MSGTYPE_CTS;
752 		ixgbe_write_mbx(hw, &ping, 1, i);
753 	}
754 }
755 
756 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
757 {
758 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
759 	if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
760 		return -EINVAL;
761 	adapter->vfinfo[vf].pf_set_mac = true;
762 	dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
763 	dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
764 				      " change effective.");
765 	if (test_bit(__IXGBE_DOWN, &adapter->state)) {
766 		dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
767 			 " but the PF device is not up.\n");
768 		dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
769 			 " attempting to use the VF device.\n");
770 	}
771 	return ixgbe_set_vf_mac(adapter, vf, mac);
772 }
773 
774 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
775 {
776 	int err = 0;
777 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
778 	struct ixgbe_hw *hw = &adapter->hw;
779 
780 	if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
781 		return -EINVAL;
782 	if (vlan || qos) {
783 		err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
784 		if (err)
785 			goto out;
786 		ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
787 		ixgbe_set_vmolr(hw, vf, false);
788 		if (adapter->vfinfo[vf].spoofchk_enabled)
789 			hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
790 		adapter->vfinfo[vf].vlan_count++;
791 		adapter->vfinfo[vf].pf_vlan = vlan;
792 		adapter->vfinfo[vf].pf_qos = qos;
793 		dev_info(&adapter->pdev->dev,
794 			 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
795 		if (test_bit(__IXGBE_DOWN, &adapter->state)) {
796 			dev_warn(&adapter->pdev->dev,
797 				 "The VF VLAN has been set,"
798 				 " but the PF device is not up.\n");
799 			dev_warn(&adapter->pdev->dev,
800 				 "Bring the PF device up before"
801 				 " attempting to use the VF device.\n");
802 		}
803 	} else {
804 		err = ixgbe_set_vf_vlan(adapter, false,
805 					adapter->vfinfo[vf].pf_vlan, vf);
806 		ixgbe_set_vmvir(adapter, vlan, vf);
807 		ixgbe_set_vmolr(hw, vf, true);
808 		hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
809 		if (adapter->vfinfo[vf].vlan_count)
810 			adapter->vfinfo[vf].vlan_count--;
811 		adapter->vfinfo[vf].pf_vlan = 0;
812 		adapter->vfinfo[vf].pf_qos = 0;
813        }
814 out:
815        return err;
816 }
817 
818 static int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
819 {
820 	switch (adapter->link_speed) {
821 	case IXGBE_LINK_SPEED_100_FULL:
822 		return 100;
823 	case IXGBE_LINK_SPEED_1GB_FULL:
824 		return 1000;
825 	case IXGBE_LINK_SPEED_10GB_FULL:
826 		return 10000;
827 	default:
828 		return 0;
829 	}
830 }
831 
832 static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
833 {
834 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
835 	struct ixgbe_hw *hw = &adapter->hw;
836 	u32 bcnrc_val = 0;
837 	u16 queue, queues_per_pool;
838 	u16 tx_rate = adapter->vfinfo[vf].tx_rate;
839 
840 	if (tx_rate) {
841 		/* start with base link speed value */
842 		bcnrc_val = adapter->vf_rate_link_speed;
843 
844 		/* Calculate the rate factor values to set */
845 		bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
846 		bcnrc_val /= tx_rate;
847 
848 		/* clear everything but the rate factor */
849 		bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
850 			     IXGBE_RTTBCNRC_RF_DEC_MASK;
851 
852 		/* enable the rate scheduler */
853 		bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
854 	}
855 
856 	/*
857 	 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
858 	 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
859 	 * and 0x004 otherwise.
860 	 */
861 	switch (hw->mac.type) {
862 	case ixgbe_mac_82599EB:
863 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
864 		break;
865 	case ixgbe_mac_X540:
866 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
867 		break;
868 	default:
869 		break;
870 	}
871 
872 	/* determine how many queues per pool based on VMDq mask */
873 	queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
874 
875 	/* write value for all Tx queues belonging to VF */
876 	for (queue = 0; queue < queues_per_pool; queue++) {
877 		unsigned int reg_idx = (vf * queues_per_pool) + queue;
878 
879 		IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
880 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
881 	}
882 }
883 
884 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
885 {
886 	int i;
887 
888 	/* VF Tx rate limit was not set */
889 	if (!adapter->vf_rate_link_speed)
890 		return;
891 
892 	if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
893 		adapter->vf_rate_link_speed = 0;
894 		dev_info(&adapter->pdev->dev,
895 			 "Link speed has been changed. VF Transmit rate is disabled\n");
896 	}
897 
898 	for (i = 0; i < adapter->num_vfs; i++) {
899 		if (!adapter->vf_rate_link_speed)
900 			adapter->vfinfo[i].tx_rate = 0;
901 
902 		ixgbe_set_vf_rate_limit(adapter, i);
903 	}
904 }
905 
906 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
907 {
908 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
909 	int link_speed;
910 
911 	/* verify VF is active */
912 	if (vf >= adapter->num_vfs)
913 		return -EINVAL;
914 
915 	/* verify link is up */
916 	if (!adapter->link_up)
917 		return -EINVAL;
918 
919 	/* verify we are linked at 10Gbps */
920 	link_speed = ixgbe_link_mbps(adapter);
921 	if (link_speed != 10000)
922 		return -EINVAL;
923 
924 	/* rate limit cannot be less than 10Mbs or greater than link speed */
925 	if (tx_rate && ((tx_rate <= 10) || (tx_rate > link_speed)))
926 		return -EINVAL;
927 
928 	/* store values */
929 	adapter->vf_rate_link_speed = link_speed;
930 	adapter->vfinfo[vf].tx_rate = tx_rate;
931 
932 	/* update hardware configuration */
933 	ixgbe_set_vf_rate_limit(adapter, vf);
934 
935 	return 0;
936 }
937 
938 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
939 {
940 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
941 	int vf_target_reg = vf >> 3;
942 	int vf_target_shift = vf % 8;
943 	struct ixgbe_hw *hw = &adapter->hw;
944 	u32 regval;
945 
946 	adapter->vfinfo[vf].spoofchk_enabled = setting;
947 
948 	regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
949 	regval &= ~(1 << vf_target_shift);
950 	regval |= (setting << vf_target_shift);
951 	IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
952 
953 	if (adapter->vfinfo[vf].vlan_count) {
954 		vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
955 		regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
956 		regval &= ~(1 << vf_target_shift);
957 		regval |= (setting << vf_target_shift);
958 		IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
959 	}
960 
961 	return 0;
962 }
963 
964 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
965 			    int vf, struct ifla_vf_info *ivi)
966 {
967 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
968 	if (vf >= adapter->num_vfs)
969 		return -EINVAL;
970 	ivi->vf = vf;
971 	memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
972 	ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
973 	ivi->vlan = adapter->vfinfo[vf].pf_vlan;
974 	ivi->qos = adapter->vfinfo[vf].pf_qos;
975 	ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
976 	return 0;
977 }
978