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_KEY_PN_0 GENMASK_ULL(7, 0) 10 #define RTW89_KEY_PN_1 GENMASK_ULL(15, 8) 11 #define RTW89_KEY_PN_2 GENMASK_ULL(23, 16) 12 #define RTW89_KEY_PN_3 GENMASK_ULL(31, 24) 13 #define RTW89_KEY_PN_4 GENMASK_ULL(39, 32) 14 #define RTW89_KEY_PN_5 GENMASK_ULL(47, 40) 15 16 #define rtw89_iterate_vifs_bh(rtwdev, iterator, data) \ 17 ieee80211_iterate_active_interfaces_atomic((rtwdev)->hw, \ 18 IEEE80211_IFACE_ITER_NORMAL, iterator, data) 19 20 /* call this function with wiphy mutex is held */ 21 #define rtw89_for_each_rtwvif(rtwdev, rtwvif) \ 22 list_for_each_entry(rtwvif, &(rtwdev)->rtwvifs_list, list) 23 24 /* Before adding rtwvif to list, we need to check if it already exist, beacase 25 * in some case such as SER L2 happen during WoWLAN flow, calling reconfig 26 * twice cause the list to be added twice. 27 */ 28 static inline bool rtw89_rtwvif_in_list(struct rtw89_dev *rtwdev, 29 struct rtw89_vif *new) 30 { 31 struct rtw89_vif *rtwvif; 32 33 lockdep_assert_wiphy(rtwdev->hw->wiphy); 34 35 rtw89_for_each_rtwvif(rtwdev, rtwvif) 36 if (rtwvif == new) 37 return true; 38 39 return false; 40 } 41 42 /* The result of negative dividend and positive divisor is undefined, but it 43 * should be one case of round-down or round-up. So, make it round-down if the 44 * result is round-up. 45 * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to 46 * signed value to make compiler to use signed divide instruction. 47 */ 48 static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder) 49 { 50 s32 i_divisor = (s32)divisor; 51 s32 i_remainder; 52 s32 quotient; 53 54 quotient = dividend / i_divisor; 55 i_remainder = dividend % i_divisor; 56 57 if (i_remainder < 0) { 58 quotient--; 59 i_remainder += i_divisor; 60 } 61 62 if (remainder) 63 *remainder = i_remainder; 64 return quotient; 65 } 66 67 static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor) 68 { 69 return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL); 70 } 71 72 static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask) 73 { 74 int i; 75 76 eth_zero_addr(dst); 77 for (i = 0; i < ETH_ALEN; i++) { 78 if (mask & BIT(i)) 79 dst[i] = src[i]; 80 } 81 } 82 83 static inline void ccmp_hdr2pn(s64 *pn, const u8 *hdr) 84 { 85 *pn = u64_encode_bits(hdr[0], RTW89_KEY_PN_0) | 86 u64_encode_bits(hdr[1], RTW89_KEY_PN_1) | 87 u64_encode_bits(hdr[4], RTW89_KEY_PN_2) | 88 u64_encode_bits(hdr[5], RTW89_KEY_PN_3) | 89 u64_encode_bits(hdr[6], RTW89_KEY_PN_4) | 90 u64_encode_bits(hdr[7], RTW89_KEY_PN_5); 91 } 92 93 s32 rtw89_linear_to_db_quarter(u64 val); 94 s32 rtw89_linear_to_db(u64 val); 95 u64 rtw89_db_quarter_to_linear(s32 db); 96 u64 rtw89_db_to_linear(s32 db); 97 void rtw89_might_trailing_ellipsis(char *buf, size_t size, ssize_t used); 98 99 #endif 100