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