net/mlx5: add define of LRO segment chunk size
[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 mlx5_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 mlx5_priv *priv = dev->data->dev_private;
107         uint16_t i;
108         uint16_t n = 0;
109         uint16_t n_ibv = 0;
110
111         if (mlx5_check_mprq_support(dev) < 0)
112                 return 0;
113         /* All the configured queues should be enabled. */
114         for (i = 0; i < priv->rxqs_n; ++i) {
115                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
116                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
117                         (rxq, struct mlx5_rxq_ctrl, rxq);
118
119                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
120                         continue;
121                 n_ibv++;
122                 if (mlx5_rxq_mprq_enabled(rxq))
123                         ++n;
124         }
125         /* Multi-Packet RQ can't be partially configured. */
126         assert(n == 0 || n == n_ibv);
127         return n == n_ibv;
128 }
129
130 /**
131  * Allocate RX queue elements for Multi-Packet RQ.
132  *
133  * @param rxq_ctrl
134  *   Pointer to RX queue structure.
135  *
136  * @return
137  *   0 on success, a negative errno value otherwise and rte_errno is set.
138  */
139 static int
140 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
141 {
142         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
143         unsigned int wqe_n = 1 << rxq->elts_n;
144         unsigned int i;
145         int err;
146
147         /* Iterate on segments. */
148         for (i = 0; i <= wqe_n; ++i) {
149                 struct mlx5_mprq_buf *buf;
150
151                 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
152                         DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
153                         rte_errno = ENOMEM;
154                         goto error;
155                 }
156                 if (i < wqe_n)
157                         (*rxq->mprq_bufs)[i] = buf;
158                 else
159                         rxq->mprq_repl = buf;
160         }
161         DRV_LOG(DEBUG,
162                 "port %u Rx queue %u allocated and configured %u segments",
163                 rxq->port_id, rxq->idx, wqe_n);
164         return 0;
165 error:
166         err = rte_errno; /* Save rte_errno before cleanup. */
167         wqe_n = i;
168         for (i = 0; (i != wqe_n); ++i) {
169                 if ((*rxq->mprq_bufs)[i] != NULL)
170                         rte_mempool_put(rxq->mprq_mp,
171                                         (*rxq->mprq_bufs)[i]);
172                 (*rxq->mprq_bufs)[i] = NULL;
173         }
174         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
175                 rxq->port_id, rxq->idx);
176         rte_errno = err; /* Restore rte_errno. */
177         return -rte_errno;
178 }
179
180 /**
181  * Allocate RX queue elements for Single-Packet RQ.
182  *
183  * @param rxq_ctrl
184  *   Pointer to RX queue structure.
185  *
186  * @return
187  *   0 on success, errno value on failure.
188  */
189 static int
190 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
191 {
192         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
193         unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
194         unsigned int i;
195         int err;
196
197         /* Iterate on segments. */
198         for (i = 0; (i != elts_n); ++i) {
199                 struct rte_mbuf *buf;
200
201                 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
202                 if (buf == NULL) {
203                         DRV_LOG(ERR, "port %u empty mbuf pool",
204                                 PORT_ID(rxq_ctrl->priv));
205                         rte_errno = ENOMEM;
206                         goto error;
207                 }
208                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
209                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
210                 /* Buffer is supposed to be empty. */
211                 assert(rte_pktmbuf_data_len(buf) == 0);
212                 assert(rte_pktmbuf_pkt_len(buf) == 0);
213                 assert(!buf->next);
214                 /* Only the first segment keeps headroom. */
215                 if (i % sges_n)
216                         SET_DATA_OFF(buf, 0);
217                 PORT(buf) = rxq_ctrl->rxq.port_id;
218                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
219                 PKT_LEN(buf) = DATA_LEN(buf);
220                 NB_SEGS(buf) = 1;
221                 (*rxq_ctrl->rxq.elts)[i] = buf;
222         }
223         /* If Rx vector is activated. */
224         if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
225                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
226                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
227                 int j;
228
229                 /* Initialize default rearm_data for vPMD. */
230                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
231                 rte_mbuf_refcnt_set(mbuf_init, 1);
232                 mbuf_init->nb_segs = 1;
233                 mbuf_init->port = rxq->port_id;
234                 /*
235                  * prevent compiler reordering:
236                  * rearm_data covers previous fields.
237                  */
238                 rte_compiler_barrier();
239                 rxq->mbuf_initializer =
240                         *(uint64_t *)&mbuf_init->rearm_data;
241                 /* Padding with a fake mbuf for vectorized Rx. */
242                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
243                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
244         }
245         DRV_LOG(DEBUG,
246                 "port %u Rx queue %u allocated and configured %u segments"
247                 " (max %u packets)",
248                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx, elts_n,
249                 elts_n / (1 << rxq_ctrl->rxq.sges_n));
250         return 0;
251 error:
252         err = rte_errno; /* Save rte_errno before cleanup. */
253         elts_n = i;
254         for (i = 0; (i != elts_n); ++i) {
255                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
256                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
257                 (*rxq_ctrl->rxq.elts)[i] = NULL;
258         }
259         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
260                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx);
261         rte_errno = err; /* Restore rte_errno. */
262         return -rte_errno;
263 }
264
265 /**
266  * Allocate RX queue elements.
267  *
268  * @param rxq_ctrl
269  *   Pointer to RX queue structure.
270  *
271  * @return
272  *   0 on success, errno value on failure.
273  */
274 int
275 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
276 {
277         return mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
278                rxq_alloc_elts_mprq(rxq_ctrl) : rxq_alloc_elts_sprq(rxq_ctrl);
279 }
280
281 /**
282  * Free RX queue elements for Multi-Packet RQ.
283  *
284  * @param rxq_ctrl
285  *   Pointer to RX queue structure.
286  */
287 static void
288 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
289 {
290         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
291         uint16_t i;
292
293         DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing WRs",
294                 rxq->port_id, rxq->idx);
295         if (rxq->mprq_bufs == NULL)
296                 return;
297         assert(mlx5_rxq_check_vec_support(rxq) < 0);
298         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
299                 if ((*rxq->mprq_bufs)[i] != NULL)
300                         mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
301                 (*rxq->mprq_bufs)[i] = NULL;
302         }
303         if (rxq->mprq_repl != NULL) {
304                 mlx5_mprq_buf_free(rxq->mprq_repl);
305                 rxq->mprq_repl = NULL;
306         }
307 }
308
309 /**
310  * Free RX queue elements for Single-Packet RQ.
311  *
312  * @param rxq_ctrl
313  *   Pointer to RX queue structure.
314  */
315 static void
316 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
317 {
318         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
319         const uint16_t q_n = (1 << rxq->elts_n);
320         const uint16_t q_mask = q_n - 1;
321         uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
322         uint16_t i;
323
324         DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
325                 PORT_ID(rxq_ctrl->priv), rxq->idx);
326         if (rxq->elts == NULL)
327                 return;
328         /**
329          * Some mbuf in the Ring belongs to the application.  They cannot be
330          * freed.
331          */
332         if (mlx5_rxq_check_vec_support(rxq) > 0) {
333                 for (i = 0; i < used; ++i)
334                         (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
335                 rxq->rq_pi = rxq->rq_ci;
336         }
337         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
338                 if ((*rxq->elts)[i] != NULL)
339                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
340                 (*rxq->elts)[i] = NULL;
341         }
342 }
343
344 /**
345  * Free RX queue elements.
346  *
347  * @param rxq_ctrl
348  *   Pointer to RX queue structure.
349  */
350 static void
351 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
352 {
353         if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
354                 rxq_free_elts_mprq(rxq_ctrl);
355         else
356                 rxq_free_elts_sprq(rxq_ctrl);
357 }
358
359 /**
360  * Returns the per-queue supported offloads.
361  *
362  * @param dev
363  *   Pointer to Ethernet device.
364  *
365  * @return
366  *   Supported Rx offloads.
367  */
368 uint64_t
369 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
370 {
371         struct mlx5_priv *priv = dev->data->dev_private;
372         struct mlx5_dev_config *config = &priv->config;
373         uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
374                              DEV_RX_OFFLOAD_TIMESTAMP |
375                              DEV_RX_OFFLOAD_JUMBO_FRAME |
376                              DEV_RX_OFFLOAD_RSS_HASH);
377
378         if (config->hw_fcs_strip)
379                 offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
380
381         if (config->hw_csum)
382                 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
383                              DEV_RX_OFFLOAD_UDP_CKSUM |
384                              DEV_RX_OFFLOAD_TCP_CKSUM);
385         if (config->hw_vlan_strip)
386                 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
387         if (MLX5_LRO_SUPPORTED(dev))
388                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
389         return offloads;
390 }
391
392
393 /**
394  * Returns the per-port supported offloads.
395  *
396  * @return
397  *   Supported Rx offloads.
398  */
399 uint64_t
400 mlx5_get_rx_port_offloads(void)
401 {
402         uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
403
404         return offloads;
405 }
406
407 /**
408  * Verify if the queue can be released.
409  *
410  * @param dev
411  *   Pointer to Ethernet device.
412  * @param idx
413  *   RX queue index.
414  *
415  * @return
416  *   1 if the queue can be released
417  *   0 if the queue can not be released, there are references to it.
418  *   Negative errno and rte_errno is set if queue doesn't exist.
419  */
420 static int
421 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
422 {
423         struct mlx5_priv *priv = dev->data->dev_private;
424         struct mlx5_rxq_ctrl *rxq_ctrl;
425
426         if (!(*priv->rxqs)[idx]) {
427                 rte_errno = EINVAL;
428                 return -rte_errno;
429         }
430         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
431         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
432 }
433
434 /**
435  * Rx queue presetup checks.
436  *
437  * @param dev
438  *   Pointer to Ethernet device structure.
439  * @param idx
440  *   RX queue index.
441  * @param desc
442  *   Number of descriptors to configure in queue.
443  *
444  * @return
445  *   0 on success, a negative errno value otherwise and rte_errno is set.
446  */
447 static int
448 mlx5_rx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc)
449 {
450         struct mlx5_priv *priv = dev->data->dev_private;
451
452         if (!rte_is_power_of_2(desc)) {
453                 desc = 1 << log2above(desc);
454                 DRV_LOG(WARNING,
455                         "port %u increased number of descriptors in Rx queue %u"
456                         " to the next power of two (%d)",
457                         dev->data->port_id, idx, desc);
458         }
459         DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
460                 dev->data->port_id, idx, desc);
461         if (idx >= priv->rxqs_n) {
462                 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
463                         dev->data->port_id, idx, priv->rxqs_n);
464                 rte_errno = EOVERFLOW;
465                 return -rte_errno;
466         }
467         if (!mlx5_rxq_releasable(dev, idx)) {
468                 DRV_LOG(ERR, "port %u unable to release queue index %u",
469                         dev->data->port_id, idx);
470                 rte_errno = EBUSY;
471                 return -rte_errno;
472         }
473         mlx5_rxq_release(dev, idx);
474         return 0;
475 }
476
477 /**
478  *
479  * @param dev
480  *   Pointer to Ethernet device structure.
481  * @param idx
482  *   RX queue index.
483  * @param desc
484  *   Number of descriptors to configure in queue.
485  * @param socket
486  *   NUMA socket on which memory must be allocated.
487  * @param[in] conf
488  *   Thresholds parameters.
489  * @param mp
490  *   Memory pool for buffer allocations.
491  *
492  * @return
493  *   0 on success, a negative errno value otherwise and rte_errno is set.
494  */
495 int
496 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
497                     unsigned int socket, const struct rte_eth_rxconf *conf,
498                     struct rte_mempool *mp)
499 {
500         struct mlx5_priv *priv = dev->data->dev_private;
501         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
502         struct mlx5_rxq_ctrl *rxq_ctrl =
503                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
504         int res;
505
506         res = mlx5_rx_queue_pre_setup(dev, idx, desc);
507         if (res)
508                 return res;
509         rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, mp);
510         if (!rxq_ctrl) {
511                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
512                         dev->data->port_id, idx);
513                 rte_errno = ENOMEM;
514                 return -rte_errno;
515         }
516         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
517                 dev->data->port_id, idx);
518         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
519         return 0;
520 }
521
522 /**
523  *
524  * @param dev
525  *   Pointer to Ethernet device structure.
526  * @param idx
527  *   RX queue index.
528  * @param desc
529  *   Number of descriptors to configure in queue.
530  * @param hairpin_conf
531  *   Hairpin configuration parameters.
532  *
533  * @return
534  *   0 on success, a negative errno value otherwise and rte_errno is set.
535  */
536 int
537 mlx5_rx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
538                             uint16_t desc,
539                             const struct rte_eth_hairpin_conf *hairpin_conf)
540 {
541         struct mlx5_priv *priv = dev->data->dev_private;
542         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
543         struct mlx5_rxq_ctrl *rxq_ctrl =
544                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
545         int res;
546
547         res = mlx5_rx_queue_pre_setup(dev, idx, desc);
548         if (res)
549                 return res;
550         if (hairpin_conf->peer_count != 1 ||
551             hairpin_conf->peers[0].port != dev->data->port_id ||
552             hairpin_conf->peers[0].queue >= priv->txqs_n) {
553                 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
554                         " invalid hairpind configuration", dev->data->port_id,
555                         idx);
556                 rte_errno = EINVAL;
557                 return -rte_errno;
558         }
559         rxq_ctrl = mlx5_rxq_hairpin_new(dev, idx, desc, hairpin_conf);
560         if (!rxq_ctrl) {
561                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
562                         dev->data->port_id, idx);
563                 rte_errno = ENOMEM;
564                 return -rte_errno;
565         }
566         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
567                 dev->data->port_id, idx);
568         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
569         return 0;
570 }
571
572 /**
573  * DPDK callback to release a RX queue.
574  *
575  * @param dpdk_rxq
576  *   Generic RX queue pointer.
577  */
578 void
579 mlx5_rx_queue_release(void *dpdk_rxq)
580 {
581         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
582         struct mlx5_rxq_ctrl *rxq_ctrl;
583         struct mlx5_priv *priv;
584
585         if (rxq == NULL)
586                 return;
587         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
588         priv = rxq_ctrl->priv;
589         if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.idx))
590                 rte_panic("port %u Rx queue %u is still used by a flow and"
591                           " cannot be removed\n",
592                           PORT_ID(priv), rxq->idx);
593         mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.idx);
594 }
595
596 /**
597  * Get an Rx queue Verbs/DevX object.
598  *
599  * @param dev
600  *   Pointer to Ethernet device.
601  * @param idx
602  *   Queue index in DPDK Rx queue array
603  *
604  * @return
605  *   The Verbs/DevX object if it exists.
606  */
607 static struct mlx5_rxq_obj *
608 mlx5_rxq_obj_get(struct rte_eth_dev *dev, uint16_t idx)
609 {
610         struct mlx5_priv *priv = dev->data->dev_private;
611         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
612         struct mlx5_rxq_ctrl *rxq_ctrl;
613
614         if (idx >= priv->rxqs_n)
615                 return NULL;
616         if (!rxq_data)
617                 return NULL;
618         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
619         if (rxq_ctrl->obj)
620                 rte_atomic32_inc(&rxq_ctrl->obj->refcnt);
621         return rxq_ctrl->obj;
622 }
623
624 /**
625  * Release the resources allocated for an RQ DevX object.
626  *
627  * @param rxq_ctrl
628  *   DevX Rx queue object.
629  */
630 static void
631 rxq_release_rq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
632 {
633         if (rxq_ctrl->rxq.wqes) {
634                 rte_free((void *)(uintptr_t)rxq_ctrl->rxq.wqes);
635                 rxq_ctrl->rxq.wqes = NULL;
636         }
637         if (rxq_ctrl->wq_umem) {
638                 mlx5_glue->devx_umem_dereg(rxq_ctrl->wq_umem);
639                 rxq_ctrl->wq_umem = NULL;
640         }
641 }
642
643 /**
644  * Release an Rx hairpin related resources.
645  *
646  * @param rxq_obj
647  *   Hairpin Rx queue object.
648  */
649 static void
650 rxq_obj_hairpin_release(struct mlx5_rxq_obj *rxq_obj)
651 {
652         struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
653
654         assert(rxq_obj);
655         rq_attr.state = MLX5_RQC_STATE_RST;
656         rq_attr.rq_state = MLX5_RQC_STATE_RDY;
657         mlx5_devx_cmd_modify_rq(rxq_obj->rq, &rq_attr);
658         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
659 }
660
661 /**
662  * Release an Rx verbs/DevX queue object.
663  *
664  * @param rxq_obj
665  *   Verbs/DevX Rx queue object.
666  *
667  * @return
668  *   1 while a reference on it exists, 0 when freed.
669  */
670 static int
671 mlx5_rxq_obj_release(struct mlx5_rxq_obj *rxq_obj)
672 {
673         assert(rxq_obj);
674         if (rte_atomic32_dec_and_test(&rxq_obj->refcnt)) {
675                 switch (rxq_obj->type) {
676                 case MLX5_RXQ_OBJ_TYPE_IBV:
677                         assert(rxq_obj->wq);
678                         assert(rxq_obj->cq);
679                         rxq_free_elts(rxq_obj->rxq_ctrl);
680                         claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
681                         claim_zero(mlx5_glue->destroy_cq(rxq_obj->cq));
682                         break;
683                 case MLX5_RXQ_OBJ_TYPE_DEVX_RQ:
684                         assert(rxq_obj->cq);
685                         assert(rxq_obj->rq);
686                         rxq_free_elts(rxq_obj->rxq_ctrl);
687                         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
688                         rxq_release_rq_resources(rxq_obj->rxq_ctrl);
689                         claim_zero(mlx5_glue->destroy_cq(rxq_obj->cq));
690                         break;
691                 case MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN:
692                         assert(rxq_obj->rq);
693                         rxq_obj_hairpin_release(rxq_obj);
694                         break;
695                 }
696                 if (rxq_obj->channel)
697                         claim_zero(mlx5_glue->destroy_comp_channel
698                                    (rxq_obj->channel));
699                 LIST_REMOVE(rxq_obj, next);
700                 rte_free(rxq_obj);
701                 return 0;
702         }
703         return 1;
704 }
705
706 /**
707  * Allocate queue vector and fill epoll fd list for Rx interrupts.
708  *
709  * @param dev
710  *   Pointer to Ethernet device.
711  *
712  * @return
713  *   0 on success, a negative errno value otherwise and rte_errno is set.
714  */
715 int
716 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
717 {
718         struct mlx5_priv *priv = dev->data->dev_private;
719         unsigned int i;
720         unsigned int rxqs_n = priv->rxqs_n;
721         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
722         unsigned int count = 0;
723         struct rte_intr_handle *intr_handle = dev->intr_handle;
724
725         if (!dev->data->dev_conf.intr_conf.rxq)
726                 return 0;
727         mlx5_rx_intr_vec_disable(dev);
728         intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
729         if (intr_handle->intr_vec == NULL) {
730                 DRV_LOG(ERR,
731                         "port %u failed to allocate memory for interrupt"
732                         " vector, Rx interrupts will not be supported",
733                         dev->data->port_id);
734                 rte_errno = ENOMEM;
735                 return -rte_errno;
736         }
737         intr_handle->type = RTE_INTR_HANDLE_EXT;
738         for (i = 0; i != n; ++i) {
739                 /* This rxq obj must not be released in this function. */
740                 struct mlx5_rxq_obj *rxq_obj = mlx5_rxq_obj_get(dev, i);
741                 int fd;
742                 int flags;
743                 int rc;
744
745                 /* Skip queues that cannot request interrupts. */
746                 if (!rxq_obj || !rxq_obj->channel) {
747                         /* Use invalid intr_vec[] index to disable entry. */
748                         intr_handle->intr_vec[i] =
749                                 RTE_INTR_VEC_RXTX_OFFSET +
750                                 RTE_MAX_RXTX_INTR_VEC_ID;
751                         continue;
752                 }
753                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
754                         DRV_LOG(ERR,
755                                 "port %u too many Rx queues for interrupt"
756                                 " vector size (%d), Rx interrupts cannot be"
757                                 " enabled",
758                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
759                         mlx5_rx_intr_vec_disable(dev);
760                         rte_errno = ENOMEM;
761                         return -rte_errno;
762                 }
763                 fd = rxq_obj->channel->fd;
764                 flags = fcntl(fd, F_GETFL);
765                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
766                 if (rc < 0) {
767                         rte_errno = errno;
768                         DRV_LOG(ERR,
769                                 "port %u failed to make Rx interrupt file"
770                                 " descriptor %d non-blocking for queue index"
771                                 " %d",
772                                 dev->data->port_id, fd, i);
773                         mlx5_rx_intr_vec_disable(dev);
774                         return -rte_errno;
775                 }
776                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
777                 intr_handle->efds[count] = fd;
778                 count++;
779         }
780         if (!count)
781                 mlx5_rx_intr_vec_disable(dev);
782         else
783                 intr_handle->nb_efd = count;
784         return 0;
785 }
786
787 /**
788  * Clean up Rx interrupts handler.
789  *
790  * @param dev
791  *   Pointer to Ethernet device.
792  */
793 void
794 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
795 {
796         struct mlx5_priv *priv = dev->data->dev_private;
797         struct rte_intr_handle *intr_handle = dev->intr_handle;
798         unsigned int i;
799         unsigned int rxqs_n = priv->rxqs_n;
800         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
801
802         if (!dev->data->dev_conf.intr_conf.rxq)
803                 return;
804         if (!intr_handle->intr_vec)
805                 goto free;
806         for (i = 0; i != n; ++i) {
807                 struct mlx5_rxq_ctrl *rxq_ctrl;
808                 struct mlx5_rxq_data *rxq_data;
809
810                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
811                     RTE_MAX_RXTX_INTR_VEC_ID)
812                         continue;
813                 /**
814                  * Need to access directly the queue to release the reference
815                  * kept in mlx5_rx_intr_vec_enable().
816                  */
817                 rxq_data = (*priv->rxqs)[i];
818                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
819                 if (rxq_ctrl->obj)
820                         mlx5_rxq_obj_release(rxq_ctrl->obj);
821         }
822 free:
823         rte_intr_free_epoll_fd(intr_handle);
824         if (intr_handle->intr_vec)
825                 free(intr_handle->intr_vec);
826         intr_handle->nb_efd = 0;
827         intr_handle->intr_vec = NULL;
828 }
829
830 /**
831  *  MLX5 CQ notification .
832  *
833  *  @param rxq
834  *     Pointer to receive queue structure.
835  *  @param sq_n_rxq
836  *     Sequence number per receive queue .
837  */
838 static inline void
839 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
840 {
841         int sq_n = 0;
842         uint32_t doorbell_hi;
843         uint64_t doorbell;
844         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
845
846         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
847         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
848         doorbell = (uint64_t)doorbell_hi << 32;
849         doorbell |=  rxq->cqn;
850         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
851         mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
852                          cq_db_reg, rxq->uar_lock_cq);
853 }
854
855 /**
856  * DPDK callback for Rx queue interrupt enable.
857  *
858  * @param dev
859  *   Pointer to Ethernet device structure.
860  * @param rx_queue_id
861  *   Rx queue number.
862  *
863  * @return
864  *   0 on success, a negative errno value otherwise and rte_errno is set.
865  */
866 int
867 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
868 {
869         struct mlx5_priv *priv = dev->data->dev_private;
870         struct mlx5_rxq_data *rxq_data;
871         struct mlx5_rxq_ctrl *rxq_ctrl;
872
873         rxq_data = (*priv->rxqs)[rx_queue_id];
874         if (!rxq_data) {
875                 rte_errno = EINVAL;
876                 return -rte_errno;
877         }
878         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
879         if (rxq_ctrl->irq) {
880                 struct mlx5_rxq_obj *rxq_obj;
881
882                 rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
883                 if (!rxq_obj) {
884                         rte_errno = EINVAL;
885                         return -rte_errno;
886                 }
887                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
888                 mlx5_rxq_obj_release(rxq_obj);
889         }
890         return 0;
891 }
892
893 /**
894  * DPDK callback for Rx queue interrupt disable.
895  *
896  * @param dev
897  *   Pointer to Ethernet device structure.
898  * @param rx_queue_id
899  *   Rx queue number.
900  *
901  * @return
902  *   0 on success, a negative errno value otherwise and rte_errno is set.
903  */
904 int
905 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
906 {
907         struct mlx5_priv *priv = dev->data->dev_private;
908         struct mlx5_rxq_data *rxq_data;
909         struct mlx5_rxq_ctrl *rxq_ctrl;
910         struct mlx5_rxq_obj *rxq_obj = NULL;
911         struct ibv_cq *ev_cq;
912         void *ev_ctx;
913         int ret;
914
915         rxq_data = (*priv->rxqs)[rx_queue_id];
916         if (!rxq_data) {
917                 rte_errno = EINVAL;
918                 return -rte_errno;
919         }
920         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
921         if (!rxq_ctrl->irq)
922                 return 0;
923         rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
924         if (!rxq_obj) {
925                 rte_errno = EINVAL;
926                 return -rte_errno;
927         }
928         ret = mlx5_glue->get_cq_event(rxq_obj->channel, &ev_cq, &ev_ctx);
929         if (ret || ev_cq != rxq_obj->cq) {
930                 rte_errno = EINVAL;
931                 goto exit;
932         }
933         rxq_data->cq_arm_sn++;
934         mlx5_glue->ack_cq_events(rxq_obj->cq, 1);
935         mlx5_rxq_obj_release(rxq_obj);
936         return 0;
937 exit:
938         ret = rte_errno; /* Save rte_errno before cleanup. */
939         if (rxq_obj)
940                 mlx5_rxq_obj_release(rxq_obj);
941         DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
942                 dev->data->port_id, rx_queue_id);
943         rte_errno = ret; /* Restore rte_errno. */
944         return -rte_errno;
945 }
946
947 /**
948  * Create a CQ Verbs object.
949  *
950  * @param dev
951  *   Pointer to Ethernet device.
952  * @param priv
953  *   Pointer to device private data.
954  * @param rxq_data
955  *   Pointer to Rx queue data.
956  * @param cqe_n
957  *   Number of CQEs in CQ.
958  * @param rxq_obj
959  *   Pointer to Rx queue object data.
960  *
961  * @return
962  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
963  */
964 static struct ibv_cq *
965 mlx5_ibv_cq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
966                 struct mlx5_rxq_data *rxq_data,
967                 unsigned int cqe_n, struct mlx5_rxq_obj *rxq_obj)
968 {
969         struct {
970                 struct ibv_cq_init_attr_ex ibv;
971                 struct mlx5dv_cq_init_attr mlx5;
972         } cq_attr;
973
974         cq_attr.ibv = (struct ibv_cq_init_attr_ex){
975                 .cqe = cqe_n,
976                 .channel = rxq_obj->channel,
977                 .comp_mask = 0,
978         };
979         cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
980                 .comp_mask = 0,
981         };
982         if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
983             !rxq_data->lro) {
984                 cq_attr.mlx5.comp_mask |=
985                                 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
986 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
987                 cq_attr.mlx5.cqe_comp_res_format =
988                                 mlx5_rxq_mprq_enabled(rxq_data) ?
989                                 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
990                                 MLX5DV_CQE_RES_FORMAT_HASH;
991 #else
992                 cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
993 #endif
994                 /*
995                  * For vectorized Rx, it must not be doubled in order to
996                  * make cq_ci and rq_ci aligned.
997                  */
998                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
999                         cq_attr.ibv.cqe *= 2;
1000         } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
1001                 DRV_LOG(DEBUG,
1002                         "port %u Rx CQE compression is disabled for HW"
1003                         " timestamp",
1004                         dev->data->port_id);
1005         } else if (priv->config.cqe_comp && rxq_data->lro) {
1006                 DRV_LOG(DEBUG,
1007                         "port %u Rx CQE compression is disabled for LRO",
1008                         dev->data->port_id);
1009         }
1010 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
1011         if (priv->config.cqe_pad) {
1012                 cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
1013                 cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
1014         }
1015 #endif
1016         return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(priv->sh->ctx,
1017                                                               &cq_attr.ibv,
1018                                                               &cq_attr.mlx5));
1019 }
1020
1021 /**
1022  * Create a WQ Verbs object.
1023  *
1024  * @param dev
1025  *   Pointer to Ethernet device.
1026  * @param priv
1027  *   Pointer to device private data.
1028  * @param rxq_data
1029  *   Pointer to Rx queue data.
1030  * @param idx
1031  *   Queue index in DPDK Rx queue array
1032  * @param wqe_n
1033  *   Number of WQEs in WQ.
1034  * @param rxq_obj
1035  *   Pointer to Rx queue object data.
1036  *
1037  * @return
1038  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1039  */
1040 static struct ibv_wq *
1041 mlx5_ibv_wq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
1042                 struct mlx5_rxq_data *rxq_data, uint16_t idx,
1043                 unsigned int wqe_n, struct mlx5_rxq_obj *rxq_obj)
1044 {
1045         struct {
1046                 struct ibv_wq_init_attr ibv;
1047 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1048                 struct mlx5dv_wq_init_attr mlx5;
1049 #endif
1050         } wq_attr;
1051
1052         wq_attr.ibv = (struct ibv_wq_init_attr){
1053                 .wq_context = NULL, /* Could be useful in the future. */
1054                 .wq_type = IBV_WQT_RQ,
1055                 /* Max number of outstanding WRs. */
1056                 .max_wr = wqe_n >> rxq_data->sges_n,
1057                 /* Max number of scatter/gather elements in a WR. */
1058                 .max_sge = 1 << rxq_data->sges_n,
1059                 .pd = priv->sh->pd,
1060                 .cq = rxq_obj->cq,
1061                 .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
1062                 .create_flags = (rxq_data->vlan_strip ?
1063                                  IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
1064         };
1065         /* By default, FCS (CRC) is stripped by hardware. */
1066         if (rxq_data->crc_present) {
1067                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
1068                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1069         }
1070         if (priv->config.hw_padding) {
1071 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
1072                 wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
1073                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1074 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
1075                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
1076                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1077 #endif
1078         }
1079 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1080         wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
1081                 .comp_mask = 0,
1082         };
1083         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1084                 struct mlx5dv_striding_rq_init_attr *mprq_attr =
1085                                                 &wq_attr.mlx5.striding_rq_attrs;
1086
1087                 wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
1088                 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
1089                         .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
1090                         .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
1091                         .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
1092                 };
1093         }
1094         rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->ctx, &wq_attr.ibv,
1095                                               &wq_attr.mlx5);
1096 #else
1097         rxq_obj->wq = mlx5_glue->create_wq(priv->sh->ctx, &wq_attr.ibv);
1098 #endif
1099         if (rxq_obj->wq) {
1100                 /*
1101                  * Make sure number of WRs*SGEs match expectations since a queue
1102                  * cannot allocate more than "desc" buffers.
1103                  */
1104                 if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
1105                     wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
1106                         DRV_LOG(ERR,
1107                                 "port %u Rx queue %u requested %u*%u but got"
1108                                 " %u*%u WRs*SGEs",
1109                                 dev->data->port_id, idx,
1110                                 wqe_n >> rxq_data->sges_n,
1111                                 (1 << rxq_data->sges_n),
1112                                 wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
1113                         claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
1114                         rxq_obj->wq = NULL;
1115                         rte_errno = EINVAL;
1116                 }
1117         }
1118         return rxq_obj->wq;
1119 }
1120
1121 /**
1122  * Fill common fields of create RQ attributes structure.
1123  *
1124  * @param rxq_data
1125  *   Pointer to Rx queue data.
1126  * @param cqn
1127  *   CQ number to use with this RQ.
1128  * @param rq_attr
1129  *   RQ attributes structure to fill..
1130  */
1131 static void
1132 mlx5_devx_create_rq_attr_fill(struct mlx5_rxq_data *rxq_data, uint32_t cqn,
1133                               struct mlx5_devx_create_rq_attr *rq_attr)
1134 {
1135         rq_attr->state = MLX5_RQC_STATE_RST;
1136         rq_attr->vsd = (rxq_data->vlan_strip) ? 0 : 1;
1137         rq_attr->cqn = cqn;
1138         rq_attr->scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
1139 }
1140
1141 /**
1142  * Fill common fields of DevX WQ attributes structure.
1143  *
1144  * @param priv
1145  *   Pointer to device private data.
1146  * @param rxq_ctrl
1147  *   Pointer to Rx queue control structure.
1148  * @param wq_attr
1149  *   WQ attributes structure to fill..
1150  */
1151 static void
1152 mlx5_devx_wq_attr_fill(struct mlx5_priv *priv, struct mlx5_rxq_ctrl *rxq_ctrl,
1153                        struct mlx5_devx_wq_attr *wq_attr)
1154 {
1155         wq_attr->end_padding_mode = priv->config.cqe_pad ?
1156                                         MLX5_WQ_END_PAD_MODE_ALIGN :
1157                                         MLX5_WQ_END_PAD_MODE_NONE;
1158         wq_attr->pd = priv->sh->pdn;
1159         wq_attr->dbr_addr = rxq_ctrl->dbr_offset;
1160         wq_attr->dbr_umem_id = rxq_ctrl->dbr_umem_id;
1161         wq_attr->dbr_umem_valid = 1;
1162         wq_attr->wq_umem_id = rxq_ctrl->wq_umem->umem_id;
1163         wq_attr->wq_umem_valid = 1;
1164 }
1165
1166 /**
1167  * Create a RQ object using DevX.
1168  *
1169  * @param dev
1170  *   Pointer to Ethernet device.
1171  * @param idx
1172  *   Queue index in DPDK Rx queue array
1173  * @param cqn
1174  *   CQ number to use with this RQ.
1175  *
1176  * @return
1177  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1178  */
1179 static struct mlx5_devx_obj *
1180 mlx5_devx_rq_new(struct rte_eth_dev *dev, uint16_t idx, uint32_t cqn)
1181 {
1182         struct mlx5_priv *priv = dev->data->dev_private;
1183         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1184         struct mlx5_rxq_ctrl *rxq_ctrl =
1185                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1186         struct mlx5_devx_create_rq_attr rq_attr;
1187         uint32_t wqe_n = 1 << (rxq_data->elts_n - rxq_data->sges_n);
1188         uint32_t wq_size = 0;
1189         uint32_t wqe_size = 0;
1190         uint32_t log_wqe_size = 0;
1191         void *buf = NULL;
1192         struct mlx5_devx_obj *rq;
1193
1194         memset(&rq_attr, 0, sizeof(rq_attr));
1195         /* Fill RQ attributes. */
1196         rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
1197         rq_attr.flush_in_error_en = 1;
1198         mlx5_devx_create_rq_attr_fill(rxq_data, cqn, &rq_attr);
1199         /* Fill WQ attributes for this RQ. */
1200         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1201                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
1202                 /*
1203                  * Number of strides in each WQE:
1204                  * 512*2^single_wqe_log_num_of_strides.
1205                  */
1206                 rq_attr.wq_attr.single_wqe_log_num_of_strides =
1207                                 rxq_data->strd_num_n -
1208                                 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1209                 /* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
1210                 rq_attr.wq_attr.single_stride_log_num_of_bytes =
1211                                 rxq_data->strd_sz_n -
1212                                 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1213                 wqe_size = sizeof(struct mlx5_wqe_mprq);
1214         } else {
1215                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1216                 wqe_size = sizeof(struct mlx5_wqe_data_seg);
1217         }
1218         log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
1219         rq_attr.wq_attr.log_wq_stride = log_wqe_size;
1220         rq_attr.wq_attr.log_wq_sz = rxq_data->elts_n - rxq_data->sges_n;
1221         /* Calculate and allocate WQ memory space. */
1222         wqe_size = 1 << log_wqe_size; /* round up power of two.*/
1223         wq_size = wqe_n * wqe_size;
1224         buf = rte_calloc_socket(__func__, 1, wq_size, MLX5_WQE_BUF_ALIGNMENT,
1225                                 rxq_ctrl->socket);
1226         if (!buf)
1227                 return NULL;
1228         rxq_data->wqes = buf;
1229         rxq_ctrl->wq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
1230                                                      buf, wq_size, 0);
1231         if (!rxq_ctrl->wq_umem) {
1232                 rte_free(buf);
1233                 return NULL;
1234         }
1235         mlx5_devx_wq_attr_fill(priv, rxq_ctrl, &rq_attr.wq_attr);
1236         rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &rq_attr, rxq_ctrl->socket);
1237         if (!rq)
1238                 rxq_release_rq_resources(rxq_ctrl);
1239         return rq;
1240 }
1241
1242 /**
1243  * Create the Rx hairpin queue object.
1244  *
1245  * @param dev
1246  *   Pointer to Ethernet device.
1247  * @param idx
1248  *   Queue index in DPDK Rx queue array
1249  *
1250  * @return
1251  *   The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
1252  */
1253 static struct mlx5_rxq_obj *
1254 mlx5_rxq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
1255 {
1256         struct mlx5_priv *priv = dev->data->dev_private;
1257         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1258         struct mlx5_rxq_ctrl *rxq_ctrl =
1259                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1260         struct mlx5_devx_create_rq_attr attr = { 0 };
1261         struct mlx5_rxq_obj *tmpl = NULL;
1262         int ret = 0;
1263
1264         assert(rxq_data);
1265         assert(!rxq_ctrl->obj);
1266         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
1267                                  rxq_ctrl->socket);
1268         if (!tmpl) {
1269                 DRV_LOG(ERR,
1270                         "port %u Rx queue %u cannot allocate verbs resources",
1271                         dev->data->port_id, rxq_data->idx);
1272                 rte_errno = ENOMEM;
1273                 goto error;
1274         }
1275         tmpl->type = MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN;
1276         tmpl->rxq_ctrl = rxq_ctrl;
1277         attr.hairpin = 1;
1278         /* Workaround for hairpin startup */
1279         attr.wq_attr.log_hairpin_num_packets = log2above(32);
1280         /* Workaround for packets larger than 1KB */
1281         attr.wq_attr.log_hairpin_data_sz =
1282                         priv->config.hca_attr.log_max_hairpin_wq_data_sz;
1283         tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &attr,
1284                                            rxq_ctrl->socket);
1285         if (!tmpl->rq) {
1286                 DRV_LOG(ERR,
1287                         "port %u Rx hairpin queue %u can't create rq object",
1288                         dev->data->port_id, idx);
1289                 rte_errno = errno;
1290                 goto error;
1291         }
1292         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1293                 idx, (void *)&tmpl);
1294         rte_atomic32_inc(&tmpl->refcnt);
1295         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1296         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1297         return tmpl;
1298 error:
1299         ret = rte_errno; /* Save rte_errno before cleanup. */
1300         if (tmpl->rq)
1301                 mlx5_devx_cmd_destroy(tmpl->rq);
1302         rte_errno = ret; /* Restore rte_errno. */
1303         return NULL;
1304 }
1305
1306 /**
1307  * Create the Rx queue Verbs/DevX object.
1308  *
1309  * @param dev
1310  *   Pointer to Ethernet device.
1311  * @param idx
1312  *   Queue index in DPDK Rx queue array
1313  * @param type
1314  *   Type of Rx queue object to create.
1315  *
1316  * @return
1317  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
1318  */
1319 struct mlx5_rxq_obj *
1320 mlx5_rxq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
1321                  enum mlx5_rxq_obj_type type)
1322 {
1323         struct mlx5_priv *priv = dev->data->dev_private;
1324         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1325         struct mlx5_rxq_ctrl *rxq_ctrl =
1326                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1327         struct ibv_wq_attr mod;
1328         unsigned int cqe_n;
1329         unsigned int wqe_n = 1 << rxq_data->elts_n;
1330         struct mlx5_rxq_obj *tmpl = NULL;
1331         struct mlx5dv_cq cq_info;
1332         struct mlx5dv_rwq rwq;
1333         int ret = 0;
1334         struct mlx5dv_obj obj;
1335
1336         assert(rxq_data);
1337         assert(!rxq_ctrl->obj);
1338         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
1339                 return mlx5_rxq_obj_hairpin_new(dev, idx);
1340         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
1341         priv->verbs_alloc_ctx.obj = rxq_ctrl;
1342         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
1343                                  rxq_ctrl->socket);
1344         if (!tmpl) {
1345                 DRV_LOG(ERR,
1346                         "port %u Rx queue %u cannot allocate verbs resources",
1347                         dev->data->port_id, rxq_data->idx);
1348                 rte_errno = ENOMEM;
1349                 goto error;
1350         }
1351         tmpl->type = type;
1352         tmpl->rxq_ctrl = rxq_ctrl;
1353         if (rxq_ctrl->irq) {
1354                 tmpl->channel = mlx5_glue->create_comp_channel(priv->sh->ctx);
1355                 if (!tmpl->channel) {
1356                         DRV_LOG(ERR, "port %u: comp channel creation failure",
1357                                 dev->data->port_id);
1358                         rte_errno = ENOMEM;
1359                         goto error;
1360                 }
1361         }
1362         if (mlx5_rxq_mprq_enabled(rxq_data))
1363                 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
1364         else
1365                 cqe_n = wqe_n  - 1;
1366         tmpl->cq = mlx5_ibv_cq_new(dev, priv, rxq_data, cqe_n, tmpl);
1367         if (!tmpl->cq) {
1368                 DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
1369                         dev->data->port_id, idx);
1370                 rte_errno = ENOMEM;
1371                 goto error;
1372         }
1373         obj.cq.in = tmpl->cq;
1374         obj.cq.out = &cq_info;
1375         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
1376         if (ret) {
1377                 rte_errno = ret;
1378                 goto error;
1379         }
1380         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1381                 DRV_LOG(ERR,
1382                         "port %u wrong MLX5_CQE_SIZE environment variable"
1383                         " value: it should be set to %u",
1384                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
1385                 rte_errno = EINVAL;
1386                 goto error;
1387         }
1388         DRV_LOG(DEBUG, "port %u device_attr.max_qp_wr is %d",
1389                 dev->data->port_id, priv->sh->device_attr.orig_attr.max_qp_wr);
1390         DRV_LOG(DEBUG, "port %u device_attr.max_sge is %d",
1391                 dev->data->port_id, priv->sh->device_attr.orig_attr.max_sge);
1392         /* Allocate door-bell for types created with DevX. */
1393         if (tmpl->type != MLX5_RXQ_OBJ_TYPE_IBV) {
1394                 struct mlx5_devx_dbr_page *dbr_page;
1395                 int64_t dbr_offset;
1396
1397                 dbr_offset = mlx5_get_dbr(dev, &dbr_page);
1398                 if (dbr_offset < 0)
1399                         goto error;
1400                 rxq_ctrl->dbr_offset = dbr_offset;
1401                 rxq_ctrl->dbr_umem_id = dbr_page->umem->umem_id;
1402                 rxq_ctrl->dbr_umem_id_valid = 1;
1403                 rxq_data->rq_db = (uint32_t *)((uintptr_t)dbr_page->dbrs +
1404                                                (uintptr_t)rxq_ctrl->dbr_offset);
1405         }
1406         if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1407                 tmpl->wq = mlx5_ibv_wq_new(dev, priv, rxq_data, idx, wqe_n,
1408                                            tmpl);
1409                 if (!tmpl->wq) {
1410                         DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
1411                                 dev->data->port_id, idx);
1412                         rte_errno = ENOMEM;
1413                         goto error;
1414                 }
1415                 /* Change queue state to ready. */
1416                 mod = (struct ibv_wq_attr){
1417                         .attr_mask = IBV_WQ_ATTR_STATE,
1418                         .wq_state = IBV_WQS_RDY,
1419                 };
1420                 ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
1421                 if (ret) {
1422                         DRV_LOG(ERR,
1423                                 "port %u Rx queue %u WQ state to IBV_WQS_RDY"
1424                                 " failed", dev->data->port_id, idx);
1425                         rte_errno = ret;
1426                         goto error;
1427                 }
1428                 obj.rwq.in = tmpl->wq;
1429                 obj.rwq.out = &rwq;
1430                 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
1431                 if (ret) {
1432                         rte_errno = ret;
1433                         goto error;
1434                 }
1435                 rxq_data->wqes = rwq.buf;
1436                 rxq_data->rq_db = rwq.dbrec;
1437         } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1438                 struct mlx5_devx_modify_rq_attr rq_attr;
1439
1440                 memset(&rq_attr, 0, sizeof(rq_attr));
1441                 tmpl->rq = mlx5_devx_rq_new(dev, idx, cq_info.cqn);
1442                 if (!tmpl->rq) {
1443                         DRV_LOG(ERR, "port %u Rx queue %u RQ creation failure",
1444                                 dev->data->port_id, idx);
1445                         rte_errno = ENOMEM;
1446                         goto error;
1447                 }
1448                 /* Change queue state to ready. */
1449                 rq_attr.rq_state = MLX5_RQC_STATE_RST;
1450                 rq_attr.state = MLX5_RQC_STATE_RDY;
1451                 ret = mlx5_devx_cmd_modify_rq(tmpl->rq, &rq_attr);
1452                 if (ret)
1453                         goto error;
1454         }
1455         /* Fill the rings. */
1456         rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
1457         rxq_data->cq_db = cq_info.dbrec;
1458         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
1459         rxq_data->cq_uar = cq_info.cq_uar;
1460         rxq_data->cqn = cq_info.cqn;
1461         rxq_data->cq_arm_sn = 0;
1462         mlx5_rxq_initialize(rxq_data);
1463         rxq_data->cq_ci = 0;
1464         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1465                 idx, (void *)&tmpl);
1466         rte_atomic32_inc(&tmpl->refcnt);
1467         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1468         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1469         return tmpl;
1470 error:
1471         if (tmpl) {
1472                 ret = rte_errno; /* Save rte_errno before cleanup. */
1473                 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV && tmpl->wq)
1474                         claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
1475                 else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ && tmpl->rq)
1476                         claim_zero(mlx5_devx_cmd_destroy(tmpl->rq));
1477                 if (tmpl->cq)
1478                         claim_zero(mlx5_glue->destroy_cq(tmpl->cq));
1479                 if (tmpl->channel)
1480                         claim_zero(mlx5_glue->destroy_comp_channel
1481                                                         (tmpl->channel));
1482                 rte_free(tmpl);
1483                 rte_errno = ret; /* Restore rte_errno. */
1484         }
1485         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ)
1486                 rxq_release_rq_resources(rxq_ctrl);
1487         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1488         return NULL;
1489 }
1490
1491 /**
1492  * Verify the Rx queue objects list is empty
1493  *
1494  * @param dev
1495  *   Pointer to Ethernet device.
1496  *
1497  * @return
1498  *   The number of objects not released.
1499  */
1500 int
1501 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1502 {
1503         struct mlx5_priv *priv = dev->data->dev_private;
1504         int ret = 0;
1505         struct mlx5_rxq_obj *rxq_obj;
1506
1507         LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1508                 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1509                         dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1510                 ++ret;
1511         }
1512         return ret;
1513 }
1514
1515 /**
1516  * Callback function to initialize mbufs for Multi-Packet RQ.
1517  */
1518 static inline void
1519 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1520                     void *_m, unsigned int i __rte_unused)
1521 {
1522         struct mlx5_mprq_buf *buf = _m;
1523         struct rte_mbuf_ext_shared_info *shinfo;
1524         unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1525         unsigned int j;
1526
1527         memset(_m, 0, sizeof(*buf));
1528         buf->mp = mp;
1529         rte_atomic16_set(&buf->refcnt, 1);
1530         for (j = 0; j != strd_n; ++j) {
1531                 shinfo = &buf->shinfos[j];
1532                 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1533                 shinfo->fcb_opaque = buf;
1534         }
1535 }
1536
1537 /**
1538  * Free mempool of Multi-Packet RQ.
1539  *
1540  * @param dev
1541  *   Pointer to Ethernet device.
1542  *
1543  * @return
1544  *   0 on success, negative errno value on failure.
1545  */
1546 int
1547 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1548 {
1549         struct mlx5_priv *priv = dev->data->dev_private;
1550         struct rte_mempool *mp = priv->mprq_mp;
1551         unsigned int i;
1552
1553         if (mp == NULL)
1554                 return 0;
1555         DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1556                 dev->data->port_id, mp->name);
1557         /*
1558          * If a buffer in the pool has been externally attached to a mbuf and it
1559          * is still in use by application, destroying the Rx queue can spoil
1560          * the packet. It is unlikely to happen but if application dynamically
1561          * creates and destroys with holding Rx packets, this can happen.
1562          *
1563          * TODO: It is unavoidable for now because the mempool for Multi-Packet
1564          * RQ isn't provided by application but managed by PMD.
1565          */
1566         if (!rte_mempool_full(mp)) {
1567                 DRV_LOG(ERR,
1568                         "port %u mempool for Multi-Packet RQ is still in use",
1569                         dev->data->port_id);
1570                 rte_errno = EBUSY;
1571                 return -rte_errno;
1572         }
1573         rte_mempool_free(mp);
1574         /* Unset mempool for each Rx queue. */
1575         for (i = 0; i != priv->rxqs_n; ++i) {
1576                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1577
1578                 if (rxq == NULL)
1579                         continue;
1580                 rxq->mprq_mp = NULL;
1581         }
1582         priv->mprq_mp = NULL;
1583         return 0;
1584 }
1585
1586 /**
1587  * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1588  * mempool. If already allocated, reuse it if there're enough elements.
1589  * Otherwise, resize it.
1590  *
1591  * @param dev
1592  *   Pointer to Ethernet device.
1593  *
1594  * @return
1595  *   0 on success, negative errno value on failure.
1596  */
1597 int
1598 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1599 {
1600         struct mlx5_priv *priv = dev->data->dev_private;
1601         struct rte_mempool *mp = priv->mprq_mp;
1602         char name[RTE_MEMPOOL_NAMESIZE];
1603         unsigned int desc = 0;
1604         unsigned int buf_len;
1605         unsigned int obj_num;
1606         unsigned int obj_size;
1607         unsigned int strd_num_n = 0;
1608         unsigned int strd_sz_n = 0;
1609         unsigned int i;
1610         unsigned int n_ibv = 0;
1611
1612         if (!mlx5_mprq_enabled(dev))
1613                 return 0;
1614         /* Count the total number of descriptors configured. */
1615         for (i = 0; i != priv->rxqs_n; ++i) {
1616                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1617                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1618                         (rxq, struct mlx5_rxq_ctrl, rxq);
1619
1620                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1621                         continue;
1622                 n_ibv++;
1623                 desc += 1 << rxq->elts_n;
1624                 /* Get the max number of strides. */
1625                 if (strd_num_n < rxq->strd_num_n)
1626                         strd_num_n = rxq->strd_num_n;
1627                 /* Get the max size of a stride. */
1628                 if (strd_sz_n < rxq->strd_sz_n)
1629                         strd_sz_n = rxq->strd_sz_n;
1630         }
1631         assert(strd_num_n && strd_sz_n);
1632         buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1633         obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
1634                 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
1635         /*
1636          * Received packets can be either memcpy'd or externally referenced. In
1637          * case that the packet is attached to an mbuf as an external buffer, as
1638          * it isn't possible to predict how the buffers will be queued by
1639          * application, there's no option to exactly pre-allocate needed buffers
1640          * in advance but to speculatively prepares enough buffers.
1641          *
1642          * In the data path, if this Mempool is depleted, PMD will try to memcpy
1643          * received packets to buffers provided by application (rxq->mp) until
1644          * this Mempool gets available again.
1645          */
1646         desc *= 4;
1647         obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
1648         /*
1649          * rte_mempool_create_empty() has sanity check to refuse large cache
1650          * size compared to the number of elements.
1651          * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1652          * constant number 2 instead.
1653          */
1654         obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1655         /* Check a mempool is already allocated and if it can be resued. */
1656         if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1657                 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1658                         dev->data->port_id, mp->name);
1659                 /* Reuse. */
1660                 goto exit;
1661         } else if (mp != NULL) {
1662                 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1663                         dev->data->port_id, mp->name);
1664                 /*
1665                  * If failed to free, which means it may be still in use, no way
1666                  * but to keep using the existing one. On buffer underrun,
1667                  * packets will be memcpy'd instead of external buffer
1668                  * attachment.
1669                  */
1670                 if (mlx5_mprq_free_mp(dev)) {
1671                         if (mp->elt_size >= obj_size)
1672                                 goto exit;
1673                         else
1674                                 return -rte_errno;
1675                 }
1676         }
1677         snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
1678         mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1679                                 0, NULL, NULL, mlx5_mprq_buf_init,
1680                                 (void *)(uintptr_t)(1 << strd_num_n),
1681                                 dev->device->numa_node, 0);
1682         if (mp == NULL) {
1683                 DRV_LOG(ERR,
1684                         "port %u failed to allocate a mempool for"
1685                         " Multi-Packet RQ, count=%u, size=%u",
1686                         dev->data->port_id, obj_num, obj_size);
1687                 rte_errno = ENOMEM;
1688                 return -rte_errno;
1689         }
1690         priv->mprq_mp = mp;
1691 exit:
1692         /* Set mempool for each Rx queue. */
1693         for (i = 0; i != priv->rxqs_n; ++i) {
1694                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1695                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1696                         (rxq, struct mlx5_rxq_ctrl, rxq);
1697
1698                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1699                         continue;
1700                 rxq->mprq_mp = mp;
1701         }
1702         DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1703                 dev->data->port_id);
1704         return 0;
1705 }
1706
1707 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
1708                                         sizeof(struct rte_vlan_hdr) * 2 + \
1709                                         sizeof(struct rte_ipv6_hdr)))
1710 #define MAX_TCP_OPTION_SIZE 40u
1711 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
1712                                  sizeof(struct rte_tcp_hdr) + \
1713                                  MAX_TCP_OPTION_SIZE))
1714
1715 /**
1716  * Adjust the maximum LRO massage size.
1717  *
1718  * @param dev
1719  *   Pointer to Ethernet device.
1720  * @param idx
1721  *   RX queue index.
1722  * @param max_lro_size
1723  *   The maximum size for LRO packet.
1724  */
1725 static void
1726 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
1727                              uint32_t max_lro_size)
1728 {
1729         struct mlx5_priv *priv = dev->data->dev_private;
1730
1731         if (priv->config.hca_attr.lro_max_msg_sz_mode ==
1732             MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
1733             MLX5_MAX_TCP_HDR_OFFSET)
1734                 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
1735         max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
1736         assert(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
1737         max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
1738         if (priv->max_lro_msg_size)
1739                 priv->max_lro_msg_size =
1740                         RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
1741         else
1742                 priv->max_lro_msg_size = max_lro_size;
1743         DRV_LOG(DEBUG,
1744                 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
1745                 dev->data->port_id, idx,
1746                 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
1747 }
1748
1749 /**
1750  * Create a DPDK Rx queue.
1751  *
1752  * @param dev
1753  *   Pointer to Ethernet device.
1754  * @param idx
1755  *   RX queue index.
1756  * @param desc
1757  *   Number of descriptors to configure in queue.
1758  * @param socket
1759  *   NUMA socket on which memory must be allocated.
1760  *
1761  * @return
1762  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1763  */
1764 struct mlx5_rxq_ctrl *
1765 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1766              unsigned int socket, const struct rte_eth_rxconf *conf,
1767              struct rte_mempool *mp)
1768 {
1769         struct mlx5_priv *priv = dev->data->dev_private;
1770         struct mlx5_rxq_ctrl *tmpl;
1771         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
1772         unsigned int mprq_stride_size;
1773         struct mlx5_dev_config *config = &priv->config;
1774         unsigned int strd_headroom_en;
1775         /*
1776          * Always allocate extra slots, even if eventually
1777          * the vector Rx will not be used.
1778          */
1779         uint16_t desc_n =
1780                 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1781         uint64_t offloads = conf->offloads |
1782                            dev->data->dev_conf.rxmode.offloads;
1783         unsigned int lro_on_queue = !!(offloads & DEV_RX_OFFLOAD_TCP_LRO);
1784         const int mprq_en = mlx5_check_mprq_support(dev) > 0;
1785         unsigned int max_rx_pkt_len = lro_on_queue ?
1786                         dev->data->dev_conf.rxmode.max_lro_pkt_size :
1787                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
1788         unsigned int non_scatter_min_mbuf_size = max_rx_pkt_len +
1789                                                         RTE_PKTMBUF_HEADROOM;
1790         unsigned int max_lro_size = 0;
1791         unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
1792
1793         if (non_scatter_min_mbuf_size > mb_len && !(offloads &
1794                                                     DEV_RX_OFFLOAD_SCATTER)) {
1795                 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
1796                         " configured and no enough mbuf space(%u) to contain "
1797                         "the maximum RX packet length(%u) with head-room(%u)",
1798                         dev->data->port_id, idx, mb_len, max_rx_pkt_len,
1799                         RTE_PKTMBUF_HEADROOM);
1800                 rte_errno = ENOSPC;
1801                 return NULL;
1802         }
1803         tmpl = rte_calloc_socket("RXQ", 1,
1804                                  sizeof(*tmpl) +
1805                                  desc_n * sizeof(struct rte_mbuf *),
1806                                  0, socket);
1807         if (!tmpl) {
1808                 rte_errno = ENOMEM;
1809                 return NULL;
1810         }
1811         tmpl->type = MLX5_RXQ_TYPE_STANDARD;
1812         if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
1813                                MLX5_MR_BTREE_CACHE_N, socket)) {
1814                 /* rte_errno is already set. */
1815                 goto error;
1816         }
1817         tmpl->socket = socket;
1818         if (dev->data->dev_conf.intr_conf.rxq)
1819                 tmpl->irq = 1;
1820         /*
1821          * LRO packet may consume all the stride memory, hence we cannot
1822          * guaranty head-room near the packet memory in the stride.
1823          * In this case scatter is, for sure, enabled and an empty mbuf may be
1824          * added in the start for the head-room.
1825          */
1826         if (lro_on_queue && RTE_PKTMBUF_HEADROOM > 0 &&
1827             non_scatter_min_mbuf_size > mb_len) {
1828                 strd_headroom_en = 0;
1829                 mprq_stride_size = RTE_MIN(max_rx_pkt_len,
1830                                         1u << config->mprq.max_stride_size_n);
1831         } else {
1832                 strd_headroom_en = 1;
1833                 mprq_stride_size = non_scatter_min_mbuf_size;
1834         }
1835         /*
1836          * This Rx queue can be configured as a Multi-Packet RQ if all of the
1837          * following conditions are met:
1838          *  - MPRQ is enabled.
1839          *  - The number of descs is more than the number of strides.
1840          *  - max_rx_pkt_len plus overhead is less than the max size of a
1841          *    stride.
1842          *  Otherwise, enable Rx scatter if necessary.
1843          */
1844         if (mprq_en &&
1845             desc > (1U << config->mprq.stride_num_n) &&
1846             mprq_stride_size <= (1U << config->mprq.max_stride_size_n)) {
1847                 /* TODO: Rx scatter isn't supported yet. */
1848                 tmpl->rxq.sges_n = 0;
1849                 /* Trim the number of descs needed. */
1850                 desc >>= config->mprq.stride_num_n;
1851                 tmpl->rxq.strd_num_n = config->mprq.stride_num_n;
1852                 tmpl->rxq.strd_sz_n = RTE_MAX(log2above(mprq_stride_size),
1853                                               config->mprq.min_stride_size_n);
1854                 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1855                 tmpl->rxq.strd_headroom_en = strd_headroom_en;
1856                 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
1857                                 config->mprq.max_memcpy_len);
1858                 max_lro_size = RTE_MIN(max_rx_pkt_len,
1859                                        (1u << tmpl->rxq.strd_num_n) *
1860                                        (1u << tmpl->rxq.strd_sz_n));
1861                 DRV_LOG(DEBUG,
1862                         "port %u Rx queue %u: Multi-Packet RQ is enabled"
1863                         " strd_num_n = %u, strd_sz_n = %u",
1864                         dev->data->port_id, idx,
1865                         tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1866         } else if (max_rx_pkt_len <= first_mb_free_size) {
1867                 tmpl->rxq.sges_n = 0;
1868                 max_lro_size = max_rx_pkt_len;
1869         } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
1870                 unsigned int size = non_scatter_min_mbuf_size;
1871                 unsigned int sges_n;
1872
1873                 if (lro_on_queue && first_mb_free_size <
1874                     MLX5_MAX_LRO_HEADER_FIX) {
1875                         DRV_LOG(ERR, "Not enough space in the first segment(%u)"
1876                                 " to include the max header size(%u) for LRO",
1877                                 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
1878                         rte_errno = ENOTSUP;
1879                         goto error;
1880                 }
1881                 /*
1882                  * Determine the number of SGEs needed for a full packet
1883                  * and round it to the next power of two.
1884                  */
1885                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
1886                 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
1887                         DRV_LOG(ERR,
1888                                 "port %u too many SGEs (%u) needed to handle"
1889                                 " requested maximum packet size %u, the maximum"
1890                                 " supported are %u", dev->data->port_id,
1891                                 1 << sges_n, max_rx_pkt_len,
1892                                 1u << MLX5_MAX_LOG_RQ_SEGS);
1893                         rte_errno = ENOTSUP;
1894                         goto error;
1895                 }
1896                 tmpl->rxq.sges_n = sges_n;
1897                 max_lro_size = max_rx_pkt_len;
1898         }
1899         if (mprq_en && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1900                 DRV_LOG(WARNING,
1901                         "port %u MPRQ is requested but cannot be enabled"
1902                         " (requested: desc = %u, stride_sz = %u,"
1903                         " supported: min_stride_num = %u, max_stride_sz = %u).",
1904                         dev->data->port_id, desc, mprq_stride_size,
1905                         (1 << config->mprq.stride_num_n),
1906                         (1 << config->mprq.max_stride_size_n));
1907         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1908                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1909         if (desc % (1 << tmpl->rxq.sges_n)) {
1910                 DRV_LOG(ERR,
1911                         "port %u number of Rx queue descriptors (%u) is not a"
1912                         " multiple of SGEs per packet (%u)",
1913                         dev->data->port_id,
1914                         desc,
1915                         1 << tmpl->rxq.sges_n);
1916                 rte_errno = EINVAL;
1917                 goto error;
1918         }
1919         mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
1920         /* Toggle RX checksum offload if hardware supports it. */
1921         tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
1922         tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1923         /* Configure VLAN stripping. */
1924         tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1925         /* By default, FCS (CRC) is stripped by hardware. */
1926         tmpl->rxq.crc_present = 0;
1927         tmpl->rxq.lro = lro_on_queue;
1928         if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
1929                 if (config->hw_fcs_strip) {
1930                         /*
1931                          * RQs used for LRO-enabled TIRs should not be
1932                          * configured to scatter the FCS.
1933                          */
1934                         if (lro_on_queue)
1935                                 DRV_LOG(WARNING,
1936                                         "port %u CRC stripping has been "
1937                                         "disabled but will still be performed "
1938                                         "by hardware, because LRO is enabled",
1939                                         dev->data->port_id);
1940                         else
1941                                 tmpl->rxq.crc_present = 1;
1942                 } else {
1943                         DRV_LOG(WARNING,
1944                                 "port %u CRC stripping has been disabled but will"
1945                                 " still be performed by hardware, make sure MLNX_OFED"
1946                                 " and firmware are up to date",
1947                                 dev->data->port_id);
1948                 }
1949         }
1950         DRV_LOG(DEBUG,
1951                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1952                 " incoming frames to hide it",
1953                 dev->data->port_id,
1954                 tmpl->rxq.crc_present ? "disabled" : "enabled",
1955                 tmpl->rxq.crc_present << 2);
1956         /* Save port ID. */
1957         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1958                 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
1959         tmpl->rxq.port_id = dev->data->port_id;
1960         tmpl->priv = priv;
1961         tmpl->rxq.mp = mp;
1962         tmpl->rxq.elts_n = log2above(desc);
1963         tmpl->rxq.rq_repl_thresh =
1964                 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
1965         tmpl->rxq.elts =
1966                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1967 #ifndef RTE_ARCH_64
1968         tmpl->rxq.uar_lock_cq = &priv->uar_lock_cq;
1969 #endif
1970         tmpl->rxq.idx = idx;
1971         rte_atomic32_inc(&tmpl->refcnt);
1972         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1973         return tmpl;
1974 error:
1975         rte_free(tmpl);
1976         return NULL;
1977 }
1978
1979 /**
1980  * Create a DPDK Rx hairpin queue.
1981  *
1982  * @param dev
1983  *   Pointer to Ethernet device.
1984  * @param idx
1985  *   RX queue index.
1986  * @param desc
1987  *   Number of descriptors to configure in queue.
1988  * @param hairpin_conf
1989  *   The hairpin binding configuration.
1990  *
1991  * @return
1992  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1993  */
1994 struct mlx5_rxq_ctrl *
1995 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1996                      const struct rte_eth_hairpin_conf *hairpin_conf)
1997 {
1998         struct mlx5_priv *priv = dev->data->dev_private;
1999         struct mlx5_rxq_ctrl *tmpl;
2000
2001         tmpl = rte_calloc_socket("RXQ", 1, sizeof(*tmpl), 0, SOCKET_ID_ANY);
2002         if (!tmpl) {
2003                 rte_errno = ENOMEM;
2004                 return NULL;
2005         }
2006         tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
2007         tmpl->socket = SOCKET_ID_ANY;
2008         tmpl->rxq.rss_hash = 0;
2009         tmpl->rxq.port_id = dev->data->port_id;
2010         tmpl->priv = priv;
2011         tmpl->rxq.mp = NULL;
2012         tmpl->rxq.elts_n = log2above(desc);
2013         tmpl->rxq.elts = NULL;
2014         tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
2015         tmpl->hairpin_conf = *hairpin_conf;
2016         tmpl->rxq.idx = idx;
2017         rte_atomic32_inc(&tmpl->refcnt);
2018         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
2019         return tmpl;
2020 }
2021
2022 /**
2023  * Get a Rx queue.
2024  *
2025  * @param dev
2026  *   Pointer to Ethernet device.
2027  * @param idx
2028  *   RX queue index.
2029  *
2030  * @return
2031  *   A pointer to the queue if it exists, NULL otherwise.
2032  */
2033 struct mlx5_rxq_ctrl *
2034 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
2035 {
2036         struct mlx5_priv *priv = dev->data->dev_private;
2037         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2038
2039         if ((*priv->rxqs)[idx]) {
2040                 rxq_ctrl = container_of((*priv->rxqs)[idx],
2041                                         struct mlx5_rxq_ctrl,
2042                                         rxq);
2043                 mlx5_rxq_obj_get(dev, idx);
2044                 rte_atomic32_inc(&rxq_ctrl->refcnt);
2045         }
2046         return rxq_ctrl;
2047 }
2048
2049 /**
2050  * Release a Rx queue.
2051  *
2052  * @param dev
2053  *   Pointer to Ethernet device.
2054  * @param idx
2055  *   RX queue index.
2056  *
2057  * @return
2058  *   1 while a reference on it exists, 0 when freed.
2059  */
2060 int
2061 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
2062 {
2063         struct mlx5_priv *priv = dev->data->dev_private;
2064         struct mlx5_rxq_ctrl *rxq_ctrl;
2065
2066         if (!(*priv->rxqs)[idx])
2067                 return 0;
2068         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
2069         assert(rxq_ctrl->priv);
2070         if (rxq_ctrl->obj && !mlx5_rxq_obj_release(rxq_ctrl->obj))
2071                 rxq_ctrl->obj = NULL;
2072         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
2073                 if (rxq_ctrl->dbr_umem_id_valid)
2074                         claim_zero(mlx5_release_dbr(dev, rxq_ctrl->dbr_umem_id,
2075                                                     rxq_ctrl->dbr_offset));
2076                 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
2077                         mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
2078                 LIST_REMOVE(rxq_ctrl, next);
2079                 rte_free(rxq_ctrl);
2080                 (*priv->rxqs)[idx] = NULL;
2081                 return 0;
2082         }
2083         return 1;
2084 }
2085
2086 /**
2087  * Verify the Rx Queue list is empty
2088  *
2089  * @param dev
2090  *   Pointer to Ethernet device.
2091  *
2092  * @return
2093  *   The number of object not released.
2094  */
2095 int
2096 mlx5_rxq_verify(struct rte_eth_dev *dev)
2097 {
2098         struct mlx5_priv *priv = dev->data->dev_private;
2099         struct mlx5_rxq_ctrl *rxq_ctrl;
2100         int ret = 0;
2101
2102         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
2103                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
2104                         dev->data->port_id, rxq_ctrl->rxq.idx);
2105                 ++ret;
2106         }
2107         return ret;
2108 }
2109
2110 /**
2111  * Get a Rx queue type.
2112  *
2113  * @param dev
2114  *   Pointer to Ethernet device.
2115  * @param idx
2116  *   Rx queue index.
2117  *
2118  * @return
2119  *   The Rx queue type.
2120  */
2121 enum mlx5_rxq_type
2122 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
2123 {
2124         struct mlx5_priv *priv = dev->data->dev_private;
2125         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2126
2127         if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
2128                 rxq_ctrl = container_of((*priv->rxqs)[idx],
2129                                         struct mlx5_rxq_ctrl,
2130                                         rxq);
2131                 return rxq_ctrl->type;
2132         }
2133         return MLX5_RXQ_TYPE_UNDEFINED;
2134 }
2135
2136 /**
2137  * Create an indirection table.
2138  *
2139  * @param dev
2140  *   Pointer to Ethernet device.
2141  * @param queues
2142  *   Queues entering in the indirection table.
2143  * @param queues_n
2144  *   Number of queues in the array.
2145  *
2146  * @return
2147  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2148  */
2149 static struct mlx5_ind_table_obj *
2150 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
2151                        uint32_t queues_n, enum mlx5_ind_tbl_type type)
2152 {
2153         struct mlx5_priv *priv = dev->data->dev_private;
2154         struct mlx5_ind_table_obj *ind_tbl;
2155         unsigned int i = 0, j = 0, k = 0;
2156
2157         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
2158                              queues_n * sizeof(uint16_t), 0);
2159         if (!ind_tbl) {
2160                 rte_errno = ENOMEM;
2161                 return NULL;
2162         }
2163         ind_tbl->type = type;
2164         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2165                 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
2166                         log2above(queues_n) :
2167                         log2above(priv->config.ind_table_max_size);
2168                 struct ibv_wq *wq[1 << wq_n];
2169
2170                 for (i = 0; i != queues_n; ++i) {
2171                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2172                                                                  queues[i]);
2173                         if (!rxq)
2174                                 goto error;
2175                         wq[i] = rxq->obj->wq;
2176                         ind_tbl->queues[i] = queues[i];
2177                 }
2178                 ind_tbl->queues_n = queues_n;
2179                 /* Finalise indirection table. */
2180                 k = i; /* Retain value of i for use in error case. */
2181                 for (j = 0; k != (unsigned int)(1 << wq_n); ++k, ++j)
2182                         wq[k] = wq[j];
2183                 ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
2184                         (priv->sh->ctx,
2185                          &(struct ibv_rwq_ind_table_init_attr){
2186                                 .log_ind_tbl_size = wq_n,
2187                                 .ind_tbl = wq,
2188                                 .comp_mask = 0,
2189                         });
2190                 if (!ind_tbl->ind_table) {
2191                         rte_errno = errno;
2192                         goto error;
2193                 }
2194         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2195                 struct mlx5_devx_rqt_attr *rqt_attr = NULL;
2196                 const unsigned int rqt_n =
2197                         1 << (rte_is_power_of_2(queues_n) ?
2198                               log2above(queues_n) :
2199                               log2above(priv->config.ind_table_max_size));
2200
2201                 rqt_attr = rte_calloc(__func__, 1, sizeof(*rqt_attr) +
2202                                       rqt_n * sizeof(uint32_t), 0);
2203                 if (!rqt_attr) {
2204                         DRV_LOG(ERR, "port %u cannot allocate RQT resources",
2205                                 dev->data->port_id);
2206                         rte_errno = ENOMEM;
2207                         goto error;
2208                 }
2209                 rqt_attr->rqt_max_size = priv->config.ind_table_max_size;
2210                 rqt_attr->rqt_actual_size = rqt_n;
2211                 for (i = 0; i != queues_n; ++i) {
2212                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2213                                                                  queues[i]);
2214                         if (!rxq)
2215                                 goto error;
2216                         rqt_attr->rq_list[i] = rxq->obj->rq->id;
2217                         ind_tbl->queues[i] = queues[i];
2218                 }
2219                 k = i; /* Retain value of i for use in error case. */
2220                 for (j = 0; k != rqt_n; ++k, ++j)
2221                         rqt_attr->rq_list[k] = rqt_attr->rq_list[j];
2222                 ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->ctx,
2223                                                         rqt_attr);
2224                 rte_free(rqt_attr);
2225                 if (!ind_tbl->rqt) {
2226                         DRV_LOG(ERR, "port %u cannot create DevX RQT",
2227                                 dev->data->port_id);
2228                         rte_errno = errno;
2229                         goto error;
2230                 }
2231                 ind_tbl->queues_n = queues_n;
2232         }
2233         rte_atomic32_inc(&ind_tbl->refcnt);
2234         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
2235         return ind_tbl;
2236 error:
2237         for (j = 0; j < i; j++)
2238                 mlx5_rxq_release(dev, ind_tbl->queues[j]);
2239         rte_free(ind_tbl);
2240         DEBUG("port %u cannot create indirection table", dev->data->port_id);
2241         return NULL;
2242 }
2243
2244 /**
2245  * Get an indirection table.
2246  *
2247  * @param dev
2248  *   Pointer to Ethernet device.
2249  * @param queues
2250  *   Queues entering in the indirection table.
2251  * @param queues_n
2252  *   Number of queues in the array.
2253  *
2254  * @return
2255  *   An indirection table if found.
2256  */
2257 static struct mlx5_ind_table_obj *
2258 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
2259                        uint32_t queues_n)
2260 {
2261         struct mlx5_priv *priv = dev->data->dev_private;
2262         struct mlx5_ind_table_obj *ind_tbl;
2263
2264         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2265                 if ((ind_tbl->queues_n == queues_n) &&
2266                     (memcmp(ind_tbl->queues, queues,
2267                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
2268                      == 0))
2269                         break;
2270         }
2271         if (ind_tbl) {
2272                 unsigned int i;
2273
2274                 rte_atomic32_inc(&ind_tbl->refcnt);
2275                 for (i = 0; i != ind_tbl->queues_n; ++i)
2276                         mlx5_rxq_get(dev, ind_tbl->queues[i]);
2277         }
2278         return ind_tbl;
2279 }
2280
2281 /**
2282  * Release an indirection table.
2283  *
2284  * @param dev
2285  *   Pointer to Ethernet device.
2286  * @param ind_table
2287  *   Indirection table to release.
2288  *
2289  * @return
2290  *   1 while a reference on it exists, 0 when freed.
2291  */
2292 static int
2293 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
2294                            struct mlx5_ind_table_obj *ind_tbl)
2295 {
2296         unsigned int i;
2297
2298         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt)) {
2299                 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV)
2300                         claim_zero(mlx5_glue->destroy_rwq_ind_table
2301                                                         (ind_tbl->ind_table));
2302                 else if (ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX)
2303                         claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
2304         }
2305         for (i = 0; i != ind_tbl->queues_n; ++i)
2306                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
2307         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
2308                 LIST_REMOVE(ind_tbl, next);
2309                 rte_free(ind_tbl);
2310                 return 0;
2311         }
2312         return 1;
2313 }
2314
2315 /**
2316  * Verify the Rx Queue list is empty
2317  *
2318  * @param dev
2319  *   Pointer to Ethernet device.
2320  *
2321  * @return
2322  *   The number of object not released.
2323  */
2324 int
2325 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
2326 {
2327         struct mlx5_priv *priv = dev->data->dev_private;
2328         struct mlx5_ind_table_obj *ind_tbl;
2329         int ret = 0;
2330
2331         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2332                 DRV_LOG(DEBUG,
2333                         "port %u indirection table obj %p still referenced",
2334                         dev->data->port_id, (void *)ind_tbl);
2335                 ++ret;
2336         }
2337         return ret;
2338 }
2339
2340 /**
2341  * Create an Rx Hash queue.
2342  *
2343  * @param dev
2344  *   Pointer to Ethernet device.
2345  * @param rss_key
2346  *   RSS key for the Rx hash queue.
2347  * @param rss_key_len
2348  *   RSS key length.
2349  * @param hash_fields
2350  *   Verbs protocol hash field to make the RSS on.
2351  * @param queues
2352  *   Queues entering in hash queue. In case of empty hash_fields only the
2353  *   first queue index will be taken for the indirection table.
2354  * @param queues_n
2355  *   Number of queues.
2356  * @param tunnel
2357  *   Tunnel type.
2358  *
2359  * @return
2360  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2361  */
2362 struct mlx5_hrxq *
2363 mlx5_hrxq_new(struct rte_eth_dev *dev,
2364               const uint8_t *rss_key, uint32_t rss_key_len,
2365               uint64_t hash_fields,
2366               const uint16_t *queues, uint32_t queues_n,
2367               int tunnel __rte_unused)
2368 {
2369         struct mlx5_priv *priv = dev->data->dev_private;
2370         struct mlx5_hrxq *hrxq;
2371         struct ibv_qp *qp = NULL;
2372         struct mlx5_ind_table_obj *ind_tbl;
2373         int err;
2374         struct mlx5_devx_obj *tir = NULL;
2375         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[queues[0]];
2376         struct mlx5_rxq_ctrl *rxq_ctrl =
2377                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2378
2379         queues_n = hash_fields ? queues_n : 1;
2380         ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2381         if (!ind_tbl) {
2382                 enum mlx5_ind_tbl_type type;
2383
2384                 type = rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV ?
2385                                 MLX5_IND_TBL_TYPE_IBV : MLX5_IND_TBL_TYPE_DEVX;
2386                 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n, type);
2387         }
2388         if (!ind_tbl) {
2389                 rte_errno = ENOMEM;
2390                 return NULL;
2391         }
2392         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2393 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2394                 struct mlx5dv_qp_init_attr qp_init_attr;
2395
2396                 memset(&qp_init_attr, 0, sizeof(qp_init_attr));
2397                 if (tunnel) {
2398                         qp_init_attr.comp_mask =
2399                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2400                         qp_init_attr.create_flags =
2401                                 MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
2402                 }
2403 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2404                 if (dev->data->dev_conf.lpbk_mode) {
2405                         /*
2406                          * Allow packet sent from NIC loop back
2407                          * w/o source MAC check.
2408                          */
2409                         qp_init_attr.comp_mask |=
2410                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2411                         qp_init_attr.create_flags |=
2412                                 MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
2413                 }
2414 #endif
2415                 qp = mlx5_glue->dv_create_qp
2416                         (priv->sh->ctx,
2417                          &(struct ibv_qp_init_attr_ex){
2418                                 .qp_type = IBV_QPT_RAW_PACKET,
2419                                 .comp_mask =
2420                                         IBV_QP_INIT_ATTR_PD |
2421                                         IBV_QP_INIT_ATTR_IND_TABLE |
2422                                         IBV_QP_INIT_ATTR_RX_HASH,
2423                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2424                                         .rx_hash_function =
2425                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2426                                         .rx_hash_key_len = rss_key_len,
2427                                         .rx_hash_key =
2428                                                 (void *)(uintptr_t)rss_key,
2429                                         .rx_hash_fields_mask = hash_fields,
2430                                 },
2431                                 .rwq_ind_tbl = ind_tbl->ind_table,
2432                                 .pd = priv->sh->pd,
2433                           },
2434                           &qp_init_attr);
2435 #else
2436                 qp = mlx5_glue->create_qp_ex
2437                         (priv->sh->ctx,
2438                          &(struct ibv_qp_init_attr_ex){
2439                                 .qp_type = IBV_QPT_RAW_PACKET,
2440                                 .comp_mask =
2441                                         IBV_QP_INIT_ATTR_PD |
2442                                         IBV_QP_INIT_ATTR_IND_TABLE |
2443                                         IBV_QP_INIT_ATTR_RX_HASH,
2444                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2445                                         .rx_hash_function =
2446                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2447                                         .rx_hash_key_len = rss_key_len,
2448                                         .rx_hash_key =
2449                                                 (void *)(uintptr_t)rss_key,
2450                                         .rx_hash_fields_mask = hash_fields,
2451                                 },
2452                                 .rwq_ind_tbl = ind_tbl->ind_table,
2453                                 .pd = priv->sh->pd,
2454                          });
2455 #endif
2456                 if (!qp) {
2457                         rte_errno = errno;
2458                         goto error;
2459                 }
2460         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2461                 struct mlx5_devx_tir_attr tir_attr;
2462                 uint32_t i;
2463                 uint32_t lro = 1;
2464
2465                 /* Enable TIR LRO only if all the queues were configured for. */
2466                 for (i = 0; i < queues_n; ++i) {
2467                         if (!(*priv->rxqs)[queues[i]]->lro) {
2468                                 lro = 0;
2469                                 break;
2470                         }
2471                 }
2472                 memset(&tir_attr, 0, sizeof(tir_attr));
2473                 tir_attr.disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
2474                 tir_attr.rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
2475                 memcpy(&tir_attr.rx_hash_field_selector_outer, &hash_fields,
2476                        sizeof(uint64_t));
2477                 if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
2478                         tir_attr.transport_domain = priv->sh->td->id;
2479                 else
2480                         tir_attr.transport_domain = priv->sh->tdn;
2481                 memcpy(tir_attr.rx_hash_toeplitz_key, rss_key, rss_key_len);
2482                 tir_attr.indirect_table = ind_tbl->rqt->id;
2483                 if (dev->data->dev_conf.lpbk_mode)
2484                         tir_attr.self_lb_block =
2485                                         MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
2486                 if (lro) {
2487                         tir_attr.lro_timeout_period_usecs =
2488                                         priv->config.lro.timeout;
2489                         tir_attr.lro_max_msg_sz = priv->max_lro_msg_size;
2490                         tir_attr.lro_enable_mask =
2491                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2492                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO;
2493                 }
2494                 tir = mlx5_devx_cmd_create_tir(priv->sh->ctx, &tir_attr);
2495                 if (!tir) {
2496                         DRV_LOG(ERR, "port %u cannot create DevX TIR",
2497                                 dev->data->port_id);
2498                         rte_errno = errno;
2499                         goto error;
2500                 }
2501         }
2502         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
2503         if (!hrxq)
2504                 goto error;
2505         hrxq->ind_table = ind_tbl;
2506         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2507                 hrxq->qp = qp;
2508 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2509                 hrxq->action =
2510                         mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
2511                 if (!hrxq->action) {
2512                         rte_errno = errno;
2513                         goto error;
2514                 }
2515 #endif
2516         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2517                 hrxq->tir = tir;
2518 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2519                 hrxq->action = mlx5_glue->dv_create_flow_action_dest_devx_tir
2520                                                         (hrxq->tir->obj);
2521                 if (!hrxq->action) {
2522                         rte_errno = errno;
2523                         goto error;
2524                 }
2525 #endif
2526         }
2527         hrxq->rss_key_len = rss_key_len;
2528         hrxq->hash_fields = hash_fields;
2529         memcpy(hrxq->rss_key, rss_key, rss_key_len);
2530         rte_atomic32_inc(&hrxq->refcnt);
2531         LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
2532         return hrxq;
2533 error:
2534         err = rte_errno; /* Save rte_errno before cleanup. */
2535         mlx5_ind_table_obj_release(dev, ind_tbl);
2536         if (qp)
2537                 claim_zero(mlx5_glue->destroy_qp(qp));
2538         else if (tir)
2539                 claim_zero(mlx5_devx_cmd_destroy(tir));
2540         rte_errno = err; /* Restore rte_errno. */
2541         return NULL;
2542 }
2543
2544 /**
2545  * Get an Rx Hash queue.
2546  *
2547  * @param dev
2548  *   Pointer to Ethernet device.
2549  * @param rss_conf
2550  *   RSS configuration for the Rx hash queue.
2551  * @param queues
2552  *   Queues entering in hash queue. In case of empty hash_fields only the
2553  *   first queue index will be taken for the indirection table.
2554  * @param queues_n
2555  *   Number of queues.
2556  *
2557  * @return
2558  *   An hash Rx queue on success.
2559  */
2560 struct mlx5_hrxq *
2561 mlx5_hrxq_get(struct rte_eth_dev *dev,
2562               const uint8_t *rss_key, uint32_t rss_key_len,
2563               uint64_t hash_fields,
2564               const uint16_t *queues, uint32_t queues_n)
2565 {
2566         struct mlx5_priv *priv = dev->data->dev_private;
2567         struct mlx5_hrxq *hrxq;
2568
2569         queues_n = hash_fields ? queues_n : 1;
2570         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
2571                 struct mlx5_ind_table_obj *ind_tbl;
2572
2573                 if (hrxq->rss_key_len != rss_key_len)
2574                         continue;
2575                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
2576                         continue;
2577                 if (hrxq->hash_fields != hash_fields)
2578                         continue;
2579                 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2580                 if (!ind_tbl)
2581                         continue;
2582                 if (ind_tbl != hrxq->ind_table) {
2583                         mlx5_ind_table_obj_release(dev, ind_tbl);
2584                         continue;
2585                 }
2586                 rte_atomic32_inc(&hrxq->refcnt);
2587                 return hrxq;
2588         }
2589         return NULL;
2590 }
2591
2592 /**
2593  * Release the hash Rx queue.
2594  *
2595  * @param dev
2596  *   Pointer to Ethernet device.
2597  * @param hrxq
2598  *   Pointer to Hash Rx queue to release.
2599  *
2600  * @return
2601  *   1 while a reference on it exists, 0 when freed.
2602  */
2603 int
2604 mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
2605 {
2606         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2607 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2608                 mlx5_glue->destroy_flow_action(hrxq->action);
2609 #endif
2610                 if (hrxq->ind_table->type == MLX5_IND_TBL_TYPE_IBV)
2611                         claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2612                 else /* hrxq->ind_table->type == MLX5_IND_TBL_TYPE_DEVX */
2613                         claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
2614                 mlx5_ind_table_obj_release(dev, hrxq->ind_table);
2615                 LIST_REMOVE(hrxq, next);
2616                 rte_free(hrxq);
2617                 return 0;
2618         }
2619         claim_nonzero(mlx5_ind_table_obj_release(dev, hrxq->ind_table));
2620         return 1;
2621 }
2622
2623 /**
2624  * Verify the Rx Queue list is empty
2625  *
2626  * @param dev
2627  *   Pointer to Ethernet device.
2628  *
2629  * @return
2630  *   The number of object not released.
2631  */
2632 int
2633 mlx5_hrxq_verify(struct rte_eth_dev *dev)
2634 {
2635         struct mlx5_priv *priv = dev->data->dev_private;
2636         struct mlx5_hrxq *hrxq;
2637         int ret = 0;
2638
2639         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
2640                 DRV_LOG(DEBUG,
2641                         "port %u hash Rx queue %p still referenced",
2642                         dev->data->port_id, (void *)hrxq);
2643                 ++ret;
2644         }
2645         return ret;
2646 }
2647
2648 /**
2649  * Create a drop Rx queue Verbs/DevX object.
2650  *
2651  * @param dev
2652  *   Pointer to Ethernet device.
2653  *
2654  * @return
2655  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2656  */
2657 static struct mlx5_rxq_obj *
2658 mlx5_rxq_obj_drop_new(struct rte_eth_dev *dev)
2659 {
2660         struct mlx5_priv *priv = dev->data->dev_private;
2661         struct ibv_context *ctx = priv->sh->ctx;
2662         struct ibv_cq *cq;
2663         struct ibv_wq *wq = NULL;
2664         struct mlx5_rxq_obj *rxq;
2665
2666         if (priv->drop_queue.rxq)
2667                 return priv->drop_queue.rxq;
2668         cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
2669         if (!cq) {
2670                 DEBUG("port %u cannot allocate CQ for drop queue",
2671                       dev->data->port_id);
2672                 rte_errno = errno;
2673                 goto error;
2674         }
2675         wq = mlx5_glue->create_wq(ctx,
2676                  &(struct ibv_wq_init_attr){
2677                         .wq_type = IBV_WQT_RQ,
2678                         .max_wr = 1,
2679                         .max_sge = 1,
2680                         .pd = priv->sh->pd,
2681                         .cq = cq,
2682                  });
2683         if (!wq) {
2684                 DEBUG("port %u cannot allocate WQ for drop queue",
2685                       dev->data->port_id);
2686                 rte_errno = errno;
2687                 goto error;
2688         }
2689         rxq = rte_calloc(__func__, 1, sizeof(*rxq), 0);
2690         if (!rxq) {
2691                 DEBUG("port %u cannot allocate drop Rx queue memory",
2692                       dev->data->port_id);
2693                 rte_errno = ENOMEM;
2694                 goto error;
2695         }
2696         rxq->cq = cq;
2697         rxq->wq = wq;
2698         priv->drop_queue.rxq = rxq;
2699         return rxq;
2700 error:
2701         if (wq)
2702                 claim_zero(mlx5_glue->destroy_wq(wq));
2703         if (cq)
2704                 claim_zero(mlx5_glue->destroy_cq(cq));
2705         return NULL;
2706 }
2707
2708 /**
2709  * Release a drop Rx queue Verbs/DevX object.
2710  *
2711  * @param dev
2712  *   Pointer to Ethernet device.
2713  *
2714  * @return
2715  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2716  */
2717 static void
2718 mlx5_rxq_obj_drop_release(struct rte_eth_dev *dev)
2719 {
2720         struct mlx5_priv *priv = dev->data->dev_private;
2721         struct mlx5_rxq_obj *rxq = priv->drop_queue.rxq;
2722
2723         if (rxq->wq)
2724                 claim_zero(mlx5_glue->destroy_wq(rxq->wq));
2725         if (rxq->cq)
2726                 claim_zero(mlx5_glue->destroy_cq(rxq->cq));
2727         rte_free(rxq);
2728         priv->drop_queue.rxq = NULL;
2729 }
2730
2731 /**
2732  * Create a drop indirection table.
2733  *
2734  * @param dev
2735  *   Pointer to Ethernet device.
2736  *
2737  * @return
2738  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2739  */
2740 static struct mlx5_ind_table_obj *
2741 mlx5_ind_table_obj_drop_new(struct rte_eth_dev *dev)
2742 {
2743         struct mlx5_priv *priv = dev->data->dev_private;
2744         struct mlx5_ind_table_obj *ind_tbl;
2745         struct mlx5_rxq_obj *rxq;
2746         struct mlx5_ind_table_obj tmpl;
2747
2748         rxq = mlx5_rxq_obj_drop_new(dev);
2749         if (!rxq)
2750                 return NULL;
2751         tmpl.ind_table = mlx5_glue->create_rwq_ind_table
2752                 (priv->sh->ctx,
2753                  &(struct ibv_rwq_ind_table_init_attr){
2754                         .log_ind_tbl_size = 0,
2755                         .ind_tbl = &rxq->wq,
2756                         .comp_mask = 0,
2757                  });
2758         if (!tmpl.ind_table) {
2759                 DEBUG("port %u cannot allocate indirection table for drop"
2760                       " queue",
2761                       dev->data->port_id);
2762                 rte_errno = errno;
2763                 goto error;
2764         }
2765         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl), 0);
2766         if (!ind_tbl) {
2767                 rte_errno = ENOMEM;
2768                 goto error;
2769         }
2770         ind_tbl->ind_table = tmpl.ind_table;
2771         return ind_tbl;
2772 error:
2773         mlx5_rxq_obj_drop_release(dev);
2774         return NULL;
2775 }
2776
2777 /**
2778  * Release a drop indirection table.
2779  *
2780  * @param dev
2781  *   Pointer to Ethernet device.
2782  */
2783 static void
2784 mlx5_ind_table_obj_drop_release(struct rte_eth_dev *dev)
2785 {
2786         struct mlx5_priv *priv = dev->data->dev_private;
2787         struct mlx5_ind_table_obj *ind_tbl = priv->drop_queue.hrxq->ind_table;
2788
2789         claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
2790         mlx5_rxq_obj_drop_release(dev);
2791         rte_free(ind_tbl);
2792         priv->drop_queue.hrxq->ind_table = NULL;
2793 }
2794
2795 /**
2796  * Create a drop Rx Hash queue.
2797  *
2798  * @param dev
2799  *   Pointer to Ethernet device.
2800  *
2801  * @return
2802  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2803  */
2804 struct mlx5_hrxq *
2805 mlx5_hrxq_drop_new(struct rte_eth_dev *dev)
2806 {
2807         struct mlx5_priv *priv = dev->data->dev_private;
2808         struct mlx5_ind_table_obj *ind_tbl = NULL;
2809         struct ibv_qp *qp = NULL;
2810         struct mlx5_hrxq *hrxq = NULL;
2811
2812         if (priv->drop_queue.hrxq) {
2813                 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
2814                 return priv->drop_queue.hrxq;
2815         }
2816         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq), 0);
2817         if (!hrxq) {
2818                 DRV_LOG(WARNING,
2819                         "port %u cannot allocate memory for drop queue",
2820                         dev->data->port_id);
2821                 rte_errno = ENOMEM;
2822                 goto error;
2823         }
2824         priv->drop_queue.hrxq = hrxq;
2825         ind_tbl = mlx5_ind_table_obj_drop_new(dev);
2826         if (!ind_tbl)
2827                 goto error;
2828         hrxq->ind_table = ind_tbl;
2829         qp = mlx5_glue->create_qp_ex(priv->sh->ctx,
2830                  &(struct ibv_qp_init_attr_ex){
2831                         .qp_type = IBV_QPT_RAW_PACKET,
2832                         .comp_mask =
2833                                 IBV_QP_INIT_ATTR_PD |
2834                                 IBV_QP_INIT_ATTR_IND_TABLE |
2835                                 IBV_QP_INIT_ATTR_RX_HASH,
2836                         .rx_hash_conf = (struct ibv_rx_hash_conf){
2837                                 .rx_hash_function =
2838                                         IBV_RX_HASH_FUNC_TOEPLITZ,
2839                                 .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
2840                                 .rx_hash_key = rss_hash_default_key,
2841                                 .rx_hash_fields_mask = 0,
2842                                 },
2843                         .rwq_ind_tbl = ind_tbl->ind_table,
2844                         .pd = priv->sh->pd
2845                  });
2846         if (!qp) {
2847                 DEBUG("port %u cannot allocate QP for drop queue",
2848                       dev->data->port_id);
2849                 rte_errno = errno;
2850                 goto error;
2851         }
2852         hrxq->qp = qp;
2853 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2854         hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
2855         if (!hrxq->action) {
2856                 rte_errno = errno;
2857                 goto error;
2858         }
2859 #endif
2860         rte_atomic32_set(&hrxq->refcnt, 1);
2861         return hrxq;
2862 error:
2863 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2864         if (hrxq && hrxq->action)
2865                 mlx5_glue->destroy_flow_action(hrxq->action);
2866 #endif
2867         if (qp)
2868                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2869         if (ind_tbl)
2870                 mlx5_ind_table_obj_drop_release(dev);
2871         if (hrxq) {
2872                 priv->drop_queue.hrxq = NULL;
2873                 rte_free(hrxq);
2874         }
2875         return NULL;
2876 }
2877
2878 /**
2879  * Release a drop hash Rx queue.
2880  *
2881  * @param dev
2882  *   Pointer to Ethernet device.
2883  */
2884 void
2885 mlx5_hrxq_drop_release(struct rte_eth_dev *dev)
2886 {
2887         struct mlx5_priv *priv = dev->data->dev_private;
2888         struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2889
2890         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2891 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2892                 mlx5_glue->destroy_flow_action(hrxq->action);
2893 #endif
2894                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2895                 mlx5_ind_table_obj_drop_release(dev);
2896                 rte_free(hrxq);
2897                 priv->drop_queue.hrxq = NULL;
2898         }
2899 }