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