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