net/mlx4: drop live queue reconfiguration support
[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 struct txq_mp2mr_mbuf_check_data {
159         int ret;
160 };
161
162 /**
163  * Callback function for rte_mempool_obj_iter() to check whether a given
164  * mempool object looks like a mbuf.
165  *
166  * @param[in] mp
167  *   The mempool pointer
168  * @param[in] arg
169  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
170  *   return value.
171  * @param[in] obj
172  *   Object address.
173  * @param index
174  *   Object index, unused.
175  */
176 static void
177 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
178                           uint32_t index)
179 {
180         struct txq_mp2mr_mbuf_check_data *data = arg;
181         struct rte_mbuf *buf = obj;
182
183         (void)index;
184         /*
185          * Check whether mbuf structure fits element size and whether mempool
186          * pointer is valid.
187          */
188         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
189                 data->ret = -1;
190 }
191
192 /**
193  * Iterator function for rte_mempool_walk() to register existing mempools and
194  * fill the MP to MR cache of a Tx queue.
195  *
196  * @param[in] mp
197  *   Memory Pool to register.
198  * @param *arg
199  *   Pointer to Tx queue structure.
200  */
201 static void
202 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
203 {
204         struct txq *txq = arg;
205         struct txq_mp2mr_mbuf_check_data data = {
206                 .ret = 0,
207         };
208
209         /* Register mempool only if the first element looks like a mbuf. */
210         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
211                         data.ret == -1)
212                 return;
213         mlx4_txq_mp2mr(txq, mp);
214 }
215
216 /**
217  * DPDK callback to configure a Tx queue.
218  *
219  * @param dev
220  *   Pointer to Ethernet device structure.
221  * @param idx
222  *   Tx queue index.
223  * @param desc
224  *   Number of descriptors to configure in queue.
225  * @param socket
226  *   NUMA socket on which memory must be allocated.
227  * @param[in] conf
228  *   Thresholds parameters.
229  *
230  * @return
231  *   0 on success, negative errno value otherwise and rte_errno is set.
232  */
233 int
234 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
235                     unsigned int socket, const struct rte_eth_txconf *conf)
236 {
237         struct priv *priv = dev->data->dev_private;
238         struct ibv_qp_init_attr qp_init_attr;
239         struct txq *txq;
240         int ret;
241
242         (void)conf; /* Thresholds configuration (ignored). */
243         DEBUG("%p: configuring queue %u for %u descriptors",
244               (void *)dev, idx, desc);
245         if (idx >= dev->data->nb_tx_queues) {
246                 rte_errno = EOVERFLOW;
247                 ERROR("%p: queue index out of range (%u >= %u)",
248                       (void *)dev, idx, dev->data->nb_tx_queues);
249                 return -rte_errno;
250         }
251         txq = dev->data->tx_queues[idx];
252         if (txq) {
253                 rte_errno = EEXIST;
254                 DEBUG("%p: Tx queue %u already configured, release it first",
255                       (void *)dev, idx);
256                 return -rte_errno;
257         }
258         if (!desc) {
259                 rte_errno = EINVAL;
260                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
261                 return -rte_errno;
262         }
263         /* Allocate and initialize Tx queue. */
264         txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
265         if (!txq) {
266                 rte_errno = ENOMEM;
267                 ERROR("%p: unable to allocate queue index %u",
268                       (void *)dev, idx);
269                 return -rte_errno;
270         }
271         *txq = (struct txq){
272                 .priv = priv,
273                 .stats.idx = idx,
274                 .socket = socket,
275         };
276         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
277         if (!txq->cq) {
278                 rte_errno = ENOMEM;
279                 ERROR("%p: CQ creation failure: %s",
280                       (void *)dev, strerror(rte_errno));
281                 goto error;
282         }
283         qp_init_attr = (struct ibv_qp_init_attr){
284                 .send_cq = txq->cq,
285                 .recv_cq = txq->cq,
286                 .cap = {
287                         .max_send_wr =
288                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
289                         .max_send_sge = 1,
290                         .max_inline_data = MLX4_PMD_MAX_INLINE,
291                 },
292                 .qp_type = IBV_QPT_RAW_PACKET,
293                 /* No completion events must occur by default. */
294                 .sq_sig_all = 0,
295         };
296         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
297         if (!txq->qp) {
298                 rte_errno = errno ? errno : EINVAL;
299                 ERROR("%p: QP creation failure: %s",
300                       (void *)dev, strerror(rte_errno));
301                 goto error;
302         }
303         txq->max_inline = qp_init_attr.cap.max_inline_data;
304         ret = ibv_modify_qp
305                 (txq->qp,
306                  &(struct ibv_qp_attr){
307                         .qp_state = IBV_QPS_INIT,
308                         .port_num = priv->port,
309                  },
310                  IBV_QP_STATE | IBV_QP_PORT);
311         if (ret) {
312                 rte_errno = ret;
313                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
314                       (void *)dev, strerror(rte_errno));
315                 goto error;
316         }
317         ret = mlx4_txq_alloc_elts(txq, desc);
318         if (ret) {
319                 ERROR("%p: TXQ allocation failed: %s",
320                       (void *)dev, strerror(rte_errno));
321                 goto error;
322         }
323         ret = ibv_modify_qp
324                 (txq->qp,
325                  &(struct ibv_qp_attr){
326                         .qp_state = IBV_QPS_RTR,
327                  },
328                  IBV_QP_STATE);
329         if (ret) {
330                 rte_errno = ret;
331                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
332                       (void *)dev, strerror(rte_errno));
333                 goto error;
334         }
335         ret = ibv_modify_qp
336                 (txq->qp,
337                  &(struct ibv_qp_attr){
338                         .qp_state = IBV_QPS_RTS,
339                  },
340                  IBV_QP_STATE);
341         if (ret) {
342                 rte_errno = ret;
343                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
344                       (void *)dev, strerror(rte_errno));
345                 goto error;
346         }
347         /* Pre-register known mempools. */
348         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
349         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
350         dev->data->tx_queues[idx] = txq;
351         return 0;
352 error:
353         dev->data->tx_queues[idx] = NULL;
354         ret = rte_errno;
355         mlx4_tx_queue_release(txq);
356         rte_errno = ret;
357         assert(rte_errno > 0);
358         return -rte_errno;
359 }
360
361 /**
362  * DPDK callback to release a Tx queue.
363  *
364  * @param dpdk_txq
365  *   Generic Tx queue pointer.
366  */
367 void
368 mlx4_tx_queue_release(void *dpdk_txq)
369 {
370         struct txq *txq = (struct txq *)dpdk_txq;
371         struct priv *priv;
372         unsigned int i;
373
374         if (txq == NULL)
375                 return;
376         priv = txq->priv;
377         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
378                 if (priv->dev->data->tx_queues[i] == txq) {
379                         DEBUG("%p: removing Tx queue %p from list",
380                               (void *)priv->dev, (void *)txq);
381                         priv->dev->data->tx_queues[i] = NULL;
382                         break;
383                 }
384         mlx4_txq_free_elts(txq);
385         if (txq->qp)
386                 claim_zero(ibv_destroy_qp(txq->qp));
387         if (txq->cq)
388                 claim_zero(ibv_destroy_cq(txq->cq));
389         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
390                 if (!txq->mp2mr[i].mp)
391                         break;
392                 assert(txq->mp2mr[i].mr);
393                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
394         }
395         rte_free(txq);
396 }