1eddfc77692a706b2ce99dbd3d01bb3ed536d9a1
[dpdk.git] / drivers / net / mlx5 / mlx5_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-pedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-pedantic"
48 #endif
49
50 /* DPDK headers don't like -pedantic. */
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic ignored "-pedantic"
53 #endif
54 #include <rte_mbuf.h>
55 #include <rte_malloc.h>
56 #include <rte_ethdev.h>
57 #include <rte_common.h>
58 #ifdef PEDANTIC
59 #pragma GCC diagnostic error "-pedantic"
60 #endif
61
62 #include "mlx5.h"
63 #include "mlx5_rxtx.h"
64 #include "mlx5_utils.h"
65 #include "mlx5_defs.h"
66
67 /**
68  * Allocate RX queue elements with scattered packets support.
69  *
70  * @param rxq
71  *   Pointer to RX queue structure.
72  * @param elts_n
73  *   Number of elements to allocate.
74  * @param[in] pool
75  *   If not NULL, fetch buffers from this array instead of allocating them
76  *   with rte_pktmbuf_alloc().
77  *
78  * @return
79  *   0 on success, errno value on failure.
80  */
81 static int
82 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
83                   struct rte_mbuf **pool)
84 {
85         unsigned int i;
86         struct rxq_elt_sp (*elts)[elts_n] =
87                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
88                                   rxq->socket);
89         int ret = 0;
90
91         if (elts == NULL) {
92                 ERROR("%p: can't allocate packets array", (void *)rxq);
93                 ret = ENOMEM;
94                 goto error;
95         }
96         /* For each WR (packet). */
97         for (i = 0; (i != elts_n); ++i) {
98                 unsigned int j;
99                 struct rxq_elt_sp *elt = &(*elts)[i];
100                 struct ibv_recv_wr *wr = &elt->wr;
101                 struct ibv_sge (*sges)[RTE_DIM(elt->sges)] = &elt->sges;
102
103                 /* These two arrays must have the same size. */
104                 assert(RTE_DIM(elt->sges) == RTE_DIM(elt->bufs));
105                 /* Configure WR. */
106                 wr->wr_id = i;
107                 wr->next = &(*elts)[(i + 1)].wr;
108                 wr->sg_list = &(*sges)[0];
109                 wr->num_sge = RTE_DIM(*sges);
110                 /* For each SGE (segment). */
111                 for (j = 0; (j != RTE_DIM(elt->bufs)); ++j) {
112                         struct ibv_sge *sge = &(*sges)[j];
113                         struct rte_mbuf *buf;
114
115                         if (pool != NULL) {
116                                 buf = *(pool++);
117                                 assert(buf != NULL);
118                                 rte_pktmbuf_reset(buf);
119                         } else
120                                 buf = rte_pktmbuf_alloc(rxq->mp);
121                         if (buf == NULL) {
122                                 assert(pool == NULL);
123                                 ERROR("%p: empty mbuf pool", (void *)rxq);
124                                 ret = ENOMEM;
125                                 goto error;
126                         }
127                         elt->bufs[j] = buf;
128                         /* Headroom is reserved by rte_pktmbuf_alloc(). */
129                         assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
130                         /* Buffer is supposed to be empty. */
131                         assert(rte_pktmbuf_data_len(buf) == 0);
132                         assert(rte_pktmbuf_pkt_len(buf) == 0);
133                         /* sge->addr must be able to store a pointer. */
134                         assert(sizeof(sge->addr) >= sizeof(uintptr_t));
135                         if (j == 0) {
136                                 /* The first SGE keeps its headroom. */
137                                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
138                                 sge->length = (buf->buf_len -
139                                                RTE_PKTMBUF_HEADROOM);
140                         } else {
141                                 /* Subsequent SGEs lose theirs. */
142                                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
143                                 SET_DATA_OFF(buf, 0);
144                                 sge->addr = (uintptr_t)buf->buf_addr;
145                                 sge->length = buf->buf_len;
146                         }
147                         sge->lkey = rxq->mr->lkey;
148                         /* Redundant check for tailroom. */
149                         assert(sge->length == rte_pktmbuf_tailroom(buf));
150                 }
151         }
152         /* The last WR pointer must be NULL. */
153         (*elts)[(i - 1)].wr.next = NULL;
154         DEBUG("%p: allocated and configured %u WRs (%zu segments)",
155               (void *)rxq, elts_n, (elts_n * RTE_DIM((*elts)[0].sges)));
156         rxq->elts_n = elts_n;
157         rxq->elts_head = 0;
158         rxq->elts.sp = elts;
159         assert(ret == 0);
160         return 0;
161 error:
162         if (elts != NULL) {
163                 assert(pool == NULL);
164                 for (i = 0; (i != RTE_DIM(*elts)); ++i) {
165                         unsigned int j;
166                         struct rxq_elt_sp *elt = &(*elts)[i];
167
168                         for (j = 0; (j != RTE_DIM(elt->bufs)); ++j) {
169                                 struct rte_mbuf *buf = elt->bufs[j];
170
171                                 if (buf != NULL)
172                                         rte_pktmbuf_free_seg(buf);
173                         }
174                 }
175                 rte_free(elts);
176         }
177         DEBUG("%p: failed, freed everything", (void *)rxq);
178         assert(ret > 0);
179         return ret;
180 }
181
182 /**
183  * Free RX queue elements with scattered packets support.
184  *
185  * @param rxq
186  *   Pointer to RX queue structure.
187  */
188 static void
189 rxq_free_elts_sp(struct rxq *rxq)
190 {
191         unsigned int i;
192         unsigned int elts_n = rxq->elts_n;
193         struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
194
195         DEBUG("%p: freeing WRs", (void *)rxq);
196         rxq->elts_n = 0;
197         rxq->elts.sp = NULL;
198         if (elts == NULL)
199                 return;
200         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
201                 unsigned int j;
202                 struct rxq_elt_sp *elt = &(*elts)[i];
203
204                 for (j = 0; (j != RTE_DIM(elt->bufs)); ++j) {
205                         struct rte_mbuf *buf = elt->bufs[j];
206
207                         if (buf != NULL)
208                                 rte_pktmbuf_free_seg(buf);
209                 }
210         }
211         rte_free(elts);
212 }
213
214 /**
215  * Allocate RX queue elements.
216  *
217  * @param rxq
218  *   Pointer to RX queue structure.
219  * @param elts_n
220  *   Number of elements to allocate.
221  * @param[in] pool
222  *   If not NULL, fetch buffers from this array instead of allocating them
223  *   with rte_pktmbuf_alloc().
224  *
225  * @return
226  *   0 on success, errno value on failure.
227  */
228 static int
229 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
230 {
231         unsigned int i;
232         struct rxq_elt (*elts)[elts_n] =
233                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
234                                   rxq->socket);
235         int ret = 0;
236
237         if (elts == NULL) {
238                 ERROR("%p: can't allocate packets array", (void *)rxq);
239                 ret = ENOMEM;
240                 goto error;
241         }
242         /* For each WR (packet). */
243         for (i = 0; (i != elts_n); ++i) {
244                 struct rxq_elt *elt = &(*elts)[i];
245                 struct ibv_recv_wr *wr = &elt->wr;
246                 struct ibv_sge *sge = &(*elts)[i].sge;
247                 struct rte_mbuf *buf;
248
249                 if (pool != NULL) {
250                         buf = *(pool++);
251                         assert(buf != NULL);
252                         rte_pktmbuf_reset(buf);
253                 } else
254                         buf = rte_pktmbuf_alloc(rxq->mp);
255                 if (buf == NULL) {
256                         assert(pool == NULL);
257                         ERROR("%p: empty mbuf pool", (void *)rxq);
258                         ret = ENOMEM;
259                         goto error;
260                 }
261                 /* Configure WR. Work request ID contains its own index in
262                  * the elts array and the offset between SGE buffer header and
263                  * its data. */
264                 WR_ID(wr->wr_id).id = i;
265                 WR_ID(wr->wr_id).offset =
266                         (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
267                          (uintptr_t)buf);
268                 wr->next = &(*elts)[(i + 1)].wr;
269                 wr->sg_list = sge;
270                 wr->num_sge = 1;
271                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
272                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
273                 /* Buffer is supposed to be empty. */
274                 assert(rte_pktmbuf_data_len(buf) == 0);
275                 assert(rte_pktmbuf_pkt_len(buf) == 0);
276                 /* sge->addr must be able to store a pointer. */
277                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
278                 /* SGE keeps its headroom. */
279                 sge->addr = (uintptr_t)
280                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
281                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
282                 sge->lkey = rxq->mr->lkey;
283                 /* Redundant check for tailroom. */
284                 assert(sge->length == rte_pktmbuf_tailroom(buf));
285                 /* Make sure elts index and SGE mbuf pointer can be deduced
286                  * from WR ID. */
287                 if ((WR_ID(wr->wr_id).id != i) ||
288                     ((void *)((uintptr_t)sge->addr -
289                         WR_ID(wr->wr_id).offset) != buf)) {
290                         ERROR("%p: cannot store index and offset in WR ID",
291                               (void *)rxq);
292                         sge->addr = 0;
293                         rte_pktmbuf_free(buf);
294                         ret = EOVERFLOW;
295                         goto error;
296                 }
297         }
298         /* The last WR pointer must be NULL. */
299         (*elts)[(i - 1)].wr.next = NULL;
300         DEBUG("%p: allocated and configured %u single-segment WRs",
301               (void *)rxq, elts_n);
302         rxq->elts_n = elts_n;
303         rxq->elts_head = 0;
304         rxq->elts.no_sp = elts;
305         assert(ret == 0);
306         return 0;
307 error:
308         if (elts != NULL) {
309                 assert(pool == NULL);
310                 for (i = 0; (i != RTE_DIM(*elts)); ++i) {
311                         struct rxq_elt *elt = &(*elts)[i];
312                         struct rte_mbuf *buf;
313
314                         if (elt->sge.addr == 0)
315                                 continue;
316                         assert(WR_ID(elt->wr.wr_id).id == i);
317                         buf = (void *)((uintptr_t)elt->sge.addr -
318                                 WR_ID(elt->wr.wr_id).offset);
319                         rte_pktmbuf_free_seg(buf);
320                 }
321                 rte_free(elts);
322         }
323         DEBUG("%p: failed, freed everything", (void *)rxq);
324         assert(ret > 0);
325         return ret;
326 }
327
328 /**
329  * Free RX queue elements.
330  *
331  * @param rxq
332  *   Pointer to RX queue structure.
333  */
334 static void
335 rxq_free_elts(struct rxq *rxq)
336 {
337         unsigned int i;
338         unsigned int elts_n = rxq->elts_n;
339         struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
340
341         DEBUG("%p: freeing WRs", (void *)rxq);
342         rxq->elts_n = 0;
343         rxq->elts.no_sp = NULL;
344         if (elts == NULL)
345                 return;
346         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
347                 struct rxq_elt *elt = &(*elts)[i];
348                 struct rte_mbuf *buf;
349
350                 if (elt->sge.addr == 0)
351                         continue;
352                 assert(WR_ID(elt->wr.wr_id).id == i);
353                 buf = (void *)((uintptr_t)elt->sge.addr -
354                         WR_ID(elt->wr.wr_id).offset);
355                 rte_pktmbuf_free_seg(buf);
356         }
357         rte_free(elts);
358 }
359
360 /**
361  * Clean up a RX queue.
362  *
363  * Destroy objects, free allocated memory and reset the structure for reuse.
364  *
365  * @param rxq
366  *   Pointer to RX queue structure.
367  */
368 void
369 rxq_cleanup(struct rxq *rxq)
370 {
371         struct ibv_exp_release_intf_params params;
372
373         DEBUG("cleaning up %p", (void *)rxq);
374         if (rxq->sp)
375                 rxq_free_elts_sp(rxq);
376         else
377                 rxq_free_elts(rxq);
378         if (rxq->if_qp != NULL) {
379                 assert(rxq->priv != NULL);
380                 assert(rxq->priv->ctx != NULL);
381                 assert(rxq->qp != NULL);
382                 params = (struct ibv_exp_release_intf_params){
383                         .comp_mask = 0,
384                 };
385                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
386                                                 rxq->if_qp,
387                                                 &params));
388         }
389         if (rxq->if_cq != NULL) {
390                 assert(rxq->priv != NULL);
391                 assert(rxq->priv->ctx != NULL);
392                 assert(rxq->cq != NULL);
393                 params = (struct ibv_exp_release_intf_params){
394                         .comp_mask = 0,
395                 };
396                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
397                                                 rxq->if_cq,
398                                                 &params));
399         }
400         if (rxq->qp != NULL) {
401                 rxq_mac_addrs_del(rxq);
402                 claim_zero(ibv_destroy_qp(rxq->qp));
403         }
404         if (rxq->cq != NULL)
405                 claim_zero(ibv_destroy_cq(rxq->cq));
406         if (rxq->rd != NULL) {
407                 struct ibv_exp_destroy_res_domain_attr attr = {
408                         .comp_mask = 0,
409                 };
410
411                 assert(rxq->priv != NULL);
412                 assert(rxq->priv->ctx != NULL);
413                 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
414                                                       rxq->rd,
415                                                       &attr));
416         }
417         if (rxq->mr != NULL)
418                 claim_zero(ibv_dereg_mr(rxq->mr));
419         memset(rxq, 0, sizeof(*rxq));
420 }
421
422 /**
423  * Allocate a Queue Pair.
424  * Optionally setup inline receive if supported.
425  *
426  * @param priv
427  *   Pointer to private structure.
428  * @param cq
429  *   Completion queue to associate with QP.
430  * @param desc
431  *   Number of descriptors in QP (hint only).
432  *
433  * @return
434  *   QP pointer or NULL in case of error.
435  */
436 static struct ibv_qp *
437 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
438              struct ibv_exp_res_domain *rd)
439 {
440         struct ibv_exp_qp_init_attr attr = {
441                 /* CQ to be associated with the send queue. */
442                 .send_cq = cq,
443                 /* CQ to be associated with the receive queue. */
444                 .recv_cq = cq,
445                 .cap = {
446                         /* Max number of outstanding WRs. */
447                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
448                                         priv->device_attr.max_qp_wr :
449                                         desc),
450                         /* Max number of scatter/gather elements in a WR. */
451                         .max_recv_sge = ((priv->device_attr.max_sge <
452                                           MLX5_PMD_SGE_WR_N) ?
453                                          priv->device_attr.max_sge :
454                                          MLX5_PMD_SGE_WR_N),
455                 },
456                 .qp_type = IBV_QPT_RAW_PACKET,
457                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
458                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
459                 .pd = priv->pd,
460                 .res_domain = rd,
461         };
462
463         return ibv_exp_create_qp(priv->ctx, &attr);
464 }
465
466 #ifdef RSS_SUPPORT
467
468 /**
469  * Allocate a RSS Queue Pair.
470  * Optionally setup inline receive if supported.
471  *
472  * @param priv
473  *   Pointer to private structure.
474  * @param cq
475  *   Completion queue to associate with QP.
476  * @param desc
477  *   Number of descriptors in QP (hint only).
478  * @param parent
479  *   If nonzero, create a parent QP, otherwise a child.
480  *
481  * @return
482  *   QP pointer or NULL in case of error.
483  */
484 static struct ibv_qp *
485 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
486                  int parent, struct ibv_exp_res_domain *rd)
487 {
488         struct ibv_exp_qp_init_attr attr = {
489                 /* CQ to be associated with the send queue. */
490                 .send_cq = cq,
491                 /* CQ to be associated with the receive queue. */
492                 .recv_cq = cq,
493                 .cap = {
494                         /* Max number of outstanding WRs. */
495                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
496                                         priv->device_attr.max_qp_wr :
497                                         desc),
498                         /* Max number of scatter/gather elements in a WR. */
499                         .max_recv_sge = ((priv->device_attr.max_sge <
500                                           MLX5_PMD_SGE_WR_N) ?
501                                          priv->device_attr.max_sge :
502                                          MLX5_PMD_SGE_WR_N),
503                 },
504                 .qp_type = IBV_QPT_RAW_PACKET,
505                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
506                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
507                               IBV_EXP_QP_INIT_ATTR_QPG),
508                 .pd = priv->pd,
509                 .res_domain = rd,
510         };
511
512         if (parent) {
513                 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
514                 /* TSS isn't necessary. */
515                 attr.qpg.parent_attrib.tss_child_count = 0;
516                 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
517                 DEBUG("initializing parent RSS queue");
518         } else {
519                 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
520                 attr.qpg.qpg_parent = priv->rxq_parent.qp;
521                 DEBUG("initializing child RSS queue");
522         }
523         return ibv_exp_create_qp(priv->ctx, &attr);
524 }
525
526 #endif /* RSS_SUPPORT */
527
528 /**
529  * Configure a RX queue.
530  *
531  * @param dev
532  *   Pointer to Ethernet device structure.
533  * @param rxq
534  *   Pointer to RX queue structure.
535  * @param desc
536  *   Number of descriptors to configure in queue.
537  * @param socket
538  *   NUMA socket on which memory must be allocated.
539  * @param[in] conf
540  *   Thresholds parameters.
541  * @param mp
542  *   Memory pool for buffer allocations.
543  *
544  * @return
545  *   0 on success, errno value on failure.
546  */
547 int
548 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
549           unsigned int socket, const struct rte_eth_rxconf *conf,
550           struct rte_mempool *mp)
551 {
552         struct priv *priv = dev->data->dev_private;
553         struct rxq tmpl = {
554                 .priv = priv,
555                 .mp = mp,
556                 .socket = socket
557         };
558         struct ibv_exp_qp_attr mod;
559         union {
560                 struct ibv_exp_query_intf_params params;
561                 struct ibv_exp_cq_init_attr cq;
562                 struct ibv_exp_res_domain_init_attr rd;
563         } attr;
564         enum ibv_exp_query_intf_status status;
565         struct ibv_recv_wr *bad_wr;
566         struct rte_mbuf *buf;
567         int ret = 0;
568         int parent = (rxq == &priv->rxq_parent);
569
570         (void)conf; /* Thresholds configuration (ignored). */
571         /*
572          * If this is a parent queue, hardware must support RSS and
573          * RSS must be enabled.
574          */
575         assert((!parent) || ((priv->hw_rss) && (priv->rss)));
576         if (parent) {
577                 /* Even if unused, ibv_create_cq() requires at least one
578                  * descriptor. */
579                 desc = 1;
580                 goto skip_mr;
581         }
582         if ((desc == 0) || (desc % MLX5_PMD_SGE_WR_N)) {
583                 ERROR("%p: invalid number of RX descriptors (must be a"
584                       " multiple of %d)", (void *)dev, MLX5_PMD_SGE_WR_N);
585                 return EINVAL;
586         }
587         /* Get mbuf length. */
588         buf = rte_pktmbuf_alloc(mp);
589         if (buf == NULL) {
590                 ERROR("%p: unable to allocate mbuf", (void *)dev);
591                 return ENOMEM;
592         }
593         tmpl.mb_len = buf->buf_len;
594         assert((rte_pktmbuf_headroom(buf) +
595                 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
596         assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
597         rte_pktmbuf_free(buf);
598         /* Enable scattered packets support for this queue if necessary. */
599         if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
600             (dev->data->dev_conf.rxmode.max_rx_pkt_len >
601              (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
602                 tmpl.sp = 1;
603                 desc /= MLX5_PMD_SGE_WR_N;
604         }
605         DEBUG("%p: %s scattered packets support (%u WRs)",
606               (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
607         /* Use the entire RX mempool as the memory region. */
608         tmpl.mr = ibv_reg_mr(priv->pd,
609                              (void *)mp->elt_va_start,
610                              (mp->elt_va_end - mp->elt_va_start),
611                              (IBV_ACCESS_LOCAL_WRITE |
612                               IBV_ACCESS_REMOTE_WRITE));
613         if (tmpl.mr == NULL) {
614                 ret = EINVAL;
615                 ERROR("%p: MR creation failure: %s",
616                       (void *)dev, strerror(ret));
617                 goto error;
618         }
619 skip_mr:
620         attr.rd = (struct ibv_exp_res_domain_init_attr){
621                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
622                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
623                 .thread_model = IBV_EXP_THREAD_SINGLE,
624                 .msg_model = IBV_EXP_MSG_HIGH_BW,
625         };
626         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
627         if (tmpl.rd == NULL) {
628                 ret = ENOMEM;
629                 ERROR("%p: RD creation failure: %s",
630                       (void *)dev, strerror(ret));
631                 goto error;
632         }
633         attr.cq = (struct ibv_exp_cq_init_attr){
634                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
635                 .res_domain = tmpl.rd,
636         };
637         tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
638         if (tmpl.cq == NULL) {
639                 ret = ENOMEM;
640                 ERROR("%p: CQ creation failure: %s",
641                       (void *)dev, strerror(ret));
642                 goto error;
643         }
644         DEBUG("priv->device_attr.max_qp_wr is %d",
645               priv->device_attr.max_qp_wr);
646         DEBUG("priv->device_attr.max_sge is %d",
647               priv->device_attr.max_sge);
648 #ifdef RSS_SUPPORT
649         if (priv->rss)
650                 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
651                                            tmpl.rd);
652         else
653 #endif /* RSS_SUPPORT */
654                 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
655         if (tmpl.qp == NULL) {
656                 ret = (errno ? errno : EINVAL);
657                 ERROR("%p: QP creation failure: %s",
658                       (void *)dev, strerror(ret));
659                 goto error;
660         }
661         mod = (struct ibv_exp_qp_attr){
662                 /* Move the QP to this state. */
663                 .qp_state = IBV_QPS_INIT,
664                 /* Primary port number. */
665                 .port_num = priv->port
666         };
667         ret = ibv_exp_modify_qp(tmpl.qp, &mod,
668                                 (IBV_EXP_QP_STATE |
669 #ifdef RSS_SUPPORT
670                                  (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
671 #endif /* RSS_SUPPORT */
672                                  IBV_EXP_QP_PORT));
673         if (ret) {
674                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
675                       (void *)dev, strerror(ret));
676                 goto error;
677         }
678         if ((parent) || (!priv->rss))  {
679                 /* Configure MAC and broadcast addresses. */
680                 ret = rxq_mac_addrs_add(&tmpl);
681                 if (ret) {
682                         ERROR("%p: QP flow attachment failed: %s",
683                               (void *)dev, strerror(ret));
684                         goto error;
685                 }
686         }
687         /* Allocate descriptors for RX queues, except for the RSS parent. */
688         if (parent)
689                 goto skip_alloc;
690         if (tmpl.sp)
691                 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
692         else
693                 ret = rxq_alloc_elts(&tmpl, desc, NULL);
694         if (ret) {
695                 ERROR("%p: RXQ allocation failed: %s",
696                       (void *)dev, strerror(ret));
697                 goto error;
698         }
699         ret = ibv_post_recv(tmpl.qp,
700                             (tmpl.sp ?
701                              &(*tmpl.elts.sp)[0].wr :
702                              &(*tmpl.elts.no_sp)[0].wr),
703                             &bad_wr);
704         if (ret) {
705                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
706                       (void *)dev,
707                       (void *)bad_wr,
708                       strerror(ret));
709                 goto error;
710         }
711 skip_alloc:
712         mod = (struct ibv_exp_qp_attr){
713                 .qp_state = IBV_QPS_RTR
714         };
715         ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
716         if (ret) {
717                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
718                       (void *)dev, strerror(ret));
719                 goto error;
720         }
721         /* Save port ID. */
722         tmpl.port_id = dev->data->port_id;
723         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
724         attr.params = (struct ibv_exp_query_intf_params){
725                 .intf_scope = IBV_EXP_INTF_GLOBAL,
726                 .intf = IBV_EXP_INTF_CQ,
727                 .obj = tmpl.cq,
728         };
729         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
730         if (tmpl.if_cq == NULL) {
731                 ERROR("%p: CQ interface family query failed with status %d",
732                       (void *)dev, status);
733                 goto error;
734         }
735         attr.params = (struct ibv_exp_query_intf_params){
736                 .intf_scope = IBV_EXP_INTF_GLOBAL,
737                 .intf = IBV_EXP_INTF_QP_BURST,
738                 .obj = tmpl.qp,
739         };
740         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
741         if (tmpl.if_qp == NULL) {
742                 ERROR("%p: QP interface family query failed with status %d",
743                       (void *)dev, status);
744                 goto error;
745         }
746         /* Clean up rxq in case we're reinitializing it. */
747         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
748         rxq_cleanup(rxq);
749         *rxq = tmpl;
750         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
751         assert(ret == 0);
752         return 0;
753 error:
754         rxq_cleanup(&tmpl);
755         assert(ret > 0);
756         return ret;
757 }
758
759 /**
760  * DPDK callback to configure a RX queue.
761  *
762  * @param dev
763  *   Pointer to Ethernet device structure.
764  * @param idx
765  *   RX queue index.
766  * @param desc
767  *   Number of descriptors to configure in queue.
768  * @param socket
769  *   NUMA socket on which memory must be allocated.
770  * @param[in] conf
771  *   Thresholds parameters.
772  * @param mp
773  *   Memory pool for buffer allocations.
774  *
775  * @return
776  *   0 on success, negative errno value on failure.
777  */
778 int
779 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
780                     unsigned int socket, const struct rte_eth_rxconf *conf,
781                     struct rte_mempool *mp)
782 {
783         struct priv *priv = dev->data->dev_private;
784         struct rxq *rxq = (*priv->rxqs)[idx];
785         int ret;
786
787         priv_lock(priv);
788         DEBUG("%p: configuring queue %u for %u descriptors",
789               (void *)dev, idx, desc);
790         if (idx >= priv->rxqs_n) {
791                 ERROR("%p: queue index out of range (%u >= %u)",
792                       (void *)dev, idx, priv->rxqs_n);
793                 priv_unlock(priv);
794                 return -EOVERFLOW;
795         }
796         if (rxq != NULL) {
797                 DEBUG("%p: reusing already allocated queue index %u (%p)",
798                       (void *)dev, idx, (void *)rxq);
799                 if (priv->started) {
800                         priv_unlock(priv);
801                         return -EEXIST;
802                 }
803                 (*priv->rxqs)[idx] = NULL;
804                 rxq_cleanup(rxq);
805         } else {
806                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
807                 if (rxq == NULL) {
808                         ERROR("%p: unable to allocate queue index %u",
809                               (void *)dev, idx);
810                         priv_unlock(priv);
811                         return -ENOMEM;
812                 }
813         }
814         ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
815         if (ret)
816                 rte_free(rxq);
817         else {
818                 DEBUG("%p: adding RX queue %p to list",
819                       (void *)dev, (void *)rxq);
820                 (*priv->rxqs)[idx] = rxq;
821                 /* Update receive callback. */
822                 if (rxq->sp)
823                         dev->rx_pkt_burst = mlx5_rx_burst_sp;
824                 else
825                         dev->rx_pkt_burst = mlx5_rx_burst;
826         }
827         priv_unlock(priv);
828         return -ret;
829 }
830
831 /**
832  * DPDK callback to release a RX queue.
833  *
834  * @param dpdk_rxq
835  *   Generic RX queue pointer.
836  */
837 void
838 mlx5_rx_queue_release(void *dpdk_rxq)
839 {
840         struct rxq *rxq = (struct rxq *)dpdk_rxq;
841         struct priv *priv;
842         unsigned int i;
843
844         if (rxq == NULL)
845                 return;
846         priv = rxq->priv;
847         priv_lock(priv);
848         assert(rxq != &priv->rxq_parent);
849         for (i = 0; (i != priv->rxqs_n); ++i)
850                 if ((*priv->rxqs)[i] == rxq) {
851                         DEBUG("%p: removing RX queue %p from list",
852                               (void *)priv->dev, (void *)rxq);
853                         (*priv->rxqs)[i] = NULL;
854                         break;
855                 }
856         rxq_cleanup(rxq);
857         rte_free(rxq);
858         priv_unlock(priv);
859 }