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