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