New upstream version 17.11.5
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <fcntl.h>
40 #include <sys/queue.h>
41
42 /* Verbs header. */
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #include <infiniband/mlx5dv.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_mbuf.h>
54 #include <rte_malloc.h>
55 #include <rte_ethdev.h>
56 #include <rte_common.h>
57 #include <rte_interrupts.h>
58 #include <rte_debug.h>
59 #include <rte_io.h>
60
61 #include "mlx5.h"
62 #include "mlx5_rxtx.h"
63 #include "mlx5_utils.h"
64 #include "mlx5_autoconf.h"
65 #include "mlx5_defs.h"
66
67 /* Default RSS hash key also used for ConnectX-3. */
68 uint8_t rss_hash_default_key[] = {
69         0x2c, 0xc6, 0x81, 0xd1,
70         0x5b, 0xdb, 0xf4, 0xf7,
71         0xfc, 0xa2, 0x83, 0x19,
72         0xdb, 0x1a, 0x3e, 0x94,
73         0x6b, 0x9e, 0x38, 0xd9,
74         0x2c, 0x9c, 0x03, 0xd1,
75         0xad, 0x99, 0x44, 0xa7,
76         0xd9, 0x56, 0x3d, 0x59,
77         0x06, 0x3c, 0x25, 0xf3,
78         0xfc, 0x1f, 0xdc, 0x2a,
79 };
80
81 /* Length of the default RSS hash key. */
82 const size_t rss_hash_default_key_len = sizeof(rss_hash_default_key);
83
84 /**
85  * Allocate RX queue elements.
86  *
87  * @param rxq_ctrl
88  *   Pointer to RX queue structure.
89  *
90  * @return
91  *   0 on success, a negative errno value otherwise and rte_errno is set.
92  */
93 int
94 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
95 {
96         const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
97         unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
98         unsigned int i;
99         int err;
100
101         /* Iterate on segments. */
102         for (i = 0; (i != elts_n); ++i) {
103                 struct rte_mbuf *buf;
104
105                 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
106                 if (buf == NULL) {
107                         DRV_LOG(ERR, "port %u empty mbuf pool",
108                                 PORT_ID(rxq_ctrl->priv));
109                         rte_errno = ENOMEM;
110                         goto error;
111                 }
112                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
113                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
114                 /* Buffer is supposed to be empty. */
115                 assert(rte_pktmbuf_data_len(buf) == 0);
116                 assert(rte_pktmbuf_pkt_len(buf) == 0);
117                 assert(!buf->next);
118                 /* Only the first segment keeps headroom. */
119                 if (i % sges_n)
120                         SET_DATA_OFF(buf, 0);
121                 PORT(buf) = rxq_ctrl->rxq.port_id;
122                 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
123                 PKT_LEN(buf) = DATA_LEN(buf);
124                 NB_SEGS(buf) = 1;
125                 (*rxq_ctrl->rxq.elts)[i] = buf;
126         }
127         /* If Rx vector is activated. */
128         if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
129                 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
130                 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
131                 int j;
132
133                 /* Initialize default rearm_data for vPMD. */
134                 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
135                 rte_mbuf_refcnt_set(mbuf_init, 1);
136                 mbuf_init->nb_segs = 1;
137                 mbuf_init->port = rxq->port_id;
138                 /*
139                  * prevent compiler reordering:
140                  * rearm_data covers previous fields.
141                  */
142                 rte_compiler_barrier();
143                 rxq->mbuf_initializer =
144                         *(uint64_t *)&mbuf_init->rearm_data;
145                 /* Padding with a fake mbuf for vectorized Rx. */
146                 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
147                         (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
148         }
149         DRV_LOG(DEBUG,
150                 "port %u Rx queue %u allocated and configured %u segments"
151                 " (max %u packets)",
152                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx, elts_n,
153                 elts_n / (1 << rxq_ctrl->rxq.sges_n));
154         return 0;
155 error:
156         err = rte_errno; /* Save rte_errno before cleanup. */
157         elts_n = i;
158         for (i = 0; (i != elts_n); ++i) {
159                 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
160                         rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
161                 (*rxq_ctrl->rxq.elts)[i] = NULL;
162         }
163         DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
164                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
165         rte_errno = err; /* Restore rte_errno. */
166         return -rte_errno;
167 }
168
169 /**
170  * Free RX queue elements.
171  *
172  * @param rxq_ctrl
173  *   Pointer to RX queue structure.
174  */
175 static void
176 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
177 {
178         struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
179         const uint16_t q_n = (1 << rxq->elts_n);
180         const uint16_t q_mask = q_n - 1;
181         uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
182         uint16_t i;
183
184         DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
185                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
186         if (rxq->elts == NULL)
187                 return;
188         /**
189          * Some mbuf in the Ring belongs to the application.  They cannot be
190          * freed.
191          */
192         if (mlx5_rxq_check_vec_support(rxq) > 0) {
193                 for (i = 0; i < used; ++i)
194                         (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
195                 rxq->rq_pi = rxq->rq_ci;
196         }
197         for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
198                 if ((*rxq->elts)[i] != NULL)
199                         rte_pktmbuf_free_seg((*rxq->elts)[i]);
200                 (*rxq->elts)[i] = NULL;
201         }
202 }
203
204 /**
205  * Clean up a RX queue.
206  *
207  * Destroy objects, free allocated memory and reset the structure for reuse.
208  *
209  * @param rxq_ctrl
210  *   Pointer to RX queue structure.
211  */
212 void
213 mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *rxq_ctrl)
214 {
215         DRV_LOG(DEBUG, "port %u cleaning up Rx queue %u",
216                 PORT_ID(rxq_ctrl->priv), rxq_ctrl->idx);
217         if (rxq_ctrl->ibv)
218                 mlx5_rxq_ibv_release(rxq_ctrl->ibv);
219         memset(rxq_ctrl, 0, sizeof(*rxq_ctrl));
220 }
221
222 /**
223  *
224  * @param dev
225  *   Pointer to Ethernet device structure.
226  * @param idx
227  *   RX queue index.
228  * @param desc
229  *   Number of descriptors to configure in queue.
230  * @param socket
231  *   NUMA socket on which memory must be allocated.
232  * @param[in] conf
233  *   Thresholds parameters.
234  * @param mp
235  *   Memory pool for buffer allocations.
236  *
237  * @return
238  *   0 on success, a negative errno value otherwise and rte_errno is set.
239  */
240 int
241 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
242                     unsigned int socket,
243                     const struct rte_eth_rxconf *conf __rte_unused,
244                     struct rte_mempool *mp)
245 {
246         struct priv *priv = dev->data->dev_private;
247         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
248         struct mlx5_rxq_ctrl *rxq_ctrl =
249                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
250
251         if (!rte_is_power_of_2(desc)) {
252                 desc = 1 << log2above(desc);
253                 DRV_LOG(WARNING,
254                         "port %u increased number of descriptors in Rx queue %u"
255                         " to the next power of two (%d)",
256                         dev->data->port_id, idx, desc);
257         }
258         DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
259                 dev->data->port_id, idx, desc);
260         if (idx >= priv->rxqs_n) {
261                 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
262                         dev->data->port_id, idx, priv->rxqs_n);
263                 rte_errno = EOVERFLOW;
264                 return -rte_errno;
265         }
266         if (!mlx5_rxq_releasable(dev, idx)) {
267                 DRV_LOG(ERR, "port %u unable to release queue index %u",
268                         dev->data->port_id, idx);
269                 rte_errno = EBUSY;
270                 return -rte_errno;
271         }
272         mlx5_rxq_release(dev, idx);
273         rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, mp);
274         if (!rxq_ctrl) {
275                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
276                         dev->data->port_id, idx);
277                 rte_errno = ENOMEM;
278                 return -rte_errno;
279         }
280         DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
281                 dev->data->port_id, idx);
282         (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
283         return 0;
284 }
285
286 /**
287  * DPDK callback to release a RX queue.
288  *
289  * @param dpdk_rxq
290  *   Generic RX queue pointer.
291  */
292 void
293 mlx5_rx_queue_release(void *dpdk_rxq)
294 {
295         struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
296         struct mlx5_rxq_ctrl *rxq_ctrl;
297         struct priv *priv;
298
299         if (rxq == NULL)
300                 return;
301         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
302         priv = rxq_ctrl->priv;
303         if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.stats.idx))
304                 rte_panic("port %u Rx queue %u is still used by a flow and"
305                           " cannot be removed\n",
306                           PORT_ID(priv), rxq_ctrl->idx);
307         mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.stats.idx);
308 }
309
310 /**
311  * Allocate queue vector and fill epoll fd list for Rx interrupts.
312  *
313  * @param dev
314  *   Pointer to Ethernet device.
315  *
316  * @return
317  *   0 on success, a negative errno value otherwise and rte_errno is set.
318  */
319 int
320 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
321 {
322         struct priv *priv = dev->data->dev_private;
323         unsigned int i;
324         unsigned int rxqs_n = priv->rxqs_n;
325         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
326         unsigned int count = 0;
327         struct rte_intr_handle *intr_handle = dev->intr_handle;
328
329         if (!dev->data->dev_conf.intr_conf.rxq)
330                 return 0;
331         mlx5_rx_intr_vec_disable(dev);
332         intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
333         if (intr_handle->intr_vec == NULL) {
334                 DRV_LOG(ERR,
335                         "port %u failed to allocate memory for interrupt"
336                         " vector, Rx interrupts will not be supported",
337                         dev->data->port_id);
338                 rte_errno = ENOMEM;
339                 return -rte_errno;
340         }
341         intr_handle->type = RTE_INTR_HANDLE_EXT;
342         for (i = 0; i != n; ++i) {
343                 /* This rxq ibv must not be released in this function. */
344                 struct mlx5_rxq_ibv *rxq_ibv = mlx5_rxq_ibv_get(dev, i);
345                 int fd;
346                 int flags;
347                 int rc;
348
349                 /* Skip queues that cannot request interrupts. */
350                 if (!rxq_ibv || !rxq_ibv->channel) {
351                         /* Use invalid intr_vec[] index to disable entry. */
352                         intr_handle->intr_vec[i] =
353                                 RTE_INTR_VEC_RXTX_OFFSET +
354                                 RTE_MAX_RXTX_INTR_VEC_ID;
355                         continue;
356                 }
357                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
358                         DRV_LOG(ERR,
359                                 "port %u too many Rx queues for interrupt"
360                                 " vector size (%d), Rx interrupts cannot be"
361                                 " enabled",
362                                 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
363                         mlx5_rx_intr_vec_disable(dev);
364                         rte_errno = ENOMEM;
365                         return -rte_errno;
366                 }
367                 fd = rxq_ibv->channel->fd;
368                 flags = fcntl(fd, F_GETFL);
369                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
370                 if (rc < 0) {
371                         rte_errno = errno;
372                         DRV_LOG(ERR,
373                                 "port %u failed to make Rx interrupt file"
374                                 " descriptor %d non-blocking for queue index"
375                                 " %d",
376                                 dev->data->port_id, fd, i);
377                         mlx5_rx_intr_vec_disable(dev);
378                         return -rte_errno;
379                 }
380                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
381                 intr_handle->efds[count] = fd;
382                 count++;
383         }
384         if (!count)
385                 mlx5_rx_intr_vec_disable(dev);
386         else
387                 intr_handle->nb_efd = count;
388         return 0;
389 }
390
391 /**
392  * Clean up Rx interrupts handler.
393  *
394  * @param dev
395  *   Pointer to Ethernet device.
396  */
397 void
398 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
399 {
400         struct priv *priv = dev->data->dev_private;
401         struct rte_intr_handle *intr_handle = dev->intr_handle;
402         unsigned int i;
403         unsigned int rxqs_n = priv->rxqs_n;
404         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
405
406         if (!dev->data->dev_conf.intr_conf.rxq)
407                 return;
408         if (!intr_handle->intr_vec)
409                 goto free;
410         for (i = 0; i != n; ++i) {
411                 struct mlx5_rxq_ctrl *rxq_ctrl;
412                 struct mlx5_rxq_data *rxq_data;
413
414                 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
415                     RTE_MAX_RXTX_INTR_VEC_ID)
416                         continue;
417                 /**
418                  * Need to access directly the queue to release the reference
419                  * kept in priv_rx_intr_vec_enable().
420                  */
421                 rxq_data = (*priv->rxqs)[i];
422                 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
423                 mlx5_rxq_ibv_release(rxq_ctrl->ibv);
424         }
425 free:
426         rte_intr_free_epoll_fd(intr_handle);
427         if (intr_handle->intr_vec)
428                 free(intr_handle->intr_vec);
429         intr_handle->nb_efd = 0;
430         intr_handle->intr_vec = NULL;
431 }
432
433 /**
434  *  MLX5 CQ notification .
435  *
436  *  @param rxq
437  *     Pointer to receive queue structure.
438  *  @param sq_n_rxq
439  *     Sequence number per receive queue .
440  */
441 static inline void
442 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
443 {
444         int sq_n = 0;
445         uint32_t doorbell_hi;
446         uint64_t doorbell;
447         void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
448
449         sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
450         doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
451         doorbell = (uint64_t)doorbell_hi << 32;
452         doorbell |=  rxq->cqn;
453         rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
454         rte_wmb();
455         rte_write64(rte_cpu_to_be_64(doorbell), cq_db_reg);
456 }
457
458 /**
459  * DPDK callback for Rx queue interrupt enable.
460  *
461  * @param dev
462  *   Pointer to Ethernet device structure.
463  * @param rx_queue_id
464  *   Rx queue number.
465  *
466  * @return
467  *   0 on success, a negative errno value otherwise and rte_errno is set.
468  */
469 int
470 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
471 {
472         struct priv *priv = dev->data->dev_private;
473         struct mlx5_rxq_data *rxq_data;
474         struct mlx5_rxq_ctrl *rxq_ctrl;
475
476         rxq_data = (*priv->rxqs)[rx_queue_id];
477         if (!rxq_data) {
478                 rte_errno = EINVAL;
479                 return -rte_errno;
480         }
481         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
482         if (rxq_ctrl->irq) {
483                 struct mlx5_rxq_ibv *rxq_ibv;
484
485                 rxq_ibv = mlx5_rxq_ibv_get(dev, rx_queue_id);
486                 if (!rxq_ibv) {
487                         rte_errno = EINVAL;
488                         return -rte_errno;
489                 }
490                 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
491                 mlx5_rxq_ibv_release(rxq_ibv);
492         }
493         return 0;
494 }
495
496 /**
497  * DPDK callback for Rx queue interrupt disable.
498  *
499  * @param dev
500  *   Pointer to Ethernet device structure.
501  * @param rx_queue_id
502  *   Rx queue number.
503  *
504  * @return
505  *   0 on success, a negative errno value otherwise and rte_errno is set.
506  */
507 int
508 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
509 {
510         struct priv *priv = dev->data->dev_private;
511         struct mlx5_rxq_data *rxq_data;
512         struct mlx5_rxq_ctrl *rxq_ctrl;
513         struct mlx5_rxq_ibv *rxq_ibv = NULL;
514         struct ibv_cq *ev_cq;
515         void *ev_ctx;
516         int ret;
517
518         rxq_data = (*priv->rxqs)[rx_queue_id];
519         if (!rxq_data) {
520                 rte_errno = EINVAL;
521                 return -rte_errno;
522         }
523         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
524         if (!rxq_ctrl->irq)
525                 return 0;
526         rxq_ibv = mlx5_rxq_ibv_get(dev, rx_queue_id);
527         if (!rxq_ibv) {
528                 rte_errno = EINVAL;
529                 return -rte_errno;
530         }
531         ret = ibv_get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
532         if (ret || ev_cq != rxq_ibv->cq) {
533                 rte_errno = EINVAL;
534                 goto exit;
535         }
536         rxq_data->cq_arm_sn++;
537         ibv_ack_cq_events(rxq_ibv->cq, 1);
538         return 0;
539 exit:
540         ret = rte_errno; /* Save rte_errno before cleanup. */
541         if (rxq_ibv)
542                 mlx5_rxq_ibv_release(rxq_ibv);
543         DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
544                 dev->data->port_id, rx_queue_id);
545         rte_errno = ret; /* Restore rte_errno. */
546         return -rte_errno;
547 }
548
549 /**
550  * Create the Rx queue Verbs object.
551  *
552  * @param dev
553  *   Pointer to Ethernet device.
554  * @param idx
555  *   Queue index in DPDK Rx queue array
556  *
557  * @return
558  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
559  */
560 struct mlx5_rxq_ibv *
561 mlx5_rxq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
562 {
563         struct priv *priv = dev->data->dev_private;
564         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
565         struct mlx5_rxq_ctrl *rxq_ctrl =
566                 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
567         struct ibv_wq_attr mod;
568         union {
569                 struct {
570                         struct ibv_cq_init_attr_ex ibv;
571                         struct mlx5dv_cq_init_attr mlx5;
572                 } cq;
573                 struct ibv_wq_init_attr wq;
574                 struct ibv_cq_ex cq_attr;
575         } attr;
576         unsigned int cqe_n = (1 << rxq_data->elts_n) - 1;
577         struct mlx5_rxq_ibv *tmpl;
578         struct mlx5dv_cq cq_info;
579         struct mlx5dv_rwq rwq;
580         unsigned int i;
581         int ret = 0;
582         struct mlx5dv_obj obj;
583
584         assert(rxq_data);
585         assert(!rxq_ctrl->ibv);
586         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
587         priv->verbs_alloc_ctx.obj = rxq_ctrl;
588         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
589                                  rxq_ctrl->socket);
590         if (!tmpl) {
591                 DRV_LOG(ERR,
592                         "port %u Rx queue %u cannot allocate verbs resources",
593                         dev->data->port_id, rxq_ctrl->idx);
594                 rte_errno = ENOMEM;
595                 goto error;
596         }
597         tmpl->rxq_ctrl = rxq_ctrl;
598         if (rxq_ctrl->irq) {
599                 tmpl->channel = ibv_create_comp_channel(priv->ctx);
600                 if (!tmpl->channel) {
601                         DRV_LOG(ERR, "port %u: comp channel creation failure",
602                                 dev->data->port_id);
603                         rte_errno = ENOMEM;
604                         goto error;
605                 }
606         }
607         attr.cq.ibv = (struct ibv_cq_init_attr_ex){
608                 .cqe = cqe_n,
609                 .channel = tmpl->channel,
610                 .comp_mask = 0,
611         };
612         attr.cq.mlx5 = (struct mlx5dv_cq_init_attr){
613                 .comp_mask = 0,
614         };
615         if (priv->cqe_comp && !rxq_data->hw_timestamp) {
616                 attr.cq.mlx5.comp_mask |=
617                         MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
618                 attr.cq.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
619                 /*
620                  * For vectorized Rx, it must not be doubled in order to
621                  * make cq_ci and rq_ci aligned.
622                  */
623                 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
624                         attr.cq.ibv.cqe *= 2;
625         } else if (priv->cqe_comp && rxq_data->hw_timestamp) {
626                 DRV_LOG(DEBUG,
627                         "port %u Rx CQE compression is disabled for HW"
628                         " timestamp",
629                         dev->data->port_id);
630         }
631         tmpl->cq = ibv_cq_ex_to_cq(mlx5dv_create_cq(priv->ctx, &attr.cq.ibv,
632                                                     &attr.cq.mlx5));
633         if (tmpl->cq == NULL) {
634                 DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
635                         dev->data->port_id, idx);
636                 rte_errno = ENOMEM;
637                 goto error;
638         }
639         DRV_LOG(DEBUG, "port %u priv->device_attr.max_qp_wr is %d",
640                 dev->data->port_id, priv->device_attr.orig_attr.max_qp_wr);
641         DRV_LOG(DEBUG, "port %u priv->device_attr.max_sge is %d",
642                 dev->data->port_id, priv->device_attr.orig_attr.max_sge);
643         attr.wq = (struct ibv_wq_init_attr){
644                 .wq_context = NULL, /* Could be useful in the future. */
645                 .wq_type = IBV_WQT_RQ,
646                 /* Max number of outstanding WRs. */
647                 .max_wr = (1 << rxq_data->elts_n) >> rxq_data->sges_n,
648                 /* Max number of scatter/gather elements in a WR. */
649                 .max_sge = 1 << rxq_data->sges_n,
650                 .pd = priv->pd,
651                 .cq = tmpl->cq,
652                 .comp_mask =
653                         IBV_WQ_FLAGS_CVLAN_STRIPPING |
654                         0,
655                 .create_flags = (rxq_data->vlan_strip ?
656                                  IBV_WQ_FLAGS_CVLAN_STRIPPING :
657                                  0),
658         };
659         /* By default, FCS (CRC) is stripped by hardware. */
660         if (rxq_data->crc_present) {
661                 attr.wq.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
662                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
663         }
664 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
665         if (priv->hw_padding) {
666                 attr.wq.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
667                 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
668         }
669 #endif
670         tmpl->wq = ibv_create_wq(priv->ctx, &attr.wq);
671         if (tmpl->wq == NULL) {
672                 DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
673                         dev->data->port_id, idx);
674                 rte_errno = ENOMEM;
675                 goto error;
676         }
677         /*
678          * Make sure number of WRs*SGEs match expectations since a queue
679          * cannot allocate more than "desc" buffers.
680          */
681         if (((int)attr.wq.max_wr !=
682              ((1 << rxq_data->elts_n) >> rxq_data->sges_n)) ||
683             ((int)attr.wq.max_sge != (1 << rxq_data->sges_n))) {
684                 DRV_LOG(ERR,
685                         "port %u Rx queue %u requested %u*%u but got %u*%u"
686                         " WRs*SGEs",
687                         dev->data->port_id, idx,
688                         ((1 << rxq_data->elts_n) >> rxq_data->sges_n),
689                         (1 << rxq_data->sges_n),
690                         attr.wq.max_wr, attr.wq.max_sge);
691                 rte_errno = EINVAL;
692                 goto error;
693         }
694         /* Change queue state to ready. */
695         mod = (struct ibv_wq_attr){
696                 .attr_mask = IBV_WQ_ATTR_STATE,
697                 .wq_state = IBV_WQS_RDY,
698         };
699         ret = ibv_modify_wq(tmpl->wq, &mod);
700         if (ret) {
701                 DRV_LOG(ERR,
702                         "port %u Rx queue %u WQ state to IBV_WQS_RDY failed",
703                         dev->data->port_id, idx);
704                 rte_errno = ret;
705                 goto error;
706         }
707         obj.cq.in = tmpl->cq;
708         obj.cq.out = &cq_info;
709         obj.rwq.in = tmpl->wq;
710         obj.rwq.out = &rwq;
711         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
712         if (ret) {
713                 rte_errno = ret;
714                 goto error;
715         }
716         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
717                 DRV_LOG(ERR,
718                         "port %u wrong MLX5_CQE_SIZE environment variable"
719                         " value: it should be set to %u",
720                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
721                 rte_errno = EINVAL;
722                 goto error;
723         }
724         /* Fill the rings. */
725         rxq_data->wqes = (volatile struct mlx5_wqe_data_seg (*)[])
726                 (uintptr_t)rwq.buf;
727         for (i = 0; (i != (unsigned int)(1 << rxq_data->elts_n)); ++i) {
728                 struct rte_mbuf *buf = (*rxq_data->elts)[i];
729                 volatile struct mlx5_wqe_data_seg *scat = &(*rxq_data->wqes)[i];
730                 uintptr_t addr = rte_pktmbuf_mtod(buf, uintptr_t);
731
732                 /* scat->addr must be able to store a pointer. */
733                 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
734                 *scat = (struct mlx5_wqe_data_seg){
735                         .addr = rte_cpu_to_be_64(addr),
736                         .byte_count = rte_cpu_to_be_32(DATA_LEN(buf)),
737                         .lkey = mlx5_rx_mb2mr(rxq_data, buf)
738                 };
739         }
740         rxq_data->rq_db = rwq.dbrec;
741         rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
742         rxq_data->cq_ci = 0;
743         rxq_data->rq_ci = 0;
744         rxq_data->rq_pi = 0;
745         rxq_data->zip = (struct rxq_zip){
746                 .ai = 0,
747         };
748         rxq_data->cq_db = cq_info.dbrec;
749         rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
750         rxq_data->cq_uar = cq_info.cq_uar;
751         rxq_data->cqn = cq_info.cqn;
752         rxq_data->cq_arm_sn = 0;
753         /* Update doorbell counter. */
754         rxq_data->rq_ci = (1 << rxq_data->elts_n) >> rxq_data->sges_n;
755         rte_wmb();
756         *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
757         DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
758                 idx, (void *)&tmpl);
759         rte_atomic32_inc(&tmpl->refcnt);
760         DRV_LOG(DEBUG, "port %u Verbs Rx queue %u: refcnt %d",
761                 dev->data->port_id, idx, rte_atomic32_read(&tmpl->refcnt));
762         LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
763         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
764         return tmpl;
765 error:
766         ret = rte_errno; /* Save rte_errno before cleanup. */
767         if (tmpl->wq)
768                 claim_zero(ibv_destroy_wq(tmpl->wq));
769         if (tmpl->cq)
770                 claim_zero(ibv_destroy_cq(tmpl->cq));
771         if (tmpl->channel)
772                 claim_zero(ibv_destroy_comp_channel(tmpl->channel));
773         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
774         rte_errno = ret; /* Restore rte_errno. */
775         return NULL;
776 }
777
778 /**
779  * Get an Rx queue Verbs object.
780  *
781  * @param dev
782  *   Pointer to Ethernet device.
783  * @param idx
784  *   Queue index in DPDK Rx queue array
785  *
786  * @return
787  *   The Verbs object if it exists.
788  */
789 struct mlx5_rxq_ibv *
790 mlx5_rxq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
791 {
792         struct priv *priv = dev->data->dev_private;
793         struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
794         struct mlx5_rxq_ctrl *rxq_ctrl;
795
796         if (idx >= priv->rxqs_n)
797                 return NULL;
798         if (!rxq_data)
799                 return NULL;
800         rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
801         if (rxq_ctrl->ibv) {
802                 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
803                 DRV_LOG(DEBUG, "port %u Verbs Rx queue %u: refcnt %d",
804                         dev->data->port_id, rxq_ctrl->idx,
805                         rte_atomic32_read(&rxq_ctrl->ibv->refcnt));
806         }
807         return rxq_ctrl->ibv;
808 }
809
810 /**
811  * Release an Rx verbs queue object.
812  *
813  * @param rxq_ibv
814  *   Verbs Rx queue object.
815  *
816  * @return
817  *   1 while a reference on it exists, 0 when freed.
818  */
819 int
820 mlx5_rxq_ibv_release(struct mlx5_rxq_ibv *rxq_ibv)
821 {
822         assert(rxq_ibv);
823         assert(rxq_ibv->wq);
824         assert(rxq_ibv->cq);
825         DRV_LOG(DEBUG, "port %u Verbs Rx queue %u: refcnt %d",
826                 PORT_ID(rxq_ibv->rxq_ctrl->priv),
827                 rxq_ibv->rxq_ctrl->idx, rte_atomic32_read(&rxq_ibv->refcnt));
828         if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
829                 rxq_free_elts(rxq_ibv->rxq_ctrl);
830                 claim_zero(ibv_destroy_wq(rxq_ibv->wq));
831                 claim_zero(ibv_destroy_cq(rxq_ibv->cq));
832                 if (rxq_ibv->channel)
833                         claim_zero(ibv_destroy_comp_channel(rxq_ibv->channel));
834                 LIST_REMOVE(rxq_ibv, next);
835                 rte_free(rxq_ibv);
836                 return 0;
837         }
838         return 1;
839 }
840
841 /**
842  * Verify the Verbs Rx queue list is empty
843  *
844  * @param dev
845  *   Pointer to Ethernet device.
846  *
847  * @return
848  *   The number of object not released.
849  */
850 int
851 mlx5_rxq_ibv_verify(struct rte_eth_dev *dev)
852 {
853         struct priv *priv = dev->data->dev_private;
854         int ret = 0;
855         struct mlx5_rxq_ibv *rxq_ibv;
856
857         LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
858                 DRV_LOG(DEBUG, "port %u Verbs Rx queue %u still referenced",
859                         dev->data->port_id, rxq_ibv->rxq_ctrl->idx);
860                 ++ret;
861         }
862         return ret;
863 }
864
865 /**
866  * Return true if a single reference exists on the object.
867  *
868  * @param rxq_ibv
869  *   Verbs Rx queue object.
870  */
871 int
872 mlx5_rxq_ibv_releasable(struct mlx5_rxq_ibv *rxq_ibv)
873 {
874         assert(rxq_ibv);
875         return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
876 }
877
878 /**
879  * Create a DPDK Rx queue.
880  *
881  * @param dev
882  *   Pointer to Ethernet device.
883  * @param idx
884  *   TX queue index.
885  * @param desc
886  *   Number of descriptors to configure in queue.
887  * @param socket
888  *   NUMA socket on which memory must be allocated.
889  *
890  * @return
891  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
892  */
893 struct mlx5_rxq_ctrl *
894 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
895              unsigned int socket, struct rte_mempool *mp)
896 {
897         struct priv *priv = dev->data->dev_private;
898         struct mlx5_rxq_ctrl *tmpl;
899         const uint16_t desc_n =
900                 desc + priv->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
901         unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
902         const unsigned int mr_n = MR_TABLE_SZ(priv->mr_n);
903
904         tmpl = rte_calloc_socket("RXQ", 1,
905                                  sizeof(*tmpl) +
906                                  desc_n * sizeof(struct rte_mbuf *) +
907                                  mr_n * sizeof(struct mlx5_mr_cache),
908                                  0, socket);
909         if (!tmpl) {
910                 rte_errno = ENOMEM;
911                 return NULL;
912         }
913         tmpl->socket = socket;
914         if (dev->data->dev_conf.intr_conf.rxq)
915                 tmpl->irq = 1;
916         /* Enable scattered packets support for this queue if necessary. */
917         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
918         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
919             (mb_len - RTE_PKTMBUF_HEADROOM)) {
920                 tmpl->rxq.sges_n = 0;
921         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
922                 unsigned int size =
923                         RTE_PKTMBUF_HEADROOM +
924                         dev->data->dev_conf.rxmode.max_rx_pkt_len;
925                 unsigned int sges_n;
926
927                 /*
928                  * Determine the number of SGEs needed for a full packet
929                  * and round it to the next power of two.
930                  */
931                 sges_n = log2above((size / mb_len) + !!(size % mb_len));
932                 tmpl->rxq.sges_n = sges_n;
933                 /* Make sure rxq.sges_n did not overflow. */
934                 size = mb_len * (1 << tmpl->rxq.sges_n);
935                 size -= RTE_PKTMBUF_HEADROOM;
936                 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
937                         DRV_LOG(ERR,
938                                 "port %u too many SGEs (%u) needed to handle"
939                                 " requested maximum packet size %u",
940                                 dev->data->port_id,
941                                 1 << sges_n,
942                                 dev->data->dev_conf.rxmode.max_rx_pkt_len);
943                         rte_errno = EOVERFLOW;
944                         goto error;
945                 }
946         } else {
947                 DRV_LOG(WARNING,
948                         "port %u the requested maximum Rx packet size (%u) is"
949                         " larger than a single mbuf (%u) and scattered mode has"
950                         " not been requested",
951                         dev->data->port_id,
952                         dev->data->dev_conf.rxmode.max_rx_pkt_len,
953                         mb_len - RTE_PKTMBUF_HEADROOM);
954         }
955         DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
956                 dev->data->port_id, 1 << tmpl->rxq.sges_n);
957         if (desc % (1 << tmpl->rxq.sges_n)) {
958                 DRV_LOG(ERR,
959                         "port %u number of Rx queue descriptors (%u) is not a"
960                         " multiple of SGEs per packet (%u)",
961                         dev->data->port_id,
962                         desc,
963                         1 << tmpl->rxq.sges_n);
964                 rte_errno = EINVAL;
965                 goto error;
966         }
967         /* Toggle RX checksum offload if hardware supports it. */
968         if (priv->hw_csum)
969                 tmpl->rxq.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
970         if (priv->hw_csum_l2tun)
971                 tmpl->rxq.csum_l2tun =
972                         !!dev->data->dev_conf.rxmode.hw_ip_checksum;
973         tmpl->rxq.hw_timestamp =
974                         !!dev->data->dev_conf.rxmode.hw_timestamp;
975         /* Configure VLAN stripping. */
976         tmpl->rxq.vlan_strip = (priv->hw_vlan_strip &&
977                                !!dev->data->dev_conf.rxmode.hw_vlan_strip);
978         /* By default, FCS (CRC) is stripped by hardware. */
979         if (dev->data->dev_conf.rxmode.hw_strip_crc) {
980                 tmpl->rxq.crc_present = 0;
981         } else if (priv->hw_fcs_strip) {
982                 tmpl->rxq.crc_present = 1;
983         } else {
984                 DRV_LOG(WARNING,
985                         "port %u CRC stripping has been disabled but will"
986                         " still be performed by hardware, make sure MLNX_OFED"
987                         " and firmware are up to date",
988                         dev->data->port_id);
989                 tmpl->rxq.crc_present = 0;
990         }
991         DRV_LOG(DEBUG,
992                 "port %u CRC stripping is %s, %u bytes will be subtracted from"
993                 " incoming frames to hide it",
994                 dev->data->port_id,
995                 tmpl->rxq.crc_present ? "disabled" : "enabled",
996                 tmpl->rxq.crc_present << 2);
997         /* Save port ID. */
998         tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
999                 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
1000         tmpl->rxq.port_id = dev->data->port_id;
1001         tmpl->priv = priv;
1002         tmpl->rxq.mp = mp;
1003         tmpl->rxq.stats.idx = idx;
1004         tmpl->rxq.elts_n = log2above(desc);
1005         tmpl->rxq.rq_repl_thresh =
1006                 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
1007         tmpl->rxq.elts =
1008                 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1009         tmpl->rxq.mr_ctrl.cache_bh =
1010                 (struct mlx5_mr_cache (*)[mr_n])&(*tmpl->rxq.elts)[desc_n];
1011         tmpl->rxq.mr_ctrl.bh_n =
1012                 mlx5_mr_update_mp(dev, *tmpl->rxq.mr_ctrl.cache_bh,
1013                                   tmpl->rxq.mr_ctrl.bh_n, mp);
1014         DRV_LOG(DEBUG, "Rx MR lookup table: %u entires built",
1015                 MR_N(tmpl->rxq.mr_ctrl.bh_n));
1016         tmpl->idx = idx;
1017         rte_atomic32_inc(&tmpl->refcnt);
1018         DRV_LOG(DEBUG, "port %u Rx queue %u: refcnt %d", dev->data->port_id,
1019                 idx, rte_atomic32_read(&tmpl->refcnt));
1020         LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1021         return tmpl;
1022 error:
1023         rte_free(tmpl);
1024         return NULL;
1025 }
1026
1027 /**
1028  * Get a Rx queue.
1029  *
1030  * @param dev
1031  *   Pointer to Ethernet device.
1032  * @param idx
1033  *   TX queue index.
1034  *
1035  * @return
1036  *   A pointer to the queue if it exists, NULL otherwise.
1037  */
1038 struct mlx5_rxq_ctrl *
1039 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1040 {
1041         struct priv *priv = dev->data->dev_private;
1042         struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1043
1044         if ((*priv->rxqs)[idx]) {
1045                 rxq_ctrl = container_of((*priv->rxqs)[idx],
1046                                         struct mlx5_rxq_ctrl,
1047                                         rxq);
1048                 mlx5_rxq_ibv_get(dev, idx);
1049                 rte_atomic32_inc(&rxq_ctrl->refcnt);
1050                 DRV_LOG(DEBUG, "port %u Rx queue %u: refcnt %d",
1051                         dev->data->port_id, rxq_ctrl->idx,
1052                         rte_atomic32_read(&rxq_ctrl->refcnt));
1053         }
1054         return rxq_ctrl;
1055 }
1056
1057 /**
1058  * Release a Rx queue.
1059  *
1060  * @param dev
1061  *   Pointer to Ethernet device.
1062  * @param idx
1063  *   TX queue index.
1064  *
1065  * @return
1066  *   1 while a reference on it exists, 0 when freed.
1067  */
1068 int
1069 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
1070 {
1071         struct priv *priv = dev->data->dev_private;
1072         struct mlx5_rxq_ctrl *rxq_ctrl;
1073
1074         if (!(*priv->rxqs)[idx])
1075                 return 0;
1076         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1077         assert(rxq_ctrl->priv);
1078         if (rxq_ctrl->ibv && !mlx5_rxq_ibv_release(rxq_ctrl->ibv))
1079                 rxq_ctrl->ibv = NULL;
1080         DRV_LOG(DEBUG, "port %u Rx queue %u: refcnt %d", dev->data->port_id,
1081                 rxq_ctrl->idx, rte_atomic32_read(&rxq_ctrl->refcnt));
1082         if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1083                 LIST_REMOVE(rxq_ctrl, next);
1084                 rte_free(rxq_ctrl);
1085                 (*priv->rxqs)[idx] = NULL;
1086                 return 0;
1087         }
1088         return 1;
1089 }
1090
1091 /**
1092  * Verify if the queue can be released.
1093  *
1094  * @param dev
1095  *   Pointer to Ethernet device.
1096  * @param idx
1097  *   TX queue index.
1098  *
1099  * @return
1100  *   1 if the queue can be released, negative errno otherwise and rte_errno is
1101  *   set.
1102  */
1103 int
1104 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
1105 {
1106         struct priv *priv = dev->data->dev_private;
1107         struct mlx5_rxq_ctrl *rxq_ctrl;
1108
1109         if (!(*priv->rxqs)[idx]) {
1110                 rte_errno = EINVAL;
1111                 return -rte_errno;
1112         }
1113         rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1114         return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1115 }
1116
1117 /**
1118  * Verify the Rx Queue list is empty
1119  *
1120  * @param dev
1121  *   Pointer to Ethernet device.
1122  *
1123  * @return
1124  *   The number of object not released.
1125  */
1126 int
1127 mlx5_rxq_verify(struct rte_eth_dev *dev)
1128 {
1129         struct priv *priv = dev->data->dev_private;
1130         struct mlx5_rxq_ctrl *rxq_ctrl;
1131         int ret = 0;
1132
1133         LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1134                 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
1135                         dev->data->port_id, rxq_ctrl->idx);
1136                 ++ret;
1137         }
1138         return ret;
1139 }
1140
1141 /**
1142  * Create an indirection table.
1143  *
1144  * @param dev
1145  *   Pointer to Ethernet device.
1146  * @param queues
1147  *   Queues entering in the indirection table.
1148  * @param queues_n
1149  *   Number of queues in the array.
1150  *
1151  * @return
1152  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1153  */
1154 struct mlx5_ind_table_ibv *
1155 mlx5_ind_table_ibv_new(struct rte_eth_dev *dev, uint16_t queues[],
1156                        uint16_t queues_n)
1157 {
1158         struct priv *priv = dev->data->dev_private;
1159         struct mlx5_ind_table_ibv *ind_tbl;
1160         const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1161                 log2above(queues_n) :
1162                 log2above(priv->ind_table_max_size);
1163         struct ibv_wq *wq[1 << wq_n];
1164         unsigned int i;
1165         unsigned int j;
1166
1167         ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1168                              queues_n * sizeof(uint16_t), 0);
1169         if (!ind_tbl) {
1170                 rte_errno = ENOMEM;
1171                 return NULL;
1172         }
1173         for (i = 0; i != queues_n; ++i) {
1174                 struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev, queues[i]);
1175
1176                 if (!rxq)
1177                         goto error;
1178                 wq[i] = rxq->ibv->wq;
1179                 ind_tbl->queues[i] = queues[i];
1180         }
1181         ind_tbl->queues_n = queues_n;
1182         /* Finalise indirection table. */
1183         for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1184                 wq[i] = wq[j];
1185         ind_tbl->ind_table = ibv_create_rwq_ind_table(
1186                 priv->ctx,
1187                 &(struct ibv_rwq_ind_table_init_attr){
1188                         .log_ind_tbl_size = wq_n,
1189                         .ind_tbl = wq,
1190                         .comp_mask = 0,
1191                 });
1192         if (!ind_tbl->ind_table) {
1193                 rte_errno = errno;
1194                 goto error;
1195         }
1196         rte_atomic32_inc(&ind_tbl->refcnt);
1197         LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1198         DRV_LOG(DEBUG, "port %u indirection table %p: refcnt %d",
1199                 dev->data->port_id, (void *)ind_tbl,
1200                 rte_atomic32_read(&ind_tbl->refcnt));
1201         return ind_tbl;
1202 error:
1203         rte_free(ind_tbl);
1204         DRV_LOG(DEBUG, "port %u cannot create indirection table",
1205                 dev->data->port_id);
1206         return NULL;
1207 }
1208
1209 /**
1210  * Get an indirection table.
1211  *
1212  * @param dev
1213  *   Pointer to Ethernet device.
1214  * @param queues
1215  *   Queues entering in the indirection table.
1216  * @param queues_n
1217  *   Number of queues in the array.
1218  *
1219  * @return
1220  *   An indirection table if found.
1221  */
1222 struct mlx5_ind_table_ibv *
1223 mlx5_ind_table_ibv_get(struct rte_eth_dev *dev, uint16_t queues[],
1224                        uint16_t queues_n)
1225 {
1226         struct priv *priv = dev->data->dev_private;
1227         struct mlx5_ind_table_ibv *ind_tbl;
1228
1229         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1230                 if ((ind_tbl->queues_n == queues_n) &&
1231                     (memcmp(ind_tbl->queues, queues,
1232                             ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1233                      == 0))
1234                         break;
1235         }
1236         if (ind_tbl) {
1237                 unsigned int i;
1238
1239                 rte_atomic32_inc(&ind_tbl->refcnt);
1240                 DRV_LOG(DEBUG, "port %u indirection table %p: refcnt %d",
1241                         dev->data->port_id, (void *)ind_tbl,
1242                         rte_atomic32_read(&ind_tbl->refcnt));
1243                 for (i = 0; i != ind_tbl->queues_n; ++i)
1244                         mlx5_rxq_get(dev, ind_tbl->queues[i]);
1245         }
1246         return ind_tbl;
1247 }
1248
1249 /**
1250  * Release an indirection table.
1251  *
1252  * @param dev
1253  *   Pointer to Ethernet device.
1254  * @param ind_table
1255  *   Indirection table to release.
1256  *
1257  * @return
1258  *   1 while a reference on it exists, 0 when freed.
1259  */
1260 int
1261 mlx5_ind_table_ibv_release(struct rte_eth_dev *dev,
1262                            struct mlx5_ind_table_ibv *ind_tbl)
1263 {
1264         unsigned int i;
1265
1266         DRV_LOG(DEBUG, "port %u indirection table %p: refcnt %d",
1267                 dev->data->port_id, (void *)ind_tbl,
1268                 rte_atomic32_read(&ind_tbl->refcnt));
1269         if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1270                 claim_zero(ibv_destroy_rwq_ind_table(ind_tbl->ind_table));
1271         for (i = 0; i != ind_tbl->queues_n; ++i)
1272                 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
1273         if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1274                 LIST_REMOVE(ind_tbl, next);
1275                 rte_free(ind_tbl);
1276                 return 0;
1277         }
1278         return 1;
1279 }
1280
1281 /**
1282  * Verify the Rx Queue list is empty
1283  *
1284  * @param dev
1285  *   Pointer to Ethernet device.
1286  *
1287  * @return
1288  *   The number of object not released.
1289  */
1290 int
1291 mlx5_ind_table_ibv_verify(struct rte_eth_dev *dev)
1292 {
1293         struct priv *priv = dev->data->dev_private;
1294         struct mlx5_ind_table_ibv *ind_tbl;
1295         int ret = 0;
1296
1297         LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1298                 DRV_LOG(DEBUG,
1299                         "port %u Verbs indirection table %p still referenced",
1300                         dev->data->port_id, (void *)ind_tbl);
1301                 ++ret;
1302         }
1303         return ret;
1304 }
1305
1306 /**
1307  * Create an Rx Hash queue.
1308  *
1309  * @param dev
1310  *   Pointer to Ethernet device.
1311  * @param rss_key
1312  *   RSS key for the Rx hash queue.
1313  * @param rss_key_len
1314  *   RSS key length.
1315  * @param hash_fields
1316  *   Verbs protocol hash field to make the RSS on.
1317  * @param queues
1318  *   Queues entering in hash queue. In case of empty hash_fields only the
1319  *   first queue index will be taken for the indirection table.
1320  * @param queues_n
1321  *   Number of queues.
1322  *
1323  * @return
1324  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1325  */
1326 struct mlx5_hrxq *
1327 mlx5_hrxq_new(struct rte_eth_dev *dev, uint8_t *rss_key, uint8_t rss_key_len,
1328               uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1329 {
1330         struct priv *priv = dev->data->dev_private;
1331         struct mlx5_hrxq *hrxq;
1332         struct mlx5_ind_table_ibv *ind_tbl;
1333         struct ibv_qp *qp;
1334         int err;
1335
1336         queues_n = hash_fields ? queues_n : 1;
1337         ind_tbl = mlx5_ind_table_ibv_get(dev, queues, queues_n);
1338         if (!ind_tbl)
1339                 ind_tbl = mlx5_ind_table_ibv_new(dev, queues, queues_n);
1340         if (!ind_tbl) {
1341                 rte_errno = ENOMEM;
1342                 return NULL;
1343         }
1344         qp = ibv_create_qp_ex(
1345                 priv->ctx,
1346                 &(struct ibv_qp_init_attr_ex){
1347                         .qp_type = IBV_QPT_RAW_PACKET,
1348                         .comp_mask =
1349                                 IBV_QP_INIT_ATTR_PD |
1350                                 IBV_QP_INIT_ATTR_IND_TABLE |
1351                                 IBV_QP_INIT_ATTR_RX_HASH,
1352                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1353                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1354                                 .rx_hash_key_len = rss_key_len,
1355                                 .rx_hash_key = rss_key,
1356                                 .rx_hash_fields_mask = hash_fields,
1357                         },
1358                         .rwq_ind_tbl = ind_tbl->ind_table,
1359                         .pd = priv->pd,
1360                 });
1361         if (!qp) {
1362                 rte_errno = errno;
1363                 goto error;
1364         }
1365         hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1366         if (!hrxq)
1367                 goto error;
1368         hrxq->ind_table = ind_tbl;
1369         hrxq->qp = qp;
1370         hrxq->rss_key_len = rss_key_len;
1371         hrxq->hash_fields = hash_fields;
1372         memcpy(hrxq->rss_key, rss_key, rss_key_len);
1373         rte_atomic32_inc(&hrxq->refcnt);
1374         LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1375         DRV_LOG(DEBUG, "port %u hash Rx queue %p: refcnt %d",
1376                 dev->data->port_id, (void *)hrxq,
1377                 rte_atomic32_read(&hrxq->refcnt));
1378         return hrxq;
1379 error:
1380         err = rte_errno; /* Save rte_errno before cleanup. */
1381         mlx5_ind_table_ibv_release(dev, ind_tbl);
1382         if (qp)
1383                 claim_zero(ibv_destroy_qp(qp));
1384         rte_errno = err; /* Restore rte_errno. */
1385         return NULL;
1386 }
1387
1388 /**
1389  * Get an Rx Hash queue.
1390  *
1391  * @param dev
1392  *   Pointer to Ethernet device.
1393  * @param rss_conf
1394  *   RSS configuration for the Rx hash queue.
1395  * @param queues
1396  *   Queues entering in hash queue. In case of empty hash_fields only the
1397  *   first queue index will be taken for the indirection table.
1398  * @param queues_n
1399  *   Number of queues.
1400  *
1401  * @return
1402  *   An hash Rx queue on success.
1403  */
1404 struct mlx5_hrxq *
1405 mlx5_hrxq_get(struct rte_eth_dev *dev, uint8_t *rss_key, uint8_t rss_key_len,
1406               uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1407 {
1408         struct priv *priv = dev->data->dev_private;
1409         struct mlx5_hrxq *hrxq;
1410
1411         queues_n = hash_fields ? queues_n : 1;
1412         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1413                 struct mlx5_ind_table_ibv *ind_tbl;
1414
1415                 if (hrxq->rss_key_len != rss_key_len)
1416                         continue;
1417                 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1418                         continue;
1419                 if (hrxq->hash_fields != hash_fields)
1420                         continue;
1421                 ind_tbl = mlx5_ind_table_ibv_get(dev, queues, queues_n);
1422                 if (!ind_tbl)
1423                         continue;
1424                 if (ind_tbl != hrxq->ind_table) {
1425                         mlx5_ind_table_ibv_release(dev, ind_tbl);
1426                         continue;
1427                 }
1428                 rte_atomic32_inc(&hrxq->refcnt);
1429                 DRV_LOG(DEBUG, "port %u hash Rx queue %p: refcnt %d",
1430                         dev->data->port_id, (void *)hrxq,
1431                         rte_atomic32_read(&hrxq->refcnt));
1432                 return hrxq;
1433         }
1434         return NULL;
1435 }
1436
1437 /**
1438  * Release the hash Rx queue.
1439  *
1440  * @param dev
1441  *   Pointer to Ethernet device.
1442  * @param hrxq
1443  *   Pointer to Hash Rx queue to release.
1444  *
1445  * @return
1446  *   1 while a reference on it exists, 0 when freed.
1447  */
1448 int
1449 mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
1450 {
1451         DRV_LOG(DEBUG, "port %u hash Rx queue %p: refcnt %d",
1452                 dev->data->port_id, (void *)hrxq,
1453                 rte_atomic32_read(&hrxq->refcnt));
1454         if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1455                 claim_zero(ibv_destroy_qp(hrxq->qp));
1456                 mlx5_ind_table_ibv_release(dev, hrxq->ind_table);
1457                 LIST_REMOVE(hrxq, next);
1458                 rte_free(hrxq);
1459                 return 0;
1460         }
1461         claim_nonzero(mlx5_ind_table_ibv_release(dev, hrxq->ind_table));
1462         return 1;
1463 }
1464
1465 /**
1466  * Verify the Rx Queue list is empty
1467  *
1468  * @param dev
1469  *   Pointer to Ethernet device.
1470  *
1471  * @return
1472  *   The number of object not released.
1473  */
1474 int
1475 mlx5_hrxq_ibv_verify(struct rte_eth_dev *dev)
1476 {
1477         struct priv *priv = dev->data->dev_private;
1478         struct mlx5_hrxq *hrxq;
1479         int ret = 0;
1480
1481         LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1482                 DRV_LOG(DEBUG,
1483                         "port %u Verbs hash Rx queue %p still referenced",
1484                         dev->data->port_id, (void *)hrxq);
1485                 ++ret;
1486         }
1487         return ret;
1488 }