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