ice_sriov.c 50.0 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2018, Intel Corporation. */

#include "ice.h"
5
#include "ice_vf_lib_private.h"
A
Anirudh Venkataramanan 已提交
6
#include "ice_base.h"
7
#include "ice_lib.h"
8
#include "ice_fltr.h"
9
#include "ice_dcb_lib.h"
Q
Qi Zhang 已提交
10
#include "ice_flow.h"
11
#include "ice_eswitch.h"
12
#include "ice_virtchnl_allowlist.h"
J
Jeff Guo 已提交
13
#include "ice_flex_pipe.h"
14
#include "ice_vf_vsi_vlan_ops.h"
15
#include "ice_vlan.h"
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
/**
 * ice_free_vf_entries - Free all VF entries from the hash table
 * @pf: pointer to the PF structure
 *
 * Iterate over the VF hash table, removing and releasing all VF entries.
 * Called during VF teardown or as cleanup during failed VF initialization.
 */
static void ice_free_vf_entries(struct ice_pf *pf)
{
	struct ice_vfs *vfs = &pf->vfs;
	struct hlist_node *tmp;
	struct ice_vf *vf;
	unsigned int bkt;

	/* Remove all VFs from the hash table and release their main
	 * reference. Once all references to the VF are dropped, ice_put_vf()
	 * will call ice_release_vf which will remove the VF memory.
	 */
	lockdep_assert_held(&vfs->table_lock);

	hash_for_each_safe(vfs->table, bkt, tmp, vf, entry) {
		hash_del_rcu(&vf->entry);
		ice_put_vf(vf);
	}
}

43 44 45 46 47 48
/**
 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
 * @vf: invalidate this VF's VSI after freeing it
 */
static void ice_vf_vsi_release(struct ice_vf *vf)
{
49 50 51 52 53 54
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);

	if (WARN_ON(!vsi))
		return;

	ice_vsi_release(vsi);
55 56 57
	ice_vf_invalidate_vsi(vf);
}

58 59 60 61 62 63 64
/**
 * ice_free_vf_res - Free a VF's resources
 * @vf: pointer to the VF info
 */
static void ice_free_vf_res(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
65
	int i, last_vector_idx;
66 67 68 69 70

	/* First, disable VF's configuration API to prevent OS from
	 * accessing the VF's VSI after it's freed or invalidated.
	 */
	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
Q
Qi Zhang 已提交
71
	ice_vf_fdir_exit(vf);
72 73 74
	/* free VF control VSI */
	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
		ice_vf_ctrl_vsi_release(vf);
75

76
	/* free VSI and disconnect it from the parent uplink */
77 78
	if (vf->lan_vsi_idx != ICE_NO_VSI) {
		ice_vf_vsi_release(vf);
79 80 81
		vf->num_mac = 0;
	}

82
	last_vector_idx = vf->first_vector_idx + pf->vfs.num_msix_per - 1;
83 84 85 86 87

	/* clear VF MDD event information */
	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));

88
	/* Disable interrupts so that VF starts in a known state */
89 90
	for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
		wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		ice_flush(&pf->hw);
	}
	/* reset some of the state variables keeping track of the resources */
	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
}

/**
 * ice_dis_vf_mappings
 * @vf: pointer to the VF structure
 */
static void ice_dis_vf_mappings(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;
B
Brett Creeley 已提交
106
	struct device *dev;
107 108 109 110
	int first, last, v;
	struct ice_hw *hw;

	hw = &pf->hw;
111
	vsi = ice_get_vf_vsi(vf);
112 113
	if (WARN_ON(!vsi))
		return;
114

B
Brett Creeley 已提交
115
	dev = ice_pf_to_dev(pf);
116
	wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
117
	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
118

B
Brett Creeley 已提交
119
	first = vf->first_vector_idx;
120
	last = first + pf->vfs.num_msix_per - 1;
121 122 123 124 125 126 127 128 129 130 131 132 133
	for (v = first; v <= last; v++) {
		u32 reg;

		reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
			GLINT_VECT2FUNC_IS_PF_M) |
		       ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
			GLINT_VECT2FUNC_PF_NUM_M));
		wr32(hw, GLINT_VECT2FUNC(v), reg);
	}

	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
	else
B
Brett Creeley 已提交
134
		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
135 136 137 138

	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
	else
139
		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
140 141
}

B
Brett Creeley 已提交
142 143 144 145
/**
 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
 * @pf: pointer to the PF structure
 *
M
Mitch Williams 已提交
146
 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
B
Brett Creeley 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 * the pf->sriov_base_vector.
 *
 * Returns 0 on success, and -EINVAL on error.
 */
static int ice_sriov_free_msix_res(struct ice_pf *pf)
{
	struct ice_res_tracker *res;

	if (!pf)
		return -EINVAL;

	res = pf->irq_tracker;
	if (!res)
		return -EINVAL;

	/* give back irq_tracker resources used */
M
Mitch Williams 已提交
163
	WARN_ON(pf->sriov_base_vector < res->num_entries);
B
Brett Creeley 已提交
164 165 166 167 168 169

	pf->sriov_base_vector = 0;

	return 0;
}

170 171 172 173 174 175
/**
 * ice_free_vfs - Free all VFs
 * @pf: pointer to the PF structure
 */
void ice_free_vfs(struct ice_pf *pf)
{
B
Brett Creeley 已提交
176
	struct device *dev = ice_pf_to_dev(pf);
177
	struct ice_vfs *vfs = &pf->vfs;
178
	struct ice_hw *hw = &pf->hw;
179 180
	struct ice_vf *vf;
	unsigned int bkt;
181

182
	if (!ice_has_vfs(pf))
183 184
		return;

185
	while (test_and_set_bit(ICE_VF_DIS, pf->state))
186 187
		usleep_range(1000, 2000);

188 189 190 191 192 193 194
	/* Disable IOV before freeing resources. This lets any VF drivers
	 * running in the host get themselves cleaned up before we yank
	 * the carpet out from underneath their feet.
	 */
	if (!pci_vfs_assigned(pf->pdev))
		pci_disable_sriov(pf->pdev);
	else
B
Brett Creeley 已提交
195
		dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
196

197 198 199 200
	mutex_lock(&vfs->table_lock);

	ice_eswitch_release(pf);

201
	ice_for_each_vf(pf, bkt, vf) {
202 203 204 205 206
		mutex_lock(&vf->cfg_lock);

		ice_dis_vf_qs(vf);

		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
207
			/* disable VF qp mappings and set VF disable state */
208 209 210
			ice_dis_vf_mappings(vf);
			set_bit(ICE_VF_STATE_DIS, vf->vf_states);
			ice_free_vf_res(vf);
211
		}
212

213 214 215 216 217 218 219 220
		if (!pci_vfs_assigned(pf->pdev)) {
			u32 reg_idx, bit_idx;

			reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
			bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
			wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
		}

221
		/* clear malicious info since the VF is getting released */
222
		if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
223
					ICE_MAX_SRIOV_VFS, vf->vf_id))
224 225 226
			dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
				vf->vf_id);

227
		mutex_unlock(&vf->cfg_lock);
228 229
	}

B
Brett Creeley 已提交
230
	if (ice_sriov_free_msix_res(pf))
