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