New upstream version 17.05.1
[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
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 /* DPDK headers don't like -pedantic. */
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic ignored "-Wpedantic"
53 #endif
54 #include <rte_mbuf.h>
55 #include <rte_malloc.h>
56 #include <rte_ethdev.h>
57 #include <rte_common.h>
58 #ifdef PEDANTIC
59 #pragma GCC diagnostic error "-Wpedantic"
60 #endif
61
62 #include "mlx5_utils.h"
63 #include "mlx5_defs.h"
64 #include "mlx5.h"
65 #include "mlx5_rxtx.h"
66 #include "mlx5_autoconf.h"
67 #include "mlx5_defs.h"
68
69 /**
70  * Allocate TX queue elements.
71  *
72  * @param txq_ctrl
73  *   Pointer to TX queue structure.
74  * @param elts_n
75  *   Number of elements to allocate.
76  */
77 static void
78 txq_alloc_elts(struct txq_ctrl *txq_ctrl, unsigned int elts_n)
79 {
80         unsigned int i;
81
82         for (i = 0; (i != elts_n); ++i)
83                 (*txq_ctrl->txq.elts)[i] = NULL;
84         for (i = 0; (i != (1u << txq_ctrl->txq.wqe_n)); ++i) {
85                 volatile struct mlx5_wqe64 *wqe =
86                         (volatile struct mlx5_wqe64 *)
87                         txq_ctrl->txq.wqes + i;
88
89                 memset((void *)(uintptr_t)wqe, 0x0, sizeof(*wqe));
90         }
91         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
92         txq_ctrl->txq.elts_head = 0;
93         txq_ctrl->txq.elts_tail = 0;
94         txq_ctrl->txq.elts_comp = 0;
95 }
96
97 /**
98  * Free TX queue elements.
99  *
100  * @param txq_ctrl
101  *   Pointer to TX queue structure.
102  */
103 static void
104 txq_free_elts(struct txq_ctrl *txq_ctrl)
105 {
106         unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
107         unsigned int elts_head = txq_ctrl->txq.elts_head;
108         unsigned int elts_tail = txq_ctrl->txq.elts_tail;
109         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
110
111         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
112         txq_ctrl->txq.elts_head = 0;
113         txq_ctrl->txq.elts_tail = 0;
114         txq_ctrl->txq.elts_comp = 0;
115
116         while (elts_tail != elts_head) {
117                 struct rte_mbuf *elt = (*elts)[elts_tail];
118
119                 assert(elt != NULL);
120                 rte_pktmbuf_free_seg(elt);
121 #ifndef NDEBUG
122                 /* Poisoning. */
123                 memset(&(*elts)[elts_tail],
124                        0x77,
125                        sizeof((*elts)[elts_tail]));
126 #endif
127                 if (++elts_tail == elts_n)
128                         elts_tail = 0;
129         }
130 }
131
132 /**
133  * Clean up a TX queue.
134  *
135  * Destroy objects, free allocated memory and reset the structure for reuse.
136  *
137  * @param txq_ctrl
138  *   Pointer to TX queue structure.
139  */
140 void
141 txq_cleanup(struct txq_ctrl *txq_ctrl)
142 {
143         size_t i;
144
145         DEBUG("cleaning up %p", (void *)txq_ctrl);
146         txq_free_elts(txq_ctrl);
147         if (txq_ctrl->qp != NULL)
148                 claim_zero(ibv_destroy_qp(txq_ctrl->qp));
149         if (txq_ctrl->cq != NULL)
150                 claim_zero(ibv_destroy_cq(txq_ctrl->cq));
151         for (i = 0; (i != RTE_DIM(txq_ctrl->txq.mp2mr)); ++i) {
152                 if (txq_ctrl->txq.mp2mr[i].mp == NULL)
153                         break;
154                 assert(txq_ctrl->txq.mp2mr[i].mr != NULL);
155                 claim_zero(ibv_dereg_mr(txq_ctrl->txq.mp2mr[i].mr));
156         }
157         memset(txq_ctrl, 0, sizeof(*txq_ctrl));
158 }
159
160 /**
161  * Initialize TX queue.
162  *
163  * @param tmpl
164  *   Pointer to TX queue control template.
165  * @param txq_ctrl
166  *   Pointer to TX queue control.
167  *
168  * @return
169  *   0 on success, errno value on failure.
170  */
171 static inline int
172 txq_setup(struct txq_ctrl *tmpl, struct txq_ctrl *txq_ctrl)
173 {
174         struct mlx5_qp *qp = to_mqp(tmpl->qp);
175         struct ibv_cq *ibcq = tmpl->cq;
176         struct ibv_mlx5_cq_info cq_info;
177
178         if (ibv_mlx5_exp_get_cq_info(ibcq, &cq_info)) {
179                 ERROR("Unable to query CQ info. check your OFED.");
180                 return ENOTSUP;
181         }
182         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
183                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
184                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
185                 return EINVAL;
186         }
187         tmpl->txq.cqe_n = log2above(cq_info.cqe_cnt);
188         tmpl->txq.qp_num_8s = qp->ctrl_seg.qp_num << 8;
189         tmpl->txq.wqes = qp->gen_data.sqstart;
190         tmpl->txq.wqe_n = log2above(qp->sq.wqe_cnt);
191         tmpl->txq.qp_db = &qp->gen_data.db[MLX5_SND_DBR];
192         tmpl->txq.bf_reg = qp->gen_data.bf->reg;
193         tmpl->txq.cq_db = cq_info.dbrec;
194         tmpl->txq.cqes =
195                 (volatile struct mlx5_cqe (*)[])
196                 (uintptr_t)cq_info.buf;
197         tmpl->txq.elts =
198                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])
199                 ((uintptr_t)txq_ctrl + sizeof(*txq_ctrl));
200         return 0;
201 }
202
203 /**
204  * Configure a TX queue.
205  *
206  * @param dev
207  *   Pointer to Ethernet device structure.
208  * @param txq_ctrl
209  *   Pointer to TX queue structure.
210  * @param desc
211  *   Number of descriptors to configure in queue.
212  * @param socket
213  *   NUMA socket on which memory must be allocated.
214  * @param[in] conf
215  *   Thresholds parameters.
216  *
217  * @return
218  *   0 on success, errno value on failure.
219  */
220 int
221 txq_ctrl_setup(struct rte_eth_dev *dev, struct txq_ctrl *txq_ctrl,
222                uint16_t desc, unsigned int socket,
223                const struct rte_eth_txconf *conf)
224 {
225         struct priv *priv = mlx5_get_priv(dev);
226         struct txq_ctrl tmpl = {
227                 .priv = priv,
228                 .socket = socket,
229         };
230         union {
231                 struct ibv_exp_qp_init_attr init;
232                 struct ibv_exp_cq_init_attr cq;
233                 struct ibv_exp_qp_attr mod;
234                 struct ibv_exp_cq_attr cq_attr;
235         } attr;
236         unsigned int cqe_n;
237         const unsigned int max_tso_inline = ((MLX5_MAX_TSO_HEADER +
238                                              (RTE_CACHE_LINE_SIZE - 1)) /
239                                               RTE_CACHE_LINE_SIZE);
240         int ret = 0;
241
242         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
243                 ret = ENOTSUP;
244                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
245                 goto error;
246         }
247         (void)conf; /* Thresholds configuration (ignored). */
248         assert(desc > MLX5_TX_COMP_THRESH);
249         tmpl.txq.elts_n = log2above(desc);
250         if (priv->mps == MLX5_MPW_ENHANCED)
251                 tmpl.txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
252         /* MRs will be registered in mp2mr[] later. */
253         attr.cq = (struct ibv_exp_cq_init_attr){
254                 .comp_mask = 0,
255         };
256         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
257                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
258         if (priv->mps == MLX5_MPW_ENHANCED)
259                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
260         tmpl.cq = ibv_exp_create_cq(priv->ctx,
261                                     cqe_n,
262                                     NULL, NULL, 0, &attr.cq);
263         if (tmpl.cq == NULL) {
264                 ret = ENOMEM;
265                 ERROR("%p: CQ creation failure: %s",
266                       (void *)dev, strerror(ret));
267                 goto error;
268         }
269         DEBUG("priv->device_attr.max_qp_wr is %d",
270               priv->device_attr.max_qp_wr);
271         DEBUG("priv->device_attr.max_sge is %d",
272               priv->device_attr.max_sge);
273         attr.init = (struct ibv_exp_qp_init_attr){
274                 /* CQ to be associated with the send queue. */
275                 .send_cq = tmpl.cq,
276                 /* CQ to be associated with the receive queue. */
277                 .recv_cq = tmpl.cq,
278                 .cap = {
279                         /* Max number of outstanding WRs. */
280                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
281                                         priv->device_attr.max_qp_wr :
282                                         desc),
283                         /*
284                          * Max number of scatter/gather elements in a WR,
285                          * must be 1 to prevent libmlx5 from trying to affect
286                          * too much memory. TX gather is not impacted by the
287                          * priv->device_attr.max_sge limit and will still work
288                          * properly.
289                          */
290                         .max_send_sge = 1,
291                 },
292                 .qp_type = IBV_QPT_RAW_PACKET,
293                 /* Do *NOT* enable this, completions events are managed per
294                  * TX burst. */
295                 .sq_sig_all = 0,
296                 .pd = priv->pd,
297                 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
298         };
299         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
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         if (priv->tso) {
334                 attr.init.max_tso_header =
335                         max_tso_inline * RTE_CACHE_LINE_SIZE;
336                 attr.init.comp_mask |= IBV_EXP_QP_INIT_ATTR_MAX_TSO_HEADER;
337                 tmpl.txq.max_inline = RTE_MAX(tmpl.txq.max_inline,
338                                               max_tso_inline);
339                 tmpl.txq.tso_en = 1;
340         }
341         if (priv->tunnel_en)
342                 tmpl.txq.tunnel_en = 1;
343         tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
344         if (tmpl.qp == NULL) {
345                 ret = (errno ? errno : EINVAL);
346                 ERROR("%p: QP creation failure: %s",
347                       (void *)dev, strerror(ret));
348                 goto error;
349         }
350         DEBUG("TX queue capabilities: max_send_wr=%u, max_send_sge=%u,"
351               " max_inline_data=%u",
352               attr.init.cap.max_send_wr,
353               attr.init.cap.max_send_sge,
354               attr.init.cap.max_inline_data);
355         attr.mod = (struct ibv_exp_qp_attr){
356                 /* Move the QP to this state. */
357                 .qp_state = IBV_QPS_INIT,
358                 /* Primary port number. */
359                 .port_num = priv->port
360         };
361         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
362                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
363         if (ret) {
364                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
365                       (void *)dev, strerror(ret));
366                 goto error;
367         }
368         ret = txq_setup(&tmpl, txq_ctrl);
369         if (ret) {
370                 ERROR("%p: cannot initialize TX queue structure: %s",
371                       (void *)dev, strerror(ret));
372                 goto error;
373         }
374         txq_alloc_elts(&tmpl, desc);
375         attr.mod = (struct ibv_exp_qp_attr){
376                 .qp_state = IBV_QPS_RTR
377         };
378         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
379         if (ret) {
380                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
381                       (void *)dev, strerror(ret));
382                 goto error;
383         }
384         attr.mod.qp_state = IBV_QPS_RTS;
385         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
386         if (ret) {
387                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
388                       (void *)dev, strerror(ret));
389                 goto error;
390         }
391         /* Clean up txq in case we're reinitializing it. */
392         DEBUG("%p: cleaning-up old txq just in case", (void *)txq_ctrl);
393         txq_cleanup(txq_ctrl);
394         *txq_ctrl = tmpl;
395         DEBUG("%p: txq updated with %p", (void *)txq_ctrl, (void *)&tmpl);
396         /* Pre-register known mempools. */
397         rte_mempool_walk(txq_mp2mr_iter, txq_ctrl);
398         assert(ret == 0);
399         return 0;
400 error:
401         txq_cleanup(&tmpl);
402         assert(ret > 0);
403         return ret;
404 }
405
406 /**
407  * DPDK callback to configure a TX queue.
408  *
409  * @param dev
410  *   Pointer to Ethernet device structure.
411  * @param idx
412  *   TX queue index.
413  * @param desc
414  *   Number of descriptors to configure in queue.
415  * @param socket
416  *   NUMA socket on which memory must be allocated.
417  * @param[in] conf
418  *   Thresholds parameters.
419  *
420  * @return
421  *   0 on success, negative errno value on failure.
422  */
423 int
424 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
425                     unsigned int socket, const struct rte_eth_txconf *conf)
426 {
427         struct priv *priv = dev->data->dev_private;
428         struct txq *txq = (*priv->txqs)[idx];
429         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
430         int ret;
431
432         if (mlx5_is_secondary())
433                 return -E_RTE_SECONDARY;
434
435         priv_lock(priv);
436         if (desc <= MLX5_TX_COMP_THRESH) {
437                 WARN("%p: number of descriptors requested for TX queue %u"
438                      " must be higher than MLX5_TX_COMP_THRESH, using"
439                      " %u instead of %u",
440                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
441                 desc = MLX5_TX_COMP_THRESH + 1;
442         }
443         if (!rte_is_power_of_2(desc)) {
444                 desc = 1 << log2above(desc);
445                 WARN("%p: increased number of descriptors in TX queue %u"
446                      " to the next power of two (%d)",
447                      (void *)dev, idx, desc);
448         }
449         DEBUG("%p: configuring queue %u for %u descriptors",
450               (void *)dev, idx, desc);
451         if (idx >= priv->txqs_n) {
452                 ERROR("%p: queue index out of range (%u >= %u)",
453                       (void *)dev, idx, priv->txqs_n);
454                 priv_unlock(priv);
455                 return -EOVERFLOW;
456         }
457         if (txq != NULL) {
458                 DEBUG("%p: reusing already allocated queue index %u (%p)",
459                       (void *)dev, idx, (void *)txq);
460                 if (priv->started) {
461                         priv_unlock(priv);
462                         return -EEXIST;
463                 }
464                 (*priv->txqs)[idx] = NULL;
465                 txq_cleanup(txq_ctrl);
466                 /* Resize if txq size is changed. */
467                 if (txq_ctrl->txq.elts_n != log2above(desc)) {
468                         txq_ctrl = rte_realloc(txq_ctrl,
469                                                sizeof(*txq_ctrl) +
470                                                desc * sizeof(struct rte_mbuf *),
471                                                RTE_CACHE_LINE_SIZE);
472                         if (!txq_ctrl) {
473                                 ERROR("%p: unable to reallocate queue index %u",
474                                         (void *)dev, idx);
475                                 priv_unlock(priv);
476                                 return -ENOMEM;
477                         }
478                 }
479         } else {
480                 txq_ctrl =
481                         rte_calloc_socket("TXQ", 1,
482                                           sizeof(*txq_ctrl) +
483                                           desc * sizeof(struct rte_mbuf *),
484                                           0, socket);
485                 if (txq_ctrl == NULL) {
486                         ERROR("%p: unable to allocate queue index %u",
487                               (void *)dev, idx);
488                         priv_unlock(priv);
489                         return -ENOMEM;
490                 }
491         }
492         ret = txq_ctrl_setup(dev, txq_ctrl, desc, socket, conf);
493         if (ret)
494                 rte_free(txq_ctrl);
495         else {
496                 txq_ctrl->txq.stats.idx = idx;
497                 DEBUG("%p: adding TX queue %p to list",
498                       (void *)dev, (void *)txq_ctrl);
499                 (*priv->txqs)[idx] = &txq_ctrl->txq;
500                 /* Update send callback. */
501                 priv_select_tx_function(priv);
502         }
503         priv_unlock(priv);
504         return -ret;
505 }
506
507 /**
508  * DPDK callback to release a TX queue.
509  *
510  * @param dpdk_txq
511  *   Generic TX queue pointer.
512  */
513 void
514 mlx5_tx_queue_release(void *dpdk_txq)
515 {
516         struct txq *txq = (struct txq *)dpdk_txq;
517         struct txq_ctrl *txq_ctrl;
518         struct priv *priv;
519         unsigned int i;
520
521         if (mlx5_is_secondary())
522                 return;
523
524         if (txq == NULL)
525                 return;
526         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
527         priv = txq_ctrl->priv;
528         priv_lock(priv);
529         for (i = 0; (i != priv->txqs_n); ++i)
530                 if ((*priv->txqs)[i] == txq) {
531                         DEBUG("%p: removing TX queue %p from list",
532                               (void *)priv->dev, (void *)txq_ctrl);
533                         (*priv->txqs)[i] = NULL;
534                         break;
535                 }
536         txq_cleanup(txq_ctrl);
537         rte_free(txq_ctrl);
538         priv_unlock(priv);
539 }
540
541 /**
542  * DPDK callback for TX in secondary processes.
543  *
544  * This function configures all queues from primary process information
545  * if necessary before reverting to the normal TX burst callback.
546  *
547  * @param dpdk_txq
548  *   Generic pointer to TX queue structure.
549  * @param[in] pkts
550  *   Packets to transmit.
551  * @param pkts_n
552  *   Number of packets in array.
553  *
554  * @return
555  *   Number of packets successfully transmitted (<= pkts_n).
556  */
557 uint16_t
558 mlx5_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
559                               uint16_t pkts_n)
560 {
561         struct txq *txq = dpdk_txq;
562         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
563         struct priv *priv = mlx5_secondary_data_setup(txq_ctrl->priv);
564         struct priv *primary_priv;
565         unsigned int index;
566
567         if (priv == NULL)
568                 return 0;
569         primary_priv =
570                 mlx5_secondary_data[priv->dev->data->port_id].primary_priv;
571         /* Look for queue index in both private structures. */
572         for (index = 0; index != priv->txqs_n; ++index)
573                 if (((*primary_priv->txqs)[index] == txq) ||
574                     ((*priv->txqs)[index] == txq))
575                         break;
576         if (index == priv->txqs_n)
577                 return 0;
578         txq = (*priv->txqs)[index];
579         return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
580 }