net/mlx5: remove Tx gather 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 #if MLX5_PMD_MAX_INLINE > 0
177         txq->send_pending_inline = NULL;
178 #endif
179         txq->send_flush = NULL;
180         if (txq->if_qp != NULL) {
181                 assert(txq->priv != NULL);
182                 assert(txq->priv->ctx != NULL);
183                 assert(txq->qp != NULL);
184                 params = (struct ibv_exp_release_intf_params){
185                         .comp_mask = 0,
186                 };
187                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
188                                                 txq->if_qp,
189                                                 &params));
190         }
191         if (txq->if_cq != NULL) {
192                 assert(txq->priv != NULL);
193                 assert(txq->priv->ctx != NULL);
194                 assert(txq->cq != NULL);
195                 params = (struct ibv_exp_release_intf_params){
196                         .comp_mask = 0,
197                 };
198                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
199                                                 txq->if_cq,
200                                                 &params));
201         }
202         if (txq->qp != NULL)
203                 claim_zero(ibv_destroy_qp(txq->qp));
204         if (txq->cq != NULL)
205                 claim_zero(ibv_destroy_cq(txq->cq));
206         if (txq->rd != NULL) {
207                 struct ibv_exp_destroy_res_domain_attr attr = {
208                         .comp_mask = 0,
209                 };
210
211                 assert(txq->priv != NULL);
212                 assert(txq->priv->ctx != NULL);
213                 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
214                                                       txq->rd,
215                                                       &attr));
216         }
217         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
218                 if (txq->mp2mr[i].mp == NULL)
219                         break;
220                 assert(txq->mp2mr[i].mr != NULL);
221                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
222         }
223         memset(txq, 0, sizeof(*txq));
224 }
225
226 /**
227  * Configure a TX queue.
228  *
229  * @param dev
230  *   Pointer to Ethernet device structure.
231  * @param txq
232  *   Pointer to TX queue structure.
233  * @param desc
234  *   Number of descriptors to configure in queue.
235  * @param socket
236  *   NUMA socket on which memory must be allocated.
237  * @param[in] conf
238  *   Thresholds parameters.
239  *
240  * @return
241  *   0 on success, errno value on failure.
242  */
243 int
244 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
245           unsigned int socket, const struct rte_eth_txconf *conf)
246 {
247         struct priv *priv = mlx5_get_priv(dev);
248         struct txq tmpl = {
249                 .priv = priv,
250                 .socket = socket
251         };
252         union {
253                 struct ibv_exp_query_intf_params params;
254                 struct ibv_exp_qp_init_attr init;
255                 struct ibv_exp_res_domain_init_attr rd;
256                 struct ibv_exp_cq_init_attr cq;
257                 struct ibv_exp_qp_attr mod;
258         } attr;
259         enum ibv_exp_query_intf_status status;
260         int ret = 0;
261
262         (void)conf; /* Thresholds configuration (ignored). */
263         if (desc == 0) {
264                 ERROR("%p: invalid number of TX descriptors", (void *)dev);
265                 return EINVAL;
266         }
267         if (MLX5_PMD_SGE_WR_N > 1) {
268                 ERROR("%p: TX gather is not supported", (void *)dev);
269                 return EINVAL;
270         }
271         /* MRs will be registered in mp2mr[] later. */
272         attr.rd = (struct ibv_exp_res_domain_init_attr){
273                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
274                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
275                 .thread_model = IBV_EXP_THREAD_SINGLE,
276                 .msg_model = IBV_EXP_MSG_HIGH_BW,
277         };
278         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
279         if (tmpl.rd == NULL) {
280                 ret = ENOMEM;
281                 ERROR("%p: RD creation failure: %s",
282                       (void *)dev, strerror(ret));
283                 goto error;
284         }
285         attr.cq = (struct ibv_exp_cq_init_attr){
286                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
287                 .res_domain = tmpl.rd,
288         };
289         tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
290         if (tmpl.cq == NULL) {
291                 ret = ENOMEM;
292                 ERROR("%p: CQ creation failure: %s",
293                       (void *)dev, strerror(ret));
294                 goto error;
295         }
296         DEBUG("priv->device_attr.max_qp_wr is %d",
297               priv->device_attr.max_qp_wr);
298         DEBUG("priv->device_attr.max_sge is %d",
299               priv->device_attr.max_sge);
300         attr.init = (struct ibv_exp_qp_init_attr){
301                 /* CQ to be associated with the send queue. */
302                 .send_cq = tmpl.cq,
303                 /* CQ to be associated with the receive queue. */
304                 .recv_cq = tmpl.cq,
305                 .cap = {
306                         /* Max number of outstanding WRs. */
307                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
308                                         priv->device_attr.max_qp_wr :
309                                         desc),
310                         /* Max number of scatter/gather elements in a WR. */
311                         .max_send_sge = 1,
312 #if MLX5_PMD_MAX_INLINE > 0
313                         .max_inline_data = MLX5_PMD_MAX_INLINE,
314 #endif
315                 },
316                 .qp_type = IBV_QPT_RAW_PACKET,
317                 /* Do *NOT* enable this, completions events are managed per
318                  * TX burst. */
319                 .sq_sig_all = 0,
320                 .pd = priv->pd,
321                 .res_domain = tmpl.rd,
322                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
323                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
324         };
325         tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
326         if (tmpl.qp == NULL) {
327                 ret = (errno ? errno : EINVAL);
328                 ERROR("%p: QP creation failure: %s",
329                       (void *)dev, strerror(ret));
330                 goto error;
331         }
332 #if MLX5_PMD_MAX_INLINE > 0
333         /* ibv_create_qp() updates this value. */
334         tmpl.max_inline = attr.init.cap.max_inline_data;
335 #endif
336         attr.mod = (struct ibv_exp_qp_attr){
337                 /* Move the QP to this state. */
338                 .qp_state = IBV_QPS_INIT,
339                 /* Primary port number. */
340                 .port_num = priv->port
341         };
342         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
343                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
344         if (ret) {
345                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
346                       (void *)dev, strerror(ret));
347                 goto error;
348         }
349         ret = txq_alloc_elts(&tmpl, desc);
350         if (ret) {
351                 ERROR("%p: TXQ allocation failed: %s",
352                       (void *)dev, strerror(ret));
353                 goto error;
354         }
355         attr.mod = (struct ibv_exp_qp_attr){
356                 .qp_state = IBV_QPS_RTR
357         };
358         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
359         if (ret) {
360                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
361                       (void *)dev, strerror(ret));
362                 goto error;
363         }
364         attr.mod.qp_state = IBV_QPS_RTS;
365         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
366         if (ret) {
367                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
368                       (void *)dev, strerror(ret));
369                 goto error;
370         }
371         attr.params = (struct ibv_exp_query_intf_params){
372                 .intf_scope = IBV_EXP_INTF_GLOBAL,
373                 .intf = IBV_EXP_INTF_CQ,
374                 .obj = tmpl.cq,
375         };
376         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
377         if (tmpl.if_cq == NULL) {
378                 ret = EINVAL;
379                 ERROR("%p: CQ interface family query failed with status %d",
380                       (void *)dev, status);
381                 goto error;
382         }
383         attr.params = (struct ibv_exp_query_intf_params){
384                 .intf_scope = IBV_EXP_INTF_GLOBAL,
385                 .intf = IBV_EXP_INTF_QP_BURST,
386                 .obj = tmpl.qp,
387 #ifdef HAVE_VERBS_VLAN_INSERTION
388                 .intf_version = 1,
389 #endif
390 #ifdef HAVE_EXP_QP_BURST_CREATE_ENABLE_MULTI_PACKET_SEND_WR
391                 /* Enable multi-packet send if supported. */
392                 .family_flags =
393                         (priv->mps ?
394                          IBV_EXP_QP_BURST_CREATE_ENABLE_MULTI_PACKET_SEND_WR :
395                          0),
396 #endif
397         };
398         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
399         if (tmpl.if_qp == NULL) {
400                 ret = EINVAL;
401                 ERROR("%p: QP interface family query failed with status %d",
402                       (void *)dev, status);
403                 goto error;
404         }
405         /* Clean up txq in case we're reinitializing it. */
406         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
407         txq_cleanup(txq);
408         *txq = tmpl;
409         txq->poll_cnt = txq->if_cq->poll_cnt;
410 #if MLX5_PMD_MAX_INLINE > 0
411         txq->send_pending_inline = txq->if_qp->send_pending_inline;
412 #ifdef HAVE_VERBS_VLAN_INSERTION
413         txq->send_pending_inline_vlan = txq->if_qp->send_pending_inline_vlan;
414 #endif
415 #endif
416         txq->send_pending = txq->if_qp->send_pending;
417 #ifdef HAVE_VERBS_VLAN_INSERTION
418         txq->send_pending_vlan = txq->if_qp->send_pending_vlan;
419 #endif
420         txq->send_flush = txq->if_qp->send_flush;
421         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
422         /* Pre-register known mempools. */
423         rte_mempool_walk(txq_mp2mr_iter, txq);
424         assert(ret == 0);
425         return 0;
426 error:
427         txq_cleanup(&tmpl);
428         assert(ret > 0);
429         return ret;
430 }
431
432 /**
433  * DPDK callback to configure a TX queue.
434  *
435  * @param dev
436  *   Pointer to Ethernet device structure.
437  * @param idx
438  *   TX queue index.
439  * @param desc
440  *   Number of descriptors to configure in queue.
441  * @param socket
442  *   NUMA socket on which memory must be allocated.
443  * @param[in] conf
444  *   Thresholds parameters.
445  *
446  * @return
447  *   0 on success, negative errno value on failure.
448  */
449 int
450 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
451                     unsigned int socket, const struct rte_eth_txconf *conf)
452 {
453         struct priv *priv = dev->data->dev_private;
454         struct txq *txq = (*priv->txqs)[idx];
455         int ret;
456
457         if (mlx5_is_secondary())
458                 return -E_RTE_SECONDARY;
459
460         priv_lock(priv);
461         DEBUG("%p: configuring queue %u for %u descriptors",
462               (void *)dev, idx, desc);
463         if (idx >= priv->txqs_n) {
464                 ERROR("%p: queue index out of range (%u >= %u)",
465                       (void *)dev, idx, priv->txqs_n);
466                 priv_unlock(priv);
467                 return -EOVERFLOW;
468         }
469         if (txq != NULL) {
470                 DEBUG("%p: reusing already allocated queue index %u (%p)",
471                       (void *)dev, idx, (void *)txq);
472                 if (priv->started) {
473                         priv_unlock(priv);
474                         return -EEXIST;
475                 }
476                 (*priv->txqs)[idx] = NULL;
477                 txq_cleanup(txq);
478         } else {
479                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
480                 if (txq == NULL) {
481                         ERROR("%p: unable to allocate queue index %u",
482                               (void *)dev, idx);
483                         priv_unlock(priv);
484                         return -ENOMEM;
485                 }
486         }
487         ret = txq_setup(dev, txq, desc, socket, conf);
488         if (ret)
489                 rte_free(txq);
490         else {
491                 txq->stats.idx = idx;
492                 DEBUG("%p: adding TX queue %p to list",
493                       (void *)dev, (void *)txq);
494                 (*priv->txqs)[idx] = txq;
495                 /* Update send callback. */
496                 dev->tx_pkt_burst = mlx5_tx_burst;
497         }
498         priv_unlock(priv);
499         return -ret;
500 }
501
502 /**
503  * DPDK callback to release a TX queue.
504  *
505  * @param dpdk_txq
506  *   Generic TX queue pointer.
507  */
508 void
509 mlx5_tx_queue_release(void *dpdk_txq)
510 {
511         struct txq *txq = (struct txq *)dpdk_txq;
512         struct priv *priv;
513         unsigned int i;
514
515         if (mlx5_is_secondary())
516                 return;
517
518         if (txq == NULL)
519                 return;
520         priv = txq->priv;
521         priv_lock(priv);
522         for (i = 0; (i != priv->txqs_n); ++i)
523                 if ((*priv->txqs)[i] == txq) {
524                         DEBUG("%p: removing TX queue %p from list",
525                               (void *)priv->dev, (void *)txq);
526                         (*priv->txqs)[i] = NULL;
527                         break;
528                 }
529         txq_cleanup(txq);
530         rte_free(txq);
531         priv_unlock(priv);
532 }
533
534 /**
535  * DPDK callback for TX in secondary processes.
536  *
537  * This function configures all queues from primary process information
538  * if necessary before reverting to the normal TX burst callback.
539  *
540  * @param dpdk_txq
541  *   Generic pointer to TX queue structure.
542  * @param[in] pkts
543  *   Packets to transmit.
544  * @param pkts_n
545  *   Number of packets in array.
546  *
547  * @return
548  *   Number of packets successfully transmitted (<= pkts_n).
549  */
550 uint16_t
551 mlx5_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
552                               uint16_t pkts_n)
553 {
554         struct txq *txq = dpdk_txq;
555         struct priv *priv = mlx5_secondary_data_setup(txq->priv);
556         struct priv *primary_priv;
557         unsigned int index;
558
559         if (priv == NULL)
560                 return 0;
561         primary_priv =
562                 mlx5_secondary_data[priv->dev->data->port_id].primary_priv;
563         /* Look for queue index in both private structures. */
564         for (index = 0; index != priv->txqs_n; ++index)
565                 if (((*primary_priv->txqs)[index] == txq) ||
566                     ((*priv->txqs)[index] == txq))
567                         break;
568         if (index == priv->txqs_n)
569                 return 0;
570         txq = (*priv->txqs)[index];
571         return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
572 }