xref: /linux/drivers/net/ethernet/intel/ice/ice_lib.c (revision ea52a0b58e41c3b2b9e97ff13fe0da9c9e430ea8)
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 	for (i = 0; i < vsi->num_rxq; i++)
50 		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
51 
52 	ice_flush(&vsi->back->hw);
53 
54 	for (i = 0; i < vsi->num_rxq; 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.ring)
394 		return IRQ_HANDLED;
395 
396 #define FDIR_RX_DESC_CLEAN_BUDGET 64
397 	ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
398 	ice_clean_ctrl_tx_irq(q_vector->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.ring && !q_vector->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.ring && !q_vector->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 	for (i = 0; i < vsi->alloc_txq; i++) {
643 		clear_bit(vsi->txq_map[i], pf->avail_txqs);
644 		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
645 	}
646 
647 	for (i = 0; i < vsi->alloc_rxq; 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.ring = NULL;
1295 				q_vector->rx.ring = NULL;
1296 			}
1297 		}
1298 	}
1299 
1300 	if (vsi->tx_rings) {
1301 		for (i = 0; i < vsi->alloc_txq; 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 		for (i = 0; i < vsi->alloc_rxq; 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 	for (i = 0; i < vsi->alloc_txq; i++) {
1331 		struct ice_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->ring_active = false;
1342 		ring->vsi = vsi;
1343 		ring->tx_tstamps = &pf->ptp.port.tx;
1344 		ring->dev = dev;
1345 		ring->count = vsi->num_tx_desc;
1346 		WRITE_ONCE(vsi->tx_rings[i], ring);
1347 	}
1348 
1349 	/* Allocate Rx rings */
1350 	for (i = 0; i < vsi->alloc_rxq; i++) {
1351 		struct ice_ring *ring;
1352 
1353 		/* allocate with kzalloc(), free with kfree_rcu() */
1354 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1355 		if (!ring)
1356 			goto err_out;
1357 
1358 		ring->q_index = i;
1359 		ring->reg_idx = vsi->rxq_map[i];
1360 		ring->ring_active = false;
1361 		ring->vsi = vsi;
1362 		ring->netdev = vsi->netdev;
1363 		ring->dev = dev;
1364 		ring->count = vsi->num_rx_desc;
1365 		WRITE_ONCE(vsi->rx_rings[i], ring);
1366 	}
1367 
1368 	return 0;
1369 
1370 err_out:
1371 	ice_vsi_clear_rings(vsi);
1372 	return -ENOMEM;
1373 }
1374 
1375 /**
1376  * ice_vsi_manage_rss_lut - disable/enable RSS
1377  * @vsi: the VSI being changed
1378  * @ena: boolean value indicating if this is an enable or disable request
1379  *
1380  * In the event of disable request for RSS, this function will zero out RSS
1381  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1382  * LUT.
1383  */
1384 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1385 {
1386 	u8 *lut;
1387 
1388 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1389 	if (!lut)
1390 		return;
1391 
1392 	if (ena) {
1393 		if (vsi->rss_lut_user)
1394 			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1395 		else
1396 			ice_fill_rss_lut(lut, vsi->rss_table_size,
1397 					 vsi->rss_size);
1398 	}
1399 
1400 	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1401 	kfree(lut);
1402 }
1403 
1404 /**
1405  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1406  * @vsi: VSI to be configured
1407  */
1408 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1409 {
1410 	struct ice_pf *pf = vsi->back;
1411 	struct device *dev;
1412 	u8 *lut, *key;
1413 	int err;
1414 
1415 	dev = ice_pf_to_dev(pf);
1416 	vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1417 
1418 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1419 	if (!lut)
1420 		return -ENOMEM;
1421 
1422 	if (vsi->rss_lut_user)
1423 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1424 	else
1425 		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1426 
1427 	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1428 	if (err) {
1429 		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1430 		goto ice_vsi_cfg_rss_exit;
1431 	}
1432 
1433 	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1434 	if (!key) {
1435 		err = -ENOMEM;
1436 		goto ice_vsi_cfg_rss_exit;
1437 	}
1438 
1439 	if (vsi->rss_hkey_user)
1440 		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1441 	else
1442 		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1443 
1444 	err = ice_set_rss_key(vsi, key);
1445 	if (err)
1446 		dev_err(dev, "set_rss_key failed, error %d\n", err);
1447 
1448 	kfree(key);
1449 ice_vsi_cfg_rss_exit:
1450 	kfree(lut);
1451 	return err;
1452 }
1453 
1454 /**
1455  * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1456  * @vsi: VSI to be configured
1457  *
1458  * This function will only be called during the VF VSI setup. Upon successful
1459  * completion of package download, this function will configure default RSS
1460  * input sets for VF VSI.
1461  */
1462 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1463 {
1464 	struct ice_pf *pf = vsi->back;
1465 	enum ice_status status;
1466 	struct device *dev;
1467 
1468 	dev = ice_pf_to_dev(pf);
1469 	if (ice_is_safe_mode(pf)) {
1470 		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1471 			vsi->vsi_num);
1472 		return;
1473 	}
1474 
1475 	status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1476 	if (status)
1477 		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1478 			vsi->vsi_num, ice_stat_str(status));
1479 }
1480 
1481 /**
1482  * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1483  * @vsi: VSI to be configured
1484  *
1485  * This function will only be called after successful download package call
1486  * during initialization of PF. Since the downloaded package will erase the
1487  * RSS section, this function will configure RSS input sets for different
1488  * flow types. The last profile added has the highest priority, therefore 2
1489  * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1490  * (i.e. IPv4 src/dst TCP src/dst port).
1491  */
1492 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1493 {
1494 	u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1495 	struct ice_pf *pf = vsi->back;
1496 	struct ice_hw *hw = &pf->hw;
1497 	enum ice_status status;
1498 	struct device *dev;
1499 
1500 	dev = ice_pf_to_dev(pf);
1501 	if (ice_is_safe_mode(pf)) {
1502 		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1503 			vsi_num);
1504 		return;
1505 	}
1506 	/* configure RSS for IPv4 with input set IP src/dst */
1507 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1508 				 ICE_FLOW_SEG_HDR_IPV4);
1509 	if (status)
1510 		dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1511 			vsi_num, ice_stat_str(status));
1512 
1513 	/* configure RSS for IPv6 with input set IPv6 src/dst */
1514 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1515 				 ICE_FLOW_SEG_HDR_IPV6);
1516 	if (status)
1517 		dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1518 			vsi_num, ice_stat_str(status));
1519 
1520 	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1521 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1522 				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1523 	if (status)
1524 		dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1525 			vsi_num, ice_stat_str(status));
1526 
1527 	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1528 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1529 				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1530 	if (status)
1531 		dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1532 			vsi_num, ice_stat_str(status));
1533 
1534 	/* configure RSS for sctp4 with input set IP src/dst */
1535 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1536 				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1537 	if (status)
1538 		dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1539 			vsi_num, ice_stat_str(status));
1540 
1541 	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1542 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1543 				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1544 	if (status)
1545 		dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1546 			vsi_num, ice_stat_str(status));
1547 
1548 	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1549 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1550 				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1551 	if (status)
1552 		dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1553 			vsi_num, ice_stat_str(status));
1554 
1555 	/* configure RSS for sctp6 with input set IPv6 src/dst */
1556 	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1557 				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1558 	if (status)
1559 		dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1560 			vsi_num, ice_stat_str(status));
1561 }
1562 
1563 /**
1564  * ice_pf_state_is_nominal - checks the PF for nominal state
1565  * @pf: pointer to PF to check
1566  *
1567  * Check the PF's state for a collection of bits that would indicate
1568  * the PF is in a state that would inhibit normal operation for
1569  * driver functionality.
1570  *
1571  * Returns true if PF is in a nominal state, false otherwise
1572  */
1573 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1574 {
1575 	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1576 
1577 	if (!pf)
1578 		return false;
1579 
1580 	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1581 	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1582 		return false;
1583 
1584 	return true;
1585 }
1586 
1587 /**
1588  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1589  * @vsi: the VSI to be updated
1590  */
1591 void ice_update_eth_stats(struct ice_vsi *vsi)
1592 {
1593 	struct ice_eth_stats *prev_es, *cur_es;
1594 	struct ice_hw *hw = &vsi->back->hw;
1595 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1596 
1597 	prev_es = &vsi->eth_stats_prev;
1598 	cur_es = &vsi->eth_stats;
1599 
1600 	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1601 			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1602 
1603 	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1604 			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1605 
1606 	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1607 			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1608 
1609 	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1610 			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1611 
1612 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1613 			  &prev_es->rx_discards, &cur_es->rx_discards);
1614 
1615 	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1616 			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1617 
1618 	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1619 			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1620 
1621 	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1622 			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1623 
1624 	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1625 			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1626 
1627 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1628 			  &prev_es->tx_errors, &cur_es->tx_errors);
1629 
1630 	vsi->stat_offsets_loaded = true;
1631 }
1632 
1633 /**
1634  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1635  * @vsi: the VSI being configured
1636  * @vid: VLAN ID to be added
1637  * @action: filter action to be performed on match
1638  */
1639 int
1640 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1641 {
1642 	struct ice_pf *pf = vsi->back;
1643 	struct device *dev;
1644 	int err = 0;
1645 
1646 	dev = ice_pf_to_dev(pf);
1647 
1648 	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1649 		vsi->num_vlan++;
1650 	} else {
1651 		err = -ENODEV;
1652 		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1653 			vsi->vsi_num);
1654 	}
1655 
1656 	return err;
1657 }
1658 
1659 /**
1660  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1661  * @vsi: the VSI being configured
1662  * @vid: VLAN ID to be removed
1663  *
1664  * Returns 0 on success and negative on failure
1665  */
1666 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1667 {
1668 	struct ice_pf *pf = vsi->back;
1669 	enum ice_status status;
1670 	struct device *dev;
1671 	int err = 0;
1672 
1673 	dev = ice_pf_to_dev(pf);
1674 
1675 	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1676 	if (!status) {
1677 		vsi->num_vlan--;
1678 	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1679 		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1680 			vid, vsi->vsi_num, ice_stat_str(status));
1681 	} else {
1682 		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1683 			vid, vsi->vsi_num, ice_stat_str(status));
1684 		err = -EIO;
1685 	}
1686 
1687 	return err;
1688 }
1689 
1690 /**
1691  * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1692  * @vsi: VSI
1693  */
1694 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1695 {
1696 	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1697 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1698 		vsi->rx_buf_len = ICE_RXBUF_2048;
1699 #if (PAGE_SIZE < 8192)
1700 	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1701 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1702 		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1703 		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1704 #endif
1705 	} else {
1706 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1707 #if (PAGE_SIZE < 8192)
1708 		vsi->rx_buf_len = ICE_RXBUF_3072;
1709 #else
1710 		vsi->rx_buf_len = ICE_RXBUF_2048;
1711 #endif
1712 	}
1713 }
1714 
1715 /**
1716  * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1717  * @hw: HW pointer
1718  * @pf_q: index of the Rx queue in the PF's queue space
1719  * @rxdid: flexible descriptor RXDID
1720  * @prio: priority for the RXDID for this queue
1721  * @ena_ts: true to enable timestamp and false to disable timestamp
1722  */
1723 void
1724 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1725 			bool ena_ts)
1726 {
1727 	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1728 
1729 	/* clear any previous values */
1730 	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1731 		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1732 		    QRXFLXP_CNTXT_TS_M);
1733 
1734 	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1735 		QRXFLXP_CNTXT_RXDID_IDX_M;
1736 
1737 	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1738 		QRXFLXP_CNTXT_RXDID_PRIO_M;
1739 
1740 	if (ena_ts)
1741 		/* Enable TimeSync on this queue */
1742 		regval |= QRXFLXP_CNTXT_TS_M;
1743 
1744 	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1745 }
1746 
1747 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1748 {
1749 	if (q_idx >= vsi->num_rxq)
1750 		return -EINVAL;
1751 
1752 	return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1753 }
1754 
1755 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_ring **tx_rings, u16 q_idx)
1756 {
1757 	struct ice_aqc_add_tx_qgrp *qg_buf;
1758 	int err;
1759 
1760 	if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1761 		return -EINVAL;
1762 
1763 	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1764 	if (!qg_buf)
1765 		return -ENOMEM;
1766 
1767 	qg_buf->num_txqs = 1;
1768 
1769 	err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1770 	kfree(qg_buf);
1771 	return err;
1772 }
1773 
1774 /**
1775  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1776  * @vsi: the VSI being configured
1777  *
1778  * Return 0 on success and a negative value on error
1779  * Configure the Rx VSI for operation.
1780  */
1781 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1782 {
1783 	u16 i;
1784 
1785 	if (vsi->type == ICE_VSI_VF)
1786 		goto setup_rings;
1787 
1788 	ice_vsi_cfg_frame_size(vsi);
1789 setup_rings:
1790 	/* set up individual rings */
1791 	ice_for_each_rxq(vsi, i) {
1792 		int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1793 
1794 		if (err)
1795 			return err;
1796 	}
1797 
1798 	return 0;
1799 }
1800 
1801 /**
1802  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1803  * @vsi: the VSI being configured
1804  * @rings: Tx ring array to be configured
1805  * @count: number of Tx ring array elements
1806  *
1807  * Return 0 on success and a negative value on error
1808  * Configure the Tx VSI for operation.
1809  */
1810 static int
1811 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, u16 count)
1812 {
1813 	struct ice_aqc_add_tx_qgrp *qg_buf;
1814 	u16 q_idx = 0;
1815 	int err = 0;
1816 
1817 	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1818 	if (!qg_buf)
1819 		return -ENOMEM;
1820 
1821 	qg_buf->num_txqs = 1;
1822 
1823 	for (q_idx = 0; q_idx < count; q_idx++) {
1824 		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1825 		if (err)
1826 			goto err_cfg_txqs;
1827 	}
1828 
1829 err_cfg_txqs:
1830 	kfree(qg_buf);
1831 	return err;
1832 }
1833 
1834 /**
1835  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1836  * @vsi: the VSI being configured
1837  *
1838  * Return 0 on success and a negative value on error
1839  * Configure the Tx VSI for operation.
1840  */
1841 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1842 {
1843 	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1844 }
1845 
1846 /**
1847  * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1848  * @vsi: the VSI being configured
1849  *
1850  * Return 0 on success and a negative value on error
1851  * Configure the Tx queues dedicated for XDP in given VSI for operation.
1852  */
1853 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1854 {
1855 	int ret;
1856 	int i;
1857 
1858 	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1859 	if (ret)
1860 		return ret;
1861 
1862 	for (i = 0; i < vsi->num_xdp_txq; i++)
1863 		vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1864 
1865 	return ret;
1866 }
1867 
1868 /**
1869  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1870  * @intrl: interrupt rate limit in usecs
1871  * @gran: interrupt rate limit granularity in usecs
1872  *
1873  * This function converts a decimal interrupt rate limit in usecs to the format
1874  * expected by firmware.
1875  */
1876 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1877 {
1878 	u32 val = intrl / gran;
1879 
1880 	if (val)
1881 		return val | GLINT_RATE_INTRL_ENA_M;
1882 	return 0;
1883 }
1884 
1885 /**
1886  * ice_write_intrl - write throttle rate limit to interrupt specific register
1887  * @q_vector: pointer to interrupt specific structure
1888  * @intrl: throttle rate limit in microseconds to write
1889  */
1890 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1891 {
1892 	struct ice_hw *hw = &q_vector->vsi->back->hw;
1893 
1894 	wr32(hw, GLINT_RATE(q_vector->reg_idx),
1895 	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1896 }
1897 
1898 /**
1899  * __ice_write_itr - write throttle rate to register
1900  * @q_vector: pointer to interrupt data structure
1901  * @rc: pointer to ring container
1902  * @itr: throttle rate in microseconds to write
1903  */
1904 static void __ice_write_itr(struct ice_q_vector *q_vector,
1905 			    struct ice_ring_container *rc, u16 itr)
1906 {
1907 	struct ice_hw *hw = &q_vector->vsi->back->hw;
1908 
1909 	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1910 	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1911 }
1912 
1913 /**
1914  * ice_write_itr - write throttle rate to queue specific register
1915  * @rc: pointer to ring container
1916  * @itr: throttle rate in microseconds to write
1917  */
1918 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1919 {
1920 	struct ice_q_vector *q_vector;
1921 
1922 	if (!rc->ring)
1923 		return;
1924 
1925 	q_vector = rc->ring->q_vector;
1926 
1927 	__ice_write_itr(q_vector, rc, itr);
1928 }
1929 
1930 /**
1931  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1932  * @vsi: the VSI being configured
1933  *
1934  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1935  * for the VF VSI.
1936  */
1937 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1938 {
1939 	struct ice_pf *pf = vsi->back;
1940 	struct ice_hw *hw = &pf->hw;
1941 	u16 txq = 0, rxq = 0;
1942 	int i, q;
1943 
1944 	for (i = 0; i < vsi->num_q_vectors; i++) {
1945 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1946 		u16 reg_idx = q_vector->reg_idx;
1947 
1948 		ice_cfg_itr(hw, q_vector);
1949 
1950 		/* Both Transmit Queue Interrupt Cause Control register
1951 		 * and Receive Queue Interrupt Cause control register
1952 		 * expects MSIX_INDX field to be the vector index
1953 		 * within the function space and not the absolute
1954 		 * vector index across PF or across device.
1955 		 * For SR-IOV VF VSIs queue vector index always starts
1956 		 * with 1 since first vector index(0) is used for OICR
1957 		 * in VF space. Since VMDq and other PF VSIs are within
1958 		 * the PF function space, use the vector index that is
1959 		 * tracked for this PF.
1960 		 */
1961 		for (q = 0; q < q_vector->num_ring_tx; q++) {
1962 			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1963 					      q_vector->tx.itr_idx);
1964 			txq++;
1965 		}
1966 
1967 		for (q = 0; q < q_vector->num_ring_rx; q++) {
1968 			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1969 					      q_vector->rx.itr_idx);
1970 			rxq++;
1971 		}
1972 	}
1973 }
1974 
1975 /**
1976  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1977  * @vsi: the VSI being changed
1978  */
1979 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1980 {
1981 	struct ice_hw *hw = &vsi->back->hw;
1982 	struct ice_vsi_ctx *ctxt;
1983 	enum ice_status status;
1984 	int ret = 0;
1985 
1986 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1987 	if (!ctxt)
1988 		return -ENOMEM;
1989 
1990 	/* Here we are configuring the VSI to let the driver add VLAN tags by
1991 	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1992 	 * insertion happens in the Tx hot path, in ice_tx_map.
1993 	 */
1994 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1995 
1996 	/* Preserve existing VLAN strip setting */
1997 	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1998 				  ICE_AQ_VSI_VLAN_EMOD_M);
1999 
2000 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2001 
2002 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2003 	if (status) {
2004 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
2005 			ice_stat_str(status),
2006 			ice_aq_str(hw->adminq.sq_last_status));
2007 		ret = -EIO;
2008 		goto out;
2009 	}
2010 
2011 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2012 out:
2013 	kfree(ctxt);
2014 	return ret;
2015 }
2016 
2017 /**
2018  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
2019  * @vsi: the VSI being changed
2020  * @ena: boolean value indicating if this is a enable or disable request
2021  */
2022 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
2023 {
2024 	struct ice_hw *hw = &vsi->back->hw;
2025 	struct ice_vsi_ctx *ctxt;
2026 	enum ice_status status;
2027 	int ret = 0;
2028 
2029 	/* do not allow modifying VLAN stripping when a port VLAN is configured
2030 	 * on this VSI
2031 	 */
2032 	if (vsi->info.pvid)
2033 		return 0;
2034 
2035 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2036 	if (!ctxt)
2037 		return -ENOMEM;
2038 
2039 	/* Here we are configuring what the VSI should do with the VLAN tag in
2040 	 * the Rx packet. We can either leave the tag in the packet or put it in
2041 	 * the Rx descriptor.
2042 	 */
2043 	if (ena)
2044 		/* Strip VLAN tag from Rx packet and put it in the desc */
2045 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
2046 	else
2047 		/* Disable stripping. Leave tag in packet */
2048 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2049 
2050 	/* Allow all packets untagged/tagged */
2051 	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
2052 
2053 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2054 
2055 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2056 	if (status) {
2057 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
2058 			ena, ice_stat_str(status),
2059 			ice_aq_str(hw->adminq.sq_last_status));
2060 		ret = -EIO;
2061 		goto out;
2062 	}
2063 
2064 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2065 out:
2066 	kfree(ctxt);
2067 	return ret;
2068 }
2069 
2070 /**
2071  * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2072  * @vsi: the VSI whose rings are to be enabled
2073  *
2074  * Returns 0 on success and a negative value on error
2075  */
2076 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2077 {
2078 	return ice_vsi_ctrl_all_rx_rings(vsi, true);
2079 }
2080 
2081 /**
2082  * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2083  * @vsi: the VSI whose rings are to be disabled
2084  *
2085  * Returns 0 on success and a negative value on error
2086  */
2087 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2088 {
2089 	return ice_vsi_ctrl_all_rx_rings(vsi, false);
2090 }
2091 
2092 /**
2093  * ice_vsi_stop_tx_rings - Disable Tx rings
2094  * @vsi: the VSI being configured
2095  * @rst_src: reset source
2096  * @rel_vmvf_num: Relative ID of VF/VM
2097  * @rings: Tx ring array to be stopped
2098  * @count: number of Tx ring array elements
2099  */
2100 static int
2101 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2102 		      u16 rel_vmvf_num, struct ice_ring **rings, u16 count)
2103 {
2104 	u16 q_idx;
2105 
2106 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2107 		return -EINVAL;
2108 
2109 	for (q_idx = 0; q_idx < count; q_idx++) {
2110 		struct ice_txq_meta txq_meta = { };
2111 		int status;
2112 
2113 		if (!rings || !rings[q_idx])
2114 			return -EINVAL;
2115 
2116 		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2117 		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2118 					      rings[q_idx], &txq_meta);
2119 
2120 		if (status)
2121 			return status;
2122 	}
2123 
2124 	return 0;
2125 }
2126 
2127 /**
2128  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2129  * @vsi: the VSI being configured
2130  * @rst_src: reset source
2131  * @rel_vmvf_num: Relative ID of VF/VM
2132  */
2133 int
2134 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2135 			  u16 rel_vmvf_num)
2136 {
2137 	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2138 }
2139 
2140 /**
2141  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2142  * @vsi: the VSI being configured
2143  */
2144 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2145 {
2146 	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2147 }
2148 
2149 /**
2150  * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2151  * @vsi: VSI to check whether or not VLAN pruning is enabled.
2152  *
2153  * returns true if Rx VLAN pruning is enabled and false otherwise.
2154  */
2155 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2156 {
2157 	if (!vsi)
2158 		return false;
2159 
2160 	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2161 }
2162 
2163 /**
2164  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2165  * @vsi: VSI to enable or disable VLAN pruning on
2166  * @ena: set to true to enable VLAN pruning and false to disable it
2167  * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2168  *
2169  * returns 0 if VSI is updated, negative otherwise
2170  */
2171 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2172 {
2173 	struct ice_vsi_ctx *ctxt;
2174 	struct ice_pf *pf;
2175 	int status;
2176 
2177 	if (!vsi)
2178 		return -EINVAL;
2179 
2180 	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
2181 	 * mode. VLAN pruning will be enabled when the interface exits
2182 	 * promiscuous mode if any VLAN filters are active.
2183 	 */
2184 	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2185 		return 0;
2186 
2187 	pf = vsi->back;
2188 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2189 	if (!ctxt)
2190 		return -ENOMEM;
2191 
2192 	ctxt->info = vsi->info;
2193 
2194 	if (ena)
2195 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2196 	else
2197 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2198 
2199 	if (!vlan_promisc)
2200 		ctxt->info.valid_sections =
2201 			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2202 
2203 	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2204 	if (status) {
2205 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2206 			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2207 			   ice_stat_str(status),
2208 			   ice_aq_str(pf->hw.adminq.sq_last_status));
2209 		goto err_out;
2210 	}
2211 
2212 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2213 
2214 	kfree(ctxt);
2215 	return 0;
2216 
2217 err_out:
2218 	kfree(ctxt);
2219 	return -EIO;
2220 }
2221 
2222 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2223 {
2224 	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2225 
2226 	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2227 	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2228 }
2229 
2230 /**
2231  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2232  * @vsi: VSI to set the q_vectors register index on
2233  */
2234 static int
2235 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2236 {
2237 	u16 i;
2238 
2239 	if (!vsi || !vsi->q_vectors)
2240 		return -EINVAL;
2241 
2242 	ice_for_each_q_vector(vsi, i) {
2243 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2244 
2245 		if (!q_vector) {
2246 			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2247 				i, vsi->vsi_num);
2248 			goto clear_reg_idx;
2249 		}
2250 
2251 		if (vsi->type == ICE_VSI_VF) {
2252 			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2253 
2254 			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2255 		} else {
2256 			q_vector->reg_idx =
2257 				q_vector->v_idx + vsi->base_vector;
2258 		}
2259 	}
2260 
2261 	return 0;
2262 
2263 clear_reg_idx:
2264 	ice_for_each_q_vector(vsi, i) {
2265 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2266 
2267 		if (q_vector)
2268 			q_vector->reg_idx = 0;
2269 	}
2270 
2271 	return -EINVAL;
2272 }
2273 
2274 /**
2275  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2276  * @vsi: the VSI being configured
2277  * @tx: bool to determine Tx or Rx rule
2278  * @create: bool to determine create or remove Rule
2279  */
2280 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2281 {
2282 	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2283 				    enum ice_sw_fwd_act_type act);
2284 	struct ice_pf *pf = vsi->back;
2285 	enum ice_status status;
2286 	struct device *dev;
2287 
2288 	dev = ice_pf_to_dev(pf);
2289 	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2290 
2291 	if (tx) {
2292 		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2293 				  ICE_DROP_PACKET);
2294 	} else {
2295 		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2296 			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2297 							  create);
2298 		} else {
2299 			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2300 					  ICE_FWD_TO_VSI);
2301 		}
2302 	}
2303 
2304 	if (status)
2305 		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2306 			create ? "adding" : "removing", tx ? "TX" : "RX",
2307 			vsi->vsi_num, ice_stat_str(status));
2308 }
2309 
2310 /**
2311  * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2312  * @vsi: pointer to the VSI
2313  *
2314  * This function will allocate new scheduler aggregator now if needed and will
2315  * move specified VSI into it.
2316  */
2317 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2318 {
2319 	struct device *dev = ice_pf_to_dev(vsi->back);
2320 	struct ice_agg_node *agg_node_iter = NULL;
2321 	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2322 	struct ice_agg_node *agg_node = NULL;
2323 	int node_offset, max_agg_nodes = 0;
2324 	struct ice_port_info *port_info;
2325 	struct ice_pf *pf = vsi->back;
2326 	u32 agg_node_id_start = 0;
2327 	enum ice_status status;
2328 
2329 	/* create (as needed) scheduler aggregator node and move VSI into
2330 	 * corresponding aggregator node
2331 	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2332 	 * - VF aggregator nodes will contain VF VSI
2333 	 */
2334 	port_info = pf->hw.port_info;
2335 	if (!port_info)
2336 		return;
2337 
2338 	switch (vsi->type) {
2339 	case ICE_VSI_CTRL:
2340 	case ICE_VSI_LB:
2341 	case ICE_VSI_PF:
2342 	case ICE_VSI_SWITCHDEV_CTRL:
2343 		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2344 		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2345 		agg_node_iter = &pf->pf_agg_node[0];
2346 		break;
2347 	case ICE_VSI_VF:
2348 		/* user can create 'n' VFs on a given PF, but since max children
2349 		 * per aggregator node can be only 64. Following code handles
2350 		 * aggregator(s) for VF VSIs, either selects a agg_node which
2351 		 * was already created provided num_vsis < 64, otherwise
2352 		 * select next available node, which will be created
2353 		 */
2354 		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2355 		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2356 		agg_node_iter = &pf->vf_agg_node[0];
2357 		break;
2358 	default:
2359 		/* other VSI type, handle later if needed */
2360 		dev_dbg(dev, "unexpected VSI type %s\n",
2361 			ice_vsi_type_str(vsi->type));
2362 		return;
2363 	}
2364 
2365 	/* find the appropriate aggregator node */
2366 	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2367 		/* see if we can find space in previously created
2368 		 * node if num_vsis < 64, otherwise skip
2369 		 */
2370 		if (agg_node_iter->num_vsis &&
2371 		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2372 			agg_node_iter++;
2373 			continue;
2374 		}
2375 
2376 		if (agg_node_iter->valid &&
2377 		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2378 			agg_id = agg_node_iter->agg_id;
2379 			agg_node = agg_node_iter;
2380 			break;
2381 		}
2382 
2383 		/* find unclaimed agg_id */
2384 		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2385 			agg_id = node_offset + agg_node_id_start;
2386 			agg_node = agg_node_iter;
2387 			break;
2388 		}
2389 		/* move to next agg_node */
2390 		agg_node_iter++;
2391 	}
2392 
2393 	if (!agg_node)
2394 		return;
2395 
2396 	/* if selected aggregator node was not created, create it */
2397 	if (!agg_node->valid) {
2398 		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2399 				     (u8)vsi->tc_cfg.ena_tc);
2400 		if (status) {
2401 			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2402 				agg_id);
2403 			return;
2404 		}
2405 		/* aggregator node is created, store the neeeded info */
2406 		agg_node->valid = true;
2407 		agg_node->agg_id = agg_id;
2408 	}
2409 
2410 	/* move VSI to corresponding aggregator node */
2411 	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2412 				     (u8)vsi->tc_cfg.ena_tc);
2413 	if (status) {
2414 		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2415 			vsi->idx, agg_id);
2416 		return;
2417 	}
2418 
2419 	/* keep active children count for aggregator node */
2420 	agg_node->num_vsis++;
2421 
2422 	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2423 	 * to aggregator node
2424 	 */
2425 	vsi->agg_node = agg_node;
2426 	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2427 		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2428 		vsi->agg_node->num_vsis);
2429 }
2430 
2431 /**
2432  * ice_vsi_setup - Set up a VSI by a given type
2433  * @pf: board private structure
2434  * @pi: pointer to the port_info instance
2435  * @vsi_type: VSI type
2436  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2437  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2438  *         fill-in ICE_INVAL_VFID as input.
2439  *
2440  * This allocates the sw VSI structure and its queue resources.
2441  *
2442  * Returns pointer to the successfully allocated and configured VSI sw struct on
2443  * success, NULL on failure.
2444  */
2445 struct ice_vsi *
2446 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2447 	      enum ice_vsi_type vsi_type, u16 vf_id)
2448 {
2449 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2450 	struct device *dev = ice_pf_to_dev(pf);
2451 	enum ice_status status;
2452 	struct ice_vsi *vsi;
2453 	int ret, i;
2454 
2455 	if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2456 		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2457 	else
2458 		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2459 
2460 	if (!vsi) {
2461 		dev_err(dev, "could not allocate VSI\n");
2462 		return NULL;
2463 	}
2464 
2465 	vsi->port_info = pi;
2466 	vsi->vsw = pf->first_sw;
2467 	if (vsi->type == ICE_VSI_PF)
2468 		vsi->ethtype = ETH_P_PAUSE;
2469 
2470 	if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2471 		vsi->vf_id = vf_id;
2472 
2473 	ice_alloc_fd_res(vsi);
2474 
2475 	if (ice_vsi_get_qs(vsi)) {
2476 		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2477 			vsi->idx);
2478 		goto unroll_vsi_alloc;
2479 	}
2480 
2481 	/* set RSS capabilities */
2482 	ice_vsi_set_rss_params(vsi);
2483 
2484 	/* set TC configuration */
2485 	ice_vsi_set_tc_cfg(vsi);
2486 
2487 	/* create the VSI */
2488 	ret = ice_vsi_init(vsi, true);
2489 	if (ret)
2490 		goto unroll_get_qs;
2491 
2492 	switch (vsi->type) {
2493 	case ICE_VSI_CTRL:
2494 	case ICE_VSI_SWITCHDEV_CTRL:
2495 	case ICE_VSI_PF:
2496 		ret = ice_vsi_alloc_q_vectors(vsi);
2497 		if (ret)
2498 			goto unroll_vsi_init;
2499 
2500 		ret = ice_vsi_setup_vector_base(vsi);
2501 		if (ret)
2502 			goto unroll_alloc_q_vector;
2503 
2504 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2505 		if (ret)
2506 			goto unroll_vector_base;
2507 
2508 		ret = ice_vsi_alloc_rings(vsi);
2509 		if (ret)
2510 			goto unroll_vector_base;
2511 
2512 		/* Always add VLAN ID 0 switch rule by default. This is needed
2513 		 * in order to allow all untagged and 0 tagged priority traffic
2514 		 * if Rx VLAN pruning is enabled. Also there are cases where we
2515 		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2516 		 * so this handles those cases (i.e. adding the PF to a bridge
2517 		 * without the 8021q module loaded).
2518 		 */
2519 		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2520 		if (ret)
2521 			goto unroll_clear_rings;
2522 
2523 		ice_vsi_map_rings_to_vectors(vsi);
2524 
2525 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2526 		if (vsi->type != ICE_VSI_CTRL)
2527 			/* Do not exit if configuring RSS had an issue, at
2528 			 * least receive traffic on first queue. Hence no
2529 			 * need to capture return value
2530 			 */
2531 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2532 				ice_vsi_cfg_rss_lut_key(vsi);
2533 				ice_vsi_set_rss_flow_fld(vsi);
2534 			}
2535 		ice_init_arfs(vsi);
2536 		break;
2537 	case ICE_VSI_VF:
2538 		/* VF driver will take care of creating netdev for this type and
2539 		 * map queues to vectors through Virtchnl, PF driver only
2540 		 * creates a VSI and corresponding structures for bookkeeping
2541 		 * purpose
2542 		 */
2543 		ret = ice_vsi_alloc_q_vectors(vsi);
2544 		if (ret)
2545 			goto unroll_vsi_init;
2546 
2547 		ret = ice_vsi_alloc_rings(vsi);
2548 		if (ret)
2549 			goto unroll_alloc_q_vector;
2550 
2551 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2552 		if (ret)
2553 			goto unroll_vector_base;
2554 
2555 		/* Do not exit if configuring RSS had an issue, at least
2556 		 * receive traffic on first queue. Hence no need to capture
2557 		 * return value
2558 		 */
2559 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2560 			ice_vsi_cfg_rss_lut_key(vsi);
2561 			ice_vsi_set_vf_rss_flow_fld(vsi);
2562 		}
2563 		break;
2564 	case ICE_VSI_LB:
2565 		ret = ice_vsi_alloc_rings(vsi);
2566 		if (ret)
2567 			goto unroll_vsi_init;
2568 		break;
2569 	default:
2570 		/* clean up the resources and exit */
2571 		goto unroll_vsi_init;
2572 	}
2573 
2574 	/* configure VSI nodes based on number of queues and TC's */
2575 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2576 		max_txqs[i] = vsi->alloc_txq;
2577 
2578 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2579 				 max_txqs);
2580 	if (status) {
2581 		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2582 			vsi->vsi_num, ice_stat_str(status));
2583 		goto unroll_clear_rings;
2584 	}
2585 
2586 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2587 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2588 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2589 	 * The rule is added once for PF VSI in order to create appropriate
2590 	 * recipe, since VSI/VSI list is ignored with drop action...
2591 	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2592 	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2593 	 * settings in the HW.
2594 	 */
2595 	if (!ice_is_safe_mode(pf))
2596 		if (vsi->type == ICE_VSI_PF) {
2597 			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2598 					 ICE_DROP_PACKET);
2599 			ice_cfg_sw_lldp(vsi, true, true);
2600 		}
2601 
2602 	if (!vsi->agg_node)
2603 		ice_set_agg_vsi(vsi);
2604 	return vsi;
2605 
2606 unroll_clear_rings:
2607 	ice_vsi_clear_rings(vsi);
2608 unroll_vector_base:
2609 	/* reclaim SW interrupts back to the common pool */
2610 	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2611 	pf->num_avail_sw_msix += vsi->num_q_vectors;
2612 unroll_alloc_q_vector:
2613 	ice_vsi_free_q_vectors(vsi);
2614 unroll_vsi_init:
2615 	ice_vsi_delete(vsi);
2616 unroll_get_qs:
2617 	ice_vsi_put_qs(vsi);
2618 unroll_vsi_alloc:
2619 	if (vsi_type == ICE_VSI_VF)
2620 		ice_enable_lag(pf->lag);
2621 	ice_vsi_clear(vsi);
2622 
2623 	return NULL;
2624 }
2625 
2626 /**
2627  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2628  * @vsi: the VSI being cleaned up
2629  */
2630 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2631 {
2632 	struct ice_pf *pf = vsi->back;
2633 	struct ice_hw *hw = &pf->hw;
2634 	u32 txq = 0;
2635 	u32 rxq = 0;
2636 	int i, q;
2637 
2638 	for (i = 0; i < vsi->num_q_vectors; i++) {
2639 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2640 
2641 		ice_write_intrl(q_vector, 0);
2642 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2643 			ice_write_itr(&q_vector->tx, 0);
2644 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2645 			if (ice_is_xdp_ena_vsi(vsi)) {
2646 				u32 xdp_txq = txq + vsi->num_xdp_txq;
2647 
2648 				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2649 			}
2650 			txq++;
2651 		}
2652 
2653 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2654 			ice_write_itr(&q_vector->rx, 0);
2655 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2656 			rxq++;
2657 		}
2658 	}
2659 
2660 	ice_flush(hw);
2661 }
2662 
2663 /**
2664  * ice_vsi_free_irq - Free the IRQ association with the OS
2665  * @vsi: the VSI being configured
2666  */
2667 void ice_vsi_free_irq(struct ice_vsi *vsi)
2668 {
2669 	struct ice_pf *pf = vsi->back;
2670 	int base = vsi->base_vector;
2671 	int i;
2672 
2673 	if (!vsi->q_vectors || !vsi->irqs_ready)
2674 		return;
2675 
2676 	ice_vsi_release_msix(vsi);
2677 	if (vsi->type == ICE_VSI_VF)
2678 		return;
2679 
2680 	vsi->irqs_ready = false;
2681 	ice_for_each_q_vector(vsi, i) {
2682 		u16 vector = i + base;
2683 		int irq_num;
2684 
2685 		irq_num = pf->msix_entries[vector].vector;
2686 
2687 		/* free only the irqs that were actually requested */
2688 		if (!vsi->q_vectors[i] ||
2689 		    !(vsi->q_vectors[i]->num_ring_tx ||
2690 		      vsi->q_vectors[i]->num_ring_rx))
2691 			continue;
2692 
2693 		/* clear the affinity notifier in the IRQ descriptor */
2694 		irq_set_affinity_notifier(irq_num, NULL);
2695 
2696 		/* clear the affinity_mask in the IRQ descriptor */
2697 		irq_set_affinity_hint(irq_num, NULL);
2698 		synchronize_irq(irq_num);
2699 		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2700 	}
2701 }
2702 
2703 /**
2704  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2705  * @vsi: the VSI having resources freed
2706  */
2707 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2708 {
2709 	int i;
2710 
2711 	if (!vsi->tx_rings)
2712 		return;
2713 
2714 	ice_for_each_txq(vsi, i)
2715 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2716 			ice_free_tx_ring(vsi->tx_rings[i]);
2717 }
2718 
2719 /**
2720  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2721  * @vsi: the VSI having resources freed
2722  */
2723 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2724 {
2725 	int i;
2726 
2727 	if (!vsi->rx_rings)
2728 		return;
2729 
2730 	ice_for_each_rxq(vsi, i)
2731 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2732 			ice_free_rx_ring(vsi->rx_rings[i]);
2733 }
2734 
2735 /**
2736  * ice_vsi_close - Shut down a VSI
2737  * @vsi: the VSI being shut down
2738  */
2739 void ice_vsi_close(struct ice_vsi *vsi)
2740 {
2741 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2742 		ice_down(vsi);
2743 
2744 	ice_vsi_free_irq(vsi);
2745 	ice_vsi_free_tx_rings(vsi);
2746 	ice_vsi_free_rx_rings(vsi);
2747 }
2748 
2749 /**
2750  * ice_ena_vsi - resume a VSI
2751  * @vsi: the VSI being resume
2752  * @locked: is the rtnl_lock already held
2753  */
2754 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2755 {
2756 	int err = 0;
2757 
2758 	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2759 		return 0;
2760 
2761 	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2762 
2763 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2764 		if (netif_running(vsi->netdev)) {
2765 			if (!locked)
2766 				rtnl_lock();
2767 
2768 			err = ice_open_internal(vsi->netdev);
2769 
2770 			if (!locked)
2771 				rtnl_unlock();
2772 		}
2773 	} else if (vsi->type == ICE_VSI_CTRL) {
2774 		err = ice_vsi_open_ctrl(vsi);
2775 	}
2776 
2777 	return err;
2778 }
2779 
2780 /**
2781  * ice_dis_vsi - pause a VSI
2782  * @vsi: the VSI being paused
2783  * @locked: is the rtnl_lock already held
2784  */
2785 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2786 {
2787 	if (test_bit(ICE_VSI_DOWN, vsi->state))
2788 		return;
2789 
2790 	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2791 
2792 	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2793 		if (netif_running(vsi->netdev)) {
2794 			if (!locked)
2795 				rtnl_lock();
2796 
2797 			ice_vsi_close(vsi);
2798 
2799 			if (!locked)
2800 				rtnl_unlock();
2801 		} else {
2802 			ice_vsi_close(vsi);
2803 		}
2804 	} else if (vsi->type == ICE_VSI_CTRL ||
2805 		   vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2806 		ice_vsi_close(vsi);
2807 	}
2808 }
2809 
2810 /**
2811  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2812  * @vsi: the VSI being un-configured
2813  */
2814 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2815 {
2816 	int base = vsi->base_vector;
2817 	struct ice_pf *pf = vsi->back;
2818 	struct ice_hw *hw = &pf->hw;
2819 	u32 val;
2820 	int i;
2821 
2822 	/* disable interrupt causation from each queue */
2823 	if (vsi->tx_rings) {
2824 		ice_for_each_txq(vsi, i) {
2825 			if (vsi->tx_rings[i]) {
2826 				u16 reg;
2827 
2828 				reg = vsi->tx_rings[i]->reg_idx;
2829 				val = rd32(hw, QINT_TQCTL(reg));
2830 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2831 				wr32(hw, QINT_TQCTL(reg), val);
2832 			}
2833 		}
2834 	}
2835 
2836 	if (vsi->rx_rings) {
2837 		ice_for_each_rxq(vsi, i) {
2838 			if (vsi->rx_rings[i]) {
2839 				u16 reg;
2840 
2841 				reg = vsi->rx_rings[i]->reg_idx;
2842 				val = rd32(hw, QINT_RQCTL(reg));
2843 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2844 				wr32(hw, QINT_RQCTL(reg), val);
2845 			}
2846 		}
2847 	}
2848 
2849 	/* disable each interrupt */
2850 	ice_for_each_q_vector(vsi, i) {
2851 		if (!vsi->q_vectors[i])
2852 			continue;
2853 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2854 	}
2855 
2856 	ice_flush(hw);
2857 
2858 	/* don't call synchronize_irq() for VF's from the host */
2859 	if (vsi->type == ICE_VSI_VF)
2860 		return;
2861 
2862 	ice_for_each_q_vector(vsi, i)
2863 		synchronize_irq(pf->msix_entries[i + base].vector);
2864 }
2865 
2866 /**
2867  * ice_napi_del - Remove NAPI handler for the VSI
2868  * @vsi: VSI for which NAPI handler is to be removed
2869  */
2870 void ice_napi_del(struct ice_vsi *vsi)
2871 {
2872 	int v_idx;
2873 
2874 	if (!vsi->netdev)
2875 		return;
2876 
2877 	ice_for_each_q_vector(vsi, v_idx)
2878 		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2879 }
2880 
2881 /**
2882  * ice_vsi_release - Delete a VSI and free its resources
2883  * @vsi: the VSI being removed
2884  *
2885  * Returns 0 on success or < 0 on error
2886  */
2887 int ice_vsi_release(struct ice_vsi *vsi)
2888 {
2889 	struct ice_pf *pf;
2890 
2891 	if (!vsi->back)
2892 		return -ENODEV;
2893 	pf = vsi->back;
2894 
2895 	/* do not unregister while driver is in the reset recovery pending
2896 	 * state. Since reset/rebuild happens through PF service task workqueue,
2897 	 * it's not a good idea to unregister netdev that is associated to the
2898 	 * PF that is running the work queue items currently. This is done to
2899 	 * avoid check_flush_dependency() warning on this wq
2900 	 */
2901 	if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2902 	    (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2903 		unregister_netdev(vsi->netdev);
2904 		clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2905 	}
2906 
2907 	if (vsi->type == ICE_VSI_PF)
2908 		ice_devlink_destroy_pf_port(pf);
2909 
2910 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2911 		ice_rss_clean(vsi);
2912 
2913 	/* Disable VSI and free resources */
2914 	if (vsi->type != ICE_VSI_LB)
2915 		ice_vsi_dis_irq(vsi);
2916 	ice_vsi_close(vsi);
2917 
2918 	/* SR-IOV determines needed MSIX resources all at once instead of per
2919 	 * VSI since when VFs are spawned we know how many VFs there are and how
2920 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2921 	 * cleared in the same manner.
2922 	 */
2923 	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2924 		int i;
2925 
2926 		ice_for_each_vf(pf, i) {
2927 			struct ice_vf *vf = &pf->vf[i];
2928 
2929 			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2930 				break;
2931 		}
2932 		if (i == pf->num_alloc_vfs) {
2933 			/* No other VFs left that have control VSI, reclaim SW
2934 			 * interrupts back to the common pool
2935 			 */
2936 			ice_free_res(pf->irq_tracker, vsi->base_vector,
2937 				     ICE_RES_VF_CTRL_VEC_ID);
2938 			pf->num_avail_sw_msix += vsi->num_q_vectors;
2939 		}
2940 	} else if (vsi->type != ICE_VSI_VF) {
2941 		/* reclaim SW interrupts back to the common pool */
2942 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2943 		pf->num_avail_sw_msix += vsi->num_q_vectors;
2944 	}
2945 
2946 	if (!ice_is_safe_mode(pf)) {
2947 		if (vsi->type == ICE_VSI_PF) {
2948 			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2949 					    ICE_DROP_PACKET);
2950 			ice_cfg_sw_lldp(vsi, true, false);
2951 			/* The Rx rule will only exist to remove if the LLDP FW
2952 			 * engine is currently stopped
2953 			 */
2954 			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2955 				ice_cfg_sw_lldp(vsi, false, false);
2956 		}
2957 	}
2958 
2959 	ice_fltr_remove_all(vsi);
2960 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2961 	ice_vsi_delete(vsi);
2962 	ice_vsi_free_q_vectors(vsi);
2963 
2964 	if (vsi->netdev) {
2965 		if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2966 			unregister_netdev(vsi->netdev);
2967 			clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2968 		}
2969 		if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2970 			free_netdev(vsi->netdev);
2971 			vsi->netdev = NULL;
2972 			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2973 		}
2974 	}
2975 
2976 	if (vsi->type == ICE_VSI_VF &&
2977 	    vsi->agg_node && vsi->agg_node->valid)
2978 		vsi->agg_node->num_vsis--;
2979 	ice_vsi_clear_rings(vsi);
2980 
2981 	ice_vsi_put_qs(vsi);
2982 
2983 	/* retain SW VSI data structure since it is needed to unregister and
2984 	 * free VSI netdev when PF is not in reset recovery pending state,\
2985 	 * for ex: during rmmod.
2986 	 */
2987 	if (!ice_is_reset_in_progress(pf->state))
2988 		ice_vsi_clear(vsi);
2989 
2990 	return 0;
2991 }
2992 
2993 /**
2994  * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2995  * @vsi: VSI connected with q_vectors
2996  * @coalesce: array of struct with stored coalesce
2997  *
2998  * Returns array size.
2999  */
3000 static int
3001 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3002 			     struct ice_coalesce_stored *coalesce)
3003 {
3004 	int i;
3005 
3006 	ice_for_each_q_vector(vsi, i) {
3007 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
3008 
3009 		coalesce[i].itr_tx = q_vector->tx.itr_setting;
3010 		coalesce[i].itr_rx = q_vector->rx.itr_setting;
3011 		coalesce[i].intrl = q_vector->intrl;
3012 
3013 		if (i < vsi->num_txq)
3014 			coalesce[i].tx_valid = true;
3015 		if (i < vsi->num_rxq)
3016 			coalesce[i].rx_valid = true;
3017 	}
3018 
3019 	return vsi->num_q_vectors;
3020 }
3021 
3022 /**
3023  * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3024  * @vsi: VSI connected with q_vectors
3025  * @coalesce: pointer to array of struct with stored coalesce
3026  * @size: size of coalesce array
3027  *
3028  * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3029  * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3030  * to default value.
3031  */
3032 static void
3033 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3034 			     struct ice_coalesce_stored *coalesce, int size)
3035 {
3036 	struct ice_ring_container *rc;
3037 	int i;
3038 
3039 	if ((size && !coalesce) || !vsi)
3040 		return;
3041 
3042 	/* There are a couple of cases that have to be handled here:
3043 	 *   1. The case where the number of queue vectors stays the same, but
3044 	 *      the number of Tx or Rx rings changes (the first for loop)
3045 	 *   2. The case where the number of queue vectors increased (the
3046 	 *      second for loop)
3047 	 */
3048 	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3049 		/* There are 2 cases to handle here and they are the same for
3050 		 * both Tx and Rx:
3051 		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
3052 		 *   and the loop variable is less than the number of rings
3053 		 *   allocated, then write the previous values
3054 		 *
3055 		 *   if the entry was not valid previously, but the number of
3056 		 *   rings is less than are allocated (this means the number of
3057 		 *   rings increased from previously), then write out the
3058 		 *   values in the first element
3059 		 *
3060 		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
3061 		 *   as there is no harm because the dynamic algorithm
3062 		 *   will just overwrite.
3063 		 */
3064 		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3065 			rc = &vsi->q_vectors[i]->rx;
3066 			rc->itr_setting = coalesce[i].itr_rx;
3067 			ice_write_itr(rc, rc->itr_setting);
3068 		} else if (i < vsi->alloc_rxq) {
3069 			rc = &vsi->q_vectors[i]->rx;
3070 			rc->itr_setting = coalesce[0].itr_rx;
3071 			ice_write_itr(rc, rc->itr_setting);
3072 		}
3073 
3074 		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3075 			rc = &vsi->q_vectors[i]->tx;
3076 			rc->itr_setting = coalesce[i].itr_tx;
3077 			ice_write_itr(rc, rc->itr_setting);
3078 		} else if (i < vsi->alloc_txq) {
3079 			rc = &vsi->q_vectors[i]->tx;
3080 			rc->itr_setting = coalesce[0].itr_tx;
3081 			ice_write_itr(rc, rc->itr_setting);
3082 		}
3083 
3084 		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3085 		ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
3086 	}
3087 
3088 	/* the number of queue vectors increased so write whatever is in
3089 	 * the first element
3090 	 */
3091 	for (; i < vsi->num_q_vectors; i++) {
3092 		/* transmit */
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 		/* receive */
3098 		rc = &vsi->q_vectors[i]->rx;
3099 		rc->itr_setting = coalesce[0].itr_rx;
3100 		ice_write_itr(rc, rc->itr_setting);
3101 
3102 		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3103 		ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3104 	}
3105 }
3106 
3107 /**
3108  * ice_vsi_rebuild - Rebuild VSI after reset
3109  * @vsi: VSI to be rebuild
3110  * @init_vsi: is this an initialization or a reconfigure of the VSI
3111  *
3112  * Returns 0 on success and negative value on failure
3113  */
3114 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3115 {
3116 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3117 	struct ice_coalesce_stored *coalesce;
3118 	int prev_num_q_vectors = 0;
3119 	struct ice_vf *vf = NULL;
3120 	enum ice_vsi_type vtype;
3121 	enum ice_status status;
3122 	struct ice_pf *pf;
3123 	int ret, i;
3124 
3125 	if (!vsi)
3126 		return -EINVAL;
3127 
3128 	pf = vsi->back;
3129 	vtype = vsi->type;
3130 	if (vtype == ICE_VSI_VF)
3131 		vf = &pf->vf[vsi->vf_id];
3132 
3133 	coalesce = kcalloc(vsi->num_q_vectors,
3134 			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3135 	if (!coalesce)
3136 		return -ENOMEM;
3137 
3138 	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3139 
3140 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3141 	ice_vsi_free_q_vectors(vsi);
3142 
3143 	/* SR-IOV determines needed MSIX resources all at once instead of per
3144 	 * VSI since when VFs are spawned we know how many VFs there are and how
3145 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
3146 	 * cleared in the same manner.
3147 	 */
3148 	if (vtype != ICE_VSI_VF) {
3149 		/* reclaim SW interrupts back to the common pool */
3150 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3151 		pf->num_avail_sw_msix += vsi->num_q_vectors;
3152 		vsi->base_vector = 0;
3153 	}
3154 
3155 	if (ice_is_xdp_ena_vsi(vsi))
3156 		/* return value check can be skipped here, it always returns
3157 		 * 0 if reset is in progress
3158 		 */
3159 		ice_destroy_xdp_rings(vsi);
3160 	ice_vsi_put_qs(vsi);
3161 	ice_vsi_clear_rings(vsi);
3162 	ice_vsi_free_arrays(vsi);
3163 	if (vtype == ICE_VSI_VF)
3164 		ice_vsi_set_num_qs(vsi, vf->vf_id);
3165 	else
3166 		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3167 
3168 	ret = ice_vsi_alloc_arrays(vsi);
3169 	if (ret < 0)
3170 		goto err_vsi;
3171 
3172 	ice_vsi_get_qs(vsi);
3173 
3174 	ice_alloc_fd_res(vsi);
3175 	ice_vsi_set_tc_cfg(vsi);
3176 
3177 	/* Initialize VSI struct elements and create VSI in FW */
3178 	ret = ice_vsi_init(vsi, init_vsi);
3179 	if (ret < 0)
3180 		goto err_vsi;
3181 
3182 	switch (vtype) {
3183 	case ICE_VSI_CTRL:
3184 	case ICE_VSI_SWITCHDEV_CTRL:
3185 	case ICE_VSI_PF:
3186 		ret = ice_vsi_alloc_q_vectors(vsi);
3187 		if (ret)
3188 			goto err_rings;
3189 
3190 		ret = ice_vsi_setup_vector_base(vsi);
3191 		if (ret)
3192 			goto err_vectors;
3193 
3194 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3195 		if (ret)
3196 			goto err_vectors;
3197 
3198 		ret = ice_vsi_alloc_rings(vsi);
3199 		if (ret)
3200 			goto err_vectors;
3201 
3202 		ice_vsi_map_rings_to_vectors(vsi);
3203 		if (ice_is_xdp_ena_vsi(vsi)) {
3204 			vsi->num_xdp_txq = vsi->alloc_rxq;
3205 			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3206 			if (ret)
3207 				goto err_vectors;
3208 		}
3209 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3210 		if (vtype != ICE_VSI_CTRL)
3211 			/* Do not exit if configuring RSS had an issue, at
3212 			 * least receive traffic on first queue. Hence no
3213 			 * need to capture return value
3214 			 */
3215 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3216 				ice_vsi_cfg_rss_lut_key(vsi);
3217 		break;
3218 	case ICE_VSI_VF:
3219 		ret = ice_vsi_alloc_q_vectors(vsi);
3220 		if (ret)
3221 			goto err_rings;
3222 
3223 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3224 		if (ret)
3225 			goto err_vectors;
3226 
3227 		ret = ice_vsi_alloc_rings(vsi);
3228 		if (ret)
3229 			goto err_vectors;
3230 
3231 		break;
3232 	default:
3233 		break;
3234 	}
3235 
3236 	/* configure VSI nodes based on number of queues and TC's */
3237 	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3238 		max_txqs[i] = vsi->alloc_txq;
3239 
3240 		if (ice_is_xdp_ena_vsi(vsi))
3241 			max_txqs[i] += vsi->num_xdp_txq;
3242 	}
3243 
3244 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3245 				 max_txqs);
3246 	if (status) {
3247 		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3248 			vsi->vsi_num, ice_stat_str(status));
3249 		if (init_vsi) {
3250 			ret = -EIO;
3251 			goto err_vectors;
3252 		} else {
3253 			return ice_schedule_reset(pf, ICE_RESET_PFR);
3254 		}
3255 	}
3256 	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3257 	kfree(coalesce);
3258 
3259 	return 0;
3260 
3261 err_vectors:
3262 	ice_vsi_free_q_vectors(vsi);
3263 err_rings:
3264 	if (vsi->netdev) {
3265 		vsi->current_netdev_flags = 0;
3266 		unregister_netdev(vsi->netdev);
3267 		free_netdev(vsi->netdev);
3268 		vsi->netdev = NULL;
3269 	}
3270 err_vsi:
3271 	ice_vsi_clear(vsi);
3272 	set_bit(ICE_RESET_FAILED, pf->state);
3273 	kfree(coalesce);
3274 	return ret;
3275 }
3276 
3277 /**
3278  * ice_is_reset_in_progress - check for a reset in progress
3279  * @state: PF state field
3280  */
3281 bool ice_is_reset_in_progress(unsigned long *state)
3282 {
3283 	return test_bit(ICE_RESET_OICR_RECV, state) ||
3284 	       test_bit(ICE_PFR_REQ, state) ||
3285 	       test_bit(ICE_CORER_REQ, state) ||
3286 	       test_bit(ICE_GLOBR_REQ, state);
3287 }
3288 
3289 /**
3290  * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3291  * @pf: pointer to the PF structure
3292  * @timeout: length of time to wait, in jiffies
3293  *
3294  * Wait (sleep) for a short time until the driver finishes cleaning up from
3295  * a device reset. The caller must be able to sleep. Use this to delay
3296  * operations that could fail while the driver is cleaning up after a device
3297  * reset.
3298  *
3299  * Returns 0 on success, -EBUSY if the reset is not finished within the
3300  * timeout, and -ERESTARTSYS if the thread was interrupted.
3301  */
3302 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3303 {
3304 	long ret;
3305 
3306 	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3307 					       !ice_is_reset_in_progress(pf->state),
3308 					       timeout);
3309 	if (ret < 0)
3310 		return ret;
3311 	else if (!ret)
3312 		return -EBUSY;
3313 	else
3314 		return 0;
3315 }
3316 
3317 #ifdef CONFIG_DCB
3318 /**
3319  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3320  * @vsi: VSI being configured
3321  * @ctx: the context buffer returned from AQ VSI update command
3322  */
3323 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3324 {
3325 	vsi->info.mapping_flags = ctx->info.mapping_flags;
3326 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3327 	       sizeof(vsi->info.q_mapping));
3328 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3329 	       sizeof(vsi->info.tc_mapping));
3330 }
3331 
3332 /**
3333  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3334  * @vsi: VSI to be configured
3335  * @ena_tc: TC bitmap
3336  *
3337  * VSI queues expected to be quiesced before calling this function
3338  */
3339 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3340 {
3341 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3342 	struct ice_pf *pf = vsi->back;
3343 	struct ice_vsi_ctx *ctx;
3344 	enum ice_status status;
3345 	struct device *dev;
3346 	int i, ret = 0;
3347 	u8 num_tc = 0;
3348 
3349 	dev = ice_pf_to_dev(pf);
3350 
3351 	ice_for_each_traffic_class(i) {
3352 		/* build bitmap of enabled TCs */
3353 		if (ena_tc & BIT(i))
3354 			num_tc++;
3355 		/* populate max_txqs per TC */
3356 		max_txqs[i] = vsi->alloc_txq;
3357 	}
3358 
3359 	vsi->tc_cfg.ena_tc = ena_tc;
3360 	vsi->tc_cfg.numtc = num_tc;
3361 
3362 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3363 	if (!ctx)
3364 		return -ENOMEM;
3365 
3366 	ctx->vf_num = 0;
3367 	ctx->info = vsi->info;
3368 
3369 	ice_vsi_setup_q_map(vsi, ctx);
3370 
3371 	/* must to indicate which section of VSI context are being modified */
3372 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3373 	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3374 	if (status) {
3375 		dev_info(dev, "Failed VSI Update\n");
3376 		ret = -EIO;
3377 		goto out;
3378 	}
3379 
3380 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3381 				 max_txqs);
3382 
3383 	if (status) {
3384 		dev_err(dev, "VSI %d failed TC config, error %s\n",
3385 			vsi->vsi_num, ice_stat_str(status));
3386 		ret = -EIO;
3387 		goto out;
3388 	}
3389 	ice_vsi_update_q_map(vsi, ctx);
3390 	vsi->info.valid_sections = 0;
3391 
3392 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3393 out:
3394 	kfree(ctx);
3395 	return ret;
3396 }
3397 #endif /* CONFIG_DCB */
3398 
3399 /**
3400  * ice_update_ring_stats - Update ring statistics
3401  * @ring: ring to update
3402  * @pkts: number of processed packets
3403  * @bytes: number of processed bytes
3404  *
3405  * This function assumes that caller has acquired a u64_stats_sync lock.
3406  */
3407 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3408 {
3409 	ring->stats.bytes += bytes;
3410 	ring->stats.pkts += pkts;
3411 }
3412 
3413 /**
3414  * ice_update_tx_ring_stats - Update Tx ring specific counters
3415  * @tx_ring: ring to update
3416  * @pkts: number of processed packets
3417  * @bytes: number of processed bytes
3418  */
3419 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3420 {
3421 	u64_stats_update_begin(&tx_ring->syncp);
3422 	ice_update_ring_stats(tx_ring, pkts, bytes);
3423 	u64_stats_update_end(&tx_ring->syncp);
3424 }
3425 
3426 /**
3427  * ice_update_rx_ring_stats - Update Rx ring specific counters
3428  * @rx_ring: ring to update
3429  * @pkts: number of processed packets
3430  * @bytes: number of processed bytes
3431  */
3432 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3433 {
3434 	u64_stats_update_begin(&rx_ring->syncp);
3435 	ice_update_ring_stats(rx_ring, pkts, bytes);
3436 	u64_stats_update_end(&rx_ring->syncp);
3437 }
3438 
3439 /**
3440  * ice_status_to_errno - convert from enum ice_status to Linux errno
3441  * @err: ice_status value to convert
3442  */
3443 int ice_status_to_errno(enum ice_status err)
3444 {
3445 	switch (err) {
3446 	case ICE_SUCCESS:
3447 		return 0;
3448 	case ICE_ERR_DOES_NOT_EXIST:
3449 		return -ENOENT;
3450 	case ICE_ERR_OUT_OF_RANGE:
3451 	case ICE_ERR_AQ_ERROR:
3452 	case ICE_ERR_AQ_TIMEOUT:
3453 	case ICE_ERR_AQ_EMPTY:
3454 	case ICE_ERR_AQ_FW_CRITICAL:
3455 		return -EIO;
3456 	case ICE_ERR_PARAM:
3457 	case ICE_ERR_INVAL_SIZE:
3458 		return -EINVAL;
3459 	case ICE_ERR_NO_MEMORY:
3460 		return -ENOMEM;
3461 	case ICE_ERR_MAX_LIMIT:
3462 		return -EAGAIN;
3463 	case ICE_ERR_RESET_ONGOING:
3464 		return -EBUSY;
3465 	case ICE_ERR_AQ_FULL:
3466 		return -ENOSPC;
3467 	default:
3468 		return -EINVAL;
3469 	}
3470 }
3471 
3472 /**
3473  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3474  * @sw: switch to check if its default forwarding VSI is free
3475  *
3476  * Return true if the default forwarding VSI is already being used, else returns
3477  * false signalling that it's available to use.
3478  */
3479 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3480 {
3481 	return (sw->dflt_vsi && sw->dflt_vsi_ena);
3482 }
3483 
3484 /**
3485  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3486  * @sw: switch for the default forwarding VSI to compare against
3487  * @vsi: VSI to compare against default forwarding VSI
3488  *
3489  * If this VSI passed in is the default forwarding VSI then return true, else
3490  * return false
3491  */
3492 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3493 {
3494 	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3495 }
3496 
3497 /**
3498  * ice_set_dflt_vsi - set the default forwarding VSI
3499  * @sw: switch used to assign the default forwarding VSI
3500  * @vsi: VSI getting set as the default forwarding VSI on the switch
3501  *
3502  * If the VSI passed in is already the default VSI and it's enabled just return
3503  * success.
3504  *
3505  * If there is already a default VSI on the switch and it's enabled then return
3506  * -EEXIST since there can only be one default VSI per switch.
3507  *
3508  *  Otherwise try to set the VSI passed in as the switch's default VSI and
3509  *  return the result.
3510  */
3511 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3512 {
3513 	enum ice_status status;
3514 	struct device *dev;
3515 
3516 	if (!sw || !vsi)
3517 		return -EINVAL;
3518 
3519 	dev = ice_pf_to_dev(vsi->back);
3520 
3521 	/* the VSI passed in is already the default VSI */
3522 	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3523 		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3524 			vsi->vsi_num);
3525 		return 0;
3526 	}
3527 
3528 	/* another VSI is already the default VSI for this switch */
3529 	if (ice_is_dflt_vsi_in_use(sw)) {
3530 		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3531 			sw->dflt_vsi->vsi_num);
3532 		return -EEXIST;
3533 	}
3534 
3535 	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3536 	if (status) {
3537 		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3538 			vsi->vsi_num, ice_stat_str(status));
3539 		return -EIO;
3540 	}
3541 
3542 	sw->dflt_vsi = vsi;
3543 	sw->dflt_vsi_ena = true;
3544 
3545 	return 0;
3546 }
3547 
3548 /**
3549  * ice_clear_dflt_vsi - clear the default forwarding VSI
3550  * @sw: switch used to clear the default VSI
3551  *
3552  * If the switch has no default VSI or it's not enabled then return error.
3553  *
3554  * Otherwise try to clear the default VSI and return the result.
3555  */
3556 int ice_clear_dflt_vsi(struct ice_sw *sw)
3557 {
3558 	struct ice_vsi *dflt_vsi;
3559 	enum ice_status status;
3560 	struct device *dev;
3561 
3562 	if (!sw)
3563 		return -EINVAL;
3564 
3565 	dev = ice_pf_to_dev(sw->pf);
3566 
3567 	dflt_vsi = sw->dflt_vsi;
3568 
3569 	/* there is no default VSI configured */
3570 	if (!ice_is_dflt_vsi_in_use(sw))
3571 		return -ENODEV;
3572 
3573 	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3574 				  ICE_FLTR_RX);
3575 	if (status) {
3576 		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3577 			dflt_vsi->vsi_num, ice_stat_str(status));
3578 		return -EIO;
3579 	}
3580 
3581 	sw->dflt_vsi = NULL;
3582 	sw->dflt_vsi_ena = false;
3583 
3584 	return 0;
3585 }
3586 
3587 /**
3588  * ice_set_link - turn on/off physical link
3589  * @vsi: VSI to modify physical link on
3590  * @ena: turn on/off physical link
3591  */
3592 int ice_set_link(struct ice_vsi *vsi, bool ena)
3593 {
3594 	struct device *dev = ice_pf_to_dev(vsi->back);
3595 	struct ice_port_info *pi = vsi->port_info;
3596 	struct ice_hw *hw = pi->hw;
3597 	enum ice_status status;
3598 
3599 	if (vsi->type != ICE_VSI_PF)
3600 		return -EINVAL;
3601 
3602 	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3603 
3604 	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3605 	 * this is not a fatal error, so print a warning message and return
3606 	 * a success code. Return an error if FW returns an error code other
3607 	 * than ICE_AQ_RC_EMODE
3608 	 */
3609 	if (status == ICE_ERR_AQ_ERROR) {
3610 		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3611 			dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3612 				 (ena ? "ON" : "OFF"), ice_stat_str(status),
3613 				 ice_aq_str(hw->adminq.sq_last_status));
3614 	} else if (status) {
3615 		dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3616 			(ena ? "ON" : "OFF"), ice_stat_str(status),
3617 			ice_aq_str(hw->adminq.sq_last_status));
3618 		return -EIO;
3619 	}
3620 
3621 	return 0;
3622 }
3623 
3624 /**
3625  * ice_is_feature_supported
3626  * @pf: pointer to the struct ice_pf instance
3627  * @f: feature enum to be checked
3628  *
3629  * returns true if feature is supported, false otherwise
3630  */
3631 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3632 {
3633 	if (f < 0 || f >= ICE_F_MAX)
3634 		return false;
3635 
3636 	return test_bit(f, pf->features);
3637 }
3638 
3639 /**
3640  * ice_set_feature_support
3641  * @pf: pointer to the struct ice_pf instance
3642  * @f: feature enum to set
3643  */
3644 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
3645 {
3646 	if (f < 0 || f >= ICE_F_MAX)
3647 		return;
3648 
3649 	set_bit(f, pf->features);
3650 }
3651 
3652 /**
3653  * ice_init_feature_support
3654  * @pf: pointer to the struct ice_pf instance
3655  *
3656  * called during init to setup supported feature
3657  */
3658 void ice_init_feature_support(struct ice_pf *pf)
3659 {
3660 	switch (pf->hw.device_id) {
3661 	case ICE_DEV_ID_E810C_BACKPLANE:
3662 	case ICE_DEV_ID_E810C_QSFP:
3663 	case ICE_DEV_ID_E810C_SFP:
3664 		ice_set_feature_support(pf, ICE_F_DSCP);
3665 		break;
3666 	default:
3667 		break;
3668 	}
3669 }
3670 
3671 /**
3672  * ice_vsi_update_security - update security block in VSI
3673  * @vsi: pointer to VSI structure
3674  * @fill: function pointer to fill ctx
3675  */
3676 int
3677 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
3678 {
3679 	struct ice_vsi_ctx ctx = { 0 };
3680 
3681 	ctx.info = vsi->info;
3682 	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
3683 	fill(&ctx);
3684 
3685 	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3686 		return -ENODEV;
3687 
3688 	vsi->info = ctx.info;
3689 	return 0;
3690 }
3691 
3692 /**
3693  * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
3694  * @ctx: pointer to VSI ctx structure
3695  */
3696 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
3697 {
3698 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
3699 			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3700 				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3701 }
3702 
3703 /**
3704  * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
3705  * @ctx: pointer to VSI ctx structure
3706  */
3707 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
3708 {
3709 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
3710 			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3711 				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3712 }
3713 
3714 /**
3715  * ice_vsi_ctx_set_allow_override - allow destination override on VSI
3716  * @ctx: pointer to VSI ctx structure
3717  */
3718 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
3719 {
3720 	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3721 }
3722 
3723 /**
3724  * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
3725  * @ctx: pointer to VSI ctx structure
3726  */
3727 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
3728 {
3729 	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3730 }
3731