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 		if (ice_is_xdp_ena_vsi(vsi)) {
2482 			ret = ice_vsi_determine_xdp_res(vsi);
2483 			if (ret)
2484 				goto unroll_vector_base;
2485 			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2486 			if (ret)
2487 				goto unroll_vector_base;
2488 		}
2489 
2490 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2491 		if (vsi->type != ICE_VSI_CTRL)
2492 			/* Do not exit if configuring RSS had an issue, at
2493 			 * least receive traffic on first queue. Hence no
2494 			 * need to capture return value
2495 			 */
2496 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2497 				ice_vsi_cfg_rss_lut_key(vsi);
2498 				ice_vsi_set_rss_flow_fld(vsi);
2499 			}
2500 		ice_init_arfs(vsi);
2501 		break;
2502 	case ICE_VSI_CHNL:
2503 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2504 			ice_vsi_cfg_rss_lut_key(vsi);
2505 			ice_vsi_set_rss_flow_fld(vsi);
2506 		}
2507 		break;
2508 	case ICE_VSI_VF:
2509 		/* VF driver will take care of creating netdev for this type and
2510 		 * map queues to vectors through Virtchnl, PF driver only
2511 		 * creates a VSI and corresponding structures for bookkeeping
2512 		 * purpose
2513 		 */
2514 		ret = ice_vsi_alloc_q_vectors(vsi);
2515 		if (ret)
2516 			goto unroll_vsi_init;
2517 
2518 		ret = ice_vsi_alloc_rings(vsi);
2519 		if (ret)
2520 			goto unroll_alloc_q_vector;
2521 
2522 		ret = ice_vsi_alloc_ring_stats(vsi);
2523 		if (ret)
2524 			goto unroll_vector_base;
2525 		/* Do not exit if configuring RSS had an issue, at least
2526 		 * receive traffic on first queue. Hence no need to capture
2527 		 * return value
2528 		 */
2529 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2530 			ice_vsi_cfg_rss_lut_key(vsi);
2531 			ice_vsi_set_vf_rss_flow_fld(vsi);
2532 		}
2533 		break;
2534 	case ICE_VSI_LB:
2535 		ret = ice_vsi_alloc_rings(vsi);
2536 		if (ret)
2537 			goto unroll_vsi_init;
2538 
2539 		ret = ice_vsi_alloc_ring_stats(vsi);
2540 		if (ret)
2541 			goto unroll_vector_base;
2542 
2543 		break;
2544 	default:
2545 		/* clean up the resources and exit */
2546 		ret = -EINVAL;
2547 		goto unroll_vsi_init;
2548 	}
2549 
2550 	return 0;
2551 
2552 unroll_vector_base:
2553 	/* reclaim SW interrupts back to the common pool */
2554 unroll_alloc_q_vector:
2555 	ice_vsi_free_q_vectors(vsi);
2556 unroll_vsi_init:
2557 	ice_vsi_delete_from_hw(vsi);
2558 unroll_get_qs:
2559 	ice_vsi_put_qs(vsi);
2560 unroll_vsi_alloc_stat:
2561 	ice_vsi_free_stats(vsi);
2562 unroll_vsi_alloc:
2563 	ice_vsi_free_arrays(vsi);
2564 	return ret;
2565 }
2566 
2567 /**
2568  * ice_vsi_cfg - configure a previously allocated VSI
2569  * @vsi: pointer to VSI
2570  * @params: parameters used to configure this VSI
2571  */
2572 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2573 {
2574 	struct ice_pf *pf = vsi->back;
2575 	int ret;
2576 
2577 	if (WARN_ON(params->type == ICE_VSI_VF && !params->vf))
2578 		return -EINVAL;
2579 
2580 	vsi->type = params->type;
2581 	vsi->port_info = params->pi;
2582 
2583 	/* For VSIs which don't have a connected VF, this will be NULL */
2584 	vsi->vf = params->vf;
2585 
2586 	ret = ice_vsi_cfg_def(vsi, params);
2587 	if (ret)
2588 		return ret;
2589 
2590 	ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2591 	if (ret)
2592 		ice_vsi_decfg(vsi);
2593 
2594 	if (vsi->type == ICE_VSI_CTRL) {
2595 		if (vsi->vf) {
2596 			WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2597 			vsi->vf->ctrl_vsi_idx = vsi->idx;
2598 		} else {
2599 			WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2600 			pf->ctrl_vsi_idx = vsi->idx;
2601 		}
2602 	}
2603 
2604 	return ret;
2605 }
2606 
2607 /**
2608  * ice_vsi_decfg - remove all VSI configuration
2609  * @vsi: pointer to VSI
2610  */
2611 void ice_vsi_decfg(struct ice_vsi *vsi)
2612 {
2613 	struct ice_pf *pf = vsi->back;
2614 	int err;
2615 
2616 	/* The Rx rule will only exist to remove if the LLDP FW
2617 	 * engine is currently stopped
2618 	 */
2619 	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2620 	    !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2621 		ice_cfg_sw_lldp(vsi, false, false);
2622 
2623 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2624 	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2625 	if (err)
2626 		dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2627 			vsi->vsi_num, err);
2628 
2629 	if (ice_is_xdp_ena_vsi(vsi))
2630 		/* return value check can be skipped here, it always returns
2631 		 * 0 if reset is in progress
2632 		 */
2633 		ice_destroy_xdp_rings(vsi);
2634 
2635 	ice_vsi_clear_rings(vsi);
2636 	ice_vsi_free_q_vectors(vsi);
2637 	ice_vsi_put_qs(vsi);
2638 	ice_vsi_free_arrays(vsi);
2639 
2640 	/* SR-IOV determines needed MSIX resources all at once instead of per
2641 	 * VSI since when VFs are spawned we know how many VFs there are and how
2642 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2643 	 * cleared in the same manner.
2644 	 */
2645 
2646 	if (vsi->type == ICE_VSI_VF &&
2647 	    vsi->agg_node && vsi->agg_node->valid)
2648 		vsi->agg_node->num_vsis--;
2649 	if (vsi->agg_node) {
2650 		vsi->agg_node->valid = false;
2651 		vsi->agg_node->agg_id = 0;
2652 	}
2653 }
2654 
2655 /**
2656  * ice_vsi_setup - Set up a VSI by a given type
2657  * @pf: board private structure
2658  * @params: parameters to use when creating the VSI
2659  *
2660  * This allocates the sw VSI structure and its queue resources.
2661  *
2662  * Returns pointer to the successfully allocated and configured VSI sw struct on
2663  * success, NULL on failure.
2664  */
2665 struct ice_vsi *
2666 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2667 {
2668 	struct device *dev = ice_pf_to_dev(pf);
2669 	struct ice_vsi *vsi;
2670 	int ret;
2671 
2672 	/* ice_vsi_setup can only initialize a new VSI, and we must have
2673 	 * a port_info structure for it.
2674 	 */
2675 	if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2676 	    WARN_ON(!params->pi))
2677 		return NULL;
2678 
2679 	vsi = ice_vsi_alloc(pf);
2680 	if (!vsi) {
2681 		dev_err(dev, "could not allocate VSI\n");
2682 		return NULL;
2683 	}
2684 
2685 	ret = ice_vsi_cfg(vsi, params);
2686 	if (ret)
2687 		goto err_vsi_cfg;
2688 
2689 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2690 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2691 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2692 	 * The rule is added once for PF VSI in order to create appropriate
2693 	 * recipe, since VSI/VSI list is ignored with drop action...
2694 	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2695 	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2696 	 * settings in the HW.
2697 	 */
2698 	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2699 		ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2700 				 ICE_DROP_PACKET);
2701 		ice_cfg_sw_lldp(vsi, true, true);
2702 	}
2703 
2704 	if (!vsi->agg_node)
2705 		ice_set_agg_vsi(vsi);
2706 
2707 	return vsi;
2708 
2709 err_vsi_cfg:
2710 	ice_vsi_free(vsi);
2711 
2712 	return NULL;
2713 }
2714 
2715 /**
2716  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2717  * @vsi: the VSI being cleaned up
2718  */
2719 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2720 {
2721 	struct ice_pf *pf = vsi->back;
2722 	struct ice_hw *hw = &pf->hw;
2723 	u32 txq = 0;
2724 	u32 rxq = 0;
2725 	int i, q;
2726 
2727 	ice_for_each_q_vector(vsi, i) {
2728 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2729 
2730 		ice_write_intrl(q_vector, 0);
2731 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2732 			ice_write_itr(&q_vector->tx, 0);
2733 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2734 			if (ice_is_xdp_ena_vsi(vsi)) {
2735 				u32 xdp_txq = txq + vsi->num_xdp_txq;
2736 
2737 				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2738 			}
2739 			txq++;
2740 		}
2741 
2742 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2743 			ice_write_itr(&q_vector->rx, 0);
2744 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2745 			rxq++;
2746 		}
2747 	}
2748 
2749 	ice_flush(hw);
2750 }
2751 
2752 /**
2753  * ice_vsi_free_irq - Free the IRQ association with the OS
2754  * @vsi: the VSI being configured
2755  */
2756 void ice_vsi_free_irq(struct ice_vsi *vsi)
2757 {
2758 	struct ice_pf *pf = vsi->back;
2759 	int i;
2760 
2761 	if (!vsi->q_vectors || !vsi->irqs_ready)
2762 		return;
2763 
2764 	ice_vsi_release_msix(vsi);
2765 	if (vsi->type == ICE_VSI_VF)
2766 		return;
2767 
2768 	vsi->irqs_ready = false;
2769 	ice_free_cpu_rx_rmap(vsi);
2770 
2771 	ice_for_each_q_vector(vsi, i) {
2772 		int irq_num;
2773 
2774 		irq_num = vsi->q_vectors[i]->irq.virq;
2775 
2776 		/* free only the irqs that were actually requested */
2777 		if (!vsi->q_vectors[i] ||
2778 		    !(vsi->q_vectors[i]->num_ring_tx ||
2779 		      vsi->q_vectors[i]->num_ring_rx))
2780 			continue;
2781 
2782 		/* clear the affinity notifier in the IRQ descriptor */
2783 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2784 			irq_set_affinity_notifier(irq_num, NULL);
2785 
2786 		/* clear the affinity_mask in the IRQ descriptor */
2787 		irq_set_affinity_hint(irq_num, NULL);
2788 		synchronize_irq(irq_num);
2789 		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2790 	}
2791 }
2792 
2793 /**
2794  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2795  * @vsi: the VSI having resources freed
2796  */
2797 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2798 {
2799 	int i;
2800 
2801 	if (!vsi->tx_rings)
2802 		return;
2803 
2804 	ice_for_each_txq(vsi, i)
2805 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2806 			ice_free_tx_ring(vsi->tx_rings[i]);
2807 }
2808 
2809 /**
2810  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2811  * @vsi: the VSI having resources freed
2812  */
2813 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2814 {
2815 	int i;
2816 
2817 	if (!vsi->rx_rings)
2818 		return;
2819 
2820 	ice_for_each_rxq(vsi, i)
2821 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2822 			ice_free_rx_ring(vsi->rx_rings[i]);
2823 }
2824 
2825 /**
2826  * ice_vsi_close - Shut down a VSI
2827  * @vsi: the VSI being shut down
2828  */
2829 void ice_vsi_close(struct ice_vsi *vsi)
2830 {
2831 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2832 		ice_down(vsi);
2833 
2834 	ice_vsi_free_irq(vsi);
2835 	ice_vsi_free_tx_rings(vsi);
2836 	ice_vsi_free_rx_rings(vsi);
2837 }
2838 
2839 /**
2840  * ice_ena_vsi - resume a VSI
2841  * @vsi: the VSI being resume
2842  * @locked: is the rtnl_lock already held
2843  */
2844 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2845 {
2846 	int err = 0;
2847 
2848 	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2849 		return 0;
2850 
2851 	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2852 
2853 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2854 		if (netif_running(vsi->netdev)) {
2855 			if (!locked)
2856 				rtnl_lock();
2857 
2858 			err = ice_open_internal(vsi->netdev);
2859 
2860 			if (!locked)
2861 				rtnl_unlock();
2862 		}
2863 	} else if (vsi->type == ICE_VSI_CTRL) {
2864 		err = ice_vsi_open_ctrl(vsi);
2865 	}
2866 
2867 	return err;
2868 }
2869 
2870 /**
2871  * ice_dis_vsi - pause a VSI
2872  * @vsi: the VSI being paused
2873  * @locked: is the rtnl_lock already held
2874  */
2875 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2876 {
2877 	if (test_bit(ICE_VSI_DOWN, vsi->state))
2878 		return;
2879 
2880 	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2881 
2882 	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2883 		if (netif_running(vsi->netdev)) {
2884 			if (!locked)
2885 				rtnl_lock();
2886 
2887 			ice_vsi_close(vsi);
2888 
2889 			if (!locked)
2890 				rtnl_unlock();
2891 		} else {
2892 			ice_vsi_close(vsi);
2893 		}
2894 	} else if (vsi->type == ICE_VSI_CTRL ||
2895 		   vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2896 		ice_vsi_close(vsi);
2897 	}
2898 }
2899 
2900 /**
2901  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2902  * @vsi: the VSI being un-configured
2903  */
2904 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2905 {
2906 	struct ice_pf *pf = vsi->back;
2907 	struct ice_hw *hw = &pf->hw;
2908 	u32 val;
2909 	int i;
2910 
2911 	/* disable interrupt causation from each queue */
2912 	if (vsi->tx_rings) {
2913 		ice_for_each_txq(vsi, i) {
2914 			if (vsi->tx_rings[i]) {
2915 				u16 reg;
2916 
2917 				reg = vsi->tx_rings[i]->reg_idx;
2918 				val = rd32(hw, QINT_TQCTL(reg));
2919 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2920 				wr32(hw, QINT_TQCTL(reg), val);
2921 			}
2922 		}
2923 	}
2924 
2925 	if (vsi->rx_rings) {
2926 		ice_for_each_rxq(vsi, i) {
2927 			if (vsi->rx_rings[i]) {
2928 				u16 reg;
2929 
2930 				reg = vsi->rx_rings[i]->reg_idx;
2931 				val = rd32(hw, QINT_RQCTL(reg));
2932 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2933 				wr32(hw, QINT_RQCTL(reg), val);
2934 			}
2935 		}
2936 	}
2937 
2938 	/* disable each interrupt */
2939 	ice_for_each_q_vector(vsi, i) {
2940 		if (!vsi->q_vectors[i])
2941 			continue;
2942 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2943 	}
2944 
2945 	ice_flush(hw);
2946 
2947 	/* don't call synchronize_irq() for VF's from the host */
2948 	if (vsi->type == ICE_VSI_VF)
2949 		return;
2950 
2951 	ice_for_each_q_vector(vsi, i)
2952 		synchronize_irq(vsi->q_vectors[i]->irq.virq);
2953 }
2954 
2955 /**
2956  * ice_napi_del - Remove NAPI handler for the VSI
2957  * @vsi: VSI for which NAPI handler is to be removed
2958  */
2959 void ice_napi_del(struct ice_vsi *vsi)
2960 {
2961 	int v_idx;
2962 
2963 	if (!vsi->netdev)
2964 		return;
2965 
2966 	ice_for_each_q_vector(vsi, v_idx)
2967 		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2968 }
2969 
2970 /**
2971  * ice_vsi_release - Delete a VSI and free its resources
2972  * @vsi: the VSI being removed
2973  *
2974  * Returns 0 on success or < 0 on error
2975  */
2976 int ice_vsi_release(struct ice_vsi *vsi)
2977 {
2978 	struct ice_pf *pf;
2979 
2980 	if (!vsi->back)
2981 		return -ENODEV;
2982 	pf = vsi->back;
2983 
2984 	/* do not unregister while driver is in the reset recovery pending
2985 	 * state. Since reset/rebuild happens through PF service task workqueue,
2986 	 * it's not a good idea to unregister netdev that is associated to the
2987 	 * PF that is running the work queue items currently. This is done to
2988 	 * avoid check_flush_dependency() warning on this wq
2989 	 */
2990 	if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2991 	    (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2992 		unregister_netdev(vsi->netdev);
2993 		clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2994 	}
2995 
2996 	if (vsi->type == ICE_VSI_PF)
2997 		ice_devlink_destroy_pf_port(pf);
2998 
2999 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3000 		ice_rss_clean(vsi);
3001 
3002 	ice_vsi_close(vsi);
3003 	ice_vsi_decfg(vsi);
3004 
3005 	if (vsi->netdev) {
3006 		if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
3007 			unregister_netdev(vsi->netdev);
3008 			clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3009 		}
3010 		if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
3011 			free_netdev(vsi->netdev);
3012 			vsi->netdev = NULL;
3013 			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3014 		}
3015 	}
3016 
3017 	/* retain SW VSI data structure since it is needed to unregister and
3018 	 * free VSI netdev when PF is not in reset recovery pending state,\
3019 	 * for ex: during rmmod.
3020 	 */
3021 	if (!ice_is_reset_in_progress(pf->state))
3022 		ice_vsi_delete(vsi);
3023 
3024 	return 0;
3025 }
3026 
3027 /**
3028  * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
3029  * @vsi: VSI connected with q_vectors
3030  * @coalesce: array of struct with stored coalesce
3031  *
3032  * Returns array size.
3033  */
3034 static int
3035 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3036 			     struct ice_coalesce_stored *coalesce)
3037 {
3038 	int i;
3039 
3040 	ice_for_each_q_vector(vsi, i) {
3041 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
3042 
3043 		coalesce[i].itr_tx = q_vector->tx.itr_settings;
3044 		coalesce[i].itr_rx = q_vector->rx.itr_settings;
3045 		coalesce[i].intrl = q_vector->intrl;
3046 
3047 		if (i < vsi->num_txq)
3048 			coalesce[i].tx_valid = true;
3049 		if (i < vsi->num_rxq)
3050 			coalesce[i].rx_valid = true;
3051 	}
3052 
3053 	return vsi->num_q_vectors;
3054 }
3055 
3056 /**
3057  * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3058  * @vsi: VSI connected with q_vectors
3059  * @coalesce: pointer to array of struct with stored coalesce
3060  * @size: size of coalesce array
3061  *
3062  * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3063  * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3064  * to default value.
3065  */
3066 static void
3067 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3068 			     struct ice_coalesce_stored *coalesce, int size)
3069 {
3070 	struct ice_ring_container *rc;
3071 	int i;
3072 
3073 	if ((size && !coalesce) || !vsi)
3074 		return;
3075 
3076 	/* There are a couple of cases that have to be handled here:
3077 	 *   1. The case where the number of queue vectors stays the same, but
3078 	 *      the number of Tx or Rx rings changes (the first for loop)
3079 	 *   2. The case where the number of queue vectors increased (the
3080 	 *      second for loop)
3081 	 */
3082 	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3083 		/* There are 2 cases to handle here and they are the same for
3084 		 * both Tx and Rx:
3085 		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
3086 		 *   and the loop variable is less than the number of rings
3087 		 *   allocated, then write the previous values
3088 		 *
3089 		 *   if the entry was not valid previously, but the number of
3090 		 *   rings is less than are allocated (this means the number of
3091 		 *   rings increased from previously), then write out the
3092 		 *   values in the first element
3093 		 *
3094 		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
3095 		 *   as there is no harm because the dynamic algorithm
3096 		 *   will just overwrite.
3097 		 */
3098 		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3099 			rc = &vsi->q_vectors[i]->rx;
3100 			rc->itr_settings = coalesce[i].itr_rx;
3101 			ice_write_itr(rc, rc->itr_setting);
3102 		} else if (i < vsi->alloc_rxq) {
3103 			rc = &vsi->q_vectors[i]->rx;
3104 			rc->itr_settings = coalesce[0].itr_rx;
3105 			ice_write_itr(rc, rc->itr_setting);
3106 		}
3107 
3108 		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3109 			rc = &vsi->q_vectors[i]->tx;
3110 			rc->itr_settings = coalesce[i].itr_tx;
3111 			ice_write_itr(rc, rc->itr_setting);
3112 		} else if (i < vsi->alloc_txq) {
3113 			rc = &vsi->q_vectors[i]->tx;
3114 			rc->itr_settings = coalesce[0].itr_tx;
3115 			ice_write_itr(rc, rc->itr_setting);
3116 		}
3117 
3118 		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3119 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
3120 	}
3121 
3122 	/* the number of queue vectors increased so write whatever is in
3123 	 * the first element
3124 	 */
3125 	for (; i < vsi->num_q_vectors; i++) {
3126 		/* transmit */
3127 		rc = &vsi->q_vectors[i]->tx;
3128 		rc->itr_settings = coalesce[0].itr_tx;
3129 		ice_write_itr(rc, rc->itr_setting);
3130 
3131 		/* receive */
3132 		rc = &vsi->q_vectors[i]->rx;
3133 		rc->itr_settings = coalesce[0].itr_rx;
3134 		ice_write_itr(rc, rc->itr_setting);
3135 
3136 		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3137 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
3138 	}
3139 }
3140 
3141 /**
3142  * ice_vsi_realloc_stat_arrays - Frees unused stat structures
3143  * @vsi: VSI pointer
3144  * @prev_txq: Number of Tx rings before ring reallocation
3145  * @prev_rxq: Number of Rx rings before ring reallocation
3146  */
3147 static void
3148 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi, int prev_txq, int prev_rxq)
3149 {
3150 	struct ice_vsi_stats *vsi_stat;
3151 	struct ice_pf *pf = vsi->back;
3152 	int i;
3153 
3154 	if (!prev_txq || !prev_rxq)
3155 		return;
3156 	if (vsi->type == ICE_VSI_CHNL)
3157 		return;
3158 
3159 	vsi_stat = pf->vsi_stats[vsi->idx];
3160 
3161 	if (vsi->num_txq < prev_txq) {
3162 		for (i = vsi->num_txq; i < prev_txq; i++) {
3163 			if (vsi_stat->tx_ring_stats[i]) {
3164 				kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
3165 				WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
3166 			}
3167 		}
3168 	}
3169 
3170 	if (vsi->num_rxq < prev_rxq) {
3171 		for (i = vsi->num_rxq; i < prev_rxq; i++) {
3172 			if (vsi_stat->rx_ring_stats[i]) {
3173 				kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
3174 				WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3175 			}
3176 		}
3177 	}
3178 }
3179 
3180 /**
3181  * ice_vsi_rebuild - Rebuild VSI after reset
3182  * @vsi: VSI to be rebuild
3183  * @vsi_flags: flags used for VSI rebuild flow
3184  *
3185  * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3186  * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3187  *
3188  * Returns 0 on success and negative value on failure
3189  */
3190 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3191 {
3192 	struct ice_vsi_cfg_params params = {};
3193 	struct ice_coalesce_stored *coalesce;
3194 	int ret, prev_txq, prev_rxq;
3195 	int prev_num_q_vectors = 0;
3196 	struct ice_pf *pf;
3197 
3198 	if (!vsi)
3199 		return -EINVAL;
3200 
3201 	params = ice_vsi_to_params(vsi);
3202 	params.flags = vsi_flags;
3203 
3204 	pf = vsi->back;
3205 	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3206 		return -EINVAL;
3207 
3208 	coalesce = kcalloc(vsi->num_q_vectors,
3209 			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3210 	if (!coalesce)
3211 		return -ENOMEM;
3212 
3213 	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3214 
3215 	prev_txq = vsi->num_txq;
3216 	prev_rxq = vsi->num_rxq;
3217 
3218 	ice_vsi_decfg(vsi);
3219 	ret = ice_vsi_cfg_def(vsi, &params);
3220 	if (ret)
3221 		goto err_vsi_cfg;
3222 
3223 	ret = ice_vsi_cfg_tc_lan(pf, vsi);
3224 	if (ret) {
3225 		if (vsi_flags & ICE_VSI_FLAG_INIT) {
3226 			ret = -EIO;
3227 			goto err_vsi_cfg_tc_lan;
3228 		}
3229 
3230 		kfree(coalesce);
3231 		return ice_schedule_reset(pf, ICE_RESET_PFR);
3232 	}
3233 
3234 	ice_vsi_realloc_stat_arrays(vsi, prev_txq, prev_rxq);
3235 
3236 	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3237 	kfree(coalesce);
3238 
3239 	return 0;
3240 
3241 err_vsi_cfg_tc_lan:
3242 	ice_vsi_decfg(vsi);
3243 err_vsi_cfg:
3244 	kfree(coalesce);
3245 	return ret;
3246 }
3247 
3248 /**
3249  * ice_is_reset_in_progress - check for a reset in progress
3250  * @state: PF state field
3251  */
3252 bool ice_is_reset_in_progress(unsigned long *state)
3253 {
3254 	return test_bit(ICE_RESET_OICR_RECV, state) ||
3255 	       test_bit(ICE_PFR_REQ, state) ||
3256 	       test_bit(ICE_CORER_REQ, state) ||
3257 	       test_bit(ICE_GLOBR_REQ, state);
3258 }
3259 
3260 /**
3261  * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3262  * @pf: pointer to the PF structure
3263  * @timeout: length of time to wait, in jiffies
3264  *
3265  * Wait (sleep) for a short time until the driver finishes cleaning up from
3266  * a device reset. The caller must be able to sleep. Use this to delay
3267  * operations that could fail while the driver is cleaning up after a device
3268  * reset.
3269  *
3270  * Returns 0 on success, -EBUSY if the reset is not finished within the
3271  * timeout, and -ERESTARTSYS if the thread was interrupted.
3272  */
3273 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3274 {
3275 	long ret;
3276 
3277 	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3278 					       !ice_is_reset_in_progress(pf->state),
3279 					       timeout);
3280 	if (ret < 0)
3281 		return ret;
3282 	else if (!ret)
3283 		return -EBUSY;
3284 	else
3285 		return 0;
3286 }
3287 
3288 /**
3289  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3290  * @vsi: VSI being configured
3291  * @ctx: the context buffer returned from AQ VSI update command
3292  */
3293 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3294 {
3295 	vsi->info.mapping_flags = ctx->info.mapping_flags;
3296 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3297 	       sizeof(vsi->info.q_mapping));
3298 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3299 	       sizeof(vsi->info.tc_mapping));
3300 }
3301 
3302 /**
3303  * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3304  * @vsi: the VSI being configured
3305  * @ena_tc: TC map to be enabled
3306  */
3307 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3308 {
3309 	struct net_device *netdev = vsi->netdev;
3310 	struct ice_pf *pf = vsi->back;
3311 	int numtc = vsi->tc_cfg.numtc;
3312 	struct ice_dcbx_cfg *dcbcfg;
3313 	u8 netdev_tc;
3314 	int i;
3315 
3316 	if (!netdev)
3317 		return;
3318 
3319 	/* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3320 	if (vsi->type == ICE_VSI_CHNL)
3321 		return;
3322 
3323 	if (!ena_tc) {
3324 		netdev_reset_tc(netdev);
3325 		return;
3326 	}
3327 
3328 	if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3329 		numtc = vsi->all_numtc;
3330 
3331 	if (netdev_set_num_tc(netdev, numtc))
3332 		return;
3333 
3334 	dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3335 
3336 	ice_for_each_traffic_class(i)
3337 		if (vsi->tc_cfg.ena_tc & BIT(i))
3338 			netdev_set_tc_queue(netdev,
3339 					    vsi->tc_cfg.tc_info[i].netdev_tc,
3340 					    vsi->tc_cfg.tc_info[i].qcount_tx,
3341 					    vsi->tc_cfg.tc_info[i].qoffset);
3342 	/* setup TC queue map for CHNL TCs */
3343 	ice_for_each_chnl_tc(i) {
3344 		if (!(vsi->all_enatc & BIT(i)))
3345 			break;
3346 		if (!vsi->mqprio_qopt.qopt.count[i])
3347 			break;
3348 		netdev_set_tc_queue(netdev, i,
3349 				    vsi->mqprio_qopt.qopt.count[i],
3350 				    vsi->mqprio_qopt.qopt.offset[i]);
3351 	}
3352 
3353 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3354 		return;
3355 
3356 	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3357 		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3358 
3359 		/* Get the mapped netdev TC# for the UP */
3360 		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3361 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3362 	}
3363 }
3364 
3365 /**
3366  * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3367  * @vsi: the VSI being configured,
3368  * @ctxt: VSI context structure
3369  * @ena_tc: number of traffic classes to enable
3370  *
3371  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3372  */
3373 static int
3374 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3375 			   u8 ena_tc)
3376 {
3377 	u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3378 	u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3379 	int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3380 	u16 new_txq, new_rxq;
3381 	u8 netdev_tc = 0;
3382 	int i;
3383 
3384 	vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3385 
3386 	pow = order_base_2(tc0_qcount);
3387 	qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3388 		ICE_AQ_VSI_TC_Q_OFFSET_M) |
3389 		((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3390 
3391 	ice_for_each_traffic_class(i) {
3392 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3393 			/* TC is not enabled */
3394 			vsi->tc_cfg.tc_info[i].qoffset = 0;
3395 			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3396 			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3397 			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3398 			ctxt->info.tc_mapping[i] = 0;
3399 			continue;
3400 		}
3401 
3402 		offset = vsi->mqprio_qopt.qopt.offset[i];
3403 		qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3404 		qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3405 		vsi->tc_cfg.tc_info[i].qoffset = offset;
3406 		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3407 		vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3408 		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3409 	}
3410 
3411 	if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3412 		ice_for_each_chnl_tc(i) {
3413 			if (!(vsi->all_enatc & BIT(i)))
3414 				continue;
3415 			offset = vsi->mqprio_qopt.qopt.offset[i];
3416 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3417 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3418 		}
3419 	}
3420 
3421 	new_txq = offset + qcount_tx;
3422 	if (new_txq > vsi->alloc_txq) {
3423 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3424 			new_txq, vsi->alloc_txq);
3425 		return -EINVAL;
3426 	}
3427 
3428 	new_rxq = offset + qcount_rx;
3429 	if (new_rxq > vsi->alloc_rxq) {
3430 		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3431 			new_rxq, vsi->alloc_rxq);
3432 		return -EINVAL;
3433 	}
3434 
3435 	/* Set actual Tx/Rx queue pairs */
3436 	vsi->num_txq = new_txq;
3437 	vsi->num_rxq = new_rxq;
3438 
3439 	/* Setup queue TC[0].qmap for given VSI context */
3440 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3441 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3442 	ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3443 
3444 	/* Find queue count available for channel VSIs and starting offset
3445 	 * for channel VSIs
3446 	 */
3447 	if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3448 		vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3449 		vsi->next_base_q = tc0_qcount;
3450 	}
3451 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n",  vsi->num_txq);
3452 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n",  vsi->num_rxq);
3453 	dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3454 		vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3455 
3456 	return 0;
3457 }
3458 
3459 /**
3460  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3461  * @vsi: VSI to be configured
3462  * @ena_tc: TC bitmap
3463  *
3464  * VSI queues expected to be quiesced before calling this function
3465  */
3466 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3467 {
3468 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3469 	struct ice_pf *pf = vsi->back;
3470 	struct ice_tc_cfg old_tc_cfg;
3471 	struct ice_vsi_ctx *ctx;
3472 	struct device *dev;
3473 	int i, ret = 0;
3474 	u8 num_tc = 0;
3475 
3476 	dev = ice_pf_to_dev(pf);
3477 	if (vsi->tc_cfg.ena_tc == ena_tc &&
3478 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3479 		return 0;
3480 
3481 	ice_for_each_traffic_class(i) {
3482 		/* build bitmap of enabled TCs */
3483 		if (ena_tc & BIT(i))
3484 			num_tc++;
3485 		/* populate max_txqs per TC */
3486 		max_txqs[i] = vsi->alloc_txq;
3487 		/* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3488 		 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3489 		 */
3490 		if (vsi->type == ICE_VSI_CHNL &&
3491 		    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3492 			max_txqs[i] = vsi->num_txq;
3493 	}
3494 
3495 	memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3496 	vsi->tc_cfg.ena_tc = ena_tc;
3497 	vsi->tc_cfg.numtc = num_tc;
3498 
3499 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3500 	if (!ctx)
3501 		return -ENOMEM;
3502 
3503 	ctx->vf_num = 0;
3504 	ctx->info = vsi->info;
3505 
3506 	if (vsi->type == ICE_VSI_PF &&
3507 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3508 		ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3509 	else
3510 		ret = ice_vsi_setup_q_map(vsi, ctx);
3511 
3512 	if (ret) {
3513 		memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3514 		goto out;
3515 	}
3516 
3517 	/* must to indicate which section of VSI context are being modified */
3518 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3519 	ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3520 	if (ret) {
3521 		dev_info(dev, "Failed VSI Update\n");
3522 		goto out;
3523 	}
3524 
3525 	if (vsi->type == ICE_VSI_PF &&
3526 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3527 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3528 	else
3529 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3530 				      vsi->tc_cfg.ena_tc, max_txqs);
3531 
3532 	if (ret) {
3533 		dev_err(dev, "VSI %d failed TC config, error %d\n",
3534 			vsi->vsi_num, ret);
3535 		goto out;
3536 	}
3537 	ice_vsi_update_q_map(vsi, ctx);
3538 	vsi->info.valid_sections = 0;
3539 
3540 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3541 out:
3542 	kfree(ctx);
3543 	return ret;
3544 }
3545 
3546 /**
3547  * ice_update_ring_stats - Update ring statistics
3548  * @stats: stats to be updated
3549  * @pkts: number of processed packets
3550  * @bytes: number of processed bytes
3551  *
3552  * This function assumes that caller has acquired a u64_stats_sync lock.
3553  */
3554 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3555 {
3556 	stats->bytes += bytes;
3557 	stats->pkts += pkts;
3558 }
3559 
3560 /**
3561  * ice_update_tx_ring_stats - Update Tx ring specific counters
3562  * @tx_ring: ring to update
3563  * @pkts: number of processed packets
3564  * @bytes: number of processed bytes
3565  */
3566 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3567 {
3568 	u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3569 	ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3570 	u64_stats_update_end(&tx_ring->ring_stats->syncp);
3571 }
3572 
3573 /**
3574  * ice_update_rx_ring_stats - Update Rx ring specific counters
3575  * @rx_ring: ring to update
3576  * @pkts: number of processed packets
3577  * @bytes: number of processed bytes
3578  */
3579 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3580 {
3581 	u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3582 	ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3583 	u64_stats_update_end(&rx_ring->ring_stats->syncp);
3584 }
3585 
3586 /**
3587  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3588  * @pi: port info of the switch with default VSI
3589  *
3590  * Return true if the there is a single VSI in default forwarding VSI list
3591  */
3592 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3593 {
3594 	bool exists = false;
3595 
3596 	ice_check_if_dflt_vsi(pi, 0, &exists);
3597 	return exists;
3598 }
3599 
3600 /**
3601  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3602  * @vsi: VSI to compare against default forwarding VSI
3603  *
3604  * If this VSI passed in is the default forwarding VSI then return true, else
3605  * return false
3606  */
3607 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3608 {
3609 	return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3610 }
3611 
3612 /**
3613  * ice_set_dflt_vsi - set the default forwarding VSI
3614  * @vsi: VSI getting set as the default forwarding VSI on the switch
3615  *
3616  * If the VSI passed in is already the default VSI and it's enabled just return
3617  * success.
3618  *
3619  * Otherwise try to set the VSI passed in as the switch's default VSI and
3620  * return the result.
3621  */
3622 int ice_set_dflt_vsi(struct ice_vsi *vsi)
3623 {
3624 	struct device *dev;
3625 	int status;
3626 
3627 	if (!vsi)
3628 		return -EINVAL;
3629 
3630 	dev = ice_pf_to_dev(vsi->back);
3631 
3632 	/* the VSI passed in is already the default VSI */
3633 	if (ice_is_vsi_dflt_vsi(vsi)) {
3634 		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3635 			vsi->vsi_num);
3636 		return 0;
3637 	}
3638 
3639 	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3640 	if (status) {
3641 		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3642 			vsi->vsi_num, status);
3643 		return status;
3644 	}
3645 
3646 	return 0;
3647 }
3648 
3649 /**
3650  * ice_clear_dflt_vsi - clear the default forwarding VSI
3651  * @vsi: VSI to remove from filter list
3652  *
3653  * If the switch has no default VSI or it's not enabled then return error.
3654  *
3655  * Otherwise try to clear the default VSI and return the result.
3656  */
3657 int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3658 {
3659 	struct device *dev;
3660 	int status;
3661 
3662 	if (!vsi)
3663 		return -EINVAL;
3664 
3665 	dev = ice_pf_to_dev(vsi->back);
3666 
3667 	/* there is no default VSI configured */
3668 	if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3669 		return -ENODEV;
3670 
3671 	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3672 				  ICE_FLTR_RX);
3673 	if (status) {
3674 		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3675 			vsi->vsi_num, status);
3676 		return -EIO;
3677 	}
3678 
3679 	return 0;
3680 }
3681 
3682 /**
3683  * ice_get_link_speed_mbps - get link speed in Mbps
3684  * @vsi: the VSI whose link speed is being queried
3685  *
3686  * Return current VSI link speed and 0 if the speed is unknown.
3687  */
3688 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3689 {
3690 	unsigned int link_speed;
3691 
3692 	link_speed = vsi->port_info->phy.link_info.link_speed;
3693 
3694 	return (int)ice_get_link_speed(fls(link_speed) - 1);
3695 }
3696 
3697 /**
3698  * ice_get_link_speed_kbps - get link speed in Kbps
3699  * @vsi: the VSI whose link speed is being queried
3700  *
3701  * Return current VSI link speed and 0 if the speed is unknown.
3702  */
3703 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3704 {
3705 	int speed_mbps;
3706 
3707 	speed_mbps = ice_get_link_speed_mbps(vsi);
3708 
3709 	return speed_mbps * 1000;
3710 }
3711 
3712 /**
3713  * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3714  * @vsi: VSI to be configured
3715  * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3716  *
3717  * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3718  * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3719  * on TC 0.
3720  */
3721 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3722 {
3723 	struct ice_pf *pf = vsi->back;
3724 	struct device *dev;
3725 	int status;
3726 	int speed;
3727 
3728 	dev = ice_pf_to_dev(pf);
3729 	if (!vsi->port_info) {
3730 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3731 			vsi->idx, vsi->type);
3732 		return -EINVAL;
3733 	}
3734 
3735 	speed = ice_get_link_speed_kbps(vsi);
3736 	if (min_tx_rate > (u64)speed) {
3737 		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3738 			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3739 			speed);
3740 		return -EINVAL;
3741 	}
3742 
3743 	/* Configure min BW for VSI limit */
3744 	if (min_tx_rate) {
3745 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3746 						   ICE_MIN_BW, min_tx_rate);
3747 		if (status) {
3748 			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3749 				min_tx_rate, ice_vsi_type_str(vsi->type),
3750 				vsi->idx);
3751 			return status;
3752 		}
3753 
3754 		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3755 			min_tx_rate, ice_vsi_type_str(vsi->type));
3756 	} else {
3757 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3758 							vsi->idx, 0,
3759 							ICE_MIN_BW);
3760 		if (status) {
3761 			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3762 				ice_vsi_type_str(vsi->type), vsi->idx);
3763 			return status;
3764 		}
3765 
3766 		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3767 			ice_vsi_type_str(vsi->type), vsi->idx);
3768 	}
3769 
3770 	return 0;
3771 }
3772 
3773 /**
3774  * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3775  * @vsi: VSI to be configured
3776  * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3777  *
3778  * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3779  * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3780  * on TC 0.
3781  */
3782 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3783 {
3784 	struct ice_pf *pf = vsi->back;
3785 	struct device *dev;
3786 	int status;
3787 	int speed;
3788 
3789 	dev = ice_pf_to_dev(pf);
3790 	if (!vsi->port_info) {
3791 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3792 			vsi->idx, vsi->type);
3793 		return -EINVAL;
3794 	}
3795 
3796 	speed = ice_get_link_speed_kbps(vsi);
3797 	if (max_tx_rate > (u64)speed) {
3798 		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3799 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3800 			speed);
3801 		return -EINVAL;
3802 	}
3803 
3804 	/* Configure max BW for VSI limit */
3805 	if (max_tx_rate) {
3806 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3807 						   ICE_MAX_BW, max_tx_rate);
3808 		if (status) {
3809 			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3810 				max_tx_rate, ice_vsi_type_str(vsi->type),
3811 				vsi->idx);
3812 			return status;
3813 		}
3814 
3815 		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3816 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3817 	} else {
3818 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3819 							vsi->idx, 0,
3820 							ICE_MAX_BW);
3821 		if (status) {
3822 			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3823 				ice_vsi_type_str(vsi->type), vsi->idx);
3824 			return status;
3825 		}
3826 
3827 		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3828 			ice_vsi_type_str(vsi->type), vsi->idx);
3829 	}
3830 
3831 	return 0;
3832 }
3833 
3834 /**
3835  * ice_set_link - turn on/off physical link
3836  * @vsi: VSI to modify physical link on
3837  * @ena: turn on/off physical link
3838  */
3839 int ice_set_link(struct ice_vsi *vsi, bool ena)
3840 {
3841 	struct device *dev = ice_pf_to_dev(vsi->back);
3842 	struct ice_port_info *pi = vsi->port_info;
3843 	struct ice_hw *hw = pi->hw;
3844 	int status;
3845 
3846 	if (vsi->type != ICE_VSI_PF)
3847 		return -EINVAL;
3848 
3849 	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3850 
3851 	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3852 	 * this is not a fatal error, so print a warning message and return
3853 	 * a success code. Return an error if FW returns an error code other
3854 	 * than ICE_AQ_RC_EMODE
3855 	 */
3856 	if (status == -EIO) {
3857 		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3858 			dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3859 				(ena ? "ON" : "OFF"), status,
3860 				ice_aq_str(hw->adminq.sq_last_status));
3861 	} else if (status) {
3862 		dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3863 			(ena ? "ON" : "OFF"), status,
3864 			ice_aq_str(hw->adminq.sq_last_status));
3865 		return status;
3866 	}
3867 
3868 	return 0;
3869 }
3870 
3871 /**
3872  * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3873  * @vsi: VSI used to add VLAN filters
3874  *
3875  * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3876  * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3877  * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3878  * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3879  *
3880  * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3881  * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3882  * traffic in SVM, since the VLAN TPID isn't part of filtering.
3883  *
3884  * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3885  * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3886  * part of filtering.
3887  */
3888 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3889 {
3890 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3891 	struct ice_vlan vlan;
3892 	int err;
3893 
3894 	vlan = ICE_VLAN(0, 0, 0);
3895 	err = vlan_ops->add_vlan(vsi, &vlan);
3896 	if (err && err != -EEXIST)
3897 		return err;
3898 
3899 	/* in SVM both VLAN 0 filters are identical */
3900 	if (!ice_is_dvm_ena(&vsi->back->hw))
3901 		return 0;
3902 
3903 	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3904 	err = vlan_ops->add_vlan(vsi, &vlan);
3905 	if (err && err != -EEXIST)
3906 		return err;
3907 
3908 	return 0;
3909 }
3910 
3911 /**
3912  * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3913  * @vsi: VSI used to add VLAN filters
3914  *
3915  * Delete the VLAN 0 filters in the same manner that they were added in
3916  * ice_vsi_add_vlan_zero.
3917  */
3918 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3919 {
3920 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3921 	struct ice_vlan vlan;
3922 	int err;
3923 
3924 	vlan = ICE_VLAN(0, 0, 0);
3925 	err = vlan_ops->del_vlan(vsi, &vlan);
3926 	if (err && err != -EEXIST)
3927 		return err;
3928 
3929 	/* in SVM both VLAN 0 filters are identical */
3930 	if (!ice_is_dvm_ena(&vsi->back->hw))
3931 		return 0;
3932 
3933 	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3934 	err = vlan_ops->del_vlan(vsi, &vlan);
3935 	if (err && err != -EEXIST)
3936 		return err;
3937 
3938 	/* when deleting the last VLAN filter, make sure to disable the VLAN
3939 	 * promisc mode so the filter isn't left by accident
3940 	 */
3941 	return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3942 				    ICE_MCAST_VLAN_PROMISC_BITS, 0);
3943 }
3944 
3945 /**
3946  * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3947  * @vsi: VSI used to get the VLAN mode
3948  *
3949  * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3950  * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3951  */
3952 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3953 {
3954 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS	2
3955 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS	1
3956 	/* no VLAN 0 filter is created when a port VLAN is active */
3957 	if (vsi->type == ICE_VSI_VF) {
3958 		if (WARN_ON(!vsi->vf))
3959 			return 0;
3960 
3961 		if (ice_vf_is_port_vlan_ena(vsi->vf))
3962 			return 0;
3963 	}
3964 
3965 	if (ice_is_dvm_ena(&vsi->back->hw))
3966 		return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3967 	else
3968 		return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3969 }
3970 
3971 /**
3972  * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3973  * @vsi: VSI used to determine if any non-zero VLANs have been added
3974  */
3975 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3976 {
3977 	return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3978 }
3979 
3980 /**
3981  * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3982  * @vsi: VSI used to get the number of non-zero VLANs added
3983  */
3984 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3985 {
3986 	return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3987 }
3988 
3989 /**
3990  * ice_is_feature_supported
3991  * @pf: pointer to the struct ice_pf instance
3992  * @f: feature enum to be checked
3993  *
3994  * returns true if feature is supported, false otherwise
3995  */
3996 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3997 {
3998 	if (f < 0 || f >= ICE_F_MAX)
3999 		return false;
4000 
4001 	return test_bit(f, pf->features);
4002 }
4003 
4004 /**
4005  * ice_set_feature_support
4006  * @pf: pointer to the struct ice_pf instance
4007  * @f: feature enum to set
4008  */
4009 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4010 {
4011 	if (f < 0 || f >= ICE_F_MAX)
4012 		return;
4013 
4014 	set_bit(f, pf->features);
4015 }
4016 
4017 /**
4018  * ice_clear_feature_support
4019  * @pf: pointer to the struct ice_pf instance
4020  * @f: feature enum to clear
4021  */
4022 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4023 {
4024 	if (f < 0 || f >= ICE_F_MAX)
4025 		return;
4026 
4027 	clear_bit(f, pf->features);
4028 }
4029 
4030 /**
4031  * ice_init_feature_support
4032  * @pf: pointer to the struct ice_pf instance
4033  *
4034  * called during init to setup supported feature
4035  */
4036 void ice_init_feature_support(struct ice_pf *pf)
4037 {
4038 	switch (pf->hw.device_id) {
4039 	case ICE_DEV_ID_E810C_BACKPLANE:
4040 	case ICE_DEV_ID_E810C_QSFP:
4041 	case ICE_DEV_ID_E810C_SFP:
4042 		ice_set_feature_support(pf, ICE_F_DSCP);
4043 		ice_set_feature_support(pf, ICE_F_PTP_EXTTS);
4044 		if (ice_is_e810t(&pf->hw)) {
4045 			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4046 			if (ice_gnss_is_gps_present(&pf->hw))
4047 				ice_set_feature_support(pf, ICE_F_GNSS);
4048 		}
4049 		break;
4050 	default:
4051 		break;
4052 	}
4053 }
4054 
4055 /**
4056  * ice_vsi_update_security - update security block in VSI
4057  * @vsi: pointer to VSI structure
4058  * @fill: function pointer to fill ctx
4059  */
4060 int
4061 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4062 {
4063 	struct ice_vsi_ctx ctx = { 0 };
4064 
4065 	ctx.info = vsi->info;
4066 	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4067 	fill(&ctx);
4068 
4069 	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4070 		return -ENODEV;
4071 
4072 	vsi->info = ctx.info;
4073 	return 0;
4074 }
4075 
4076 /**
4077  * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4078  * @ctx: pointer to VSI ctx structure
4079  */
4080 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4081 {
4082 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4083 			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4084 				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4085 }
4086 
4087 /**
4088  * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4089  * @ctx: pointer to VSI ctx structure
4090  */
4091 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4092 {
4093 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4094 			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4095 				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4096 }
4097 
4098 /**
4099  * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4100  * @ctx: pointer to VSI ctx structure
4101  */
4102 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4103 {
4104 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4105 }
4106 
4107 /**
4108  * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4109  * @ctx: pointer to VSI ctx structure
4110  */
4111 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4112 {
4113 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4114 }
4115