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