net/mlx4: move rdma-core calls to separate file
[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_driver.h>
58 #include <rte_malloc.h>
59 #include <rte_mbuf.h>
60 #include <rte_mempool.h>
61
62 #include "mlx4.h"
63 #include "mlx4_glue.h"
64 #include "mlx4_prm.h"
65 #include "mlx4_rxtx.h"
66 #include "mlx4_utils.h"
67
68 /**
69  * Free Tx queue elements.
70  *
71  * @param txq
72  *   Pointer to Tx queue structure.
73  */
74 static void
75 mlx4_txq_free_elts(struct txq *txq)
76 {
77         unsigned int elts_head = txq->elts_head;
78         unsigned int elts_tail = txq->elts_tail;
79         struct txq_elt (*elts)[txq->elts_n] = txq->elts;
80         unsigned int elts_m = txq->elts_n - 1;
81
82         DEBUG("%p: freeing WRs", (void *)txq);
83         while (elts_tail != elts_head) {
84                 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
85
86                 assert(elt->buf != NULL);
87                 rte_pktmbuf_free(elt->buf);
88                 elt->buf = NULL;
89                 elt->wqe = NULL;
90         }
91         txq->elts_tail = txq->elts_head;
92 }
93
94 struct txq_mp2mr_mbuf_check_data {
95         int ret;
96 };
97
98 /**
99  * Callback function for rte_mempool_obj_iter() to check whether a given
100  * mempool object looks like a mbuf.
101  *
102  * @param[in] mp
103  *   The mempool pointer
104  * @param[in] arg
105  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
106  *   return value.
107  * @param[in] obj
108  *   Object address.
109  * @param index
110  *   Object index, unused.
111  */
112 static void
113 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
114                           uint32_t index)
115 {
116         struct txq_mp2mr_mbuf_check_data *data = arg;
117         struct rte_mbuf *buf = obj;
118
119         (void)index;
120         /*
121          * Check whether mbuf structure fits element size and whether mempool
122          * pointer is valid.
123          */
124         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
125                 data->ret = -1;
126 }
127
128 /**
129  * Iterator function for rte_mempool_walk() to register existing mempools and
130  * fill the MP to MR cache of a Tx queue.
131  *
132  * @param[in] mp
133  *   Memory Pool to register.
134  * @param *arg
135  *   Pointer to Tx queue structure.
136  */
137 static void
138 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
139 {
140         struct txq *txq = arg;
141         struct txq_mp2mr_mbuf_check_data data = {
142                 .ret = 0,
143         };
144
145         /* Register mempool only if the first element looks like a mbuf. */
146         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
147                         data.ret == -1)
148                 return;
149         mlx4_txq_mp2mr(txq, mp);
150 }
151
152 /**
153  * Retrieves information needed in order to directly access the Tx queue.
154  *
155  * @param txq
156  *   Pointer to Tx queue structure.
157  * @param mlxdv
158  *   Pointer to device information for this Tx queue.
159  */
160 static void
161 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
162 {
163         struct mlx4_sq *sq = &txq->msq;
164         struct mlx4_cq *cq = &txq->mcq;
165         struct mlx4dv_qp *dqp = mlxdv->qp.out;
166         struct mlx4dv_cq *dcq = mlxdv->cq.out;
167
168         /* Total length, including headroom and spare WQEs. */
169         sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
170         sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
171         sq->eob = sq->buf + sq->size;
172         uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
173         /* Continuous headroom size bytes must always stay freed. */
174         sq->remain_size = sq->size - headroom_size;
175         sq->owner_opcode = MLX4_OPCODE_SEND | (0 << MLX4_SQ_OWNER_BIT);
176         sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
177                                      (0 << MLX4_SQ_OWNER_BIT));
178         sq->db = dqp->sdb;
179         sq->doorbell_qpn = dqp->doorbell_qpn;
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  * Returns the per-port supported offloads.
188  *
189  * @param priv
190  *   Pointer to private structure.
191  *
192  * @return
193  *   Supported Tx offloads.
194  */
195 uint64_t
196 mlx4_get_tx_port_offloads(struct priv *priv)
197 {
198         uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
199
200         if (priv->hw_csum) {
201                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
202                              DEV_TX_OFFLOAD_UDP_CKSUM |
203                              DEV_TX_OFFLOAD_TCP_CKSUM);
204         }
205         if (priv->hw_csum_l2tun)
206                 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
207         return offloads;
208 }
209
210 /**
211  * Checks if the per-queue offload configuration is valid.
212  *
213  * @param priv
214  *   Pointer to private structure.
215  * @param requested
216  *   Per-queue offloads configuration.
217  *
218  * @return
219  *   Nonzero when configuration is valid.
220  */
221 static int
222 mlx4_check_tx_queue_offloads(struct priv *priv, uint64_t requested)
223 {
224         uint64_t mandatory = priv->dev->data->dev_conf.txmode.offloads;
225         uint64_t supported = mlx4_get_tx_port_offloads(priv);
226
227         return !((mandatory ^ requested) & supported);
228 }
229
230 /**
231  * DPDK callback to configure a Tx queue.
232  *
233  * @param dev
234  *   Pointer to Ethernet device structure.
235  * @param idx
236  *   Tx queue index.
237  * @param desc
238  *   Number of descriptors to configure in queue.
239  * @param socket
240  *   NUMA socket on which memory must be allocated.
241  * @param[in] conf
242  *   Thresholds parameters.
243  *
244  * @return
245  *   0 on success, negative errno value otherwise and rte_errno is set.
246  */
247 int
248 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
249                     unsigned int socket, const struct rte_eth_txconf *conf)
250 {
251         struct priv *priv = dev->data->dev_private;
252         struct mlx4dv_obj mlxdv;
253         struct mlx4dv_qp dv_qp;
254         struct mlx4dv_cq dv_cq;
255         struct txq_elt (*elts)[rte_align32pow2(desc)];
256         struct ibv_qp_init_attr qp_init_attr;
257         struct txq *txq;
258         uint8_t *bounce_buf;
259         struct mlx4_malloc_vec vec[] = {
260                 {
261                         .align = RTE_CACHE_LINE_SIZE,
262                         .size = sizeof(*txq),
263                         .addr = (void **)&txq,
264                 },
265                 {
266                         .align = RTE_CACHE_LINE_SIZE,
267                         .size = sizeof(*elts),
268                         .addr = (void **)&elts,
269                 },
270                 {
271                         .align = RTE_CACHE_LINE_SIZE,
272                         .size = MLX4_MAX_WQE_SIZE,
273                         .addr = (void **)&bounce_buf,
274                 },
275         };
276         int ret;
277
278         DEBUG("%p: configuring queue %u for %u descriptors",
279               (void *)dev, idx, desc);
280         /*
281          * Don't verify port offloads for application which
282          * use the old API.
283          */
284         if ((conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) &&
285             !mlx4_check_tx_queue_offloads(priv, conf->offloads)) {
286                 rte_errno = ENOTSUP;
287                 ERROR("%p: Tx queue offloads 0x%" PRIx64 " don't match port "
288                       "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
289                       (void *)dev, conf->offloads,
290                       dev->data->dev_conf.txmode.offloads,
291                       mlx4_get_tx_port_offloads(priv));
292                 return -rte_errno;
293         }
294         if (idx >= dev->data->nb_tx_queues) {
295                 rte_errno = EOVERFLOW;
296                 ERROR("%p: queue index out of range (%u >= %u)",
297                       (void *)dev, idx, dev->data->nb_tx_queues);
298                 return -rte_errno;
299         }
300         txq = dev->data->tx_queues[idx];
301         if (txq) {
302                 rte_errno = EEXIST;
303                 DEBUG("%p: Tx queue %u already configured, release it first",
304                       (void *)dev, idx);
305                 return -rte_errno;
306         }
307         if (!desc) {
308                 rte_errno = EINVAL;
309                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
310                 return -rte_errno;
311         }
312         if (desc != RTE_DIM(*elts)) {
313                 desc = RTE_DIM(*elts);
314                 WARN("%p: increased number of descriptors in Tx queue %u"
315                      " to the next power of two (%u)",
316                      (void *)dev, idx, desc);
317         }
318         /* Allocate and initialize Tx queue. */
319         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
320         if (!txq) {
321                 ERROR("%p: unable to allocate queue index %u",
322                       (void *)dev, idx);
323                 return -rte_errno;
324         }
325         *txq = (struct txq){
326                 .priv = priv,
327                 .stats = {
328                         .idx = idx,
329                 },
330                 .socket = socket,
331                 .elts_n = desc,
332                 .elts = elts,
333                 .elts_head = 0,
334                 .elts_tail = 0,
335                 /*
336                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
337                  * packets or at least 4 times per ring.
338                  */
339                 .elts_comp_cd =
340                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
341                 .elts_comp_cd_init =
342                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
343                 .csum = priv->hw_csum &&
344                         (conf->offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
345                                            DEV_TX_OFFLOAD_UDP_CKSUM |
346                                            DEV_TX_OFFLOAD_TCP_CKSUM)),
347                 .csum_l2tun = priv->hw_csum_l2tun &&
348                               (conf->offloads &
349                                DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
350                 /* Enable Tx loopback for VF devices. */
351                 .lb = !!priv->vf,
352                 .bounce_buf = bounce_buf,
353         };
354         txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
355         if (!txq->cq) {
356                 rte_errno = ENOMEM;
357                 ERROR("%p: CQ creation failure: %s",
358                       (void *)dev, strerror(rte_errno));
359                 goto error;
360         }
361         qp_init_attr = (struct ibv_qp_init_attr){
362                 .send_cq = txq->cq,
363                 .recv_cq = txq->cq,
364                 .cap = {
365                         .max_send_wr =
366                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
367                         .max_send_sge = 1,
368                         .max_inline_data = MLX4_PMD_MAX_INLINE,
369                 },
370                 .qp_type = IBV_QPT_RAW_PACKET,
371                 /* No completion events must occur by default. */
372                 .sq_sig_all = 0,
373         };
374         txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
375         if (!txq->qp) {
376                 rte_errno = errno ? errno : EINVAL;
377                 ERROR("%p: QP creation failure: %s",
378                       (void *)dev, strerror(rte_errno));
379                 goto error;
380         }
381         txq->max_inline = qp_init_attr.cap.max_inline_data;
382         ret = mlx4_glue->modify_qp
383                 (txq->qp,
384                  &(struct ibv_qp_attr){
385                         .qp_state = IBV_QPS_INIT,
386                         .port_num = priv->port,
387                  },
388                  IBV_QP_STATE | IBV_QP_PORT);
389         if (ret) {
390                 rte_errno = ret;
391                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
392                       (void *)dev, strerror(rte_errno));
393                 goto error;
394         }
395         ret = mlx4_glue->modify_qp
396                 (txq->qp,
397                  &(struct ibv_qp_attr){
398                         .qp_state = IBV_QPS_RTR,
399                  },
400                  IBV_QP_STATE);
401         if (ret) {
402                 rte_errno = ret;
403                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
404                       (void *)dev, strerror(rte_errno));
405                 goto error;
406         }
407         ret = mlx4_glue->modify_qp
408                 (txq->qp,
409                  &(struct ibv_qp_attr){
410                         .qp_state = IBV_QPS_RTS,
411                  },
412                  IBV_QP_STATE);
413         if (ret) {
414                 rte_errno = ret;
415                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
416                       (void *)dev, strerror(rte_errno));
417                 goto error;
418         }
419         /* Retrieve device queue information. */
420         mlxdv.cq.in = txq->cq;
421         mlxdv.cq.out = &dv_cq;
422         mlxdv.qp.in = txq->qp;
423         mlxdv.qp.out = &dv_qp;
424         ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
425         if (ret) {
426                 rte_errno = EINVAL;
427                 ERROR("%p: failed to obtain information needed for"
428                       " accessing the device queues", (void *)dev);
429                 goto error;
430         }
431         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
432         /* Save first wqe pointer in the first element. */
433         (&(*txq->elts)[0])->wqe =
434                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
435         /* Pre-register known mempools. */
436         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
437         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
438         dev->data->tx_queues[idx] = txq;
439         return 0;
440 error:
441         dev->data->tx_queues[idx] = NULL;
442         ret = rte_errno;
443         mlx4_tx_queue_release(txq);
444         rte_errno = ret;
445         assert(rte_errno > 0);
446         return -rte_errno;
447 }
448
449 /**
450  * DPDK callback to release a Tx queue.
451  *
452  * @param dpdk_txq
453  *   Generic Tx queue pointer.
454  */
455 void
456 mlx4_tx_queue_release(void *dpdk_txq)
457 {
458         struct txq *txq = (struct txq *)dpdk_txq;
459         struct priv *priv;
460         unsigned int i;
461
462         if (txq == NULL)
463                 return;
464         priv = txq->priv;
465         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
466                 if (priv->dev->data->tx_queues[i] == txq) {
467                         DEBUG("%p: removing Tx queue %p from list",
468                               (void *)priv->dev, (void *)txq);
469                         priv->dev->data->tx_queues[i] = NULL;
470                         break;
471                 }
472         mlx4_txq_free_elts(txq);
473         if (txq->qp)
474                 claim_zero(mlx4_glue->destroy_qp(txq->qp));
475         if (txq->cq)
476                 claim_zero(mlx4_glue->destroy_cq(txq->cq));
477         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
478                 if (!txq->mp2mr[i].mp)
479                         break;
480                 assert(txq->mp2mr[i].mr);
481                 mlx4_mr_put(txq->mp2mr[i].mr);
482         }
483         rte_free(txq);
484 }