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

  Intel 10 Gigabit PCI Express Linux driver
4
  Copyright(c) 1999 - 2015 Intel Corporation.
G
Greg Rose 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  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:
23
  Linux NICS <linux.nics@intel.com>
G
Greg Rose 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  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>
39
#include <linux/if_bridge.h>
40
#ifdef NETIF_F_HW_VLAN_CTAG_TX
G
Greg Rose 已提交
41 42 43 44
#include <linux/if_vlan.h>
#endif

#include "ixgbe.h"
G
Greg Rose 已提交
45
#include "ixgbe_type.h"
G
Greg Rose 已提交
46 47
#include "ixgbe_sriov.h"

G
Greg Rose 已提交
48
#ifdef CONFIG_PCI_IOV
49
static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
G
Greg Rose 已提交
50 51 52 53 54
{
	struct ixgbe_hw *hw = &adapter->hw;
	int num_vf_macvlans, i;
	struct vf_macvlans *mv_list;

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

58 59 60 61 62 63
	/* 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 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	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;
			list_add(&mv_list->l, &adapter->vf_mvs.l);
			mv_list++;
		}
	}

81 82
	/* Initialize default switching mode VEB */
	IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
83
	adapter->bridge_mode = BRIDGE_MODE_VEB;
84

G
Greg Rose 已提交
85 86 87 88 89 90 91
	/* 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) {
92 93 94 95 96 97 98 99 100 101 102 103 104
		/* 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;
		}

G
Greg Rose 已提交
105 106 107
		/* Disable RSC when in SR-IOV mode */
		adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
				     IXGBE_FLAG2_RSC_ENABLED);
108

109 110
		for (i = 0; i < adapter->num_vfs; i++) {
			/* enable spoof checking for all VFs */
111
			adapter->vfinfo[i].spoofchk_enabled = true;
112 113 114 115 116 117 118

			/* We support VF RSS querying only for 82599 and x540
			 * devices at the moment. These devices share RSS
			 * indirection table and RSS hash key with PF therefore
			 * we want to disable the querying by default.
			 */
			adapter->vfinfo[i].rss_query_enabled = 0;
H
Hiroshi Shimamoto 已提交
119 120 121

			/* Untrust all VFs */
			adapter->vfinfo[i].trusted = false;
122 123 124

			/* set the default xcast mode */
			adapter->vfinfo[i].xcast_mode = IXGBEVF_XCAST_MODE_NONE;
125 126
		}

127 128 129 130 131 132
		return 0;
	}

	return -ENOMEM;
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/**
 * ixgbe_get_vfs - Find and take references to all vf devices
 * @adapter: Pointer to adapter struct
 */
static void ixgbe_get_vfs(struct ixgbe_adapter *adapter)
{
	struct pci_dev *pdev = adapter->pdev;
	u16 vendor = pdev->vendor;
	struct pci_dev *vfdev;
	int vf = 0;
	u16 vf_id;
	int pos;

	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
	if (!pos)
		return;
	pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_id);

	vfdev = pci_get_device(vendor, vf_id, NULL);
	for (; vfdev; vfdev = pci_get_device(vendor, vf_id, vfdev)) {
		if (!vfdev->is_virtfn)
			continue;
		if (vfdev->physfn != pdev)
			continue;
		if (vf >= adapter->num_vfs)
			continue;
		pci_dev_get(vfdev);
		adapter->vfinfo[vf].vfdev = vfdev;
		++vf;
	}
}

165 166 167 168 169 170 171 172 173
/* Note this function is called when the user wants to enable SR-IOV
 * VFs using the now deprecated module parameter
 */
void ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
{
	int pre_existing_vfs = 0;

	pre_existing_vfs = pci_num_vf(adapter->pdev);
	if (!pre_existing_vfs && !adapter->num_vfs)
G
Greg Rose 已提交
174
		return;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	/* If there are pre-existing VFs then we have to force
	 * use of that many - over ride any module parameter value.
	 * This may result from the user unloading the PF driver
	 * while VFs were assigned to guest VMs or because the VFs
	 * have been created via the new PCI SR-IOV sysfs interface.
	 */
	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 {
		int err;
		/*
		 * The 82599 supports up to 64 VFs per physical function
		 * but this implementation limits allocation to 63 so that
		 * basic networking resources are still available to the
192
		 * physical function.  If the user requests greater than
193 194
		 * 63 VFs then it is an error - reset to default of zero.
		 */
195
		adapter->num_vfs = min_t(unsigned int, adapter->num_vfs, IXGBE_MAX_VFS_DRV_LIMIT);
196 197 198 199 200 201 202

		err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
		if (err) {
			e_err(probe, "Failed to enable PCI sriov: %d\n", err);
			adapter->num_vfs = 0;
			return;
		}
G
Greg Rose 已提交
203 204
	}

205 206
	if (!__ixgbe_enable_sriov(adapter)) {
		ixgbe_get_vfs(adapter);
207
		return;
208
	}
209 210 211 212

	/* If we have gotten to this point then there is no memory available
	 * to manage the VF devices - print message and bail.
	 */
G
Greg Rose 已提交
213 214
	e_err(probe, "Unable to allocate memory for VF Data Storage - "
	      "SRIOV disabled\n");
215
	ixgbe_disable_sriov(adapter);
G
Greg Rose 已提交
216 217
}

