mac80211: increase minstrel_ht precision by properly using a-mpdu frame stats
[openwrt.git] / package / mac80211 / patches / 560-minstrel_ht.patch
1 --- a/net/mac80211/Makefile
2 +++ b/net/mac80211/Makefile
3 @@ -47,8 +47,8 @@ CFLAGS_driver-trace.o := -I$(src)
4  rc80211_pid-y := rc80211_pid_algo.o
5  rc80211_pid-$(CONFIG_MAC80211_DEBUGFS) += rc80211_pid_debugfs.o
6  
7 -rc80211_minstrel-y := rc80211_minstrel.o
8 -rc80211_minstrel-$(CONFIG_MAC80211_DEBUGFS) += rc80211_minstrel_debugfs.o
9 +rc80211_minstrel-y := rc80211_minstrel.o rc80211_minstrel_ht.o
10 +rc80211_minstrel-$(CONFIG_MAC80211_DEBUGFS) += rc80211_minstrel_debugfs.o rc80211_minstrel_ht_debugfs.o
11  
12  mac80211-$(CONFIG_MAC80211_RC_PID) += $(rc80211_pid-y)
13  mac80211-$(CONFIG_MAC80211_RC_MINSTREL) += $(rc80211_minstrel-y)
14 --- a/net/mac80211/main.c
15 +++ b/net/mac80211/main.c
16 @@ -714,6 +714,10 @@ static int __init ieee80211_init(void)
17         if (ret)
18                 return ret;
19  
20 +       ret = rc80211_minstrel_ht_init();
21 +       if (ret)
22 +               goto err_minstrel;
23 +
24         ret = rc80211_pid_init();
25         if (ret)
26                 goto err_pid;
27 @@ -726,6 +730,8 @@ static int __init ieee80211_init(void)
28   err_netdev:
29         rc80211_pid_exit();
30   err_pid:
31 +       rc80211_minstrel_ht_exit();
32 + err_minstrel:
33         rc80211_minstrel_exit();
34  
35         return ret;
36 @@ -734,6 +740,7 @@ static int __init ieee80211_init(void)
37  static void __exit ieee80211_exit(void)
38  {
39         rc80211_pid_exit();
40 +       rc80211_minstrel_ht_exit();
41         rc80211_minstrel_exit();
42  
43         /*
44 --- a/net/mac80211/rate.h
45 +++ b/net/mac80211/rate.h
46 @@ -137,6 +137,8 @@ static inline void rc80211_pid_exit(void
47  #ifdef CONFIG_MAC80211_RC_MINSTREL
48  extern int rc80211_minstrel_init(void);
49  extern void rc80211_minstrel_exit(void);
50 +extern int rc80211_minstrel_ht_init(void);
51 +extern void rc80211_minstrel_ht_exit(void);
52  #else
53  static inline int rc80211_minstrel_init(void)
54  {
55 @@ -145,6 +147,13 @@ static inline int rc80211_minstrel_init(
56  static inline void rc80211_minstrel_exit(void)
57  {
58  }
59 +static inline int rc80211_minstrel_ht_init(void)
60 +{
61 +       return 0;
62 +}
63 +static inline void rc80211_minstrel_ht_exit(void)
64 +{
65 +}
66  #endif
67  
68  
69 --- /dev/null
70 +++ b/net/mac80211/rc80211_minstrel_ht.c
71 @@ -0,0 +1,824 @@
72 +/*
73 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
74 + *
75 + * This program is free software; you can redistribute it and/or modify
76 + * it under the terms of the GNU General Public License version 2 as
77 + * published by the Free Software Foundation.
78 + */
79 +#include <linux/netdevice.h>
80 +#include <linux/types.h>
81 +#include <linux/skbuff.h>
82 +#include <linux/debugfs.h>
83 +#include <linux/random.h>
84 +#include <linux/ieee80211.h>
85 +#include <net/mac80211.h>
86 +#include "rate.h"
87 +#include "rc80211_minstrel.h"
88 +#include "rc80211_minstrel_ht.h"
89 +
90 +#define AVG_PKT_SIZE   1200
91 +#define SAMPLE_COLUMNS 10
92 +#define EWMA_LEVEL             75
93 +
94 +/* Number of bits for an average sized packet */
95 +#define MCS_NBITS (AVG_PKT_SIZE << 3)
96 +
97 +/* Number of symbols for a packet with (bps) bits per symbol */
98 +#define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
99 +
100 +/* Transmission time for a packet containing (syms) symbols */
101 +#define MCS_SYMBOL_TIME(sgi, syms)                                     \
102 +       (sgi ?                                                          \
103 +         ((syms) * 18 + 4) / 5 :       /* syms * 3.6 us */             \
104 +         (syms) << 2                   /* syms * 4 us */               \
105 +       )
106 +
107 +/* Transmit duration for the raw data part of an average sized packet */
108 +#define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
109 +
110 +/* MCS rate information for an MCS group */
111 +#define MCS_GROUP(_streams, _sgi, _ht40) {                             \
112 +       .streams = _streams,                                            \
113 +       .flags =                                                        \
114 +               (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |                 \
115 +               (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),             \
116 +       .duration = {                                                   \
117 +               MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26),          \
118 +               MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52),         \
119 +               MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78),         \
120 +               MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104),        \
121 +               MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156),        \
122 +               MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208),        \
123 +               MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234),        \
124 +               MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260)         \
125 +       }                                                               \
126 +}
127 +
128 +#define MINSTREL_INTFL_SAMPLE_SLOT0    BIT(30)
129 +#define MINSTREL_INTFL_SAMPLE_SLOT1    BIT(31)
130 +
131 +/*
132 + * To enable sufficiently targeted rate sampling, MCS rates are divided into
133 + * groups, based on the number of streams and flags (HT40, SGI) that they
134 + * use.
135 + */
136 +const struct mcs_group minstrel_mcs_groups[] = {
137 +       MCS_GROUP(1, 0, 0),
138 +       MCS_GROUP(2, 0, 0),
139 +#if MINSTREL_MAX_STREAMS >= 3
140 +       MCS_GROUP(3, 0, 0),
141 +#endif
142 +
143 +       MCS_GROUP(1, 1, 0),
144 +       MCS_GROUP(2, 1, 0),
145 +#if MINSTREL_MAX_STREAMS >= 3
146 +       MCS_GROUP(3, 1, 0),
147 +#endif
148 +
149 +       MCS_GROUP(1, 0, 1),
150 +       MCS_GROUP(2, 0, 1),
151 +#if MINSTREL_MAX_STREAMS >= 3
152 +       MCS_GROUP(3, 0, 1),
153 +#endif
154 +
155 +       MCS_GROUP(1, 1, 1),
156 +       MCS_GROUP(2, 1, 1),
157 +#if MINSTREL_MAX_STREAMS >= 3
158 +       MCS_GROUP(3, 1, 1),
159 +#endif
160 +};
161 +
162 +static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
163 +
164 +/*
165 + * Perform EWMA (Exponentially Weighted Moving Average) calculation
166 + */
167 +static int
168 +minstrel_ewma(int old, int new, int weight)
169 +{
170 +       return (new * (100 - weight) + old * weight) / 100;
171 +}
172 +
173 +/*
174 + * Look up an MCS group index based on mac80211 rate information
175 + */
176 +static int
177 +minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
178 +{
179 +       int streams = (rate->idx / MCS_GROUP_RATES) + 1;
180 +       u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;
181 +       int i;
182 +
183 +       for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
184 +               if (minstrel_mcs_groups[i].streams != streams)
185 +                       continue;
186 +               if (minstrel_mcs_groups[i].flags != (rate->flags & flags))
187 +                       continue;
188 +
189 +               return i;
190 +       }
191 +
192 +       WARN_ON(1);
193 +       return 0;
194 +}
195 +
196 +static inline struct minstrel_rate_stats *
197 +minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
198 +{
199 +       return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
200 +}
201 +
202 +
203 +/*
204 + * Recalculate success probabilities and counters for a rate using EWMA
205 + */
206 +static void
207 +minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
208 +{
209 +       if (mr->attempts) {
210 +               mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
211 +               if (!mr->att_hist)
212 +                       mr->probability = mr->cur_prob;
213 +               else
214 +                       mr->probability = minstrel_ewma(mr->probability,
215 +                               mr->cur_prob, EWMA_LEVEL);
216 +               mr->att_hist += mr->attempts;
217 +               mr->succ_hist += mr->success;
218 +       }
219 +       mr->last_success = mr->success;
220 +       mr->last_attempts = mr->attempts;
221 +       mr->success = 0;
222 +       mr->attempts = 0;
223 +}
224 +
225 +/*
226 + * Calculate throughput based on the average A-MPDU length, taking into account
227 + * the expected number of retransmissions and their expected length
228 + */
229 +static void
230 +minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
231 +                    int group, int rate)
232 +{
233 +       struct minstrel_rate_stats *mr;
234 +       unsigned int usecs;
235 +
236 +       mr = &mi->groups[group].rates[rate];
237 +
238 +       if (mr->probability < MINSTREL_FRAC(1, 10)) {
239 +               mr->cur_tp = 0;
240 +               return;
241 +       }
242 +
243 +       usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
244 +       usecs += minstrel_mcs_groups[group].duration[rate];
245 +       mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
246 +}
247 +
248 +/*
249 + * Update rate statistics and select new primary rates
250 + *
251 + * Rules for rate selection:
252 + *  - max_prob_rate must use only one stream, as a tradeoff between delivery
253 + *    probability and throughput during strong fluctuations
254 + *  - as long as the max prob rate has a probability of more than 3/4, pick
255 + *    higher throughput rates, even if the probablity is a bit lower
256 + */
257 +static void
258 +minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
259 +{
260 +       struct minstrel_mcs_group_data *mg;
261 +       struct minstrel_rate_stats *mr;
262 +       int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
263 +       int group, i, index;
264 +
265 +       mi->max_tp_rate = 0;
266 +       mi->max_tp_rate2 = 0;
267 +       mi->max_prob_rate = 0;
268 +
269 +       for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
270 +               cur_prob = 0;
271 +               cur_prob_tp = 0;
272 +               cur_tp = 0;
273 +               cur_tp2 = 0;
274 +
275 +               mg = &mi->groups[group];
276 +               if (!mg->supported)
277 +                       continue;
278 +
279 +               mg->max_tp_rate = 0;
280 +               mg->max_tp_rate2 = 0;
281 +               mg->max_prob_rate = 0;
282 +
283 +               for (i = 0; i < MCS_GROUP_RATES; i++) {
284 +                       if (!(mg->supported & BIT(i)))
285 +                               continue;
286 +
287 +                       mr = &mg->rates[i];
288 +                       mr->retry_updated = false;
289 +                       index = MCS_GROUP_RATES * group + i;
290 +                       minstrel_calc_rate_ewma(mp, mr);
291 +                       minstrel_ht_calc_tp(mp, mi, group, i);
292 +
293 +                       if (!mr->cur_tp)
294 +                               continue;
295 +
296 +                       /* ignore the lowest rate of each single-stream group */
297 +                       if (!i && minstrel_mcs_groups[group].streams == 1)
298 +                               continue;
299 +
300 +                       if ((mr->cur_tp > cur_prob_tp && mr->probability >
301 +                            MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
302 +                               mg->max_prob_rate = index;
303 +                               cur_prob = mr->probability;
304 +                       }
305 +
306 +                       if (mr->cur_tp > cur_tp) {
307 +                               swap(index, mg->max_tp_rate);
308 +                               cur_tp = mr->cur_tp;
309 +                               mr = minstrel_get_ratestats(mi, index);
310 +                       }
311 +
312 +                       if (index == mg->max_tp_rate)
313 +                               continue;
314 +
315 +                       if (mr->cur_tp > cur_tp2) {
316 +                               mg->max_tp_rate2 = index;
317 +                               cur_tp2 = mr->cur_tp;
318 +                       }
319 +               }
320 +       }
321 +
322 +       cur_prob = 0;
323 +       cur_prob_tp = 0;
324 +       cur_tp = 0;
325 +       cur_tp2 = 0;
326 +       for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
327 +               mg = &mi->groups[group];
328 +               if (!mg->supported)
329 +                       continue;
330 +
331 +               mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
332 +               if (cur_prob_tp < mr->cur_tp &&
333 +                   minstrel_mcs_groups[group].streams == 1) {
334 +                       mi->max_prob_rate = mg->max_prob_rate;
335 +                       cur_prob = mr->cur_prob;
336 +               }
337 +
338 +               mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
339 +               if (cur_tp < mr->cur_tp) {
340 +                       mi->max_tp_rate = mg->max_tp_rate;
341 +                       cur_tp = mr->cur_tp;
342 +               }
343 +
344 +               mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
345 +               if (cur_tp2 < mr->cur_tp) {
346 +                       mi->max_tp_rate2 = mg->max_tp_rate2;
347 +                       cur_tp2 = mr->cur_tp;
348 +               }
349 +       }
350 +
351 +       mi->stats_update = jiffies;
352 +}
353 +
354 +static bool
355 +minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
356 +{
357 +       if (!rate->count)
358 +               return false;
359 +
360 +       if (rate->idx < 0)
361 +               return false;
362 +
363 +       return !!(rate->flags & IEEE80211_TX_RC_MCS);
364 +}
365 +
366 +static void
367 +minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
368 +{
369 +       struct minstrel_mcs_group_data *mg;
370 +
371 +       for (;;) {
372 +               mi->sample_group++;
373 +               mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
374 +               mg = &mi->groups[mi->sample_group];
375 +
376 +               if (!mg->supported)
377 +                       continue;
378 +
379 +               if (++mg->index > MCS_GROUP_RATES) {
380 +                       mg->index = 0;
381 +                       if (++mg->column > ARRAY_SIZE(sample_table))
382 +                               mg->column = 0;
383 +               }
384 +               break;
385 +       }
386 +}
387 +
388 +static void
389 +minstrel_downgrade_rate(struct minstrel_ht_sta *mi, int *idx, int type)
390 +{
391 +       int group, orig_group;
392 +
393 +       orig_group = group = *idx / MCS_GROUP_RATES;
394 +       while (group > 0) {
395 +               group--;
396 +
397 +               if (!mi->groups[group].supported)
398 +                       continue;
399 +
400 +               if (minstrel_mcs_groups[group].streams >=
401 +                   minstrel_mcs_groups[orig_group].streams)
402 +                       continue;
403 +
404 +               switch(type) {
405 +               case 0:
406 +                       *idx = mi->groups[group].max_tp_rate;
407 +                       break;
408 +               case 1:
409 +                       *idx = mi->groups[group].max_tp_rate2;
410 +                       break;
411 +               }
412 +               break;
413 +       }
414 +}
415 +
416 +
417 +static void
418 +minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
419 +                      struct ieee80211_sta *sta, void *priv_sta,
420 +                      struct sk_buff *skb)
421 +{
422 +       struct minstrel_ht_sta_priv *msp = priv_sta;
423 +       struct minstrel_ht_sta *mi = &msp->ht;
424 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
425 +       struct ieee80211_tx_rate *ar = info->status.rates;
426 +       struct minstrel_rate_stats *rate, *rate2;
427 +       struct minstrel_priv *mp = priv;
428 +       bool last = false;
429 +       int group;
430 +       int i = 0;
431 +
432 +       if (!msp->is_ht)
433 +               return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
434 +
435 +       /* This packet was aggregated but doesn't carry status info */
436 +       if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
437 +           !(info->flags & IEEE80211_TX_STAT_AMPDU))
438 +               return;
439 +
440 +       if (!info->status.ampdu_len) {
441 +               info->status.ampdu_ack_len = 1;
442 +               info->status.ampdu_len = 1;
443 +       }
444 +
445 +       mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
446 +               MINSTREL_FRAC(info->status.ampdu_len, 1), 90);
447 +
448 +       for (i = 0; !last; i++) {
449 +               last = (i == IEEE80211_TX_MAX_RATES - 1) ||
450 +                      !minstrel_ht_txstat_valid(&ar[i + 1]);
451 +
452 +               if (!minstrel_ht_txstat_valid(&ar[i]))
453 +                       break;
454 +
455 +               if ((i == 0 && (info->flags & MINSTREL_INTFL_SAMPLE_SLOT0)) ||
456 +                   (i == 1 && (info->flags & MINSTREL_INTFL_SAMPLE_SLOT1))) {
457 +                       if (mi->sample_pending > 0)
458 +                               mi->sample_pending--;
459 +                       mi->sample_packets++;
460 +                       minstrel_next_sample_idx(mi);
461 +               }
462 +
463 +               group = minstrel_ht_get_group_idx(&ar[i]);
464 +               rate = &mi->groups[group].rates[ar[i].idx % 8];
465 +
466 +               if (last && (info->flags & IEEE80211_TX_STAT_ACK))
467 +                       rate->success += info->status.ampdu_ack_len;
468 +
469 +               rate->attempts += ar[i].count * info->status.ampdu_len;
470 +       }
471 +
472 +
473 +       /*
474 +        * check for sudden death of spatial multiplexing,
475 +        * downgrade to a lower number of streams if necessary.
476 +        */
477 +       rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
478 +       if (MINSTREL_FRAC(rate->success, rate->attempts) <
479 +           MINSTREL_FRAC(20, 100) && rate->attempts > 15)
480 +               minstrel_downgrade_rate(mi, &mi->max_tp_rate, 0);
481 +
482 +       rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
483 +       if (MINSTREL_FRAC(rate->success, rate->attempts) <
484 +           MINSTREL_FRAC(20, 100) && rate->attempts > 15)
485 +               minstrel_downgrade_rate(mi, &mi->max_tp_rate2, 1);
486 +
487 +       if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000))
488 +               minstrel_ht_update_stats(mp, mi);
489 +}
490 +
491 +static void
492 +minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
493 +                         int index)
494 +{
495 +       struct minstrel_rate_stats *mr;
496 +       const struct mcs_group *group;
497 +       unsigned int tx_time, tx_time_rtscts, tx_time_data;
498 +       unsigned int cw = mp->cw_min;
499 +       unsigned int t_slot = 9; /* FIXME */
500 +       unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
501 +
502 +       mr = minstrel_get_ratestats(mi, index);
503 +       if (mr->probability < MINSTREL_FRAC(1, 10)) {
504 +               mr->retry_count = 1;
505 +               mr->retry_count_rtscts = 1;
506 +               return;
507 +       }
508 +
509 +       mr->retry_count = 2;
510 +       mr->retry_count_rtscts = 2;
511 +       mr->retry_updated = true;
512 +
513 +       group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
514 +       tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
515 +       tx_time = 2 * (t_slot + mi->overhead + tx_time_data);
516 +       tx_time_rtscts = 2 * (t_slot + mi->overhead_rtscts + tx_time_data);
517 +       do {
518 +               cw = (cw << 1) | 1;
519 +               cw = min(cw, mp->cw_max);
520 +               tx_time += cw + t_slot + mi->overhead;
521 +               tx_time_rtscts += cw + t_slot + mi->overhead_rtscts;
522 +               if (tx_time_rtscts < mp->segment_size)
523 +                       mr->retry_count_rtscts++;
524 +       } while ((tx_time < mp->segment_size) &&
525 +                (++mr->retry_count < mp->max_retry));
526 +}
527 +
528 +
529 +static void
530 +minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
531 +                     struct ieee80211_tx_rate *rate, int index,
532 +                     struct ieee80211_tx_rate_control *txrc,
533 +                     bool sample, bool rtscts)
534 +{
535 +       const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
536 +       struct minstrel_rate_stats *mr;
537 +
538 +       mr = minstrel_get_ratestats(mi, index);
539 +       if (!mr->retry_updated)
540 +               minstrel_calc_retransmit(mp, mi, index);
541 +
542 +       if (rtscts)
543 +               rate->count = mr->retry_count_rtscts;
544 +       else
545 +               rate->count = mr->retry_count;
546 +
547 +       rate->flags = IEEE80211_TX_RC_MCS | group->flags;
548 +       if (txrc->short_preamble)
549 +               rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
550 +       if (txrc->rts || rtscts)
551 +               rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
552 +       rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
553 +}
554 +
555 +static inline int
556 +minstrel_get_duration(int index)
557 +{
558 +       const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
559 +       return group->duration[index % MCS_GROUP_RATES];
560 +}
561 +
562 +static int
563 +minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
564 +                         bool *defer)
565 +{
566 +       struct minstrel_rate_stats *mr;
567 +       struct minstrel_mcs_group_data *mg;
568 +       int sample_idx = 0;
569 +       int sample_rate;
570 +       int delta;
571 +
572 +       if (mp->has_mrr)
573 +               sample_rate = mp->lookaround_rate_mrr;
574 +       else
575 +               sample_rate = mp->lookaround_rate;
576 +
577 +       delta = (mi->total_packets * sample_rate) / 100 - mi->sample_packets;
578 +       delta -= mi->sample_pending / 2;
579 +
580 +       if (delta <= 0)
581 +               return -1;
582 +
583 +       delta -= 16;
584 +       if (delta > 1) {
585 +               /* With multi-rate retry, not every planned sample
586 +               * attempt actually gets used, due to the way the retry
587 +               * chain is set up - [max_tp,sample,prob,lowest] for
588 +               * sample_rate < max_tp.
589 +               *
590 +               * If there's too much sampling backlog and the link
591 +               * starts getting worse, minstrel would start bursting
592 +               * out lots of sampling frames, which would result
593 +               * in a large throughput loss.
594 +               */
595 +               mi->sample_packets += delta - 1;
596 +       }
597 +
598 +       mg = &mi->groups[mi->sample_group];
599 +       sample_idx = sample_table[mg->column][mg->index];
600 +       mr = &mg->rates[sample_idx];
601 +       sample_idx += mi->sample_group * MCS_GROUP_RATES;
602 +
603 +       /*
604 +        * When not using MRR, do not sample if the probability is already
605 +        * higher than 95% to avoid wasting airtime
606 +        */
607 +       if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
608 +               return -1;
609 +
610 +       if (minstrel_get_duration(sample_idx) >
611 +           minstrel_get_duration(mi->max_tp_rate)) {
612 +               /*
613 +                * Make sure that lower rates get sampled occasionally, even
614 +                * if the link is working perfectly. Some drivers such as ath9k
615 +                * severely limit aggregation size if the MRR chain contains low
616 +                * rates
617 +                *
618 +                * If the lower rate has already been tried a few times, there's
619 +                * no point in forcing it to be sampled again, so skip to the
620 +                * next sampling index after applying this one in the tx control
621 +                */
622 +               if (mr->att_hist > 15) {
623 +                       *defer = true;
624 +                       minstrel_next_sample_idx(mi);
625 +               }
626 +       }
627 +
628 +       return sample_idx;
629 +}
630 +
631 +static void
632 +minstrel_aggr_check(struct minstrel_priv *mp, struct ieee80211_sta *pubsta, struct sk_buff *skb)
633 +{
634 +       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
635 +       struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
636 +       u16 tid;
637 +
638 +       if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
639 +               return;
640 +
641 +       if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
642 +               return;
643 +
644 +       tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
645 +       if (likely(sta->ampdu_mlme.tid_state_tx[tid] != HT_AGG_STATE_IDLE))
646 +               return;
647 +
648 +       ieee80211_start_tx_ba_session(pubsta, tid);
649 +}
650 +
651 +static void
652 +minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
653 +                     struct ieee80211_tx_rate_control *txrc)
654 +{
655 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
656 +       struct ieee80211_tx_rate *ar = info->status.rates;
657 +       struct minstrel_ht_sta_priv *msp = priv_sta;
658 +       struct minstrel_ht_sta *mi = &msp->ht;
659 +       struct minstrel_priv *mp = priv;
660 +       bool sample_defer = false;
661 +       int sample_idx;
662 +
663 +       if (rate_control_send_low(sta, priv_sta, txrc))
664 +               return;
665 +
666 +       if (!msp->is_ht)
667 +               return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
668 +
669 +       minstrel_aggr_check(mp, sta, txrc->skb);
670 +
671 +       sample_idx = minstrel_get_sample_rate(mp, mi, &sample_defer);
672 +       if (sample_idx >= 0) {
673 +               if (sample_defer) {
674 +                       minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
675 +                               txrc, false, false);
676 +                       minstrel_ht_set_rate(mp, mi, &ar[1], sample_idx,
677 +                               txrc, true, true);
678 +                       info->flags |= MINSTREL_INTFL_SAMPLE_SLOT1;
679 +               } else {
680 +                       minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
681 +                               txrc, true, false);
682 +                       minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
683 +                               txrc, false, true);
684 +                       info->flags |= MINSTREL_INTFL_SAMPLE_SLOT0;
685 +               }
686 +               mi->sample_pending++;
687 +       } else {
688 +               minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
689 +                       txrc, false, false);
690 +               minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
691 +                       txrc, false, true);
692 +       }
693 +       minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate, txrc, false, true);
694 +
695 +       ar[3].count = 0;
696 +       ar[3].idx = -1;
697 +
698 +       mi->total_packets++;
699 +
700 +       /* wraparound */
701 +       if (mi->total_packets >= 100000) {
702 +               mi->total_packets = 0;
703 +               mi->sample_packets = 0;
704 +               mi->sample_pending = 0;
705 +       }
706 +}
707 +
708 +static void
709 +minstrel_ht_update_cap(struct minstrel_ht_sta *mi, struct ieee80211_sta *sta,
710 +                       enum nl80211_channel_type oper_chan_type)
711 +{
712 +       struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
713 +       u16 sta_cap = sta->ht_cap.cap;
714 +       int i;
715 +
716 +       if (oper_chan_type != NL80211_CHAN_HT40MINUS &&
717 +           oper_chan_type != NL80211_CHAN_HT40PLUS)
718 +               sta_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
719 +
720 +       for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
721 +               u16 req = 0;
722 +
723 +               mi->groups[i].supported = 0;
724 +               if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
725 +                       if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
726 +                               req |= IEEE80211_HT_CAP_SGI_40;
727 +                       else
728 +                               req |= IEEE80211_HT_CAP_SGI_20;
729 +               }
730 +
731 +               if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
732 +                       req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
733 +
734 +               if ((sta_cap & req) != req)
735 +                       continue;
736 +
737 +               mi->groups[i].supported =
738 +                       mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
739 +       }
740 +}
741 +
742 +static void
743 +minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
744 +                      struct ieee80211_sta *sta, void *priv_sta)
745 +{
746 +       struct minstrel_priv *mp = priv;
747 +       struct minstrel_ht_sta_priv *msp = priv_sta;
748 +       struct minstrel_ht_sta *mi = &msp->ht;
749 +       struct ieee80211_local *local = hw_to_local(mp->hw);
750 +       int ack_dur;
751 +
752 +       /* fall back to the old minstrel for legacy stations */
753 +       if (sta && !sta->ht_cap.ht_supported) {
754 +               msp->is_ht = false;
755 +               memset(&msp->legacy, 0, sizeof(msp->legacy));
756 +               msp->legacy.r = msp->ratelist;
757 +               msp->legacy.sample_table = msp->sample_table;
758 +               return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
759 +       }
760 +
761 +       BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
762 +               MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
763 +
764 +       msp->is_ht = true;
765 +       memset(mi, 0, sizeof(*mi));
766 +       mi->stats_update = jiffies;
767 +
768 +       ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);
769 +       mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;
770 +       mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
771 +
772 +       mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
773 +
774 +       minstrel_ht_update_cap(mi, sta, mp->hw->conf.channel_type);
775 +}
776 +
777 +static void
778 +minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
779 +                        struct ieee80211_sta *sta, void *priv_sta,
780 +                        u32 changed, enum nl80211_channel_type oper_chan_type)
781 +{
782 +       struct minstrel_ht_sta_priv *msp = priv_sta;
783 +       struct minstrel_ht_sta *mi = &msp->ht;
784 +
785 +       minstrel_ht_update_cap(mi, sta, oper_chan_type);
786 +}
787 +
788 +static void *
789 +minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
790 +{
791 +       struct ieee80211_supported_band *sband;
792 +       struct minstrel_ht_sta_priv *msp;
793 +       struct minstrel_priv *mp = priv;
794 +       struct ieee80211_hw *hw = mp->hw;
795 +       int max_rates = 0;
796 +       int i;
797 +
798 +       for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
799 +               sband = hw->wiphy->bands[i];
800 +               if (sband && sband->n_bitrates > max_rates)
801 +                       max_rates = sband->n_bitrates;
802 +       }
803 +
804 +       msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);
805 +       if (!msp)
806 +               return NULL;
807 +
808 +       msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
809 +       if (!msp->ratelist)
810 +               goto error;
811 +
812 +       msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
813 +       if (!msp->sample_table)
814 +               goto error1;
815 +
816 +       return msp;
817 +
818 +error1:
819 +       kfree(msp->sample_table);
820 +error:
821 +       kfree(msp);
822 +       return NULL;
823 +}
824 +
825 +static void
826 +minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
827 +{
828 +       struct minstrel_ht_sta_priv *msp = priv_sta;
829 +
830 +       kfree(msp->sample_table);
831 +       kfree(msp->ratelist);
832 +       kfree(msp);
833 +}
834 +
835 +static void *
836 +minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
837 +{
838 +       return mac80211_minstrel.alloc(hw, debugfsdir);
839 +}
840 +
841 +static void
842 +minstrel_ht_free(void *priv)
843 +{
844 +       mac80211_minstrel.free(priv);
845 +}
846 +
847 +static struct rate_control_ops mac80211_minstrel_ht = {
848 +       .name = "minstrel_ht",
849 +       .tx_status = minstrel_ht_tx_status,
850 +       .get_rate = minstrel_ht_get_rate,
851 +       .rate_init = minstrel_ht_rate_init,
852 +       .rate_update = minstrel_ht_rate_update,
853 +       .alloc_sta = minstrel_ht_alloc_sta,
854 +       .free_sta = minstrel_ht_free_sta,
855 +       .alloc = minstrel_ht_alloc,
856 +       .free = minstrel_ht_free,
857 +#ifdef CONFIG_MAC80211_DEBUGFS
858 +       .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
859 +       .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
860 +#endif
861 +};
862 +
863 +
864 +static void
865 +init_sample_table(void)
866 +{
867 +       int col, i, new_idx;
868 +       u8 rnd[MCS_GROUP_RATES];
869 +
870 +       memset(sample_table, 0xff, sizeof(sample_table));
871 +       for (col = 0; col < SAMPLE_COLUMNS; col++) {
872 +               for (i = 0; i < MCS_GROUP_RATES; i++) {
873 +                       get_random_bytes(rnd, sizeof(rnd));
874 +                       new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
875 +
876 +                       while (sample_table[col][new_idx] != 0xff)
877 +                               new_idx = (new_idx + 1) % MCS_GROUP_RATES;
878 +
879 +                       sample_table[col][new_idx] = i;
880 +               }
881 +       }
882 +}
883 +
884 +int __init
885 +rc80211_minstrel_ht_init(void)
886 +{
887 +       init_sample_table();
888 +       return ieee80211_rate_control_register(&mac80211_minstrel_ht);
889 +}
890 +
891 +void
892 +rc80211_minstrel_ht_exit(void)
893 +{
894 +       ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
895 +}
896 --- /dev/null
897 +++ b/net/mac80211/rc80211_minstrel_ht.h
898 @@ -0,0 +1,115 @@
899 +/*
900 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
901 + *
902 + * This program is free software; you can redistribute it and/or modify
903 + * it under the terms of the GNU General Public License version 2 as
904 + * published by the Free Software Foundation.
905 + */
906 +
907 +#ifndef __RC_MINSTREL_HT_H
908 +#define __RC_MINSTREL_HT_H
909 +
910 +/*
911 + * maximum number of spatial streams to make use of
912 + * set this value to 3 once we have drivers that support it
913 + */
914 +#define MINSTREL_MAX_STREAMS   2
915 +#define MINSTREL_STREAM_GROUPS 4
916 +
917 +/* scaled fraction values */
918 +#define MINSTREL_SCALE 16
919 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
920 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
921 +
922 +#define MCS_GROUP_RATES        8
923 +
924 +struct mcs_group {
925 +       u32 flags;
926 +       unsigned int streams;
927 +       unsigned int duration[MCS_GROUP_RATES];
928 +};
929 +
930 +struct minstrel_rate_stats {
931 +       /* current / last sampling period attempts/success counters */
932 +       unsigned int attempts, last_attempts;
933 +       unsigned int success, last_success;
934 +
935 +       /* total attempts/success counters */
936 +       u64 att_hist, succ_hist;
937 +
938 +       /* current throughput */
939 +       unsigned int cur_tp;
940 +
941 +       /* packet delivery probabilities */
942 +       unsigned int cur_prob, probability;
943 +
944 +       /* maximum retry counts */
945 +       bool retry_updated;
946 +       unsigned int retry_count;
947 +       unsigned int retry_count_rtscts;
948 +};
949 +
950 +struct minstrel_mcs_group_data {
951 +       u8 index;
952 +       u8 column;
953 +
954 +       /* bitfield of supported MCS rates of this group */
955 +       u8 supported;
956 +
957 +       /* selected primary rates */
958 +       unsigned int max_tp_rate;
959 +       unsigned int max_tp_rate2;
960 +       unsigned int max_prob_rate;
961 +
962 +       /* MCS rate statistics */
963 +       struct minstrel_rate_stats rates[MCS_GROUP_RATES];
964 +};
965 +
966 +struct minstrel_ht_sta {
967 +       /* ampdu length average (EWMA) */
968 +       unsigned int avg_ampdu_len;
969 +
970 +       /* best throughput rate */
971 +       unsigned int max_tp_rate;
972 +
973 +       /* second best throughput rate */
974 +       unsigned int max_tp_rate2;
975 +
976 +       /* best probability rate */
977 +       unsigned int max_prob_rate;
978 +
979 +       /* time of last status update */
980 +       unsigned long stats_update;
981 +
982 +       /* overhead time in usec for each frame */
983 +       unsigned int overhead;
984 +       unsigned int overhead_rtscts;
985 +
986 +       unsigned int total_packets;
987 +       unsigned int sample_packets;
988 +       unsigned int sample_pending;
989 +
990 +       /* current MCS group to be sampled */
991 +       unsigned int sample_group;
992 +
993 +       /* MCS rate group info and statistics */
994 +       struct minstrel_mcs_group_data groups[MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS];
995 +};
996 +
997 +struct minstrel_ht_sta_priv {
998 +       union {
999 +               struct minstrel_ht_sta ht;
1000 +               struct minstrel_sta_info legacy;
1001 +       };
1002 +#ifdef CONFIG_MAC80211_DEBUGFS
1003 +       struct dentry *dbg_stats;
1004 +#endif
1005 +       void *ratelist;
1006 +       void *sample_table;
1007 +       bool is_ht;
1008 +};
1009 +
1010 +void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
1011 +void minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta);
1012 +
1013 +#endif
1014 --- /dev/null
1015 +++ b/net/mac80211/rc80211_minstrel_ht_debugfs.c
1016 @@ -0,0 +1,120 @@
1017 +/*
1018 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
1019 + *
1020 + * This program is free software; you can redistribute it and/or modify
1021 + * it under the terms of the GNU General Public License version 2 as
1022 + * published by the Free Software Foundation.
1023 + */
1024 +#include <linux/netdevice.h>
1025 +#include <linux/types.h>
1026 +#include <linux/skbuff.h>
1027 +#include <linux/debugfs.h>
1028 +#include <linux/ieee80211.h>
1029 +#include <net/mac80211.h>
1030 +#include "rc80211_minstrel.h"
1031 +#include "rc80211_minstrel_ht.h"
1032 +
1033 +extern const struct mcs_group minstrel_mcs_groups[];
1034 +
1035 +static int
1036 +minstrel_ht_stats_open(struct inode *inode, struct file *file)
1037 +{
1038 +       struct minstrel_ht_sta_priv *msp = inode->i_private;
1039 +       struct minstrel_ht_sta *mi = &msp->ht;
1040 +       struct minstrel_debugfs_info *ms;
1041 +       unsigned int i, j, tp, prob, eprob;
1042 +       char *p;
1043 +       int ret;
1044 +
1045 +       if (!msp->is_ht) {
1046 +               inode->i_private = &msp->legacy;
1047 +               ret = minstrel_stats_open(inode, file);
1048 +               inode->i_private = msp;
1049 +               return ret;
1050 +       }
1051 +
1052 +       ms = kmalloc(sizeof(*ms) + 8192, GFP_KERNEL);
1053 +       if (!ms)
1054 +               return -ENOMEM;
1055 +
1056 +       file->private_data = ms;
1057 +       p = ms->buf;
1058 +       p += sprintf(p, "type      rate     throughput  ewma prob   this prob  "
1059 +                       "this succ/attempt   success    attempts\n");
1060 +       for (i = 0; i < MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS; i++) {
1061 +               char htmode = '2';
1062 +               char gimode = 'L';
1063 +
1064 +               if (!mi->groups[i].supported)
1065 +                       continue;
1066 +
1067 +               if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1068 +                       htmode = '4';
1069 +               if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI)
1070 +                       gimode = 'S';
1071 +
1072 +               for (j = 0; j < MCS_GROUP_RATES; j++) {
1073 +                       struct minstrel_rate_stats *mr = &mi->groups[i].rates[j];
1074 +                       int idx = i * MCS_GROUP_RATES + j;
1075 +
1076 +                       if (!mi->groups[i].supported & BIT(j))
1077 +                               continue;
1078 +
1079 +                       p += sprintf(p, "HT%c0/%cGI ", htmode, gimode);
1080 +
1081 +                       *(p++) = (idx == mi->max_tp_rate) ? 'T' : ' ';
1082 +                       *(p++) = (idx == mi->max_tp_rate2) ? 't' : ' ';
1083 +                       *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' ';
1084 +                       p += sprintf(p, "MCS%-2u", (minstrel_mcs_groups[i].streams - 1) *
1085 +                                       MCS_GROUP_RATES + j);
1086 +
1087 +                       tp = mr->cur_tp / 10;
1088 +                       prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
1089 +                       eprob = MINSTREL_TRUNC(mr->probability * 1000);
1090 +
1091 +                       p += sprintf(p, "  %6u.%1u   %6u.%1u   %6u.%1u        "
1092 +                                       "%3u(%3u)   %8llu    %8llu\n",
1093 +                                       tp / 10, tp % 10,
1094 +                                       eprob / 10, eprob % 10,
1095 +                                       prob / 10, prob % 10,
1096 +                                       mr->last_success,
1097 +                                       mr->last_attempts,
1098 +                                       (unsigned long long)mr->succ_hist,
1099 +                                       (unsigned long long)mr->att_hist);
1100 +               }
1101 +       }
1102 +       p += sprintf(p, "\nTotal packet count::    ideal %d      "
1103 +                       "lookaround %d\n",
1104 +                       max(0, (int) mi->total_packets - (int) mi->sample_packets),
1105 +                       mi->sample_packets);
1106 +       p += sprintf(p, "Average A-MPDU length: %d.%d\n",
1107 +               MINSTREL_TRUNC(mi->avg_ampdu_len),
1108 +               MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
1109 +       ms->len = p - ms->buf;
1110 +
1111 +       return 0;
1112 +}
1113 +
1114 +static const struct file_operations minstrel_ht_stat_fops = {
1115 +       .owner = THIS_MODULE,
1116 +       .open = minstrel_ht_stats_open,
1117 +       .read = minstrel_stats_read,
1118 +       .release = minstrel_stats_release,
1119 +};
1120 +
1121 +void
1122 +minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
1123 +{
1124 +       struct minstrel_ht_sta_priv *msp = priv_sta;
1125 +
1126 +       msp->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, msp,
1127 +                       &minstrel_ht_stat_fops);
1128 +}
1129 +
1130 +void
1131 +minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta)
1132 +{
1133 +       struct minstrel_ht_sta_priv *msp = priv_sta;
1134 +
1135 +       debugfs_remove(msp->dbg_stats);
1136 +}