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