B
Brett Creeley 已提交
231
		dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
B
Brett Creeley 已提交
232

233
	vfs->num_qps_per = 0;
234 235 236
	ice_free_vf_entries(pf);

	mutex_unlock(&vfs->table_lock);
237

238
	clear_bit(ICE_VF_DIS, pf->state);
239 240 241 242 243
	clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
}

/**
 * ice_vf_vsi_setup - Set up a VF VSI
244
 * @vf: VF to setup VSI for
245 246 247 248
 *
 * Returns pointer to the successfully allocated VSI struct on success,
 * otherwise returns NULL on failure.
 */
249
static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
250
{
251 252 253 254
	struct ice_port_info *pi = ice_vf_get_port_info(vf);
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;

255
	vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf, NULL);
256 257 258 259 260 261 262 263 264 265 266

	if (!vsi) {
		dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
		ice_vf_invalidate_vsi(vf);
		return NULL;
	}

	vf->lan_vsi_idx = vsi->idx;
	vf->lan_vsi_num = vsi->vsi_num;

	return vsi;
267 268
}

B
Brett Creeley 已提交
269
/**
270
 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
B
Brett Creeley 已提交
271 272 273
 * @pf: pointer to PF structure
 * @vf: pointer to VF that the first MSIX vector index is being calculated for
 *
274 275 276 277
 * This returns the first MSIX vector index in PF space that is used by this VF.
 * This index is used when accessing PF relative registers such as
 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
 * This will always be the OICR index in the AVF driver so any functionality
B
Brett Creeley 已提交
278 279 280 281 282
 * using vf->first_vector_idx for queue configuration will have to increment by
 * 1 to avoid meddling with the OICR index.
 */
static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
{
283
	return pf->sriov_base_vector + vf->vf_id * pf->vfs.num_msix_per;
B
Brett Creeley 已提交
284 285
}

286
/**
287 288
 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
 * @vf: VF to enable MSIX mappings for
289
 *
290 291 292
 * Some of the registers need to be indexed/configured using hardware global
 * device values and other registers need 0-based values, which represent PF
 * based values.
293
 */
294
static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
295
{
296 297
	int device_based_first_msix, device_based_last_msix;
	int pf_based_first_msix, pf_based_last_msix, v;
298
	struct ice_pf *pf = vf->pf;
299
	int device_based_vf_id;
300 301 302 303
	struct ice_hw *hw;
	u32 reg;

	hw = &pf->hw;
304
	pf_based_first_msix = vf->first_vector_idx;
305
	pf_based_last_msix = (pf_based_first_msix + pf->vfs.num_msix_per) - 1;
306 307 308 309

	device_based_first_msix = pf_based_first_msix +
		pf->hw.func_caps.common_cap.msix_vector_first_id;
	device_based_last_msix =
310
		(device_based_first_msix + pf->vfs.num_msix_per) - 1;
311 312 313 314 315 316
	device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;

	reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
		VPINT_ALLOC_FIRST_M) |
	       ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
		VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
317 318
	wr32(hw, VPINT_ALLOC(vf->vf_id), reg);

319
	reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
320
		 & VPINT_ALLOC_PCI_FIRST_M) |
321 322
	       ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
		VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
323
	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
324

325
	/* map the interrupts to its functions */
326 327
	for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
		reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
328 329 330 331 332 333
			GLINT_VECT2FUNC_VF_NUM_M) |
		       ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
			GLINT_VECT2FUNC_PF_NUM_M));
		wr32(hw, GLINT_VECT2FUNC(v), reg);
	}

334 335 336 337 338 339 340 341 342 343 344 345 346
	/* Map mailbox interrupt to VF MSI-X vector 0 */
	wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
}

/**
 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
 * @vf: VF to enable the mappings for
 * @max_txq: max Tx queues allowed on the VF's VSI
 * @max_rxq: max Rx queues allowed on the VF's VSI
 */
static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
{
	struct device *dev = ice_pf_to_dev(vf->pf);
347
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
348 349 350
	struct ice_hw *hw = &vf->pf->hw;
	u32 reg;

351 352 353
	if (WARN_ON(!vsi))
		return;

354 355 356
	/* set regardless of mapping mode */
	wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);

357 358 359 360 361 362 363 364
	/* VF Tx queues allocation */
	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
		/* set the VF PF Tx queue range
		 * VFNUMQ value should be set to (number of queues - 1). A value
		 * of 0 means 1 queue and a value of 255 means 256 queues
		 */
		reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
			VPLAN_TX_QBASE_VFFIRSTQ_M) |
365
		       (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
366 367 368
			VPLAN_TX_QBASE_VFNUMQ_M));
		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
	} else {
B
Brett Creeley 已提交
369
		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
370 371
	}

372 373 374
	/* set regardless of mapping mode */
	wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);

375 376 377 378 379 380 381 382
	/* VF Rx queues allocation */
	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
		/* set the VF PF Rx queue range
		 * VFNUMQ value should be set to (number of queues - 1). A value
		 * of 0 means 1 queue and a value of 255 means 256 queues
		 */
		reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
			VPLAN_RX_QBASE_VFFIRSTQ_M) |
383
		       (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
384 385 386
			VPLAN_RX_QBASE_VFNUMQ_M));
		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
	} else {
B
Brett Creeley 已提交
387
		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
388 389 390
	}
}

391 392 393 394 395 396
/**
 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
 * @vf: pointer to the VF structure
 */
static void ice_ena_vf_mappings(struct ice_vf *vf)
{
397
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
398

399 400 401
	if (WARN_ON(!vsi))
		return;

402 403 404 405
	ice_ena_vf_msix_mappings(vf);
	ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
}

B
Brett Creeley 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/**
 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
 * @vf: VF to calculate the register index for
 * @q_vector: a q_vector associated to the VF
 */
int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
{
	struct ice_pf *pf;

	if (!vf || !q_vector)
		return -EINVAL;

	pf = vf->pf;

	/* always add one to account for the OICR being the first MSIX */
421
	return pf->sriov_base_vector + pf->vfs.num_msix_per * vf->vf_id +
B
Brett Creeley 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		q_vector->v_idx + 1;
}

/**
 * ice_get_max_valid_res_idx - Get the max valid resource index
 * @res: pointer to the resource to find the max valid index for
 *
 * Start from the end of the ice_res_tracker and return right when we find the
 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
 * valid for SR-IOV because it is the only consumer that manipulates the
 * res->end and this is always called when res->end is set to res->num_entries.
 */
static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
{
	int i;

	if (!res)
		return -EINVAL;

	for (i = res->num_entries - 1; i >= 0; i--)
		if (res->list[i] & ICE_RES_VALID_BIT)
			return i;

	return 0;
}

/**
 * ice_sriov_set_msix_res - Set any used MSIX resources
 * @pf: pointer to PF structure
 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
 *
 * This function allows SR-IOV resources to be taken from the end of the PF's
M
Mitch Williams 已提交
454 455
 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
 * just set the pf->sriov_base_vector and return success.
B
Brett Creeley 已提交
456
 *
M
Mitch Williams 已提交
457 458
 * If there are not enough resources available, return an error. This should
 * always be caught by ice_set_per_vf_res().
B
Brett Creeley 已提交
459
 *
B
Bruce Allan 已提交
460
 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
B
Brett Creeley 已提交
461 462 463 464
 * in the PF's space available for SR-IOV.
 */
