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