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