]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_lib.c
ice: Move common functions out of ice_main.c part 7/7
[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
7 /**
8  * ice_setup_rx_ctx - Configure a receive ring context
9  * @ring: The Rx ring to configure
10  *
11  * Configure the Rx descriptor ring in RLAN context.
12  */
13 static int ice_setup_rx_ctx(struct ice_ring *ring)
14 {
15         struct ice_vsi *vsi = ring->vsi;
16         struct ice_hw *hw = &vsi->back->hw;
17         u32 rxdid = ICE_RXDID_FLEX_NIC;
18         struct ice_rlan_ctx rlan_ctx;
19         u32 regval;
20         u16 pf_q;
21         int err;
22
23         /* what is RX queue number in global space of 2K Rx queues */
24         pf_q = vsi->rxq_map[ring->q_index];
25
26         /* clear the context structure first */
27         memset(&rlan_ctx, 0, sizeof(rlan_ctx));
28
29         rlan_ctx.base = ring->dma >> 7;
30
31         rlan_ctx.qlen = ring->count;
32
33         /* Receive Packet Data Buffer Size.
34          * The Packet Data Buffer Size is defined in 128 byte units.
35          */
36         rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
37
38         /* use 32 byte descriptors */
39         rlan_ctx.dsize = 1;
40
41         /* Strip the Ethernet CRC bytes before the packet is posted to host
42          * memory.
43          */
44         rlan_ctx.crcstrip = 1;
45
46         /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
47         rlan_ctx.l2tsel = 1;
48
49         rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
50         rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
51         rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
52
53         /* This controls whether VLAN is stripped from inner headers
54          * The VLAN in the inner L2 header is stripped to the receive
55          * descriptor if enabled by this flag.
56          */
57         rlan_ctx.showiv = 0;
58
59         /* Max packet size for this queue - must not be set to a larger value
60          * than 5 x DBUF
61          */
62         rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
63                                ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
64
65         /* Rx queue threshold in units of 64 */
66         rlan_ctx.lrxqthresh = 1;
67
68          /* Enable Flexible Descriptors in the queue context which
69           * allows this driver to select a specific receive descriptor format
70           */
71         regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
72         regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
73                 QRXFLXP_CNTXT_RXDID_IDX_M;
74
75         /* increasing context priority to pick up profile id;
76          * default is 0x01; setting to 0x03 to ensure profile
77          * is programming if prev context is of same priority
78          */
79         regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
80                 QRXFLXP_CNTXT_RXDID_PRIO_M;
81
82         wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
83
84         /* Absolute queue number out of 2K needs to be passed */
85         err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
86         if (err) {
87                 dev_err(&vsi->back->pdev->dev,
88                         "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
89                         pf_q, err);
90                 return -EIO;
91         }
92
93         /* init queue specific tail register */
94         ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
95         writel(0, ring->tail);
96         ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
97
98         return 0;
99 }
100
101 /**
102  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
103  * @ring: The Tx ring to configure
104  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
105  * @pf_q: queue index in the PF space
106  *
107  * Configure the Tx descriptor ring in TLAN context.
108  */
109 static void
110 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
111 {
112         struct ice_vsi *vsi = ring->vsi;
113         struct ice_hw *hw = &vsi->back->hw;
114
115         tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
116
117         tlan_ctx->port_num = vsi->port_info->lport;
118
119         /* Transmit Queue Length */
120         tlan_ctx->qlen = ring->count;
121
122         /* PF number */
123         tlan_ctx->pf_num = hw->pf_id;
124
125         /* queue belongs to a specific VSI type
126          * VF / VM index should be programmed per vmvf_type setting:
127          * for vmvf_type = VF, it is VF number between 0-256
128          * for vmvf_type = VM, it is VM number between 0-767
129          * for PF or EMP this field should be set to zero
130          */
131         switch (vsi->type) {
132         case ICE_VSI_PF:
133                 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
134                 break;
135         default:
136                 return;
137         }
138
139         /* make sure the context is associated with the right VSI */
140         tlan_ctx->src_vsi = vsi->vsi_num;
141
142         tlan_ctx->tso_ena = ICE_TX_LEGACY;
143         tlan_ctx->tso_qnum = pf_q;
144
145         /* Legacy or Advanced Host Interface:
146          * 0: Advanced Host Interface
147          * 1: Legacy Host Interface
148          */
149         tlan_ctx->legacy_int = ICE_TX_LEGACY;
150 }
151
152 /**
153  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
154  * @pf: the PF being configured
155  * @pf_q: the PF queue
156  * @ena: enable or disable state of the queue
157  *
158  * This routine will wait for the given Rx queue of the PF to reach the
159  * enabled or disabled state.
160  * Returns -ETIMEDOUT in case of failing to reach the requested state after
161  * multiple retries; else will return 0 in case of success.
162  */
163 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
164 {
165         int i;
166
167         for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
168                 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
169
170                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
171                         break;
172
173                 usleep_range(10, 20);
174         }
175         if (i >= ICE_Q_WAIT_RETRY_LIMIT)
176                 return -ETIMEDOUT;
177
178         return 0;
179 }
180
181 /**
182  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
183  * @vsi: the VSI being configured
184  * @ena: start or stop the Rx rings
185  */
186 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
187 {
188         struct ice_pf *pf = vsi->back;
189         struct ice_hw *hw = &pf->hw;
190         int i, j, ret = 0;
191
192         for (i = 0; i < vsi->num_rxq; i++) {
193                 int pf_q = vsi->rxq_map[i];
194                 u32 rx_reg;
195
196                 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
197                         rx_reg = rd32(hw, QRX_CTRL(pf_q));
198                         if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
199                             ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
200                                 break;
201                         usleep_range(1000, 2000);
202                 }
203
204                 /* Skip if the queue is already in the requested state */
205                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
206                         continue;
207
208                 /* turn on/off the queue */
209                 if (ena)
210                         rx_reg |= QRX_CTRL_QENA_REQ_M;
211                 else
212                         rx_reg &= ~QRX_CTRL_QENA_REQ_M;
213                 wr32(hw, QRX_CTRL(pf_q), rx_reg);
214
215                 /* wait for the change to finish */
216                 ret = ice_pf_rxq_wait(pf, pf_q, ena);
217                 if (ret) {
218                         dev_err(&pf->pdev->dev,
219                                 "VSI idx %d Rx ring %d %sable timeout\n",
220                                 vsi->idx, pf_q, (ena ? "en" : "dis"));
221                         break;
222                 }
223         }
224
225         return ret;
226 }
227
228 /**
229  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
230  * @vsi: VSI pointer
231  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
232  *
233  * On error: returns error code (negative)
234  * On success: returns 0
235  */
236 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
237 {
238         struct ice_pf *pf = vsi->back;
239
240         /* allocate memory for both Tx and Rx ring pointers */
241         vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
242                                      sizeof(struct ice_ring *), GFP_KERNEL);
243         if (!vsi->tx_rings)
244                 goto err_txrings;
245
246         vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
247                                      sizeof(struct ice_ring *), GFP_KERNEL);
248         if (!vsi->rx_rings)
249                 goto err_rxrings;
250
251         if (alloc_qvectors) {
252                 /* allocate memory for q_vector pointers */
253                 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
254                                               vsi->num_q_vectors,
255                                               sizeof(struct ice_q_vector *),
256                                               GFP_KERNEL);
257                 if (!vsi->q_vectors)
258                         goto err_vectors;
259         }
260
261         return 0;
262
263 err_vectors:
264         devm_kfree(&pf->pdev->dev, vsi->rx_rings);
265 err_rxrings:
266         devm_kfree(&pf->pdev->dev, vsi->tx_rings);
267 err_txrings:
268         return -ENOMEM;
269 }
270
271 /**
272  * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
273  * @vsi: the VSI being configured
274  *
275  * Return 0 on success and a negative value on error
276  */
277 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
278 {
279         struct ice_pf *pf = vsi->back;
280
281         switch (vsi->type) {
282         case ICE_VSI_PF:
283                 vsi->alloc_txq = pf->num_lan_tx;
284                 vsi->alloc_rxq = pf->num_lan_rx;
285                 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
286                 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
287                 break;
288         default:
289                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
290                          vsi->type);
291                 break;
292         }
293 }
294
295 /**
296  * ice_get_free_slot - get the next non-NULL location index in array
297  * @array: array to search
298  * @size: size of the array
299  * @curr: last known occupied index to be used as a search hint
300  *
301  * void * is being used to keep the functionality generic. This lets us use this
302  * function on any array of pointers.
303  */
304 static int ice_get_free_slot(void *array, int size, int curr)
305 {
306         int **tmp_array = (int **)array;
307         int next;
308
309         if (curr < (size - 1) && !tmp_array[curr + 1]) {
310                 next = curr + 1;
311         } else {
312                 int i = 0;
313
314                 while ((i < size) && (tmp_array[i]))
315                         i++;
316                 if (i == size)
317                         next = ICE_NO_VSI;
318                 else
319                         next = i;
320         }
321         return next;
322 }
323
324 /**
325  * ice_vsi_delete - delete a VSI from the switch
326  * @vsi: pointer to VSI being removed
327  */
328 void ice_vsi_delete(struct ice_vsi *vsi)
329 {
330         struct ice_pf *pf = vsi->back;
331         struct ice_vsi_ctx ctxt;
332         enum ice_status status;
333
334         ctxt.vsi_num = vsi->vsi_num;
335
336         memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
337
338         status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
339         if (status)
340                 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
341                         vsi->vsi_num);
342 }
343
344 /**
345  * ice_vsi_free_arrays - clean up VSI resources
346  * @vsi: pointer to VSI being cleared
347  * @free_qvectors: bool to specify if q_vectors should be deallocated
348  */
349 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
350 {
351         struct ice_pf *pf = vsi->back;
352
353         /* free the ring and vector containers */
354         if (free_qvectors && vsi->q_vectors) {
355                 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
356                 vsi->q_vectors = NULL;
357         }
358         if (vsi->tx_rings) {
359                 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
360                 vsi->tx_rings = NULL;
361         }
362         if (vsi->rx_rings) {
363                 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
364                 vsi->rx_rings = NULL;
365         }
366 }
367
368 /**
369  * ice_vsi_clear - clean up and deallocate the provided VSI
370  * @vsi: pointer to VSI being cleared
371  *
372  * This deallocates the VSI's queue resources, removes it from the PF's
373  * VSI array if necessary, and deallocates the VSI
374  *
375  * Returns 0 on success, negative on failure
376  */
377 int ice_vsi_clear(struct ice_vsi *vsi)
378 {
379         struct ice_pf *pf = NULL;
380
381         if (!vsi)
382                 return 0;
383
384         if (!vsi->back)
385                 return -EINVAL;
386
387         pf = vsi->back;
388
389         if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
390                 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
391                         vsi->idx);
392                 return -EINVAL;
393         }
394
395         mutex_lock(&pf->sw_mutex);
396         /* updates the PF for this cleared VSI */
397
398         pf->vsi[vsi->idx] = NULL;
399         if (vsi->idx < pf->next_vsi)
400                 pf->next_vsi = vsi->idx;
401
402         ice_vsi_free_arrays(vsi, true);
403         mutex_unlock(&pf->sw_mutex);
404         devm_kfree(&pf->pdev->dev, vsi);
405
406         return 0;
407 }
408
409 /**
410  * ice_msix_clean_rings - MSIX mode Interrupt Handler
411  * @irq: interrupt number
412  * @data: pointer to a q_vector
413  */
414 irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
415 {
416         struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
417
418         if (!q_vector->tx.ring && !q_vector->rx.ring)
419                 return IRQ_HANDLED;
420
421         napi_schedule(&q_vector->napi);
422
423         return IRQ_HANDLED;
424 }
425
426 /**
427  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
428  * @pf: board private structure
429  * @type: type of VSI
430  *
431  * returns a pointer to a VSI on success, NULL on failure.
432  */
433 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
434 {
435         struct ice_vsi *vsi = NULL;
436
437         /* Need to protect the allocation of the VSIs at the PF level */
438         mutex_lock(&pf->sw_mutex);
439
440         /* If we have already allocated our maximum number of VSIs,
441          * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
442          * is available to be populated
443          */
444         if (pf->next_vsi == ICE_NO_VSI) {
445                 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
446                 goto unlock_pf;
447         }
448
449         vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
450         if (!vsi)
451                 goto unlock_pf;
452
453         vsi->type = type;
454         vsi->back = pf;
455         set_bit(__ICE_DOWN, vsi->state);
456         vsi->idx = pf->next_vsi;
457         vsi->work_lmt = ICE_DFLT_IRQ_WORK;
458
459         ice_vsi_set_num_qs(vsi);
460
461         switch (vsi->type) {
462         case ICE_VSI_PF:
463                 if (ice_vsi_alloc_arrays(vsi, true))
464                         goto err_rings;
465
466                 /* Setup default MSIX irq handler for VSI */
467                 vsi->irq_handler = ice_msix_clean_rings;
468                 break;
469         default:
470                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
471                 goto unlock_pf;
472         }
473
474         /* fill VSI slot in the PF struct */
475         pf->vsi[pf->next_vsi] = vsi;
476
477         /* prepare pf->next_vsi for next use */
478         pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
479                                          pf->next_vsi);
480         goto unlock_pf;
481
482 err_rings:
483         devm_kfree(&pf->pdev->dev, vsi);
484         vsi = NULL;
485 unlock_pf:
486         mutex_unlock(&pf->sw_mutex);
487         return vsi;
488 }
489
490 /**
491  * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
492  * @vsi: the VSI getting queues
493  *
494  * Return 0 on success and a negative value on error
495  */
496 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
497 {
498         struct ice_pf *pf = vsi->back;
499         int offset, ret = 0;
500
501         mutex_lock(&pf->avail_q_mutex);
502         /* look for contiguous block of queues for Tx */
503         offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
504                                             0, vsi->alloc_txq, 0);
505         if (offset < ICE_MAX_TXQS) {
506                 int i;
507
508                 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
509                 for (i = 0; i < vsi->alloc_txq; i++)
510                         vsi->txq_map[i] = i + offset;
511         } else {
512                 ret = -ENOMEM;
513                 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
514         }
515
516         /* look for contiguous block of queues for Rx */
517         offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
518                                             0, vsi->alloc_rxq, 0);
519         if (offset < ICE_MAX_RXQS) {
520                 int i;
521
522                 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
523                 for (i = 0; i < vsi->alloc_rxq; i++)
524                         vsi->rxq_map[i] = i + offset;
525         } else {
526                 ret = -ENOMEM;
527                 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
528         }
529         mutex_unlock(&pf->avail_q_mutex);
530
531         return ret;
532 }
533
534 /**
535  * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
536  * @vsi: the VSI getting queues
537  *
538  * Return 0 on success and a negative value on error
539  */
540 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
541 {
542         struct ice_pf *pf = vsi->back;
543         int i, index = 0;
544
545         mutex_lock(&pf->avail_q_mutex);
546
547         if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
548                 for (i = 0; i < vsi->alloc_txq; i++) {
549                         index = find_next_zero_bit(pf->avail_txqs,
550                                                    ICE_MAX_TXQS, index);
551                         if (index < ICE_MAX_TXQS) {
552                                 set_bit(index, pf->avail_txqs);
553                                 vsi->txq_map[i] = index;
554                         } else {
555                                 goto err_scatter_tx;
556                         }
557                 }
558         }
559
560         if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
561                 for (i = 0; i < vsi->alloc_rxq; i++) {
562                         index = find_next_zero_bit(pf->avail_rxqs,
563                                                    ICE_MAX_RXQS, index);
564                         if (index < ICE_MAX_RXQS) {
565                                 set_bit(index, pf->avail_rxqs);
566                                 vsi->rxq_map[i] = index;
567                         } else {
568                                 goto err_scatter_rx;
569                         }
570                 }
571         }
572
573         mutex_unlock(&pf->avail_q_mutex);
574         return 0;
575
576 err_scatter_rx:
577         /* unflag any queues we have grabbed (i is failed position) */
578         for (index = 0; index < i; index++) {
579                 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
580                 vsi->rxq_map[index] = 0;
581         }
582         i = vsi->alloc_txq;
583 err_scatter_tx:
584         /* i is either position of failed attempt or vsi->alloc_txq */
585         for (index = 0; index < i; index++) {
586                 clear_bit(vsi->txq_map[index], pf->avail_txqs);
587                 vsi->txq_map[index] = 0;
588         }
589
590         mutex_unlock(&pf->avail_q_mutex);
591         return -ENOMEM;
592 }
593
594 /**
595  * ice_vsi_get_qs - Assign queues from PF to VSI
596  * @vsi: the VSI to assign queues to
597  *
598  * Returns 0 on success and a negative value on error
599  */
600 static int ice_vsi_get_qs(struct ice_vsi *vsi)
601 {
602         int ret = 0;
603
604         vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
605         vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
606
607         /* NOTE: ice_vsi_get_qs_contig() will set the Rx/Tx mapping
608          * modes individually to scatter if assigning contiguous queues
609          * to Rx or Tx fails
610          */
611         ret = ice_vsi_get_qs_contig(vsi);
612         if (ret < 0) {
613                 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
614                         vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
615                                                ICE_MAX_SCATTER_TXQS);
616                 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
617                         vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
618                                                ICE_MAX_SCATTER_RXQS);
619                 ret = ice_vsi_get_qs_scatter(vsi);
620         }
621
622         return ret;
623 }
624
625 /**
626  * ice_vsi_put_qs - Release queues from VSI to PF
627  * @vsi: the VSI that is going to release queues
628  */
629 void ice_vsi_put_qs(struct ice_vsi *vsi)
630 {
631         struct ice_pf *pf = vsi->back;
632         int i;
633
634         mutex_lock(&pf->avail_q_mutex);
635
636         for (i = 0; i < vsi->alloc_txq; i++) {
637                 clear_bit(vsi->txq_map[i], pf->avail_txqs);
638                 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
639         }
640
641         for (i = 0; i < vsi->alloc_rxq; i++) {
642                 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
643                 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
644         }
645
646         mutex_unlock(&pf->avail_q_mutex);
647 }
648
649 /**
650  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
651  * @vsi: the VSI being removed
652  */
653 static void ice_rss_clean(struct ice_vsi *vsi)
654 {
655         struct ice_pf *pf;
656
657         pf = vsi->back;
658
659         if (vsi->rss_hkey_user)
660                 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
661         if (vsi->rss_lut_user)
662                 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
663 }
664
665 /**
666  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
667  * @vsi: the VSI being configured
668  */
669 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
670 {
671         struct ice_hw_common_caps *cap;
672         struct ice_pf *pf = vsi->back;
673
674         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
675                 vsi->rss_size = 1;
676                 return;
677         }
678
679         cap = &pf->hw.func_caps.common_cap;
680         switch (vsi->type) {
681         case ICE_VSI_PF:
682                 /* PF VSI will inherit RSS instance of PF */
683                 vsi->rss_table_size = cap->rss_table_size;
684                 vsi->rss_size = min_t(int, num_online_cpus(),
685                                       BIT(cap->rss_table_entry_width));
686                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
687                 break;
688         default:
689                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n",
690                          vsi->type);
691                 break;
692         }
693 }
694
695 /**
696  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
697  * @ctxt: the VSI context being set
698  *
699  * This initializes a default VSI context for all sections except the Queues.
700  */
701 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
702 {
703         u32 table = 0;
704
705         memset(&ctxt->info, 0, sizeof(ctxt->info));
706         /* VSI's should be allocated from shared pool */
707         ctxt->alloc_from_pool = true;
708         /* Src pruning enabled by default */
709         ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
710         /* Traffic from VSI can be sent to LAN */
711         ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
712         /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
713          * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
714          * packets untagged/tagged.
715          */
716         ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
717                                   ICE_AQ_VSI_VLAN_MODE_M) >>
718                                  ICE_AQ_VSI_VLAN_MODE_S);
719         /* Have 1:1 UP mapping for both ingress/egress tables */
720         table |= ICE_UP_TABLE_TRANSLATE(0, 0);
721         table |= ICE_UP_TABLE_TRANSLATE(1, 1);
722         table |= ICE_UP_TABLE_TRANSLATE(2, 2);
723         table |= ICE_UP_TABLE_TRANSLATE(3, 3);
724         table |= ICE_UP_TABLE_TRANSLATE(4, 4);
725         table |= ICE_UP_TABLE_TRANSLATE(5, 5);
726         table |= ICE_UP_TABLE_TRANSLATE(6, 6);
727         table |= ICE_UP_TABLE_TRANSLATE(7, 7);
728         ctxt->info.ingress_table = cpu_to_le32(table);
729         ctxt->info.egress_table = cpu_to_le32(table);
730         /* Have 1:1 UP mapping for outer to inner UP table */
731         ctxt->info.outer_up_table = cpu_to_le32(table);
732         /* No Outer tag support outer_tag_flags remains to zero */
733 }
734
735 /**
736  * ice_vsi_setup_q_map - Setup a VSI queue map
737  * @vsi: the VSI being configured
738  * @ctxt: VSI context structure
739  */
740 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
741 {
742         u16 offset = 0, qmap = 0, numq_tc;
743         u16 pow = 0, max_rss = 0, qcount;
744         u16 qcount_tx = vsi->alloc_txq;
745         u16 qcount_rx = vsi->alloc_rxq;
746         bool ena_tc0 = false;
747         int i;
748
749         /* at least TC0 should be enabled by default */
750         if (vsi->tc_cfg.numtc) {
751                 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
752                         ena_tc0 = true;
753         } else {
754                 ena_tc0 = true;
755         }
756
757         if (ena_tc0) {
758                 vsi->tc_cfg.numtc++;
759                 vsi->tc_cfg.ena_tc |= 1;
760         }
761
762         numq_tc = qcount_rx / vsi->tc_cfg.numtc;
763
764         /* TC mapping is a function of the number of Rx queues assigned to the
765          * VSI for each traffic class and the offset of these queues.
766          * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
767          * queues allocated to TC0. No:of queues is a power-of-2.
768          *
769          * If TC is not enabled, the queue offset is set to 0, and allocate one
770          * queue, this way, traffic for the given TC will be sent to the default
771          * queue.
772          *
773          * Setup number and offset of Rx queues for all TCs for the VSI
774          */
775
776         /* qcount will change if RSS is enabled */
777         if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
778                 if (vsi->type == ICE_VSI_PF)
779                         max_rss = ICE_MAX_LG_RSS_QS;
780                 else
781                         max_rss = ICE_MAX_SMALL_RSS_QS;
782
783                 qcount = min_t(int, numq_tc, max_rss);
784                 qcount = min_t(int, qcount, vsi->rss_size);
785         } else {
786                 qcount = numq_tc;
787         }
788
789         /* find the (rounded up) power-of-2 of qcount */
790         pow = order_base_2(qcount);
791
792         for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
793                 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
794                         /* TC is not enabled */
795                         vsi->tc_cfg.tc_info[i].qoffset = 0;
796                         vsi->tc_cfg.tc_info[i].qcount = 1;
797                         ctxt->info.tc_mapping[i] = 0;
798                         continue;
799                 }
800
801                 /* TC is enabled */
802                 vsi->tc_cfg.tc_info[i].qoffset = offset;
803                 vsi->tc_cfg.tc_info[i].qcount = qcount;
804
805                 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
806                         ICE_AQ_VSI_TC_Q_OFFSET_M) |
807                         ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
808                          ICE_AQ_VSI_TC_Q_NUM_M);
809                 offset += qcount;
810                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
811         }
812
813         vsi->num_txq = qcount_tx;
814         vsi->num_rxq = offset;
815
816         /* Rx queue mapping */
817         ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
818         /* q_mapping buffer holds the info for the first queue allocated for
819          * this VSI in the PF space and also the number of queues associated
820          * with this VSI.
821          */
822         ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
823         ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
824 }
825
826 /**
827  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
828  * @ctxt: the VSI context being set
829  * @vsi: the VSI being configured
830  */
831 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
832 {
833         u8 lut_type, hash_type;
834
835         switch (vsi->type) {
836         case ICE_VSI_PF:
837                 /* PF VSI will inherit RSS instance of PF */
838                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
839                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
840                 break;
841         default:
842                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
843                          vsi->type);
844                 return;
845         }
846
847         ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
848                                 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
849                                 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
850                                  ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
851 }
852
853 /**
854  * ice_vsi_init - Create and initialize a VSI
855  * @vsi: the VSI being configured
856  *
857  * This initializes a VSI context depending on the VSI type to be added and
858  * passes it down to the add_vsi aq command to create a new VSI.
859  */
860 static int ice_vsi_init(struct ice_vsi *vsi)
861 {
862         struct ice_vsi_ctx ctxt = { 0 };
863         struct ice_pf *pf = vsi->back;
864         struct ice_hw *hw = &pf->hw;
865         int ret = 0;
866
867         switch (vsi->type) {
868         case ICE_VSI_PF:
869                 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
870                 break;
871         default:
872                 return -ENODEV;
873         }
874
875         ice_set_dflt_vsi_ctx(&ctxt);
876         /* if the switch is in VEB mode, allow VSI loopback */
877         if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
878                 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
879
880         /* Set LUT type and HASH type if RSS is enabled */
881         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
882                 ice_set_rss_vsi_ctx(&ctxt, vsi);
883
884         ctxt.info.sw_id = vsi->port_info->sw_id;
885         ice_vsi_setup_q_map(vsi, &ctxt);
886
887         ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
888         if (ret) {
889                 dev_err(&pf->pdev->dev,
890                         "Add VSI failed, err %d\n", ret);
891                 return -EIO;
892         }
893
894         /* keep context for update VSI operations */
895         vsi->info = ctxt.info;
896
897         /* record VSI number returned */
898         vsi->vsi_num = ctxt.vsi_num;
899
900         return ret;
901 }
902
903 /**
904  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
905  * @vsi: VSI having the memory freed
906  * @v_idx: index of the vector to be freed
907  */
908 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
909 {
910         struct ice_q_vector *q_vector;
911         struct ice_ring *ring;
912
913         if (!vsi->q_vectors[v_idx]) {
914                 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
915                         v_idx);
916                 return;
917         }
918         q_vector = vsi->q_vectors[v_idx];
919
920         ice_for_each_ring(ring, q_vector->tx)
921                 ring->q_vector = NULL;
922         ice_for_each_ring(ring, q_vector->rx)
923                 ring->q_vector = NULL;
924
925         /* only VSI with an associated netdev is set up with NAPI */
926         if (vsi->netdev)
927                 netif_napi_del(&q_vector->napi);
928
929         devm_kfree(&vsi->back->pdev->dev, q_vector);
930         vsi->q_vectors[v_idx] = NULL;
931 }
932
933 /**
934  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
935  * @vsi: the VSI having memory freed
936  */
937 void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
938 {
939         int v_idx;
940
941         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
942                 ice_free_q_vector(vsi, v_idx);
943 }
944
945 /**
946  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
947  * @vsi: the VSI being configured
948  * @v_idx: index of the vector in the VSI struct
949  *
950  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
951  */
952 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
953 {
954         struct ice_pf *pf = vsi->back;
955         struct ice_q_vector *q_vector;
956
957         /* allocate q_vector */
958         q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
959         if (!q_vector)
960                 return -ENOMEM;
961
962         q_vector->vsi = vsi;
963         q_vector->v_idx = v_idx;
964         /* only set affinity_mask if the CPU is online */
965         if (cpu_online(v_idx))
966                 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
967
968         /* This will not be called in the driver load path because the netdev
969          * will not be created yet. All other cases with register the NAPI
970          * handler here (i.e. resume, reset/rebuild, etc.)
971          */
972         if (vsi->netdev)
973                 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
974                                NAPI_POLL_WEIGHT);
975
976         /* tie q_vector and VSI together */
977         vsi->q_vectors[v_idx] = q_vector;
978
979         return 0;
980 }
981
982 /**
983  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
984  * @vsi: the VSI being configured
985  *
986  * We allocate one q_vector per queue interrupt.  If allocation fails we
987  * return -ENOMEM.
988  */
989 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
990 {
991         struct ice_pf *pf = vsi->back;
992         int v_idx = 0, num_q_vectors;
993         int err;
994
995         if (vsi->q_vectors[0]) {
996                 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
997                         vsi->vsi_num);
998                 return -EEXIST;
999         }
1000
1001         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1002                 num_q_vectors = vsi->num_q_vectors;
1003         } else {
1004                 err = -EINVAL;
1005                 goto err_out;
1006         }
1007
1008         for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
1009                 err = ice_vsi_alloc_q_vector(vsi, v_idx);
1010                 if (err)
1011                         goto err_out;
1012         }
1013
1014         return 0;
1015
1016 err_out:
1017         while (v_idx--)
1018                 ice_free_q_vector(vsi, v_idx);
1019
1020         dev_err(&pf->pdev->dev,
1021                 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
1022                 vsi->num_q_vectors, vsi->vsi_num, err);
1023         vsi->num_q_vectors = 0;
1024         return err;
1025 }
1026
1027 /**
1028  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1029  * @vsi: ptr to the VSI
1030  *
1031  * This should only be called after ice_vsi_alloc() which allocates the
1032  * corresponding SW VSI structure and initializes num_queue_pairs for the
1033  * newly allocated VSI.
1034  *
1035  * Returns 0 on success or negative on failure
1036  */
1037 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1038 {
1039         struct ice_pf *pf = vsi->back;
1040         int num_q_vectors = 0;
1041
1042         if (vsi->base_vector) {
1043                 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
1044                         vsi->vsi_num, vsi->base_vector);
1045                 return -EEXIST;
1046         }
1047
1048         if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
1049                 return -ENOENT;
1050
1051         switch (vsi->type) {
1052         case ICE_VSI_PF:
1053                 num_q_vectors = vsi->num_q_vectors;
1054                 break;
1055         default:
1056                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1057                          vsi->type);
1058                 break;
1059         }
1060
1061         if (num_q_vectors)
1062                 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
1063                                                num_q_vectors, vsi->idx);
1064
1065         if (vsi->base_vector < 0) {
1066                 dev_err(&pf->pdev->dev,
1067                         "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
1068                         num_q_vectors, vsi->vsi_num, vsi->base_vector);
1069                 return -ENOENT;
1070         }
1071
1072         return 0;
1073 }
1074
1075 /**
1076  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1077  * @vsi: the VSI having rings deallocated
1078  */
1079 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1080 {
1081         int i;
1082
1083         if (vsi->tx_rings) {
1084                 for (i = 0; i < vsi->alloc_txq; i++) {
1085                         if (vsi->tx_rings[i]) {
1086                                 kfree_rcu(vsi->tx_rings[i], rcu);
1087                                 vsi->tx_rings[i] = NULL;
1088                         }
1089                 }
1090         }
1091         if (vsi->rx_rings) {
1092                 for (i = 0; i < vsi->alloc_rxq; i++) {
1093                         if (vsi->rx_rings[i]) {
1094                                 kfree_rcu(vsi->rx_rings[i], rcu);
1095                                 vsi->rx_rings[i] = NULL;
1096                         }
1097                 }
1098         }
1099 }
1100
1101 /**
1102  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1103  * @vsi: VSI which is having rings allocated
1104  */
1105 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1106 {
1107         struct ice_pf *pf = vsi->back;
1108         int i;
1109
1110         /* Allocate tx_rings */
1111         for (i = 0; i < vsi->alloc_txq; i++) {
1112                 struct ice_ring *ring;
1113
1114                 /* allocate with kzalloc(), free with kfree_rcu() */
1115                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1116
1117                 if (!ring)
1118                         goto err_out;
1119
1120                 ring->q_index = i;
1121                 ring->reg_idx = vsi->txq_map[i];
1122                 ring->ring_active = false;
1123                 ring->vsi = vsi;
1124                 ring->dev = &pf->pdev->dev;
1125                 ring->count = vsi->num_desc;
1126                 vsi->tx_rings[i] = ring;
1127         }
1128
1129         /* Allocate rx_rings */
1130         for (i = 0; i < vsi->alloc_rxq; i++) {
1131                 struct ice_ring *ring;
1132
1133                 /* allocate with kzalloc(), free with kfree_rcu() */
1134                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1135                 if (!ring)
1136                         goto err_out;
1137
1138                 ring->q_index = i;
1139                 ring->reg_idx = vsi->rxq_map[i];
1140                 ring->ring_active = false;
1141                 ring->vsi = vsi;
1142                 ring->netdev = vsi->netdev;
1143                 ring->dev = &pf->pdev->dev;
1144                 ring->count = vsi->num_desc;
1145                 vsi->rx_rings[i] = ring;
1146         }
1147
1148         return 0;
1149
1150 err_out:
1151         ice_vsi_clear_rings(vsi);
1152         return -ENOMEM;
1153 }
1154
1155 /**
1156  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1157  * @vsi: the VSI being configured
1158  *
1159  * This function maps descriptor rings to the queue-specific vectors allotted
1160  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1161  * and Rx rings to the vector as "efficiently" as possible.
1162  */
1163 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1164 {
1165         int q_vectors = vsi->num_q_vectors;
1166         int tx_rings_rem, rx_rings_rem;
1167         int v_id;
1168
1169         /* initially assigning remaining rings count to VSIs num queue value */
1170         tx_rings_rem = vsi->num_txq;
1171         rx_rings_rem = vsi->num_rxq;
1172
1173         for (v_id = 0; v_id < q_vectors; v_id++) {
1174                 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1175                 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1176
1177                 /* Tx rings mapping to vector */
1178                 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1179                 q_vector->num_ring_tx = tx_rings_per_v;
1180                 q_vector->tx.ring = NULL;
1181                 q_base = vsi->num_txq - tx_rings_rem;
1182
1183                 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1184                         struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1185
1186                         tx_ring->q_vector = q_vector;
1187                         tx_ring->next = q_vector->tx.ring;
1188                         q_vector->tx.ring = tx_ring;
1189                 }
1190                 tx_rings_rem -= tx_rings_per_v;
1191
1192                 /* Rx rings mapping to vector */
1193                 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1194                 q_vector->num_ring_rx = rx_rings_per_v;
1195                 q_vector->rx.ring = NULL;
1196                 q_base = vsi->num_rxq - rx_rings_rem;
1197
1198                 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1199                         struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1200
1201                         rx_ring->q_vector = q_vector;
1202                         rx_ring->next = q_vector->rx.ring;
1203                         q_vector->rx.ring = rx_ring;
1204                 }
1205                 rx_rings_rem -= rx_rings_per_v;
1206         }
1207 }
1208
1209 /**
1210  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1211  * @vsi: VSI to be configured
1212  */
1213 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1214 {
1215         u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
1216         struct ice_aqc_get_set_rss_keys *key;
1217         struct ice_pf *pf = vsi->back;
1218         enum ice_status status;
1219         int err = 0;
1220         u8 *lut;
1221
1222         vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1223
1224         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
1225         if (!lut)
1226                 return -ENOMEM;
1227
1228         if (vsi->rss_lut_user)
1229                 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1230         else
1231                 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1232
1233         status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
1234                                     lut, vsi->rss_table_size);
1235
1236         if (status) {
1237                 dev_err(&vsi->back->pdev->dev,
1238                         "set_rss_lut failed, error %d\n", status);
1239                 err = -EIO;
1240                 goto ice_vsi_cfg_rss_exit;
1241         }
1242
1243         key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
1244         if (!key) {
1245                 err = -ENOMEM;
1246                 goto ice_vsi_cfg_rss_exit;
1247         }
1248
1249         if (vsi->rss_hkey_user)
1250                 memcpy(seed, vsi->rss_hkey_user,
1251                        ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1252         else
1253                 netdev_rss_key_fill((void *)seed,
1254                                     ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1255         memcpy(&key->standard_rss_key, seed,
1256                ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1257
1258         status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
1259
1260         if (status) {
1261                 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
1262                         status);
1263                 err = -EIO;
1264         }
1265
1266         devm_kfree(&pf->pdev->dev, key);
1267 ice_vsi_cfg_rss_exit:
1268         devm_kfree(&pf->pdev->dev, lut);
1269         return err;
1270 }
1271
1272 /**
1273  * ice_add_mac_to_list - Add a mac address filter entry to the list
1274  * @vsi: the VSI to be forwarded to
1275  * @add_list: pointer to the list which contains MAC filter entries
1276  * @macaddr: the MAC address to be added.
1277  *
1278  * Adds mac address filter entry to the temp list
1279  *
1280  * Returns 0 on success or ENOMEM on failure.
1281  */
1282 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1283                         const u8 *macaddr)
1284 {
1285         struct ice_fltr_list_entry *tmp;
1286         struct ice_pf *pf = vsi->back;
1287
1288         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
1289         if (!tmp)
1290                 return -ENOMEM;
1291
1292         tmp->fltr_info.flag = ICE_FLTR_TX;
1293         tmp->fltr_info.src = vsi->vsi_num;
1294         tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1295         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1296         tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
1297         ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1298
1299         INIT_LIST_HEAD(&tmp->list_entry);
1300         list_add(&tmp->list_entry, add_list);
1301
1302         return 0;
1303 }
1304
1305 /**
1306  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1307  * @vsi: the VSI to be updated
1308  */
1309 void ice_update_eth_stats(struct ice_vsi *vsi)
1310 {
1311         struct ice_eth_stats *prev_es, *cur_es;
1312         struct ice_hw *hw = &vsi->back->hw;
1313         u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1314
1315         prev_es = &vsi->eth_stats_prev;
1316         cur_es = &vsi->eth_stats;
1317
1318         ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
1319                           vsi->stat_offsets_loaded, &prev_es->rx_bytes,
1320                           &cur_es->rx_bytes);
1321
1322         ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
1323                           vsi->stat_offsets_loaded, &prev_es->rx_unicast,
1324                           &cur_es->rx_unicast);
1325
1326         ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
1327                           vsi->stat_offsets_loaded, &prev_es->rx_multicast,
1328                           &cur_es->rx_multicast);
1329
1330         ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
1331                           vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
1332                           &cur_es->rx_broadcast);
1333
1334         ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1335                           &prev_es->rx_discards, &cur_es->rx_discards);
1336
1337         ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
1338                           vsi->stat_offsets_loaded, &prev_es->tx_bytes,
1339                           &cur_es->tx_bytes);
1340
1341         ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
1342                           vsi->stat_offsets_loaded, &prev_es->tx_unicast,
1343                           &cur_es->tx_unicast);
1344
1345         ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
1346                           vsi->stat_offsets_loaded, &prev_es->tx_multicast,
1347                           &cur_es->tx_multicast);
1348
1349         ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
1350                           vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
1351                           &cur_es->tx_broadcast);
1352
1353         ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1354                           &prev_es->tx_errors, &cur_es->tx_errors);
1355
1356         vsi->stat_offsets_loaded = true;
1357 }
1358
1359 /**
1360  * ice_free_fltr_list - free filter lists helper
1361  * @dev: pointer to the device struct
1362  * @h: pointer to the list head to be freed
1363  *
1364  * Helper function to free filter lists previously created using
1365  * ice_add_mac_to_list
1366  */
1367 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1368 {
1369         struct ice_fltr_list_entry *e, *tmp;
1370
1371         list_for_each_entry_safe(e, tmp, h, list_entry) {
1372                 list_del(&e->list_entry);
1373                 devm_kfree(dev, e);
1374         }
1375 }
1376
1377 /**
1378  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1379  * @vsi: the VSI being configured
1380  * @vid: VLAN id to be added
1381  */
1382 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1383 {
1384         struct ice_fltr_list_entry *tmp;
1385         struct ice_pf *pf = vsi->back;
1386         LIST_HEAD(tmp_add_list);
1387         enum ice_status status;
1388         int err = 0;
1389
1390         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
1391         if (!tmp)
1392                 return -ENOMEM;
1393
1394         tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1395         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1396         tmp->fltr_info.flag = ICE_FLTR_TX;
1397         tmp->fltr_info.src = vsi->vsi_num;
1398         tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
1399         tmp->fltr_info.l_data.vlan.vlan_id = vid;
1400
1401         INIT_LIST_HEAD(&tmp->list_entry);
1402         list_add(&tmp->list_entry, &tmp_add_list);
1403
1404         status = ice_add_vlan(&pf->hw, &tmp_add_list);
1405         if (status) {
1406                 err = -ENODEV;
1407                 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
1408                         vid, vsi->vsi_num);
1409         }
1410
1411         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1412         return err;
1413 }
1414
1415 /**
1416  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1417  * @vsi: the VSI being configured
1418  * @vid: VLAN id to be removed
1419  *
1420  * Returns 0 on success and negative on failure
1421  */
1422 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1423 {
1424         struct ice_fltr_list_entry *list;
1425         struct ice_pf *pf = vsi->back;
1426         LIST_HEAD(tmp_add_list);
1427         int status = 0;
1428
1429         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1430         if (!list)
1431                 return -ENOMEM;
1432
1433         list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1434         list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
1435         list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1436         list->fltr_info.l_data.vlan.vlan_id = vid;
1437         list->fltr_info.flag = ICE_FLTR_TX;
1438         list->fltr_info.src = vsi->vsi_num;
1439
1440         INIT_LIST_HEAD(&list->list_entry);
1441         list_add(&list->list_entry, &tmp_add_list);
1442
1443         if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
1444                 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
1445                         vid, vsi->vsi_num);
1446                 status = -EIO;
1447         }
1448
1449         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1450         return status;
1451 }
1452
1453 /**
1454  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1455  * @vsi: the VSI being configured
1456  *
1457  * Return 0 on success and a negative value on error
1458  * Configure the Rx VSI for operation.
1459  */
1460 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1461 {
1462         int err = 0;
1463         u16 i;
1464
1465         if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
1466                 vsi->max_frame = vsi->netdev->mtu +
1467                         ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1468         else
1469                 vsi->max_frame = ICE_RXBUF_2048;
1470
1471         vsi->rx_buf_len = ICE_RXBUF_2048;
1472         /* set up individual rings */
1473         for (i = 0; i < vsi->num_rxq && !err; i++)
1474                 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1475
1476         if (err) {
1477                 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
1478                 return -EIO;
1479         }
1480         return err;
1481 }
1482
1483 /**
1484  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1485  * @vsi: the VSI being configured
1486  *
1487  * Return 0 on success and a negative value on error
1488  * Configure the Tx VSI for operation.
1489  */
1490 int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
1491 {
1492         struct ice_aqc_add_tx_qgrp *qg_buf;
1493         struct ice_aqc_add_txqs_perq *txq;
1494         struct ice_pf *pf = vsi->back;
1495         enum ice_status status;
1496         u16 buf_len, i, pf_q;
1497         int err = 0, tc = 0;
1498         u8 num_q_grps;
1499
1500         buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
1501         qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
1502         if (!qg_buf)
1503                 return -ENOMEM;
1504
1505         if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
1506                 err = -EINVAL;
1507                 goto err_cfg_txqs;
1508         }
1509         qg_buf->num_txqs = 1;
1510         num_q_grps = 1;
1511
1512         /* set up and configure the Tx queues */
1513         ice_for_each_txq(vsi, i) {
1514                 struct ice_tlan_ctx tlan_ctx = { 0 };
1515
1516                 pf_q = vsi->txq_map[i];
1517                 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
1518                 /* copy context contents into the qg_buf */
1519                 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
1520                 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
1521                             ice_tlan_ctx_info);
1522
1523                 /* init queue specific tail reg. It is referred as transmit
1524                  * comm scheduler queue doorbell.
1525                  */
1526                 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
1527                 status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
1528                                          num_q_grps, qg_buf, buf_len, NULL);
1529                 if (status) {
1530                         dev_err(&vsi->back->pdev->dev,
1531                                 "Failed to set LAN Tx queue context, error: %d\n",
1532                                 status);
1533                         err = -ENODEV;
1534                         goto err_cfg_txqs;
1535                 }
1536
1537                 /* Add Tx Queue TEID into the VSI Tx ring from the response
1538                  * This will complete configuring and enabling the queue.
1539                  */
1540                 txq = &qg_buf->txqs[0];
1541                 if (pf_q == le16_to_cpu(txq->txq_id))
1542                         vsi->tx_rings[i]->txq_teid =
1543                                 le32_to_cpu(txq->q_teid);
1544         }
1545 err_cfg_txqs:
1546         devm_kfree(&pf->pdev->dev, qg_buf);
1547         return err;
1548 }
1549
1550 /**
1551  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1552  * @vsi: the VSI being configured
1553  */
1554 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1555 {
1556         struct ice_pf *pf = vsi->back;
1557         u16 vector = vsi->base_vector;
1558         struct ice_hw *hw = &pf->hw;
1559         u32 txq = 0, rxq = 0;
1560         int i, q, itr;
1561         u8 itr_gran;
1562
1563         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1564                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1565
1566                 itr_gran = hw->itr_gran_200;
1567
1568                 if (q_vector->num_ring_rx) {
1569                         q_vector->rx.itr =
1570                                 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1571                                            itr_gran);
1572                         q_vector->rx.latency_range = ICE_LOW_LATENCY;
1573                 }
1574
1575                 if (q_vector->num_ring_tx) {
1576                         q_vector->tx.itr =
1577                                 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1578                                            itr_gran);
1579                         q_vector->tx.latency_range = ICE_LOW_LATENCY;
1580                 }
1581                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1582                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1583
1584                 /* Both Transmit Queue Interrupt Cause Control register
1585                  * and Receive Queue Interrupt Cause control register
1586                  * expects MSIX_INDX field to be the vector index
1587                  * within the function space and not the absolute
1588                  * vector index across PF or across device.
1589                  * For SR-IOV VF VSIs queue vector index always starts
1590                  * with 1 since first vector index(0) is used for OICR
1591                  * in VF space. Since VMDq and other PF VSIs are within
1592                  * the PF function space, use the vector index that is
1593                  * tracked for this PF.
1594                  */
1595                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1596                         u32 val;
1597
1598                         itr = ICE_ITR_NONE;
1599                         val = QINT_TQCTL_CAUSE_ENA_M |
1600                               (itr << QINT_TQCTL_ITR_INDX_S)  |
1601                               (vector << QINT_TQCTL_MSIX_INDX_S);
1602                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1603                         txq++;
1604                 }
1605
1606                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1607                         u32 val;
1608
1609                         itr = ICE_ITR_NONE;
1610                         val = QINT_RQCTL_CAUSE_ENA_M |
1611                               (itr << QINT_RQCTL_ITR_INDX_S)  |
1612                               (vector << QINT_RQCTL_MSIX_INDX_S);
1613                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1614                         rxq++;
1615                 }
1616         }
1617
1618         ice_flush(hw);
1619 }
1620
1621 /**
1622  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1623  * @vsi: the VSI being changed
1624  */
1625 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1626 {
1627         struct device *dev = &vsi->back->pdev->dev;
1628         struct ice_hw *hw = &vsi->back->hw;
1629         struct ice_vsi_ctx ctxt = { 0 };
1630         enum ice_status status;
1631
1632         /* Here we are configuring the VSI to let the driver add VLAN tags by
1633          * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1634          * insertion happens in the Tx hot path, in ice_tx_map.
1635          */
1636         ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1637
1638         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1639         ctxt.vsi_num = vsi->vsi_num;
1640
1641         status = ice_aq_update_vsi(hw, &ctxt, NULL);
1642         if (status) {
1643                 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1644                         status, hw->adminq.sq_last_status);
1645                 return -EIO;
1646         }
1647
1648         vsi->info.vlan_flags = ctxt.info.vlan_flags;
1649         return 0;
1650 }
1651
1652 /**
1653  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1654  * @vsi: the VSI being changed
1655  * @ena: boolean value indicating if this is a enable or disable request
1656  */
1657 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1658 {
1659         struct device *dev = &vsi->back->pdev->dev;
1660         struct ice_hw *hw = &vsi->back->hw;
1661         struct ice_vsi_ctx ctxt = { 0 };
1662         enum ice_status status;
1663
1664         /* Here we are configuring what the VSI should do with the VLAN tag in
1665          * the Rx packet. We can either leave the tag in the packet or put it in
1666          * the Rx descriptor.
1667          */
1668         if (ena) {
1669                 /* Strip VLAN tag from Rx packet and put it in the desc */
1670                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1671         } else {
1672                 /* Disable stripping. Leave tag in packet */
1673                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1674         }
1675
1676         /* Allow all packets untagged/tagged */
1677         ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1678
1679         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1680         ctxt.vsi_num = vsi->vsi_num;
1681
1682         status = ice_aq_update_vsi(hw, &ctxt, NULL);
1683         if (status) {
1684                 dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1685                         ena, status, hw->adminq.sq_last_status);
1686                 return -EIO;
1687         }
1688
1689         vsi->info.vlan_flags = ctxt.info.vlan_flags;
1690         return 0;
1691 }
1692
1693 /**
1694  * ice_vsi_start_rx_rings - start VSI's Rx rings
1695  * @vsi: the VSI whose rings are to be started
1696  *
1697  * Returns 0 on success and a negative value on error
1698  */
1699 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1700 {
1701         return ice_vsi_ctrl_rx_rings(vsi, true);
1702 }
1703
1704 /**
1705  * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1706  * @vsi: the VSI
1707  *
1708  * Returns 0 on success and a negative value on error
1709  */
1710 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1711 {
1712         return ice_vsi_ctrl_rx_rings(vsi, false);
1713 }
1714
1715 /**
1716  * ice_vsi_stop_tx_rings - Disable Tx rings
1717  * @vsi: the VSI being configured
1718  */
1719 int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
1720 {
1721         struct ice_pf *pf = vsi->back;
1722         struct ice_hw *hw = &pf->hw;
1723         enum ice_status status;
1724         u32 *q_teids, val;
1725         u16 *q_ids, i;
1726         int err = 0;
1727
1728         if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1729                 return -EINVAL;
1730
1731         q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
1732                                GFP_KERNEL);
1733         if (!q_teids)
1734                 return -ENOMEM;
1735
1736         q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
1737                              GFP_KERNEL);
1738         if (!q_ids) {
1739                 err = -ENOMEM;
1740                 goto err_alloc_q_ids;
1741         }
1742
1743         /* set up the Tx queue list to be disabled */
1744         ice_for_each_txq(vsi, i) {
1745                 u16 v_idx;
1746
1747                 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
1748                         err = -EINVAL;
1749                         goto err_out;
1750                 }
1751
1752                 q_ids[i] = vsi->txq_map[i];
1753                 q_teids[i] = vsi->tx_rings[i]->txq_teid;
1754
1755                 /* clear cause_ena bit for disabled queues */
1756                 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
1757                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1758                 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
1759
1760                 /* software is expected to wait for 100 ns */
1761                 ndelay(100);
1762
1763                 /* trigger a software interrupt for the vector associated to
1764                  * the queue to schedule NAPI handler
1765                  */
1766                 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
1767                 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
1768                      GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
1769         }
1770         status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
1771                                  NULL);
1772         /* if the disable queue command was exercised during an active reset
1773          * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
1774          * the reset operation disables queues at the hardware level anyway.
1775          */
1776         if (status == ICE_ERR_RESET_ONGOING) {
1777                 dev_info(&pf->pdev->dev,
1778                          "Reset in progress. LAN Tx queues already disabled\n");
1779         } else if (status) {
1780                 dev_err(&pf->pdev->dev,
1781                         "Failed to disable LAN Tx queues, error: %d\n",
1782                         status);
1783                 err = -ENODEV;
1784         }
1785
1786 err_out:
1787         devm_kfree(&pf->pdev->dev, q_ids);
1788
1789 err_alloc_q_ids:
1790         devm_kfree(&pf->pdev->dev, q_teids);
1791
1792         return err;
1793 }
1794
1795 /**
1796  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1797  * @vsi: VSI to enable or disable VLAN pruning on
1798  * @ena: set to true to enable VLAN pruning and false to disable it
1799  *
1800  * returns 0 if VSI is updated, negative otherwise
1801  */
1802 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
1803 {
1804         struct ice_vsi_ctx *ctxt;
1805         struct device *dev;
1806         int status;
1807
1808         if (!vsi)
1809                 return -EINVAL;
1810
1811         dev = &vsi->back->pdev->dev;
1812         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
1813         if (!ctxt)
1814                 return -ENOMEM;
1815
1816         ctxt->info = vsi->info;
1817
1818         if (ena) {
1819                 ctxt->info.sec_flags |=
1820                         ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1821                         ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
1822                 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1823         } else {
1824                 ctxt->info.sec_flags &=
1825                         ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1826                           ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1827                 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1828         }
1829
1830         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
1831                                                 ICE_AQ_VSI_PROP_SW_VALID);
1832         ctxt->vsi_num = vsi->vsi_num;
1833         status = ice_aq_update_vsi(&vsi->back->hw, ctxt, NULL);
1834         if (status) {
1835                 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI %d failed, err = %d, aq_err = %d\n",
1836                            ena ? "Ena" : "Dis", vsi->vsi_num, status,
1837                            vsi->back->hw.adminq.sq_last_status);
1838                 goto err_out;
1839         }
1840
1841         vsi->info.sec_flags = ctxt->info.sec_flags;
1842         vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1843
1844         devm_kfree(dev, ctxt);
1845         return 0;
1846
1847 err_out:
1848         devm_kfree(dev, ctxt);
1849         return -EIO;
1850 }
1851
1852 /**
1853  * ice_vsi_setup - Set up a VSI by a given type
1854  * @pf: board private structure
1855  * @pi: pointer to the port_info instance
1856  * @type: VSI type
1857  * @vf_id: defines VF id to which this VSI connects. This field is meant to be
1858  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
1859  *         fill-in ICE_INVAL_VFID as input.
1860  *
1861  * This allocates the sw VSI structure and its queue resources.
1862  *
1863  * Returns pointer to the successfully allocated and configured VSI sw struct on
1864  * success, NULL on failure.
1865  */
1866 struct ice_vsi *
1867 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1868               enum ice_vsi_type type, u16 __always_unused vf_id)
1869 {
1870         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1871         struct device *dev = &pf->pdev->dev;
1872         struct ice_vsi *vsi;
1873         int ret, i;
1874
1875         vsi = ice_vsi_alloc(pf, type);
1876         if (!vsi) {
1877                 dev_err(dev, "could not allocate VSI\n");
1878                 return NULL;
1879         }
1880
1881         vsi->port_info = pi;
1882         vsi->vsw = pf->first_sw;
1883
1884         if (ice_vsi_get_qs(vsi)) {
1885                 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1886                         vsi->idx);
1887                 goto unroll_get_qs;
1888         }
1889
1890         /* set RSS capabilities */
1891         ice_vsi_set_rss_params(vsi);
1892
1893         /* create the VSI */
1894         ret = ice_vsi_init(vsi);
1895         if (ret)
1896                 goto unroll_get_qs;
1897
1898         switch (vsi->type) {
1899         case ICE_VSI_PF:
1900                 ret = ice_vsi_alloc_q_vectors(vsi);
1901                 if (ret)
1902                         goto unroll_vsi_init;
1903
1904                 ret = ice_vsi_setup_vector_base(vsi);
1905                 if (ret)
1906                         goto unroll_alloc_q_vector;
1907
1908                 ret = ice_vsi_alloc_rings(vsi);
1909                 if (ret)
1910                         goto unroll_vector_base;
1911
1912                 ice_vsi_map_rings_to_vectors(vsi);
1913
1914                 /* Do not exit if configuring RSS had an issue, at least
1915                  * receive traffic on first queue. Hence no need to capture
1916                  * return value
1917                  */
1918                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1919                         ice_vsi_cfg_rss_lut_key(vsi);
1920                 break;
1921         default:
1922                 /* if VSI type is not recognized, clean up the resources and
1923                  * exit
1924                  */
1925                 goto unroll_vsi_init;
1926         }
1927
1928         ice_vsi_set_tc_cfg(vsi);
1929
1930         /* configure VSI nodes based on number of queues and TC's */
1931         for (i = 0; i < vsi->tc_cfg.numtc; i++)
1932                 max_txqs[i] = vsi->num_txq;
1933
1934         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
1935                               vsi->tc_cfg.ena_tc, max_txqs);
1936         if (ret) {
1937                 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
1938                 goto unroll_vector_base;
1939         }
1940
1941         return vsi;
1942
1943 unroll_vector_base:
1944         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
1945 unroll_alloc_q_vector:
1946         ice_vsi_free_q_vectors(vsi);
1947 unroll_vsi_init:
1948         ice_vsi_delete(vsi);
1949 unroll_get_qs:
1950         ice_vsi_put_qs(vsi);
1951         pf->q_left_tx += vsi->alloc_txq;
1952         pf->q_left_rx += vsi->alloc_rxq;
1953         ice_vsi_clear(vsi);
1954
1955         return NULL;
1956 }
1957
1958 /**
1959  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1960  * @vsi: the VSI being cleaned up
1961  */
1962 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1963 {
1964         struct ice_pf *pf = vsi->back;
1965         u16 vector = vsi->base_vector;
1966         struct ice_hw *hw = &pf->hw;
1967         u32 txq = 0;
1968         u32 rxq = 0;
1969         int i, q;
1970
1971         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1972                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1973
1974                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1975                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1976                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1977                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1978                         txq++;
1979                 }
1980
1981                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1982                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1983                         rxq++;
1984                 }
1985         }
1986
1987         ice_flush(hw);
1988 }
1989
1990 /**
1991  * ice_vsi_free_irq - Free the IRQ association with the OS
1992  * @vsi: the VSI being configured
1993  */
1994 void ice_vsi_free_irq(struct ice_vsi *vsi)
1995 {
1996         struct ice_pf *pf = vsi->back;
1997         int base = vsi->base_vector;
1998
1999         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2000                 int i;
2001
2002                 if (!vsi->q_vectors || !vsi->irqs_ready)
2003                         return;
2004
2005                 vsi->irqs_ready = false;
2006                 for (i = 0; i < vsi->num_q_vectors; i++) {
2007                         u16 vector = i + base;
2008                         int irq_num;
2009
2010                         irq_num = pf->msix_entries[vector].vector;
2011
2012                         /* free only the irqs that were actually requested */
2013                         if (!vsi->q_vectors[i] ||
2014                             !(vsi->q_vectors[i]->num_ring_tx ||
2015                               vsi->q_vectors[i]->num_ring_rx))
2016                                 continue;
2017
2018                         /* clear the affinity notifier in the IRQ descriptor */
2019                         irq_set_affinity_notifier(irq_num, NULL);
2020
2021                         /* clear the affinity_mask in the IRQ descriptor */
2022                         irq_set_affinity_hint(irq_num, NULL);
2023                         synchronize_irq(irq_num);
2024                         devm_free_irq(&pf->pdev->dev, irq_num,
2025                                       vsi->q_vectors[i]);
2026                 }
2027                 ice_vsi_release_msix(vsi);
2028         }
2029 }
2030
2031 /**
2032  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2033  * @vsi: the VSI having resources freed
2034  */
2035 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2036 {
2037         int i;
2038
2039         if (!vsi->tx_rings)
2040                 return;
2041
2042         ice_for_each_txq(vsi, i)
2043                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2044                         ice_free_tx_ring(vsi->tx_rings[i]);
2045 }
2046
2047 /**
2048  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2049  * @vsi: the VSI having resources freed
2050  */
2051 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2052 {
2053         int i;
2054
2055         if (!vsi->rx_rings)
2056                 return;
2057
2058         ice_for_each_rxq(vsi, i)
2059                 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2060                         ice_free_rx_ring(vsi->rx_rings[i]);
2061 }
2062
2063 /**
2064  * ice_vsi_close - Shut down a VSI
2065  * @vsi: the VSI being shut down
2066  */
2067 void ice_vsi_close(struct ice_vsi *vsi)
2068 {
2069         if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2070                 ice_down(vsi);
2071
2072         ice_vsi_free_irq(vsi);
2073         ice_vsi_free_tx_rings(vsi);
2074         ice_vsi_free_rx_rings(vsi);
2075 }
2076
2077 /**
2078  * ice_free_res - free a block of resources
2079  * @res: pointer to the resource
2080  * @index: starting index previously returned by ice_get_res
2081  * @id: identifier to track owner
2082  *
2083  * Returns number of resources freed
2084  */
2085 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2086 {
2087         int count = 0;
2088         int i;
2089
2090         if (!res || index >= res->num_entries)
2091                 return -EINVAL;
2092
2093         id |= ICE_RES_VALID_BIT;
2094         for (i = index; i < res->num_entries && res->list[i] == id; i++) {
2095                 res->list[i] = 0;
2096                 count++;
2097         }
2098
2099         return count;
2100 }
2101
2102 /**
2103  * ice_search_res - Search the tracker for a block of resources
2104  * @res: pointer to the resource
2105  * @needed: size of the block needed
2106  * @id: identifier to track owner
2107  *
2108  * Returns the base item index of the block, or -ENOMEM for error
2109  */
2110 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2111 {
2112         int start = res->search_hint;
2113         int end = start;
2114
2115         id |= ICE_RES_VALID_BIT;
2116
2117         do {
2118                 /* skip already allocated entries */
2119                 if (res->list[end++] & ICE_RES_VALID_BIT) {
2120                         start = end;
2121                         if ((start + needed) > res->num_entries)
2122                                 break;
2123                 }
2124
2125                 if (end == (start + needed)) {
2126                         int i = start;
2127
2128                         /* there was enough, so assign it to the requestor */
2129                         while (i != end)
2130                                 res->list[i++] = id;
2131
2132                         if (end == res->num_entries)
2133                                 end = 0;
2134
2135                         res->search_hint = end;
2136                         return start;
2137                 }
2138         } while (1);
2139
2140         return -ENOMEM;
2141 }
2142
2143 /**
2144  * ice_get_res - get a block of resources
2145  * @pf: board private structure
2146  * @res: pointer to the resource
2147  * @needed: size of the block needed
2148  * @id: identifier to track owner
2149  *
2150  * Returns the base item index of the block, or -ENOMEM for error
2151  * The search_hint trick and lack of advanced fit-finding only works
2152  * because we're highly likely to have all the same sized requests.
2153  * Linear search time and any fragmentation should be minimal.
2154  */
2155 int
2156 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2157 {
2158         int ret;
2159
2160         if (!res || !pf)
2161                 return -EINVAL;
2162
2163         if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2164                 dev_err(&pf->pdev->dev,
2165                         "param err: needed=%d, num_entries = %d id=0x%04x\n",
2166                         needed, res->num_entries, id);
2167                 return -EINVAL;
2168         }
2169
2170         /* search based on search_hint */
2171         ret = ice_search_res(res, needed, id);
2172
2173         if (ret < 0) {
2174                 /* previous search failed. Reset search hint and try again */
2175                 res->search_hint = 0;
2176                 ret = ice_search_res(res, needed, id);
2177         }
2178
2179         return ret;
2180 }
2181
2182 /**
2183  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2184  * @vsi: the VSI being un-configured
2185  */
2186 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2187 {
2188         struct ice_pf *pf = vsi->back;
2189         struct ice_hw *hw = &pf->hw;
2190         int base = vsi->base_vector;
2191         u32 val;
2192         int i;
2193
2194         /* disable interrupt causation from each queue */
2195         if (vsi->tx_rings) {
2196                 ice_for_each_txq(vsi, i) {
2197                         if (vsi->tx_rings[i]) {
2198                                 u16 reg;
2199
2200                                 reg = vsi->tx_rings[i]->reg_idx;
2201                                 val = rd32(hw, QINT_TQCTL(reg));
2202                                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2203                                 wr32(hw, QINT_TQCTL(reg), val);
2204                         }
2205                 }
2206         }
2207
2208         if (vsi->rx_rings) {
2209                 ice_for_each_rxq(vsi, i) {
2210                         if (vsi->rx_rings[i]) {
2211                                 u16 reg;
2212
2213                                 reg = vsi->rx_rings[i]->reg_idx;
2214                                 val = rd32(hw, QINT_RQCTL(reg));
2215                                 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2216                                 wr32(hw, QINT_RQCTL(reg), val);
2217                         }
2218                 }
2219         }
2220
2221         /* disable each interrupt */
2222         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2223                 for (i = vsi->base_vector;
2224                      i < (vsi->num_q_vectors + vsi->base_vector); i++)
2225                         wr32(hw, GLINT_DYN_CTL(i), 0);
2226
2227                 ice_flush(hw);
2228                 for (i = 0; i < vsi->num_q_vectors; i++)
2229                         synchronize_irq(pf->msix_entries[i + base].vector);
2230         }
2231 }
2232
2233 /**
2234  * ice_vsi_release - Delete a VSI and free its resources
2235  * @vsi: the VSI being removed
2236  *
2237  * Returns 0 on success or < 0 on error
2238  */
2239 int ice_vsi_release(struct ice_vsi *vsi)
2240 {
2241         struct ice_pf *pf;
2242
2243         if (!vsi->back)
2244                 return -ENODEV;
2245         pf = vsi->back;
2246         /* do not unregister and free netdevs while driver is in the reset
2247          * recovery pending state. Since reset/rebuild happens through PF
2248          * service task workqueue, its not a good idea to unregister netdev
2249          * that is associated to the PF that is running the work queue items
2250          * currently. This is done to avoid check_flush_dependency() warning
2251          * on this wq
2252          */
2253         if (vsi->netdev && !ice_is_reset_recovery_pending(pf->state)) {
2254                 unregister_netdev(vsi->netdev);
2255                 free_netdev(vsi->netdev);
2256                 vsi->netdev = NULL;
2257         }
2258
2259         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2260                 ice_rss_clean(vsi);
2261
2262         /* Disable VSI and free resources */
2263         ice_vsi_dis_irq(vsi);
2264         ice_vsi_close(vsi);
2265
2266         /* reclaim interrupt vectors back to PF */
2267         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2268         pf->num_avail_msix += vsi->num_q_vectors;
2269
2270         ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
2271         ice_vsi_delete(vsi);
2272         ice_vsi_free_q_vectors(vsi);
2273         ice_vsi_clear_rings(vsi);
2274
2275         ice_vsi_put_qs(vsi);
2276         pf->q_left_tx += vsi->alloc_txq;
2277         pf->q_left_rx += vsi->alloc_rxq;
2278
2279         /* retain SW VSI data structure since it is needed to unregister and
2280          * free VSI netdev when PF is not in reset recovery pending state,\
2281          * for ex: during rmmod.
2282          */
2283         if (!ice_is_reset_recovery_pending(pf->state))
2284                 ice_vsi_clear(vsi);
2285
2286         return 0;
2287 }
2288
2289 /**
2290  * ice_vsi_rebuild - Rebuild VSI after reset
2291  * @vsi: VSI to be rebuild
2292  *
2293  * Returns 0 on success and negative value on failure
2294  */
2295 int ice_vsi_rebuild(struct ice_vsi *vsi)
2296 {
2297         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2298         int ret, i;
2299
2300         if (!vsi)
2301                 return -EINVAL;
2302
2303         ice_vsi_free_q_vectors(vsi);
2304         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2305         vsi->base_vector = 0;
2306         ice_vsi_clear_rings(vsi);
2307         ice_vsi_free_arrays(vsi, false);
2308         ice_vsi_set_num_qs(vsi);
2309
2310         /* Initialize VSI struct elements and create VSI in FW */
2311         ret = ice_vsi_init(vsi);
2312         if (ret < 0)
2313                 goto err_vsi;
2314
2315         ret = ice_vsi_alloc_arrays(vsi, false);
2316         if (ret < 0)
2317                 goto err_vsi;
2318
2319         switch (vsi->type) {
2320         case ICE_VSI_PF:
2321                 ret = ice_vsi_alloc_q_vectors(vsi);
2322                 if (ret)
2323                         goto err_rings;
2324
2325                 ret = ice_vsi_setup_vector_base(vsi);
2326                 if (ret)
2327                         goto err_vectors;
2328
2329                 ret = ice_vsi_alloc_rings(vsi);
2330                 if (ret)
2331                         goto err_vectors;
2332
2333                 ice_vsi_map_rings_to_vectors(vsi);
2334                 break;
2335         default:
2336                 break;
2337         }
2338
2339         ice_vsi_set_tc_cfg(vsi);
2340
2341         /* configure VSI nodes based on number of queues and TC's */
2342         for (i = 0; i < vsi->tc_cfg.numtc; i++)
2343                 max_txqs[i] = vsi->num_txq;
2344
2345         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2346                               vsi->tc_cfg.ena_tc, max_txqs);
2347         if (ret) {
2348                 dev_info(&vsi->back->pdev->dev,
2349                          "Failed VSI lan queue config\n");
2350                 goto err_vectors;
2351         }
2352         return 0;
2353
2354 err_vectors:
2355         ice_vsi_free_q_vectors(vsi);
2356 err_rings:
2357         if (vsi->netdev) {
2358                 vsi->current_netdev_flags = 0;
2359                 unregister_netdev(vsi->netdev);
2360                 free_netdev(vsi->netdev);
2361                 vsi->netdev = NULL;
2362         }
2363 err_vsi:
2364         ice_vsi_clear(vsi);
2365         set_bit(__ICE_RESET_FAILED, vsi->back->state);
2366         return ret;
2367 }
2368
2369 /**
2370  * ice_is_reset_recovery_pending - schedule a reset
2371  * @state: pf state field
2372  */
2373 bool ice_is_reset_recovery_pending(unsigned long *state)
2374 {
2375         return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
2376 }