net/mlx5: fix secondary process verification
[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.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
63 /**
64  * Allocate TX queue elements.
65  *
66  * @param txq_ctrl
67  *   Pointer to TX queue structure.
68  */
69 void
70 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
71 {
72         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
73         unsigned int i;
74
75         for (i = 0; (i != elts_n); ++i)
76                 (*txq_ctrl->txq.elts)[i] = NULL;
77         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
78         txq_ctrl->txq.elts_head = 0;
79         txq_ctrl->txq.elts_tail = 0;
80         txq_ctrl->txq.elts_comp = 0;
81 }
82
83 /**
84  * Free TX queue elements.
85  *
86  * @param txq_ctrl
87  *   Pointer to TX queue structure.
88  */
89 static void
90 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
91 {
92         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
93         const uint16_t elts_m = elts_n - 1;
94         uint16_t elts_head = txq_ctrl->txq.elts_head;
95         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
96         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
97
98         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
99         txq_ctrl->txq.elts_head = 0;
100         txq_ctrl->txq.elts_tail = 0;
101         txq_ctrl->txq.elts_comp = 0;
102
103         while (elts_tail != elts_head) {
104                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
105
106                 assert(elt != NULL);
107                 rte_pktmbuf_free_seg(elt);
108 #ifndef NDEBUG
109                 /* Poisoning. */
110                 memset(&(*elts)[elts_tail & elts_m],
111                        0x77,
112                        sizeof((*elts)[elts_tail & elts_m]));
113 #endif
114                 ++elts_tail;
115         }
116 }
117
118 /**
119  * DPDK callback to configure a TX queue.
120  *
121  * @param dev
122  *   Pointer to Ethernet device structure.
123  * @param idx
124  *   TX queue index.
125  * @param desc
126  *   Number of descriptors to configure in queue.
127  * @param socket
128  *   NUMA socket on which memory must be allocated.
129  * @param[in] conf
130  *   Thresholds parameters.
131  *
132  * @return
133  *   0 on success, negative errno value on failure.
134  */
135 int
136 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
137                     unsigned int socket, const struct rte_eth_txconf *conf)
138 {
139         struct priv *priv = dev->data->dev_private;
140         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
141         struct mlx5_txq_ctrl *txq_ctrl =
142                 container_of(txq, struct mlx5_txq_ctrl, txq);
143         int ret = 0;
144
145         priv_lock(priv);
146         if (desc <= MLX5_TX_COMP_THRESH) {
147                 WARN("%p: number of descriptors requested for TX queue %u"
148                      " must be higher than MLX5_TX_COMP_THRESH, using"
149                      " %u instead of %u",
150                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
151                 desc = MLX5_TX_COMP_THRESH + 1;
152         }
153         if (!rte_is_power_of_2(desc)) {
154                 desc = 1 << log2above(desc);
155                 WARN("%p: increased number of descriptors in TX queue %u"
156                      " to the next power of two (%d)",
157                      (void *)dev, idx, desc);
158         }
159         DEBUG("%p: configuring queue %u for %u descriptors",
160               (void *)dev, idx, desc);
161         if (idx >= priv->txqs_n) {
162                 ERROR("%p: queue index out of range (%u >= %u)",
163                       (void *)dev, idx, priv->txqs_n);
164                 priv_unlock(priv);
165                 return -EOVERFLOW;
166         }
167         if (!mlx5_priv_txq_releasable(priv, idx)) {
168                 ret = EBUSY;
169                 ERROR("%p: unable to release queue index %u",
170                       (void *)dev, idx);
171                 goto out;
172         }
173         mlx5_priv_txq_release(priv, idx);
174         txq_ctrl = mlx5_priv_txq_new(priv, idx, desc, socket, conf);
175         if (!txq_ctrl) {
176                 ERROR("%p: unable to allocate queue index %u",
177                       (void *)dev, idx);
178                 ret = ENOMEM;
179                 goto out;
180         }
181         DEBUG("%p: adding TX queue %p to list",
182               (void *)dev, (void *)txq_ctrl);
183         (*priv->txqs)[idx] = &txq_ctrl->txq;
184 out:
185         priv_unlock(priv);
186         return -ret;
187 }
188
189 /**
190  * DPDK callback to release a TX queue.
191  *
192  * @param dpdk_txq
193  *   Generic TX queue pointer.
194  */
195 void
196 mlx5_tx_queue_release(void *dpdk_txq)
197 {
198         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
199         struct mlx5_txq_ctrl *txq_ctrl;
200         struct priv *priv;
201         unsigned int i;
202
203         if (txq == NULL)
204                 return;
205         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
206         priv = txq_ctrl->priv;
207         priv_lock(priv);
208         for (i = 0; (i != priv->txqs_n); ++i)
209                 if ((*priv->txqs)[i] == txq) {
210                         DEBUG("%p: removing TX queue %p from list",
211                               (void *)priv->dev, (void *)txq_ctrl);
212                         mlx5_priv_txq_release(priv, i);
213                         break;
214                 }
215         priv_unlock(priv);
216 }
217
218
219 /**
220  * Map locally UAR used in Tx queues for BlueFlame doorbell.
221  *
222  * @param[in] priv
223  *   Pointer to private structure.
224  * @param fd
225  *   Verbs file descriptor to map UAR pages.
226  *
227  * @return
228  *   0 on success, errno value on failure.
229  */
230 int
231 priv_tx_uar_remap(struct priv *priv, int fd)
232 {
233         unsigned int i, j;
234         uintptr_t pages[priv->txqs_n];
235         unsigned int pages_n = 0;
236         uintptr_t uar_va;
237         void *addr;
238         struct mlx5_txq_data *txq;
239         struct mlx5_txq_ctrl *txq_ctrl;
240         int already_mapped;
241         size_t page_size = sysconf(_SC_PAGESIZE);
242
243         memset(pages, 0, priv->txqs_n * sizeof(uintptr_t));
244         /*
245          * As rdma-core, UARs are mapped in size of OS page size.
246          * Use aligned address to avoid duplicate mmap.
247          * Ref to libmlx5 function: mlx5_init_context()
248          */
249         for (i = 0; i != priv->txqs_n; ++i) {
250                 txq = (*priv->txqs)[i];
251                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
252                 uar_va = (uintptr_t)txq_ctrl->txq.bf_reg;
253                 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size);
254                 already_mapped = 0;
255                 for (j = 0; j != pages_n; ++j) {
256                         if (pages[j] == uar_va) {
257                                 already_mapped = 1;
258                                 break;
259                         }
260                 }
261                 if (already_mapped)
262                         continue;
263                 pages[pages_n++] = uar_va;
264                 addr = mmap((void *)uar_va, page_size,
265                             PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
266                             txq_ctrl->uar_mmap_offset);
267                 if (addr != (void *)uar_va) {
268                         ERROR("call to mmap failed on UAR for txq %d\n", i);
269                         return -1;
270                 }
271         }
272         return 0;
273 }
274
275 /**
276  * Create the Tx queue Verbs object.
277  *
278  * @param priv
279  *   Pointer to private structure.
280  * @param idx
281  *   Queue index in DPDK Rx queue array
282  *
283  * @return
284  *   The Verbs object initialised if it can be created.
285  */
286 struct mlx5_txq_ibv*
287 mlx5_priv_txq_ibv_new(struct priv *priv, uint16_t idx)
288 {
289         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
290         struct mlx5_txq_ctrl *txq_ctrl =
291                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
292         struct mlx5_txq_ibv tmpl;
293         struct mlx5_txq_ibv *txq_ibv;
294         union {
295                 struct ibv_qp_init_attr_ex init;
296                 struct ibv_cq_init_attr_ex cq;
297                 struct ibv_qp_attr mod;
298                 struct ibv_cq_ex cq_attr;
299         } attr;
300         unsigned int cqe_n;
301         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
302         struct mlx5dv_cq cq_info;
303         struct mlx5dv_obj obj;
304         const int desc = 1 << txq_data->elts_n;
305         int ret = 0;
306
307         assert(txq_data);
308         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
309                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
310                 goto error;
311         }
312         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
313         /* MRs will be registered in mp2mr[] later. */
314         attr.cq = (struct ibv_cq_init_attr_ex){
315                 .comp_mask = 0,
316         };
317         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
318                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
319         if (priv->mps == MLX5_MPW_ENHANCED)
320                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
321         tmpl.cq = ibv_create_cq(priv->ctx, cqe_n, NULL, NULL, 0);
322         if (tmpl.cq == NULL) {
323                 ERROR("%p: CQ creation failure", (void *)txq_ctrl);
324                 goto error;
325         }
326         attr.init = (struct ibv_qp_init_attr_ex){
327                 /* CQ to be associated with the send queue. */
328                 .send_cq = tmpl.cq,
329                 /* CQ to be associated with the receive queue. */
330                 .recv_cq = tmpl.cq,
331                 .cap = {
332                         /* Max number of outstanding WRs. */
333                         .max_send_wr =
334                                 ((priv->device_attr.orig_attr.max_qp_wr <
335                                   desc) ?
336                                  priv->device_attr.orig_attr.max_qp_wr :
337                                  desc),
338                         /*
339                          * Max number of scatter/gather elements in a WR,
340                          * must be 1 to prevent libmlx5 from trying to affect
341                          * too much memory. TX gather is not impacted by the
342                          * priv->device_attr.max_sge limit and will still work
343                          * properly.
344                          */
345                         .max_send_sge = 1,
346                 },
347                 .qp_type = IBV_QPT_RAW_PACKET,
348                 /*
349                  * Do *NOT* enable this, completions events are managed per
350                  * Tx burst.
351                  */
352                 .sq_sig_all = 0,
353                 .pd = priv->pd,
354                 .comp_mask = IBV_QP_INIT_ATTR_PD,
355         };
356         if (txq_data->inline_en)
357                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
358         if (txq_data->tso_en) {
359                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
360                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
361         }
362         tmpl.qp = ibv_create_qp_ex(priv->ctx, &attr.init);
363         if (tmpl.qp == NULL) {
364                 ERROR("%p: QP creation failure", (void *)txq_ctrl);
365                 goto error;
366         }
367         attr.mod = (struct ibv_qp_attr){
368                 /* Move the QP to this state. */
369                 .qp_state = IBV_QPS_INIT,
370                 /* Primary port number. */
371                 .port_num = priv->port
372         };
373         ret = ibv_modify_qp(tmpl.qp, &attr.mod, (IBV_QP_STATE | IBV_QP_PORT));
374         if (ret) {
375                 ERROR("%p: QP state to IBV_QPS_INIT failed", (void *)txq_ctrl);
376                 goto error;
377         }
378         attr.mod = (struct ibv_qp_attr){
379                 .qp_state = IBV_QPS_RTR
380         };
381         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
382         if (ret) {
383                 ERROR("%p: QP state to IBV_QPS_RTR failed", (void *)txq_ctrl);
384                 goto error;
385         }
386         attr.mod.qp_state = IBV_QPS_RTS;
387         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
388         if (ret) {
389                 ERROR("%p: QP state to IBV_QPS_RTS failed", (void *)txq_ctrl);
390                 goto error;
391         }
392         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
393                                     txq_ctrl->socket);
394         if (!txq_ibv) {
395                 ERROR("%p: cannot allocate memory", (void *)txq_ctrl);
396                 goto error;
397         }
398         obj.cq.in = tmpl.cq;
399         obj.cq.out = &cq_info;
400         obj.qp.in = tmpl.qp;
401         obj.qp.out = &qp;
402         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
403         if (ret != 0)
404                 goto error;
405         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
406                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
407                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
408                 goto error;
409         }
410         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
411         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
412         txq_data->wqes = qp.sq.buf;
413         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
414         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
415         txq_data->bf_reg = qp.bf.reg;
416         txq_data->cq_db = cq_info.dbrec;
417         txq_data->cqes =
418                 (volatile struct mlx5_cqe (*)[])
419                 (uintptr_t)cq_info.buf;
420         txq_data->cq_ci = 0;
421         txq_data->cq_pi = 0;
422         txq_data->wqe_ci = 0;
423         txq_data->wqe_pi = 0;
424         txq_ibv->qp = tmpl.qp;
425         txq_ibv->cq = tmpl.cq;
426         rte_atomic32_inc(&txq_ibv->refcnt);
427         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
428                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
429         } else {
430                 ERROR("Failed to retrieve UAR info, invalid libmlx5.so version");
431                 goto error;
432         }
433         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
434               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
435         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
436         return txq_ibv;
437 error:
438         if (tmpl.cq)
439                 claim_zero(ibv_destroy_cq(tmpl.cq));
440         if (tmpl.qp)
441                 claim_zero(ibv_destroy_qp(tmpl.qp));
442         return NULL;
443 }
444
445 /**
446  * Get an Tx queue Verbs object.
447  *
448  * @param priv
449  *   Pointer to private structure.
450  * @param idx
451  *   Queue index in DPDK Rx queue array
452  *
453  * @return
454  *   The Verbs object if it exists.
455  */
456 struct mlx5_txq_ibv*
457 mlx5_priv_txq_ibv_get(struct priv *priv, uint16_t idx)
458 {
459         struct mlx5_txq_ctrl *txq_ctrl;
460
461         if (idx >= priv->txqs_n)
462                 return NULL;
463         if (!(*priv->txqs)[idx])
464                 return NULL;
465         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
466         if (txq_ctrl->ibv) {
467                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
468                 DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
469                       (void *)txq_ctrl->ibv,
470                       rte_atomic32_read(&txq_ctrl->ibv->refcnt));
471         }
472         return txq_ctrl->ibv;
473 }
474
475 /**
476  * Release an Tx verbs queue object.
477  *
478  * @param priv
479  *   Pointer to private structure.
480  * @param txq_ibv
481  *   Verbs Tx queue object.
482  *
483  * @return
484  *   0 on success, errno on failure.
485  */
486 int
487 mlx5_priv_txq_ibv_release(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
488 {
489         (void)priv;
490         assert(txq_ibv);
491         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
492               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
493         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
494                 claim_zero(ibv_destroy_qp(txq_ibv->qp));
495                 claim_zero(ibv_destroy_cq(txq_ibv->cq));
496                 LIST_REMOVE(txq_ibv, next);
497                 rte_free(txq_ibv);
498                 return 0;
499         }
500         return EBUSY;
501 }
502
503 /**
504  * Return true if a single reference exists on the object.
505  *
506  * @param priv
507  *   Pointer to private structure.
508  * @param txq_ibv
509  *   Verbs Tx queue object.
510  */
511 int
512 mlx5_priv_txq_ibv_releasable(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
513 {
514         (void)priv;
515         assert(txq_ibv);
516         return (rte_atomic32_read(&txq_ibv->refcnt) == 1);
517 }
518
519 /**
520  * Verify the Verbs Tx queue list is empty
521  *
522  * @param priv
523  *  Pointer to private structure.
524  *
525  * @return the number of object not released.
526  */
527 int
528 mlx5_priv_txq_ibv_verify(struct priv *priv)
529 {
530         int ret = 0;
531         struct mlx5_txq_ibv *txq_ibv;
532
533         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
534                 DEBUG("%p: Verbs Tx queue %p still referenced", (void *)priv,
535                       (void *)txq_ibv);
536                 ++ret;
537         }
538         return ret;
539 }
540
541 /**
542  * Create a DPDK Tx queue.
543  *
544  * @param priv
545  *   Pointer to private structure.
546  * @param idx
547  *   TX queue index.
548  * @param desc
549  *   Number of descriptors to configure in queue.
550  * @param socket
551  *   NUMA socket on which memory must be allocated.
552  * @param[in] conf
553  *  Thresholds parameters.
554  *
555  * @return
556  *   A DPDK queue object on success.
557  */
558 struct mlx5_txq_ctrl*
559 mlx5_priv_txq_new(struct priv *priv, uint16_t idx, uint16_t desc,
560                   unsigned int socket,
561                   const struct rte_eth_txconf *conf)
562 {
563         const unsigned int max_tso_inline =
564                 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
565                  RTE_CACHE_LINE_SIZE);
566         struct mlx5_txq_ctrl *tmpl;
567
568         tmpl = rte_calloc_socket("TXQ", 1,
569                                  sizeof(*tmpl) +
570                                  desc * sizeof(struct rte_mbuf *),
571                                  0, socket);
572         if (!tmpl)
573                 return NULL;
574         assert(desc > MLX5_TX_COMP_THRESH);
575         tmpl->txq.flags = conf->txq_flags;
576         tmpl->priv = priv;
577         tmpl->socket = socket;
578         tmpl->txq.elts_n = log2above(desc);
579         if (priv->mps == MLX5_MPW_ENHANCED)
580                 tmpl->txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
581         /* MRs will be registered in mp2mr[] later. */
582         DEBUG("priv->device_attr.max_qp_wr is %d",
583               priv->device_attr.orig_attr.max_qp_wr);
584         DEBUG("priv->device_attr.max_sge is %d",
585               priv->device_attr.orig_attr.max_sge);
586         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
587                 unsigned int ds_cnt;
588
589                 tmpl->txq.max_inline =
590                         ((priv->txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
591                          RTE_CACHE_LINE_SIZE);
592                 tmpl->txq.inline_en = 1;
593                 /* TSO and MPS can't be enabled concurrently. */
594                 assert(!priv->tso || !priv->mps);
595                 if (priv->mps == MLX5_MPW_ENHANCED) {
596                         tmpl->txq.inline_max_packet_sz =
597                                 priv->inline_max_packet_sz;
598                         /* To minimize the size of data set, avoid requesting
599                          * too large WQ.
600                          */
601                         tmpl->max_inline_data =
602                                 ((RTE_MIN(priv->txq_inline,
603                                           priv->inline_max_packet_sz) +
604                                   (RTE_CACHE_LINE_SIZE - 1)) /
605                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
606                 } else if (priv->tso) {
607                         int inline_diff = tmpl->txq.max_inline - max_tso_inline;
608
609                         /*
610                          * Adjust inline value as Verbs aggregates
611                          * tso_inline and txq_inline fields.
612                          */
613                         tmpl->max_inline_data = inline_diff > 0 ?
614                                                inline_diff *
615                                                RTE_CACHE_LINE_SIZE :
616                                                0;
617                 } else {
618                         tmpl->max_inline_data =
619                                 tmpl->txq.max_inline * RTE_CACHE_LINE_SIZE;
620                 }
621                 /*
622                  * Check if the inline size is too large in a way which
623                  * can make the WQE DS to overflow.
624                  * Considering in calculation:
625                  *      WQE CTRL (1 DS)
626                  *      WQE ETH  (1 DS)
627                  *      Inline part (N DS)
628                  */
629                 ds_cnt = 2 + (tmpl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
630                 if (ds_cnt > MLX5_DSEG_MAX) {
631                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
632                                                   MLX5_WQE_DWORD_SIZE;
633
634                         max_inline = max_inline - (max_inline %
635                                                    RTE_CACHE_LINE_SIZE);
636                         WARN("txq inline is too large (%d) setting it to "
637                              "the maximum possible: %d\n",
638                              priv->txq_inline, max_inline);
639                         tmpl->txq.max_inline = max_inline / RTE_CACHE_LINE_SIZE;
640                 }
641         }
642         if (priv->tso) {
643                 tmpl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
644                 tmpl->txq.max_inline = RTE_MAX(tmpl->txq.max_inline,
645                                                max_tso_inline);
646                 tmpl->txq.tso_en = 1;
647         }
648         if (priv->tunnel_en)
649                 tmpl->txq.tunnel_en = 1;
650         tmpl->txq.elts =
651                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
652         tmpl->txq.stats.idx = idx;
653         rte_atomic32_inc(&tmpl->refcnt);
654         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
655               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
656         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
657         return tmpl;
658 }
659
660 /**
661  * Get a Tx queue.
662  *
663  * @param priv
664  *   Pointer to private structure.
665  * @param idx
666  *   TX queue index.
667  *
668  * @return
669  *   A pointer to the queue if it exists.
670  */
671 struct mlx5_txq_ctrl*
672 mlx5_priv_txq_get(struct priv *priv, uint16_t idx)
673 {
674         struct mlx5_txq_ctrl *ctrl = NULL;
675
676         if ((*priv->txqs)[idx]) {
677                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
678                                     txq);
679                 unsigned int i;
680
681                 mlx5_priv_txq_ibv_get(priv, idx);
682                 for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
683                         struct mlx5_mr *mr = NULL;
684
685                         (void)mr;
686                         if (ctrl->txq.mp2mr[i]) {
687                                 mr = priv_mr_get(priv, ctrl->txq.mp2mr[i]->mp);
688                                 assert(mr);
689                         }
690                 }
691                 rte_atomic32_inc(&ctrl->refcnt);
692                 DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
693                       (void *)ctrl, rte_atomic32_read(&ctrl->refcnt));
694         }
695         return ctrl;
696 }
697
698 /**
699  * Release a Tx queue.
700  *
701  * @param priv
702  *   Pointer to private structure.
703  * @param idx
704  *   TX queue index.
705  *
706  * @return
707  *   0 on success, errno on failure.
708  */
709 int
710 mlx5_priv_txq_release(struct priv *priv, uint16_t idx)
711 {
712         unsigned int i;
713         struct mlx5_txq_ctrl *txq;
714
715         if (!(*priv->txqs)[idx])
716                 return 0;
717         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
718         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
719               (void *)txq, rte_atomic32_read(&txq->refcnt));
720         if (txq->ibv) {
721                 int ret;
722
723                 ret = mlx5_priv_txq_ibv_release(priv, txq->ibv);
724                 if (!ret)
725                         txq->ibv = NULL;
726         }
727         for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
728                 if (txq->txq.mp2mr[i]) {
729                         priv_mr_release(priv, txq->txq.mp2mr[i]);
730                         txq->txq.mp2mr[i] = NULL;
731                 }
732         }
733         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
734                 txq_free_elts(txq);
735                 LIST_REMOVE(txq, next);
736                 rte_free(txq);
737                 (*priv->txqs)[idx] = NULL;
738                 return 0;
739         }
740         return EBUSY;
741 }
742
743 /**
744  * Verify if the queue can be released.
745  *
746  * @param priv
747  *   Pointer to private structure.
748  * @param idx
749  *   TX queue index.
750  *
751  * @return
752  *   1 if the queue can be released.
753  */
754 int
755 mlx5_priv_txq_releasable(struct priv *priv, uint16_t idx)
756 {
757         struct mlx5_txq_ctrl *txq;
758
759         if (!(*priv->txqs)[idx])
760                 return -1;
761         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
762         return (rte_atomic32_read(&txq->refcnt) == 1);
763 }
764
765 /**
766  * Verify the Tx Queue list is empty
767  *
768  * @param priv
769  *  Pointer to private structure.
770  *
771  * @return the number of object not released.
772  */
773 int
774 mlx5_priv_txq_verify(struct priv *priv)
775 {
776         struct mlx5_txq_ctrl *txq;
777         int ret = 0;
778
779         LIST_FOREACH(txq, &priv->txqsctrl, next) {
780                 DEBUG("%p: Tx Queue %p still referenced", (void *)priv,
781                       (void *)txq);
782                 ++ret;
783         }
784         return ret;
785 }