bdaca5a009efbd8c080f987f9a9118038aecb9d2
[dpdk.git] / drivers / net / mlx5 / mlx5_txq.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 <unistd.h>
40 #include <sys/mman.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 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_mbuf.h>
53 #include <rte_malloc.h>
54 #include <rte_ethdev_driver.h>
55 #include <rte_common.h>
56
57 #include "mlx5_utils.h"
58 #include "mlx5_defs.h"
59 #include "mlx5.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_autoconf.h"
62 #include "mlx5_glue.h"
63
64 /**
65  * Allocate TX queue elements.
66  *
67  * @param txq_ctrl
68  *   Pointer to TX queue structure.
69  */
70 void
71 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
72 {
73         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
74         unsigned int i;
75
76         for (i = 0; (i != elts_n); ++i)
77                 (*txq_ctrl->txq.elts)[i] = NULL;
78         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
79         txq_ctrl->txq.elts_head = 0;
80         txq_ctrl->txq.elts_tail = 0;
81         txq_ctrl->txq.elts_comp = 0;
82 }
83
84 /**
85  * Free TX queue elements.
86  *
87  * @param txq_ctrl
88  *   Pointer to TX queue structure.
89  */
90 static void
91 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
92 {
93         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
94         const uint16_t elts_m = elts_n - 1;
95         uint16_t elts_head = txq_ctrl->txq.elts_head;
96         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
97         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
98
99         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
100         txq_ctrl->txq.elts_head = 0;
101         txq_ctrl->txq.elts_tail = 0;
102         txq_ctrl->txq.elts_comp = 0;
103
104         while (elts_tail != elts_head) {
105                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
106
107                 assert(elt != NULL);
108                 rte_pktmbuf_free_seg(elt);
109 #ifndef NDEBUG
110                 /* Poisoning. */
111                 memset(&(*elts)[elts_tail & elts_m],
112                        0x77,
113                        sizeof((*elts)[elts_tail & elts_m]));
114 #endif
115                 ++elts_tail;
116         }
117 }
118
119 /**
120  * Returns the per-port supported offloads.
121  *
122  * @param priv
123  *   Pointer to private structure.
124  *
125  * @return
126  *   Supported Tx offloads.
127  */
128 uint64_t
129 mlx5_priv_get_tx_port_offloads(struct priv *priv)
130 {
131         uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
132                              DEV_TX_OFFLOAD_VLAN_INSERT);
133         struct mlx5_dev_config *config = &priv->config;
134
135         if (config->hw_csum)
136                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
137                              DEV_TX_OFFLOAD_UDP_CKSUM |
138                              DEV_TX_OFFLOAD_TCP_CKSUM);
139         if (config->tso)
140                 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
141         if (config->tunnel_en) {
142                 if (config->hw_csum)
143                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
144                 if (config->tso)
145                         offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
146                                      DEV_TX_OFFLOAD_GRE_TNL_TSO);
147         }
148         return offloads;
149 }
150
151 /**
152  * Checks if the per-queue offload configuration is valid.
153  *
154  * @param priv
155  *   Pointer to private structure.
156  * @param offloads
157  *   Per-queue offloads configuration.
158  *
159  * @return
160  *   1 if the configuration is valid, 0 otherwise.
161  */
162 static int
163 priv_is_tx_queue_offloads_allowed(struct priv *priv, uint64_t offloads)
164 {
165         uint64_t port_offloads = priv->dev->data->dev_conf.txmode.offloads;
166         uint64_t port_supp_offloads = mlx5_priv_get_tx_port_offloads(priv);
167
168         /* There are no Tx offloads which are per queue. */
169         if ((offloads & port_supp_offloads) != offloads)
170                 return 0;
171         if ((port_offloads ^ offloads) & port_supp_offloads)
172                 return 0;
173         return 1;
174 }
175
176 /**
177  * DPDK callback to configure a TX queue.
178  *
179  * @param dev
180  *   Pointer to Ethernet device structure.
181  * @param idx
182  *   TX queue index.
183  * @param desc
184  *   Number of descriptors to configure in queue.
185  * @param socket
186  *   NUMA socket on which memory must be allocated.
187  * @param[in] conf
188  *   Thresholds parameters.
189  *
190  * @return
191  *   0 on success, negative errno value on failure.
192  */
193 int
194 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
195                     unsigned int socket, const struct rte_eth_txconf *conf)
196 {
197         struct priv *priv = dev->data->dev_private;
198         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
199         struct mlx5_txq_ctrl *txq_ctrl =
200                 container_of(txq, struct mlx5_txq_ctrl, txq);
201         int ret = 0;
202
203         priv_lock(priv);
204         /*
205          * Don't verify port offloads for application which
206          * use the old API.
207          */
208         if (!!(conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) &&
209             !priv_is_tx_queue_offloads_allowed(priv, conf->offloads)) {
210                 ret = ENOTSUP;
211                 ERROR("%p: Tx queue offloads 0x%" PRIx64 " don't match port "
212                       "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
213                       (void *)dev, conf->offloads,
214                       dev->data->dev_conf.txmode.offloads,
215                       mlx5_priv_get_tx_port_offloads(priv));
216                 goto out;
217         }
218         if (desc <= MLX5_TX_COMP_THRESH) {
219                 WARN("%p: number of descriptors requested for TX queue %u"
220                      " must be higher than MLX5_TX_COMP_THRESH, using"
221                      " %u instead of %u",
222                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
223                 desc = MLX5_TX_COMP_THRESH + 1;
224         }
225         if (!rte_is_power_of_2(desc)) {
226                 desc = 1 << log2above(desc);
227                 WARN("%p: increased number of descriptors in TX queue %u"
228                      " to the next power of two (%d)",
229                      (void *)dev, idx, desc);
230         }
231         DEBUG("%p: configuring queue %u for %u descriptors",
232               (void *)dev, idx, desc);
233         if (idx >= priv->txqs_n) {
234                 ERROR("%p: queue index out of range (%u >= %u)",
235                       (void *)dev, idx, priv->txqs_n);
236                 priv_unlock(priv);
237                 return -EOVERFLOW;
238         }
239         if (!mlx5_priv_txq_releasable(priv, idx)) {
240                 ret = EBUSY;
241                 ERROR("%p: unable to release queue index %u",
242                       (void *)dev, idx);
243                 goto out;
244         }
245         mlx5_priv_txq_release(priv, idx);
246         txq_ctrl = mlx5_priv_txq_new(priv, idx, desc, socket, conf);
247         if (!txq_ctrl) {
248                 ERROR("%p: unable to allocate queue index %u",
249                       (void *)dev, idx);
250                 ret = ENOMEM;
251                 goto out;
252         }
253         DEBUG("%p: adding TX queue %p to list",
254               (void *)dev, (void *)txq_ctrl);
255         (*priv->txqs)[idx] = &txq_ctrl->txq;
256 out:
257         priv_unlock(priv);
258         return -ret;
259 }
260
261 /**
262  * DPDK callback to release a TX queue.
263  *
264  * @param dpdk_txq
265  *   Generic TX queue pointer.
266  */
267 void
268 mlx5_tx_queue_release(void *dpdk_txq)
269 {
270         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
271         struct mlx5_txq_ctrl *txq_ctrl;
272         struct priv *priv;
273         unsigned int i;
274
275         if (txq == NULL)
276                 return;
277         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
278         priv = txq_ctrl->priv;
279         priv_lock(priv);
280         for (i = 0; (i != priv->txqs_n); ++i)
281                 if ((*priv->txqs)[i] == txq) {
282                         DEBUG("%p: removing TX queue %p from list",
283                               (void *)priv->dev, (void *)txq_ctrl);
284                         mlx5_priv_txq_release(priv, i);
285                         break;
286                 }
287         priv_unlock(priv);
288 }
289
290
291 /**
292  * Mmap TX UAR(HW doorbell) pages into reserved UAR address space.
293  * Both primary and secondary process do mmap to make UAR address
294  * aligned.
295  *
296  * @param[in] priv
297  *   Pointer to private structure.
298  * @param fd
299  *   Verbs file descriptor to map UAR pages.
300  *
301  * @return
302  *   0 on success, errno value on failure.
303  */
304 int
305 priv_tx_uar_remap(struct priv *priv, int fd)
306 {
307         unsigned int i, j;
308         uintptr_t pages[priv->txqs_n];
309         unsigned int pages_n = 0;
310         uintptr_t uar_va;
311         uintptr_t off;
312         void *addr;
313         void *ret;
314         struct mlx5_txq_data *txq;
315         struct mlx5_txq_ctrl *txq_ctrl;
316         int already_mapped;
317         size_t page_size = sysconf(_SC_PAGESIZE);
318         int r;
319
320         memset(pages, 0, priv->txqs_n * sizeof(uintptr_t));
321         /*
322          * As rdma-core, UARs are mapped in size of OS page size.
323          * Use aligned address to avoid duplicate mmap.
324          * Ref to libmlx5 function: mlx5_init_context()
325          */
326         for (i = 0; i != priv->txqs_n; ++i) {
327                 txq = (*priv->txqs)[i];
328                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
329                 /* UAR addr form verbs used to find dup and offset in page. */
330                 uar_va = (uintptr_t)txq_ctrl->bf_reg_orig;
331                 off = uar_va & (page_size - 1); /* offset in page. */
332                 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size); /* page addr. */
333                 already_mapped = 0;
334                 for (j = 0; j != pages_n; ++j) {
335                         if (pages[j] == uar_va) {
336                                 already_mapped = 1;
337                                 break;
338                         }
339                 }
340                 /* new address in reserved UAR address space. */
341                 addr = RTE_PTR_ADD(priv->uar_base,
342                                    uar_va & (MLX5_UAR_SIZE - 1));
343                 if (!already_mapped) {
344                         pages[pages_n++] = uar_va;
345                         /* fixed mmap to specified address in reserved
346                          * address space.
347                          */
348                         ret = mmap(addr, page_size,
349                                    PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
350                                    txq_ctrl->uar_mmap_offset);
351                         if (ret != addr) {
352                                 /* fixed mmap have to return same address */
353                                 ERROR("call to mmap failed on UAR for txq %d\n",
354                                       i);
355                                 r = ENXIO;
356                                 return r;
357                         }
358                 }
359                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) /* save once */
360                         txq_ctrl->txq.bf_reg = RTE_PTR_ADD((void *)addr, off);
361                 else
362                         assert(txq_ctrl->txq.bf_reg ==
363                                RTE_PTR_ADD((void *)addr, off));
364         }
365         return 0;
366 }
367
368 /**
369  * Check if the burst function is using eMPW.
370  *
371  * @param tx_pkt_burst
372  *   Tx burst function pointer.
373  *
374  * @return
375  *   1 if the burst function is using eMPW, 0 otherwise.
376  */
377 static int
378 is_empw_burst_func(eth_tx_burst_t tx_pkt_burst)
379 {
380         if (tx_pkt_burst == mlx5_tx_burst_raw_vec ||
381             tx_pkt_burst == mlx5_tx_burst_vec ||
382             tx_pkt_burst == mlx5_tx_burst_empw)
383                 return 1;
384         return 0;
385 }
386
387 /**
388  * Create the Tx queue Verbs object.
389  *
390  * @param priv
391  *   Pointer to private structure.
392  * @param idx
393  *   Queue index in DPDK Rx queue array
394  *
395  * @return
396  *   The Verbs object initialised if it can be created.
397  */
398 struct mlx5_txq_ibv*
399 mlx5_priv_txq_ibv_new(struct priv *priv, uint16_t idx)
400 {
401         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
402         struct mlx5_txq_ctrl *txq_ctrl =
403                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
404         struct mlx5_txq_ibv tmpl;
405         struct mlx5_txq_ibv *txq_ibv;
406         union {
407                 struct ibv_qp_init_attr_ex init;
408                 struct ibv_cq_init_attr_ex cq;
409                 struct ibv_qp_attr mod;
410                 struct ibv_cq_ex cq_attr;
411         } attr;
412         unsigned int cqe_n;
413         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
414         struct mlx5dv_cq cq_info;
415         struct mlx5dv_obj obj;
416         const int desc = 1 << txq_data->elts_n;
417         eth_tx_burst_t tx_pkt_burst = priv_select_tx_function(priv, priv->dev);
418         int ret = 0;
419
420         assert(txq_data);
421         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
422         priv->verbs_alloc_ctx.obj = txq_ctrl;
423         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
424                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
425                 goto error;
426         }
427         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
428         /* MRs will be registered in mp2mr[] later. */
429         attr.cq = (struct ibv_cq_init_attr_ex){
430                 .comp_mask = 0,
431         };
432         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
433                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
434         if (is_empw_burst_func(tx_pkt_burst))
435                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
436         tmpl.cq = mlx5_glue->create_cq(priv->ctx, cqe_n, NULL, NULL, 0);
437         if (tmpl.cq == NULL) {
438                 ERROR("%p: CQ creation failure", (void *)txq_ctrl);
439                 goto error;
440         }
441         attr.init = (struct ibv_qp_init_attr_ex){
442                 /* CQ to be associated with the send queue. */
443                 .send_cq = tmpl.cq,
444                 /* CQ to be associated with the receive queue. */
445                 .recv_cq = tmpl.cq,
446                 .cap = {
447                         /* Max number of outstanding WRs. */
448                         .max_send_wr =
449                                 ((priv->device_attr.orig_attr.max_qp_wr <
450                                   desc) ?
451                                  priv->device_attr.orig_attr.max_qp_wr :
452                                  desc),
453                         /*
454                          * Max number of scatter/gather elements in a WR,
455                          * must be 1 to prevent libmlx5 from trying to affect
456                          * too much memory. TX gather is not impacted by the
457                          * priv->device_attr.max_sge limit and will still work
458                          * properly.
459                          */
460                         .max_send_sge = 1,
461                 },
462                 .qp_type = IBV_QPT_RAW_PACKET,
463                 /*
464                  * Do *NOT* enable this, completions events are managed per
465                  * Tx burst.
466                  */
467                 .sq_sig_all = 0,
468                 .pd = priv->pd,
469                 .comp_mask = IBV_QP_INIT_ATTR_PD,
470         };
471         if (txq_data->max_inline)
472                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
473         if (txq_data->tso_en) {
474                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
475                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
476         }
477         tmpl.qp = mlx5_glue->create_qp_ex(priv->ctx, &attr.init);
478         if (tmpl.qp == NULL) {
479                 ERROR("%p: QP creation failure", (void *)txq_ctrl);
480                 goto error;
481         }
482         attr.mod = (struct ibv_qp_attr){
483                 /* Move the QP to this state. */
484                 .qp_state = IBV_QPS_INIT,
485                 /* Primary port number. */
486                 .port_num = priv->port
487         };
488         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
489                                    (IBV_QP_STATE | IBV_QP_PORT));
490         if (ret) {
491                 ERROR("%p: QP state to IBV_QPS_INIT failed", (void *)txq_ctrl);
492                 goto error;
493         }
494         attr.mod = (struct ibv_qp_attr){
495                 .qp_state = IBV_QPS_RTR
496         };
497         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
498         if (ret) {
499                 ERROR("%p: QP state to IBV_QPS_RTR failed", (void *)txq_ctrl);
500                 goto error;
501         }
502         attr.mod.qp_state = IBV_QPS_RTS;
503         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
504         if (ret) {
505                 ERROR("%p: QP state to IBV_QPS_RTS failed", (void *)txq_ctrl);
506                 goto error;
507         }
508         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
509                                     txq_ctrl->socket);
510         if (!txq_ibv) {
511                 ERROR("%p: cannot allocate memory", (void *)txq_ctrl);
512                 goto error;
513         }
514         obj.cq.in = tmpl.cq;
515         obj.cq.out = &cq_info;
516         obj.qp.in = tmpl.qp;
517         obj.qp.out = &qp;
518         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
519         if (ret != 0)
520                 goto error;
521         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
522                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
523                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
524                 goto error;
525         }
526         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
527         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
528         txq_data->wqes = qp.sq.buf;
529         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
530         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
531         txq_ctrl->bf_reg_orig = qp.bf.reg;
532         txq_data->cq_db = cq_info.dbrec;
533         txq_data->cqes =
534                 (volatile struct mlx5_cqe (*)[])
535                 (uintptr_t)cq_info.buf;
536         txq_data->cq_ci = 0;
537 #ifndef NDEBUG
538         txq_data->cq_pi = 0;
539 #endif
540         txq_data->wqe_ci = 0;
541         txq_data->wqe_pi = 0;
542         txq_ibv->qp = tmpl.qp;
543         txq_ibv->cq = tmpl.cq;
544         rte_atomic32_inc(&txq_ibv->refcnt);
545         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
546                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
547         } else {
548                 ERROR("Failed to retrieve UAR info, invalid libmlx5.so version");
549                 goto error;
550         }
551         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
552               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
553         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
554         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
555         return txq_ibv;
556 error:
557         if (tmpl.cq)
558                 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
559         if (tmpl.qp)
560                 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
561         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
562         return NULL;
563 }
564
565 /**
566  * Get an Tx queue Verbs object.
567  *
568  * @param priv
569  *   Pointer to private structure.
570  * @param idx
571  *   Queue index in DPDK Rx queue array
572  *
573  * @return
574  *   The Verbs object if it exists.
575  */
576 struct mlx5_txq_ibv*
577 mlx5_priv_txq_ibv_get(struct priv *priv, uint16_t idx)
578 {
579         struct mlx5_txq_ctrl *txq_ctrl;
580
581         if (idx >= priv->txqs_n)
582                 return NULL;
583         if (!(*priv->txqs)[idx])
584                 return NULL;
585         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
586         if (txq_ctrl->ibv) {
587                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
588                 DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
589                       (void *)txq_ctrl->ibv,
590                       rte_atomic32_read(&txq_ctrl->ibv->refcnt));
591         }
592         return txq_ctrl->ibv;
593 }
594
595 /**
596  * Release an Tx verbs queue object.
597  *
598  * @param priv
599  *   Pointer to private structure.
600  * @param txq_ibv
601  *   Verbs Tx queue object.
602  *
603  * @return
604  *   0 on success, errno on failure.
605  */
606 int
607 mlx5_priv_txq_ibv_release(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
608 {
609         (void)priv;
610         assert(txq_ibv);
611         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
612               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
613         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
614                 claim_zero(mlx5_glue->destroy_qp(txq_ibv->qp));
615                 claim_zero(mlx5_glue->destroy_cq(txq_ibv->cq));
616                 LIST_REMOVE(txq_ibv, next);
617                 rte_free(txq_ibv);
618                 return 0;
619         }
620         return EBUSY;
621 }
622
623 /**
624  * Return true if a single reference exists on the object.
625  *
626  * @param priv
627  *   Pointer to private structure.
628  * @param txq_ibv
629  *   Verbs Tx queue object.
630  */
631 int
632 mlx5_priv_txq_ibv_releasable(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
633 {
634         (void)priv;
635         assert(txq_ibv);
636         return (rte_atomic32_read(&txq_ibv->refcnt) == 1);
637 }
638
639 /**
640  * Verify the Verbs Tx queue list is empty
641  *
642  * @param priv
643  *  Pointer to private structure.
644  *
645  * @return the number of object not released.
646  */
647 int
648 mlx5_priv_txq_ibv_verify(struct priv *priv)
649 {
650         int ret = 0;
651         struct mlx5_txq_ibv *txq_ibv;
652
653         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
654                 DEBUG("%p: Verbs Tx queue %p still referenced", (void *)priv,
655                       (void *)txq_ibv);
656                 ++ret;
657         }
658         return ret;
659 }
660
661 /**
662  * Set Tx queue parameters from device configuration.
663  *
664  * @param txq_ctrl
665  *   Pointer to Tx queue control structure.
666  */
667 static void
668 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
669 {
670         struct priv *priv = txq_ctrl->priv;
671         struct mlx5_dev_config *config = &priv->config;
672         const unsigned int max_tso_inline =
673                 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
674                  RTE_CACHE_LINE_SIZE);
675         unsigned int txq_inline;
676         unsigned int txqs_inline;
677         unsigned int inline_max_packet_sz;
678         eth_tx_burst_t tx_pkt_burst = priv_select_tx_function(priv, priv->dev);
679         int is_empw_func = is_empw_burst_func(tx_pkt_burst);
680         int tso = !!(txq_ctrl->txq.offloads & DEV_TX_OFFLOAD_TCP_TSO);
681
682         txq_inline = (config->txq_inline == MLX5_ARG_UNSET) ?
683                 0 : config->txq_inline;
684         txqs_inline = (config->txqs_inline == MLX5_ARG_UNSET) ?
685                 0 : config->txqs_inline;
686         inline_max_packet_sz =
687                 (config->inline_max_packet_sz == MLX5_ARG_UNSET) ?
688                 0 : config->inline_max_packet_sz;
689         if (is_empw_func) {
690                 if (config->txq_inline == MLX5_ARG_UNSET)
691                         txq_inline = MLX5_WQE_SIZE_MAX - MLX5_WQE_SIZE;
692                 if (config->txqs_inline == MLX5_ARG_UNSET)
693                         txqs_inline = MLX5_EMPW_MIN_TXQS;
694                 if (config->inline_max_packet_sz == MLX5_ARG_UNSET)
695                         inline_max_packet_sz = MLX5_EMPW_MAX_INLINE_LEN;
696                 txq_ctrl->txq.mpw_hdr_dseg = config->mpw_hdr_dseg;
697                 txq_ctrl->txq.inline_max_packet_sz = inline_max_packet_sz;
698         }
699         if (txq_inline && priv->txqs_n >= txqs_inline) {
700                 unsigned int ds_cnt;
701
702                 txq_ctrl->txq.max_inline =
703                         ((txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
704                          RTE_CACHE_LINE_SIZE);
705                 if (is_empw_func) {
706                         /* To minimize the size of data set, avoid requesting
707                          * too large WQ.
708                          */
709                         txq_ctrl->max_inline_data =
710                                 ((RTE_MIN(txq_inline,
711                                           inline_max_packet_sz) +
712                                   (RTE_CACHE_LINE_SIZE - 1)) /
713                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
714                 } else if (tso) {
715                         int inline_diff = txq_ctrl->txq.max_inline -
716                                           max_tso_inline;
717
718                         /*
719                          * Adjust inline value as Verbs aggregates
720                          * tso_inline and txq_inline fields.
721                          */
722                         txq_ctrl->max_inline_data = inline_diff > 0 ?
723                                                inline_diff *
724                                                RTE_CACHE_LINE_SIZE :
725                                                0;
726                 } else {
727                         txq_ctrl->max_inline_data =
728                                 txq_ctrl->txq.max_inline * RTE_CACHE_LINE_SIZE;
729                 }
730                 /*
731                  * Check if the inline size is too large in a way which
732                  * can make the WQE DS to overflow.
733                  * Considering in calculation:
734                  *      WQE CTRL (1 DS)
735                  *      WQE ETH  (1 DS)
736                  *      Inline part (N DS)
737                  */
738                 ds_cnt = 2 + (txq_ctrl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
739                 if (ds_cnt > MLX5_DSEG_MAX) {
740                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
741                                                   MLX5_WQE_DWORD_SIZE;
742
743                         max_inline = max_inline - (max_inline %
744                                                    RTE_CACHE_LINE_SIZE);
745                         WARN("txq inline is too large (%d) setting it to "
746                              "the maximum possible: %d\n",
747                              txq_inline, max_inline);
748                         txq_ctrl->txq.max_inline = max_inline /
749                                                    RTE_CACHE_LINE_SIZE;
750                 }
751         }
752         if (tso) {
753                 txq_ctrl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
754                 txq_ctrl->txq.max_inline = RTE_MAX(txq_ctrl->txq.max_inline,
755                                                    max_tso_inline);
756                 txq_ctrl->txq.tso_en = 1;
757         }
758         txq_ctrl->txq.tunnel_en = config->tunnel_en;
759 }
760
761 /**
762  * Create a DPDK Tx queue.
763  *
764  * @param priv
765  *   Pointer to private structure.
766  * @param idx
767  *   TX queue index.
768  * @param desc
769  *   Number of descriptors to configure in queue.
770  * @param socket
771  *   NUMA socket on which memory must be allocated.
772  * @param[in] conf
773  *  Thresholds parameters.
774  *
775  * @return
776  *   A DPDK queue object on success.
777  */
778 struct mlx5_txq_ctrl*
779 mlx5_priv_txq_new(struct priv *priv, uint16_t idx, uint16_t desc,
780                   unsigned int socket,
781                   const struct rte_eth_txconf *conf)
782 {
783         struct mlx5_txq_ctrl *tmpl;
784
785         tmpl = rte_calloc_socket("TXQ", 1,
786                                  sizeof(*tmpl) +
787                                  desc * sizeof(struct rte_mbuf *),
788                                  0, socket);
789         if (!tmpl)
790                 return NULL;
791         assert(desc > MLX5_TX_COMP_THRESH);
792         tmpl->txq.offloads = conf->offloads;
793         tmpl->priv = priv;
794         tmpl->socket = socket;
795         tmpl->txq.elts_n = log2above(desc);
796         txq_set_params(tmpl);
797         /* MRs will be registered in mp2mr[] later. */
798         DEBUG("priv->device_attr.max_qp_wr is %d",
799               priv->device_attr.orig_attr.max_qp_wr);
800         DEBUG("priv->device_attr.max_sge is %d",
801               priv->device_attr.orig_attr.max_sge);
802         tmpl->txq.elts =
803                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
804         tmpl->txq.stats.idx = idx;
805         rte_atomic32_inc(&tmpl->refcnt);
806         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
807               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
808         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
809         return tmpl;
810 }
811
812 /**
813  * Get a Tx queue.
814  *
815  * @param priv
816  *   Pointer to private structure.
817  * @param idx
818  *   TX queue index.
819  *
820  * @return
821  *   A pointer to the queue if it exists.
822  */
823 struct mlx5_txq_ctrl*
824 mlx5_priv_txq_get(struct priv *priv, uint16_t idx)
825 {
826         struct mlx5_txq_ctrl *ctrl = NULL;
827
828         if ((*priv->txqs)[idx]) {
829                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
830                                     txq);
831                 unsigned int i;
832
833                 mlx5_priv_txq_ibv_get(priv, idx);
834                 for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
835                         struct mlx5_mr *mr = NULL;
836
837                         (void)mr;
838                         if (ctrl->txq.mp2mr[i]) {
839                                 mr = priv_mr_get(priv, ctrl->txq.mp2mr[i]->mp);
840                                 assert(mr);
841                         }
842                 }
843                 rte_atomic32_inc(&ctrl->refcnt);
844                 DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
845                       (void *)ctrl, rte_atomic32_read(&ctrl->refcnt));
846         }
847         return ctrl;
848 }
849
850 /**
851  * Release a Tx queue.
852  *
853  * @param priv
854  *   Pointer to private structure.
855  * @param idx
856  *   TX queue index.
857  *
858  * @return
859  *   0 on success, errno on failure.
860  */
861 int
862 mlx5_priv_txq_release(struct priv *priv, uint16_t idx)
863 {
864         unsigned int i;
865         struct mlx5_txq_ctrl *txq;
866         size_t page_size = sysconf(_SC_PAGESIZE);
867
868         if (!(*priv->txqs)[idx])
869                 return 0;
870         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
871         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
872               (void *)txq, rte_atomic32_read(&txq->refcnt));
873         if (txq->ibv) {
874                 int ret;
875
876                 ret = mlx5_priv_txq_ibv_release(priv, txq->ibv);
877                 if (!ret)
878                         txq->ibv = NULL;
879         }
880         for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
881                 if (txq->txq.mp2mr[i]) {
882                         priv_mr_release(priv, txq->txq.mp2mr[i]);
883                         txq->txq.mp2mr[i] = NULL;
884                 }
885         }
886         if (priv->uar_base)
887                 munmap((void *)RTE_ALIGN_FLOOR((uintptr_t)txq->txq.bf_reg,
888                        page_size), page_size);
889         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
890                 txq_free_elts(txq);
891                 LIST_REMOVE(txq, next);
892                 rte_free(txq);
893                 (*priv->txqs)[idx] = NULL;
894                 return 0;
895         }
896         return EBUSY;
897 }
898
899 /**
900  * Verify if the queue can be released.
901  *
902  * @param priv
903  *   Pointer to private structure.
904  * @param idx
905  *   TX queue index.
906  *
907  * @return
908  *   1 if the queue can be released.
909  */
910 int
911 mlx5_priv_txq_releasable(struct priv *priv, uint16_t idx)
912 {
913         struct mlx5_txq_ctrl *txq;
914
915         if (!(*priv->txqs)[idx])
916                 return -1;
917         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
918         return (rte_atomic32_read(&txq->refcnt) == 1);
919 }
920
921 /**
922  * Verify the Tx Queue list is empty
923  *
924  * @param priv
925  *  Pointer to private structure.
926  *
927  * @return the number of object not released.
928  */
929 int
930 mlx5_priv_txq_verify(struct priv *priv)
931 {
932         struct mlx5_txq_ctrl *txq;
933         int ret = 0;
934
935         LIST_FOREACH(txq, &priv->txqsctrl, next) {
936                 DEBUG("%p: Tx Queue %p still referenced", (void *)priv,
937                       (void *)txq);
938                 ++ret;
939         }
940         return ret;
941 }