vhost: mark vDPA driver API as internal
[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 <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_malloc.h>
24 #include <mlx5_common_mr.h>
25
26 #include "mlx5_defs.h"
27 #include "mlx5.h"
28 #include "mlx5_tx.h"
29 #include "mlx5_rx.h"
30 #include "mlx5_utils.h"
31 #include "mlx5_autoconf.h"
32
33
34 /* Default RSS hash key also used for ConnectX-3. */
35 uint8_t rss_hash_default_key[] = {
36         0x2c, 0xc6, 0x81, 0xd1,
37         0x5b, 0xdb, 0xf4, 0xf7,
38         0xfc, 0xa2, 0x83, 0x19,
39         0xdb, 0x1a, 0x3e, 0x94,
40         0x6b, 0x9e, 0x38, 0xd9,
41         0x2c, 0x9c, 0x03, 0xd1,
42         0xad, 0x99, 0x44, 0xa7,
43         0xd9, 0x56, 0x3d, 0x59,
44         0x06, 0x3c, 0x25, 0xf3,
45         0xfc, 0x1f, 0xdc, 0x2a,
46 };
47
48 /* Length of the default RSS hash key. */
49 static_assert(MLX5_RSS_HASH_KEY_LEN ==
50               (unsigned int)sizeof(rss_hash_default_key),
51               "wrong RSS default key size.");
52
53 /**
54  * Calculate the number of CQEs in CQ for the Rx queue.
55  *
56  *  @param rxq_data
57  *     Pointer to receive queue structure.
58  *
59  * @return
60  *   Number of CQEs in CQ.
61  */
62 unsigned int
63 mlx5_rxq_cqe_num(struct mlx5_rxq_data *rxq_data)
64 {
65         unsigned int cqe_n;
66         unsigned int wqe_n = 1 << rxq_data->elts_n;
67
68         if (mlx5_rxq_mprq_enabled(rxq_data))
69                 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
70         else
71                 cqe_n = wqe_n - 1;
72         return cqe_n;
73 }
74
75 /**
76  * Allocate RX queue elements for Multi-Packet RQ.
77  *
78  * @param rxq_ctrl
79  *   Pointer to RX queue structure.
80  *
81  * @return
82  *   0 on success, a negative errno value otherwise and rte_errno is set.
83  */
84 static int
85 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
86 {
87         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
88         unsigned int wqe_n = 1 << rxq->elts_n;
89         unsigned int i;
90         int err;
91
92         /* Iterate on segments. */
93         for (i = 0; i <= wqe_n; ++i) {
94                 struct mlx5_mprq_buf *buf;
95
96                 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
97                         DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
98                         rte_errno = ENOMEM;
99                         goto error;
100                 }
101                 if (i < wqe_n)
102                         (*rxq->mprq_bufs)[i] = buf;
103                 else
104                         rxq->mprq_repl = buf;
105         }
106         DRV_LOG(DEBUG,
107                 "port %u MPRQ queue %u allocated and configured %u segments",
108                 rxq->port_id, rxq->idx, wqe_n);
109         return 0;
110 error:
111         err = rte_errno; /* Save rte_errno before cleanup. */
112         wqe_n = i;
113         for (i = 0; (i != wqe_n); ++i) {
114                 if ((*rxq->mprq_bufs)[i] != NULL)
115                         rte_mempool_put(rxq->mprq_mp,
116                                         (*rxq->mprq_bufs)[i]);
117                 (*rxq->mprq_bufs)[i] = NULL;
118         }
119         DRV_LOG(DEBUG, "port %u MPRQ queue %u failed, freed everything",
120                 rxq->port_id, rxq->idx);
121         rte_errno = err; /* Restore rte_errno. */
122         return -rte_errno;
123 }
124
125 /**
126  * Allocate RX queue elements for Single-Packet RQ.
127  *
128  * @param rxq_ctrl
129  *   Pointer to RX queue structure.
130  *
131  * @return
132  *   0 on success, errno value on failure.
133  */
134 static int
135 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
136 {
137         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
138         unsigned int elts_n = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
139                 (1 << rxq_ctrl->rxq.elts_n) * (1 << rxq_ctrl->rxq.strd_num_n) :
140                 (1 << rxq_ctrl->rxq.elts_n);
141         unsigned int i;
142         int err;
143
144         /* Iterate on segments. */
145         for (i = 0; (i != elts_n); ++i) {
146                 struct mlx5_eth_rxseg *seg = &rxq_ctrl->rxq.rxseg[i % sges_n];
147                 struct rte_mbuf *buf;
148
149                 buf = rte_pktmbuf_alloc(seg->mp);
150                 if (buf == NULL) {
151                         DRV_LOG(ERR, "port %u empty mbuf pool",
152                                 PORT_ID(rxq_ctrl->priv));
153                         rte_errno = ENOMEM;
154                         goto error;
155                 }
156                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
157                 MLX5_ASSERT(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
158                 /* Buffer is supposed to be empty. */
159                 MLX5_ASSERT(rte_pktmbuf_data_len(buf) == 0);
160                 MLX5_ASSERT(rte_pktmbuf_pkt_len(buf) == 0);
161                 MLX5_ASSERT(!buf->next);
162                 SET_DATA_OFF(buf, seg->offset);
163                 PORT(buf) = rxq_ctrl->rxq.port_id;
164                 DATA_LEN(buf) = seg->length;
165                 PKT_LEN(buf) = seg->length;
166                 NB_SEGS(buf) = 1;
167                 (*rxq_ctrl->rxq.elts)[i] = buf;
168         }
169         /* If Rx vector is activated. */
170         if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
171                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
172                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
173                 struct rte_pktmbuf_pool_private *priv =
174                         (struct rte_pktmbuf_pool_private *)
175                                 rte_mempool_get_priv(rxq_ctrl->rxq.mp);
176                 int j;
177
178                 /* Initialize default rearm_data for vPMD. */
179                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
180                 rte_mbuf_refcnt_set(mbuf_init, 1);
181                 mbuf_init->nb_segs = 1;
182                 mbuf_init->port = rxq->port_id;
183                 if (priv->flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
184                         mbuf_init->ol_flags = RTE_MBUF_F_EXTERNAL;
185                 /*
186                  * prevent compiler reordering:
187                  * rearm_data covers previous fields.
188                  */
189                 rte_compiler_barrier();
190                 rxq->mbuf_initializer =
191                         *(rte_xmm_t *)&mbuf_init->rearm_data;
192                 /* Padding with a fake mbuf for vectorized Rx. */
193                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
194                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
195         }
196         DRV_LOG(DEBUG,
197                 "port %u SPRQ queue %u allocated and configured %u segments"
198                 " (max %u packets)",
199                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx, elts_n,
200                 elts_n / (1 << rxq_ctrl->rxq.sges_n));
201         return 0;
202 error:
203         err = rte_errno; /* Save rte_errno before cleanup. */
204         elts_n = i;
205         for (i = 0; (i != elts_n); ++i) {
206                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
207                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
208                 (*rxq_ctrl->rxq.elts)[i] = NULL;
209         }
210         DRV_LOG(DEBUG, "port %u SPRQ queue %u failed, freed everything",
211                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx);
212         rte_errno = err; /* Restore rte_errno. */
213         return -rte_errno;
214 }
215
216 /**
217  * Allocate RX queue elements.
218  *
219  * @param rxq_ctrl
220  *   Pointer to RX queue structure.
221  *
222  * @return
223  *   0 on success, errno value on failure.
224  */
225 int
226 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
227 {
228         int ret = 0;
229
230         /**
231          * For MPRQ we need to allocate both MPRQ buffers
232          * for WQEs and simple mbufs for vector processing.
233          */
234         if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
235                 ret = rxq_alloc_elts_mprq(rxq_ctrl);
236         return (ret || rxq_alloc_elts_sprq(rxq_ctrl));
237 }
238
239 /**
240  * Free RX queue elements for Multi-Packet RQ.
241  *
242  * @param rxq_ctrl
243  *   Pointer to RX queue structure.
244  */
245 static void
246 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
247 {
248         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
249         uint16_t i;
250
251         DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing %d WRs",
252                 rxq->port_id, rxq->idx, (1u << rxq->elts_n));
253         if (rxq->mprq_bufs == NULL)
254                 return;
255         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
256                 if ((*rxq->mprq_bufs)[i] != NULL)
257                         mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
258                 (*rxq->mprq_bufs)[i] = NULL;
259         }
260         if (rxq->mprq_repl != NULL) {
261                 mlx5_mprq_buf_free(rxq->mprq_repl);
262                 rxq->mprq_repl = NULL;
263         }
264 }
265
266 /**
267  * Free RX queue elements for Single-Packet RQ.
268  *
269  * @param rxq_ctrl
270  *   Pointer to RX queue structure.
271  */
272 static void
273 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
274 {
275         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
276         const uint16_t q_n = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
277                 (1 << rxq->elts_n) * (1 << rxq->strd_num_n) :
278                 (1 << rxq->elts_n);
279         const uint16_t q_mask = q_n - 1;
280         uint16_t elts_ci = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
281                 rxq->elts_ci : rxq->rq_ci;
282         uint16_t used = q_n - (elts_ci - rxq->rq_pi);
283         uint16_t i;
284
285         DRV_LOG(DEBUG, "port %u Rx queue %u freeing %d WRs",
286                 PORT_ID(rxq_ctrl->priv), rxq->idx, q_n);
287         if (rxq->elts == NULL)
288                 return;
289         /**
290          * Some mbuf in the Ring belongs to the application.
291          * They cannot be freed.
292          */
293         if (mlx5_rxq_check_vec_support(rxq) > 0) {
294                 for (i = 0; i < used; ++i)
295                         (*rxq->elts)[(elts_ci + i) & q_mask] = NULL;
296                 rxq->rq_pi = elts_ci;
297         }
298         for (i = 0; i != q_n; ++i) {
299                 if ((*rxq->elts)[i] != NULL)
300                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
301                 (*rxq->elts)[i] = NULL;
302         }
303 }
304
305 /**
306  * Free RX queue elements.
307  *
308  * @param rxq_ctrl
309  *   Pointer to RX queue structure.
310  */
311 static void
312 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
313 {
314         /*
315          * For MPRQ we need to allocate both MPRQ buffers
316          * for WQEs and simple mbufs for vector processing.
317          */
318         if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
319                 rxq_free_elts_mprq(rxq_ctrl);
320         rxq_free_elts_sprq(rxq_ctrl);
321 }
322
323 /**
324  * Returns the per-queue supported offloads.
325  *
326  * @param dev
327  *   Pointer to Ethernet device.
328  *
329  * @return
330  *   Supported Rx offloads.
331  */
332 uint64_t
333 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
334 {
335         struct mlx5_priv *priv = dev->data->dev_private;
336         struct mlx5_dev_config *config = &priv->config;
337         uint64_t offloads = (RTE_ETH_RX_OFFLOAD_SCATTER |
338                              RTE_ETH_RX_OFFLOAD_TIMESTAMP |
339                              RTE_ETH_RX_OFFLOAD_RSS_HASH);
340
341         if (!config->mprq.enabled)
342                 offloads |= RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT;
343         if (config->hw_fcs_strip)
344                 offloads |= RTE_ETH_RX_OFFLOAD_KEEP_CRC;
345         if (config->hw_csum)
346                 offloads |= (RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
347                              RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
348                              RTE_ETH_RX_OFFLOAD_TCP_CKSUM);
349         if (config->hw_vlan_strip)
350                 offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
351         if (MLX5_LRO_SUPPORTED(dev))
352                 offloads |= RTE_ETH_RX_OFFLOAD_TCP_LRO;
353         return offloads;
354 }
355
356
357 /**
358  * Returns the per-port supported offloads.
359  *
360  * @return
361  *   Supported Rx offloads.
362  */
363 uint64_t
364 mlx5_get_rx_port_offloads(void)
365 {
366         uint64_t offloads = RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
367
368         return offloads;
369 }
370
371 /**
372  * Verify if the queue can be released.
373  *
374  * @param dev
375  *   Pointer to Ethernet device.
376  * @param idx
377  *   RX queue index.
378  *
379  * @return
380  *   1 if the queue can be released
381  *   0 if the queue can not be released, there are references to it.
382  *   Negative errno and rte_errno is set if queue doesn't exist.
383  */
384 static int
385 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
386 {
387         struct mlx5_priv *priv = dev->data->dev_private;
388         struct mlx5_rxq_ctrl *rxq_ctrl;
389
390         if (!(*priv->rxqs)[idx]) {
391                 rte_errno = EINVAL;
392                 return -rte_errno;
393         }
394         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
395         return (__atomic_load_n(&rxq_ctrl->refcnt, __ATOMIC_RELAXED) == 1);
396 }
397
398 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
399 static void
400 rxq_sync_cq(struct mlx5_rxq_data *rxq)
401 {
402         const uint16_t cqe_n = 1 << rxq->cqe_n;
403         const uint16_t cqe_mask = cqe_n - 1;
404         volatile struct mlx5_cqe *cqe;
405         int ret, i;
406
407         i = cqe_n;
408         do {
409                 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_mask];
410                 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
411                 if (ret == MLX5_CQE_STATUS_HW_OWN)
412                         break;
413                 if (ret == MLX5_CQE_STATUS_ERR) {
414                         rxq->cq_ci++;
415                         continue;
416                 }
417                 MLX5_ASSERT(ret == MLX5_CQE_STATUS_SW_OWN);
418                 if (MLX5_CQE_FORMAT(cqe->op_own) != MLX5_COMPRESSED) {
419                         rxq->cq_ci++;
420                         continue;
421                 }
422                 /* Compute the next non compressed CQE. */
423                 rxq->cq_ci += rte_be_to_cpu_32(cqe->byte_cnt);
424
425         } while (--i);
426         /* Move all CQEs to HW ownership, including possible MiniCQEs. */
427         for (i = 0; i < cqe_n; i++) {
428                 cqe = &(*rxq->cqes)[i];
429                 cqe->op_own = MLX5_CQE_INVALIDATE;
430         }
431         /* Resync CQE and WQE (WQ in RESET state). */
432         rte_io_wmb();
433         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
434         rte_io_wmb();
435         *rxq->rq_db = rte_cpu_to_be_32(0);
436         rte_io_wmb();
437 }
438
439 /**
440  * Rx queue stop. Device queue goes to the RESET state,
441  * all involved mbufs are freed from WQ.
442  *
443  * @param dev
444  *   Pointer to Ethernet device structure.
445  * @param idx
446  *   RX queue index.
447  *
448  * @return
449  *   0 on success, a negative errno value otherwise and rte_errno is set.
450  */
451 int
452 mlx5_rx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
453 {
454         struct mlx5_priv *priv = dev->data->dev_private;
455         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
456         struct mlx5_rxq_ctrl *rxq_ctrl =
457                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
458         int ret;
459
460         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
461         ret = priv->obj_ops.rxq_obj_modify(rxq_ctrl->obj, MLX5_RXQ_MOD_RDY2RST);
462         if (ret) {
463                 DRV_LOG(ERR, "Cannot change Rx WQ state to RESET:  %s",
464                         strerror(errno));
465                 rte_errno = errno;
466                 return ret;
467         }
468         /* Remove all processes CQEs. */
469         rxq_sync_cq(rxq);
470         /* Free all involved mbufs. */
471         rxq_free_elts(rxq_ctrl);
472         /* Set the actual queue state. */
473         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
474         return 0;
475 }
476
477 /**
478  * Rx queue stop. Device queue goes to the RESET state,
479  * all involved mbufs are freed from WQ.
480  *
481  * @param dev
482  *   Pointer to Ethernet device structure.
483  * @param idx
484  *   RX queue index.
485  *
486  * @return
487  *   0 on success, a negative errno value otherwise and rte_errno is set.
488  */
489 int
490 mlx5_rx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
491 {
492         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
493         int ret;
494
495         if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
496                 DRV_LOG(ERR, "Hairpin queue can't be stopped");
497                 rte_errno = EINVAL;
498                 return -EINVAL;
499         }
500         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
501                 return 0;
502         /*
503          * Vectorized Rx burst requires the CQ and RQ indices
504          * synchronized, that might be broken on RQ restart
505          * and cause Rx malfunction, so queue stopping is
506          * not supported if vectorized Rx burst is engaged.
507          * The routine pointer depends on the process
508          * type, should perform check there.
509          */
510         if (pkt_burst == mlx5_rx_burst_vec) {
511                 DRV_LOG(ERR, "Rx queue stop is not supported "
512                         "for vectorized Rx");
513                 rte_errno = EINVAL;
514                 return -EINVAL;
515         }
516         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
517                 ret = mlx5_mp_os_req_queue_control(dev, idx,
518                                                    MLX5_MP_REQ_QUEUE_RX_STOP);
519         } else {
520                 ret = mlx5_rx_queue_stop_primary(dev, idx);
521         }
522         return ret;
523 }
524
525 /**
526  * Rx queue start. Device queue goes to the ready state,
527  * all required mbufs are allocated and WQ is replenished.
528  *
529  * @param dev
530  *   Pointer to Ethernet device structure.
531  * @param idx
532  *   RX queue index.
533  *
534  * @return
535  *   0 on success, a negative errno value otherwise and rte_errno is set.
536  */
537 int
538 mlx5_rx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
539 {
540         struct mlx5_priv *priv = dev->data->dev_private;
541         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
542         struct mlx5_rxq_ctrl *rxq_ctrl =
543                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
544         int ret;
545
546         MLX5_ASSERT(rte_eal_process_type() ==  RTE_PROC_PRIMARY);
547         /* Allocate needed buffers. */
548         ret = rxq_alloc_elts(rxq_ctrl);
549         if (ret) {
550                 DRV_LOG(ERR, "Cannot reallocate buffers for Rx WQ");
551                 rte_errno = errno;
552                 return ret;
553         }
554         rte_io_wmb();
555         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
556         rte_io_wmb();
557         /* Reset RQ consumer before moving queue to READY state. */
558         *rxq->rq_db = rte_cpu_to_be_32(0);
559         rte_io_wmb();
560         ret = priv->obj_ops.rxq_obj_modify(rxq_ctrl->obj, MLX5_RXQ_MOD_RST2RDY);
561         if (ret) {
562                 DRV_LOG(ERR, "Cannot change Rx WQ state to READY:  %s",
563                         strerror(errno));
564                 rte_errno = errno;
565                 return ret;
566         }
567         /* Reinitialize RQ - set WQEs. */
568         mlx5_rxq_initialize(rxq);
569         rxq->err_state = MLX5_RXQ_ERR_STATE_NO_ERROR;
570         /* Set actual queue state. */
571         dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
572         return 0;
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(struct rte_eth_dev *dev, uint16_t idx)
589 {
590         int ret;
591
592         if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
593                 DRV_LOG(ERR, "Hairpin queue can't be started");
594                 rte_errno = EINVAL;
595                 return -EINVAL;
596         }
597         if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
598                 return 0;
599         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
600                 ret = mlx5_mp_os_req_queue_control(dev, idx,
601                                                    MLX5_MP_REQ_QUEUE_RX_START);
602         } else {
603                 ret = mlx5_rx_queue_start_primary(dev, idx);
604         }
605         return ret;
606 }
607
608 /**
609  * Rx queue presetup checks.
610  *
611  * @param dev
612  *   Pointer to Ethernet device structure.
613  * @param idx
614  *   RX queue index.
615  * @param desc
616  *   Number of descriptors to configure in queue.
617  *
618  * @return
619  *   0 on success, a negative errno value otherwise and rte_errno is set.
620  */
621 static int
622 mlx5_rx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc)
623 {
624         struct mlx5_priv *priv = dev->data->dev_private;
625
626         if (!rte_is_power_of_2(*desc)) {
627                 *desc = 1 << log2above(*desc);
628                 DRV_LOG(WARNING,
629                         "port %u increased number of descriptors in Rx queue %u"
630                         " to the next power of two (%d)",
631                         dev->data->port_id, idx, *desc);
632         }
633         DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
634                 dev->data->port_id, idx, *desc);
635         if (idx >= priv->rxqs_n) {
636                 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
637                         dev->data->port_id, idx, priv->rxqs_n);
638                 rte_errno = EOVERFLOW;
639                 return -rte_errno;
640         }
641         if (!mlx5_rxq_releasable(dev, idx)) {
642                 DRV_LOG(ERR, "port %u unable to release queue index %u",
643                         dev->data->port_id, idx);
644                 rte_errno = EBUSY;
645                 return -rte_errno;
646         }
647         mlx5_rxq_release(dev, idx);
648         return 0;
649 }
650
651 /**
652  *
653  * @param dev
654  *   Pointer to Ethernet device structure.
655  * @param idx
656  *   RX queue index.
657  * @param desc
658  *   Number of descriptors to configure in queue.
659  * @param socket
660  *   NUMA socket on which memory must be allocated.
661  * @param[in] conf
662  *   Thresholds parameters.
663  * @param mp
664  *   Memory pool for buffer allocations.
665  *
666  * @return
667  *   0 on success, a negative errno value otherwise and rte_errno is set.
668  */
669 int
670 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
671                     unsigned int socket, const struct rte_eth_rxconf *conf,
672                     struct rte_mempool *mp)
673 {
674         struct mlx5_priv *priv = dev->data->dev_private;
675         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
676         struct mlx5_rxq_ctrl *rxq_ctrl =
677                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
678         struct rte_eth_rxseg_split *rx_seg =
679                                 (struct rte_eth_rxseg_split *)conf->rx_seg;
680         struct rte_eth_rxseg_split rx_single = {.mp = mp};
681         uint16_t n_seg = conf->rx_nseg;
682         int res;
683
684         if (mp) {
685                 /*
686                  * The parameters should be checked on rte_eth_dev layer.
687                  * If mp is specified it means the compatible configuration
688                  * without buffer split feature tuning.
689                  */
690                 rx_seg = &rx_single;
691                 n_seg = 1;
692         }
693         if (n_seg > 1) {
694                 uint64_t offloads = conf->offloads |
695                                     dev->data->dev_conf.rxmode.offloads;
696
697                 /* The offloads should be checked on rte_eth_dev layer. */
698                 MLX5_ASSERT(offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
699                 if (!(offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT)) {
700                         DRV_LOG(ERR, "port %u queue index %u split "
701                                      "offload not configured",
702                                      dev->data->port_id, idx);
703                         rte_errno = ENOSPC;
704                         return -rte_errno;
705                 }
706                 MLX5_ASSERT(n_seg < MLX5_MAX_RXQ_NSEG);
707         }
708         res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
709         if (res)
710                 return res;
711         rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, rx_seg, n_seg);
712         if (!rxq_ctrl) {
713                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
714                         dev->data->port_id, idx);
715                 rte_errno = ENOMEM;
716                 return -rte_errno;
717         }
718         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
719                 dev->data->port_id, idx);
720         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
721         return 0;
722 }
723
724 /**
725  *
726  * @param dev
727  *   Pointer to Ethernet device structure.
728  * @param idx
729  *   RX queue index.
730  * @param desc
731  *   Number of descriptors to configure in queue.
732  * @param hairpin_conf
733  *   Hairpin configuration parameters.
734  *
735  * @return
736  *   0 on success, a negative errno value otherwise and rte_errno is set.
737  */
738 int
739 mlx5_rx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
740                             uint16_t desc,
741                             const struct rte_eth_hairpin_conf *hairpin_conf)
742 {
743         struct mlx5_priv *priv = dev->data->dev_private;
744         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
745         struct mlx5_rxq_ctrl *rxq_ctrl =
746                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
747         int res;
748
749         res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
750         if (res)
751                 return res;
752         if (hairpin_conf->peer_count != 1) {
753                 rte_errno = EINVAL;
754                 DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue index %u"
755                         " peer count is %u", dev->data->port_id,
756                         idx, hairpin_conf->peer_count);
757                 return -rte_errno;
758         }
759         if (hairpin_conf->peers[0].port == dev->data->port_id) {
760                 if (hairpin_conf->peers[0].queue >= priv->txqs_n) {
761                         rte_errno = EINVAL;
762                         DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue"
763                                 " index %u, Tx %u is larger than %u",
764                                 dev->data->port_id, idx,
765                                 hairpin_conf->peers[0].queue, priv->txqs_n);
766                         return -rte_errno;
767                 }
768         } else {
769                 if (hairpin_conf->manual_bind == 0 ||
770                     hairpin_conf->tx_explicit == 0) {
771                         rte_errno = EINVAL;
772                         DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue"
773                                 " index %u peer port %u with attributes %u %u",
774                                 dev->data->port_id, idx,
775                                 hairpin_conf->peers[0].port,
776                                 hairpin_conf->manual_bind,
777                                 hairpin_conf->tx_explicit);
778                         return -rte_errno;
779                 }
780         }
781         rxq_ctrl = mlx5_rxq_hairpin_new(dev, idx, desc, hairpin_conf);
782         if (!rxq_ctrl) {
783                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
784                         dev->data->port_id, idx);
785                 rte_errno = ENOMEM;
786                 return -rte_errno;
787         }
788         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
789                 dev->data->port_id, idx);
790         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
791         return 0;
792 }
793
794 /**
795  * DPDK callback to release a RX queue.
796  *
797  * @param dev
798  *   Pointer to Ethernet device structure.
799  * @param qid
800  *   Receive queue index.
801  */
802 void
803 mlx5_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
804 {
805         struct mlx5_rxq_data *rxq = dev->data->rx_queues[qid];
806
807         if (rxq == NULL)
808                 return;
809         if (!mlx5_rxq_releasable(dev, qid))
810                 rte_panic("port %u Rx queue %u is still used by a flow and"
811                           " cannot be removed\n", dev->data->port_id, qid);
812         mlx5_rxq_release(dev, qid);
813 }
814
815 /**
816  * Allocate queue vector and fill epoll fd list for Rx interrupts.
817  *
818  * @param dev
819  *   Pointer to Ethernet device.
820  *
821  * @return
822  *   0 on success, a negative errno value otherwise and rte_errno is set.
823  */
824 int
825 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
826 {
827         struct mlx5_priv *priv = dev->data->dev_private;
828         unsigned int i;
829         unsigned int rxqs_n = priv->rxqs_n;
830         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
831         unsigned int count = 0;
832         struct rte_intr_handle *intr_handle = dev->intr_handle;
833
834         if (!dev->data->dev_conf.intr_conf.rxq)
835                 return 0;
836         mlx5_rx_intr_vec_disable(dev);
837         if (rte_intr_vec_list_alloc(intr_handle, NULL, n)) {
838                 DRV_LOG(ERR,
839                         "port %u failed to allocate memory for interrupt"
840                         " vector, Rx interrupts will not be supported",
841                         dev->data->port_id);
842                 rte_errno = ENOMEM;
843                 return -rte_errno;
844         }
845
846         if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_EXT))
847                 return -rte_errno;
848
849         for (i = 0; i != n; ++i) {
850                 /* This rxq obj must not be released in this function. */
851                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
852                 struct mlx5_rxq_obj *rxq_obj = rxq_ctrl ? rxq_ctrl->obj : NULL;
853                 int rc;
854
855                 /* Skip queues that cannot request interrupts. */
856                 if (!rxq_obj || (!rxq_obj->ibv_channel &&
857                                  !rxq_obj->devx_channel)) {
858                         /* Use invalid intr_vec[] index to disable entry. */
859                         if (rte_intr_vec_list_index_set(intr_handle, i,
860                            RTE_INTR_VEC_RXTX_OFFSET + RTE_MAX_RXTX_INTR_VEC_ID))
861                                 return -rte_errno;
862                         /* Decrease the rxq_ctrl's refcnt */
863                         if (rxq_ctrl)
864                                 mlx5_rxq_release(dev, i);
865                         continue;
866                 }
867                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
868                         DRV_LOG(ERR,
869                                 "port %u too many Rx queues for interrupt"
870                                 " vector size (%d), Rx interrupts cannot be"
871                                 " enabled",
872                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
873                         mlx5_rx_intr_vec_disable(dev);
874                         rte_errno = ENOMEM;
875                         return -rte_errno;
876                 }
877                 rc = mlx5_os_set_nonblock_channel_fd(rxq_obj->fd);
878                 if (rc < 0) {
879                         rte_errno = errno;
880                         DRV_LOG(ERR,
881                                 "port %u failed to make Rx interrupt file"
882                                 " descriptor %d non-blocking for queue index"
883                                 " %d",
884                                 dev->data->port_id, rxq_obj->fd, i);
885                         mlx5_rx_intr_vec_disable(dev);
886                         return -rte_errno;
887                 }
888
889                 if (rte_intr_vec_list_index_set(intr_handle, i,
890                                         RTE_INTR_VEC_RXTX_OFFSET + count))
891                         return -rte_errno;
892                 if (rte_intr_efds_index_set(intr_handle, count,
893                                                    rxq_obj->fd))
894                         return -rte_errno;
895                 count++;
896         }
897         if (!count)
898                 mlx5_rx_intr_vec_disable(dev);
899         else if (rte_intr_nb_efd_set(intr_handle, count))
900                 return -rte_errno;
901         return 0;
902 }
903
904 /**
905  * Clean up Rx interrupts handler.
906  *
907  * @param dev
908  *   Pointer to Ethernet device.
909  */
910 void
911 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
912 {
913         struct mlx5_priv *priv = dev->data->dev_private;
914         struct rte_intr_handle *intr_handle = dev->intr_handle;
915         unsigned int i;
916         unsigned int rxqs_n = priv->rxqs_n;
917         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
918
919         if (!dev->data->dev_conf.intr_conf.rxq)
920                 return;
921         if (rte_intr_vec_list_index_get(intr_handle, 0) < 0)
922                 goto free;
923         for (i = 0; i != n; ++i) {
924                 if (rte_intr_vec_list_index_get(intr_handle, i) ==
925                     RTE_INTR_VEC_RXTX_OFFSET + RTE_MAX_RXTX_INTR_VEC_ID)
926                         continue;
927                 /**
928                  * Need to access directly the queue to release the reference
929                  * kept in mlx5_rx_intr_vec_enable().
930                  */
931                 mlx5_rxq_release(dev, i);
932         }
933 free:
934         rte_intr_free_epoll_fd(intr_handle);
935
936         rte_intr_vec_list_free(intr_handle);
937
938         rte_intr_nb_efd_set(intr_handle, 0);
939 }
940
941 /**
942  *  MLX5 CQ notification .
943  *
944  *  @param rxq
945  *     Pointer to receive queue structure.
946  *  @param sq_n_rxq
947  *     Sequence number per receive queue .
948  */
949 static inline void
950 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
951 {
952         int sq_n = 0;
953         uint32_t doorbell_hi;
954         uint64_t doorbell;
955         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
956
957         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
958         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
959         doorbell = (uint64_t)doorbell_hi << 32;
960         doorbell |= rxq->cqn;
961         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
962         mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
963                          cq_db_reg, rxq->uar_lock_cq);
964 }
965
966 /**
967  * DPDK callback for Rx queue interrupt enable.
968  *
969  * @param dev
970  *   Pointer to Ethernet device structure.
971  * @param rx_queue_id
972  *   Rx queue number.
973  *
974  * @return
975  *   0 on success, a negative errno value otherwise and rte_errno is set.
976  */
977 int
978 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
979 {
980         struct mlx5_rxq_ctrl *rxq_ctrl;
981
982         rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
983         if (!rxq_ctrl)
984                 goto error;
985         if (rxq_ctrl->irq) {
986                 if (!rxq_ctrl->obj) {
987                         mlx5_rxq_release(dev, rx_queue_id);
988                         goto error;
989                 }
990                 mlx5_arm_cq(&rxq_ctrl->rxq, rxq_ctrl->rxq.cq_arm_sn);
991         }
992         mlx5_rxq_release(dev, rx_queue_id);
993         return 0;
994 error:
995         rte_errno = EINVAL;
996         return -rte_errno;
997 }
998
999 /**
1000  * DPDK callback for Rx queue interrupt disable.
1001  *
1002  * @param dev
1003  *   Pointer to Ethernet device structure.
1004  * @param rx_queue_id
1005  *   Rx queue number.
1006  *
1007  * @return
1008  *   0 on success, a negative errno value otherwise and rte_errno is set.
1009  */
1010 int
1011 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1012 {
1013         struct mlx5_priv *priv = dev->data->dev_private;
1014         struct mlx5_rxq_ctrl *rxq_ctrl;
1015         int ret = 0;
1016
1017         rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
1018         if (!rxq_ctrl) {
1019                 rte_errno = EINVAL;
1020                 return -rte_errno;
1021         }
1022         if (!rxq_ctrl->obj)
1023                 goto error;
1024         if (rxq_ctrl->irq) {
1025                 ret = priv->obj_ops.rxq_event_get(rxq_ctrl->obj);
1026                 if (ret < 0)
1027                         goto error;
1028                 rxq_ctrl->rxq.cq_arm_sn++;
1029         }
1030         mlx5_rxq_release(dev, rx_queue_id);
1031         return 0;
1032 error:
1033         /**
1034          * The ret variable may be EAGAIN which means the get_event function was
1035          * called before receiving one.
1036          */
1037         if (ret < 0)
1038                 rte_errno = errno;
1039         else
1040                 rte_errno = EINVAL;
1041         ret = rte_errno; /* Save rte_errno before cleanup. */
1042         mlx5_rxq_release(dev, rx_queue_id);
1043         if (ret != EAGAIN)
1044                 DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
1045                         dev->data->port_id, rx_queue_id);
1046         rte_errno = ret; /* Restore rte_errno. */
1047         return -rte_errno;
1048 }
1049
1050 /**
1051  * Verify the Rx queue objects list is empty
1052  *
1053  * @param dev
1054  *   Pointer to Ethernet device.
1055  *
1056  * @return
1057  *   The number of objects not released.
1058  */
1059 int
1060 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1061 {
1062         struct mlx5_priv *priv = dev->data->dev_private;
1063         int ret = 0;
1064         struct mlx5_rxq_obj *rxq_obj;
1065
1066         LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1067                 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1068                         dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1069                 ++ret;
1070         }
1071         return ret;
1072 }
1073
1074 /**
1075  * Callback function to initialize mbufs for Multi-Packet RQ.
1076  */
1077 static inline void
1078 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1079                     void *_m, unsigned int i __rte_unused)
1080 {
1081         struct mlx5_mprq_buf *buf = _m;
1082         struct rte_mbuf_ext_shared_info *shinfo;
1083         unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1084         unsigned int j;
1085
1086         memset(_m, 0, sizeof(*buf));
1087         buf->mp = mp;
1088         __atomic_store_n(&buf->refcnt, 1, __ATOMIC_RELAXED);
1089         for (j = 0; j != strd_n; ++j) {
1090                 shinfo = &buf->shinfos[j];
1091                 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1092                 shinfo->fcb_opaque = buf;
1093         }
1094 }
1095
1096 /**
1097  * Free mempool of Multi-Packet RQ.
1098  *
1099  * @param dev
1100  *   Pointer to Ethernet device.
1101  *
1102  * @return
1103  *   0 on success, negative errno value on failure.
1104  */
1105 int
1106 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1107 {
1108         struct mlx5_priv *priv = dev->data->dev_private;
1109         struct rte_mempool *mp = priv->mprq_mp;
1110         unsigned int i;
1111
1112         if (mp == NULL)
1113                 return 0;
1114         DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1115                 dev->data->port_id, mp->name);
1116         /*
1117          * If a buffer in the pool has been externally attached to a mbuf and it
1118          * is still in use by application, destroying the Rx queue can spoil
1119          * the packet. It is unlikely to happen but if application dynamically
1120          * creates and destroys with holding Rx packets, this can happen.
1121          *
1122          * TODO: It is unavoidable for now because the mempool for Multi-Packet
1123          * RQ isn't provided by application but managed by PMD.
1124          */
1125         if (!rte_mempool_full(mp)) {
1126                 DRV_LOG(ERR,
1127                         "port %u mempool for Multi-Packet RQ is still in use",
1128                         dev->data->port_id);
1129                 rte_errno = EBUSY;
1130                 return -rte_errno;
1131         }
1132         rte_mempool_free(mp);
1133         /* Unset mempool for each Rx queue. */
1134         for (i = 0; i != priv->rxqs_n; ++i) {
1135                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1136
1137                 if (rxq == NULL)
1138                         continue;
1139                 rxq->mprq_mp = NULL;
1140         }
1141         priv->mprq_mp = NULL;
1142         return 0;
1143 }
1144
1145 /**
1146  * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1147  * mempool. If already allocated, reuse it if there're enough elements.
1148  * Otherwise, resize it.
1149  *
1150  * @param dev
1151  *   Pointer to Ethernet device.
1152  *
1153  * @return
1154  *   0 on success, negative errno value on failure.
1155  */
1156 int
1157 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1158 {
1159         struct mlx5_priv *priv = dev->data->dev_private;
1160         struct rte_mempool *mp = priv->mprq_mp;
1161         char name[RTE_MEMPOOL_NAMESIZE];
1162         unsigned int desc = 0;
1163         unsigned int buf_len;
1164         unsigned int obj_num;
1165         unsigned int obj_size;
1166         unsigned int strd_num_n = 0;
1167         unsigned int strd_sz_n = 0;
1168         unsigned int i;
1169         unsigned int n_ibv = 0;
1170         int ret;
1171
1172         if (!mlx5_mprq_enabled(dev))
1173                 return 0;
1174         /* Count the total number of descriptors configured. */
1175         for (i = 0; i != priv->rxqs_n; ++i) {
1176                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1177                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1178                         (rxq, struct mlx5_rxq_ctrl, rxq);
1179
1180                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1181                         continue;
1182                 n_ibv++;
1183                 desc += 1 << rxq->elts_n;
1184                 /* Get the max number of strides. */
1185                 if (strd_num_n < rxq->strd_num_n)
1186                         strd_num_n = rxq->strd_num_n;
1187                 /* Get the max size of a stride. */
1188                 if (strd_sz_n < rxq->strd_sz_n)
1189                         strd_sz_n = rxq->strd_sz_n;
1190         }
1191         MLX5_ASSERT(strd_num_n && strd_sz_n);
1192         buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1193         obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
1194                 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
1195         /*
1196          * Received packets can be either memcpy'd or externally referenced. In
1197          * case that the packet is attached to an mbuf as an external buffer, as
1198          * it isn't possible to predict how the buffers will be queued by
1199          * application, there's no option to exactly pre-allocate needed buffers
1200          * in advance but to speculatively prepares enough buffers.
1201          *
1202          * In the data path, if this Mempool is depleted, PMD will try to memcpy
1203          * received packets to buffers provided by application (rxq->mp) until
1204          * this Mempool gets available again.
1205          */
1206         desc *= 4;
1207         obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
1208         /*
1209          * rte_mempool_create_empty() has sanity check to refuse large cache
1210          * size compared to the number of elements.
1211          * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1212          * constant number 2 instead.
1213          */
1214         obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1215         /* Check a mempool is already allocated and if it can be resued. */
1216         if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1217                 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1218                         dev->data->port_id, mp->name);
1219                 /* Reuse. */
1220                 goto exit;
1221         } else if (mp != NULL) {
1222                 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1223                         dev->data->port_id, mp->name);
1224                 /*
1225                  * If failed to free, which means it may be still in use, no way
1226                  * but to keep using the existing one. On buffer underrun,
1227                  * packets will be memcpy'd instead of external buffer
1228                  * attachment.
1229                  */
1230                 if (mlx5_mprq_free_mp(dev)) {
1231                         if (mp->elt_size >= obj_size)
1232                                 goto exit;
1233                         else
1234                                 return -rte_errno;
1235                 }
1236         }
1237         snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
1238         mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1239                                 0, NULL, NULL, mlx5_mprq_buf_init,
1240                                 (void *)((uintptr_t)1 << strd_num_n),
1241                                 dev->device->numa_node, 0);
1242         if (mp == NULL) {
1243                 DRV_LOG(ERR,
1244                         "port %u failed to allocate a mempool for"
1245                         " Multi-Packet RQ, count=%u, size=%u",
1246                         dev->data->port_id, obj_num, obj_size);
1247                 rte_errno = ENOMEM;
1248                 return -rte_errno;
1249         }
1250         ret = mlx5_mr_mempool_register(&priv->sh->cdev->mr_scache,
1251                                        priv->sh->cdev->pd, mp, &priv->mp_id);
1252         if (ret < 0 && rte_errno != EEXIST) {
1253                 ret = rte_errno;
1254                 DRV_LOG(ERR, "port %u failed to register a mempool for Multi-Packet RQ",
1255                         dev->data->port_id);
1256                 rte_mempool_free(mp);
1257                 rte_errno = ret;
1258                 return -rte_errno;
1259         }
1260         priv->mprq_mp = mp;
1261 exit:
1262         /* Set mempool for each Rx queue. */
1263         for (i = 0; i != priv->rxqs_n; ++i) {
1264                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1265                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1266                         (rxq, struct mlx5_rxq_ctrl, rxq);
1267
1268                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1269                         continue;
1270                 rxq->mprq_mp = mp;
1271         }
1272         DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1273                 dev->data->port_id);
1274         return 0;
1275 }
1276
1277 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
1278                                         sizeof(struct rte_vlan_hdr) * 2 + \
1279                                         sizeof(struct rte_ipv6_hdr)))
1280 #define MAX_TCP_OPTION_SIZE 40u
1281 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
1282                                  sizeof(struct rte_tcp_hdr) + \
1283                                  MAX_TCP_OPTION_SIZE))
1284
1285 /**
1286  * Adjust the maximum LRO massage size.
1287  *
1288  * @param dev
1289  *   Pointer to Ethernet device.
1290  * @param idx
1291  *   RX queue index.
1292  * @param max_lro_size
1293  *   The maximum size for LRO packet.
1294  */
1295 static void
1296 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
1297                              uint32_t max_lro_size)
1298 {
1299         struct mlx5_priv *priv = dev->data->dev_private;
1300
1301         if (priv->config.hca_attr.lro_max_msg_sz_mode ==
1302             MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
1303             MLX5_MAX_TCP_HDR_OFFSET)
1304                 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
1305         max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
1306         MLX5_ASSERT(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
1307         max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
1308         if (priv->max_lro_msg_size)
1309                 priv->max_lro_msg_size =
1310                         RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
1311         else
1312                 priv->max_lro_msg_size = max_lro_size;
1313         DRV_LOG(DEBUG,
1314                 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
1315                 dev->data->port_id, idx,
1316                 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
1317 }
1318
1319 /**
1320  * Create a DPDK Rx queue.
1321  *
1322  * @param dev
1323  *   Pointer to Ethernet device.
1324  * @param idx
1325  *   RX queue index.
1326  * @param desc
1327  *   Number of descriptors to configure in queue.
1328  * @param socket
1329  *   NUMA socket on which memory must be allocated.
1330  *
1331  * @return
1332  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1333  */
1334 struct mlx5_rxq_ctrl *
1335 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1336              unsigned int socket, const struct rte_eth_rxconf *conf,
1337              const struct rte_eth_rxseg_split *rx_seg, uint16_t n_seg)
1338 {
1339         struct mlx5_priv *priv = dev->data->dev_private;
1340         struct mlx5_rxq_ctrl *tmpl;
1341         unsigned int mb_len = rte_pktmbuf_data_room_size(rx_seg[0].mp);
1342         struct mlx5_dev_config *config = &priv->config;
1343         uint64_t offloads = conf->offloads |
1344                            dev->data->dev_conf.rxmode.offloads;
1345         unsigned int lro_on_queue = !!(offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO);
1346         unsigned int max_rx_pktlen = lro_on_queue ?
1347                         dev->data->dev_conf.rxmode.max_lro_pkt_size :
1348                         dev->data->mtu + (unsigned int)RTE_ETHER_HDR_LEN +
1349                                 RTE_ETHER_CRC_LEN;
1350         unsigned int non_scatter_min_mbuf_size = max_rx_pktlen +
1351                                                         RTE_PKTMBUF_HEADROOM;
1352         unsigned int max_lro_size = 0;
1353         unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
1354         const int mprq_en = mlx5_check_mprq_support(dev) > 0 && n_seg == 1 &&
1355                             !rx_seg[0].offset && !rx_seg[0].length;
1356         unsigned int mprq_stride_nums = config->mprq.stride_num_n ?
1357                 config->mprq.stride_num_n : MLX5_MPRQ_STRIDE_NUM_N;
1358         unsigned int mprq_stride_size = non_scatter_min_mbuf_size <=
1359                 (1U << config->mprq.max_stride_size_n) ?
1360                 log2above(non_scatter_min_mbuf_size) : MLX5_MPRQ_STRIDE_SIZE_N;
1361         unsigned int mprq_stride_cap = (config->mprq.stride_num_n ?
1362                 (1U << config->mprq.stride_num_n) : (1U << mprq_stride_nums)) *
1363                 (config->mprq.stride_size_n ?
1364                 (1U << config->mprq.stride_size_n) : (1U << mprq_stride_size));
1365         /*
1366          * Always allocate extra slots, even if eventually
1367          * the vector Rx will not be used.
1368          */
1369         uint16_t desc_n = desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1370         const struct rte_eth_rxseg_split *qs_seg = rx_seg;
1371         unsigned int tail_len;
1372
1373         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1374                 sizeof(*tmpl) + desc_n * sizeof(struct rte_mbuf *) +
1375                 (!!mprq_en) *
1376                 (desc >> mprq_stride_nums) * sizeof(struct mlx5_mprq_buf *),
1377                 0, socket);
1378         if (!tmpl) {
1379                 rte_errno = ENOMEM;
1380                 return NULL;
1381         }
1382         MLX5_ASSERT(n_seg && n_seg <= MLX5_MAX_RXQ_NSEG);
1383         /*
1384          * Build the array of actual buffer offsets and lengths.
1385          * Pad with the buffers from the last memory pool if
1386          * needed to handle max size packets, replace zero length
1387          * with the buffer length from the pool.
1388          */
1389         tail_len = max_rx_pktlen;
1390         do {
1391                 struct mlx5_eth_rxseg *hw_seg =
1392                                         &tmpl->rxq.rxseg[tmpl->rxq.rxseg_n];
1393                 uint32_t buf_len, offset, seg_len;
1394
1395                 /*
1396                  * For the buffers beyond descriptions offset is zero,
1397                  * the first buffer contains head room.
1398                  */
1399                 buf_len = rte_pktmbuf_data_room_size(qs_seg->mp);
1400                 offset = (tmpl->rxq.rxseg_n >= n_seg ? 0 : qs_seg->offset) +
1401                          (tmpl->rxq.rxseg_n ? 0 : RTE_PKTMBUF_HEADROOM);
1402                 /*
1403                  * For the buffers beyond descriptions the length is
1404                  * pool buffer length, zero lengths are replaced with
1405                  * pool buffer length either.
1406                  */
1407                 seg_len = tmpl->rxq.rxseg_n >= n_seg ? buf_len :
1408                                                        qs_seg->length ?
1409                                                        qs_seg->length :
1410                                                        (buf_len - offset);
1411                 /* Check is done in long int, now overflows. */
1412                 if (buf_len < seg_len + offset) {
1413                         DRV_LOG(ERR, "port %u Rx queue %u: Split offset/length "
1414                                      "%u/%u can't be satisfied",
1415                                      dev->data->port_id, idx,
1416                                      qs_seg->length, qs_seg->offset);
1417                         rte_errno = EINVAL;
1418                         goto error;
1419                 }
1420                 if (seg_len > tail_len)
1421                         seg_len = buf_len - offset;
1422                 if (++tmpl->rxq.rxseg_n > MLX5_MAX_RXQ_NSEG) {
1423                         DRV_LOG(ERR,
1424                                 "port %u too many SGEs (%u) needed to handle"
1425                                 " requested maximum packet size %u, the maximum"
1426                                 " supported are %u", dev->data->port_id,
1427                                 tmpl->rxq.rxseg_n, max_rx_pktlen,
1428                                 MLX5_MAX_RXQ_NSEG);
1429                         rte_errno = ENOTSUP;
1430                         goto error;
1431                 }
1432                 /* Build the actual scattering element in the queue object. */
1433                 hw_seg->mp = qs_seg->mp;
1434                 MLX5_ASSERT(offset <= UINT16_MAX);
1435                 MLX5_ASSERT(seg_len <= UINT16_MAX);
1436                 hw_seg->offset = (uint16_t)offset;
1437                 hw_seg->length = (uint16_t)seg_len;
1438                 /*
1439                  * Advance the segment descriptor, the padding is the based
1440                  * on the attributes of the last descriptor.
1441                  */
1442                 if (tmpl->rxq.rxseg_n < n_seg)
1443                         qs_seg++;
1444                 tail_len -= RTE_MIN(tail_len, seg_len);
1445         } while (tail_len || !rte_is_power_of_2(tmpl->rxq.rxseg_n));
1446         MLX5_ASSERT(tmpl->rxq.rxseg_n &&
1447                     tmpl->rxq.rxseg_n <= MLX5_MAX_RXQ_NSEG);
1448         if (tmpl->rxq.rxseg_n > 1 && !(offloads & RTE_ETH_RX_OFFLOAD_SCATTER)) {
1449                 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
1450                         " configured and no enough mbuf space(%u) to contain "
1451                         "the maximum RX packet length(%u) with head-room(%u)",
1452                         dev->data->port_id, idx, mb_len, max_rx_pktlen,
1453                         RTE_PKTMBUF_HEADROOM);
1454                 rte_errno = ENOSPC;
1455                 goto error;
1456         }
1457         tmpl->type = MLX5_RXQ_TYPE_STANDARD;
1458         if (mlx5_mr_ctrl_init(&tmpl->rxq.mr_ctrl,
1459                               &priv->sh->cdev->mr_scache.dev_gen, socket)) {
1460                 /* rte_errno is already set. */
1461                 goto error;
1462         }
1463         tmpl->socket = socket;
1464         if (dev->data->dev_conf.intr_conf.rxq)
1465                 tmpl->irq = 1;
1466         /*
1467          * This Rx queue can be configured as a Multi-Packet RQ if all of the
1468          * following conditions are met:
1469          *  - MPRQ is enabled.
1470          *  - The number of descs is more than the number of strides.
1471          *  - max_rx_pktlen plus overhead is less than the max size
1472          *    of a stride or mprq_stride_size is specified by a user.
1473          *    Need to make sure that there are enough strides to encap
1474          *    the maximum packet size in case mprq_stride_size is set.
1475          *  Otherwise, enable Rx scatter if necessary.
1476          */
1477         if (mprq_en && desc > (1U << mprq_stride_nums) &&
1478             (non_scatter_min_mbuf_size <=
1479              (1U << config->mprq.max_stride_size_n) ||
1480              (config->mprq.stride_size_n &&
1481               non_scatter_min_mbuf_size <= mprq_stride_cap))) {
1482                 /* TODO: Rx scatter isn't supported yet. */
1483                 tmpl->rxq.sges_n = 0;
1484                 /* Trim the number of descs needed. */
1485                 desc >>= mprq_stride_nums;
1486                 tmpl->rxq.strd_num_n = config->mprq.stride_num_n ?
1487                         config->mprq.stride_num_n : mprq_stride_nums;
1488                 tmpl->rxq.strd_sz_n = config->mprq.stride_size_n ?
1489                         config->mprq.stride_size_n : mprq_stride_size;
1490                 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1491                 tmpl->rxq.strd_scatter_en =
1492                                 !!(offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
1493                 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
1494                                 config->mprq.max_memcpy_len);
1495                 max_lro_size = RTE_MIN(max_rx_pktlen,
1496                                        (1u << tmpl->rxq.strd_num_n) *
1497                                        (1u << tmpl->rxq.strd_sz_n));
1498                 DRV_LOG(DEBUG,
1499                         "port %u Rx queue %u: Multi-Packet RQ is enabled"
1500                         " strd_num_n = %u, strd_sz_n = %u",
1501                         dev->data->port_id, idx,
1502                         tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1503         } else if (tmpl->rxq.rxseg_n == 1) {
1504                 MLX5_ASSERT(max_rx_pktlen <= first_mb_free_size);
1505                 tmpl->rxq.sges_n = 0;
1506                 max_lro_size = max_rx_pktlen;
1507         } else if (offloads & RTE_ETH_RX_OFFLOAD_SCATTER) {
1508                 unsigned int sges_n;
1509
1510                 if (lro_on_queue && first_mb_free_size <
1511                     MLX5_MAX_LRO_HEADER_FIX) {
1512                         DRV_LOG(ERR, "Not enough space in the first segment(%u)"
1513                                 " to include the max header size(%u) for LRO",
1514                                 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
1515                         rte_errno = ENOTSUP;
1516                         goto error;
1517                 }
1518                 /*
1519                  * Determine the number of SGEs needed for a full packet
1520                  * and round it to the next power of two.
1521                  */
1522                 sges_n = log2above(tmpl->rxq.rxseg_n);
1523                 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
1524                         DRV_LOG(ERR,
1525                                 "port %u too many SGEs (%u) needed to handle"
1526                                 " requested maximum packet size %u, the maximum"
1527                                 " supported are %u", dev->data->port_id,
1528                                 1 << sges_n, max_rx_pktlen,
1529                                 1u << MLX5_MAX_LOG_RQ_SEGS);
1530                         rte_errno = ENOTSUP;
1531                         goto error;
1532                 }
1533                 tmpl->rxq.sges_n = sges_n;
1534                 max_lro_size = max_rx_pktlen;
1535         }
1536         if (config->mprq.enabled && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1537                 DRV_LOG(WARNING,
1538                         "port %u MPRQ is requested but cannot be enabled\n"
1539                         " (requested: pkt_sz = %u, desc_num = %u,"
1540                         " rxq_num = %u, stride_sz = %u, stride_num = %u\n"
1541                         "  supported: min_rxqs_num = %u,"
1542                         " min_stride_sz = %u, max_stride_sz = %u).",
1543                         dev->data->port_id, non_scatter_min_mbuf_size,
1544                         desc, priv->rxqs_n,
1545                         config->mprq.stride_size_n ?
1546                                 (1U << config->mprq.stride_size_n) :
1547                                 (1U << mprq_stride_size),
1548                         config->mprq.stride_num_n ?
1549                                 (1U << config->mprq.stride_num_n) :
1550                                 (1U << mprq_stride_nums),
1551                         config->mprq.min_rxqs_num,
1552                         (1U << config->mprq.min_stride_size_n),
1553                         (1U << config->mprq.max_stride_size_n));
1554         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1555                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1556         if (desc % (1 << tmpl->rxq.sges_n)) {
1557                 DRV_LOG(ERR,
1558                         "port %u number of Rx queue descriptors (%u) is not a"
1559                         " multiple of SGEs per packet (%u)",
1560                         dev->data->port_id,
1561                         desc,
1562                         1 << tmpl->rxq.sges_n);
1563                 rte_errno = EINVAL;
1564                 goto error;
1565         }
1566         mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
1567         /* Toggle RX checksum offload if hardware supports it. */
1568         tmpl->rxq.csum = !!(offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM);
1569         /* Configure Rx timestamp. */
1570         tmpl->rxq.hw_timestamp = !!(offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP);
1571         tmpl->rxq.timestamp_rx_flag = 0;
1572         if (tmpl->rxq.hw_timestamp && rte_mbuf_dyn_rx_timestamp_register(
1573                         &tmpl->rxq.timestamp_offset,
1574                         &tmpl->rxq.timestamp_rx_flag) != 0) {
1575                 DRV_LOG(ERR, "Cannot register Rx timestamp field/flag");
1576                 goto error;
1577         }
1578         /* Configure VLAN stripping. */
1579         tmpl->rxq.vlan_strip = !!(offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
1580         /* By default, FCS (CRC) is stripped by hardware. */
1581         tmpl->rxq.crc_present = 0;
1582         tmpl->rxq.lro = lro_on_queue;
1583         if (offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC) {
1584                 if (config->hw_fcs_strip) {
1585                         /*
1586                          * RQs used for LRO-enabled TIRs should not be
1587                          * configured to scatter the FCS.
1588                          */
1589                         if (lro_on_queue)
1590                                 DRV_LOG(WARNING,
1591                                         "port %u CRC stripping has been "
1592                                         "disabled but will still be performed "
1593                                         "by hardware, because LRO is enabled",
1594                                         dev->data->port_id);
1595                         else
1596                                 tmpl->rxq.crc_present = 1;
1597                 } else {
1598                         DRV_LOG(WARNING,
1599                                 "port %u CRC stripping has been disabled but will"
1600                                 " still be performed by hardware, make sure MLNX_OFED"
1601                                 " and firmware are up to date",
1602                                 dev->data->port_id);
1603                 }
1604         }
1605         DRV_LOG(DEBUG,
1606                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1607                 " incoming frames to hide it",
1608                 dev->data->port_id,
1609                 tmpl->rxq.crc_present ? "disabled" : "enabled",
1610                 tmpl->rxq.crc_present << 2);
1611         /* Save port ID. */
1612         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1613                 (!!(dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS));
1614         tmpl->rxq.port_id = dev->data->port_id;
1615         tmpl->priv = priv;
1616         tmpl->rxq.mp = rx_seg[0].mp;
1617         tmpl->rxq.elts_n = log2above(desc);
1618         tmpl->rxq.rq_repl_thresh =
1619                 MLX5_VPMD_RXQ_RPLNSH_THRESH(desc_n);
1620         tmpl->rxq.elts =
1621                 (struct rte_mbuf *(*)[desc_n])(tmpl + 1);
1622         tmpl->rxq.mprq_bufs =
1623                 (struct mlx5_mprq_buf *(*)[desc])(*tmpl->rxq.elts + desc_n);
1624 #ifndef RTE_ARCH_64
1625         tmpl->rxq.uar_lock_cq = &priv->sh->uar_lock_cq;
1626 #endif
1627         tmpl->rxq.idx = idx;
1628         __atomic_fetch_add(&tmpl->refcnt, 1, __ATOMIC_RELAXED);
1629         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1630         return tmpl;
1631 error:
1632         mlx5_mr_btree_free(&tmpl->rxq.mr_ctrl.cache_bh);
1633         mlx5_free(tmpl);
1634         return NULL;
1635 }
1636
1637 /**
1638  * Create a DPDK Rx hairpin queue.
1639  *
1640  * @param dev
1641  *   Pointer to Ethernet device.
1642  * @param idx
1643  *   RX queue index.
1644  * @param desc
1645  *   Number of descriptors to configure in queue.
1646  * @param hairpin_conf
1647  *   The hairpin binding configuration.
1648  *
1649  * @return
1650  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1651  */
1652 struct mlx5_rxq_ctrl *
1653 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1654                      const struct rte_eth_hairpin_conf *hairpin_conf)
1655 {
1656         struct mlx5_priv *priv = dev->data->dev_private;
1657         struct mlx5_rxq_ctrl *tmpl;
1658
1659         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1660                            SOCKET_ID_ANY);
1661         if (!tmpl) {
1662                 rte_errno = ENOMEM;
1663                 return NULL;
1664         }
1665         tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
1666         tmpl->socket = SOCKET_ID_ANY;
1667         tmpl->rxq.rss_hash = 0;
1668         tmpl->rxq.port_id = dev->data->port_id;
1669         tmpl->priv = priv;
1670         tmpl->rxq.mp = NULL;
1671         tmpl->rxq.elts_n = log2above(desc);
1672         tmpl->rxq.elts = NULL;
1673         tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
1674         tmpl->hairpin_conf = *hairpin_conf;
1675         tmpl->rxq.idx = idx;
1676         __atomic_fetch_add(&tmpl->refcnt, 1, __ATOMIC_RELAXED);
1677         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1678         return tmpl;
1679 }
1680
1681 /**
1682  * Get a Rx queue.
1683  *
1684  * @param dev
1685  *   Pointer to Ethernet device.
1686  * @param idx
1687  *   RX queue index.
1688  *
1689  * @return
1690  *   A pointer to the queue if it exists, NULL otherwise.
1691  */
1692 struct mlx5_rxq_ctrl *
1693 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1694 {
1695         struct mlx5_priv *priv = dev->data->dev_private;
1696         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1697         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1698
1699         if (rxq_data) {
1700                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1701                 __atomic_fetch_add(&rxq_ctrl->refcnt, 1, __ATOMIC_RELAXED);
1702         }
1703         return rxq_ctrl;
1704 }
1705
1706 /**
1707  * Release a Rx queue.
1708  *
1709  * @param dev
1710  *   Pointer to Ethernet device.
1711  * @param idx
1712  *   RX queue index.
1713  *
1714  * @return
1715  *   1 while a reference on it exists, 0 when freed.
1716  */
1717 int
1718 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
1719 {
1720         struct mlx5_priv *priv = dev->data->dev_private;
1721         struct mlx5_rxq_ctrl *rxq_ctrl;
1722
1723         if (priv->rxqs == NULL || (*priv->rxqs)[idx] == NULL)
1724                 return 0;
1725         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1726         if (__atomic_sub_fetch(&rxq_ctrl->refcnt, 1, __ATOMIC_RELAXED) > 1)
1727                 return 1;
1728         if (rxq_ctrl->obj) {
1729                 priv->obj_ops.rxq_obj_release(rxq_ctrl->obj);
1730                 LIST_REMOVE(rxq_ctrl->obj, next);
1731                 mlx5_free(rxq_ctrl->obj);
1732                 rxq_ctrl->obj = NULL;
1733         }
1734         if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD) {
1735                 rxq_free_elts(rxq_ctrl);
1736                 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
1737         }
1738         if (!__atomic_load_n(&rxq_ctrl->refcnt, __ATOMIC_RELAXED)) {
1739                 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
1740                         mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
1741                 LIST_REMOVE(rxq_ctrl, next);
1742                 mlx5_free(rxq_ctrl);
1743                 (*priv->rxqs)[idx] = NULL;
1744         }
1745         return 0;
1746 }
1747
1748 /**
1749  * Verify the Rx Queue list is empty
1750  *
1751  * @param dev
1752  *   Pointer to Ethernet device.
1753  *
1754  * @return
1755  *   The number of object not released.
1756  */
1757 int
1758 mlx5_rxq_verify(struct rte_eth_dev *dev)
1759 {
1760         struct mlx5_priv *priv = dev->data->dev_private;
1761         struct mlx5_rxq_ctrl *rxq_ctrl;
1762         int ret = 0;
1763
1764         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1765                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
1766                         dev->data->port_id, rxq_ctrl->rxq.idx);
1767                 ++ret;
1768         }
1769         return ret;
1770 }
1771
1772 /**
1773  * Get a Rx queue type.
1774  *
1775  * @param dev
1776  *   Pointer to Ethernet device.
1777  * @param idx
1778  *   Rx queue index.
1779  *
1780  * @return
1781  *   The Rx queue type.
1782  */
1783 enum mlx5_rxq_type
1784 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
1785 {
1786         struct mlx5_priv *priv = dev->data->dev_private;
1787         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1788
1789         if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
1790                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1791                                         struct mlx5_rxq_ctrl,
1792                                         rxq);
1793                 return rxq_ctrl->type;
1794         }
1795         return MLX5_RXQ_TYPE_UNDEFINED;
1796 }
1797
1798 /*
1799  * Get a Rx hairpin queue configuration.
1800  *
1801  * @param dev
1802  *   Pointer to Ethernet device.
1803  * @param idx
1804  *   Rx queue index.
1805  *
1806  * @return
1807  *   Pointer to the configuration if a hairpin RX queue, otherwise NULL.
1808  */
1809 const struct rte_eth_hairpin_conf *
1810 mlx5_rxq_get_hairpin_conf(struct rte_eth_dev *dev, uint16_t idx)
1811 {
1812         struct mlx5_priv *priv = dev->data->dev_private;
1813         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1814
1815         if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
1816                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1817                                         struct mlx5_rxq_ctrl,
1818                                         rxq);
1819                 if (rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN)
1820                         return &rxq_ctrl->hairpin_conf;
1821         }
1822         return NULL;
1823 }
1824
1825 /**
1826  * Match queues listed in arguments to queues contained in indirection table
1827  * object.
1828  *
1829  * @param ind_tbl
1830  *   Pointer to indirection table to match.
1831  * @param queues
1832  *   Queues to match to ques in indirection table.
1833  * @param queues_n
1834  *   Number of queues in the array.
1835  *
1836  * @return
1837  *   1 if all queues in indirection table match 0 othrwise.
1838  */
1839 static int
1840 mlx5_ind_table_obj_match_queues(const struct mlx5_ind_table_obj *ind_tbl,
1841                        const uint16_t *queues, uint32_t queues_n)
1842 {
1843                 return (ind_tbl->queues_n == queues_n) &&
1844                     (!memcmp(ind_tbl->queues, queues,
1845                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0])));
1846 }
1847
1848 /**
1849  * Get an indirection table.
1850  *
1851  * @param dev
1852  *   Pointer to Ethernet device.
1853  * @param queues
1854  *   Queues entering in the indirection table.
1855  * @param queues_n
1856  *   Number of queues in the array.
1857  *
1858  * @return
1859  *   An indirection table if found.
1860  */
1861 struct mlx5_ind_table_obj *
1862 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
1863                        uint32_t queues_n)
1864 {
1865         struct mlx5_priv *priv = dev->data->dev_private;
1866         struct mlx5_ind_table_obj *ind_tbl;
1867
1868         rte_rwlock_read_lock(&priv->ind_tbls_lock);
1869         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1870                 if ((ind_tbl->queues_n == queues_n) &&
1871                     (memcmp(ind_tbl->queues, queues,
1872                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1873                      == 0)) {
1874                         __atomic_fetch_add(&ind_tbl->refcnt, 1,
1875                                            __ATOMIC_RELAXED);
1876                         break;
1877                 }
1878         }
1879         rte_rwlock_read_unlock(&priv->ind_tbls_lock);
1880         return ind_tbl;
1881 }
1882
1883 /**
1884  * Release an indirection table.
1885  *
1886  * @param dev
1887  *   Pointer to Ethernet device.
1888  * @param ind_table
1889  *   Indirection table to release.
1890  * @param standalone
1891  *   Indirection table for Standalone queue.
1892  *
1893  * @return
1894  *   1 while a reference on it exists, 0 when freed.
1895  */
1896 int
1897 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
1898                            struct mlx5_ind_table_obj *ind_tbl,
1899                            bool standalone)
1900 {
1901         struct mlx5_priv *priv = dev->data->dev_private;
1902         unsigned int i, ret;
1903
1904         rte_rwlock_write_lock(&priv->ind_tbls_lock);
1905         ret = __atomic_sub_fetch(&ind_tbl->refcnt, 1, __ATOMIC_RELAXED);
1906         if (!ret && !standalone)
1907                 LIST_REMOVE(ind_tbl, next);
1908         rte_rwlock_write_unlock(&priv->ind_tbls_lock);
1909         if (ret)
1910                 return 1;
1911         priv->obj_ops.ind_table_destroy(ind_tbl);
1912         for (i = 0; i != ind_tbl->queues_n; ++i)
1913                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
1914         mlx5_free(ind_tbl);
1915         return 0;
1916 }
1917
1918 /**
1919  * Verify the Rx Queue list is empty
1920  *
1921  * @param dev
1922  *   Pointer to Ethernet device.
1923  *
1924  * @return
1925  *   The number of object not released.
1926  */
1927 int
1928 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
1929 {
1930         struct mlx5_priv *priv = dev->data->dev_private;
1931         struct mlx5_ind_table_obj *ind_tbl;
1932         int ret = 0;
1933
1934         rte_rwlock_read_lock(&priv->ind_tbls_lock);
1935         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1936                 DRV_LOG(DEBUG,
1937                         "port %u indirection table obj %p still referenced",
1938                         dev->data->port_id, (void *)ind_tbl);
1939                 ++ret;
1940         }
1941         rte_rwlock_read_unlock(&priv->ind_tbls_lock);
1942         return ret;
1943 }
1944
1945 /**
1946  * Setup an indirection table structure fields.
1947  *
1948  * @param dev
1949  *   Pointer to Ethernet device.
1950  * @param ind_table
1951  *   Indirection table to modify.
1952  *
1953  * @return
1954  *   0 on success, a negative errno value otherwise and rte_errno is set.
1955  */
1956 int
1957 mlx5_ind_table_obj_setup(struct rte_eth_dev *dev,
1958                          struct mlx5_ind_table_obj *ind_tbl)
1959 {
1960         struct mlx5_priv *priv = dev->data->dev_private;
1961         uint32_t queues_n = ind_tbl->queues_n;
1962         uint16_t *queues = ind_tbl->queues;
1963         unsigned int i, j;
1964         int ret = 0, err;
1965         const unsigned int n = rte_is_power_of_2(queues_n) ?
1966                                log2above(queues_n) :
1967                                log2above(priv->config.ind_table_max_size);
1968
1969         for (i = 0; i != queues_n; ++i) {
1970                 if (!mlx5_rxq_get(dev, queues[i])) {
1971                         ret = -rte_errno;
1972                         goto error;
1973                 }
1974         }
1975         ret = priv->obj_ops.ind_table_new(dev, n, ind_tbl);
1976         if (ret)
1977                 goto error;
1978         __atomic_fetch_add(&ind_tbl->refcnt, 1, __ATOMIC_RELAXED);
1979         return 0;
1980 error:
1981         err = rte_errno;
1982         for (j = 0; j < i; j++)
1983                 mlx5_rxq_release(dev, ind_tbl->queues[j]);
1984         rte_errno = err;
1985         DRV_LOG(DEBUG, "Port %u cannot setup indirection table.",
1986                 dev->data->port_id);
1987         return ret;
1988 }
1989
1990 /**
1991  * Create an indirection table.
1992  *
1993  * @param dev
1994  *   Pointer to Ethernet device.
1995  * @param queues
1996  *   Queues entering in the indirection table.
1997  * @param queues_n
1998  *   Number of queues in the array.
1999  * @param standalone
2000  *   Indirection table for Standalone queue.
2001  *
2002  * @return
2003  *   The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
2004  */
2005 static struct mlx5_ind_table_obj *
2006 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
2007                        uint32_t queues_n, bool standalone)
2008 {
2009         struct mlx5_priv *priv = dev->data->dev_private;
2010         struct mlx5_ind_table_obj *ind_tbl;
2011         int ret;
2012
2013         ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl) +
2014                               queues_n * sizeof(uint16_t), 0, SOCKET_ID_ANY);
2015         if (!ind_tbl) {
2016                 rte_errno = ENOMEM;
2017                 return NULL;
2018         }
2019         ind_tbl->queues_n = queues_n;
2020         ind_tbl->queues = (uint16_t *)(ind_tbl + 1);
2021         memcpy(ind_tbl->queues, queues, queues_n * sizeof(*queues));
2022         ret = mlx5_ind_table_obj_setup(dev, ind_tbl);
2023         if (ret < 0) {
2024                 mlx5_free(ind_tbl);
2025                 return NULL;
2026         }
2027         if (!standalone) {
2028                 rte_rwlock_write_lock(&priv->ind_tbls_lock);
2029                 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
2030                 rte_rwlock_write_unlock(&priv->ind_tbls_lock);
2031         }
2032         return ind_tbl;
2033 }
2034
2035 static int
2036 mlx5_ind_table_obj_check_standalone(struct rte_eth_dev *dev __rte_unused,
2037                                     struct mlx5_ind_table_obj *ind_tbl)
2038 {
2039         uint32_t refcnt;
2040
2041         refcnt = __atomic_load_n(&ind_tbl->refcnt, __ATOMIC_RELAXED);
2042         if (refcnt <= 1)
2043                 return 0;
2044         /*
2045          * Modification of indirection tables having more than 1
2046          * reference is unsupported.
2047          */
2048         DRV_LOG(DEBUG,
2049                 "Port %u cannot modify indirection table %p (refcnt %u > 1).",
2050                 dev->data->port_id, (void *)ind_tbl, refcnt);
2051         rte_errno = EINVAL;
2052         return -rte_errno;
2053 }
2054
2055 /**
2056  * Modify an indirection table.
2057  *
2058  * @param dev
2059  *   Pointer to Ethernet device.
2060  * @param ind_table
2061  *   Indirection table to modify.
2062  * @param queues
2063  *   Queues replacement for the indirection table.
2064  * @param queues_n
2065  *   Number of queues in the array.
2066  * @param standalone
2067  *   Indirection table for Standalone queue.
2068  *
2069  * @return
2070  *   0 on success, a negative errno value otherwise and rte_errno is set.
2071  */
2072 int
2073 mlx5_ind_table_obj_modify(struct rte_eth_dev *dev,
2074                           struct mlx5_ind_table_obj *ind_tbl,
2075                           uint16_t *queues, const uint32_t queues_n,
2076                           bool standalone)
2077 {
2078         struct mlx5_priv *priv = dev->data->dev_private;
2079         unsigned int i, j;
2080         int ret = 0, err;
2081         const unsigned int n = rte_is_power_of_2(queues_n) ?
2082                                log2above(queues_n) :
2083                                log2above(priv->config.ind_table_max_size);
2084
2085         MLX5_ASSERT(standalone);
2086         RTE_SET_USED(standalone);
2087         if (mlx5_ind_table_obj_check_standalone(dev, ind_tbl) < 0)
2088                 return -rte_errno;
2089         for (i = 0; i != queues_n; ++i) {
2090                 if (!mlx5_rxq_get(dev, queues[i])) {
2091                         ret = -rte_errno;
2092                         goto error;
2093                 }
2094         }
2095         MLX5_ASSERT(priv->obj_ops.ind_table_modify);
2096         ret = priv->obj_ops.ind_table_modify(dev, n, queues, queues_n, ind_tbl);
2097         if (ret)
2098                 goto error;
2099         for (j = 0; j < ind_tbl->queues_n; j++)
2100                 mlx5_rxq_release(dev, ind_tbl->queues[j]);
2101         ind_tbl->queues_n = queues_n;
2102         ind_tbl->queues = queues;
2103         return 0;
2104 error:
2105         err = rte_errno;
2106         for (j = 0; j < i; j++)
2107                 mlx5_rxq_release(dev, queues[j]);
2108         rte_errno = err;
2109         DRV_LOG(DEBUG, "Port %u cannot setup indirection table.",
2110                 dev->data->port_id);
2111         return ret;
2112 }
2113
2114 /**
2115  * Attach an indirection table to its queues.
2116  *
2117  * @param dev
2118  *   Pointer to Ethernet device.
2119  * @param ind_table
2120  *   Indirection table to attach.
2121  *
2122  * @return
2123  *   0 on success, a negative errno value otherwise and rte_errno is set.
2124  */
2125 int
2126 mlx5_ind_table_obj_attach(struct rte_eth_dev *dev,
2127                           struct mlx5_ind_table_obj *ind_tbl)
2128 {
2129         unsigned int i;
2130         int ret;
2131
2132         ret = mlx5_ind_table_obj_modify(dev, ind_tbl, ind_tbl->queues,
2133                                         ind_tbl->queues_n, true);
2134         if (ret != 0) {
2135                 DRV_LOG(ERR, "Port %u could not modify indirect table obj %p",
2136                         dev->data->port_id, (void *)ind_tbl);
2137                 return ret;
2138         }
2139         for (i = 0; i < ind_tbl->queues_n; i++)
2140                 mlx5_rxq_get(dev, ind_tbl->queues[i]);
2141         return 0;
2142 }
2143
2144 /**
2145  * Detach an indirection table from its queues.
2146  *
2147  * @param dev
2148  *   Pointer to Ethernet device.
2149  * @param ind_table
2150  *   Indirection table to detach.
2151  *
2152  * @return
2153  *   0 on success, a negative errno value otherwise and rte_errno is set.
2154  */
2155 int
2156 mlx5_ind_table_obj_detach(struct rte_eth_dev *dev,
2157                           struct mlx5_ind_table_obj *ind_tbl)
2158 {
2159         struct mlx5_priv *priv = dev->data->dev_private;
2160         const unsigned int n = rte_is_power_of_2(ind_tbl->queues_n) ?
2161                                log2above(ind_tbl->queues_n) :
2162                                log2above(priv->config.ind_table_max_size);
2163         unsigned int i;
2164         int ret;
2165
2166         ret = mlx5_ind_table_obj_check_standalone(dev, ind_tbl);
2167         if (ret != 0)
2168                 return ret;
2169         MLX5_ASSERT(priv->obj_ops.ind_table_modify);
2170         ret = priv->obj_ops.ind_table_modify(dev, n, NULL, 0, ind_tbl);
2171         if (ret != 0) {
2172                 DRV_LOG(ERR, "Port %u could not modify indirect table obj %p",
2173                         dev->data->port_id, (void *)ind_tbl);
2174                 return ret;
2175         }
2176         for (i = 0; i < ind_tbl->queues_n; i++)
2177                 mlx5_rxq_release(dev, ind_tbl->queues[i]);
2178         return ret;
2179 }
2180
2181 int
2182 mlx5_hrxq_match_cb(void *tool_ctx __rte_unused, struct mlx5_list_entry *entry,
2183                    void *cb_ctx)
2184 {
2185         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
2186         struct mlx5_flow_rss_desc *rss_desc = ctx->data;
2187         struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2188
2189         return (hrxq->rss_key_len != rss_desc->key_len ||
2190             memcmp(hrxq->rss_key, rss_desc->key, rss_desc->key_len) ||
2191             hrxq->hash_fields != rss_desc->hash_fields ||
2192             hrxq->ind_table->queues_n != rss_desc->queue_num ||
2193             memcmp(hrxq->ind_table->queues, rss_desc->queue,
2194             rss_desc->queue_num * sizeof(rss_desc->queue[0])));
2195 }
2196
2197 /**
2198  * Modify an Rx Hash queue configuration.
2199  *
2200  * @param dev
2201  *   Pointer to Ethernet device.
2202  * @param hrxq
2203  *   Index to Hash Rx queue to modify.
2204  * @param rss_key
2205  *   RSS key for the Rx hash queue.
2206  * @param rss_key_len
2207  *   RSS key length.
2208  * @param hash_fields
2209  *   Verbs protocol hash field to make the RSS on.
2210  * @param queues
2211  *   Queues entering in hash queue. In case of empty hash_fields only the
2212  *   first queue index will be taken for the indirection table.
2213  * @param queues_n
2214  *   Number of queues.
2215  *
2216  * @return
2217  *   0 on success, a negative errno value otherwise and rte_errno is set.
2218  */
2219 int
2220 mlx5_hrxq_modify(struct rte_eth_dev *dev, uint32_t hrxq_idx,
2221                  const uint8_t *rss_key, uint32_t rss_key_len,
2222                  uint64_t hash_fields,
2223                  const uint16_t *queues, uint32_t queues_n)
2224 {
2225         int err;
2226         struct mlx5_ind_table_obj *ind_tbl = NULL;
2227         struct mlx5_priv *priv = dev->data->dev_private;
2228         struct mlx5_hrxq *hrxq =
2229                 mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2230         int ret;
2231
2232         if (!hrxq) {
2233                 rte_errno = EINVAL;
2234                 return -rte_errno;
2235         }
2236         /* validations */
2237         if (hrxq->rss_key_len != rss_key_len) {
2238                 /* rss_key_len is fixed size 40 byte & not supposed to change */
2239                 rte_errno = EINVAL;
2240                 return -rte_errno;
2241         }
2242         queues_n = hash_fields ? queues_n : 1;
2243         if (mlx5_ind_table_obj_match_queues(hrxq->ind_table,
2244                                             queues, queues_n)) {
2245                 ind_tbl = hrxq->ind_table;
2246         } else {
2247                 if (hrxq->standalone) {
2248                         /*
2249                          * Replacement of indirection table unsupported for
2250                          * stanalone hrxq objects (used by shared RSS).
2251                          */
2252                         rte_errno = ENOTSUP;
2253                         return -rte_errno;
2254                 }
2255                 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2256                 if (!ind_tbl)
2257                         ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n,
2258                                                          hrxq->standalone);
2259         }
2260         if (!ind_tbl) {
2261                 rte_errno = ENOMEM;
2262                 return -rte_errno;
2263         }
2264         MLX5_ASSERT(priv->obj_ops.hrxq_modify);
2265         ret = priv->obj_ops.hrxq_modify(dev, hrxq, rss_key,
2266                                         hash_fields, ind_tbl);
2267         if (ret) {
2268                 rte_errno = errno;
2269                 goto error;
2270         }
2271         if (ind_tbl != hrxq->ind_table) {
2272                 MLX5_ASSERT(!hrxq->standalone);
2273                 mlx5_ind_table_obj_release(dev, hrxq->ind_table,
2274                                            hrxq->standalone);
2275                 hrxq->ind_table = ind_tbl;
2276         }
2277         hrxq->hash_fields = hash_fields;
2278         memcpy(hrxq->rss_key, rss_key, rss_key_len);
2279         return 0;
2280 error:
2281         err = rte_errno;
2282         if (ind_tbl != hrxq->ind_table) {
2283                 MLX5_ASSERT(!hrxq->standalone);
2284                 mlx5_ind_table_obj_release(dev, ind_tbl, hrxq->standalone);
2285         }
2286         rte_errno = err;
2287         return -rte_errno;
2288 }
2289
2290 static void
2291 __mlx5_hrxq_remove(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
2292 {
2293         struct mlx5_priv *priv = dev->data->dev_private;
2294
2295 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2296         mlx5_glue->destroy_flow_action(hrxq->action);
2297 #endif
2298         priv->obj_ops.hrxq_destroy(hrxq);
2299         if (!hrxq->standalone) {
2300                 mlx5_ind_table_obj_release(dev, hrxq->ind_table,
2301                                            hrxq->standalone);
2302         }
2303         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq->idx);
2304 }
2305
2306 /**
2307  * Release the hash Rx queue.
2308  *
2309  * @param dev
2310  *   Pointer to Ethernet device.
2311  * @param hrxq
2312  *   Index to Hash Rx queue to release.
2313  *
2314  * @param list
2315  *   mlx5 list pointer.
2316  * @param entry
2317  *   Hash queue entry pointer.
2318  */
2319 void
2320 mlx5_hrxq_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
2321 {
2322         struct rte_eth_dev *dev = tool_ctx;
2323         struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2324
2325         __mlx5_hrxq_remove(dev, hrxq);
2326 }
2327
2328 static struct mlx5_hrxq *
2329 __mlx5_hrxq_create(struct rte_eth_dev *dev,
2330                    struct mlx5_flow_rss_desc *rss_desc)
2331 {
2332         struct mlx5_priv *priv = dev->data->dev_private;
2333         const uint8_t *rss_key = rss_desc->key;
2334         uint32_t rss_key_len =  rss_desc->key_len;
2335         bool standalone = !!rss_desc->shared_rss;
2336         const uint16_t *queues =
2337                 standalone ? rss_desc->const_q : rss_desc->queue;
2338         uint32_t queues_n = rss_desc->queue_num;
2339         struct mlx5_hrxq *hrxq = NULL;
2340         uint32_t hrxq_idx = 0;
2341         struct mlx5_ind_table_obj *ind_tbl = rss_desc->ind_tbl;
2342         int ret;
2343
2344         queues_n = rss_desc->hash_fields ? queues_n : 1;
2345         if (!ind_tbl)
2346                 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2347         if (!ind_tbl)
2348                 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n,
2349                                                  standalone);
2350         if (!ind_tbl)
2351                 return NULL;
2352         hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
2353         if (!hrxq)
2354                 goto error;
2355         hrxq->standalone = standalone;
2356         hrxq->idx = hrxq_idx;
2357         hrxq->ind_table = ind_tbl;
2358         hrxq->rss_key_len = rss_key_len;
2359         hrxq->hash_fields = rss_desc->hash_fields;
2360         memcpy(hrxq->rss_key, rss_key, rss_key_len);
2361         ret = priv->obj_ops.hrxq_new(dev, hrxq, rss_desc->tunnel);
2362         if (ret < 0)
2363                 goto error;
2364         return hrxq;
2365 error:
2366         if (!rss_desc->ind_tbl)
2367                 mlx5_ind_table_obj_release(dev, ind_tbl, standalone);
2368         if (hrxq)
2369                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2370         return NULL;
2371 }
2372
2373 struct mlx5_list_entry *
2374 mlx5_hrxq_create_cb(void *tool_ctx, void *cb_ctx)
2375 {
2376         struct rte_eth_dev *dev = tool_ctx;
2377         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
2378         struct mlx5_flow_rss_desc *rss_desc = ctx->data;
2379         struct mlx5_hrxq *hrxq;
2380
2381         hrxq = __mlx5_hrxq_create(dev, rss_desc);
2382         return hrxq ? &hrxq->entry : NULL;
2383 }
2384
2385 struct mlx5_list_entry *
2386 mlx5_hrxq_clone_cb(void *tool_ctx, struct mlx5_list_entry *entry,
2387                     void *cb_ctx __rte_unused)
2388 {
2389         struct rte_eth_dev *dev = tool_ctx;
2390         struct mlx5_priv *priv = dev->data->dev_private;
2391         struct mlx5_hrxq *hrxq;
2392         uint32_t hrxq_idx = 0;
2393
2394         hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
2395         if (!hrxq)
2396                 return NULL;
2397         memcpy(hrxq, entry, sizeof(*hrxq) + MLX5_RSS_HASH_KEY_LEN);
2398         hrxq->idx = hrxq_idx;
2399         return &hrxq->entry;
2400 }
2401
2402 void
2403 mlx5_hrxq_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
2404 {
2405         struct rte_eth_dev *dev = tool_ctx;
2406         struct mlx5_priv *priv = dev->data->dev_private;
2407         struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2408
2409         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq->idx);
2410 }
2411
2412 /**
2413  * Get an Rx Hash queue.
2414  *
2415  * @param dev
2416  *   Pointer to Ethernet device.
2417  * @param rss_desc
2418  *   RSS configuration for the Rx hash queue.
2419  *
2420  * @return
2421  *   An hash Rx queue index on success.
2422  */
2423 uint32_t mlx5_hrxq_get(struct rte_eth_dev *dev,
2424                        struct mlx5_flow_rss_desc *rss_desc)
2425 {
2426         struct mlx5_priv *priv = dev->data->dev_private;
2427         struct mlx5_hrxq *hrxq;
2428         struct mlx5_list_entry *entry;
2429         struct mlx5_flow_cb_ctx ctx = {
2430                 .data = rss_desc,
2431         };
2432
2433         if (rss_desc->shared_rss) {
2434                 hrxq = __mlx5_hrxq_create(dev, rss_desc);
2435         } else {
2436                 entry = mlx5_list_register(priv->hrxqs, &ctx);
2437                 if (!entry)
2438                         return 0;
2439                 hrxq = container_of(entry, typeof(*hrxq), entry);
2440         }
2441         if (hrxq)
2442                 return hrxq->idx;
2443         return 0;
2444 }
2445
2446 /**
2447  * Release the hash Rx queue.
2448  *
2449  * @param dev
2450  *   Pointer to Ethernet device.
2451  * @param hrxq_idx
2452  *   Index to Hash Rx queue to release.
2453  *
2454  * @return
2455  *   1 while a reference on it exists, 0 when freed.
2456  */
2457 int mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hrxq_idx)
2458 {
2459         struct mlx5_priv *priv = dev->data->dev_private;
2460         struct mlx5_hrxq *hrxq;
2461
2462         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2463         if (!hrxq)
2464                 return 0;
2465         if (!hrxq->standalone)
2466                 return mlx5_list_unregister(priv->hrxqs, &hrxq->entry);
2467         __mlx5_hrxq_remove(dev, hrxq);
2468         return 0;
2469 }
2470
2471 /**
2472  * Create a drop Rx Hash queue.
2473  *
2474  * @param dev
2475  *   Pointer to Ethernet device.
2476  *
2477  * @return
2478  *   The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
2479  */
2480 struct mlx5_hrxq *
2481 mlx5_drop_action_create(struct rte_eth_dev *dev)
2482 {
2483         struct mlx5_priv *priv = dev->data->dev_private;
2484         struct mlx5_hrxq *hrxq = NULL;
2485         int ret;
2486
2487         if (priv->drop_queue.hrxq)
2488                 return priv->drop_queue.hrxq;
2489         hrxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq), 0, SOCKET_ID_ANY);
2490         if (!hrxq) {
2491                 DRV_LOG(WARNING,
2492                         "Port %u cannot allocate memory for drop queue.",
2493                         dev->data->port_id);
2494                 rte_errno = ENOMEM;
2495                 goto error;
2496         }
2497         priv->drop_queue.hrxq = hrxq;
2498         hrxq->ind_table = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq->ind_table),
2499                                       0, SOCKET_ID_ANY);
2500         if (!hrxq->ind_table) {
2501                 rte_errno = ENOMEM;
2502                 goto error;
2503         }
2504         ret = priv->obj_ops.drop_action_create(dev);
2505         if (ret < 0)
2506                 goto error;
2507         return hrxq;
2508 error:
2509         if (hrxq) {
2510                 if (hrxq->ind_table)
2511                         mlx5_free(hrxq->ind_table);
2512                 priv->drop_queue.hrxq = NULL;
2513                 mlx5_free(hrxq);
2514         }
2515         return NULL;
2516 }
2517
2518 /**
2519  * Release a drop hash Rx queue.
2520  *
2521  * @param dev
2522  *   Pointer to Ethernet device.
2523  */
2524 void
2525 mlx5_drop_action_destroy(struct rte_eth_dev *dev)
2526 {
2527         struct mlx5_priv *priv = dev->data->dev_private;
2528         struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2529
2530         if (!priv->drop_queue.hrxq)
2531                 return;
2532         priv->obj_ops.drop_action_destroy(dev);
2533         mlx5_free(priv->drop_queue.rxq);
2534         mlx5_free(hrxq->ind_table);
2535         mlx5_free(hrxq);
2536         priv->drop_queue.rxq = NULL;
2537         priv->drop_queue.hrxq = NULL;
2538 }
2539
2540 /**
2541  * Verify the Rx Queue list is empty
2542  *
2543  * @param dev
2544  *   Pointer to Ethernet device.
2545  *
2546  * @return
2547  *   The number of object not released.
2548  */
2549 uint32_t
2550 mlx5_hrxq_verify(struct rte_eth_dev *dev)
2551 {
2552         struct mlx5_priv *priv = dev->data->dev_private;
2553
2554         return mlx5_list_get_entry_num(priv->hrxqs);
2555 }
2556
2557 /**
2558  * Set the Rx queue timestamp conversion parameters
2559  *
2560  * @param[in] dev
2561  *   Pointer to the Ethernet device structure.
2562  */
2563 void
2564 mlx5_rxq_timestamp_set(struct rte_eth_dev *dev)
2565 {
2566         struct mlx5_priv *priv = dev->data->dev_private;
2567         struct mlx5_dev_ctx_shared *sh = priv->sh;
2568         struct mlx5_rxq_data *data;
2569         unsigned int i;
2570
2571         for (i = 0; i != priv->rxqs_n; ++i) {
2572                 if (!(*priv->rxqs)[i])
2573                         continue;
2574                 data = (*priv->rxqs)[i];
2575                 data->sh = sh;
2576                 data->rt_timestamp = priv->config.rt_timestamp;
2577         }
2578 }