1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3 *
4 * Copyright(c) 2007 - 2017 Realtek Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 *****************************************************************************/
16 #ifndef __OSDEP_SERVICE_H_
17 #define __OSDEP_SERVICE_H_
18
19
20 #define _FAIL 0
21 #define _SUCCESS 1
22 #define RTW_RX_HANDLED 2
23 #define RTW_RFRAME_UNAVAIL 3
24 #define RTW_RFRAME_PKT_UNAVAIL 4
25 #define RTW_RBUF_UNAVAIL 5
26 #define RTW_RBUF_PKT_UNAVAIL 6
27 #define RTW_SDIO_READ_PORT_FAIL 7
28 #define RTW_ALREADY 8
29 #define RTW_RA_RESOLVING 9
30 #define RTW_BMC_NO_NEED 10
31 #define RTW_XBUF_UNAVAIL 11
32 #define RTW_TX_BALANCE 12
33 #define RTW_TX_WAIT_MORE_FRAME 13
34 #define RTW_QUEUE_MGMT 14
35
36 /* #define RTW_STATUS_TIMEDOUT -110 */
37
38 #undef _TRUE
39 #define _TRUE 1
40
41 #undef _FALSE
42 #define _FALSE 0
43
44
45 #ifdef PLATFORM_FREEBSD
46 #include <osdep_service_bsd.h>
47 #endif
48
49 #ifdef PLATFORM_LINUX
50 #include <linux/version.h>
51 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
52 #include <linux/sched/signal.h>
53 #include <linux/sched/types.h>
54 #endif
55 #include <osdep_service_linux.h>
56 #include <drv_types_linux.h>
57 #endif
58
59 #ifdef PLATFORM_OS_XP
60 #include <osdep_service_xp.h>
61 #include <drv_types_xp.h>
62 #endif
63
64 #ifdef PLATFORM_OS_CE
65 #include <osdep_service_ce.h>
66 #include <drv_types_ce.h>
67 #endif
68
69 /* #include <rtw_byteorder.h> */
70
71 #ifndef BIT
72 #define BIT(x) (1 << (x))
73 #endif
74 #ifndef BIT_ULL
75 #define BIT_ULL(x) (1ULL << (x))
76 #endif
77
78 #define CHECK_BIT(a, b) (!!((a) & (b)))
79
80 #define BIT0 0x00000001
81 #define BIT1 0x00000002
82 #define BIT2 0x00000004
83 #define BIT3 0x00000008
84 #define BIT4 0x00000010
85 #define BIT5 0x00000020
86 #define BIT6 0x00000040
87 #define BIT7 0x00000080
88 #define BIT8 0x00000100
89 #define BIT9 0x00000200
90 #define BIT10 0x00000400
91 #define BIT11 0x00000800
92 #define BIT12 0x00001000
93 #define BIT13 0x00002000
94 #define BIT14 0x00004000
95 #define BIT15 0x00008000
96 #define BIT16 0x00010000
97 #define BIT17 0x00020000
98 #define BIT18 0x00040000
99 #define BIT19 0x00080000
100 #define BIT20 0x00100000
101 #define BIT21 0x00200000
102 #define BIT22 0x00400000
103 #define BIT23 0x00800000
104 #define BIT24 0x01000000
105 #define BIT25 0x02000000
106 #define BIT26 0x04000000
107 #define BIT27 0x08000000
108 #define BIT28 0x10000000
109 #define BIT29 0x20000000
110 #define BIT30 0x40000000
111 #define BIT31 0x80000000
112 #define BIT32 0x0100000000
113 #define BIT33 0x0200000000
114 #define BIT34 0x0400000000
115 #define BIT35 0x0800000000
116 #define BIT36 0x1000000000
117
118 #ifndef GENMASK
119 #define GENMASK(h, l) \
120 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
121 #endif
122
123 extern int RTW_STATUS_CODE(int error_code);
124
125 #ifndef RTK_DMP_PLATFORM
126 #define CONFIG_USE_VMALLOC
127 #endif
128
129 /* flags used for rtw_mstat_update() */
130 enum mstat_f {
131 /* type: 0x00ff */
132 MSTAT_TYPE_VIR = 0x00,
133 MSTAT_TYPE_PHY = 0x01,
134 MSTAT_TYPE_SKB = 0x02,
135 MSTAT_TYPE_USB = 0x03,
136 MSTAT_TYPE_MAX = 0x04,
137
138 /* func: 0xff00 */
139 MSTAT_FUNC_UNSPECIFIED = 0x00 << 8,
140 MSTAT_FUNC_IO = 0x01 << 8,
141 MSTAT_FUNC_TX_IO = 0x02 << 8,
142 MSTAT_FUNC_RX_IO = 0x03 << 8,
143 MSTAT_FUNC_TX = 0x04 << 8,
144 MSTAT_FUNC_RX = 0x05 << 8,
145 MSTAT_FUNC_CFG_VENDOR = 0x06 << 8,
146 MSTAT_FUNC_MAX = 0x07 << 8,
147 };
148
149 #define mstat_tf_idx(flags) ((flags) & 0xff)
150 #define mstat_ff_idx(flags) (((flags) & 0xff00) >> 8)
151
152 typedef enum mstat_status {
153 MSTAT_ALLOC_SUCCESS = 0,
154 MSTAT_ALLOC_FAIL,
155 MSTAT_FREE
156 } MSTAT_STATUS;
157
158 #ifdef DBG_MEM_ALLOC
159 void rtw_mstat_update(const enum mstat_f flags, const MSTAT_STATUS status, u32 sz);
160 void rtw_mstat_dump(void *sel);
161 bool match_mstat_sniff_rules(const enum mstat_f flags, const size_t size);
162 void *dbg_rtw_vmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
163 void *dbg_rtw_zvmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
164 void dbg_rtw_vmfree(void *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
165 void *dbg_rtw_malloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
166 void *dbg_rtw_zmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
167 void dbg_rtw_mfree(void *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
168
169 struct sk_buff *dbg_rtw_skb_alloc(unsigned int size, const enum mstat_f flags, const char *func, const int line);
170 void dbg_rtw_skb_free(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
171 struct sk_buff *dbg_rtw_skb_copy(const struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
172 struct sk_buff *dbg_rtw_skb_clone(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
173 int dbg_rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
174 #ifdef CONFIG_RTW_NAPI
175 int dbg_rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
176 #ifdef CONFIG_RTW_GRO
177 gro_result_t dbg_rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
178 #endif
179 #endif /* CONFIG_RTW_NAPI */
180 void dbg_rtw_skb_queue_purge(struct sk_buff_head *list, enum mstat_f flags, const char *func, int line);
181 #ifdef CONFIG_USB_HCI
182 void *dbg_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma, const enum mstat_f flags, const char *func, const int line);
183 void dbg_rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma, const enum mstat_f flags, const char *func, const int line);
184 #endif /* CONFIG_USB_HCI */
185
186 #ifdef CONFIG_USE_VMALLOC
187 #define rtw_vmalloc(sz) dbg_rtw_vmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
188 #define rtw_zvmalloc(sz) dbg_rtw_zvmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
189 #define rtw_vmfree(pbuf, sz) dbg_rtw_vmfree((pbuf), (sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
190 #define rtw_vmalloc_f(sz, mstat_f) dbg_rtw_vmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
191 #define rtw_zvmalloc_f(sz, mstat_f) dbg_rtw_zvmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
192 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_vmfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
193 #else /* CONFIG_USE_VMALLOC */
194 #define rtw_vmalloc(sz) dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
195 #define rtw_zvmalloc(sz) dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
196 #define rtw_vmfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
197 #define rtw_vmalloc_f(sz, mstat_f) dbg_rtw_malloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
198 #define rtw_zvmalloc_f(sz, mstat_f) dbg_rtw_zmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
199 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_mfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
200 #endif /* CONFIG_USE_VMALLOC */
201 #define rtw_malloc(sz) dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
202 #define rtw_zmalloc(sz) dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
203 #define rtw_mfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
204 #define rtw_malloc_f(sz, mstat_f) dbg_rtw_malloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
205 #define rtw_zmalloc_f(sz, mstat_f) dbg_rtw_zmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
206 #define rtw_mfree_f(pbuf, sz, mstat_f) dbg_rtw_mfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
207
208 #define rtw_skb_alloc(size) dbg_rtw_skb_alloc((size), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
209 #define rtw_skb_free(skb) dbg_rtw_skb_free((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
210 #define rtw_skb_alloc_f(size, mstat_f) dbg_rtw_skb_alloc((size), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
211 #define rtw_skb_free_f(skb, mstat_f) dbg_rtw_skb_free((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
212 #define rtw_skb_copy(skb) dbg_rtw_skb_copy((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
213 #define rtw_skb_clone(skb) dbg_rtw_skb_clone((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
214 #define rtw_skb_copy_f(skb, mstat_f) dbg_rtw_skb_copy((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
215 #define rtw_skb_clone_f(skb, mstat_f) dbg_rtw_skb_clone((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
216 #define rtw_netif_rx(ndev, skb) dbg_rtw_netif_rx(ndev, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
217 #ifdef CONFIG_RTW_NAPI
218 #define rtw_netif_receive_skb(ndev, skb) dbg_rtw_netif_receive_skb(ndev, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
219 #ifdef CONFIG_RTW_GRO
220 #define rtw_napi_gro_receive(napi, skb) dbg_rtw_napi_gro_receive(napi, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
221 #endif
222 #endif /* CONFIG_RTW_NAPI */
223 #define rtw_skb_queue_purge(sk_buff_head) dbg_rtw_skb_queue_purge(sk_buff_head, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
224 #ifdef CONFIG_USB_HCI
225 #define rtw_usb_buffer_alloc(dev, size, dma) dbg_rtw_usb_buffer_alloc((dev), (size), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
226 #define rtw_usb_buffer_free(dev, size, addr, dma) dbg_rtw_usb_buffer_free((dev), (size), (addr), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
227 #define rtw_usb_buffer_alloc_f(dev, size, dma, mstat_f) dbg_rtw_usb_buffer_alloc((dev), (size), (dma), ((mstat_f) & 0xff00) | MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
228 #define rtw_usb_buffer_free_f(dev, size, addr, dma, mstat_f) dbg_rtw_usb_buffer_free((dev), (size), (addr), (dma), ((mstat_f) & 0xff00) | MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
229 #endif /* CONFIG_USB_HCI */
230
231 #else /* DBG_MEM_ALLOC */
232 #define rtw_mstat_update(flag, status, sz) do {} while (0)
233 #define rtw_mstat_dump(sel) do {} while (0)
234 #define match_mstat_sniff_rules(flags, size) _FALSE
235 void *_rtw_vmalloc(u32 sz);
236 void *_rtw_zvmalloc(u32 sz);
237 void _rtw_vmfree(void *pbuf, u32 sz);
238 void *_rtw_zmalloc(u32 sz);
239 void *_rtw_malloc(u32 sz);
240 void _rtw_mfree(void *pbuf, u32 sz);
241
242 struct sk_buff *_rtw_skb_alloc(u32 sz);
243 void _rtw_skb_free(struct sk_buff *skb);
244 struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb);
245 struct sk_buff *_rtw_skb_clone(struct sk_buff *skb);
246 int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb);
247 #ifdef CONFIG_RTW_NAPI
248 int _rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb);
249 #ifdef CONFIG_RTW_GRO
250 gro_result_t _rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb);
251 #endif
252 #endif /* CONFIG_RTW_NAPI */
253 void _rtw_skb_queue_purge(struct sk_buff_head *list);
254
255 #ifdef CONFIG_USB_HCI
256 void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma);
257 void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma);
258 #endif /* CONFIG_USB_HCI */
259
260 #ifdef CONFIG_USE_VMALLOC
261 #define rtw_vmalloc(sz) _rtw_vmalloc((sz))
262 #define rtw_zvmalloc(sz) _rtw_zvmalloc((sz))
263 #define rtw_vmfree(pbuf, sz) _rtw_vmfree((pbuf), (sz))
264 #define rtw_vmalloc_f(sz, mstat_f) _rtw_vmalloc((sz))
265 #define rtw_zvmalloc_f(sz, mstat_f) _rtw_zvmalloc((sz))
266 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_vmfree((pbuf), (sz))
267 #else /* CONFIG_USE_VMALLOC */
268 #define rtw_vmalloc(sz) _rtw_malloc((sz))
269 #define rtw_zvmalloc(sz) _rtw_zmalloc((sz))
270 #define rtw_vmfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
271 #define rtw_vmalloc_f(sz, mstat_f) _rtw_malloc((sz))
272 #define rtw_zvmalloc_f(sz, mstat_f) _rtw_zmalloc((sz))
273 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_mfree((pbuf), (sz))
274 #endif /* CONFIG_USE_VMALLOC */
275 #define rtw_malloc(sz) _rtw_malloc((sz))
276 #define rtw_zmalloc(sz) _rtw_zmalloc((sz))
277 #define rtw_mfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
278 #define rtw_malloc_f(sz, mstat_f) _rtw_malloc((sz))
279 #define rtw_zmalloc_f(sz, mstat_f) _rtw_zmalloc((sz))
280 #define rtw_mfree_f(pbuf, sz, mstat_f) _rtw_mfree((pbuf), (sz))
281
282 #define rtw_skb_alloc(size) _rtw_skb_alloc((size))
283 #define rtw_skb_free(skb) _rtw_skb_free((skb))
284 #define rtw_skb_alloc_f(size, mstat_f) _rtw_skb_alloc((size))
285 #define rtw_skb_free_f(skb, mstat_f) _rtw_skb_free((skb))
286 #define rtw_skb_copy(skb) _rtw_skb_copy((skb))
287 #define rtw_skb_clone(skb) _rtw_skb_clone((skb))
288 #define rtw_skb_copy_f(skb, mstat_f) _rtw_skb_copy((skb))
289 #define rtw_skb_clone_f(skb, mstat_f) _rtw_skb_clone((skb))
290 #define rtw_netif_rx(ndev, skb) _rtw_netif_rx(ndev, skb)
291 #ifdef CONFIG_RTW_NAPI
292 #define rtw_netif_receive_skb(ndev, skb) _rtw_netif_receive_skb(ndev, skb)
293 #ifdef CONFIG_RTW_GRO
294 #define rtw_napi_gro_receive(napi, skb) _rtw_napi_gro_receive(napi, skb)
295 #endif
296 #endif /* CONFIG_RTW_NAPI */
297 #define rtw_skb_queue_purge(sk_buff_head) _rtw_skb_queue_purge(sk_buff_head)
298 #ifdef CONFIG_USB_HCI
299 #define rtw_usb_buffer_alloc(dev, size, dma) _rtw_usb_buffer_alloc((dev), (size), (dma))
300 #define rtw_usb_buffer_free(dev, size, addr, dma) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
301 #define rtw_usb_buffer_alloc_f(dev, size, dma, mstat_f) _rtw_usb_buffer_alloc((dev), (size), (dma))
302 #define rtw_usb_buffer_free_f(dev, size, addr, dma, mstat_f) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
303 #endif /* CONFIG_USB_HCI */
304 #endif /* DBG_MEM_ALLOC */
305
306 extern void *rtw_malloc2d(int h, int w, size_t size);
307 extern void rtw_mfree2d(void *pbuf, int h, int w, int size);
308
309 void rtw_os_pkt_free(_pkt *pkt);
310 _pkt *rtw_os_pkt_copy(_pkt *pkt);
311 void *rtw_os_pkt_data(_pkt *pkt);
312 u32 rtw_os_pkt_len(_pkt *pkt);
313
314 extern void _rtw_memcpy(void *dec, const void *sour, u32 sz);
315 extern void _rtw_memmove(void *dst, const void *src, u32 sz);
316 extern int _rtw_memcmp(const void *dst, const void *src, u32 sz);
317 extern int _rtw_memcmp2(const void *dst, const void *src, u32 sz);
318 extern void _rtw_memset(void *pbuf, int c, u32 sz);
319
320 extern void _rtw_init_listhead(_list *list);
321 extern u32 rtw_is_list_empty(_list *phead);
322 extern void rtw_list_insert_head(_list *plist, _list *phead);
323 extern void rtw_list_insert_tail(_list *plist, _list *phead);
324 void rtw_list_splice(_list *list, _list *head);
325 void rtw_list_splice_init(_list *list, _list *head);
326 void rtw_list_splice_tail(_list *list, _list *head);
327
328 #ifndef PLATFORM_FREEBSD
329 extern void rtw_list_delete(_list *plist);
330 #endif /* PLATFORM_FREEBSD */
331
332 void rtw_hlist_head_init(rtw_hlist_head *h);
333 void rtw_hlist_add_head(rtw_hlist_node *n, rtw_hlist_head *h);
334 void rtw_hlist_del(rtw_hlist_node *n);
335 void rtw_hlist_add_head_rcu(rtw_hlist_node *n, rtw_hlist_head *h);
336 void rtw_hlist_del_rcu(rtw_hlist_node *n);
337
338 extern void _rtw_init_sema(_sema *sema, int init_val);
339 extern void _rtw_free_sema(_sema *sema);
340 extern void _rtw_up_sema(_sema *sema);
341 extern u32 _rtw_down_sema(_sema *sema);
342 extern void _rtw_mutex_init(_mutex *pmutex);
343 extern void _rtw_mutex_free(_mutex *pmutex);
344 #ifndef PLATFORM_FREEBSD
345 extern void _rtw_spinlock_init(_lock *plock);
346 #endif /* PLATFORM_FREEBSD */
347 extern void _rtw_spinlock_free(_lock *plock);
348 extern void _rtw_spinlock(_lock *plock);
349 extern void _rtw_spinunlock(_lock *plock);
350 extern void _rtw_spinlock_ex(_lock *plock);
351 extern void _rtw_spinunlock_ex(_lock *plock);
352
353 extern void _rtw_init_queue(_queue *pqueue);
354 extern void _rtw_deinit_queue(_queue *pqueue);
355 extern u32 _rtw_queue_empty(_queue *pqueue);
356 extern u32 rtw_end_of_queue_search(_list *queue, _list *pelement);
357
358 extern systime _rtw_get_current_time(void);
359 extern u32 _rtw_systime_to_ms(systime stime);
360 extern systime _rtw_ms_to_systime(u32 ms);
361 extern systime _rtw_us_to_systime(u32 us);
362 extern s32 _rtw_get_passing_time_ms(systime start);
363 extern s32 _rtw_get_remaining_time_ms(systime end);
364 extern s32 _rtw_get_time_interval_ms(systime start, systime end);
365 extern bool _rtw_time_after(systime a, systime b);
366
367 #ifdef DBG_SYSTIME
368 #define rtw_get_current_time() ({systime __stime = _rtw_get_current_time(); __stime;})
369 #define rtw_systime_to_ms(stime) ({u32 __ms = _rtw_systime_to_ms(stime); typecheck(systime, stime); __ms;})
370 #define rtw_ms_to_systime(ms) ({systime __stime = _rtw_ms_to_systime(ms); __stime;})
371 #define rtw_us_to_systime(us) ({systime __stime = _rtw_us_to_systime(us); __stime;})
372 #define rtw_get_passing_time_ms(start) ({u32 __ms = _rtw_get_passing_time_ms(start); typecheck(systime, start); __ms;})
373 #define rtw_get_remaining_time_ms(end) ({u32 __ms = _rtw_get_remaining_time_ms(end); typecheck(systime, end); __ms;})
374 #define rtw_get_time_interval_ms(start, end) ({u32 __ms = _rtw_get_time_interval_ms(start, end); typecheck(systime, start); typecheck(systime, end); __ms;})
375 #define rtw_time_after(a,b) ({bool __r = _rtw_time_after(a,b); typecheck(systime, a); typecheck(systime, b); __r;})
376 #define rtw_time_before(a,b) ({bool __r = _rtw_time_after(b, a); typecheck(systime, a); typecheck(systime, b); __r;})
377 #else
378 #define rtw_get_current_time() _rtw_get_current_time()
379 #define rtw_systime_to_ms(stime) _rtw_systime_to_ms(stime)
380 #define rtw_ms_to_systime(ms) _rtw_ms_to_systime(ms)
381 #define rtw_us_to_systime(us) _rtw_us_to_systime(us)
382 #define rtw_get_passing_time_ms(start) _rtw_get_passing_time_ms(start)
383 #define rtw_get_remaining_time_ms(end) _rtw_get_remaining_time_ms(end)
384 #define rtw_get_time_interval_ms(start, end) _rtw_get_time_interval_ms(start, end)
385 #define rtw_time_after(a,b) _rtw_time_after(a,b)
386 #define rtw_time_before(a,b) _rtw_time_after(b,a)
387 #endif
388
389 sysptime rtw_sptime_get(void);
390 sysptime rtw_sptime_set(s64 secs, const u32 nsecs);
391 sysptime rtw_sptime_zero(void);
392
393 int rtw_sptime_cmp(const sysptime cmp1, const sysptime cmp2);
394 bool rtw_sptime_eql(const sysptime cmp1, const sysptime cmp2);
395 bool rtw_sptime_is_zero(const sysptime sptime);
396 sysptime rtw_sptime_sub(const sysptime lhs, const sysptime rhs);
397 sysptime rtw_sptime_add(const sysptime lhs, const sysptime rhs);
398
399 s64 rtw_sptime_to_ms(const sysptime sptime);
400 sysptime rtw_ms_to_sptime(u64 ms);
401 s64 rtw_sptime_to_us(const sysptime sptime);
402 sysptime rtw_us_to_sptime(u64 us);
403 s64 rtw_sptime_to_ns(const sysptime sptime);
404 sysptime rtw_ns_to_sptime(u64 ns);
405
406 s64 rtw_sptime_diff_ms(const sysptime start, const sysptime end);
407 s64 rtw_sptime_pass_ms(const sysptime start);
408 s64 rtw_sptime_diff_us(const sysptime start, const sysptime end);
409 s64 rtw_sptime_pass_us(const sysptime start);
410 s64 rtw_sptime_diff_ns(const sysptime start, const sysptime end);
411 s64 rtw_sptime_pass_ns(const sysptime start);
412
413 extern void rtw_sleep_schedulable(int ms);
414
415 extern void rtw_msleep_os(int ms);
416 extern void rtw_usleep_os(int us);
417
418 extern u32 rtw_atoi(u8 *s);
419
420 #ifdef DBG_DELAY_OS
421 #define rtw_mdelay_os(ms) _rtw_mdelay_os((ms), __FUNCTION__, __LINE__)
422 #define rtw_udelay_os(ms) _rtw_udelay_os((ms), __FUNCTION__, __LINE__)
423 extern void _rtw_mdelay_os(int ms, const char *func, const int line);
424 extern void _rtw_udelay_os(int us, const char *func, const int line);
425 #else
426 extern void rtw_mdelay_os(int ms);
427 extern void rtw_udelay_os(int us);
428 #endif
429
430 extern void rtw_yield_os(void);
431
432 enum rtw_pwait_type {
433 RTW_PWAIT_TYPE_MSLEEP,
434 RTW_PWAIT_TYPE_USLEEP,
435 RTW_PWAIT_TYPE_YIELD,
436 RTW_PWAIT_TYPE_MDELAY,
437 RTW_PWAIT_TYPE_UDELAY,
438
439 RTW_PWAIT_TYPE_NUM,
440 };
441
442 #define RTW_PWAIT_TYPE_VALID(type) (type < RTW_PWAIT_TYPE_NUM)
443
444 struct rtw_pwait_conf {
445 enum rtw_pwait_type type;
446 s32 wait_time;
447 s32 wait_cnt_lmt;
448 };
449
450 struct rtw_pwait_ctx {
451 struct rtw_pwait_conf conf;
452 s32 wait_cnt;
453 void (*wait_hdl)(int us);
454 };
455
456 extern const char *_rtw_pwait_type_str[];
457 #define rtw_pwait_type_str(type) (RTW_PWAIT_TYPE_VALID(type) ? _rtw_pwait_type_str[type] : _rtw_pwait_type_str[RTW_PWAIT_TYPE_NUM])
458
459 #define rtw_pwctx_reset(pwctx) (pwctx)->wait_cnt = 0
460 #define rtw_pwctx_wait(pwctx) do { (pwctx)->wait_hdl((pwctx)->conf.wait_time); (pwctx)->wait_cnt++; } while(0)
461 #define rtw_pwctx_waited(pwctx) ((pwctx)->wait_cnt)
462 #define rtw_pwctx_exceed(pwctx) ((pwctx)->conf.wait_cnt_lmt >= 0 && (pwctx)->wait_cnt >= (pwctx)->conf.wait_cnt_lmt)
463
464 int rtw_pwctx_config(struct rtw_pwait_ctx *pwctx, enum rtw_pwait_type type, s32 time, s32 cnt_lmt);
465
466 extern void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc, void *ctx);
467
468
_cancel_timer_ex(_timer * ptimer)469 __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
470 {
471 u8 bcancelled;
472
473 _cancel_timer(ptimer, &bcancelled);
474
475 return bcancelled;
476 }
477
thread_enter(char * name)478 static __inline void thread_enter(char *name)
479 {
480 #ifdef PLATFORM_LINUX
481 allow_signal(SIGTERM);
482 #endif
483 #ifdef PLATFORM_FREEBSD
484 printf("%s", "RTKTHREAD_enter");
485 #endif
486 }
487 void thread_exit(_completion *comp);
488 void _rtw_init_completion(_completion *comp);
489 void _rtw_wait_for_comp_timeout(_completion *comp);
490 void _rtw_wait_for_comp(_completion *comp);
491
rtw_thread_stop(_thread_hdl_ th)492 static inline bool rtw_thread_stop(_thread_hdl_ th)
493 {
494 #ifdef PLATFORM_LINUX
495 return kthread_stop(th);
496 #endif
497 }
rtw_thread_wait_stop(void)498 static inline void rtw_thread_wait_stop(void)
499 {
500 #ifdef PLATFORM_LINUX
501 #if 0
502 while (!kthread_should_stop())
503 rtw_msleep_os(10);
504 #else
505 set_current_state(TASK_INTERRUPTIBLE);
506 while (!kthread_should_stop()) {
507 schedule();
508 set_current_state(TASK_INTERRUPTIBLE);
509 }
510 __set_current_state(TASK_RUNNING);
511 #endif
512 #endif
513 }
514
flush_signals_thread(void)515 __inline static void flush_signals_thread(void)
516 {
517 #ifdef PLATFORM_LINUX
518 if (signal_pending(current))
519 flush_signals(current);
520 #endif
521 }
522
res_to_status(sint res)523 __inline static _OS_STATUS res_to_status(sint res)
524 {
525
526 #if defined(PLATFORM_LINUX) || defined (PLATFORM_MPIXEL) || defined (PLATFORM_FREEBSD)
527 return res;
528 #endif
529
530 #ifdef PLATFORM_WINDOWS
531
532 if (res == _SUCCESS)
533 return NDIS_STATUS_SUCCESS;
534 else
535 return NDIS_STATUS_FAILURE;
536
537 #endif
538
539 }
540
rtw_dump_stack(void)541 __inline static void rtw_dump_stack(void)
542 {
543 #ifdef PLATFORM_LINUX
544 dump_stack();
545 #endif
546 }
547
548 #ifdef PLATFORM_LINUX
549 #define rtw_warn_on(condition) WARN_ON(condition)
550 #else
551 #define rtw_warn_on(condition) do {} while (0)
552 #endif
553
rtw_bug_check(void * parg1,void * parg2,void * parg3,void * parg4)554 __inline static int rtw_bug_check(void *parg1, void *parg2, void *parg3, void *parg4)
555 {
556 int ret = _TRUE;
557
558 #ifdef PLATFORM_WINDOWS
559 if (((uint)parg1) <= 0x7fffffff ||
560 ((uint)parg2) <= 0x7fffffff ||
561 ((uint)parg3) <= 0x7fffffff ||
562 ((uint)parg4) <= 0x7fffffff) {
563 ret = _FALSE;
564 KeBugCheckEx(0x87110000, (ULONG_PTR)parg1, (ULONG_PTR)parg2, (ULONG_PTR)parg3, (ULONG_PTR)parg4);
565 }
566 #endif
567
568 return ret;
569
570 }
571 #ifdef PLATFORM_LINUX
572 #define RTW_DIV_ROUND_UP(n, d) DIV_ROUND_UP(n, d)
573 #else /* !PLATFORM_LINUX */
574 #define RTW_DIV_ROUND_UP(n, d) (((n) + (d - 1)) / d)
575 #endif /* !PLATFORM_LINUX */
576
577 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
578 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0 : 1)) << 2)
579
_RND4(u32 sz)580 __inline static u32 _RND4(u32 sz)
581 {
582
583 u32 val;
584
585 val = ((sz >> 2) + ((sz & 3) ? 1 : 0)) << 2;
586
587 return val;
588
589 }
590
_RND8(u32 sz)591 __inline static u32 _RND8(u32 sz)
592 {
593
594 u32 val;
595
596 val = ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
597
598 return val;
599
600 }
601
_RND128(u32 sz)602 __inline static u32 _RND128(u32 sz)
603 {
604
605 u32 val;
606
607 val = ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
608
609 return val;
610
611 }
612
_RND256(u32 sz)613 __inline static u32 _RND256(u32 sz)
614 {
615
616 u32 val;
617
618 val = ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
619
620 return val;
621
622 }
623
_RND512(u32 sz)624 __inline static u32 _RND512(u32 sz)
625 {
626
627 u32 val;
628
629 val = ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
630
631 return val;
632
633 }
634
bitshift(u32 bitmask)635 __inline static u32 bitshift(u32 bitmask)
636 {
637 u32 i;
638
639 for (i = 0; i <= 31; i++)
640 if (((bitmask >> i) & 0x1) == 1)
641 break;
642
643 return i;
644 }
645
largest_bit(u32 bitmask)646 static inline int largest_bit(u32 bitmask)
647 {
648 int i;
649
650 for (i = 31; i >= 0; i--)
651 if (bitmask & BIT(i))
652 break;
653
654 return i;
655 }
656
largest_bit_64(u64 bitmask)657 static inline int largest_bit_64(u64 bitmask)
658 {
659 int i;
660
661 for (i = 63; i >= 0; i--)
662 if (bitmask & BIT_ULL(i))
663 break;
664
665 return i;
666 }
667
668 #define rtw_abs(a) (a < 0 ? -a : a)
669 #define rtw_min(a, b) ((a > b) ? b : a)
670 #define rtw_max(a, b) ((a > b) ? a : b)
671 #define rtw_is_range_a_in_b(hi_a, lo_a, hi_b, lo_b) (((hi_a) <= (hi_b)) && ((lo_a) >= (lo_b)))
672 #define rtw_is_range_overlap(hi_a, lo_a, hi_b, lo_b) (((hi_a) > (lo_b)) && ((lo_a) < (hi_b)))
673
674 #ifndef MAC_FMT
675 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
676 #endif
677 #ifndef MAC_ARG
678 #define MAC_ARG(x) ((u8 *)(x))[0], ((u8 *)(x))[1], ((u8 *)(x))[2], ((u8 *)(x))[3], ((u8 *)(x))[4], ((u8 *)(x))[5]
679 #endif
680
681 bool rtw_macaddr_is_larger(const u8 *a, const u8 *b);
682
683 extern void rtw_suspend_lock_init(void);
684 extern void rtw_suspend_lock_uninit(void);
685 extern void rtw_lock_suspend(void);
686 extern void rtw_unlock_suspend(void);
687 extern void rtw_lock_suspend_timeout(u32 timeout_ms);
688 extern void rtw_lock_traffic_suspend_timeout(u32 timeout_ms);
689 extern void rtw_resume_lock_suspend(void);
690 extern void rtw_resume_unlock_suspend(void);
691 #ifdef CONFIG_AP_WOWLAN
692 extern void rtw_softap_lock_suspend(void);
693 extern void rtw_softap_unlock_suspend(void);
694 #endif
695
696 extern void rtw_set_bit(int nr, unsigned long *addr);
697 extern void rtw_clear_bit(int nr, unsigned long *addr);
698 extern int rtw_test_and_clear_bit(int nr, unsigned long *addr);
699
700 extern void ATOMIC_SET(ATOMIC_T *v, int i);
701 extern int ATOMIC_READ(ATOMIC_T *v);
702 extern void ATOMIC_ADD(ATOMIC_T *v, int i);
703 extern void ATOMIC_SUB(ATOMIC_T *v, int i);
704 extern void ATOMIC_INC(ATOMIC_T *v);
705 extern void ATOMIC_DEC(ATOMIC_T *v);
706 extern int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i);
707 extern int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i);
708 extern int ATOMIC_INC_RETURN(ATOMIC_T *v);
709 extern int ATOMIC_DEC_RETURN(ATOMIC_T *v);
710 extern bool ATOMIC_INC_UNLESS(ATOMIC_T *v, int u);
711
712 /* File operation APIs, just for linux now */
713 extern int rtw_is_dir_readable(const char *path);
714 extern int rtw_is_file_readable(const char *path);
715 extern int rtw_is_file_readable_with_size(const char *path, u32 *sz);
716 extern int rtw_readable_file_sz_chk(const char *path, u32 sz);
717 extern int rtw_retrieve_from_file(const char *path, u8 *buf, u32 sz);
718 extern int rtw_store_to_file(const char *path, u8 *buf, u32 sz);
719
720
721 #ifndef PLATFORM_FREEBSD
722 extern void rtw_free_netdev(struct net_device *netdev);
723 #endif /* PLATFORM_FREEBSD */
724
725
726 extern u64 rtw_modular64(u64 x, u64 y);
727 extern u64 rtw_division64(u64 x, u64 y);
728 extern u32 rtw_random32(void);
729
730 /* Macros for handling unaligned memory accesses */
731
732 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
733 #define RTW_PUT_BE16(a, val) \
734 do { \
735 (a)[0] = ((u16) (val)) >> 8; \
736 (a)[1] = ((u16) (val)) & 0xff; \
737 } while (0)
738
739 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
740 #define RTW_PUT_LE16(a, val) \
741 do { \
742 (a)[1] = ((u16) (val)) >> 8; \
743 (a)[0] = ((u16) (val)) & 0xff; \
744 } while (0)
745
746 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
747 ((u32) (a)[2]))
748 #define RTW_PUT_BE24(a, val) \
749 do { \
750 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
751 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
752 (a)[2] = (u8) (((u32) (val)) & 0xff); \
753 } while (0)
754
755 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
756 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
757 #define RTW_PUT_BE32(a, val) \
758 do { \
759 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
760 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
761 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
762 (a)[3] = (u8) (((u32) (val)) & 0xff); \
763 } while (0)
764
765 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
766 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
767 #define RTW_PUT_LE32(a, val) \
768 do { \
769 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
770 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
771 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
772 (a)[0] = (u8) (((u32) (val)) & 0xff); \
773 } while (0)
774
775 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
776 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
777 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
778 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
779 #define RTW_PUT_BE64(a, val) \
780 do { \
781 (a)[0] = (u8) (((u64) (val)) >> 56); \
782 (a)[1] = (u8) (((u64) (val)) >> 48); \
783 (a)[2] = (u8) (((u64) (val)) >> 40); \
784 (a)[3] = (u8) (((u64) (val)) >> 32); \
785 (a)[4] = (u8) (((u64) (val)) >> 24); \
786 (a)[5] = (u8) (((u64) (val)) >> 16); \
787 (a)[6] = (u8) (((u64) (val)) >> 8); \
788 (a)[7] = (u8) (((u64) (val)) & 0xff); \
789 } while (0)
790
791 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
792 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
793 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
794 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
795 #define RTW_PUT_LE64(a, val) \
796 do { \
797 (a)[7] = (u8) ((((u64) (val)) >> 56) & 0xff); \
798 (a)[6] = (u8) ((((u64) (val)) >> 48) & 0xff); \
799 (a)[5] = (u8) ((((u64) (val)) >> 40) & 0xff); \
800 (a)[4] = (u8) ((((u64) (val)) >> 32) & 0xff); \
801 (a)[3] = (u8) ((((u64) (val)) >> 24) & 0xff); \
802 (a)[2] = (u8) ((((u64) (val)) >> 16) & 0xff); \
803 (a)[1] = (u8) ((((u64) (val)) >> 8) & 0xff); \
804 (a)[0] = (u8) (((u64) (val)) & 0xff); \
805 } while (0)
806
807 void rtw_buf_free(u8 **buf, u32 *buf_len);
808 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
809
810 struct rtw_cbuf {
811 u32 write;
812 u32 read;
813 u32 size;
814 void *bufs[0];
815 };
816
817 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
818 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
819 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
820 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
821 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
822 void rtw_cbuf_free(struct rtw_cbuf *cbuf);
823
824 struct map_seg_t {
825 u16 sa;
826 u16 len;
827 u8 *c;
828 };
829
830 struct map_t {
831 u16 len;
832 u16 seg_num;
833 u8 init_value;
834 struct map_seg_t *segs;
835 };
836
837 #define MAPSEG_ARRAY_ENT(_sa, _len, _c, arg...) \
838 { .sa = _sa, .len = _len, .c = (u8[_len]){ _c, ##arg}, }
839
840 #define MAPSEG_PTR_ENT(_sa, _len, _p) \
841 { .sa = _sa, .len = _len, .c = _p, }
842
843 #define MAP_ENT(_len, _seg_num, _init_v, _seg, arg...) \
844 { .len = _len, .seg_num = _seg_num, .init_value = _init_v, .segs = (struct map_seg_t[_seg_num]){ _seg, ##arg}, }
845
846 int map_readN(const struct map_t *map, u16 offset, u16 len, u8 *buf);
847 u8 map_read8(const struct map_t *map, u16 offset);
848
849 struct blacklist_ent {
850 _list list;
851 u8 addr[ETH_ALEN];
852 systime exp_time;
853 };
854
855 #ifdef CONFIG_RTW_MESH
856 int rtw_blacklist_add(_queue *blist, const u8 *addr, u32 timeout_ms);
857 int rtw_blacklist_del(_queue *blist, const u8 *addr);
858 int rtw_blacklist_search(_queue *blist, const u8 *addr);
859 void rtw_blacklist_flush(_queue *blist);
860 void dump_blacklist(void *sel, _queue *blist, const char *title);
861 #endif
862
863 /* String handler */
864
865 BOOLEAN is_null(char c);
866 BOOLEAN is_all_null(char *c, int len);
867 BOOLEAN is_eol(char c);
868 BOOLEAN is_space(char c);
869 BOOLEAN IsHexDigit(char chTmp);
870 BOOLEAN is_alpha(char chTmp);
871 char alpha_to_upper(char c);
872
873 int hex2num_i(char c);
874 int hex2byte_i(const char *hex);
875 int hexstr2bin(const char *hex, u8 *buf, size_t len);
876
877 int hwaddr_aton_i(const char *txt, u8 *addr);
878
879 /*
880 * Write formatted output to sized buffer
881 */
882 #ifdef PLATFORM_LINUX
883 #define rtw_sprintf(buf, size, format, arg...) snprintf(buf, size, format, ##arg)
884 #else /* !PLATFORM_LINUX */
885 #error "NOT DEFINE \"rtw_sprintf\"!!"
886 #endif /* !PLATFORM_LINUX */
887
888 #endif
889