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