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 * Rx 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>
58 #include <rte_malloc.h>
60 #include <rte_mempool.h>
63 #include "mlx4_flow.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
68 * Allocate Rx queue elements.
71 * Pointer to Rx queue structure.
73 * Number of elements to allocate.
76 * 0 on success, negative errno value otherwise and rte_errno is set.
79 mlx4_rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
82 struct rxq_elt (*elts)[elts_n] =
83 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
88 ERROR("%p: can't allocate packets array", (void *)rxq);
91 /* For each WR (packet). */
92 for (i = 0; (i != elts_n); ++i) {
93 struct rxq_elt *elt = &(*elts)[i];
94 struct ibv_recv_wr *wr = &elt->wr;
95 struct ibv_sge *sge = &(*elts)[i].sge;
96 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
100 ERROR("%p: empty mbuf pool", (void *)rxq);
104 wr->next = &(*elts)[(i + 1)].wr;
107 /* Headroom is reserved by rte_pktmbuf_alloc(). */
108 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
109 /* Buffer is supposed to be empty. */
110 assert(rte_pktmbuf_data_len(buf) == 0);
111 assert(rte_pktmbuf_pkt_len(buf) == 0);
112 /* sge->addr must be able to store a pointer. */
113 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
114 /* SGE keeps its headroom. */
115 sge->addr = (uintptr_t)
116 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
117 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
118 sge->lkey = rxq->mr->lkey;
119 /* Redundant check for tailroom. */
120 assert(sge->length == rte_pktmbuf_tailroom(buf));
122 /* The last WR pointer must be NULL. */
123 (*elts)[(i - 1)].wr.next = NULL;
124 DEBUG("%p: allocated and configured %u single-segment WRs",
125 (void *)rxq, elts_n);
126 rxq->elts_n = elts_n;
132 for (i = 0; (i != RTE_DIM(*elts)); ++i)
133 rte_pktmbuf_free_seg((*elts)[i].buf);
136 DEBUG("%p: failed, freed everything", (void *)rxq);
137 assert(rte_errno > 0);
142 * Free Rx queue elements.
145 * Pointer to Rx queue structure.
148 mlx4_rxq_free_elts(struct rxq *rxq)
151 unsigned int elts_n = rxq->elts_n;
152 struct rxq_elt (*elts)[elts_n] = rxq->elts;
154 DEBUG("%p: freeing WRs", (void *)rxq);
159 for (i = 0; (i != RTE_DIM(*elts)); ++i)
160 rte_pktmbuf_free_seg((*elts)[i].buf);
165 * DPDK callback to configure a Rx queue.
168 * Pointer to Ethernet device structure.
172 * Number of descriptors to configure in queue.
174 * NUMA socket on which memory must be allocated.
176 * Thresholds parameters.
178 * Memory pool for buffer allocations.
181 * 0 on success, negative errno value otherwise and rte_errno is set.
184 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
185 unsigned int socket, const struct rte_eth_rxconf *conf,
186 struct rte_mempool *mp)
188 struct priv *priv = dev->data->dev_private;
189 uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
190 struct rte_flow_error error;
194 (void)conf; /* Thresholds configuration (ignored). */
195 DEBUG("%p: configuring queue %u for %u descriptors",
196 (void *)dev, idx, desc);
197 if (idx >= dev->data->nb_rx_queues) {
198 rte_errno = EOVERFLOW;
199 ERROR("%p: queue index out of range (%u >= %u)",
200 (void *)dev, idx, dev->data->nb_rx_queues);
203 rxq = dev->data->rx_queues[idx];
206 ERROR("%p: Rx queue %u already configured, release it first",
212 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
215 /* Allocate and initialize Rx queue. */
216 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
219 ERROR("%p: unable to allocate queue index %u",
226 .port_id = dev->data->port_id,
230 /* Enable scattered packets support for this queue if necessary. */
231 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
232 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
233 (mb_len - RTE_PKTMBUF_HEADROOM)) {
235 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
236 WARN("%p: scattered mode has been requested but is"
237 " not supported, this may lead to packet loss",
240 WARN("%p: the requested maximum Rx packet size (%u) is"
241 " larger than a single mbuf (%u) and scattered"
242 " mode has not been requested",
244 dev->data->dev_conf.rxmode.max_rx_pkt_len,
245 mb_len - RTE_PKTMBUF_HEADROOM);
247 /* Use the entire Rx mempool as the memory region. */
248 rxq->mr = mlx4_mp2mr(priv->pd, mp);
251 ERROR("%p: MR creation failure: %s",
252 (void *)dev, strerror(rte_errno));
255 if (dev->data->dev_conf.intr_conf.rxq) {
256 rxq->channel = ibv_create_comp_channel(priv->ctx);
257 if (rxq->channel == NULL) {
259 ERROR("%p: Rx interrupt completion channel creation"
261 (void *)dev, strerror(rte_errno));
264 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
265 ERROR("%p: unable to make Rx interrupt completion"
266 " channel non-blocking: %s",
267 (void *)dev, strerror(rte_errno));
271 rxq->cq = ibv_create_cq(priv->ctx, desc, NULL, rxq->channel, 0);
274 ERROR("%p: CQ creation failure: %s",
275 (void *)dev, strerror(rte_errno));
278 rxq->qp = ibv_create_qp
280 &(struct ibv_qp_init_attr){
285 RTE_MIN(priv->device_attr.max_qp_wr,
289 .qp_type = IBV_QPT_RAW_PACKET,
292 rte_errno = errno ? errno : EINVAL;
293 ERROR("%p: QP creation failure: %s",
294 (void *)dev, strerror(rte_errno));
299 &(struct ibv_qp_attr){
300 .qp_state = IBV_QPS_INIT,
301 .port_num = priv->port,
303 IBV_QP_STATE | IBV_QP_PORT);
306 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
307 (void *)dev, strerror(rte_errno));
310 ret = mlx4_rxq_alloc_elts(rxq, desc);
312 ERROR("%p: RXQ allocation failed: %s",
313 (void *)dev, strerror(rte_errno));
316 ret = ibv_post_recv(rxq->qp, &(*rxq->elts)[0].wr,
317 &(struct ibv_recv_wr *){ NULL });
320 ERROR("%p: ibv_post_recv() failed: %s",
322 strerror(rte_errno));
327 &(struct ibv_qp_attr){
328 .qp_state = IBV_QPS_RTR,
333 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
334 (void *)dev, strerror(rte_errno));
337 DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
338 dev->data->rx_queues[idx] = rxq;
339 /* Enable associated flows. */
340 ret = mlx4_flow_sync(priv, &error);
343 ERROR("cannot re-attach flow rules to queue %u"
344 " (code %d, \"%s\"), flow error type %d, cause %p, message: %s",
345 idx, -ret, strerror(-ret), error.type, error.cause,
346 error.message ? error.message : "(unspecified)");
348 dev->data->rx_queues[idx] = NULL;
350 mlx4_rx_queue_release(rxq);
352 assert(rte_errno > 0);
357 * DPDK callback to release a Rx queue.
360 * Generic Rx queue pointer.
363 mlx4_rx_queue_release(void *dpdk_rxq)
365 struct rxq *rxq = (struct rxq *)dpdk_rxq;
372 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
373 if (priv->dev->data->rx_queues[i] == rxq) {
374 DEBUG("%p: removing Rx queue %p from list",
375 (void *)priv->dev, (void *)rxq);
376 priv->dev->data->rx_queues[i] = NULL;
379 mlx4_flow_sync(priv, NULL);
380 mlx4_rxq_free_elts(rxq);
382 claim_zero(ibv_destroy_qp(rxq->qp));
384 claim_zero(ibv_destroy_cq(rxq->cq));
386 claim_zero(ibv_destroy_comp_channel(rxq->channel));
388 claim_zero(ibv_dereg_mr(rxq->mr));