1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
14 #include <rte_malloc.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_common.h>
17 #include <rte_eal_paging.h>
19 #include <mlx5_glue.h>
20 #include <mlx5_devx_cmds.h>
21 #include <mlx5_common.h>
22 #include <mlx5_common_mr.h>
23 #include <mlx5_malloc.h>
25 #include "mlx5_defs.h"
26 #include "mlx5_utils.h"
28 #include "mlx5_rxtx.h"
29 #include "mlx5_autoconf.h"
32 * Allocate TX queue elements.
35 * Pointer to TX queue structure.
38 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
40 const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
43 for (i = 0; (i != elts_n); ++i)
44 txq_ctrl->txq.elts[i] = NULL;
45 DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
46 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
47 txq_ctrl->txq.elts_head = 0;
48 txq_ctrl->txq.elts_tail = 0;
49 txq_ctrl->txq.elts_comp = 0;
53 * Free TX queue elements.
56 * Pointer to TX queue structure.
59 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
61 const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
62 const uint16_t elts_m = elts_n - 1;
63 uint16_t elts_head = txq_ctrl->txq.elts_head;
64 uint16_t elts_tail = txq_ctrl->txq.elts_tail;
65 struct rte_mbuf *(*elts)[elts_n] = &txq_ctrl->txq.elts;
67 DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
68 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
69 txq_ctrl->txq.elts_head = 0;
70 txq_ctrl->txq.elts_tail = 0;
71 txq_ctrl->txq.elts_comp = 0;
73 while (elts_tail != elts_head) {
74 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
76 MLX5_ASSERT(elt != NULL);
77 rte_pktmbuf_free_seg(elt);
78 #ifdef RTE_LIBRTE_MLX5_DEBUG
80 memset(&(*elts)[elts_tail & elts_m],
82 sizeof((*elts)[elts_tail & elts_m]));
89 * Returns the per-port supported offloads.
92 * Pointer to Ethernet device.
95 * Supported Tx offloads.
98 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
100 struct mlx5_priv *priv = dev->data->dev_private;
101 uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
102 DEV_TX_OFFLOAD_VLAN_INSERT);
103 struct mlx5_dev_config *config = &priv->config;
106 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
107 DEV_TX_OFFLOAD_UDP_CKSUM |
108 DEV_TX_OFFLOAD_TCP_CKSUM);
110 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
112 offloads |= DEV_TX_OFFLOAD_SEND_ON_TIMESTAMP;
115 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
117 offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
118 DEV_TX_OFFLOAD_UDP_TNL_TSO);
120 if (config->tunnel_en) {
122 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
124 offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
125 DEV_TX_OFFLOAD_GRE_TNL_TSO |
126 DEV_TX_OFFLOAD_GENEVE_TNL_TSO);
131 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
133 txq_sync_cq(struct mlx5_txq_data *txq)
135 volatile struct mlx5_cqe *cqe;
140 cqe = &txq->cqes[txq->cq_ci & txq->cqe_m];
141 ret = check_cqe(cqe, txq->cqe_s, txq->cq_ci);
142 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
143 if (likely(ret != MLX5_CQE_STATUS_ERR)) {
144 /* No new CQEs in completion queue. */
145 MLX5_ASSERT(ret == MLX5_CQE_STATUS_HW_OWN);
151 /* Move all CQEs to HW ownership. */
152 for (i = 0; i < txq->cqe_s; i++) {
154 cqe->op_own = MLX5_CQE_INVALIDATE;
156 /* Resync CQE and WQE (WQ in reset state). */
158 *txq->cq_db = rte_cpu_to_be_32(txq->cq_ci);
163 * Tx queue stop. Device queue goes to the idle state,
164 * all involved mbufs are freed from elts/WQ.
167 * Pointer to Ethernet device structure.
172 * 0 on success, a negative errno value otherwise and rte_errno is set.
175 mlx5_tx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
177 struct mlx5_priv *priv = dev->data->dev_private;
178 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
179 struct mlx5_txq_ctrl *txq_ctrl =
180 container_of(txq, struct mlx5_txq_ctrl, txq);
183 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
184 /* Move QP to RESET state. */
185 ret = priv->obj_ops.txq_obj_modify(txq_ctrl->obj, MLX5_TXQ_MOD_RDY2RST,
186 (uint8_t)priv->dev_port);
189 /* Handle all send completions. */
191 /* Free elts stored in the SQ. */
192 txq_free_elts(txq_ctrl);
193 /* Prevent writing new pkts to SQ by setting no free WQE.*/
194 txq->wqe_ci = txq->wqe_s;
197 /* Set the actual queue state. */
198 dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
203 * Tx queue stop. Device queue goes to the idle state,
204 * all involved mbufs are freed from elts/WQ.
207 * Pointer to Ethernet device structure.
212 * 0 on success, a negative errno value otherwise and rte_errno is set.
215 mlx5_tx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
219 if (rte_eth_dev_is_tx_hairpin_queue(dev, idx)) {
220 DRV_LOG(ERR, "Hairpin queue can't be stopped");
224 if (dev->data->tx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
226 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
227 ret = mlx5_mp_os_req_queue_control(dev, idx,
228 MLX5_MP_REQ_QUEUE_TX_STOP);
230 ret = mlx5_tx_queue_stop_primary(dev, idx);
236 * Rx queue start. Device queue goes to the ready state,
237 * all required mbufs are allocated and WQ is replenished.
240 * Pointer to Ethernet device structure.
245 * 0 on success, a negative errno value otherwise and rte_errno is set.
248 mlx5_tx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
250 struct mlx5_priv *priv = dev->data->dev_private;
251 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
252 struct mlx5_txq_ctrl *txq_ctrl =
253 container_of(txq, struct mlx5_txq_ctrl, txq);
256 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
257 ret = priv->obj_ops.txq_obj_modify(txq_ctrl->obj,
258 MLX5_TXQ_MOD_RDY2RDY,
259 (uint8_t)priv->dev_port);
262 txq_ctrl->txq.wqe_ci = 0;
263 txq_ctrl->txq.wqe_pi = 0;
264 txq_ctrl->txq.elts_comp = 0;
265 /* Set the actual queue state. */
266 dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
271 * Rx queue start. Device queue goes to the ready state,
272 * all required mbufs are allocated and WQ is replenished.
275 * Pointer to Ethernet device structure.
280 * 0 on success, a negative errno value otherwise and rte_errno is set.
283 mlx5_tx_queue_start(struct rte_eth_dev *dev, uint16_t idx)
287 if (rte_eth_dev_is_tx_hairpin_queue(dev, idx)) {
288 DRV_LOG(ERR, "Hairpin queue can't be started");
292 if (dev->data->tx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
294 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
295 ret = mlx5_mp_os_req_queue_control(dev, idx,
296 MLX5_MP_REQ_QUEUE_TX_START);
298 ret = mlx5_tx_queue_start_primary(dev, idx);
304 * Tx queue presetup checks.
307 * Pointer to Ethernet device structure.
311 * Number of descriptors to configure in queue.
314 * 0 on success, a negative errno value otherwise and rte_errno is set.
317 mlx5_tx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc)
319 struct mlx5_priv *priv = dev->data->dev_private;
321 if (*desc <= MLX5_TX_COMP_THRESH) {
323 "port %u number of descriptors requested for Tx queue"
324 " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
325 " instead of %u", dev->data->port_id, idx,
326 MLX5_TX_COMP_THRESH + 1, *desc);
327 *desc = MLX5_TX_COMP_THRESH + 1;
329 if (!rte_is_power_of_2(*desc)) {
330 *desc = 1 << log2above(*desc);
332 "port %u increased number of descriptors in Tx queue"
333 " %u to the next power of two (%d)",
334 dev->data->port_id, idx, *desc);
336 DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
337 dev->data->port_id, idx, *desc);
338 if (idx >= priv->txqs_n) {
339 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
340 dev->data->port_id, idx, priv->txqs_n);
341 rte_errno = EOVERFLOW;
344 if (!mlx5_txq_releasable(dev, idx)) {
346 DRV_LOG(ERR, "port %u unable to release queue index %u",
347 dev->data->port_id, idx);
350 mlx5_txq_release(dev, idx);
355 * DPDK callback to configure a TX queue.
358 * Pointer to Ethernet device structure.
362 * Number of descriptors to configure in queue.
364 * NUMA socket on which memory must be allocated.
366 * Thresholds parameters.
369 * 0 on success, a negative errno value otherwise and rte_errno is set.
372 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
373 unsigned int socket, const struct rte_eth_txconf *conf)
375 struct mlx5_priv *priv = dev->data->dev_private;
376 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
377 struct mlx5_txq_ctrl *txq_ctrl =
378 container_of(txq, struct mlx5_txq_ctrl, txq);
381 res = mlx5_tx_queue_pre_setup(dev, idx, &desc);
384 txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
386 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
387 dev->data->port_id, idx);
390 DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
391 dev->data->port_id, idx);
392 (*priv->txqs)[idx] = &txq_ctrl->txq;
393 dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
398 * DPDK callback to configure a TX hairpin queue.
401 * Pointer to Ethernet device structure.
405 * Number of descriptors to configure in queue.
406 * @param[in] hairpin_conf
407 * The hairpin binding configuration.
410 * 0 on success, a negative errno value otherwise and rte_errno is set.
413 mlx5_tx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
415 const struct rte_eth_hairpin_conf *hairpin_conf)
417 struct mlx5_priv *priv = dev->data->dev_private;
418 struct mlx5_txq_data *txq = (*priv->txqs)[idx];
419 struct mlx5_txq_ctrl *txq_ctrl =
420 container_of(txq, struct mlx5_txq_ctrl, txq);
423 res = mlx5_tx_queue_pre_setup(dev, idx, &desc);
426 if (hairpin_conf->peer_count != 1 ||
427 hairpin_conf->peers[0].port != dev->data->port_id ||
428 hairpin_conf->peers[0].queue >= priv->rxqs_n) {
429 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
430 " invalid hairpind configuration", dev->data->port_id,
435 txq_ctrl = mlx5_txq_hairpin_new(dev, idx, desc, hairpin_conf);
437 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
438 dev->data->port_id, idx);
441 DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
442 dev->data->port_id, idx);
443 (*priv->txqs)[idx] = &txq_ctrl->txq;
444 dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
449 * DPDK callback to release a TX queue.
452 * Generic TX queue pointer.
455 mlx5_tx_queue_release(void *dpdk_txq)
457 struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
458 struct mlx5_txq_ctrl *txq_ctrl;
459 struct mlx5_priv *priv;
464 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
465 priv = txq_ctrl->priv;
466 for (i = 0; (i != priv->txqs_n); ++i)
467 if ((*priv->txqs)[i] == txq) {
468 DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
469 PORT_ID(priv), txq->idx);
470 mlx5_txq_release(ETH_DEV(priv), i);
476 * Configure the doorbell register non-cached attribute.
479 * Pointer to Tx queue control structure.
484 txq_uar_ncattr_init(struct mlx5_txq_ctrl *txq_ctrl, size_t page_size)
486 struct mlx5_priv *priv = txq_ctrl->priv;
489 txq_ctrl->txq.db_heu = priv->config.dbnc == MLX5_TXDB_HEURISTIC;
490 txq_ctrl->txq.db_nc = 0;
491 /* Check the doorbell register mapping type. */
492 cmd = txq_ctrl->uar_mmap_offset / page_size;
493 cmd >>= MLX5_UAR_MMAP_CMD_SHIFT;
494 cmd &= MLX5_UAR_MMAP_CMD_MASK;
495 if (cmd == MLX5_MMAP_GET_NC_PAGES_CMD)
496 txq_ctrl->txq.db_nc = 1;
500 * Initialize Tx UAR registers for primary process.
503 * Pointer to Tx queue control structure.
506 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
508 struct mlx5_priv *priv = txq_ctrl->priv;
509 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
511 unsigned int lock_idx;
513 const size_t page_size = rte_mem_page_size();
514 if (page_size == (size_t)-1) {
515 DRV_LOG(ERR, "Failed to get mem page size");
519 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
521 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
523 ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
524 txq_uar_ncattr_init(txq_ctrl, page_size);
526 /* Assign an UAR lock according to UAR page number */
527 lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
528 MLX5_UAR_PAGE_NUM_MASK;
529 txq_ctrl->txq.uar_lock = &priv->sh->uar_lock[lock_idx];
534 * Remap UAR register of a Tx queue for secondary process.
536 * Remapped address is stored at the table in the process private structure of
537 * the device, indexed by queue index.
540 * Pointer to Tx queue control structure.
542 * Verbs file descriptor to map UAR pages.
545 * 0 on success, a negative errno value otherwise and rte_errno is set.
548 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
550 struct mlx5_priv *priv = txq_ctrl->priv;
551 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
552 struct mlx5_txq_data *txq = &txq_ctrl->txq;
556 const size_t page_size = rte_mem_page_size();
557 if (page_size == (size_t)-1) {
558 DRV_LOG(ERR, "Failed to get mem page size");
563 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
567 * As rdma-core, UARs are mapped in size of OS page
568 * size. Ref to libmlx5 function: mlx5_init_context()
570 uar_va = (uintptr_t)txq_ctrl->bf_reg;
571 offset = uar_va & (page_size - 1); /* Offset in page. */
572 addr = rte_mem_map(NULL, page_size, RTE_PROT_WRITE, RTE_MAP_SHARED,
573 fd, txq_ctrl->uar_mmap_offset);
576 "port %u mmap failed for BF reg of txq %u",
577 txq->port_id, txq->idx);
581 addr = RTE_PTR_ADD(addr, offset);
582 ppriv->uar_table[txq->idx] = addr;
583 txq_uar_ncattr_init(txq_ctrl, page_size);
588 * Unmap UAR register of a Tx queue for secondary process.
591 * Pointer to Tx queue control structure.
594 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
596 struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
598 const size_t page_size = rte_mem_page_size();
599 if (page_size == (size_t)-1) {
600 DRV_LOG(ERR, "Failed to get mem page size");
604 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
606 addr = ppriv->uar_table[txq_ctrl->txq.idx];
607 rte_mem_unmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
611 * Deinitialize Tx UAR registers for secondary process.
614 * Pointer to Ethernet device.
617 mlx5_tx_uar_uninit_secondary(struct rte_eth_dev *dev)
619 struct mlx5_priv *priv = dev->data->dev_private;
620 struct mlx5_txq_data *txq;
621 struct mlx5_txq_ctrl *txq_ctrl;
624 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
625 for (i = 0; i != priv->txqs_n; ++i) {
626 if (!(*priv->txqs)[i])
628 txq = (*priv->txqs)[i];
629 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
630 txq_uar_uninit_secondary(txq_ctrl);
635 * Initialize Tx UAR registers for secondary process.
638 * Pointer to Ethernet device.
640 * Verbs file descriptor to map UAR pages.
643 * 0 on success, a negative errno value otherwise and rte_errno is set.
646 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
648 struct mlx5_priv *priv = dev->data->dev_private;
649 struct mlx5_txq_data *txq;
650 struct mlx5_txq_ctrl *txq_ctrl;
654 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
655 for (i = 0; i != priv->txqs_n; ++i) {
656 if (!(*priv->txqs)[i])
658 txq = (*priv->txqs)[i];
659 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
660 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
662 MLX5_ASSERT(txq->idx == (uint16_t)i);
663 ret = txq_uar_init_secondary(txq_ctrl, fd);
671 if (!(*priv->txqs)[i])
673 txq = (*priv->txqs)[i];
674 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
675 txq_uar_uninit_secondary(txq_ctrl);
681 * Verify the Verbs Tx queue list is empty
684 * Pointer to Ethernet device.
687 * The number of object not released.
690 mlx5_txq_obj_verify(struct rte_eth_dev *dev)
692 struct mlx5_priv *priv = dev->data->dev_private;
694 struct mlx5_txq_obj *txq_obj;
696 LIST_FOREACH(txq_obj, &priv->txqsobj, next) {
697 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
698 dev->data->port_id, txq_obj->txq_ctrl->txq.idx);
705 * Calculate the total number of WQEBB for Tx queue.
707 * Simplified version of calc_sq_size() in rdma-core.
710 * Pointer to Tx queue control structure.
713 * The number of WQEBB.
716 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
718 unsigned int wqe_size;
719 const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
721 wqe_size = MLX5_WQE_CSEG_SIZE +
724 MLX5_ESEG_MIN_INLINE_SIZE +
725 txq_ctrl->max_inline_data;
726 return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
730 * Calculate the maximal inline data size for Tx queue.
733 * Pointer to Tx queue control structure.
736 * The maximal inline data size.
739 txq_calc_inline_max(struct mlx5_txq_ctrl *txq_ctrl)
741 const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
742 struct mlx5_priv *priv = txq_ctrl->priv;
743 unsigned int wqe_size;
745 wqe_size = priv->sh->device_attr.max_qp_wr / desc;
749 * This calculation is derived from tthe source of
750 * mlx5_calc_send_wqe() in rdma_core library.
752 wqe_size = wqe_size * MLX5_WQE_SIZE -
757 MLX5_DSEG_MIN_INLINE_SIZE;
762 * Set Tx queue parameters from device configuration.
765 * Pointer to Tx queue control structure.
768 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
770 struct mlx5_priv *priv = txq_ctrl->priv;
771 struct mlx5_dev_config *config = &priv->config;
772 unsigned int inlen_send; /* Inline data for ordinary SEND.*/
773 unsigned int inlen_empw; /* Inline data for enhanced MPW. */
774 unsigned int inlen_mode; /* Minimal required Inline data. */
775 unsigned int txqs_inline; /* Min Tx queues to enable inline. */
776 uint64_t dev_txoff = priv->dev_data->dev_conf.txmode.offloads;
777 bool tso = txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
778 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
779 DEV_TX_OFFLOAD_GRE_TNL_TSO |
780 DEV_TX_OFFLOAD_IP_TNL_TSO |
781 DEV_TX_OFFLOAD_UDP_TNL_TSO);
785 if (config->txqs_inline == MLX5_ARG_UNSET)
787 #if defined(RTE_ARCH_ARM64)
788 (priv->pci_dev->id.device_id ==
789 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF) ?
790 MLX5_INLINE_MAX_TXQS_BLUEFIELD :
792 MLX5_INLINE_MAX_TXQS;
794 txqs_inline = (unsigned int)config->txqs_inline;
795 inlen_send = (config->txq_inline_max == MLX5_ARG_UNSET) ?
796 MLX5_SEND_DEF_INLINE_LEN :
797 (unsigned int)config->txq_inline_max;
798 inlen_empw = (config->txq_inline_mpw == MLX5_ARG_UNSET) ?
799 MLX5_EMPW_DEF_INLINE_LEN :
800 (unsigned int)config->txq_inline_mpw;
801 inlen_mode = (config->txq_inline_min == MLX5_ARG_UNSET) ?
802 0 : (unsigned int)config->txq_inline_min;
803 if (config->mps != MLX5_MPW_ENHANCED && config->mps != MLX5_MPW)
806 * If there is requested minimal amount of data to inline
807 * we MUST enable inlining. This is a case for ConnectX-4
808 * which usually requires L2 inlined for correct operating
809 * and ConnectX-4 Lx which requires L2-L4 inlined to
810 * support E-Switch Flows.
813 if (inlen_mode <= MLX5_ESEG_MIN_INLINE_SIZE) {
815 * Optimize minimal inlining for single
816 * segment packets to fill one WQEBB
819 temp = MLX5_ESEG_MIN_INLINE_SIZE;
821 temp = inlen_mode - MLX5_ESEG_MIN_INLINE_SIZE;
822 temp = RTE_ALIGN(temp, MLX5_WSEG_SIZE) +
823 MLX5_ESEG_MIN_INLINE_SIZE;
824 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
826 if (temp != inlen_mode) {
828 "port %u minimal required inline setting"
829 " aligned from %u to %u",
830 PORT_ID(priv), inlen_mode, temp);
835 * If port is configured to support VLAN insertion and device
836 * does not support this feature by HW (for NICs before ConnectX-5
837 * or in case of wqe_vlan_insert flag is not set) we must enable
838 * data inline on all queues because it is supported by single
841 txq_ctrl->txq.vlan_en = config->hw_vlan_insert;
842 vlan_inline = (dev_txoff & DEV_TX_OFFLOAD_VLAN_INSERT) &&
843 !config->hw_vlan_insert;
845 * If there are few Tx queues it is prioritized
846 * to save CPU cycles and disable data inlining at all.
848 if (inlen_send && priv->txqs_n >= txqs_inline) {
850 * The data sent with ordinal MLX5_OPCODE_SEND
851 * may be inlined in Ethernet Segment, align the
852 * length accordingly to fit entire WQEBBs.
854 temp = RTE_MAX(inlen_send,
855 MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE);
856 temp -= MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
857 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
858 temp += MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
859 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
860 MLX5_ESEG_MIN_INLINE_SIZE -
863 MLX5_WQE_DSEG_SIZE * 2);
864 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
865 temp = RTE_MAX(temp, inlen_mode);
866 if (temp != inlen_send) {
868 "port %u ordinary send inline setting"
869 " aligned from %u to %u",
870 PORT_ID(priv), inlen_send, temp);
874 * Not aligned to cache lines, but to WQEs.
875 * First bytes of data (initial alignment)
876 * is going to be copied explicitly at the
877 * beginning of inlining buffer in Ethernet
880 MLX5_ASSERT(inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
881 MLX5_ASSERT(inlen_send <= MLX5_WQE_SIZE_MAX +
882 MLX5_ESEG_MIN_INLINE_SIZE -
885 MLX5_WQE_DSEG_SIZE * 2);
886 } else if (inlen_mode) {
888 * If minimal inlining is requested we must
889 * enable inlining in general, despite the
890 * number of configured queues. Ignore the
891 * txq_inline_max devarg, this is not
892 * full-featured inline.
894 inlen_send = inlen_mode;
896 } else if (vlan_inline) {
898 * Hardware does not report offload for
899 * VLAN insertion, we must enable data inline
900 * to implement feature by software.
902 inlen_send = MLX5_ESEG_MIN_INLINE_SIZE;
908 txq_ctrl->txq.inlen_send = inlen_send;
909 txq_ctrl->txq.inlen_mode = inlen_mode;
910 txq_ctrl->txq.inlen_empw = 0;
911 if (inlen_send && inlen_empw && priv->txqs_n >= txqs_inline) {
913 * The data sent with MLX5_OPCODE_ENHANCED_MPSW
914 * may be inlined in Data Segment, align the
915 * length accordingly to fit entire WQEBBs.
917 temp = RTE_MAX(inlen_empw,
918 MLX5_WQE_SIZE + MLX5_DSEG_MIN_INLINE_SIZE);
919 temp -= MLX5_DSEG_MIN_INLINE_SIZE;
920 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
921 temp += MLX5_DSEG_MIN_INLINE_SIZE;
922 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
923 MLX5_DSEG_MIN_INLINE_SIZE -
927 temp = RTE_MIN(temp, MLX5_EMPW_MAX_INLINE_LEN);
928 if (temp != inlen_empw) {
930 "port %u enhanced empw inline setting"
931 " aligned from %u to %u",
932 PORT_ID(priv), inlen_empw, temp);
935 MLX5_ASSERT(inlen_empw >= MLX5_ESEG_MIN_INLINE_SIZE);
936 MLX5_ASSERT(inlen_empw <= MLX5_WQE_SIZE_MAX +
937 MLX5_DSEG_MIN_INLINE_SIZE -
941 txq_ctrl->txq.inlen_empw = inlen_empw;
943 txq_ctrl->max_inline_data = RTE_MAX(inlen_send, inlen_empw);
945 txq_ctrl->max_tso_header = MLX5_MAX_TSO_HEADER;
946 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->max_inline_data,
947 MLX5_MAX_TSO_HEADER);
948 txq_ctrl->txq.tso_en = 1;
950 txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
951 txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
952 DEV_TX_OFFLOAD_UDP_TNL_TSO |
953 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
954 txq_ctrl->txq.offloads) && config->swp;
958 * Adjust Tx queue data inline parameters for large queue sizes.
959 * The data inline feature requires multiple WQEs to fit the packets,
960 * and if the large amount of Tx descriptors is requested by application
961 * the total WQE amount may exceed the hardware capabilities. If the
962 * default inline setting are used we can try to adjust these ones and
963 * meet the hardware requirements and not exceed the queue size.
966 * Pointer to Tx queue control structure.
969 * Zero on success, otherwise the parameters can not be adjusted.
972 txq_adjust_params(struct mlx5_txq_ctrl *txq_ctrl)
974 struct mlx5_priv *priv = txq_ctrl->priv;
975 struct mlx5_dev_config *config = &priv->config;
976 unsigned int max_inline;
978 max_inline = txq_calc_inline_max(txq_ctrl);
979 if (!txq_ctrl->txq.inlen_send) {
981 * Inline data feature is not engaged at all.
982 * There is nothing to adjust.
986 if (txq_ctrl->max_inline_data <= max_inline) {
988 * The requested inline data length does not
989 * exceed queue capabilities.
993 if (txq_ctrl->txq.inlen_mode > max_inline) {
995 "minimal data inline requirements (%u) are not"
996 " satisfied (%u) on port %u, try the smaller"
997 " Tx queue size (%d)",
998 txq_ctrl->txq.inlen_mode, max_inline,
999 priv->dev_data->port_id,
1000 priv->sh->device_attr.max_qp_wr);
1003 if (txq_ctrl->txq.inlen_send > max_inline &&
1004 config->txq_inline_max != MLX5_ARG_UNSET &&
1005 config->txq_inline_max > (int)max_inline) {
1007 "txq_inline_max requirements (%u) are not"
1008 " satisfied (%u) on port %u, try the smaller"
1009 " Tx queue size (%d)",
1010 txq_ctrl->txq.inlen_send, max_inline,
1011 priv->dev_data->port_id,
1012 priv->sh->device_attr.max_qp_wr);
1015 if (txq_ctrl->txq.inlen_empw > max_inline &&
1016 config->txq_inline_mpw != MLX5_ARG_UNSET &&
1017 config->txq_inline_mpw > (int)max_inline) {
1019 "txq_inline_mpw requirements (%u) are not"
1020 " satisfied (%u) on port %u, try the smaller"
1021 " Tx queue size (%d)",
1022 txq_ctrl->txq.inlen_empw, max_inline,
1023 priv->dev_data->port_id,
1024 priv->sh->device_attr.max_qp_wr);
1027 if (txq_ctrl->txq.tso_en && max_inline < MLX5_MAX_TSO_HEADER) {
1029 "tso header inline requirements (%u) are not"
1030 " satisfied (%u) on port %u, try the smaller"
1031 " Tx queue size (%d)",
1032 MLX5_MAX_TSO_HEADER, max_inline,
1033 priv->dev_data->port_id,
1034 priv->sh->device_attr.max_qp_wr);
1037 if (txq_ctrl->txq.inlen_send > max_inline) {
1039 "adjust txq_inline_max (%u->%u)"
1040 " due to large Tx queue on port %u",
1041 txq_ctrl->txq.inlen_send, max_inline,
1042 priv->dev_data->port_id);
1043 txq_ctrl->txq.inlen_send = max_inline;
1045 if (txq_ctrl->txq.inlen_empw > max_inline) {
1047 "adjust txq_inline_mpw (%u->%u)"
1048 "due to large Tx queue on port %u",
1049 txq_ctrl->txq.inlen_empw, max_inline,
1050 priv->dev_data->port_id);
1051 txq_ctrl->txq.inlen_empw = max_inline;
1053 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->txq.inlen_send,
1054 txq_ctrl->txq.inlen_empw);
1055 MLX5_ASSERT(txq_ctrl->max_inline_data <= max_inline);
1056 MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= max_inline);
1057 MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_send);
1058 MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_empw ||
1059 !txq_ctrl->txq.inlen_empw);
1067 * Create a DPDK Tx queue.
1070 * Pointer to Ethernet device.
1074 * Number of descriptors to configure in queue.
1076 * NUMA socket on which memory must be allocated.
1078 * Thresholds parameters.
1081 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1083 struct mlx5_txq_ctrl *
1084 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1085 unsigned int socket, const struct rte_eth_txconf *conf)
1087 struct mlx5_priv *priv = dev->data->dev_private;
1088 struct mlx5_txq_ctrl *tmpl;
1090 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl) +
1091 desc * sizeof(struct rte_mbuf *), 0, socket);
1096 if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
1097 MLX5_MR_BTREE_CACHE_N, socket)) {
1098 /* rte_errno is already set. */
1101 /* Save pointer of global generation number to check memory event. */
1102 tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->share_cache.dev_gen;
1103 MLX5_ASSERT(desc > MLX5_TX_COMP_THRESH);
1104 tmpl->txq.offloads = conf->offloads |
1105 dev->data->dev_conf.txmode.offloads;
1107 tmpl->socket = socket;
1108 tmpl->txq.elts_n = log2above(desc);
1109 tmpl->txq.elts_s = desc;
1110 tmpl->txq.elts_m = desc - 1;
1111 tmpl->txq.port_id = dev->data->port_id;
1112 tmpl->txq.idx = idx;
1113 txq_set_params(tmpl);
1114 if (txq_adjust_params(tmpl))
1116 if (txq_calc_wqebb_cnt(tmpl) >
1117 priv->sh->device_attr.max_qp_wr) {
1119 "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
1120 " try smaller queue size",
1121 dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
1122 priv->sh->device_attr.max_qp_wr);
1126 rte_atomic32_inc(&tmpl->refcnt);
1127 tmpl->type = MLX5_TXQ_TYPE_STANDARD;
1128 LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1136 * Create a DPDK Tx hairpin queue.
1139 * Pointer to Ethernet device.
1143 * Number of descriptors to configure in queue.
1144 * @param hairpin_conf
1145 * The hairpin configuration.
1148 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1150 struct mlx5_txq_ctrl *
1151 mlx5_txq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1152 const struct rte_eth_hairpin_conf *hairpin_conf)
1154 struct mlx5_priv *priv = dev->data->dev_private;
1155 struct mlx5_txq_ctrl *tmpl;
1157 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1164 tmpl->socket = SOCKET_ID_ANY;
1165 tmpl->txq.elts_n = log2above(desc);
1166 tmpl->txq.port_id = dev->data->port_id;
1167 tmpl->txq.idx = idx;
1168 tmpl->hairpin_conf = *hairpin_conf;
1169 tmpl->type = MLX5_TXQ_TYPE_HAIRPIN;
1170 rte_atomic32_inc(&tmpl->refcnt);
1171 LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1179 * Pointer to Ethernet device.
1184 * A pointer to the queue if it exists.
1186 struct mlx5_txq_ctrl *
1187 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
1189 struct mlx5_priv *priv = dev->data->dev_private;
1190 struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1191 struct mlx5_txq_ctrl *ctrl = NULL;
1194 ctrl = container_of(txq_data, struct mlx5_txq_ctrl, txq);
1195 rte_atomic32_inc(&ctrl->refcnt);
1201 * Release a Tx queue.
1204 * Pointer to Ethernet device.
1209 * 1 while a reference on it exists, 0 when freed.
1212 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
1214 struct mlx5_priv *priv = dev->data->dev_private;
1215 struct mlx5_txq_ctrl *txq_ctrl;
1217 if (!(*priv->txqs)[idx])
1219 txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1220 if (!rte_atomic32_dec_and_test(&txq_ctrl->refcnt))
1222 if (txq_ctrl->obj) {
1223 priv->obj_ops.txq_obj_release(txq_ctrl->obj);
1224 LIST_REMOVE(txq_ctrl->obj, next);
1225 mlx5_free(txq_ctrl->obj);
1226 txq_ctrl->obj = NULL;
1228 if (txq_ctrl->type == MLX5_TXQ_TYPE_STANDARD) {
1229 if (txq_ctrl->txq.fcqs) {
1230 mlx5_free(txq_ctrl->txq.fcqs);
1231 txq_ctrl->txq.fcqs = NULL;
1233 txq_free_elts(txq_ctrl);
1234 mlx5_mr_btree_free(&txq_ctrl->txq.mr_ctrl.cache_bh);
1236 LIST_REMOVE(txq_ctrl, next);
1237 mlx5_free(txq_ctrl);
1238 (*priv->txqs)[idx] = NULL;
1239 dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
1244 * Verify if the queue can be released.
1247 * Pointer to Ethernet device.
1252 * 1 if the queue can be released.
1255 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
1257 struct mlx5_priv *priv = dev->data->dev_private;
1258 struct mlx5_txq_ctrl *txq;
1260 if (!(*priv->txqs)[idx])
1262 txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1263 return (rte_atomic32_read(&txq->refcnt) == 1);
1267 * Verify the Tx Queue list is empty
1270 * Pointer to Ethernet device.
1273 * The number of object not released.
1276 mlx5_txq_verify(struct rte_eth_dev *dev)
1278 struct mlx5_priv *priv = dev->data->dev_private;
1279 struct mlx5_txq_ctrl *txq_ctrl;
1282 LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
1283 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
1284 dev->data->port_id, txq_ctrl->txq.idx);
1291 * Set the Tx queue dynamic timestamp (mask and offset)
1294 * Pointer to the Ethernet device structure.
1297 mlx5_txq_dynf_timestamp_set(struct rte_eth_dev *dev)
1299 struct mlx5_priv *priv = dev->data->dev_private;
1300 struct mlx5_dev_ctx_shared *sh = priv->sh;
1301 struct mlx5_txq_data *data;
1306 nbit = rte_mbuf_dynflag_lookup
1307 (RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
1308 off = rte_mbuf_dynfield_lookup
1309 (RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL);
1310 if (nbit > 0 && off >= 0 && sh->txpp.refcnt)
1311 mask = 1ULL << nbit;
1312 for (i = 0; i != priv->txqs_n; ++i) {
1313 data = (*priv->txqs)[i];
1317 data->ts_mask = mask;
1318 data->ts_offset = off;