218
#endif /* #ifdef CONFIG_PCI_IOV */
219
int ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
G
Greg Rose 已提交
220
{
221
	unsigned int num_vfs = adapter->num_vfs, vf;
G
Greg Rose 已提交
222 223 224
	struct ixgbe_hw *hw = &adapter->hw;
	u32 gpie;
	u32 vmdctl;
225
	int rss;
G
Greg Rose 已提交
226

227 228 229
	/* set num VFs to 0 to prevent access to vfinfo */
	adapter->num_vfs = 0;

230 231 232 233 234 235 236 237 238 239
	/* put the reference to all of the vf devices */
	for (vf = 0; vf < num_vfs; ++vf) {
		struct pci_dev *vfdev = adapter->vfinfo[vf].vfdev;

		if (!vfdev)
			continue;
		adapter->vfinfo[vf].vfdev = NULL;
		pci_dev_put(vfdev);
	}

240 241 242 243 244 245 246 247
	/* free VF control structures */
	kfree(adapter->vfinfo);
	adapter->vfinfo = NULL;

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

248 249
	/* if SR-IOV is already disabled then there is nothing to do */
	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
250
		return 0;
251

G
Greg Rose 已提交
252
#ifdef CONFIG_PCI_IOV
253 254 255 256 257
	/*
	 * If our VFs are assigned we cannot shut down SR-IOV
	 * without causing issues, so just leave the hardware
	 * available but disabled
	 */
258
	if (pci_vfs_assigned(adapter->pdev)) {
259
		e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
260
		return -EPERM;
261
	}
G
Greg Rose 已提交
262 263 264 265 266
	/* disable iov and allow time for transactions to clear */
	pci_disable_sriov(adapter->pdev);
#endif

	/* turn off device IOV mode */
267
	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
G
Greg Rose 已提交
268 269 270 271 272 273 274 275 276 277
	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);

278
	/* Disable VMDq flag so device will be set in VM mode */
279
	if (adapter->ring_feature[RING_F_VMDQ].limit == 1) {
280
		adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
281
		adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
282 283
		rss = min_t(int, ixgbe_max_rss_indices(adapter),
			    num_online_cpus());
284 285 286
	} else {
		rss = min_t(int, IXGBE_MAX_L2A_QUEUES, num_online_cpus());
	}
287

288
	adapter->ring_feature[RING_F_VMDQ].offset = 0;
289 290
	adapter->ring_feature[RING_F_RSS].limit = rss;

G
Greg Rose 已提交
291 292
	/* take a breather then clean up driver data */
	msleep(100);
293 294 295 296 297 298 299 300 301 302 303 304 305 306
	return 0;
}

static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs)
{
#ifdef CONFIG_PCI_IOV
	struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
	int err = 0;
	int i;
	int pre_existing_vfs = pci_num_vf(dev);

	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
		err = ixgbe_disable_sriov(adapter);
	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
307
		return num_vfs;
308 309

	if (err)
310
		return err;
311

312 313 314 315 316 317 318
	/* While the SR-IOV capability structure reports total VFs to be 64,
	 * we have to limit the actual number allocated based on two factors.
	 * First, we reserve some transmit/receive resources for the PF.
	 * Second, VMDQ also uses the same pools that SR-IOV does. We need to
	 * account for this, so that we don't accidentally allocate more VFs
	 * than we have available pools. The PCI bus driver already checks for
	 * other values out of range.
319
	 */
320
	if ((num_vfs + adapter->num_rx_pools) > IXGBE_MAX_VF_FUNCTIONS)
321
		return -EPERM;
322 323 324 325 326

	adapter->num_vfs = num_vfs;

	err = __ixgbe_enable_sriov(adapter);
	if (err)
327
		return  err;
328 329 330 331

	for (i = 0; i < adapter->num_vfs; i++)
		ixgbe_vf_configuration(dev, (i | 0x10000000));

332 333 334
	/* reset before enabling SRIOV to avoid mailbox issues */
	ixgbe_sriov_reinit(adapter);

335 336 337
	err = pci_enable_sriov(dev, num_vfs);
	if (err) {
		e_dev_warn("Failed to enable PCI sriov: %d\n", err);
338
		return err;
339
	}
340
	ixgbe_get_vfs(adapter);
341 342

	return num_vfs;
343
#else
344
	return 0;
345
#endif
346 347 348 349 350 351
}

static int ixgbe_pci_sriov_disable(struct pci_dev *dev)
{
	struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
	int err;
352
#ifdef CONFIG_PCI_IOV
353
	u32 current_flags = adapter->flags;
354
#endif
355 356 357 358 359

	err = ixgbe_disable_sriov(adapter);

	/* Only reinit if no error and state changed */
#ifdef CONFIG_PCI_IOV
360
	if (!err && current_flags != adapter->flags)
361 362 363 364 365 366 367 368 369 370 371 372
		ixgbe_sriov_reinit(adapter);
#endif

	return err;
}

int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
{
	if (num_vfs == 0)
		return ixgbe_pci_sriov_disable(dev);
	else
		return ixgbe_pci_sriov_enable(dev, num_vfs);
G
Greg Rose 已提交
373 374
}

E
Emil Tantilov 已提交
375
static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
376
				   u32 *msgbuf, u32 vf)
