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