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