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