2abdb2c3ae6e91126fe6e65c9355b346044428e3
[dpdk.git] / drivers / common / mlx5 / windows / 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 "mlx5_autoconf.h"
11 #include "mlx5_glue.h"
12 #include "mlx5_malloc.h"
13
14 /**
15  * This API allocates aligned or non-aligned memory.  The free can be on either
16  * aligned or nonaligned memory.  To be protected - even though there may be no
17  * alignment - in Windows this API will unconditioanlly call _aligned_malloc()
18  * with at least a minimal alignment size.
19  *
20  * @param[in] align
21  *    The alignment value, which must be an integer power of 2 (or 0 for
22  *    non-alignment)
23  * @param[in] size
24  *    Size in bytes to allocate
25  *
26  * @return
27  *    Valid pointer to allocated memory, NULL in case of failure
28  */
29 static inline void *
30 mlx5_os_malloc(size_t align, size_t size)
31 {
32         if (align < MLX5_MALLOC_ALIGNMENT)
33                 align = MLX5_MALLOC_ALIGNMENT;
34         return _aligned_malloc(size, align);
35 }
36
37 /**
38  * This API de-allocates a memory that originally could have been allocated
39  * aligned or non-aligned. In Windows since the allocation was with
40  * _aligned_malloc() - it is safe to always call _aligned_free().
41  *
42  * @param[in] addr
43  *    Pointer to address to free
44  *
45  */
46 static inline void
47 mlx5_os_free(void *addr)
48 {
49         _aligned_free(addr);
50 }
51
52 /**
53  * Get fd. Given a pointer to DevX channel object of type
54  * 'struct mlx5dv_devx_event_channel*' - return its fd.
55  * Under Windows it is a stub.
56  *
57  * @param[in] channel
58  *    Pointer to channel object.
59  *
60  * @return
61  *    0
62  */
63 static inline int
64 mlx5_os_get_devx_channel_fd(void *channel)
65 {
66         if (!channel)
67                 return 0;
68         return 0;
69 }
70
71 /**
72  * Get device name. Given a device pointer - return a
73  * pointer to the corresponding device name.
74  *
75  * @param[in] dev
76  *   Pointer to device.
77  *
78  * @return
79  *   Pointer to device name if dev is valid, NULL otherwise.
80  */
81 static inline const char *
82 mlx5_os_get_dev_device_name(void *dev)
83 {
84         if (!dev)
85                 return NULL;
86         return ((struct devx_device *)dev)->name;
87 }
88
89 /**
90  * Get device name. Given a context pointer - return a
91  * pointer to the corresponding device name.
92  *
93  * @param[in] ctx
94  *   Pointer to context.
95  *
96  * @return
97  *   Pointer to device name if ctx is valid, NULL otherwise.
98  */
99 static inline const char *
100 mlx5_os_get_ctx_device_name(void *ctx)
101 {
102         if (!ctx)
103                 return NULL;
104         return ((mlx5_context_st *)ctx)->mlx5_dev.name;
105 }
106
107 /**
108  * Get a device path name. Given acontext pointer - return a
109  * pointer to the corresponding device path name.
110  *
111  * @param[in] ctx
112  *   Pointer to context.
113  *
114  * @return
115  *   Pointer to device path name if ctx is valid, NULL otherwise.
116  */
117
118 static inline const char *
119 mlx5_os_get_ctx_device_path(void *ctx)
120 {
121         if (!ctx)
122                 return NULL;
123         return ((mlx5_context_st *)ctx)->mlx5_dev.dev_pnp_id;
124 }
125
126 /**
127  * Get umem id. Given a pointer to umem object of type return its id.
128  *
129  * @param[in] umem
130  *    Pointer to umem object.
131  *
132  * @return
133  *    The umem id if umem is valid, 0 otherwise.
134  */
135 static inline uint32_t
136 mlx5_os_get_umem_id(void *umem)
137 {
138         if (!umem)
139                 return 0;
140         return ((struct mlx5_devx_umem *)umem)->umem_id;
141 }
142 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */