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