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