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