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