9700884b4aa9499dda04e3f7a86f2b2076ac2e57
[dpdk.git] / drivers / net / mlx4 / mlx4_mr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @file
36  * Memory management functions for mlx4 driver.
37  */
38
39 #include <errno.h>
40 #include <stddef.h>
41 #include <stdint.h>
42 #include <string.h>
43
44 /* Verbs headers do not support -pedantic. */
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic ignored "-Wpedantic"
47 #endif
48 #include <infiniband/verbs.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_common.h>
54 #include <rte_errno.h>
55 #include <rte_memory.h>
56 #include <rte_mempool.h>
57
58 #include "mlx4_utils.h"
59
60 struct mlx4_check_mempool_data {
61         int ret;
62         char *start;
63         char *end;
64 };
65
66 /**
67  * Called by mlx4_check_mempool() when iterating the memory chunks.
68  *
69  * @param[in] mp
70  *   Pointer to memory pool (unused).
71  * @param[in, out] data
72  *   Pointer to shared buffer with mlx4_check_mempool().
73  * @param[in] memhdr
74  *   Pointer to mempool chunk header.
75  * @param mem_idx
76  *   Mempool element index (unused).
77  */
78 static void
79 mlx4_check_mempool_cb(struct rte_mempool *mp, void *opaque,
80                       struct rte_mempool_memhdr *memhdr,
81                       unsigned int mem_idx)
82 {
83         struct mlx4_check_mempool_data *data = opaque;
84
85         (void)mp;
86         (void)mem_idx;
87         /* It already failed, skip the next chunks. */
88         if (data->ret != 0)
89                 return;
90         /* It is the first chunk. */
91         if (data->start == NULL && data->end == NULL) {
92                 data->start = memhdr->addr;
93                 data->end = data->start + memhdr->len;
94                 return;
95         }
96         if (data->end == memhdr->addr) {
97                 data->end += memhdr->len;
98                 return;
99         }
100         if (data->start == (char *)memhdr->addr + memhdr->len) {
101                 data->start -= memhdr->len;
102                 return;
103         }
104         /* Error, mempool is not virtually contiguous. */
105         data->ret = -1;
106 }
107
108 /**
109  * Check if a mempool can be used: it must be virtually contiguous.
110  *
111  * @param[in] mp
112  *   Pointer to memory pool.
113  * @param[out] start
114  *   Pointer to the start address of the mempool virtual memory area.
115  * @param[out] end
116  *   Pointer to the end address of the mempool virtual memory area.
117  *
118  * @return
119  *   0 on success (mempool is virtually contiguous), -1 on error.
120  */
121 static int
122 mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start, uintptr_t *end)
123 {
124         struct mlx4_check_mempool_data data;
125
126         memset(&data, 0, sizeof(data));
127         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
128         *start = (uintptr_t)data.start;
129         *end = (uintptr_t)data.end;
130         return data.ret;
131 }
132
133 /**
134  * Register mempool as a memory region.
135  *
136  * @param pd
137  *   Pointer to protection domain.
138  * @param mp
139  *   Pointer to memory pool.
140  *
141  * @return
142  *   Memory region pointer, NULL in case of error and rte_errno is set.
143  */
144 struct ibv_mr *
145 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
146 {
147         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
148         uintptr_t start;
149         uintptr_t end;
150         unsigned int i;
151         struct ibv_mr *mr;
152
153         if (mlx4_check_mempool(mp, &start, &end) != 0) {
154                 rte_errno = EINVAL;
155                 ERROR("mempool %p: not virtually contiguous",
156                         (void *)mp);
157                 return NULL;
158         }
159         DEBUG("mempool %p area start=%p end=%p size=%zu",
160               (void *)mp, (void *)start, (void *)end,
161               (size_t)(end - start));
162         /* Round start and end to page boundary if found in memory segments. */
163         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
164                 uintptr_t addr = (uintptr_t)ms[i].addr;
165                 size_t len = ms[i].len;
166                 unsigned int align = ms[i].hugepage_sz;
167
168                 if ((start > addr) && (start < addr + len))
169                         start = RTE_ALIGN_FLOOR(start, align);
170                 if ((end > addr) && (end < addr + len))
171                         end = RTE_ALIGN_CEIL(end, align);
172         }
173         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
174               (void *)mp, (void *)start, (void *)end,
175               (size_t)(end - start));
176         mr = ibv_reg_mr(pd,
177                         (void *)start,
178                         end - start,
179                         IBV_ACCESS_LOCAL_WRITE);
180         if (!mr)
181                 rte_errno = errno ? errno : EINVAL;
182         return mr;
183 }