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 * Clean up a Rx queue.
167 * Destroy objects, free allocated memory and reset the structure for reuse.
170 * Pointer to Rx queue structure.
173 mlx4_rxq_cleanup(struct rxq *rxq)
175 DEBUG("cleaning up %p", (void *)rxq);
176 mlx4_rxq_free_elts(rxq);
178 claim_zero(ibv_destroy_qp(rxq->qp));
180 claim_zero(ibv_destroy_cq(rxq->cq));
181 if (rxq->channel != NULL)
182 claim_zero(ibv_destroy_comp_channel(rxq->channel));
184 claim_zero(ibv_dereg_mr(rxq->mr));
185 memset(rxq, 0, sizeof(*rxq));
189 * Configure a Rx queue.
192 * Pointer to Ethernet device structure.
194 * Pointer to Rx queue structure.
196 * Number of descriptors to configure in queue.
198 * NUMA socket on which memory must be allocated.
200 * Thresholds parameters.
202 * Memory pool for buffer allocations.
205 * 0 on success, negative errno value otherwise and rte_errno is set.
208 mlx4_rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
209 unsigned int socket, const struct rte_eth_rxconf *conf,
210 struct rte_mempool *mp)
212 struct priv *priv = dev->data->dev_private;
218 struct ibv_qp_attr mod;
219 struct ibv_qp_init_attr qp_init;
220 struct ibv_recv_wr *bad_wr;
224 (void)conf; /* Thresholds configuration (ignored). */
225 mb_len = rte_pktmbuf_data_room_size(mp);
228 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
231 /* Enable scattered packets support for this queue if necessary. */
232 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
233 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
234 (mb_len - RTE_PKTMBUF_HEADROOM)) {
236 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
237 WARN("%p: scattered mode has been requested but is"
238 " not supported, this may lead to packet loss",
241 WARN("%p: the requested maximum Rx packet size (%u) is"
242 " larger than a single mbuf (%u) and scattered"
243 " mode has not been requested",
245 dev->data->dev_conf.rxmode.max_rx_pkt_len,
246 mb_len - RTE_PKTMBUF_HEADROOM);
248 /* Use the entire Rx mempool as the memory region. */
249 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
250 if (tmpl.mr == NULL) {
252 ERROR("%p: MR creation failure: %s",
253 (void *)dev, strerror(rte_errno));
256 if (dev->data->dev_conf.intr_conf.rxq) {
257 tmpl.channel = ibv_create_comp_channel(priv->ctx);
258 if (tmpl.channel == NULL) {
260 ERROR("%p: Rx interrupt completion channel creation"
262 (void *)dev, strerror(rte_errno));
265 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
266 ERROR("%p: unable to make Rx interrupt completion"
267 " channel non-blocking: %s",
268 (void *)dev, strerror(rte_errno));
272 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
273 if (tmpl.cq == NULL) {
275 ERROR("%p: CQ creation failure: %s",
276 (void *)dev, strerror(rte_errno));
279 DEBUG("priv->device_attr.max_qp_wr is %d",
280 priv->device_attr.max_qp_wr);
281 DEBUG("priv->device_attr.max_sge is %d",
282 priv->device_attr.max_sge);
283 qp_init = (struct ibv_qp_init_attr){
284 /* CQ to be associated with the send queue. */
286 /* CQ to be associated with the receive queue. */
289 /* Max number of outstanding WRs. */
290 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
291 priv->device_attr.max_qp_wr :
293 /* Max number of scatter/gather elements in a WR. */
296 .qp_type = IBV_QPT_RAW_PACKET,
298 tmpl.qp = ibv_create_qp(priv->pd, &qp_init);
299 if (tmpl.qp == NULL) {
300 rte_errno = errno ? errno : EINVAL;
301 ERROR("%p: QP creation failure: %s",
302 (void *)dev, strerror(rte_errno));
305 mod = (struct ibv_qp_attr){
306 /* Move the QP to this state. */
307 .qp_state = IBV_QPS_INIT,
308 /* Primary port number. */
309 .port_num = priv->port
311 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
314 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
315 (void *)dev, strerror(rte_errno));
318 ret = mlx4_rxq_alloc_elts(&tmpl, desc);
320 ERROR("%p: RXQ allocation failed: %s",
321 (void *)dev, strerror(rte_errno));
324 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
327 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
330 strerror(rte_errno));
333 mod = (struct ibv_qp_attr){
334 .qp_state = IBV_QPS_RTR
336 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
339 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
340 (void *)dev, strerror(rte_errno));
344 tmpl.port_id = dev->data->port_id;
345 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
346 /* Clean up rxq in case we're reinitializing it. */
347 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
348 mlx4_rxq_cleanup(rxq);
350 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
354 mlx4_rxq_cleanup(&tmpl);
356 assert(rte_errno > 0);
361 * DPDK callback to configure a Rx queue.
364 * Pointer to Ethernet device structure.
368 * Number of descriptors to configure in queue.
370 * NUMA socket on which memory must be allocated.
372 * Thresholds parameters.
374 * Memory pool for buffer allocations.
377 * 0 on success, negative errno value otherwise and rte_errno is set.
380 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
381 unsigned int socket, const struct rte_eth_rxconf *conf,
382 struct rte_mempool *mp)
384 struct priv *priv = dev->data->dev_private;
385 struct rxq *rxq = dev->data->rx_queues[idx];
388 DEBUG("%p: configuring queue %u for %u descriptors",
389 (void *)dev, idx, desc);
390 if (idx >= dev->data->nb_rx_queues) {
391 rte_errno = EOVERFLOW;
392 ERROR("%p: queue index out of range (%u >= %u)",
393 (void *)dev, idx, dev->data->nb_rx_queues);
397 DEBUG("%p: reusing already allocated queue index %u (%p)",
398 (void *)dev, idx, (void *)rxq);
403 dev->data->rx_queues[idx] = NULL;
404 /* Disable associated flows. */
405 mlx4_flow_sync(priv, NULL);
406 mlx4_rxq_cleanup(rxq);
408 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
411 ERROR("%p: unable to allocate queue index %u",
416 ret = mlx4_rxq_setup(dev, rxq, desc, socket, conf, mp);
420 struct rte_flow_error error;
422 rxq->stats.idx = idx;
423 DEBUG("%p: adding Rx queue %p to list",
424 (void *)dev, (void *)rxq);
425 dev->data->rx_queues[idx] = rxq;
426 /* Re-enable associated flows. */
427 ret = mlx4_flow_sync(priv, &error);
429 ERROR("cannot re-attach flow rules to queue %u"
430 " (code %d, \"%s\"), flow error type %d,"
431 " cause %p, message: %s", idx,
432 -ret, strerror(-ret), error.type, error.cause,
433 error.message ? error.message : "(unspecified)");
434 dev->data->rx_queues[idx] = NULL;
435 mlx4_rxq_cleanup(rxq);
439 /* Update receive callback. */
440 dev->rx_pkt_burst = mlx4_rx_burst;
446 * DPDK callback to release a Rx queue.
449 * Generic Rx queue pointer.
452 mlx4_rx_queue_release(void *dpdk_rxq)
454 struct rxq *rxq = (struct rxq *)dpdk_rxq;
461 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
462 if (priv->dev->data->rx_queues[i] == rxq) {
463 DEBUG("%p: removing Rx queue %p from list",
464 (void *)priv->dev, (void *)rxq);
465 priv->dev->data->rx_queues[i] = NULL;
468 mlx4_flow_sync(priv, NULL);
469 mlx4_rxq_cleanup(rxq);