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