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