]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_lib.c
b5cd275606bb18b31b90ca5ff8195cc808556d51
[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_lib.h"
7 #include "ice_dcb_lib.h"
8
9 /**
10  * ice_vsi_type_str - maps VSI type enum to string equivalents
11  * @type: VSI type enum
12  */
13 const char *ice_vsi_type_str(enum ice_vsi_type type)
14 {
15         switch (type) {
16         case ICE_VSI_PF:
17                 return "ICE_VSI_PF";
18         case ICE_VSI_VF:
19                 return "ICE_VSI_VF";
20         case ICE_VSI_LB:
21                 return "ICE_VSI_LB";
22         default:
23                 return "unknown";
24         }
25 }
26
27 /**
28  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
29  * @vsi: the VSI being configured
30  * @ena: start or stop the Rx rings
31  */
32 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
33 {
34         int i, ret = 0;
35
36         for (i = 0; i < vsi->num_rxq; i++) {
37                 ret = ice_vsi_ctrl_rx_ring(vsi, ena, i);
38                 if (ret)
39                         break;
40         }
41
42         return ret;
43 }
44
45 /**
46  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
47  * @vsi: VSI pointer
48  *
49  * On error: returns error code (negative)
50  * On success: returns 0
51  */
52 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
53 {
54         struct ice_pf *pf = vsi->back;
55         struct device *dev;
56
57         dev = ice_pf_to_dev(pf);
58
59         /* allocate memory for both Tx and Rx ring pointers */
60         vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
61                                      sizeof(*vsi->tx_rings), GFP_KERNEL);
62         if (!vsi->tx_rings)
63                 return -ENOMEM;
64
65         vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
66                                      sizeof(*vsi->rx_rings), GFP_KERNEL);
67         if (!vsi->rx_rings)
68                 goto err_rings;
69
70         /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
71         vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
72                                     sizeof(*vsi->txq_map), GFP_KERNEL);
73
74         if (!vsi->txq_map)
75                 goto err_txq_map;
76
77         vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
78                                     sizeof(*vsi->rxq_map), GFP_KERNEL);
79         if (!vsi->rxq_map)
80                 goto err_rxq_map;
81
82         /* There is no need to allocate q_vectors for a loopback VSI. */
83         if (vsi->type == ICE_VSI_LB)
84                 return 0;
85
86         /* allocate memory for q_vector pointers */
87         vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
88                                       sizeof(*vsi->q_vectors), GFP_KERNEL);
89         if (!vsi->q_vectors)
90                 goto err_vectors;
91
92         return 0;
93
94 err_vectors:
95         devm_kfree(dev, vsi->rxq_map);
96 err_rxq_map:
97         devm_kfree(dev, vsi->txq_map);
98 err_txq_map:
99         devm_kfree(dev, vsi->rx_rings);
100 err_rings:
101         devm_kfree(dev, vsi->tx_rings);
102         return -ENOMEM;
103 }
104
105 /**
106  * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
107  * @vsi: the VSI being configured
108  */
109 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
110 {
111         switch (vsi->type) {
112         case ICE_VSI_PF:
113                 /* fall through */
114         case ICE_VSI_LB:
115                 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
116                 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
117                 break;
118         default:
119                 dev_dbg(&vsi->back->pdev->dev,
120                         "Not setting number of Tx/Rx descriptors for VSI type %d\n",
121                         vsi->type);
122                 break;
123         }
124 }
125
126 /**
127  * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
128  * @vsi: the VSI being configured
129  * @vf_id: ID of the VF being configured
130  *
131  * Return 0 on success and a negative value on error
132  */
133 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
134 {
135         struct ice_pf *pf = vsi->back;
136         struct ice_vf *vf = NULL;
137
138         if (vsi->type == ICE_VSI_VF)
139                 vsi->vf_id = vf_id;
140
141         switch (vsi->type) {
142         case ICE_VSI_PF:
143                 vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
144                                        num_online_cpus());
145                 if (vsi->req_txq) {
146                         vsi->alloc_txq = vsi->req_txq;
147                         vsi->num_txq = vsi->req_txq;
148                 }
149
150                 pf->num_lan_tx = vsi->alloc_txq;
151
152                 /* only 1 Rx queue unless RSS is enabled */
153                 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
154                         vsi->alloc_rxq = 1;
155                 } else {
156                         vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
157                                                num_online_cpus());
158                         if (vsi->req_rxq) {
159                                 vsi->alloc_rxq = vsi->req_rxq;
160                                 vsi->num_rxq = vsi->req_rxq;
161                         }
162                 }
163
164                 pf->num_lan_rx = vsi->alloc_rxq;
165
166                 vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
167                 break;
168         case ICE_VSI_VF:
169                 vf = &pf->vf[vsi->vf_id];
170                 vsi->alloc_txq = vf->num_vf_qs;
171                 vsi->alloc_rxq = vf->num_vf_qs;
172                 /* pf->num_vf_msix includes (VF miscellaneous vector +
173                  * data queue interrupts). Since vsi->num_q_vectors is number
174                  * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
175                  * original vector count
176                  */
177                 vsi->num_q_vectors = pf->num_vf_msix - ICE_NONQ_VECS_VF;
178                 break;
179         case ICE_VSI_LB:
180                 vsi->alloc_txq = 1;
181                 vsi->alloc_rxq = 1;
182                 break;
183         default:
184                 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
185                 break;
186         }
187
188         ice_vsi_set_num_desc(vsi);
189 }
190
191 /**
192  * ice_get_free_slot - get the next non-NULL location index in array
193  * @array: array to search
194  * @size: size of the array
195  * @curr: last known occupied index to be used as a search hint
196  *
197  * void * is being used to keep the functionality generic. This lets us use this
198  * function on any array of pointers.
199  */
200 static int ice_get_free_slot(void *array, int size, int curr)
201 {
202         int **tmp_array = (int **)array;
203         int next;
204
205         if (curr < (size - 1) && !tmp_array[curr + 1]) {
206                 next = curr + 1;
207         } else {
208                 int i = 0;
209
210                 while ((i < size) && (tmp_array[i]))
211                         i++;
212                 if (i == size)
213                         next = ICE_NO_VSI;
214                 else
215                         next = i;
216         }
217         return next;
218 }
219
220 /**
221  * ice_vsi_delete - delete a VSI from the switch
222  * @vsi: pointer to VSI being removed
223  */
224 void ice_vsi_delete(struct ice_vsi *vsi)
225 {
226         struct ice_pf *pf = vsi->back;
227         struct ice_vsi_ctx *ctxt;
228         enum ice_status status;
229
230         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
231         if (!ctxt)
232                 return;
233
234         if (vsi->type == ICE_VSI_VF)
235                 ctxt->vf_num = vsi->vf_id;
236         ctxt->vsi_num = vsi->vsi_num;
237
238         memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
239
240         status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
241         if (status)
242                 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
243                         vsi->vsi_num, status);
244
245         kfree(ctxt);
246 }
247
248 /**
249  * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
250  * @vsi: pointer to VSI being cleared
251  */
252 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
253 {
254         struct ice_pf *pf = vsi->back;
255         struct device *dev;
256
257         dev = ice_pf_to_dev(pf);
258
259         /* free the ring and vector containers */
260         if (vsi->q_vectors) {
261                 devm_kfree(dev, vsi->q_vectors);
262                 vsi->q_vectors = NULL;
263         }
264         if (vsi->tx_rings) {
265                 devm_kfree(dev, vsi->tx_rings);
266                 vsi->tx_rings = NULL;
267         }
268         if (vsi->rx_rings) {
269                 devm_kfree(dev, vsi->rx_rings);
270                 vsi->rx_rings = NULL;
271         }
272         if (vsi->txq_map) {
273                 devm_kfree(dev, vsi->txq_map);
274                 vsi->txq_map = NULL;
275         }
276         if (vsi->rxq_map) {
277                 devm_kfree(dev, vsi->rxq_map);
278                 vsi->rxq_map = NULL;
279         }
280 }
281
282 /**
283  * ice_vsi_clear - clean up and deallocate the provided VSI
284  * @vsi: pointer to VSI being cleared
285  *
286  * This deallocates the VSI's queue resources, removes it from the PF's
287  * VSI array if necessary, and deallocates the VSI
288  *
289  * Returns 0 on success, negative on failure
290  */
291 int ice_vsi_clear(struct ice_vsi *vsi)
292 {
293         struct ice_pf *pf = NULL;
294         struct device *dev;
295
296         if (!vsi)
297                 return 0;
298
299         if (!vsi->back)
300                 return -EINVAL;
301
302         pf = vsi->back;
303         dev = ice_pf_to_dev(pf);
304
305         if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
306                 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
307                 return -EINVAL;
308         }
309
310         mutex_lock(&pf->sw_mutex);
311         /* updates the PF for this cleared VSI */
312
313         pf->vsi[vsi->idx] = NULL;
314         if (vsi->idx < pf->next_vsi)
315                 pf->next_vsi = vsi->idx;
316
317         ice_vsi_free_arrays(vsi);
318         mutex_unlock(&pf->sw_mutex);
319         devm_kfree(dev, vsi);
320
321         return 0;
322 }
323
324 /**
325  * ice_msix_clean_rings - MSIX mode Interrupt Handler
326  * @irq: interrupt number
327  * @data: pointer to a q_vector
328  */
329 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
330 {
331         struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
332
333         if (!q_vector->tx.ring && !q_vector->rx.ring)
334                 return IRQ_HANDLED;
335
336         napi_schedule(&q_vector->napi);
337
338         return IRQ_HANDLED;
339 }
340
341 /**
342  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
343  * @pf: board private structure
344  * @type: type of VSI
345  * @vf_id: ID of the VF being configured
346  *
347  * returns a pointer to a VSI on success, NULL on failure.
348  */
349 static struct ice_vsi *
350 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type, u16 vf_id)
351 {
352         struct device *dev = ice_pf_to_dev(pf);
353         struct ice_vsi *vsi = NULL;
354
355         /* Need to protect the allocation of the VSIs at the PF level */
356         mutex_lock(&pf->sw_mutex);
357
358         /* If we have already allocated our maximum number of VSIs,
359          * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
360          * is available to be populated
361          */
362         if (pf->next_vsi == ICE_NO_VSI) {
363                 dev_dbg(dev, "out of VSI slots!\n");
364                 goto unlock_pf;
365         }
366
367         vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
368         if (!vsi)
369                 goto unlock_pf;
370
371         vsi->type = type;
372         vsi->back = pf;
373         set_bit(__ICE_DOWN, vsi->state);
374
375         vsi->idx = pf->next_vsi;
376
377         if (type == ICE_VSI_VF)
378                 ice_vsi_set_num_qs(vsi, vf_id);
379         else
380                 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
381
382         switch (vsi->type) {
383         case ICE_VSI_PF:
384                 if (ice_vsi_alloc_arrays(vsi))
385                         goto err_rings;
386
387                 /* Setup default MSIX irq handler for VSI */
388                 vsi->irq_handler = ice_msix_clean_rings;
389                 break;
390         case ICE_VSI_VF:
391                 if (ice_vsi_alloc_arrays(vsi))
392                         goto err_rings;
393                 break;
394         case ICE_VSI_LB:
395                 if (ice_vsi_alloc_arrays(vsi))
396                         goto err_rings;
397                 break;
398         default:
399                 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
400                 goto unlock_pf;
401         }
402
403         /* fill VSI slot in the PF struct */
404         pf->vsi[pf->next_vsi] = vsi;
405
406         /* prepare pf->next_vsi for next use */
407         pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
408                                          pf->next_vsi);
409         goto unlock_pf;
410
411 err_rings:
412         devm_kfree(dev, vsi);
413         vsi = NULL;
414 unlock_pf:
415         mutex_unlock(&pf->sw_mutex);
416         return vsi;
417 }
418
419 /**
420  * ice_vsi_get_qs - Assign queues from PF to VSI
421  * @vsi: the VSI to assign queues to
422  *
423  * Returns 0 on success and a negative value on error
424  */
425 static int ice_vsi_get_qs(struct ice_vsi *vsi)
426 {
427         struct ice_pf *pf = vsi->back;
428         struct ice_qs_cfg tx_qs_cfg = {
429                 .qs_mutex = &pf->avail_q_mutex,
430                 .pf_map = pf->avail_txqs,
431                 .pf_map_size = pf->max_pf_txqs,
432                 .q_count = vsi->alloc_txq,
433                 .scatter_count = ICE_MAX_SCATTER_TXQS,
434                 .vsi_map = vsi->txq_map,
435                 .vsi_map_offset = 0,
436                 .mapping_mode = vsi->tx_mapping_mode
437         };
438         struct ice_qs_cfg rx_qs_cfg = {
439                 .qs_mutex = &pf->avail_q_mutex,
440                 .pf_map = pf->avail_rxqs,
441                 .pf_map_size = pf->max_pf_rxqs,
442                 .q_count = vsi->alloc_rxq,
443                 .scatter_count = ICE_MAX_SCATTER_RXQS,
444                 .vsi_map = vsi->rxq_map,
445                 .vsi_map_offset = 0,
446                 .mapping_mode = vsi->rx_mapping_mode
447         };
448         int ret = 0;
449
450         vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
451         vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
452
453         ret = __ice_vsi_get_qs(&tx_qs_cfg);
454         if (!ret)
455                 ret = __ice_vsi_get_qs(&rx_qs_cfg);
456
457         return ret;
458 }
459
460 /**
461  * ice_vsi_put_qs - Release queues from VSI to PF
462  * @vsi: the VSI that is going to release queues
463  */
464 void ice_vsi_put_qs(struct ice_vsi *vsi)
465 {
466         struct ice_pf *pf = vsi->back;
467         int i;
468
469         mutex_lock(&pf->avail_q_mutex);
470
471         for (i = 0; i < vsi->alloc_txq; i++) {
472                 clear_bit(vsi->txq_map[i], pf->avail_txqs);
473                 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
474         }
475
476         for (i = 0; i < vsi->alloc_rxq; i++) {
477                 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
478                 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
479         }
480
481         mutex_unlock(&pf->avail_q_mutex);
482 }
483
484 /**
485  * ice_is_safe_mode
486  * @pf: pointer to the PF struct
487  *
488  * returns true if driver is in safe mode, false otherwise
489  */
490 bool ice_is_safe_mode(struct ice_pf *pf)
491 {
492         return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
493 }
494
495 /**
496  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
497  * @vsi: the VSI being removed
498  */
499 static void ice_rss_clean(struct ice_vsi *vsi)
500 {
501         struct ice_pf *pf = vsi->back;
502         struct device *dev;
503
504         dev = ice_pf_to_dev(pf);
505
506         if (vsi->rss_hkey_user)
507                 devm_kfree(dev, vsi->rss_hkey_user);
508         if (vsi->rss_lut_user)
509                 devm_kfree(dev, vsi->rss_lut_user);
510 }
511
512 /**
513  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
514  * @vsi: the VSI being configured
515  */
516 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
517 {
518         struct ice_hw_common_caps *cap;
519         struct ice_pf *pf = vsi->back;
520
521         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
522                 vsi->rss_size = 1;
523                 return;
524         }
525
526         cap = &pf->hw.func_caps.common_cap;
527         switch (vsi->type) {
528         case ICE_VSI_PF:
529                 /* PF VSI will inherit RSS instance of PF */
530                 vsi->rss_table_size = cap->rss_table_size;
531                 vsi->rss_size = min_t(int, num_online_cpus(),
532                                       BIT(cap->rss_table_entry_width));
533                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
534                 break;
535         case ICE_VSI_VF:
536                 /* VF VSI will gets a small RSS table
537                  * For VSI_LUT, LUT size should be set to 64 bytes
538                  */
539                 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
540                 vsi->rss_size = min_t(int, num_online_cpus(),
541                                       BIT(cap->rss_table_entry_width));
542                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
543                 break;
544         case ICE_VSI_LB:
545                 break;
546         default:
547                 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n",
548                          vsi->type);
549                 break;
550         }
551 }
552
553 /**
554  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
555  * @ctxt: the VSI context being set
556  *
557  * This initializes a default VSI context for all sections except the Queues.
558  */
559 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
560 {
561         u32 table = 0;
562
563         memset(&ctxt->info, 0, sizeof(ctxt->info));
564         /* VSI's should be allocated from shared pool */
565         ctxt->alloc_from_pool = true;
566         /* Src pruning enabled by default */
567         ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
568         /* Traffic from VSI can be sent to LAN */
569         ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
570         /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
571          * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
572          * packets untagged/tagged.
573          */
574         ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
575                                   ICE_AQ_VSI_VLAN_MODE_M) >>
576                                  ICE_AQ_VSI_VLAN_MODE_S);
577         /* Have 1:1 UP mapping for both ingress/egress tables */
578         table |= ICE_UP_TABLE_TRANSLATE(0, 0);
579         table |= ICE_UP_TABLE_TRANSLATE(1, 1);
580         table |= ICE_UP_TABLE_TRANSLATE(2, 2);
581         table |= ICE_UP_TABLE_TRANSLATE(3, 3);
582         table |= ICE_UP_TABLE_TRANSLATE(4, 4);
583         table |= ICE_UP_TABLE_TRANSLATE(5, 5);
584         table |= ICE_UP_TABLE_TRANSLATE(6, 6);
585         table |= ICE_UP_TABLE_TRANSLATE(7, 7);
586         ctxt->info.ingress_table = cpu_to_le32(table);
587         ctxt->info.egress_table = cpu_to_le32(table);
588         /* Have 1:1 UP mapping for outer to inner UP table */
589         ctxt->info.outer_up_table = cpu_to_le32(table);
590         /* No Outer tag support outer_tag_flags remains to zero */
591 }
592
593 /**
594  * ice_vsi_setup_q_map - Setup a VSI queue map
595  * @vsi: the VSI being configured
596  * @ctxt: VSI context structure
597  */
598 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
599 {
600         u16 offset = 0, qmap = 0, tx_count = 0;
601         u16 qcount_tx = vsi->alloc_txq;
602         u16 qcount_rx = vsi->alloc_rxq;
603         u16 tx_numq_tc, rx_numq_tc;
604         u16 pow = 0, max_rss = 0;
605         bool ena_tc0 = false;
606         u8 netdev_tc = 0;
607         int i;
608
609         /* at least TC0 should be enabled by default */
610         if (vsi->tc_cfg.numtc) {
611                 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
612                         ena_tc0 = true;
613         } else {
614                 ena_tc0 = true;
615         }
616
617         if (ena_tc0) {
618                 vsi->tc_cfg.numtc++;
619                 vsi->tc_cfg.ena_tc |= 1;
620         }
621
622         rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
623         if (!rx_numq_tc)
624                 rx_numq_tc = 1;
625         tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
626         if (!tx_numq_tc)
627                 tx_numq_tc = 1;
628
629         /* TC mapping is a function of the number of Rx queues assigned to the
630          * VSI for each traffic class and the offset of these queues.
631          * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
632          * queues allocated to TC0. No:of queues is a power-of-2.
633          *
634          * If TC is not enabled, the queue offset is set to 0, and allocate one
635          * queue, this way, traffic for the given TC will be sent to the default
636          * queue.
637          *
638          * Setup number and offset of Rx queues for all TCs for the VSI
639          */
640
641         qcount_rx = rx_numq_tc;
642
643         /* qcount will change if RSS is enabled */
644         if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
645                 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
646                         if (vsi->type == ICE_VSI_PF)
647                                 max_rss = ICE_MAX_LG_RSS_QS;
648                         else
649                                 max_rss = ICE_MAX_SMALL_RSS_QS;
650                         qcount_rx = min_t(int, rx_numq_tc, max_rss);
651                         if (!vsi->req_rxq)
652                                 qcount_rx = min_t(int, qcount_rx,
653                                                   vsi->rss_size);
654                 }
655         }
656
657         /* find the (rounded up) power-of-2 of qcount */
658         pow = order_base_2(qcount_rx);
659
660         ice_for_each_traffic_class(i) {
661                 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
662                         /* TC is not enabled */
663                         vsi->tc_cfg.tc_info[i].qoffset = 0;
664                         vsi->tc_cfg.tc_info[i].qcount_rx = 1;
665                         vsi->tc_cfg.tc_info[i].qcount_tx = 1;
666                         vsi->tc_cfg.tc_info[i].netdev_tc = 0;
667                         ctxt->info.tc_mapping[i] = 0;
668                         continue;
669                 }
670
671                 /* TC is enabled */
672                 vsi->tc_cfg.tc_info[i].qoffset = offset;
673                 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
674                 vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
675                 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
676
677                 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
678                         ICE_AQ_VSI_TC_Q_OFFSET_M) |
679                         ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
680                          ICE_AQ_VSI_TC_Q_NUM_M);
681                 offset += qcount_rx;
682                 tx_count += tx_numq_tc;
683                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
684         }
685
686         /* if offset is non-zero, means it is calculated correctly based on
687          * enabled TCs for a given VSI otherwise qcount_rx will always
688          * be correct and non-zero because it is based off - VSI's
689          * allocated Rx queues which is at least 1 (hence qcount_tx will be
690          * at least 1)
691          */
692         if (offset)
693                 vsi->num_rxq = offset;
694         else
695                 vsi->num_rxq = qcount_rx;
696
697         vsi->num_txq = tx_count;
698
699         if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
700                 dev_dbg(&vsi->back->pdev->dev, "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
701                 /* since there is a chance that num_rxq could have been changed
702                  * in the above for loop, make num_txq equal to num_rxq.
703                  */
704                 vsi->num_txq = vsi->num_rxq;
705         }
706
707         /* Rx queue mapping */
708         ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
709         /* q_mapping buffer holds the info for the first queue allocated for
710          * this VSI in the PF space and also the number of queues associated
711          * with this VSI.
712          */
713         ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
714         ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
715 }
716
717 /**
718  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
719  * @ctxt: the VSI context being set
720  * @vsi: the VSI being configured
721  */
722 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
723 {
724         u8 lut_type, hash_type;
725         struct device *dev;
726         struct ice_pf *pf;
727
728         pf = vsi->back;
729         dev = ice_pf_to_dev(pf);
730
731         switch (vsi->type) {
732         case ICE_VSI_PF:
733                 /* PF VSI will inherit RSS instance of PF */
734                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
735                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
736                 break;
737         case ICE_VSI_VF:
738                 /* VF VSI will gets a small RSS table which is a VSI LUT type */
739                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
740                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
741                 break;
742         case ICE_VSI_LB:
743                 dev_dbg(dev, "Unsupported VSI type %s\n",
744                         ice_vsi_type_str(vsi->type));
745                 return;
746         default:
747                 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
748                 return;
749         }
750
751         ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
752                                 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
753                                 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
754                                  ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
755 }
756
757 /**
758  * ice_vsi_init - Create and initialize a VSI
759  * @vsi: the VSI being configured
760  * @init_vsi: is this call creating a VSI
761  *
762  * This initializes a VSI context depending on the VSI type to be added and
763  * passes it down to the add_vsi aq command to create a new VSI.
764  */
765 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
766 {
767         struct ice_pf *pf = vsi->back;
768         struct ice_hw *hw = &pf->hw;
769         struct ice_vsi_ctx *ctxt;
770         struct device *dev;
771         int ret = 0;
772
773         dev = ice_pf_to_dev(pf);
774         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
775         if (!ctxt)
776                 return -ENOMEM;
777
778         ctxt->info = vsi->info;
779         switch (vsi->type) {
780         case ICE_VSI_LB:
781                 /* fall through */
782         case ICE_VSI_PF:
783                 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
784                 break;
785         case ICE_VSI_VF:
786                 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
787                 /* VF number here is the absolute VF number (0-255) */
788                 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
789                 break;
790         default:
791                 ret = -ENODEV;
792                 goto out;
793         }
794
795         ice_set_dflt_vsi_ctx(ctxt);
796         /* if the switch is in VEB mode, allow VSI loopback */
797         if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
798                 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
799
800         /* Set LUT type and HASH type if RSS is enabled */
801         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
802                 ice_set_rss_vsi_ctx(ctxt, vsi);
803                 /* if updating VSI context, make sure to set valid_section:
804                  * to indicate which section of VSI context being updated
805                  */
806                 if (!init_vsi)
807                         ctxt->info.valid_sections |=
808                                 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
809         }
810
811         ctxt->info.sw_id = vsi->port_info->sw_id;
812         ice_vsi_setup_q_map(vsi, ctxt);
813         if (!init_vsi) /* means VSI being updated */
814                 /* must to indicate which section of VSI context are
815                  * being modified
816                  */
817                 ctxt->info.valid_sections |=
818                         cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
819
820         /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
821          * respectively
822          */
823         if (vsi->type == ICE_VSI_VF) {
824                 ctxt->info.valid_sections |=
825                         cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
826                 if (pf->vf[vsi->vf_id].spoofchk) {
827                         ctxt->info.sec_flags |=
828                                 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
829                                 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
830                                  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
831                 } else {
832                         ctxt->info.sec_flags &=
833                                 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
834                                   (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
835                                    ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
836                 }
837         }
838
839         /* Allow control frames out of main VSI */
840         if (vsi->type == ICE_VSI_PF) {
841                 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
842                 ctxt->info.valid_sections |=
843                         cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
844         }
845
846         if (init_vsi) {
847                 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
848                 if (ret) {
849                         dev_err(dev, "Add VSI failed, err %d\n", ret);
850                         ret = -EIO;
851                         goto out;
852                 }
853         } else {
854                 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
855                 if (ret) {
856                         dev_err(dev, "Update VSI failed, err %d\n", ret);
857                         ret = -EIO;
858                         goto out;
859                 }
860         }
861
862         /* keep context for update VSI operations */
863         vsi->info = ctxt->info;
864
865         /* record VSI number returned */
866         vsi->vsi_num = ctxt->vsi_num;
867
868 out:
869         kfree(ctxt);
870         return ret;
871 }
872
873 /**
874  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
875  * @vsi: ptr to the VSI
876  *
877  * This should only be called after ice_vsi_alloc() which allocates the
878  * corresponding SW VSI structure and initializes num_queue_pairs for the
879  * newly allocated VSI.
880  *
881  * Returns 0 on success or negative on failure
882  */
883 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
884 {
885         struct ice_pf *pf = vsi->back;
886         struct device *dev;
887         u16 num_q_vectors;
888
889         dev = ice_pf_to_dev(pf);
890         /* SRIOV doesn't grab irq_tracker entries for each VSI */
891         if (vsi->type == ICE_VSI_VF)
892                 return 0;
893
894         if (vsi->base_vector) {
895                 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
896                         vsi->vsi_num, vsi->base_vector);
897                 return -EEXIST;
898         }
899
900         num_q_vectors = vsi->num_q_vectors;
901         /* reserve slots from OS requested IRQs */
902         vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
903                                        vsi->idx);
904         if (vsi->base_vector < 0) {
905                 dev_err(dev,
906                         "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
907                         num_q_vectors, vsi->vsi_num, vsi->base_vector);
908                 return -ENOENT;
909         }
910         pf->num_avail_sw_msix -= num_q_vectors;
911
912         return 0;
913 }
914
915 /**
916  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
917  * @vsi: the VSI having rings deallocated
918  */
919 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
920 {
921         int i;
922
923         if (vsi->tx_rings) {
924                 for (i = 0; i < vsi->alloc_txq; i++) {
925                         if (vsi->tx_rings[i]) {
926                                 kfree_rcu(vsi->tx_rings[i], rcu);
927                                 vsi->tx_rings[i] = NULL;
928                         }
929                 }
930         }
931         if (vsi->rx_rings) {
932                 for (i = 0; i < vsi->alloc_rxq; i++) {
933                         if (vsi->rx_rings[i]) {
934                                 kfree_rcu(vsi->rx_rings[i], rcu);
935                                 vsi->rx_rings[i] = NULL;
936                         }
937                 }
938         }
939 }
940
941 /**
942  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
943  * @vsi: VSI which is having rings allocated
944  */
945 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
946 {
947         struct ice_pf *pf = vsi->back;
948         struct device *dev;
949         int i;
950
951         dev = ice_pf_to_dev(pf);
952         /* Allocate Tx rings */
953         for (i = 0; i < vsi->alloc_txq; i++) {
954                 struct ice_ring *ring;
955
956                 /* allocate with kzalloc(), free with kfree_rcu() */
957                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
958
959                 if (!ring)
960                         goto err_out;
961
962                 ring->q_index = i;
963                 ring->reg_idx = vsi->txq_map[i];
964                 ring->ring_active = false;
965                 ring->vsi = vsi;
966                 ring->dev = dev;
967                 ring->count = vsi->num_tx_desc;
968                 vsi->tx_rings[i] = ring;
969         }
970
971         /* Allocate Rx rings */
972         for (i = 0; i < vsi->alloc_rxq; i++) {
973                 struct ice_ring *ring;
974
975                 /* allocate with kzalloc(), free with kfree_rcu() */
976                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
977                 if (!ring)
978                         goto err_out;
979
980                 ring->q_index = i;
981                 ring->reg_idx = vsi->rxq_map[i];
982                 ring->ring_active = false;
983                 ring->vsi = vsi;
984                 ring->netdev = vsi->netdev;
985                 ring->dev = dev;
986                 ring->count = vsi->num_rx_desc;
987                 vsi->rx_rings[i] = ring;
988         }
989
990         return 0;
991
992 err_out:
993         ice_vsi_clear_rings(vsi);
994         return -ENOMEM;
995 }
996
997 /**
998  * ice_vsi_manage_rss_lut - disable/enable RSS
999  * @vsi: the VSI being changed
1000  * @ena: boolean value indicating if this is an enable or disable request
1001  *
1002  * In the event of disable request for RSS, this function will zero out RSS
1003  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1004  * LUT.
1005  */
1006 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1007 {
1008         int err = 0;
1009         u8 *lut;
1010
1011         lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1012         if (!lut)
1013                 return -ENOMEM;
1014
1015         if (ena) {
1016                 if (vsi->rss_lut_user)
1017                         memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1018                 else
1019                         ice_fill_rss_lut(lut, vsi->rss_table_size,
1020                                          vsi->rss_size);
1021         }
1022
1023         err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1024         kfree(lut);
1025         return err;
1026 }
1027
1028 /**
1029  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1030  * @vsi: VSI to be configured
1031  */
1032 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1033 {
1034         struct ice_aqc_get_set_rss_keys *key;
1035         struct ice_pf *pf = vsi->back;
1036         enum ice_status status;
1037         struct device *dev;
1038         int err = 0;
1039         u8 *lut;
1040
1041         dev = ice_pf_to_dev(pf);
1042         vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1043
1044         lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1045         if (!lut)
1046                 return -ENOMEM;
1047
1048         if (vsi->rss_lut_user)
1049                 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1050         else
1051                 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1052
1053         status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1054                                     vsi->rss_table_size);
1055
1056         if (status) {
1057                 dev_err(dev, "set_rss_lut failed, error %d\n", status);
1058                 err = -EIO;
1059                 goto ice_vsi_cfg_rss_exit;
1060         }
1061
1062         key = kzalloc(sizeof(*key), GFP_KERNEL);
1063         if (!key) {
1064                 err = -ENOMEM;
1065                 goto ice_vsi_cfg_rss_exit;
1066         }
1067
1068         if (vsi->rss_hkey_user)
1069                 memcpy(key,
1070                        (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1071                        ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1072         else
1073                 netdev_rss_key_fill((void *)key,
1074                                     ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1075
1076         status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1077
1078         if (status) {
1079                 dev_err(dev, "set_rss_key failed, error %d\n", status);
1080                 err = -EIO;
1081         }
1082
1083         kfree(key);
1084 ice_vsi_cfg_rss_exit:
1085         kfree(lut);
1086         return err;
1087 }
1088
1089 /**
1090  * ice_add_mac_to_list - Add a MAC address filter entry to the list
1091  * @vsi: the VSI to be forwarded to
1092  * @add_list: pointer to the list which contains MAC filter entries
1093  * @macaddr: the MAC address to be added.
1094  *
1095  * Adds MAC address filter entry to the temp list
1096  *
1097  * Returns 0 on success or ENOMEM on failure.
1098  */
1099 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1100                         const u8 *macaddr)
1101 {
1102         struct ice_fltr_list_entry *tmp;
1103         struct ice_pf *pf = vsi->back;
1104
1105         tmp = devm_kzalloc(ice_pf_to_dev(pf), sizeof(*tmp), GFP_ATOMIC);
1106         if (!tmp)
1107                 return -ENOMEM;
1108
1109         tmp->fltr_info.flag = ICE_FLTR_TX;
1110         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1111         tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1112         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1113         tmp->fltr_info.vsi_handle = vsi->idx;
1114         ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1115
1116         INIT_LIST_HEAD(&tmp->list_entry);
1117         list_add(&tmp->list_entry, add_list);
1118
1119         return 0;
1120 }
1121
1122 /**
1123  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1124  * @vsi: the VSI to be updated
1125  */
1126 void ice_update_eth_stats(struct ice_vsi *vsi)
1127 {
1128         struct ice_eth_stats *prev_es, *cur_es;
1129         struct ice_hw *hw = &vsi->back->hw;
1130         u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1131
1132         prev_es = &vsi->eth_stats_prev;
1133         cur_es = &vsi->eth_stats;
1134
1135         ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1136                           &prev_es->rx_bytes, &cur_es->rx_bytes);
1137
1138         ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1139                           &prev_es->rx_unicast, &cur_es->rx_unicast);
1140
1141         ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1142                           &prev_es->rx_multicast, &cur_es->rx_multicast);
1143
1144         ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1145                           &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1146
1147         ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1148                           &prev_es->rx_discards, &cur_es->rx_discards);
1149
1150         ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1151                           &prev_es->tx_bytes, &cur_es->tx_bytes);
1152
1153         ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1154                           &prev_es->tx_unicast, &cur_es->tx_unicast);
1155
1156         ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1157                           &prev_es->tx_multicast, &cur_es->tx_multicast);
1158
1159         ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1160                           &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1161
1162         ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1163                           &prev_es->tx_errors, &cur_es->tx_errors);
1164
1165         vsi->stat_offsets_loaded = true;
1166 }
1167
1168 /**
1169  * ice_free_fltr_list - free filter lists helper
1170  * @dev: pointer to the device struct
1171  * @h: pointer to the list head to be freed
1172  *
1173  * Helper function to free filter lists previously created using
1174  * ice_add_mac_to_list
1175  */
1176 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1177 {
1178         struct ice_fltr_list_entry *e, *tmp;
1179
1180         list_for_each_entry_safe(e, tmp, h, list_entry) {
1181                 list_del(&e->list_entry);
1182                 devm_kfree(dev, e);
1183         }
1184 }
1185
1186 /**
1187  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1188  * @vsi: the VSI being configured
1189  * @vid: VLAN ID to be added
1190  */
1191 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1192 {
1193         struct ice_fltr_list_entry *tmp;
1194         struct ice_pf *pf = vsi->back;
1195         LIST_HEAD(tmp_add_list);
1196         enum ice_status status;
1197         struct device *dev;
1198         int err = 0;
1199
1200         dev = ice_pf_to_dev(pf);
1201         tmp = devm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
1202         if (!tmp)
1203                 return -ENOMEM;
1204
1205         tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1206         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1207         tmp->fltr_info.flag = ICE_FLTR_TX;
1208         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1209         tmp->fltr_info.vsi_handle = vsi->idx;
1210         tmp->fltr_info.l_data.vlan.vlan_id = vid;
1211
1212         INIT_LIST_HEAD(&tmp->list_entry);
1213         list_add(&tmp->list_entry, &tmp_add_list);
1214
1215         status = ice_add_vlan(&pf->hw, &tmp_add_list);
1216         if (status) {
1217                 err = -ENODEV;
1218                 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1219                         vsi->vsi_num);
1220         }
1221
1222         ice_free_fltr_list(dev, &tmp_add_list);
1223         return err;
1224 }
1225
1226 /**
1227  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1228  * @vsi: the VSI being configured
1229  * @vid: VLAN ID to be removed
1230  *
1231  * Returns 0 on success and negative on failure
1232  */
1233 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1234 {
1235         struct ice_fltr_list_entry *list;
1236         struct ice_pf *pf = vsi->back;
1237         LIST_HEAD(tmp_add_list);
1238         enum ice_status status;
1239         struct device *dev;
1240         int err = 0;
1241
1242         dev = ice_pf_to_dev(pf);
1243         list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1244         if (!list)
1245                 return -ENOMEM;
1246
1247         list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1248         list->fltr_info.vsi_handle = vsi->idx;
1249         list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1250         list->fltr_info.l_data.vlan.vlan_id = vid;
1251         list->fltr_info.flag = ICE_FLTR_TX;
1252         list->fltr_info.src_id = ICE_SRC_ID_VSI;
1253
1254         INIT_LIST_HEAD(&list->list_entry);
1255         list_add(&list->list_entry, &tmp_add_list);
1256
1257         status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1258         if (status == ICE_ERR_DOES_NOT_EXIST) {
1259                 dev_dbg(dev,
1260                         "Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1261                         vid, vsi->vsi_num, status);
1262         } else if (status) {
1263                 dev_err(dev,
1264                         "Error removing VLAN %d on vsi %i error: %d\n",
1265                         vid, vsi->vsi_num, status);
1266                 err = -EIO;
1267         }
1268
1269         ice_free_fltr_list(dev, &tmp_add_list);
1270         return err;
1271 }
1272
1273 /**
1274  * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1275  * @vsi: VSI
1276  */
1277 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1278 {
1279         if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1280                 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1281                 vsi->rx_buf_len = ICE_RXBUF_2048;
1282 #if (PAGE_SIZE < 8192)
1283         } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1284                    (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1285                 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1286                 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1287 #endif
1288         } else {
1289                 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1290 #if (PAGE_SIZE < 8192)
1291                 vsi->rx_buf_len = ICE_RXBUF_3072;
1292 #else
1293                 vsi->rx_buf_len = ICE_RXBUF_2048;
1294 #endif
1295         }
1296 }
1297
1298 /**
1299  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1300  * @vsi: the VSI being configured
1301  *
1302  * Return 0 on success and a negative value on error
1303  * Configure the Rx VSI for operation.
1304  */
1305 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1306 {
1307         u16 i;
1308
1309         if (vsi->type == ICE_VSI_VF)
1310                 goto setup_rings;
1311
1312         ice_vsi_cfg_frame_size(vsi);
1313 setup_rings:
1314         /* set up individual rings */
1315         for (i = 0; i < vsi->num_rxq; i++) {
1316                 int err;
1317
1318                 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1319                 if (err) {
1320                         dev_err(&vsi->back->pdev->dev,
1321                                 "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1322                                 i, err);
1323                         return err;
1324                 }
1325         }
1326
1327         return 0;
1328 }
1329
1330 /**
1331  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1332  * @vsi: the VSI being configured
1333  * @rings: Tx ring array to be configured
1334  *
1335  * Return 0 on success and a negative value on error
1336  * Configure the Tx VSI for operation.
1337  */
1338 static int
1339 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1340 {
1341         struct ice_aqc_add_tx_qgrp *qg_buf;
1342         u16 q_idx = 0;
1343         int err = 0;
1344
1345         qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1346         if (!qg_buf)
1347                 return -ENOMEM;
1348
1349         qg_buf->num_txqs = 1;
1350
1351         for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1352                 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1353                 if (err)
1354                         goto err_cfg_txqs;
1355         }
1356
1357 err_cfg_txqs:
1358         kfree(qg_buf);
1359         return err;
1360 }
1361
1362 /**
1363  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1364  * @vsi: the VSI being configured
1365  *
1366  * Return 0 on success and a negative value on error
1367  * Configure the Tx VSI for operation.
1368  */
1369 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1370 {
1371         return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1372 }
1373
1374 /**
1375  * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1376  * @vsi: the VSI being configured
1377  *
1378  * Return 0 on success and a negative value on error
1379  * Configure the Tx queues dedicated for XDP in given VSI for operation.
1380  */
1381 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1382 {
1383         int ret;
1384         int i;
1385
1386         ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1387         if (ret)
1388                 return ret;
1389
1390         for (i = 0; i < vsi->num_xdp_txq; i++)
1391                 vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1392
1393         return ret;
1394 }
1395
1396 /**
1397  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1398  * @intrl: interrupt rate limit in usecs
1399  * @gran: interrupt rate limit granularity in usecs
1400  *
1401  * This function converts a decimal interrupt rate limit in usecs to the format
1402  * expected by firmware.
1403  */
1404 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1405 {
1406         u32 val = intrl / gran;
1407
1408         if (val)
1409                 return val | GLINT_RATE_INTRL_ENA_M;
1410         return 0;
1411 }
1412
1413 /**
1414  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1415  * @vsi: the VSI being configured
1416  *
1417  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1418  * for the VF VSI.
1419  */
1420 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1421 {
1422         struct ice_pf *pf = vsi->back;
1423         struct ice_hw *hw = &pf->hw;
1424         u32 txq = 0, rxq = 0;
1425         int i, q;
1426
1427         for (i = 0; i < vsi->num_q_vectors; i++) {
1428                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1429                 u16 reg_idx = q_vector->reg_idx;
1430
1431                 ice_cfg_itr(hw, q_vector);
1432
1433                 wr32(hw, GLINT_RATE(reg_idx),
1434                      ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1435
1436                 /* Both Transmit Queue Interrupt Cause Control register
1437                  * and Receive Queue Interrupt Cause control register
1438                  * expects MSIX_INDX field to be the vector index
1439                  * within the function space and not the absolute
1440                  * vector index across PF or across device.
1441                  * For SR-IOV VF VSIs queue vector index always starts
1442                  * with 1 since first vector index(0) is used for OICR
1443                  * in VF space. Since VMDq and other PF VSIs are within
1444                  * the PF function space, use the vector index that is
1445                  * tracked for this PF.
1446                  */
1447                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1448                         ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1449                                               q_vector->tx.itr_idx);
1450                         txq++;
1451                 }
1452
1453                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1454                         ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1455                                               q_vector->rx.itr_idx);
1456                         rxq++;
1457                 }
1458         }
1459 }
1460
1461 /**
1462  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1463  * @vsi: the VSI being changed
1464  */
1465 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1466 {
1467         struct ice_hw *hw = &vsi->back->hw;
1468         struct ice_vsi_ctx *ctxt;
1469         enum ice_status status;
1470         int ret = 0;
1471
1472         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1473         if (!ctxt)
1474                 return -ENOMEM;
1475
1476         /* Here we are configuring the VSI to let the driver add VLAN tags by
1477          * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1478          * insertion happens in the Tx hot path, in ice_tx_map.
1479          */
1480         ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1481
1482         /* Preserve existing VLAN strip setting */
1483         ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1484                                   ICE_AQ_VSI_VLAN_EMOD_M);
1485
1486         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1487
1488         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1489         if (status) {
1490                 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1491                         status, hw->adminq.sq_last_status);
1492                 ret = -EIO;
1493                 goto out;
1494         }
1495
1496         vsi->info.vlan_flags = ctxt->info.vlan_flags;
1497 out:
1498         kfree(ctxt);
1499         return ret;
1500 }
1501
1502 /**
1503  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1504  * @vsi: the VSI being changed
1505  * @ena: boolean value indicating if this is a enable or disable request
1506  */
1507 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1508 {
1509         struct ice_hw *hw = &vsi->back->hw;
1510         struct ice_vsi_ctx *ctxt;
1511         enum ice_status status;
1512         int ret = 0;
1513
1514         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1515         if (!ctxt)
1516                 return -ENOMEM;
1517
1518         /* Here we are configuring what the VSI should do with the VLAN tag in
1519          * the Rx packet. We can either leave the tag in the packet or put it in
1520          * the Rx descriptor.
1521          */
1522         if (ena)
1523                 /* Strip VLAN tag from Rx packet and put it in the desc */
1524                 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1525         else
1526                 /* Disable stripping. Leave tag in packet */
1527                 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1528
1529         /* Allow all packets untagged/tagged */
1530         ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1531
1532         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1533
1534         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1535         if (status) {
1536                 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1537                         ena, status, hw->adminq.sq_last_status);
1538                 ret = -EIO;
1539                 goto out;
1540         }
1541
1542         vsi->info.vlan_flags = ctxt->info.vlan_flags;
1543 out:
1544         kfree(ctxt);
1545         return ret;
1546 }
1547
1548 /**
1549  * ice_vsi_start_rx_rings - start VSI's Rx rings
1550  * @vsi: the VSI whose rings are to be started
1551  *
1552  * Returns 0 on success and a negative value on error
1553  */
1554 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1555 {
1556         return ice_vsi_ctrl_rx_rings(vsi, true);
1557 }
1558
1559 /**
1560  * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1561  * @vsi: the VSI
1562  *
1563  * Returns 0 on success and a negative value on error
1564  */
1565 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1566 {
1567         return ice_vsi_ctrl_rx_rings(vsi, false);
1568 }
1569
1570 /**
1571  * ice_vsi_stop_tx_rings - Disable Tx rings
1572  * @vsi: the VSI being configured
1573  * @rst_src: reset source
1574  * @rel_vmvf_num: Relative ID of VF/VM
1575  * @rings: Tx ring array to be stopped
1576  */
1577 static int
1578 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1579                       u16 rel_vmvf_num, struct ice_ring **rings)
1580 {
1581         u16 q_idx;
1582
1583         if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1584                 return -EINVAL;
1585
1586         for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1587                 struct ice_txq_meta txq_meta = { };
1588                 int status;
1589
1590                 if (!rings || !rings[q_idx])
1591                         return -EINVAL;
1592
1593                 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1594                 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1595                                               rings[q_idx], &txq_meta);
1596
1597                 if (status)
1598                         return status;
1599         }
1600
1601         return 0;
1602 }
1603
1604 /**
1605  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1606  * @vsi: the VSI being configured
1607  * @rst_src: reset source
1608  * @rel_vmvf_num: Relative ID of VF/VM
1609  */
1610 int
1611 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1612                           u16 rel_vmvf_num)
1613 {
1614         return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1615 }
1616
1617 /**
1618  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1619  * @vsi: the VSI being configured
1620  */
1621 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1622 {
1623         return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1624 }
1625
1626 /**
1627  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1628  * @vsi: VSI to enable or disable VLAN pruning on
1629  * @ena: set to true to enable VLAN pruning and false to disable it
1630  * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1631  *
1632  * returns 0 if VSI is updated, negative otherwise
1633  */
1634 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1635 {
1636         struct ice_vsi_ctx *ctxt;
1637         struct ice_pf *pf;
1638         int status;
1639
1640         if (!vsi)
1641                 return -EINVAL;
1642
1643         pf = vsi->back;
1644         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1645         if (!ctxt)
1646                 return -ENOMEM;
1647
1648         ctxt->info = vsi->info;
1649
1650         if (ena)
1651                 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1652         else
1653                 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1654
1655         if (!vlan_promisc)
1656                 ctxt->info.valid_sections =
1657                         cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
1658
1659         status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1660         if (status) {
1661                 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1662                            ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
1663                            pf->hw.adminq.sq_last_status);
1664                 goto err_out;
1665         }
1666
1667         vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1668
1669         kfree(ctxt);
1670         return 0;
1671
1672 err_out:
1673         kfree(ctxt);
1674         return -EIO;
1675 }
1676
1677 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
1678 {
1679         struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
1680
1681         vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
1682         vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
1683 }
1684
1685 /**
1686  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
1687  * @vsi: VSI to set the q_vectors register index on
1688  */
1689 static int
1690 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
1691 {
1692         u16 i;
1693
1694         if (!vsi || !vsi->q_vectors)
1695                 return -EINVAL;
1696
1697         ice_for_each_q_vector(vsi, i) {
1698                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1699
1700                 if (!q_vector) {
1701                         dev_err(&vsi->back->pdev->dev,
1702                                 "Failed to set reg_idx on q_vector %d VSI %d\n",
1703                                 i, vsi->vsi_num);
1704                         goto clear_reg_idx;
1705                 }
1706
1707                 if (vsi->type == ICE_VSI_VF) {
1708                         struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
1709
1710                         q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
1711                 } else {
1712                         q_vector->reg_idx =
1713                                 q_vector->v_idx + vsi->base_vector;
1714                 }
1715         }
1716
1717         return 0;
1718
1719 clear_reg_idx:
1720         ice_for_each_q_vector(vsi, i) {
1721                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1722
1723                 if (q_vector)
1724                         q_vector->reg_idx = 0;
1725         }
1726
1727         return -EINVAL;
1728 }
1729
1730 /**
1731  * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
1732  * @vsi: the VSI being configured
1733  * @add_rule: boolean value to add or remove ethertype filter rule
1734  */
1735 static void
1736 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
1737 {
1738         struct ice_fltr_list_entry *list;
1739         struct ice_pf *pf = vsi->back;
1740         LIST_HEAD(tmp_add_list);
1741         enum ice_status status;
1742         struct device *dev;
1743
1744         dev = ice_pf_to_dev(pf);
1745         list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1746         if (!list)
1747                 return;
1748
1749         list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1750         list->fltr_info.fltr_act = ICE_DROP_PACKET;
1751         list->fltr_info.flag = ICE_FLTR_TX;
1752         list->fltr_info.src_id = ICE_SRC_ID_VSI;
1753         list->fltr_info.vsi_handle = vsi->idx;
1754         list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
1755
1756         INIT_LIST_HEAD(&list->list_entry);
1757         list_add(&list->list_entry, &tmp_add_list);
1758
1759         if (add_rule)
1760                 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1761         else
1762                 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1763
1764         if (status)
1765                 dev_err(dev,
1766                         "Failure Adding or Removing Ethertype on VSI %i error: %d\n",
1767                         vsi->vsi_num, status);
1768
1769         ice_free_fltr_list(dev, &tmp_add_list);
1770 }
1771
1772 /**
1773  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
1774  * @vsi: the VSI being configured
1775  * @tx: bool to determine Tx or Rx rule
1776  * @create: bool to determine create or remove Rule
1777  */
1778 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
1779 {
1780         struct ice_fltr_list_entry *list;
1781         struct ice_pf *pf = vsi->back;
1782         LIST_HEAD(tmp_add_list);
1783         enum ice_status status;
1784         struct device *dev;
1785
1786         dev = ice_pf_to_dev(pf);
1787         list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1788         if (!list)
1789                 return;
1790
1791         list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1792         list->fltr_info.vsi_handle = vsi->idx;
1793         list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
1794
1795         if (tx) {
1796                 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1797                 list->fltr_info.flag = ICE_FLTR_TX;
1798                 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1799         } else {
1800                 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1801                 list->fltr_info.flag = ICE_FLTR_RX;
1802                 list->fltr_info.src_id = ICE_SRC_ID_LPORT;
1803         }
1804
1805         INIT_LIST_HEAD(&list->list_entry);
1806         list_add(&list->list_entry, &tmp_add_list);
1807
1808         if (create)
1809                 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1810         else
1811                 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1812
1813         if (status)
1814                 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
1815                         create ? "adding" : "removing", tx ? "TX" : "RX",
1816                         vsi->vsi_num, status);
1817
1818         ice_free_fltr_list(dev, &tmp_add_list);
1819 }
1820
1821 /**
1822  * ice_vsi_setup - Set up a VSI by a given type
1823  * @pf: board private structure
1824  * @pi: pointer to the port_info instance
1825  * @type: VSI type
1826  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1827  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
1828  *         fill-in ICE_INVAL_VFID as input.
1829  *
1830  * This allocates the sw VSI structure and its queue resources.
1831  *
1832  * Returns pointer to the successfully allocated and configured VSI sw struct on
1833  * success, NULL on failure.
1834  */
1835 struct ice_vsi *
1836 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1837               enum ice_vsi_type type, u16 vf_id)
1838 {
1839         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1840         struct device *dev = ice_pf_to_dev(pf);
1841         enum ice_status status;
1842         struct ice_vsi *vsi;
1843         int ret, i;
1844
1845         if (type == ICE_VSI_VF)
1846                 vsi = ice_vsi_alloc(pf, type, vf_id);
1847         else
1848                 vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
1849
1850         if (!vsi) {
1851                 dev_err(dev, "could not allocate VSI\n");
1852                 return NULL;
1853         }
1854
1855         vsi->port_info = pi;
1856         vsi->vsw = pf->first_sw;
1857         if (vsi->type == ICE_VSI_PF)
1858                 vsi->ethtype = ETH_P_PAUSE;
1859
1860         if (vsi->type == ICE_VSI_VF)
1861                 vsi->vf_id = vf_id;
1862
1863         if (ice_vsi_get_qs(vsi)) {
1864                 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1865                         vsi->idx);
1866                 goto unroll_get_qs;
1867         }
1868
1869         /* set RSS capabilities */
1870         ice_vsi_set_rss_params(vsi);
1871
1872         /* set TC configuration */
1873         ice_vsi_set_tc_cfg(vsi);
1874
1875         /* create the VSI */
1876         ret = ice_vsi_init(vsi, true);
1877         if (ret)
1878                 goto unroll_get_qs;
1879
1880         switch (vsi->type) {
1881         case ICE_VSI_PF:
1882                 ret = ice_vsi_alloc_q_vectors(vsi);
1883                 if (ret)
1884                         goto unroll_vsi_init;
1885
1886                 ret = ice_vsi_setup_vector_base(vsi);
1887                 if (ret)
1888                         goto unroll_alloc_q_vector;
1889
1890                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1891                 if (ret)
1892                         goto unroll_vector_base;
1893
1894                 ret = ice_vsi_alloc_rings(vsi);
1895                 if (ret)
1896                         goto unroll_vector_base;
1897
1898                 ice_vsi_map_rings_to_vectors(vsi);
1899
1900                 /* Do not exit if configuring RSS had an issue, at least
1901                  * receive traffic on first queue. Hence no need to capture
1902                  * return value
1903                  */
1904                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1905                         ice_vsi_cfg_rss_lut_key(vsi);
1906                 break;
1907         case ICE_VSI_VF:
1908                 /* VF driver will take care of creating netdev for this type and
1909                  * map queues to vectors through Virtchnl, PF driver only
1910                  * creates a VSI and corresponding structures for bookkeeping
1911                  * purpose
1912                  */
1913                 ret = ice_vsi_alloc_q_vectors(vsi);
1914                 if (ret)
1915                         goto unroll_vsi_init;
1916
1917                 ret = ice_vsi_alloc_rings(vsi);
1918                 if (ret)
1919                         goto unroll_alloc_q_vector;
1920
1921                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1922                 if (ret)
1923                         goto unroll_vector_base;
1924
1925                 /* Do not exit if configuring RSS had an issue, at least
1926                  * receive traffic on first queue. Hence no need to capture
1927                  * return value
1928                  */
1929                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1930                         ice_vsi_cfg_rss_lut_key(vsi);
1931                 break;
1932         case ICE_VSI_LB:
1933                 ret = ice_vsi_alloc_rings(vsi);
1934                 if (ret)
1935                         goto unroll_vsi_init;
1936                 break;
1937         default:
1938                 /* clean up the resources and exit */
1939                 goto unroll_vsi_init;
1940         }
1941
1942         /* configure VSI nodes based on number of queues and TC's */
1943         for (i = 0; i < vsi->tc_cfg.numtc; i++)
1944                 max_txqs[i] = vsi->alloc_txq;
1945
1946         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1947                                  max_txqs);
1948         if (status) {
1949                 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
1950                         vsi->vsi_num, status);
1951                 goto unroll_vector_base;
1952         }
1953
1954         /* Add switch rule to drop all Tx Flow Control Frames, of look up
1955          * type ETHERTYPE from VSIs, and restrict malicious VF from sending
1956          * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
1957          * The rule is added once for PF VSI in order to create appropriate
1958          * recipe, since VSI/VSI list is ignored with drop action...
1959          * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
1960          * be dropped so that VFs cannot send LLDP packets to reconfig DCB
1961          * settings in the HW.
1962          */
1963         if (!ice_is_safe_mode(pf))
1964                 if (vsi->type == ICE_VSI_PF) {
1965                         ice_vsi_add_rem_eth_mac(vsi, true);
1966
1967                         /* Tx LLDP packets */
1968                         ice_cfg_sw_lldp(vsi, true, true);
1969                 }
1970
1971         return vsi;
1972
1973 unroll_vector_base:
1974         /* reclaim SW interrupts back to the common pool */
1975         ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
1976         pf->num_avail_sw_msix += vsi->num_q_vectors;
1977 unroll_alloc_q_vector:
1978         ice_vsi_free_q_vectors(vsi);
1979 unroll_vsi_init:
1980         ice_vsi_delete(vsi);
1981 unroll_get_qs:
1982         ice_vsi_put_qs(vsi);
1983         ice_vsi_clear(vsi);
1984
1985         return NULL;
1986 }
1987
1988 /**
1989  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1990  * @vsi: the VSI being cleaned up
1991  */
1992 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1993 {
1994         struct ice_pf *pf = vsi->back;
1995         struct ice_hw *hw = &pf->hw;
1996         u32 txq = 0;
1997         u32 rxq = 0;
1998         int i, q;
1999
2000         for (i = 0; i < vsi->num_q_vectors; i++) {
2001                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2002                 u16 reg_idx = q_vector->reg_idx;
2003
2004                 wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2005                 wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2006                 for (q = 0; q < q_vector->num_ring_tx; q++) {
2007                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2008                         if (ice_is_xdp_ena_vsi(vsi)) {
2009                                 u32 xdp_txq = txq + vsi->num_xdp_txq;
2010
2011                                 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2012                         }
2013                         txq++;
2014                 }
2015
2016                 for (q = 0; q < q_vector->num_ring_rx; q++) {
2017                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2018                         rxq++;
2019                 }
2020         }
2021
2022         ice_flush(hw);
2023 }
2024
2025 /**
2026  * ice_vsi_free_irq - Free the IRQ association with the OS
2027  * @vsi: the VSI being configured
2028  */
2029 void ice_vsi_free_irq(struct ice_vsi *vsi)
2030 {
2031         struct ice_pf *pf = vsi->back;
2032         int base = vsi->base_vector;
2033         int i;
2034
2035         if (!vsi->q_vectors || !vsi->irqs_ready)
2036                 return;
2037
2038         ice_vsi_release_msix(vsi);
2039         if (vsi->type == ICE_VSI_VF)
2040                 return;
2041
2042         vsi->irqs_ready = false;
2043         ice_for_each_q_vector(vsi, i) {
2044                 u16 vector = i + base;
2045                 int irq_num;
2046
2047                 irq_num = pf->msix_entries[vector].vector;
2048
2049                 /* free only the irqs that were actually requested */
2050                 if (!vsi->q_vectors[i] ||
2051                     !(vsi->q_vectors[i]->num_ring_tx ||
2052                       vsi->q_vectors[i]->num_ring_rx))
2053                         continue;
2054
2055                 /* clear the affinity notifier in the IRQ descriptor */
2056                 irq_set_affinity_notifier(irq_num, NULL);
2057
2058                 /* clear the affinity_mask in the IRQ descriptor */
2059                 irq_set_affinity_hint(irq_num, NULL);
2060                 synchronize_irq(irq_num);
2061                 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2062         }
2063 }
2064
2065 /**
2066  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2067  * @vsi: the VSI having resources freed
2068  */
2069 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2070 {
2071         int i;
2072
2073         if (!vsi->tx_rings)
2074                 return;
2075
2076         ice_for_each_txq(vsi, i)
2077                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2078                         ice_free_tx_ring(vsi->tx_rings[i]);
2079 }
2080
2081 /**
2082  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2083  * @vsi: the VSI having resources freed
2084  */
2085 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2086 {
2087         int i;
2088
2089         if (!vsi->rx_rings)
2090                 return;
2091
2092         ice_for_each_rxq(vsi, i)
2093                 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2094                         ice_free_rx_ring(vsi->rx_rings[i]);
2095 }
2096
2097 /**
2098  * ice_vsi_close - Shut down a VSI
2099  * @vsi: the VSI being shut down
2100  */
2101 void ice_vsi_close(struct ice_vsi *vsi)
2102 {
2103         if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2104                 ice_down(vsi);
2105
2106         ice_vsi_free_irq(vsi);
2107         ice_vsi_free_tx_rings(vsi);
2108         ice_vsi_free_rx_rings(vsi);
2109 }
2110
2111 /**
2112  * ice_ena_vsi - resume a VSI
2113  * @vsi: the VSI being resume
2114  * @locked: is the rtnl_lock already held
2115  */
2116 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2117 {
2118         int err = 0;
2119
2120         if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2121                 return 0;
2122
2123         clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2124
2125         if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2126                 if (netif_running(vsi->netdev)) {
2127                         if (!locked)
2128                                 rtnl_lock();
2129
2130                         err = ice_open(vsi->netdev);
2131
2132                         if (!locked)
2133                                 rtnl_unlock();
2134                 }
2135         }
2136
2137         return err;
2138 }
2139
2140 /**
2141  * ice_dis_vsi - pause a VSI
2142  * @vsi: the VSI being paused
2143  * @locked: is the rtnl_lock already held
2144  */
2145 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2146 {
2147         if (test_bit(__ICE_DOWN, vsi->state))
2148                 return;
2149
2150         set_bit(__ICE_NEEDS_RESTART, vsi->state);
2151
2152         if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2153                 if (netif_running(vsi->netdev)) {
2154                         if (!locked)
2155                                 rtnl_lock();
2156
2157                         ice_stop(vsi->netdev);
2158
2159                         if (!locked)
2160                                 rtnl_unlock();
2161                 } else {
2162                         ice_vsi_close(vsi);
2163                 }
2164         }
2165 }
2166
2167 /**
2168  * ice_free_res - free a block of resources
2169  * @res: pointer to the resource
2170  * @index: starting index previously returned by ice_get_res
2171  * @id: identifier to track owner
2172  *
2173  * Returns number of resources freed
2174  */
2175 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2176 {
2177         int count = 0;
2178         int i;
2179
2180         if (!res || index >= res->end)
2181                 return -EINVAL;
2182
2183         id |= ICE_RES_VALID_BIT;
2184         for (i = index; i < res->end && res->list[i] == id; i++) {
2185                 res->list[i] = 0;
2186                 count++;
2187         }
2188
2189         return count;
2190 }
2191
2192 /**
2193  * ice_search_res - Search the tracker for a block of resources
2194  * @res: pointer to the resource
2195  * @needed: size of the block needed
2196  * @id: identifier to track owner
2197  *
2198  * Returns the base item index of the block, or -ENOMEM for error
2199  */
2200 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2201 {
2202         int start = 0, end = 0;
2203
2204         if (needed > res->end)
2205                 return -ENOMEM;
2206
2207         id |= ICE_RES_VALID_BIT;
2208
2209         do {
2210                 /* skip already allocated entries */
2211                 if (res->list[end++] & ICE_RES_VALID_BIT) {
2212                         start = end;
2213                         if ((start + needed) > res->end)
2214                                 break;
2215                 }
2216
2217                 if (end == (start + needed)) {
2218                         int i = start;
2219
2220                         /* there was enough, so assign it to the requestor */
2221                         while (i != end)
2222                                 res->list[i++] = id;
2223
2224                         return start;
2225                 }
2226         } while (end < res->end);
2227
2228         return -ENOMEM;
2229 }
2230
2231 /**
2232  * ice_get_res - get a block of resources
2233  * @pf: board private structure
2234  * @res: pointer to the resource
2235  * @needed: size of the block needed
2236  * @id: identifier to track owner
2237  *
2238  * Returns the base item index of the block, or negative for error
2239  */
2240 int
2241 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2242 {
2243         if (!res || !pf)
2244                 return -EINVAL;
2245
2246         if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2247                 dev_err(ice_pf_to_dev(pf),
2248                         "param err: needed=%d, num_entries = %d id=0x%04x\n",
2249                         needed, res->num_entries, id);
2250                 return -EINVAL;
2251         }
2252
2253         return ice_search_res(res, needed, id);
2254 }
2255
2256 /**
2257  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2258  * @vsi: the VSI being un-configured
2259  */
2260 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2261 {
2262         int base = vsi->base_vector;
2263         struct ice_pf *pf = vsi->back;
2264         struct ice_hw *hw = &pf->hw;
2265         u32 val;
2266         int i;
2267
2268         /* disable interrupt causation from each queue */
2269         if (vsi->tx_rings) {
2270                 ice_for_each_txq(vsi, i) {
2271                         if (vsi->tx_rings[i]) {
2272                                 u16 reg;
2273
2274                                 reg = vsi->tx_rings[i]->reg_idx;
2275                                 val = rd32(hw, QINT_TQCTL(reg));
2276                                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2277                                 wr32(hw, QINT_TQCTL(reg), val);
2278                         }
2279                 }
2280         }
2281
2282         if (vsi->rx_rings) {
2283                 ice_for_each_rxq(vsi, i) {
2284                         if (vsi->rx_rings[i]) {
2285                                 u16 reg;
2286
2287                                 reg = vsi->rx_rings[i]->reg_idx;
2288                                 val = rd32(hw, QINT_RQCTL(reg));
2289                                 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2290                                 wr32(hw, QINT_RQCTL(reg), val);
2291                         }
2292                 }
2293         }
2294
2295         /* disable each interrupt */
2296         ice_for_each_q_vector(vsi, i) {
2297                 if (!vsi->q_vectors[i])
2298                         continue;
2299                 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2300         }
2301
2302         ice_flush(hw);
2303
2304         /* don't call synchronize_irq() for VF's from the host */
2305         if (vsi->type == ICE_VSI_VF)
2306                 return;
2307
2308         ice_for_each_q_vector(vsi, i)
2309                 synchronize_irq(pf->msix_entries[i + base].vector);
2310 }
2311
2312 /**
2313  * ice_napi_del - Remove NAPI handler for the VSI
2314  * @vsi: VSI for which NAPI handler is to be removed
2315  */
2316 void ice_napi_del(struct ice_vsi *vsi)
2317 {
2318         int v_idx;
2319
2320         if (!vsi->netdev)
2321                 return;
2322
2323         ice_for_each_q_vector(vsi, v_idx)
2324                 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2325 }
2326
2327 /**
2328  * ice_vsi_release - Delete a VSI and free its resources
2329  * @vsi: the VSI being removed
2330  *
2331  * Returns 0 on success or < 0 on error
2332  */
2333 int ice_vsi_release(struct ice_vsi *vsi)
2334 {
2335         struct ice_pf *pf;
2336
2337         if (!vsi->back)
2338                 return -ENODEV;
2339         pf = vsi->back;
2340
2341         /* do not unregister while driver is in the reset recovery pending
2342          * state. Since reset/rebuild happens through PF service task workqueue,
2343          * it's not a good idea to unregister netdev that is associated to the
2344          * PF that is running the work queue items currently. This is done to
2345          * avoid check_flush_dependency() warning on this wq
2346          */
2347         if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2348                 unregister_netdev(vsi->netdev);
2349
2350         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2351                 ice_rss_clean(vsi);
2352
2353         /* Disable VSI and free resources */
2354         if (vsi->type != ICE_VSI_LB)
2355                 ice_vsi_dis_irq(vsi);
2356         ice_vsi_close(vsi);
2357
2358         /* SR-IOV determines needed MSIX resources all at once instead of per
2359          * VSI since when VFs are spawned we know how many VFs there are and how
2360          * many interrupts each VF needs. SR-IOV MSIX resources are also
2361          * cleared in the same manner.
2362          */
2363         if (vsi->type != ICE_VSI_VF) {
2364                 /* reclaim SW interrupts back to the common pool */
2365                 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2366                 pf->num_avail_sw_msix += vsi->num_q_vectors;
2367         }
2368
2369         if (!ice_is_safe_mode(pf)) {
2370                 if (vsi->type == ICE_VSI_PF) {
2371                         ice_vsi_add_rem_eth_mac(vsi, false);
2372                         ice_cfg_sw_lldp(vsi, true, false);
2373                         /* The Rx rule will only exist to remove if the LLDP FW
2374                          * engine is currently stopped
2375                          */
2376                         if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2377                                 ice_cfg_sw_lldp(vsi, false, false);
2378                 }
2379         }
2380
2381         ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2382         ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2383         ice_vsi_delete(vsi);
2384         ice_vsi_free_q_vectors(vsi);
2385
2386         /* make sure unregister_netdev() was called by checking __ICE_DOWN */
2387         if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2388                 free_netdev(vsi->netdev);
2389                 vsi->netdev = NULL;
2390         }
2391
2392         ice_vsi_clear_rings(vsi);
2393
2394         ice_vsi_put_qs(vsi);
2395
2396         /* retain SW VSI data structure since it is needed to unregister and
2397          * free VSI netdev when PF is not in reset recovery pending state,\
2398          * for ex: during rmmod.
2399          */
2400         if (!ice_is_reset_in_progress(pf->state))
2401                 ice_vsi_clear(vsi);
2402
2403         return 0;
2404 }
2405
2406 /**
2407  * ice_vsi_rebuild - Rebuild VSI after reset
2408  * @vsi: VSI to be rebuild
2409  * @init_vsi: is this an initialization or a reconfigure of the VSI
2410  *
2411  * Returns 0 on success and negative value on failure
2412  */
2413 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2414 {
2415         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2416         struct ice_vf *vf = NULL;
2417         enum ice_status status;
2418         struct ice_pf *pf;
2419         int ret, i;
2420
2421         if (!vsi)
2422                 return -EINVAL;
2423
2424         pf = vsi->back;
2425         if (vsi->type == ICE_VSI_VF)
2426                 vf = &pf->vf[vsi->vf_id];
2427
2428         ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2429         ice_vsi_free_q_vectors(vsi);
2430
2431         /* SR-IOV determines needed MSIX resources all at once instead of per
2432          * VSI since when VFs are spawned we know how many VFs there are and how
2433          * many interrupts each VF needs. SR-IOV MSIX resources are also
2434          * cleared in the same manner.
2435          */
2436         if (vsi->type != ICE_VSI_VF) {
2437                 /* reclaim SW interrupts back to the common pool */
2438                 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2439                 pf->num_avail_sw_msix += vsi->num_q_vectors;
2440                 vsi->base_vector = 0;
2441         }
2442
2443         if (ice_is_xdp_ena_vsi(vsi))
2444                 /* return value check can be skipped here, it always returns
2445                  * 0 if reset is in progress
2446                  */
2447                 ice_destroy_xdp_rings(vsi);
2448         ice_vsi_put_qs(vsi);
2449         ice_vsi_clear_rings(vsi);
2450         ice_vsi_free_arrays(vsi);
2451         ice_dev_onetime_setup(&pf->hw);
2452         if (vsi->type == ICE_VSI_VF)
2453                 ice_vsi_set_num_qs(vsi, vf->vf_id);
2454         else
2455                 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2456
2457         ret = ice_vsi_alloc_arrays(vsi);
2458         if (ret < 0)
2459                 goto err_vsi;
2460
2461         ice_vsi_get_qs(vsi);
2462         ice_vsi_set_tc_cfg(vsi);
2463
2464         /* Initialize VSI struct elements and create VSI in FW */
2465         ret = ice_vsi_init(vsi, init_vsi);
2466         if (ret < 0)
2467                 goto err_vsi;
2468
2469         switch (vsi->type) {
2470         case ICE_VSI_PF:
2471                 ret = ice_vsi_alloc_q_vectors(vsi);
2472                 if (ret)
2473                         goto err_rings;
2474
2475                 ret = ice_vsi_setup_vector_base(vsi);
2476                 if (ret)
2477                         goto err_vectors;
2478
2479                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2480                 if (ret)
2481                         goto err_vectors;
2482
2483                 ret = ice_vsi_alloc_rings(vsi);
2484                 if (ret)
2485                         goto err_vectors;
2486
2487                 ice_vsi_map_rings_to_vectors(vsi);
2488                 if (ice_is_xdp_ena_vsi(vsi)) {
2489                         vsi->num_xdp_txq = vsi->alloc_txq;
2490                         ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2491                         if (ret)
2492                                 goto err_vectors;
2493                 }
2494                 /* Do not exit if configuring RSS had an issue, at least
2495                  * receive traffic on first queue. Hence no need to capture
2496                  * return value
2497                  */
2498                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2499                         ice_vsi_cfg_rss_lut_key(vsi);
2500                 break;
2501         case ICE_VSI_VF:
2502                 ret = ice_vsi_alloc_q_vectors(vsi);
2503                 if (ret)
2504                         goto err_rings;
2505
2506                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2507                 if (ret)
2508                         goto err_vectors;
2509
2510                 ret = ice_vsi_alloc_rings(vsi);
2511                 if (ret)
2512                         goto err_vectors;
2513
2514                 break;
2515         default:
2516                 break;
2517         }
2518
2519         /* configure VSI nodes based on number of queues and TC's */
2520         for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2521                 max_txqs[i] = vsi->alloc_txq;
2522
2523                 if (ice_is_xdp_ena_vsi(vsi))
2524                         max_txqs[i] += vsi->num_xdp_txq;
2525         }
2526
2527         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2528                                  max_txqs);
2529         if (status) {
2530                 dev_err(ice_pf_to_dev(pf),
2531                         "VSI %d failed lan queue config, error %d\n",
2532                         vsi->vsi_num, status);
2533                 if (init_vsi) {
2534                         ret = -EIO;
2535                         goto err_vectors;
2536                 } else {
2537                         return ice_schedule_reset(pf, ICE_RESET_PFR);
2538                 }
2539         }
2540         return 0;
2541
2542 err_vectors:
2543         ice_vsi_free_q_vectors(vsi);
2544 err_rings:
2545         if (vsi->netdev) {
2546                 vsi->current_netdev_flags = 0;
2547                 unregister_netdev(vsi->netdev);
2548                 free_netdev(vsi->netdev);
2549                 vsi->netdev = NULL;
2550         }
2551 err_vsi:
2552         ice_vsi_clear(vsi);
2553         set_bit(__ICE_RESET_FAILED, pf->state);
2554         return ret;
2555 }
2556
2557 /**
2558  * ice_is_reset_in_progress - check for a reset in progress
2559  * @state: PF state field
2560  */
2561 bool ice_is_reset_in_progress(unsigned long *state)
2562 {
2563         return test_bit(__ICE_RESET_OICR_RECV, state) ||
2564                test_bit(__ICE_DCBNL_DEVRESET, state) ||
2565                test_bit(__ICE_PFR_REQ, state) ||
2566                test_bit(__ICE_CORER_REQ, state) ||
2567                test_bit(__ICE_GLOBR_REQ, state);
2568 }
2569
2570 #ifdef CONFIG_DCB
2571 /**
2572  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2573  * @vsi: VSI being configured
2574  * @ctx: the context buffer returned from AQ VSI update command
2575  */
2576 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2577 {
2578         vsi->info.mapping_flags = ctx->info.mapping_flags;
2579         memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2580                sizeof(vsi->info.q_mapping));
2581         memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2582                sizeof(vsi->info.tc_mapping));
2583 }
2584
2585 /**
2586  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2587  * @vsi: VSI to be configured
2588  * @ena_tc: TC bitmap
2589  *
2590  * VSI queues expected to be quiesced before calling this function
2591  */
2592 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2593 {
2594         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2595         struct ice_vsi_ctx *ctx;
2596         struct ice_pf *pf = vsi->back;
2597         enum ice_status status;
2598         struct device *dev;
2599         int i, ret = 0;
2600         u8 num_tc = 0;
2601
2602         dev = ice_pf_to_dev(pf);
2603
2604         ice_for_each_traffic_class(i) {
2605                 /* build bitmap of enabled TCs */
2606                 if (ena_tc & BIT(i))
2607                         num_tc++;
2608                 /* populate max_txqs per TC */
2609                 max_txqs[i] = vsi->alloc_txq;
2610         }
2611
2612         vsi->tc_cfg.ena_tc = ena_tc;
2613         vsi->tc_cfg.numtc = num_tc;
2614
2615         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2616         if (!ctx)
2617                 return -ENOMEM;
2618
2619         ctx->vf_num = 0;
2620         ctx->info = vsi->info;
2621
2622         ice_vsi_setup_q_map(vsi, ctx);
2623
2624         /* must to indicate which section of VSI context are being modified */
2625         ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2626         status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2627         if (status) {
2628                 dev_info(dev, "Failed VSI Update\n");
2629                 ret = -EIO;
2630                 goto out;
2631         }
2632
2633         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2634                                  max_txqs);
2635
2636         if (status) {
2637                 dev_err(dev, "VSI %d failed TC config, error %d\n",
2638                         vsi->vsi_num, status);
2639                 ret = -EIO;
2640                 goto out;
2641         }
2642         ice_vsi_update_q_map(vsi, ctx);
2643         vsi->info.valid_sections = 0;
2644
2645         ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2646 out:
2647         kfree(ctx);
2648         return ret;
2649 }
2650 #endif /* CONFIG_DCB */
2651
2652 /**
2653  * ice_nvm_version_str - format the NVM version strings
2654  * @hw: ptr to the hardware info
2655  */
2656 char *ice_nvm_version_str(struct ice_hw *hw)
2657 {
2658         u8 oem_ver, oem_patch, ver_hi, ver_lo;
2659         static char buf[ICE_NVM_VER_LEN];
2660         u16 oem_build;
2661
2662         ice_get_nvm_version(hw, &oem_ver, &oem_build, &oem_patch, &ver_hi,
2663                             &ver_lo);
2664
2665         snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d", ver_hi, ver_lo,
2666                  hw->nvm.eetrack, oem_ver, oem_build, oem_patch);
2667
2668         return buf;
2669 }
2670
2671 /**
2672  * ice_update_ring_stats - Update ring statistics
2673  * @ring: ring to update
2674  * @cont: used to increment per-vector counters
2675  * @pkts: number of processed packets
2676  * @bytes: number of processed bytes
2677  *
2678  * This function assumes that caller has acquired a u64_stats_sync lock.
2679  */
2680 static void
2681 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2682                       u64 pkts, u64 bytes)
2683 {
2684         ring->stats.bytes += bytes;
2685         ring->stats.pkts += pkts;
2686         cont->total_bytes += bytes;
2687         cont->total_pkts += pkts;
2688 }
2689
2690 /**
2691  * ice_update_tx_ring_stats - Update Tx ring specific counters
2692  * @tx_ring: ring to update
2693  * @pkts: number of processed packets
2694  * @bytes: number of processed bytes
2695  */
2696 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
2697 {
2698         u64_stats_update_begin(&tx_ring->syncp);
2699         ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
2700         u64_stats_update_end(&tx_ring->syncp);
2701 }
2702
2703 /**
2704  * ice_update_rx_ring_stats - Update Rx ring specific counters
2705  * @rx_ring: ring to update
2706  * @pkts: number of processed packets
2707  * @bytes: number of processed bytes
2708  */
2709 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
2710 {
2711         u64_stats_update_begin(&rx_ring->syncp);
2712         ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
2713         u64_stats_update_end(&rx_ring->syncp);
2714 }
2715
2716 /**
2717  * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
2718  * @vsi: the VSI being configured MAC filter
2719  * @macaddr: the MAC address to be added.
2720  * @set: Add or delete a MAC filter
2721  *
2722  * Adds or removes MAC address filter entry for VF VSI
2723  */
2724 enum ice_status
2725 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
2726 {
2727         LIST_HEAD(tmp_add_list);
2728         enum ice_status status;
2729
2730          /* Update MAC filter list to be added or removed for a VSI */
2731         if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
2732                 status = ICE_ERR_NO_MEMORY;
2733                 goto cfg_mac_fltr_exit;
2734         }
2735
2736         if (set)
2737                 status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
2738         else
2739                 status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
2740
2741 cfg_mac_fltr_exit:
2742         ice_free_fltr_list(&vsi->back->pdev->dev, &tmp_add_list);
2743         return status;
2744 }
2745
2746 /**
2747  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
2748  * @sw: switch to check if its default forwarding VSI is free
2749  *
2750  * Return true if the default forwarding VSI is already being used, else returns
2751  * false signalling that it's available to use.
2752  */
2753 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
2754 {
2755         return (sw->dflt_vsi && sw->dflt_vsi_ena);
2756 }
2757
2758 /**
2759  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
2760  * @sw: switch for the default forwarding VSI to compare against
2761  * @vsi: VSI to compare against default forwarding VSI
2762  *
2763  * If this VSI passed in is the default forwarding VSI then return true, else
2764  * return false
2765  */
2766 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
2767 {
2768         return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
2769 }
2770
2771 /**
2772  * ice_set_dflt_vsi - set the default forwarding VSI
2773  * @sw: switch used to assign the default forwarding VSI
2774  * @vsi: VSI getting set as the default forwarding VSI on the switch
2775  *
2776  * If the VSI passed in is already the default VSI and it's enabled just return
2777  * success.
2778  *
2779  * If there is already a default VSI on the switch and it's enabled then return
2780  * -EEXIST since there can only be one default VSI per switch.
2781  *
2782  *  Otherwise try to set the VSI passed in as the switch's default VSI and
2783  *  return the result.
2784  */
2785 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
2786 {
2787         enum ice_status status;
2788         struct device *dev;
2789
2790         if (!sw || !vsi)
2791                 return -EINVAL;
2792
2793         dev = ice_pf_to_dev(vsi->back);
2794
2795         /* the VSI passed in is already the default VSI */
2796         if (ice_is_vsi_dflt_vsi(sw, vsi)) {
2797                 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
2798                         vsi->vsi_num);
2799                 return 0;
2800         }
2801
2802         /* another VSI is already the default VSI for this switch */
2803         if (ice_is_dflt_vsi_in_use(sw)) {
2804                 dev_err(dev,
2805                         "Default forwarding VSI %d already in use, disable it and try again\n",
2806                         sw->dflt_vsi->vsi_num);
2807                 return -EEXIST;
2808         }
2809
2810         status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
2811         if (status) {
2812                 dev_err(dev,
2813                         "Failed to set VSI %d as the default forwarding VSI, error %d\n",
2814                         vsi->vsi_num, status);
2815                 return -EIO;
2816         }
2817
2818         sw->dflt_vsi = vsi;
2819         sw->dflt_vsi_ena = true;
2820
2821         return 0;
2822 }
2823
2824 /**
2825  * ice_clear_dflt_vsi - clear the default forwarding VSI
2826  * @sw: switch used to clear the default VSI
2827  *
2828  * If the switch has no default VSI or it's not enabled then return error.
2829  *
2830  * Otherwise try to clear the default VSI and return the result.
2831  */
2832 int ice_clear_dflt_vsi(struct ice_sw *sw)
2833 {
2834         struct ice_vsi *dflt_vsi;
2835         enum ice_status status;
2836         struct device *dev;
2837
2838         if (!sw)
2839                 return -EINVAL;
2840
2841         dev = ice_pf_to_dev(sw->pf);
2842
2843         dflt_vsi = sw->dflt_vsi;
2844
2845         /* there is no default VSI configured */
2846         if (!ice_is_dflt_vsi_in_use(sw))
2847                 return -ENODEV;
2848
2849         status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
2850                                   ICE_FLTR_RX);
2851         if (status) {
2852                 dev_err(dev,
2853                         "Failed to clear the default forwarding VSI %d, error %d\n",
2854                         dflt_vsi->vsi_num, status);
2855                 return -EIO;
2856         }
2857
2858         sw->dflt_vsi = NULL;
2859         sw->dflt_vsi_ena = false;
2860
2861         return 0;
2862 }