8cee5de12ddc5d07fd429d89a79241281ab021e3
[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_driver.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  * Returns the per-queue supported offloads.
217  *
218  * @param priv
219  *   Pointer to private structure.
220  *
221  * @return
222  *   Supported Rx offloads.
223  */
224 uint64_t
225 mlx5_priv_get_rx_queue_offloads(struct priv *priv)
226 {
227         struct mlx5_dev_config *config = &priv->config;
228         uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
229                              DEV_RX_OFFLOAD_TIMESTAMP |
230                              DEV_RX_OFFLOAD_JUMBO_FRAME);
231
232         if (config->hw_fcs_strip)
233                 offloads |= DEV_RX_OFFLOAD_CRC_STRIP;
234         if (config->hw_csum)
235                 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
236                              DEV_RX_OFFLOAD_UDP_CKSUM |
237                              DEV_RX_OFFLOAD_TCP_CKSUM);
238         if (config->hw_vlan_strip)
239                 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
240         return offloads;
241 }
242
243
244 /**
245  * Returns the per-port supported offloads.
246  *
247  * @param priv
248  *   Pointer to private structure.
249  * @return
250  *   Supported Rx offloads.
251  */
252 uint64_t
253 mlx5_priv_get_rx_port_offloads(struct priv *priv __rte_unused)
254 {
255         uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
256
257         return offloads;
258 }
259
260 /**
261  * Checks if the per-queue offload configuration is valid.
262  *
263  * @param priv
264  *   Pointer to private structure.
265  * @param offloads
266  *   Per-queue offloads configuration.
267  *
268  * @return
269  *   1 if the configuration is valid, 0 otherwise.
270  */
271 static int
272 priv_is_rx_queue_offloads_allowed(struct priv *priv, uint64_t offloads)
273 {
274         uint64_t port_offloads = priv->dev->data->dev_conf.rxmode.offloads;
275         uint64_t queue_supp_offloads =
276                 mlx5_priv_get_rx_queue_offloads(priv);
277         uint64_t port_supp_offloads = mlx5_priv_get_rx_port_offloads(priv);
278
279         if ((offloads & (queue_supp_offloads | port_supp_offloads)) !=
280             offloads)
281                 return 0;
282         if (((port_offloads ^ offloads) & port_supp_offloads))
283                 return 0;
284         return 1;
285 }
286
287 /**
288  *
289  * @param dev
290  *   Pointer to Ethernet device structure.
291  * @param idx
292  *   RX queue index.
293  * @param desc
294  *   Number of descriptors to configure in queue.
295  * @param socket
296  *   NUMA socket on which memory must be allocated.
297  * @param[in] conf
298  *   Thresholds parameters.
299  * @param mp
300  *   Memory pool for buffer allocations.
301  *
302  * @return
303  *   0 on success, negative errno value on failure.
304  */
305 int
306 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
307                     unsigned int socket, const struct rte_eth_rxconf *conf,
308                     struct rte_mempool *mp)
309 {
310         struct priv *priv = dev->data->dev_private;
311         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
312         struct mlx5_rxq_ctrl *rxq_ctrl =
313                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
314         int ret = 0;
315
316         priv_lock(priv);
317         if (!rte_is_power_of_2(desc)) {
318                 desc = 1 << log2above(desc);
319                 WARN("%p: increased number of descriptors in RX queue %u"
320                      " to the next power of two (%d)",
321                      (void *)dev, idx, desc);
322         }
323         DEBUG("%p: configuring queue %u for %u descriptors",
324               (void *)dev, idx, desc);
325         if (idx >= priv->rxqs_n) {
326                 ERROR("%p: queue index out of range (%u >= %u)",
327                       (void *)dev, idx, priv->rxqs_n);
328                 priv_unlock(priv);
329                 return -EOVERFLOW;
330         }
331         if (!priv_is_rx_queue_offloads_allowed(priv, conf->offloads)) {
332                 ret = ENOTSUP;
333                 ERROR("%p: Rx queue offloads 0x%" PRIx64 " don't match port "
334                       "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
335                       (void *)dev, conf->offloads,
336                       dev->data->dev_conf.rxmode.offloads,
337                       (mlx5_priv_get_rx_port_offloads(priv) |
338                        mlx5_priv_get_rx_queue_offloads(priv)));
339                 goto out;
340         }
341         if (!mlx5_priv_rxq_releasable(priv, idx)) {
342                 ret = EBUSY;
343                 ERROR("%p: unable to release queue index %u",
344                       (void *)dev, idx);
345                 goto out;
346         }
347         mlx5_priv_rxq_release(priv, idx);
348         rxq_ctrl = mlx5_priv_rxq_new(priv, idx, desc, socket, conf, mp);
349         if (!rxq_ctrl) {
350                 ERROR("%p: unable to allocate queue index %u",
351                       (void *)dev, idx);
352                 ret = ENOMEM;
353                 goto out;
354         }
355         DEBUG("%p: adding RX queue %p to list",
356               (void *)dev, (void *)rxq_ctrl);
357         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
358 out:
359         priv_unlock(priv);
360         return -ret;
361 }
362
363 /**
364  * DPDK callback to release a RX queue.
365  *
366  * @param dpdk_rxq
367  *   Generic RX queue pointer.
368  */
369 void
370 mlx5_rx_queue_release(void *dpdk_rxq)
371 {
372         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
373         struct mlx5_rxq_ctrl *rxq_ctrl;
374         struct priv *priv;
375
376         if (rxq == NULL)
377                 return;
378         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
379         priv = rxq_ctrl->priv;
380         priv_lock(priv);
381         if (!mlx5_priv_rxq_releasable(priv, rxq_ctrl->rxq.stats.idx))
382                 rte_panic("Rx queue %p is still used by a flow and cannot be"
383                           " removed\n", (void *)rxq_ctrl);
384         mlx5_priv_rxq_release(priv, rxq_ctrl->rxq.stats.idx);
385         priv_unlock(priv);
386 }
387
388 /**
389  * Allocate queue vector and fill epoll fd list for Rx interrupts.
390  *
391  * @param priv
392  *   Pointer to private structure.
393  *
394  * @return
395  *   0 on success, negative on failure.
396  */
397 int
398 priv_rx_intr_vec_enable(struct priv *priv)
399 {
400         unsigned int i;
401         unsigned int rxqs_n = priv->rxqs_n;
402         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
403         unsigned int count = 0;
404         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
405
406         if (!priv->dev->data->dev_conf.intr_conf.rxq)
407                 return 0;
408         priv_rx_intr_vec_disable(priv);
409         intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
410         if (intr_handle->intr_vec == NULL) {
411                 ERROR("failed to allocate memory for interrupt vector,"
412                       " Rx interrupts will not be supported");
413                 return -ENOMEM;
414         }
415         intr_handle->type = RTE_INTR_HANDLE_EXT;
416         for (i = 0; i != n; ++i) {
417                 /* This rxq ibv must not be released in this function. */
418                 struct mlx5_rxq_ibv *rxq_ibv = mlx5_priv_rxq_ibv_get(priv, i);
419                 int fd;
420                 int flags;
421                 int rc;
422
423                 /* Skip queues that cannot request interrupts. */
424                 if (!rxq_ibv || !rxq_ibv->channel) {
425                         /* Use invalid intr_vec[] index to disable entry. */
426                         intr_handle->intr_vec[i] =
427                                 RTE_INTR_VEC_RXTX_OFFSET +
428                                 RTE_MAX_RXTX_INTR_VEC_ID;
429                         continue;
430                 }
431                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
432                         ERROR("too many Rx queues for interrupt vector size"
433                               " (%d), Rx interrupts cannot be enabled",
434                               RTE_MAX_RXTX_INTR_VEC_ID);
435                         priv_rx_intr_vec_disable(priv);
436                         return -1;
437                 }
438                 fd = rxq_ibv->channel->fd;
439                 flags = fcntl(fd, F_GETFL);
440                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
441                 if (rc < 0) {
442                         ERROR("failed to make Rx interrupt file descriptor"
443                               " %d non-blocking for queue index %d", fd, i);
444                         priv_rx_intr_vec_disable(priv);
445                         return -1;
446                 }
447                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
448                 intr_handle->efds[count] = fd;
449                 count++;
450         }
451         if (!count)
452                 priv_rx_intr_vec_disable(priv);
453         else
454                 intr_handle->nb_efd = count;
455         return 0;
456 }
457
458 /**
459  * Clean up Rx interrupts handler.
460  *
461  * @param priv
462  *   Pointer to private structure.
463  */
464 void
465 priv_rx_intr_vec_disable(struct priv *priv)
466 {
467         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
468         unsigned int i;
469         unsigned int rxqs_n = priv->rxqs_n;
470         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
471
472         if (!priv->dev->data->dev_conf.intr_conf.rxq)
473                 return;
474         if (!intr_handle->intr_vec)
475                 goto free;
476         for (i = 0; i != n; ++i) {
477                 struct mlx5_rxq_ctrl *rxq_ctrl;
478                 struct mlx5_rxq_data *rxq_data;
479
480                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
481                     RTE_MAX_RXTX_INTR_VEC_ID)
482                         continue;
483                 /**
484                  * Need to access directly the queue to release the reference
485                  * kept in priv_rx_intr_vec_enable().
486                  */
487                 rxq_data = (*priv->rxqs)[i];
488                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
489                 mlx5_priv_rxq_ibv_release(priv, rxq_ctrl->ibv);
490         }
491 free:
492         rte_intr_free_epoll_fd(intr_handle);
493         if (intr_handle->intr_vec)
494                 free(intr_handle->intr_vec);
495         intr_handle->nb_efd = 0;
496         intr_handle->intr_vec = NULL;
497 }
498
499 /**
500  *  MLX5 CQ notification .
501  *
502  *  @param rxq
503  *     Pointer to receive queue structure.
504  *  @param sq_n_rxq
505  *     Sequence number per receive queue .
506  */
507 static inline void
508 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
509 {
510         int sq_n = 0;
511         uint32_t doorbell_hi;
512         uint64_t doorbell;
513         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
514
515         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
516         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
517         doorbell = (uint64_t)doorbell_hi << 32;
518         doorbell |=  rxq->cqn;
519         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
520         rte_write64(rte_cpu_to_be_64(doorbell), cq_db_reg);
521 }
522
523 /**
524  * DPDK callback for Rx queue interrupt enable.
525  *
526  * @param dev
527  *   Pointer to Ethernet device structure.
528  * @param rx_queue_id
529  *   Rx queue number.
530  *
531  * @return
532  *   0 on success, negative on failure.
533  */
534 int
535 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
536 {
537         struct priv *priv = dev->data->dev_private;
538         struct mlx5_rxq_data *rxq_data;
539         struct mlx5_rxq_ctrl *rxq_ctrl;
540         int ret = 0;
541
542         priv_lock(priv);
543         rxq_data = (*priv->rxqs)[rx_queue_id];
544         if (!rxq_data) {
545                 ret = EINVAL;
546                 goto exit;
547         }
548         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
549         if (rxq_ctrl->irq) {
550                 struct mlx5_rxq_ibv *rxq_ibv;
551
552                 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
553                 if (!rxq_ibv) {
554                         ret = EINVAL;
555                         goto exit;
556                 }
557                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
558                 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
559         }
560 exit:
561         priv_unlock(priv);
562         if (ret)
563                 WARN("unable to arm interrupt on rx queue %d", rx_queue_id);
564         return -ret;
565 }
566
567 /**
568  * DPDK callback for Rx queue interrupt disable.
569  *
570  * @param dev
571  *   Pointer to Ethernet device structure.
572  * @param rx_queue_id
573  *   Rx queue number.
574  *
575  * @return
576  *   0 on success, negative on failure.
577  */
578 int
579 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
580 {
581         struct priv *priv = dev->data->dev_private;
582         struct mlx5_rxq_data *rxq_data;
583         struct mlx5_rxq_ctrl *rxq_ctrl;
584         struct mlx5_rxq_ibv *rxq_ibv = NULL;
585         struct ibv_cq *ev_cq;
586         void *ev_ctx;
587         int ret = 0;
588
589         priv_lock(priv);
590         rxq_data = (*priv->rxqs)[rx_queue_id];
591         if (!rxq_data) {
592                 ret = EINVAL;
593                 goto exit;
594         }
595         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
596         if (!rxq_ctrl->irq)
597                 goto exit;
598         rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
599         if (!rxq_ibv) {
600                 ret = EINVAL;
601                 goto exit;
602         }
603         ret = ibv_get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
604         if (ret || ev_cq != rxq_ibv->cq) {
605                 ret = EINVAL;
606                 goto exit;
607         }
608         rxq_data->cq_arm_sn++;
609         ibv_ack_cq_events(rxq_ibv->cq, 1);
610 exit:
611         if (rxq_ibv)
612                 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
613         priv_unlock(priv);
614         if (ret)
615                 WARN("unable to disable interrupt on rx queue %d",
616                      rx_queue_id);
617         return -ret;
618 }
619
620 /**
621  * Create the Rx queue Verbs object.
622  *
623  * @param priv
624  *   Pointer to private structure.
625  * @param idx
626  *   Queue index in DPDK Rx queue array
627  *
628  * @return
629  *   The Verbs object initialised if it can be created.
630  */
631 struct mlx5_rxq_ibv*
632 mlx5_priv_rxq_ibv_new(struct priv *priv, uint16_t idx)
633 {
634         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
635         struct mlx5_rxq_ctrl *rxq_ctrl =
636                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
637         struct ibv_wq_attr mod;
638         union {
639                 struct {
640                         struct ibv_cq_init_attr_ex ibv;
641                         struct mlx5dv_cq_init_attr mlx5;
642                 } cq;
643                 struct ibv_wq_init_attr wq;
644                 struct ibv_cq_ex cq_attr;
645         } attr;
646         unsigned int cqe_n = (1 << rxq_data->elts_n) - 1;
647         struct mlx5_rxq_ibv *tmpl;
648         struct mlx5dv_cq cq_info;
649         struct mlx5dv_rwq rwq;
650         unsigned int i;
651         int ret = 0;
652         struct mlx5dv_obj obj;
653         struct mlx5_dev_config *config = &priv->config;
654
655         assert(rxq_data);
656         assert(!rxq_ctrl->ibv);
657         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
658                                  rxq_ctrl->socket);
659         if (!tmpl) {
660                 ERROR("%p: cannot allocate verbs resources",
661                        (void *)rxq_ctrl);
662                 goto error;
663         }
664         tmpl->rxq_ctrl = rxq_ctrl;
665         /* Use the entire RX mempool as the memory region. */
666         tmpl->mr = priv_mr_get(priv, rxq_data->mp);
667         if (!tmpl->mr) {
668                 tmpl->mr = priv_mr_new(priv, rxq_data->mp);
669                 if (!tmpl->mr) {
670                         ERROR("%p: MR creation failure", (void *)rxq_ctrl);
671                         goto error;
672                 }
673         }
674         if (rxq_ctrl->irq) {
675                 tmpl->channel = ibv_create_comp_channel(priv->ctx);
676                 if (!tmpl->channel) {
677                         ERROR("%p: Comp Channel creation failure",
678                               (void *)rxq_ctrl);
679                         goto error;
680                 }
681         }
682         attr.cq.ibv = (struct ibv_cq_init_attr_ex){
683                 .cqe = cqe_n,
684                 .channel = tmpl->channel,
685                 .comp_mask = 0,
686         };
687         attr.cq.mlx5 = (struct mlx5dv_cq_init_attr){
688                 .comp_mask = 0,
689         };
690         if (config->cqe_comp && !rxq_data->hw_timestamp) {
691                 attr.cq.mlx5.comp_mask |=
692                         MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
693                 attr.cq.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
694                 /*
695                  * For vectorized Rx, it must not be doubled in order to
696                  * make cq_ci and rq_ci aligned.
697                  */
698                 if (rxq_check_vec_support(rxq_data) < 0)
699                         attr.cq.ibv.cqe *= 2;
700         } else if (config->cqe_comp && rxq_data->hw_timestamp) {
701                 DEBUG("Rx CQE compression is disabled for HW timestamp");
702         }
703         tmpl->cq = ibv_cq_ex_to_cq(mlx5dv_create_cq(priv->ctx, &attr.cq.ibv,
704                                                     &attr.cq.mlx5));
705         if (tmpl->cq == NULL) {
706                 ERROR("%p: CQ creation failure", (void *)rxq_ctrl);
707                 goto error;
708         }
709         DEBUG("priv->device_attr.max_qp_wr is %d",
710               priv->device_attr.orig_attr.max_qp_wr);
711         DEBUG("priv->device_attr.max_sge is %d",
712               priv->device_attr.orig_attr.max_sge);
713         attr.wq = (struct ibv_wq_init_attr){
714                 .wq_context = NULL, /* Could be useful in the future. */
715                 .wq_type = IBV_WQT_RQ,
716                 /* Max number of outstanding WRs. */
717                 .max_wr = (1 << rxq_data->elts_n) >> rxq_data->sges_n,
718                 /* Max number of scatter/gather elements in a WR. */
719                 .max_sge = 1 << rxq_data->sges_n,
720                 .pd = priv->pd,
721                 .cq = tmpl->cq,
722                 .comp_mask =
723                         IBV_WQ_FLAGS_CVLAN_STRIPPING |
724                         0,
725                 .create_flags = (rxq_data->vlan_strip ?
726                                  IBV_WQ_FLAGS_CVLAN_STRIPPING :
727                                  0),
728         };
729         /* By default, FCS (CRC) is stripped by hardware. */
730         if (rxq_data->crc_present) {
731                 attr.wq.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
732                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
733         }
734 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
735         if (config->hw_padding) {
736                 attr.wq.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
737                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
738         }
739 #endif
740         tmpl->wq = ibv_create_wq(priv->ctx, &attr.wq);
741         if (tmpl->wq == NULL) {
742                 ERROR("%p: WQ creation failure", (void *)rxq_ctrl);
743                 goto error;
744         }
745         /*
746          * Make sure number of WRs*SGEs match expectations since a queue
747          * cannot allocate more than "desc" buffers.
748          */
749         if (((int)attr.wq.max_wr !=
750              ((1 << rxq_data->elts_n) >> rxq_data->sges_n)) ||
751             ((int)attr.wq.max_sge != (1 << rxq_data->sges_n))) {
752                 ERROR("%p: requested %u*%u but got %u*%u WRs*SGEs",
753                       (void *)rxq_ctrl,
754                       ((1 << rxq_data->elts_n) >> rxq_data->sges_n),
755                       (1 << rxq_data->sges_n),
756                       attr.wq.max_wr, attr.wq.max_sge);
757                 goto error;
758         }
759         /* Change queue state to ready. */
760         mod = (struct ibv_wq_attr){
761                 .attr_mask = IBV_WQ_ATTR_STATE,
762                 .wq_state = IBV_WQS_RDY,
763         };
764         ret = ibv_modify_wq(tmpl->wq, &mod);
765         if (ret) {
766                 ERROR("%p: WQ state to IBV_WQS_RDY failed",
767                       (void *)rxq_ctrl);
768                 goto error;
769         }
770         obj.cq.in = tmpl->cq;
771         obj.cq.out = &cq_info;
772         obj.rwq.in = tmpl->wq;
773         obj.rwq.out = &rwq;
774         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
775         if (ret != 0)
776                 goto error;
777         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
778                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
779                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
780                 goto error;
781         }
782         /* Fill the rings. */
783         rxq_data->wqes = (volatile struct mlx5_wqe_data_seg (*)[])
784                 (uintptr_t)rwq.buf;
785         for (i = 0; (i != (unsigned int)(1 << rxq_data->elts_n)); ++i) {
786                 struct rte_mbuf *buf = (*rxq_data->elts)[i];
787                 volatile struct mlx5_wqe_data_seg *scat = &(*rxq_data->wqes)[i];
788
789                 /* scat->addr must be able to store a pointer. */
790                 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
791                 *scat = (struct mlx5_wqe_data_seg){
792                         .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
793                                                                   uintptr_t)),
794                         .byte_count = rte_cpu_to_be_32(DATA_LEN(buf)),
795                         .lkey = tmpl->mr->lkey,
796                 };
797         }
798         rxq_data->rq_db = rwq.dbrec;
799         rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
800         rxq_data->cq_ci = 0;
801         rxq_data->rq_ci = 0;
802         rxq_data->rq_pi = 0;
803         rxq_data->zip = (struct rxq_zip){
804                 .ai = 0,
805         };
806         rxq_data->cq_db = cq_info.dbrec;
807         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
808         rxq_data->cq_uar = cq_info.cq_uar;
809         rxq_data->cqn = cq_info.cqn;
810         rxq_data->cq_arm_sn = 0;
811         /* Update doorbell counter. */
812         rxq_data->rq_ci = (1 << rxq_data->elts_n) >> rxq_data->sges_n;
813         rte_wmb();
814         *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
815         DEBUG("%p: rxq updated with %p", (void *)rxq_ctrl, (void *)&tmpl);
816         rte_atomic32_inc(&tmpl->refcnt);
817         DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
818               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
819         LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
820         return tmpl;
821 error:
822         if (tmpl->wq)
823                 claim_zero(ibv_destroy_wq(tmpl->wq));
824         if (tmpl->cq)
825                 claim_zero(ibv_destroy_cq(tmpl->cq));
826         if (tmpl->channel)
827                 claim_zero(ibv_destroy_comp_channel(tmpl->channel));
828         if (tmpl->mr)
829                 priv_mr_release(priv, tmpl->mr);
830         return NULL;
831 }
832
833 /**
834  * Get an Rx queue Verbs object.
835  *
836  * @param priv
837  *   Pointer to private structure.
838  * @param idx
839  *   Queue index in DPDK Rx queue array
840  *
841  * @return
842  *   The Verbs object if it exists.
843  */
844 struct mlx5_rxq_ibv*
845 mlx5_priv_rxq_ibv_get(struct priv *priv, uint16_t idx)
846 {
847         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
848         struct mlx5_rxq_ctrl *rxq_ctrl;
849
850         if (idx >= priv->rxqs_n)
851                 return NULL;
852         if (!rxq_data)
853                 return NULL;
854         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
855         if (rxq_ctrl->ibv) {
856                 priv_mr_get(priv, rxq_data->mp);
857                 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
858                 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
859                       (void *)rxq_ctrl->ibv,
860                       rte_atomic32_read(&rxq_ctrl->ibv->refcnt));
861         }
862         return rxq_ctrl->ibv;
863 }
864
865 /**
866  * Release an Rx verbs queue object.
867  *
868  * @param priv
869  *   Pointer to private structure.
870  * @param rxq_ibv
871  *   Verbs Rx queue object.
872  *
873  * @return
874  *   0 on success, errno value on failure.
875  */
876 int
877 mlx5_priv_rxq_ibv_release(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
878 {
879         int ret;
880
881         assert(rxq_ibv);
882         assert(rxq_ibv->wq);
883         assert(rxq_ibv->cq);
884         assert(rxq_ibv->mr);
885         ret = priv_mr_release(priv, rxq_ibv->mr);
886         if (!ret)
887                 rxq_ibv->mr = NULL;
888         DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
889               (void *)rxq_ibv, rte_atomic32_read(&rxq_ibv->refcnt));
890         if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
891                 rxq_free_elts(rxq_ibv->rxq_ctrl);
892                 claim_zero(ibv_destroy_wq(rxq_ibv->wq));
893                 claim_zero(ibv_destroy_cq(rxq_ibv->cq));
894                 if (rxq_ibv->channel)
895                         claim_zero(ibv_destroy_comp_channel(rxq_ibv->channel));
896                 LIST_REMOVE(rxq_ibv, next);
897                 rte_free(rxq_ibv);
898                 return 0;
899         }
900         return EBUSY;
901 }
902
903 /**
904  * Verify the Verbs Rx queue list is empty
905  *
906  * @param priv
907  *  Pointer to private structure.
908  *
909  * @return the number of object not released.
910  */
911 int
912 mlx5_priv_rxq_ibv_verify(struct priv *priv)
913 {
914         int ret = 0;
915         struct mlx5_rxq_ibv *rxq_ibv;
916
917         LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
918                 DEBUG("%p: Verbs Rx queue %p still referenced", (void *)priv,
919                       (void *)rxq_ibv);
920                 ++ret;
921         }
922         return ret;
923 }
924
925 /**
926  * Return true if a single reference exists on the object.
927  *
928  * @param priv
929  *   Pointer to private structure.
930  * @param rxq_ibv
931  *   Verbs Rx queue object.
932  */
933 int
934 mlx5_priv_rxq_ibv_releasable(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
935 {
936         (void)priv;
937         assert(rxq_ibv);
938         return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
939 }
940
941 /**
942  * Create a DPDK Rx queue.
943  *
944  * @param priv
945  *   Pointer to private structure.
946  * @param idx
947  *   TX queue index.
948  * @param desc
949  *   Number of descriptors to configure in queue.
950  * @param socket
951  *   NUMA socket on which memory must be allocated.
952  *
953  * @return
954  *   A DPDK queue object on success.
955  */
956 struct mlx5_rxq_ctrl*
957 mlx5_priv_rxq_new(struct priv *priv, uint16_t idx, uint16_t desc,
958                   unsigned int socket, const struct rte_eth_rxconf *conf,
959                   struct rte_mempool *mp)
960 {
961         struct rte_eth_dev *dev = priv->dev;
962         struct mlx5_rxq_ctrl *tmpl;
963         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
964         struct mlx5_dev_config *config = &priv->config;
965         /*
966          * Always allocate extra slots, even if eventually
967          * the vector Rx will not be used.
968          */
969         const uint16_t desc_n =
970                 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
971
972         tmpl = rte_calloc_socket("RXQ", 1,
973                                  sizeof(*tmpl) +
974                                  desc_n * sizeof(struct rte_mbuf *),
975                                  0, socket);
976         if (!tmpl)
977                 return NULL;
978         tmpl->socket = socket;
979         if (priv->dev->data->dev_conf.intr_conf.rxq)
980                 tmpl->irq = 1;
981         /* Enable scattered packets support for this queue if necessary. */
982         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
983         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
984             (mb_len - RTE_PKTMBUF_HEADROOM)) {
985                 tmpl->rxq.sges_n = 0;
986         } else if (conf->offloads & DEV_RX_OFFLOAD_SCATTER) {
987                 unsigned int size =
988                         RTE_PKTMBUF_HEADROOM +
989                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
990                 unsigned int sges_n;
991
992                 /*
993                  * Determine the number of SGEs needed for a full packet
994                  * and round it to the next power of two.
995                  */
996                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
997                 tmpl->rxq.sges_n = sges_n;
998                 /* Make sure rxq.sges_n did not overflow. */
999                 size = mb_len * (1 << tmpl->rxq.sges_n);
1000                 size -= RTE_PKTMBUF_HEADROOM;
1001                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
1002                         ERROR("%p: too many SGEs (%u) needed to handle"
1003                               " requested maximum packet size %u",
1004                               (void *)dev,
1005                               1 << sges_n,
1006                               dev->data->dev_conf.rxmode.max_rx_pkt_len);
1007                         goto error;
1008                 }
1009         } else {
1010                 WARN("%p: the requested maximum Rx packet size (%u) is"
1011                      " larger than a single mbuf (%u) and scattered"
1012                      " mode has not been requested",
1013                      (void *)dev,
1014                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
1015                      mb_len - RTE_PKTMBUF_HEADROOM);
1016         }
1017         DEBUG("%p: maximum number of segments per packet: %u",
1018               (void *)dev, 1 << tmpl->rxq.sges_n);
1019         if (desc % (1 << tmpl->rxq.sges_n)) {
1020                 ERROR("%p: number of RX queue descriptors (%u) is not a"
1021                       " multiple of SGEs per packet (%u)",
1022                       (void *)dev,
1023                       desc,
1024                       1 << tmpl->rxq.sges_n);
1025                 goto error;
1026         }
1027         /* Toggle RX checksum offload if hardware supports it. */
1028         tmpl->rxq.csum = !!(conf->offloads & DEV_RX_OFFLOAD_CHECKSUM);
1029         tmpl->rxq.csum_l2tun = (!!(conf->offloads & DEV_RX_OFFLOAD_CHECKSUM) &&
1030                                 priv->config.hw_csum_l2tun);
1031         tmpl->rxq.hw_timestamp = !!(conf->offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1032         /* Configure VLAN stripping. */
1033         tmpl->rxq.vlan_strip = !!(conf->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1034         /* By default, FCS (CRC) is stripped by hardware. */
1035         if (conf->offloads & DEV_RX_OFFLOAD_CRC_STRIP) {
1036                 tmpl->rxq.crc_present = 0;
1037         } else if (config->hw_fcs_strip) {
1038                 tmpl->rxq.crc_present = 1;
1039         } else {
1040                 WARN("%p: CRC stripping has been disabled but will still"
1041                      " be performed by hardware, make sure MLNX_OFED and"
1042                      " firmware are up to date",
1043                      (void *)dev);
1044                 tmpl->rxq.crc_present = 0;
1045         }
1046         DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
1047               " incoming frames to hide it",
1048               (void *)dev,
1049               tmpl->rxq.crc_present ? "disabled" : "enabled",
1050               tmpl->rxq.crc_present << 2);
1051         /* Save port ID. */
1052         tmpl->rxq.rss_hash = priv->rxqs_n > 1;
1053         tmpl->rxq.port_id = dev->data->port_id;
1054         tmpl->priv = priv;
1055         tmpl->rxq.mp = mp;
1056         tmpl->rxq.stats.idx = idx;
1057         tmpl->rxq.elts_n = log2above(desc);
1058         tmpl->rxq.elts =
1059                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1060         rte_atomic32_inc(&tmpl->refcnt);
1061         DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1062               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
1063         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1064         return tmpl;
1065 error:
1066         rte_free(tmpl);
1067         return NULL;
1068 }
1069
1070 /**
1071  * Get a Rx queue.
1072  *
1073  * @param priv
1074  *   Pointer to private structure.
1075  * @param idx
1076  *   TX queue index.
1077  *
1078  * @return
1079  *   A pointer to the queue if it exists.
1080  */
1081 struct mlx5_rxq_ctrl*
1082 mlx5_priv_rxq_get(struct priv *priv, uint16_t idx)
1083 {
1084         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1085
1086         if ((*priv->rxqs)[idx]) {
1087                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1088                                         struct mlx5_rxq_ctrl,
1089                                         rxq);
1090
1091                 mlx5_priv_rxq_ibv_get(priv, idx);
1092                 rte_atomic32_inc(&rxq_ctrl->refcnt);
1093                 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1094                       (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1095         }
1096         return rxq_ctrl;
1097 }
1098
1099 /**
1100  * Release a Rx queue.
1101  *
1102  * @param priv
1103  *   Pointer to private structure.
1104  * @param idx
1105  *   TX queue index.
1106  *
1107  * @return
1108  *   0 on success, errno value on failure.
1109  */
1110 int
1111 mlx5_priv_rxq_release(struct priv *priv, uint16_t idx)
1112 {
1113         struct mlx5_rxq_ctrl *rxq_ctrl;
1114
1115         if (!(*priv->rxqs)[idx])
1116                 return 0;
1117         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1118         assert(rxq_ctrl->priv);
1119         if (rxq_ctrl->ibv) {
1120                 int ret;
1121
1122                 ret = mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
1123                 if (!ret)
1124                         rxq_ctrl->ibv = NULL;
1125         }
1126         DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1127               (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1128         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1129                 LIST_REMOVE(rxq_ctrl, next);
1130                 rte_free(rxq_ctrl);
1131                 (*priv->rxqs)[idx] = NULL;
1132                 return 0;
1133         }
1134         return EBUSY;
1135 }
1136
1137 /**
1138  * Verify if the queue can be released.
1139  *
1140  * @param priv
1141  *   Pointer to private structure.
1142  * @param idx
1143  *   TX queue index.
1144  *
1145  * @return
1146  *   1 if the queue can be released.
1147  */
1148 int
1149 mlx5_priv_rxq_releasable(struct priv *priv, uint16_t idx)
1150 {
1151         struct mlx5_rxq_ctrl *rxq_ctrl;
1152
1153         if (!(*priv->rxqs)[idx])
1154                 return -1;
1155         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1156         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1157 }
1158
1159 /**
1160  * Verify the Rx Queue list is empty
1161  *
1162  * @param priv
1163  *  Pointer to private structure.
1164  *
1165  * @return the number of object not released.
1166  */
1167 int
1168 mlx5_priv_rxq_verify(struct priv *priv)
1169 {
1170         struct mlx5_rxq_ctrl *rxq_ctrl;
1171         int ret = 0;
1172
1173         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1174                 DEBUG("%p: Rx Queue %p still referenced", (void *)priv,
1175                       (void *)rxq_ctrl);
1176                 ++ret;
1177         }
1178         return ret;
1179 }
1180
1181 /**
1182  * Create an indirection table.
1183  *
1184  * @param priv
1185  *   Pointer to private structure.
1186  * @param queues
1187  *   Queues entering in the indirection table.
1188  * @param queues_n
1189  *   Number of queues in the array.
1190  *
1191  * @return
1192  *   A new indirection table.
1193  */
1194 struct mlx5_ind_table_ibv*
1195 mlx5_priv_ind_table_ibv_new(struct priv *priv, uint16_t queues[],
1196                             uint16_t queues_n)
1197 {
1198         struct mlx5_ind_table_ibv *ind_tbl;
1199         const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1200                 log2above(queues_n) :
1201                 log2above(priv->ind_table_max_size);
1202         struct ibv_wq *wq[1 << wq_n];
1203         unsigned int i;
1204         unsigned int j;
1205
1206         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1207                              queues_n * sizeof(uint16_t), 0);
1208         if (!ind_tbl)
1209                 return NULL;
1210         for (i = 0; i != queues_n; ++i) {
1211                 struct mlx5_rxq_ctrl *rxq =
1212                         mlx5_priv_rxq_get(priv, queues[i]);
1213
1214                 if (!rxq)
1215                         goto error;
1216                 wq[i] = rxq->ibv->wq;
1217                 ind_tbl->queues[i] = queues[i];
1218         }
1219         ind_tbl->queues_n = queues_n;
1220         /* Finalise indirection table. */
1221         for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1222                 wq[i] = wq[j];
1223         ind_tbl->ind_table = ibv_create_rwq_ind_table(
1224                 priv->ctx,
1225                 &(struct ibv_rwq_ind_table_init_attr){
1226                         .log_ind_tbl_size = wq_n,
1227                         .ind_tbl = wq,
1228                         .comp_mask = 0,
1229                 });
1230         if (!ind_tbl->ind_table)
1231                 goto error;
1232         rte_atomic32_inc(&ind_tbl->refcnt);
1233         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1234         DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1235               (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1236         return ind_tbl;
1237 error:
1238         rte_free(ind_tbl);
1239         DEBUG("%p cannot create indirection table", (void *)priv);
1240         return NULL;
1241 }
1242
1243 /**
1244  * Get an indirection table.
1245  *
1246  * @param priv
1247  *   Pointer to private structure.
1248  * @param queues
1249  *   Queues entering in the indirection table.
1250  * @param queues_n
1251  *   Number of queues in the array.
1252  *
1253  * @return
1254  *   An indirection table if found.
1255  */
1256 struct mlx5_ind_table_ibv*
1257 mlx5_priv_ind_table_ibv_get(struct priv *priv, uint16_t queues[],
1258                             uint16_t queues_n)
1259 {
1260         struct mlx5_ind_table_ibv *ind_tbl;
1261
1262         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1263                 if ((ind_tbl->queues_n == queues_n) &&
1264                     (memcmp(ind_tbl->queues, queues,
1265                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1266                      == 0))
1267                         break;
1268         }
1269         if (ind_tbl) {
1270                 unsigned int i;
1271
1272                 rte_atomic32_inc(&ind_tbl->refcnt);
1273                 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1274                       (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1275                 for (i = 0; i != ind_tbl->queues_n; ++i)
1276                         mlx5_priv_rxq_get(priv, ind_tbl->queues[i]);
1277         }
1278         return ind_tbl;
1279 }
1280
1281 /**
1282  * Release an indirection table.
1283  *
1284  * @param priv
1285  *   Pointer to private structure.
1286  * @param ind_table
1287  *   Indirection table to release.
1288  *
1289  * @return
1290  *   0 on success, errno value on failure.
1291  */
1292 int
1293 mlx5_priv_ind_table_ibv_release(struct priv *priv,
1294                                 struct mlx5_ind_table_ibv *ind_tbl)
1295 {
1296         unsigned int i;
1297
1298         DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1299               (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1300         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1301                 claim_zero(ibv_destroy_rwq_ind_table(ind_tbl->ind_table));
1302         for (i = 0; i != ind_tbl->queues_n; ++i)
1303                 claim_nonzero(mlx5_priv_rxq_release(priv, ind_tbl->queues[i]));
1304         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1305                 LIST_REMOVE(ind_tbl, next);
1306                 rte_free(ind_tbl);
1307                 return 0;
1308         }
1309         return EBUSY;
1310 }
1311
1312 /**
1313  * Verify the Rx Queue list is empty
1314  *
1315  * @param priv
1316  *  Pointer to private structure.
1317  *
1318  * @return the number of object not released.
1319  */
1320 int
1321 mlx5_priv_ind_table_ibv_verify(struct priv *priv)
1322 {
1323         struct mlx5_ind_table_ibv *ind_tbl;
1324         int ret = 0;
1325
1326         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1327                 DEBUG("%p: Verbs indirection table %p still referenced",
1328                       (void *)priv, (void *)ind_tbl);
1329                 ++ret;
1330         }
1331         return ret;
1332 }
1333
1334 /**
1335  * Create an Rx Hash queue.
1336  *
1337  * @param priv
1338  *   Pointer to private structure.
1339  * @param rss_key
1340  *   RSS key for the Rx hash queue.
1341  * @param rss_key_len
1342  *   RSS key length.
1343  * @param hash_fields
1344  *   Verbs protocol hash field to make the RSS on.
1345  * @param queues
1346  *   Queues entering in hash queue. In case of empty hash_fields only the
1347  *   first queue index will be taken for the indirection table.
1348  * @param queues_n
1349  *   Number of queues.
1350  *
1351  * @return
1352  *   An hash Rx queue on success.
1353  */
1354 struct mlx5_hrxq*
1355 mlx5_priv_hrxq_new(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1356                    uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1357 {
1358         struct mlx5_hrxq *hrxq;
1359         struct mlx5_ind_table_ibv *ind_tbl;
1360         struct ibv_qp *qp;
1361
1362         queues_n = hash_fields ? queues_n : 1;
1363         ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1364         if (!ind_tbl)
1365                 ind_tbl = mlx5_priv_ind_table_ibv_new(priv, queues, queues_n);
1366         if (!ind_tbl)
1367                 return NULL;
1368         qp = ibv_create_qp_ex(
1369                 priv->ctx,
1370                 &(struct ibv_qp_init_attr_ex){
1371                         .qp_type = IBV_QPT_RAW_PACKET,
1372                         .comp_mask =
1373                                 IBV_QP_INIT_ATTR_PD |
1374                                 IBV_QP_INIT_ATTR_IND_TABLE |
1375                                 IBV_QP_INIT_ATTR_RX_HASH,
1376                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1377                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1378                                 .rx_hash_key_len = rss_key_len,
1379                                 .rx_hash_key = rss_key,
1380                                 .rx_hash_fields_mask = hash_fields,
1381                         },
1382                         .rwq_ind_tbl = ind_tbl->ind_table,
1383                         .pd = priv->pd,
1384                 });
1385         if (!qp)
1386                 goto error;
1387         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1388         if (!hrxq)
1389                 goto error;
1390         hrxq->ind_table = ind_tbl;
1391         hrxq->qp = qp;
1392         hrxq->rss_key_len = rss_key_len;
1393         hrxq->hash_fields = hash_fields;
1394         memcpy(hrxq->rss_key, rss_key, rss_key_len);
1395         rte_atomic32_inc(&hrxq->refcnt);
1396         LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1397         DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1398               (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1399         return hrxq;
1400 error:
1401         mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1402         if (qp)
1403                 claim_zero(ibv_destroy_qp(qp));
1404         return NULL;
1405 }
1406
1407 /**
1408  * Get an Rx Hash queue.
1409  *
1410  * @param priv
1411  *   Pointer to private structure.
1412  * @param rss_conf
1413  *   RSS configuration for the Rx hash queue.
1414  * @param queues
1415  *   Queues entering in hash queue. In case of empty hash_fields only the
1416  *   first queue index will be taken for the indirection table.
1417  * @param queues_n
1418  *   Number of queues.
1419  *
1420  * @return
1421  *   An hash Rx queue on success.
1422  */
1423 struct mlx5_hrxq*
1424 mlx5_priv_hrxq_get(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1425                    uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1426 {
1427         struct mlx5_hrxq *hrxq;
1428
1429         queues_n = hash_fields ? queues_n : 1;
1430         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1431                 struct mlx5_ind_table_ibv *ind_tbl;
1432
1433                 if (hrxq->rss_key_len != rss_key_len)
1434                         continue;
1435                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1436                         continue;
1437                 if (hrxq->hash_fields != hash_fields)
1438                         continue;
1439                 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1440                 if (!ind_tbl)
1441                         continue;
1442                 if (ind_tbl != hrxq->ind_table) {
1443                         mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1444                         continue;
1445                 }
1446                 rte_atomic32_inc(&hrxq->refcnt);
1447                 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1448                       (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1449                 return hrxq;
1450         }
1451         return NULL;
1452 }
1453
1454 /**
1455  * Release the hash Rx queue.
1456  *
1457  * @param priv
1458  *   Pointer to private structure.
1459  * @param hrxq
1460  *   Pointer to Hash Rx queue to release.
1461  *
1462  * @return
1463  *   0 on success, errno value on failure.
1464  */
1465 int
1466 mlx5_priv_hrxq_release(struct priv *priv, struct mlx5_hrxq *hrxq)
1467 {
1468         DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1469               (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1470         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1471                 claim_zero(ibv_destroy_qp(hrxq->qp));
1472                 mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table);
1473                 LIST_REMOVE(hrxq, next);
1474                 rte_free(hrxq);
1475                 return 0;
1476         }
1477         claim_nonzero(mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table));
1478         return EBUSY;
1479 }
1480
1481 /**
1482  * Verify the Rx Queue list is empty
1483  *
1484  * @param priv
1485  *  Pointer to private structure.
1486  *
1487  * @return the number of object not released.
1488  */
1489 int
1490 mlx5_priv_hrxq_ibv_verify(struct priv *priv)
1491 {
1492         struct mlx5_hrxq *hrxq;
1493         int ret = 0;
1494
1495         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1496                 DEBUG("%p: Verbs Hash Rx queue %p still referenced",
1497                       (void *)priv, (void *)hrxq);
1498                 ++ret;
1499         }
1500         return ret;
1501 }