net/mlx4: fix alignment of memory region
[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         end = RTE_ALIGN_CEIL(end, ms->hugepage_sz);
148         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
149               (void *)mp, (void *)start, (void *)end,
150               (size_t)(end - start));
151         rte_spinlock_lock(&priv->mr_lock);
152         LIST_FOREACH(mr, &priv->mr, next)
153                 if (mp == mr->mp && start >= mr->start && end <= mr->end)
154                         break;
155         if (mr) {
156                 ++mr->refcnt;
157                 goto release;
158         }
159         mr = rte_malloc(__func__, sizeof(*mr), 0);
160         if (!mr) {
161                 rte_errno = ENOMEM;
162                 goto release;
163         }
164         *mr = (struct mlx4_mr){
165                 .start = start,
166                 .end = end,
167                 .refcnt = 1,
168                 .priv = priv,
169                 .mr = mlx4_glue->reg_mr(priv->pd, (void *)start, end - start,
170                                         IBV_ACCESS_LOCAL_WRITE),
171                 .mp = mp,
172         };
173         if (mr->mr) {
174                 mr->lkey = mr->mr->lkey;
175                 LIST_INSERT_HEAD(&priv->mr, mr, next);
176         } else {
177                 rte_free(mr);
178                 mr = NULL;
179                 rte_errno = errno ? errno : EINVAL;
180         }
181 release:
182         rte_spinlock_unlock(&priv->mr_lock);
183         return mr;
184 }
185
186 /**
187  * Release a memory region.
188  *
189  * This function decrements its reference count and destroys it after
190  * reaching 0.
191  *
192  * Note to avoid race conditions given this function may be used from the
193  * data plane, it's extremely important that each user holds its own
194  * reference.
195  *
196  * @param mr
197  *   Memory region to release.
198  */
199 void
200 mlx4_mr_put(struct mlx4_mr *mr)
201 {
202         struct priv *priv = mr->priv;
203
204         rte_spinlock_lock(&priv->mr_lock);
205         assert(mr->refcnt);
206         if (--mr->refcnt)
207                 goto release;
208         LIST_REMOVE(mr, next);
209         claim_zero(mlx4_glue->dereg_mr(mr->mr));
210         rte_free(mr);
211 release:
212         rte_spinlock_unlock(&priv->mr_lock);
213 }
214
215 /**
216  * Add memory region (MR) <-> memory pool (MP) association to txq->mp2mr[].
217  * If mp2mr[] is full, remove an entry first.
218  *
219  * @param txq
220  *   Pointer to Tx queue structure.
221  * @param[in] mp
222  *   Memory pool for which a memory region lkey must be added.
223  * @param[in] i
224  *   Index in memory pool (MP) where to add memory region (MR).
225  *
226  * @return
227  *   Added mr->lkey on success, (uint32_t)-1 on failure.
228  */
229 uint32_t
230 mlx4_txq_add_mr(struct txq *txq, struct rte_mempool *mp, uint32_t i)
231 {
232         struct mlx4_mr *mr;
233
234         /* Add a new entry, register MR first. */
235         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
236               (void *)txq, mp->name, (void *)mp);
237         mr = mlx4_mr_get(txq->priv, mp);
238         if (unlikely(mr == NULL)) {
239                 DEBUG("%p: unable to configure MR, mlx4_mr_get() failed",
240                       (void *)txq);
241                 return (uint32_t)-1;
242         }
243         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
244                 /* Table is full, remove oldest entry. */
245                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
246                       (void *)txq);
247                 --i;
248                 mlx4_mr_put(txq->mp2mr[0].mr);
249                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
250                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
251         }
252         /* Store the new entry. */
253         txq->mp2mr[i].mp = mp;
254         txq->mp2mr[i].mr = mr;
255         txq->mp2mr[i].lkey = mr->lkey;
256         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
257               (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
258         return txq->mp2mr[i].lkey;
259 }