net/mlx4: move rdma-core calls to separate file
[dpdk.git] / drivers / net / mlx4 / mlx4_mr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 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 /**
35  * @file
36  * Memory management functions for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <string.h>
45
46 /* Verbs headers do not support -pedantic. */
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic ignored "-Wpedantic"
49 #endif
50 #include <infiniband/verbs.h>
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic error "-Wpedantic"
53 #endif
54
55 #include <rte_branch_prediction.h>
56 #include <rte_common.h>
57 #include <rte_errno.h>
58 #include <rte_malloc.h>
59 #include <rte_memory.h>
60 #include <rte_mempool.h>
61 #include <rte_spinlock.h>
62
63 #include "mlx4_glue.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
66
67 struct mlx4_check_mempool_data {
68         int ret;
69         char *start;
70         char *end;
71 };
72
73 /**
74  * Called by mlx4_check_mempool() when iterating the memory chunks.
75  *
76  * @param[in] mp
77  *   Pointer to memory pool (unused).
78  * @param[in, out] data
79  *   Pointer to shared buffer with mlx4_check_mempool().
80  * @param[in] memhdr
81  *   Pointer to mempool chunk header.
82  * @param mem_idx
83  *   Mempool element index (unused).
84  */
85 static void
86 mlx4_check_mempool_cb(struct rte_mempool *mp, void *opaque,
87                       struct rte_mempool_memhdr *memhdr,
88                       unsigned int mem_idx)
89 {
90         struct mlx4_check_mempool_data *data = opaque;
91
92         (void)mp;
93         (void)mem_idx;
94         /* It already failed, skip the next chunks. */
95         if (data->ret != 0)
96                 return;
97         /* It is the first chunk. */
98         if (data->start == NULL && data->end == NULL) {
99                 data->start = memhdr->addr;
100                 data->end = data->start + memhdr->len;
101                 return;
102         }
103         if (data->end == memhdr->addr) {
104                 data->end += memhdr->len;
105                 return;
106         }
107         if (data->start == (char *)memhdr->addr + memhdr->len) {
108                 data->start -= memhdr->len;
109                 return;
110         }
111         /* Error, mempool is not virtually contiguous. */
112         data->ret = -1;
113 }
114
115 /**
116  * Check if a mempool can be used: it must be virtually contiguous.
117  *
118  * @param[in] mp
119  *   Pointer to memory pool.
120  * @param[out] start
121  *   Pointer to the start address of the mempool virtual memory area.
122  * @param[out] end
123  *   Pointer to the end address of the mempool virtual memory area.
124  *
125  * @return
126  *   0 on success (mempool is virtually contiguous), -1 on error.
127  */
128 static int
129 mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start, uintptr_t *end)
130 {
131         struct mlx4_check_mempool_data data;
132
133         memset(&data, 0, sizeof(data));
134         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
135         *start = (uintptr_t)data.start;
136         *end = (uintptr_t)data.end;
137         return data.ret;
138 }
139
140 /**
141  * Obtain a memory region from a memory pool.
142  *
143  * If a matching memory region already exists, it is returned with its
144  * reference count incremented, otherwise a new one is registered.
145  *
146  * @param priv
147  *   Pointer to private structure.
148  * @param mp
149  *   Pointer to memory pool.
150  *
151  * @return
152  *   Memory region pointer, NULL in case of error and rte_errno is set.
153  */
154 struct mlx4_mr *
155 mlx4_mr_get(struct priv *priv, struct rte_mempool *mp)
156 {
157         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
158         uintptr_t start;
159         uintptr_t end;
160         unsigned int i;
161         struct mlx4_mr *mr;
162
163         if (mlx4_check_mempool(mp, &start, &end) != 0) {
164                 rte_errno = EINVAL;
165                 ERROR("mempool %p: not virtually contiguous",
166                         (void *)mp);
167                 return NULL;
168         }
169         DEBUG("mempool %p area start=%p end=%p size=%zu",
170               (void *)mp, (void *)start, (void *)end,
171               (size_t)(end - start));
172         /* Round start and end to page boundary if found in memory segments. */
173         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
174                 uintptr_t addr = (uintptr_t)ms[i].addr;
175                 size_t len = ms[i].len;
176                 unsigned int align = ms[i].hugepage_sz;
177
178                 if ((start > addr) && (start < addr + len))
179                         start = RTE_ALIGN_FLOOR(start, align);
180                 if ((end > addr) && (end < addr + len))
181                         end = RTE_ALIGN_CEIL(end, align);
182         }
183         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
184               (void *)mp, (void *)start, (void *)end,
185               (size_t)(end - start));
186         rte_spinlock_lock(&priv->mr_lock);
187         LIST_FOREACH(mr, &priv->mr, next)
188                 if (mp == mr->mp && start >= mr->start && end <= mr->end)
189                         break;
190         if (mr) {
191                 ++mr->refcnt;
192                 goto release;
193         }
194         mr = rte_malloc(__func__, sizeof(*mr), 0);
195         if (!mr) {
196                 rte_errno = ENOMEM;
197                 goto release;
198         }
199         *mr = (struct mlx4_mr){
200                 .start = start,
201                 .end = end,
202                 .refcnt = 1,
203                 .priv = priv,
204                 .mr = mlx4_glue->reg_mr(priv->pd, (void *)start, end - start,
205                                         IBV_ACCESS_LOCAL_WRITE),
206                 .mp = mp,
207         };
208         if (mr->mr) {
209                 mr->lkey = mr->mr->lkey;
210                 LIST_INSERT_HEAD(&priv->mr, mr, next);
211         } else {
212                 rte_free(mr);
213                 mr = NULL;
214                 rte_errno = errno ? errno : EINVAL;
215         }
216 release:
217         rte_spinlock_unlock(&priv->mr_lock);
218         return mr;
219 }
220
221 /**
222  * Release a memory region.
223  *
224  * This function decrements its reference count and destroys it after
225  * reaching 0.
226  *
227  * Note to avoid race conditions given this function may be used from the
228  * data plane, it's extremely important that each user holds its own
229  * reference.
230  *
231  * @param mr
232  *   Memory region to release.
233  */
234 void
235 mlx4_mr_put(struct mlx4_mr *mr)
236 {
237         struct priv *priv = mr->priv;
238
239         rte_spinlock_lock(&priv->mr_lock);
240         assert(mr->refcnt);
241         if (--mr->refcnt)
242                 goto release;
243         LIST_REMOVE(mr, next);
244         claim_zero(mlx4_glue->dereg_mr(mr->mr));
245         rte_free(mr);
246 release:
247         rte_spinlock_unlock(&priv->mr_lock);
248 }
249
250 /**
251  * Add memory region (MR) <-> memory pool (MP) association to txq->mp2mr[].
252  * If mp2mr[] is full, remove an entry first.
253  *
254  * @param txq
255  *   Pointer to Tx queue structure.
256  * @param[in] mp
257  *   Memory pool for which a memory region lkey must be added.
258  * @param[in] i
259  *   Index in memory pool (MP) where to add memory region (MR).
260  *
261  * @return
262  *   Added mr->lkey on success, (uint32_t)-1 on failure.
263  */
264 uint32_t
265 mlx4_txq_add_mr(struct txq *txq, struct rte_mempool *mp, uint32_t i)
266 {
267         struct mlx4_mr *mr;
268
269         /* Add a new entry, register MR first. */
270         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
271               (void *)txq, mp->name, (void *)mp);
272         mr = mlx4_mr_get(txq->priv, mp);
273         if (unlikely(mr == NULL)) {
274                 DEBUG("%p: unable to configure MR, mlx4_mr_get() failed",
275                       (void *)txq);
276                 return (uint32_t)-1;
277         }
278         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
279                 /* Table is full, remove oldest entry. */
280                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
281                       (void *)txq);
282                 --i;
283                 mlx4_mr_put(txq->mp2mr[0].mr);
284                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
285                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
286         }
287         /* Store the new entry. */
288         txq->mp2mr[i].mp = mp;
289         txq->mp2mr[i].mr = mr;
290         txq->mp2mr[i].lkey = mr->lkey;
291         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
292               (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
293         return txq->mp2mr[i].lkey;
294 }