G
Greg Rose 已提交
377
{
378 379 380
	int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
		       >> IXGBE_VT_MSGINFO_SHIFT;
	u16 *hash_list = (u16 *)&msgbuf[1];
G
Greg Rose 已提交
381
	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
382
	struct ixgbe_hw *hw = &adapter->hw;
G
Greg Rose 已提交
383
	int i;
384 385 386
	u32 vector_bit;
	u32 vector_reg;
	u32 mta_reg;
387
	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
G
Greg Rose 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

	/* 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++) {
404
		vfinfo->vf_mc_hashes[i] = hash_list[i];
G
Greg Rose 已提交
405 406
	}

407 408 409 410
	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));
J
Jacob Keller 已提交
411
		mta_reg |= BIT(vector_bit);
412 413
		IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
	}
414 415
	vmolr |= IXGBE_VMOLR_ROMPE;
	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
G
Greg Rose 已提交
416 417 418 419

	return 0;
}

420
#ifdef CONFIG_PCI_IOV
G
Greg Rose 已提交
421 422 423 424 425 426 427 428 429 430
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++) {
431
		u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(i));
G
Greg Rose 已提交
432 433 434 435 436 437
		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));
J
Jacob Keller 已提交
438
			mta_reg |= BIT(vector_bit);
G
Greg Rose 已提交
439 440
			IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
		}
441 442 443 444 445 446

		if (vfinfo->num_vf_mc_hashes)
			vmolr |= IXGBE_VMOLR_ROMPE;
		else
			vmolr &= ~IXGBE_VMOLR_ROMPE;
		IXGBE_WRITE_REG(hw, IXGBE_VMOLR(i), vmolr);
G
Greg Rose 已提交
447
	}
G
Greg Rose 已提交
448 449

	/* Restore any VF macvlans */
450
	ixgbe_full_sync_mac_table(adapter);
G
Greg Rose 已提交
451
}
452
#endif
G
Greg Rose 已提交
453

E
Emil Tantilov 已提交
454 455
static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
			     u32 vf)
G
Greg Rose 已提交
456
{
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	struct ixgbe_hw *hw = &adapter->hw;
	int err;

	/* If VLAN overlaps with one the PF is currently monitoring make
	 * sure that we are able to allocate a VLVF entry.  This may be
	 * redundant but it guarantees PF will maintain visibility to
	 * the VLAN.
	 */
	if (add && test_bit(vid, adapter->active_vlans)) {
		err = hw->mac.ops.set_vfta(hw, vid, VMDQ_P(0), true, false);
		if (err)
			return err;
	}

	err = hw->mac.ops.set_vfta(hw, vid, vf, !!add, false);

473 474 475 476 477 478 479 480 481 482 483
	if (add && !err)
		return err;

	/* If we failed to add the VF VLAN or we are removing the VF VLAN
	 * we may need to drop the PF pool bit in order to allow us to free
	 * up the VLVF resources.
	 */
	if (test_bit(vid, adapter->active_vlans) ||
	    (adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
		ixgbe_update_pf_promisc_vlvf(adapter, vid);

484
	return err;
G
Greg Rose 已提交
485 486
}

487
static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
488 489
{
	struct ixgbe_hw *hw = &adapter->hw;
490
	int max_frame = msgbuf[1];
491 492
	u32 max_frs;

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	/*
	 * For 82599EB we have to keep all PFs and VFs operating with
	 * the same max_frame value in order to avoid sending an oversize
	 * frame to a VF.  In order to guarantee this is handled correctly
	 * for all cases we have several special exceptions to take into
	 * account before we can enable the VF for receive
	 */
	if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
		struct net_device *dev = adapter->netdev;
		int pf_max_frame = dev->mtu + ETH_HLEN;
		u32 reg_offset, vf_shift, vfre;
		s32 err = 0;

#ifdef CONFIG_FCOE
		if (dev->features & NETIF_F_FCOE_MTU)
			pf_max_frame = max_t(int, pf_max_frame,
					     IXGBE_FCOE_JUMBO_FRAME_SIZE);

#endif /* CONFIG_FCOE */
512 513
		switch (adapter->vfinfo[vf].vf_api) {
		case ixgbe_mbox_api_11:
514
		case ixgbe_mbox_api_12:
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
			/*
			 * Version 1.1 supports jumbo frames on VFs if PF has
			 * jumbo frames enabled which means legacy VFs are
			 * disabled
			 */
			if (pf_max_frame > ETH_FRAME_LEN)
				break;
		default:
			/*
			 * If the PF or VF are running w/ jumbo frames enabled
			 * we need to shut down the VF Rx path as we cannot
			 * support jumbo frames on legacy VFs
			 */
			if ((pf_max_frame > ETH_FRAME_LEN) ||
			    (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
				err = -EINVAL;
			break;
		}
533 534 535 536 537 538 539 540

		/* determine VF receive enable location */
		vf_shift = vf % 32;
		reg_offset = vf / 32;

		/* enable or disable receive depending on error */
		vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
		if (err)
J
Jacob Keller 已提交
541
			vfre &= ~BIT(vf_shift);
542
		else
J
Jacob Keller 已提交
543
			vfre |= BIT(vf_shift);
544 545 546 547 548 549 550
		IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);

		if (err) {
			e_err(drv, "VF max_frame %d out of range\n", max_frame);
			return err;
		}
	}
551 552

	/* MTU < 68 is an error and causes problems on some kernels */
553 554 555
	if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
		e_err(drv, "VF max_frame %d out of range\n", max_frame);
		return -EINVAL;
556 557
	}

558 559 560 561 562 563 564
	/* pull current max frame size from hardware */
	max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
	max_frs &= IXGBE_MHADD_MFS_MASK;
	max_frs >>= IXGBE_MHADD_MFS_SHIFT;

	if (max_frs < max_frame) {
		max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
565 566 567
		IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
	}

568 569 570
	e_info(hw, "VF requests change max MTU to %d\n", max_frame);

	return 0;
571 572
}

E
Emil Tantilov 已提交
573
static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
G
Greg Rose 已提交
574 575
{
	u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
576
	vmolr |= IXGBE_VMOLR_BAM;
577 578 579 580
	if (aupe)
		vmolr |= IXGBE_VMOLR_AUPE;
	else
		vmolr &= ~IXGBE_VMOLR_AUPE;
G
Greg Rose 已提交
581 582 583
	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
}

584 585 586 587 588 589
static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf)
{
	struct ixgbe_hw *hw = &adapter->hw;

	IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
}
590 591 592 593

