net/mlx4: add new memory region support
[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 /**
67  * Retrieves information needed in order to directly access the Tx queue.
68  *
69  * @param txq
70  *   Pointer to Tx queue structure.
71  * @param mlxdv
72  *   Pointer to device information for this Tx queue.
73  */
74 static void
75 mlx4_txq_fill_dv_obj_info(struct txq *txq, struct mlx4dv_obj *mlxdv)
76 {
77         struct mlx4_sq *sq = &txq->msq;
78         struct mlx4_cq *cq = &txq->mcq;
79         struct mlx4dv_qp *dqp = mlxdv->qp.out;
80         struct mlx4dv_cq *dcq = mlxdv->cq.out;
81
82         /* Total length, including headroom and spare WQEs. */
83         sq->size = (uint32_t)dqp->rq.offset - (uint32_t)dqp->sq.offset;
84         sq->buf = (uint8_t *)dqp->buf.buf + dqp->sq.offset;
85         sq->eob = sq->buf + sq->size;
86         uint32_t headroom_size = 2048 + (1 << dqp->sq.wqe_shift);
87         /* Continuous headroom size bytes must always stay freed. */
88         sq->remain_size = sq->size - headroom_size;
89         sq->owner_opcode = MLX4_OPCODE_SEND | (0 << MLX4_SQ_OWNER_BIT);
90         sq->stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
91                                      (0 << MLX4_SQ_OWNER_BIT));
92         sq->db = dqp->sdb;
93         sq->doorbell_qpn = dqp->doorbell_qpn;
94         cq->buf = dcq->buf.buf;
95         cq->cqe_cnt = dcq->cqe_cnt;
96         cq->set_ci_db = dcq->set_ci_db;
97         cq->cqe_64 = (dcq->cqe_size & 64) ? 1 : 0;
98 }
99
100 /**
101  * Returns the per-port supported offloads.
102  *
103  * @param priv
104  *   Pointer to private structure.
105  *
106  * @return
107  *   Supported Tx offloads.
108  */
109 uint64_t
110 mlx4_get_tx_port_offloads(struct priv *priv)
111 {
112         uint64_t offloads = DEV_TX_OFFLOAD_MULTI_SEGS;
113
114         if (priv->hw_csum) {
115                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
116                              DEV_TX_OFFLOAD_UDP_CKSUM |
117                              DEV_TX_OFFLOAD_TCP_CKSUM);
118         }
119         if (priv->hw_csum_l2tun)
120                 offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
121         return offloads;
122 }
123
124 /**
125  * DPDK callback to configure a Tx queue.
126  *
127  * @param dev
128  *   Pointer to Ethernet device structure.
129  * @param idx
130  *   Tx queue index.
131  * @param desc
132  *   Number of descriptors to configure in queue.
133  * @param socket
134  *   NUMA socket on which memory must be allocated.
135  * @param[in] conf
136  *   Thresholds parameters.
137  *
138  * @return
139  *   0 on success, negative errno value otherwise and rte_errno is set.
140  */
141 int
142 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
143                     unsigned int socket, const struct rte_eth_txconf *conf)
144 {
145         struct priv *priv = dev->data->dev_private;
146         struct mlx4dv_obj mlxdv;
147         struct mlx4dv_qp dv_qp;
148         struct mlx4dv_cq dv_cq;
149         struct txq_elt (*elts)[rte_align32pow2(desc)];
150         struct ibv_qp_init_attr qp_init_attr;
151         struct txq *txq;
152         uint8_t *bounce_buf;
153         struct mlx4_malloc_vec vec[] = {
154                 {
155                         .align = RTE_CACHE_LINE_SIZE,
156                         .size = sizeof(*txq),
157                         .addr = (void **)&txq,
158                 },
159                 {
160                         .align = RTE_CACHE_LINE_SIZE,
161                         .size = sizeof(*elts),
162                         .addr = (void **)&elts,
163                 },
164                 {
165                         .align = RTE_CACHE_LINE_SIZE,
166                         .size = MLX4_MAX_WQE_SIZE,
167                         .addr = (void **)&bounce_buf,
168                 },
169         };
170         int ret;
171         uint64_t offloads;
172
173         offloads = conf->offloads | dev->data->dev_conf.txmode.offloads;
174
175         DEBUG("%p: configuring queue %u for %u descriptors",
176               (void *)dev, idx, desc);
177
178         if (idx >= dev->data->nb_tx_queues) {
179                 rte_errno = EOVERFLOW;
180                 ERROR("%p: queue index out of range (%u >= %u)",
181                       (void *)dev, idx, dev->data->nb_tx_queues);
182                 return -rte_errno;
183         }
184         txq = dev->data->tx_queues[idx];
185         if (txq) {
186                 rte_errno = EEXIST;
187                 DEBUG("%p: Tx queue %u already configured, release it first",
188                       (void *)dev, idx);
189                 return -rte_errno;
190         }
191         if (!desc) {
192                 rte_errno = EINVAL;
193                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
194                 return -rte_errno;
195         }
196         if (desc != RTE_DIM(*elts)) {
197                 desc = RTE_DIM(*elts);
198                 WARN("%p: increased number of descriptors in Tx queue %u"
199                      " to the next power of two (%u)",
200                      (void *)dev, idx, desc);
201         }
202         /* Allocate and initialize Tx queue. */
203         mlx4_zmallocv_socket("TXQ", vec, RTE_DIM(vec), socket);
204         if (!txq) {
205                 ERROR("%p: unable to allocate queue index %u",
206                       (void *)dev, idx);
207                 return -rte_errno;
208         }
209         *txq = (struct txq){
210                 .priv = priv,
211                 .stats = {
212                         .idx = idx,
213                 },
214                 .socket = socket,
215                 .elts_n = desc,
216                 .elts = elts,
217                 .elts_head = 0,
218                 .elts_tail = 0,
219                 /*
220                  * Request send completion every MLX4_PMD_TX_PER_COMP_REQ
221                  * packets or at least 4 times per ring.
222                  */
223                 .elts_comp_cd =
224                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
225                 .elts_comp_cd_init =
226                         RTE_MIN(MLX4_PMD_TX_PER_COMP_REQ, desc / 4),
227                 .csum = priv->hw_csum &&
228                         (offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
229                                            DEV_TX_OFFLOAD_UDP_CKSUM |
230                                            DEV_TX_OFFLOAD_TCP_CKSUM)),
231                 .csum_l2tun = priv->hw_csum_l2tun &&
232                               (offloads &
233                                DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM),
234                 /* Enable Tx loopback for VF devices. */
235                 .lb = !!priv->vf,
236                 .bounce_buf = bounce_buf,
237         };
238         txq->cq = mlx4_glue->create_cq(priv->ctx, desc, NULL, NULL, 0);
239         if (!txq->cq) {
240                 rte_errno = ENOMEM;
241                 ERROR("%p: CQ creation failure: %s",
242                       (void *)dev, strerror(rte_errno));
243                 goto error;
244         }
245         qp_init_attr = (struct ibv_qp_init_attr){
246                 .send_cq = txq->cq,
247                 .recv_cq = txq->cq,
248                 .cap = {
249                         .max_send_wr =
250                                 RTE_MIN(priv->device_attr.max_qp_wr, desc),
251                         .max_send_sge = 1,
252                         .max_inline_data = MLX4_PMD_MAX_INLINE,
253                 },
254                 .qp_type = IBV_QPT_RAW_PACKET,
255                 /* No completion events must occur by default. */
256                 .sq_sig_all = 0,
257         };
258         txq->qp = mlx4_glue->create_qp(priv->pd, &qp_init_attr);
259         if (!txq->qp) {
260                 rte_errno = errno ? errno : EINVAL;
261                 ERROR("%p: QP creation failure: %s",
262                       (void *)dev, strerror(rte_errno));
263                 goto error;
264         }
265         txq->max_inline = qp_init_attr.cap.max_inline_data;
266         ret = mlx4_glue->modify_qp
267                 (txq->qp,
268                  &(struct ibv_qp_attr){
269                         .qp_state = IBV_QPS_INIT,
270                         .port_num = priv->port,
271                  },
272                  IBV_QP_STATE | IBV_QP_PORT);
273         if (ret) {
274                 rte_errno = ret;
275                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
276                       (void *)dev, strerror(rte_errno));
277                 goto error;
278         }
279         ret = mlx4_glue->modify_qp
280                 (txq->qp,
281                  &(struct ibv_qp_attr){
282                         .qp_state = IBV_QPS_RTR,
283                  },
284                  IBV_QP_STATE);
285         if (ret) {
286                 rte_errno = ret;
287                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
288                       (void *)dev, strerror(rte_errno));
289                 goto error;
290         }
291         ret = mlx4_glue->modify_qp
292                 (txq->qp,
293                  &(struct ibv_qp_attr){
294                         .qp_state = IBV_QPS_RTS,
295                  },
296                  IBV_QP_STATE);
297         if (ret) {
298                 rte_errno = ret;
299                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
300                       (void *)dev, strerror(rte_errno));
301                 goto error;
302         }
303         /* Retrieve device queue information. */
304         mlxdv.cq.in = txq->cq;
305         mlxdv.cq.out = &dv_cq;
306         mlxdv.qp.in = txq->qp;
307         mlxdv.qp.out = &dv_qp;
308         ret = mlx4_glue->dv_init_obj(&mlxdv, MLX4DV_OBJ_QP | MLX4DV_OBJ_CQ);
309         if (ret) {
310                 rte_errno = EINVAL;
311                 ERROR("%p: failed to obtain information needed for"
312                       " accessing the device queues", (void *)dev);
313                 goto error;
314         }
315         mlx4_txq_fill_dv_obj_info(txq, &mlxdv);
316         /* Save first wqe pointer in the first element. */
317         (&(*txq->elts)[0])->wqe =
318                 (volatile struct mlx4_wqe_ctrl_seg *)txq->msq.buf;
319         if (mlx4_mr_btree_init(&txq->mr_ctrl.cache_bh,
320                                MLX4_MR_BTREE_CACHE_N, socket)) {
321                 /* rte_errno is already set. */
322                 goto error;
323         }
324         /* Save pointer of global generation number to check memory event. */
325         txq->mr_ctrl.dev_gen_ptr = &priv->mr.dev_gen;
326         DEBUG("%p: adding Tx queue %p to list", (void *)dev, (void *)txq);
327         dev->data->tx_queues[idx] = txq;
328         return 0;
329 error:
330         dev->data->tx_queues[idx] = NULL;
331         ret = rte_errno;
332         mlx4_tx_queue_release(txq);
333         rte_errno = ret;
334         assert(rte_errno > 0);
335         return -rte_errno;
336 }
337
338 /**
339  * DPDK callback to release a Tx queue.
340  *
341  * @param dpdk_txq
342  *   Generic Tx queue pointer.
343  */
344 void
345 mlx4_tx_queue_release(void *dpdk_txq)
346 {
347         struct txq *txq = (struct txq *)dpdk_txq;
348         struct priv *priv;
349         unsigned int i;
350
351         if (txq == NULL)
352                 return;
353         priv = txq->priv;
354         for (i = 0; i != priv->dev->data->nb_tx_queues; ++i)
355                 if (priv->dev->data->tx_queues[i] == txq) {
356                         DEBUG("%p: removing Tx queue %p from list",
357                               (void *)priv->dev, (void *)txq);
358                         priv->dev->data->tx_queues[i] = NULL;
359                         break;
360                 }
361         mlx4_txq_free_elts(txq);
362         if (txq->qp)
363                 claim_zero(mlx4_glue->destroy_qp(txq->qp));
364         if (txq->cq)
365                 claim_zero(mlx4_glue->destroy_cq(txq->cq));
366         mlx4_mr_btree_free(&txq->mr_ctrl.cache_bh);
367         rte_free(txq);
368 }