ixgbe_sriov.c 27.2 KB
Newer Older
G
Greg Rose 已提交
1 2 3
/*******************************************************************************

  Intel 10 Gigabit PCI Express Linux driver
D
Don Skidmore 已提交
4
  Copyright(c) 1999 - 2012 Intel Corporation.
G
Greg Rose 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information:
  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497

*******************************************************************************/

#include <linux/types.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/ipv6.h>
#ifdef NETIF_F_HW_VLAN_TX
#include <linux/if_vlan.h>
#endif

#include "ixgbe.h"
G
Greg Rose 已提交
43
#include "ixgbe_type.h"
G
Greg Rose 已提交
44 45
#include "ixgbe_sriov.h"

G
Greg Rose 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#ifdef CONFIG_PCI_IOV
static int ixgbe_find_enabled_vfs(struct ixgbe_adapter *adapter)
{
	struct pci_dev *pdev = adapter->pdev;
	struct pci_dev *pvfdev;
	u16 vf_devfn = 0;
	int device_id;
	int vfs_found = 0;

	switch (adapter->hw.mac.type) {
	case ixgbe_mac_82599EB:
		device_id = IXGBE_DEV_ID_82599_VF;
		break;
	case ixgbe_mac_X540:
		device_id = IXGBE_DEV_ID_X540_VF;
		break;
	default:
		device_id = 0;
		break;
	}

	vf_devfn = pdev->devfn + 0x80;
	pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
	while (pvfdev) {
G
Greg Rose 已提交
70 71
		if (pvfdev->devfn == vf_devfn &&
		    (pvfdev->bus->number >= pdev->bus->number))
G
Greg Rose 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
			vfs_found++;
		vf_devfn += 2;
		pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
					device_id, pvfdev);
	}

	return vfs_found;
}

void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
			 const struct ixgbe_info *ii)
{
	struct ixgbe_hw *hw = &adapter->hw;
	int err = 0;
	int num_vf_macvlans, i;
	struct vf_macvlans *mv_list;
	int pre_existing_vfs = 0;

	pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
	if (!pre_existing_vfs && !adapter->num_vfs)
		return;

	/* If there are pre-existing VFs then we have to force
	 * use of that many because they were not deleted the last
	 * time someone removed the PF driver.  That would have
	 * been because they were allocated to guest VMs and can't
	 * be removed.  Go ahead and just re-enable the old amount.
	 * If the user wants to change the number of VFs they can
	 * use ethtool while making sure no VFs are allocated to
	 * guest VMs... i.e. the right way.
	 */
	if (pre_existing_vfs) {
		adapter->num_vfs = pre_existing_vfs;
		dev_warn(&adapter->pdev->dev, "Virtual Functions already "
			 "enabled for this device - Please reload all "
			 "VF drivers to avoid spoofed packet errors\n");
	} else {
		err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
110 111 112 113
		if (err) {
			e_err(probe, "Failed to enable PCI sriov: %d\n", err);
			goto err_novfs;
		}
G
Greg Rose 已提交
114 115
	}

116
	adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
G
Greg Rose 已提交
117 118
	e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);

119 120 121 122 123 124
	/* Enable VMDq flag so device will be set in VM mode */
	adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED;
	if (!adapter->ring_feature[RING_F_VMDQ].limit)
		adapter->ring_feature[RING_F_VMDQ].limit = 1;
	adapter->ring_feature[RING_F_VMDQ].offset = adapter->num_vfs;

