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