static void ixgbe_clear_vf_vlans(struct ixgbe_adapter *adapter, u32 vf)
{
	struct ixgbe_hw *hw = &adapter->hw;
594 595 596
	u32 vlvfb_mask, pool_mask, i;

	/* create mask for VF and other pools */
J
Jacob Keller 已提交
597 598
	pool_mask = ~BIT(VMDQ_P(0) % 32);
	vlvfb_mask = BIT(vf % 32);
599 600 601 602

	/* post increment loop, covers VLVF_ENTRIES - 1 to 0 */
	for (i = IXGBE_VLVF_ENTRIES; i--;) {
		u32 bits[2], vlvfb, vid, vfta, vlvf;
603
		u32 word = i * 2 + vf / 32;
604
		u32 mask;
605

606
		vlvfb = IXGBE_READ_REG(hw, IXGBE_VLVFB(word));
607 608

		/* if our bit isn't set we can skip it */
609
		if (!(vlvfb & vlvfb_mask))
610 611 612
			continue;

		/* clear our bit from vlvfb */
613
		vlvfb ^= vlvfb_mask;
614 615 616

		/* create 64b mask to chedk to see if we should clear VLVF */
		bits[word % 2] = vlvfb;
617
		bits[~word % 2] = IXGBE_READ_REG(hw, IXGBE_VLVFB(word ^ 1));
618 619

		/* if other pools are present, just remove ourselves */
620 621
		if (bits[(VMDQ_P(0) / 32) ^ 1] ||
		    (bits[VMDQ_P(0) / 32] & pool_mask))
622 623
			goto update_vlvfb;

624 625 626 627
		/* if PF is present, leave VFTA */
		if (bits[0] || bits[1])
			goto update_vlvf;

628 629 630 631 632 633
		/* if we cannot determine VLAN just remove ourselves */
		vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(i));
		if (!vlvf)
			goto update_vlvfb;

		vid = vlvf & VLAN_VID_MASK;
J
Jacob Keller 已提交
634
		mask = BIT(vid % 32);
635 636 637 638 639 640 641 642

		/* clear bit from VFTA */
		vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid / 32));
		if (vfta & mask)
			IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid / 32), vfta ^ mask);
update_vlvf:
		/* clear POOL selection enable */
		IXGBE_WRITE_REG(hw, IXGBE_VLVF(i), 0);
643 644 645

		if (!(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
			vlvfb = 0;
646 647 648 649 650 651
update_vlvfb:
		/* clear pool bits */
		IXGBE_WRITE_REG(hw, IXGBE_VLVFB(word), vlvfb);
	}
}

E
Emil Tantilov 已提交
652
static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
G
Greg Rose 已提交
653 654
{
	struct ixgbe_hw *hw = &adapter->hw;
655 656 657
	struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
	u8 num_tcs = netdev_get_num_tc(adapter->netdev);

658 659 660 661
	/* remove VLAN filters beloning to this VF */
	ixgbe_clear_vf_vlans(adapter, vf);

	/* add back PF assigned VLAN or VLAN 0 */
662
	ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf);
G
Greg Rose 已提交
663 664

	/* reset offloads to defaults */
665 666 667 668 669
	ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan);

	/* set outgoing tags for VFs */
	if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
		ixgbe_clear_vmvir(adapter, vf);
670
	} else {
671 672 673 674 675 676 677 678 679
		if (vfinfo->pf_qos || !num_tcs)
			ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
					vfinfo->pf_qos, vf);
		else
			ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
					adapter->default_up, vf);

		if (vfinfo->spoofchk_enabled)
			hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
680
	}
G
Greg Rose 已提交
681 682 683 684 685 686 687

	/* 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);

688
	ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
689 690 691

	/* reset VF api back to unknown */
	adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
G
Greg Rose 已提交
692 693
}

E
Emil Tantilov 已提交
694 695
static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
			    int vf, unsigned char *mac_addr)
G
Greg Rose 已提交
696
{
697
	ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
698
	memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
699
	ixgbe_add_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf);
G
Greg Rose 已提交
700 701 702 703

	return 0;
}

G
Greg Rose 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716
static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
				int vf, int index, unsigned char *mac_addr)
{
	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;
717 718
				ixgbe_del_mac_filter(adapter,
						     entry->vf_macvlan, vf);
G
Greg Rose 已提交
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
			}
		}
	}

	/*
	 * 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);

754
	ixgbe_add_mac_filter(adapter, mac_addr, vf);
G
Greg Rose 已提交
755 756 757 758

	return 0;
}

G
Greg Rose 已提交
759 760
int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
{
761
	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
G
Greg Rose 已提交
762 763 764 765
	unsigned int vfn = (event_mask & 0x3f);

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

766 767
	if (enable)
		eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses);
G
Greg Rose 已提交
768 769 770 771

	return 0;
}

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
static inline void ixgbe_write_qde(struct ixgbe_adapter *adapter, u32 vf,
				   u32 qde)
{
	struct ixgbe_hw *hw = &adapter->hw;
	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
	int i;

	for (i = vf * q_per_pool; i < ((vf + 1) * q_per_pool); i++) {
		u32 reg;

		/* flush previous write */
		IXGBE_WRITE_FLUSH(hw);

		/* indicate to hardware that we want to set drop enable */
		reg = IXGBE_QDE_WRITE | IXGBE_QDE_ENABLE;
		reg |= i <<  IXGBE_QDE_IDX_SHIFT;
		IXGBE_WRITE_REG(hw, IXGBE_QDE, reg);
	}
}

