net/mlx5: fix Multi-Packet RQ mempool free
[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 Technologies, Ltd
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 static_assert(MLX5_RSS_HASH_KEY_LEN ==
56               (unsigned int)sizeof(rss_hash_default_key),
57               "wrong RSS default key size.");
58
59 /**
60  * Check whether Multi-Packet RQ can be enabled for the device.
61  *
62  * @param dev
63  *   Pointer to Ethernet device.
64  *
65  * @return
66  *   1 if supported, negative errno value if not.
67  */
68 inline int
69 mlx5_check_mprq_support(struct rte_eth_dev *dev)
70 {
71         struct priv *priv = dev->data->dev_private;
72
73         if (priv->config.mprq.enabled &&
74             priv->rxqs_n >= priv->config.mprq.min_rxqs_num)
75                 return 1;
76         return -ENOTSUP;
77 }
78
79 /**
80  * Check whether Multi-Packet RQ is enabled for the Rx queue.
81  *
82  *  @param rxq
83  *     Pointer to receive queue structure.
84  *
85  * @return
86  *   0 if disabled, otherwise enabled.
87  */
88 inline int
89 mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq)
90 {
91         return rxq->strd_num_n > 0;
92 }
93
94 /**
95  * Check whether Multi-Packet RQ is enabled for the device.
96  *
97  * @param dev
98  *   Pointer to Ethernet device.
99  *
100  * @return
101  *   0 if disabled, otherwise enabled.
102  */
103 inline int
104 mlx5_mprq_enabled(struct rte_eth_dev *dev)
105 {
106         struct priv *priv = dev->data->dev_private;
107         uint16_t i;
108         uint16_t n = 0;
109
110         if (mlx5_check_mprq_support(dev) < 0)
111                 return 0;
112         /* All the configured queues should be enabled. */
113         for (i = 0; i < priv->rxqs_n; ++i) {
114                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
115
116                 if (!rxq)
117                         continue;
118                 if (mlx5_rxq_mprq_enabled(rxq))
119                         ++n;
120         }
121         /* Multi-Packet RQ can't be partially configured. */
122         assert(n == 0 || n == priv->rxqs_n);
123         return n == priv->rxqs_n;
124 }
125
126 /**
127  * Allocate RX queue elements for Multi-Packet RQ.
128  *
129  * @param rxq_ctrl
130  *   Pointer to RX queue structure.
131  *
132  * @return
133  *   0 on success, a negative errno value otherwise and rte_errno is set.
134  */
135 static int
136 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
137 {
138         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
139         unsigned int wqe_n = 1 << rxq->elts_n;
140         unsigned int i;
141         int err;
142
143         /* Iterate on segments. */
144         for (i = 0; i <= wqe_n; ++i) {
145                 struct mlx5_mprq_buf *buf;
146
147                 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
148                         DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
149                         rte_errno = ENOMEM;
150                         goto error;
151                 }
152                 if (i < wqe_n)
153                         (*rxq->mprq_bufs)[i] = buf;
154                 else
155                         rxq->mprq_repl = buf;
156         }
157         DRV_LOG(DEBUG,
158                 "port %u Rx queue %u allocated and configured %u segments",
159                 rxq->port_id, rxq_ctrl->idx, wqe_n);
160         return 0;
161 error:
162         err = rte_errno; /* Save rte_errno before cleanup. */
163         wqe_n = i;
164         for (i = 0; (i != wqe_n); ++i) {
165                 if ((*rxq->mprq_bufs)[i] != NULL)
166                         rte_mempool_put(rxq->mprq_mp,
167                                         (*rxq->mprq_bufs)[i]);
168                 (*rxq->mprq_bufs)[i] = NULL;
169         }
170         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
171                 rxq->port_id, rxq_ctrl->idx);
172         rte_errno = err; /* Restore rte_errno. */
173         return -rte_errno;
174 }
175
176 /**
177  * Allocate RX queue elements for Single-Packet RQ.
178  *
179  * @param rxq_ctrl
180  *   Pointer to RX queue structure.
181  *
182  * @return
183  *   0 on success, errno value on failure.
184  */
185 static int
186 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
187 {
188         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
189         unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
190         unsigned int i;
191         int err;
192
193         /* Iterate on segments. */
194         for (i = 0; (i != elts_n); ++i) {
195                 struct rte_mbuf *buf;
196
197                 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
198                 if (buf == NULL) {
199                         DRV_LOG(ERR, "port %u empty mbuf pool",
200                                 PORT_ID(rxq_ctrl->priv));
201                         rte_errno = ENOMEM;
202                         goto error;
203                 }
204                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
205                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
206                 /* Buffer is supposed to be empty. */
207                 assert(rte_pktmbuf_data_len(buf) == 0);
208                 assert(rte_pktmbuf_pkt_len(buf) == 0);
209                 assert(!buf->next);
210                 /* Only the first segment keeps headroom. */
211                 if (i % sges_n)
212                         SET_DATA_OFF(buf, 0);
213                 PORT(buf) = rxq_ctrl->rxq.port_id;
214                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
215                 PKT_LEN(buf) = DATA_LEN(buf);
216                 NB_SEGS(buf) = 1;
217                 (*rxq_ctrl->rxq.elts)[i] = buf;
218         }
219         /* If Rx vector is activated. */
220         if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
221                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
222                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
223                 int j;
224
225                 /* Initialize default rearm_data for vPMD. */
226                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
227                 rte_mbuf_refcnt_set(mbuf_init, 1);
228                 mbuf_init->nb_segs = 1;
229                 mbuf_init->port = rxq->port_id;
230                 /*
231                  * prevent compiler reordering:
232                  * rearm_data covers previous fields.
233                  */
234                 rte_compiler_barrier();
235                 rxq->mbuf_initializer =
236                         *(uint64_t *)&mbuf_init->rearm_data;
237                 /* Padding with a fake mbuf for vectorized Rx. */
238                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
239                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
240         }
241         DRV_LOG(DEBUG,
242                 "port %u Rx queue %u allocated and configured %u segments"
243                 " (max %u packets)",
244                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx, elts_n,
245                 elts_n / (1 << rxq_ctrl->rxq.sges_n));
246         return 0;
247 error:
248         err = rte_errno; /* Save rte_errno before cleanup. */
249         elts_n = i;
250         for (i = 0; (i != elts_n); ++i) {
251                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
252                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
253                 (*rxq_ctrl->rxq.elts)[i] = NULL;
254         }
255         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
256                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
257         rte_errno = err; /* Restore rte_errno. */
258         return -rte_errno;
259 }
260
261 /**
262  * Allocate RX queue elements.
263  *
264  * @param rxq_ctrl
265  *   Pointer to RX queue structure.
266  *
267  * @return
268  *   0 on success, errno value on failure.
269  */
270 int
271 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
272 {
273         return mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
274                rxq_alloc_elts_mprq(rxq_ctrl) : rxq_alloc_elts_sprq(rxq_ctrl);
275 }
276
277 /**
278  * Free RX queue elements for Multi-Packet RQ.
279  *
280  * @param rxq_ctrl
281  *   Pointer to RX queue structure.
282  */
283 static void
284 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
285 {
286         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
287         uint16_t i;
288
289         DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing WRs",
290                 rxq->port_id, rxq_ctrl->idx);
291         if (rxq->mprq_bufs == NULL)
292                 return;
293         assert(mlx5_rxq_check_vec_support(rxq) < 0);
294         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
295                 if ((*rxq->mprq_bufs)[i] != NULL)
296                         mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
297                 (*rxq->mprq_bufs)[i] = NULL;
298         }
299         if (rxq->mprq_repl != NULL) {
300                 mlx5_mprq_buf_free(rxq->mprq_repl);
301                 rxq->mprq_repl = NULL;
302         }
303 }
304
305 /**
306  * Free RX queue elements for Single-Packet RQ.
307  *
308  * @param rxq_ctrl
309  *   Pointer to RX queue structure.
310  */
311 static void
312 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
313 {
314         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
315         const uint16_t q_n = (1 << rxq->elts_n);
316         const uint16_t q_mask = q_n - 1;
317         uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
318         uint16_t i;
319
320         DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
321                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
322         if (rxq->elts == NULL)
323                 return;
324         /**
325          * Some mbuf in the Ring belongs to the application.  They cannot be
326          * freed.
327          */
328         if (mlx5_rxq_check_vec_support(rxq) > 0) {
329                 for (i = 0; i < used; ++i)
330                         (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
331                 rxq->rq_pi = rxq->rq_ci;
332         }
333         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
334                 if ((*rxq->elts)[i] != NULL)
335                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
336                 (*rxq->elts)[i] = NULL;
337         }
338 }
339
340 /**
341  * Free RX queue elements.
342  *
343  * @param rxq_ctrl
344  *   Pointer to RX queue structure.
345  */
346 static void
347 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
348 {
349         if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
350                 rxq_free_elts_mprq(rxq_ctrl);
351         else
352                 rxq_free_elts_sprq(rxq_ctrl);
353 }
354
355 /**
356  * Clean up a RX queue.
357  *
358  * Destroy objects, free allocated memory and reset the structure for reuse.
359  *
360  * @param rxq_ctrl
361  *   Pointer to RX queue structure.
362  */
363 void
364 mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *rxq_ctrl)
365 {
366         DRV_LOG(DEBUG, "port %u cleaning up Rx queue %u",
367                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
368         if (rxq_ctrl->ibv)
369                 mlx5_rxq_ibv_release(rxq_ctrl->ibv);
370         memset(rxq_ctrl, 0, sizeof(*rxq_ctrl));
371 }
372
373 /**
374  * Returns the per-queue supported offloads.
375  *
376  * @param dev
377  *   Pointer to Ethernet device.
378  *
379  * @return
380  *   Supported Rx offloads.
381  */
382 uint64_t
383 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
384 {
385         struct priv *priv = dev->data->dev_private;
386         struct mlx5_dev_config *config = &priv->config;
387         uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
388                              DEV_RX_OFFLOAD_TIMESTAMP |
389                              DEV_RX_OFFLOAD_JUMBO_FRAME);
390
391         if (config->hw_fcs_strip)
392                 offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
393
394         if (config->hw_csum)
395                 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
396                              DEV_RX_OFFLOAD_UDP_CKSUM |
397                              DEV_RX_OFFLOAD_TCP_CKSUM);
398         if (config->hw_vlan_strip)
399                 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
400         return offloads;
401 }
402
403
404 /**
405  * Returns the per-port supported offloads.
406  *
407  * @return
408  *   Supported Rx offloads.
409  */
410 uint64_t
411 mlx5_get_rx_port_offloads(void)
412 {
413         uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
414
415         return offloads;
416 }
417
418 /**
419  *
420  * @param dev
421  *   Pointer to Ethernet device structure.
422  * @param idx
423  *   RX queue index.
424  * @param desc
425  *   Number of descriptors to configure in queue.
426  * @param socket
427  *   NUMA socket on which memory must be allocated.
428  * @param[in] conf
429  *   Thresholds parameters.
430  * @param mp
431  *   Memory pool for buffer allocations.
432  *
433  * @return
434  *   0 on success, a negative errno value otherwise and rte_errno is set.
435  */
436 int
437 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
438                     unsigned int socket, const struct rte_eth_rxconf *conf,
439                     struct rte_mempool *mp)
440 {
441         struct priv *priv = dev->data->dev_private;
442         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
443         struct mlx5_rxq_ctrl *rxq_ctrl =
444                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
445
446         if (!rte_is_power_of_2(desc)) {
447                 desc = 1 << log2above(desc);
448                 DRV_LOG(WARNING,
449                         "port %u increased number of descriptors in Rx queue %u"
450                         " to the next power of two (%d)",
451                         dev->data->port_id, idx, desc);
452         }
453         DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
454                 dev->data->port_id, idx, desc);
455         if (idx >= priv->rxqs_n) {
456                 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
457                         dev->data->port_id, idx, priv->rxqs_n);
458                 rte_errno = EOVERFLOW;
459                 return -rte_errno;
460         }
461         if (!mlx5_rxq_releasable(dev, idx)) {
462                 DRV_LOG(ERR, "port %u unable to release queue index %u",
463                         dev->data->port_id, idx);
464                 rte_errno = EBUSY;
465                 return -rte_errno;
466         }
467         mlx5_rxq_release(dev, idx);
468         rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, mp);
469         if (!rxq_ctrl) {
470                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
471                         dev->data->port_id, idx);
472                 rte_errno = ENOMEM;
473                 return -rte_errno;
474         }
475         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
476                 dev->data->port_id, idx);
477         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
478         return 0;
479 }
480
481 /**
482  * DPDK callback to release a RX queue.
483  *
484  * @param dpdk_rxq
485  *   Generic RX queue pointer.
486  */
487 void
488 mlx5_rx_queue_release(void *dpdk_rxq)
489 {
490         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
491         struct mlx5_rxq_ctrl *rxq_ctrl;
492         struct priv *priv;
493
494         if (rxq == NULL)
495                 return;
496         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
497         priv = rxq_ctrl->priv;
498         if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.stats.idx))
499                 rte_panic("port %u Rx queue %u is still used by a flow and"
500                           " cannot be removed\n",
501                           PORT_ID(priv), rxq_ctrl->idx);
502         mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.stats.idx);
503 }
504
505 /**
506  * Allocate queue vector and fill epoll fd list for Rx interrupts.
507  *
508  * @param dev
509  *   Pointer to Ethernet device.
510  *
511  * @return
512  *   0 on success, a negative errno value otherwise and rte_errno is set.
513  */
514 int
515 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
516 {
517         struct priv *priv = dev->data->dev_private;
518         unsigned int i;
519         unsigned int rxqs_n = priv->rxqs_n;
520         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
521         unsigned int count = 0;
522         struct rte_intr_handle *intr_handle = dev->intr_handle;
523
524         if (!dev->data->dev_conf.intr_conf.rxq)
525                 return 0;
526         mlx5_rx_intr_vec_disable(dev);
527         intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
528         if (intr_handle->intr_vec == NULL) {
529                 DRV_LOG(ERR,
530                         "port %u failed to allocate memory for interrupt"
531                         " vector, Rx interrupts will not be supported",
532                         dev->data->port_id);
533                 rte_errno = ENOMEM;
534                 return -rte_errno;
535         }
536         intr_handle->type = RTE_INTR_HANDLE_EXT;
537         for (i = 0; i != n; ++i) {
538                 /* This rxq ibv must not be released in this function. */
539                 struct mlx5_rxq_ibv *rxq_ibv = mlx5_rxq_ibv_get(dev, i);
540                 int fd;
541                 int flags;
542                 int rc;
543
544                 /* Skip queues that cannot request interrupts. */
545                 if (!rxq_ibv || !rxq_ibv->channel) {
546                         /* Use invalid intr_vec[] index to disable entry. */
547                         intr_handle->intr_vec[i] =
548                                 RTE_INTR_VEC_RXTX_OFFSET +
549                                 RTE_MAX_RXTX_INTR_VEC_ID;
550                         continue;
551                 }
552                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
553                         DRV_LOG(ERR,
554                                 "port %u too many Rx queues for interrupt"
555                                 " vector size (%d), Rx interrupts cannot be"
556                                 " enabled",
557                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
558                         mlx5_rx_intr_vec_disable(dev);
559                         rte_errno = ENOMEM;
560                         return -rte_errno;
561                 }
562                 fd = rxq_ibv->channel->fd;
563                 flags = fcntl(fd, F_GETFL);
564                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
565                 if (rc < 0) {
566                         rte_errno = errno;
567                         DRV_LOG(ERR,
568                                 "port %u failed to make Rx interrupt file"
569                                 " descriptor %d non-blocking for queue index"
570                                 " %d",
571                                 dev->data->port_id, fd, i);
572                         mlx5_rx_intr_vec_disable(dev);
573                         return -rte_errno;
574                 }
575                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
576                 intr_handle->efds[count] = fd;
577                 count++;
578         }
579         if (!count)
580                 mlx5_rx_intr_vec_disable(dev);
581         else
582                 intr_handle->nb_efd = count;
583         return 0;
584 }
585
586 /**
587  * Clean up Rx interrupts handler.
588  *
589  * @param dev
590  *   Pointer to Ethernet device.
591  */
592 void
593 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
594 {
595         struct priv *priv = dev->data->dev_private;
596         struct rte_intr_handle *intr_handle = dev->intr_handle;
597         unsigned int i;
598         unsigned int rxqs_n = priv->rxqs_n;
599         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
600
601         if (!dev->data->dev_conf.intr_conf.rxq)
602                 return;
603         if (!intr_handle->intr_vec)
604                 goto free;
605         for (i = 0; i != n; ++i) {
606                 struct mlx5_rxq_ctrl *rxq_ctrl;
607                 struct mlx5_rxq_data *rxq_data;
608
609                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
610                     RTE_MAX_RXTX_INTR_VEC_ID)
611                         continue;
612                 /**
613                  * Need to access directly the queue to release the reference
614                  * kept in priv_rx_intr_vec_enable().
615                  */
616                 rxq_data = (*priv->rxqs)[i];
617                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
618                 mlx5_rxq_ibv_release(rxq_ctrl->ibv);
619         }
620 free:
621         rte_intr_free_epoll_fd(intr_handle);
622         if (intr_handle->intr_vec)
623                 free(intr_handle->intr_vec);
624         intr_handle->nb_efd = 0;
625         intr_handle->intr_vec = NULL;
626 }
627
628 /**
629  *  MLX5 CQ notification .
630  *
631  *  @param rxq
632  *     Pointer to receive queue structure.
633  *  @param sq_n_rxq
634  *     Sequence number per receive queue .
635  */
636 static inline void
637 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
638 {
639         int sq_n = 0;
640         uint32_t doorbell_hi;
641         uint64_t doorbell;
642         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
643
644         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
645         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
646         doorbell = (uint64_t)doorbell_hi << 32;
647         doorbell |=  rxq->cqn;
648         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
649         mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
650                          cq_db_reg, rxq->uar_lock_cq);
651 }
652
653 /**
654  * DPDK callback for Rx queue interrupt enable.
655  *
656  * @param dev
657  *   Pointer to Ethernet device structure.
658  * @param rx_queue_id
659  *   Rx queue number.
660  *
661  * @return
662  *   0 on success, a negative errno value otherwise and rte_errno is set.
663  */
664 int
665 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
666 {
667         struct priv *priv = dev->data->dev_private;
668         struct mlx5_rxq_data *rxq_data;
669         struct mlx5_rxq_ctrl *rxq_ctrl;
670
671         rxq_data = (*priv->rxqs)[rx_queue_id];
672         if (!rxq_data) {
673                 rte_errno = EINVAL;
674                 return -rte_errno;
675         }
676         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
677         if (rxq_ctrl->irq) {
678                 struct mlx5_rxq_ibv *rxq_ibv;
679
680                 rxq_ibv = mlx5_rxq_ibv_get(dev, rx_queue_id);
681                 if (!rxq_ibv) {
682                         rte_errno = EINVAL;
683                         return -rte_errno;
684                 }
685                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
686                 mlx5_rxq_ibv_release(rxq_ibv);
687         }
688         return 0;
689 }
690
691 /**
692  * DPDK callback for Rx queue interrupt disable.
693  *
694  * @param dev
695  *   Pointer to Ethernet device structure.
696  * @param rx_queue_id
697  *   Rx queue number.
698  *
699  * @return
700  *   0 on success, a negative errno value otherwise and rte_errno is set.
701  */
702 int
703 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
704 {
705         struct priv *priv = dev->data->dev_private;
706         struct mlx5_rxq_data *rxq_data;
707         struct mlx5_rxq_ctrl *rxq_ctrl;
708         struct mlx5_rxq_ibv *rxq_ibv = NULL;
709         struct ibv_cq *ev_cq;
710         void *ev_ctx;
711         int ret;
712
713         rxq_data = (*priv->rxqs)[rx_queue_id];
714         if (!rxq_data) {
715                 rte_errno = EINVAL;
716                 return -rte_errno;
717         }
718         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
719         if (!rxq_ctrl->irq)
720                 return 0;
721         rxq_ibv = mlx5_rxq_ibv_get(dev, rx_queue_id);
722         if (!rxq_ibv) {
723                 rte_errno = EINVAL;
724                 return -rte_errno;
725         }
726         ret = mlx5_glue->get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
727         if (ret || ev_cq != rxq_ibv->cq) {
728                 rte_errno = EINVAL;
729                 goto exit;
730         }
731         rxq_data->cq_arm_sn++;
732         mlx5_glue->ack_cq_events(rxq_ibv->cq, 1);
733         return 0;
734 exit:
735         ret = rte_errno; /* Save rte_errno before cleanup. */
736         if (rxq_ibv)
737                 mlx5_rxq_ibv_release(rxq_ibv);
738         DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
739                 dev->data->port_id, rx_queue_id);
740         rte_errno = ret; /* Restore rte_errno. */
741         return -rte_errno;
742 }
743
744 /**
745  * Create the Rx queue Verbs object.
746  *
747  * @param dev
748  *   Pointer to Ethernet device.
749  * @param idx
750  *   Queue index in DPDK Rx queue array
751  *
752  * @return
753  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
754  */
755 struct mlx5_rxq_ibv *
756 mlx5_rxq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
757 {
758         struct priv *priv = dev->data->dev_private;
759         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
760         struct mlx5_rxq_ctrl *rxq_ctrl =
761                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
762         struct ibv_wq_attr mod;
763         union {
764                 struct {
765                         struct ibv_cq_init_attr_ex ibv;
766                         struct mlx5dv_cq_init_attr mlx5;
767                 } cq;
768                 struct {
769                         struct ibv_wq_init_attr ibv;
770 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
771                         struct mlx5dv_wq_init_attr mlx5;
772 #endif
773                 } wq;
774                 struct ibv_cq_ex cq_attr;
775         } attr;
776         unsigned int cqe_n;
777         unsigned int wqe_n = 1 << rxq_data->elts_n;
778         struct mlx5_rxq_ibv *tmpl;
779         struct mlx5dv_cq cq_info;
780         struct mlx5dv_rwq rwq;
781         unsigned int i;
782         int ret = 0;
783         struct mlx5dv_obj obj;
784         struct mlx5_dev_config *config = &priv->config;
785         const int mprq_en = mlx5_rxq_mprq_enabled(rxq_data);
786
787         assert(rxq_data);
788         assert(!rxq_ctrl->ibv);
789         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
790         priv->verbs_alloc_ctx.obj = rxq_ctrl;
791         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
792                                  rxq_ctrl->socket);
793         if (!tmpl) {
794                 DRV_LOG(ERR,
795                         "port %u Rx queue %u cannot allocate verbs resources",
796                         dev->data->port_id, rxq_ctrl->idx);
797                 rte_errno = ENOMEM;
798                 goto error;
799         }
800         tmpl->rxq_ctrl = rxq_ctrl;
801         if (rxq_ctrl->irq) {
802                 tmpl->channel = mlx5_glue->create_comp_channel(priv->ctx);
803                 if (!tmpl->channel) {
804                         DRV_LOG(ERR, "port %u: comp channel creation failure",
805                                 dev->data->port_id);
806                         rte_errno = ENOMEM;
807                         goto error;
808                 }
809         }
810         if (mprq_en)
811                 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
812         else
813                 cqe_n = wqe_n  - 1;
814         attr.cq.ibv = (struct ibv_cq_init_attr_ex){
815                 .cqe = cqe_n,
816                 .channel = tmpl->channel,
817                 .comp_mask = 0,
818         };
819         attr.cq.mlx5 = (struct mlx5dv_cq_init_attr){
820                 .comp_mask = 0,
821         };
822         if (config->cqe_comp && !rxq_data->hw_timestamp) {
823                 attr.cq.mlx5.comp_mask |=
824                         MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
825 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
826                 attr.cq.mlx5.cqe_comp_res_format =
827                         mprq_en ? MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
828                                   MLX5DV_CQE_RES_FORMAT_HASH;
829 #else
830                 attr.cq.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
831 #endif
832                 /*
833                  * For vectorized Rx, it must not be doubled in order to
834                  * make cq_ci and rq_ci aligned.
835                  */
836                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
837                         attr.cq.ibv.cqe *= 2;
838         } else if (config->cqe_comp && rxq_data->hw_timestamp) {
839                 DRV_LOG(DEBUG,
840                         "port %u Rx CQE compression is disabled for HW"
841                         " timestamp",
842                         dev->data->port_id);
843         }
844 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
845         if (config->cqe_pad) {
846                 attr.cq.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
847                 attr.cq.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
848         }
849 #endif
850         tmpl->cq = mlx5_glue->cq_ex_to_cq
851                 (mlx5_glue->dv_create_cq(priv->ctx, &attr.cq.ibv,
852                                          &attr.cq.mlx5));
853         if (tmpl->cq == NULL) {
854                 DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
855                         dev->data->port_id, idx);
856                 rte_errno = ENOMEM;
857                 goto error;
858         }
859         DRV_LOG(DEBUG, "port %u priv->device_attr.max_qp_wr is %d",
860                 dev->data->port_id, priv->device_attr.orig_attr.max_qp_wr);
861         DRV_LOG(DEBUG, "port %u priv->device_attr.max_sge is %d",
862                 dev->data->port_id, priv->device_attr.orig_attr.max_sge);
863         attr.wq.ibv = (struct ibv_wq_init_attr){
864                 .wq_context = NULL, /* Could be useful in the future. */
865                 .wq_type = IBV_WQT_RQ,
866                 /* Max number of outstanding WRs. */
867                 .max_wr = wqe_n >> rxq_data->sges_n,
868                 /* Max number of scatter/gather elements in a WR. */
869                 .max_sge = 1 << rxq_data->sges_n,
870                 .pd = priv->pd,
871                 .cq = tmpl->cq,
872                 .comp_mask =
873                         IBV_WQ_FLAGS_CVLAN_STRIPPING |
874                         0,
875                 .create_flags = (rxq_data->vlan_strip ?
876                                  IBV_WQ_FLAGS_CVLAN_STRIPPING :
877                                  0),
878         };
879         /* By default, FCS (CRC) is stripped by hardware. */
880         if (rxq_data->crc_present) {
881                 attr.wq.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
882                 attr.wq.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
883         }
884 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
885         if (config->hw_padding) {
886                 attr.wq.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
887                 attr.wq.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
888         }
889 #endif
890 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
891         attr.wq.mlx5 = (struct mlx5dv_wq_init_attr){
892                 .comp_mask = 0,
893         };
894         if (mprq_en) {
895                 struct mlx5dv_striding_rq_init_attr *mprq_attr =
896                         &attr.wq.mlx5.striding_rq_attrs;
897
898                 attr.wq.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
899                 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
900                         .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
901                         .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
902                         .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
903                 };
904         }
905         tmpl->wq = mlx5_glue->dv_create_wq(priv->ctx, &attr.wq.ibv,
906                                            &attr.wq.mlx5);
907 #else
908         tmpl->wq = mlx5_glue->create_wq(priv->ctx, &attr.wq.ibv);
909 #endif
910         if (tmpl->wq == NULL) {
911                 DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
912                         dev->data->port_id, idx);
913                 rte_errno = ENOMEM;
914                 goto error;
915         }
916         /*
917          * Make sure number of WRs*SGEs match expectations since a queue
918          * cannot allocate more than "desc" buffers.
919          */
920         if (attr.wq.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
921             attr.wq.ibv.max_sge != (1u << rxq_data->sges_n)) {
922                 DRV_LOG(ERR,
923                         "port %u Rx queue %u requested %u*%u but got %u*%u"
924                         " WRs*SGEs",
925                         dev->data->port_id, idx,
926                         wqe_n >> rxq_data->sges_n, (1 << rxq_data->sges_n),
927                         attr.wq.ibv.max_wr, attr.wq.ibv.max_sge);
928                 rte_errno = EINVAL;
929                 goto error;
930         }
931         /* Change queue state to ready. */
932         mod = (struct ibv_wq_attr){
933                 .attr_mask = IBV_WQ_ATTR_STATE,
934                 .wq_state = IBV_WQS_RDY,
935         };
936         ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
937         if (ret) {
938                 DRV_LOG(ERR,
939                         "port %u Rx queue %u WQ state to IBV_WQS_RDY failed",
940                         dev->data->port_id, idx);
941                 rte_errno = ret;
942                 goto error;
943         }
944         obj.cq.in = tmpl->cq;
945         obj.cq.out = &cq_info;
946         obj.rwq.in = tmpl->wq;
947         obj.rwq.out = &rwq;
948         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
949         if (ret) {
950                 rte_errno = ret;
951                 goto error;
952         }
953         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
954                 DRV_LOG(ERR,
955                         "port %u wrong MLX5_CQE_SIZE environment variable"
956                         " value: it should be set to %u",
957                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
958                 rte_errno = EINVAL;
959                 goto error;
960         }
961         /* Fill the rings. */
962         rxq_data->wqes = rwq.buf;
963         for (i = 0; (i != wqe_n); ++i) {
964                 volatile struct mlx5_wqe_data_seg *scat;
965                 uintptr_t addr;
966                 uint32_t byte_count;
967
968                 if (mprq_en) {
969                         struct mlx5_mprq_buf *buf = (*rxq_data->mprq_bufs)[i];
970
971                         scat = &((volatile struct mlx5_wqe_mprq *)
972                                  rxq_data->wqes)[i].dseg;
973                         addr = (uintptr_t)mlx5_mprq_buf_addr(buf);
974                         byte_count = (1 << rxq_data->strd_sz_n) *
975                                      (1 << rxq_data->strd_num_n);
976                 } else {
977                         struct rte_mbuf *buf = (*rxq_data->elts)[i];
978
979                         scat = &((volatile struct mlx5_wqe_data_seg *)
980                                  rxq_data->wqes)[i];
981                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
982                         byte_count = DATA_LEN(buf);
983                 }
984                 /* scat->addr must be able to store a pointer. */
985                 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
986                 *scat = (struct mlx5_wqe_data_seg){
987                         .addr = rte_cpu_to_be_64(addr),
988                         .byte_count = rte_cpu_to_be_32(byte_count),
989                         .lkey = mlx5_rx_addr2mr(rxq_data, addr),
990                 };
991         }
992         rxq_data->rq_db = rwq.dbrec;
993         rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
994         rxq_data->cq_ci = 0;
995         rxq_data->consumed_strd = 0;
996         rxq_data->rq_pi = 0;
997         rxq_data->zip = (struct rxq_zip){
998                 .ai = 0,
999         };
1000         rxq_data->cq_db = cq_info.dbrec;
1001         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
1002         rxq_data->cq_uar = cq_info.cq_uar;
1003         rxq_data->cqn = cq_info.cqn;
1004         rxq_data->cq_arm_sn = 0;
1005         /* Update doorbell counter. */
1006         rxq_data->rq_ci = wqe_n >> rxq_data->sges_n;
1007         rte_wmb();
1008         *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
1009         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1010                 idx, (void *)&tmpl);
1011         rte_atomic32_inc(&tmpl->refcnt);
1012         LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
1013         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1014         return tmpl;
1015 error:
1016         ret = rte_errno; /* Save rte_errno before cleanup. */
1017         if (tmpl->wq)
1018                 claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
1019         if (tmpl->cq)
1020                 claim_zero(mlx5_glue->destroy_cq(tmpl->cq));
1021         if (tmpl->channel)
1022                 claim_zero(mlx5_glue->destroy_comp_channel(tmpl->channel));
1023         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1024         rte_errno = ret; /* Restore rte_errno. */
1025         return NULL;
1026 }
1027
1028 /**
1029  * Get an Rx queue Verbs object.
1030  *
1031  * @param dev
1032  *   Pointer to Ethernet device.
1033  * @param idx
1034  *   Queue index in DPDK Rx queue array
1035  *
1036  * @return
1037  *   The Verbs object if it exists.
1038  */
1039 struct mlx5_rxq_ibv *
1040 mlx5_rxq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
1041 {
1042         struct priv *priv = dev->data->dev_private;
1043         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1044         struct mlx5_rxq_ctrl *rxq_ctrl;
1045
1046         if (idx >= priv->rxqs_n)
1047                 return NULL;
1048         if (!rxq_data)
1049                 return NULL;
1050         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1051         if (rxq_ctrl->ibv) {
1052                 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
1053         }
1054         return rxq_ctrl->ibv;
1055 }
1056
1057 /**
1058  * Release an Rx verbs queue object.
1059  *
1060  * @param rxq_ibv
1061  *   Verbs Rx queue object.
1062  *
1063  * @return
1064  *   1 while a reference on it exists, 0 when freed.
1065  */
1066 int
1067 mlx5_rxq_ibv_release(struct mlx5_rxq_ibv *rxq_ibv)
1068 {
1069         assert(rxq_ibv);
1070         assert(rxq_ibv->wq);
1071         assert(rxq_ibv->cq);
1072         if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
1073                 rxq_free_elts(rxq_ibv->rxq_ctrl);
1074                 claim_zero(mlx5_glue->destroy_wq(rxq_ibv->wq));
1075                 claim_zero(mlx5_glue->destroy_cq(rxq_ibv->cq));
1076                 if (rxq_ibv->channel)
1077                         claim_zero(mlx5_glue->destroy_comp_channel
1078                                    (rxq_ibv->channel));
1079                 LIST_REMOVE(rxq_ibv, next);
1080                 rte_free(rxq_ibv);
1081                 return 0;
1082         }
1083         return 1;
1084 }
1085
1086 /**
1087  * Verify the Verbs Rx queue list is empty
1088  *
1089  * @param dev
1090  *   Pointer to Ethernet device.
1091  *
1092  * @return
1093  *   The number of object not released.
1094  */
1095 int
1096 mlx5_rxq_ibv_verify(struct rte_eth_dev *dev)
1097 {
1098         struct priv *priv = dev->data->dev_private;
1099         int ret = 0;
1100         struct mlx5_rxq_ibv *rxq_ibv;
1101
1102         LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
1103                 DRV_LOG(DEBUG, "port %u Verbs Rx queue %u still referenced",
1104                         dev->data->port_id, rxq_ibv->rxq_ctrl->idx);
1105                 ++ret;
1106         }
1107         return ret;
1108 }
1109
1110 /**
1111  * Return true if a single reference exists on the object.
1112  *
1113  * @param rxq_ibv
1114  *   Verbs Rx queue object.
1115  */
1116 int
1117 mlx5_rxq_ibv_releasable(struct mlx5_rxq_ibv *rxq_ibv)
1118 {
1119         assert(rxq_ibv);
1120         return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
1121 }
1122
1123 /**
1124  * Callback function to initialize mbufs for Multi-Packet RQ.
1125  */
1126 static inline void
1127 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg __rte_unused,
1128                     void *_m, unsigned int i __rte_unused)
1129 {
1130         struct mlx5_mprq_buf *buf = _m;
1131
1132         memset(_m, 0, sizeof(*buf));
1133         buf->mp = mp;
1134         rte_atomic16_set(&buf->refcnt, 1);
1135 }
1136
1137 /**
1138  * Free mempool of Multi-Packet RQ.
1139  *
1140  * @param dev
1141  *   Pointer to Ethernet device.
1142  *
1143  * @return
1144  *   0 on success, negative errno value on failure.
1145  */
1146 int
1147 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1148 {
1149         struct priv *priv = dev->data->dev_private;
1150         struct rte_mempool *mp = priv->mprq_mp;
1151         unsigned int i;
1152
1153         if (mp == NULL)
1154                 return 0;
1155         DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1156                 dev->data->port_id, mp->name);
1157         /*
1158          * If a buffer in the pool has been externally attached to a mbuf and it
1159          * is still in use by application, destroying the Rx qeueue can spoil
1160          * the packet. It is unlikely to happen but if application dynamically
1161          * creates and destroys with holding Rx packets, this can happen.
1162          *
1163          * TODO: It is unavoidable for now because the mempool for Multi-Packet
1164          * RQ isn't provided by application but managed by PMD.
1165          */
1166         if (!rte_mempool_full(mp)) {
1167                 DRV_LOG(ERR,
1168                         "port %u mempool for Multi-Packet RQ is still in use",
1169                         dev->data->port_id);
1170                 rte_errno = EBUSY;
1171                 return -rte_errno;
1172         }
1173         rte_mempool_free(mp);
1174         /* Unset mempool for each Rx queue. */
1175         for (i = 0; i != priv->rxqs_n; ++i) {
1176                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1177
1178                 if (rxq == NULL)
1179                         continue;
1180                 rxq->mprq_mp = NULL;
1181         }
1182         priv->mprq_mp = NULL;
1183         return 0;
1184 }
1185
1186 /**
1187  * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1188  * mempool. If already allocated, reuse it if there're enough elements.
1189  * Otherwise, resize it.
1190  *
1191  * @param dev
1192  *   Pointer to Ethernet device.
1193  *
1194  * @return
1195  *   0 on success, negative errno value on failure.
1196  */
1197 int
1198 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1199 {
1200         struct priv *priv = dev->data->dev_private;
1201         struct rte_mempool *mp = priv->mprq_mp;
1202         char name[RTE_MEMPOOL_NAMESIZE];
1203         unsigned int desc = 0;
1204         unsigned int buf_len;
1205         unsigned int obj_num;
1206         unsigned int obj_size;
1207         unsigned int strd_num_n = 0;
1208         unsigned int strd_sz_n = 0;
1209         unsigned int i;
1210
1211         if (!mlx5_mprq_enabled(dev))
1212                 return 0;
1213         /* Count the total number of descriptors configured. */
1214         for (i = 0; i != priv->rxqs_n; ++i) {
1215                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1216
1217                 if (rxq == NULL)
1218                         continue;
1219                 desc += 1 << rxq->elts_n;
1220                 /* Get the max number of strides. */
1221                 if (strd_num_n < rxq->strd_num_n)
1222                         strd_num_n = rxq->strd_num_n;
1223                 /* Get the max size of a stride. */
1224                 if (strd_sz_n < rxq->strd_sz_n)
1225                         strd_sz_n = rxq->strd_sz_n;
1226         }
1227         assert(strd_num_n && strd_sz_n);
1228         buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1229         obj_size = buf_len + sizeof(struct mlx5_mprq_buf);
1230         /*
1231          * Received packets can be either memcpy'd or externally referenced. In
1232          * case that the packet is attached to an mbuf as an external buffer, as
1233          * it isn't possible to predict how the buffers will be queued by
1234          * application, there's no option to exactly pre-allocate needed buffers
1235          * in advance but to speculatively prepares enough buffers.
1236          *
1237          * In the data path, if this Mempool is depleted, PMD will try to memcpy
1238          * received packets to buffers provided by application (rxq->mp) until
1239          * this Mempool gets available again.
1240          */
1241         desc *= 4;
1242         obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * priv->rxqs_n;
1243         /*
1244          * rte_mempool_create_empty() has sanity check to refuse large cache
1245          * size compared to the number of elements.
1246          * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1247          * constant number 2 instead.
1248          */
1249         obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1250         /* Check a mempool is already allocated and if it can be resued. */
1251         if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1252                 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1253                         dev->data->port_id, mp->name);
1254                 /* Reuse. */
1255                 goto exit;
1256         } else if (mp != NULL) {
1257                 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1258                         dev->data->port_id, mp->name);
1259                 /*
1260                  * If failed to free, which means it may be still in use, no way
1261                  * but to keep using the existing one. On buffer underrun,
1262                  * packets will be memcpy'd instead of external buffer
1263                  * attachment.
1264                  */
1265                 if (mlx5_mprq_free_mp(dev)) {
1266                         if (mp->elt_size >= obj_size)
1267                                 goto exit;
1268                         else
1269                                 return -rte_errno;
1270                 }
1271         }
1272         snprintf(name, sizeof(name), "%s-mprq", dev->device->name);
1273         mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1274                                 0, NULL, NULL, mlx5_mprq_buf_init, NULL,
1275                                 dev->device->numa_node, 0);
1276         if (mp == NULL) {
1277                 DRV_LOG(ERR,
1278                         "port %u failed to allocate a mempool for"
1279                         " Multi-Packet RQ, count=%u, size=%u",
1280                         dev->data->port_id, obj_num, obj_size);
1281                 rte_errno = ENOMEM;
1282                 return -rte_errno;
1283         }
1284         priv->mprq_mp = mp;
1285 exit:
1286         /* Set mempool for each Rx queue. */
1287         for (i = 0; i != priv->rxqs_n; ++i) {
1288                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1289
1290                 if (rxq == NULL)
1291                         continue;
1292                 rxq->mprq_mp = mp;
1293         }
1294         DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1295                 dev->data->port_id);
1296         return 0;
1297 }
1298
1299 /**
1300  * Create a DPDK Rx queue.
1301  *
1302  * @param dev
1303  *   Pointer to Ethernet device.
1304  * @param idx
1305  *   RX queue index.
1306  * @param desc
1307  *   Number of descriptors to configure in queue.
1308  * @param socket
1309  *   NUMA socket on which memory must be allocated.
1310  *
1311  * @return
1312  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1313  */
1314 struct mlx5_rxq_ctrl *
1315 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1316              unsigned int socket, const struct rte_eth_rxconf *conf,
1317              struct rte_mempool *mp)
1318 {
1319         struct priv *priv = dev->data->dev_private;
1320         struct mlx5_rxq_ctrl *tmpl;
1321         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
1322         unsigned int mprq_stride_size;
1323         struct mlx5_dev_config *config = &priv->config;
1324         /*
1325          * Always allocate extra slots, even if eventually
1326          * the vector Rx will not be used.
1327          */
1328         uint16_t desc_n =
1329                 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1330         uint64_t offloads = conf->offloads |
1331                            dev->data->dev_conf.rxmode.offloads;
1332         const int mprq_en = mlx5_check_mprq_support(dev) > 0;
1333
1334         tmpl = rte_calloc_socket("RXQ", 1,
1335                                  sizeof(*tmpl) +
1336                                  desc_n * sizeof(struct rte_mbuf *),
1337                                  0, socket);
1338         if (!tmpl) {
1339                 rte_errno = ENOMEM;
1340                 return NULL;
1341         }
1342         if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
1343                                MLX5_MR_BTREE_CACHE_N, socket)) {
1344                 /* rte_errno is already set. */
1345                 goto error;
1346         }
1347         tmpl->socket = socket;
1348         if (dev->data->dev_conf.intr_conf.rxq)
1349                 tmpl->irq = 1;
1350         /*
1351          * This Rx queue can be configured as a Multi-Packet RQ if all of the
1352          * following conditions are met:
1353          *  - MPRQ is enabled.
1354          *  - The number of descs is more than the number of strides.
1355          *  - max_rx_pkt_len plus overhead is less than the max size of a
1356          *    stride.
1357          *  Otherwise, enable Rx scatter if necessary.
1358          */
1359         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1360         mprq_stride_size =
1361                 dev->data->dev_conf.rxmode.max_rx_pkt_len +
1362                 sizeof(struct rte_mbuf_ext_shared_info) +
1363                 RTE_PKTMBUF_HEADROOM;
1364         if (mprq_en &&
1365             desc > (1U << config->mprq.stride_num_n) &&
1366             mprq_stride_size <= (1U << config->mprq.max_stride_size_n)) {
1367                 /* TODO: Rx scatter isn't supported yet. */
1368                 tmpl->rxq.sges_n = 0;
1369                 /* Trim the number of descs needed. */
1370                 desc >>= config->mprq.stride_num_n;
1371                 tmpl->rxq.strd_num_n = config->mprq.stride_num_n;
1372                 tmpl->rxq.strd_sz_n = RTE_MAX(log2above(mprq_stride_size),
1373                                               config->mprq.min_stride_size_n);
1374                 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1375                 tmpl->rxq.mprq_max_memcpy_len =
1376                         RTE_MIN(mb_len - RTE_PKTMBUF_HEADROOM,
1377                                 config->mprq.max_memcpy_len);
1378                 DRV_LOG(DEBUG,
1379                         "port %u Rx queue %u: Multi-Packet RQ is enabled"
1380                         " strd_num_n = %u, strd_sz_n = %u",
1381                         dev->data->port_id, idx,
1382                         tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1383         } else if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1384                    (mb_len - RTE_PKTMBUF_HEADROOM)) {
1385                 tmpl->rxq.sges_n = 0;
1386         } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
1387                 unsigned int size =
1388                         RTE_PKTMBUF_HEADROOM +
1389                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
1390                 unsigned int sges_n;
1391
1392                 /*
1393                  * Determine the number of SGEs needed for a full packet
1394                  * and round it to the next power of two.
1395                  */
1396                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
1397                 tmpl->rxq.sges_n = sges_n;
1398                 /* Make sure rxq.sges_n did not overflow. */
1399                 size = mb_len * (1 << tmpl->rxq.sges_n);
1400                 size -= RTE_PKTMBUF_HEADROOM;
1401                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
1402                         DRV_LOG(ERR,
1403                                 "port %u too many SGEs (%u) needed to handle"
1404                                 " requested maximum packet size %u",
1405                                 dev->data->port_id,
1406                                 1 << sges_n,
1407                                 dev->data->dev_conf.rxmode.max_rx_pkt_len);
1408                         rte_errno = EOVERFLOW;
1409                         goto error;
1410                 }
1411         } else {
1412                 DRV_LOG(WARNING,
1413                         "port %u the requested maximum Rx packet size (%u) is"
1414                         " larger than a single mbuf (%u) and scattered mode has"
1415                         " not been requested",
1416                         dev->data->port_id,
1417                         dev->data->dev_conf.rxmode.max_rx_pkt_len,
1418                         mb_len - RTE_PKTMBUF_HEADROOM);
1419         }
1420         if (mprq_en && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1421                 DRV_LOG(WARNING,
1422                         "port %u MPRQ is requested but cannot be enabled"
1423                         " (requested: desc = %u, stride_sz = %u,"
1424                         " supported: min_stride_num = %u, max_stride_sz = %u).",
1425                         dev->data->port_id, desc, mprq_stride_size,
1426                         (1 << config->mprq.stride_num_n),
1427                         (1 << config->mprq.max_stride_size_n));
1428         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1429                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1430         if (desc % (1 << tmpl->rxq.sges_n)) {
1431                 DRV_LOG(ERR,
1432                         "port %u number of Rx queue descriptors (%u) is not a"
1433                         " multiple of SGEs per packet (%u)",
1434                         dev->data->port_id,
1435                         desc,
1436                         1 << tmpl->rxq.sges_n);
1437                 rte_errno = EINVAL;
1438                 goto error;
1439         }
1440         /* Toggle RX checksum offload if hardware supports it. */
1441         tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
1442         tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1443         /* Configure VLAN stripping. */
1444         tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1445         /* By default, FCS (CRC) is stripped by hardware. */
1446         tmpl->rxq.crc_present = 0;
1447         if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
1448                 if (config->hw_fcs_strip) {
1449                         tmpl->rxq.crc_present = 1;
1450                 } else {
1451                         DRV_LOG(WARNING,
1452                                 "port %u CRC stripping has been disabled but will"
1453                                 " still be performed by hardware, make sure MLNX_OFED"
1454                                 " and firmware are up to date",
1455                                 dev->data->port_id);
1456                 }
1457         }
1458         DRV_LOG(DEBUG,
1459                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1460                 " incoming frames to hide it",
1461                 dev->data->port_id,
1462                 tmpl->rxq.crc_present ? "disabled" : "enabled",
1463                 tmpl->rxq.crc_present << 2);
1464         /* Save port ID. */
1465         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1466                 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
1467         tmpl->rxq.port_id = dev->data->port_id;
1468         tmpl->priv = priv;
1469         tmpl->rxq.mp = mp;
1470         tmpl->rxq.stats.idx = idx;
1471         tmpl->rxq.elts_n = log2above(desc);
1472         tmpl->rxq.rq_repl_thresh =
1473                 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
1474         tmpl->rxq.elts =
1475                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1476 #ifndef RTE_ARCH_64
1477         tmpl->rxq.uar_lock_cq = &priv->uar_lock_cq;
1478 #endif
1479         tmpl->idx = idx;
1480         rte_atomic32_inc(&tmpl->refcnt);
1481         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1482         return tmpl;
1483 error:
1484         rte_free(tmpl);
1485         return NULL;
1486 }
1487
1488 /**
1489  * Get a Rx queue.
1490  *
1491  * @param dev
1492  *   Pointer to Ethernet device.
1493  * @param idx
1494  *   TX queue index.
1495  *
1496  * @return
1497  *   A pointer to the queue if it exists, NULL otherwise.
1498  */
1499 struct mlx5_rxq_ctrl *
1500 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1501 {
1502         struct priv *priv = dev->data->dev_private;
1503         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1504
1505         if ((*priv->rxqs)[idx]) {
1506                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1507                                         struct mlx5_rxq_ctrl,
1508                                         rxq);
1509                 mlx5_rxq_ibv_get(dev, idx);
1510                 rte_atomic32_inc(&rxq_ctrl->refcnt);
1511         }
1512         return rxq_ctrl;
1513 }
1514
1515 /**
1516  * Release a Rx queue.
1517  *
1518  * @param dev
1519  *   Pointer to Ethernet device.
1520  * @param idx
1521  *   TX queue index.
1522  *
1523  * @return
1524  *   1 while a reference on it exists, 0 when freed.
1525  */
1526 int
1527 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
1528 {
1529         struct priv *priv = dev->data->dev_private;
1530         struct mlx5_rxq_ctrl *rxq_ctrl;
1531
1532         if (!(*priv->rxqs)[idx])
1533                 return 0;
1534         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1535         assert(rxq_ctrl->priv);
1536         if (rxq_ctrl->ibv && !mlx5_rxq_ibv_release(rxq_ctrl->ibv))
1537                 rxq_ctrl->ibv = NULL;
1538         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1539                 mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
1540                 LIST_REMOVE(rxq_ctrl, next);
1541                 rte_free(rxq_ctrl);
1542                 (*priv->rxqs)[idx] = NULL;
1543                 return 0;
1544         }
1545         return 1;
1546 }
1547
1548 /**
1549  * Verify if the queue can be released.
1550  *
1551  * @param dev
1552  *   Pointer to Ethernet device.
1553  * @param idx
1554  *   TX queue index.
1555  *
1556  * @return
1557  *   1 if the queue can be released, negative errno otherwise and rte_errno is
1558  *   set.
1559  */
1560 int
1561 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
1562 {
1563         struct priv *priv = dev->data->dev_private;
1564         struct mlx5_rxq_ctrl *rxq_ctrl;
1565
1566         if (!(*priv->rxqs)[idx]) {
1567                 rte_errno = EINVAL;
1568                 return -rte_errno;
1569         }
1570         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1571         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1572 }
1573
1574 /**
1575  * Verify the Rx Queue list is empty
1576  *
1577  * @param dev
1578  *   Pointer to Ethernet device.
1579  *
1580  * @return
1581  *   The number of object not released.
1582  */
1583 int
1584 mlx5_rxq_verify(struct rte_eth_dev *dev)
1585 {
1586         struct priv *priv = dev->data->dev_private;
1587         struct mlx5_rxq_ctrl *rxq_ctrl;
1588         int ret = 0;
1589
1590         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1591                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
1592                         dev->data->port_id, rxq_ctrl->idx);
1593                 ++ret;
1594         }
1595         return ret;
1596 }
1597
1598 /**
1599  * Create an indirection table.
1600  *
1601  * @param dev
1602  *   Pointer to Ethernet device.
1603  * @param queues
1604  *   Queues entering in the indirection table.
1605  * @param queues_n
1606  *   Number of queues in the array.
1607  *
1608  * @return
1609  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1610  */
1611 struct mlx5_ind_table_ibv *
1612 mlx5_ind_table_ibv_new(struct rte_eth_dev *dev, const uint16_t *queues,
1613                        uint32_t queues_n)
1614 {
1615         struct priv *priv = dev->data->dev_private;
1616         struct mlx5_ind_table_ibv *ind_tbl;
1617         const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1618                 log2above(queues_n) :
1619                 log2above(priv->config.ind_table_max_size);
1620         struct ibv_wq *wq[1 << wq_n];
1621         unsigned int i;
1622         unsigned int j;
1623
1624         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1625                              queues_n * sizeof(uint16_t), 0);
1626         if (!ind_tbl) {
1627                 rte_errno = ENOMEM;
1628                 return NULL;
1629         }
1630         for (i = 0; i != queues_n; ++i) {
1631                 struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev, queues[i]);
1632
1633                 if (!rxq)
1634                         goto error;
1635                 wq[i] = rxq->ibv->wq;
1636                 ind_tbl->queues[i] = queues[i];
1637         }
1638         ind_tbl->queues_n = queues_n;
1639         /* Finalise indirection table. */
1640         for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1641                 wq[i] = wq[j];
1642         ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
1643                 (priv->ctx,
1644                  &(struct ibv_rwq_ind_table_init_attr){
1645                         .log_ind_tbl_size = wq_n,
1646                         .ind_tbl = wq,
1647                         .comp_mask = 0,
1648                  });
1649         if (!ind_tbl->ind_table) {
1650                 rte_errno = errno;
1651                 goto error;
1652         }
1653         rte_atomic32_inc(&ind_tbl->refcnt);
1654         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1655         return ind_tbl;
1656 error:
1657         rte_free(ind_tbl);
1658         DEBUG("port %u cannot create indirection table", dev->data->port_id);
1659         return NULL;
1660 }
1661
1662 /**
1663  * Get an indirection table.
1664  *
1665  * @param dev
1666  *   Pointer to Ethernet device.
1667  * @param queues
1668  *   Queues entering in the indirection table.
1669  * @param queues_n
1670  *   Number of queues in the array.
1671  *
1672  * @return
1673  *   An indirection table if found.
1674  */
1675 struct mlx5_ind_table_ibv *
1676 mlx5_ind_table_ibv_get(struct rte_eth_dev *dev, const uint16_t *queues,
1677                        uint32_t queues_n)
1678 {
1679         struct priv *priv = dev->data->dev_private;
1680         struct mlx5_ind_table_ibv *ind_tbl;
1681
1682         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1683                 if ((ind_tbl->queues_n == queues_n) &&
1684                     (memcmp(ind_tbl->queues, queues,
1685                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1686                      == 0))
1687                         break;
1688         }
1689         if (ind_tbl) {
1690                 unsigned int i;
1691
1692                 rte_atomic32_inc(&ind_tbl->refcnt);
1693                 for (i = 0; i != ind_tbl->queues_n; ++i)
1694                         mlx5_rxq_get(dev, ind_tbl->queues[i]);
1695         }
1696         return ind_tbl;
1697 }
1698
1699 /**
1700  * Release an indirection table.
1701  *
1702  * @param dev
1703  *   Pointer to Ethernet device.
1704  * @param ind_table
1705  *   Indirection table to release.
1706  *
1707  * @return
1708  *   1 while a reference on it exists, 0 when freed.
1709  */
1710 int
1711 mlx5_ind_table_ibv_release(struct rte_eth_dev *dev,
1712                            struct mlx5_ind_table_ibv *ind_tbl)
1713 {
1714         unsigned int i;
1715
1716         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1717                 claim_zero(mlx5_glue->destroy_rwq_ind_table
1718                            (ind_tbl->ind_table));
1719         for (i = 0; i != ind_tbl->queues_n; ++i)
1720                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
1721         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1722                 LIST_REMOVE(ind_tbl, next);
1723                 rte_free(ind_tbl);
1724                 return 0;
1725         }
1726         return 1;
1727 }
1728
1729 /**
1730  * Verify the Rx Queue list is empty
1731  *
1732  * @param dev
1733  *   Pointer to Ethernet device.
1734  *
1735  * @return
1736  *   The number of object not released.
1737  */
1738 int
1739 mlx5_ind_table_ibv_verify(struct rte_eth_dev *dev)
1740 {
1741         struct priv *priv = dev->data->dev_private;
1742         struct mlx5_ind_table_ibv *ind_tbl;
1743         int ret = 0;
1744
1745         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1746                 DRV_LOG(DEBUG,
1747                         "port %u Verbs indirection table %p still referenced",
1748                         dev->data->port_id, (void *)ind_tbl);
1749                 ++ret;
1750         }
1751         return ret;
1752 }
1753
1754 /**
1755  * Create an Rx Hash queue.
1756  *
1757  * @param dev
1758  *   Pointer to Ethernet device.
1759  * @param rss_key
1760  *   RSS key for the Rx hash queue.
1761  * @param rss_key_len
1762  *   RSS key length.
1763  * @param hash_fields
1764  *   Verbs protocol hash field to make the RSS on.
1765  * @param queues
1766  *   Queues entering in hash queue. In case of empty hash_fields only the
1767  *   first queue index will be taken for the indirection table.
1768  * @param queues_n
1769  *   Number of queues.
1770  * @param tunnel
1771  *   Tunnel type.
1772  *
1773  * @return
1774  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1775  */
1776 struct mlx5_hrxq *
1777 mlx5_hrxq_new(struct rte_eth_dev *dev,
1778               const uint8_t *rss_key, uint32_t rss_key_len,
1779               uint64_t hash_fields,
1780               const uint16_t *queues, uint32_t queues_n,
1781               int tunnel __rte_unused)
1782 {
1783         struct priv *priv = dev->data->dev_private;
1784         struct mlx5_hrxq *hrxq;
1785         struct mlx5_ind_table_ibv *ind_tbl;
1786         struct ibv_qp *qp;
1787 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1788         struct mlx5dv_qp_init_attr qp_init_attr;
1789 #endif
1790         int err;
1791
1792         queues_n = hash_fields ? queues_n : 1;
1793         ind_tbl = mlx5_ind_table_ibv_get(dev, queues, queues_n);
1794         if (!ind_tbl)
1795                 ind_tbl = mlx5_ind_table_ibv_new(dev, queues, queues_n);
1796         if (!ind_tbl) {
1797                 rte_errno = ENOMEM;
1798                 return NULL;
1799         }
1800 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1801         memset(&qp_init_attr, 0, sizeof(qp_init_attr));
1802         if (tunnel) {
1803                 qp_init_attr.comp_mask =
1804                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
1805                 qp_init_attr.create_flags = MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
1806         }
1807 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1808         if (dev->data->dev_conf.lpbk_mode) {
1809                 /* Allow packet sent from NIC loop back w/o source MAC check. */
1810                 qp_init_attr.comp_mask |=
1811                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
1812                 qp_init_attr.create_flags |=
1813                                 MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
1814         }
1815 #endif
1816         qp = mlx5_glue->dv_create_qp
1817                 (priv->ctx,
1818                  &(struct ibv_qp_init_attr_ex){
1819                         .qp_type = IBV_QPT_RAW_PACKET,
1820                         .comp_mask =
1821                                 IBV_QP_INIT_ATTR_PD |
1822                                 IBV_QP_INIT_ATTR_IND_TABLE |
1823                                 IBV_QP_INIT_ATTR_RX_HASH,
1824                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1825                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1826                                 .rx_hash_key_len = rss_key_len,
1827                                 .rx_hash_key = (void *)(uintptr_t)rss_key,
1828                                 .rx_hash_fields_mask = hash_fields,
1829                         },
1830                         .rwq_ind_tbl = ind_tbl->ind_table,
1831                         .pd = priv->pd,
1832                  },
1833                  &qp_init_attr);
1834 #else
1835         qp = mlx5_glue->create_qp_ex
1836                 (priv->ctx,
1837                  &(struct ibv_qp_init_attr_ex){
1838                         .qp_type = IBV_QPT_RAW_PACKET,
1839                         .comp_mask =
1840                                 IBV_QP_INIT_ATTR_PD |
1841                                 IBV_QP_INIT_ATTR_IND_TABLE |
1842                                 IBV_QP_INIT_ATTR_RX_HASH,
1843                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1844                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1845                                 .rx_hash_key_len = rss_key_len,
1846                                 .rx_hash_key = (void *)(uintptr_t)rss_key,
1847                                 .rx_hash_fields_mask = hash_fields,
1848                         },
1849                         .rwq_ind_tbl = ind_tbl->ind_table,
1850                         .pd = priv->pd,
1851                  });
1852 #endif
1853         if (!qp) {
1854                 rte_errno = errno;
1855                 goto error;
1856         }
1857         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1858         if (!hrxq)
1859                 goto error;
1860         hrxq->ind_table = ind_tbl;
1861         hrxq->qp = qp;
1862         hrxq->rss_key_len = rss_key_len;
1863         hrxq->hash_fields = hash_fields;
1864         memcpy(hrxq->rss_key, rss_key, rss_key_len);
1865         rte_atomic32_inc(&hrxq->refcnt);
1866         LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1867         return hrxq;
1868 error:
1869         err = rte_errno; /* Save rte_errno before cleanup. */
1870         mlx5_ind_table_ibv_release(dev, ind_tbl);
1871         if (qp)
1872                 claim_zero(mlx5_glue->destroy_qp(qp));
1873         rte_errno = err; /* Restore rte_errno. */
1874         return NULL;
1875 }
1876
1877 /**
1878  * Get an Rx Hash queue.
1879  *
1880  * @param dev
1881  *   Pointer to Ethernet device.
1882  * @param rss_conf
1883  *   RSS configuration for the Rx hash queue.
1884  * @param queues
1885  *   Queues entering in hash queue. In case of empty hash_fields only the
1886  *   first queue index will be taken for the indirection table.
1887  * @param queues_n
1888  *   Number of queues.
1889  *
1890  * @return
1891  *   An hash Rx queue on success.
1892  */
1893 struct mlx5_hrxq *
1894 mlx5_hrxq_get(struct rte_eth_dev *dev,
1895               const uint8_t *rss_key, uint32_t rss_key_len,
1896               uint64_t hash_fields,
1897               const uint16_t *queues, uint32_t queues_n)
1898 {
1899         struct priv *priv = dev->data->dev_private;
1900         struct mlx5_hrxq *hrxq;
1901
1902         queues_n = hash_fields ? queues_n : 1;
1903         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1904                 struct mlx5_ind_table_ibv *ind_tbl;
1905
1906                 if (hrxq->rss_key_len != rss_key_len)
1907                         continue;
1908                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1909                         continue;
1910                 if (hrxq->hash_fields != hash_fields)
1911                         continue;
1912                 ind_tbl = mlx5_ind_table_ibv_get(dev, queues, queues_n);
1913                 if (!ind_tbl)
1914                         continue;
1915                 if (ind_tbl != hrxq->ind_table) {
1916                         mlx5_ind_table_ibv_release(dev, ind_tbl);
1917                         continue;
1918                 }
1919                 rte_atomic32_inc(&hrxq->refcnt);
1920                 return hrxq;
1921         }
1922         return NULL;
1923 }
1924
1925 /**
1926  * Release the hash Rx queue.
1927  *
1928  * @param dev
1929  *   Pointer to Ethernet device.
1930  * @param hrxq
1931  *   Pointer to Hash Rx queue to release.
1932  *
1933  * @return
1934  *   1 while a reference on it exists, 0 when freed.
1935  */
1936 int
1937 mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
1938 {
1939         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1940                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
1941                 mlx5_ind_table_ibv_release(dev, hrxq->ind_table);
1942                 LIST_REMOVE(hrxq, next);
1943                 rte_free(hrxq);
1944                 return 0;
1945         }
1946         claim_nonzero(mlx5_ind_table_ibv_release(dev, hrxq->ind_table));
1947         return 1;
1948 }
1949
1950 /**
1951  * Verify the Rx Queue list is empty
1952  *
1953  * @param dev
1954  *   Pointer to Ethernet device.
1955  *
1956  * @return
1957  *   The number of object not released.
1958  */
1959 int
1960 mlx5_hrxq_ibv_verify(struct rte_eth_dev *dev)
1961 {
1962         struct priv *priv = dev->data->dev_private;
1963         struct mlx5_hrxq *hrxq;
1964         int ret = 0;
1965
1966         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1967                 DRV_LOG(DEBUG,
1968                         "port %u Verbs hash Rx queue %p still referenced",
1969                         dev->data->port_id, (void *)hrxq);
1970                 ++ret;
1971         }
1972         return ret;
1973 }
1974
1975 /**
1976  * Create a drop Rx queue Verbs object.
1977  *
1978  * @param dev
1979  *   Pointer to Ethernet device.
1980  *
1981  * @return
1982  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1983  */
1984 struct mlx5_rxq_ibv *
1985 mlx5_rxq_ibv_drop_new(struct rte_eth_dev *dev)
1986 {
1987         struct priv *priv = dev->data->dev_private;
1988         struct ibv_cq *cq;
1989         struct ibv_wq *wq = NULL;
1990         struct mlx5_rxq_ibv *rxq;
1991
1992         if (priv->drop_queue.rxq)
1993                 return priv->drop_queue.rxq;
1994         cq = mlx5_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
1995         if (!cq) {
1996                 DEBUG("port %u cannot allocate CQ for drop queue",
1997                       dev->data->port_id);
1998                 rte_errno = errno;
1999                 goto error;
2000         }
2001         wq = mlx5_glue->create_wq(priv->ctx,
2002                  &(struct ibv_wq_init_attr){
2003                         .wq_type = IBV_WQT_RQ,
2004                         .max_wr = 1,
2005                         .max_sge = 1,
2006                         .pd = priv->pd,
2007                         .cq = cq,
2008                  });
2009         if (!wq) {
2010                 DEBUG("port %u cannot allocate WQ for drop queue",
2011                       dev->data->port_id);
2012                 rte_errno = errno;
2013                 goto error;
2014         }
2015         rxq = rte_calloc(__func__, 1, sizeof(*rxq), 0);
2016         if (!rxq) {
2017                 DEBUG("port %u cannot allocate drop Rx queue memory",
2018                       dev->data->port_id);
2019                 rte_errno = ENOMEM;
2020                 goto error;
2021         }
2022         rxq->cq = cq;
2023         rxq->wq = wq;
2024         priv->drop_queue.rxq = rxq;
2025         return rxq;
2026 error:
2027         if (wq)
2028                 claim_zero(mlx5_glue->destroy_wq(wq));
2029         if (cq)
2030                 claim_zero(mlx5_glue->destroy_cq(cq));
2031         return NULL;
2032 }
2033
2034 /**
2035  * Release a drop Rx queue Verbs object.
2036  *
2037  * @param dev
2038  *   Pointer to Ethernet device.
2039  *
2040  * @return
2041  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
2042  */
2043 void
2044 mlx5_rxq_ibv_drop_release(struct rte_eth_dev *dev)
2045 {
2046         struct priv *priv = dev->data->dev_private;
2047         struct mlx5_rxq_ibv *rxq = priv->drop_queue.rxq;
2048
2049         if (rxq->wq)
2050                 claim_zero(mlx5_glue->destroy_wq(rxq->wq));
2051         if (rxq->cq)
2052                 claim_zero(mlx5_glue->destroy_cq(rxq->cq));
2053         rte_free(rxq);
2054         priv->drop_queue.rxq = NULL;
2055 }
2056
2057 /**
2058  * Create a drop indirection table.
2059  *
2060  * @param dev
2061  *   Pointer to Ethernet device.
2062  *
2063  * @return
2064  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
2065  */
2066 struct mlx5_ind_table_ibv *
2067 mlx5_ind_table_ibv_drop_new(struct rte_eth_dev *dev)
2068 {
2069         struct priv *priv = dev->data->dev_private;
2070         struct mlx5_ind_table_ibv *ind_tbl;
2071         struct mlx5_rxq_ibv *rxq;
2072         struct mlx5_ind_table_ibv tmpl;
2073
2074         rxq = mlx5_rxq_ibv_drop_new(dev);
2075         if (!rxq)
2076                 return NULL;
2077         tmpl.ind_table = mlx5_glue->create_rwq_ind_table
2078                 (priv->ctx,
2079                  &(struct ibv_rwq_ind_table_init_attr){
2080                         .log_ind_tbl_size = 0,
2081                         .ind_tbl = &rxq->wq,
2082                         .comp_mask = 0,
2083                  });
2084         if (!tmpl.ind_table) {
2085                 DEBUG("port %u cannot allocate indirection table for drop"
2086                       " queue",
2087                       dev->data->port_id);
2088                 rte_errno = errno;
2089                 goto error;
2090         }
2091         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl), 0);
2092         if (!ind_tbl) {
2093                 rte_errno = ENOMEM;
2094                 goto error;
2095         }
2096         ind_tbl->ind_table = tmpl.ind_table;
2097         return ind_tbl;
2098 error:
2099         mlx5_rxq_ibv_drop_release(dev);
2100         return NULL;
2101 }
2102
2103 /**
2104  * Release a drop indirection table.
2105  *
2106  * @param dev
2107  *   Pointer to Ethernet device.
2108  */
2109 void
2110 mlx5_ind_table_ibv_drop_release(struct rte_eth_dev *dev)
2111 {
2112         struct priv *priv = dev->data->dev_private;
2113         struct mlx5_ind_table_ibv *ind_tbl = priv->drop_queue.hrxq->ind_table;
2114
2115         claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
2116         mlx5_rxq_ibv_drop_release(dev);
2117         rte_free(ind_tbl);
2118         priv->drop_queue.hrxq->ind_table = NULL;
2119 }
2120
2121 /**
2122  * Create a drop Rx Hash queue.
2123  *
2124  * @param dev
2125  *   Pointer to Ethernet device.
2126  *
2127  * @return
2128  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
2129  */
2130 struct mlx5_hrxq *
2131 mlx5_hrxq_drop_new(struct rte_eth_dev *dev)
2132 {
2133         struct priv *priv = dev->data->dev_private;
2134         struct mlx5_ind_table_ibv *ind_tbl;
2135         struct ibv_qp *qp;
2136         struct mlx5_hrxq *hrxq;
2137
2138         if (priv->drop_queue.hrxq) {
2139                 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
2140                 return priv->drop_queue.hrxq;
2141         }
2142         ind_tbl = mlx5_ind_table_ibv_drop_new(dev);
2143         if (!ind_tbl)
2144                 return NULL;
2145         qp = mlx5_glue->create_qp_ex(priv->ctx,
2146                  &(struct ibv_qp_init_attr_ex){
2147                         .qp_type = IBV_QPT_RAW_PACKET,
2148                         .comp_mask =
2149                                 IBV_QP_INIT_ATTR_PD |
2150                                 IBV_QP_INIT_ATTR_IND_TABLE |
2151                                 IBV_QP_INIT_ATTR_RX_HASH,
2152                         .rx_hash_conf = (struct ibv_rx_hash_conf){
2153                                 .rx_hash_function =
2154                                         IBV_RX_HASH_FUNC_TOEPLITZ,
2155                                 .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
2156                                 .rx_hash_key = rss_hash_default_key,
2157                                 .rx_hash_fields_mask = 0,
2158                                 },
2159                         .rwq_ind_tbl = ind_tbl->ind_table,
2160                         .pd = priv->pd
2161                  });
2162         if (!qp) {
2163                 DEBUG("port %u cannot allocate QP for drop queue",
2164                       dev->data->port_id);
2165                 rte_errno = errno;
2166                 goto error;
2167         }
2168         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq), 0);
2169         if (!hrxq) {
2170                 DRV_LOG(WARNING,
2171                         "port %u cannot allocate memory for drop queue",
2172                         dev->data->port_id);
2173                 rte_errno = ENOMEM;
2174                 goto error;
2175         }
2176         hrxq->ind_table = ind_tbl;
2177         hrxq->qp = qp;
2178         priv->drop_queue.hrxq = hrxq;
2179         rte_atomic32_set(&hrxq->refcnt, 1);
2180         return hrxq;
2181 error:
2182         if (ind_tbl)
2183                 mlx5_ind_table_ibv_drop_release(dev);
2184         return NULL;
2185 }
2186
2187 /**
2188  * Release a drop hash Rx queue.
2189  *
2190  * @param dev
2191  *   Pointer to Ethernet device.
2192  */
2193 void
2194 mlx5_hrxq_drop_release(struct rte_eth_dev *dev)
2195 {
2196         struct priv *priv = dev->data->dev_private;
2197         struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2198
2199         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2200                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2201                 mlx5_ind_table_ibv_drop_release(dev);
2202                 rte_free(hrxq);
2203                 priv->drop_queue.hrxq = NULL;
2204         }
2205 }