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