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