d651e49800018e08759dce9290abaf777ca3c626
[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_prm.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         unsigned int elts_m = txq->elts_n - 1;
79
80         DEBUG("%p: freeing WRs", (void *)txq);
81         while (elts_tail != elts_head) {
82                 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
83
84                 assert(elt->buf != NULL);
85                 rte_pktmbuf_free(elt->buf);
86                 elt->buf = NULL;
87                 elt->wqe = NULL;
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  * Retrieves information needed in order to directly access the Tx queue.
152  *
153  * @param txq
154  *   Pointer to Tx queue structure.
155  * @param mlxdv
156  *   Pointer to device information for this Tx queue.
157  */
158 static void
159 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
160 {
161         struct mlx4_sq *sq = &txq->msq;
162         struct mlx4_cq *cq = &txq->mcq;
163         struct mlx4dv_qp *dqp = mlxdv->qp.out;
164         struct mlx4dv_cq *dcq = mlxdv->cq.out;
165
166         /* Total length, including headroom and spare WQEs. */
167         sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
168         sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
169         sq->eob = sq->buf + sq->size;
170         uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
171         /* Continuous headroom size bytes must always stay freed. */
172         sq->remain_size = sq->size - headroom_size;
173         sq->owner_opcode = MLX4_OPCODE_SEND | (0 << MLX4_SQ_OWNER_BIT);
174         sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
175                                      (0 << MLX4_SQ_OWNER_BIT));
176         sq->db = dqp->sdb;
177         sq->doorbell_qpn = dqp->doorbell_qpn;
178         cq->buf = dcq->buf.buf;
179         cq->cqe_cnt = dcq->cqe_cnt;
180         cq->set_ci_db = dcq->set_ci_db;
181         cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
182 }
183
184 /**
185  * DPDK callback to configure a Tx queue.
186  *
187  * @param dev
188  *   Pointer to Ethernet device structure.
189  * @param idx
190  *   Tx queue index.
191  * @param desc
192  *   Number of descriptors to configure in queue.
193  * @param socket
194  *   NUMA socket on which memory must be allocated.
195  * @param[in] conf
196  *   Thresholds parameters.
197  *
198  * @return
199  *   0 on success, negative errno value otherwise and rte_errno is set.
200  */
201 int
202 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
203                     unsigned int socket, const struct rte_eth_txconf *conf)
204 {
205         struct priv *priv = dev->data->dev_private;
206         struct mlx4dv_obj mlxdv;
207         struct mlx4dv_qp dv_qp;
208         struct mlx4dv_cq dv_cq;
209         struct txq_elt (*elts)[rte_align32pow2(desc)];
210         struct ibv_qp_init_attr qp_init_attr;
211         struct txq *txq;
212         uint8_t *bounce_buf;
213         struct mlx4_malloc_vec vec[] = {
214                 {
215                         .align = RTE_CACHE_LINE_SIZE,
216                         .size = sizeof(*txq),
217                         .addr = (void **)&txq,
218                 },
219                 {
220                         .align = RTE_CACHE_LINE_SIZE,
221                         .size = sizeof(*elts),
222                         .addr = (void **)&elts,
223                 },
224                 {
225                         .align = RTE_CACHE_LINE_SIZE,
226                         .size = MLX4_MAX_WQE_SIZE,
227                         .addr = (void **)&bounce_buf,
228                 },
229         };
230         int ret;
231
232         (void)conf; /* Thresholds configuration (ignored). */
233         DEBUG("%p: configuring queue %u for %u descriptors",
234               (void *)dev, idx, desc);
235         if (idx >= dev->data->nb_tx_queues) {
236                 rte_errno = EOVERFLOW;
237                 ERROR("%p: queue index out of range (%u >= %u)",
238                       (void *)dev, idx, dev->data->nb_tx_queues);
239                 return -rte_errno;
240         }
241         txq = dev->data->tx_queues[idx];
242         if (txq) {
243                 rte_errno = EEXIST;
244                 DEBUG("%p: Tx queue %u already configured, release it first",
245                       (void *)dev, idx);
246                 return -rte_errno;
247         }
248         if (!desc) {
249                 rte_errno = EINVAL;
250                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
251                 return -rte_errno;
252         }
253         if (desc != RTE_DIM(*elts)) {
254                 desc = RTE_DIM(*elts);
255                 WARN("%p: increased number of descriptors in Tx queue %u"
256                      " to the next power of two (%u)",
257                      (void *)dev, idx, desc);
258         }
259         /* Allocate and initialize Tx queue. */
260         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
261         if (!txq) {
262                 ERROR("%p: unable to allocate queue index %u",
263                       (void *)dev, idx);
264                 return -rte_errno;
265         }
266         *txq = (struct txq){
267                 .priv = priv,
268                 .stats = {
269                         .idx = idx,
270                 },
271                 .socket = socket,
272                 .elts_n = desc,
273                 .elts = elts,
274                 .elts_head = 0,
275                 .elts_tail = 0,
276                 /*
277                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
278                  * packets or at least 4 times per ring.
279                  */
280                 .elts_comp_cd =
281                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
282                 .elts_comp_cd_init =
283                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
284                 .csum = priv->hw_csum,
285                 .csum_l2tun = priv->hw_csum_l2tun,
286                 /* Enable Tx loopback for VF devices. */
287                 .lb = !!priv->vf,
288                 .bounce_buf = bounce_buf,
289         };
290         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
291         if (!txq->cq) {
292                 rte_errno = ENOMEM;
293                 ERROR("%p: CQ creation failure: %s",
294                       (void *)dev, strerror(rte_errno));
295                 goto error;
296         }
297         qp_init_attr = (struct ibv_qp_init_attr){
298                 .send_cq = txq->cq,
299                 .recv_cq = txq->cq,
300                 .cap = {
301                         .max_send_wr =
302                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
303                         .max_send_sge = 1,
304                         .max_inline_data = MLX4_PMD_MAX_INLINE,
305                 },
306                 .qp_type = IBV_QPT_RAW_PACKET,
307                 /* No completion events must occur by default. */
308                 .sq_sig_all = 0,
309         };
310         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
311         if (!txq->qp) {
312                 rte_errno = errno ? errno : EINVAL;
313                 ERROR("%p: QP creation failure: %s",
314                       (void *)dev, strerror(rte_errno));
315                 goto error;
316         }
317         txq->max_inline = qp_init_attr.cap.max_inline_data;
318         ret = ibv_modify_qp
319                 (txq->qp,
320                  &(struct ibv_qp_attr){
321                         .qp_state = IBV_QPS_INIT,
322                         .port_num = priv->port,
323                  },
324                  IBV_QP_STATE | IBV_QP_PORT);
325         if (ret) {
326                 rte_errno = ret;
327                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
328                       (void *)dev, strerror(rte_errno));
329                 goto error;
330         }
331         ret = ibv_modify_qp
332                 (txq->qp,
333                  &(struct ibv_qp_attr){
334                         .qp_state = IBV_QPS_RTR,
335                  },
336                  IBV_QP_STATE);
337         if (ret) {
338                 rte_errno = ret;
339                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
340                       (void *)dev, strerror(rte_errno));
341                 goto error;
342         }
343         ret = ibv_modify_qp
344                 (txq->qp,
345                  &(struct ibv_qp_attr){
346                         .qp_state = IBV_QPS_RTS,
347                  },
348                  IBV_QP_STATE);
349         if (ret) {
350                 rte_errno = ret;
351                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
352                       (void *)dev, strerror(rte_errno));
353                 goto error;
354         }
355         /* Retrieve device queue information. */
356         mlxdv.cq.in = txq->cq;
357         mlxdv.cq.out = &dv_cq;
358         mlxdv.qp.in = txq->qp;
359         mlxdv.qp.out = &dv_qp;
360         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
361         if (ret) {
362                 rte_errno = EINVAL;
363                 ERROR("%p: failed to obtain information needed for"
364                       " accessing the device queues", (void *)dev);
365                 goto error;
366         }
367         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
368         /* Save first wqe pointer in the first element. */
369         (&(*txq->elts)[0])->wqe =
370                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
371         /* Pre-register known mempools. */
372         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
373         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
374         dev->data->tx_queues[idx] = txq;
375         return 0;
376 error:
377         dev->data->tx_queues[idx] = NULL;
378         ret = rte_errno;
379         mlx4_tx_queue_release(txq);
380         rte_errno = ret;
381         assert(rte_errno > 0);
382         return -rte_errno;
383 }
384
385 /**
386  * DPDK callback to release a Tx queue.
387  *
388  * @param dpdk_txq
389  *   Generic Tx queue pointer.
390  */
391 void
392 mlx4_tx_queue_release(void *dpdk_txq)
393 {
394         struct txq *txq = (struct txq *)dpdk_txq;
395         struct priv *priv;
396         unsigned int i;
397
398         if (txq == NULL)
399                 return;
400         priv = txq->priv;
401         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
402                 if (priv->dev->data->tx_queues[i] == txq) {
403                         DEBUG("%p: removing Tx queue %p from list",
404                               (void *)priv->dev, (void *)txq);
405                         priv->dev->data->tx_queues[i] = NULL;
406                         break;
407                 }
408         mlx4_txq_free_elts(txq);
409         if (txq->qp)
410                 claim_zero(ibv_destroy_qp(txq->qp));
411         if (txq->cq)
412                 claim_zero(ibv_destroy_cq(txq->cq));
413         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
414                 if (!txq->mp2mr[i].mp)
415                         break;
416                 assert(txq->mp2mr[i].mr);
417                 mlx4_mr_put(txq->mp2mr[i].mr);
418         }
419         rte_free(txq);
420 }