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.
18 /* Verbs headers do not support -pedantic. */
20 #pragma GCC diagnostic ignored "-Wpedantic"
22 #include <infiniband/verbs.h>
24 #pragma GCC diagnostic error "-Wpedantic"
27 #include <rte_common.h>
28 #include <rte_errno.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_malloc.h>
32 #include <rte_mempool.h>
35 #include "mlx4_glue.h"
37 #include "mlx4_rxtx.h"
38 #include "mlx4_utils.h"
41 * Free Tx queue elements.
44 * Pointer to Tx queue structure.
47 mlx4_txq_free_elts(struct txq *txq)
49 unsigned int elts_head = txq->elts_head;
50 unsigned int elts_tail = txq->elts_tail;
51 struct txq_elt (*elts)[txq->elts_n] = txq->elts;
52 unsigned int elts_m = txq->elts_n - 1;
54 DEBUG("%p: freeing WRs", (void *)txq);
55 while (elts_tail != elts_head) {
56 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
58 assert(elt->buf != NULL);
59 rte_pktmbuf_free(elt->buf);
63 txq->elts_tail = txq->elts_head;
66 struct txq_mp2mr_mbuf_check_data {
71 * Callback function for rte_mempool_obj_iter() to check whether a given
72 * mempool object looks like a mbuf.
77 * Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
82 * Object index, unused.
85 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
88 struct txq_mp2mr_mbuf_check_data *data = arg;
89 struct rte_mbuf *buf = obj;
93 * Check whether mbuf structure fits element size and whether mempool
96 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
101 * Iterator function for rte_mempool_walk() to register existing mempools and
102 * fill the MP to MR cache of a Tx queue.
105 * Memory Pool to register.
107 * Pointer to Tx queue structure.
110 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
112 struct txq *txq = arg;
113 struct txq_mp2mr_mbuf_check_data data = {
117 /* Register mempool only if the first element looks like a mbuf. */
118 if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
121 mlx4_txq_mp2mr(txq, mp);
125 * Retrieves information needed in order to directly access the Tx queue.
128 * Pointer to Tx queue structure.
130 * Pointer to device information for this Tx queue.
133 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
135 struct mlx4_sq *sq = &txq->msq;
136 struct mlx4_cq *cq = &txq->mcq;
137 struct mlx4dv_qp *dqp = mlxdv->qp.out;
138 struct mlx4dv_cq *dcq = mlxdv->cq.out;
140 /* Total length, including headroom and spare WQEs. */
141 sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
142 sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
143 sq->eob = sq->buf + sq->size;
144 uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
145 /* Continuous headroom size bytes must always stay freed. */
146 sq->remain_size = sq->size - headroom_size;
147 sq->owner_opcode = MLX4_OPCODE_SEND | (0 << MLX4_SQ_OWNER_BIT);
148 sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
149 (0 << MLX4_SQ_OWNER_BIT));
151 sq->doorbell_qpn = dqp->doorbell_qpn;
152 cq->buf = dcq->buf.buf;
153 cq->cqe_cnt = dcq->cqe_cnt;
154 cq->set_ci_db = dcq->set_ci_db;
155 cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
159 * Returns the per-port supported offloads.
162 * Pointer to private structure.
165 * Supported Tx offloads.
168 mlx4_get_tx_port_offloads(struct priv *priv)
170 uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
173 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
174 DEV_TX_OFFLOAD_UDP_CKSUM |
175 DEV_TX_OFFLOAD_TCP_CKSUM);
177 if (priv->hw_csum_l2tun)
178 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
183 * DPDK callback to configure a Tx queue.
186 * Pointer to Ethernet device structure.
190 * Number of descriptors to configure in queue.
192 * NUMA socket on which memory must be allocated.
194 * Thresholds parameters.
197 * 0 on success, negative errno value otherwise and rte_errno is set.
200 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
201 unsigned int socket, const struct rte_eth_txconf *conf)
203 struct priv *priv = dev->data->dev_private;
204 struct mlx4dv_obj mlxdv;
205 struct mlx4dv_qp dv_qp;
206 struct mlx4dv_cq dv_cq;
207 struct txq_elt (*elts)[rte_align32pow2(desc)];
208 struct ibv_qp_init_attr qp_init_attr;
211 struct mlx4_malloc_vec vec[] = {
213 .align = RTE_CACHE_LINE_SIZE,
214 .size = sizeof(*txq),
215 .addr = (void **)&txq,
218 .align = RTE_CACHE_LINE_SIZE,
219 .size = sizeof(*elts),
220 .addr = (void **)&elts,
223 .align = RTE_CACHE_LINE_SIZE,
224 .size = MLX4_MAX_WQE_SIZE,
225 .addr = (void **)&bounce_buf,
231 offloads = conf->offloads | dev->data->dev_conf.txmode.offloads;
233 DEBUG("%p: configuring queue %u for %u descriptors",
234 (void *)dev, idx, desc);
236 if (idx >= dev->data->nb_tx_queues) {
237 rte_errno = EOVERFLOW;
238 ERROR("%p: queue index out of range (%u >= %u)",
239 (void *)dev, idx, dev->data->nb_tx_queues);
242 txq = dev->data->tx_queues[idx];
245 DEBUG("%p: Tx queue %u already configured, release it first",
251 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
254 if (desc != RTE_DIM(*elts)) {
255 desc = RTE_DIM(*elts);
256 WARN("%p: increased number of descriptors in Tx queue %u"
257 " to the next power of two (%u)",
258 (void *)dev, idx, desc);
260 /* Allocate and initialize Tx queue. */
261 mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
263 ERROR("%p: unable to allocate queue index %u",
278 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
279 * packets or at least 4 times per ring.
282 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
284 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
285 .csum = priv->hw_csum &&
286 (offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
287 DEV_TX_OFFLOAD_UDP_CKSUM |
288 DEV_TX_OFFLOAD_TCP_CKSUM)),
289 .csum_l2tun = priv->hw_csum_l2tun &&
291 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
292 /* Enable Tx loopback for VF devices. */
294 .bounce_buf = bounce_buf,
296 txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
299 ERROR("%p: CQ creation failure: %s",
300 (void *)dev, strerror(rte_errno));
303 qp_init_attr = (struct ibv_qp_init_attr){
308 RTE_MIN(priv->device_attr.max_qp_wr, desc),
310 .max_inline_data = MLX4_PMD_MAX_INLINE,
312 .qp_type = IBV_QPT_RAW_PACKET,
313 /* No completion events must occur by default. */
316 txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
318 rte_errno = errno ? errno : EINVAL;
319 ERROR("%p: QP creation failure: %s",
320 (void *)dev, strerror(rte_errno));
323 txq->max_inline = qp_init_attr.cap.max_inline_data;
324 ret = mlx4_glue->modify_qp
326 &(struct ibv_qp_attr){
327 .qp_state = IBV_QPS_INIT,
328 .port_num = priv->port,
330 IBV_QP_STATE | IBV_QP_PORT);
333 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
334 (void *)dev, strerror(rte_errno));
337 ret = mlx4_glue->modify_qp
339 &(struct ibv_qp_attr){
340 .qp_state = IBV_QPS_RTR,
345 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
346 (void *)dev, strerror(rte_errno));
349 ret = mlx4_glue->modify_qp
351 &(struct ibv_qp_attr){
352 .qp_state = IBV_QPS_RTS,
357 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
358 (void *)dev, strerror(rte_errno));
361 /* Retrieve device queue information. */
362 mlxdv.cq.in = txq->cq;
363 mlxdv.cq.out = &dv_cq;
364 mlxdv.qp.in = txq->qp;
365 mlxdv.qp.out = &dv_qp;
366 ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
369 ERROR("%p: failed to obtain information needed for"
370 " accessing the device queues", (void *)dev);
373 mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
374 /* Save first wqe pointer in the first element. */
375 (&(*txq->elts)[0])->wqe =
376 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
377 /* Pre-register known mempools. */
378 rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
379 DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
380 dev->data->tx_queues[idx] = txq;
383 dev->data->tx_queues[idx] = NULL;
385 mlx4_tx_queue_release(txq);
387 assert(rte_errno > 0);
392 * DPDK callback to release a Tx queue.
395 * Generic Tx queue pointer.
398 mlx4_tx_queue_release(void *dpdk_txq)
400 struct txq *txq = (struct txq *)dpdk_txq;
407 for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
408 if (priv->dev->data->tx_queues[i] == txq) {
409 DEBUG("%p: removing Tx queue %p from list",
410 (void *)priv->dev, (void *)txq);
411 priv->dev->data->tx_queues[i] = NULL;
414 mlx4_txq_free_elts(txq);
416 claim_zero(mlx4_glue->destroy_qp(txq->qp));
418 claim_zero(mlx4_glue->destroy_cq(txq->cq));
419 for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
420 if (!txq->mp2mr[i].mp)
422 assert(txq->mp2mr[i].mr);
423 mlx4_mr_put(txq->mp2mr[i].mr);