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