dynack.c 8.8 KB

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