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>
57 #include <rte_malloc.h>
59 #include <rte_mempool.h>
62 #include "mlx4_flow.h"
63 #include "mlx4_rxtx.h"
64 #include "mlx4_utils.h"
67 * Allocate Rx queue elements.
70 * Pointer to Rx queue structure.
72 * Number of elements to allocate.
75 * 0 on success, negative errno value otherwise and rte_errno is set.
78 mlx4_rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
81 struct rxq_elt (*elts)[elts_n] =
82 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
87 ERROR("%p: can't allocate packets array", (void *)rxq);
90 /* For each WR (packet). */
91 for (i = 0; (i != elts_n); ++i) {
92 struct rxq_elt *elt = &(*elts)[i];
93 struct ibv_recv_wr *wr = &elt->wr;
94 struct ibv_sge *sge = &(*elts)[i].sge;
95 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
99 ERROR("%p: empty mbuf pool", (void *)rxq);
103 wr->next = &(*elts)[(i + 1)].wr;
106 /* Headroom is reserved by rte_pktmbuf_alloc(). */
107 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
108 /* Buffer is supposed to be empty. */
109 assert(rte_pktmbuf_data_len(buf) == 0);
110 assert(rte_pktmbuf_pkt_len(buf) == 0);
111 /* sge->addr must be able to store a pointer. */
112 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
113 /* SGE keeps its headroom. */
114 sge->addr = (uintptr_t)
115 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
116 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
117 sge->lkey = rxq->mr->lkey;
118 /* Redundant check for tailroom. */
119 assert(sge->length == rte_pktmbuf_tailroom(buf));
121 /* The last WR pointer must be NULL. */
122 (*elts)[(i - 1)].wr.next = NULL;
123 DEBUG("%p: allocated and configured %u single-segment WRs",
124 (void *)rxq, elts_n);
125 rxq->elts_n = elts_n;
131 for (i = 0; (i != RTE_DIM(*elts)); ++i)
132 rte_pktmbuf_free_seg((*elts)[i].buf);
135 DEBUG("%p: failed, freed everything", (void *)rxq);
136 assert(rte_errno > 0);
141 * Free Rx queue elements.
144 * Pointer to Rx queue structure.
147 mlx4_rxq_free_elts(struct rxq *rxq)
150 unsigned int elts_n = rxq->elts_n;
151 struct rxq_elt (*elts)[elts_n] = rxq->elts;
153 DEBUG("%p: freeing WRs", (void *)rxq);
158 for (i = 0; (i != RTE_DIM(*elts)); ++i)
159 rte_pktmbuf_free_seg((*elts)[i].buf);
164 * Clean up a Rx queue.
166 * Destroy objects, free allocated memory and reset the structure for reuse.
169 * Pointer to Rx queue structure.
172 mlx4_rxq_cleanup(struct rxq *rxq)
174 DEBUG("cleaning up %p", (void *)rxq);
175 mlx4_rxq_free_elts(rxq);
177 claim_zero(ibv_destroy_qp(rxq->qp));
179 claim_zero(ibv_destroy_cq(rxq->cq));
180 if (rxq->channel != NULL)
181 claim_zero(ibv_destroy_comp_channel(rxq->channel));
183 claim_zero(ibv_dereg_mr(rxq->mr));
184 memset(rxq, 0, sizeof(*rxq));
188 * Configure a Rx queue.
191 * Pointer to Ethernet device structure.
193 * Pointer to Rx queue structure.
195 * Number of descriptors to configure in queue.
197 * NUMA socket on which memory must be allocated.
199 * Thresholds parameters.
201 * Memory pool for buffer allocations.
204 * 0 on success, negative errno value otherwise and rte_errno is set.
207 mlx4_rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
208 unsigned int socket, const struct rte_eth_rxconf *conf,
209 struct rte_mempool *mp)
211 struct priv *priv = dev->data->dev_private;
217 struct ibv_qp_attr mod;
218 struct ibv_qp_init_attr qp_init;
219 struct ibv_recv_wr *bad_wr;
223 (void)conf; /* Thresholds configuration (ignored). */
224 mb_len = rte_pktmbuf_data_room_size(mp);
227 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
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 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
249 if (tmpl.mr == NULL) {
251 ERROR("%p: MR creation failure: %s",
252 (void *)dev, strerror(rte_errno));
255 if (dev->data->dev_conf.intr_conf.rxq) {
256 tmpl.channel = ibv_create_comp_channel(priv->ctx);
257 if (tmpl.channel == NULL) {
259 ERROR("%p: Rx interrupt completion channel creation"
261 (void *)dev, strerror(rte_errno));
264 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
265 ERROR("%p: unable to make Rx interrupt completion"
266 " channel non-blocking: %s",
267 (void *)dev, strerror(rte_errno));
271 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
272 if (tmpl.cq == NULL) {
274 ERROR("%p: CQ creation failure: %s",
275 (void *)dev, strerror(rte_errno));
278 DEBUG("priv->device_attr.max_qp_wr is %d",
279 priv->device_attr.max_qp_wr);
280 DEBUG("priv->device_attr.max_sge is %d",
281 priv->device_attr.max_sge);
282 qp_init = (struct ibv_qp_init_attr){
283 /* CQ to be associated with the send queue. */
285 /* CQ to be associated with the receive queue. */
288 /* Max number of outstanding WRs. */
289 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
290 priv->device_attr.max_qp_wr :
292 /* Max number of scatter/gather elements in a WR. */
295 .qp_type = IBV_QPT_RAW_PACKET,
297 tmpl.qp = ibv_create_qp(priv->pd, &qp_init);
298 if (tmpl.qp == NULL) {
299 rte_errno = errno ? errno : EINVAL;
300 ERROR("%p: QP creation failure: %s",
301 (void *)dev, strerror(rte_errno));
304 mod = (struct ibv_qp_attr){
305 /* Move the QP to this state. */
306 .qp_state = IBV_QPS_INIT,
307 /* Primary port number. */
308 .port_num = priv->port
310 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
313 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
314 (void *)dev, strerror(rte_errno));
317 ret = mlx4_rxq_alloc_elts(&tmpl, desc);
319 ERROR("%p: RXQ allocation failed: %s",
320 (void *)dev, strerror(rte_errno));
323 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
326 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
329 strerror(rte_errno));
332 mod = (struct ibv_qp_attr){
333 .qp_state = IBV_QPS_RTR
335 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
338 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
339 (void *)dev, strerror(rte_errno));
343 tmpl.port_id = dev->data->port_id;
344 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
345 /* Clean up rxq in case we're reinitializing it. */
346 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
347 mlx4_rxq_cleanup(rxq);
349 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
353 mlx4_rxq_cleanup(&tmpl);
355 assert(rte_errno > 0);
360 * DPDK callback to configure a Rx queue.
363 * Pointer to Ethernet device structure.
367 * Number of descriptors to configure in queue.
369 * NUMA socket on which memory must be allocated.
371 * Thresholds parameters.
373 * Memory pool for buffer allocations.
376 * 0 on success, negative errno value otherwise and rte_errno is set.
379 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
380 unsigned int socket, const struct rte_eth_rxconf *conf,
381 struct rte_mempool *mp)
383 struct priv *priv = dev->data->dev_private;
384 struct rxq *rxq = dev->data->rx_queues[idx];
387 DEBUG("%p: configuring queue %u for %u descriptors",
388 (void *)dev, idx, desc);
389 if (idx >= dev->data->nb_rx_queues) {
390 rte_errno = EOVERFLOW;
391 ERROR("%p: queue index out of range (%u >= %u)",
392 (void *)dev, idx, dev->data->nb_rx_queues);
396 DEBUG("%p: reusing already allocated queue index %u (%p)",
397 (void *)dev, idx, (void *)rxq);
402 dev->data->rx_queues[idx] = NULL;
403 /* Disable associated flows. */
404 mlx4_flow_sync(priv);
405 mlx4_rxq_cleanup(rxq);
407 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
410 ERROR("%p: unable to allocate queue index %u",
415 ret = mlx4_rxq_setup(dev, rxq, desc, socket, conf, mp);
419 rxq->stats.idx = idx;
420 DEBUG("%p: adding Rx queue %p to list",
421 (void *)dev, (void *)rxq);
422 dev->data->rx_queues[idx] = rxq;
423 /* Re-enable associated flows. */
424 ret = mlx4_flow_sync(priv);
426 dev->data->rx_queues[idx] = NULL;
427 mlx4_rxq_cleanup(rxq);
431 /* Update receive callback. */
432 dev->rx_pkt_burst = mlx4_rx_burst;
438 * DPDK callback to release a Rx queue.
441 * Generic Rx queue pointer.
444 mlx4_rx_queue_release(void *dpdk_rxq)
446 struct rxq *rxq = (struct rxq *)dpdk_rxq;
453 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
454 if (priv->dev->data->rx_queues[i] == rxq) {
455 DEBUG("%p: removing Rx queue %p from list",
456 (void *)priv->dev, (void *)rxq);
457 priv->dev->data->rx_queues[i] = NULL;
460 mlx4_flow_sync(priv);
461 mlx4_rxq_cleanup(rxq);