xref: /linux/drivers/net/ethernet/intel/ice/ice_vf_lib.c (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "ice_vf_lib_private.h"
5 #include "ice.h"
6 #include "ice_lib.h"
7 #include "ice_fltr.h"
8 #include "ice_virtchnl_allowlist.h"
9 
10 /* Public functions which may be accessed by all driver files */
11 
12 /**
13  * ice_get_vf_by_id - Get pointer to VF by ID
14  * @pf: the PF private structure
15  * @vf_id: the VF ID to locate
16  *
17  * Locate and return a pointer to the VF structure associated with a given ID.
18  * Returns NULL if the ID does not have a valid VF structure associated with
19  * it.
20  *
21  * This function takes a reference to the VF, which must be released by
22  * calling ice_put_vf() once the caller is finished accessing the VF structure
23  * returned.
24  */
25 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
26 {
27 	struct ice_vf *vf;
28 
29 	rcu_read_lock();
30 	hash_for_each_possible_rcu(pf->vfs.table, vf, entry, vf_id) {
31 		if (vf->vf_id == vf_id) {
32 			struct ice_vf *found;
33 
34 			if (kref_get_unless_zero(&vf->refcnt))
35 				found = vf;
36 			else
37 				found = NULL;
38 
39 			rcu_read_unlock();
40 			return found;
41 		}
42 	}
43 	rcu_read_unlock();
44 
45 	return NULL;
46 }
47 
48 /**
49  * ice_release_vf - Release VF associated with a refcount
50  * @ref: the kref decremented to zero
51  *
52  * Callback function for kref_put to release a VF once its reference count has
53  * hit zero.
54  */
55 static void ice_release_vf(struct kref *ref)
56 {
57 	struct ice_vf *vf = container_of(ref, struct ice_vf, refcnt);
58 
59 	pci_dev_put(vf->vfdev);
60 
61 	vf->vf_ops->free(vf);
62 }
63 
64 /**
65  * ice_put_vf - Release a reference to a VF
66  * @vf: the VF structure to decrease reference count on
67  *
68  * Decrease the reference count for a VF, and free the entry if it is no
69  * longer in use.
70  *
71  * This must be called after ice_get_vf_by_id() once the reference to the VF
72  * structure is no longer used. Otherwise, the VF structure will never be
73  * freed.
74  */
75 void ice_put_vf(struct ice_vf *vf)
76 {
77 	kref_put(&vf->refcnt, ice_release_vf);
78 }
79 
80 /**
81  * ice_has_vfs - Return true if the PF has any associated VFs
82  * @pf: the PF private structure
83  *
84  * Return whether or not the PF has any allocated VFs.
85  *
86  * Note that this function only guarantees that there are no VFs at the point
87  * of calling it. It does not guarantee that no more VFs will be added.
88  */
89 bool ice_has_vfs(struct ice_pf *pf)
90 {
91 	/* A simple check that the hash table is not empty does not require
92 	 * the mutex or rcu_read_lock.
93 	 */
94 	return !hash_empty(pf->vfs.table);
95 }
96 
97 /**
98  * ice_get_num_vfs - Get number of allocated VFs
99  * @pf: the PF private structure
100  *
101  * Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
102  * to be contiguous. Do not assume that a VF ID is guaranteed to be less than
103  * the output of this function.
104  */
105 u16 ice_get_num_vfs(struct ice_pf *pf)
106 {
107 	struct ice_vf *vf;
108 	unsigned int bkt;
109 	u16 num_vfs = 0;
110 
111 	rcu_read_lock();
112 	ice_for_each_vf_rcu(pf, bkt, vf)
113 		num_vfs++;
114 	rcu_read_unlock();
115 
116 	return num_vfs;
117 }
118 
119 /**
120  * ice_get_vf_vsi - get VF's VSI based on the stored index
121  * @vf: VF used to get VSI
122  */
123 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
124 {
125 	if (vf->lan_vsi_idx == ICE_NO_VSI)
126 		return NULL;
127 
128 	return vf->pf->vsi[vf->lan_vsi_idx];
129 }
130 
131 /**
132  * ice_is_vf_disabled
133  * @vf: pointer to the VF info
134  *
135  * If the PF has been disabled, there is no need resetting VF until PF is
136  * active again. Similarly, if the VF has been disabled, this means something
137  * else is resetting the VF, so we shouldn't continue.
138  *
139  * Returns true if the caller should consider the VF as disabled whether
140  * because that single VF is explicitly disabled or because the PF is
141  * currently disabled.
142  */
143 bool ice_is_vf_disabled(struct ice_vf *vf)
144 {
145 	struct ice_pf *pf = vf->pf;
146 
147 	return (test_bit(ICE_VF_DIS, pf->state) ||
148 		test_bit(ICE_VF_STATE_DIS, vf->vf_states));
149 }
150 
151 /**
152  * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
153  * @vf: The VF being resseting
154  *
155  * The max poll time is about ~800ms, which is about the maximum time it takes
156  * for a VF to be reset and/or a VF driver to be removed.
157  */
158 static void ice_wait_on_vf_reset(struct ice_vf *vf)
159 {
160 	int i;
161 
162 	for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
163 		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
164 			break;
165 		msleep(ICE_MAX_VF_RESET_SLEEP_MS);
166 	}
167 }
168 
169 /**
170  * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
171  * @vf: VF to check if it's ready to be configured/queried
172  *
173  * The purpose of this function is to make sure the VF is not in reset, not
174  * disabled, and initialized so it can be configured and/or queried by a host
175  * administrator.
176  */
177 int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
178 {
179 	ice_wait_on_vf_reset(vf);
180 
181 	if (ice_is_vf_disabled(vf))
182 		return -EINVAL;
183 
184 	if (ice_check_vf_init(vf))
185 		return -EBUSY;
186 
187 	return 0;
188 }
189 
190 /**
191  * ice_trigger_vf_reset - Reset a VF on HW
192  * @vf: pointer to the VF structure
193  * @is_vflr: true if VFLR was issued, false if not
194  * @is_pfr: true if the reset was triggered due to a previous PFR
195  *
196  * Trigger hardware to start a reset for a particular VF. Expects the caller
197  * to wait the proper amount of time to allow hardware to reset the VF before
198  * it cleans up and restores VF functionality.
199  */
200 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
201 {
202 	/* Inform VF that it is no longer active, as a warning */
203 	clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
204 
205 	/* Disable VF's configuration API during reset. The flag is re-enabled
206 	 * when it's safe again to access VF's VSI.
207 	 */
208 	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
209 
210 	/* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
211 	 * needs to clear them in the case of VFR/VFLR. If this is done for
212 	 * PFR, it can mess up VF resets because the VF driver may already
213 	 * have started cleanup by the time we get here.
214 	 */
215 	if (!is_pfr)
216 		vf->vf_ops->clear_mbx_register(vf);
217 
218 	vf->vf_ops->trigger_reset_register(vf, is_vflr);
219 }
220 
221 static void ice_vf_clear_counters(struct ice_vf *vf)
222 {
223 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
224 
225 	if (vsi)
226 		vsi->num_vlan = 0;
227 
228 	vf->num_mac = 0;
229 	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
230 	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
231 }
232 
233 /**
234  * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
235  * @vf: VF to perform pre VSI rebuild tasks
236  *
237  * These tasks are items that don't need to be amortized since they are most
238  * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
239  */
240 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
241 {
242 	/* Close any IRQ mapping now */
243 	if (vf->vf_ops->irq_close)
244 		vf->vf_ops->irq_close(vf);
245 
246 	ice_vf_clear_counters(vf);
247 	vf->vf_ops->clear_reset_trigger(vf);
248 }
249 
250 /**
251  * ice_vf_reconfig_vsi - Reconfigure a VF VSI with the device
252  * @vf: VF to reconfigure the VSI for
253  *
254  * This is called when a single VF is being reset (i.e. VVF, VFLR, host VF
255  * configuration change, etc).
256  *
257  * It brings the VSI down and then reconfigures it with the hardware.
258  */
259 int ice_vf_reconfig_vsi(struct ice_vf *vf)
260 {
261 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
262 	struct ice_pf *pf = vf->pf;
263 	int err;
264 
265 	if (WARN_ON(!vsi))
266 		return -EINVAL;
267 
268 	vsi->flags = ICE_VSI_FLAG_NO_INIT;
269 
270 	ice_vsi_decfg(vsi);
271 	ice_fltr_remove_all(vsi);
272 
273 	err = ice_vsi_cfg(vsi);
274 	if (err) {
275 		dev_err(ice_pf_to_dev(pf),
276 			"Failed to reconfigure the VF%u's VSI, error %d\n",
277 			vf->vf_id, err);
278 		return err;
279 	}
280 
281 	return 0;
282 }
283 
284 /**
285  * ice_vf_rebuild_vsi - rebuild the VF's VSI
286  * @vf: VF to rebuild the VSI for
287  *
288  * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
289  * host, PFR, CORER, etc.).
290  *
291  * It reprograms the VSI configuration back into hardware.
292  */
293 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
294 {
295 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
296 	struct ice_pf *pf = vf->pf;
297 
298 	if (WARN_ON(!vsi))
299 		return -EINVAL;
300 
301 	if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT)) {
302 		dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
303 			vf->vf_id);
304 		return -EIO;
305 	}
306 	/* vsi->idx will remain the same in this case so don't update
307 	 * vf->lan_vsi_idx
308 	 */
309 	vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
310 
311 	return 0;
312 }
313 
314 /**
315  * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
316  * @vf: VF to add MAC filters for
317  * @vsi: Pointer to VSI
318  *
319  * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
320  * always re-adds either a VLAN 0 or port VLAN based filter after reset.
321  */
322 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
323 {
324 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
325 	struct device *dev = ice_pf_to_dev(vf->pf);
326 	int err;
327 
328 	if (ice_vf_is_port_vlan_ena(vf)) {
329 		err = vlan_ops->set_port_vlan(vsi, &vf->port_vlan_info);
330 		if (err) {
331 			dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
332 				vf->vf_id, err);
333 			return err;
334 		}
335 
336 		err = vlan_ops->add_vlan(vsi, &vf->port_vlan_info);
337 	} else {
338 		err = ice_vsi_add_vlan_zero(vsi);
339 	}
340 
341 	if (err) {
342 		dev_err(dev, "failed to add VLAN %u filter for VF %u during VF rebuild, error %d\n",
343 			ice_vf_is_port_vlan_ena(vf) ?
344 			ice_vf_get_port_vlan_id(vf) : 0, vf->vf_id, err);
345 		return err;
346 	}
347 
348 	err = vlan_ops->ena_rx_filtering(vsi);
349 	if (err)
350 		dev_warn(dev, "failed to enable Rx VLAN filtering for VF %d VSI %d during VF rebuild, error %d\n",
351 			 vf->vf_id, vsi->idx, err);
352 
353 	return 0;
354 }
355 
356 /**
357  * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
358  * @vf: VF to re-apply the configuration for
359  *
360  * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
361  * needs to re-apply the host configured Tx rate limiting configuration.
362  */
363 static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
364 {
365 	struct device *dev = ice_pf_to_dev(vf->pf);
366 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
367 	int err;
368 
369 	if (WARN_ON(!vsi))
370 		return -EINVAL;
371 
372 	if (vf->min_tx_rate) {
373 		err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
374 		if (err) {
375 			dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
376 				vf->min_tx_rate, vf->vf_id, err);
377 			return err;
378 		}
379 	}
380 
381 	if (vf->max_tx_rate) {
382 		err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
383 		if (err) {
384 			dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
385 				vf->max_tx_rate, vf->vf_id, err);
386 			return err;
387 		}
388 	}
389 
390 	return 0;
391 }
392 
393 /**
394  * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
395  * @vf: VF to configure trust setting for
396  */
397 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
398 {
399 	assign_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps, vf->trusted);
400 }
401 
402 /**
403  * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
404  * @vf: VF to add MAC filters for
405  *
406  * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
407  * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
408  */
409 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
410 {
411 	struct device *dev = ice_pf_to_dev(vf->pf);
412 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
413 	u8 broadcast[ETH_ALEN];
414 	int status;
415 
416 	if (WARN_ON(!vsi))
417 		return -EINVAL;
418 
419 	if (ice_is_eswitch_mode_switchdev(vf->pf))
420 		return 0;
421 
422 	eth_broadcast_addr(broadcast);
423 	status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
424 	if (status) {
425 		dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
426 			vf->vf_id, status);
427 		return status;
428 	}
429 
430 	vf->num_mac++;
431 
432 	if (is_valid_ether_addr(vf->hw_lan_addr)) {
433 		status = ice_fltr_add_mac(vsi, vf->hw_lan_addr,
434 					  ICE_FWD_TO_VSI);
435 		if (status) {
436 			dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
437 				&vf->hw_lan_addr[0], vf->vf_id,
438 				status);
439 			return status;
440 		}
441 		vf->num_mac++;
442 
443 		ether_addr_copy(vf->dev_lan_addr, vf->hw_lan_addr);
444 	}
445 
446 	return 0;
447 }
448 
449 /**
450  * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
451  * @vsi: Pointer to VSI
452  *
453  * This function moves VSI into corresponding scheduler aggregator node
454  * based on cached value of "aggregator node info" per VSI
455  */
456 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
457 {
458 	struct ice_pf *pf = vsi->back;
459 	struct device *dev;
460 	int status;
461 
462 	if (!vsi->agg_node)
463 		return;
464 
465 	dev = ice_pf_to_dev(pf);
466 	if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
467 		dev_dbg(dev,
468 			"agg_id %u already has reached max_num_vsis %u\n",
469 			vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
470 		return;
471 	}
472 
473 	status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
474 				     vsi->idx, vsi->tc_cfg.ena_tc);
475 	if (status)
476 		dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
477 			vsi->idx, vsi->agg_node->agg_id);
478 	else
479 		vsi->agg_node->num_vsis++;
480 }
481 
482 /**
483  * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
484  * @vf: VF to rebuild host configuration on
485  */
486 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
487 {
488 	struct device *dev = ice_pf_to_dev(vf->pf);
489 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
490 
491 	if (WARN_ON(!vsi))
492 		return;
493 
494 	ice_vf_set_host_trust_cfg(vf);
495 
496 	if (ice_vf_rebuild_host_mac_cfg(vf))
497 		dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
498 			vf->vf_id);
499 
500 	if (ice_vf_rebuild_host_vlan_cfg(vf, vsi))
501 		dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
502 			vf->vf_id);
503 
504 	if (ice_vf_rebuild_host_tx_rate_cfg(vf))
505 		dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
506 			vf->vf_id);
507 
508 	if (ice_vsi_apply_spoofchk(vsi, vf->spoofchk))
509 		dev_err(dev, "failed to rebuild spoofchk configuration for VF %d\n",
510 			vf->vf_id);
511 
512 	/* rebuild aggregator node config for main VF VSI */
513 	ice_vf_rebuild_aggregator_node_cfg(vsi);
514 }
515 
516 /**
517  * ice_set_vf_state_qs_dis - Set VF queues state to disabled
518  * @vf: pointer to the VF structure
519  */
520 static void ice_set_vf_state_qs_dis(struct ice_vf *vf)
521 {
522 	/* Clear Rx/Tx enabled queues flag */
523 	bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
524 	bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
525 	clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
526 }
527 
528 /**
529  * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
530  * @vf: VF to set in initialized state
531  *
532  * After this function the VF will be ready to receive/handle the
533  * VIRTCHNL_OP_GET_VF_RESOURCES message
534  */
535 static void ice_vf_set_initialized(struct ice_vf *vf)
536 {
537 	ice_set_vf_state_qs_dis(vf);
538 	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
539 	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
540 	clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
541 	set_bit(ICE_VF_STATE_INIT, vf->vf_states);
542 	memset(&vf->vlan_v2_caps, 0, sizeof(vf->vlan_v2_caps));
543 }
544 
545 /**
546  * ice_vf_post_vsi_rebuild - Reset tasks that occur after VSI rebuild
547  * @vf: the VF being reset
548  *
549  * Perform reset tasks which must occur after the VSI has been re-created or
550  * rebuilt during a VF reset.
551  */
552 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
553 {
554 	ice_vf_rebuild_host_cfg(vf);
555 	ice_vf_set_initialized(vf);
556 
557 	vf->vf_ops->post_vsi_rebuild(vf);
558 }
559 
560 /**
561  * ice_is_any_vf_in_unicast_promisc - check if any VF(s)
562  * are in unicast promiscuous mode
563  * @pf: PF structure for accessing VF(s)
564  *
565  * Return false if no VF(s) are in unicast promiscuous mode,
566  * else return true
567  */
568 bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
569 {
570 	bool is_vf_promisc = false;
571 	struct ice_vf *vf;
572 	unsigned int bkt;
573 
574 	rcu_read_lock();
575 	ice_for_each_vf_rcu(pf, bkt, vf) {
576 		/* found a VF that has promiscuous mode configured */
577 		if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
578 			is_vf_promisc = true;
579 			break;
580 		}
581 	}
582 	rcu_read_unlock();
583 
584 	return is_vf_promisc;
585 }
586 
587 /**
588  * ice_vf_get_promisc_masks - Calculate masks for promiscuous modes
589  * @vf: the VF pointer
590  * @vsi: the VSI to configure
591  * @ucast_m: promiscuous mask to apply to unicast
592  * @mcast_m: promiscuous mask to apply to multicast
593  *
594  * Decide which mask should be used for unicast and multicast filter,
595  * based on presence of VLANs
596  */
597 void
598 ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
599 			 u8 *ucast_m, u8 *mcast_m)
600 {
601 	if (ice_vf_is_port_vlan_ena(vf) ||
602 	    ice_vsi_has_non_zero_vlans(vsi)) {
603 		*mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
604 		*ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
605 	} else {
606 		*mcast_m = ICE_MCAST_PROMISC_BITS;
607 		*ucast_m = ICE_UCAST_PROMISC_BITS;
608 	}
609 }
610 
611 /**
612  * ice_vf_clear_all_promisc_modes - Clear promisc/allmulticast on VF VSI
613  * @vf: the VF pointer
614  * @vsi: the VSI to configure
615  *
616  * Clear all promiscuous/allmulticast filters for a VF
617  */
618 static int
619 ice_vf_clear_all_promisc_modes(struct ice_vf *vf, struct ice_vsi *vsi)
620 {
621 	struct ice_pf *pf = vf->pf;
622 	u8 ucast_m, mcast_m;
623 	int ret = 0;
624 
625 	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
626 	if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
627 		if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
628 			if (ice_is_dflt_vsi_in_use(vsi->port_info))
629 				ret = ice_clear_dflt_vsi(vsi);
630 		} else {
631 			ret = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
632 		}
633 
634 		if (ret) {
635 			dev_err(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode failed\n");
636 		} else {
637 			clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
638 			dev_info(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode succeeded\n");
639 		}
640 	}
641 
642 	if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
643 		ret = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
644 		if (ret) {
645 			dev_err(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode failed\n");
646 		} else {
647 			clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
648 			dev_info(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode succeeded\n");
649 		}
650 	}
651 	return ret;
652 }
653 
654 /**
655  * ice_vf_set_vsi_promisc - Enable promiscuous mode for a VF VSI
656  * @vf: the VF to configure
657  * @vsi: the VF's VSI
658  * @promisc_m: the promiscuous mode to enable
659  */
660 int
661 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
662 {
663 	struct ice_hw *hw = &vsi->back->hw;
664 	int status;
665 
666 	if (ice_vf_is_port_vlan_ena(vf))
667 		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
668 						  ice_vf_get_port_vlan_id(vf));
669 	else if (ice_vsi_has_non_zero_vlans(vsi))
670 		status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
671 	else
672 		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
673 
674 	if (status && status != -EEXIST) {
675 		dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
676 			vf->vf_id, status);
677 		return status;
678 	}
679 
680 	return 0;
681 }
682 
683 /**
684  * ice_vf_clear_vsi_promisc - Disable promiscuous mode for a VF VSI
685  * @vf: the VF to configure
686  * @vsi: the VF's VSI
687  * @promisc_m: the promiscuous mode to disable
688  */
689 int
690 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
691 {
692 	struct ice_hw *hw = &vsi->back->hw;
693 	int status;
694 
695 	if (ice_vf_is_port_vlan_ena(vf))
696 		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
697 						    ice_vf_get_port_vlan_id(vf));
698 	else if (ice_vsi_has_non_zero_vlans(vsi))
699 		status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
700 	else
701 		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
702 
703 	if (status && status != -ENOENT) {
704 		dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
705 			vf->vf_id, status);
706 		return status;
707 	}
708 
709 	return 0;
710 }
711 
712 /**
713  * ice_reset_all_vfs - reset all allocated VFs in one go
714  * @pf: pointer to the PF structure
715  *
716  * Reset all VFs at once, in response to a PF or other device reset.
717  *
718  * First, tell the hardware to reset each VF, then do all the waiting in one
719  * chunk, and finally finish restoring each VF after the wait. This is useful
720  * during PF routines which need to reset all VFs, as otherwise it must perform
721  * these resets in a serialized fashion.
722  */
723 void ice_reset_all_vfs(struct ice_pf *pf)
724 {
725 	struct device *dev = ice_pf_to_dev(pf);
726 	struct ice_hw *hw = &pf->hw;
727 	struct ice_vf *vf;
728 	unsigned int bkt;
729 
730 	/* If we don't have any VFs, then there is nothing to reset */
731 	if (!ice_has_vfs(pf))
732 		return;
733 
734 	mutex_lock(&pf->vfs.table_lock);
735 
736 	/* clear all malicious info if the VFs are getting reset */
737 	ice_for_each_vf(pf, bkt, vf)
738 		ice_mbx_clear_malvf(&vf->mbx_info);
739 
740 	/* If VFs have been disabled, there is no need to reset */
741 	if (test_and_set_bit(ICE_VF_DIS, pf->state)) {
742 		mutex_unlock(&pf->vfs.table_lock);
743 		return;
744 	}
745 
746 	/* Begin reset on all VFs at once */
747 	ice_for_each_vf(pf, bkt, vf)
748 		ice_trigger_vf_reset(vf, true, true);
749 
750 	/* HW requires some time to make sure it can flush the FIFO for a VF
751 	 * when it resets it. Now that we've triggered all of the VFs, iterate
752 	 * the table again and wait for each VF to complete.
753 	 */
754 	ice_for_each_vf(pf, bkt, vf) {
755 		if (!vf->vf_ops->poll_reset_status(vf)) {
756 			/* Display a warning if at least one VF didn't manage
757 			 * to reset in time, but continue on with the
758 			 * operation.
759 			 */
760 			dev_warn(dev, "VF %u reset check timeout\n", vf->vf_id);
761 			break;
762 		}
763 	}
764 
765 	/* free VF resources to begin resetting the VSI state */
766 	ice_for_each_vf(pf, bkt, vf) {
767 		mutex_lock(&vf->cfg_lock);
768 
769 		ice_eswitch_detach(pf, vf);
770 		vf->driver_caps = 0;
771 		ice_vc_set_default_allowlist(vf);
772 
773 		ice_vf_fdir_exit(vf);
774 		ice_vf_fdir_init(vf);
775 		/* clean VF control VSI when resetting VFs since it should be
776 		 * setup only when VF creates its first FDIR rule.
777 		 */
778 		if (vf->ctrl_vsi_idx != ICE_NO_VSI)
779 			ice_vf_ctrl_invalidate_vsi(vf);
780 
781 		ice_vf_pre_vsi_rebuild(vf);
782 		ice_vf_rebuild_vsi(vf);
783 		ice_vf_post_vsi_rebuild(vf);
784 
785 		ice_eswitch_attach(pf, vf);
786 
787 		mutex_unlock(&vf->cfg_lock);
788 	}
789 
790 	ice_flush(hw);
791 	clear_bit(ICE_VF_DIS, pf->state);
792 
793 	mutex_unlock(&pf->vfs.table_lock);
794 }
795 
796 /**
797  * ice_notify_vf_reset - Notify VF of a reset event
798  * @vf: pointer to the VF structure
799  */
800 static void ice_notify_vf_reset(struct ice_vf *vf)
801 {
802 	struct ice_hw *hw = &vf->pf->hw;
803 	struct virtchnl_pf_event pfe;
804 
805 	/* Bail out if VF is in disabled state, neither initialized, nor active
806 	 * state - otherwise proceed with notifications
807 	 */
808 	if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
809 	     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
810 	    test_bit(ICE_VF_STATE_DIS, vf->vf_states))
811 		return;
812 
813 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
814 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
815 	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
816 			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
817 			      NULL);
818 }
819 
820 /**
821  * ice_reset_vf - Reset a particular VF
822  * @vf: pointer to the VF structure
823  * @flags: flags controlling behavior of the reset
824  *
825  * Flags:
826  *   ICE_VF_RESET_VFLR - Indicates a reset is due to VFLR event
827  *   ICE_VF_RESET_NOTIFY - Send VF a notification prior to reset
828  *   ICE_VF_RESET_LOCK - Acquire VF cfg_lock before resetting
829  *
830  * Returns 0 if the VF is currently in reset, if resets are disabled, or if
831  * the VF resets successfully. Returns an error code if the VF fails to
832  * rebuild.
833  */
834 int ice_reset_vf(struct ice_vf *vf, u32 flags)
835 {
836 	struct ice_pf *pf = vf->pf;
837 	struct ice_lag *lag;
838 	struct ice_vsi *vsi;
839 	u8 act_prt, pri_prt;
840 	struct device *dev;
841 	int err = 0;
842 	bool rsd;
843 
844 	dev = ice_pf_to_dev(pf);
845 	act_prt = ICE_LAG_INVALID_PORT;
846 	pri_prt = pf->hw.port_info->lport;
847 
848 	if (flags & ICE_VF_RESET_NOTIFY)
849 		ice_notify_vf_reset(vf);
850 
851 	if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
852 		dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
853 			vf->vf_id);
854 		return 0;
855 	}
856 
857 	if (flags & ICE_VF_RESET_LOCK)
858 		mutex_lock(&vf->cfg_lock);
859 	else
860 		lockdep_assert_held(&vf->cfg_lock);
861 
862 	lag = pf->lag;
863 	mutex_lock(&pf->lag_mutex);
864 	if (lag && lag->bonded && lag->primary) {
865 		act_prt = lag->active_port;
866 		if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
867 		    lag->upper_netdev)
868 			ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
869 		else
870 			act_prt = ICE_LAG_INVALID_PORT;
871 	}
872 
873 	if (ice_is_vf_disabled(vf)) {
874 		vsi = ice_get_vf_vsi(vf);
875 		if (!vsi) {
876 			dev_dbg(dev, "VF is already removed\n");
877 			err = -EINVAL;
878 			goto out_unlock;
879 		}
880 		ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
881 
882 		if (ice_vsi_is_rx_queue_active(vsi))
883 			ice_vsi_stop_all_rx_rings(vsi);
884 
885 		dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
886 			vf->vf_id);
887 		goto out_unlock;
888 	}
889 
890 	/* Set VF disable bit state here, before triggering reset */
891 	set_bit(ICE_VF_STATE_DIS, vf->vf_states);
892 	ice_trigger_vf_reset(vf, flags & ICE_VF_RESET_VFLR, false);
893 
894 	vsi = ice_get_vf_vsi(vf);
895 	if (WARN_ON(!vsi)) {
896 		err = -EIO;
897 		goto out_unlock;
898 	}
899 
900 	ice_dis_vf_qs(vf);
901 
902 	/* Call Disable LAN Tx queue AQ whether or not queues are
903 	 * enabled. This is needed for successful completion of VFR.
904 	 */
905 	ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
906 			NULL, vf->vf_ops->reset_type, vf->vf_id, NULL);
907 
908 	/* poll VPGEN_VFRSTAT reg to make sure
909 	 * that reset is complete
910 	 */
911 	rsd = vf->vf_ops->poll_reset_status(vf);
912 
913 	/* Display a warning if VF didn't manage to reset in time, but need to
914 	 * continue on with the operation.
915 	 */
916 	if (!rsd)
917 		dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
918 
919 	vf->driver_caps = 0;
920 	ice_vc_set_default_allowlist(vf);
921 
922 	/* disable promiscuous modes in case they were enabled
923 	 * ignore any error if disabling process failed
924 	 */
925 	ice_vf_clear_all_promisc_modes(vf, vsi);
926 
927 	ice_vf_fdir_exit(vf);
928 	ice_vf_fdir_init(vf);
929 	/* clean VF control VSI when resetting VF since it should be setup
930 	 * only when VF creates its first FDIR rule.
931 	 */
932 	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
933 		ice_vf_ctrl_vsi_release(vf);
934 
935 	ice_vf_pre_vsi_rebuild(vf);
936 
937 	if (ice_vf_reconfig_vsi(vf)) {
938 		dev_err(dev, "Failed to release and setup the VF%u's VSI\n",
939 			vf->vf_id);
940 		err = -EFAULT;
941 		goto out_unlock;
942 	}
943 
944 	ice_vf_post_vsi_rebuild(vf);
945 	vsi = ice_get_vf_vsi(vf);
946 	if (WARN_ON(!vsi)) {
947 		err = -EINVAL;
948 		goto out_unlock;
949 	}
950 
951 	ice_eswitch_update_repr(vf->repr_id, vsi);
952 
953 	/* if the VF has been reset allow it to come up again */
954 	ice_mbx_clear_malvf(&vf->mbx_info);
955 
956 out_unlock:
957 	if (lag && lag->bonded && lag->primary &&
958 	    act_prt != ICE_LAG_INVALID_PORT)
959 		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
960 	mutex_unlock(&pf->lag_mutex);
961 
962 	if (flags & ICE_VF_RESET_LOCK)
963 		mutex_unlock(&vf->cfg_lock);
964 
965 	return err;
966 }
967 
968 /**
969  * ice_set_vf_state_dis - Set VF state to disabled
970  * @vf: pointer to the VF structure
971  */
972 void ice_set_vf_state_dis(struct ice_vf *vf)
973 {
974 	ice_set_vf_state_qs_dis(vf);
975 	vf->vf_ops->clear_reset_state(vf);
976 }
977 
978 /* Private functions only accessed from other virtualization files */
979 
980 /**
981  * ice_initialize_vf_entry - Initialize a VF entry
982  * @vf: pointer to the VF structure
983  */
984 void ice_initialize_vf_entry(struct ice_vf *vf)
985 {
986 	struct ice_pf *pf = vf->pf;
987 	struct ice_vfs *vfs;
988 
989 	vfs = &pf->vfs;
990 
991 	/* assign default capabilities */
992 	vf->spoofchk = true;
993 	ice_vc_set_default_allowlist(vf);
994 	ice_virtchnl_set_dflt_ops(vf);
995 
996 	/* set default number of MSI-X */
997 	vf->num_msix = vfs->num_msix_per;
998 	vf->num_vf_qs = vfs->num_qps_per;
999 
1000 	/* ctrl_vsi_idx will be set to a valid value only when iAVF
1001 	 * creates its first fdir rule.
1002 	 */
1003 	ice_vf_ctrl_invalidate_vsi(vf);
1004 	ice_vf_fdir_init(vf);
1005 
1006 	/* Initialize mailbox info for this VF */
1007 	ice_mbx_init_vf_info(&pf->hw, &vf->mbx_info);
1008 
1009 	mutex_init(&vf->cfg_lock);
1010 }
1011 
1012 /**
1013  * ice_dis_vf_qs - Disable the VF queues
1014  * @vf: pointer to the VF structure
1015  */
1016 void ice_dis_vf_qs(struct ice_vf *vf)
1017 {
1018 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1019 
1020 	if (WARN_ON(!vsi))
1021 		return;
1022 
1023 	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
1024 	ice_vsi_stop_all_rx_rings(vsi);
1025 	ice_set_vf_state_qs_dis(vf);
1026 }
1027 
1028 /**
1029  * ice_err_to_virt_err - translate errors for VF return code
1030  * @err: error return code
1031  */
1032 enum virtchnl_status_code ice_err_to_virt_err(int err)
1033 {
1034 	switch (err) {
1035 	case 0:
1036 		return VIRTCHNL_STATUS_SUCCESS;
1037 	case -EINVAL:
1038 	case -ENODEV:
1039 		return VIRTCHNL_STATUS_ERR_PARAM;
1040 	case -ENOMEM:
1041 		return VIRTCHNL_STATUS_ERR_NO_MEMORY;
1042 	case -EALREADY:
1043 	case -EBUSY:
1044 	case -EIO:
1045 	case -ENOSPC:
1046 		return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1047 	default:
1048 		return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
1049 	}
1050 }
1051 
1052 /**
1053  * ice_check_vf_init - helper to check if VF init complete
1054  * @vf: the pointer to the VF to check
1055  */
1056 int ice_check_vf_init(struct ice_vf *vf)
1057 {
1058 	struct ice_pf *pf = vf->pf;
1059 
1060 	if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
1061 		dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
1062 			vf->vf_id);
1063 		return -EBUSY;
1064 	}
1065 	return 0;
1066 }
1067 
1068 /**
1069  * ice_vf_get_port_info - Get the VF's port info structure
1070  * @vf: VF used to get the port info structure for
1071  */
1072 struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
1073 {
1074 	return vf->pf->hw.port_info;
1075 }
1076 
1077 /**
1078  * ice_cfg_mac_antispoof - Configure MAC antispoof checking behavior
1079  * @vsi: the VSI to configure
1080  * @enable: whether to enable or disable the spoof checking
1081  *
1082  * Configure a VSI to enable (or disable) spoof checking behavior.
1083  */
1084 static int ice_cfg_mac_antispoof(struct ice_vsi *vsi, bool enable)
1085 {
1086 	struct ice_vsi_ctx *ctx;
1087 	int err;
1088 
1089 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1090 	if (!ctx)
1091 		return -ENOMEM;
1092 
1093 	ctx->info.sec_flags = vsi->info.sec_flags;
1094 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1095 
1096 	if (enable)
1097 		ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
1098 	else
1099 		ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
1100 
1101 	err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
1102 	if (err)
1103 		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx MAC anti-spoof %s for VSI %d, error %d\n",
1104 			enable ? "ON" : "OFF", vsi->vsi_num, err);
1105 	else
1106 		vsi->info.sec_flags = ctx->info.sec_flags;
1107 
1108 	kfree(ctx);
1109 
1110 	return err;
1111 }
1112 
1113 /**
1114  * ice_vsi_ena_spoofchk - enable Tx spoof checking for this VSI
1115  * @vsi: VSI to enable Tx spoof checking for
1116  */
1117 static int ice_vsi_ena_spoofchk(struct ice_vsi *vsi)
1118 {
1119 	struct ice_vsi_vlan_ops *vlan_ops;
1120 	int err = 0;
1121 
1122 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1123 
1124 	/* Allow VF with VLAN 0 only to send all tagged traffic */
1125 	if (vsi->type != ICE_VSI_VF || ice_vsi_has_non_zero_vlans(vsi)) {
1126 		err = vlan_ops->ena_tx_filtering(vsi);
1127 		if (err)
1128 			return err;
1129 	}
1130 
1131 	return ice_cfg_mac_antispoof(vsi, true);
1132 }
1133 
1134 /**
1135  * ice_vsi_dis_spoofchk - disable Tx spoof checking for this VSI
1136  * @vsi: VSI to disable Tx spoof checking for
1137  */
1138 static int ice_vsi_dis_spoofchk(struct ice_vsi *vsi)
1139 {
1140 	struct ice_vsi_vlan_ops *vlan_ops;
1141 	int err;
1142 
1143 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1144 
1145 	err = vlan_ops->dis_tx_filtering(vsi);
1146 	if (err)
1147 		return err;
1148 
1149 	return ice_cfg_mac_antispoof(vsi, false);
1150 }
1151 
1152 /**
1153  * ice_vsi_apply_spoofchk - Apply Tx spoof checking setting to a VSI
1154  * @vsi: VSI associated to the VF
1155  * @enable: whether to enable or disable the spoof checking
1156  */
1157 int ice_vsi_apply_spoofchk(struct ice_vsi *vsi, bool enable)
1158 {
1159 	int err;
1160 
1161 	if (enable)
1162 		err = ice_vsi_ena_spoofchk(vsi);
1163 	else
1164 		err = ice_vsi_dis_spoofchk(vsi);
1165 
1166 	return err;
1167 }
1168 
1169 /**
1170  * ice_is_vf_trusted
1171  * @vf: pointer to the VF info
1172  */
1173 bool ice_is_vf_trusted(struct ice_vf *vf)
1174 {
1175 	return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1176 }
1177 
1178 /**
1179  * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
1180  * @vf: the VF to check
1181  *
1182  * Returns true if the VF has no Rx and no Tx queues enabled and returns false
1183  * otherwise
1184  */
1185 bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
1186 {
1187 	return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
1188 		!bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
1189 }
1190 
1191 /**
1192  * ice_is_vf_link_up - check if the VF's link is up
1193  * @vf: VF to check if link is up
1194  */
1195 bool ice_is_vf_link_up(struct ice_vf *vf)
1196 {
1197 	struct ice_port_info *pi = ice_vf_get_port_info(vf);
1198 
1199 	if (ice_check_vf_init(vf))
1200 		return false;
1201 
1202 	if (ice_vf_has_no_qs_ena(vf))
1203 		return false;
1204 	else if (vf->link_forced)
1205 		return vf->link_up;
1206 	else
1207 		return pi->phy.link_info.link_info &
1208 			ICE_AQ_LINK_UP;
1209 }
1210 
1211 /**
1212  * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
1213  * @vf: VF that control VSI is being invalidated on
1214  */
1215 void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
1216 {
1217 	vf->ctrl_vsi_idx = ICE_NO_VSI;
1218 }
1219 
1220 /**
1221  * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
1222  * @vf: VF that control VSI is being released on
1223  */
1224 void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
1225 {
1226 	ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
1227 	ice_vf_ctrl_invalidate_vsi(vf);
1228 }
1229 
1230 /**
1231  * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
1232  * @vf: VF to setup control VSI for
1233  *
1234  * Returns pointer to the successfully allocated VSI struct on success,
1235  * otherwise returns NULL on failure.
1236  */
1237 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
1238 {
1239 	struct ice_vsi_cfg_params params = {};
1240 	struct ice_pf *pf = vf->pf;
1241 	struct ice_vsi *vsi;
1242 
1243 	params.type = ICE_VSI_CTRL;
1244 	params.port_info = ice_vf_get_port_info(vf);
1245 	params.vf = vf;
1246 	params.flags = ICE_VSI_FLAG_INIT;
1247 
1248 	vsi = ice_vsi_setup(pf, &params);
1249 	if (!vsi) {
1250 		dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
1251 		ice_vf_ctrl_invalidate_vsi(vf);
1252 	}
1253 
1254 	return vsi;
1255 }
1256 
1257 /**
1258  * ice_vf_init_host_cfg - Initialize host admin configuration
1259  * @vf: VF to initialize
1260  * @vsi: the VSI created at initialization
1261  *
1262  * Initialize the VF host configuration. Called during VF creation to setup
1263  * VLAN 0, add the VF VSI broadcast filter, and setup spoof checking. It
1264  * should only be called during VF creation.
1265  */
1266 int ice_vf_init_host_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
1267 {
1268 	struct ice_vsi_vlan_ops *vlan_ops;
1269 	struct ice_pf *pf = vf->pf;
1270 	u8 broadcast[ETH_ALEN];
1271 	struct device *dev;
1272 	int err;
1273 
1274 	dev = ice_pf_to_dev(pf);
1275 
1276 	err = ice_vsi_add_vlan_zero(vsi);
1277 	if (err) {
1278 		dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1279 			 vf->vf_id);
1280 		return err;
1281 	}
1282 
1283 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1284 	err = vlan_ops->ena_rx_filtering(vsi);
1285 	if (err) {
1286 		dev_warn(dev, "Failed to enable Rx VLAN filtering for VF %d\n",
1287 			 vf->vf_id);
1288 		return err;
1289 	}
1290 
1291 	eth_broadcast_addr(broadcast);
1292 	err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1293 	if (err) {
1294 		dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %d\n",
1295 			vf->vf_id, err);
1296 		return err;
1297 	}
1298 
1299 	vf->num_mac = 1;
1300 
1301 	err = ice_vsi_apply_spoofchk(vsi, vf->spoofchk);
1302 	if (err) {
1303 		dev_warn(dev, "Failed to initialize spoofchk setting for VF %d\n",
1304 			 vf->vf_id);
1305 		return err;
1306 	}
1307 
1308 	return 0;
1309 }
1310 
1311 /**
1312  * ice_vf_invalidate_vsi - invalidate vsi_idx to remove VSI access
1313  * @vf: VF to remove access to VSI for
1314  */
1315 void ice_vf_invalidate_vsi(struct ice_vf *vf)
1316 {
1317 	vf->lan_vsi_idx = ICE_NO_VSI;
1318 }
1319 
1320 /**
1321  * ice_vf_vsi_release - Release the VF VSI and invalidate indexes
1322  * @vf: pointer to the VF structure
1323  *
1324  * Release the VF associated with this VSI and then invalidate the VSI
1325  * indexes.
1326  */
1327 void ice_vf_vsi_release(struct ice_vf *vf)
1328 {
1329 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1330 
1331 	if (WARN_ON(!vsi))
1332 		return;
1333 
1334 	ice_vsi_release(vsi);
1335 	ice_vf_invalidate_vsi(vf);
1336 }
1337 
1338 /**
1339  * ice_get_vf_ctrl_vsi - Get first VF control VSI pointer
1340  * @pf: the PF private structure
1341  * @vsi: pointer to the VSI
1342  *
1343  * Return first found VF control VSI other than the vsi
1344  * passed by parameter. This function is used to determine
1345  * whether new resources have to be allocated for control VSI
1346  * or they can be shared with existing one.
1347  *
1348  * Return found VF control VSI pointer other itself. Return
1349  * NULL Otherwise.
1350  *
1351  */
1352 struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
1353 {
1354 	struct ice_vsi *ctrl_vsi = NULL;
1355 	struct ice_vf *vf;
1356 	unsigned int bkt;
1357 
1358 	rcu_read_lock();
1359 	ice_for_each_vf_rcu(pf, bkt, vf) {
1360 		if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1361 			ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
1362 			break;
1363 		}
1364 	}
1365 
1366 	rcu_read_unlock();
1367 	return ctrl_vsi;
1368 }
1369