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.
74 * 0 on success, negative errno value otherwise and rte_errno is set.
77 mlx4_rxq_alloc_elts(struct rxq *rxq)
79 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
82 /* For each WR (packet). */
83 for (i = 0; i != RTE_DIM(*elts); ++i) {
84 struct rxq_elt *elt = &(*elts)[i];
85 struct ibv_recv_wr *wr = &elt->wr;
86 struct ibv_sge *sge = &(*elts)[i].sge;
87 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
91 rte_pktmbuf_free_seg((*elts)[i].buf);
92 (*elts)[i].buf = NULL;
98 wr->next = &(*elts)[(i + 1)].wr;
101 /* Headroom is reserved by rte_pktmbuf_alloc(). */
102 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
103 /* Buffer is supposed to be empty. */
104 assert(rte_pktmbuf_data_len(buf) == 0);
105 assert(rte_pktmbuf_pkt_len(buf) == 0);
106 /* sge->addr must be able to store a pointer. */
107 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
108 /* SGE keeps its headroom. */
109 sge->addr = (uintptr_t)
110 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
111 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
112 sge->lkey = rxq->mr->lkey;
113 /* Redundant check for tailroom. */
114 assert(sge->length == rte_pktmbuf_tailroom(buf));
116 /* The last WR pointer must be NULL. */
117 (*elts)[(i - 1)].wr.next = NULL;
122 * Free Rx queue elements.
125 * Pointer to Rx queue structure.
128 mlx4_rxq_free_elts(struct rxq *rxq)
131 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
133 DEBUG("%p: freeing WRs", (void *)rxq);
134 for (i = 0; (i != RTE_DIM(*elts)); ++i) {
137 rte_pktmbuf_free_seg((*elts)[i].buf);
138 (*elts)[i].buf = NULL;
143 * DPDK callback to configure a Rx queue.
146 * Pointer to Ethernet device structure.
150 * Number of descriptors to configure in queue.
152 * NUMA socket on which memory must be allocated.
154 * Thresholds parameters.
156 * Memory pool for buffer allocations.
159 * 0 on success, negative errno value otherwise and rte_errno is set.
162 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
163 unsigned int socket, const struct rte_eth_rxconf *conf,
164 struct rte_mempool *mp)
166 struct priv *priv = dev->data->dev_private;
167 uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
168 struct rxq_elt (*elts)[desc];
169 struct rte_flow_error error;
171 struct mlx4_malloc_vec vec[] = {
173 .align = RTE_CACHE_LINE_SIZE,
174 .size = sizeof(*rxq),
175 .addr = (void **)&rxq,
178 .align = RTE_CACHE_LINE_SIZE,
179 .size = sizeof(*elts),
180 .addr = (void **)&elts,
185 (void)conf; /* Thresholds configuration (ignored). */
186 DEBUG("%p: configuring queue %u for %u descriptors",
187 (void *)dev, idx, desc);
188 if (idx >= dev->data->nb_rx_queues) {
189 rte_errno = EOVERFLOW;
190 ERROR("%p: queue index out of range (%u >= %u)",
191 (void *)dev, idx, dev->data->nb_rx_queues);
194 rxq = dev->data->rx_queues[idx];
197 ERROR("%p: Rx queue %u already configured, release it first",
203 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
206 /* Allocate and initialize Rx queue. */
207 mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
209 ERROR("%p: unable to allocate queue index %u",
216 .port_id = dev->data->port_id,
223 /* Enable scattered packets support for this queue if necessary. */
224 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
225 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
226 (mb_len - RTE_PKTMBUF_HEADROOM)) {
228 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
229 WARN("%p: scattered mode has been requested but is"
230 " not supported, this may lead to packet loss",
233 WARN("%p: the requested maximum Rx packet size (%u) is"
234 " larger than a single mbuf (%u) and scattered"
235 " mode has not been requested",
237 dev->data->dev_conf.rxmode.max_rx_pkt_len,
238 mb_len - RTE_PKTMBUF_HEADROOM);
240 /* Use the entire Rx mempool as the memory region. */
241 rxq->mr = mlx4_mp2mr(priv->pd, mp);
244 ERROR("%p: MR creation failure: %s",
245 (void *)dev, strerror(rte_errno));
248 if (dev->data->dev_conf.intr_conf.rxq) {
249 rxq->channel = ibv_create_comp_channel(priv->ctx);
250 if (rxq->channel == NULL) {
252 ERROR("%p: Rx interrupt completion channel creation"
254 (void *)dev, strerror(rte_errno));
257 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
258 ERROR("%p: unable to make Rx interrupt completion"
259 " channel non-blocking: %s",
260 (void *)dev, strerror(rte_errno));
264 rxq->cq = ibv_create_cq(priv->ctx, desc, NULL, rxq->channel, 0);
267 ERROR("%p: CQ creation failure: %s",
268 (void *)dev, strerror(rte_errno));
271 rxq->qp = ibv_create_qp
273 &(struct ibv_qp_init_attr){
278 RTE_MIN(priv->device_attr.max_qp_wr,
282 .qp_type = IBV_QPT_RAW_PACKET,
285 rte_errno = errno ? errno : EINVAL;
286 ERROR("%p: QP creation failure: %s",
287 (void *)dev, strerror(rte_errno));
292 &(struct ibv_qp_attr){
293 .qp_state = IBV_QPS_INIT,
294 .port_num = priv->port,
296 IBV_QP_STATE | IBV_QP_PORT);
299 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
300 (void *)dev, strerror(rte_errno));
303 ret = mlx4_rxq_alloc_elts(rxq);
305 ERROR("%p: RXQ allocation failed: %s",
306 (void *)dev, strerror(rte_errno));
309 ret = ibv_post_recv(rxq->qp, &(*rxq->elts)[0].wr,
310 &(struct ibv_recv_wr *){ NULL });
313 ERROR("%p: ibv_post_recv() failed: %s",
315 strerror(rte_errno));
320 &(struct ibv_qp_attr){
321 .qp_state = IBV_QPS_RTR,
326 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
327 (void *)dev, strerror(rte_errno));
330 DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
331 dev->data->rx_queues[idx] = rxq;
332 /* Enable associated flows. */
333 ret = mlx4_flow_sync(priv, &error);
336 ERROR("cannot re-attach flow rules to queue %u"
337 " (code %d, \"%s\"), flow error type %d, cause %p, message: %s",
338 idx, -ret, strerror(-ret), error.type, error.cause,
339 error.message ? error.message : "(unspecified)");
341 dev->data->rx_queues[idx] = NULL;
343 mlx4_rx_queue_release(rxq);
345 assert(rte_errno > 0);
350 * DPDK callback to release a Rx queue.
353 * Generic Rx queue pointer.
356 mlx4_rx_queue_release(void *dpdk_rxq)
358 struct rxq *rxq = (struct rxq *)dpdk_rxq;
365 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
366 if (priv->dev->data->rx_queues[i] == rxq) {
367 DEBUG("%p: removing Rx queue %p from list",
368 (void *)priv->dev, (void *)rxq);
369 priv->dev->data->rx_queues[i] = NULL;
372 mlx4_flow_sync(priv, NULL);
373 mlx4_rxq_free_elts(rxq);
375 claim_zero(ibv_destroy_qp(rxq->qp));
377 claim_zero(ibv_destroy_cq(rxq->cq));
379 claim_zero(ibv_destroy_comp_channel(rxq->channel));
381 claim_zero(ibv_dereg_mr(rxq->mr));