static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
{
M
Mitch Williams 已提交
465 466
	u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
	int vectors_used = pf->irq_tracker->num_entries;
B
Brett Creeley 已提交
467 468
	int sriov_base_vector;

M
Mitch Williams 已提交
469
	sriov_base_vector = total_vectors - num_msix_needed;
B
Brett Creeley 已提交
470 471 472 473

	/* make sure we only grab irq_tracker entries from the list end and
	 * that we have enough available MSIX vectors
	 */
M
Mitch Williams 已提交
474
	if (sriov_base_vector < vectors_used)
B
Brett Creeley 已提交
475 476 477 478 479 480 481
		return -EINVAL;

	pf->sriov_base_vector = sriov_base_vector;

	return 0;
}

482
/**
M
Mitch Williams 已提交
483
 * ice_set_per_vf_res - check if vectors and queues are available
484
 * @pf: pointer to the PF structure
485
 * @num_vfs: the number of SR-IOV VFs being configured
486
 *
M
Mitch Williams 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
 * get more vectors and can enable more queues per VF. Note that this does not
 * grab any vectors from the SW pool already allocated. Also note, that all
 * vector counts include one for each VF's miscellaneous interrupt vector
 * (i.e. OICR).
 *
 * Minimum VFs - 2 vectors, 1 queue pair
 * Small VFs - 5 vectors, 4 queue pairs
 * Medium VFs - 17 vectors, 16 queue pairs
 *
 * Second, determine number of queue pairs per VF by starting with a pre-defined
 * maximum each VF supports. If this is not possible, then we adjust based on
 * queue pairs available on the device.
 *
 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
 * by each VF during VF initialization and reset.
503
 */
504
static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs)
505
{
B
Brett Creeley 已提交
506
	int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
507
	u16 num_msix_per_vf, num_txq, num_rxq, avail_qs;
508
	int msix_avail_per_vf, msix_avail_for_sriov;
B
Brett Creeley 已提交
509
	struct device *dev = ice_pf_to_dev(pf);
510
	int err;
511

512 513
	lockdep_assert_held(&pf->vfs.table_lock);

514
	if (!num_vfs)
515 516
		return -EINVAL;

517 518 519
	if (max_valid_res_idx < 0)
		return -ENOSPC;

M
Mitch Williams 已提交
520
	/* determine MSI-X resources per VF */
521 522
	msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
		pf->irq_tracker->num_entries;
523
	msix_avail_per_vf = msix_avail_for_sriov / num_vfs;
524 525 526 527
	if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
528 529
	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
530 531
	} else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
		num_msix_per_vf = ICE_MIN_INTR_PER_VF;
532
	} else {
533 534
		dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
			msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
535
			num_vfs);
536
		return -ENOSPC;
537 538
	}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
			ICE_MAX_RSS_QS_PER_VF);
	avail_qs = ice_get_avail_txq_count(pf) / num_vfs;
	if (!avail_qs)
		num_txq = 0;
	else if (num_txq > avail_qs)
		num_txq = rounddown_pow_of_two(avail_qs);

	num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
			ICE_MAX_RSS_QS_PER_VF);
	avail_qs = ice_get_avail_rxq_count(pf) / num_vfs;
	if (!avail_qs)
		num_rxq = 0;
	else if (num_rxq > avail_qs)
		num_rxq = rounddown_pow_of_two(avail_qs);

	if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) {
556
		dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
557
			ICE_MIN_QS_PER_VF, num_vfs);
558
		return -ENOSPC;
M
Mitch Williams 已提交
559
	}
560

561 562 563 564 565
	err = ice_sriov_set_msix_res(pf, num_msix_per_vf * num_vfs);
	if (err) {
		dev_err(dev, "Unable to set MSI-X resources for %d VFs, err %d\n",
			num_vfs, err);
		return err;
M
Mitch Williams 已提交
566
	}
B
Brett Creeley 已提交
567

M
Mitch Williams 已提交
568
	/* only allow equal Tx/Rx queue count (i.e. queue pairs) */
569 570
	pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq);
	pf->vfs.num_msix_per = num_msix_per_vf;
M
Mitch Williams 已提交
571
	dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
572
		 num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per);
573 574 575 576

	return 0;
}

577 578 579 580 581 582 583 584 585
/**
 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
 * @vf: VF to initialize/setup the VSI for
 *
 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
 * VF VSI's broadcast filter and is only used during initial VF creation.
 */
static int ice_init_vf_vsi_res(struct ice_vf *vf)
{
586
	struct ice_vsi_vlan_ops *vlan_ops;
587 588 589 590 591 592 593 594 595
	struct ice_pf *pf = vf->pf;
	u8 broadcast[ETH_ALEN];
	struct ice_vsi *vsi;
	struct device *dev;
	int err;

	vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);

	dev = ice_pf_to_dev(pf);
596 597
	vsi = ice_vf_vsi_setup(vf);
	if (!vsi)
598 599
		return -ENOMEM;

600
	err = ice_vsi_add_vlan_zero(vsi);
