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