net/mlx5: fix segfault on interrupt disable
[dpdk.git] / drivers / net / mlx5 / mlx5_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <fcntl.h>
40 #include <sys/queue.h>
41
42 /* Verbs header. */
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #include <infiniband/mlx5dv.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_mbuf.h>
54 #include <rte_malloc.h>
55 #include <rte_ethdev.h>
56 #include <rte_common.h>
57 #include <rte_interrupts.h>
58 #include <rte_debug.h>
59 #include <rte_io.h>
60
61 #include "mlx5.h"
62 #include "mlx5_rxtx.h"
63 #include "mlx5_utils.h"
64 #include "mlx5_autoconf.h"
65 #include "mlx5_defs.h"
66
67 /* Default RSS hash key also used for ConnectX-3. */
68 uint8_t rss_hash_default_key[] = {
69         0x2c, 0xc6, 0x81, 0xd1,
70         0x5b, 0xdb, 0xf4, 0xf7,
71         0xfc, 0xa2, 0x83, 0x19,
72         0xdb, 0x1a, 0x3e, 0x94,
73         0x6b, 0x9e, 0x38, 0xd9,
74         0x2c, 0x9c, 0x03, 0xd1,
75         0xad, 0x99, 0x44, 0xa7,
76         0xd9, 0x56, 0x3d, 0x59,
77         0x06, 0x3c, 0x25, 0xf3,
78         0xfc, 0x1f, 0xdc, 0x2a,
79 };
80
81 /* Length of the default RSS hash key. */
82 const size_t rss_hash_default_key_len = sizeof(rss_hash_default_key);
83
84 /**
85  * Allocate RX queue elements.
86  *
87  * @param rxq_ctrl
88  *   Pointer to RX queue structure.
89  *
90  * @return
91  *   0 on success, errno value on failure.
92  */
93 int
94 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
95 {
96         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
97         unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
98         unsigned int i;
99         int ret = 0;
100
101         /* Iterate on segments. */
102         for (i = 0; (i != elts_n); ++i) {
103                 struct rte_mbuf *buf;
104
105                 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
106                 if (buf == NULL) {
107                         ERROR("%p: empty mbuf pool", (void *)rxq_ctrl);
108                         ret = ENOMEM;
109                         goto error;
110                 }
111                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
112                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
113                 /* Buffer is supposed to be empty. */
114                 assert(rte_pktmbuf_data_len(buf) == 0);
115                 assert(rte_pktmbuf_pkt_len(buf) == 0);
116                 assert(!buf->next);
117                 /* Only the first segment keeps headroom. */
118                 if (i % sges_n)
119                         SET_DATA_OFF(buf, 0);
120                 PORT(buf) = rxq_ctrl->rxq.port_id;
121                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
122                 PKT_LEN(buf) = DATA_LEN(buf);
123                 NB_SEGS(buf) = 1;
124                 (*rxq_ctrl->rxq.elts)[i] = buf;
125         }
126         /* If Rx vector is activated. */
127         if (rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
128                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
129                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
130                 int j;
131
132                 /* Initialize default rearm_data for vPMD. */
133                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
134                 rte_mbuf_refcnt_set(mbuf_init, 1);
135                 mbuf_init->nb_segs = 1;
136                 mbuf_init->port = rxq->port_id;
137                 /*
138                  * prevent compiler reordering:
139                  * rearm_data covers previous fields.
140                  */
141                 rte_compiler_barrier();
142                 rxq->mbuf_initializer =
143                         *(uint64_t *)&mbuf_init->rearm_data;
144                 /* Padding with a fake mbuf for vectorized Rx. */
145                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
146                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
147         }
148         DEBUG("%p: allocated and configured %u segments (max %u packets)",
149               (void *)rxq_ctrl, elts_n, elts_n / (1 << rxq_ctrl->rxq.sges_n));
150         assert(ret == 0);
151         return 0;
152 error:
153         elts_n = i;
154         for (i = 0; (i != elts_n); ++i) {
155                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
156                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
157                 (*rxq_ctrl->rxq.elts)[i] = NULL;
158         }
159         DEBUG("%p: failed, freed everything", (void *)rxq_ctrl);
160         assert(ret > 0);
161         return ret;
162 }
163
164 /**
165  * Free RX queue elements.
166  *
167  * @param rxq_ctrl
168  *   Pointer to RX queue structure.
169  */
170 static void
171 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
172 {
173         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
174         const uint16_t q_n = (1 << rxq->elts_n);
175         const uint16_t q_mask = q_n - 1;
176         uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
177         uint16_t i;
178
179         DEBUG("%p: freeing WRs", (void *)rxq_ctrl);
180         if (rxq->elts == NULL)
181                 return;
182         /**
183          * Some mbuf in the Ring belongs to the application.  They cannot be
184          * freed.
185          */
186         if (rxq_check_vec_support(rxq) > 0) {
187                 for (i = 0; i < used; ++i)
188                         (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
189                 rxq->rq_pi = rxq->rq_ci;
190         }
191         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
192                 if ((*rxq->elts)[i] != NULL)
193                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
194                 (*rxq->elts)[i] = NULL;
195         }
196 }
197
198 /**
199  * Clean up a RX queue.
200  *
201  * Destroy objects, free allocated memory and reset the structure for reuse.
202  *
203  * @param rxq_ctrl
204  *   Pointer to RX queue structure.
205  */
206 void
207 mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *rxq_ctrl)
208 {
209         DEBUG("cleaning up %p", (void *)rxq_ctrl);
210         if (rxq_ctrl->ibv)
211                 mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
212         memset(rxq_ctrl, 0, sizeof(*rxq_ctrl));
213 }
214
215 /**
216  *
217  * @param dev
218  *   Pointer to Ethernet device structure.
219  * @param idx
220  *   RX queue index.
221  * @param desc
222  *   Number of descriptors to configure in queue.
223  * @param socket
224  *   NUMA socket on which memory must be allocated.
225  * @param[in] conf
226  *   Thresholds parameters.
227  * @param mp
228  *   Memory pool for buffer allocations.
229  *
230  * @return
231  *   0 on success, negative errno value on failure.
232  */
233 int
234 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
235                     unsigned int socket, const struct rte_eth_rxconf *conf,
236                     struct rte_mempool *mp)
237 {
238         struct priv *priv = dev->data->dev_private;
239         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
240         struct mlx5_rxq_ctrl *rxq_ctrl =
241                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
242         int ret = 0;
243
244         (void)conf;
245         if (mlx5_is_secondary())
246                 return -E_RTE_SECONDARY;
247         priv_lock(priv);
248         if (!rte_is_power_of_2(desc)) {
249                 desc = 1 << log2above(desc);
250                 WARN("%p: increased number of descriptors in RX queue %u"
251                      " to the next power of two (%d)",
252                      (void *)dev, idx, desc);
253         }
254         DEBUG("%p: configuring queue %u for %u descriptors",
255               (void *)dev, idx, desc);
256         if (idx >= priv->rxqs_n) {
257                 ERROR("%p: queue index out of range (%u >= %u)",
258                       (void *)dev, idx, priv->rxqs_n);
259                 priv_unlock(priv);
260                 return -EOVERFLOW;
261         }
262         if (!mlx5_priv_rxq_releasable(priv, idx)) {
263                 ret = EBUSY;
264                 ERROR("%p: unable to release queue index %u",
265                       (void *)dev, idx);
266                 goto out;
267         }
268         mlx5_priv_rxq_release(priv, idx);
269         rxq_ctrl = mlx5_priv_rxq_new(priv, idx, desc, socket, mp);
270         if (!rxq_ctrl) {
271                 ERROR("%p: unable to allocate queue index %u",
272                       (void *)dev, idx);
273                 ret = ENOMEM;
274                 goto out;
275         }
276         DEBUG("%p: adding RX queue %p to list",
277               (void *)dev, (void *)rxq_ctrl);
278         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
279 out:
280         priv_unlock(priv);
281         return -ret;
282 }
283
284 /**
285  * DPDK callback to release a RX queue.
286  *
287  * @param dpdk_rxq
288  *   Generic RX queue pointer.
289  */
290 void
291 mlx5_rx_queue_release(void *dpdk_rxq)
292 {
293         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
294         struct mlx5_rxq_ctrl *rxq_ctrl;
295         struct priv *priv;
296
297         if (mlx5_is_secondary())
298                 return;
299
300         if (rxq == NULL)
301                 return;
302         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
303         priv = rxq_ctrl->priv;
304         priv_lock(priv);
305         if (!mlx5_priv_rxq_releasable(priv, rxq_ctrl->rxq.stats.idx))
306                 rte_panic("Rx queue %p is still used by a flow and cannot be"
307                           " removed\n", (void *)rxq_ctrl);
308         mlx5_priv_rxq_release(priv, rxq_ctrl->rxq.stats.idx);
309         priv_unlock(priv);
310 }
311
312 /**
313  * Allocate queue vector and fill epoll fd list for Rx interrupts.
314  *
315  * @param priv
316  *   Pointer to private structure.
317  *
318  * @return
319  *   0 on success, negative on failure.
320  */
321 int
322 priv_rx_intr_vec_enable(struct priv *priv)
323 {
324         unsigned int i;
325         unsigned int rxqs_n = priv->rxqs_n;
326         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
327         unsigned int count = 0;
328         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
329
330         assert(!mlx5_is_secondary());
331         if (!priv->dev->data->dev_conf.intr_conf.rxq)
332                 return 0;
333         priv_rx_intr_vec_disable(priv);
334         intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
335         if (intr_handle->intr_vec == NULL) {
336                 ERROR("failed to allocate memory for interrupt vector,"
337                       " Rx interrupts will not be supported");
338                 return -ENOMEM;
339         }
340         intr_handle->type = RTE_INTR_HANDLE_EXT;
341         for (i = 0; i != n; ++i) {
342                 /* This rxq ibv must not be released in this function. */
343                 struct mlx5_rxq_ibv *rxq_ibv = mlx5_priv_rxq_ibv_get(priv, i);
344                 int fd;
345                 int flags;
346                 int rc;
347
348                 /* Skip queues that cannot request interrupts. */
349                 if (!rxq_ibv || !rxq_ibv->channel) {
350                         /* Use invalid intr_vec[] index to disable entry. */
351                         intr_handle->intr_vec[i] =
352                                 RTE_INTR_VEC_RXTX_OFFSET +
353                                 RTE_MAX_RXTX_INTR_VEC_ID;
354                         continue;
355                 }
356                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
357                         ERROR("too many Rx queues for interrupt vector size"
358                               " (%d), Rx interrupts cannot be enabled",
359                               RTE_MAX_RXTX_INTR_VEC_ID);
360                         priv_rx_intr_vec_disable(priv);
361                         return -1;
362                 }
363                 fd = rxq_ibv->channel->fd;
364                 flags = fcntl(fd, F_GETFL);
365                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
366                 if (rc < 0) {
367                         ERROR("failed to make Rx interrupt file descriptor"
368                               " %d non-blocking for queue index %d", fd, i);
369                         priv_rx_intr_vec_disable(priv);
370                         return -1;
371                 }
372                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
373                 intr_handle->efds[count] = fd;
374                 count++;
375         }
376         if (!count)
377                 priv_rx_intr_vec_disable(priv);
378         else
379                 intr_handle->nb_efd = count;
380         return 0;
381 }
382
383 /**
384  * Clean up Rx interrupts handler.
385  *
386  * @param priv
387  *   Pointer to private structure.
388  */
389 void
390 priv_rx_intr_vec_disable(struct priv *priv)
391 {
392         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
393         unsigned int i;
394         unsigned int rxqs_n = priv->rxqs_n;
395         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
396
397         if (!priv->dev->data->dev_conf.intr_conf.rxq)
398                 return;
399         if (!intr_handle->intr_vec)
400                 goto free;
401         for (i = 0; i != n; ++i) {
402                 struct mlx5_rxq_ctrl *rxq_ctrl;
403                 struct mlx5_rxq_data *rxq_data;
404
405                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
406                     RTE_MAX_RXTX_INTR_VEC_ID)
407                         continue;
408                 /**
409                  * Need to access directly the queue to release the reference
410                  * kept in priv_rx_intr_vec_enable().
411                  */
412                 rxq_data = (*priv->rxqs)[i];
413                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
414                 mlx5_priv_rxq_ibv_release(priv, rxq_ctrl->ibv);
415         }
416 free:
417         rte_intr_free_epoll_fd(intr_handle);
418         if (intr_handle->intr_vec)
419                 free(intr_handle->intr_vec);
420         intr_handle->nb_efd = 0;
421         intr_handle->intr_vec = NULL;
422 }
423
424 /**
425  *  MLX5 CQ notification .
426  *
427  *  @param rxq
428  *     Pointer to receive queue structure.
429  *  @param sq_n_rxq
430  *     Sequence number per receive queue .
431  */
432 static inline void
433 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
434 {
435         int sq_n = 0;
436         uint32_t doorbell_hi;
437         uint64_t doorbell;
438         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
439
440         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
441         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
442         doorbell = (uint64_t)doorbell_hi << 32;
443         doorbell |=  rxq->cqn;
444         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
445         rte_wmb();
446         rte_write64(rte_cpu_to_be_64(doorbell), cq_db_reg);
447 }
448
449 /**
450  * DPDK callback for Rx queue interrupt enable.
451  *
452  * @param dev
453  *   Pointer to Ethernet device structure.
454  * @param rx_queue_id
455  *   Rx queue number.
456  *
457  * @return
458  *   0 on success, negative on failure.
459  */
460 int
461 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
462 {
463         struct priv *priv = mlx5_get_priv(dev);
464         struct mlx5_rxq_data *rxq_data;
465         struct mlx5_rxq_ctrl *rxq_ctrl;
466         int ret = 0;
467
468         priv_lock(priv);
469         rxq_data = (*priv->rxqs)[rx_queue_id];
470         if (!rxq_data) {
471                 ret = EINVAL;
472                 goto exit;
473         }
474         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
475         if (rxq_ctrl->irq) {
476                 struct mlx5_rxq_ibv *rxq_ibv;
477
478                 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
479                 if (!rxq_ibv) {
480                         ret = EINVAL;
481                         goto exit;
482                 }
483                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
484                 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
485         }
486 exit:
487         priv_unlock(priv);
488         if (ret)
489                 WARN("unable to arm interrupt on rx queue %d", rx_queue_id);
490         return -ret;
491 }
492
493 /**
494  * DPDK callback for Rx queue interrupt disable.
495  *
496  * @param dev
497  *   Pointer to Ethernet device structure.
498  * @param rx_queue_id
499  *   Rx queue number.
500  *
501  * @return
502  *   0 on success, negative on failure.
503  */
504 int
505 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
506 {
507         struct priv *priv = mlx5_get_priv(dev);
508         struct mlx5_rxq_data *rxq_data;
509         struct mlx5_rxq_ctrl *rxq_ctrl;
510         struct mlx5_rxq_ibv *rxq_ibv = NULL;
511         struct ibv_cq *ev_cq;
512         void *ev_ctx;
513         int ret = 0;
514
515         priv_lock(priv);
516         rxq_data = (*priv->rxqs)[rx_queue_id];
517         if (!rxq_data) {
518                 ret = EINVAL;
519                 goto exit;
520         }
521         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
522         if (!rxq_ctrl->irq)
523                 goto exit;
524         rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
525         if (!rxq_ibv) {
526                 ret = EINVAL;
527                 goto exit;
528         }
529         ret = ibv_get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
530         if (ret || ev_cq != rxq_ibv->cq) {
531                 ret = EINVAL;
532                 goto exit;
533         }
534         rxq_data->cq_arm_sn++;
535         ibv_ack_cq_events(rxq_ibv->cq, 1);
536 exit:
537         if (rxq_ibv)
538                 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
539         priv_unlock(priv);
540         if (ret)
541                 WARN("unable to disable interrupt on rx queue %d",
542                      rx_queue_id);
543         return -ret;
544 }
545
546 /**
547  * Create the Rx queue Verbs object.
548  *
549  * @param priv
550  *   Pointer to private structure.
551  * @param idx
552  *   Queue index in DPDK Rx queue array
553  *
554  * @return
555  *   The Verbs object initialised if it can be created.
556  */
557 struct mlx5_rxq_ibv*
558 mlx5_priv_rxq_ibv_new(struct priv *priv, uint16_t idx)
559 {
560         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
561         struct mlx5_rxq_ctrl *rxq_ctrl =
562                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
563         struct ibv_wq_attr mod;
564         union {
565                 struct {
566                         struct ibv_cq_init_attr_ex ibv;
567                         struct mlx5dv_cq_init_attr mlx5;
568                 } cq;
569                 struct ibv_wq_init_attr wq;
570                 struct ibv_cq_ex cq_attr;
571         } attr;
572         unsigned int cqe_n = (1 << rxq_data->elts_n) - 1;
573         struct mlx5_rxq_ibv *tmpl;
574         struct mlx5dv_cq cq_info;
575         struct mlx5dv_rwq rwq;
576         unsigned int i;
577         int ret = 0;
578         struct mlx5dv_obj obj;
579
580         assert(rxq_data);
581         assert(!rxq_ctrl->ibv);
582         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
583                                  rxq_ctrl->socket);
584         if (!tmpl) {
585                 ERROR("%p: cannot allocate verbs resources",
586                        (void *)rxq_ctrl);
587                 goto error;
588         }
589         tmpl->rxq_ctrl = rxq_ctrl;
590         /* Use the entire RX mempool as the memory region. */
591         tmpl->mr = priv_mr_get(priv, rxq_data->mp);
592         if (!tmpl->mr) {
593                 tmpl->mr = priv_mr_new(priv, rxq_data->mp);
594                 if (!tmpl->mr) {
595                         ERROR("%p: MR creation failure", (void *)rxq_ctrl);
596                         goto error;
597                 }
598         }
599         if (rxq_ctrl->irq) {
600                 tmpl->channel = ibv_create_comp_channel(priv->ctx);
601                 if (!tmpl->channel) {
602                         ERROR("%p: Comp Channel creation failure",
603                               (void *)rxq_ctrl);
604                         goto error;
605                 }
606         }
607         attr.cq.ibv = (struct ibv_cq_init_attr_ex){
608                 .cqe = cqe_n,
609                 .channel = tmpl->channel,
610                 .comp_mask = 0,
611         };
612         attr.cq.mlx5 = (struct mlx5dv_cq_init_attr){
613                 .comp_mask = 0,
614         };
615         if (priv->cqe_comp && !rxq_data->hw_timestamp) {
616                 attr.cq.mlx5.comp_mask |=
617                         MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
618                 attr.cq.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
619                 /*
620                  * For vectorized Rx, it must not be doubled in order to
621                  * make cq_ci and rq_ci aligned.
622                  */
623                 if (rxq_check_vec_support(rxq_data) < 0)
624                         attr.cq.ibv.cqe *= 2;
625         } else if (priv->cqe_comp && rxq_data->hw_timestamp) {
626                 DEBUG("Rx CQE compression is disabled for HW timestamp");
627         }
628         tmpl->cq = ibv_cq_ex_to_cq(mlx5dv_create_cq(priv->ctx, &attr.cq.ibv,
629                                                     &attr.cq.mlx5));
630         if (tmpl->cq == NULL) {
631                 ERROR("%p: CQ creation failure", (void *)rxq_ctrl);
632                 goto error;
633         }
634         DEBUG("priv->device_attr.max_qp_wr is %d",
635               priv->device_attr.orig_attr.max_qp_wr);
636         DEBUG("priv->device_attr.max_sge is %d",
637               priv->device_attr.orig_attr.max_sge);
638         attr.wq = (struct ibv_wq_init_attr){
639                 .wq_context = NULL, /* Could be useful in the future. */
640                 .wq_type = IBV_WQT_RQ,
641                 /* Max number of outstanding WRs. */
642                 .max_wr = (1 << rxq_data->elts_n) >> rxq_data->sges_n,
643                 /* Max number of scatter/gather elements in a WR. */
644                 .max_sge = 1 << rxq_data->sges_n,
645                 .pd = priv->pd,
646                 .cq = tmpl->cq,
647                 .comp_mask =
648                         IBV_WQ_FLAGS_CVLAN_STRIPPING |
649                         0,
650                 .create_flags = (rxq_data->vlan_strip ?
651                                  IBV_WQ_FLAGS_CVLAN_STRIPPING :
652                                  0),
653         };
654         /* By default, FCS (CRC) is stripped by hardware. */
655         if (rxq_data->crc_present) {
656                 attr.wq.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
657                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
658         }
659 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
660         if (priv->hw_padding) {
661                 attr.wq.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
662                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
663         }
664 #endif
665         tmpl->wq = ibv_create_wq(priv->ctx, &attr.wq);
666         if (tmpl->wq == NULL) {
667                 ERROR("%p: WQ creation failure", (void *)rxq_ctrl);
668                 goto error;
669         }
670         /*
671          * Make sure number of WRs*SGEs match expectations since a queue
672          * cannot allocate more than "desc" buffers.
673          */
674         if (((int)attr.wq.max_wr !=
675              ((1 << rxq_data->elts_n) >> rxq_data->sges_n)) ||
676             ((int)attr.wq.max_sge != (1 << rxq_data->sges_n))) {
677                 ERROR("%p: requested %u*%u but got %u*%u WRs*SGEs",
678                       (void *)rxq_ctrl,
679                       ((1 << rxq_data->elts_n) >> rxq_data->sges_n),
680                       (1 << rxq_data->sges_n),
681                       attr.wq.max_wr, attr.wq.max_sge);
682                 goto error;
683         }
684         /* Change queue state to ready. */
685         mod = (struct ibv_wq_attr){
686                 .attr_mask = IBV_WQ_ATTR_STATE,
687                 .wq_state = IBV_WQS_RDY,
688         };
689         ret = ibv_modify_wq(tmpl->wq, &mod);
690         if (ret) {
691                 ERROR("%p: WQ state to IBV_WQS_RDY failed",
692                       (void *)rxq_ctrl);
693                 goto error;
694         }
695         obj.cq.in = tmpl->cq;
696         obj.cq.out = &cq_info;
697         obj.rwq.in = tmpl->wq;
698         obj.rwq.out = &rwq;
699         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
700         if (ret != 0)
701                 goto error;
702         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
703                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
704                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
705                 goto error;
706         }
707         /* Fill the rings. */
708         rxq_data->wqes = (volatile struct mlx5_wqe_data_seg (*)[])
709                 (uintptr_t)rwq.buf;
710         for (i = 0; (i != (unsigned int)(1 << rxq_data->elts_n)); ++i) {
711                 struct rte_mbuf *buf = (*rxq_data->elts)[i];
712                 volatile struct mlx5_wqe_data_seg *scat = &(*rxq_data->wqes)[i];
713
714                 /* scat->addr must be able to store a pointer. */
715                 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
716                 *scat = (struct mlx5_wqe_data_seg){
717                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
718                                                                   uintptr_t)),
719                         .byte_count = rte_cpu_to_be_32(DATA_LEN(buf)),
720                         .lkey = tmpl->mr->lkey,
721                 };
722         }
723         rxq_data->rq_db = rwq.dbrec;
724         rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
725         rxq_data->cq_ci = 0;
726         rxq_data->rq_ci = 0;
727         rxq_data->rq_pi = 0;
728         rxq_data->zip = (struct rxq_zip){
729                 .ai = 0,
730         };
731         rxq_data->cq_db = cq_info.dbrec;
732         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
733         /* Update doorbell counter. */
734         rxq_data->rq_ci = (1 << rxq_data->elts_n) >> rxq_data->sges_n;
735         rte_wmb();
736         *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
737         DEBUG("%p: rxq updated with %p", (void *)rxq_ctrl, (void *)&tmpl);
738         rte_atomic32_inc(&tmpl->refcnt);
739         DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
740               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
741         LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
742         return tmpl;
743 error:
744         if (tmpl->wq)
745                 claim_zero(ibv_destroy_wq(tmpl->wq));
746         if (tmpl->cq)
747                 claim_zero(ibv_destroy_cq(tmpl->cq));
748         if (tmpl->channel)
749                 claim_zero(ibv_destroy_comp_channel(tmpl->channel));
750         if (tmpl->mr)
751                 priv_mr_release(priv, tmpl->mr);
752         return NULL;
753 }
754
755 /**
756  * Get an Rx queue Verbs object.
757  *
758  * @param priv
759  *   Pointer to private structure.
760  * @param idx
761  *   Queue index in DPDK Rx queue array
762  *
763  * @return
764  *   The Verbs object if it exists.
765  */
766 struct mlx5_rxq_ibv*
767 mlx5_priv_rxq_ibv_get(struct priv *priv, uint16_t idx)
768 {
769         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
770         struct mlx5_rxq_ctrl *rxq_ctrl;
771
772         if (idx >= priv->rxqs_n)
773                 return NULL;
774         if (!rxq_data)
775                 return NULL;
776         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
777         if (rxq_ctrl->ibv) {
778                 priv_mr_get(priv, rxq_data->mp);
779                 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
780                 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
781                       (void *)rxq_ctrl->ibv,
782                       rte_atomic32_read(&rxq_ctrl->ibv->refcnt));
783         }
784         return rxq_ctrl->ibv;
785 }
786
787 /**
788  * Release an Rx verbs queue object.
789  *
790  * @param priv
791  *   Pointer to private structure.
792  * @param rxq_ibv
793  *   Verbs Rx queue object.
794  *
795  * @return
796  *   0 on success, errno value on failure.
797  */
798 int
799 mlx5_priv_rxq_ibv_release(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
800 {
801         int ret;
802
803         assert(rxq_ibv);
804         assert(rxq_ibv->wq);
805         assert(rxq_ibv->cq);
806         assert(rxq_ibv->mr);
807         ret = priv_mr_release(priv, rxq_ibv->mr);
808         if (!ret)
809                 rxq_ibv->mr = NULL;
810         DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
811               (void *)rxq_ibv, rte_atomic32_read(&rxq_ibv->refcnt));
812         if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
813                 rxq_free_elts(rxq_ibv->rxq_ctrl);
814                 claim_zero(ibv_destroy_wq(rxq_ibv->wq));
815                 claim_zero(ibv_destroy_cq(rxq_ibv->cq));
816                 if (rxq_ibv->channel)
817                         claim_zero(ibv_destroy_comp_channel(rxq_ibv->channel));
818                 LIST_REMOVE(rxq_ibv, next);
819                 rte_free(rxq_ibv);
820                 return 0;
821         }
822         return EBUSY;
823 }
824
825 /**
826  * Verify the Verbs Rx queue list is empty
827  *
828  * @param priv
829  *  Pointer to private structure.
830  *
831  * @return the number of object not released.
832  */
833 int
834 mlx5_priv_rxq_ibv_verify(struct priv *priv)
835 {
836         int ret = 0;
837         struct mlx5_rxq_ibv *rxq_ibv;
838
839         LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
840                 DEBUG("%p: Verbs Rx queue %p still referenced", (void *)priv,
841                       (void *)rxq_ibv);
842                 ++ret;
843         }
844         return ret;
845 }
846
847 /**
848  * Return true if a single reference exists on the object.
849  *
850  * @param priv
851  *   Pointer to private structure.
852  * @param rxq_ibv
853  *   Verbs Rx queue object.
854  */
855 int
856 mlx5_priv_rxq_ibv_releasable(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
857 {
858         (void)priv;
859         assert(rxq_ibv);
860         return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
861 }
862
863 /**
864  * Create a DPDK Rx queue.
865  *
866  * @param priv
867  *   Pointer to private structure.
868  * @param idx
869  *   TX queue index.
870  * @param desc
871  *   Number of descriptors to configure in queue.
872  * @param socket
873  *   NUMA socket on which memory must be allocated.
874  *
875  * @return
876  *   A DPDK queue object on success.
877  */
878 struct mlx5_rxq_ctrl*
879 mlx5_priv_rxq_new(struct priv *priv, uint16_t idx, uint16_t desc,
880                   unsigned int socket, struct rte_mempool *mp)
881 {
882         struct rte_eth_dev *dev = priv->dev;
883         struct mlx5_rxq_ctrl *tmpl;
884         const uint16_t desc_n =
885                 desc + priv->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
886         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
887
888         tmpl = rte_calloc_socket("RXQ", 1,
889                                  sizeof(*tmpl) +
890                                  desc_n * sizeof(struct rte_mbuf *),
891                                  0, socket);
892         if (!tmpl)
893                 return NULL;
894         if (priv->dev->data->dev_conf.intr_conf.rxq)
895                 tmpl->irq = 1;
896         /* Enable scattered packets support for this queue if necessary. */
897         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
898         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
899             (mb_len - RTE_PKTMBUF_HEADROOM)) {
900                 tmpl->rxq.sges_n = 0;
901         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
902                 unsigned int size =
903                         RTE_PKTMBUF_HEADROOM +
904                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
905                 unsigned int sges_n;
906
907                 /*
908                  * Determine the number of SGEs needed for a full packet
909                  * and round it to the next power of two.
910                  */
911                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
912                 tmpl->rxq.sges_n = sges_n;
913                 /* Make sure rxq.sges_n did not overflow. */
914                 size = mb_len * (1 << tmpl->rxq.sges_n);
915                 size -= RTE_PKTMBUF_HEADROOM;
916                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
917                         ERROR("%p: too many SGEs (%u) needed to handle"
918                               " requested maximum packet size %u",
919                               (void *)dev,
920                               1 << sges_n,
921                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
922                         goto error;
923                 }
924         } else {
925                 WARN("%p: the requested maximum Rx packet size (%u) is"
926                      " larger than a single mbuf (%u) and scattered"
927                      " mode has not been requested",
928                      (void *)dev,
929                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
930                      mb_len - RTE_PKTMBUF_HEADROOM);
931         }
932         DEBUG("%p: maximum number of segments per packet: %u",
933               (void *)dev, 1 << tmpl->rxq.sges_n);
934         if (desc % (1 << tmpl->rxq.sges_n)) {
935                 ERROR("%p: number of RX queue descriptors (%u) is not a"
936                       " multiple of SGEs per packet (%u)",
937                       (void *)dev,
938                       desc,
939                       1 << tmpl->rxq.sges_n);
940                 goto error;
941         }
942         /* Toggle RX checksum offload if hardware supports it. */
943         if (priv->hw_csum)
944                 tmpl->rxq.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
945         if (priv->hw_csum_l2tun)
946                 tmpl->rxq.csum_l2tun =
947                         !!dev->data->dev_conf.rxmode.hw_ip_checksum;
948         tmpl->rxq.hw_timestamp =
949                         !!dev->data->dev_conf.rxmode.hw_timestamp;
950         /* Configure VLAN stripping. */
951         tmpl->rxq.vlan_strip = (priv->hw_vlan_strip &&
952                                !!dev->data->dev_conf.rxmode.hw_vlan_strip);
953         /* By default, FCS (CRC) is stripped by hardware. */
954         if (dev->data->dev_conf.rxmode.hw_strip_crc) {
955                 tmpl->rxq.crc_present = 0;
956         } else if (priv->hw_fcs_strip) {
957                 tmpl->rxq.crc_present = 1;
958         } else {
959                 WARN("%p: CRC stripping has been disabled but will still"
960                      " be performed by hardware, make sure MLNX_OFED and"
961                      " firmware are up to date",
962                      (void *)dev);
963                 tmpl->rxq.crc_present = 0;
964         }
965         DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
966               " incoming frames to hide it",
967               (void *)dev,
968               tmpl->rxq.crc_present ? "disabled" : "enabled",
969               tmpl->rxq.crc_present << 2);
970         /* Save port ID. */
971         tmpl->rxq.rss_hash = priv->rxqs_n > 1;
972         tmpl->rxq.port_id = dev->data->port_id;
973         tmpl->priv = priv;
974         tmpl->rxq.mp = mp;
975         tmpl->rxq.stats.idx = idx;
976         tmpl->rxq.elts_n = log2above(desc);
977         tmpl->rxq.elts =
978                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
979         rte_atomic32_inc(&tmpl->refcnt);
980         DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
981               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
982         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
983         return tmpl;
984 error:
985         rte_free(tmpl);
986         return NULL;
987 }
988
989 /**
990  * Get a Rx queue.
991  *
992  * @param priv
993  *   Pointer to private structure.
994  * @param idx
995  *   TX queue index.
996  *
997  * @return
998  *   A pointer to the queue if it exists.
999  */
1000 struct mlx5_rxq_ctrl*
1001 mlx5_priv_rxq_get(struct priv *priv, uint16_t idx)
1002 {
1003         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1004
1005         if ((*priv->rxqs)[idx]) {
1006                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1007                                         struct mlx5_rxq_ctrl,
1008                                         rxq);
1009
1010                 mlx5_priv_rxq_ibv_get(priv, idx);
1011                 rte_atomic32_inc(&rxq_ctrl->refcnt);
1012                 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1013                       (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1014         }
1015         return rxq_ctrl;
1016 }
1017
1018 /**
1019  * Release a Rx queue.
1020  *
1021  * @param priv
1022  *   Pointer to private structure.
1023  * @param idx
1024  *   TX queue index.
1025  *
1026  * @return
1027  *   0 on success, errno value on failure.
1028  */
1029 int
1030 mlx5_priv_rxq_release(struct priv *priv, uint16_t idx)
1031 {
1032         struct mlx5_rxq_ctrl *rxq_ctrl;
1033
1034         if (!(*priv->rxqs)[idx])
1035                 return 0;
1036         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1037         assert(rxq_ctrl->priv);
1038         if (rxq_ctrl->ibv) {
1039                 int ret;
1040
1041                 ret = mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
1042                 if (!ret)
1043                         rxq_ctrl->ibv = NULL;
1044         }
1045         DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1046               (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1047         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1048                 LIST_REMOVE(rxq_ctrl, next);
1049                 rte_free(rxq_ctrl);
1050                 (*priv->rxqs)[idx] = NULL;
1051                 return 0;
1052         }
1053         return EBUSY;
1054 }
1055
1056 /**
1057  * Verify if the queue can be released.
1058  *
1059  * @param priv
1060  *   Pointer to private structure.
1061  * @param idx
1062  *   TX queue index.
1063  *
1064  * @return
1065  *   1 if the queue can be released.
1066  */
1067 int
1068 mlx5_priv_rxq_releasable(struct priv *priv, uint16_t idx)
1069 {
1070         struct mlx5_rxq_ctrl *rxq_ctrl;
1071
1072         if (!(*priv->rxqs)[idx])
1073                 return -1;
1074         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1075         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1076 }
1077
1078 /**
1079  * Verify the Rx Queue list is empty
1080  *
1081  * @param priv
1082  *  Pointer to private structure.
1083  *
1084  * @return the number of object not released.
1085  */
1086 int
1087 mlx5_priv_rxq_verify(struct priv *priv)
1088 {
1089         struct mlx5_rxq_ctrl *rxq_ctrl;
1090         int ret = 0;
1091
1092         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1093                 DEBUG("%p: Rx Queue %p still referenced", (void *)priv,
1094                       (void *)rxq_ctrl);
1095                 ++ret;
1096         }
1097         return ret;
1098 }
1099
1100 /**
1101  * Create an indirection table.
1102  *
1103  * @param priv
1104  *   Pointer to private structure.
1105  * @param queues
1106  *   Queues entering in the indirection table.
1107  * @param queues_n
1108  *   Number of queues in the array.
1109  *
1110  * @return
1111  *   A new indirection table.
1112  */
1113 struct mlx5_ind_table_ibv*
1114 mlx5_priv_ind_table_ibv_new(struct priv *priv, uint16_t queues[],
1115                             uint16_t queues_n)
1116 {
1117         struct mlx5_ind_table_ibv *ind_tbl;
1118         const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1119                 log2above(queues_n) :
1120                 priv->ind_table_max_size;
1121         struct ibv_wq *wq[1 << wq_n];
1122         unsigned int i;
1123         unsigned int j;
1124
1125         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1126                              queues_n * sizeof(uint16_t), 0);
1127         if (!ind_tbl)
1128                 return NULL;
1129         for (i = 0; i != queues_n; ++i) {
1130                 struct mlx5_rxq_ctrl *rxq =
1131                         mlx5_priv_rxq_get(priv, queues[i]);
1132
1133                 if (!rxq)
1134                         goto error;
1135                 wq[i] = rxq->ibv->wq;
1136                 ind_tbl->queues[i] = queues[i];
1137         }
1138         ind_tbl->queues_n = queues_n;
1139         /* Finalise indirection table. */
1140         for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1141                 wq[i] = wq[j];
1142         ind_tbl->ind_table = ibv_create_rwq_ind_table(
1143                 priv->ctx,
1144                 &(struct ibv_rwq_ind_table_init_attr){
1145                         .log_ind_tbl_size = wq_n,
1146                         .ind_tbl = wq,
1147                         .comp_mask = 0,
1148                 });
1149         if (!ind_tbl->ind_table)
1150                 goto error;
1151         rte_atomic32_inc(&ind_tbl->refcnt);
1152         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1153         DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1154               (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1155         return ind_tbl;
1156 error:
1157         rte_free(ind_tbl);
1158         DEBUG("%p cannot create indirection table", (void *)priv);
1159         return NULL;
1160 }
1161
1162 /**
1163  * Get an indirection table.
1164  *
1165  * @param priv
1166  *   Pointer to private structure.
1167  * @param queues
1168  *   Queues entering in the indirection table.
1169  * @param queues_n
1170  *   Number of queues in the array.
1171  *
1172  * @return
1173  *   An indirection table if found.
1174  */
1175 struct mlx5_ind_table_ibv*
1176 mlx5_priv_ind_table_ibv_get(struct priv *priv, uint16_t queues[],
1177                             uint16_t queues_n)
1178 {
1179         struct mlx5_ind_table_ibv *ind_tbl;
1180
1181         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1182                 if ((ind_tbl->queues_n == queues_n) &&
1183                     (memcmp(ind_tbl->queues, queues,
1184                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1185                      == 0))
1186                         break;
1187         }
1188         if (ind_tbl) {
1189                 unsigned int i;
1190
1191                 rte_atomic32_inc(&ind_tbl->refcnt);
1192                 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1193                       (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1194                 for (i = 0; i != ind_tbl->queues_n; ++i)
1195                         mlx5_priv_rxq_get(priv, ind_tbl->queues[i]);
1196         }
1197         return ind_tbl;
1198 }
1199
1200 /**
1201  * Release an indirection table.
1202  *
1203  * @param priv
1204  *   Pointer to private structure.
1205  * @param ind_table
1206  *   Indirection table to release.
1207  *
1208  * @return
1209  *   0 on success, errno value on failure.
1210  */
1211 int
1212 mlx5_priv_ind_table_ibv_release(struct priv *priv,
1213                                 struct mlx5_ind_table_ibv *ind_tbl)
1214 {
1215         unsigned int i;
1216
1217         DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1218               (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1219         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1220                 claim_zero(ibv_destroy_rwq_ind_table(ind_tbl->ind_table));
1221         for (i = 0; i != ind_tbl->queues_n; ++i)
1222                 claim_nonzero(mlx5_priv_rxq_release(priv, ind_tbl->queues[i]));
1223         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1224                 LIST_REMOVE(ind_tbl, next);
1225                 rte_free(ind_tbl);
1226                 return 0;
1227         }
1228         return EBUSY;
1229 }
1230
1231 /**
1232  * Verify the Rx Queue list is empty
1233  *
1234  * @param priv
1235  *  Pointer to private structure.
1236  *
1237  * @return the number of object not released.
1238  */
1239 int
1240 mlx5_priv_ind_table_ibv_verify(struct priv *priv)
1241 {
1242         struct mlx5_ind_table_ibv *ind_tbl;
1243         int ret = 0;
1244
1245         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1246                 DEBUG("%p: Verbs indirection table %p still referenced",
1247                       (void *)priv, (void *)ind_tbl);
1248                 ++ret;
1249         }
1250         return ret;
1251 }
1252
1253 /**
1254  * Create an Rx Hash queue.
1255  *
1256  * @param priv
1257  *   Pointer to private structure.
1258  * @param rss_key
1259  *   RSS key for the Rx hash queue.
1260  * @param rss_key_len
1261  *   RSS key length.
1262  * @param hash_fields
1263  *   Verbs protocol hash field to make the RSS on.
1264  * @param queues
1265  *   Queues entering in hash queue.
1266  * @param queues_n
1267  *   Number of queues.
1268  *
1269  * @return
1270  *   An hash Rx queue on success.
1271  */
1272 struct mlx5_hrxq*
1273 mlx5_priv_hrxq_new(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1274                    uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1275 {
1276         struct mlx5_hrxq *hrxq;
1277         struct mlx5_ind_table_ibv *ind_tbl;
1278         struct ibv_qp *qp;
1279
1280         ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1281         if (!ind_tbl)
1282                 ind_tbl = mlx5_priv_ind_table_ibv_new(priv, queues, queues_n);
1283         if (!ind_tbl)
1284                 return NULL;
1285         qp = ibv_create_qp_ex(
1286                 priv->ctx,
1287                 &(struct ibv_qp_init_attr_ex){
1288                         .qp_type = IBV_QPT_RAW_PACKET,
1289                         .comp_mask =
1290                                 IBV_QP_INIT_ATTR_PD |
1291                                 IBV_QP_INIT_ATTR_IND_TABLE |
1292                                 IBV_QP_INIT_ATTR_RX_HASH,
1293                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1294                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1295                                 .rx_hash_key_len = rss_key_len,
1296                                 .rx_hash_key = rss_key,
1297                                 .rx_hash_fields_mask = hash_fields,
1298                         },
1299                         .rwq_ind_tbl = ind_tbl->ind_table,
1300                         .pd = priv->pd,
1301                 });
1302         if (!qp)
1303                 goto error;
1304         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1305         if (!hrxq)
1306                 goto error;
1307         hrxq->ind_table = ind_tbl;
1308         hrxq->qp = qp;
1309         hrxq->rss_key_len = rss_key_len;
1310         hrxq->hash_fields = hash_fields;
1311         memcpy(hrxq->rss_key, rss_key, rss_key_len);
1312         rte_atomic32_inc(&hrxq->refcnt);
1313         LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1314         DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1315               (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1316         return hrxq;
1317 error:
1318         mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1319         if (qp)
1320                 claim_zero(ibv_destroy_qp(qp));
1321         return NULL;
1322 }
1323
1324 /**
1325  * Get an Rx Hash queue.
1326  *
1327  * @param priv
1328  *   Pointer to private structure.
1329  * @param rss_conf
1330  *   RSS configuration for the Rx hash queue.
1331  * @param queues
1332  *   Queues entering in hash queue.
1333  * @param queues_n
1334  *   Number of queues.
1335  *
1336  * @return
1337  *   An hash Rx queue on success.
1338  */
1339 struct mlx5_hrxq*
1340 mlx5_priv_hrxq_get(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1341                    uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1342 {
1343         struct mlx5_hrxq *hrxq;
1344
1345         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1346                 struct mlx5_ind_table_ibv *ind_tbl;
1347
1348                 if (hrxq->rss_key_len != rss_key_len)
1349                         continue;
1350                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1351                         continue;
1352                 if (hrxq->hash_fields != hash_fields)
1353                         continue;
1354                 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1355                 if (!ind_tbl)
1356                         continue;
1357                 if (ind_tbl != hrxq->ind_table) {
1358                         mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1359                         continue;
1360                 }
1361                 rte_atomic32_inc(&hrxq->refcnt);
1362                 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1363                       (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1364                 return hrxq;
1365         }
1366         return NULL;
1367 }
1368
1369 /**
1370  * Release the hash Rx queue.
1371  *
1372  * @param priv
1373  *   Pointer to private structure.
1374  * @param hrxq
1375  *   Pointer to Hash Rx queue to release.
1376  *
1377  * @return
1378  *   0 on success, errno value on failure.
1379  */
1380 int
1381 mlx5_priv_hrxq_release(struct priv *priv, struct mlx5_hrxq *hrxq)
1382 {
1383         DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1384               (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1385         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1386                 claim_zero(ibv_destroy_qp(hrxq->qp));
1387                 mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table);
1388                 LIST_REMOVE(hrxq, next);
1389                 rte_free(hrxq);
1390                 return 0;
1391         }
1392         claim_nonzero(mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table));
1393         return EBUSY;
1394 }
1395
1396 /**
1397  * Verify the Rx Queue list is empty
1398  *
1399  * @param priv
1400  *  Pointer to private structure.
1401  *
1402  * @return the number of object not released.
1403  */
1404 int
1405 mlx5_priv_hrxq_ibv_verify(struct priv *priv)
1406 {
1407         struct mlx5_hrxq *hrxq;
1408         int ret = 0;
1409
1410         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1411                 DEBUG("%p: Verbs Hash Rx queue %p still referenced",
1412                       (void *)priv, (void *)hrxq);
1413                 ++ret;
1414         }
1415         return ret;
1416 }