601 602 603 604 605 606
	if (err) {
		dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

607 608 609 610 611 612 613 614
	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
	err = vlan_ops->ena_rx_filtering(vsi);
	if (err) {
		dev_warn(dev, "Failed to enable Rx VLAN filtering for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

615
	eth_broadcast_addr(broadcast);
T
Tony Nguyen 已提交
616 617
	err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
	if (err) {
618
		dev_err(dev, "Failed to add broadcast MAC filter for VF %d, error %d\n",
T
Tony Nguyen 已提交
619
			vf->vf_id, err);
620 621 622
		goto release_vsi;
	}

623
	err = ice_vsi_apply_spoofchk(vsi, vf->spoofchk);
624 625 626 627 628 629
	if (err) {
		dev_warn(dev, "Failed to initialize spoofchk setting for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

630 631 632 633 634
	vf->num_mac = 1;

	return 0;

release_vsi:
635
	ice_vf_vsi_release(vf);
636 637 638 639 640 641 642 643 644 645
	return err;
}

/**
 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
 * @pf: PF the VFs are associated with
 */
static int ice_start_vfs(struct ice_pf *pf)
{
	struct ice_hw *hw = &pf->hw;
646 647 648
	unsigned int bkt, it_cnt;
	struct ice_vf *vf;
	int retval;
649

650 651
	lockdep_assert_held(&pf->vfs.table_lock);

652 653
	it_cnt = 0;
	ice_for_each_vf(pf, bkt, vf) {
654
		vf->vf_ops->clear_reset_trigger(vf);
655 656 657 658 659 660 661 662 663 664 665

		retval = ice_init_vf_vsi_res(vf);
		if (retval) {
			dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
				vf->vf_id, retval);
			goto teardown;
		}

		set_bit(ICE_VF_STATE_INIT, vf->vf_states);
		ice_ena_vf_mappings(vf);
		wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
666
		it_cnt++;
667 668 669 670 671 672
	}

	ice_flush(hw);
	return 0;

teardown:
673 674 675
	ice_for_each_vf(pf, bkt, vf) {
		if (it_cnt == 0)
			break;
676 677

		ice_dis_vf_mappings(vf);
678
		ice_vf_vsi_release(vf);
679
		it_cnt--;
680 681 682 683 684
	}

	return retval;
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 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 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 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
/**
 * ice_sriov_free_vf - Free VF memory after all references are dropped
 * @vf: pointer to VF to free
 *
 * Called by ice_put_vf through ice_release_vf once the last reference to a VF
 * structure has been dropped.
 */
static void ice_sriov_free_vf(struct ice_vf *vf)
{
	mutex_destroy(&vf->cfg_lock);

	kfree_rcu(vf, rcu);
}

/**
 * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers
 * @vf: the vf to configure
 */
static void ice_sriov_clear_mbx_register(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;

	wr32(&pf->hw, VF_MBX_ARQLEN(vf->vf_id), 0);
	wr32(&pf->hw, VF_MBX_ATQLEN(vf->vf_id), 0);
}

/**
 * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF
 * @vf: pointer to VF structure
 * @is_vflr: true if reset occurred due to VFLR
 *
 * Trigger and cleanup after a VF reset for a SR-IOV VF.
 */
static void ice_sriov_trigger_reset_register(struct ice_vf *vf, bool is_vflr)
{
	struct ice_pf *pf = vf->pf;
	u32 reg, reg_idx, bit_idx;
	unsigned int vf_abs_id, i;
	struct device *dev;
	struct ice_hw *hw;

	dev = ice_pf_to_dev(pf);
	hw = &pf->hw;
	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;

	/* In the case of a VFLR, HW has already reset the VF and we just need
	 * to clean up. Otherwise we must first trigger the reset using the
	 * VFRTRIG register.
	 */
	if (!is_vflr) {
		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
		reg |= VPGEN_VFRTRIG_VFSWR_M;
		wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
	}

	/* clear the VFLR bit in GLGEN_VFLRSTAT */
	reg_idx = (vf_abs_id) / 32;
	bit_idx = (vf_abs_id) % 32;
	wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
	ice_flush(hw);

	wr32(hw, PF_PCI_CIAA,
	     VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
	for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
		reg = rd32(hw, PF_PCI_CIAD);
		/* no transactions pending so stop polling */
		if ((reg & VF_TRANS_PENDING_M) == 0)
			break;

		dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
		udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
	}
}

/**
 * ice_sriov_poll_reset_status - poll SRIOV VF reset status
 * @vf: pointer to VF structure
 *
 * Returns true when reset is successful, else returns false
 */
static bool ice_sriov_poll_reset_status(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	unsigned int i;
	u32 reg;

	for (i = 0; i < 10; i++) {
		/* VF reset requires driver to first reset the VF and then
		 * poll the status register to make sure that the reset
		 * completed successfully.
		 */
		reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id));
		if (reg & VPGEN_VFRSTAT_VFRD_M)
			return true;

		/* only sleep if the reset is not done */
		usleep_range(10, 20);
	}
	return false;
}

/**
 * ice_sriov_clear_reset_trigger - enable VF to access hardware
 * @vf: VF to enabled hardware access for
 */
static void ice_sriov_clear_reset_trigger(struct ice_vf *vf)
{
	struct ice_hw *hw = &vf->pf->hw;
	u32 reg;

	reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
	reg &= ~VPGEN_VFRTRIG_VFSWR_M;
	wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
	ice_flush(hw);
}

/**
 * ice_sriov_vsi_rebuild - release and rebuild VF's VSI
 * @vf: VF to release and setup the VSI for
 *
 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
 * configuration change, etc.).
 */
static int ice_sriov_vsi_rebuild(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;

	ice_vf_vsi_release(vf);
	if (!ice_vf_vsi_setup(vf)) {
		dev_err(ice_pf_to_dev(pf),
			"Failed to release and setup the VF%u's VSI\n",
			vf->vf_id);
		return -ENOMEM;
	}

	return 0;
}

/**
 * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
 * @vf: VF to perform tasks on
 */
static void ice_sriov_post_vsi_rebuild(struct ice_vf *vf)
{
	ice_vf_rebuild_host_cfg(vf);
	ice_vf_set_initialized(vf);
	ice_ena_vf_mappings(vf);
	wr32(&vf->pf->hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
}

static const struct ice_vf_ops ice_sriov_vf_ops = {
	.reset_type = ICE_VF_RESET,
	.free = ice_sriov_free_vf,
	.clear_mbx_register = ice_sriov_clear_mbx_register,
	.trigger_reset_register = ice_sriov_trigger_reset_register,
	.poll_reset_status = ice_sriov_poll_reset_status,
	.clear_reset_trigger = ice_sriov_clear_reset_trigger,
	.vsi_rebuild = ice_sriov_vsi_rebuild,
	.post_vsi_rebuild = ice_sriov_post_vsi_rebuild,
};

846
/**
847 848 849 850 851 852 853 854 855 856 857
 * ice_create_vf_entries - Allocate and insert VF entries
 * @pf: pointer to the PF structure
 * @num_vfs: the number of VFs to allocate
 *
 * Allocate new VF entries and insert them into the hash table. Set some
 * basic default fields for initializing the new VFs.
 *
 * After this function exits, the hash table will have num_vfs entries
 * inserted.
 *
 * Returns 0 on success or an integer error code on failure.
858
 */
859
static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs)
860
{
861
	struct ice_vfs *vfs = &pf->vfs;
862
	struct ice_vf *vf;
863 864 865 866 867 868 869 870 871 872 873 874
	u16 vf_id;
	int err;

	lockdep_assert_held(&vfs->table_lock);

	for (vf_id = 0; vf_id < num_vfs; vf_id++) {
		vf = kzalloc(sizeof(*vf), GFP_KERNEL);
		if (!vf) {
			err = -ENOMEM;
			goto err_free_entries;
		}
		kref_init(&vf->refcnt);
875 876

		vf->pf = pf;
877 878
		vf->vf_id = vf_id;

879 880 881
		/* set sriov vf ops for VFs created during SRIOV flow */
		vf->vf_ops = &ice_sriov_vf_ops;

882 883 884
		vf->vf_sw_id = pf->first_sw;
		/* assign default capabilities */
		vf->spoofchk = true;
885
		vf->num_vf_qs = pf->vfs.num_qps_per;
886
		ice_vc_set_default_allowlist(vf);
887 888 889 890 891

		/* ctrl_vsi_idx will be set to a valid value only when VF
		 * creates its first fdir rule.
		 */
		ice_vf_ctrl_invalidate_vsi(vf);
Q
Qi Zhang 已提交
892
		ice_vf_fdir_init(vf);
893

894
		ice_virtchnl_set_dflt_ops(vf);
895 896

		mutex_init(&vf->cfg_lock);
897

898 899
		hash_add_rcu(vfs->table, &vf->entry, vf_id);
	}
900 901

	return 0;
902 903 904 905

err_free_entries:
	ice_free_vf_entries(pf);
	return err;
906 907 908 909
}

/**
 * ice_ena_vfs - enable VFs so they are ready to be used
910
 * @pf: pointer to the PF structure
911
 * @num_vfs: number of VFs to enable
912
 */
913
static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
914
{
B
Brett Creeley 已提交
915
	struct device *dev = ice_pf_to_dev(pf);
916
	struct ice_hw *hw = &pf->hw;
917
	int ret;
918 919

	/* Disable global interrupt 0 so we don't try to handle the VFLR. */
B
Brett Creeley 已提交
920
	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
921
	     ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
922
	set_bit(ICE_OICR_INTR_DIS, pf->state);
923 924
	ice_flush(hw);

925
	ret = pci_enable_sriov(pf->pdev, num_vfs);
926
	if (ret)
927
		goto err_unroll_intr;
928

929
	mutex_lock(&pf->vfs.table_lock);
930

931 932 933 934
	ret = ice_set_per_vf_res(pf, num_vfs);
	if (ret) {
		dev_err(dev, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n",
			num_vfs, ret);
935 936 937
		goto err_unroll_sriov;
	}

938 939 940 941 942 943
	ret = ice_create_vf_entries(pf, num_vfs);
	if (ret) {
		dev_err(dev, "Failed to allocate VF entries for %d VFs\n",
			num_vfs);
		goto err_unroll_sriov;
	}
944

945 946 947
	ret = ice_start_vfs(pf);
	if (ret) {
		dev_err(dev, "Failed to start %d VFs, err %d\n", num_vfs, ret);
948
		ret = -EAGAIN;
949
		goto err_unroll_vf_entries;
950
	}
951

952
	clear_bit(ICE_VF_DIS, pf->state);
953

954
	ret = ice_eswitch_configure(pf);
955 956
	if (ret) {
		dev_err(dev, "Failed to configure eswitch, err %d\n", ret);
957
		goto err_unroll_sriov;
958
	}
959

960 961 962 963
	/* rearm global interrupts */
	if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
		ice_irq_dynamic_ena(hw, NULL, NULL);

964 965
	mutex_unlock(&pf->vfs.table_lock);

966
	return 0;
967

968 969
err_unroll_vf_entries:
	ice_free_vf_entries(pf);
970
err_unroll_sriov:
971
	mutex_unlock(&pf->vfs.table_lock);
972 973 974 975
	pci_disable_sriov(pf->pdev);
err_unroll_intr:
	/* rearm interrupts here */
	ice_irq_dynamic_ena(hw, NULL, NULL);
976
	clear_bit(ICE_OICR_INTR_DIS, pf->state);
977 978 979 980 981 982 983
	return ret;
}

/**
 * ice_pci_sriov_ena - Enable or change number of VFs
 * @pf: pointer to the PF structure
 * @num_vfs: number of VFs to allocate
984 985
 *
 * Returns 0 on success and negative on failure
986 987 988 989
 */
static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
{
	int pre_existing_vfs = pci_num_vf(pf->pdev);
B
Brett Creeley 已提交
990
	struct device *dev = ice_pf_to_dev(pf);
991 992 993 994 995
	int err;

	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
		ice_free_vfs(pf);
	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
996
		return 0;
997

998
	if (num_vfs > pf->vfs.num_supported) {
999
		dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1000
			num_vfs, pf->vfs.num_supported);
1001
		return -EOPNOTSUPP;
1002 1003
	}

1004 1005
	dev_info(dev, "Enabling %d VFs\n", num_vfs);
	err = ice_ena_vfs(pf, num_vfs);
1006 1007 1008 1009 1010 1011
	if (err) {
		dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
		return err;
	}

	set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	return 0;
}

/**
 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
 * @pf: PF to enabled SR-IOV on
 */
static int ice_check_sriov_allowed(struct ice_pf *pf)
{
	struct device *dev = ice_pf_to_dev(pf);

	if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
		dev_err(dev, "This device is not capable of SR-IOV\n");
		return -EOPNOTSUPP;
	}

	if (ice_is_safe_mode(pf)) {
		dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
		return -EOPNOTSUPP;
	}

	if (!ice_pf_state_is_nominal(pf)) {
		dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
		return -EBUSY;
	}

	return 0;
1039 1040 1041 1042 1043
}

