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