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