net/mlx4: fix no Rx interrupts
[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/mlx4dv.h>
50 #include <infiniband/verbs.h>
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic error "-Wpedantic"
53 #endif
54
55 #include <rte_byteorder.h>
56 #include <rte_common.h>
57 #include <rte_errno.h>
58 #include <rte_ethdev.h>
59 #include <rte_flow.h>
60 #include <rte_malloc.h>
61 #include <rte_mbuf.h>
62 #include <rte_mempool.h>
63
64 #include "mlx4.h"
65 #include "mlx4_flow.h"
66 #include "mlx4_rxtx.h"
67 #include "mlx4_utils.h"
68
69 /**
70  * Historical RSS hash key.
71  *
72  * This used to be the default for mlx4 in Linux before v3.19 switched to
73  * generating random hash keys through netdev_rss_key_fill().
74  *
75  * It is used in this PMD for consistency with past DPDK releases but can
76  * now be overridden through user configuration.
77  *
78  * Note: this is not const to work around API quirks.
79  */
80 uint8_t
81 mlx4_rss_hash_key_default[MLX4_RSS_HASH_KEY_SIZE] = {
82         0x2c, 0xc6, 0x81, 0xd1,
83         0x5b, 0xdb, 0xf4, 0xf7,
84         0xfc, 0xa2, 0x83, 0x19,
85         0xdb, 0x1a, 0x3e, 0x94,
86         0x6b, 0x9e, 0x38, 0xd9,
87         0x2c, 0x9c, 0x03, 0xd1,
88         0xad, 0x99, 0x44, 0xa7,
89         0xd9, 0x56, 0x3d, 0x59,
90         0x06, 0x3c, 0x25, 0xf3,
91         0xfc, 0x1f, 0xdc, 0x2a,
92 };
93
94 /**
95  * Obtain a RSS context with specified properties.
96  *
97  * Used when creating a flow rule targeting one or several Rx queues.
98  *
99  * If a matching RSS context already exists, it is returned with its
100  * reference count incremented.
101  *
102  * @param priv
103  *   Pointer to private structure.
104  * @param fields
105  *   Fields for RSS processing (Verbs format).
106  * @param[in] key
107  *   Hash key to use (whose size is exactly MLX4_RSS_HASH_KEY_SIZE).
108  * @param queues
109  *   Number of target queues.
110  * @param[in] queue_id
111  *   Target queues.
112  *
113  * @return
114  *   Pointer to RSS context on success, NULL otherwise and rte_errno is set.
115  */
116 struct mlx4_rss *
117 mlx4_rss_get(struct priv *priv, uint64_t fields,
118              uint8_t key[MLX4_RSS_HASH_KEY_SIZE],
119              uint16_t queues, const uint16_t queue_id[])
120 {
121         struct mlx4_rss *rss;
122         size_t queue_id_size = sizeof(queue_id[0]) * queues;
123
124         LIST_FOREACH(rss, &priv->rss, next)
125                 if (fields == rss->fields &&
126                     queues == rss->queues &&
127                     !memcmp(key, rss->key, MLX4_RSS_HASH_KEY_SIZE) &&
128                     !memcmp(queue_id, rss->queue_id, queue_id_size)) {
129                         ++rss->refcnt;
130                         return rss;
131                 }
132         rss = rte_malloc(__func__, offsetof(struct mlx4_rss, queue_id) +
133                          queue_id_size, 0);
134         if (!rss)
135                 goto error;
136         *rss = (struct mlx4_rss){
137                 .priv = priv,
138                 .refcnt = 1,
139                 .usecnt = 0,
140                 .qp = NULL,
141                 .ind = NULL,
142                 .fields = fields,
143                 .queues = queues,
144         };
145         memcpy(rss->key, key, MLX4_RSS_HASH_KEY_SIZE);
146         memcpy(rss->queue_id, queue_id, queue_id_size);
147         LIST_INSERT_HEAD(&priv->rss, rss, next);
148         return rss;
149 error:
150         rte_errno = ENOMEM;
151         return NULL;
152 }
153
154 /**
155  * Release a RSS context instance.
156  *
157  * Used when destroying a flow rule targeting one or several Rx queues.
158  *
159  * This function decrements the reference count of the context and destroys
160  * it after reaching 0. The context must have no users at this point; all
161  * prior calls to mlx4_rss_attach() must have been followed by matching
162  * calls to mlx4_rss_detach().
163  *
164  * @param rss
165  *   RSS context to release.
166  */
167 void mlx4_rss_put(struct mlx4_rss *rss)
168 {
169         assert(rss->refcnt);
170         if (--rss->refcnt)
171                 return;
172         assert(!rss->usecnt);
173         assert(!rss->qp);
174         assert(!rss->ind);
175         LIST_REMOVE(rss, next);
176         rte_free(rss);
177 }
178
179 /**
180  * Attach a user to a RSS context instance.
181  *
182  * Used when the RSS QP and indirection table objects must be instantiated,
183  * that is, when a flow rule must be enabled.
184  *
185  * This function increments the usage count of the context.
186  *
187  * @param rss
188  *   RSS context to attach to.
189  *
190  * @return
191  *   0 on success, a negative errno value otherwise and rte_errno is set.
192  */
193 int mlx4_rss_attach(struct mlx4_rss *rss)
194 {
195         assert(rss->refcnt);
196         if (rss->usecnt++) {
197                 assert(rss->qp);
198                 assert(rss->ind);
199                 return 0;
200         }
201
202         struct ibv_wq *ind_tbl[rss->queues];
203         struct priv *priv = rss->priv;
204         const char *msg;
205         unsigned int i = 0;
206         int ret;
207
208         if (!rte_is_power_of_2(RTE_DIM(ind_tbl))) {
209                 ret = EINVAL;
210                 msg = "number of RSS queues must be a power of two";
211                 goto error;
212         }
213         for (i = 0; i != RTE_DIM(ind_tbl); ++i) {
214                 uint16_t id = rss->queue_id[i];
215                 struct rxq *rxq = NULL;
216
217                 if (id < priv->dev->data->nb_rx_queues)
218                         rxq = priv->dev->data->rx_queues[id];
219                 if (!rxq) {
220                         ret = EINVAL;
221                         msg = "RSS target queue is not configured";
222                         goto error;
223                 }
224                 ret = mlx4_rxq_attach(rxq);
225                 if (ret) {
226                         ret = -ret;
227                         msg = "unable to attach RSS target queue";
228                         goto error;
229                 }
230                 ind_tbl[i] = rxq->wq;
231         }
232         rss->ind = ibv_create_rwq_ind_table
233                 (priv->ctx,
234                  &(struct ibv_rwq_ind_table_init_attr){
235                         .log_ind_tbl_size = rte_log2_u32(RTE_DIM(ind_tbl)),
236                         .ind_tbl = ind_tbl,
237                         .comp_mask = 0,
238                  });
239         if (!rss->ind) {
240                 ret = errno ? errno : EINVAL;
241                 msg = "RSS indirection table creation failure";
242                 goto error;
243         }
244         rss->qp = ibv_create_qp_ex
245                 (priv->ctx,
246                  &(struct ibv_qp_init_attr_ex){
247                         .comp_mask = (IBV_QP_INIT_ATTR_PD |
248                                       IBV_QP_INIT_ATTR_RX_HASH |
249                                       IBV_QP_INIT_ATTR_IND_TABLE),
250                         .qp_type = IBV_QPT_RAW_PACKET,
251                         .pd = priv->pd,
252                         .rwq_ind_tbl = rss->ind,
253                         .rx_hash_conf = {
254                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
255                                 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
256                                 .rx_hash_key = rss->key,
257                                 .rx_hash_fields_mask = rss->fields,
258                         },
259                  });
260         if (!rss->qp) {
261                 ret = errno ? errno : EINVAL;
262                 msg = "RSS hash QP creation failure";
263                 goto error;
264         }
265         ret = ibv_modify_qp
266                 (rss->qp,
267                  &(struct ibv_qp_attr){
268                         .qp_state = IBV_QPS_INIT,
269                         .port_num = priv->port,
270                  },
271                  IBV_QP_STATE | IBV_QP_PORT);
272         if (ret) {
273                 msg = "failed to switch RSS hash QP to INIT state";
274                 goto error;
275         }
276         ret = ibv_modify_qp
277                 (rss->qp,
278                  &(struct ibv_qp_attr){
279                         .qp_state = IBV_QPS_RTR,
280                  },
281                  IBV_QP_STATE);
282         if (ret) {
283                 msg = "failed to switch RSS hash QP to RTR state";
284                 goto error;
285         }
286         return 0;
287 error:
288         if (rss->qp) {
289                 claim_zero(ibv_destroy_qp(rss->qp));
290                 rss->qp = NULL;
291         }
292         if (rss->ind) {
293                 claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
294                 rss->ind = NULL;
295         }
296         while (i--)
297                 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
298         ERROR("mlx4: %s", msg);
299         --rss->usecnt;
300         rte_errno = ret;
301         return -ret;
302 }
303
304 /**
305  * Detach a user from a RSS context instance.
306  *
307  * Used when disabling (not destroying) a flow rule.
308  *
309  * This function decrements the usage count of the context and destroys
310  * usage resources after reaching 0.
311  *
312  * @param rss
313  *   RSS context to detach from.
314  */
315 void mlx4_rss_detach(struct mlx4_rss *rss)
316 {
317         struct priv *priv = rss->priv;
318         unsigned int i;
319
320         assert(rss->refcnt);
321         assert(rss->qp);
322         assert(rss->ind);
323         if (--rss->usecnt)
324                 return;
325         claim_zero(ibv_destroy_qp(rss->qp));
326         rss->qp = NULL;
327         claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
328         rss->ind = NULL;
329         for (i = 0; i != rss->queues; ++i)
330                 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
331 }
332
333 /**
334  * Initialize common RSS context resources.
335  *
336  * Because ConnectX-3 hardware limitations require a fixed order in the
337  * indirection table, WQs must be allocated sequentially to be part of a
338  * common RSS context.
339  *
340  * Since a newly created WQ cannot be moved to a different context, this
341  * function allocates them all at once, one for each configured Rx queue,
342  * as well as all related resources (CQs and mbufs).
343  *
344  * This must therefore be done before creating any Rx flow rules relying on
345  * indirection tables.
346  *
347  * @param priv
348  *   Pointer to private structure.
349  *
350  * @return
351  *   0 on success, a negative errno value otherwise and rte_errno is set.
352  */
353 int
354 mlx4_rss_init(struct priv *priv)
355 {
356         struct rte_eth_dev *dev = priv->dev;
357         uint8_t log2_range = rte_log2_u32(dev->data->nb_rx_queues);
358         uint32_t wq_num_prev = 0;
359         const char *msg;
360         unsigned int i;
361         int ret;
362
363         /* Prepare range for RSS contexts before creating the first WQ. */
364         ret = mlx4dv_set_context_attr(priv->ctx,
365                                       MLX4DV_SET_CTX_ATTR_LOG_WQS_RANGE_SZ,
366                                       &log2_range);
367         if (ret) {
368                 ERROR("cannot set up range size for RSS context to %u"
369                       " (for %u Rx queues), error: %s",
370                       1 << log2_range, dev->data->nb_rx_queues, strerror(ret));
371                 rte_errno = ret;
372                 return -ret;
373         }
374         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
375                 struct rxq *rxq = priv->dev->data->rx_queues[i];
376                 struct ibv_cq *cq;
377                 struct ibv_wq *wq;
378                 uint32_t wq_num;
379
380                 /* Attach the configured Rx queues. */
381                 if (rxq) {
382                         assert(!rxq->usecnt);
383                         ret = mlx4_rxq_attach(rxq);
384                         if (!ret) {
385                                 wq_num = rxq->wq->wq_num;
386                                 goto wq_num_check;
387                         }
388                         ret = -ret;
389                         msg = "unable to create Rx queue resources";
390                         goto error;
391                 }
392                 /*
393                  * WQs are temporarily allocated for unconfigured Rx queues
394                  * to maintain proper index alignment in indirection table
395                  * by skipping unused WQ numbers.
396                  *
397                  * The reason this works at all even though these WQs are
398                  * immediately destroyed is that WQNs are allocated
399                  * sequentially and are guaranteed to never be reused in the
400                  * same context by the underlying implementation.
401                  */
402                 cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
403                 if (!cq) {
404                         ret = ENOMEM;
405                         msg = "placeholder CQ creation failure";
406                         goto error;
407                 }
408                 wq = ibv_create_wq
409                         (priv->ctx,
410                          &(struct ibv_wq_init_attr){
411                                 .wq_type = IBV_WQT_RQ,
412                                 .max_wr = 1,
413                                 .max_sge = 1,
414                                 .pd = priv->pd,
415                                 .cq = cq,
416                          });
417                 if (wq) {
418                         wq_num = wq->wq_num;
419                         claim_zero(ibv_destroy_wq(wq));
420                 } else {
421                         wq_num = 0; /* Shut up GCC 4.8 warnings. */
422                 }
423                 claim_zero(ibv_destroy_cq(cq));
424                 if (!wq) {
425                         ret = ENOMEM;
426                         msg = "placeholder WQ creation failure";
427                         goto error;
428                 }
429 wq_num_check:
430                 /*
431                  * While guaranteed by the implementation, make sure WQ
432                  * numbers are really sequential (as the saying goes,
433                  * trust, but verify).
434                  */
435                 if (i && wq_num - wq_num_prev != 1) {
436                         if (rxq)
437                                 mlx4_rxq_detach(rxq);
438                         ret = ERANGE;
439                         msg = "WQ numbers are not sequential";
440                         goto error;
441                 }
442                 wq_num_prev = wq_num;
443         }
444         return 0;
445 error:
446         ERROR("cannot initialize common RSS resources (queue %u): %s: %s",
447               i, msg, strerror(ret));
448         while (i--) {
449                 struct rxq *rxq = priv->dev->data->rx_queues[i];
450
451                 if (rxq)
452                         mlx4_rxq_detach(rxq);
453         }
454         rte_errno = ret;
455         return -ret;
456 }
457
458 /**
459  * Release common RSS context resources.
460  *
461  * As the reverse of mlx4_rss_init(), this must be done after removing all
462  * flow rules relying on indirection tables.
463  *
464  * @param priv
465  *   Pointer to private structure.
466  */
467 void
468 mlx4_rss_deinit(struct priv *priv)
469 {
470         unsigned int i;
471
472         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
473                 struct rxq *rxq = priv->dev->data->rx_queues[i];
474
475                 if (rxq) {
476                         assert(rxq->usecnt == 1);
477                         mlx4_rxq_detach(rxq);
478                 }
479         }
480 }
481
482 /**
483  * Attach a user to a Rx queue.
484  *
485  * Used when the resources of an Rx queue must be instantiated for it to
486  * become in a usable state.
487  *
488  * This function increments the usage count of the Rx queue.
489  *
490  * @param rxq
491  *   Pointer to Rx queue structure.
492  *
493  * @return
494  *   0 on success, negative errno value otherwise and rte_errno is set.
495  */
496 int
497 mlx4_rxq_attach(struct rxq *rxq)
498 {
499         if (rxq->usecnt++) {
500                 assert(rxq->cq);
501                 assert(rxq->wq);
502                 assert(rxq->wqes);
503                 assert(rxq->rq_db);
504                 return 0;
505         }
506
507         struct priv *priv = rxq->priv;
508         const uint32_t elts_n = 1 << rxq->elts_n;
509         const uint32_t sges_n = 1 << rxq->sges_n;
510         struct rte_mbuf *(*elts)[elts_n] = rxq->elts;
511         struct mlx4dv_obj mlxdv;
512         struct mlx4dv_rwq dv_rwq;
513         struct mlx4dv_cq dv_cq = { .comp_mask = MLX4DV_CQ_MASK_UAR, };
514         const char *msg;
515         struct ibv_cq *cq = NULL;
516         struct ibv_wq *wq = NULL;
517         volatile struct mlx4_wqe_data_seg (*wqes)[];
518         unsigned int i;
519         int ret;
520
521         assert(rte_is_power_of_2(elts_n));
522         cq = ibv_create_cq(priv->ctx, elts_n / sges_n, NULL, rxq->channel, 0);
523         if (!cq) {
524                 ret = ENOMEM;
525                 msg = "CQ creation failure";
526                 goto error;
527         }
528         wq = ibv_create_wq
529                 (priv->ctx,
530                  &(struct ibv_wq_init_attr){
531                         .wq_type = IBV_WQT_RQ,
532                         .max_wr = elts_n / sges_n,
533                         .max_sge = sges_n,
534                         .pd = priv->pd,
535                         .cq = cq,
536                  });
537         if (!wq) {
538                 ret = errno ? errno : EINVAL;
539                 msg = "WQ creation failure";
540                 goto error;
541         }
542         ret = ibv_modify_wq
543                 (wq,
544                  &(struct ibv_wq_attr){
545                         .attr_mask = IBV_WQ_ATTR_STATE,
546                         .wq_state = IBV_WQS_RDY,
547                  });
548         if (ret) {
549                 msg = "WQ state change to IBV_WQS_RDY failed";
550                 goto error;
551         }
552         /* Retrieve device queue information. */
553         mlxdv.cq.in = cq;
554         mlxdv.cq.out = &dv_cq;
555         mlxdv.rwq.in = wq;
556         mlxdv.rwq.out = &dv_rwq;
557         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_RWQ | MLX4DV_OBJ_CQ);
558         if (ret) {
559                 msg = "failed to obtain device information from WQ/CQ objects";
560                 goto error;
561         }
562         wqes = (volatile struct mlx4_wqe_data_seg (*)[])
563                 ((uintptr_t)dv_rwq.buf.buf + dv_rwq.rq.offset);
564         for (i = 0; i != RTE_DIM(*elts); ++i) {
565                 volatile struct mlx4_wqe_data_seg *scat = &(*wqes)[i];
566                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
567
568                 if (buf == NULL) {
569                         while (i--) {
570                                 rte_pktmbuf_free_seg((*elts)[i]);
571                                 (*elts)[i] = NULL;
572                         }
573                         ret = ENOMEM;
574                         msg = "cannot allocate mbuf";
575                         goto error;
576                 }
577                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
578                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
579                 /* Buffer is supposed to be empty. */
580                 assert(rte_pktmbuf_data_len(buf) == 0);
581                 assert(rte_pktmbuf_pkt_len(buf) == 0);
582                 /* Only the first segment keeps headroom. */
583                 if (i % sges_n)
584                         buf->data_off = 0;
585                 buf->port = rxq->port_id;
586                 buf->data_len = rte_pktmbuf_tailroom(buf);
587                 buf->pkt_len = rte_pktmbuf_tailroom(buf);
588                 buf->nb_segs = 1;
589                 *scat = (struct mlx4_wqe_data_seg){
590                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
591                                                                   uintptr_t)),
592                         .byte_count = rte_cpu_to_be_32(buf->data_len),
593                         .lkey = rte_cpu_to_be_32(rxq->mr->lkey),
594                 };
595                 (*elts)[i] = buf;
596         }
597         DEBUG("%p: allocated and configured %u segments (max %u packets)",
598               (void *)rxq, elts_n, elts_n / sges_n);
599         rxq->cq = cq;
600         rxq->wq = wq;
601         rxq->wqes = wqes;
602         rxq->rq_db = dv_rwq.rdb;
603         rxq->mcq.buf = dv_cq.buf.buf;
604         rxq->mcq.cqe_cnt = dv_cq.cqe_cnt;
605         rxq->mcq.set_ci_db = dv_cq.set_ci_db;
606         rxq->mcq.cqe_64 = (dv_cq.cqe_size & 64) ? 1 : 0;
607         rxq->mcq.arm_db = dv_cq.arm_db;
608         rxq->mcq.arm_sn = dv_cq.arm_sn;
609         rxq->mcq.cqn = dv_cq.cqn;
610         rxq->mcq.cq_uar = dv_cq.cq_uar;
611         rxq->mcq.cq_db_reg = (uint8_t *)dv_cq.cq_uar + MLX4_CQ_DOORBELL;
612         /* Update doorbell counter. */
613         rxq->rq_ci = elts_n / sges_n;
614         rte_wmb();
615         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
616         return 0;
617 error:
618         if (wq)
619                 claim_zero(ibv_destroy_wq(wq));
620         if (cq)
621                 claim_zero(ibv_destroy_cq(cq));
622         rte_errno = ret;
623         ERROR("error while attaching Rx queue %p: %s: %s",
624               (void *)rxq, msg, strerror(ret));
625         return -ret;
626 }
627
628 /**
629  * Detach a user from a Rx queue.
630  *
631  * This function decrements the usage count of the Rx queue and destroys
632  * usage resources after reaching 0.
633  *
634  * @param rxq
635  *   Pointer to Rx queue structure.
636  */
637 void
638 mlx4_rxq_detach(struct rxq *rxq)
639 {
640         unsigned int i;
641         struct rte_mbuf *(*elts)[1 << rxq->elts_n] = rxq->elts;
642
643         if (--rxq->usecnt)
644                 return;
645         rxq->rq_ci = 0;
646         memset(&rxq->mcq, 0, sizeof(rxq->mcq));
647         rxq->rq_db = NULL;
648         rxq->wqes = NULL;
649         claim_zero(ibv_destroy_wq(rxq->wq));
650         rxq->wq = NULL;
651         claim_zero(ibv_destroy_cq(rxq->cq));
652         rxq->cq = NULL;
653         DEBUG("%p: freeing Rx queue elements", (void *)rxq);
654         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
655                 if (!(*elts)[i])
656                         continue;
657                 rte_pktmbuf_free_seg((*elts)[i]);
658                 (*elts)[i] = NULL;
659         }
660 }
661
662 /**
663  * DPDK callback to configure a Rx queue.
664  *
665  * @param dev
666  *   Pointer to Ethernet device structure.
667  * @param idx
668  *   Rx queue index.
669  * @param desc
670  *   Number of descriptors to configure in queue.
671  * @param socket
672  *   NUMA socket on which memory must be allocated.
673  * @param[in] conf
674  *   Thresholds parameters.
675  * @param mp
676  *   Memory pool for buffer allocations.
677  *
678  * @return
679  *   0 on success, negative errno value otherwise and rte_errno is set.
680  */
681 int
682 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
683                     unsigned int socket, const struct rte_eth_rxconf *conf,
684                     struct rte_mempool *mp)
685 {
686         struct priv *priv = dev->data->dev_private;
687         uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
688         struct rte_mbuf *(*elts)[rte_align32pow2(desc)];
689         struct rxq *rxq;
690         struct mlx4_malloc_vec vec[] = {
691                 {
692                         .align = RTE_CACHE_LINE_SIZE,
693                         .size = sizeof(*rxq),
694                         .addr = (void **)&rxq,
695                 },
696                 {
697                         .align = RTE_CACHE_LINE_SIZE,
698                         .size = sizeof(*elts),
699                         .addr = (void **)&elts,
700                 },
701         };
702         int ret;
703
704         (void)conf; /* Thresholds configuration (ignored). */
705         DEBUG("%p: configuring queue %u for %u descriptors",
706               (void *)dev, idx, desc);
707         if (idx >= dev->data->nb_rx_queues) {
708                 rte_errno = EOVERFLOW;
709                 ERROR("%p: queue index out of range (%u >= %u)",
710                       (void *)dev, idx, dev->data->nb_rx_queues);
711                 return -rte_errno;
712         }
713         rxq = dev->data->rx_queues[idx];
714         if (rxq) {
715                 rte_errno = EEXIST;
716                 ERROR("%p: Rx queue %u already configured, release it first",
717                       (void *)dev, idx);
718                 return -rte_errno;
719         }
720         if (!desc) {
721                 rte_errno = EINVAL;
722                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
723                 return -rte_errno;
724         }
725         if (desc != RTE_DIM(*elts)) {
726                 desc = RTE_DIM(*elts);
727                 WARN("%p: increased number of descriptors in Rx queue %u"
728                      " to the next power of two (%u)",
729                      (void *)dev, idx, desc);
730         }
731         /* Allocate and initialize Rx queue. */
732         mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
733         if (!rxq) {
734                 ERROR("%p: unable to allocate queue index %u",
735                       (void *)dev, idx);
736                 return -rte_errno;
737         }
738         *rxq = (struct rxq){
739                 .priv = priv,
740                 .mp = mp,
741                 .port_id = dev->data->port_id,
742                 .sges_n = 0,
743                 .elts_n = rte_log2_u32(desc),
744                 .elts = elts,
745                 /* Toggle Rx checksum offload if hardware supports it. */
746                 .csum = (priv->hw_csum &&
747                          dev->data->dev_conf.rxmode.hw_ip_checksum),
748                 .csum_l2tun = (priv->hw_csum_l2tun &&
749                                dev->data->dev_conf.rxmode.hw_ip_checksum),
750                 .stats = {
751                         .idx = idx,
752                 },
753                 .socket = socket,
754         };
755         /* Enable scattered packets support for this queue if necessary. */
756         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
757         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
758             (mb_len - RTE_PKTMBUF_HEADROOM)) {
759                 ;
760         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
761                 uint32_t size =
762                         RTE_PKTMBUF_HEADROOM +
763                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
764                 uint32_t sges_n;
765
766                 /*
767                  * Determine the number of SGEs needed for a full packet
768                  * and round it to the next power of two.
769                  */
770                 sges_n = rte_log2_u32((size / mb_len) + !!(size % mb_len));
771                 rxq->sges_n = sges_n;
772                 /* Make sure sges_n did not overflow. */
773                 size = mb_len * (1 << rxq->sges_n);
774                 size -= RTE_PKTMBUF_HEADROOM;
775                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
776                         rte_errno = EOVERFLOW;
777                         ERROR("%p: too many SGEs (%u) needed to handle"
778                               " requested maximum packet size %u",
779                               (void *)dev,
780                               1 << sges_n,
781                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
782                         goto error;
783                 }
784         } else {
785                 WARN("%p: the requested maximum Rx packet size (%u) is"
786                      " larger than a single mbuf (%u) and scattered"
787                      " mode has not been requested",
788                      (void *)dev,
789                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
790                      mb_len - RTE_PKTMBUF_HEADROOM);
791         }
792         DEBUG("%p: maximum number of segments per packet: %u",
793               (void *)dev, 1 << rxq->sges_n);
794         if (desc % (1 << rxq->sges_n)) {
795                 rte_errno = EINVAL;
796                 ERROR("%p: number of Rx queue descriptors (%u) is not a"
797                       " multiple of maximum segments per packet (%u)",
798                       (void *)dev,
799                       desc,
800                       1 << rxq->sges_n);
801                 goto error;
802         }
803         /* Use the entire Rx mempool as the memory region. */
804         rxq->mr = mlx4_mp2mr(priv->pd, mp);
805         if (!rxq->mr) {
806                 rte_errno = EINVAL;
807                 ERROR("%p: MR creation failure: %s",
808                       (void *)dev, strerror(rte_errno));
809                 goto error;
810         }
811         if (dev->data->dev_conf.intr_conf.rxq) {
812                 rxq->channel = ibv_create_comp_channel(priv->ctx);
813                 if (rxq->channel == NULL) {
814                         rte_errno = ENOMEM;
815                         ERROR("%p: Rx interrupt completion channel creation"
816                               " failure: %s",
817                               (void *)dev, strerror(rte_errno));
818                         goto error;
819                 }
820                 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
821                         ERROR("%p: unable to make Rx interrupt completion"
822                               " channel non-blocking: %s",
823                               (void *)dev, strerror(rte_errno));
824                         goto error;
825                 }
826         }
827         DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
828         dev->data->rx_queues[idx] = rxq;
829         return 0;
830 error:
831         dev->data->rx_queues[idx] = NULL;
832         ret = rte_errno;
833         mlx4_rx_queue_release(rxq);
834         rte_errno = ret;
835         assert(rte_errno > 0);
836         return -rte_errno;
837 }
838
839 /**
840  * DPDK callback to release a Rx queue.
841  *
842  * @param dpdk_rxq
843  *   Generic Rx queue pointer.
844  */
845 void
846 mlx4_rx_queue_release(void *dpdk_rxq)
847 {
848         struct rxq *rxq = (struct rxq *)dpdk_rxq;
849         struct priv *priv;
850         unsigned int i;
851
852         if (rxq == NULL)
853                 return;
854         priv = rxq->priv;
855         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
856                 if (priv->dev->data->rx_queues[i] == rxq) {
857                         DEBUG("%p: removing Rx queue %p from list",
858                               (void *)priv->dev, (void *)rxq);
859                         priv->dev->data->rx_queues[i] = NULL;
860                         break;
861                 }
862         assert(!rxq->cq);
863         assert(!rxq->wq);
864         assert(!rxq->wqes);
865         assert(!rxq->rq_db);
866         if (rxq->channel)
867                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
868         if (rxq->mr)
869                 claim_zero(ibv_dereg_mr(rxq->mr));
870         rte_free(rxq);
871 }