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