1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2018, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_base.h"
6#include "ice_flow.h"
7#include "ice_lib.h"
8#include "ice_fltr.h"
9#include "ice_dcb_lib.h"
10#include "ice_devlink.h"
11
12/**
13 * ice_vsi_type_str - maps VSI type enum to string equivalents
14 * @vsi_type: VSI type enum
15 */
16const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
17{
18	switch (vsi_type) {
19	case ICE_VSI_PF:
20		return "ICE_VSI_PF";
21	case ICE_VSI_VF:
22		return "ICE_VSI_VF";
23	case ICE_VSI_CTRL:
24		return "ICE_VSI_CTRL";
25	case ICE_VSI_LB:
26		return "ICE_VSI_LB";
27	default:
28		return "unknown";
29	}
30}
31
32/**
33 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
34 * @vsi: the VSI being configured
35 * @ena: start or stop the Rx rings
36 *
37 * First enable/disable all of the Rx rings, flush any remaining writes, and
38 * then verify that they have all been enabled/disabled successfully. This will
39 * let all of the register writes complete when enabling/disabling the Rx rings
40 * before waiting for the change in hardware to complete.
41 */
42static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
43{
44	int ret = 0;
45	u16 i;
46
47	for (i = 0; i < vsi->num_rxq; i++)
48		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
49
50	ice_flush(&vsi->back->hw);
51
52	for (i = 0; i < vsi->num_rxq; i++) {
53		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
54		if (ret)
55			break;
56	}
57
58	return ret;
59}
60
61/**
62 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
63 * @vsi: VSI pointer
64 *
65 * On error: returns error code (negative)
66 * On success: returns 0
67 */
68static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
69{
70	struct ice_pf *pf = vsi->back;
71	struct device *dev;
72
73	dev = ice_pf_to_dev(pf);
74
75	/* allocate memory for both Tx and Rx ring pointers */
76	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
77				     sizeof(*vsi->tx_rings), GFP_KERNEL);
78	if (!vsi->tx_rings)
79		return -ENOMEM;
80
81	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
82				     sizeof(*vsi->rx_rings), GFP_KERNEL);
83	if (!vsi->rx_rings)
84		goto err_rings;
85
86	/* txq_map needs to have enough space to track both Tx (stack) rings
87	 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
88	 * so use num_possible_cpus() as we want to always provide XDP ring
89	 * per CPU, regardless of queue count settings from user that might
90	 * have come from ethtool's set_channels() callback;
91	 */
92	vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
93				    sizeof(*vsi->txq_map), GFP_KERNEL);
94
95	if (!vsi->txq_map)
96		goto err_txq_map;
97
98	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
99				    sizeof(*vsi->rxq_map), GFP_KERNEL);
100	if (!vsi->rxq_map)
101		goto err_rxq_map;
102
103	/* There is no need to allocate q_vectors for a loopback VSI. */
104	if (vsi->type == ICE_VSI_LB)
105		return 0;
106
107	/* allocate memory for q_vector pointers */
108	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
109				      sizeof(*vsi->q_vectors), GFP_KERNEL);
110	if (!vsi->q_vectors)
111		goto err_vectors;
112
113	return 0;
114
115err_vectors:
116	devm_kfree(dev, vsi->rxq_map);
117err_rxq_map:
118	devm_kfree(dev, vsi->txq_map);
119err_txq_map:
120	devm_kfree(dev, vsi->rx_rings);
121err_rings:
122	devm_kfree(dev, vsi->tx_rings);
123	return -ENOMEM;
124}
125
126/**
127 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
128 * @vsi: the VSI being configured
129 */
130static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
131{
132	switch (vsi->type) {
133	case ICE_VSI_PF:
134	case ICE_VSI_CTRL:
135	case ICE_VSI_LB:
136		/* a user could change the values of num_[tr]x_desc using
137		 * ethtool -G so we should keep those values instead of
138		 * overwriting them with the defaults.
139		 */
140		if (!vsi->num_rx_desc)
141			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
142		if (!vsi->num_tx_desc)
143			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
144		break;
145	default:
146		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
147			vsi->type);
148		break;
149	}
150}
151
152/**
153 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
154 * @vsi: the VSI being configured
155 * @vf_id: ID of the VF being configured
156 *
157 * Return 0 on success and a negative value on error
158 */
159static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
160{
161	struct ice_pf *pf = vsi->back;
162	struct ice_vf *vf = NULL;
163
164	if (vsi->type == ICE_VSI_VF)
165		vsi->vf_id = vf_id;
166
167	switch (vsi->type) {
168	case ICE_VSI_PF:
169		vsi->alloc_txq = min3(pf->num_lan_msix,
170				      ice_get_avail_txq_count(pf),
171				      (u16)num_online_cpus());
172		if (vsi->req_txq) {
173			vsi->alloc_txq = vsi->req_txq;
174			vsi->num_txq = vsi->req_txq;
175		}
176
177		pf->num_lan_tx = vsi->alloc_txq;
178
179		/* only 1 Rx queue unless RSS is enabled */
180		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
181			vsi->alloc_rxq = 1;
182		} else {
183			vsi->alloc_rxq = min3(pf->num_lan_msix,
184					      ice_get_avail_rxq_count(pf),
185					      (u16)num_online_cpus());
186			if (vsi->req_rxq) {
187				vsi->alloc_rxq = vsi->req_rxq;
188				vsi->num_rxq = vsi->req_rxq;
189			}
190		}
191
192		pf->num_lan_rx = vsi->alloc_rxq;
193
194		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
195					   max_t(int, vsi->alloc_rxq,
196						 vsi->alloc_txq));
197		break;
198	case ICE_VSI_VF:
199		vf = &pf->vf[vsi->vf_id];
200		if (vf->num_req_qs)
201			vf->num_vf_qs = vf->num_req_qs;
202		vsi->alloc_txq = vf->num_vf_qs;
203		vsi->alloc_rxq = vf->num_vf_qs;
204		/* pf->num_msix_per_vf includes (VF miscellaneous vector +
205		 * data queue interrupts). Since vsi->num_q_vectors is number
206		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
207		 * original vector count
208		 */
209		vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
210		break;
211	case ICE_VSI_CTRL:
212		vsi->alloc_txq = 1;
213		vsi->alloc_rxq = 1;
214		vsi->num_q_vectors = 1;
215		break;
216	case ICE_VSI_LB:
217		vsi->alloc_txq = 1;
218		vsi->alloc_rxq = 1;
219		break;
220	default:
221		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
222		break;
223	}
224
225	ice_vsi_set_num_desc(vsi);
226}
227
228/**
229 * ice_get_free_slot - get the next non-NULL location index in array
230 * @array: array to search
231 * @size: size of the array
232 * @curr: last known occupied index to be used as a search hint
233 *
234 * void * is being used to keep the functionality generic. This lets us use this
235 * function on any array of pointers.
236 */
237static int ice_get_free_slot(void *array, int size, int curr)
238{
239	int **tmp_array = (int **)array;
240	int next;
241
242	if (curr < (size - 1) && !tmp_array[curr + 1]) {
243		next = curr + 1;
244	} else {
245		int i = 0;
246
247		while ((i < size) && (tmp_array[i]))
248			i++;
249		if (i == size)
250			next = ICE_NO_VSI;
251		else
252			next = i;
253	}
254	return next;
255}
256
257/**
258 * ice_vsi_delete - delete a VSI from the switch
259 * @vsi: pointer to VSI being removed
260 */
261static void ice_vsi_delete(struct ice_vsi *vsi)
262{
263	struct ice_pf *pf = vsi->back;
264	struct ice_vsi_ctx *ctxt;
265	enum ice_status status;
266
267	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
268	if (!ctxt)
269		return;
270
271	if (vsi->type == ICE_VSI_VF)
272		ctxt->vf_num = vsi->vf_id;
273	ctxt->vsi_num = vsi->vsi_num;
274
275	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
276
277	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
278	if (status)
279		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
280			vsi->vsi_num, ice_stat_str(status));
281
282	kfree(ctxt);
283}
284
285/**
286 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
287 * @vsi: pointer to VSI being cleared
288 */
289static void ice_vsi_free_arrays(struct ice_vsi *vsi)
290{
291	struct ice_pf *pf = vsi->back;
292	struct device *dev;
293
294	dev = ice_pf_to_dev(pf);
295
296	/* free the ring and vector containers */
297	if (vsi->q_vectors) {
298		devm_kfree(dev, vsi->q_vectors);
299		vsi->q_vectors = NULL;
300	}
301	if (vsi->tx_rings) {
302		devm_kfree(dev, vsi->tx_rings);
303		vsi->tx_rings = NULL;
304	}
305	if (vsi->rx_rings) {
306		devm_kfree(dev, vsi->rx_rings);
307		vsi->rx_rings = NULL;
308	}
309	if (vsi->txq_map) {
310		devm_kfree(dev, vsi->txq_map);
311		vsi->txq_map = NULL;
312	}
313	if (vsi->rxq_map) {
314		devm_kfree(dev, vsi->rxq_map);
315		vsi->rxq_map = NULL;
316	}
317}
318
319/**
320 * ice_vsi_clear - clean up and deallocate the provided VSI
321 * @vsi: pointer to VSI being cleared
322 *
323 * This deallocates the VSI's queue resources, removes it from the PF's
324 * VSI array if necessary, and deallocates the VSI
325 *
326 * Returns 0 on success, negative on failure
327 */
328static int ice_vsi_clear(struct ice_vsi *vsi)
329{
330	struct ice_pf *pf = NULL;
331	struct device *dev;
332
333	if (!vsi)
334		return 0;
335
336	if (!vsi->back)
337		return -EINVAL;
338
339	pf = vsi->back;
340	dev = ice_pf_to_dev(pf);
341
342	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
343		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
344		return -EINVAL;
345	}
346
347	mutex_lock(&pf->sw_mutex);
348	/* updates the PF for this cleared VSI */
349
350	pf->vsi[vsi->idx] = NULL;
351	if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
352		pf->next_vsi = vsi->idx;
353
354	ice_vsi_free_arrays(vsi);
355	mutex_unlock(&pf->sw_mutex);
356	devm_kfree(dev, vsi);
357
358	return 0;
359}
360
361/**
362 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
363 * @irq: interrupt number
364 * @data: pointer to a q_vector
365 */
366static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
367{
368	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
369
370	if (!q_vector->tx.ring)
371		return IRQ_HANDLED;
372
373#define FDIR_RX_DESC_CLEAN_BUDGET 64
374	ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
375	ice_clean_ctrl_tx_irq(q_vector->tx.ring);
376
377	return IRQ_HANDLED;
378}
379
380/**
381 * ice_msix_clean_rings - MSIX mode Interrupt Handler
382 * @irq: interrupt number
383 * @data: pointer to a q_vector
384 */
385static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
386{
387	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
388
389	if (!q_vector->tx.ring && !q_vector->rx.ring)
390		return IRQ_HANDLED;
391
392	napi_schedule(&q_vector->napi);
393
394	return IRQ_HANDLED;
395}
396
397/**
398 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
399 * @pf: board private structure
400 * @vsi_type: type of VSI
401 * @vf_id: ID of the VF being configured
402 *
403 * returns a pointer to a VSI on success, NULL on failure.
404 */
405static struct ice_vsi *
406ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
407{
408	struct device *dev = ice_pf_to_dev(pf);
409	struct ice_vsi *vsi = NULL;
410
411	/* Need to protect the allocation of the VSIs at the PF level */
412	mutex_lock(&pf->sw_mutex);
413
414	/* If we have already allocated our maximum number of VSIs,
415	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
416	 * is available to be populated
417	 */
418	if (pf->next_vsi == ICE_NO_VSI) {
419		dev_dbg(dev, "out of VSI slots!\n");
420		goto unlock_pf;
421	}
422
423	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
424	if (!vsi)
425		goto unlock_pf;
426
427	vsi->type = vsi_type;
428	vsi->back = pf;
429	set_bit(__ICE_DOWN, vsi->state);
430
431	if (vsi_type == ICE_VSI_VF)
432		ice_vsi_set_num_qs(vsi, vf_id);
433	else
434		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
435
436	switch (vsi->type) {
437	case ICE_VSI_PF:
438		if (ice_vsi_alloc_arrays(vsi))
439			goto err_rings;
440
441		/* Setup default MSIX irq handler for VSI */
442		vsi->irq_handler = ice_msix_clean_rings;
443		break;
444	case ICE_VSI_CTRL:
445		if (ice_vsi_alloc_arrays(vsi))
446			goto err_rings;
447
448		/* Setup ctrl VSI MSIX irq handler */
449		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
450		break;
451	case ICE_VSI_VF:
452		if (ice_vsi_alloc_arrays(vsi))
453			goto err_rings;
454		break;
455	case ICE_VSI_LB:
456		if (ice_vsi_alloc_arrays(vsi))
457			goto err_rings;
458		break;
459	default:
460		dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
461		goto unlock_pf;
462	}
463
464	if (vsi->type == ICE_VSI_CTRL) {
465		/* Use the last VSI slot as the index for the control VSI */
466		vsi->idx = pf->num_alloc_vsi - 1;
467		pf->ctrl_vsi_idx = vsi->idx;
468		pf->vsi[vsi->idx] = vsi;
469	} else {
470		/* fill slot and make note of the index */
471		vsi->idx = pf->next_vsi;
472		pf->vsi[pf->next_vsi] = vsi;
473
474		/* prepare pf->next_vsi for next use */
475		pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
476						 pf->next_vsi);
477	}
478	goto unlock_pf;
479
480err_rings:
481	devm_kfree(dev, vsi);
482	vsi = NULL;
483unlock_pf:
484	mutex_unlock(&pf->sw_mutex);
485	return vsi;
486}
487
488/**
489 * ice_alloc_fd_res - Allocate FD resource for a VSI
490 * @vsi: pointer to the ice_vsi
491 *
492 * This allocates the FD resources
493 *
494 * Returns 0 on success, -EPERM on no-op or -EIO on failure
495 */
496static int ice_alloc_fd_res(struct ice_vsi *vsi)
497{
498	struct ice_pf *pf = vsi->back;
499	u32 g_val, b_val;
500
501	/* Flow Director filters are only allocated/assigned to the PF VSI which
502	 * passes the traffic. The CTRL VSI is only used to add/delete filters
503	 * so we don't allocate resources to it
504	 */
505
506	/* FD filters from guaranteed pool per VSI */
507	g_val = pf->hw.func_caps.fd_fltr_guar;
508	if (!g_val)
509		return -EPERM;
510
511	/* FD filters from best effort pool */
512	b_val = pf->hw.func_caps.fd_fltr_best_effort;
513	if (!b_val)
514		return -EPERM;
515
516	if (vsi->type != ICE_VSI_PF)
517		return -EPERM;
518
519	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
520		return -EPERM;
521
522	vsi->num_gfltr = g_val / pf->num_alloc_vsi;
523
524	/* each VSI gets same "best_effort" quota */
525	vsi->num_bfltr = b_val;
526
527	return 0;
528}
529
530/**
531 * ice_vsi_get_qs - Assign queues from PF to VSI
532 * @vsi: the VSI to assign queues to
533 *
534 * Returns 0 on success and a negative value on error
535 */
536static int ice_vsi_get_qs(struct ice_vsi *vsi)
537{
538	struct ice_pf *pf = vsi->back;
539	struct ice_qs_cfg tx_qs_cfg = {
540		.qs_mutex = &pf->avail_q_mutex,
541		.pf_map = pf->avail_txqs,
542		.pf_map_size = pf->max_pf_txqs,
543		.q_count = vsi->alloc_txq,
544		.scatter_count = ICE_MAX_SCATTER_TXQS,
545		.vsi_map = vsi->txq_map,
546		.vsi_map_offset = 0,
547		.mapping_mode = ICE_VSI_MAP_CONTIG
548	};
549	struct ice_qs_cfg rx_qs_cfg = {
550		.qs_mutex = &pf->avail_q_mutex,
551		.pf_map = pf->avail_rxqs,
552		.pf_map_size = pf->max_pf_rxqs,
553		.q_count = vsi->alloc_rxq,
554		.scatter_count = ICE_MAX_SCATTER_RXQS,
555		.vsi_map = vsi->rxq_map,
556		.vsi_map_offset = 0,
557		.mapping_mode = ICE_VSI_MAP_CONTIG
558	};
559	int ret;
560
561	ret = __ice_vsi_get_qs(&tx_qs_cfg);
562	if (ret)
563		return ret;
564	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
565
566	ret = __ice_vsi_get_qs(&rx_qs_cfg);
567	if (ret)
568		return ret;
569	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
570
571	return 0;
572}
573
574/**
575 * ice_vsi_put_qs - Release queues from VSI to PF
576 * @vsi: the VSI that is going to release queues
577 */
578static void ice_vsi_put_qs(struct ice_vsi *vsi)
579{
580	struct ice_pf *pf = vsi->back;
581	int i;
582
583	mutex_lock(&pf->avail_q_mutex);
584
585	for (i = 0; i < vsi->alloc_txq; i++) {
586		clear_bit(vsi->txq_map[i], pf->avail_txqs);
587		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
588	}
589
590	for (i = 0; i < vsi->alloc_rxq; i++) {
591		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
592		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
593	}
594
595	mutex_unlock(&pf->avail_q_mutex);
596}
597
598/**
599 * ice_is_safe_mode
600 * @pf: pointer to the PF struct
601 *
602 * returns true if driver is in safe mode, false otherwise
603 */
604bool ice_is_safe_mode(struct ice_pf *pf)
605{
606	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
607}
608
609/**
610 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
611 * @vsi: the VSI being cleaned up
612 *
613 * This function deletes RSS input set for all flows that were configured
614 * for this VSI
615 */
616static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
617{
618	struct ice_pf *pf = vsi->back;
619	enum ice_status status;
620
621	if (ice_is_safe_mode(pf))
622		return;
623
624	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
625	if (status)
626		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
627			vsi->vsi_num, ice_stat_str(status));
628}
629
630/**
631 * ice_rss_clean - Delete RSS related VSI structures and configuration
632 * @vsi: the VSI being removed
633 */
634static void ice_rss_clean(struct ice_vsi *vsi)
635{
636	struct ice_pf *pf = vsi->back;
637	struct device *dev;
638
639	dev = ice_pf_to_dev(pf);
640
641	if (vsi->rss_hkey_user)
642		devm_kfree(dev, vsi->rss_hkey_user);
643	if (vsi->rss_lut_user)
644		devm_kfree(dev, vsi->rss_lut_user);
645
646	ice_vsi_clean_rss_flow_fld(vsi);
647	/* remove RSS replay list */
648	if (!ice_is_safe_mode(pf))
649		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
650}
651
652/**
653 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
654 * @vsi: the VSI being configured
655 */
656static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
657{
658	struct ice_hw_common_caps *cap;
659	struct ice_pf *pf = vsi->back;
660
661	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
662		vsi->rss_size = 1;
663		return;
664	}
665
666	cap = &pf->hw.func_caps.common_cap;
667	switch (vsi->type) {
668	case ICE_VSI_PF:
669		/* PF VSI will inherit RSS instance of PF */
670		vsi->rss_table_size = (u16)cap->rss_table_size;
671		vsi->rss_size = min_t(u16, num_online_cpus(),
672				      BIT(cap->rss_table_entry_width));
673		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
674		break;
675	case ICE_VSI_VF:
676		/* VF VSI will get a small RSS table.
677		 * For VSI_LUT, LUT size should be set to 64 bytes.
678		 */
679		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
680		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
681		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
682		break;
683	case ICE_VSI_LB:
684		break;
685	default:
686		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
687			ice_vsi_type_str(vsi->type));
688		break;
689	}
690}
691
692/**
693 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
694 * @ctxt: the VSI context being set
695 *
696 * This initializes a default VSI context for all sections except the Queues.
697 */
698static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
699{
700	u32 table = 0;
701
702	memset(&ctxt->info, 0, sizeof(ctxt->info));
703	/* VSI's should be allocated from shared pool */
704	ctxt->alloc_from_pool = true;
705	/* Src pruning enabled by default */
706	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
707	/* Traffic from VSI can be sent to LAN */
708	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
709	/* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
710	 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
711	 * packets untagged/tagged.
712	 */
713	ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
714				  ICE_AQ_VSI_VLAN_MODE_M) >>
715				 ICE_AQ_VSI_VLAN_MODE_S);
716	/* Have 1:1 UP mapping for both ingress/egress tables */
717	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
718	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
719	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
720	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
721	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
722	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
723	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
724	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
725	ctxt->info.ingress_table = cpu_to_le32(table);
726	ctxt->info.egress_table = cpu_to_le32(table);
727	/* Have 1:1 UP mapping for outer to inner UP table */
728	ctxt->info.outer_up_table = cpu_to_le32(table);
729	/* No Outer tag support outer_tag_flags remains to zero */
730}
731
732/**
733 * ice_vsi_setup_q_map - Setup a VSI queue map
734 * @vsi: the VSI being configured
735 * @ctxt: VSI context structure
736 */
737static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
738{
739	u16 offset = 0, qmap = 0, tx_count = 0;
740	u16 qcount_tx = vsi->alloc_txq;
741	u16 qcount_rx = vsi->alloc_rxq;
742	u16 tx_numq_tc, rx_numq_tc;
743	u16 pow = 0, max_rss = 0;
744	bool ena_tc0 = false;
745	u8 netdev_tc = 0;
746	int i;
747
748	/* at least TC0 should be enabled by default */
749	if (vsi->tc_cfg.numtc) {
750		if (!(vsi->tc_cfg.ena_tc & BIT(0)))
751			ena_tc0 = true;
752	} else {
753		ena_tc0 = true;
754	}
755
756	if (ena_tc0) {
757		vsi->tc_cfg.numtc++;
758		vsi->tc_cfg.ena_tc |= 1;
759	}
760
761	rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
762	if (!rx_numq_tc)
763		rx_numq_tc = 1;
764	tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
765	if (!tx_numq_tc)
766		tx_numq_tc = 1;
767
768	/* TC mapping is a function of the number of Rx queues assigned to the
769	 * VSI for each traffic class and the offset of these queues.
770	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
771	 * queues allocated to TC0. No:of queues is a power-of-2.
772	 *
773	 * If TC is not enabled, the queue offset is set to 0, and allocate one
774	 * queue, this way, traffic for the given TC will be sent to the default
775	 * queue.
776	 *
777	 * Setup number and offset of Rx queues for all TCs for the VSI
778	 */
779
780	qcount_rx = rx_numq_tc;
781
782	/* qcount will change if RSS is enabled */
783	if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
784		if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
785			if (vsi->type == ICE_VSI_PF)
786				max_rss = ICE_MAX_LG_RSS_QS;
787			else
788				max_rss = ICE_MAX_RSS_QS_PER_VF;
789			qcount_rx = min_t(u16, rx_numq_tc, max_rss);
790			if (!vsi->req_rxq)
791				qcount_rx = min_t(u16, qcount_rx,
792						  vsi->rss_size);
793		}
794	}
795
796	/* find the (rounded up) power-of-2 of qcount */
797	pow = (u16)order_base_2(qcount_rx);
798
799	ice_for_each_traffic_class(i) {
800		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
801			/* TC is not enabled */
802			vsi->tc_cfg.tc_info[i].qoffset = 0;
803			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
804			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
805			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
806			ctxt->info.tc_mapping[i] = 0;
807			continue;
808		}
809
810		/* TC is enabled */
811		vsi->tc_cfg.tc_info[i].qoffset = offset;
812		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
813		vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
814		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
815
816		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
817			ICE_AQ_VSI_TC_Q_OFFSET_M) |
818			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
819			 ICE_AQ_VSI_TC_Q_NUM_M);
820		offset += qcount_rx;
821		tx_count += tx_numq_tc;
822		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
823	}
824
825	/* if offset is non-zero, means it is calculated correctly based on
826	 * enabled TCs for a given VSI otherwise qcount_rx will always
827	 * be correct and non-zero because it is based off - VSI's
828	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
829	 * at least 1)
830	 */
831	if (offset)
832		vsi->num_rxq = offset;
833	else
834		vsi->num_rxq = qcount_rx;
835
836	vsi->num_txq = tx_count;
837
838	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
839		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
840		/* since there is a chance that num_rxq could have been changed
841		 * in the above for loop, make num_txq equal to num_rxq.
842		 */
843		vsi->num_txq = vsi->num_rxq;
844	}
845
846	/* Rx queue mapping */
847	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
848	/* q_mapping buffer holds the info for the first queue allocated for
849	 * this VSI in the PF space and also the number of queues associated
850	 * with this VSI.
851	 */
852	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
853	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
854}
855
856/**
857 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
858 * @ctxt: the VSI context being set
859 * @vsi: the VSI being configured
860 */
861static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
862{
863	u8 dflt_q_group, dflt_q_prio;
864	u16 dflt_q, report_q, val;
865
866	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL)
867		return;
868
869	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
870	ctxt->info.valid_sections |= cpu_to_le16(val);
871	dflt_q = 0;
872	dflt_q_group = 0;
873	report_q = 0;
874	dflt_q_prio = 0;
875
876	/* enable flow director filtering/programming */
877	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
878	ctxt->info.fd_options = cpu_to_le16(val);
879	/* max of allocated flow director filters */
880	ctxt->info.max_fd_fltr_dedicated =
881			cpu_to_le16(vsi->num_gfltr);
882	/* max of shared flow director filters any VSI may program */
883	ctxt->info.max_fd_fltr_shared =
884			cpu_to_le16(vsi->num_bfltr);
885	/* default queue index within the VSI of the default FD */
886	val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
887	       ICE_AQ_VSI_FD_DEF_Q_M);
888	/* target queue or queue group to the FD filter */
889	val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
890		ICE_AQ_VSI_FD_DEF_GRP_M);
891	ctxt->info.fd_def_q = cpu_to_le16(val);
892	/* queue index on which FD filter completion is reported */
893	val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
894	       ICE_AQ_VSI_FD_REPORT_Q_M);
895	/* priority of the default qindex action */
896	val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
897		ICE_AQ_VSI_FD_DEF_PRIORITY_M);
898	ctxt->info.fd_report_opt = cpu_to_le16(val);
899}
900
901/**
902 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
903 * @ctxt: the VSI context being set
904 * @vsi: the VSI being configured
905 */
906static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
907{
908	u8 lut_type, hash_type;
909	struct device *dev;
910	struct ice_pf *pf;
911
912	pf = vsi->back;
913	dev = ice_pf_to_dev(pf);
914
915	switch (vsi->type) {
916	case ICE_VSI_PF:
917		/* PF VSI will inherit RSS instance of PF */
918		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
919		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
920		break;
921	case ICE_VSI_VF:
922		/* VF VSI will gets a small RSS table which is a VSI LUT type */
923		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
924		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
925		break;
926	default:
927		dev_dbg(dev, "Unsupported VSI type %s\n",
928			ice_vsi_type_str(vsi->type));
929		return;
930	}
931
932	ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
933				ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
934				(hash_type & ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
935}
936
937/**
938 * ice_vsi_init - Create and initialize a VSI
939 * @vsi: the VSI being configured
940 * @init_vsi: is this call creating a VSI
941 *
942 * This initializes a VSI context depending on the VSI type to be added and
943 * passes it down to the add_vsi aq command to create a new VSI.
944 */
945static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
946{
947	struct ice_pf *pf = vsi->back;
948	struct ice_hw *hw = &pf->hw;
949	struct ice_vsi_ctx *ctxt;
950	struct device *dev;
951	int ret = 0;
952
953	dev = ice_pf_to_dev(pf);
954	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
955	if (!ctxt)
956		return -ENOMEM;
957
958	switch (vsi->type) {
959	case ICE_VSI_CTRL:
960	case ICE_VSI_LB:
961	case ICE_VSI_PF:
962		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
963		break;
964	case ICE_VSI_VF:
965		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
966		/* VF number here is the absolute VF number (0-255) */
967		ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
968		break;
969	default:
970		ret = -ENODEV;
971		goto out;
972	}
973
974	ice_set_dflt_vsi_ctx(ctxt);
975	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
976		ice_set_fd_vsi_ctx(ctxt, vsi);
977	/* if the switch is in VEB mode, allow VSI loopback */
978	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
979		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
980
981	/* Set LUT type and HASH type if RSS is enabled */
982	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
983	    vsi->type != ICE_VSI_CTRL) {
984		ice_set_rss_vsi_ctx(ctxt, vsi);
985		/* if updating VSI context, make sure to set valid_section:
986		 * to indicate which section of VSI context being updated
987		 */
988		if (!init_vsi)
989			ctxt->info.valid_sections |=
990				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
991	}
992
993	ctxt->info.sw_id = vsi->port_info->sw_id;
994	ice_vsi_setup_q_map(vsi, ctxt);
995	if (!init_vsi) /* means VSI being updated */
996		/* must to indicate which section of VSI context are
997		 * being modified
998		 */
999		ctxt->info.valid_sections |=
1000			cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1001
1002	/* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
1003	 * respectively
1004	 */
1005	if (vsi->type == ICE_VSI_VF) {
1006		ctxt->info.valid_sections |=
1007			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1008		if (pf->vf[vsi->vf_id].spoofchk) {
1009			ctxt->info.sec_flags |=
1010				ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1011				(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1012				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1013		} else {
1014			ctxt->info.sec_flags &=
1015				~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1016				  (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1017				   ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1018		}
1019	}
1020
1021	/* Allow control frames out of main VSI */
1022	if (vsi->type == ICE_VSI_PF) {
1023		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1024		ctxt->info.valid_sections |=
1025			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1026	}
1027
1028	if (init_vsi) {
1029		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1030		if (ret) {
1031			dev_err(dev, "Add VSI failed, err %d\n", ret);
1032			ret = -EIO;
1033			goto out;
1034		}
1035	} else {
1036		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1037		if (ret) {
1038			dev_err(dev, "Update VSI failed, err %d\n", ret);
1039			ret = -EIO;
1040			goto out;
1041		}
1042	}
1043
1044	/* keep context for update VSI operations */
1045	vsi->info = ctxt->info;
1046
1047	/* record VSI number returned */
1048	vsi->vsi_num = ctxt->vsi_num;
1049
1050out:
1051	kfree(ctxt);
1052	return ret;
1053}
1054
1055/**
1056 * ice_free_res - free a block of resources
1057 * @res: pointer to the resource
1058 * @index: starting index previously returned by ice_get_res
1059 * @id: identifier to track owner
1060 *
1061 * Returns number of resources freed
1062 */
1063int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1064{
1065	int count = 0;
1066	int i;
1067
1068	if (!res || index >= res->end)
1069		return -EINVAL;
1070
1071	id |= ICE_RES_VALID_BIT;
1072	for (i = index; i < res->end && res->list[i] == id; i++) {
1073		res->list[i] = 0;
1074		count++;
1075	}
1076
1077	return count;
1078}
1079
1080/**
1081 * ice_search_res - Search the tracker for a block of resources
1082 * @res: pointer to the resource
1083 * @needed: size of the block needed
1084 * @id: identifier to track owner
1085 *
1086 * Returns the base item index of the block, or -ENOMEM for error
1087 */
1088static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1089{
1090	u16 start = 0, end = 0;
1091
1092	if (needed > res->end)
1093		return -ENOMEM;
1094
1095	id |= ICE_RES_VALID_BIT;
1096
1097	do {
1098		/* skip already allocated entries */
1099		if (res->list[end++] & ICE_RES_VALID_BIT) {
1100			start = end;
1101			if ((start + needed) > res->end)
1102				break;
1103		}
1104
1105		if (end == (start + needed)) {
1106			int i = start;
1107
1108			/* there was enough, so assign it to the requestor */
1109			while (i != end)
1110				res->list[i++] = id;
1111
1112			return start;
1113		}
1114	} while (end < res->end);
1115
1116	return -ENOMEM;
1117}
1118
1119/**
1120 * ice_get_free_res_count - Get free count from a resource tracker
1121 * @res: Resource tracker instance
1122 */
1123static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1124{
1125	u16 i, count = 0;
1126
1127	for (i = 0; i < res->end; i++)
1128		if (!(res->list[i] & ICE_RES_VALID_BIT))
1129			count++;
1130
1131	return count;
1132}
1133
1134/**
1135 * ice_get_res - get a block of resources
1136 * @pf: board private structure
1137 * @res: pointer to the resource
1138 * @needed: size of the block needed
1139 * @id: identifier to track owner
1140 *
1141 * Returns the base item index of the block, or negative for error
1142 */
1143int
1144ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1145{
1146	if (!res || !pf)
1147		return -EINVAL;
1148
1149	if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1150		dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1151			needed, res->num_entries, id);
1152		return -EINVAL;
1153	}
1154
1155	return ice_search_res(res, needed, id);
1156}
1157
1158/**
1159 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1160 * @vsi: ptr to the VSI
1161 *
1162 * This should only be called after ice_vsi_alloc() which allocates the
1163 * corresponding SW VSI structure and initializes num_queue_pairs for the
1164 * newly allocated VSI.
1165 *
1166 * Returns 0 on success or negative on failure
1167 */
1168static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1169{
1170	struct ice_pf *pf = vsi->back;
1171	struct device *dev;
1172	u16 num_q_vectors;
1173	int base;
1174
1175	dev = ice_pf_to_dev(pf);
1176	/* SRIOV doesn't grab irq_tracker entries for each VSI */
1177	if (vsi->type == ICE_VSI_VF)
1178		return 0;
1179
1180	if (vsi->base_vector) {
1181		dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1182			vsi->vsi_num, vsi->base_vector);
1183		return -EEXIST;
1184	}
1185
1186	num_q_vectors = vsi->num_q_vectors;
1187	/* reserve slots from OS requested IRQs */
1188	base = ice_get_res(pf, pf->irq_tracker, num_q_vectors, vsi->idx);
1189
1190	if (base < 0) {
1191		dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1192			ice_get_free_res_count(pf->irq_tracker),
1193			ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1194		return -ENOENT;
1195	}
1196	vsi->base_vector = (u16)base;
1197	pf->num_avail_sw_msix -= num_q_vectors;
1198
1199	return 0;
1200}
1201
1202/**
1203 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1204 * @vsi: the VSI having rings deallocated
1205 */
1206static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1207{
1208	int i;
1209
1210	/* Avoid stale references by clearing map from vector to ring */
1211	if (vsi->q_vectors) {
1212		ice_for_each_q_vector(vsi, i) {
1213			struct ice_q_vector *q_vector = vsi->q_vectors[i];
1214
1215			if (q_vector) {
1216				q_vector->tx.ring = NULL;
1217				q_vector->rx.ring = NULL;
1218			}
1219		}
1220	}
1221
1222	if (vsi->tx_rings) {
1223		for (i = 0; i < vsi->alloc_txq; i++) {
1224			if (vsi->tx_rings[i]) {
1225				kfree_rcu(vsi->tx_rings[i], rcu);
1226				WRITE_ONCE(vsi->tx_rings[i], NULL);
1227			}
1228		}
1229	}
1230	if (vsi->rx_rings) {
1231		for (i = 0; i < vsi->alloc_rxq; i++) {
1232			if (vsi->rx_rings[i]) {
1233				kfree_rcu(vsi->rx_rings[i], rcu);
1234				WRITE_ONCE(vsi->rx_rings[i], NULL);
1235			}
1236		}
1237	}
1238}
1239
1240/**
1241 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1242 * @vsi: VSI which is having rings allocated
1243 */
1244static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1245{
1246	struct ice_pf *pf = vsi->back;
1247	struct device *dev;
1248	u16 i;
1249
1250	dev = ice_pf_to_dev(pf);
1251	/* Allocate Tx rings */
1252	for (i = 0; i < vsi->alloc_txq; i++) {
1253		struct ice_ring *ring;
1254
1255		/* allocate with kzalloc(), free with kfree_rcu() */
1256		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1257
1258		if (!ring)
1259			goto err_out;
1260
1261		ring->q_index = i;
1262		ring->reg_idx = vsi->txq_map[i];
1263		ring->ring_active = false;
1264		ring->vsi = vsi;
1265		ring->dev = dev;
1266		ring->count = vsi->num_tx_desc;
1267		ring->txq_teid = ICE_INVAL_TEID;
1268		WRITE_ONCE(vsi->tx_rings[i], ring);
1269	}
1270
1271	/* Allocate Rx rings */
1272	for (i = 0; i < vsi->alloc_rxq; i++) {
1273		struct ice_ring *ring;
1274
1275		/* allocate with kzalloc(), free with kfree_rcu() */
1276		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1277		if (!ring)
1278			goto err_out;
1279
1280		ring->q_index = i;
1281		ring->reg_idx = vsi->rxq_map[i];
1282		ring->ring_active = false;
1283		ring->vsi = vsi;
1284		ring->netdev = vsi->netdev;
1285		ring->dev = dev;
1286		ring->count = vsi->num_rx_desc;
1287		WRITE_ONCE(vsi->rx_rings[i], ring);
1288	}
1289
1290	return 0;
1291
1292err_out:
1293	ice_vsi_clear_rings(vsi);
1294	return -ENOMEM;
1295}
1296
1297/**
1298 * ice_vsi_manage_rss_lut - disable/enable RSS
1299 * @vsi: the VSI being changed
1300 * @ena: boolean value indicating if this is an enable or disable request
1301 *
1302 * In the event of disable request for RSS, this function will zero out RSS
1303 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1304 * LUT.
1305 */
1306int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1307{
1308	int err = 0;
1309	u8 *lut;
1310
1311	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1312	if (!lut)
1313		return -ENOMEM;
1314
1315	if (ena) {
1316		if (vsi->rss_lut_user)
1317			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1318		else
1319			ice_fill_rss_lut(lut, vsi->rss_table_size,
1320					 vsi->rss_size);
1321	}
1322
1323	err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1324	kfree(lut);
1325	return err;
1326}
1327
1328/**
1329 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1330 * @vsi: VSI to be configured
1331 */
1332static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1333{
1334	struct ice_aqc_get_set_rss_keys *key;
1335	struct ice_pf *pf = vsi->back;
1336	enum ice_status status;
1337	struct device *dev;
1338	int err = 0;
1339	u8 *lut;
1340
1341	dev = ice_pf_to_dev(pf);
1342	vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1343
1344	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1345	if (!lut)
1346		return -ENOMEM;
1347
1348	if (vsi->rss_lut_user)
1349		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1350	else
1351		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1352
1353	status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1354				    vsi->rss_table_size);
1355
1356	if (status) {
1357		dev_err(dev, "set_rss_lut failed, error %s\n",
1358			ice_stat_str(status));
1359		err = -EIO;
1360		goto ice_vsi_cfg_rss_exit;
1361	}
1362
1363	key = kzalloc(sizeof(*key), GFP_KERNEL);
1364	if (!key) {
1365		err = -ENOMEM;
1366		goto ice_vsi_cfg_rss_exit;
1367	}
1368
1369	if (vsi->rss_hkey_user)
1370		memcpy(key,
1371		       (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1372		       ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1373	else
1374		netdev_rss_key_fill((void *)key,
1375				    ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1376
1377	status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1378
1379	if (status) {
1380		dev_err(dev, "set_rss_key failed, error %s\n",
1381			ice_stat_str(status));
1382		err = -EIO;
1383	}
1384
1385	kfree(key);
1386ice_vsi_cfg_rss_exit:
1387	kfree(lut);
1388	return err;
1389}
1390
1391/**
1392 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1393 * @vsi: VSI to be configured
1394 *
1395 * This function will only be called during the VF VSI setup. Upon successful
1396 * completion of package download, this function will configure default RSS
1397 * input sets for VF VSI.
1398 */
1399static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1400{
1401	struct ice_pf *pf = vsi->back;
1402	enum ice_status status;
1403	struct device *dev;
1404
1405	dev = ice_pf_to_dev(pf);
1406	if (ice_is_safe_mode(pf)) {
1407		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1408			vsi->vsi_num);
1409		return;
1410	}
1411
1412	status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1413	if (status)
1414		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1415			vsi->vsi_num, ice_stat_str(status));
1416}
1417
1418/**
1419 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1420 * @vsi: VSI to be configured
1421 *
1422 * This function will only be called after successful download package call
1423 * during initialization of PF. Since the downloaded package will erase the
1424 * RSS section, this function will configure RSS input sets for different
1425 * flow types. The last profile added has the highest priority, therefore 2
1426 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1427 * (i.e. IPv4 src/dst TCP src/dst port).
1428 */
1429static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1430{
1431	u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1432	struct ice_pf *pf = vsi->back;
1433	struct ice_hw *hw = &pf->hw;
1434	enum ice_status status;
1435	struct device *dev;
1436
1437	dev = ice_pf_to_dev(pf);
1438	if (ice_is_safe_mode(pf)) {
1439		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1440			vsi_num);
1441		return;
1442	}
1443	/* configure RSS for IPv4 with input set IP src/dst */
1444	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1445				 ICE_FLOW_SEG_HDR_IPV4);
1446	if (status)
1447		dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1448			vsi_num, ice_stat_str(status));
1449
1450	/* configure RSS for IPv6 with input set IPv6 src/dst */
1451	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1452				 ICE_FLOW_SEG_HDR_IPV6);
1453	if (status)
1454		dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1455			vsi_num, ice_stat_str(status));
1456
1457	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1458	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1459				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1460	if (status)
1461		dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1462			vsi_num, ice_stat_str(status));
1463
1464	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1465	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1466				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1467	if (status)
1468		dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1469			vsi_num, ice_stat_str(status));
1470
1471	/* configure RSS for sctp4 with input set IP src/dst */
1472	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1473				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1474	if (status)
1475		dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1476			vsi_num, ice_stat_str(status));
1477
1478	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1479	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1480				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1481	if (status)
1482		dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1483			vsi_num, ice_stat_str(status));
1484
1485	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1486	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1487				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1488	if (status)
1489		dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1490			vsi_num, ice_stat_str(status));
1491
1492	/* configure RSS for sctp6 with input set IPv6 src/dst */
1493	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1494				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1495	if (status)
1496		dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1497			vsi_num, ice_stat_str(status));
1498}
1499
1500/**
1501 * ice_pf_state_is_nominal - checks the PF for nominal state
1502 * @pf: pointer to PF to check
1503 *
1504 * Check the PF's state for a collection of bits that would indicate
1505 * the PF is in a state that would inhibit normal operation for
1506 * driver functionality.
1507 *
1508 * Returns true if PF is in a nominal state, false otherwise
1509 */
1510bool ice_pf_state_is_nominal(struct ice_pf *pf)
1511{
1512	DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1513
1514	if (!pf)
1515		return false;
1516
1517	bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1518	if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1519		return false;
1520
1521	return true;
1522}
1523
1524/**
1525 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1526 * @vsi: the VSI to be updated
1527 */
1528void ice_update_eth_stats(struct ice_vsi *vsi)
1529{
1530	struct ice_eth_stats *prev_es, *cur_es;
1531	struct ice_hw *hw = &vsi->back->hw;
1532	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1533
1534	prev_es = &vsi->eth_stats_prev;
1535	cur_es = &vsi->eth_stats;
1536
1537	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1538			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1539
1540	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1541			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1542
1543	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1544			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1545
1546	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1547			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1548
1549	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1550			  &prev_es->rx_discards, &cur_es->rx_discards);
1551
1552	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1553			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1554
1555	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1556			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1557
1558	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1559			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1560
1561	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1562			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1563
1564	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1565			  &prev_es->tx_errors, &cur_es->tx_errors);
1566
1567	vsi->stat_offsets_loaded = true;
1568}
1569
1570/**
1571 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1572 * @vsi: the VSI being configured
1573 * @vid: VLAN ID to be added
1574 * @action: filter action to be performed on match
1575 */
1576int
1577ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1578{
1579	struct ice_pf *pf = vsi->back;
1580	struct device *dev;
1581	int err = 0;
1582
1583	dev = ice_pf_to_dev(pf);
1584
1585	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1586		vsi->num_vlan++;
1587	} else {
1588		err = -ENODEV;
1589		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1590			vsi->vsi_num);
1591	}
1592
1593	return err;
1594}
1595
1596/**
1597 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1598 * @vsi: the VSI being configured
1599 * @vid: VLAN ID to be removed
1600 *
1601 * Returns 0 on success and negative on failure
1602 */
1603int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1604{
1605	struct ice_pf *pf = vsi->back;
1606	enum ice_status status;
1607	struct device *dev;
1608	int err = 0;
1609
1610	dev = ice_pf_to_dev(pf);
1611
1612	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1613	if (!status) {
1614		vsi->num_vlan--;
1615	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1616		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1617			vid, vsi->vsi_num, ice_stat_str(status));
1618	} else {
1619		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1620			vid, vsi->vsi_num, ice_stat_str(status));
1621		err = -EIO;
1622	}
1623
1624	return err;
1625}
1626
1627/**
1628 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1629 * @vsi: VSI
1630 */
1631void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1632{
1633	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1634		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1635		vsi->rx_buf_len = ICE_RXBUF_2048;
1636#if (PAGE_SIZE < 8192)
1637	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1638		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1639		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1640		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1641#endif
1642	} else {
1643		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1644#if (PAGE_SIZE < 8192)
1645		vsi->rx_buf_len = ICE_RXBUF_3072;
1646#else
1647		vsi->rx_buf_len = ICE_RXBUF_2048;
1648#endif
1649	}
1650}
1651
1652/**
1653 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1654 * @hw: HW pointer
1655 * @pf_q: index of the Rx queue in the PF's queue space
1656 * @rxdid: flexible descriptor RXDID
1657 * @prio: priority for the RXDID for this queue
1658 */
1659void
1660ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio)
1661{
1662	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1663
1664	/* clear any previous values */
1665	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1666		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1667		    QRXFLXP_CNTXT_TS_M);
1668
1669	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1670		QRXFLXP_CNTXT_RXDID_IDX_M;
1671
1672	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1673		QRXFLXP_CNTXT_RXDID_PRIO_M;
1674
1675	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1676}
1677
1678/**
1679 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1680 * @vsi: the VSI being configured
1681 *
1682 * Return 0 on success and a negative value on error
1683 * Configure the Rx VSI for operation.
1684 */
1685int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1686{
1687	u16 i;
1688
1689	if (vsi->type == ICE_VSI_VF)
1690		goto setup_rings;
1691
1692	ice_vsi_cfg_frame_size(vsi);
1693setup_rings:
1694	/* set up individual rings */
1695	for (i = 0; i < vsi->num_rxq; i++) {
1696		int err;
1697
1698		err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1699		if (err) {
1700			dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1701				i, err);
1702			return err;
1703		}
1704	}
1705
1706	return 0;
1707}
1708
1709/**
1710 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1711 * @vsi: the VSI being configured
1712 * @rings: Tx ring array to be configured
1713 * @count: number of Tx ring array elements
1714 *
1715 * Return 0 on success and a negative value on error
1716 * Configure the Tx VSI for operation.
1717 */
1718static int
1719ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, u16 count)
1720{
1721	struct ice_aqc_add_tx_qgrp *qg_buf;
1722	u16 q_idx = 0;
1723	int err = 0;
1724
1725	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1726	if (!qg_buf)
1727		return -ENOMEM;
1728
1729	qg_buf->num_txqs = 1;
1730
1731	for (q_idx = 0; q_idx < count; q_idx++) {
1732		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1733		if (err)
1734			goto err_cfg_txqs;
1735	}
1736
1737err_cfg_txqs:
1738	kfree(qg_buf);
1739	return err;
1740}
1741
1742/**
1743 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1744 * @vsi: the VSI being configured
1745 *
1746 * Return 0 on success and a negative value on error
1747 * Configure the Tx VSI for operation.
1748 */
1749int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1750{
1751	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1752}
1753
1754/**
1755 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1756 * @vsi: the VSI being configured
1757 *
1758 * Return 0 on success and a negative value on error
1759 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1760 */
1761int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1762{
1763	int ret;
1764	int i;
1765
1766	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1767	if (ret)
1768		return ret;
1769
1770	for (i = 0; i < vsi->num_xdp_txq; i++)
1771		vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1772
1773	return ret;
1774}
1775
1776/**
1777 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1778 * @intrl: interrupt rate limit in usecs
1779 * @gran: interrupt rate limit granularity in usecs
1780 *
1781 * This function converts a decimal interrupt rate limit in usecs to the format
1782 * expected by firmware.
1783 */
1784u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1785{
1786	u32 val = intrl / gran;
1787
1788	if (val)
1789		return val | GLINT_RATE_INTRL_ENA_M;
1790	return 0;
1791}
1792
1793/**
1794 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1795 * @vsi: the VSI being configured
1796 *
1797 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1798 * for the VF VSI.
1799 */
1800void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1801{
1802	struct ice_pf *pf = vsi->back;
1803	struct ice_hw *hw = &pf->hw;
1804	u16 txq = 0, rxq = 0;
1805	int i, q;
1806
1807	for (i = 0; i < vsi->num_q_vectors; i++) {
1808		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1809		u16 reg_idx = q_vector->reg_idx;
1810
1811		ice_cfg_itr(hw, q_vector);
1812
1813		wr32(hw, GLINT_RATE(reg_idx),
1814		     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1815
1816		/* Both Transmit Queue Interrupt Cause Control register
1817		 * and Receive Queue Interrupt Cause control register
1818		 * expects MSIX_INDX field to be the vector index
1819		 * within the function space and not the absolute
1820		 * vector index across PF or across device.
1821		 * For SR-IOV VF VSIs queue vector index always starts
1822		 * with 1 since first vector index(0) is used for OICR
1823		 * in VF space. Since VMDq and other PF VSIs are within
1824		 * the PF function space, use the vector index that is
1825		 * tracked for this PF.
1826		 */
1827		for (q = 0; q < q_vector->num_ring_tx; q++) {
1828			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1829					      q_vector->tx.itr_idx);
1830			txq++;
1831		}
1832
1833		for (q = 0; q < q_vector->num_ring_rx; q++) {
1834			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1835					      q_vector->rx.itr_idx);
1836			rxq++;
1837		}
1838	}
1839}
1840
1841/**
1842 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1843 * @vsi: the VSI being changed
1844 */
1845int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1846{
1847	struct ice_hw *hw = &vsi->back->hw;
1848	struct ice_vsi_ctx *ctxt;
1849	enum ice_status status;
1850	int ret = 0;
1851
1852	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1853	if (!ctxt)
1854		return -ENOMEM;
1855
1856	/* Here we are configuring the VSI to let the driver add VLAN tags by
1857	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1858	 * insertion happens in the Tx hot path, in ice_tx_map.
1859	 */
1860	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1861
1862	/* Preserve existing VLAN strip setting */
1863	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1864				  ICE_AQ_VSI_VLAN_EMOD_M);
1865
1866	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1867
1868	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1869	if (status) {
1870		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1871			ice_stat_str(status),
1872			ice_aq_str(hw->adminq.sq_last_status));
1873		ret = -EIO;
1874		goto out;
1875	}
1876
1877	vsi->info.vlan_flags = ctxt->info.vlan_flags;
1878out:
1879	kfree(ctxt);
1880	return ret;
1881}
1882
1883/**
1884 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1885 * @vsi: the VSI being changed
1886 * @ena: boolean value indicating if this is a enable or disable request
1887 */
1888int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1889{
1890	struct ice_hw *hw = &vsi->back->hw;
1891	struct ice_vsi_ctx *ctxt;
1892	enum ice_status status;
1893	int ret = 0;
1894
1895	/* do not allow modifying VLAN stripping when a port VLAN is configured
1896	 * on this VSI
1897	 */
1898	if (vsi->info.pvid)
1899		return 0;
1900
1901	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1902	if (!ctxt)
1903		return -ENOMEM;
1904
1905	/* Here we are configuring what the VSI should do with the VLAN tag in
1906	 * the Rx packet. We can either leave the tag in the packet or put it in
1907	 * the Rx descriptor.
1908	 */
1909	if (ena)
1910		/* Strip VLAN tag from Rx packet and put it in the desc */
1911		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1912	else
1913		/* Disable stripping. Leave tag in packet */
1914		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1915
1916	/* Allow all packets untagged/tagged */
1917	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1918
1919	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1920
1921	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1922	if (status) {
1923		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1924			ena, ice_stat_str(status),
1925			ice_aq_str(hw->adminq.sq_last_status));
1926		ret = -EIO;
1927		goto out;
1928	}
1929
1930	vsi->info.vlan_flags = ctxt->info.vlan_flags;
1931out:
1932	kfree(ctxt);
1933	return ret;
1934}
1935
1936/**
1937 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1938 * @vsi: the VSI whose rings are to be enabled
1939 *
1940 * Returns 0 on success and a negative value on error
1941 */
1942int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1943{
1944	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1945}
1946
1947/**
1948 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1949 * @vsi: the VSI whose rings are to be disabled
1950 *
1951 * Returns 0 on success and a negative value on error
1952 */
1953int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1954{
1955	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1956}
1957
1958/**
1959 * ice_vsi_stop_tx_rings - Disable Tx rings
1960 * @vsi: the VSI being configured
1961 * @rst_src: reset source
1962 * @rel_vmvf_num: Relative ID of VF/VM
1963 * @rings: Tx ring array to be stopped
1964 * @count: number of Tx ring array elements
1965 */
1966static int
1967ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1968		      u16 rel_vmvf_num, struct ice_ring **rings, u16 count)
1969{
1970	u16 q_idx;
1971
1972	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1973		return -EINVAL;
1974
1975	for (q_idx = 0; q_idx < count; q_idx++) {
1976		struct ice_txq_meta txq_meta = { };
1977		int status;
1978
1979		if (!rings || !rings[q_idx])
1980			return -EINVAL;
1981
1982		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1983		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1984					      rings[q_idx], &txq_meta);
1985
1986		if (status)
1987			return status;
1988	}
1989
1990	return 0;
1991}
1992
1993/**
1994 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1995 * @vsi: the VSI being configured
1996 * @rst_src: reset source
1997 * @rel_vmvf_num: Relative ID of VF/VM
1998 */
1999int
2000ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2001			  u16 rel_vmvf_num)
2002{
2003	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2004}
2005
2006/**
2007 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2008 * @vsi: the VSI being configured
2009 */
2010int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2011{
2012	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2013}
2014
2015/**
2016 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2017 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2018 *
2019 * returns true if Rx VLAN pruning is enabled and false otherwise.
2020 */
2021bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2022{
2023	if (!vsi)
2024		return false;
2025
2026	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2027}
2028
2029/**
2030 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2031 * @vsi: VSI to enable or disable VLAN pruning on
2032 * @ena: set to true to enable VLAN pruning and false to disable it
2033 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2034 *
2035 * returns 0 if VSI is updated, negative otherwise
2036 */
2037int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2038{
2039	struct ice_vsi_ctx *ctxt;
2040	struct ice_pf *pf;
2041	int status;
2042
2043	if (!vsi)
2044		return -EINVAL;
2045
2046	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
2047	 * mode. VLAN pruning will be enabled when the interface exits
2048	 * promiscuous mode if any VLAN filters are active.
2049	 */
2050	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2051		return 0;
2052
2053	pf = vsi->back;
2054	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2055	if (!ctxt)
2056		return -ENOMEM;
2057
2058	ctxt->info = vsi->info;
2059
2060	if (ena)
2061		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2062	else
2063		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2064
2065	if (!vlan_promisc)
2066		ctxt->info.valid_sections =
2067			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2068
2069	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2070	if (status) {
2071		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2072			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2073			   ice_stat_str(status),
2074			   ice_aq_str(pf->hw.adminq.sq_last_status));
2075		goto err_out;
2076	}
2077
2078	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2079
2080	kfree(ctxt);
2081	return 0;
2082
2083err_out:
2084	kfree(ctxt);
2085	return -EIO;
2086}
2087
2088static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2089{
2090	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2091
2092	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2093	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2094}
2095
2096/**
2097 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2098 * @vsi: VSI to set the q_vectors register index on
2099 */
2100static int
2101ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2102{
2103	u16 i;
2104
2105	if (!vsi || !vsi->q_vectors)
2106		return -EINVAL;
2107
2108	ice_for_each_q_vector(vsi, i) {
2109		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2110
2111		if (!q_vector) {
2112			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2113				i, vsi->vsi_num);
2114			goto clear_reg_idx;
2115		}
2116
2117		if (vsi->type == ICE_VSI_VF) {
2118			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2119
2120			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2121		} else {
2122			q_vector->reg_idx =
2123				q_vector->v_idx + vsi->base_vector;
2124		}
2125	}
2126
2127	return 0;
2128
2129clear_reg_idx:
2130	ice_for_each_q_vector(vsi, i) {
2131		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2132
2133		if (q_vector)
2134			q_vector->reg_idx = 0;
2135	}
2136
2137	return -EINVAL;
2138}
2139
2140/**
2141 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2142 * @vsi: the VSI being configured
2143 * @tx: bool to determine Tx or Rx rule
2144 * @create: bool to determine create or remove Rule
2145 */
2146void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2147{
2148	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2149				    enum ice_sw_fwd_act_type act);
2150	struct ice_pf *pf = vsi->back;
2151	enum ice_status status;
2152	struct device *dev;
2153
2154	dev = ice_pf_to_dev(pf);
2155	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2156
2157	if (tx)
2158		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2159				  ICE_DROP_PACKET);
2160	else
2161		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX, ICE_FWD_TO_VSI);
2162
2163	if (status)
2164		dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2165			create ? "adding" : "removing", tx ? "TX" : "RX",
2166			vsi->vsi_num, ice_stat_str(status));
2167}
2168
2169/**
2170 * ice_vsi_setup - Set up a VSI by a given type
2171 * @pf: board private structure
2172 * @pi: pointer to the port_info instance
2173 * @vsi_type: VSI type
2174 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2175 *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2176 *         fill-in ICE_INVAL_VFID as input.
2177 *
2178 * This allocates the sw VSI structure and its queue resources.
2179 *
2180 * Returns pointer to the successfully allocated and configured VSI sw struct on
2181 * success, NULL on failure.
2182 */
2183struct ice_vsi *
2184ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2185	      enum ice_vsi_type vsi_type, u16 vf_id)
2186{
2187	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2188	struct device *dev = ice_pf_to_dev(pf);
2189	enum ice_status status;
2190	struct ice_vsi *vsi;
2191	int ret, i;
2192
2193	if (vsi_type == ICE_VSI_VF)
2194		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2195	else
2196		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2197
2198	if (!vsi) {
2199		dev_err(dev, "could not allocate VSI\n");
2200		return NULL;
2201	}
2202
2203	vsi->port_info = pi;
2204	vsi->vsw = pf->first_sw;
2205	if (vsi->type == ICE_VSI_PF)
2206		vsi->ethtype = ETH_P_PAUSE;
2207
2208	if (vsi->type == ICE_VSI_VF)
2209		vsi->vf_id = vf_id;
2210
2211	ice_alloc_fd_res(vsi);
2212
2213	if (ice_vsi_get_qs(vsi)) {
2214		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2215			vsi->idx);
2216		goto unroll_vsi_alloc;
2217	}
2218
2219	/* set RSS capabilities */
2220	ice_vsi_set_rss_params(vsi);
2221
2222	/* set TC configuration */
2223	ice_vsi_set_tc_cfg(vsi);
2224
2225	/* create the VSI */
2226	ret = ice_vsi_init(vsi, true);
2227	if (ret)
2228		goto unroll_get_qs;
2229
2230	switch (vsi->type) {
2231	case ICE_VSI_CTRL:
2232	case ICE_VSI_PF:
2233		ret = ice_vsi_alloc_q_vectors(vsi);
2234		if (ret)
2235			goto unroll_vsi_init;
2236
2237		ret = ice_vsi_setup_vector_base(vsi);
2238		if (ret)
2239			goto unroll_alloc_q_vector;
2240
2241		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2242		if (ret)
2243			goto unroll_vector_base;
2244
2245		ret = ice_vsi_alloc_rings(vsi);
2246		if (ret)
2247			goto unroll_vector_base;
2248
2249		/* Always add VLAN ID 0 switch rule by default. This is needed
2250		 * in order to allow all untagged and 0 tagged priority traffic
2251		 * if Rx VLAN pruning is enabled. Also there are cases where we
2252		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2253		 * so this handles those cases (i.e. adding the PF to a bridge
2254		 * without the 8021q module loaded).
2255		 */
2256		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2257		if (ret)
2258			goto unroll_clear_rings;
2259
2260		ice_vsi_map_rings_to_vectors(vsi);
2261
2262		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2263		if (vsi->type != ICE_VSI_CTRL)
2264			/* Do not exit if configuring RSS had an issue, at
2265			 * least receive traffic on first queue. Hence no
2266			 * need to capture return value
2267			 */
2268			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2269				ice_vsi_cfg_rss_lut_key(vsi);
2270				ice_vsi_set_rss_flow_fld(vsi);
2271			}
2272		ice_init_arfs(vsi);
2273		break;
2274	case ICE_VSI_VF:
2275		/* VF driver will take care of creating netdev for this type and
2276		 * map queues to vectors through Virtchnl, PF driver only
2277		 * creates a VSI and corresponding structures for bookkeeping
2278		 * purpose
2279		 */
2280		ret = ice_vsi_alloc_q_vectors(vsi);
2281		if (ret)
2282			goto unroll_vsi_init;
2283
2284		ret = ice_vsi_alloc_rings(vsi);
2285		if (ret)
2286			goto unroll_alloc_q_vector;
2287
2288		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2289		if (ret)
2290			goto unroll_vector_base;
2291
2292		/* Do not exit if configuring RSS had an issue, at least
2293		 * receive traffic on first queue. Hence no need to capture
2294		 * return value
2295		 */
2296		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2297			ice_vsi_cfg_rss_lut_key(vsi);
2298			ice_vsi_set_vf_rss_flow_fld(vsi);
2299		}
2300		break;
2301	case ICE_VSI_LB:
2302		ret = ice_vsi_alloc_rings(vsi);
2303		if (ret)
2304			goto unroll_vsi_init;
2305		break;
2306	default:
2307		/* clean up the resources and exit */
2308		goto unroll_vsi_init;
2309	}
2310
2311	/* configure VSI nodes based on number of queues and TC's */
2312	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2313		max_txqs[i] = vsi->alloc_txq;
2314
2315	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2316				 max_txqs);
2317	if (status) {
2318		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2319			vsi->vsi_num, ice_stat_str(status));
2320		goto unroll_clear_rings;
2321	}
2322
2323	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2324	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2325	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2326	 * The rule is added once for PF VSI in order to create appropriate
2327	 * recipe, since VSI/VSI list is ignored with drop action...
2328	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2329	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2330	 * settings in the HW.
2331	 */
2332	if (!ice_is_safe_mode(pf))
2333		if (vsi->type == ICE_VSI_PF) {
2334			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2335					 ICE_DROP_PACKET);
2336			ice_cfg_sw_lldp(vsi, true, true);
2337		}
2338
2339	return vsi;
2340
2341unroll_clear_rings:
2342	ice_vsi_clear_rings(vsi);
2343unroll_vector_base:
2344	/* reclaim SW interrupts back to the common pool */
2345	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2346	pf->num_avail_sw_msix += vsi->num_q_vectors;
2347unroll_alloc_q_vector:
2348	ice_vsi_free_q_vectors(vsi);
2349unroll_vsi_init:
2350	ice_vsi_delete(vsi);
2351unroll_get_qs:
2352	ice_vsi_put_qs(vsi);
2353unroll_vsi_alloc:
2354	ice_vsi_clear(vsi);
2355
2356	return NULL;
2357}
2358
2359/**
2360 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2361 * @vsi: the VSI being cleaned up
2362 */
2363static void ice_vsi_release_msix(struct ice_vsi *vsi)
2364{
2365	struct ice_pf *pf = vsi->back;
2366	struct ice_hw *hw = &pf->hw;
2367	u32 txq = 0;
2368	u32 rxq = 0;
2369	int i, q;
2370
2371	for (i = 0; i < vsi->num_q_vectors; i++) {
2372		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2373		u16 reg_idx = q_vector->reg_idx;
2374
2375		wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2376		wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2377		for (q = 0; q < q_vector->num_ring_tx; q++) {
2378			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2379			if (ice_is_xdp_ena_vsi(vsi)) {
2380				u32 xdp_txq = txq + vsi->num_xdp_txq;
2381
2382				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2383			}
2384			txq++;
2385		}
2386
2387		for (q = 0; q < q_vector->num_ring_rx; q++) {
2388			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2389			rxq++;
2390		}
2391	}
2392
2393	ice_flush(hw);
2394}
2395
2396/**
2397 * ice_vsi_free_irq - Free the IRQ association with the OS
2398 * @vsi: the VSI being configured
2399 */
2400void ice_vsi_free_irq(struct ice_vsi *vsi)
2401{
2402	struct ice_pf *pf = vsi->back;
2403	int base = vsi->base_vector;
2404	int i;
2405
2406	if (!vsi->q_vectors || !vsi->irqs_ready)
2407		return;
2408
2409	ice_vsi_release_msix(vsi);
2410	if (vsi->type == ICE_VSI_VF)
2411		return;
2412
2413	vsi->irqs_ready = false;
2414	ice_for_each_q_vector(vsi, i) {
2415		u16 vector = i + base;
2416		int irq_num;
2417
2418		irq_num = pf->msix_entries[vector].vector;
2419
2420		/* free only the irqs that were actually requested */
2421		if (!vsi->q_vectors[i] ||
2422		    !(vsi->q_vectors[i]->num_ring_tx ||
2423		      vsi->q_vectors[i]->num_ring_rx))
2424			continue;
2425
2426		/* clear the affinity notifier in the IRQ descriptor */
2427		irq_set_affinity_notifier(irq_num, NULL);
2428
2429		/* clear the affinity_mask in the IRQ descriptor */
2430		irq_set_affinity_hint(irq_num, NULL);
2431		synchronize_irq(irq_num);
2432		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2433	}
2434}
2435
2436/**
2437 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2438 * @vsi: the VSI having resources freed
2439 */
2440void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2441{
2442	int i;
2443
2444	if (!vsi->tx_rings)
2445		return;
2446
2447	ice_for_each_txq(vsi, i)
2448		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2449			ice_free_tx_ring(vsi->tx_rings[i]);
2450}
2451
2452/**
2453 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2454 * @vsi: the VSI having resources freed
2455 */
2456void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2457{
2458	int i;
2459
2460	if (!vsi->rx_rings)
2461		return;
2462
2463	ice_for_each_rxq(vsi, i)
2464		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2465			ice_free_rx_ring(vsi->rx_rings[i]);
2466}
2467
2468/**
2469 * ice_vsi_close - Shut down a VSI
2470 * @vsi: the VSI being shut down
2471 */
2472void ice_vsi_close(struct ice_vsi *vsi)
2473{
2474	if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2475		ice_down(vsi);
2476
2477	ice_vsi_free_irq(vsi);
2478	ice_vsi_free_tx_rings(vsi);
2479	ice_vsi_free_rx_rings(vsi);
2480}
2481
2482/**
2483 * ice_ena_vsi - resume a VSI
2484 * @vsi: the VSI being resume
2485 * @locked: is the rtnl_lock already held
2486 */
2487int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2488{
2489	int err = 0;
2490
2491	if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2492		return 0;
2493
2494	clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2495
2496	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2497		if (netif_running(vsi->netdev)) {
2498			if (!locked)
2499				rtnl_lock();
2500
2501			err = ice_open_internal(vsi->netdev);
2502
2503			if (!locked)
2504				rtnl_unlock();
2505		}
2506	} else if (vsi->type == ICE_VSI_CTRL) {
2507		err = ice_vsi_open_ctrl(vsi);
2508	}
2509
2510	return err;
2511}
2512
2513/**
2514 * ice_dis_vsi - pause a VSI
2515 * @vsi: the VSI being paused
2516 * @locked: is the rtnl_lock already held
2517 */
2518void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2519{
2520	if (test_bit(__ICE_DOWN, vsi->state))
2521		return;
2522
2523	set_bit(__ICE_NEEDS_RESTART, vsi->state);
2524
2525	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2526		if (netif_running(vsi->netdev)) {
2527			if (!locked)
2528				rtnl_lock();
2529
2530			ice_vsi_close(vsi);
2531
2532			if (!locked)
2533				rtnl_unlock();
2534		} else {
2535			ice_vsi_close(vsi);
2536		}
2537	} else if (vsi->type == ICE_VSI_CTRL) {
2538		ice_vsi_close(vsi);
2539	}
2540}
2541
2542/**
2543 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2544 * @vsi: the VSI being un-configured
2545 */
2546void ice_vsi_dis_irq(struct ice_vsi *vsi)
2547{
2548	int base = vsi->base_vector;
2549	struct ice_pf *pf = vsi->back;
2550	struct ice_hw *hw = &pf->hw;
2551	u32 val;
2552	int i;
2553
2554	/* disable interrupt causation from each queue */
2555	if (vsi->tx_rings) {
2556		ice_for_each_txq(vsi, i) {
2557			if (vsi->tx_rings[i]) {
2558				u16 reg;
2559
2560				reg = vsi->tx_rings[i]->reg_idx;
2561				val = rd32(hw, QINT_TQCTL(reg));
2562				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2563				wr32(hw, QINT_TQCTL(reg), val);
2564			}
2565		}
2566	}
2567
2568	if (vsi->rx_rings) {
2569		ice_for_each_rxq(vsi, i) {
2570			if (vsi->rx_rings[i]) {
2571				u16 reg;
2572
2573				reg = vsi->rx_rings[i]->reg_idx;
2574				val = rd32(hw, QINT_RQCTL(reg));
2575				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2576				wr32(hw, QINT_RQCTL(reg), val);
2577			}
2578		}
2579	}
2580
2581	/* disable each interrupt */
2582	ice_for_each_q_vector(vsi, i) {
2583		if (!vsi->q_vectors[i])
2584			continue;
2585		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2586	}
2587
2588	ice_flush(hw);
2589
2590	/* don't call synchronize_irq() for VF's from the host */
2591	if (vsi->type == ICE_VSI_VF)
2592		return;
2593
2594	ice_for_each_q_vector(vsi, i)
2595		synchronize_irq(pf->msix_entries[i + base].vector);
2596}
2597
2598/**
2599 * ice_napi_del - Remove NAPI handler for the VSI
2600 * @vsi: VSI for which NAPI handler is to be removed
2601 */
2602void ice_napi_del(struct ice_vsi *vsi)
2603{
2604	int v_idx;
2605
2606	if (!vsi->netdev)
2607		return;
2608
2609	ice_for_each_q_vector(vsi, v_idx)
2610		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2611}
2612
2613/**
2614 * ice_vsi_release - Delete a VSI and free its resources
2615 * @vsi: the VSI being removed
2616 *
2617 * Returns 0 on success or < 0 on error
2618 */
2619int ice_vsi_release(struct ice_vsi *vsi)
2620{
2621	struct ice_pf *pf;
2622
2623	if (!vsi->back)
2624		return -ENODEV;
2625	pf = vsi->back;
2626
2627	/* do not unregister while driver is in the reset recovery pending
2628	 * state. Since reset/rebuild happens through PF service task workqueue,
2629	 * it's not a good idea to unregister netdev that is associated to the
2630	 * PF that is running the work queue items currently. This is done to
2631	 * avoid check_flush_dependency() warning on this wq
2632	 */
2633	if (vsi->netdev && !ice_is_reset_in_progress(pf->state)) {
2634		unregister_netdev(vsi->netdev);
2635		ice_devlink_destroy_port(vsi);
2636	}
2637
2638	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2639		ice_rss_clean(vsi);
2640
2641	/* Disable VSI and free resources */
2642	if (vsi->type != ICE_VSI_LB)
2643		ice_vsi_dis_irq(vsi);
2644	ice_vsi_close(vsi);
2645
2646	/* SR-IOV determines needed MSIX resources all at once instead of per
2647	 * VSI since when VFs are spawned we know how many VFs there are and how
2648	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2649	 * cleared in the same manner.
2650	 */
2651	if (vsi->type != ICE_VSI_VF) {
2652		/* reclaim SW interrupts back to the common pool */
2653		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2654		pf->num_avail_sw_msix += vsi->num_q_vectors;
2655	}
2656
2657	if (!ice_is_safe_mode(pf)) {
2658		if (vsi->type == ICE_VSI_PF) {
2659			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2660					    ICE_DROP_PACKET);
2661			ice_cfg_sw_lldp(vsi, true, false);
2662			/* The Rx rule will only exist to remove if the LLDP FW
2663			 * engine is currently stopped
2664			 */
2665			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2666				ice_cfg_sw_lldp(vsi, false, false);
2667		}
2668	}
2669
2670	if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
2671		ice_clear_dflt_vsi(pf->first_sw);
2672	ice_fltr_remove_all(vsi);
2673	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2674	ice_vsi_delete(vsi);
2675	ice_vsi_free_q_vectors(vsi);
2676
2677	/* make sure unregister_netdev() was called by checking __ICE_DOWN */
2678	if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2679		free_netdev(vsi->netdev);
2680		vsi->netdev = NULL;
2681	}
2682
2683	ice_vsi_clear_rings(vsi);
2684
2685	ice_vsi_put_qs(vsi);
2686
2687	/* retain SW VSI data structure since it is needed to unregister and
2688	 * free VSI netdev when PF is not in reset recovery pending state,\
2689	 * for ex: during rmmod.
2690	 */
2691	if (!ice_is_reset_in_progress(pf->state))
2692		ice_vsi_clear(vsi);
2693
2694	return 0;
2695}
2696
2697/**
2698 * ice_vsi_rebuild_update_coalesce_intrl - set interrupt rate limit for a q_vector
2699 * @q_vector: pointer to q_vector which is being updated
2700 * @stored_intrl_setting: original INTRL setting
2701 *
2702 * Set coalesce param in q_vector and update these parameters in HW.
2703 */
2704static void
2705ice_vsi_rebuild_update_coalesce_intrl(struct ice_q_vector *q_vector,
2706				      u16 stored_intrl_setting)
2707{
2708	struct ice_hw *hw = &q_vector->vsi->back->hw;
2709
2710	q_vector->intrl = stored_intrl_setting;
2711	wr32(hw, GLINT_RATE(q_vector->reg_idx),
2712	     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
2713}
2714
2715/**
2716 * ice_vsi_rebuild_update_coalesce_itr - set coalesce for a q_vector
2717 * @q_vector: pointer to q_vector which is being updated
2718 * @rc: pointer to ring container
2719 * @stored_itr_setting: original ITR setting
2720 *
2721 * Set coalesce param in q_vector and update these parameters in HW.
2722 */
2723static void
2724ice_vsi_rebuild_update_coalesce_itr(struct ice_q_vector *q_vector,
2725				    struct ice_ring_container *rc,
2726				    u16 stored_itr_setting)
2727{
2728	struct ice_hw *hw = &q_vector->vsi->back->hw;
2729
2730	rc->itr_setting = stored_itr_setting;
2731
2732	/* dynamic ITR values will be updated during Tx/Rx */
2733	if (!ITR_IS_DYNAMIC(rc->itr_setting))
2734		wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
2735		     ITR_REG_ALIGN(rc->itr_setting) >> ICE_ITR_GRAN_S);
2736}
2737
2738/**
2739 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2740 * @vsi: VSI connected with q_vectors
2741 * @coalesce: array of struct with stored coalesce
2742 *
2743 * Returns array size.
2744 */
2745static int
2746ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2747			     struct ice_coalesce_stored *coalesce)
2748{
2749	int i;
2750
2751	ice_for_each_q_vector(vsi, i) {
2752		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2753
2754		coalesce[i].itr_tx = q_vector->tx.itr_setting;
2755		coalesce[i].itr_rx = q_vector->rx.itr_setting;
2756		coalesce[i].intrl = q_vector->intrl;
2757
2758		if (i < vsi->num_txq)
2759			coalesce[i].tx_valid = true;
2760		if (i < vsi->num_rxq)
2761			coalesce[i].rx_valid = true;
2762	}
2763
2764	return vsi->num_q_vectors;
2765}
2766
2767/**
2768 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2769 * @vsi: VSI connected with q_vectors
2770 * @coalesce: pointer to array of struct with stored coalesce
2771 * @size: size of coalesce array
2772 *
2773 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2774 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2775 * to default value.
2776 */
2777static void
2778ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2779			     struct ice_coalesce_stored *coalesce, int size)
2780{
2781	int i;
2782
2783	if ((size && !coalesce) || !vsi)
2784		return;
2785
2786	/* There are a couple of cases that have to be handled here:
2787	 *   1. The case where the number of queue vectors stays the same, but
2788	 *      the number of Tx or Rx rings changes (the first for loop)
2789	 *   2. The case where the number of queue vectors increased (the
2790	 *      second for loop)
2791	 */
2792	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2793		/* There are 2 cases to handle here and they are the same for
2794		 * both Tx and Rx:
2795		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
2796		 *   and the loop variable is less than the number of rings
2797		 *   allocated, then write the previous values
2798		 *
2799		 *   if the entry was not valid previously, but the number of
2800		 *   rings is less than are allocated (this means the number of
2801		 *   rings increased from previously), then write out the
2802		 *   values in the first element
2803		 */
2804		if (i < vsi->alloc_rxq && coalesce[i].rx_valid)
2805			ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2806							    &vsi->q_vectors[i]->rx,
2807							    coalesce[i].itr_rx);
2808		else if (i < vsi->alloc_rxq)
2809			ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2810							    &vsi->q_vectors[i]->rx,
2811							    coalesce[0].itr_rx);
2812
2813		if (i < vsi->alloc_txq && coalesce[i].tx_valid)
2814			ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2815							    &vsi->q_vectors[i]->tx,
2816							    coalesce[i].itr_tx);
2817		else if (i < vsi->alloc_txq)
2818			ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2819							    &vsi->q_vectors[i]->tx,
2820							    coalesce[0].itr_tx);
2821
2822		ice_vsi_rebuild_update_coalesce_intrl(vsi->q_vectors[i],
2823						      coalesce[i].intrl);
2824	}
2825
2826	/* the number of queue vectors increased so write whatever is in
2827	 * the first element
2828	 */
2829	for (; i < vsi->num_q_vectors; i++) {
2830		ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2831						    &vsi->q_vectors[i]->tx,
2832						    coalesce[0].itr_tx);
2833		ice_vsi_rebuild_update_coalesce_itr(vsi->q_vectors[i],
2834						    &vsi->q_vectors[i]->rx,
2835						    coalesce[0].itr_rx);
2836		ice_vsi_rebuild_update_coalesce_intrl(vsi->q_vectors[i],
2837						      coalesce[0].intrl);
2838	}
2839}
2840
2841/**
2842 * ice_vsi_rebuild - Rebuild VSI after reset
2843 * @vsi: VSI to be rebuild
2844 * @init_vsi: is this an initialization or a reconfigure of the VSI
2845 *
2846 * Returns 0 on success and negative value on failure
2847 */
2848int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2849{
2850	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2851	struct ice_coalesce_stored *coalesce;
2852	int prev_num_q_vectors = 0;
2853	struct ice_vf *vf = NULL;
2854	enum ice_status status;
2855	struct ice_pf *pf;
2856	int ret, i;
2857
2858	if (!vsi)
2859		return -EINVAL;
2860
2861	pf = vsi->back;
2862	if (vsi->type == ICE_VSI_VF)
2863		vf = &pf->vf[vsi->vf_id];
2864
2865	coalesce = kcalloc(vsi->num_q_vectors,
2866			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
2867	if (!coalesce)
2868		return -ENOMEM;
2869
2870	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
2871
2872	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2873	ice_vsi_free_q_vectors(vsi);
2874
2875	/* SR-IOV determines needed MSIX resources all at once instead of per
2876	 * VSI since when VFs are spawned we know how many VFs there are and how
2877	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2878	 * cleared in the same manner.
2879	 */
2880	if (vsi->type != ICE_VSI_VF) {
2881		/* reclaim SW interrupts back to the common pool */
2882		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2883		pf->num_avail_sw_msix += vsi->num_q_vectors;
2884		vsi->base_vector = 0;
2885	}
2886
2887	if (ice_is_xdp_ena_vsi(vsi))
2888		/* return value check can be skipped here, it always returns
2889		 * 0 if reset is in progress
2890		 */
2891		ice_destroy_xdp_rings(vsi);
2892	ice_vsi_put_qs(vsi);
2893	ice_vsi_clear_rings(vsi);
2894	ice_vsi_free_arrays(vsi);
2895	if (vsi->type == ICE_VSI_VF)
2896		ice_vsi_set_num_qs(vsi, vf->vf_id);
2897	else
2898		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2899
2900	ret = ice_vsi_alloc_arrays(vsi);
2901	if (ret < 0)
2902		goto err_vsi;
2903
2904	ice_vsi_get_qs(vsi);
2905
2906	ice_alloc_fd_res(vsi);
2907	ice_vsi_set_tc_cfg(vsi);
2908
2909	/* Initialize VSI struct elements and create VSI in FW */
2910	ret = ice_vsi_init(vsi, init_vsi);
2911	if (ret < 0)
2912		goto err_vsi;
2913
2914	switch (vsi->type) {
2915	case ICE_VSI_CTRL:
2916	case ICE_VSI_PF:
2917		ret = ice_vsi_alloc_q_vectors(vsi);
2918		if (ret)
2919			goto err_rings;
2920
2921		ret = ice_vsi_setup_vector_base(vsi);
2922		if (ret)
2923			goto err_vectors;
2924
2925		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2926		if (ret)
2927			goto err_vectors;
2928
2929		ret = ice_vsi_alloc_rings(vsi);
2930		if (ret)
2931			goto err_vectors;
2932
2933		ice_vsi_map_rings_to_vectors(vsi);
2934		if (ice_is_xdp_ena_vsi(vsi)) {
2935			vsi->num_xdp_txq = vsi->alloc_rxq;
2936			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2937			if (ret)
2938				goto err_vectors;
2939		}
2940		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2941		if (vsi->type != ICE_VSI_CTRL)
2942			/* Do not exit if configuring RSS had an issue, at
2943			 * least receive traffic on first queue. Hence no
2944			 * need to capture return value
2945			 */
2946			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2947				ice_vsi_cfg_rss_lut_key(vsi);
2948		break;
2949	case ICE_VSI_VF:
2950		ret = ice_vsi_alloc_q_vectors(vsi);
2951		if (ret)
2952			goto err_rings;
2953
2954		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2955		if (ret)
2956			goto err_vectors;
2957
2958		ret = ice_vsi_alloc_rings(vsi);
2959		if (ret)
2960			goto err_vectors;
2961
2962		break;
2963	default:
2964		break;
2965	}
2966
2967	/* configure VSI nodes based on number of queues and TC's */
2968	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2969		max_txqs[i] = vsi->alloc_txq;
2970
2971		if (ice_is_xdp_ena_vsi(vsi))
2972			max_txqs[i] += vsi->num_xdp_txq;
2973	}
2974
2975	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2976				 max_txqs);
2977	if (status) {
2978		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
2979			vsi->vsi_num, ice_stat_str(status));
2980		if (init_vsi) {
2981			ret = -EIO;
2982			goto err_vectors;
2983		} else {
2984			return ice_schedule_reset(pf, ICE_RESET_PFR);
2985		}
2986	}
2987	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
2988	kfree(coalesce);
2989
2990	return 0;
2991
2992err_vectors:
2993	ice_vsi_free_q_vectors(vsi);
2994err_rings:
2995	if (vsi->netdev) {
2996		vsi->current_netdev_flags = 0;
2997		unregister_netdev(vsi->netdev);
2998		free_netdev(vsi->netdev);
2999		vsi->netdev = NULL;
3000	}
3001err_vsi:
3002	ice_vsi_clear(vsi);
3003	set_bit(__ICE_RESET_FAILED, pf->state);
3004	kfree(coalesce);
3005	return ret;
3006}
3007
3008/**
3009 * ice_is_reset_in_progress - check for a reset in progress
3010 * @state: PF state field
3011 */
3012bool ice_is_reset_in_progress(unsigned long *state)
3013{
3014	return test_bit(__ICE_RESET_OICR_RECV, state) ||
3015	       test_bit(__ICE_PFR_REQ, state) ||
3016	       test_bit(__ICE_CORER_REQ, state) ||
3017	       test_bit(__ICE_GLOBR_REQ, state);
3018}
3019
3020#ifdef CONFIG_DCB
3021/**
3022 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3023 * @vsi: VSI being configured
3024 * @ctx: the context buffer returned from AQ VSI update command
3025 */
3026static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3027{
3028	vsi->info.mapping_flags = ctx->info.mapping_flags;
3029	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3030	       sizeof(vsi->info.q_mapping));
3031	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3032	       sizeof(vsi->info.tc_mapping));
3033}
3034
3035/**
3036 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3037 * @vsi: VSI to be configured
3038 * @ena_tc: TC bitmap
3039 *
3040 * VSI queues expected to be quiesced before calling this function
3041 */
3042int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3043{
3044	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3045	struct ice_pf *pf = vsi->back;
3046	struct ice_vsi_ctx *ctx;
3047	enum ice_status status;
3048	struct device *dev;
3049	int i, ret = 0;
3050	u8 num_tc = 0;
3051
3052	dev = ice_pf_to_dev(pf);
3053
3054	ice_for_each_traffic_class(i) {
3055		/* build bitmap of enabled TCs */
3056		if (ena_tc & BIT(i))
3057			num_tc++;
3058		/* populate max_txqs per TC */
3059		max_txqs[i] = vsi->alloc_txq;
3060	}
3061
3062	vsi->tc_cfg.ena_tc = ena_tc;
3063	vsi->tc_cfg.numtc = num_tc;
3064
3065	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3066	if (!ctx)
3067		return -ENOMEM;
3068
3069	ctx->vf_num = 0;
3070	ctx->info = vsi->info;
3071
3072	ice_vsi_setup_q_map(vsi, ctx);
3073
3074	/* must to indicate which section of VSI context are being modified */
3075	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3076	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3077	if (status) {
3078		dev_info(dev, "Failed VSI Update\n");
3079		ret = -EIO;
3080		goto out;
3081	}
3082
3083	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3084				 max_txqs);
3085
3086	if (status) {
3087		dev_err(dev, "VSI %d failed TC config, error %s\n",
3088			vsi->vsi_num, ice_stat_str(status));
3089		ret = -EIO;
3090		goto out;
3091	}
3092	ice_vsi_update_q_map(vsi, ctx);
3093	vsi->info.valid_sections = 0;
3094
3095	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3096out:
3097	kfree(ctx);
3098	return ret;
3099}
3100#endif /* CONFIG_DCB */
3101
3102/**
3103 * ice_update_ring_stats - Update ring statistics
3104 * @ring: ring to update
3105 * @cont: used to increment per-vector counters
3106 * @pkts: number of processed packets
3107 * @bytes: number of processed bytes
3108 *
3109 * This function assumes that caller has acquired a u64_stats_sync lock.
3110 */
3111static void
3112ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
3113		      u64 pkts, u64 bytes)
3114{
3115	ring->stats.bytes += bytes;
3116	ring->stats.pkts += pkts;
3117	cont->total_bytes += bytes;
3118	cont->total_pkts += pkts;
3119}
3120
3121/**
3122 * ice_update_tx_ring_stats - Update Tx ring specific counters
3123 * @tx_ring: ring to update
3124 * @pkts: number of processed packets
3125 * @bytes: number of processed bytes
3126 */
3127void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3128{
3129	u64_stats_update_begin(&tx_ring->syncp);
3130	ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
3131	u64_stats_update_end(&tx_ring->syncp);
3132}
3133
3134/**
3135 * ice_update_rx_ring_stats - Update Rx ring specific counters
3136 * @rx_ring: ring to update
3137 * @pkts: number of processed packets
3138 * @bytes: number of processed bytes
3139 */
3140void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3141{
3142	u64_stats_update_begin(&rx_ring->syncp);
3143	ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
3144	u64_stats_update_end(&rx_ring->syncp);
3145}
3146
3147/**
3148 * ice_status_to_errno - convert from enum ice_status to Linux errno
3149 * @err: ice_status value to convert
3150 */
3151int ice_status_to_errno(enum ice_status err)
3152{
3153	switch (err) {
3154	case ICE_SUCCESS:
3155		return 0;
3156	case ICE_ERR_DOES_NOT_EXIST:
3157		return -ENOENT;
3158	case ICE_ERR_OUT_OF_RANGE:
3159		return -ENOTTY;
3160	case ICE_ERR_PARAM:
3161		return -EINVAL;
3162	case ICE_ERR_NO_MEMORY:
3163		return -ENOMEM;
3164	case ICE_ERR_MAX_LIMIT:
3165		return -EAGAIN;
3166	default:
3167		return -EINVAL;
3168	}
3169}
3170
3171/**
3172 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3173 * @sw: switch to check if its default forwarding VSI is free
3174 *
3175 * Return true if the default forwarding VSI is already being used, else returns
3176 * false signalling that it's available to use.
3177 */
3178bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3179{
3180	return (sw->dflt_vsi && sw->dflt_vsi_ena);
3181}
3182
3183/**
3184 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3185 * @sw: switch for the default forwarding VSI to compare against
3186 * @vsi: VSI to compare against default forwarding VSI
3187 *
3188 * If this VSI passed in is the default forwarding VSI then return true, else
3189 * return false
3190 */
3191bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3192{
3193	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3194}
3195
3196/**
3197 * ice_set_dflt_vsi - set the default forwarding VSI
3198 * @sw: switch used to assign the default forwarding VSI
3199 * @vsi: VSI getting set as the default forwarding VSI on the switch
3200 *
3201 * If the VSI passed in is already the default VSI and it's enabled just return
3202 * success.
3203 *
3204 * If there is already a default VSI on the switch and it's enabled then return
3205 * -EEXIST since there can only be one default VSI per switch.
3206 *
3207 *  Otherwise try to set the VSI passed in as the switch's default VSI and
3208 *  return the result.
3209 */
3210int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3211{
3212	enum ice_status status;
3213	struct device *dev;
3214
3215	if (!sw || !vsi)
3216		return -EINVAL;
3217
3218	dev = ice_pf_to_dev(vsi->back);
3219
3220	/* the VSI passed in is already the default VSI */
3221	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3222		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3223			vsi->vsi_num);
3224		return 0;
3225	}
3226
3227	/* another VSI is already the default VSI for this switch */
3228	if (ice_is_dflt_vsi_in_use(sw)) {
3229		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3230			sw->dflt_vsi->vsi_num);
3231		return -EEXIST;
3232	}
3233
3234	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3235	if (status) {
3236		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3237			vsi->vsi_num, ice_stat_str(status));
3238		return -EIO;
3239	}
3240
3241	sw->dflt_vsi = vsi;
3242	sw->dflt_vsi_ena = true;
3243
3244	return 0;
3245}
3246
3247/**
3248 * ice_clear_dflt_vsi - clear the default forwarding VSI
3249 * @sw: switch used to clear the default VSI
3250 *
3251 * If the switch has no default VSI or it's not enabled then return error.
3252 *
3253 * Otherwise try to clear the default VSI and return the result.
3254 */
3255int ice_clear_dflt_vsi(struct ice_sw *sw)
3256{
3257	struct ice_vsi *dflt_vsi;
3258	enum ice_status status;
3259	struct device *dev;
3260
3261	if (!sw)
3262		return -EINVAL;
3263
3264	dev = ice_pf_to_dev(sw->pf);
3265
3266	dflt_vsi = sw->dflt_vsi;
3267
3268	/* there is no default VSI configured */
3269	if (!ice_is_dflt_vsi_in_use(sw))
3270		return -ENODEV;
3271
3272	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3273				  ICE_FLTR_RX);
3274	if (status) {
3275		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3276			dflt_vsi->vsi_num, ice_stat_str(status));
3277		return -EIO;
3278	}
3279
3280	sw->dflt_vsi = NULL;
3281	sw->dflt_vsi_ena = false;
3282
3283	return 0;
3284}
3285