net/mlx5: support upstream rdma-core
[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
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-Wpedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-Wpedantic"
48 #endif
49
50 #include <rte_mbuf.h>
51 #include <rte_malloc.h>
52 #include <rte_ethdev.h>
53 #include <rte_common.h>
54
55 #include "mlx5_utils.h"
56 #include "mlx5_defs.h"
57 #include "mlx5.h"
58 #include "mlx5_rxtx.h"
59 #include "mlx5_autoconf.h"
60
61 /**
62  * Allocate TX queue elements.
63  *
64  * @param txq_ctrl
65  *   Pointer to TX queue structure.
66  * @param elts_n
67  *   Number of elements to allocate.
68  */
69 static void
70 txq_alloc_elts(struct txq_ctrl *txq_ctrl, unsigned int elts_n)
71 {
72         unsigned int i;
73
74         for (i = 0; (i != elts_n); ++i)
75                 (*txq_ctrl->txq.elts)[i] = NULL;
76         for (i = 0; (i != (1u << txq_ctrl->txq.wqe_n)); ++i) {
77                 volatile struct mlx5_wqe64 *wqe =
78                         (volatile struct mlx5_wqe64 *)
79                         txq_ctrl->txq.wqes + i;
80
81                 memset((void *)(uintptr_t)wqe, 0x0, sizeof(*wqe));
82         }
83         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
84         txq_ctrl->txq.elts_head = 0;
85         txq_ctrl->txq.elts_tail = 0;
86         txq_ctrl->txq.elts_comp = 0;
87 }
88
89 /**
90  * Free TX queue elements.
91  *
92  * @param txq_ctrl
93  *   Pointer to TX queue structure.
94  */
95 static void
96 txq_free_elts(struct txq_ctrl *txq_ctrl)
97 {
98         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
99         const uint16_t elts_m = elts_n - 1;
100         uint16_t elts_head = txq_ctrl->txq.elts_head;
101         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
102         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
103
104         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
105         txq_ctrl->txq.elts_head = 0;
106         txq_ctrl->txq.elts_tail = 0;
107         txq_ctrl->txq.elts_comp = 0;
108
109         while (elts_tail != elts_head) {
110                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
111
112                 assert(elt != NULL);
113                 rte_pktmbuf_free_seg(elt);
114 #ifndef NDEBUG
115                 /* Poisoning. */
116                 memset(&(*elts)[elts_tail & elts_m],
117                        0x77,
118                        sizeof((*elts)[elts_tail & elts_m]));
119 #endif
120                 ++elts_tail;
121         }
122 }
123
124 /**
125  * Clean up a TX queue.
126  *
127  * Destroy objects, free allocated memory and reset the structure for reuse.
128  *
129  * @param txq_ctrl
130  *   Pointer to TX queue structure.
131  */
132 void
133 txq_cleanup(struct txq_ctrl *txq_ctrl)
134 {
135         size_t i;
136
137         DEBUG("cleaning up %p", (void *)txq_ctrl);
138         txq_free_elts(txq_ctrl);
139         if (txq_ctrl->qp != NULL)
140                 claim_zero(ibv_destroy_qp(txq_ctrl->qp));
141         if (txq_ctrl->cq != NULL)
142                 claim_zero(ibv_destroy_cq(txq_ctrl->cq));
143         for (i = 0; (i != RTE_DIM(txq_ctrl->txq.mp2mr)); ++i) {
144                 if (txq_ctrl->txq.mp2mr[i].mr == NULL)
145                         break;
146                 claim_zero(ibv_dereg_mr(txq_ctrl->txq.mp2mr[i].mr));
147         }
148         memset(txq_ctrl, 0, sizeof(*txq_ctrl));
149 }
150
151 /**
152  * Initialize TX queue.
153  *
154  * @param tmpl
155  *   Pointer to TX queue control template.
156  * @param txq_ctrl
157  *   Pointer to TX queue control.
158  *
159  * @return
160  *   0 on success, errno value on failure.
161  */
162 static inline int
163 txq_setup(struct txq_ctrl *tmpl, struct txq_ctrl *txq_ctrl)
164 {
165         struct mlx5dv_qp qp;
166         struct ibv_cq *ibcq = tmpl->cq;
167         struct mlx5dv_cq cq_info;
168         struct mlx5dv_obj obj;
169         int ret = 0;
170
171         obj.cq.in = ibcq;
172         obj.cq.out = &cq_info;
173         obj.qp.in = tmpl->qp;
174         obj.qp.out = &qp;
175         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
176         if (ret != 0) {
177                 return -EINVAL;
178         }
179         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
180                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
181                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
182                 return EINVAL;
183         }
184         tmpl->txq.cqe_n = log2above(cq_info.cqe_cnt);
185         tmpl->txq.qp_num_8s = tmpl->qp->qp_num << 8;
186         tmpl->txq.wqes = qp.sq.buf;
187         tmpl->txq.wqe_n = log2above(qp.sq.wqe_cnt);
188         tmpl->txq.qp_db = &qp.dbrec[MLX5_SND_DBR];
189         tmpl->txq.bf_reg = qp.bf.reg;
190         tmpl->txq.cq_db = cq_info.dbrec;
191         tmpl->txq.cqes =
192                 (volatile struct mlx5_cqe (*)[])
193                 (uintptr_t)cq_info.buf;
194         tmpl->txq.elts =
195                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])
196                 ((uintptr_t)txq_ctrl + sizeof(*txq_ctrl));
197         return 0;
198 }
199
200 /**
201  * Configure a TX queue.
202  *
203  * @param dev
204  *   Pointer to Ethernet device structure.
205  * @param txq_ctrl
206  *   Pointer to TX queue structure.
207  * @param desc
208  *   Number of descriptors to configure in queue.
209  * @param socket
210  *   NUMA socket on which memory must be allocated.
211  * @param[in] conf
212  *   Thresholds parameters.
213  *
214  * @return
215  *   0 on success, errno value on failure.
216  */
217 int
218 txq_ctrl_setup(struct rte_eth_dev *dev, struct txq_ctrl *txq_ctrl,
219                uint16_t desc, unsigned int socket,
220                const struct rte_eth_txconf *conf)
221 {
222         struct priv *priv = mlx5_get_priv(dev);
223         struct txq_ctrl tmpl = {
224                 .priv = priv,
225                 .socket = socket,
226         };
227         union {
228                 struct ibv_qp_init_attr_ex init;
229                 struct ibv_cq_init_attr_ex cq;
230                 struct ibv_qp_attr mod;
231                 struct ibv_cq_ex cq_attr;
232         } attr;
233         unsigned int cqe_n;
234         const unsigned int max_tso_inline = ((MLX5_MAX_TSO_HEADER +
235                                              (RTE_CACHE_LINE_SIZE - 1)) /
236                                               RTE_CACHE_LINE_SIZE);
237         int ret = 0;
238
239         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
240                 ret = ENOTSUP;
241                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
242                 goto error;
243         }
244         tmpl.txq.flags = conf->txq_flags;
245         assert(desc > MLX5_TX_COMP_THRESH);
246         tmpl.txq.elts_n = log2above(desc);
247         if (priv->mps == MLX5_MPW_ENHANCED)
248                 tmpl.txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
249         /* MRs will be registered in mp2mr[] later. */
250         attr.cq = (struct ibv_cq_init_attr_ex){
251                 .comp_mask = 0,
252         };
253         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
254                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
255         if (priv->mps == MLX5_MPW_ENHANCED)
256                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
257         tmpl.cq = ibv_create_cq(priv->ctx,
258                                 cqe_n,
259                                 NULL, NULL, 0);
260         if (tmpl.cq == NULL) {
261                 ret = ENOMEM;
262                 ERROR("%p: CQ creation failure: %s",
263                       (void *)dev, strerror(ret));
264                 goto error;
265         }
266         DEBUG("priv->device_attr.max_qp_wr is %d",
267               priv->device_attr.orig_attr.max_qp_wr);
268         DEBUG("priv->device_attr.max_sge is %d",
269               priv->device_attr.orig_attr.max_sge);
270         attr.init = (struct ibv_qp_init_attr_ex){
271                 /* CQ to be associated with the send queue. */
272                 .send_cq = tmpl.cq,
273                 /* CQ to be associated with the receive queue. */
274                 .recv_cq = tmpl.cq,
275                 .cap = {
276                         /* Max number of outstanding WRs. */
277                         .max_send_wr =
278                          ((priv->device_attr.orig_attr.max_qp_wr < desc) ?
279                            priv->device_attr.orig_attr.max_qp_wr :
280                            desc),
281                         /*
282                          * Max number of scatter/gather elements in a WR,
283                          * must be 1 to prevent libmlx5 from trying to affect
284                          * too much memory. TX gather is not impacted by the
285                          * priv->device_attr.max_sge limit and will still work
286                          * properly.
287                          */
288                         .max_send_sge = 1,
289                 },
290                 .qp_type = IBV_QPT_RAW_PACKET,
291                 /* Do *NOT* enable this, completions events are managed per
292                  * TX burst. */
293                 .sq_sig_all = 0,
294                 .pd = priv->pd,
295                 .comp_mask = IBV_QP_INIT_ATTR_PD,
296         };
297         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
298                 unsigned int ds_cnt;
299
300                 tmpl.txq.max_inline =
301                         ((priv->txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
302                          RTE_CACHE_LINE_SIZE);
303                 tmpl.txq.inline_en = 1;
304                 /* TSO and MPS can't be enabled concurrently. */
305                 assert(!priv->tso || !priv->mps);
306                 if (priv->mps == MLX5_MPW_ENHANCED) {
307                         tmpl.txq.inline_max_packet_sz =
308                                 priv->inline_max_packet_sz;
309                         /* To minimize the size of data set, avoid requesting
310                          * too large WQ.
311                          */
312                         attr.init.cap.max_inline_data =
313                                 ((RTE_MIN(priv->txq_inline,
314                                           priv->inline_max_packet_sz) +
315                                   (RTE_CACHE_LINE_SIZE - 1)) /
316                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
317                 } else if (priv->tso) {
318                         int inline_diff = tmpl.txq.max_inline - max_tso_inline;
319
320                         /*
321                          * Adjust inline value as Verbs aggregates
322                          * tso_inline and txq_inline fields.
323                          */
324                         attr.init.cap.max_inline_data = inline_diff > 0 ?
325                                                         inline_diff *
326                                                         RTE_CACHE_LINE_SIZE :
327                                                         0;
328                 } else {
329                         attr.init.cap.max_inline_data =
330                                 tmpl.txq.max_inline * RTE_CACHE_LINE_SIZE;
331                 }
332                 /*
333                  * Check if the inline size is too large in a way which
334                  * can make the WQE DS to overflow.
335                  * Considering in calculation:
336                  *      WQE CTRL (1 DS)
337                  *      WQE ETH  (1 DS)
338                  *      Inline part (N DS)
339                  */
340                 ds_cnt = 2 +
341                         (attr.init.cap.max_inline_data / MLX5_WQE_DWORD_SIZE);
342                 if (ds_cnt > MLX5_DSEG_MAX) {
343                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
344                                                    MLX5_WQE_DWORD_SIZE;
345
346                         max_inline = max_inline - (max_inline %
347                                                    RTE_CACHE_LINE_SIZE);
348                         WARN("txq inline is too large (%d) setting it to "
349                              "the maximum possible: %d\n",
350                              priv->txq_inline, max_inline);
351                         tmpl.txq.max_inline = max_inline / RTE_CACHE_LINE_SIZE;
352                         attr.init.cap.max_inline_data = max_inline;
353                 }
354         }
355         if (priv->tso) {
356                 attr.init.max_tso_header =
357                         max_tso_inline * RTE_CACHE_LINE_SIZE;
358                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
359                 tmpl.txq.max_inline = RTE_MAX(tmpl.txq.max_inline,
360                                               max_tso_inline);
361                 tmpl.txq.tso_en = 1;
362         }
363         if (priv->tunnel_en)
364                 tmpl.txq.tunnel_en = 1;
365         tmpl.qp = ibv_create_qp_ex(priv->ctx, &attr.init);
366         if (tmpl.qp == NULL) {
367                 ret = (errno ? errno : EINVAL);
368                 ERROR("%p: QP creation failure: %s",
369                       (void *)dev, strerror(ret));
370                 goto error;
371         }
372         DEBUG("TX queue capabilities: max_send_wr=%u, max_send_sge=%u,"
373               " max_inline_data=%u",
374               attr.init.cap.max_send_wr,
375               attr.init.cap.max_send_sge,
376               attr.init.cap.max_inline_data);
377         attr.mod = (struct ibv_qp_attr){
378                 /* Move the QP to this state. */
379                 .qp_state = IBV_QPS_INIT,
380                 /* Primary port number. */
381                 .port_num = priv->port
382         };
383         ret = ibv_modify_qp(tmpl.qp, &attr.mod,
384                             (IBV_QP_STATE | IBV_QP_PORT));
385         if (ret) {
386                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
387                       (void *)dev, strerror(ret));
388                 goto error;
389         }
390         ret = txq_setup(&tmpl, txq_ctrl);
391         if (ret) {
392                 ERROR("%p: cannot initialize TX queue structure: %s",
393                       (void *)dev, strerror(ret));
394                 goto error;
395         }
396         txq_alloc_elts(&tmpl, desc);
397         attr.mod = (struct ibv_qp_attr){
398                 .qp_state = IBV_QPS_RTR
399         };
400         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
401         if (ret) {
402                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
403                       (void *)dev, strerror(ret));
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: %s",
410                       (void *)dev, strerror(ret));
411                 goto error;
412         }
413         /* Clean up txq in case we're reinitializing it. */
414         DEBUG("%p: cleaning-up old txq just in case", (void *)txq_ctrl);
415         txq_cleanup(txq_ctrl);
416         *txq_ctrl = tmpl;
417         DEBUG("%p: txq updated with %p", (void *)txq_ctrl, (void *)&tmpl);
418         /* Pre-register known mempools. */
419         rte_mempool_walk(txq_mp2mr_iter, txq_ctrl);
420         assert(ret == 0);
421         return 0;
422 error:
423         txq_cleanup(&tmpl);
424         assert(ret > 0);
425         return ret;
426 }
427
428 /**
429  * DPDK callback to configure a TX queue.
430  *
431  * @param dev
432  *   Pointer to Ethernet device structure.
433  * @param idx
434  *   TX queue index.
435  * @param desc
436  *   Number of descriptors to configure in queue.
437  * @param socket
438  *   NUMA socket on which memory must be allocated.
439  * @param[in] conf
440  *   Thresholds parameters.
441  *
442  * @return
443  *   0 on success, negative errno value on failure.
444  */
445 int
446 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
447                     unsigned int socket, const struct rte_eth_txconf *conf)
448 {
449         struct priv *priv = dev->data->dev_private;
450         struct txq *txq = (*priv->txqs)[idx];
451         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
452         int ret;
453
454         if (mlx5_is_secondary())
455                 return -E_RTE_SECONDARY;
456
457         priv_lock(priv);
458         if (desc <= MLX5_TX_COMP_THRESH) {
459                 WARN("%p: number of descriptors requested for TX queue %u"
460                      " must be higher than MLX5_TX_COMP_THRESH, using"
461                      " %u instead of %u",
462                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
463                 desc = MLX5_TX_COMP_THRESH + 1;
464         }
465         if (!rte_is_power_of_2(desc)) {
466                 desc = 1 << log2above(desc);
467                 WARN("%p: increased number of descriptors in TX queue %u"
468                      " to the next power of two (%d)",
469                      (void *)dev, idx, desc);
470         }
471         DEBUG("%p: configuring queue %u for %u descriptors",
472               (void *)dev, idx, desc);
473         if (idx >= priv->txqs_n) {
474                 ERROR("%p: queue index out of range (%u >= %u)",
475                       (void *)dev, idx, priv->txqs_n);
476                 priv_unlock(priv);
477                 return -EOVERFLOW;
478         }
479         if (txq != NULL) {
480                 DEBUG("%p: reusing already allocated queue index %u (%p)",
481                       (void *)dev, idx, (void *)txq);
482                 if (priv->started) {
483                         priv_unlock(priv);
484                         return -EEXIST;
485                 }
486                 (*priv->txqs)[idx] = NULL;
487                 txq_cleanup(txq_ctrl);
488                 /* Resize if txq size is changed. */
489                 if (txq_ctrl->txq.elts_n != log2above(desc)) {
490                         txq_ctrl = rte_realloc(txq_ctrl,
491                                                sizeof(*txq_ctrl) +
492                                                desc * sizeof(struct rte_mbuf *),
493                                                RTE_CACHE_LINE_SIZE);
494                         if (!txq_ctrl) {
495                                 ERROR("%p: unable to reallocate queue index %u",
496                                         (void *)dev, idx);
497                                 priv_unlock(priv);
498                                 return -ENOMEM;
499                         }
500                 }
501         } else {
502                 txq_ctrl =
503                         rte_calloc_socket("TXQ", 1,
504                                           sizeof(*txq_ctrl) +
505                                           desc * sizeof(struct rte_mbuf *),
506                                           0, socket);
507                 if (txq_ctrl == NULL) {
508                         ERROR("%p: unable to allocate queue index %u",
509                               (void *)dev, idx);
510                         priv_unlock(priv);
511                         return -ENOMEM;
512                 }
513         }
514         ret = txq_ctrl_setup(dev, txq_ctrl, desc, socket, conf);
515         if (ret)
516                 rte_free(txq_ctrl);
517         else {
518                 txq_ctrl->txq.stats.idx = idx;
519                 DEBUG("%p: adding TX queue %p to list",
520                       (void *)dev, (void *)txq_ctrl);
521                 (*priv->txqs)[idx] = &txq_ctrl->txq;
522         }
523         priv_unlock(priv);
524         return -ret;
525 }
526
527 /**
528  * DPDK callback to release a TX queue.
529  *
530  * @param dpdk_txq
531  *   Generic TX queue pointer.
532  */
533 void
534 mlx5_tx_queue_release(void *dpdk_txq)
535 {
536         struct txq *txq = (struct txq *)dpdk_txq;
537         struct txq_ctrl *txq_ctrl;
538         struct priv *priv;
539         unsigned int i;
540
541         if (mlx5_is_secondary())
542                 return;
543
544         if (txq == NULL)
545                 return;
546         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
547         priv = txq_ctrl->priv;
548         priv_lock(priv);
549         for (i = 0; (i != priv->txqs_n); ++i)
550                 if ((*priv->txqs)[i] == txq) {
551                         DEBUG("%p: removing TX queue %p from list",
552                               (void *)priv->dev, (void *)txq_ctrl);
553                         (*priv->txqs)[i] = NULL;
554                         break;
555                 }
556         txq_cleanup(txq_ctrl);
557         rte_free(txq_ctrl);
558         priv_unlock(priv);
559 }