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