net/mlx4: allocate queues and mbuf rings together
[dpdk.git] / drivers / net / mlx4 / mlx4_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /**
35  * @file
36  * Rx queues configuration for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <string.h>
44
45 /* Verbs headers do not support -pedantic. */
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic ignored "-Wpedantic"
48 #endif
49 #include <infiniband/verbs.h>
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic error "-Wpedantic"
52 #endif
53
54 #include <rte_common.h>
55 #include <rte_errno.h>
56 #include <rte_ethdev.h>
57 #include <rte_flow.h>
58 #include <rte_malloc.h>
59 #include <rte_mbuf.h>
60 #include <rte_mempool.h>
61
62 #include "mlx4.h"
63 #include "mlx4_flow.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
66
67 /**
68  * Allocate Rx queue elements.
69  *
70  * @param rxq
71  *   Pointer to Rx queue structure.
72  *
73  * @return
74  *   0 on success, negative errno value otherwise and rte_errno is set.
75  */
76 static int
77 mlx4_rxq_alloc_elts(struct rxq *rxq)
78 {
79         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
80         unsigned int i;
81
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);
88
89                 if (buf == NULL) {
90                         while (i--) {
91                                 rte_pktmbuf_free_seg((*elts)[i].buf);
92                                 (*elts)[i].buf = NULL;
93                         }
94                         rte_errno = ENOMEM;
95                         return -rte_errno;
96                 }
97                 elt->buf = buf;
98                 wr->next = &(*elts)[(i + 1)].wr;
99                 wr->sg_list = sge;
100                 wr->num_sge = 1;
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));
115         }
116         /* The last WR pointer must be NULL. */
117         (*elts)[(i - 1)].wr.next = NULL;
118         return 0;
119 }
120
121 /**
122  * Free Rx queue elements.
123  *
124  * @param rxq
125  *   Pointer to Rx queue structure.
126  */
127 static void
128 mlx4_rxq_free_elts(struct rxq *rxq)
129 {
130         unsigned int i;
131         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
132
133         DEBUG("%p: freeing WRs", (void *)rxq);
134         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
135                 if (!(*elts)[i].buf)
136                         continue;
137                 rte_pktmbuf_free_seg((*elts)[i].buf);
138                 (*elts)[i].buf = NULL;
139         }
140 }
141
142 /**
143  * DPDK callback to configure a Rx queue.
144  *
145  * @param dev
146  *   Pointer to Ethernet device structure.
147  * @param idx
148  *   Rx queue index.
149  * @param desc
150  *   Number of descriptors to configure in queue.
151  * @param socket
152  *   NUMA socket on which memory must be allocated.
153  * @param[in] conf
154  *   Thresholds parameters.
155  * @param mp
156  *   Memory pool for buffer allocations.
157  *
158  * @return
159  *   0 on success, negative errno value otherwise and rte_errno is set.
160  */
161 int
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)
165 {
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;
170         struct rxq *rxq;
171         struct mlx4_malloc_vec vec[] = {
172                 {
173                         .align = RTE_CACHE_LINE_SIZE,
174                         .size = sizeof(*rxq),
175                         .addr = (void **)&rxq,
176                 },
177                 {
178                         .align = RTE_CACHE_LINE_SIZE,
179                         .size = sizeof(*elts),
180                         .addr = (void **)&elts,
181                 },
182         };
183         int ret;
184
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);
192                 return -rte_errno;
193         }
194         rxq = dev->data->rx_queues[idx];
195         if (rxq) {
196                 rte_errno = EEXIST;
197                 ERROR("%p: Rx queue %u already configured, release it first",
198                       (void *)dev, idx);
199                 return -rte_errno;
200         }
201         if (!desc) {
202                 rte_errno = EINVAL;
203                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
204                 return -rte_errno;
205         }
206         /* Allocate and initialize Rx queue. */
207         mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
208         if (!rxq) {
209                 ERROR("%p: unable to allocate queue index %u",
210                       (void *)dev, idx);
211                 return -rte_errno;
212         }
213         *rxq = (struct rxq){
214                 .priv = priv,
215                 .mp = mp,
216                 .port_id = dev->data->port_id,
217                 .elts_n = desc,
218                 .elts_head = 0,
219                 .elts = elts,
220                 .stats.idx = idx,
221                 .socket = socket,
222         };
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)) {
227                 ;
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",
231                      (void *)dev);
232         } else {
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",
236                      (void *)dev,
237                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
238                      mb_len - RTE_PKTMBUF_HEADROOM);
239         }
240         /* Use the entire Rx mempool as the memory region. */
241         rxq->mr = mlx4_mp2mr(priv->pd, mp);
242         if (!rxq->mr) {
243                 rte_errno = EINVAL;
244                 ERROR("%p: MR creation failure: %s",
245                       (void *)dev, strerror(rte_errno));
246                 goto error;
247         }
248         if (dev->data->dev_conf.intr_conf.rxq) {
249                 rxq->channel = ibv_create_comp_channel(priv->ctx);
250                 if (rxq->channel == NULL) {
251                         rte_errno = ENOMEM;
252                         ERROR("%p: Rx interrupt completion channel creation"
253                               " failure: %s",
254                               (void *)dev, strerror(rte_errno));
255                         goto error;
256                 }
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));
261                         goto error;
262                 }
263         }
264         rxq->cq = ibv_create_cq(priv->ctx, desc, NULL, rxq->channel, 0);
265         if (!rxq->cq) {
266                 rte_errno = ENOMEM;
267                 ERROR("%p: CQ creation failure: %s",
268                       (void *)dev, strerror(rte_errno));
269                 goto error;
270         }
271         rxq->qp = ibv_create_qp
272                 (priv->pd,
273                  &(struct ibv_qp_init_attr){
274                         .send_cq = rxq->cq,
275                         .recv_cq = rxq->cq,
276                         .cap = {
277                                 .max_recv_wr =
278                                         RTE_MIN(priv->device_attr.max_qp_wr,
279                                                 desc),
280                                 .max_recv_sge = 1,
281                         },
282                         .qp_type = IBV_QPT_RAW_PACKET,
283                  });
284         if (!rxq->qp) {
285                 rte_errno = errno ? errno : EINVAL;
286                 ERROR("%p: QP creation failure: %s",
287                       (void *)dev, strerror(rte_errno));
288                 goto error;
289         }
290         ret = ibv_modify_qp
291                 (rxq->qp,
292                  &(struct ibv_qp_attr){
293                         .qp_state = IBV_QPS_INIT,
294                         .port_num = priv->port,
295                  },
296                  IBV_QP_STATE | IBV_QP_PORT);
297         if (ret) {
298                 rte_errno = ret;
299                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
300                       (void *)dev, strerror(rte_errno));
301                 goto error;
302         }
303         ret = mlx4_rxq_alloc_elts(rxq);
304         if (ret) {
305                 ERROR("%p: RXQ allocation failed: %s",
306                       (void *)dev, strerror(rte_errno));
307                 goto error;
308         }
309         ret = ibv_post_recv(rxq->qp, &(*rxq->elts)[0].wr,
310                             &(struct ibv_recv_wr *){ NULL });
311         if (ret) {
312                 rte_errno = ret;
313                 ERROR("%p: ibv_post_recv() failed: %s",
314                       (void *)dev,
315                       strerror(rte_errno));
316                 goto error;
317         }
318         ret = ibv_modify_qp
319                 (rxq->qp,
320                  &(struct ibv_qp_attr){
321                         .qp_state = IBV_QPS_RTR,
322                  },
323                  IBV_QP_STATE);
324         if (ret) {
325                 rte_errno = ret;
326                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
327                       (void *)dev, strerror(rte_errno));
328                 goto error;
329         }
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);
334         if (!ret)
335                 return 0;
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)");
340 error:
341         dev->data->rx_queues[idx] = NULL;
342         ret = rte_errno;
343         mlx4_rx_queue_release(rxq);
344         rte_errno = ret;
345         assert(rte_errno > 0);
346         return -rte_errno;
347 }
348
349 /**
350  * DPDK callback to release a Rx queue.
351  *
352  * @param dpdk_rxq
353  *   Generic Rx queue pointer.
354  */
355 void
356 mlx4_rx_queue_release(void *dpdk_rxq)
357 {
358         struct rxq *rxq = (struct rxq *)dpdk_rxq;
359         struct priv *priv;
360         unsigned int i;
361
362         if (rxq == NULL)
363                 return;
364         priv = rxq->priv;
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;
370                         break;
371                 }
372         mlx4_flow_sync(priv, NULL);
373         mlx4_rxq_free_elts(rxq);
374         if (rxq->qp)
375                 claim_zero(ibv_destroy_qp(rxq->qp));
376         if (rxq->cq)
377                 claim_zero(ibv_destroy_cq(rxq->cq));
378         if (rxq->channel)
379                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
380         if (rxq->mr)
381                 claim_zero(ibv_dereg_mr(rxq->mr));
382         rte_free(rxq);
383 }