793
static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
G
Greg Rose 已提交
794
{
795
	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
G
Greg Rose 已提交
796
	struct ixgbe_hw *hw = &adapter->hw;
797
	unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
798 799
	u32 reg, reg_offset, vf_shift;
	u32 msgbuf[4] = {0, 0, 0, 0};
800
	u8 *addr = (u8 *)(&msgbuf[1]);
801 802
	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
	int i;
803 804 805 806 807 808 809

	e_info(probe, "VF Reset msg received from vf %d\n", vf);

	/* reset the filters for the device */
	ixgbe_vf_reset_event(adapter, vf);

	/* set vf mac address */
810 811
	if (!is_zero_ether_addr(vf_mac))
		ixgbe_set_vf_mac(adapter, vf, vf_mac);
G
Greg Rose 已提交
812 813 814 815

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

816
	/* enable transmit for vf */
G
Greg Rose 已提交
817
	reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
J
Jacob Keller 已提交
818
	reg |= BIT(vf_shift);
G
Greg Rose 已提交
819 820
	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);

821
	/* force drop enable for all VF Rx queues */
822
	ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE);
823

824
	/* enable receive for vf */
G
Greg Rose 已提交
825
	reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
J
Jacob Keller 已提交
826
	reg |= BIT(vf_shift);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
	/*
	 * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
	 * For more info take a look at ixgbe_set_vf_lpe
	 */
	if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
		struct net_device *dev = adapter->netdev;
		int pf_max_frame = dev->mtu + ETH_HLEN;

#ifdef CONFIG_FCOE
		if (dev->features & NETIF_F_FCOE_MTU)
			pf_max_frame = max_t(int, pf_max_frame,
					     IXGBE_FCOE_JUMBO_FRAME_SIZE);

#endif /* CONFIG_FCOE */
		if (pf_max_frame > ETH_FRAME_LEN)
J
Jacob Keller 已提交
842
			reg &= ~BIT(vf_shift);
843
	}
G
Greg Rose 已提交
844 845
	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);

846 847 848
	/* enable VF mailbox for further messages */
	adapter->vfinfo[vf].clear_to_send = true;

849 850
	/* Enable counting of spoofed packets in the SSVPC register */
	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
J
Jacob Keller 已提交
851
	reg |= BIT(vf_shift);
852 853
	IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);

854 855 856 857 858 859 860 861 862
	/*
	 * Reset the VFs TDWBAL and TDWBAH registers
	 * which are not cleared by an FLR
	 */
	for (i = 0; i < q_per_pool; i++) {
		IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBAHn(q_per_pool, vf, i), 0);
		IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBALn(q_per_pool, vf, i), 0);
	}

863
	/* reply to reset with ack and vf mac address */
864 865 866 867 868 869 870 871 872 873
	msgbuf[0] = IXGBE_VF_RESET;
	if (!is_zero_ether_addr(vf_mac)) {
		msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
		memcpy(addr, vf_mac, ETH_ALEN);
	} else {
		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
		dev_warn(&adapter->pdev->dev,
			 "VF %d has no MAC address assigned, you may have to assign one manually\n",
			 vf);
	}
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

	/*
	 * 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 0;
}

static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
				 u32 *msgbuf, u32 vf)
{
	u8 *new_mac = ((u8 *)(&msgbuf[1]));

	if (!is_valid_ether_addr(new_mac)) {
		e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
		return -1;
	}

895
	if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted &&
896
	    !ether_addr_equal(adapter->vfinfo[vf].vf_mac_addresses, new_mac)) {
897 898 899 900 901 902 903
		e_warn(drv,
		       "VF %d attempted to override administratively set MAC address\n"
		       "Reload the VF driver to resume operations\n",
		       vf);
		return -1;
	}

904
	return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0;
905 906 907 908 909
}

static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
				 u32 *msgbuf, u32 vf)
{
910 911 912
	u32 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
	u32 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
	u8 tcs = netdev_get_num_tc(adapter->netdev);
913

914
	if (adapter->vfinfo[vf].pf_vlan || tcs) {
915 916 917 918 919 920 921
		e_warn(drv,
		       "VF %d attempted to override administratively set VLAN configuration\n"
		       "Reload the VF driver to resume operations\n",
		       vf);
		return -1;
	}

922 923 924 925
	/* VLAN 0 is a special case, don't allow it to be removed */
	if (!vid && !add)
		return 0;

926
	return ixgbe_set_vf_vlan(adapter, add, vid, vf);
927 928 929 930 931 932 933 934 935 936
}

static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
				    u32 *msgbuf, u32 vf)
{
	u8 *new_mac = ((u8 *)(&msgbuf[1]));
	int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
		    IXGBE_VT_MSGINFO_SHIFT;
	int err;

937 938
	if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted &&
	    index > 0) {
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
		e_warn(drv,
		       "VF %d requested MACVLAN filter but is administratively denied\n",
		       vf);
		return -1;
	}

	/* An non-zero index indicates the VF is setting a filter */
	if (index) {
		if (!is_valid_ether_addr(new_mac)) {
			e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
			return -1;
		}

		/*
		 * If the VF is allowed to set MAC filters then turn off
		 * anti-spoofing to avoid false positives.
		 */
956 957 958 959
		if (adapter->vfinfo[vf].spoofchk_enabled) {
			struct ixgbe_hw *hw = &adapter->hw;

			hw->mac.ops.set_mac_anti_spoofing(hw, false, vf);
960
			hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
961
		}
962 963 964 965 966 967 968
	}

	err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
	if (err == -ENOSPC)
		e_warn(drv,
		       "VF %d has requested a MACVLAN filter but there is no space for it\n",
		       vf);
969 970

	return err < 0;
G
Greg Rose 已提交
971 972
}

