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