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