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
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
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 @@ -710,6 +710,10 @@ static int __init ieee80211_init(void)
20 + ret = rc80211_minstrel_ht_init();
24 ret = rc80211_pid_init();
27 @@ -722,6 +726,8 @@ static int __init ieee80211_init(void)
31 + rc80211_minstrel_ht_exit();
33 rc80211_minstrel_exit();
36 @@ -730,6 +736,7 @@ static int __init ieee80211_init(void)
37 static void __exit ieee80211_exit(void)
40 + rc80211_minstrel_ht_exit();
41 rc80211_minstrel_exit();
44 --- a/net/mac80211/rate.h
45 +++ b/net/mac80211/rate.h
46 @@ -136,6 +136,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);
53 static inline int rc80211_minstrel_init(void)
55 @@ -144,6 +146,13 @@ static inline int rc80211_minstrel_init(
56 static inline void rc80211_minstrel_exit(void)
59 +static inline int rc80211_minstrel_ht_init(void)
63 +static inline void rc80211_minstrel_ht_exit(void)
70 +++ b/net/mac80211/rc80211_minstrel_ht.c
73 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
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.
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>
87 +#include "rc80211_minstrel.h"
88 +#include "rc80211_minstrel_ht.h"
90 +#define AVG_PKT_SIZE 1200
91 +#define SAMPLE_COLUMNS 10
92 +#define EWMA_LEVEL 75
94 +/* Number of bits for an average sized packet */
95 +#define MCS_NBITS (AVG_PKT_SIZE << 3)
97 +/* Number of symbols for a packet with (bps) bits per symbol */
98 +#define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
100 +/* Transmission time for a packet containing (syms) symbols */
101 +#define MCS_SYMBOL_TIME(sgi, syms) \
103 + ((syms) * 18 + 4) / 5 : /* syms * 3.6 us */ \
104 + (syms) << 2 /* syms * 4 us */ \
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)))
110 +/* MCS rate information for an MCS group */
111 +#define MCS_GROUP(_streams, _sgi, _ht40) { \
112 + .streams = _streams, \
114 + (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
115 + (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
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) \
128 +#define MINSTREL_INTFL_SAMPLE_SLOT0 BIT(30)
129 +#define MINSTREL_INTFL_SAMPLE_SLOT1 BIT(31)
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
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),
143 + MCS_GROUP(1, 1, 0),
144 + MCS_GROUP(2, 1, 0),
145 +#if MINSTREL_MAX_STREAMS >= 3
146 + MCS_GROUP(3, 1, 0),
149 + MCS_GROUP(1, 0, 1),
150 + MCS_GROUP(2, 0, 1),
151 +#if MINSTREL_MAX_STREAMS >= 3
152 + MCS_GROUP(3, 0, 1),
155 + MCS_GROUP(1, 1, 1),
156 + MCS_GROUP(2, 1, 1),
157 +#if MINSTREL_MAX_STREAMS >= 3
158 + MCS_GROUP(3, 1, 1),
162 +static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
165 + * Perform EWMA (Exponentially Weighted Moving Average) calculation
168 +minstrel_ewma(int old, int new, int weight)
170 + return (new * (100 - weight) + old * weight) / 100;
174 + * Look up an MCS group index based on mac80211 rate information
177 +minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
179 + int streams = (rate->idx / MCS_GROUP_RATES) + 1;
180 + u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;
183 + for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
184 + if (minstrel_mcs_groups[i].streams != streams)
186 + if (minstrel_mcs_groups[i].flags != (rate->flags & flags))
196 +static inline struct minstrel_rate_stats *
197 +minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
199 + return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
204 + * Recalculate success probabilities and counters for a rate using EWMA
207 +minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
209 + if (mr->attempts) {
210 + mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
212 + mr->probability = mr->cur_prob;
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;
219 + mr->last_success = mr->success;
220 + mr->last_attempts = mr->attempts;
226 + * Calculate throughput based on the average A-MPDU length, taking into account
227 + * the expected number of retransmissions and their expected length
230 +minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
231 + int group, int rate)
233 + struct minstrel_rate_stats *mr;
234 + unsigned int usecs;
236 + mr = &mi->groups[group].rates[rate];
238 + if (mr->probability < MINSTREL_FRAC(1, 10)) {
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);
249 + * Update rate statistics and select new primary rates
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
258 +minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
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;
265 + mi->max_tp_rate = 0;
266 + mi->max_tp_rate2 = 0;
267 + mi->max_prob_rate = 0;
269 + for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
275 + mg = &mi->groups[group];
276 + if (!mg->supported)
279 + mg->max_tp_rate = 0;
280 + mg->max_tp_rate2 = 0;
281 + mg->max_prob_rate = 0;
283 + for (i = 0; i < MCS_GROUP_RATES; i++) {
284 + if (!(mg->supported & BIT(i)))
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);
296 + /* ignore the lowest rate of each single-stream group */
297 + if (!i && minstrel_mcs_groups[group].streams == 1)
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;
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);
312 + if (index == mg->max_tp_rate)
315 + if (mr->cur_tp > cur_tp2) {
316 + mg->max_tp_rate2 = index;
317 + cur_tp2 = mr->cur_tp;
326 + for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
327 + mg = &mi->groups[group];
328 + if (!mg->supported)
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;
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;
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;
351 + mi->stats_update = jiffies;
355 +minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
363 + return !!(rate->flags & IEEE80211_TX_RC_MCS);
367 +minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
369 + struct minstrel_mcs_group_data *mg;
372 + mi->sample_group++;
373 + mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
374 + mg = &mi->groups[mi->sample_group];
376 + if (!mg->supported)
379 + if (++mg->index > MCS_GROUP_RATES) {
381 + if (++mg->column > ARRAY_SIZE(sample_table))
389 +minstrel_downgrade_rate(struct minstrel_ht_sta *mi, int *idx, int type)
391 + int group, orig_group;
393 + orig_group = group = *idx / MCS_GROUP_RATES;
394 + while (group > 0) {
397 + if (!mi->groups[group].supported)
400 + if (minstrel_mcs_groups[group].streams >=
401 + minstrel_mcs_groups[orig_group].streams)
406 + *idx = mi->groups[group].max_tp_rate;
409 + *idx = mi->groups[group].max_tp_rate2;
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)
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;
433 + return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
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))
440 + if (!info->status.ampdu_len) {
441 + info->status.ampdu_ack_len = 1;
442 + info->status.ampdu_len = 1;
445 + mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
446 + MINSTREL_FRAC(info->status.ampdu_len, 1), 90);
448 + for (i = 0; !last; i++) {
449 + last = (i == IEEE80211_TX_MAX_RATES - 1) ||
450 + !minstrel_ht_txstat_valid(&ar[i + 1]);
452 + if (!minstrel_ht_txstat_valid(&ar[i]))
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);
463 + group = minstrel_ht_get_group_idx(&ar[i]);
464 + rate = &mi->groups[group].rates[ar[i].idx % 8];
466 + if (last && (info->flags & IEEE80211_TX_STAT_ACK) &&
467 + info->status.ampdu_len == info->status.ampdu_ack_len)
470 + rate->attempts += ar[i].count;
475 + * check for sudden death of spatial multiplexing,
476 + * downgrade to a lower number of streams if necessary.
478 + rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
479 + if (MINSTREL_FRAC(rate->success, rate->attempts) <
480 + MINSTREL_FRAC(20, 100) && rate->attempts > 15)
481 + minstrel_downgrade_rate(mi, &mi->max_tp_rate, 0);
483 + rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
484 + if (MINSTREL_FRAC(rate->success, rate->attempts) <
485 + MINSTREL_FRAC(20, 100) && rate->attempts > 15)
486 + minstrel_downgrade_rate(mi, &mi->max_tp_rate2, 1);
488 + if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000))
489 + minstrel_ht_update_stats(mp, mi);
493 +minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
496 + struct minstrel_rate_stats *mr;
497 + const struct mcs_group *group;
498 + unsigned int tx_time, tx_time_rtscts, tx_time_data;
499 + unsigned int cw = mp->cw_min;
500 + unsigned int t_slot = 9; /* FIXME */
501 + unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
503 + mr = minstrel_get_ratestats(mi, index);
504 + if (mr->probability < MINSTREL_FRAC(1, 10)) {
505 + mr->retry_count = 1;
506 + mr->retry_count_rtscts = 1;
510 + mr->retry_count = 2;
511 + mr->retry_count_rtscts = 2;
512 + mr->retry_updated = true;
514 + group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
515 + tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
516 + tx_time = 2 * (t_slot + mi->overhead + tx_time_data);
517 + tx_time_rtscts = 2 * (t_slot + mi->overhead_rtscts + tx_time_data);
519 + cw = (cw << 1) | 1;
520 + cw = min(cw, mp->cw_max);
521 + tx_time += cw + t_slot + mi->overhead;
522 + tx_time_rtscts += cw + t_slot + mi->overhead_rtscts;
523 + if (tx_time_rtscts < mp->segment_size)
524 + mr->retry_count_rtscts++;
525 + } while ((tx_time < mp->segment_size) &&
526 + (++mr->retry_count < mp->max_retry));
531 +minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
532 + struct ieee80211_tx_rate *rate, int index,
533 + struct ieee80211_tx_rate_control *txrc,
534 + bool sample, bool rtscts)
536 + const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
537 + struct minstrel_rate_stats *mr;
539 + mr = minstrel_get_ratestats(mi, index);
540 + if (!mr->retry_updated)
541 + minstrel_calc_retransmit(mp, mi, index);
544 + rate->count = mr->retry_count_rtscts;
546 + rate->count = mr->retry_count;
548 + rate->flags = IEEE80211_TX_RC_MCS | group->flags;
549 + if (txrc->short_preamble)
550 + rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
551 + if (txrc->rts || rtscts)
552 + rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
553 + rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
557 +minstrel_get_duration(int index)
559 + const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
560 + return group->duration[index % MCS_GROUP_RATES];
564 +minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
567 + struct minstrel_rate_stats *mr;
568 + struct minstrel_mcs_group_data *mg;
569 + int sample_idx = 0;
574 + sample_rate = mp->lookaround_rate_mrr;
576 + sample_rate = mp->lookaround_rate;
578 + delta = (mi->total_packets * sample_rate) / 100 - mi->sample_packets;
579 + delta -= mi->sample_pending / 2;
586 + /* With multi-rate retry, not every planned sample
587 + * attempt actually gets used, due to the way the retry
588 + * chain is set up - [max_tp,sample,prob,lowest] for
589 + * sample_rate < max_tp.
591 + * If there's too much sampling backlog and the link
592 + * starts getting worse, minstrel would start bursting
593 + * out lots of sampling frames, which would result
594 + * in a large throughput loss.
596 + mi->sample_packets += delta - 1;
599 + mg = &mi->groups[mi->sample_group];
600 + sample_idx = sample_table[mg->column][mg->index];
601 + mr = &mg->rates[sample_idx];
602 + sample_idx += mi->sample_group * MCS_GROUP_RATES;
605 + * When not using MRR, do not sample if the probability is already
606 + * higher than 95% to avoid wasting airtime
608 + if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
611 + if (minstrel_get_duration(sample_idx) >
612 + minstrel_get_duration(mi->max_tp_rate)) {
614 + * Make sure that lower rates get sampled occasionally, even
615 + * if the link is working perfectly. Some drivers such as ath9k
616 + * severely limit aggregation size if the MRR chain contains low
619 + * If the lower rate has already been tried a few times, there's
620 + * no point in forcing it to be sampled again, so skip to the
621 + * next sampling index after applying this one in the tx control
623 + if (mr->att_hist > 15) {
625 + minstrel_next_sample_idx(mi);
633 +minstrel_aggr_check(struct minstrel_priv *mp, struct ieee80211_sta *pubsta, struct sk_buff *skb)
635 + struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
636 + struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
639 + if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
642 + if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
645 + tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
646 + if (likely(sta->ampdu_mlme.tid_state_tx[tid] != HT_AGG_STATE_IDLE))
649 + ieee80211_start_tx_ba_session(pubsta, tid);
653 +minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
654 + struct ieee80211_tx_rate_control *txrc)
656 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
657 + struct ieee80211_tx_rate *ar = info->status.rates;
658 + struct minstrel_ht_sta_priv *msp = priv_sta;
659 + struct minstrel_ht_sta *mi = &msp->ht;
660 + struct minstrel_priv *mp = priv;
661 + bool sample_defer = false;
664 + if (rate_control_send_low(sta, priv_sta, txrc))
668 + return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
670 + minstrel_aggr_check(mp, sta, txrc->skb);
672 + sample_idx = minstrel_get_sample_rate(mp, mi, &sample_defer);
673 + if (sample_idx >= 0) {
674 + if (sample_defer) {
675 + minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
676 + txrc, false, false);
677 + minstrel_ht_set_rate(mp, mi, &ar[1], sample_idx,
679 + info->flags |= MINSTREL_INTFL_SAMPLE_SLOT1;
681 + minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
682 + txrc, true, false);
683 + minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
684 + txrc, false, true);
685 + info->flags |= MINSTREL_INTFL_SAMPLE_SLOT0;
687 + mi->sample_pending++;
689 + minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
690 + txrc, false, false);
691 + minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
692 + txrc, false, true);
694 + minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate, txrc, false, true);
699 + mi->total_packets++;
702 + if (mi->total_packets >= 100000) {
703 + mi->total_packets = 0;
704 + mi->sample_packets = 0;
705 + mi->sample_pending = 0;
711 +minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
712 + struct ieee80211_sta *sta, void *priv_sta)
714 + struct minstrel_priv *mp = priv;
715 + struct minstrel_ht_sta_priv *msp = priv_sta;
716 + struct minstrel_ht_sta *mi = &msp->ht;
717 + struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
718 + struct ieee80211_local *local = hw_to_local(mp->hw);
723 + /* fall back to the old minstrel for legacy stations */
724 + if (sta && !sta->ht_cap.ht_supported) {
725 + msp->is_ht = false;
726 + memset(&msp->legacy, 0, sizeof(msp->legacy));
727 + msp->legacy.r = msp->ratelist;
728 + msp->legacy.sample_table = msp->sample_table;
729 + return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
732 + BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
733 + MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
736 + memset(mi, 0, sizeof(*mi));
737 + mi->stats_update = jiffies;
739 + ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);
740 + mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;
741 + mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
743 + mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
744 + tx_streams = ((mcs->tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) >>
745 + IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
747 + for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
750 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
751 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
752 + req |= IEEE80211_HT_CAP_SGI_40;
754 + req |= IEEE80211_HT_CAP_SGI_20;
757 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
758 + req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
760 + if ((sta->ht_cap.cap & req) != req)
763 + mi->groups[i].supported =
764 + mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
769 +minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
771 + struct ieee80211_supported_band *sband;
772 + struct minstrel_ht_sta_priv *msp;
773 + struct minstrel_priv *mp = priv;
774 + struct ieee80211_hw *hw = mp->hw;
778 + for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
779 + sband = hw->wiphy->bands[i];
780 + if (sband && sband->n_bitrates > max_rates)
781 + max_rates = sband->n_bitrates;
784 + msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);
788 + msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
789 + if (!msp->ratelist)
792 + msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
793 + if (!msp->sample_table)
799 + kfree(msp->sample_table);
806 +minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
808 + struct minstrel_ht_sta_priv *msp = priv_sta;
810 + kfree(msp->sample_table);
811 + kfree(msp->ratelist);
816 +minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
818 + return mac80211_minstrel.alloc(hw, debugfsdir);
822 +minstrel_ht_free(void *priv)
824 + mac80211_minstrel.free(priv);
827 +static struct rate_control_ops mac80211_minstrel_ht = {
828 + .name = "minstrel_ht",
829 + .tx_status = minstrel_ht_tx_status,
830 + .get_rate = minstrel_ht_get_rate,
831 + .rate_init = minstrel_ht_rate_init,
832 + .alloc_sta = minstrel_ht_alloc_sta,
833 + .free_sta = minstrel_ht_free_sta,
834 + .alloc = minstrel_ht_alloc,
835 + .free = minstrel_ht_free,
836 +#ifdef CONFIG_MAC80211_DEBUGFS
837 + .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
838 + .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
844 +init_sample_table(void)
846 + int col, i, new_idx;
847 + u8 rnd[MCS_GROUP_RATES];
849 + memset(sample_table, 0xff, sizeof(sample_table));
850 + for (col = 0; col < SAMPLE_COLUMNS; col++) {
851 + for (i = 0; i < MCS_GROUP_RATES; i++) {
852 + get_random_bytes(rnd, sizeof(rnd));
853 + new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
855 + while (sample_table[col][new_idx] != 0xff)
856 + new_idx = (new_idx + 1) % MCS_GROUP_RATES;
858 + sample_table[col][new_idx] = i;
864 +rc80211_minstrel_ht_init(void)
866 + init_sample_table();
867 + return ieee80211_rate_control_register(&mac80211_minstrel_ht);
871 +rc80211_minstrel_ht_exit(void)
873 + ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
876 +++ b/net/mac80211/rc80211_minstrel_ht.h
879 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
881 + * This program is free software; you can redistribute it and/or modify
882 + * it under the terms of the GNU General Public License version 2 as
883 + * published by the Free Software Foundation.
886 +#ifndef __RC_MINSTREL_HT_H
887 +#define __RC_MINSTREL_HT_H
890 + * maximum number of spatial streams to make use of
891 + * set this value to 3 once we have drivers that support it
893 +#define MINSTREL_MAX_STREAMS 2
894 +#define MINSTREL_STREAM_GROUPS 4
896 +/* scaled fraction values */
897 +#define MINSTREL_SCALE 16
898 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
899 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
901 +#define MCS_GROUP_RATES 8
905 + unsigned int streams;
906 + unsigned int duration[MCS_GROUP_RATES];
909 +struct minstrel_rate_stats {
910 + /* current / last sampling period attempts/success counters */
911 + unsigned int attempts, last_attempts;
912 + unsigned int success, last_success;
914 + /* total attempts/success counters */
915 + u64 att_hist, succ_hist;
917 + /* current throughput */
918 + unsigned int cur_tp;
920 + /* packet delivery probabilities */
921 + unsigned int cur_prob, probability;
923 + /* maximum retry counts */
924 + bool retry_updated;
925 + unsigned int retry_count;
926 + unsigned int retry_count_rtscts;
929 +struct minstrel_mcs_group_data {
933 + /* bitfield of supported MCS rates of this group */
936 + /* selected primary rates */
937 + unsigned int max_tp_rate;
938 + unsigned int max_tp_rate2;
939 + unsigned int max_prob_rate;
941 + /* MCS rate statistics */
942 + struct minstrel_rate_stats rates[MCS_GROUP_RATES];
945 +struct minstrel_ht_sta {
946 + /* ampdu length average (EWMA) */
947 + unsigned int avg_ampdu_len;
949 + /* best throughput rate */
950 + unsigned int max_tp_rate;
952 + /* second best throughput rate */
953 + unsigned int max_tp_rate2;
955 + /* best probability rate */
956 + unsigned int max_prob_rate;
958 + /* time of last status update */
959 + unsigned long stats_update;
961 + /* overhead time in usec for each frame */
962 + unsigned int overhead;
963 + unsigned int overhead_rtscts;
965 + unsigned int total_packets;
966 + unsigned int sample_packets;
967 + unsigned int sample_pending;
969 + /* current MCS group to be sampled */
970 + unsigned int sample_group;
972 + /* MCS rate group info and statistics */
973 + struct minstrel_mcs_group_data groups[MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS];
976 +struct minstrel_ht_sta_priv {
978 + struct minstrel_ht_sta ht;
979 + struct minstrel_sta_info legacy;
981 +#ifdef CONFIG_MAC80211_DEBUGFS
982 + struct dentry *dbg_stats;
985 + void *sample_table;
989 +void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
990 +void minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta);
994 +++ b/net/mac80211/rc80211_minstrel_ht_debugfs.c
997 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
999 + * This program is free software; you can redistribute it and/or modify
1000 + * it under the terms of the GNU General Public License version 2 as
1001 + * published by the Free Software Foundation.
1003 +#include <linux/netdevice.h>
1004 +#include <linux/types.h>
1005 +#include <linux/skbuff.h>
1006 +#include <linux/debugfs.h>
1007 +#include <linux/ieee80211.h>
1008 +#include <net/mac80211.h>
1009 +#include "rc80211_minstrel.h"
1010 +#include "rc80211_minstrel_ht.h"
1012 +extern const struct mcs_group minstrel_mcs_groups[];
1015 +minstrel_ht_stats_open(struct inode *inode, struct file *file)
1017 + struct minstrel_ht_sta_priv *msp = inode->i_private;
1018 + struct minstrel_ht_sta *mi = &msp->ht;
1019 + struct minstrel_debugfs_info *ms;
1020 + unsigned int i, j, tp, prob, eprob;
1024 + if (!msp->is_ht) {
1025 + inode->i_private = &msp->legacy;
1026 + ret = minstrel_stats_open(inode, file);
1027 + inode->i_private = msp;
1031 + ms = kmalloc(sizeof(*ms) + 8192, GFP_KERNEL);
1035 + file->private_data = ms;
1037 + p += sprintf(p, "type rate throughput ewma prob this prob "
1038 + "this succ/attempt success attempts\n");
1039 + for (i = 0; i < MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS; i++) {
1040 + char htmode = '2';
1041 + char gimode = 'L';
1043 + if (!mi->groups[i].supported)
1046 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1048 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI)
1051 + for (j = 0; j < MCS_GROUP_RATES; j++) {
1052 + struct minstrel_rate_stats *mr = &mi->groups[i].rates[j];
1053 + int idx = i * MCS_GROUP_RATES + j;
1055 + if (!mi->groups[i].supported & BIT(j))
1058 + p += sprintf(p, "HT%c0/%cGI ", htmode, gimode);
1060 + *(p++) = (idx == mi->max_tp_rate) ? 'T' : ' ';
1061 + *(p++) = (idx == mi->max_tp_rate2) ? 't' : ' ';
1062 + *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' ';
1063 + p += sprintf(p, "MCS%-2u", (minstrel_mcs_groups[i].streams - 1) *
1064 + MCS_GROUP_RATES + j);
1066 + tp = mr->cur_tp / 10;
1067 + prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
1068 + eprob = MINSTREL_TRUNC(mr->probability * 1000);
1070 + p += sprintf(p, " %6u.%1u %6u.%1u %6u.%1u "
1071 + "%3u(%3u) %8llu %8llu\n",
1073 + eprob / 10, eprob % 10,
1074 + prob / 10, prob % 10,
1076 + mr->last_attempts,
1077 + (unsigned long long)mr->succ_hist,
1078 + (unsigned long long)mr->att_hist);
1081 + p += sprintf(p, "\nTotal packet count:: ideal %d "
1082 + "lookaround %d\n",
1083 + max(0, (int) mi->total_packets - (int) mi->sample_packets),
1084 + mi->sample_packets);
1085 + p += sprintf(p, "Average A-MPDU length: %d.%d\n",
1086 + MINSTREL_TRUNC(mi->avg_ampdu_len),
1087 + MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
1088 + ms->len = p - ms->buf;
1093 +static const struct file_operations minstrel_ht_stat_fops = {
1094 + .owner = THIS_MODULE,
1095 + .open = minstrel_ht_stats_open,
1096 + .read = minstrel_stats_read,
1097 + .release = minstrel_stats_release,
1101 +minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
1103 + struct minstrel_ht_sta_priv *msp = priv_sta;
1105 + msp->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, msp,
1106 + &minstrel_ht_stat_fops);
1110 +minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta)
1112 + struct minstrel_ht_sta_priv *msp = priv_sta;
1114 + debugfs_remove(msp->dbg_stats);