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