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