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