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