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