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