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