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