net/mlx5: remove unused functions
[dpdk.git] / drivers / net / mlx5 / mlx5_txq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13
14 /* Verbs header. */
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic ignored "-Wpedantic"
18 #endif
19 #include <infiniband/verbs.h>
20 #ifdef PEDANTIC
21 #pragma GCC diagnostic error "-Wpedantic"
22 #endif
23
24 #include <rte_mbuf.h>
25 #include <rte_malloc.h>
26 #include <rte_ethdev_driver.h>
27 #include <rte_common.h>
28
29 #include "mlx5_utils.h"
30 #include "mlx5_defs.h"
31 #include "mlx5.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_autoconf.h"
34 #include "mlx5_glue.h"
35
36 /**
37  * Allocate TX queue elements.
38  *
39  * @param txq_ctrl
40  *   Pointer to TX queue structure.
41  */
42 void
43 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
44 {
45         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
46         unsigned int i;
47
48         for (i = 0; (i != elts_n); ++i)
49                 (*txq_ctrl->txq.elts)[i] = NULL;
50         DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
51                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
52         txq_ctrl->txq.elts_head = 0;
53         txq_ctrl->txq.elts_tail = 0;
54         txq_ctrl->txq.elts_comp = 0;
55 }
56
57 /**
58  * Free TX queue elements.
59  *
60  * @param txq_ctrl
61  *   Pointer to TX queue structure.
62  */
63 static void
64 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
65 {
66         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
67         const uint16_t elts_m = elts_n - 1;
68         uint16_t elts_head = txq_ctrl->txq.elts_head;
69         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
70         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
71
72         DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
73                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
74         txq_ctrl->txq.elts_head = 0;
75         txq_ctrl->txq.elts_tail = 0;
76         txq_ctrl->txq.elts_comp = 0;
77
78         while (elts_tail != elts_head) {
79                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
80
81                 assert(elt != NULL);
82                 rte_pktmbuf_free_seg(elt);
83 #ifndef NDEBUG
84                 /* Poisoning. */
85                 memset(&(*elts)[elts_tail & elts_m],
86                        0x77,
87                        sizeof((*elts)[elts_tail & elts_m]));
88 #endif
89                 ++elts_tail;
90         }
91 }
92
93 /**
94  * Returns the per-port supported offloads.
95  *
96  * @param dev
97  *   Pointer to Ethernet device.
98  *
99  * @return
100  *   Supported Tx offloads.
101  */
102 uint64_t
103 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
104 {
105         struct mlx5_priv *priv = dev->data->dev_private;
106         uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
107                              DEV_TX_OFFLOAD_VLAN_INSERT);
108         struct mlx5_dev_config *config = &priv->config;
109
110         if (config->hw_csum)
111                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
112                              DEV_TX_OFFLOAD_UDP_CKSUM |
113                              DEV_TX_OFFLOAD_TCP_CKSUM);
114         if (config->tso)
115                 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
116         if (config->swp) {
117                 if (config->hw_csum)
118                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
119                 if (config->tso)
120                         offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
121                                      DEV_TX_OFFLOAD_UDP_TNL_TSO);
122         }
123         if (config->tunnel_en) {
124                 if (config->hw_csum)
125                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
126                 if (config->tso)
127                         offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
128                                      DEV_TX_OFFLOAD_GRE_TNL_TSO);
129         }
130 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
131         if (config->dv_flow_en)
132                 offloads |= DEV_TX_OFFLOAD_MATCH_METADATA;
133 #endif
134         return offloads;
135 }
136
137 /**
138  * DPDK callback to configure a TX queue.
139  *
140  * @param dev
141  *   Pointer to Ethernet device structure.
142  * @param idx
143  *   TX queue index.
144  * @param desc
145  *   Number of descriptors to configure in queue.
146  * @param socket
147  *   NUMA socket on which memory must be allocated.
148  * @param[in] conf
149  *   Thresholds parameters.
150  *
151  * @return
152  *   0 on success, a negative errno value otherwise and rte_errno is set.
153  */
154 int
155 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
156                     unsigned int socket, const struct rte_eth_txconf *conf)
157 {
158         struct mlx5_priv *priv = dev->data->dev_private;
159         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
160         struct mlx5_txq_ctrl *txq_ctrl =
161                 container_of(txq, struct mlx5_txq_ctrl, txq);
162
163         if (desc <= MLX5_TX_COMP_THRESH) {
164                 DRV_LOG(WARNING,
165                         "port %u number of descriptors requested for Tx queue"
166                         " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
167                         " instead of %u",
168                         dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
169                 desc = MLX5_TX_COMP_THRESH + 1;
170         }
171         if (!rte_is_power_of_2(desc)) {
172                 desc = 1 << log2above(desc);
173                 DRV_LOG(WARNING,
174                         "port %u increased number of descriptors in Tx queue"
175                         " %u to the next power of two (%d)",
176                         dev->data->port_id, idx, desc);
177         }
178         DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
179                 dev->data->port_id, idx, desc);
180         if (idx >= priv->txqs_n) {
181                 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
182                         dev->data->port_id, idx, priv->txqs_n);
183                 rte_errno = EOVERFLOW;
184                 return -rte_errno;
185         }
186         if (!mlx5_txq_releasable(dev, idx)) {
187                 rte_errno = EBUSY;
188                 DRV_LOG(ERR, "port %u unable to release queue index %u",
189                         dev->data->port_id, idx);
190                 return -rte_errno;
191         }
192         mlx5_txq_release(dev, idx);
193         txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
194         if (!txq_ctrl) {
195                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
196                         dev->data->port_id, idx);
197                 return -rte_errno;
198         }
199         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
200                 dev->data->port_id, idx);
201         (*priv->txqs)[idx] = &txq_ctrl->txq;
202         return 0;
203 }
204
205 /**
206  * DPDK callback to release a TX queue.
207  *
208  * @param dpdk_txq
209  *   Generic TX queue pointer.
210  */
211 void
212 mlx5_tx_queue_release(void *dpdk_txq)
213 {
214         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
215         struct mlx5_txq_ctrl *txq_ctrl;
216         struct mlx5_priv *priv;
217         unsigned int i;
218
219         if (txq == NULL)
220                 return;
221         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
222         priv = txq_ctrl->priv;
223         for (i = 0; (i != priv->txqs_n); ++i)
224                 if ((*priv->txqs)[i] == txq) {
225                         mlx5_txq_release(ETH_DEV(priv), i);
226                         DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
227                                 PORT_ID(priv), txq->idx);
228                         break;
229                 }
230 }
231
232 /**
233  * Initialize Tx UAR registers for primary process.
234  *
235  * @param txq_ctrl
236  *   Pointer to Tx queue control structure.
237  */
238 static void
239 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
240 {
241         struct mlx5_priv *priv = txq_ctrl->priv;
242         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
243 #ifndef RTE_ARCH_64
244         unsigned int lock_idx;
245         const size_t page_size = sysconf(_SC_PAGESIZE);
246 #endif
247
248         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
249         assert(ppriv);
250         ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
251 #ifndef RTE_ARCH_64
252         /* Assign an UAR lock according to UAR page number */
253         lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
254                    MLX5_UAR_PAGE_NUM_MASK;
255         txq_ctrl->txq.uar_lock = &priv->uar_lock[lock_idx];
256 #endif
257 }
258
259 /**
260  * Remap UAR register of a Tx queue for secondary process.
261  *
262  * Remapped address is stored at the table in the process private structure of
263  * the device, indexed by queue index.
264  *
265  * @param txq_ctrl
266  *   Pointer to Tx queue control structure.
267  * @param fd
268  *   Verbs file descriptor to map UAR pages.
269  *
270  * @return
271  *   0 on success, a negative errno value otherwise and rte_errno is set.
272  */
273 static int
274 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
275 {
276         struct mlx5_priv *priv = txq_ctrl->priv;
277         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
278         struct mlx5_txq_data *txq = &txq_ctrl->txq;
279         void *addr;
280         uintptr_t uar_va;
281         uintptr_t offset;
282         const size_t page_size = sysconf(_SC_PAGESIZE);
283
284         assert(ppriv);
285         /*
286          * As rdma-core, UARs are mapped in size of OS page
287          * size. Ref to libmlx5 function: mlx5_init_context()
288          */
289         uar_va = (uintptr_t)txq_ctrl->bf_reg;
290         offset = uar_va & (page_size - 1); /* Offset in page. */
291         addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
292                         txq_ctrl->uar_mmap_offset);
293         if (addr == MAP_FAILED) {
294                 DRV_LOG(ERR,
295                         "port %u mmap failed for BF reg of txq %u",
296                         txq->port_id, txq->idx);
297                 rte_errno = ENXIO;
298                 return -rte_errno;
299         }
300         addr = RTE_PTR_ADD(addr, offset);
301         ppriv->uar_table[txq->idx] = addr;
302         return 0;
303 }
304
305 /**
306  * Unmap UAR register of a Tx queue for secondary process.
307  *
308  * @param txq_ctrl
309  *   Pointer to Tx queue control structure.
310  */
311 static void
312 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
313 {
314         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
315         const size_t page_size = sysconf(_SC_PAGESIZE);
316         void *addr;
317
318         addr = ppriv->uar_table[txq_ctrl->txq.idx];
319         munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
320 }
321
322 /**
323  * Initialize Tx UAR registers for secondary process.
324  *
325  * @param dev
326  *   Pointer to Ethernet device.
327  * @param fd
328  *   Verbs file descriptor to map UAR pages.
329  *
330  * @return
331  *   0 on success, a negative errno value otherwise and rte_errno is set.
332  */
333 int
334 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
335 {
336         struct mlx5_priv *priv = dev->data->dev_private;
337         struct mlx5_txq_data *txq;
338         struct mlx5_txq_ctrl *txq_ctrl;
339         unsigned int i;
340         int ret;
341
342         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
343         for (i = 0; i != priv->txqs_n; ++i) {
344                 if (!(*priv->txqs)[i])
345                         continue;
346                 txq = (*priv->txqs)[i];
347                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
348                 assert(txq->idx == (uint16_t)i);
349                 ret = txq_uar_init_secondary(txq_ctrl, fd);
350                 if (ret)
351                         goto error;
352         }
353         return 0;
354 error:
355         /* Rollback. */
356         do {
357                 if (!(*priv->txqs)[i])
358                         continue;
359                 txq = (*priv->txqs)[i];
360                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
361                 txq_uar_uninit_secondary(txq_ctrl);
362         } while (i--);
363         return -rte_errno;
364 }
365
366 /**
367  * Check if the burst function is using eMPW.
368  *
369  * @param tx_pkt_burst
370  *   Tx burst function pointer.
371  *
372  * @return
373  *   1 if the burst function is using eMPW, 0 otherwise.
374  */
375 static int
376 is_empw_burst_func(eth_tx_burst_t tx_pkt_burst)
377 {
378         if (tx_pkt_burst == mlx5_tx_burst_raw_vec ||
379             tx_pkt_burst == mlx5_tx_burst_vec ||
380             tx_pkt_burst == mlx5_tx_burst_empw)
381                 return 1;
382         return 0;
383 }
384
385 /**
386  * Create the Tx queue Verbs object.
387  *
388  * @param dev
389  *   Pointer to Ethernet device.
390  * @param idx
391  *   Queue index in DPDK Tx queue array.
392  *
393  * @return
394  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
395  */
396 struct mlx5_txq_ibv *
397 mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
398 {
399         struct mlx5_priv *priv = dev->data->dev_private;
400         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
401         struct mlx5_txq_ctrl *txq_ctrl =
402                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
403         struct mlx5_txq_ibv tmpl;
404         struct mlx5_txq_ibv *txq_ibv;
405         union {
406                 struct ibv_qp_init_attr_ex init;
407                 struct ibv_cq_init_attr_ex cq;
408                 struct ibv_qp_attr mod;
409                 struct ibv_cq_ex cq_attr;
410         } attr;
411         unsigned int cqe_n;
412         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
413         struct mlx5dv_cq cq_info;
414         struct mlx5dv_obj obj;
415         const int desc = 1 << txq_data->elts_n;
416         eth_tx_burst_t tx_pkt_burst = mlx5_select_tx_function(dev);
417         int ret = 0;
418
419         assert(txq_data);
420         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
421         priv->verbs_alloc_ctx.obj = txq_ctrl;
422         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
423                 DRV_LOG(ERR,
424                         "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
425                         dev->data->port_id);
426                 rte_errno = EINVAL;
427                 return NULL;
428         }
429         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
430         attr.cq = (struct ibv_cq_init_attr_ex){
431                 .comp_mask = 0,
432         };
433         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
434                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
435         if (is_empw_burst_func(tx_pkt_burst))
436                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
437         tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
438         if (tmpl.cq == NULL) {
439                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
440                         dev->data->port_id, idx);
441                 rte_errno = errno;
442                 goto error;
443         }
444         attr.init = (struct ibv_qp_init_attr_ex){
445                 /* CQ to be associated with the send queue. */
446                 .send_cq = tmpl.cq,
447                 /* CQ to be associated with the receive queue. */
448                 .recv_cq = tmpl.cq,
449                 .cap = {
450                         /* Max number of outstanding WRs. */
451                         .max_send_wr =
452                                 ((priv->sh->device_attr.orig_attr.max_qp_wr <
453                                   desc) ?
454                                  priv->sh->device_attr.orig_attr.max_qp_wr :
455                                  desc),
456                         /*
457                          * Max number of scatter/gather elements in a WR,
458                          * must be 1 to prevent libmlx5 from trying to affect
459                          * too much memory. TX gather is not impacted by the
460                          * device_attr.max_sge limit and will still work
461                          * properly.
462                          */
463                         .max_send_sge = 1,
464                 },
465                 .qp_type = IBV_QPT_RAW_PACKET,
466                 /*
467                  * Do *NOT* enable this, completions events are managed per
468                  * Tx burst.
469                  */
470                 .sq_sig_all = 0,
471                 .pd = priv->sh->pd,
472                 .comp_mask = IBV_QP_INIT_ATTR_PD,
473         };
474         if (txq_data->max_inline)
475                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
476         if (txq_data->tso_en) {
477                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
478                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
479         }
480         tmpl.qp = mlx5_glue->create_qp_ex(priv->sh->ctx, &attr.init);
481         if (tmpl.qp == NULL) {
482                 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
483                         dev->data->port_id, idx);
484                 rte_errno = errno;
485                 goto error;
486         }
487         attr.mod = (struct ibv_qp_attr){
488                 /* Move the QP to this state. */
489                 .qp_state = IBV_QPS_INIT,
490                 /* IB device port number. */
491                 .port_num = (uint8_t)priv->ibv_port,
492         };
493         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod,
494                                    (IBV_QP_STATE | IBV_QP_PORT));
495         if (ret) {
496                 DRV_LOG(ERR,
497                         "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
498                         dev->data->port_id, idx);
499                 rte_errno = errno;
500                 goto error;
501         }
502         attr.mod = (struct ibv_qp_attr){
503                 .qp_state = IBV_QPS_RTR
504         };
505         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
506         if (ret) {
507                 DRV_LOG(ERR,
508                         "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
509                         dev->data->port_id, idx);
510                 rte_errno = errno;
511                 goto error;
512         }
513         attr.mod.qp_state = IBV_QPS_RTS;
514         ret = mlx5_glue->modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
515         if (ret) {
516                 DRV_LOG(ERR,
517                         "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
518                         dev->data->port_id, idx);
519                 rte_errno = errno;
520                 goto error;
521         }
522         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
523                                     txq_ctrl->socket);
524         if (!txq_ibv) {
525                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
526                         dev->data->port_id, idx);
527                 rte_errno = ENOMEM;
528                 goto error;
529         }
530         obj.cq.in = tmpl.cq;
531         obj.cq.out = &cq_info;
532         obj.qp.in = tmpl.qp;
533         obj.qp.out = &qp;
534         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
535         if (ret != 0) {
536                 rte_errno = errno;
537                 goto error;
538         }
539         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
540                 DRV_LOG(ERR,
541                         "port %u wrong MLX5_CQE_SIZE environment variable"
542                         " value: it should be set to %u",
543                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
544                 rte_errno = EINVAL;
545                 goto error;
546         }
547         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
548         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
549         txq_data->wqes = qp.sq.buf;
550         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
551         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
552         txq_data->cq_db = cq_info.dbrec;
553         txq_data->cqes =
554                 (volatile struct mlx5_cqe (*)[])
555                 (uintptr_t)cq_info.buf;
556         txq_data->cq_ci = 0;
557 #ifndef NDEBUG
558         txq_data->cq_pi = 0;
559 #endif
560         txq_data->wqe_ci = 0;
561         txq_data->wqe_pi = 0;
562         txq_ibv->qp = tmpl.qp;
563         txq_ibv->cq = tmpl.cq;
564         rte_atomic32_inc(&txq_ibv->refcnt);
565         txq_ctrl->bf_reg = qp.bf.reg;
566         txq_uar_init(txq_ctrl);
567         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
568                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
569                 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%lx",
570                         dev->data->port_id, txq_ctrl->uar_mmap_offset);
571         } else {
572                 DRV_LOG(ERR,
573                         "port %u failed to retrieve UAR info, invalid"
574                         " libmlx5.so",
575                         dev->data->port_id);
576                 rte_errno = EINVAL;
577                 goto error;
578         }
579         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
580         txq_ibv->txq_ctrl = txq_ctrl;
581         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
582         return txq_ibv;
583 error:
584         ret = rte_errno; /* Save rte_errno before cleanup. */
585         if (tmpl.cq)
586                 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
587         if (tmpl.qp)
588                 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
589         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
590         rte_errno = ret; /* Restore rte_errno. */
591         return NULL;
592 }
593
594 /**
595  * Get an Tx queue Verbs object.
596  *
597  * @param dev
598  *   Pointer to Ethernet device.
599  * @param idx
600  *   Queue index in DPDK Tx queue array.
601  *
602  * @return
603  *   The Verbs object if it exists.
604  */
605 struct mlx5_txq_ibv *
606 mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
607 {
608         struct mlx5_priv *priv = dev->data->dev_private;
609         struct mlx5_txq_ctrl *txq_ctrl;
610
611         if (idx >= priv->txqs_n)
612                 return NULL;
613         if (!(*priv->txqs)[idx])
614                 return NULL;
615         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
616         if (txq_ctrl->ibv)
617                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
618         return txq_ctrl->ibv;
619 }
620
621 /**
622  * Release an Tx verbs queue object.
623  *
624  * @param txq_ibv
625  *   Verbs Tx queue object.
626  *
627  * @return
628  *   1 while a reference on it exists, 0 when freed.
629  */
630 int
631 mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv)
632 {
633         assert(txq_ibv);
634         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
635                 claim_zero(mlx5_glue->destroy_qp(txq_ibv->qp));
636                 claim_zero(mlx5_glue->destroy_cq(txq_ibv->cq));
637                 LIST_REMOVE(txq_ibv, next);
638                 rte_free(txq_ibv);
639                 return 0;
640         }
641         return 1;
642 }
643
644 /**
645  * Verify the Verbs Tx queue list is empty
646  *
647  * @param dev
648  *   Pointer to Ethernet device.
649  *
650  * @return
651  *   The number of object not released.
652  */
653 int
654 mlx5_txq_ibv_verify(struct rte_eth_dev *dev)
655 {
656         struct mlx5_priv *priv = dev->data->dev_private;
657         int ret = 0;
658         struct mlx5_txq_ibv *txq_ibv;
659
660         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
661                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
662                         dev->data->port_id, txq_ibv->txq_ctrl->txq.idx);
663                 ++ret;
664         }
665         return ret;
666 }
667
668 /**
669  * Calcuate the total number of WQEBB for Tx queue.
670  *
671  * Simplified version of calc_sq_size() in rdma-core.
672  *
673  * @param txq_ctrl
674  *   Pointer to Tx queue control structure.
675  *
676  * @return
677  *   The number of WQEBB.
678  */
679 static int
680 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
681 {
682         unsigned int wqe_size;
683         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
684
685         wqe_size = MLX5_WQE_SIZE + txq_ctrl->max_inline_data;
686         return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
687 }
688
689 /**
690  * Set Tx queue parameters from device configuration.
691  *
692  * @param txq_ctrl
693  *   Pointer to Tx queue control structure.
694  */
695 static void
696 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
697 {
698         struct mlx5_priv *priv = txq_ctrl->priv;
699         struct mlx5_dev_config *config = &priv->config;
700         const unsigned int max_tso_inline =
701                 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
702                  RTE_CACHE_LINE_SIZE);
703         unsigned int txq_inline;
704         unsigned int txqs_inline;
705         unsigned int inline_max_packet_sz;
706         eth_tx_burst_t tx_pkt_burst =
707                 mlx5_select_tx_function(ETH_DEV(priv));
708         int is_empw_func = is_empw_burst_func(tx_pkt_burst);
709         int tso = !!(txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
710                                                DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
711                                                DEV_TX_OFFLOAD_GRE_TNL_TSO |
712                                                DEV_TX_OFFLOAD_IP_TNL_TSO |
713                                                DEV_TX_OFFLOAD_UDP_TNL_TSO));
714
715         txq_inline = (config->txq_inline == MLX5_ARG_UNSET) ?
716                 0 : config->txq_inline;
717         txqs_inline = (config->txqs_inline == MLX5_ARG_UNSET) ?
718                 0 : config->txqs_inline;
719         inline_max_packet_sz =
720                 (config->inline_max_packet_sz == MLX5_ARG_UNSET) ?
721                 0 : config->inline_max_packet_sz;
722         if (is_empw_func) {
723                 if (config->txq_inline == MLX5_ARG_UNSET)
724                         txq_inline = MLX5_WQE_SIZE_MAX - MLX5_WQE_SIZE;
725                 if (config->txqs_inline == MLX5_ARG_UNSET)
726                         txqs_inline = MLX5_EMPW_MIN_TXQS;
727                 if (config->inline_max_packet_sz == MLX5_ARG_UNSET)
728                         inline_max_packet_sz = MLX5_EMPW_MAX_INLINE_LEN;
729                 txq_ctrl->txq.mpw_hdr_dseg = config->mpw_hdr_dseg;
730                 txq_ctrl->txq.inline_max_packet_sz = inline_max_packet_sz;
731         }
732         if (txq_inline && priv->txqs_n >= txqs_inline) {
733                 unsigned int ds_cnt;
734
735                 txq_ctrl->txq.max_inline =
736                         ((txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
737                          RTE_CACHE_LINE_SIZE);
738                 if (is_empw_func) {
739                         /* To minimize the size of data set, avoid requesting
740                          * too large WQ.
741                          */
742                         txq_ctrl->max_inline_data =
743                                 ((RTE_MIN(txq_inline,
744                                           inline_max_packet_sz) +
745                                   (RTE_CACHE_LINE_SIZE - 1)) /
746                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
747                 } else {
748                         txq_ctrl->max_inline_data =
749                                 txq_ctrl->txq.max_inline * RTE_CACHE_LINE_SIZE;
750                 }
751                 /*
752                  * Check if the inline size is too large in a way which
753                  * can make the WQE DS to overflow.
754                  * Considering in calculation:
755                  *      WQE CTRL (1 DS)
756                  *      WQE ETH  (1 DS)
757                  *      Inline part (N DS)
758                  */
759                 ds_cnt = 2 + (txq_ctrl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
760                 if (ds_cnt > MLX5_DSEG_MAX) {
761                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
762                                                   MLX5_WQE_DWORD_SIZE;
763
764                         max_inline = max_inline - (max_inline %
765                                                    RTE_CACHE_LINE_SIZE);
766                         DRV_LOG(WARNING,
767                                 "port %u txq inline is too large (%d) setting"
768                                 " it to the maximum possible: %d\n",
769                                 PORT_ID(priv), txq_inline, max_inline);
770                         txq_ctrl->txq.max_inline = max_inline /
771                                                    RTE_CACHE_LINE_SIZE;
772                 }
773         }
774         if (tso) {
775                 txq_ctrl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
776                 txq_ctrl->txq.max_inline = RTE_MAX(txq_ctrl->txq.max_inline,
777                                                    max_tso_inline);
778                 txq_ctrl->txq.tso_en = 1;
779         }
780         txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
781         txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
782                                  DEV_TX_OFFLOAD_UDP_TNL_TSO |
783                                  DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
784                                 txq_ctrl->txq.offloads) && config->swp;
785 }
786
787 /**
788  * Create a DPDK Tx queue.
789  *
790  * @param dev
791  *   Pointer to Ethernet device.
792  * @param idx
793  *   TX queue index.
794  * @param desc
795  *   Number of descriptors to configure in queue.
796  * @param socket
797  *   NUMA socket on which memory must be allocated.
798  * @param[in] conf
799  *  Thresholds parameters.
800  *
801  * @return
802  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
803  */
804 struct mlx5_txq_ctrl *
805 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
806              unsigned int socket, const struct rte_eth_txconf *conf)
807 {
808         struct mlx5_priv *priv = dev->data->dev_private;
809         struct mlx5_txq_ctrl *tmpl;
810
811         tmpl = rte_calloc_socket("TXQ", 1,
812                                  sizeof(*tmpl) +
813                                  desc * sizeof(struct rte_mbuf *),
814                                  0, socket);
815         if (!tmpl) {
816                 rte_errno = ENOMEM;
817                 return NULL;
818         }
819         if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
820                                MLX5_MR_BTREE_CACHE_N, socket)) {
821                 /* rte_errno is already set. */
822                 goto error;
823         }
824         /* Save pointer of global generation number to check memory event. */
825         tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->mr.dev_gen;
826         assert(desc > MLX5_TX_COMP_THRESH);
827         tmpl->txq.offloads = conf->offloads |
828                              dev->data->dev_conf.txmode.offloads;
829         tmpl->priv = priv;
830         tmpl->socket = socket;
831         tmpl->txq.elts_n = log2above(desc);
832         tmpl->txq.port_id = dev->data->port_id;
833         tmpl->txq.idx = idx;
834         txq_set_params(tmpl);
835         if (txq_calc_wqebb_cnt(tmpl) >
836             priv->sh->device_attr.orig_attr.max_qp_wr) {
837                 DRV_LOG(ERR,
838                         "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
839                         " try smaller queue size",
840                         dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
841                         priv->sh->device_attr.orig_attr.max_qp_wr);
842                 rte_errno = ENOMEM;
843                 goto error;
844         }
845         tmpl->txq.elts =
846                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
847         rte_atomic32_inc(&tmpl->refcnt);
848         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
849         return tmpl;
850 error:
851         rte_free(tmpl);
852         return NULL;
853 }
854
855 /**
856  * Get a Tx queue.
857  *
858  * @param dev
859  *   Pointer to Ethernet device.
860  * @param idx
861  *   TX queue index.
862  *
863  * @return
864  *   A pointer to the queue if it exists.
865  */
866 struct mlx5_txq_ctrl *
867 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
868 {
869         struct mlx5_priv *priv = dev->data->dev_private;
870         struct mlx5_txq_ctrl *ctrl = NULL;
871
872         if ((*priv->txqs)[idx]) {
873                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
874                                     txq);
875                 mlx5_txq_ibv_get(dev, idx);
876                 rte_atomic32_inc(&ctrl->refcnt);
877         }
878         return ctrl;
879 }
880
881 /**
882  * Release a Tx queue.
883  *
884  * @param dev
885  *   Pointer to Ethernet device.
886  * @param idx
887  *   TX queue index.
888  *
889  * @return
890  *   1 while a reference on it exists, 0 when freed.
891  */
892 int
893 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
894 {
895         struct mlx5_priv *priv = dev->data->dev_private;
896         struct mlx5_txq_ctrl *txq;
897
898         if (!(*priv->txqs)[idx])
899                 return 0;
900         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
901         if (txq->ibv && !mlx5_txq_ibv_release(txq->ibv))
902                 txq->ibv = NULL;
903         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
904                 txq_free_elts(txq);
905                 mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
906                 LIST_REMOVE(txq, next);
907                 rte_free(txq);
908                 (*priv->txqs)[idx] = NULL;
909                 return 0;
910         }
911         return 1;
912 }
913
914 /**
915  * Verify if the queue can be released.
916  *
917  * @param dev
918  *   Pointer to Ethernet device.
919  * @param idx
920  *   TX queue index.
921  *
922  * @return
923  *   1 if the queue can be released.
924  */
925 int
926 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
927 {
928         struct mlx5_priv *priv = dev->data->dev_private;
929         struct mlx5_txq_ctrl *txq;
930
931         if (!(*priv->txqs)[idx])
932                 return -1;
933         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
934         return (rte_atomic32_read(&txq->refcnt) == 1);
935 }
936
937 /**
938  * Verify the Tx Queue list is empty
939  *
940  * @param dev
941  *   Pointer to Ethernet device.
942  *
943  * @return
944  *   The number of object not released.
945  */
946 int
947 mlx5_txq_verify(struct rte_eth_dev *dev)
948 {
949         struct mlx5_priv *priv = dev->data->dev_private;
950         struct mlx5_txq_ctrl *txq_ctrl;
951         int ret = 0;
952
953         LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
954                 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
955                         dev->data->port_id, txq_ctrl->txq.idx);
956                 ++ret;
957         }
958         return ret;
959 }