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