1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2019-2021 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_ht.h"
18 #define AVG_AMPDU_SIZE 16
19 #define AVG_PKT_SIZE 1200
21 /* Number of bits for an average sized packet */
22 #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
24 /* Number of symbols for a packet with (bps) bits per symbol */
25 #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
27 /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
28 #define MCS_SYMBOL_TIME(sgi, syms) \
30 ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
31 ((syms) * 1000) << 2 /* syms * 4 us */ \
34 /* Transmit duration for the raw data part of an average sized packet */
35 #define MCS_DURATION(streams, sgi, bps) \
36 (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
43 * Define group sort order: HT40 -> SGI -> #streams
45 #define GROUP_IDX(_streams, _sgi, _ht40) \
46 MINSTREL_HT_GROUP_0 + \
47 MINSTREL_MAX_STREAMS * 2 * _ht40 + \
48 MINSTREL_MAX_STREAMS * _sgi + \
51 #define _MAX(a, b) (((a)>(b))?(a):(b))
53 #define GROUP_SHIFT(duration) \
54 _MAX(0, 16 - __builtin_clz(duration))
56 /* MCS rate information for an MCS group */
57 #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \
58 [GROUP_IDX(_streams, _sgi, _ht40)] = { \
59 .streams = _streams, \
63 IEEE80211_TX_RC_MCS | \
64 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
65 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
67 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s, \
68 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s, \
69 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s, \
70 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s, \
71 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s, \
72 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s, \
73 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s, \
74 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s \
78 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \
79 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
81 #define MCS_GROUP(_streams, _sgi, _ht40) \
82 __MCS_GROUP(_streams, _sgi, _ht40, \
83 MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
85 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
86 (MINSTREL_VHT_GROUP_0 + \
87 MINSTREL_MAX_STREAMS * 2 * (_bw) + \
88 MINSTREL_MAX_STREAMS * (_sgi) + \
91 #define BW2VBPS(_bw, r3, r2, r1) \
92 (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
94 #define __VHT_GROUP(_streams, _sgi, _bw, _s) \
95 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
96 .streams = _streams, \
100 IEEE80211_TX_RC_VHT_MCS | \
101 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
102 (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \
103 _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
105 MCS_DURATION(_streams, _sgi, \
106 BW2VBPS(_bw, 117, 54, 26)) >> _s, \
107 MCS_DURATION(_streams, _sgi, \
108 BW2VBPS(_bw, 234, 108, 52)) >> _s, \
109 MCS_DURATION(_streams, _sgi, \
110 BW2VBPS(_bw, 351, 162, 78)) >> _s, \
111 MCS_DURATION(_streams, _sgi, \
112 BW2VBPS(_bw, 468, 216, 104)) >> _s, \
113 MCS_DURATION(_streams, _sgi, \
114 BW2VBPS(_bw, 702, 324, 156)) >> _s, \
115 MCS_DURATION(_streams, _sgi, \
116 BW2VBPS(_bw, 936, 432, 208)) >> _s, \
117 MCS_DURATION(_streams, _sgi, \
118 BW2VBPS(_bw, 1053, 486, 234)) >> _s, \
119 MCS_DURATION(_streams, _sgi, \
120 BW2VBPS(_bw, 1170, 540, 260)) >> _s, \
121 MCS_DURATION(_streams, _sgi, \
122 BW2VBPS(_bw, 1404, 648, 312)) >> _s, \
123 MCS_DURATION(_streams, _sgi, \
124 BW2VBPS(_bw, 1560, 720, 346)) >> _s \
128 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \
129 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \
130 BW2VBPS(_bw, 117, 54, 26)))
132 #define VHT_GROUP(_streams, _sgi, _bw) \
133 __VHT_GROUP(_streams, _sgi, _bw, \
134 VHT_GROUP_SHIFT(_streams, _sgi, _bw))
136 #define CCK_DURATION(_bitrate, _short) \
137 (1000 * (10 /* SIFS */ + \
138 (_short ? 72 + 24 : 144 + 48) + \
139 (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate)))
141 #define CCK_DURATION_LIST(_short, _s) \
142 CCK_DURATION(10, _short) >> _s, \
143 CCK_DURATION(20, _short) >> _s, \
144 CCK_DURATION(55, _short) >> _s, \
145 CCK_DURATION(110, _short) >> _s
147 #define __CCK_GROUP(_s) \
148 [MINSTREL_CCK_GROUP] = { \
153 CCK_DURATION_LIST(false, _s), \
154 CCK_DURATION_LIST(true, _s) \
158 #define CCK_GROUP_SHIFT \
159 GROUP_SHIFT(CCK_DURATION(10, false))
161 #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT)
163 #define OFDM_DURATION(_bitrate) \
164 (1000 * (16 /* SIFS + signal ext */ + \
165 16 /* T_PREAMBLE */ + \
167 4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) / \
170 #define OFDM_DURATION_LIST(_s) \
171 OFDM_DURATION(60) >> _s, \
172 OFDM_DURATION(90) >> _s, \
173 OFDM_DURATION(120) >> _s, \
174 OFDM_DURATION(180) >> _s, \
175 OFDM_DURATION(240) >> _s, \
176 OFDM_DURATION(360) >> _s, \
177 OFDM_DURATION(480) >> _s, \
178 OFDM_DURATION(540) >> _s
180 #define __OFDM_GROUP(_s) \
181 [MINSTREL_OFDM_GROUP] = { \
186 OFDM_DURATION_LIST(_s), \
190 #define OFDM_GROUP_SHIFT \
191 GROUP_SHIFT(OFDM_DURATION(60))
193 #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT)
196 static bool minstrel_vht_only = true;
197 module_param(minstrel_vht_only, bool, 0644);
198 MODULE_PARM_DESC(minstrel_vht_only,
199 "Use only VHT rates when VHT is supported by sta.");
202 * To enable sufficiently targeted rate sampling, MCS rates are divided into
203 * groups, based on the number of streams and flags (HT40, SGI) that they
206 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
207 * BW -> SGI -> #streams
209 const struct mcs_group minstrel_mcs_groups[] = {
210 MCS_GROUP(1, 0, BW_20),
211 MCS_GROUP(2, 0, BW_20),
212 MCS_GROUP(3, 0, BW_20),
213 MCS_GROUP(4, 0, BW_20),
215 MCS_GROUP(1, 1, BW_20),
216 MCS_GROUP(2, 1, BW_20),
217 MCS_GROUP(3, 1, BW_20),
218 MCS_GROUP(4, 1, BW_20),
220 MCS_GROUP(1, 0, BW_40),
221 MCS_GROUP(2, 0, BW_40),
222 MCS_GROUP(3, 0, BW_40),
223 MCS_GROUP(4, 0, BW_40),
225 MCS_GROUP(1, 1, BW_40),
226 MCS_GROUP(2, 1, BW_40),
227 MCS_GROUP(3, 1, BW_40),
228 MCS_GROUP(4, 1, BW_40),
233 VHT_GROUP(1, 0, BW_20),
234 VHT_GROUP(2, 0, BW_20),
235 VHT_GROUP(3, 0, BW_20),
236 VHT_GROUP(4, 0, BW_20),
238 VHT_GROUP(1, 1, BW_20),
239 VHT_GROUP(2, 1, BW_20),
240 VHT_GROUP(3, 1, BW_20),
241 VHT_GROUP(4, 1, BW_20),
243 VHT_GROUP(1, 0, BW_40),
244 VHT_GROUP(2, 0, BW_40),
245 VHT_GROUP(3, 0, BW_40),
246 VHT_GROUP(4, 0, BW_40),
248 VHT_GROUP(1, 1, BW_40),
249 VHT_GROUP(2, 1, BW_40),
250 VHT_GROUP(3, 1, BW_40),
251 VHT_GROUP(4, 1, BW_40),
253 VHT_GROUP(1, 0, BW_80),
254 VHT_GROUP(2, 0, BW_80),
255 VHT_GROUP(3, 0, BW_80),
256 VHT_GROUP(4, 0, BW_80),
258 VHT_GROUP(1, 1, BW_80),
259 VHT_GROUP(2, 1, BW_80),
260 VHT_GROUP(3, 1, BW_80),
261 VHT_GROUP(4, 1, BW_80),
264 const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 };
265 const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
266 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
267 static const u8 minstrel_sample_seq[] = {
268 MINSTREL_SAMPLE_TYPE_INC,
269 MINSTREL_SAMPLE_TYPE_JUMP,
270 MINSTREL_SAMPLE_TYPE_INC,
271 MINSTREL_SAMPLE_TYPE_JUMP,
272 MINSTREL_SAMPLE_TYPE_INC,
273 MINSTREL_SAMPLE_TYPE_SLOW,
277 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
280 * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
281 * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
283 * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
286 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
291 if (nss != 3 && nss != 6)
293 } else if (bw == BW_80) {
294 if (nss == 3 || nss == 7)
299 WARN_ON(bw != BW_40);
302 switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
303 case IEEE80211_VHT_MCS_SUPPORT_0_7:
306 case IEEE80211_VHT_MCS_SUPPORT_0_8:
309 case IEEE80211_VHT_MCS_SUPPORT_0_9:
315 return 0x3ff & ~mask;
319 minstrel_ht_is_legacy_group(int group)
321 return group == MINSTREL_CCK_GROUP ||
322 group == MINSTREL_OFDM_GROUP;
326 * Look up an MCS group index based on mac80211 rate information
329 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
331 return GROUP_IDX((rate->idx / 8) + 1,
332 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
333 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
337 * Look up an MCS group index based on new cfg80211 rate_info.
340 minstrel_ht_ri_get_group_idx(struct rate_info *rate)
342 return GROUP_IDX((rate->mcs / 8) + 1,
343 !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
344 !!(rate->bw & RATE_INFO_BW_40));
348 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
350 return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
351 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
352 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
353 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
357 * Look up an MCS group index based on new cfg80211 rate_info.
360 minstrel_vht_ri_get_group_idx(struct rate_info *rate)
362 return VHT_GROUP_IDX(rate->nss,
363 !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
364 !!(rate->bw & RATE_INFO_BW_40) +
365 2*!!(rate->bw & RATE_INFO_BW_80));
368 static struct minstrel_rate_stats *
369 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
370 struct ieee80211_tx_rate *rate)
374 if (rate->flags & IEEE80211_TX_RC_MCS) {
375 group = minstrel_ht_get_group_idx(rate);
380 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
381 group = minstrel_vht_get_group_idx(rate);
382 idx = ieee80211_rate_get_vht_mcs(rate);
386 group = MINSTREL_CCK_GROUP;
387 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
388 if (!(mi->supported[group] & BIT(idx)))
391 if (rate->idx != mp->cck_rates[idx])
395 if ((mi->supported[group] & BIT(idx + 4)) &&
396 (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
401 group = MINSTREL_OFDM_GROUP;
402 for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
403 if (rate->idx == mp->ofdm_rates[mi->band][idx])
408 return &mi->groups[group].rates[idx];
412 * Get the minstrel rate statistics for specified STA and rate info.
414 static struct minstrel_rate_stats *
415 minstrel_ht_ri_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
416 struct ieee80211_rate_status *rate_status)
419 struct rate_info *rate = &rate_status->rate_idx;
421 if (rate->flags & RATE_INFO_FLAGS_MCS) {
422 group = minstrel_ht_ri_get_group_idx(rate);
427 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) {
428 group = minstrel_vht_ri_get_group_idx(rate);
433 group = MINSTREL_CCK_GROUP;
434 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
435 if (rate->legacy != minstrel_cck_bitrates[ mp->cck_rates[idx] ])
439 if ((mi->supported[group] & BIT(idx + 4)) &&
440 mi->use_short_preamble)
445 group = MINSTREL_OFDM_GROUP;
446 for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
447 if (rate->legacy == minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][idx] ])
452 return &mi->groups[group].rates[idx];
455 static inline struct minstrel_rate_stats *
456 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
458 return &mi->groups[MI_RATE_GROUP(index)].rates[MI_RATE_IDX(index)];
461 static inline int minstrel_get_duration(int index)
463 const struct mcs_group *group = &minstrel_mcs_groups[MI_RATE_GROUP(index)];
464 unsigned int duration = group->duration[MI_RATE_IDX(index)];
466 return duration << group->shift;
470 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
474 if (mi->avg_ampdu_len)
475 return MINSTREL_TRUNC(mi->avg_ampdu_len);
477 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(mi->max_tp_rate[0])))
480 duration = minstrel_get_duration(mi->max_tp_rate[0]);
482 if (duration > 400 * 1000)
485 if (duration > 250 * 1000)
488 if (duration > 150 * 1000)
495 * Return current throughput based on the average A-MPDU length, taking into
496 * account the expected number of retransmissions and their expected length
499 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
502 unsigned int nsecs = 0, overhead = mi->overhead;
503 unsigned int ampdu_len = 1;
505 /* do not account throughput if success prob is below 10% */
506 if (prob_avg < MINSTREL_FRAC(10, 100))
509 if (minstrel_ht_is_legacy_group(group))
510 overhead = mi->overhead_legacy;
512 ampdu_len = minstrel_ht_avg_ampdu_len(mi);
514 nsecs = 1000 * overhead / ampdu_len;
515 nsecs += minstrel_mcs_groups[group].duration[rate] <<
516 minstrel_mcs_groups[group].shift;
519 * For the throughput calculation, limit the probability value to 90% to
520 * account for collision related packet error rate fluctuation
521 * (prob is scaled - see MINSTREL_FRAC above)
523 if (prob_avg > MINSTREL_FRAC(90, 100))
524 prob_avg = MINSTREL_FRAC(90, 100);
526 return MINSTREL_TRUNC(100 * ((prob_avg * 1000000) / nsecs));
530 * Find & sort topmost throughput rates
532 * If multiple rates provide equal throughput the sorting is based on their
533 * current success probability. Higher success probability is preferred among
534 * MCS groups, CCK rates do not provide aggregation and are therefore at last.
537 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
540 int cur_group, cur_idx, cur_tp_avg, cur_prob;
541 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
542 int j = MAX_THR_RATES;
544 cur_group = MI_RATE_GROUP(index);
545 cur_idx = MI_RATE_IDX(index);
546 cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg;
547 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
550 tmp_group = MI_RATE_GROUP(tp_list[j - 1]);
551 tmp_idx = MI_RATE_IDX(tp_list[j - 1]);
552 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
553 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
555 if (cur_tp_avg < tmp_tp_avg ||
556 (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
561 if (j < MAX_THR_RATES - 1) {
562 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
563 (MAX_THR_RATES - (j + 1))));
565 if (j < MAX_THR_RATES)
570 * Find and set the topmost probability rate per sta and per group
573 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 *dest, u16 index)
575 struct minstrel_mcs_group_data *mg;
576 struct minstrel_rate_stats *mrs;
577 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
578 int max_tp_group, max_tp_idx, max_tp_prob;
579 int cur_tp_avg, cur_group, cur_idx;
580 int max_gpr_group, max_gpr_idx;
581 int max_gpr_tp_avg, max_gpr_prob;
583 cur_group = MI_RATE_GROUP(index);
584 cur_idx = MI_RATE_IDX(index);
585 mg = &mi->groups[cur_group];
586 mrs = &mg->rates[cur_idx];
588 tmp_group = MI_RATE_GROUP(*dest);
589 tmp_idx = MI_RATE_IDX(*dest);
590 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
591 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
593 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
594 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
595 max_tp_group = MI_RATE_GROUP(mi->max_tp_rate[0]);
596 max_tp_idx = MI_RATE_IDX(mi->max_tp_rate[0]);
597 max_tp_prob = mi->groups[max_tp_group].rates[max_tp_idx].prob_avg;
599 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index)) &&
600 !minstrel_ht_is_legacy_group(max_tp_group))
603 /* skip rates faster than max tp rate with lower prob */
604 if (minstrel_get_duration(mi->max_tp_rate[0]) > minstrel_get_duration(index) &&
605 mrs->prob_avg < max_tp_prob)
608 max_gpr_group = MI_RATE_GROUP(mg->max_group_prob_rate);
609 max_gpr_idx = MI_RATE_IDX(mg->max_group_prob_rate);
610 max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg;
612 if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) {
613 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
615 if (cur_tp_avg > tmp_tp_avg)
618 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
621 if (cur_tp_avg > max_gpr_tp_avg)
622 mg->max_group_prob_rate = index;
624 if (mrs->prob_avg > tmp_prob)
626 if (mrs->prob_avg > max_gpr_prob)
627 mg->max_group_prob_rate = index;
633 * Assign new rate set per sta and use CCK rates only if the fastest
634 * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
635 * rate sets where MCS and CCK rates are mixed, because CCK rates can
636 * not use aggregation.
639 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
640 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
641 u16 tmp_legacy_tp_rate[MAX_THR_RATES])
643 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
646 tmp_group = MI_RATE_GROUP(tmp_legacy_tp_rate[0]);
647 tmp_idx = MI_RATE_IDX(tmp_legacy_tp_rate[0]);
648 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
649 tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
651 tmp_group = MI_RATE_GROUP(tmp_mcs_tp_rate[0]);
652 tmp_idx = MI_RATE_IDX(tmp_mcs_tp_rate[0]);
653 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
654 tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
656 if (tmp_cck_tp > tmp_mcs_tp) {
657 for(i = 0; i < MAX_THR_RATES; i++) {
658 minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i],
666 * Try to increase robustness of max_prob rate by decrease number of
667 * streams if possible.
670 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
672 struct minstrel_mcs_group_data *mg;
673 int tmp_max_streams, group, tmp_idx, tmp_prob;
676 if (!mi->sta->deflink.ht_cap.ht_supported)
679 group = MI_RATE_GROUP(mi->max_tp_rate[0]);
680 tmp_max_streams = minstrel_mcs_groups[group].streams;
681 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
682 mg = &mi->groups[group];
683 if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
686 tmp_idx = MI_RATE_IDX(mg->max_group_prob_rate);
687 tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg;
689 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
690 (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
691 mi->max_prob_rate = mg->max_group_prob_rate;
692 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
700 __minstrel_ht_get_sample_rate(struct minstrel_ht_sta *mi,
701 enum minstrel_sample_type type)
703 u16 *rates = mi->sample[type].sample_rates;
707 for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
720 minstrel_ewma(int old, int new, int weight)
725 incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
730 static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
744 val = MINSTREL_AVG_COEFF1 * in;
745 val += MINSTREL_AVG_COEFF2 * out_1;
746 val += MINSTREL_AVG_COEFF3 * out_2;
747 val >>= MINSTREL_SCALE;
749 if (val > 1 << MINSTREL_SCALE)
750 val = 1 << MINSTREL_SCALE;
762 * Recalculate statistics and counters of a given rate
765 minstrel_ht_calc_rate_stats(struct minstrel_priv *mp,
766 struct minstrel_rate_stats *mrs)
768 unsigned int cur_prob;
770 if (unlikely(mrs->attempts > 0)) {
771 cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
772 minstrel_filter_avg_add(&mrs->prob_avg,
773 &mrs->prob_avg_1, cur_prob);
774 mrs->att_hist += mrs->attempts;
775 mrs->succ_hist += mrs->success;
778 mrs->last_success = mrs->success;
779 mrs->last_attempts = mrs->attempts;
785 minstrel_ht_find_sample_rate(struct minstrel_ht_sta *mi, int type, int idx)
789 for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
790 u16 cur = mi->sample[type].sample_rates[i];
803 minstrel_ht_move_sample_rates(struct minstrel_ht_sta *mi, int type,
804 u32 fast_rate_dur, u32 slow_rate_dur)
806 u16 *rates = mi->sample[type].sample_rates;
809 for (i = 0, j = 0; i < MINSTREL_SAMPLE_RATES; i++) {
818 duration = minstrel_get_duration(cur);
820 case MINSTREL_SAMPLE_TYPE_SLOW:
821 valid = duration > fast_rate_dur &&
822 duration < slow_rate_dur;
824 case MINSTREL_SAMPLE_TYPE_INC:
825 case MINSTREL_SAMPLE_TYPE_JUMP:
826 valid = duration < fast_rate_dur;
849 minstrel_ht_group_min_rate_offset(struct minstrel_ht_sta *mi, int group,
852 u16 supported = mi->supported[group];
855 for (i = 0; i < MCS_GROUP_RATES && supported; i++, supported >>= 1) {
856 if (!(supported & BIT(0)))
859 if (minstrel_get_duration(MI_RATE(group, i)) >= max_duration)
869 * Incremental update rates:
870 * Flip through groups and pick the first group rate that is faster than the
871 * highest currently selected rate
874 minstrel_ht_next_inc_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur)
876 u8 type = MINSTREL_SAMPLE_TYPE_INC;
880 group = mi->sample[type].sample_group;
881 for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
882 group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
884 index = minstrel_ht_group_min_rate_offset(mi, group,
889 index = MI_RATE(group, index & 0xf);
890 if (!minstrel_ht_find_sample_rate(mi, type, index))
896 mi->sample[type].sample_group = group;
902 minstrel_ht_next_group_sample_rate(struct minstrel_ht_sta *mi, int group,
903 u16 supported, int offset)
905 struct minstrel_mcs_group_data *mg = &mi->groups[group];
909 for (i = 0; i < MCS_GROUP_RATES; i++) {
910 idx = sample_table[mg->column][mg->index];
911 if (++mg->index >= MCS_GROUP_RATES) {
913 if (++mg->column >= ARRAY_SIZE(sample_table))
920 if (!(supported & BIT(idx)))
923 return MI_RATE(group, idx);
931 * Sample random rates, use those that are faster than the highest
932 * currently selected rate. Rates between the fastest and the slowest
933 * get sorted into the slow sample bucket, but only if it has room
936 minstrel_ht_next_jump_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur,
937 u32 slow_rate_dur, int *slow_rate_ofs)
939 struct minstrel_rate_stats *mrs;
940 u32 max_duration = slow_rate_dur;
941 int i, index, offset;
947 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
948 max_duration = fast_rate_dur;
950 slow_rates = mi->sample[MINSTREL_SAMPLE_TYPE_SLOW].sample_rates;
951 group = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group;
952 for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
955 group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
957 supported = mi->supported[group];
961 offset = minstrel_ht_group_min_rate_offset(mi, group,
966 index = minstrel_ht_next_group_sample_rate(mi, group, supported,
971 duration = minstrel_get_duration(index);
972 if (duration < fast_rate_dur)
973 type = MINSTREL_SAMPLE_TYPE_JUMP;
975 type = MINSTREL_SAMPLE_TYPE_SLOW;
977 if (minstrel_ht_find_sample_rate(mi, type, index))
980 if (type == MINSTREL_SAMPLE_TYPE_JUMP)
983 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
986 if (duration >= slow_rate_dur)
989 /* skip slow rates with high success probability */
990 mrs = minstrel_get_ratestats(mi, index);
991 if (mrs->prob_avg > MINSTREL_FRAC(95, 100))
994 slow_rates[(*slow_rate_ofs)++] = index;
995 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
996 max_duration = fast_rate_dur;
1001 mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group = group;
1007 minstrel_ht_refill_sample_rates(struct minstrel_ht_sta *mi)
1009 u32 prob_dur = minstrel_get_duration(mi->max_prob_rate);
1010 u32 tp_dur = minstrel_get_duration(mi->max_tp_rate[0]);
1011 u32 tp2_dur = minstrel_get_duration(mi->max_tp_rate[1]);
1012 u32 fast_rate_dur = min(min(tp_dur, tp2_dur), prob_dur);
1013 u32 slow_rate_dur = max(max(tp_dur, tp2_dur), prob_dur);
1017 rates = mi->sample[MINSTREL_SAMPLE_TYPE_INC].sample_rates;
1018 i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_INC,
1019 fast_rate_dur, slow_rate_dur);
1020 while (i < MINSTREL_SAMPLE_RATES) {
1021 rates[i] = minstrel_ht_next_inc_rate(mi, tp_dur);
1028 rates = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_rates;
1029 i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_JUMP,
1030 fast_rate_dur, slow_rate_dur);
1031 j = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_SLOW,
1032 fast_rate_dur, slow_rate_dur);
1033 while (i < MINSTREL_SAMPLE_RATES) {
1034 rates[i] = minstrel_ht_next_jump_rate(mi, fast_rate_dur,
1042 for (i = 0; i < ARRAY_SIZE(mi->sample); i++)
1043 memcpy(mi->sample[i].cur_sample_rates, mi->sample[i].sample_rates,
1044 sizeof(mi->sample[i].cur_sample_rates));
1049 * Update rate statistics and select new primary rates
1051 * Rules for rate selection:
1052 * - max_prob_rate must use only one stream, as a tradeoff between delivery
1053 * probability and throughput during strong fluctuations
1054 * - as long as the max prob rate has a probability of more than 75%, pick
1055 * higher throughput rates, even if the probablity is a bit lower
1058 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1060 struct minstrel_mcs_group_data *mg;
1061 struct minstrel_rate_stats *mrs;
1062 int group, i, j, cur_prob;
1063 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
1064 u16 tmp_legacy_tp_rate[MAX_THR_RATES], tmp_max_prob_rate;
1066 bool ht_supported = mi->sta->deflink.ht_cap.ht_supported;
1068 if (mi->ampdu_packets > 0) {
1069 if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
1070 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
1071 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
1074 mi->avg_ampdu_len = 0;
1076 mi->ampdu_packets = 0;
1079 if (mi->supported[MINSTREL_CCK_GROUP])
1080 group = MINSTREL_CCK_GROUP;
1081 else if (mi->supported[MINSTREL_OFDM_GROUP])
1082 group = MINSTREL_OFDM_GROUP;
1086 index = MI_RATE(group, 0);
1087 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
1088 tmp_legacy_tp_rate[j] = index;
1090 if (mi->supported[MINSTREL_VHT_GROUP_0])
1091 group = MINSTREL_VHT_GROUP_0;
1092 else if (ht_supported)
1093 group = MINSTREL_HT_GROUP_0;
1094 else if (mi->supported[MINSTREL_CCK_GROUP])
1095 group = MINSTREL_CCK_GROUP;
1097 group = MINSTREL_OFDM_GROUP;
1099 index = MI_RATE(group, 0);
1100 tmp_max_prob_rate = index;
1101 for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++)
1102 tmp_mcs_tp_rate[j] = index;
1104 /* Find best rate sets within all MCS groups*/
1105 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
1106 u16 *tp_rate = tmp_mcs_tp_rate;
1109 mg = &mi->groups[group];
1110 if (!mi->supported[group])
1113 /* (re)Initialize group rate indexes */
1114 for(j = 0; j < MAX_THR_RATES; j++)
1115 tmp_group_tp_rate[j] = MI_RATE(group, 0);
1117 if (group == MINSTREL_CCK_GROUP && ht_supported)
1118 tp_rate = tmp_legacy_tp_rate;
1120 for (i = MCS_GROUP_RATES - 1; i >= 0; i--) {
1121 if (!(mi->supported[group] & BIT(i)))
1124 index = MI_RATE(group, i);
1126 mrs = &mg->rates[i];
1127 mrs->retry_updated = false;
1128 minstrel_ht_calc_rate_stats(mp, mrs);
1131 last_prob = max(last_prob, mrs->prob_avg);
1133 mrs->prob_avg = max(last_prob, mrs->prob_avg);
1134 cur_prob = mrs->prob_avg;
1136 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
1139 /* Find max throughput rate set */
1140 minstrel_ht_sort_best_tp_rates(mi, index, tp_rate);
1142 /* Find max throughput rate set within a group */
1143 minstrel_ht_sort_best_tp_rates(mi, index,
1147 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
1148 sizeof(mg->max_group_tp_rate));
1151 /* Assign new rate set per sta */
1152 minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate,
1153 tmp_legacy_tp_rate);
1154 memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
1156 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
1157 if (!mi->supported[group])
1160 mg = &mi->groups[group];
1161 mg->max_group_prob_rate = MI_RATE(group, 0);
1163 for (i = 0; i < MCS_GROUP_RATES; i++) {
1164 if (!(mi->supported[group] & BIT(i)))
1167 index = MI_RATE(group, i);
1169 /* Find max probability rate per group and global */
1170 minstrel_ht_set_best_prob_rate(mi, &tmp_max_prob_rate,
1175 mi->max_prob_rate = tmp_max_prob_rate;
1177 /* Try to increase robustness of max_prob_rate*/
1178 minstrel_ht_prob_rate_reduce_streams(mi);
1179 minstrel_ht_refill_sample_rates(mi);
1181 #ifdef CONFIG_MAC80211_DEBUGFS
1182 /* use fixed index if set */
1183 if (mp->fixed_rate_idx != -1) {
1184 for (i = 0; i < 4; i++)
1185 mi->max_tp_rate[i] = mp->fixed_rate_idx;
1186 mi->max_prob_rate = mp->fixed_rate_idx;
1190 /* Reset update timer */
1191 mi->last_stats_update = jiffies;
1192 mi->sample_time = jiffies;
1196 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1197 struct ieee80211_tx_rate *rate)
1207 if (rate->flags & IEEE80211_TX_RC_MCS ||
1208 rate->flags & IEEE80211_TX_RC_VHT_MCS)
1211 for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++)
1212 if (rate->idx == mp->cck_rates[i])
1215 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++)
1216 if (rate->idx == mp->ofdm_rates[mi->band][i])
1223 * Check whether rate_status contains valid information.
1226 minstrel_ht_ri_txstat_valid(struct minstrel_priv *mp,
1227 struct minstrel_ht_sta *mi,
1228 struct ieee80211_rate_status *rate_status)
1234 if (!rate_status->try_count)
1237 if (rate_status->rate_idx.flags & RATE_INFO_FLAGS_MCS ||
1238 rate_status->rate_idx.flags & RATE_INFO_FLAGS_VHT_MCS)
1241 for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++) {
1242 if (rate_status->rate_idx.legacy ==
1243 minstrel_cck_bitrates[ mp->cck_rates[i] ])
1247 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates); i++) {
1248 if (rate_status->rate_idx.legacy ==
1249 minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][i] ])
1257 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
1259 int group, orig_group;
1261 orig_group = group = MI_RATE_GROUP(*idx);
1265 if (!mi->supported[group])
1268 if (minstrel_mcs_groups[group].streams >
1269 minstrel_mcs_groups[orig_group].streams)
1273 *idx = mi->groups[group].max_group_tp_rate[0];
1275 *idx = mi->groups[group].max_group_tp_rate[1];
1281 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
1282 void *priv_sta, struct ieee80211_tx_status *st)
1284 struct ieee80211_tx_info *info = st->info;
1285 struct minstrel_ht_sta *mi = priv_sta;
1286 struct ieee80211_tx_rate *ar = info->status.rates;
1287 struct minstrel_rate_stats *rate, *rate2;
1288 struct minstrel_priv *mp = priv;
1289 u32 update_interval = mp->update_interval;
1290 bool last, update = false;
1293 /* Ignore packet that was sent with noAck flag */
1294 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1297 /* This packet was aggregated but doesn't carry status info */
1298 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1299 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1302 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
1303 info->status.ampdu_ack_len =
1304 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
1305 info->status.ampdu_len = 1;
1309 if (mi->total_packets >= ~0 - info->status.ampdu_len) {
1310 mi->total_packets = 0;
1311 mi->sample_packets = 0;
1314 mi->total_packets += info->status.ampdu_len;
1315 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
1316 mi->sample_packets += info->status.ampdu_len;
1318 mi->ampdu_packets++;
1319 mi->ampdu_len += info->status.ampdu_len;
1321 if (st->rates && st->n_rates) {
1322 last = !minstrel_ht_ri_txstat_valid(mp, mi, &(st->rates[0]));
1323 for (i = 0; !last; i++) {
1324 last = (i == st->n_rates - 1) ||
1325 !minstrel_ht_ri_txstat_valid(mp, mi,
1326 &(st->rates[i + 1]));
1328 rate = minstrel_ht_ri_get_stats(mp, mi,
1332 rate->success += info->status.ampdu_ack_len;
1334 rate->attempts += st->rates[i].try_count *
1335 info->status.ampdu_len;
1338 last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]);
1339 for (i = 0; !last; i++) {
1340 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
1341 !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]);
1343 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
1345 rate->success += info->status.ampdu_ack_len;
1347 rate->attempts += ar[i].count * info->status.ampdu_len;
1351 if (mp->hw->max_rates > 1) {
1353 * check for sudden death of spatial multiplexing,
1354 * downgrade to a lower number of streams if necessary.
1356 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
1357 if (rate->attempts > 30 &&
1358 rate->success < rate->attempts / 4) {
1359 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
1363 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
1364 if (rate2->attempts > 30 &&
1365 rate2->success < rate2->attempts / 4) {
1366 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
1371 if (time_after(jiffies, mi->last_stats_update + update_interval)) {
1373 minstrel_ht_update_stats(mp, mi);
1377 minstrel_ht_update_rates(mp, mi);
1381 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1384 struct minstrel_rate_stats *mrs;
1385 unsigned int tx_time, tx_time_rtscts, tx_time_data;
1386 unsigned int cw = mp->cw_min;
1387 unsigned int ctime = 0;
1388 unsigned int t_slot = 9; /* FIXME */
1389 unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
1390 unsigned int overhead = 0, overhead_rtscts = 0;
1392 mrs = minstrel_get_ratestats(mi, index);
1393 if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) {
1394 mrs->retry_count = 1;
1395 mrs->retry_count_rtscts = 1;
1399 mrs->retry_count = 2;
1400 mrs->retry_count_rtscts = 2;
1401 mrs->retry_updated = true;
1403 tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
1405 /* Contention time for first 2 tries */
1406 ctime = (t_slot * cw) >> 1;
1407 cw = min((cw << 1) | 1, mp->cw_max);
1408 ctime += (t_slot * cw) >> 1;
1409 cw = min((cw << 1) | 1, mp->cw_max);
1411 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index))) {
1412 overhead = mi->overhead_legacy;
1413 overhead_rtscts = mi->overhead_legacy_rtscts;
1415 overhead = mi->overhead;
1416 overhead_rtscts = mi->overhead_rtscts;
1419 /* Total TX time for data and Contention after first 2 tries */
1420 tx_time = ctime + 2 * (overhead + tx_time_data);
1421 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
1423 /* See how many more tries we can fit inside segment size */
1425 /* Contention time for this try */
1426 ctime = (t_slot * cw) >> 1;
1427 cw = min((cw << 1) | 1, mp->cw_max);
1429 /* Total TX time after this try */
1430 tx_time += ctime + overhead + tx_time_data;
1431 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
1433 if (tx_time_rtscts < mp->segment_size)
1434 mrs->retry_count_rtscts++;
1435 } while ((tx_time < mp->segment_size) &&
1436 (++mrs->retry_count < mp->max_retry));
1441 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1442 struct ieee80211_sta_rates *ratetbl, int offset, int index)
1444 int group_idx = MI_RATE_GROUP(index);
1445 const struct mcs_group *group = &minstrel_mcs_groups[group_idx];
1446 struct minstrel_rate_stats *mrs;
1448 u16 flags = group->flags;
1450 mrs = minstrel_get_ratestats(mi, index);
1451 if (!mrs->retry_updated)
1452 minstrel_calc_retransmit(mp, mi, index);
1454 if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
1455 ratetbl->rate[offset].count = 2;
1456 ratetbl->rate[offset].count_rts = 2;
1457 ratetbl->rate[offset].count_cts = 2;
1459 ratetbl->rate[offset].count = mrs->retry_count;
1460 ratetbl->rate[offset].count_cts = mrs->retry_count;
1461 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
1464 index = MI_RATE_IDX(index);
1465 if (group_idx == MINSTREL_CCK_GROUP)
1466 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
1467 else if (group_idx == MINSTREL_OFDM_GROUP)
1468 idx = mp->ofdm_rates[mi->band][index %
1469 ARRAY_SIZE(mp->ofdm_rates[0])];
1470 else if (flags & IEEE80211_TX_RC_VHT_MCS)
1471 idx = ((group->streams - 1) << 4) |
1474 idx = index + (group->streams - 1) * 8;
1476 /* enable RTS/CTS if needed:
1477 * - if station is in dynamic SMPS (and streams > 1)
1478 * - for fallback rates, to increase chances of getting through
1481 (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
1482 group->streams > 1)) {
1483 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
1484 flags |= IEEE80211_TX_RC_USE_RTS_CTS;
1487 ratetbl->rate[offset].idx = idx;
1488 ratetbl->rate[offset].flags = flags;
1492 minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate)
1494 int group = MI_RATE_GROUP(rate);
1495 rate = MI_RATE_IDX(rate);
1496 return mi->groups[group].rates[rate].prob_avg;
1500 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
1502 int group = MI_RATE_GROUP(mi->max_prob_rate);
1503 const struct mcs_group *g = &minstrel_mcs_groups[group];
1504 int rate = MI_RATE_IDX(mi->max_prob_rate);
1505 unsigned int duration;
1507 /* Disable A-MSDU if max_prob_rate is bad */
1508 if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100))
1511 duration = g->duration[rate];
1512 duration <<= g->shift;
1514 /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
1515 if (duration > MCS_DURATION(1, 0, 52))
1519 * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
1522 if (duration > MCS_DURATION(1, 0, 104))
1526 * If the rate is slower than single-stream MCS7, or if the max throughput
1527 * rate success probability is less than 75%, limit A-MSDU to twice the usual
1530 if (duration > MCS_DURATION(1, 0, 260) ||
1531 (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) <
1532 MINSTREL_FRAC(75, 100)))
1536 * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
1537 * Since aggregation sessions are started/stopped without txq flush, use
1538 * the limit here to avoid the complexity of having to de-aggregate
1539 * packets in the queue.
1541 if (!mi->sta->deflink.vht_cap.vht_supported)
1542 return IEEE80211_MAX_MPDU_LEN_HT_BA;
1549 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1551 struct ieee80211_sta_rates *rates;
1554 rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
1558 /* Start with max_tp_rate[0] */
1559 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
1561 /* Fill up remaining, keep one entry for max_probe_rate */
1562 for (; i < (mp->hw->max_rates - 1); i++)
1563 minstrel_ht_set_rate(mp, mi, rates, i, mi->max_tp_rate[i]);
1565 if (i < mp->hw->max_rates)
1566 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
1568 if (i < IEEE80211_TX_RATE_TABLE_SIZE)
1569 rates->rate[i].idx = -1;
1571 mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
1572 rate_control_set_rates(mp->hw, mi->sta, rates);
1576 minstrel_ht_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1580 if (mp->hw->max_rates > 1) {
1581 seq = mi->sample_seq;
1582 mi->sample_seq = (seq + 1) % ARRAY_SIZE(minstrel_sample_seq);
1583 seq = minstrel_sample_seq[seq];
1585 seq = MINSTREL_SAMPLE_TYPE_INC;
1588 return __minstrel_ht_get_sample_rate(mi, seq);
1592 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1593 struct ieee80211_tx_rate_control *txrc)
1595 const struct mcs_group *sample_group;
1596 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1597 struct ieee80211_tx_rate *rate = &info->status.rates[0];
1598 struct minstrel_ht_sta *mi = priv_sta;
1599 struct minstrel_priv *mp = priv;
1602 info->flags |= mi->tx_flags;
1604 #ifdef CONFIG_MAC80211_DEBUGFS
1605 if (mp->fixed_rate_idx != -1)
1609 /* Don't use EAPOL frames for sampling on non-mrr hw */
1610 if (mp->hw->max_rates == 1 &&
1611 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
1614 if (time_is_after_jiffies(mi->sample_time))
1617 mi->sample_time = jiffies + MINSTREL_SAMPLE_INTERVAL;
1618 sample_idx = minstrel_ht_get_sample_rate(mp, mi);
1622 sample_group = &minstrel_mcs_groups[MI_RATE_GROUP(sample_idx)];
1623 sample_idx = MI_RATE_IDX(sample_idx);
1625 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
1626 (sample_idx >= 4) != txrc->short_preamble)
1629 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1632 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
1633 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
1634 rate->idx = mp->cck_rates[idx];
1635 } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) {
1636 int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]);
1637 rate->idx = mp->ofdm_rates[mi->band][idx];
1638 } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
1639 ieee80211_rate_set_vht(rate, MI_RATE_IDX(sample_idx),
1640 sample_group->streams);
1642 rate->idx = sample_idx + (sample_group->streams - 1) * 8;
1645 rate->flags = sample_group->flags;
1649 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1650 struct ieee80211_supported_band *sband,
1651 struct ieee80211_sta *sta)
1655 if (sband->band != NL80211_BAND_2GHZ)
1658 if (sta->deflink.ht_cap.ht_supported &&
1659 !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
1662 for (i = 0; i < 4; i++) {
1663 if (mp->cck_rates[i] == 0xff ||
1664 !rate_supported(sta, sband->band, mp->cck_rates[i]))
1667 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i);
1668 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1669 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4);
1674 minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1675 struct ieee80211_supported_band *sband,
1676 struct ieee80211_sta *sta)
1681 if (sta->deflink.ht_cap.ht_supported)
1684 rates = mp->ofdm_rates[sband->band];
1685 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) {
1686 if (rates[i] == 0xff ||
1687 !rate_supported(sta, sband->band, rates[i]))
1690 mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i);
1695 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1696 struct cfg80211_chan_def *chandef,
1697 struct ieee80211_sta *sta, void *priv_sta)
1699 struct minstrel_priv *mp = priv;
1700 struct minstrel_ht_sta *mi = priv_sta;
1701 struct ieee80211_mcs_info *mcs = &sta->deflink.ht_cap.mcs;
1702 u16 ht_cap = sta->deflink.ht_cap.cap;
1703 struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
1704 const struct ieee80211_rate *ctl_rate;
1705 struct sta_info *sta_info;
1708 int n_supported = 0;
1713 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
1715 if (vht_cap->vht_supported)
1716 use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
1720 memset(mi, 0, sizeof(*mi));
1723 mi->band = sband->band;
1724 mi->last_stats_update = jiffies;
1726 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1727 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
1728 mi->overhead += ack_dur;
1729 mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
1731 ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)];
1732 erp = ctl_rate->flags & IEEE80211_RATE_ERP_G;
1733 ack_dur = ieee80211_frame_duration(sband->band, 10,
1734 ctl_rate->bitrate, erp, 1,
1735 ieee80211_chandef_get_shift(chandef));
1736 mi->overhead_legacy = ack_dur;
1737 mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur;
1739 mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
1742 stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
1743 IEEE80211_HT_CAP_RX_STBC_SHIFT;
1745 ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
1747 stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
1748 IEEE80211_VHT_CAP_RXSTBC_SHIFT;
1750 ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
1753 mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1755 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1757 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
1758 u32 gflags = minstrel_mcs_groups[i].flags;
1761 mi->supported[i] = 0;
1762 if (minstrel_ht_is_legacy_group(i))
1765 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1766 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1767 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
1770 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
1775 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1776 sta->deflink.bandwidth < IEEE80211_STA_RX_BW_40)
1779 nss = minstrel_mcs_groups[i].streams;
1781 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
1782 if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
1786 if (gflags & IEEE80211_TX_RC_MCS) {
1787 if (use_vht && minstrel_vht_only)
1790 mi->supported[i] = mcs->rx_mask[nss - 1];
1791 if (mi->supported[i])
1797 if (!vht_cap->vht_supported ||
1798 WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
1799 WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
1802 if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
1803 if (sta->deflink.bandwidth < IEEE80211_STA_RX_BW_80 ||
1804 ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
1805 !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
1810 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1812 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1817 mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
1818 vht_cap->vht_mcs.tx_mcs_map);
1820 if (mi->supported[i])
1824 sta_info = container_of(sta, struct sta_info, sta);
1825 mi->use_short_preamble = test_sta_flag(sta_info, WLAN_STA_SHORT_PREAMBLE) &&
1826 sta_info->sdata->vif.bss_conf.use_short_preamble;
1828 minstrel_ht_update_cck(mp, mi, sband, sta);
1829 minstrel_ht_update_ofdm(mp, mi, sband, sta);
1831 /* create an initial rate table with the lowest supported rates */
1832 minstrel_ht_update_stats(mp, mi);
1833 minstrel_ht_update_rates(mp, mi);
1837 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
1838 struct cfg80211_chan_def *chandef,
1839 struct ieee80211_sta *sta, void *priv_sta)
1841 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1845 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
1846 struct cfg80211_chan_def *chandef,
1847 struct ieee80211_sta *sta, void *priv_sta,
1850 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1854 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1856 struct ieee80211_supported_band *sband;
1857 struct minstrel_ht_sta *mi;
1858 struct minstrel_priv *mp = priv;
1859 struct ieee80211_hw *hw = mp->hw;
1863 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1864 sband = hw->wiphy->bands[i];
1865 if (sband && sband->n_bitrates > max_rates)
1866 max_rates = sband->n_bitrates;
1869 return kzalloc(sizeof(*mi), gfp);
1873 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1879 minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband,
1880 const s16 *bitrates, int n_rates, u32 rate_flags)
1884 for (i = 0; i < sband->n_bitrates; i++) {
1885 struct ieee80211_rate *rate = &sband->bitrates[i];
1887 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1890 for (j = 0; j < n_rates; j++) {
1891 if (rate->bitrate != bitrates[j])
1901 minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
1903 static const s16 bitrates[4] = { 10, 20, 55, 110 };
1904 struct ieee80211_supported_band *sband;
1905 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1907 memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates));
1908 sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
1912 BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates));
1913 minstrel_ht_fill_rate_array(mp->cck_rates, sband,
1914 minstrel_cck_bitrates,
1915 ARRAY_SIZE(minstrel_cck_bitrates),
1920 minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band)
1922 static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
1923 struct ieee80211_supported_band *sband;
1924 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1926 memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band]));
1927 sband = mp->hw->wiphy->bands[band];
1931 BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates));
1932 minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband,
1933 minstrel_ofdm_bitrates,
1934 ARRAY_SIZE(minstrel_ofdm_bitrates),
1939 minstrel_ht_alloc(struct ieee80211_hw *hw)
1941 struct minstrel_priv *mp;
1944 mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
1948 /* contention window settings
1949 * Just an approximation. Using the per-queue values would complicate
1950 * the calculations and is probably unnecessary */
1954 /* maximum time that the hw is allowed to stay in one MRR segment */
1955 mp->segment_size = 6000;
1957 if (hw->max_rate_tries > 0)
1958 mp->max_retry = hw->max_rate_tries;
1960 /* safe default, does not necessarily have to match hw properties */
1963 if (hw->max_rates >= 4)
1967 mp->update_interval = HZ / 20;
1969 minstrel_ht_init_cck_rates(mp);
1970 for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++)
1971 minstrel_ht_init_ofdm_rates(mp, i);
1976 #ifdef CONFIG_MAC80211_DEBUGFS
1977 static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv,
1978 struct dentry *debugfsdir)
1980 struct minstrel_priv *mp = priv;
1982 mp->fixed_rate_idx = (u32) -1;
1983 debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
1984 &mp->fixed_rate_idx);
1989 minstrel_ht_free(void *priv)
1994 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1996 struct minstrel_ht_sta *mi = priv_sta;
1997 int i, j, prob, tp_avg;
1999 i = MI_RATE_GROUP(mi->max_tp_rate[0]);
2000 j = MI_RATE_IDX(mi->max_tp_rate[0]);
2001 prob = mi->groups[i].rates[j].prob_avg;
2003 /* convert tp_avg from pkt per second in kbps */
2004 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
2005 tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
2010 static const struct rate_control_ops mac80211_minstrel_ht = {
2011 .name = "minstrel_ht",
2012 .capa = RATE_CTRL_CAPA_AMPDU_TRIGGER,
2013 .tx_status_ext = minstrel_ht_tx_status,
2014 .get_rate = minstrel_ht_get_rate,
2015 .rate_init = minstrel_ht_rate_init,
2016 .rate_update = minstrel_ht_rate_update,
2017 .alloc_sta = minstrel_ht_alloc_sta,
2018 .free_sta = minstrel_ht_free_sta,
2019 .alloc = minstrel_ht_alloc,
2020 .free = minstrel_ht_free,
2021 #ifdef CONFIG_MAC80211_DEBUGFS
2022 .add_debugfs = minstrel_ht_add_debugfs,
2023 .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
2025 .get_expected_throughput = minstrel_ht_get_expected_throughput,
2029 static void __init init_sample_table(void)
2031 int col, i, new_idx;
2032 u8 rnd[MCS_GROUP_RATES];
2034 memset(sample_table, 0xff, sizeof(sample_table));
2035 for (col = 0; col < SAMPLE_COLUMNS; col++) {
2036 prandom_bytes(rnd, sizeof(rnd));
2037 for (i = 0; i < MCS_GROUP_RATES; i++) {
2038 new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
2039 while (sample_table[col][new_idx] != 0xff)
2040 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
2042 sample_table[col][new_idx] = i;
2048 rc80211_minstrel_init(void)
2050 init_sample_table();
2051 return ieee80211_rate_control_register(&mac80211_minstrel_ht);
2055 rc80211_minstrel_exit(void)
2057 ieee80211_rate_control_unregister(&mac80211_minstrel_ht);