1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Tx queues configuration for mlx4 driver.
20 /* Verbs headers do not support -pedantic. */
22 #pragma GCC diagnostic ignored "-Wpedantic"
24 #include <infiniband/verbs.h>
26 #pragma GCC diagnostic error "-Wpedantic"
29 #include <rte_common.h>
30 #include <rte_errno.h>
31 #include <rte_ethdev_driver.h>
32 #include <rte_malloc.h>
34 #include <rte_mempool.h>
37 #include "mlx4_glue.h"
39 #include "mlx4_rxtx.h"
40 #include "mlx4_utils.h"
43 * Initialize Tx UAR registers for primary process.
46 * Pointer to Tx queue structure.
49 txq_uar_init(struct txq *txq)
51 struct mlx4_priv *priv = txq->priv;
52 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(priv));
54 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
56 ppriv->uar_table[txq->stats.idx] = txq->msq.db;
59 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
61 * Remap UAR register of a Tx queue for secondary process.
63 * Remapped address is stored at the table in the process private structure of
64 * the device, indexed by queue index.
67 * Pointer to Tx queue structure.
69 * Verbs file descriptor to map UAR pages.
72 * 0 on success, a negative errno value otherwise and rte_errno is set.
75 txq_uar_init_secondary(struct txq *txq, int fd)
77 struct mlx4_priv *priv = txq->priv;
78 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(priv));
82 const size_t page_size = sysconf(_SC_PAGESIZE);
86 * As rdma-core, UARs are mapped in size of OS page
87 * size. Ref to libmlx4 function: mlx4_init_context()
89 uar_va = (uintptr_t)txq->msq.db;
90 offset = uar_va & (page_size - 1); /* Offset in page. */
91 addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
92 txq->msq.uar_mmap_offset);
93 if (addr == MAP_FAILED) {
94 ERROR("port %u mmap failed for BF reg of txq %u",
95 txq->port_id, txq->stats.idx);
99 addr = RTE_PTR_ADD(addr, offset);
100 ppriv->uar_table[txq->stats.idx] = addr;
105 * Unmap UAR register of a Tx queue for secondary process.
108 * Pointer to Tx queue structure.
111 txq_uar_uninit_secondary(struct txq *txq)
113 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(txq->priv));
114 const size_t page_size = sysconf(_SC_PAGESIZE);
117 addr = ppriv->uar_table[txq->stats.idx];
118 munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
122 * Initialize Tx UAR registers for secondary process.
125 * Pointer to Ethernet device.
127 * Verbs file descriptor to map UAR pages.
130 * 0 on success, a negative errno value otherwise and rte_errno is set.
133 mlx4_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
135 const unsigned int txqs_n = dev->data->nb_tx_queues;
140 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
141 for (i = 0; i != txqs_n; ++i) {
142 txq = dev->data->tx_queues[i];
145 assert(txq->stats.idx == (uint16_t)i);
146 ret = txq_uar_init_secondary(txq, fd);
154 txq = dev->data->tx_queues[i];
157 txq_uar_uninit_secondary(txq);
163 mlx4_tx_uar_init_secondary(struct rte_eth_dev *dev __rte_unused,
166 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
167 ERROR("UAR remap is not supported");
174 * Free Tx queue elements.
177 * Pointer to Tx queue structure.
180 mlx4_txq_free_elts(struct txq *txq)
182 unsigned int elts_head = txq->elts_head;
183 unsigned int elts_tail = txq->elts_tail;
184 struct txq_elt (*elts)[txq->elts_n] = txq->elts;
185 unsigned int elts_m = txq->elts_n - 1;
187 DEBUG("%p: freeing WRs", (void *)txq);
188 while (elts_tail != elts_head) {
189 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
191 assert(elt->buf != NULL);
192 rte_pktmbuf_free(elt->buf);
196 txq->elts_tail = txq->elts_head;
200 * Retrieves information needed in order to directly access the Tx queue.
203 * Pointer to Tx queue structure.
205 * Pointer to device information for this Tx queue.
208 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
210 struct mlx4_sq *sq = &txq->msq;
211 struct mlx4_cq *cq = &txq->mcq;
212 struct mlx4dv_qp *dqp = mlxdv->qp.out;
213 struct mlx4dv_cq *dcq = mlxdv->cq.out;
215 /* Total length, including headroom and spare WQEs. */
216 sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
217 sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
218 sq->eob = sq->buf + sq->size;
219 uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
220 /* Continuous headroom size bytes must always stay freed. */
221 sq->remain_size = sq->size - headroom_size;
222 sq->owner_opcode = MLX4_OPCODE_SEND | (0u << MLX4_SQ_OWNER_BIT);
223 sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
224 (0u << MLX4_SQ_OWNER_BIT));
225 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
226 sq->uar_mmap_offset = dqp->uar_mmap_offset;
228 sq->uar_mmap_offset = -1; /* Make mmap() fail. */
231 sq->doorbell_qpn = dqp->doorbell_qpn;
232 cq->buf = dcq->buf.buf;
233 cq->cqe_cnt = dcq->cqe_cnt;
234 cq->set_ci_db = dcq->set_ci_db;
235 cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
239 * Returns the per-port supported offloads.
242 * Pointer to private structure.
245 * Supported Tx offloads.
248 mlx4_get_tx_port_offloads(struct mlx4_priv *priv)
250 uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
253 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
254 DEV_TX_OFFLOAD_UDP_CKSUM |
255 DEV_TX_OFFLOAD_TCP_CKSUM);
258 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
259 if (priv->hw_csum_l2tun) {
260 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
262 offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
263 DEV_TX_OFFLOAD_GRE_TNL_TSO);
269 * DPDK callback to configure a Tx queue.
272 * Pointer to Ethernet device structure.
276 * Number of descriptors to configure in queue.
278 * NUMA socket on which memory must be allocated.
280 * Thresholds parameters.
283 * 0 on success, negative errno value otherwise and rte_errno is set.
286 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
287 unsigned int socket, const struct rte_eth_txconf *conf)
289 struct mlx4_priv *priv = dev->data->dev_private;
290 struct mlx4dv_obj mlxdv;
291 struct mlx4dv_qp dv_qp;
292 struct mlx4dv_cq dv_cq;
293 struct txq_elt (*elts)[rte_align32pow2(desc)];
294 struct ibv_qp_init_attr qp_init_attr;
297 struct mlx4_malloc_vec vec[] = {
299 .align = RTE_CACHE_LINE_SIZE,
300 .size = sizeof(*txq),
301 .addr = (void **)&txq,
304 .align = RTE_CACHE_LINE_SIZE,
305 .size = sizeof(*elts),
306 .addr = (void **)&elts,
309 .align = RTE_CACHE_LINE_SIZE,
310 .size = MLX4_MAX_WQE_SIZE,
311 .addr = (void **)&bounce_buf,
317 offloads = conf->offloads | dev->data->dev_conf.txmode.offloads;
318 DEBUG("%p: configuring queue %u for %u descriptors",
319 (void *)dev, idx, desc);
320 if (idx >= dev->data->nb_tx_queues) {
321 rte_errno = EOVERFLOW;
322 ERROR("%p: queue index out of range (%u >= %u)",
323 (void *)dev, idx, dev->data->nb_tx_queues);
326 txq = dev->data->tx_queues[idx];
329 DEBUG("%p: Tx queue %u already configured, release it first",
335 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
338 if (desc != RTE_DIM(*elts)) {
339 desc = RTE_DIM(*elts);
340 WARN("%p: increased number of descriptors in Tx queue %u"
341 " to the next power of two (%u)",
342 (void *)dev, idx, desc);
344 /* Allocate and initialize Tx queue. */
345 mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
347 ERROR("%p: unable to allocate queue index %u",
353 .port_id = dev->data->port_id,
363 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
364 * packets or at least 4 times per ring.
367 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
369 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
370 .csum = priv->hw_csum &&
371 (offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
372 DEV_TX_OFFLOAD_UDP_CKSUM |
373 DEV_TX_OFFLOAD_TCP_CKSUM)),
374 .csum_l2tun = priv->hw_csum_l2tun &&
376 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
377 /* Enable Tx loopback for VF devices. */
379 .bounce_buf = bounce_buf,
381 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_TX_QUEUE;
382 priv->verbs_alloc_ctx.obj = txq;
383 txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
386 ERROR("%p: CQ creation failure: %s",
387 (void *)dev, strerror(rte_errno));
390 qp_init_attr = (struct ibv_qp_init_attr){
395 RTE_MIN(priv->device_attr.max_qp_wr, desc),
397 .max_inline_data = MLX4_PMD_MAX_INLINE,
399 .qp_type = IBV_QPT_RAW_PACKET,
400 /* No completion events must occur by default. */
403 txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
405 rte_errno = errno ? errno : EINVAL;
406 ERROR("%p: QP creation failure: %s",
407 (void *)dev, strerror(rte_errno));
410 txq->max_inline = qp_init_attr.cap.max_inline_data;
411 ret = mlx4_glue->modify_qp
413 &(struct ibv_qp_attr){
414 .qp_state = IBV_QPS_INIT,
415 .port_num = priv->port,
417 IBV_QP_STATE | IBV_QP_PORT);
420 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
421 (void *)dev, strerror(rte_errno));
424 ret = mlx4_glue->modify_qp
426 &(struct ibv_qp_attr){
427 .qp_state = IBV_QPS_RTR,
432 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
433 (void *)dev, strerror(rte_errno));
436 ret = mlx4_glue->modify_qp
438 &(struct ibv_qp_attr){
439 .qp_state = IBV_QPS_RTS,
444 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
445 (void *)dev, strerror(rte_errno));
448 /* Retrieve device queue information. */
449 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
450 dv_qp = (struct mlx4dv_qp){
451 .comp_mask = MLX4DV_QP_MASK_UAR_MMAP_OFFSET,
454 mlxdv.cq.in = txq->cq;
455 mlxdv.cq.out = &dv_cq;
456 mlxdv.qp.in = txq->qp;
457 mlxdv.qp.out = &dv_qp;
458 ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
461 ERROR("%p: failed to obtain information needed for"
462 " accessing the device queues", (void *)dev);
465 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
466 if (!(dv_qp.comp_mask & MLX4DV_QP_MASK_UAR_MMAP_OFFSET)) {
467 WARN("%p: failed to obtain UAR mmap offset", (void *)dev);
468 dv_qp.uar_mmap_offset = -1; /* Make mmap() fail. */
471 mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
473 /* Save first wqe pointer in the first element. */
474 (&(*txq->elts)[0])->wqe =
475 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
476 if (mlx4_mr_btree_init(&txq->mr_ctrl.cache_bh,
477 MLX4_MR_BTREE_CACHE_N, socket)) {
478 /* rte_errno is already set. */
481 /* Save pointer of global generation number to check memory event. */
482 txq->mr_ctrl.dev_gen_ptr = &priv->mr.dev_gen;
483 DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
484 dev->data->tx_queues[idx] = txq;
485 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_NONE;
488 dev->data->tx_queues[idx] = NULL;
490 mlx4_tx_queue_release(txq);
492 assert(rte_errno > 0);
493 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_NONE;
498 * DPDK callback to release a Tx queue.
501 * Generic Tx queue pointer.
504 mlx4_tx_queue_release(void *dpdk_txq)
506 struct txq *txq = (struct txq *)dpdk_txq;
507 struct mlx4_priv *priv;
513 for (i = 0; i != ETH_DEV(priv)->data->nb_tx_queues; ++i)
514 if (ETH_DEV(priv)->data->tx_queues[i] == txq) {
515 DEBUG("%p: removing Tx queue %p from list",
516 (void *)ETH_DEV(priv), (void *)txq);
517 ETH_DEV(priv)->data->tx_queues[i] = NULL;
520 mlx4_txq_free_elts(txq);
522 claim_zero(mlx4_glue->destroy_qp(txq->qp));
524 claim_zero(mlx4_glue->destroy_cq(txq->cq));
525 mlx4_mr_btree_free(&txq->mr_ctrl.cache_bh);