net/mlx4: separate Tx configuration functions
[dpdk.git] / drivers / net / mlx4 / mlx4_txq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 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 /**
35  * @file
36  * Tx queues configuration for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <string.h>
44
45 /* Verbs headers do not support -pedantic. */
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic ignored "-Wpedantic"
48 #endif
49 #include <infiniband/verbs.h>
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic error "-Wpedantic"
52 #endif
53
54 #include <rte_common.h>
55 #include <rte_errno.h>
56 #include <rte_ethdev.h>
57 #include <rte_malloc.h>
58 #include <rte_mbuf.h>
59 #include <rte_mempool.h>
60
61 #include "mlx4.h"
62 #include "mlx4_autoconf.h"
63 #include "mlx4_rxtx.h"
64 #include "mlx4_utils.h"
65
66 /**
67  * Allocate Tx queue elements.
68  *
69  * @param txq
70  *   Pointer to Tx queue structure.
71  * @param elts_n
72  *   Number of elements to allocate.
73  *
74  * @return
75  *   0 on success, negative errno value otherwise and rte_errno is set.
76  */
77 static int
78 mlx4_txq_alloc_elts(struct txq *txq, unsigned int elts_n)
79 {
80         unsigned int i;
81         struct txq_elt (*elts)[elts_n] =
82                 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
83         int ret = 0;
84
85         if (elts == NULL) {
86                 ERROR("%p: can't allocate packets array", (void *)txq);
87                 ret = ENOMEM;
88                 goto error;
89         }
90         for (i = 0; (i != elts_n); ++i) {
91                 struct txq_elt *elt = &(*elts)[i];
92
93                 elt->buf = NULL;
94         }
95         DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
96         txq->elts_n = elts_n;
97         txq->elts = elts;
98         txq->elts_head = 0;
99         txq->elts_tail = 0;
100         txq->elts_comp = 0;
101         /*
102          * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
103          * at least 4 times per ring.
104          */
105         txq->elts_comp_cd_init =
106                 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
107                  MLX4_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         DEBUG("%p: failed, freed everything", (void *)txq);
114         assert(ret > 0);
115         rte_errno = ret;
116         return -rte_errno;
117 }
118
119 /**
120  * Free Tx queue elements.
121  *
122  * @param txq
123  *   Pointer to Tx queue structure.
124  */
125 static void
126 mlx4_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         if (elts == NULL)
142                 return;
143         while (elts_tail != elts_head) {
144                 struct txq_elt *elt = &(*elts)[elts_tail];
145
146                 assert(elt->buf != NULL);
147                 rte_pktmbuf_free(elt->buf);
148 #ifndef NDEBUG
149                 /* Poisoning. */
150                 memset(elt, 0x77, sizeof(*elt));
151 #endif
152                 if (++elts_tail == elts_n)
153                         elts_tail = 0;
154         }
155         rte_free(elts);
156 }
157
158 /**
159  * Clean up a Tx queue.
160  *
161  * Destroy objects, free allocated memory and reset the structure for reuse.
162  *
163  * @param txq
164  *   Pointer to Tx queue structure.
165  */
166 void
167 mlx4_txq_cleanup(struct txq *txq)
168 {
169         size_t i;
170
171         DEBUG("cleaning up %p", (void *)txq);
172         mlx4_txq_free_elts(txq);
173         if (txq->qp != NULL)
174                 claim_zero(ibv_destroy_qp(txq->qp));
175         if (txq->cq != NULL)
176                 claim_zero(ibv_destroy_cq(txq->cq));
177         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
178                 if (txq->mp2mr[i].mp == NULL)
179                         break;
180                 assert(txq->mp2mr[i].mr != NULL);
181                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
182         }
183         memset(txq, 0, sizeof(*txq));
184 }
185
186 struct txq_mp2mr_mbuf_check_data {
187         int ret;
188 };
189
190 /**
191  * Callback function for rte_mempool_obj_iter() to check whether a given
192  * mempool object looks like a mbuf.
193  *
194  * @param[in] mp
195  *   The mempool pointer
196  * @param[in] arg
197  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
198  *   return value.
199  * @param[in] obj
200  *   Object address.
201  * @param index
202  *   Object index, unused.
203  */
204 static void
205 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
206                           uint32_t index)
207 {
208         struct txq_mp2mr_mbuf_check_data *data = arg;
209         struct rte_mbuf *buf = obj;
210
211         (void)index;
212         /*
213          * Check whether mbuf structure fits element size and whether mempool
214          * pointer is valid.
215          */
216         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
217                 data->ret = -1;
218 }
219
220 /**
221  * Iterator function for rte_mempool_walk() to register existing mempools and
222  * fill the MP to MR cache of a Tx queue.
223  *
224  * @param[in] mp
225  *   Memory Pool to register.
226  * @param *arg
227  *   Pointer to Tx queue structure.
228  */
229 static void
230 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
231 {
232         struct txq *txq = arg;
233         struct txq_mp2mr_mbuf_check_data data = {
234                 .ret = 0,
235         };
236
237         /* Register mempool only if the first element looks like a mbuf. */
238         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
239                         data.ret == -1)
240                 return;
241         mlx4_txq_mp2mr(txq, mp);
242 }
243
244 /**
245  * Configure a Tx queue.
246  *
247  * @param dev
248  *   Pointer to Ethernet device structure.
249  * @param txq
250  *   Pointer to Tx queue structure.
251  * @param desc
252  *   Number of descriptors to configure in queue.
253  * @param socket
254  *   NUMA socket on which memory must be allocated.
255  * @param[in] conf
256  *   Thresholds parameters.
257  *
258  * @return
259  *   0 on success, negative errno value otherwise and rte_errno is set.
260  */
261 static int
262 mlx4_txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
263                unsigned int socket, const struct rte_eth_txconf *conf)
264 {
265         struct priv *priv = dev->data->dev_private;
266         struct txq tmpl = {
267                 .priv = priv,
268                 .socket = socket
269         };
270         union {
271                 struct ibv_qp_init_attr init;
272                 struct ibv_qp_attr mod;
273         } attr;
274         int ret;
275
276         (void)conf; /* Thresholds configuration (ignored). */
277         if (priv == NULL) {
278                 rte_errno = EINVAL;
279                 goto error;
280         }
281         if (desc == 0) {
282                 rte_errno = EINVAL;
283                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
284                 goto error;
285         }
286         /* MRs will be registered in mp2mr[] later. */
287         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
288         if (tmpl.cq == NULL) {
289                 rte_errno = ENOMEM;
290                 ERROR("%p: CQ creation failure: %s",
291                       (void *)dev, strerror(rte_errno));
292                 goto error;
293         }
294         DEBUG("priv->device_attr.max_qp_wr is %d",
295               priv->device_attr.max_qp_wr);
296         DEBUG("priv->device_attr.max_sge is %d",
297               priv->device_attr.max_sge);
298         attr.init = (struct ibv_qp_init_attr){
299                 /* CQ to be associated with the send queue. */
300                 .send_cq = tmpl.cq,
301                 /* CQ to be associated with the receive queue. */
302                 .recv_cq = tmpl.cq,
303                 .cap = {
304                         /* Max number of outstanding WRs. */
305                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
306                                         priv->device_attr.max_qp_wr :
307                                         desc),
308                         /* Max number of scatter/gather elements in a WR. */
309                         .max_send_sge = 1,
310                         .max_inline_data = MLX4_PMD_MAX_INLINE,
311                 },
312                 .qp_type = IBV_QPT_RAW_PACKET,
313                 /*
314                  * Do *NOT* enable this, completions events are managed per
315                  * Tx burst.
316                  */
317                 .sq_sig_all = 0,
318         };
319         tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
320         if (tmpl.qp == NULL) {
321                 rte_errno = errno ? errno : EINVAL;
322                 ERROR("%p: QP creation failure: %s",
323                       (void *)dev, strerror(rte_errno));
324                 goto error;
325         }
326         /* ibv_create_qp() updates this value. */
327         tmpl.max_inline = attr.init.cap.max_inline_data;
328         attr.mod = (struct ibv_qp_attr){
329                 /* Move the QP to this state. */
330                 .qp_state = IBV_QPS_INIT,
331                 /* Primary port number. */
332                 .port_num = priv->port
333         };
334         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
335         if (ret) {
336                 rte_errno = ret;
337                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
338                       (void *)dev, strerror(rte_errno));
339                 goto error;
340         }
341         ret = mlx4_txq_alloc_elts(&tmpl, desc);
342         if (ret) {
343                 rte_errno = ret;
344                 ERROR("%p: TXQ allocation failed: %s",
345                       (void *)dev, strerror(rte_errno));
346                 goto error;
347         }
348         attr.mod = (struct ibv_qp_attr){
349                 .qp_state = IBV_QPS_RTR
350         };
351         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
352         if (ret) {
353                 rte_errno = ret;
354                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
355                       (void *)dev, strerror(rte_errno));
356                 goto error;
357         }
358         attr.mod.qp_state = IBV_QPS_RTS;
359         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
360         if (ret) {
361                 rte_errno = ret;
362                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
363                       (void *)dev, strerror(rte_errno));
364                 goto error;
365         }
366         /* Clean up txq in case we're reinitializing it. */
367         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
368         mlx4_txq_cleanup(txq);
369         *txq = tmpl;
370         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
371         /* Pre-register known mempools. */
372         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
373         return 0;
374 error:
375         ret = rte_errno;
376         mlx4_txq_cleanup(&tmpl);
377         rte_errno = ret;
378         assert(rte_errno > 0);
379         return -rte_errno;
380 }
381
382 /**
383  * DPDK callback to configure a Tx queue.
384  *
385  * @param dev
386  *   Pointer to Ethernet device structure.
387  * @param idx
388  *   Tx queue index.
389  * @param desc
390  *   Number of descriptors to configure in queue.
391  * @param socket
392  *   NUMA socket on which memory must be allocated.
393  * @param[in] conf
394  *   Thresholds parameters.
395  *
396  * @return
397  *   0 on success, negative errno value otherwise and rte_errno is set.
398  */
399 int
400 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
401                     unsigned int socket, const struct rte_eth_txconf *conf)
402 {
403         struct priv *priv = dev->data->dev_private;
404         struct txq *txq = (*priv->txqs)[idx];
405         int ret;
406
407         DEBUG("%p: configuring queue %u for %u descriptors",
408               (void *)dev, idx, desc);
409         if (idx >= priv->txqs_n) {
410                 rte_errno = EOVERFLOW;
411                 ERROR("%p: queue index out of range (%u >= %u)",
412                       (void *)dev, idx, priv->txqs_n);
413                 return -rte_errno;
414         }
415         if (txq != NULL) {
416                 DEBUG("%p: reusing already allocated queue index %u (%p)",
417                       (void *)dev, idx, (void *)txq);
418                 if (priv->started) {
419                         rte_errno = EEXIST;
420                         return -rte_errno;
421                 }
422                 (*priv->txqs)[idx] = NULL;
423                 mlx4_txq_cleanup(txq);
424         } else {
425                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
426                 if (txq == NULL) {
427                         rte_errno = ENOMEM;
428                         ERROR("%p: unable to allocate queue index %u",
429                               (void *)dev, idx);
430                         return -rte_errno;
431                 }
432         }
433         ret = mlx4_txq_setup(dev, txq, desc, socket, conf);
434         if (ret) {
435                 rte_free(txq);
436         } else {
437                 txq->stats.idx = idx;
438                 DEBUG("%p: adding Tx queue %p to list",
439                       (void *)dev, (void *)txq);
440                 (*priv->txqs)[idx] = txq;
441                 /* Update send callback. */
442                 dev->tx_pkt_burst = mlx4_tx_burst;
443         }
444         return ret;
445 }
446
447 /**
448  * DPDK callback to release a Tx queue.
449  *
450  * @param dpdk_txq
451  *   Generic Tx queue pointer.
452  */
453 void
454 mlx4_tx_queue_release(void *dpdk_txq)
455 {
456         struct txq *txq = (struct txq *)dpdk_txq;
457         struct priv *priv;
458         unsigned int i;
459
460         if (txq == NULL)
461                 return;
462         priv = txq->priv;
463         for (i = 0; (i != priv->txqs_n); ++i)
464                 if ((*priv->txqs)[i] == txq) {
465                         DEBUG("%p: removing Tx queue %p from list",
466                               (void *)priv->dev, (void *)txq);
467                         (*priv->txqs)[i] = NULL;
468                         break;
469                 }
470         mlx4_txq_cleanup(txq);
471         rte_free(txq);
472 }