f8b215cc290928994c186c9f65665324edeab3a3
[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
10 #include <rte_pci.h>
11 #include <rte_debug.h>
12 #include <rte_atomic.h>
13 #include <rte_log.h>
14 #include <rte_kvargs.h>
15 #include <rte_devargs.h>
16
17 #include "mlx5_autoconf.h"
18 #include "mlx5_glue.h"
19
20 /**
21  * Get device name. Given an ibv_device pointer - return a
22  * pointer to the corresponding device name.
23  *
24  * @param[in] dev
25  *   Pointer to ibv device.
26  *
27  * @return
28  *   Pointer to device name if dev is valid, NULL otherwise.
29  */
30 static inline const char *
31 mlx5_os_get_dev_device_name(void *dev)
32 {
33         if (!dev)
34                 return NULL;
35         return ((struct ibv_device *)dev)->name;
36 }
37
38 /**
39  * Get ibv device name. Given an ibv_context pointer - return a
40  * pointer to the corresponding device name.
41  *
42  * @param[in] ctx
43  *   Pointer to ibv context.
44  *
45  * @return
46  *   Pointer to device name if ctx is valid, NULL otherwise.
47  */
48 static inline const char *
49 mlx5_os_get_ctx_device_name(void *ctx)
50 {
51         if (!ctx)
52                 return NULL;
53         return ((struct ibv_context *)ctx)->device->name;
54 }
55
56 /**
57  * Get ibv device path name. Given an ibv_context pointer - return a
58  * pointer to the corresponding device path name.
59  *
60  * @param[in] ctx
61  *   Pointer to ibv context.
62  *
63  * @return
64  *   Pointer to device path name if ctx is valid, NULL otherwise.
65  */
66
67 static inline const char *
68 mlx5_os_get_ctx_device_path(void *ctx)
69 {
70         if (!ctx)
71                 return NULL;
72
73         return ((struct ibv_context *)ctx)->device->ibdev_path;
74 }
75
76 /**
77  * Get umem id. Given a pointer to umem object of type
78  * 'struct mlx5dv_devx_umem *' - return its id.
79  *
80  * @param[in] umem
81  *    Pointer to umem object.
82  *
83  * @return
84  *    The umem id if umem is valid, 0 otherwise.
85  */
86 static inline uint32_t
87 mlx5_os_get_umem_id(void *umem)
88 {
89         if (!umem)
90                 return 0;
91         return ((struct mlx5dv_devx_umem *)umem)->umem_id;
92 }
93
94 /**
95  * Get fd. Given a pointer to DevX channel object of type
96  * 'struct mlx5dv_devx_event_channel*' - return its fd.
97  *
98  * @param[in] channel
99  *    Pointer to channel object.
100  *
101  * @return
102  *    The fd if channel is valid, 0 otherwise.
103  */
104 static inline int
105 mlx5_os_get_devx_channel_fd(void *channel)
106 {
107         if (!channel)
108                 return 0;
109         return ((struct mlx5dv_devx_event_channel *)channel)->fd;
110 }
111
112 /**
113  * Get mmap offset. Given a pointer to an DevX UAR object of type
114  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
115  *
116  * @param[in] uar
117  *    Pointer to UAR object.
118  *
119  * @return
120  *    The mmap offset if uar is valid, 0 otherwise.
121  */
122 static inline off_t
123 mlx5_os_get_devx_uar_mmap_offset(void *uar)
124 {
125 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
126         if (!uar)
127                 return 0;
128         return ((struct mlx5dv_devx_uar *)uar)->mmap_off;
129 #else
130         RTE_SET_USED(uar);
131         return 0;
132 #endif
133 }
134
135 /**
136  * Get base addr pointer. Given a pointer to an UAR object of type
137  * 'struct mlx5dv_devx_uar *' - return its base address.
138  *
139  * @param[in] uar
140  *    Pointer to an UAR object.
141  *
142  * @return
143  *    The base address if UAR is valid, 0 otherwise.
144  */
145 static inline void *
146 mlx5_os_get_devx_uar_base_addr(void *uar)
147 {
148 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
149         if (!uar)
150                 return NULL;
151         return ((struct mlx5dv_devx_uar *)uar)->base_addr;
152 #else
153         RTE_SET_USED(uar);
154         return NULL;
155 #endif
156 }
157
158 /**
159  * Get reg addr pointer. Given a pointer to an UAR object of type
160  * 'struct mlx5dv_devx_uar *' - return its reg address.
161  *
162  * @param[in] uar
163  *    Pointer to an UAR object.
164  *
165  * @return
166  *    The reg address if UAR is valid, 0 otherwise.
167  */
168 static inline void *
169 mlx5_os_get_devx_uar_reg_addr(void *uar)
170 {
171 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
172         if (!uar)
173                 return NULL;
174         return ((struct mlx5dv_devx_uar *)uar)->reg_addr;
175 #else
176         RTE_SET_USED(uar);
177         return NULL;
178 #endif
179 }
180
181 /**
182  * Get page id. Given a pointer to an UAR object of type
183  * 'struct mlx5dv_devx_uar *' - return its page id.
184  *
185  * @param[in] uar
186  *    Pointer to an UAR object.
187  *
188  * @return
189  *    The page id if UAR is valid, 0 otherwise.
190  */
191 static inline uint32_t
192 mlx5_os_get_devx_uar_page_id(void *uar)
193 {
194 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
195         if (!uar)
196                 return 0;
197         return ((struct mlx5dv_devx_uar *)uar)->page_id;
198 #else
199         RTE_SET_USED(uar);
200         return 0;
201 #endif
202 }
203
204 static inline void *
205 mlx5_os_alloc_pd(void *ctx)
206 {
207         return mlx5_glue->alloc_pd(ctx);
208 }
209
210 static inline int
211 mlx5_os_dealloc_pd(void *pd)
212 {
213         return mlx5_glue->dealloc_pd(pd);
214 }
215
216 static inline void *
217 mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access)
218 {
219         return mlx5_glue->devx_umem_reg(ctx, addr, size, access);
220 }
221
222 static inline int
223 mlx5_os_umem_dereg(void *pumem)
224 {
225         return mlx5_glue->devx_umem_dereg(pumem);
226 }
227 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */