55892e281d65075532f2910d7df02facf8a4c24d
[dpdk.git] / drivers / net / mlx5 / mlx5_txq.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 <assert.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #include <inttypes.h>
14
15 /* Verbs header. */
16 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic ignored "-Wpedantic"
19 #endif
20 #include <infiniband/verbs.h>
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic error "-Wpedantic"
23 #endif
24
25 #include <rte_mbuf.h>
26 #include <rte_malloc.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include "mlx5_utils.h"
31 #include "mlx5_defs.h"
32 #include "mlx5.h"
33 #include "mlx5_rxtx.h"
34 #include "mlx5_autoconf.h"
35 #include "mlx5_glue.h"
36
37 /**
38  * Allocate TX queue elements.
39  *
40  * @param txq_ctrl
41  *   Pointer to TX queue structure.
42  */
43 void
44 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
45 {
46         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
47         unsigned int i;
48
49         for (i = 0; (i != elts_n); ++i)
50                 (*txq_ctrl->txq.elts)[i] = NULL;
51         DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
52                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
53         txq_ctrl->txq.elts_head = 0;
54         txq_ctrl->txq.elts_tail = 0;
55         txq_ctrl->txq.elts_comp = 0;
56 }
57
58 /**
59  * Free TX queue elements.
60  *
61  * @param txq_ctrl
62  *   Pointer to TX queue structure.
63  */
64 static void
65 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
66 {
67         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
68         const uint16_t elts_m = elts_n - 1;
69         uint16_t elts_head = txq_ctrl->txq.elts_head;
70         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
71         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
72
73         DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
74                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
75         txq_ctrl->txq.elts_head = 0;
76         txq_ctrl->txq.elts_tail = 0;
77         txq_ctrl->txq.elts_comp = 0;
78
79         while (elts_tail != elts_head) {
80                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
81
82                 assert(elt != NULL);
83                 rte_pktmbuf_free_seg(elt);
84 #ifndef NDEBUG
85                 /* Poisoning. */
86                 memset(&(*elts)[elts_tail & elts_m],
87                        0x77,
88                        sizeof((*elts)[elts_tail & elts_m]));
89 #endif
90                 ++elts_tail;
91         }
92 }
93
94 /**
95  * Returns the per-port supported offloads.
96  *
97  * @param dev
98  *   Pointer to Ethernet device.
99  *
100  * @return
101  *   Supported Tx offloads.
102  */
103 uint64_t
104 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
105 {
106         struct mlx5_priv *priv = dev->data->dev_private;
107         uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
108                              DEV_TX_OFFLOAD_VLAN_INSERT);
109         struct mlx5_dev_config *config = &priv->config;
110
111         if (config->hw_csum)
112                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
113                              DEV_TX_OFFLOAD_UDP_CKSUM |
114                              DEV_TX_OFFLOAD_TCP_CKSUM);
115         if (config->tso)
116                 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
117         if (config->swp) {
118                 if (config->hw_csum)
119                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
120                 if (config->tso)
121                         offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
122                                      DEV_TX_OFFLOAD_UDP_TNL_TSO);
123         }
124         if (config->tunnel_en) {
125                 if (config->hw_csum)
126                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
127                 if (config->tso)
128                         offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
129                                      DEV_TX_OFFLOAD_GRE_TNL_TSO);
130         }
131 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
132         if (config->dv_flow_en)
133                 offloads |= DEV_TX_OFFLOAD_MATCH_METADATA;
134 #endif
135         return offloads;
136 }
137
138 /**
139  * DPDK callback to configure a TX queue.
140  *
141  * @param dev
142  *   Pointer to Ethernet device structure.
143  * @param idx
144  *   TX queue index.
145  * @param desc
146  *   Number of descriptors to configure in queue.
147  * @param socket
148  *   NUMA socket on which memory must be allocated.
149  * @param[in] conf
150  *   Thresholds parameters.
151  *
152  * @return
153  *   0 on success, a negative errno value otherwise and rte_errno is set.
154  */
155 int
156 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
157                     unsigned int socket, const struct rte_eth_txconf *conf)
158 {
159         struct mlx5_priv *priv = dev->data->dev_private;
160         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
161         struct mlx5_txq_ctrl *txq_ctrl =
162                 container_of(txq, struct mlx5_txq_ctrl, txq);
163
164         if (desc <= MLX5_TX_COMP_THRESH) {
165                 DRV_LOG(WARNING,
166                         "port %u number of descriptors requested for Tx queue"
167                         " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
168                         " instead of %u",
169                         dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
170                 desc = MLX5_TX_COMP_THRESH + 1;
171         }
172         if (!rte_is_power_of_2(desc)) {
173                 desc = 1 << log2above(desc);
174                 DRV_LOG(WARNING,
175                         "port %u increased number of descriptors in Tx queue"
176                         " %u to the next power of two (%d)",
177                         dev->data->port_id, idx, desc);
178         }
179         DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
180                 dev->data->port_id, idx, desc);
181         if (idx >= priv->txqs_n) {
182                 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
183                         dev->data->port_id, idx, priv->txqs_n);
184                 rte_errno = EOVERFLOW;
185                 return -rte_errno;
186         }
187         if (!mlx5_txq_releasable(dev, idx)) {
188                 rte_errno = EBUSY;
189                 DRV_LOG(ERR, "port %u unable to release queue index %u",
190                         dev->data->port_id, idx);
191                 return -rte_errno;
192         }
193         mlx5_txq_release(dev, idx);
194         txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
195         if (!txq_ctrl) {
196                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
197                         dev->data->port_id, idx);
198                 return -rte_errno;
199         }
200         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
201                 dev->data->port_id, idx);
202         (*priv->txqs)[idx] = &txq_ctrl->txq;
203         return 0;
204 }
205
206 /**
207  * DPDK callback to release a TX queue.
208  *
209  * @param dpdk_txq
210  *   Generic TX queue pointer.
211  */
212 void
213 mlx5_tx_queue_release(void *dpdk_txq)
214 {
215         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
216         struct mlx5_txq_ctrl *txq_ctrl;
217         struct mlx5_priv *priv;
218         unsigned int i;
219
220         if (txq == NULL)
221                 return;
222         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
223         priv = txq_ctrl->priv;
224         for (i = 0; (i != priv->txqs_n); ++i)
225                 if ((*priv->txqs)[i] == txq) {
226                         mlx5_txq_release(ETH_DEV(priv), i);
227                         DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
228                                 PORT_ID(priv), txq->idx);
229                         break;
230                 }
231 }
232
233 /**
234  * Initialize Tx UAR registers for primary process.
235  *
236  * @param txq_ctrl
237  *   Pointer to Tx queue control structure.
238  */
239 static void
240 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
241 {
242         struct mlx5_priv *priv = txq_ctrl->priv;
243         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
244 #ifndef RTE_ARCH_64
245         unsigned int lock_idx;
246         const size_t page_size = sysconf(_SC_PAGESIZE);
247 #endif
248
249         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
250         assert(ppriv);
251         ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
252 #ifndef RTE_ARCH_64
253         /* Assign an UAR lock according to UAR page number */
254         lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
255                    MLX5_UAR_PAGE_NUM_MASK;
256         txq_ctrl->txq.uar_lock = &priv->uar_lock[lock_idx];
257 #endif
258 }
259
260 /**
261  * Remap UAR register of a Tx queue for secondary process.
262  *
263  * Remapped address is stored at the table in the process private structure of
264  * the device, indexed by queue index.
265  *
266  * @param txq_ctrl
267  *   Pointer to Tx queue control structure.
268  * @param fd
269  *   Verbs file descriptor to map UAR pages.
270  *
271  * @return
272  *   0 on success, a negative errno value otherwise and rte_errno is set.
273  */
274 static int
275 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
276 {
277         struct mlx5_priv *priv = txq_ctrl->priv;
278         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
279         struct mlx5_txq_data *txq = &txq_ctrl->txq;
280         void *addr;
281         uintptr_t uar_va;
282         uintptr_t offset;
283         const size_t page_size = sysconf(_SC_PAGESIZE);
284
285         assert(ppriv);
286         /*
287          * As rdma-core, UARs are mapped in size of OS page
288          * size. Ref to libmlx5 function: mlx5_init_context()
289          */
290         uar_va = (uintptr_t)txq_ctrl->bf_reg;
291         offset = uar_va & (page_size - 1); /* Offset in page. */
292         addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
293                         txq_ctrl->uar_mmap_offset);
294         if (addr == MAP_FAILED) {
295                 DRV_LOG(ERR,
296                         "port %u mmap failed for BF reg of txq %u",
297                         txq->port_id, txq->idx);
298                 rte_errno = ENXIO;
299                 return -rte_errno;
300         }
301         addr = RTE_PTR_ADD(addr, offset);
302         ppriv->uar_table[txq->idx] = addr;
303         return 0;
304 }
305
306 /**
307  * Unmap UAR register of a Tx queue for secondary process.
308  *
309  * @param txq_ctrl
310  *   Pointer to Tx queue control structure.
311  */
312 static void
313 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
314 {
315         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
316         const size_t page_size = sysconf(_SC_PAGESIZE);
317         void *addr;
318
319         addr = ppriv->uar_table[txq_ctrl->txq.idx];
320         munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
321 }
322
323 /**
324  * Initialize Tx UAR registers for secondary process.
325  *
326  * @param dev
327  *   Pointer to Ethernet device.
328  * @param fd
329  *   Verbs file descriptor to map UAR pages.
330  *
331  * @return
332  *   0 on success, a negative errno value otherwise and rte_errno is set.
333  */
334 int
335 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
336 {
337         struct mlx5_priv *priv = dev->data->dev_private;
338         struct mlx5_txq_data *txq;
339         struct mlx5_txq_ctrl *txq_ctrl;
340         unsigned int i;
341         int ret;
342
343         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
344         for (i = 0; i != priv->txqs_n; ++i) {
345                 if (!(*priv->txqs)[i])
346                         continue;
347                 txq = (*priv->txqs)[i];
348                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
349                 assert(txq->idx == (uint16_t)i);
350                 ret = txq_uar_init_secondary(txq_ctrl, fd);
351                 if (ret)
352                         goto error;
353         }
354         return 0;
355 error:
356         /* Rollback. */
357         do {
358                 if (!(*priv->txqs)[i])
359                         continue;
360                 txq = (*priv->txqs)[i];
361                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
362                 txq_uar_uninit_secondary(txq_ctrl);
363         } while (i--);
364         return -rte_errno;
365 }
366
367 /**
368  * Create the Tx queue Verbs object.
369  *
370  * @param dev
371  *   Pointer to Ethernet device.
372  * @param idx
373  *   Queue index in DPDK Tx queue array.
374  *
375  * @return
376  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
377  */
378 struct mlx5_txq_ibv *
379 mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
380 {
381         struct mlx5_priv *priv = dev->data->dev_private;
382         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
383         struct mlx5_txq_ctrl *txq_ctrl =
384                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
385         struct mlx5_txq_ibv tmpl;
386         struct mlx5_txq_ibv *txq_ibv = NULL;
387         union {
388                 struct ibv_qp_init_attr_ex init;
389                 struct ibv_cq_init_attr_ex cq;
390                 struct ibv_qp_attr mod;
391                 struct ibv_cq_ex cq_attr;
392         } attr;
393         unsigned int cqe_n;
394         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
395         struct mlx5dv_cq cq_info;
396         struct mlx5dv_obj obj;
397         const int desc = 1 << txq_data->elts_n;
398         int ret = 0;
399
400         assert(txq_data);
401         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
402         priv->verbs_alloc_ctx.obj = txq_ctrl;
403         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
404                 DRV_LOG(ERR,
405                         "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
406                         dev->data->port_id);
407                 rte_errno = EINVAL;
408                 return NULL;
409         }
410         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
411         attr.cq = (struct ibv_cq_init_attr_ex){
412                 .comp_mask = 0,
413         };
414         cqe_n = desc / MLX5_TX_COMP_THRESH + 1;
415         tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
416         if (tmpl.cq == NULL) {
417                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
418                         dev->data->port_id, idx);
419                 rte_errno = errno;
420                 goto error;
421         }
422         attr.init = (struct ibv_qp_init_attr_ex){
423                 /* CQ to be associated with the send queue. */
424                 .send_cq = tmpl.cq,
425                 /* CQ to be associated with the receive queue. */
426                 .recv_cq = tmpl.cq,
427                 .cap = {
428                         /* Max number of outstanding WRs. */
429                         .max_send_wr =
430                                 ((priv->sh->device_attr.orig_attr.max_qp_wr <
431                                   desc) ?
432                                  priv->sh->device_attr.orig_attr.max_qp_wr :
433                                  desc),
434                         /*
435                          * Max number of scatter/gather elements in a WR,
436                          * must be 1 to prevent libmlx5 from trying to affect
437                          * too much memory. TX gather is not impacted by the
438                          * device_attr.max_sge limit and will still work
439                          * properly.
440                          */
441                         .max_send_sge = 1,
442                 },
443                 .qp_type = IBV_QPT_RAW_PACKET,
444                 /*
445                  * Do *NOT* enable this, completions events are managed per
446                  * Tx burst.
447                  */
448                 .sq_sig_all = 0,
449                 .pd = priv->sh->pd,
450                 .comp_mask = IBV_QP_INIT_ATTR_PD,
451         };
452         if (txq_data->max_inline)
453                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
454         if (txq_data->tso_en) {
455                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
456                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
457         }
458         tmpl.qp = mlx5_glue->create_qp_ex(priv->sh->ctx, &attr.init);
459         if (tmpl.qp == NULL) {
460                 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
461                         dev->data->port_id, idx);
462                 rte_errno = errno;
463                 goto error;
464         }
465         attr.mod = (struct ibv_qp_attr){
466                 /* Move the QP to this state. */
467                 .qp_state = IBV_QPS_INIT,
468                 /* IB device port number. */
469                 .port_num = (uint8_t)priv->ibv_port,
470         };
471         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
472                                    (IBV_QP_STATE | IBV_QP_PORT));
473         if (ret) {
474                 DRV_LOG(ERR,
475                         "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
476                         dev->data->port_id, idx);
477                 rte_errno = errno;
478                 goto error;
479         }
480         attr.mod = (struct ibv_qp_attr){
481                 .qp_state = IBV_QPS_RTR
482         };
483         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
484         if (ret) {
485                 DRV_LOG(ERR,
486                         "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
487                         dev->data->port_id, idx);
488                 rte_errno = errno;
489                 goto error;
490         }
491         attr.mod.qp_state = IBV_QPS_RTS;
492         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
493         if (ret) {
494                 DRV_LOG(ERR,
495                         "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
496                         dev->data->port_id, idx);
497                 rte_errno = errno;
498                 goto error;
499         }
500         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
501                                     txq_ctrl->socket);
502         if (!txq_ibv) {
503                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
504                         dev->data->port_id, idx);
505                 rte_errno = ENOMEM;
506                 goto error;
507         }
508         obj.cq.in = tmpl.cq;
509         obj.cq.out = &cq_info;
510         obj.qp.in = tmpl.qp;
511         obj.qp.out = &qp;
512         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
513         if (ret != 0) {
514                 rte_errno = errno;
515                 goto error;
516         }
517         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
518                 DRV_LOG(ERR,
519                         "port %u wrong MLX5_CQE_SIZE environment variable"
520                         " value: it should be set to %u",
521                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
522                 rte_errno = EINVAL;
523                 goto error;
524         }
525         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
526         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
527         txq_data->wqes = qp.sq.buf;
528         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
529         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
530         txq_data->cq_db = cq_info.dbrec;
531         txq_data->cqes =
532                 (volatile struct mlx5_cqe (*)[])
533                 (uintptr_t)cq_info.buf;
534         txq_data->cq_ci = 0;
535 #ifndef NDEBUG
536         txq_data->cq_pi = 0;
537 #endif
538         txq_data->wqe_ci = 0;
539         txq_data->wqe_pi = 0;
540         txq_ibv->qp = tmpl.qp;
541         txq_ibv->cq = tmpl.cq;
542         rte_atomic32_inc(&txq_ibv->refcnt);
543         txq_ctrl->bf_reg = qp.bf.reg;
544         txq_ctrl->cqn = cq_info.cqn;
545         txq_uar_init(txq_ctrl);
546         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
547                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
548                 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%"PRIx64,
549                         dev->data->port_id, txq_ctrl->uar_mmap_offset);
550         } else {
551                 DRV_LOG(ERR,
552                         "port %u failed to retrieve UAR info, invalid"
553                         " libmlx5.so",
554                         dev->data->port_id);
555                 rte_errno = EINVAL;
556                 goto error;
557         }
558         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
559         txq_ibv->txq_ctrl = txq_ctrl;
560         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
561         return txq_ibv;
562 error:
563         ret = rte_errno; /* Save rte_errno before cleanup. */
564         if (tmpl.cq)
565                 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
566         if (tmpl.qp)
567                 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
568         if (txq_ibv)
569                 rte_free(txq_ibv);
570         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
571         rte_errno = ret; /* Restore rte_errno. */
572         return NULL;
573 }
574
575 /**
576  * Get an Tx queue Verbs object.
577  *
578  * @param dev
579  *   Pointer to Ethernet device.
580  * @param idx
581  *   Queue index in DPDK Tx queue array.
582  *
583  * @return
584  *   The Verbs object if it exists.
585  */
586 struct mlx5_txq_ibv *
587 mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
588 {
589         struct mlx5_priv *priv = dev->data->dev_private;
590         struct mlx5_txq_ctrl *txq_ctrl;
591
592         if (idx >= priv->txqs_n)
593                 return NULL;
594         if (!(*priv->txqs)[idx])
595                 return NULL;
596         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
597         if (txq_ctrl->ibv)
598                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
599         return txq_ctrl->ibv;
600 }
601
602 /**
603  * Release an Tx verbs queue object.
604  *
605  * @param txq_ibv
606  *   Verbs Tx queue object.
607  *
608  * @return
609  *   1 while a reference on it exists, 0 when freed.
610  */
611 int
612 mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv)
613 {
614         assert(txq_ibv);
615         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
616                 claim_zero(mlx5_glue->destroy_qp(txq_ibv->qp));
617                 claim_zero(mlx5_glue->destroy_cq(txq_ibv->cq));
618                 LIST_REMOVE(txq_ibv, next);
619                 rte_free(txq_ibv);
620                 return 0;
621         }
622         return 1;
623 }
624
625 /**
626  * Verify the Verbs Tx queue list is empty
627  *
628  * @param dev
629  *   Pointer to Ethernet device.
630  *
631  * @return
632  *   The number of object not released.
633  */
634 int
635 mlx5_txq_ibv_verify(struct rte_eth_dev *dev)
636 {
637         struct mlx5_priv *priv = dev->data->dev_private;
638         int ret = 0;
639         struct mlx5_txq_ibv *txq_ibv;
640
641         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
642                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
643                         dev->data->port_id, txq_ibv->txq_ctrl->txq.idx);
644                 ++ret;
645         }
646         return ret;
647 }
648
649 /**
650  * Calculate the total number of WQEBB for Tx queue.
651  *
652  * Simplified version of calc_sq_size() in rdma-core.
653  *
654  * @param txq_ctrl
655  *   Pointer to Tx queue control structure.
656  *
657  * @return
658  *   The number of WQEBB.
659  */
660 static int
661 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
662 {
663         unsigned int wqe_size;
664         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
665
666         wqe_size = MLX5_WQE_SIZE + txq_ctrl->max_inline_data;
667         return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
668 }
669
670 /**
671  * Set Tx queue parameters from device configuration.
672  *
673  * @param txq_ctrl
674  *   Pointer to Tx queue control structure.
675  */
676 static void
677 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
678 {
679         (void)txq_ctrl;
680 }
681
682 /**
683  * Create a DPDK Tx queue.
684  *
685  * @param dev
686  *   Pointer to Ethernet device.
687  * @param idx
688  *   TX queue index.
689  * @param desc
690  *   Number of descriptors to configure in queue.
691  * @param socket
692  *   NUMA socket on which memory must be allocated.
693  * @param[in] conf
694  *  Thresholds parameters.
695  *
696  * @return
697  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
698  */
699 struct mlx5_txq_ctrl *
700 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
701              unsigned int socket, const struct rte_eth_txconf *conf)
702 {
703         struct mlx5_priv *priv = dev->data->dev_private;
704         struct mlx5_txq_ctrl *tmpl;
705
706         tmpl = rte_calloc_socket("TXQ", 1,
707                                  sizeof(*tmpl) +
708                                  desc * sizeof(struct rte_mbuf *),
709                                  0, socket);
710         if (!tmpl) {
711                 rte_errno = ENOMEM;
712                 return NULL;
713         }
714         if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
715                                MLX5_MR_BTREE_CACHE_N, socket)) {
716                 /* rte_errno is already set. */
717                 goto error;
718         }
719         /* Save pointer of global generation number to check memory event. */
720         tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->mr.dev_gen;
721         assert(desc > MLX5_TX_COMP_THRESH);
722         tmpl->txq.offloads = conf->offloads |
723                              dev->data->dev_conf.txmode.offloads;
724         tmpl->priv = priv;
725         tmpl->socket = socket;
726         tmpl->txq.elts_n = log2above(desc);
727         tmpl->txq.port_id = dev->data->port_id;
728         tmpl->txq.idx = idx;
729         txq_set_params(tmpl);
730         if (txq_calc_wqebb_cnt(tmpl) >
731             priv->sh->device_attr.orig_attr.max_qp_wr) {
732                 DRV_LOG(ERR,
733                         "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
734                         " try smaller queue size",
735                         dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
736                         priv->sh->device_attr.orig_attr.max_qp_wr);
737                 rte_errno = ENOMEM;
738                 goto error;
739         }
740         tmpl->txq.elts =
741                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
742         rte_atomic32_inc(&tmpl->refcnt);
743         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
744         return tmpl;
745 error:
746         rte_free(tmpl);
747         return NULL;
748 }
749
750 /**
751  * Get a Tx queue.
752  *
753  * @param dev
754  *   Pointer to Ethernet device.
755  * @param idx
756  *   TX queue index.
757  *
758  * @return
759  *   A pointer to the queue if it exists.
760  */
761 struct mlx5_txq_ctrl *
762 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
763 {
764         struct mlx5_priv *priv = dev->data->dev_private;
765         struct mlx5_txq_ctrl *ctrl = NULL;
766
767         if ((*priv->txqs)[idx]) {
768                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
769                                     txq);
770                 mlx5_txq_ibv_get(dev, idx);
771                 rte_atomic32_inc(&ctrl->refcnt);
772         }
773         return ctrl;
774 }
775
776 /**
777  * Release a Tx queue.
778  *
779  * @param dev
780  *   Pointer to Ethernet device.
781  * @param idx
782  *   TX queue index.
783  *
784  * @return
785  *   1 while a reference on it exists, 0 when freed.
786  */
787 int
788 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
789 {
790         struct mlx5_priv *priv = dev->data->dev_private;
791         struct mlx5_txq_ctrl *txq;
792
793         if (!(*priv->txqs)[idx])
794                 return 0;
795         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
796         if (txq->ibv && !mlx5_txq_ibv_release(txq->ibv))
797                 txq->ibv = NULL;
798         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
799                 txq_free_elts(txq);
800                 mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
801                 LIST_REMOVE(txq, next);
802                 rte_free(txq);
803                 (*priv->txqs)[idx] = NULL;
804                 return 0;
805         }
806         return 1;
807 }
808
809 /**
810  * Verify if the queue can be released.
811  *
812  * @param dev
813  *   Pointer to Ethernet device.
814  * @param idx
815  *   TX queue index.
816  *
817  * @return
818  *   1 if the queue can be released.
819  */
820 int
821 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
822 {
823         struct mlx5_priv *priv = dev->data->dev_private;
824         struct mlx5_txq_ctrl *txq;
825
826         if (!(*priv->txqs)[idx])
827                 return -1;
828         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
829         return (rte_atomic32_read(&txq->refcnt) == 1);
830 }
831
832 /**
833  * Verify the Tx Queue list is empty
834  *
835  * @param dev
836  *   Pointer to Ethernet device.
837  *
838  * @return
839  *   The number of object not released.
840  */
841 int
842 mlx5_txq_verify(struct rte_eth_dev *dev)
843 {
844         struct mlx5_priv *priv = dev->data->dev_private;
845         struct mlx5_txq_ctrl *txq_ctrl;
846         int ret = 0;
847
848         LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
849                 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
850                         dev->data->port_id, txq_ctrl->txq.idx);
851                 ++ret;
852         }
853         return ret;
854 }