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