/**
 * ice_sriov_configure - Enable or change number of VFs via sysfs
 * @pdev: pointer to a pci_dev structure
1044
 * @num_vfs: number of VFs to allocate or 0 to free VFs
1045
 *
1046 1047 1048
 * This function is called when the user updates the number of VFs in sysfs. On
 * success return whatever num_vfs was set to by the caller. Return negative on
 * failure.
1049 1050 1051 1052
 */
int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
	struct ice_pf *pf = pci_get_drvdata(pdev);
B
Brett Creeley 已提交
1053
	struct device *dev = ice_pf_to_dev(pf);
1054
	int err;
1055

1056 1057 1058
	err = ice_check_sriov_allowed(pf);
	if (err)
		return err;
T
Tony Nguyen 已提交
1059

1060 1061 1062
	if (!num_vfs) {
		if (!pci_vfs_assigned(pdev)) {
			ice_free_vfs(pf);
1063
			ice_mbx_deinit_snapshot(&pf->hw);
1064 1065
			if (pf->lag)
				ice_enable_lag(pf->lag);
1066 1067
			return 0;
		}
1068

B
Brett Creeley 已提交
1069
		dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1070 1071 1072
		return -EBUSY;
	}

T
Tony Nguyen 已提交
1073 1074 1075
	err = ice_mbx_init_snapshot(&pf->hw, num_vfs);
	if (err)
		return err;
1076

1077
	err = ice_pci_sriov_ena(pf, num_vfs);
1078 1079
	if (err) {
		ice_mbx_deinit_snapshot(&pf->hw);
1080
		return err;
1081
	}
1082

1083 1084
	if (pf->lag)
		ice_disable_lag(pf->lag);
1085
	return num_vfs;
1086
}
1087 1088 1089 1090 1091

/**
 * ice_process_vflr_event - Free VF resources via IRQ calls
 * @pf: pointer to the PF structure
 *
1092
 * called from the VFLR IRQ handler to
1093 1094 1095 1096 1097
 * free up VF resources and state variables
 */
void ice_process_vflr_event(struct ice_pf *pf)
{
	struct ice_hw *hw = &pf->hw;
1098 1099
	struct ice_vf *vf;
	unsigned int bkt;
1100 1101
	u32 reg;

1102
	if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
1103
	    !ice_has_vfs(pf))
1104 1105
		return;

1106
	mutex_lock(&pf->vfs.table_lock);
