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