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