net/mlx5: use dynamic logging
[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         DRV_LOG(DEBUG, "port %u discovered new memory pool \"%s\" (%p)",
108                 txq_ctrl->priv->dev->data->port_id, 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                         DRV_LOG(DEBUG,
114                                 "port %u using unregistered mempool 0x%p(%s)"
115                                 " in secondary process, please create mempool"
116                                 " before rte_eth_dev_start()",
117                                 txq_ctrl->priv->dev->data->port_id,
118                                 (void *)mp, mp->name);
119                         rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
120                         rte_errno = ENOTSUP;
121                         return NULL;
122                 }
123                 mr = mlx5_mr_new(dev, mp);
124         }
125         if (unlikely(mr == NULL)) {
126                 DRV_LOG(DEBUG,
127                         "port %u unable to configure memory region,"
128                         " ibv_reg_mr() failed.",
129                         txq_ctrl->priv->dev->data->port_id);
130                 rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
131                 return NULL;
132         }
133         if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
134                 /* Table is full, remove oldest entry. */
135                 DRV_LOG(DEBUG,
136                         "port %u memory region <-> memory pool table full, "
137                         " dropping oldest entry",
138                         txq_ctrl->priv->dev->data->port_id);
139                 --idx;
140                 mlx5_mr_release(txq->mp2mr[0]);
141                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
142                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
143         }
144         /* Store the new entry. */
145         txq_ctrl->txq.mp2mr[idx] = mr;
146         DRV_LOG(DEBUG,
147                 "port %u new memory region lkey for MP \"%s\" (%p): 0x%08"
148                 PRIu32,
149                 txq_ctrl->priv->dev->data->port_id, mp->name, (void *)mp,
150                 txq_ctrl->txq.mp2mr[idx]->lkey);
151         rte_spinlock_unlock(&txq_ctrl->priv->mr_lock);
152         return mr;
153 }
154
155 struct mlx5_mp2mr_mbuf_check_data {
156         int ret;
157 };
158
159 /**
160  * Callback function for rte_mempool_obj_iter() to check whether a given
161  * mempool object looks like a mbuf.
162  *
163  * @param[in] mp
164  *   The mempool pointer
165  * @param[in] arg
166  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
167  *   return value.
168  * @param[in] obj
169  *   Object address.
170  * @param index
171  *   Object index, unused.
172  */
173 static void
174 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
175         uint32_t index __rte_unused)
176 {
177         struct mlx5_mp2mr_mbuf_check_data *data = arg;
178         struct rte_mbuf *buf = obj;
179
180         /*
181          * Check whether mbuf structure fits element size and whether mempool
182          * pointer is valid.
183          */
184         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
185                 data->ret = -1;
186 }
187
188 /**
189  * Iterator function for rte_mempool_walk() to register existing mempools and
190  * fill the MP to MR cache of a TX queue.
191  *
192  * @param[in] mp
193  *   Memory Pool to register.
194  * @param *arg
195  *   Pointer to TX queue structure.
196  */
197 void
198 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
199 {
200         struct priv *priv = (struct priv *)arg;
201         struct mlx5_mp2mr_mbuf_check_data data = {
202                 .ret = 0,
203         };
204         struct mlx5_mr *mr;
205
206         /* Register mempool only if the first element looks like a mbuf. */
207         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
208                         data.ret == -1)
209                 return;
210         mr = mlx5_mr_get(priv->dev, mp);
211         if (mr) {
212                 mlx5_mr_release(mr);
213                 return;
214         }
215         mr = mlx5_mr_new(priv->dev, mp);
216         if (!mr)
217                 DRV_LOG(ERR, "port %u cannot create memory region: %s",
218                         priv->dev->data->port_id, strerror(rte_errno));
219 }
220
221 /**
222  * Register a new memory region from the mempool and store it in the memory
223  * region list.
224  *
225  * @param dev
226  *   Pointer to Ethernet device.
227  * @param mp
228  *   Pointer to the memory pool to register.
229  *
230  * @return
231  *   The memory region on success, NULL on failure and rte_errno is set.
232  */
233 struct mlx5_mr *
234 mlx5_mr_new(struct rte_eth_dev *dev, struct rte_mempool *mp)
235 {
236         struct priv *priv = dev->data->dev_private;
237         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
238         uintptr_t start;
239         uintptr_t end;
240         unsigned int i;
241         struct mlx5_mr *mr;
242
243         mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
244         if (!mr) {
245                 DRV_LOG(DEBUG,
246                         "port %u unable to configure memory region,"
247                         " ibv_reg_mr() failed.",
248                         dev->data->port_id);
249                 rte_errno = ENOMEM;
250                 return NULL;
251         }
252         if (mlx5_check_mempool(mp, &start, &end) != 0) {
253                 DRV_LOG(ERR, "port %u mempool %p: not virtually contiguous",
254                         dev->data->port_id, (void *)mp);
255                 rte_errno = ENOMEM;
256                 return NULL;
257         }
258         DRV_LOG(DEBUG, "port %u mempool %p area start=%p end=%p size=%zu",
259                 dev->data->port_id, (void *)mp, (void *)start, (void *)end,
260                 (size_t)(end - start));
261         /* Save original addresses for exact MR lookup. */
262         mr->start = start;
263         mr->end = end;
264         /* Round start and end to page boundary if found in memory segments. */
265         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
266                 uintptr_t addr = (uintptr_t)ms[i].addr;
267                 size_t len = ms[i].len;
268                 unsigned int align = ms[i].hugepage_sz;
269
270                 if ((start > addr) && (start < addr + len))
271                         start = RTE_ALIGN_FLOOR(start, align);
272                 if ((end > addr) && (end < addr + len))
273                         end = RTE_ALIGN_CEIL(end, align);
274         }
275         DRV_LOG(DEBUG,
276                 "port %u mempool %p using start=%p end=%p size=%zu for memory"
277                 " region",
278                 dev->data->port_id, (void *)mp, (void *)start, (void *)end,
279                 (size_t)(end - start));
280         mr->mr = mlx5_glue->reg_mr(priv->pd, (void *)start, end - start,
281                                    IBV_ACCESS_LOCAL_WRITE);
282         if (!mr->mr) {
283                 rte_errno = ENOMEM;
284                 return NULL;
285         }
286         mr->mp = mp;
287         mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
288         rte_atomic32_inc(&mr->refcnt);
289         DRV_LOG(DEBUG, "port %u new memory Region %p refcnt: %d",
290                 dev->data->port_id, (void *)mr, rte_atomic32_read(&mr->refcnt));
291         LIST_INSERT_HEAD(&priv->mr, mr, next);
292         return mr;
293 }
294
295 /**
296  * Search the memory region object in the memory region list.
297  *
298  * @param dev
299  *   Pointer to Ethernet device.
300  * @param mp
301  *   Pointer to the memory pool to register.
302  *
303  * @return
304  *   The memory region on success.
305  */
306 struct mlx5_mr *
307 mlx5_mr_get(struct rte_eth_dev *dev, struct rte_mempool *mp)
308 {
309         struct priv *priv = dev->data->dev_private;
310         struct mlx5_mr *mr;
311
312         assert(mp);
313         if (LIST_EMPTY(&priv->mr))
314                 return NULL;
315         LIST_FOREACH(mr, &priv->mr, next) {
316                 if (mr->mp == mp) {
317                         rte_atomic32_inc(&mr->refcnt);
318                         DRV_LOG(DEBUG, "port %u memory region %p refcnt: %d",
319                                 dev->data->port_id, (void *)mr,
320                                 rte_atomic32_read(&mr->refcnt));
321                         return mr;
322                 }
323         }
324         return NULL;
325 }
326
327 /**
328  * Release the memory region object.
329  *
330  * @param  mr
331  *   Pointer to memory region to release.
332  *
333  * @return
334  *   1 while a reference on it exists, 0 when freed.
335  */
336 int
337 mlx5_mr_release(struct mlx5_mr *mr)
338 {
339         assert(mr);
340         DRV_LOG(DEBUG, "memory region %p refcnt: %d", (void *)mr,
341                 rte_atomic32_read(&mr->refcnt));
342         if (rte_atomic32_dec_and_test(&mr->refcnt)) {
343                 claim_zero(mlx5_glue->dereg_mr(mr->mr));
344                 LIST_REMOVE(mr, next);
345                 rte_free(mr);
346                 return 0;
347         }
348         return 1;
349 }
350
351 /**
352  * Verify the flow list is empty
353  *
354  * @param dev
355  *   Pointer to Ethernet device.
356  *
357  * @return
358  *   The number of object not released.
359  */
360 int
361 mlx5_mr_verify(struct rte_eth_dev *dev)
362 {
363         struct priv *priv = dev->data->dev_private;
364         int ret = 0;
365         struct mlx5_mr *mr;
366
367         LIST_FOREACH(mr, &priv->mr, next) {
368                 DRV_LOG(DEBUG, "port %u memory region %p still referenced",
369                         dev->data->port_id, (void *)mr);
370                 ++ret;
371         }
372         return ret;
373 }