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