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