973 974 975 976 977 978 979
static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
				  u32 *msgbuf, u32 vf)
{
	int api = msgbuf[1];

	switch (api) {
	case ixgbe_mbox_api_10:
980
	case ixgbe_mbox_api_11:
981
	case ixgbe_mbox_api_12:
982 983 984 985 986 987 988 989 990 991 992
		adapter->vfinfo[vf].vf_api = api;
		return 0;
	default:
		break;
	}

	e_info(drv, "VF %d requested invalid api version %u\n", vf, api);

	return -1;
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004
static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter,
			       u32 *msgbuf, u32 vf)
{
	struct net_device *dev = adapter->netdev;
	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
	unsigned int default_tc = 0;
	u8 num_tcs = netdev_get_num_tc(dev);

	/* verify the PF is supporting the correct APIs */
	switch (adapter->vfinfo[vf].vf_api) {
	case ixgbe_mbox_api_20:
	case ixgbe_mbox_api_11:
1005
	case ixgbe_mbox_api_12:
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
		break;
	default:
		return -1;
	}

	/* only allow 1 Tx queue for bandwidth limiting */
	msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
	msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);

	/* if TCs > 1 determine which TC belongs to default user priority */
	if (num_tcs > 1)
		default_tc = netdev_get_prio_tc_map(dev, adapter->default_up);

	/* notify VF of need for VLAN tag stripping, and correct queue */
	if (num_tcs)
		msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
	else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos)
		msgbuf[IXGBE_VF_TRANS_VLAN] = 1;
	else
		msgbuf[IXGBE_VF_TRANS_VLAN] = 0;

	/* notify VF of default queue */
	msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc;

	return 0;
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
static int ixgbe_get_vf_reta(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
{
	u32 i, j;
	u32 *out_buf = &msgbuf[1];
	const u8 *reta = adapter->rss_indir_tbl;
	u32 reta_size = ixgbe_rss_indir_tbl_entries(adapter);

	/* Check if operation is permitted */
	if (!adapter->vfinfo[vf].rss_query_enabled)
		return -EPERM;

	/* verify the PF is supporting the correct API */
	if (adapter->vfinfo[vf].vf_api != ixgbe_mbox_api_12)
		return -EOPNOTSUPP;

	/* This mailbox command is supported (required) only for 82599 and x540
	 * VFs which support up to 4 RSS queues. Therefore we will compress the
	 * RETA by saving only 2 bits from each entry. This way we will be able
	 * to transfer the whole RETA in a single mailbox operation.
	 */
	for (i = 0; i < reta_size / 16; i++) {
		out_buf[i] = 0;
		for (j = 0; j < 16; j++)
			out_buf[i] |= (u32)(reta[16 * i + j] & 0x3) << (2 * j);
	}

	return 0;
}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
static int ixgbe_get_vf_rss_key(struct ixgbe_adapter *adapter,
				u32 *msgbuf, u32 vf)
{
	u32 *rss_key = &msgbuf[1];

	/* Check if the operation is permitted */
	if (!adapter->vfinfo[vf].rss_query_enabled)
		return -EPERM;

	/* verify the PF is supporting the correct API */
	if (adapter->vfinfo[vf].vf_api != ixgbe_mbox_api_12)
		return -EOPNOTSUPP;

	memcpy(rss_key, adapter->rss_key, sizeof(adapter->rss_key));

	return 0;
}

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
static int ixgbe_update_vf_xcast_mode(struct ixgbe_adapter *adapter,
				      u32 *msgbuf, u32 vf)
{
	struct ixgbe_hw *hw = &adapter->hw;
	int xcast_mode = msgbuf[1];
	u32 vmolr, disable, enable;

	/* verify the PF is supporting the correct APIs */
	switch (adapter->vfinfo[vf].vf_api) {
	case ixgbe_mbox_api_12:
		break;
	default:
		return -EOPNOTSUPP;
	}

	if (xcast_mode > IXGBEVF_XCAST_MODE_MULTI &&
	    !adapter->vfinfo[vf].trusted) {
		xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
	}

	if (adapter->vfinfo[vf].xcast_mode == xcast_mode)
		goto out;

	switch (xcast_mode) {
	case IXGBEVF_XCAST_MODE_NONE:
		disable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
		enable = 0;
		break;
	case IXGBEVF_XCAST_MODE_MULTI:
		disable = IXGBE_VMOLR_MPE;
		enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE;
		break;
	case IXGBEVF_XCAST_MODE_ALLMULTI:
		disable = 0;
		enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
		break;
	default:
		return -EOPNOTSUPP;
	}

	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
	vmolr &= ~disable;
	vmolr |= enable;
	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);

	adapter->vfinfo[vf].xcast_mode = xcast_mode;

out:
	msgbuf[1] = xcast_mode;

	return 0;
}

