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