1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
16 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #pragma GCC diagnostic ignored "-Wpedantic"
20 #include <infiniband/verbs.h>
21 #include <infiniband/mlx5dv.h>
23 #pragma GCC diagnostic error "-Wpedantic"
27 #include <rte_malloc.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_common.h>
31 #include "mlx5_utils.h"
32 #include "mlx5_defs.h"
34 #include "mlx5_rxtx.h"
35 #include "mlx5_autoconf.h"
36 #include "mlx5_glue.h"
39 * Allocate TX queue elements.
42 * Pointer to TX queue structure.
45 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
47 const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
50 for (i = 0; (i != elts_n); ++i)
51 txq_ctrl->txq.elts[i] = NULL;
52 DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
53 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
54 txq_ctrl->txq.elts_head = 0;
55 txq_ctrl->txq.elts_tail = 0;
56 txq_ctrl->txq.elts_comp = 0;
60 * Free TX queue elements.
63 * Pointer to TX queue structure.
66 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
68 const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
69 const uint16_t elts_m = elts_n - 1;
70 uint16_t elts_head = txq_ctrl->txq.elts_head;
71 uint16_t elts_tail = txq_ctrl->txq.elts_tail;
72 struct rte_mbuf *(*elts)[elts_n] = &txq_ctrl->txq.elts;
74 DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
75 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
76 txq_ctrl->txq.elts_head = 0;
77 txq_ctrl->txq.elts_tail = 0;
78 txq_ctrl->txq.elts_comp = 0;
80 while (elts_tail != elts_head) {
81 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
84 rte_pktmbuf_free_seg(elt);
87 memset(&(*elts)[elts_tail & elts_m],
89 sizeof((*elts)[elts_tail & elts_m]));
96 * Returns the per-port supported offloads.
99 * Pointer to Ethernet device.
102 * Supported Tx offloads.
105 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
107 struct mlx5_priv *priv = dev->data->dev_private;
108 uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
109 DEV_TX_OFFLOAD_VLAN_INSERT);
110 struct mlx5_dev_config *config = &priv->config;
113 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
114 DEV_TX_OFFLOAD_UDP_CKSUM |
115 DEV_TX_OFFLOAD_TCP_CKSUM);
117 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
120 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
122 offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
123 DEV_TX_OFFLOAD_UDP_TNL_TSO);
125 if (config->tunnel_en) {
127 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
129 offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
130 DEV_TX_OFFLOAD_GRE_TNL_TSO |
131 DEV_TX_OFFLOAD_GENEVE_TNL_TSO);
137 * Tx queue presetup checks.
140 * Pointer to Ethernet device structure.
144 * Number of descriptors to configure in queue.
147 * 0 on success, a negative errno value otherwise and rte_errno is set.
150 mlx5_tx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc)
152 struct mlx5_priv *priv = dev->data->dev_private;
154 if (desc <= MLX5_TX_COMP_THRESH) {
156 "port %u number of descriptors requested for Tx queue"
157 " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
159 dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
160 desc = MLX5_TX_COMP_THRESH + 1;
162 if (!rte_is_power_of_2(desc)) {
163 desc = 1 << log2above(desc);
165 "port %u increased number of descriptors in Tx queue"
166 " %u to the next power of two (%d)",
167 dev->data->port_id, idx, desc);
169 DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
170 dev->data->port_id, idx, desc);
171 if (idx >= priv->txqs_n) {
172 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
173 dev->data->port_id, idx, priv->txqs_n);
174 rte_errno = EOVERFLOW;
177 if (!mlx5_txq_releasable(dev, idx)) {
179 DRV_LOG(ERR, "port %u unable to release queue index %u",
180 dev->data->port_id, idx);
183 mlx5_txq_release(dev, idx);
187 * DPDK callback to configure a TX queue.
190 * Pointer to Ethernet device structure.
194 * Number of descriptors to configure in queue.
196 * NUMA socket on which memory must be allocated.
198 * Thresholds parameters.
201 * 0 on success, a negative errno value otherwise and rte_errno is set.
204 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
205 unsigned int socket, const struct rte_eth_txconf *conf)
207 struct mlx5_priv *priv = dev->data->dev_private;
208 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
209 struct mlx5_txq_ctrl *txq_ctrl =
210 container_of(txq, struct mlx5_txq_ctrl, txq);
213 res = mlx5_tx_queue_pre_setup(dev, idx, desc);
216 txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
218 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
219 dev->data->port_id, idx);
222 DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
223 dev->data->port_id, idx);
224 (*priv->txqs)[idx] = &txq_ctrl->txq;
229 * DPDK callback to configure a TX hairpin queue.
232 * Pointer to Ethernet device structure.
236 * Number of descriptors to configure in queue.
237 * @param[in] hairpin_conf
238 * The hairpin binding configuration.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_tx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
246 const struct rte_eth_hairpin_conf *hairpin_conf)
248 struct mlx5_priv *priv = dev->data->dev_private;
249 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
250 struct mlx5_txq_ctrl *txq_ctrl =
251 container_of(txq, struct mlx5_txq_ctrl, txq);
254 res = mlx5_tx_queue_pre_setup(dev, idx, desc);
257 if (hairpin_conf->peer_count != 1 ||
258 hairpin_conf->peers[0].port != dev->data->port_id ||
259 hairpin_conf->peers[0].queue >= priv->rxqs_n) {
260 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
261 " invalid hairpind configuration", dev->data->port_id,
266 txq_ctrl = mlx5_txq_hairpin_new(dev, idx, desc, hairpin_conf);
268 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
269 dev->data->port_id, idx);
272 DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
273 dev->data->port_id, idx);
274 (*priv->txqs)[idx] = &txq_ctrl->txq;
279 * DPDK callback to release a TX queue.
282 * Generic TX queue pointer.
285 mlx5_tx_queue_release(void *dpdk_txq)
287 struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
288 struct mlx5_txq_ctrl *txq_ctrl;
289 struct mlx5_priv *priv;
294 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
295 priv = txq_ctrl->priv;
296 for (i = 0; (i != priv->txqs_n); ++i)
297 if ((*priv->txqs)[i] == txq) {
298 mlx5_txq_release(ETH_DEV(priv), i);
299 DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
300 PORT_ID(priv), txq->idx);
306 * Configure the doorbell register non-cached attribute.
309 * Pointer to Tx queue control structure.
314 txq_uar_ncattr_init(struct mlx5_txq_ctrl *txq_ctrl, size_t page_size)
316 struct mlx5_priv *priv = txq_ctrl->priv;
319 txq_ctrl->txq.db_heu = priv->config.dbnc == MLX5_TXDB_HEURISTIC;
320 txq_ctrl->txq.db_nc = 0;
321 /* Check the doorbell register mapping type. */
322 cmd = txq_ctrl->uar_mmap_offset / page_size;
323 cmd >>= MLX5_UAR_MMAP_CMD_SHIFT;
324 cmd &= MLX5_UAR_MMAP_CMD_MASK;
325 if (cmd == MLX5_MMAP_GET_NC_PAGES_CMD)
326 txq_ctrl->txq.db_nc = 1;
330 * Initialize Tx UAR registers for primary process.
333 * Pointer to Tx queue control structure.
336 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
338 struct mlx5_priv *priv = txq_ctrl->priv;
339 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
340 const size_t page_size = sysconf(_SC_PAGESIZE);
342 unsigned int lock_idx;
345 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
347 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
349 ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
350 txq_uar_ncattr_init(txq_ctrl, page_size);
352 /* Assign an UAR lock according to UAR page number */
353 lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
354 MLX5_UAR_PAGE_NUM_MASK;
355 txq_ctrl->txq.uar_lock = &priv->uar_lock[lock_idx];
360 * Remap UAR register of a Tx queue for secondary process.
362 * Remapped address is stored at the table in the process private structure of
363 * the device, indexed by queue index.
366 * Pointer to Tx queue control structure.
368 * Verbs file descriptor to map UAR pages.
371 * 0 on success, a negative errno value otherwise and rte_errno is set.
374 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
376 struct mlx5_priv *priv = txq_ctrl->priv;
377 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
378 struct mlx5_txq_data *txq = &txq_ctrl->txq;
382 const size_t page_size = sysconf(_SC_PAGESIZE);
384 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
388 * As rdma-core, UARs are mapped in size of OS page
389 * size. Ref to libmlx5 function: mlx5_init_context()
391 uar_va = (uintptr_t)txq_ctrl->bf_reg;
392 offset = uar_va & (page_size - 1); /* Offset in page. */
393 addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
394 txq_ctrl->uar_mmap_offset);
395 if (addr == MAP_FAILED) {
397 "port %u mmap failed for BF reg of txq %u",
398 txq->port_id, txq->idx);
402 addr = RTE_PTR_ADD(addr, offset);
403 ppriv->uar_table[txq->idx] = addr;
404 txq_uar_ncattr_init(txq_ctrl, page_size);
409 * Unmap UAR register of a Tx queue for secondary process.
412 * Pointer to Tx queue control structure.
415 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
417 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
418 const size_t page_size = sysconf(_SC_PAGESIZE);
421 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
423 addr = ppriv->uar_table[txq_ctrl->txq.idx];
424 munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
428 * Initialize Tx UAR registers for secondary process.
431 * Pointer to Ethernet device.
433 * Verbs file descriptor to map UAR pages.
436 * 0 on success, a negative errno value otherwise and rte_errno is set.
439 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
441 struct mlx5_priv *priv = dev->data->dev_private;
442 struct mlx5_txq_data *txq;
443 struct mlx5_txq_ctrl *txq_ctrl;
447 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
448 for (i = 0; i != priv->txqs_n; ++i) {
449 if (!(*priv->txqs)[i])
451 txq = (*priv->txqs)[i];
452 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
453 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
455 assert(txq->idx == (uint16_t)i);
456 ret = txq_uar_init_secondary(txq_ctrl, fd);
464 if (!(*priv->txqs)[i])
466 txq = (*priv->txqs)[i];
467 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
468 txq_uar_uninit_secondary(txq_ctrl);
474 * Create the Tx hairpin queue object.
477 * Pointer to Ethernet device.
479 * Queue index in DPDK Tx queue array
482 * The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
484 static struct mlx5_txq_obj *
485 mlx5_txq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
487 struct mlx5_priv *priv = dev->data->dev_private;
488 struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
489 struct mlx5_txq_ctrl *txq_ctrl =
490 container_of(txq_data, struct mlx5_txq_ctrl, txq);
491 struct mlx5_devx_create_sq_attr attr = { 0 };
492 struct mlx5_txq_obj *tmpl = NULL;
496 assert(!txq_ctrl->obj);
497 tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
501 "port %u Tx queue %u cannot allocate memory resources",
502 dev->data->port_id, txq_data->idx);
506 tmpl->type = MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN;
507 tmpl->txq_ctrl = txq_ctrl;
510 /* Workaround for hairpin startup */
511 attr.wq_attr.log_hairpin_num_packets = log2above(32);
512 /* Workaround for packets larger than 1KB */
513 attr.wq_attr.log_hairpin_data_sz =
514 priv->config.hca_attr.log_max_hairpin_wq_data_sz;
515 attr.tis_num = priv->sh->tis->id;
516 tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->ctx, &attr);
519 "port %u tx hairpin queue %u can't create sq object",
520 dev->data->port_id, idx);
524 DRV_LOG(DEBUG, "port %u sxq %u updated with %p", dev->data->port_id,
526 rte_atomic32_inc(&tmpl->refcnt);
527 LIST_INSERT_HEAD(&priv->txqsobj, tmpl, next);
530 ret = rte_errno; /* Save rte_errno before cleanup. */
532 mlx5_devx_cmd_destroy(tmpl->tis);
534 mlx5_devx_cmd_destroy(tmpl->sq);
535 rte_errno = ret; /* Restore rte_errno. */
540 * Create the Tx queue Verbs object.
543 * Pointer to Ethernet device.
545 * Queue index in DPDK Tx queue array.
547 * Type of the Tx queue object to create.
550 * The Verbs object initialised, NULL otherwise and rte_errno is set.
552 struct mlx5_txq_obj *
553 mlx5_txq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
554 enum mlx5_txq_obj_type type)
556 struct mlx5_priv *priv = dev->data->dev_private;
557 struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
558 struct mlx5_txq_ctrl *txq_ctrl =
559 container_of(txq_data, struct mlx5_txq_ctrl, txq);
560 struct mlx5_txq_obj tmpl;
561 struct mlx5_txq_obj *txq_obj = NULL;
563 struct ibv_qp_init_attr_ex init;
564 struct ibv_cq_init_attr_ex cq;
565 struct ibv_qp_attr mod;
568 struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
569 struct mlx5dv_cq cq_info;
570 struct mlx5dv_obj obj;
571 const int desc = 1 << txq_data->elts_n;
574 if (type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN)
575 return mlx5_txq_obj_hairpin_new(dev, idx);
576 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
577 /* If using DevX, need additional mask to read tisn value. */
578 if (priv->config.devx && !priv->sh->tdn)
579 qp.comp_mask |= MLX5DV_QP_MASK_RAW_QP_HANDLES;
582 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
583 priv->verbs_alloc_ctx.obj = txq_ctrl;
584 if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
586 "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
591 memset(&tmpl, 0, sizeof(struct mlx5_txq_obj));
592 attr.cq = (struct ibv_cq_init_attr_ex){
595 cqe_n = desc / MLX5_TX_COMP_THRESH +
596 1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
597 tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
598 if (tmpl.cq == NULL) {
599 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
600 dev->data->port_id, idx);
604 attr.init = (struct ibv_qp_init_attr_ex){
605 /* CQ to be associated with the send queue. */
607 /* CQ to be associated with the receive queue. */
610 /* Max number of outstanding WRs. */
612 ((priv->sh->device_attr.orig_attr.max_qp_wr <
614 priv->sh->device_attr.orig_attr.max_qp_wr :
617 * Max number of scatter/gather elements in a WR,
618 * must be 1 to prevent libmlx5 from trying to affect
619 * too much memory. TX gather is not impacted by the
620 * device_attr.max_sge limit and will still work
625 .qp_type = IBV_QPT_RAW_PACKET,
627 * Do *NOT* enable this, completions events are managed per
632 .comp_mask = IBV_QP_INIT_ATTR_PD,
634 if (txq_data->inlen_send)
635 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
636 if (txq_data->tso_en) {
637 attr.init.max_tso_header = txq_ctrl->max_tso_header;
638 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
640 tmpl.qp = mlx5_glue->create_qp_ex(priv->sh->ctx, &attr.init);
641 if (tmpl.qp == NULL) {
642 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
643 dev->data->port_id, idx);
647 attr.mod = (struct ibv_qp_attr){
648 /* Move the QP to this state. */
649 .qp_state = IBV_QPS_INIT,
650 /* IB device port number. */
651 .port_num = (uint8_t)priv->ibv_port,
653 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
654 (IBV_QP_STATE | IBV_QP_PORT));
657 "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
658 dev->data->port_id, idx);
662 attr.mod = (struct ibv_qp_attr){
663 .qp_state = IBV_QPS_RTR
665 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
668 "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
669 dev->data->port_id, idx);
673 attr.mod.qp_state = IBV_QPS_RTS;
674 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
677 "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
678 dev->data->port_id, idx);
682 txq_obj = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_obj), 0,
685 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
686 dev->data->port_id, idx);
691 obj.cq.out = &cq_info;
694 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
699 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
701 "port %u wrong MLX5_CQE_SIZE environment variable"
702 " value: it should be set to %u",
703 dev->data->port_id, RTE_CACHE_LINE_SIZE);
707 txq_data->cqe_n = log2above(cq_info.cqe_cnt);
708 txq_data->cqe_s = 1 << txq_data->cqe_n;
709 txq_data->cqe_m = txq_data->cqe_s - 1;
710 txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
711 txq_data->wqes = qp.sq.buf;
712 txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
713 txq_data->wqe_s = 1 << txq_data->wqe_n;
714 txq_data->wqe_m = txq_data->wqe_s - 1;
715 txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
716 txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
717 txq_data->cq_db = cq_info.dbrec;
718 txq_data->cqes = (volatile struct mlx5_cqe *)cq_info.buf;
721 txq_data->wqe_ci = 0;
722 txq_data->wqe_pi = 0;
723 txq_data->wqe_comp = 0;
724 txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
725 txq_data->fcqs = rte_calloc_socket(__func__,
727 sizeof(*txq_data->fcqs),
730 if (!txq_data->fcqs) {
731 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory (FCQ)",
732 dev->data->port_id, idx);
736 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
738 * If using DevX need to query and store TIS transport domain value.
739 * This is done once per port.
740 * Will use this value on Rx, when creating matching TIR.
742 if (priv->config.devx && !priv->sh->tdn) {
743 ret = mlx5_devx_cmd_qp_query_tis_td(tmpl.qp, qp.tisn,
746 DRV_LOG(ERR, "Fail to query port %u Tx queue %u QP TIS "
747 "transport domain", dev->data->port_id, idx);
751 DRV_LOG(DEBUG, "port %u Tx queue %u TIS number %d "
752 "transport domain %d", dev->data->port_id,
753 idx, qp.tisn, priv->sh->tdn);
757 txq_obj->qp = tmpl.qp;
758 txq_obj->cq = tmpl.cq;
759 rte_atomic32_inc(&txq_obj->refcnt);
760 txq_ctrl->bf_reg = qp.bf.reg;
761 if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
762 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
763 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%"PRIx64,
764 dev->data->port_id, txq_ctrl->uar_mmap_offset);
767 "port %u failed to retrieve UAR info, invalid"
773 txq_uar_init(txq_ctrl);
774 LIST_INSERT_HEAD(&priv->txqsobj, txq_obj, next);
775 txq_obj->txq_ctrl = txq_ctrl;
776 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
779 ret = rte_errno; /* Save rte_errno before cleanup. */
781 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
783 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
784 if (txq_data && txq_data->fcqs)
785 rte_free(txq_data->fcqs);
788 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
789 rte_errno = ret; /* Restore rte_errno. */
794 * Get an Tx queue Verbs object.
797 * Pointer to Ethernet device.
799 * Queue index in DPDK Tx queue array.
802 * The Verbs object if it exists.
804 struct mlx5_txq_obj *
805 mlx5_txq_obj_get(struct rte_eth_dev *dev, uint16_t idx)
807 struct mlx5_priv *priv = dev->data->dev_private;
808 struct mlx5_txq_ctrl *txq_ctrl;
810 if (idx >= priv->txqs_n)
812 if (!(*priv->txqs)[idx])
814 txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
816 rte_atomic32_inc(&txq_ctrl->obj->refcnt);
817 return txq_ctrl->obj;
821 * Release an Tx verbs queue object.
824 * Verbs Tx queue object.
827 * 1 while a reference on it exists, 0 when freed.
830 mlx5_txq_obj_release(struct mlx5_txq_obj *txq_obj)
833 if (rte_atomic32_dec_and_test(&txq_obj->refcnt)) {
834 if (txq_obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN) {
836 claim_zero(mlx5_devx_cmd_destroy(txq_obj->tis));
838 claim_zero(mlx5_glue->destroy_qp(txq_obj->qp));
839 claim_zero(mlx5_glue->destroy_cq(txq_obj->cq));
840 if (txq_obj->txq_ctrl->txq.fcqs)
841 rte_free(txq_obj->txq_ctrl->txq.fcqs);
843 LIST_REMOVE(txq_obj, next);
851 * Verify the Verbs Tx queue list is empty
854 * Pointer to Ethernet device.
857 * The number of object not released.
860 mlx5_txq_obj_verify(struct rte_eth_dev *dev)
862 struct mlx5_priv *priv = dev->data->dev_private;
864 struct mlx5_txq_obj *txq_obj;
866 LIST_FOREACH(txq_obj, &priv->txqsobj, next) {
867 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
868 dev->data->port_id, txq_obj->txq_ctrl->txq.idx);
875 * Calculate the total number of WQEBB for Tx queue.
877 * Simplified version of calc_sq_size() in rdma-core.
880 * Pointer to Tx queue control structure.
883 * The number of WQEBB.
886 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
888 unsigned int wqe_size;
889 const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
891 wqe_size = MLX5_WQE_CSEG_SIZE +
894 MLX5_ESEG_MIN_INLINE_SIZE +
895 txq_ctrl->max_inline_data;
896 return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
900 * Calculate the maximal inline data size for Tx queue.
903 * Pointer to Tx queue control structure.
906 * The maximal inline data size.
909 txq_calc_inline_max(struct mlx5_txq_ctrl *txq_ctrl)
911 const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
912 struct mlx5_priv *priv = txq_ctrl->priv;
913 unsigned int wqe_size;
915 wqe_size = priv->sh->device_attr.orig_attr.max_qp_wr / desc;
919 * This calculation is derived from tthe source of
920 * mlx5_calc_send_wqe() in rdma_core library.
922 wqe_size = wqe_size * MLX5_WQE_SIZE -
927 MLX5_DSEG_MIN_INLINE_SIZE;
932 * Set Tx queue parameters from device configuration.
935 * Pointer to Tx queue control structure.
938 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
940 struct mlx5_priv *priv = txq_ctrl->priv;
941 struct mlx5_dev_config *config = &priv->config;
942 unsigned int inlen_send; /* Inline data for ordinary SEND.*/
943 unsigned int inlen_empw; /* Inline data for enhanced MPW. */
944 unsigned int inlen_mode; /* Minimal required Inline data. */
945 unsigned int txqs_inline; /* Min Tx queues to enable inline. */
946 uint64_t dev_txoff = priv->dev_data->dev_conf.txmode.offloads;
947 bool tso = txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
948 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
949 DEV_TX_OFFLOAD_GRE_TNL_TSO |
950 DEV_TX_OFFLOAD_IP_TNL_TSO |
951 DEV_TX_OFFLOAD_UDP_TNL_TSO);
955 if (config->txqs_inline == MLX5_ARG_UNSET)
957 #if defined(RTE_ARCH_ARM64)
958 (priv->pci_dev->id.device_id ==
959 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF) ?
960 MLX5_INLINE_MAX_TXQS_BLUEFIELD :
962 MLX5_INLINE_MAX_TXQS;
964 txqs_inline = (unsigned int)config->txqs_inline;
965 inlen_send = (config->txq_inline_max == MLX5_ARG_UNSET) ?
966 MLX5_SEND_DEF_INLINE_LEN :
967 (unsigned int)config->txq_inline_max;
968 inlen_empw = (config->txq_inline_mpw == MLX5_ARG_UNSET) ?
969 MLX5_EMPW_DEF_INLINE_LEN :
970 (unsigned int)config->txq_inline_mpw;
971 inlen_mode = (config->txq_inline_min == MLX5_ARG_UNSET) ?
972 0 : (unsigned int)config->txq_inline_min;
973 if (config->mps != MLX5_MPW_ENHANCED && config->mps != MLX5_MPW)
976 * If there is requested minimal amount of data to inline
977 * we MUST enable inlining. This is a case for ConnectX-4
978 * which usually requires L2 inlined for correct operating
979 * and ConnectX-4LX which requires L2-L4 inlined to
980 * support E-Switch Flows.
983 if (inlen_mode <= MLX5_ESEG_MIN_INLINE_SIZE) {
985 * Optimize minimal inlining for single
986 * segment packets to fill one WQEBB
989 temp = MLX5_ESEG_MIN_INLINE_SIZE;
991 temp = inlen_mode - MLX5_ESEG_MIN_INLINE_SIZE;
992 temp = RTE_ALIGN(temp, MLX5_WSEG_SIZE) +
993 MLX5_ESEG_MIN_INLINE_SIZE;
994 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
996 if (temp != inlen_mode) {
998 "port %u minimal required inline setting"
999 " aligned from %u to %u",
1000 PORT_ID(priv), inlen_mode, temp);
1005 * If port is configured to support VLAN insertion and device
1006 * does not support this feature by HW (for NICs before ConnectX-5
1007 * or in case of wqe_vlan_insert flag is not set) we must enable
1008 * data inline on all queues because it is supported by single
1011 txq_ctrl->txq.vlan_en = config->hw_vlan_insert;
1012 vlan_inline = (dev_txoff & DEV_TX_OFFLOAD_VLAN_INSERT) &&
1013 !config->hw_vlan_insert;
1015 * If there are few Tx queues it is prioritized
1016 * to save CPU cycles and disable data inlining at all.
1018 if (inlen_send && priv->txqs_n >= txqs_inline) {
1020 * The data sent with ordinal MLX5_OPCODE_SEND
1021 * may be inlined in Ethernet Segment, align the
1022 * length accordingly to fit entire WQEBBs.
1024 temp = RTE_MAX(inlen_send,
1025 MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE);
1026 temp -= MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1027 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1028 temp += MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1029 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1030 MLX5_ESEG_MIN_INLINE_SIZE -
1031 MLX5_WQE_CSEG_SIZE -
1032 MLX5_WQE_ESEG_SIZE -
1033 MLX5_WQE_DSEG_SIZE * 2);
1034 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
1035 temp = RTE_MAX(temp, inlen_mode);
1036 if (temp != inlen_send) {
1038 "port %u ordinary send inline setting"
1039 " aligned from %u to %u",
1040 PORT_ID(priv), inlen_send, temp);
1044 * Not aligned to cache lines, but to WQEs.
1045 * First bytes of data (initial alignment)
1046 * is going to be copied explicitly at the
1047 * beginning of inlining buffer in Ethernet
1050 assert(inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
1051 assert(inlen_send <= MLX5_WQE_SIZE_MAX +
1052 MLX5_ESEG_MIN_INLINE_SIZE -
1053 MLX5_WQE_CSEG_SIZE -
1054 MLX5_WQE_ESEG_SIZE -
1055 MLX5_WQE_DSEG_SIZE * 2);
1056 } else if (inlen_mode) {
1058 * If minimal inlining is requested we must
1059 * enable inlining in general, despite the
1060 * number of configured queues. Ignore the
1061 * txq_inline_max devarg, this is not
1062 * full-featured inline.
1064 inlen_send = inlen_mode;
1066 } else if (vlan_inline) {
1068 * Hardware does not report offload for
1069 * VLAN insertion, we must enable data inline
1070 * to implement feature by software.
1072 inlen_send = MLX5_ESEG_MIN_INLINE_SIZE;
1078 txq_ctrl->txq.inlen_send = inlen_send;
1079 txq_ctrl->txq.inlen_mode = inlen_mode;
1080 txq_ctrl->txq.inlen_empw = 0;
1081 if (inlen_send && inlen_empw && priv->txqs_n >= txqs_inline) {
1083 * The data sent with MLX5_OPCODE_ENHANCED_MPSW
1084 * may be inlined in Data Segment, align the
1085 * length accordingly to fit entire WQEBBs.
1087 temp = RTE_MAX(inlen_empw,
1088 MLX5_WQE_SIZE + MLX5_DSEG_MIN_INLINE_SIZE);
1089 temp -= MLX5_DSEG_MIN_INLINE_SIZE;
1090 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1091 temp += MLX5_DSEG_MIN_INLINE_SIZE;
1092 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1093 MLX5_DSEG_MIN_INLINE_SIZE -
1094 MLX5_WQE_CSEG_SIZE -
1095 MLX5_WQE_ESEG_SIZE -
1096 MLX5_WQE_DSEG_SIZE);
1097 temp = RTE_MIN(temp, MLX5_EMPW_MAX_INLINE_LEN);
1098 if (temp != inlen_empw) {
1100 "port %u enhanced empw inline setting"
1101 " aligned from %u to %u",
1102 PORT_ID(priv), inlen_empw, temp);
1105 assert(inlen_empw >= MLX5_ESEG_MIN_INLINE_SIZE);
1106 assert(inlen_empw <= MLX5_WQE_SIZE_MAX +
1107 MLX5_DSEG_MIN_INLINE_SIZE -
1108 MLX5_WQE_CSEG_SIZE -
1109 MLX5_WQE_ESEG_SIZE -
1110 MLX5_WQE_DSEG_SIZE);
1111 txq_ctrl->txq.inlen_empw = inlen_empw;
1113 txq_ctrl->max_inline_data = RTE_MAX(inlen_send, inlen_empw);
1115 txq_ctrl->max_tso_header = MLX5_MAX_TSO_HEADER;
1116 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->max_inline_data,
1117 MLX5_MAX_TSO_HEADER);
1118 txq_ctrl->txq.tso_en = 1;
1120 txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
1121 txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
1122 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1123 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
1124 txq_ctrl->txq.offloads) && config->swp;
1128 * Adjust Tx queue data inline parameters for large queue sizes.
1129 * The data inline feature requires multiple WQEs to fit the packets,
1130 * and if the large amount of Tx descriptors is requested by application
1131 * the total WQE amount may exceed the hardware capabilities. If the
1132 * default inline setting are used we can try to adjust these ones and
1133 * meet the hardware requirements and not exceed the queue size.
1136 * Pointer to Tx queue control structure.
1139 * Zero on success, otherwise the parameters can not be adjusted.
1142 txq_adjust_params(struct mlx5_txq_ctrl *txq_ctrl)
1144 struct mlx5_priv *priv = txq_ctrl->priv;
1145 struct mlx5_dev_config *config = &priv->config;
1146 unsigned int max_inline;
1148 max_inline = txq_calc_inline_max(txq_ctrl);
1149 if (!txq_ctrl->txq.inlen_send) {
1151 * Inline data feature is not engaged at all.
1152 * There is nothing to adjust.
1156 if (txq_ctrl->max_inline_data <= max_inline) {
1158 * The requested inline data length does not
1159 * exceed queue capabilities.
1163 if (txq_ctrl->txq.inlen_mode > max_inline) {
1165 "minimal data inline requirements (%u) are not"
1166 " satisfied (%u) on port %u, try the smaller"
1167 " Tx queue size (%d)",
1168 txq_ctrl->txq.inlen_mode, max_inline,
1169 priv->dev_data->port_id,
1170 priv->sh->device_attr.orig_attr.max_qp_wr);
1173 if (txq_ctrl->txq.inlen_send > max_inline &&
1174 config->txq_inline_max != MLX5_ARG_UNSET &&
1175 config->txq_inline_max > (int)max_inline) {
1177 "txq_inline_max requirements (%u) are not"
1178 " satisfied (%u) on port %u, try the smaller"
1179 " Tx queue size (%d)",
1180 txq_ctrl->txq.inlen_send, max_inline,
1181 priv->dev_data->port_id,
1182 priv->sh->device_attr.orig_attr.max_qp_wr);
1185 if (txq_ctrl->txq.inlen_empw > max_inline &&
1186 config->txq_inline_mpw != MLX5_ARG_UNSET &&
1187 config->txq_inline_mpw > (int)max_inline) {
1189 "txq_inline_mpw requirements (%u) are not"
1190 " satisfied (%u) on port %u, try the smaller"
1191 " Tx queue size (%d)",
1192 txq_ctrl->txq.inlen_empw, max_inline,
1193 priv->dev_data->port_id,
1194 priv->sh->device_attr.orig_attr.max_qp_wr);
1197 if (txq_ctrl->txq.tso_en && max_inline < MLX5_MAX_TSO_HEADER) {
1199 "tso header inline requirements (%u) are not"
1200 " satisfied (%u) on port %u, try the smaller"
1201 " Tx queue size (%d)",
1202 MLX5_MAX_TSO_HEADER, max_inline,
1203 priv->dev_data->port_id,
1204 priv->sh->device_attr.orig_attr.max_qp_wr);
1207 if (txq_ctrl->txq.inlen_send > max_inline) {
1209 "adjust txq_inline_max (%u->%u)"
1210 " due to large Tx queue on port %u",
1211 txq_ctrl->txq.inlen_send, max_inline,
1212 priv->dev_data->port_id);
1213 txq_ctrl->txq.inlen_send = max_inline;
1215 if (txq_ctrl->txq.inlen_empw > max_inline) {
1217 "adjust txq_inline_mpw (%u->%u)"
1218 "due to large Tx queue on port %u",
1219 txq_ctrl->txq.inlen_empw, max_inline,
1220 priv->dev_data->port_id);
1221 txq_ctrl->txq.inlen_empw = max_inline;
1223 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->txq.inlen_send,
1224 txq_ctrl->txq.inlen_empw);
1225 assert(txq_ctrl->max_inline_data <= max_inline);
1226 assert(txq_ctrl->txq.inlen_mode <= max_inline);
1227 assert(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_send);
1228 assert(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_empw ||
1229 !txq_ctrl->txq.inlen_empw);
1237 * Create a DPDK Tx queue.
1240 * Pointer to Ethernet device.
1244 * Number of descriptors to configure in queue.
1246 * NUMA socket on which memory must be allocated.
1248 * Thresholds parameters.
1251 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1253 struct mlx5_txq_ctrl *
1254 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1255 unsigned int socket, const struct rte_eth_txconf *conf)
1257 struct mlx5_priv *priv = dev->data->dev_private;
1258 struct mlx5_txq_ctrl *tmpl;
1260 tmpl = rte_calloc_socket("TXQ", 1,
1262 desc * sizeof(struct rte_mbuf *),
1268 if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
1269 MLX5_MR_BTREE_CACHE_N, socket)) {
1270 /* rte_errno is already set. */
1273 /* Save pointer of global generation number to check memory event. */
1274 tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->mr.dev_gen;
1275 assert(desc > MLX5_TX_COMP_THRESH);
1276 tmpl->txq.offloads = conf->offloads |
1277 dev->data->dev_conf.txmode.offloads;
1279 tmpl->socket = socket;
1280 tmpl->txq.elts_n = log2above(desc);
1281 tmpl->txq.elts_s = desc;
1282 tmpl->txq.elts_m = desc - 1;
1283 tmpl->txq.port_id = dev->data->port_id;
1284 tmpl->txq.idx = idx;
1285 txq_set_params(tmpl);
1286 if (txq_adjust_params(tmpl))
1288 if (txq_calc_wqebb_cnt(tmpl) >
1289 priv->sh->device_attr.orig_attr.max_qp_wr) {
1291 "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
1292 " try smaller queue size",
1293 dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
1294 priv->sh->device_attr.orig_attr.max_qp_wr);
1298 rte_atomic32_inc(&tmpl->refcnt);
1299 tmpl->type = MLX5_TXQ_TYPE_STANDARD;
1300 LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1308 * Create a DPDK Tx hairpin queue.
1311 * Pointer to Ethernet device.
1315 * Number of descriptors to configure in queue.
1316 * @param hairpin_conf
1317 * The hairpin configuration.
1320 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1322 struct mlx5_txq_ctrl *
1323 mlx5_txq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1324 const struct rte_eth_hairpin_conf *hairpin_conf)
1326 struct mlx5_priv *priv = dev->data->dev_private;
1327 struct mlx5_txq_ctrl *tmpl;
1329 tmpl = rte_calloc_socket("TXQ", 1,
1330 sizeof(*tmpl), 0, SOCKET_ID_ANY);
1336 tmpl->socket = SOCKET_ID_ANY;
1337 tmpl->txq.elts_n = log2above(desc);
1338 tmpl->txq.port_id = dev->data->port_id;
1339 tmpl->txq.idx = idx;
1340 tmpl->hairpin_conf = *hairpin_conf;
1341 tmpl->type = MLX5_TXQ_TYPE_HAIRPIN;
1342 rte_atomic32_inc(&tmpl->refcnt);
1343 LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1351 * Pointer to Ethernet device.
1356 * A pointer to the queue if it exists.
1358 struct mlx5_txq_ctrl *
1359 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
1361 struct mlx5_priv *priv = dev->data->dev_private;
1362 struct mlx5_txq_ctrl *ctrl = NULL;
1364 if ((*priv->txqs)[idx]) {
1365 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
1367 mlx5_txq_obj_get(dev, idx);
1368 rte_atomic32_inc(&ctrl->refcnt);
1374 * Release a Tx queue.
1377 * Pointer to Ethernet device.
1382 * 1 while a reference on it exists, 0 when freed.
1385 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
1387 struct mlx5_priv *priv = dev->data->dev_private;
1388 struct mlx5_txq_ctrl *txq;
1390 if (!(*priv->txqs)[idx])
1392 txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1393 if (txq->obj && !mlx5_txq_obj_release(txq->obj))
1395 if (rte_atomic32_dec_and_test(&txq->refcnt)) {
1397 mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
1398 LIST_REMOVE(txq, next);
1400 (*priv->txqs)[idx] = NULL;
1407 * Verify if the queue can be released.
1410 * Pointer to Ethernet device.
1415 * 1 if the queue can be released.
1418 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
1420 struct mlx5_priv *priv = dev->data->dev_private;
1421 struct mlx5_txq_ctrl *txq;
1423 if (!(*priv->txqs)[idx])
1425 txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1426 return (rte_atomic32_read(&txq->refcnt) == 1);
1430 * Verify the Tx Queue list is empty
1433 * Pointer to Ethernet device.
1436 * The number of object not released.
1439 mlx5_txq_verify(struct rte_eth_dev *dev)
1441 struct mlx5_priv *priv = dev->data->dev_private;
1442 struct mlx5_txq_ctrl *txq_ctrl;
1445 LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
1446 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
1447 dev->data->port_id, txq_ctrl->txq.idx);