G
Greg Rose 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	num_vf_macvlans = hw->mac.num_rar_entries -
	(IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);

	adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
					     sizeof(struct vf_macvlans),
					     GFP_KERNEL);
	if (mv_list) {
		/* Initialize list of VF macvlans */
		INIT_LIST_HEAD(&adapter->vf_mvs.l);
		for (i = 0; i < num_vf_macvlans; i++) {
			mv_list->vf = -1;
			mv_list->free = true;
			mv_list->rar_entry = hw->mac.num_rar_entries -
				(i + adapter->num_vfs + 1);
			list_add(&mv_list->l, &adapter->vf_mvs.l);
			mv_list++;
		}
	}

	/* If call to enable VFs succeeded then allocate memory
	 * for per VF control structures.
	 */
	adapter->vfinfo =
		kcalloc(adapter->num_vfs,
			sizeof(struct vf_data_storage), GFP_KERNEL);
	if (adapter->vfinfo) {
		/* Now that we're sure SR-IOV is enabled
		 * and memory allocated set up the mailbox parameters
		 */
		ixgbe_init_mbx_params_pf(hw);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		memcpy(&hw->mbx.ops, ii->mbx_ops, sizeof(hw->mbx.ops));

		/* limit trafffic classes based on VFs enabled */
		if ((adapter->hw.mac.type == ixgbe_mac_82599EB) &&
		    (adapter->num_vfs < 16)) {
			adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
			adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
		} else if (adapter->num_vfs < 32) {
			adapter->dcb_cfg.num_tcs.pg_tcs = 4;
			adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
		} else {
			adapter->dcb_cfg.num_tcs.pg_tcs = 1;
			adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
		}

		/* We do not support RSS w/ SR-IOV */
		adapter->ring_feature[RING_F_RSS].limit = 1;
G
Greg Rose 已提交
172 173 174 175

		/* Disable RSC when in SR-IOV mode */
		adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
				     IXGBE_FLAG2_RSC_ENABLED);
176 177 178 179 180 181 182 183 184 185 186 187

#ifdef IXGBE_FCOE
		/*
		 * When SR-IOV is enabled 82599 cannot support jumbo frames
		 * so we must disable FCoE because we cannot support FCoE MTU.
		 */
		if (adapter->hw.mac.type == ixgbe_mac_82599EB)
			adapter->flags &= ~(IXGBE_FLAG_FCOE_ENABLED |
					    IXGBE_FLAG_FCOE_CAPABLE);
#endif

		/* enable spoof checking for all VFs */
188 189
		for (i = 0; i < adapter->num_vfs; i++)
			adapter->vfinfo[i].spoofchk_enabled = true;
G
Greg Rose 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		return;
	}

	/* Oh oh */
	e_err(probe, "Unable to allocate memory for VF Data Storage - "
	      "SRIOV disabled\n");
	pci_disable_sriov(adapter->pdev);

err_novfs:
	adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
	adapter->num_vfs = 0;
}
#endif /* #ifdef CONFIG_PCI_IOV */

void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 gpie;
	u32 vmdctl;
	int i;

211 212 213 214 215 216 217 218 219 220 221
	/* set num VFs to 0 to prevent access to vfinfo */
	adapter->num_vfs = 0;

	/* free VF control structures */
	kfree(adapter->vfinfo);
	adapter->vfinfo = NULL;

	/* free macvlan list */
	kfree(adapter->mv_list);
	adapter->mv_list = NULL;

G
Greg Rose 已提交
222 223 224 225 226 227
#ifdef CONFIG_PCI_IOV
	/* disable iov and allow time for transactions to clear */
	pci_disable_sriov(adapter->pdev);
#endif

	/* turn off device IOV mode */
228
	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
G
Greg Rose 已提交
229 230 231 232 233 234 235 236 237 238
	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
	gpie &= ~IXGBE_GPIE_VTMODE_MASK;
	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);

	/* set default pool back to 0 */
	vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
	vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
	IXGBE_WRITE_FLUSH(hw);

239 240 241 242 243
	/* Disable VMDq flag so device will be set in VM mode */
	if (adapter->ring_feature[RING_F_VMDQ].limit == 1)
		adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
	adapter->ring_feature[RING_F_VMDQ].offset = 0;

G
Greg Rose 已提交
244 245 246 247 248 249 250 251 252 253 254 255
	/* take a breather then clean up driver data */
	msleep(100);

	/* Release reference to VF devices */
	for (i = 0; i < adapter->num_vfs; i++) {
		if (adapter->vfinfo[i].vfdev)
			pci_dev_put(adapter->vfinfo[i].vfdev);
	}

	adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
}

E
Emil Tantilov 已提交
256 257
static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
				   int entries, u16 *hash_list, u32 vf)
G
Greg Rose 已提交
258 259
{
	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
260
	struct ixgbe_hw *hw = &adapter->hw;
G
Greg Rose 已提交
261
	int i;
262 263 264
	u32 vector_bit;
	u32 vector_reg;
	u32 mta_reg;
G
Greg Rose 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

	/* only so many hash values supported */
	entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);

	/*
	 * salt away the number of multi cast addresses assigned
	 * to this VF for later use to restore when the PF multi cast
	 * list changes
	 */
	vfinfo->num_vf_mc_hashes = entries;

	/*
	 * VFs are limited to using the MTA hash table for their multicast
	 * addresses
	 */
	for (i = 0; i < entries; i++) {
281
		vfinfo->vf_mc_hashes[i] = hash_list[i];
G
Greg Rose 已提交
282 283
	}

