7bb2f9e2f20d5502ea726d8c03ec8a95286ed365
[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_malloc.h>
58 #include <rte_mbuf.h>
59 #include <rte_mempool.h>
60
61 #include "mlx4.h"
62 #include "mlx4_flow.h"
63 #include "mlx4_rxtx.h"
64 #include "mlx4_utils.h"
65
66 /**
67  * Allocate Rx queue elements.
68  *
69  * @param rxq
70  *   Pointer to Rx queue structure.
71  * @param elts_n
72  *   Number of elements to allocate.
73  *
74  * @return
75  *   0 on success, negative errno value otherwise and rte_errno is set.
76  */
77 static int
78 mlx4_rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
79 {
80         unsigned int i;
81         struct rxq_elt (*elts)[elts_n] =
82                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
83                                   rxq->socket);
84
85         if (elts == NULL) {
86                 rte_errno = ENOMEM;
87                 ERROR("%p: can't allocate packets array", (void *)rxq);
88                 goto error;
89         }
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);
96
97                 if (buf == NULL) {
98                         rte_errno = ENOMEM;
99                         ERROR("%p: empty mbuf pool", (void *)rxq);
100                         goto error;
101                 }
102                 elt->buf = buf;
103                 wr->next = &(*elts)[(i + 1)].wr;
104                 wr->sg_list = sge;
105                 wr->num_sge = 1;
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));
120         }
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;
126         rxq->elts_head = 0;
127         rxq->elts = elts;
128         return 0;
129 error:
130         if (elts != NULL) {
131                 for (i = 0; (i != RTE_DIM(*elts)); ++i)
132                         rte_pktmbuf_free_seg((*elts)[i].buf);
133                 rte_free(elts);
134         }
135         DEBUG("%p: failed, freed everything", (void *)rxq);
136         assert(rte_errno > 0);
137         return -rte_errno;
138 }
139
140 /**
141  * Free Rx queue elements.
142  *
143  * @param rxq
144  *   Pointer to Rx queue structure.
145  */
146 static void
147 mlx4_rxq_free_elts(struct rxq *rxq)
148 {
149         unsigned int i;
150         unsigned int elts_n = rxq->elts_n;
151         struct rxq_elt (*elts)[elts_n] = rxq->elts;
152
153         DEBUG("%p: freeing WRs", (void *)rxq);
154         rxq->elts_n = 0;
155         rxq->elts = NULL;
156         if (elts == NULL)
157                 return;
158         for (i = 0; (i != RTE_DIM(*elts)); ++i)
159                 rte_pktmbuf_free_seg((*elts)[i].buf);
160         rte_free(elts);
161 }
162
163 /**
164  * Clean up a Rx queue.
165  *
166  * Destroy objects, free allocated memory and reset the structure for reuse.
167  *
168  * @param rxq
169  *   Pointer to Rx queue structure.
170  */
171 void
172 mlx4_rxq_cleanup(struct rxq *rxq)
173 {
174         DEBUG("cleaning up %p", (void *)rxq);
175         mlx4_rxq_free_elts(rxq);
176         if (rxq->qp != NULL)
177                 claim_zero(ibv_destroy_qp(rxq->qp));
178         if (rxq->cq != NULL)
179                 claim_zero(ibv_destroy_cq(rxq->cq));
180         if (rxq->channel != NULL)
181                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
182         if (rxq->mr != NULL)
183                 claim_zero(ibv_dereg_mr(rxq->mr));
184         memset(rxq, 0, sizeof(*rxq));
185 }
186
187 /**
188  * Configure a Rx queue.
189  *
190  * @param dev
191  *   Pointer to Ethernet device structure.
192  * @param rxq
193  *   Pointer to Rx queue structure.
194  * @param desc
195  *   Number of descriptors to configure in queue.
196  * @param socket
197  *   NUMA socket on which memory must be allocated.
198  * @param[in] conf
199  *   Thresholds parameters.
200  * @param mp
201  *   Memory pool for buffer allocations.
202  *
203  * @return
204  *   0 on success, negative errno value otherwise and rte_errno is set.
205  */
206 static int
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)
210 {
211         struct priv *priv = dev->data->dev_private;
212         struct rxq tmpl = {
213                 .priv = priv,
214                 .mp = mp,
215                 .socket = socket
216         };
217         struct ibv_qp_attr mod;
218         struct ibv_qp_init_attr qp_init;
219         struct ibv_recv_wr *bad_wr;
220         unsigned int mb_len;
221         int ret;
222
223         (void)conf; /* Thresholds configuration (ignored). */
224         mb_len = rte_pktmbuf_data_room_size(mp);
225         if (desc == 0) {
226                 rte_errno = EINVAL;
227                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
228                 goto error;
229         }
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)) {
234                 ;
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",
238                      (void *)dev);
239         } else {
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",
243                      (void *)dev,
244                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
245                      mb_len - RTE_PKTMBUF_HEADROOM);
246         }
247         /* Use the entire Rx mempool as the memory region. */
248         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
249         if (tmpl.mr == NULL) {
250                 rte_errno = EINVAL;
251                 ERROR("%p: MR creation failure: %s",
252                       (void *)dev, strerror(rte_errno));
253                 goto error;
254         }
255         if (dev->data->dev_conf.intr_conf.rxq) {
256                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
257                 if (tmpl.channel == NULL) {
258                         rte_errno = ENOMEM;
259                         ERROR("%p: Rx interrupt completion channel creation"
260                               " failure: %s",
261                               (void *)dev, strerror(rte_errno));
262                         goto error;
263                 }
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));
268                         goto error;
269                 }
270         }
271         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
272         if (tmpl.cq == NULL) {
273                 rte_errno = ENOMEM;
274                 ERROR("%p: CQ creation failure: %s",
275                       (void *)dev, strerror(rte_errno));
276                 goto error;
277         }
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. */
284                 .send_cq = tmpl.cq,
285                 /* CQ to be associated with the receive queue. */
286                 .recv_cq = tmpl.cq,
287                 .cap = {
288                         /* Max number of outstanding WRs. */
289                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
290                                         priv->device_attr.max_qp_wr :
291                                         desc),
292                         /* Max number of scatter/gather elements in a WR. */
293                         .max_recv_sge = 1,
294                 },
295                 .qp_type = IBV_QPT_RAW_PACKET,
296         };
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));
302                 goto error;
303         }
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
309         };
310         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
311         if (ret) {
312                 rte_errno = ret;
313                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
314                       (void *)dev, strerror(rte_errno));
315                 goto error;
316         }
317         ret = mlx4_rxq_alloc_elts(&tmpl, desc);
318         if (ret) {
319                 ERROR("%p: RXQ allocation failed: %s",
320                       (void *)dev, strerror(rte_errno));
321                 goto error;
322         }
323         ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
324         if (ret) {
325                 rte_errno = ret;
326                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
327                       (void *)dev,
328                       (void *)bad_wr,
329                       strerror(rte_errno));
330                 goto error;
331         }
332         mod = (struct ibv_qp_attr){
333                 .qp_state = IBV_QPS_RTR
334         };
335         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
336         if (ret) {
337                 rte_errno = ret;
338                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
339                       (void *)dev, strerror(rte_errno));
340                 goto error;
341         }
342         /* Save port ID. */
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);
348         *rxq = tmpl;
349         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
350         return 0;
351 error:
352         ret = rte_errno;
353         mlx4_rxq_cleanup(&tmpl);
354         rte_errno = ret;
355         assert(rte_errno > 0);
356         return -rte_errno;
357 }
358
359 /**
360  * DPDK callback to configure a Rx queue.
361  *
362  * @param dev
363  *   Pointer to Ethernet device structure.
364  * @param idx
365  *   Rx queue index.
366  * @param desc
367  *   Number of descriptors to configure in queue.
368  * @param socket
369  *   NUMA socket on which memory must be allocated.
370  * @param[in] conf
371  *   Thresholds parameters.
372  * @param mp
373  *   Memory pool for buffer allocations.
374  *
375  * @return
376  *   0 on success, negative errno value otherwise and rte_errno is set.
377  */
378 int
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)
382 {
383         struct priv *priv = dev->data->dev_private;
384         struct rxq *rxq = dev->data->rx_queues[idx];
385         int ret;
386
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);
393                 return -rte_errno;
394         }
395         if (rxq != NULL) {
396                 DEBUG("%p: reusing already allocated queue index %u (%p)",
397                       (void *)dev, idx, (void *)rxq);
398                 if (priv->started) {
399                         rte_errno = EEXIST;
400                         return -rte_errno;
401                 }
402                 dev->data->rx_queues[idx] = NULL;
403                 /* Disable associated flows. */
404                 mlx4_flow_sync(priv);
405                 mlx4_rxq_cleanup(rxq);
406         } else {
407                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
408                 if (rxq == NULL) {
409                         rte_errno = ENOMEM;
410                         ERROR("%p: unable to allocate queue index %u",
411                               (void *)dev, idx);
412                         return -rte_errno;
413                 }
414         }
415         ret = mlx4_rxq_setup(dev, rxq, desc, socket, conf, mp);
416         if (ret) {
417                 rte_free(rxq);
418         } else {
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);
425                 if (ret) {
426                         dev->data->rx_queues[idx] = NULL;
427                         mlx4_rxq_cleanup(rxq);
428                         rte_free(rxq);
429                         return ret;
430                 }
431                 /* Update receive callback. */
432                 dev->rx_pkt_burst = mlx4_rx_burst;
433         }
434         return ret;
435 }
436
437 /**
438  * DPDK callback to release a Rx queue.
439  *
440  * @param dpdk_rxq
441  *   Generic Rx queue pointer.
442  */
443 void
444 mlx4_rx_queue_release(void *dpdk_rxq)
445 {
446         struct rxq *rxq = (struct rxq *)dpdk_rxq;
447         struct priv *priv;
448         unsigned int i;
449
450         if (rxq == NULL)
451                 return;
452         priv = rxq->priv;
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;
458                         break;
459                 }
460         mlx4_flow_sync(priv);
461         mlx4_rxq_cleanup(rxq);
462         rte_free(rxq);
463 }