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