net/mlx5: add device configuration structure
[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         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
78         txq_ctrl->txq.elts_head = 0;
79         txq_ctrl->txq.elts_tail = 0;
80         txq_ctrl->txq.elts_comp = 0;
81 }
82
83 /**
84  * Free TX queue elements.
85  *
86  * @param txq_ctrl
87  *   Pointer to TX queue structure.
88  */
89 static void
90 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
91 {
92         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
93         const uint16_t elts_m = elts_n - 1;
94         uint16_t elts_head = txq_ctrl->txq.elts_head;
95         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
96         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
97
98         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
99         txq_ctrl->txq.elts_head = 0;
100         txq_ctrl->txq.elts_tail = 0;
101         txq_ctrl->txq.elts_comp = 0;
102
103         while (elts_tail != elts_head) {
104                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
105
106                 assert(elt != NULL);
107                 rte_pktmbuf_free_seg(elt);
108 #ifndef NDEBUG
109                 /* Poisoning. */
110                 memset(&(*elts)[elts_tail & elts_m],
111                        0x77,
112                        sizeof((*elts)[elts_tail & elts_m]));
113 #endif
114                 ++elts_tail;
115         }
116 }
117
118 /**
119  * DPDK callback to configure a TX queue.
120  *
121  * @param dev
122  *   Pointer to Ethernet device structure.
123  * @param idx
124  *   TX queue index.
125  * @param desc
126  *   Number of descriptors to configure in queue.
127  * @param socket
128  *   NUMA socket on which memory must be allocated.
129  * @param[in] conf
130  *   Thresholds parameters.
131  *
132  * @return
133  *   0 on success, negative errno value on failure.
134  */
135 int
136 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
137                     unsigned int socket, const struct rte_eth_txconf *conf)
138 {
139         struct priv *priv = dev->data->dev_private;
140         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
141         struct mlx5_txq_ctrl *txq_ctrl =
142                 container_of(txq, struct mlx5_txq_ctrl, txq);
143         int ret = 0;
144
145         priv_lock(priv);
146         if (desc <= MLX5_TX_COMP_THRESH) {
147                 WARN("%p: number of descriptors requested for TX queue %u"
148                      " must be higher than MLX5_TX_COMP_THRESH, using"
149                      " %u instead of %u",
150                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
151                 desc = MLX5_TX_COMP_THRESH + 1;
152         }
153         if (!rte_is_power_of_2(desc)) {
154                 desc = 1 << log2above(desc);
155                 WARN("%p: increased number of descriptors in TX queue %u"
156                      " to the next power of two (%d)",
157                      (void *)dev, idx, desc);
158         }
159         DEBUG("%p: configuring queue %u for %u descriptors",
160               (void *)dev, idx, desc);
161         if (idx >= priv->txqs_n) {
162                 ERROR("%p: queue index out of range (%u >= %u)",
163                       (void *)dev, idx, priv->txqs_n);
164                 priv_unlock(priv);
165                 return -EOVERFLOW;
166         }
167         if (!mlx5_priv_txq_releasable(priv, idx)) {
168                 ret = EBUSY;
169                 ERROR("%p: unable to release queue index %u",
170                       (void *)dev, idx);
171                 goto out;
172         }
173         mlx5_priv_txq_release(priv, idx);
174         txq_ctrl = mlx5_priv_txq_new(priv, idx, desc, socket, conf);
175         if (!txq_ctrl) {
176                 ERROR("%p: unable to allocate queue index %u",
177                       (void *)dev, idx);
178                 ret = ENOMEM;
179                 goto out;
180         }
181         DEBUG("%p: adding TX queue %p to list",
182               (void *)dev, (void *)txq_ctrl);
183         (*priv->txqs)[idx] = &txq_ctrl->txq;
184 out:
185         priv_unlock(priv);
186         return -ret;
187 }
188
189 /**
190  * DPDK callback to release a TX queue.
191  *
192  * @param dpdk_txq
193  *   Generic TX queue pointer.
194  */
195 void
196 mlx5_tx_queue_release(void *dpdk_txq)
197 {
198         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
199         struct mlx5_txq_ctrl *txq_ctrl;
200         struct priv *priv;
201         unsigned int i;
202
203         if (txq == NULL)
204                 return;
205         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
206         priv = txq_ctrl->priv;
207         priv_lock(priv);
208         for (i = 0; (i != priv->txqs_n); ++i)
209                 if ((*priv->txqs)[i] == txq) {
210                         DEBUG("%p: removing TX queue %p from list",
211                               (void *)priv->dev, (void *)txq_ctrl);
212                         mlx5_priv_txq_release(priv, i);
213                         break;
214                 }
215         priv_unlock(priv);
216 }
217
218
219 /**
220  * Map locally UAR used in Tx queues for BlueFlame doorbell.
221  *
222  * @param[in] priv
223  *   Pointer to private structure.
224  * @param fd
225  *   Verbs file descriptor to map UAR pages.
226  *
227  * @return
228  *   0 on success, errno value on failure.
229  */
230 int
231 priv_tx_uar_remap(struct priv *priv, int fd)
232 {
233         unsigned int i, j;
234         uintptr_t pages[priv->txqs_n];
235         unsigned int pages_n = 0;
236         uintptr_t uar_va;
237         void *addr;
238         struct mlx5_txq_data *txq;
239         struct mlx5_txq_ctrl *txq_ctrl;
240         int already_mapped;
241         size_t page_size = sysconf(_SC_PAGESIZE);
242
243         memset(pages, 0, priv->txqs_n * sizeof(uintptr_t));
244         /*
245          * As rdma-core, UARs are mapped in size of OS page size.
246          * Use aligned address to avoid duplicate mmap.
247          * Ref to libmlx5 function: mlx5_init_context()
248          */
249         for (i = 0; i != priv->txqs_n; ++i) {
250                 txq = (*priv->txqs)[i];
251                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
252                 uar_va = (uintptr_t)txq_ctrl->txq.bf_reg;
253                 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size);
254                 already_mapped = 0;
255                 for (j = 0; j != pages_n; ++j) {
256                         if (pages[j] == uar_va) {
257                                 already_mapped = 1;
258                                 break;
259                         }
260                 }
261                 if (already_mapped)
262                         continue;
263                 pages[pages_n++] = uar_va;
264                 addr = mmap((void *)uar_va, page_size,
265                             PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
266                             txq_ctrl->uar_mmap_offset);
267                 if (addr != (void *)uar_va) {
268                         ERROR("call to mmap failed on UAR for txq %d\n", i);
269                         return -1;
270                 }
271         }
272         return 0;
273 }
274
275 /**
276  * Check if the burst function is using eMPW.
277  *
278  * @param tx_pkt_burst
279  *   Tx burst function pointer.
280  *
281  * @return
282  *   1 if the burst function is using eMPW, 0 otherwise.
283  */
284 static int
285 is_empw_burst_func(eth_tx_burst_t tx_pkt_burst)
286 {
287         if (tx_pkt_burst == mlx5_tx_burst_raw_vec ||
288             tx_pkt_burst == mlx5_tx_burst_vec ||
289             tx_pkt_burst == mlx5_tx_burst_empw)
290                 return 1;
291         return 0;
292 }
293
294 /**
295  * Create the Tx queue Verbs object.
296  *
297  * @param priv
298  *   Pointer to private structure.
299  * @param idx
300  *   Queue index in DPDK Rx queue array
301  *
302  * @return
303  *   The Verbs object initialised if it can be created.
304  */
305 struct mlx5_txq_ibv*
306 mlx5_priv_txq_ibv_new(struct priv *priv, uint16_t idx)
307 {
308         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
309         struct mlx5_txq_ctrl *txq_ctrl =
310                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
311         struct mlx5_txq_ibv tmpl;
312         struct mlx5_txq_ibv *txq_ibv;
313         union {
314                 struct ibv_qp_init_attr_ex init;
315                 struct ibv_cq_init_attr_ex cq;
316                 struct ibv_qp_attr mod;
317                 struct ibv_cq_ex cq_attr;
318         } attr;
319         unsigned int cqe_n;
320         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
321         struct mlx5dv_cq cq_info;
322         struct mlx5dv_obj obj;
323         const int desc = 1 << txq_data->elts_n;
324         eth_tx_burst_t tx_pkt_burst = priv_select_tx_function(priv, priv->dev);
325         int ret = 0;
326
327         assert(txq_data);
328         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
329                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
330                 goto error;
331         }
332         memset(&tmpl, 0, sizeof(struct mlx5_txq_ibv));
333         /* MRs will be registered in mp2mr[] later. */
334         attr.cq = (struct ibv_cq_init_attr_ex){
335                 .comp_mask = 0,
336         };
337         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
338                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
339         if (is_empw_burst_func(tx_pkt_burst))
340                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
341         tmpl.cq = ibv_create_cq(priv->ctx, cqe_n, NULL, NULL, 0);
342         if (tmpl.cq == NULL) {
343                 ERROR("%p: CQ creation failure", (void *)txq_ctrl);
344                 goto error;
345         }
346         attr.init = (struct ibv_qp_init_attr_ex){
347                 /* CQ to be associated with the send queue. */
348                 .send_cq = tmpl.cq,
349                 /* CQ to be associated with the receive queue. */
350                 .recv_cq = tmpl.cq,
351                 .cap = {
352                         /* Max number of outstanding WRs. */
353                         .max_send_wr =
354                                 ((priv->device_attr.orig_attr.max_qp_wr <
355                                   desc) ?
356                                  priv->device_attr.orig_attr.max_qp_wr :
357                                  desc),
358                         /*
359                          * Max number of scatter/gather elements in a WR,
360                          * must be 1 to prevent libmlx5 from trying to affect
361                          * too much memory. TX gather is not impacted by the
362                          * priv->device_attr.max_sge limit and will still work
363                          * properly.
364                          */
365                         .max_send_sge = 1,
366                 },
367                 .qp_type = IBV_QPT_RAW_PACKET,
368                 /*
369                  * Do *NOT* enable this, completions events are managed per
370                  * Tx burst.
371                  */
372                 .sq_sig_all = 0,
373                 .pd = priv->pd,
374                 .comp_mask = IBV_QP_INIT_ATTR_PD,
375         };
376         if (txq_data->max_inline)
377                 attr.init.cap.max_inline_data = txq_ctrl->max_inline_data;
378         if (txq_data->tso_en) {
379                 attr.init.max_tso_header = txq_ctrl->max_tso_header;
380                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
381         }
382         tmpl.qp = ibv_create_qp_ex(priv->ctx, &attr.init);
383         if (tmpl.qp == NULL) {
384                 ERROR("%p: QP creation failure", (void *)txq_ctrl);
385                 goto error;
386         }
387         attr.mod = (struct ibv_qp_attr){
388                 /* Move the QP to this state. */
389                 .qp_state = IBV_QPS_INIT,
390                 /* Primary port number. */
391                 .port_num = priv->port
392         };
393         ret = ibv_modify_qp(tmpl.qp, &attr.mod, (IBV_QP_STATE | IBV_QP_PORT));
394         if (ret) {
395                 ERROR("%p: QP state to IBV_QPS_INIT failed", (void *)txq_ctrl);
396                 goto error;
397         }
398         attr.mod = (struct ibv_qp_attr){
399                 .qp_state = IBV_QPS_RTR
400         };
401         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
402         if (ret) {
403                 ERROR("%p: QP state to IBV_QPS_RTR failed", (void *)txq_ctrl);
404                 goto error;
405         }
406         attr.mod.qp_state = IBV_QPS_RTS;
407         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
408         if (ret) {
409                 ERROR("%p: QP state to IBV_QPS_RTS failed", (void *)txq_ctrl);
410                 goto error;
411         }
412         txq_ibv = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_txq_ibv), 0,
413                                     txq_ctrl->socket);
414         if (!txq_ibv) {
415                 ERROR("%p: cannot allocate memory", (void *)txq_ctrl);
416                 goto error;
417         }
418         obj.cq.in = tmpl.cq;
419         obj.cq.out = &cq_info;
420         obj.qp.in = tmpl.qp;
421         obj.qp.out = &qp;
422         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
423         if (ret != 0)
424                 goto error;
425         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
426                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
427                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
428                 goto error;
429         }
430         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
431         txq_data->qp_num_8s = tmpl.qp->qp_num << 8;
432         txq_data->wqes = qp.sq.buf;
433         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
434         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
435         txq_data->bf_reg = qp.bf.reg;
436         txq_data->cq_db = cq_info.dbrec;
437         txq_data->cqes =
438                 (volatile struct mlx5_cqe (*)[])
439                 (uintptr_t)cq_info.buf;
440         txq_data->cq_ci = 0;
441 #ifndef NDEBUG
442         txq_data->cq_pi = 0;
443 #endif
444         txq_data->wqe_ci = 0;
445         txq_data->wqe_pi = 0;
446         txq_ibv->qp = tmpl.qp;
447         txq_ibv->cq = tmpl.cq;
448         rte_atomic32_inc(&txq_ibv->refcnt);
449         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
450                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
451         } else {
452                 ERROR("Failed to retrieve UAR info, invalid libmlx5.so version");
453                 goto error;
454         }
455         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
456               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
457         LIST_INSERT_HEAD(&priv->txqsibv, txq_ibv, next);
458         return txq_ibv;
459 error:
460         if (tmpl.cq)
461                 claim_zero(ibv_destroy_cq(tmpl.cq));
462         if (tmpl.qp)
463                 claim_zero(ibv_destroy_qp(tmpl.qp));
464         return NULL;
465 }
466
467 /**
468  * Get an Tx queue Verbs object.
469  *
470  * @param priv
471  *   Pointer to private structure.
472  * @param idx
473  *   Queue index in DPDK Rx queue array
474  *
475  * @return
476  *   The Verbs object if it exists.
477  */
478 struct mlx5_txq_ibv*
479 mlx5_priv_txq_ibv_get(struct priv *priv, uint16_t idx)
480 {
481         struct mlx5_txq_ctrl *txq_ctrl;
482
483         if (idx >= priv->txqs_n)
484                 return NULL;
485         if (!(*priv->txqs)[idx])
486                 return NULL;
487         txq_ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
488         if (txq_ctrl->ibv) {
489                 rte_atomic32_inc(&txq_ctrl->ibv->refcnt);
490                 DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
491                       (void *)txq_ctrl->ibv,
492                       rte_atomic32_read(&txq_ctrl->ibv->refcnt));
493         }
494         return txq_ctrl->ibv;
495 }
496
497 /**
498  * Release an Tx verbs queue object.
499  *
500  * @param priv
501  *   Pointer to private structure.
502  * @param txq_ibv
503  *   Verbs Tx queue object.
504  *
505  * @return
506  *   0 on success, errno on failure.
507  */
508 int
509 mlx5_priv_txq_ibv_release(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
510 {
511         (void)priv;
512         assert(txq_ibv);
513         DEBUG("%p: Verbs Tx queue %p: refcnt %d", (void *)priv,
514               (void *)txq_ibv, rte_atomic32_read(&txq_ibv->refcnt));
515         if (rte_atomic32_dec_and_test(&txq_ibv->refcnt)) {
516                 claim_zero(ibv_destroy_qp(txq_ibv->qp));
517                 claim_zero(ibv_destroy_cq(txq_ibv->cq));
518                 LIST_REMOVE(txq_ibv, next);
519                 rte_free(txq_ibv);
520                 return 0;
521         }
522         return EBUSY;
523 }
524
525 /**
526  * Return true if a single reference exists on the object.
527  *
528  * @param priv
529  *   Pointer to private structure.
530  * @param txq_ibv
531  *   Verbs Tx queue object.
532  */
533 int
534 mlx5_priv_txq_ibv_releasable(struct priv *priv, struct mlx5_txq_ibv *txq_ibv)
535 {
536         (void)priv;
537         assert(txq_ibv);
538         return (rte_atomic32_read(&txq_ibv->refcnt) == 1);
539 }
540
541 /**
542  * Verify the Verbs Tx queue list is empty
543  *
544  * @param priv
545  *  Pointer to private structure.
546  *
547  * @return the number of object not released.
548  */
549 int
550 mlx5_priv_txq_ibv_verify(struct priv *priv)
551 {
552         int ret = 0;
553         struct mlx5_txq_ibv *txq_ibv;
554
555         LIST_FOREACH(txq_ibv, &priv->txqsibv, next) {
556                 DEBUG("%p: Verbs Tx queue %p still referenced", (void *)priv,
557                       (void *)txq_ibv);
558                 ++ret;
559         }
560         return ret;
561 }
562
563 /**
564  * Set Tx queue parameters from device configuration.
565  *
566  * @param txq_ctrl
567  *   Pointer to Tx queue control structure.
568  */
569 static void
570 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
571 {
572         struct priv *priv = txq_ctrl->priv;
573         struct mlx5_dev_config *config = &priv->config;
574         const unsigned int max_tso_inline =
575                 ((MLX5_MAX_TSO_HEADER + (RTE_CACHE_LINE_SIZE - 1)) /
576                  RTE_CACHE_LINE_SIZE);
577         unsigned int txq_inline;
578         unsigned int txqs_inline;
579         unsigned int inline_max_packet_sz;
580         eth_tx_burst_t tx_pkt_burst = priv_select_tx_function(priv, priv->dev);
581         int is_empw_func = is_empw_burst_func(tx_pkt_burst);
582
583         txq_inline = (config->txq_inline == MLX5_ARG_UNSET) ?
584                 0 : config->txq_inline;
585         txqs_inline = (config->txqs_inline == MLX5_ARG_UNSET) ?
586                 0 : config->txqs_inline;
587         inline_max_packet_sz =
588                 (config->inline_max_packet_sz == MLX5_ARG_UNSET) ?
589                 0 : config->inline_max_packet_sz;
590         if (is_empw_func) {
591                 if (config->txq_inline == MLX5_ARG_UNSET)
592                         txq_inline = MLX5_WQE_SIZE_MAX - MLX5_WQE_SIZE;
593                 if (config->txqs_inline == MLX5_ARG_UNSET)
594                         txqs_inline = MLX5_EMPW_MIN_TXQS;
595                 if (config->inline_max_packet_sz == MLX5_ARG_UNSET)
596                         inline_max_packet_sz = MLX5_EMPW_MAX_INLINE_LEN;
597                 txq_ctrl->txq.mpw_hdr_dseg = config->mpw_hdr_dseg;
598                 txq_ctrl->txq.inline_max_packet_sz = inline_max_packet_sz;
599         }
600         if (txq_inline && priv->txqs_n >= txqs_inline) {
601                 unsigned int ds_cnt;
602
603                 txq_ctrl->txq.max_inline =
604                         ((txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
605                          RTE_CACHE_LINE_SIZE);
606                 /* TSO and MPS can't be enabled concurrently. */
607                 assert(!config->tso || !config->mps);
608                 if (is_empw_func) {
609                         /* To minimize the size of data set, avoid requesting
610                          * too large WQ.
611                          */
612                         txq_ctrl->max_inline_data =
613                                 ((RTE_MIN(txq_inline,
614                                           inline_max_packet_sz) +
615                                   (RTE_CACHE_LINE_SIZE - 1)) /
616                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
617                 } else if (config->tso) {
618                         int inline_diff = txq_ctrl->txq.max_inline -
619                                           max_tso_inline;
620
621                         /*
622                          * Adjust inline value as Verbs aggregates
623                          * tso_inline and txq_inline fields.
624                          */
625                         txq_ctrl->max_inline_data = inline_diff > 0 ?
626                                                inline_diff *
627                                                RTE_CACHE_LINE_SIZE :
628                                                0;
629                 } else {
630                         txq_ctrl->max_inline_data =
631                                 txq_ctrl->txq.max_inline * RTE_CACHE_LINE_SIZE;
632                 }
633                 /*
634                  * Check if the inline size is too large in a way which
635                  * can make the WQE DS to overflow.
636                  * Considering in calculation:
637                  *      WQE CTRL (1 DS)
638                  *      WQE ETH  (1 DS)
639                  *      Inline part (N DS)
640                  */
641                 ds_cnt = 2 + (txq_ctrl->txq.max_inline / MLX5_WQE_DWORD_SIZE);
642                 if (ds_cnt > MLX5_DSEG_MAX) {
643                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
644                                                   MLX5_WQE_DWORD_SIZE;
645
646                         max_inline = max_inline - (max_inline %
647                                                    RTE_CACHE_LINE_SIZE);
648                         WARN("txq inline is too large (%d) setting it to "
649                              "the maximum possible: %d\n",
650                              txq_inline, max_inline);
651                         txq_ctrl->txq.max_inline = max_inline /
652                                                    RTE_CACHE_LINE_SIZE;
653                 }
654         }
655         if (config->tso) {
656                 txq_ctrl->max_tso_header = max_tso_inline * RTE_CACHE_LINE_SIZE;
657                 txq_ctrl->txq.max_inline = RTE_MAX(txq_ctrl->txq.max_inline,
658                                                    max_tso_inline);
659                 txq_ctrl->txq.tso_en = 1;
660         }
661         txq_ctrl->txq.tunnel_en = config->tunnel_en;
662 }
663
664 /**
665  * Create a DPDK Tx queue.
666  *
667  * @param priv
668  *   Pointer to private structure.
669  * @param idx
670  *   TX queue index.
671  * @param desc
672  *   Number of descriptors to configure in queue.
673  * @param socket
674  *   NUMA socket on which memory must be allocated.
675  * @param[in] conf
676  *  Thresholds parameters.
677  *
678  * @return
679  *   A DPDK queue object on success.
680  */
681 struct mlx5_txq_ctrl*
682 mlx5_priv_txq_new(struct priv *priv, uint16_t idx, uint16_t desc,
683                   unsigned int socket,
684                   const struct rte_eth_txconf *conf)
685 {
686         struct mlx5_txq_ctrl *tmpl;
687
688         tmpl = rte_calloc_socket("TXQ", 1,
689                                  sizeof(*tmpl) +
690                                  desc * sizeof(struct rte_mbuf *),
691                                  0, socket);
692         if (!tmpl)
693                 return NULL;
694         assert(desc > MLX5_TX_COMP_THRESH);
695         tmpl->txq.flags = conf->txq_flags;
696         tmpl->priv = priv;
697         tmpl->socket = socket;
698         tmpl->txq.elts_n = log2above(desc);
699         txq_set_params(tmpl);
700         /* MRs will be registered in mp2mr[] later. */
701         DEBUG("priv->device_attr.max_qp_wr is %d",
702               priv->device_attr.orig_attr.max_qp_wr);
703         DEBUG("priv->device_attr.max_sge is %d",
704               priv->device_attr.orig_attr.max_sge);
705         tmpl->txq.elts =
706                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])(tmpl + 1);
707         tmpl->txq.stats.idx = idx;
708         rte_atomic32_inc(&tmpl->refcnt);
709         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
710               (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
711         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
712         return tmpl;
713 }
714
715 /**
716  * Get a Tx queue.
717  *
718  * @param priv
719  *   Pointer to private structure.
720  * @param idx
721  *   TX queue index.
722  *
723  * @return
724  *   A pointer to the queue if it exists.
725  */
726 struct mlx5_txq_ctrl*
727 mlx5_priv_txq_get(struct priv *priv, uint16_t idx)
728 {
729         struct mlx5_txq_ctrl *ctrl = NULL;
730
731         if ((*priv->txqs)[idx]) {
732                 ctrl = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl,
733                                     txq);
734                 unsigned int i;
735
736                 mlx5_priv_txq_ibv_get(priv, idx);
737                 for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
738                         struct mlx5_mr *mr = NULL;
739
740                         (void)mr;
741                         if (ctrl->txq.mp2mr[i]) {
742                                 mr = priv_mr_get(priv, ctrl->txq.mp2mr[i]->mp);
743                                 assert(mr);
744                         }
745                 }
746                 rte_atomic32_inc(&ctrl->refcnt);
747                 DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
748                       (void *)ctrl, rte_atomic32_read(&ctrl->refcnt));
749         }
750         return ctrl;
751 }
752
753 /**
754  * Release a Tx queue.
755  *
756  * @param priv
757  *   Pointer to private structure.
758  * @param idx
759  *   TX queue index.
760  *
761  * @return
762  *   0 on success, errno on failure.
763  */
764 int
765 mlx5_priv_txq_release(struct priv *priv, uint16_t idx)
766 {
767         unsigned int i;
768         struct mlx5_txq_ctrl *txq;
769
770         if (!(*priv->txqs)[idx])
771                 return 0;
772         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
773         DEBUG("%p: Tx queue %p: refcnt %d", (void *)priv,
774               (void *)txq, rte_atomic32_read(&txq->refcnt));
775         if (txq->ibv) {
776                 int ret;
777
778                 ret = mlx5_priv_txq_ibv_release(priv, txq->ibv);
779                 if (!ret)
780                         txq->ibv = NULL;
781         }
782         for (i = 0; i != MLX5_PMD_TX_MP_CACHE; ++i) {
783                 if (txq->txq.mp2mr[i]) {
784                         priv_mr_release(priv, txq->txq.mp2mr[i]);
785                         txq->txq.mp2mr[i] = NULL;
786                 }
787         }
788         if (rte_atomic32_dec_and_test(&txq->refcnt)) {
789                 txq_free_elts(txq);
790                 LIST_REMOVE(txq, next);
791                 rte_free(txq);
792                 (*priv->txqs)[idx] = NULL;
793                 return 0;
794         }
795         return EBUSY;
796 }
797
798 /**
799  * Verify if the queue can be released.
800  *
801  * @param priv
802  *   Pointer to private structure.
803  * @param idx
804  *   TX queue index.
805  *
806  * @return
807  *   1 if the queue can be released.
808  */
809 int
810 mlx5_priv_txq_releasable(struct priv *priv, uint16_t idx)
811 {
812         struct mlx5_txq_ctrl *txq;
813
814         if (!(*priv->txqs)[idx])
815                 return -1;
816         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
817         return (rte_atomic32_read(&txq->refcnt) == 1);
818 }
819
820 /**
821  * Verify the Tx Queue list is empty
822  *
823  * @param priv
824  *  Pointer to private structure.
825  *
826  * @return the number of object not released.
827  */
828 int
829 mlx5_priv_txq_verify(struct priv *priv)
830 {
831         struct mlx5_txq_ctrl *txq;
832         int ret = 0;
833
834         LIST_FOREACH(txq, &priv->txqsctrl, next) {
835                 DEBUG("%p: Tx Queue %p still referenced", (void *)priv,
836                       (void *)txq);
837                 ++ret;
838         }
839         return ret;
840 }