net/mlx4: align Tx descriptors number
[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                 .elts_comp = 0,
277                 /*
278                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
279                  * packets or at least 4 times per ring.
280                  */
281                 .elts_comp_cd =
282                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
283                 .elts_comp_cd_init =
284                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
285                 .csum = priv->hw_csum,
286                 .csum_l2tun = priv->hw_csum_l2tun,
287                 /* Enable Tx loopback for VF devices. */
288                 .lb = !!priv->vf,
289                 .bounce_buf = bounce_buf,
290         };
291         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
292         if (!txq->cq) {
293                 rte_errno = ENOMEM;
294                 ERROR("%p: CQ creation failure: %s",
295                       (void *)dev, strerror(rte_errno));
296                 goto error;
297         }
298         qp_init_attr = (struct ibv_qp_init_attr){
299                 .send_cq = txq->cq,
300                 .recv_cq = txq->cq,
301                 .cap = {
302                         .max_send_wr =
303                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
304                         .max_send_sge = 1,
305                         .max_inline_data = MLX4_PMD_MAX_INLINE,
306                 },
307                 .qp_type = IBV_QPT_RAW_PACKET,
308                 /* No completion events must occur by default. */
309                 .sq_sig_all = 0,
310         };
311         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
312         if (!txq->qp) {
313                 rte_errno = errno ? errno : EINVAL;
314                 ERROR("%p: QP creation failure: %s",
315                       (void *)dev, strerror(rte_errno));
316                 goto error;
317         }
318         txq->max_inline = qp_init_attr.cap.max_inline_data;
319         ret = ibv_modify_qp
320                 (txq->qp,
321                  &(struct ibv_qp_attr){
322                         .qp_state = IBV_QPS_INIT,
323                         .port_num = priv->port,
324                  },
325                  IBV_QP_STATE | IBV_QP_PORT);
326         if (ret) {
327                 rte_errno = ret;
328                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
329                       (void *)dev, strerror(rte_errno));
330                 goto error;
331         }
332         ret = ibv_modify_qp
333                 (txq->qp,
334                  &(struct ibv_qp_attr){
335                         .qp_state = IBV_QPS_RTR,
336                  },
337                  IBV_QP_STATE);
338         if (ret) {
339                 rte_errno = ret;
340                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
341                       (void *)dev, strerror(rte_errno));
342                 goto error;
343         }
344         ret = ibv_modify_qp
345                 (txq->qp,
346                  &(struct ibv_qp_attr){
347                         .qp_state = IBV_QPS_RTS,
348                  },
349                  IBV_QP_STATE);
350         if (ret) {
351                 rte_errno = ret;
352                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
353                       (void *)dev, strerror(rte_errno));
354                 goto error;
355         }
356         /* Retrieve device queue information. */
357         mlxdv.cq.in = txq->cq;
358         mlxdv.cq.out = &dv_cq;
359         mlxdv.qp.in = txq->qp;
360         mlxdv.qp.out = &dv_qp;
361         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
362         if (ret) {
363                 rte_errno = EINVAL;
364                 ERROR("%p: failed to obtain information needed for"
365                       " accessing the device queues", (void *)dev);
366                 goto error;
367         }
368         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
369         /* Save first wqe pointer in the first element. */
370         (&(*txq->elts)[0])->wqe =
371                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
372         /* Pre-register known mempools. */
373         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
374         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
375         dev->data->tx_queues[idx] = txq;
376         return 0;
377 error:
378         dev->data->tx_queues[idx] = NULL;
379         ret = rte_errno;
380         mlx4_tx_queue_release(txq);
381         rte_errno = ret;
382         assert(rte_errno > 0);
383         return -rte_errno;
384 }
385
386 /**
387  * DPDK callback to release a Tx queue.
388  *
389  * @param dpdk_txq
390  *   Generic Tx queue pointer.
391  */
392 void
393 mlx4_tx_queue_release(void *dpdk_txq)
394 {
395         struct txq *txq = (struct txq *)dpdk_txq;
396         struct priv *priv;
397         unsigned int i;
398
399         if (txq == NULL)
400                 return;
401         priv = txq->priv;
402         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
403                 if (priv->dev->data->tx_queues[i] == txq) {
404                         DEBUG("%p: removing Tx queue %p from list",
405                               (void *)priv->dev, (void *)txq);
406                         priv->dev->data->tx_queues[i] = NULL;
407                         break;
408                 }
409         mlx4_txq_free_elts(txq);
410         if (txq->qp)
411                 claim_zero(ibv_destroy_qp(txq->qp));
412         if (txq->cq)
413                 claim_zero(ibv_destroy_cq(txq->cq));
414         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
415                 if (!txq->mp2mr[i].mp)
416                         break;
417                 assert(txq->mp2mr[i].mr);
418                 mlx4_mr_put(txq->mp2mr[i].mr);
419         }
420         rte_free(txq);
421 }