1107
	ice_for_each_vf(pf, bkt, vf) {
1108 1109
		u32 reg_idx, bit_idx;

1110 1111
		reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
		bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1112 1113
		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
		reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1114
		if (reg & BIT(bit_idx))
1115
			/* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1116
			ice_reset_vf(vf, ICE_VF_RESET_VFLR | ICE_VF_RESET_LOCK);
1117
	}
1118
	mutex_unlock(&pf->vfs.table_lock);
1119
}
1120

1121 1122 1123 1124 1125 1126 1127
/**
 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
 * @pf: PF used to index all VFs
 * @pfq: queue index relative to the PF's function space
 *
 * If no VF is found who owns the pfq then return NULL, otherwise return a
 * pointer to the VF who owns the pfq
1128 1129 1130 1131
 *
 * If this function returns non-NULL, it acquires a reference count of the VF
 * structure. The caller is responsible for calling ice_put_vf() to drop this
 * reference.
1132 1133 1134
 */
static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
{
1135 1136
	struct ice_vf *vf;
	unsigned int bkt;
1137

1138 1139
	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf) {
1140 1141 1142
		struct ice_vsi *vsi;
		u16 rxq_idx;

1143
		vsi = ice_get_vf_vsi(vf);
1144 1145
		if (!vsi)
			continue;
1146 1147

		ice_for_each_rxq(vsi, rxq_idx)
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
			if (vsi->rxq_map[rxq_idx] == pfq) {
				struct ice_vf *found;

				if (kref_get_unless_zero(&vf->refcnt))
					found = vf;
				else
					found = NULL;
				rcu_read_unlock();
				return found;
			}
1158
	}
1159
	rcu_read_unlock();
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

	return NULL;
}

/**
 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
 * @pf: PF used for conversion
 * @globalq: global queue index used to convert to PF space queue index
 */
static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
{
	return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
}

/**
 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
 * @pf: PF that the LAN overflow event happened on
 * @event: structure holding the event information for the LAN overflow event
 *
 * Determine if the LAN overflow event was caused by a VF queue. If it was not
 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
 * reset on the offending VF.
 */
void
ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
{
	u32 gldcb_rtctq, queue;
	struct ice_vf *vf;

	gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
	dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);

	/* event returns device global Rx queue number */
	queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
		GLDCB_RTCTQ_RXQNUM_S;

	vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
	if (!vf)
		return;

1200
	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1201
	ice_put_vf(vf);
1202 1203
}

1204
/**
1205 1206 1207 1208
 * ice_set_vf_spoofchk
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @ena: flag to enable or disable feature
1209
 *
1210
 * Enable or disable VF spoof checking
1211
 */
1212
int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
1213
{
1214 1215 1216
	struct ice_netdev_priv *np = netdev_priv(netdev);
	struct ice_pf *pf = np->vsi->back;
	struct ice_vsi *vf_vsi;
B
Brett Creeley 已提交
1217
	struct device *dev;
1218
	struct ice_vf *vf;
1219 1220
	int ret;

1221
	dev = ice_pf_to_dev(pf);
1222

1223 1224 1225
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
		return -EINVAL;
1226

1227 1228 1229
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
		goto out_put_vf;
1230

1231 1232 1233 1234 1235 1236
	vf_vsi = ice_get_vf_vsi(vf);
	if (!vf_vsi) {
		netdev_err(netdev, "VSI %d for VF %d is null\n",
			   vf->lan_vsi_idx, vf->vf_id);
		ret = -EINVAL;
		goto out_put_vf;
1237 1238
	}

1239 1240 1241 1242 1243
	if (vf_vsi->type != ICE_VSI_VF) {
		netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
			   vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
		ret = -ENODEV;
		goto out_put_vf;
1244
	}
1245

1246 1247 1248 1249
	if (ena == vf->spoofchk) {
		dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
		ret = 0;
		goto out_put_vf;
1250 1251
	}

1252 1253 1254 1255 1256 1257
	ret = ice_vsi_apply_spoofchk(vf_vsi, ena);
	if (ret)
		dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
			ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret);
	else
		vf->spoofchk = ena;
1258

1259 1260
out_put_vf:
	ice_put_vf(vf);
1261 1262 1263
	return ret;
}

1264 1265 1266 1267 1268 1269 1270 1271
/**
 * ice_get_vf_cfg
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @ivi: VF configuration structure
 *
 * return VF configuration
 */
1272 1273
int
ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
1274
{
J
Jesse Brandeburg 已提交
1275
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1276
	struct ice_vf *vf;
1277
	int ret;
1278

1279 1280
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
1281 1282
		return -EINVAL;

1283 1284
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1285
		goto out_put_vf;
1286 1287

	ivi->vf = vf_id;
1288
	ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
1289 1290

	/* VF configuration for VLAN and applicable QoS */
1291 1292
	ivi->vlan = ice_vf_get_port_vlan_id(vf);
	ivi->qos = ice_vf_get_port_vlan_prio(vf);
1293 1294
	if (ice_vf_is_port_vlan_ena(vf))
		ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf));
1295 1296 1297 1298 1299 1300 1301 1302 1303

	ivi->trusted = vf->trusted;
	ivi->spoofchk = vf->spoofchk;
	if (!vf->link_forced)
		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
	else if (vf->link_up)
		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
	else
		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
1304 1305
	ivi->max_tx_rate = vf->max_tx_rate;
	ivi->min_tx_rate = vf->min_tx_rate;
1306 1307 1308 1309

out_put_vf:
	ice_put_vf(vf);
	return ret;
1310 1311
}

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/**
 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
 * @pf: PF used to reference the switch's rules
 * @umac: unicast MAC to compare against existing switch rules
 *
 * Return true on the first/any match, else return false
 */
static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
{
	struct ice_sw_recipe *mac_recipe_list =
		&pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
	struct ice_fltr_mgmt_list_entry *list_itr;
	struct list_head *rule_head;
	struct mutex *rule_lock; /* protect MAC filter list access */

	rule_head = &mac_recipe_list->filt_rules;
	rule_lock = &mac_recipe_list->filt_rule_lock;

	mutex_lock(rule_lock);
	list_for_each_entry(list_itr, rule_head, list_entry) {
		u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];

		if (ether_addr_equal(existing_mac, umac)) {
			mutex_unlock(rule_lock);
			return true;
		}
	}

	mutex_unlock(rule_lock);

	return false;
}

1345 1346 1347 1348
/**
 * ice_set_vf_mac
 * @netdev: network interface device structure
 * @vf_id: VF identifier
1349
 * @mac: MAC address
1350
 *
1351
 * program VF MAC address
1352 1353 1354
 */
int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
{
J
Jesse Brandeburg 已提交
1355
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1356
	struct ice_vf *vf;
1357
	int ret;
1358

1359
	if (is_multicast_ether_addr(mac)) {
1360 1361 1362 1363
		netdev_err(netdev, "%pM not a valid unicast address\n", mac);
		return -EINVAL;
	}

1364 1365 1366 1367
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
		return -EINVAL;

1368
	/* nothing left to do, unicast MAC already set */
B
Brett Creeley 已提交
1369
	if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
1370 1371 1372 1373
	    ether_addr_equal(vf->hw_lan_addr.addr, mac)) {
		ret = 0;
		goto out_put_vf;
	}
1374

1375 1376
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1377
		goto out_put_vf;
1378

1379 1380 1381
	if (ice_unicast_mac_exists(pf, mac)) {
		netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
			   mac, vf_id, mac);
1382 1383
		ret = -EINVAL;
		goto out_put_vf;
1384 1385
	}

1386 1387
	mutex_lock(&vf->cfg_lock);

1388 1389
	/* VF is notified of its new MAC via the PF's response to the
	 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
1390
	 */
B
Brett Creeley 已提交
1391
	ether_addr_copy(vf->dev_lan_addr.addr, mac);
