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