284 285 286 287 288 289 290
	for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
		vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
		vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
		mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
		mta_reg |= (1 << vector_bit);
		IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
	}
G
Greg Rose 已提交
291 292 293 294

	return 0;
}

G
Greg Rose 已提交
295 296 297 298 299 300 301 302
static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;
	struct list_head *pos;
	struct vf_macvlans *entry;

	list_for_each(pos, &adapter->vf_mvs.l) {
		entry = list_entry(pos, struct vf_macvlans, l);
303
		if (!entry->free)
G
Greg Rose 已提交
304 305 306 307 308 309
			hw->mac.ops.set_rar(hw, entry->rar_entry,
					    entry->vf_macvlan,
					    entry->vf, IXGBE_RAH_AV);
	}
}

G
Greg Rose 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;
	struct vf_data_storage *vfinfo;
	int i, j;
	u32 vector_bit;
	u32 vector_reg;
	u32 mta_reg;

	for (i = 0; i < adapter->num_vfs; i++) {
		vfinfo = &adapter->vfinfo[i];
		for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
			hw->addr_ctrl.mta_in_use++;
			vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
			vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
			mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
			mta_reg |= (1 << vector_bit);
			IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
		}
	}
G
Greg Rose 已提交
330 331 332

	/* Restore any VF macvlans */
	ixgbe_restore_vf_macvlans(adapter);
G
Greg Rose 已提交
333 334
}

E
Emil Tantilov 已提交
335 336
static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
			     u32 vf)
G
Greg Rose 已提交
337 338 339 340
{
	return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
}

J
John Fastabend 已提交
341
static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
{
	struct ixgbe_hw *hw = &adapter->hw;
	int new_mtu = msgbuf[1];
	u32 max_frs;
	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;

	/* Only X540 supports jumbo frames in IOV mode */
	if (adapter->hw.mac.type != ixgbe_mac_X540)
		return;

	/* MTU < 68 is an error and causes problems on some kernels */
	if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
		e_err(drv, "VF mtu %d out of range\n", new_mtu);
		return;
	}

	max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
		   IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
	if (max_frs < new_mtu) {
		max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
		IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
	}

	e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
}

E
Emil Tantilov 已提交
368
static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
G
Greg Rose 已提交
369 370
{
	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
371
	vmolr |= (IXGBE_VMOLR_ROMPE |
G
Greg Rose 已提交
372
		  IXGBE_VMOLR_BAM);
373 374 375 376
	if (aupe)
		vmolr |= IXGBE_VMOLR_AUPE;
	else
		vmolr &= ~IXGBE_VMOLR_AUPE;
G
Greg Rose 已提交
377 378 379
	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
}

380 381 382 383 384 385 386 387 388 389 390
static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
{
	struct ixgbe_hw *hw = &adapter->hw;

	if (vid)
		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
				(vid | IXGBE_VMVIR_VLANA_DEFAULT));
	else
		IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
}

E
Emil Tantilov 已提交
391
static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
G
Greg Rose 已提交
392 393
{
	struct ixgbe_hw *hw = &adapter->hw;
394
	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
G
Greg Rose 已提交
395 396

	/* reset offloads to defaults */
397 398 399 400 401 402 403 404 405 406 407 408
	if (adapter->vfinfo[vf].pf_vlan) {
		ixgbe_set_vf_vlan(adapter, true,
				  adapter->vfinfo[vf].pf_vlan, vf);
		ixgbe_set_vmvir(adapter,
				(adapter->vfinfo[vf].pf_vlan |
				 (adapter->vfinfo[vf].pf_qos <<
				  VLAN_PRIO_SHIFT)), vf);
		ixgbe_set_vmolr(hw, vf, false);
	} else {
		ixgbe_set_vmvir(adapter, 0, vf);
		ixgbe_set_vmolr(hw, vf, true);
	}
G
Greg Rose 已提交
409 410 411 412 413 414 415

	/* reset multicast table array for vf */
	adapter->vfinfo[vf].num_vf_mc_hashes = 0;

	/* Flush and reset the mta with the new values */
	ixgbe_set_rx_mode(adapter->netdev);

416
	hw->mac.ops.clear_rar(hw, rar_entry);
G
Greg Rose 已提交
417 418
}