1392
	ether_addr_copy(vf->hw_lan_addr.addr, mac);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	if (is_zero_ether_addr(mac)) {
		/* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
		vf->pf_set_mac = false;
		netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
			    vf->vf_id);
	} else {
		/* PF will add MAC rule for the VF */
		vf->pf_set_mac = true;
		netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
			    mac, vf_id);
	}
1404

1405
	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1406
	mutex_unlock(&vf->cfg_lock);
1407 1408 1409 1410

out_put_vf:
	ice_put_vf(vf);
	return ret;
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
}

/**
 * ice_set_vf_trust
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @trusted: Boolean value to enable/disable trusted VF
 *
 * Enable or disable a given VF as trusted
 */
int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
{
J
Jesse Brandeburg 已提交
1423
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1424
	struct ice_vf *vf;
1425
	int ret;
1426

1427 1428 1429 1430 1431
	if (ice_is_eswitch_mode_switchdev(pf)) {
		dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
		return -EOPNOTSUPP;
	}

1432 1433
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
1434 1435
		return -EINVAL;

1436 1437
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1438
		goto out_put_vf;
1439 1440

	/* Check if already trusted */
1441 1442 1443 1444
	if (trusted == vf->trusted) {
		ret = 0;
		goto out_put_vf;
	}
1445

1446 1447
	mutex_lock(&vf->cfg_lock);

1448
	vf->trusted = trusted;
1449
	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1450
	dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
1451 1452
		 vf_id, trusted ? "" : "un");

1453 1454
	mutex_unlock(&vf->cfg_lock);

1455 1456 1457
out_put_vf:
	ice_put_vf(vf);
	return ret;
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
}

/**
 * ice_set_vf_link_state
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @link_state: required link state
 *
 * Set VF's link state, irrespective of physical link state status
 */
int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
{
J
Jesse Brandeburg 已提交
1470
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1471
	struct ice_vf *vf;
1472
	int ret;
1473

1474 1475
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
1476 1477
		return -EINVAL;

1478 1479
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1480
		goto out_put_vf;
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

	switch (link_state) {
	case IFLA_VF_LINK_STATE_AUTO:
		vf->link_forced = false;
		break;
	case IFLA_VF_LINK_STATE_ENABLE:
		vf->link_forced = true;
		vf->link_up = true;
		break;
	case IFLA_VF_LINK_STATE_DISABLE:
		vf->link_forced = true;
		vf->link_up = false;
		break;
	default:
1495 1496
		ret = -EINVAL;
		goto out_put_vf;
1497 1498
	}

1499
	ice_vc_notify_vf_link_state(vf);
1500

1501 1502 1503
out_put_vf:
	ice_put_vf(vf);
	return ret;
1504
}
J
Jesse Brandeburg 已提交
1505

1506 1507 1508 1509 1510 1511
/**
 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
 * @pf: PF associated with VFs
 */
static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
{
1512 1513 1514
	struct ice_vf *vf;
	unsigned int bkt;
	int rate = 0;
1515

1516 1517
	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf)
1518
		rate += vf->min_tx_rate;
1519
	rcu_read_unlock();
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

	return rate;
}

/**
 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
 * @vf: VF trying to configure min_tx_rate
 * @min_tx_rate: min Tx rate in Mbps
 *
 * Check if the min_tx_rate being passed in will cause oversubscription of total
 * min_tx_rate based on the current link speed and all other VFs configured
 * min_tx_rate
 *
 * Return true if the passed min_tx_rate would cause oversubscription, else
 * return false
 */
static bool
ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
{
1539 1540 1541 1542 1543 1544 1545 1546 1547
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
	int all_vfs_min_tx_rate;
	int link_speed_mbps;

	if (WARN_ON(!vsi))
		return false;

	link_speed_mbps = ice_get_link_speed_mbps(vsi);
	all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580

	/* this VF's previous rate is being overwritten */
	all_vfs_min_tx_rate -= vf->min_tx_rate;

	if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
		dev_err(ice_pf_to_dev(vf->pf), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n",
			min_tx_rate, vf->vf_id,
			all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
			link_speed_mbps);
		return true;
	}

	return false;
}

/**
 * ice_set_vf_bw - set min/max VF bandwidth
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @min_tx_rate: Minimum Tx rate in Mbps
 * @max_tx_rate: Maximum Tx rate in Mbps
 */
int
ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
	      int max_tx_rate)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	struct ice_vsi *vsi;
	struct device *dev;
	struct ice_vf *vf;
	int ret;

	dev = ice_pf_to_dev(pf);
1581 1582 1583

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
1584 1585 1586 1587
		return -EINVAL;

	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1588
		goto out_put_vf;
1589 1590

	vsi = ice_get_vf_vsi(vf);
1591 1592 1593 1594
	if (!vsi) {
		ret = -EINVAL;
		goto out_put_vf;
	}
1595 1596 1597 1598 1599 1600 1601

	/* when max_tx_rate is zero that means no max Tx rate limiting, so only
	 * check if max_tx_rate is non-zero
	 */
	if (max_tx_rate && min_tx_rate > max_tx_rate) {
		dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
			min_tx_rate, max_tx_rate);
1602 1603
		ret = -EINVAL;
		goto out_put_vf;
1604 1605 1606 1607
	}

	if (min_tx_rate && ice_is_dcb_active(pf)) {
		dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
1608 1609
		ret = -EOPNOTSUPP;
		goto out_put_vf;
1610 1611
	}

1612 1613 1614 1615
	if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) {
		ret = -EINVAL;
		goto out_put_vf;
	}
1616 1617 1618 1619 1620 1621

	if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
		ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
		if (ret) {
			dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
				vf->vf_id);
1622
			goto out_put_vf;
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
		}

		vf->min_tx_rate = min_tx_rate;
	}

	if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
		ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
		if (ret) {
			dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
				vf->vf_id);
1633
			goto out_put_vf;
1634 1635 1636 1637 1638
		}

		vf->max_tx_rate = max_tx_rate;
	}

1639 1640 1641
out_put_vf:
	ice_put_vf(vf);
	return ret;
1642 1643
}

J
Jesse Brandeburg 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
/**
 * ice_get_vf_stats - populate some stats for the VF
 * @netdev: the netdev of the PF
 * @vf_id: the host OS identifier (0-255)
 * @vf_stats: pointer to the OS memory to be initialized
 */
int ice_get_vf_stats(struct net_device *netdev, int vf_id,
		     struct ifla_vf_stats *vf_stats)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	struct ice_eth_stats *stats;
	struct ice_vsi *vsi;
	struct ice_vf *vf;
1657
	int ret;
J
Jesse Brandeburg 已提交
1658

1659 1660
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
J
Jesse Brandeburg 已提交
1661 1662
		return -EINVAL;

1663 1664
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
1665
		goto out_put_vf;
J
Jesse Brandeburg 已提交
1666

1667
	vsi = ice_get_vf_vsi(vf);
1668 1669 1670 1671
	if (!vsi) {
		ret = -EINVAL;
		goto out_put_vf;
	}
