net/bnxt: fix forwarding with higher mbuf size
[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->rx_deferred_start = rx_conf->rx_deferred_start;
361         queue_state = rxq->rx_deferred_start ? RTE_ETH_QUEUE_STATE_STOPPED :
362                                                 RTE_ETH_QUEUE_STATE_STARTED;
363         eth_dev->data->rx_queue_state[queue_idx] = queue_state;
364         rte_spinlock_init(&rxq->lock);
365
366 out:
367         return rc;
368 }
369
370 int
371 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
372 {
373         struct bnxt *bp = eth_dev->data->dev_private;
374         struct bnxt_rx_queue *rxq;
375         struct bnxt_cp_ring_info *cpr;
376         int rc = 0;
377
378         rc = is_bnxt_in_error(bp);
379         if (rc)
380                 return rc;
381
382         if (eth_dev->data->rx_queues) {
383                 rxq = eth_dev->data->rx_queues[queue_id];
384                 if (!rxq) {
385                         rc = -EINVAL;
386                         return rc;
387                 }
388                 cpr = rxq->cp_ring;
389                 B_CP_DB_REARM(cpr, cpr->cp_raw_cons);
390         }
391         return rc;
392 }
393
394 int
395 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
396 {
397         struct bnxt *bp = eth_dev->data->dev_private;
398         struct bnxt_rx_queue *rxq;
399         struct bnxt_cp_ring_info *cpr;
400         int rc = 0;
401
402         rc = is_bnxt_in_error(bp);
403         if (rc)
404                 return rc;
405
406         if (eth_dev->data->rx_queues) {
407                 rxq = eth_dev->data->rx_queues[queue_id];
408                 if (!rxq) {
409                         rc = -EINVAL;
410                         return rc;
411                 }
412                 cpr = rxq->cp_ring;
413                 B_CP_DB_DISARM(cpr);
414         }
415         return rc;
416 }
417
418 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
419 {
420         struct bnxt *bp = dev->data->dev_private;
421         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
422         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
423         struct bnxt_vnic_info *vnic = NULL;
424         int rc = 0;
425
426         rc = is_bnxt_in_error(bp);
427         if (rc)
428                 return rc;
429
430         if (rxq == NULL) {
431                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
432                 return -EINVAL;
433         }
434
435         bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
436         rc = bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
437         if (rc)
438                 return rc;
439
440         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
441
442         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
443                 vnic = rxq->vnic;
444
445                 if (BNXT_HAS_RING_GRPS(bp)) {
446                         if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
447                                 return 0;
448
449                         vnic->fw_grp_ids[rx_queue_id] =
450                                         bp->grp_info[rx_queue_id].fw_grp_id;
451                 }
452
453                 PMD_DRV_LOG(DEBUG,
454                             "vnic = %p fw_grp_id = %d\n",
455                             vnic, bp->grp_info[rx_queue_id].fw_grp_id);
456
457                 rc = bnxt_vnic_rss_configure(bp, vnic);
458         }
459
460         if (rc == 0) {
461                 dev->data->rx_queue_state[rx_queue_id] =
462                                 RTE_ETH_QUEUE_STATE_STARTED;
463                 rxq->rx_deferred_start = false;
464         }
465
466         PMD_DRV_LOG(INFO,
467                     "queue %d, rx_deferred_start %d, state %d!\n",
468                     rx_queue_id, rxq->rx_deferred_start,
469                     bp->eth_dev->data->rx_queue_state[rx_queue_id]);
470
471         return rc;
472 }
473
474 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
475 {
476         struct bnxt *bp = dev->data->dev_private;
477         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
478         struct bnxt_vnic_info *vnic = NULL;
479         struct bnxt_rx_queue *rxq = NULL;
480         int rc = 0;
481
482         rc = is_bnxt_in_error(bp);
483         if (rc)
484                 return rc;
485
486         /* For the stingray platform and other platforms needing tighter
487          * control of resource utilization, Rx CQ 0 also works as
488          * Default CQ for async notifications
489          */
490         if (!BNXT_NUM_ASYNC_CPR(bp) && !rx_queue_id) {
491                 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
492                 return -EINVAL;
493         }
494
495         rxq = bp->rx_queues[rx_queue_id];
496
497         if (rxq == NULL) {
498                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
499                 return -EINVAL;
500         }
501
502         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
503         rxq->rx_deferred_start = true;
504         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
505
506         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
507                 vnic = rxq->vnic;
508                 if (BNXT_HAS_RING_GRPS(bp))
509                         vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
510                 rc = bnxt_vnic_rss_configure(bp, vnic);
511         }
512
513         if (rc == 0)
514                 bnxt_rx_queue_release_mbufs(rxq);
515
516         return rc;
517 }