884ac33eb2923dfef4447a010fab952f8a839382
[dpdk.git] / drivers / net / mlx5 / mlx5_mr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox.
4  */
5
6 #ifdef PEDANTIC
7 #pragma GCC diagnostic ignored "-Wpedantic"
8 #endif
9 #include <infiniband/verbs.h>
10 #ifdef PEDANTIC
11 #pragma GCC diagnostic error "-Wpedantic"
12 #endif
13
14 #include <rte_mempool.h>
15 #include <rte_malloc.h>
16
17 #include "mlx5.h"
18 #include "mlx5_rxtx.h"
19 #include "mlx5_glue.h"
20
21 struct mlx5_check_mempool_data {
22         int ret;
23         char *start;
24         char *end;
25 };
26
27 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
28 static void
29 mlx5_check_mempool_cb(struct rte_mempool *mp __rte_unused,
30                       void *opaque, struct rte_mempool_memhdr *memhdr,
31                       unsigned int mem_idx __rte_unused)
32 {
33         struct mlx5_check_mempool_data *data = opaque;
34
35         /* It already failed, skip the next chunks. */
36         if (data->ret != 0)
37                 return;
38         /* It is the first chunk. */
39         if (data->start == NULL && data->end == NULL) {
40                 data->start = memhdr->addr;
41                 data->end = data->start + memhdr->len;
42                 return;
43         }
44         if (data->end == memhdr->addr) {
45                 data->end += memhdr->len;
46                 return;
47         }
48         if (data->start == (char *)memhdr->addr + memhdr->len) {
49                 data->start -= memhdr->len;
50                 return;
51         }
52         /* Error, mempool is not virtually contiguous. */
53         data->ret = -1;
54 }
55
56 /**
57  * Check if a mempool can be used: it must be virtually contiguous.
58  *
59  * @param[in] mp
60  *   Pointer to memory pool.
61  * @param[out] start
62  *   Pointer to the start address of the mempool virtual memory area
63  * @param[out] end
64  *   Pointer to the end address of the mempool virtual memory area
65  *
66  * @return
67  *   0 on success (mempool is virtually contiguous), -1 on error.
68  */
69 static int
70 mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
71                    uintptr_t *end)
72 {
73         struct mlx5_check_mempool_data data;
74
75         memset(&data, 0, sizeof(data));
76         rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
77         *start = (uintptr_t)data.start;
78         *end = (uintptr_t)data.end;
79         return data.ret;
80 }
81
82 /**
83  * Register a Memory Region (MR) <-> Memory Pool (MP) association in
84  * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
85  *
86  * @param txq
87  *   Pointer to TX queue structure.
88  * @param[in] mp
89  *   Memory Pool for which a Memory Region lkey must be returned.
90  * @param idx
91  *   Index of the next available entry.
92  *
93  * @return
94  *   mr on success, NULL on failure and rte_errno is set.
95  */
96 struct mlx5_mr *
97 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
98                    unsigned int idx)
99 {
100         struct mlx5_txq_ctrl *txq_ctrl =
101                 container_of(txq, struct mlx5_txq_ctrl, txq);
102         struct rte_eth_dev *dev;
103         struct mlx5_mr *mr;
104
105         rte_spinlock_lock(&txq_ctrl->priv->mr_lock);
106         /* Add a new entry, register MR first. */
107         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
108               (void *)txq_ctrl, mp->name, (void *)mp);
109         dev = txq_ctrl->priv->dev;
110         mr = mlx5_mr_get(dev, mp);
111         if (mr == NULL) {
112                 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
113                         DEBUG("Using unregistered mempool 0x%p(%s) in "
114                               "secondary process, please create mempool before "
115                               " rte_eth_dev_start()",
116                              (void *)mp, mp->name);
117                         rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
118                         rte_errno = ENOTSUP;
119                         return NULL;
120                 }
121                 mr = mlx5_mr_new(dev, mp);
122         }
123         if (unlikely(mr == NULL)) {
124                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
125                       (void *)txq_ctrl);
126                 rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
127                 return NULL;
128         }
129         if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
130                 /* Table is full, remove oldest entry. */
131                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
132                       (void *)txq_ctrl);
133                 --idx;
134                 mlx5_mr_release(txq->mp2mr[0]);
135                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
136                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
137         }
138         /* Store the new entry. */
139         txq_ctrl->txq.mp2mr[idx] = mr;
140         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
141               (void *)txq_ctrl, mp->name, (void *)mp,
142               txq_ctrl->txq.mp2mr[idx]->lkey);
143         rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
144         return mr;
145 }
146
147 struct mlx5_mp2mr_mbuf_check_data {
148         int ret;
149 };
150
151 /**
152  * Callback function for rte_mempool_obj_iter() to check whether a given
153  * mempool object looks like a mbuf.
154  *
155  * @param[in] mp
156  *   The mempool pointer
157  * @param[in] arg
158  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
159  *   return value.
160  * @param[in] obj
161  *   Object address.
162  * @param index
163  *   Object index, unused.
164  */
165 static void
166 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
167         uint32_t index __rte_unused)
168 {
169         struct mlx5_mp2mr_mbuf_check_data *data = arg;
170         struct rte_mbuf *buf = obj;
171
172         /*
173          * Check whether mbuf structure fits element size and whether mempool
174          * pointer is valid.
175          */
176         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
177                 data->ret = -1;
178 }
179
180 /**
181  * Iterator function for rte_mempool_walk() to register existing mempools and
182  * fill the MP to MR cache of a TX queue.
183  *
184  * @param[in] mp
185  *   Memory Pool to register.
186  * @param *arg
187  *   Pointer to TX queue structure.
188  */
189 void
190 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
191 {
192         struct priv *priv = (struct priv *)arg;
193         struct mlx5_mp2mr_mbuf_check_data data = {
194                 .ret = 0,
195         };
196         struct mlx5_mr *mr;
197
198         /* Register mempool only if the first element looks like a mbuf. */
199         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
200                         data.ret == -1)
201                 return;
202         mr = mlx5_mr_get(priv->dev, mp);
203         if (mr) {
204                 mlx5_mr_release(mr);
205                 return;
206         }
207         mr = mlx5_mr_new(priv->dev, mp);
208         if (!mr)
209                 ERROR("cannot create memory region: %s", strerror(rte_errno));
210 }
211
212 /**
213  * Register a new memory region from the mempool and store it in the memory
214  * region list.
215  *
216  * @param dev
217  *   Pointer to Ethernet device.
218  * @param mp
219  *   Pointer to the memory pool to register.
220  *
221  * @return
222  *   The memory region on success, NULL on failure and rte_errno is set.
223  */
224 struct mlx5_mr *
225 mlx5_mr_new(struct rte_eth_dev *dev, struct rte_mempool *mp)
226 {
227         struct priv *priv = dev->data->dev_private;
228         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
229         uintptr_t start;
230         uintptr_t end;
231         unsigned int i;
232         struct mlx5_mr *mr;
233
234         mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
235         if (!mr) {
236                 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
237                 rte_errno = ENOMEM;
238                 return NULL;
239         }
240         if (mlx5_check_mempool(mp, &start, &end) != 0) {
241                 ERROR("mempool %p: not virtually contiguous",
242                       (void *)mp);
243                 rte_errno = ENOMEM;
244                 return NULL;
245         }
246         DEBUG("mempool %p area start=%p end=%p size=%zu",
247               (void *)mp, (void *)start, (void *)end,
248               (size_t)(end - start));
249         /* Save original addresses for exact MR lookup. */
250         mr->start = start;
251         mr->end = end;
252         /* Round start and end to page boundary if found in memory segments. */
253         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
254                 uintptr_t addr = (uintptr_t)ms[i].addr;
255                 size_t len = ms[i].len;
256                 unsigned int align = ms[i].hugepage_sz;
257
258                 if ((start > addr) && (start < addr + len))
259                         start = RTE_ALIGN_FLOOR(start, align);
260                 if ((end > addr) && (end < addr + len))
261                         end = RTE_ALIGN_CEIL(end, align);
262         }
263         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
264               (void *)mp, (void *)start, (void *)end,
265               (size_t)(end - start));
266         mr->mr = mlx5_glue->reg_mr(priv->pd, (void *)start, end - start,
267                                    IBV_ACCESS_LOCAL_WRITE);
268         if (!mr->mr) {
269                 rte_errno = ENOMEM;
270                 return NULL;
271         }
272         mr->mp = mp;
273         mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
274         rte_atomic32_inc(&mr->refcnt);
275         DEBUG("%p: new Memory Region %p refcnt: %d", (void *)dev,
276               (void *)mr, rte_atomic32_read(&mr->refcnt));
277         LIST_INSERT_HEAD(&priv->mr, mr, next);
278         return mr;
279 }
280
281 /**
282  * Search the memory region object in the memory region list.
283  *
284  * @param dev
285  *   Pointer to Ethernet device.
286  * @param mp
287  *   Pointer to the memory pool to register.
288  *
289  * @return
290  *   The memory region on success.
291  */
292 struct mlx5_mr *
293 mlx5_mr_get(struct rte_eth_dev *dev, struct rte_mempool *mp)
294 {
295         struct priv *priv = dev->data->dev_private;
296         struct mlx5_mr *mr;
297
298         assert(mp);
299         if (LIST_EMPTY(&priv->mr))
300                 return NULL;
301         LIST_FOREACH(mr, &priv->mr, next) {
302                 if (mr->mp == mp) {
303                         rte_atomic32_inc(&mr->refcnt);
304                         DEBUG("Memory Region %p refcnt: %d",
305                               (void *)mr, rte_atomic32_read(&mr->refcnt));
306                         return mr;
307                 }
308         }
309         return NULL;
310 }
311
312 /**
313  * Release the memory region object.
314  *
315  * @param  mr
316  *   Pointer to memory region to release.
317  *
318  * @return
319  *   1 while a reference on it exists, 0 when freed.
320  */
321 int
322 mlx5_mr_release(struct mlx5_mr *mr)
323 {
324         assert(mr);
325         DEBUG("Memory Region %p refcnt: %d",
326               (void *)mr, rte_atomic32_read(&mr->refcnt));
327         if (rte_atomic32_dec_and_test(&mr->refcnt)) {
328                 claim_zero(mlx5_glue->dereg_mr(mr->mr));
329                 LIST_REMOVE(mr, next);
330                 rte_free(mr);
331                 return 0;
332         }
333         return 1;
334 }
335
336 /**
337  * Verify the flow list is empty
338  *
339  * @param dev
340  *   Pointer to Ethernet device.
341  *
342  * @return
343  *   The number of object not released.
344  */
345 int
346 mlx5_mr_verify(struct rte_eth_dev *dev)
347 {
348         struct priv *priv = dev->data->dev_private;
349         int ret = 0;
350         struct mlx5_mr *mr;
351
352         LIST_FOREACH(mr, &priv->mr, next) {
353                 DEBUG("%p: mr %p still referenced", (void *)dev,
354                       (void *)mr);
355                 ++ret;
356         }
357         return ret;
358 }