e6dc5aca70f9244da9db7cc46e75490f7075e4a7
[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 <errno.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <fcntl.h>
11 #include <sys/queue.h>
12
13 #include <rte_mbuf.h>
14 #include <rte_malloc.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_common.h>
17 #include <rte_interrupts.h>
18 #include <rte_debug.h>
19 #include <rte_io.h>
20 #include <rte_eal_paging.h>
21
22 #include <mlx5_glue.h>
23 #include <mlx5_devx_cmds.h>
24 #include <mlx5_malloc.h>
25
26 #include "mlx5_defs.h"
27 #include "mlx5.h"
28 #include "mlx5_common_os.h"
29 #include "mlx5_rxtx.h"
30 #include "mlx5_utils.h"
31 #include "mlx5_autoconf.h"
32 #include "mlx5_flow.h"
33
34
35 /* Default RSS hash key also used for ConnectX-3. */
36 uint8_t rss_hash_default_key[] = {
37         0x2c, 0xc6, 0x81, 0xd1,
38         0x5b, 0xdb, 0xf4, 0xf7,
39         0xfc, 0xa2, 0x83, 0x19,
40         0xdb, 0x1a, 0x3e, 0x94,
41         0x6b, 0x9e, 0x38, 0xd9,
42         0x2c, 0x9c, 0x03, 0xd1,
43         0xad, 0x99, 0x44, 0xa7,
44         0xd9, 0x56, 0x3d, 0x59,
45         0x06, 0x3c, 0x25, 0xf3,
46         0xfc, 0x1f, 0xdc, 0x2a,
47 };
48
49 /* Length of the default RSS hash key. */
50 static_assert(MLX5_RSS_HASH_KEY_LEN ==
51               (unsigned int)sizeof(rss_hash_default_key),
52               "wrong RSS default key size.");
53
54 /**
55  * Check whether Multi-Packet RQ can be enabled for the device.
56  *
57  * @param dev
58  *   Pointer to Ethernet device.
59  *
60  * @return
61  *   1 if supported, negative errno value if not.
62  */
63 inline int
64 mlx5_check_mprq_support(struct rte_eth_dev *dev)
65 {
66         struct mlx5_priv *priv = dev->data->dev_private;
67
68         if (priv->config.mprq.enabled &&
69             priv->rxqs_n >= priv->config.mprq.min_rxqs_num)
70                 return 1;
71         return -ENOTSUP;
72 }
73
74 /**
75  * Check whether Multi-Packet RQ is enabled for the Rx queue.
76  *
77  *  @param rxq
78  *     Pointer to receive queue structure.
79  *
80  * @return
81  *   0 if disabled, otherwise enabled.
82  */
83 inline int
84 mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq)
85 {
86         return rxq->strd_num_n > 0;
87 }
88
89 /**
90  * Check whether Multi-Packet RQ is enabled for the device.
91  *
92  * @param dev
93  *   Pointer to Ethernet device.
94  *
95  * @return
96  *   0 if disabled, otherwise enabled.
97  */
98 inline int
99 mlx5_mprq_enabled(struct rte_eth_dev *dev)
100 {
101         struct mlx5_priv *priv = dev->data->dev_private;
102         uint32_t i;
103         uint16_t n = 0;
104         uint16_t n_ibv = 0;
105
106         if (mlx5_check_mprq_support(dev) < 0)
107                 return 0;
108         /* All the configured queues should be enabled. */
109         for (i = 0; i < priv->rxqs_n; ++i) {
110                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
111                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
112                         (rxq, struct mlx5_rxq_ctrl, rxq);
113
114                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
115                         continue;
116                 n_ibv++;
117                 if (mlx5_rxq_mprq_enabled(rxq))
118                         ++n;
119         }
120         /* Multi-Packet RQ can't be partially configured. */
121         MLX5_ASSERT(n == 0 || n == n_ibv);
122         return n == n_ibv;
123 }
124
125 /**
126  * Allocate RX queue elements for Multi-Packet RQ.
127  *
128  * @param rxq_ctrl
129  *   Pointer to RX queue structure.
130  *
131  * @return
132  *   0 on success, a negative errno value otherwise and rte_errno is set.
133  */
134 static int
135 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
136 {
137         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
138         unsigned int wqe_n = 1 << rxq->elts_n;
139         unsigned int i;
140         int err;
141
142         /* Iterate on segments. */
143         for (i = 0; i <= wqe_n; ++i) {
144                 struct mlx5_mprq_buf *buf;
145
146                 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
147                         DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
148                         rte_errno = ENOMEM;
149                         goto error;
150                 }
151                 if (i < wqe_n)
152                         (*rxq->mprq_bufs)[i] = buf;
153                 else
154                         rxq->mprq_repl = buf;
155         }
156         DRV_LOG(DEBUG,
157                 "port %u Rx queue %u allocated and configured %u segments",
158                 rxq->port_id, rxq->idx, wqe_n);
159         return 0;
160 error:
161         err = rte_errno; /* Save rte_errno before cleanup. */
162         wqe_n = i;
163         for (i = 0; (i != wqe_n); ++i) {
164                 if ((*rxq->mprq_bufs)[i] != NULL)
165                         rte_mempool_put(rxq->mprq_mp,
166                                         (*rxq->mprq_bufs)[i]);
167                 (*rxq->mprq_bufs)[i] = NULL;
168         }
169         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
170                 rxq->port_id, rxq->idx);
171         rte_errno = err; /* Restore rte_errno. */
172         return -rte_errno;
173 }
174
175 /**
176  * Allocate RX queue elements for Single-Packet RQ.
177  *
178  * @param rxq_ctrl
179  *   Pointer to RX queue structure.
180  *
181  * @return
182  *   0 on success, errno value on failure.
183  */
184 static int
185 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
186 {
187         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
188         unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
189         unsigned int i;
190         int err;
191
192         /* Iterate on segments. */
193         for (i = 0; (i != elts_n); ++i) {
194                 struct rte_mbuf *buf;
195
196                 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
197                 if (buf == NULL) {
198                         DRV_LOG(ERR, "port %u empty mbuf pool",
199                                 PORT_ID(rxq_ctrl->priv));
200                         rte_errno = ENOMEM;
201                         goto error;
202                 }
203                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
204                 MLX5_ASSERT(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
205                 /* Buffer is supposed to be empty. */
206                 MLX5_ASSERT(rte_pktmbuf_data_len(buf) == 0);
207                 MLX5_ASSERT(rte_pktmbuf_pkt_len(buf) == 0);
208                 MLX5_ASSERT(!buf->next);
209                 /* Only the first segment keeps headroom. */
210                 if (i % sges_n)
211                         SET_DATA_OFF(buf, 0);
212                 PORT(buf) = rxq_ctrl->rxq.port_id;
213                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
214                 PKT_LEN(buf) = DATA_LEN(buf);
215                 NB_SEGS(buf) = 1;
216                 (*rxq_ctrl->rxq.elts)[i] = buf;
217         }
218         /* If Rx vector is activated. */
219         if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
220                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
221                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
222                 struct rte_pktmbuf_pool_private *priv =
223                         (struct rte_pktmbuf_pool_private *)
224                                 rte_mempool_get_priv(rxq_ctrl->rxq.mp);
225                 int j;
226
227                 /* Initialize default rearm_data for vPMD. */
228                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
229                 rte_mbuf_refcnt_set(mbuf_init, 1);
230                 mbuf_init->nb_segs = 1;
231                 mbuf_init->port = rxq->port_id;
232                 if (priv->flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
233                         mbuf_init->ol_flags = EXT_ATTACHED_MBUF;
234                 /*
235                  * prevent compiler reordering:
236                  * rearm_data covers previous fields.
237                  */
238                 rte_compiler_barrier();
239                 rxq->mbuf_initializer =
240                         *(rte_xmm_t *)&mbuf_init->rearm_data;
241                 /* Padding with a fake mbuf for vectorized Rx. */
242                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
243                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
244         }
245         DRV_LOG(DEBUG,
246                 "port %u Rx queue %u allocated and configured %u segments"
247                 " (max %u packets)",
248                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx, elts_n,
249                 elts_n / (1 << rxq_ctrl->rxq.sges_n));
250         return 0;
251 error:
252         err = rte_errno; /* Save rte_errno before cleanup. */
253         elts_n = i;
254         for (i = 0; (i != elts_n); ++i) {
255                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
256                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
257                 (*rxq_ctrl->rxq.elts)[i] = NULL;
258         }
259         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
260                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx);
261         rte_errno = err; /* Restore rte_errno. */
262         return -rte_errno;
263 }
264
265 /**
266  * Allocate RX queue elements.
267  *
268  * @param rxq_ctrl
269  *   Pointer to RX queue structure.
270  *
271  * @return
272  *   0 on success, errno value on failure.
273  */
274 int
275 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
276 {
277         return mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
278                rxq_alloc_elts_mprq(rxq_ctrl) : rxq_alloc_elts_sprq(rxq_ctrl);
279 }
280
281 /**
282  * Free RX queue elements for Multi-Packet RQ.
283  *
284  * @param rxq_ctrl
285  *   Pointer to RX queue structure.
286  */
287 static void
288 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
289 {
290         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
291         uint16_t i;
292
293         DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing WRs",
294                 rxq->port_id, rxq->idx);
295         if (rxq->mprq_bufs == NULL)
296                 return;
297         MLX5_ASSERT(mlx5_rxq_check_vec_support(rxq) < 0);
298         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
299                 if ((*rxq->mprq_bufs)[i] != NULL)
300                         mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
301                 (*rxq->mprq_bufs)[i] = NULL;
302         }
303         if (rxq->mprq_repl != NULL) {
304                 mlx5_mprq_buf_free(rxq->mprq_repl);
305                 rxq->mprq_repl = NULL;
306         }
307 }
308
309 /**
310  * Free RX queue elements for Single-Packet RQ.
311  *
312  * @param rxq_ctrl
313  *   Pointer to RX queue structure.
314  */
315 static void
316 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
317 {
318         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
319         const uint16_t q_n = (1 << rxq->elts_n);
320         const uint16_t q_mask = q_n - 1;
321         uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
322         uint16_t i;
323
324         DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
325                 PORT_ID(rxq_ctrl->priv), rxq->idx);
326         if (rxq->elts == NULL)
327                 return;
328         /**
329          * Some mbuf in the Ring belongs to the application.  They cannot be
330          * freed.
331          */
332         if (mlx5_rxq_check_vec_support(rxq) > 0) {
333                 for (i = 0; i < used; ++i)
334                         (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
335                 rxq->rq_pi = rxq->rq_ci;
336         }
337         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
338                 if ((*rxq->elts)[i] != NULL)
339                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
340                 (*rxq->elts)[i] = NULL;
341         }
342 }
343
344 /**
345  * Free RX queue elements.
346  *
347  * @param rxq_ctrl
348  *   Pointer to RX queue structure.
349  */
350 static void
351 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
352 {
353         if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
354                 rxq_free_elts_mprq(rxq_ctrl);
355         else
356                 rxq_free_elts_sprq(rxq_ctrl);
357 }
358
359 /**
360  * Returns the per-queue supported offloads.
361  *
362  * @param dev
363  *   Pointer to Ethernet device.
364  *
365  * @return
366  *   Supported Rx offloads.
367  */
368 uint64_t
369 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
370 {
371         struct mlx5_priv *priv = dev->data->dev_private;
372         struct mlx5_dev_config *config = &priv->config;
373         uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
374                              DEV_RX_OFFLOAD_TIMESTAMP |
375                              DEV_RX_OFFLOAD_JUMBO_FRAME |
376                              DEV_RX_OFFLOAD_RSS_HASH);
377
378         if (config->hw_fcs_strip)
379                 offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
380
381         if (config->hw_csum)
382                 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
383                              DEV_RX_OFFLOAD_UDP_CKSUM |
384                              DEV_RX_OFFLOAD_TCP_CKSUM);
385         if (config->hw_vlan_strip)
386                 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
387         if (MLX5_LRO_SUPPORTED(dev))
388                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
389         return offloads;
390 }
391
392
393 /**
394  * Returns the per-port supported offloads.
395  *
396  * @return
397  *   Supported Rx offloads.
398  */
399 uint64_t
400 mlx5_get_rx_port_offloads(void)
401 {
402         uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
403
404         return offloads;
405 }
406
407 /**
408  * Verify if the queue can be released.
409  *
410  * @param dev
411  *   Pointer to Ethernet device.
412  * @param idx
413  *   RX queue index.
414  *
415  * @return
416  *   1 if the queue can be released
417  *   0 if the queue can not be released, there are references to it.
418  *   Negative errno and rte_errno is set if queue doesn't exist.
419  */
420 static int
421 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
422 {
423         struct mlx5_priv *priv = dev->data->dev_private;
424         struct mlx5_rxq_ctrl *rxq_ctrl;
425
426         if (!(*priv->rxqs)[idx]) {
427                 rte_errno = EINVAL;
428                 return -rte_errno;
429         }
430         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
431         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
432 }
433
434 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
435 static void
436 rxq_sync_cq(struct mlx5_rxq_data *rxq)
437 {
438         const uint16_t cqe_n = 1 << rxq->cqe_n;
439         const uint16_t cqe_mask = cqe_n - 1;
440         volatile struct mlx5_cqe *cqe;
441         int ret, i;
442
443         i = cqe_n;
444         do {
445                 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_mask];
446                 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
447                 if (ret == MLX5_CQE_STATUS_HW_OWN)
448                         break;
449                 if (ret == MLX5_CQE_STATUS_ERR) {
450                         rxq->cq_ci++;
451                         continue;
452                 }
453                 MLX5_ASSERT(ret == MLX5_CQE_STATUS_SW_OWN);
454                 if (MLX5_CQE_FORMAT(cqe->op_own) != MLX5_COMPRESSED) {
455                         rxq->cq_ci++;
456                         continue;
457                 }
458                 /* Compute the next non compressed CQE. */
459                 rxq->cq_ci += rte_be_to_cpu_32(cqe->byte_cnt);
460
461         } while (--i);
462         /* Move all CQEs to HW ownership, including possible MiniCQEs. */
463         for (i = 0; i < cqe_n; i++) {
464                 cqe = &(*rxq->cqes)[i];
465                 cqe->op_own = MLX5_CQE_INVALIDATE;
466         }
467         /* Resync CQE and WQE (WQ in RESET state). */
468         rte_cio_wmb();
469         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
470         rte_cio_wmb();
471         *rxq->rq_db = rte_cpu_to_be_32(0);
472         rte_cio_wmb();
473 }
474
475 /**
476  * Rx queue stop. Device queue goes to the RESET state,
477  * all involved mbufs are freed from WQ.
478  *
479  * @param dev
480  *   Pointer to Ethernet device structure.
481  * @param idx
482  *   RX queue index.
483  *
484  * @return
485  *   0 on success, a negative errno value otherwise and rte_errno is set.
486  */
487 int
488 mlx5_rx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
489 {
490         struct mlx5_priv *priv = dev->data->dev_private;
491         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
492         struct mlx5_rxq_ctrl *rxq_ctrl =
493                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
494         int ret;
495
496         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
497         if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
498                 struct ibv_wq_attr mod = {
499                         .attr_mask = IBV_WQ_ATTR_STATE,
500                         .wq_state = IBV_WQS_RESET,
501                 };
502
503                 ret = mlx5_glue->modify_wq(rxq_ctrl->obj->wq, &mod);
504         } else { /* rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ. */
505                 struct mlx5_devx_modify_rq_attr rq_attr;
506
507                 memset(&rq_attr, 0, sizeof(rq_attr));
508                 rq_attr.rq_state = MLX5_RQC_STATE_RST;
509                 rq_attr.state = MLX5_RQC_STATE_RDY;
510                 ret = mlx5_devx_cmd_modify_rq(rxq_ctrl->obj->rq, &rq_attr);
511         }
512         if (ret) {
513                 DRV_LOG(ERR, "Cannot change Rx WQ state to RESET:  %s",
514                         strerror(errno));
515                 rte_errno = errno;
516                 return ret;
517         }
518         /* Remove all processes CQEs. */
519         rxq_sync_cq(rxq);
520         /* Free all involved mbufs. */
521         rxq_free_elts(rxq_ctrl);
522         /* Set the actual queue state. */
523         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
524         return 0;
525 }
526
527 /**
528  * Rx queue stop. Device queue goes to the RESET state,
529  * all involved mbufs are freed from WQ.
530  *
531  * @param dev
532  *   Pointer to Ethernet device structure.
533  * @param idx
534  *   RX queue index.
535  *
536  * @return
537  *   0 on success, a negative errno value otherwise and rte_errno is set.
538  */
539 int
540 mlx5_rx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
541 {
542         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
543         int ret;
544
545         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_HAIRPIN) {
546                 DRV_LOG(ERR, "Hairpin queue can't be stopped");
547                 rte_errno = EINVAL;
548                 return -EINVAL;
549         }
550         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
551                 return 0;
552         /*
553          * Vectorized Rx burst requires the CQ and RQ indices
554          * synchronized, that might be broken on RQ restart
555          * and cause Rx malfunction, so queue stopping is
556          * not supported if vectorized Rx burst is engaged.
557          * The routine pointer depends on the process
558          * type, should perform check there.
559          */
560         if (pkt_burst == mlx5_rx_burst) {
561                 DRV_LOG(ERR, "Rx queue stop is not supported "
562                         "for vectorized Rx");
563                 rte_errno = EINVAL;
564                 return -EINVAL;
565         }
566         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
567                 ret = mlx5_mp_os_req_queue_control(dev, idx,
568                                                    MLX5_MP_REQ_QUEUE_RX_STOP);
569         } else {
570                 ret = mlx5_rx_queue_stop_primary(dev, idx);
571         }
572         return ret;
573 }
574
575 /**
576  * Rx queue start. Device queue goes to the ready state,
577  * all required mbufs are allocated and WQ is replenished.
578  *
579  * @param dev
580  *   Pointer to Ethernet device structure.
581  * @param idx
582  *   RX queue index.
583  *
584  * @return
585  *   0 on success, a negative errno value otherwise and rte_errno is set.
586  */
587 int
588 mlx5_rx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
589 {
590         struct mlx5_priv *priv = dev->data->dev_private;
591         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
592         struct mlx5_rxq_ctrl *rxq_ctrl =
593                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
594         int ret;
595
596         MLX5_ASSERT(rte_eal_process_type() ==  RTE_PROC_PRIMARY);
597         /* Allocate needed buffers. */
598         ret = rxq_alloc_elts(rxq_ctrl);
599         if (ret) {
600                 DRV_LOG(ERR, "Cannot reallocate buffers for Rx WQ");
601                 rte_errno = errno;
602                 return ret;
603         }
604         rte_cio_wmb();
605         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
606         rte_cio_wmb();
607         /* Reset RQ consumer before moving queue ro READY state. */
608         *rxq->rq_db = rte_cpu_to_be_32(0);
609         rte_cio_wmb();
610         if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
611                 struct ibv_wq_attr mod = {
612                         .attr_mask = IBV_WQ_ATTR_STATE,
613                         .wq_state = IBV_WQS_RDY,
614                 };
615
616                 ret = mlx5_glue->modify_wq(rxq_ctrl->obj->wq, &mod);
617         } else { /* rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ. */
618                 struct mlx5_devx_modify_rq_attr rq_attr;
619
620                 memset(&rq_attr, 0, sizeof(rq_attr));
621                 rq_attr.rq_state = MLX5_RQC_STATE_RDY;
622                 rq_attr.state = MLX5_RQC_STATE_RST;
623                 ret = mlx5_devx_cmd_modify_rq(rxq_ctrl->obj->rq, &rq_attr);
624         }
625         if (ret) {
626                 DRV_LOG(ERR, "Cannot change Rx WQ state to READY:  %s",
627                         strerror(errno));
628                 rte_errno = errno;
629                 return ret;
630         }
631         /* Reinitialize RQ - set WQEs. */
632         mlx5_rxq_initialize(rxq);
633         rxq->err_state = MLX5_RXQ_ERR_STATE_NO_ERROR;
634         /* Set actual queue state. */
635         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
636         return 0;
637 }
638
639 /**
640  * Rx queue start. Device queue goes to the ready state,
641  * all required mbufs are allocated and WQ is replenished.
642  *
643  * @param dev
644  *   Pointer to Ethernet device structure.
645  * @param idx
646  *   RX queue index.
647  *
648  * @return
649  *   0 on success, a negative errno value otherwise and rte_errno is set.
650  */
651 int
652 mlx5_rx_queue_start(struct rte_eth_dev *dev, uint16_t idx)
653 {
654         int ret;
655
656         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_HAIRPIN) {
657                 DRV_LOG(ERR, "Hairpin queue can't be started");
658                 rte_errno = EINVAL;
659                 return -EINVAL;
660         }
661         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
662                 return 0;
663         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
664                 ret = mlx5_mp_os_req_queue_control(dev, idx,
665                                                    MLX5_MP_REQ_QUEUE_RX_START);
666         } else {
667                 ret = mlx5_rx_queue_start_primary(dev, idx);
668         }
669         return ret;
670 }
671
672 /**
673  * Rx queue presetup checks.
674  *
675  * @param dev
676  *   Pointer to Ethernet device structure.
677  * @param idx
678  *   RX queue index.
679  * @param desc
680  *   Number of descriptors to configure in queue.
681  *
682  * @return
683  *   0 on success, a negative errno value otherwise and rte_errno is set.
684  */
685 static int
686 mlx5_rx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc)
687 {
688         struct mlx5_priv *priv = dev->data->dev_private;
689
690         if (!rte_is_power_of_2(*desc)) {
691                 *desc = 1 << log2above(*desc);
692                 DRV_LOG(WARNING,
693                         "port %u increased number of descriptors in Rx queue %u"
694                         " to the next power of two (%d)",
695                         dev->data->port_id, idx, *desc);
696         }
697         DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
698                 dev->data->port_id, idx, *desc);
699         if (idx >= priv->rxqs_n) {
700                 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
701                         dev->data->port_id, idx, priv->rxqs_n);
702                 rte_errno = EOVERFLOW;
703                 return -rte_errno;
704         }
705         if (!mlx5_rxq_releasable(dev, idx)) {
706                 DRV_LOG(ERR, "port %u unable to release queue index %u",
707                         dev->data->port_id, idx);
708                 rte_errno = EBUSY;
709                 return -rte_errno;
710         }
711         mlx5_rxq_release(dev, idx);
712         return 0;
713 }
714
715 /**
716  *
717  * @param dev
718  *   Pointer to Ethernet device structure.
719  * @param idx
720  *   RX queue index.
721  * @param desc
722  *   Number of descriptors to configure in queue.
723  * @param socket
724  *   NUMA socket on which memory must be allocated.
725  * @param[in] conf
726  *   Thresholds parameters.
727  * @param mp
728  *   Memory pool for buffer allocations.
729  *
730  * @return
731  *   0 on success, a negative errno value otherwise and rte_errno is set.
732  */
733 int
734 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
735                     unsigned int socket, const struct rte_eth_rxconf *conf,
736                     struct rte_mempool *mp)
737 {
738         struct mlx5_priv *priv = dev->data->dev_private;
739         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
740         struct mlx5_rxq_ctrl *rxq_ctrl =
741                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
742         int res;
743
744         res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
745         if (res)
746                 return res;
747         rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, mp);
748         if (!rxq_ctrl) {
749                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
750                         dev->data->port_id, idx);
751                 rte_errno = ENOMEM;
752                 return -rte_errno;
753         }
754         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
755                 dev->data->port_id, idx);
756         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
757         return 0;
758 }
759
760 /**
761  *
762  * @param dev
763  *   Pointer to Ethernet device structure.
764  * @param idx
765  *   RX queue index.
766  * @param desc
767  *   Number of descriptors to configure in queue.
768  * @param hairpin_conf
769  *   Hairpin configuration parameters.
770  *
771  * @return
772  *   0 on success, a negative errno value otherwise and rte_errno is set.
773  */
774 int
775 mlx5_rx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
776                             uint16_t desc,
777                             const struct rte_eth_hairpin_conf *hairpin_conf)
778 {
779         struct mlx5_priv *priv = dev->data->dev_private;
780         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
781         struct mlx5_rxq_ctrl *rxq_ctrl =
782                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
783         int res;
784
785         res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
786         if (res)
787                 return res;
788         if (hairpin_conf->peer_count != 1 ||
789             hairpin_conf->peers[0].port != dev->data->port_id ||
790             hairpin_conf->peers[0].queue >= priv->txqs_n) {
791                 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
792                         " invalid hairpind configuration", dev->data->port_id,
793                         idx);
794                 rte_errno = EINVAL;
795                 return -rte_errno;
796         }
797         rxq_ctrl = mlx5_rxq_hairpin_new(dev, idx, desc, hairpin_conf);
798         if (!rxq_ctrl) {
799                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
800                         dev->data->port_id, idx);
801                 rte_errno = ENOMEM;
802                 return -rte_errno;
803         }
804         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
805                 dev->data->port_id, idx);
806         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
807         return 0;
808 }
809
810 /**
811  * DPDK callback to release a RX queue.
812  *
813  * @param dpdk_rxq
814  *   Generic RX queue pointer.
815  */
816 void
817 mlx5_rx_queue_release(void *dpdk_rxq)
818 {
819         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
820         struct mlx5_rxq_ctrl *rxq_ctrl;
821         struct mlx5_priv *priv;
822
823         if (rxq == NULL)
824                 return;
825         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
826         priv = rxq_ctrl->priv;
827         if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.idx))
828                 rte_panic("port %u Rx queue %u is still used by a flow and"
829                           " cannot be removed\n",
830                           PORT_ID(priv), rxq->idx);
831         mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.idx);
832 }
833
834 /**
835  * Get an Rx queue Verbs/DevX object.
836  *
837  * @param dev
838  *   Pointer to Ethernet device.
839  * @param idx
840  *   Queue index in DPDK Rx queue array
841  *
842  * @return
843  *   The Verbs/DevX object if it exists.
844  */
845 static struct mlx5_rxq_obj *
846 mlx5_rxq_obj_get(struct rte_eth_dev *dev, uint16_t idx)
847 {
848         struct mlx5_priv *priv = dev->data->dev_private;
849         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
850         struct mlx5_rxq_ctrl *rxq_ctrl;
851
852         if (idx >= priv->rxqs_n)
853                 return NULL;
854         if (!rxq_data)
855                 return NULL;
856         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
857         if (rxq_ctrl->obj)
858                 rte_atomic32_inc(&rxq_ctrl->obj->refcnt);
859         return rxq_ctrl->obj;
860 }
861
862 /**
863  * Release the resources allocated for an RQ DevX object.
864  *
865  * @param rxq_ctrl
866  *   DevX Rx queue object.
867  */
868 static void
869 rxq_release_devx_rq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
870 {
871         if (rxq_ctrl->rxq.wqes) {
872                 mlx5_free((void *)(uintptr_t)rxq_ctrl->rxq.wqes);
873                 rxq_ctrl->rxq.wqes = NULL;
874         }
875         if (rxq_ctrl->wq_umem) {
876                 mlx5_glue->devx_umem_dereg(rxq_ctrl->wq_umem);
877                 rxq_ctrl->wq_umem = NULL;
878         }
879 }
880
881 /**
882  * Release the resources allocated for the Rx CQ DevX object.
883  *
884  * @param rxq_ctrl
885  *   DevX Rx queue object.
886  */
887 static void
888 rxq_release_devx_cq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
889 {
890         if (rxq_ctrl->rxq.cqes) {
891                 rte_free((void *)(uintptr_t)rxq_ctrl->rxq.cqes);
892                 rxq_ctrl->rxq.cqes = NULL;
893         }
894         if (rxq_ctrl->cq_umem) {
895                 mlx5_glue->devx_umem_dereg(rxq_ctrl->cq_umem);
896                 rxq_ctrl->cq_umem = NULL;
897         }
898 }
899
900 /**
901  * Release an Rx hairpin related resources.
902  *
903  * @param rxq_obj
904  *   Hairpin Rx queue object.
905  */
906 static void
907 rxq_obj_hairpin_release(struct mlx5_rxq_obj *rxq_obj)
908 {
909         struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
910
911         MLX5_ASSERT(rxq_obj);
912         rq_attr.state = MLX5_RQC_STATE_RST;
913         rq_attr.rq_state = MLX5_RQC_STATE_RDY;
914         mlx5_devx_cmd_modify_rq(rxq_obj->rq, &rq_attr);
915         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
916 }
917
918 /**
919  * Release an Rx verbs/DevX queue object.
920  *
921  * @param rxq_obj
922  *   Verbs/DevX Rx queue object.
923  *
924  * @return
925  *   1 while a reference on it exists, 0 when freed.
926  */
927 static int
928 mlx5_rxq_obj_release(struct mlx5_rxq_obj *rxq_obj)
929 {
930         MLX5_ASSERT(rxq_obj);
931         if (rte_atomic32_dec_and_test(&rxq_obj->refcnt)) {
932                 switch (rxq_obj->type) {
933                 case MLX5_RXQ_OBJ_TYPE_IBV:
934                         MLX5_ASSERT(rxq_obj->wq);
935                         MLX5_ASSERT(rxq_obj->ibv_cq);
936                         rxq_free_elts(rxq_obj->rxq_ctrl);
937                         claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
938                         claim_zero(mlx5_glue->destroy_cq(rxq_obj->ibv_cq));
939                         if (rxq_obj->ibv_channel)
940                                 claim_zero(mlx5_glue->destroy_comp_channel
941                                            (rxq_obj->ibv_channel));
942                         break;
943                 case MLX5_RXQ_OBJ_TYPE_DEVX_RQ:
944                         MLX5_ASSERT(rxq_obj->rq);
945                         MLX5_ASSERT(rxq_obj->devx_cq);
946                         rxq_free_elts(rxq_obj->rxq_ctrl);
947                         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
948                         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->devx_cq));
949                         if (rxq_obj->devx_channel)
950                                 mlx5_glue->devx_destroy_event_channel
951                                                         (rxq_obj->devx_channel);
952                         rxq_release_devx_rq_resources(rxq_obj->rxq_ctrl);
953                         rxq_release_devx_cq_resources(rxq_obj->rxq_ctrl);
954                         break;
955                 case MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN:
956                         MLX5_ASSERT(rxq_obj->rq);
957                         rxq_obj_hairpin_release(rxq_obj);
958                         break;
959                 }
960                 LIST_REMOVE(rxq_obj, next);
961                 mlx5_free(rxq_obj);
962                 return 0;
963         }
964         return 1;
965 }
966
967 /**
968  * Allocate queue vector and fill epoll fd list for Rx interrupts.
969  *
970  * @param dev
971  *   Pointer to Ethernet device.
972  *
973  * @return
974  *   0 on success, a negative errno value otherwise and rte_errno is set.
975  */
976 int
977 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
978 {
979         struct mlx5_priv *priv = dev->data->dev_private;
980         unsigned int i;
981         unsigned int rxqs_n = priv->rxqs_n;
982         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
983         unsigned int count = 0;
984         struct rte_intr_handle *intr_handle = dev->intr_handle;
985
986         if (!dev->data->dev_conf.intr_conf.rxq)
987                 return 0;
988         mlx5_rx_intr_vec_disable(dev);
989         intr_handle->intr_vec = mlx5_malloc(0,
990                                 n * sizeof(intr_handle->intr_vec[0]),
991                                 0, SOCKET_ID_ANY);
992         if (intr_handle->intr_vec == NULL) {
993                 DRV_LOG(ERR,
994                         "port %u failed to allocate memory for interrupt"
995                         " vector, Rx interrupts will not be supported",
996                         dev->data->port_id);
997                 rte_errno = ENOMEM;
998                 return -rte_errno;
999         }
1000         intr_handle->type = RTE_INTR_HANDLE_EXT;
1001         for (i = 0; i != n; ++i) {
1002                 /* This rxq obj must not be released in this function. */
1003                 struct mlx5_rxq_obj *rxq_obj = mlx5_rxq_obj_get(dev, i);
1004                 int rc;
1005
1006                 /* Skip queues that cannot request interrupts. */
1007                 if (!rxq_obj || (!rxq_obj->ibv_channel &&
1008                                  !rxq_obj->devx_channel)) {
1009                         /* Use invalid intr_vec[] index to disable entry. */
1010                         intr_handle->intr_vec[i] =
1011                                 RTE_INTR_VEC_RXTX_OFFSET +
1012                                 RTE_MAX_RXTX_INTR_VEC_ID;
1013                         continue;
1014                 }
1015                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
1016                         DRV_LOG(ERR,
1017                                 "port %u too many Rx queues for interrupt"
1018                                 " vector size (%d), Rx interrupts cannot be"
1019                                 " enabled",
1020                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
1021                         mlx5_rx_intr_vec_disable(dev);
1022                         rte_errno = ENOMEM;
1023                         return -rte_errno;
1024                 }
1025                 rc = mlx5_os_set_nonblock_channel_fd(rxq_obj->fd);
1026                 if (rc < 0) {
1027                         rte_errno = errno;
1028                         DRV_LOG(ERR,
1029                                 "port %u failed to make Rx interrupt file"
1030                                 " descriptor %d non-blocking for queue index"
1031                                 " %d",
1032                                 dev->data->port_id, rxq_obj->fd, i);
1033                         mlx5_rx_intr_vec_disable(dev);
1034                         return -rte_errno;
1035                 }
1036                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
1037                 intr_handle->efds[count] = rxq_obj->fd;
1038                 count++;
1039         }
1040         if (!count)
1041                 mlx5_rx_intr_vec_disable(dev);
1042         else
1043                 intr_handle->nb_efd = count;
1044         return 0;
1045 }
1046
1047 /**
1048  * Clean up Rx interrupts handler.
1049  *
1050  * @param dev
1051  *   Pointer to Ethernet device.
1052  */
1053 void
1054 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
1055 {
1056         struct mlx5_priv *priv = dev->data->dev_private;
1057         struct rte_intr_handle *intr_handle = dev->intr_handle;
1058         unsigned int i;
1059         unsigned int rxqs_n = priv->rxqs_n;
1060         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1061
1062         if (!dev->data->dev_conf.intr_conf.rxq)
1063                 return;
1064         if (!intr_handle->intr_vec)
1065                 goto free;
1066         for (i = 0; i != n; ++i) {
1067                 struct mlx5_rxq_ctrl *rxq_ctrl;
1068                 struct mlx5_rxq_data *rxq_data;
1069
1070                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
1071                     RTE_MAX_RXTX_INTR_VEC_ID)
1072                         continue;
1073                 /**
1074                  * Need to access directly the queue to release the reference
1075                  * kept in mlx5_rx_intr_vec_enable().
1076                  */
1077                 rxq_data = (*priv->rxqs)[i];
1078                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1079                 if (rxq_ctrl->obj)
1080                         mlx5_rxq_obj_release(rxq_ctrl->obj);
1081         }
1082 free:
1083         rte_intr_free_epoll_fd(intr_handle);
1084         if (intr_handle->intr_vec)
1085                 mlx5_free(intr_handle->intr_vec);
1086         intr_handle->nb_efd = 0;
1087         intr_handle->intr_vec = NULL;
1088 }
1089
1090 /**
1091  *  MLX5 CQ notification .
1092  *
1093  *  @param rxq
1094  *     Pointer to receive queue structure.
1095  *  @param sq_n_rxq
1096  *     Sequence number per receive queue .
1097  */
1098 static inline void
1099 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
1100 {
1101         int sq_n = 0;
1102         uint32_t doorbell_hi;
1103         uint64_t doorbell;
1104         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
1105
1106         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
1107         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
1108         doorbell = (uint64_t)doorbell_hi << 32;
1109         doorbell |= rxq->cqn;
1110         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
1111         mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
1112                          cq_db_reg, rxq->uar_lock_cq);
1113 }
1114
1115 /**
1116  * DPDK callback for Rx queue interrupt enable.
1117  *
1118  * @param dev
1119  *   Pointer to Ethernet device structure.
1120  * @param rx_queue_id
1121  *   Rx queue number.
1122  *
1123  * @return
1124  *   0 on success, a negative errno value otherwise and rte_errno is set.
1125  */
1126 int
1127 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1128 {
1129         struct mlx5_priv *priv = dev->data->dev_private;
1130         struct mlx5_rxq_data *rxq_data;
1131         struct mlx5_rxq_ctrl *rxq_ctrl;
1132
1133         rxq_data = (*priv->rxqs)[rx_queue_id];
1134         if (!rxq_data) {
1135                 rte_errno = EINVAL;
1136                 return -rte_errno;
1137         }
1138         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1139         if (rxq_ctrl->irq) {
1140                 struct mlx5_rxq_obj *rxq_obj;
1141
1142                 rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
1143                 if (!rxq_obj) {
1144                         rte_errno = EINVAL;
1145                         return -rte_errno;
1146                 }
1147                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
1148                 mlx5_rxq_obj_release(rxq_obj);
1149         }
1150         return 0;
1151 }
1152
1153 /**
1154  * DPDK callback for Rx queue interrupt disable.
1155  *
1156  * @param dev
1157  *   Pointer to Ethernet device structure.
1158  * @param rx_queue_id
1159  *   Rx queue number.
1160  *
1161  * @return
1162  *   0 on success, a negative errno value otherwise and rte_errno is set.
1163  */
1164 int
1165 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1166 {
1167         struct mlx5_priv *priv = dev->data->dev_private;
1168         struct mlx5_rxq_data *rxq_data;
1169         struct mlx5_rxq_ctrl *rxq_ctrl;
1170         struct mlx5_rxq_obj *rxq_obj = NULL;
1171         struct ibv_cq *ev_cq;
1172         void *ev_ctx;
1173         int ret;
1174
1175         rxq_data = (*priv->rxqs)[rx_queue_id];
1176         if (!rxq_data) {
1177                 rte_errno = EINVAL;
1178                 return -rte_errno;
1179         }
1180         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1181         if (!rxq_ctrl->irq)
1182                 return 0;
1183         rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
1184         if (!rxq_obj) {
1185                 rte_errno = EINVAL;
1186                 return -rte_errno;
1187         }
1188         if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1189                 ret = mlx5_glue->get_cq_event(rxq_obj->ibv_channel, &ev_cq,
1190                                               &ev_ctx);
1191                 if (ret || ev_cq != rxq_obj->ibv_cq) {
1192                         rte_errno = EINVAL;
1193                         goto exit;
1194                 }
1195                 mlx5_glue->ack_cq_events(rxq_obj->ibv_cq, 1);
1196         } else if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1197 #ifdef HAVE_IBV_DEVX_EVENT
1198                 struct mlx5dv_devx_async_event_hdr *event_data = NULL;
1199
1200                 ret = mlx5_glue->devx_get_event
1201                                 (rxq_obj->devx_channel, event_data,
1202                                  sizeof(struct mlx5dv_devx_async_event_hdr));
1203                 if (ret <= 0 || event_data->cookie !=
1204                                 (uint64_t)(uintptr_t)rxq_obj->devx_cq) {
1205                         rte_errno = EINVAL;
1206                         goto exit;
1207                 }
1208 #endif /* HAVE_IBV_DEVX_EVENT */
1209         }
1210         rxq_data->cq_arm_sn++;
1211         mlx5_rxq_obj_release(rxq_obj);
1212         return 0;
1213 exit:
1214         ret = rte_errno; /* Save rte_errno before cleanup. */
1215         if (rxq_obj)
1216                 mlx5_rxq_obj_release(rxq_obj);
1217         DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
1218                 dev->data->port_id, rx_queue_id);
1219         rte_errno = ret; /* Restore rte_errno. */
1220         return -rte_errno;
1221 }
1222
1223 /**
1224  * Create a CQ Verbs object.
1225  *
1226  * @param dev
1227  *   Pointer to Ethernet device.
1228  * @param priv
1229  *   Pointer to device private data.
1230  * @param rxq_data
1231  *   Pointer to Rx queue data.
1232  * @param cqe_n
1233  *   Number of CQEs in CQ.
1234  * @param rxq_obj
1235  *   Pointer to Rx queue object data.
1236  *
1237  * @return
1238  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1239  */
1240 static struct ibv_cq *
1241 mlx5_ibv_cq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
1242                 struct mlx5_rxq_data *rxq_data,
1243                 unsigned int cqe_n, struct mlx5_rxq_obj *rxq_obj)
1244 {
1245         struct {
1246                 struct ibv_cq_init_attr_ex ibv;
1247                 struct mlx5dv_cq_init_attr mlx5;
1248         } cq_attr;
1249
1250         cq_attr.ibv = (struct ibv_cq_init_attr_ex){
1251                 .cqe = cqe_n,
1252                 .channel = rxq_obj->ibv_channel,
1253                 .comp_mask = 0,
1254         };
1255         cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
1256                 .comp_mask = 0,
1257         };
1258         if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
1259             !rxq_data->lro) {
1260                 cq_attr.mlx5.comp_mask |=
1261                                 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
1262 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1263                 cq_attr.mlx5.cqe_comp_res_format =
1264                                 mlx5_rxq_mprq_enabled(rxq_data) ?
1265                                 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
1266                                 MLX5DV_CQE_RES_FORMAT_HASH;
1267 #else
1268                 cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
1269 #endif
1270                 /*
1271                  * For vectorized Rx, it must not be doubled in order to
1272                  * make cq_ci and rq_ci aligned.
1273                  */
1274                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
1275                         cq_attr.ibv.cqe *= 2;
1276         } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
1277                 DRV_LOG(DEBUG,
1278                         "port %u Rx CQE compression is disabled for HW"
1279                         " timestamp",
1280                         dev->data->port_id);
1281         } else if (priv->config.cqe_comp && rxq_data->lro) {
1282                 DRV_LOG(DEBUG,
1283                         "port %u Rx CQE compression is disabled for LRO",
1284                         dev->data->port_id);
1285         }
1286 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
1287         if (priv->config.cqe_pad) {
1288                 cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
1289                 cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
1290         }
1291 #endif
1292         return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(priv->sh->ctx,
1293                                                               &cq_attr.ibv,
1294                                                               &cq_attr.mlx5));
1295 }
1296
1297 /**
1298  * Create a WQ Verbs object.
1299  *
1300  * @param dev
1301  *   Pointer to Ethernet device.
1302  * @param priv
1303  *   Pointer to device private data.
1304  * @param rxq_data
1305  *   Pointer to Rx queue data.
1306  * @param idx
1307  *   Queue index in DPDK Rx queue array
1308  * @param wqe_n
1309  *   Number of WQEs in WQ.
1310  * @param rxq_obj
1311  *   Pointer to Rx queue object data.
1312  *
1313  * @return
1314  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1315  */
1316 static struct ibv_wq *
1317 mlx5_ibv_wq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
1318                 struct mlx5_rxq_data *rxq_data, uint16_t idx,
1319                 unsigned int wqe_n, struct mlx5_rxq_obj *rxq_obj)
1320 {
1321         struct {
1322                 struct ibv_wq_init_attr ibv;
1323 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1324                 struct mlx5dv_wq_init_attr mlx5;
1325 #endif
1326         } wq_attr;
1327
1328         wq_attr.ibv = (struct ibv_wq_init_attr){
1329                 .wq_context = NULL, /* Could be useful in the future. */
1330                 .wq_type = IBV_WQT_RQ,
1331                 /* Max number of outstanding WRs. */
1332                 .max_wr = wqe_n >> rxq_data->sges_n,
1333                 /* Max number of scatter/gather elements in a WR. */
1334                 .max_sge = 1 << rxq_data->sges_n,
1335                 .pd = priv->sh->pd,
1336                 .cq = rxq_obj->ibv_cq,
1337                 .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
1338                 .create_flags = (rxq_data->vlan_strip ?
1339                                  IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
1340         };
1341         /* By default, FCS (CRC) is stripped by hardware. */
1342         if (rxq_data->crc_present) {
1343                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
1344                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1345         }
1346         if (priv->config.hw_padding) {
1347 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
1348                 wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
1349                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1350 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
1351                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
1352                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1353 #endif
1354         }
1355 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1356         wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
1357                 .comp_mask = 0,
1358         };
1359         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1360                 struct mlx5dv_striding_rq_init_attr *mprq_attr =
1361                                                 &wq_attr.mlx5.striding_rq_attrs;
1362
1363                 wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
1364                 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
1365                         .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
1366                         .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
1367                         .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
1368                 };
1369         }
1370         rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->ctx, &wq_attr.ibv,
1371                                               &wq_attr.mlx5);
1372 #else
1373         rxq_obj->wq = mlx5_glue->create_wq(priv->sh->ctx, &wq_attr.ibv);
1374 #endif
1375         if (rxq_obj->wq) {
1376                 /*
1377                  * Make sure number of WRs*SGEs match expectations since a queue
1378                  * cannot allocate more than "desc" buffers.
1379                  */
1380                 if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
1381                     wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
1382                         DRV_LOG(ERR,
1383                                 "port %u Rx queue %u requested %u*%u but got"
1384                                 " %u*%u WRs*SGEs",
1385                                 dev->data->port_id, idx,
1386                                 wqe_n >> rxq_data->sges_n,
1387                                 (1 << rxq_data->sges_n),
1388                                 wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
1389                         claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
1390                         rxq_obj->wq = NULL;
1391                         rte_errno = EINVAL;
1392                 }
1393         }
1394         return rxq_obj->wq;
1395 }
1396
1397 /**
1398  * Fill common fields of create RQ attributes structure.
1399  *
1400  * @param rxq_data
1401  *   Pointer to Rx queue data.
1402  * @param cqn
1403  *   CQ number to use with this RQ.
1404  * @param rq_attr
1405  *   RQ attributes structure to fill..
1406  */
1407 static void
1408 mlx5_devx_create_rq_attr_fill(struct mlx5_rxq_data *rxq_data, uint32_t cqn,
1409                               struct mlx5_devx_create_rq_attr *rq_attr)
1410 {
1411         rq_attr->state = MLX5_RQC_STATE_RST;
1412         rq_attr->vsd = (rxq_data->vlan_strip) ? 0 : 1;
1413         rq_attr->cqn = cqn;
1414         rq_attr->scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
1415 }
1416
1417 /**
1418  * Fill common fields of DevX WQ attributes structure.
1419  *
1420  * @param priv
1421  *   Pointer to device private data.
1422  * @param rxq_ctrl
1423  *   Pointer to Rx queue control structure.
1424  * @param wq_attr
1425  *   WQ attributes structure to fill..
1426  */
1427 static void
1428 mlx5_devx_wq_attr_fill(struct mlx5_priv *priv, struct mlx5_rxq_ctrl *rxq_ctrl,
1429                        struct mlx5_devx_wq_attr *wq_attr)
1430 {
1431         wq_attr->end_padding_mode = priv->config.cqe_pad ?
1432                                         MLX5_WQ_END_PAD_MODE_ALIGN :
1433                                         MLX5_WQ_END_PAD_MODE_NONE;
1434         wq_attr->pd = priv->sh->pdn;
1435         wq_attr->dbr_addr = rxq_ctrl->rq_dbr_offset;
1436         wq_attr->dbr_umem_id = rxq_ctrl->rq_dbr_umem_id;
1437         wq_attr->dbr_umem_valid = 1;
1438         wq_attr->wq_umem_id = rxq_ctrl->wq_umem->umem_id;
1439         wq_attr->wq_umem_valid = 1;
1440 }
1441
1442 /**
1443  * Create a RQ object using DevX.
1444  *
1445  * @param dev
1446  *   Pointer to Ethernet device.
1447  * @param idx
1448  *   Queue index in DPDK Rx queue array
1449  * @param cqn
1450  *   CQ number to use with this RQ.
1451  *
1452  * @return
1453  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1454  */
1455 static struct mlx5_devx_obj *
1456 mlx5_devx_rq_new(struct rte_eth_dev *dev, uint16_t idx, uint32_t cqn)
1457 {
1458         struct mlx5_priv *priv = dev->data->dev_private;
1459         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1460         struct mlx5_rxq_ctrl *rxq_ctrl =
1461                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1462         struct mlx5_devx_create_rq_attr rq_attr = { 0 };
1463         uint32_t wqe_n = 1 << (rxq_data->elts_n - rxq_data->sges_n);
1464         uint32_t wq_size = 0;
1465         uint32_t wqe_size = 0;
1466         uint32_t log_wqe_size = 0;
1467         void *buf = NULL;
1468         struct mlx5_devx_obj *rq;
1469
1470         /* Fill RQ attributes. */
1471         rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
1472         rq_attr.flush_in_error_en = 1;
1473         mlx5_devx_create_rq_attr_fill(rxq_data, cqn, &rq_attr);
1474         /* Fill WQ attributes for this RQ. */
1475         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1476                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
1477                 /*
1478                  * Number of strides in each WQE:
1479                  * 512*2^single_wqe_log_num_of_strides.
1480                  */
1481                 rq_attr.wq_attr.single_wqe_log_num_of_strides =
1482                                 rxq_data->strd_num_n -
1483                                 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1484                 /* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
1485                 rq_attr.wq_attr.single_stride_log_num_of_bytes =
1486                                 rxq_data->strd_sz_n -
1487                                 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1488                 wqe_size = sizeof(struct mlx5_wqe_mprq);
1489         } else {
1490                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1491                 wqe_size = sizeof(struct mlx5_wqe_data_seg);
1492         }
1493         log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
1494         rq_attr.wq_attr.log_wq_stride = log_wqe_size;
1495         rq_attr.wq_attr.log_wq_sz = rxq_data->elts_n - rxq_data->sges_n;
1496         /* Calculate and allocate WQ memory space. */
1497         wqe_size = 1 << log_wqe_size; /* round up power of two.*/
1498         wq_size = wqe_n * wqe_size;
1499         size_t alignment = MLX5_WQE_BUF_ALIGNMENT;
1500         if (alignment == (size_t)-1) {
1501                 DRV_LOG(ERR, "Failed to get mem page size");
1502                 rte_errno = ENOMEM;
1503                 return NULL;
1504         }
1505         buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, wq_size,
1506                           alignment, rxq_ctrl->socket);
1507         if (!buf)
1508                 return NULL;
1509         rxq_data->wqes = buf;
1510         rxq_ctrl->wq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
1511                                                      buf, wq_size, 0);
1512         if (!rxq_ctrl->wq_umem) {
1513                 mlx5_free(buf);
1514                 return NULL;
1515         }
1516         mlx5_devx_wq_attr_fill(priv, rxq_ctrl, &rq_attr.wq_attr);
1517         rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &rq_attr, rxq_ctrl->socket);
1518         if (!rq)
1519                 rxq_release_devx_rq_resources(rxq_ctrl);
1520         return rq;
1521 }
1522
1523 /**
1524  * Create a DevX CQ object for an Rx queue.
1525  *
1526  * @param dev
1527  *   Pointer to Ethernet device.
1528  * @param cqe_n
1529  *   Number of CQEs in CQ.
1530  * @param idx
1531  *   Queue index in DPDK Rx queue array
1532  * @param rxq_obj
1533  *   Pointer to Rx queue object data.
1534  *
1535  * @return
1536  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1537  */
1538 static struct mlx5_devx_obj *
1539 mlx5_devx_cq_new(struct rte_eth_dev *dev, unsigned int cqe_n, uint16_t idx,
1540                  struct mlx5_rxq_obj *rxq_obj)
1541 {
1542         struct mlx5_devx_obj *cq_obj = 0;
1543         struct mlx5_devx_cq_attr cq_attr = { 0 };
1544         struct mlx5_priv *priv = dev->data->dev_private;
1545         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1546         struct mlx5_rxq_ctrl *rxq_ctrl =
1547                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1548         size_t page_size = rte_mem_page_size();
1549         uint32_t lcore = (uint32_t)rte_lcore_to_cpu_id(-1);
1550         uint32_t eqn = 0;
1551         void *buf = NULL;
1552         uint16_t event_nums[1] = {0};
1553         uint32_t log_cqe_n;
1554         uint32_t cq_size;
1555         int ret = 0;
1556
1557         if (page_size == (size_t)-1) {
1558                 DRV_LOG(ERR, "Failed to get page_size.");
1559                 goto error;
1560         }
1561         if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
1562             !rxq_data->lro) {
1563                 cq_attr.cqe_comp_en = MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
1564 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1565                 cq_attr.mini_cqe_res_format =
1566                                 mlx5_rxq_mprq_enabled(rxq_data) ?
1567                                 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
1568                                 MLX5DV_CQE_RES_FORMAT_HASH;
1569 #else
1570                 cq_attr.mini_cqe_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
1571 #endif
1572                 /*
1573                  * For vectorized Rx, it must not be doubled in order to
1574                  * make cq_ci and rq_ci aligned.
1575                  */
1576                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
1577                         cqe_n *= 2;
1578         } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
1579                 DRV_LOG(DEBUG,
1580                         "port %u Rx CQE compression is disabled for HW"
1581                         " timestamp",
1582                         dev->data->port_id);
1583         } else if (priv->config.cqe_comp && rxq_data->lro) {
1584                 DRV_LOG(DEBUG,
1585                         "port %u Rx CQE compression is disabled for LRO",
1586                         dev->data->port_id);
1587         }
1588 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
1589         if (priv->config.cqe_pad)
1590                 cq_attr.cqe_size = MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
1591 #endif
1592         log_cqe_n = log2above(cqe_n);
1593         cq_size = sizeof(struct mlx5_cqe) * (1 << log_cqe_n);
1594         /* Query the EQN for this core. */
1595         if (mlx5_glue->devx_query_eqn(priv->sh->ctx, lcore, &eqn)) {
1596                 DRV_LOG(ERR, "Failed to query EQN for CQ.");
1597                 goto error;
1598         }
1599         cq_attr.eqn = eqn;
1600         buf = rte_calloc_socket(__func__, 1, cq_size, page_size,
1601                                 rxq_ctrl->socket);
1602         if (!buf) {
1603                 DRV_LOG(ERR, "Failed to allocate memory for CQ.");
1604                 goto error;
1605         }
1606         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)buf;
1607         rxq_ctrl->cq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx, buf,
1608                                                      cq_size,
1609                                                      IBV_ACCESS_LOCAL_WRITE);
1610         if (!rxq_ctrl->cq_umem) {
1611                 DRV_LOG(ERR, "Failed to register umem for CQ.");
1612                 goto error;
1613         }
1614         cq_attr.uar_page_id = priv->sh->devx_rx_uar->page_id;
1615         cq_attr.q_umem_id = rxq_ctrl->cq_umem->umem_id;
1616         cq_attr.q_umem_valid = 1;
1617         cq_attr.log_cq_size = log_cqe_n;
1618         cq_attr.log_page_size = rte_log2_u32(page_size);
1619         cq_attr.db_umem_offset = rxq_ctrl->cq_dbr_offset;
1620         cq_attr.db_umem_id = rxq_ctrl->cq_dbr_umem_id;
1621         cq_attr.db_umem_valid = rxq_ctrl->cq_dbr_umem_id_valid;
1622         cq_obj = mlx5_devx_cmd_create_cq(priv->sh->ctx, &cq_attr);
1623         if (!cq_obj)
1624                 goto error;
1625         rxq_data->cqe_n = log_cqe_n;
1626         rxq_data->cqn = cq_obj->id;
1627         if (rxq_obj->devx_channel) {
1628                 ret = mlx5_glue->devx_subscribe_devx_event
1629                                                 (rxq_obj->devx_channel,
1630                                                  cq_obj->obj,
1631                                                  sizeof(event_nums),
1632                                                  event_nums,
1633                                                  (uint64_t)(uintptr_t)cq_obj);
1634                 if (ret) {
1635                         DRV_LOG(ERR, "Fail to subscribe CQ to event channel.");
1636                         rte_errno = errno;
1637                         goto error;
1638                 }
1639         }
1640         /* Initialise CQ to 1's to mark HW ownership for all CQEs. */
1641         memset((void *)(uintptr_t)rxq_data->cqes, 0xFF, cq_size);
1642         return cq_obj;
1643 error:
1644         rxq_release_devx_cq_resources(rxq_ctrl);
1645         return NULL;
1646 }
1647
1648 /**
1649  * Create the Rx hairpin queue object.
1650  *
1651  * @param dev
1652  *   Pointer to Ethernet device.
1653  * @param idx
1654  *   Queue index in DPDK Rx queue array
1655  *
1656  * @return
1657  *   The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
1658  */
1659 static struct mlx5_rxq_obj *
1660 mlx5_rxq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
1661 {
1662         struct mlx5_priv *priv = dev->data->dev_private;
1663         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1664         struct mlx5_rxq_ctrl *rxq_ctrl =
1665                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1666         struct mlx5_devx_create_rq_attr attr = { 0 };
1667         struct mlx5_rxq_obj *tmpl = NULL;
1668         uint32_t max_wq_data;
1669
1670         MLX5_ASSERT(rxq_data);
1671         MLX5_ASSERT(!rxq_ctrl->obj);
1672         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1673                            rxq_ctrl->socket);
1674         if (!tmpl) {
1675                 DRV_LOG(ERR,
1676                         "port %u Rx queue %u cannot allocate verbs resources",
1677                         dev->data->port_id, rxq_data->idx);
1678                 rte_errno = ENOMEM;
1679                 return NULL;
1680         }
1681         tmpl->type = MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN;
1682         tmpl->rxq_ctrl = rxq_ctrl;
1683         attr.hairpin = 1;
1684         max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
1685         /* Jumbo frames > 9KB should be supported, and more packets. */
1686         if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
1687                 if (priv->config.log_hp_size > max_wq_data) {
1688                         DRV_LOG(ERR, "total data size %u power of 2 is "
1689                                 "too large for hairpin",
1690                                 priv->config.log_hp_size);
1691                         mlx5_free(tmpl);
1692                         rte_errno = ERANGE;
1693                         return NULL;
1694                 }
1695                 attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
1696         } else {
1697                 attr.wq_attr.log_hairpin_data_sz =
1698                                 (max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
1699                                  max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
1700         }
1701         /* Set the packets number to the maximum value for performance. */
1702         attr.wq_attr.log_hairpin_num_packets =
1703                         attr.wq_attr.log_hairpin_data_sz -
1704                         MLX5_HAIRPIN_QUEUE_STRIDE;
1705         tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &attr,
1706                                            rxq_ctrl->socket);
1707         if (!tmpl->rq) {
1708                 DRV_LOG(ERR,
1709                         "port %u Rx hairpin queue %u can't create rq object",
1710                         dev->data->port_id, idx);
1711                 mlx5_free(tmpl);
1712                 rte_errno = errno;
1713                 return NULL;
1714         }
1715         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1716                 idx, (void *)&tmpl);
1717         rte_atomic32_inc(&tmpl->refcnt);
1718         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1719         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1720         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
1721         return tmpl;
1722 }
1723
1724 /**
1725  * Create the Rx queue Verbs/DevX object.
1726  *
1727  * @param dev
1728  *   Pointer to Ethernet device.
1729  * @param idx
1730  *   Queue index in DPDK Rx queue array
1731  * @param type
1732  *   Type of Rx queue object to create.
1733  *
1734  * @return
1735  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
1736  */
1737 struct mlx5_rxq_obj *
1738 mlx5_rxq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
1739                  enum mlx5_rxq_obj_type type)
1740 {
1741         struct mlx5_priv *priv = dev->data->dev_private;
1742         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1743         struct mlx5_rxq_ctrl *rxq_ctrl =
1744                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1745         struct ibv_wq_attr mod;
1746         unsigned int cqe_n;
1747         unsigned int wqe_n = 1 << rxq_data->elts_n;
1748         struct mlx5_rxq_obj *tmpl = NULL;
1749         struct mlx5dv_cq cq_info;
1750         struct mlx5dv_rwq rwq;
1751         int ret = 0;
1752         struct mlx5dv_obj obj;
1753
1754         MLX5_ASSERT(rxq_data);
1755         MLX5_ASSERT(!rxq_ctrl->obj);
1756         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
1757                 return mlx5_rxq_obj_hairpin_new(dev, idx);
1758         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
1759         priv->verbs_alloc_ctx.obj = rxq_ctrl;
1760         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1761                            rxq_ctrl->socket);
1762         if (!tmpl) {
1763                 DRV_LOG(ERR,
1764                         "port %u Rx queue %u cannot allocate resources",
1765                         dev->data->port_id, rxq_data->idx);
1766                 rte_errno = ENOMEM;
1767                 goto error;
1768         }
1769         tmpl->type = type;
1770         tmpl->rxq_ctrl = rxq_ctrl;
1771         if (rxq_ctrl->irq) {
1772                 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1773                         tmpl->ibv_channel =
1774                                 mlx5_glue->create_comp_channel(priv->sh->ctx);
1775                         if (!tmpl->ibv_channel) {
1776                                 DRV_LOG(ERR, "port %u: comp channel creation "
1777                                         "failure", dev->data->port_id);
1778                                 rte_errno = ENOMEM;
1779                                 goto error;
1780                         }
1781                         tmpl->fd = tmpl->ibv_channel->fd;
1782                 } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1783                         int devx_ev_flag =
1784                           MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA;
1785
1786                         tmpl->devx_channel =
1787                                 mlx5_glue->devx_create_event_channel
1788                                                                 (priv->sh->ctx,
1789                                                                  devx_ev_flag);
1790                         if (!tmpl->devx_channel) {
1791                                 rte_errno = errno;
1792                                 DRV_LOG(ERR,
1793                                         "Failed to create event channel %d.",
1794                                         rte_errno);
1795                                 goto error;
1796                         }
1797                         tmpl->fd = tmpl->devx_channel->fd;
1798                 }
1799         }
1800         if (mlx5_rxq_mprq_enabled(rxq_data))
1801                 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
1802         else
1803                 cqe_n = wqe_n - 1;
1804         DRV_LOG(DEBUG, "port %u device_attr.max_qp_wr is %d",
1805                 dev->data->port_id, priv->sh->device_attr.max_qp_wr);
1806         DRV_LOG(DEBUG, "port %u device_attr.max_sge is %d",
1807                 dev->data->port_id, priv->sh->device_attr.max_sge);
1808         if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1809                 /* Create CQ using Verbs API. */
1810                 tmpl->ibv_cq = mlx5_ibv_cq_new(dev, priv, rxq_data, cqe_n,
1811                                                tmpl);
1812                 if (!tmpl->ibv_cq) {
1813                         DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
1814                                 dev->data->port_id, idx);
1815                         rte_errno = ENOMEM;
1816                         goto error;
1817                 }
1818                 obj.cq.in = tmpl->ibv_cq;
1819                 obj.cq.out = &cq_info;
1820                 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
1821                 if (ret) {
1822                         rte_errno = ret;
1823                         goto error;
1824                 }
1825                 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1826                         DRV_LOG(ERR,
1827                                 "port %u wrong MLX5_CQE_SIZE environment "
1828                                 "variable value: it should be set to %u",
1829                                 dev->data->port_id, RTE_CACHE_LINE_SIZE);
1830                         rte_errno = EINVAL;
1831                         goto error;
1832                 }
1833                 /* Fill the rings. */
1834                 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
1835                 rxq_data->cq_db = cq_info.dbrec;
1836                 rxq_data->cqes =
1837                         (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
1838                 rxq_data->cq_uar = cq_info.cq_uar;
1839                 rxq_data->cqn = cq_info.cqn;
1840                 /* Create WQ (RQ) using Verbs API. */
1841                 tmpl->wq = mlx5_ibv_wq_new(dev, priv, rxq_data, idx, wqe_n,
1842                                            tmpl);
1843                 if (!tmpl->wq) {
1844                         DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
1845                                 dev->data->port_id, idx);
1846                         rte_errno = ENOMEM;
1847                         goto error;
1848                 }
1849                 /* Change queue state to ready. */
1850                 mod = (struct ibv_wq_attr){
1851                         .attr_mask = IBV_WQ_ATTR_STATE,
1852                         .wq_state = IBV_WQS_RDY,
1853                 };
1854                 ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
1855                 if (ret) {
1856                         DRV_LOG(ERR,
1857                                 "port %u Rx queue %u WQ state to IBV_WQS_RDY"
1858                                 " failed", dev->data->port_id, idx);
1859                         rte_errno = ret;
1860                         goto error;
1861                 }
1862                 obj.rwq.in = tmpl->wq;
1863                 obj.rwq.out = &rwq;
1864                 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
1865                 if (ret) {
1866                         rte_errno = ret;
1867                         goto error;
1868                 }
1869                 rxq_data->wqes = rwq.buf;
1870                 rxq_data->rq_db = rwq.dbrec;
1871         } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1872                 struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
1873                 struct mlx5_devx_dbr_page *dbr_page;
1874                 int64_t dbr_offset;
1875
1876                 /* Allocate CQ door-bell. */
1877                 dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs,
1878                                           &dbr_page);
1879                 if (dbr_offset < 0) {
1880                         DRV_LOG(ERR, "Failed to allocate CQ door-bell.");
1881                         goto error;
1882                 }
1883                 rxq_ctrl->cq_dbr_offset = dbr_offset;
1884                 rxq_ctrl->cq_dbr_umem_id = mlx5_os_get_umem_id(dbr_page->umem);
1885                 rxq_ctrl->cq_dbr_umem_id_valid = 1;
1886                 rxq_data->cq_db =
1887                         (uint32_t *)((uintptr_t)dbr_page->dbrs +
1888                                      (uintptr_t)rxq_ctrl->cq_dbr_offset);
1889                 rxq_data->cq_uar = priv->sh->devx_rx_uar->base_addr;
1890                 /* Create CQ using DevX API. */
1891                 tmpl->devx_cq = mlx5_devx_cq_new(dev, cqe_n, idx, tmpl);
1892                 if (!tmpl->devx_cq) {
1893                         DRV_LOG(ERR, "Failed to create CQ.");
1894                         goto error;
1895                 }
1896                 /* Allocate RQ door-bell. */
1897                 dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs,
1898                                           &dbr_page);
1899                 if (dbr_offset < 0) {
1900                         DRV_LOG(ERR, "Failed to allocate RQ door-bell.");
1901                         goto error;
1902                 }
1903                 rxq_ctrl->rq_dbr_offset = dbr_offset;
1904                 rxq_ctrl->rq_dbr_umem_id = mlx5_os_get_umem_id(dbr_page->umem);
1905                 rxq_ctrl->rq_dbr_umem_id_valid = 1;
1906                 rxq_data->rq_db =
1907                         (uint32_t *)((uintptr_t)dbr_page->dbrs +
1908                                      (uintptr_t)rxq_ctrl->rq_dbr_offset);
1909                 /* Create RQ using DevX API. */
1910                 tmpl->rq = mlx5_devx_rq_new(dev, idx, tmpl->devx_cq->id);
1911                 if (!tmpl->rq) {
1912                         DRV_LOG(ERR, "port %u Rx queue %u RQ creation failure",
1913                                 dev->data->port_id, idx);
1914                         rte_errno = ENOMEM;
1915                         goto error;
1916                 }
1917                 /* Change queue state to ready. */
1918                 rq_attr.rq_state = MLX5_RQC_STATE_RST;
1919                 rq_attr.state = MLX5_RQC_STATE_RDY;
1920                 ret = mlx5_devx_cmd_modify_rq(tmpl->rq, &rq_attr);
1921                 if (ret)
1922                         goto error;
1923         }
1924         rxq_data->cq_arm_sn = 0;
1925         mlx5_rxq_initialize(rxq_data);
1926         rxq_data->cq_ci = 0;
1927         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1928                 idx, (void *)&tmpl);
1929         rte_atomic32_inc(&tmpl->refcnt);
1930         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1931         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1932         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
1933         return tmpl;
1934 error:
1935         if (tmpl) {
1936                 ret = rte_errno; /* Save rte_errno before cleanup. */
1937                 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1938                         if (tmpl->wq)
1939                                 claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
1940                         if (tmpl->ibv_cq)
1941                                 claim_zero(mlx5_glue->destroy_cq(tmpl->ibv_cq));
1942                         if (tmpl->ibv_channel)
1943                                 claim_zero(mlx5_glue->destroy_comp_channel
1944                                                         (tmpl->ibv_channel));
1945                 } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1946                         if (tmpl->rq)
1947                                 claim_zero(mlx5_devx_cmd_destroy(tmpl->rq));
1948                         if (tmpl->devx_cq)
1949                                 claim_zero(mlx5_devx_cmd_destroy
1950                                                         (tmpl->devx_cq));
1951                         if (tmpl->devx_channel)
1952                                 mlx5_glue->devx_destroy_event_channel
1953                                                         (tmpl->devx_channel);
1954                 }
1955                 mlx5_free(tmpl);
1956                 rte_errno = ret; /* Restore rte_errno. */
1957         }
1958         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1959                 rxq_release_devx_rq_resources(rxq_ctrl);
1960                 rxq_release_devx_cq_resources(rxq_ctrl);
1961         }
1962         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1963         return NULL;
1964 }
1965
1966 /**
1967  * Verify the Rx queue objects list is empty
1968  *
1969  * @param dev
1970  *   Pointer to Ethernet device.
1971  *
1972  * @return
1973  *   The number of objects not released.
1974  */
1975 int
1976 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1977 {
1978         struct mlx5_priv *priv = dev->data->dev_private;
1979         int ret = 0;
1980         struct mlx5_rxq_obj *rxq_obj;
1981
1982         LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1983                 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1984                         dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1985                 ++ret;
1986         }
1987         return ret;
1988 }
1989
1990 /**
1991  * Callback function to initialize mbufs for Multi-Packet RQ.
1992  */
1993 static inline void
1994 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1995                     void *_m, unsigned int i __rte_unused)
1996 {
1997         struct mlx5_mprq_buf *buf = _m;
1998         struct rte_mbuf_ext_shared_info *shinfo;
1999         unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
2000         unsigned int j;
2001
2002         memset(_m, 0, sizeof(*buf));
2003         buf->mp = mp;
2004         rte_atomic16_set(&buf->refcnt, 1);
2005         for (j = 0; j != strd_n; ++j) {
2006                 shinfo = &buf->shinfos[j];
2007                 shinfo->free_cb = mlx5_mprq_buf_free_cb;
2008                 shinfo->fcb_opaque = buf;
2009         }
2010 }
2011
2012 /**
2013  * Free mempool of Multi-Packet RQ.
2014  *
2015  * @param dev
2016  *   Pointer to Ethernet device.
2017  *
2018  * @return
2019  *   0 on success, negative errno value on failure.
2020  */
2021 int
2022 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
2023 {
2024         struct mlx5_priv *priv = dev->data->dev_private;
2025         struct rte_mempool *mp = priv->mprq_mp;
2026         unsigned int i;
2027
2028         if (mp == NULL)
2029                 return 0;
2030         DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
2031                 dev->data->port_id, mp->name);
2032         /*
2033          * If a buffer in the pool has been externally attached to a mbuf and it
2034          * is still in use by application, destroying the Rx queue can spoil
2035          * the packet. It is unlikely to happen but if application dynamically
2036          * creates and destroys with holding Rx packets, this can happen.
2037          *
2038          * TODO: It is unavoidable for now because the mempool for Multi-Packet
2039          * RQ isn't provided by application but managed by PMD.
2040          */
2041         if (!rte_mempool_full(mp)) {
2042                 DRV_LOG(ERR,
2043                         "port %u mempool for Multi-Packet RQ is still in use",
2044                         dev->data->port_id);
2045                 rte_errno = EBUSY;
2046                 return -rte_errno;
2047         }
2048         rte_mempool_free(mp);
2049         /* Unset mempool for each Rx queue. */
2050         for (i = 0; i != priv->rxqs_n; ++i) {
2051                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2052
2053                 if (rxq == NULL)
2054                         continue;
2055                 rxq->mprq_mp = NULL;
2056         }
2057         priv->mprq_mp = NULL;
2058         return 0;
2059 }
2060
2061 /**
2062  * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
2063  * mempool. If already allocated, reuse it if there're enough elements.
2064  * Otherwise, resize it.
2065  *
2066  * @param dev
2067  *   Pointer to Ethernet device.
2068  *
2069  * @return
2070  *   0 on success, negative errno value on failure.
2071  */
2072 int
2073 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
2074 {
2075         struct mlx5_priv *priv = dev->data->dev_private;
2076         struct rte_mempool *mp = priv->mprq_mp;
2077         char name[RTE_MEMPOOL_NAMESIZE];
2078         unsigned int desc = 0;
2079         unsigned int buf_len;
2080         unsigned int obj_num;
2081         unsigned int obj_size;
2082         unsigned int strd_num_n = 0;
2083         unsigned int strd_sz_n = 0;
2084         unsigned int i;
2085         unsigned int n_ibv = 0;
2086
2087         if (!mlx5_mprq_enabled(dev))
2088                 return 0;
2089         /* Count the total number of descriptors configured. */
2090         for (i = 0; i != priv->rxqs_n; ++i) {
2091                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2092                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
2093                         (rxq, struct mlx5_rxq_ctrl, rxq);
2094
2095                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
2096                         continue;
2097                 n_ibv++;
2098                 desc += 1 << rxq->elts_n;
2099                 /* Get the max number of strides. */
2100                 if (strd_num_n < rxq->strd_num_n)
2101                         strd_num_n = rxq->strd_num_n;
2102                 /* Get the max size of a stride. */
2103                 if (strd_sz_n < rxq->strd_sz_n)
2104                         strd_sz_n = rxq->strd_sz_n;
2105         }
2106         MLX5_ASSERT(strd_num_n && strd_sz_n);
2107         buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
2108         obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
2109                 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
2110         /*
2111          * Received packets can be either memcpy'd or externally referenced. In
2112          * case that the packet is attached to an mbuf as an external buffer, as
2113          * it isn't possible to predict how the buffers will be queued by
2114          * application, there's no option to exactly pre-allocate needed buffers
2115          * in advance but to speculatively prepares enough buffers.
2116          *
2117          * In the data path, if this Mempool is depleted, PMD will try to memcpy
2118          * received packets to buffers provided by application (rxq->mp) until
2119          * this Mempool gets available again.
2120          */
2121         desc *= 4;
2122         obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
2123         /*
2124          * rte_mempool_create_empty() has sanity check to refuse large cache
2125          * size compared to the number of elements.
2126          * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
2127          * constant number 2 instead.
2128          */
2129         obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
2130         /* Check a mempool is already allocated and if it can be resued. */
2131         if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
2132                 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
2133                         dev->data->port_id, mp->name);
2134                 /* Reuse. */
2135                 goto exit;
2136         } else if (mp != NULL) {
2137                 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
2138                         dev->data->port_id, mp->name);
2139                 /*
2140                  * If failed to free, which means it may be still in use, no way
2141                  * but to keep using the existing one. On buffer underrun,
2142                  * packets will be memcpy'd instead of external buffer
2143                  * attachment.
2144                  */
2145                 if (mlx5_mprq_free_mp(dev)) {
2146                         if (mp->elt_size >= obj_size)
2147                                 goto exit;
2148                         else
2149                                 return -rte_errno;
2150                 }
2151         }
2152         snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
2153         mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
2154                                 0, NULL, NULL, mlx5_mprq_buf_init,
2155                                 (void *)(uintptr_t)(1 << strd_num_n),
2156                                 dev->device->numa_node, 0);
2157         if (mp == NULL) {
2158                 DRV_LOG(ERR,
2159                         "port %u failed to allocate a mempool for"
2160                         " Multi-Packet RQ, count=%u, size=%u",
2161                         dev->data->port_id, obj_num, obj_size);
2162                 rte_errno = ENOMEM;
2163                 return -rte_errno;
2164         }
2165         priv->mprq_mp = mp;
2166 exit:
2167         /* Set mempool for each Rx queue. */
2168         for (i = 0; i != priv->rxqs_n; ++i) {
2169                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2170                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
2171                         (rxq, struct mlx5_rxq_ctrl, rxq);
2172
2173                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
2174                         continue;
2175                 rxq->mprq_mp = mp;
2176         }
2177         DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
2178                 dev->data->port_id);
2179         return 0;
2180 }
2181
2182 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
2183                                         sizeof(struct rte_vlan_hdr) * 2 + \
2184                                         sizeof(struct rte_ipv6_hdr)))
2185 #define MAX_TCP_OPTION_SIZE 40u
2186 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
2187                                  sizeof(struct rte_tcp_hdr) + \
2188                                  MAX_TCP_OPTION_SIZE))
2189
2190 /**
2191  * Adjust the maximum LRO massage size.
2192  *
2193  * @param dev
2194  *   Pointer to Ethernet device.
2195  * @param idx
2196  *   RX queue index.
2197  * @param max_lro_size
2198  *   The maximum size for LRO packet.
2199  */
2200 static void
2201 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
2202                              uint32_t max_lro_size)
2203 {
2204         struct mlx5_priv *priv = dev->data->dev_private;
2205
2206         if (priv->config.hca_attr.lro_max_msg_sz_mode ==
2207             MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
2208             MLX5_MAX_TCP_HDR_OFFSET)
2209                 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
2210         max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
2211         MLX5_ASSERT(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
2212         max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
2213         if (priv->max_lro_msg_size)
2214                 priv->max_lro_msg_size =
2215                         RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
2216         else
2217                 priv->max_lro_msg_size = max_lro_size;
2218         DRV_LOG(DEBUG,
2219                 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
2220                 dev->data->port_id, idx,
2221                 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
2222 }
2223
2224 /**
2225  * Create a DPDK Rx queue.
2226  *
2227  * @param dev
2228  *   Pointer to Ethernet device.
2229  * @param idx
2230  *   RX queue index.
2231  * @param desc
2232  *   Number of descriptors to configure in queue.
2233  * @param socket
2234  *   NUMA socket on which memory must be allocated.
2235  *
2236  * @return
2237  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
2238  */
2239 struct mlx5_rxq_ctrl *
2240 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2241              unsigned int socket, const struct rte_eth_rxconf *conf,
2242              struct rte_mempool *mp)
2243 {
2244         struct mlx5_priv *priv = dev->data->dev_private;
2245         struct mlx5_rxq_ctrl *tmpl;
2246         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
2247         unsigned int mprq_stride_nums;
2248         unsigned int mprq_stride_size;
2249         unsigned int mprq_stride_cap;
2250         struct mlx5_dev_config *config = &priv->config;
2251         /*
2252          * Always allocate extra slots, even if eventually
2253          * the vector Rx will not be used.
2254          */
2255         uint16_t desc_n =
2256                 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
2257         uint64_t offloads = conf->offloads |
2258                            dev->data->dev_conf.rxmode.offloads;
2259         unsigned int lro_on_queue = !!(offloads & DEV_RX_OFFLOAD_TCP_LRO);
2260         const int mprq_en = mlx5_check_mprq_support(dev) > 0;
2261         unsigned int max_rx_pkt_len = lro_on_queue ?
2262                         dev->data->dev_conf.rxmode.max_lro_pkt_size :
2263                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
2264         unsigned int non_scatter_min_mbuf_size = max_rx_pkt_len +
2265                                                         RTE_PKTMBUF_HEADROOM;
2266         unsigned int max_lro_size = 0;
2267         unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
2268
2269         if (non_scatter_min_mbuf_size > mb_len && !(offloads &
2270                                                     DEV_RX_OFFLOAD_SCATTER)) {
2271                 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
2272                         " configured and no enough mbuf space(%u) to contain "
2273                         "the maximum RX packet length(%u) with head-room(%u)",
2274                         dev->data->port_id, idx, mb_len, max_rx_pkt_len,
2275                         RTE_PKTMBUF_HEADROOM);
2276                 rte_errno = ENOSPC;
2277                 return NULL;
2278         }
2279         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl) +
2280                            desc_n * sizeof(struct rte_mbuf *), 0, socket);
2281         if (!tmpl) {
2282                 rte_errno = ENOMEM;
2283                 return NULL;
2284         }
2285         tmpl->type = MLX5_RXQ_TYPE_STANDARD;
2286         if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
2287                                MLX5_MR_BTREE_CACHE_N, socket)) {
2288                 /* rte_errno is already set. */
2289                 goto error;
2290         }
2291         tmpl->socket = socket;
2292         if (dev->data->dev_conf.intr_conf.rxq)
2293                 tmpl->irq = 1;
2294         mprq_stride_nums = config->mprq.stride_num_n ?
2295                 config->mprq.stride_num_n : MLX5_MPRQ_STRIDE_NUM_N;
2296         mprq_stride_size = non_scatter_min_mbuf_size <=
2297                 (1U << config->mprq.max_stride_size_n) ?
2298                 log2above(non_scatter_min_mbuf_size) : MLX5_MPRQ_STRIDE_SIZE_N;
2299         mprq_stride_cap = (config->mprq.stride_num_n ?
2300                 (1U << config->mprq.stride_num_n) : (1U << mprq_stride_nums)) *
2301                         (config->mprq.stride_size_n ?
2302                 (1U << config->mprq.stride_size_n) : (1U << mprq_stride_size));
2303         /*
2304          * This Rx queue can be configured as a Multi-Packet RQ if all of the
2305          * following conditions are met:
2306          *  - MPRQ is enabled.
2307          *  - The number of descs is more than the number of strides.
2308          *  - max_rx_pkt_len plus overhead is less than the max size
2309          *    of a stride or mprq_stride_size is specified by a user.
2310          *    Need to nake sure that there are enough stides to encap
2311          *    the maximum packet size in case mprq_stride_size is set.
2312          *  Otherwise, enable Rx scatter if necessary.
2313          */
2314         if (mprq_en && desc > (1U << mprq_stride_nums) &&
2315             (non_scatter_min_mbuf_size <=
2316              (1U << config->mprq.max_stride_size_n) ||
2317              (config->mprq.stride_size_n &&
2318               non_scatter_min_mbuf_size <= mprq_stride_cap))) {
2319                 /* TODO: Rx scatter isn't supported yet. */
2320                 tmpl->rxq.sges_n = 0;
2321                 /* Trim the number of descs needed. */
2322                 desc >>= mprq_stride_nums;
2323                 tmpl->rxq.strd_num_n = config->mprq.stride_num_n ?
2324                         config->mprq.stride_num_n : mprq_stride_nums;
2325                 tmpl->rxq.strd_sz_n = config->mprq.stride_size_n ?
2326                         config->mprq.stride_size_n : mprq_stride_size;
2327                 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
2328                 tmpl->rxq.strd_scatter_en =
2329                                 !!(offloads & DEV_RX_OFFLOAD_SCATTER);
2330                 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
2331                                 config->mprq.max_memcpy_len);
2332                 max_lro_size = RTE_MIN(max_rx_pkt_len,
2333                                        (1u << tmpl->rxq.strd_num_n) *
2334                                        (1u << tmpl->rxq.strd_sz_n));
2335                 DRV_LOG(DEBUG,
2336                         "port %u Rx queue %u: Multi-Packet RQ is enabled"
2337                         " strd_num_n = %u, strd_sz_n = %u",
2338                         dev->data->port_id, idx,
2339                         tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
2340         } else if (max_rx_pkt_len <= first_mb_free_size) {
2341                 tmpl->rxq.sges_n = 0;
2342                 max_lro_size = max_rx_pkt_len;
2343         } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
2344                 unsigned int size = non_scatter_min_mbuf_size;
2345                 unsigned int sges_n;
2346
2347                 if (lro_on_queue && first_mb_free_size <
2348                     MLX5_MAX_LRO_HEADER_FIX) {
2349                         DRV_LOG(ERR, "Not enough space in the first segment(%u)"
2350                                 " to include the max header size(%u) for LRO",
2351                                 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
2352                         rte_errno = ENOTSUP;
2353                         goto error;
2354                 }
2355                 /*
2356                  * Determine the number of SGEs needed for a full packet
2357                  * and round it to the next power of two.
2358                  */
2359                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
2360                 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
2361                         DRV_LOG(ERR,
2362                                 "port %u too many SGEs (%u) needed to handle"
2363                                 " requested maximum packet size %u, the maximum"
2364                                 " supported are %u", dev->data->port_id,
2365                                 1 << sges_n, max_rx_pkt_len,
2366                                 1u << MLX5_MAX_LOG_RQ_SEGS);
2367                         rte_errno = ENOTSUP;
2368                         goto error;
2369                 }
2370                 tmpl->rxq.sges_n = sges_n;
2371                 max_lro_size = max_rx_pkt_len;
2372         }
2373         if (config->mprq.enabled && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
2374                 DRV_LOG(WARNING,
2375                         "port %u MPRQ is requested but cannot be enabled\n"
2376                         " (requested: pkt_sz = %u, desc_num = %u,"
2377                         " rxq_num = %u, stride_sz = %u, stride_num = %u\n"
2378                         "  supported: min_rxqs_num = %u,"
2379                         " min_stride_sz = %u, max_stride_sz = %u).",
2380                         dev->data->port_id, non_scatter_min_mbuf_size,
2381                         desc, priv->rxqs_n,
2382                         config->mprq.stride_size_n ?
2383                                 (1U << config->mprq.stride_size_n) :
2384                                 (1U << mprq_stride_size),
2385                         config->mprq.stride_num_n ?
2386                                 (1U << config->mprq.stride_num_n) :
2387                                 (1U << mprq_stride_nums),
2388                         config->mprq.min_rxqs_num,
2389                         (1U << config->mprq.min_stride_size_n),
2390                         (1U << config->mprq.max_stride_size_n));
2391         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
2392                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
2393         if (desc % (1 << tmpl->rxq.sges_n)) {
2394                 DRV_LOG(ERR,
2395                         "port %u number of Rx queue descriptors (%u) is not a"
2396                         " multiple of SGEs per packet (%u)",
2397                         dev->data->port_id,
2398                         desc,
2399                         1 << tmpl->rxq.sges_n);
2400                 rte_errno = EINVAL;
2401                 goto error;
2402         }
2403         mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
2404         /* Toggle RX checksum offload if hardware supports it. */
2405         tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
2406         tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
2407         /* Configure VLAN stripping. */
2408         tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2409         /* By default, FCS (CRC) is stripped by hardware. */
2410         tmpl->rxq.crc_present = 0;
2411         tmpl->rxq.lro = lro_on_queue;
2412         if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
2413                 if (config->hw_fcs_strip) {
2414                         /*
2415                          * RQs used for LRO-enabled TIRs should not be
2416                          * configured to scatter the FCS.
2417                          */
2418                         if (lro_on_queue)
2419                                 DRV_LOG(WARNING,
2420                                         "port %u CRC stripping has been "
2421                                         "disabled but will still be performed "
2422                                         "by hardware, because LRO is enabled",
2423                                         dev->data->port_id);
2424                         else
2425                                 tmpl->rxq.crc_present = 1;
2426                 } else {
2427                         DRV_LOG(WARNING,
2428                                 "port %u CRC stripping has been disabled but will"
2429                                 " still be performed by hardware, make sure MLNX_OFED"
2430                                 " and firmware are up to date",
2431                                 dev->data->port_id);
2432                 }
2433         }
2434         DRV_LOG(DEBUG,
2435                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
2436                 " incoming frames to hide it",
2437                 dev->data->port_id,
2438                 tmpl->rxq.crc_present ? "disabled" : "enabled",
2439                 tmpl->rxq.crc_present << 2);
2440         /* Save port ID. */
2441         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
2442                 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
2443         tmpl->rxq.port_id = dev->data->port_id;
2444         tmpl->priv = priv;
2445         tmpl->rxq.mp = mp;
2446         tmpl->rxq.elts_n = log2above(desc);
2447         tmpl->rxq.rq_repl_thresh =
2448                 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
2449         tmpl->rxq.elts =
2450                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
2451 #ifndef RTE_ARCH_64
2452         tmpl->rxq.uar_lock_cq = &priv->sh->uar_lock_cq;
2453 #endif
2454         tmpl->rxq.idx = idx;
2455         rte_atomic32_inc(&tmpl->refcnt);
2456         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
2457         return tmpl;
2458 error:
2459         mlx5_free(tmpl);
2460         return NULL;
2461 }
2462
2463 /**
2464  * Create a DPDK Rx hairpin queue.
2465  *
2466  * @param dev
2467  *   Pointer to Ethernet device.
2468  * @param idx
2469  *   RX queue index.
2470  * @param desc
2471  *   Number of descriptors to configure in queue.
2472  * @param hairpin_conf
2473  *   The hairpin binding configuration.
2474  *
2475  * @return
2476  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
2477  */
2478 struct mlx5_rxq_ctrl *
2479 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2480                      const struct rte_eth_hairpin_conf *hairpin_conf)
2481 {
2482         struct mlx5_priv *priv = dev->data->dev_private;
2483         struct mlx5_rxq_ctrl *tmpl;
2484
2485         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
2486                            SOCKET_ID_ANY);
2487         if (!tmpl) {
2488                 rte_errno = ENOMEM;
2489                 return NULL;
2490         }
2491         tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
2492         tmpl->socket = SOCKET_ID_ANY;
2493         tmpl->rxq.rss_hash = 0;
2494         tmpl->rxq.port_id = dev->data->port_id;
2495         tmpl->priv = priv;
2496         tmpl->rxq.mp = NULL;
2497         tmpl->rxq.elts_n = log2above(desc);
2498         tmpl->rxq.elts = NULL;
2499         tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
2500         tmpl->hairpin_conf = *hairpin_conf;
2501         tmpl->rxq.idx = idx;
2502         rte_atomic32_inc(&tmpl->refcnt);
2503         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
2504         return tmpl;
2505 }
2506
2507 /**
2508  * Get a Rx queue.
2509  *
2510  * @param dev
2511  *   Pointer to Ethernet device.
2512  * @param idx
2513  *   RX queue index.
2514  *
2515  * @return
2516  *   A pointer to the queue if it exists, NULL otherwise.
2517  */
2518 struct mlx5_rxq_ctrl *
2519 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
2520 {
2521         struct mlx5_priv *priv = dev->data->dev_private;
2522         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2523
2524         if ((*priv->rxqs)[idx]) {
2525                 rxq_ctrl = container_of((*priv->rxqs)[idx],
2526                                         struct mlx5_rxq_ctrl,
2527                                         rxq);
2528                 mlx5_rxq_obj_get(dev, idx);
2529                 rte_atomic32_inc(&rxq_ctrl->refcnt);
2530         }
2531         return rxq_ctrl;
2532 }
2533
2534 /**
2535  * Release a Rx queue.
2536  *
2537  * @param dev
2538  *   Pointer to Ethernet device.
2539  * @param idx
2540  *   RX queue index.
2541  *
2542  * @return
2543  *   1 while a reference on it exists, 0 when freed.
2544  */
2545 int
2546 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
2547 {
2548         struct mlx5_priv *priv = dev->data->dev_private;
2549         struct mlx5_rxq_ctrl *rxq_ctrl;
2550
2551         if (!(*priv->rxqs)[idx])
2552                 return 0;
2553         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
2554         MLX5_ASSERT(rxq_ctrl->priv);
2555         if (rxq_ctrl->obj && !mlx5_rxq_obj_release(rxq_ctrl->obj))
2556                 rxq_ctrl->obj = NULL;
2557         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
2558                 if (rxq_ctrl->rq_dbr_umem_id_valid)
2559                         claim_zero(mlx5_release_dbr(&priv->dbrpgs,
2560                                                     rxq_ctrl->rq_dbr_umem_id,
2561                                                     rxq_ctrl->rq_dbr_offset));
2562                 if (rxq_ctrl->cq_dbr_umem_id_valid)
2563                         claim_zero(mlx5_release_dbr(&priv->dbrpgs,
2564                                                     rxq_ctrl->cq_dbr_umem_id,
2565                                                     rxq_ctrl->cq_dbr_offset));
2566                 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
2567                         mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
2568                 LIST_REMOVE(rxq_ctrl, next);
2569                 mlx5_free(rxq_ctrl);
2570                 (*priv->rxqs)[idx] = NULL;
2571                 return 0;
2572         }
2573         return 1;
2574 }
2575
2576 /**
2577  * Verify the Rx Queue list is empty
2578  *
2579  * @param dev
2580  *   Pointer to Ethernet device.
2581  *
2582  * @return
2583  *   The number of object not released.
2584  */
2585 int
2586 mlx5_rxq_verify(struct rte_eth_dev *dev)
2587 {
2588         struct mlx5_priv *priv = dev->data->dev_private;
2589         struct mlx5_rxq_ctrl *rxq_ctrl;
2590         int ret = 0;
2591
2592         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
2593                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
2594                         dev->data->port_id, rxq_ctrl->rxq.idx);
2595                 ++ret;
2596         }
2597         return ret;
2598 }
2599
2600 /**
2601  * Get a Rx queue type.
2602  *
2603  * @param dev
2604  *   Pointer to Ethernet device.
2605  * @param idx
2606  *   Rx queue index.
2607  *
2608  * @return
2609  *   The Rx queue type.
2610  */
2611 enum mlx5_rxq_type
2612 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
2613 {
2614         struct mlx5_priv *priv = dev->data->dev_private;
2615         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2616
2617         if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
2618                 rxq_ctrl = container_of((*priv->rxqs)[idx],
2619                                         struct mlx5_rxq_ctrl,
2620                                         rxq);
2621                 return rxq_ctrl->type;
2622         }
2623         return MLX5_RXQ_TYPE_UNDEFINED;
2624 }
2625
2626 /**
2627  * Create an indirection table.
2628  *
2629  * @param dev
2630  *   Pointer to Ethernet device.
2631  * @param queues
2632  *   Queues entering in the indirection table.
2633  * @param queues_n
2634  *   Number of queues in the array.
2635  *
2636  * @return
2637  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2638  */
2639 static struct mlx5_ind_table_obj *
2640 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
2641                        uint32_t queues_n, enum mlx5_ind_tbl_type type)
2642 {
2643         struct mlx5_priv *priv = dev->data->dev_private;
2644         struct mlx5_ind_table_obj *ind_tbl;
2645         unsigned int i = 0, j = 0, k = 0;
2646
2647         ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl) +
2648                               queues_n * sizeof(uint16_t), 0, SOCKET_ID_ANY);
2649         if (!ind_tbl) {
2650                 rte_errno = ENOMEM;
2651                 return NULL;
2652         }
2653         ind_tbl->type = type;
2654         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2655                 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
2656                         log2above(queues_n) :
2657                         log2above(priv->config.ind_table_max_size);
2658                 struct ibv_wq *wq[1 << wq_n];
2659
2660                 for (i = 0; i != queues_n; ++i) {
2661                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2662                                                                  queues[i]);
2663                         if (!rxq)
2664                                 goto error;
2665                         wq[i] = rxq->obj->wq;
2666                         ind_tbl->queues[i] = queues[i];
2667                 }
2668                 ind_tbl->queues_n = queues_n;
2669                 /* Finalise indirection table. */
2670                 k = i; /* Retain value of i for use in error case. */
2671                 for (j = 0; k != (unsigned int)(1 << wq_n); ++k, ++j)
2672                         wq[k] = wq[j];
2673                 ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
2674                         (priv->sh->ctx,
2675                          &(struct ibv_rwq_ind_table_init_attr){
2676                                 .log_ind_tbl_size = wq_n,
2677                                 .ind_tbl = wq,
2678                                 .comp_mask = 0,
2679                         });
2680                 if (!ind_tbl->ind_table) {
2681                         rte_errno = errno;
2682                         goto error;
2683                 }
2684         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2685                 struct mlx5_devx_rqt_attr *rqt_attr = NULL;
2686                 const unsigned int rqt_n =
2687                         1 << (rte_is_power_of_2(queues_n) ?
2688                               log2above(queues_n) :
2689                               log2above(priv->config.ind_table_max_size));
2690
2691                 rqt_attr = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rqt_attr) +
2692                                       rqt_n * sizeof(uint32_t), 0,
2693                                       SOCKET_ID_ANY);
2694                 if (!rqt_attr) {
2695                         DRV_LOG(ERR, "port %u cannot allocate RQT resources",
2696                                 dev->data->port_id);
2697                         rte_errno = ENOMEM;
2698                         goto error;
2699                 }
2700                 rqt_attr->rqt_max_size = priv->config.ind_table_max_size;
2701                 rqt_attr->rqt_actual_size = rqt_n;
2702                 for (i = 0; i != queues_n; ++i) {
2703                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2704                                                                  queues[i]);
2705                         if (!rxq)
2706                                 goto error;
2707                         rqt_attr->rq_list[i] = rxq->obj->rq->id;
2708                         ind_tbl->queues[i] = queues[i];
2709                 }
2710                 k = i; /* Retain value of i for use in error case. */
2711                 for (j = 0; k != rqt_n; ++k, ++j)
2712                         rqt_attr->rq_list[k] = rqt_attr->rq_list[j];
2713                 ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->ctx,
2714                                                         rqt_attr);
2715                 mlx5_free(rqt_attr);
2716                 if (!ind_tbl->rqt) {
2717                         DRV_LOG(ERR, "port %u cannot create DevX RQT",
2718                                 dev->data->port_id);
2719                         rte_errno = errno;
2720                         goto error;
2721                 }
2722                 ind_tbl->queues_n = queues_n;
2723         }
2724         rte_atomic32_inc(&ind_tbl->refcnt);
2725         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
2726         return ind_tbl;
2727 error:
2728         for (j = 0; j < i; j++)
2729                 mlx5_rxq_release(dev, ind_tbl->queues[j]);
2730         mlx5_free(ind_tbl);
2731         DEBUG("port %u cannot create indirection table", dev->data->port_id);
2732         return NULL;
2733 }
2734
2735 /**
2736  * Get an indirection table.
2737  *
2738  * @param dev
2739  *   Pointer to Ethernet device.
2740  * @param queues
2741  *   Queues entering in the indirection table.
2742  * @param queues_n
2743  *   Number of queues in the array.
2744  *
2745  * @return
2746  *   An indirection table if found.
2747  */
2748 static struct mlx5_ind_table_obj *
2749 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
2750                        uint32_t queues_n)
2751 {
2752         struct mlx5_priv *priv = dev->data->dev_private;
2753         struct mlx5_ind_table_obj *ind_tbl;
2754
2755         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2756                 if ((ind_tbl->queues_n == queues_n) &&
2757                     (memcmp(ind_tbl->queues, queues,
2758                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
2759                      == 0))
2760                         break;
2761         }
2762         if (ind_tbl) {
2763                 unsigned int i;
2764
2765                 rte_atomic32_inc(&ind_tbl->refcnt);
2766                 for (i = 0; i != ind_tbl->queues_n; ++i)
2767                         mlx5_rxq_get(dev, ind_tbl->queues[i]);
2768         }
2769         return ind_tbl;
2770 }
2771
2772 /**
2773  * Release an indirection table.
2774  *
2775  * @param dev
2776  *   Pointer to Ethernet device.
2777  * @param ind_table
2778  *   Indirection table to release.
2779  *
2780  * @return
2781  *   1 while a reference on it exists, 0 when freed.
2782  */
2783 static int
2784 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
2785                            struct mlx5_ind_table_obj *ind_tbl)
2786 {
2787         unsigned int i;
2788
2789         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt)) {
2790                 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV)
2791                         claim_zero(mlx5_glue->destroy_rwq_ind_table
2792                                                         (ind_tbl->ind_table));
2793                 else if (ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX)
2794                         claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
2795         }
2796         for (i = 0; i != ind_tbl->queues_n; ++i)
2797                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
2798         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
2799                 LIST_REMOVE(ind_tbl, next);
2800                 mlx5_free(ind_tbl);
2801                 return 0;
2802         }
2803         return 1;
2804 }
2805
2806 /**
2807  * Verify the Rx Queue list is empty
2808  *
2809  * @param dev
2810  *   Pointer to Ethernet device.
2811  *
2812  * @return
2813  *   The number of object not released.
2814  */
2815 int
2816 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
2817 {
2818         struct mlx5_priv *priv = dev->data->dev_private;
2819         struct mlx5_ind_table_obj *ind_tbl;
2820         int ret = 0;
2821
2822         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2823                 DRV_LOG(DEBUG,
2824                         "port %u indirection table obj %p still referenced",
2825                         dev->data->port_id, (void *)ind_tbl);
2826                 ++ret;
2827         }
2828         return ret;
2829 }
2830
2831 /**
2832  * Create an Rx Hash queue.
2833  *
2834  * @param dev
2835  *   Pointer to Ethernet device.
2836  * @param rss_key
2837  *   RSS key for the Rx hash queue.
2838  * @param rss_key_len
2839  *   RSS key length.
2840  * @param hash_fields
2841  *   Verbs protocol hash field to make the RSS on.
2842  * @param queues
2843  *   Queues entering in hash queue. In case of empty hash_fields only the
2844  *   first queue index will be taken for the indirection table.
2845  * @param queues_n
2846  *   Number of queues.
2847  * @param tunnel
2848  *   Tunnel type.
2849  *
2850  * @return
2851  *   The Verbs/DevX object initialised index, 0 otherwise and rte_errno is set.
2852  */
2853 uint32_t
2854 mlx5_hrxq_new(struct rte_eth_dev *dev,
2855               const uint8_t *rss_key, uint32_t rss_key_len,
2856               uint64_t hash_fields,
2857               const uint16_t *queues, uint32_t queues_n,
2858               int tunnel __rte_unused)
2859 {
2860         struct mlx5_priv *priv = dev->data->dev_private;
2861         struct mlx5_hrxq *hrxq;
2862         uint32_t hrxq_idx = 0;
2863         struct ibv_qp *qp = NULL;
2864         struct mlx5_ind_table_obj *ind_tbl;
2865         int err;
2866         struct mlx5_devx_obj *tir = NULL;
2867         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[queues[0]];
2868         struct mlx5_rxq_ctrl *rxq_ctrl =
2869                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2870
2871         queues_n = hash_fields ? queues_n : 1;
2872         ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2873         if (!ind_tbl) {
2874                 enum mlx5_ind_tbl_type type;
2875
2876                 type = rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV ?
2877                                 MLX5_IND_TBL_TYPE_IBV : MLX5_IND_TBL_TYPE_DEVX;
2878                 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n, type);
2879         }
2880         if (!ind_tbl) {
2881                 rte_errno = ENOMEM;
2882                 return 0;
2883         }
2884         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2885 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2886                 struct mlx5dv_qp_init_attr qp_init_attr;
2887
2888                 memset(&qp_init_attr, 0, sizeof(qp_init_attr));
2889                 if (tunnel) {
2890                         qp_init_attr.comp_mask =
2891                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2892                         qp_init_attr.create_flags =
2893                                 MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
2894                 }
2895 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2896                 if (dev->data->dev_conf.lpbk_mode) {
2897                         /*
2898                          * Allow packet sent from NIC loop back
2899                          * w/o source MAC check.
2900                          */
2901                         qp_init_attr.comp_mask |=
2902                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2903                         qp_init_attr.create_flags |=
2904                                 MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
2905                 }
2906 #endif
2907                 qp = mlx5_glue->dv_create_qp
2908                         (priv->sh->ctx,
2909                          &(struct ibv_qp_init_attr_ex){
2910                                 .qp_type = IBV_QPT_RAW_PACKET,
2911                                 .comp_mask =
2912                                         IBV_QP_INIT_ATTR_PD |
2913                                         IBV_QP_INIT_ATTR_IND_TABLE |
2914                                         IBV_QP_INIT_ATTR_RX_HASH,
2915                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2916                                         .rx_hash_function =
2917                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2918                                         .rx_hash_key_len = rss_key_len,
2919                                         .rx_hash_key =
2920                                                 (void *)(uintptr_t)rss_key,
2921                                         .rx_hash_fields_mask = hash_fields,
2922                                 },
2923                                 .rwq_ind_tbl = ind_tbl->ind_table,
2924                                 .pd = priv->sh->pd,
2925                           },
2926                           &qp_init_attr);
2927 #else
2928                 qp = mlx5_glue->create_qp_ex
2929                         (priv->sh->ctx,
2930                          &(struct ibv_qp_init_attr_ex){
2931                                 .qp_type = IBV_QPT_RAW_PACKET,
2932                                 .comp_mask =
2933                                         IBV_QP_INIT_ATTR_PD |
2934                                         IBV_QP_INIT_ATTR_IND_TABLE |
2935                                         IBV_QP_INIT_ATTR_RX_HASH,
2936                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2937                                         .rx_hash_function =
2938                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2939                                         .rx_hash_key_len = rss_key_len,
2940                                         .rx_hash_key =
2941                                                 (void *)(uintptr_t)rss_key,
2942                                         .rx_hash_fields_mask = hash_fields,
2943                                 },
2944                                 .rwq_ind_tbl = ind_tbl->ind_table,
2945                                 .pd = priv->sh->pd,
2946                          });
2947 #endif
2948                 if (!qp) {
2949                         rte_errno = errno;
2950                         goto error;
2951                 }
2952         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2953                 struct mlx5_devx_tir_attr tir_attr;
2954                 uint32_t i;
2955                 uint32_t lro = 1;
2956
2957                 /* Enable TIR LRO only if all the queues were configured for. */
2958                 for (i = 0; i < queues_n; ++i) {
2959                         if (!(*priv->rxqs)[queues[i]]->lro) {
2960                                 lro = 0;
2961                                 break;
2962                         }
2963                 }
2964                 memset(&tir_attr, 0, sizeof(tir_attr));
2965                 tir_attr.disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
2966                 tir_attr.rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
2967                 tir_attr.tunneled_offload_en = !!tunnel;
2968                 /* If needed, translate hash_fields bitmap to PRM format. */
2969                 if (hash_fields) {
2970 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2971                         struct mlx5_rx_hash_field_select *rx_hash_field_select =
2972                                         hash_fields & IBV_RX_HASH_INNER ?
2973                                         &tir_attr.rx_hash_field_selector_inner :
2974                                         &tir_attr.rx_hash_field_selector_outer;
2975 #else
2976                         struct mlx5_rx_hash_field_select *rx_hash_field_select =
2977                                         &tir_attr.rx_hash_field_selector_outer;
2978 #endif
2979
2980                         /* 1 bit: 0: IPv4, 1: IPv6. */
2981                         rx_hash_field_select->l3_prot_type =
2982                                 !!(hash_fields & MLX5_IPV6_IBV_RX_HASH);
2983                         /* 1 bit: 0: TCP, 1: UDP. */
2984                         rx_hash_field_select->l4_prot_type =
2985                                 !!(hash_fields & MLX5_UDP_IBV_RX_HASH);
2986                         /* Bitmask which sets which fields to use in RX Hash. */
2987                         rx_hash_field_select->selected_fields =
2988                         ((!!(hash_fields & MLX5_L3_SRC_IBV_RX_HASH)) <<
2989                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_SRC_IP) |
2990                         (!!(hash_fields & MLX5_L3_DST_IBV_RX_HASH)) <<
2991                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_DST_IP |
2992                         (!!(hash_fields & MLX5_L4_SRC_IBV_RX_HASH)) <<
2993                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_SPORT |
2994                         (!!(hash_fields & MLX5_L4_DST_IBV_RX_HASH)) <<
2995                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_DPORT;
2996                 }
2997                 if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
2998                         tir_attr.transport_domain = priv->sh->td->id;
2999                 else
3000                         tir_attr.transport_domain = priv->sh->tdn;
3001                 memcpy(tir_attr.rx_hash_toeplitz_key, rss_key,
3002                        MLX5_RSS_HASH_KEY_LEN);
3003                 tir_attr.indirect_table = ind_tbl->rqt->id;
3004                 if (dev->data->dev_conf.lpbk_mode)
3005                         tir_attr.self_lb_block =
3006                                         MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
3007                 if (lro) {
3008                         tir_attr.lro_timeout_period_usecs =
3009                                         priv->config.lro.timeout;
3010                         tir_attr.lro_max_msg_sz = priv->max_lro_msg_size;
3011                         tir_attr.lro_enable_mask =
3012                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
3013                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO;
3014                 }
3015                 tir = mlx5_devx_cmd_create_tir(priv->sh->ctx, &tir_attr);
3016                 if (!tir) {
3017                         DRV_LOG(ERR, "port %u cannot create DevX TIR",
3018                                 dev->data->port_id);
3019                         rte_errno = errno;
3020                         goto error;
3021                 }
3022         }
3023         hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
3024         if (!hrxq)
3025                 goto error;
3026         hrxq->ind_table = ind_tbl;
3027         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
3028                 hrxq->qp = qp;
3029 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3030                 hrxq->action =
3031                         mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
3032                 if (!hrxq->action) {
3033                         rte_errno = errno;
3034                         goto error;
3035                 }
3036 #endif
3037         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
3038                 hrxq->tir = tir;
3039 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3040                 hrxq->action = mlx5_glue->dv_create_flow_action_dest_devx_tir
3041                                                         (hrxq->tir->obj);
3042                 if (!hrxq->action) {
3043                         rte_errno = errno;
3044                         goto error;
3045                 }
3046 #endif
3047         }
3048         hrxq->rss_key_len = rss_key_len;
3049         hrxq->hash_fields = hash_fields;
3050         memcpy(hrxq->rss_key, rss_key, rss_key_len);
3051         rte_atomic32_inc(&hrxq->refcnt);
3052         ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs, hrxq_idx,
3053                      hrxq, next);
3054         return hrxq_idx;
3055 error:
3056         err = rte_errno; /* Save rte_errno before cleanup. */
3057         mlx5_ind_table_obj_release(dev, ind_tbl);
3058         if (qp)
3059                 claim_zero(mlx5_glue->destroy_qp(qp));
3060         else if (tir)
3061                 claim_zero(mlx5_devx_cmd_destroy(tir));
3062         rte_errno = err; /* Restore rte_errno. */
3063         return 0;
3064 }
3065
3066 /**
3067  * Get an Rx Hash queue.
3068  *
3069  * @param dev
3070  *   Pointer to Ethernet device.
3071  * @param rss_conf
3072  *   RSS configuration for the Rx hash queue.
3073  * @param queues
3074  *   Queues entering in hash queue. In case of empty hash_fields only the
3075  *   first queue index will be taken for the indirection table.
3076  * @param queues_n
3077  *   Number of queues.
3078  *
3079  * @return
3080  *   An hash Rx queue index on success.
3081  */
3082 uint32_t
3083 mlx5_hrxq_get(struct rte_eth_dev *dev,
3084               const uint8_t *rss_key, uint32_t rss_key_len,
3085               uint64_t hash_fields,
3086               const uint16_t *queues, uint32_t queues_n)
3087 {
3088         struct mlx5_priv *priv = dev->data->dev_private;
3089         struct mlx5_hrxq *hrxq;
3090         uint32_t idx;
3091
3092         queues_n = hash_fields ? queues_n : 1;
3093         ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
3094                       hrxq, next) {
3095                 struct mlx5_ind_table_obj *ind_tbl;
3096
3097                 if (hrxq->rss_key_len != rss_key_len)
3098                         continue;
3099                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
3100                         continue;
3101                 if (hrxq->hash_fields != hash_fields)
3102                         continue;
3103                 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
3104                 if (!ind_tbl)
3105                         continue;
3106                 if (ind_tbl != hrxq->ind_table) {
3107                         mlx5_ind_table_obj_release(dev, ind_tbl);
3108                         continue;
3109                 }
3110                 rte_atomic32_inc(&hrxq->refcnt);
3111                 return idx;
3112         }
3113         return 0;
3114 }
3115
3116 /**
3117  * Release the hash Rx queue.
3118  *
3119  * @param dev
3120  *   Pointer to Ethernet device.
3121  * @param hrxq
3122  *   Index to Hash Rx queue to release.
3123  *
3124  * @return
3125  *   1 while a reference on it exists, 0 when freed.
3126  */
3127 int
3128 mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hrxq_idx)
3129 {
3130         struct mlx5_priv *priv = dev->data->dev_private;
3131         struct mlx5_hrxq *hrxq;
3132
3133         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
3134         if (!hrxq)
3135                 return 0;
3136         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
3137 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3138                 mlx5_glue->destroy_flow_action(hrxq->action);
3139 #endif
3140                 if (hrxq->ind_table->type == MLX5_IND_TBL_TYPE_IBV)
3141                         claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3142                 else /* hrxq->ind_table->type == MLX5_IND_TBL_TYPE_DEVX */
3143                         claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
3144                 mlx5_ind_table_obj_release(dev, hrxq->ind_table);
3145                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs,
3146                              hrxq_idx, hrxq, next);
3147                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
3148                 return 0;
3149         }
3150         claim_nonzero(mlx5_ind_table_obj_release(dev, hrxq->ind_table));
3151         return 1;
3152 }
3153
3154 /**
3155  * Verify the Rx Queue list is empty
3156  *
3157  * @param dev
3158  *   Pointer to Ethernet device.
3159  *
3160  * @return
3161  *   The number of object not released.
3162  */
3163 int
3164 mlx5_hrxq_verify(struct rte_eth_dev *dev)
3165 {
3166         struct mlx5_priv *priv = dev->data->dev_private;
3167         struct mlx5_hrxq *hrxq;
3168         uint32_t idx;
3169         int ret = 0;
3170
3171         ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
3172                       hrxq, next) {
3173                 DRV_LOG(DEBUG,
3174                         "port %u hash Rx queue %p still referenced",
3175                         dev->data->port_id, (void *)hrxq);
3176                 ++ret;
3177         }
3178         return ret;
3179 }
3180
3181 /**
3182  * Create a drop Rx queue Verbs/DevX object.
3183  *
3184  * @param dev
3185  *   Pointer to Ethernet device.
3186  *
3187  * @return
3188  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3189  */
3190 static struct mlx5_rxq_obj *
3191 mlx5_rxq_obj_drop_new(struct rte_eth_dev *dev)
3192 {
3193         struct mlx5_priv *priv = dev->data->dev_private;
3194         struct ibv_context *ctx = priv->sh->ctx;
3195         struct ibv_cq *cq;
3196         struct ibv_wq *wq = NULL;
3197         struct mlx5_rxq_obj *rxq;
3198
3199         if (priv->drop_queue.rxq)
3200                 return priv->drop_queue.rxq;
3201         cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
3202         if (!cq) {
3203                 DEBUG("port %u cannot allocate CQ for drop queue",
3204                       dev->data->port_id);
3205                 rte_errno = errno;
3206                 goto error;
3207         }
3208         wq = mlx5_glue->create_wq(ctx,
3209                  &(struct ibv_wq_init_attr){
3210                         .wq_type = IBV_WQT_RQ,
3211                         .max_wr = 1,
3212                         .max_sge = 1,
3213                         .pd = priv->sh->pd,
3214                         .cq = cq,
3215                  });
3216         if (!wq) {
3217                 DEBUG("port %u cannot allocate WQ for drop queue",
3218                       dev->data->port_id);
3219                 rte_errno = errno;
3220                 goto error;
3221         }
3222         rxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq), 0, SOCKET_ID_ANY);
3223         if (!rxq) {
3224                 DEBUG("port %u cannot allocate drop Rx queue memory",
3225                       dev->data->port_id);
3226                 rte_errno = ENOMEM;
3227                 goto error;
3228         }
3229         rxq->ibv_cq = cq;
3230         rxq->wq = wq;
3231         priv->drop_queue.rxq = rxq;
3232         return rxq;
3233 error:
3234         if (wq)
3235                 claim_zero(mlx5_glue->destroy_wq(wq));
3236         if (cq)
3237                 claim_zero(mlx5_glue->destroy_cq(cq));
3238         return NULL;
3239 }
3240
3241 /**
3242  * Release a drop Rx queue Verbs/DevX object.
3243  *
3244  * @param dev
3245  *   Pointer to Ethernet device.
3246  *
3247  * @return
3248  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3249  */
3250 static void
3251 mlx5_rxq_obj_drop_release(struct rte_eth_dev *dev)
3252 {
3253         struct mlx5_priv *priv = dev->data->dev_private;
3254         struct mlx5_rxq_obj *rxq = priv->drop_queue.rxq;
3255
3256         if (rxq->wq)
3257                 claim_zero(mlx5_glue->destroy_wq(rxq->wq));
3258         if (rxq->ibv_cq)
3259                 claim_zero(mlx5_glue->destroy_cq(rxq->ibv_cq));
3260         mlx5_free(rxq);
3261         priv->drop_queue.rxq = NULL;
3262 }
3263
3264 /**
3265  * Create a drop indirection table.
3266  *
3267  * @param dev
3268  *   Pointer to Ethernet device.
3269  *
3270  * @return
3271  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3272  */
3273 static struct mlx5_ind_table_obj *
3274 mlx5_ind_table_obj_drop_new(struct rte_eth_dev *dev)
3275 {
3276         struct mlx5_priv *priv = dev->data->dev_private;
3277         struct mlx5_ind_table_obj *ind_tbl;
3278         struct mlx5_rxq_obj *rxq;
3279         struct mlx5_ind_table_obj tmpl;
3280
3281         rxq = mlx5_rxq_obj_drop_new(dev);
3282         if (!rxq)
3283                 return NULL;
3284         tmpl.ind_table = mlx5_glue->create_rwq_ind_table
3285                 (priv->sh->ctx,
3286                  &(struct ibv_rwq_ind_table_init_attr){
3287                         .log_ind_tbl_size = 0,
3288                         .ind_tbl = &rxq->wq,
3289                         .comp_mask = 0,
3290                  });
3291         if (!tmpl.ind_table) {
3292                 DEBUG("port %u cannot allocate indirection table for drop"
3293                       " queue",
3294                       dev->data->port_id);
3295                 rte_errno = errno;
3296                 goto error;
3297         }
3298         ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl), 0,
3299                               SOCKET_ID_ANY);
3300         if (!ind_tbl) {
3301                 rte_errno = ENOMEM;
3302                 goto error;
3303         }
3304         ind_tbl->ind_table = tmpl.ind_table;
3305         return ind_tbl;
3306 error:
3307         mlx5_rxq_obj_drop_release(dev);
3308         return NULL;
3309 }
3310
3311 /**
3312  * Release a drop indirection table.
3313  *
3314  * @param dev
3315  *   Pointer to Ethernet device.
3316  */
3317 static void
3318 mlx5_ind_table_obj_drop_release(struct rte_eth_dev *dev)
3319 {
3320         struct mlx5_priv *priv = dev->data->dev_private;
3321         struct mlx5_ind_table_obj *ind_tbl = priv->drop_queue.hrxq->ind_table;
3322
3323         claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
3324         mlx5_rxq_obj_drop_release(dev);
3325         mlx5_free(ind_tbl);
3326         priv->drop_queue.hrxq->ind_table = NULL;
3327 }
3328
3329 /**
3330  * Create a drop Rx Hash queue.
3331  *
3332  * @param dev
3333  *   Pointer to Ethernet device.
3334  *
3335  * @return
3336  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3337  */
3338 struct mlx5_hrxq *
3339 mlx5_hrxq_drop_new(struct rte_eth_dev *dev)
3340 {
3341         struct mlx5_priv *priv = dev->data->dev_private;
3342         struct mlx5_ind_table_obj *ind_tbl = NULL;
3343         struct ibv_qp *qp = NULL;
3344         struct mlx5_hrxq *hrxq = NULL;
3345
3346         if (priv->drop_queue.hrxq) {
3347                 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
3348                 return priv->drop_queue.hrxq;
3349         }
3350         hrxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq), 0, SOCKET_ID_ANY);
3351         if (!hrxq) {
3352                 DRV_LOG(WARNING,
3353                         "port %u cannot allocate memory for drop queue",
3354                         dev->data->port_id);
3355                 rte_errno = ENOMEM;
3356                 goto error;
3357         }
3358         priv->drop_queue.hrxq = hrxq;
3359         ind_tbl = mlx5_ind_table_obj_drop_new(dev);
3360         if (!ind_tbl)
3361                 goto error;
3362         hrxq->ind_table = ind_tbl;
3363         qp = mlx5_glue->create_qp_ex(priv->sh->ctx,
3364                  &(struct ibv_qp_init_attr_ex){
3365                         .qp_type = IBV_QPT_RAW_PACKET,
3366                         .comp_mask =
3367                                 IBV_QP_INIT_ATTR_PD |
3368                                 IBV_QP_INIT_ATTR_IND_TABLE |
3369                                 IBV_QP_INIT_ATTR_RX_HASH,
3370                         .rx_hash_conf = (struct ibv_rx_hash_conf){
3371                                 .rx_hash_function =
3372                                         IBV_RX_HASH_FUNC_TOEPLITZ,
3373                                 .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
3374                                 .rx_hash_key = rss_hash_default_key,
3375                                 .rx_hash_fields_mask = 0,
3376                                 },
3377                         .rwq_ind_tbl = ind_tbl->ind_table,
3378                         .pd = priv->sh->pd
3379                  });
3380         if (!qp) {
3381                 DEBUG("port %u cannot allocate QP for drop queue",
3382                       dev->data->port_id);
3383                 rte_errno = errno;
3384                 goto error;
3385         }
3386         hrxq->qp = qp;
3387 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3388         hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
3389         if (!hrxq->action) {
3390                 rte_errno = errno;
3391                 goto error;
3392         }
3393 #endif
3394         rte_atomic32_set(&hrxq->refcnt, 1);
3395         return hrxq;
3396 error:
3397 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3398         if (hrxq && hrxq->action)
3399                 mlx5_glue->destroy_flow_action(hrxq->action);
3400 #endif
3401         if (qp)
3402                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3403         if (ind_tbl)
3404                 mlx5_ind_table_obj_drop_release(dev);
3405         if (hrxq) {
3406                 priv->drop_queue.hrxq = NULL;
3407                 mlx5_free(hrxq);
3408         }
3409         return NULL;
3410 }
3411
3412 /**
3413  * Release a drop hash Rx queue.
3414  *
3415  * @param dev
3416  *   Pointer to Ethernet device.
3417  */
3418 void
3419 mlx5_hrxq_drop_release(struct rte_eth_dev *dev)
3420 {
3421         struct mlx5_priv *priv = dev->data->dev_private;
3422         struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
3423
3424         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
3425 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3426                 mlx5_glue->destroy_flow_action(hrxq->action);
3427 #endif
3428                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3429                 mlx5_ind_table_obj_drop_release(dev);
3430                 mlx5_free(hrxq);
3431                 priv->drop_queue.hrxq = NULL;
3432         }
3433 }
3434
3435
3436 /**
3437  * Set the Rx queue timestamp conversion parameters
3438  *
3439  * @param[in] dev
3440  *   Pointer to the Ethernet device structure.
3441  */
3442 void
3443 mlx5_rxq_timestamp_set(struct rte_eth_dev *dev)
3444 {
3445         struct mlx5_priv *priv = dev->data->dev_private;
3446         struct mlx5_dev_ctx_shared *sh = priv->sh;
3447         struct mlx5_rxq_data *data;
3448         unsigned int i;
3449
3450         for (i = 0; i != priv->rxqs_n; ++i) {
3451                 if (!(*priv->rxqs)[i])
3452                         continue;
3453                 data = (*priv->rxqs)[i];
3454                 data->sh = sh;
3455                 data->rt_timestamp = priv->config.rt_timestamp;
3456         }
3457 }