net/mlx4: allocate queues and mbuf rings together
[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  * Free Tx queue elements.
68  *
69  * @param txq
70  *   Pointer to Tx queue structure.
71  */
72 static void
73 mlx4_txq_free_elts(struct txq *txq)
74 {
75         unsigned int elts_head = txq->elts_head;
76         unsigned int elts_tail = txq->elts_tail;
77         struct txq_elt (*elts)[txq->elts_n] = txq->elts;
78
79         DEBUG("%p: freeing WRs", (void *)txq);
80         while (elts_tail != elts_head) {
81                 struct txq_elt *elt = &(*elts)[elts_tail];
82
83                 assert(elt->buf != NULL);
84                 rte_pktmbuf_free(elt->buf);
85                 elt->buf = NULL;
86                 if (++elts_tail == RTE_DIM(*elts))
87                         elts_tail = 0;
88         }
89         txq->elts_tail = txq->elts_head;
90 }
91
92 struct txq_mp2mr_mbuf_check_data {
93         int ret;
94 };
95
96 /**
97  * Callback function for rte_mempool_obj_iter() to check whether a given
98  * mempool object looks like a mbuf.
99  *
100  * @param[in] mp
101  *   The mempool pointer
102  * @param[in] arg
103  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
104  *   return value.
105  * @param[in] obj
106  *   Object address.
107  * @param index
108  *   Object index, unused.
109  */
110 static void
111 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
112                           uint32_t index)
113 {
114         struct txq_mp2mr_mbuf_check_data *data = arg;
115         struct rte_mbuf *buf = obj;
116
117         (void)index;
118         /*
119          * Check whether mbuf structure fits element size and whether mempool
120          * pointer is valid.
121          */
122         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
123                 data->ret = -1;
124 }
125
126 /**
127  * Iterator function for rte_mempool_walk() to register existing mempools and
128  * fill the MP to MR cache of a Tx queue.
129  *
130  * @param[in] mp
131  *   Memory Pool to register.
132  * @param *arg
133  *   Pointer to Tx queue structure.
134  */
135 static void
136 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
137 {
138         struct txq *txq = arg;
139         struct txq_mp2mr_mbuf_check_data data = {
140                 .ret = 0,
141         };
142
143         /* Register mempool only if the first element looks like a mbuf. */
144         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
145                         data.ret == -1)
146                 return;
147         mlx4_txq_mp2mr(txq, mp);
148 }
149
150 /**
151  * DPDK callback to configure a Tx queue.
152  *
153  * @param dev
154  *   Pointer to Ethernet device structure.
155  * @param idx
156  *   Tx queue index.
157  * @param desc
158  *   Number of descriptors to configure in queue.
159  * @param socket
160  *   NUMA socket on which memory must be allocated.
161  * @param[in] conf
162  *   Thresholds parameters.
163  *
164  * @return
165  *   0 on success, negative errno value otherwise and rte_errno is set.
166  */
167 int
168 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
169                     unsigned int socket, const struct rte_eth_txconf *conf)
170 {
171         struct priv *priv = dev->data->dev_private;
172         struct txq_elt (*elts)[desc];
173         struct ibv_qp_init_attr qp_init_attr;
174         struct txq *txq;
175         struct mlx4_malloc_vec vec[] = {
176                 {
177                         .align = RTE_CACHE_LINE_SIZE,
178                         .size = sizeof(*txq),
179                         .addr = (void **)&txq,
180                 },
181                 {
182                         .align = RTE_CACHE_LINE_SIZE,
183                         .size = sizeof(*elts),
184                         .addr = (void **)&elts,
185                 },
186         };
187         int ret;
188
189         (void)conf; /* Thresholds configuration (ignored). */
190         DEBUG("%p: configuring queue %u for %u descriptors",
191               (void *)dev, idx, desc);
192         if (idx >= dev->data->nb_tx_queues) {
193                 rte_errno = EOVERFLOW;
194                 ERROR("%p: queue index out of range (%u >= %u)",
195                       (void *)dev, idx, dev->data->nb_tx_queues);
196                 return -rte_errno;
197         }
198         txq = dev->data->tx_queues[idx];
199         if (txq) {
200                 rte_errno = EEXIST;
201                 DEBUG("%p: Tx queue %u already configured, release it first",
202                       (void *)dev, idx);
203                 return -rte_errno;
204         }
205         if (!desc) {
206                 rte_errno = EINVAL;
207                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
208                 return -rte_errno;
209         }
210         /* Allocate and initialize Tx queue. */
211         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
212         if (!txq) {
213                 ERROR("%p: unable to allocate queue index %u",
214                       (void *)dev, idx);
215                 return -rte_errno;
216         }
217         *txq = (struct txq){
218                 .priv = priv,
219                 .stats.idx = idx,
220                 .socket = socket,
221                 .elts_n = desc,
222                 .elts = elts,
223                 .elts_head = 0,
224                 .elts_tail = 0,
225                 .elts_comp = 0,
226                 /*
227                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
228                  * packets or at least 4 times per ring.
229                  */
230                 .elts_comp_cd =
231                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
232                 .elts_comp_cd_init =
233                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
234         };
235         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
236         if (!txq->cq) {
237                 rte_errno = ENOMEM;
238                 ERROR("%p: CQ creation failure: %s",
239                       (void *)dev, strerror(rte_errno));
240                 goto error;
241         }
242         qp_init_attr = (struct ibv_qp_init_attr){
243                 .send_cq = txq->cq,
244                 .recv_cq = txq->cq,
245                 .cap = {
246                         .max_send_wr =
247                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
248                         .max_send_sge = 1,
249                         .max_inline_data = MLX4_PMD_MAX_INLINE,
250                 },
251                 .qp_type = IBV_QPT_RAW_PACKET,
252                 /* No completion events must occur by default. */
253                 .sq_sig_all = 0,
254         };
255         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
256         if (!txq->qp) {
257                 rte_errno = errno ? errno : EINVAL;
258                 ERROR("%p: QP creation failure: %s",
259                       (void *)dev, strerror(rte_errno));
260                 goto error;
261         }
262         txq->max_inline = qp_init_attr.cap.max_inline_data;
263         ret = ibv_modify_qp
264                 (txq->qp,
265                  &(struct ibv_qp_attr){
266                         .qp_state = IBV_QPS_INIT,
267                         .port_num = priv->port,
268                  },
269                  IBV_QP_STATE | IBV_QP_PORT);
270         if (ret) {
271                 rte_errno = ret;
272                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
273                       (void *)dev, strerror(rte_errno));
274                 goto error;
275         }
276         ret = ibv_modify_qp
277                 (txq->qp,
278                  &(struct ibv_qp_attr){
279                         .qp_state = IBV_QPS_RTR,
280                  },
281                  IBV_QP_STATE);
282         if (ret) {
283                 rte_errno = ret;
284                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
285                       (void *)dev, strerror(rte_errno));
286                 goto error;
287         }
288         ret = ibv_modify_qp
289                 (txq->qp,
290                  &(struct ibv_qp_attr){
291                         .qp_state = IBV_QPS_RTS,
292                  },
293                  IBV_QP_STATE);
294         if (ret) {
295                 rte_errno = ret;
296                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
297                       (void *)dev, strerror(rte_errno));
298                 goto error;
299         }
300         /* Pre-register known mempools. */
301         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
302         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
303         dev->data->tx_queues[idx] = txq;
304         return 0;
305 error:
306         dev->data->tx_queues[idx] = NULL;
307         ret = rte_errno;
308         mlx4_tx_queue_release(txq);
309         rte_errno = ret;
310         assert(rte_errno > 0);
311         return -rte_errno;
312 }
313
314 /**
315  * DPDK callback to release a Tx queue.
316  *
317  * @param dpdk_txq
318  *   Generic Tx queue pointer.
319  */
320 void
321 mlx4_tx_queue_release(void *dpdk_txq)
322 {
323         struct txq *txq = (struct txq *)dpdk_txq;
324         struct priv *priv;
325         unsigned int i;
326
327         if (txq == NULL)
328                 return;
329         priv = txq->priv;
330         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
331                 if (priv->dev->data->tx_queues[i] == txq) {
332                         DEBUG("%p: removing Tx queue %p from list",
333                               (void *)priv->dev, (void *)txq);
334                         priv->dev->data->tx_queues[i] = NULL;
335                         break;
336                 }
337         mlx4_txq_free_elts(txq);
338         if (txq->qp)
339                 claim_zero(ibv_destroy_qp(txq->qp));
340         if (txq->cq)
341                 claim_zero(ibv_destroy_cq(txq->cq));
342         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
343                 if (!txq->mp2mr[i].mp)
344                         break;
345                 assert(txq->mp2mr[i].mr);
346                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
347         }
348         rte_free(txq);
349 }