net/mlx5: refactor Tx data path
[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 "-pedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-pedantic"
48 #endif
49
50 /* DPDK headers don't like -pedantic. */
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic ignored "-pedantic"
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 "-pedantic"
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 != txq_ctrl->txq.wqe_n); ++i) {
85                 volatile union mlx5_wqe *wqe = &(*txq_ctrl->txq.wqes)[i];
86
87                 memset((void *)(uintptr_t)wqe, 0x0, sizeof(*wqe));
88         }
89         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
90         txq_ctrl->txq.elts_head = 0;
91         txq_ctrl->txq.elts_tail = 0;
92 }
93
94 /**
95  * Free TX queue elements.
96  *
97  * @param txq_ctrl
98  *   Pointer to TX queue structure.
99  */
100 static void
101 txq_free_elts(struct txq_ctrl *txq_ctrl)
102 {
103         unsigned int elts_n = txq_ctrl->txq.elts_n;
104         unsigned int elts_head = txq_ctrl->txq.elts_head;
105         unsigned int elts_tail = txq_ctrl->txq.elts_tail;
106         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
107
108         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
109         txq_ctrl->txq.elts_head = 0;
110         txq_ctrl->txq.elts_tail = 0;
111
112         while (elts_tail != elts_head) {
113                 struct rte_mbuf *elt = (*elts)[elts_tail];
114
115                 assert(elt != NULL);
116                 rte_pktmbuf_free(elt);
117 #ifndef NDEBUG
118                 /* Poisoning. */
119                 memset(&(*elts)[elts_tail],
120                        0x77,
121                        sizeof((*elts)[elts_tail]));
122 #endif
123                 if (++elts_tail == elts_n)
124                         elts_tail = 0;
125         }
126 }
127
128 /**
129  * Clean up a TX queue.
130  *
131  * Destroy objects, free allocated memory and reset the structure for reuse.
132  *
133  * @param txq_ctrl
134  *   Pointer to TX queue structure.
135  */
136 void
137 txq_cleanup(struct txq_ctrl *txq_ctrl)
138 {
139         struct ibv_exp_release_intf_params params;
140         size_t i;
141
142         DEBUG("cleaning up %p", (void *)txq_ctrl);
143         txq_free_elts(txq_ctrl);
144         if (txq_ctrl->if_qp != NULL) {
145                 assert(txq_ctrl->priv != NULL);
146                 assert(txq_ctrl->priv->ctx != NULL);
147                 assert(txq_ctrl->qp != NULL);
148                 params = (struct ibv_exp_release_intf_params){
149                         .comp_mask = 0,
150                 };
151                 claim_zero(ibv_exp_release_intf(txq_ctrl->priv->ctx,
152                                                 txq_ctrl->if_qp,
153                                                 &params));
154         }
155         if (txq_ctrl->if_cq != NULL) {
156                 assert(txq_ctrl->priv != NULL);
157                 assert(txq_ctrl->priv->ctx != NULL);
158                 assert(txq_ctrl->cq != NULL);
159                 params = (struct ibv_exp_release_intf_params){
160                         .comp_mask = 0,
161                 };
162                 claim_zero(ibv_exp_release_intf(txq_ctrl->priv->ctx,
163                                                 txq_ctrl->if_cq,
164                                                 &params));
165         }
166         if (txq_ctrl->qp != NULL)
167                 claim_zero(ibv_destroy_qp(txq_ctrl->qp));
168         if (txq_ctrl->cq != NULL)
169                 claim_zero(ibv_destroy_cq(txq_ctrl->cq));
170         if (txq_ctrl->rd != NULL) {
171                 struct ibv_exp_destroy_res_domain_attr attr = {
172                         .comp_mask = 0,
173                 };
174
175                 assert(txq_ctrl->priv != NULL);
176                 assert(txq_ctrl->priv->ctx != NULL);
177                 claim_zero(ibv_exp_destroy_res_domain(txq_ctrl->priv->ctx,
178                                                       txq_ctrl->rd,
179                                                       &attr));
180         }
181         for (i = 0; (i != RTE_DIM(txq_ctrl->txq.mp2mr)); ++i) {
182                 if (txq_ctrl->txq.mp2mr[i].mp == NULL)
183                         break;
184                 assert(txq_ctrl->txq.mp2mr[i].mr != NULL);
185                 claim_zero(ibv_dereg_mr(txq_ctrl->txq.mp2mr[i].mr));
186         }
187         memset(txq_ctrl, 0, sizeof(*txq_ctrl));
188 }
189
190 /**
191  * Initialize TX queue.
192  *
193  * @param tmpl
194  *   Pointer to TX queue control template.
195  * @param txq_ctrl
196  *   Pointer to TX queue control.
197  *
198  * @return
199  *   0 on success, errno value on failure.
200  */
201 static inline int
202 txq_setup(struct txq_ctrl *tmpl, struct txq_ctrl *txq_ctrl)
203 {
204         struct mlx5_qp *qp = to_mqp(tmpl->qp);
205         struct ibv_cq *ibcq = tmpl->cq;
206         struct mlx5_cq *cq = to_mxxx(cq, cq);
207
208         if (cq->cqe_sz != RTE_CACHE_LINE_SIZE) {
209                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
210                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
211                 return EINVAL;
212         }
213         tmpl->txq.cqe_n = ibcq->cqe + 1;
214         tmpl->txq.qp_num_8s = qp->ctrl_seg.qp_num << 8;
215         tmpl->txq.wqes =
216                 (volatile union mlx5_wqe (*)[])
217                 (uintptr_t)qp->gen_data.sqstart;
218         tmpl->txq.wqe_n = qp->sq.wqe_cnt;
219         tmpl->txq.qp_db = &qp->gen_data.db[MLX5_SND_DBR];
220         tmpl->txq.bf_reg = qp->gen_data.bf->reg;
221         tmpl->txq.bf_offset = qp->gen_data.bf->offset;
222         tmpl->txq.bf_buf_size = qp->gen_data.bf->buf_size;
223         tmpl->txq.cq_db = cq->dbrec;
224         tmpl->txq.cqes =
225                 (volatile struct mlx5_cqe (*)[])
226                 (uintptr_t)cq->active_buf->buf;
227         tmpl->txq.elts =
228                 (struct rte_mbuf *(*)[tmpl->txq.elts_n])
229                 ((uintptr_t)txq_ctrl + sizeof(*txq_ctrl));
230         return 0;
231 }
232
233 /**
234  * Configure a TX queue.
235  *
236  * @param dev
237  *   Pointer to Ethernet device structure.
238  * @param txq_ctrl
239  *   Pointer to TX queue structure.
240  * @param desc
241  *   Number of descriptors to configure in queue.
242  * @param socket
243  *   NUMA socket on which memory must be allocated.
244  * @param[in] conf
245  *   Thresholds parameters.
246  *
247  * @return
248  *   0 on success, errno value on failure.
249  */
250 int
251 txq_ctrl_setup(struct rte_eth_dev *dev, struct txq_ctrl *txq_ctrl,
252                uint16_t desc, unsigned int socket,
253                const struct rte_eth_txconf *conf)
254 {
255         struct priv *priv = mlx5_get_priv(dev);
256         struct txq_ctrl tmpl = {
257                 .priv = priv,
258                 .socket = socket,
259         };
260         union {
261                 struct ibv_exp_query_intf_params params;
262                 struct ibv_exp_qp_init_attr init;
263                 struct ibv_exp_res_domain_init_attr rd;
264                 struct ibv_exp_cq_init_attr cq;
265                 struct ibv_exp_qp_attr mod;
266                 struct ibv_exp_cq_attr cq_attr;
267         } attr;
268         enum ibv_exp_query_intf_status status;
269         int ret = 0;
270
271         (void)conf; /* Thresholds configuration (ignored). */
272         tmpl.txq.elts_n = desc;
273         /*
274          * Request send completion every MLX5_PMD_TX_PER_COMP_REQ packets or
275          * at least 4 times per ring.
276          */
277         tmpl.txq.elts_comp_cd_init =
278                 ((MLX5_PMD_TX_PER_COMP_REQ < (desc / 4)) ?
279                  MLX5_PMD_TX_PER_COMP_REQ : (desc / 4));
280         tmpl.txq.elts_comp = tmpl.txq.elts_comp_cd_init;
281         /* MRs will be registered in mp2mr[] later. */
282         attr.rd = (struct ibv_exp_res_domain_init_attr){
283                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
284                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
285                 .thread_model = IBV_EXP_THREAD_SINGLE,
286                 .msg_model = IBV_EXP_MSG_HIGH_BW,
287         };
288         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
289         if (tmpl.rd == NULL) {
290                 ret = ENOMEM;
291                 ERROR("%p: RD creation failure: %s",
292                       (void *)dev, strerror(ret));
293                 goto error;
294         }
295         attr.cq = (struct ibv_exp_cq_init_attr){
296                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
297                 .res_domain = tmpl.rd,
298         };
299         tmpl.cq = ibv_exp_create_cq(priv->ctx,
300                                     (desc / tmpl.txq.elts_comp_cd_init) - 1,
301                                     NULL, NULL, 0, &attr.cq);
302         if (tmpl.cq == NULL) {
303                 ret = ENOMEM;
304                 ERROR("%p: CQ creation failure: %s",
305                       (void *)dev, strerror(ret));
306                 goto error;
307         }
308         DEBUG("priv->device_attr.max_qp_wr is %d",
309               priv->device_attr.max_qp_wr);
310         DEBUG("priv->device_attr.max_sge is %d",
311               priv->device_attr.max_sge);
312         attr.init = (struct ibv_exp_qp_init_attr){
313                 /* CQ to be associated with the send queue. */
314                 .send_cq = tmpl.cq,
315                 /* CQ to be associated with the receive queue. */
316                 .recv_cq = tmpl.cq,
317                 .cap = {
318                         /* Max number of outstanding WRs. */
319                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
320                                         priv->device_attr.max_qp_wr :
321                                         desc),
322                         /* Max number of scatter/gather elements in a WR. */
323                         .max_send_sge = 1,
324                 },
325                 .qp_type = IBV_QPT_RAW_PACKET,
326                 /* Do *NOT* enable this, completions events are managed per
327                  * TX burst. */
328                 .sq_sig_all = 0,
329                 .pd = priv->pd,
330                 .res_domain = tmpl.rd,
331                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
332                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
333         };
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         attr.mod = (struct ibv_exp_qp_attr){
342                 /* Move the QP to this state. */
343                 .qp_state = IBV_QPS_INIT,
344                 /* Primary port number. */
345                 .port_num = priv->port
346         };
347         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
348                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
349         if (ret) {
350                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
351                       (void *)dev, strerror(ret));
352                 goto error;
353         }
354         ret = txq_setup(&tmpl, txq_ctrl);
355         if (ret) {
356                 ERROR("%p: cannot initialize TX queue structure: %s",
357                       (void *)dev, strerror(ret));
358                 goto error;
359         }
360         txq_alloc_elts(&tmpl, desc);
361         attr.mod = (struct ibv_exp_qp_attr){
362                 .qp_state = IBV_QPS_RTR
363         };
364         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
365         if (ret) {
366                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
367                       (void *)dev, strerror(ret));
368                 goto error;
369         }
370         attr.mod.qp_state = IBV_QPS_RTS;
371         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
372         if (ret) {
373                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
374                       (void *)dev, strerror(ret));
375                 goto error;
376         }
377         attr.params = (struct ibv_exp_query_intf_params){
378                 .intf_scope = IBV_EXP_INTF_GLOBAL,
379                 .intf = IBV_EXP_INTF_CQ,
380                 .obj = tmpl.cq,
381         };
382         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
383         if (tmpl.if_cq == NULL) {
384                 ret = EINVAL;
385                 ERROR("%p: CQ interface family query failed with status %d",
386                       (void *)dev, status);
387                 goto error;
388         }
389         attr.params = (struct ibv_exp_query_intf_params){
390                 .intf_scope = IBV_EXP_INTF_GLOBAL,
391                 .intf = IBV_EXP_INTF_QP_BURST,
392                 .intf_version = 1,
393                 .obj = tmpl.qp,
394                 /* Enable multi-packet send if supported. */
395                 .family_flags =
396                         (priv->mps ?
397                          IBV_EXP_QP_BURST_CREATE_ENABLE_MULTI_PACKET_SEND_WR :
398                          0),
399         };
400         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
401         if (tmpl.if_qp == NULL) {
402                 ret = EINVAL;
403                 ERROR("%p: QP interface family query failed with status %d",
404                       (void *)dev, status);
405                 goto error;
406         }
407         /* Clean up txq in case we're reinitializing it. */
408         DEBUG("%p: cleaning-up old txq just in case", (void *)txq_ctrl);
409         txq_cleanup(txq_ctrl);
410         *txq_ctrl = tmpl;
411         DEBUG("%p: txq updated with %p", (void *)txq_ctrl, (void *)&tmpl);
412         /* Pre-register known mempools. */
413         rte_mempool_walk(txq_mp2mr_iter, txq_ctrl);
414         assert(ret == 0);
415         return 0;
416 error:
417         txq_cleanup(&tmpl);
418         assert(ret > 0);
419         return ret;
420 }
421
422 /**
423  * DPDK callback to configure a TX queue.
424  *
425  * @param dev
426  *   Pointer to Ethernet device structure.
427  * @param idx
428  *   TX queue index.
429  * @param desc
430  *   Number of descriptors to configure in queue.
431  * @param socket
432  *   NUMA socket on which memory must be allocated.
433  * @param[in] conf
434  *   Thresholds parameters.
435  *
436  * @return
437  *   0 on success, negative errno value on failure.
438  */
439 int
440 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
441                     unsigned int socket, const struct rte_eth_txconf *conf)
442 {
443         struct priv *priv = dev->data->dev_private;
444         struct txq *txq = (*priv->txqs)[idx];
445         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
446         int ret;
447
448         if (mlx5_is_secondary())
449                 return -E_RTE_SECONDARY;
450
451         priv_lock(priv);
452         if (!rte_is_power_of_2(desc)) {
453                 desc = 1 << log2above(desc);
454                 WARN("%p: increased number of descriptors in TX queue %u"
455                      " to the next power of two (%d)",
456                      (void *)dev, idx, desc);
457         }
458         DEBUG("%p: configuring queue %u for %u descriptors",
459               (void *)dev, idx, desc);
460         if (idx >= priv->txqs_n) {
461                 ERROR("%p: queue index out of range (%u >= %u)",
462                       (void *)dev, idx, priv->txqs_n);
463                 priv_unlock(priv);
464                 return -EOVERFLOW;
465         }
466         if (txq != NULL) {
467                 DEBUG("%p: reusing already allocated queue index %u (%p)",
468                       (void *)dev, idx, (void *)txq);
469                 if (priv->started) {
470                         priv_unlock(priv);
471                         return -EEXIST;
472                 }
473                 (*priv->txqs)[idx] = NULL;
474                 txq_cleanup(txq_ctrl);
475         } else {
476                 txq_ctrl =
477                         rte_calloc_socket("TXQ", 1,
478                                           sizeof(*txq_ctrl) +
479                                           desc * sizeof(struct rte_mbuf *),
480                                           0, socket);
481                 if (txq_ctrl == NULL) {
482                         ERROR("%p: unable to allocate queue index %u",
483                               (void *)dev, idx);
484                         priv_unlock(priv);
485                         return -ENOMEM;
486                 }
487         }
488         ret = txq_ctrl_setup(dev, txq_ctrl, desc, socket, conf);
489         if (ret)
490                 rte_free(txq_ctrl);
491         else {
492                 txq_ctrl->txq.stats.idx = idx;
493                 DEBUG("%p: adding TX queue %p to list",
494                       (void *)dev, (void *)txq_ctrl);
495                 (*priv->txqs)[idx] = &txq_ctrl->txq;
496                 /* Update send callback. */
497                 priv_select_tx_function(priv);
498         }
499         priv_unlock(priv);
500         return -ret;
501 }
502
503 /**
504  * DPDK callback to release a TX queue.
505  *
506  * @param dpdk_txq
507  *   Generic TX queue pointer.
508  */
509 void
510 mlx5_tx_queue_release(void *dpdk_txq)
511 {
512         struct txq *txq = (struct txq *)dpdk_txq;
513         struct txq_ctrl *txq_ctrl;
514         struct priv *priv;
515         unsigned int i;
516
517         if (mlx5_is_secondary())
518                 return;
519
520         if (txq == NULL)
521                 return;
522         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
523         priv = txq_ctrl->priv;
524         priv_lock(priv);
525         for (i = 0; (i != priv->txqs_n); ++i)
526                 if ((*priv->txqs)[i] == txq) {
527                         DEBUG("%p: removing TX queue %p from list",
528                               (void *)priv->dev, (void *)txq_ctrl);
529                         (*priv->txqs)[i] = NULL;
530                         break;
531                 }
532         txq_cleanup(txq_ctrl);
533         rte_free(txq_ctrl);
534         priv_unlock(priv);
535 }
536
537 /**
538  * DPDK callback for TX in secondary processes.
539  *
540  * This function configures all queues from primary process information
541  * if necessary before reverting to the normal TX burst callback.
542  *
543  * @param dpdk_txq
544  *   Generic pointer to TX queue structure.
545  * @param[in] pkts
546  *   Packets to transmit.
547  * @param pkts_n
548  *   Number of packets in array.
549  *
550  * @return
551  *   Number of packets successfully transmitted (<= pkts_n).
552  */
553 uint16_t
554 mlx5_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
555                               uint16_t pkts_n)
556 {
557         struct txq *txq = dpdk_txq;
558         struct txq_ctrl *txq_ctrl = container_of(txq, struct txq_ctrl, txq);
559         struct priv *priv = mlx5_secondary_data_setup(txq_ctrl->priv);
560         struct priv *primary_priv;
561         unsigned int index;
562
563         if (priv == NULL)
564                 return 0;
565         primary_priv =
566                 mlx5_secondary_data[priv->dev->data->port_id].primary_priv;
567         /* Look for queue index in both private structures. */
568         for (index = 0; index != priv->txqs_n; ++index)
569                 if (((*primary_priv->txqs)[index] == txq) ||
570                     ((*priv->txqs)[index] == txq))
571                         break;
572         if (index == priv->txqs_n)
573                 return 0;
574         txq = (*priv->txqs)[index];
575         return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
576 }