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