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