J
Jesse Brandeburg 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

	ice_update_eth_stats(vsi);
	stats = &vsi->eth_stats;

	memset(vf_stats, 0, sizeof(*vf_stats));

	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
		stats->rx_multicast;
	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
		stats->tx_multicast;
	vf_stats->rx_bytes   = stats->rx_bytes;
	vf_stats->tx_bytes   = stats->tx_bytes;
	vf_stats->broadcast  = stats->rx_broadcast;
	vf_stats->multicast  = stats->rx_multicast;
	vf_stats->rx_dropped = stats->rx_discards;
	vf_stats->tx_dropped = stats->tx_discards;

1689 1690 1691
out_put_vf:
	ice_put_vf(vf);
	return ret;
J
Jesse Brandeburg 已提交
1692
}
1693

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
/**
 * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
 * @hw: hardware structure used to check the VLAN mode
 * @vlan_proto: VLAN TPID being checked
 *
 * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
 * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
 * Mode (SVM), then only ETH_P_8021Q is supported.
 */
static bool
ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto)
{
	bool is_supported = false;

	switch (vlan_proto) {
	case ETH_P_8021Q:
		is_supported = true;
		break;
	case ETH_P_8021AD:
		if (ice_is_dvm_ena(hw))
			is_supported = true;
		break;
	}

	return is_supported;
}

/**
 * ice_set_vf_port_vlan
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @vlan_id: VLAN ID being set
 * @qos: priority setting
 * @vlan_proto: VLAN protocol
 *
 * program VF Port VLAN ID and/or QoS
 */
int
ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
		     __be16 vlan_proto)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	u16 local_vlan_proto = ntohs(vlan_proto);
	struct device *dev;
	struct ice_vf *vf;
	int ret;

	dev = ice_pf_to_dev(pf);

	if (vlan_id >= VLAN_N_VID || qos > 7) {
		dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
			vf_id, vlan_id, qos);
		return -EINVAL;
	}

	if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) {
		dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n",
			local_vlan_proto);
		return -EPROTONOSUPPORT;
	}

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
		return -EINVAL;

	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
		goto out_put_vf;

	if (ice_vf_get_port_vlan_prio(vf) == qos &&
	    ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto &&
	    ice_vf_get_port_vlan_id(vf) == vlan_id) {
		/* duplicate request, so just return success */
		dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
			vlan_id, qos, local_vlan_proto);
		ret = 0;
		goto out_put_vf;
	}

	mutex_lock(&vf->cfg_lock);

	vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos);
	if (ice_vf_is_port_vlan_ena(vf))
		dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
			 vlan_id, qos, local_vlan_proto, vf_id);
	else
		dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);

1782
	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1783 1784 1785 1786 1787 1788 1789
	mutex_unlock(&vf->cfg_lock);

out_put_vf:
	ice_put_vf(vf);
	return ret;
}

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
/**
 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
 * @vf: pointer to the VF structure
 */
void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	struct device *dev;

	dev = ice_pf_to_dev(pf);

	dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
		 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
B
Brett Creeley 已提交
1803
		 vf->dev_lan_addr.addr,
1804 1805 1806 1807
		 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
			  ? "on" : "off");
}

1808
/**
T
Tony Nguyen 已提交
1809
 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
1810 1811 1812 1813 1814 1815 1816 1817
 * @pf: pointer to the PF structure
 *
 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
 */
void ice_print_vfs_mdd_events(struct ice_pf *pf)
{
	struct device *dev = ice_pf_to_dev(pf);
	struct ice_hw *hw = &pf->hw;
1818 1819
	struct ice_vf *vf;
	unsigned int bkt;
1820 1821

	/* check that there are pending MDD events to print */
1822
	if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
1823 1824 1825
		return;

	/* VF MDD event logs are rate limited to one second intervals */
1826
	if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1))
1827 1828
		return;

1829
	pf->vfs.last_printed_mdd_jiffies = jiffies;
1830

1831
	mutex_lock(&pf->vfs.table_lock);
1832
	ice_for_each_vf(pf, bkt, vf) {
1833 1834 1835 1836
		/* only print Rx MDD event message if there are new events */
		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
			vf->mdd_rx_events.last_printed =
							vf->mdd_rx_events.count;
1837
			ice_print_vf_rx_mdd_event(vf);
1838 1839 1840 1841 1842 1843 1844 1845
		}

		/* only print Tx MDD event message if there are new events */
		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
			vf->mdd_tx_events.last_printed =
							vf->mdd_tx_events.count;

			dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
1846
				 vf->mdd_tx_events.count, hw->pf_id, vf->vf_id,
B
Brett Creeley 已提交
1847
				 vf->dev_lan_addr.addr);
1848 1849
		}
	}
1850
	mutex_unlock(&pf->vfs.table_lock);
1851
}
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869

/**
 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
 * @pdev: pointer to a pci_dev structure
 *
 * Called when recovering from a PF FLR to restore interrupt capability to
 * the VFs.
 */
void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
{
	u16 vf_id;
	int pos;

	if (!pci_num_vf(pdev))
		return;

	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
	if (pos) {
1870 1871
		struct pci_dev *vfdev;

1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
		pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
				     &vf_id);
		vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
		while (vfdev) {
			if (vfdev->is_virtfn && vfdev->physfn == pdev)
				pci_restore_msi_state(vfdev);
			vfdev = pci_get_device(pdev->vendor, vf_id,
					       vfdev);
		}
	}
}
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899

/**
 * ice_is_malicious_vf - helper function to detect a malicious VF
 * @pf: ptr to struct ice_pf
 * @event: pointer to the AQ event
 * @num_msg_proc: the number of messages processed so far
 * @num_msg_pending: the number of messages peinding in admin queue
 */
bool
ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
		    u16 num_msg_proc, u16 num_msg_pending)
{
	s16 vf_id = le16_to_cpu(event->desc.retval);
	struct device *dev = ice_pf_to_dev(pf);
	struct ice_mbx_data mbxdata;
	bool malvf = false;
	struct ice_vf *vf;
1900
	int status;
1901

1902 1903
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
1904 1905 1906
		return false;

	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1907
		goto out_put_vf;
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917

	mbxdata.num_msg_proc = num_msg_proc;
	mbxdata.num_pending_arq = num_msg_pending;
	mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
#define ICE_MBX_OVERFLOW_WATERMARK 64
	mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;

	/* check to see if we have a malicious VF */
	status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
	if (status)
1918
		goto out_put_vf;
1919 1920 1921 1922 1923 1924 1925

	if (malvf) {
		bool report_vf = false;

		/* if the VF is malicious and we haven't let the user
		 * know about it, then let them know now
		 */
1926
		status = ice_mbx_report_malvf(&pf->hw, pf->vfs.malvfs,
1927
					      ICE_MAX_SRIOV_VFS, vf_id,
1928 1929 1930 1931 1932 1933 1934 1935 1936
					      &report_vf);
		if (status)
			dev_dbg(dev, "Error reporting malicious VF\n");

		if (report_vf) {
			struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);

			if (pf_vsi)
				dev_warn(dev, "VF MAC %pM on PF MAC %pM is generating asynchronous messages and may be overflowing the PF message queue. Please see the Adapter User Guide for more information\n",
B
Brett Creeley 已提交
1937
					 &vf->dev_lan_addr.addr[0],
1938 1939 1940 1941
					 pf_vsi->netdev->dev_addr);
		}
	}

1942 1943 1944
out_put_vf:
	ice_put_vf(vf);
	return malvf;
1945
}