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