28769d386e9fc3e4fe1101add6bb71248876a660
[dpdk.git] / drivers / net / mlx5 / mlx5_mr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 6WIND S.A.
5  *   Copyright 2016 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /* Verbs header. */
35 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
36 #ifdef PEDANTIC
37 #pragma GCC diagnostic ignored "-Wpedantic"
38 #endif
39 #include <infiniband/verbs.h>
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic error "-Wpedantic"
42 #endif
43
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
46
47 #include "mlx5.h"
48 #include "mlx5_rxtx.h"
49 #include "mlx5_glue.h"
50
51 struct mlx5_check_mempool_data {
52         int ret;
53         char *start;
54         char *end;
55 };
56
57 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
58 static void
59 mlx5_check_mempool_cb(struct rte_mempool *mp,
60                       void *opaque, struct rte_mempool_memhdr *memhdr,
61                       unsigned int mem_idx)
62 {
63         struct mlx5_check_mempool_data *data = opaque;
64
65         (void)mp;
66         (void)mem_idx;
67
68         /* It already failed, skip the next chunks. */
69         if (data->ret != 0)
70                 return;
71         /* It is the first chunk. */
72         if (data->start == NULL && data->end == NULL) {
73                 data->start = memhdr->addr;
74                 data->end = data->start + memhdr->len;
75                 return;
76         }
77         if (data->end == memhdr->addr) {
78                 data->end += memhdr->len;
79                 return;
80         }
81         if (data->start == (char *)memhdr->addr + memhdr->len) {
82                 data->start -= memhdr->len;
83                 return;
84         }
85         /* Error, mempool is not virtually contiguous. */
86         data->ret = -1;
87 }
88
89 /**
90  * Check if a mempool can be used: it must be virtually contiguous.
91  *
92  * @param[in] mp
93  *   Pointer to memory pool.
94  * @param[out] start
95  *   Pointer to the start address of the mempool virtual memory area
96  * @param[out] end
97  *   Pointer to the end address of the mempool virtual memory area
98  *
99  * @return
100  *   0 on success (mempool is virtually contiguous), -1 on error.
101  */
102 static int mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
103         uintptr_t *end)
104 {
105         struct mlx5_check_mempool_data data;
106
107         memset(&data, 0, sizeof(data));
108         rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
109         *start = (uintptr_t)data.start;
110         *end = (uintptr_t)data.end;
111
112         return data.ret;
113 }
114
115 /**
116  * Register a Memory Region (MR) <-> Memory Pool (MP) association in
117  * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
118  *
119  * This function should only be called by txq_mp2mr().
120  *
121  * @param priv
122  *   Pointer to private structure.
123  * @param txq
124  *   Pointer to TX queue structure.
125  * @param[in] mp
126  *   Memory Pool for which a Memory Region lkey must be returned.
127  * @param idx
128  *   Index of the next available entry.
129  *
130  * @return
131  *   mr on success, NULL on failure.
132  */
133 struct mlx5_mr*
134 priv_txq_mp2mr_reg(struct priv *priv, struct mlx5_txq_data *txq,
135                    struct rte_mempool *mp, unsigned int idx)
136 {
137         struct mlx5_txq_ctrl *txq_ctrl =
138                 container_of(txq, struct mlx5_txq_ctrl, txq);
139         struct mlx5_mr *mr;
140
141         /* Add a new entry, register MR first. */
142         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
143               (void *)txq_ctrl, mp->name, (void *)mp);
144         mr = priv_mr_get(priv, mp);
145         if (mr == NULL) {
146                 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
147                         DEBUG("Using unregistered mempool 0x%p(%s) in secondary process,"
148                              " please create mempool before rte_eth_dev_start()",
149                              (void *)mp, mp->name);
150                         return NULL;
151                 }
152                 mr = priv_mr_new(priv, mp);
153         }
154         if (unlikely(mr == NULL)) {
155                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
156                       (void *)txq_ctrl);
157                 return NULL;
158         }
159         if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
160                 /* Table is full, remove oldest entry. */
161                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
162                       (void *)txq_ctrl);
163                 --idx;
164                 priv_mr_release(priv, txq->mp2mr[0]);
165                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
166                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
167         }
168         /* Store the new entry. */
169         txq_ctrl->txq.mp2mr[idx] = mr;
170         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
171               (void *)txq_ctrl, mp->name, (void *)mp,
172               txq_ctrl->txq.mp2mr[idx]->lkey);
173         return mr;
174 }
175
176 /**
177  * Register a Memory Region (MR) <-> Memory Pool (MP) association in
178  * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
179  *
180  * This function should only be called by txq_mp2mr().
181  *
182  * @param txq
183  *   Pointer to TX queue structure.
184  * @param[in] mp
185  *   Memory Pool for which a Memory Region lkey must be returned.
186  * @param idx
187  *   Index of the next available entry.
188  *
189  * @return
190  *   mr on success, NULL on failure.
191  */
192 struct mlx5_mr*
193 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
194                    unsigned int idx)
195 {
196         struct mlx5_txq_ctrl *txq_ctrl =
197                 container_of(txq, struct mlx5_txq_ctrl, txq);
198         struct mlx5_mr *mr;
199
200         priv_lock(txq_ctrl->priv);
201         mr = priv_txq_mp2mr_reg(txq_ctrl->priv, txq, mp, idx);
202         priv_unlock(txq_ctrl->priv);
203         return mr;
204 }
205
206 struct mlx5_mp2mr_mbuf_check_data {
207         int ret;
208 };
209
210 /**
211  * Callback function for rte_mempool_obj_iter() to check whether a given
212  * mempool object looks like a mbuf.
213  *
214  * @param[in] mp
215  *   The mempool pointer
216  * @param[in] arg
217  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
218  *   return value.
219  * @param[in] obj
220  *   Object address.
221  * @param index
222  *   Object index, unused.
223  */
224 static void
225 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
226         uint32_t index __rte_unused)
227 {
228         struct mlx5_mp2mr_mbuf_check_data *data = arg;
229         struct rte_mbuf *buf = obj;
230
231         /*
232          * Check whether mbuf structure fits element size and whether mempool
233          * pointer is valid.
234          */
235         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
236                 data->ret = -1;
237 }
238
239 /**
240  * Iterator function for rte_mempool_walk() to register existing mempools and
241  * fill the MP to MR cache of a TX queue.
242  *
243  * @param[in] mp
244  *   Memory Pool to register.
245  * @param *arg
246  *   Pointer to TX queue structure.
247  */
248 void
249 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
250 {
251         struct priv *priv = (struct priv *)arg;
252         struct mlx5_mp2mr_mbuf_check_data data = {
253                 .ret = 0,
254         };
255         struct mlx5_mr *mr;
256
257         /* Register mempool only if the first element looks like a mbuf. */
258         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
259                         data.ret == -1)
260                 return;
261         mr = priv_mr_get(priv, mp);
262         if (mr) {
263                 priv_mr_release(priv, mr);
264                 return;
265         }
266         priv_mr_new(priv, mp);
267 }
268
269 /**
270  * Register a new memory region from the mempool and store it in the memory
271  * region list.
272  *
273  * @param  priv
274  *   Pointer to private structure.
275  * @param mp
276  *   Pointer to the memory pool to register.
277  * @return
278  *   The memory region on success.
279  */
280 struct mlx5_mr*
281 priv_mr_new(struct priv *priv, struct rte_mempool *mp)
282 {
283         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
284         uintptr_t start;
285         uintptr_t end;
286         unsigned int i;
287         struct mlx5_mr *mr;
288
289         mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
290         if (!mr) {
291                 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
292                 return NULL;
293         }
294         if (mlx5_check_mempool(mp, &start, &end) != 0) {
295                 ERROR("mempool %p: not virtually contiguous",
296                       (void *)mp);
297                 return NULL;
298         }
299         DEBUG("mempool %p area start=%p end=%p size=%zu",
300               (void *)mp, (void *)start, (void *)end,
301               (size_t)(end - start));
302         /* Save original addresses for exact MR lookup. */
303         mr->start = start;
304         mr->end = end;
305         /* Round start and end to page boundary if found in memory segments. */
306         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
307                 uintptr_t addr = (uintptr_t)ms[i].addr;
308                 size_t len = ms[i].len;
309                 unsigned int align = ms[i].hugepage_sz;
310
311                 if ((start > addr) && (start < addr + len))
312                         start = RTE_ALIGN_FLOOR(start, align);
313                 if ((end > addr) && (end < addr + len))
314                         end = RTE_ALIGN_CEIL(end, align);
315         }
316         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
317               (void *)mp, (void *)start, (void *)end,
318               (size_t)(end - start));
319         mr->mr = mlx5_glue->reg_mr(priv->pd, (void *)start, end - start,
320                                    IBV_ACCESS_LOCAL_WRITE);
321         mr->mp = mp;
322         mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
323         rte_atomic32_inc(&mr->refcnt);
324         DEBUG("%p: new Memory Region %p refcnt: %d", (void *)priv,
325               (void *)mr, rte_atomic32_read(&mr->refcnt));
326         LIST_INSERT_HEAD(&priv->mr, mr, next);
327         return mr;
328 }
329
330 /**
331  * Search the memory region object in the memory region list.
332  *
333  * @param  priv
334  *   Pointer to private structure.
335  * @param mp
336  *   Pointer to the memory pool to register.
337  * @return
338  *   The memory region on success.
339  */
340 struct mlx5_mr*
341 priv_mr_get(struct priv *priv, struct rte_mempool *mp)
342 {
343         struct mlx5_mr *mr;
344
345         assert(mp);
346         if (LIST_EMPTY(&priv->mr))
347                 return NULL;
348         LIST_FOREACH(mr, &priv->mr, next) {
349                 if (mr->mp == mp) {
350                         rte_atomic32_inc(&mr->refcnt);
351                         DEBUG("Memory Region %p refcnt: %d",
352                               (void *)mr, rte_atomic32_read(&mr->refcnt));
353                         return mr;
354                 }
355         }
356         return NULL;
357 }
358
359 /**
360  * Release the memory region object.
361  *
362  * @param  mr
363  *   Pointer to memory region to release.
364  *
365  * @return
366  *   0 on success, errno on failure.
367  */
368 int
369 priv_mr_release(struct priv *priv, struct mlx5_mr *mr)
370 {
371         (void)priv;
372         assert(mr);
373         DEBUG("Memory Region %p refcnt: %d",
374               (void *)mr, rte_atomic32_read(&mr->refcnt));
375         if (rte_atomic32_dec_and_test(&mr->refcnt)) {
376                 claim_zero(mlx5_glue->dereg_mr(mr->mr));
377                 LIST_REMOVE(mr, next);
378                 rte_free(mr);
379                 return 0;
380         }
381         return EBUSY;
382 }
383
384 /**
385  * Verify the flow list is empty
386  *
387  * @param priv
388  *  Pointer to private structure.
389  *
390  * @return the number of object not released.
391  */
392 int
393 priv_mr_verify(struct priv *priv)
394 {
395         int ret = 0;
396         struct mlx5_mr *mr;
397
398         LIST_FOREACH(mr, &priv->mr, next) {
399                 DEBUG("%p: mr %p still referenced", (void *)priv,
400                       (void *)mr);
401                 ++ret;
402         }
403         return ret;
404 }