E
Emil Tantilov 已提交
419 420
static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
			    int vf, unsigned char *mac_addr)
G
Greg Rose 已提交
421 422
{
	struct ixgbe_hw *hw = &adapter->hw;
423
	int rar_entry = hw->mac.num_rar_entries - (vf + 1);
G
Greg Rose 已提交
424 425

	memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
426
	hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
G
Greg Rose 已提交
427 428 429 430

	return 0;
}

G
Greg Rose 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
				int vf, int index, unsigned char *mac_addr)
{
	struct ixgbe_hw *hw = &adapter->hw;
	struct list_head *pos;
	struct vf_macvlans *entry;

	if (index <= 1) {
		list_for_each(pos, &adapter->vf_mvs.l) {
			entry = list_entry(pos, struct vf_macvlans, l);
			if (entry->vf == vf) {
				entry->vf = -1;
				entry->free = true;
				entry->is_macvlan = false;
				hw->mac.ops.clear_rar(hw, entry->rar_entry);
			}
		}
	}

	/*
	 * If index was zero then we were asked to clear the uc list
	 * for the VF.  We're done.
	 */
	if (!index)
		return 0;

	entry = NULL;

	list_for_each(pos, &adapter->vf_mvs.l) {
		entry = list_entry(pos, struct vf_macvlans, l);
		if (entry->free)
			break;
	}

	/*
	 * If we traversed the entire list and didn't find a free entry
	 * then we're out of space on the RAR table.  Also entry may
	 * be NULL because the original memory allocation for the list
	 * failed, which is not fatal but does mean we can't support
	 * VF requests for MACVLAN because we couldn't allocate
	 * memory for the list management required.
	 */
	if (!entry || !entry->free)
		return -ENOSPC;

	entry->free = false;
	entry->is_macvlan = true;
	entry->vf = vf;
	memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);

	hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);

	return 0;
}

G
Greg Rose 已提交
486 487
int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
{
488
#ifdef CONFIG_PCI_IOV
G
Greg Rose 已提交
489 490 491 492 493 494
	int i;
	for (i = 0; i < adapter->num_vfs; i++) {
		if (adapter->vfinfo[i].vfdev->dev_flags &
				PCI_DEV_FLAGS_ASSIGNED)
			return true;
	}
495
#endif
G
Greg Rose 已提交
496 497 498
	return false;
}

G
Greg Rose 已提交
499 500 501
int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
{
	unsigned char vf_mac_addr[6];
502
	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
G
Greg Rose 已提交
503
	unsigned int vfn = (event_mask & 0x3f);
G
Greg Rose 已提交
504 505 506 507
	struct pci_dev *pvfdev;
	unsigned int device_id;
	u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
				(pdev->devfn & 1);
G
Greg Rose 已提交
508 509 510 511

	bool enable = ((event_mask & 0x10000000U) != 0);

	if (enable) {
J
Joe Perches 已提交
512
		eth_random_addr(vf_mac_addr);
513 514
		e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
		       vfn, vf_mac_addr);
G
Greg Rose 已提交
515 516 517 518 519
		/*
		 * Store away the VF "permananet" MAC address, it will ask
		 * for it later.
		 */
		memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
G
Greg Rose 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

		switch (adapter->hw.mac.type) {
		case ixgbe_mac_82599EB:
			device_id = IXGBE_DEV_ID_82599_VF;
			break;
		case ixgbe_mac_X540:
			device_id = IXGBE_DEV_ID_X540_VF;
			break;
		default:
			device_id = 0;
			break;
		}

		pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
		while (pvfdev) {
			if (pvfdev->devfn == thisvf_devfn)
				break;
			pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
						device_id, pvfdev);
		}
		if (pvfdev)
			adapter->vfinfo[vfn].vfdev = pvfdev;
		else
			e_err(drv, "Couldn't find pci dev ptr for VF %4.4x\n",
			      thisvf_devfn);
G
Greg Rose 已提交
545 546 547 548 549
	}

	return 0;
}

