506c4d3707866e4caee6bd2fd8b00c607560283f
[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_RDY;
509                 rq_attr.state = MLX5_RQC_STATE_RST;
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 to 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_RST;
622                 rq_attr.state = MLX5_RQC_STATE_RDY;
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  * Release the resources allocated for an RQ DevX object.
836  *
837  * @param rxq_ctrl
838  *   DevX Rx queue object.
839  */
840 static void
841 rxq_release_devx_rq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
842 {
843         if (rxq_ctrl->rxq.wqes) {
844                 mlx5_free((void *)(uintptr_t)rxq_ctrl->rxq.wqes);
845                 rxq_ctrl->rxq.wqes = NULL;
846         }
847         if (rxq_ctrl->wq_umem) {
848                 mlx5_glue->devx_umem_dereg(rxq_ctrl->wq_umem);
849                 rxq_ctrl->wq_umem = NULL;
850         }
851 }
852
853 /**
854  * Release the resources allocated for the Rx CQ DevX object.
855  *
856  * @param rxq_ctrl
857  *   DevX Rx queue object.
858  */
859 static void
860 rxq_release_devx_cq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
861 {
862         if (rxq_ctrl->rxq.cqes) {
863                 rte_free((void *)(uintptr_t)rxq_ctrl->rxq.cqes);
864                 rxq_ctrl->rxq.cqes = NULL;
865         }
866         if (rxq_ctrl->cq_umem) {
867                 mlx5_glue->devx_umem_dereg(rxq_ctrl->cq_umem);
868                 rxq_ctrl->cq_umem = NULL;
869         }
870 }
871
872 /**
873  * Release an Rx hairpin related resources.
874  *
875  * @param rxq_obj
876  *   Hairpin Rx queue object.
877  */
878 static void
879 rxq_obj_hairpin_release(struct mlx5_rxq_obj *rxq_obj)
880 {
881         struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
882
883         MLX5_ASSERT(rxq_obj);
884         rq_attr.state = MLX5_RQC_STATE_RST;
885         rq_attr.rq_state = MLX5_RQC_STATE_RDY;
886         mlx5_devx_cmd_modify_rq(rxq_obj->rq, &rq_attr);
887         claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
888 }
889
890 /**
891  * Release an Rx verbs/DevX queue object.
892  *
893  * @param rxq_obj
894  *   Verbs/DevX Rx queue object.
895  */
896 static void
897 mlx5_rxq_obj_release(struct mlx5_rxq_obj *rxq_obj)
898 {
899         struct mlx5_priv *priv = rxq_obj->rxq_ctrl->priv;
900         struct mlx5_rxq_ctrl *rxq_ctrl = rxq_obj->rxq_ctrl;
901
902         MLX5_ASSERT(rxq_obj);
903         switch (rxq_obj->type) {
904         case MLX5_RXQ_OBJ_TYPE_IBV:
905                 MLX5_ASSERT(rxq_obj->wq);
906                 MLX5_ASSERT(rxq_obj->ibv_cq);
907                 rxq_free_elts(rxq_ctrl);
908                 claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
909                 claim_zero(mlx5_glue->destroy_cq(rxq_obj->ibv_cq));
910                 if (rxq_obj->ibv_channel)
911                         claim_zero(mlx5_glue->destroy_comp_channel
912                                                         (rxq_obj->ibv_channel));
913                 break;
914         case MLX5_RXQ_OBJ_TYPE_DEVX_RQ:
915                 MLX5_ASSERT(rxq_obj->rq);
916                 MLX5_ASSERT(rxq_obj->devx_cq);
917                 rxq_free_elts(rxq_ctrl);
918                 claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
919                 claim_zero(mlx5_devx_cmd_destroy(rxq_obj->devx_cq));
920                 claim_zero(mlx5_release_dbr(&priv->dbrpgs,
921                                             rxq_ctrl->rq_dbr_umem_id,
922                                             rxq_ctrl->rq_dbr_offset));
923                 claim_zero(mlx5_release_dbr(&priv->dbrpgs,
924                                             rxq_ctrl->cq_dbr_umem_id,
925                                             rxq_ctrl->cq_dbr_offset));
926                 if (rxq_obj->devx_channel)
927                         mlx5_glue->devx_destroy_event_channel
928                                                         (rxq_obj->devx_channel);
929                 rxq_release_devx_rq_resources(rxq_ctrl);
930                 rxq_release_devx_cq_resources(rxq_ctrl);
931                 break;
932         case MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN:
933                 MLX5_ASSERT(rxq_obj->rq);
934                 rxq_obj_hairpin_release(rxq_obj);
935                 break;
936         }
937         LIST_REMOVE(rxq_obj, next);
938         mlx5_free(rxq_obj);
939 }
940
941 /**
942  * Allocate queue vector and fill epoll fd list for Rx interrupts.
943  *
944  * @param dev
945  *   Pointer to Ethernet device.
946  *
947  * @return
948  *   0 on success, a negative errno value otherwise and rte_errno is set.
949  */
950 int
951 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
952 {
953         struct mlx5_priv *priv = dev->data->dev_private;
954         unsigned int i;
955         unsigned int rxqs_n = priv->rxqs_n;
956         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
957         unsigned int count = 0;
958         struct rte_intr_handle *intr_handle = dev->intr_handle;
959
960         if (!dev->data->dev_conf.intr_conf.rxq)
961                 return 0;
962         mlx5_rx_intr_vec_disable(dev);
963         intr_handle->intr_vec = mlx5_malloc(0,
964                                 n * sizeof(intr_handle->intr_vec[0]),
965                                 0, SOCKET_ID_ANY);
966         if (intr_handle->intr_vec == NULL) {
967                 DRV_LOG(ERR,
968                         "port %u failed to allocate memory for interrupt"
969                         " vector, Rx interrupts will not be supported",
970                         dev->data->port_id);
971                 rte_errno = ENOMEM;
972                 return -rte_errno;
973         }
974         intr_handle->type = RTE_INTR_HANDLE_EXT;
975         for (i = 0; i != n; ++i) {
976                 /* This rxq obj must not be released in this function. */
977                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
978                 struct mlx5_rxq_obj *rxq_obj = rxq_ctrl ? rxq_ctrl->obj : NULL;
979                 int rc;
980
981                 /* Skip queues that cannot request interrupts. */
982                 if (!rxq_obj || (!rxq_obj->ibv_channel &&
983                                  !rxq_obj->devx_channel)) {
984                         /* Use invalid intr_vec[] index to disable entry. */
985                         intr_handle->intr_vec[i] =
986                                 RTE_INTR_VEC_RXTX_OFFSET +
987                                 RTE_MAX_RXTX_INTR_VEC_ID;
988                         /* Decrease the rxq_ctrl's refcnt */
989                         if (rxq_ctrl)
990                                 mlx5_rxq_release(dev, i);
991                         continue;
992                 }
993                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
994                         DRV_LOG(ERR,
995                                 "port %u too many Rx queues for interrupt"
996                                 " vector size (%d), Rx interrupts cannot be"
997                                 " enabled",
998                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
999                         mlx5_rx_intr_vec_disable(dev);
1000                         rte_errno = ENOMEM;
1001                         return -rte_errno;
1002                 }
1003                 rc = mlx5_os_set_nonblock_channel_fd(rxq_obj->fd);
1004                 if (rc < 0) {
1005                         rte_errno = errno;
1006                         DRV_LOG(ERR,
1007                                 "port %u failed to make Rx interrupt file"
1008                                 " descriptor %d non-blocking for queue index"
1009                                 " %d",
1010                                 dev->data->port_id, rxq_obj->fd, i);
1011                         mlx5_rx_intr_vec_disable(dev);
1012                         return -rte_errno;
1013                 }
1014                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
1015                 intr_handle->efds[count] = rxq_obj->fd;
1016                 count++;
1017         }
1018         if (!count)
1019                 mlx5_rx_intr_vec_disable(dev);
1020         else
1021                 intr_handle->nb_efd = count;
1022         return 0;
1023 }
1024
1025 /**
1026  * Clean up Rx interrupts handler.
1027  *
1028  * @param dev
1029  *   Pointer to Ethernet device.
1030  */
1031 void
1032 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
1033 {
1034         struct mlx5_priv *priv = dev->data->dev_private;
1035         struct rte_intr_handle *intr_handle = dev->intr_handle;
1036         unsigned int i;
1037         unsigned int rxqs_n = priv->rxqs_n;
1038         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1039
1040         if (!dev->data->dev_conf.intr_conf.rxq)
1041                 return;
1042         if (!intr_handle->intr_vec)
1043                 goto free;
1044         for (i = 0; i != n; ++i) {
1045                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
1046                     RTE_MAX_RXTX_INTR_VEC_ID)
1047                         continue;
1048                 /**
1049                  * Need to access directly the queue to release the reference
1050                  * kept in mlx5_rx_intr_vec_enable().
1051                  */
1052                 mlx5_rxq_release(dev, i);
1053         }
1054 free:
1055         rte_intr_free_epoll_fd(intr_handle);
1056         if (intr_handle->intr_vec)
1057                 mlx5_free(intr_handle->intr_vec);
1058         intr_handle->nb_efd = 0;
1059         intr_handle->intr_vec = NULL;
1060 }
1061
1062 /**
1063  *  MLX5 CQ notification .
1064  *
1065  *  @param rxq
1066  *     Pointer to receive queue structure.
1067  *  @param sq_n_rxq
1068  *     Sequence number per receive queue .
1069  */
1070 static inline void
1071 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
1072 {
1073         int sq_n = 0;
1074         uint32_t doorbell_hi;
1075         uint64_t doorbell;
1076         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
1077
1078         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
1079         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
1080         doorbell = (uint64_t)doorbell_hi << 32;
1081         doorbell |= rxq->cqn;
1082         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
1083         mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
1084                          cq_db_reg, rxq->uar_lock_cq);
1085 }
1086
1087 /**
1088  * DPDK callback for Rx queue interrupt enable.
1089  *
1090  * @param dev
1091  *   Pointer to Ethernet device structure.
1092  * @param rx_queue_id
1093  *   Rx queue number.
1094  *
1095  * @return
1096  *   0 on success, a negative errno value otherwise and rte_errno is set.
1097  */
1098 int
1099 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1100 {
1101         struct mlx5_rxq_ctrl *rxq_ctrl;
1102
1103         rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
1104         if (!rxq_ctrl)
1105                 goto error;
1106         if (rxq_ctrl->irq) {
1107                 if (!rxq_ctrl->obj) {
1108                         mlx5_rxq_release(dev, rx_queue_id);
1109                         goto error;
1110                 }
1111                 mlx5_arm_cq(&rxq_ctrl->rxq, rxq_ctrl->rxq.cq_arm_sn);
1112         }
1113         mlx5_rxq_release(dev, rx_queue_id);
1114         return 0;
1115 error:
1116         rte_errno = EINVAL;
1117         return -rte_errno;
1118 }
1119
1120 /**
1121  * DPDK callback for Rx queue interrupt disable.
1122  *
1123  * @param dev
1124  *   Pointer to Ethernet device structure.
1125  * @param rx_queue_id
1126  *   Rx queue number.
1127  *
1128  * @return
1129  *   0 on success, a negative errno value otherwise and rte_errno is set.
1130  */
1131 int
1132 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1133 {
1134         struct mlx5_rxq_ctrl *rxq_ctrl;
1135         struct mlx5_rxq_obj *rxq_obj = NULL;
1136         struct ibv_cq *ev_cq;
1137         void *ev_ctx;
1138         int ret = 0;
1139
1140         rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
1141         if (!rxq_ctrl) {
1142                 rte_errno = EINVAL;
1143                 return -rte_errno;
1144         }
1145         if (!rxq_ctrl->irq) {
1146                 mlx5_rxq_release(dev, rx_queue_id);
1147                 return 0;
1148         }
1149         rxq_obj = rxq_ctrl->obj;
1150         if (!rxq_obj)
1151                 goto error;
1152         if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1153                 ret = mlx5_glue->get_cq_event(rxq_obj->ibv_channel, &ev_cq,
1154                                               &ev_ctx);
1155                 if (ret < 0 || ev_cq != rxq_obj->ibv_cq)
1156                         goto error;
1157                 mlx5_glue->ack_cq_events(rxq_obj->ibv_cq, 1);
1158         } else if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1159 #ifdef HAVE_IBV_DEVX_EVENT
1160                 union {
1161                         struct mlx5dv_devx_async_event_hdr event_resp;
1162                         uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr)
1163                                     + 128];
1164                 } out;
1165
1166                 ret = mlx5_glue->devx_get_event
1167                                 (rxq_obj->devx_channel, &out.event_resp,
1168                                  sizeof(out.buf));
1169                 if (ret < 0 || out.event_resp.cookie !=
1170                                 (uint64_t)(uintptr_t)rxq_obj->devx_cq)
1171                         goto error;
1172 #endif /* HAVE_IBV_DEVX_EVENT */
1173         }
1174         rxq_ctrl->rxq.cq_arm_sn++;
1175         mlx5_rxq_release(dev, rx_queue_id);
1176         return 0;
1177 error:
1178         /**
1179          * For ret < 0 save the errno (may be EAGAIN which means the get_event
1180          * function was called before receiving one).
1181          */
1182         if (ret < 0)
1183                 rte_errno = errno;
1184         else
1185                 rte_errno = EINVAL;
1186         ret = rte_errno; /* Save rte_errno before cleanup. */
1187         mlx5_rxq_release(dev, rx_queue_id);
1188         if (ret != EAGAIN)
1189                 DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
1190                         dev->data->port_id, rx_queue_id);
1191         rte_errno = ret; /* Restore rte_errno. */
1192         return -rte_errno;
1193 }
1194
1195 /**
1196  * Create a CQ Verbs object.
1197  *
1198  * @param dev
1199  *   Pointer to Ethernet device.
1200  * @param priv
1201  *   Pointer to device private data.
1202  * @param rxq_data
1203  *   Pointer to Rx queue data.
1204  * @param cqe_n
1205  *   Number of CQEs in CQ.
1206  * @param rxq_obj
1207  *   Pointer to Rx queue object data.
1208  *
1209  * @return
1210  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1211  */
1212 static struct ibv_cq *
1213 mlx5_ibv_cq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
1214                 struct mlx5_rxq_data *rxq_data,
1215                 unsigned int cqe_n, struct mlx5_rxq_obj *rxq_obj)
1216 {
1217         struct {
1218                 struct ibv_cq_init_attr_ex ibv;
1219                 struct mlx5dv_cq_init_attr mlx5;
1220         } cq_attr;
1221
1222         cq_attr.ibv = (struct ibv_cq_init_attr_ex){
1223                 .cqe = cqe_n,
1224                 .channel = rxq_obj->ibv_channel,
1225                 .comp_mask = 0,
1226         };
1227         cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
1228                 .comp_mask = 0,
1229         };
1230         if (priv->config.cqe_comp && !rxq_data->hw_timestamp) {
1231                 cq_attr.mlx5.comp_mask |=
1232                                 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
1233 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1234                 cq_attr.mlx5.cqe_comp_res_format =
1235                                 mlx5_rxq_mprq_enabled(rxq_data) ?
1236                                 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
1237                                 MLX5DV_CQE_RES_FORMAT_HASH;
1238 #else
1239                 cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
1240 #endif
1241                 /*
1242                  * For vectorized Rx, it must not be doubled in order to
1243                  * make cq_ci and rq_ci aligned.
1244                  */
1245                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
1246                         cq_attr.ibv.cqe *= 2;
1247         } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
1248                 DRV_LOG(DEBUG,
1249                         "port %u Rx CQE compression is disabled for HW"
1250                         " timestamp",
1251                         dev->data->port_id);
1252         }
1253 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
1254         if (priv->config.cqe_pad) {
1255                 cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
1256                 cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
1257         }
1258 #endif
1259         return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(priv->sh->ctx,
1260                                                               &cq_attr.ibv,
1261                                                               &cq_attr.mlx5));
1262 }
1263
1264 /**
1265  * Create a WQ Verbs object.
1266  *
1267  * @param dev
1268  *   Pointer to Ethernet device.
1269  * @param priv
1270  *   Pointer to device private data.
1271  * @param rxq_data
1272  *   Pointer to Rx queue data.
1273  * @param idx
1274  *   Queue index in DPDK Rx queue array
1275  * @param wqe_n
1276  *   Number of WQEs in WQ.
1277  * @param rxq_obj
1278  *   Pointer to Rx queue object data.
1279  *
1280  * @return
1281  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1282  */
1283 static struct ibv_wq *
1284 mlx5_ibv_wq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
1285                 struct mlx5_rxq_data *rxq_data, uint16_t idx,
1286                 unsigned int wqe_n, struct mlx5_rxq_obj *rxq_obj)
1287 {
1288         struct {
1289                 struct ibv_wq_init_attr ibv;
1290 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1291                 struct mlx5dv_wq_init_attr mlx5;
1292 #endif
1293         } wq_attr;
1294
1295         wq_attr.ibv = (struct ibv_wq_init_attr){
1296                 .wq_context = NULL, /* Could be useful in the future. */
1297                 .wq_type = IBV_WQT_RQ,
1298                 /* Max number of outstanding WRs. */
1299                 .max_wr = wqe_n >> rxq_data->sges_n,
1300                 /* Max number of scatter/gather elements in a WR. */
1301                 .max_sge = 1 << rxq_data->sges_n,
1302                 .pd = priv->sh->pd,
1303                 .cq = rxq_obj->ibv_cq,
1304                 .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
1305                 .create_flags = (rxq_data->vlan_strip ?
1306                                  IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
1307         };
1308         /* By default, FCS (CRC) is stripped by hardware. */
1309         if (rxq_data->crc_present) {
1310                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
1311                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1312         }
1313         if (priv->config.hw_padding) {
1314 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
1315                 wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
1316                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1317 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
1318                 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
1319                 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
1320 #endif
1321         }
1322 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1323         wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
1324                 .comp_mask = 0,
1325         };
1326         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1327                 struct mlx5dv_striding_rq_init_attr *mprq_attr =
1328                                                 &wq_attr.mlx5.striding_rq_attrs;
1329
1330                 wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
1331                 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
1332                         .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
1333                         .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
1334                         .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
1335                 };
1336         }
1337         rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->ctx, &wq_attr.ibv,
1338                                               &wq_attr.mlx5);
1339 #else
1340         rxq_obj->wq = mlx5_glue->create_wq(priv->sh->ctx, &wq_attr.ibv);
1341 #endif
1342         if (rxq_obj->wq) {
1343                 /*
1344                  * Make sure number of WRs*SGEs match expectations since a queue
1345                  * cannot allocate more than "desc" buffers.
1346                  */
1347                 if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
1348                     wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
1349                         DRV_LOG(ERR,
1350                                 "port %u Rx queue %u requested %u*%u but got"
1351                                 " %u*%u WRs*SGEs",
1352                                 dev->data->port_id, idx,
1353                                 wqe_n >> rxq_data->sges_n,
1354                                 (1 << rxq_data->sges_n),
1355                                 wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
1356                         claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
1357                         rxq_obj->wq = NULL;
1358                         rte_errno = EINVAL;
1359                 }
1360         }
1361         return rxq_obj->wq;
1362 }
1363
1364 /**
1365  * Fill common fields of create RQ attributes structure.
1366  *
1367  * @param rxq_data
1368  *   Pointer to Rx queue data.
1369  * @param cqn
1370  *   CQ number to use with this RQ.
1371  * @param rq_attr
1372  *   RQ attributes structure to fill..
1373  */
1374 static void
1375 mlx5_devx_create_rq_attr_fill(struct mlx5_rxq_data *rxq_data, uint32_t cqn,
1376                               struct mlx5_devx_create_rq_attr *rq_attr)
1377 {
1378         rq_attr->state = MLX5_RQC_STATE_RST;
1379         rq_attr->vsd = (rxq_data->vlan_strip) ? 0 : 1;
1380         rq_attr->cqn = cqn;
1381         rq_attr->scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
1382 }
1383
1384 /**
1385  * Fill common fields of DevX WQ attributes structure.
1386  *
1387  * @param priv
1388  *   Pointer to device private data.
1389  * @param rxq_ctrl
1390  *   Pointer to Rx queue control structure.
1391  * @param wq_attr
1392  *   WQ attributes structure to fill..
1393  */
1394 static void
1395 mlx5_devx_wq_attr_fill(struct mlx5_priv *priv, struct mlx5_rxq_ctrl *rxq_ctrl,
1396                        struct mlx5_devx_wq_attr *wq_attr)
1397 {
1398         wq_attr->end_padding_mode = priv->config.cqe_pad ?
1399                                         MLX5_WQ_END_PAD_MODE_ALIGN :
1400                                         MLX5_WQ_END_PAD_MODE_NONE;
1401         wq_attr->pd = priv->sh->pdn;
1402         wq_attr->dbr_addr = rxq_ctrl->rq_dbr_offset;
1403         wq_attr->dbr_umem_id = rxq_ctrl->rq_dbr_umem_id;
1404         wq_attr->dbr_umem_valid = 1;
1405         wq_attr->wq_umem_id = mlx5_os_get_umem_id(rxq_ctrl->wq_umem);
1406         wq_attr->wq_umem_valid = 1;
1407 }
1408
1409 /**
1410  * Create a RQ object using DevX.
1411  *
1412  * @param dev
1413  *   Pointer to Ethernet device.
1414  * @param idx
1415  *   Queue index in DPDK Rx queue array
1416  * @param cqn
1417  *   CQ number to use with this RQ.
1418  *
1419  * @return
1420  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1421  */
1422 static struct mlx5_devx_obj *
1423 mlx5_devx_rq_new(struct rte_eth_dev *dev, uint16_t idx, uint32_t cqn)
1424 {
1425         struct mlx5_priv *priv = dev->data->dev_private;
1426         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1427         struct mlx5_rxq_ctrl *rxq_ctrl =
1428                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1429         struct mlx5_devx_create_rq_attr rq_attr = { 0 };
1430         uint32_t wqe_n = 1 << (rxq_data->elts_n - rxq_data->sges_n);
1431         uint32_t wq_size = 0;
1432         uint32_t wqe_size = 0;
1433         uint32_t log_wqe_size = 0;
1434         void *buf = NULL;
1435         struct mlx5_devx_obj *rq;
1436
1437         /* Fill RQ attributes. */
1438         rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
1439         rq_attr.flush_in_error_en = 1;
1440         mlx5_devx_create_rq_attr_fill(rxq_data, cqn, &rq_attr);
1441         /* Fill WQ attributes for this RQ. */
1442         if (mlx5_rxq_mprq_enabled(rxq_data)) {
1443                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
1444                 /*
1445                  * Number of strides in each WQE:
1446                  * 512*2^single_wqe_log_num_of_strides.
1447                  */
1448                 rq_attr.wq_attr.single_wqe_log_num_of_strides =
1449                                 rxq_data->strd_num_n -
1450                                 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1451                 /* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
1452                 rq_attr.wq_attr.single_stride_log_num_of_bytes =
1453                                 rxq_data->strd_sz_n -
1454                                 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1455                 wqe_size = sizeof(struct mlx5_wqe_mprq);
1456         } else {
1457                 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1458                 wqe_size = sizeof(struct mlx5_wqe_data_seg);
1459         }
1460         log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
1461         rq_attr.wq_attr.log_wq_stride = log_wqe_size;
1462         rq_attr.wq_attr.log_wq_sz = rxq_data->elts_n - rxq_data->sges_n;
1463         /* Calculate and allocate WQ memory space. */
1464         wqe_size = 1 << log_wqe_size; /* round up power of two.*/
1465         wq_size = wqe_n * wqe_size;
1466         size_t alignment = MLX5_WQE_BUF_ALIGNMENT;
1467         if (alignment == (size_t)-1) {
1468                 DRV_LOG(ERR, "Failed to get mem page size");
1469                 rte_errno = ENOMEM;
1470                 return NULL;
1471         }
1472         buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, wq_size,
1473                           alignment, rxq_ctrl->socket);
1474         if (!buf)
1475                 return NULL;
1476         rxq_data->wqes = buf;
1477         rxq_ctrl->wq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
1478                                                      buf, wq_size, 0);
1479         if (!rxq_ctrl->wq_umem) {
1480                 mlx5_free(buf);
1481                 return NULL;
1482         }
1483         mlx5_devx_wq_attr_fill(priv, rxq_ctrl, &rq_attr.wq_attr);
1484         rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &rq_attr, rxq_ctrl->socket);
1485         if (!rq)
1486                 rxq_release_devx_rq_resources(rxq_ctrl);
1487         return rq;
1488 }
1489
1490 /**
1491  * Create a DevX CQ object for an Rx queue.
1492  *
1493  * @param dev
1494  *   Pointer to Ethernet device.
1495  * @param cqe_n
1496  *   Number of CQEs in CQ.
1497  * @param idx
1498  *   Queue index in DPDK Rx queue array
1499  * @param rxq_obj
1500  *   Pointer to Rx queue object data.
1501  *
1502  * @return
1503  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1504  */
1505 static struct mlx5_devx_obj *
1506 mlx5_devx_cq_new(struct rte_eth_dev *dev, unsigned int cqe_n, uint16_t idx,
1507                  struct mlx5_rxq_obj *rxq_obj)
1508 {
1509         struct mlx5_devx_obj *cq_obj = 0;
1510         struct mlx5_devx_cq_attr cq_attr = { 0 };
1511         struct mlx5_priv *priv = dev->data->dev_private;
1512         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1513         struct mlx5_rxq_ctrl *rxq_ctrl =
1514                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1515         size_t page_size = rte_mem_page_size();
1516         uint32_t lcore = (uint32_t)rte_lcore_to_cpu_id(-1);
1517         uint32_t eqn = 0;
1518         void *buf = NULL;
1519         uint16_t event_nums[1] = {0};
1520         uint32_t log_cqe_n;
1521         uint32_t cq_size;
1522         int ret = 0;
1523
1524         if (page_size == (size_t)-1) {
1525                 DRV_LOG(ERR, "Failed to get page_size.");
1526                 goto error;
1527         }
1528         if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
1529             !rxq_data->lro) {
1530                 cq_attr.cqe_comp_en = MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
1531 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1532                 cq_attr.mini_cqe_res_format =
1533                                 mlx5_rxq_mprq_enabled(rxq_data) ?
1534                                 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
1535                                 MLX5DV_CQE_RES_FORMAT_HASH;
1536 #else
1537                 cq_attr.mini_cqe_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
1538 #endif
1539                 /*
1540                  * For vectorized Rx, it must not be doubled in order to
1541                  * make cq_ci and rq_ci aligned.
1542                  */
1543                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
1544                         cqe_n *= 2;
1545         } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
1546                 DRV_LOG(DEBUG,
1547                         "port %u Rx CQE compression is disabled for HW"
1548                         " timestamp",
1549                         dev->data->port_id);
1550         } else if (priv->config.cqe_comp && rxq_data->lro) {
1551                 DRV_LOG(DEBUG,
1552                         "port %u Rx CQE compression is disabled for LRO",
1553                         dev->data->port_id);
1554         }
1555 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
1556         if (priv->config.cqe_pad)
1557                 cq_attr.cqe_size = MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
1558 #endif
1559         log_cqe_n = log2above(cqe_n);
1560         cq_size = sizeof(struct mlx5_cqe) * (1 << log_cqe_n);
1561         /* Query the EQN for this core. */
1562         if (mlx5_glue->devx_query_eqn(priv->sh->ctx, lcore, &eqn)) {
1563                 DRV_LOG(ERR, "Failed to query EQN for CQ.");
1564                 goto error;
1565         }
1566         cq_attr.eqn = eqn;
1567         buf = rte_calloc_socket(__func__, 1, cq_size, page_size,
1568                                 rxq_ctrl->socket);
1569         if (!buf) {
1570                 DRV_LOG(ERR, "Failed to allocate memory for CQ.");
1571                 goto error;
1572         }
1573         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)buf;
1574         rxq_ctrl->cq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx, buf,
1575                                                      cq_size,
1576                                                      IBV_ACCESS_LOCAL_WRITE);
1577         if (!rxq_ctrl->cq_umem) {
1578                 DRV_LOG(ERR, "Failed to register umem for CQ.");
1579                 goto error;
1580         }
1581         cq_attr.uar_page_id =
1582                 mlx5_os_get_devx_uar_page_id(priv->sh->devx_rx_uar);
1583         cq_attr.q_umem_id = mlx5_os_get_umem_id(rxq_ctrl->cq_umem);
1584         cq_attr.q_umem_valid = 1;
1585         cq_attr.log_cq_size = log_cqe_n;
1586         cq_attr.log_page_size = rte_log2_u32(page_size);
1587         cq_attr.db_umem_offset = rxq_ctrl->cq_dbr_offset;
1588         cq_attr.db_umem_id = rxq_ctrl->cq_dbr_umem_id;
1589         cq_attr.db_umem_valid = 1;
1590         cq_obj = mlx5_devx_cmd_create_cq(priv->sh->ctx, &cq_attr);
1591         if (!cq_obj)
1592                 goto error;
1593         rxq_data->cqe_n = log_cqe_n;
1594         rxq_data->cqn = cq_obj->id;
1595         if (rxq_obj->devx_channel) {
1596                 ret = mlx5_glue->devx_subscribe_devx_event
1597                                                 (rxq_obj->devx_channel,
1598                                                  cq_obj->obj,
1599                                                  sizeof(event_nums),
1600                                                  event_nums,
1601                                                  (uint64_t)(uintptr_t)cq_obj);
1602                 if (ret) {
1603                         DRV_LOG(ERR, "Fail to subscribe CQ to event channel.");
1604                         rte_errno = errno;
1605                         goto error;
1606                 }
1607         }
1608         /* Initialise CQ to 1's to mark HW ownership for all CQEs. */
1609         memset((void *)(uintptr_t)rxq_data->cqes, 0xFF, cq_size);
1610         return cq_obj;
1611 error:
1612         if (cq_obj)
1613                 mlx5_devx_cmd_destroy(cq_obj);
1614         rxq_release_devx_cq_resources(rxq_ctrl);
1615         return NULL;
1616 }
1617
1618 /**
1619  * Create the Rx hairpin queue object.
1620  *
1621  * @param dev
1622  *   Pointer to Ethernet device.
1623  * @param idx
1624  *   Queue index in DPDK Rx queue array
1625  *
1626  * @return
1627  *   The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
1628  */
1629 static struct mlx5_rxq_obj *
1630 mlx5_rxq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
1631 {
1632         struct mlx5_priv *priv = dev->data->dev_private;
1633         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1634         struct mlx5_rxq_ctrl *rxq_ctrl =
1635                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1636         struct mlx5_devx_create_rq_attr attr = { 0 };
1637         struct mlx5_rxq_obj *tmpl = NULL;
1638         uint32_t max_wq_data;
1639
1640         MLX5_ASSERT(rxq_data);
1641         MLX5_ASSERT(!rxq_ctrl->obj);
1642         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1643                            rxq_ctrl->socket);
1644         if (!tmpl) {
1645                 DRV_LOG(ERR, "port %u Rx queue %u cannot allocate resources",
1646                         dev->data->port_id, rxq_data->idx);
1647                 rte_errno = ENOMEM;
1648                 return NULL;
1649         }
1650         tmpl->type = MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN;
1651         tmpl->rxq_ctrl = rxq_ctrl;
1652         attr.hairpin = 1;
1653         max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
1654         /* Jumbo frames > 9KB should be supported, and more packets. */
1655         if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
1656                 if (priv->config.log_hp_size > max_wq_data) {
1657                         DRV_LOG(ERR, "total data size %u power of 2 is "
1658                                 "too large for hairpin",
1659                                 priv->config.log_hp_size);
1660                         mlx5_free(tmpl);
1661                         rte_errno = ERANGE;
1662                         return NULL;
1663                 }
1664                 attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
1665         } else {
1666                 attr.wq_attr.log_hairpin_data_sz =
1667                                 (max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
1668                                  max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
1669         }
1670         /* Set the packets number to the maximum value for performance. */
1671         attr.wq_attr.log_hairpin_num_packets =
1672                         attr.wq_attr.log_hairpin_data_sz -
1673                         MLX5_HAIRPIN_QUEUE_STRIDE;
1674         tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &attr,
1675                                            rxq_ctrl->socket);
1676         if (!tmpl->rq) {
1677                 DRV_LOG(ERR,
1678                         "port %u Rx hairpin queue %u can't create rq object",
1679                         dev->data->port_id, idx);
1680                 mlx5_free(tmpl);
1681                 rte_errno = errno;
1682                 return NULL;
1683         }
1684         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1685                 idx, (void *)&tmpl);
1686         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1687         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
1688         return tmpl;
1689 }
1690
1691 /**
1692  * Create the Rx queue Verbs/DevX object.
1693  *
1694  * @param dev
1695  *   Pointer to Ethernet device.
1696  * @param idx
1697  *   Queue index in DPDK Rx queue array
1698  * @param type
1699  *   Type of Rx queue object to create.
1700  *
1701  * @return
1702  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
1703  */
1704 struct mlx5_rxq_obj *
1705 mlx5_rxq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
1706                  enum mlx5_rxq_obj_type type)
1707 {
1708         struct mlx5_priv *priv = dev->data->dev_private;
1709         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1710         struct mlx5_rxq_ctrl *rxq_ctrl =
1711                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1712         struct ibv_wq_attr mod;
1713         unsigned int cqe_n;
1714         unsigned int wqe_n = 1 << rxq_data->elts_n;
1715         struct mlx5_rxq_obj *tmpl = NULL;
1716         struct mlx5_devx_dbr_page *cq_dbr_page = NULL;
1717         struct mlx5_devx_dbr_page *rq_dbr_page = NULL;
1718         struct mlx5dv_cq cq_info;
1719         struct mlx5dv_rwq rwq;
1720         int ret = 0;
1721         struct mlx5dv_obj obj;
1722
1723         MLX5_ASSERT(rxq_data);
1724         MLX5_ASSERT(!rxq_ctrl->obj);
1725         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
1726                 return mlx5_rxq_obj_hairpin_new(dev, idx);
1727         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1728                            rxq_ctrl->socket);
1729         if (!tmpl) {
1730                 DRV_LOG(ERR, "port %u Rx queue %u cannot allocate resources",
1731                         dev->data->port_id, rxq_data->idx);
1732                 rte_errno = ENOMEM;
1733                 goto error;
1734         }
1735         tmpl->type = type;
1736         tmpl->rxq_ctrl = rxq_ctrl;
1737         if (rxq_ctrl->irq) {
1738                 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1739                         tmpl->ibv_channel =
1740                                 mlx5_glue->create_comp_channel(priv->sh->ctx);
1741                         if (!tmpl->ibv_channel) {
1742                                 DRV_LOG(ERR, "port %u: comp channel creation "
1743                                         "failure", dev->data->port_id);
1744                                 rte_errno = ENOMEM;
1745                                 goto error;
1746                         }
1747                         tmpl->fd = ((struct ibv_comp_channel *)
1748                                         (tmpl->ibv_channel))->fd;
1749                 } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1750                         int devx_ev_flag =
1751                           MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA;
1752
1753                         tmpl->devx_channel =
1754                                 mlx5_glue->devx_create_event_channel
1755                                                                 (priv->sh->ctx,
1756                                                                  devx_ev_flag);
1757                         if (!tmpl->devx_channel) {
1758                                 rte_errno = errno;
1759                                 DRV_LOG(ERR,
1760                                         "Failed to create event channel %d.",
1761                                         rte_errno);
1762                                 goto error;
1763                         }
1764                         tmpl->fd =
1765                                 mlx5_os_get_devx_channel_fd(tmpl->devx_channel);
1766                 }
1767         }
1768         if (mlx5_rxq_mprq_enabled(rxq_data))
1769                 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
1770         else
1771                 cqe_n = wqe_n - 1;
1772         DRV_LOG(DEBUG, "port %u device_attr.max_qp_wr is %d",
1773                 dev->data->port_id, priv->sh->device_attr.max_qp_wr);
1774         DRV_LOG(DEBUG, "port %u device_attr.max_sge is %d",
1775                 dev->data->port_id, priv->sh->device_attr.max_sge);
1776         if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1777                 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
1778                 priv->verbs_alloc_ctx.obj = rxq_ctrl;
1779                 /* Create CQ using Verbs API. */
1780                 tmpl->ibv_cq = mlx5_ibv_cq_new(dev, priv, rxq_data, cqe_n,
1781                                                tmpl);
1782                 if (!tmpl->ibv_cq) {
1783                         DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
1784                                 dev->data->port_id, idx);
1785                         rte_errno = ENOMEM;
1786                         goto error;
1787                 }
1788                 obj.cq.in = tmpl->ibv_cq;
1789                 obj.cq.out = &cq_info;
1790                 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
1791                 if (ret) {
1792                         rte_errno = ret;
1793                         goto error;
1794                 }
1795                 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1796                         DRV_LOG(ERR,
1797                                 "port %u wrong MLX5_CQE_SIZE environment "
1798                                 "variable value: it should be set to %u",
1799                                 dev->data->port_id, RTE_CACHE_LINE_SIZE);
1800                         rte_errno = EINVAL;
1801                         goto error;
1802                 }
1803                 /* Fill the rings. */
1804                 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
1805                 rxq_data->cq_db = cq_info.dbrec;
1806                 rxq_data->cqes =
1807                         (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
1808                 rxq_data->cq_uar = cq_info.cq_uar;
1809                 rxq_data->cqn = cq_info.cqn;
1810                 /* Create WQ (RQ) using Verbs API. */
1811                 tmpl->wq = mlx5_ibv_wq_new(dev, priv, rxq_data, idx, wqe_n,
1812                                            tmpl);
1813                 if (!tmpl->wq) {
1814                         DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
1815                                 dev->data->port_id, idx);
1816                         rte_errno = ENOMEM;
1817                         goto error;
1818                 }
1819                 /* Change queue state to ready. */
1820                 mod = (struct ibv_wq_attr){
1821                         .attr_mask = IBV_WQ_ATTR_STATE,
1822                         .wq_state = IBV_WQS_RDY,
1823                 };
1824                 ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
1825                 if (ret) {
1826                         DRV_LOG(ERR,
1827                                 "port %u Rx queue %u WQ state to IBV_WQS_RDY"
1828                                 " failed", dev->data->port_id, idx);
1829                         rte_errno = ret;
1830                         goto error;
1831                 }
1832                 obj.rwq.in = tmpl->wq;
1833                 obj.rwq.out = &rwq;
1834                 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
1835                 if (ret) {
1836                         rte_errno = ret;
1837                         goto error;
1838                 }
1839                 rxq_data->wqes = rwq.buf;
1840                 rxq_data->rq_db = rwq.dbrec;
1841                 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1842         } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1843                 struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
1844                 int64_t dbr_offset;
1845
1846                 /* Allocate CQ door-bell. */
1847                 dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs,
1848                                           &cq_dbr_page);
1849                 if (dbr_offset < 0) {
1850                         DRV_LOG(ERR, "Failed to allocate CQ door-bell.");
1851                         goto error;
1852                 }
1853                 rxq_ctrl->cq_dbr_offset = dbr_offset;
1854                 rxq_ctrl->cq_dbr_umem_id =
1855                                         mlx5_os_get_umem_id(cq_dbr_page->umem);
1856                 rxq_data->cq_db =
1857                         (uint32_t *)((uintptr_t)cq_dbr_page->dbrs +
1858                                      (uintptr_t)rxq_ctrl->cq_dbr_offset);
1859                 rxq_data->cq_uar =
1860                         mlx5_os_get_devx_uar_base_addr(priv->sh->devx_rx_uar);
1861                 /* Create CQ using DevX API. */
1862                 tmpl->devx_cq = mlx5_devx_cq_new(dev, cqe_n, idx, tmpl);
1863                 if (!tmpl->devx_cq) {
1864                         DRV_LOG(ERR, "Failed to create CQ.");
1865                         goto error;
1866                 }
1867                 /* Allocate RQ door-bell. */
1868                 dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs,
1869                                           &rq_dbr_page);
1870                 if (dbr_offset < 0) {
1871                         DRV_LOG(ERR, "Failed to allocate RQ door-bell.");
1872                         goto error;
1873                 }
1874                 rxq_ctrl->rq_dbr_offset = dbr_offset;
1875                 rxq_ctrl->rq_dbr_umem_id =
1876                                         mlx5_os_get_umem_id(rq_dbr_page->umem);
1877                 rxq_data->rq_db =
1878                         (uint32_t *)((uintptr_t)rq_dbr_page->dbrs +
1879                                      (uintptr_t)rxq_ctrl->rq_dbr_offset);
1880                 /* Create RQ using DevX API. */
1881                 tmpl->rq = mlx5_devx_rq_new(dev, idx, tmpl->devx_cq->id);
1882                 if (!tmpl->rq) {
1883                         DRV_LOG(ERR, "port %u Rx queue %u RQ creation failure",
1884                                 dev->data->port_id, idx);
1885                         rte_errno = ENOMEM;
1886                         goto error;
1887                 }
1888                 /* Change queue state to ready. */
1889                 rq_attr.rq_state = MLX5_RQC_STATE_RST;
1890                 rq_attr.state = MLX5_RQC_STATE_RDY;
1891                 ret = mlx5_devx_cmd_modify_rq(tmpl->rq, &rq_attr);
1892                 if (ret)
1893                         goto error;
1894         }
1895         rxq_data->cq_arm_sn = 0;
1896         mlx5_rxq_initialize(rxq_data);
1897         rxq_data->cq_ci = 0;
1898         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1899                 idx, (void *)&tmpl);
1900         LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1901         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
1902         return tmpl;
1903 error:
1904         if (tmpl) {
1905                 ret = rte_errno; /* Save rte_errno before cleanup. */
1906                 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1907                         if (tmpl->wq)
1908                                 claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
1909                         if (tmpl->ibv_cq)
1910                                 claim_zero(mlx5_glue->destroy_cq(tmpl->ibv_cq));
1911                         if (tmpl->ibv_channel)
1912                                 claim_zero(mlx5_glue->destroy_comp_channel
1913                                                         (tmpl->ibv_channel));
1914                         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1915                 } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1916                         if (tmpl->rq)
1917                                 claim_zero(mlx5_devx_cmd_destroy(tmpl->rq));
1918                         if (tmpl->devx_cq)
1919                                 claim_zero(mlx5_devx_cmd_destroy
1920                                                         (tmpl->devx_cq));
1921                         if (tmpl->devx_channel)
1922                                 mlx5_glue->devx_destroy_event_channel
1923                                                         (tmpl->devx_channel);
1924                         if (rq_dbr_page)
1925                                 claim_zero(mlx5_release_dbr
1926                                                      (&priv->dbrpgs,
1927                                                       rxq_ctrl->rq_dbr_umem_id,
1928                                                       rxq_ctrl->rq_dbr_offset));
1929                         if (cq_dbr_page)
1930                                 claim_zero(mlx5_release_dbr
1931                                                      (&priv->dbrpgs,
1932                                                       rxq_ctrl->cq_dbr_umem_id,
1933                                                       rxq_ctrl->cq_dbr_offset));
1934                 }
1935                 mlx5_free(tmpl);
1936                 rte_errno = ret; /* Restore rte_errno. */
1937         }
1938         if (type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1939                 rxq_release_devx_rq_resources(rxq_ctrl);
1940                 rxq_release_devx_cq_resources(rxq_ctrl);
1941         }
1942         return NULL;
1943 }
1944
1945 /**
1946  * Verify the Rx queue objects list is empty
1947  *
1948  * @param dev
1949  *   Pointer to Ethernet device.
1950  *
1951  * @return
1952  *   The number of objects not released.
1953  */
1954 int
1955 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1956 {
1957         struct mlx5_priv *priv = dev->data->dev_private;
1958         int ret = 0;
1959         struct mlx5_rxq_obj *rxq_obj;
1960
1961         LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1962                 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1963                         dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1964                 ++ret;
1965         }
1966         return ret;
1967 }
1968
1969 /**
1970  * Callback function to initialize mbufs for Multi-Packet RQ.
1971  */
1972 static inline void
1973 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1974                     void *_m, unsigned int i __rte_unused)
1975 {
1976         struct mlx5_mprq_buf *buf = _m;
1977         struct rte_mbuf_ext_shared_info *shinfo;
1978         unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1979         unsigned int j;
1980
1981         memset(_m, 0, sizeof(*buf));
1982         buf->mp = mp;
1983         rte_atomic16_set(&buf->refcnt, 1);
1984         for (j = 0; j != strd_n; ++j) {
1985                 shinfo = &buf->shinfos[j];
1986                 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1987                 shinfo->fcb_opaque = buf;
1988         }
1989 }
1990
1991 /**
1992  * Free mempool of Multi-Packet RQ.
1993  *
1994  * @param dev
1995  *   Pointer to Ethernet device.
1996  *
1997  * @return
1998  *   0 on success, negative errno value on failure.
1999  */
2000 int
2001 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
2002 {
2003         struct mlx5_priv *priv = dev->data->dev_private;
2004         struct rte_mempool *mp = priv->mprq_mp;
2005         unsigned int i;
2006
2007         if (mp == NULL)
2008                 return 0;
2009         DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
2010                 dev->data->port_id, mp->name);
2011         /*
2012          * If a buffer in the pool has been externally attached to a mbuf and it
2013          * is still in use by application, destroying the Rx queue can spoil
2014          * the packet. It is unlikely to happen but if application dynamically
2015          * creates and destroys with holding Rx packets, this can happen.
2016          *
2017          * TODO: It is unavoidable for now because the mempool for Multi-Packet
2018          * RQ isn't provided by application but managed by PMD.
2019          */
2020         if (!rte_mempool_full(mp)) {
2021                 DRV_LOG(ERR,
2022                         "port %u mempool for Multi-Packet RQ is still in use",
2023                         dev->data->port_id);
2024                 rte_errno = EBUSY;
2025                 return -rte_errno;
2026         }
2027         rte_mempool_free(mp);
2028         /* Unset mempool for each Rx queue. */
2029         for (i = 0; i != priv->rxqs_n; ++i) {
2030                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2031
2032                 if (rxq == NULL)
2033                         continue;
2034                 rxq->mprq_mp = NULL;
2035         }
2036         priv->mprq_mp = NULL;
2037         return 0;
2038 }
2039
2040 /**
2041  * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
2042  * mempool. If already allocated, reuse it if there're enough elements.
2043  * Otherwise, resize it.
2044  *
2045  * @param dev
2046  *   Pointer to Ethernet device.
2047  *
2048  * @return
2049  *   0 on success, negative errno value on failure.
2050  */
2051 int
2052 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
2053 {
2054         struct mlx5_priv *priv = dev->data->dev_private;
2055         struct rte_mempool *mp = priv->mprq_mp;
2056         char name[RTE_MEMPOOL_NAMESIZE];
2057         unsigned int desc = 0;
2058         unsigned int buf_len;
2059         unsigned int obj_num;
2060         unsigned int obj_size;
2061         unsigned int strd_num_n = 0;
2062         unsigned int strd_sz_n = 0;
2063         unsigned int i;
2064         unsigned int n_ibv = 0;
2065
2066         if (!mlx5_mprq_enabled(dev))
2067                 return 0;
2068         /* Count the total number of descriptors configured. */
2069         for (i = 0; i != priv->rxqs_n; ++i) {
2070                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2071                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
2072                         (rxq, struct mlx5_rxq_ctrl, rxq);
2073
2074                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
2075                         continue;
2076                 n_ibv++;
2077                 desc += 1 << rxq->elts_n;
2078                 /* Get the max number of strides. */
2079                 if (strd_num_n < rxq->strd_num_n)
2080                         strd_num_n = rxq->strd_num_n;
2081                 /* Get the max size of a stride. */
2082                 if (strd_sz_n < rxq->strd_sz_n)
2083                         strd_sz_n = rxq->strd_sz_n;
2084         }
2085         MLX5_ASSERT(strd_num_n && strd_sz_n);
2086         buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
2087         obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
2088                 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
2089         /*
2090          * Received packets can be either memcpy'd or externally referenced. In
2091          * case that the packet is attached to an mbuf as an external buffer, as
2092          * it isn't possible to predict how the buffers will be queued by
2093          * application, there's no option to exactly pre-allocate needed buffers
2094          * in advance but to speculatively prepares enough buffers.
2095          *
2096          * In the data path, if this Mempool is depleted, PMD will try to memcpy
2097          * received packets to buffers provided by application (rxq->mp) until
2098          * this Mempool gets available again.
2099          */
2100         desc *= 4;
2101         obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
2102         /*
2103          * rte_mempool_create_empty() has sanity check to refuse large cache
2104          * size compared to the number of elements.
2105          * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
2106          * constant number 2 instead.
2107          */
2108         obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
2109         /* Check a mempool is already allocated and if it can be resued. */
2110         if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
2111                 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
2112                         dev->data->port_id, mp->name);
2113                 /* Reuse. */
2114                 goto exit;
2115         } else if (mp != NULL) {
2116                 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
2117                         dev->data->port_id, mp->name);
2118                 /*
2119                  * If failed to free, which means it may be still in use, no way
2120                  * but to keep using the existing one. On buffer underrun,
2121                  * packets will be memcpy'd instead of external buffer
2122                  * attachment.
2123                  */
2124                 if (mlx5_mprq_free_mp(dev)) {
2125                         if (mp->elt_size >= obj_size)
2126                                 goto exit;
2127                         else
2128                                 return -rte_errno;
2129                 }
2130         }
2131         snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
2132         mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
2133                                 0, NULL, NULL, mlx5_mprq_buf_init,
2134                                 (void *)(uintptr_t)(1 << strd_num_n),
2135                                 dev->device->numa_node, 0);
2136         if (mp == NULL) {
2137                 DRV_LOG(ERR,
2138                         "port %u failed to allocate a mempool for"
2139                         " Multi-Packet RQ, count=%u, size=%u",
2140                         dev->data->port_id, obj_num, obj_size);
2141                 rte_errno = ENOMEM;
2142                 return -rte_errno;
2143         }
2144         priv->mprq_mp = mp;
2145 exit:
2146         /* Set mempool for each Rx queue. */
2147         for (i = 0; i != priv->rxqs_n; ++i) {
2148                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
2149                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
2150                         (rxq, struct mlx5_rxq_ctrl, rxq);
2151
2152                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
2153                         continue;
2154                 rxq->mprq_mp = mp;
2155         }
2156         DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
2157                 dev->data->port_id);
2158         return 0;
2159 }
2160
2161 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
2162                                         sizeof(struct rte_vlan_hdr) * 2 + \
2163                                         sizeof(struct rte_ipv6_hdr)))
2164 #define MAX_TCP_OPTION_SIZE 40u
2165 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
2166                                  sizeof(struct rte_tcp_hdr) + \
2167                                  MAX_TCP_OPTION_SIZE))
2168
2169 /**
2170  * Adjust the maximum LRO massage size.
2171  *
2172  * @param dev
2173  *   Pointer to Ethernet device.
2174  * @param idx
2175  *   RX queue index.
2176  * @param max_lro_size
2177  *   The maximum size for LRO packet.
2178  */
2179 static void
2180 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
2181                              uint32_t max_lro_size)
2182 {
2183         struct mlx5_priv *priv = dev->data->dev_private;
2184
2185         if (priv->config.hca_attr.lro_max_msg_sz_mode ==
2186             MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
2187             MLX5_MAX_TCP_HDR_OFFSET)
2188                 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
2189         max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
2190         MLX5_ASSERT(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
2191         max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
2192         if (priv->max_lro_msg_size)
2193                 priv->max_lro_msg_size =
2194                         RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
2195         else
2196                 priv->max_lro_msg_size = max_lro_size;
2197         DRV_LOG(DEBUG,
2198                 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
2199                 dev->data->port_id, idx,
2200                 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
2201 }
2202
2203 /**
2204  * Create a DPDK Rx queue.
2205  *
2206  * @param dev
2207  *   Pointer to Ethernet device.
2208  * @param idx
2209  *   RX queue index.
2210  * @param desc
2211  *   Number of descriptors to configure in queue.
2212  * @param socket
2213  *   NUMA socket on which memory must be allocated.
2214  *
2215  * @return
2216  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
2217  */
2218 struct mlx5_rxq_ctrl *
2219 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2220              unsigned int socket, const struct rte_eth_rxconf *conf,
2221              struct rte_mempool *mp)
2222 {
2223         struct mlx5_priv *priv = dev->data->dev_private;
2224         struct mlx5_rxq_ctrl *tmpl;
2225         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
2226         unsigned int mprq_stride_nums;
2227         unsigned int mprq_stride_size;
2228         unsigned int mprq_stride_cap;
2229         struct mlx5_dev_config *config = &priv->config;
2230         /*
2231          * Always allocate extra slots, even if eventually
2232          * the vector Rx will not be used.
2233          */
2234         uint16_t desc_n =
2235                 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
2236         uint64_t offloads = conf->offloads |
2237                            dev->data->dev_conf.rxmode.offloads;
2238         unsigned int lro_on_queue = !!(offloads & DEV_RX_OFFLOAD_TCP_LRO);
2239         const int mprq_en = mlx5_check_mprq_support(dev) > 0;
2240         unsigned int max_rx_pkt_len = lro_on_queue ?
2241                         dev->data->dev_conf.rxmode.max_lro_pkt_size :
2242                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
2243         unsigned int non_scatter_min_mbuf_size = max_rx_pkt_len +
2244                                                         RTE_PKTMBUF_HEADROOM;
2245         unsigned int max_lro_size = 0;
2246         unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
2247
2248         if (non_scatter_min_mbuf_size > mb_len && !(offloads &
2249                                                     DEV_RX_OFFLOAD_SCATTER)) {
2250                 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
2251                         " configured and no enough mbuf space(%u) to contain "
2252                         "the maximum RX packet length(%u) with head-room(%u)",
2253                         dev->data->port_id, idx, mb_len, max_rx_pkt_len,
2254                         RTE_PKTMBUF_HEADROOM);
2255                 rte_errno = ENOSPC;
2256                 return NULL;
2257         }
2258         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl) +
2259                            desc_n * sizeof(struct rte_mbuf *), 0, socket);
2260         if (!tmpl) {
2261                 rte_errno = ENOMEM;
2262                 return NULL;
2263         }
2264         tmpl->type = MLX5_RXQ_TYPE_STANDARD;
2265         if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
2266                                MLX5_MR_BTREE_CACHE_N, socket)) {
2267                 /* rte_errno is already set. */
2268                 goto error;
2269         }
2270         tmpl->socket = socket;
2271         if (dev->data->dev_conf.intr_conf.rxq)
2272                 tmpl->irq = 1;
2273         mprq_stride_nums = config->mprq.stride_num_n ?
2274                 config->mprq.stride_num_n : MLX5_MPRQ_STRIDE_NUM_N;
2275         mprq_stride_size = non_scatter_min_mbuf_size <=
2276                 (1U << config->mprq.max_stride_size_n) ?
2277                 log2above(non_scatter_min_mbuf_size) : MLX5_MPRQ_STRIDE_SIZE_N;
2278         mprq_stride_cap = (config->mprq.stride_num_n ?
2279                 (1U << config->mprq.stride_num_n) : (1U << mprq_stride_nums)) *
2280                         (config->mprq.stride_size_n ?
2281                 (1U << config->mprq.stride_size_n) : (1U << mprq_stride_size));
2282         /*
2283          * This Rx queue can be configured as a Multi-Packet RQ if all of the
2284          * following conditions are met:
2285          *  - MPRQ is enabled.
2286          *  - The number of descs is more than the number of strides.
2287          *  - max_rx_pkt_len plus overhead is less than the max size
2288          *    of a stride or mprq_stride_size is specified by a user.
2289          *    Need to nake sure that there are enough stides to encap
2290          *    the maximum packet size in case mprq_stride_size is set.
2291          *  Otherwise, enable Rx scatter if necessary.
2292          */
2293         if (mprq_en && desc > (1U << mprq_stride_nums) &&
2294             (non_scatter_min_mbuf_size <=
2295              (1U << config->mprq.max_stride_size_n) ||
2296              (config->mprq.stride_size_n &&
2297               non_scatter_min_mbuf_size <= mprq_stride_cap))) {
2298                 /* TODO: Rx scatter isn't supported yet. */
2299                 tmpl->rxq.sges_n = 0;
2300                 /* Trim the number of descs needed. */
2301                 desc >>= mprq_stride_nums;
2302                 tmpl->rxq.strd_num_n = config->mprq.stride_num_n ?
2303                         config->mprq.stride_num_n : mprq_stride_nums;
2304                 tmpl->rxq.strd_sz_n = config->mprq.stride_size_n ?
2305                         config->mprq.stride_size_n : mprq_stride_size;
2306                 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
2307                 tmpl->rxq.strd_scatter_en =
2308                                 !!(offloads & DEV_RX_OFFLOAD_SCATTER);
2309                 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
2310                                 config->mprq.max_memcpy_len);
2311                 max_lro_size = RTE_MIN(max_rx_pkt_len,
2312                                        (1u << tmpl->rxq.strd_num_n) *
2313                                        (1u << tmpl->rxq.strd_sz_n));
2314                 DRV_LOG(DEBUG,
2315                         "port %u Rx queue %u: Multi-Packet RQ is enabled"
2316                         " strd_num_n = %u, strd_sz_n = %u",
2317                         dev->data->port_id, idx,
2318                         tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
2319         } else if (max_rx_pkt_len <= first_mb_free_size) {
2320                 tmpl->rxq.sges_n = 0;
2321                 max_lro_size = max_rx_pkt_len;
2322         } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
2323                 unsigned int size = non_scatter_min_mbuf_size;
2324                 unsigned int sges_n;
2325
2326                 if (lro_on_queue && first_mb_free_size <
2327                     MLX5_MAX_LRO_HEADER_FIX) {
2328                         DRV_LOG(ERR, "Not enough space in the first segment(%u)"
2329                                 " to include the max header size(%u) for LRO",
2330                                 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
2331                         rte_errno = ENOTSUP;
2332                         goto error;
2333                 }
2334                 /*
2335                  * Determine the number of SGEs needed for a full packet
2336                  * and round it to the next power of two.
2337                  */
2338                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
2339                 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
2340                         DRV_LOG(ERR,
2341                                 "port %u too many SGEs (%u) needed to handle"
2342                                 " requested maximum packet size %u, the maximum"
2343                                 " supported are %u", dev->data->port_id,
2344                                 1 << sges_n, max_rx_pkt_len,
2345                                 1u << MLX5_MAX_LOG_RQ_SEGS);
2346                         rte_errno = ENOTSUP;
2347                         goto error;
2348                 }
2349                 tmpl->rxq.sges_n = sges_n;
2350                 max_lro_size = max_rx_pkt_len;
2351         }
2352         if (config->mprq.enabled && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
2353                 DRV_LOG(WARNING,
2354                         "port %u MPRQ is requested but cannot be enabled\n"
2355                         " (requested: pkt_sz = %u, desc_num = %u,"
2356                         " rxq_num = %u, stride_sz = %u, stride_num = %u\n"
2357                         "  supported: min_rxqs_num = %u,"
2358                         " min_stride_sz = %u, max_stride_sz = %u).",
2359                         dev->data->port_id, non_scatter_min_mbuf_size,
2360                         desc, priv->rxqs_n,
2361                         config->mprq.stride_size_n ?
2362                                 (1U << config->mprq.stride_size_n) :
2363                                 (1U << mprq_stride_size),
2364                         config->mprq.stride_num_n ?
2365                                 (1U << config->mprq.stride_num_n) :
2366                                 (1U << mprq_stride_nums),
2367                         config->mprq.min_rxqs_num,
2368                         (1U << config->mprq.min_stride_size_n),
2369                         (1U << config->mprq.max_stride_size_n));
2370         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
2371                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
2372         if (desc % (1 << tmpl->rxq.sges_n)) {
2373                 DRV_LOG(ERR,
2374                         "port %u number of Rx queue descriptors (%u) is not a"
2375                         " multiple of SGEs per packet (%u)",
2376                         dev->data->port_id,
2377                         desc,
2378                         1 << tmpl->rxq.sges_n);
2379                 rte_errno = EINVAL;
2380                 goto error;
2381         }
2382         mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
2383         /* Toggle RX checksum offload if hardware supports it. */
2384         tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
2385         tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
2386         /* Configure VLAN stripping. */
2387         tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2388         /* By default, FCS (CRC) is stripped by hardware. */
2389         tmpl->rxq.crc_present = 0;
2390         tmpl->rxq.lro = lro_on_queue;
2391         if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
2392                 if (config->hw_fcs_strip) {
2393                         /*
2394                          * RQs used for LRO-enabled TIRs should not be
2395                          * configured to scatter the FCS.
2396                          */
2397                         if (lro_on_queue)
2398                                 DRV_LOG(WARNING,
2399                                         "port %u CRC stripping has been "
2400                                         "disabled but will still be performed "
2401                                         "by hardware, because LRO is enabled",
2402                                         dev->data->port_id);
2403                         else
2404                                 tmpl->rxq.crc_present = 1;
2405                 } else {
2406                         DRV_LOG(WARNING,
2407                                 "port %u CRC stripping has been disabled but will"
2408                                 " still be performed by hardware, make sure MLNX_OFED"
2409                                 " and firmware are up to date",
2410                                 dev->data->port_id);
2411                 }
2412         }
2413         DRV_LOG(DEBUG,
2414                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
2415                 " incoming frames to hide it",
2416                 dev->data->port_id,
2417                 tmpl->rxq.crc_present ? "disabled" : "enabled",
2418                 tmpl->rxq.crc_present << 2);
2419         /* Save port ID. */
2420         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
2421                 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
2422         tmpl->rxq.port_id = dev->data->port_id;
2423         tmpl->priv = priv;
2424         tmpl->rxq.mp = mp;
2425         tmpl->rxq.elts_n = log2above(desc);
2426         tmpl->rxq.rq_repl_thresh =
2427                 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
2428         tmpl->rxq.elts =
2429                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
2430 #ifndef RTE_ARCH_64
2431         tmpl->rxq.uar_lock_cq = &priv->sh->uar_lock_cq;
2432 #endif
2433         tmpl->rxq.idx = idx;
2434         rte_atomic32_inc(&tmpl->refcnt);
2435         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
2436         return tmpl;
2437 error:
2438         mlx5_free(tmpl);
2439         return NULL;
2440 }
2441
2442 /**
2443  * Create a DPDK Rx hairpin queue.
2444  *
2445  * @param dev
2446  *   Pointer to Ethernet device.
2447  * @param idx
2448  *   RX queue index.
2449  * @param desc
2450  *   Number of descriptors to configure in queue.
2451  * @param hairpin_conf
2452  *   The hairpin binding configuration.
2453  *
2454  * @return
2455  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
2456  */
2457 struct mlx5_rxq_ctrl *
2458 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2459                      const struct rte_eth_hairpin_conf *hairpin_conf)
2460 {
2461         struct mlx5_priv *priv = dev->data->dev_private;
2462         struct mlx5_rxq_ctrl *tmpl;
2463
2464         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
2465                            SOCKET_ID_ANY);
2466         if (!tmpl) {
2467                 rte_errno = ENOMEM;
2468                 return NULL;
2469         }
2470         tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
2471         tmpl->socket = SOCKET_ID_ANY;
2472         tmpl->rxq.rss_hash = 0;
2473         tmpl->rxq.port_id = dev->data->port_id;
2474         tmpl->priv = priv;
2475         tmpl->rxq.mp = NULL;
2476         tmpl->rxq.elts_n = log2above(desc);
2477         tmpl->rxq.elts = NULL;
2478         tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
2479         tmpl->hairpin_conf = *hairpin_conf;
2480         tmpl->rxq.idx = idx;
2481         rte_atomic32_inc(&tmpl->refcnt);
2482         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
2483         return tmpl;
2484 }
2485
2486 /**
2487  * Get a Rx queue.
2488  *
2489  * @param dev
2490  *   Pointer to Ethernet device.
2491  * @param idx
2492  *   RX queue index.
2493  *
2494  * @return
2495  *   A pointer to the queue if it exists, NULL otherwise.
2496  */
2497 struct mlx5_rxq_ctrl *
2498 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
2499 {
2500         struct mlx5_priv *priv = dev->data->dev_private;
2501         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
2502         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2503
2504         if (rxq_data) {
2505                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2506                 rte_atomic32_inc(&rxq_ctrl->refcnt);
2507         }
2508         return rxq_ctrl;
2509 }
2510
2511 /**
2512  * Release a Rx queue.
2513  *
2514  * @param dev
2515  *   Pointer to Ethernet device.
2516  * @param idx
2517  *   RX queue index.
2518  *
2519  * @return
2520  *   1 while a reference on it exists, 0 when freed.
2521  */
2522 int
2523 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
2524 {
2525         struct mlx5_priv *priv = dev->data->dev_private;
2526         struct mlx5_rxq_ctrl *rxq_ctrl;
2527
2528         if (!(*priv->rxqs)[idx])
2529                 return 0;
2530         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
2531         if (!rte_atomic32_dec_and_test(&rxq_ctrl->refcnt))
2532                 return 1;
2533         if (rxq_ctrl->obj) {
2534                 mlx5_rxq_obj_release(rxq_ctrl->obj);
2535                 rxq_ctrl->obj = NULL;
2536         }
2537         if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
2538                 mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
2539         LIST_REMOVE(rxq_ctrl, next);
2540         mlx5_free(rxq_ctrl);
2541         (*priv->rxqs)[idx] = NULL;
2542         return 0;
2543 }
2544
2545 /**
2546  * Verify the Rx Queue list is empty
2547  *
2548  * @param dev
2549  *   Pointer to Ethernet device.
2550  *
2551  * @return
2552  *   The number of object not released.
2553  */
2554 int
2555 mlx5_rxq_verify(struct rte_eth_dev *dev)
2556 {
2557         struct mlx5_priv *priv = dev->data->dev_private;
2558         struct mlx5_rxq_ctrl *rxq_ctrl;
2559         int ret = 0;
2560
2561         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
2562                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
2563                         dev->data->port_id, rxq_ctrl->rxq.idx);
2564                 ++ret;
2565         }
2566         return ret;
2567 }
2568
2569 /**
2570  * Get a Rx queue type.
2571  *
2572  * @param dev
2573  *   Pointer to Ethernet device.
2574  * @param idx
2575  *   Rx queue index.
2576  *
2577  * @return
2578  *   The Rx queue type.
2579  */
2580 enum mlx5_rxq_type
2581 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
2582 {
2583         struct mlx5_priv *priv = dev->data->dev_private;
2584         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
2585
2586         if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
2587                 rxq_ctrl = container_of((*priv->rxqs)[idx],
2588                                         struct mlx5_rxq_ctrl,
2589                                         rxq);
2590                 return rxq_ctrl->type;
2591         }
2592         return MLX5_RXQ_TYPE_UNDEFINED;
2593 }
2594
2595 /**
2596  * Create an indirection table.
2597  *
2598  * @param dev
2599  *   Pointer to Ethernet device.
2600  * @param queues
2601  *   Queues entering in the indirection table.
2602  * @param queues_n
2603  *   Number of queues in the array.
2604  *
2605  * @return
2606  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2607  */
2608 static struct mlx5_ind_table_obj *
2609 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
2610                        uint32_t queues_n, enum mlx5_ind_tbl_type type)
2611 {
2612         struct mlx5_priv *priv = dev->data->dev_private;
2613         struct mlx5_ind_table_obj *ind_tbl;
2614         unsigned int i = 0, j = 0, k = 0;
2615
2616         ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl) +
2617                               queues_n * sizeof(uint16_t), 0, SOCKET_ID_ANY);
2618         if (!ind_tbl) {
2619                 rte_errno = ENOMEM;
2620                 return NULL;
2621         }
2622         ind_tbl->type = type;
2623         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2624                 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
2625                         log2above(queues_n) :
2626                         log2above(priv->config.ind_table_max_size);
2627                 struct ibv_wq *wq[1 << wq_n];
2628
2629                 for (i = 0; i != queues_n; ++i) {
2630                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2631                                                                  queues[i]);
2632                         if (!rxq)
2633                                 goto error;
2634                         wq[i] = rxq->obj->wq;
2635                         ind_tbl->queues[i] = queues[i];
2636                 }
2637                 ind_tbl->queues_n = queues_n;
2638                 /* Finalise indirection table. */
2639                 k = i; /* Retain value of i for use in error case. */
2640                 for (j = 0; k != (unsigned int)(1 << wq_n); ++k, ++j)
2641                         wq[k] = wq[j];
2642                 ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
2643                         (priv->sh->ctx,
2644                          &(struct ibv_rwq_ind_table_init_attr){
2645                                 .log_ind_tbl_size = wq_n,
2646                                 .ind_tbl = wq,
2647                                 .comp_mask = 0,
2648                         });
2649                 if (!ind_tbl->ind_table) {
2650                         rte_errno = errno;
2651                         goto error;
2652                 }
2653         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2654                 struct mlx5_devx_rqt_attr *rqt_attr = NULL;
2655                 const unsigned int rqt_n =
2656                         1 << (rte_is_power_of_2(queues_n) ?
2657                               log2above(queues_n) :
2658                               log2above(priv->config.ind_table_max_size));
2659
2660                 rqt_attr = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rqt_attr) +
2661                                       rqt_n * sizeof(uint32_t), 0,
2662                                       SOCKET_ID_ANY);
2663                 if (!rqt_attr) {
2664                         DRV_LOG(ERR, "port %u cannot allocate RQT resources",
2665                                 dev->data->port_id);
2666                         rte_errno = ENOMEM;
2667                         goto error;
2668                 }
2669                 rqt_attr->rqt_max_size = priv->config.ind_table_max_size;
2670                 rqt_attr->rqt_actual_size = rqt_n;
2671                 for (i = 0; i != queues_n; ++i) {
2672                         struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
2673                                                                  queues[i]);
2674                         if (!rxq)
2675                                 goto error;
2676                         rqt_attr->rq_list[i] = rxq->obj->rq->id;
2677                         ind_tbl->queues[i] = queues[i];
2678                 }
2679                 k = i; /* Retain value of i for use in error case. */
2680                 for (j = 0; k != rqt_n; ++k, ++j)
2681                         rqt_attr->rq_list[k] = rqt_attr->rq_list[j];
2682                 ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->ctx,
2683                                                         rqt_attr);
2684                 mlx5_free(rqt_attr);
2685                 if (!ind_tbl->rqt) {
2686                         DRV_LOG(ERR, "port %u cannot create DevX RQT",
2687                                 dev->data->port_id);
2688                         rte_errno = errno;
2689                         goto error;
2690                 }
2691                 ind_tbl->queues_n = queues_n;
2692         }
2693         rte_atomic32_inc(&ind_tbl->refcnt);
2694         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
2695         return ind_tbl;
2696 error:
2697         for (j = 0; j < i; j++)
2698                 mlx5_rxq_release(dev, ind_tbl->queues[j]);
2699         mlx5_free(ind_tbl);
2700         DEBUG("port %u cannot create indirection table", dev->data->port_id);
2701         return NULL;
2702 }
2703
2704 /**
2705  * Get an indirection table.
2706  *
2707  * @param dev
2708  *   Pointer to Ethernet device.
2709  * @param queues
2710  *   Queues entering in the indirection table.
2711  * @param queues_n
2712  *   Number of queues in the array.
2713  *
2714  * @return
2715  *   An indirection table if found.
2716  */
2717 static struct mlx5_ind_table_obj *
2718 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
2719                        uint32_t queues_n)
2720 {
2721         struct mlx5_priv *priv = dev->data->dev_private;
2722         struct mlx5_ind_table_obj *ind_tbl;
2723
2724         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2725                 if ((ind_tbl->queues_n == queues_n) &&
2726                     (memcmp(ind_tbl->queues, queues,
2727                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
2728                      == 0))
2729                         break;
2730         }
2731         if (ind_tbl) {
2732                 unsigned int i;
2733
2734                 rte_atomic32_inc(&ind_tbl->refcnt);
2735                 for (i = 0; i != ind_tbl->queues_n; ++i)
2736                         mlx5_rxq_get(dev, ind_tbl->queues[i]);
2737         }
2738         return ind_tbl;
2739 }
2740
2741 /**
2742  * Release an indirection table.
2743  *
2744  * @param dev
2745  *   Pointer to Ethernet device.
2746  * @param ind_table
2747  *   Indirection table to release.
2748  *
2749  * @return
2750  *   1 while a reference on it exists, 0 when freed.
2751  */
2752 static int
2753 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
2754                            struct mlx5_ind_table_obj *ind_tbl)
2755 {
2756         unsigned int i;
2757
2758         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt)) {
2759                 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV)
2760                         claim_zero(mlx5_glue->destroy_rwq_ind_table
2761                                                         (ind_tbl->ind_table));
2762                 else if (ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX)
2763                         claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
2764         }
2765         for (i = 0; i != ind_tbl->queues_n; ++i)
2766                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
2767         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
2768                 LIST_REMOVE(ind_tbl, next);
2769                 mlx5_free(ind_tbl);
2770                 return 0;
2771         }
2772         return 1;
2773 }
2774
2775 /**
2776  * Verify the Rx Queue list is empty
2777  *
2778  * @param dev
2779  *   Pointer to Ethernet device.
2780  *
2781  * @return
2782  *   The number of object not released.
2783  */
2784 int
2785 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
2786 {
2787         struct mlx5_priv *priv = dev->data->dev_private;
2788         struct mlx5_ind_table_obj *ind_tbl;
2789         int ret = 0;
2790
2791         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2792                 DRV_LOG(DEBUG,
2793                         "port %u indirection table obj %p still referenced",
2794                         dev->data->port_id, (void *)ind_tbl);
2795                 ++ret;
2796         }
2797         return ret;
2798 }
2799
2800 /**
2801  * Create an Rx Hash queue.
2802  *
2803  * @param dev
2804  *   Pointer to Ethernet device.
2805  * @param rss_key
2806  *   RSS key for the Rx hash queue.
2807  * @param rss_key_len
2808  *   RSS key length.
2809  * @param hash_fields
2810  *   Verbs protocol hash field to make the RSS on.
2811  * @param queues
2812  *   Queues entering in hash queue. In case of empty hash_fields only the
2813  *   first queue index will be taken for the indirection table.
2814  * @param queues_n
2815  *   Number of queues.
2816  * @param tunnel
2817  *   Tunnel type.
2818  *
2819  * @return
2820  *   The Verbs/DevX object initialised index, 0 otherwise and rte_errno is set.
2821  */
2822 uint32_t
2823 mlx5_hrxq_new(struct rte_eth_dev *dev,
2824               const uint8_t *rss_key, uint32_t rss_key_len,
2825               uint64_t hash_fields,
2826               const uint16_t *queues, uint32_t queues_n,
2827               int tunnel __rte_unused)
2828 {
2829         struct mlx5_priv *priv = dev->data->dev_private;
2830         struct mlx5_hrxq *hrxq = NULL;
2831         uint32_t hrxq_idx = 0;
2832         struct ibv_qp *qp = NULL;
2833         struct mlx5_ind_table_obj *ind_tbl;
2834         int err;
2835         struct mlx5_devx_obj *tir = NULL;
2836         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[queues[0]];
2837         struct mlx5_rxq_ctrl *rxq_ctrl =
2838                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2839
2840         queues_n = hash_fields ? queues_n : 1;
2841         ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2842         if (!ind_tbl) {
2843                 enum mlx5_ind_tbl_type type;
2844
2845                 type = rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV ?
2846                                 MLX5_IND_TBL_TYPE_IBV : MLX5_IND_TBL_TYPE_DEVX;
2847                 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n, type);
2848         }
2849         if (!ind_tbl) {
2850                 rte_errno = ENOMEM;
2851                 return 0;
2852         }
2853         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2854 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2855                 struct mlx5dv_qp_init_attr qp_init_attr;
2856
2857                 memset(&qp_init_attr, 0, sizeof(qp_init_attr));
2858                 if (tunnel) {
2859                         qp_init_attr.comp_mask =
2860                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2861                         qp_init_attr.create_flags =
2862                                 MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
2863                 }
2864 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2865                 if (dev->data->dev_conf.lpbk_mode) {
2866                         /*
2867                          * Allow packet sent from NIC loop back
2868                          * w/o source MAC check.
2869                          */
2870                         qp_init_attr.comp_mask |=
2871                                 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2872                         qp_init_attr.create_flags |=
2873                                 MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
2874                 }
2875 #endif
2876                 qp = mlx5_glue->dv_create_qp
2877                         (priv->sh->ctx,
2878                          &(struct ibv_qp_init_attr_ex){
2879                                 .qp_type = IBV_QPT_RAW_PACKET,
2880                                 .comp_mask =
2881                                         IBV_QP_INIT_ATTR_PD |
2882                                         IBV_QP_INIT_ATTR_IND_TABLE |
2883                                         IBV_QP_INIT_ATTR_RX_HASH,
2884                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2885                                         .rx_hash_function =
2886                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2887                                         .rx_hash_key_len = rss_key_len,
2888                                         .rx_hash_key =
2889                                                 (void *)(uintptr_t)rss_key,
2890                                         .rx_hash_fields_mask = hash_fields,
2891                                 },
2892                                 .rwq_ind_tbl = ind_tbl->ind_table,
2893                                 .pd = priv->sh->pd,
2894                           },
2895                           &qp_init_attr);
2896 #else
2897                 qp = mlx5_glue->create_qp_ex
2898                         (priv->sh->ctx,
2899                          &(struct ibv_qp_init_attr_ex){
2900                                 .qp_type = IBV_QPT_RAW_PACKET,
2901                                 .comp_mask =
2902                                         IBV_QP_INIT_ATTR_PD |
2903                                         IBV_QP_INIT_ATTR_IND_TABLE |
2904                                         IBV_QP_INIT_ATTR_RX_HASH,
2905                                 .rx_hash_conf = (struct ibv_rx_hash_conf){
2906                                         .rx_hash_function =
2907                                                 IBV_RX_HASH_FUNC_TOEPLITZ,
2908                                         .rx_hash_key_len = rss_key_len,
2909                                         .rx_hash_key =
2910                                                 (void *)(uintptr_t)rss_key,
2911                                         .rx_hash_fields_mask = hash_fields,
2912                                 },
2913                                 .rwq_ind_tbl = ind_tbl->ind_table,
2914                                 .pd = priv->sh->pd,
2915                          });
2916 #endif
2917                 if (!qp) {
2918                         rte_errno = errno;
2919                         goto error;
2920                 }
2921         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2922                 struct mlx5_devx_tir_attr tir_attr;
2923                 uint32_t i;
2924                 uint32_t lro = 1;
2925
2926                 /* Enable TIR LRO only if all the queues were configured for. */
2927                 for (i = 0; i < queues_n; ++i) {
2928                         if (!(*priv->rxqs)[queues[i]]->lro) {
2929                                 lro = 0;
2930                                 break;
2931                         }
2932                 }
2933                 memset(&tir_attr, 0, sizeof(tir_attr));
2934                 tir_attr.disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
2935                 tir_attr.rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
2936                 tir_attr.tunneled_offload_en = !!tunnel;
2937                 /* If needed, translate hash_fields bitmap to PRM format. */
2938                 if (hash_fields) {
2939 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2940                         struct mlx5_rx_hash_field_select *rx_hash_field_select =
2941                                         hash_fields & IBV_RX_HASH_INNER ?
2942                                         &tir_attr.rx_hash_field_selector_inner :
2943                                         &tir_attr.rx_hash_field_selector_outer;
2944 #else
2945                         struct mlx5_rx_hash_field_select *rx_hash_field_select =
2946                                         &tir_attr.rx_hash_field_selector_outer;
2947 #endif
2948
2949                         /* 1 bit: 0: IPv4, 1: IPv6. */
2950                         rx_hash_field_select->l3_prot_type =
2951                                 !!(hash_fields & MLX5_IPV6_IBV_RX_HASH);
2952                         /* 1 bit: 0: TCP, 1: UDP. */
2953                         rx_hash_field_select->l4_prot_type =
2954                                 !!(hash_fields & MLX5_UDP_IBV_RX_HASH);
2955                         /* Bitmask which sets which fields to use in RX Hash. */
2956                         rx_hash_field_select->selected_fields =
2957                         ((!!(hash_fields & MLX5_L3_SRC_IBV_RX_HASH)) <<
2958                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_SRC_IP) |
2959                         (!!(hash_fields & MLX5_L3_DST_IBV_RX_HASH)) <<
2960                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_DST_IP |
2961                         (!!(hash_fields & MLX5_L4_SRC_IBV_RX_HASH)) <<
2962                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_SPORT |
2963                         (!!(hash_fields & MLX5_L4_DST_IBV_RX_HASH)) <<
2964                          MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_DPORT;
2965                 }
2966                 if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN)
2967                         tir_attr.transport_domain = priv->sh->td->id;
2968                 else
2969                         tir_attr.transport_domain = priv->sh->tdn;
2970                 memcpy(tir_attr.rx_hash_toeplitz_key, rss_key,
2971                        MLX5_RSS_HASH_KEY_LEN);
2972                 tir_attr.indirect_table = ind_tbl->rqt->id;
2973                 if (dev->data->dev_conf.lpbk_mode)
2974                         tir_attr.self_lb_block =
2975                                         MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
2976                 if (lro) {
2977                         tir_attr.lro_timeout_period_usecs =
2978                                         priv->config.lro.timeout;
2979                         tir_attr.lro_max_msg_sz = priv->max_lro_msg_size;
2980                         tir_attr.lro_enable_mask =
2981                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2982                                         MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO;
2983                 }
2984                 tir = mlx5_devx_cmd_create_tir(priv->sh->ctx, &tir_attr);
2985                 if (!tir) {
2986                         DRV_LOG(ERR, "port %u cannot create DevX TIR",
2987                                 dev->data->port_id);
2988                         rte_errno = errno;
2989                         goto error;
2990                 }
2991         }
2992         hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
2993         if (!hrxq)
2994                 goto error;
2995         hrxq->ind_table = ind_tbl;
2996         if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2997                 hrxq->qp = qp;
2998 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2999                 hrxq->action =
3000                         mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
3001                 if (!hrxq->action) {
3002                         rte_errno = errno;
3003                         goto error;
3004                 }
3005 #endif
3006         } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
3007                 hrxq->tir = tir;
3008 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3009                 hrxq->action = mlx5_glue->dv_create_flow_action_dest_devx_tir
3010                                                         (hrxq->tir->obj);
3011                 if (!hrxq->action) {
3012                         rte_errno = errno;
3013                         goto error;
3014                 }
3015 #endif
3016         }
3017         hrxq->rss_key_len = rss_key_len;
3018         hrxq->hash_fields = hash_fields;
3019         memcpy(hrxq->rss_key, rss_key, rss_key_len);
3020         rte_atomic32_inc(&hrxq->refcnt);
3021         ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs, hrxq_idx,
3022                      hrxq, next);
3023         return hrxq_idx;
3024 error:
3025         err = rte_errno; /* Save rte_errno before cleanup. */
3026         mlx5_ind_table_obj_release(dev, ind_tbl);
3027         if (qp)
3028                 claim_zero(mlx5_glue->destroy_qp(qp));
3029         else if (tir)
3030                 claim_zero(mlx5_devx_cmd_destroy(tir));
3031         if (hrxq)
3032                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
3033         rte_errno = err; /* Restore rte_errno. */
3034         return 0;
3035 }
3036
3037 /**
3038  * Get an Rx Hash queue.
3039  *
3040  * @param dev
3041  *   Pointer to Ethernet device.
3042  * @param rss_conf
3043  *   RSS configuration for the Rx hash queue.
3044  * @param queues
3045  *   Queues entering in hash queue. In case of empty hash_fields only the
3046  *   first queue index will be taken for the indirection table.
3047  * @param queues_n
3048  *   Number of queues.
3049  *
3050  * @return
3051  *   An hash Rx queue index on success.
3052  */
3053 uint32_t
3054 mlx5_hrxq_get(struct rte_eth_dev *dev,
3055               const uint8_t *rss_key, uint32_t rss_key_len,
3056               uint64_t hash_fields,
3057               const uint16_t *queues, uint32_t queues_n)
3058 {
3059         struct mlx5_priv *priv = dev->data->dev_private;
3060         struct mlx5_hrxq *hrxq;
3061         uint32_t idx;
3062
3063         queues_n = hash_fields ? queues_n : 1;
3064         ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
3065                       hrxq, next) {
3066                 struct mlx5_ind_table_obj *ind_tbl;
3067
3068                 if (hrxq->rss_key_len != rss_key_len)
3069                         continue;
3070                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
3071                         continue;
3072                 if (hrxq->hash_fields != hash_fields)
3073                         continue;
3074                 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
3075                 if (!ind_tbl)
3076                         continue;
3077                 if (ind_tbl != hrxq->ind_table) {
3078                         mlx5_ind_table_obj_release(dev, ind_tbl);
3079                         continue;
3080                 }
3081                 rte_atomic32_inc(&hrxq->refcnt);
3082                 return idx;
3083         }
3084         return 0;
3085 }
3086
3087 /**
3088  * Release the hash Rx queue.
3089  *
3090  * @param dev
3091  *   Pointer to Ethernet device.
3092  * @param hrxq
3093  *   Index to Hash Rx queue to release.
3094  *
3095  * @return
3096  *   1 while a reference on it exists, 0 when freed.
3097  */
3098 int
3099 mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hrxq_idx)
3100 {
3101         struct mlx5_priv *priv = dev->data->dev_private;
3102         struct mlx5_hrxq *hrxq;
3103
3104         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
3105         if (!hrxq)
3106                 return 0;
3107         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
3108 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3109                 mlx5_glue->destroy_flow_action(hrxq->action);
3110 #endif
3111                 if (hrxq->ind_table->type == MLX5_IND_TBL_TYPE_IBV)
3112                         claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3113                 else /* hrxq->ind_table->type == MLX5_IND_TBL_TYPE_DEVX */
3114                         claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
3115                 mlx5_ind_table_obj_release(dev, hrxq->ind_table);
3116                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs,
3117                              hrxq_idx, hrxq, next);
3118                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
3119                 return 0;
3120         }
3121         claim_nonzero(mlx5_ind_table_obj_release(dev, hrxq->ind_table));
3122         return 1;
3123 }
3124
3125 /**
3126  * Verify the Rx Queue list is empty
3127  *
3128  * @param dev
3129  *   Pointer to Ethernet device.
3130  *
3131  * @return
3132  *   The number of object not released.
3133  */
3134 int
3135 mlx5_hrxq_verify(struct rte_eth_dev *dev)
3136 {
3137         struct mlx5_priv *priv = dev->data->dev_private;
3138         struct mlx5_hrxq *hrxq;
3139         uint32_t idx;
3140         int ret = 0;
3141
3142         ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
3143                       hrxq, next) {
3144                 DRV_LOG(DEBUG,
3145                         "port %u hash Rx queue %p still referenced",
3146                         dev->data->port_id, (void *)hrxq);
3147                 ++ret;
3148         }
3149         return ret;
3150 }
3151
3152 /**
3153  * Create a drop Rx queue Verbs/DevX object.
3154  *
3155  * @param dev
3156  *   Pointer to Ethernet device.
3157  *
3158  * @return
3159  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3160  */
3161 static struct mlx5_rxq_obj *
3162 mlx5_rxq_obj_drop_new(struct rte_eth_dev *dev)
3163 {
3164         struct mlx5_priv *priv = dev->data->dev_private;
3165         struct ibv_context *ctx = priv->sh->ctx;
3166         struct ibv_cq *cq;
3167         struct ibv_wq *wq = NULL;
3168         struct mlx5_rxq_obj *rxq;
3169
3170         if (priv->drop_queue.rxq)
3171                 return priv->drop_queue.rxq;
3172         cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
3173         if (!cq) {
3174                 DEBUG("port %u cannot allocate CQ for drop queue",
3175                       dev->data->port_id);
3176                 rte_errno = errno;
3177                 goto error;
3178         }
3179         wq = mlx5_glue->create_wq(ctx,
3180                  &(struct ibv_wq_init_attr){
3181                         .wq_type = IBV_WQT_RQ,
3182                         .max_wr = 1,
3183                         .max_sge = 1,
3184                         .pd = priv->sh->pd,
3185                         .cq = cq,
3186                  });
3187         if (!wq) {
3188                 DEBUG("port %u cannot allocate WQ for drop queue",
3189                       dev->data->port_id);
3190                 rte_errno = errno;
3191                 goto error;
3192         }
3193         rxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq), 0, SOCKET_ID_ANY);
3194         if (!rxq) {
3195                 DEBUG("port %u cannot allocate drop Rx queue memory",
3196                       dev->data->port_id);
3197                 rte_errno = ENOMEM;
3198                 goto error;
3199         }
3200         rxq->ibv_cq = cq;
3201         rxq->wq = wq;
3202         priv->drop_queue.rxq = rxq;
3203         return rxq;
3204 error:
3205         if (wq)
3206                 claim_zero(mlx5_glue->destroy_wq(wq));
3207         if (cq)
3208                 claim_zero(mlx5_glue->destroy_cq(cq));
3209         return NULL;
3210 }
3211
3212 /**
3213  * Release a drop Rx queue Verbs/DevX object.
3214  *
3215  * @param dev
3216  *   Pointer to Ethernet device.
3217  *
3218  * @return
3219  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3220  */
3221 static void
3222 mlx5_rxq_obj_drop_release(struct rte_eth_dev *dev)
3223 {
3224         struct mlx5_priv *priv = dev->data->dev_private;
3225         struct mlx5_rxq_obj *rxq = priv->drop_queue.rxq;
3226
3227         if (rxq->wq)
3228                 claim_zero(mlx5_glue->destroy_wq(rxq->wq));
3229         if (rxq->ibv_cq)
3230                 claim_zero(mlx5_glue->destroy_cq(rxq->ibv_cq));
3231         mlx5_free(rxq);
3232         priv->drop_queue.rxq = NULL;
3233 }
3234
3235 /**
3236  * Create a drop indirection table.
3237  *
3238  * @param dev
3239  *   Pointer to Ethernet device.
3240  *
3241  * @return
3242  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3243  */
3244 static struct mlx5_ind_table_obj *
3245 mlx5_ind_table_obj_drop_new(struct rte_eth_dev *dev)
3246 {
3247         struct mlx5_priv *priv = dev->data->dev_private;
3248         struct mlx5_ind_table_obj *ind_tbl;
3249         struct mlx5_rxq_obj *rxq;
3250         struct mlx5_ind_table_obj tmpl;
3251
3252         rxq = mlx5_rxq_obj_drop_new(dev);
3253         if (!rxq)
3254                 return NULL;
3255         tmpl.ind_table = mlx5_glue->create_rwq_ind_table
3256                 (priv->sh->ctx,
3257                  &(struct ibv_rwq_ind_table_init_attr){
3258                         .log_ind_tbl_size = 0,
3259                         .ind_tbl = (struct ibv_wq **)&rxq->wq,
3260                         .comp_mask = 0,
3261                  });
3262         if (!tmpl.ind_table) {
3263                 DEBUG("port %u cannot allocate indirection table for drop"
3264                       " queue",
3265                       dev->data->port_id);
3266                 rte_errno = errno;
3267                 goto error;
3268         }
3269         ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl), 0,
3270                               SOCKET_ID_ANY);
3271         if (!ind_tbl) {
3272                 rte_errno = ENOMEM;
3273                 goto error;
3274         }
3275         ind_tbl->ind_table = tmpl.ind_table;
3276         return ind_tbl;
3277 error:
3278         mlx5_rxq_obj_drop_release(dev);
3279         return NULL;
3280 }
3281
3282 /**
3283  * Release a drop indirection table.
3284  *
3285  * @param dev
3286  *   Pointer to Ethernet device.
3287  */
3288 static void
3289 mlx5_ind_table_obj_drop_release(struct rte_eth_dev *dev)
3290 {
3291         struct mlx5_priv *priv = dev->data->dev_private;
3292         struct mlx5_ind_table_obj *ind_tbl = priv->drop_queue.hrxq->ind_table;
3293
3294         claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
3295         mlx5_rxq_obj_drop_release(dev);
3296         mlx5_free(ind_tbl);
3297         priv->drop_queue.hrxq->ind_table = NULL;
3298 }
3299
3300 /**
3301  * Create a drop Rx Hash queue.
3302  *
3303  * @param dev
3304  *   Pointer to Ethernet device.
3305  *
3306  * @return
3307  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
3308  */
3309 struct mlx5_hrxq *
3310 mlx5_hrxq_drop_new(struct rte_eth_dev *dev)
3311 {
3312         struct mlx5_priv *priv = dev->data->dev_private;
3313         struct mlx5_ind_table_obj *ind_tbl = NULL;
3314         struct ibv_qp *qp = NULL;
3315         struct mlx5_hrxq *hrxq = NULL;
3316
3317         if (priv->drop_queue.hrxq) {
3318                 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
3319                 return priv->drop_queue.hrxq;
3320         }
3321         hrxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq), 0, SOCKET_ID_ANY);
3322         if (!hrxq) {
3323                 DRV_LOG(WARNING,
3324                         "port %u cannot allocate memory for drop queue",
3325                         dev->data->port_id);
3326                 rte_errno = ENOMEM;
3327                 goto error;
3328         }
3329         priv->drop_queue.hrxq = hrxq;
3330         ind_tbl = mlx5_ind_table_obj_drop_new(dev);
3331         if (!ind_tbl)
3332                 goto error;
3333         hrxq->ind_table = ind_tbl;
3334         qp = mlx5_glue->create_qp_ex(priv->sh->ctx,
3335                  &(struct ibv_qp_init_attr_ex){
3336                         .qp_type = IBV_QPT_RAW_PACKET,
3337                         .comp_mask =
3338                                 IBV_QP_INIT_ATTR_PD |
3339                                 IBV_QP_INIT_ATTR_IND_TABLE |
3340                                 IBV_QP_INIT_ATTR_RX_HASH,
3341                         .rx_hash_conf = (struct ibv_rx_hash_conf){
3342                                 .rx_hash_function =
3343                                         IBV_RX_HASH_FUNC_TOEPLITZ,
3344                                 .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
3345                                 .rx_hash_key = rss_hash_default_key,
3346                                 .rx_hash_fields_mask = 0,
3347                                 },
3348                         .rwq_ind_tbl = ind_tbl->ind_table,
3349                         .pd = priv->sh->pd
3350                  });
3351         if (!qp) {
3352                 DEBUG("port %u cannot allocate QP for drop queue",
3353                       dev->data->port_id);
3354                 rte_errno = errno;
3355                 goto error;
3356         }
3357         hrxq->qp = qp;
3358 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3359         hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
3360         if (!hrxq->action) {
3361                 rte_errno = errno;
3362                 goto error;
3363         }
3364 #endif
3365         rte_atomic32_set(&hrxq->refcnt, 1);
3366         return hrxq;
3367 error:
3368 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3369         if (hrxq && hrxq->action)
3370                 mlx5_glue->destroy_flow_action(hrxq->action);
3371 #endif
3372         if (qp)
3373                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3374         if (ind_tbl)
3375                 mlx5_ind_table_obj_drop_release(dev);
3376         if (hrxq) {
3377                 priv->drop_queue.hrxq = NULL;
3378                 mlx5_free(hrxq);
3379         }
3380         return NULL;
3381 }
3382
3383 /**
3384  * Release a drop hash Rx queue.
3385  *
3386  * @param dev
3387  *   Pointer to Ethernet device.
3388  */
3389 void
3390 mlx5_hrxq_drop_release(struct rte_eth_dev *dev)
3391 {
3392         struct mlx5_priv *priv = dev->data->dev_private;
3393         struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
3394
3395         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
3396 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
3397                 mlx5_glue->destroy_flow_action(hrxq->action);
3398 #endif
3399                 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
3400                 mlx5_ind_table_obj_drop_release(dev);
3401                 mlx5_free(hrxq);
3402                 priv->drop_queue.hrxq = NULL;
3403         }
3404 }
3405
3406
3407 /**
3408  * Set the Rx queue timestamp conversion parameters
3409  *
3410  * @param[in] dev
3411  *   Pointer to the Ethernet device structure.
3412  */
3413 void
3414 mlx5_rxq_timestamp_set(struct rte_eth_dev *dev)
3415 {
3416         struct mlx5_priv *priv = dev->data->dev_private;
3417         struct mlx5_dev_ctx_shared *sh = priv->sh;
3418         struct mlx5_rxq_data *data;
3419         unsigned int i;
3420
3421         for (i = 0; i != priv->rxqs_n; ++i) {
3422                 if (!(*priv->rxqs)[i])
3423                         continue;
3424                 data = (*priv->rxqs)[i];
3425                 data->sh = sh;
3426                 data->rt_timestamp = priv->config.rt_timestamp;
3427         }
3428 }