New upstream version 17.11.3
[deb_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         if (priv->rss_init)
367                 return 0;
368         /* Prepare range for RSS contexts before creating the first WQ. */
369         ret = mlx4dv_set_context_attr(priv->ctx,
370                                       MLX4DV_SET_CTX_ATTR_LOG_WQS_RANGE_SZ,
371                                       &log2_range);
372         if (ret) {
373                 ERROR("cannot set up range size for RSS context to %u"
374                       " (for %u Rx queues), error: %s",
375                       1 << log2_range, dev->data->nb_rx_queues, strerror(ret));
376                 rte_errno = ret;
377                 return -ret;
378         }
379         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
380                 struct rxq *rxq = priv->dev->data->rx_queues[i];
381                 struct ibv_cq *cq;
382                 struct ibv_wq *wq;
383                 uint32_t wq_num;
384
385                 /* Attach the configured Rx queues. */
386                 if (rxq) {
387                         assert(!rxq->usecnt);
388                         ret = mlx4_rxq_attach(rxq);
389                         if (!ret) {
390                                 wq_num = rxq->wq->wq_num;
391                                 goto wq_num_check;
392                         }
393                         ret = -ret;
394                         msg = "unable to create Rx queue resources";
395                         goto error;
396                 }
397                 /*
398                  * WQs are temporarily allocated for unconfigured Rx queues
399                  * to maintain proper index alignment in indirection table
400                  * by skipping unused WQ numbers.
401                  *
402                  * The reason this works at all even though these WQs are
403                  * immediately destroyed is that WQNs are allocated
404                  * sequentially and are guaranteed to never be reused in the
405                  * same context by the underlying implementation.
406                  */
407                 cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
408                 if (!cq) {
409                         ret = ENOMEM;
410                         msg = "placeholder CQ creation failure";
411                         goto error;
412                 }
413                 wq = ibv_create_wq
414                         (priv->ctx,
415                          &(struct ibv_wq_init_attr){
416                                 .wq_type = IBV_WQT_RQ,
417                                 .max_wr = 1,
418                                 .max_sge = 1,
419                                 .pd = priv->pd,
420                                 .cq = cq,
421                          });
422                 if (wq) {
423                         wq_num = wq->wq_num;
424                         claim_zero(ibv_destroy_wq(wq));
425                 } else {
426                         wq_num = 0; /* Shut up GCC 4.8 warnings. */
427                 }
428                 claim_zero(ibv_destroy_cq(cq));
429                 if (!wq) {
430                         ret = ENOMEM;
431                         msg = "placeholder WQ creation failure";
432                         goto error;
433                 }
434 wq_num_check:
435                 /*
436                  * While guaranteed by the implementation, make sure WQ
437                  * numbers are really sequential (as the saying goes,
438                  * trust, but verify).
439                  */
440                 if (i && wq_num - wq_num_prev != 1) {
441                         if (rxq)
442                                 mlx4_rxq_detach(rxq);
443                         ret = ERANGE;
444                         msg = "WQ numbers are not sequential";
445                         goto error;
446                 }
447                 wq_num_prev = wq_num;
448         }
449         priv->rss_init = 1;
450         return 0;
451 error:
452         ERROR("cannot initialize common RSS resources (queue %u): %s: %s",
453               i, msg, strerror(ret));
454         while (i--) {
455                 struct rxq *rxq = priv->dev->data->rx_queues[i];
456
457                 if (rxq)
458                         mlx4_rxq_detach(rxq);
459         }
460         rte_errno = ret;
461         return -ret;
462 }
463
464 /**
465  * Release common RSS context resources.
466  *
467  * As the reverse of mlx4_rss_init(), this must be done after removing all
468  * flow rules relying on indirection tables.
469  *
470  * @param priv
471  *   Pointer to private structure.
472  */
473 void
474 mlx4_rss_deinit(struct priv *priv)
475 {
476         unsigned int i;
477
478         if (!priv->rss_init)
479                 return;
480         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
481                 struct rxq *rxq = priv->dev->data->rx_queues[i];
482
483                 if (rxq) {
484                         assert(rxq->usecnt == 1);
485                         mlx4_rxq_detach(rxq);
486                 }
487         }
488         priv->rss_init = 0;
489 }
490
491 /**
492  * Attach a user to a Rx queue.
493  *
494  * Used when the resources of an Rx queue must be instantiated for it to
495  * become in a usable state.
496  *
497  * This function increments the usage count of the Rx queue.
498  *
499  * @param rxq
500  *   Pointer to Rx queue structure.
501  *
502  * @return
503  *   0 on success, negative errno value otherwise and rte_errno is set.
504  */
505 int
506 mlx4_rxq_attach(struct rxq *rxq)
507 {
508         if (rxq->usecnt++) {
509                 assert(rxq->cq);
510                 assert(rxq->wq);
511                 assert(rxq->wqes);
512                 assert(rxq->rq_db);
513                 return 0;
514         }
515
516         struct priv *priv = rxq->priv;
517         const uint32_t elts_n = 1 << rxq->elts_n;
518         const uint32_t sges_n = 1 << rxq->sges_n;
519         struct rte_mbuf *(*elts)[elts_n] = rxq->elts;
520         struct mlx4dv_obj mlxdv;
521         struct mlx4dv_rwq dv_rwq;
522         struct mlx4dv_cq dv_cq = { .comp_mask = MLX4DV_CQ_MASK_UAR, };
523         const char *msg;
524         struct ibv_cq *cq = NULL;
525         struct ibv_wq *wq = NULL;
526         volatile struct mlx4_wqe_data_seg (*wqes)[];
527         unsigned int i;
528         int ret;
529
530         assert(rte_is_power_of_2(elts_n));
531         cq = ibv_create_cq(priv->ctx, elts_n / sges_n, NULL, rxq->channel, 0);
532         if (!cq) {
533                 ret = ENOMEM;
534                 msg = "CQ creation failure";
535                 goto error;
536         }
537         wq = ibv_create_wq
538                 (priv->ctx,
539                  &(struct ibv_wq_init_attr){
540                         .wq_type = IBV_WQT_RQ,
541                         .max_wr = elts_n / sges_n,
542                         .max_sge = sges_n,
543                         .pd = priv->pd,
544                         .cq = cq,
545                  });
546         if (!wq) {
547                 ret = errno ? errno : EINVAL;
548                 msg = "WQ creation failure";
549                 goto error;
550         }
551         ret = ibv_modify_wq
552                 (wq,
553                  &(struct ibv_wq_attr){
554                         .attr_mask = IBV_WQ_ATTR_STATE,
555                         .wq_state = IBV_WQS_RDY,
556                  });
557         if (ret) {
558                 msg = "WQ state change to IBV_WQS_RDY failed";
559                 goto error;
560         }
561         /* Retrieve device queue information. */
562         mlxdv.cq.in = cq;
563         mlxdv.cq.out = &dv_cq;
564         mlxdv.rwq.in = wq;
565         mlxdv.rwq.out = &dv_rwq;
566         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_RWQ | MLX4DV_OBJ_CQ);
567         if (ret) {
568                 msg = "failed to obtain device information from WQ/CQ objects";
569                 goto error;
570         }
571         wqes = (volatile struct mlx4_wqe_data_seg (*)[])
572                 ((uintptr_t)dv_rwq.buf.buf + dv_rwq.rq.offset);
573         for (i = 0; i != RTE_DIM(*elts); ++i) {
574                 volatile struct mlx4_wqe_data_seg *scat = &(*wqes)[i];
575                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
576
577                 if (buf == NULL) {
578                         while (i--) {
579                                 rte_pktmbuf_free_seg((*elts)[i]);
580                                 (*elts)[i] = NULL;
581                         }
582                         ret = ENOMEM;
583                         msg = "cannot allocate mbuf";
584                         goto error;
585                 }
586                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
587                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
588                 /* Buffer is supposed to be empty. */
589                 assert(rte_pktmbuf_data_len(buf) == 0);
590                 assert(rte_pktmbuf_pkt_len(buf) == 0);
591                 /* Only the first segment keeps headroom. */
592                 if (i % sges_n)
593                         buf->data_off = 0;
594                 buf->port = rxq->port_id;
595                 buf->data_len = rte_pktmbuf_tailroom(buf);
596                 buf->pkt_len = rte_pktmbuf_tailroom(buf);
597                 buf->nb_segs = 1;
598                 *scat = (struct mlx4_wqe_data_seg){
599                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
600                                                                   uintptr_t)),
601                         .byte_count = rte_cpu_to_be_32(buf->data_len),
602                         .lkey = rte_cpu_to_be_32(rxq->mr->lkey),
603                 };
604                 (*elts)[i] = buf;
605         }
606         DEBUG("%p: allocated and configured %u segments (max %u packets)",
607               (void *)rxq, elts_n, elts_n / sges_n);
608         rxq->cq = cq;
609         rxq->wq = wq;
610         rxq->wqes = wqes;
611         rxq->rq_db = dv_rwq.rdb;
612         rxq->mcq.buf = dv_cq.buf.buf;
613         rxq->mcq.cqe_cnt = dv_cq.cqe_cnt;
614         rxq->mcq.set_ci_db = dv_cq.set_ci_db;
615         rxq->mcq.cqe_64 = (dv_cq.cqe_size & 64) ? 1 : 0;
616         rxq->mcq.arm_db = dv_cq.arm_db;
617         rxq->mcq.arm_sn = dv_cq.arm_sn;
618         rxq->mcq.cqn = dv_cq.cqn;
619         rxq->mcq.cq_uar = dv_cq.cq_uar;
620         rxq->mcq.cq_db_reg = (uint8_t *)dv_cq.cq_uar + MLX4_CQ_DOORBELL;
621         /* Update doorbell counter. */
622         rxq->rq_ci = elts_n / sges_n;
623         rte_wmb();
624         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
625         return 0;
626 error:
627         if (wq)
628                 claim_zero(ibv_destroy_wq(wq));
629         if (cq)
630                 claim_zero(ibv_destroy_cq(cq));
631         --rxq->usecnt;
632         rte_errno = ret;
633         ERROR("error while attaching Rx queue %p: %s: %s",
634               (void *)rxq, msg, strerror(ret));
635         return -ret;
636 }
637
638 /**
639  * Detach a user from a Rx queue.
640  *
641  * This function decrements the usage count of the Rx queue and destroys
642  * usage resources after reaching 0.
643  *
644  * @param rxq
645  *   Pointer to Rx queue structure.
646  */
647 void
648 mlx4_rxq_detach(struct rxq *rxq)
649 {
650         unsigned int i;
651         struct rte_mbuf *(*elts)[1 << rxq->elts_n] = rxq->elts;
652
653         if (--rxq->usecnt)
654                 return;
655         rxq->rq_ci = 0;
656         memset(&rxq->mcq, 0, sizeof(rxq->mcq));
657         rxq->rq_db = NULL;
658         rxq->wqes = NULL;
659         claim_zero(ibv_destroy_wq(rxq->wq));
660         rxq->wq = NULL;
661         claim_zero(ibv_destroy_cq(rxq->cq));
662         rxq->cq = NULL;
663         DEBUG("%p: freeing Rx queue elements", (void *)rxq);
664         for (i = 0; (i != RTE_DIM(*elts)); ++i) {
665                 if (!(*elts)[i])
666                         continue;
667                 rte_pktmbuf_free_seg((*elts)[i]);
668                 (*elts)[i] = NULL;
669         }
670 }
671
672 /**
673  * DPDK callback to configure a Rx queue.
674  *
675  * @param dev
676  *   Pointer to Ethernet device structure.
677  * @param idx
678  *   Rx queue index.
679  * @param desc
680  *   Number of descriptors to configure in queue.
681  * @param socket
682  *   NUMA socket on which memory must be allocated.
683  * @param[in] conf
684  *   Thresholds parameters.
685  * @param mp
686  *   Memory pool for buffer allocations.
687  *
688  * @return
689  *   0 on success, negative errno value otherwise and rte_errno is set.
690  */
691 int
692 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
693                     unsigned int socket, const struct rte_eth_rxconf *conf,
694                     struct rte_mempool *mp)
695 {
696         struct priv *priv = dev->data->dev_private;
697         uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
698         struct rte_mbuf *(*elts)[rte_align32pow2(desc)];
699         struct rxq *rxq;
700         struct mlx4_malloc_vec vec[] = {
701                 {
702                         .align = RTE_CACHE_LINE_SIZE,
703                         .size = sizeof(*rxq),
704                         .addr = (void **)&rxq,
705                 },
706                 {
707                         .align = RTE_CACHE_LINE_SIZE,
708                         .size = sizeof(*elts),
709                         .addr = (void **)&elts,
710                 },
711         };
712         int ret;
713
714         (void)conf; /* Thresholds configuration (ignored). */
715         DEBUG("%p: configuring queue %u for %u descriptors",
716               (void *)dev, idx, desc);
717         if (idx >= dev->data->nb_rx_queues) {
718                 rte_errno = EOVERFLOW;
719                 ERROR("%p: queue index out of range (%u >= %u)",
720                       (void *)dev, idx, dev->data->nb_rx_queues);
721                 return -rte_errno;
722         }
723         rxq = dev->data->rx_queues[idx];
724         if (rxq) {
725                 rte_errno = EEXIST;
726                 ERROR("%p: Rx queue %u already configured, release it first",
727                       (void *)dev, idx);
728                 return -rte_errno;
729         }
730         if (!desc) {
731                 rte_errno = EINVAL;
732                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
733                 return -rte_errno;
734         }
735         if (desc != RTE_DIM(*elts)) {
736                 desc = RTE_DIM(*elts);
737                 WARN("%p: increased number of descriptors in Rx queue %u"
738                      " to the next power of two (%u)",
739                      (void *)dev, idx, desc);
740         }
741         /* Allocate and initialize Rx queue. */
742         mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
743         if (!rxq) {
744                 ERROR("%p: unable to allocate queue index %u",
745                       (void *)dev, idx);
746                 return -rte_errno;
747         }
748         *rxq = (struct rxq){
749                 .priv = priv,
750                 .mp = mp,
751                 .port_id = dev->data->port_id,
752                 .sges_n = 0,
753                 .elts_n = rte_log2_u32(desc),
754                 .elts = elts,
755                 /* Toggle Rx checksum offload if hardware supports it. */
756                 .csum = (priv->hw_csum &&
757                          dev->data->dev_conf.rxmode.hw_ip_checksum),
758                 .csum_l2tun = (priv->hw_csum_l2tun &&
759                                dev->data->dev_conf.rxmode.hw_ip_checksum),
760                 .l2tun_offload = priv->hw_csum_l2tun,
761                 .stats = {
762                         .idx = idx,
763                 },
764                 .socket = socket,
765         };
766         /* Enable scattered packets support for this queue if necessary. */
767         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
768         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
769             (mb_len - RTE_PKTMBUF_HEADROOM)) {
770                 ;
771         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
772                 uint32_t size =
773                         RTE_PKTMBUF_HEADROOM +
774                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
775                 uint32_t sges_n;
776
777                 /*
778                  * Determine the number of SGEs needed for a full packet
779                  * and round it to the next power of two.
780                  */
781                 sges_n = rte_log2_u32((size / mb_len) + !!(size % mb_len));
782                 rxq->sges_n = sges_n;
783                 /* Make sure sges_n did not overflow. */
784                 size = mb_len * (1 << rxq->sges_n);
785                 size -= RTE_PKTMBUF_HEADROOM;
786                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
787                         rte_errno = EOVERFLOW;
788                         ERROR("%p: too many SGEs (%u) needed to handle"
789                               " requested maximum packet size %u",
790                               (void *)dev,
791                               1 << sges_n,
792                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
793                         goto error;
794                 }
795         } else {
796                 WARN("%p: the requested maximum Rx packet size (%u) is"
797                      " larger than a single mbuf (%u) and scattered"
798                      " mode has not been requested",
799                      (void *)dev,
800                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
801                      mb_len - RTE_PKTMBUF_HEADROOM);
802         }
803         DEBUG("%p: maximum number of segments per packet: %u",
804               (void *)dev, 1 << rxq->sges_n);
805         if (desc % (1 << rxq->sges_n)) {
806                 rte_errno = EINVAL;
807                 ERROR("%p: number of Rx queue descriptors (%u) is not a"
808                       " multiple of maximum segments per packet (%u)",
809                       (void *)dev,
810                       desc,
811                       1 << rxq->sges_n);
812                 goto error;
813         }
814         /* Use the entire Rx mempool as the memory region. */
815         rxq->mr = mlx4_mr_get(priv, mp);
816         if (!rxq->mr) {
817                 ERROR("%p: MR creation failure: %s",
818                       (void *)dev, strerror(rte_errno));
819                 goto error;
820         }
821         if (dev->data->dev_conf.intr_conf.rxq) {
822                 rxq->channel = ibv_create_comp_channel(priv->ctx);
823                 if (rxq->channel == NULL) {
824                         rte_errno = ENOMEM;
825                         ERROR("%p: Rx interrupt completion channel creation"
826                               " failure: %s",
827                               (void *)dev, strerror(rte_errno));
828                         goto error;
829                 }
830                 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
831                         ERROR("%p: unable to make Rx interrupt completion"
832                               " channel non-blocking: %s",
833                               (void *)dev, strerror(rte_errno));
834                         goto error;
835                 }
836         }
837         DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
838         dev->data->rx_queues[idx] = rxq;
839         return 0;
840 error:
841         dev->data->rx_queues[idx] = NULL;
842         ret = rte_errno;
843         mlx4_rx_queue_release(rxq);
844         rte_errno = ret;
845         assert(rte_errno > 0);
846         return -rte_errno;
847 }
848
849 /**
850  * DPDK callback to release a Rx queue.
851  *
852  * @param dpdk_rxq
853  *   Generic Rx queue pointer.
854  */
855 void
856 mlx4_rx_queue_release(void *dpdk_rxq)
857 {
858         struct rxq *rxq = (struct rxq *)dpdk_rxq;
859         struct priv *priv;
860         unsigned int i;
861
862         if (rxq == NULL)
863                 return;
864         priv = rxq->priv;
865         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
866                 if (priv->dev->data->rx_queues[i] == rxq) {
867                         DEBUG("%p: removing Rx queue %p from list",
868                               (void *)priv->dev, (void *)rxq);
869                         priv->dev->data->rx_queues[i] = NULL;
870                         break;
871                 }
872         assert(!rxq->cq);
873         assert(!rxq->wq);
874         assert(!rxq->wqes);
875         assert(!rxq->rq_db);
876         if (rxq->channel)
877                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
878         if (rxq->mr)
879                 mlx4_mr_put(rxq->mr);
880         rte_free(rxq);
881 }