1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
17 #pragma GCC diagnostic ignored "-Wpedantic"
19 #include <infiniband/verbs.h>
21 #pragma GCC diagnostic error "-Wpedantic"
25 #include <rte_malloc.h>
26 #include <rte_ethdev_driver.h>
27 #include <rte_common.h>
29 #include "mlx5_utils.h"
30 #include "mlx5_defs.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_autoconf.h"
34 #include "mlx5_glue.h"
37 * Allocate TX queue elements.
40 * Pointer to TX queue structure.
43 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
45 const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
48 for (i = 0; (i != elts_n); ++i)
49 (*txq_ctrl->txq.elts)[i] = NULL;
50 DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
51 PORT_ID(txq_ctrl->priv), txq_ctrl->idx, elts_n);
52 txq_ctrl->txq.elts_head = 0;
53 txq_ctrl->txq.elts_tail = 0;
54 txq_ctrl->txq.elts_comp = 0;
58 * Free TX queue elements.
61 * Pointer to TX queue structure.
64 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
66 const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
67 const uint16_t elts_m = elts_n - 1;
68 uint16_t elts_head = txq_ctrl->txq.elts_head;
69 uint16_t elts_tail = txq_ctrl->txq.elts_tail;
70 struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
72 DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
73 PORT_ID(txq_ctrl->priv), txq_ctrl->idx);
74 txq_ctrl->txq.elts_head = 0;
75 txq_ctrl->txq.elts_tail = 0;
76 txq_ctrl->txq.elts_comp = 0;
78 while (elts_tail != elts_head) {
79 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
82 rte_pktmbuf_free_seg(elt);
85 memset(&(*elts)[elts_tail & elts_m],
87 sizeof((*elts)[elts_tail & elts_m]));
94 * Returns the per-port supported offloads.
97 * Pointer to Ethernet device.
100 * Supported Tx offloads.
103 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
105 struct mlx5_priv *priv = dev->data->dev_private;
106 uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
107 DEV_TX_OFFLOAD_VLAN_INSERT);
108 struct mlx5_dev_config *config = &priv->config;
111 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
112 DEV_TX_OFFLOAD_UDP_CKSUM |
113 DEV_TX_OFFLOAD_TCP_CKSUM);
115 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
118 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
120 offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
121 DEV_TX_OFFLOAD_UDP_TNL_TSO);
123 if (config->tunnel_en) {
125 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
127 offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
128 DEV_TX_OFFLOAD_GRE_TNL_TSO);
130 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
131 if (config->dv_flow_en)
132 offloads |= DEV_TX_OFFLOAD_MATCH_METADATA;
138 * DPDK callback to configure a TX queue.
141 * Pointer to Ethernet device structure.
145 * Number of descriptors to configure in queue.
147 * NUMA socket on which memory must be allocated.
149 * Thresholds parameters.
152 * 0 on success, a negative errno value otherwise and rte_errno is set.
155 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
156 unsigned int socket, const struct rte_eth_txconf *conf)
158 struct mlx5_priv *priv = dev->data->dev_private;
159 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
160 struct mlx5_txq_ctrl *txq_ctrl =
161 container_of(txq, struct mlx5_txq_ctrl, txq);
163 if (desc <= MLX5_TX_COMP_THRESH) {
165 "port %u number of descriptors requested for Tx queue"
166 " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
168 dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
169 desc = MLX5_TX_COMP_THRESH + 1;
171 if (!rte_is_power_of_2(desc)) {
172 desc = 1 << log2above(desc);
174 "port %u increased number of descriptors in Tx queue"
175 " %u to the next power of two (%d)",
176 dev->data->port_id, idx, desc);
178 DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
179 dev->data->port_id, idx, desc);
180 if (idx >= priv->txqs_n) {
181 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
182 dev->data->port_id, idx, priv->txqs_n);
183 rte_errno = EOVERFLOW;
186 if (!mlx5_txq_releasable(dev, idx)) {
188 DRV_LOG(ERR, "port %u unable to release queue index %u",
189 dev->data->port_id, idx);
192 mlx5_txq_release(dev, idx);
193 txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
195 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
196 dev->data->port_id, idx);
199 DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
200 dev->data->port_id, idx);
201 (*priv->txqs)[idx] = &txq_ctrl->txq;
206 * DPDK callback to release a TX queue.
209 * Generic TX queue pointer.
212 mlx5_tx_queue_release(void *dpdk_txq)
214 struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
215 struct mlx5_txq_ctrl *txq_ctrl;
216 struct mlx5_priv *priv;
221 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
222 priv = txq_ctrl->priv;
223 for (i = 0; (i != priv->txqs_n); ++i)
224 if ((*priv->txqs)[i] == txq) {
225 mlx5_txq_release(ETH_DEV(priv), i);
226 DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
227 PORT_ID(priv), txq_ctrl->idx);
234 * Mmap TX UAR(HW doorbell) pages into reserved UAR address space.
235 * Both primary and secondary process do mmap to make UAR address
239 * Pointer to Ethernet device.
241 * Verbs file descriptor to map UAR pages.
244 * 0 on success, a negative errno value otherwise and rte_errno is set.
247 mlx5_tx_uar_remap(struct rte_eth_dev *dev, int fd)
249 struct mlx5_priv *priv = dev->data->dev_private;
251 uintptr_t pages[priv->txqs_n];
252 unsigned int pages_n = 0;
257 struct mlx5_txq_data *txq;
258 struct mlx5_txq_ctrl *txq_ctrl;
260 size_t page_size = sysconf(_SC_PAGESIZE);
262 unsigned int lock_idx;
265 memset(pages, 0, priv->txqs_n * sizeof(uintptr_t));
267 * As rdma-core, UARs are mapped in size of OS page size.
268 * Use aligned address to avoid duplicate mmap.
269 * Ref to libmlx5 function: mlx5_init_context()
271 for (i = 0; i != priv->txqs_n; ++i) {
272 if (!(*priv->txqs)[i])
274 txq = (*priv->txqs)[i];
275 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
276 assert(txq_ctrl->idx == (uint16_t)i);
277 /* UAR addr form verbs used to find dup and offset in page. */
278 uar_va = (uintptr_t)txq_ctrl->bf_reg_orig;
279 off = uar_va & (page_size - 1); /* offset in page. */
280 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size); /* page addr. */
282 for (j = 0; j != pages_n; ++j) {
283 if (pages[j] == uar_va) {
288 /* new address in reserved UAR address space. */
289 addr = RTE_PTR_ADD(mlx5_shared_data->uar_base,
290 uar_va & (uintptr_t)(MLX5_UAR_SIZE - 1));
291 if (!already_mapped) {
292 pages[pages_n++] = uar_va;
293 /* fixed mmap to specified address in reserved
296 ret = mmap(addr, page_size,
297 PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
298 txq_ctrl->uar_mmap_offset);
300 /* fixed mmap have to return same address */
302 "port %u call to mmap failed on UAR"
304 dev->data->port_id, txq_ctrl->idx);
309 if (rte_eal_process_type() == RTE_PROC_PRIMARY) /* save once */
310 txq_ctrl->txq.bf_reg = RTE_PTR_ADD((void *)addr, off);
312 assert(txq_ctrl->txq.bf_reg ==
313 RTE_PTR_ADD((void *)addr, off));
315 /* Assign a UAR lock according to UAR page number */
316 lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
317 MLX5_UAR_PAGE_NUM_MASK;
318 txq->uar_lock = &priv->uar_lock[lock_idx];
325 * Check if the burst function is using eMPW.
327 * @param tx_pkt_burst
328 * Tx burst function pointer.
331 * 1 if the burst function is using eMPW, 0 otherwise.
334 is_empw_burst_func(eth_tx_burst_t tx_pkt_burst)
336 if (tx_pkt_burst == mlx5_tx_burst_raw_vec ||
337 tx_pkt_burst == mlx5_tx_burst_vec ||
338 tx_pkt_burst == mlx5_tx_burst_empw)
344 * Create the Tx queue Verbs object.
347 * Pointer to Ethernet device.
349 * Queue index in DPDK Rx queue array
352 * The Verbs object initialised, NULL otherwise and rte_errno is set.
354 struct mlx5_txq_ibv *
355 mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
357 struct mlx5_priv *priv = dev->data->dev_private;
358 struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
359 struct mlx5_txq_ctrl *txq_ctrl =
360 container_of(txq_data, struct mlx5_txq_ctrl, txq);
361 struct mlx5_txq_ibv tmpl;
362 struct mlx5_txq_ibv *txq_ibv;
364 struct ibv_qp_init_attr_ex init;
365 struct ibv_cq_init_attr_ex cq;
366 struct ibv_qp_attr mod;
367 struct ibv_cq_ex cq_attr;
370 struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
371 struct mlx5dv_cq cq_info;
372 struct mlx5dv_obj obj;
373 const int desc = 1 << txq_data->elts_n;
374 eth_tx_burst_t tx_pkt_burst = mlx5_select_tx_function(dev);
378 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
379 priv->verbs_alloc_ctx.obj = txq_ctrl;
380 if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
382 "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
387 memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
388 attr.cq = (struct ibv_cq_init_attr_ex){
391 cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
392 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
393 if (is_empw_burst_func(tx_pkt_burst))
394 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
395 tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
396 if (tmpl.cq == NULL) {
397 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
398 dev->data->port_id, idx);
402 attr.init = (struct ibv_qp_init_attr_ex){
403 /* CQ to be associated with the send queue. */
405 /* CQ to be associated with the receive queue. */
408 /* Max number of outstanding WRs. */
410 ((priv->sh->device_attr.orig_attr.max_qp_wr <
412 priv->sh->device_attr.orig_attr.max_qp_wr :
415 * Max number of scatter/gather elements in a WR,
416 * must be 1 to prevent libmlx5 from trying to affect
417 * too much memory. TX gather is not impacted by the
418 * device_attr.max_sge limit and will still work
423 .qp_type = IBV_QPT_RAW_PACKET,
425 * Do *NOT* enable this, completions events are managed per
430 .comp_mask = IBV_QP_INIT_ATTR_PD,
432 if (txq_data->max_inline)
433 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
434 if (txq_data->tso_en) {
435 attr.init.max_tso_header = txq_ctrl->max_tso_header;
436 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
438 tmpl.qp = mlx5_glue->create_qp_ex(priv->sh->ctx, &attr.init);
439 if (tmpl.qp == NULL) {
440 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
441 dev->data->port_id, idx);
445 attr.mod = (struct ibv_qp_attr){
446 /* Move the QP to this state. */
447 .qp_state = IBV_QPS_INIT,
448 /* IB device port number. */
449 .port_num = (uint8_t)priv->ibv_port,
451 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
452 (IBV_QP_STATE | IBV_QP_PORT));
455 "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
456 dev->data->port_id, idx);
460 attr.mod = (struct ibv_qp_attr){
461 .qp_state = IBV_QPS_RTR
463 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
466 "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
467 dev->data->port_id, idx);
471 attr.mod.qp_state = IBV_QPS_RTS;
472 ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
475 "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
476 dev->data->port_id, idx);
480 txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
483 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
484 dev->data->port_id, idx);
489 obj.cq.out = &cq_info;
492 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
497 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
499 "port %u wrong MLX5_CQE_SIZE environment variable"
500 " value: it should be set to %u",
501 dev->data->port_id, RTE_CACHE_LINE_SIZE);
505 txq_data->cqe_n = log2above(cq_info.cqe_cnt);
506 txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
507 txq_data->wqes = qp.sq.buf;
508 txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
509 txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
510 txq_ctrl->bf_reg_orig = qp.bf.reg;
511 txq_data->cq_db = cq_info.dbrec;
513 (volatile struct mlx5_cqe (*)[])
514 (uintptr_t)cq_info.buf;
519 txq_data->wqe_ci = 0;
520 txq_data->wqe_pi = 0;
521 txq_ibv->qp = tmpl.qp;
522 txq_ibv->cq = tmpl.cq;
523 rte_atomic32_inc(&txq_ibv->refcnt);
524 if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
525 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
526 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%lx",
527 dev->data->port_id, txq_ctrl->uar_mmap_offset);
530 "port %u failed to retrieve UAR info, invalid"
536 LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
537 txq_ibv->txq_ctrl = txq_ctrl;
538 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
541 ret = rte_errno; /* Save rte_errno before cleanup. */
543 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
545 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
546 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
547 rte_errno = ret; /* Restore rte_errno. */
552 * Get an Tx queue Verbs object.
555 * Pointer to Ethernet device.
557 * Queue index in DPDK Rx queue array
560 * The Verbs object if it exists.
562 struct mlx5_txq_ibv *
563 mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
565 struct mlx5_priv *priv = dev->data->dev_private;
566 struct mlx5_txq_ctrl *txq_ctrl;
568 if (idx >= priv->txqs_n)
570 if (!(*priv->txqs)[idx])
572 txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
574 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
575 return txq_ctrl->ibv;
579 * Release an Tx verbs queue object.
582 * Verbs Tx queue object.
585 * 1 while a reference on it exists, 0 when freed.
588 mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv)
591 if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
592 claim_zero(mlx5_glue->destroy_qp(txq_ibv->qp));
593 claim_zero(mlx5_glue->destroy_cq(txq_ibv->cq));
594 LIST_REMOVE(txq_ibv, next);
602 * Return true if a single reference exists on the object.
605 * Verbs Tx queue object.
608 mlx5_txq_ibv_releasable(struct mlx5_txq_ibv *txq_ibv)
611 return (rte_atomic32_read(&txq_ibv->refcnt) == 1);
615 * Verify the Verbs Tx queue list is empty
618 * Pointer to Ethernet device.
621 * The number of object not released.
624 mlx5_txq_ibv_verify(struct rte_eth_dev *dev)
626 struct mlx5_priv *priv = dev->data->dev_private;
628 struct mlx5_txq_ibv *txq_ibv;
630 LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
631 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
632 dev->data->port_id, txq_ibv->txq_ctrl->idx);
639 * Set Tx queue parameters from device configuration.
642 * Pointer to Tx queue control structure.
645 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
647 struct mlx5_priv *priv = txq_ctrl->priv;
648 struct mlx5_dev_config *config = &priv->config;
649 const unsigned int max_tso_inline =
650 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
651 RTE_CACHE_LINE_SIZE);
652 unsigned int txq_inline;
653 unsigned int txqs_inline;
654 unsigned int inline_max_packet_sz;
655 eth_tx_burst_t tx_pkt_burst =
656 mlx5_select_tx_function(ETH_DEV(priv));
657 int is_empw_func = is_empw_burst_func(tx_pkt_burst);
658 int tso = !!(txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
659 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
660 DEV_TX_OFFLOAD_GRE_TNL_TSO |
661 DEV_TX_OFFLOAD_IP_TNL_TSO |
662 DEV_TX_OFFLOAD_UDP_TNL_TSO));
664 txq_inline = (config->txq_inline == MLX5_ARG_UNSET) ?
665 0 : config->txq_inline;
666 txqs_inline = (config->txqs_inline == MLX5_ARG_UNSET) ?
667 0 : config->txqs_inline;
668 inline_max_packet_sz =
669 (config->inline_max_packet_sz == MLX5_ARG_UNSET) ?
670 0 : config->inline_max_packet_sz;
672 if (config->txq_inline == MLX5_ARG_UNSET)
673 txq_inline = MLX5_WQE_SIZE_MAX - MLX5_WQE_SIZE;
674 if (config->txqs_inline == MLX5_ARG_UNSET)
675 txqs_inline = MLX5_EMPW_MIN_TXQS;
676 if (config->inline_max_packet_sz == MLX5_ARG_UNSET)
677 inline_max_packet_sz = MLX5_EMPW_MAX_INLINE_LEN;
678 txq_ctrl->txq.mpw_hdr_dseg = config->mpw_hdr_dseg;
679 txq_ctrl->txq.inline_max_packet_sz = inline_max_packet_sz;
681 if (txq_inline && priv->txqs_n >= txqs_inline) {
684 txq_ctrl->txq.max_inline =
685 ((txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
686 RTE_CACHE_LINE_SIZE);
688 /* To minimize the size of data set, avoid requesting
691 txq_ctrl->max_inline_data =
692 ((RTE_MIN(txq_inline,
693 inline_max_packet_sz) +
694 (RTE_CACHE_LINE_SIZE - 1)) /
695 RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
697 txq_ctrl->max_inline_data =
698 txq_ctrl->txq.max_inline * RTE_CACHE_LINE_SIZE;
701 * Check if the inline size is too large in a way which
702 * can make the WQE DS to overflow.
703 * Considering in calculation:
708 ds_cnt = 2 + (txq_ctrl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
709 if (ds_cnt > MLX5_DSEG_MAX) {
710 unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
713 max_inline = max_inline - (max_inline %
714 RTE_CACHE_LINE_SIZE);
716 "port %u txq inline is too large (%d) setting"
717 " it to the maximum possible: %d\n",
718 PORT_ID(priv), txq_inline, max_inline);
719 txq_ctrl->txq.max_inline = max_inline /
724 txq_ctrl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
725 txq_ctrl->txq.max_inline = RTE_MAX(txq_ctrl->txq.max_inline,
727 txq_ctrl->txq.tso_en = 1;
729 txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
730 txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
731 DEV_TX_OFFLOAD_UDP_TNL_TSO |
732 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
733 txq_ctrl->txq.offloads) && config->swp;
737 * Create a DPDK Tx queue.
740 * Pointer to Ethernet device.
744 * Number of descriptors to configure in queue.
746 * NUMA socket on which memory must be allocated.
748 * Thresholds parameters.
751 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
753 struct mlx5_txq_ctrl *
754 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
755 unsigned int socket, const struct rte_eth_txconf *conf)
757 struct mlx5_priv *priv = dev->data->dev_private;
758 struct mlx5_txq_ctrl *tmpl;
760 tmpl = rte_calloc_socket("TXQ", 1,
762 desc * sizeof(struct rte_mbuf *),
768 if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
769 MLX5_MR_BTREE_CACHE_N, socket)) {
770 /* rte_errno is already set. */
773 /* Save pointer of global generation number to check memory event. */
774 tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->mr.dev_gen;
775 assert(desc > MLX5_TX_COMP_THRESH);
776 tmpl->txq.offloads = conf->offloads |
777 dev->data->dev_conf.txmode.offloads;
779 tmpl->socket = socket;
780 tmpl->txq.elts_n = log2above(desc);
782 txq_set_params(tmpl);
783 DRV_LOG(DEBUG, "port %u device_attr.max_qp_wr is %d",
784 dev->data->port_id, priv->sh->device_attr.orig_attr.max_qp_wr);
785 DRV_LOG(DEBUG, "port %u device_attr.max_sge is %d",
786 dev->data->port_id, priv->sh->device_attr.orig_attr.max_sge);
788 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
789 tmpl->txq.stats.idx = idx;
790 rte_atomic32_inc(&tmpl->refcnt);
791 LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
802 * Pointer to Ethernet device.
807 * A pointer to the queue if it exists.
809 struct mlx5_txq_ctrl *
810 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
812 struct mlx5_priv *priv = dev->data->dev_private;
813 struct mlx5_txq_ctrl *ctrl = NULL;
815 if ((*priv->txqs)[idx]) {
816 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
818 mlx5_txq_ibv_get(dev, idx);
819 rte_atomic32_inc(&ctrl->refcnt);
825 * Release a Tx queue.
828 * Pointer to Ethernet device.
833 * 1 while a reference on it exists, 0 when freed.
836 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
838 struct mlx5_priv *priv = dev->data->dev_private;
839 struct mlx5_txq_ctrl *txq;
840 size_t page_size = sysconf(_SC_PAGESIZE);
842 if (!(*priv->txqs)[idx])
844 txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
845 if (txq->ibv && !mlx5_txq_ibv_release(txq->ibv))
847 munmap((void *)RTE_ALIGN_FLOOR((uintptr_t)txq->txq.bf_reg, page_size),
849 if (rte_atomic32_dec_and_test(&txq->refcnt)) {
851 mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
852 LIST_REMOVE(txq, next);
854 (*priv->txqs)[idx] = NULL;
861 * Verify if the queue can be released.
864 * Pointer to Ethernet device.
869 * 1 if the queue can be released.
872 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
874 struct mlx5_priv *priv = dev->data->dev_private;
875 struct mlx5_txq_ctrl *txq;
877 if (!(*priv->txqs)[idx])
879 txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
880 return (rte_atomic32_read(&txq->refcnt) == 1);
884 * Verify the Tx Queue list is empty
887 * Pointer to Ethernet device.
890 * The number of object not released.
893 mlx5_txq_verify(struct rte_eth_dev *dev)
895 struct mlx5_priv *priv = dev->data->dev_private;
896 struct mlx5_txq_ctrl *txq;
899 LIST_FOREACH(txq, &priv->txqsctrl, next) {
900 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
901 dev->data->port_id, txq->idx);