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