ethdev: new Rx/Tx offloads API
[dpdk.git] / drivers / net / mlx4 / mlx4_txq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 /**
7  * @file
8  * Tx queues configuration for mlx4 driver.
9  */
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <string.h>
16 #include <inttypes.h>
17
18 /* Verbs headers do not support -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_common.h>
28 #include <rte_errno.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_malloc.h>
31 #include <rte_mbuf.h>
32 #include <rte_mempool.h>
33
34 #include "mlx4.h"
35 #include "mlx4_glue.h"
36 #include "mlx4_prm.h"
37 #include "mlx4_rxtx.h"
38 #include "mlx4_utils.h"
39
40 /**
41  * Free Tx queue elements.
42  *
43  * @param txq
44  *   Pointer to Tx queue structure.
45  */
46 static void
47 mlx4_txq_free_elts(struct txq *txq)
48 {
49         unsigned int elts_head = txq->elts_head;
50         unsigned int elts_tail = txq->elts_tail;
51         struct txq_elt (*elts)[txq->elts_n] = txq->elts;
52         unsigned int elts_m = txq->elts_n - 1;
53
54         DEBUG("%p: freeing WRs", (void *)txq);
55         while (elts_tail != elts_head) {
56                 struct txq_elt *elt = &(*elts)[elts_tail++ & elts_m];
57
58                 assert(elt->buf != NULL);
59                 rte_pktmbuf_free(elt->buf);
60                 elt->buf = NULL;
61                 elt->wqe = NULL;
62         }
63         txq->elts_tail = txq->elts_head;
64 }
65
66 struct txq_mp2mr_mbuf_check_data {
67         int ret;
68 };
69
70 /**
71  * Callback function for rte_mempool_obj_iter() to check whether a given
72  * mempool object looks like a mbuf.
73  *
74  * @param[in] mp
75  *   The mempool pointer
76  * @param[in] arg
77  *   Context data (struct mlx4_txq_mp2mr_mbuf_check_data). Contains the
78  *   return value.
79  * @param[in] obj
80  *   Object address.
81  * @param index
82  *   Object index, unused.
83  */
84 static void
85 mlx4_txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
86                           uint32_t index)
87 {
88         struct txq_mp2mr_mbuf_check_data *data = arg;
89         struct rte_mbuf *buf = obj;
90
91         (void)index;
92         /*
93          * Check whether mbuf structure fits element size and whether mempool
94          * pointer is valid.
95          */
96         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
97                 data->ret = -1;
98 }
99
100 /**
101  * Iterator function for rte_mempool_walk() to register existing mempools and
102  * fill the MP to MR cache of a Tx queue.
103  *
104  * @param[in] mp
105  *   Memory Pool to register.
106  * @param *arg
107  *   Pointer to Tx queue structure.
108  */
109 static void
110 mlx4_txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
111 {
112         struct txq *txq = arg;
113         struct txq_mp2mr_mbuf_check_data data = {
114                 .ret = 0,
115         };
116
117         /* Register mempool only if the first element looks like a mbuf. */
118         if (rte_mempool_obj_iter(mp, mlx4_txq_mp2mr_mbuf_check, &data) == 0 ||
119                         data.ret == -1)
120                 return;
121         mlx4_txq_mp2mr(txq, mp);
122 }
123
124 /**
125  * Retrieves information needed in order to directly access the Tx queue.
126  *
127  * @param txq
128  *   Pointer to Tx queue structure.
129  * @param mlxdv
130  *   Pointer to device information for this Tx queue.
131  */
132 static void
133 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
134 {
135         struct mlx4_sq *sq = &txq->msq;
136         struct mlx4_cq *cq = &txq->mcq;
137         struct mlx4dv_qp *dqp = mlxdv->qp.out;
138         struct mlx4dv_cq *dcq = mlxdv->cq.out;
139
140         /* Total length, including headroom and spare WQEs. */
141         sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
142         sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
143         sq->eob = sq->buf + sq->size;
144         uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
145         /* Continuous headroom size bytes must always stay freed. */
146         sq->remain_size = sq->size - headroom_size;
147         sq->owner_opcode = MLX4_OPCODE_SEND | (0 << MLX4_SQ_OWNER_BIT);
148         sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
149                                      (0 << MLX4_SQ_OWNER_BIT));
150         sq->db = dqp->sdb;
151         sq->doorbell_qpn = dqp->doorbell_qpn;
152         cq->buf = dcq->buf.buf;
153         cq->cqe_cnt = dcq->cqe_cnt;
154         cq->set_ci_db = dcq->set_ci_db;
155         cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
156 }
157
158 /**
159  * Returns the per-port supported offloads.
160  *
161  * @param priv
162  *   Pointer to private structure.
163  *
164  * @return
165  *   Supported Tx offloads.
166  */
167 uint64_t
168 mlx4_get_tx_port_offloads(struct priv *priv)
169 {
170         uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
171
172         if (priv->hw_csum) {
173                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
174                              DEV_TX_OFFLOAD_UDP_CKSUM |
175                              DEV_TX_OFFLOAD_TCP_CKSUM);
176         }
177         if (priv->hw_csum_l2tun)
178                 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
179         return offloads;
180 }
181
182 /**
183  * DPDK callback to configure a Tx queue.
184  *
185  * @param dev
186  *   Pointer to Ethernet device structure.
187  * @param idx
188  *   Tx queue index.
189  * @param desc
190  *   Number of descriptors to configure in queue.
191  * @param socket
192  *   NUMA socket on which memory must be allocated.
193  * @param[in] conf
194  *   Thresholds parameters.
195  *
196  * @return
197  *   0 on success, negative errno value otherwise and rte_errno is set.
198  */
199 int
200 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
201                     unsigned int socket, const struct rte_eth_txconf *conf)
202 {
203         struct priv *priv = dev->data->dev_private;
204         struct mlx4dv_obj mlxdv;
205         struct mlx4dv_qp dv_qp;
206         struct mlx4dv_cq dv_cq;
207         struct txq_elt (*elts)[rte_align32pow2(desc)];
208         struct ibv_qp_init_attr qp_init_attr;
209         struct txq *txq;
210         uint8_t *bounce_buf;
211         struct mlx4_malloc_vec vec[] = {
212                 {
213                         .align = RTE_CACHE_LINE_SIZE,
214                         .size = sizeof(*txq),
215                         .addr = (void **)&txq,
216                 },
217                 {
218                         .align = RTE_CACHE_LINE_SIZE,
219                         .size = sizeof(*elts),
220                         .addr = (void **)&elts,
221                 },
222                 {
223                         .align = RTE_CACHE_LINE_SIZE,
224                         .size = MLX4_MAX_WQE_SIZE,
225                         .addr = (void **)&bounce_buf,
226                 },
227         };
228         int ret;
229         uint64_t offloads;
230
231         offloads = conf->offloads | dev->data->dev_conf.txmode.offloads;
232
233         DEBUG("%p: configuring queue %u for %u descriptors",
234               (void *)dev, idx, desc);
235
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         if (desc != RTE_DIM(*elts)) {
255                 desc = RTE_DIM(*elts);
256                 WARN("%p: increased number of descriptors in Tx queue %u"
257                      " to the next power of two (%u)",
258                      (void *)dev, idx, desc);
259         }
260         /* Allocate and initialize Tx queue. */
261         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
262         if (!txq) {
263                 ERROR("%p: unable to allocate queue index %u",
264                       (void *)dev, idx);
265                 return -rte_errno;
266         }
267         *txq = (struct txq){
268                 .priv = priv,
269                 .stats = {
270                         .idx = idx,
271                 },
272                 .socket = socket,
273                 .elts_n = desc,
274                 .elts = elts,
275                 .elts_head = 0,
276                 .elts_tail = 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                         (offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
287                                            DEV_TX_OFFLOAD_UDP_CKSUM |
288                                            DEV_TX_OFFLOAD_TCP_CKSUM)),
289                 .csum_l2tun = priv->hw_csum_l2tun &&
290                               (offloads &
291                                DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
292                 /* Enable Tx loopback for VF devices. */
293                 .lb = !!priv->vf,
294                 .bounce_buf = bounce_buf,
295         };
296         txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
297         if (!txq->cq) {
298                 rte_errno = ENOMEM;
299                 ERROR("%p: CQ creation failure: %s",
300                       (void *)dev, strerror(rte_errno));
301                 goto error;
302         }
303         qp_init_attr = (struct ibv_qp_init_attr){
304                 .send_cq = txq->cq,
305                 .recv_cq = txq->cq,
306                 .cap = {
307                         .max_send_wr =
308                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
309                         .max_send_sge = 1,
310                         .max_inline_data = MLX4_PMD_MAX_INLINE,
311                 },
312                 .qp_type = IBV_QPT_RAW_PACKET,
313                 /* No completion events must occur by default. */
314                 .sq_sig_all = 0,
315         };
316         txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
317         if (!txq->qp) {
318                 rte_errno = errno ? errno : EINVAL;
319                 ERROR("%p: QP creation failure: %s",
320                       (void *)dev, strerror(rte_errno));
321                 goto error;
322         }
323         txq->max_inline = qp_init_attr.cap.max_inline_data;
324         ret = mlx4_glue->modify_qp
325                 (txq->qp,
326                  &(struct ibv_qp_attr){
327                         .qp_state = IBV_QPS_INIT,
328                         .port_num = priv->port,
329                  },
330                  IBV_QP_STATE | IBV_QP_PORT);
331         if (ret) {
332                 rte_errno = ret;
333                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
334                       (void *)dev, strerror(rte_errno));
335                 goto error;
336         }
337         ret = mlx4_glue->modify_qp
338                 (txq->qp,
339                  &(struct ibv_qp_attr){
340                         .qp_state = IBV_QPS_RTR,
341                  },
342                  IBV_QP_STATE);
343         if (ret) {
344                 rte_errno = ret;
345                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
346                       (void *)dev, strerror(rte_errno));
347                 goto error;
348         }
349         ret = mlx4_glue->modify_qp
350                 (txq->qp,
351                  &(struct ibv_qp_attr){
352                         .qp_state = IBV_QPS_RTS,
353                  },
354                  IBV_QP_STATE);
355         if (ret) {
356                 rte_errno = ret;
357                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
358                       (void *)dev, strerror(rte_errno));
359                 goto error;
360         }
361         /* Retrieve device queue information. */
362         mlxdv.cq.in = txq->cq;
363         mlxdv.cq.out = &dv_cq;
364         mlxdv.qp.in = txq->qp;
365         mlxdv.qp.out = &dv_qp;
366         ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
367         if (ret) {
368                 rte_errno = EINVAL;
369                 ERROR("%p: failed to obtain information needed for"
370                       " accessing the device queues", (void *)dev);
371                 goto error;
372         }
373         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
374         /* Save first wqe pointer in the first element. */
375         (&(*txq->elts)[0])->wqe =
376                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
377         /* Pre-register known mempools. */
378         rte_mempool_walk(mlx4_txq_mp2mr_iter, txq);
379         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
380         dev->data->tx_queues[idx] = txq;
381         return 0;
382 error:
383         dev->data->tx_queues[idx] = NULL;
384         ret = rte_errno;
385         mlx4_tx_queue_release(txq);
386         rte_errno = ret;
387         assert(rte_errno > 0);
388         return -rte_errno;
389 }
390
391 /**
392  * DPDK callback to release a Tx queue.
393  *
394  * @param dpdk_txq
395  *   Generic Tx queue pointer.
396  */
397 void
398 mlx4_tx_queue_release(void *dpdk_txq)
399 {
400         struct txq *txq = (struct txq *)dpdk_txq;
401         struct priv *priv;
402         unsigned int i;
403
404         if (txq == NULL)
405                 return;
406         priv = txq->priv;
407         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
408                 if (priv->dev->data->tx_queues[i] == txq) {
409                         DEBUG("%p: removing Tx queue %p from list",
410                               (void *)priv->dev, (void *)txq);
411                         priv->dev->data->tx_queues[i] = NULL;
412                         break;
413                 }
414         mlx4_txq_free_elts(txq);
415         if (txq->qp)
416                 claim_zero(mlx4_glue->destroy_qp(txq->qp));
417         if (txq->cq)
418                 claim_zero(mlx4_glue->destroy_cq(txq->cq));
419         for (i = 0; i != RTE_DIM(txq->mp2mr); ++i) {
420                 if (!txq->mp2mr[i].mp)
421                         break;
422                 assert(txq->mp2mr[i].mr);
423                 mlx4_mr_put(txq->mp2mr[i].mr);
424         }
425         rte_free(txq);
426 }