xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.h (revision 72206323a5dd3182b13f61b25a64abdddfee595c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #ifndef RTE_PMD_MLX5_COMMON_OS_H_
6 #define RTE_PMD_MLX5_COMMON_OS_H_
7 
8 #include <stdio.h>
9 #include <malloc.h>
10 
11 #include <rte_pci.h>
12 #include <rte_bus_pci.h>
13 #include <rte_debug.h>
14 #include <rte_atomic.h>
15 #include <rte_log.h>
16 #include <rte_kvargs.h>
17 #include <rte_devargs.h>
18 #include <rte_interrupts.h>
19 
20 #include "mlx5_autoconf.h"
21 #include "mlx5_glue.h"
22 #include "mlx5_malloc.h"
23 
24 /**
25  * Get device name. Given an ibv_device pointer - return a
26  * pointer to the corresponding device name.
27  *
28  * @param[in] dev
29  *   Pointer to ibv device.
30  *
31  * @return
32  *   Pointer to device name if dev is valid, NULL otherwise.
33  */
34 static inline const char *
35 mlx5_os_get_dev_device_name(void *dev)
36 {
37 	if (!dev)
38 		return NULL;
39 	return ((struct ibv_device *)dev)->name;
40 }
41 
42 /**
43  * Get ibv device name. Given an ibv_context pointer - return a
44  * pointer to the corresponding device name.
45  *
46  * @param[in] ctx
47  *   Pointer to ibv context.
48  *
49  * @return
50  *   Pointer to device name if ctx is valid, NULL otherwise.
51  */
52 static inline const char *
53 mlx5_os_get_ctx_device_name(void *ctx)
54 {
55 	if (!ctx)
56 		return NULL;
57 	return ((struct ibv_context *)ctx)->device->name;
58 }
59 
60 /**
61  * Get ibv device path name. Given an ibv_context pointer - return a
62  * pointer to the corresponding device path name.
63  *
64  * @param[in] ctx
65  *   Pointer to ibv context.
66  *
67  * @return
68  *   Pointer to device path name if ctx is valid, NULL otherwise.
69  */
70 
71 static inline const char *
72 mlx5_os_get_ctx_device_path(void *ctx)
73 {
74 	if (!ctx)
75 		return NULL;
76 
77 	return ((struct ibv_context *)ctx)->device->ibdev_path;
78 }
79 
80 /**
81  * Get umem id. Given a pointer to umem object of type
82  * 'struct mlx5dv_devx_umem *' - return its id.
83  *
84  * @param[in] umem
85  *    Pointer to umem object.
86  *
87  * @return
88  *    The umem id if umem is valid, 0 otherwise.
89  */
90 static inline uint32_t
91 mlx5_os_get_umem_id(void *umem)
92 {
93 	if (!umem)
94 		return 0;
95 	return ((struct mlx5dv_devx_umem *)umem)->umem_id;
96 }
97 
98 /**
99  * Get fd. Given a pointer to DevX channel object of type
100  * 'struct mlx5dv_devx_event_channel*' - return its fd.
101  *
102  * @param[in] channel
103  *    Pointer to channel object.
104  *
105  * @return
106  *    The fd if channel is valid, 0 otherwise.
107  */
108 static inline int
109 mlx5_os_get_devx_channel_fd(void *channel)
110 {
111 	if (!channel)
112 		return 0;
113 	return ((struct mlx5dv_devx_event_channel *)channel)->fd;
114 }
115 
116 /**
117  * Get mmap offset. Given a pointer to an DevX UAR object of type
118  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
119  *
120  * @param[in] uar
121  *    Pointer to UAR object.
122  *
123  * @return
124  *    The mmap offset if uar is valid, 0 otherwise.
125  */
126 static inline off_t
127 mlx5_os_get_devx_uar_mmap_offset(void *uar)
128 {
129 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
130 	if (!uar)
131 		return 0;
132 	return ((struct mlx5dv_devx_uar *)uar)->mmap_off;
133 #else
134 	RTE_SET_USED(uar);
135 	return 0;
136 #endif
137 }
138 
139 /**
140  * Get base addr pointer. Given a pointer to an UAR object of type
141  * 'struct mlx5dv_devx_uar *' - return its base address.
142  *
143  * @param[in] uar
144  *    Pointer to an UAR object.
145  *
146  * @return
147  *    The base address if UAR is valid, 0 otherwise.
148  */
149 static inline void *
150 mlx5_os_get_devx_uar_base_addr(void *uar)
151 {
152 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
153 	if (!uar)
154 		return NULL;
155 	return ((struct mlx5dv_devx_uar *)uar)->base_addr;
156 #else
157 	RTE_SET_USED(uar);
158 	return NULL;
159 #endif
160 }
161 
162 /**
163  * Get reg addr pointer. Given a pointer to an UAR object of type
164  * 'struct mlx5dv_devx_uar *' - return its reg address.
165  *
166  * @param[in] uar
167  *    Pointer to an UAR object.
168  *
169  * @return
170  *    The reg address if UAR is valid, 0 otherwise.
171  */
172 static inline void *
173 mlx5_os_get_devx_uar_reg_addr(void *uar)
174 {
175 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
176 	if (!uar)
177 		return NULL;
178 	return ((struct mlx5dv_devx_uar *)uar)->reg_addr;
179 #else
180 	RTE_SET_USED(uar);
181 	return NULL;
182 #endif
183 }
184 
185 /**
186  * Get page id. Given a pointer to an UAR object of type
187  * 'struct mlx5dv_devx_uar *' - return its page id.
188  *
189  * @param[in] uar
190  *    Pointer to an UAR object.
191  *
192  * @return
193  *    The page id if UAR is valid, 0 otherwise.
194  */
195 static inline uint32_t
196 mlx5_os_get_devx_uar_page_id(void *uar)
197 {
198 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
199 	if (!uar)
200 		return 0;
201 	return ((struct mlx5dv_devx_uar *)uar)->page_id;
202 #else
203 	RTE_SET_USED(uar);
204 	return 0;
205 #endif
206 }
207 
208 __rte_internal
209 static inline void *
210 mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access)
211 {
212 	return mlx5_glue->devx_umem_reg(ctx, addr, size, access);
213 }
214 
215 __rte_internal
216 static inline int
217 mlx5_os_umem_dereg(void *pumem)
218 {
219 	return mlx5_glue->devx_umem_dereg(pumem);
220 }
221 
222 static inline void *
223 mlx5_os_devx_create_event_channel(void *ctx, int flags)
224 {
225 	return mlx5_glue->devx_create_event_channel(ctx, flags);
226 }
227 
228 static inline void
229 mlx5_os_devx_destroy_event_channel(void *eventc)
230 {
231 	mlx5_glue->devx_destroy_event_channel(eventc);
232 }
233 
234 static inline int
235 mlx5_os_devx_subscribe_devx_event(void *eventc,
236 				  void *obj,
237 				  uint16_t events_sz, uint16_t events_num[],
238 				  uint64_t cookie)
239 {
240 	return mlx5_glue->devx_subscribe_devx_event(eventc, obj, events_sz,
241 						    events_num, cookie);
242 }
243 
244 /**
245  * Memory allocation optionally with alignment.
246  *
247  * @param[in] align
248  *    Alignment size (may be zero)
249  * @param[in] size
250  *    Size in bytes to allocate
251  *
252  * @return
253  *    Valid pointer to allocated memory, NULL in case of failure
254  */
255 static inline void *
256 mlx5_os_malloc(size_t align, size_t size)
257 {
258 	void *buf;
259 
260 	if (posix_memalign(&buf, align, size))
261 		return NULL;
262 	return buf;
263 }
264 
265 /**
266  * This API de-allocates a memory that originally could have been
267  * allocated aligned or non-aligned. In Linux it is a wrapper
268  * around free().
269  *
270  * @param[in] addr
271  *    Pointer to address to free
272  *
273  */
274 static inline void
275 mlx5_os_free(void *addr)
276 {
277 	free(addr);
278 }
279 
280 void
281 mlx5_set_context_attr(struct rte_device *dev, struct ibv_context *ctx);
282 
283 /**
284  * This is used to query system_image_guid as describing in PRM.
285  *
286  * @param dev[in]
287  *  Pointer to a device instance as PCIe id.
288  * @param guid[out]
289  *  Pointer to the buffer to hold device guid.
290  *  Guid is uint64_t and corresponding to 17 bytes string.
291  * @param len[in]
292  *  Guid buffer length, 17 bytes at least.
293  *
294  * @return
295  *  -1 if internal failure.
296  *  0 if OFED doesn't support.
297  *  >0 if success.
298  */
299 __rte_internal
300 int
301 mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len);
302 
303 __rte_internal
304 struct rte_intr_handle *
305 mlx5_os_interrupt_handler_create(int mode, bool set_fd_nonblock, int fd,
306 				 rte_intr_callback_fn cb, void *cb_arg);
307 
308 __rte_internal
309 void
310 mlx5_os_interrupt_handler_destroy(struct rte_intr_handle *intr_handle,
311 				  rte_intr_callback_fn cb, void *cb_arg);
312 
313 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
314