net/bnxt: remove redundant header file inclusion
[dpdk.git] / drivers / net / bnxt / bnxt_rxq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_malloc.h>
9
10 #include "bnxt.h"
11 #include "bnxt_filter.h"
12 #include "bnxt_hwrm.h"
13 #include "bnxt_ring.h"
14 #include "bnxt_rxq.h"
15 #include "bnxt_rxr.h"
16 #include "bnxt_vnic.h"
17 #include "hsi_struct_def_dpdk.h"
18
19 /*
20  * RX Queues
21  */
22
23 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
24 {
25         if (rxq && rxq->cp_ring && rxq->cp_ring->hw_stats)
26                 rxq->cp_ring->hw_stats = NULL;
27 }
28
29 int bnxt_mq_rx_configure(struct bnxt *bp)
30 {
31         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
32         const struct rte_eth_vmdq_rx_conf *conf =
33                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
34         unsigned int i, j, nb_q_per_grp = 1, ring_idx = 0;
35         int start_grp_id, end_grp_id = 1, rc = 0;
36         struct bnxt_vnic_info *vnic;
37         struct bnxt_filter_info *filter;
38         enum rte_eth_nb_pools pools = bp->rx_cp_nr_rings, max_pools = 0;
39         struct bnxt_rx_queue *rxq;
40
41         bp->nr_vnics = 0;
42
43         /* Single queue mode */
44         if (bp->rx_cp_nr_rings < 2) {
45                 vnic = &bp->vnic_info[0];
46                 if (!vnic) {
47                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
48                         rc = -ENOMEM;
49                         goto err_out;
50                 }
51                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
52                 bp->nr_vnics++;
53
54                 rxq = bp->eth_dev->data->rx_queues[0];
55                 rxq->vnic = vnic;
56
57                 vnic->func_default = true;
58                 vnic->start_grp_id = 0;
59                 vnic->end_grp_id = vnic->start_grp_id;
60                 filter = bnxt_alloc_filter(bp);
61                 if (!filter) {
62                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
63                         rc = -ENOMEM;
64                         goto err_out;
65                 }
66                 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
67                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
68                 goto out;
69         }
70
71         /* Multi-queue mode */
72         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
73                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
74
75                 switch (dev_conf->rxmode.mq_mode) {
76                 case ETH_MQ_RX_VMDQ_RSS:
77                 case ETH_MQ_RX_VMDQ_ONLY:
78                 case ETH_MQ_RX_VMDQ_DCB_RSS:
79                         /* FALLTHROUGH */
80                         /* ETH_8/64_POOLs */
81                         pools = conf->nb_queue_pools;
82                         /* For each pool, allocate MACVLAN CFA rule & VNIC */
83                         max_pools = RTE_MIN(bp->max_vnics,
84                                             RTE_MIN(bp->max_l2_ctx,
85                                             RTE_MIN(bp->max_rsscos_ctx,
86                                                     ETH_64_POOLS)));
87                         PMD_DRV_LOG(DEBUG,
88                                     "pools = %u max_pools = %u\n",
89                                     pools, max_pools);
90                         if (pools > max_pools)
91                                 pools = max_pools;
92                         break;
93                 case ETH_MQ_RX_RSS:
94                         pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : 1;
95                         break;
96                 default:
97                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
98                                 dev_conf->rxmode.mq_mode);
99                         rc = -EINVAL;
100                         goto err_out;
101                 }
102         }
103         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
104         bp->rx_num_qs_per_vnic = nb_q_per_grp;
105         PMD_DRV_LOG(DEBUG, "pools = %u nb_q_per_grp = %u\n",
106                     pools, nb_q_per_grp);
107         start_grp_id = 0;
108         end_grp_id = nb_q_per_grp;
109
110         for (i = 0; i < pools; i++) {
111                 vnic = &bp->vnic_info[i];
112                 if (!vnic) {
113                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
114                         rc = -ENOMEM;
115                         goto err_out;
116                 }
117                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
118                 bp->nr_vnics++;
119
120                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
121                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
122                         rxq->vnic = vnic;
123                         PMD_DRV_LOG(DEBUG,
124                                     "rxq[%d] = %p vnic[%d] = %p\n",
125                                     ring_idx, rxq, i, vnic);
126                 }
127                 if (i == 0) {
128                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
129                                 bp->eth_dev->data->promiscuous = 1;
130                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
131                         }
132                         vnic->func_default = true;
133                 }
134                 vnic->start_grp_id = start_grp_id;
135                 vnic->end_grp_id = end_grp_id;
136
137                 if (i) {
138                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
139                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
140                                 vnic->rss_dflt_cr = true;
141                         goto skip_filter_allocation;
142                 }
143                 filter = bnxt_alloc_filter(bp);
144                 if (!filter) {
145                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
146                         rc = -ENOMEM;
147                         goto err_out;
148                 }
149                 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
150                 /*
151                  * TODO: Configure & associate CFA rule for
152                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
153                  */
154                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
155
156 skip_filter_allocation:
157                 start_grp_id = end_grp_id;
158                 end_grp_id += nb_q_per_grp;
159         }
160
161 out:
162         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
163                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
164
165                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
166                         rss = &bp->rss_conf;
167                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
168                 }
169
170                 for (i = 0; i < bp->nr_vnics; i++) {
171                         vnic = &bp->vnic_info[i];
172                         vnic->hash_type =
173                                 bnxt_rte_to_hwrm_hash_types(rss->rss_hf);
174
175                         /*
176                          * Use the supplied key if the key length is
177                          * acceptable and the rss_key is not NULL
178                          */
179                         if (rss->rss_key &&
180                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
181                                 memcpy(vnic->rss_hash_key,
182                                        rss->rss_key, rss->rss_key_len);
183                 }
184         }
185
186         return rc;
187
188 err_out:
189         /* Free allocated vnic/filters */
190
191         return rc;
192 }
193
194 void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
195 {
196         struct bnxt_sw_rx_bd *sw_ring;
197         struct bnxt_tpa_info *tpa_info;
198         uint16_t i;
199
200         if (!rxq)
201                 return;
202
203         rte_spinlock_lock(&rxq->lock);
204
205         sw_ring = rxq->rx_ring->rx_buf_ring;
206         if (sw_ring) {
207                 for (i = 0;
208                      i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
209                         if (sw_ring[i].mbuf) {
210                                 rte_pktmbuf_free_seg(sw_ring[i].mbuf);
211                                 sw_ring[i].mbuf = NULL;
212                         }
213                 }
214         }
215         /* Free up mbufs in Agg ring */
216         sw_ring = rxq->rx_ring->ag_buf_ring;
217         if (sw_ring) {
218                 for (i = 0;
219                      i < rxq->rx_ring->ag_ring_struct->ring_size; i++) {
220                         if (sw_ring[i].mbuf) {
221                                 rte_pktmbuf_free_seg(sw_ring[i].mbuf);
222                                 sw_ring[i].mbuf = NULL;
223                         }
224                 }
225         }
226
227         /* Free up mbufs in TPA */
228         tpa_info = rxq->rx_ring->tpa_info;
229         if (tpa_info) {
230                 int max_aggs = BNXT_TPA_MAX_AGGS(rxq->bp);
231
232                 for (i = 0; i < max_aggs; i++) {
233                         if (tpa_info[i].mbuf) {
234                                 rte_pktmbuf_free_seg(tpa_info[i].mbuf);
235                                 tpa_info[i].mbuf = NULL;
236                         }
237                 }
238         }
239
240         rte_spinlock_unlock(&rxq->lock);
241 }
242
243 void bnxt_free_rx_mbufs(struct bnxt *bp)
244 {
245         struct bnxt_rx_queue *rxq;
246         int i;
247
248         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
249                 rxq = bp->rx_queues[i];
250                 bnxt_rx_queue_release_mbufs(rxq);
251         }
252 }
253
254 void bnxt_rx_queue_release_op(void *rx_queue)
255 {
256         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
257
258         if (rxq) {
259                 if (is_bnxt_in_error(rxq->bp))
260                         return;
261
262                 bnxt_rx_queue_release_mbufs(rxq);
263
264                 /* Free RX ring hardware descriptors */
265                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
266                 /* Free RX Agg ring hardware descriptors */
267                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
268
269                 /* Free RX completion ring hardware descriptors */
270                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
271
272                 bnxt_free_rxq_stats(rxq);
273                 rte_memzone_free(rxq->mz);
274                 rxq->mz = NULL;
275
276                 rte_free(rxq);
277         }
278 }
279
280 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
281                                uint16_t queue_idx,
282                                uint16_t nb_desc,
283                                unsigned int socket_id,
284                                const struct rte_eth_rxconf *rx_conf,
285                                struct rte_mempool *mp)
286 {
287         struct bnxt *bp = eth_dev->data->dev_private;
288         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
289         struct bnxt_rx_queue *rxq;
290         int rc = 0;
291         uint8_t queue_state;
292
293         rc = is_bnxt_in_error(bp);
294         if (rc)
295                 return rc;
296
297         if (queue_idx >= bp->max_rx_rings) {
298                 PMD_DRV_LOG(ERR,
299                         "Cannot create Rx ring %d. Only %d rings available\n",
300                         queue_idx, bp->max_rx_rings);
301                 return -EINVAL;
302         }
303
304         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
305                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
306                 rc = -EINVAL;
307                 goto out;
308         }
309
310         if (eth_dev->data->rx_queues) {
311                 rxq = eth_dev->data->rx_queues[queue_idx];
312                 if (rxq)
313                         bnxt_rx_queue_release_op(rxq);
314         }
315         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
316                                  RTE_CACHE_LINE_SIZE, socket_id);
317         if (!rxq) {
318                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
319                 rc = -ENOMEM;
320                 goto out;
321         }
322         rxq->bp = bp;
323         rxq->mb_pool = mp;
324         rxq->nb_rx_desc = nb_desc;
325         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
326
327         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
328
329         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
330         if (rc)
331                 goto out;
332
333         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_size);
334         rxq->queue_id = queue_idx;
335         rxq->port_id = eth_dev->data->port_id;
336         if (rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC)
337                 rxq->crc_len = RTE_ETHER_CRC_LEN;
338         else
339                 rxq->crc_len = 0;
340
341         eth_dev->data->rx_queues[queue_idx] = rxq;
342         /* Allocate RX ring hardware descriptors */
343         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq, rxq->cp_ring, NULL,
344                              "rxr")) {
345                 PMD_DRV_LOG(ERR,
346                         "ring_dma_zone_reserve for rx_ring failed!\n");
347                 bnxt_rx_queue_release_op(rxq);
348                 rc = -ENOMEM;
349                 goto out;
350         }
351         rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
352
353         /* rxq 0 must not be stopped when used as async CPR */
354         if (!BNXT_NUM_ASYNC_CPR(bp) && queue_idx == 0)
355                 rxq->rx_deferred_start = false;
356         else
357                 rxq->rx_deferred_start = rx_conf->rx_deferred_start;
358
359         if (rxq->rx_deferred_start) {
360                 queue_state = RTE_ETH_QUEUE_STATE_STOPPED;
361                 rxq->rx_started = false;
362         } else {
363                 queue_state = RTE_ETH_QUEUE_STATE_STARTED;
364                 rxq->rx_started = true;
365         }
366         eth_dev->data->rx_queue_state[queue_idx] = queue_state;
367         rte_spinlock_init(&rxq->lock);
368
369 out:
370         return rc;
371 }
372
373 int
374 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
375 {
376         struct bnxt *bp = eth_dev->data->dev_private;
377         struct bnxt_rx_queue *rxq;
378         struct bnxt_cp_ring_info *cpr;
379         int rc = 0;
380
381         rc = is_bnxt_in_error(bp);
382         if (rc)
383                 return rc;
384
385         if (eth_dev->data->rx_queues) {
386                 rxq = eth_dev->data->rx_queues[queue_id];
387                 if (!rxq) {
388                         rc = -EINVAL;
389                         return rc;
390                 }
391                 cpr = rxq->cp_ring;
392                 B_CP_DB_REARM(cpr, cpr->cp_raw_cons);
393         }
394         return rc;
395 }
396
397 int
398 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
399 {
400         struct bnxt *bp = eth_dev->data->dev_private;
401         struct bnxt_rx_queue *rxq;
402         struct bnxt_cp_ring_info *cpr;
403         int rc = 0;
404
405         rc = is_bnxt_in_error(bp);
406         if (rc)
407                 return rc;
408
409         if (eth_dev->data->rx_queues) {
410                 rxq = eth_dev->data->rx_queues[queue_id];
411                 if (!rxq) {
412                         rc = -EINVAL;
413                         return rc;
414                 }
415                 cpr = rxq->cp_ring;
416                 B_CP_DB_DISARM(cpr);
417         }
418         return rc;
419 }
420
421 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
422 {
423         struct bnxt *bp = dev->data->dev_private;
424         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
425         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
426         struct bnxt_vnic_info *vnic = NULL;
427         int rc = 0;
428
429         rc = is_bnxt_in_error(bp);
430         if (rc)
431                 return rc;
432
433         if (rxq == NULL) {
434                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
435                 return -EINVAL;
436         }
437
438         /* Set the queue state to started here.
439          * We check the status of the queue while posting buffer.
440          * If queue is it started, we do not post buffers for Rx.
441          */
442         rxq->rx_started = true;
443         bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
444         rc = bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
445         if (rc)
446                 return rc;
447
448         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
449
450         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
451                 vnic = rxq->vnic;
452
453                 if (BNXT_HAS_RING_GRPS(bp)) {
454                         if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
455                                 return 0;
456
457                         vnic->fw_grp_ids[rx_queue_id] =
458                                         bp->grp_info[rx_queue_id].fw_grp_id;
459                         PMD_DRV_LOG(DEBUG,
460                                     "vnic = %p fw_grp_id = %d\n",
461                                     vnic, bp->grp_info[rx_queue_id].fw_grp_id);
462                 }
463
464                 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
465                 if (vnic->rx_queue_cnt > 1)
466                         rc = bnxt_vnic_rss_configure(bp, vnic);
467         }
468
469         if (rc == 0)
470                 dev->data->rx_queue_state[rx_queue_id] =
471                                 RTE_ETH_QUEUE_STATE_STARTED;
472         else
473                 rxq->rx_started = false;
474
475         PMD_DRV_LOG(INFO,
476                     "queue %d, rx_deferred_start %d, state %d!\n",
477                     rx_queue_id, rxq->rx_deferred_start,
478                     bp->eth_dev->data->rx_queue_state[rx_queue_id]);
479
480         return rc;
481 }
482
483 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
484 {
485         struct bnxt *bp = dev->data->dev_private;
486         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
487         struct bnxt_vnic_info *vnic = NULL;
488         struct bnxt_rx_queue *rxq = NULL;
489         int rc = 0;
490
491         rc = is_bnxt_in_error(bp);
492         if (rc)
493                 return rc;
494
495         /* For the stingray platform and other platforms needing tighter
496          * control of resource utilization, Rx CQ 0 also works as
497          * Default CQ for async notifications
498          */
499         if (!BNXT_NUM_ASYNC_CPR(bp) && !rx_queue_id) {
500                 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
501                 return -EINVAL;
502         }
503
504         rxq = bp->rx_queues[rx_queue_id];
505
506         if (rxq == NULL) {
507                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
508                 return -EINVAL;
509         }
510
511         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
512         rxq->rx_started = false;
513         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
514
515         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
516                 vnic = rxq->vnic;
517                 if (BNXT_HAS_RING_GRPS(bp))
518                         vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
519
520                 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
521                 if (vnic->rx_queue_cnt > 1)
522                         rc = bnxt_vnic_rss_configure(bp, vnic);
523         }
524
525         if (rc == 0)
526                 bnxt_rx_queue_release_mbufs(rxq);
527
528         return rc;
529 }