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