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