GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / net / wireless / ath / ath9k / dynack.c
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 "ath9k.h"
18 #include "hw.h"
19 #include "dynack.h"
20
21 #define COMPUTE_TO              (5 * HZ)
22 #define LATEACK_DELAY           (10 * HZ)
23 #define LATEACK_TO              256
24 #define MAX_DELAY               300
25 #define EWMA_LEVEL              96
26 #define EWMA_DIV                128
27
28 /**
29  * ath_dynack_ewma - EWMA (Exponentially Weighted Moving Average) calculation
30  *
31  */
32 static inline int ath_dynack_ewma(int old, int new)
33 {
34         if (old > 0)
35                 return (new * (EWMA_DIV - EWMA_LEVEL) +
36                         old * EWMA_LEVEL) / EWMA_DIV;
37         else
38                 return new;
39 }
40
41 /**
42  * ath_dynack_get_sifs - get sifs time based on phy used
43  * @ah: ath hw
44  * @phy: phy used
45  *
46  */
47 static inline u32 ath_dynack_get_sifs(struct ath_hw *ah, int phy)
48 {
49         u32 sifs = CCK_SIFS_TIME;
50
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;
56                 else
57                         sifs = OFDM_SIFS_TIME;
58         }
59         return sifs;
60 }
61
62 /**
63  * ath_dynack_bssidmask - filter out ACK frames based on BSSID mask
64  * @ah: ath hw
65  * @mac: receiver address
66  */
67 static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac)
68 {
69         int i;
70         struct ath_common *common = ath9k_hw_common(ah);
71
72         for (i = 0; i < ETH_ALEN; i++) {
73                 if ((common->macaddr[i] & common->bssidmask[i]) !=
74                     (mac[i] & common->bssidmask[i]))
75                         return false;
76         }
77
78         return true;
79 }
80
81 /**
82  * ath_dynack_compute_ackto - compute ACK timeout as the maximum STA timeout
83  * @ah: ath hw
84  *
85  * should be called while holding qlock
86  */
87 static void ath_dynack_compute_ackto(struct ath_hw *ah)
88 {
89         struct ath_common *common = ath9k_hw_common(ah);
90         struct ath_dynack *da = &ah->dynack;
91         struct ath_node *an;
92         int to = 0;
93
94         list_for_each_entry(an, &da->nodes, list)
95                 if (an->ackto > to)
96                         to = an->ackto;
97
98         if (to && da->ackto != to) {
99                 u32 slottime;
100
101                 slottime = (to - 3) / 2;
102                 da->ackto = to;
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);
108         }
109 }
110
111 /**
112  * ath_dynack_compute_to - compute STA ACK timeout
113  * @ah: ath hw
114  *
115  * should be called while holding qlock
116  */
117 static void ath_dynack_compute_to(struct ath_hw *ah)
118 {
119         u32 ackto, ack_ts;
120         u8 *dst, *src;
121         struct ieee80211_sta *sta;
122         struct ath_node *an;
123         struct ts_info *st_ts;
124         struct ath_dynack *da = &ah->dynack;
125
126         rcu_read_lock();
127
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;
134
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);
139
140                 if (ack_ts > st_ts->tstamp + st_ts->dur) {
141                         ackto = ack_ts - st_ts->tstamp - st_ts->dur;
142
143                         if (ackto < MAX_DELAY) {
144                                 sta = ieee80211_find_sta_by_ifaddr(ah->hw, dst,
145                                                                    src);
146                                 if (sta) {
147                                         an = (struct ath_node *)sta->drv_priv;
148                                         an->ackto = ath_dynack_ewma(an->ackto,
149                                                                     ackto);
150                                         ath_dbg(ath9k_hw_common(ah), DYNACK,
151                                                 "%pM to %d [%u]\n", dst,
152                                                 an->ackto, ackto);
153                                         if (time_is_before_jiffies(da->lto)) {
154                                                 ath_dynack_compute_ackto(ah);
155                                                 da->lto = jiffies + COMPUTE_TO;
156                                         }
157                                 }
158                                 INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
159                         }
160                         INCR(da->st_rbf.h_rb, ATH_DYN_BUF);
161                 } else {
162                         INCR(da->ack_rbf.h_rb, ATH_DYN_BUF);
163                 }
164         }
165
166         rcu_read_unlock();
167 }
168
169 /**
170  * ath_dynack_sample_tx_ts - status timestamp sampling method
171  * @ah: ath hw
172  * @skb: socket buffer
173  * @ts: tx status info
174  * @sta: station pointer
175  *
176  */
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)
180 {
181         u8 ridx;
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);
186
187         if (!da->enabled || (info->flags & IEEE80211_TX_CTL_NO_ACK))
188                 return;
189
190         spin_lock_bh(&da->qlock);
191
192         hdr = (struct ieee80211_hdr *)skb->data;
193
194         /* late ACK */
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");
200
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);
204                         if (sta) {
205                                 struct ath_node *an;
206
207                                 an = (struct ath_node *)sta->drv_priv;
208                                 an->ackto = -1;
209                         }
210                         da->lto = jiffies + LATEACK_DELAY;
211                 }
212
213                 spin_unlock_bh(&da->qlock);
214                 return;
215         }
216
217         ridx = ts->ts_rateindex;
218
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);
223
224         if (!(info->status.rates[ridx].flags & IEEE80211_TX_RC_MCS)) {
225                 u32 phy, sifs;
226                 const struct ieee80211_rate *rate;
227                 struct ieee80211_tx_rate *rates = info->status.rates;
228
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;
233                 else
234                         phy = WLAN_RC_PHY_OFDM;
235
236                 sifs = ath_dynack_get_sifs(ah, phy);
237                 da->st_rbf.ts[da->st_rbf.t_rb].dur -= sifs;
238         }
239
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);
244
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);
248
249         ath_dynack_compute_to(ah);
250
251         spin_unlock_bh(&da->qlock);
252 }
253 EXPORT_SYMBOL(ath_dynack_sample_tx_ts);
254
255 /**
256  * ath_dynack_sample_ack_ts - ACK timestamp sampling method
257  * @ah: ath hw
258  * @skb: socket buffer
259  * @ts: rx timestamp
260  *
261  */
262 void ath_dynack_sample_ack_ts(struct ath_hw *ah, struct sk_buff *skb,
263                               u32 ts)
264 {
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;
268
269         if (!da->enabled || !ath_dynack_bssidmask(ah, hdr->addr1))
270                 return;
271
272         spin_lock_bh(&da->qlock);
273         da->ack_rbf.tstamp[da->ack_rbf.t_rb] = ts;
274
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);
278
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);
282
283         ath_dynack_compute_to(ah);
284
285         spin_unlock_bh(&da->qlock);
286 }
287 EXPORT_SYMBOL(ath_dynack_sample_ack_ts);
288
289 /**
290  * ath_dynack_node_init - init ath_node related info
291  * @ah: ath hw
292  * @an: ath node
293  *
294  */
295 void ath_dynack_node_init(struct ath_hw *ah, struct ath_node *an)
296 {
297         /* ackto = slottime + sifs + air delay */
298         u32 ackto = 9 + 16 + 64;
299         struct ath_dynack *da = &ah->dynack;
300
301         an->ackto = ackto;
302
303         spin_lock_bh(&da->qlock);
304         list_add_tail(&an->list, &da->nodes);
305         spin_unlock_bh(&da->qlock);
306 }
307 EXPORT_SYMBOL(ath_dynack_node_init);
308
309 /**
310  * ath_dynack_node_deinit - deinit ath_node related info
311  * @ah: ath hw
312  * @an: ath node
313  *
314  */
315 void ath_dynack_node_deinit(struct ath_hw *ah, struct ath_node *an)
316 {
317         struct ath_dynack *da = &ah->dynack;
318
319         spin_lock_bh(&da->qlock);
320         list_del(&an->list);
321         spin_unlock_bh(&da->qlock);
322 }
323 EXPORT_SYMBOL(ath_dynack_node_deinit);
324
325 /**
326  * ath_dynack_reset - reset dynack processing
327  * @ah: ath hw
328  *
329  */
330 void ath_dynack_reset(struct ath_hw *ah)
331 {
332         /* ackto = slottime + sifs + air delay */
333         u32 ackto = 9 + 16 + 64;
334         struct ath_dynack *da = &ah->dynack;
335
336         da->lto = jiffies;
337         da->ackto = ackto;
338
339         da->st_rbf.t_rb = 0;
340         da->st_rbf.h_rb = 0;
341         da->ack_rbf.t_rb = 0;
342         da->ack_rbf.h_rb = 0;
343
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);
348 }
349 EXPORT_SYMBOL(ath_dynack_reset);
350
351 /**
352  * ath_dynack_init - init dynack data structure
353  * @ah: ath hw
354  *
355  */
356 void ath_dynack_init(struct ath_hw *ah)
357 {
358         struct ath_dynack *da = &ah->dynack;
359
360         memset(da, 0, sizeof(struct ath_dynack));
361
362         spin_lock_init(&da->qlock);
363         INIT_LIST_HEAD(&da->nodes);
364
365         ah->hw->wiphy->features |= NL80211_FEATURE_ACKTO_ESTIMATION;
366 }