Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1 | /* |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 2 | * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org> |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 3 | * |
| 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. |
| 7 | */ |
| 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/ieee80211.h> |
| 14 | #include <net/mac80211.h> |
| 15 | #include "rate.h" |
| 16 | #include "rc80211_minstrel.h" |
| 17 | #include "rc80211_minstrel_ht.h" |
| 18 | |
| 19 | #define AVG_PKT_SIZE 1200 |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 20 | |
| 21 | /* Number of bits for an average sized packet */ |
| 22 | #define MCS_NBITS (AVG_PKT_SIZE << 3) |
| 23 | |
| 24 | /* Number of symbols for a packet with (bps) bits per symbol */ |
Johannes Berg | 00591ce | 2014-05-19 11:24:19 +0200 | [diff] [blame] | 25 | #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps)) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 26 | |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 27 | /* Transmission time (nanoseconds) for a packet containing (syms) symbols */ |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 28 | #define MCS_SYMBOL_TIME(sgi, syms) \ |
| 29 | (sgi ? \ |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 30 | ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \ |
| 31 | ((syms) * 1000) << 2 /* syms * 4 us */ \ |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | /* Transmit duration for the raw data part of an average sized packet */ |
| 35 | #define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) |
| 36 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 37 | #define BW_20 0 |
| 38 | #define BW_40 1 |
| 39 | |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Define group sort order: HT40 -> SGI -> #streams |
| 42 | */ |
| 43 | #define GROUP_IDX(_streams, _sgi, _ht40) \ |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 44 | MINSTREL_HT_GROUP_0 + \ |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 45 | MINSTREL_MAX_STREAMS * 2 * _ht40 + \ |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 46 | MINSTREL_MAX_STREAMS * _sgi + \ |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 47 | _streams - 1 |
| 48 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 49 | /* MCS rate information for an MCS group */ |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 50 | #define MCS_GROUP(_streams, _sgi, _ht40) \ |
| 51 | [GROUP_IDX(_streams, _sgi, _ht40)] = { \ |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 52 | .streams = _streams, \ |
| 53 | .flags = \ |
| 54 | (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \ |
| 55 | (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \ |
| 56 | .duration = { \ |
| 57 | MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \ |
| 58 | MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \ |
| 59 | MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \ |
| 60 | MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \ |
| 61 | MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \ |
| 62 | MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \ |
| 63 | MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \ |
| 64 | MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \ |
| 65 | } \ |
| 66 | } |
| 67 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 68 | #define CCK_DURATION(_bitrate, _short, _len) \ |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 69 | (1000 * (10 /* SIFS */ + \ |
Weilong Chen | f359d3f | 2013-12-18 15:44:16 +0800 | [diff] [blame] | 70 | (_short ? 72 + 24 : 144 + 48) + \ |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 71 | (8 * (_len + 4) * 10) / (_bitrate))) |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 72 | |
| 73 | #define CCK_ACK_DURATION(_bitrate, _short) \ |
| 74 | (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \ |
| 75 | CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE)) |
| 76 | |
| 77 | #define CCK_DURATION_LIST(_short) \ |
| 78 | CCK_ACK_DURATION(10, _short), \ |
| 79 | CCK_ACK_DURATION(20, _short), \ |
| 80 | CCK_ACK_DURATION(55, _short), \ |
| 81 | CCK_ACK_DURATION(110, _short) |
| 82 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 83 | #define CCK_GROUP \ |
| 84 | [MINSTREL_CCK_GROUP] = { \ |
| 85 | .streams = 0, \ |
| 86 | .duration = { \ |
| 87 | CCK_DURATION_LIST(false), \ |
| 88 | CCK_DURATION_LIST(true) \ |
| 89 | } \ |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 92 | /* |
| 93 | * To enable sufficiently targeted rate sampling, MCS rates are divided into |
| 94 | * groups, based on the number of streams and flags (HT40, SGI) that they |
| 95 | * use. |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 96 | * |
| 97 | * Sortorder has to be fixed for GROUP_IDX macro to be applicable: |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 98 | * BW -> SGI -> #streams |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 99 | */ |
| 100 | const struct mcs_group minstrel_mcs_groups[] = { |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 101 | MCS_GROUP(1, 0, BW_20), |
| 102 | MCS_GROUP(2, 0, BW_20), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 103 | #if MINSTREL_MAX_STREAMS >= 3 |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 104 | MCS_GROUP(3, 0, BW_20), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 105 | #endif |
| 106 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 107 | MCS_GROUP(1, 1, BW_20), |
| 108 | MCS_GROUP(2, 1, BW_20), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 109 | #if MINSTREL_MAX_STREAMS >= 3 |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 110 | MCS_GROUP(3, 1, BW_20), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 111 | #endif |
| 112 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 113 | MCS_GROUP(1, 0, BW_40), |
| 114 | MCS_GROUP(2, 0, BW_40), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 115 | #if MINSTREL_MAX_STREAMS >= 3 |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 116 | MCS_GROUP(3, 0, BW_40), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 117 | #endif |
| 118 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 119 | MCS_GROUP(1, 1, BW_40), |
| 120 | MCS_GROUP(2, 1, BW_40), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 121 | #if MINSTREL_MAX_STREAMS >= 3 |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 122 | MCS_GROUP(3, 1, BW_40), |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 123 | #endif |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 124 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 125 | CCK_GROUP |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 128 | |
Johannes Berg | f6e1a73 | 2014-01-21 00:32:52 +0100 | [diff] [blame] | 129 | static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 130 | |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 131 | static void |
| 132 | minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi); |
| 133 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 134 | /* |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 135 | * Look up an MCS group index based on mac80211 rate information |
| 136 | */ |
| 137 | static int |
| 138 | minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate) |
| 139 | { |
Karl Beldan | cc61d8d | 2014-09-29 02:36:30 +0200 | [diff] [blame] | 140 | return GROUP_IDX((rate->idx / 8) + 1, |
Helmut Schaa | a5f69d94 | 2011-11-14 15:28:20 +0100 | [diff] [blame] | 141 | !!(rate->flags & IEEE80211_TX_RC_SHORT_GI), |
| 142 | !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 145 | static struct minstrel_rate_stats * |
| 146 | minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, |
| 147 | struct ieee80211_tx_rate *rate) |
| 148 | { |
| 149 | int group, idx; |
| 150 | |
| 151 | if (rate->flags & IEEE80211_TX_RC_MCS) { |
| 152 | group = minstrel_ht_get_group_idx(rate); |
Karl Beldan | 7a5e3fa | 2013-11-11 13:12:55 +0100 | [diff] [blame] | 153 | idx = rate->idx % 8; |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 154 | } else { |
| 155 | group = MINSTREL_CCK_GROUP; |
| 156 | |
| 157 | for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) |
| 158 | if (rate->idx == mp->cck_rates[idx]) |
| 159 | break; |
| 160 | |
| 161 | /* short preamble */ |
| 162 | if (!(mi->groups[group].supported & BIT(idx))) |
| 163 | idx += 4; |
| 164 | } |
| 165 | return &mi->groups[group].rates[idx]; |
| 166 | } |
| 167 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 168 | static inline struct minstrel_rate_stats * |
| 169 | minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index) |
| 170 | { |
| 171 | return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES]; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | * Recalculate success probabilities and counters for a rate using EWMA |
| 177 | */ |
| 178 | static void |
Patrick Kelle | 6048d763 | 2011-11-15 16:44:48 +0100 | [diff] [blame] | 179 | minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 180 | { |
| 181 | if (unlikely(mr->attempts > 0)) { |
| 182 | mr->sample_skipped = 0; |
| 183 | mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts); |
| 184 | if (!mr->att_hist) |
| 185 | mr->probability = mr->cur_prob; |
| 186 | else |
| 187 | mr->probability = minstrel_ewma(mr->probability, |
| 188 | mr->cur_prob, EWMA_LEVEL); |
| 189 | mr->att_hist += mr->attempts; |
| 190 | mr->succ_hist += mr->success; |
| 191 | } else { |
| 192 | mr->sample_skipped++; |
| 193 | } |
| 194 | mr->last_success = mr->success; |
| 195 | mr->last_attempts = mr->attempts; |
| 196 | mr->success = 0; |
| 197 | mr->attempts = 0; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Calculate throughput based on the average A-MPDU length, taking into account |
| 202 | * the expected number of retransmissions and their expected length |
| 203 | */ |
| 204 | static void |
Patrick Kelle | 6048d763 | 2011-11-15 16:44:48 +0100 | [diff] [blame] | 205 | minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 206 | { |
| 207 | struct minstrel_rate_stats *mr; |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 208 | unsigned int nsecs = 0; |
| 209 | unsigned int tp; |
Felix Fietkau | 3e8b1eb | 2013-03-16 17:00:25 +0100 | [diff] [blame] | 210 | unsigned int prob; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 211 | |
| 212 | mr = &mi->groups[group].rates[rate]; |
Felix Fietkau | 3e8b1eb | 2013-03-16 17:00:25 +0100 | [diff] [blame] | 213 | prob = mr->probability; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 214 | |
Felix Fietkau | 3e8b1eb | 2013-03-16 17:00:25 +0100 | [diff] [blame] | 215 | if (prob < MINSTREL_FRAC(1, 10)) { |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 216 | mr->cur_tp = 0; |
| 217 | return; |
| 218 | } |
| 219 | |
Felix Fietkau | 3e8b1eb | 2013-03-16 17:00:25 +0100 | [diff] [blame] | 220 | /* |
| 221 | * For the throughput calculation, limit the probability value to 90% to |
| 222 | * account for collision related packet error rate fluctuation |
| 223 | */ |
| 224 | if (prob > MINSTREL_FRAC(9, 10)) |
| 225 | prob = MINSTREL_FRAC(9, 10); |
| 226 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 227 | if (group != MINSTREL_CCK_GROUP) |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 228 | nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len); |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 229 | |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 230 | nsecs += minstrel_mcs_groups[group].duration[rate]; |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 231 | |
Johannes Berg | 00591ce | 2014-05-19 11:24:19 +0200 | [diff] [blame] | 232 | /* prob is scaled - see MINSTREL_FRAC above */ |
| 233 | tp = 1000000 * ((prob * 1000) / nsecs); |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 234 | mr->cur_tp = MINSTREL_TRUNC(tp); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 238 | * Find & sort topmost throughput rates |
| 239 | * |
| 240 | * If multiple rates provide equal throughput the sorting is based on their |
| 241 | * current success probability. Higher success probability is preferred among |
| 242 | * MCS groups, CCK rates do not provide aggregation and are therefore at last. |
| 243 | */ |
| 244 | static void |
Karl Beldan | d4d141c | 2014-10-20 15:45:59 +0200 | [diff] [blame] | 245 | minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index, |
| 246 | u16 *tp_list) |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 247 | { |
| 248 | int cur_group, cur_idx, cur_thr, cur_prob; |
| 249 | int tmp_group, tmp_idx, tmp_thr, tmp_prob; |
| 250 | int j = MAX_THR_RATES; |
| 251 | |
| 252 | cur_group = index / MCS_GROUP_RATES; |
| 253 | cur_idx = index % MCS_GROUP_RATES; |
| 254 | cur_thr = mi->groups[cur_group].rates[cur_idx].cur_tp; |
| 255 | cur_prob = mi->groups[cur_group].rates[cur_idx].probability; |
| 256 | |
| 257 | tmp_group = tp_list[j - 1] / MCS_GROUP_RATES; |
| 258 | tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES; |
| 259 | tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp; |
| 260 | tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability; |
| 261 | |
| 262 | while (j > 0 && (cur_thr > tmp_thr || |
| 263 | (cur_thr == tmp_thr && cur_prob > tmp_prob))) { |
| 264 | j--; |
| 265 | tmp_group = tp_list[j - 1] / MCS_GROUP_RATES; |
| 266 | tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES; |
| 267 | tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp; |
| 268 | tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability; |
| 269 | } |
| 270 | |
| 271 | if (j < MAX_THR_RATES - 1) { |
| 272 | memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) * |
| 273 | (MAX_THR_RATES - (j + 1)))); |
| 274 | } |
| 275 | if (j < MAX_THR_RATES) |
| 276 | tp_list[j] = index; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Find and set the topmost probability rate per sta and per group |
| 281 | */ |
| 282 | static void |
Karl Beldan | d4d141c | 2014-10-20 15:45:59 +0200 | [diff] [blame] | 283 | minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index) |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 284 | { |
| 285 | struct minstrel_mcs_group_data *mg; |
| 286 | struct minstrel_rate_stats *mr; |
| 287 | int tmp_group, tmp_idx, tmp_tp, tmp_prob, max_tp_group; |
| 288 | |
| 289 | mg = &mi->groups[index / MCS_GROUP_RATES]; |
| 290 | mr = &mg->rates[index % MCS_GROUP_RATES]; |
| 291 | |
| 292 | tmp_group = mi->max_prob_rate / MCS_GROUP_RATES; |
| 293 | tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES; |
| 294 | tmp_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; |
| 295 | tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability; |
| 296 | |
| 297 | /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from |
| 298 | * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */ |
| 299 | max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES; |
| 300 | if((index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) && |
| 301 | (max_tp_group != MINSTREL_CCK_GROUP)) |
| 302 | return; |
| 303 | |
| 304 | if (mr->probability > MINSTREL_FRAC(75, 100)) { |
| 305 | if (mr->cur_tp > tmp_tp) |
| 306 | mi->max_prob_rate = index; |
| 307 | if (mr->cur_tp > mg->rates[mg->max_group_prob_rate].cur_tp) |
| 308 | mg->max_group_prob_rate = index; |
| 309 | } else { |
| 310 | if (mr->probability > tmp_prob) |
| 311 | mi->max_prob_rate = index; |
| 312 | if (mr->probability > mg->rates[mg->max_group_prob_rate].probability) |
| 313 | mg->max_group_prob_rate = index; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* |
| 319 | * Assign new rate set per sta and use CCK rates only if the fastest |
| 320 | * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted |
| 321 | * rate sets where MCS and CCK rates are mixed, because CCK rates can |
| 322 | * not use aggregation. |
| 323 | */ |
| 324 | static void |
| 325 | minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi, |
Karl Beldan | d4d141c | 2014-10-20 15:45:59 +0200 | [diff] [blame] | 326 | u16 tmp_mcs_tp_rate[MAX_THR_RATES], |
| 327 | u16 tmp_cck_tp_rate[MAX_THR_RATES]) |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 328 | { |
| 329 | unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp; |
| 330 | int i; |
| 331 | |
| 332 | tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES; |
| 333 | tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES; |
| 334 | tmp_cck_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; |
| 335 | |
| 336 | tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES; |
| 337 | tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES; |
| 338 | tmp_mcs_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; |
| 339 | |
| 340 | if (tmp_cck_tp > tmp_mcs_tp) { |
| 341 | for(i = 0; i < MAX_THR_RATES; i++) { |
| 342 | minstrel_ht_sort_best_tp_rates(mi, tmp_cck_tp_rate[i], |
| 343 | tmp_mcs_tp_rate); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Try to increase robustness of max_prob rate by decrease number of |
| 351 | * streams if possible. |
| 352 | */ |
| 353 | static inline void |
| 354 | minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi) |
| 355 | { |
| 356 | struct minstrel_mcs_group_data *mg; |
| 357 | struct minstrel_rate_stats *mr; |
| 358 | int tmp_max_streams, group; |
| 359 | int tmp_tp = 0; |
| 360 | |
| 361 | tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] / |
| 362 | MCS_GROUP_RATES].streams; |
| 363 | for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) { |
| 364 | mg = &mi->groups[group]; |
| 365 | if (!mg->supported || group == MINSTREL_CCK_GROUP) |
| 366 | continue; |
| 367 | mr = minstrel_get_ratestats(mi, mg->max_group_prob_rate); |
| 368 | if (tmp_tp < mr->cur_tp && |
| 369 | (minstrel_mcs_groups[group].streams < tmp_max_streams)) { |
| 370 | mi->max_prob_rate = mg->max_group_prob_rate; |
| 371 | tmp_tp = mr->cur_tp; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 377 | * Update rate statistics and select new primary rates |
| 378 | * |
| 379 | * Rules for rate selection: |
| 380 | * - max_prob_rate must use only one stream, as a tradeoff between delivery |
| 381 | * probability and throughput during strong fluctuations |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 382 | * - as long as the max prob rate has a probability of more than 75%, pick |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 383 | * higher throughput rates, even if the probablity is a bit lower |
| 384 | */ |
| 385 | static void |
| 386 | minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) |
| 387 | { |
| 388 | struct minstrel_mcs_group_data *mg; |
| 389 | struct minstrel_rate_stats *mr; |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 390 | int group, i, j; |
Karl Beldan | d4d141c | 2014-10-20 15:45:59 +0200 | [diff] [blame] | 391 | u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES]; |
| 392 | u16 tmp_cck_tp_rate[MAX_THR_RATES], index; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 393 | |
| 394 | if (mi->ampdu_packets > 0) { |
| 395 | mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len, |
| 396 | MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL); |
| 397 | mi->ampdu_len = 0; |
| 398 | mi->ampdu_packets = 0; |
| 399 | } |
| 400 | |
| 401 | mi->sample_slow = 0; |
| 402 | mi->sample_count = 0; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 403 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 404 | /* Initialize global rate indexes */ |
| 405 | for(j = 0; j < MAX_THR_RATES; j++){ |
| 406 | tmp_mcs_tp_rate[j] = 0; |
| 407 | tmp_cck_tp_rate[j] = 0; |
| 408 | } |
Karl Beldan | c2eb5b0 | 2013-04-18 14:26:20 +0200 | [diff] [blame] | 409 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 410 | /* Find best rate sets within all MCS groups*/ |
| 411 | for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) { |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 412 | |
| 413 | mg = &mi->groups[group]; |
| 414 | if (!mg->supported) |
| 415 | continue; |
| 416 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 417 | mi->sample_count++; |
| 418 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 419 | /* (re)Initialize group rate indexes */ |
| 420 | for(j = 0; j < MAX_THR_RATES; j++) |
| 421 | tmp_group_tp_rate[j] = group; |
| 422 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 423 | for (i = 0; i < MCS_GROUP_RATES; i++) { |
| 424 | if (!(mg->supported & BIT(i))) |
| 425 | continue; |
| 426 | |
Karl Beldan | 351df09 | 2013-11-13 23:07:07 +0100 | [diff] [blame] | 427 | index = MCS_GROUP_RATES * group + i; |
| 428 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 429 | mr = &mg->rates[i]; |
| 430 | mr->retry_updated = false; |
Patrick Kelle | 6048d763 | 2011-11-15 16:44:48 +0100 | [diff] [blame] | 431 | minstrel_calc_rate_ewma(mr); |
| 432 | minstrel_ht_calc_tp(mi, group, i); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 433 | |
| 434 | if (!mr->cur_tp) |
| 435 | continue; |
| 436 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 437 | /* Find max throughput rate set */ |
| 438 | if (group != MINSTREL_CCK_GROUP) { |
| 439 | minstrel_ht_sort_best_tp_rates(mi, index, |
| 440 | tmp_mcs_tp_rate); |
| 441 | } else if (group == MINSTREL_CCK_GROUP) { |
| 442 | minstrel_ht_sort_best_tp_rates(mi, index, |
| 443 | tmp_cck_tp_rate); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 446 | /* Find max throughput rate set within a group */ |
| 447 | minstrel_ht_sort_best_tp_rates(mi, index, |
| 448 | tmp_group_tp_rate); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 449 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 450 | /* Find max probability rate per group and global */ |
| 451 | minstrel_ht_set_best_prob_rate(mi, index); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 452 | } |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 453 | |
| 454 | memcpy(mg->max_group_tp_rate, tmp_group_tp_rate, |
| 455 | sizeof(mg->max_group_tp_rate)); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 458 | /* Assign new rate set per sta */ |
| 459 | minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, tmp_cck_tp_rate); |
| 460 | memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate)); |
| 461 | |
| 462 | /* Try to increase robustness of max_prob_rate*/ |
| 463 | minstrel_ht_prob_rate_reduce_streams(mi); |
| 464 | |
Felix Fietkau | 96d4ac3 | 2013-03-02 21:20:14 +0100 | [diff] [blame] | 465 | /* try to sample all available rates during each interval */ |
| 466 | mi->sample_count *= 8; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 467 | |
Lorenzo Bianconi | 37feb7e | 2013-08-27 16:59:47 +0200 | [diff] [blame] | 468 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 469 | /* use fixed index if set */ |
| 470 | if (mp->fixed_rate_idx != -1) { |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 471 | for (i = 0; i < 4; i++) |
| 472 | mi->max_tp_rate[i] = mp->fixed_rate_idx; |
Lorenzo Bianconi | 37feb7e | 2013-08-27 16:59:47 +0200 | [diff] [blame] | 473 | mi->max_prob_rate = mp->fixed_rate_idx; |
| 474 | } |
| 475 | #endif |
Felix Fietkau | a299c6d | 2013-03-02 21:20:13 +0100 | [diff] [blame] | 476 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 477 | /* Reset update timer */ |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 478 | mi->stats_update = jiffies; |
| 479 | } |
| 480 | |
| 481 | static bool |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 482 | minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 483 | { |
Helmut Schaa | b79296b | 2011-11-14 15:28:19 +0100 | [diff] [blame] | 484 | if (rate->idx < 0) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 485 | return false; |
| 486 | |
Helmut Schaa | b79296b | 2011-11-14 15:28:19 +0100 | [diff] [blame] | 487 | if (!rate->count) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 488 | return false; |
| 489 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 490 | if (rate->flags & IEEE80211_TX_RC_MCS) |
| 491 | return true; |
| 492 | |
| 493 | return rate->idx == mp->cck_rates[0] || |
| 494 | rate->idx == mp->cck_rates[1] || |
| 495 | rate->idx == mp->cck_rates[2] || |
| 496 | rate->idx == mp->cck_rates[3]; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static void |
| 500 | minstrel_next_sample_idx(struct minstrel_ht_sta *mi) |
| 501 | { |
| 502 | struct minstrel_mcs_group_data *mg; |
| 503 | |
| 504 | for (;;) { |
| 505 | mi->sample_group++; |
| 506 | mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups); |
| 507 | mg = &mi->groups[mi->sample_group]; |
| 508 | |
| 509 | if (!mg->supported) |
| 510 | continue; |
| 511 | |
| 512 | if (++mg->index >= MCS_GROUP_RATES) { |
| 513 | mg->index = 0; |
| 514 | if (++mg->column >= ARRAY_SIZE(sample_table)) |
| 515 | mg->column = 0; |
| 516 | } |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void |
Karl Beldan | d4d141c | 2014-10-20 15:45:59 +0200 | [diff] [blame] | 522 | minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 523 | { |
| 524 | int group, orig_group; |
| 525 | |
| 526 | orig_group = group = *idx / MCS_GROUP_RATES; |
| 527 | while (group > 0) { |
| 528 | group--; |
| 529 | |
| 530 | if (!mi->groups[group].supported) |
| 531 | continue; |
| 532 | |
| 533 | if (minstrel_mcs_groups[group].streams > |
| 534 | minstrel_mcs_groups[orig_group].streams) |
| 535 | continue; |
| 536 | |
| 537 | if (primary) |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 538 | *idx = mi->groups[group].max_group_tp_rate[0]; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 539 | else |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 540 | *idx = mi->groups[group].max_group_tp_rate[1]; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static void |
Patrick Kelle | 6048d763 | 2011-11-15 16:44:48 +0100 | [diff] [blame] | 546 | minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 547 | { |
| 548 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 549 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); |
| 550 | u16 tid; |
| 551 | |
| 552 | if (unlikely(!ieee80211_is_data_qos(hdr->frame_control))) |
| 553 | return; |
| 554 | |
Johannes Berg | e133fae | 2013-08-22 08:36:41 +0200 | [diff] [blame] | 555 | if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 556 | return; |
| 557 | |
| 558 | tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK; |
Johannes Berg | a622ab7 | 2010-06-10 10:21:39 +0200 | [diff] [blame] | 559 | if (likely(sta->ampdu_mlme.tid_tx[tid])) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 560 | return; |
| 561 | |
Luis R. Rodriguez | 48124d1 | 2010-11-23 15:05:02 -0800 | [diff] [blame] | 562 | if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO) |
| 563 | return; |
| 564 | |
Sujith Manoharan | bd2ce6e | 2010-12-15 07:47:10 +0530 | [diff] [blame] | 565 | ieee80211_start_tx_ba_session(pubsta, tid, 5000); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void |
| 569 | minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband, |
| 570 | struct ieee80211_sta *sta, void *priv_sta, |
| 571 | struct sk_buff *skb) |
| 572 | { |
| 573 | struct minstrel_ht_sta_priv *msp = priv_sta; |
| 574 | struct minstrel_ht_sta *mi = &msp->ht; |
| 575 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 576 | struct ieee80211_tx_rate *ar = info->status.rates; |
| 577 | struct minstrel_rate_stats *rate, *rate2; |
| 578 | struct minstrel_priv *mp = priv; |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 579 | bool last, update = false; |
Johannes Berg | a544ab7 | 2012-11-13 21:36:27 +0100 | [diff] [blame] | 580 | int i; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 581 | |
| 582 | if (!msp->is_ht) |
| 583 | return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb); |
| 584 | |
| 585 | /* This packet was aggregated but doesn't carry status info */ |
| 586 | if ((info->flags & IEEE80211_TX_CTL_AMPDU) && |
| 587 | !(info->flags & IEEE80211_TX_STAT_AMPDU)) |
| 588 | return; |
| 589 | |
Björn Smedman | 15d46f3 | 2010-10-10 22:14:25 +0200 | [diff] [blame] | 590 | if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) { |
| 591 | info->status.ampdu_ack_len = |
| 592 | (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 593 | info->status.ampdu_len = 1; |
| 594 | } |
| 595 | |
| 596 | mi->ampdu_packets++; |
| 597 | mi->ampdu_len += info->status.ampdu_len; |
| 598 | |
| 599 | if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) { |
Felix Fietkau | c7317e4 | 2010-10-21 02:47:25 +0200 | [diff] [blame] | 600 | mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len); |
Felix Fietkau | 52c00a3 | 2013-03-05 14:20:19 +0100 | [diff] [blame] | 601 | mi->sample_tries = 1; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 602 | mi->sample_count--; |
| 603 | } |
| 604 | |
Daniel Halperin | 8d5eab5 | 2011-03-09 03:10:18 -0800 | [diff] [blame] | 605 | if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 606 | mi->sample_packets += info->status.ampdu_len; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 607 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 608 | last = !minstrel_ht_txstat_valid(mp, &ar[0]); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 609 | for (i = 0; !last; i++) { |
| 610 | last = (i == IEEE80211_TX_MAX_RATES - 1) || |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 611 | !minstrel_ht_txstat_valid(mp, &ar[i + 1]); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 612 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 613 | rate = minstrel_ht_get_stats(mp, mi, &ar[i]); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 614 | |
Björn Smedman | 15d46f3 | 2010-10-10 22:14:25 +0200 | [diff] [blame] | 615 | if (last) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 616 | rate->success += info->status.ampdu_ack_len; |
| 617 | |
| 618 | rate->attempts += ar[i].count * info->status.ampdu_len; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * check for sudden death of spatial multiplexing, |
| 623 | * downgrade to a lower number of streams if necessary. |
| 624 | */ |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 625 | rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 626 | if (rate->attempts > 30 && |
| 627 | MINSTREL_FRAC(rate->success, rate->attempts) < |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 628 | MINSTREL_FRAC(20, 100)) { |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 629 | minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 630 | update = true; |
| 631 | } |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 632 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 633 | rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]); |
Ming Lei | ecc3d5a | 2010-07-01 23:19:50 +0800 | [diff] [blame] | 634 | if (rate2->attempts > 30 && |
| 635 | MINSTREL_FRAC(rate2->success, rate2->attempts) < |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 636 | MINSTREL_FRAC(20, 100)) { |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 637 | minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 638 | update = true; |
| 639 | } |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 640 | |
| 641 | if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) { |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 642 | update = true; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 643 | minstrel_ht_update_stats(mp, mi); |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 644 | if (!(info->flags & IEEE80211_TX_CTL_AMPDU) && |
| 645 | mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) |
Patrick Kelle | 6048d763 | 2011-11-15 16:44:48 +0100 | [diff] [blame] | 646 | minstrel_aggr_check(sta, skb); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 647 | } |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 648 | |
| 649 | if (update) |
| 650 | minstrel_ht_update_rates(mp, mi); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void |
| 654 | minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, |
| 655 | int index) |
| 656 | { |
| 657 | struct minstrel_rate_stats *mr; |
| 658 | const struct mcs_group *group; |
| 659 | unsigned int tx_time, tx_time_rtscts, tx_time_data; |
| 660 | unsigned int cw = mp->cw_min; |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 661 | unsigned int ctime = 0; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 662 | unsigned int t_slot = 9; /* FIXME */ |
| 663 | unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len); |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 664 | unsigned int overhead = 0, overhead_rtscts = 0; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 665 | |
| 666 | mr = minstrel_get_ratestats(mi, index); |
| 667 | if (mr->probability < MINSTREL_FRAC(1, 10)) { |
| 668 | mr->retry_count = 1; |
| 669 | mr->retry_count_rtscts = 1; |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | mr->retry_count = 2; |
| 674 | mr->retry_count_rtscts = 2; |
| 675 | mr->retry_updated = true; |
| 676 | |
| 677 | group = &minstrel_mcs_groups[index / MCS_GROUP_RATES]; |
Felix Fietkau | ed97a13 | 2013-03-02 21:20:12 +0100 | [diff] [blame] | 678 | tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000; |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 679 | |
| 680 | /* Contention time for first 2 tries */ |
| 681 | ctime = (t_slot * cw) >> 1; |
| 682 | cw = min((cw << 1) | 1, mp->cw_max); |
| 683 | ctime += (t_slot * cw) >> 1; |
| 684 | cw = min((cw << 1) | 1, mp->cw_max); |
| 685 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 686 | if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) { |
| 687 | overhead = mi->overhead; |
| 688 | overhead_rtscts = mi->overhead_rtscts; |
| 689 | } |
| 690 | |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 691 | /* Total TX time for data and Contention after first 2 tries */ |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 692 | tx_time = ctime + 2 * (overhead + tx_time_data); |
| 693 | tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data); |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 694 | |
| 695 | /* See how many more tries we can fit inside segment size */ |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 696 | do { |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 697 | /* Contention time for this try */ |
| 698 | ctime = (t_slot * cw) >> 1; |
| 699 | cw = min((cw << 1) | 1, mp->cw_max); |
| 700 | |
| 701 | /* Total TX time after this try */ |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 702 | tx_time += ctime + overhead + tx_time_data; |
| 703 | tx_time_rtscts += ctime + overhead_rtscts + tx_time_data; |
Daniel Halperin | 8fddddf | 2011-05-10 19:00:45 -0700 | [diff] [blame] | 704 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 705 | if (tx_time_rtscts < mp->segment_size) |
| 706 | mr->retry_count_rtscts++; |
| 707 | } while ((tx_time < mp->segment_size) && |
| 708 | (++mr->retry_count < mp->max_retry)); |
| 709 | } |
| 710 | |
| 711 | |
| 712 | static void |
| 713 | minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 714 | struct ieee80211_sta_rates *ratetbl, int offset, int index) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 715 | { |
| 716 | const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES]; |
| 717 | struct minstrel_rate_stats *mr; |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 718 | u8 idx; |
| 719 | u16 flags; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 720 | |
| 721 | mr = minstrel_get_ratestats(mi, index); |
| 722 | if (!mr->retry_updated) |
| 723 | minstrel_calc_retransmit(mp, mi, index); |
| 724 | |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 725 | if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) { |
| 726 | ratetbl->rate[offset].count = 2; |
| 727 | ratetbl->rate[offset].count_rts = 2; |
| 728 | ratetbl->rate[offset].count_cts = 2; |
| 729 | } else { |
| 730 | ratetbl->rate[offset].count = mr->retry_count; |
| 731 | ratetbl->rate[offset].count_cts = mr->retry_count; |
| 732 | ratetbl->rate[offset].count_rts = mr->retry_count_rtscts; |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 735 | if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) { |
| 736 | idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)]; |
| 737 | flags = 0; |
| 738 | } else { |
Karl Beldan | 7a5e3fa | 2013-11-11 13:12:55 +0100 | [diff] [blame] | 739 | idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8; |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 740 | flags = IEEE80211_TX_RC_MCS | group->flags; |
| 741 | } |
| 742 | |
| 743 | if (offset > 0) { |
| 744 | ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts; |
| 745 | flags |= IEEE80211_TX_RC_USE_RTS_CTS; |
| 746 | } |
| 747 | |
| 748 | ratetbl->rate[offset].idx = idx; |
| 749 | ratetbl->rate[offset].flags = flags; |
| 750 | } |
| 751 | |
| 752 | static void |
| 753 | minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) |
| 754 | { |
| 755 | struct ieee80211_sta_rates *rates; |
| 756 | int i = 0; |
| 757 | |
| 758 | rates = kzalloc(sizeof(*rates), GFP_ATOMIC); |
| 759 | if (!rates) |
| 760 | return; |
| 761 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 762 | /* Start with max_tp_rate[0] */ |
| 763 | minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 764 | |
| 765 | if (mp->hw->max_rates >= 3) { |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 766 | /* At least 3 tx rates supported, use max_tp_rate[1] next */ |
| 767 | minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (mp->hw->max_rates >= 2) { |
| 771 | /* |
| 772 | * At least 2 tx rates supported, use max_prob_rate next */ |
| 773 | minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate); |
| 774 | } |
| 775 | |
| 776 | rates->rate[i].idx = -1; |
| 777 | rate_control_set_rates(mp->hw, mi->sta, rates); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static inline int |
| 781 | minstrel_get_duration(int index) |
| 782 | { |
| 783 | const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES]; |
| 784 | return group->duration[index % MCS_GROUP_RATES]; |
| 785 | } |
| 786 | |
| 787 | static int |
| 788 | minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) |
| 789 | { |
| 790 | struct minstrel_rate_stats *mr; |
| 791 | struct minstrel_mcs_group_data *mg; |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 792 | unsigned int sample_dur, sample_group, cur_max_tp_streams; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 793 | int sample_idx = 0; |
| 794 | |
| 795 | if (mi->sample_wait > 0) { |
| 796 | mi->sample_wait--; |
| 797 | return -1; |
| 798 | } |
| 799 | |
| 800 | if (!mi->sample_tries) |
| 801 | return -1; |
| 802 | |
Felix Fietkau | 965237a | 2013-03-03 12:49:51 +0100 | [diff] [blame] | 803 | sample_group = mi->sample_group; |
Karl Beldan | f12140c | 2013-11-11 13:10:49 +0100 | [diff] [blame] | 804 | mg = &mi->groups[sample_group]; |
| 805 | sample_idx = sample_table[mg->column][mg->index]; |
Daniel Halperin | 8d5eab5 | 2011-03-09 03:10:18 -0800 | [diff] [blame] | 806 | minstrel_next_sample_idx(mi); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 807 | |
Karl Beldan | f12140c | 2013-11-11 13:10:49 +0100 | [diff] [blame] | 808 | if (!(mg->supported & BIT(sample_idx))) |
| 809 | return -1; |
| 810 | |
| 811 | mr = &mg->rates[sample_idx]; |
| 812 | sample_idx += sample_group * MCS_GROUP_RATES; |
| 813 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 814 | /* |
Helmut Schaa | ba6fa29 | 2012-03-14 13:31:11 +0100 | [diff] [blame] | 815 | * Sampling might add some overhead (RTS, no aggregation) |
| 816 | * to the frame. Hence, don't use sampling for the currently |
Felix Fietkau | a0ca796 | 2013-03-16 17:00:27 +0100 | [diff] [blame] | 817 | * used rates. |
Helmut Schaa | ba6fa29 | 2012-03-14 13:31:11 +0100 | [diff] [blame] | 818 | */ |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 819 | if (sample_idx == mi->max_tp_rate[0] || |
| 820 | sample_idx == mi->max_tp_rate[1] || |
Felix Fietkau | a0ca796 | 2013-03-16 17:00:27 +0100 | [diff] [blame] | 821 | sample_idx == mi->max_prob_rate) |
Helmut Schaa | ba6fa29 | 2012-03-14 13:31:11 +0100 | [diff] [blame] | 822 | return -1; |
Felix Fietkau | a0ca796 | 2013-03-16 17:00:27 +0100 | [diff] [blame] | 823 | |
Helmut Schaa | ba6fa29 | 2012-03-14 13:31:11 +0100 | [diff] [blame] | 824 | /* |
Felix Fietkau | bc96f24 | 2013-03-16 17:00:26 +0100 | [diff] [blame] | 825 | * Do not sample if the probability is already higher than 95% |
| 826 | * to avoid wasting airtime. |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 827 | */ |
Felix Fietkau | bc96f24 | 2013-03-16 17:00:26 +0100 | [diff] [blame] | 828 | if (mr->probability > MINSTREL_FRAC(95, 100)) |
Daniel Halperin | 8d5eab5 | 2011-03-09 03:10:18 -0800 | [diff] [blame] | 829 | return -1; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | * Make sure that lower rates get sampled only occasionally, |
| 833 | * if the link is working perfectly. |
| 834 | */ |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 835 | |
| 836 | cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] / |
| 837 | MCS_GROUP_RATES].streams; |
Felix Fietkau | 965237a | 2013-03-03 12:49:51 +0100 | [diff] [blame] | 838 | sample_dur = minstrel_get_duration(sample_idx); |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 839 | if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) && |
| 840 | (cur_max_tp_streams - 1 < |
Felix Fietkau | 965237a | 2013-03-03 12:49:51 +0100 | [diff] [blame] | 841 | minstrel_mcs_groups[sample_group].streams || |
| 842 | sample_dur >= minstrel_get_duration(mi->max_prob_rate))) { |
Felix Fietkau | c7317e4 | 2010-10-21 02:47:25 +0200 | [diff] [blame] | 843 | if (mr->sample_skipped < 20) |
Daniel Halperin | 8d5eab5 | 2011-03-09 03:10:18 -0800 | [diff] [blame] | 844 | return -1; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 845 | |
| 846 | if (mi->sample_slow++ > 2) |
Daniel Halperin | 8d5eab5 | 2011-03-09 03:10:18 -0800 | [diff] [blame] | 847 | return -1; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 848 | } |
Felix Fietkau | 098b8af | 2013-03-03 12:49:52 +0100 | [diff] [blame] | 849 | mi->sample_tries--; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 850 | |
| 851 | return sample_idx; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 855 | minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp, |
| 856 | struct minstrel_ht_sta *mi, bool val) |
| 857 | { |
| 858 | u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported; |
| 859 | |
| 860 | if (!supported || !mi->cck_supported_short) |
| 861 | return; |
| 862 | |
| 863 | if (supported & (mi->cck_supported_short << (val * 4))) |
| 864 | return; |
| 865 | |
| 866 | supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4); |
| 867 | mi->groups[MINSTREL_CCK_GROUP].supported = supported; |
| 868 | } |
| 869 | |
| 870 | static void |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 871 | minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta, |
| 872 | struct ieee80211_tx_rate_control *txrc) |
| 873 | { |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 874 | const struct mcs_group *sample_group; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 875 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 876 | struct ieee80211_tx_rate *rate = &info->status.rates[0]; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 877 | struct minstrel_ht_sta_priv *msp = priv_sta; |
| 878 | struct minstrel_ht_sta *mi = &msp->ht; |
| 879 | struct minstrel_priv *mp = priv; |
| 880 | int sample_idx; |
| 881 | |
| 882 | if (rate_control_send_low(sta, priv_sta, txrc)) |
| 883 | return; |
| 884 | |
| 885 | if (!msp->is_ht) |
| 886 | return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc); |
| 887 | |
| 888 | info->flags |= mi->tx_flags; |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 889 | minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble); |
Helmut Schaa | 6de062c | 2011-08-01 11:32:53 +0200 | [diff] [blame] | 890 | |
Lorenzo Bianconi | 37feb7e | 2013-08-27 16:59:47 +0200 | [diff] [blame] | 891 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 892 | if (mp->fixed_rate_idx != -1) |
| 893 | return; |
| 894 | #endif |
| 895 | |
Helmut Schaa | 6de062c | 2011-08-01 11:32:53 +0200 | [diff] [blame] | 896 | /* Don't use EAPOL frames for sampling on non-mrr hw */ |
| 897 | if (mp->hw->max_rates == 1 && |
Johannes Berg | af61a16 | 2013-07-02 18:09:12 +0200 | [diff] [blame] | 898 | (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)) |
Helmut Schaa | 6de062c | 2011-08-01 11:32:53 +0200 | [diff] [blame] | 899 | sample_idx = -1; |
| 900 | else |
| 901 | sample_idx = minstrel_get_sample_rate(mp, mi); |
Zefir Kurtisi | 24f7580 | 2011-05-20 20:29:17 +0200 | [diff] [blame] | 902 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 903 | mi->total_packets++; |
| 904 | |
| 905 | /* wraparound */ |
| 906 | if (mi->total_packets == ~0) { |
| 907 | mi->total_packets = 0; |
| 908 | mi->sample_packets = 0; |
| 909 | } |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 910 | |
| 911 | if (sample_idx < 0) |
| 912 | return; |
| 913 | |
| 914 | sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES]; |
| 915 | info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE; |
Felix Fietkau | 1cd1585 | 2013-06-28 21:04:35 +0200 | [diff] [blame] | 916 | rate->count = 1; |
| 917 | |
| 918 | if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) { |
| 919 | int idx = sample_idx % ARRAY_SIZE(mp->cck_rates); |
| 920 | rate->idx = mp->cck_rates[idx]; |
| 921 | rate->flags = 0; |
| 922 | return; |
| 923 | } |
| 924 | |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 925 | rate->idx = sample_idx % MCS_GROUP_RATES + |
Karl Beldan | 7a5e3fa | 2013-11-11 13:12:55 +0100 | [diff] [blame] | 926 | (sample_group->streams - 1) * 8; |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 927 | rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static void |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 931 | minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, |
| 932 | struct ieee80211_supported_band *sband, |
| 933 | struct ieee80211_sta *sta) |
| 934 | { |
| 935 | int i; |
| 936 | |
| 937 | if (sband->band != IEEE80211_BAND_2GHZ) |
| 938 | return; |
| 939 | |
Felix Fietkau | 2dfca31 | 2013-08-20 19:43:54 +0200 | [diff] [blame] | 940 | if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES)) |
| 941 | return; |
| 942 | |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 943 | mi->cck_supported = 0; |
| 944 | mi->cck_supported_short = 0; |
| 945 | for (i = 0; i < 4; i++) { |
| 946 | if (!rate_supported(sta, sband->band, mp->cck_rates[i])) |
| 947 | continue; |
| 948 | |
| 949 | mi->cck_supported |= BIT(i); |
| 950 | if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE) |
| 951 | mi->cck_supported_short |= BIT(i); |
| 952 | } |
| 953 | |
| 954 | mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported; |
| 955 | } |
| 956 | |
| 957 | static void |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 958 | minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband, |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 959 | struct cfg80211_chan_def *chandef, |
Johannes Berg | 64f68e5 | 2012-03-28 10:58:37 +0200 | [diff] [blame] | 960 | struct ieee80211_sta *sta, void *priv_sta) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 961 | { |
| 962 | struct minstrel_priv *mp = priv; |
| 963 | struct minstrel_ht_sta_priv *msp = priv_sta; |
| 964 | struct minstrel_ht_sta *mi = &msp->ht; |
| 965 | struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 966 | u16 sta_cap = sta->ht_cap.cap; |
Felix Fietkau | 4dc217d | 2011-03-25 15:30:38 +0100 | [diff] [blame] | 967 | int n_supported = 0; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 968 | int ack_dur; |
| 969 | int stbc; |
| 970 | int i; |
| 971 | |
| 972 | /* fall back to the old minstrel for legacy stations */ |
Felix Fietkau | 4dc217d | 2011-03-25 15:30:38 +0100 | [diff] [blame] | 973 | if (!sta->ht_cap.ht_supported) |
| 974 | goto use_legacy; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 975 | |
Karl Beldan | 8a0ee4f | 2014-10-20 15:46:00 +0200 | [diff] [blame^] | 976 | BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 977 | |
| 978 | msp->is_ht = true; |
| 979 | memset(mi, 0, sizeof(*mi)); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 980 | |
| 981 | mi->sta = sta; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 982 | mi->stats_update = jiffies; |
| 983 | |
Simon Wunderlich | 438b61b | 2013-07-08 16:55:51 +0200 | [diff] [blame] | 984 | ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0); |
| 985 | mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0); |
| 986 | mi->overhead += ack_dur; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 987 | mi->overhead_rtscts = mi->overhead + 2 * ack_dur; |
| 988 | |
| 989 | mi->avg_ampdu_len = MINSTREL_FRAC(1, 1); |
| 990 | |
| 991 | /* When using MRR, sample more on the first attempt, without delay */ |
| 992 | if (mp->has_mrr) { |
| 993 | mi->sample_count = 16; |
| 994 | mi->sample_wait = 0; |
| 995 | } else { |
| 996 | mi->sample_count = 8; |
| 997 | mi->sample_wait = 8; |
| 998 | } |
| 999 | mi->sample_tries = 4; |
| 1000 | |
| 1001 | stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >> |
| 1002 | IEEE80211_HT_CAP_RX_STBC_SHIFT; |
| 1003 | mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT; |
| 1004 | |
| 1005 | if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING) |
| 1006 | mi->tx_flags |= IEEE80211_TX_CTL_LDPC; |
| 1007 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1008 | for (i = 0; i < ARRAY_SIZE(mi->groups); i++) { |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1009 | mi->groups[i].supported = 0; |
Felix Fietkau | a0497f9 | 2013-02-13 10:51:08 +0100 | [diff] [blame] | 1010 | if (i == MINSTREL_CCK_GROUP) { |
| 1011 | minstrel_ht_update_cck(mp, mi, sband, sta); |
| 1012 | continue; |
| 1013 | } |
| 1014 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1015 | if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) { |
Johannes Berg | e1a0c6b | 2013-02-07 11:47:44 +0100 | [diff] [blame] | 1016 | if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) { |
| 1017 | if (!(sta_cap & IEEE80211_HT_CAP_SGI_40)) |
| 1018 | continue; |
| 1019 | } else { |
| 1020 | if (!(sta_cap & IEEE80211_HT_CAP_SGI_20)) |
| 1021 | continue; |
| 1022 | } |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Johannes Berg | e1a0c6b | 2013-02-07 11:47:44 +0100 | [diff] [blame] | 1025 | if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH && |
| 1026 | sta->bandwidth < IEEE80211_STA_RX_BW_40) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1027 | continue; |
| 1028 | |
Helmut Schaa | e921977 | 2012-03-09 14:13:45 +0100 | [diff] [blame] | 1029 | /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */ |
Johannes Berg | af0ed69 | 2013-02-12 14:21:00 +0100 | [diff] [blame] | 1030 | if (sta->smps_mode == IEEE80211_SMPS_STATIC && |
Helmut Schaa | e921977 | 2012-03-09 14:13:45 +0100 | [diff] [blame] | 1031 | minstrel_mcs_groups[i].streams > 1) |
| 1032 | continue; |
| 1033 | |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1034 | mi->groups[i].supported = |
| 1035 | mcs->rx_mask[minstrel_mcs_groups[i].streams - 1]; |
Felix Fietkau | 4dc217d | 2011-03-25 15:30:38 +0100 | [diff] [blame] | 1036 | |
| 1037 | if (mi->groups[i].supported) |
| 1038 | n_supported++; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1039 | } |
Felix Fietkau | 4dc217d | 2011-03-25 15:30:38 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (!n_supported) |
| 1042 | goto use_legacy; |
| 1043 | |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 1044 | /* create an initial rate table with the lowest supported rates */ |
Karl Beldan | a364736 | 2013-04-18 14:26:21 +0200 | [diff] [blame] | 1045 | minstrel_ht_update_stats(mp, mi); |
Felix Fietkau | a856666 | 2013-04-22 16:14:42 +0200 | [diff] [blame] | 1046 | minstrel_ht_update_rates(mp, mi); |
Karl Beldan | a364736 | 2013-04-18 14:26:21 +0200 | [diff] [blame] | 1047 | |
Felix Fietkau | 4dc217d | 2011-03-25 15:30:38 +0100 | [diff] [blame] | 1048 | return; |
| 1049 | |
| 1050 | use_legacy: |
| 1051 | msp->is_ht = false; |
| 1052 | memset(&msp->legacy, 0, sizeof(msp->legacy)); |
| 1053 | msp->legacy.r = msp->ratelist; |
| 1054 | msp->legacy.sample_table = msp->sample_table; |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 1055 | return mac80211_minstrel.rate_init(priv, sband, chandef, sta, |
| 1056 | &msp->legacy); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static void |
| 1060 | minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband, |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 1061 | struct cfg80211_chan_def *chandef, |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1062 | struct ieee80211_sta *sta, void *priv_sta) |
| 1063 | { |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 1064 | minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static void |
| 1068 | minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband, |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 1069 | struct cfg80211_chan_def *chandef, |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1070 | struct ieee80211_sta *sta, void *priv_sta, |
Johannes Berg | 64f68e5 | 2012-03-28 10:58:37 +0200 | [diff] [blame] | 1071 | u32 changed) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1072 | { |
Simon Wunderlich | 3de805c | 2013-07-08 16:55:50 +0200 | [diff] [blame] | 1073 | minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static void * |
| 1077 | minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp) |
| 1078 | { |
| 1079 | struct ieee80211_supported_band *sband; |
| 1080 | struct minstrel_ht_sta_priv *msp; |
| 1081 | struct minstrel_priv *mp = priv; |
| 1082 | struct ieee80211_hw *hw = mp->hw; |
| 1083 | int max_rates = 0; |
| 1084 | int i; |
| 1085 | |
| 1086 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) { |
| 1087 | sband = hw->wiphy->bands[i]; |
| 1088 | if (sband && sband->n_bitrates > max_rates) |
| 1089 | max_rates = sband->n_bitrates; |
| 1090 | } |
| 1091 | |
Thomas Huehn | 472dd35 | 2012-06-29 06:26:27 -0700 | [diff] [blame] | 1092 | msp = kzalloc(sizeof(*msp), gfp); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1093 | if (!msp) |
| 1094 | return NULL; |
| 1095 | |
| 1096 | msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp); |
| 1097 | if (!msp->ratelist) |
| 1098 | goto error; |
| 1099 | |
| 1100 | msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp); |
| 1101 | if (!msp->sample_table) |
| 1102 | goto error1; |
| 1103 | |
| 1104 | return msp; |
| 1105 | |
| 1106 | error1: |
Dan Carpenter | 7e98801 | 2010-07-22 13:14:19 +0200 | [diff] [blame] | 1107 | kfree(msp->ratelist); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1108 | error: |
| 1109 | kfree(msp); |
| 1110 | return NULL; |
| 1111 | } |
| 1112 | |
| 1113 | static void |
| 1114 | minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta) |
| 1115 | { |
| 1116 | struct minstrel_ht_sta_priv *msp = priv_sta; |
| 1117 | |
| 1118 | kfree(msp->sample_table); |
| 1119 | kfree(msp->ratelist); |
| 1120 | kfree(msp); |
| 1121 | } |
| 1122 | |
| 1123 | static void * |
| 1124 | minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir) |
| 1125 | { |
| 1126 | return mac80211_minstrel.alloc(hw, debugfsdir); |
| 1127 | } |
| 1128 | |
| 1129 | static void |
| 1130 | minstrel_ht_free(void *priv) |
| 1131 | { |
| 1132 | mac80211_minstrel.free(priv); |
| 1133 | } |
| 1134 | |
Antonio Quartulli | cca674d | 2014-05-19 21:53:20 +0200 | [diff] [blame] | 1135 | static u32 minstrel_ht_get_expected_throughput(void *priv_sta) |
| 1136 | { |
| 1137 | struct minstrel_ht_sta_priv *msp = priv_sta; |
| 1138 | struct minstrel_ht_sta *mi = &msp->ht; |
| 1139 | int i, j; |
| 1140 | |
| 1141 | if (!msp->is_ht) |
| 1142 | return mac80211_minstrel.get_expected_throughput(priv_sta); |
| 1143 | |
Thomas Huehn | 5935839 | 2014-09-09 23:22:14 +0200 | [diff] [blame] | 1144 | i = mi->max_tp_rate[0] / MCS_GROUP_RATES; |
| 1145 | j = mi->max_tp_rate[0] % MCS_GROUP_RATES; |
Antonio Quartulli | cca674d | 2014-05-19 21:53:20 +0200 | [diff] [blame] | 1146 | |
| 1147 | /* convert cur_tp from pkt per second in kbps */ |
| 1148 | return mi->groups[i].rates[j].cur_tp * AVG_PKT_SIZE * 8 / 1024; |
| 1149 | } |
| 1150 | |
Johannes Berg | 631ad70 | 2014-01-20 23:29:34 +0100 | [diff] [blame] | 1151 | static const struct rate_control_ops mac80211_minstrel_ht = { |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1152 | .name = "minstrel_ht", |
| 1153 | .tx_status = minstrel_ht_tx_status, |
| 1154 | .get_rate = minstrel_ht_get_rate, |
| 1155 | .rate_init = minstrel_ht_rate_init, |
| 1156 | .rate_update = minstrel_ht_rate_update, |
| 1157 | .alloc_sta = minstrel_ht_alloc_sta, |
| 1158 | .free_sta = minstrel_ht_free_sta, |
| 1159 | .alloc = minstrel_ht_alloc, |
| 1160 | .free = minstrel_ht_free, |
| 1161 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 1162 | .add_sta_debugfs = minstrel_ht_add_sta_debugfs, |
| 1163 | .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs, |
| 1164 | #endif |
Antonio Quartulli | cca674d | 2014-05-19 21:53:20 +0200 | [diff] [blame] | 1165 | .get_expected_throughput = minstrel_ht_get_expected_throughput, |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1166 | }; |
| 1167 | |
| 1168 | |
Johannes Berg | f6e1a73 | 2014-01-21 00:32:52 +0100 | [diff] [blame] | 1169 | static void __init init_sample_table(void) |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1170 | { |
| 1171 | int col, i, new_idx; |
| 1172 | u8 rnd[MCS_GROUP_RATES]; |
| 1173 | |
| 1174 | memset(sample_table, 0xff, sizeof(sample_table)); |
| 1175 | for (col = 0; col < SAMPLE_COLUMNS; col++) { |
Karl Beldan | f7d8ad8 | 2013-11-13 10:54:19 +0100 | [diff] [blame] | 1176 | prandom_bytes(rnd, sizeof(rnd)); |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1177 | for (i = 0; i < MCS_GROUP_RATES; i++) { |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1178 | new_idx = (i + rnd[i]) % MCS_GROUP_RATES; |
Felix Fietkau | ec8aa66 | 2010-05-13 16:48:03 +0200 | [diff] [blame] | 1179 | while (sample_table[col][new_idx] != 0xff) |
| 1180 | new_idx = (new_idx + 1) % MCS_GROUP_RATES; |
| 1181 | |
| 1182 | sample_table[col][new_idx] = i; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | int __init |
| 1188 | rc80211_minstrel_ht_init(void) |
| 1189 | { |
| 1190 | init_sample_table(); |
| 1191 | return ieee80211_rate_control_register(&mac80211_minstrel_ht); |
| 1192 | } |
| 1193 | |
| 1194 | void |
| 1195 | rc80211_minstrel_ht_exit(void) |
| 1196 | { |
| 1197 | ieee80211_rate_control_unregister(&mac80211_minstrel_ht); |
| 1198 | } |