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