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_iterate_vifs_bh(rtwdev, iterator, data) \
10 ieee80211_iterate_active_interfaces_atomic((rtwdev)->hw, \
11 IEEE80211_IFACE_ITER_NORMAL, iterator, data)
12
13 /* call this function with rtwdev->mutex is held */
14 #define rtw89_for_each_rtwvif(rtwdev, rtwvif) \
15 list_for_each_entry(rtwvif, &(rtwdev)->rtwvifs_list, list)
16
17 /* Before adding rtwvif to list, we need to check if it already exist, beacase
18 * in some case such as SER L2 happen during WoWLAN flow, calling reconfig
19 * twice cause the list to be added twice.
20 */
rtw89_rtwvif_in_list(struct rtw89_dev * rtwdev,struct rtw89_vif * new)21 static inline bool rtw89_rtwvif_in_list(struct rtw89_dev *rtwdev,
22 struct rtw89_vif *new)
23 {
24 struct rtw89_vif *rtwvif;
25
26 lockdep_assert_held(&rtwdev->mutex);
27
28 rtw89_for_each_rtwvif(rtwdev, rtwvif)
29 if (rtwvif == new)
30 return true;
31
32 return false;
33 }
34
35 /* The result of negative dividend and positive divisor is undefined, but it
36 * should be one case of round-down or round-up. So, make it round-down if the
37 * result is round-up.
38 * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to
39 * signed value to make compiler to use signed divide instruction.
40 */
s32_div_u32_round_down(s32 dividend,u32 divisor,s32 * remainder)41 static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder)
42 {
43 s32 i_divisor = (s32)divisor;
44 s32 i_remainder;
45 s32 quotient;
46
47 quotient = dividend / i_divisor;
48 i_remainder = dividend % i_divisor;
49
50 if (i_remainder < 0) {
51 quotient--;
52 i_remainder += i_divisor;
53 }
54
55 if (remainder)
56 *remainder = i_remainder;
57 return quotient;
58 }
59
s32_div_u32_round_closest(s32 dividend,u32 divisor)60 static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor)
61 {
62 return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL);
63 }
64
ether_addr_copy_mask(u8 * dst,const u8 * src,u8 mask)65 static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask)
66 {
67 int i;
68
69 eth_zero_addr(dst);
70 for (i = 0; i < ETH_ALEN; i++) {
71 if (mask & BIT(i))
72 dst[i] = src[i];
73 }
74 }
75
76 #endif
77