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