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