dynack.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "ath9k.h"
  17. #include "hw.h"
  18. #include "dynack.h"
  19. #define COMPUTE_TO (5 * HZ)
  20. #define LATEACK_DELAY (10 * HZ)
  21. #define LATEACK_TO 256
  22. #define MAX_DELAY 300
  23. #define EWMA_LEVEL 96
  24. #define EWMA_DIV 128
  25. /**
  26. * ath_dynack_ewma - EWMA (Exponentially Weighted Moving Average) calculation
  27. *
  28. */
  29. static inline int ath_dynack_ewma(int old, int new)
  30. {
  31. if (old > 0)
  32. return (new * (EWMA_DIV - EWMA_LEVEL) +
  33. old * EWMA_LEVEL) / EWMA_DIV;
  34. else
  35. return new;
  36. }
  37. /**
  38. * ath_dynack_get_sifs - get sifs time based on phy used
  39. * @ah: ath hw
  40. * @phy: phy used
  41. *
  42. */
  43. static inline u32 ath_dynack_get_sifs(struct ath_hw *ah, int phy)
  44. {
  45. u32 sifs = CCK_SIFS_TIME;
  46. if (phy == WLAN_RC_PHY_OFDM) {
  47. if (IS_CHAN_QUARTER_RATE(ah->curchan))
  48. sifs = OFDM_SIFS_TIME_QUARTER;
  49. else if (IS_CHAN_HALF_RATE(ah->curchan))
  50. sifs = OFDM_SIFS_TIME_HALF;
  51. else
  52. sifs = OFDM_SIFS_TIME;
  53. }
  54. return sifs;
  55. }
  56. /**
  57. * ath_dynack_bssidmask - filter out ACK frames based on BSSID mask
  58. * @ah: ath hw
  59. * @mac: receiver address
  60. */
  61. static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac)
  62. {
  63. int i;
  64. struct ath_common *common = ath9k_hw_common(ah);
  65. for (i = 0; i < ETH_ALEN; i++) {
  66. if ((common->macaddr[i] & common->bssidmask[i]) !=
  67. (mac[i] & common->bssidmask[i]))
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * ath_dynack_compute_ackto - compute ACK timeout as the maximum STA timeout
  74. * @ah: ath hw
  75. *
  76. * should be called while holding qlock
  77. */
  78. static void ath_dynack_compute_ackto(struct ath_hw *ah)
  79. {
  80. struct ath_common *common = ath9k_hw_common(ah);
  81. struct ath_dynack *da = &ah->dynack;
  82. struct ath_node *an;
  83. int to = 0;
  84. list_for_each_entry(an, &da->nodes, list)
  85. if (an->ackto > to)
  86. to = an->ackto;
  87. if (to && da->ackto != to) {
  88. u32 slottime;
  89. slottime = (to - 3) / 2;
  90. da->ackto = to;
  91. ath_dbg(common, DYNACK, "ACK timeout %u slottime %u\n",
  92. da->ackto, slottime);
  93. ath9k_hw_setslottime(ah, slottime);
  94. ath9k_hw_set_ack_timeout(ah, da->ackto);
  95. ath9k_hw_set_cts_timeout(ah, da->ackto);
  96. }
  97. }
  98. /**
  99. * ath_dynack_compute_to - compute STA ACK timeout
  100. * @ah: ath hw
  101. *
  102. * should be called while holding qlock
  103. */
  104. static void ath_dynack_compute_to(struct ath_hw *ah)
  105. {
  106. u32 ackto, ack_ts;
  107. u8 *dst, *src;
  108. struct ieee80211_sta *sta;
  109. struct ath_node *an;
  110. struct ts_info *st_ts;
  111. struct ath_dynack *da = &ah->dynack;
  112. rcu_read_lock();
  113. while (da->st_rbf.h_rb != da->st_rbf.t_rb &&
  114. da->ack_rbf.h_rb != da->ack_rbf.t_rb) {
  115. ack_ts = da->ack_rbf.tstamp[da->ack_rbf.h_rb];
  116. st_ts = &da->st_rbf.ts[da->st_rbf.h_rb];
  117. dst = da->st_rbf.addr[da->st_rbf.h_rb].h_dest;
  118. src = da->st_rbf.addr[da->st_rbf.h_rb].h_src;
  119. ath_dbg(ath9k_hw_common(ah), DYNACK,
  120. "ack_ts %u st_ts %u st_dur %u [%u-%u]\n",
  121. ack_ts, st_ts->tstamp, st_ts->dur,
  122. da->ack_rbf.h_rb, da->st_rbf.h_rb);
  123. if (ack_ts > st_ts->tstamp + st_ts->dur) {
  124. ackto = ack_ts - st_ts->tstamp - st_ts->dur;
  125. if (ackto < MAX_DELAY) {
  126. sta = ieee80211_find_sta_by_ifaddr(ah->hw, dst,
  127. src);
  128. if (sta) {
  129. an = (struct ath_node *)sta->drv_priv;
  130. an->ackto = ath_dynack_ewma(an->ackto,
  131. ackto);
  132. ath_dbg(ath9k_hw_common(ah), DYNACK,
  133. "%pM to %d [%u]\n", dst,
  134. an->ackto, ackto);
  135. if (time_is_before_jiffies(da->lto)) {
  136. ath_dynack_compute_ackto(ah);
  137. da->lto = jiffies + COMPUTE_TO;
  138. }
  139. }
  140. INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
  141. }
  142. INCR(da->st_rbf.h_rb, ATH_DYN_BUF);
  143. } else {
  144. INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
  145. }
  146. }
  147. rcu_read_unlock();
  148. }
  149. /**
  150. * ath_dynack_sample_tx_ts - status timestamp sampling method
  151. * @ah: ath hw
  152. * @skb: socket buffer
  153. * @ts: tx status info
  154. * @sta: station pointer
  155. *
  156. */
  157. void ath_dynack_sample_tx_ts(struct ath_hw *ah, struct sk_buff *skb,
  158. struct ath_tx_status *ts,
  159. struct ieee80211_sta *sta)
  160. {
  161. u8 ridx;
  162. struct ieee80211_hdr *hdr;
  163. struct ath_dynack *da = &ah->dynack;
  164. struct ath_common *common = ath9k_hw_common(ah);
  165. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  166. if (!da->enabled || (info->flags & IEEE80211_TX_CTL_NO_ACK))
  167. return;
  168. spin_lock_bh(&da->qlock);
  169. hdr = (struct ieee80211_hdr *)skb->data;
  170. /* late ACK */
  171. if (ts->ts_status & ATH9K_TXERR_XRETRY) {
  172. if (ieee80211_is_assoc_req(hdr->frame_control) ||
  173. ieee80211_is_assoc_resp(hdr->frame_control) ||
  174. ieee80211_is_auth(hdr->frame_control)) {
  175. ath_dbg(common, DYNACK, "late ack\n");
  176. ath9k_hw_setslottime(ah, (LATEACK_TO - 3) / 2);
  177. ath9k_hw_set_ack_timeout(ah, LATEACK_TO);
  178. ath9k_hw_set_cts_timeout(ah, LATEACK_TO);
  179. if (sta) {
  180. struct ath_node *an;
  181. an = (struct ath_node *)sta->drv_priv;
  182. an->ackto = -1;
  183. }
  184. da->lto = jiffies + LATEACK_DELAY;
  185. }
  186. spin_unlock_bh(&da->qlock);
  187. return;
  188. }
  189. ridx = ts->ts_rateindex;
  190. da->st_rbf.ts[da->st_rbf.t_rb].tstamp = ts->ts_tstamp;
  191. da->st_rbf.ts[da->st_rbf.t_rb].dur = ts->duration;
  192. ether_addr_copy(da->st_rbf.addr[da->st_rbf.t_rb].h_dest, hdr->addr1);
  193. ether_addr_copy(da->st_rbf.addr[da->st_rbf.t_rb].h_src, hdr->addr2);
  194. if (!(info->status.rates[ridx].flags & IEEE80211_TX_RC_MCS)) {
  195. u32 phy, sifs;
  196. const struct ieee80211_rate *rate;
  197. struct ieee80211_tx_rate *rates = info->status.rates;
  198. rate = &common->sbands[info->band].bitrates[rates[ridx].idx];
  199. if (info->band == NL80211_BAND_2GHZ &&
  200. !(rate->flags & IEEE80211_RATE_ERP_G))
  201. phy = WLAN_RC_PHY_CCK;
  202. else
  203. phy = WLAN_RC_PHY_OFDM;
  204. sifs = ath_dynack_get_sifs(ah, phy);
  205. da->st_rbf.ts[da->st_rbf.t_rb].dur -= sifs;
  206. }
  207. ath_dbg(common, DYNACK, "{%pM} tx sample %u [dur %u][h %u-t %u]\n",
  208. hdr->addr1, da->st_rbf.ts[da->st_rbf.t_rb].tstamp,
  209. da->st_rbf.ts[da->st_rbf.t_rb].dur, da->st_rbf.h_rb,
  210. (da->st_rbf.t_rb + 1) % ATH_DYN_BUF);
  211. INCR(da->st_rbf.t_rb, ATH_DYN_BUF);
  212. if (da->st_rbf.t_rb == da->st_rbf.h_rb)
  213. INCR(da->st_rbf.h_rb, ATH_DYN_BUF);
  214. ath_dynack_compute_to(ah);
  215. spin_unlock_bh(&da->qlock);
  216. }
  217. EXPORT_SYMBOL(ath_dynack_sample_tx_ts);
  218. /**
  219. * ath_dynack_sample_ack_ts - ACK timestamp sampling method
  220. * @ah: ath hw
  221. * @skb: socket buffer
  222. * @ts: rx timestamp
  223. *
  224. */
  225. void ath_dynack_sample_ack_ts(struct ath_hw *ah, struct sk_buff *skb,
  226. u32 ts)
  227. {
  228. struct ath_dynack *da = &ah->dynack;
  229. struct ath_common *common = ath9k_hw_common(ah);
  230. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  231. if (!da->enabled || !ath_dynack_bssidmask(ah, hdr->addr1))
  232. return;
  233. spin_lock_bh(&da->qlock);
  234. da->ack_rbf.tstamp[da->ack_rbf.t_rb] = ts;
  235. ath_dbg(common, DYNACK, "rx sample %u [h %u-t %u]\n",
  236. da->ack_rbf.tstamp[da->ack_rbf.t_rb],
  237. da->ack_rbf.h_rb, (da->ack_rbf.t_rb + 1) % ATH_DYN_BUF);
  238. INCR(da->ack_rbf.t_rb, ATH_DYN_BUF);
  239. if (da->ack_rbf.t_rb == da->ack_rbf.h_rb)
  240. INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
  241. ath_dynack_compute_to(ah);
  242. spin_unlock_bh(&da->qlock);
  243. }
  244. EXPORT_SYMBOL(ath_dynack_sample_ack_ts);
  245. /**
  246. * ath_dynack_node_init - init ath_node related info
  247. * @ah: ath hw
  248. * @an: ath node
  249. *
  250. */
  251. void ath_dynack_node_init(struct ath_hw *ah, struct ath_node *an)
  252. {
  253. /* ackto = slottime + sifs + air delay */
  254. u32 ackto = 9 + 16 + 64;
  255. struct ath_dynack *da = &ah->dynack;
  256. an->ackto = ackto;
  257. spin_lock_bh(&da->qlock);
  258. list_add_tail(&an->list, &da->nodes);
  259. spin_unlock_bh(&da->qlock);
  260. }
  261. EXPORT_SYMBOL(ath_dynack_node_init);
  262. /**
  263. * ath_dynack_node_deinit - deinit ath_node related info
  264. * @ah: ath hw
  265. * @an: ath node
  266. *
  267. */
  268. void ath_dynack_node_deinit(struct ath_hw *ah, struct ath_node *an)
  269. {
  270. struct ath_dynack *da = &ah->dynack;
  271. spin_lock_bh(&da->qlock);
  272. list_del(&an->list);
  273. spin_unlock_bh(&da->qlock);
  274. }
  275. EXPORT_SYMBOL(ath_dynack_node_deinit);
  276. /**
  277. * ath_dynack_reset - reset dynack processing
  278. * @ah: ath hw
  279. *
  280. */
  281. void ath_dynack_reset(struct ath_hw *ah)
  282. {
  283. /* ackto = slottime + sifs + air delay */
  284. u32 ackto = 9 + 16 + 64;
  285. struct ath_dynack *da = &ah->dynack;
  286. da->lto = jiffies;
  287. da->ackto = ackto;
  288. da->st_rbf.t_rb = 0;
  289. da->st_rbf.h_rb = 0;
  290. da->ack_rbf.t_rb = 0;
  291. da->ack_rbf.h_rb = 0;
  292. /* init acktimeout */
  293. ath9k_hw_setslottime(ah, (ackto - 3) / 2);
  294. ath9k_hw_set_ack_timeout(ah, ackto);
  295. ath9k_hw_set_cts_timeout(ah, ackto);
  296. }
  297. EXPORT_SYMBOL(ath_dynack_reset);
  298. /**
  299. * ath_dynack_init - init dynack data structure
  300. * @ah: ath hw
  301. *
  302. */
  303. void ath_dynack_init(struct ath_hw *ah)
  304. {
  305. struct ath_dynack *da = &ah->dynack;
  306. memset(da, 0, sizeof(struct ath_dynack));
  307. spin_lock_init(&da->qlock);
  308. INIT_LIST_HEAD(&da->nodes);
  309. ah->hw->wiphy->features |= NL80211_FEATURE_ACKTO_ESTIMATION;
  310. }