New upstream version 17.11.4
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_txq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41
42 /* Verbs header. */
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_mbuf.h>
53 #include <rte_malloc.h>
54 #include <rte_ethdev.h>
55 #include <rte_common.h>
56
57 #include "mlx5_utils.h"
58 #include "mlx5_defs.h"
59 #include "mlx5.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_autoconf.h"
62
63 /**
64  * Allocate TX queue elements.
65  *
66  * @param txq_ctrl
67  *   Pointer to TX queue structure.
68  */
69 void
70 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
71 {
72         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
73         unsigned int i;
74
75         for (i = 0; (i != elts_n); ++i)
76                 (*txq_ctrl->txq.elts)[i] = NULL;
77         DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
78                 PORT_ID(txq_ctrl->priv), txq_ctrl->idx, elts_n);
79         txq_ctrl->txq.elts_head = 0;
80         txq_ctrl->txq.elts_tail = 0;
81         txq_ctrl->txq.elts_comp = 0;
82 }
83
84 /**
85  * Free TX queue elements.
86  *
87  * @param txq_ctrl
88  *   Pointer to TX queue structure.
89  */
90 static void
91 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
92 {
93         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
94         const uint16_t elts_m = elts_n - 1;
95         uint16_t elts_head = txq_ctrl->txq.elts_head;
96         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
97         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
98
99         DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
100                 PORT_ID(txq_ctrl->priv), txq_ctrl->idx);
101         txq_ctrl->txq.elts_head = 0;
102         txq_ctrl->txq.elts_tail = 0;
103         txq_ctrl->txq.elts_comp = 0;
104
105         while (elts_tail != elts_head) {
106                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
107
108                 assert(elt != NULL);
109                 rte_pktmbuf_free_seg(elt);
110 #ifndef NDEBUG
111                 /* Poisoning. */
112                 memset(&(*elts)[elts_tail & elts_m],
113                        0x77,
114                        sizeof((*elts)[elts_tail & elts_m]));
115 #endif
116                 ++elts_tail;
117         }
118 }
119
120 /**
121  * DPDK callback to configure a TX queue.
122  *
123  * @param dev
124  *   Pointer to Ethernet device structure.
125  * @param idx
126  *   TX queue index.
127  * @param desc
128  *   Number of descriptors to configure in queue.
129  * @param socket
130  *   NUMA socket on which memory must be allocated.
131  * @param[in] conf
132  *   Thresholds parameters.
133  *
134  * @return
135  *   0 on success, a negative errno value otherwise and rte_errno is set.
136  */
137 int
138 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
139                     unsigned int socket, const struct rte_eth_txconf *conf)
140 {
141         struct priv *priv = dev->data->dev_private;
142         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
143         struct mlx5_txq_ctrl *txq_ctrl =
144                 container_of(txq, struct mlx5_txq_ctrl, txq);
145
146         if (desc <= MLX5_TX_COMP_THRESH) {
147                 DRV_LOG(WARNING,
148                         "port %u number of descriptors requested for Tx queue"
149                         " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
150                         " instead of %u",
151                         dev->data->port_id, idx, MLX5_TX_COMP_THRESH + 1, desc);
152                 desc = MLX5_TX_COMP_THRESH + 1;
153         }
154         if (!rte_is_power_of_2(desc)) {
155                 desc = 1 << log2above(desc);
156                 DRV_LOG(WARNING,
157                         "port %u increased number of descriptors in Tx queue"
158                         " %u to the next power of two (%d)",
159                         dev->data->port_id, idx, desc);
160         }
161         DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
162                 dev->data->port_id, idx, desc);
163         if (idx >= priv->txqs_n) {
164                 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
165                         dev->data->port_id, idx, priv->txqs_n);
166                 rte_errno = EOVERFLOW;
167                 return -rte_errno;
168         }
169         if (!mlx5_txq_releasable(dev, idx)) {
170                 rte_errno = EBUSY;
171                 DRV_LOG(ERR, "port %u unable to release queue index %u",
172                         dev->data->port_id, idx);
173                 return -rte_errno;
174         }
175         mlx5_txq_release(dev, idx);
176         txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
177         if (!txq_ctrl) {
178                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
179                         dev->data->port_id, idx);
180                 return -rte_errno;
181         }
182         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
183                 dev->data->port_id, idx);
184         (*priv->txqs)[idx] = &txq_ctrl->txq;
185         return 0;
186 }
187
188 /**
189  * DPDK callback to release a TX queue.
190  *
191  * @param dpdk_txq
192  *   Generic TX queue pointer.
193  */
194 void
195 mlx5_tx_queue_release(void *dpdk_txq)
196 {
197         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
198         struct mlx5_txq_ctrl *txq_ctrl;
199         struct priv *priv;
200         unsigned int i;
201
202         if (txq == NULL)
203                 return;
204         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
205         priv = txq_ctrl->priv;
206         for (i = 0; (i != priv->txqs_n); ++i)
207                 if ((*priv->txqs)[i] == txq) {
208                         mlx5_txq_release(ETH_DEV(priv), i);
209                         DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
210                                 PORT_ID(priv), txq_ctrl->idx);
211                         break;
212                 }
213 }
214
215
216 /**
217  * Mmap TX UAR(HW doorbell) pages into reserved UAR address space.
218  * Both primary and secondary process do mmap to make UAR address
219  * aligned.
220  *
221  * @param[in] dev
222  *   Pointer to Ethernet device.
223  * @param fd
224  *   Verbs file descriptor to map UAR pages.
225  *
226  * @return
227  *   0 on success, a negative errno value otherwise and rte_errno is set.
228  */
229 int
230 mlx5_tx_uar_remap(struct rte_eth_dev *dev, int fd)
231 {
232         struct priv *priv = dev->data->dev_private;
233         unsigned int i, j;
234         uintptr_t pages[priv->txqs_n];
235         unsigned int pages_n = 0;
236         uintptr_t uar_va;
237         uintptr_t off;
238         void *addr;
239         void *ret;
240         struct mlx5_txq_data *txq;
241         struct mlx5_txq_ctrl *txq_ctrl;
242         int already_mapped;
243         size_t page_size = sysconf(_SC_PAGESIZE);
244
245         memset(pages, 0, priv->txqs_n * sizeof(uintptr_t));
246         /*
247          * As rdma-core, UARs are mapped in size of OS page size.
248          * Use aligned address to avoid duplicate mmap.
249          * Ref to libmlx5 function: mlx5_init_context()
250          */
251         for (i = 0; i != priv->txqs_n; ++i) {
252                 if (!(*priv->txqs)[i])
253                         continue;
254                 txq = (*priv->txqs)[i];
255                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
256                 assert(txq_ctrl->idx == (uint16_t)i);
257                 /* UAR addr form verbs used to find dup and offset in page. */
258                 uar_va = (uintptr_t)txq_ctrl->bf_reg_orig;
259                 off = uar_va & (page_size - 1); /* offset in page. */
260                 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size); /* page addr. */
261                 already_mapped = 0;
262                 for (j = 0; j != pages_n; ++j) {
263                         if (pages[j] == uar_va) {
264                                 already_mapped = 1;
265                                 break;
266                         }
267                 }
268                 /* new address in reserved UAR address space. */
269                 addr = RTE_PTR_ADD(priv->uar_base,
270                                    uar_va & (MLX5_UAR_SIZE - 1));
271                 if (!already_mapped) {
272                         pages[pages_n++] = uar_va;
273                         /* fixed mmap to specified address in reserved
274                          * address space.
275                          */
276                         ret = mmap(addr, page_size,
277                                    PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
278                                    txq_ctrl->uar_mmap_offset);
279                         if (ret != addr) {
280                                 /* fixed mmap have to return same address */
281                                 DRV_LOG(ERR,
282                                         "port %u call to mmap failed on UAR"
283                                         " for txq %u",
284                                         dev->data->port_id, txq_ctrl->idx);
285                                 rte_errno = ENXIO;
286                                 return -rte_errno;
287                         }
288                 }
289                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) /* save once */
290                         txq_ctrl->txq.bf_reg = RTE_PTR_ADD((void *)addr, off);
291                 else
292                         assert(txq_ctrl->txq.bf_reg ==
293                                RTE_PTR_ADD((void *)addr, off));
294         }
295         return 0;
296 }
297
298 /**
299  * Create the Tx queue Verbs object.
300  *
301  * @param dev
302  *   Pointer to Ethernet device.
303  * @param idx
304  *   Queue index in DPDK Rx queue array
305  *
306  * @return
307  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
308  */
309 struct mlx5_txq_ibv *
310 mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx)
311 {
312         struct priv *priv = dev->data->dev_private;
313         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
314         struct mlx5_txq_ctrl *txq_ctrl =
315                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
316         struct mlx5_txq_ibv tmpl;
317         struct mlx5_txq_ibv *txq_ibv;
318         union {
319                 struct ibv_qp_init_attr_ex init;
320                 struct ibv_cq_init_attr_ex cq;
321                 struct ibv_qp_attr mod;
322                 struct ibv_cq_ex cq_attr;
323         } attr;
324         unsigned int cqe_n;
325         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
326         struct mlx5dv_cq cq_info;
327         struct mlx5dv_obj obj;
328         const int desc = 1 << txq_data->elts_n;
329         int ret = 0;
330
331         assert(txq_data);
332         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
333         priv->verbs_alloc_ctx.obj = txq_ctrl;
334         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
335                 DRV_LOG(ERR,
336                         "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
337                         dev->data->port_id);
338                 rte_errno = EINVAL;
339                 return NULL;
340         }
341         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
342         /* MRs will be registered in mp2mr[] later. */
343         attr.cq = (struct ibv_cq_init_attr_ex){
344                 .comp_mask = 0,
345         };
346         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
347                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
348         if (priv->mps == MLX5_MPW_ENHANCED)
349                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
350         tmpl.cq = ibv_create_cq(priv->ctx, cqe_n, NULL, NULL, 0);
351         if (tmpl.cq == NULL) {
352                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
353                         dev->data->port_id, idx);
354                 rte_errno = errno;
355                 goto error;
356         }
357         attr.init = (struct ibv_qp_init_attr_ex){
358                 /* CQ to be associated with the send queue. */
359                 .send_cq = tmpl.cq,
360                 /* CQ to be associated with the receive queue. */
361                 .recv_cq = tmpl.cq,
362                 .cap = {
363                         /* Max number of outstanding WRs. */
364                         .max_send_wr =
365                                 ((priv->device_attr.orig_attr.max_qp_wr <
366                                   desc) ?
367                                  priv->device_attr.orig_attr.max_qp_wr :
368                                  desc),
369                         /*
370                          * Max number of scatter/gather elements in a WR,
371                          * must be 1 to prevent libmlx5 from trying to affect
372                          * too much memory. TX gather is not impacted by the
373                          * priv->device_attr.max_sge limit and will still work
374                          * properly.
375                          */
376                         .max_send_sge = 1,
377                 },
378                 .qp_type = IBV_QPT_RAW_PACKET,
379                 /*
380                  * Do *NOT* enable this, completions events are managed per
381                  * Tx burst.
382                  */
383                 .sq_sig_all = 0,
384                 .pd = priv->pd,
385                 .comp_mask = IBV_QP_INIT_ATTR_PD,
386         };
387         if (txq_data->inline_en)
388                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
389         if (txq_data->tso_en) {
390                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
391                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
392         }
393         tmpl.qp = ibv_create_qp_ex(priv->ctx, &attr.init);
394         if (tmpl.qp == NULL) {
395                 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
396                         dev->data->port_id, idx);
397                 rte_errno = errno;
398                 goto error;
399         }
400         attr.mod = (struct ibv_qp_attr){
401                 /* Move the QP to this state. */
402                 .qp_state = IBV_QPS_INIT,
403                 /* Primary port number. */
404                 .port_num = priv->port
405         };
406         ret = ibv_modify_qp(tmpl.qp, &attr.mod, (IBV_QP_STATE | IBV_QP_PORT));
407         if (ret) {
408                 DRV_LOG(ERR,
409                         "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
410                         dev->data->port_id, idx);
411                 rte_errno = errno;
412                 goto error;
413         }
414         attr.mod = (struct ibv_qp_attr){
415                 .qp_state = IBV_QPS_RTR
416         };
417         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
418         if (ret) {
419                 DRV_LOG(ERR,
420                         "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
421                         dev->data->port_id, idx);
422                 rte_errno = errno;
423                 goto error;
424         }
425         attr.mod.qp_state = IBV_QPS_RTS;
426         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
427         if (ret) {
428                 DRV_LOG(ERR,
429                         "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
430                         dev->data->port_id, idx);
431                 rte_errno = errno;
432                 goto error;
433         }
434         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
435                                     txq_ctrl->socket);
436         if (!txq_ibv) {
437                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
438                         dev->data->port_id, idx);
439                 rte_errno = ENOMEM;
440                 goto error;
441         }
442         obj.cq.in = tmpl.cq;
443         obj.cq.out = &cq_info;
444         obj.qp.in = tmpl.qp;
445         obj.qp.out = &qp;
446         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
447         if (ret != 0) {
448                 rte_errno = errno;
449                 goto error;
450         }
451         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
452                 DRV_LOG(ERR,
453                         "port %u wrong MLX5_CQE_SIZE environment variable"
454                         " value: it should be set to %u",
455                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
456                 rte_errno = EINVAL;
457                 goto error;
458         }
459         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
460         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
461         txq_data->wqes = qp.sq.buf;
462         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
463         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
464         txq_ctrl->bf_reg_orig = qp.bf.reg;
465         txq_data->cq_db = cq_info.dbrec;
466         txq_data->cqes =
467                 (volatile struct mlx5_cqe (*)[])
468                 (uintptr_t)cq_info.buf;
469         txq_data->cq_ci = 0;
470 #ifndef NDEBUG
471         txq_data->cq_pi = 0;
472 #endif
473         txq_data->wqe_ci = 0;
474         txq_data->wqe_pi = 0;
475         txq_ibv->qp = tmpl.qp;
476         txq_ibv->cq = tmpl.cq;
477         rte_atomic32_inc(&txq_ibv->refcnt);
478         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
479                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
480         } else {
481                 DRV_LOG(ERR,
482                         "port %u failed to retrieve UAR info, invalid"
483                         " libmlx5.so",
484                         dev->data->port_id);
485                 rte_errno = EINVAL;
486                 goto error;
487         }
488         DRV_LOG(DEBUG, "port %u Verbs Tx queue %u: refcnt %d",
489                 dev->data->port_id, idx, rte_atomic32_read(&txq_ibv->refcnt));
490         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
491         txq_ibv->txq_ctrl = txq_ctrl;
492         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
493         return txq_ibv;
494 error:
495         ret = rte_errno; /* Save rte_errno before cleanup. */
496         if (tmpl.cq)
497                 claim_zero(ibv_destroy_cq(tmpl.cq));
498         if (tmpl.qp)
499                 claim_zero(ibv_destroy_qp(tmpl.qp));
500         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
501         rte_errno = ret; /* Restore rte_errno. */
502         return NULL;
503 }
504
505 /**
506  * Get an Tx queue Verbs object.
507  *
508  * @param dev
509  *   Pointer to Ethernet device.
510  * @param idx
511  *   Queue index in DPDK Rx queue array
512  *
513  * @return
514  *   The Verbs object if it exists.
515  */
516 struct mlx5_txq_ibv *
517 mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx)
518 {
519         struct priv *priv = dev->data->dev_private;
520         struct mlx5_txq_ctrl *txq_ctrl;
521
522         if (idx >= priv->txqs_n)
523                 return NULL;
524         if (!(*priv->txqs)[idx])
525                 return NULL;
526         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
527         if (txq_ctrl->ibv) {
528                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
529                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u: refcnt %d",
530                         dev->data->port_id, txq_ctrl->idx,
531                       rte_atomic32_read(&txq_ctrl->ibv->refcnt));
532         }
533         return txq_ctrl->ibv;
534 }
535
536 /**
537  * Release an Tx verbs queue object.
538  *
539  * @param txq_ibv
540  *   Verbs Tx queue object.
541  *
542  * @return
543  *   1 while a reference on it exists, 0 when freed.
544  */
545 int
546 mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv)
547 {
548         assert(txq_ibv);
549         DRV_LOG(DEBUG, "port %u Verbs Tx queue %u: refcnt %d",
550                 PORT_ID(txq_ibv->txq_ctrl->priv),
551                 txq_ibv->txq_ctrl->idx, rte_atomic32_read(&txq_ibv->refcnt));
552         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
553                 claim_zero(ibv_destroy_qp(txq_ibv->qp));
554                 claim_zero(ibv_destroy_cq(txq_ibv->cq));
555                 LIST_REMOVE(txq_ibv, next);
556                 rte_free(txq_ibv);
557                 return 0;
558         }
559         return 1;
560 }
561
562 /**
563  * Return true if a single reference exists on the object.
564  *
565  * @param txq_ibv
566  *   Verbs Tx queue object.
567  */
568 int
569 mlx5_txq_ibv_releasable(struct mlx5_txq_ibv *txq_ibv)
570 {
571         assert(txq_ibv);
572         return (rte_atomic32_read(&txq_ibv->refcnt) == 1);
573 }
574
575 /**
576  * Verify the Verbs Tx queue list is empty
577  *
578  * @param dev
579  *   Pointer to Ethernet device.
580  *
581  * @return
582  *   The number of object not released.
583  */
584 int
585 mlx5_txq_ibv_verify(struct rte_eth_dev *dev)
586 {
587         struct priv *priv = dev->data->dev_private;
588         int ret = 0;
589         struct mlx5_txq_ibv *txq_ibv;
590
591         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
592                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
593                         dev->data->port_id, txq_ibv->txq_ctrl->idx);
594                 ++ret;
595         }
596         return ret;
597 }
598
599 /**
600  * Create a DPDK Tx queue.
601  *
602  * @param dev
603  *   Pointer to Ethernet device.
604  * @param idx
605  *   TX queue index.
606  * @param desc
607  *   Number of descriptors to configure in queue.
608  * @param socket
609  *   NUMA socket on which memory must be allocated.
610  * @param[in] conf
611  *  Thresholds parameters.
612  *
613  * @return
614  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
615  */
616 struct mlx5_txq_ctrl *
617 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
618              unsigned int socket, const struct rte_eth_txconf *conf)
619 {
620         struct priv *priv = dev->data->dev_private;
621         const unsigned int max_tso_inline =
622                 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
623                  RTE_CACHE_LINE_SIZE);
624         struct mlx5_txq_ctrl *tmpl;
625
626         tmpl = rte_calloc_socket("TXQ", 1,
627                                  sizeof(*tmpl) +
628                                  desc * sizeof(struct rte_mbuf *),
629                                  0, socket);
630         if (!tmpl) {
631                 rte_errno = ENOMEM;
632                 return NULL;
633         }
634         assert(desc > MLX5_TX_COMP_THRESH);
635         tmpl->txq.flags = conf->txq_flags;
636         tmpl->priv = priv;
637         tmpl->socket = socket;
638         tmpl->txq.elts_n = log2above(desc);
639         tmpl->idx = idx;
640         if (priv->mps == MLX5_MPW_ENHANCED)
641                 tmpl->txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
642         /* MRs will be registered in mp2mr[] later. */
643         DRV_LOG(DEBUG, "port %u priv->device_attr.max_qp_wr is %d",
644                 dev->data->port_id, priv->device_attr.orig_attr.max_qp_wr);
645         DRV_LOG(DEBUG, "port %u priv->device_attr.max_sge is %d",
646                 dev->data->port_id, priv->device_attr.orig_attr.max_sge);
647         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
648                 unsigned int ds_cnt;
649
650                 tmpl->txq.max_inline =
651                         ((priv->txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
652                          RTE_CACHE_LINE_SIZE);
653                 tmpl->txq.inline_en = 1;
654                 /* TSO and MPS can't be enabled concurrently. */
655                 assert(!priv->tso || !priv->mps);
656                 if (priv->mps == MLX5_MPW_ENHANCED) {
657                         tmpl->txq.inline_max_packet_sz =
658                                 priv->inline_max_packet_sz;
659                         /* To minimize the size of data set, avoid requesting
660                          * too large WQ.
661                          */
662                         tmpl->max_inline_data =
663                                 ((RTE_MIN(priv->txq_inline,
664                                           priv->inline_max_packet_sz) +
665                                   (RTE_CACHE_LINE_SIZE - 1)) /
666                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
667                 } else {
668                         tmpl->max_inline_data =
669                                 tmpl->txq.max_inline * RTE_CACHE_LINE_SIZE;
670                 }
671                 /*
672                  * Check if the inline size is too large in a way which
673                  * can make the WQE DS to overflow.
674                  * Considering in calculation:
675                  *      WQE CTRL (1 DS)
676                  *      WQE ETH  (1 DS)
677                  *      Inline part (N DS)
678                  */
679                 ds_cnt = 2 + (tmpl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
680                 if (ds_cnt > MLX5_DSEG_MAX) {
681                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
682                                                   MLX5_WQE_DWORD_SIZE;
683
684                         max_inline = max_inline - (max_inline %
685                                                    RTE_CACHE_LINE_SIZE);
686                         DRV_LOG(WARNING,
687                                 "port %u txq inline is too large (%d) setting it"
688                                 " to the maximum possible: %d\n",
689                                 PORT_ID(priv), priv->txq_inline, max_inline);
690                         tmpl->txq.max_inline = max_inline / RTE_CACHE_LINE_SIZE;
691                 }
692         }
693         if (priv->tso) {
694                 tmpl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
695                 tmpl->txq.max_inline = RTE_MAX(tmpl->txq.max_inline,
696                                                max_tso_inline);
697                 tmpl->txq.tso_en = 1;
698         }
699         if (priv->tunnel_en)
700                 tmpl->txq.tunnel_en = 1;
701         tmpl->txq.elts =
702                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
703         tmpl->txq.stats.idx = idx;
704         rte_atomic32_inc(&tmpl->refcnt);
705         DRV_LOG(DEBUG, "port %u Tx queue %u: refcnt %d", dev->data->port_id,
706                 idx, rte_atomic32_read(&tmpl->refcnt));
707         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
708         return tmpl;
709 }
710
711 /**
712  * Get a Tx queue.
713  *
714  * @param dev
715  *   Pointer to Ethernet device.
716  * @param idx
717  *   TX queue index.
718  *
719  * @return
720  *   A pointer to the queue if it exists.
721  */
722 struct mlx5_txq_ctrl *
723 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
724 {
725         struct priv *priv = dev->data->dev_private;
726         struct mlx5_txq_ctrl *ctrl = NULL;
727
728         if ((*priv->txqs)[idx]) {
729                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
730                                     txq);
731                 unsigned int i;
732
733                 mlx5_txq_ibv_get(dev, idx);
734                 for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
735                         if (ctrl->txq.mp2mr[i])
736                                 claim_nonzero
737                                         (mlx5_mr_get(dev,
738                                                      ctrl->txq.mp2mr[i]->mp));
739                 }
740                 rte_atomic32_inc(&ctrl->refcnt);
741                 DRV_LOG(DEBUG, "port %u Tx queue %u refcnt %d",
742                         dev->data->port_id,
743                         ctrl->idx, rte_atomic32_read(&ctrl->refcnt));
744         }
745         return ctrl;
746 }
747
748 /**
749  * Release a Tx queue.
750  *
751  * @param dev
752  *   Pointer to Ethernet device.
753  * @param idx
754  *   TX queue index.
755  *
756  * @return
757  *   1 while a reference on it exists, 0 when freed.
758  */
759 int
760 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
761 {
762         struct priv *priv = dev->data->dev_private;
763         unsigned int i;
764         struct mlx5_txq_ctrl *txq;
765         size_t page_size = sysconf(_SC_PAGESIZE);
766
767         if (!(*priv->txqs)[idx])
768                 return 0;
769         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
770         DRV_LOG(DEBUG, "port %u Tx queue %u: refcnt %d", dev->data->port_id,
771                 txq->idx, rte_atomic32_read(&txq->refcnt));
772         if (txq->ibv && !mlx5_txq_ibv_release(txq->ibv))
773                 txq->ibv = NULL;
774         for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
775                 if (txq->txq.mp2mr[i]) {
776                         mlx5_mr_release(txq->txq.mp2mr[i]);
777                         txq->txq.mp2mr[i] = NULL;
778                 }
779         }
780         if (priv->uar_base)
781                 munmap((void *)RTE_ALIGN_FLOOR((uintptr_t)txq->txq.bf_reg,
782                        page_size), page_size);
783         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
784                 txq_free_elts(txq);
785                 LIST_REMOVE(txq, next);
786                 rte_free(txq);
787                 (*priv->txqs)[idx] = NULL;
788                 return 0;
789         }
790         return 1;
791 }
792
793 /**
794  * Verify if the queue can be released.
795  *
796  * @param dev
797  *   Pointer to Ethernet device.
798  * @param idx
799  *   TX queue index.
800  *
801  * @return
802  *   1 if the queue can be released.
803  */
804 int
805 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
806 {
807         struct priv *priv = dev->data->dev_private;
808         struct mlx5_txq_ctrl *txq;
809
810         if (!(*priv->txqs)[idx])
811                 return -1;
812         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
813         return (rte_atomic32_read(&txq->refcnt) == 1);
814 }
815
816 /**
817  * Verify the Tx Queue list is empty
818  *
819  * @param dev
820  *   Pointer to Ethernet device.
821  *
822  * @return
823  *   The number of object not released.
824  */
825 int
826 mlx5_txq_verify(struct rte_eth_dev *dev)
827 {
828         struct priv *priv = dev->data->dev_private;
829         struct mlx5_txq_ctrl *txq;
830         int ret = 0;
831
832         LIST_FOREACH(txq, &priv->txqsctrl, next) {
833                 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
834                         dev->data->port_id, txq->idx);
835                 ++ret;
836         }
837         return ret;
838 }