1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
13 #include "bnxt_hwrm.h"
14 #include "hsi_struct_def_dpdk.h"
16 static const struct eth_dev_ops bnxt_vf_rep_dev_ops = {
17 .dev_infos_get = bnxt_vf_rep_dev_info_get_op,
18 .dev_configure = bnxt_vf_rep_dev_configure_op,
19 .dev_start = bnxt_vf_rep_dev_start_op,
20 .rx_queue_setup = bnxt_vf_rep_rx_queue_setup_op,
21 .rx_queue_release = bnxt_vf_rep_rx_queue_release_op,
22 .tx_queue_setup = bnxt_vf_rep_tx_queue_setup_op,
23 .tx_queue_release = bnxt_vf_rep_tx_queue_release_op,
24 .link_update = bnxt_vf_rep_link_update_op,
25 .dev_close = bnxt_vf_rep_dev_close_op,
26 .dev_stop = bnxt_vf_rep_dev_stop_op,
27 .stats_get = bnxt_vf_rep_stats_get_op,
28 .stats_reset = bnxt_vf_rep_stats_reset_op,
32 bnxt_vfr_recv(struct bnxt *bp, uint16_t cfa_code, uint16_t queue_id,
33 struct rte_mbuf *mbuf)
35 struct bnxt_sw_rx_bd *prod_rx_buf;
36 struct bnxt_rx_ring_info *rep_rxr;
37 struct bnxt_rx_queue *rep_rxq;
38 struct rte_eth_dev *vfr_eth_dev;
39 struct bnxt_vf_representor *vfr_bp;
44 vf_id = bp->cfa_code_map[cfa_code];
45 /* cfa_code is invalid OR vf_id > MAX REP. Assume normal Rx */
46 if (vf_id == BNXT_VF_IDX_INVALID || vf_id > BNXT_MAX_VF_REPS)
48 vfr_eth_dev = bp->rep_info[vf_id].vfr_eth_dev;
51 vfr_bp = vfr_eth_dev->data->dev_private;
52 if (vfr_bp->rx_cfa_code != cfa_code) {
53 /* cfa_code not meant for this VF rep!!?? */
56 /* If rxq_id happens to be > max rep_queue, use rxq0 */
57 que = queue_id < BNXT_MAX_VF_REP_RINGS ? queue_id : 0;
58 rep_rxq = vfr_bp->rx_queues[que];
59 rep_rxr = rep_rxq->rx_ring;
60 mask = rep_rxr->rx_ring_struct->ring_mask;
62 /* Put this mbuf on the RxQ of the Representor */
64 &rep_rxr->rx_buf_ring[rep_rxr->rx_prod++ & mask];
65 if (!prod_rx_buf->mbuf) {
66 prod_rx_buf->mbuf = mbuf;
67 vfr_bp->rx_bytes[que] += mbuf->pkt_len;
68 vfr_bp->rx_pkts[que]++;
70 vfr_bp->rx_drop_bytes[que] += mbuf->pkt_len;
71 vfr_bp->rx_drop_pkts[que]++;
72 rte_free(mbuf); /* Representor Rx ring full, drop pkt */
79 bnxt_vf_rep_rx_burst(void *rx_queue,
80 struct rte_mbuf **rx_pkts,
83 struct bnxt_rx_queue *rxq = rx_queue;
84 struct bnxt_sw_rx_bd *cons_rx_buf;
85 struct bnxt_rx_ring_info *rxr;
86 uint16_t nb_rx_pkts = 0;
93 mask = rxr->rx_ring_struct->ring_mask;
94 for (i = 0; i < nb_pkts; i++) {
95 cons_rx_buf = &rxr->rx_buf_ring[rxr->rx_cons & mask];
96 if (!cons_rx_buf->mbuf)
98 rx_pkts[nb_rx_pkts] = cons_rx_buf->mbuf;
99 rx_pkts[nb_rx_pkts]->port = rxq->port_id;
100 cons_rx_buf->mbuf = NULL;
109 bnxt_vf_rep_tx_burst(void *tx_queue,
110 struct rte_mbuf **tx_pkts,
111 __rte_unused uint16_t nb_pkts)
113 struct bnxt_vf_rep_tx_queue *vfr_txq = tx_queue;
114 struct bnxt_tx_queue *ptxq;
116 struct bnxt_vf_representor *vf_rep_bp;
124 qid = vfr_txq->txq->queue_id;
125 vf_rep_bp = vfr_txq->bp;
126 parent = vf_rep_bp->parent_dev->data->dev_private;
127 pthread_mutex_lock(&parent->rep_info->vfr_lock);
128 ptxq = parent->tx_queues[qid];
130 ptxq->tx_cfa_action = vf_rep_bp->tx_cfa_action;
132 for (i = 0; i < nb_pkts; i++) {
133 vf_rep_bp->tx_bytes[qid] += tx_pkts[i]->pkt_len;
134 vf_rep_bp->tx_pkts[qid]++;
137 rc = bnxt_xmit_pkts(ptxq, tx_pkts, nb_pkts);
138 ptxq->tx_cfa_action = 0;
139 pthread_mutex_unlock(&parent->rep_info->vfr_lock);
146 int bnxt_vf_representor_init(struct rte_eth_dev *eth_dev, void *params)
148 struct bnxt_vf_representor *vf_rep_bp = eth_dev->data->dev_private;
149 struct bnxt_vf_representor *rep_params =
150 (struct bnxt_vf_representor *)params;
151 struct rte_eth_link *link;
152 struct bnxt *parent_bp;
154 vf_rep_bp->vf_id = rep_params->vf_id;
155 vf_rep_bp->switch_domain_id = rep_params->switch_domain_id;
156 vf_rep_bp->parent_dev = rep_params->parent_dev;
158 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
159 eth_dev->data->representor_id = rep_params->vf_id;
161 rte_eth_random_addr(vf_rep_bp->dflt_mac_addr);
162 memcpy(vf_rep_bp->mac_addr, vf_rep_bp->dflt_mac_addr,
163 sizeof(vf_rep_bp->mac_addr));
164 eth_dev->data->mac_addrs =
165 (struct rte_ether_addr *)&vf_rep_bp->mac_addr;
166 eth_dev->dev_ops = &bnxt_vf_rep_dev_ops;
168 /* No data-path, but need stub Rx/Tx functions to avoid crash
169 * when testing with ovs-dpdk
171 eth_dev->rx_pkt_burst = bnxt_vf_rep_rx_burst;
172 eth_dev->tx_pkt_burst = bnxt_vf_rep_tx_burst;
173 /* Link state. Inherited from PF or trusted VF */
174 parent_bp = vf_rep_bp->parent_dev->data->dev_private;
175 link = &parent_bp->eth_dev->data->dev_link;
177 eth_dev->data->dev_link.link_speed = link->link_speed;
178 eth_dev->data->dev_link.link_duplex = link->link_duplex;
179 eth_dev->data->dev_link.link_status = link->link_status;
180 eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
182 PMD_DRV_LOG(INFO, "calling bnxt_print_link_info\n");
183 bnxt_print_link_info(eth_dev);
185 /* Pass the information to the rte_eth_dev_close() that it should also
186 * release the private port resources.
188 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
190 "Switch domain id %d: Representor Device %d init done\n",
191 vf_rep_bp->switch_domain_id, vf_rep_bp->vf_id);
196 int bnxt_vf_representor_uninit(struct rte_eth_dev *eth_dev)
198 struct bnxt *parent_bp;
199 struct bnxt_vf_representor *rep =
200 (struct bnxt_vf_representor *)eth_dev->data->dev_private;
204 eth_dev->data->mac_addrs = NULL;
205 eth_dev->dev_ops = NULL;
207 parent_bp = rep->parent_dev->data->dev_private;
211 parent_bp->num_reps--;
213 if (parent_bp->rep_info)
214 memset(&parent_bp->rep_info[vf_id], 0,
215 sizeof(parent_bp->rep_info[vf_id]));
216 /* mark that this representor has been freed */
220 int bnxt_vf_rep_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_compl)
222 struct bnxt *parent_bp;
223 struct bnxt_vf_representor *rep =
224 (struct bnxt_vf_representor *)eth_dev->data->dev_private;
225 struct rte_eth_link *link;
228 parent_bp = rep->parent_dev->data->dev_private;
229 rc = bnxt_link_update_op(parent_bp->eth_dev, wait_to_compl);
231 /* Link state. Inherited from PF or trusted VF */
232 link = &parent_bp->eth_dev->data->dev_link;
234 eth_dev->data->dev_link.link_speed = link->link_speed;
235 eth_dev->data->dev_link.link_duplex = link->link_duplex;
236 eth_dev->data->dev_link.link_status = link->link_status;
237 eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
238 bnxt_print_link_info(eth_dev);
243 static int bnxt_vfr_alloc(struct bnxt_vf_representor *vfr)
246 struct bnxt *parent_bp;
248 if (!vfr || !vfr->parent_dev) {
250 "No memory allocated for representor\n");
254 parent_bp = vfr->parent_dev->data->dev_private;
256 /* Check if representor has been already allocated in FW */
257 if (vfr->tx_cfa_action && vfr->rx_cfa_code)
261 * Alloc VF rep rules in CFA after default VNIC is created.
262 * Otherwise the FW will create the VF-rep rules with
263 * default drop action.
267 * This is where we need to replace invoking an HWRM cmd
268 * with the new TFLIB ULP API to do more/less the same job
269 rc = bnxt_hwrm_cfa_vfr_alloc(parent_bp,
275 parent_bp->cfa_code_map[vfr->rx_cfa_code] = vfr->vf_id;
276 PMD_DRV_LOG(DEBUG, "allocated representor %d in FW\n",
280 "Failed to alloc representor %d in FW\n",
287 static void bnxt_vf_rep_free_rx_mbufs(struct bnxt_vf_representor *rep_bp)
289 struct bnxt_rx_queue *rxq;
292 for (i = 0; i < rep_bp->rx_nr_rings; i++) {
293 rxq = rep_bp->rx_queues[i];
294 bnxt_rx_queue_release_mbufs(rxq);
298 int bnxt_vf_rep_dev_start_op(struct rte_eth_dev *eth_dev)
300 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
303 rc = bnxt_vfr_alloc(rep_bp);
306 eth_dev->rx_pkt_burst = &bnxt_vf_rep_rx_burst;
307 eth_dev->tx_pkt_burst = &bnxt_vf_rep_tx_burst;
309 bnxt_vf_rep_link_update_op(eth_dev, 1);
311 eth_dev->data->dev_link.link_status = 0;
312 bnxt_vf_rep_free_rx_mbufs(rep_bp);
318 static int bnxt_vfr_free(struct bnxt_vf_representor *vfr)
321 struct bnxt *parent_bp;
323 if (!vfr || !vfr->parent_dev) {
325 "No memory allocated for representor\n");
329 parent_bp = vfr->parent_dev->data->dev_private;
331 /* Check if representor has been already freed in FW */
332 if (!vfr->tx_cfa_action && !vfr->rx_cfa_code)
336 * This is where we need to replace invoking an HWRM cmd
337 * with the new TFLIB ULP API to do more/less the same job
338 rc = bnxt_hwrm_cfa_vfr_free(parent_bp,
343 "Failed to free representor %d in FW\n",
348 parent_bp->cfa_code_map[vfr->rx_cfa_code] = BNXT_VF_IDX_INVALID;
349 PMD_DRV_LOG(DEBUG, "freed representor %d in FW\n",
351 vfr->tx_cfa_action = 0;
352 vfr->rx_cfa_code = 0;
357 void bnxt_vf_rep_dev_stop_op(struct rte_eth_dev *eth_dev)
359 struct bnxt_vf_representor *vfr_bp = eth_dev->data->dev_private;
361 /* Avoid crashes as we are about to free queues */
362 eth_dev->rx_pkt_burst = &bnxt_dummy_recv_pkts;
363 eth_dev->tx_pkt_burst = &bnxt_dummy_xmit_pkts;
365 bnxt_vfr_free(vfr_bp);
367 if (eth_dev->data->dev_started)
368 eth_dev->data->dev_link.link_status = 0;
370 bnxt_vf_rep_free_rx_mbufs(vfr_bp);
373 void bnxt_vf_rep_dev_close_op(struct rte_eth_dev *eth_dev)
375 bnxt_vf_representor_uninit(eth_dev);
378 int bnxt_vf_rep_dev_info_get_op(struct rte_eth_dev *eth_dev,
379 struct rte_eth_dev_info *dev_info)
381 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
382 struct bnxt *parent_bp;
383 uint16_t max_vnics, i, j, vpool, vrxq;
384 unsigned int max_rx_rings;
388 parent_bp = rep_bp->parent_dev->data->dev_private;
390 PMD_DRV_LOG(ERR, "Rep parent NULL!\n");
393 PMD_DRV_LOG(DEBUG, "Representor dev_info_get_op\n");
394 dev_info->max_mac_addrs = parent_bp->max_l2_ctx;
395 dev_info->max_hash_mac_addrs = 0;
397 max_rx_rings = BNXT_MAX_VF_REP_RINGS;
398 /* For the sake of symmetry, max_rx_queues = max_tx_queues */
399 dev_info->max_rx_queues = max_rx_rings;
400 dev_info->max_tx_queues = max_rx_rings;
401 dev_info->reta_size = bnxt_rss_hash_tbl_size(parent_bp);
402 dev_info->hash_key_size = 40;
403 max_vnics = parent_bp->max_vnics;
406 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
407 dev_info->max_mtu = BNXT_MAX_MTU;
409 /* Fast path specifics */
410 dev_info->min_rx_bufsize = 1;
411 dev_info->max_rx_pktlen = BNXT_MAX_PKT_LEN;
413 dev_info->rx_offload_capa = BNXT_DEV_RX_OFFLOAD_SUPPORT;
414 if (parent_bp->flags & BNXT_FLAG_PTP_SUPPORTED)
415 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TIMESTAMP;
416 dev_info->tx_offload_capa = BNXT_DEV_TX_OFFLOAD_SUPPORT;
417 dev_info->flow_type_rss_offloads = BNXT_ETH_RSS_SUPPORT;
420 dev_info->default_rxconf = (struct rte_eth_rxconf) {
426 .rx_free_thresh = 32,
427 /* If no descriptors available, pkts are dropped by default */
431 dev_info->default_txconf = (struct rte_eth_txconf) {
437 .tx_free_thresh = 32,
440 eth_dev->data->dev_conf.intr_conf.lsc = 1;
442 eth_dev->data->dev_conf.intr_conf.rxq = 1;
443 dev_info->rx_desc_lim.nb_min = BNXT_MIN_RING_DESC;
444 dev_info->rx_desc_lim.nb_max = BNXT_MAX_RX_RING_DESC;
445 dev_info->tx_desc_lim.nb_min = BNXT_MIN_RING_DESC;
446 dev_info->tx_desc_lim.nb_max = BNXT_MAX_TX_RING_DESC;
451 * TODO: default_rxconf, default_txconf, rx_desc_lim, and tx_desc_lim
452 * need further investigation.
456 vpool = 64; /* ETH_64_POOLS */
457 vrxq = 128; /* ETH_VMDQ_DCB_NUM_QUEUES */
458 for (i = 0; i < 4; vpool >>= 1, i++) {
459 if (max_vnics > vpool) {
460 for (j = 0; j < 5; vrxq >>= 1, j++) {
461 if (dev_info->max_rx_queues > vrxq) {
467 /* Not enough resources to support VMDq */
471 /* Not enough resources to support VMDq */
475 dev_info->max_vmdq_pools = vpool;
476 dev_info->vmdq_queue_num = vrxq;
478 dev_info->vmdq_pool_base = 0;
479 dev_info->vmdq_queue_base = 0;
484 int bnxt_vf_rep_dev_configure_op(__rte_unused struct rte_eth_dev *eth_dev)
486 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
488 PMD_DRV_LOG(DEBUG, "Representor dev_configure_op\n");
489 rep_bp->rx_queues = (void *)eth_dev->data->rx_queues;
490 rep_bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
491 rep_bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
496 int bnxt_vf_rep_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
499 unsigned int socket_id,
500 __rte_unused const struct rte_eth_rxconf *rx_conf,
501 __rte_unused struct rte_mempool *mp)
503 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
504 struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
505 struct bnxt_rx_queue *parent_rxq;
506 struct bnxt_rx_queue *rxq;
507 struct bnxt_sw_rx_bd *buf_ring;
510 if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
512 "Cannot create Rx ring %d. %d rings available\n",
513 queue_idx, BNXT_MAX_VF_REP_RINGS);
517 if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
518 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
522 parent_rxq = parent_bp->rx_queues[queue_idx];
524 PMD_DRV_LOG(ERR, "Parent RxQ has not been configured yet\n");
528 if (nb_desc != parent_rxq->nb_rx_desc) {
529 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent rxq", nb_desc);
533 if (eth_dev->data->rx_queues) {
534 rxq = eth_dev->data->rx_queues[queue_idx];
536 bnxt_rx_queue_release_op(rxq);
539 rxq = rte_zmalloc_socket("bnxt_vfr_rx_queue",
540 sizeof(struct bnxt_rx_queue),
541 RTE_CACHE_LINE_SIZE, socket_id);
543 PMD_DRV_LOG(ERR, "bnxt_vfr_rx_queue allocation failed!\n");
547 rxq->nb_rx_desc = nb_desc;
549 rc = bnxt_init_rx_ring_struct(rxq, socket_id);
553 buf_ring = rte_zmalloc_socket("bnxt_rx_vfr_buf_ring",
554 sizeof(struct bnxt_sw_rx_bd) *
555 rxq->rx_ring->rx_ring_struct->ring_size,
556 RTE_CACHE_LINE_SIZE, socket_id);
558 PMD_DRV_LOG(ERR, "bnxt_rx_vfr_buf_ring allocation failed!\n");
563 rxq->rx_ring->rx_buf_ring = buf_ring;
564 rxq->queue_id = queue_idx;
565 rxq->port_id = eth_dev->data->port_id;
566 eth_dev->data->rx_queues[queue_idx] = rxq;
572 bnxt_rx_queue_release_op(rxq);
577 void bnxt_vf_rep_rx_queue_release_op(void *rx_queue)
579 struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
584 bnxt_rx_queue_release_mbufs(rxq);
586 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
587 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
588 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
593 int bnxt_vf_rep_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
596 unsigned int socket_id,
597 __rte_unused const struct rte_eth_txconf *tx_conf)
599 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
600 struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
601 struct bnxt_tx_queue *parent_txq, *txq;
602 struct bnxt_vf_rep_tx_queue *vfr_txq;
604 if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
606 "Cannot create Tx rings %d. %d rings available\n",
607 queue_idx, BNXT_MAX_VF_REP_RINGS);
611 if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
612 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
616 parent_txq = parent_bp->tx_queues[queue_idx];
618 PMD_DRV_LOG(ERR, "Parent TxQ has not been configured yet\n");
622 if (nb_desc != parent_txq->nb_tx_desc) {
623 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent txq", nb_desc);
627 if (eth_dev->data->tx_queues) {
628 vfr_txq = eth_dev->data->tx_queues[queue_idx];
629 bnxt_vf_rep_tx_queue_release_op(vfr_txq);
633 vfr_txq = rte_zmalloc_socket("bnxt_vfr_tx_queue",
634 sizeof(struct bnxt_vf_rep_tx_queue),
635 RTE_CACHE_LINE_SIZE, socket_id);
637 PMD_DRV_LOG(ERR, "bnxt_vfr_tx_queue allocation failed!");
640 txq = rte_zmalloc_socket("bnxt_tx_queue",
641 sizeof(struct bnxt_tx_queue),
642 RTE_CACHE_LINE_SIZE, socket_id);
644 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
649 txq->nb_tx_desc = nb_desc;
650 txq->queue_id = queue_idx;
651 txq->port_id = eth_dev->data->port_id;
653 vfr_txq->bp = rep_bp;
654 eth_dev->data->tx_queues[queue_idx] = vfr_txq;
659 void bnxt_vf_rep_tx_queue_release_op(void *tx_queue)
661 struct bnxt_vf_rep_tx_queue *vfr_txq = tx_queue;
666 rte_free(vfr_txq->txq);
670 int bnxt_vf_rep_stats_get_op(struct rte_eth_dev *eth_dev,
671 struct rte_eth_stats *stats)
673 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
676 memset(stats, 0, sizeof(*stats));
677 for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
678 stats->obytes += rep_bp->tx_bytes[i];
679 stats->opackets += rep_bp->tx_pkts[i];
680 stats->ibytes += rep_bp->rx_bytes[i];
681 stats->ipackets += rep_bp->rx_pkts[i];
682 stats->imissed += rep_bp->rx_drop_pkts[i];
684 stats->q_ipackets[i] = rep_bp->rx_pkts[i];
685 stats->q_ibytes[i] = rep_bp->rx_bytes[i];
686 stats->q_opackets[i] = rep_bp->tx_pkts[i];
687 stats->q_obytes[i] = rep_bp->tx_bytes[i];
688 stats->q_errors[i] = rep_bp->rx_drop_pkts[i];
694 int bnxt_vf_rep_stats_reset_op(struct rte_eth_dev *eth_dev)
696 struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
699 for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
700 rep_bp->tx_pkts[i] = 0;
701 rep_bp->tx_bytes[i] = 0;
702 rep_bp->rx_pkts[i] = 0;
703 rep_bp->rx_bytes[i] = 0;
704 rep_bp->rx_drop_pkts[i] = 0;