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