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 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
25 if (rxq && rxq->cp_ring && rxq->cp_ring->hw_stats)
26 rxq->cp_ring->hw_stats = NULL;
29 int bnxt_mq_rx_configure(struct bnxt *bp)
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 = 1, max_pools = 0;
39 struct bnxt_rx_queue *rxq;
43 /* Multi-queue mode */
44 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
45 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
47 switch (dev_conf->rxmode.mq_mode) {
48 case ETH_MQ_RX_VMDQ_RSS:
49 case ETH_MQ_RX_VMDQ_ONLY:
50 case ETH_MQ_RX_VMDQ_DCB_RSS:
53 pools = conf->nb_queue_pools;
54 /* For each pool, allocate MACVLAN CFA rule & VNIC */
55 max_pools = RTE_MIN(bp->max_vnics,
56 RTE_MIN(bp->max_l2_ctx,
57 RTE_MIN(bp->max_rsscos_ctx,
60 "pools = %u max_pools = %u\n",
62 if (pools > max_pools)
66 pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : 1;
69 PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
70 dev_conf->rxmode.mq_mode);
74 } else if (!dev_conf->rxmode.mq_mode) {
75 pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : pools;
78 pools = RTE_MIN(pools, bp->rx_cp_nr_rings);
79 nb_q_per_grp = bp->rx_cp_nr_rings / pools;
80 PMD_DRV_LOG(DEBUG, "pools = %u nb_q_per_grp = %u\n",
83 end_grp_id = nb_q_per_grp;
85 for (i = 0; i < pools; i++) {
86 vnic = &bp->vnic_info[i];
88 PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
92 vnic->flags |= BNXT_VNIC_INFO_BCAST;
95 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
96 rxq = bp->eth_dev->data->rx_queues[ring_idx];
99 "rxq[%d] = %p vnic[%d] = %p\n",
100 ring_idx, rxq, i, vnic);
103 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
104 bp->eth_dev->data->promiscuous = 1;
105 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
107 vnic->func_default = true;
109 vnic->start_grp_id = start_grp_id;
110 vnic->end_grp_id = end_grp_id;
113 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
114 !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
115 vnic->rss_dflt_cr = true;
116 goto skip_filter_allocation;
118 filter = bnxt_alloc_filter(bp);
120 PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
124 filter->mac_index = 0;
125 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
127 * TODO: Configure & associate CFA rule for
128 * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
130 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
132 skip_filter_allocation:
133 start_grp_id = end_grp_id;
134 end_grp_id += nb_q_per_grp;
137 bp->rx_num_qs_per_vnic = nb_q_per_grp;
139 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
140 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
142 if (bp->flags & BNXT_FLAG_UPDATE_HASH)
143 bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
145 for (i = 0; i < bp->nr_vnics; i++) {
146 uint32_t lvl = ETH_RSS_LEVEL(rss->rss_hf);
148 vnic = &bp->vnic_info[i];
150 bnxt_rte_to_hwrm_hash_types(rss->rss_hf);
152 bnxt_rte_to_hwrm_hash_level(bp,
157 * Use the supplied key if the key length is
158 * acceptable and the rss_key is not NULL
161 rss->rss_key_len <= HW_HASH_KEY_SIZE)
162 memcpy(vnic->rss_hash_key,
163 rss->rss_key, rss->rss_key_len);
170 /* Free allocated vnic/filters */
175 void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
177 struct rte_mbuf **sw_ring;
178 struct bnxt_tpa_info *tpa_info;
181 if (!rxq || !rxq->rx_ring)
184 sw_ring = rxq->rx_ring->rx_buf_ring;
186 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
188 * The vector receive burst function does not set used
189 * mbuf pointers to NULL, do that here to simplify
192 for (i = 0; i < rxq->rxrearm_nb; i++)
193 sw_ring[rxq->rxrearm_start + i] = NULL;
197 i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
199 if (sw_ring[i] != &rxq->fake_mbuf)
200 rte_pktmbuf_free_seg(sw_ring[i]);
205 /* Free up mbufs in Agg ring */
206 sw_ring = rxq->rx_ring->ag_buf_ring;
209 i < rxq->rx_ring->ag_ring_struct->ring_size; i++) {
211 rte_pktmbuf_free_seg(sw_ring[i]);
217 /* Free up mbufs in TPA */
218 tpa_info = rxq->rx_ring->tpa_info;
220 int max_aggs = BNXT_TPA_MAX_AGGS(rxq->bp);
222 for (i = 0; i < max_aggs; i++) {
223 if (tpa_info[i].mbuf) {
224 rte_pktmbuf_free_seg(tpa_info[i].mbuf);
225 tpa_info[i].mbuf = NULL;
232 void bnxt_free_rx_mbufs(struct bnxt *bp)
234 struct bnxt_rx_queue *rxq;
237 for (i = 0; i < (int)bp->rx_nr_rings; i++) {
238 rxq = bp->rx_queues[i];
239 bnxt_rx_queue_release_mbufs(rxq);
243 void bnxt_rx_queue_release_op(void *rx_queue)
245 struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
248 if (is_bnxt_in_error(rxq->bp))
251 bnxt_rx_queue_release_mbufs(rxq);
253 /* Free RX ring hardware descriptors */
255 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
256 rte_free(rxq->rx_ring->rx_ring_struct);
257 /* Free RX Agg ring hardware descriptors */
258 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
259 rte_free(rxq->rx_ring->ag_ring_struct);
261 rte_free(rxq->rx_ring);
263 /* Free RX completion ring hardware descriptors */
265 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
266 rte_free(rxq->cp_ring->cp_ring_struct);
267 rte_free(rxq->cp_ring);
270 bnxt_free_rxq_stats(rxq);
271 rte_memzone_free(rxq->mz);
278 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
281 unsigned int socket_id,
282 const struct rte_eth_rxconf *rx_conf,
283 struct rte_mempool *mp)
285 struct bnxt *bp = eth_dev->data->dev_private;
286 uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
287 struct bnxt_rx_queue *rxq;
291 rc = is_bnxt_in_error(bp);
295 if (queue_idx >= bnxt_max_rings(bp)) {
297 "Cannot create Rx ring %d. Only %d rings available\n",
298 queue_idx, bp->max_rx_rings);
302 if (nb_desc < BNXT_MIN_RING_DESC || nb_desc > MAX_RX_DESC_CNT) {
303 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
307 if (eth_dev->data->rx_queues) {
308 rxq = eth_dev->data->rx_queues[queue_idx];
310 bnxt_rx_queue_release_op(rxq);
312 rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
313 RTE_CACHE_LINE_SIZE, socket_id);
315 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
320 rxq->nb_rx_desc = nb_desc;
321 rxq->rx_free_thresh =
322 RTE_MIN(rte_align32pow2(nb_desc) / 4, RTE_BNXT_MAX_RX_BURST);
324 if (rx_conf->rx_drop_en != BNXT_DEFAULT_RX_DROP_EN)
326 "Per-queue config of drop-en is not supported.\n");
327 rxq->drop_en = BNXT_DEFAULT_RX_DROP_EN;
329 PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
331 rc = bnxt_init_rx_ring_struct(rxq, socket_id);
334 "init_rx_ring_struct failed!\n");
338 PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_size);
339 rxq->queue_id = queue_idx;
340 rxq->port_id = eth_dev->data->port_id;
341 if (rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC)
342 rxq->crc_len = RTE_ETHER_CRC_LEN;
346 eth_dev->data->rx_queues[queue_idx] = rxq;
347 /* Allocate RX ring hardware descriptors */
348 rc = bnxt_alloc_rings(bp, socket_id, queue_idx, NULL, rxq, rxq->cp_ring,
352 "ring_dma_zone_reserve for rx_ring failed!\n");
355 rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
357 /* rxq 0 must not be stopped when used as async CPR */
358 if (!BNXT_NUM_ASYNC_CPR(bp) && queue_idx == 0)
359 rxq->rx_deferred_start = false;
361 rxq->rx_deferred_start = rx_conf->rx_deferred_start;
363 if (rxq->rx_deferred_start) {
364 queue_state = RTE_ETH_QUEUE_STATE_STOPPED;
365 rxq->rx_started = false;
367 queue_state = RTE_ETH_QUEUE_STATE_STARTED;
368 rxq->rx_started = true;
370 eth_dev->data->rx_queue_state[queue_idx] = queue_state;
372 /* Configure mtu if it is different from what was configured before */
374 bnxt_mtu_set_op(eth_dev, eth_dev->data->mtu);
378 bnxt_rx_queue_release_op(rxq);
383 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
385 struct bnxt *bp = eth_dev->data->dev_private;
386 struct bnxt_rx_queue *rxq;
387 struct bnxt_cp_ring_info *cpr;
390 rc = is_bnxt_in_error(bp);
394 if (eth_dev->data->rx_queues) {
395 rxq = eth_dev->data->rx_queues[queue_id];
400 B_CP_DB_REARM(cpr, cpr->cp_raw_cons);
406 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
408 struct bnxt *bp = eth_dev->data->dev_private;
409 struct bnxt_rx_queue *rxq;
410 struct bnxt_cp_ring_info *cpr;
413 rc = is_bnxt_in_error(bp);
417 if (eth_dev->data->rx_queues) {
418 rxq = eth_dev->data->rx_queues[queue_id];
428 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
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;
436 rc = is_bnxt_in_error(bp);
441 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
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.
449 rxq->rx_started = true;
450 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
452 bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
453 rc = bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
457 if (BNXT_CHIP_P5(bp)) {
458 /* Reconfigure default receive ring and MRU. */
459 bnxt_hwrm_vnic_cfg(bp, rxq->vnic);
461 PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
463 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
466 if (BNXT_HAS_RING_GRPS(bp)) {
467 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
470 vnic->fw_grp_ids[rx_queue_id] =
471 bp->grp_info[rx_queue_id].fw_grp_id;
473 "vnic = %p fw_grp_id = %d\n",
474 vnic, bp->grp_info[rx_queue_id].fw_grp_id);
477 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
478 rc = bnxt_vnic_rss_configure(bp, vnic);
482 dev->data->rx_queue_state[rx_queue_id] =
483 RTE_ETH_QUEUE_STATE_STOPPED;
484 rxq->rx_started = false;
488 "queue %d, rx_deferred_start %d, state %d!\n",
489 rx_queue_id, rxq->rx_deferred_start,
490 bp->eth_dev->data->rx_queue_state[rx_queue_id]);
495 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
497 struct bnxt *bp = dev->data->dev_private;
498 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
499 struct bnxt_vnic_info *vnic = NULL;
500 struct bnxt_rx_queue *rxq = NULL;
501 int active_queue_cnt = 0;
504 rc = is_bnxt_in_error(bp);
508 /* For the stingray platform and other platforms needing tighter
509 * control of resource utilization, Rx CQ 0 also works as
510 * Default CQ for async notifications
512 if (!BNXT_NUM_ASYNC_CPR(bp) && !rx_queue_id) {
513 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
517 rxq = bp->rx_queues[rx_queue_id];
519 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
525 PMD_DRV_LOG(ERR, "VNIC not initialized for RxQ %d\n",
530 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
531 rxq->rx_started = false;
532 PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
534 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
535 if (BNXT_HAS_RING_GRPS(bp))
536 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
538 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
539 rc = bnxt_vnic_rss_configure(bp, vnic);
542 /* Compute current number of active receive queues. */
543 for (i = vnic->start_grp_id; i < vnic->end_grp_id; i++)
544 if (bp->rx_queues[i]->rx_started)
547 if (BNXT_CHIP_P5(bp)) {
549 * For Thor, we need to ensure that the VNIC default receive
550 * ring corresponds to an active receive queue. When no queue
551 * is active, we need to temporarily set the MRU to zero so
552 * that packets are dropped early in the receive pipeline in
553 * order to prevent the VNIC default receive ring from being
556 if (active_queue_cnt == 0) {
557 uint16_t saved_mru = vnic->mru;
560 /* Reconfigure default receive ring and MRU. */
561 bnxt_hwrm_vnic_cfg(bp, vnic);
562 vnic->mru = saved_mru;
564 /* Reconfigure default receive ring. */
565 bnxt_hwrm_vnic_cfg(bp, vnic);
567 } else if (active_queue_cnt) {
569 * If the queue being stopped is the current default queue and
570 * there are other active queues, pick one of them as the
571 * default and reconfigure the vnic.
573 if (vnic->dflt_ring_grp == bp->grp_info[rx_queue_id].fw_grp_id) {
574 for (i = vnic->start_grp_id; i < vnic->end_grp_id; i++) {
575 if (bp->rx_queues[i]->rx_started) {
576 vnic->dflt_ring_grp =
577 bp->grp_info[i].fw_grp_id;
578 bnxt_hwrm_vnic_cfg(bp, vnic);
586 bnxt_rx_queue_release_mbufs(rxq);