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