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