net/mlx5: remove pedantic pragma
[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 mlx5_qp *qp = to_mqp(tmpl->qp);
166         struct ibv_cq *ibcq = tmpl->cq;
167         struct ibv_mlx5_cq_info cq_info;
168
169         if (ibv_mlx5_exp_get_cq_info(ibcq, &cq_info)) {
170                 ERROR("Unable to query CQ info. check your OFED.");
171                 return ENOTSUP;
172         }
173         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
174                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
175                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
176                 return EINVAL;
177         }
178         tmpl->txq.cqe_n = log2above(cq_info.cqe_cnt);
179         tmpl->txq.qp_num_8s = qp->ctrl_seg.qp_num << 8;
180         tmpl->txq.wqes = qp->gen_data.sqstart;
181         tmpl->txq.wqe_n = log2above(qp->sq.wqe_cnt);
182         tmpl->txq.qp_db = &qp->gen_data.db[MLX5_SND_DBR];
183         tmpl->txq.bf_reg = qp->gen_data.bf->reg;
184         tmpl->txq.cq_db = cq_info.dbrec;
185         tmpl->txq.cqes =
186                 (volatile struct mlx5_cqe (*)[])
187                 (uintptr_t)cq_info.buf;
188         tmpl->txq.elts =
189                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])
190                 ((uintptr_t)txq_ctrl + sizeof(*txq_ctrl));
191         return 0;
192 }
193
194 /**
195  * Configure a TX queue.
196  *
197  * @param dev
198  *   Pointer to Ethernet device structure.
199  * @param txq_ctrl
200  *   Pointer to TX queue structure.
201  * @param desc
202  *   Number of descriptors to configure in queue.
203  * @param socket
204  *   NUMA socket on which memory must be allocated.
205  * @param[in] conf
206  *   Thresholds parameters.
207  *
208  * @return
209  *   0 on success, errno value on failure.
210  */
211 int
212 txq_ctrl_setup(struct rte_eth_dev *dev, struct txq_ctrl *txq_ctrl,
213                uint16_t desc, unsigned int socket,
214                const struct rte_eth_txconf *conf)
215 {
216         struct priv *priv = mlx5_get_priv(dev);
217         struct txq_ctrl tmpl = {
218                 .priv = priv,
219                 .socket = socket,
220         };
221         union {
222                 struct ibv_exp_qp_init_attr init;
223                 struct ibv_exp_cq_init_attr cq;
224                 struct ibv_exp_qp_attr mod;
225                 struct ibv_exp_cq_attr cq_attr;
226         } attr;
227         unsigned int cqe_n;
228         const unsigned int max_tso_inline = ((MLX5_MAX_TSO_HEADER +
229                                              (RTE_CACHE_LINE_SIZE - 1)) /
230                                               RTE_CACHE_LINE_SIZE);
231         int ret = 0;
232
233         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
234                 ret = ENOTSUP;
235                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
236                 goto error;
237         }
238         tmpl.txq.flags = conf->txq_flags;
239         assert(desc > MLX5_TX_COMP_THRESH);
240         tmpl.txq.elts_n = log2above(desc);
241         if (priv->mps == MLX5_MPW_ENHANCED)
242                 tmpl.txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
243         /* MRs will be registered in mp2mr[] later. */
244         attr.cq = (struct ibv_exp_cq_init_attr){
245                 .comp_mask = 0,
246         };
247         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
248                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
249         if (priv->mps == MLX5_MPW_ENHANCED)
250                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
251         tmpl.cq = ibv_exp_create_cq(priv->ctx,
252                                     cqe_n,
253                                     NULL, NULL, 0, &attr.cq);
254         if (tmpl.cq == NULL) {
255                 ret = ENOMEM;
256                 ERROR("%p: CQ creation failure: %s",
257                       (void *)dev, strerror(ret));
258                 goto error;
259         }
260         DEBUG("priv->device_attr.max_qp_wr is %d",
261               priv->device_attr.max_qp_wr);
262         DEBUG("priv->device_attr.max_sge is %d",
263               priv->device_attr.max_sge);
264         attr.init = (struct ibv_exp_qp_init_attr){
265                 /* CQ to be associated with the send queue. */
266                 .send_cq = tmpl.cq,
267                 /* CQ to be associated with the receive queue. */
268                 .recv_cq = tmpl.cq,
269                 .cap = {
270                         /* Max number of outstanding WRs. */
271                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
272                                         priv->device_attr.max_qp_wr :
273                                         desc),
274                         /*
275                          * Max number of scatter/gather elements in a WR,
276                          * must be 1 to prevent libmlx5 from trying to affect
277                          * too much memory. TX gather is not impacted by the
278                          * priv->device_attr.max_sge limit and will still work
279                          * properly.
280                          */
281                         .max_send_sge = 1,
282                 },
283                 .qp_type = IBV_QPT_RAW_PACKET,
284                 /* Do *NOT* enable this, completions events are managed per
285                  * TX burst. */
286                 .sq_sig_all = 0,
287                 .pd = priv->pd,
288                 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
289         };
290         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
291                 tmpl.txq.max_inline =
292                         ((priv->txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
293                          RTE_CACHE_LINE_SIZE);
294                 tmpl.txq.inline_en = 1;
295                 /* TSO and MPS can't be enabled concurrently. */
296                 assert(!priv->tso || !priv->mps);
297                 if (priv->mps == MLX5_MPW_ENHANCED) {
298                         tmpl.txq.inline_max_packet_sz =
299                                 priv->inline_max_packet_sz;
300                         /* To minimize the size of data set, avoid requesting
301                          * too large WQ.
302                          */
303                         attr.init.cap.max_inline_data =
304                                 ((RTE_MIN(priv->txq_inline,
305                                           priv->inline_max_packet_sz) +
306                                   (RTE_CACHE_LINE_SIZE - 1)) /
307                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
308                 } else if (priv->tso) {
309                         int inline_diff = tmpl.txq.max_inline - max_tso_inline;
310
311                         /*
312                          * Adjust inline value as Verbs aggregates
313                          * tso_inline and txq_inline fields.
314                          */
315                         attr.init.cap.max_inline_data = inline_diff > 0 ?
316                                                         inline_diff *
317                                                         RTE_CACHE_LINE_SIZE :
318                                                         0;
319                 } else {
320                         attr.init.cap.max_inline_data =
321                                 tmpl.txq.max_inline * RTE_CACHE_LINE_SIZE;
322                 }
323         }
324         if (priv->tso) {
325                 attr.init.max_tso_header =
326                         max_tso_inline * RTE_CACHE_LINE_SIZE;
327                 attr.init.comp_mask |= IBV_EXP_QP_INIT_ATTR_MAX_TSO_HEADER;
328                 tmpl.txq.max_inline = RTE_MAX(tmpl.txq.max_inline,
329                                               max_tso_inline);
330                 tmpl.txq.tso_en = 1;
331         }
332         if (priv->tunnel_en)
333                 tmpl.txq.tunnel_en = 1;
334         tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
335         if (tmpl.qp == NULL) {
336                 ret = (errno ? errno : EINVAL);
337                 ERROR("%p: QP creation failure: %s",
338                       (void *)dev, strerror(ret));
339                 goto error;
340         }
341         DEBUG("TX queue capabilities: max_send_wr=%u, max_send_sge=%u,"
342               " max_inline_data=%u",
343               attr.init.cap.max_send_wr,
344               attr.init.cap.max_send_sge,
345               attr.init.cap.max_inline_data);
346         attr.mod = (struct ibv_exp_qp_attr){
347                 /* Move the QP to this state. */
348                 .qp_state = IBV_QPS_INIT,
349                 /* Primary port number. */
350                 .port_num = priv->port
351         };
352         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
353                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
354         if (ret) {
355                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
356                       (void *)dev, strerror(ret));
357                 goto error;
358         }
359         ret = txq_setup(&tmpl, txq_ctrl);
360         if (ret) {
361                 ERROR("%p: cannot initialize TX queue structure: %s",
362                       (void *)dev, strerror(ret));
363                 goto error;
364         }
365         txq_alloc_elts(&tmpl, desc);
366         attr.mod = (struct ibv_exp_qp_attr){
367                 .qp_state = IBV_QPS_RTR
368         };
369         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
370         if (ret) {
371                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
372                       (void *)dev, strerror(ret));
373                 goto error;
374         }
375         attr.mod.qp_state = IBV_QPS_RTS;
376         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
377         if (ret) {
378                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
379                       (void *)dev, strerror(ret));
380                 goto error;
381         }
382         /* Clean up txq in case we're reinitializing it. */
383         DEBUG("%p: cleaning-up old txq just in case", (void *)txq_ctrl);
384         txq_cleanup(txq_ctrl);
385         *txq_ctrl = tmpl;
386         DEBUG("%p: txq updated with %p", (void *)txq_ctrl, (void *)&tmpl);
387         /* Pre-register known mempools. */
388         rte_mempool_walk(txq_mp2mr_iter, txq_ctrl);
389         assert(ret == 0);
390         return 0;
391 error:
392         txq_cleanup(&tmpl);
393         assert(ret > 0);
394         return ret;
395 }
396
397 /**
398  * DPDK callback to configure a TX queue.
399  *
400  * @param dev
401  *   Pointer to Ethernet device structure.
402  * @param idx
403  *   TX queue index.
404  * @param desc
405  *   Number of descriptors to configure in queue.
406  * @param socket
407  *   NUMA socket on which memory must be allocated.
408  * @param[in] conf
409  *   Thresholds parameters.
410  *
411  * @return
412  *   0 on success, negative errno value on failure.
413  */
414 int
415 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
416                     unsigned int socket, const struct rte_eth_txconf *conf)
417 {
418         struct priv *priv = dev->data->dev_private;
419         struct txq *txq = (*priv->txqs)[idx];
420         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
421         int ret;
422
423         if (mlx5_is_secondary())
424                 return -E_RTE_SECONDARY;
425
426         priv_lock(priv);
427         if (desc <= MLX5_TX_COMP_THRESH) {
428                 WARN("%p: number of descriptors requested for TX queue %u"
429                      " must be higher than MLX5_TX_COMP_THRESH, using"
430                      " %u instead of %u",
431                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
432                 desc = MLX5_TX_COMP_THRESH + 1;
433         }
434         if (!rte_is_power_of_2(desc)) {
435                 desc = 1 << log2above(desc);
436                 WARN("%p: increased number of descriptors in TX queue %u"
437                      " to the next power of two (%d)",
438                      (void *)dev, idx, desc);
439         }
440         DEBUG("%p: configuring queue %u for %u descriptors",
441               (void *)dev, idx, desc);
442         if (idx >= priv->txqs_n) {
443                 ERROR("%p: queue index out of range (%u >= %u)",
444                       (void *)dev, idx, priv->txqs_n);
445                 priv_unlock(priv);
446                 return -EOVERFLOW;
447         }
448         if (txq != NULL) {
449                 DEBUG("%p: reusing already allocated queue index %u (%p)",
450                       (void *)dev, idx, (void *)txq);
451                 if (priv->started) {
452                         priv_unlock(priv);
453                         return -EEXIST;
454                 }
455                 (*priv->txqs)[idx] = NULL;
456                 txq_cleanup(txq_ctrl);
457                 /* Resize if txq size is changed. */
458                 if (txq_ctrl->txq.elts_n != log2above(desc)) {
459                         txq_ctrl = rte_realloc(txq_ctrl,
460                                                sizeof(*txq_ctrl) +
461                                                desc * sizeof(struct rte_mbuf *),
462                                                RTE_CACHE_LINE_SIZE);
463                         if (!txq_ctrl) {
464                                 ERROR("%p: unable to reallocate queue index %u",
465                                         (void *)dev, idx);
466                                 priv_unlock(priv);
467                                 return -ENOMEM;
468                         }
469                 }
470         } else {
471                 txq_ctrl =
472                         rte_calloc_socket("TXQ", 1,
473                                           sizeof(*txq_ctrl) +
474                                           desc * sizeof(struct rte_mbuf *),
475                                           0, socket);
476                 if (txq_ctrl == NULL) {
477                         ERROR("%p: unable to allocate queue index %u",
478                               (void *)dev, idx);
479                         priv_unlock(priv);
480                         return -ENOMEM;
481                 }
482         }
483         ret = txq_ctrl_setup(dev, txq_ctrl, desc, socket, conf);
484         if (ret)
485                 rte_free(txq_ctrl);
486         else {
487                 txq_ctrl->txq.stats.idx = idx;
488                 DEBUG("%p: adding TX queue %p to list",
489                       (void *)dev, (void *)txq_ctrl);
490                 (*priv->txqs)[idx] = &txq_ctrl->txq;
491         }
492         priv_unlock(priv);
493         return -ret;
494 }
495
496 /**
497  * DPDK callback to release a TX queue.
498  *
499  * @param dpdk_txq
500  *   Generic TX queue pointer.
501  */
502 void
503 mlx5_tx_queue_release(void *dpdk_txq)
504 {
505         struct txq *txq = (struct txq *)dpdk_txq;
506         struct txq_ctrl *txq_ctrl;
507         struct priv *priv;
508         unsigned int i;
509
510         if (mlx5_is_secondary())
511                 return;
512
513         if (txq == NULL)
514                 return;
515         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
516         priv = txq_ctrl->priv;
517         priv_lock(priv);
518         for (i = 0; (i != priv->txqs_n); ++i)
519                 if ((*priv->txqs)[i] == txq) {
520                         DEBUG("%p: removing TX queue %p from list",
521                               (void *)priv->dev, (void *)txq_ctrl);
522                         (*priv->txqs)[i] = NULL;
523                         break;
524                 }
525         txq_cleanup(txq_ctrl);
526         rte_free(txq_ctrl);
527         priv_unlock(priv);
528 }
529
530 /**
531  * DPDK callback for TX in secondary processes.
532  *
533  * This function configures all queues from primary process information
534  * if necessary before reverting to the normal TX burst callback.
535  *
536  * @param dpdk_txq
537  *   Generic pointer to TX queue structure.
538  * @param[in] pkts
539  *   Packets to transmit.
540  * @param pkts_n
541  *   Number of packets in array.
542  *
543  * @return
544  *   Number of packets successfully transmitted (<= pkts_n).
545  */
546 uint16_t
547 mlx5_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
548                               uint16_t pkts_n)
549 {
550         struct txq *txq = dpdk_txq;
551         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
552         struct priv *priv = mlx5_secondary_data_setup(txq_ctrl->priv);
553         struct priv *primary_priv;
554         unsigned int index;
555
556         if (priv == NULL)
557                 return 0;
558         primary_priv =
559                 mlx5_secondary_data[priv->dev->data->port_id].primary_priv;
560         /* Look for queue index in both private structures. */
561         for (index = 0; index != priv->txqs_n; ++index)
562                 if (((*primary_priv->txqs)[index] == txq) ||
563                     ((*priv->txqs)[index] == txq))
564                         break;
565         if (index == priv->txqs_n)
566                 return 0;
567         txq = (*priv->txqs)[index];
568         return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
569 }