net/mlx5: fix Tx queue release debug log timing
[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 <errno.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <sys/mman.h>
12 #include <inttypes.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 #include <infiniband/mlx5dv.h>
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic error "-Wpedantic"
23 #endif
24
25 #include <rte_mbuf.h>
26 #include <rte_malloc.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include <mlx5_glue.h>
31 #include <mlx5_devx_cmds.h>
32 #include <mlx5_common.h>
33 #include <mlx5_common_mr.h>
34
35 #include "mlx5_defs.h"
36 #include "mlx5_utils.h"
37 #include "mlx5.h"
38 #include "mlx5_rxtx.h"
39 #include "mlx5_autoconf.h"
40
41 /**
42  * Allocate TX queue elements.
43  *
44  * @param txq_ctrl
45  *   Pointer to TX queue structure.
46  */
47 void
48 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
49 {
50         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
51         unsigned int i;
52
53         for (i = 0; (i != elts_n); ++i)
54                 txq_ctrl->txq.elts[i] = NULL;
55         DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
56                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
57         txq_ctrl->txq.elts_head = 0;
58         txq_ctrl->txq.elts_tail = 0;
59         txq_ctrl->txq.elts_comp = 0;
60 }
61
62 /**
63  * Free TX queue elements.
64  *
65  * @param txq_ctrl
66  *   Pointer to TX queue structure.
67  */
68 void
69 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
70 {
71         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
72         const uint16_t elts_m = elts_n - 1;
73         uint16_t elts_head = txq_ctrl->txq.elts_head;
74         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
75         struct rte_mbuf *(*elts)[elts_n] = &txq_ctrl->txq.elts;
76
77         DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
78                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
79         txq_ctrl->txq.elts_head = 0;
80         txq_ctrl->txq.elts_tail = 0;
81         txq_ctrl->txq.elts_comp = 0;
82
83         while (elts_tail != elts_head) {
84                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
85
86                 MLX5_ASSERT(elt != NULL);
87                 rte_pktmbuf_free_seg(elt);
88 #ifdef RTE_LIBRTE_MLX5_DEBUG
89                 /* Poisoning. */
90                 memset(&(*elts)[elts_tail & elts_m],
91                        0x77,
92                        sizeof((*elts)[elts_tail & elts_m]));
93 #endif
94                 ++elts_tail;
95         }
96 }
97
98 /**
99  * Returns the per-port supported offloads.
100  *
101  * @param dev
102  *   Pointer to Ethernet device.
103  *
104  * @return
105  *   Supported Tx offloads.
106  */
107 uint64_t
108 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
109 {
110         struct mlx5_priv *priv = dev->data->dev_private;
111         uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
112                              DEV_TX_OFFLOAD_VLAN_INSERT);
113         struct mlx5_dev_config *config = &priv->config;
114
115         if (config->hw_csum)
116                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
117                              DEV_TX_OFFLOAD_UDP_CKSUM |
118                              DEV_TX_OFFLOAD_TCP_CKSUM);
119         if (config->tso)
120                 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
121         if (config->swp) {
122                 if (config->hw_csum)
123                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
124                 if (config->tso)
125                         offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
126                                      DEV_TX_OFFLOAD_UDP_TNL_TSO);
127         }
128         if (config->tunnel_en) {
129                 if (config->hw_csum)
130                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
131                 if (config->tso)
132                         offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
133                                      DEV_TX_OFFLOAD_GRE_TNL_TSO |
134                                      DEV_TX_OFFLOAD_GENEVE_TNL_TSO);
135         }
136         return offloads;
137 }
138
139 /**
140  * Tx queue presetup checks.
141  *
142  * @param dev
143  *   Pointer to Ethernet device structure.
144  * @param idx
145  *   Tx queue index.
146  * @param desc
147  *   Number of descriptors to configure in queue.
148  *
149  * @return
150  *   0 on success, a negative errno value otherwise and rte_errno is set.
151  */
152 static int
153 mlx5_tx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc)
154 {
155         struct mlx5_priv *priv = dev->data->dev_private;
156
157         if (desc <= MLX5_TX_COMP_THRESH) {
158                 DRV_LOG(WARNING,
159                         "port %u number of descriptors requested for Tx queue"
160                         " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
161                         " instead of %u",
162                         dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
163                 desc = MLX5_TX_COMP_THRESH + 1;
164         }
165         if (!rte_is_power_of_2(desc)) {
166                 desc = 1 << log2above(desc);
167                 DRV_LOG(WARNING,
168                         "port %u increased number of descriptors in Tx queue"
169                         " %u to the next power of two (%d)",
170                         dev->data->port_id, idx, desc);
171         }
172         DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
173                 dev->data->port_id, idx, desc);
174         if (idx >= priv->txqs_n) {
175                 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
176                         dev->data->port_id, idx, priv->txqs_n);
177                 rte_errno = EOVERFLOW;
178                 return -rte_errno;
179         }
180         if (!mlx5_txq_releasable(dev, idx)) {
181                 rte_errno = EBUSY;
182                 DRV_LOG(ERR, "port %u unable to release queue index %u",
183                         dev->data->port_id, idx);
184                 return -rte_errno;
185         }
186         mlx5_txq_release(dev, idx);
187         return 0;
188 }
189 /**
190  * DPDK callback to configure a TX queue.
191  *
192  * @param dev
193  *   Pointer to Ethernet device structure.
194  * @param idx
195  *   TX queue index.
196  * @param desc
197  *   Number of descriptors to configure in queue.
198  * @param socket
199  *   NUMA socket on which memory must be allocated.
200  * @param[in] conf
201  *   Thresholds parameters.
202  *
203  * @return
204  *   0 on success, a negative errno value otherwise and rte_errno is set.
205  */
206 int
207 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
208                     unsigned int socket, const struct rte_eth_txconf *conf)
209 {
210         struct mlx5_priv *priv = dev->data->dev_private;
211         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
212         struct mlx5_txq_ctrl *txq_ctrl =
213                 container_of(txq, struct mlx5_txq_ctrl, txq);
214         int res;
215
216         res = mlx5_tx_queue_pre_setup(dev, idx, desc);
217         if (res)
218                 return res;
219         txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
220         if (!txq_ctrl) {
221                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
222                         dev->data->port_id, idx);
223                 return -rte_errno;
224         }
225         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
226                 dev->data->port_id, idx);
227         (*priv->txqs)[idx] = &txq_ctrl->txq;
228         return 0;
229 }
230
231 /**
232  * DPDK callback to configure a TX hairpin queue.
233  *
234  * @param dev
235  *   Pointer to Ethernet device structure.
236  * @param idx
237  *   TX queue index.
238  * @param desc
239  *   Number of descriptors to configure in queue.
240  * @param[in] hairpin_conf
241  *   The hairpin binding configuration.
242  *
243  * @return
244  *   0 on success, a negative errno value otherwise and rte_errno is set.
245  */
246 int
247 mlx5_tx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
248                             uint16_t desc,
249                             const struct rte_eth_hairpin_conf *hairpin_conf)
250 {
251         struct mlx5_priv *priv = dev->data->dev_private;
252         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
253         struct mlx5_txq_ctrl *txq_ctrl =
254                 container_of(txq, struct mlx5_txq_ctrl, txq);
255         int res;
256
257         res = mlx5_tx_queue_pre_setup(dev, idx, desc);
258         if (res)
259                 return res;
260         if (hairpin_conf->peer_count != 1 ||
261             hairpin_conf->peers[0].port != dev->data->port_id ||
262             hairpin_conf->peers[0].queue >= priv->rxqs_n) {
263                 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
264                         " invalid hairpind configuration", dev->data->port_id,
265                         idx);
266                 rte_errno = EINVAL;
267                 return -rte_errno;
268         }
269         txq_ctrl = mlx5_txq_hairpin_new(dev, idx, desc, hairpin_conf);
270         if (!txq_ctrl) {
271                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
272                         dev->data->port_id, idx);
273                 return -rte_errno;
274         }
275         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
276                 dev->data->port_id, idx);
277         (*priv->txqs)[idx] = &txq_ctrl->txq;
278         return 0;
279 }
280
281 /**
282  * DPDK callback to release a TX queue.
283  *
284  * @param dpdk_txq
285  *   Generic TX queue pointer.
286  */
287 void
288 mlx5_tx_queue_release(void *dpdk_txq)
289 {
290         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
291         struct mlx5_txq_ctrl *txq_ctrl;
292         struct mlx5_priv *priv;
293         unsigned int i;
294
295         if (txq == NULL)
296                 return;
297         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
298         priv = txq_ctrl->priv;
299         for (i = 0; (i != priv->txqs_n); ++i)
300                 if ((*priv->txqs)[i] == txq) {
301                         DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
302                                 PORT_ID(priv), txq->idx);
303                         mlx5_txq_release(ETH_DEV(priv), i);
304                         break;
305                 }
306 }
307
308 /**
309  * Configure the doorbell register non-cached attribute.
310  *
311  * @param txq_ctrl
312  *   Pointer to Tx queue control structure.
313  * @param page_size
314  *   Systme page size
315  */
316 static void
317 txq_uar_ncattr_init(struct mlx5_txq_ctrl *txq_ctrl, size_t page_size)
318 {
319         struct mlx5_priv *priv = txq_ctrl->priv;
320         off_t cmd;
321
322         txq_ctrl->txq.db_heu = priv->config.dbnc == MLX5_TXDB_HEURISTIC;
323         txq_ctrl->txq.db_nc = 0;
324         /* Check the doorbell register mapping type. */
325         cmd = txq_ctrl->uar_mmap_offset / page_size;
326         cmd >>= MLX5_UAR_MMAP_CMD_SHIFT;
327         cmd &= MLX5_UAR_MMAP_CMD_MASK;
328         if (cmd == MLX5_MMAP_GET_NC_PAGES_CMD)
329                 txq_ctrl->txq.db_nc = 1;
330 }
331
332 /**
333  * Initialize Tx UAR registers for primary process.
334  *
335  * @param txq_ctrl
336  *   Pointer to Tx queue control structure.
337  */
338 static void
339 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
340 {
341         struct mlx5_priv *priv = txq_ctrl->priv;
342         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
343         const size_t page_size = sysconf(_SC_PAGESIZE);
344 #ifndef RTE_ARCH_64
345         unsigned int lock_idx;
346 #endif
347
348         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
349                 return;
350         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
351         MLX5_ASSERT(ppriv);
352         ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
353         txq_uar_ncattr_init(txq_ctrl, page_size);
354 #ifndef RTE_ARCH_64
355         /* Assign an UAR lock according to UAR page number */
356         lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
357                    MLX5_UAR_PAGE_NUM_MASK;
358         txq_ctrl->txq.uar_lock = &priv->uar_lock[lock_idx];
359 #endif
360 }
361
362 /**
363  * Remap UAR register of a Tx queue for secondary process.
364  *
365  * Remapped address is stored at the table in the process private structure of
366  * the device, indexed by queue index.
367  *
368  * @param txq_ctrl
369  *   Pointer to Tx queue control structure.
370  * @param fd
371  *   Verbs file descriptor to map UAR pages.
372  *
373  * @return
374  *   0 on success, a negative errno value otherwise and rte_errno is set.
375  */
376 static int
377 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
378 {
379         struct mlx5_priv *priv = txq_ctrl->priv;
380         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
381         struct mlx5_txq_data *txq = &txq_ctrl->txq;
382         void *addr;
383         uintptr_t uar_va;
384         uintptr_t offset;
385         const size_t page_size = sysconf(_SC_PAGESIZE);
386
387         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
388                 return 0;
389         MLX5_ASSERT(ppriv);
390         /*
391          * As rdma-core, UARs are mapped in size of OS page
392          * size. Ref to libmlx5 function: mlx5_init_context()
393          */
394         uar_va = (uintptr_t)txq_ctrl->bf_reg;
395         offset = uar_va & (page_size - 1); /* Offset in page. */
396         addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
397                         txq_ctrl->uar_mmap_offset);
398         if (addr == MAP_FAILED) {
399                 DRV_LOG(ERR,
400                         "port %u mmap failed for BF reg of txq %u",
401                         txq->port_id, txq->idx);
402                 rte_errno = ENXIO;
403                 return -rte_errno;
404         }
405         addr = RTE_PTR_ADD(addr, offset);
406         ppriv->uar_table[txq->idx] = addr;
407         txq_uar_ncattr_init(txq_ctrl, page_size);
408         return 0;
409 }
410
411 /**
412  * Unmap UAR register of a Tx queue for secondary process.
413  *
414  * @param txq_ctrl
415  *   Pointer to Tx queue control structure.
416  */
417 static void
418 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
419 {
420         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
421         const size_t page_size = sysconf(_SC_PAGESIZE);
422         void *addr;
423
424         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
425                 return;
426         addr = ppriv->uar_table[txq_ctrl->txq.idx];
427         munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
428 }
429
430 /**
431  * Initialize Tx UAR registers for secondary process.
432  *
433  * @param dev
434  *   Pointer to Ethernet device.
435  * @param fd
436  *   Verbs file descriptor to map UAR pages.
437  *
438  * @return
439  *   0 on success, a negative errno value otherwise and rte_errno is set.
440  */
441 int
442 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
443 {
444         struct mlx5_priv *priv = dev->data->dev_private;
445         struct mlx5_txq_data *txq;
446         struct mlx5_txq_ctrl *txq_ctrl;
447         unsigned int i;
448         int ret;
449
450         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
451         for (i = 0; i != priv->txqs_n; ++i) {
452                 if (!(*priv->txqs)[i])
453                         continue;
454                 txq = (*priv->txqs)[i];
455                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
456                 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
457                         continue;
458                 MLX5_ASSERT(txq->idx == (uint16_t)i);
459                 ret = txq_uar_init_secondary(txq_ctrl, fd);
460                 if (ret)
461                         goto error;
462         }
463         return 0;
464 error:
465         /* Rollback. */
466         do {
467                 if (!(*priv->txqs)[i])
468                         continue;
469                 txq = (*priv->txqs)[i];
470                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
471                 txq_uar_uninit_secondary(txq_ctrl);
472         } while (i--);
473         return -rte_errno;
474 }
475
476 /**
477  * Create the Tx hairpin queue object.
478  *
479  * @param dev
480  *   Pointer to Ethernet device.
481  * @param idx
482  *   Queue index in DPDK Tx queue array
483  *
484  * @return
485  *   The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
486  */
487 static struct mlx5_txq_obj *
488 mlx5_txq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
489 {
490         struct mlx5_priv *priv = dev->data->dev_private;
491         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
492         struct mlx5_txq_ctrl *txq_ctrl =
493                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
494         struct mlx5_devx_create_sq_attr attr = { 0 };
495         struct mlx5_txq_obj *tmpl = NULL;
496         int ret = 0;
497         uint32_t max_wq_data;
498
499         MLX5_ASSERT(txq_data);
500         MLX5_ASSERT(!txq_ctrl->obj);
501         tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
502                                  txq_ctrl->socket);
503         if (!tmpl) {
504                 DRV_LOG(ERR,
505                         "port %u Tx queue %u cannot allocate memory resources",
506                         dev->data->port_id, txq_data->idx);
507                 rte_errno = ENOMEM;
508                 goto error;
509         }
510         tmpl->type = MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN;
511         tmpl->txq_ctrl = txq_ctrl;
512         attr.hairpin = 1;
513         attr.tis_lst_sz = 1;
514         max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
515         /* Jumbo frames > 9KB should be supported, and more packets. */
516         if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
517                 if (priv->config.log_hp_size > max_wq_data) {
518                         DRV_LOG(ERR, "total data size %u power of 2 is "
519                                 "too large for hairpin",
520                                 priv->config.log_hp_size);
521                         rte_errno = ERANGE;
522                         return NULL;
523                 }
524                 attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
525         } else {
526                 attr.wq_attr.log_hairpin_data_sz =
527                                 (max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
528                                  max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
529         }
530         /* Set the packets number to the maximum value for performance. */
531         attr.wq_attr.log_hairpin_num_packets =
532                         attr.wq_attr.log_hairpin_data_sz -
533                         MLX5_HAIRPIN_QUEUE_STRIDE;
534         attr.tis_num = priv->sh->tis->id;
535         tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->ctx, &attr);
536         if (!tmpl->sq) {
537                 DRV_LOG(ERR,
538                         "port %u tx hairpin queue %u can't create sq object",
539                         dev->data->port_id, idx);
540                 rte_errno = errno;
541                 goto error;
542         }
543         DRV_LOG(DEBUG, "port %u sxq %u updated with %p", dev->data->port_id,
544                 idx, (void *)&tmpl);
545         rte_atomic32_inc(&tmpl->refcnt);
546         LIST_INSERT_HEAD(&priv->txqsobj, tmpl, next);
547         return tmpl;
548 error:
549         ret = rte_errno; /* Save rte_errno before cleanup. */
550         if (tmpl->tis)
551                 mlx5_devx_cmd_destroy(tmpl->tis);
552         if (tmpl->sq)
553                 mlx5_devx_cmd_destroy(tmpl->sq);
554         rte_errno = ret; /* Restore rte_errno. */
555         return NULL;
556 }
557
558 /**
559  * Create the Tx queue Verbs object.
560  *
561  * @param dev
562  *   Pointer to Ethernet device.
563  * @param idx
564  *   Queue index in DPDK Tx queue array.
565  * @param type
566  *   Type of the Tx queue object to create.
567  *
568  * @return
569  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
570  */
571 struct mlx5_txq_obj *
572 mlx5_txq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
573                  enum mlx5_txq_obj_type type)
574 {
575         struct mlx5_priv *priv = dev->data->dev_private;
576         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
577         struct mlx5_txq_ctrl *txq_ctrl =
578                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
579         struct mlx5_txq_obj tmpl;
580         struct mlx5_txq_obj *txq_obj = NULL;
581         union {
582                 struct ibv_qp_init_attr_ex init;
583                 struct ibv_cq_init_attr_ex cq;
584                 struct ibv_qp_attr mod;
585         } attr;
586         unsigned int cqe_n;
587         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
588         struct mlx5dv_cq cq_info;
589         struct mlx5dv_obj obj;
590         const int desc = 1 << txq_data->elts_n;
591         int ret = 0;
592
593         if (type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN)
594                 return mlx5_txq_obj_hairpin_new(dev, idx);
595 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
596         /* If using DevX, need additional mask to read tisn value. */
597         if (priv->config.devx && !priv->sh->tdn)
598                 qp.comp_mask |= MLX5DV_QP_MASK_RAW_QP_HANDLES;
599 #endif
600         MLX5_ASSERT(txq_data);
601         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
602         priv->verbs_alloc_ctx.obj = txq_ctrl;
603         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
604                 DRV_LOG(ERR,
605                         "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
606                         dev->data->port_id);
607                 rte_errno = EINVAL;
608                 return NULL;
609         }
610         memset(&tmpl, 0, sizeof(struct mlx5_txq_obj));
611         attr.cq = (struct ibv_cq_init_attr_ex){
612                 .comp_mask = 0,
613         };
614         cqe_n = desc / MLX5_TX_COMP_THRESH +
615                 1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
616         tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
617         if (tmpl.cq == NULL) {
618                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
619                         dev->data->port_id, idx);
620                 rte_errno = errno;
621                 goto error;
622         }
623         attr.init = (struct ibv_qp_init_attr_ex){
624                 /* CQ to be associated with the send queue. */
625                 .send_cq = tmpl.cq,
626                 /* CQ to be associated with the receive queue. */
627                 .recv_cq = tmpl.cq,
628                 .cap = {
629                         /* Max number of outstanding WRs. */
630                         .max_send_wr =
631                                 ((priv->sh->device_attr.orig_attr.max_qp_wr <
632                                   desc) ?
633                                  priv->sh->device_attr.orig_attr.max_qp_wr :
634                                  desc),
635                         /*
636                          * Max number of scatter/gather elements in a WR,
637                          * must be 1 to prevent libmlx5 from trying to affect
638                          * too much memory. TX gather is not impacted by the
639                          * device_attr.max_sge limit and will still work
640                          * properly.
641                          */
642                         .max_send_sge = 1,
643                 },
644                 .qp_type = IBV_QPT_RAW_PACKET,
645                 /*
646                  * Do *NOT* enable this, completions events are managed per
647                  * Tx burst.
648                  */
649                 .sq_sig_all = 0,
650                 .pd = priv->sh->pd,
651                 .comp_mask = IBV_QP_INIT_ATTR_PD,
652         };
653         if (txq_data->inlen_send)
654                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
655         if (txq_data->tso_en) {
656                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
657                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
658         }
659         tmpl.qp = mlx5_glue->create_qp_ex(priv->sh->ctx, &attr.init);
660         if (tmpl.qp == NULL) {
661                 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
662                         dev->data->port_id, idx);
663                 rte_errno = errno;
664                 goto error;
665         }
666         attr.mod = (struct ibv_qp_attr){
667                 /* Move the QP to this state. */
668                 .qp_state = IBV_QPS_INIT,
669                 /* IB device port number. */
670                 .port_num = (uint8_t)priv->ibv_port,
671         };
672         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
673                                    (IBV_QP_STATE | IBV_QP_PORT));
674         if (ret) {
675                 DRV_LOG(ERR,
676                         "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
677                         dev->data->port_id, idx);
678                 rte_errno = errno;
679                 goto error;
680         }
681         attr.mod = (struct ibv_qp_attr){
682                 .qp_state = IBV_QPS_RTR
683         };
684         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
685         if (ret) {
686                 DRV_LOG(ERR,
687                         "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
688                         dev->data->port_id, idx);
689                 rte_errno = errno;
690                 goto error;
691         }
692         attr.mod.qp_state = IBV_QPS_RTS;
693         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
694         if (ret) {
695                 DRV_LOG(ERR,
696                         "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
697                         dev->data->port_id, idx);
698                 rte_errno = errno;
699                 goto error;
700         }
701         txq_obj = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_obj), 0,
702                                     txq_ctrl->socket);
703         if (!txq_obj) {
704                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
705                         dev->data->port_id, idx);
706                 rte_errno = ENOMEM;
707                 goto error;
708         }
709         obj.cq.in = tmpl.cq;
710         obj.cq.out = &cq_info;
711         obj.qp.in = tmpl.qp;
712         obj.qp.out = &qp;
713         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
714         if (ret != 0) {
715                 rte_errno = errno;
716                 goto error;
717         }
718         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
719                 DRV_LOG(ERR,
720                         "port %u wrong MLX5_CQE_SIZE environment variable"
721                         " value: it should be set to %u",
722                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
723                 rte_errno = EINVAL;
724                 goto error;
725         }
726         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
727         txq_data->cqe_s = 1 << txq_data->cqe_n;
728         txq_data->cqe_m = txq_data->cqe_s - 1;
729         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
730         txq_data->wqes = qp.sq.buf;
731         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
732         txq_data->wqe_s = 1 << txq_data->wqe_n;
733         txq_data->wqe_m = txq_data->wqe_s - 1;
734         txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
735         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
736         txq_data->cq_db = cq_info.dbrec;
737         txq_data->cqes = (volatile struct mlx5_cqe *)cq_info.buf;
738         txq_data->cq_ci = 0;
739         txq_data->cq_pi = 0;
740         txq_data->wqe_ci = 0;
741         txq_data->wqe_pi = 0;
742         txq_data->wqe_comp = 0;
743         txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
744         txq_data->fcqs = rte_calloc_socket(__func__,
745                                            txq_data->cqe_s,
746                                            sizeof(*txq_data->fcqs),
747                                            RTE_CACHE_LINE_SIZE,
748                                            txq_ctrl->socket);
749         if (!txq_data->fcqs) {
750                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory (FCQ)",
751                         dev->data->port_id, idx);
752                 rte_errno = ENOMEM;
753                 goto error;
754         }
755 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
756         /*
757          * If using DevX need to query and store TIS transport domain value.
758          * This is done once per port.
759          * Will use this value on Rx, when creating matching TIR.
760          */
761         if (priv->config.devx && !priv->sh->tdn) {
762                 ret = mlx5_devx_cmd_qp_query_tis_td(tmpl.qp, qp.tisn,
763                                                     &priv->sh->tdn);
764                 if (ret) {
765                         DRV_LOG(ERR, "Fail to query port %u Tx queue %u QP TIS "
766                                 "transport domain", dev->data->port_id, idx);
767                         rte_errno = EINVAL;
768                         goto error;
769                 } else {
770                         DRV_LOG(DEBUG, "port %u Tx queue %u TIS number %d "
771                                 "transport domain %d", dev->data->port_id,
772                                 idx, qp.tisn, priv->sh->tdn);
773                 }
774         }
775 #endif
776         txq_obj->qp = tmpl.qp;
777         txq_obj->cq = tmpl.cq;
778         rte_atomic32_inc(&txq_obj->refcnt);
779         txq_ctrl->bf_reg = qp.bf.reg;
780         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
781                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
782                 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%"PRIx64,
783                         dev->data->port_id, txq_ctrl->uar_mmap_offset);
784         } else {
785                 DRV_LOG(ERR,
786                         "port %u failed to retrieve UAR info, invalid"
787                         " libmlx5.so",
788                         dev->data->port_id);
789                 rte_errno = EINVAL;
790                 goto error;
791         }
792         txq_uar_init(txq_ctrl);
793         LIST_INSERT_HEAD(&priv->txqsobj, txq_obj, next);
794         txq_obj->txq_ctrl = txq_ctrl;
795         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
796         return txq_obj;
797 error:
798         ret = rte_errno; /* Save rte_errno before cleanup. */
799         if (tmpl.cq)
800                 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
801         if (tmpl.qp)
802                 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
803         if (txq_data && txq_data->fcqs)
804                 rte_free(txq_data->fcqs);
805         if (txq_obj)
806                 rte_free(txq_obj);
807         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
808         rte_errno = ret; /* Restore rte_errno. */
809         return NULL;
810 }
811
812 /**
813  * Get an Tx queue Verbs object.
814  *
815  * @param dev
816  *   Pointer to Ethernet device.
817  * @param idx
818  *   Queue index in DPDK Tx queue array.
819  *
820  * @return
821  *   The Verbs object if it exists.
822  */
823 struct mlx5_txq_obj *
824 mlx5_txq_obj_get(struct rte_eth_dev *dev, uint16_t idx)
825 {
826         struct mlx5_priv *priv = dev->data->dev_private;
827         struct mlx5_txq_ctrl *txq_ctrl;
828
829         if (idx >= priv->txqs_n)
830                 return NULL;
831         if (!(*priv->txqs)[idx])
832                 return NULL;
833         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
834         if (txq_ctrl->obj)
835                 rte_atomic32_inc(&txq_ctrl->obj->refcnt);
836         return txq_ctrl->obj;
837 }
838
839 /**
840  * Release an Tx verbs queue object.
841  *
842  * @param txq_obj
843  *   Verbs Tx queue object.
844  *
845  * @return
846  *   1 while a reference on it exists, 0 when freed.
847  */
848 int
849 mlx5_txq_obj_release(struct mlx5_txq_obj *txq_obj)
850 {
851         MLX5_ASSERT(txq_obj);
852         if (rte_atomic32_dec_and_test(&txq_obj->refcnt)) {
853                 if (txq_obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN) {
854                         if (txq_obj->tis)
855                                 claim_zero(mlx5_devx_cmd_destroy(txq_obj->tis));
856                 } else {
857                         claim_zero(mlx5_glue->destroy_qp(txq_obj->qp));
858                         claim_zero(mlx5_glue->destroy_cq(txq_obj->cq));
859                                 if (txq_obj->txq_ctrl->txq.fcqs)
860                                         rte_free(txq_obj->txq_ctrl->txq.fcqs);
861                 }
862                 LIST_REMOVE(txq_obj, next);
863                 rte_free(txq_obj);
864                 return 0;
865         }
866         return 1;
867 }
868
869 /**
870  * Verify the Verbs Tx queue list is empty
871  *
872  * @param dev
873  *   Pointer to Ethernet device.
874  *
875  * @return
876  *   The number of object not released.
877  */
878 int
879 mlx5_txq_obj_verify(struct rte_eth_dev *dev)
880 {
881         struct mlx5_priv *priv = dev->data->dev_private;
882         int ret = 0;
883         struct mlx5_txq_obj *txq_obj;
884
885         LIST_FOREACH(txq_obj, &priv->txqsobj, next) {
886                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
887                         dev->data->port_id, txq_obj->txq_ctrl->txq.idx);
888                 ++ret;
889         }
890         return ret;
891 }
892
893 /**
894  * Calculate the total number of WQEBB for Tx queue.
895  *
896  * Simplified version of calc_sq_size() in rdma-core.
897  *
898  * @param txq_ctrl
899  *   Pointer to Tx queue control structure.
900  *
901  * @return
902  *   The number of WQEBB.
903  */
904 static int
905 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
906 {
907         unsigned int wqe_size;
908         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
909
910         wqe_size = MLX5_WQE_CSEG_SIZE +
911                    MLX5_WQE_ESEG_SIZE +
912                    MLX5_WSEG_SIZE -
913                    MLX5_ESEG_MIN_INLINE_SIZE +
914                    txq_ctrl->max_inline_data;
915         return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
916 }
917
918 /**
919  * Calculate the maximal inline data size for Tx queue.
920  *
921  * @param txq_ctrl
922  *   Pointer to Tx queue control structure.
923  *
924  * @return
925  *   The maximal inline data size.
926  */
927 static unsigned int
928 txq_calc_inline_max(struct mlx5_txq_ctrl *txq_ctrl)
929 {
930         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
931         struct mlx5_priv *priv = txq_ctrl->priv;
932         unsigned int wqe_size;
933
934         wqe_size = priv->sh->device_attr.orig_attr.max_qp_wr / desc;
935         if (!wqe_size)
936                 return 0;
937         /*
938          * This calculation is derived from tthe source of
939          * mlx5_calc_send_wqe() in rdma_core library.
940          */
941         wqe_size = wqe_size * MLX5_WQE_SIZE -
942                    MLX5_WQE_CSEG_SIZE -
943                    MLX5_WQE_ESEG_SIZE -
944                    MLX5_WSEG_SIZE -
945                    MLX5_WSEG_SIZE +
946                    MLX5_DSEG_MIN_INLINE_SIZE;
947         return wqe_size;
948 }
949
950 /**
951  * Set Tx queue parameters from device configuration.
952  *
953  * @param txq_ctrl
954  *   Pointer to Tx queue control structure.
955  */
956 static void
957 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
958 {
959         struct mlx5_priv *priv = txq_ctrl->priv;
960         struct mlx5_dev_config *config = &priv->config;
961         unsigned int inlen_send; /* Inline data for ordinary SEND.*/
962         unsigned int inlen_empw; /* Inline data for enhanced MPW. */
963         unsigned int inlen_mode; /* Minimal required Inline data. */
964         unsigned int txqs_inline; /* Min Tx queues to enable inline. */
965         uint64_t dev_txoff = priv->dev_data->dev_conf.txmode.offloads;
966         bool tso = txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
967                                             DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
968                                             DEV_TX_OFFLOAD_GRE_TNL_TSO |
969                                             DEV_TX_OFFLOAD_IP_TNL_TSO |
970                                             DEV_TX_OFFLOAD_UDP_TNL_TSO);
971         bool vlan_inline;
972         unsigned int temp;
973
974         if (config->txqs_inline == MLX5_ARG_UNSET)
975                 txqs_inline =
976 #if defined(RTE_ARCH_ARM64)
977                 (priv->pci_dev->id.device_id ==
978                         PCI_DEVICE_ID_MELLANOX_CONNECTX5BF) ?
979                         MLX5_INLINE_MAX_TXQS_BLUEFIELD :
980 #endif
981                         MLX5_INLINE_MAX_TXQS;
982         else
983                 txqs_inline = (unsigned int)config->txqs_inline;
984         inlen_send = (config->txq_inline_max == MLX5_ARG_UNSET) ?
985                      MLX5_SEND_DEF_INLINE_LEN :
986                      (unsigned int)config->txq_inline_max;
987         inlen_empw = (config->txq_inline_mpw == MLX5_ARG_UNSET) ?
988                      MLX5_EMPW_DEF_INLINE_LEN :
989                      (unsigned int)config->txq_inline_mpw;
990         inlen_mode = (config->txq_inline_min == MLX5_ARG_UNSET) ?
991                      0 : (unsigned int)config->txq_inline_min;
992         if (config->mps != MLX5_MPW_ENHANCED && config->mps != MLX5_MPW)
993                 inlen_empw = 0;
994         /*
995          * If there is requested minimal amount of data to inline
996          * we MUST enable inlining. This is a case for ConnectX-4
997          * which usually requires L2 inlined for correct operating
998          * and ConnectX-4 Lx which requires L2-L4 inlined to
999          * support E-Switch Flows.
1000          */
1001         if (inlen_mode) {
1002                 if (inlen_mode <= MLX5_ESEG_MIN_INLINE_SIZE) {
1003                         /*
1004                          * Optimize minimal inlining for single
1005                          * segment packets to fill one WQEBB
1006                          * without gaps.
1007                          */
1008                         temp = MLX5_ESEG_MIN_INLINE_SIZE;
1009                 } else {
1010                         temp = inlen_mode - MLX5_ESEG_MIN_INLINE_SIZE;
1011                         temp = RTE_ALIGN(temp, MLX5_WSEG_SIZE) +
1012                                MLX5_ESEG_MIN_INLINE_SIZE;
1013                         temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
1014                 }
1015                 if (temp != inlen_mode) {
1016                         DRV_LOG(INFO,
1017                                 "port %u minimal required inline setting"
1018                                 " aligned from %u to %u",
1019                                 PORT_ID(priv), inlen_mode, temp);
1020                         inlen_mode = temp;
1021                 }
1022         }
1023         /*
1024          * If port is configured to support VLAN insertion and device
1025          * does not support this feature by HW (for NICs before ConnectX-5
1026          * or in case of wqe_vlan_insert flag is not set) we must enable
1027          * data inline on all queues because it is supported by single
1028          * tx_burst routine.
1029          */
1030         txq_ctrl->txq.vlan_en = config->hw_vlan_insert;
1031         vlan_inline = (dev_txoff & DEV_TX_OFFLOAD_VLAN_INSERT) &&
1032                       !config->hw_vlan_insert;
1033         /*
1034          * If there are few Tx queues it is prioritized
1035          * to save CPU cycles and disable data inlining at all.
1036          */
1037         if (inlen_send && priv->txqs_n >= txqs_inline) {
1038                 /*
1039                  * The data sent with ordinal MLX5_OPCODE_SEND
1040                  * may be inlined in Ethernet Segment, align the
1041                  * length accordingly to fit entire WQEBBs.
1042                  */
1043                 temp = RTE_MAX(inlen_send,
1044                                MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE);
1045                 temp -= MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1046                 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1047                 temp += MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1048                 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1049                                      MLX5_ESEG_MIN_INLINE_SIZE -
1050                                      MLX5_WQE_CSEG_SIZE -
1051                                      MLX5_WQE_ESEG_SIZE -
1052                                      MLX5_WQE_DSEG_SIZE * 2);
1053                 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
1054                 temp = RTE_MAX(temp, inlen_mode);
1055                 if (temp != inlen_send) {
1056                         DRV_LOG(INFO,
1057                                 "port %u ordinary send inline setting"
1058                                 " aligned from %u to %u",
1059                                 PORT_ID(priv), inlen_send, temp);
1060                         inlen_send = temp;
1061                 }
1062                 /*
1063                  * Not aligned to cache lines, but to WQEs.
1064                  * First bytes of data (initial alignment)
1065                  * is going to be copied explicitly at the
1066                  * beginning of inlining buffer in Ethernet
1067                  * Segment.
1068                  */
1069                 MLX5_ASSERT(inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
1070                 MLX5_ASSERT(inlen_send <= MLX5_WQE_SIZE_MAX +
1071                                           MLX5_ESEG_MIN_INLINE_SIZE -
1072                                           MLX5_WQE_CSEG_SIZE -
1073                                           MLX5_WQE_ESEG_SIZE -
1074                                           MLX5_WQE_DSEG_SIZE * 2);
1075         } else if (inlen_mode) {
1076                 /*
1077                  * If minimal inlining is requested we must
1078                  * enable inlining in general, despite the
1079                  * number of configured queues. Ignore the
1080                  * txq_inline_max devarg, this is not
1081                  * full-featured inline.
1082                  */
1083                 inlen_send = inlen_mode;
1084                 inlen_empw = 0;
1085         } else if (vlan_inline) {
1086                 /*
1087                  * Hardware does not report offload for
1088                  * VLAN insertion, we must enable data inline
1089                  * to implement feature by software.
1090                  */
1091                 inlen_send = MLX5_ESEG_MIN_INLINE_SIZE;
1092                 inlen_empw = 0;
1093         } else {
1094                 inlen_send = 0;
1095                 inlen_empw = 0;
1096         }
1097         txq_ctrl->txq.inlen_send = inlen_send;
1098         txq_ctrl->txq.inlen_mode = inlen_mode;
1099         txq_ctrl->txq.inlen_empw = 0;
1100         if (inlen_send && inlen_empw && priv->txqs_n >= txqs_inline) {
1101                 /*
1102                  * The data sent with MLX5_OPCODE_ENHANCED_MPSW
1103                  * may be inlined in Data Segment, align the
1104                  * length accordingly to fit entire WQEBBs.
1105                  */
1106                 temp = RTE_MAX(inlen_empw,
1107                                MLX5_WQE_SIZE + MLX5_DSEG_MIN_INLINE_SIZE);
1108                 temp -= MLX5_DSEG_MIN_INLINE_SIZE;
1109                 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1110                 temp += MLX5_DSEG_MIN_INLINE_SIZE;
1111                 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1112                                      MLX5_DSEG_MIN_INLINE_SIZE -
1113                                      MLX5_WQE_CSEG_SIZE -
1114                                      MLX5_WQE_ESEG_SIZE -
1115                                      MLX5_WQE_DSEG_SIZE);
1116                 temp = RTE_MIN(temp, MLX5_EMPW_MAX_INLINE_LEN);
1117                 if (temp != inlen_empw) {
1118                         DRV_LOG(INFO,
1119                                 "port %u enhanced empw inline setting"
1120                                 " aligned from %u to %u",
1121                                 PORT_ID(priv), inlen_empw, temp);
1122                         inlen_empw = temp;
1123                 }
1124                 MLX5_ASSERT(inlen_empw >= MLX5_ESEG_MIN_INLINE_SIZE);
1125                 MLX5_ASSERT(inlen_empw <= MLX5_WQE_SIZE_MAX +
1126                                           MLX5_DSEG_MIN_INLINE_SIZE -
1127                                           MLX5_WQE_CSEG_SIZE -
1128                                           MLX5_WQE_ESEG_SIZE -
1129                                           MLX5_WQE_DSEG_SIZE);
1130                 txq_ctrl->txq.inlen_empw = inlen_empw;
1131         }
1132         txq_ctrl->max_inline_data = RTE_MAX(inlen_send, inlen_empw);
1133         if (tso) {
1134                 txq_ctrl->max_tso_header = MLX5_MAX_TSO_HEADER;
1135                 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->max_inline_data,
1136                                                     MLX5_MAX_TSO_HEADER);
1137                 txq_ctrl->txq.tso_en = 1;
1138         }
1139         txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
1140         txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
1141                                  DEV_TX_OFFLOAD_UDP_TNL_TSO |
1142                                  DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
1143                                 txq_ctrl->txq.offloads) && config->swp;
1144 }
1145
1146 /**
1147  * Adjust Tx queue data inline parameters for large queue sizes.
1148  * The data inline feature requires multiple WQEs to fit the packets,
1149  * and if the large amount of Tx descriptors is requested by application
1150  * the total WQE amount may exceed the hardware capabilities. If the
1151  * default inline setting are used we can try to adjust these ones and
1152  * meet the hardware requirements and not exceed the queue size.
1153  *
1154  * @param txq_ctrl
1155  *   Pointer to Tx queue control structure.
1156  *
1157  * @return
1158  *   Zero on success, otherwise the parameters can not be adjusted.
1159  */
1160 static int
1161 txq_adjust_params(struct mlx5_txq_ctrl *txq_ctrl)
1162 {
1163         struct mlx5_priv *priv = txq_ctrl->priv;
1164         struct mlx5_dev_config *config = &priv->config;
1165         unsigned int max_inline;
1166
1167         max_inline = txq_calc_inline_max(txq_ctrl);
1168         if (!txq_ctrl->txq.inlen_send) {
1169                 /*
1170                  * Inline data feature is not engaged at all.
1171                  * There is nothing to adjust.
1172                  */
1173                 return 0;
1174         }
1175         if (txq_ctrl->max_inline_data <= max_inline) {
1176                 /*
1177                  * The requested inline data length does not
1178                  * exceed queue capabilities.
1179                  */
1180                 return 0;
1181         }
1182         if (txq_ctrl->txq.inlen_mode > max_inline) {
1183                 DRV_LOG(ERR,
1184                         "minimal data inline requirements (%u) are not"
1185                         " satisfied (%u) on port %u, try the smaller"
1186                         " Tx queue size (%d)",
1187                         txq_ctrl->txq.inlen_mode, max_inline,
1188                         priv->dev_data->port_id,
1189                         priv->sh->device_attr.orig_attr.max_qp_wr);
1190                 goto error;
1191         }
1192         if (txq_ctrl->txq.inlen_send > max_inline &&
1193             config->txq_inline_max != MLX5_ARG_UNSET &&
1194             config->txq_inline_max > (int)max_inline) {
1195                 DRV_LOG(ERR,
1196                         "txq_inline_max requirements (%u) are not"
1197                         " satisfied (%u) on port %u, try the smaller"
1198                         " Tx queue size (%d)",
1199                         txq_ctrl->txq.inlen_send, max_inline,
1200                         priv->dev_data->port_id,
1201                         priv->sh->device_attr.orig_attr.max_qp_wr);
1202                 goto error;
1203         }
1204         if (txq_ctrl->txq.inlen_empw > max_inline &&
1205             config->txq_inline_mpw != MLX5_ARG_UNSET &&
1206             config->txq_inline_mpw > (int)max_inline) {
1207                 DRV_LOG(ERR,
1208                         "txq_inline_mpw requirements (%u) are not"
1209                         " satisfied (%u) on port %u, try the smaller"
1210                         " Tx queue size (%d)",
1211                         txq_ctrl->txq.inlen_empw, max_inline,
1212                         priv->dev_data->port_id,
1213                         priv->sh->device_attr.orig_attr.max_qp_wr);
1214                 goto error;
1215         }
1216         if (txq_ctrl->txq.tso_en && max_inline < MLX5_MAX_TSO_HEADER) {
1217                 DRV_LOG(ERR,
1218                         "tso header inline requirements (%u) are not"
1219                         " satisfied (%u) on port %u, try the smaller"
1220                         " Tx queue size (%d)",
1221                         MLX5_MAX_TSO_HEADER, max_inline,
1222                         priv->dev_data->port_id,
1223                         priv->sh->device_attr.orig_attr.max_qp_wr);
1224                 goto error;
1225         }
1226         if (txq_ctrl->txq.inlen_send > max_inline) {
1227                 DRV_LOG(WARNING,
1228                         "adjust txq_inline_max (%u->%u)"
1229                         " due to large Tx queue on port %u",
1230                         txq_ctrl->txq.inlen_send, max_inline,
1231                         priv->dev_data->port_id);
1232                 txq_ctrl->txq.inlen_send = max_inline;
1233         }
1234         if (txq_ctrl->txq.inlen_empw > max_inline) {
1235                 DRV_LOG(WARNING,
1236                         "adjust txq_inline_mpw (%u->%u)"
1237                         "due to large Tx queue on port %u",
1238                         txq_ctrl->txq.inlen_empw, max_inline,
1239                         priv->dev_data->port_id);
1240                 txq_ctrl->txq.inlen_empw = max_inline;
1241         }
1242         txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->txq.inlen_send,
1243                                             txq_ctrl->txq.inlen_empw);
1244         MLX5_ASSERT(txq_ctrl->max_inline_data <= max_inline);
1245         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= max_inline);
1246         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_send);
1247         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_empw ||
1248                     !txq_ctrl->txq.inlen_empw);
1249         return 0;
1250 error:
1251         rte_errno = ENOMEM;
1252         return -ENOMEM;
1253 }
1254
1255 /**
1256  * Create a DPDK Tx queue.
1257  *
1258  * @param dev
1259  *   Pointer to Ethernet device.
1260  * @param idx
1261  *   TX queue index.
1262  * @param desc
1263  *   Number of descriptors to configure in queue.
1264  * @param socket
1265  *   NUMA socket on which memory must be allocated.
1266  * @param[in] conf
1267  *  Thresholds parameters.
1268  *
1269  * @return
1270  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1271  */
1272 struct mlx5_txq_ctrl *
1273 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1274              unsigned int socket, const struct rte_eth_txconf *conf)
1275 {
1276         struct mlx5_priv *priv = dev->data->dev_private;
1277         struct mlx5_txq_ctrl *tmpl;
1278
1279         tmpl = rte_calloc_socket("TXQ", 1,
1280                                  sizeof(*tmpl) +
1281                                  desc * sizeof(struct rte_mbuf *),
1282                                  0, socket);
1283         if (!tmpl) {
1284                 rte_errno = ENOMEM;
1285                 return NULL;
1286         }
1287         if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
1288                                MLX5_MR_BTREE_CACHE_N, socket)) {
1289                 /* rte_errno is already set. */
1290                 goto error;
1291         }
1292         /* Save pointer of global generation number to check memory event. */
1293         tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->share_cache.dev_gen;
1294         MLX5_ASSERT(desc > MLX5_TX_COMP_THRESH);
1295         tmpl->txq.offloads = conf->offloads |
1296                              dev->data->dev_conf.txmode.offloads;
1297         tmpl->priv = priv;
1298         tmpl->socket = socket;
1299         tmpl->txq.elts_n = log2above(desc);
1300         tmpl->txq.elts_s = desc;
1301         tmpl->txq.elts_m = desc - 1;
1302         tmpl->txq.port_id = dev->data->port_id;
1303         tmpl->txq.idx = idx;
1304         txq_set_params(tmpl);
1305         if (txq_adjust_params(tmpl))
1306                 goto error;
1307         if (txq_calc_wqebb_cnt(tmpl) >
1308             priv->sh->device_attr.orig_attr.max_qp_wr) {
1309                 DRV_LOG(ERR,
1310                         "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
1311                         " try smaller queue size",
1312                         dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
1313                         priv->sh->device_attr.orig_attr.max_qp_wr);
1314                 rte_errno = ENOMEM;
1315                 goto error;
1316         }
1317         rte_atomic32_inc(&tmpl->refcnt);
1318         tmpl->type = MLX5_TXQ_TYPE_STANDARD;
1319         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1320         return tmpl;
1321 error:
1322         rte_free(tmpl);
1323         return NULL;
1324 }
1325
1326 /**
1327  * Create a DPDK Tx hairpin queue.
1328  *
1329  * @param dev
1330  *   Pointer to Ethernet device.
1331  * @param idx
1332  *   TX queue index.
1333  * @param desc
1334  *   Number of descriptors to configure in queue.
1335  * @param hairpin_conf
1336  *  The hairpin configuration.
1337  *
1338  * @return
1339  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1340  */
1341 struct mlx5_txq_ctrl *
1342 mlx5_txq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1343                      const struct rte_eth_hairpin_conf *hairpin_conf)
1344 {
1345         struct mlx5_priv *priv = dev->data->dev_private;
1346         struct mlx5_txq_ctrl *tmpl;
1347
1348         tmpl = rte_calloc_socket("TXQ", 1,
1349                                  sizeof(*tmpl), 0, SOCKET_ID_ANY);
1350         if (!tmpl) {
1351                 rte_errno = ENOMEM;
1352                 return NULL;
1353         }
1354         tmpl->priv = priv;
1355         tmpl->socket = SOCKET_ID_ANY;
1356         tmpl->txq.elts_n = log2above(desc);
1357         tmpl->txq.port_id = dev->data->port_id;
1358         tmpl->txq.idx = idx;
1359         tmpl->hairpin_conf = *hairpin_conf;
1360         tmpl->type = MLX5_TXQ_TYPE_HAIRPIN;
1361         rte_atomic32_inc(&tmpl->refcnt);
1362         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
1363         return tmpl;
1364 }
1365
1366 /**
1367  * Get a Tx queue.
1368  *
1369  * @param dev
1370  *   Pointer to Ethernet device.
1371  * @param idx
1372  *   TX queue index.
1373  *
1374  * @return
1375  *   A pointer to the queue if it exists.
1376  */
1377 struct mlx5_txq_ctrl *
1378 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
1379 {
1380         struct mlx5_priv *priv = dev->data->dev_private;
1381         struct mlx5_txq_ctrl *ctrl = NULL;
1382
1383         if ((*priv->txqs)[idx]) {
1384                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
1385                                     txq);
1386                 mlx5_txq_obj_get(dev, idx);
1387                 rte_atomic32_inc(&ctrl->refcnt);
1388         }
1389         return ctrl;
1390 }
1391
1392 /**
1393  * Release a Tx queue.
1394  *
1395  * @param dev
1396  *   Pointer to Ethernet device.
1397  * @param idx
1398  *   TX queue index.
1399  *
1400  * @return
1401  *   1 while a reference on it exists, 0 when freed.
1402  */
1403 int
1404 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
1405 {
1406         struct mlx5_priv *priv = dev->data->dev_private;
1407         struct mlx5_txq_ctrl *txq;
1408
1409         if (!(*priv->txqs)[idx])
1410                 return 0;
1411         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1412         if (txq->obj && !mlx5_txq_obj_release(txq->obj))
1413                 txq->obj = NULL;
1414         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
1415                 txq_free_elts(txq);
1416                 mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
1417                 LIST_REMOVE(txq, next);
1418                 rte_free(txq);
1419                 (*priv->txqs)[idx] = NULL;
1420                 return 0;
1421         }
1422         return 1;
1423 }
1424
1425 /**
1426  * Verify if the queue can be released.
1427  *
1428  * @param dev
1429  *   Pointer to Ethernet device.
1430  * @param idx
1431  *   TX queue index.
1432  *
1433  * @return
1434  *   1 if the queue can be released.
1435  */
1436 int
1437 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
1438 {
1439         struct mlx5_priv *priv = dev->data->dev_private;
1440         struct mlx5_txq_ctrl *txq;
1441
1442         if (!(*priv->txqs)[idx])
1443                 return -1;
1444         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
1445         return (rte_atomic32_read(&txq->refcnt) == 1);
1446 }
1447
1448 /**
1449  * Verify the Tx Queue list is empty
1450  *
1451  * @param dev
1452  *   Pointer to Ethernet device.
1453  *
1454  * @return
1455  *   The number of object not released.
1456  */
1457 int
1458 mlx5_txq_verify(struct rte_eth_dev *dev)
1459 {
1460         struct mlx5_priv *priv = dev->data->dev_private;
1461         struct mlx5_txq_ctrl *txq_ctrl;
1462         int ret = 0;
1463
1464         LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
1465                 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
1466                         dev->data->port_id, txq_ctrl->txq.idx);
1467                 ++ret;
1468         }
1469         return ret;
1470 }