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