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