4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Tx queues configuration for mlx4 driver.
45 /* Verbs headers do not support -pedantic. */
47 #pragma GCC diagnostic ignored "-Wpedantic"
49 #include <infiniband/verbs.h>
51 #pragma GCC diagnostic error "-Wpedantic"
54 #include <rte_common.h>
55 #include <rte_errno.h>
56 #include <rte_ethdev.h>
57 #include <rte_malloc.h>
59 #include <rte_mempool.h>
62 #include "mlx4_autoconf.h"
63 #include "mlx4_rxtx.h"
64 #include "mlx4_utils.h"
67 * Free Tx queue elements.
70 * Pointer to Tx queue structure.
73 mlx4_txq_free_elts(struct txq *txq)
75 unsigned int elts_head = txq->elts_head;
76 unsigned int elts_tail = txq->elts_tail;
77 struct txq_elt (*elts)[txq->elts_n] = txq->elts;
79 DEBUG("%p: freeing WRs", (void *)txq);
80 while (elts_tail != elts_head) {
81 struct txq_elt *elt = &(*elts)[elts_tail];
83 assert(elt->buf != NULL);
84 rte_pktmbuf_free(elt->buf);
86 if (++elts_tail == RTE_DIM(*elts))
89 txq->elts_tail = txq->elts_head;
92 struct txq_mp2mr_mbuf_check_data {
97 * Callback function for rte_mempool_obj_iter() to check whether a given
98 * mempool object looks like a mbuf.
101 * The mempool pointer
103 * Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
108 * Object index, unused.
111 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
114 struct txq_mp2mr_mbuf_check_data *data = arg;
115 struct rte_mbuf *buf = obj;
119 * Check whether mbuf structure fits element size and whether mempool
122 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
127 * Iterator function for rte_mempool_walk() to register existing mempools and
128 * fill the MP to MR cache of a Tx queue.
131 * Memory Pool to register.
133 * Pointer to Tx queue structure.
136 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
138 struct txq *txq = arg;
139 struct txq_mp2mr_mbuf_check_data data = {
143 /* Register mempool only if the first element looks like a mbuf. */
144 if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
147 mlx4_txq_mp2mr(txq, mp);
151 * DPDK callback to configure a Tx queue.
154 * Pointer to Ethernet device structure.
158 * Number of descriptors to configure in queue.
160 * NUMA socket on which memory must be allocated.
162 * Thresholds parameters.
165 * 0 on success, negative errno value otherwise and rte_errno is set.
168 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
169 unsigned int socket, const struct rte_eth_txconf *conf)
171 struct priv *priv = dev->data->dev_private;
172 struct txq_elt (*elts)[desc];
173 struct ibv_qp_init_attr qp_init_attr;
175 struct mlx4_malloc_vec vec[] = {
177 .align = RTE_CACHE_LINE_SIZE,
178 .size = sizeof(*txq),
179 .addr = (void **)&txq,
182 .align = RTE_CACHE_LINE_SIZE,
183 .size = sizeof(*elts),
184 .addr = (void **)&elts,
189 (void)conf; /* Thresholds configuration (ignored). */
190 DEBUG("%p: configuring queue %u for %u descriptors",
191 (void *)dev, idx, desc);
192 if (idx >= dev->data->nb_tx_queues) {
193 rte_errno = EOVERFLOW;
194 ERROR("%p: queue index out of range (%u >= %u)",
195 (void *)dev, idx, dev->data->nb_tx_queues);
198 txq = dev->data->tx_queues[idx];
201 DEBUG("%p: Tx queue %u already configured, release it first",
207 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
210 /* Allocate and initialize Tx queue. */
211 mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
213 ERROR("%p: unable to allocate queue index %u",
227 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
228 * packets or at least 4 times per ring.
231 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
233 RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
235 txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
238 ERROR("%p: CQ creation failure: %s",
239 (void *)dev, strerror(rte_errno));
242 qp_init_attr = (struct ibv_qp_init_attr){
247 RTE_MIN(priv->device_attr.max_qp_wr, desc),
249 .max_inline_data = MLX4_PMD_MAX_INLINE,
251 .qp_type = IBV_QPT_RAW_PACKET,
252 /* No completion events must occur by default. */
255 txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
257 rte_errno = errno ? errno : EINVAL;
258 ERROR("%p: QP creation failure: %s",
259 (void *)dev, strerror(rte_errno));
262 txq->max_inline = qp_init_attr.cap.max_inline_data;
265 &(struct ibv_qp_attr){
266 .qp_state = IBV_QPS_INIT,
267 .port_num = priv->port,
269 IBV_QP_STATE | IBV_QP_PORT);
272 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
273 (void *)dev, strerror(rte_errno));
278 &(struct ibv_qp_attr){
279 .qp_state = IBV_QPS_RTR,
284 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
285 (void *)dev, strerror(rte_errno));
290 &(struct ibv_qp_attr){
291 .qp_state = IBV_QPS_RTS,
296 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
297 (void *)dev, strerror(rte_errno));
300 /* Pre-register known mempools. */
301 rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
302 DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
303 dev->data->tx_queues[idx] = txq;
306 dev->data->tx_queues[idx] = NULL;
308 mlx4_tx_queue_release(txq);
310 assert(rte_errno > 0);
315 * DPDK callback to release a Tx queue.
318 * Generic Tx queue pointer.
321 mlx4_tx_queue_release(void *dpdk_txq)
323 struct txq *txq = (struct txq *)dpdk_txq;
330 for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
331 if (priv->dev->data->tx_queues[i] == txq) {
332 DEBUG("%p: removing Tx queue %p from list",
333 (void *)priv->dev, (void *)txq);
334 priv->dev->data->tx_queues[i] = NULL;
337 mlx4_txq_free_elts(txq);
339 claim_zero(ibv_destroy_qp(txq->qp));
341 claim_zero(ibv_destroy_cq(txq->cq));
342 for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
343 if (!txq->mp2mr[i].mp)
345 assert(txq->mp2mr[i].mr);
346 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));