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