2 * Copyright (c) 2014, Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
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.
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.
21 #define COMPUTE_TO (5 * HZ)
22 #define LATEACK_DELAY (10 * HZ)
23 #define LATEACK_TO 256
29 * ath_dynack_ewma - EWMA (Exponentially Weighted Moving Average) calculation
32 static inline int ath_dynack_ewma(int old, int new)
35 return (new * (EWMA_DIV - EWMA_LEVEL) +
36 old * EWMA_LEVEL) / EWMA_DIV;
42 * ath_dynack_get_sifs - get sifs time based on phy used
47 static inline u32 ath_dynack_get_sifs(struct ath_hw *ah, int phy)
49 u32 sifs = CCK_SIFS_TIME;
51 if (phy == WLAN_RC_PHY_OFDM) {
52 if (IS_CHAN_QUARTER_RATE(ah->curchan))
53 sifs = OFDM_SIFS_TIME_QUARTER;
54 else if (IS_CHAN_HALF_RATE(ah->curchan))
55 sifs = OFDM_SIFS_TIME_HALF;
57 sifs = OFDM_SIFS_TIME;
63 * ath_dynack_bssidmask - filter out ACK frames based on BSSID mask
65 * @mac: receiver address
67 static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac)
70 struct ath_common *common = ath9k_hw_common(ah);
72 for (i = 0; i < ETH_ALEN; i++) {
73 if ((common->macaddr[i] & common->bssidmask[i]) !=
74 (mac[i] & common->bssidmask[i]))
82 * ath_dynack_compute_ackto - compute ACK timeout as the maximum STA timeout
85 * should be called while holding qlock
87 static void ath_dynack_compute_ackto(struct ath_hw *ah)
89 struct ath_common *common = ath9k_hw_common(ah);
90 struct ath_dynack *da = &ah->dynack;
94 list_for_each_entry(an, &da->nodes, list)
98 if (to && da->ackto != to) {
101 slottime = (to - 3) / 2;
103 ath_dbg(common, DYNACK, "ACK timeout %u slottime %u\n",
104 da->ackto, slottime);
105 ath9k_hw_setslottime(ah, slottime);
106 ath9k_hw_set_ack_timeout(ah, da->ackto);
107 ath9k_hw_set_cts_timeout(ah, da->ackto);
112 * ath_dynack_compute_to - compute STA ACK timeout
115 * should be called while holding qlock
117 static void ath_dynack_compute_to(struct ath_hw *ah)
121 struct ieee80211_sta *sta;
123 struct ts_info *st_ts;
124 struct ath_dynack *da = &ah->dynack;
128 while (da->st_rbf.h_rb != da->st_rbf.t_rb &&
129 da->ack_rbf.h_rb != da->ack_rbf.t_rb) {
130 ack_ts = da->ack_rbf.tstamp[da->ack_rbf.h_rb];
131 st_ts = &da->st_rbf.ts[da->st_rbf.h_rb];
132 dst = da->st_rbf.addr[da->st_rbf.h_rb].h_dest;
133 src = da->st_rbf.addr[da->st_rbf.h_rb].h_src;
135 ath_dbg(ath9k_hw_common(ah), DYNACK,
136 "ack_ts %u st_ts %u st_dur %u [%u-%u]\n",
137 ack_ts, st_ts->tstamp, st_ts->dur,
138 da->ack_rbf.h_rb, da->st_rbf.h_rb);
140 if (ack_ts > st_ts->tstamp + st_ts->dur) {
141 ackto = ack_ts - st_ts->tstamp - st_ts->dur;
143 if (ackto < MAX_DELAY) {
144 sta = ieee80211_find_sta_by_ifaddr(ah->hw, dst,
147 an = (struct ath_node *)sta->drv_priv;
148 an->ackto = ath_dynack_ewma(an->ackto,
150 ath_dbg(ath9k_hw_common(ah), DYNACK,
151 "%pM to %d [%u]\n", dst,
153 if (time_is_before_jiffies(da->lto)) {
154 ath_dynack_compute_ackto(ah);
155 da->lto = jiffies + COMPUTE_TO;
158 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
160 INCR(da->st_rbf.h_rb, ATH_DYN_BUF);
162 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
170 * ath_dynack_sample_tx_ts - status timestamp sampling method
172 * @skb: socket buffer
173 * @ts: tx status info
174 * @sta: station pointer
177 void ath_dynack_sample_tx_ts(struct ath_hw *ah, struct sk_buff *skb,
178 struct ath_tx_status *ts,
179 struct ieee80211_sta *sta)
182 struct ieee80211_hdr *hdr;
183 struct ath_dynack *da = &ah->dynack;
184 struct ath_common *common = ath9k_hw_common(ah);
185 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
187 if (!da->enabled || (info->flags & IEEE80211_TX_CTL_NO_ACK))
190 spin_lock_bh(&da->qlock);
192 hdr = (struct ieee80211_hdr *)skb->data;
195 if (ts->ts_status & ATH9K_TXERR_XRETRY) {
196 if (ieee80211_is_assoc_req(hdr->frame_control) ||
197 ieee80211_is_assoc_resp(hdr->frame_control) ||
198 ieee80211_is_auth(hdr->frame_control)) {
199 ath_dbg(common, DYNACK, "late ack\n");
201 ath9k_hw_setslottime(ah, (LATEACK_TO - 3) / 2);
202 ath9k_hw_set_ack_timeout(ah, LATEACK_TO);
203 ath9k_hw_set_cts_timeout(ah, LATEACK_TO);
207 an = (struct ath_node *)sta->drv_priv;
210 da->lto = jiffies + LATEACK_DELAY;
213 spin_unlock_bh(&da->qlock);
217 ridx = ts->ts_rateindex;
219 da->st_rbf.ts[da->st_rbf.t_rb].tstamp = ts->ts_tstamp;
220 da->st_rbf.ts[da->st_rbf.t_rb].dur = ts->duration;
221 ether_addr_copy(da->st_rbf.addr[da->st_rbf.t_rb].h_dest, hdr->addr1);
222 ether_addr_copy(da->st_rbf.addr[da->st_rbf.t_rb].h_src, hdr->addr2);
224 if (!(info->status.rates[ridx].flags & IEEE80211_TX_RC_MCS)) {
226 const struct ieee80211_rate *rate;
227 struct ieee80211_tx_rate *rates = info->status.rates;
229 rate = &common->sbands[info->band].bitrates[rates[ridx].idx];
230 if (info->band == NL80211_BAND_2GHZ &&
231 !(rate->flags & IEEE80211_RATE_ERP_G))
232 phy = WLAN_RC_PHY_CCK;
234 phy = WLAN_RC_PHY_OFDM;
236 sifs = ath_dynack_get_sifs(ah, phy);
237 da->st_rbf.ts[da->st_rbf.t_rb].dur -= sifs;
240 ath_dbg(common, DYNACK, "{%pM} tx sample %u [dur %u][h %u-t %u]\n",
241 hdr->addr1, da->st_rbf.ts[da->st_rbf.t_rb].tstamp,
242 da->st_rbf.ts[da->st_rbf.t_rb].dur, da->st_rbf.h_rb,
243 (da->st_rbf.t_rb + 1) % ATH_DYN_BUF);
245 INCR(da->st_rbf.t_rb, ATH_DYN_BUF);
246 if (da->st_rbf.t_rb == da->st_rbf.h_rb)
247 INCR(da->st_rbf.h_rb, ATH_DYN_BUF);
249 ath_dynack_compute_to(ah);
251 spin_unlock_bh(&da->qlock);
253 EXPORT_SYMBOL(ath_dynack_sample_tx_ts);
256 * ath_dynack_sample_ack_ts - ACK timestamp sampling method
258 * @skb: socket buffer
262 void ath_dynack_sample_ack_ts(struct ath_hw *ah, struct sk_buff *skb,
265 struct ath_dynack *da = &ah->dynack;
266 struct ath_common *common = ath9k_hw_common(ah);
267 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
269 if (!da->enabled || !ath_dynack_bssidmask(ah, hdr->addr1))
272 spin_lock_bh(&da->qlock);
273 da->ack_rbf.tstamp[da->ack_rbf.t_rb] = ts;
275 ath_dbg(common, DYNACK, "rx sample %u [h %u-t %u]\n",
276 da->ack_rbf.tstamp[da->ack_rbf.t_rb],
277 da->ack_rbf.h_rb, (da->ack_rbf.t_rb + 1) % ATH_DYN_BUF);
279 INCR(da->ack_rbf.t_rb, ATH_DYN_BUF);
280 if (da->ack_rbf.t_rb == da->ack_rbf.h_rb)
281 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
283 ath_dynack_compute_to(ah);
285 spin_unlock_bh(&da->qlock);
287 EXPORT_SYMBOL(ath_dynack_sample_ack_ts);
290 * ath_dynack_node_init - init ath_node related info
295 void ath_dynack_node_init(struct ath_hw *ah, struct ath_node *an)
297 /* ackto = slottime + sifs + air delay */
298 u32 ackto = 9 + 16 + 64;
299 struct ath_dynack *da = &ah->dynack;
303 spin_lock_bh(&da->qlock);
304 list_add_tail(&an->list, &da->nodes);
305 spin_unlock_bh(&da->qlock);
307 EXPORT_SYMBOL(ath_dynack_node_init);
310 * ath_dynack_node_deinit - deinit ath_node related info
315 void ath_dynack_node_deinit(struct ath_hw *ah, struct ath_node *an)
317 struct ath_dynack *da = &ah->dynack;
319 spin_lock_bh(&da->qlock);
321 spin_unlock_bh(&da->qlock);
323 EXPORT_SYMBOL(ath_dynack_node_deinit);
326 * ath_dynack_reset - reset dynack processing
330 void ath_dynack_reset(struct ath_hw *ah)
332 /* ackto = slottime + sifs + air delay */
333 u32 ackto = 9 + 16 + 64;
334 struct ath_dynack *da = &ah->dynack;
341 da->ack_rbf.t_rb = 0;
342 da->ack_rbf.h_rb = 0;
344 /* init acktimeout */
345 ath9k_hw_setslottime(ah, (ackto - 3) / 2);
346 ath9k_hw_set_ack_timeout(ah, ackto);
347 ath9k_hw_set_cts_timeout(ah, ackto);
349 EXPORT_SYMBOL(ath_dynack_reset);
352 * ath_dynack_init - init dynack data structure
356 void ath_dynack_init(struct ath_hw *ah)
358 struct ath_dynack *da = &ah->dynack;
360 memset(da, 0, sizeof(struct ath_dynack));
362 spin_lock_init(&da->qlock);
363 INIT_LIST_HEAD(&da->nodes);
365 ah->hw->wiphy->features |= NL80211_FEATURE_ACKTO_ESTIMATION;