xref: /linux/drivers/net/ethernet/intel/ice/ice_lib.c (revision a37fbe666c016fd89e4460d0ebfcea05baba46dc)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_base.h"
6 #include "ice_flow.h"
7 #include "ice_lib.h"
8 #include "ice_fltr.h"
9 #include "ice_dcb_lib.h"
10 #include "ice_devlink.h"
11 #include "ice_vsi_vlan_ops.h"
12 
13 /**
14  * ice_vsi_type_str - maps VSI type enum to string equivalents
15  * @vsi_type: VSI type enum
16  */
17 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
18 {
19 	switch (vsi_type) {
20 	case ICE_VSI_PF:
21 		return "ICE_VSI_PF";
22 	case ICE_VSI_VF:
23 		return "ICE_VSI_VF";
24 	case ICE_VSI_CTRL:
25 		return "ICE_VSI_CTRL";
26 	case ICE_VSI_CHNL:
27 		return "ICE_VSI_CHNL";
28 	case ICE_VSI_LB:
29 		return "ICE_VSI_LB";
30 	default:
31 		return "unknown";
32 	}
33 }
34 
35 /**
36  * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
37  * @vsi: the VSI being configured
38  * @ena: start or stop the Rx rings
39  *
40  * First enable/disable all of the Rx rings, flush any remaining writes, and
41  * then verify that they have all been enabled/disabled successfully. This will
42  * let all of the register writes complete when enabling/disabling the Rx rings
43  * before waiting for the change in hardware to complete.
44  */
45 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
46 {
47 	int ret = 0;
48 	u16 i;
49 
50 	ice_for_each_rxq(vsi, i)
51 		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
52 
53 	ice_flush(&vsi->back->hw);
54 
55 	ice_for_each_rxq(vsi, i) {
56 		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
57 		if (ret)
58 			break;
59 	}
60 
61 	return ret;
62 }
63 
64 /**
65  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
66  * @vsi: VSI pointer
67  *
68  * On error: returns error code (negative)
69  * On success: returns 0
70  */
71 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
72 {
73 	struct ice_pf *pf = vsi->back;
74 	struct device *dev;
75 
76 	dev = ice_pf_to_dev(pf);
77 	if (vsi->type == ICE_VSI_CHNL)
78 		return 0;
79 
80 	/* allocate memory for both Tx and Rx ring pointers */
81 	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
82 				     sizeof(*vsi->tx_rings), GFP_KERNEL);
83 	if (!vsi->tx_rings)
84 		return -ENOMEM;
85 
86 	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
87 				     sizeof(*vsi->rx_rings), GFP_KERNEL);
88 	if (!vsi->rx_rings)
89 		goto err_rings;
90 
91 	/* txq_map needs to have enough space to track both Tx (stack) rings
92 	 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
93 	 * so use num_possible_cpus() as we want to always provide XDP ring
94 	 * per CPU, regardless of queue count settings from user that might
95 	 * have come from ethtool's set_channels() callback;
96 	 */
97 	vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
98 				    sizeof(*vsi->txq_map), GFP_KERNEL);
99 
100 	if (!vsi->txq_map)
101 		goto err_txq_map;
102 
103 	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
104 				    sizeof(*vsi->rxq_map), GFP_KERNEL);
105 	if (!vsi->rxq_map)
106 		goto err_rxq_map;
107 
108 	/* There is no need to allocate q_vectors for a loopback VSI. */
109 	if (vsi->type == ICE_VSI_LB)
110 		return 0;
111 
112 	/* allocate memory for q_vector pointers */
113 	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
114 				      sizeof(*vsi->q_vectors), GFP_KERNEL);
115 	if (!vsi->q_vectors)
116 		goto err_vectors;
117 
118 	vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
119 	if (!vsi->af_xdp_zc_qps)
120 		goto err_zc_qps;
121 
122 	return 0;
123 
124 err_zc_qps:
125 	devm_kfree(dev, vsi->q_vectors);
126 err_vectors:
127 	devm_kfree(dev, vsi->rxq_map);
128 err_rxq_map:
129 	devm_kfree(dev, vsi->txq_map);
130 err_txq_map:
131 	devm_kfree(dev, vsi->rx_rings);
132 err_rings:
133 	devm_kfree(dev, vsi->tx_rings);
134 	return -ENOMEM;
135 }
136 
137 /**
138  * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
139  * @vsi: the VSI being configured
140  */
141 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
142 {
143 	switch (vsi->type) {
144 	case ICE_VSI_PF:
145 	case ICE_VSI_CTRL:
146 	case ICE_VSI_LB:
147 		/* a user could change the values of num_[tr]x_desc using
148 		 * ethtool -G so we should keep those values instead of
149 		 * overwriting them with the defaults.
150 		 */
151 		if (!vsi->num_rx_desc)
152 			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
153 		if (!vsi->num_tx_desc)
154 			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
155 		break;
156 	default:
157 		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
158 			vsi->type);
159 		break;
160 	}
161 }
162 
163 /**
164  * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
165  * @vsi: the VSI being configured
166  *
167  * Return 0 on success and a negative value on error
168  */
169 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
170 {
171 	enum ice_vsi_type vsi_type = vsi->type;
172 	struct ice_pf *pf = vsi->back;
173 	struct ice_vf *vf = vsi->vf;
174 
175 	if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
176 		return;
177 
178 	switch (vsi_type) {
179 	case ICE_VSI_PF:
180 		if (vsi->req_txq) {
181 			vsi->alloc_txq = vsi->req_txq;
182 			vsi->num_txq = vsi->req_txq;
183 		} else {
184 			vsi->alloc_txq = min3(pf->num_lan_msix,
185 					      ice_get_avail_txq_count(pf),
186 					      (u16)num_online_cpus());
187 		}
188 
189 		pf->num_lan_tx = vsi->alloc_txq;
190 
191 		/* only 1 Rx queue unless RSS is enabled */
192 		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
193 			vsi->alloc_rxq = 1;
194 		} else {
195 			if (vsi->req_rxq) {
196 				vsi->alloc_rxq = vsi->req_rxq;
197 				vsi->num_rxq = vsi->req_rxq;
198 			} else {
199 				vsi->alloc_rxq = min3(pf->num_lan_msix,
200 						      ice_get_avail_rxq_count(pf),
201 						      (u16)num_online_cpus());
202 			}
203 		}
204 
205 		pf->num_lan_rx = vsi->alloc_rxq;
206 
207 		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
208 					   max_t(int, vsi->alloc_rxq,
209 						 vsi->alloc_txq));
210 		break;
211 	case ICE_VSI_VF:
212 		if (vf->num_req_qs)
213 			vf->num_vf_qs = vf->num_req_qs;
214 		vsi->alloc_txq = vf->num_vf_qs;
215 		vsi->alloc_rxq = vf->num_vf_qs;
216 		/* pf->vfs.num_msix_per includes (VF miscellaneous vector +
217 		 * data queue interrupts). Since vsi->num_q_vectors is number
218 		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
219 		 * original vector count
220 		 */
221 		vsi->num_q_vectors = vf->num_msix - ICE_NONQ_VECS_VF;
222 		break;
223 	case ICE_VSI_CTRL:
224 		vsi->alloc_txq = 1;
225 		vsi->alloc_rxq = 1;
226 		vsi->num_q_vectors = 1;
227 		break;
228 	case ICE_VSI_CHNL:
229 		vsi->alloc_txq = 0;
230 		vsi->alloc_rxq = 0;
231 		break;
232 	case ICE_VSI_LB:
233 		vsi->alloc_txq = 1;
234 		vsi->alloc_rxq = 1;
235 		break;
236 	default:
237 		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type);
238 		break;
239 	}
240 
241 	ice_vsi_set_num_desc(vsi);
242 }
243 
244 /**
245  * ice_get_free_slot - get the next non-NULL location index in array
246  * @array: array to search
247  * @size: size of the array
248  * @curr: last known occupied index to be used as a search hint
249  *
250  * void * is being used to keep the functionality generic. This lets us use this
251  * function on any array of pointers.
252  */
253 static int ice_get_free_slot(void *array, int size, int curr)
254 {
255 	int **tmp_array = (int **)array;
256 	int next;
257 
258 	if (curr < (size - 1) && !tmp_array[curr + 1]) {
259 		next = curr + 1;
260 	} else {
261 		int i = 0;
262 
263 		while ((i < size) && (tmp_array[i]))
264 			i++;
265 		if (i == size)
266 			next = ICE_NO_VSI;
267 		else
268 			next = i;
269 	}
270 	return next;
271 }
272 
273 /**
274  * ice_vsi_delete_from_hw - delete a VSI from the switch
275  * @vsi: pointer to VSI being removed
276  */
277 static void ice_vsi_delete_from_hw(struct ice_vsi *vsi)
278 {
279 	struct ice_pf *pf = vsi->back;
280 	struct ice_vsi_ctx *ctxt;
281 	int status;
282 
283 	ice_fltr_remove_all(vsi);
284 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
285 	if (!ctxt)
286 		return;
287 
288 	if (vsi->type == ICE_VSI_VF)
289 		ctxt->vf_num = vsi->vf->vf_id;
290 	ctxt->vsi_num = vsi->vsi_num;
291 
292 	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
293 
294 	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
295 	if (status)
296 		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
297 			vsi->vsi_num, status);
298 
299 	kfree(ctxt);
300 }
301 
302 /**
303  * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
304  * @vsi: pointer to VSI being cleared
305  */
306 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
307 {
308 	struct ice_pf *pf = vsi->back;
309 	struct device *dev;
310 
311 	dev = ice_pf_to_dev(pf);
312 
313 	bitmap_free(vsi->af_xdp_zc_qps);
314 	vsi->af_xdp_zc_qps = NULL;
315 	/* free the ring and vector containers */
316 	devm_kfree(dev, vsi->q_vectors);
317 	vsi->q_vectors = NULL;
318 	devm_kfree(dev, vsi->tx_rings);
319 	vsi->tx_rings = NULL;
320 	devm_kfree(dev, vsi->rx_rings);
321 	vsi->rx_rings = NULL;
322 	devm_kfree(dev, vsi->txq_map);
323 	vsi->txq_map = NULL;
324 	devm_kfree(dev, vsi->rxq_map);
325 	vsi->rxq_map = NULL;
326 }
327 
328 /**
329  * ice_vsi_free_stats - Free the ring statistics structures
330  * @vsi: VSI pointer
331  */
332 static void ice_vsi_free_stats(struct ice_vsi *vsi)
333 {
334 	struct ice_vsi_stats *vsi_stat;
335 	struct ice_pf *pf = vsi->back;
336 	int i;
337 
338 	if (vsi->type == ICE_VSI_CHNL)
339 		return;
340 	if (!pf->vsi_stats)
341 		return;
342 
343 	vsi_stat = pf->vsi_stats[vsi->idx];
344 	if (!vsi_stat)
345 		return;
346 
347 	ice_for_each_alloc_txq(vsi, i) {
348 		if (vsi_stat->tx_ring_stats[i]) {
349 			kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
350 			WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
351 		}
352 	}
353 
354 	ice_for_each_alloc_rxq(vsi, i) {
355 		if (vsi_stat->rx_ring_stats[i]) {
356 			kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
357 			WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
358 		}
359 	}
360 
361 	kfree(vsi_stat->tx_ring_stats);
362 	kfree(vsi_stat->rx_ring_stats);
363 	kfree(vsi_stat);
364 	pf->vsi_stats[vsi->idx] = NULL;
365 }
366 
367 /**
368  * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
369  * @vsi: VSI which is having stats allocated
370  */
371 static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
372 {
373 	struct ice_ring_stats **tx_ring_stats;
374 	struct ice_ring_stats **rx_ring_stats;
375 	struct ice_vsi_stats *vsi_stats;
376 	struct ice_pf *pf = vsi->back;
377 	u16 i;
378 
379 	vsi_stats = pf->vsi_stats[vsi->idx];
380 	tx_ring_stats = vsi_stats->tx_ring_stats;
381 	rx_ring_stats = vsi_stats->rx_ring_stats;
382 
383 	/* Allocate Tx ring stats */
384 	ice_for_each_alloc_txq(vsi, i) {
385 		struct ice_ring_stats *ring_stats;
386 		struct ice_tx_ring *ring;
387 
388 		ring = vsi->tx_rings[i];
389 		ring_stats = tx_ring_stats[i];
390 
391 		if (!ring_stats) {
392 			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
393 			if (!ring_stats)
394 				goto err_out;
395 
396 			WRITE_ONCE(tx_ring_stats[i], ring_stats);
397 		}
398 
399 		ring->ring_stats = ring_stats;
400 	}
401 
402 	/* Allocate Rx ring stats */
403 	ice_for_each_alloc_rxq(vsi, i) {
404 		struct ice_ring_stats *ring_stats;
405 		struct ice_rx_ring *ring;
406 
407 		ring = vsi->rx_rings[i];
408 		ring_stats = rx_ring_stats[i];
409 
410 		if (!ring_stats) {
411 			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
412 			if (!ring_stats)
413 				goto err_out;
414 
415 			WRITE_ONCE(rx_ring_stats[i], ring_stats);
416 		}
417 
418 		ring->ring_stats = ring_stats;
419 	}
420 
421 	return 0;
422 
423 err_out:
424 	ice_vsi_free_stats(vsi);
425 	return -ENOMEM;
426 }
427 
428 /**
429  * ice_vsi_free - clean up and deallocate the provided VSI
430  * @vsi: pointer to VSI being cleared
431  *
432  * This deallocates the VSI's queue resources, removes it from the PF's
433  * VSI array if necessary, and deallocates the VSI
434  */
435 static void ice_vsi_free(struct ice_vsi *vsi)
436 {
437 	struct ice_pf *pf = NULL;
438 	struct device *dev;
439 
440 	if (!vsi || !vsi->back)
441 		return;
442 
443 	pf = vsi->back;
444 	dev = ice_pf_to_dev(pf);
445 
446 	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
447 		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
448 		return;
449 	}
450 
451 	mutex_lock(&pf->sw_mutex);
452 	/* updates the PF for this cleared VSI */
453 
454 	pf->vsi[vsi->idx] = NULL;
455 	pf->next_vsi = vsi->idx;
456 
457 	ice_vsi_free_stats(vsi);
458 	ice_vsi_free_arrays(vsi);
459 	mutex_unlock(&pf->sw_mutex);
460 	devm_kfree(dev, vsi);
461 }
462 
463 void ice_vsi_delete(struct ice_vsi *vsi)
464 {
465 	ice_vsi_delete_from_hw(vsi);
466 	ice_vsi_free(vsi);
467 }
468 
469 /**
470  * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
471  * @irq: interrupt number
472  * @data: pointer to a q_vector
473  */
474 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
475 {
476 	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
477 
478 	if (!q_vector->tx.tx_ring)
479 		return IRQ_HANDLED;
480 
481 #define FDIR_RX_DESC_CLEAN_BUDGET 64
482 	ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
483 	ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
484 
485 	return IRQ_HANDLED;
486 }
487 
488 /**
489  * ice_msix_clean_rings - MSIX mode Interrupt Handler
490  * @irq: interrupt number
491  * @data: pointer to a q_vector
492  */
493 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
494 {
495 	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
496 
497 	if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
498 		return IRQ_HANDLED;
499 
500 	q_vector->total_events++;
501 
502 	napi_schedule(&q_vector->napi);
503 
504 	return IRQ_HANDLED;
505 }
506 
507 /**
508  * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
509  * @vsi: VSI pointer
510  */
511 static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
512 {
513 	struct ice_vsi_stats *vsi_stat;
514 	struct ice_pf *pf = vsi->back;
515 
516 	if (vsi->type == ICE_VSI_CHNL)
517 		return 0;
518 	if (!pf->vsi_stats)
519 		return -ENOENT;
520 
521 	if (pf->vsi_stats[vsi->idx])
522 	/* realloc will happen in rebuild path */
523 		return 0;
524 
525 	vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
526 	if (!vsi_stat)
527 		return -ENOMEM;
528 
529 	vsi_stat->tx_ring_stats =
530 		kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
531 			GFP_KERNEL);
532 	if (!vsi_stat->tx_ring_stats)
533 		goto err_alloc_tx;
534 
535 	vsi_stat->rx_ring_stats =
536 		kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
537 			GFP_KERNEL);
538 	if (!vsi_stat->rx_ring_stats)
539 		goto err_alloc_rx;
540 
541 	pf->vsi_stats[vsi->idx] = vsi_stat;
542 
543 	return 0;
544 
545 err_alloc_rx:
546 	kfree(vsi_stat->rx_ring_stats);
547 err_alloc_tx:
548 	kfree(vsi_stat->tx_ring_stats);
549 	kfree(vsi_stat);
550 	pf->vsi_stats[vsi->idx] = NULL;
551 	return -ENOMEM;
552 }
553 
554 /**
555  * ice_vsi_alloc_def - set default values for already allocated VSI
556  * @vsi: ptr to VSI
557  * @ch: ptr to channel
558  */
559 static int
560 ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
561 {
562 	if (vsi->type != ICE_VSI_CHNL) {
563 		ice_vsi_set_num_qs(vsi);
564 		if (ice_vsi_alloc_arrays(vsi))
565 			return -ENOMEM;
566 	}
567 
568 	switch (vsi->type) {
569 	case ICE_VSI_PF:
570 		/* Setup default MSIX irq handler for VSI */
571 		vsi->irq_handler = ice_msix_clean_rings;
572 		break;
573 	case ICE_VSI_CTRL:
574 		/* Setup ctrl VSI MSIX irq handler */
575 		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
576 		break;
577 	case ICE_VSI_CHNL:
578 		if (!ch)
579 			return -EINVAL;
580 
581 		vsi->num_rxq = ch->num_rxq;
582 		vsi->num_txq = ch->num_txq;
583 		vsi->next_base_q = ch->base_q;
584 		break;
585 	case ICE_VSI_VF:
586 	case ICE_VSI_LB:
587 		break;
588 	default:
589 		ice_vsi_free_arrays(vsi);
590 		return -EINVAL;
591 	}
592 
593 	return 0;
594 }
595 
596 /**
597  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
598  * @pf: board private structure
599  *
600  * Reserves a VSI index from the PF and allocates an empty VSI structure
601  * without a type. The VSI structure must later be initialized by calling
602  * ice_vsi_cfg().
603  *
604  * returns a pointer to a VSI on success, NULL on failure.
605  */
606 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
607 {
608 	struct device *dev = ice_pf_to_dev(pf);
609 	struct ice_vsi *vsi = NULL;
610 
611 	/* Need to protect the allocation of the VSIs at the PF level */
612 	mutex_lock(&pf->sw_mutex);
613 
614 	/* If we have already allocated our maximum number of VSIs,
615 	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
616 	 * is available to be populated
617 	 */
618 	if (pf->next_vsi == ICE_NO_VSI) {
619 		dev_dbg(dev, "out of VSI slots!\n");
620 		goto unlock_pf;
621 	}
622 
623 	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
624 	if (!vsi)
625 		goto unlock_pf;
626 
627 	vsi->back = pf;
628 	set_bit(ICE_VSI_DOWN, vsi->state);
629 
630 	/* fill slot and make note of the index */
631 	vsi->idx = pf->next_vsi;
632 	pf->vsi[pf->next_vsi] = vsi;
633 
634 	/* prepare pf->next_vsi for next use */
635 	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
636 					 pf->next_vsi);
637 
638 unlock_pf:
639 	mutex_unlock(&pf->sw_mutex);
640 	return vsi;
641 }
642 
643 /**
644  * ice_alloc_fd_res - Allocate FD resource for a VSI
645  * @vsi: pointer to the ice_vsi
646  *
647  * This allocates the FD resources
648  *
649  * Returns 0 on success, -EPERM on no-op or -EIO on failure
650  */
651 static int ice_alloc_fd_res(struct ice_vsi *vsi)
652 {
653 	struct ice_pf *pf = vsi->back;
654 	u32 g_val, b_val;
655 
656 	/* Flow Director filters are only allocated/assigned to the PF VSI or
657 	 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
658 	 * add/delete filters so resources are not allocated to it
659 	 */
660 	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
661 		return -EPERM;
662 
663 	if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
664 	      vsi->type == ICE_VSI_CHNL))
665 		return -EPERM;
666 
667 	/* FD filters from guaranteed pool per VSI */
668 	g_val = pf->hw.func_caps.fd_fltr_guar;
669 	if (!g_val)
670 		return -EPERM;
671 
672 	/* FD filters from best effort pool */
673 	b_val = pf->hw.func_caps.fd_fltr_best_effort;
674 	if (!b_val)
675 		return -EPERM;
676 
677 	/* PF main VSI gets only 64 FD resources from guaranteed pool
678 	 * when ADQ is configured.
679 	 */
680 #define ICE_PF_VSI_GFLTR	64
681 
682 	/* determine FD filter resources per VSI from shared(best effort) and
683 	 * dedicated pool
684 	 */
685 	if (vsi->type == ICE_VSI_PF) {
686 		vsi->num_gfltr = g_val;
687 		/* if MQPRIO is configured, main VSI doesn't get all FD
688 		 * resources from guaranteed pool. PF VSI gets 64 FD resources
689 		 */
690 		if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
691 			if (g_val < ICE_PF_VSI_GFLTR)
692 				return -EPERM;
693 			/* allow bare minimum entries for PF VSI */
694 			vsi->num_gfltr = ICE_PF_VSI_GFLTR;
695 		}
696 
697 		/* each VSI gets same "best_effort" quota */
698 		vsi->num_bfltr = b_val;
699 	} else if (vsi->type == ICE_VSI_VF) {
700 		vsi->num_gfltr = 0;
701 
702 		/* each VSI gets same "best_effort" quota */
703 		vsi->num_bfltr = b_val;
704 	} else {
705 		struct ice_vsi *main_vsi;
706 		int numtc;
707 
708 		main_vsi = ice_get_main_vsi(pf);
709 		if (!main_vsi)
710 			return -EPERM;
711 
712 		if (!main_vsi->all_numtc)
713 			return -EINVAL;
714 
715 		/* figure out ADQ numtc */
716 		numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
717 
718 		/* only one TC but still asking resources for channels,
719 		 * invalid config
720 		 */
721 		if (numtc < ICE_CHNL_START_TC)
722 			return -EPERM;
723 
724 		g_val -= ICE_PF_VSI_GFLTR;
725 		/* channel VSIs gets equal share from guaranteed pool */
726 		vsi->num_gfltr = g_val / numtc;
727 
728 		/* each VSI gets same "best_effort" quota */
729 		vsi->num_bfltr = b_val;
730 	}
731 
732 	return 0;
733 }
734 
735 /**
736  * ice_vsi_get_qs - Assign queues from PF to VSI
737  * @vsi: the VSI to assign queues to
738  *
739  * Returns 0 on success and a negative value on error
740  */
741 static int ice_vsi_get_qs(struct ice_vsi *vsi)
742 {
743 	struct ice_pf *pf = vsi->back;
744 	struct ice_qs_cfg tx_qs_cfg = {
745 		.qs_mutex = &pf->avail_q_mutex,
746 		.pf_map = pf->avail_txqs,
747 		.pf_map_size = pf->max_pf_txqs,
748 		.q_count = vsi->alloc_txq,
749 		.scatter_count = ICE_MAX_SCATTER_TXQS,
750 		.vsi_map = vsi->txq_map,
751 		.vsi_map_offset = 0,
752 		.mapping_mode = ICE_VSI_MAP_CONTIG
753 	};
754 	struct ice_qs_cfg rx_qs_cfg = {
755 		.qs_mutex = &pf->avail_q_mutex,
756 		.pf_map = pf->avail_rxqs,
757 		.pf_map_size = pf->max_pf_rxqs,
758 		.q_count = vsi->alloc_rxq,
759 		.scatter_count = ICE_MAX_SCATTER_RXQS,
760 		.vsi_map = vsi->rxq_map,
761 		.vsi_map_offset = 0,
762 		.mapping_mode = ICE_VSI_MAP_CONTIG
763 	};
764 	int ret;
765 
766 	if (vsi->type == ICE_VSI_CHNL)
767 		return 0;
768 
769 	ret = __ice_vsi_get_qs(&tx_qs_cfg);
770 	if (ret)
771 		return ret;
772 	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
773 
774 	ret = __ice_vsi_get_qs(&rx_qs_cfg);
775 	if (ret)
776 		return ret;
777 	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
778 
779 	return 0;
780 }
781 
782 /**
783  * ice_vsi_put_qs - Release queues from VSI to PF
784  * @vsi: the VSI that is going to release queues
785  */
786 static void ice_vsi_put_qs(struct ice_vsi *vsi)
787 {
788 	struct ice_pf *pf = vsi->back;
789 	int i;
790 
791 	mutex_lock(&pf->avail_q_mutex);
792 
793 	ice_for_each_alloc_txq(vsi, i) {
794 		clear_bit(vsi->txq_map[i], pf->avail_txqs);
795 		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
796 	}
797 
798 	ice_for_each_alloc_rxq(vsi, i) {
799 		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
800 		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
801 	}
802 
803 	mutex_unlock(&pf->avail_q_mutex);
804 }
805 
806 /**
807  * ice_is_safe_mode
808  * @pf: pointer to the PF struct
809  *
810  * returns true if driver is in safe mode, false otherwise
811  */
812 bool ice_is_safe_mode(struct ice_pf *pf)
813 {
814 	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
815 }
816 
817 /**
818  * ice_is_rdma_ena
819  * @pf: pointer to the PF struct
820  *
821  * returns true if RDMA is currently supported, false otherwise
822  */
823 bool ice_is_rdma_ena(struct ice_pf *pf)
824 {
825 	return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
826 }
827 
828 /**
829  * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
830  * @vsi: the VSI being cleaned up
831  *
832  * This function deletes RSS input set for all flows that were configured
833  * for this VSI
834  */
835 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
836 {
837 	struct ice_pf *pf = vsi->back;
838 	int status;
839 
840 	if (ice_is_safe_mode(pf))
841 		return;
842 
843 	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
844 	if (status)
845 		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
846 			vsi->vsi_num, status);
847 }
848 
849 /**
850  * ice_rss_clean - Delete RSS related VSI structures and configuration
851  * @vsi: the VSI being removed
852  */
853 static void ice_rss_clean(struct ice_vsi *vsi)
854 {
855 	struct ice_pf *pf = vsi->back;
856 	struct device *dev;
857 
858 	dev = ice_pf_to_dev(pf);
859 
860 	devm_kfree(dev, vsi->rss_hkey_user);
861 	devm_kfree(dev, vsi->rss_lut_user);
862 
863 	ice_vsi_clean_rss_flow_fld(vsi);
864 	/* remove RSS replay list */
865 	if (!ice_is_safe_mode(pf))
866 		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
867 }
868 
869 /**
870  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
871  * @vsi: the VSI being configured
872  */
873 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
874 {
875 	struct ice_hw_common_caps *cap;
876 	struct ice_pf *pf = vsi->back;
877 	u16 max_rss_size;
878 
879 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
880 		vsi->rss_size = 1;
881 		return;
882 	}
883 
884 	cap = &pf->hw.func_caps.common_cap;
885 	max_rss_size = BIT(cap->rss_table_entry_width);
886 	switch (vsi->type) {
887 	case ICE_VSI_CHNL:
888 	case ICE_VSI_PF:
889 		/* PF VSI will inherit RSS instance of PF */
890 		vsi->rss_table_size = (u16)cap->rss_table_size;
891 		if (vsi->type == ICE_VSI_CHNL)
892 			vsi->rss_size = min_t(u16, vsi->num_rxq, max_rss_size);
893 		else
894 			vsi->rss_size = min_t(u16, num_online_cpus(),
895 					      max_rss_size);
896 		vsi->rss_lut_type = ICE_LUT_PF;
897 		break;
898 	case ICE_VSI_VF:
899 		/* VF VSI will get a small RSS table.
900 		 * For VSI_LUT, LUT size should be set to 64 bytes.
901 		 */
902 		vsi->rss_table_size = ICE_LUT_VSI_SIZE;
903 		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
904 		vsi->rss_lut_type = ICE_LUT_VSI;
905 		break;
906 	case ICE_VSI_LB:
907 		break;
908 	default:
909 		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
910 			ice_vsi_type_str(vsi->type));
911 		break;
912 	}
913 }
914 
915 /**
916  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
917  * @hw: HW structure used to determine the VLAN mode of the device
918  * @ctxt: the VSI context being set
919  *
920  * This initializes a default VSI context for all sections except the Queues.
921  */
922 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
923 {
924 	u32 table = 0;
925 
926 	memset(&ctxt->info, 0, sizeof(ctxt->info));
927 	/* VSI's should be allocated from shared pool */
928 	ctxt->alloc_from_pool = true;
929 	/* Src pruning enabled by default */
930 	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
931 	/* Traffic from VSI can be sent to LAN */
932 	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
933 	/* allow all untagged/tagged packets by default on Tx */
934 	ctxt->info.inner_vlan_flags = FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_TX_MODE_M,
935 						 ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL);
936 	/* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
937 	 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
938 	 *
939 	 * DVM - leave inner VLAN in packet by default
940 	 */
941 	if (ice_is_dvm_ena(hw)) {
942 		ctxt->info.inner_vlan_flags |=
943 			FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M,
944 				   ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING);
945 		ctxt->info.outer_vlan_flags =
946 			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M,
947 				   ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL);
948 		ctxt->info.outer_vlan_flags |=
949 			FIELD_PREP(ICE_AQ_VSI_OUTER_TAG_TYPE_M,
950 				   ICE_AQ_VSI_OUTER_TAG_VLAN_8100);
951 		ctxt->info.outer_vlan_flags |=
952 			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
953 				   ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
954 	}
955 	/* Have 1:1 UP mapping for both ingress/egress tables */
956 	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
957 	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
958 	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
959 	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
960 	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
961 	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
962 	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
963 	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
964 	ctxt->info.ingress_table = cpu_to_le32(table);
965 	ctxt->info.egress_table = cpu_to_le32(table);
966 	/* Have 1:1 UP mapping for outer to inner UP table */
967 	ctxt->info.outer_up_table = cpu_to_le32(table);
968 	/* No Outer tag support outer_tag_flags remains to zero */
969 }
970 
971 /**
972  * ice_vsi_setup_q_map - Setup a VSI queue map
973  * @vsi: the VSI being configured
974  * @ctxt: VSI context structure
975  */
976 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
977 {
978 	u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
979 	u16 num_txq_per_tc, num_rxq_per_tc;
980 	u16 qcount_tx = vsi->alloc_txq;
981 	u16 qcount_rx = vsi->alloc_rxq;
982 	u8 netdev_tc = 0;
983 	int i;
984 
985 	if (!vsi->tc_cfg.numtc) {
986 		/* at least TC0 should be enabled by default */
987 		vsi->tc_cfg.numtc = 1;
988 		vsi->tc_cfg.ena_tc = 1;
989 	}
990 
991 	num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
992 	if (!num_rxq_per_tc)
993 		num_rxq_per_tc = 1;
994 	num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
995 	if (!num_txq_per_tc)
996 		num_txq_per_tc = 1;
997 
998 	/* find the (rounded up) power-of-2 of qcount */
999 	pow = (u16)order_base_2(num_rxq_per_tc);
1000 
1001 	/* TC mapping is a function of the number of Rx queues assigned to the
1002 	 * VSI for each traffic class and the offset of these queues.
1003 	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1004 	 * queues allocated to TC0. No:of queues is a power-of-2.
1005 	 *
1006 	 * If TC is not enabled, the queue offset is set to 0, and allocate one
1007 	 * queue, this way, traffic for the given TC will be sent to the default
1008 	 * queue.
1009 	 *
1010 	 * Setup number and offset of Rx queues for all TCs for the VSI
1011 	 */
1012 	ice_for_each_traffic_class(i) {
1013 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1014 			/* TC is not enabled */
1015 			vsi->tc_cfg.tc_info[i].qoffset = 0;
1016 			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1017 			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1018 			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1019 			ctxt->info.tc_mapping[i] = 0;
1020 			continue;
1021 		}
1022 
1023 		/* TC is enabled */
1024 		vsi->tc_cfg.tc_info[i].qoffset = offset;
1025 		vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1026 		vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1027 		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1028 
1029 		qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1030 		qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
1031 		offset += num_rxq_per_tc;
1032 		tx_count += num_txq_per_tc;
1033 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1034 	}
1035 
1036 	/* if offset is non-zero, means it is calculated correctly based on
1037 	 * enabled TCs for a given VSI otherwise qcount_rx will always
1038 	 * be correct and non-zero because it is based off - VSI's
1039 	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1040 	 * at least 1)
1041 	 */
1042 	if (offset)
1043 		rx_count = offset;
1044 	else
1045 		rx_count = num_rxq_per_tc;
1046 
1047 	if (rx_count > vsi->alloc_rxq) {
1048 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1049 			rx_count, vsi->alloc_rxq);
1050 		return -EINVAL;
1051 	}
1052 
1053 	if (tx_count > vsi->alloc_txq) {
1054 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1055 			tx_count, vsi->alloc_txq);
1056 		return -EINVAL;
1057 	}
1058 
1059 	vsi->num_txq = tx_count;
1060 	vsi->num_rxq = rx_count;
1061 
1062 	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1063 		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1064 		/* since there is a chance that num_rxq could have been changed
1065 		 * in the above for loop, make num_txq equal to num_rxq.
1066 		 */
1067 		vsi->num_txq = vsi->num_rxq;
1068 	}
1069 
1070 	/* Rx queue mapping */
1071 	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1072 	/* q_mapping buffer holds the info for the first queue allocated for
1073 	 * this VSI in the PF space and also the number of queues associated
1074 	 * with this VSI.
1075 	 */
1076 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1077 	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1078 
1079 	return 0;
1080 }
1081 
1082 /**
1083  * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1084  * @ctxt: the VSI context being set
1085  * @vsi: the VSI being configured
1086  */
1087 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1088 {
1089 	u8 dflt_q_group, dflt_q_prio;
1090 	u16 dflt_q, report_q, val;
1091 
1092 	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1093 	    vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1094 		return;
1095 
1096 	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1097 	ctxt->info.valid_sections |= cpu_to_le16(val);
1098 	dflt_q = 0;
1099 	dflt_q_group = 0;
1100 	report_q = 0;
1101 	dflt_q_prio = 0;
1102 
1103 	/* enable flow director filtering/programming */
1104 	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1105 	ctxt->info.fd_options = cpu_to_le16(val);
1106 	/* max of allocated flow director filters */
1107 	ctxt->info.max_fd_fltr_dedicated =
1108 			cpu_to_le16(vsi->num_gfltr);
1109 	/* max of shared flow director filters any VSI may program */
1110 	ctxt->info.max_fd_fltr_shared =
1111 			cpu_to_le16(vsi->num_bfltr);
1112 	/* default queue index within the VSI of the default FD */
1113 	val = FIELD_PREP(ICE_AQ_VSI_FD_DEF_Q_M, dflt_q);
1114 	/* target queue or queue group to the FD filter */
1115 	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_GRP_M, dflt_q_group);
1116 	ctxt->info.fd_def_q = cpu_to_le16(val);
1117 	/* queue index on which FD filter completion is reported */
1118 	val = FIELD_PREP(ICE_AQ_VSI_FD_REPORT_Q_M, report_q);
1119 	/* priority of the default qindex action */
1120 	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_PRIORITY_M, dflt_q_prio);
1121 	ctxt->info.fd_report_opt = cpu_to_le16(val);
1122 }
1123 
1124 /**
1125  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1126  * @ctxt: the VSI context being set
1127  * @vsi: the VSI being configured
1128  */
1129 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1130 {
1131 	u8 lut_type, hash_type;
1132 	struct device *dev;
1133 	struct ice_pf *pf;
1134 
1135 	pf = vsi->back;
1136 	dev = ice_pf_to_dev(pf);
1137 
1138 	switch (vsi->type) {
1139 	case ICE_VSI_CHNL:
1140 	case ICE_VSI_PF:
1141 		/* PF VSI will inherit RSS instance of PF */
1142 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1143 		break;
1144 	case ICE_VSI_VF:
1145 		/* VF VSI will gets a small RSS table which is a VSI LUT type */
1146 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1147 		break;
1148 	default:
1149 		dev_dbg(dev, "Unsupported VSI type %s\n",
1150 			ice_vsi_type_str(vsi->type));
1151 		return;
1152 	}
1153 
1154 	hash_type = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
1155 	vsi->rss_hfunc = hash_type;
1156 
1157 	ctxt->info.q_opt_rss =
1158 		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
1159 		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
1160 }
1161 
1162 static void
1163 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1164 {
1165 	struct ice_pf *pf = vsi->back;
1166 	u16 qcount, qmap;
1167 	u8 offset = 0;
1168 	int pow;
1169 
1170 	qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1171 
1172 	pow = order_base_2(qcount);
1173 	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1174 	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
1175 
1176 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1177 	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1178 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1179 	ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1180 }
1181 
1182 /**
1183  * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
1184  * @vsi: VSI to check whether or not VLAN pruning is enabled.
1185  *
1186  * returns true if Rx VLAN pruning is enabled and false otherwise.
1187  */
1188 static bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
1189 {
1190 	return vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1191 }
1192 
1193 /**
1194  * ice_vsi_init - Create and initialize a VSI
1195  * @vsi: the VSI being configured
1196  * @vsi_flags: VSI configuration flags
1197  *
1198  * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1199  * reconfigure an existing context.
1200  *
1201  * This initializes a VSI context depending on the VSI type to be added and
1202  * passes it down to the add_vsi aq command to create a new VSI.
1203  */
1204 static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1205 {
1206 	struct ice_pf *pf = vsi->back;
1207 	struct ice_hw *hw = &pf->hw;
1208 	struct ice_vsi_ctx *ctxt;
1209 	struct device *dev;
1210 	int ret = 0;
1211 
1212 	dev = ice_pf_to_dev(pf);
1213 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1214 	if (!ctxt)
1215 		return -ENOMEM;
1216 
1217 	switch (vsi->type) {
1218 	case ICE_VSI_CTRL:
1219 	case ICE_VSI_LB:
1220 	case ICE_VSI_PF:
1221 		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1222 		break;
1223 	case ICE_VSI_CHNL:
1224 		ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1225 		break;
1226 	case ICE_VSI_VF:
1227 		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1228 		/* VF number here is the absolute VF number (0-255) */
1229 		ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1230 		break;
1231 	default:
1232 		ret = -ENODEV;
1233 		goto out;
1234 	}
1235 
1236 	/* Handle VLAN pruning for channel VSI if main VSI has VLAN
1237 	 * prune enabled
1238 	 */
1239 	if (vsi->type == ICE_VSI_CHNL) {
1240 		struct ice_vsi *main_vsi;
1241 
1242 		main_vsi = ice_get_main_vsi(pf);
1243 		if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1244 			ctxt->info.sw_flags2 |=
1245 				ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1246 		else
1247 			ctxt->info.sw_flags2 &=
1248 				~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1249 	}
1250 
1251 	ice_set_dflt_vsi_ctx(hw, ctxt);
1252 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1253 		ice_set_fd_vsi_ctx(ctxt, vsi);
1254 	/* if the switch is in VEB mode, allow VSI loopback */
1255 	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1256 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1257 
1258 	/* Set LUT type and HASH type if RSS is enabled */
1259 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1260 	    vsi->type != ICE_VSI_CTRL) {
1261 		ice_set_rss_vsi_ctx(ctxt, vsi);
1262 		/* if updating VSI context, make sure to set valid_section:
1263 		 * to indicate which section of VSI context being updated
1264 		 */
1265 		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1266 			ctxt->info.valid_sections |=
1267 				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1268 	}
1269 
1270 	ctxt->info.sw_id = vsi->port_info->sw_id;
1271 	if (vsi->type == ICE_VSI_CHNL) {
1272 		ice_chnl_vsi_setup_q_map(vsi, ctxt);
1273 	} else {
1274 		ret = ice_vsi_setup_q_map(vsi, ctxt);
1275 		if (ret)
1276 			goto out;
1277 
1278 		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1279 			/* means VSI being updated */
1280 			/* must to indicate which section of VSI context are
1281 			 * being modified
1282 			 */
1283 			ctxt->info.valid_sections |=
1284 				cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1285 	}
1286 
1287 	/* Allow control frames out of main VSI */
1288 	if (vsi->type == ICE_VSI_PF) {
1289 		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1290 		ctxt->info.valid_sections |=
1291 			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1292 	}
1293 
1294 	if (vsi_flags & ICE_VSI_FLAG_INIT) {
1295 		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1296 		if (ret) {
1297 			dev_err(dev, "Add VSI failed, err %d\n", ret);
1298 			ret = -EIO;
1299 			goto out;
1300 		}
1301 	} else {
1302 		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1303 		if (ret) {
1304 			dev_err(dev, "Update VSI failed, err %d\n", ret);
1305 			ret = -EIO;
1306 			goto out;
1307 		}
1308 	}
1309 
1310 	/* keep context for update VSI operations */
1311 	vsi->info = ctxt->info;
1312 
1313 	/* record VSI number returned */
1314 	vsi->vsi_num = ctxt->vsi_num;
1315 
1316 out:
1317 	kfree(ctxt);
1318 	return ret;
1319 }
1320 
1321 /**
1322  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1323  * @vsi: the VSI having rings deallocated
1324  */
1325 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1326 {
1327 	int i;
1328 
1329 	/* Avoid stale references by clearing map from vector to ring */
1330 	if (vsi->q_vectors) {
1331 		ice_for_each_q_vector(vsi, i) {
1332 			struct ice_q_vector *q_vector = vsi->q_vectors[i];
1333 
1334 			if (q_vector) {
1335 				q_vector->tx.tx_ring = NULL;
1336 				q_vector->rx.rx_ring = NULL;
1337 			}
1338 		}
1339 	}
1340 
1341 	if (vsi->tx_rings) {
1342 		ice_for_each_alloc_txq(vsi, i) {
1343 			if (vsi->tx_rings[i]) {
1344 				kfree_rcu(vsi->tx_rings[i], rcu);
1345 				WRITE_ONCE(vsi->tx_rings[i], NULL);
1346 			}
1347 		}
1348 	}
1349 	if (vsi->rx_rings) {
1350 		ice_for_each_alloc_rxq(vsi, i) {
1351 			if (vsi->rx_rings[i]) {
1352 				kfree_rcu(vsi->rx_rings[i], rcu);
1353 				WRITE_ONCE(vsi->rx_rings[i], NULL);
1354 			}
1355 		}
1356 	}
1357 }
1358 
1359 /**
1360  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1361  * @vsi: VSI which is having rings allocated
1362  */
1363 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1364 {
1365 	bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1366 	struct ice_pf *pf = vsi->back;
1367 	struct device *dev;
1368 	u16 i;
1369 
1370 	dev = ice_pf_to_dev(pf);
1371 	/* Allocate Tx rings */
1372 	ice_for_each_alloc_txq(vsi, i) {
1373 		struct ice_tx_ring *ring;
1374 
1375 		/* allocate with kzalloc(), free with kfree_rcu() */
1376 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1377 
1378 		if (!ring)
1379 			goto err_out;
1380 
1381 		ring->q_index = i;
1382 		ring->reg_idx = vsi->txq_map[i];
1383 		ring->vsi = vsi;
1384 		ring->tx_tstamps = &pf->ptp.port.tx;
1385 		ring->dev = dev;
1386 		ring->count = vsi->num_tx_desc;
1387 		ring->txq_teid = ICE_INVAL_TEID;
1388 		if (dvm_ena)
1389 			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1390 		else
1391 			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1392 		WRITE_ONCE(vsi->tx_rings[i], ring);
1393 	}
1394 
1395 	/* Allocate Rx rings */
1396 	ice_for_each_alloc_rxq(vsi, i) {
1397 		struct ice_rx_ring *ring;
1398 
1399 		/* allocate with kzalloc(), free with kfree_rcu() */
1400 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1401 		if (!ring)
1402 			goto err_out;
1403 
1404 		ring->q_index = i;
1405 		ring->reg_idx = vsi->rxq_map[i];
1406 		ring->vsi = vsi;
1407 		ring->netdev = vsi->netdev;
1408 		ring->dev = dev;
1409 		ring->count = vsi->num_rx_desc;
1410 		ring->cached_phctime = pf->ptp.cached_phc_time;
1411 		WRITE_ONCE(vsi->rx_rings[i], ring);
1412 	}
1413 
1414 	return 0;
1415 
1416 err_out:
1417 	ice_vsi_clear_rings(vsi);
1418 	return -ENOMEM;
1419 }
1420 
1421 /**
1422  * ice_vsi_manage_rss_lut - disable/enable RSS
1423  * @vsi: the VSI being changed
1424  * @ena: boolean value indicating if this is an enable or disable request
1425  *
1426  * In the event of disable request for RSS, this function will zero out RSS
1427  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1428  * LUT.
1429  */
1430 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1431 {
1432 	u8 *lut;
1433 
1434 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1435 	if (!lut)
1436 		return;
1437 
1438 	if (ena) {
1439 		if (vsi->rss_lut_user)
1440 			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1441 		else
1442 			ice_fill_rss_lut(lut, vsi->rss_table_size,
1443 					 vsi->rss_size);
1444 	}
1445 
1446 	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1447 	kfree(lut);
1448 }
1449 
1450 /**
1451  * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1452  * @vsi: VSI to be configured
1453  * @disable: set to true to have FCS / CRC in the frame data
1454  */
1455 void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1456 {
1457 	int i;
1458 
1459 	ice_for_each_rxq(vsi, i)
1460 		if (disable)
1461 			vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1462 		else
1463 			vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1464 }
1465 
1466 /**
1467  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1468  * @vsi: VSI to be configured
1469  */
1470 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1471 {
1472 	struct ice_pf *pf = vsi->back;
1473 	struct device *dev;
1474 	u8 *lut, *key;
1475 	int err;
1476 
1477 	dev = ice_pf_to_dev(pf);
1478 	if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1479 	    (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1480 		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1481 	} else {
1482 		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1483 
1484 		/* If orig_rss_size is valid and it is less than determined
1485 		 * main VSI's rss_size, update main VSI's rss_size to be
1486 		 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1487 		 * RSS table gets programmed to be correct (whatever it was
1488 		 * to begin with (prior to setup-tc for ADQ config)
1489 		 */
1490 		if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1491 		    vsi->orig_rss_size <= vsi->num_rxq) {
1492 			vsi->rss_size = vsi->orig_rss_size;
1493 			/* now orig_rss_size is used, reset it to zero */
1494 			vsi->orig_rss_size = 0;
1495 		}
1496 	}
1497 
1498 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1499 	if (!lut)
1500 		return -ENOMEM;
1501 
1502 	if (vsi->rss_lut_user)
1503 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1504 	else
1505 		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1506 
1507 	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1508 	if (err) {
1509 		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1510 		goto ice_vsi_cfg_rss_exit;
1511 	}
1512 
1513 	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1514 	if (!key) {
1515 		err = -ENOMEM;
1516 		goto ice_vsi_cfg_rss_exit;
1517 	}
1518 
1519 	if (vsi->rss_hkey_user)
1520 		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1521 	else
1522 		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1523 
1524 	err = ice_set_rss_key(vsi, key);
1525 	if (err)
1526 		dev_err(dev, "set_rss_key failed, error %d\n", err);
1527 
1528 	kfree(key);
1529 ice_vsi_cfg_rss_exit:
1530 	kfree(lut);
1531 	return err;
1532 }
1533 
1534 /**
1535  * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1536  * @vsi: VSI to be configured
1537  *
1538  * This function will only be called during the VF VSI setup. Upon successful
1539  * completion of package download, this function will configure default RSS
1540  * input sets for VF VSI.
1541  */
1542 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1543 {
1544 	struct ice_pf *pf = vsi->back;
1545 	struct device *dev;
1546 	int status;
1547 
1548 	dev = ice_pf_to_dev(pf);
1549 	if (ice_is_safe_mode(pf)) {
1550 		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1551 			vsi->vsi_num);
1552 		return;
1553 	}
1554 
1555 	status = ice_add_avf_rss_cfg(&pf->hw, vsi, ICE_DEFAULT_RSS_HENA);
1556 	if (status)
1557 		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1558 			vsi->vsi_num, status);
1559 }
1560 
1561 static const struct ice_rss_hash_cfg default_rss_cfgs[] = {
1562 	/* configure RSS for IPv4 with input set IP src/dst */
1563 	{ICE_FLOW_SEG_HDR_IPV4, ICE_FLOW_HASH_IPV4, ICE_RSS_ANY_HEADERS, false},
1564 	/* configure RSS for IPv6 with input set IPv6 src/dst */
1565 	{ICE_FLOW_SEG_HDR_IPV6, ICE_FLOW_HASH_IPV6, ICE_RSS_ANY_HEADERS, false},
1566 	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1567 	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4,
1568 				ICE_HASH_TCP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1569 	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1570 	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4,
1571 				ICE_HASH_UDP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1572 	/* configure RSS for sctp4 with input set IP src/dst - only support
1573 	 * RSS on SCTPv4 on outer headers (non-tunneled)
1574 	 */
1575 	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4,
1576 		ICE_HASH_SCTP_IPV4, ICE_RSS_OUTER_HEADERS, false},
1577 	/* configure RSS for gtpc4 with input set IPv4 src/dst */
1578 	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV4,
1579 		ICE_FLOW_HASH_IPV4, ICE_RSS_OUTER_HEADERS, false},
1580 	/* configure RSS for gtpc4t with input set IPv4 src/dst */
1581 	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV4,
1582 		ICE_FLOW_HASH_GTP_C_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1583 	/* configure RSS for gtpu4 with input set IPv4 src/dst */
1584 	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV4,
1585 		ICE_FLOW_HASH_GTP_U_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1586 	/* configure RSS for gtpu4e with input set IPv4 src/dst */
1587 	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV4,
1588 		ICE_FLOW_HASH_GTP_U_IPV4_EH, ICE_RSS_OUTER_HEADERS, false},
1589 	/* configure RSS for gtpu4u with input set IPv4 src/dst */
1590 	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV4,
1591 		ICE_FLOW_HASH_GTP_U_IPV4_UP, ICE_RSS_OUTER_HEADERS, false},
1592 	/* configure RSS for gtpu4d with input set IPv4 src/dst */
1593 	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV4,
1594 		ICE_FLOW_HASH_GTP_U_IPV4_DWN, ICE_RSS_OUTER_HEADERS, false},
1595 
1596 	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1597 	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6,
1598 				ICE_HASH_TCP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1599 	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1600 	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6,
1601 				ICE_HASH_UDP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1602 	/* configure RSS for sctp6 with input set IPv6 src/dst - only support
1603 	 * RSS on SCTPv6 on outer headers (non-tunneled)
1604 	 */
1605 	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6,
1606 		ICE_HASH_SCTP_IPV6, ICE_RSS_OUTER_HEADERS, false},
1607 	/* configure RSS for IPSEC ESP SPI with input set MAC_IPV4_SPI */
1608 	{ICE_FLOW_SEG_HDR_ESP,
1609 		ICE_FLOW_HASH_ESP_SPI, ICE_RSS_OUTER_HEADERS, false},
1610 	/* configure RSS for gtpc6 with input set IPv6 src/dst */
1611 	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV6,
1612 		ICE_FLOW_HASH_IPV6, ICE_RSS_OUTER_HEADERS, false},
1613 	/* configure RSS for gtpc6t with input set IPv6 src/dst */
1614 	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV6,
1615 		ICE_FLOW_HASH_GTP_C_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1616 	/* configure RSS for gtpu6 with input set IPv6 src/dst */
1617 	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV6,
1618 		ICE_FLOW_HASH_GTP_U_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1619 	/* configure RSS for gtpu6e with input set IPv6 src/dst */
1620 	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV6,
1621 		ICE_FLOW_HASH_GTP_U_IPV6_EH, ICE_RSS_OUTER_HEADERS, false},
1622 	/* configure RSS for gtpu6u with input set IPv6 src/dst */
1623 	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV6,
1624 		ICE_FLOW_HASH_GTP_U_IPV6_UP, ICE_RSS_OUTER_HEADERS, false},
1625 	/* configure RSS for gtpu6d with input set IPv6 src/dst */
1626 	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV6,
1627 		ICE_FLOW_HASH_GTP_U_IPV6_DWN, ICE_RSS_OUTER_HEADERS, false},
1628 };
1629 
1630 /**
1631  * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1632  * @vsi: VSI to be configured
1633  *
1634  * This function will only be called after successful download package call
1635  * during initialization of PF. Since the downloaded package will erase the
1636  * RSS section, this function will configure RSS input sets for different
1637  * flow types. The last profile added has the highest priority, therefore 2
1638  * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1639  * (i.e. IPv4 src/dst TCP src/dst port).
1640  */
1641 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1642 {
1643 	u16 vsi_num = vsi->vsi_num;
1644 	struct ice_pf *pf = vsi->back;
1645 	struct ice_hw *hw = &pf->hw;
1646 	struct device *dev;
1647 	int status;
1648 	u32 i;
1649 
1650 	dev = ice_pf_to_dev(pf);
1651 	if (ice_is_safe_mode(pf)) {
1652 		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1653 			vsi_num);
1654 		return;
1655 	}
1656 	for (i = 0; i < ARRAY_SIZE(default_rss_cfgs); i++) {
1657 		const struct ice_rss_hash_cfg *cfg = &default_rss_cfgs[i];
1658 
1659 		status = ice_add_rss_cfg(hw, vsi, cfg);
1660 		if (status)
1661 			dev_dbg(dev, "ice_add_rss_cfg failed, addl_hdrs = %x, hash_flds = %llx, hdr_type = %d, symm = %d\n",
1662 				cfg->addl_hdrs, cfg->hash_flds,
1663 				cfg->hdr_type, cfg->symm);
1664 	}
1665 }
1666 
1667 /**
1668  * ice_pf_state_is_nominal - checks the PF for nominal state
1669  * @pf: pointer to PF to check
1670  *
1671  * Check the PF's state for a collection of bits that would indicate
1672  * the PF is in a state that would inhibit normal operation for
1673  * driver functionality.
1674  *
1675  * Returns true if PF is in a nominal state, false otherwise
1676  */
1677 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1678 {
1679 	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1680 
1681 	if (!pf)
1682 		return false;
1683 
1684 	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1685 	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1686 		return false;
1687 
1688 	return true;
1689 }
1690 
1691 /**
1692  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1693  * @vsi: the VSI to be updated
1694  */
1695 void ice_update_eth_stats(struct ice_vsi *vsi)
1696 {
1697 	struct ice_eth_stats *prev_es, *cur_es;
1698 	struct ice_hw *hw = &vsi->back->hw;
1699 	struct ice_pf *pf = vsi->back;
1700 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1701 
1702 	prev_es = &vsi->eth_stats_prev;
1703 	cur_es = &vsi->eth_stats;
1704 
1705 	if (ice_is_reset_in_progress(pf->state))
1706 		vsi->stat_offsets_loaded = false;
1707 
1708 	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1709 			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1710 
1711 	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1712 			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1713 
1714 	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1715 			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1716 
1717 	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1718 			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1719 
1720 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1721 			  &prev_es->rx_discards, &cur_es->rx_discards);
1722 
1723 	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1724 			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1725 
1726 	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1727 			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1728 
1729 	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1730 			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1731 
1732 	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1733 			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1734 
1735 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1736 			  &prev_es->tx_errors, &cur_es->tx_errors);
1737 
1738 	vsi->stat_offsets_loaded = true;
1739 }
1740 
1741 /**
1742  * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1743  * @hw: HW pointer
1744  * @pf_q: index of the Rx queue in the PF's queue space
1745  * @rxdid: flexible descriptor RXDID
1746  * @prio: priority for the RXDID for this queue
1747  * @ena_ts: true to enable timestamp and false to disable timestamp
1748  */
1749 void
1750 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1751 			bool ena_ts)
1752 {
1753 	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1754 
1755 	/* clear any previous values */
1756 	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1757 		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1758 		    QRXFLXP_CNTXT_TS_M);
1759 
1760 	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_IDX_M, rxdid);
1761 	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_PRIO_M, prio);
1762 
1763 	if (ena_ts)
1764 		/* Enable TimeSync on this queue */
1765 		regval |= QRXFLXP_CNTXT_TS_M;
1766 
1767 	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1768 }
1769 
1770 /**
1771  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1772  * @intrl: interrupt rate limit in usecs
1773  * @gran: interrupt rate limit granularity in usecs
1774  *
1775  * This function converts a decimal interrupt rate limit in usecs to the format
1776  * expected by firmware.
1777  */
1778 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1779 {
1780 	u32 val = intrl / gran;
1781 
1782 	if (val)
1783 		return val | GLINT_RATE_INTRL_ENA_M;
1784 	return 0;
1785 }
1786 
1787 /**
1788  * ice_write_intrl - write throttle rate limit to interrupt specific register
1789  * @q_vector: pointer to interrupt specific structure
1790  * @intrl: throttle rate limit in microseconds to write
1791  */
1792 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1793 {
1794 	struct ice_hw *hw = &q_vector->vsi->back->hw;
1795 
1796 	wr32(hw, GLINT_RATE(q_vector->reg_idx),
1797 	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1798 }
1799 
1800 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1801 {
1802 	switch (rc->type) {
1803 	case ICE_RX_CONTAINER:
1804 		if (rc->rx_ring)
1805 			return rc->rx_ring->q_vector;
1806 		break;
1807 	case ICE_TX_CONTAINER:
1808 		if (rc->tx_ring)
1809 			return rc->tx_ring->q_vector;
1810 		break;
1811 	default:
1812 		break;
1813 	}
1814 
1815 	return NULL;
1816 }
1817 
1818 /**
1819  * __ice_write_itr - write throttle rate to register
1820  * @q_vector: pointer to interrupt data structure
1821  * @rc: pointer to ring container
1822  * @itr: throttle rate in microseconds to write
1823  */
1824 static void __ice_write_itr(struct ice_q_vector *q_vector,
1825 			    struct ice_ring_container *rc, u16 itr)
1826 {
1827 	struct ice_hw *hw = &q_vector->vsi->back->hw;
1828 
1829 	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1830 	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1831 }
1832 
1833 /**
1834  * ice_write_itr - write throttle rate to queue specific register
1835  * @rc: pointer to ring container
1836  * @itr: throttle rate in microseconds to write
1837  */
1838 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1839 {
1840 	struct ice_q_vector *q_vector;
1841 
1842 	q_vector = ice_pull_qvec_from_rc(rc);
1843 	if (!q_vector)
1844 		return;
1845 
1846 	__ice_write_itr(q_vector, rc, itr);
1847 }
1848 
1849 /**
1850  * ice_set_q_vector_intrl - set up interrupt rate limiting
1851  * @q_vector: the vector to be configured
1852  *
1853  * Interrupt rate limiting is local to the vector, not per-queue so we must
1854  * detect if either ring container has dynamic moderation enabled to decide
1855  * what to set the interrupt rate limit to via INTRL settings. In the case that
1856  * dynamic moderation is disabled on both, write the value with the cached
1857  * setting to make sure INTRL register matches the user visible value.
1858  */
1859 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
1860 {
1861 	if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
1862 		/* in the case of dynamic enabled, cap each vector to no more
1863 		 * than (4 us) 250,000 ints/sec, which allows low latency
1864 		 * but still less than 500,000 interrupts per second, which
1865 		 * reduces CPU a bit in the case of the lowest latency
1866 		 * setting. The 4 here is a value in microseconds.
1867 		 */
1868 		ice_write_intrl(q_vector, 4);
1869 	} else {
1870 		ice_write_intrl(q_vector, q_vector->intrl);
1871 	}
1872 }
1873 
1874 /**
1875  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1876  * @vsi: the VSI being configured
1877  *
1878  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1879  * for the VF VSI.
1880  */
1881 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1882 {
1883 	struct ice_pf *pf = vsi->back;
1884 	struct ice_hw *hw = &pf->hw;
1885 	u16 txq = 0, rxq = 0;
1886 	int i, q;
1887 
1888 	ice_for_each_q_vector(vsi, i) {
1889 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1890 		u16 reg_idx = q_vector->reg_idx;
1891 
1892 		ice_cfg_itr(hw, q_vector);
1893 
1894 		/* Both Transmit Queue Interrupt Cause Control register
1895 		 * and Receive Queue Interrupt Cause control register
1896 		 * expects MSIX_INDX field to be the vector index
1897 		 * within the function space and not the absolute
1898 		 * vector index across PF or across device.
1899 		 * For SR-IOV VF VSIs queue vector index always starts
1900 		 * with 1 since first vector index(0) is used for OICR
1901 		 * in VF space. Since VMDq and other PF VSIs are within
1902 		 * the PF function space, use the vector index that is
1903 		 * tracked for this PF.
1904 		 */
1905 		for (q = 0; q < q_vector->num_ring_tx; q++) {
1906 			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1907 					      q_vector->tx.itr_idx);
1908 			txq++;
1909 		}
1910 
1911 		for (q = 0; q < q_vector->num_ring_rx; q++) {
1912 			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1913 					      q_vector->rx.itr_idx);
1914 			rxq++;
1915 		}
1916 	}
1917 }
1918 
1919 /**
1920  * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1921  * @vsi: the VSI whose rings are to be enabled
1922  *
1923  * Returns 0 on success and a negative value on error
1924  */
1925 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1926 {
1927 	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1928 }
1929 
1930 /**
1931  * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1932  * @vsi: the VSI whose rings are to be disabled
1933  *
1934  * Returns 0 on success and a negative value on error
1935  */
1936 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1937 {
1938 	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1939 }
1940 
1941 /**
1942  * ice_vsi_stop_tx_rings - Disable Tx rings
1943  * @vsi: the VSI being configured
1944  * @rst_src: reset source
1945  * @rel_vmvf_num: Relative ID of VF/VM
1946  * @rings: Tx ring array to be stopped
1947  * @count: number of Tx ring array elements
1948  */
1949 static int
1950 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1951 		      u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
1952 {
1953 	u16 q_idx;
1954 
1955 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1956 		return -EINVAL;
1957 
1958 	for (q_idx = 0; q_idx < count; q_idx++) {
1959 		struct ice_txq_meta txq_meta = { };
1960 		int status;
1961 
1962 		if (!rings || !rings[q_idx])
1963 			return -EINVAL;
1964 
1965 		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1966 		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1967 					      rings[q_idx], &txq_meta);
1968 
1969 		if (status)
1970 			return status;
1971 	}
1972 
1973 	return 0;
1974 }
1975 
1976 /**
1977  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1978  * @vsi: the VSI being configured
1979  * @rst_src: reset source
1980  * @rel_vmvf_num: Relative ID of VF/VM
1981  */
1982 int
1983 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1984 			  u16 rel_vmvf_num)
1985 {
1986 	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
1987 }
1988 
1989 /**
1990  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1991  * @vsi: the VSI being configured
1992  */
1993 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1994 {
1995 	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
1996 }
1997 
1998 /**
1999  * ice_vsi_is_rx_queue_active
2000  * @vsi: the VSI being configured
2001  *
2002  * Return true if at least one queue is active.
2003  */
2004 bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
2005 {
2006 	struct ice_pf *pf = vsi->back;
2007 	struct ice_hw *hw = &pf->hw;
2008 	int i;
2009 
2010 	ice_for_each_rxq(vsi, i) {
2011 		u32 rx_reg;
2012 		int pf_q;
2013 
2014 		pf_q = vsi->rxq_map[i];
2015 		rx_reg = rd32(hw, QRX_CTRL(pf_q));
2016 		if (rx_reg & QRX_CTRL_QENA_STAT_M)
2017 			return true;
2018 	}
2019 
2020 	return false;
2021 }
2022 
2023 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2024 {
2025 	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2026 		vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2027 		vsi->tc_cfg.numtc = 1;
2028 		return;
2029 	}
2030 
2031 	/* set VSI TC information based on DCB config */
2032 	ice_vsi_set_dcb_tc_cfg(vsi);
2033 }
2034 
2035 /**
2036  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2037  * @vsi: the VSI being configured
2038  * @tx: bool to determine Tx or Rx rule
2039  * @create: bool to determine create or remove Rule
2040  */
2041 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2042 {
2043 	int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2044 			enum ice_sw_fwd_act_type act);
2045 	struct ice_pf *pf = vsi->back;
2046 	struct device *dev;
2047 	int status;
2048 
2049 	dev = ice_pf_to_dev(pf);
2050 	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2051 
2052 	if (tx) {
2053 		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2054 				  ICE_DROP_PACKET);
2055 	} else {
2056 		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2057 			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2058 							  create);
2059 		} else {
2060 			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2061 					  ICE_FWD_TO_VSI);
2062 		}
2063 	}
2064 
2065 	if (status)
2066 		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2067 			create ? "adding" : "removing", tx ? "TX" : "RX",
2068 			vsi->vsi_num, status);
2069 }
2070 
2071 /**
2072  * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2073  * @vsi: pointer to the VSI
2074  *
2075  * This function will allocate new scheduler aggregator now if needed and will
2076  * move specified VSI into it.
2077  */
2078 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2079 {
2080 	struct device *dev = ice_pf_to_dev(vsi->back);
2081 	struct ice_agg_node *agg_node_iter = NULL;
2082 	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2083 	struct ice_agg_node *agg_node = NULL;
2084 	int node_offset, max_agg_nodes = 0;
2085 	struct ice_port_info *port_info;
2086 	struct ice_pf *pf = vsi->back;
2087 	u32 agg_node_id_start = 0;
2088 	int status;
2089 
2090 	/* create (as needed) scheduler aggregator node and move VSI into
2091 	 * corresponding aggregator node
2092 	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2093 	 * - VF aggregator nodes will contain VF VSI
2094 	 */
2095 	port_info = pf->hw.port_info;
2096 	if (!port_info)
2097 		return;
2098 
2099 	switch (vsi->type) {
2100 	case ICE_VSI_CTRL:
2101 	case ICE_VSI_CHNL:
2102 	case ICE_VSI_LB:
2103 	case ICE_VSI_PF:
2104 		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2105 		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2106 		agg_node_iter = &pf->pf_agg_node[0];
2107 		break;
2108 	case ICE_VSI_VF:
2109 		/* user can create 'n' VFs on a given PF, but since max children
2110 		 * per aggregator node can be only 64. Following code handles
2111 		 * aggregator(s) for VF VSIs, either selects a agg_node which
2112 		 * was already created provided num_vsis < 64, otherwise
2113 		 * select next available node, which will be created
2114 		 */
2115 		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2116 		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2117 		agg_node_iter = &pf->vf_agg_node[0];
2118 		break;
2119 	default:
2120 		/* other VSI type, handle later if needed */
2121 		dev_dbg(dev, "unexpected VSI type %s\n",
2122 			ice_vsi_type_str(vsi->type));
2123 		return;
2124 	}
2125 
2126 	/* find the appropriate aggregator node */
2127 	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2128 		/* see if we can find space in previously created
2129 		 * node if num_vsis < 64, otherwise skip
2130 		 */
2131 		if (agg_node_iter->num_vsis &&
2132 		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2133 			agg_node_iter++;
2134 			continue;
2135 		}
2136 
2137 		if (agg_node_iter->valid &&
2138 		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2139 			agg_id = agg_node_iter->agg_id;
2140 			agg_node = agg_node_iter;
2141 			break;
2142 		}
2143 
2144 		/* find unclaimed agg_id */
2145 		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2146 			agg_id = node_offset + agg_node_id_start;
2147 			agg_node = agg_node_iter;
2148 			break;
2149 		}
2150 		/* move to next agg_node */
2151 		agg_node_iter++;
2152 	}
2153 
2154 	if (!agg_node)
2155 		return;
2156 
2157 	/* if selected aggregator node was not created, create it */
2158 	if (!agg_node->valid) {
2159 		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2160 				     (u8)vsi->tc_cfg.ena_tc);
2161 		if (status) {
2162 			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2163 				agg_id);
2164 			return;
2165 		}
2166 		/* aggregator node is created, store the needed info */
2167 		agg_node->valid = true;
2168 		agg_node->agg_id = agg_id;
2169 	}
2170 
2171 	/* move VSI to corresponding aggregator node */
2172 	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2173 				     (u8)vsi->tc_cfg.ena_tc);
2174 	if (status) {
2175 		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2176 			vsi->idx, agg_id);
2177 		return;
2178 	}
2179 
2180 	/* keep active children count for aggregator node */
2181 	agg_node->num_vsis++;
2182 
2183 	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2184 	 * to aggregator node
2185 	 */
2186 	vsi->agg_node = agg_node;
2187 	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2188 		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2189 		vsi->agg_node->num_vsis);
2190 }
2191 
2192 static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
2193 {
2194 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2195 	struct device *dev = ice_pf_to_dev(pf);
2196 	int ret, i;
2197 
2198 	/* configure VSI nodes based on number of queues and TC's */
2199 	ice_for_each_traffic_class(i) {
2200 		if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2201 			continue;
2202 
2203 		if (vsi->type == ICE_VSI_CHNL) {
2204 			if (!vsi->alloc_txq && vsi->num_txq)
2205 				max_txqs[i] = vsi->num_txq;
2206 			else
2207 				max_txqs[i] = pf->num_lan_tx;
2208 		} else {
2209 			max_txqs[i] = vsi->alloc_txq;
2210 		}
2211 
2212 		if (vsi->type == ICE_VSI_PF)
2213 			max_txqs[i] += vsi->num_xdp_txq;
2214 	}
2215 
2216 	dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2217 	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2218 			      max_txqs);
2219 	if (ret) {
2220 		dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2221 			vsi->vsi_num, ret);
2222 		return ret;
2223 	}
2224 
2225 	return 0;
2226 }
2227 
2228 /**
2229  * ice_vsi_cfg_def - configure default VSI based on the type
2230  * @vsi: pointer to VSI
2231  * @params: the parameters to configure this VSI with
2232  */
2233 static int
2234 ice_vsi_cfg_def(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2235 {
2236 	struct device *dev = ice_pf_to_dev(vsi->back);
2237 	struct ice_pf *pf = vsi->back;
2238 	int ret;
2239 
2240 	vsi->vsw = pf->first_sw;
2241 
2242 	ret = ice_vsi_alloc_def(vsi, params->ch);
2243 	if (ret)
2244 		return ret;
2245 
2246 	/* allocate memory for Tx/Rx ring stat pointers */
2247 	ret = ice_vsi_alloc_stat_arrays(vsi);
2248 	if (ret)
2249 		goto unroll_vsi_alloc;
2250 
2251 	ice_alloc_fd_res(vsi);
2252 
2253 	ret = ice_vsi_get_qs(vsi);
2254 	if (ret) {
2255 		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2256 			vsi->idx);
2257 		goto unroll_vsi_alloc_stat;
2258 	}
2259 
2260 	/* set RSS capabilities */
2261 	ice_vsi_set_rss_params(vsi);
2262 
2263 	/* set TC configuration */
2264 	ice_vsi_set_tc_cfg(vsi);
2265 
2266 	/* create the VSI */
2267 	ret = ice_vsi_init(vsi, params->flags);
2268 	if (ret)
2269 		goto unroll_get_qs;
2270 
2271 	ice_vsi_init_vlan_ops(vsi);
2272 
2273 	switch (vsi->type) {
2274 	case ICE_VSI_CTRL:
2275 	case ICE_VSI_PF:
2276 		ret = ice_vsi_alloc_q_vectors(vsi);
2277 		if (ret)
2278 			goto unroll_vsi_init;
2279 
2280 		ret = ice_vsi_alloc_rings(vsi);
2281 		if (ret)
2282 			goto unroll_vector_base;
2283 
2284 		ret = ice_vsi_alloc_ring_stats(vsi);
2285 		if (ret)
2286 			goto unroll_vector_base;
2287 
2288 		ice_vsi_map_rings_to_vectors(vsi);
2289 
2290 		/* Associate q_vector rings to napi */
2291 		ice_vsi_set_napi_queues(vsi);
2292 
2293 		vsi->stat_offsets_loaded = false;
2294 
2295 		if (ice_is_xdp_ena_vsi(vsi)) {
2296 			ret = ice_vsi_determine_xdp_res(vsi);
2297 			if (ret)
2298 				goto unroll_vector_base;
2299 			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2300 			if (ret)
2301 				goto unroll_vector_base;
2302 		}
2303 
2304 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2305 		if (vsi->type != ICE_VSI_CTRL)
2306 			/* Do not exit if configuring RSS had an issue, at
2307 			 * least receive traffic on first queue. Hence no
2308 			 * need to capture return value
2309 			 */
2310 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2311 				ice_vsi_cfg_rss_lut_key(vsi);
2312 				ice_vsi_set_rss_flow_fld(vsi);
2313 			}
2314 		ice_init_arfs(vsi);
2315 		break;
2316 	case ICE_VSI_CHNL:
2317 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2318 			ice_vsi_cfg_rss_lut_key(vsi);
2319 			ice_vsi_set_rss_flow_fld(vsi);
2320 		}
2321 		break;
2322 	case ICE_VSI_VF:
2323 		/* VF driver will take care of creating netdev for this type and
2324 		 * map queues to vectors through Virtchnl, PF driver only
2325 		 * creates a VSI and corresponding structures for bookkeeping
2326 		 * purpose
2327 		 */
2328 		ret = ice_vsi_alloc_q_vectors(vsi);
2329 		if (ret)
2330 			goto unroll_vsi_init;
2331 
2332 		ret = ice_vsi_alloc_rings(vsi);
2333 		if (ret)
2334 			goto unroll_alloc_q_vector;
2335 
2336 		ret = ice_vsi_alloc_ring_stats(vsi);
2337 		if (ret)
2338 			goto unroll_vector_base;
2339 
2340 		vsi->stat_offsets_loaded = false;
2341 
2342 		/* Do not exit if configuring RSS had an issue, at least
2343 		 * receive traffic on first queue. Hence no need to capture
2344 		 * return value
2345 		 */
2346 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2347 			ice_vsi_cfg_rss_lut_key(vsi);
2348 			ice_vsi_set_vf_rss_flow_fld(vsi);
2349 		}
2350 		break;
2351 	case ICE_VSI_LB:
2352 		ret = ice_vsi_alloc_rings(vsi);
2353 		if (ret)
2354 			goto unroll_vsi_init;
2355 
2356 		ret = ice_vsi_alloc_ring_stats(vsi);
2357 		if (ret)
2358 			goto unroll_vector_base;
2359 
2360 		break;
2361 	default:
2362 		/* clean up the resources and exit */
2363 		ret = -EINVAL;
2364 		goto unroll_vsi_init;
2365 	}
2366 
2367 	return 0;
2368 
2369 unroll_vector_base:
2370 	/* reclaim SW interrupts back to the common pool */
2371 unroll_alloc_q_vector:
2372 	ice_vsi_free_q_vectors(vsi);
2373 unroll_vsi_init:
2374 	ice_vsi_delete_from_hw(vsi);
2375 unroll_get_qs:
2376 	ice_vsi_put_qs(vsi);
2377 unroll_vsi_alloc_stat:
2378 	ice_vsi_free_stats(vsi);
2379 unroll_vsi_alloc:
2380 	ice_vsi_free_arrays(vsi);
2381 	return ret;
2382 }
2383 
2384 /**
2385  * ice_vsi_cfg - configure a previously allocated VSI
2386  * @vsi: pointer to VSI
2387  * @params: parameters used to configure this VSI
2388  */
2389 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2390 {
2391 	struct ice_pf *pf = vsi->back;
2392 	int ret;
2393 
2394 	if (WARN_ON(params->type == ICE_VSI_VF && !params->vf))
2395 		return -EINVAL;
2396 
2397 	vsi->type = params->type;
2398 	vsi->port_info = params->pi;
2399 
2400 	/* For VSIs which don't have a connected VF, this will be NULL */
2401 	vsi->vf = params->vf;
2402 
2403 	ret = ice_vsi_cfg_def(vsi, params);
2404 	if (ret)
2405 		return ret;
2406 
2407 	ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2408 	if (ret)
2409 		ice_vsi_decfg(vsi);
2410 
2411 	if (vsi->type == ICE_VSI_CTRL) {
2412 		if (vsi->vf) {
2413 			WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2414 			vsi->vf->ctrl_vsi_idx = vsi->idx;
2415 		} else {
2416 			WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2417 			pf->ctrl_vsi_idx = vsi->idx;
2418 		}
2419 	}
2420 
2421 	return ret;
2422 }
2423 
2424 /**
2425  * ice_vsi_decfg - remove all VSI configuration
2426  * @vsi: pointer to VSI
2427  */
2428 void ice_vsi_decfg(struct ice_vsi *vsi)
2429 {
2430 	struct ice_pf *pf = vsi->back;
2431 	int err;
2432 
2433 	/* The Rx rule will only exist to remove if the LLDP FW
2434 	 * engine is currently stopped
2435 	 */
2436 	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2437 	    !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2438 		ice_cfg_sw_lldp(vsi, false, false);
2439 
2440 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2441 	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2442 	if (err)
2443 		dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2444 			vsi->vsi_num, err);
2445 
2446 	if (ice_is_xdp_ena_vsi(vsi))
2447 		/* return value check can be skipped here, it always returns
2448 		 * 0 if reset is in progress
2449 		 */
2450 		ice_destroy_xdp_rings(vsi);
2451 
2452 	ice_vsi_clear_rings(vsi);
2453 	ice_vsi_free_q_vectors(vsi);
2454 	ice_vsi_put_qs(vsi);
2455 	ice_vsi_free_arrays(vsi);
2456 
2457 	/* SR-IOV determines needed MSIX resources all at once instead of per
2458 	 * VSI since when VFs are spawned we know how many VFs there are and how
2459 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2460 	 * cleared in the same manner.
2461 	 */
2462 
2463 	if (vsi->type == ICE_VSI_VF &&
2464 	    vsi->agg_node && vsi->agg_node->valid)
2465 		vsi->agg_node->num_vsis--;
2466 }
2467 
2468 /**
2469  * ice_vsi_setup - Set up a VSI by a given type
2470  * @pf: board private structure
2471  * @params: parameters to use when creating the VSI
2472  *
2473  * This allocates the sw VSI structure and its queue resources.
2474  *
2475  * Returns pointer to the successfully allocated and configured VSI sw struct on
2476  * success, NULL on failure.
2477  */
2478 struct ice_vsi *
2479 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2480 {
2481 	struct device *dev = ice_pf_to_dev(pf);
2482 	struct ice_vsi *vsi;
2483 	int ret;
2484 
2485 	/* ice_vsi_setup can only initialize a new VSI, and we must have
2486 	 * a port_info structure for it.
2487 	 */
2488 	if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2489 	    WARN_ON(!params->pi))
2490 		return NULL;
2491 
2492 	vsi = ice_vsi_alloc(pf);
2493 	if (!vsi) {
2494 		dev_err(dev, "could not allocate VSI\n");
2495 		return NULL;
2496 	}
2497 
2498 	ret = ice_vsi_cfg(vsi, params);
2499 	if (ret)
2500 		goto err_vsi_cfg;
2501 
2502 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2503 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2504 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2505 	 * The rule is added once for PF VSI in order to create appropriate
2506 	 * recipe, since VSI/VSI list is ignored with drop action...
2507 	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2508 	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2509 	 * settings in the HW.
2510 	 */
2511 	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2512 		ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2513 				 ICE_DROP_PACKET);
2514 		ice_cfg_sw_lldp(vsi, true, true);
2515 	}
2516 
2517 	if (!vsi->agg_node)
2518 		ice_set_agg_vsi(vsi);
2519 
2520 	return vsi;
2521 
2522 err_vsi_cfg:
2523 	ice_vsi_free(vsi);
2524 
2525 	return NULL;
2526 }
2527 
2528 /**
2529  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2530  * @vsi: the VSI being cleaned up
2531  */
2532 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2533 {
2534 	struct ice_pf *pf = vsi->back;
2535 	struct ice_hw *hw = &pf->hw;
2536 	u32 txq = 0;
2537 	u32 rxq = 0;
2538 	int i, q;
2539 
2540 	ice_for_each_q_vector(vsi, i) {
2541 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2542 
2543 		ice_write_intrl(q_vector, 0);
2544 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2545 			ice_write_itr(&q_vector->tx, 0);
2546 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2547 			if (ice_is_xdp_ena_vsi(vsi)) {
2548 				u32 xdp_txq = txq + vsi->num_xdp_txq;
2549 
2550 				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2551 			}
2552 			txq++;
2553 		}
2554 
2555 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2556 			ice_write_itr(&q_vector->rx, 0);
2557 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2558 			rxq++;
2559 		}
2560 	}
2561 
2562 	ice_flush(hw);
2563 }
2564 
2565 /**
2566  * ice_vsi_free_irq - Free the IRQ association with the OS
2567  * @vsi: the VSI being configured
2568  */
2569 void ice_vsi_free_irq(struct ice_vsi *vsi)
2570 {
2571 	struct ice_pf *pf = vsi->back;
2572 	int i;
2573 
2574 	if (!vsi->q_vectors || !vsi->irqs_ready)
2575 		return;
2576 
2577 	ice_vsi_release_msix(vsi);
2578 	if (vsi->type == ICE_VSI_VF)
2579 		return;
2580 
2581 	vsi->irqs_ready = false;
2582 	ice_free_cpu_rx_rmap(vsi);
2583 
2584 	ice_for_each_q_vector(vsi, i) {
2585 		int irq_num;
2586 
2587 		irq_num = vsi->q_vectors[i]->irq.virq;
2588 
2589 		/* free only the irqs that were actually requested */
2590 		if (!vsi->q_vectors[i] ||
2591 		    !(vsi->q_vectors[i]->num_ring_tx ||
2592 		      vsi->q_vectors[i]->num_ring_rx))
2593 			continue;
2594 
2595 		/* clear the affinity notifier in the IRQ descriptor */
2596 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2597 			irq_set_affinity_notifier(irq_num, NULL);
2598 
2599 		/* clear the affinity_mask in the IRQ descriptor */
2600 		irq_set_affinity_hint(irq_num, NULL);
2601 		synchronize_irq(irq_num);
2602 		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2603 	}
2604 }
2605 
2606 /**
2607  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2608  * @vsi: the VSI having resources freed
2609  */
2610 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2611 {
2612 	int i;
2613 
2614 	if (!vsi->tx_rings)
2615 		return;
2616 
2617 	ice_for_each_txq(vsi, i)
2618 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2619 			ice_free_tx_ring(vsi->tx_rings[i]);
2620 }
2621 
2622 /**
2623  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2624  * @vsi: the VSI having resources freed
2625  */
2626 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2627 {
2628 	int i;
2629 
2630 	if (!vsi->rx_rings)
2631 		return;
2632 
2633 	ice_for_each_rxq(vsi, i)
2634 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2635 			ice_free_rx_ring(vsi->rx_rings[i]);
2636 }
2637 
2638 /**
2639  * ice_vsi_close - Shut down a VSI
2640  * @vsi: the VSI being shut down
2641  */
2642 void ice_vsi_close(struct ice_vsi *vsi)
2643 {
2644 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2645 		ice_down(vsi);
2646 
2647 	ice_vsi_free_irq(vsi);
2648 	ice_vsi_free_tx_rings(vsi);
2649 	ice_vsi_free_rx_rings(vsi);
2650 }
2651 
2652 /**
2653  * ice_ena_vsi - resume a VSI
2654  * @vsi: the VSI being resume
2655  * @locked: is the rtnl_lock already held
2656  */
2657 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2658 {
2659 	int err = 0;
2660 
2661 	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2662 		return 0;
2663 
2664 	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2665 
2666 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2667 		if (netif_running(vsi->netdev)) {
2668 			if (!locked)
2669 				rtnl_lock();
2670 
2671 			err = ice_open_internal(vsi->netdev);
2672 
2673 			if (!locked)
2674 				rtnl_unlock();
2675 		}
2676 	} else if (vsi->type == ICE_VSI_CTRL) {
2677 		err = ice_vsi_open_ctrl(vsi);
2678 	}
2679 
2680 	return err;
2681 }
2682 
2683 /**
2684  * ice_dis_vsi - pause a VSI
2685  * @vsi: the VSI being paused
2686  * @locked: is the rtnl_lock already held
2687  */
2688 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2689 {
2690 	if (test_bit(ICE_VSI_DOWN, vsi->state))
2691 		return;
2692 
2693 	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2694 
2695 	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2696 		if (netif_running(vsi->netdev)) {
2697 			if (!locked)
2698 				rtnl_lock();
2699 
2700 			ice_vsi_close(vsi);
2701 
2702 			if (!locked)
2703 				rtnl_unlock();
2704 		} else {
2705 			ice_vsi_close(vsi);
2706 		}
2707 	} else if (vsi->type == ICE_VSI_CTRL) {
2708 		ice_vsi_close(vsi);
2709 	}
2710 }
2711 
2712 /**
2713  * __ice_queue_set_napi - Set the napi instance for the queue
2714  * @dev: device to which NAPI and queue belong
2715  * @queue_index: Index of queue
2716  * @type: queue type as RX or TX
2717  * @napi: NAPI context
2718  * @locked: is the rtnl_lock already held
2719  *
2720  * Set the napi instance for the queue. Caller indicates the lock status.
2721  */
2722 static void
2723 __ice_queue_set_napi(struct net_device *dev, unsigned int queue_index,
2724 		     enum netdev_queue_type type, struct napi_struct *napi,
2725 		     bool locked)
2726 {
2727 	if (!locked)
2728 		rtnl_lock();
2729 	netif_queue_set_napi(dev, queue_index, type, napi);
2730 	if (!locked)
2731 		rtnl_unlock();
2732 }
2733 
2734 /**
2735  * ice_queue_set_napi - Set the napi instance for the queue
2736  * @vsi: VSI being configured
2737  * @queue_index: Index of queue
2738  * @type: queue type as RX or TX
2739  * @napi: NAPI context
2740  *
2741  * Set the napi instance for the queue. The rtnl lock state is derived from the
2742  * execution path.
2743  */
2744 void
2745 ice_queue_set_napi(struct ice_vsi *vsi, unsigned int queue_index,
2746 		   enum netdev_queue_type type, struct napi_struct *napi)
2747 {
2748 	struct ice_pf *pf = vsi->back;
2749 
2750 	if (!vsi->netdev)
2751 		return;
2752 
2753 	if (current_work() == &pf->serv_task ||
2754 	    test_bit(ICE_PREPARED_FOR_RESET, pf->state) ||
2755 	    test_bit(ICE_DOWN, pf->state) ||
2756 	    test_bit(ICE_SUSPENDED, pf->state))
2757 		__ice_queue_set_napi(vsi->netdev, queue_index, type, napi,
2758 				     false);
2759 	else
2760 		__ice_queue_set_napi(vsi->netdev, queue_index, type, napi,
2761 				     true);
2762 }
2763 
2764 /**
2765  * __ice_q_vector_set_napi_queues - Map queue[s] associated with the napi
2766  * @q_vector: q_vector pointer
2767  * @locked: is the rtnl_lock already held
2768  *
2769  * Associate the q_vector napi with all the queue[s] on the vector.
2770  * Caller indicates the lock status.
2771  */
2772 void __ice_q_vector_set_napi_queues(struct ice_q_vector *q_vector, bool locked)
2773 {
2774 	struct ice_rx_ring *rx_ring;
2775 	struct ice_tx_ring *tx_ring;
2776 
2777 	ice_for_each_rx_ring(rx_ring, q_vector->rx)
2778 		__ice_queue_set_napi(q_vector->vsi->netdev, rx_ring->q_index,
2779 				     NETDEV_QUEUE_TYPE_RX, &q_vector->napi,
2780 				     locked);
2781 
2782 	ice_for_each_tx_ring(tx_ring, q_vector->tx)
2783 		__ice_queue_set_napi(q_vector->vsi->netdev, tx_ring->q_index,
2784 				     NETDEV_QUEUE_TYPE_TX, &q_vector->napi,
2785 				     locked);
2786 	/* Also set the interrupt number for the NAPI */
2787 	netif_napi_set_irq(&q_vector->napi, q_vector->irq.virq);
2788 }
2789 
2790 /**
2791  * ice_q_vector_set_napi_queues - Map queue[s] associated with the napi
2792  * @q_vector: q_vector pointer
2793  *
2794  * Associate the q_vector napi with all the queue[s] on the vector
2795  */
2796 void ice_q_vector_set_napi_queues(struct ice_q_vector *q_vector)
2797 {
2798 	struct ice_rx_ring *rx_ring;
2799 	struct ice_tx_ring *tx_ring;
2800 
2801 	ice_for_each_rx_ring(rx_ring, q_vector->rx)
2802 		ice_queue_set_napi(q_vector->vsi, rx_ring->q_index,
2803 				   NETDEV_QUEUE_TYPE_RX, &q_vector->napi);
2804 
2805 	ice_for_each_tx_ring(tx_ring, q_vector->tx)
2806 		ice_queue_set_napi(q_vector->vsi, tx_ring->q_index,
2807 				   NETDEV_QUEUE_TYPE_TX, &q_vector->napi);
2808 	/* Also set the interrupt number for the NAPI */
2809 	netif_napi_set_irq(&q_vector->napi, q_vector->irq.virq);
2810 }
2811 
2812 /**
2813  * ice_vsi_set_napi_queues
2814  * @vsi: VSI pointer
2815  *
2816  * Associate queue[s] with napi for all vectors
2817  */
2818 void ice_vsi_set_napi_queues(struct ice_vsi *vsi)
2819 {
2820 	int i;
2821 
2822 	if (!vsi->netdev)
2823 		return;
2824 
2825 	ice_for_each_q_vector(vsi, i)
2826 		ice_q_vector_set_napi_queues(vsi->q_vectors[i]);
2827 }
2828 
2829 /**
2830  * ice_vsi_release - Delete a VSI and free its resources
2831  * @vsi: the VSI being removed
2832  *
2833  * Returns 0 on success or < 0 on error
2834  */
2835 int ice_vsi_release(struct ice_vsi *vsi)
2836 {
2837 	struct ice_pf *pf;
2838 
2839 	if (!vsi->back)
2840 		return -ENODEV;
2841 	pf = vsi->back;
2842 
2843 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2844 		ice_rss_clean(vsi);
2845 
2846 	ice_vsi_close(vsi);
2847 	ice_vsi_decfg(vsi);
2848 
2849 	/* retain SW VSI data structure since it is needed to unregister and
2850 	 * free VSI netdev when PF is not in reset recovery pending state,\
2851 	 * for ex: during rmmod.
2852 	 */
2853 	if (!ice_is_reset_in_progress(pf->state))
2854 		ice_vsi_delete(vsi);
2855 
2856 	return 0;
2857 }
2858 
2859 /**
2860  * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2861  * @vsi: VSI connected with q_vectors
2862  * @coalesce: array of struct with stored coalesce
2863  *
2864  * Returns array size.
2865  */
2866 static int
2867 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2868 			     struct ice_coalesce_stored *coalesce)
2869 {
2870 	int i;
2871 
2872 	ice_for_each_q_vector(vsi, i) {
2873 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2874 
2875 		coalesce[i].itr_tx = q_vector->tx.itr_settings;
2876 		coalesce[i].itr_rx = q_vector->rx.itr_settings;
2877 		coalesce[i].intrl = q_vector->intrl;
2878 
2879 		if (i < vsi->num_txq)
2880 			coalesce[i].tx_valid = true;
2881 		if (i < vsi->num_rxq)
2882 			coalesce[i].rx_valid = true;
2883 	}
2884 
2885 	return vsi->num_q_vectors;
2886 }
2887 
2888 /**
2889  * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2890  * @vsi: VSI connected with q_vectors
2891  * @coalesce: pointer to array of struct with stored coalesce
2892  * @size: size of coalesce array
2893  *
2894  * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2895  * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2896  * to default value.
2897  */
2898 static void
2899 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2900 			     struct ice_coalesce_stored *coalesce, int size)
2901 {
2902 	struct ice_ring_container *rc;
2903 	int i;
2904 
2905 	if ((size && !coalesce) || !vsi)
2906 		return;
2907 
2908 	/* There are a couple of cases that have to be handled here:
2909 	 *   1. The case where the number of queue vectors stays the same, but
2910 	 *      the number of Tx or Rx rings changes (the first for loop)
2911 	 *   2. The case where the number of queue vectors increased (the
2912 	 *      second for loop)
2913 	 */
2914 	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2915 		/* There are 2 cases to handle here and they are the same for
2916 		 * both Tx and Rx:
2917 		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
2918 		 *   and the loop variable is less than the number of rings
2919 		 *   allocated, then write the previous values
2920 		 *
2921 		 *   if the entry was not valid previously, but the number of
2922 		 *   rings is less than are allocated (this means the number of
2923 		 *   rings increased from previously), then write out the
2924 		 *   values in the first element
2925 		 *
2926 		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
2927 		 *   as there is no harm because the dynamic algorithm
2928 		 *   will just overwrite.
2929 		 */
2930 		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2931 			rc = &vsi->q_vectors[i]->rx;
2932 			rc->itr_settings = coalesce[i].itr_rx;
2933 			ice_write_itr(rc, rc->itr_setting);
2934 		} else if (i < vsi->alloc_rxq) {
2935 			rc = &vsi->q_vectors[i]->rx;
2936 			rc->itr_settings = coalesce[0].itr_rx;
2937 			ice_write_itr(rc, rc->itr_setting);
2938 		}
2939 
2940 		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2941 			rc = &vsi->q_vectors[i]->tx;
2942 			rc->itr_settings = coalesce[i].itr_tx;
2943 			ice_write_itr(rc, rc->itr_setting);
2944 		} else if (i < vsi->alloc_txq) {
2945 			rc = &vsi->q_vectors[i]->tx;
2946 			rc->itr_settings = coalesce[0].itr_tx;
2947 			ice_write_itr(rc, rc->itr_setting);
2948 		}
2949 
2950 		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2951 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2952 	}
2953 
2954 	/* the number of queue vectors increased so write whatever is in
2955 	 * the first element
2956 	 */
2957 	for (; i < vsi->num_q_vectors; i++) {
2958 		/* transmit */
2959 		rc = &vsi->q_vectors[i]->tx;
2960 		rc->itr_settings = coalesce[0].itr_tx;
2961 		ice_write_itr(rc, rc->itr_setting);
2962 
2963 		/* receive */
2964 		rc = &vsi->q_vectors[i]->rx;
2965 		rc->itr_settings = coalesce[0].itr_rx;
2966 		ice_write_itr(rc, rc->itr_setting);
2967 
2968 		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
2969 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2970 	}
2971 }
2972 
2973 /**
2974  * ice_vsi_realloc_stat_arrays - Frees unused stat structures or alloc new ones
2975  * @vsi: VSI pointer
2976  */
2977 static int
2978 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi)
2979 {
2980 	u16 req_txq = vsi->req_txq ? vsi->req_txq : vsi->alloc_txq;
2981 	u16 req_rxq = vsi->req_rxq ? vsi->req_rxq : vsi->alloc_rxq;
2982 	struct ice_ring_stats **tx_ring_stats;
2983 	struct ice_ring_stats **rx_ring_stats;
2984 	struct ice_vsi_stats *vsi_stat;
2985 	struct ice_pf *pf = vsi->back;
2986 	u16 prev_txq = vsi->alloc_txq;
2987 	u16 prev_rxq = vsi->alloc_rxq;
2988 	int i;
2989 
2990 	vsi_stat = pf->vsi_stats[vsi->idx];
2991 
2992 	if (req_txq < prev_txq) {
2993 		for (i = req_txq; i < prev_txq; i++) {
2994 			if (vsi_stat->tx_ring_stats[i]) {
2995 				kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
2996 				WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
2997 			}
2998 		}
2999 	}
3000 
3001 	tx_ring_stats = vsi_stat->tx_ring_stats;
3002 	vsi_stat->tx_ring_stats =
3003 		krealloc_array(vsi_stat->tx_ring_stats, req_txq,
3004 			       sizeof(*vsi_stat->tx_ring_stats),
3005 			       GFP_KERNEL | __GFP_ZERO);
3006 	if (!vsi_stat->tx_ring_stats) {
3007 		vsi_stat->tx_ring_stats = tx_ring_stats;
3008 		return -ENOMEM;
3009 	}
3010 
3011 	if (req_rxq < prev_rxq) {
3012 		for (i = req_rxq; i < prev_rxq; i++) {
3013 			if (vsi_stat->rx_ring_stats[i]) {
3014 				kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
3015 				WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3016 			}
3017 		}
3018 	}
3019 
3020 	rx_ring_stats = vsi_stat->rx_ring_stats;
3021 	vsi_stat->rx_ring_stats =
3022 		krealloc_array(vsi_stat->rx_ring_stats, req_rxq,
3023 			       sizeof(*vsi_stat->rx_ring_stats),
3024 			       GFP_KERNEL | __GFP_ZERO);
3025 	if (!vsi_stat->rx_ring_stats) {
3026 		vsi_stat->rx_ring_stats = rx_ring_stats;
3027 		return -ENOMEM;
3028 	}
3029 
3030 	return 0;
3031 }
3032 
3033 /**
3034  * ice_vsi_rebuild - Rebuild VSI after reset
3035  * @vsi: VSI to be rebuild
3036  * @vsi_flags: flags used for VSI rebuild flow
3037  *
3038  * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3039  * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3040  *
3041  * Returns 0 on success and negative value on failure
3042  */
3043 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3044 {
3045 	struct ice_vsi_cfg_params params = {};
3046 	struct ice_coalesce_stored *coalesce;
3047 	int prev_num_q_vectors;
3048 	struct ice_pf *pf;
3049 	int ret;
3050 
3051 	if (!vsi)
3052 		return -EINVAL;
3053 
3054 	params = ice_vsi_to_params(vsi);
3055 	params.flags = vsi_flags;
3056 
3057 	pf = vsi->back;
3058 	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3059 		return -EINVAL;
3060 
3061 	ret = ice_vsi_realloc_stat_arrays(vsi);
3062 	if (ret)
3063 		goto err_vsi_cfg;
3064 
3065 	ice_vsi_decfg(vsi);
3066 	ret = ice_vsi_cfg_def(vsi, &params);
3067 	if (ret)
3068 		goto err_vsi_cfg;
3069 
3070 	coalesce = kcalloc(vsi->num_q_vectors,
3071 			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3072 	if (!coalesce)
3073 		return -ENOMEM;
3074 
3075 	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3076 
3077 	ret = ice_vsi_cfg_tc_lan(pf, vsi);
3078 	if (ret) {
3079 		if (vsi_flags & ICE_VSI_FLAG_INIT) {
3080 			ret = -EIO;
3081 			goto err_vsi_cfg_tc_lan;
3082 		}
3083 
3084 		kfree(coalesce);
3085 		return ice_schedule_reset(pf, ICE_RESET_PFR);
3086 	}
3087 
3088 	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3089 	kfree(coalesce);
3090 
3091 	return 0;
3092 
3093 err_vsi_cfg_tc_lan:
3094 	ice_vsi_decfg(vsi);
3095 	kfree(coalesce);
3096 err_vsi_cfg:
3097 	return ret;
3098 }
3099 
3100 /**
3101  * ice_is_reset_in_progress - check for a reset in progress
3102  * @state: PF state field
3103  */
3104 bool ice_is_reset_in_progress(unsigned long *state)
3105 {
3106 	return test_bit(ICE_RESET_OICR_RECV, state) ||
3107 	       test_bit(ICE_PFR_REQ, state) ||
3108 	       test_bit(ICE_CORER_REQ, state) ||
3109 	       test_bit(ICE_GLOBR_REQ, state);
3110 }
3111 
3112 /**
3113  * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3114  * @pf: pointer to the PF structure
3115  * @timeout: length of time to wait, in jiffies
3116  *
3117  * Wait (sleep) for a short time until the driver finishes cleaning up from
3118  * a device reset. The caller must be able to sleep. Use this to delay
3119  * operations that could fail while the driver is cleaning up after a device
3120  * reset.
3121  *
3122  * Returns 0 on success, -EBUSY if the reset is not finished within the
3123  * timeout, and -ERESTARTSYS if the thread was interrupted.
3124  */
3125 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3126 {
3127 	long ret;
3128 
3129 	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3130 					       !ice_is_reset_in_progress(pf->state),
3131 					       timeout);
3132 	if (ret < 0)
3133 		return ret;
3134 	else if (!ret)
3135 		return -EBUSY;
3136 	else
3137 		return 0;
3138 }
3139 
3140 /**
3141  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3142  * @vsi: VSI being configured
3143  * @ctx: the context buffer returned from AQ VSI update command
3144  */
3145 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3146 {
3147 	vsi->info.mapping_flags = ctx->info.mapping_flags;
3148 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3149 	       sizeof(vsi->info.q_mapping));
3150 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3151 	       sizeof(vsi->info.tc_mapping));
3152 }
3153 
3154 /**
3155  * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3156  * @vsi: the VSI being configured
3157  * @ena_tc: TC map to be enabled
3158  */
3159 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3160 {
3161 	struct net_device *netdev = vsi->netdev;
3162 	struct ice_pf *pf = vsi->back;
3163 	int numtc = vsi->tc_cfg.numtc;
3164 	struct ice_dcbx_cfg *dcbcfg;
3165 	u8 netdev_tc;
3166 	int i;
3167 
3168 	if (!netdev)
3169 		return;
3170 
3171 	/* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3172 	if (vsi->type == ICE_VSI_CHNL)
3173 		return;
3174 
3175 	if (!ena_tc) {
3176 		netdev_reset_tc(netdev);
3177 		return;
3178 	}
3179 
3180 	if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3181 		numtc = vsi->all_numtc;
3182 
3183 	if (netdev_set_num_tc(netdev, numtc))
3184 		return;
3185 
3186 	dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3187 
3188 	ice_for_each_traffic_class(i)
3189 		if (vsi->tc_cfg.ena_tc & BIT(i))
3190 			netdev_set_tc_queue(netdev,
3191 					    vsi->tc_cfg.tc_info[i].netdev_tc,
3192 					    vsi->tc_cfg.tc_info[i].qcount_tx,
3193 					    vsi->tc_cfg.tc_info[i].qoffset);
3194 	/* setup TC queue map for CHNL TCs */
3195 	ice_for_each_chnl_tc(i) {
3196 		if (!(vsi->all_enatc & BIT(i)))
3197 			break;
3198 		if (!vsi->mqprio_qopt.qopt.count[i])
3199 			break;
3200 		netdev_set_tc_queue(netdev, i,
3201 				    vsi->mqprio_qopt.qopt.count[i],
3202 				    vsi->mqprio_qopt.qopt.offset[i]);
3203 	}
3204 
3205 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3206 		return;
3207 
3208 	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3209 		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3210 
3211 		/* Get the mapped netdev TC# for the UP */
3212 		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3213 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3214 	}
3215 }
3216 
3217 /**
3218  * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3219  * @vsi: the VSI being configured,
3220  * @ctxt: VSI context structure
3221  * @ena_tc: number of traffic classes to enable
3222  *
3223  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3224  */
3225 static int
3226 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3227 			   u8 ena_tc)
3228 {
3229 	u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3230 	u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3231 	int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3232 	u16 new_txq, new_rxq;
3233 	u8 netdev_tc = 0;
3234 	int i;
3235 
3236 	vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3237 
3238 	pow = order_base_2(tc0_qcount);
3239 	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, tc0_offset);
3240 	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
3241 
3242 	ice_for_each_traffic_class(i) {
3243 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3244 			/* TC is not enabled */
3245 			vsi->tc_cfg.tc_info[i].qoffset = 0;
3246 			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3247 			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3248 			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3249 			ctxt->info.tc_mapping[i] = 0;
3250 			continue;
3251 		}
3252 
3253 		offset = vsi->mqprio_qopt.qopt.offset[i];
3254 		qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3255 		qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3256 		vsi->tc_cfg.tc_info[i].qoffset = offset;
3257 		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3258 		vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3259 		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3260 	}
3261 
3262 	if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3263 		ice_for_each_chnl_tc(i) {
3264 			if (!(vsi->all_enatc & BIT(i)))
3265 				continue;
3266 			offset = vsi->mqprio_qopt.qopt.offset[i];
3267 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3268 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3269 		}
3270 	}
3271 
3272 	new_txq = offset + qcount_tx;
3273 	if (new_txq > vsi->alloc_txq) {
3274 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3275 			new_txq, vsi->alloc_txq);
3276 		return -EINVAL;
3277 	}
3278 
3279 	new_rxq = offset + qcount_rx;
3280 	if (new_rxq > vsi->alloc_rxq) {
3281 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3282 			new_rxq, vsi->alloc_rxq);
3283 		return -EINVAL;
3284 	}
3285 
3286 	/* Set actual Tx/Rx queue pairs */
3287 	vsi->num_txq = new_txq;
3288 	vsi->num_rxq = new_rxq;
3289 
3290 	/* Setup queue TC[0].qmap for given VSI context */
3291 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3292 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3293 	ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3294 
3295 	/* Find queue count available for channel VSIs and starting offset
3296 	 * for channel VSIs
3297 	 */
3298 	if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3299 		vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3300 		vsi->next_base_q = tc0_qcount;
3301 	}
3302 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n",  vsi->num_txq);
3303 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n",  vsi->num_rxq);
3304 	dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3305 		vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3306 
3307 	return 0;
3308 }
3309 
3310 /**
3311  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3312  * @vsi: VSI to be configured
3313  * @ena_tc: TC bitmap
3314  *
3315  * VSI queues expected to be quiesced before calling this function
3316  */
3317 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3318 {
3319 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3320 	struct ice_pf *pf = vsi->back;
3321 	struct ice_tc_cfg old_tc_cfg;
3322 	struct ice_vsi_ctx *ctx;
3323 	struct device *dev;
3324 	int i, ret = 0;
3325 	u8 num_tc = 0;
3326 
3327 	dev = ice_pf_to_dev(pf);
3328 	if (vsi->tc_cfg.ena_tc == ena_tc &&
3329 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3330 		return 0;
3331 
3332 	ice_for_each_traffic_class(i) {
3333 		/* build bitmap of enabled TCs */
3334 		if (ena_tc & BIT(i))
3335 			num_tc++;
3336 		/* populate max_txqs per TC */
3337 		max_txqs[i] = vsi->alloc_txq;
3338 		/* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3339 		 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3340 		 */
3341 		if (vsi->type == ICE_VSI_CHNL &&
3342 		    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3343 			max_txqs[i] = vsi->num_txq;
3344 	}
3345 
3346 	memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3347 	vsi->tc_cfg.ena_tc = ena_tc;
3348 	vsi->tc_cfg.numtc = num_tc;
3349 
3350 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3351 	if (!ctx)
3352 		return -ENOMEM;
3353 
3354 	ctx->vf_num = 0;
3355 	ctx->info = vsi->info;
3356 
3357 	if (vsi->type == ICE_VSI_PF &&
3358 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3359 		ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3360 	else
3361 		ret = ice_vsi_setup_q_map(vsi, ctx);
3362 
3363 	if (ret) {
3364 		memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3365 		goto out;
3366 	}
3367 
3368 	/* must to indicate which section of VSI context are being modified */
3369 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3370 	ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3371 	if (ret) {
3372 		dev_info(dev, "Failed VSI Update\n");
3373 		goto out;
3374 	}
3375 
3376 	if (vsi->type == ICE_VSI_PF &&
3377 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3378 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3379 	else
3380 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3381 				      vsi->tc_cfg.ena_tc, max_txqs);
3382 
3383 	if (ret) {
3384 		dev_err(dev, "VSI %d failed TC config, error %d\n",
3385 			vsi->vsi_num, ret);
3386 		goto out;
3387 	}
3388 	ice_vsi_update_q_map(vsi, ctx);
3389 	vsi->info.valid_sections = 0;
3390 
3391 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3392 out:
3393 	kfree(ctx);
3394 	return ret;
3395 }
3396 
3397 /**
3398  * ice_update_ring_stats - Update ring statistics
3399  * @stats: stats to be updated
3400  * @pkts: number of processed packets
3401  * @bytes: number of processed bytes
3402  *
3403  * This function assumes that caller has acquired a u64_stats_sync lock.
3404  */
3405 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3406 {
3407 	stats->bytes += bytes;
3408 	stats->pkts += pkts;
3409 }
3410 
3411 /**
3412  * ice_update_tx_ring_stats - Update Tx ring specific counters
3413  * @tx_ring: ring to update
3414  * @pkts: number of processed packets
3415  * @bytes: number of processed bytes
3416  */
3417 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3418 {
3419 	u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3420 	ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3421 	u64_stats_update_end(&tx_ring->ring_stats->syncp);
3422 }
3423 
3424 /**
3425  * ice_update_rx_ring_stats - Update Rx ring specific counters
3426  * @rx_ring: ring to update
3427  * @pkts: number of processed packets
3428  * @bytes: number of processed bytes
3429  */
3430 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3431 {
3432 	u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3433 	ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3434 	u64_stats_update_end(&rx_ring->ring_stats->syncp);
3435 }
3436 
3437 /**
3438  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3439  * @pi: port info of the switch with default VSI
3440  *
3441  * Return true if the there is a single VSI in default forwarding VSI list
3442  */
3443 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3444 {
3445 	bool exists = false;
3446 
3447 	ice_check_if_dflt_vsi(pi, 0, &exists);
3448 	return exists;
3449 }
3450 
3451 /**
3452  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3453  * @vsi: VSI to compare against default forwarding VSI
3454  *
3455  * If this VSI passed in is the default forwarding VSI then return true, else
3456  * return false
3457  */
3458 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3459 {
3460 	return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3461 }
3462 
3463 /**
3464  * ice_set_dflt_vsi - set the default forwarding VSI
3465  * @vsi: VSI getting set as the default forwarding VSI on the switch
3466  *
3467  * If the VSI passed in is already the default VSI and it's enabled just return
3468  * success.
3469  *
3470  * Otherwise try to set the VSI passed in as the switch's default VSI and
3471  * return the result.
3472  */
3473 int ice_set_dflt_vsi(struct ice_vsi *vsi)
3474 {
3475 	struct device *dev;
3476 	int status;
3477 
3478 	if (!vsi)
3479 		return -EINVAL;
3480 
3481 	dev = ice_pf_to_dev(vsi->back);
3482 
3483 	if (ice_lag_is_switchdev_running(vsi->back)) {
3484 		dev_dbg(dev, "VSI %d passed is a part of LAG containing interfaces in switchdev mode, nothing to do\n",
3485 			vsi->vsi_num);
3486 		return 0;
3487 	}
3488 
3489 	/* the VSI passed in is already the default VSI */
3490 	if (ice_is_vsi_dflt_vsi(vsi)) {
3491 		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3492 			vsi->vsi_num);
3493 		return 0;
3494 	}
3495 
3496 	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3497 	if (status) {
3498 		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3499 			vsi->vsi_num, status);
3500 		return status;
3501 	}
3502 
3503 	return 0;
3504 }
3505 
3506 /**
3507  * ice_clear_dflt_vsi - clear the default forwarding VSI
3508  * @vsi: VSI to remove from filter list
3509  *
3510  * If the switch has no default VSI or it's not enabled then return error.
3511  *
3512  * Otherwise try to clear the default VSI and return the result.
3513  */
3514 int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3515 {
3516 	struct device *dev;
3517 	int status;
3518 
3519 	if (!vsi)
3520 		return -EINVAL;
3521 
3522 	dev = ice_pf_to_dev(vsi->back);
3523 
3524 	/* there is no default VSI configured */
3525 	if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3526 		return -ENODEV;
3527 
3528 	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3529 				  ICE_FLTR_RX);
3530 	if (status) {
3531 		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3532 			vsi->vsi_num, status);
3533 		return -EIO;
3534 	}
3535 
3536 	return 0;
3537 }
3538 
3539 /**
3540  * ice_get_link_speed_mbps - get link speed in Mbps
3541  * @vsi: the VSI whose link speed is being queried
3542  *
3543  * Return current VSI link speed and 0 if the speed is unknown.
3544  */
3545 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3546 {
3547 	unsigned int link_speed;
3548 
3549 	link_speed = vsi->port_info->phy.link_info.link_speed;
3550 
3551 	return (int)ice_get_link_speed(fls(link_speed) - 1);
3552 }
3553 
3554 /**
3555  * ice_get_link_speed_kbps - get link speed in Kbps
3556  * @vsi: the VSI whose link speed is being queried
3557  *
3558  * Return current VSI link speed and 0 if the speed is unknown.
3559  */
3560 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3561 {
3562 	int speed_mbps;
3563 
3564 	speed_mbps = ice_get_link_speed_mbps(vsi);
3565 
3566 	return speed_mbps * 1000;
3567 }
3568 
3569 /**
3570  * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3571  * @vsi: VSI to be configured
3572  * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3573  *
3574  * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3575  * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3576  * on TC 0.
3577  */
3578 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3579 {
3580 	struct ice_pf *pf = vsi->back;
3581 	struct device *dev;
3582 	int status;
3583 	int speed;
3584 
3585 	dev = ice_pf_to_dev(pf);
3586 	if (!vsi->port_info) {
3587 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3588 			vsi->idx, vsi->type);
3589 		return -EINVAL;
3590 	}
3591 
3592 	speed = ice_get_link_speed_kbps(vsi);
3593 	if (min_tx_rate > (u64)speed) {
3594 		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3595 			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3596 			speed);
3597 		return -EINVAL;
3598 	}
3599 
3600 	/* Configure min BW for VSI limit */
3601 	if (min_tx_rate) {
3602 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3603 						   ICE_MIN_BW, min_tx_rate);
3604 		if (status) {
3605 			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3606 				min_tx_rate, ice_vsi_type_str(vsi->type),
3607 				vsi->idx);
3608 			return status;
3609 		}
3610 
3611 		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3612 			min_tx_rate, ice_vsi_type_str(vsi->type));
3613 	} else {
3614 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3615 							vsi->idx, 0,
3616 							ICE_MIN_BW);
3617 		if (status) {
3618 			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3619 				ice_vsi_type_str(vsi->type), vsi->idx);
3620 			return status;
3621 		}
3622 
3623 		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3624 			ice_vsi_type_str(vsi->type), vsi->idx);
3625 	}
3626 
3627 	return 0;
3628 }
3629 
3630 /**
3631  * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3632  * @vsi: VSI to be configured
3633  * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3634  *
3635  * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3636  * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3637  * on TC 0.
3638  */
3639 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3640 {
3641 	struct ice_pf *pf = vsi->back;
3642 	struct device *dev;
3643 	int status;
3644 	int speed;
3645 
3646 	dev = ice_pf_to_dev(pf);
3647 	if (!vsi->port_info) {
3648 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3649 			vsi->idx, vsi->type);
3650 		return -EINVAL;
3651 	}
3652 
3653 	speed = ice_get_link_speed_kbps(vsi);
3654 	if (max_tx_rate > (u64)speed) {
3655 		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3656 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3657 			speed);
3658 		return -EINVAL;
3659 	}
3660 
3661 	/* Configure max BW for VSI limit */
3662 	if (max_tx_rate) {
3663 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3664 						   ICE_MAX_BW, max_tx_rate);
3665 		if (status) {
3666 			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3667 				max_tx_rate, ice_vsi_type_str(vsi->type),
3668 				vsi->idx);
3669 			return status;
3670 		}
3671 
3672 		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3673 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3674 	} else {
3675 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3676 							vsi->idx, 0,
3677 							ICE_MAX_BW);
3678 		if (status) {
3679 			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3680 				ice_vsi_type_str(vsi->type), vsi->idx);
3681 			return status;
3682 		}
3683 
3684 		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3685 			ice_vsi_type_str(vsi->type), vsi->idx);
3686 	}
3687 
3688 	return 0;
3689 }
3690 
3691 /**
3692  * ice_set_link - turn on/off physical link
3693  * @vsi: VSI to modify physical link on
3694  * @ena: turn on/off physical link
3695  */
3696 int ice_set_link(struct ice_vsi *vsi, bool ena)
3697 {
3698 	struct device *dev = ice_pf_to_dev(vsi->back);
3699 	struct ice_port_info *pi = vsi->port_info;
3700 	struct ice_hw *hw = pi->hw;
3701 	int status;
3702 
3703 	if (vsi->type != ICE_VSI_PF)
3704 		return -EINVAL;
3705 
3706 	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3707 
3708 	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3709 	 * this is not a fatal error, so print a warning message and return
3710 	 * a success code. Return an error if FW returns an error code other
3711 	 * than ICE_AQ_RC_EMODE
3712 	 */
3713 	if (status == -EIO) {
3714 		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3715 			dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3716 				(ena ? "ON" : "OFF"), status,
3717 				ice_aq_str(hw->adminq.sq_last_status));
3718 	} else if (status) {
3719 		dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3720 			(ena ? "ON" : "OFF"), status,
3721 			ice_aq_str(hw->adminq.sq_last_status));
3722 		return status;
3723 	}
3724 
3725 	return 0;
3726 }
3727 
3728 /**
3729  * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3730  * @vsi: VSI used to add VLAN filters
3731  *
3732  * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3733  * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3734  * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3735  * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3736  *
3737  * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3738  * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3739  * traffic in SVM, since the VLAN TPID isn't part of filtering.
3740  *
3741  * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3742  * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3743  * part of filtering.
3744  */
3745 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3746 {
3747 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3748 	struct ice_vlan vlan;
3749 	int err;
3750 
3751 	vlan = ICE_VLAN(0, 0, 0);
3752 	err = vlan_ops->add_vlan(vsi, &vlan);
3753 	if (err && err != -EEXIST)
3754 		return err;
3755 
3756 	/* in SVM both VLAN 0 filters are identical */
3757 	if (!ice_is_dvm_ena(&vsi->back->hw))
3758 		return 0;
3759 
3760 	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3761 	err = vlan_ops->add_vlan(vsi, &vlan);
3762 	if (err && err != -EEXIST)
3763 		return err;
3764 
3765 	return 0;
3766 }
3767 
3768 /**
3769  * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3770  * @vsi: VSI used to add VLAN filters
3771  *
3772  * Delete the VLAN 0 filters in the same manner that they were added in
3773  * ice_vsi_add_vlan_zero.
3774  */
3775 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3776 {
3777 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3778 	struct ice_vlan vlan;
3779 	int err;
3780 
3781 	vlan = ICE_VLAN(0, 0, 0);
3782 	err = vlan_ops->del_vlan(vsi, &vlan);
3783 	if (err && err != -EEXIST)
3784 		return err;
3785 
3786 	/* in SVM both VLAN 0 filters are identical */
3787 	if (!ice_is_dvm_ena(&vsi->back->hw))
3788 		return 0;
3789 
3790 	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3791 	err = vlan_ops->del_vlan(vsi, &vlan);
3792 	if (err && err != -EEXIST)
3793 		return err;
3794 
3795 	/* when deleting the last VLAN filter, make sure to disable the VLAN
3796 	 * promisc mode so the filter isn't left by accident
3797 	 */
3798 	return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3799 				    ICE_MCAST_VLAN_PROMISC_BITS, 0);
3800 }
3801 
3802 /**
3803  * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3804  * @vsi: VSI used to get the VLAN mode
3805  *
3806  * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3807  * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3808  */
3809 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3810 {
3811 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS	2
3812 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS	1
3813 	/* no VLAN 0 filter is created when a port VLAN is active */
3814 	if (vsi->type == ICE_VSI_VF) {
3815 		if (WARN_ON(!vsi->vf))
3816 			return 0;
3817 
3818 		if (ice_vf_is_port_vlan_ena(vsi->vf))
3819 			return 0;
3820 	}
3821 
3822 	if (ice_is_dvm_ena(&vsi->back->hw))
3823 		return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3824 	else
3825 		return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3826 }
3827 
3828 /**
3829  * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3830  * @vsi: VSI used to determine if any non-zero VLANs have been added
3831  */
3832 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3833 {
3834 	return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3835 }
3836 
3837 /**
3838  * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3839  * @vsi: VSI used to get the number of non-zero VLANs added
3840  */
3841 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3842 {
3843 	return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3844 }
3845 
3846 /**
3847  * ice_is_feature_supported
3848  * @pf: pointer to the struct ice_pf instance
3849  * @f: feature enum to be checked
3850  *
3851  * returns true if feature is supported, false otherwise
3852  */
3853 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3854 {
3855 	if (f < 0 || f >= ICE_F_MAX)
3856 		return false;
3857 
3858 	return test_bit(f, pf->features);
3859 }
3860 
3861 /**
3862  * ice_set_feature_support
3863  * @pf: pointer to the struct ice_pf instance
3864  * @f: feature enum to set
3865  */
3866 void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
3867 {
3868 	if (f < 0 || f >= ICE_F_MAX)
3869 		return;
3870 
3871 	set_bit(f, pf->features);
3872 }
3873 
3874 /**
3875  * ice_clear_feature_support
3876  * @pf: pointer to the struct ice_pf instance
3877  * @f: feature enum to clear
3878  */
3879 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
3880 {
3881 	if (f < 0 || f >= ICE_F_MAX)
3882 		return;
3883 
3884 	clear_bit(f, pf->features);
3885 }
3886 
3887 /**
3888  * ice_init_feature_support
3889  * @pf: pointer to the struct ice_pf instance
3890  *
3891  * called during init to setup supported feature
3892  */
3893 void ice_init_feature_support(struct ice_pf *pf)
3894 {
3895 	switch (pf->hw.device_id) {
3896 	case ICE_DEV_ID_E810C_BACKPLANE:
3897 	case ICE_DEV_ID_E810C_QSFP:
3898 	case ICE_DEV_ID_E810C_SFP:
3899 	case ICE_DEV_ID_E810_XXV_BACKPLANE:
3900 	case ICE_DEV_ID_E810_XXV_QSFP:
3901 	case ICE_DEV_ID_E810_XXV_SFP:
3902 		ice_set_feature_support(pf, ICE_F_DSCP);
3903 		if (ice_is_phy_rclk_in_netlist(&pf->hw))
3904 			ice_set_feature_support(pf, ICE_F_PHY_RCLK);
3905 		/* If we don't own the timer - don't enable other caps */
3906 		if (!ice_pf_src_tmr_owned(pf))
3907 			break;
3908 		if (ice_is_cgu_in_netlist(&pf->hw))
3909 			ice_set_feature_support(pf, ICE_F_CGU);
3910 		if (ice_is_clock_mux_in_netlist(&pf->hw))
3911 			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
3912 		if (ice_gnss_is_gps_present(&pf->hw))
3913 			ice_set_feature_support(pf, ICE_F_GNSS);
3914 		break;
3915 	default:
3916 		break;
3917 	}
3918 }
3919 
3920 /**
3921  * ice_vsi_update_security - update security block in VSI
3922  * @vsi: pointer to VSI structure
3923  * @fill: function pointer to fill ctx
3924  */
3925 int
3926 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
3927 {
3928 	struct ice_vsi_ctx ctx = { 0 };
3929 
3930 	ctx.info = vsi->info;
3931 	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
3932 	fill(&ctx);
3933 
3934 	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3935 		return -ENODEV;
3936 
3937 	vsi->info = ctx.info;
3938 	return 0;
3939 }
3940 
3941 /**
3942  * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
3943  * @ctx: pointer to VSI ctx structure
3944  */
3945 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
3946 {
3947 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
3948 			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3949 				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3950 }
3951 
3952 /**
3953  * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
3954  * @ctx: pointer to VSI ctx structure
3955  */
3956 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
3957 {
3958 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
3959 			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3960 				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3961 }
3962 
3963 /**
3964  * ice_vsi_ctx_set_allow_override - allow destination override on VSI
3965  * @ctx: pointer to VSI ctx structure
3966  */
3967 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
3968 {
3969 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3970 }
3971 
3972 /**
3973  * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
3974  * @ctx: pointer to VSI ctx structure
3975  */
3976 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
3977 {
3978 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3979 }
3980 
3981 /**
3982  * ice_vsi_update_local_lb - update sw block in VSI with local loopback bit
3983  * @vsi: pointer to VSI structure
3984  * @set: set or unset the bit
3985  */
3986 int
3987 ice_vsi_update_local_lb(struct ice_vsi *vsi, bool set)
3988 {
3989 	struct ice_vsi_ctx ctx = {
3990 		.info	= vsi->info,
3991 	};
3992 
3993 	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
3994 	if (set)
3995 		ctx.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3996 	else
3997 		ctx.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3998 
3999 	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4000 		return -ENODEV;
4001 
4002 	vsi->info = ctx.info;
4003 	return 0;
4004 }
4005