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