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 	return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
350 }
351 
352 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
353 {
354 	struct ixgbe_hw *hw = &adapter->hw;
355 	int new_mtu = msgbuf[1];
356 	u32 max_frs;
357 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
358 
359 	/* Only X540 supports jumbo frames in IOV mode */
360 	if (adapter->hw.mac.type != ixgbe_mac_X540)
361 		return;
362 
363 	/* MTU < 68 is an error and causes problems on some kernels */
364 	if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
365 		e_err(drv, "VF mtu %d out of range\n", new_mtu);
366 		return;
367 	}
368 
369 	max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
370 		   IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
371 	if (max_frs < new_mtu) {
372 		max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
373 		IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
374 	}
375 
376 	e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
377 }
378 
379 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
380 {
381 	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
382 	vmolr |= (IXGBE_VMOLR_ROMPE |
383 		  IXGBE_VMOLR_BAM);
384 	if (aupe)
385 		vmolr |= IXGBE_VMOLR_AUPE;
386 	else
387 		vmolr &= ~IXGBE_VMOLR_AUPE;
388 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
389 }
390 
391 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
392 {
393 	struct ixgbe_hw *hw = &adapter->hw;
394 
395 	if (vid)
396 		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
397 				(vid | IXGBE_VMVIR_VLANA_DEFAULT));
398 	else
399 		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
400 }
401 
402 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
403 {
404 	struct ixgbe_hw *hw = &adapter->hw;
405 	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
406 
407 	/* reset offloads to defaults */
408 	if (adapter->vfinfo[vf].pf_vlan) {
409 		ixgbe_set_vf_vlan(adapter, true,
410 				  adapter->vfinfo[vf].pf_vlan, vf);
411 		ixgbe_set_vmvir(adapter,
412 				(adapter->vfinfo[vf].pf_vlan |
413 				 (adapter->vfinfo[vf].pf_qos <<
414 				  VLAN_PRIO_SHIFT)), vf);
415 		ixgbe_set_vmolr(hw, vf, false);
416 	} else {
417 		ixgbe_set_vmvir(adapter, 0, vf);
418 		ixgbe_set_vmolr(hw, vf, true);
419 	}
420 
421 	/* reset multicast table array for vf */
422 	adapter->vfinfo[vf].num_vf_mc_hashes = 0;
423 
424 	/* Flush and reset the mta with the new values */
425 	ixgbe_set_rx_mode(adapter->netdev);
426 
427 	hw->mac.ops.clear_rar(hw, rar_entry);
428 }
429 
430 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
431 			    int vf, unsigned char *mac_addr)
432 {
433 	struct ixgbe_hw *hw = &adapter->hw;
434 	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
435 
436 	memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
437 	hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
438 
439 	return 0;
440 }
441 
442 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
443 				int vf, int index, unsigned char *mac_addr)
444 {
445 	struct ixgbe_hw *hw = &adapter->hw;
446 	struct list_head *pos;
447 	struct vf_macvlans *entry;
448 
449 	if (index <= 1) {
450 		list_for_each(pos, &adapter->vf_mvs.l) {
451 			entry = list_entry(pos, struct vf_macvlans, l);
452 			if (entry->vf == vf) {
453 				entry->vf = -1;
454 				entry->free = true;
455 				entry->is_macvlan = false;
456 				hw->mac.ops.clear_rar(hw, entry->rar_entry);
457 			}
458 		}
459 	}
460 
461 	/*
462 	 * If index was zero then we were asked to clear the uc list
463 	 * for the VF.  We're done.
464 	 */
465 	if (!index)
466 		return 0;
467 
468 	entry = NULL;
469 
470 	list_for_each(pos, &adapter->vf_mvs.l) {
471 		entry = list_entry(pos, struct vf_macvlans, l);
472 		if (entry->free)
473 			break;
474 	}
475 
476 	/*
477 	 * If we traversed the entire list and didn't find a free entry
478 	 * then we're out of space on the RAR table.  Also entry may
479 	 * be NULL because the original memory allocation for the list
480 	 * failed, which is not fatal but does mean we can't support
481 	 * VF requests for MACVLAN because we couldn't allocate
482 	 * memory for the list management required.
483 	 */
484 	if (!entry || !entry->free)
485 		return -ENOSPC;
486 
487 	entry->free = false;
488 	entry->is_macvlan = true;
489 	entry->vf = vf;
490 	memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
491 
492 	hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
493 
494 	return 0;
495 }
496 
497 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
498 {
499 	unsigned char vf_mac_addr[6];
500 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
501 	unsigned int vfn = (event_mask & 0x3f);
502 
503 	bool enable = ((event_mask & 0x10000000U) != 0);
504 
505 	if (enable) {
506 		eth_random_addr(vf_mac_addr);
507 		e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
508 		       vfn, vf_mac_addr);
509 		/*
510 		 * Store away the VF "permananet" MAC address, it will ask
511 		 * for it later.
512 		 */
513 		memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
514 	}
515 
516 	return 0;
517 }
518 
519 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
520 {
521 	struct ixgbe_hw *hw = &adapter->hw;
522 	u32 reg;
523 	u32 reg_offset, vf_shift;
524 
525 	vf_shift = vf % 32;
526 	reg_offset = vf / 32;
527 
528 	/* enable transmit and receive for vf */
529 	reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
530 	reg |= (reg | (1 << vf_shift));
531 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
532 
533 	reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
534 	reg |= (reg | (1 << vf_shift));
535 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
536 
537 	/* Enable counting of spoofed packets in the SSVPC register */
538 	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
539 	reg |= (1 << vf_shift);
540 	IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
541 
542 	ixgbe_vf_reset_event(adapter, vf);
543 }
544 
545 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
546 {
547 	u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
548 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
549 	struct ixgbe_hw *hw = &adapter->hw;
550 	s32 retval;
551 	int entries;
552 	u16 *hash_list;
553 	int add, vid, index;
554 	u8 *new_mac;
555 
556 	retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
557 
558 	if (retval) {
559 		pr_err("Error receiving message from VF\n");
560 		return retval;
561 	}
562 
563 	/* this is a message we already processed, do nothing */
564 	if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
565 		return retval;
566 
567 	/* flush the ack before we write any messages back */
568 	IXGBE_WRITE_FLUSH(hw);
569 
570 	/*
571 	 * until the vf completes a virtual function reset it should not be
572 	 * allowed to start any configuration.
573 	 */
574 
575 	if (msgbuf[0] == IXGBE_VF_RESET) {
576 		unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
577 		new_mac = (u8 *)(&msgbuf[1]);
578 		e_info(probe, "VF Reset msg received from vf %d\n", vf);
579 		adapter->vfinfo[vf].clear_to_send = false;
580 		ixgbe_vf_reset_msg(adapter, vf);
581 		adapter->vfinfo[vf].clear_to_send = true;
582 
583 		if (is_valid_ether_addr(new_mac) &&
584 		    !adapter->vfinfo[vf].pf_set_mac)
585 			ixgbe_set_vf_mac(adapter, vf, vf_mac);
586 		else
587 			ixgbe_set_vf_mac(adapter,
588 				 vf, adapter->vfinfo[vf].vf_mac_addresses);
589 
590 		/* reply to reset with ack and vf mac address */
591 		msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
592 		memcpy(new_mac, vf_mac, ETH_ALEN);
593 		/*
594 		 * Piggyback the multicast filter type so VF can compute the
595 		 * correct vectors
596 		 */
597 		msgbuf[3] = hw->mac.mc_filter_type;
598 		ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
599 
600 		return retval;
601 	}
602 
603 	if (!adapter->vfinfo[vf].clear_to_send) {
604 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
605 		ixgbe_write_mbx(hw, msgbuf, 1, vf);
606 		return retval;
607 	}
608 
609 	switch ((msgbuf[0] & 0xFFFF)) {
610 	case IXGBE_VF_SET_MAC_ADDR:
611 		new_mac = ((u8 *)(&msgbuf[1]));
612 		if (is_valid_ether_addr(new_mac) &&
613 		    !adapter->vfinfo[vf].pf_set_mac) {
614 			ixgbe_set_vf_mac(adapter, vf, new_mac);
615 		} else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
616 				  new_mac, ETH_ALEN)) {
617 			e_warn(drv, "VF %d attempted to override "
618 			       "administratively set MAC address\nReload "
619 			       "the VF driver to resume operations\n", vf);
620 			retval = -1;
621 		}
622 		break;
623 	case IXGBE_VF_SET_MULTICAST:
624 		entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
625 		          >> IXGBE_VT_MSGINFO_SHIFT;
626 		hash_list = (u16 *)&msgbuf[1];
627 		retval = ixgbe_set_vf_multicasts(adapter, entries,
628 		                                 hash_list, vf);
629 		break;
630 	case IXGBE_VF_SET_LPE:
631 		ixgbe_set_vf_lpe(adapter, msgbuf);
632 		break;
633 	case IXGBE_VF_SET_VLAN:
634 		add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
635 		      >> IXGBE_VT_MSGINFO_SHIFT;
636 		vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
637 		if (adapter->vfinfo[vf].pf_vlan) {
638 			e_warn(drv, "VF %d attempted to override "
639 			       "administratively set VLAN configuration\n"
640 			       "Reload the VF driver to resume operations\n",
641 			       vf);
642 			retval = -1;
643 		} else {
644 			if (add)
645 				adapter->vfinfo[vf].vlan_count++;
646 			else if (adapter->vfinfo[vf].vlan_count)
647 				adapter->vfinfo[vf].vlan_count--;
648 			retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
649 			if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
650 				hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
651 		}
652 		break;
653 	case IXGBE_VF_SET_MACVLAN:
654 		index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
655 			IXGBE_VT_MSGINFO_SHIFT;
656 		if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
657 			e_warn(drv, "VF %d requested MACVLAN filter but is "
658 				    "administratively denied\n", vf);
659 			retval = -1;
660 			break;
661 		}
662 		/*
663 		 * If the VF is allowed to set MAC filters then turn off
664 		 * anti-spoofing to avoid false positives.  An index
665 		 * greater than 0 will indicate the VF is setting a
666 		 * macvlan MAC filter.
667 		 */
668 		if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
669 			ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
670 		retval = ixgbe_set_vf_macvlan(adapter, vf, index,
671 					      (unsigned char *)(&msgbuf[1]));
672 		if (retval == -ENOSPC)
673 			e_warn(drv, "VF %d has requested a MACVLAN filter "
674 				    "but there is no space for it\n", vf);
675 		break;
676 	default:
677 		e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
678 		retval = IXGBE_ERR_MBX;
679 		break;
680 	}
681 
682 	/* notify the VF of the results of what it sent us */
683 	if (retval)
684 		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
685 	else
686 		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
687 
688 	msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
689 
690 	ixgbe_write_mbx(hw, msgbuf, 1, vf);
691 
692 	return retval;
693 }
694 
695 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
696 {
697 	struct ixgbe_hw *hw = &adapter->hw;
698 	u32 msg = IXGBE_VT_MSGTYPE_NACK;
699 
700 	/* if device isn't clear to send it shouldn't be reading either */
701 	if (!adapter->vfinfo[vf].clear_to_send)
702 		ixgbe_write_mbx(hw, &msg, 1, vf);
703 }
704 
705 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
706 {
707 	struct ixgbe_hw *hw = &adapter->hw;
708 	u32 vf;
709 
710 	for (vf = 0; vf < adapter->num_vfs; vf++) {
711 		/* process any reset requests */
712 		if (!ixgbe_check_for_rst(hw, vf))
713 			ixgbe_vf_reset_event(adapter, vf);
714 
715 		/* process any messages pending */
716 		if (!ixgbe_check_for_msg(hw, vf))
717 			ixgbe_rcv_msg_from_vf(adapter, vf);
718 
719 		/* process any acks */
720 		if (!ixgbe_check_for_ack(hw, vf))
721 			ixgbe_rcv_ack_from_vf(adapter, vf);
722 	}
723 }
724 
725 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
726 {
727 	struct ixgbe_hw *hw = &adapter->hw;
728 
729 	/* disable transmit and receive for all vfs */
730 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
731 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
732 
733 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
734 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
735 }
736 
737 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
738 {
739 	struct ixgbe_hw *hw = &adapter->hw;
740 	u32 ping;
741 	int i;
742 
743 	for (i = 0 ; i < adapter->num_vfs; i++) {
744 		ping = IXGBE_PF_CONTROL_MSG;
745 		if (adapter->vfinfo[i].clear_to_send)
746 			ping |= IXGBE_VT_MSGTYPE_CTS;
747 		ixgbe_write_mbx(hw, &ping, 1, i);
748 	}
749 }
750 
751 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
752 {
753 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
754 	if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
755 		return -EINVAL;
756 	adapter->vfinfo[vf].pf_set_mac = true;
757 	dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
758 	dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
759 				      " change effective.");
760 	if (test_bit(__IXGBE_DOWN, &adapter->state)) {
761 		dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
762 			 " but the PF device is not up.\n");
763 		dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
764 			 " attempting to use the VF device.\n");
765 	}
766 	return ixgbe_set_vf_mac(adapter, vf, mac);
767 }
768 
769 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
770 {
771 	int err = 0;
772 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
773 	struct ixgbe_hw *hw = &adapter->hw;
774 
775 	if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
776 		return -EINVAL;
777 	if (vlan || qos) {
778 		err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
779 		if (err)
780 			goto out;
781 		ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
782 		ixgbe_set_vmolr(hw, vf, false);
783 		if (adapter->vfinfo[vf].spoofchk_enabled)
784 			hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
785 		adapter->vfinfo[vf].vlan_count++;
786 		adapter->vfinfo[vf].pf_vlan = vlan;
787 		adapter->vfinfo[vf].pf_qos = qos;
788 		dev_info(&adapter->pdev->dev,
789 			 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
790 		if (test_bit(__IXGBE_DOWN, &adapter->state)) {
791 			dev_warn(&adapter->pdev->dev,
792 				 "The VF VLAN has been set,"
793 				 " but the PF device is not up.\n");
794 			dev_warn(&adapter->pdev->dev,
795 				 "Bring the PF device up before"
796 				 " attempting to use the VF device.\n");
797 		}
798 	} else {
799 		err = ixgbe_set_vf_vlan(adapter, false,
800 					adapter->vfinfo[vf].pf_vlan, vf);
801 		ixgbe_set_vmvir(adapter, vlan, vf);
802 		ixgbe_set_vmolr(hw, vf, true);
803 		hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
804 		if (adapter->vfinfo[vf].vlan_count)
805 			adapter->vfinfo[vf].vlan_count--;
806 		adapter->vfinfo[vf].pf_vlan = 0;
807 		adapter->vfinfo[vf].pf_qos = 0;
808        }
809 out:
810        return err;
811 }
812 
813 static int ixgbe_link_mbps(int internal_link_speed)
814 {
815 	switch (internal_link_speed) {
816 	case IXGBE_LINK_SPEED_100_FULL:
817 		return 100;
818 	case IXGBE_LINK_SPEED_1GB_FULL:
819 		return 1000;
820 	case IXGBE_LINK_SPEED_10GB_FULL:
821 		return 10000;
822 	default:
823 		return 0;
824 	}
825 }
826 
827 static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
828 				    int link_speed)
829 {
830 	int rf_dec, rf_int;
831 	u32 bcnrc_val;
832 
833 	if (tx_rate != 0) {
834 		/* Calculate the rate factor values to set */
835 		rf_int = link_speed / tx_rate;
836 		rf_dec = (link_speed - (rf_int * tx_rate));
837 		rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
838 
839 		bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
840 		bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
841 		               IXGBE_RTTBCNRC_RF_INT_MASK);
842 		bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
843 	} else {
844 		bcnrc_val = 0;
845 	}
846 
847 	IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
848 	/*
849 	 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
850 	 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
851 	 * and 0x004 otherwise.
852 	 */
853 	switch (hw->mac.type) {
854 	case ixgbe_mac_82599EB:
855 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
856 		break;
857 	case ixgbe_mac_X540:
858 		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
859 		break;
860 	default:
861 		break;
862 	}
863 
864 	IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
865 }
866 
867 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
868 {
869 	int actual_link_speed, i;
870 	bool reset_rate = false;
871 
872 	/* VF Tx rate limit was not set */
873 	if (adapter->vf_rate_link_speed == 0)
874 		return;
875 
876 	actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
877 	if (actual_link_speed != adapter->vf_rate_link_speed) {
878 		reset_rate = true;
879 		adapter->vf_rate_link_speed = 0;
880 		dev_info(&adapter->pdev->dev,
881 		         "Link speed has been changed. VF Transmit rate "
882 		         "is disabled\n");
883 	}
884 
885 	for (i = 0; i < adapter->num_vfs; i++) {
886 		if (reset_rate)
887 			adapter->vfinfo[i].tx_rate = 0;
888 
889 		ixgbe_set_vf_rate_limit(&adapter->hw, i,
890 					adapter->vfinfo[i].tx_rate,
891 					actual_link_speed);
892 	}
893 }
894 
895 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
896 {
897 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
898 	struct ixgbe_hw *hw = &adapter->hw;
899 	int actual_link_speed;
900 
901 	actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
902 	if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
903 	    (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
904 	    ((tx_rate != 0) && (tx_rate <= 10)))
905 	    /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
906 		return -EINVAL;
907 
908 	adapter->vf_rate_link_speed = actual_link_speed;
909 	adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
910 	ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
911 
912 	return 0;
913 }
914 
915 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
916 {
917 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
918 	int vf_target_reg = vf >> 3;
919 	int vf_target_shift = vf % 8;
920 	struct ixgbe_hw *hw = &adapter->hw;
921 	u32 regval;
922 
923 	adapter->vfinfo[vf].spoofchk_enabled = setting;
924 
925 	regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
926 	regval &= ~(1 << vf_target_shift);
927 	regval |= (setting << vf_target_shift);
928 	IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
929 
930 	if (adapter->vfinfo[vf].vlan_count) {
931 		vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
932 		regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
933 		regval &= ~(1 << vf_target_shift);
934 		regval |= (setting << vf_target_shift);
935 		IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
936 	}
937 
938 	return 0;
939 }
940 
941 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
942 			    int vf, struct ifla_vf_info *ivi)
943 {
944 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
945 	if (vf >= adapter->num_vfs)
946 		return -EINVAL;
947 	ivi->vf = vf;
948 	memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
949 	ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
950 	ivi->vlan = adapter->vfinfo[vf].pf_vlan;
951 	ivi->qos = adapter->vfinfo[vf].pf_qos;
952 	ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
953 	return 0;
954 }
955