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