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