1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
8 #include <rte_malloc.h>
11 #include "bnxt_filter.h"
12 #include "bnxt_hwrm.h"
13 #include "bnxt_ring.h"
16 #include "bnxt_vnic.h"
17 #include "hsi_struct_def_dpdk.h"
23 uint64_t bnxt_get_rx_port_offloads(struct bnxt *bp)
25 uint64_t rx_offload_capa;
27 rx_offload_capa = RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
28 RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
29 RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
30 RTE_ETH_RX_OFFLOAD_KEEP_CRC |
31 RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
32 RTE_ETH_RX_OFFLOAD_VLAN_EXTEND |
33 RTE_ETH_RX_OFFLOAD_TCP_LRO |
34 RTE_ETH_RX_OFFLOAD_SCATTER |
35 RTE_ETH_RX_OFFLOAD_RSS_HASH;
37 if (bp->flags & BNXT_FLAG_PTP_SUPPORTED)
38 rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
39 if (bp->vnic_cap_flags & BNXT_VNIC_CAP_VLAN_RX_STRIP)
40 rx_offload_capa |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
42 if (BNXT_TUNNELED_OFFLOADS_CAP_ALL_EN(bp))
43 rx_offload_capa |= RTE_ETH_RX_OFFLOAD_OUTER_IPV4_CKSUM |
44 RTE_ETH_RX_OFFLOAD_OUTER_UDP_CKSUM;
46 return rx_offload_capa;
49 /* Determine whether the current configuration needs aggregation ring in HW. */
50 int bnxt_need_agg_ring(struct rte_eth_dev *eth_dev)
52 /* scattered_rx will be true if OFFLOAD_SCATTER is enabled,
53 * if LRO is enabled, or if the max packet len is greater than the
54 * mbuf data size. So AGG ring will be needed whenever scattered_rx
57 return eth_dev->data->scattered_rx ? 1 : 0;
60 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
62 if (rxq && rxq->cp_ring && rxq->cp_ring->hw_stats)
63 rxq->cp_ring->hw_stats = NULL;
66 int bnxt_mq_rx_configure(struct bnxt *bp)
68 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
69 struct rte_eth_rss_conf *rss = &bp->rss_conf;
70 const struct rte_eth_vmdq_rx_conf *conf =
71 &dev_conf->rx_adv_conf.vmdq_rx_conf;
72 unsigned int i, j, nb_q_per_grp = 1, ring_idx = 0;
73 int start_grp_id, end_grp_id = 1, rc = 0;
74 struct bnxt_vnic_info *vnic;
75 struct bnxt_filter_info *filter;
76 enum rte_eth_nb_pools pools = 1, max_pools = 0;
77 struct bnxt_rx_queue *rxq;
81 /* Multi-queue mode */
82 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_VMDQ_DCB_RSS) {
83 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
85 switch (dev_conf->rxmode.mq_mode) {
86 case RTE_ETH_MQ_RX_VMDQ_RSS:
87 case RTE_ETH_MQ_RX_VMDQ_ONLY:
88 case RTE_ETH_MQ_RX_VMDQ_DCB_RSS:
91 pools = conf->nb_queue_pools;
92 /* For each pool, allocate MACVLAN CFA rule & VNIC */
93 max_pools = RTE_MIN(bp->max_vnics,
94 RTE_MIN(bp->max_l2_ctx,
95 RTE_MIN(bp->max_rsscos_ctx,
98 "pools = %u max_pools = %u\n",
100 if (pools > max_pools)
103 case RTE_ETH_MQ_RX_RSS:
104 pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : 1;
107 PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
108 dev_conf->rxmode.mq_mode);
112 } else if (!dev_conf->rxmode.mq_mode) {
113 pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : pools;
116 pools = RTE_MIN(pools, bp->rx_cp_nr_rings);
117 nb_q_per_grp = bp->rx_cp_nr_rings / pools;
118 PMD_DRV_LOG(DEBUG, "pools = %u nb_q_per_grp = %u\n",
119 pools, nb_q_per_grp);
121 end_grp_id = nb_q_per_grp;
123 for (i = 0; i < pools; i++) {
124 vnic = &bp->vnic_info[i];
126 PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
130 vnic->flags |= BNXT_VNIC_INFO_BCAST;
133 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
134 rxq = bp->eth_dev->data->rx_queues[ring_idx];
137 "rxq[%d] = %p vnic[%d] = %p\n",
138 ring_idx, rxq, i, vnic);
141 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_VMDQ_DCB) {
142 bp->eth_dev->data->promiscuous = 1;
143 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
145 vnic->func_default = true;
147 vnic->start_grp_id = start_grp_id;
148 vnic->end_grp_id = end_grp_id;
151 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_VMDQ_DCB ||
152 !(dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS))
153 vnic->rss_dflt_cr = true;
154 goto skip_filter_allocation;
156 filter = bnxt_alloc_filter(bp);
158 PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
162 filter->mac_index = 0;
163 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
165 * TODO: Configure & associate CFA rule for
166 * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
168 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
170 skip_filter_allocation:
171 start_grp_id = end_grp_id;
172 end_grp_id += nb_q_per_grp;
175 bp->rx_num_qs_per_vnic = nb_q_per_grp;
177 for (i = 0; i < bp->nr_vnics; i++) {
178 uint32_t lvl = RTE_ETH_RSS_LEVEL(rss->rss_hf);
180 vnic = &bp->vnic_info[i];
181 vnic->hash_type = bnxt_rte_to_hwrm_hash_types(rss->rss_hf);
182 vnic->hash_mode = bnxt_rte_to_hwrm_hash_level(bp, rss->rss_hf, lvl);
185 * Use the supplied key if the key length is
186 * acceptable and the rss_key is not NULL
188 if (rss->rss_key && rss->rss_key_len <= HW_HASH_KEY_SIZE)
189 memcpy(vnic->rss_hash_key, rss->rss_key, rss->rss_key_len);
195 /* Free allocated vnic/filters */
200 void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
202 struct rte_mbuf **sw_ring;
203 struct bnxt_tpa_info *tpa_info;
206 if (!rxq || !rxq->rx_ring)
209 sw_ring = rxq->rx_ring->rx_buf_ring;
211 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
213 * The vector receive burst function does not set used
214 * mbuf pointers to NULL, do that here to simplify
217 for (i = 0; i < rxq->rxrearm_nb; i++)
218 sw_ring[rxq->rxrearm_start + i] = NULL;
222 i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
224 if (sw_ring[i] != &rxq->fake_mbuf)
225 rte_pktmbuf_free_seg(sw_ring[i]);
230 /* Free up mbufs in Agg ring */
231 if (rxq->bp == NULL ||
232 rxq->bp->eth_dev == NULL ||
233 !bnxt_need_agg_ring(rxq->bp->eth_dev))
236 sw_ring = rxq->rx_ring->ag_buf_ring;
239 i < rxq->rx_ring->ag_ring_struct->ring_size; i++) {
241 rte_pktmbuf_free_seg(sw_ring[i]);
247 /* Free up mbufs in TPA */
248 tpa_info = rxq->rx_ring->tpa_info;
250 int max_aggs = BNXT_TPA_MAX_AGGS(rxq->bp);
252 for (i = 0; i < max_aggs; i++) {
253 if (tpa_info[i].mbuf) {
254 rte_pktmbuf_free_seg(tpa_info[i].mbuf);
255 tpa_info[i].mbuf = NULL;
262 void bnxt_free_rx_mbufs(struct bnxt *bp)
264 struct bnxt_rx_queue *rxq;
267 for (i = 0; i < (int)bp->rx_nr_rings; i++) {
268 rxq = bp->rx_queues[i];
269 bnxt_rx_queue_release_mbufs(rxq);
273 void bnxt_free_rxq_mem(struct bnxt_rx_queue *rxq)
275 bnxt_rx_queue_release_mbufs(rxq);
277 /* Free RX, AGG ring hardware descriptors */
279 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
280 rte_free(rxq->rx_ring->rx_ring_struct);
281 rxq->rx_ring->rx_ring_struct = NULL;
282 /* Free RX Agg ring hardware descriptors */
283 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
284 rte_free(rxq->rx_ring->ag_ring_struct);
285 rxq->rx_ring->ag_ring_struct = NULL;
287 rte_free(rxq->rx_ring);
290 /* Free RX completion ring hardware descriptors */
292 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
293 rte_free(rxq->cp_ring->cp_ring_struct);
294 rxq->cp_ring->cp_ring_struct = NULL;
295 rte_free(rxq->cp_ring);
299 bnxt_free_rxq_stats(rxq);
300 rte_memzone_free(rxq->mz);
304 void bnxt_rx_queue_release_op(struct rte_eth_dev *dev, uint16_t queue_idx)
306 struct bnxt_rx_queue *rxq = dev->data->rx_queues[queue_idx];
309 if (is_bnxt_in_error(rxq->bp))
312 bnxt_free_hwrm_rx_ring(rxq->bp, rxq->queue_id);
313 bnxt_free_rxq_mem(rxq);
318 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
321 unsigned int socket_id,
322 const struct rte_eth_rxconf *rx_conf,
323 struct rte_mempool *mp)
325 struct bnxt *bp = eth_dev->data->dev_private;
326 uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
327 struct bnxt_rx_queue *rxq;
330 rc = is_bnxt_in_error(bp);
334 if (queue_idx >= bnxt_max_rings(bp)) {
336 "Cannot create Rx ring %d. Only %d rings available\n",
337 queue_idx, bp->max_rx_rings);
341 if (nb_desc < BNXT_MIN_RING_DESC || nb_desc > MAX_RX_DESC_CNT) {
342 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
346 if (eth_dev->data->rx_queues) {
347 rxq = eth_dev->data->rx_queues[queue_idx];
349 bnxt_rx_queue_release_op(eth_dev, queue_idx);
351 rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
352 RTE_CACHE_LINE_SIZE, socket_id);
354 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
359 rxq->nb_rx_desc = nb_desc;
360 rxq->rx_free_thresh =
361 RTE_MIN(rte_align32pow2(nb_desc) / 4, RTE_BNXT_MAX_RX_BURST);
363 if (rx_conf->rx_drop_en != BNXT_DEFAULT_RX_DROP_EN)
365 "Per-queue config of drop-en is not supported.\n");
366 rxq->drop_en = BNXT_DEFAULT_RX_DROP_EN;
368 PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
370 eth_dev->data->rx_queues[queue_idx] = rxq;
372 rc = bnxt_init_rx_ring_struct(rxq, socket_id);
375 "init_rx_ring_struct failed!\n");
379 PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_size);
380 rxq->queue_id = queue_idx;
381 rxq->port_id = eth_dev->data->port_id;
382 if (rx_offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
383 rxq->crc_len = RTE_ETHER_CRC_LEN;
387 /* Allocate RX ring hardware descriptors */
388 rc = bnxt_alloc_rings(bp, socket_id, queue_idx, NULL, rxq, rxq->cp_ring,
392 "ring_dma_zone_reserve for rx_ring failed!\n");
395 rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
397 /* rxq 0 must not be stopped when used as async CPR */
398 if (!BNXT_NUM_ASYNC_CPR(bp) && queue_idx == 0)
399 rxq->rx_deferred_start = false;
401 rxq->rx_deferred_start = rx_conf->rx_deferred_start;
403 rxq->rx_started = rxq->rx_deferred_start ? false : true;
404 rxq->vnic = BNXT_GET_DEFAULT_VNIC(bp);
406 /* Configure mtu if it is different from what was configured before */
408 bnxt_mtu_set_op(eth_dev, eth_dev->data->mtu);
412 bnxt_rx_queue_release_op(eth_dev, queue_idx);
417 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
419 struct bnxt *bp = eth_dev->data->dev_private;
420 struct bnxt_rx_queue *rxq;
421 struct bnxt_cp_ring_info *cpr;
424 rc = is_bnxt_in_error(bp);
428 if (eth_dev->data->rx_queues) {
429 rxq = eth_dev->data->rx_queues[queue_id];
434 B_CP_DB_REARM(cpr, cpr->cp_raw_cons);
440 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
442 struct bnxt *bp = eth_dev->data->dev_private;
443 struct bnxt_rx_queue *rxq;
444 struct bnxt_cp_ring_info *cpr;
447 rc = is_bnxt_in_error(bp);
451 if (eth_dev->data->rx_queues) {
452 rxq = eth_dev->data->rx_queues[queue_id];
462 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
464 struct bnxt *bp = dev->data->dev_private;
465 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
466 struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
467 struct bnxt_vnic_info *vnic = NULL;
470 rc = is_bnxt_in_error(bp);
475 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
479 /* Set the queue state to started here.
480 * We check the status of the queue while posting buffer.
481 * If queue is it started, we do not post buffers for Rx.
483 rxq->rx_started = true;
484 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
486 bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
487 rc = bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
491 if (BNXT_HAS_RING_GRPS(bp))
492 rxq->vnic->dflt_ring_grp = bp->grp_info[rx_queue_id].fw_grp_id;
493 /* Reconfigure default receive ring and MRU. */
494 bnxt_hwrm_vnic_cfg(bp, rxq->vnic);
496 PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
498 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
501 if (BNXT_HAS_RING_GRPS(bp)) {
502 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
505 vnic->fw_grp_ids[rx_queue_id] =
506 bp->grp_info[rx_queue_id].fw_grp_id;
508 "vnic = %p fw_grp_id = %d\n",
509 vnic, bp->grp_info[rx_queue_id].fw_grp_id);
512 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
513 rc = bnxt_vnic_rss_configure(bp, vnic);
517 dev->data->rx_queue_state[rx_queue_id] =
518 RTE_ETH_QUEUE_STATE_STOPPED;
519 rxq->rx_started = false;
523 "queue %d, rx_deferred_start %d, state %d!\n",
524 rx_queue_id, rxq->rx_deferred_start,
525 bp->eth_dev->data->rx_queue_state[rx_queue_id]);
530 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
532 struct bnxt *bp = dev->data->dev_private;
533 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
534 struct bnxt_vnic_info *vnic = NULL;
535 struct bnxt_rx_queue *rxq = NULL;
536 int active_queue_cnt = 0;
539 rc = is_bnxt_in_error(bp);
543 /* For the stingray platform and other platforms needing tighter
544 * control of resource utilization, Rx CQ 0 also works as
545 * Default CQ for async notifications
547 if (!BNXT_NUM_ASYNC_CPR(bp) && !rx_queue_id) {
548 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
552 rxq = bp->rx_queues[rx_queue_id];
554 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
560 PMD_DRV_LOG(ERR, "VNIC not initialized for RxQ %d\n",
565 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
566 rxq->rx_started = false;
567 PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
569 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
570 if (BNXT_HAS_RING_GRPS(bp))
571 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
573 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
574 rc = bnxt_vnic_rss_configure(bp, vnic);
577 /* Compute current number of active receive queues. */
578 for (i = vnic->start_grp_id; i < vnic->end_grp_id; i++)
579 if (bp->rx_queues[i]->rx_started)
582 if (BNXT_CHIP_P5(bp)) {
584 * For Thor, we need to ensure that the VNIC default receive
585 * ring corresponds to an active receive queue. When no queue
586 * is active, we need to temporarily set the MRU to zero so
587 * that packets are dropped early in the receive pipeline in
588 * order to prevent the VNIC default receive ring from being
591 if (active_queue_cnt == 0) {
592 uint16_t saved_mru = vnic->mru;
594 /* clear RSS setting on vnic. */
595 bnxt_vnic_rss_clear_p5(bp, vnic);
598 /* Reconfigure default receive ring and MRU. */
599 bnxt_hwrm_vnic_cfg(bp, vnic);
600 vnic->mru = saved_mru;
602 /* Reconfigure default receive ring. */
603 bnxt_hwrm_vnic_cfg(bp, vnic);
605 } else if (active_queue_cnt) {
607 * If the queue being stopped is the current default queue and
608 * there are other active queues, pick one of them as the
609 * default and reconfigure the vnic.
611 if (vnic->dflt_ring_grp == bp->grp_info[rx_queue_id].fw_grp_id) {
612 for (i = vnic->start_grp_id; i < vnic->end_grp_id; i++) {
613 if (bp->rx_queues[i]->rx_started) {
614 vnic->dflt_ring_grp =
615 bp->grp_info[i].fw_grp_id;
616 bnxt_hwrm_vnic_cfg(bp, vnic);
624 bnxt_rx_queue_release_mbufs(rxq);