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.
19 /* Verbs headers do not support -pedantic. */
21 #pragma GCC diagnostic ignored "-Wpedantic"
23 #include <infiniband/verbs.h>
25 #pragma GCC diagnostic error "-Wpedantic"
28 #include <rte_common.h>
29 #include <rte_errno.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_malloc.h>
33 #include <rte_mempool.h>
36 #include "mlx4_glue.h"
38 #include "mlx4_rxtx.h"
39 #include "mlx4_utils.h"
42 * Initialize Tx UAR registers for primary process.
45 * Pointer to Tx queue structure.
48 txq_uar_init(struct txq *txq)
50 struct mlx4_priv *priv = txq->priv;
51 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(priv));
53 MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
55 ppriv->uar_table[txq->stats.idx] = txq->msq.db;
58 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
60 * Remap UAR register of a Tx queue for secondary process.
62 * Remapped address is stored at the table in the process private structure of
63 * the device, indexed by queue index.
66 * Pointer to Tx queue structure.
68 * Verbs file descriptor to map UAR pages.
71 * 0 on success, a negative errno value otherwise and rte_errno is set.
74 txq_uar_init_secondary(struct txq *txq, int fd)
76 struct mlx4_priv *priv = txq->priv;
77 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(priv));
81 const size_t page_size = sysconf(_SC_PAGESIZE);
85 * As rdma-core, UARs are mapped in size of OS page
86 * size. Ref to libmlx4 function: mlx4_init_context()
88 uar_va = (uintptr_t)txq->msq.db;
89 offset = uar_va & (page_size - 1); /* Offset in page. */
90 addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
91 txq->msq.uar_mmap_offset);
92 if (addr == MAP_FAILED) {
93 ERROR("port %u mmap failed for BF reg of txq %u",
94 txq->port_id, txq->stats.idx);
98 addr = RTE_PTR_ADD(addr, offset);
99 ppriv->uar_table[txq->stats.idx] = addr;
104 * Unmap UAR register of a Tx queue for secondary process.
107 * Pointer to Tx queue structure.
110 txq_uar_uninit_secondary(struct txq *txq)
112 struct mlx4_proc_priv *ppriv = MLX4_PROC_PRIV(PORT_ID(txq->priv));
113 const size_t page_size = sysconf(_SC_PAGESIZE);
116 addr = ppriv->uar_table[txq->stats.idx];
117 munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
121 * Initialize Tx UAR registers for secondary process.
124 * Pointer to Ethernet device.
126 * Verbs file descriptor to map UAR pages.
129 * 0 on success, a negative errno value otherwise and rte_errno is set.
132 mlx4_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
134 const unsigned int txqs_n = dev->data->nb_tx_queues;
139 MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
140 for (i = 0; i != txqs_n; ++i) {
141 txq = dev->data->tx_queues[i];
144 MLX4_ASSERT(txq->stats.idx == (uint16_t)i);
145 ret = txq_uar_init_secondary(txq, fd);
153 txq = dev->data->tx_queues[i];
156 txq_uar_uninit_secondary(txq);
162 mlx4_tx_uar_init_secondary(struct rte_eth_dev *dev __rte_unused,
165 MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
166 ERROR("UAR remap is not supported");
173 * Free Tx queue elements.
176 * Pointer to Tx queue structure.
179 mlx4_txq_free_elts(struct txq *txq)
181 unsigned int elts_head = txq->elts_head;
182 unsigned int elts_tail = txq->elts_tail;
183 struct txq_elt (*elts)[txq->elts_n] = txq->elts;
184 unsigned int elts_m = txq->elts_n - 1;
186 DEBUG("%p: freeing WRs", (void *)txq);
187 while (elts_tail != elts_head) {
188 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
190 MLX4_ASSERT(elt->buf != NULL);
191 rte_pktmbuf_free(elt->buf);
195 txq->elts_tail = txq->elts_head;
199 * Retrieves information needed in order to directly access the Tx queue.
202 * Pointer to Tx queue structure.
204 * Pointer to device information for this Tx queue.
207 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
209 struct mlx4_sq *sq = &txq->msq;
210 struct mlx4_cq *cq = &txq->mcq;
211 struct mlx4dv_qp *dqp = mlxdv->qp.out;
212 struct mlx4dv_cq *dcq = mlxdv->cq.out;
214 /* Total length, including headroom and spare WQEs. */
215 sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
216 sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
217 sq->eob = sq->buf + sq->size;
218 uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
219 /* Continuous headroom size bytes must always stay freed. */
220 sq->remain_size = sq->size - headroom_size;
221 sq->owner_opcode = MLX4_OPCODE_SEND | (0u << MLX4_SQ_OWNER_BIT);
222 sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
223 (0u << MLX4_SQ_OWNER_BIT));
224 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
225 sq->uar_mmap_offset = dqp->uar_mmap_offset;
227 sq->uar_mmap_offset = -1; /* Make mmap() fail. */
230 sq->doorbell_qpn = dqp->doorbell_qpn;
231 cq->buf = dcq->buf.buf;
232 cq->cqe_cnt = dcq->cqe_cnt;
233 cq->set_ci_db = dcq->set_ci_db;
234 cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
238 * Returns the per-port supported offloads.
241 * Pointer to private structure.
244 * Supported Tx offloads.
247 mlx4_get_tx_port_offloads(struct mlx4_priv *priv)
249 uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
252 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
253 DEV_TX_OFFLOAD_UDP_CKSUM |
254 DEV_TX_OFFLOAD_TCP_CKSUM);
257 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
258 if (priv->hw_csum_l2tun) {
259 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
261 offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
262 DEV_TX_OFFLOAD_GRE_TNL_TSO);
268 * DPDK callback to configure a Tx queue.
271 * Pointer to Ethernet device structure.
275 * Number of descriptors to configure in queue.
277 * NUMA socket on which memory must be allocated.
279 * Thresholds parameters.
282 * 0 on success, negative errno value otherwise and rte_errno is set.
285 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
286 unsigned int socket, const struct rte_eth_txconf *conf)
288 struct mlx4_priv *priv = dev->data->dev_private;
289 struct mlx4dv_obj mlxdv;
290 struct mlx4dv_qp dv_qp;
291 struct mlx4dv_cq dv_cq;
292 struct txq_elt (*elts)[rte_align32pow2(desc)];
293 struct ibv_qp_init_attr qp_init_attr;
296 struct mlx4_malloc_vec vec[] = {
298 .align = RTE_CACHE_LINE_SIZE,
299 .size = sizeof(*txq),
300 .addr = (void **)&txq,
303 .align = RTE_CACHE_LINE_SIZE,
304 .size = sizeof(*elts),
305 .addr = (void **)&elts,
308 .align = RTE_CACHE_LINE_SIZE,
309 .size = MLX4_MAX_WQE_SIZE,
310 .addr = (void **)&bounce_buf,
316 offloads = conf->offloads | dev->data->dev_conf.txmode.offloads;
317 DEBUG("%p: configuring queue %u for %u descriptors",
318 (void *)dev, idx, desc);
319 if (idx >= dev->data->nb_tx_queues) {
320 rte_errno = EOVERFLOW;
321 ERROR("%p: queue index out of range (%u >= %u)",
322 (void *)dev, idx, dev->data->nb_tx_queues);
325 txq = dev->data->tx_queues[idx];
328 DEBUG("%p: Tx queue %u already configured, release it first",
334 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
337 if (desc != RTE_DIM(*elts)) {
338 desc = RTE_DIM(*elts);
339 WARN("%p: increased number of descriptors in Tx queue %u"
340 " to the next power of two (%u)",
341 (void *)dev, idx, desc);
343 /* Allocate and initialize Tx queue. */
344 mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
346 ERROR("%p: unable to allocate queue index %u",
352 .port_id = dev->data->port_id,
362 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
363 * packets or at least 4 times per ring.
366 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
368 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
369 .csum = priv->hw_csum &&
370 (offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
371 DEV_TX_OFFLOAD_UDP_CKSUM |
372 DEV_TX_OFFLOAD_TCP_CKSUM)),
373 .csum_l2tun = priv->hw_csum_l2tun &&
375 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
376 /* Enable Tx loopback for VF devices. */
378 .bounce_buf = bounce_buf,
380 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_TX_QUEUE;
381 priv->verbs_alloc_ctx.obj = txq;
382 txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
385 ERROR("%p: CQ creation failure: %s",
386 (void *)dev, strerror(rte_errno));
389 qp_init_attr = (struct ibv_qp_init_attr){
394 RTE_MIN(priv->device_attr.max_qp_wr, desc),
396 .max_inline_data = MLX4_PMD_MAX_INLINE,
398 .qp_type = IBV_QPT_RAW_PACKET,
399 /* No completion events must occur by default. */
402 txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
404 rte_errno = errno ? errno : EINVAL;
405 ERROR("%p: QP creation failure: %s",
406 (void *)dev, strerror(rte_errno));
409 txq->max_inline = qp_init_attr.cap.max_inline_data;
410 ret = mlx4_glue->modify_qp
412 &(struct ibv_qp_attr){
413 .qp_state = IBV_QPS_INIT,
414 .port_num = priv->port,
416 IBV_QP_STATE | IBV_QP_PORT);
419 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
420 (void *)dev, strerror(rte_errno));
423 ret = mlx4_glue->modify_qp
425 &(struct ibv_qp_attr){
426 .qp_state = IBV_QPS_RTR,
431 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
432 (void *)dev, strerror(rte_errno));
435 ret = mlx4_glue->modify_qp
437 &(struct ibv_qp_attr){
438 .qp_state = IBV_QPS_RTS,
443 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
444 (void *)dev, strerror(rte_errno));
447 /* Retrieve device queue information. */
448 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
449 dv_qp = (struct mlx4dv_qp){
450 .comp_mask = MLX4DV_QP_MASK_UAR_MMAP_OFFSET,
453 mlxdv.cq.in = txq->cq;
454 mlxdv.cq.out = &dv_cq;
455 mlxdv.qp.in = txq->qp;
456 mlxdv.qp.out = &dv_qp;
457 ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
460 ERROR("%p: failed to obtain information needed for"
461 " accessing the device queues", (void *)dev);
464 #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
465 if (!(dv_qp.comp_mask & MLX4DV_QP_MASK_UAR_MMAP_OFFSET)) {
466 WARN("%p: failed to obtain UAR mmap offset", (void *)dev);
467 dv_qp.uar_mmap_offset = -1; /* Make mmap() fail. */
470 mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
472 /* Save first wqe pointer in the first element. */
473 (&(*txq->elts)[0])->wqe =
474 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
475 if (mlx4_mr_btree_init(&txq->mr_ctrl.cache_bh,
476 MLX4_MR_BTREE_CACHE_N, socket)) {
477 /* rte_errno is already set. */
480 /* Save pointer of global generation number to check memory event. */
481 txq->mr_ctrl.dev_gen_ptr = &priv->mr.dev_gen;
482 DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
483 dev->data->tx_queues[idx] = txq;
484 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_NONE;
487 dev->data->tx_queues[idx] = NULL;
489 mlx4_tx_queue_release(txq);
491 MLX4_ASSERT(rte_errno > 0);
492 priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_NONE;
497 * DPDK callback to release a Tx queue.
500 * Generic Tx queue pointer.
503 mlx4_tx_queue_release(void *dpdk_txq)
505 struct txq *txq = (struct txq *)dpdk_txq;
506 struct mlx4_priv *priv;
512 for (i = 0; i != ETH_DEV(priv)->data->nb_tx_queues; ++i)
513 if (ETH_DEV(priv)->data->tx_queues[i] == txq) {
514 DEBUG("%p: removing Tx queue %p from list",
515 (void *)ETH_DEV(priv), (void *)txq);
516 ETH_DEV(priv)->data->tx_queues[i] = NULL;
519 mlx4_txq_free_elts(txq);
521 claim_zero(mlx4_glue->destroy_qp(txq->qp));
523 claim_zero(mlx4_glue->destroy_cq(txq->cq));
524 mlx4_mr_btree_free(&txq->mr_ctrl.cache_bh);