1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2019-2020 Intel Corporation
6 #include <linux/netdevice.h>
7 #include <linux/types.h>
8 #include <linux/skbuff.h>
9 #include <linux/debugfs.h>
10 #include <linux/random.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ieee80211.h>
13 #include <net/mac80211.h>
16 #include "rc80211_minstrel.h"
17 #include "rc80211_minstrel_ht.h"
19 #define AVG_AMPDU_SIZE 16
20 #define AVG_PKT_SIZE 1200
22 #define SAMPLE_SWITCH_THR 100
24 /* Number of bits for an average sized packet */
25 #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
27 /* Number of symbols for a packet with (bps) bits per symbol */
28 #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
30 /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
31 #define MCS_SYMBOL_TIME(sgi, syms) \
33 ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
34 ((syms) * 1000) << 2 /* syms * 4 us */ \
37 /* Transmit duration for the raw data part of an average sized packet */
38 #define MCS_DURATION(streams, sgi, bps) \
39 (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
46 * Define group sort order: HT40 -> SGI -> #streams
48 #define GROUP_IDX(_streams, _sgi, _ht40) \
49 MINSTREL_HT_GROUP_0 + \
50 MINSTREL_MAX_STREAMS * 2 * _ht40 + \
51 MINSTREL_MAX_STREAMS * _sgi + \
54 #define _MAX(a, b) (((a)>(b))?(a):(b))
56 #define GROUP_SHIFT(duration) \
57 _MAX(0, 16 - __builtin_clz(duration))
59 /* MCS rate information for an MCS group */
60 #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \
61 [GROUP_IDX(_streams, _sgi, _ht40)] = { \
62 .streams = _streams, \
66 IEEE80211_TX_RC_MCS | \
67 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
68 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
70 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s, \
71 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s, \
72 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s, \
73 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s, \
74 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s, \
75 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s, \
76 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s, \
77 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s \
81 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \
82 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
84 #define MCS_GROUP(_streams, _sgi, _ht40) \
85 __MCS_GROUP(_streams, _sgi, _ht40, \
86 MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
88 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
89 (MINSTREL_VHT_GROUP_0 + \
90 MINSTREL_MAX_STREAMS * 2 * (_bw) + \
91 MINSTREL_MAX_STREAMS * (_sgi) + \
94 #define BW2VBPS(_bw, r3, r2, r1) \
95 (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
97 #define __VHT_GROUP(_streams, _sgi, _bw, _s) \
98 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
99 .streams = _streams, \
103 IEEE80211_TX_RC_VHT_MCS | \
104 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
105 (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \
106 _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
108 MCS_DURATION(_streams, _sgi, \
109 BW2VBPS(_bw, 117, 54, 26)) >> _s, \
110 MCS_DURATION(_streams, _sgi, \
111 BW2VBPS(_bw, 234, 108, 52)) >> _s, \
112 MCS_DURATION(_streams, _sgi, \
113 BW2VBPS(_bw, 351, 162, 78)) >> _s, \
114 MCS_DURATION(_streams, _sgi, \
115 BW2VBPS(_bw, 468, 216, 104)) >> _s, \
116 MCS_DURATION(_streams, _sgi, \
117 BW2VBPS(_bw, 702, 324, 156)) >> _s, \
118 MCS_DURATION(_streams, _sgi, \
119 BW2VBPS(_bw, 936, 432, 208)) >> _s, \
120 MCS_DURATION(_streams, _sgi, \
121 BW2VBPS(_bw, 1053, 486, 234)) >> _s, \
122 MCS_DURATION(_streams, _sgi, \
123 BW2VBPS(_bw, 1170, 540, 260)) >> _s, \
124 MCS_DURATION(_streams, _sgi, \
125 BW2VBPS(_bw, 1404, 648, 312)) >> _s, \
126 MCS_DURATION(_streams, _sgi, \
127 BW2VBPS(_bw, 1560, 720, 346)) >> _s \
131 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \
132 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \
133 BW2VBPS(_bw, 117, 54, 26)))
135 #define VHT_GROUP(_streams, _sgi, _bw) \
136 __VHT_GROUP(_streams, _sgi, _bw, \
137 VHT_GROUP_SHIFT(_streams, _sgi, _bw))
139 #define CCK_DURATION(_bitrate, _short) \
140 (1000 * (10 /* SIFS */ + \
141 (_short ? 72 + 24 : 144 + 48) + \
142 (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate)))
144 #define CCK_DURATION_LIST(_short, _s) \
145 CCK_DURATION(10, _short) >> _s, \
146 CCK_DURATION(20, _short) >> _s, \
147 CCK_DURATION(55, _short) >> _s, \
148 CCK_DURATION(110, _short) >> _s
150 #define __CCK_GROUP(_s) \
151 [MINSTREL_CCK_GROUP] = { \
156 CCK_DURATION_LIST(false, _s), \
157 CCK_DURATION_LIST(true, _s) \
161 #define CCK_GROUP_SHIFT \
162 GROUP_SHIFT(CCK_DURATION(10, false))
164 #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT)
166 #define OFDM_DURATION(_bitrate) \
167 (1000 * (16 /* SIFS + signal ext */ + \
168 16 /* T_PREAMBLE */ + \
170 4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) / \
173 #define OFDM_DURATION_LIST(_s) \
174 OFDM_DURATION(60) >> _s, \
175 OFDM_DURATION(90) >> _s, \
176 OFDM_DURATION(120) >> _s, \
177 OFDM_DURATION(180) >> _s, \
178 OFDM_DURATION(240) >> _s, \
179 OFDM_DURATION(360) >> _s, \
180 OFDM_DURATION(480) >> _s, \
181 OFDM_DURATION(540) >> _s
183 #define __OFDM_GROUP(_s) \
184 [MINSTREL_OFDM_GROUP] = { \
189 OFDM_DURATION_LIST(_s), \
193 #define OFDM_GROUP_SHIFT \
194 GROUP_SHIFT(OFDM_DURATION(60))
196 #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT)
199 static bool minstrel_vht_only = true;
200 module_param(minstrel_vht_only, bool, 0644);
201 MODULE_PARM_DESC(minstrel_vht_only,
202 "Use only VHT rates when VHT is supported by sta.");
205 * To enable sufficiently targeted rate sampling, MCS rates are divided into
206 * groups, based on the number of streams and flags (HT40, SGI) that they
209 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
210 * BW -> SGI -> #streams
212 const struct mcs_group minstrel_mcs_groups[] = {
213 MCS_GROUP(1, 0, BW_20),
214 MCS_GROUP(2, 0, BW_20),
215 MCS_GROUP(3, 0, BW_20),
216 MCS_GROUP(4, 0, BW_20),
218 MCS_GROUP(1, 1, BW_20),
219 MCS_GROUP(2, 1, BW_20),
220 MCS_GROUP(3, 1, BW_20),
221 MCS_GROUP(4, 1, BW_20),
223 MCS_GROUP(1, 0, BW_40),
224 MCS_GROUP(2, 0, BW_40),
225 MCS_GROUP(3, 0, BW_40),
226 MCS_GROUP(4, 0, BW_40),
228 MCS_GROUP(1, 1, BW_40),
229 MCS_GROUP(2, 1, BW_40),
230 MCS_GROUP(3, 1, BW_40),
231 MCS_GROUP(4, 1, BW_40),
236 VHT_GROUP(1, 0, BW_20),
237 VHT_GROUP(2, 0, BW_20),
238 VHT_GROUP(3, 0, BW_20),
239 VHT_GROUP(4, 0, BW_20),
241 VHT_GROUP(1, 1, BW_20),
242 VHT_GROUP(2, 1, BW_20),
243 VHT_GROUP(3, 1, BW_20),
244 VHT_GROUP(4, 1, BW_20),
246 VHT_GROUP(1, 0, BW_40),
247 VHT_GROUP(2, 0, BW_40),
248 VHT_GROUP(3, 0, BW_40),
249 VHT_GROUP(4, 0, BW_40),
251 VHT_GROUP(1, 1, BW_40),
252 VHT_GROUP(2, 1, BW_40),
253 VHT_GROUP(3, 1, BW_40),
254 VHT_GROUP(4, 1, BW_40),
256 VHT_GROUP(1, 0, BW_80),
257 VHT_GROUP(2, 0, BW_80),
258 VHT_GROUP(3, 0, BW_80),
259 VHT_GROUP(4, 0, BW_80),
261 VHT_GROUP(1, 1, BW_80),
262 VHT_GROUP(2, 1, BW_80),
263 VHT_GROUP(3, 1, BW_80),
264 VHT_GROUP(4, 1, BW_80),
267 const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 };
268 const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
269 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
272 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
275 * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
276 * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
278 * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
281 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
286 if (nss != 3 && nss != 6)
288 } else if (bw == BW_80) {
289 if (nss == 3 || nss == 7)
294 WARN_ON(bw != BW_40);
297 switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
298 case IEEE80211_VHT_MCS_SUPPORT_0_7:
301 case IEEE80211_VHT_MCS_SUPPORT_0_8:
304 case IEEE80211_VHT_MCS_SUPPORT_0_9:
310 return 0x3ff & ~mask;
314 minstrel_ht_is_legacy_group(int group)
316 return group == MINSTREL_CCK_GROUP ||
317 group == MINSTREL_OFDM_GROUP;
321 * Look up an MCS group index based on mac80211 rate information
324 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
326 return GROUP_IDX((rate->idx / 8) + 1,
327 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
328 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
332 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
334 return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
335 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
336 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
337 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
340 static struct minstrel_rate_stats *
341 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
342 struct ieee80211_tx_rate *rate)
346 if (rate->flags & IEEE80211_TX_RC_MCS) {
347 group = minstrel_ht_get_group_idx(rate);
352 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
353 group = minstrel_vht_get_group_idx(rate);
354 idx = ieee80211_rate_get_vht_mcs(rate);
358 group = MINSTREL_CCK_GROUP;
359 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
360 if (rate->idx != mp->cck_rates[idx])
364 if ((mi->supported[group] & BIT(idx + 4)) &&
365 (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
370 group = MINSTREL_OFDM_GROUP;
371 for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
372 if (rate->idx == mp->ofdm_rates[mi->band][idx])
377 return &mi->groups[group].rates[idx];
380 static inline struct minstrel_rate_stats *
381 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
383 return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
387 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
389 if (!mi->avg_ampdu_len)
390 return AVG_AMPDU_SIZE;
392 return MINSTREL_TRUNC(mi->avg_ampdu_len);
396 * Return current throughput based on the average A-MPDU length, taking into
397 * account the expected number of retransmissions and their expected length
400 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
403 unsigned int nsecs = 0, overhead = mi->overhead;
404 unsigned int ampdu_len = 1;
406 /* do not account throughput if sucess prob is below 10% */
407 if (prob_avg < MINSTREL_FRAC(10, 100))
410 if (minstrel_ht_is_legacy_group(group))
411 overhead = mi->overhead_legacy;
413 ampdu_len = minstrel_ht_avg_ampdu_len(mi);
415 nsecs = 1000 * overhead / ampdu_len;
416 nsecs += minstrel_mcs_groups[group].duration[rate] <<
417 minstrel_mcs_groups[group].shift;
420 * For the throughput calculation, limit the probability value to 90% to
421 * account for collision related packet error rate fluctuation
422 * (prob is scaled - see MINSTREL_FRAC above)
424 if (prob_avg > MINSTREL_FRAC(90, 100))
425 return MINSTREL_TRUNC(100000 * ((MINSTREL_FRAC(90, 100) * 1000)
428 return MINSTREL_TRUNC(100000 * ((prob_avg * 1000) / nsecs));
432 * Find & sort topmost throughput rates
434 * If multiple rates provide equal throughput the sorting is based on their
435 * current success probability. Higher success probability is preferred among
436 * MCS groups, CCK rates do not provide aggregation and are therefore at last.
439 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
442 int cur_group, cur_idx, cur_tp_avg, cur_prob;
443 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
444 int j = MAX_THR_RATES;
446 cur_group = index / MCS_GROUP_RATES;
447 cur_idx = index % MCS_GROUP_RATES;
448 cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg;
449 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
452 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
453 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
454 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
455 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
457 if (cur_tp_avg < tmp_tp_avg ||
458 (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
463 if (j < MAX_THR_RATES - 1) {
464 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
465 (MAX_THR_RATES - (j + 1))));
467 if (j < MAX_THR_RATES)
472 * Find and set the topmost probability rate per sta and per group
475 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
477 struct minstrel_mcs_group_data *mg;
478 struct minstrel_rate_stats *mrs;
479 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
480 int max_tp_group, cur_tp_avg, cur_group, cur_idx;
481 int max_gpr_group, max_gpr_idx;
482 int max_gpr_tp_avg, max_gpr_prob;
484 cur_group = index / MCS_GROUP_RATES;
485 cur_idx = index % MCS_GROUP_RATES;
486 mg = &mi->groups[index / MCS_GROUP_RATES];
487 mrs = &mg->rates[index % MCS_GROUP_RATES];
489 tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
490 tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
491 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
492 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
494 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
495 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
496 max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
497 if (minstrel_ht_is_legacy_group(index / MCS_GROUP_RATES) &&
498 !minstrel_ht_is_legacy_group(max_tp_group))
501 max_gpr_group = mg->max_group_prob_rate / MCS_GROUP_RATES;
502 max_gpr_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
503 max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg;
505 if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) {
506 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
508 if (cur_tp_avg > tmp_tp_avg)
509 mi->max_prob_rate = index;
511 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
514 if (cur_tp_avg > max_gpr_tp_avg)
515 mg->max_group_prob_rate = index;
517 if (mrs->prob_avg > tmp_prob)
518 mi->max_prob_rate = index;
519 if (mrs->prob_avg > max_gpr_prob)
520 mg->max_group_prob_rate = index;
526 * Assign new rate set per sta and use CCK rates only if the fastest
527 * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
528 * rate sets where MCS and CCK rates are mixed, because CCK rates can
529 * not use aggregation.
532 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
533 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
534 u16 tmp_legacy_tp_rate[MAX_THR_RATES])
536 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
539 tmp_group = tmp_legacy_tp_rate[0] / MCS_GROUP_RATES;
540 tmp_idx = tmp_legacy_tp_rate[0] % MCS_GROUP_RATES;
541 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
542 tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
544 tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
545 tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
546 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
547 tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
549 if (tmp_cck_tp > tmp_mcs_tp) {
550 for(i = 0; i < MAX_THR_RATES; i++) {
551 minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i],
559 * Try to increase robustness of max_prob rate by decrease number of
560 * streams if possible.
563 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
565 struct minstrel_mcs_group_data *mg;
566 int tmp_max_streams, group, tmp_idx, tmp_prob;
569 if (!mi->sta->ht_cap.ht_supported)
572 tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
573 MCS_GROUP_RATES].streams;
574 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
575 mg = &mi->groups[group];
576 if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
579 tmp_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
580 tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg;
582 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
583 (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
584 mi->max_prob_rate = mg->max_group_prob_rate;
585 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
593 minstrel_get_duration(int index)
595 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
596 unsigned int duration = group->duration[index % MCS_GROUP_RATES];
597 return duration << group->shift;
601 minstrel_ht_probe_group(struct minstrel_ht_sta *mi, const struct mcs_group *tp_group,
602 int tp_idx, const struct mcs_group *group)
604 if (group->bw < tp_group->bw)
607 if (group->streams == tp_group->streams)
610 if (tp_idx < 4 && group->streams == tp_group->streams - 1)
613 return group->streams == tp_group->streams + 1;
617 minstrel_ht_find_probe_rates(struct minstrel_ht_sta *mi, u16 *rates, int *n_rates,
620 const struct mcs_group *group, *tp_group;
624 tp_group = &minstrel_mcs_groups[mi->max_tp_rate[0] / MCS_GROUP_RATES];
625 tp_idx = mi->max_tp_rate[0] % MCS_GROUP_RATES;
627 max_dur = minstrel_get_duration(mi->max_tp_rate[0]);
629 max_dur -= max_dur / 16;
631 for (g = 0; g < MINSTREL_GROUPS_NB; g++) {
632 u16 supported = mi->supported[g];
637 group = &minstrel_mcs_groups[g];
638 if (!minstrel_ht_probe_group(mi, tp_group, tp_idx, group))
641 for (i = 0; supported; supported >>= 1, i++) {
644 if (!(supported & 1))
647 if ((group->duration[i] << group->shift) > max_dur)
650 idx = g * MCS_GROUP_RATES + i;
651 if (idx == mi->max_tp_rate[0])
654 rates[(*n_rates)++] = idx;
661 minstrel_ht_rate_sample_switch(struct minstrel_priv *mp,
662 struct minstrel_ht_sta *mi)
664 struct minstrel_rate_stats *mrs;
665 u16 rates[MINSTREL_GROUPS_NB];
673 * Use rate switching instead of probing packets for devices with
674 * little control over retry fallback behavior
676 if (mp->hw->max_rates > 1)
680 * If the current EWMA prob is >75%, look for a rate that's 6.25%
681 * faster than the max tp rate.
682 * If that fails, look again for a rate that is at least as fast
684 mrs = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
685 faster_rate = mrs->prob_avg > MINSTREL_FRAC(75, 100);
686 minstrel_ht_find_probe_rates(mi, rates, &n_rates, faster_rate);
687 if (!n_rates && faster_rate)
688 minstrel_ht_find_probe_rates(mi, rates, &n_rates, false);
690 /* If no suitable rate was found, try to pick the next one in the group */
692 int g_idx = mi->max_tp_rate[0] / MCS_GROUP_RATES;
693 u16 supported = mi->supported[g_idx];
695 supported >>= mi->max_tp_rate[0] % MCS_GROUP_RATES;
696 for (i = 0; supported; supported >>= 1, i++) {
697 if (!(supported & 1))
700 probe_rate = mi->max_tp_rate[0] + i;
709 random = prandom_u32();
710 i = random % n_rates;
712 probe_rate = rates[i];
715 mi->sample_rate = probe_rate;
716 mi->sample_mode = MINSTREL_SAMPLE_ACTIVE;
720 * Update rate statistics and select new primary rates
722 * Rules for rate selection:
723 * - max_prob_rate must use only one stream, as a tradeoff between delivery
724 * probability and throughput during strong fluctuations
725 * - as long as the max prob rate has a probability of more than 75%, pick
726 * higher throughput rates, even if the probablity is a bit lower
729 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
732 struct minstrel_mcs_group_data *mg;
733 struct minstrel_rate_stats *mrs;
734 int group, i, j, cur_prob;
735 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
736 u16 tmp_legacy_tp_rate[MAX_THR_RATES], index;
737 bool ht_supported = mi->sta->ht_cap.ht_supported;
739 mi->sample_mode = MINSTREL_SAMPLE_IDLE;
742 mi->total_packets_cur = mi->total_packets -
743 mi->total_packets_last;
744 mi->total_packets_last = mi->total_packets;
746 if (!mp->sample_switch)
748 if (mi->total_packets_cur < SAMPLE_SWITCH_THR && mp->sample_switch != 1)
751 if (mi->ampdu_packets > 0) {
752 if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
753 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
754 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
757 mi->avg_ampdu_len = 0;
759 mi->ampdu_packets = 0;
763 mi->sample_count = 0;
765 memset(tmp_mcs_tp_rate, 0, sizeof(tmp_mcs_tp_rate));
766 memset(tmp_legacy_tp_rate, 0, sizeof(tmp_legacy_tp_rate));
767 if (mi->supported[MINSTREL_CCK_GROUP])
768 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
769 tmp_legacy_tp_rate[j] = MINSTREL_CCK_GROUP * MCS_GROUP_RATES;
770 else if (mi->supported[MINSTREL_OFDM_GROUP])
771 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
772 tmp_legacy_tp_rate[j] = MINSTREL_OFDM_GROUP * MCS_GROUP_RATES;
774 if (mi->supported[MINSTREL_VHT_GROUP_0])
775 index = MINSTREL_VHT_GROUP_0 * MCS_GROUP_RATES;
776 else if (ht_supported)
777 index = MINSTREL_HT_GROUP_0 * MCS_GROUP_RATES;
778 else if (mi->supported[MINSTREL_CCK_GROUP])
779 index = MINSTREL_CCK_GROUP * MCS_GROUP_RATES;
781 index = MINSTREL_OFDM_GROUP * MCS_GROUP_RATES;
783 for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++)
784 tmp_mcs_tp_rate[j] = index;
786 /* Find best rate sets within all MCS groups*/
787 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
788 u16 *tp_rate = tmp_mcs_tp_rate;
790 mg = &mi->groups[group];
791 if (!mi->supported[group])
796 /* (re)Initialize group rate indexes */
797 for(j = 0; j < MAX_THR_RATES; j++)
798 tmp_group_tp_rate[j] = MCS_GROUP_RATES * group;
800 if (group == MINSTREL_CCK_GROUP && ht_supported)
801 tp_rate = tmp_legacy_tp_rate;
803 for (i = 0; i < MCS_GROUP_RATES; i++) {
804 if (!(mi->supported[group] & BIT(i)))
807 index = MCS_GROUP_RATES * group + i;
810 mrs->retry_updated = false;
811 minstrel_calc_rate_stats(mp, mrs);
812 cur_prob = mrs->prob_avg;
814 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
817 /* Find max throughput rate set */
818 minstrel_ht_sort_best_tp_rates(mi, index, tp_rate);
820 /* Find max throughput rate set within a group */
821 minstrel_ht_sort_best_tp_rates(mi, index,
824 /* Find max probability rate per group and global */
825 minstrel_ht_set_best_prob_rate(mi, index);
828 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
829 sizeof(mg->max_group_tp_rate));
832 /* Assign new rate set per sta */
833 minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate,
835 memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
837 /* Try to increase robustness of max_prob_rate*/
838 minstrel_ht_prob_rate_reduce_streams(mi);
840 /* try to sample all available rates during each interval */
841 mi->sample_count *= 8;
843 mi->sample_count /= 2;
846 minstrel_ht_rate_sample_switch(mp, mi);
848 #ifdef CONFIG_MAC80211_DEBUGFS
849 /* use fixed index if set */
850 if (mp->fixed_rate_idx != -1) {
851 for (i = 0; i < 4; i++)
852 mi->max_tp_rate[i] = mp->fixed_rate_idx;
853 mi->max_prob_rate = mp->fixed_rate_idx;
854 mi->sample_mode = MINSTREL_SAMPLE_IDLE;
858 /* Reset update timer */
859 mi->last_stats_update = jiffies;
863 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
864 struct ieee80211_tx_rate *rate)
874 if (rate->flags & IEEE80211_TX_RC_MCS ||
875 rate->flags & IEEE80211_TX_RC_VHT_MCS)
878 for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++)
879 if (rate->idx == mp->cck_rates[i])
882 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++)
883 if (rate->idx == mp->ofdm_rates[mi->band][i])
890 minstrel_set_next_sample_idx(struct minstrel_ht_sta *mi)
892 struct minstrel_mcs_group_data *mg;
896 mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
897 mg = &mi->groups[mi->sample_group];
899 if (!mi->supported[mi->sample_group])
902 if (++mg->index >= MCS_GROUP_RATES) {
904 if (++mg->column >= ARRAY_SIZE(sample_table))
912 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
914 int group, orig_group;
916 orig_group = group = *idx / MCS_GROUP_RATES;
920 if (!mi->supported[group])
923 if (minstrel_mcs_groups[group].streams >
924 minstrel_mcs_groups[orig_group].streams)
928 *idx = mi->groups[group].max_group_tp_rate[0];
930 *idx = mi->groups[group].max_group_tp_rate[1];
936 minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
938 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
939 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
942 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
945 if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
948 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
951 tid = ieee80211_get_tid(hdr);
952 if (likely(sta->ampdu_mlme.tid_tx[tid]))
955 ieee80211_start_tx_ba_session(pubsta, tid, 0);
959 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
960 void *priv_sta, struct ieee80211_tx_status *st)
962 struct ieee80211_tx_info *info = st->info;
963 struct minstrel_ht_sta_priv *msp = priv_sta;
964 struct minstrel_ht_sta *mi = &msp->ht;
965 struct ieee80211_tx_rate *ar = info->status.rates;
966 struct minstrel_rate_stats *rate, *rate2, *rate_sample = NULL;
967 struct minstrel_priv *mp = priv;
968 u32 update_interval = mp->update_interval / 2;
969 bool last, update = false;
970 bool sample_status = false;
973 /* This packet was aggregated but doesn't carry status info */
974 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
975 !(info->flags & IEEE80211_TX_STAT_AMPDU))
978 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
979 info->status.ampdu_ack_len =
980 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
981 info->status.ampdu_len = 1;
985 mi->ampdu_len += info->status.ampdu_len;
987 if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
988 int avg_ampdu_len = minstrel_ht_avg_ampdu_len(mi);
990 mi->sample_wait = 16 + 2 * avg_ampdu_len;
991 mi->sample_tries = 1;
995 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
996 mi->sample_packets += info->status.ampdu_len;
998 if (mi->sample_mode != MINSTREL_SAMPLE_IDLE)
999 rate_sample = minstrel_get_ratestats(mi, mi->sample_rate);
1001 last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]);
1002 for (i = 0; !last; i++) {
1003 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
1004 !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]);
1006 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
1007 if (rate == rate_sample)
1008 sample_status = true;
1011 rate->success += info->status.ampdu_ack_len;
1013 rate->attempts += ar[i].count * info->status.ampdu_len;
1016 switch (mi->sample_mode) {
1017 case MINSTREL_SAMPLE_IDLE:
1019 (mp->hw->max_rates > 1 ||
1020 mi->total_packets_cur < SAMPLE_SWITCH_THR))
1021 update_interval /= 2;
1024 case MINSTREL_SAMPLE_ACTIVE:
1028 mi->sample_mode = MINSTREL_SAMPLE_PENDING;
1032 case MINSTREL_SAMPLE_PENDING:
1037 minstrel_ht_update_stats(mp, mi, false);
1042 if (mp->hw->max_rates > 1) {
1044 * check for sudden death of spatial multiplexing,
1045 * downgrade to a lower number of streams if necessary.
1047 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
1048 if (rate->attempts > 30 &&
1049 rate->success < rate->attempts / 4) {
1050 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
1054 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
1055 if (rate2->attempts > 30 &&
1056 rate2->success < rate2->attempts / 4) {
1057 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
1062 if (time_after(jiffies, mi->last_stats_update + update_interval)) {
1064 minstrel_ht_update_stats(mp, mi, true);
1068 minstrel_ht_update_rates(mp, mi);
1072 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1075 struct minstrel_rate_stats *mrs;
1076 unsigned int tx_time, tx_time_rtscts, tx_time_data;
1077 unsigned int cw = mp->cw_min;
1078 unsigned int ctime = 0;
1079 unsigned int t_slot = 9; /* FIXME */
1080 unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
1081 unsigned int overhead = 0, overhead_rtscts = 0;
1083 mrs = minstrel_get_ratestats(mi, index);
1084 if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) {
1085 mrs->retry_count = 1;
1086 mrs->retry_count_rtscts = 1;
1090 mrs->retry_count = 2;
1091 mrs->retry_count_rtscts = 2;
1092 mrs->retry_updated = true;
1094 tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
1096 /* Contention time for first 2 tries */
1097 ctime = (t_slot * cw) >> 1;
1098 cw = min((cw << 1) | 1, mp->cw_max);
1099 ctime += (t_slot * cw) >> 1;
1100 cw = min((cw << 1) | 1, mp->cw_max);
1102 if (minstrel_ht_is_legacy_group(index / MCS_GROUP_RATES)) {
1103 overhead = mi->overhead_legacy;
1104 overhead_rtscts = mi->overhead_legacy_rtscts;
1106 overhead = mi->overhead;
1107 overhead_rtscts = mi->overhead_rtscts;
1110 /* Total TX time for data and Contention after first 2 tries */
1111 tx_time = ctime + 2 * (overhead + tx_time_data);
1112 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
1114 /* See how many more tries we can fit inside segment size */
1116 /* Contention time for this try */
1117 ctime = (t_slot * cw) >> 1;
1118 cw = min((cw << 1) | 1, mp->cw_max);
1120 /* Total TX time after this try */
1121 tx_time += ctime + overhead + tx_time_data;
1122 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
1124 if (tx_time_rtscts < mp->segment_size)
1125 mrs->retry_count_rtscts++;
1126 } while ((tx_time < mp->segment_size) &&
1127 (++mrs->retry_count < mp->max_retry));
1132 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1133 struct ieee80211_sta_rates *ratetbl, int offset, int index)
1135 int group_idx = index / MCS_GROUP_RATES;
1136 const struct mcs_group *group = &minstrel_mcs_groups[group_idx];
1137 struct minstrel_rate_stats *mrs;
1139 u16 flags = group->flags;
1141 mrs = minstrel_get_ratestats(mi, index);
1142 if (!mrs->retry_updated)
1143 minstrel_calc_retransmit(mp, mi, index);
1145 if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
1146 ratetbl->rate[offset].count = 2;
1147 ratetbl->rate[offset].count_rts = 2;
1148 ratetbl->rate[offset].count_cts = 2;
1150 ratetbl->rate[offset].count = mrs->retry_count;
1151 ratetbl->rate[offset].count_cts = mrs->retry_count;
1152 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
1155 index %= MCS_GROUP_RATES;
1156 if (group_idx == MINSTREL_CCK_GROUP)
1157 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
1158 else if (group_idx == MINSTREL_OFDM_GROUP)
1159 idx = mp->ofdm_rates[mi->band][index %
1160 ARRAY_SIZE(mp->ofdm_rates[0])];
1161 else if (flags & IEEE80211_TX_RC_VHT_MCS)
1162 idx = ((group->streams - 1) << 4) |
1165 idx = index + (group->streams - 1) * 8;
1167 /* enable RTS/CTS if needed:
1168 * - if station is in dynamic SMPS (and streams > 1)
1169 * - for fallback rates, to increase chances of getting through
1172 (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
1173 group->streams > 1)) {
1174 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
1175 flags |= IEEE80211_TX_RC_USE_RTS_CTS;
1178 ratetbl->rate[offset].idx = idx;
1179 ratetbl->rate[offset].flags = flags;
1183 minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate)
1185 int group = rate / MCS_GROUP_RATES;
1186 rate %= MCS_GROUP_RATES;
1187 return mi->groups[group].rates[rate].prob_avg;
1191 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
1193 int group = mi->max_prob_rate / MCS_GROUP_RATES;
1194 const struct mcs_group *g = &minstrel_mcs_groups[group];
1195 int rate = mi->max_prob_rate % MCS_GROUP_RATES;
1196 unsigned int duration;
1198 /* Disable A-MSDU if max_prob_rate is bad */
1199 if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100))
1202 duration = g->duration[rate];
1203 duration <<= g->shift;
1205 /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
1206 if (duration > MCS_DURATION(1, 0, 52))
1210 * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
1213 if (duration > MCS_DURATION(1, 0, 104))
1217 * If the rate is slower than single-stream MCS7, or if the max throughput
1218 * rate success probability is less than 75%, limit A-MSDU to twice the usual
1221 if (duration > MCS_DURATION(1, 0, 260) ||
1222 (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) <
1223 MINSTREL_FRAC(75, 100)))
1227 * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
1228 * Since aggregation sessions are started/stopped without txq flush, use
1229 * the limit here to avoid the complexity of having to de-aggregate
1230 * packets in the queue.
1232 if (!mi->sta->vht_cap.vht_supported)
1233 return IEEE80211_MAX_MPDU_LEN_HT_BA;
1240 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1242 struct ieee80211_sta_rates *rates;
1243 u16 first_rate = mi->max_tp_rate[0];
1246 if (mi->sample_mode == MINSTREL_SAMPLE_ACTIVE)
1247 first_rate = mi->sample_rate;
1249 rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
1253 /* Start with max_tp_rate[0] */
1254 minstrel_ht_set_rate(mp, mi, rates, i++, first_rate);
1256 if (mp->hw->max_rates >= 3) {
1257 /* At least 3 tx rates supported, use max_tp_rate[1] next */
1258 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
1261 if (mp->hw->max_rates >= 2) {
1262 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
1265 mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
1266 rates->rate[i].idx = -1;
1267 rate_control_set_rates(mp->hw, mi->sta, rates);
1271 minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1273 struct minstrel_rate_stats *mrs;
1274 struct minstrel_mcs_group_data *mg;
1275 unsigned int sample_dur, sample_group, cur_max_tp_streams;
1276 int tp_rate1, tp_rate2;
1279 if (mp->hw->max_rates == 1 && mp->sample_switch &&
1280 (mi->total_packets_cur >= SAMPLE_SWITCH_THR ||
1281 mp->sample_switch == 1))
1284 if (mi->sample_wait > 0) {
1289 if (!mi->sample_tries)
1292 sample_group = mi->sample_group;
1293 mg = &mi->groups[sample_group];
1294 sample_idx = sample_table[mg->column][mg->index];
1295 minstrel_set_next_sample_idx(mi);
1297 if (!(mi->supported[sample_group] & BIT(sample_idx)))
1300 mrs = &mg->rates[sample_idx];
1301 sample_idx += sample_group * MCS_GROUP_RATES;
1303 /* Set tp_rate1, tp_rate2 to the highest / second highest max_tp_rate */
1304 if (minstrel_get_duration(mi->max_tp_rate[0]) >
1305 minstrel_get_duration(mi->max_tp_rate[1])) {
1306 tp_rate1 = mi->max_tp_rate[1];
1307 tp_rate2 = mi->max_tp_rate[0];
1309 tp_rate1 = mi->max_tp_rate[0];
1310 tp_rate2 = mi->max_tp_rate[1];
1314 * Sampling might add some overhead (RTS, no aggregation)
1315 * to the frame. Hence, don't use sampling for the highest currently
1316 * used highest throughput or probability rate.
1318 if (sample_idx == mi->max_tp_rate[0] || sample_idx == mi->max_prob_rate)
1322 * Do not sample if the probability is already higher than 95%,
1323 * or if the rate is 3 times slower than the current max probability
1324 * rate, to avoid wasting airtime.
1326 sample_dur = minstrel_get_duration(sample_idx);
1327 if (mrs->prob_avg > MINSTREL_FRAC(95, 100) ||
1328 minstrel_get_duration(mi->max_prob_rate) * 3 < sample_dur)
1333 * For devices with no configurable multi-rate retry, skip sampling
1334 * below the per-group max throughput rate, and only use one sampling
1337 if (mp->hw->max_rates == 1 &&
1338 (minstrel_get_duration(mg->max_group_tp_rate[0]) < sample_dur ||
1342 /* Skip already sampled slow rates */
1343 if (sample_dur >= minstrel_get_duration(tp_rate1) && mrs->attempts)
1347 * Make sure that lower rates get sampled only occasionally,
1348 * if the link is working perfectly.
1351 cur_max_tp_streams = minstrel_mcs_groups[tp_rate1 /
1352 MCS_GROUP_RATES].streams;
1353 if (sample_dur >= minstrel_get_duration(tp_rate2) &&
1354 (cur_max_tp_streams - 1 <
1355 minstrel_mcs_groups[sample_group].streams ||
1356 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
1357 if (mrs->sample_skipped < 20)
1360 if (mi->sample_slow++ > 2)
1369 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1370 struct ieee80211_tx_rate_control *txrc)
1372 const struct mcs_group *sample_group;
1373 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1374 struct ieee80211_tx_rate *rate = &info->status.rates[0];
1375 struct minstrel_ht_sta_priv *msp = priv_sta;
1376 struct minstrel_ht_sta *mi = &msp->ht;
1377 struct minstrel_priv *mp = priv;
1380 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
1381 !minstrel_ht_is_legacy_group(mi->max_prob_rate / MCS_GROUP_RATES))
1382 minstrel_aggr_check(sta, txrc->skb);
1384 info->flags |= mi->tx_flags;
1386 #ifdef CONFIG_MAC80211_DEBUGFS
1387 if (mp->fixed_rate_idx != -1)
1391 /* Don't use EAPOL frames for sampling on non-mrr hw */
1392 if (mp->hw->max_rates == 1 &&
1393 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
1396 sample_idx = minstrel_get_sample_rate(mp, mi);
1398 mi->total_packets++;
1401 if (mi->total_packets == ~0) {
1402 mi->total_packets = 0;
1403 mi->sample_packets = 0;
1409 sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
1410 sample_idx %= MCS_GROUP_RATES;
1412 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
1413 (sample_idx >= 4) != txrc->short_preamble)
1416 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1419 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
1420 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
1421 rate->idx = mp->cck_rates[idx];
1422 } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) {
1423 int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]);
1424 rate->idx = mp->ofdm_rates[mi->band][idx];
1425 } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
1426 ieee80211_rate_set_vht(rate, sample_idx % MCS_GROUP_RATES,
1427 sample_group->streams);
1429 rate->idx = sample_idx + (sample_group->streams - 1) * 8;
1432 rate->flags = sample_group->flags;
1436 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1437 struct ieee80211_supported_band *sband,
1438 struct ieee80211_sta *sta)
1442 if (sband->band != NL80211_BAND_2GHZ)
1445 if (sta->ht_cap.ht_supported &&
1446 !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
1449 for (i = 0; i < 4; i++) {
1450 if (mp->cck_rates[i] == 0xff ||
1451 !rate_supported(sta, sband->band, mp->cck_rates[i]))
1454 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i);
1455 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1456 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4);
1461 minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1462 struct ieee80211_supported_band *sband,
1463 struct ieee80211_sta *sta)
1468 if (sta->ht_cap.ht_supported)
1471 rates = mp->ofdm_rates[sband->band];
1472 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) {
1473 if (rates[i] == 0xff ||
1474 !rate_supported(sta, sband->band, rates[i]))
1477 mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i);
1482 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1483 struct cfg80211_chan_def *chandef,
1484 struct ieee80211_sta *sta, void *priv_sta)
1486 struct minstrel_priv *mp = priv;
1487 struct minstrel_ht_sta_priv *msp = priv_sta;
1488 struct minstrel_ht_sta *mi = &msp->ht;
1489 struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
1490 u16 ht_cap = sta->ht_cap.cap;
1491 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1492 const struct ieee80211_rate *ctl_rate;
1495 int n_supported = 0;
1500 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
1502 if (vht_cap->vht_supported)
1503 use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
1507 memset(mi, 0, sizeof(*mi));
1510 mi->band = sband->band;
1511 mi->last_stats_update = jiffies;
1513 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1514 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
1515 mi->overhead += ack_dur;
1516 mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
1518 ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)];
1519 erp = ctl_rate->flags & IEEE80211_RATE_ERP_G;
1520 ack_dur = ieee80211_frame_duration(sband->band, 10,
1521 ctl_rate->bitrate, erp, 1,
1522 ieee80211_chandef_get_shift(chandef));
1523 mi->overhead_legacy = ack_dur;
1524 mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur;
1526 mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
1528 /* When using MRR, sample more on the first attempt, without delay */
1530 mi->sample_count = 16;
1531 mi->sample_wait = 0;
1533 mi->sample_count = 8;
1534 mi->sample_wait = 8;
1536 mi->sample_tries = 4;
1539 stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
1540 IEEE80211_HT_CAP_RX_STBC_SHIFT;
1542 ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
1544 stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
1545 IEEE80211_VHT_CAP_RXSTBC_SHIFT;
1547 ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
1550 mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1552 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1554 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
1555 u32 gflags = minstrel_mcs_groups[i].flags;
1558 mi->supported[i] = 0;
1559 if (minstrel_ht_is_legacy_group(i))
1562 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1563 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1564 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
1567 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
1572 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1573 sta->bandwidth < IEEE80211_STA_RX_BW_40)
1576 nss = minstrel_mcs_groups[i].streams;
1578 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
1579 if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
1583 if (gflags & IEEE80211_TX_RC_MCS) {
1584 if (use_vht && minstrel_vht_only)
1587 mi->supported[i] = mcs->rx_mask[nss - 1];
1588 if (mi->supported[i])
1594 if (!vht_cap->vht_supported ||
1595 WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
1596 WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
1599 if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
1600 if (sta->bandwidth < IEEE80211_STA_RX_BW_80 ||
1601 ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
1602 !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
1607 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1609 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1614 mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
1615 vht_cap->vht_mcs.tx_mcs_map);
1617 if (mi->supported[i])
1621 minstrel_ht_update_cck(mp, mi, sband, sta);
1622 minstrel_ht_update_ofdm(mp, mi, sband, sta);
1624 /* create an initial rate table with the lowest supported rates */
1625 minstrel_ht_update_stats(mp, mi, true);
1626 minstrel_ht_update_rates(mp, mi);
1630 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
1631 struct cfg80211_chan_def *chandef,
1632 struct ieee80211_sta *sta, void *priv_sta)
1634 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1638 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
1639 struct cfg80211_chan_def *chandef,
1640 struct ieee80211_sta *sta, void *priv_sta,
1643 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1647 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1649 struct ieee80211_supported_band *sband;
1650 struct minstrel_ht_sta_priv *msp;
1651 struct minstrel_priv *mp = priv;
1652 struct ieee80211_hw *hw = mp->hw;
1656 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1657 sband = hw->wiphy->bands[i];
1658 if (sband && sband->n_bitrates > max_rates)
1659 max_rates = sband->n_bitrates;
1662 msp = kzalloc(sizeof(*msp), gfp);
1666 msp->ratelist = kcalloc(max_rates, sizeof(struct minstrel_rate), gfp);
1670 msp->sample_table = kmalloc_array(max_rates, SAMPLE_COLUMNS, gfp);
1671 if (!msp->sample_table)
1677 kfree(msp->ratelist);
1684 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1686 struct minstrel_ht_sta_priv *msp = priv_sta;
1688 kfree(msp->sample_table);
1689 kfree(msp->ratelist);
1694 minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband,
1695 const s16 *bitrates, int n_rates, u32 rate_flags)
1699 for (i = 0; i < sband->n_bitrates; i++) {
1700 struct ieee80211_rate *rate = &sband->bitrates[i];
1702 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1705 for (j = 0; j < n_rates; j++) {
1706 if (rate->bitrate != bitrates[j])
1716 minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
1718 static const s16 bitrates[4] = { 10, 20, 55, 110 };
1719 struct ieee80211_supported_band *sband;
1720 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1722 memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates));
1723 sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
1727 BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates));
1728 minstrel_ht_fill_rate_array(mp->cck_rates, sband,
1729 minstrel_cck_bitrates,
1730 ARRAY_SIZE(minstrel_cck_bitrates),
1735 minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band)
1737 static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
1738 struct ieee80211_supported_band *sband;
1739 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1741 memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band]));
1742 sband = mp->hw->wiphy->bands[band];
1746 BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates));
1747 minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband,
1748 minstrel_ofdm_bitrates,
1749 ARRAY_SIZE(minstrel_ofdm_bitrates),
1754 minstrel_ht_alloc(struct ieee80211_hw *hw)
1756 struct minstrel_priv *mp;
1759 mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
1763 mp->sample_switch = -1;
1765 /* contention window settings
1766 * Just an approximation. Using the per-queue values would complicate
1767 * the calculations and is probably unnecessary */
1771 /* number of packets (in %) to use for sampling other rates
1772 * sample less often for non-mrr packets, because the overhead
1773 * is much higher than with mrr */
1774 mp->lookaround_rate = 5;
1775 mp->lookaround_rate_mrr = 10;
1777 /* maximum time that the hw is allowed to stay in one MRR segment */
1778 mp->segment_size = 6000;
1780 if (hw->max_rate_tries > 0)
1781 mp->max_retry = hw->max_rate_tries;
1783 /* safe default, does not necessarily have to match hw properties */
1786 if (hw->max_rates >= 4)
1790 mp->update_interval = HZ / 10;
1793 minstrel_ht_init_cck_rates(mp);
1794 for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++)
1795 minstrel_ht_init_ofdm_rates(mp, i);
1800 #ifdef CONFIG_MAC80211_DEBUGFS
1801 static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv,
1802 struct dentry *debugfsdir)
1804 struct minstrel_priv *mp = priv;
1806 mp->fixed_rate_idx = (u32) -1;
1807 debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
1808 &mp->fixed_rate_idx);
1809 debugfs_create_u32("sample_switch", S_IRUGO | S_IWUSR, debugfsdir,
1810 &mp->sample_switch);
1811 debugfs_create_bool("new_avg", S_IRUGO | S_IWUSR, debugfsdir,
1817 minstrel_ht_free(void *priv)
1822 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1824 struct minstrel_ht_sta_priv *msp = priv_sta;
1825 struct minstrel_ht_sta *mi = &msp->ht;
1826 int i, j, prob, tp_avg;
1828 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1829 j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
1830 prob = mi->groups[i].rates[j].prob_avg;
1832 /* convert tp_avg from pkt per second in kbps */
1833 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
1834 tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
1839 static const struct rate_control_ops mac80211_minstrel_ht = {
1840 .name = "minstrel_ht",
1841 .tx_status_ext = minstrel_ht_tx_status,
1842 .get_rate = minstrel_ht_get_rate,
1843 .rate_init = minstrel_ht_rate_init,
1844 .rate_update = minstrel_ht_rate_update,
1845 .alloc_sta = minstrel_ht_alloc_sta,
1846 .free_sta = minstrel_ht_free_sta,
1847 .alloc = minstrel_ht_alloc,
1848 .free = minstrel_ht_free,
1849 #ifdef CONFIG_MAC80211_DEBUGFS
1850 .add_debugfs = minstrel_ht_add_debugfs,
1851 .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
1853 .get_expected_throughput = minstrel_ht_get_expected_throughput,
1857 static void __init init_sample_table(void)
1859 int col, i, new_idx;
1860 u8 rnd[MCS_GROUP_RATES];
1862 memset(sample_table, 0xff, sizeof(sample_table));
1863 for (col = 0; col < SAMPLE_COLUMNS; col++) {
1864 prandom_bytes(rnd, sizeof(rnd));
1865 for (i = 0; i < MCS_GROUP_RATES; i++) {
1866 new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
1867 while (sample_table[col][new_idx] != 0xff)
1868 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
1870 sample_table[col][new_idx] = i;
1876 rc80211_minstrel_init(void)
1878 init_sample_table();
1879 return ieee80211_rate_control_register(&mac80211_minstrel_ht);
1883 rc80211_minstrel_exit(void)
1885 ieee80211_rate_control_unregister(&mac80211_minstrel_ht);