E
Emil Tantilov 已提交
550
static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
G
Greg Rose 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 reg;
	u32 reg_offset, vf_shift;

	vf_shift = vf % 32;
	reg_offset = vf / 32;

	/* enable transmit and receive for vf */
	reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
	reg |= (reg | (1 << vf_shift));
	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);

	reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
	reg |= (reg | (1 << vf_shift));
	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);

568 569 570 571 572
	/* Enable counting of spoofed packets in the SSVPC register */
	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
	reg |= (1 << vf_shift);
	IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);

G
Greg Rose 已提交
573 574 575 576 577 578
	ixgbe_vf_reset_event(adapter, vf);
}

static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
{
	u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
E
Emil Tantilov 已提交
579
	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
G
Greg Rose 已提交
580 581 582 583
	struct ixgbe_hw *hw = &adapter->hw;
	s32 retval;
	int entries;
	u16 *hash_list;
G
Greg Rose 已提交
584
	int add, vid, index;
585
	u8 *new_mac;
G
Greg Rose 已提交
586 587 588

	retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);

589
	if (retval) {
590
		pr_err("Error receiving message from VF\n");
591 592
		return retval;
	}
G
Greg Rose 已提交
593 594 595 596 597

	/* this is a message we already processed, do nothing */
	if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
		return retval;

598 599 600
	/* flush the ack before we write any messages back */
	IXGBE_WRITE_FLUSH(hw);

G
Greg Rose 已提交
601 602 603 604 605 606 607
	/*
	 * until the vf completes a virtual function reset it should not be
	 * allowed to start any configuration.
	 */

	if (msgbuf[0] == IXGBE_VF_RESET) {
		unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
608
		new_mac = (u8 *)(&msgbuf[1]);
609
		e_info(probe, "VF Reset msg received from vf %d\n", vf);
G
Greg Rose 已提交
610 611 612 613
		adapter->vfinfo[vf].clear_to_send = false;
		ixgbe_vf_reset_msg(adapter, vf);
		adapter->vfinfo[vf].clear_to_send = true;

614 615 616 617 618 619 620
		if (is_valid_ether_addr(new_mac) &&
		    !adapter->vfinfo[vf].pf_set_mac)
			ixgbe_set_vf_mac(adapter, vf, vf_mac);
		else
			ixgbe_set_vf_mac(adapter,
				 vf, adapter->vfinfo[vf].vf_mac_addresses);

G
Greg Rose 已提交
621 622
		/* reply to reset with ack and vf mac address */
		msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
623
		memcpy(new_mac, vf_mac, ETH_ALEN);
G
Greg Rose 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
		/*
		 * Piggyback the multicast filter type so VF can compute the
		 * correct vectors
		 */
		msgbuf[3] = hw->mac.mc_filter_type;
		ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);

		return retval;
	}

	if (!adapter->vfinfo[vf].clear_to_send) {
		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
		ixgbe_write_mbx(hw, msgbuf, 1, vf);
		return retval;
	}

	switch ((msgbuf[0] & 0xFFFF)) {
	case IXGBE_VF_SET_MAC_ADDR:
642 643 644 645 646 647 648 649 650 651
		new_mac = ((u8 *)(&msgbuf[1]));
		if (is_valid_ether_addr(new_mac) &&
		    !adapter->vfinfo[vf].pf_set_mac) {
			ixgbe_set_vf_mac(adapter, vf, new_mac);
		} else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
				  new_mac, ETH_ALEN)) {
			e_warn(drv, "VF %d attempted to override "
			       "administratively set MAC address\nReload "
			       "the VF driver to resume operations\n", vf);
			retval = -1;
G
Greg Rose 已提交
652 653 654 655 656 657 658 659 660 661
		}
		break;
	case IXGBE_VF_SET_MULTICAST:
		entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
		          >> IXGBE_VT_MSGINFO_SHIFT;
		hash_list = (u16 *)&msgbuf[1];
		retval = ixgbe_set_vf_multicasts(adapter, entries,
		                                 hash_list, vf);
		break;
	case IXGBE_VF_SET_LPE:
662
		ixgbe_set_vf_lpe(adapter, msgbuf);
G
Greg Rose 已提交
663 664 665 666 667
		break;
	case IXGBE_VF_SET_VLAN:
		add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
		      >> IXGBE_VT_MSGINFO_SHIFT;
		vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
