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