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