1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2018-2019 Realtek Corporation 3 */ 4 5 #include "main.h" 6 #include "util.h" 7 #include "reg.h" 8 9 bool check_hw_ready(struct rtw_dev *rtwdev, u32 addr, u32 mask, u32 target) 10 { 11 u32 cnt; 12 13 for (cnt = 0; cnt < 1000; cnt++) { 14 if (rtw_read32_mask(rtwdev, addr, mask) == target) 15 return true; 16 17 udelay(10); 18 } 19 20 return false; 21 } 22 EXPORT_SYMBOL(check_hw_ready); 23 24 bool ltecoex_read_reg(struct rtw_dev *rtwdev, u16 offset, u32 *val) 25 { 26 const struct rtw_chip_info *chip = rtwdev->chip; 27 const struct rtw_ltecoex_addr *ltecoex = chip->ltecoex_addr; 28 29 if (!check_hw_ready(rtwdev, ltecoex->ctrl, LTECOEX_READY, 1)) 30 return false; 31 32 rtw_write32(rtwdev, ltecoex->ctrl, 0x800F0000 | offset); 33 *val = rtw_read32(rtwdev, ltecoex->rdata); 34 35 return true; 36 } 37 38 bool ltecoex_reg_write(struct rtw_dev *rtwdev, u16 offset, u32 value) 39 { 40 const struct rtw_chip_info *chip = rtwdev->chip; 41 const struct rtw_ltecoex_addr *ltecoex = chip->ltecoex_addr; 42 43 if (!check_hw_ready(rtwdev, ltecoex->ctrl, LTECOEX_READY, 1)) 44 return false; 45 46 rtw_write32(rtwdev, ltecoex->wdata, value); 47 rtw_write32(rtwdev, ltecoex->ctrl, 0xC00F0000 | offset); 48 49 return true; 50 } 51 52 void rtw_restore_reg(struct rtw_dev *rtwdev, 53 struct rtw_backup_info *bckp, u32 num) 54 { 55 u8 len; 56 u32 reg; 57 u32 val; 58 int i; 59 60 for (i = 0; i < num; i++, bckp++) { 61 len = bckp->len; 62 reg = bckp->reg; 63 val = bckp->val; 64 65 switch (len) { 66 case 1: 67 rtw_write8(rtwdev, reg, (u8)val); 68 break; 69 case 2: 70 rtw_write16(rtwdev, reg, (u16)val); 71 break; 72 case 4: 73 rtw_write32(rtwdev, reg, (u32)val); 74 break; 75 default: 76 break; 77 } 78 } 79 } 80 EXPORT_SYMBOL(rtw_restore_reg); 81 82 void rtw_desc_to_mcsrate(u16 rate, u8 *mcs, u8 *nss) 83 { 84 if (rate <= DESC_RATE54M) 85 return; 86 87 if (rate >= DESC_RATEVHT1SS_MCS0 && 88 rate <= DESC_RATEVHT1SS_MCS9) { 89 *nss = 1; 90 *mcs = rate - DESC_RATEVHT1SS_MCS0; 91 } else if (rate >= DESC_RATEVHT2SS_MCS0 && 92 rate <= DESC_RATEVHT2SS_MCS9) { 93 *nss = 2; 94 *mcs = rate - DESC_RATEVHT2SS_MCS0; 95 } else if (rate >= DESC_RATEVHT3SS_MCS0 && 96 rate <= DESC_RATEVHT3SS_MCS9) { 97 *nss = 3; 98 *mcs = rate - DESC_RATEVHT3SS_MCS0; 99 } else if (rate >= DESC_RATEVHT4SS_MCS0 && 100 rate <= DESC_RATEVHT4SS_MCS9) { 101 *nss = 4; 102 *mcs = rate - DESC_RATEVHT4SS_MCS0; 103 } else if (rate >= DESC_RATEMCS0 && 104 rate <= DESC_RATEMCS15) { 105 *mcs = rate - DESC_RATEMCS0; 106 } 107 } 108 109 struct rtw_stas_entry { 110 struct list_head list; 111 struct ieee80211_sta *sta; 112 }; 113 114 struct rtw_iter_stas_data { 115 struct rtw_dev *rtwdev; 116 struct list_head list; 117 }; 118 119 static void rtw_collect_sta_iter(void *data, struct ieee80211_sta *sta) 120 { 121 struct rtw_iter_stas_data *iter_stas = data; 122 struct rtw_stas_entry *stas_entry; 123 124 stas_entry = kmalloc(sizeof(*stas_entry), GFP_ATOMIC); 125 if (!stas_entry) 126 return; 127 128 stas_entry->sta = sta; 129 list_add_tail(&stas_entry->list, &iter_stas->list); 130 } 131 132 void rtw_iterate_stas(struct rtw_dev *rtwdev, 133 void (*iterator)(void *data, 134 struct ieee80211_sta *sta), 135 void *data) 136 { 137 struct rtw_iter_stas_data iter_data; 138 struct rtw_stas_entry *sta_entry, *tmp; 139 140 /* &rtwdev->mutex makes sure no stations can be removed between 141 * collecting the stations and iterating over them. 142 */ 143 lockdep_assert_held(&rtwdev->mutex); 144 145 iter_data.rtwdev = rtwdev; 146 INIT_LIST_HEAD(&iter_data.list); 147 148 ieee80211_iterate_stations_atomic(rtwdev->hw, rtw_collect_sta_iter, 149 &iter_data); 150 151 list_for_each_entry_safe(sta_entry, tmp, &iter_data.list, 152 list) { 153 list_del_init(&sta_entry->list); 154 iterator(data, sta_entry->sta); 155 kfree(sta_entry); 156 } 157 } 158 159 struct rtw_vifs_entry { 160 struct list_head list; 161 struct ieee80211_vif *vif; 162 u8 mac[ETH_ALEN]; 163 }; 164 165 struct rtw_iter_vifs_data { 166 struct rtw_dev *rtwdev; 167 struct list_head list; 168 }; 169 170 static void rtw_collect_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif) 171 { 172 struct rtw_iter_vifs_data *iter_stas = data; 173 struct rtw_vifs_entry *vifs_entry; 174 175 vifs_entry = kmalloc(sizeof(*vifs_entry), GFP_ATOMIC); 176 if (!vifs_entry) 177 return; 178 179 vifs_entry->vif = vif; 180 ether_addr_copy(vifs_entry->mac, mac); 181 list_add_tail(&vifs_entry->list, &iter_stas->list); 182 } 183 184 void rtw_iterate_vifs(struct rtw_dev *rtwdev, 185 void (*iterator)(void *data, u8 *mac, 186 struct ieee80211_vif *vif), 187 void *data) 188 { 189 struct rtw_iter_vifs_data iter_data; 190 struct rtw_vifs_entry *vif_entry, *tmp; 191 192 /* &rtwdev->mutex makes sure no interfaces can be removed between 193 * collecting the interfaces and iterating over them. 194 */ 195 lockdep_assert_held(&rtwdev->mutex); 196 197 iter_data.rtwdev = rtwdev; 198 INIT_LIST_HEAD(&iter_data.list); 199 200 ieee80211_iterate_active_interfaces_atomic(rtwdev->hw, 201 IEEE80211_IFACE_ITER_NORMAL, 202 rtw_collect_vif_iter, &iter_data); 203 204 list_for_each_entry_safe(vif_entry, tmp, &iter_data.list, 205 list) { 206 list_del_init(&vif_entry->list); 207 iterator(data, vif_entry->mac, vif_entry->vif); 208 kfree(vif_entry); 209 } 210 } 211