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