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