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