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