xref: /linux/drivers/net/wireless/realtek/rtw89/util.h (revision 9410645520e9b820069761f3450ef6661418e279)
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 /* Before adding rtwvif to list, we need to check if it already exist, beacase
20  * in some case such as SER L2 happen during WoWLAN flow, calling reconfig
21  * twice cause the list to be added twice.
22  */
rtw89_rtwvif_in_list(struct rtw89_dev * rtwdev,struct rtw89_vif * new)23 static inline bool rtw89_rtwvif_in_list(struct rtw89_dev *rtwdev,
24 					struct rtw89_vif *new)
25 {
26 	struct rtw89_vif *rtwvif;
27 
28 	lockdep_assert_held(&rtwdev->mutex);
29 
30 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
31 		if (rtwvif == new)
32 			return true;
33 
34 	return false;
35 }
36 
37 /* The result of negative dividend and positive divisor is undefined, but it
38  * should be one case of round-down or round-up. So, make it round-down if the
39  * result is round-up.
40  * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to
41  *       signed value to make compiler to use signed divide instruction.
42  */
s32_div_u32_round_down(s32 dividend,u32 divisor,s32 * remainder)43 static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder)
44 {
45 	s32 i_divisor = (s32)divisor;
46 	s32 i_remainder;
47 	s32 quotient;
48 
49 	quotient = dividend / i_divisor;
50 	i_remainder = dividend % i_divisor;
51 
52 	if (i_remainder < 0) {
53 		quotient--;
54 		i_remainder += i_divisor;
55 	}
56 
57 	if (remainder)
58 		*remainder = i_remainder;
59 	return quotient;
60 }
61 
s32_div_u32_round_closest(s32 dividend,u32 divisor)62 static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor)
63 {
64 	return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL);
65 }
66 
ether_addr_copy_mask(u8 * dst,const u8 * src,u8 mask)67 static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask)
68 {
69 	int i;
70 
71 	eth_zero_addr(dst);
72 	for (i = 0; i < ETH_ALEN; i++) {
73 		if (mask & BIT(i))
74 			dst[i] = src[i];
75 	}
76 }
77 
78 u32 rtw89_linear_2_db(u64 linear);
79 u64 rtw89_db_2_linear(u32 db);
80 
81 #endif
82