xref: /linux/drivers/net/ethernet/intel/ice/ice_sriov.c (revision cff9c565e65f3622e8dc1dcc21c1520a083dff35)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_vf_lib_private.h"
6 #include "ice_base.h"
7 #include "ice_lib.h"
8 #include "ice_fltr.h"
9 #include "ice_dcb_lib.h"
10 #include "ice_flow.h"
11 #include "ice_eswitch.h"
12 #include "ice_virtchnl_allowlist.h"
13 #include "ice_flex_pipe.h"
14 #include "ice_vf_vsi_vlan_ops.h"
15 #include "ice_vlan.h"
16 
17 /**
18  * ice_free_vf_entries - Free all VF entries from the hash table
19  * @pf: pointer to the PF structure
20  *
21  * Iterate over the VF hash table, removing and releasing all VF entries.
22  * Called during VF teardown or as cleanup during failed VF initialization.
23  */
24 static void ice_free_vf_entries(struct ice_pf *pf)
25 {
26 	struct ice_vfs *vfs = &pf->vfs;
27 	struct hlist_node *tmp;
28 	struct ice_vf *vf;
29 	unsigned int bkt;
30 
31 	/* Remove all VFs from the hash table and release their main
32 	 * reference. Once all references to the VF are dropped, ice_put_vf()
33 	 * will call ice_release_vf which will remove the VF memory.
34 	 */
35 	lockdep_assert_held(&vfs->table_lock);
36 
37 	hash_for_each_safe(vfs->table, bkt, tmp, vf, entry) {
38 		hash_del_rcu(&vf->entry);
39 		ice_put_vf(vf);
40 	}
41 }
42 
43 /**
44  * ice_free_vf_res - Free a VF's resources
45  * @vf: pointer to the VF info
46  */
47 static void ice_free_vf_res(struct ice_vf *vf)
48 {
49 	struct ice_pf *pf = vf->pf;
50 	int i, last_vector_idx;
51 
52 	/* First, disable VF's configuration API to prevent OS from
53 	 * accessing the VF's VSI after it's freed or invalidated.
54 	 */
55 	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
56 	ice_vf_fdir_exit(vf);
57 	/* free VF control VSI */
58 	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
59 		ice_vf_ctrl_vsi_release(vf);
60 
61 	/* free VSI and disconnect it from the parent uplink */
62 	if (vf->lan_vsi_idx != ICE_NO_VSI) {
63 		ice_vf_vsi_release(vf);
64 		vf->num_mac = 0;
65 	}
66 
67 	last_vector_idx = vf->first_vector_idx + vf->num_msix - 1;
68 
69 	/* clear VF MDD event information */
70 	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
71 	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
72 
73 	/* Disable interrupts so that VF starts in a known state */
74 	for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
75 		wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
76 		ice_flush(&pf->hw);
77 	}
78 	/* reset some of the state variables keeping track of the resources */
79 	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
80 	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
81 }
82 
83 /**
84  * ice_dis_vf_mappings
85  * @vf: pointer to the VF structure
86  */
87 static void ice_dis_vf_mappings(struct ice_vf *vf)
88 {
89 	struct ice_pf *pf = vf->pf;
90 	struct ice_vsi *vsi;
91 	struct device *dev;
92 	int first, last, v;
93 	struct ice_hw *hw;
94 
95 	hw = &pf->hw;
96 	vsi = ice_get_vf_vsi(vf);
97 	if (WARN_ON(!vsi))
98 		return;
99 
100 	dev = ice_pf_to_dev(pf);
101 	wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
102 	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
103 
104 	first = vf->first_vector_idx;
105 	last = first + vf->num_msix - 1;
106 	for (v = first; v <= last; v++) {
107 		u32 reg;
108 
109 		reg = FIELD_PREP(GLINT_VECT2FUNC_IS_PF_M, 1) |
110 		      FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id);
111 		wr32(hw, GLINT_VECT2FUNC(v), reg);
112 	}
113 
114 	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
115 		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
116 	else
117 		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
118 
119 	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
120 		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
121 	else
122 		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
123 }
124 
125 /**
126  * ice_sriov_free_msix_res - Reset/free any used MSIX resources
127  * @pf: pointer to the PF structure
128  *
129  * Since no MSIX entries are taken from the pf->irq_tracker then just clear
130  * the pf->sriov_base_vector.
131  *
132  * Returns 0 on success, and -EINVAL on error.
133  */
134 static int ice_sriov_free_msix_res(struct ice_pf *pf)
135 {
136 	if (!pf)
137 		return -EINVAL;
138 
139 	bitmap_free(pf->sriov_irq_bm);
140 	pf->sriov_irq_size = 0;
141 	pf->sriov_base_vector = 0;
142 
143 	return 0;
144 }
145 
146 /**
147  * ice_free_vfs - Free all VFs
148  * @pf: pointer to the PF structure
149  */
150 void ice_free_vfs(struct ice_pf *pf)
151 {
152 	struct device *dev = ice_pf_to_dev(pf);
153 	struct ice_vfs *vfs = &pf->vfs;
154 	struct ice_hw *hw = &pf->hw;
155 	struct ice_vf *vf;
156 	unsigned int bkt;
157 
158 	if (!ice_has_vfs(pf))
159 		return;
160 
161 	while (test_and_set_bit(ICE_VF_DIS, pf->state))
162 		usleep_range(1000, 2000);
163 
164 	/* Disable IOV before freeing resources. This lets any VF drivers
165 	 * running in the host get themselves cleaned up before we yank
166 	 * the carpet out from underneath their feet.
167 	 */
168 	if (!pci_vfs_assigned(pf->pdev))
169 		pci_disable_sriov(pf->pdev);
170 	else
171 		dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
172 
173 	ice_eswitch_reserve_cp_queues(pf, -ice_get_num_vfs(pf));
174 
175 	mutex_lock(&vfs->table_lock);
176 
177 	ice_for_each_vf(pf, bkt, vf) {
178 		mutex_lock(&vf->cfg_lock);
179 
180 		ice_eswitch_detach(pf, vf);
181 		ice_dis_vf_qs(vf);
182 
183 		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
184 			/* disable VF qp mappings and set VF disable state */
185 			ice_dis_vf_mappings(vf);
186 			set_bit(ICE_VF_STATE_DIS, vf->vf_states);
187 			ice_free_vf_res(vf);
188 		}
189 
190 		if (!pci_vfs_assigned(pf->pdev)) {
191 			u32 reg_idx, bit_idx;
192 
193 			reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
194 			bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
195 			wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
196 		}
197 
198 		/* clear malicious info since the VF is getting released */
199 		list_del(&vf->mbx_info.list_entry);
200 
201 		mutex_unlock(&vf->cfg_lock);
202 	}
203 
204 	if (ice_sriov_free_msix_res(pf))
205 		dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
206 
207 	vfs->num_qps_per = 0;
208 	ice_free_vf_entries(pf);
209 
210 	mutex_unlock(&vfs->table_lock);
211 
212 	clear_bit(ICE_VF_DIS, pf->state);
213 	clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
214 }
215 
216 /**
217  * ice_vf_vsi_setup - Set up a VF VSI
218  * @vf: VF to setup VSI for
219  *
220  * Returns pointer to the successfully allocated VSI struct on success,
221  * otherwise returns NULL on failure.
222  */
223 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
224 {
225 	struct ice_vsi_cfg_params params = {};
226 	struct ice_pf *pf = vf->pf;
227 	struct ice_vsi *vsi;
228 
229 	params.type = ICE_VSI_VF;
230 	params.pi = ice_vf_get_port_info(vf);
231 	params.vf = vf;
232 	params.flags = ICE_VSI_FLAG_INIT;
233 
234 	vsi = ice_vsi_setup(pf, &params);
235 
236 	if (!vsi) {
237 		dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
238 		ice_vf_invalidate_vsi(vf);
239 		return NULL;
240 	}
241 
242 	vf->lan_vsi_idx = vsi->idx;
243 	vf->lan_vsi_num = vsi->vsi_num;
244 
245 	return vsi;
246 }
247 
248 
249 /**
250  * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
251  * @vf: VF to enable MSIX mappings for
252  *
253  * Some of the registers need to be indexed/configured using hardware global
254  * device values and other registers need 0-based values, which represent PF
255  * based values.
256  */
257 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
258 {
259 	int device_based_first_msix, device_based_last_msix;
260 	int pf_based_first_msix, pf_based_last_msix, v;
261 	struct ice_pf *pf = vf->pf;
262 	int device_based_vf_id;
263 	struct ice_hw *hw;
264 	u32 reg;
265 
266 	hw = &pf->hw;
267 	pf_based_first_msix = vf->first_vector_idx;
268 	pf_based_last_msix = (pf_based_first_msix + vf->num_msix) - 1;
269 
270 	device_based_first_msix = pf_based_first_msix +
271 		pf->hw.func_caps.common_cap.msix_vector_first_id;
272 	device_based_last_msix =
273 		(device_based_first_msix + vf->num_msix) - 1;
274 	device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
275 
276 	reg = FIELD_PREP(VPINT_ALLOC_FIRST_M, device_based_first_msix) |
277 	      FIELD_PREP(VPINT_ALLOC_LAST_M, device_based_last_msix) |
278 	      VPINT_ALLOC_VALID_M;
279 	wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
280 
281 	reg = FIELD_PREP(VPINT_ALLOC_PCI_FIRST_M, device_based_first_msix) |
282 	      FIELD_PREP(VPINT_ALLOC_PCI_LAST_M, device_based_last_msix) |
283 	      VPINT_ALLOC_PCI_VALID_M;
284 	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
285 
286 	/* map the interrupts to its functions */
287 	for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
288 		reg = FIELD_PREP(GLINT_VECT2FUNC_VF_NUM_M, device_based_vf_id) |
289 		      FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id);
290 		wr32(hw, GLINT_VECT2FUNC(v), reg);
291 	}
292 
293 	/* Map mailbox interrupt to VF MSI-X vector 0 */
294 	wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
295 }
296 
297 /**
298  * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
299  * @vf: VF to enable the mappings for
300  * @max_txq: max Tx queues allowed on the VF's VSI
301  * @max_rxq: max Rx queues allowed on the VF's VSI
302  */
303 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
304 {
305 	struct device *dev = ice_pf_to_dev(vf->pf);
306 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
307 	struct ice_hw *hw = &vf->pf->hw;
308 	u32 reg;
309 
310 	if (WARN_ON(!vsi))
311 		return;
312 
313 	/* set regardless of mapping mode */
314 	wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
315 
316 	/* VF Tx queues allocation */
317 	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
318 		/* set the VF PF Tx queue range
319 		 * VFNUMQ value should be set to (number of queues - 1). A value
320 		 * of 0 means 1 queue and a value of 255 means 256 queues
321 		 */
322 		reg = FIELD_PREP(VPLAN_TX_QBASE_VFFIRSTQ_M, vsi->txq_map[0]) |
323 		      FIELD_PREP(VPLAN_TX_QBASE_VFNUMQ_M, max_txq - 1);
324 		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
325 	} else {
326 		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
327 	}
328 
329 	/* set regardless of mapping mode */
330 	wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
331 
332 	/* VF Rx queues allocation */
333 	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
334 		/* set the VF PF Rx queue range
335 		 * VFNUMQ value should be set to (number of queues - 1). A value
336 		 * of 0 means 1 queue and a value of 255 means 256 queues
337 		 */
338 		reg = FIELD_PREP(VPLAN_RX_QBASE_VFFIRSTQ_M, vsi->rxq_map[0]) |
339 		      FIELD_PREP(VPLAN_RX_QBASE_VFNUMQ_M, max_rxq - 1);
340 		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
341 	} else {
342 		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
343 	}
344 }
345 
346 /**
347  * ice_ena_vf_mappings - enable VF MSIX and queue mapping
348  * @vf: pointer to the VF structure
349  */
350 static void ice_ena_vf_mappings(struct ice_vf *vf)
351 {
352 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
353 
354 	if (WARN_ON(!vsi))
355 		return;
356 
357 	ice_ena_vf_msix_mappings(vf);
358 	ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
359 }
360 
361 /**
362  * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
363  * @vf: VF to calculate the register index for
364  * @q_vector: a q_vector associated to the VF
365  */
366 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
367 {
368 	if (!vf || !q_vector)
369 		return -EINVAL;
370 
371 	/* always add one to account for the OICR being the first MSIX */
372 	return vf->first_vector_idx + q_vector->v_idx + 1;
373 }
374 
375 /**
376  * ice_sriov_set_msix_res - Set any used MSIX resources
377  * @pf: pointer to PF structure
378  * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
379  *
380  * This function allows SR-IOV resources to be taken from the end of the PF's
381  * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
382  * just set the pf->sriov_base_vector and return success.
383  *
384  * If there are not enough resources available, return an error. This should
385  * always be caught by ice_set_per_vf_res().
386  *
387  * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
388  * in the PF's space available for SR-IOV.
389  */
390 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
391 {
392 	u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
393 	int vectors_used = ice_get_max_used_msix_vector(pf);
394 	int sriov_base_vector;
395 
396 	sriov_base_vector = total_vectors - num_msix_needed;
397 
398 	/* make sure we only grab irq_tracker entries from the list end and
399 	 * that we have enough available MSIX vectors
400 	 */
401 	if (sriov_base_vector < vectors_used)
402 		return -EINVAL;
403 
404 	pf->sriov_base_vector = sriov_base_vector;
405 
406 	return 0;
407 }
408 
409 /**
410  * ice_set_per_vf_res - check if vectors and queues are available
411  * @pf: pointer to the PF structure
412  * @num_vfs: the number of SR-IOV VFs being configured
413  *
414  * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
415  * get more vectors and can enable more queues per VF. Note that this does not
416  * grab any vectors from the SW pool already allocated. Also note, that all
417  * vector counts include one for each VF's miscellaneous interrupt vector
418  * (i.e. OICR).
419  *
420  * Minimum VFs - 2 vectors, 1 queue pair
421  * Small VFs - 5 vectors, 4 queue pairs
422  * Medium VFs - 17 vectors, 16 queue pairs
423  *
424  * Second, determine number of queue pairs per VF by starting with a pre-defined
425  * maximum each VF supports. If this is not possible, then we adjust based on
426  * queue pairs available on the device.
427  *
428  * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
429  * by each VF during VF initialization and reset.
430  */
431 static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs)
432 {
433 	int vectors_used = ice_get_max_used_msix_vector(pf);
434 	u16 num_msix_per_vf, num_txq, num_rxq, avail_qs;
435 	int msix_avail_per_vf, msix_avail_for_sriov;
436 	struct device *dev = ice_pf_to_dev(pf);
437 	int err;
438 
439 	lockdep_assert_held(&pf->vfs.table_lock);
440 
441 	if (!num_vfs)
442 		return -EINVAL;
443 
444 	/* determine MSI-X resources per VF */
445 	msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
446 		vectors_used;
447 	msix_avail_per_vf = msix_avail_for_sriov / num_vfs;
448 	if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
449 		num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
450 	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
451 		num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
452 	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
453 		num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
454 	} else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
455 		num_msix_per_vf = ICE_MIN_INTR_PER_VF;
456 	} else {
457 		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",
458 			msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
459 			num_vfs);
460 		return -ENOSPC;
461 	}
462 
463 	num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
464 			ICE_MAX_RSS_QS_PER_VF);
465 	avail_qs = ice_get_avail_txq_count(pf) / num_vfs;
466 	if (!avail_qs)
467 		num_txq = 0;
468 	else if (num_txq > avail_qs)
469 		num_txq = rounddown_pow_of_two(avail_qs);
470 
471 	num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
472 			ICE_MAX_RSS_QS_PER_VF);
473 	avail_qs = ice_get_avail_rxq_count(pf) / num_vfs;
474 	if (!avail_qs)
475 		num_rxq = 0;
476 	else if (num_rxq > avail_qs)
477 		num_rxq = rounddown_pow_of_two(avail_qs);
478 
479 	if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) {
480 		dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
481 			ICE_MIN_QS_PER_VF, num_vfs);
482 		return -ENOSPC;
483 	}
484 
485 	err = ice_sriov_set_msix_res(pf, num_msix_per_vf * num_vfs);
486 	if (err) {
487 		dev_err(dev, "Unable to set MSI-X resources for %d VFs, err %d\n",
488 			num_vfs, err);
489 		return err;
490 	}
491 
492 	/* only allow equal Tx/Rx queue count (i.e. queue pairs) */
493 	pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq);
494 	pf->vfs.num_msix_per = num_msix_per_vf;
495 	dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
496 		 num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per);
497 
498 	return 0;
499 }
500 
501 /**
502  * ice_sriov_get_irqs - get irqs for SR-IOV usacase
503  * @pf: pointer to PF structure
504  * @needed: number of irqs to get
505  *
506  * This returns the first MSI-X vector index in PF space that is used by this
507  * VF. This index is used when accessing PF relative registers such as
508  * GLINT_VECT2FUNC and GLINT_DYN_CTL.
509  * This will always be the OICR index in the AVF driver so any functionality
510  * using vf->first_vector_idx for queue configuration_id: id of VF which will
511  * use this irqs
512  *
513  * Only SRIOV specific vectors are tracked in sriov_irq_bm. SRIOV vectors are
514  * allocated from the end of global irq index. First bit in sriov_irq_bm means
515  * last irq index etc. It simplifies extension of SRIOV vectors.
516  * They will be always located from sriov_base_vector to the last irq
517  * index. While increasing/decreasing sriov_base_vector can be moved.
518  */
519 static int ice_sriov_get_irqs(struct ice_pf *pf, u16 needed)
520 {
521 	int res = bitmap_find_next_zero_area(pf->sriov_irq_bm,
522 					     pf->sriov_irq_size, 0, needed, 0);
523 	/* conversion from number in bitmap to global irq index */
524 	int index = pf->sriov_irq_size - res - needed;
525 
526 	if (res >= pf->sriov_irq_size || index < pf->sriov_base_vector)
527 		return -ENOENT;
528 
529 	bitmap_set(pf->sriov_irq_bm, res, needed);
530 	return index;
531 }
532 
533 /**
534  * ice_sriov_free_irqs - free irqs used by the VF
535  * @pf: pointer to PF structure
536  * @vf: pointer to VF structure
537  */
538 static void ice_sriov_free_irqs(struct ice_pf *pf, struct ice_vf *vf)
539 {
540 	/* Move back from first vector index to first index in bitmap */
541 	int bm_i = pf->sriov_irq_size - vf->first_vector_idx - vf->num_msix;
542 
543 	bitmap_clear(pf->sriov_irq_bm, bm_i, vf->num_msix);
544 	vf->first_vector_idx = 0;
545 }
546 
547 /**
548  * ice_init_vf_vsi_res - initialize/setup VF VSI resources
549  * @vf: VF to initialize/setup the VSI for
550  *
551  * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
552  * VF VSI's broadcast filter and is only used during initial VF creation.
553  */
554 static int ice_init_vf_vsi_res(struct ice_vf *vf)
555 {
556 	struct ice_pf *pf = vf->pf;
557 	struct ice_vsi *vsi;
558 	int err;
559 
560 	vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
561 	if (vf->first_vector_idx < 0)
562 		return -ENOMEM;
563 
564 	vsi = ice_vf_vsi_setup(vf);
565 	if (!vsi)
566 		return -ENOMEM;
567 
568 	err = ice_vf_init_host_cfg(vf, vsi);
569 	if (err)
570 		goto release_vsi;
571 
572 	return 0;
573 
574 release_vsi:
575 	ice_vf_vsi_release(vf);
576 	return err;
577 }
578 
579 /**
580  * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
581  * @pf: PF the VFs are associated with
582  */
583 static int ice_start_vfs(struct ice_pf *pf)
584 {
585 	struct ice_hw *hw = &pf->hw;
586 	unsigned int bkt, it_cnt;
587 	struct ice_vf *vf;
588 	int retval;
589 
590 	lockdep_assert_held(&pf->vfs.table_lock);
591 
592 	it_cnt = 0;
593 	ice_for_each_vf(pf, bkt, vf) {
594 		vf->vf_ops->clear_reset_trigger(vf);
595 
596 		retval = ice_init_vf_vsi_res(vf);
597 		if (retval) {
598 			dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
599 				vf->vf_id, retval);
600 			goto teardown;
601 		}
602 
603 		retval = ice_eswitch_attach(pf, vf);
604 		if (retval) {
605 			dev_err(ice_pf_to_dev(pf), "Failed to attach VF %d to eswitch, error %d",
606 				vf->vf_id, retval);
607 			ice_vf_vsi_release(vf);
608 			goto teardown;
609 		}
610 
611 		set_bit(ICE_VF_STATE_INIT, vf->vf_states);
612 		ice_ena_vf_mappings(vf);
613 		wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
614 		it_cnt++;
615 	}
616 
617 	ice_flush(hw);
618 	return 0;
619 
620 teardown:
621 	ice_for_each_vf(pf, bkt, vf) {
622 		if (it_cnt == 0)
623 			break;
624 
625 		ice_dis_vf_mappings(vf);
626 		ice_vf_vsi_release(vf);
627 		it_cnt--;
628 	}
629 
630 	return retval;
631 }
632 
633 /**
634  * ice_sriov_free_vf - Free VF memory after all references are dropped
635  * @vf: pointer to VF to free
636  *
637  * Called by ice_put_vf through ice_release_vf once the last reference to a VF
638  * structure has been dropped.
639  */
640 static void ice_sriov_free_vf(struct ice_vf *vf)
641 {
642 	mutex_destroy(&vf->cfg_lock);
643 
644 	kfree_rcu(vf, rcu);
645 }
646 
647 /**
648  * ice_sriov_clear_reset_state - clears VF Reset status register
649  * @vf: the vf to configure
650  */
651 static void ice_sriov_clear_reset_state(struct ice_vf *vf)
652 {
653 	struct ice_hw *hw = &vf->pf->hw;
654 
655 	/* Clear the reset status register so that VF immediately sees that
656 	 * the device is resetting, even if hardware hasn't yet gotten around
657 	 * to clearing VFGEN_RSTAT for us.
658 	 */
659 	wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_INPROGRESS);
660 }
661 
662 /**
663  * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers
664  * @vf: the vf to configure
665  */
666 static void ice_sriov_clear_mbx_register(struct ice_vf *vf)
667 {
668 	struct ice_pf *pf = vf->pf;
669 
670 	wr32(&pf->hw, VF_MBX_ARQLEN(vf->vf_id), 0);
671 	wr32(&pf->hw, VF_MBX_ATQLEN(vf->vf_id), 0);
672 }
673 
674 /**
675  * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF
676  * @vf: pointer to VF structure
677  * @is_vflr: true if reset occurred due to VFLR
678  *
679  * Trigger and cleanup after a VF reset for a SR-IOV VF.
680  */
681 static void ice_sriov_trigger_reset_register(struct ice_vf *vf, bool is_vflr)
682 {
683 	struct ice_pf *pf = vf->pf;
684 	u32 reg, reg_idx, bit_idx;
685 	unsigned int vf_abs_id, i;
686 	struct device *dev;
687 	struct ice_hw *hw;
688 
689 	dev = ice_pf_to_dev(pf);
690 	hw = &pf->hw;
691 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
692 
693 	/* In the case of a VFLR, HW has already reset the VF and we just need
694 	 * to clean up. Otherwise we must first trigger the reset using the
695 	 * VFRTRIG register.
696 	 */
697 	if (!is_vflr) {
698 		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
699 		reg |= VPGEN_VFRTRIG_VFSWR_M;
700 		wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
701 	}
702 
703 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
704 	reg_idx = (vf_abs_id) / 32;
705 	bit_idx = (vf_abs_id) % 32;
706 	wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
707 	ice_flush(hw);
708 
709 	wr32(hw, PF_PCI_CIAA,
710 	     VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
711 	for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
712 		reg = rd32(hw, PF_PCI_CIAD);
713 		/* no transactions pending so stop polling */
714 		if ((reg & VF_TRANS_PENDING_M) == 0)
715 			break;
716 
717 		dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
718 		udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
719 	}
720 }
721 
722 /**
723  * ice_sriov_poll_reset_status - poll SRIOV VF reset status
724  * @vf: pointer to VF structure
725  *
726  * Returns true when reset is successful, else returns false
727  */
728 static bool ice_sriov_poll_reset_status(struct ice_vf *vf)
729 {
730 	struct ice_pf *pf = vf->pf;
731 	unsigned int i;
732 	u32 reg;
733 
734 	for (i = 0; i < 10; i++) {
735 		/* VF reset requires driver to first reset the VF and then
736 		 * poll the status register to make sure that the reset
737 		 * completed successfully.
738 		 */
739 		reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id));
740 		if (reg & VPGEN_VFRSTAT_VFRD_M)
741 			return true;
742 
743 		/* only sleep if the reset is not done */
744 		usleep_range(10, 20);
745 	}
746 	return false;
747 }
748 
749 /**
750  * ice_sriov_clear_reset_trigger - enable VF to access hardware
751  * @vf: VF to enabled hardware access for
752  */
753 static void ice_sriov_clear_reset_trigger(struct ice_vf *vf)
754 {
755 	struct ice_hw *hw = &vf->pf->hw;
756 	u32 reg;
757 
758 	reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
759 	reg &= ~VPGEN_VFRTRIG_VFSWR_M;
760 	wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
761 	ice_flush(hw);
762 }
763 
764 /**
765  * ice_sriov_create_vsi - Create a new VSI for a VF
766  * @vf: VF to create the VSI for
767  *
768  * This is called by ice_vf_recreate_vsi to create the new VSI after the old
769  * VSI has been released.
770  */
771 static int ice_sriov_create_vsi(struct ice_vf *vf)
772 {
773 	struct ice_vsi *vsi;
774 
775 	vsi = ice_vf_vsi_setup(vf);
776 	if (!vsi)
777 		return -ENOMEM;
778 
779 	return 0;
780 }
781 
782 /**
783  * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
784  * @vf: VF to perform tasks on
785  */
786 static void ice_sriov_post_vsi_rebuild(struct ice_vf *vf)
787 {
788 	ice_ena_vf_mappings(vf);
789 	wr32(&vf->pf->hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
790 }
791 
792 static const struct ice_vf_ops ice_sriov_vf_ops = {
793 	.reset_type = ICE_VF_RESET,
794 	.free = ice_sriov_free_vf,
795 	.clear_reset_state = ice_sriov_clear_reset_state,
796 	.clear_mbx_register = ice_sriov_clear_mbx_register,
797 	.trigger_reset_register = ice_sriov_trigger_reset_register,
798 	.poll_reset_status = ice_sriov_poll_reset_status,
799 	.clear_reset_trigger = ice_sriov_clear_reset_trigger,
800 	.irq_close = NULL,
801 	.create_vsi = ice_sriov_create_vsi,
802 	.post_vsi_rebuild = ice_sriov_post_vsi_rebuild,
803 };
804 
805 /**
806  * ice_create_vf_entries - Allocate and insert VF entries
807  * @pf: pointer to the PF structure
808  * @num_vfs: the number of VFs to allocate
809  *
810  * Allocate new VF entries and insert them into the hash table. Set some
811  * basic default fields for initializing the new VFs.
812  *
813  * After this function exits, the hash table will have num_vfs entries
814  * inserted.
815  *
816  * Returns 0 on success or an integer error code on failure.
817  */
818 static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs)
819 {
820 	struct pci_dev *pdev = pf->pdev;
821 	struct ice_vfs *vfs = &pf->vfs;
822 	struct pci_dev *vfdev = NULL;
823 	struct ice_vf *vf;
824 	u16 vf_pdev_id;
825 	int err, pos;
826 
827 	lockdep_assert_held(&vfs->table_lock);
828 
829 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
830 	pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_pdev_id);
831 
832 	for (u16 vf_id = 0; vf_id < num_vfs; vf_id++) {
833 		vf = kzalloc(sizeof(*vf), GFP_KERNEL);
834 		if (!vf) {
835 			err = -ENOMEM;
836 			goto err_free_entries;
837 		}
838 		kref_init(&vf->refcnt);
839 
840 		vf->pf = pf;
841 		vf->vf_id = vf_id;
842 
843 		/* set sriov vf ops for VFs created during SRIOV flow */
844 		vf->vf_ops = &ice_sriov_vf_ops;
845 
846 		ice_initialize_vf_entry(vf);
847 
848 		do {
849 			vfdev = pci_get_device(pdev->vendor, vf_pdev_id, vfdev);
850 		} while (vfdev && vfdev->physfn != pdev);
851 		vf->vfdev = vfdev;
852 		vf->vf_sw_id = pf->first_sw;
853 
854 		pci_dev_get(vfdev);
855 
856 		/* set default number of MSI-X */
857 		vf->num_msix = pf->vfs.num_msix_per;
858 		vf->num_vf_qs = pf->vfs.num_qps_per;
859 		ice_vc_set_default_allowlist(vf);
860 
861 		hash_add_rcu(vfs->table, &vf->entry, vf_id);
862 	}
863 
864 	/* Decrement of refcount done by pci_get_device() inside the loop does
865 	 * not touch the last iteration's vfdev, so it has to be done manually
866 	 * to balance pci_dev_get() added within the loop.
867 	 */
868 	pci_dev_put(vfdev);
869 
870 	return 0;
871 
872 err_free_entries:
873 	ice_free_vf_entries(pf);
874 	return err;
875 }
876 
877 /**
878  * ice_ena_vfs - enable VFs so they are ready to be used
879  * @pf: pointer to the PF structure
880  * @num_vfs: number of VFs to enable
881  */
882 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
883 {
884 	int total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
885 	struct device *dev = ice_pf_to_dev(pf);
886 	struct ice_hw *hw = &pf->hw;
887 	int ret;
888 
889 	pf->sriov_irq_bm = bitmap_zalloc(total_vectors, GFP_KERNEL);
890 	if (!pf->sriov_irq_bm)
891 		return -ENOMEM;
892 	pf->sriov_irq_size = total_vectors;
893 
894 	/* Disable global interrupt 0 so we don't try to handle the VFLR. */
895 	wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
896 	     ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
897 	set_bit(ICE_OICR_INTR_DIS, pf->state);
898 	ice_flush(hw);
899 
900 	ret = pci_enable_sriov(pf->pdev, num_vfs);
901 	if (ret)
902 		goto err_unroll_intr;
903 
904 	mutex_lock(&pf->vfs.table_lock);
905 
906 	ret = ice_set_per_vf_res(pf, num_vfs);
907 	if (ret) {
908 		dev_err(dev, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n",
909 			num_vfs, ret);
910 		goto err_unroll_sriov;
911 	}
912 
913 	ret = ice_create_vf_entries(pf, num_vfs);
914 	if (ret) {
915 		dev_err(dev, "Failed to allocate VF entries for %d VFs\n",
916 			num_vfs);
917 		goto err_unroll_sriov;
918 	}
919 
920 	ice_eswitch_reserve_cp_queues(pf, num_vfs);
921 	ret = ice_start_vfs(pf);
922 	if (ret) {
923 		dev_err(dev, "Failed to start %d VFs, err %d\n", num_vfs, ret);
924 		ret = -EAGAIN;
925 		goto err_unroll_vf_entries;
926 	}
927 
928 	clear_bit(ICE_VF_DIS, pf->state);
929 
930 	/* rearm global interrupts */
931 	if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
932 		ice_irq_dynamic_ena(hw, NULL, NULL);
933 
934 	mutex_unlock(&pf->vfs.table_lock);
935 
936 	return 0;
937 
938 err_unroll_vf_entries:
939 	ice_free_vf_entries(pf);
940 err_unroll_sriov:
941 	mutex_unlock(&pf->vfs.table_lock);
942 	pci_disable_sriov(pf->pdev);
943 err_unroll_intr:
944 	/* rearm interrupts here */
945 	ice_irq_dynamic_ena(hw, NULL, NULL);
946 	clear_bit(ICE_OICR_INTR_DIS, pf->state);
947 	bitmap_free(pf->sriov_irq_bm);
948 	return ret;
949 }
950 
951 /**
952  * ice_pci_sriov_ena - Enable or change number of VFs
953  * @pf: pointer to the PF structure
954  * @num_vfs: number of VFs to allocate
955  *
956  * Returns 0 on success and negative on failure
957  */
958 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
959 {
960 	struct device *dev = ice_pf_to_dev(pf);
961 	int err;
962 
963 	if (!num_vfs) {
964 		ice_free_vfs(pf);
965 		return 0;
966 	}
967 
968 	if (num_vfs > pf->vfs.num_supported) {
969 		dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
970 			num_vfs, pf->vfs.num_supported);
971 		return -EOPNOTSUPP;
972 	}
973 
974 	dev_info(dev, "Enabling %d VFs\n", num_vfs);
975 	err = ice_ena_vfs(pf, num_vfs);
976 	if (err) {
977 		dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
978 		return err;
979 	}
980 
981 	set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
982 	return 0;
983 }
984 
985 /**
986  * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
987  * @pf: PF to enabled SR-IOV on
988  */
989 static int ice_check_sriov_allowed(struct ice_pf *pf)
990 {
991 	struct device *dev = ice_pf_to_dev(pf);
992 
993 	if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
994 		dev_err(dev, "This device is not capable of SR-IOV\n");
995 		return -EOPNOTSUPP;
996 	}
997 
998 	if (ice_is_safe_mode(pf)) {
999 		dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1000 		return -EOPNOTSUPP;
1001 	}
1002 
1003 	if (!ice_pf_state_is_nominal(pf)) {
1004 		dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1005 		return -EBUSY;
1006 	}
1007 
1008 	return 0;
1009 }
1010 
1011 /**
1012  * ice_sriov_get_vf_total_msix - return number of MSI-X used by VFs
1013  * @pdev: pointer to pci_dev struct
1014  *
1015  * The function is called via sysfs ops
1016  */
1017 u32 ice_sriov_get_vf_total_msix(struct pci_dev *pdev)
1018 {
1019 	struct ice_pf *pf = pci_get_drvdata(pdev);
1020 
1021 	return pf->sriov_irq_size - ice_get_max_used_msix_vector(pf);
1022 }
1023 
1024 static int ice_sriov_move_base_vector(struct ice_pf *pf, int move)
1025 {
1026 	if (pf->sriov_base_vector - move < ice_get_max_used_msix_vector(pf))
1027 		return -ENOMEM;
1028 
1029 	pf->sriov_base_vector -= move;
1030 	return 0;
1031 }
1032 
1033 static void ice_sriov_remap_vectors(struct ice_pf *pf, u16 restricted_id)
1034 {
1035 	u16 vf_ids[ICE_MAX_SRIOV_VFS];
1036 	struct ice_vf *tmp_vf;
1037 	int to_remap = 0, bkt;
1038 
1039 	/* For better irqs usage try to remap irqs of VFs
1040 	 * that aren't running yet
1041 	 */
1042 	ice_for_each_vf(pf, bkt, tmp_vf) {
1043 		/* skip VF which is changing the number of MSI-X */
1044 		if (restricted_id == tmp_vf->vf_id ||
1045 		    test_bit(ICE_VF_STATE_ACTIVE, tmp_vf->vf_states))
1046 			continue;
1047 
1048 		ice_dis_vf_mappings(tmp_vf);
1049 		ice_sriov_free_irqs(pf, tmp_vf);
1050 
1051 		vf_ids[to_remap] = tmp_vf->vf_id;
1052 		to_remap += 1;
1053 	}
1054 
1055 	for (int i = 0; i < to_remap; i++) {
1056 		tmp_vf = ice_get_vf_by_id(pf, vf_ids[i]);
1057 		if (!tmp_vf)
1058 			continue;
1059 
1060 		tmp_vf->first_vector_idx =
1061 			ice_sriov_get_irqs(pf, tmp_vf->num_msix);
1062 		/* there is no need to rebuild VSI as we are only changing the
1063 		 * vector indexes not amount of MSI-X or queues
1064 		 */
1065 		ice_ena_vf_mappings(tmp_vf);
1066 		ice_put_vf(tmp_vf);
1067 	}
1068 }
1069 
1070 /**
1071  * ice_sriov_set_msix_vec_count
1072  * @vf_dev: pointer to pci_dev struct of VF device
1073  * @msix_vec_count: new value for MSI-X amount on this VF
1074  *
1075  * Set requested MSI-X, queues and registers for @vf_dev.
1076  *
1077  * First do some sanity checks like if there are any VFs, if the new value
1078  * is correct etc. Then disable old mapping (MSI-X and queues registers), change
1079  * MSI-X and queues, rebuild VSI and enable new mapping.
1080  *
1081  * If it is possible (driver not binded to VF) try to remap also other VFs to
1082  * linearize irqs register usage.
1083  */
1084 int ice_sriov_set_msix_vec_count(struct pci_dev *vf_dev, int msix_vec_count)
1085 {
1086 	struct pci_dev *pdev = pci_physfn(vf_dev);
1087 	struct ice_pf *pf = pci_get_drvdata(pdev);
1088 	u16 prev_msix, prev_queues, queues;
1089 	bool needs_rebuild = false;
1090 	struct ice_vf *vf;
1091 	int id;
1092 
1093 	if (!ice_get_num_vfs(pf))
1094 		return -ENOENT;
1095 
1096 	if (!msix_vec_count)
1097 		return 0;
1098 
1099 	queues = msix_vec_count;
1100 	/* add 1 MSI-X for OICR */
1101 	msix_vec_count += 1;
1102 
1103 	if (queues > min(ice_get_avail_txq_count(pf),
1104 			 ice_get_avail_rxq_count(pf)))
1105 		return -EINVAL;
1106 
1107 	if (msix_vec_count < ICE_MIN_INTR_PER_VF)
1108 		return -EINVAL;
1109 
1110 	/* Transition of PCI VF function number to function_id */
1111 	for (id = 0; id < pci_num_vf(pdev); id++) {
1112 		if (vf_dev->devfn == pci_iov_virtfn_devfn(pdev, id))
1113 			break;
1114 	}
1115 
1116 	if (id == pci_num_vf(pdev))
1117 		return -ENOENT;
1118 
1119 	vf = ice_get_vf_by_id(pf, id);
1120 
1121 	if (!vf)
1122 		return -ENOENT;
1123 
1124 	prev_msix = vf->num_msix;
1125 	prev_queues = vf->num_vf_qs;
1126 
1127 	if (ice_sriov_move_base_vector(pf, msix_vec_count - prev_msix)) {
1128 		ice_put_vf(vf);
1129 		return -ENOSPC;
1130 	}
1131 
1132 	ice_dis_vf_mappings(vf);
1133 	ice_sriov_free_irqs(pf, vf);
1134 
1135 	/* Remap all VFs beside the one is now configured */
1136 	ice_sriov_remap_vectors(pf, vf->vf_id);
1137 
1138 	vf->num_msix = msix_vec_count;
1139 	vf->num_vf_qs = queues;
1140 	vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
1141 	if (vf->first_vector_idx < 0)
1142 		goto unroll;
1143 
1144 	ice_vf_vsi_release(vf);
1145 	if (vf->vf_ops->create_vsi(vf)) {
1146 		/* Try to rebuild with previous values */
1147 		needs_rebuild = true;
1148 		goto unroll;
1149 	}
1150 
1151 	dev_info(ice_pf_to_dev(pf),
1152 		 "Changing VF %d resources to %d vectors and %d queues\n",
1153 		 vf->vf_id, vf->num_msix, vf->num_vf_qs);
1154 
1155 	ice_ena_vf_mappings(vf);
1156 	ice_put_vf(vf);
1157 
1158 	return 0;
1159 
1160 unroll:
1161 	dev_info(ice_pf_to_dev(pf),
1162 		 "Can't set %d vectors on VF %d, falling back to %d\n",
1163 		 vf->num_msix, vf->vf_id, prev_msix);
1164 
1165 	vf->num_msix = prev_msix;
1166 	vf->num_vf_qs = prev_queues;
1167 	vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
1168 	if (vf->first_vector_idx < 0)
1169 		return -EINVAL;
1170 
1171 	if (needs_rebuild)
1172 		vf->vf_ops->create_vsi(vf);
1173 
1174 	ice_ena_vf_mappings(vf);
1175 	ice_put_vf(vf);
1176 
1177 	return -EINVAL;
1178 }
1179 
1180 /**
1181  * ice_sriov_configure - Enable or change number of VFs via sysfs
1182  * @pdev: pointer to a pci_dev structure
1183  * @num_vfs: number of VFs to allocate or 0 to free VFs
1184  *
1185  * This function is called when the user updates the number of VFs in sysfs. On
1186  * success return whatever num_vfs was set to by the caller. Return negative on
1187  * failure.
1188  */
1189 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1190 {
1191 	struct ice_pf *pf = pci_get_drvdata(pdev);
1192 	struct device *dev = ice_pf_to_dev(pf);
1193 	int err;
1194 
1195 	err = ice_check_sriov_allowed(pf);
1196 	if (err)
1197 		return err;
1198 
1199 	if (!num_vfs) {
1200 		if (!pci_vfs_assigned(pdev)) {
1201 			ice_free_vfs(pf);
1202 			return 0;
1203 		}
1204 
1205 		dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1206 		return -EBUSY;
1207 	}
1208 
1209 	err = ice_pci_sriov_ena(pf, num_vfs);
1210 	if (err)
1211 		return err;
1212 
1213 	return num_vfs;
1214 }
1215 
1216 /**
1217  * ice_process_vflr_event - Free VF resources via IRQ calls
1218  * @pf: pointer to the PF structure
1219  *
1220  * called from the VFLR IRQ handler to
1221  * free up VF resources and state variables
1222  */
1223 void ice_process_vflr_event(struct ice_pf *pf)
1224 {
1225 	struct ice_hw *hw = &pf->hw;
1226 	struct ice_vf *vf;
1227 	unsigned int bkt;
1228 	u32 reg;
1229 
1230 	if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
1231 	    !ice_has_vfs(pf))
1232 		return;
1233 
1234 	mutex_lock(&pf->vfs.table_lock);
1235 	ice_for_each_vf(pf, bkt, vf) {
1236 		u32 reg_idx, bit_idx;
1237 
1238 		reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1239 		bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1240 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
1241 		reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1242 		if (reg & BIT(bit_idx))
1243 			/* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1244 			ice_reset_vf(vf, ICE_VF_RESET_VFLR | ICE_VF_RESET_LOCK);
1245 	}
1246 	mutex_unlock(&pf->vfs.table_lock);
1247 }
1248 
1249 /**
1250  * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1251  * @pf: PF used to index all VFs
1252  * @pfq: queue index relative to the PF's function space
1253  *
1254  * If no VF is found who owns the pfq then return NULL, otherwise return a
1255  * pointer to the VF who owns the pfq
1256  *
1257  * If this function returns non-NULL, it acquires a reference count of the VF
1258  * structure. The caller is responsible for calling ice_put_vf() to drop this
1259  * reference.
1260  */
1261 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1262 {
1263 	struct ice_vf *vf;
1264 	unsigned int bkt;
1265 
1266 	rcu_read_lock();
1267 	ice_for_each_vf_rcu(pf, bkt, vf) {
1268 		struct ice_vsi *vsi;
1269 		u16 rxq_idx;
1270 
1271 		vsi = ice_get_vf_vsi(vf);
1272 		if (!vsi)
1273 			continue;
1274 
1275 		ice_for_each_rxq(vsi, rxq_idx)
1276 			if (vsi->rxq_map[rxq_idx] == pfq) {
1277 				struct ice_vf *found;
1278 
1279 				if (kref_get_unless_zero(&vf->refcnt))
1280 					found = vf;
1281 				else
1282 					found = NULL;
1283 				rcu_read_unlock();
1284 				return found;
1285 			}
1286 	}
1287 	rcu_read_unlock();
1288 
1289 	return NULL;
1290 }
1291 
1292 /**
1293  * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1294  * @pf: PF used for conversion
1295  * @globalq: global queue index used to convert to PF space queue index
1296  */
1297 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1298 {
1299 	return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1300 }
1301 
1302 /**
1303  * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1304  * @pf: PF that the LAN overflow event happened on
1305  * @event: structure holding the event information for the LAN overflow event
1306  *
1307  * Determine if the LAN overflow event was caused by a VF queue. If it was not
1308  * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1309  * reset on the offending VF.
1310  */
1311 void
1312 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1313 {
1314 	u32 gldcb_rtctq, queue;
1315 	struct ice_vf *vf;
1316 
1317 	gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1318 	dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1319 
1320 	/* event returns device global Rx queue number */
1321 	queue = FIELD_GET(GLDCB_RTCTQ_RXQNUM_M, gldcb_rtctq);
1322 
1323 	vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1324 	if (!vf)
1325 		return;
1326 
1327 	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1328 	ice_put_vf(vf);
1329 }
1330 
1331 /**
1332  * ice_set_vf_spoofchk
1333  * @netdev: network interface device structure
1334  * @vf_id: VF identifier
1335  * @ena: flag to enable or disable feature
1336  *
1337  * Enable or disable VF spoof checking
1338  */
1339 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
1340 {
1341 	struct ice_netdev_priv *np = netdev_priv(netdev);
1342 	struct ice_pf *pf = np->vsi->back;
1343 	struct ice_vsi *vf_vsi;
1344 	struct device *dev;
1345 	struct ice_vf *vf;
1346 	int ret;
1347 
1348 	dev = ice_pf_to_dev(pf);
1349 
1350 	vf = ice_get_vf_by_id(pf, vf_id);
1351 	if (!vf)
1352 		return -EINVAL;
1353 
1354 	ret = ice_check_vf_ready_for_cfg(vf);
1355 	if (ret)
1356 		goto out_put_vf;
1357 
1358 	vf_vsi = ice_get_vf_vsi(vf);
1359 	if (!vf_vsi) {
1360 		netdev_err(netdev, "VSI %d for VF %d is null\n",
1361 			   vf->lan_vsi_idx, vf->vf_id);
1362 		ret = -EINVAL;
1363 		goto out_put_vf;
1364 	}
1365 
1366 	if (vf_vsi->type != ICE_VSI_VF) {
1367 		netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
1368 			   vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
1369 		ret = -ENODEV;
1370 		goto out_put_vf;
1371 	}
1372 
1373 	if (ena == vf->spoofchk) {
1374 		dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
1375 		ret = 0;
1376 		goto out_put_vf;
1377 	}
1378 
1379 	ret = ice_vsi_apply_spoofchk(vf_vsi, ena);
1380 	if (ret)
1381 		dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
1382 			ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret);
1383 	else
1384 		vf->spoofchk = ena;
1385 
1386 out_put_vf:
1387 	ice_put_vf(vf);
1388 	return ret;
1389 }
1390 
1391 /**
1392  * ice_get_vf_cfg
1393  * @netdev: network interface device structure
1394  * @vf_id: VF identifier
1395  * @ivi: VF configuration structure
1396  *
1397  * return VF configuration
1398  */
1399 int
1400 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
1401 {
1402 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1403 	struct ice_vf *vf;
1404 	int ret;
1405 
1406 	vf = ice_get_vf_by_id(pf, vf_id);
1407 	if (!vf)
1408 		return -EINVAL;
1409 
1410 	ret = ice_check_vf_ready_for_cfg(vf);
1411 	if (ret)
1412 		goto out_put_vf;
1413 
1414 	ivi->vf = vf_id;
1415 	ether_addr_copy(ivi->mac, vf->hw_lan_addr);
1416 
1417 	/* VF configuration for VLAN and applicable QoS */
1418 	ivi->vlan = ice_vf_get_port_vlan_id(vf);
1419 	ivi->qos = ice_vf_get_port_vlan_prio(vf);
1420 	if (ice_vf_is_port_vlan_ena(vf))
1421 		ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf));
1422 
1423 	ivi->trusted = vf->trusted;
1424 	ivi->spoofchk = vf->spoofchk;
1425 	if (!vf->link_forced)
1426 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
1427 	else if (vf->link_up)
1428 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
1429 	else
1430 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
1431 	ivi->max_tx_rate = vf->max_tx_rate;
1432 	ivi->min_tx_rate = vf->min_tx_rate;
1433 
1434 out_put_vf:
1435 	ice_put_vf(vf);
1436 	return ret;
1437 }
1438 
1439 /**
1440  * ice_set_vf_mac
1441  * @netdev: network interface device structure
1442  * @vf_id: VF identifier
1443  * @mac: MAC address
1444  *
1445  * program VF MAC address
1446  */
1447 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1448 {
1449 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1450 	struct ice_vf *vf;
1451 	int ret;
1452 
1453 	if (is_multicast_ether_addr(mac)) {
1454 		netdev_err(netdev, "%pM not a valid unicast address\n", mac);
1455 		return -EINVAL;
1456 	}
1457 
1458 	vf = ice_get_vf_by_id(pf, vf_id);
1459 	if (!vf)
1460 		return -EINVAL;
1461 
1462 	/* nothing left to do, unicast MAC already set */
1463 	if (ether_addr_equal(vf->dev_lan_addr, mac) &&
1464 	    ether_addr_equal(vf->hw_lan_addr, mac)) {
1465 		ret = 0;
1466 		goto out_put_vf;
1467 	}
1468 
1469 	ret = ice_check_vf_ready_for_cfg(vf);
1470 	if (ret)
1471 		goto out_put_vf;
1472 
1473 	mutex_lock(&vf->cfg_lock);
1474 
1475 	/* VF is notified of its new MAC via the PF's response to the
1476 	 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
1477 	 */
1478 	ether_addr_copy(vf->dev_lan_addr, mac);
1479 	ether_addr_copy(vf->hw_lan_addr, mac);
1480 	if (is_zero_ether_addr(mac)) {
1481 		/* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
1482 		vf->pf_set_mac = false;
1483 		netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
1484 			    vf->vf_id);
1485 	} else {
1486 		/* PF will add MAC rule for the VF */
1487 		vf->pf_set_mac = true;
1488 		netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
1489 			    mac, vf_id);
1490 	}
1491 
1492 	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1493 	mutex_unlock(&vf->cfg_lock);
1494 
1495 out_put_vf:
1496 	ice_put_vf(vf);
1497 	return ret;
1498 }
1499 
1500 /**
1501  * ice_set_vf_trust
1502  * @netdev: network interface device structure
1503  * @vf_id: VF identifier
1504  * @trusted: Boolean value to enable/disable trusted VF
1505  *
1506  * Enable or disable a given VF as trusted
1507  */
1508 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
1509 {
1510 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1511 	struct ice_vf *vf;
1512 	int ret;
1513 
1514 	vf = ice_get_vf_by_id(pf, vf_id);
1515 	if (!vf)
1516 		return -EINVAL;
1517 
1518 	if (ice_is_eswitch_mode_switchdev(pf)) {
1519 		dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
1520 		return -EOPNOTSUPP;
1521 	}
1522 
1523 	ret = ice_check_vf_ready_for_cfg(vf);
1524 	if (ret)
1525 		goto out_put_vf;
1526 
1527 	/* Check if already trusted */
1528 	if (trusted == vf->trusted) {
1529 		ret = 0;
1530 		goto out_put_vf;
1531 	}
1532 
1533 	mutex_lock(&vf->cfg_lock);
1534 
1535 	vf->trusted = trusted;
1536 	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1537 	dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
1538 		 vf_id, trusted ? "" : "un");
1539 
1540 	mutex_unlock(&vf->cfg_lock);
1541 
1542 out_put_vf:
1543 	ice_put_vf(vf);
1544 	return ret;
1545 }
1546 
1547 /**
1548  * ice_set_vf_link_state
1549  * @netdev: network interface device structure
1550  * @vf_id: VF identifier
1551  * @link_state: required link state
1552  *
1553  * Set VF's link state, irrespective of physical link state status
1554  */
1555 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
1556 {
1557 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1558 	struct ice_vf *vf;
1559 	int ret;
1560 
1561 	vf = ice_get_vf_by_id(pf, vf_id);
1562 	if (!vf)
1563 		return -EINVAL;
1564 
1565 	ret = ice_check_vf_ready_for_cfg(vf);
1566 	if (ret)
1567 		goto out_put_vf;
1568 
1569 	switch (link_state) {
1570 	case IFLA_VF_LINK_STATE_AUTO:
1571 		vf->link_forced = false;
1572 		break;
1573 	case IFLA_VF_LINK_STATE_ENABLE:
1574 		vf->link_forced = true;
1575 		vf->link_up = true;
1576 		break;
1577 	case IFLA_VF_LINK_STATE_DISABLE:
1578 		vf->link_forced = true;
1579 		vf->link_up = false;
1580 		break;
1581 	default:
1582 		ret = -EINVAL;
1583 		goto out_put_vf;
1584 	}
1585 
1586 	ice_vc_notify_vf_link_state(vf);
1587 
1588 out_put_vf:
1589 	ice_put_vf(vf);
1590 	return ret;
1591 }
1592 
1593 /**
1594  * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
1595  * @pf: PF associated with VFs
1596  */
1597 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
1598 {
1599 	struct ice_vf *vf;
1600 	unsigned int bkt;
1601 	int rate = 0;
1602 
1603 	rcu_read_lock();
1604 	ice_for_each_vf_rcu(pf, bkt, vf)
1605 		rate += vf->min_tx_rate;
1606 	rcu_read_unlock();
1607 
1608 	return rate;
1609 }
1610 
1611 /**
1612  * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
1613  * @vf: VF trying to configure min_tx_rate
1614  * @min_tx_rate: min Tx rate in Mbps
1615  *
1616  * Check if the min_tx_rate being passed in will cause oversubscription of total
1617  * min_tx_rate based on the current link speed and all other VFs configured
1618  * min_tx_rate
1619  *
1620  * Return true if the passed min_tx_rate would cause oversubscription, else
1621  * return false
1622  */
1623 static bool
1624 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
1625 {
1626 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1627 	int all_vfs_min_tx_rate;
1628 	int link_speed_mbps;
1629 
1630 	if (WARN_ON(!vsi))
1631 		return false;
1632 
1633 	link_speed_mbps = ice_get_link_speed_mbps(vsi);
1634 	all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
1635 
1636 	/* this VF's previous rate is being overwritten */
1637 	all_vfs_min_tx_rate -= vf->min_tx_rate;
1638 
1639 	if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
1640 		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",
1641 			min_tx_rate, vf->vf_id,
1642 			all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
1643 			link_speed_mbps);
1644 		return true;
1645 	}
1646 
1647 	return false;
1648 }
1649 
1650 /**
1651  * ice_set_vf_bw - set min/max VF bandwidth
1652  * @netdev: network interface device structure
1653  * @vf_id: VF identifier
1654  * @min_tx_rate: Minimum Tx rate in Mbps
1655  * @max_tx_rate: Maximum Tx rate in Mbps
1656  */
1657 int
1658 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
1659 	      int max_tx_rate)
1660 {
1661 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1662 	struct ice_vsi *vsi;
1663 	struct device *dev;
1664 	struct ice_vf *vf;
1665 	int ret;
1666 
1667 	dev = ice_pf_to_dev(pf);
1668 
1669 	vf = ice_get_vf_by_id(pf, vf_id);
1670 	if (!vf)
1671 		return -EINVAL;
1672 
1673 	ret = ice_check_vf_ready_for_cfg(vf);
1674 	if (ret)
1675 		goto out_put_vf;
1676 
1677 	vsi = ice_get_vf_vsi(vf);
1678 	if (!vsi) {
1679 		ret = -EINVAL;
1680 		goto out_put_vf;
1681 	}
1682 
1683 	if (min_tx_rate && ice_is_dcb_active(pf)) {
1684 		dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
1685 		ret = -EOPNOTSUPP;
1686 		goto out_put_vf;
1687 	}
1688 
1689 	if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) {
1690 		ret = -EINVAL;
1691 		goto out_put_vf;
1692 	}
1693 
1694 	if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
1695 		ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
1696 		if (ret) {
1697 			dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
1698 				vf->vf_id);
1699 			goto out_put_vf;
1700 		}
1701 
1702 		vf->min_tx_rate = min_tx_rate;
1703 	}
1704 
1705 	if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
1706 		ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
1707 		if (ret) {
1708 			dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
1709 				vf->vf_id);
1710 			goto out_put_vf;
1711 		}
1712 
1713 		vf->max_tx_rate = max_tx_rate;
1714 	}
1715 
1716 out_put_vf:
1717 	ice_put_vf(vf);
1718 	return ret;
1719 }
1720 
1721 /**
1722  * ice_get_vf_stats - populate some stats for the VF
1723  * @netdev: the netdev of the PF
1724  * @vf_id: the host OS identifier (0-255)
1725  * @vf_stats: pointer to the OS memory to be initialized
1726  */
1727 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
1728 		     struct ifla_vf_stats *vf_stats)
1729 {
1730 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1731 	struct ice_eth_stats *stats;
1732 	struct ice_vsi *vsi;
1733 	struct ice_vf *vf;
1734 	int ret;
1735 
1736 	vf = ice_get_vf_by_id(pf, vf_id);
1737 	if (!vf)
1738 		return -EINVAL;
1739 
1740 	ret = ice_check_vf_ready_for_cfg(vf);
1741 	if (ret)
1742 		goto out_put_vf;
1743 
1744 	vsi = ice_get_vf_vsi(vf);
1745 	if (!vsi) {
1746 		ret = -EINVAL;
1747 		goto out_put_vf;
1748 	}
1749 
1750 	ice_update_eth_stats(vsi);
1751 	stats = &vsi->eth_stats;
1752 
1753 	memset(vf_stats, 0, sizeof(*vf_stats));
1754 
1755 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
1756 		stats->rx_multicast;
1757 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
1758 		stats->tx_multicast;
1759 	vf_stats->rx_bytes   = stats->rx_bytes;
1760 	vf_stats->tx_bytes   = stats->tx_bytes;
1761 	vf_stats->broadcast  = stats->rx_broadcast;
1762 	vf_stats->multicast  = stats->rx_multicast;
1763 	vf_stats->rx_dropped = stats->rx_discards;
1764 	vf_stats->tx_dropped = stats->tx_discards;
1765 
1766 out_put_vf:
1767 	ice_put_vf(vf);
1768 	return ret;
1769 }
1770 
1771 /**
1772  * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
1773  * @hw: hardware structure used to check the VLAN mode
1774  * @vlan_proto: VLAN TPID being checked
1775  *
1776  * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
1777  * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
1778  * Mode (SVM), then only ETH_P_8021Q is supported.
1779  */
1780 static bool
1781 ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto)
1782 {
1783 	bool is_supported = false;
1784 
1785 	switch (vlan_proto) {
1786 	case ETH_P_8021Q:
1787 		is_supported = true;
1788 		break;
1789 	case ETH_P_8021AD:
1790 		if (ice_is_dvm_ena(hw))
1791 			is_supported = true;
1792 		break;
1793 	}
1794 
1795 	return is_supported;
1796 }
1797 
1798 /**
1799  * ice_set_vf_port_vlan
1800  * @netdev: network interface device structure
1801  * @vf_id: VF identifier
1802  * @vlan_id: VLAN ID being set
1803  * @qos: priority setting
1804  * @vlan_proto: VLAN protocol
1805  *
1806  * program VF Port VLAN ID and/or QoS
1807  */
1808 int
1809 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
1810 		     __be16 vlan_proto)
1811 {
1812 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
1813 	u16 local_vlan_proto = ntohs(vlan_proto);
1814 	struct device *dev;
1815 	struct ice_vf *vf;
1816 	int ret;
1817 
1818 	dev = ice_pf_to_dev(pf);
1819 
1820 	if (vlan_id >= VLAN_N_VID || qos > 7) {
1821 		dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
1822 			vf_id, vlan_id, qos);
1823 		return -EINVAL;
1824 	}
1825 
1826 	if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) {
1827 		dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n",
1828 			local_vlan_proto);
1829 		return -EPROTONOSUPPORT;
1830 	}
1831 
1832 	vf = ice_get_vf_by_id(pf, vf_id);
1833 	if (!vf)
1834 		return -EINVAL;
1835 
1836 	ret = ice_check_vf_ready_for_cfg(vf);
1837 	if (ret)
1838 		goto out_put_vf;
1839 
1840 	if (ice_vf_get_port_vlan_prio(vf) == qos &&
1841 	    ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto &&
1842 	    ice_vf_get_port_vlan_id(vf) == vlan_id) {
1843 		/* duplicate request, so just return success */
1844 		dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
1845 			vlan_id, qos, local_vlan_proto);
1846 		ret = 0;
1847 		goto out_put_vf;
1848 	}
1849 
1850 	mutex_lock(&vf->cfg_lock);
1851 
1852 	vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos);
1853 	if (ice_vf_is_port_vlan_ena(vf))
1854 		dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
1855 			 vlan_id, qos, local_vlan_proto, vf_id);
1856 	else
1857 		dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
1858 
1859 	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1860 	mutex_unlock(&vf->cfg_lock);
1861 
1862 out_put_vf:
1863 	ice_put_vf(vf);
1864 	return ret;
1865 }
1866 
1867 /**
1868  * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
1869  * @vf: pointer to the VF structure
1870  */
1871 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
1872 {
1873 	struct ice_pf *pf = vf->pf;
1874 	struct device *dev;
1875 
1876 	dev = ice_pf_to_dev(pf);
1877 
1878 	dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
1879 		 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
1880 		 vf->dev_lan_addr,
1881 		 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
1882 			  ? "on" : "off");
1883 }
1884 
1885 /**
1886  * ice_print_vfs_mdd_events - print VFs malicious driver detect event
1887  * @pf: pointer to the PF structure
1888  *
1889  * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
1890  */
1891 void ice_print_vfs_mdd_events(struct ice_pf *pf)
1892 {
1893 	struct device *dev = ice_pf_to_dev(pf);
1894 	struct ice_hw *hw = &pf->hw;
1895 	struct ice_vf *vf;
1896 	unsigned int bkt;
1897 
1898 	/* check that there are pending MDD events to print */
1899 	if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
1900 		return;
1901 
1902 	/* VF MDD event logs are rate limited to one second intervals */
1903 	if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1))
1904 		return;
1905 
1906 	pf->vfs.last_printed_mdd_jiffies = jiffies;
1907 
1908 	mutex_lock(&pf->vfs.table_lock);
1909 	ice_for_each_vf(pf, bkt, vf) {
1910 		/* only print Rx MDD event message if there are new events */
1911 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
1912 			vf->mdd_rx_events.last_printed =
1913 							vf->mdd_rx_events.count;
1914 			ice_print_vf_rx_mdd_event(vf);
1915 		}
1916 
1917 		/* only print Tx MDD event message if there are new events */
1918 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
1919 			vf->mdd_tx_events.last_printed =
1920 							vf->mdd_tx_events.count;
1921 
1922 			dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
1923 				 vf->mdd_tx_events.count, hw->pf_id, vf->vf_id,
1924 				 vf->dev_lan_addr);
1925 		}
1926 	}
1927 	mutex_unlock(&pf->vfs.table_lock);
1928 }
1929 
1930 /**
1931  * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
1932  * @pf: pointer to the PF structure
1933  *
1934  * Called when recovering from a PF FLR to restore interrupt capability to
1935  * the VFs.
1936  */
1937 void ice_restore_all_vfs_msi_state(struct ice_pf *pf)
1938 {
1939 	struct ice_vf *vf;
1940 	u32 bkt;
1941 
1942 	ice_for_each_vf(pf, bkt, vf)
1943 		pci_restore_msi_state(vf->vfdev);
1944 }
1945