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