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