668 669 670 671 672 673 674
		if (adapter->vfinfo[vf].pf_vlan) {
			e_warn(drv, "VF %d attempted to override "
			       "administratively set VLAN configuration\n"
			       "Reload the VF driver to resume operations\n",
			       vf);
			retval = -1;
		} else {
675 676 677 678
			if (add)
				adapter->vfinfo[vf].vlan_count++;
			else if (adapter->vfinfo[vf].vlan_count)
				adapter->vfinfo[vf].vlan_count--;
679
			retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
680 681
			if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
				hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
682
		}
G
Greg Rose 已提交
683
		break;
G
Greg Rose 已提交
684
	case IXGBE_VF_SET_MACVLAN:
G
Greg Rose 已提交
685 686 687
		index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
			IXGBE_VT_MSGINFO_SHIFT;
		if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
688 689 690 691 692
			e_warn(drv, "VF %d requested MACVLAN filter but is "
				    "administratively denied\n", vf);
			retval = -1;
			break;
		}
G
Greg Rose 已提交
693 694 695 696 697 698
		/*
		 * If the VF is allowed to set MAC filters then turn off
		 * anti-spoofing to avoid false positives.  An index
		 * greater than 0 will indicate the VF is setting a
		 * macvlan MAC filter.
		 */
699 700
		if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
			ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
G
Greg Rose 已提交
701 702
		retval = ixgbe_set_vf_macvlan(adapter, vf, index,
					      (unsigned char *)(&msgbuf[1]));
703 704 705
		if (retval == -ENOSPC)
			e_warn(drv, "VF %d has requested a MACVLAN filter "
				    "but there is no space for it\n", vf);
G
Greg Rose 已提交
706
		break;
G
Greg Rose 已提交
707
	default:
708
		e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
G
Greg Rose 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
		retval = IXGBE_ERR_MBX;
		break;
	}

	/* notify the VF of the results of what it sent us */
	if (retval)
		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
	else
		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;

	msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;

	ixgbe_write_mbx(hw, msgbuf, 1, vf);

	return retval;
}

static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 msg = IXGBE_VT_MSGTYPE_NACK;

	/* if device isn't clear to send it shouldn't be reading either */
	if (!adapter->vfinfo[vf].clear_to_send)
		ixgbe_write_mbx(hw, &msg, 1, vf);
}

void ixgbe_msg_task(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 vf;

	for (vf = 0; vf < adapter->num_vfs; vf++) {
		/* process any reset requests */
		if (!ixgbe_check_for_rst(hw, vf))
			ixgbe_vf_reset_event(adapter, vf);

		/* process any messages pending */
		if (!ixgbe_check_for_msg(hw, vf))
			ixgbe_rcv_msg_from_vf(adapter, vf);

		/* process any acks */
		if (!ixgbe_check_for_ack(hw, vf))
			ixgbe_rcv_ack_from_vf(adapter, vf);
	}
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;

	/* disable transmit and receive for all vfs */
	IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
	IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);

	IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
	IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
}

void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 ping;
	int i;

	for (i = 0 ; i < adapter->num_vfs; i++) {
		ping = IXGBE_PF_CONTROL_MSG;
		if (adapter->vfinfo[i].clear_to_send)
			ping |= IXGBE_VT_MSGTYPE_CTS;
		ixgbe_write_mbx(hw, &ping, 1, i);
	}
}

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
	if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
		return -EINVAL;
	adapter->vfinfo[vf].pf_set_mac = true;
	dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
	dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
				      " change effective.");
	if (test_bit(__IXGBE_DOWN, &adapter->state)) {
		dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
			 " but the PF device is not up.\n");
		dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
			 " attempting to use the VF device.\n");
	}
	return ixgbe_set_vf_mac(adapter, vf, mac);
}

