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