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 
1689 /**
1690  * ice_pf_state_is_nominal - checks the PF for nominal state
1691  * @pf: pointer to PF to check
1692  *
1693  * Check the PF's state for a collection of bits that would indicate
1694  * the PF is in a state that would inhibit normal operation for
1695  * driver functionality.
1696  *
1697  * Returns true if PF is in a nominal state, false otherwise
1698  */
1699 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1700 {
1701 	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1702 
1703 	if (!pf)
1704 		return false;
1705 
1706 	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1707 	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1708 		return false;
1709 
1710 	return true;
1711 }
1712 
1713 /**
1714  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1715  * @vsi: the VSI to be updated
1716  */
1717 void ice_update_eth_stats(struct ice_vsi *vsi)
1718 {
1719 	struct ice_eth_stats *prev_es, *cur_es;
1720 	struct ice_hw *hw = &vsi->back->hw;
1721 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1722 
1723 	prev_es = &vsi->eth_stats_prev;
1724 	cur_es = &vsi->eth_stats;
1725 
1726 	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1727 			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1728 
1729 	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1730 			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1731 
1732 	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1733 			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1734 
1735 	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1736 			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1737 
1738 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1739 			  &prev_es->rx_discards, &cur_es->rx_discards);
1740 
1741 	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1742 			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1743 
1744 	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1745 			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1746 
1747 	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1748 			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1749 
1750 	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1751 			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1752 
1753 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1754 			  &prev_es->tx_errors, &cur_es->tx_errors);
1755 
1756 	vsi->stat_offsets_loaded = true;
1757 }
1758 
1759 /**
1760  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1761  * @vsi: the VSI being configured
1762  * @vid: VLAN ID to be added
1763  * @action: filter action to be performed on match
1764  */
1765 int
1766 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1767 {
1768 	struct ice_pf *pf = vsi->back;
1769 	struct device *dev;
1770 	int err = 0;
1771 
1772 	dev = ice_pf_to_dev(pf);
1773 
1774 	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1775 		vsi->num_vlan++;
1776 	} else {
1777 		err = -ENODEV;
1778 		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1779 			vsi->vsi_num);
1780 	}
1781 
1782 	return err;
1783 }
1784 
1785 /**
1786  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1787  * @vsi: the VSI being configured
1788  * @vid: VLAN ID to be removed
1789  *
1790  * Returns 0 on success and negative on failure
1791  */
1792 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1793 {
1794 	struct ice_pf *pf = vsi->back;
1795 	struct device *dev;
1796 	int err;
1797 
1798 	dev = ice_pf_to_dev(pf);
1799 
1800 	err = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1801 	if (!err) {
1802 		vsi->num_vlan--;
1803 	} else if (err == -ENOENT) {
1804 		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, error: %d\n",
1805 			vid, vsi->vsi_num, err);
1806 		err = 0;
1807 	} else {
1808 		dev_err(dev, "Error removing VLAN %d on vsi %i error: %d\n",
1809 			vid, vsi->vsi_num, err);
1810 	}
1811 
1812 	return err;
1813 }
1814 
1815 /**
1816  * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1817  * @vsi: VSI
1818  */
1819 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1820 {
1821 	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1822 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1823 		vsi->rx_buf_len = ICE_RXBUF_2048;
1824 #if (PAGE_SIZE < 8192)
1825 	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1826 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1827 		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1828 		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1829 #endif
1830 	} else {
1831 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1832 #if (PAGE_SIZE < 8192)
1833 		vsi->rx_buf_len = ICE_RXBUF_3072;
1834 #else
1835 		vsi->rx_buf_len = ICE_RXBUF_2048;
1836 #endif
1837 	}
1838 }
1839 
1840 /**
1841  * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1842  * @hw: HW pointer
1843  * @pf_q: index of the Rx queue in the PF's queue space
1844  * @rxdid: flexible descriptor RXDID
1845  * @prio: priority for the RXDID for this queue
1846  * @ena_ts: true to enable timestamp and false to disable timestamp
1847  */
1848 void
1849 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1850 			bool ena_ts)
1851 {
1852 	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1853 
1854 	/* clear any previous values */
1855 	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1856 		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1857 		    QRXFLXP_CNTXT_TS_M);
1858 
1859 	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1860 		QRXFLXP_CNTXT_RXDID_IDX_M;
1861 
1862 	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1863 		QRXFLXP_CNTXT_RXDID_PRIO_M;
1864 
1865 	if (ena_ts)
1866 		/* Enable TimeSync on this queue */
1867 		regval |= QRXFLXP_CNTXT_TS_M;
1868 
1869 	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1870 }
1871 
1872 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1873 {
1874 	if (q_idx >= vsi->num_rxq)
1875 		return -EINVAL;
1876 
1877 	return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1878 }
1879 
1880 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1881 {
1882 	struct ice_aqc_add_tx_qgrp *qg_buf;
1883 	int err;
1884 
1885 	if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1886 		return -EINVAL;
1887 
1888 	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1889 	if (!qg_buf)
1890 		return -ENOMEM;
1891 
1892 	qg_buf->num_txqs = 1;
1893 
1894 	err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1895 	kfree(qg_buf);
1896 	return err;
1897 }
1898 
1899 /**
1900  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1901  * @vsi: the VSI being configured
1902  *
1903  * Return 0 on success and a negative value on error
1904  * Configure the Rx VSI for operation.
1905  */
1906 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1907 {
1908 	u16 i;
1909 
1910 	if (vsi->type == ICE_VSI_VF)
1911 		goto setup_rings;
1912 
1913 	ice_vsi_cfg_frame_size(vsi);
1914 setup_rings:
1915 	/* set up individual rings */
1916 	ice_for_each_rxq(vsi, i) {
1917 		int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1918 
1919 		if (err)
1920 			return err;
1921 	}
1922 
1923 	return 0;
1924 }
1925 
1926 /**
1927  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1928  * @vsi: the VSI being configured
1929  * @rings: Tx ring array to be configured
1930  * @count: number of Tx ring array elements
1931  *
1932  * Return 0 on success and a negative value on error
1933  * Configure the Tx VSI for operation.
1934  */
1935 static int
1936 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1937 {
1938 	struct ice_aqc_add_tx_qgrp *qg_buf;
1939 	u16 q_idx = 0;
1940 	int err = 0;
1941 
1942 	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1943 	if (!qg_buf)
1944 		return -ENOMEM;
1945 
1946 	qg_buf->num_txqs = 1;
1947 
1948 	for (q_idx = 0; q_idx < count; q_idx++) {
1949 		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1950 		if (err)
1951 			goto err_cfg_txqs;
1952 	}
1953 
1954 err_cfg_txqs:
1955 	kfree(qg_buf);
1956 	return err;
1957 }
1958 
1959 /**
1960  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1961  * @vsi: the VSI being configured
1962  *
1963  * Return 0 on success and a negative value on error
1964  * Configure the Tx VSI for operation.
1965  */
1966 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1967 {
1968 	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1969 }
1970 
1971 /**
1972  * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1973  * @vsi: the VSI being configured
1974  *
1975  * Return 0 on success and a negative value on error
1976  * Configure the Tx queues dedicated for XDP in given VSI for operation.
1977  */
1978 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1979 {
1980 	int ret;
1981 	int i;
1982 
1983 	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1984 	if (ret)
1985 		return ret;
1986 
1987 	ice_for_each_xdp_txq(vsi, i)
1988 		vsi->xdp_rings[i]->xsk_pool = ice_tx_xsk_pool(vsi->xdp_rings[i]);
1989 
1990 	return ret;
1991 }
1992 
1993 /**
1994  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1995  * @intrl: interrupt rate limit in usecs
1996  * @gran: interrupt rate limit granularity in usecs
1997  *
1998  * This function converts a decimal interrupt rate limit in usecs to the format
1999  * expected by firmware.
2000  */
2001 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
2002 {
2003 	u32 val = intrl / gran;
2004 
2005 	if (val)
2006 		return val | GLINT_RATE_INTRL_ENA_M;
2007 	return 0;
2008 }
2009 
2010 /**
2011  * ice_write_intrl - write throttle rate limit to interrupt specific register
2012  * @q_vector: pointer to interrupt specific structure
2013  * @intrl: throttle rate limit in microseconds to write
2014  */
2015 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
2016 {
2017 	struct ice_hw *hw = &q_vector->vsi->back->hw;
2018 
2019 	wr32(hw, GLINT_RATE(q_vector->reg_idx),
2020 	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
2021 }
2022 
2023 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
2024 {
2025 	switch (rc->type) {
2026 	case ICE_RX_CONTAINER:
2027 		if (rc->rx_ring)
2028 			return rc->rx_ring->q_vector;
2029 		break;
2030 	case ICE_TX_CONTAINER:
2031 		if (rc->tx_ring)
2032 			return rc->tx_ring->q_vector;
2033 		break;
2034 	default:
2035 		break;
2036 	}
2037 
2038 	return NULL;
2039 }
2040 
2041 /**
2042  * __ice_write_itr - write throttle rate to register
2043  * @q_vector: pointer to interrupt data structure
2044  * @rc: pointer to ring container
2045  * @itr: throttle rate in microseconds to write
2046  */
2047 static void __ice_write_itr(struct ice_q_vector *q_vector,
2048 			    struct ice_ring_container *rc, u16 itr)
2049 {
2050 	struct ice_hw *hw = &q_vector->vsi->back->hw;
2051 
2052 	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
2053 	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
2054 }
2055 
2056 /**
2057  * ice_write_itr - write throttle rate to queue specific register
2058  * @rc: pointer to ring container
2059  * @itr: throttle rate in microseconds to write
2060  */
2061 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
2062 {
2063 	struct ice_q_vector *q_vector;
2064 
2065 	q_vector = ice_pull_qvec_from_rc(rc);
2066 	if (!q_vector)
2067 		return;
2068 
2069 	__ice_write_itr(q_vector, rc, itr);
2070 }
2071 
2072 /**
2073  * ice_set_q_vector_intrl - set up interrupt rate limiting
2074  * @q_vector: the vector to be configured
2075  *
2076  * Interrupt rate limiting is local to the vector, not per-queue so we must
2077  * detect if either ring container has dynamic moderation enabled to decide
2078  * what to set the interrupt rate limit to via INTRL settings. In the case that
2079  * dynamic moderation is disabled on both, write the value with the cached
2080  * setting to make sure INTRL register matches the user visible value.
2081  */
2082 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2083 {
2084 	if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2085 		/* in the case of dynamic enabled, cap each vector to no more
2086 		 * than (4 us) 250,000 ints/sec, which allows low latency
2087 		 * but still less than 500,000 interrupts per second, which
2088 		 * reduces CPU a bit in the case of the lowest latency
2089 		 * setting. The 4 here is a value in microseconds.
2090 		 */
2091 		ice_write_intrl(q_vector, 4);
2092 	} else {
2093 		ice_write_intrl(q_vector, q_vector->intrl);
2094 	}
2095 }
2096 
2097 /**
2098  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2099  * @vsi: the VSI being configured
2100  *
2101  * This configures MSIX mode interrupts for the PF VSI, and should not be used
2102  * for the VF VSI.
2103  */
2104 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2105 {
2106 	struct ice_pf *pf = vsi->back;
2107 	struct ice_hw *hw = &pf->hw;
2108 	u16 txq = 0, rxq = 0;
2109 	int i, q;
2110 
2111 	ice_for_each_q_vector(vsi, i) {
2112 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2113 		u16 reg_idx = q_vector->reg_idx;
2114 
2115 		ice_cfg_itr(hw, q_vector);
2116 
2117 		/* Both Transmit Queue Interrupt Cause Control register
2118 		 * and Receive Queue Interrupt Cause control register
2119 		 * expects MSIX_INDX field to be the vector index
2120 		 * within the function space and not the absolute
2121 		 * vector index across PF or across device.
2122 		 * For SR-IOV VF VSIs queue vector index always starts
2123 		 * with 1 since first vector index(0) is used for OICR
2124 		 * in VF space. Since VMDq and other PF VSIs are within
2125 		 * the PF function space, use the vector index that is
2126 		 * tracked for this PF.
2127 		 */
2128 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2129 			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2130 					      q_vector->tx.itr_idx);
2131 			txq++;
2132 		}
2133 
2134 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2135 			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2136 					      q_vector->rx.itr_idx);
2137 			rxq++;
2138 		}
2139 	}
2140 }
2141 
2142 /**
2143  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
2144  * @vsi: the VSI being changed
2145  */
2146 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
2147 {
2148 	struct ice_hw *hw = &vsi->back->hw;
2149 	struct ice_vsi_ctx *ctxt;
2150 	int ret;
2151 
2152 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2153 	if (!ctxt)
2154 		return -ENOMEM;
2155 
2156 	/* Here we are configuring the VSI to let the driver add VLAN tags by
2157 	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
2158 	 * insertion happens in the Tx hot path, in ice_tx_map.
2159 	 */
2160 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
2161 
2162 	/* Preserve existing VLAN strip setting */
2163 	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
2164 				  ICE_AQ_VSI_VLAN_EMOD_M);
2165 
2166 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2167 
2168 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2169 	if (ret) {
2170 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %d aq_err %s\n",
2171 			ret, ice_aq_str(hw->adminq.sq_last_status));
2172 		goto out;
2173 	}
2174 
2175 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2176 out:
2177 	kfree(ctxt);
2178 	return ret;
2179 }
2180 
2181 /**
2182  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
2183  * @vsi: the VSI being changed
2184  * @ena: boolean value indicating if this is a enable or disable request
2185  */
2186 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
2187 {
2188 	struct ice_hw *hw = &vsi->back->hw;
2189 	struct ice_vsi_ctx *ctxt;
2190 	int ret;
2191 
2192 	/* do not allow modifying VLAN stripping when a port VLAN is configured
2193 	 * on this VSI
2194 	 */
2195 	if (vsi->info.pvid)
2196 		return 0;
2197 
2198 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2199 	if (!ctxt)
2200 		return -ENOMEM;
2201 
2202 	/* Here we are configuring what the VSI should do with the VLAN tag in
2203 	 * the Rx packet. We can either leave the tag in the packet or put it in
2204 	 * the Rx descriptor.
2205 	 */
2206 	if (ena)
2207 		/* Strip VLAN tag from Rx packet and put it in the desc */
2208 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
2209 	else
2210 		/* Disable stripping. Leave tag in packet */
2211 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2212 
2213 	/* Allow all packets untagged/tagged */
2214 	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
2215 
2216 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2217 
2218 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2219 	if (ret) {
2220 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %d aq_err %s\n",
2221 			ena, ret, ice_aq_str(hw->adminq.sq_last_status));
2222 		goto out;
2223 	}
2224 
2225 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2226 out:
2227 	kfree(ctxt);
2228 	return ret;
2229 }
2230 
2231 /**
2232  * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2233  * @vsi: the VSI whose rings are to be enabled
2234  *
2235  * Returns 0 on success and a negative value on error
2236  */
2237 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2238 {
2239 	return ice_vsi_ctrl_all_rx_rings(vsi, true);
2240 }
2241 
2242 /**
2243  * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2244  * @vsi: the VSI whose rings are to be disabled
2245  *
2246  * Returns 0 on success and a negative value on error
2247  */
2248 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2249 {
2250 	return ice_vsi_ctrl_all_rx_rings(vsi, false);
2251 }
2252 
2253 /**
2254  * ice_vsi_stop_tx_rings - Disable Tx rings
2255  * @vsi: the VSI being configured
2256  * @rst_src: reset source
2257  * @rel_vmvf_num: Relative ID of VF/VM
2258  * @rings: Tx ring array to be stopped
2259  * @count: number of Tx ring array elements
2260  */
2261 static int
2262 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2263 		      u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2264 {
2265 	u16 q_idx;
2266 
2267 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2268 		return -EINVAL;
2269 
2270 	for (q_idx = 0; q_idx < count; q_idx++) {
2271 		struct ice_txq_meta txq_meta = { };
2272 		int status;
2273 
2274 		if (!rings || !rings[q_idx])
2275 			return -EINVAL;
2276 
2277 		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2278 		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2279 					      rings[q_idx], &txq_meta);
2280 
2281 		if (status)
2282 			return status;
2283 	}
2284 
2285 	return 0;
2286 }
2287 
2288 /**
2289  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2290  * @vsi: the VSI being configured
2291  * @rst_src: reset source
2292  * @rel_vmvf_num: Relative ID of VF/VM
2293  */
2294 int
2295 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2296 			  u16 rel_vmvf_num)
2297 {
2298 	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2299 }
2300 
2301 /**
2302  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2303  * @vsi: the VSI being configured
2304  */
2305 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2306 {
2307 	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2308 }
2309 
2310 /**
2311  * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2312  * @vsi: VSI to check whether or not VLAN pruning is enabled.
2313  *
2314  * returns true if Rx VLAN pruning is enabled and false otherwise.
2315  */
2316 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2317 {
2318 	if (!vsi)
2319 		return false;
2320 
2321 	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2322 }
2323 
2324 /**
2325  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2326  * @vsi: VSI to enable or disable VLAN pruning on
2327  * @ena: set to true to enable VLAN pruning and false to disable it
2328  *
2329  * returns 0 if VSI is updated, negative otherwise
2330  */
2331 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
2332 {
2333 	struct ice_vsi_ctx *ctxt;
2334 	struct ice_pf *pf;
2335 	int status;
2336 
2337 	if (!vsi)
2338 		return -EINVAL;
2339 
2340 	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
2341 	 * mode. VLAN pruning will be enabled when the interface exits
2342 	 * promiscuous mode if any VLAN filters are active.
2343 	 */
2344 	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2345 		return 0;
2346 
2347 	pf = vsi->back;
2348 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2349 	if (!ctxt)
2350 		return -ENOMEM;
2351 
2352 	ctxt->info = vsi->info;
2353 
2354 	if (ena)
2355 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2356 	else
2357 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2358 
2359 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2360 
2361 	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2362 	if (status) {
2363 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %s\n",
2364 			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2365 			   status, ice_aq_str(pf->hw.adminq.sq_last_status));
2366 		goto err_out;
2367 	}
2368 
2369 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2370 
2371 	kfree(ctxt);
2372 	return 0;
2373 
2374 err_out:
2375 	kfree(ctxt);
2376 	return -EIO;
2377 }
2378 
2379 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2380 {
2381 	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2382 		vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2383 		vsi->tc_cfg.numtc = 1;
2384 		return;
2385 	}
2386 
2387 	/* set VSI TC information based on DCB config */
2388 	ice_vsi_set_dcb_tc_cfg(vsi);
2389 }
2390 
2391 /**
2392  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2393  * @vsi: VSI to set the q_vectors register index on
2394  */
2395 static int
2396 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2397 {
2398 	u16 i;
2399 
2400 	if (!vsi || !vsi->q_vectors)
2401 		return -EINVAL;
2402 
2403 	ice_for_each_q_vector(vsi, i) {
2404 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2405 
2406 		if (!q_vector) {
2407 			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2408 				i, vsi->vsi_num);
2409 			goto clear_reg_idx;
2410 		}
2411 
2412 		if (vsi->type == ICE_VSI_VF) {
2413 			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2414 
2415 			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2416 		} else {
2417 			q_vector->reg_idx =
2418 				q_vector->v_idx + vsi->base_vector;
2419 		}
2420 	}
2421 
2422 	return 0;
2423 
2424 clear_reg_idx:
2425 	ice_for_each_q_vector(vsi, i) {
2426 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2427 
2428 		if (q_vector)
2429 			q_vector->reg_idx = 0;
2430 	}
2431 
2432 	return -EINVAL;
2433 }
2434 
2435 /**
2436  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2437  * @vsi: the VSI being configured
2438  * @tx: bool to determine Tx or Rx rule
2439  * @create: bool to determine create or remove Rule
2440  */
2441 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2442 {
2443 	int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2444 			enum ice_sw_fwd_act_type act);
2445 	struct ice_pf *pf = vsi->back;
2446 	struct device *dev;
2447 	int status;
2448 
2449 	dev = ice_pf_to_dev(pf);
2450 	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2451 
2452 	if (tx) {
2453 		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2454 				  ICE_DROP_PACKET);
2455 	} else {
2456 		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2457 			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2458 							  create);
2459 		} else {
2460 			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2461 					  ICE_FWD_TO_VSI);
2462 		}
2463 	}
2464 
2465 	if (status)
2466 		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2467 			create ? "adding" : "removing", tx ? "TX" : "RX",
2468 			vsi->vsi_num, status);
2469 }
2470 
2471 /**
2472  * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2473  * @vsi: pointer to the VSI
2474  *
2475  * This function will allocate new scheduler aggregator now if needed and will
2476  * move specified VSI into it.
2477  */
2478 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2479 {
2480 	struct device *dev = ice_pf_to_dev(vsi->back);
2481 	struct ice_agg_node *agg_node_iter = NULL;
2482 	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2483 	struct ice_agg_node *agg_node = NULL;
2484 	int node_offset, max_agg_nodes = 0;
2485 	struct ice_port_info *port_info;
2486 	struct ice_pf *pf = vsi->back;
2487 	u32 agg_node_id_start = 0;
2488 	int status;
2489 
2490 	/* create (as needed) scheduler aggregator node and move VSI into
2491 	 * corresponding aggregator node
2492 	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2493 	 * - VF aggregator nodes will contain VF VSI
2494 	 */
2495 	port_info = pf->hw.port_info;
2496 	if (!port_info)
2497 		return;
2498 
2499 	switch (vsi->type) {
2500 	case ICE_VSI_CTRL:
2501 	case ICE_VSI_CHNL:
2502 	case ICE_VSI_LB:
2503 	case ICE_VSI_PF:
2504 	case ICE_VSI_SWITCHDEV_CTRL:
2505 		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2506 		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2507 		agg_node_iter = &pf->pf_agg_node[0];
2508 		break;
2509 	case ICE_VSI_VF:
2510 		/* user can create 'n' VFs on a given PF, but since max children
2511 		 * per aggregator node can be only 64. Following code handles
2512 		 * aggregator(s) for VF VSIs, either selects a agg_node which
2513 		 * was already created provided num_vsis < 64, otherwise
2514 		 * select next available node, which will be created
2515 		 */
2516 		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2517 		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2518 		agg_node_iter = &pf->vf_agg_node[0];
2519 		break;
2520 	default:
2521 		/* other VSI type, handle later if needed */
2522 		dev_dbg(dev, "unexpected VSI type %s\n",
2523 			ice_vsi_type_str(vsi->type));
2524 		return;
2525 	}
2526 
2527 	/* find the appropriate aggregator node */
2528 	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2529 		/* see if we can find space in previously created
2530 		 * node if num_vsis < 64, otherwise skip
2531 		 */
2532 		if (agg_node_iter->num_vsis &&
2533 		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2534 			agg_node_iter++;
2535 			continue;
2536 		}
2537 
2538 		if (agg_node_iter->valid &&
2539 		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2540 			agg_id = agg_node_iter->agg_id;
2541 			agg_node = agg_node_iter;
2542 			break;
2543 		}
2544 
2545 		/* find unclaimed agg_id */
2546 		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2547 			agg_id = node_offset + agg_node_id_start;
2548 			agg_node = agg_node_iter;
2549 			break;
2550 		}
2551 		/* move to next agg_node */
2552 		agg_node_iter++;
2553 	}
2554 
2555 	if (!agg_node)
2556 		return;
2557 
2558 	/* if selected aggregator node was not created, create it */
2559 	if (!agg_node->valid) {
2560 		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2561 				     (u8)vsi->tc_cfg.ena_tc);
2562 		if (status) {
2563 			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2564 				agg_id);
2565 			return;
2566 		}
2567 		/* aggregator node is created, store the neeeded info */
2568 		agg_node->valid = true;
2569 		agg_node->agg_id = agg_id;
2570 	}
2571 
2572 	/* move VSI to corresponding aggregator node */
2573 	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2574 				     (u8)vsi->tc_cfg.ena_tc);
2575 	if (status) {
2576 		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2577 			vsi->idx, agg_id);
2578 		return;
2579 	}
2580 
2581 	/* keep active children count for aggregator node */
2582 	agg_node->num_vsis++;
2583 
2584 	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2585 	 * to aggregator node
2586 	 */
2587 	vsi->agg_node = agg_node;
2588 	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2589 		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2590 		vsi->agg_node->num_vsis);
2591 }
2592 
2593 /**
2594  * ice_vsi_setup - Set up a VSI by a given type
2595  * @pf: board private structure
2596  * @pi: pointer to the port_info instance
2597  * @vsi_type: VSI type
2598  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2599  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2600  *         fill-in ICE_INVAL_VFID as input.
2601  * @ch: ptr to channel
2602  *
2603  * This allocates the sw VSI structure and its queue resources.
2604  *
2605  * Returns pointer to the successfully allocated and configured VSI sw struct on
2606  * success, NULL on failure.
2607  */
2608 struct ice_vsi *
2609 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2610 	      enum ice_vsi_type vsi_type, u16 vf_id, struct ice_channel *ch)
2611 {
2612 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2613 	struct device *dev = ice_pf_to_dev(pf);
2614 	struct ice_vsi *vsi;
2615 	int ret, i;
2616 
2617 	if (vsi_type == ICE_VSI_CHNL)
2618 		vsi = ice_vsi_alloc(pf, vsi_type, ch, ICE_INVAL_VFID);
2619 	else if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2620 		vsi = ice_vsi_alloc(pf, vsi_type, NULL, vf_id);
2621 	else
2622 		vsi = ice_vsi_alloc(pf, vsi_type, NULL, ICE_INVAL_VFID);
2623 
2624 	if (!vsi) {
2625 		dev_err(dev, "could not allocate VSI\n");
2626 		return NULL;
2627 	}
2628 
2629 	vsi->port_info = pi;
2630 	vsi->vsw = pf->first_sw;
2631 	if (vsi->type == ICE_VSI_PF)
2632 		vsi->ethtype = ETH_P_PAUSE;
2633 
2634 	if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2635 		vsi->vf_id = vf_id;
2636 
2637 	ice_alloc_fd_res(vsi);
2638 
2639 	if (vsi_type != ICE_VSI_CHNL) {
2640 		if (ice_vsi_get_qs(vsi)) {
2641 			dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2642 				vsi->idx);
2643 			goto unroll_vsi_alloc;
2644 		}
2645 	}
2646 
2647 	/* set RSS capabilities */
2648 	ice_vsi_set_rss_params(vsi);
2649 
2650 	/* set TC configuration */
2651 	ice_vsi_set_tc_cfg(vsi);
2652 
2653 	/* create the VSI */
2654 	ret = ice_vsi_init(vsi, true);
2655 	if (ret)
2656 		goto unroll_get_qs;
2657 
2658 	switch (vsi->type) {
2659 	case ICE_VSI_CTRL:
2660 	case ICE_VSI_SWITCHDEV_CTRL:
2661 	case ICE_VSI_PF:
2662 		ret = ice_vsi_alloc_q_vectors(vsi);
2663 		if (ret)
2664 			goto unroll_vsi_init;
2665 
2666 		ret = ice_vsi_setup_vector_base(vsi);
2667 		if (ret)
2668 			goto unroll_alloc_q_vector;
2669 
2670 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2671 		if (ret)
2672 			goto unroll_vector_base;
2673 
2674 		ret = ice_vsi_alloc_rings(vsi);
2675 		if (ret)
2676 			goto unroll_vector_base;
2677 
2678 		/* Always add VLAN ID 0 switch rule by default. This is needed
2679 		 * in order to allow all untagged and 0 tagged priority traffic
2680 		 * if Rx VLAN pruning is enabled. Also there are cases where we
2681 		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2682 		 * so this handles those cases (i.e. adding the PF to a bridge
2683 		 * without the 8021q module loaded).
2684 		 */
2685 		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2686 		if (ret)
2687 			goto unroll_clear_rings;
2688 
2689 		ice_vsi_map_rings_to_vectors(vsi);
2690 
2691 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2692 		if (vsi->type != ICE_VSI_CTRL)
2693 			/* Do not exit if configuring RSS had an issue, at
2694 			 * least receive traffic on first queue. Hence no
2695 			 * need to capture return value
2696 			 */
2697 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2698 				ice_vsi_cfg_rss_lut_key(vsi);
2699 				ice_vsi_set_rss_flow_fld(vsi);
2700 			}
2701 		ice_init_arfs(vsi);
2702 		break;
2703 	case ICE_VSI_CHNL:
2704 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2705 			ice_vsi_cfg_rss_lut_key(vsi);
2706 			ice_vsi_set_rss_flow_fld(vsi);
2707 		}
2708 		break;
2709 	case ICE_VSI_VF:
2710 		/* VF driver will take care of creating netdev for this type and
2711 		 * map queues to vectors through Virtchnl, PF driver only
2712 		 * creates a VSI and corresponding structures for bookkeeping
2713 		 * purpose
2714 		 */
2715 		ret = ice_vsi_alloc_q_vectors(vsi);
2716 		if (ret)
2717 			goto unroll_vsi_init;
2718 
2719 		ret = ice_vsi_alloc_rings(vsi);
2720 		if (ret)
2721 			goto unroll_alloc_q_vector;
2722 
2723 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2724 		if (ret)
2725 			goto unroll_vector_base;
2726 
2727 		/* Do not exit if configuring RSS had an issue, at least
2728 		 * receive traffic on first queue. Hence no need to capture
2729 		 * return value
2730 		 */
2731 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2732 			ice_vsi_cfg_rss_lut_key(vsi);
2733 			ice_vsi_set_vf_rss_flow_fld(vsi);
2734 		}
2735 		break;
2736 	case ICE_VSI_LB:
2737 		ret = ice_vsi_alloc_rings(vsi);
2738 		if (ret)
2739 			goto unroll_vsi_init;
2740 		break;
2741 	default:
2742 		/* clean up the resources and exit */
2743 		goto unroll_vsi_init;
2744 	}
2745 
2746 	/* configure VSI nodes based on number of queues and TC's */
2747 	ice_for_each_traffic_class(i) {
2748 		if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2749 			continue;
2750 
2751 		if (vsi->type == ICE_VSI_CHNL) {
2752 			if (!vsi->alloc_txq && vsi->num_txq)
2753 				max_txqs[i] = vsi->num_txq;
2754 			else
2755 				max_txqs[i] = pf->num_lan_tx;
2756 		} else {
2757 			max_txqs[i] = vsi->alloc_txq;
2758 		}
2759 	}
2760 
2761 	dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2762 	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2763 			      max_txqs);
2764 	if (ret) {
2765 		dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2766 			vsi->vsi_num, ret);
2767 		goto unroll_clear_rings;
2768 	}
2769 
2770 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2771 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2772 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2773 	 * The rule is added once for PF VSI in order to create appropriate
2774 	 * recipe, since VSI/VSI list is ignored with drop action...
2775 	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2776 	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2777 	 * settings in the HW.
2778 	 */
2779 	if (!ice_is_safe_mode(pf))
2780 		if (vsi->type == ICE_VSI_PF) {
2781 			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2782 					 ICE_DROP_PACKET);
2783 			ice_cfg_sw_lldp(vsi, true, true);
2784 		}
2785 
2786 	if (!vsi->agg_node)
2787 		ice_set_agg_vsi(vsi);
2788 	return vsi;
2789 
2790 unroll_clear_rings:
2791 	ice_vsi_clear_rings(vsi);
2792 unroll_vector_base:
2793 	/* reclaim SW interrupts back to the common pool */
2794 	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2795 	pf->num_avail_sw_msix += vsi->num_q_vectors;
2796 unroll_alloc_q_vector:
2797 	ice_vsi_free_q_vectors(vsi);
2798 unroll_vsi_init:
2799 	ice_vsi_delete(vsi);
2800 unroll_get_qs:
2801 	ice_vsi_put_qs(vsi);
2802 unroll_vsi_alloc:
2803 	if (vsi_type == ICE_VSI_VF)
2804 		ice_enable_lag(pf->lag);
2805 	ice_vsi_clear(vsi);
2806 
2807 	return NULL;
2808 }
2809 
2810 /**
2811  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2812  * @vsi: the VSI being cleaned up
2813  */
2814 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2815 {
2816 	struct ice_pf *pf = vsi->back;
2817 	struct ice_hw *hw = &pf->hw;
2818 	u32 txq = 0;
2819 	u32 rxq = 0;
2820 	int i, q;
2821 
2822 	ice_for_each_q_vector(vsi, i) {
2823 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2824 
2825 		ice_write_intrl(q_vector, 0);
2826 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2827 			ice_write_itr(&q_vector->tx, 0);
2828 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2829 			if (ice_is_xdp_ena_vsi(vsi)) {
2830 				u32 xdp_txq = txq + vsi->num_xdp_txq;
2831 
2832 				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2833 			}
2834 			txq++;
2835 		}
2836 
2837 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2838 			ice_write_itr(&q_vector->rx, 0);
2839 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2840 			rxq++;
2841 		}
2842 	}
2843 
2844 	ice_flush(hw);
2845 }
2846 
2847 /**
2848  * ice_vsi_free_irq - Free the IRQ association with the OS
2849  * @vsi: the VSI being configured
2850  */
2851 void ice_vsi_free_irq(struct ice_vsi *vsi)
2852 {
2853 	struct ice_pf *pf = vsi->back;
2854 	int base = vsi->base_vector;
2855 	int i;
2856 
2857 	if (!vsi->q_vectors || !vsi->irqs_ready)
2858 		return;
2859 
2860 	ice_vsi_release_msix(vsi);
2861 	if (vsi->type == ICE_VSI_VF)
2862 		return;
2863 
2864 	vsi->irqs_ready = false;
2865 	ice_for_each_q_vector(vsi, i) {
2866 		u16 vector = i + base;
2867 		int irq_num;
2868 
2869 		irq_num = pf->msix_entries[vector].vector;
2870 
2871 		/* free only the irqs that were actually requested */
2872 		if (!vsi->q_vectors[i] ||
2873 		    !(vsi->q_vectors[i]->num_ring_tx ||
2874 		      vsi->q_vectors[i]->num_ring_rx))
2875 			continue;
2876 
2877 		/* clear the affinity notifier in the IRQ descriptor */
2878 		irq_set_affinity_notifier(irq_num, NULL);
2879 
2880 		/* clear the affinity_mask in the IRQ descriptor */
2881 		irq_set_affinity_hint(irq_num, NULL);
2882 		synchronize_irq(irq_num);
2883 		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2884 	}
2885 }
2886 
2887 /**
2888  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2889  * @vsi: the VSI having resources freed
2890  */
2891 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2892 {
2893 	int i;
2894 
2895 	if (!vsi->tx_rings)
2896 		return;
2897 
2898 	ice_for_each_txq(vsi, i)
2899 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2900 			ice_free_tx_ring(vsi->tx_rings[i]);
2901 }
2902 
2903 /**
2904  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2905  * @vsi: the VSI having resources freed
2906  */
2907 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2908 {
2909 	int i;
2910 
2911 	if (!vsi->rx_rings)
2912 		return;
2913 
2914 	ice_for_each_rxq(vsi, i)
2915 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2916 			ice_free_rx_ring(vsi->rx_rings[i]);
2917 }
2918 
2919 /**
2920  * ice_vsi_close - Shut down a VSI
2921  * @vsi: the VSI being shut down
2922  */
2923 void ice_vsi_close(struct ice_vsi *vsi)
2924 {
2925 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2926 		ice_down(vsi);
2927 
2928 	ice_vsi_free_irq(vsi);
2929 	ice_vsi_free_tx_rings(vsi);
2930 	ice_vsi_free_rx_rings(vsi);
2931 }
2932 
2933 /**
2934  * ice_ena_vsi - resume a VSI
2935  * @vsi: the VSI being resume
2936  * @locked: is the rtnl_lock already held
2937  */
2938 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2939 {
2940 	int err = 0;
2941 
2942 	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2943 		return 0;
2944 
2945 	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2946 
2947 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2948 		if (netif_running(vsi->netdev)) {
2949 			if (!locked)
2950 				rtnl_lock();
2951 
2952 			err = ice_open_internal(vsi->netdev);
2953 
2954 			if (!locked)
2955 				rtnl_unlock();
2956 		}
2957 	} else if (vsi->type == ICE_VSI_CTRL) {
2958 		err = ice_vsi_open_ctrl(vsi);
2959 	}
2960 
2961 	return err;
2962 }
2963 
2964 /**
2965  * ice_dis_vsi - pause a VSI
2966  * @vsi: the VSI being paused
2967  * @locked: is the rtnl_lock already held
2968  */
2969 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2970 {
2971 	if (test_bit(ICE_VSI_DOWN, vsi->state))
2972 		return;
2973 
2974 	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2975 
2976 	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2977 		if (netif_running(vsi->netdev)) {
2978 			if (!locked)
2979 				rtnl_lock();
2980 
2981 			ice_vsi_close(vsi);
2982 
2983 			if (!locked)
2984 				rtnl_unlock();
2985 		} else {
2986 			ice_vsi_close(vsi);
2987 		}
2988 	} else if (vsi->type == ICE_VSI_CTRL ||
2989 		   vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2990 		ice_vsi_close(vsi);
2991 	}
2992 }
2993 
2994 /**
2995  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2996  * @vsi: the VSI being un-configured
2997  */
2998 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2999 {
3000 	int base = vsi->base_vector;
3001 	struct ice_pf *pf = vsi->back;
3002 	struct ice_hw *hw = &pf->hw;
3003 	u32 val;
3004 	int i;
3005 
3006 	/* disable interrupt causation from each queue */
3007 	if (vsi->tx_rings) {
3008 		ice_for_each_txq(vsi, i) {
3009 			if (vsi->tx_rings[i]) {
3010 				u16 reg;
3011 
3012 				reg = vsi->tx_rings[i]->reg_idx;
3013 				val = rd32(hw, QINT_TQCTL(reg));
3014 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
3015 				wr32(hw, QINT_TQCTL(reg), val);
3016 			}
3017 		}
3018 	}
3019 
3020 	if (vsi->rx_rings) {
3021 		ice_for_each_rxq(vsi, i) {
3022 			if (vsi->rx_rings[i]) {
3023 				u16 reg;
3024 
3025 				reg = vsi->rx_rings[i]->reg_idx;
3026 				val = rd32(hw, QINT_RQCTL(reg));
3027 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
3028 				wr32(hw, QINT_RQCTL(reg), val);
3029 			}
3030 		}
3031 	}
3032 
3033 	/* disable each interrupt */
3034 	ice_for_each_q_vector(vsi, i) {
3035 		if (!vsi->q_vectors[i])
3036 			continue;
3037 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
3038 	}
3039 
3040 	ice_flush(hw);
3041 
3042 	/* don't call synchronize_irq() for VF's from the host */
3043 	if (vsi->type == ICE_VSI_VF)
3044 		return;
3045 
3046 	ice_for_each_q_vector(vsi, i)
3047 		synchronize_irq(pf->msix_entries[i + base].vector);
3048 }
3049 
3050 /**
3051  * ice_napi_del - Remove NAPI handler for the VSI
3052  * @vsi: VSI for which NAPI handler is to be removed
3053  */
3054 void ice_napi_del(struct ice_vsi *vsi)
3055 {
3056 	int v_idx;
3057 
3058 	if (!vsi->netdev)
3059 		return;
3060 
3061 	ice_for_each_q_vector(vsi, v_idx)
3062 		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
3063 }
3064 
3065 /**
3066  * ice_vsi_release - Delete a VSI and free its resources
3067  * @vsi: the VSI being removed
3068  *
3069  * Returns 0 on success or < 0 on error
3070  */
3071 int ice_vsi_release(struct ice_vsi *vsi)
3072 {
3073 	struct ice_pf *pf;
3074 	int err;
3075 
3076 	if (!vsi->back)
3077 		return -ENODEV;
3078 	pf = vsi->back;
3079 
3080 	/* do not unregister while driver is in the reset recovery pending
3081 	 * state. Since reset/rebuild happens through PF service task workqueue,
3082 	 * it's not a good idea to unregister netdev that is associated to the
3083 	 * PF that is running the work queue items currently. This is done to
3084 	 * avoid check_flush_dependency() warning on this wq
3085 	 */
3086 	if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
3087 	    (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
3088 		unregister_netdev(vsi->netdev);
3089 		clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3090 	}
3091 
3092 	if (vsi->type == ICE_VSI_PF)
3093 		ice_devlink_destroy_pf_port(pf);
3094 
3095 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3096 		ice_rss_clean(vsi);
3097 
3098 	/* Disable VSI and free resources */
3099 	if (vsi->type != ICE_VSI_LB)
3100 		ice_vsi_dis_irq(vsi);
3101 	ice_vsi_close(vsi);
3102 
3103 	/* SR-IOV determines needed MSIX resources all at once instead of per
3104 	 * VSI since when VFs are spawned we know how many VFs there are and how
3105 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
3106 	 * cleared in the same manner.
3107 	 */
3108 	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
3109 		int i;
3110 
3111 		ice_for_each_vf(pf, i) {
3112 			struct ice_vf *vf = &pf->vf[i];
3113 
3114 			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
3115 				break;
3116 		}
3117 		if (i == pf->num_alloc_vfs) {
3118 			/* No other VFs left that have control VSI, reclaim SW
3119 			 * interrupts back to the common pool
3120 			 */
3121 			ice_free_res(pf->irq_tracker, vsi->base_vector,
3122 				     ICE_RES_VF_CTRL_VEC_ID);
3123 			pf->num_avail_sw_msix += vsi->num_q_vectors;
3124 		}
3125 	} else if (vsi->type != ICE_VSI_VF) {
3126 		/* reclaim SW interrupts back to the common pool */
3127 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3128 		pf->num_avail_sw_msix += vsi->num_q_vectors;
3129 	}
3130 
3131 	if (!ice_is_safe_mode(pf)) {
3132 		if (vsi->type == ICE_VSI_PF) {
3133 			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
3134 					    ICE_DROP_PACKET);
3135 			ice_cfg_sw_lldp(vsi, true, false);
3136 			/* The Rx rule will only exist to remove if the LLDP FW
3137 			 * engine is currently stopped
3138 			 */
3139 			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
3140 				ice_cfg_sw_lldp(vsi, false, false);
3141 		}
3142 	}
3143 
3144 	ice_fltr_remove_all(vsi);
3145 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3146 	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3147 	if (err)
3148 		dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3149 			vsi->vsi_num, err);
3150 	ice_vsi_delete(vsi);
3151 	ice_vsi_free_q_vectors(vsi);
3152 
3153 	if (vsi->netdev) {
3154 		if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
3155 			unregister_netdev(vsi->netdev);
3156 			clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3157 		}
3158 		if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
3159 			free_netdev(vsi->netdev);
3160 			vsi->netdev = NULL;
3161 			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3162 		}
3163 	}
3164 
3165 	if (vsi->type == ICE_VSI_VF &&
3166 	    vsi->agg_node && vsi->agg_node->valid)
3167 		vsi->agg_node->num_vsis--;
3168 	ice_vsi_clear_rings(vsi);
3169 
3170 	ice_vsi_put_qs(vsi);
3171 
3172 	/* retain SW VSI data structure since it is needed to unregister and
3173 	 * free VSI netdev when PF is not in reset recovery pending state,\
3174 	 * for ex: during rmmod.
3175 	 */
3176 	if (!ice_is_reset_in_progress(pf->state))
3177 		ice_vsi_clear(vsi);
3178 
3179 	return 0;
3180 }
3181 
3182 /**
3183  * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
3184  * @vsi: VSI connected with q_vectors
3185  * @coalesce: array of struct with stored coalesce
3186  *
3187  * Returns array size.
3188  */
3189 static int
3190 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3191 			     struct ice_coalesce_stored *coalesce)
3192 {
3193 	int i;
3194 
3195 	ice_for_each_q_vector(vsi, i) {
3196 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
3197 
3198 		coalesce[i].itr_tx = q_vector->tx.itr_setting;
3199 		coalesce[i].itr_rx = q_vector->rx.itr_setting;
3200 		coalesce[i].intrl = q_vector->intrl;
3201 
3202 		if (i < vsi->num_txq)
3203 			coalesce[i].tx_valid = true;
3204 		if (i < vsi->num_rxq)
3205 			coalesce[i].rx_valid = true;
3206 	}
3207 
3208 	return vsi->num_q_vectors;
3209 }
3210 
3211 /**
3212  * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3213  * @vsi: VSI connected with q_vectors
3214  * @coalesce: pointer to array of struct with stored coalesce
3215  * @size: size of coalesce array
3216  *
3217  * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3218  * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3219  * to default value.
3220  */
3221 static void
3222 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3223 			     struct ice_coalesce_stored *coalesce, int size)
3224 {
3225 	struct ice_ring_container *rc;
3226 	int i;
3227 
3228 	if ((size && !coalesce) || !vsi)
3229 		return;
3230 
3231 	/* There are a couple of cases that have to be handled here:
3232 	 *   1. The case where the number of queue vectors stays the same, but
3233 	 *      the number of Tx or Rx rings changes (the first for loop)
3234 	 *   2. The case where the number of queue vectors increased (the
3235 	 *      second for loop)
3236 	 */
3237 	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3238 		/* There are 2 cases to handle here and they are the same for
3239 		 * both Tx and Rx:
3240 		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
3241 		 *   and the loop variable is less than the number of rings
3242 		 *   allocated, then write the previous values
3243 		 *
3244 		 *   if the entry was not valid previously, but the number of
3245 		 *   rings is less than are allocated (this means the number of
3246 		 *   rings increased from previously), then write out the
3247 		 *   values in the first element
3248 		 *
3249 		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
3250 		 *   as there is no harm because the dynamic algorithm
3251 		 *   will just overwrite.
3252 		 */
3253 		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3254 			rc = &vsi->q_vectors[i]->rx;
3255 			rc->itr_setting = coalesce[i].itr_rx;
3256 			ice_write_itr(rc, rc->itr_setting);
3257 		} else if (i < vsi->alloc_rxq) {
3258 			rc = &vsi->q_vectors[i]->rx;
3259 			rc->itr_setting = coalesce[0].itr_rx;
3260 			ice_write_itr(rc, rc->itr_setting);
3261 		}
3262 
3263 		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3264 			rc = &vsi->q_vectors[i]->tx;
3265 			rc->itr_setting = coalesce[i].itr_tx;
3266 			ice_write_itr(rc, rc->itr_setting);
3267 		} else if (i < vsi->alloc_txq) {
3268 			rc = &vsi->q_vectors[i]->tx;
3269 			rc->itr_setting = coalesce[0].itr_tx;
3270 			ice_write_itr(rc, rc->itr_setting);
3271 		}
3272 
3273 		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3274 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
3275 	}
3276 
3277 	/* the number of queue vectors increased so write whatever is in
3278 	 * the first element
3279 	 */
3280 	for (; i < vsi->num_q_vectors; i++) {
3281 		/* transmit */
3282 		rc = &vsi->q_vectors[i]->tx;
3283 		rc->itr_setting = coalesce[0].itr_tx;
3284 		ice_write_itr(rc, rc->itr_setting);
3285 
3286 		/* receive */
3287 		rc = &vsi->q_vectors[i]->rx;
3288 		rc->itr_setting = coalesce[0].itr_rx;
3289 		ice_write_itr(rc, rc->itr_setting);
3290 
3291 		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3292 		ice_set_q_vector_intrl(vsi->q_vectors[i]);
3293 	}
3294 }
3295 
3296 /**
3297  * ice_vsi_rebuild - Rebuild VSI after reset
3298  * @vsi: VSI to be rebuild
3299  * @init_vsi: is this an initialization or a reconfigure of the VSI
3300  *
3301  * Returns 0 on success and negative value on failure
3302  */
3303 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3304 {
3305 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3306 	struct ice_coalesce_stored *coalesce;
3307 	int prev_num_q_vectors = 0;
3308 	struct ice_vf *vf = NULL;
3309 	enum ice_vsi_type vtype;
3310 	struct ice_pf *pf;
3311 	int ret, i;
3312 
3313 	if (!vsi)
3314 		return -EINVAL;
3315 
3316 	pf = vsi->back;
3317 	vtype = vsi->type;
3318 	if (vtype == ICE_VSI_VF)
3319 		vf = &pf->vf[vsi->vf_id];
3320 
3321 	coalesce = kcalloc(vsi->num_q_vectors,
3322 			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3323 	if (!coalesce)
3324 		return -ENOMEM;
3325 
3326 	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3327 
3328 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3329 	ret = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3330 	if (ret)
3331 		dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3332 			vsi->vsi_num, ret);
3333 	ice_vsi_free_q_vectors(vsi);
3334 
3335 	/* SR-IOV determines needed MSIX resources all at once instead of per
3336 	 * VSI since when VFs are spawned we know how many VFs there are and how
3337 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
3338 	 * cleared in the same manner.
3339 	 */
3340 	if (vtype != ICE_VSI_VF) {
3341 		/* reclaim SW interrupts back to the common pool */
3342 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3343 		pf->num_avail_sw_msix += vsi->num_q_vectors;
3344 		vsi->base_vector = 0;
3345 	}
3346 
3347 	if (ice_is_xdp_ena_vsi(vsi))
3348 		/* return value check can be skipped here, it always returns
3349 		 * 0 if reset is in progress
3350 		 */
3351 		ice_destroy_xdp_rings(vsi);
3352 	ice_vsi_put_qs(vsi);
3353 	ice_vsi_clear_rings(vsi);
3354 	ice_vsi_free_arrays(vsi);
3355 	if (vtype == ICE_VSI_VF)
3356 		ice_vsi_set_num_qs(vsi, vf->vf_id);
3357 	else
3358 		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3359 
3360 	ret = ice_vsi_alloc_arrays(vsi);
3361 	if (ret < 0)
3362 		goto err_vsi;
3363 
3364 	ice_vsi_get_qs(vsi);
3365 
3366 	ice_alloc_fd_res(vsi);
3367 	ice_vsi_set_tc_cfg(vsi);
3368 
3369 	/* Initialize VSI struct elements and create VSI in FW */
3370 	ret = ice_vsi_init(vsi, init_vsi);
3371 	if (ret < 0)
3372 		goto err_vsi;
3373 
3374 	switch (vtype) {
3375 	case ICE_VSI_CTRL:
3376 	case ICE_VSI_SWITCHDEV_CTRL:
3377 	case ICE_VSI_PF:
3378 		ret = ice_vsi_alloc_q_vectors(vsi);
3379 		if (ret)
3380 			goto err_rings;
3381 
3382 		ret = ice_vsi_setup_vector_base(vsi);
3383 		if (ret)
3384 			goto err_vectors;
3385 
3386 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3387 		if (ret)
3388 			goto err_vectors;
3389 
3390 		ret = ice_vsi_alloc_rings(vsi);
3391 		if (ret)
3392 			goto err_vectors;
3393 
3394 		ice_vsi_map_rings_to_vectors(vsi);
3395 		if (ice_is_xdp_ena_vsi(vsi)) {
3396 			ret = ice_vsi_determine_xdp_res(vsi);
3397 			if (ret)
3398 				goto err_vectors;
3399 			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3400 			if (ret)
3401 				goto err_vectors;
3402 		}
3403 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3404 		if (vtype != ICE_VSI_CTRL)
3405 			/* Do not exit if configuring RSS had an issue, at
3406 			 * least receive traffic on first queue. Hence no
3407 			 * need to capture return value
3408 			 */
3409 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3410 				ice_vsi_cfg_rss_lut_key(vsi);
3411 		break;
3412 	case ICE_VSI_VF:
3413 		ret = ice_vsi_alloc_q_vectors(vsi);
3414 		if (ret)
3415 			goto err_rings;
3416 
3417 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3418 		if (ret)
3419 			goto err_vectors;
3420 
3421 		ret = ice_vsi_alloc_rings(vsi);
3422 		if (ret)
3423 			goto err_vectors;
3424 
3425 		break;
3426 	case ICE_VSI_CHNL:
3427 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3428 			ice_vsi_cfg_rss_lut_key(vsi);
3429 			ice_vsi_set_rss_flow_fld(vsi);
3430 		}
3431 		break;
3432 	default:
3433 		break;
3434 	}
3435 
3436 	/* configure VSI nodes based on number of queues and TC's */
3437 	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3438 		/* configure VSI nodes based on number of queues and TC's.
3439 		 * ADQ creates VSIs for each TC/Channel but doesn't
3440 		 * allocate queues instead it reconfigures the PF queues
3441 		 * as per the TC command. So max_txqs should point to the
3442 		 * PF Tx queues.
3443 		 */
3444 		if (vtype == ICE_VSI_CHNL)
3445 			max_txqs[i] = pf->num_lan_tx;
3446 		else
3447 			max_txqs[i] = vsi->alloc_txq;
3448 
3449 		if (ice_is_xdp_ena_vsi(vsi))
3450 			max_txqs[i] += vsi->num_xdp_txq;
3451 	}
3452 
3453 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3454 		/* If MQPRIO is set, means channel code path, hence for main
3455 		 * VSI's, use TC as 1
3456 		 */
3457 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3458 	else
3459 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3460 				      vsi->tc_cfg.ena_tc, max_txqs);
3461 
3462 	if (ret) {
3463 		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %d\n",
3464 			vsi->vsi_num, ret);
3465 		if (init_vsi) {
3466 			ret = -EIO;
3467 			goto err_vectors;
3468 		} else {
3469 			return ice_schedule_reset(pf, ICE_RESET_PFR);
3470 		}
3471 	}
3472 	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3473 	kfree(coalesce);
3474 
3475 	return 0;
3476 
3477 err_vectors:
3478 	ice_vsi_free_q_vectors(vsi);
3479 err_rings:
3480 	if (vsi->netdev) {
3481 		vsi->current_netdev_flags = 0;
3482 		unregister_netdev(vsi->netdev);
3483 		free_netdev(vsi->netdev);
3484 		vsi->netdev = NULL;
3485 	}
3486 err_vsi:
3487 	ice_vsi_clear(vsi);
3488 	set_bit(ICE_RESET_FAILED, pf->state);
3489 	kfree(coalesce);
3490 	return ret;
3491 }
3492 
3493 /**
3494  * ice_is_reset_in_progress - check for a reset in progress
3495  * @state: PF state field
3496  */
3497 bool ice_is_reset_in_progress(unsigned long *state)
3498 {
3499 	return test_bit(ICE_RESET_OICR_RECV, state) ||
3500 	       test_bit(ICE_PFR_REQ, state) ||
3501 	       test_bit(ICE_CORER_REQ, state) ||
3502 	       test_bit(ICE_GLOBR_REQ, state);
3503 }
3504 
3505 /**
3506  * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3507  * @pf: pointer to the PF structure
3508  * @timeout: length of time to wait, in jiffies
3509  *
3510  * Wait (sleep) for a short time until the driver finishes cleaning up from
3511  * a device reset. The caller must be able to sleep. Use this to delay
3512  * operations that could fail while the driver is cleaning up after a device
3513  * reset.
3514  *
3515  * Returns 0 on success, -EBUSY if the reset is not finished within the
3516  * timeout, and -ERESTARTSYS if the thread was interrupted.
3517  */
3518 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3519 {
3520 	long ret;
3521 
3522 	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3523 					       !ice_is_reset_in_progress(pf->state),
3524 					       timeout);
3525 	if (ret < 0)
3526 		return ret;
3527 	else if (!ret)
3528 		return -EBUSY;
3529 	else
3530 		return 0;
3531 }
3532 
3533 /**
3534  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3535  * @vsi: VSI being configured
3536  * @ctx: the context buffer returned from AQ VSI update command
3537  */
3538 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3539 {
3540 	vsi->info.mapping_flags = ctx->info.mapping_flags;
3541 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3542 	       sizeof(vsi->info.q_mapping));
3543 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3544 	       sizeof(vsi->info.tc_mapping));
3545 }
3546 
3547 /**
3548  * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3549  * @vsi: the VSI being configured
3550  * @ena_tc: TC map to be enabled
3551  */
3552 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3553 {
3554 	struct net_device *netdev = vsi->netdev;
3555 	struct ice_pf *pf = vsi->back;
3556 	int numtc = vsi->tc_cfg.numtc;
3557 	struct ice_dcbx_cfg *dcbcfg;
3558 	u8 netdev_tc;
3559 	int i;
3560 
3561 	if (!netdev)
3562 		return;
3563 
3564 	/* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3565 	if (vsi->type == ICE_VSI_CHNL)
3566 		return;
3567 
3568 	if (!ena_tc) {
3569 		netdev_reset_tc(netdev);
3570 		return;
3571 	}
3572 
3573 	if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3574 		numtc = vsi->all_numtc;
3575 
3576 	if (netdev_set_num_tc(netdev, numtc))
3577 		return;
3578 
3579 	dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3580 
3581 	ice_for_each_traffic_class(i)
3582 		if (vsi->tc_cfg.ena_tc & BIT(i))
3583 			netdev_set_tc_queue(netdev,
3584 					    vsi->tc_cfg.tc_info[i].netdev_tc,
3585 					    vsi->tc_cfg.tc_info[i].qcount_tx,
3586 					    vsi->tc_cfg.tc_info[i].qoffset);
3587 	/* setup TC queue map for CHNL TCs */
3588 	ice_for_each_chnl_tc(i) {
3589 		if (!(vsi->all_enatc & BIT(i)))
3590 			break;
3591 		if (!vsi->mqprio_qopt.qopt.count[i])
3592 			break;
3593 		netdev_set_tc_queue(netdev, i,
3594 				    vsi->mqprio_qopt.qopt.count[i],
3595 				    vsi->mqprio_qopt.qopt.offset[i]);
3596 	}
3597 
3598 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3599 		return;
3600 
3601 	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3602 		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3603 
3604 		/* Get the mapped netdev TC# for the UP */
3605 		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3606 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3607 	}
3608 }
3609 
3610 /**
3611  * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3612  * @vsi: the VSI being configured,
3613  * @ctxt: VSI context structure
3614  * @ena_tc: number of traffic classes to enable
3615  *
3616  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3617  */
3618 static void
3619 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3620 			   u8 ena_tc)
3621 {
3622 	u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3623 	u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3624 	int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3625 	u8 netdev_tc = 0;
3626 	int i;
3627 
3628 	vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3629 
3630 	pow = order_base_2(tc0_qcount);
3631 	qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3632 		ICE_AQ_VSI_TC_Q_OFFSET_M) |
3633 		((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3634 
3635 	ice_for_each_traffic_class(i) {
3636 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3637 			/* TC is not enabled */
3638 			vsi->tc_cfg.tc_info[i].qoffset = 0;
3639 			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3640 			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3641 			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3642 			ctxt->info.tc_mapping[i] = 0;
3643 			continue;
3644 		}
3645 
3646 		offset = vsi->mqprio_qopt.qopt.offset[i];
3647 		qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3648 		qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3649 		vsi->tc_cfg.tc_info[i].qoffset = offset;
3650 		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3651 		vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3652 		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3653 	}
3654 
3655 	if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3656 		ice_for_each_chnl_tc(i) {
3657 			if (!(vsi->all_enatc & BIT(i)))
3658 				continue;
3659 			offset = vsi->mqprio_qopt.qopt.offset[i];
3660 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3661 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3662 		}
3663 	}
3664 
3665 	/* Set actual Tx/Rx queue pairs */
3666 	vsi->num_txq = offset + qcount_tx;
3667 	vsi->num_rxq = offset + qcount_rx;
3668 
3669 	/* Setup queue TC[0].qmap for given VSI context */
3670 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3671 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3672 	ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3673 
3674 	/* Find queue count available for channel VSIs and starting offset
3675 	 * for channel VSIs
3676 	 */
3677 	if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3678 		vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3679 		vsi->next_base_q = tc0_qcount;
3680 	}
3681 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n",  vsi->num_txq);
3682 	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n",  vsi->num_rxq);
3683 	dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3684 		vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3685 }
3686 
3687 /**
3688  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3689  * @vsi: VSI to be configured
3690  * @ena_tc: TC bitmap
3691  *
3692  * VSI queues expected to be quiesced before calling this function
3693  */
3694 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3695 {
3696 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3697 	struct ice_pf *pf = vsi->back;
3698 	struct ice_vsi_ctx *ctx;
3699 	struct device *dev;
3700 	int i, ret = 0;
3701 	u8 num_tc = 0;
3702 
3703 	dev = ice_pf_to_dev(pf);
3704 	if (vsi->tc_cfg.ena_tc == ena_tc &&
3705 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3706 		return ret;
3707 
3708 	ice_for_each_traffic_class(i) {
3709 		/* build bitmap of enabled TCs */
3710 		if (ena_tc & BIT(i))
3711 			num_tc++;
3712 		/* populate max_txqs per TC */
3713 		max_txqs[i] = vsi->alloc_txq;
3714 		/* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3715 		 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3716 		 */
3717 		if (vsi->type == ICE_VSI_CHNL &&
3718 		    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3719 			max_txqs[i] = vsi->num_txq;
3720 	}
3721 
3722 	vsi->tc_cfg.ena_tc = ena_tc;
3723 	vsi->tc_cfg.numtc = num_tc;
3724 
3725 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3726 	if (!ctx)
3727 		return -ENOMEM;
3728 
3729 	ctx->vf_num = 0;
3730 	ctx->info = vsi->info;
3731 
3732 	if (vsi->type == ICE_VSI_PF &&
3733 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3734 		ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3735 	else
3736 		ice_vsi_setup_q_map(vsi, ctx);
3737 
3738 	/* must to indicate which section of VSI context are being modified */
3739 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3740 	ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3741 	if (ret) {
3742 		dev_info(dev, "Failed VSI Update\n");
3743 		goto out;
3744 	}
3745 
3746 	if (vsi->type == ICE_VSI_PF &&
3747 	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3748 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3749 	else
3750 		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3751 				      vsi->tc_cfg.ena_tc, max_txqs);
3752 
3753 	if (ret) {
3754 		dev_err(dev, "VSI %d failed TC config, error %d\n",
3755 			vsi->vsi_num, ret);
3756 		goto out;
3757 	}
3758 	ice_vsi_update_q_map(vsi, ctx);
3759 	vsi->info.valid_sections = 0;
3760 
3761 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3762 out:
3763 	kfree(ctx);
3764 	return ret;
3765 }
3766 
3767 /**
3768  * ice_update_ring_stats - Update ring statistics
3769  * @stats: stats to be updated
3770  * @pkts: number of processed packets
3771  * @bytes: number of processed bytes
3772  *
3773  * This function assumes that caller has acquired a u64_stats_sync lock.
3774  */
3775 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3776 {
3777 	stats->bytes += bytes;
3778 	stats->pkts += pkts;
3779 }
3780 
3781 /**
3782  * ice_update_tx_ring_stats - Update Tx ring specific counters
3783  * @tx_ring: ring to update
3784  * @pkts: number of processed packets
3785  * @bytes: number of processed bytes
3786  */
3787 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3788 {
3789 	u64_stats_update_begin(&tx_ring->syncp);
3790 	ice_update_ring_stats(&tx_ring->stats, pkts, bytes);
3791 	u64_stats_update_end(&tx_ring->syncp);
3792 }
3793 
3794 /**
3795  * ice_update_rx_ring_stats - Update Rx ring specific counters
3796  * @rx_ring: ring to update
3797  * @pkts: number of processed packets
3798  * @bytes: number of processed bytes
3799  */
3800 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3801 {
3802 	u64_stats_update_begin(&rx_ring->syncp);
3803 	ice_update_ring_stats(&rx_ring->stats, pkts, bytes);
3804 	u64_stats_update_end(&rx_ring->syncp);
3805 }
3806 
3807 /**
3808  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3809  * @sw: switch to check if its default forwarding VSI is free
3810  *
3811  * Return true if the default forwarding VSI is already being used, else returns
3812  * false signalling that it's available to use.
3813  */
3814 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3815 {
3816 	return (sw->dflt_vsi && sw->dflt_vsi_ena);
3817 }
3818 
3819 /**
3820  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3821  * @sw: switch for the default forwarding VSI to compare against
3822  * @vsi: VSI to compare against default forwarding VSI
3823  *
3824  * If this VSI passed in is the default forwarding VSI then return true, else
3825  * return false
3826  */
3827 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3828 {
3829 	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3830 }
3831 
3832 /**
3833  * ice_set_dflt_vsi - set the default forwarding VSI
3834  * @sw: switch used to assign the default forwarding VSI
3835  * @vsi: VSI getting set as the default forwarding VSI on the switch
3836  *
3837  * If the VSI passed in is already the default VSI and it's enabled just return
3838  * success.
3839  *
3840  * If there is already a default VSI on the switch and it's enabled then return
3841  * -EEXIST since there can only be one default VSI per switch.
3842  *
3843  *  Otherwise try to set the VSI passed in as the switch's default VSI and
3844  *  return the result.
3845  */
3846 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3847 {
3848 	struct device *dev;
3849 	int status;
3850 
3851 	if (!sw || !vsi)
3852 		return -EINVAL;
3853 
3854 	dev = ice_pf_to_dev(vsi->back);
3855 
3856 	/* the VSI passed in is already the default VSI */
3857 	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3858 		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3859 			vsi->vsi_num);
3860 		return 0;
3861 	}
3862 
3863 	/* another VSI is already the default VSI for this switch */
3864 	if (ice_is_dflt_vsi_in_use(sw)) {
3865 		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3866 			sw->dflt_vsi->vsi_num);
3867 		return -EEXIST;
3868 	}
3869 
3870 	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3871 	if (status) {
3872 		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3873 			vsi->vsi_num, status);
3874 		return status;
3875 	}
3876 
3877 	sw->dflt_vsi = vsi;
3878 	sw->dflt_vsi_ena = true;
3879 
3880 	return 0;
3881 }
3882 
3883 /**
3884  * ice_clear_dflt_vsi - clear the default forwarding VSI
3885  * @sw: switch used to clear the default VSI
3886  *
3887  * If the switch has no default VSI or it's not enabled then return error.
3888  *
3889  * Otherwise try to clear the default VSI and return the result.
3890  */
3891 int ice_clear_dflt_vsi(struct ice_sw *sw)
3892 {
3893 	struct ice_vsi *dflt_vsi;
3894 	struct device *dev;
3895 	int status;
3896 
3897 	if (!sw)
3898 		return -EINVAL;
3899 
3900 	dev = ice_pf_to_dev(sw->pf);
3901 
3902 	dflt_vsi = sw->dflt_vsi;
3903 
3904 	/* there is no default VSI configured */
3905 	if (!ice_is_dflt_vsi_in_use(sw))
3906 		return -ENODEV;
3907 
3908 	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3909 				  ICE_FLTR_RX);
3910 	if (status) {
3911 		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3912 			dflt_vsi->vsi_num, status);
3913 		return -EIO;
3914 	}
3915 
3916 	sw->dflt_vsi = NULL;
3917 	sw->dflt_vsi_ena = false;
3918 
3919 	return 0;
3920 }
3921 
3922 /**
3923  * ice_get_link_speed_mbps - get link speed in Mbps
3924  * @vsi: the VSI whose link speed is being queried
3925  *
3926  * Return current VSI link speed and 0 if the speed is unknown.
3927  */
3928 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3929 {
3930 	switch (vsi->port_info->phy.link_info.link_speed) {
3931 	case ICE_AQ_LINK_SPEED_100GB:
3932 		return SPEED_100000;
3933 	case ICE_AQ_LINK_SPEED_50GB:
3934 		return SPEED_50000;
3935 	case ICE_AQ_LINK_SPEED_40GB:
3936 		return SPEED_40000;
3937 	case ICE_AQ_LINK_SPEED_25GB:
3938 		return SPEED_25000;
3939 	case ICE_AQ_LINK_SPEED_20GB:
3940 		return SPEED_20000;
3941 	case ICE_AQ_LINK_SPEED_10GB:
3942 		return SPEED_10000;
3943 	case ICE_AQ_LINK_SPEED_5GB:
3944 		return SPEED_5000;
3945 	case ICE_AQ_LINK_SPEED_2500MB:
3946 		return SPEED_2500;
3947 	case ICE_AQ_LINK_SPEED_1000MB:
3948 		return SPEED_1000;
3949 	case ICE_AQ_LINK_SPEED_100MB:
3950 		return SPEED_100;
3951 	case ICE_AQ_LINK_SPEED_10MB:
3952 		return SPEED_10;
3953 	case ICE_AQ_LINK_SPEED_UNKNOWN:
3954 	default:
3955 		return 0;
3956 	}
3957 }
3958 
3959 /**
3960  * ice_get_link_speed_kbps - get link speed in Kbps
3961  * @vsi: the VSI whose link speed is being queried
3962  *
3963  * Return current VSI link speed and 0 if the speed is unknown.
3964  */
3965 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3966 {
3967 	int speed_mbps;
3968 
3969 	speed_mbps = ice_get_link_speed_mbps(vsi);
3970 
3971 	return speed_mbps * 1000;
3972 }
3973 
3974 /**
3975  * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3976  * @vsi: VSI to be configured
3977  * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3978  *
3979  * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3980  * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3981  * on TC 0.
3982  */
3983 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3984 {
3985 	struct ice_pf *pf = vsi->back;
3986 	struct device *dev;
3987 	int status;
3988 	int speed;
3989 
3990 	dev = ice_pf_to_dev(pf);
3991 	if (!vsi->port_info) {
3992 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3993 			vsi->idx, vsi->type);
3994 		return -EINVAL;
3995 	}
3996 
3997 	speed = ice_get_link_speed_kbps(vsi);
3998 	if (min_tx_rate > (u64)speed) {
3999 		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
4000 			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
4001 			speed);
4002 		return -EINVAL;
4003 	}
4004 
4005 	/* Configure min BW for VSI limit */
4006 	if (min_tx_rate) {
4007 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
4008 						   ICE_MIN_BW, min_tx_rate);
4009 		if (status) {
4010 			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
4011 				min_tx_rate, ice_vsi_type_str(vsi->type),
4012 				vsi->idx);
4013 			return status;
4014 		}
4015 
4016 		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
4017 			min_tx_rate, ice_vsi_type_str(vsi->type));
4018 	} else {
4019 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
4020 							vsi->idx, 0,
4021 							ICE_MIN_BW);
4022 		if (status) {
4023 			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
4024 				ice_vsi_type_str(vsi->type), vsi->idx);
4025 			return status;
4026 		}
4027 
4028 		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
4029 			ice_vsi_type_str(vsi->type), vsi->idx);
4030 	}
4031 
4032 	return 0;
4033 }
4034 
4035 /**
4036  * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
4037  * @vsi: VSI to be configured
4038  * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
4039  *
4040  * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
4041  * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
4042  * on TC 0.
4043  */
4044 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
4045 {
4046 	struct ice_pf *pf = vsi->back;
4047 	struct device *dev;
4048 	int status;
4049 	int speed;
4050 
4051 	dev = ice_pf_to_dev(pf);
4052 	if (!vsi->port_info) {
4053 		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
4054 			vsi->idx, vsi->type);
4055 		return -EINVAL;
4056 	}
4057 
4058 	speed = ice_get_link_speed_kbps(vsi);
4059 	if (max_tx_rate > (u64)speed) {
4060 		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
4061 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
4062 			speed);
4063 		return -EINVAL;
4064 	}
4065 
4066 	/* Configure max BW for VSI limit */
4067 	if (max_tx_rate) {
4068 		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
4069 						   ICE_MAX_BW, max_tx_rate);
4070 		if (status) {
4071 			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
4072 				max_tx_rate, ice_vsi_type_str(vsi->type),
4073 				vsi->idx);
4074 			return status;
4075 		}
4076 
4077 		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
4078 			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
4079 	} else {
4080 		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
4081 							vsi->idx, 0,
4082 							ICE_MAX_BW);
4083 		if (status) {
4084 			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
4085 				ice_vsi_type_str(vsi->type), vsi->idx);
4086 			return status;
4087 		}
4088 
4089 		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
4090 			ice_vsi_type_str(vsi->type), vsi->idx);
4091 	}
4092 
4093 	return 0;
4094 }
4095 
4096 /**
4097  * ice_set_link - turn on/off physical link
4098  * @vsi: VSI to modify physical link on
4099  * @ena: turn on/off physical link
4100  */
4101 int ice_set_link(struct ice_vsi *vsi, bool ena)
4102 {
4103 	struct device *dev = ice_pf_to_dev(vsi->back);
4104 	struct ice_port_info *pi = vsi->port_info;
4105 	struct ice_hw *hw = pi->hw;
4106 	int status;
4107 
4108 	if (vsi->type != ICE_VSI_PF)
4109 		return -EINVAL;
4110 
4111 	status = ice_aq_set_link_restart_an(pi, ena, NULL);
4112 
4113 	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
4114 	 * this is not a fatal error, so print a warning message and return
4115 	 * a success code. Return an error if FW returns an error code other
4116 	 * than ICE_AQ_RC_EMODE
4117 	 */
4118 	if (status == -EIO) {
4119 		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
4120 			dev_warn(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
4121 				 (ena ? "ON" : "OFF"), status,
4122 				 ice_aq_str(hw->adminq.sq_last_status));
4123 	} else if (status) {
4124 		dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
4125 			(ena ? "ON" : "OFF"), status,
4126 			ice_aq_str(hw->adminq.sq_last_status));
4127 		return status;
4128 	}
4129 
4130 	return 0;
4131 }
4132 
4133 /**
4134  * ice_is_feature_supported
4135  * @pf: pointer to the struct ice_pf instance
4136  * @f: feature enum to be checked
4137  *
4138  * returns true if feature is supported, false otherwise
4139  */
4140 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
4141 {
4142 	if (f < 0 || f >= ICE_F_MAX)
4143 		return false;
4144 
4145 	return test_bit(f, pf->features);
4146 }
4147 
4148 /**
4149  * ice_set_feature_support
4150  * @pf: pointer to the struct ice_pf instance
4151  * @f: feature enum to set
4152  */
4153 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4154 {
4155 	if (f < 0 || f >= ICE_F_MAX)
4156 		return;
4157 
4158 	set_bit(f, pf->features);
4159 }
4160 
4161 /**
4162  * ice_clear_feature_support
4163  * @pf: pointer to the struct ice_pf instance
4164  * @f: feature enum to clear
4165  */
4166 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4167 {
4168 	if (f < 0 || f >= ICE_F_MAX)
4169 		return;
4170 
4171 	clear_bit(f, pf->features);
4172 }
4173 
4174 /**
4175  * ice_init_feature_support
4176  * @pf: pointer to the struct ice_pf instance
4177  *
4178  * called during init to setup supported feature
4179  */
4180 void ice_init_feature_support(struct ice_pf *pf)
4181 {
4182 	switch (pf->hw.device_id) {
4183 	case ICE_DEV_ID_E810C_BACKPLANE:
4184 	case ICE_DEV_ID_E810C_QSFP:
4185 	case ICE_DEV_ID_E810C_SFP:
4186 		ice_set_feature_support(pf, ICE_F_DSCP);
4187 		if (ice_is_e810t(&pf->hw))
4188 			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4189 		break;
4190 	default:
4191 		break;
4192 	}
4193 }
4194 
4195 /**
4196  * ice_vsi_update_security - update security block in VSI
4197  * @vsi: pointer to VSI structure
4198  * @fill: function pointer to fill ctx
4199  */
4200 int
4201 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4202 {
4203 	struct ice_vsi_ctx ctx = { 0 };
4204 
4205 	ctx.info = vsi->info;
4206 	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4207 	fill(&ctx);
4208 
4209 	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4210 		return -ENODEV;
4211 
4212 	vsi->info = ctx.info;
4213 	return 0;
4214 }
4215 
4216 /**
4217  * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4218  * @ctx: pointer to VSI ctx structure
4219  */
4220 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4221 {
4222 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4223 			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4224 				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4225 }
4226 
4227 /**
4228  * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4229  * @ctx: pointer to VSI ctx structure
4230  */
4231 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4232 {
4233 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4234 			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4235 				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4236 }
4237 
4238 /**
4239  * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4240  * @ctx: pointer to VSI ctx structure
4241  */
4242 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4243 {
4244 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4245 }
4246 
4247 /**
4248  * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4249  * @ctx: pointer to VSI ctx structure
4250  */
4251 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4252 {
4253 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4254 }
4255