1 /* 2 * Copyright (c) 2014, Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <linux/export.h> 18 #include "ath9k.h" 19 #include "hw.h" 20 #include "dynack.h" 21 22 #define COMPUTE_TO (5 * HZ) 23 #define LATEACK_DELAY (10 * HZ) 24 #define EWMA_LEVEL 96 25 #define EWMA_DIV 128 26 27 /** 28 * ath_dynack_get_max_to - set max timeout according to channel width 29 * @ah: ath hw 30 * 31 */ 32 static u32 ath_dynack_get_max_to(struct ath_hw *ah) 33 { 34 const struct ath9k_channel *chan = ah->curchan; 35 36 if (!chan) 37 return 300; 38 39 if (IS_CHAN_HT40(chan)) 40 return 300; 41 if (IS_CHAN_HALF_RATE(chan)) 42 return 750; 43 if (IS_CHAN_QUARTER_RATE(chan)) 44 return 1500; 45 return 600; 46 } 47 48 /* 49 * ath_dynack_ewma - EWMA (Exponentially Weighted Moving Average) calculation 50 */ 51 static inline int ath_dynack_ewma(int old, int new) 52 { 53 if (old > 0) 54 return (new * (EWMA_DIV - EWMA_LEVEL) + 55 old * EWMA_LEVEL) / EWMA_DIV; 56 else 57 return new; 58 } 59 60 /** 61 * ath_dynack_get_sifs - get sifs time based on phy used 62 * @ah: ath hw 63 * @phy: phy used 64 * 65 */ 66 static inline u32 ath_dynack_get_sifs(struct ath_hw *ah, int phy) 67 { 68 u32 sifs = CCK_SIFS_TIME; 69 70 if (phy == WLAN_RC_PHY_OFDM) { 71 if (IS_CHAN_QUARTER_RATE(ah->curchan)) 72 sifs = OFDM_SIFS_TIME_QUARTER; 73 else if (IS_CHAN_HALF_RATE(ah->curchan)) 74 sifs = OFDM_SIFS_TIME_HALF; 75 else 76 sifs = OFDM_SIFS_TIME; 77 } 78 return sifs; 79 } 80 81 /** 82 * ath_dynack_bssidmask - filter out ACK frames based on BSSID mask 83 * @ah: ath hw 84 * @mac: receiver address 85 */ 86 static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac) 87 { 88 int i; 89 struct ath_common *common = ath9k_hw_common(ah); 90 91 for (i = 0; i < ETH_ALEN; i++) { 92 if ((common->macaddr[i] & common->bssidmask[i]) != 93 (mac[i] & common->bssidmask[i])) 94 return false; 95 } 96 97 return true; 98 } 99 100 /** 101 * ath_dynack_set_timeout - configure timeouts/slottime registers 102 * @ah: ath hw 103 * @to: timeout value 104 * 105 */ 106 static void ath_dynack_set_timeout(struct ath_hw *ah, int to) 107 { 108 struct ath_common *common = ath9k_hw_common(ah); 109 int slottime = (to - 3) / 2; 110 111 ath_dbg(common, DYNACK, "ACK timeout %u slottime %u\n", 112 to, slottime); 113 ath9k_hw_setslottime(ah, slottime); 114 ath9k_hw_set_ack_timeout(ah, to); 115 ath9k_hw_set_cts_timeout(ah, to); 116 } 117 118 /** 119 * ath_dynack_compute_ackto - compute ACK timeout as the maximum STA timeout 120 * @ah: ath hw 121 * 122 * should be called while holding qlock 123 */ 124 static void ath_dynack_compute_ackto(struct ath_hw *ah) 125 { 126 struct ath_dynack *da = &ah->dynack; 127 struct ath_node *an; 128 int to = 0; 129 130 list_for_each_entry(an, &da->nodes, list) 131 if (an->ackto > to) 132 to = an->ackto; 133 134 if (to && da->ackto != to) { 135 ath_dynack_set_timeout(ah, to); 136 da->ackto = to; 137 } 138 } 139 140 /** 141 * ath_dynack_compute_to - compute STA ACK timeout 142 * @ah: ath hw 143 * 144 * should be called while holding qlock 145 */ 146 static void ath_dynack_compute_to(struct ath_hw *ah) 147 { 148 struct ath_dynack *da = &ah->dynack; 149 u32 ackto, ack_ts, max_to; 150 struct ieee80211_sta *sta; 151 struct ts_info *st_ts; 152 struct ath_node *an; 153 u8 *dst, *src; 154 155 rcu_read_lock(); 156 157 max_to = ath_dynack_get_max_to(ah); 158 while (da->st_rbf.h_rb != da->st_rbf.t_rb && 159 da->ack_rbf.h_rb != da->ack_rbf.t_rb) { 160 ack_ts = da->ack_rbf.tstamp[da->ack_rbf.h_rb]; 161 st_ts = &da->st_rbf.ts[da->st_rbf.h_rb]; 162 dst = da->st_rbf.addr[da->st_rbf.h_rb].h_dest; 163 src = da->st_rbf.addr[da->st_rbf.h_rb].h_src; 164 165 ath_dbg(ath9k_hw_common(ah), DYNACK, 166 "ack_ts %u st_ts %u st_dur %u [%u-%u]\n", 167 ack_ts, st_ts->tstamp, st_ts->dur, 168 da->ack_rbf.h_rb, da->st_rbf.h_rb); 169 170 if (ack_ts > st_ts->tstamp + st_ts->dur) { 171 ackto = ack_ts - st_ts->tstamp - st_ts->dur; 172 173 if (ackto < max_to) { 174 sta = ieee80211_find_sta_by_ifaddr(ah->hw, dst, 175 src); 176 if (sta) { 177 an = (struct ath_node *)sta->drv_priv; 178 an->ackto = ath_dynack_ewma(an->ackto, 179 ackto); 180 ath_dbg(ath9k_hw_common(ah), DYNACK, 181 "%pM to %d [%u]\n", dst, 182 an->ackto, ackto); 183 if (time_is_before_jiffies(da->lto)) { 184 ath_dynack_compute_ackto(ah); 185 da->lto = jiffies + COMPUTE_TO; 186 } 187 } 188 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF); 189 } 190 INCR(da->st_rbf.h_rb, ATH_DYN_BUF); 191 } else { 192 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF); 193 } 194 } 195 196 rcu_read_unlock(); 197 } 198 199 /** 200 * ath_dynack_sample_tx_ts - status timestamp sampling method 201 * @ah: ath hw 202 * @skb: socket buffer 203 * @ts: tx status info 204 * @sta: station pointer 205 * 206 */ 207 void ath_dynack_sample_tx_ts(struct ath_hw *ah, struct sk_buff *skb, 208 struct ath_tx_status *ts, 209 struct ieee80211_sta *sta) 210 { 211 struct ieee80211_hdr *hdr; 212 struct ath_dynack *da = &ah->dynack; 213 struct ath_common *common = ath9k_hw_common(ah); 214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 215 u32 dur = ts->duration; 216 u8 ridx; 217 218 if (!da->enabled || (info->flags & IEEE80211_TX_CTL_NO_ACK)) 219 return; 220 221 spin_lock_bh(&da->qlock); 222 223 hdr = (struct ieee80211_hdr *)skb->data; 224 225 /* late ACK */ 226 if (ts->ts_status & ATH9K_TXERR_XRETRY) { 227 if (ieee80211_is_assoc_req(hdr->frame_control) || 228 ieee80211_is_assoc_resp(hdr->frame_control) || 229 ieee80211_is_auth(hdr->frame_control)) { 230 u32 max_to = ath_dynack_get_max_to(ah); 231 232 ath_dbg(common, DYNACK, "late ack\n"); 233 ath_dynack_set_timeout(ah, max_to); 234 if (sta) { 235 struct ath_node *an; 236 237 an = (struct ath_node *)sta->drv_priv; 238 an->ackto = -1; 239 } 240 da->lto = jiffies + LATEACK_DELAY; 241 } 242 243 spin_unlock_bh(&da->qlock); 244 return; 245 } 246 247 ridx = ts->ts_rateindex; 248 249 da->st_rbf.ts[da->st_rbf.t_rb].tstamp = ts->ts_tstamp; 250 251 /* ether_addr_copy() gives a false warning on gcc-10 so use memcpy() 252 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97490 253 */ 254 memcpy(da->st_rbf.addr[da->st_rbf.t_rb].h_dest, hdr->addr1, ETH_ALEN); 255 memcpy(da->st_rbf.addr[da->st_rbf.t_rb].h_src, hdr->addr2, ETH_ALEN); 256 257 if (!(info->status.rates[ridx].flags & IEEE80211_TX_RC_MCS)) { 258 const struct ieee80211_rate *rate; 259 struct ieee80211_tx_rate *rates = info->status.rates; 260 u32 phy; 261 262 rate = &common->sbands[info->band].bitrates[rates[ridx].idx]; 263 if (info->band == NL80211_BAND_2GHZ && 264 !(rate->flags & IEEE80211_RATE_ERP_G)) 265 phy = WLAN_RC_PHY_CCK; 266 else 267 phy = WLAN_RC_PHY_OFDM; 268 269 dur -= ath_dynack_get_sifs(ah, phy); 270 } 271 da->st_rbf.ts[da->st_rbf.t_rb].dur = dur; 272 273 INCR(da->st_rbf.t_rb, ATH_DYN_BUF); 274 if (da->st_rbf.t_rb == da->st_rbf.h_rb) 275 INCR(da->st_rbf.h_rb, ATH_DYN_BUF); 276 277 ath_dbg(common, DYNACK, "{%pM} tx sample %u [dur %u][h %u-t %u]\n", 278 hdr->addr1, ts->ts_tstamp, dur, da->st_rbf.h_rb, 279 da->st_rbf.t_rb); 280 281 ath_dynack_compute_to(ah); 282 283 spin_unlock_bh(&da->qlock); 284 } 285 EXPORT_SYMBOL(ath_dynack_sample_tx_ts); 286 287 /** 288 * ath_dynack_sample_ack_ts - ACK timestamp sampling method 289 * @ah: ath hw 290 * @skb: socket buffer 291 * @ts: rx timestamp 292 * 293 */ 294 void ath_dynack_sample_ack_ts(struct ath_hw *ah, struct sk_buff *skb, 295 u32 ts) 296 { 297 struct ath_dynack *da = &ah->dynack; 298 struct ath_common *common = ath9k_hw_common(ah); 299 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 300 301 if (!da->enabled || !ath_dynack_bssidmask(ah, hdr->addr1)) 302 return; 303 304 spin_lock_bh(&da->qlock); 305 da->ack_rbf.tstamp[da->ack_rbf.t_rb] = ts; 306 307 INCR(da->ack_rbf.t_rb, ATH_DYN_BUF); 308 if (da->ack_rbf.t_rb == da->ack_rbf.h_rb) 309 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF); 310 311 ath_dbg(common, DYNACK, "rx sample %u [h %u-t %u]\n", 312 ts, da->ack_rbf.h_rb, da->ack_rbf.t_rb); 313 314 ath_dynack_compute_to(ah); 315 316 spin_unlock_bh(&da->qlock); 317 } 318 EXPORT_SYMBOL(ath_dynack_sample_ack_ts); 319 320 /** 321 * ath_dynack_node_init - init ath_node related info 322 * @ah: ath hw 323 * @an: ath node 324 * 325 */ 326 void ath_dynack_node_init(struct ath_hw *ah, struct ath_node *an) 327 { 328 struct ath_dynack *da = &ah->dynack; 329 330 an->ackto = da->ackto; 331 332 spin_lock_bh(&da->qlock); 333 list_add_tail(&an->list, &da->nodes); 334 spin_unlock_bh(&da->qlock); 335 } 336 EXPORT_SYMBOL(ath_dynack_node_init); 337 338 /** 339 * ath_dynack_node_deinit - deinit ath_node related info 340 * @ah: ath hw 341 * @an: ath node 342 * 343 */ 344 void ath_dynack_node_deinit(struct ath_hw *ah, struct ath_node *an) 345 { 346 struct ath_dynack *da = &ah->dynack; 347 348 spin_lock_bh(&da->qlock); 349 list_del(&an->list); 350 spin_unlock_bh(&da->qlock); 351 } 352 EXPORT_SYMBOL(ath_dynack_node_deinit); 353 354 /** 355 * ath_dynack_reset - reset dynack processing 356 * @ah: ath hw 357 * 358 */ 359 void ath_dynack_reset(struct ath_hw *ah) 360 { 361 struct ath_dynack *da = &ah->dynack; 362 struct ath_node *an; 363 364 spin_lock_bh(&da->qlock); 365 366 da->lto = jiffies + COMPUTE_TO; 367 368 da->st_rbf.t_rb = 0; 369 da->st_rbf.h_rb = 0; 370 da->ack_rbf.t_rb = 0; 371 da->ack_rbf.h_rb = 0; 372 373 da->ackto = ath_dynack_get_max_to(ah); 374 list_for_each_entry(an, &da->nodes, list) 375 an->ackto = da->ackto; 376 377 /* init acktimeout */ 378 ath_dynack_set_timeout(ah, da->ackto); 379 380 spin_unlock_bh(&da->qlock); 381 } 382 EXPORT_SYMBOL(ath_dynack_reset); 383 384 /** 385 * ath_dynack_init - init dynack data structure 386 * @ah: ath hw 387 * 388 */ 389 void ath_dynack_init(struct ath_hw *ah) 390 { 391 struct ath_dynack *da = &ah->dynack; 392 393 memset(da, 0, sizeof(struct ath_dynack)); 394 395 spin_lock_init(&da->qlock); 396 INIT_LIST_HEAD(&da->nodes); 397 /* ackto = slottime + sifs + air delay */ 398 da->ackto = 9 + 16 + 64; 399 400 ah->hw->wiphy->features |= NL80211_FEATURE_ACKTO_ESTIMATION; 401 } 402