7882a4d0b5fc9ee7f4049a1efcbab23a4f32d287
[deb_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_prm.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
66
67 /**
68  * Free Tx queue elements.
69  *
70  * @param txq
71  *   Pointer to Tx queue structure.
72  */
73 static void
74 mlx4_txq_free_elts(struct txq *txq)
75 {
76         unsigned int elts_head = txq->elts_head;
77         unsigned int elts_tail = txq->elts_tail;
78         struct txq_elt (*elts)[txq->elts_n] = txq->elts;
79
80         DEBUG("%p: freeing WRs", (void *)txq);
81         while (elts_tail != elts_head) {
82                 struct txq_elt *elt = &(*elts)[elts_tail];
83
84                 assert(elt->buf != NULL);
85                 rte_pktmbuf_free(elt->buf);
86                 elt->buf = NULL;
87                 if (++elts_tail == RTE_DIM(*elts))
88                         elts_tail = 0;
89         }
90         txq->elts_tail = txq->elts_head;
91 }
92
93 struct txq_mp2mr_mbuf_check_data {
94         int ret;
95 };
96
97 /**
98  * Callback function for rte_mempool_obj_iter() to check whether a given
99  * mempool object looks like a mbuf.
100  *
101  * @param[in] mp
102  *   The mempool pointer
103  * @param[in] arg
104  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
105  *   return value.
106  * @param[in] obj
107  *   Object address.
108  * @param index
109  *   Object index, unused.
110  */
111 static void
112 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
113                           uint32_t index)
114 {
115         struct txq_mp2mr_mbuf_check_data *data = arg;
116         struct rte_mbuf *buf = obj;
117
118         (void)index;
119         /*
120          * Check whether mbuf structure fits element size and whether mempool
121          * pointer is valid.
122          */
123         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
124                 data->ret = -1;
125 }
126
127 /**
128  * Iterator function for rte_mempool_walk() to register existing mempools and
129  * fill the MP to MR cache of a Tx queue.
130  *
131  * @param[in] mp
132  *   Memory Pool to register.
133  * @param *arg
134  *   Pointer to Tx queue structure.
135  */
136 static void
137 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
138 {
139         struct txq *txq = arg;
140         struct txq_mp2mr_mbuf_check_data data = {
141                 .ret = 0,
142         };
143
144         /* Register mempool only if the first element looks like a mbuf. */
145         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
146                         data.ret == -1)
147                 return;
148         mlx4_txq_mp2mr(txq, mp);
149 }
150
151 /**
152  * Retrieves information needed in order to directly access the Tx queue.
153  *
154  * @param txq
155  *   Pointer to Tx queue structure.
156  * @param mlxdv
157  *   Pointer to device information for this Tx queue.
158  */
159 static void
160 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
161 {
162         struct mlx4_sq *sq = &txq->msq;
163         struct mlx4_cq *cq = &txq->mcq;
164         struct mlx4dv_qp *dqp = mlxdv->qp.out;
165         struct mlx4dv_cq *dcq = mlxdv->cq.out;
166         uint32_t sq_size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
167
168         sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
169         /* Total length, including headroom and spare WQEs. */
170         sq->eob = sq->buf + sq_size;
171         sq->head = 0;
172         sq->tail = 0;
173         sq->txbb_cnt =
174                 (dqp->sq.wqe_cnt << dqp->sq.wqe_shift) >> MLX4_TXBB_SHIFT;
175         sq->txbb_cnt_mask = sq->txbb_cnt - 1;
176         sq->db = dqp->sdb;
177         sq->doorbell_qpn = dqp->doorbell_qpn;
178         sq->headroom_txbbs =
179                 (2048 + (1 << dqp->sq.wqe_shift)) >> MLX4_TXBB_SHIFT;
180         cq->buf = dcq->buf.buf;
181         cq->cqe_cnt = dcq->cqe_cnt;
182         cq->set_ci_db = dcq->set_ci_db;
183         cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
184 }
185
186 /**
187  * DPDK callback to configure a Tx queue.
188  *
189  * @param dev
190  *   Pointer to Ethernet device structure.
191  * @param idx
192  *   Tx queue index.
193  * @param desc
194  *   Number of descriptors to configure in queue.
195  * @param socket
196  *   NUMA socket on which memory must be allocated.
197  * @param[in] conf
198  *   Thresholds parameters.
199  *
200  * @return
201  *   0 on success, negative errno value otherwise and rte_errno is set.
202  */
203 int
204 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
205                     unsigned int socket, const struct rte_eth_txconf *conf)
206 {
207         struct priv *priv = dev->data->dev_private;
208         struct mlx4dv_obj mlxdv;
209         struct mlx4dv_qp dv_qp;
210         struct mlx4dv_cq dv_cq;
211         struct txq_elt (*elts)[desc];
212         struct ibv_qp_init_attr qp_init_attr;
213         struct txq *txq;
214         uint8_t *bounce_buf;
215         struct mlx4_malloc_vec vec[] = {
216                 {
217                         .align = RTE_CACHE_LINE_SIZE,
218                         .size = sizeof(*txq),
219                         .addr = (void **)&txq,
220                 },
221                 {
222                         .align = RTE_CACHE_LINE_SIZE,
223                         .size = sizeof(*elts),
224                         .addr = (void **)&elts,
225                 },
226                 {
227                         .align = RTE_CACHE_LINE_SIZE,
228                         .size = MLX4_MAX_WQE_SIZE,
229                         .addr = (void **)&bounce_buf,
230                 },
231         };
232         int ret;
233
234         (void)conf; /* Thresholds configuration (ignored). */
235         DEBUG("%p: configuring queue %u for %u descriptors",
236               (void *)dev, idx, desc);
237         if (idx >= dev->data->nb_tx_queues) {
238                 rte_errno = EOVERFLOW;
239                 ERROR("%p: queue index out of range (%u >= %u)",
240                       (void *)dev, idx, dev->data->nb_tx_queues);
241                 return -rte_errno;
242         }
243         txq = dev->data->tx_queues[idx];
244         if (txq) {
245                 rte_errno = EEXIST;
246                 DEBUG("%p: Tx queue %u already configured, release it first",
247                       (void *)dev, idx);
248                 return -rte_errno;
249         }
250         if (!desc) {
251                 rte_errno = EINVAL;
252                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
253                 return -rte_errno;
254         }
255         /* Allocate and initialize Tx queue. */
256         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
257         if (!txq) {
258                 ERROR("%p: unable to allocate queue index %u",
259                       (void *)dev, idx);
260                 return -rte_errno;
261         }
262         *txq = (struct txq){
263                 .priv = priv,
264                 .stats = {
265                         .idx = idx,
266                 },
267                 .socket = socket,
268                 .elts_n = desc,
269                 .elts = elts,
270                 .elts_head = 0,
271                 .elts_tail = 0,
272                 .elts_comp = 0,
273                 /*
274                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
275                  * packets or at least 4 times per ring.
276                  */
277                 .elts_comp_cd =
278                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
279                 .elts_comp_cd_init =
280                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
281                 .csum = priv->hw_csum,
282                 .csum_l2tun = priv->hw_csum_l2tun,
283                 /* Enable Tx loopback for VF devices. */
284                 .lb = !!priv->vf,
285                 .bounce_buf = bounce_buf,
286         };
287         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
288         if (!txq->cq) {
289                 rte_errno = ENOMEM;
290                 ERROR("%p: CQ creation failure: %s",
291                       (void *)dev, strerror(rte_errno));
292                 goto error;
293         }
294         qp_init_attr = (struct ibv_qp_init_attr){
295                 .send_cq = txq->cq,
296                 .recv_cq = txq->cq,
297                 .cap = {
298                         .max_send_wr =
299                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
300                         .max_send_sge = 1,
301                         .max_inline_data = MLX4_PMD_MAX_INLINE,
302                 },
303                 .qp_type = IBV_QPT_RAW_PACKET,
304                 /* No completion events must occur by default. */
305                 .sq_sig_all = 0,
306         };
307         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
308         if (!txq->qp) {
309                 rte_errno = errno ? errno : EINVAL;
310                 ERROR("%p: QP creation failure: %s",
311                       (void *)dev, strerror(rte_errno));
312                 goto error;
313         }
314         txq->max_inline = qp_init_attr.cap.max_inline_data;
315         ret = ibv_modify_qp
316                 (txq->qp,
317                  &(struct ibv_qp_attr){
318                         .qp_state = IBV_QPS_INIT,
319                         .port_num = priv->port,
320                  },
321                  IBV_QP_STATE | IBV_QP_PORT);
322         if (ret) {
323                 rte_errno = ret;
324                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
325                       (void *)dev, strerror(rte_errno));
326                 goto error;
327         }
328         ret = ibv_modify_qp
329                 (txq->qp,
330                  &(struct ibv_qp_attr){
331                         .qp_state = IBV_QPS_RTR,
332                  },
333                  IBV_QP_STATE);
334         if (ret) {
335                 rte_errno = ret;
336                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
337                       (void *)dev, strerror(rte_errno));
338                 goto error;
339         }
340         ret = ibv_modify_qp
341                 (txq->qp,
342                  &(struct ibv_qp_attr){
343                         .qp_state = IBV_QPS_RTS,
344                  },
345                  IBV_QP_STATE);
346         if (ret) {
347                 rte_errno = ret;
348                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
349                       (void *)dev, strerror(rte_errno));
350                 goto error;
351         }
352         /* Retrieve device queue information. */
353         mlxdv.cq.in = txq->cq;
354         mlxdv.cq.out = &dv_cq;
355         mlxdv.qp.in = txq->qp;
356         mlxdv.qp.out = &dv_qp;
357         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
358         if (ret) {
359                 rte_errno = EINVAL;
360                 ERROR("%p: failed to obtain information needed for"
361                       " accessing the device queues", (void *)dev);
362                 goto error;
363         }
364         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
365         /* Pre-register known mempools. */
366         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
367         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
368         dev->data->tx_queues[idx] = txq;
369         return 0;
370 error:
371         dev->data->tx_queues[idx] = NULL;
372         ret = rte_errno;
373         mlx4_tx_queue_release(txq);
374         rte_errno = ret;
375         assert(rte_errno > 0);
376         return -rte_errno;
377 }
378
379 /**
380  * DPDK callback to release a Tx queue.
381  *
382  * @param dpdk_txq
383  *   Generic Tx queue pointer.
384  */
385 void
386 mlx4_tx_queue_release(void *dpdk_txq)
387 {
388         struct txq *txq = (struct txq *)dpdk_txq;
389         struct priv *priv;
390         unsigned int i;
391
392         if (txq == NULL)
393                 return;
394         priv = txq->priv;
395         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
396                 if (priv->dev->data->tx_queues[i] == txq) {
397                         DEBUG("%p: removing Tx queue %p from list",
398                               (void *)priv->dev, (void *)txq);
399                         priv->dev->data->tx_queues[i] = NULL;
400                         break;
401                 }
402         mlx4_txq_free_elts(txq);
403         if (txq->qp)
404                 claim_zero(ibv_destroy_qp(txq->qp));
405         if (txq->cq)
406                 claim_zero(ibv_destroy_cq(txq->cq));
407         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
408                 if (!txq->mp2mr[i].mp)
409                         break;
410                 assert(txq->mp2mr[i].mr);
411                 mlx4_mr_put(txq->mp2mr[i].mr);
412         }
413         rte_free(txq);
414 }