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