1255253Ssjg// SPDX-License-Identifier: GPL-2.0
2236769Sobrien/* Copyright (c) 2018, Intel Corporation. */
3236769Sobrien
4236769Sobrien#include "ice.h"
5236769Sobrien#include "ice_base.h"
6236769Sobrien#include "ice_flow.h"
7236769Sobrien#include "ice_lib.h"
8236769Sobrien#include "ice_fltr.h"
9236769Sobrien#include "ice_dcb_lib.h"
10236769Sobrien#include "ice_vsi_vlan_ops.h"
11236769Sobrien
12236769Sobrien/**
13236769Sobrien * ice_vsi_type_str - maps VSI type enum to string equivalents
14236769Sobrien * @vsi_type: VSI type enum
15236769Sobrien */
16236769Sobrienconst char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
17236769Sobrien{
18236769Sobrien	switch (vsi_type) {
19236769Sobrien	case ICE_VSI_PF:
20236769Sobrien		return "ICE_VSI_PF";
21236769Sobrien	case ICE_VSI_VF:
22236769Sobrien		return "ICE_VSI_VF";
23236769Sobrien	case ICE_VSI_CTRL:
24236769Sobrien		return "ICE_VSI_CTRL";
25236769Sobrien	case ICE_VSI_CHNL:
26236769Sobrien		return "ICE_VSI_CHNL";
27236769Sobrien	case ICE_VSI_LB:
28236769Sobrien		return "ICE_VSI_LB";
29236769Sobrien	default:
30236769Sobrien		return "unknown";
31236769Sobrien	}
32236769Sobrien}
33236769Sobrien
34236769Sobrien/**
35236769Sobrien * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
36236769Sobrien * @vsi: the VSI being configured
37236769Sobrien * @ena: start or stop the Rx rings
38236769Sobrien *
39236769Sobrien * First enable/disable all of the Rx rings, flush any remaining writes, and
40236769Sobrien * then verify that they have all been enabled/disabled successfully. This will
41236769Sobrien * let all of the register writes complete when enabling/disabling the Rx rings
42236769Sobrien * before waiting for the change in hardware to complete.
43236769Sobrien */
44236769Sobrienstatic int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
45236769Sobrien{
46236769Sobrien	int ret = 0;
47236769Sobrien	u16 i;
48236769Sobrien
49236769Sobrien	ice_for_each_rxq(vsi, i)
50236769Sobrien		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
51236769Sobrien
52236769Sobrien	ice_flush(&vsi->back->hw);
53236769Sobrien
54236769Sobrien	ice_for_each_rxq(vsi, i) {
55236769Sobrien		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
56236769Sobrien		if (ret)
57236769Sobrien			break;
58236769Sobrien	}
59236769Sobrien
60236769Sobrien	return ret;
61236769Sobrien}
62236769Sobrien
63236769Sobrien/**
64236769Sobrien * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
65236769Sobrien * @vsi: VSI pointer
66236769Sobrien *
67236769Sobrien * On error: returns error code (negative)
68236769Sobrien * On success: returns 0
69236769Sobrien */
70236769Sobrienstatic int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
71236769Sobrien{
72255253Ssjg	struct ice_pf *pf = vsi->back;
73236769Sobrien	struct device *dev;
74236769Sobrien
75236769Sobrien	dev = ice_pf_to_dev(pf);
76236769Sobrien	if (vsi->type == ICE_VSI_CHNL)
77236769Sobrien		return 0;
78236769Sobrien
79236769Sobrien	/* allocate memory for both Tx and Rx ring pointers */
80236769Sobrien	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
81236769Sobrien				     sizeof(*vsi->tx_rings), GFP_KERNEL);
82236769Sobrien	if (!vsi->tx_rings)
83236769Sobrien		return -ENOMEM;
84255253Ssjg
85236769Sobrien	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
86236769Sobrien				     sizeof(*vsi->rx_rings), GFP_KERNEL);
87236769Sobrien	if (!vsi->rx_rings)
88236769Sobrien		goto err_rings;
89236769Sobrien
90236769Sobrien	/* txq_map needs to have enough space to track both Tx (stack) rings
91236769Sobrien	 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
92236769Sobrien	 * so use num_possible_cpus() as we want to always provide XDP ring
93236769Sobrien	 * per CPU, regardless of queue count settings from user that might
94236769Sobrien	 * have come from ethtool's set_channels() callback;
95236769Sobrien	 */
96236769Sobrien	vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
97236769Sobrien				    sizeof(*vsi->txq_map), GFP_KERNEL);
98236769Sobrien
99236769Sobrien	if (!vsi->txq_map)
100236769Sobrien		goto err_txq_map;
101236769Sobrien
102236769Sobrien	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
103236769Sobrien				    sizeof(*vsi->rxq_map), GFP_KERNEL);
104236769Sobrien	if (!vsi->rxq_map)
105236769Sobrien		goto err_rxq_map;
106236769Sobrien
107236769Sobrien	/* There is no need to allocate q_vectors for a loopback VSI. */
108236769Sobrien	if (vsi->type == ICE_VSI_LB)
109236769Sobrien		return 0;
110236769Sobrien
111236769Sobrien	/* allocate memory for q_vector pointers */
112236769Sobrien	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
113236769Sobrien				      sizeof(*vsi->q_vectors), GFP_KERNEL);
114236769Sobrien	if (!vsi->q_vectors)
115236769Sobrien		goto err_vectors;
116236769Sobrien
117236769Sobrien	return 0;
118236769Sobrien
119236769Sobrienerr_vectors:
120236769Sobrien	devm_kfree(dev, vsi->rxq_map);
121236769Sobrienerr_rxq_map:
122236769Sobrien	devm_kfree(dev, vsi->txq_map);
123236769Sobrienerr_txq_map:
124236769Sobrien	devm_kfree(dev, vsi->rx_rings);
125236769Sobrienerr_rings:
126253883Ssjg	devm_kfree(dev, vsi->tx_rings);
127236769Sobrien	return -ENOMEM;
128236769Sobrien}
129236769Sobrien
130236769Sobrien/**
131253883Ssjg * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
132236769Sobrien * @vsi: the VSI being configured
133236769Sobrien */
134236769Sobrienstatic void ice_vsi_set_num_desc(struct ice_vsi *vsi)
135236769Sobrien{
136236769Sobrien	switch (vsi->type) {
137236769Sobrien	case ICE_VSI_PF:
138236769Sobrien	case ICE_VSI_CTRL:
139236769Sobrien	case ICE_VSI_LB:
140236769Sobrien		/* a user could change the values of num_[tr]x_desc using
141236769Sobrien		 * ethtool -G so we should keep those values instead of
142236769Sobrien		 * overwriting them with the defaults.
143236769Sobrien		 */
144236769Sobrien		if (!vsi->num_rx_desc)
145236769Sobrien			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
146236769Sobrien		if (!vsi->num_tx_desc)
147236769Sobrien			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
148236769Sobrien		break;
149236769Sobrien	default:
150236769Sobrien		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
151236769Sobrien			vsi->type);
152236769Sobrien		break;
153236769Sobrien	}
154236769Sobrien}
155236769Sobrien
156236769Sobrien/**
157236769Sobrien * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
158236769Sobrien * @vsi: the VSI being configured
159236769Sobrien *
160236769Sobrien * Return 0 on success and a negative value on error
161240330Smarcel */
162236769Sobrienstatic void ice_vsi_set_num_qs(struct ice_vsi *vsi)
163236769Sobrien{
164236769Sobrien	enum ice_vsi_type vsi_type = vsi->type;
165236769Sobrien	struct ice_pf *pf = vsi->back;
166236769Sobrien	struct ice_vf *vf = vsi->vf;
167253883Ssjg
168236769Sobrien	if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
169236769Sobrien		return;
170236769Sobrien
171236769Sobrien	switch (vsi_type) {
172236769Sobrien	case ICE_VSI_PF:
173236769Sobrien		if (vsi->req_txq) {
174236769Sobrien			vsi->alloc_txq = vsi->req_txq;
175236769Sobrien			vsi->num_txq = vsi->req_txq;
176236769Sobrien		} else {
177236769Sobrien			vsi->alloc_txq = min3(pf->num_lan_msix,
178236769Sobrien					      ice_get_avail_txq_count(pf),
179236769Sobrien					      (u16)num_online_cpus());
180236769Sobrien		}
181237578Sobrien
182236769Sobrien		pf->num_lan_tx = vsi->alloc_txq;
183236769Sobrien
184236769Sobrien		/* only 1 Rx queue unless RSS is enabled */
185236769Sobrien		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
186236769Sobrien			vsi->alloc_rxq = 1;
187236769Sobrien		} else {
188236769Sobrien			if (vsi->req_rxq) {
189253883Ssjg				vsi->alloc_rxq = vsi->req_rxq;
190236769Sobrien				vsi->num_rxq = vsi->req_rxq;
191236769Sobrien			} else {
192236769Sobrien				vsi->alloc_rxq = min3(pf->num_lan_msix,
193236769Sobrien						      ice_get_avail_rxq_count(pf),
194236769Sobrien						      (u16)num_online_cpus());
195236769Sobrien			}
196236769Sobrien		}
197236769Sobrien
198236769Sobrien		pf->num_lan_rx = vsi->alloc_rxq;
199236769Sobrien
200236769Sobrien		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
201236769Sobrien					   max_t(int, vsi->alloc_rxq,
202236769Sobrien						 vsi->alloc_txq));
203236769Sobrien		break;
204253883Ssjg	case ICE_VSI_VF:
205253883Ssjg		if (vf->num_req_qs)
206253883Ssjg			vf->num_vf_qs = vf->num_req_qs;
207253883Ssjg		vsi->alloc_txq = vf->num_vf_qs;
208253883Ssjg		vsi->alloc_rxq = vf->num_vf_qs;
209253883Ssjg		/* pf->vfs.num_msix_per includes (VF miscellaneous vector +
210253883Ssjg		 * data queue interrupts). Since vsi->num_q_vectors is number
211253883Ssjg		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
212253883Ssjg		 * original vector count
213253883Ssjg		 */
214253883Ssjg		vsi->num_q_vectors = vf->num_msix - ICE_NONQ_VECS_VF;
215253883Ssjg		break;
216253883Ssjg	case ICE_VSI_CTRL:
217253883Ssjg		vsi->alloc_txq = 1;
218253883Ssjg		vsi->alloc_rxq = 1;
219253883Ssjg		vsi->num_q_vectors = 1;
220253883Ssjg		break;
221253883Ssjg	case ICE_VSI_CHNL:
222253883Ssjg		vsi->alloc_txq = 0;
223253883Ssjg		vsi->alloc_rxq = 0;
224253883Ssjg		break;
225253883Ssjg	case ICE_VSI_LB:
226253883Ssjg		vsi->alloc_txq = 1;
227253883Ssjg		vsi->alloc_rxq = 1;
228253883Ssjg		break;
229253883Ssjg	default:
230253883Ssjg		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type);
231253883Ssjg		break;
232253883Ssjg	}
233253883Ssjg
234253883Ssjg	ice_vsi_set_num_desc(vsi);
235253883Ssjg}
236236769Sobrien
237236769Sobrien/**
238236769Sobrien * ice_get_free_slot - get the next non-NULL location index in array
239236769Sobrien * @array: array to search
240236769Sobrien * @size: size of the array
241236769Sobrien * @curr: last known occupied index to be used as a search hint
242236769Sobrien *
243236769Sobrien * void * is being used to keep the functionality generic. This lets us use this
244236769Sobrien * function on any array of pointers.
245236769Sobrien */
246236769Sobrienstatic int ice_get_free_slot(void *array, int size, int curr)
247236769Sobrien{
248236769Sobrien	int **tmp_array = (int **)array;
249236769Sobrien	int next;
250236769Sobrien
251236769Sobrien	if (curr < (size - 1) && !tmp_array[curr + 1]) {
252236769Sobrien		next = curr + 1;
253236769Sobrien	} else {
254236769Sobrien		int i = 0;
255236769Sobrien
256236769Sobrien		while ((i < size) && (tmp_array[i]))
257236769Sobrien			i++;
258236769Sobrien		if (i == size)
259236769Sobrien			next = ICE_NO_VSI;
260236769Sobrien		else
261236769Sobrien			next = i;
262236769Sobrien	}
263236769Sobrien	return next;
264236769Sobrien}
265236769Sobrien
266236769Sobrien/**
267236769Sobrien * ice_vsi_delete_from_hw - delete a VSI from the switch
268236769Sobrien * @vsi: pointer to VSI being removed
269236769Sobrien */
270236769Sobrienstatic void ice_vsi_delete_from_hw(struct ice_vsi *vsi)
271236769Sobrien{
272236769Sobrien	struct ice_pf *pf = vsi->back;
273236769Sobrien	struct ice_vsi_ctx *ctxt;
274236769Sobrien	int status;
275236769Sobrien
276236769Sobrien	ice_fltr_remove_all(vsi);
277236769Sobrien	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
278236769Sobrien	if (!ctxt)
279236769Sobrien		return;
280236769Sobrien
281236769Sobrien	if (vsi->type == ICE_VSI_VF)
282236769Sobrien		ctxt->vf_num = vsi->vf->vf_id;
283236769Sobrien	ctxt->vsi_num = vsi->vsi_num;
284236769Sobrien
285236769Sobrien	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
286236769Sobrien
287236769Sobrien	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
288236769Sobrien	if (status)
289236769Sobrien		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
290236769Sobrien			vsi->vsi_num, status);
291236769Sobrien
292236769Sobrien	kfree(ctxt);
293236769Sobrien}
294236769Sobrien
295236769Sobrien/**
296236769Sobrien * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
297236769Sobrien * @vsi: pointer to VSI being cleared
298236769Sobrien */
299236769Sobrienstatic void ice_vsi_free_arrays(struct ice_vsi *vsi)
300236769Sobrien{
301236769Sobrien	struct ice_pf *pf = vsi->back;
302236769Sobrien	struct device *dev;
303236769Sobrien
304236769Sobrien	dev = ice_pf_to_dev(pf);
305240330Smarcel
306240330Smarcel	/* free the ring and vector containers */
307240330Smarcel	devm_kfree(dev, vsi->q_vectors);
308236769Sobrien	vsi->q_vectors = NULL;
309236769Sobrien	devm_kfree(dev, vsi->tx_rings);
310236769Sobrien	vsi->tx_rings = NULL;
311236769Sobrien	devm_kfree(dev, vsi->rx_rings);
312236769Sobrien	vsi->rx_rings = NULL;
313236769Sobrien	devm_kfree(dev, vsi->txq_map);
314236769Sobrien	vsi->txq_map = NULL;
315236769Sobrien	devm_kfree(dev, vsi->rxq_map);
316236769Sobrien	vsi->rxq_map = NULL;
317236769Sobrien}
318236769Sobrien
319236769Sobrien/**
320236769Sobrien * ice_vsi_free_stats - Free the ring statistics structures
321236769Sobrien * @vsi: VSI pointer
322236769Sobrien */
323236769Sobrienstatic void ice_vsi_free_stats(struct ice_vsi *vsi)
324236769Sobrien{
325236769Sobrien	struct ice_vsi_stats *vsi_stat;
326236769Sobrien	struct ice_pf *pf = vsi->back;
327236769Sobrien	int i;
328236769Sobrien
329236769Sobrien	if (vsi->type == ICE_VSI_CHNL)
330236769Sobrien		return;
331236769Sobrien	if (!pf->vsi_stats)
332236769Sobrien		return;
333236769Sobrien
334236769Sobrien	vsi_stat = pf->vsi_stats[vsi->idx];
335236769Sobrien	if (!vsi_stat)
336236769Sobrien		return;
337236769Sobrien
338236769Sobrien	ice_for_each_alloc_txq(vsi, i) {
339236769Sobrien		if (vsi_stat->tx_ring_stats[i]) {
340236769Sobrien			kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
341236769Sobrien			WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
342236769Sobrien		}
343236769Sobrien	}
344236769Sobrien
345236769Sobrien	ice_for_each_alloc_rxq(vsi, i) {
346236769Sobrien		if (vsi_stat->rx_ring_stats[i]) {
347236769Sobrien			kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
348236769Sobrien			WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
349236769Sobrien		}
350236769Sobrien	}
351236769Sobrien
352236769Sobrien	kfree(vsi_stat->tx_ring_stats);
353236769Sobrien	kfree(vsi_stat->rx_ring_stats);
354236769Sobrien	kfree(vsi_stat);
355236769Sobrien	pf->vsi_stats[vsi->idx] = NULL;
356236769Sobrien}
357236769Sobrien
358236769Sobrien/**
359236769Sobrien * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
360236769Sobrien * @vsi: VSI which is having stats allocated
361236769Sobrien */
362236769Sobrienstatic int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
363236769Sobrien{
364236769Sobrien	struct ice_ring_stats **tx_ring_stats;
365236769Sobrien	struct ice_ring_stats **rx_ring_stats;
366236769Sobrien	struct ice_vsi_stats *vsi_stats;
367236769Sobrien	struct ice_pf *pf = vsi->back;
368236769Sobrien	u16 i;
369236769Sobrien
370236769Sobrien	vsi_stats = pf->vsi_stats[vsi->idx];
371236769Sobrien	tx_ring_stats = vsi_stats->tx_ring_stats;
372236769Sobrien	rx_ring_stats = vsi_stats->rx_ring_stats;
373236769Sobrien
374236769Sobrien	/* Allocate Tx ring stats */
375236769Sobrien	ice_for_each_alloc_txq(vsi, i) {
376236769Sobrien		struct ice_ring_stats *ring_stats;
377236769Sobrien		struct ice_tx_ring *ring;
378236769Sobrien
379236769Sobrien		ring = vsi->tx_rings[i];
380236769Sobrien		ring_stats = tx_ring_stats[i];
381236769Sobrien
382236769Sobrien		if (!ring_stats) {
383236769Sobrien			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
384236769Sobrien			if (!ring_stats)
385236769Sobrien				goto err_out;
386236769Sobrien
387236769Sobrien			WRITE_ONCE(tx_ring_stats[i], ring_stats);
388253883Ssjg		}
389236769Sobrien
390236769Sobrien		ring->ring_stats = ring_stats;
391236769Sobrien	}
392236769Sobrien
393236769Sobrien	/* Allocate Rx ring stats */
394236769Sobrien	ice_for_each_alloc_rxq(vsi, i) {
395236769Sobrien		struct ice_ring_stats *ring_stats;
396236769Sobrien		struct ice_rx_ring *ring;
397236769Sobrien
398236769Sobrien		ring = vsi->rx_rings[i];
399236769Sobrien		ring_stats = rx_ring_stats[i];
400236769Sobrien
401236769Sobrien		if (!ring_stats) {
402236769Sobrien			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
403236769Sobrien			if (!ring_stats)
404236769Sobrien				goto err_out;
405236769Sobrien
406236769Sobrien			WRITE_ONCE(rx_ring_stats[i], ring_stats);
407236769Sobrien		}
408236769Sobrien
409236769Sobrien		ring->ring_stats = ring_stats;
410236769Sobrien	}
411236769Sobrien
412236769Sobrien	return 0;
413236769Sobrien
414236769Sobrienerr_out:
415236769Sobrien	ice_vsi_free_stats(vsi);
416236769Sobrien	return -ENOMEM;
417236769Sobrien}
418236769Sobrien
419236769Sobrien/**
420236769Sobrien * ice_vsi_free - clean up and deallocate the provided VSI
421236769Sobrien * @vsi: pointer to VSI being cleared
422236769Sobrien *
423236769Sobrien * This deallocates the VSI's queue resources, removes it from the PF's
424236769Sobrien * VSI array if necessary, and deallocates the VSI
425236769Sobrien */
426236769Sobrienstatic void ice_vsi_free(struct ice_vsi *vsi)
427236769Sobrien{
428236769Sobrien	struct ice_pf *pf = NULL;
429236769Sobrien	struct device *dev;
430236769Sobrien
431236769Sobrien	if (!vsi || !vsi->back)
432236769Sobrien		return;
433236769Sobrien
434236769Sobrien	pf = vsi->back;
435236769Sobrien	dev = ice_pf_to_dev(pf);
436236769Sobrien
437236769Sobrien	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
438236769Sobrien		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
439236769Sobrien		return;
440236769Sobrien	}
441236769Sobrien
442236769Sobrien	mutex_lock(&pf->sw_mutex);
443236769Sobrien	/* updates the PF for this cleared VSI */
444236769Sobrien
445236769Sobrien	pf->vsi[vsi->idx] = NULL;
446236769Sobrien	pf->next_vsi = vsi->idx;
447236769Sobrien
448236769Sobrien	ice_vsi_free_stats(vsi);
449236769Sobrien	ice_vsi_free_arrays(vsi);
450236769Sobrien	mutex_unlock(&pf->sw_mutex);
451236769Sobrien	devm_kfree(dev, vsi);
452236769Sobrien}
453236769Sobrien
454236769Sobrienvoid ice_vsi_delete(struct ice_vsi *vsi)
455236769Sobrien{
456236769Sobrien	ice_vsi_delete_from_hw(vsi);
457236769Sobrien	ice_vsi_free(vsi);
458236769Sobrien}
459236769Sobrien
460236769Sobrien/**
461236769Sobrien * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
462236769Sobrien * @irq: interrupt number
463236769Sobrien * @data: pointer to a q_vector
464236769Sobrien */
465236769Sobrienstatic irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
466236769Sobrien{
467236769Sobrien	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
468236769Sobrien
469236769Sobrien	if (!q_vector->tx.tx_ring)
470236769Sobrien		return IRQ_HANDLED;
471236769Sobrien
472236769Sobrien#define FDIR_RX_DESC_CLEAN_BUDGET 64
473236769Sobrien	ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
474236769Sobrien	ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
475236769Sobrien
476236769Sobrien	return IRQ_HANDLED;
477236769Sobrien}
478236769Sobrien
479236769Sobrien/**
480236769Sobrien * ice_msix_clean_rings - MSIX mode Interrupt Handler
481236769Sobrien * @irq: interrupt number
482236769Sobrien * @data: pointer to a q_vector
483236769Sobrien */
484236769Sobrienstatic irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
485236769Sobrien{
486236769Sobrien	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
487236769Sobrien
488236769Sobrien	if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
489236769Sobrien		return IRQ_HANDLED;
490236769Sobrien
491236769Sobrien	q_vector->total_events++;
492236769Sobrien
493236769Sobrien	napi_schedule(&q_vector->napi);
494236769Sobrien
495236769Sobrien	return IRQ_HANDLED;
496236769Sobrien}
497236769Sobrien
498236769Sobrien/**
499236769Sobrien * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
500236769Sobrien * @vsi: VSI pointer
501236769Sobrien */
502236769Sobrienstatic int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
503236769Sobrien{
504236769Sobrien	struct ice_vsi_stats *vsi_stat;
505236769Sobrien	struct ice_pf *pf = vsi->back;
506236769Sobrien
507236769Sobrien	if (vsi->type == ICE_VSI_CHNL)
508236769Sobrien		return 0;
509236769Sobrien	if (!pf->vsi_stats)
510236769Sobrien		return -ENOENT;
511236769Sobrien
512236769Sobrien	if (pf->vsi_stats[vsi->idx])
513236769Sobrien	/* realloc will happen in rebuild path */
514236769Sobrien		return 0;
515236769Sobrien
516236769Sobrien	vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
517236769Sobrien	if (!vsi_stat)
518236769Sobrien		return -ENOMEM;
519236769Sobrien
520236769Sobrien	vsi_stat->tx_ring_stats =
521236769Sobrien		kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
522236769Sobrien			GFP_KERNEL);
523236769Sobrien	if (!vsi_stat->tx_ring_stats)
524236769Sobrien		goto err_alloc_tx;
525236769Sobrien
526236769Sobrien	vsi_stat->rx_ring_stats =
527236769Sobrien		kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
528236769Sobrien			GFP_KERNEL);
529236769Sobrien	if (!vsi_stat->rx_ring_stats)
530236769Sobrien		goto err_alloc_rx;
531236769Sobrien
532236769Sobrien	pf->vsi_stats[vsi->idx] = vsi_stat;
533236769Sobrien
534236769Sobrien	return 0;
535236769Sobrien
536236769Sobrienerr_alloc_rx:
537236769Sobrien	kfree(vsi_stat->rx_ring_stats);
538236769Sobrienerr_alloc_tx:
539236769Sobrien	kfree(vsi_stat->tx_ring_stats);
540236769Sobrien	kfree(vsi_stat);
541236769Sobrien	pf->vsi_stats[vsi->idx] = NULL;
542236769Sobrien	return -ENOMEM;
543236769Sobrien}
544236769Sobrien
545236769Sobrien/**
546236769Sobrien * ice_vsi_alloc_def - set default values for already allocated VSI
547236769Sobrien * @vsi: ptr to VSI
548236769Sobrien * @ch: ptr to channel
549236769Sobrien */
550236769Sobrienstatic int
551236769Sobrienice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
552236769Sobrien{
553236769Sobrien	if (vsi->type != ICE_VSI_CHNL) {
554236769Sobrien		ice_vsi_set_num_qs(vsi);
555236769Sobrien		if (ice_vsi_alloc_arrays(vsi))
556236769Sobrien			return -ENOMEM;
557236769Sobrien	}
558236769Sobrien
559236769Sobrien	switch (vsi->type) {
560236769Sobrien	case ICE_VSI_PF:
561236769Sobrien		/* Setup default MSIX irq handler for VSI */
562236769Sobrien		vsi->irq_handler = ice_msix_clean_rings;
563236769Sobrien		break;
564236769Sobrien	case ICE_VSI_CTRL:
565236769Sobrien		/* Setup ctrl VSI MSIX irq handler */
566236769Sobrien		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
567236769Sobrien		break;
568236769Sobrien	case ICE_VSI_CHNL:
569236769Sobrien		if (!ch)
570236769Sobrien			return -EINVAL;
571236769Sobrien
572236769Sobrien		vsi->num_rxq = ch->num_rxq;
573236769Sobrien		vsi->num_txq = ch->num_txq;
574236769Sobrien		vsi->next_base_q = ch->base_q;
575236769Sobrien		break;
576236769Sobrien	case ICE_VSI_VF:
577236769Sobrien	case ICE_VSI_LB:
578236769Sobrien		break;
579236769Sobrien	default:
580236769Sobrien		ice_vsi_free_arrays(vsi);
581236769Sobrien		return -EINVAL;
582236769Sobrien	}
583236769Sobrien
584236769Sobrien	return 0;
585236769Sobrien}
586236769Sobrien
587236769Sobrien/**
588236769Sobrien * ice_vsi_alloc - Allocates the next available struct VSI in the PF
589236769Sobrien * @pf: board private structure
590236769Sobrien *
591236769Sobrien * Reserves a VSI index from the PF and allocates an empty VSI structure
592236769Sobrien * without a type. The VSI structure must later be initialized by calling
593236769Sobrien * ice_vsi_cfg().
594236769Sobrien *
595236769Sobrien * returns a pointer to a VSI on success, NULL on failure.
596253883Ssjg */
597253883Ssjgstatic struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
598253883Ssjg{
599253883Ssjg	struct device *dev = ice_pf_to_dev(pf);
600236769Sobrien	struct ice_vsi *vsi = NULL;
601236769Sobrien
602236769Sobrien	/* Need to protect the allocation of the VSIs at the PF level */
603236769Sobrien	mutex_lock(&pf->sw_mutex);
604236769Sobrien
605236769Sobrien	/* If we have already allocated our maximum number of VSIs,
606236769Sobrien	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
607236769Sobrien	 * is available to be populated
608236769Sobrien	 */
609236769Sobrien	if (pf->next_vsi == ICE_NO_VSI) {
610236769Sobrien		dev_dbg(dev, "out of VSI slots!\n");
611236769Sobrien		goto unlock_pf;
612236769Sobrien	}
613236769Sobrien
614236769Sobrien	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
615236769Sobrien	if (!vsi)
616236769Sobrien		goto unlock_pf;
617236769Sobrien
618236769Sobrien	vsi->back = pf;
619236769Sobrien	set_bit(ICE_VSI_DOWN, vsi->state);
620236769Sobrien
621236769Sobrien	/* fill slot and make note of the index */
622236769Sobrien	vsi->idx = pf->next_vsi;
623236769Sobrien	pf->vsi[pf->next_vsi] = vsi;
624236769Sobrien
625236769Sobrien	/* prepare pf->next_vsi for next use */
626236769Sobrien	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
627236769Sobrien					 pf->next_vsi);
628236769Sobrien
629236769Sobrienunlock_pf:
630236769Sobrien	mutex_unlock(&pf->sw_mutex);
631236769Sobrien	return vsi;
632236769Sobrien}
633236769Sobrien
634236769Sobrien/**
635236769Sobrien * ice_alloc_fd_res - Allocate FD resource for a VSI
636236769Sobrien * @vsi: pointer to the ice_vsi
637236769Sobrien *
638236769Sobrien * This allocates the FD resources
639236769Sobrien *
640236769Sobrien * Returns 0 on success, -EPERM on no-op or -EIO on failure
641236769Sobrien */
642236769Sobrienstatic int ice_alloc_fd_res(struct ice_vsi *vsi)
643236769Sobrien{
644236769Sobrien	struct ice_pf *pf = vsi->back;
645236769Sobrien	u32 g_val, b_val;
646236769Sobrien
647236769Sobrien	/* Flow Director filters are only allocated/assigned to the PF VSI or
648236769Sobrien	 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
649236769Sobrien	 * add/delete filters so resources are not allocated to it
650236769Sobrien	 */
651236769Sobrien	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
652236769Sobrien		return -EPERM;
653236769Sobrien
654236769Sobrien	if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
655236769Sobrien	      vsi->type == ICE_VSI_CHNL))
656236769Sobrien		return -EPERM;
657236769Sobrien
658236769Sobrien	/* FD filters from guaranteed pool per VSI */
659236769Sobrien	g_val = pf->hw.func_caps.fd_fltr_guar;
660236769Sobrien	if (!g_val)
661236769Sobrien		return -EPERM;
662236769Sobrien
663236769Sobrien	/* FD filters from best effort pool */
664236769Sobrien	b_val = pf->hw.func_caps.fd_fltr_best_effort;
665236769Sobrien	if (!b_val)
666236769Sobrien		return -EPERM;
667236769Sobrien
668236769Sobrien	/* PF main VSI gets only 64 FD resources from guaranteed pool
669236769Sobrien	 * when ADQ is configured.
670236769Sobrien	 */
671236769Sobrien#define ICE_PF_VSI_GFLTR	64
672236769Sobrien
673236769Sobrien	/* determine FD filter resources per VSI from shared(best effort) and
674236769Sobrien	 * dedicated pool
675236769Sobrien	 */
676236769Sobrien	if (vsi->type == ICE_VSI_PF) {
677236769Sobrien		vsi->num_gfltr = g_val;
678236769Sobrien		/* if MQPRIO is configured, main VSI doesn't get all FD
679236769Sobrien		 * resources from guaranteed pool. PF VSI gets 64 FD resources
680236769Sobrien		 */
681236769Sobrien		if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
682236769Sobrien			if (g_val < ICE_PF_VSI_GFLTR)
683236769Sobrien				return -EPERM;
684236769Sobrien			/* allow bare minimum entries for PF VSI */
685236769Sobrien			vsi->num_gfltr = ICE_PF_VSI_GFLTR;
686236769Sobrien		}
687236769Sobrien
688236769Sobrien		/* each VSI gets same "best_effort" quota */
689236769Sobrien		vsi->num_bfltr = b_val;
690236769Sobrien	} else if (vsi->type == ICE_VSI_VF) {
691236769Sobrien		vsi->num_gfltr = 0;
692236769Sobrien
693236769Sobrien		/* each VSI gets same "best_effort" quota */
694236769Sobrien		vsi->num_bfltr = b_val;
695236769Sobrien	} else {
696236769Sobrien		struct ice_vsi *main_vsi;
697236769Sobrien		int numtc;
698236769Sobrien
699236769Sobrien		main_vsi = ice_get_main_vsi(pf);
700236769Sobrien		if (!main_vsi)
701236769Sobrien			return -EPERM;
702236769Sobrien
703236769Sobrien		if (!main_vsi->all_numtc)
704236769Sobrien			return -EINVAL;
705236769Sobrien
706236769Sobrien		/* figure out ADQ numtc */
707236769Sobrien		numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
708236769Sobrien
709236769Sobrien		/* only one TC but still asking resources for channels,
710236769Sobrien		 * invalid config
711236769Sobrien		 */
712236769Sobrien		if (numtc < ICE_CHNL_START_TC)
713236769Sobrien			return -EPERM;
714236769Sobrien
715236769Sobrien		g_val -= ICE_PF_VSI_GFLTR;
716236769Sobrien		/* channel VSIs gets equal share from guaranteed pool */
717236769Sobrien		vsi->num_gfltr = g_val / numtc;
718236769Sobrien
719236769Sobrien		/* each VSI gets same "best_effort" quota */
720236769Sobrien		vsi->num_bfltr = b_val;
721236769Sobrien	}
722236769Sobrien
723236769Sobrien	return 0;
724236769Sobrien}
725236769Sobrien
726236769Sobrien/**
727236769Sobrien * ice_vsi_get_qs - Assign queues from PF to VSI
728236769Sobrien * @vsi: the VSI to assign queues to
729236769Sobrien *
730236769Sobrien * Returns 0 on success and a negative value on error
731236769Sobrien */
732236769Sobrienstatic int ice_vsi_get_qs(struct ice_vsi *vsi)
733236769Sobrien{
734236769Sobrien	struct ice_pf *pf = vsi->back;
735236769Sobrien	struct ice_qs_cfg tx_qs_cfg = {
736236769Sobrien		.qs_mutex = &pf->avail_q_mutex,
737236769Sobrien		.pf_map = pf->avail_txqs,
738236769Sobrien		.pf_map_size = pf->max_pf_txqs,
739236769Sobrien		.q_count = vsi->alloc_txq,
740236769Sobrien		.scatter_count = ICE_MAX_SCATTER_TXQS,
741236769Sobrien		.vsi_map = vsi->txq_map,
742236769Sobrien		.vsi_map_offset = 0,
743236769Sobrien		.mapping_mode = ICE_VSI_MAP_CONTIG
744236769Sobrien	};
745236769Sobrien	struct ice_qs_cfg rx_qs_cfg = {
746236769Sobrien		.qs_mutex = &pf->avail_q_mutex,
747236769Sobrien		.pf_map = pf->avail_rxqs,
748236769Sobrien		.pf_map_size = pf->max_pf_rxqs,
749236769Sobrien		.q_count = vsi->alloc_rxq,
750236769Sobrien		.scatter_count = ICE_MAX_SCATTER_RXQS,
751236769Sobrien		.vsi_map = vsi->rxq_map,
752236769Sobrien		.vsi_map_offset = 0,
753236769Sobrien		.mapping_mode = ICE_VSI_MAP_CONTIG
754236769Sobrien	};
755236769Sobrien	int ret;
756236769Sobrien
757236769Sobrien	if (vsi->type == ICE_VSI_CHNL)
758236769Sobrien		return 0;
759236769Sobrien
760236769Sobrien	ret = __ice_vsi_get_qs(&tx_qs_cfg);
761236769Sobrien	if (ret)
762236769Sobrien		return ret;
763236769Sobrien	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
764236769Sobrien
765236769Sobrien	ret = __ice_vsi_get_qs(&rx_qs_cfg);
766236769Sobrien	if (ret)
767236769Sobrien		return ret;
768236769Sobrien	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
769236769Sobrien
770236769Sobrien	return 0;
771236769Sobrien}
772236769Sobrien
773236769Sobrien/**
774236769Sobrien * ice_vsi_put_qs - Release queues from VSI to PF
775236769Sobrien * @vsi: the VSI that is going to release queues
776236769Sobrien */
777237578Sobrienstatic void ice_vsi_put_qs(struct ice_vsi *vsi)
778236769Sobrien{
779236769Sobrien	struct ice_pf *pf = vsi->back;
780236769Sobrien	int i;
781236769Sobrien
782236769Sobrien	mutex_lock(&pf->avail_q_mutex);
783236769Sobrien
784236769Sobrien	ice_for_each_alloc_txq(vsi, i) {
785236769Sobrien		clear_bit(vsi->txq_map[i], pf->avail_txqs);
786236769Sobrien		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
787236769Sobrien	}
788236769Sobrien
789236769Sobrien	ice_for_each_alloc_rxq(vsi, i) {
790236769Sobrien		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
791236769Sobrien		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
792236769Sobrien	}
793236769Sobrien
794236769Sobrien	mutex_unlock(&pf->avail_q_mutex);
795236769Sobrien}
796236769Sobrien
797236769Sobrien/**
798236769Sobrien * ice_is_safe_mode
799236769Sobrien * @pf: pointer to the PF struct
800236769Sobrien *
801236769Sobrien * returns true if driver is in safe mode, false otherwise
802236769Sobrien */
803236769Sobrienbool ice_is_safe_mode(struct ice_pf *pf)
804236769Sobrien{
805236769Sobrien	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
806236769Sobrien}
807236769Sobrien
808249033Ssjg/**
809236769Sobrien * ice_is_rdma_ena
810236769Sobrien * @pf: pointer to the PF struct
811236769Sobrien *
812236769Sobrien * returns true if RDMA is currently supported, false otherwise
813236769Sobrien */
814236769Sobrienbool ice_is_rdma_ena(struct ice_pf *pf)
815236769Sobrien{
816236769Sobrien	return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
817236769Sobrien}
818236769Sobrien
819236769Sobrien/**
820236769Sobrien * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
821236769Sobrien * @vsi: the VSI being cleaned up
822236769Sobrien *
823236769Sobrien * This function deletes RSS input set for all flows that were configured
824236769Sobrien * for this VSI
825236769Sobrien */
826236769Sobrienstatic void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
827236769Sobrien{
828236769Sobrien	struct ice_pf *pf = vsi->back;
829236769Sobrien	int status;
830236769Sobrien
831236769Sobrien	if (ice_is_safe_mode(pf))
832236769Sobrien		return;
833236769Sobrien
834236769Sobrien	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
835236769Sobrien	if (status)
836236769Sobrien		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
837236769Sobrien			vsi->vsi_num, status);
838250750Ssjg}
839236769Sobrien
840236769Sobrien/**
841236769Sobrien * ice_rss_clean - Delete RSS related VSI structures and configuration
842236769Sobrien * @vsi: the VSI being removed
843236769Sobrien */
844236769Sobrienstatic void ice_rss_clean(struct ice_vsi *vsi)
845236769Sobrien{
846236769Sobrien	struct ice_pf *pf = vsi->back;
847236769Sobrien	struct device *dev;
848236769Sobrien
849236769Sobrien	dev = ice_pf_to_dev(pf);
850236769Sobrien
851236769Sobrien	devm_kfree(dev, vsi->rss_hkey_user);
852236769Sobrien	devm_kfree(dev, vsi->rss_lut_user);
853236769Sobrien
854236769Sobrien	ice_vsi_clean_rss_flow_fld(vsi);
855236769Sobrien	/* remove RSS replay list */
856236769Sobrien	if (!ice_is_safe_mode(pf))
857236769Sobrien		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
858236769Sobrien}
859236769Sobrien
860236769Sobrien/**
861236769Sobrien * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
862236769Sobrien * @vsi: the VSI being configured
863236769Sobrien */
864236769Sobrienstatic void ice_vsi_set_rss_params(struct ice_vsi *vsi)
865236769Sobrien{
866236769Sobrien	struct ice_hw_common_caps *cap;
867236769Sobrien	struct ice_pf *pf = vsi->back;
868236769Sobrien	u16 max_rss_size;
869236769Sobrien
870236769Sobrien	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
871236769Sobrien		vsi->rss_size = 1;
872249033Ssjg		return;
873236769Sobrien	}
874236769Sobrien
875236769Sobrien	cap = &pf->hw.func_caps.common_cap;
876236769Sobrien	max_rss_size = BIT(cap->rss_table_entry_width);
877236769Sobrien	switch (vsi->type) {
878236769Sobrien	case ICE_VSI_CHNL:
879236769Sobrien	case ICE_VSI_PF:
880236769Sobrien		/* PF VSI will inherit RSS instance of PF */
881236769Sobrien		vsi->rss_table_size = (u16)cap->rss_table_size;
882236769Sobrien		if (vsi->type == ICE_VSI_CHNL)
883236769Sobrien			vsi->rss_size = min_t(u16, vsi->num_rxq, max_rss_size);
884236769Sobrien		else
885236769Sobrien			vsi->rss_size = min_t(u16, num_online_cpus(),
886249033Ssjg					      max_rss_size);
887249033Ssjg		vsi->rss_lut_type = ICE_LUT_PF;
888249033Ssjg		break;
889249033Ssjg	case ICE_VSI_VF:
890249033Ssjg		/* VF VSI will get a small RSS table.
891249033Ssjg		 * For VSI_LUT, LUT size should be set to 64 bytes.
892236769Sobrien		 */
893236769Sobrien		vsi->rss_table_size = ICE_LUT_VSI_SIZE;
894236769Sobrien		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
895236769Sobrien		vsi->rss_lut_type = ICE_LUT_VSI;
896236769Sobrien		break;
897236769Sobrien	case ICE_VSI_LB:
898236769Sobrien		break;
899236769Sobrien	default:
900236769Sobrien		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
901236769Sobrien			ice_vsi_type_str(vsi->type));
902236769Sobrien		break;
903236769Sobrien	}
904236769Sobrien}
905236769Sobrien
906236769Sobrien/**
907236769Sobrien * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
908236769Sobrien * @hw: HW structure used to determine the VLAN mode of the device
909236769Sobrien * @ctxt: the VSI context being set
910236769Sobrien *
911236769Sobrien * This initializes a default VSI context for all sections except the Queues.
912236769Sobrien */
913236769Sobrienstatic void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
914236769Sobrien{
915236769Sobrien	u32 table = 0;
916236769Sobrien
917236769Sobrien	memset(&ctxt->info, 0, sizeof(ctxt->info));
918236769Sobrien	/* VSI's should be allocated from shared pool */
919236769Sobrien	ctxt->alloc_from_pool = true;
920236769Sobrien	/* Src pruning enabled by default */
921236769Sobrien	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
922236769Sobrien	/* Traffic from VSI can be sent to LAN */
923236769Sobrien	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
924236769Sobrien	/* allow all untagged/tagged packets by default on Tx */
925236769Sobrien	ctxt->info.inner_vlan_flags = FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_TX_MODE_M,
926236769Sobrien						 ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL);
927236769Sobrien	/* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
928236769Sobrien	 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
929236769Sobrien	 *
930236769Sobrien	 * DVM - leave inner VLAN in packet by default
931249033Ssjg	 */
932236769Sobrien	if (ice_is_dvm_ena(hw)) {
933236769Sobrien		ctxt->info.inner_vlan_flags |=
934236769Sobrien			FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M,
935236769Sobrien				   ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING);
936236769Sobrien		ctxt->info.outer_vlan_flags =
937236769Sobrien			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M,
938236769Sobrien				   ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL);
939236769Sobrien		ctxt->info.outer_vlan_flags |=
940236769Sobrien			FIELD_PREP(ICE_AQ_VSI_OUTER_TAG_TYPE_M,
941236769Sobrien				   ICE_AQ_VSI_OUTER_TAG_VLAN_8100);
942236769Sobrien		ctxt->info.outer_vlan_flags |=
943236769Sobrien			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
944236769Sobrien				   ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
945236769Sobrien	}
946236769Sobrien	/* Have 1:1 UP mapping for both ingress/egress tables */
947236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
948236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
949236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
950236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
951240330Smarcel	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
952236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
953236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
954236769Sobrien	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
955236769Sobrien	ctxt->info.ingress_table = cpu_to_le32(table);
956236769Sobrien	ctxt->info.egress_table = cpu_to_le32(table);
957236769Sobrien	/* Have 1:1 UP mapping for outer to inner UP table */
958236769Sobrien	ctxt->info.outer_up_table = cpu_to_le32(table);
959236769Sobrien	/* No Outer tag support outer_tag_flags remains to zero */
960236769Sobrien}
961236769Sobrien
962236769Sobrien/**
963236769Sobrien * ice_vsi_setup_q_map - Setup a VSI queue map
964236769Sobrien * @vsi: the VSI being configured
965236769Sobrien * @ctxt: VSI context structure
966236769Sobrien */
967236769Sobrienstatic int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
968236769Sobrien{
969236769Sobrien	u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
970236769Sobrien	u16 num_txq_per_tc, num_rxq_per_tc;
971236769Sobrien	u16 qcount_tx = vsi->alloc_txq;
972236769Sobrien	u16 qcount_rx = vsi->alloc_rxq;
973236769Sobrien	u8 netdev_tc = 0;
974236769Sobrien	int i;
975236769Sobrien
976236769Sobrien	if (!vsi->tc_cfg.numtc) {
977236769Sobrien		/* at least TC0 should be enabled by default */
978236769Sobrien		vsi->tc_cfg.numtc = 1;
979236769Sobrien		vsi->tc_cfg.ena_tc = 1;
980236769Sobrien	}
981236769Sobrien
982236769Sobrien	num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
983236769Sobrien	if (!num_rxq_per_tc)
984236769Sobrien		num_rxq_per_tc = 1;
985236769Sobrien	num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
986236769Sobrien	if (!num_txq_per_tc)
987236769Sobrien		num_txq_per_tc = 1;
988236769Sobrien
989236769Sobrien	/* find the (rounded up) power-of-2 of qcount */
990236769Sobrien	pow = (u16)order_base_2(num_rxq_per_tc);
991236769Sobrien
992236769Sobrien	/* TC mapping is a function of the number of Rx queues assigned to the
993236769Sobrien	 * VSI for each traffic class and the offset of these queues.
994236769Sobrien	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
995236769Sobrien	 * queues allocated to TC0. No:of queues is a power-of-2.
996236769Sobrien	 *
997236769Sobrien	 * If TC is not enabled, the queue offset is set to 0, and allocate one
998236769Sobrien	 * queue, this way, traffic for the given TC will be sent to the default
999236769Sobrien	 * queue.
1000236769Sobrien	 *
1001236769Sobrien	 * Setup number and offset of Rx queues for all TCs for the VSI
1002236769Sobrien	 */
1003236769Sobrien	ice_for_each_traffic_class(i) {
1004236769Sobrien		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1005253883Ssjg			/* TC is not enabled */
1006253883Ssjg			vsi->tc_cfg.tc_info[i].qoffset = 0;
1007236769Sobrien			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1008236769Sobrien			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1009236769Sobrien			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1010236769Sobrien			ctxt->info.tc_mapping[i] = 0;
1011236769Sobrien			continue;
1012253883Ssjg		}
1013236769Sobrien
1014253883Ssjg		/* TC is enabled */
1015253883Ssjg		vsi->tc_cfg.tc_info[i].qoffset = offset;
1016253883Ssjg		vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1017253883Ssjg		vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1018253883Ssjg		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1019236769Sobrien
1020236769Sobrien		qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1021236769Sobrien		qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
1022236769Sobrien		offset += num_rxq_per_tc;
1023236769Sobrien		tx_count += num_txq_per_tc;
1024253883Ssjg		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1025253883Ssjg	}
1026253883Ssjg
1027253883Ssjg	/* if offset is non-zero, means it is calculated correctly based on
1028253883Ssjg	 * enabled TCs for a given VSI otherwise qcount_rx will always
1029236769Sobrien	 * be correct and non-zero because it is based off - VSI's
1030249033Ssjg	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1031249033Ssjg	 * at least 1)
1032249033Ssjg	 */
1033236769Sobrien	if (offset)
1034236769Sobrien		rx_count = offset;
1035236769Sobrien	else
1036236769Sobrien		rx_count = num_rxq_per_tc;
1037236769Sobrien
1038236769Sobrien	if (rx_count > vsi->alloc_rxq) {
1039253883Ssjg		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1040253883Ssjg			rx_count, vsi->alloc_rxq);
1041253883Ssjg		return -EINVAL;
1042236769Sobrien	}
1043236769Sobrien
1044236769Sobrien	if (tx_count > vsi->alloc_txq) {
1045236769Sobrien		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1046236769Sobrien			tx_count, vsi->alloc_txq);
1047236769Sobrien		return -EINVAL;
1048236769Sobrien	}
1049236769Sobrien
1050236769Sobrien	vsi->num_txq = tx_count;
1051236769Sobrien	vsi->num_rxq = rx_count;
1052236769Sobrien
1053236769Sobrien	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1054236769Sobrien		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1055236769Sobrien		/* since there is a chance that num_rxq could have been changed
1056236769Sobrien		 * in the above for loop, make num_txq equal to num_rxq.
1057236769Sobrien		 */
1058253883Ssjg		vsi->num_txq = vsi->num_rxq;
1059253883Ssjg	}
1060253883Ssjg
1061236769Sobrien	/* Rx queue mapping */
1062236769Sobrien	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1063236769Sobrien	/* q_mapping buffer holds the info for the first queue allocated for
1064236769Sobrien	 * this VSI in the PF space and also the number of queues associated
1065236769Sobrien	 * with this VSI.
1066236769Sobrien	 */
1067236769Sobrien	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1068236769Sobrien	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1069236769Sobrien
1070236769Sobrien	return 0;
1071236769Sobrien}
1072236769Sobrien
1073236769Sobrien/**
1074236769Sobrien * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1075236769Sobrien * @ctxt: the VSI context being set
1076236769Sobrien * @vsi: the VSI being configured
1077236769Sobrien */
1078236769Sobrienstatic void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1079236769Sobrien{
1080236769Sobrien	u8 dflt_q_group, dflt_q_prio;
1081236769Sobrien	u16 dflt_q, report_q, val;
1082253883Ssjg
1083253883Ssjg	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1084236769Sobrien	    vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1085253883Ssjg		return;
1086253883Ssjg
1087253883Ssjg	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1088253883Ssjg	ctxt->info.valid_sections |= cpu_to_le16(val);
1089253883Ssjg	dflt_q = 0;
1090253883Ssjg	dflt_q_group = 0;
1091253883Ssjg	report_q = 0;
1092253883Ssjg	dflt_q_prio = 0;
1093253883Ssjg
1094253883Ssjg	/* enable flow director filtering/programming */
1095236769Sobrien	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1096236769Sobrien	ctxt->info.fd_options = cpu_to_le16(val);
1097236769Sobrien	/* max of allocated flow director filters */
1098236769Sobrien	ctxt->info.max_fd_fltr_dedicated =
1099236769Sobrien			cpu_to_le16(vsi->num_gfltr);
1100236769Sobrien	/* max of shared flow director filters any VSI may program */
1101236769Sobrien	ctxt->info.max_fd_fltr_shared =
1102236769Sobrien			cpu_to_le16(vsi->num_bfltr);
1103236769Sobrien	/* default queue index within the VSI of the default FD */
1104236769Sobrien	val = FIELD_PREP(ICE_AQ_VSI_FD_DEF_Q_M, dflt_q);
1105236769Sobrien	/* target queue or queue group to the FD filter */
1106236769Sobrien	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_GRP_M, dflt_q_group);
1107236769Sobrien	ctxt->info.fd_def_q = cpu_to_le16(val);
1108236769Sobrien	/* queue index on which FD filter completion is reported */
1109236769Sobrien	val = FIELD_PREP(ICE_AQ_VSI_FD_REPORT_Q_M, report_q);
1110236769Sobrien	/* priority of the default qindex action */
1111236769Sobrien	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_PRIORITY_M, dflt_q_prio);
1112236769Sobrien	ctxt->info.fd_report_opt = cpu_to_le16(val);
1113236769Sobrien}
1114236769Sobrien
1115236769Sobrien/**
1116236769Sobrien * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1117236769Sobrien * @ctxt: the VSI context being set
1118236769Sobrien * @vsi: the VSI being configured
1119236769Sobrien */
1120236769Sobrienstatic void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1121236769Sobrien{
1122236769Sobrien	u8 lut_type, hash_type;
1123236769Sobrien	struct device *dev;
1124236769Sobrien	struct ice_pf *pf;
1125236769Sobrien
1126236769Sobrien	pf = vsi->back;
1127236769Sobrien	dev = ice_pf_to_dev(pf);
1128236769Sobrien
1129236769Sobrien	switch (vsi->type) {
1130236769Sobrien	case ICE_VSI_CHNL:
1131236769Sobrien	case ICE_VSI_PF:
1132236769Sobrien		/* PF VSI will inherit RSS instance of PF */
1133236769Sobrien		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1134236769Sobrien		break;
1135236769Sobrien	case ICE_VSI_VF:
1136236769Sobrien		/* VF VSI will gets a small RSS table which is a VSI LUT type */
1137236769Sobrien		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1138236769Sobrien		break;
1139236769Sobrien	default:
1140236769Sobrien		dev_dbg(dev, "Unsupported VSI type %s\n",
1141236769Sobrien			ice_vsi_type_str(vsi->type));
1142236769Sobrien		return;
1143236769Sobrien	}
1144236769Sobrien
1145236769Sobrien	hash_type = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
1146236769Sobrien	vsi->rss_hfunc = hash_type;
1147236769Sobrien
1148236769Sobrien	ctxt->info.q_opt_rss =
1149236769Sobrien		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
1150236769Sobrien		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
1151236769Sobrien}
1152236769Sobrien
1153236769Sobrienstatic void
1154236769Sobrienice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1155236769Sobrien{
1156236769Sobrien	struct ice_pf *pf = vsi->back;
1157236769Sobrien	u16 qcount, qmap;
1158236769Sobrien	u8 offset = 0;
1159236769Sobrien	int pow;
1160236769Sobrien
1161236769Sobrien	qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1162236769Sobrien
1163236769Sobrien	pow = order_base_2(qcount);
1164236769Sobrien	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1165236769Sobrien	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
1166236769Sobrien
1167236769Sobrien	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1168236769Sobrien	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1169236769Sobrien	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1170236769Sobrien	ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1171236769Sobrien}
1172236769Sobrien
1173236769Sobrien/**
1174236769Sobrien * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
1175236769Sobrien * @vsi: VSI to check whether or not VLAN pruning is enabled.
1176236769Sobrien *
1177236769Sobrien * returns true if Rx VLAN pruning is enabled and false otherwise.
1178236769Sobrien */
1179236769Sobrienstatic bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
1180236769Sobrien{
1181236769Sobrien	return vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1182236769Sobrien}
1183236769Sobrien
1184236769Sobrien/**
1185236769Sobrien * ice_vsi_init - Create and initialize a VSI
1186236769Sobrien * @vsi: the VSI being configured
1187236769Sobrien * @vsi_flags: VSI configuration flags
1188236769Sobrien *
1189236769Sobrien * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1190236769Sobrien * reconfigure an existing context.
1191236769Sobrien *
1192236769Sobrien * This initializes a VSI context depending on the VSI type to be added and
1193236769Sobrien * passes it down to the add_vsi aq command to create a new VSI.
1194236769Sobrien */
1195236769Sobrienstatic int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1196236769Sobrien{
1197236769Sobrien	struct ice_pf *pf = vsi->back;
1198236769Sobrien	struct ice_hw *hw = &pf->hw;
1199236769Sobrien	struct ice_vsi_ctx *ctxt;
1200236769Sobrien	struct device *dev;
1201236769Sobrien	int ret = 0;
1202236769Sobrien
1203236769Sobrien	dev = ice_pf_to_dev(pf);
1204236769Sobrien	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1205236769Sobrien	if (!ctxt)
1206236769Sobrien		return -ENOMEM;
1207236769Sobrien
1208236769Sobrien	switch (vsi->type) {
1209236769Sobrien	case ICE_VSI_CTRL:
1210236769Sobrien	case ICE_VSI_LB:
1211236769Sobrien	case ICE_VSI_PF:
1212236769Sobrien		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1213236769Sobrien		break;
1214236769Sobrien	case ICE_VSI_CHNL:
1215236769Sobrien		ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1216236769Sobrien		break;
1217236769Sobrien	case ICE_VSI_VF:
1218236769Sobrien		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1219236769Sobrien		/* VF number here is the absolute VF number (0-255) */
1220236769Sobrien		ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1221236769Sobrien		break;
1222236769Sobrien	default:
1223236769Sobrien		ret = -ENODEV;
1224236769Sobrien		goto out;
1225236769Sobrien	}
1226236769Sobrien
1227236769Sobrien	/* Handle VLAN pruning for channel VSI if main VSI has VLAN
1228236769Sobrien	 * prune enabled
1229236769Sobrien	 */
1230236769Sobrien	if (vsi->type == ICE_VSI_CHNL) {
1231236769Sobrien		struct ice_vsi *main_vsi;
1232236769Sobrien
1233236769Sobrien		main_vsi = ice_get_main_vsi(pf);
1234236769Sobrien		if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1235236769Sobrien			ctxt->info.sw_flags2 |=
1236236769Sobrien				ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1237236769Sobrien		else
1238236769Sobrien			ctxt->info.sw_flags2 &=
1239236769Sobrien				~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1240236769Sobrien	}
1241236769Sobrien
1242236769Sobrien	ice_set_dflt_vsi_ctx(hw, ctxt);
1243236769Sobrien	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1244236769Sobrien		ice_set_fd_vsi_ctx(ctxt, vsi);
1245236769Sobrien	/* if the switch is in VEB mode, allow VSI loopback */
1246236769Sobrien	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1247236769Sobrien		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1248236769Sobrien
1249236769Sobrien	/* Set LUT type and HASH type if RSS is enabled */
1250236769Sobrien	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1251236769Sobrien	    vsi->type != ICE_VSI_CTRL) {
1252236769Sobrien		ice_set_rss_vsi_ctx(ctxt, vsi);
1253236769Sobrien		/* if updating VSI context, make sure to set valid_section:
1254236769Sobrien		 * to indicate which section of VSI context being updated
1255236769Sobrien		 */
1256236769Sobrien		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1257236769Sobrien			ctxt->info.valid_sections |=
1258236769Sobrien				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1259236769Sobrien	}
1260236769Sobrien
1261236769Sobrien	ctxt->info.sw_id = vsi->port_info->sw_id;
1262236769Sobrien	if (vsi->type == ICE_VSI_CHNL) {
1263236769Sobrien		ice_chnl_vsi_setup_q_map(vsi, ctxt);
1264236769Sobrien	} else {
1265236769Sobrien		ret = ice_vsi_setup_q_map(vsi, ctxt);
1266236769Sobrien		if (ret)
1267236769Sobrien			goto out;
1268236769Sobrien
1269236769Sobrien		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1270236769Sobrien			/* means VSI being updated */
1271236769Sobrien			/* must to indicate which section of VSI context are
1272236769Sobrien			 * being modified
1273236769Sobrien			 */
1274236769Sobrien			ctxt->info.valid_sections |=
1275236769Sobrien				cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1276236769Sobrien	}
1277236769Sobrien
1278236769Sobrien	/* Allow control frames out of main VSI */
1279236769Sobrien	if (vsi->type == ICE_VSI_PF) {
1280236769Sobrien		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1281236769Sobrien		ctxt->info.valid_sections |=
1282236769Sobrien			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1283236769Sobrien	}
1284236769Sobrien
1285236769Sobrien	if (vsi_flags & ICE_VSI_FLAG_INIT) {
1286236769Sobrien		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1287236769Sobrien		if (ret) {
1288236769Sobrien			dev_err(dev, "Add VSI failed, err %d\n", ret);
1289236769Sobrien			ret = -EIO;
1290236769Sobrien			goto out;
1291236769Sobrien		}
1292236769Sobrien	} else {
1293236769Sobrien		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1294236769Sobrien		if (ret) {
1295236769Sobrien			dev_err(dev, "Update VSI failed, err %d\n", ret);
1296236769Sobrien			ret = -EIO;
1297236769Sobrien			goto out;
1298236769Sobrien		}
1299236769Sobrien	}
1300236769Sobrien
1301236769Sobrien	/* keep context for update VSI operations */
1302236769Sobrien	vsi->info = ctxt->info;
1303236769Sobrien
1304236769Sobrien	/* record VSI number returned */
1305236769Sobrien	vsi->vsi_num = ctxt->vsi_num;
1306240330Smarcel
1307236769Sobrienout:
1308240330Smarcel	kfree(ctxt);
1309240330Smarcel	return ret;
1310240330Smarcel}
1311240330Smarcel
1312236769Sobrien/**
1313236769Sobrien * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1314236769Sobrien * @vsi: the VSI having rings deallocated
1315236769Sobrien */
1316236769Sobrienstatic void ice_vsi_clear_rings(struct ice_vsi *vsi)
1317236769Sobrien{
1318236769Sobrien	int i;
1319240330Smarcel
1320240330Smarcel	/* Avoid stale references by clearing map from vector to ring */
1321240330Smarcel	if (vsi->q_vectors) {
1322240330Smarcel		ice_for_each_q_vector(vsi, i) {
1323240330Smarcel			struct ice_q_vector *q_vector = vsi->q_vectors[i];
1324240330Smarcel
1325240330Smarcel			if (q_vector) {
1326236769Sobrien				q_vector->tx.tx_ring = NULL;
1327236769Sobrien				q_vector->rx.rx_ring = NULL;
1328236769Sobrien			}
1329236769Sobrien		}
1330236769Sobrien	}
1331236769Sobrien
1332236769Sobrien	if (vsi->tx_rings) {
1333236769Sobrien		ice_for_each_alloc_txq(vsi, i) {
1334236769Sobrien			if (vsi->tx_rings[i]) {
1335236769Sobrien				kfree_rcu(vsi->tx_rings[i], rcu);
1336236769Sobrien				WRITE_ONCE(vsi->tx_rings[i], NULL);
1337236769Sobrien			}
1338236769Sobrien		}
1339236769Sobrien	}
1340236769Sobrien	if (vsi->rx_rings) {
1341236769Sobrien		ice_for_each_alloc_rxq(vsi, i) {
1342236769Sobrien			if (vsi->rx_rings[i]) {
1343236769Sobrien				kfree_rcu(vsi->rx_rings[i], rcu);
1344236769Sobrien				WRITE_ONCE(vsi->rx_rings[i], NULL);
1345236769Sobrien			}
1346236769Sobrien		}
1347236769Sobrien	}
1348236769Sobrien}
1349236769Sobrien
1350236769Sobrien/**
1351236769Sobrien * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1352236769Sobrien * @vsi: VSI which is having rings allocated
1353236769Sobrien */
1354236769Sobrienstatic int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1355236769Sobrien{
1356236769Sobrien	bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1357236769Sobrien	struct ice_pf *pf = vsi->back;
1358236769Sobrien	struct device *dev;
1359236769Sobrien	u16 i;
1360236769Sobrien
1361236769Sobrien	dev = ice_pf_to_dev(pf);
1362236769Sobrien	/* Allocate Tx rings */
1363236769Sobrien	ice_for_each_alloc_txq(vsi, i) {
1364236769Sobrien		struct ice_tx_ring *ring;
1365236769Sobrien
1366236769Sobrien		/* allocate with kzalloc(), free with kfree_rcu() */
1367236769Sobrien		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1368236769Sobrien
1369236769Sobrien		if (!ring)
1370236769Sobrien			goto err_out;
1371236769Sobrien
1372236769Sobrien		ring->q_index = i;
1373236769Sobrien		ring->reg_idx = vsi->txq_map[i];
1374236769Sobrien		ring->vsi = vsi;
1375236769Sobrien		ring->tx_tstamps = &pf->ptp.port.tx;
1376236769Sobrien		ring->dev = dev;
1377236769Sobrien		ring->count = vsi->num_tx_desc;
1378236769Sobrien		ring->txq_teid = ICE_INVAL_TEID;
1379236769Sobrien		if (dvm_ena)
1380236769Sobrien			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1381236769Sobrien		else
1382253883Ssjg			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1383253883Ssjg		WRITE_ONCE(vsi->tx_rings[i], ring);
1384253883Ssjg	}
1385236769Sobrien
1386236769Sobrien	/* Allocate Rx rings */
1387236769Sobrien	ice_for_each_alloc_rxq(vsi, i) {
1388236769Sobrien		struct ice_rx_ring *ring;
1389236769Sobrien
1390236769Sobrien		/* allocate with kzalloc(), free with kfree_rcu() */
1391236769Sobrien		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1392236769Sobrien		if (!ring)
1393236769Sobrien			goto err_out;
1394236769Sobrien
1395236769Sobrien		ring->q_index = i;
1396236769Sobrien		ring->reg_idx = vsi->rxq_map[i];
1397236769Sobrien		ring->vsi = vsi;
1398236769Sobrien		ring->netdev = vsi->netdev;
1399236769Sobrien		ring->dev = dev;
1400236769Sobrien		ring->count = vsi->num_rx_desc;
1401236769Sobrien		ring->cached_phctime = pf->ptp.cached_phc_time;
1402236769Sobrien		WRITE_ONCE(vsi->rx_rings[i], ring);
1403236769Sobrien	}
1404236769Sobrien
1405236769Sobrien	return 0;
1406236769Sobrien
1407236769Sobrienerr_out:
1408237578Sobrien	ice_vsi_clear_rings(vsi);
1409236769Sobrien	return -ENOMEM;
1410236769Sobrien}
1411236769Sobrien
1412236769Sobrien/**
1413236769Sobrien * ice_vsi_manage_rss_lut - disable/enable RSS
1414236769Sobrien * @vsi: the VSI being changed
1415236769Sobrien * @ena: boolean value indicating if this is an enable or disable request
1416236769Sobrien *
1417255253Ssjg * In the event of disable request for RSS, this function will zero out RSS
1418236769Sobrien * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1419236769Sobrien * LUT.
1420236769Sobrien */
1421236769Sobrienvoid ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1422236769Sobrien{
1423236769Sobrien	u8 *lut;
1424236769Sobrien
1425236769Sobrien	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1426236769Sobrien	if (!lut)
1427236769Sobrien		return;
1428236769Sobrien
1429236769Sobrien	if (ena) {
1430236769Sobrien		if (vsi->rss_lut_user)
1431236769Sobrien			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1432236769Sobrien		else
1433236769Sobrien			ice_fill_rss_lut(lut, vsi->rss_table_size,
1434236769Sobrien					 vsi->rss_size);
1435236769Sobrien	}
1436236769Sobrien
1437236769Sobrien	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1438236769Sobrien	kfree(lut);
1439236769Sobrien}
1440236769Sobrien
1441236769Sobrien/**
1442236769Sobrien * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1443236769Sobrien * @vsi: VSI to be configured
1444236769Sobrien * @disable: set to true to have FCS / CRC in the frame data
1445236769Sobrien */
1446236769Sobrienvoid ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1447236769Sobrien{
1448236769Sobrien	int i;
1449236769Sobrien
1450236769Sobrien	ice_for_each_rxq(vsi, i)
1451236769Sobrien		if (disable)
1452236769Sobrien			vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1453236769Sobrien		else
1454236769Sobrien			vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1455236769Sobrien}
1456236769Sobrien
1457236769Sobrien/**
1458236769Sobrien * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1459236769Sobrien * @vsi: VSI to be configured
1460236769Sobrien */
1461236769Sobrienint ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1462236769Sobrien{
1463236769Sobrien	struct ice_pf *pf = vsi->back;
1464236769Sobrien	struct device *dev;
1465236769Sobrien	u8 *lut, *key;
1466255253Ssjg	int err;
1467236769Sobrien
1468236769Sobrien	dev = ice_pf_to_dev(pf);
1469236769Sobrien	if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1470236769Sobrien	    (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1471236769Sobrien		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1472236769Sobrien	} else {
1473236769Sobrien		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1474236769Sobrien
1475236769Sobrien		/* If orig_rss_size is valid and it is less than determined
1476236769Sobrien		 * main VSI's rss_size, update main VSI's rss_size to be
1477236769Sobrien		 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1478236769Sobrien		 * RSS table gets programmed to be correct (whatever it was
1479236769Sobrien		 * to begin with (prior to setup-tc for ADQ config)
1480236769Sobrien		 */
1481236769Sobrien		if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1482236769Sobrien		    vsi->orig_rss_size <= vsi->num_rxq) {
1483236769Sobrien			vsi->rss_size = vsi->orig_rss_size;
1484236769Sobrien			/* now orig_rss_size is used, reset it to zero */
1485236769Sobrien			vsi->orig_rss_size = 0;
1486236769Sobrien		}
1487236769Sobrien	}
1488236769Sobrien
1489236769Sobrien	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1490236769Sobrien	if (!lut)
1491236769Sobrien		return -ENOMEM;
1492236769Sobrien
1493236769Sobrien	if (vsi->rss_lut_user)
1494236769Sobrien		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1495236769Sobrien	else
1496236769Sobrien		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1497236769Sobrien
1498236769Sobrien	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1499236769Sobrien	if (err) {
1500236769Sobrien		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1501236769Sobrien		goto ice_vsi_cfg_rss_exit;
1502236769Sobrien	}
1503236769Sobrien
1504236769Sobrien	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1505236769Sobrien	if (!key) {
1506236769Sobrien		err = -ENOMEM;
1507236769Sobrien		goto ice_vsi_cfg_rss_exit;
1508236769Sobrien	}
1509236769Sobrien
1510236769Sobrien	if (vsi->rss_hkey_user)
1511236769Sobrien		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1512236769Sobrien	else
1513236769Sobrien		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1514236769Sobrien
1515236769Sobrien	err = ice_set_rss_key(vsi, key);
1516236769Sobrien	if (err)
1517236769Sobrien		dev_err(dev, "set_rss_key failed, error %d\n", err);
1518236769Sobrien
1519236769Sobrien	kfree(key);
1520236769Sobrienice_vsi_cfg_rss_exit:
1521236769Sobrien	kfree(lut);
1522236769Sobrien	return err;
1523236769Sobrien}
1524236769Sobrien
1525236769Sobrien/**
1526236769Sobrien * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1527236769Sobrien * @vsi: VSI to be configured
1528236769Sobrien *
1529236769Sobrien * This function will only be called during the VF VSI setup. Upon successful
1530236769Sobrien * completion of package download, this function will configure default RSS
1531236769Sobrien * input sets for VF VSI.
1532236769Sobrien */
1533236769Sobrienstatic void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1534236769Sobrien{
1535236769Sobrien	struct ice_pf *pf = vsi->back;
1536236769Sobrien	struct device *dev;
1537236769Sobrien	int status;
1538236769Sobrien
1539236769Sobrien	dev = ice_pf_to_dev(pf);
1540236769Sobrien	if (ice_is_safe_mode(pf)) {
1541236769Sobrien		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1542236769Sobrien			vsi->vsi_num);
1543236769Sobrien		return;
1544236769Sobrien	}
1545236769Sobrien
1546236769Sobrien	status = ice_add_avf_rss_cfg(&pf->hw, vsi, ICE_DEFAULT_RSS_HENA);
1547236769Sobrien	if (status)
1548236769Sobrien		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1549236769Sobrien			vsi->vsi_num, status);
1550236769Sobrien}
1551236769Sobrien
1552236769Sobrienstatic const struct ice_rss_hash_cfg default_rss_cfgs[] = {
1553236769Sobrien	/* configure RSS for IPv4 with input set IP src/dst */
1554236769Sobrien	{ICE_FLOW_SEG_HDR_IPV4, ICE_FLOW_HASH_IPV4, ICE_RSS_ANY_HEADERS, false},
1555236769Sobrien	/* configure RSS for IPv6 with input set IPv6 src/dst */
1556236769Sobrien	{ICE_FLOW_SEG_HDR_IPV6, ICE_FLOW_HASH_IPV6, ICE_RSS_ANY_HEADERS, false},
1557236769Sobrien	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1558236769Sobrien	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4,
1559236769Sobrien				ICE_HASH_TCP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1560236769Sobrien	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1561236769Sobrien	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4,
1562236769Sobrien				ICE_HASH_UDP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1563236769Sobrien	/* configure RSS for sctp4 with input set IP src/dst - only support
1564236769Sobrien	 * RSS on SCTPv4 on outer headers (non-tunneled)
1565236769Sobrien	 */
1566236769Sobrien	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4,
1567236769Sobrien		ICE_HASH_SCTP_IPV4, ICE_RSS_OUTER_HEADERS, false},
1568236769Sobrien	/* configure RSS for gtpc4 with input set IPv4 src/dst */
1569236769Sobrien	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV4,
1570236769Sobrien		ICE_FLOW_HASH_IPV4, ICE_RSS_OUTER_HEADERS, false},
1571236769Sobrien	/* configure RSS for gtpc4t with input set IPv4 src/dst */
1572236769Sobrien	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV4,
1573236769Sobrien		ICE_FLOW_HASH_GTP_C_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1574236769Sobrien	/* configure RSS for gtpu4 with input set IPv4 src/dst */
1575236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV4,
1576236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1577236769Sobrien	/* configure RSS for gtpu4e with input set IPv4 src/dst */
1578236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV4,
1579236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV4_EH, ICE_RSS_OUTER_HEADERS, false},
1580236769Sobrien	/* configure RSS for gtpu4u with input set IPv4 src/dst */
1581236769Sobrien	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV4,
1582236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV4_UP, ICE_RSS_OUTER_HEADERS, false},
1583236769Sobrien	/* configure RSS for gtpu4d with input set IPv4 src/dst */
1584236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV4,
1585236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV4_DWN, ICE_RSS_OUTER_HEADERS, false},
1586236769Sobrien
1587236769Sobrien	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1588236769Sobrien	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6,
1589236769Sobrien				ICE_HASH_TCP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1590236769Sobrien	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1591236769Sobrien	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6,
1592236769Sobrien				ICE_HASH_UDP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1593236769Sobrien	/* configure RSS for sctp6 with input set IPv6 src/dst - only support
1594236769Sobrien	 * RSS on SCTPv6 on outer headers (non-tunneled)
1595236769Sobrien	 */
1596236769Sobrien	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6,
1597236769Sobrien		ICE_HASH_SCTP_IPV6, ICE_RSS_OUTER_HEADERS, false},
1598236769Sobrien	/* configure RSS for IPSEC ESP SPI with input set MAC_IPV4_SPI */
1599236769Sobrien	{ICE_FLOW_SEG_HDR_ESP,
1600236769Sobrien		ICE_FLOW_HASH_ESP_SPI, ICE_RSS_OUTER_HEADERS, false},
1601236769Sobrien	/* configure RSS for gtpc6 with input set IPv6 src/dst */
1602236769Sobrien	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV6,
1603236769Sobrien		ICE_FLOW_HASH_IPV6, ICE_RSS_OUTER_HEADERS, false},
1604236769Sobrien	/* configure RSS for gtpc6t with input set IPv6 src/dst */
1605236769Sobrien	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV6,
1606236769Sobrien		ICE_FLOW_HASH_GTP_C_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1607236769Sobrien	/* configure RSS for gtpu6 with input set IPv6 src/dst */
1608236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV6,
1609236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1610236769Sobrien	/* configure RSS for gtpu6e with input set IPv6 src/dst */
1611236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV6,
1612236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV6_EH, ICE_RSS_OUTER_HEADERS, false},
1613236769Sobrien	/* configure RSS for gtpu6u with input set IPv6 src/dst */
1614236769Sobrien	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV6,
1615236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV6_UP, ICE_RSS_OUTER_HEADERS, false},
1616236769Sobrien	/* configure RSS for gtpu6d with input set IPv6 src/dst */
1617236769Sobrien	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV6,
1618236769Sobrien		ICE_FLOW_HASH_GTP_U_IPV6_DWN, ICE_RSS_OUTER_HEADERS, false},
1619236769Sobrien};
1620236769Sobrien
1621236769Sobrien/**
1622236769Sobrien * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1623236769Sobrien * @vsi: VSI to be configured
1624236769Sobrien *
1625236769Sobrien * This function will only be called after successful download package call
1626236769Sobrien * during initialization of PF. Since the downloaded package will erase the
1627236769Sobrien * RSS section, this function will configure RSS input sets for different
1628236769Sobrien * flow types. The last profile added has the highest priority, therefore 2
1629236769Sobrien * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1630236769Sobrien * (i.e. IPv4 src/dst TCP src/dst port).
1631236769Sobrien */
1632236769Sobrienstatic void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1633236769Sobrien{
1634236769Sobrien	u16 vsi_num = vsi->vsi_num;
1635236769Sobrien	struct ice_pf *pf = vsi->back;
1636236769Sobrien	struct ice_hw *hw = &pf->hw;
1637236769Sobrien	struct device *dev;
1638236769Sobrien	int status;
1639236769Sobrien	u32 i;
1640236769Sobrien
1641236769Sobrien	dev = ice_pf_to_dev(pf);
1642236769Sobrien	if (ice_is_safe_mode(pf)) {
1643236769Sobrien		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1644236769Sobrien			vsi_num);
1645236769Sobrien		return;
1646236769Sobrien	}
1647236769Sobrien	for (i = 0; i < ARRAY_SIZE(default_rss_cfgs); i++) {
1648236769Sobrien		const struct ice_rss_hash_cfg *cfg = &default_rss_cfgs[i];
1649236769Sobrien
1650236769Sobrien		status = ice_add_rss_cfg(hw, vsi, cfg);
1651236769Sobrien		if (status)
1652236769Sobrien			dev_dbg(dev, "ice_add_rss_cfg failed, addl_hdrs = %x, hash_flds = %llx, hdr_type = %d, symm = %d\n",
1653236769Sobrien				cfg->addl_hdrs, cfg->hash_flds,
1654236769Sobrien				cfg->hdr_type, cfg->symm);
1655236769Sobrien	}
1656236769Sobrien}
1657236769Sobrien
1658236769Sobrien/**
1659236769Sobrien * ice_pf_state_is_nominal - checks the PF for nominal state
1660236769Sobrien * @pf: pointer to PF to check
1661236769Sobrien *
1662236769Sobrien * Check the PF's state for a collection of bits that would indicate
1663236769Sobrien * the PF is in a state that would inhibit normal operation for
1664236769Sobrien * driver functionality.
1665236769Sobrien *
1666236769Sobrien * Returns true if PF is in a nominal state, false otherwise
1667236769Sobrien */
1668236769Sobrienbool ice_pf_state_is_nominal(struct ice_pf *pf)
1669236769Sobrien{
1670236769Sobrien	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1671236769Sobrien
1672236769Sobrien	if (!pf)
1673236769Sobrien		return false;
1674236769Sobrien
1675236769Sobrien	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1676236769Sobrien	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1677236769Sobrien		return false;
1678236769Sobrien
1679236769Sobrien	return true;
1680236769Sobrien}
1681236769Sobrien
1682236769Sobrien/**
1683236769Sobrien * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1684236769Sobrien * @vsi: the VSI to be updated
1685236769Sobrien */
1686236769Sobrienvoid ice_update_eth_stats(struct ice_vsi *vsi)
1687236769Sobrien{
1688236769Sobrien	struct ice_eth_stats *prev_es, *cur_es;
1689236769Sobrien	struct ice_hw *hw = &vsi->back->hw;
1690236769Sobrien	struct ice_pf *pf = vsi->back;
1691236769Sobrien	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1692236769Sobrien
1693236769Sobrien	prev_es = &vsi->eth_stats_prev;
1694236769Sobrien	cur_es = &vsi->eth_stats;
1695236769Sobrien
1696236769Sobrien	if (ice_is_reset_in_progress(pf->state))
1697236769Sobrien		vsi->stat_offsets_loaded = false;
1698236769Sobrien
1699236769Sobrien	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1700236769Sobrien			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1701236769Sobrien
1702236769Sobrien	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1703236769Sobrien			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1704236769Sobrien
1705236769Sobrien	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1706236769Sobrien			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1707236769Sobrien
1708236769Sobrien	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1709236769Sobrien			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1710236769Sobrien
1711236769Sobrien	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1712236769Sobrien			  &prev_es->rx_discards, &cur_es->rx_discards);
1713236769Sobrien
1714236769Sobrien	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1715236769Sobrien			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1716236769Sobrien
1717236769Sobrien	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1718236769Sobrien			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1719236769Sobrien
1720236769Sobrien	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1721236769Sobrien			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1722236769Sobrien
1723236769Sobrien	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1724236769Sobrien			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1725236769Sobrien
1726236769Sobrien	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1727236769Sobrien			  &prev_es->tx_errors, &cur_es->tx_errors);
1728236769Sobrien
1729236769Sobrien	vsi->stat_offsets_loaded = true;
1730236769Sobrien}
1731236769Sobrien
1732236769Sobrien/**
1733236769Sobrien * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1734236769Sobrien * @hw: HW pointer
1735236769Sobrien * @pf_q: index of the Rx queue in the PF's queue space
1736236769Sobrien * @rxdid: flexible descriptor RXDID
1737236769Sobrien * @prio: priority for the RXDID for this queue
1738236769Sobrien * @ena_ts: true to enable timestamp and false to disable timestamp
1739236769Sobrien */
1740236769Sobrienvoid
1741236769Sobrienice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1742236769Sobrien			bool ena_ts)
1743236769Sobrien{
1744236769Sobrien	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1745236769Sobrien
1746236769Sobrien	/* clear any previous values */
1747236769Sobrien	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1748236769Sobrien		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1749236769Sobrien		    QRXFLXP_CNTXT_TS_M);
1750236769Sobrien
1751236769Sobrien	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_IDX_M, rxdid);
1752236769Sobrien	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_PRIO_M, prio);
1753236769Sobrien
1754236769Sobrien	if (ena_ts)
1755249033Ssjg		/* Enable TimeSync on this queue */
1756236769Sobrien		regval |= QRXFLXP_CNTXT_TS_M;
1757236769Sobrien
1758236769Sobrien	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1759236769Sobrien}
1760236769Sobrien
1761236769Sobrien/**
1762236769Sobrien * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1763236769Sobrien * @intrl: interrupt rate limit in usecs
1764236769Sobrien * @gran: interrupt rate limit granularity in usecs
1765236769Sobrien *
1766236769Sobrien * This function converts a decimal interrupt rate limit in usecs to the format
1767236769Sobrien * expected by firmware.
1768236769Sobrien */
1769236769Sobrienstatic u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1770236769Sobrien{
1771236769Sobrien	u32 val = intrl / gran;
1772236769Sobrien
1773236769Sobrien	if (val)
1774236769Sobrien		return val | GLINT_RATE_INTRL_ENA_M;
1775236769Sobrien	return 0;
1776236769Sobrien}
1777236769Sobrien
1778236769Sobrien/**
1779236769Sobrien * ice_write_intrl - write throttle rate limit to interrupt specific register
1780236769Sobrien * @q_vector: pointer to interrupt specific structure
1781236769Sobrien * @intrl: throttle rate limit in microseconds to write
1782236769Sobrien */
1783236769Sobrienvoid ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1784236769Sobrien{
1785236769Sobrien	struct ice_hw *hw = &q_vector->vsi->back->hw;
1786236769Sobrien
1787236769Sobrien	wr32(hw, GLINT_RATE(q_vector->reg_idx),
1788236769Sobrien	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1789236769Sobrien}
1790236769Sobrien
1791236769Sobrienstatic struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1792236769Sobrien{
1793236769Sobrien	switch (rc->type) {
1794236769Sobrien	case ICE_RX_CONTAINER:
1795236769Sobrien		if (rc->rx_ring)
1796236769Sobrien			return rc->rx_ring->q_vector;
1797236769Sobrien		break;
1798236769Sobrien	case ICE_TX_CONTAINER:
1799236769Sobrien		if (rc->tx_ring)
1800236769Sobrien			return rc->tx_ring->q_vector;
1801246223Ssjg		break;
1802246223Ssjg	default:
1803236769Sobrien		break;
1804236769Sobrien	}
1805236769Sobrien
1806236769Sobrien	return NULL;
1807236769Sobrien}
1808236769Sobrien
1809236769Sobrien/**
1810236769Sobrien * __ice_write_itr - write throttle rate to register
1811236769Sobrien * @q_vector: pointer to interrupt data structure
1812236769Sobrien * @rc: pointer to ring container
1813253883Ssjg * @itr: throttle rate in microseconds to write
1814253883Ssjg */
1815253883Ssjgstatic void __ice_write_itr(struct ice_q_vector *q_vector,
1816253883Ssjg			    struct ice_ring_container *rc, u16 itr)
1817236769Sobrien{
1818253883Ssjg	struct ice_hw *hw = &q_vector->vsi->back->hw;
1819236769Sobrien
1820236769Sobrien	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1821236769Sobrien	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1822236769Sobrien}
1823236769Sobrien
1824236769Sobrien/**
1825236769Sobrien * ice_write_itr - write throttle rate to queue specific register
1826236769Sobrien * @rc: pointer to ring container
1827236769Sobrien * @itr: throttle rate in microseconds to write
1828236769Sobrien */
1829236769Sobrienvoid ice_write_itr(struct ice_ring_container *rc, u16 itr)
1830236769Sobrien{
1831236769Sobrien	struct ice_q_vector *q_vector;
1832236769Sobrien
1833236769Sobrien	q_vector = ice_pull_qvec_from_rc(rc);
1834236769Sobrien	if (!q_vector)
1835236769Sobrien		return;
1836236769Sobrien
1837236769Sobrien	__ice_write_itr(q_vector, rc, itr);
1838236769Sobrien}
1839236769Sobrien
1840236769Sobrien/**
1841236769Sobrien * ice_set_q_vector_intrl - set up interrupt rate limiting
1842236769Sobrien * @q_vector: the vector to be configured
1843236769Sobrien *
1844236769Sobrien * Interrupt rate limiting is local to the vector, not per-queue so we must
1845236769Sobrien * detect if either ring container has dynamic moderation enabled to decide
1846236769Sobrien * what to set the interrupt rate limit to via INTRL settings. In the case that
1847236769Sobrien * dynamic moderation is disabled on both, write the value with the cached
1848236769Sobrien * setting to make sure INTRL register matches the user visible value.
1849236769Sobrien */
1850236769Sobrienvoid ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
1851236769Sobrien{
1852236769Sobrien	if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
1853236769Sobrien		/* in the case of dynamic enabled, cap each vector to no more
1854236769Sobrien		 * than (4 us) 250,000 ints/sec, which allows low latency
1855236769Sobrien		 * but still less than 500,000 interrupts per second, which
1856236769Sobrien		 * reduces CPU a bit in the case of the lowest latency
1857236769Sobrien		 * setting. The 4 here is a value in microseconds.
1858236769Sobrien		 */
1859236769Sobrien		ice_write_intrl(q_vector, 4);
1860236769Sobrien	} else {
1861236769Sobrien		ice_write_intrl(q_vector, q_vector->intrl);
1862236769Sobrien	}
1863236769Sobrien}
1864236769Sobrien
1865236769Sobrien/**
1866236769Sobrien * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1867236769Sobrien * @vsi: the VSI being configured
1868236769Sobrien *
1869236769Sobrien * This configures MSIX mode interrupts for the PF VSI, and should not be used
1870236769Sobrien * for the VF VSI.
1871236769Sobrien */
1872236769Sobrienvoid ice_vsi_cfg_msix(struct ice_vsi *vsi)
1873236769Sobrien{
1874236769Sobrien	struct ice_pf *pf = vsi->back;
1875236769Sobrien	struct ice_hw *hw = &pf->hw;
1876236769Sobrien	u16 txq = 0, rxq = 0;
1877236769Sobrien	int i, q;
1878236769Sobrien
1879236769Sobrien	ice_for_each_q_vector(vsi, i) {
1880236769Sobrien		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1881236769Sobrien		u16 reg_idx = q_vector->reg_idx;
1882236769Sobrien
1883236769Sobrien		ice_cfg_itr(hw, q_vector);
1884236769Sobrien
1885236769Sobrien		/* Both Transmit Queue Interrupt Cause Control register
1886236769Sobrien		 * and Receive Queue Interrupt Cause control register
1887236769Sobrien		 * expects MSIX_INDX field to be the vector index
1888236769Sobrien		 * within the function space and not the absolute
1889236769Sobrien		 * vector index across PF or across device.
1890236769Sobrien		 * For SR-IOV VF VSIs queue vector index always starts
1891236769Sobrien		 * with 1 since first vector index(0) is used for OICR
1892236769Sobrien		 * in VF space. Since VMDq and other PF VSIs are within
1893236769Sobrien		 * the PF function space, use the vector index that is
1894236769Sobrien		 * tracked for this PF.
1895236769Sobrien		 */
1896236769Sobrien		for (q = 0; q < q_vector->num_ring_tx; q++) {
1897236769Sobrien			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1898236769Sobrien					      q_vector->tx.itr_idx);
1899236769Sobrien			txq++;
1900236769Sobrien		}
1901236769Sobrien
1902236769Sobrien		for (q = 0; q < q_vector->num_ring_rx; q++) {
1903236769Sobrien			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1904236769Sobrien					      q_vector->rx.itr_idx);
1905236769Sobrien			rxq++;
1906236769Sobrien		}
1907236769Sobrien	}
1908236769Sobrien}
1909236769Sobrien
1910236769Sobrien/**
1911236769Sobrien * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1912236769Sobrien * @vsi: the VSI whose rings are to be enabled
1913236769Sobrien *
1914236769Sobrien * Returns 0 on success and a negative value on error
1915236769Sobrien */
1916236769Sobrienint ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1917236769Sobrien{
1918236769Sobrien	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1919236769Sobrien}
1920236769Sobrien
1921236769Sobrien/**
1922236769Sobrien * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1923236769Sobrien * @vsi: the VSI whose rings are to be disabled
1924236769Sobrien *
1925236769Sobrien * Returns 0 on success and a negative value on error
1926236769Sobrien */
1927236769Sobrienint ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1928236769Sobrien{
1929236769Sobrien	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1930236769Sobrien}
1931236769Sobrien
1932236769Sobrien/**
1933236769Sobrien * ice_vsi_stop_tx_rings - Disable Tx rings
1934236769Sobrien * @vsi: the VSI being configured
1935236769Sobrien * @rst_src: reset source
1936236769Sobrien * @rel_vmvf_num: Relative ID of VF/VM
1937236769Sobrien * @rings: Tx ring array to be stopped
1938236769Sobrien * @count: number of Tx ring array elements
1939236769Sobrien */
1940236769Sobrienstatic int
1941236769Sobrienice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1942236769Sobrien		      u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
1943236769Sobrien{
1944236769Sobrien	u16 q_idx;
1945236769Sobrien
1946236769Sobrien	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1947240330Smarcel		return -EINVAL;
1948240330Smarcel
1949240330Smarcel	for (q_idx = 0; q_idx < count; q_idx++) {
1950240330Smarcel		struct ice_txq_meta txq_meta = { };
1951240330Smarcel		int status;
1952240330Smarcel
1953240330Smarcel		if (!rings || !rings[q_idx])
1954240330Smarcel			return -EINVAL;
1955240330Smarcel
1956240330Smarcel		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1957240330Smarcel		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1958240330Smarcel					      rings[q_idx], &txq_meta);
1959240330Smarcel
1960240330Smarcel		if (status)
1961240330Smarcel			return status;
1962240330Smarcel	}
1963240330Smarcel
1964240330Smarcel	return 0;
1965240330Smarcel}
1966240330Smarcel
1967240330Smarcel/**
1968240330Smarcel * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1969240330Smarcel * @vsi: the VSI being configured
1970240330Smarcel * @rst_src: reset source
1971240330Smarcel * @rel_vmvf_num: Relative ID of VF/VM
1972240330Smarcel */
1973240330Smarcelint
1974240330Smarcelice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1975240330Smarcel			  u16 rel_vmvf_num)
1976240330Smarcel{
1977240330Smarcel	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
1978240330Smarcel}
1979240330Smarcel
1980240330Smarcel/**
1981240330Smarcel * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1982240330Smarcel * @vsi: the VSI being configured
1983240330Smarcel */
1984240330Smarcelint ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1985240330Smarcel{
1986240330Smarcel	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
1987240330Smarcel}
1988240330Smarcel
1989240330Smarcel/**
1990240330Smarcel * ice_vsi_is_rx_queue_active
1991240330Smarcel * @vsi: the VSI being configured
1992240330Smarcel *
1993 * Return true if at least one queue is active.
1994 */
1995bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
1996{
1997	struct ice_pf *pf = vsi->back;
1998	struct ice_hw *hw = &pf->hw;
1999	int i;
2000
2001	ice_for_each_rxq(vsi, i) {
2002		u32 rx_reg;
2003		int pf_q;
2004
2005		pf_q = vsi->rxq_map[i];
2006		rx_reg = rd32(hw, QRX_CTRL(pf_q));
2007		if (rx_reg & QRX_CTRL_QENA_STAT_M)
2008			return true;
2009	}
2010
2011	return false;
2012}
2013
2014static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2015{
2016	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2017		vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2018		vsi->tc_cfg.numtc = 1;
2019		return;
2020	}
2021
2022	/* set VSI TC information based on DCB config */
2023	ice_vsi_set_dcb_tc_cfg(vsi);
2024}
2025
2026/**
2027 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2028 * @vsi: the VSI being configured
2029 * @tx: bool to determine Tx or Rx rule
2030 * @create: bool to determine create or remove Rule
2031 */
2032void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2033{
2034	int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2035			enum ice_sw_fwd_act_type act);
2036	struct ice_pf *pf = vsi->back;
2037	struct device *dev;
2038	int status;
2039
2040	dev = ice_pf_to_dev(pf);
2041	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2042
2043	if (tx) {
2044		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2045				  ICE_DROP_PACKET);
2046	} else {
2047		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2048			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2049							  create);
2050		} else {
2051			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2052					  ICE_FWD_TO_VSI);
2053		}
2054	}
2055
2056	if (status)
2057		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2058			create ? "adding" : "removing", tx ? "TX" : "RX",
2059			vsi->vsi_num, status);
2060}
2061
2062/**
2063 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2064 * @vsi: pointer to the VSI
2065 *
2066 * This function will allocate new scheduler aggregator now if needed and will
2067 * move specified VSI into it.
2068 */
2069static void ice_set_agg_vsi(struct ice_vsi *vsi)
2070{
2071	struct device *dev = ice_pf_to_dev(vsi->back);
2072	struct ice_agg_node *agg_node_iter = NULL;
2073	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2074	struct ice_agg_node *agg_node = NULL;
2075	int node_offset, max_agg_nodes = 0;
2076	struct ice_port_info *port_info;
2077	struct ice_pf *pf = vsi->back;
2078	u32 agg_node_id_start = 0;
2079	int status;
2080
2081	/* create (as needed) scheduler aggregator node and move VSI into
2082	 * corresponding aggregator node
2083	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2084	 * - VF aggregator nodes will contain VF VSI
2085	 */
2086	port_info = pf->hw.port_info;
2087	if (!port_info)
2088		return;
2089
2090	switch (vsi->type) {
2091	case ICE_VSI_CTRL:
2092	case ICE_VSI_CHNL:
2093	case ICE_VSI_LB:
2094	case ICE_VSI_PF:
2095		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2096		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2097		agg_node_iter = &pf->pf_agg_node[0];
2098		break;
2099	case ICE_VSI_VF:
2100		/* user can create 'n' VFs on a given PF, but since max children
2101		 * per aggregator node can be only 64. Following code handles
2102		 * aggregator(s) for VF VSIs, either selects a agg_node which
2103		 * was already created provided num_vsis < 64, otherwise
2104		 * select next available node, which will be created
2105		 */
2106		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2107		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2108		agg_node_iter = &pf->vf_agg_node[0];
2109		break;
2110	default:
2111		/* other VSI type, handle later if needed */
2112		dev_dbg(dev, "unexpected VSI type %s\n",
2113			ice_vsi_type_str(vsi->type));
2114		return;
2115	}
2116
2117	/* find the appropriate aggregator node */
2118	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2119		/* see if we can find space in previously created
2120		 * node if num_vsis < 64, otherwise skip
2121		 */
2122		if (agg_node_iter->num_vsis &&
2123		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2124			agg_node_iter++;
2125			continue;
2126		}
2127
2128		if (agg_node_iter->valid &&
2129		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2130			agg_id = agg_node_iter->agg_id;
2131			agg_node = agg_node_iter;
2132			break;
2133		}
2134
2135		/* find unclaimed agg_id */
2136		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2137			agg_id = node_offset + agg_node_id_start;
2138			agg_node = agg_node_iter;
2139			break;
2140		}
2141		/* move to next agg_node */
2142		agg_node_iter++;
2143	}
2144
2145	if (!agg_node)
2146		return;
2147
2148	/* if selected aggregator node was not created, create it */
2149	if (!agg_node->valid) {
2150		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2151				     (u8)vsi->tc_cfg.ena_tc);
2152		if (status) {
2153			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2154				agg_id);
2155			return;
2156		}
2157		/* aggregator node is created, store the needed info */
2158		agg_node->valid = true;
2159		agg_node->agg_id = agg_id;
2160	}
2161
2162	/* move VSI to corresponding aggregator node */
2163	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2164				     (u8)vsi->tc_cfg.ena_tc);
2165	if (status) {
2166		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2167			vsi->idx, agg_id);
2168		return;
2169	}
2170
2171	/* keep active children count for aggregator node */
2172	agg_node->num_vsis++;
2173
2174	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2175	 * to aggregator node
2176	 */
2177	vsi->agg_node = agg_node;
2178	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2179		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2180		vsi->agg_node->num_vsis);
2181}
2182
2183static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
2184{
2185	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2186	struct device *dev = ice_pf_to_dev(pf);
2187	int ret, i;
2188
2189	/* configure VSI nodes based on number of queues and TC's */
2190	ice_for_each_traffic_class(i) {
2191		if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2192			continue;
2193
2194		if (vsi->type == ICE_VSI_CHNL) {
2195			if (!vsi->alloc_txq && vsi->num_txq)
2196				max_txqs[i] = vsi->num_txq;
2197			else
2198				max_txqs[i] = pf->num_lan_tx;
2199		} else {
2200			max_txqs[i] = vsi->alloc_txq;
2201		}
2202
2203		if (vsi->type == ICE_VSI_PF)
2204			max_txqs[i] += vsi->num_xdp_txq;
2205	}
2206
2207	dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2208	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2209			      max_txqs);
2210	if (ret) {
2211		dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2212			vsi->vsi_num, ret);
2213		return ret;
2214	}
2215
2216	return 0;
2217}
2218
2219/**
2220 * ice_vsi_cfg_def - configure default VSI based on the type
2221 * @vsi: pointer to VSI
2222 */
2223static int ice_vsi_cfg_def(struct ice_vsi *vsi)
2224{
2225	struct device *dev = ice_pf_to_dev(vsi->back);
2226	struct ice_pf *pf = vsi->back;
2227	int ret;
2228
2229	vsi->vsw = pf->first_sw;
2230
2231	ret = ice_vsi_alloc_def(vsi, vsi->ch);
2232	if (ret)
2233		return ret;
2234
2235	/* allocate memory for Tx/Rx ring stat pointers */
2236	ret = ice_vsi_alloc_stat_arrays(vsi);
2237	if (ret)
2238		goto unroll_vsi_alloc;
2239
2240	ice_alloc_fd_res(vsi);
2241
2242	ret = ice_vsi_get_qs(vsi);
2243	if (ret) {
2244		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2245			vsi->idx);
2246		goto unroll_vsi_alloc_stat;
2247	}
2248
2249	/* set RSS capabilities */
2250	ice_vsi_set_rss_params(vsi);
2251
2252	/* set TC configuration */
2253	ice_vsi_set_tc_cfg(vsi);
2254
2255	/* create the VSI */
2256	ret = ice_vsi_init(vsi, vsi->flags);
2257	if (ret)
2258		goto unroll_get_qs;
2259
2260	ice_vsi_init_vlan_ops(vsi);
2261
2262	switch (vsi->type) {
2263	case ICE_VSI_CTRL:
2264	case ICE_VSI_PF:
2265		ret = ice_vsi_alloc_q_vectors(vsi);
2266		if (ret)
2267			goto unroll_vsi_init;
2268
2269		ret = ice_vsi_alloc_rings(vsi);
2270		if (ret)
2271			goto unroll_vector_base;
2272
2273		ret = ice_vsi_alloc_ring_stats(vsi);
2274		if (ret)
2275			goto unroll_vector_base;
2276
2277		if (ice_is_xdp_ena_vsi(vsi)) {
2278			ret = ice_vsi_determine_xdp_res(vsi);
2279			if (ret)
2280				goto unroll_vector_base;
2281			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog,
2282						    ICE_XDP_CFG_PART);
2283			if (ret)
2284				goto unroll_vector_base;
2285		}
2286
2287		ice_vsi_map_rings_to_vectors(vsi);
2288
2289		/* Associate q_vector rings to napi */
2290		ice_vsi_set_napi_queues(vsi);
2291
2292		vsi->stat_offsets_loaded = false;
2293
2294		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2295		if (vsi->type != ICE_VSI_CTRL)
2296			/* Do not exit if configuring RSS had an issue, at
2297			 * least receive traffic on first queue. Hence no
2298			 * need to capture return value
2299			 */
2300			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2301				ice_vsi_cfg_rss_lut_key(vsi);
2302				ice_vsi_set_rss_flow_fld(vsi);
2303			}
2304		ice_init_arfs(vsi);
2305		break;
2306	case ICE_VSI_CHNL:
2307		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2308			ice_vsi_cfg_rss_lut_key(vsi);
2309			ice_vsi_set_rss_flow_fld(vsi);
2310		}
2311		break;
2312	case ICE_VSI_VF:
2313		/* VF driver will take care of creating netdev for this type and
2314		 * map queues to vectors through Virtchnl, PF driver only
2315		 * creates a VSI and corresponding structures for bookkeeping
2316		 * purpose
2317		 */
2318		ret = ice_vsi_alloc_q_vectors(vsi);
2319		if (ret)
2320			goto unroll_vsi_init;
2321
2322		ret = ice_vsi_alloc_rings(vsi);
2323		if (ret)
2324			goto unroll_alloc_q_vector;
2325
2326		ret = ice_vsi_alloc_ring_stats(vsi);
2327		if (ret)
2328			goto unroll_vector_base;
2329
2330		vsi->stat_offsets_loaded = false;
2331
2332		/* Do not exit if configuring RSS had an issue, at least
2333		 * receive traffic on first queue. Hence no need to capture
2334		 * return value
2335		 */
2336		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2337			ice_vsi_cfg_rss_lut_key(vsi);
2338			ice_vsi_set_vf_rss_flow_fld(vsi);
2339		}
2340		break;
2341	case ICE_VSI_LB:
2342		ret = ice_vsi_alloc_rings(vsi);
2343		if (ret)
2344			goto unroll_vsi_init;
2345
2346		ret = ice_vsi_alloc_ring_stats(vsi);
2347		if (ret)
2348			goto unroll_vector_base;
2349
2350		break;
2351	default:
2352		/* clean up the resources and exit */
2353		ret = -EINVAL;
2354		goto unroll_vsi_init;
2355	}
2356
2357	return 0;
2358
2359unroll_vector_base:
2360	/* reclaim SW interrupts back to the common pool */
2361unroll_alloc_q_vector:
2362	ice_vsi_free_q_vectors(vsi);
2363unroll_vsi_init:
2364	ice_vsi_delete_from_hw(vsi);
2365unroll_get_qs:
2366	ice_vsi_put_qs(vsi);
2367unroll_vsi_alloc_stat:
2368	ice_vsi_free_stats(vsi);
2369unroll_vsi_alloc:
2370	ice_vsi_free_arrays(vsi);
2371	return ret;
2372}
2373
2374/**
2375 * ice_vsi_cfg - configure a previously allocated VSI
2376 * @vsi: pointer to VSI
2377 */
2378int ice_vsi_cfg(struct ice_vsi *vsi)
2379{
2380	struct ice_pf *pf = vsi->back;
2381	int ret;
2382
2383	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
2384		return -EINVAL;
2385
2386	ret = ice_vsi_cfg_def(vsi);
2387	if (ret)
2388		return ret;
2389
2390	ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2391	if (ret)
2392		ice_vsi_decfg(vsi);
2393
2394	if (vsi->type == ICE_VSI_CTRL) {
2395		if (vsi->vf) {
2396			WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2397			vsi->vf->ctrl_vsi_idx = vsi->idx;
2398		} else {
2399			WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2400			pf->ctrl_vsi_idx = vsi->idx;
2401		}
2402	}
2403
2404	return ret;
2405}
2406
2407/**
2408 * ice_vsi_decfg - remove all VSI configuration
2409 * @vsi: pointer to VSI
2410 */
2411void ice_vsi_decfg(struct ice_vsi *vsi)
2412{
2413	struct ice_pf *pf = vsi->back;
2414	int err;
2415
2416	/* The Rx rule will only exist to remove if the LLDP FW
2417	 * engine is currently stopped
2418	 */
2419	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2420	    !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2421		ice_cfg_sw_lldp(vsi, false, false);
2422
2423	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2424	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2425	if (err)
2426		dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2427			vsi->vsi_num, err);
2428
2429	if (ice_is_xdp_ena_vsi(vsi))
2430		/* return value check can be skipped here, it always returns
2431		 * 0 if reset is in progress
2432		 */
2433		ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_PART);
2434
2435	ice_vsi_clear_rings(vsi);
2436	ice_vsi_free_q_vectors(vsi);
2437	ice_vsi_put_qs(vsi);
2438	ice_vsi_free_arrays(vsi);
2439
2440	/* SR-IOV determines needed MSIX resources all at once instead of per
2441	 * VSI since when VFs are spawned we know how many VFs there are and how
2442	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2443	 * cleared in the same manner.
2444	 */
2445
2446	if (vsi->type == ICE_VSI_VF &&
2447	    vsi->agg_node && vsi->agg_node->valid)
2448		vsi->agg_node->num_vsis--;
2449}
2450
2451/**
2452 * ice_vsi_setup - Set up a VSI by a given type
2453 * @pf: board private structure
2454 * @params: parameters to use when creating the VSI
2455 *
2456 * This allocates the sw VSI structure and its queue resources.
2457 *
2458 * Returns pointer to the successfully allocated and configured VSI sw struct on
2459 * success, NULL on failure.
2460 */
2461struct ice_vsi *
2462ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2463{
2464	struct device *dev = ice_pf_to_dev(pf);
2465	struct ice_vsi *vsi;
2466	int ret;
2467
2468	/* ice_vsi_setup can only initialize a new VSI, and we must have
2469	 * a port_info structure for it.
2470	 */
2471	if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2472	    WARN_ON(!params->port_info))
2473		return NULL;
2474
2475	vsi = ice_vsi_alloc(pf);
2476	if (!vsi) {
2477		dev_err(dev, "could not allocate VSI\n");
2478		return NULL;
2479	}
2480
2481	vsi->params = *params;
2482	ret = ice_vsi_cfg(vsi);
2483	if (ret)
2484		goto err_vsi_cfg;
2485
2486	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2487	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2488	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2489	 * The rule is added once for PF VSI in order to create appropriate
2490	 * recipe, since VSI/VSI list is ignored with drop action...
2491	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2492	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2493	 * settings in the HW.
2494	 */
2495	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2496		ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2497				 ICE_DROP_PACKET);
2498		ice_cfg_sw_lldp(vsi, true, true);
2499	}
2500
2501	if (!vsi->agg_node)
2502		ice_set_agg_vsi(vsi);
2503
2504	return vsi;
2505
2506err_vsi_cfg:
2507	ice_vsi_free(vsi);
2508
2509	return NULL;
2510}
2511
2512/**
2513 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2514 * @vsi: the VSI being cleaned up
2515 */
2516static void ice_vsi_release_msix(struct ice_vsi *vsi)
2517{
2518	struct ice_pf *pf = vsi->back;
2519	struct ice_hw *hw = &pf->hw;
2520	u32 txq = 0;
2521	u32 rxq = 0;
2522	int i, q;
2523
2524	ice_for_each_q_vector(vsi, i) {
2525		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2526
2527		ice_write_intrl(q_vector, 0);
2528		for (q = 0; q < q_vector->num_ring_tx; q++) {
2529			ice_write_itr(&q_vector->tx, 0);
2530			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2531			if (ice_is_xdp_ena_vsi(vsi)) {
2532				u32 xdp_txq = txq + vsi->num_xdp_txq;
2533
2534				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2535			}
2536			txq++;
2537		}
2538
2539		for (q = 0; q < q_vector->num_ring_rx; q++) {
2540			ice_write_itr(&q_vector->rx, 0);
2541			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2542			rxq++;
2543		}
2544	}
2545
2546	ice_flush(hw);
2547}
2548
2549/**
2550 * ice_vsi_free_irq - Free the IRQ association with the OS
2551 * @vsi: the VSI being configured
2552 */
2553void ice_vsi_free_irq(struct ice_vsi *vsi)
2554{
2555	struct ice_pf *pf = vsi->back;
2556	int i;
2557
2558	if (!vsi->q_vectors || !vsi->irqs_ready)
2559		return;
2560
2561	ice_vsi_release_msix(vsi);
2562	if (vsi->type == ICE_VSI_VF)
2563		return;
2564
2565	vsi->irqs_ready = false;
2566	ice_free_cpu_rx_rmap(vsi);
2567
2568	ice_for_each_q_vector(vsi, i) {
2569		int irq_num;
2570
2571		irq_num = vsi->q_vectors[i]->irq.virq;
2572
2573		/* free only the irqs that were actually requested */
2574		if (!vsi->q_vectors[i] ||
2575		    !(vsi->q_vectors[i]->num_ring_tx ||
2576		      vsi->q_vectors[i]->num_ring_rx))
2577			continue;
2578
2579		/* clear the affinity notifier in the IRQ descriptor */
2580		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2581			irq_set_affinity_notifier(irq_num, NULL);
2582
2583		/* clear the affinity_mask in the IRQ descriptor */
2584		irq_set_affinity_hint(irq_num, NULL);
2585		synchronize_irq(irq_num);
2586		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2587	}
2588}
2589
2590/**
2591 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2592 * @vsi: the VSI having resources freed
2593 */
2594void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2595{
2596	int i;
2597
2598	if (!vsi->tx_rings)
2599		return;
2600
2601	ice_for_each_txq(vsi, i)
2602		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2603			ice_free_tx_ring(vsi->tx_rings[i]);
2604}
2605
2606/**
2607 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2608 * @vsi: the VSI having resources freed
2609 */
2610void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2611{
2612	int i;
2613
2614	if (!vsi->rx_rings)
2615		return;
2616
2617	ice_for_each_rxq(vsi, i)
2618		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2619			ice_free_rx_ring(vsi->rx_rings[i]);
2620}
2621
2622/**
2623 * ice_vsi_close - Shut down a VSI
2624 * @vsi: the VSI being shut down
2625 */
2626void ice_vsi_close(struct ice_vsi *vsi)
2627{
2628	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2629		ice_down(vsi);
2630
2631	ice_vsi_free_irq(vsi);
2632	ice_vsi_free_tx_rings(vsi);
2633	ice_vsi_free_rx_rings(vsi);
2634}
2635
2636/**
2637 * ice_ena_vsi - resume a VSI
2638 * @vsi: the VSI being resume
2639 * @locked: is the rtnl_lock already held
2640 */
2641int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2642{
2643	int err = 0;
2644
2645	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2646		return 0;
2647
2648	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2649
2650	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2651		if (netif_running(vsi->netdev)) {
2652			if (!locked)
2653				rtnl_lock();
2654
2655			err = ice_open_internal(vsi->netdev);
2656
2657			if (!locked)
2658				rtnl_unlock();
2659		}
2660	} else if (vsi->type == ICE_VSI_CTRL) {
2661		err = ice_vsi_open_ctrl(vsi);
2662	}
2663
2664	return err;
2665}
2666
2667/**
2668 * ice_dis_vsi - pause a VSI
2669 * @vsi: the VSI being paused
2670 * @locked: is the rtnl_lock already held
2671 */
2672void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2673{
2674	if (test_bit(ICE_VSI_DOWN, vsi->state))
2675		return;
2676
2677	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2678
2679	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2680		if (netif_running(vsi->netdev)) {
2681			if (!locked)
2682				rtnl_lock();
2683
2684			ice_vsi_close(vsi);
2685
2686			if (!locked)
2687				rtnl_unlock();
2688		} else {
2689			ice_vsi_close(vsi);
2690		}
2691	} else if (vsi->type == ICE_VSI_CTRL) {
2692		ice_vsi_close(vsi);
2693	}
2694}
2695
2696/**
2697 * __ice_queue_set_napi - Set the napi instance for the queue
2698 * @dev: device to which NAPI and queue belong
2699 * @queue_index: Index of queue
2700 * @type: queue type as RX or TX
2701 * @napi: NAPI context
2702 * @locked: is the rtnl_lock already held
2703 *
2704 * Set the napi instance for the queue. Caller indicates the lock status.
2705 */
2706static void
2707__ice_queue_set_napi(struct net_device *dev, unsigned int queue_index,
2708		     enum netdev_queue_type type, struct napi_struct *napi,
2709		     bool locked)
2710{
2711	if (!locked)
2712		rtnl_lock();
2713	netif_queue_set_napi(dev, queue_index, type, napi);
2714	if (!locked)
2715		rtnl_unlock();
2716}
2717
2718/**
2719 * ice_queue_set_napi - Set the napi instance for the queue
2720 * @vsi: VSI being configured
2721 * @queue_index: Index of queue
2722 * @type: queue type as RX or TX
2723 * @napi: NAPI context
2724 *
2725 * Set the napi instance for the queue. The rtnl lock state is derived from the
2726 * execution path.
2727 */
2728void
2729ice_queue_set_napi(struct ice_vsi *vsi, unsigned int queue_index,
2730		   enum netdev_queue_type type, struct napi_struct *napi)
2731{
2732	struct ice_pf *pf = vsi->back;
2733
2734	if (!vsi->netdev)
2735		return;
2736
2737	if (current_work() == &pf->serv_task ||
2738	    test_bit(ICE_PREPARED_FOR_RESET, pf->state) ||
2739	    test_bit(ICE_DOWN, pf->state) ||
2740	    test_bit(ICE_SUSPENDED, pf->state))
2741		__ice_queue_set_napi(vsi->netdev, queue_index, type, napi,
2742				     false);
2743	else
2744		__ice_queue_set_napi(vsi->netdev, queue_index, type, napi,
2745				     true);
2746}
2747
2748/**
2749 * __ice_q_vector_set_napi_queues - Map queue[s] associated with the napi
2750 * @q_vector: q_vector pointer
2751 * @locked: is the rtnl_lock already held
2752 *
2753 * Associate the q_vector napi with all the queue[s] on the vector.
2754 * Caller indicates the lock status.
2755 */
2756void __ice_q_vector_set_napi_queues(struct ice_q_vector *q_vector, bool locked)
2757{
2758	struct ice_rx_ring *rx_ring;
2759	struct ice_tx_ring *tx_ring;
2760
2761	ice_for_each_rx_ring(rx_ring, q_vector->rx)
2762		__ice_queue_set_napi(q_vector->vsi->netdev, rx_ring->q_index,
2763				     NETDEV_QUEUE_TYPE_RX, &q_vector->napi,
2764				     locked);
2765
2766	ice_for_each_tx_ring(tx_ring, q_vector->tx)
2767		__ice_queue_set_napi(q_vector->vsi->netdev, tx_ring->q_index,
2768				     NETDEV_QUEUE_TYPE_TX, &q_vector->napi,
2769				     locked);
2770	/* Also set the interrupt number for the NAPI */
2771	netif_napi_set_irq(&q_vector->napi, q_vector->irq.virq);
2772}
2773
2774/**
2775 * ice_q_vector_set_napi_queues - Map queue[s] associated with the napi
2776 * @q_vector: q_vector pointer
2777 *
2778 * Associate the q_vector napi with all the queue[s] on the vector
2779 */
2780void ice_q_vector_set_napi_queues(struct ice_q_vector *q_vector)
2781{
2782	struct ice_rx_ring *rx_ring;
2783	struct ice_tx_ring *tx_ring;
2784
2785	ice_for_each_rx_ring(rx_ring, q_vector->rx)
2786		ice_queue_set_napi(q_vector->vsi, rx_ring->q_index,
2787				   NETDEV_QUEUE_TYPE_RX, &q_vector->napi);
2788
2789	ice_for_each_tx_ring(tx_ring, q_vector->tx)
2790		ice_queue_set_napi(q_vector->vsi, tx_ring->q_index,
2791				   NETDEV_QUEUE_TYPE_TX, &q_vector->napi);
2792	/* Also set the interrupt number for the NAPI */
2793	netif_napi_set_irq(&q_vector->napi, q_vector->irq.virq);
2794}
2795
2796/**
2797 * ice_vsi_set_napi_queues
2798 * @vsi: VSI pointer
2799 *
2800 * Associate queue[s] with napi for all vectors
2801 */
2802void ice_vsi_set_napi_queues(struct ice_vsi *vsi)
2803{
2804	int i;
2805
2806	if (!vsi->netdev)
2807		return;
2808
2809	ice_for_each_q_vector(vsi, i)
2810		ice_q_vector_set_napi_queues(vsi->q_vectors[i]);
2811}
2812
2813/**
2814 * ice_vsi_release - Delete a VSI and free its resources
2815 * @vsi: the VSI being removed
2816 *
2817 * Returns 0 on success or < 0 on error
2818 */
2819int ice_vsi_release(struct ice_vsi *vsi)
2820{
2821	struct ice_pf *pf;
2822
2823	if (!vsi->back)
2824		return -ENODEV;
2825	pf = vsi->back;
2826
2827	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2828		ice_rss_clean(vsi);
2829
2830	ice_vsi_close(vsi);
2831	ice_vsi_decfg(vsi);
2832
2833	/* retain SW VSI data structure since it is needed to unregister and
2834	 * free VSI netdev when PF is not in reset recovery pending state,\
2835	 * for ex: during rmmod.
2836	 */
2837	if (!ice_is_reset_in_progress(pf->state))
2838		ice_vsi_delete(vsi);
2839
2840	return 0;
2841}
2842
2843/**
2844 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2845 * @vsi: VSI connected with q_vectors
2846 * @coalesce: array of struct with stored coalesce
2847 *
2848 * Returns array size.
2849 */
2850static int
2851ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2852			     struct ice_coalesce_stored *coalesce)
2853{
2854	int i;
2855
2856	ice_for_each_q_vector(vsi, i) {
2857		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2858
2859		coalesce[i].itr_tx = q_vector->tx.itr_settings;
2860		coalesce[i].itr_rx = q_vector->rx.itr_settings;
2861		coalesce[i].intrl = q_vector->intrl;
2862
2863		if (i < vsi->num_txq)
2864			coalesce[i].tx_valid = true;
2865		if (i < vsi->num_rxq)
2866			coalesce[i].rx_valid = true;
2867	}
2868
2869	return vsi->num_q_vectors;
2870}
2871
2872/**
2873 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2874 * @vsi: VSI connected with q_vectors
2875 * @coalesce: pointer to array of struct with stored coalesce
2876 * @size: size of coalesce array
2877 *
2878 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2879 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2880 * to default value.
2881 */
2882static void
2883ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2884			     struct ice_coalesce_stored *coalesce, int size)
2885{
2886	struct ice_ring_container *rc;
2887	int i;
2888
2889	if ((size && !coalesce) || !vsi)
2890		return;
2891
2892	/* There are a couple of cases that have to be handled here:
2893	 *   1. The case where the number of queue vectors stays the same, but
2894	 *      the number of Tx or Rx rings changes (the first for loop)
2895	 *   2. The case where the number of queue vectors increased (the
2896	 *      second for loop)
2897	 */
2898	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2899		/* There are 2 cases to handle here and they are the same for
2900		 * both Tx and Rx:
2901		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
2902		 *   and the loop variable is less than the number of rings
2903		 *   allocated, then write the previous values
2904		 *
2905		 *   if the entry was not valid previously, but the number of
2906		 *   rings is less than are allocated (this means the number of
2907		 *   rings increased from previously), then write out the
2908		 *   values in the first element
2909		 *
2910		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
2911		 *   as there is no harm because the dynamic algorithm
2912		 *   will just overwrite.
2913		 */
2914		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2915			rc = &vsi->q_vectors[i]->rx;
2916			rc->itr_settings = coalesce[i].itr_rx;
2917			ice_write_itr(rc, rc->itr_setting);
2918		} else if (i < vsi->alloc_rxq) {
2919			rc = &vsi->q_vectors[i]->rx;
2920			rc->itr_settings = coalesce[0].itr_rx;
2921			ice_write_itr(rc, rc->itr_setting);
2922		}
2923
2924		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2925			rc = &vsi->q_vectors[i]->tx;
2926			rc->itr_settings = coalesce[i].itr_tx;
2927			ice_write_itr(rc, rc->itr_setting);
2928		} else if (i < vsi->alloc_txq) {
2929			rc = &vsi->q_vectors[i]->tx;
2930			rc->itr_settings = coalesce[0].itr_tx;
2931			ice_write_itr(rc, rc->itr_setting);
2932		}
2933
2934		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2935		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2936	}
2937
2938	/* the number of queue vectors increased so write whatever is in
2939	 * the first element
2940	 */
2941	for (; i < vsi->num_q_vectors; i++) {
2942		/* transmit */
2943		rc = &vsi->q_vectors[i]->tx;
2944		rc->itr_settings = coalesce[0].itr_tx;
2945		ice_write_itr(rc, rc->itr_setting);
2946
2947		/* receive */
2948		rc = &vsi->q_vectors[i]->rx;
2949		rc->itr_settings = coalesce[0].itr_rx;
2950		ice_write_itr(rc, rc->itr_setting);
2951
2952		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
2953		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2954	}
2955}
2956
2957/**
2958 * ice_vsi_realloc_stat_arrays - Frees unused stat structures or alloc new ones
2959 * @vsi: VSI pointer
2960 */
2961static int
2962ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi)
2963{
2964	u16 req_txq = vsi->req_txq ? vsi->req_txq : vsi->alloc_txq;
2965	u16 req_rxq = vsi->req_rxq ? vsi->req_rxq : vsi->alloc_rxq;
2966	struct ice_ring_stats **tx_ring_stats;
2967	struct ice_ring_stats **rx_ring_stats;
2968	struct ice_vsi_stats *vsi_stat;
2969	struct ice_pf *pf = vsi->back;
2970	u16 prev_txq = vsi->alloc_txq;
2971	u16 prev_rxq = vsi->alloc_rxq;
2972	int i;
2973
2974	vsi_stat = pf->vsi_stats[vsi->idx];
2975
2976	if (req_txq < prev_txq) {
2977		for (i = req_txq; i < prev_txq; i++) {
2978			if (vsi_stat->tx_ring_stats[i]) {
2979				kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
2980				WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
2981			}
2982		}
2983	}
2984
2985	tx_ring_stats = vsi_stat->tx_ring_stats;
2986	vsi_stat->tx_ring_stats =
2987		krealloc_array(vsi_stat->tx_ring_stats, req_txq,
2988			       sizeof(*vsi_stat->tx_ring_stats),
2989			       GFP_KERNEL | __GFP_ZERO);
2990	if (!vsi_stat->tx_ring_stats) {
2991		vsi_stat->tx_ring_stats = tx_ring_stats;
2992		return -ENOMEM;
2993	}
2994
2995	if (req_rxq < prev_rxq) {
2996		for (i = req_rxq; i < prev_rxq; i++) {
2997			if (vsi_stat->rx_ring_stats[i]) {
2998				kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
2999				WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3000			}
3001		}
3002	}
3003
3004	rx_ring_stats = vsi_stat->rx_ring_stats;
3005	vsi_stat->rx_ring_stats =
3006		krealloc_array(vsi_stat->rx_ring_stats, req_rxq,
3007			       sizeof(*vsi_stat->rx_ring_stats),
3008			       GFP_KERNEL | __GFP_ZERO);
3009	if (!vsi_stat->rx_ring_stats) {
3010		vsi_stat->rx_ring_stats = rx_ring_stats;
3011		return -ENOMEM;
3012	}
3013
3014	return 0;
3015}
3016
3017/**
3018 * ice_vsi_rebuild - Rebuild VSI after reset
3019 * @vsi: VSI to be rebuild
3020 * @vsi_flags: flags used for VSI rebuild flow
3021 *
3022 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3023 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3024 *
3025 * Returns 0 on success and negative value on failure
3026 */
3027int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3028{
3029	struct ice_coalesce_stored *coalesce;
3030	int prev_num_q_vectors;
3031	struct ice_pf *pf;
3032	int ret;
3033
3034	if (!vsi)
3035		return -EINVAL;
3036
3037	vsi->flags = vsi_flags;
3038	pf = vsi->back;
3039	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3040		return -EINVAL;
3041
3042	ret = ice_vsi_realloc_stat_arrays(vsi);
3043	if (ret)
3044		goto err_vsi_cfg;
3045
3046	ice_vsi_decfg(vsi);
3047	ret = ice_vsi_cfg_def(vsi);
3048	if (ret)
3049		goto err_vsi_cfg;
3050
3051	coalesce = kcalloc(vsi->num_q_vectors,
3052			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3053	if (!coalesce)
3054		return -ENOMEM;
3055
3056	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3057
3058	ret = ice_vsi_cfg_tc_lan(pf, vsi);
3059	if (ret) {
3060		if (vsi_flags & ICE_VSI_FLAG_INIT) {
3061			ret = -EIO;
3062			goto err_vsi_cfg_tc_lan;
3063		}
3064
3065		kfree(coalesce);
3066		return ice_schedule_reset(pf, ICE_RESET_PFR);
3067	}
3068
3069	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3070	kfree(coalesce);
3071
3072	return 0;
3073
3074err_vsi_cfg_tc_lan:
3075	ice_vsi_decfg(vsi);
3076	kfree(coalesce);
3077err_vsi_cfg:
3078	return ret;
3079}
3080
3081/**
3082 * ice_is_reset_in_progress - check for a reset in progress
3083 * @state: PF state field
3084 */
3085bool ice_is_reset_in_progress(unsigned long *state)
3086{
3087	return test_bit(ICE_RESET_OICR_RECV, state) ||
3088	       test_bit(ICE_PFR_REQ, state) ||
3089	       test_bit(ICE_CORER_REQ, state) ||
3090	       test_bit(ICE_GLOBR_REQ, state);
3091}
3092
3093/**
3094 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3095 * @pf: pointer to the PF structure
3096 * @timeout: length of time to wait, in jiffies
3097 *
3098 * Wait (sleep) for a short time until the driver finishes cleaning up from
3099 * a device reset. The caller must be able to sleep. Use this to delay
3100 * operations that could fail while the driver is cleaning up after a device
3101 * reset.
3102 *
3103 * Returns 0 on success, -EBUSY if the reset is not finished within the
3104 * timeout, and -ERESTARTSYS if the thread was interrupted.
3105 */
3106int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3107{
3108	long ret;
3109
3110	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3111					       !ice_is_reset_in_progress(pf->state),
3112					       timeout);
3113	if (ret < 0)
3114		return ret;
3115	else if (!ret)
3116		return -EBUSY;
3117	else
3118		return 0;
3119}
3120
3121/**
3122 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3123 * @vsi: VSI being configured
3124 * @ctx: the context buffer returned from AQ VSI update command
3125 */
3126static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3127{
3128	vsi->info.mapping_flags = ctx->info.mapping_flags;
3129	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3130	       sizeof(vsi->info.q_mapping));
3131	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3132	       sizeof(vsi->info.tc_mapping));
3133}
3134
3135/**
3136 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3137 * @vsi: the VSI being configured
3138 * @ena_tc: TC map to be enabled
3139 */
3140void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3141{
3142	struct net_device *netdev = vsi->netdev;
3143	struct ice_pf *pf = vsi->back;
3144	int numtc = vsi->tc_cfg.numtc;
3145	struct ice_dcbx_cfg *dcbcfg;
3146	u8 netdev_tc;
3147	int i;
3148
3149	if (!netdev)
3150		return;
3151
3152	/* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3153	if (vsi->type == ICE_VSI_CHNL)
3154		return;
3155
3156	if (!ena_tc) {
3157		netdev_reset_tc(netdev);
3158		return;
3159	}
3160
3161	if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3162		numtc = vsi->all_numtc;
3163
3164	if (netdev_set_num_tc(netdev, numtc))
3165		return;
3166
3167	dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3168
3169	ice_for_each_traffic_class(i)
3170		if (vsi->tc_cfg.ena_tc & BIT(i))
3171			netdev_set_tc_queue(netdev,
3172					    vsi->tc_cfg.tc_info[i].netdev_tc,
3173					    vsi->tc_cfg.tc_info[i].qcount_tx,
3174					    vsi->tc_cfg.tc_info[i].qoffset);
3175	/* setup TC queue map for CHNL TCs */
3176	ice_for_each_chnl_tc(i) {
3177		if (!(vsi->all_enatc & BIT(i)))
3178			break;
3179		if (!vsi->mqprio_qopt.qopt.count[i])
3180			break;
3181		netdev_set_tc_queue(netdev, i,
3182				    vsi->mqprio_qopt.qopt.count[i],
3183				    vsi->mqprio_qopt.qopt.offset[i]);
3184	}
3185
3186	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3187		return;
3188
3189	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3190		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3191
3192		/* Get the mapped netdev TC# for the UP */
3193		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3194		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3195	}
3196}
3197
3198/**
3199 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3200 * @vsi: the VSI being configured,
3201 * @ctxt: VSI context structure
3202 * @ena_tc: number of traffic classes to enable
3203 *
3204 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3205 */
3206static int
3207ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3208			   u8 ena_tc)
3209{
3210	u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3211	u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3212	int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3213	u16 new_txq, new_rxq;
3214	u8 netdev_tc = 0;
3215	int i;
3216
3217	vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3218
3219	pow = order_base_2(tc0_qcount);
3220	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, tc0_offset);
3221	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
3222
3223	ice_for_each_traffic_class(i) {
3224		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3225			/* TC is not enabled */
3226			vsi->tc_cfg.tc_info[i].qoffset = 0;
3227			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3228			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3229			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3230			ctxt->info.tc_mapping[i] = 0;
3231			continue;
3232		}
3233
3234		offset = vsi->mqprio_qopt.qopt.offset[i];
3235		qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3236		qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3237		vsi->tc_cfg.tc_info[i].qoffset = offset;
3238		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3239		vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3240		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3241	}
3242
3243	if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3244		ice_for_each_chnl_tc(i) {
3245			if (!(vsi->all_enatc & BIT(i)))
3246				continue;
3247			offset = vsi->mqprio_qopt.qopt.offset[i];
3248			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3249			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3250		}
3251	}
3252
3253	new_txq = offset + qcount_tx;
3254	if (new_txq > vsi->alloc_txq) {
3255		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3256			new_txq, vsi->alloc_txq);
3257		return -EINVAL;
3258	}
3259
3260	new_rxq = offset + qcount_rx;
3261	if (new_rxq > vsi->alloc_rxq) {
3262		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3263			new_rxq, vsi->alloc_rxq);
3264		return -EINVAL;
3265	}
3266
3267	/* Set actual Tx/Rx queue pairs */
3268	vsi->num_txq = new_txq;
3269	vsi->num_rxq = new_rxq;
3270
3271	/* Setup queue TC[0].qmap for given VSI context */
3272	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3273	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3274	ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3275
3276	/* Find queue count available for channel VSIs and starting offset
3277	 * for channel VSIs
3278	 */
3279	if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3280		vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3281		vsi->next_base_q = tc0_qcount;
3282	}
3283	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n",  vsi->num_txq);
3284	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n",  vsi->num_rxq);
3285	dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3286		vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3287
3288	return 0;
3289}
3290
3291/**
3292 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3293 * @vsi: VSI to be configured
3294 * @ena_tc: TC bitmap
3295 *
3296 * VSI queues expected to be quiesced before calling this function
3297 */
3298int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3299{
3300	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3301	struct ice_pf *pf = vsi->back;
3302	struct ice_tc_cfg old_tc_cfg;
3303	struct ice_vsi_ctx *ctx;
3304	struct device *dev;
3305	int i, ret = 0;
3306	u8 num_tc = 0;
3307
3308	dev = ice_pf_to_dev(pf);
3309	if (vsi->tc_cfg.ena_tc == ena_tc &&
3310	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3311		return 0;
3312
3313	ice_for_each_traffic_class(i) {
3314		/* build bitmap of enabled TCs */
3315		if (ena_tc & BIT(i))
3316			num_tc++;
3317		/* populate max_txqs per TC */
3318		max_txqs[i] = vsi->alloc_txq;
3319		/* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3320		 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3321		 */
3322		if (vsi->type == ICE_VSI_CHNL &&
3323		    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3324			max_txqs[i] = vsi->num_txq;
3325	}
3326
3327	memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3328	vsi->tc_cfg.ena_tc = ena_tc;
3329	vsi->tc_cfg.numtc = num_tc;
3330
3331	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3332	if (!ctx)
3333		return -ENOMEM;
3334
3335	ctx->vf_num = 0;
3336	ctx->info = vsi->info;
3337
3338	if (vsi->type == ICE_VSI_PF &&
3339	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3340		ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3341	else
3342		ret = ice_vsi_setup_q_map(vsi, ctx);
3343
3344	if (ret) {
3345		memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3346		goto out;
3347	}
3348
3349	/* must to indicate which section of VSI context are being modified */
3350	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3351	ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3352	if (ret) {
3353		dev_info(dev, "Failed VSI Update\n");
3354		goto out;
3355	}
3356
3357	if (vsi->type == ICE_VSI_PF &&
3358	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3359		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3360	else
3361		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3362				      vsi->tc_cfg.ena_tc, max_txqs);
3363
3364	if (ret) {
3365		dev_err(dev, "VSI %d failed TC config, error %d\n",
3366			vsi->vsi_num, ret);
3367		goto out;
3368	}
3369	ice_vsi_update_q_map(vsi, ctx);
3370	vsi->info.valid_sections = 0;
3371
3372	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3373out:
3374	kfree(ctx);
3375	return ret;
3376}
3377
3378/**
3379 * ice_update_ring_stats - Update ring statistics
3380 * @stats: stats to be updated
3381 * @pkts: number of processed packets
3382 * @bytes: number of processed bytes
3383 *
3384 * This function assumes that caller has acquired a u64_stats_sync lock.
3385 */
3386static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3387{
3388	stats->bytes += bytes;
3389	stats->pkts += pkts;
3390}
3391
3392/**
3393 * ice_update_tx_ring_stats - Update Tx ring specific counters
3394 * @tx_ring: ring to update
3395 * @pkts: number of processed packets
3396 * @bytes: number of processed bytes
3397 */
3398void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3399{
3400	u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3401	ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3402	u64_stats_update_end(&tx_ring->ring_stats->syncp);
3403}
3404
3405/**
3406 * ice_update_rx_ring_stats - Update Rx ring specific counters
3407 * @rx_ring: ring to update
3408 * @pkts: number of processed packets
3409 * @bytes: number of processed bytes
3410 */
3411void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3412{
3413	u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3414	ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3415	u64_stats_update_end(&rx_ring->ring_stats->syncp);
3416}
3417
3418/**
3419 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3420 * @pi: port info of the switch with default VSI
3421 *
3422 * Return true if the there is a single VSI in default forwarding VSI list
3423 */
3424bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3425{
3426	bool exists = false;
3427
3428	ice_check_if_dflt_vsi(pi, 0, &exists);
3429	return exists;
3430}
3431
3432/**
3433 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3434 * @vsi: VSI to compare against default forwarding VSI
3435 *
3436 * If this VSI passed in is the default forwarding VSI then return true, else
3437 * return false
3438 */
3439bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3440{
3441	return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3442}
3443
3444/**
3445 * ice_set_dflt_vsi - set the default forwarding VSI
3446 * @vsi: VSI getting set as the default forwarding VSI on the switch
3447 *
3448 * If the VSI passed in is already the default VSI and it's enabled just return
3449 * success.
3450 *
3451 * Otherwise try to set the VSI passed in as the switch's default VSI and
3452 * return the result.
3453 */
3454int ice_set_dflt_vsi(struct ice_vsi *vsi)
3455{
3456	struct device *dev;
3457	int status;
3458
3459	if (!vsi)
3460		return -EINVAL;
3461
3462	dev = ice_pf_to_dev(vsi->back);
3463
3464	if (ice_lag_is_switchdev_running(vsi->back)) {
3465		dev_dbg(dev, "VSI %d passed is a part of LAG containing interfaces in switchdev mode, nothing to do\n",
3466			vsi->vsi_num);
3467		return 0;
3468	}
3469
3470	/* the VSI passed in is already the default VSI */
3471	if (ice_is_vsi_dflt_vsi(vsi)) {
3472		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3473			vsi->vsi_num);
3474		return 0;
3475	}
3476
3477	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3478	if (status) {
3479		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3480			vsi->vsi_num, status);
3481		return status;
3482	}
3483
3484	return 0;
3485}
3486
3487/**
3488 * ice_clear_dflt_vsi - clear the default forwarding VSI
3489 * @vsi: VSI to remove from filter list
3490 *
3491 * If the switch has no default VSI or it's not enabled then return error.
3492 *
3493 * Otherwise try to clear the default VSI and return the result.
3494 */
3495int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3496{
3497	struct device *dev;
3498	int status;
3499
3500	if (!vsi)
3501		return -EINVAL;
3502
3503	dev = ice_pf_to_dev(vsi->back);
3504
3505	/* there is no default VSI configured */
3506	if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3507		return -ENODEV;
3508
3509	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3510				  ICE_FLTR_RX);
3511	if (status) {
3512		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3513			vsi->vsi_num, status);
3514		return -EIO;
3515	}
3516
3517	return 0;
3518}
3519
3520/**
3521 * ice_get_link_speed_mbps - get link speed in Mbps
3522 * @vsi: the VSI whose link speed is being queried
3523 *
3524 * Return current VSI link speed and 0 if the speed is unknown.
3525 */
3526int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3527{
3528	unsigned int link_speed;
3529
3530	link_speed = vsi->port_info->phy.link_info.link_speed;
3531
3532	return (int)ice_get_link_speed(fls(link_speed) - 1);
3533}
3534
3535/**
3536 * ice_get_link_speed_kbps - get link speed in Kbps
3537 * @vsi: the VSI whose link speed is being queried
3538 *
3539 * Return current VSI link speed and 0 if the speed is unknown.
3540 */
3541int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3542{
3543	int speed_mbps;
3544
3545	speed_mbps = ice_get_link_speed_mbps(vsi);
3546
3547	return speed_mbps * 1000;
3548}
3549
3550/**
3551 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3552 * @vsi: VSI to be configured
3553 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3554 *
3555 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3556 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3557 * on TC 0.
3558 */
3559int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3560{
3561	struct ice_pf *pf = vsi->back;
3562	struct device *dev;
3563	int status;
3564	int speed;
3565
3566	dev = ice_pf_to_dev(pf);
3567	if (!vsi->port_info) {
3568		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3569			vsi->idx, vsi->type);
3570		return -EINVAL;
3571	}
3572
3573	speed = ice_get_link_speed_kbps(vsi);
3574	if (min_tx_rate > (u64)speed) {
3575		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3576			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3577			speed);
3578		return -EINVAL;
3579	}
3580
3581	/* Configure min BW for VSI limit */
3582	if (min_tx_rate) {
3583		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3584						   ICE_MIN_BW, min_tx_rate);
3585		if (status) {
3586			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3587				min_tx_rate, ice_vsi_type_str(vsi->type),
3588				vsi->idx);
3589			return status;
3590		}
3591
3592		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3593			min_tx_rate, ice_vsi_type_str(vsi->type));
3594	} else {
3595		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3596							vsi->idx, 0,
3597							ICE_MIN_BW);
3598		if (status) {
3599			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3600				ice_vsi_type_str(vsi->type), vsi->idx);
3601			return status;
3602		}
3603
3604		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3605			ice_vsi_type_str(vsi->type), vsi->idx);
3606	}
3607
3608	return 0;
3609}
3610
3611/**
3612 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3613 * @vsi: VSI to be configured
3614 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3615 *
3616 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3617 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3618 * on TC 0.
3619 */
3620int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3621{
3622	struct ice_pf *pf = vsi->back;
3623	struct device *dev;
3624	int status;
3625	int speed;
3626
3627	dev = ice_pf_to_dev(pf);
3628	if (!vsi->port_info) {
3629		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3630			vsi->idx, vsi->type);
3631		return -EINVAL;
3632	}
3633
3634	speed = ice_get_link_speed_kbps(vsi);
3635	if (max_tx_rate > (u64)speed) {
3636		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3637			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3638			speed);
3639		return -EINVAL;
3640	}
3641
3642	/* Configure max BW for VSI limit */
3643	if (max_tx_rate) {
3644		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3645						   ICE_MAX_BW, max_tx_rate);
3646		if (status) {
3647			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3648				max_tx_rate, ice_vsi_type_str(vsi->type),
3649				vsi->idx);
3650			return status;
3651		}
3652
3653		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3654			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3655	} else {
3656		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3657							vsi->idx, 0,
3658							ICE_MAX_BW);
3659		if (status) {
3660			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3661				ice_vsi_type_str(vsi->type), vsi->idx);
3662			return status;
3663		}
3664
3665		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3666			ice_vsi_type_str(vsi->type), vsi->idx);
3667	}
3668
3669	return 0;
3670}
3671
3672/**
3673 * ice_set_link - turn on/off physical link
3674 * @vsi: VSI to modify physical link on
3675 * @ena: turn on/off physical link
3676 */
3677int ice_set_link(struct ice_vsi *vsi, bool ena)
3678{
3679	struct device *dev = ice_pf_to_dev(vsi->back);
3680	struct ice_port_info *pi = vsi->port_info;
3681	struct ice_hw *hw = pi->hw;
3682	int status;
3683
3684	if (vsi->type != ICE_VSI_PF)
3685		return -EINVAL;
3686
3687	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3688
3689	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3690	 * this is not a fatal error, so print a warning message and return
3691	 * a success code. Return an error if FW returns an error code other
3692	 * than ICE_AQ_RC_EMODE
3693	 */
3694	if (status == -EIO) {
3695		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3696			dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3697				(ena ? "ON" : "OFF"), status,
3698				ice_aq_str(hw->adminq.sq_last_status));
3699	} else if (status) {
3700		dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3701			(ena ? "ON" : "OFF"), status,
3702			ice_aq_str(hw->adminq.sq_last_status));
3703		return status;
3704	}
3705
3706	return 0;
3707}
3708
3709/**
3710 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3711 * @vsi: VSI used to add VLAN filters
3712 *
3713 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3714 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3715 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3716 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3717 *
3718 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3719 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3720 * traffic in SVM, since the VLAN TPID isn't part of filtering.
3721 *
3722 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3723 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3724 * part of filtering.
3725 */
3726int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3727{
3728	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3729	struct ice_vlan vlan;
3730	int err;
3731
3732	vlan = ICE_VLAN(0, 0, 0);
3733	err = vlan_ops->add_vlan(vsi, &vlan);
3734	if (err && err != -EEXIST)
3735		return err;
3736
3737	/* in SVM both VLAN 0 filters are identical */
3738	if (!ice_is_dvm_ena(&vsi->back->hw))
3739		return 0;
3740
3741	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3742	err = vlan_ops->add_vlan(vsi, &vlan);
3743	if (err && err != -EEXIST)
3744		return err;
3745
3746	return 0;
3747}
3748
3749/**
3750 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3751 * @vsi: VSI used to add VLAN filters
3752 *
3753 * Delete the VLAN 0 filters in the same manner that they were added in
3754 * ice_vsi_add_vlan_zero.
3755 */
3756int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3757{
3758	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3759	struct ice_vlan vlan;
3760	int err;
3761
3762	vlan = ICE_VLAN(0, 0, 0);
3763	err = vlan_ops->del_vlan(vsi, &vlan);
3764	if (err && err != -EEXIST)
3765		return err;
3766
3767	/* in SVM both VLAN 0 filters are identical */
3768	if (!ice_is_dvm_ena(&vsi->back->hw))
3769		return 0;
3770
3771	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3772	err = vlan_ops->del_vlan(vsi, &vlan);
3773	if (err && err != -EEXIST)
3774		return err;
3775
3776	/* when deleting the last VLAN filter, make sure to disable the VLAN
3777	 * promisc mode so the filter isn't left by accident
3778	 */
3779	return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3780				    ICE_MCAST_VLAN_PROMISC_BITS, 0);
3781}
3782
3783/**
3784 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3785 * @vsi: VSI used to get the VLAN mode
3786 *
3787 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3788 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3789 */
3790static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3791{
3792#define ICE_DVM_NUM_ZERO_VLAN_FLTRS	2
3793#define ICE_SVM_NUM_ZERO_VLAN_FLTRS	1
3794	/* no VLAN 0 filter is created when a port VLAN is active */
3795	if (vsi->type == ICE_VSI_VF) {
3796		if (WARN_ON(!vsi->vf))
3797			return 0;
3798
3799		if (ice_vf_is_port_vlan_ena(vsi->vf))
3800			return 0;
3801	}
3802
3803	if (ice_is_dvm_ena(&vsi->back->hw))
3804		return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3805	else
3806		return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3807}
3808
3809/**
3810 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3811 * @vsi: VSI used to determine if any non-zero VLANs have been added
3812 */
3813bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3814{
3815	return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3816}
3817
3818/**
3819 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3820 * @vsi: VSI used to get the number of non-zero VLANs added
3821 */
3822u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3823{
3824	return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3825}
3826
3827/**
3828 * ice_is_feature_supported
3829 * @pf: pointer to the struct ice_pf instance
3830 * @f: feature enum to be checked
3831 *
3832 * returns true if feature is supported, false otherwise
3833 */
3834bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3835{
3836	if (f < 0 || f >= ICE_F_MAX)
3837		return false;
3838
3839	return test_bit(f, pf->features);
3840}
3841
3842/**
3843 * ice_set_feature_support
3844 * @pf: pointer to the struct ice_pf instance
3845 * @f: feature enum to set
3846 */
3847void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
3848{
3849	if (f < 0 || f >= ICE_F_MAX)
3850		return;
3851
3852	set_bit(f, pf->features);
3853}
3854
3855/**
3856 * ice_clear_feature_support
3857 * @pf: pointer to the struct ice_pf instance
3858 * @f: feature enum to clear
3859 */
3860void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
3861{
3862	if (f < 0 || f >= ICE_F_MAX)
3863		return;
3864
3865	clear_bit(f, pf->features);
3866}
3867
3868/**
3869 * ice_init_feature_support
3870 * @pf: pointer to the struct ice_pf instance
3871 *
3872 * called during init to setup supported feature
3873 */
3874void ice_init_feature_support(struct ice_pf *pf)
3875{
3876	switch (pf->hw.device_id) {
3877	case ICE_DEV_ID_E810C_BACKPLANE:
3878	case ICE_DEV_ID_E810C_QSFP:
3879	case ICE_DEV_ID_E810C_SFP:
3880	case ICE_DEV_ID_E810_XXV_BACKPLANE:
3881	case ICE_DEV_ID_E810_XXV_QSFP:
3882	case ICE_DEV_ID_E810_XXV_SFP:
3883		ice_set_feature_support(pf, ICE_F_DSCP);
3884		if (ice_is_phy_rclk_in_netlist(&pf->hw))
3885			ice_set_feature_support(pf, ICE_F_PHY_RCLK);
3886		/* If we don't own the timer - don't enable other caps */
3887		if (!ice_pf_src_tmr_owned(pf))
3888			break;
3889		if (ice_is_cgu_in_netlist(&pf->hw))
3890			ice_set_feature_support(pf, ICE_F_CGU);
3891		if (ice_is_clock_mux_in_netlist(&pf->hw))
3892			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
3893		if (ice_gnss_is_gps_present(&pf->hw))
3894			ice_set_feature_support(pf, ICE_F_GNSS);
3895		break;
3896	default:
3897		break;
3898	}
3899}
3900
3901/**
3902 * ice_vsi_update_security - update security block in VSI
3903 * @vsi: pointer to VSI structure
3904 * @fill: function pointer to fill ctx
3905 */
3906int
3907ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
3908{
3909	struct ice_vsi_ctx ctx = { 0 };
3910
3911	ctx.info = vsi->info;
3912	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
3913	fill(&ctx);
3914
3915	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3916		return -ENODEV;
3917
3918	vsi->info = ctx.info;
3919	return 0;
3920}
3921
3922/**
3923 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
3924 * @ctx: pointer to VSI ctx structure
3925 */
3926void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
3927{
3928	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
3929			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3930				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3931}
3932
3933/**
3934 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
3935 * @ctx: pointer to VSI ctx structure
3936 */
3937void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
3938{
3939	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
3940			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3941				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3942}
3943
3944/**
3945 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
3946 * @ctx: pointer to VSI ctx structure
3947 */
3948void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
3949{
3950	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3951}
3952
3953/**
3954 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
3955 * @ctx: pointer to VSI ctx structure
3956 */
3957void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
3958{
3959	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3960}
3961
3962/**
3963 * ice_vsi_update_local_lb - update sw block in VSI with local loopback bit
3964 * @vsi: pointer to VSI structure
3965 * @set: set or unset the bit
3966 */
3967int
3968ice_vsi_update_local_lb(struct ice_vsi *vsi, bool set)
3969{
3970	struct ice_vsi_ctx ctx = {
3971		.info	= vsi->info,
3972	};
3973
3974	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
3975	if (set)
3976		ctx.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3977	else
3978		ctx.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3979
3980	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3981		return -ENODEV;
3982
3983	vsi->info = ctx.info;
3984	return 0;
3985}
3986