G
Greg Rose 已提交
1133 1134 1135
static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
{
	u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
E
Emil Tantilov 已提交
1136
	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
G
Greg Rose 已提交
1137 1138 1139 1140 1141
	struct ixgbe_hw *hw = &adapter->hw;
	s32 retval;

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

1142
	if (retval) {
1143
		pr_err("Error receiving message from VF\n");
1144 1145
		return retval;
	}
G
Greg Rose 已提交
1146 1147 1148

	/* this is a message we already processed, do nothing */
	if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
1149
		return 0;
G
Greg Rose 已提交
1150

1151 1152 1153
	/* flush the ack before we write any messages back */
	IXGBE_WRITE_FLUSH(hw);

1154 1155 1156
	if (msgbuf[0] == IXGBE_VF_RESET)
		return ixgbe_vf_reset_msg(adapter, vf);

G
Greg Rose 已提交
1157 1158 1159 1160 1161 1162 1163
	/*
	 * until the vf completes a virtual function reset it should not be
	 * allowed to start any configuration.
	 */
	if (!adapter->vfinfo[vf].clear_to_send) {
		msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
		ixgbe_write_mbx(hw, msgbuf, 1, vf);
1164
		return 0;
G
Greg Rose 已提交
1165 1166 1167 1168
	}

	switch ((msgbuf[0] & 0xFFFF)) {
	case IXGBE_VF_SET_MAC_ADDR:
1169
		retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
G
Greg Rose 已提交
1170 1171
		break;
	case IXGBE_VF_SET_MULTICAST:
1172 1173 1174 1175
		retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
		break;
	case IXGBE_VF_SET_VLAN:
		retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
G
Greg Rose 已提交
1176 1177
		break;
	case IXGBE_VF_SET_LPE:
1178
		retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf);
G
Greg Rose 已提交
1179
		break;
G
Greg Rose 已提交
1180
	case IXGBE_VF_SET_MACVLAN:
1181
		retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
G
Greg Rose 已提交
1182
		break;
1183 1184 1185
	case IXGBE_VF_API_NEGOTIATE:
		retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
		break;
1186 1187 1188
	case IXGBE_VF_GET_QUEUES:
		retval = ixgbe_get_vf_queues(adapter, msgbuf, vf);
		break;
1189 1190 1191
	case IXGBE_VF_GET_RETA:
		retval = ixgbe_get_vf_reta(adapter, msgbuf, vf);
		break;
1192 1193 1194
	case IXGBE_VF_GET_RSS_KEY:
		retval = ixgbe_get_vf_rss_key(adapter, msgbuf, vf);
		break;
1195 1196 1197
	case IXGBE_VF_UPDATE_XCAST_MODE:
		retval = ixgbe_update_vf_xcast_mode(adapter, msgbuf, vf);
		break;
G
Greg Rose 已提交
1198
	default:
1199
		e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
G
Greg Rose 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		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;

1212
	ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
G
Greg Rose 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

	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);
	}
}

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
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);
}

H
Hiroshi Shimamoto 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static inline void ixgbe_ping_vf(struct ixgbe_adapter *adapter, int vf)
{
	struct ixgbe_hw *hw = &adapter->hw;
	u32 ping;

	ping = IXGBE_PF_CONTROL_MSG;
	if (adapter->vfinfo[vf].clear_to_send)
		ping |= IXGBE_VT_MSGTYPE_CTS;
	ixgbe_write_mbx(hw, &ping, 1, vf);
}

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
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);
	}
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
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);
}

1302 1303 1304 1305
static int ixgbe_enable_port_vlan(struct ixgbe_adapter *adapter, int vf,
				  u16 vlan, u8 qos)
{
	struct ixgbe_hw *hw = &adapter->hw;
E
Emil Tantilov 已提交
1306
	int err;
1307

E
Emil Tantilov 已提交
1308
	err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
1309 1310
	if (err)
		goto out;
E
Emil Tantilov 已提交
1311

1312 1313 1314
	/* Revoke tagless access via VLAN 0 */
	ixgbe_set_vf_vlan(adapter, false, 0, vf);

1315 1316
	ixgbe_set_vmvir(adapter, vlan, qos, vf);
	ixgbe_set_vmolr(hw, vf, false);
1317 1318 1319 1320 1321 1322

	/* enable hide vlan on X550 */
	if (hw->mac.type >= ixgbe_mac_X550)
		ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE |
				IXGBE_QDE_HIDE_VLAN);

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	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");
	}

out:
	return err;
}

static int ixgbe_disable_port_vlan(struct ixgbe_adapter *adapter, int vf)
{
	struct ixgbe_hw *hw = &adapter->hw;
	int err;

	err = ixgbe_set_vf_vlan(adapter, false,
				adapter->vfinfo[vf].pf_vlan, vf);
1345 1346
	/* Restore tagless access via VLAN 0 */
	ixgbe_set_vf_vlan(adapter, true, 0, vf);
1347 1348
	ixgbe_clear_vmvir(adapter, vf);
	ixgbe_set_vmolr(hw, vf, true);
E
Emil Tantilov 已提交
1349 1350 1351 1352 1353

	/* disable hide VLAN on X550 */
	if (hw->mac.type >= ixgbe_mac_X550)
		ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE);

1354 1355 1356 1357 1358 1359
	adapter->vfinfo[vf].pf_vlan = 0;
	adapter->vfinfo[vf].pf_qos = 0;

	return err;
}

1360 1361
int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
			  u8 qos, __be16 vlan_proto)
1362 1363 1364 1365 1366 1367
{
	int err = 0;
	struct ixgbe_adapter *adapter = netdev_priv(netdev);

	if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
		return -EINVAL;
1368 1369
	if (vlan_proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;
1370
	if (vlan || qos) {
1371 1372 1373 1374 1375 1376 1377
		/* Check if there is already a port VLAN set, if so
		 * we have to delete the old one first before we
		 * can set the new one.  The usage model had
		 * previously assumed the user would delete the
		 * old port VLAN before setting a new one but this
		 * is not necessarily the case.
		 */
1378
		if (adapter->vfinfo[vf].pf_vlan)
1379
			err = ixgbe_disable_port_vlan(adapter, vf);
1380 1381
		if (err)
			goto out;
1382
		err = ixgbe_enable_port_vlan(adapter, vf, vlan, qos);
1383
	} else {
1384
		err = ixgbe_disable_port_vlan(adapter, vf);
1385
	}
1386

1387
out:
1388
	return err;
1389 1390
}

1391
int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
1392
{
1393
	switch (adapter->link_speed) {
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	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;
	}
}

1405
static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
1406
{
1407 1408 1409 1410 1411 1412 1413 1414 1415
	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
	struct ixgbe_hw *hw = &adapter->hw;
	u32 bcnrc_val = 0;
	u16 queue, queues_per_pool;
	u16 tx_rate = adapter->vfinfo[vf].tx_rate;

	if (tx_rate) {
		/* start with base link speed value */
		bcnrc_val = adapter->vf_rate_link_speed;
1416 1417

		/* Calculate the rate factor values to set */
1418 1419 1420 1421 1422 1423 1424 1425 1426
		bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
		bcnrc_val /= tx_rate;

		/* clear everything but the rate factor */
		bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
			     IXGBE_RTTBCNRC_RF_DEC_MASK;

		/* enable the rate scheduler */
		bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
1427 1428
	}

L
Lior Levy 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	/*
	 * 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;
	}

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	/* determine how many queues per pool based on VMDq mask */
	queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);

	/* write value for all Tx queues belonging to VF */
	for (queue = 0; queue < queues_per_pool; queue++) {
		unsigned int reg_idx = (vf * queues_per_pool) + queue;

		IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
		IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
	}
1455 1456 1457 1458
}

