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 /* The result of negative dividend and positive divisor is undefined, but it
18  * should be one case of round-down or round-up. So, make it round-down if the
19  * result is round-up.
20  * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to
21  *       signed value to make compiler to use signed divide instruction.
22  */
s32_div_u32_round_down(s32 dividend,u32 divisor,s32 * remainder)23 static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder)
24 {
25 	s32 i_divisor = (s32)divisor;
26 	s32 i_remainder;
27 	s32 quotient;
28 
29 	quotient = dividend / i_divisor;
30 	i_remainder = dividend % i_divisor;
31 
32 	if (i_remainder < 0) {
33 		quotient--;
34 		i_remainder += i_divisor;
35 	}
36 
37 	if (remainder)
38 		*remainder = i_remainder;
39 	return quotient;
40 }
41 
s32_div_u32_round_closest(s32 dividend,u32 divisor)42 static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor)
43 {
44 	return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL);
45 }
46 
ether_addr_copy_mask(u8 * dst,const u8 * src,u8 mask)47 static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask)
48 {
49 	int i;
50 
51 	eth_zero_addr(dst);
52 	for (i = 0; i < ETH_ALEN; i++) {
53 		if (mask & BIT(i))
54 			dst[i] = src[i];
55 	}
56 }
57 
58 #endif
59