int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
{
	int err = 0;
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
804
	struct ixgbe_hw *hw = &adapter->hw;
805 806 807 808 809 810 811 812

	if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
		return -EINVAL;
	if (vlan || qos) {
		err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
		if (err)
			goto out;
		ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
813
		ixgbe_set_vmolr(hw, vf, false);
814
		if (adapter->vfinfo[vf].spoofchk_enabled)
G
Greg Rose 已提交
815
			hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
816
		adapter->vfinfo[vf].vlan_count++;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
		adapter->vfinfo[vf].pf_vlan = vlan;
		adapter->vfinfo[vf].pf_qos = qos;
		dev_info(&adapter->pdev->dev,
			 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
		if (test_bit(__IXGBE_DOWN, &adapter->state)) {
			dev_warn(&adapter->pdev->dev,
				 "The VF VLAN has been set,"
				 " but the PF device is not up.\n");
			dev_warn(&adapter->pdev->dev,
				 "Bring the PF device up before"
				 " attempting to use the VF device.\n");
		}
	} else {
		err = ixgbe_set_vf_vlan(adapter, false,
					adapter->vfinfo[vf].pf_vlan, vf);
		ixgbe_set_vmvir(adapter, vlan, vf);
833 834
		ixgbe_set_vmolr(hw, vf, true);
		hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
835 836
		if (adapter->vfinfo[vf].vlan_count)
			adapter->vfinfo[vf].vlan_count--;
837 838 839 840 841 842 843
		adapter->vfinfo[vf].pf_vlan = 0;
		adapter->vfinfo[vf].pf_qos = 0;
       }
out:
       return err;
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
static int ixgbe_link_mbps(int internal_link_speed)
{
	switch (internal_link_speed) {
	case IXGBE_LINK_SPEED_100_FULL:
		return 100;
	case IXGBE_LINK_SPEED_1GB_FULL:
		return 1000;
	case IXGBE_LINK_SPEED_10GB_FULL:
		return 10000;
	default:
		return 0;
	}
}

static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
				    int link_speed)
{
	int rf_dec, rf_int;
	u32 bcnrc_val;

	if (tx_rate != 0) {
		/* Calculate the rate factor values to set */
		rf_int = link_speed / tx_rate;
		rf_dec = (link_speed - (rf_int * tx_rate));
		rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;

		bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
		bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
		               IXGBE_RTTBCNRC_RF_INT_MASK);
		bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
	} else {
		bcnrc_val = 0;
	}

	IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
L
Lior Levy 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	/*
	 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
	 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
	 * and 0x004 otherwise.
	 */
	switch (hw->mac.type) {
	case ixgbe_mac_82599EB:
		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
		break;
	case ixgbe_mac_X540:
		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
		break;
	default:
		break;
	}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
	IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
}

void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
{
	int actual_link_speed, i;
	bool reset_rate = false;

	/* VF Tx rate limit was not set */
	if (adapter->vf_rate_link_speed == 0)
		return;

	actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
	if (actual_link_speed != adapter->vf_rate_link_speed) {
		reset_rate = true;
		adapter->vf_rate_link_speed = 0;
		dev_info(&adapter->pdev->dev,
		         "Link speed has been changed. VF Transmit rate "
		         "is disabled\n");
	}

	for (i = 0; i < adapter->num_vfs; i++) {
		if (reset_rate)
			adapter->vfinfo[i].tx_rate = 0;

		ixgbe_set_vf_rate_limit(&adapter->hw, i,
					adapter->vfinfo[i].tx_rate,
					actual_link_speed);
	}
}

926 927
int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
{
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
	struct ixgbe_hw *hw = &adapter->hw;
	int actual_link_speed;

	actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
	if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
	    (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
	    ((tx_rate != 0) && (tx_rate <= 10)))
	    /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
		return -EINVAL;

	adapter->vf_rate_link_speed = actual_link_speed;
	adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
	ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);

	return 0;
944 945
}

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
	int vf_target_reg = vf >> 3;
	int vf_target_shift = vf % 8;
	struct ixgbe_hw *hw = &adapter->hw;
	u32 regval;

	adapter->vfinfo[vf].spoofchk_enabled = setting;

	regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
	regval &= ~(1 << vf_target_shift);
	regval |= (setting << vf_target_shift);
	IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);

	if (adapter->vfinfo[vf].vlan_count) {
		vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
		regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
		regval &= ~(1 << vf_target_shift);
		regval |= (setting << vf_target_shift);
		IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
	}

	return 0;
}

972 973 974 975 976 977 978 979
int ixgbe_ndo_get_vf_config(struct net_device *netdev,
			    int vf, struct ifla_vf_info *ivi)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
	if (vf >= adapter->num_vfs)
		return -EINVAL;
	ivi->vf = vf;
	memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
980
	ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
981 982
	ivi->vlan = adapter->vfinfo[vf].pf_vlan;
	ivi->qos = adapter->vfinfo[vf].pf_qos;
983
	ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
984 985
	return 0;
}