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