7664c3e1aa7ac3064656db89cd5785bf3eace9dc
[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 #include <inttypes.h>
45
46 /* Verbs headers do not support -pedantic. */
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic ignored "-Wpedantic"
49 #endif
50 #include <infiniband/verbs.h>
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic error "-Wpedantic"
53 #endif
54
55 #include <rte_common.h>
56 #include <rte_errno.h>
57 #include <rte_ethdev.h>
58 #include <rte_malloc.h>
59 #include <rte_mbuf.h>
60 #include <rte_mempool.h>
61
62 #include "mlx4.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         unsigned int elts_m = txq->elts_n - 1;
80
81         DEBUG("%p: freeing WRs", (void *)txq);
82         while (elts_tail != elts_head) {
83                 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
84
85                 assert(elt->buf != NULL);
86                 rte_pktmbuf_free(elt->buf);
87                 elt->buf = NULL;
88                 elt->wqe = NULL;
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  * Returns the per-port supported offloads.
187  *
188  * @param priv
189  *   Pointer to private structure.
190  *
191  * @return
192  *   Supported Tx offloads.
193  */
194 uint64_t
195 mlx4_get_tx_port_offloads(struct priv *priv)
196 {
197         uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
198
199         if (priv->hw_csum) {
200                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
201                              DEV_TX_OFFLOAD_UDP_CKSUM |
202                              DEV_TX_OFFLOAD_TCP_CKSUM);
203         }
204         if (priv->hw_csum_l2tun)
205                 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
206         return offloads;
207 }
208
209 /**
210  * Checks if the per-queue offload configuration is valid.
211  *
212  * @param priv
213  *   Pointer to private structure.
214  * @param requested
215  *   Per-queue offloads configuration.
216  *
217  * @return
218  *   Nonzero when configuration is valid.
219  */
220 static int
221 mlx4_check_tx_queue_offloads(struct priv *priv, uint64_t requested)
222 {
223         uint64_t mandatory = priv->dev->data->dev_conf.txmode.offloads;
224         uint64_t supported = mlx4_get_tx_port_offloads(priv);
225
226         return !((mandatory ^ requested) & supported);
227 }
228
229 /**
230  * DPDK callback to configure a Tx queue.
231  *
232  * @param dev
233  *   Pointer to Ethernet device structure.
234  * @param idx
235  *   Tx queue index.
236  * @param desc
237  *   Number of descriptors to configure in queue.
238  * @param socket
239  *   NUMA socket on which memory must be allocated.
240  * @param[in] conf
241  *   Thresholds parameters.
242  *
243  * @return
244  *   0 on success, negative errno value otherwise and rte_errno is set.
245  */
246 int
247 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
248                     unsigned int socket, const struct rte_eth_txconf *conf)
249 {
250         struct priv *priv = dev->data->dev_private;
251         struct mlx4dv_obj mlxdv;
252         struct mlx4dv_qp dv_qp;
253         struct mlx4dv_cq dv_cq;
254         struct txq_elt (*elts)[rte_align32pow2(desc)];
255         struct ibv_qp_init_attr qp_init_attr;
256         struct txq *txq;
257         uint8_t *bounce_buf;
258         struct mlx4_malloc_vec vec[] = {
259                 {
260                         .align = RTE_CACHE_LINE_SIZE,
261                         .size = sizeof(*txq),
262                         .addr = (void **)&txq,
263                 },
264                 {
265                         .align = RTE_CACHE_LINE_SIZE,
266                         .size = sizeof(*elts),
267                         .addr = (void **)&elts,
268                 },
269                 {
270                         .align = RTE_CACHE_LINE_SIZE,
271                         .size = MLX4_MAX_WQE_SIZE,
272                         .addr = (void **)&bounce_buf,
273                 },
274         };
275         int ret;
276
277         DEBUG("%p: configuring queue %u for %u descriptors",
278               (void *)dev, idx, desc);
279         /*
280          * Don't verify port offloads for application which
281          * use the old API.
282          */
283         if ((conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) &&
284             !mlx4_check_tx_queue_offloads(priv, conf->offloads)) {
285                 rte_errno = ENOTSUP;
286                 ERROR("%p: Tx queue offloads 0x%" PRIx64 " don't match port "
287                       "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
288                       (void *)dev, conf->offloads,
289                       dev->data->dev_conf.txmode.offloads,
290                       mlx4_get_tx_port_offloads(priv));
291                 return -rte_errno;
292         }
293         if (idx >= dev->data->nb_tx_queues) {
294                 rte_errno = EOVERFLOW;
295                 ERROR("%p: queue index out of range (%u >= %u)",
296                       (void *)dev, idx, dev->data->nb_tx_queues);
297                 return -rte_errno;
298         }
299         txq = dev->data->tx_queues[idx];
300         if (txq) {
301                 rte_errno = EEXIST;
302                 DEBUG("%p: Tx queue %u already configured, release it first",
303                       (void *)dev, idx);
304                 return -rte_errno;
305         }
306         if (!desc) {
307                 rte_errno = EINVAL;
308                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
309                 return -rte_errno;
310         }
311         if (desc != RTE_DIM(*elts)) {
312                 desc = RTE_DIM(*elts);
313                 WARN("%p: increased number of descriptors in Tx queue %u"
314                      " to the next power of two (%u)",
315                      (void *)dev, idx, desc);
316         }
317         /* Allocate and initialize Tx queue. */
318         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
319         if (!txq) {
320                 ERROR("%p: unable to allocate queue index %u",
321                       (void *)dev, idx);
322                 return -rte_errno;
323         }
324         *txq = (struct txq){
325                 .priv = priv,
326                 .stats = {
327                         .idx = idx,
328                 },
329                 .socket = socket,
330                 .elts_n = desc,
331                 .elts = elts,
332                 .elts_head = 0,
333                 .elts_tail = 0,
334                 /*
335                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
336                  * packets or at least 4 times per ring.
337                  */
338                 .elts_comp_cd =
339                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
340                 .elts_comp_cd_init =
341                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
342                 .csum = priv->hw_csum &&
343                         (conf->offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
344                                            DEV_TX_OFFLOAD_UDP_CKSUM |
345                                            DEV_TX_OFFLOAD_TCP_CKSUM)),
346                 .csum_l2tun = priv->hw_csum_l2tun &&
347                               (conf->offloads &
348                                DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
349                 /* Enable Tx loopback for VF devices. */
350                 .lb = !!priv->vf,
351                 .bounce_buf = bounce_buf,
352         };
353         txq->cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
354         if (!txq->cq) {
355                 rte_errno = ENOMEM;
356                 ERROR("%p: CQ creation failure: %s",
357                       (void *)dev, strerror(rte_errno));
358                 goto error;
359         }
360         qp_init_attr = (struct ibv_qp_init_attr){
361                 .send_cq = txq->cq,
362                 .recv_cq = txq->cq,
363                 .cap = {
364                         .max_send_wr =
365                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
366                         .max_send_sge = 1,
367                         .max_inline_data = MLX4_PMD_MAX_INLINE,
368                 },
369                 .qp_type = IBV_QPT_RAW_PACKET,
370                 /* No completion events must occur by default. */
371                 .sq_sig_all = 0,
372         };
373         txq->qp = ibv_create_qp(priv->pd, &qp_init_attr);
374         if (!txq->qp) {
375                 rte_errno = errno ? errno : EINVAL;
376                 ERROR("%p: QP creation failure: %s",
377                       (void *)dev, strerror(rte_errno));
378                 goto error;
379         }
380         txq->max_inline = qp_init_attr.cap.max_inline_data;
381         ret = ibv_modify_qp
382                 (txq->qp,
383                  &(struct ibv_qp_attr){
384                         .qp_state = IBV_QPS_INIT,
385                         .port_num = priv->port,
386                  },
387                  IBV_QP_STATE | IBV_QP_PORT);
388         if (ret) {
389                 rte_errno = ret;
390                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
391                       (void *)dev, strerror(rte_errno));
392                 goto error;
393         }
394         ret = ibv_modify_qp
395                 (txq->qp,
396                  &(struct ibv_qp_attr){
397                         .qp_state = IBV_QPS_RTR,
398                  },
399                  IBV_QP_STATE);
400         if (ret) {
401                 rte_errno = ret;
402                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
403                       (void *)dev, strerror(rte_errno));
404                 goto error;
405         }
406         ret = ibv_modify_qp
407                 (txq->qp,
408                  &(struct ibv_qp_attr){
409                         .qp_state = IBV_QPS_RTS,
410                  },
411                  IBV_QP_STATE);
412         if (ret) {
413                 rte_errno = ret;
414                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
415                       (void *)dev, strerror(rte_errno));
416                 goto error;
417         }
418         /* Retrieve device queue information. */
419         mlxdv.cq.in = txq->cq;
420         mlxdv.cq.out = &dv_cq;
421         mlxdv.qp.in = txq->qp;
422         mlxdv.qp.out = &dv_qp;
423         ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
424         if (ret) {
425                 rte_errno = EINVAL;
426                 ERROR("%p: failed to obtain information needed for"
427                       " accessing the device queues", (void *)dev);
428                 goto error;
429         }
430         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
431         /* Save first wqe pointer in the first element. */
432         (&(*txq->elts)[0])->wqe =
433                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
434         /* Pre-register known mempools. */
435         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
436         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
437         dev->data->tx_queues[idx] = txq;
438         return 0;
439 error:
440         dev->data->tx_queues[idx] = NULL;
441         ret = rte_errno;
442         mlx4_tx_queue_release(txq);
443         rte_errno = ret;
444         assert(rte_errno > 0);
445         return -rte_errno;
446 }
447
448 /**
449  * DPDK callback to release a Tx queue.
450  *
451  * @param dpdk_txq
452  *   Generic Tx queue pointer.
453  */
454 void
455 mlx4_tx_queue_release(void *dpdk_txq)
456 {
457         struct txq *txq = (struct txq *)dpdk_txq;
458         struct priv *priv;
459         unsigned int i;
460
461         if (txq == NULL)
462                 return;
463         priv = txq->priv;
464         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
465                 if (priv->dev->data->tx_queues[i] == txq) {
466                         DEBUG("%p: removing Tx queue %p from list",
467                               (void *)priv->dev, (void *)txq);
468                         priv->dev->data->tx_queues[i] = NULL;
469                         break;
470                 }
471         mlx4_txq_free_elts(txq);
472         if (txq->qp)
473                 claim_zero(ibv_destroy_qp(txq->qp));
474         if (txq->cq)
475                 claim_zero(ibv_destroy_cq(txq->cq));
476         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
477                 if (!txq->mp2mr[i].mp)
478                         break;
479                 assert(txq->mp2mr[i].mr);
480                 mlx4_mr_put(txq->mp2mr[i].mr);
481         }
482         rte_free(txq);
483 }