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