void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
{
1459
	int i;
1460 1461

	/* VF Tx rate limit was not set */
1462
	if (!adapter->vf_rate_link_speed)
1463 1464
		return;

1465
	if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
1466 1467
		adapter->vf_rate_link_speed = 0;
		dev_info(&adapter->pdev->dev,
1468
			 "Link speed has been changed. VF Transmit rate is disabled\n");
1469 1470 1471
	}

	for (i = 0; i < adapter->num_vfs; i++) {
1472
		if (!adapter->vf_rate_link_speed)
1473 1474
			adapter->vfinfo[i].tx_rate = 0;

1475
		ixgbe_set_vf_rate_limit(adapter, i);
1476 1477 1478
	}
}

1479 1480
int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int min_tx_rate,
			int max_tx_rate)
1481
{
1482
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	int link_speed;

	/* verify VF is active */
	if (vf >= adapter->num_vfs)
		return -EINVAL;

	/* verify link is up */
	if (!adapter->link_up)
		return -EINVAL;

	/* verify we are linked at 10Gbps */
	link_speed = ixgbe_link_mbps(adapter);
	if (link_speed != 10000)
		return -EINVAL;
1497

1498 1499 1500
	if (min_tx_rate)
		return -EINVAL;

1501
	/* rate limit cannot be less than 10Mbs or greater than link speed */
1502
	if (max_tx_rate && ((max_tx_rate <= 10) || (max_tx_rate > link_speed)))
1503 1504
		return -EINVAL;

1505 1506
	/* store values */
	adapter->vf_rate_link_speed = link_speed;
1507
	adapter->vfinfo[vf].tx_rate = max_tx_rate;
1508 1509 1510

	/* update hardware configuration */
	ixgbe_set_vf_rate_limit(adapter, vf);
1511 1512

	return 0;
1513 1514
}

1515 1516 1517 1518 1519
int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);
	struct ixgbe_hw *hw = &adapter->hw;

1520 1521 1522
	if (vf >= adapter->num_vfs)
		return -EINVAL;

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

1525 1526 1527 1528
	/* configure MAC spoofing */
	hw->mac.ops.set_mac_anti_spoofing(hw, setting, vf);

	/* configure VLAN spoofing */
1529
	hw->mac.ops.set_vlan_anti_spoofing(hw, setting, vf);
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

	/* Ensure LLDP and FC is set for Ethertype Antispoofing if we will be
	 * calling set_ethertype_anti_spoofing for each VF in loop below
	 */
	if (hw->mac.ops.set_ethertype_anti_spoofing) {
		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_LLDP),
				(IXGBE_ETQF_FILTER_EN    |
				 IXGBE_ETQF_TX_ANTISPOOF |
				 IXGBE_ETH_P_LLDP));

		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FC),
				(IXGBE_ETQF_FILTER_EN |
				 IXGBE_ETQF_TX_ANTISPOOF |
				 ETH_P_PAUSE));

		hw->mac.ops.set_ethertype_anti_spoofing(hw, setting, vf);
1546 1547 1548 1549 1550
	}

	return 0;
}

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
int ixgbe_ndo_set_vf_rss_query_en(struct net_device *netdev, int vf,
				  bool setting)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);

	/* This operation is currently supported only for 82599 and x540
	 * devices.
	 */
	if (adapter->hw.mac.type < ixgbe_mac_82599EB ||
	    adapter->hw.mac.type >= ixgbe_mac_X550)
		return -EOPNOTSUPP;

	if (vf >= adapter->num_vfs)
		return -EINVAL;

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

	return 0;
}

H
Hiroshi Shimamoto 已提交
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
int ixgbe_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
{
	struct ixgbe_adapter *adapter = netdev_priv(netdev);

	if (vf >= adapter->num_vfs)
		return -EINVAL;

	/* nothing to do */
	if (adapter->vfinfo[vf].trusted == setting)
		return 0;

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

	/* reset VF to reconfigure features */
	adapter->vfinfo[vf].clear_to_send = false;
	ixgbe_ping_vf(adapter, vf);

	e_info(drv, "VF %u is %strusted\n", vf, setting ? "" : "not ");

	return 0;
}

1593 1594 1595 1596 1597 1598 1599 1600
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);
1601 1602
	ivi->max_tx_rate = adapter->vfinfo[vf].tx_rate;
	ivi->min_tx_rate = 0;
1603 1604
	ivi->vlan = adapter->vfinfo[vf].pf_vlan;
	ivi->qos = adapter->vfinfo[vf].pf_qos;
1605
	ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
1606
	ivi->rss_query_en = adapter->vfinfo[vf].rss_query_enabled;
H
Hiroshi Shimamoto 已提交
1607
	ivi->trusted = adapter->vfinfo[vf].trusted;
1608 1609
	return 0;
}