net/mlx5: mark parameters with unused attribute
[dpdk.git] / drivers / net / mlx5 / mlx5_mr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox.
4  */
5
6 #ifdef PEDANTIC
7 #pragma GCC diagnostic ignored "-Wpedantic"
8 #endif
9 #include <infiniband/verbs.h>
10 #ifdef PEDANTIC
11 #pragma GCC diagnostic error "-Wpedantic"
12 #endif
13
14 #include <rte_mempool.h>
15 #include <rte_malloc.h>
16
17 #include "mlx5.h"
18 #include "mlx5_rxtx.h"
19 #include "mlx5_glue.h"
20
21 struct mlx5_check_mempool_data {
22         int ret;
23         char *start;
24         char *end;
25 };
26
27 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
28 static void
29 mlx5_check_mempool_cb(struct rte_mempool *mp __rte_unused,
30                       void *opaque, struct rte_mempool_memhdr *memhdr,
31                       unsigned int mem_idx __rte_unused)
32 {
33         struct mlx5_check_mempool_data *data = opaque;
34
35         /* It already failed, skip the next chunks. */
36         if (data->ret != 0)
37                 return;
38         /* It is the first chunk. */
39         if (data->start == NULL && data->end == NULL) {
40                 data->start = memhdr->addr;
41                 data->end = data->start + memhdr->len;
42                 return;
43         }
44         if (data->end == memhdr->addr) {
45                 data->end += memhdr->len;
46                 return;
47         }
48         if (data->start == (char *)memhdr->addr + memhdr->len) {
49                 data->start -= memhdr->len;
50                 return;
51         }
52         /* Error, mempool is not virtually contiguous. */
53         data->ret = -1;
54 }
55
56 /**
57  * Check if a mempool can be used: it must be virtually contiguous.
58  *
59  * @param[in] mp
60  *   Pointer to memory pool.
61  * @param[out] start
62  *   Pointer to the start address of the mempool virtual memory area
63  * @param[out] end
64  *   Pointer to the end address of the mempool virtual memory area
65  *
66  * @return
67  *   0 on success (mempool is virtually contiguous), -1 on error.
68  */
69 static int mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
70         uintptr_t *end)
71 {
72         struct mlx5_check_mempool_data data;
73
74         memset(&data, 0, sizeof(data));
75         rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
76         *start = (uintptr_t)data.start;
77         *end = (uintptr_t)data.end;
78
79         return data.ret;
80 }
81
82 /**
83  * Register a Memory Region (MR) <-> Memory Pool (MP) association in
84  * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
85  *
86  * This function should only be called by txq_mp2mr().
87  *
88  * @param priv
89  *   Pointer to private structure.
90  * @param txq
91  *   Pointer to TX queue structure.
92  * @param[in] mp
93  *   Memory Pool for which a Memory Region lkey must be returned.
94  * @param idx
95  *   Index of the next available entry.
96  *
97  * @return
98  *   mr on success, NULL on failure.
99  */
100 struct mlx5_mr*
101 priv_txq_mp2mr_reg(struct priv *priv, struct mlx5_txq_data *txq,
102                    struct rte_mempool *mp, unsigned int idx)
103 {
104         struct mlx5_txq_ctrl *txq_ctrl =
105                 container_of(txq, struct mlx5_txq_ctrl, txq);
106         struct mlx5_mr *mr;
107
108         /* Add a new entry, register MR first. */
109         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
110               (void *)txq_ctrl, mp->name, (void *)mp);
111         mr = priv_mr_get(priv, mp);
112         if (mr == NULL) {
113                 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
114                         DEBUG("Using unregistered mempool 0x%p(%s) in secondary process,"
115                              " please create mempool before rte_eth_dev_start()",
116                              (void *)mp, mp->name);
117                         return NULL;
118                 }
119                 mr = priv_mr_new(priv, mp);
120         }
121         if (unlikely(mr == NULL)) {
122                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
123                       (void *)txq_ctrl);
124                 return NULL;
125         }
126         if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
127                 /* Table is full, remove oldest entry. */
128                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
129                       (void *)txq_ctrl);
130                 --idx;
131                 priv_mr_release(priv, txq->mp2mr[0]);
132                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
133                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
134         }
135         /* Store the new entry. */
136         txq_ctrl->txq.mp2mr[idx] = mr;
137         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
138               (void *)txq_ctrl, mp->name, (void *)mp,
139               txq_ctrl->txq.mp2mr[idx]->lkey);
140         return mr;
141 }
142
143 /**
144  * Register a Memory Region (MR) <-> Memory Pool (MP) association in
145  * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
146  *
147  * This function should only be called by txq_mp2mr().
148  *
149  * @param txq
150  *   Pointer to TX queue structure.
151  * @param[in] mp
152  *   Memory Pool for which a Memory Region lkey must be returned.
153  * @param idx
154  *   Index of the next available entry.
155  *
156  * @return
157  *   mr on success, NULL on failure.
158  */
159 struct mlx5_mr*
160 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
161                    unsigned int idx)
162 {
163         struct mlx5_txq_ctrl *txq_ctrl =
164                 container_of(txq, struct mlx5_txq_ctrl, txq);
165         struct mlx5_mr *mr;
166
167         priv_lock(txq_ctrl->priv);
168         mr = priv_txq_mp2mr_reg(txq_ctrl->priv, txq, mp, idx);
169         priv_unlock(txq_ctrl->priv);
170         return mr;
171 }
172
173 struct mlx5_mp2mr_mbuf_check_data {
174         int ret;
175 };
176
177 /**
178  * Callback function for rte_mempool_obj_iter() to check whether a given
179  * mempool object looks like a mbuf.
180  *
181  * @param[in] mp
182  *   The mempool pointer
183  * @param[in] arg
184  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
185  *   return value.
186  * @param[in] obj
187  *   Object address.
188  * @param index
189  *   Object index, unused.
190  */
191 static void
192 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
193         uint32_t index __rte_unused)
194 {
195         struct mlx5_mp2mr_mbuf_check_data *data = arg;
196         struct rte_mbuf *buf = obj;
197
198         /*
199          * Check whether mbuf structure fits element size and whether mempool
200          * pointer is valid.
201          */
202         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
203                 data->ret = -1;
204 }
205
206 /**
207  * Iterator function for rte_mempool_walk() to register existing mempools and
208  * fill the MP to MR cache of a TX queue.
209  *
210  * @param[in] mp
211  *   Memory Pool to register.
212  * @param *arg
213  *   Pointer to TX queue structure.
214  */
215 void
216 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
217 {
218         struct priv *priv = (struct priv *)arg;
219         struct mlx5_mp2mr_mbuf_check_data data = {
220                 .ret = 0,
221         };
222         struct mlx5_mr *mr;
223
224         /* Register mempool only if the first element looks like a mbuf. */
225         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
226                         data.ret == -1)
227                 return;
228         mr = priv_mr_get(priv, mp);
229         if (mr) {
230                 priv_mr_release(priv, mr);
231                 return;
232         }
233         priv_mr_new(priv, mp);
234 }
235
236 /**
237  * Register a new memory region from the mempool and store it in the memory
238  * region list.
239  *
240  * @param  priv
241  *   Pointer to private structure.
242  * @param mp
243  *   Pointer to the memory pool to register.
244  * @return
245  *   The memory region on success.
246  */
247 struct mlx5_mr*
248 priv_mr_new(struct priv *priv, struct rte_mempool *mp)
249 {
250         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
251         uintptr_t start;
252         uintptr_t end;
253         unsigned int i;
254         struct mlx5_mr *mr;
255
256         mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
257         if (!mr) {
258                 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
259                 return NULL;
260         }
261         if (mlx5_check_mempool(mp, &start, &end) != 0) {
262                 ERROR("mempool %p: not virtually contiguous",
263                       (void *)mp);
264                 return NULL;
265         }
266         DEBUG("mempool %p area start=%p end=%p size=%zu",
267               (void *)mp, (void *)start, (void *)end,
268               (size_t)(end - start));
269         /* Save original addresses for exact MR lookup. */
270         mr->start = start;
271         mr->end = end;
272         /* Round start and end to page boundary if found in memory segments. */
273         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
274                 uintptr_t addr = (uintptr_t)ms[i].addr;
275                 size_t len = ms[i].len;
276                 unsigned int align = ms[i].hugepage_sz;
277
278                 if ((start > addr) && (start < addr + len))
279                         start = RTE_ALIGN_FLOOR(start, align);
280                 if ((end > addr) && (end < addr + len))
281                         end = RTE_ALIGN_CEIL(end, align);
282         }
283         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
284               (void *)mp, (void *)start, (void *)end,
285               (size_t)(end - start));
286         mr->mr = mlx5_glue->reg_mr(priv->pd, (void *)start, end - start,
287                                    IBV_ACCESS_LOCAL_WRITE);
288         mr->mp = mp;
289         mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
290         rte_atomic32_inc(&mr->refcnt);
291         DEBUG("%p: new Memory Region %p refcnt: %d", (void *)priv,
292               (void *)mr, rte_atomic32_read(&mr->refcnt));
293         LIST_INSERT_HEAD(&priv->mr, mr, next);
294         return mr;
295 }
296
297 /**
298  * Search the memory region object in the memory region list.
299  *
300  * @param  priv
301  *   Pointer to private structure.
302  * @param mp
303  *   Pointer to the memory pool to register.
304  * @return
305  *   The memory region on success.
306  */
307 struct mlx5_mr*
308 priv_mr_get(struct priv *priv, struct rte_mempool *mp)
309 {
310         struct mlx5_mr *mr;
311
312         assert(mp);
313         if (LIST_EMPTY(&priv->mr))
314                 return NULL;
315         LIST_FOREACH(mr, &priv->mr, next) {
316                 if (mr->mp == mp) {
317                         rte_atomic32_inc(&mr->refcnt);
318                         DEBUG("Memory Region %p refcnt: %d",
319                               (void *)mr, rte_atomic32_read(&mr->refcnt));
320                         return mr;
321                 }
322         }
323         return NULL;
324 }
325
326 /**
327  * Release the memory region object.
328  *
329  * @param  mr
330  *   Pointer to memory region to release.
331  *
332  * @return
333  *   0 on success, errno on failure.
334  */
335 int
336 priv_mr_release(struct priv *priv __rte_unused, struct mlx5_mr *mr)
337 {
338         assert(mr);
339         DEBUG("Memory Region %p refcnt: %d",
340               (void *)mr, rte_atomic32_read(&mr->refcnt));
341         if (rte_atomic32_dec_and_test(&mr->refcnt)) {
342                 claim_zero(mlx5_glue->dereg_mr(mr->mr));
343                 LIST_REMOVE(mr, next);
344                 rte_free(mr);
345                 return 0;
346         }
347         return EBUSY;
348 }
349
350 /**
351  * Verify the flow list is empty
352  *
353  * @param priv
354  *  Pointer to private structure.
355  *
356  * @return the number of object not released.
357  */
358 int
359 priv_mr_verify(struct priv *priv)
360 {
361         int ret = 0;
362         struct mlx5_mr *mr;
363
364         LIST_FOREACH(mr, &priv->mr, next) {
365                 DEBUG("%p: mr %p still referenced", (void *)priv,
366                       (void *)mr);
367                 ++ret;
368         }
369         return ret;
370 }