xref: /freebsd/sys/contrib/dev/rtw89/util.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2  * Copyright(c) 2019-2020  Realtek Corporation
3  */
4 #ifndef __RTW89_UTIL_H__
5 #define __RTW89_UTIL_H__
6 
7 #include "core.h"
8 
9 #define RTW89_LINEAR_FRAC_BITS 3
10 
11 #define rtw89_iterate_vifs_bh(rtwdev, iterator, data)                          \
12 	ieee80211_iterate_active_interfaces_atomic((rtwdev)->hw,               \
13 			IEEE80211_IFACE_ITER_NORMAL, iterator, data)
14 
15 /* call this function with rtwdev->mutex is held */
16 #define rtw89_for_each_rtwvif(rtwdev, rtwvif)				       \
17 	list_for_each_entry(rtwvif, &(rtwdev)->rtwvifs_list, list)
18 
19 /* The result of negative dividend and positive divisor is undefined, but it
20  * should be one case of round-down or round-up. So, make it round-down if the
21  * result is round-up.
22  * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to
23  *       signed value to make compiler to use signed divide instruction.
24  */
25 static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder)
26 {
27 	s32 i_divisor = (s32)divisor;
28 	s32 i_remainder;
29 	s32 quotient;
30 
31 	quotient = dividend / i_divisor;
32 	i_remainder = dividend % i_divisor;
33 
34 	if (i_remainder < 0) {
35 		quotient--;
36 		i_remainder += i_divisor;
37 	}
38 
39 	if (remainder)
40 		*remainder = i_remainder;
41 	return quotient;
42 }
43 
44 static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor)
45 {
46 	return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL);
47 }
48 
49 static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask)
50 {
51 	int i;
52 
53 	eth_zero_addr(dst);
54 	for (i = 0; i < ETH_ALEN; i++) {
55 		if (mask & BIT(i))
56 			dst[i] = src[i];
57 	}
58 }
59 
60 u32 rtw89_linear_2_db(u64 linear);
61 u64 rtw89_db_2_linear(u32 db);
62 
63 #endif
64