2 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/netdevice.h>
9 #include <linux/types.h>
10 #include <linux/skbuff.h>
11 #include <linux/debugfs.h>
12 #include <linux/random.h>
13 #include <linux/moduleparam.h>
14 #include <linux/ieee80211.h>
15 #include <net/mac80211.h>
18 #include "rc80211_minstrel.h"
19 #include "rc80211_minstrel_ht.h"
21 #define AVG_AMPDU_SIZE 16
22 #define AVG_PKT_SIZE 1200
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 /* MCS rate information for an MCS group */
55 #define MCS_GROUP(_streams, _sgi, _ht40, _s) \
56 [GROUP_IDX(_streams, _sgi, _ht40)] = { \
57 .streams = _streams, \
60 IEEE80211_TX_RC_MCS | \
61 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
62 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
64 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s, \
65 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s, \
66 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s, \
67 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s, \
68 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s, \
69 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s, \
70 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s, \
71 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s \
75 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
76 (MINSTREL_VHT_GROUP_0 + \
77 MINSTREL_MAX_STREAMS * 2 * (_bw) + \
78 MINSTREL_MAX_STREAMS * (_sgi) + \
81 #define BW2VBPS(_bw, r3, r2, r1) \
82 (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
84 #define VHT_GROUP(_streams, _sgi, _bw, _s) \
85 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
86 .streams = _streams, \
89 IEEE80211_TX_RC_VHT_MCS | \
90 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
91 (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \
92 _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
94 MCS_DURATION(_streams, _sgi, \
95 BW2VBPS(_bw, 117, 54, 26)) >> _s, \
96 MCS_DURATION(_streams, _sgi, \
97 BW2VBPS(_bw, 234, 108, 52)) >> _s, \
98 MCS_DURATION(_streams, _sgi, \
99 BW2VBPS(_bw, 351, 162, 78)) >> _s, \
100 MCS_DURATION(_streams, _sgi, \
101 BW2VBPS(_bw, 468, 216, 104)) >> _s, \
102 MCS_DURATION(_streams, _sgi, \
103 BW2VBPS(_bw, 702, 324, 156)) >> _s, \
104 MCS_DURATION(_streams, _sgi, \
105 BW2VBPS(_bw, 936, 432, 208)) >> _s, \
106 MCS_DURATION(_streams, _sgi, \
107 BW2VBPS(_bw, 1053, 486, 234)) >> _s, \
108 MCS_DURATION(_streams, _sgi, \
109 BW2VBPS(_bw, 1170, 540, 260)) >> _s, \
110 MCS_DURATION(_streams, _sgi, \
111 BW2VBPS(_bw, 1404, 648, 312)) >> _s, \
112 MCS_DURATION(_streams, _sgi, \
113 BW2VBPS(_bw, 1560, 720, 346)) >> _s \
117 #define CCK_DURATION(_bitrate, _short, _len) \
118 (1000 * (10 /* SIFS */ + \
119 (_short ? 72 + 24 : 144 + 48) + \
120 (8 * (_len + 4) * 10) / (_bitrate)))
122 #define CCK_ACK_DURATION(_bitrate, _short) \
123 (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \
124 CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE))
126 #define CCK_DURATION_LIST(_short, _s) \
127 CCK_ACK_DURATION(10, _short) >> _s, \
128 CCK_ACK_DURATION(20, _short) >> _s, \
129 CCK_ACK_DURATION(55, _short) >> _s, \
130 CCK_ACK_DURATION(110, _short) >> _s
132 #define CCK_GROUP(_s) \
133 [MINSTREL_CCK_GROUP] = { \
138 CCK_DURATION_LIST(false, _s), \
139 CCK_DURATION_LIST(true, _s) \
143 static bool minstrel_vht_only = true;
144 module_param(minstrel_vht_only, bool, 0644);
145 MODULE_PARM_DESC(minstrel_vht_only,
146 "Use only VHT rates when VHT is supported by sta.");
149 * To enable sufficiently targeted rate sampling, MCS rates are divided into
150 * groups, based on the number of streams and flags (HT40, SGI) that they
153 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
154 * BW -> SGI -> #streams
156 const struct mcs_group minstrel_mcs_groups[] = {
157 MCS_GROUP(1, 0, BW_20, 5),
158 MCS_GROUP(2, 0, BW_20, 4),
159 MCS_GROUP(3, 0, BW_20, 4),
160 MCS_GROUP(4, 0, BW_20, 4),
162 MCS_GROUP(1, 1, BW_20, 5),
163 MCS_GROUP(2, 1, BW_20, 4),
164 MCS_GROUP(3, 1, BW_20, 4),
165 MCS_GROUP(4, 1, BW_20, 4),
167 MCS_GROUP(1, 0, BW_40, 4),
168 MCS_GROUP(2, 0, BW_40, 4),
169 MCS_GROUP(3, 0, BW_40, 4),
170 MCS_GROUP(4, 0, BW_40, 4),
172 MCS_GROUP(1, 1, BW_40, 4),
173 MCS_GROUP(2, 1, BW_40, 4),
174 MCS_GROUP(3, 1, BW_40, 4),
175 MCS_GROUP(4, 1, BW_40, 4),
179 VHT_GROUP(1, 0, BW_20, 5),
180 VHT_GROUP(2, 0, BW_20, 4),
181 VHT_GROUP(3, 0, BW_20, 4),
182 VHT_GROUP(4, 0, BW_20, 4),
184 VHT_GROUP(1, 1, BW_20, 5),
185 VHT_GROUP(2, 1, BW_20, 4),
186 VHT_GROUP(3, 1, BW_20, 4),
187 VHT_GROUP(4, 1, BW_20, 4),
189 VHT_GROUP(1, 0, BW_40, 4),
190 VHT_GROUP(2, 0, BW_40, 4),
191 VHT_GROUP(3, 0, BW_40, 4),
192 VHT_GROUP(4, 0, BW_40, 3),
194 VHT_GROUP(1, 1, BW_40, 4),
195 VHT_GROUP(2, 1, BW_40, 4),
196 VHT_GROUP(3, 1, BW_40, 4),
197 VHT_GROUP(4, 1, BW_40, 3),
199 VHT_GROUP(1, 0, BW_80, 4),
200 VHT_GROUP(2, 0, BW_80, 4),
201 VHT_GROUP(3, 0, BW_80, 4),
202 VHT_GROUP(4, 0, BW_80, 2),
204 VHT_GROUP(1, 1, BW_80, 4),
205 VHT_GROUP(2, 1, BW_80, 4),
206 VHT_GROUP(3, 1, BW_80, 4),
207 VHT_GROUP(4, 1, BW_80, 2),
210 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
213 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
216 * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
217 * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
219 * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
222 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
227 if (nss != 3 && nss != 6)
229 } else if (bw == BW_80) {
230 if (nss == 3 || nss == 7)
235 WARN_ON(bw != BW_40);
238 switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
239 case IEEE80211_VHT_MCS_SUPPORT_0_7:
242 case IEEE80211_VHT_MCS_SUPPORT_0_8:
245 case IEEE80211_VHT_MCS_SUPPORT_0_9:
251 return 0x3ff & ~mask;
255 * Look up an MCS group index based on mac80211 rate information
258 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
260 return GROUP_IDX((rate->idx / 8) + 1,
261 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
262 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
266 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
268 return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
269 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
270 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
271 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
274 static struct minstrel_rate_stats *
275 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
276 struct ieee80211_tx_rate *rate)
280 if (rate->flags & IEEE80211_TX_RC_MCS) {
281 group = minstrel_ht_get_group_idx(rate);
283 } else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
284 group = minstrel_vht_get_group_idx(rate);
285 idx = ieee80211_rate_get_vht_mcs(rate);
287 group = MINSTREL_CCK_GROUP;
289 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++)
290 if (rate->idx == mp->cck_rates[idx])
294 if ((mi->supported[group] & BIT(idx + 4)) &&
295 (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
298 return &mi->groups[group].rates[idx];
301 static inline struct minstrel_rate_stats *
302 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
304 return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
308 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
310 if (!mi->avg_ampdu_len)
311 return AVG_AMPDU_SIZE;
313 return MINSTREL_TRUNC(mi->avg_ampdu_len);
317 * Return current throughput based on the average A-MPDU length, taking into
318 * account the expected number of retransmissions and their expected length
321 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
324 unsigned int nsecs = 0;
326 /* do not account throughput if sucess prob is below 10% */
327 if (prob_ewma < MINSTREL_FRAC(10, 100))
330 if (group != MINSTREL_CCK_GROUP)
331 nsecs = 1000 * mi->overhead / minstrel_ht_avg_ampdu_len(mi);
333 nsecs += minstrel_mcs_groups[group].duration[rate] <<
334 minstrel_mcs_groups[group].shift;
337 * For the throughput calculation, limit the probability value to 90% to
338 * account for collision related packet error rate fluctuation
339 * (prob is scaled - see MINSTREL_FRAC above)
341 if (prob_ewma > MINSTREL_FRAC(90, 100))
342 return MINSTREL_TRUNC(100000 * ((MINSTREL_FRAC(90, 100) * 1000)
345 return MINSTREL_TRUNC(100000 * ((prob_ewma * 1000) / nsecs));
349 * Find & sort topmost throughput rates
351 * If multiple rates provide equal throughput the sorting is based on their
352 * current success probability. Higher success probability is preferred among
353 * MCS groups, CCK rates do not provide aggregation and are therefore at last.
356 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
359 int cur_group, cur_idx, cur_tp_avg, cur_prob;
360 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
361 int j = MAX_THR_RATES;
363 cur_group = index / MCS_GROUP_RATES;
364 cur_idx = index % MCS_GROUP_RATES;
365 cur_prob = mi->groups[cur_group].rates[cur_idx].prob_ewma;
366 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
369 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
370 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
371 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
372 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
374 if (cur_tp_avg < tmp_tp_avg ||
375 (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
380 if (j < MAX_THR_RATES - 1) {
381 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
382 (MAX_THR_RATES - (j + 1))));
384 if (j < MAX_THR_RATES)
389 * Find and set the topmost probability rate per sta and per group
392 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
394 struct minstrel_mcs_group_data *mg;
395 struct minstrel_rate_stats *mrs;
396 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
397 int max_tp_group, cur_tp_avg, cur_group, cur_idx;
398 int max_gpr_group, max_gpr_idx;
399 int max_gpr_tp_avg, max_gpr_prob;
401 cur_group = index / MCS_GROUP_RATES;
402 cur_idx = index % MCS_GROUP_RATES;
403 mg = &mi->groups[index / MCS_GROUP_RATES];
404 mrs = &mg->rates[index % MCS_GROUP_RATES];
406 tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
407 tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
408 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
409 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
411 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
412 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
413 max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
414 if((index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) &&
415 (max_tp_group != MINSTREL_CCK_GROUP))
418 max_gpr_group = mg->max_group_prob_rate / MCS_GROUP_RATES;
419 max_gpr_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
420 max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_ewma;
422 if (mrs->prob_ewma > MINSTREL_FRAC(75, 100)) {
423 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
425 if (cur_tp_avg > tmp_tp_avg)
426 mi->max_prob_rate = index;
428 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
431 if (cur_tp_avg > max_gpr_tp_avg)
432 mg->max_group_prob_rate = index;
434 if (mrs->prob_ewma > tmp_prob)
435 mi->max_prob_rate = index;
436 if (mrs->prob_ewma > max_gpr_prob)
437 mg->max_group_prob_rate = index;
443 * Assign new rate set per sta and use CCK rates only if the fastest
444 * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
445 * rate sets where MCS and CCK rates are mixed, because CCK rates can
446 * not use aggregation.
449 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
450 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
451 u16 tmp_cck_tp_rate[MAX_THR_RATES])
453 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
456 tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES;
457 tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES;
458 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
459 tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
461 tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
462 tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
463 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
464 tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
466 if (tmp_cck_tp > tmp_mcs_tp) {
467 for(i = 0; i < MAX_THR_RATES; i++) {
468 minstrel_ht_sort_best_tp_rates(mi, tmp_cck_tp_rate[i],
476 * Try to increase robustness of max_prob rate by decrease number of
477 * streams if possible.
480 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
482 struct minstrel_mcs_group_data *mg;
483 int tmp_max_streams, group, tmp_idx, tmp_prob;
486 tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
487 MCS_GROUP_RATES].streams;
488 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
489 mg = &mi->groups[group];
490 if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
493 tmp_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
494 tmp_prob = mi->groups[group].rates[tmp_idx].prob_ewma;
496 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
497 (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
498 mi->max_prob_rate = mg->max_group_prob_rate;
499 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
507 * Update rate statistics and select new primary rates
509 * Rules for rate selection:
510 * - max_prob_rate must use only one stream, as a tradeoff between delivery
511 * probability and throughput during strong fluctuations
512 * - as long as the max prob rate has a probability of more than 75%, pick
513 * higher throughput rates, even if the probablity is a bit lower
516 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
518 struct minstrel_mcs_group_data *mg;
519 struct minstrel_rate_stats *mrs;
520 int group, i, j, cur_prob;
521 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
522 u16 tmp_cck_tp_rate[MAX_THR_RATES], index;
524 if (mi->ampdu_packets > 0) {
525 if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
526 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
527 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
530 mi->avg_ampdu_len = 0;
532 mi->ampdu_packets = 0;
536 mi->sample_count = 0;
538 /* Initialize global rate indexes */
539 for(j = 0; j < MAX_THR_RATES; j++){
540 tmp_mcs_tp_rate[j] = 0;
541 tmp_cck_tp_rate[j] = 0;
544 /* Find best rate sets within all MCS groups*/
545 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
547 mg = &mi->groups[group];
548 if (!mi->supported[group])
553 /* (re)Initialize group rate indexes */
554 for(j = 0; j < MAX_THR_RATES; j++)
555 tmp_group_tp_rate[j] = group;
557 for (i = 0; i < MCS_GROUP_RATES; i++) {
558 if (!(mi->supported[group] & BIT(i)))
561 index = MCS_GROUP_RATES * group + i;
564 mrs->retry_updated = false;
565 minstrel_calc_rate_stats(mrs);
566 cur_prob = mrs->prob_ewma;
568 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
571 /* Find max throughput rate set */
572 if (group != MINSTREL_CCK_GROUP) {
573 minstrel_ht_sort_best_tp_rates(mi, index,
575 } else if (group == MINSTREL_CCK_GROUP) {
576 minstrel_ht_sort_best_tp_rates(mi, index,
580 /* Find max throughput rate set within a group */
581 minstrel_ht_sort_best_tp_rates(mi, index,
584 /* Find max probability rate per group and global */
585 minstrel_ht_set_best_prob_rate(mi, index);
588 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
589 sizeof(mg->max_group_tp_rate));
592 /* Assign new rate set per sta */
593 minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, tmp_cck_tp_rate);
594 memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
596 /* Try to increase robustness of max_prob_rate*/
597 minstrel_ht_prob_rate_reduce_streams(mi);
599 /* try to sample all available rates during each interval */
600 mi->sample_count *= 8;
602 #ifdef CONFIG_MAC80211_DEBUGFS
603 /* use fixed index if set */
604 if (mp->fixed_rate_idx != -1) {
605 for (i = 0; i < 4; i++)
606 mi->max_tp_rate[i] = mp->fixed_rate_idx;
607 mi->max_prob_rate = mp->fixed_rate_idx;
611 /* Reset update timer */
612 mi->last_stats_update = jiffies;
616 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
624 if (rate->flags & IEEE80211_TX_RC_MCS ||
625 rate->flags & IEEE80211_TX_RC_VHT_MCS)
628 return rate->idx == mp->cck_rates[0] ||
629 rate->idx == mp->cck_rates[1] ||
630 rate->idx == mp->cck_rates[2] ||
631 rate->idx == mp->cck_rates[3];
635 minstrel_set_next_sample_idx(struct minstrel_ht_sta *mi)
637 struct minstrel_mcs_group_data *mg;
641 mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
642 mg = &mi->groups[mi->sample_group];
644 if (!mi->supported[mi->sample_group])
647 if (++mg->index >= MCS_GROUP_RATES) {
649 if (++mg->column >= ARRAY_SIZE(sample_table))
657 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
659 int group, orig_group;
661 orig_group = group = *idx / MCS_GROUP_RATES;
665 if (!mi->supported[group])
668 if (minstrel_mcs_groups[group].streams >
669 minstrel_mcs_groups[orig_group].streams)
673 *idx = mi->groups[group].max_group_tp_rate[0];
675 *idx = mi->groups[group].max_group_tp_rate[1];
681 minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
683 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
684 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
687 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
690 if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
693 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
696 tid = ieee80211_get_tid(hdr);
697 if (likely(sta->ampdu_mlme.tid_tx[tid]))
700 ieee80211_start_tx_ba_session(pubsta, tid, 0);
704 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
705 void *priv_sta, struct ieee80211_tx_status *st)
707 struct ieee80211_tx_info *info = st->info;
708 struct minstrel_ht_sta_priv *msp = priv_sta;
709 struct minstrel_ht_sta *mi = &msp->ht;
710 struct ieee80211_tx_rate *ar = info->status.rates;
711 struct minstrel_rate_stats *rate, *rate2;
712 struct minstrel_priv *mp = priv;
713 bool last, update = false;
717 return mac80211_minstrel.tx_status_ext(priv, sband,
720 /* This packet was aggregated but doesn't carry status info */
721 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
722 !(info->flags & IEEE80211_TX_STAT_AMPDU))
725 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
726 info->status.ampdu_ack_len =
727 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
728 info->status.ampdu_len = 1;
732 mi->ampdu_len += info->status.ampdu_len;
734 if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
735 int avg_ampdu_len = minstrel_ht_avg_ampdu_len(mi);
737 mi->sample_wait = 16 + 2 * avg_ampdu_len;
738 mi->sample_tries = 1;
742 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
743 mi->sample_packets += info->status.ampdu_len;
745 last = !minstrel_ht_txstat_valid(mp, &ar[0]);
746 for (i = 0; !last; i++) {
747 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
748 !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
750 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
753 rate->success += info->status.ampdu_ack_len;
755 rate->attempts += ar[i].count * info->status.ampdu_len;
759 * check for sudden death of spatial multiplexing,
760 * downgrade to a lower number of streams if necessary.
762 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
763 if (rate->attempts > 30 &&
764 MINSTREL_FRAC(rate->success, rate->attempts) <
765 MINSTREL_FRAC(20, 100)) {
766 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
770 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
771 if (rate2->attempts > 30 &&
772 MINSTREL_FRAC(rate2->success, rate2->attempts) <
773 MINSTREL_FRAC(20, 100)) {
774 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
778 if (time_after(jiffies, mi->last_stats_update +
779 (mp->update_interval / 2 * HZ) / 1000)) {
781 minstrel_ht_update_stats(mp, mi);
785 minstrel_ht_update_rates(mp, mi);
789 minstrel_get_duration(int index)
791 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
792 unsigned int duration = group->duration[index % MCS_GROUP_RATES];
793 return duration << group->shift;
797 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
800 struct minstrel_rate_stats *mrs;
801 unsigned int tx_time, tx_time_rtscts, tx_time_data;
802 unsigned int cw = mp->cw_min;
803 unsigned int ctime = 0;
804 unsigned int t_slot = 9; /* FIXME */
805 unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
806 unsigned int overhead = 0, overhead_rtscts = 0;
808 mrs = minstrel_get_ratestats(mi, index);
809 if (mrs->prob_ewma < MINSTREL_FRAC(1, 10)) {
810 mrs->retry_count = 1;
811 mrs->retry_count_rtscts = 1;
815 mrs->retry_count = 2;
816 mrs->retry_count_rtscts = 2;
817 mrs->retry_updated = true;
819 tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
821 /* Contention time for first 2 tries */
822 ctime = (t_slot * cw) >> 1;
823 cw = min((cw << 1) | 1, mp->cw_max);
824 ctime += (t_slot * cw) >> 1;
825 cw = min((cw << 1) | 1, mp->cw_max);
827 if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
828 overhead = mi->overhead;
829 overhead_rtscts = mi->overhead_rtscts;
832 /* Total TX time for data and Contention after first 2 tries */
833 tx_time = ctime + 2 * (overhead + tx_time_data);
834 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
836 /* See how many more tries we can fit inside segment size */
838 /* Contention time for this try */
839 ctime = (t_slot * cw) >> 1;
840 cw = min((cw << 1) | 1, mp->cw_max);
842 /* Total TX time after this try */
843 tx_time += ctime + overhead + tx_time_data;
844 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
846 if (tx_time_rtscts < mp->segment_size)
847 mrs->retry_count_rtscts++;
848 } while ((tx_time < mp->segment_size) &&
849 (++mrs->retry_count < mp->max_retry));
854 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
855 struct ieee80211_sta_rates *ratetbl, int offset, int index)
857 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
858 struct minstrel_rate_stats *mrs;
860 u16 flags = group->flags;
862 mrs = minstrel_get_ratestats(mi, index);
863 if (!mrs->retry_updated)
864 minstrel_calc_retransmit(mp, mi, index);
866 if (mrs->prob_ewma < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
867 ratetbl->rate[offset].count = 2;
868 ratetbl->rate[offset].count_rts = 2;
869 ratetbl->rate[offset].count_cts = 2;
871 ratetbl->rate[offset].count = mrs->retry_count;
872 ratetbl->rate[offset].count_cts = mrs->retry_count;
873 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
876 if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP)
877 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
878 else if (flags & IEEE80211_TX_RC_VHT_MCS)
879 idx = ((group->streams - 1) << 4) |
880 ((index % MCS_GROUP_RATES) & 0xF);
882 idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
884 /* enable RTS/CTS if needed:
885 * - if station is in dynamic SMPS (and streams > 1)
886 * - for fallback rates, to increase chances of getting through
889 (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
890 group->streams > 1)) {
891 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
892 flags |= IEEE80211_TX_RC_USE_RTS_CTS;
895 ratetbl->rate[offset].idx = idx;
896 ratetbl->rate[offset].flags = flags;
900 minstrel_ht_get_prob_ewma(struct minstrel_ht_sta *mi, int rate)
902 int group = rate / MCS_GROUP_RATES;
903 rate %= MCS_GROUP_RATES;
904 return mi->groups[group].rates[rate].prob_ewma;
908 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
910 int group = mi->max_prob_rate / MCS_GROUP_RATES;
911 const struct mcs_group *g = &minstrel_mcs_groups[group];
912 int rate = mi->max_prob_rate % MCS_GROUP_RATES;
913 unsigned int duration;
915 /* Disable A-MSDU if max_prob_rate is bad */
916 if (mi->groups[group].rates[rate].prob_ewma < MINSTREL_FRAC(50, 100))
919 duration = g->duration[rate];
920 duration <<= g->shift;
922 /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
923 if (duration > MCS_DURATION(1, 0, 52))
927 * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
930 if (duration > MCS_DURATION(1, 0, 104))
934 * If the rate is slower than single-stream MCS7, or if the max throughput
935 * rate success probability is less than 75%, limit A-MSDU to twice the usual
938 if (duration > MCS_DURATION(1, 0, 260) ||
939 (minstrel_ht_get_prob_ewma(mi, mi->max_tp_rate[0]) <
940 MINSTREL_FRAC(75, 100)))
944 * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
945 * Since aggregation sessions are started/stopped without txq flush, use
946 * the limit here to avoid the complexity of having to de-aggregate
947 * packets in the queue.
949 if (!mi->sta->vht_cap.vht_supported)
950 return IEEE80211_MAX_MPDU_LEN_HT_BA;
957 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
959 struct ieee80211_sta_rates *rates;
962 rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
966 /* Start with max_tp_rate[0] */
967 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
969 if (mp->hw->max_rates >= 3) {
970 /* At least 3 tx rates supported, use max_tp_rate[1] next */
971 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
974 if (mp->hw->max_rates >= 2) {
976 * At least 2 tx rates supported, use max_prob_rate next */
977 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
980 mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
981 rates->rate[i].idx = -1;
982 rate_control_set_rates(mp->hw, mi->sta, rates);
986 minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
988 struct minstrel_rate_stats *mrs;
989 struct minstrel_mcs_group_data *mg;
990 unsigned int sample_dur, sample_group, cur_max_tp_streams;
991 int tp_rate1, tp_rate2;
994 if (mi->sample_wait > 0) {
999 if (!mi->sample_tries)
1002 sample_group = mi->sample_group;
1003 mg = &mi->groups[sample_group];
1004 sample_idx = sample_table[mg->column][mg->index];
1005 minstrel_set_next_sample_idx(mi);
1007 if (!(mi->supported[sample_group] & BIT(sample_idx)))
1010 mrs = &mg->rates[sample_idx];
1011 sample_idx += sample_group * MCS_GROUP_RATES;
1013 /* Set tp_rate1, tp_rate2 to the highest / second highest max_tp_rate */
1014 if (minstrel_get_duration(mi->max_tp_rate[0]) >
1015 minstrel_get_duration(mi->max_tp_rate[1])) {
1016 tp_rate1 = mi->max_tp_rate[1];
1017 tp_rate2 = mi->max_tp_rate[0];
1019 tp_rate1 = mi->max_tp_rate[0];
1020 tp_rate2 = mi->max_tp_rate[1];
1024 * Sampling might add some overhead (RTS, no aggregation)
1025 * to the frame. Hence, don't use sampling for the highest currently
1026 * used highest throughput or probability rate.
1028 if (sample_idx == mi->max_tp_rate[0] || sample_idx == mi->max_prob_rate)
1032 * Do not sample if the probability is already higher than 95%,
1033 * or if the rate is 3 times slower than the current max probability
1034 * rate, to avoid wasting airtime.
1036 sample_dur = minstrel_get_duration(sample_idx);
1037 if (mrs->prob_ewma > MINSTREL_FRAC(95, 100) ||
1038 minstrel_get_duration(mi->max_prob_rate) * 3 < sample_dur)
1042 * Make sure that lower rates get sampled only occasionally,
1043 * if the link is working perfectly.
1046 cur_max_tp_streams = minstrel_mcs_groups[tp_rate1 /
1047 MCS_GROUP_RATES].streams;
1048 if (sample_dur >= minstrel_get_duration(tp_rate2) &&
1049 (cur_max_tp_streams - 1 <
1050 minstrel_mcs_groups[sample_group].streams ||
1051 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
1052 if (mrs->sample_skipped < 20)
1055 if (mi->sample_slow++ > 2)
1064 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1065 struct ieee80211_tx_rate_control *txrc)
1067 const struct mcs_group *sample_group;
1068 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1069 struct ieee80211_tx_rate *rate = &info->status.rates[0];
1070 struct minstrel_ht_sta_priv *msp = priv_sta;
1071 struct minstrel_ht_sta *mi = &msp->ht;
1072 struct minstrel_priv *mp = priv;
1075 if (rate_control_send_low(sta, priv_sta, txrc))
1079 return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
1081 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
1082 mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
1083 minstrel_aggr_check(sta, txrc->skb);
1085 info->flags |= mi->tx_flags;
1087 #ifdef CONFIG_MAC80211_DEBUGFS
1088 if (mp->fixed_rate_idx != -1)
1092 /* Don't use EAPOL frames for sampling on non-mrr hw */
1093 if (mp->hw->max_rates == 1 &&
1094 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
1097 sample_idx = minstrel_get_sample_rate(mp, mi);
1099 mi->total_packets++;
1102 if (mi->total_packets == ~0) {
1103 mi->total_packets = 0;
1104 mi->sample_packets = 0;
1110 sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
1111 sample_idx %= MCS_GROUP_RATES;
1113 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
1114 (sample_idx >= 4) != txrc->short_preamble)
1117 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1120 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
1121 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
1122 rate->idx = mp->cck_rates[idx];
1123 } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
1124 ieee80211_rate_set_vht(rate, sample_idx % MCS_GROUP_RATES,
1125 sample_group->streams);
1127 rate->idx = sample_idx + (sample_group->streams - 1) * 8;
1130 rate->flags = sample_group->flags;
1134 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1135 struct ieee80211_supported_band *sband,
1136 struct ieee80211_sta *sta)
1140 if (sband->band != NL80211_BAND_2GHZ)
1143 if (!ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
1146 mi->cck_supported = 0;
1147 mi->cck_supported_short = 0;
1148 for (i = 0; i < 4; i++) {
1149 if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
1152 mi->cck_supported |= BIT(i);
1153 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1154 mi->cck_supported_short |= BIT(i);
1157 mi->supported[MINSTREL_CCK_GROUP] = mi->cck_supported;
1161 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1162 struct cfg80211_chan_def *chandef,
1163 struct ieee80211_sta *sta, void *priv_sta)
1165 struct minstrel_priv *mp = priv;
1166 struct minstrel_ht_sta_priv *msp = priv_sta;
1167 struct minstrel_ht_sta *mi = &msp->ht;
1168 struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
1169 u16 ht_cap = sta->ht_cap.cap;
1170 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1172 int n_supported = 0;
1178 /* fall back to the old minstrel for legacy stations */
1179 if (!sta->ht_cap.ht_supported)
1182 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
1184 if (vht_cap->vht_supported)
1185 use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
1190 memset(mi, 0, sizeof(*mi));
1193 mi->last_stats_update = jiffies;
1195 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1196 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
1197 mi->overhead += ack_dur;
1198 mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
1200 mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
1202 /* When using MRR, sample more on the first attempt, without delay */
1204 mi->sample_count = 16;
1205 mi->sample_wait = 0;
1207 mi->sample_count = 8;
1208 mi->sample_wait = 8;
1210 mi->sample_tries = 4;
1213 stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
1214 IEEE80211_HT_CAP_RX_STBC_SHIFT;
1216 ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
1218 stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
1219 IEEE80211_VHT_CAP_RXSTBC_SHIFT;
1221 ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
1224 mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1226 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1228 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
1229 u32 gflags = minstrel_mcs_groups[i].flags;
1232 mi->supported[i] = 0;
1233 if (i == MINSTREL_CCK_GROUP) {
1234 minstrel_ht_update_cck(mp, mi, sband, sta);
1238 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1239 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1240 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
1243 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
1248 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1249 sta->bandwidth < IEEE80211_STA_RX_BW_40)
1252 nss = minstrel_mcs_groups[i].streams;
1254 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
1255 if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
1259 if (gflags & IEEE80211_TX_RC_MCS) {
1260 if (use_vht && minstrel_vht_only)
1263 mi->supported[i] = mcs->rx_mask[nss - 1];
1264 if (mi->supported[i])
1270 if (!vht_cap->vht_supported ||
1271 WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
1272 WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
1275 if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
1276 if (sta->bandwidth < IEEE80211_STA_RX_BW_80 ||
1277 ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
1278 !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
1283 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1285 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1290 mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
1291 vht_cap->vht_mcs.tx_mcs_map);
1293 if (mi->supported[i])
1300 mi->supported[MINSTREL_CCK_GROUP] |= mi->cck_supported_short << 4;
1302 /* create an initial rate table with the lowest supported rates */
1303 minstrel_ht_update_stats(mp, mi);
1304 minstrel_ht_update_rates(mp, mi);
1310 memset(&msp->legacy, 0, sizeof(msp->legacy));
1311 msp->legacy.r = msp->ratelist;
1312 msp->legacy.sample_table = msp->sample_table;
1313 return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
1318 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
1319 struct cfg80211_chan_def *chandef,
1320 struct ieee80211_sta *sta, void *priv_sta)
1322 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1326 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
1327 struct cfg80211_chan_def *chandef,
1328 struct ieee80211_sta *sta, void *priv_sta,
1331 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1335 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1337 struct ieee80211_supported_band *sband;
1338 struct minstrel_ht_sta_priv *msp;
1339 struct minstrel_priv *mp = priv;
1340 struct ieee80211_hw *hw = mp->hw;
1344 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1345 sband = hw->wiphy->bands[i];
1346 if (sband && sband->n_bitrates > max_rates)
1347 max_rates = sband->n_bitrates;
1350 msp = kzalloc(sizeof(*msp), gfp);
1354 msp->ratelist = kcalloc(max_rates, sizeof(struct minstrel_rate), gfp);
1358 msp->sample_table = kmalloc_array(max_rates, SAMPLE_COLUMNS, gfp);
1359 if (!msp->sample_table)
1365 kfree(msp->ratelist);
1372 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1374 struct minstrel_ht_sta_priv *msp = priv_sta;
1376 kfree(msp->sample_table);
1377 kfree(msp->ratelist);
1382 minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
1384 static const int bitrates[4] = { 10, 20, 55, 110 };
1385 struct ieee80211_supported_band *sband;
1386 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1389 sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
1393 for (i = 0; i < sband->n_bitrates; i++) {
1394 struct ieee80211_rate *rate = &sband->bitrates[i];
1396 if (rate->flags & IEEE80211_RATE_ERP_G)
1399 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1402 for (j = 0; j < ARRAY_SIZE(bitrates); j++) {
1403 if (rate->bitrate != bitrates[j])
1406 mp->cck_rates[j] = i;
1413 minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
1415 struct minstrel_priv *mp;
1417 mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
1421 /* contention window settings
1422 * Just an approximation. Using the per-queue values would complicate
1423 * the calculations and is probably unnecessary */
1427 /* number of packets (in %) to use for sampling other rates
1428 * sample less often for non-mrr packets, because the overhead
1429 * is much higher than with mrr */
1430 mp->lookaround_rate = 5;
1431 mp->lookaround_rate_mrr = 10;
1433 /* maximum time that the hw is allowed to stay in one MRR segment */
1434 mp->segment_size = 6000;
1436 if (hw->max_rate_tries > 0)
1437 mp->max_retry = hw->max_rate_tries;
1439 /* safe default, does not necessarily have to match hw properties */
1442 if (hw->max_rates >= 4)
1446 mp->update_interval = 100;
1448 #ifdef CONFIG_MAC80211_DEBUGFS
1449 mp->fixed_rate_idx = (u32) -1;
1450 debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
1451 &mp->fixed_rate_idx);
1454 minstrel_ht_init_cck_rates(mp);
1460 minstrel_ht_free(void *priv)
1465 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1467 struct minstrel_ht_sta_priv *msp = priv_sta;
1468 struct minstrel_ht_sta *mi = &msp->ht;
1469 int i, j, prob, tp_avg;
1472 return mac80211_minstrel.get_expected_throughput(priv_sta);
1474 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1475 j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
1476 prob = mi->groups[i].rates[j].prob_ewma;
1478 /* convert tp_avg from pkt per second in kbps */
1479 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
1480 tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
1485 static const struct rate_control_ops mac80211_minstrel_ht = {
1486 .name = "minstrel_ht",
1487 .tx_status_ext = minstrel_ht_tx_status,
1488 .get_rate = minstrel_ht_get_rate,
1489 .rate_init = minstrel_ht_rate_init,
1490 .rate_update = minstrel_ht_rate_update,
1491 .alloc_sta = minstrel_ht_alloc_sta,
1492 .free_sta = minstrel_ht_free_sta,
1493 .alloc = minstrel_ht_alloc,
1494 .free = minstrel_ht_free,
1495 #ifdef CONFIG_MAC80211_DEBUGFS
1496 .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
1498 .get_expected_throughput = minstrel_ht_get_expected_throughput,
1502 static void __init init_sample_table(void)
1504 int col, i, new_idx;
1505 u8 rnd[MCS_GROUP_RATES];
1507 memset(sample_table, 0xff, sizeof(sample_table));
1508 for (col = 0; col < SAMPLE_COLUMNS; col++) {
1509 prandom_bytes(rnd, sizeof(rnd));
1510 for (i = 0; i < MCS_GROUP_RATES; i++) {
1511 new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
1512 while (sample_table[col][new_idx] != 0xff)
1513 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
1515 sample_table[col][new_idx] = i;
1521 rc80211_minstrel_init(void)
1523 init_sample_table();
1524 return ieee80211_rate_control_register(&mac80211_minstrel_ht);
1528 rc80211_minstrel_exit(void)
1530 ieee80211_rate_control_unregister(&mac80211_minstrel_ht);