blob: a48eb76440c640df50262743502655869d12533f [file] [log] [blame]
Felix Fietkauec8aa662010-05-13 16:48:03 +02001/*
Felix Fietkaua0497f92013-02-13 10:51:08 +01002 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
Felix Fietkauec8aa662010-05-13 16:48:03 +02003 *
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 Fietkauec8aa662010-05-13 16:48:03 +020020
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 Berg00591ce2014-05-19 11:24:19 +020025#define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
Felix Fietkauec8aa662010-05-13 16:48:03 +020026
Felix Fietkaued97a132013-03-02 21:20:12 +010027/* Transmission time (nanoseconds) for a packet containing (syms) symbols */
Felix Fietkauec8aa662010-05-13 16:48:03 +020028#define MCS_SYMBOL_TIME(sgi, syms) \
29 (sgi ? \
Felix Fietkaued97a132013-03-02 21:20:12 +010030 ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
31 ((syms) * 1000) << 2 /* syms * 4 us */ \
Felix Fietkauec8aa662010-05-13 16:48:03 +020032 )
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 Beldan8a0ee4f2014-10-20 15:46:00 +020037#define BW_20 0
38#define BW_40 1
39
Helmut Schaaa5f69d942011-11-14 15:28:20 +010040/*
41 * Define group sort order: HT40 -> SGI -> #streams
42 */
43#define GROUP_IDX(_streams, _sgi, _ht40) \
Karl Beldan8a0ee4f2014-10-20 15:46:00 +020044 MINSTREL_HT_GROUP_0 + \
Helmut Schaaa5f69d942011-11-14 15:28:20 +010045 MINSTREL_MAX_STREAMS * 2 * _ht40 + \
Karl Beldan8a0ee4f2014-10-20 15:46:00 +020046 MINSTREL_MAX_STREAMS * _sgi + \
Helmut Schaaa5f69d942011-11-14 15:28:20 +010047 _streams - 1
48
Felix Fietkauec8aa662010-05-13 16:48:03 +020049/* MCS rate information for an MCS group */
Helmut Schaaa5f69d942011-11-14 15:28:20 +010050#define MCS_GROUP(_streams, _sgi, _ht40) \
51 [GROUP_IDX(_streams, _sgi, _ht40)] = { \
Felix Fietkauec8aa662010-05-13 16:48:03 +020052 .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 Fietkaua0497f92013-02-13 10:51:08 +010068#define CCK_DURATION(_bitrate, _short, _len) \
Felix Fietkaued97a132013-03-02 21:20:12 +010069 (1000 * (10 /* SIFS */ + \
Weilong Chenf359d3f2013-12-18 15:44:16 +080070 (_short ? 72 + 24 : 144 + 48) + \
Felix Fietkaued97a132013-03-02 21:20:12 +010071 (8 * (_len + 4) * 10) / (_bitrate)))
Felix Fietkaua0497f92013-02-13 10:51:08 +010072
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 Beldan8a0ee4f2014-10-20 15:46:00 +020083#define CCK_GROUP \
84 [MINSTREL_CCK_GROUP] = { \
85 .streams = 0, \
86 .duration = { \
87 CCK_DURATION_LIST(false), \
88 CCK_DURATION_LIST(true) \
89 } \
Felix Fietkaua0497f92013-02-13 10:51:08 +010090 }
91
Felix Fietkauec8aa662010-05-13 16:48:03 +020092/*
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 Schaaa5f69d942011-11-14 15:28:20 +010096 *
97 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
Karl Beldan8a0ee4f2014-10-20 15:46:00 +020098 * BW -> SGI -> #streams
Felix Fietkauec8aa662010-05-13 16:48:03 +020099 */
100const struct mcs_group minstrel_mcs_groups[] = {
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200101 MCS_GROUP(1, 0, BW_20),
102 MCS_GROUP(2, 0, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200103#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200104 MCS_GROUP(3, 0, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200105#endif
106
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200107 MCS_GROUP(1, 1, BW_20),
108 MCS_GROUP(2, 1, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200109#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200110 MCS_GROUP(3, 1, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200111#endif
112
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200113 MCS_GROUP(1, 0, BW_40),
114 MCS_GROUP(2, 0, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200115#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200116 MCS_GROUP(3, 0, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200117#endif
118
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200119 MCS_GROUP(1, 1, BW_40),
120 MCS_GROUP(2, 1, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200121#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200122 MCS_GROUP(3, 1, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200123#endif
Felix Fietkaua0497f92013-02-13 10:51:08 +0100124
Felix Fietkaua0497f92013-02-13 10:51:08 +0100125 CCK_GROUP
Felix Fietkauec8aa662010-05-13 16:48:03 +0200126};
127
Felix Fietkaua0497f92013-02-13 10:51:08 +0100128
Johannes Bergf6e1a732014-01-21 00:32:52 +0100129static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200130
Felix Fietkaua8566662013-04-22 16:14:42 +0200131static void
132minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
133
Felix Fietkauec8aa662010-05-13 16:48:03 +0200134/*
Felix Fietkauec8aa662010-05-13 16:48:03 +0200135 * Look up an MCS group index based on mac80211 rate information
136 */
137static int
138minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
139{
Karl Beldancc61d8d2014-09-29 02:36:30 +0200140 return GROUP_IDX((rate->idx / 8) + 1,
Helmut Schaaa5f69d942011-11-14 15:28:20 +0100141 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
142 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
Felix Fietkauec8aa662010-05-13 16:48:03 +0200143}
144
Felix Fietkaua0497f92013-02-13 10:51:08 +0100145static struct minstrel_rate_stats *
146minstrel_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 Beldan7a5e3fa2013-11-11 13:12:55 +0100153 idx = rate->idx % 8;
Felix Fietkaua0497f92013-02-13 10:51:08 +0100154 } 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 Fietkauec8aa662010-05-13 16:48:03 +0200168static inline struct minstrel_rate_stats *
169minstrel_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 */
178static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100179minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200180{
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 */
204static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100205minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200206{
207 struct minstrel_rate_stats *mr;
Felix Fietkaued97a132013-03-02 21:20:12 +0100208 unsigned int nsecs = 0;
209 unsigned int tp;
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100210 unsigned int prob;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200211
212 mr = &mi->groups[group].rates[rate];
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100213 prob = mr->probability;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200214
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100215 if (prob < MINSTREL_FRAC(1, 10)) {
Felix Fietkauec8aa662010-05-13 16:48:03 +0200216 mr->cur_tp = 0;
217 return;
218 }
219
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100220 /*
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 Fietkaua0497f92013-02-13 10:51:08 +0100227 if (group != MINSTREL_CCK_GROUP)
Felix Fietkaued97a132013-03-02 21:20:12 +0100228 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100229
Felix Fietkaued97a132013-03-02 21:20:12 +0100230 nsecs += minstrel_mcs_groups[group].duration[rate];
Felix Fietkaued97a132013-03-02 21:20:12 +0100231
Johannes Berg00591ce2014-05-19 11:24:19 +0200232 /* prob is scaled - see MINSTREL_FRAC above */
233 tp = 1000000 * ((prob * 1000) / nsecs);
Felix Fietkaued97a132013-03-02 21:20:12 +0100234 mr->cur_tp = MINSTREL_TRUNC(tp);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200235}
236
237/*
Thomas Huehn59358392014-09-09 23:22:14 +0200238 * 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 */
244static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200245minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
246 u16 *tp_list)
Thomas Huehn59358392014-09-09 23:22:14 +0200247{
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 */
282static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200283minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
Thomas Huehn59358392014-09-09 23:22:14 +0200284{
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 */
324static void
325minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
Karl Beldand4d141c2014-10-20 15:45:59 +0200326 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
327 u16 tmp_cck_tp_rate[MAX_THR_RATES])
Thomas Huehn59358392014-09-09 23:22:14 +0200328{
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 */
353static inline void
354minstrel_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 Fietkauec8aa662010-05-13 16:48:03 +0200377 * 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 Huehn59358392014-09-09 23:22:14 +0200382 * - as long as the max prob rate has a probability of more than 75%, pick
Felix Fietkauec8aa662010-05-13 16:48:03 +0200383 * higher throughput rates, even if the probablity is a bit lower
384 */
385static void
386minstrel_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 Huehn59358392014-09-09 23:22:14 +0200390 int group, i, j;
Karl Beldand4d141c2014-10-20 15:45:59 +0200391 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 Fietkauec8aa662010-05-13 16:48:03 +0200393
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 Fietkauec8aa662010-05-13 16:48:03 +0200403
Thomas Huehn59358392014-09-09 23:22:14 +0200404 /* 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 Beldanc2eb5b02013-04-18 14:26:20 +0200409
Thomas Huehn59358392014-09-09 23:22:14 +0200410 /* Find best rate sets within all MCS groups*/
411 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
Felix Fietkauec8aa662010-05-13 16:48:03 +0200412
413 mg = &mi->groups[group];
414 if (!mg->supported)
415 continue;
416
Felix Fietkauec8aa662010-05-13 16:48:03 +0200417 mi->sample_count++;
418
Thomas Huehn59358392014-09-09 23:22:14 +0200419 /* (re)Initialize group rate indexes */
420 for(j = 0; j < MAX_THR_RATES; j++)
421 tmp_group_tp_rate[j] = group;
422
Felix Fietkauec8aa662010-05-13 16:48:03 +0200423 for (i = 0; i < MCS_GROUP_RATES; i++) {
424 if (!(mg->supported & BIT(i)))
425 continue;
426
Karl Beldan351df092013-11-13 23:07:07 +0100427 index = MCS_GROUP_RATES * group + i;
428
Felix Fietkauec8aa662010-05-13 16:48:03 +0200429 mr = &mg->rates[i];
430 mr->retry_updated = false;
Patrick Kelle6048d7632011-11-15 16:44:48 +0100431 minstrel_calc_rate_ewma(mr);
432 minstrel_ht_calc_tp(mi, group, i);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200433
434 if (!mr->cur_tp)
435 continue;
436
Thomas Huehn59358392014-09-09 23:22:14 +0200437 /* 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 Fietkauec8aa662010-05-13 16:48:03 +0200444 }
445
Thomas Huehn59358392014-09-09 23:22:14 +0200446 /* Find max throughput rate set within a group */
447 minstrel_ht_sort_best_tp_rates(mi, index,
448 tmp_group_tp_rate);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200449
Thomas Huehn59358392014-09-09 23:22:14 +0200450 /* Find max probability rate per group and global */
451 minstrel_ht_set_best_prob_rate(mi, index);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200452 }
Thomas Huehn59358392014-09-09 23:22:14 +0200453
454 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
455 sizeof(mg->max_group_tp_rate));
Felix Fietkauec8aa662010-05-13 16:48:03 +0200456 }
457
Thomas Huehn59358392014-09-09 23:22:14 +0200458 /* 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 Fietkau96d4ac32013-03-02 21:20:14 +0100465 /* try to sample all available rates during each interval */
466 mi->sample_count *= 8;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200467
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200468#ifdef CONFIG_MAC80211_DEBUGFS
469 /* use fixed index if set */
470 if (mp->fixed_rate_idx != -1) {
Thomas Huehn59358392014-09-09 23:22:14 +0200471 for (i = 0; i < 4; i++)
472 mi->max_tp_rate[i] = mp->fixed_rate_idx;
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200473 mi->max_prob_rate = mp->fixed_rate_idx;
474 }
475#endif
Felix Fietkaua299c6d2013-03-02 21:20:13 +0100476
Thomas Huehn59358392014-09-09 23:22:14 +0200477 /* Reset update timer */
Felix Fietkauec8aa662010-05-13 16:48:03 +0200478 mi->stats_update = jiffies;
479}
480
481static bool
Felix Fietkaua0497f92013-02-13 10:51:08 +0100482minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200483{
Helmut Schaab79296b2011-11-14 15:28:19 +0100484 if (rate->idx < 0)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200485 return false;
486
Helmut Schaab79296b2011-11-14 15:28:19 +0100487 if (!rate->count)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200488 return false;
489
Felix Fietkaua0497f92013-02-13 10:51:08 +0100490 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 Fietkauec8aa662010-05-13 16:48:03 +0200497}
498
499static void
500minstrel_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
521static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200522minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200523{
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 Huehn59358392014-09-09 23:22:14 +0200538 *idx = mi->groups[group].max_group_tp_rate[0];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200539 else
Thomas Huehn59358392014-09-09 23:22:14 +0200540 *idx = mi->groups[group].max_group_tp_rate[1];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200541 break;
542 }
543}
544
545static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100546minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200547{
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 Berge133fae2013-08-22 08:36:41 +0200555 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
Felix Fietkauec8aa662010-05-13 16:48:03 +0200556 return;
557
558 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berga622ab72010-06-10 10:21:39 +0200559 if (likely(sta->ampdu_mlme.tid_tx[tid]))
Felix Fietkauec8aa662010-05-13 16:48:03 +0200560 return;
561
Luis R. Rodriguez48124d12010-11-23 15:05:02 -0800562 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
563 return;
564
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530565 ieee80211_start_tx_ba_session(pubsta, tid, 5000);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200566}
567
568static void
569minstrel_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 Fietkaua8566662013-04-22 16:14:42 +0200579 bool last, update = false;
Johannes Berga544ab72012-11-13 21:36:27 +0100580 int i;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200581
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 Smedman15d46f32010-10-10 22:14:25 +0200590 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
591 info->status.ampdu_ack_len =
592 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200593 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 Fietkauc7317e42010-10-21 02:47:25 +0200600 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkau52c00a32013-03-05 14:20:19 +0100601 mi->sample_tries = 1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200602 mi->sample_count--;
603 }
604
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800605 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200606 mi->sample_packets += info->status.ampdu_len;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200607
Felix Fietkaua0497f92013-02-13 10:51:08 +0100608 last = !minstrel_ht_txstat_valid(mp, &ar[0]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200609 for (i = 0; !last; i++) {
610 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
Felix Fietkaua0497f92013-02-13 10:51:08 +0100611 !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200612
Felix Fietkaua0497f92013-02-13 10:51:08 +0100613 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200614
Björn Smedman15d46f32010-10-10 22:14:25 +0200615 if (last)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200616 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 Huehn59358392014-09-09 23:22:14 +0200625 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200626 if (rate->attempts > 30 &&
627 MINSTREL_FRAC(rate->success, rate->attempts) <
Felix Fietkaua8566662013-04-22 16:14:42 +0200628 MINSTREL_FRAC(20, 100)) {
Thomas Huehn59358392014-09-09 23:22:14 +0200629 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
Felix Fietkaua8566662013-04-22 16:14:42 +0200630 update = true;
631 }
Felix Fietkauec8aa662010-05-13 16:48:03 +0200632
Thomas Huehn59358392014-09-09 23:22:14 +0200633 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
Ming Leiecc3d5a2010-07-01 23:19:50 +0800634 if (rate2->attempts > 30 &&
635 MINSTREL_FRAC(rate2->success, rate2->attempts) <
Felix Fietkaua8566662013-04-22 16:14:42 +0200636 MINSTREL_FRAC(20, 100)) {
Thomas Huehn59358392014-09-09 23:22:14 +0200637 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
Felix Fietkaua8566662013-04-22 16:14:42 +0200638 update = true;
639 }
Felix Fietkauec8aa662010-05-13 16:48:03 +0200640
641 if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
Felix Fietkaua8566662013-04-22 16:14:42 +0200642 update = true;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200643 minstrel_ht_update_stats(mp, mi);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100644 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
645 mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
Patrick Kelle6048d7632011-11-15 16:44:48 +0100646 minstrel_aggr_check(sta, skb);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200647 }
Felix Fietkaua8566662013-04-22 16:14:42 +0200648
649 if (update)
650 minstrel_ht_update_rates(mp, mi);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200651}
652
653static void
654minstrel_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 Halperin8fddddf2011-05-10 19:00:45 -0700661 unsigned int ctime = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200662 unsigned int t_slot = 9; /* FIXME */
663 unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100664 unsigned int overhead = 0, overhead_rtscts = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200665
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 Fietkaued97a132013-03-02 21:20:12 +0100678 tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700679
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 Fietkaua0497f92013-02-13 10:51:08 +0100686 if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
687 overhead = mi->overhead;
688 overhead_rtscts = mi->overhead_rtscts;
689 }
690
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700691 /* Total TX time for data and Contention after first 2 tries */
Felix Fietkaua0497f92013-02-13 10:51:08 +0100692 tx_time = ctime + 2 * (overhead + tx_time_data);
693 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700694
695 /* See how many more tries we can fit inside segment size */
Felix Fietkauec8aa662010-05-13 16:48:03 +0200696 do {
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700697 /* 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 Fietkaua0497f92013-02-13 10:51:08 +0100702 tx_time += ctime + overhead + tx_time_data;
703 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700704
Felix Fietkauec8aa662010-05-13 16:48:03 +0200705 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
712static void
713minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
Felix Fietkaua8566662013-04-22 16:14:42 +0200714 struct ieee80211_sta_rates *ratetbl, int offset, int index)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200715{
716 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
717 struct minstrel_rate_stats *mr;
Felix Fietkaua8566662013-04-22 16:14:42 +0200718 u8 idx;
719 u16 flags;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200720
721 mr = minstrel_get_ratestats(mi, index);
722 if (!mr->retry_updated)
723 minstrel_calc_retransmit(mp, mi, index);
724
Felix Fietkaua8566662013-04-22 16:14:42 +0200725 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 Fietkaua0497f92013-02-13 10:51:08 +0100733 }
734
Felix Fietkaua8566662013-04-22 16:14:42 +0200735 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 Beldan7a5e3fa2013-11-11 13:12:55 +0100739 idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
Felix Fietkaua8566662013-04-22 16:14:42 +0200740 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
752static void
753minstrel_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 Huehn59358392014-09-09 23:22:14 +0200762 /* Start with max_tp_rate[0] */
763 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
Felix Fietkaua8566662013-04-22 16:14:42 +0200764
765 if (mp->hw->max_rates >= 3) {
Thomas Huehn59358392014-09-09 23:22:14 +0200766 /* 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 Fietkaua8566662013-04-22 16:14:42 +0200768 }
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 Fietkauec8aa662010-05-13 16:48:03 +0200778}
779
780static inline int
781minstrel_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
787static int
788minstrel_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 Huehn59358392014-09-09 23:22:14 +0200792 unsigned int sample_dur, sample_group, cur_max_tp_streams;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200793 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 Fietkau965237a2013-03-03 12:49:51 +0100803 sample_group = mi->sample_group;
Karl Beldanf12140c2013-11-11 13:10:49 +0100804 mg = &mi->groups[sample_group];
805 sample_idx = sample_table[mg->column][mg->index];
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800806 minstrel_next_sample_idx(mi);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200807
Karl Beldanf12140c2013-11-11 13:10:49 +0100808 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 Fietkauec8aa662010-05-13 16:48:03 +0200814 /*
Helmut Schaaba6fa292012-03-14 13:31:11 +0100815 * Sampling might add some overhead (RTS, no aggregation)
816 * to the frame. Hence, don't use sampling for the currently
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100817 * used rates.
Helmut Schaaba6fa292012-03-14 13:31:11 +0100818 */
Thomas Huehn59358392014-09-09 23:22:14 +0200819 if (sample_idx == mi->max_tp_rate[0] ||
820 sample_idx == mi->max_tp_rate[1] ||
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100821 sample_idx == mi->max_prob_rate)
Helmut Schaaba6fa292012-03-14 13:31:11 +0100822 return -1;
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100823
Helmut Schaaba6fa292012-03-14 13:31:11 +0100824 /*
Felix Fietkaubc96f242013-03-16 17:00:26 +0100825 * Do not sample if the probability is already higher than 95%
826 * to avoid wasting airtime.
Felix Fietkauec8aa662010-05-13 16:48:03 +0200827 */
Felix Fietkaubc96f242013-03-16 17:00:26 +0100828 if (mr->probability > MINSTREL_FRAC(95, 100))
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800829 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200830
831 /*
832 * Make sure that lower rates get sampled only occasionally,
833 * if the link is working perfectly.
834 */
Thomas Huehn59358392014-09-09 23:22:14 +0200835
836 cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
837 MCS_GROUP_RATES].streams;
Felix Fietkau965237a2013-03-03 12:49:51 +0100838 sample_dur = minstrel_get_duration(sample_idx);
Thomas Huehn59358392014-09-09 23:22:14 +0200839 if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) &&
840 (cur_max_tp_streams - 1 <
Felix Fietkau965237a2013-03-03 12:49:51 +0100841 minstrel_mcs_groups[sample_group].streams ||
842 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
Felix Fietkauc7317e42010-10-21 02:47:25 +0200843 if (mr->sample_skipped < 20)
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800844 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200845
846 if (mi->sample_slow++ > 2)
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800847 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200848 }
Felix Fietkau098b8af2013-03-03 12:49:52 +0100849 mi->sample_tries--;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200850
851 return sample_idx;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200852}
853
854static void
Felix Fietkaua0497f92013-02-13 10:51:08 +0100855minstrel_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
870static void
Felix Fietkauec8aa662010-05-13 16:48:03 +0200871minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
872 struct ieee80211_tx_rate_control *txrc)
873{
Felix Fietkaua8566662013-04-22 16:14:42 +0200874 const struct mcs_group *sample_group;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200875 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Felix Fietkaua8566662013-04-22 16:14:42 +0200876 struct ieee80211_tx_rate *rate = &info->status.rates[0];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200877 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 Fietkaua0497f92013-02-13 10:51:08 +0100889 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
Helmut Schaa6de062c2011-08-01 11:32:53 +0200890
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200891#ifdef CONFIG_MAC80211_DEBUGFS
892 if (mp->fixed_rate_idx != -1)
893 return;
894#endif
895
Helmut Schaa6de062c2011-08-01 11:32:53 +0200896 /* Don't use EAPOL frames for sampling on non-mrr hw */
897 if (mp->hw->max_rates == 1 &&
Johannes Bergaf61a162013-07-02 18:09:12 +0200898 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
Helmut Schaa6de062c2011-08-01 11:32:53 +0200899 sample_idx = -1;
900 else
901 sample_idx = minstrel_get_sample_rate(mp, mi);
Zefir Kurtisi24f75802011-05-20 20:29:17 +0200902
Felix Fietkauec8aa662010-05-13 16:48:03 +0200903 mi->total_packets++;
904
905 /* wraparound */
906 if (mi->total_packets == ~0) {
907 mi->total_packets = 0;
908 mi->sample_packets = 0;
909 }
Felix Fietkaua8566662013-04-22 16:14:42 +0200910
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 Fietkau1cd15852013-06-28 21:04:35 +0200916 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 Fietkaua8566662013-04-22 16:14:42 +0200925 rate->idx = sample_idx % MCS_GROUP_RATES +
Karl Beldan7a5e3fa2013-11-11 13:12:55 +0100926 (sample_group->streams - 1) * 8;
Felix Fietkaua8566662013-04-22 16:14:42 +0200927 rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200928}
929
930static void
Felix Fietkaua0497f92013-02-13 10:51:08 +0100931minstrel_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 Fietkau2dfca312013-08-20 19:43:54 +0200940 if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
941 return;
942
Felix Fietkaua0497f92013-02-13 10:51:08 +0100943 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
957static void
Felix Fietkauec8aa662010-05-13 16:48:03 +0200958minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +0200959 struct cfg80211_chan_def *chandef,
Johannes Berg64f68e52012-03-28 10:58:37 +0200960 struct ieee80211_sta *sta, void *priv_sta)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200961{
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 Fietkauec8aa662010-05-13 16:48:03 +0200966 u16 sta_cap = sta->ht_cap.cap;
Felix Fietkau4dc217d2011-03-25 15:30:38 +0100967 int n_supported = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200968 int ack_dur;
969 int stbc;
970 int i;
971
972 /* fall back to the old minstrel for legacy stations */
Felix Fietkau4dc217d2011-03-25 15:30:38 +0100973 if (!sta->ht_cap.ht_supported)
974 goto use_legacy;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200975
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200976 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200977
978 msp->is_ht = true;
979 memset(mi, 0, sizeof(*mi));
Felix Fietkaua8566662013-04-22 16:14:42 +0200980
981 mi->sta = sta;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200982 mi->stats_update = jiffies;
983
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200984 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 Fietkauec8aa662010-05-13 16:48:03 +0200987 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 Fietkauec8aa662010-05-13 16:48:03 +02001008 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
Felix Fietkauec8aa662010-05-13 16:48:03 +02001009 mi->groups[i].supported = 0;
Felix Fietkaua0497f92013-02-13 10:51:08 +01001010 if (i == MINSTREL_CCK_GROUP) {
1011 minstrel_ht_update_cck(mp, mi, sband, sta);
1012 continue;
1013 }
1014
Felix Fietkauec8aa662010-05-13 16:48:03 +02001015 if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001016 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 Fietkauec8aa662010-05-13 16:48:03 +02001023 }
1024
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001025 if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1026 sta->bandwidth < IEEE80211_STA_RX_BW_40)
Felix Fietkauec8aa662010-05-13 16:48:03 +02001027 continue;
1028
Helmut Schaae9219772012-03-09 14:13:45 +01001029 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
Johannes Bergaf0ed692013-02-12 14:21:00 +01001030 if (sta->smps_mode == IEEE80211_SMPS_STATIC &&
Helmut Schaae9219772012-03-09 14:13:45 +01001031 minstrel_mcs_groups[i].streams > 1)
1032 continue;
1033
Felix Fietkauec8aa662010-05-13 16:48:03 +02001034 mi->groups[i].supported =
1035 mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
Felix Fietkau4dc217d2011-03-25 15:30:38 +01001036
1037 if (mi->groups[i].supported)
1038 n_supported++;
Felix Fietkauec8aa662010-05-13 16:48:03 +02001039 }
Felix Fietkau4dc217d2011-03-25 15:30:38 +01001040
1041 if (!n_supported)
1042 goto use_legacy;
1043
Felix Fietkaua8566662013-04-22 16:14:42 +02001044 /* create an initial rate table with the lowest supported rates */
Karl Beldana3647362013-04-18 14:26:21 +02001045 minstrel_ht_update_stats(mp, mi);
Felix Fietkaua8566662013-04-22 16:14:42 +02001046 minstrel_ht_update_rates(mp, mi);
Karl Beldana3647362013-04-18 14:26:21 +02001047
Felix Fietkau4dc217d2011-03-25 15:30:38 +01001048 return;
1049
1050use_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 Wunderlich3de805c2013-07-08 16:55:50 +02001055 return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
1056 &msp->legacy);
Felix Fietkauec8aa662010-05-13 16:48:03 +02001057}
1058
1059static void
1060minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02001061 struct cfg80211_chan_def *chandef,
Felix Fietkauec8aa662010-05-13 16:48:03 +02001062 struct ieee80211_sta *sta, void *priv_sta)
1063{
Simon Wunderlich3de805c2013-07-08 16:55:50 +02001064 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
Felix Fietkauec8aa662010-05-13 16:48:03 +02001065}
1066
1067static void
1068minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02001069 struct cfg80211_chan_def *chandef,
Felix Fietkauec8aa662010-05-13 16:48:03 +02001070 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berg64f68e52012-03-28 10:58:37 +02001071 u32 changed)
Felix Fietkauec8aa662010-05-13 16:48:03 +02001072{
Simon Wunderlich3de805c2013-07-08 16:55:50 +02001073 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
Felix Fietkauec8aa662010-05-13 16:48:03 +02001074}
1075
1076static void *
1077minstrel_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 Huehn472dd352012-06-29 06:26:27 -07001092 msp = kzalloc(sizeof(*msp), gfp);
Felix Fietkauec8aa662010-05-13 16:48:03 +02001093 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
1106error1:
Dan Carpenter7e988012010-07-22 13:14:19 +02001107 kfree(msp->ratelist);
Felix Fietkauec8aa662010-05-13 16:48:03 +02001108error:
1109 kfree(msp);
1110 return NULL;
1111}
1112
1113static void
1114minstrel_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
1123static void *
1124minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
1125{
1126 return mac80211_minstrel.alloc(hw, debugfsdir);
1127}
1128
1129static void
1130minstrel_ht_free(void *priv)
1131{
1132 mac80211_minstrel.free(priv);
1133}
1134
Antonio Quartullicca674d2014-05-19 21:53:20 +02001135static 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 Huehn59358392014-09-09 23:22:14 +02001144 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1145 j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
Antonio Quartullicca674d2014-05-19 21:53:20 +02001146
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 Berg631ad702014-01-20 23:29:34 +01001151static const struct rate_control_ops mac80211_minstrel_ht = {
Felix Fietkauec8aa662010-05-13 16:48:03 +02001152 .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 Quartullicca674d2014-05-19 21:53:20 +02001165 .get_expected_throughput = minstrel_ht_get_expected_throughput,
Felix Fietkauec8aa662010-05-13 16:48:03 +02001166};
1167
1168
Johannes Bergf6e1a732014-01-21 00:32:52 +01001169static void __init init_sample_table(void)
Felix Fietkauec8aa662010-05-13 16:48:03 +02001170{
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 Beldanf7d8ad82013-11-13 10:54:19 +01001176 prandom_bytes(rnd, sizeof(rnd));
Felix Fietkauec8aa662010-05-13 16:48:03 +02001177 for (i = 0; i < MCS_GROUP_RATES; i++) {
Felix Fietkauec8aa662010-05-13 16:48:03 +02001178 new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
Felix Fietkauec8aa662010-05-13 16:48:03 +02001179 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
1187int __init
1188rc80211_minstrel_ht_init(void)
1189{
1190 init_sample_table();
1191 return ieee80211_rate_control_register(&mac80211_minstrel_ht);
1192}
1193
1194void
1195rc80211_minstrel_ht_exit(void)
1196{
1197 ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
1198}