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