blob: e760d3d71fe1887a83e8477ba0687b4f60e4e0b9 [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 = \
Karl Beldan3ec373c2014-10-20 15:46:01 +020054 IEEE80211_TX_RC_MCS | \
Felix Fietkauec8aa662010-05-13 16:48:03 +020055 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
56 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
57 .duration = { \
58 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
59 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
60 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
61 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
62 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
63 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
64 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
65 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
66 } \
67}
68
Felix Fietkaua0497f92013-02-13 10:51:08 +010069#define CCK_DURATION(_bitrate, _short, _len) \
Felix Fietkaued97a132013-03-02 21:20:12 +010070 (1000 * (10 /* SIFS */ + \
Weilong Chenf359d3f2013-12-18 15:44:16 +080071 (_short ? 72 + 24 : 144 + 48) + \
Felix Fietkaued97a132013-03-02 21:20:12 +010072 (8 * (_len + 4) * 10) / (_bitrate)))
Felix Fietkaua0497f92013-02-13 10:51:08 +010073
74#define CCK_ACK_DURATION(_bitrate, _short) \
75 (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \
76 CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE))
77
78#define CCK_DURATION_LIST(_short) \
79 CCK_ACK_DURATION(10, _short), \
80 CCK_ACK_DURATION(20, _short), \
81 CCK_ACK_DURATION(55, _short), \
82 CCK_ACK_DURATION(110, _short)
83
Karl Beldan8a0ee4f2014-10-20 15:46:00 +020084#define CCK_GROUP \
85 [MINSTREL_CCK_GROUP] = { \
86 .streams = 0, \
Karl Beldan3ec373c2014-10-20 15:46:01 +020087 .flags = 0, \
Karl Beldan8a0ee4f2014-10-20 15:46:00 +020088 .duration = { \
89 CCK_DURATION_LIST(false), \
90 CCK_DURATION_LIST(true) \
91 } \
Felix Fietkaua0497f92013-02-13 10:51:08 +010092 }
93
Felix Fietkauec8aa662010-05-13 16:48:03 +020094/*
95 * To enable sufficiently targeted rate sampling, MCS rates are divided into
96 * groups, based on the number of streams and flags (HT40, SGI) that they
97 * use.
Helmut Schaaa5f69d942011-11-14 15:28:20 +010098 *
99 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200100 * BW -> SGI -> #streams
Felix Fietkauec8aa662010-05-13 16:48:03 +0200101 */
102const struct mcs_group minstrel_mcs_groups[] = {
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200103 MCS_GROUP(1, 0, BW_20),
104 MCS_GROUP(2, 0, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200105#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200106 MCS_GROUP(3, 0, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200107#endif
108
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200109 MCS_GROUP(1, 1, BW_20),
110 MCS_GROUP(2, 1, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200111#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200112 MCS_GROUP(3, 1, BW_20),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200113#endif
114
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200115 MCS_GROUP(1, 0, BW_40),
116 MCS_GROUP(2, 0, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200117#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200118 MCS_GROUP(3, 0, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200119#endif
120
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200121 MCS_GROUP(1, 1, BW_40),
122 MCS_GROUP(2, 1, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200123#if MINSTREL_MAX_STREAMS >= 3
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200124 MCS_GROUP(3, 1, BW_40),
Felix Fietkauec8aa662010-05-13 16:48:03 +0200125#endif
Felix Fietkaua0497f92013-02-13 10:51:08 +0100126
Felix Fietkaua0497f92013-02-13 10:51:08 +0100127 CCK_GROUP
Felix Fietkauec8aa662010-05-13 16:48:03 +0200128};
129
Felix Fietkaua0497f92013-02-13 10:51:08 +0100130
Johannes Bergf6e1a732014-01-21 00:32:52 +0100131static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200132
Felix Fietkaua8566662013-04-22 16:14:42 +0200133static void
134minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
135
Felix Fietkauec8aa662010-05-13 16:48:03 +0200136/*
Felix Fietkauec8aa662010-05-13 16:48:03 +0200137 * Look up an MCS group index based on mac80211 rate information
138 */
139static int
140minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
141{
Karl Beldancc61d8d2014-09-29 02:36:30 +0200142 return GROUP_IDX((rate->idx / 8) + 1,
Helmut Schaaa5f69d942011-11-14 15:28:20 +0100143 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
144 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
Felix Fietkauec8aa662010-05-13 16:48:03 +0200145}
146
Felix Fietkaua0497f92013-02-13 10:51:08 +0100147static struct minstrel_rate_stats *
148minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
149 struct ieee80211_tx_rate *rate)
150{
151 int group, idx;
152
153 if (rate->flags & IEEE80211_TX_RC_MCS) {
154 group = minstrel_ht_get_group_idx(rate);
Karl Beldan7a5e3fa2013-11-11 13:12:55 +0100155 idx = rate->idx % 8;
Felix Fietkaua0497f92013-02-13 10:51:08 +0100156 } else {
157 group = MINSTREL_CCK_GROUP;
158
159 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++)
160 if (rate->idx == mp->cck_rates[idx])
161 break;
162
163 /* short preamble */
164 if (!(mi->groups[group].supported & BIT(idx)))
165 idx += 4;
166 }
167 return &mi->groups[group].rates[idx];
168}
169
Felix Fietkauec8aa662010-05-13 16:48:03 +0200170static inline struct minstrel_rate_stats *
171minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
172{
173 return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
174}
175
176
177/*
178 * Recalculate success probabilities and counters for a rate using EWMA
179 */
180static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100181minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200182{
183 if (unlikely(mr->attempts > 0)) {
184 mr->sample_skipped = 0;
185 mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
186 if (!mr->att_hist)
187 mr->probability = mr->cur_prob;
188 else
189 mr->probability = minstrel_ewma(mr->probability,
190 mr->cur_prob, EWMA_LEVEL);
191 mr->att_hist += mr->attempts;
192 mr->succ_hist += mr->success;
193 } else {
194 mr->sample_skipped++;
195 }
196 mr->last_success = mr->success;
197 mr->last_attempts = mr->attempts;
198 mr->success = 0;
199 mr->attempts = 0;
200}
201
202/*
203 * Calculate throughput based on the average A-MPDU length, taking into account
204 * the expected number of retransmissions and their expected length
205 */
206static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100207minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200208{
209 struct minstrel_rate_stats *mr;
Felix Fietkaued97a132013-03-02 21:20:12 +0100210 unsigned int nsecs = 0;
211 unsigned int tp;
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100212 unsigned int prob;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200213
214 mr = &mi->groups[group].rates[rate];
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100215 prob = mr->probability;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200216
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100217 if (prob < MINSTREL_FRAC(1, 10)) {
Felix Fietkauec8aa662010-05-13 16:48:03 +0200218 mr->cur_tp = 0;
219 return;
220 }
221
Felix Fietkau3e8b1eb2013-03-16 17:00:25 +0100222 /*
223 * For the throughput calculation, limit the probability value to 90% to
224 * account for collision related packet error rate fluctuation
225 */
226 if (prob > MINSTREL_FRAC(9, 10))
227 prob = MINSTREL_FRAC(9, 10);
228
Felix Fietkaua0497f92013-02-13 10:51:08 +0100229 if (group != MINSTREL_CCK_GROUP)
Felix Fietkaued97a132013-03-02 21:20:12 +0100230 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100231
Felix Fietkaued97a132013-03-02 21:20:12 +0100232 nsecs += minstrel_mcs_groups[group].duration[rate];
Felix Fietkaued97a132013-03-02 21:20:12 +0100233
Johannes Berg00591ce2014-05-19 11:24:19 +0200234 /* prob is scaled - see MINSTREL_FRAC above */
235 tp = 1000000 * ((prob * 1000) / nsecs);
Felix Fietkaued97a132013-03-02 21:20:12 +0100236 mr->cur_tp = MINSTREL_TRUNC(tp);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200237}
238
239/*
Thomas Huehn59358392014-09-09 23:22:14 +0200240 * Find & sort topmost throughput rates
241 *
242 * If multiple rates provide equal throughput the sorting is based on their
243 * current success probability. Higher success probability is preferred among
244 * MCS groups, CCK rates do not provide aggregation and are therefore at last.
245 */
246static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200247minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
248 u16 *tp_list)
Thomas Huehn59358392014-09-09 23:22:14 +0200249{
250 int cur_group, cur_idx, cur_thr, cur_prob;
251 int tmp_group, tmp_idx, tmp_thr, tmp_prob;
252 int j = MAX_THR_RATES;
253
254 cur_group = index / MCS_GROUP_RATES;
255 cur_idx = index % MCS_GROUP_RATES;
256 cur_thr = mi->groups[cur_group].rates[cur_idx].cur_tp;
257 cur_prob = mi->groups[cur_group].rates[cur_idx].probability;
258
259 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
260 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
261 tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
262 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
263
264 while (j > 0 && (cur_thr > tmp_thr ||
265 (cur_thr == tmp_thr && cur_prob > tmp_prob))) {
266 j--;
267 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
268 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
269 tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
270 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
271 }
272
273 if (j < MAX_THR_RATES - 1) {
274 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
275 (MAX_THR_RATES - (j + 1))));
276 }
277 if (j < MAX_THR_RATES)
278 tp_list[j] = index;
279}
280
281/*
282 * Find and set the topmost probability rate per sta and per group
283 */
284static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200285minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
Thomas Huehn59358392014-09-09 23:22:14 +0200286{
287 struct minstrel_mcs_group_data *mg;
288 struct minstrel_rate_stats *mr;
289 int tmp_group, tmp_idx, tmp_tp, tmp_prob, max_tp_group;
290
291 mg = &mi->groups[index / MCS_GROUP_RATES];
292 mr = &mg->rates[index % MCS_GROUP_RATES];
293
294 tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
295 tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
296 tmp_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
297 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
298
299 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
300 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
301 max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
302 if((index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) &&
303 (max_tp_group != MINSTREL_CCK_GROUP))
304 return;
305
306 if (mr->probability > MINSTREL_FRAC(75, 100)) {
307 if (mr->cur_tp > tmp_tp)
308 mi->max_prob_rate = index;
309 if (mr->cur_tp > mg->rates[mg->max_group_prob_rate].cur_tp)
310 mg->max_group_prob_rate = index;
311 } else {
312 if (mr->probability > tmp_prob)
313 mi->max_prob_rate = index;
314 if (mr->probability > mg->rates[mg->max_group_prob_rate].probability)
315 mg->max_group_prob_rate = index;
316 }
317}
318
319
320/*
321 * Assign new rate set per sta and use CCK rates only if the fastest
322 * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
323 * rate sets where MCS and CCK rates are mixed, because CCK rates can
324 * not use aggregation.
325 */
326static void
327minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
Karl Beldand4d141c2014-10-20 15:45:59 +0200328 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
329 u16 tmp_cck_tp_rate[MAX_THR_RATES])
Thomas Huehn59358392014-09-09 23:22:14 +0200330{
331 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp;
332 int i;
333
334 tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES;
335 tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES;
336 tmp_cck_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
337
338 tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
339 tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
340 tmp_mcs_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
341
342 if (tmp_cck_tp > tmp_mcs_tp) {
343 for(i = 0; i < MAX_THR_RATES; i++) {
344 minstrel_ht_sort_best_tp_rates(mi, tmp_cck_tp_rate[i],
345 tmp_mcs_tp_rate);
346 }
347 }
348
349}
350
351/*
352 * Try to increase robustness of max_prob rate by decrease number of
353 * streams if possible.
354 */
355static inline void
356minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
357{
358 struct minstrel_mcs_group_data *mg;
359 struct minstrel_rate_stats *mr;
360 int tmp_max_streams, group;
361 int tmp_tp = 0;
362
363 tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
364 MCS_GROUP_RATES].streams;
365 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
366 mg = &mi->groups[group];
367 if (!mg->supported || group == MINSTREL_CCK_GROUP)
368 continue;
369 mr = minstrel_get_ratestats(mi, mg->max_group_prob_rate);
370 if (tmp_tp < mr->cur_tp &&
371 (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
372 mi->max_prob_rate = mg->max_group_prob_rate;
373 tmp_tp = mr->cur_tp;
374 }
375 }
376}
377
378/*
Felix Fietkauec8aa662010-05-13 16:48:03 +0200379 * Update rate statistics and select new primary rates
380 *
381 * Rules for rate selection:
382 * - max_prob_rate must use only one stream, as a tradeoff between delivery
383 * probability and throughput during strong fluctuations
Thomas Huehn59358392014-09-09 23:22:14 +0200384 * - as long as the max prob rate has a probability of more than 75%, pick
Felix Fietkauec8aa662010-05-13 16:48:03 +0200385 * higher throughput rates, even if the probablity is a bit lower
386 */
387static void
388minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
389{
390 struct minstrel_mcs_group_data *mg;
391 struct minstrel_rate_stats *mr;
Thomas Huehn59358392014-09-09 23:22:14 +0200392 int group, i, j;
Karl Beldand4d141c2014-10-20 15:45:59 +0200393 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
394 u16 tmp_cck_tp_rate[MAX_THR_RATES], index;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200395
396 if (mi->ampdu_packets > 0) {
397 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
398 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
399 mi->ampdu_len = 0;
400 mi->ampdu_packets = 0;
401 }
402
403 mi->sample_slow = 0;
404 mi->sample_count = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200405
Thomas Huehn59358392014-09-09 23:22:14 +0200406 /* Initialize global rate indexes */
407 for(j = 0; j < MAX_THR_RATES; j++){
408 tmp_mcs_tp_rate[j] = 0;
409 tmp_cck_tp_rate[j] = 0;
410 }
Karl Beldanc2eb5b02013-04-18 14:26:20 +0200411
Thomas Huehn59358392014-09-09 23:22:14 +0200412 /* Find best rate sets within all MCS groups*/
413 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
Felix Fietkauec8aa662010-05-13 16:48:03 +0200414
415 mg = &mi->groups[group];
416 if (!mg->supported)
417 continue;
418
Felix Fietkauec8aa662010-05-13 16:48:03 +0200419 mi->sample_count++;
420
Thomas Huehn59358392014-09-09 23:22:14 +0200421 /* (re)Initialize group rate indexes */
422 for(j = 0; j < MAX_THR_RATES; j++)
423 tmp_group_tp_rate[j] = group;
424
Felix Fietkauec8aa662010-05-13 16:48:03 +0200425 for (i = 0; i < MCS_GROUP_RATES; i++) {
426 if (!(mg->supported & BIT(i)))
427 continue;
428
Karl Beldan351df092013-11-13 23:07:07 +0100429 index = MCS_GROUP_RATES * group + i;
430
Felix Fietkauec8aa662010-05-13 16:48:03 +0200431 mr = &mg->rates[i];
432 mr->retry_updated = false;
Patrick Kelle6048d7632011-11-15 16:44:48 +0100433 minstrel_calc_rate_ewma(mr);
434 minstrel_ht_calc_tp(mi, group, i);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200435
436 if (!mr->cur_tp)
437 continue;
438
Thomas Huehn59358392014-09-09 23:22:14 +0200439 /* Find max throughput rate set */
440 if (group != MINSTREL_CCK_GROUP) {
441 minstrel_ht_sort_best_tp_rates(mi, index,
442 tmp_mcs_tp_rate);
443 } else if (group == MINSTREL_CCK_GROUP) {
444 minstrel_ht_sort_best_tp_rates(mi, index,
445 tmp_cck_tp_rate);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200446 }
447
Thomas Huehn59358392014-09-09 23:22:14 +0200448 /* Find max throughput rate set within a group */
449 minstrel_ht_sort_best_tp_rates(mi, index,
450 tmp_group_tp_rate);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200451
Thomas Huehn59358392014-09-09 23:22:14 +0200452 /* Find max probability rate per group and global */
453 minstrel_ht_set_best_prob_rate(mi, index);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200454 }
Thomas Huehn59358392014-09-09 23:22:14 +0200455
456 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
457 sizeof(mg->max_group_tp_rate));
Felix Fietkauec8aa662010-05-13 16:48:03 +0200458 }
459
Thomas Huehn59358392014-09-09 23:22:14 +0200460 /* Assign new rate set per sta */
461 minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, tmp_cck_tp_rate);
462 memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
463
464 /* Try to increase robustness of max_prob_rate*/
465 minstrel_ht_prob_rate_reduce_streams(mi);
466
Felix Fietkau96d4ac32013-03-02 21:20:14 +0100467 /* try to sample all available rates during each interval */
468 mi->sample_count *= 8;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200469
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200470#ifdef CONFIG_MAC80211_DEBUGFS
471 /* use fixed index if set */
472 if (mp->fixed_rate_idx != -1) {
Thomas Huehn59358392014-09-09 23:22:14 +0200473 for (i = 0; i < 4; i++)
474 mi->max_tp_rate[i] = mp->fixed_rate_idx;
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200475 mi->max_prob_rate = mp->fixed_rate_idx;
476 }
477#endif
Felix Fietkaua299c6d2013-03-02 21:20:13 +0100478
Thomas Huehn59358392014-09-09 23:22:14 +0200479 /* Reset update timer */
Felix Fietkauec8aa662010-05-13 16:48:03 +0200480 mi->stats_update = jiffies;
481}
482
483static bool
Felix Fietkaua0497f92013-02-13 10:51:08 +0100484minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200485{
Helmut Schaab79296b2011-11-14 15:28:19 +0100486 if (rate->idx < 0)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200487 return false;
488
Helmut Schaab79296b2011-11-14 15:28:19 +0100489 if (!rate->count)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200490 return false;
491
Felix Fietkaua0497f92013-02-13 10:51:08 +0100492 if (rate->flags & IEEE80211_TX_RC_MCS)
493 return true;
494
495 return rate->idx == mp->cck_rates[0] ||
496 rate->idx == mp->cck_rates[1] ||
497 rate->idx == mp->cck_rates[2] ||
498 rate->idx == mp->cck_rates[3];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200499}
500
501static void
502minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
503{
504 struct minstrel_mcs_group_data *mg;
505
506 for (;;) {
507 mi->sample_group++;
508 mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
509 mg = &mi->groups[mi->sample_group];
510
511 if (!mg->supported)
512 continue;
513
514 if (++mg->index >= MCS_GROUP_RATES) {
515 mg->index = 0;
516 if (++mg->column >= ARRAY_SIZE(sample_table))
517 mg->column = 0;
518 }
519 break;
520 }
521}
522
523static void
Karl Beldand4d141c2014-10-20 15:45:59 +0200524minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200525{
526 int group, orig_group;
527
528 orig_group = group = *idx / MCS_GROUP_RATES;
529 while (group > 0) {
530 group--;
531
532 if (!mi->groups[group].supported)
533 continue;
534
535 if (minstrel_mcs_groups[group].streams >
536 minstrel_mcs_groups[orig_group].streams)
537 continue;
538
539 if (primary)
Thomas Huehn59358392014-09-09 23:22:14 +0200540 *idx = mi->groups[group].max_group_tp_rate[0];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200541 else
Thomas Huehn59358392014-09-09 23:22:14 +0200542 *idx = mi->groups[group].max_group_tp_rate[1];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200543 break;
544 }
545}
546
547static void
Patrick Kelle6048d7632011-11-15 16:44:48 +0100548minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200549{
550 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
551 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
552 u16 tid;
553
554 if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
555 return;
556
Johannes Berge133fae2013-08-22 08:36:41 +0200557 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
Felix Fietkauec8aa662010-05-13 16:48:03 +0200558 return;
559
560 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berga622ab72010-06-10 10:21:39 +0200561 if (likely(sta->ampdu_mlme.tid_tx[tid]))
Felix Fietkauec8aa662010-05-13 16:48:03 +0200562 return;
563
Luis R. Rodriguez48124d12010-11-23 15:05:02 -0800564 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
565 return;
566
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530567 ieee80211_start_tx_ba_session(pubsta, tid, 5000);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200568}
569
570static void
571minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
572 struct ieee80211_sta *sta, void *priv_sta,
573 struct sk_buff *skb)
574{
575 struct minstrel_ht_sta_priv *msp = priv_sta;
576 struct minstrel_ht_sta *mi = &msp->ht;
577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
578 struct ieee80211_tx_rate *ar = info->status.rates;
579 struct minstrel_rate_stats *rate, *rate2;
580 struct minstrel_priv *mp = priv;
Felix Fietkaua8566662013-04-22 16:14:42 +0200581 bool last, update = false;
Johannes Berga544ab72012-11-13 21:36:27 +0100582 int i;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200583
584 if (!msp->is_ht)
585 return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
586
587 /* This packet was aggregated but doesn't carry status info */
588 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
589 !(info->flags & IEEE80211_TX_STAT_AMPDU))
590 return;
591
Björn Smedman15d46f32010-10-10 22:14:25 +0200592 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
593 info->status.ampdu_ack_len =
594 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200595 info->status.ampdu_len = 1;
596 }
597
598 mi->ampdu_packets++;
599 mi->ampdu_len += info->status.ampdu_len;
600
601 if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
Felix Fietkauc7317e42010-10-21 02:47:25 +0200602 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkau52c00a32013-03-05 14:20:19 +0100603 mi->sample_tries = 1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200604 mi->sample_count--;
605 }
606
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800607 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200608 mi->sample_packets += info->status.ampdu_len;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200609
Felix Fietkaua0497f92013-02-13 10:51:08 +0100610 last = !minstrel_ht_txstat_valid(mp, &ar[0]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200611 for (i = 0; !last; i++) {
612 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
Felix Fietkaua0497f92013-02-13 10:51:08 +0100613 !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200614
Felix Fietkaua0497f92013-02-13 10:51:08 +0100615 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200616
Björn Smedman15d46f32010-10-10 22:14:25 +0200617 if (last)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200618 rate->success += info->status.ampdu_ack_len;
619
620 rate->attempts += ar[i].count * info->status.ampdu_len;
621 }
622
623 /*
624 * check for sudden death of spatial multiplexing,
625 * downgrade to a lower number of streams if necessary.
626 */
Thomas Huehn59358392014-09-09 23:22:14 +0200627 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200628 if (rate->attempts > 30 &&
629 MINSTREL_FRAC(rate->success, rate->attempts) <
Felix Fietkaua8566662013-04-22 16:14:42 +0200630 MINSTREL_FRAC(20, 100)) {
Thomas Huehn59358392014-09-09 23:22:14 +0200631 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
Felix Fietkaua8566662013-04-22 16:14:42 +0200632 update = true;
633 }
Felix Fietkauec8aa662010-05-13 16:48:03 +0200634
Thomas Huehn59358392014-09-09 23:22:14 +0200635 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
Ming Leiecc3d5a2010-07-01 23:19:50 +0800636 if (rate2->attempts > 30 &&
637 MINSTREL_FRAC(rate2->success, rate2->attempts) <
Felix Fietkaua8566662013-04-22 16:14:42 +0200638 MINSTREL_FRAC(20, 100)) {
Thomas Huehn59358392014-09-09 23:22:14 +0200639 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
Felix Fietkaua8566662013-04-22 16:14:42 +0200640 update = true;
641 }
Felix Fietkauec8aa662010-05-13 16:48:03 +0200642
643 if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
Felix Fietkaua8566662013-04-22 16:14:42 +0200644 update = true;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200645 minstrel_ht_update_stats(mp, mi);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100646 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
647 mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
Patrick Kelle6048d7632011-11-15 16:44:48 +0100648 minstrel_aggr_check(sta, skb);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200649 }
Felix Fietkaua8566662013-04-22 16:14:42 +0200650
651 if (update)
652 minstrel_ht_update_rates(mp, mi);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200653}
654
655static void
656minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
657 int index)
658{
659 struct minstrel_rate_stats *mr;
660 const struct mcs_group *group;
661 unsigned int tx_time, tx_time_rtscts, tx_time_data;
662 unsigned int cw = mp->cw_min;
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700663 unsigned int ctime = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200664 unsigned int t_slot = 9; /* FIXME */
665 unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
Felix Fietkaua0497f92013-02-13 10:51:08 +0100666 unsigned int overhead = 0, overhead_rtscts = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200667
668 mr = minstrel_get_ratestats(mi, index);
669 if (mr->probability < MINSTREL_FRAC(1, 10)) {
670 mr->retry_count = 1;
671 mr->retry_count_rtscts = 1;
672 return;
673 }
674
675 mr->retry_count = 2;
676 mr->retry_count_rtscts = 2;
677 mr->retry_updated = true;
678
679 group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
Felix Fietkaued97a132013-03-02 21:20:12 +0100680 tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700681
682 /* Contention time for first 2 tries */
683 ctime = (t_slot * cw) >> 1;
684 cw = min((cw << 1) | 1, mp->cw_max);
685 ctime += (t_slot * cw) >> 1;
686 cw = min((cw << 1) | 1, mp->cw_max);
687
Felix Fietkaua0497f92013-02-13 10:51:08 +0100688 if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
689 overhead = mi->overhead;
690 overhead_rtscts = mi->overhead_rtscts;
691 }
692
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700693 /* Total TX time for data and Contention after first 2 tries */
Felix Fietkaua0497f92013-02-13 10:51:08 +0100694 tx_time = ctime + 2 * (overhead + tx_time_data);
695 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700696
697 /* See how many more tries we can fit inside segment size */
Felix Fietkauec8aa662010-05-13 16:48:03 +0200698 do {
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700699 /* Contention time for this try */
700 ctime = (t_slot * cw) >> 1;
701 cw = min((cw << 1) | 1, mp->cw_max);
702
703 /* Total TX time after this try */
Felix Fietkaua0497f92013-02-13 10:51:08 +0100704 tx_time += ctime + overhead + tx_time_data;
705 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
Daniel Halperin8fddddf2011-05-10 19:00:45 -0700706
Felix Fietkauec8aa662010-05-13 16:48:03 +0200707 if (tx_time_rtscts < mp->segment_size)
708 mr->retry_count_rtscts++;
709 } while ((tx_time < mp->segment_size) &&
710 (++mr->retry_count < mp->max_retry));
711}
712
713
714static void
715minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
Felix Fietkaua8566662013-04-22 16:14:42 +0200716 struct ieee80211_sta_rates *ratetbl, int offset, int index)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200717{
718 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
719 struct minstrel_rate_stats *mr;
Felix Fietkaua8566662013-04-22 16:14:42 +0200720 u8 idx;
Karl Beldan3ec373c2014-10-20 15:46:01 +0200721 u16 flags = group->flags;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200722
723 mr = minstrel_get_ratestats(mi, index);
724 if (!mr->retry_updated)
725 minstrel_calc_retransmit(mp, mi, index);
726
Felix Fietkaua8566662013-04-22 16:14:42 +0200727 if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
728 ratetbl->rate[offset].count = 2;
729 ratetbl->rate[offset].count_rts = 2;
730 ratetbl->rate[offset].count_cts = 2;
731 } else {
732 ratetbl->rate[offset].count = mr->retry_count;
733 ratetbl->rate[offset].count_cts = mr->retry_count;
734 ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
Felix Fietkaua0497f92013-02-13 10:51:08 +0100735 }
736
Karl Beldan3ec373c2014-10-20 15:46:01 +0200737 if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP)
Felix Fietkaua8566662013-04-22 16:14:42 +0200738 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
Karl Beldan3ec373c2014-10-20 15:46:01 +0200739 else
Karl Beldan7a5e3fa2013-11-11 13:12:55 +0100740 idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
Felix Fietkaua8566662013-04-22 16:14:42 +0200741
742 if (offset > 0) {
743 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
744 flags |= IEEE80211_TX_RC_USE_RTS_CTS;
745 }
746
747 ratetbl->rate[offset].idx = idx;
748 ratetbl->rate[offset].flags = flags;
749}
750
751static void
752minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
753{
754 struct ieee80211_sta_rates *rates;
755 int i = 0;
756
757 rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
758 if (!rates)
759 return;
760
Thomas Huehn59358392014-09-09 23:22:14 +0200761 /* Start with max_tp_rate[0] */
762 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
Felix Fietkaua8566662013-04-22 16:14:42 +0200763
764 if (mp->hw->max_rates >= 3) {
Thomas Huehn59358392014-09-09 23:22:14 +0200765 /* At least 3 tx rates supported, use max_tp_rate[1] next */
766 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
Felix Fietkaua8566662013-04-22 16:14:42 +0200767 }
768
769 if (mp->hw->max_rates >= 2) {
770 /*
771 * At least 2 tx rates supported, use max_prob_rate next */
772 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
773 }
774
775 rates->rate[i].idx = -1;
776 rate_control_set_rates(mp->hw, mi->sta, rates);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200777}
778
779static inline int
780minstrel_get_duration(int index)
781{
782 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
783 return group->duration[index % MCS_GROUP_RATES];
784}
785
786static int
787minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
788{
789 struct minstrel_rate_stats *mr;
790 struct minstrel_mcs_group_data *mg;
Thomas Huehn59358392014-09-09 23:22:14 +0200791 unsigned int sample_dur, sample_group, cur_max_tp_streams;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200792 int sample_idx = 0;
793
794 if (mi->sample_wait > 0) {
795 mi->sample_wait--;
796 return -1;
797 }
798
799 if (!mi->sample_tries)
800 return -1;
801
Felix Fietkau965237a2013-03-03 12:49:51 +0100802 sample_group = mi->sample_group;
Karl Beldanf12140c2013-11-11 13:10:49 +0100803 mg = &mi->groups[sample_group];
804 sample_idx = sample_table[mg->column][mg->index];
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800805 minstrel_next_sample_idx(mi);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200806
Karl Beldanf12140c2013-11-11 13:10:49 +0100807 if (!(mg->supported & BIT(sample_idx)))
808 return -1;
809
810 mr = &mg->rates[sample_idx];
811 sample_idx += sample_group * MCS_GROUP_RATES;
812
Felix Fietkauec8aa662010-05-13 16:48:03 +0200813 /*
Helmut Schaaba6fa292012-03-14 13:31:11 +0100814 * Sampling might add some overhead (RTS, no aggregation)
815 * to the frame. Hence, don't use sampling for the currently
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100816 * used rates.
Helmut Schaaba6fa292012-03-14 13:31:11 +0100817 */
Thomas Huehn59358392014-09-09 23:22:14 +0200818 if (sample_idx == mi->max_tp_rate[0] ||
819 sample_idx == mi->max_tp_rate[1] ||
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100820 sample_idx == mi->max_prob_rate)
Helmut Schaaba6fa292012-03-14 13:31:11 +0100821 return -1;
Felix Fietkaua0ca7962013-03-16 17:00:27 +0100822
Helmut Schaaba6fa292012-03-14 13:31:11 +0100823 /*
Felix Fietkaubc96f242013-03-16 17:00:26 +0100824 * Do not sample if the probability is already higher than 95%
825 * to avoid wasting airtime.
Felix Fietkauec8aa662010-05-13 16:48:03 +0200826 */
Felix Fietkaubc96f242013-03-16 17:00:26 +0100827 if (mr->probability > MINSTREL_FRAC(95, 100))
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800828 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200829
830 /*
831 * Make sure that lower rates get sampled only occasionally,
832 * if the link is working perfectly.
833 */
Thomas Huehn59358392014-09-09 23:22:14 +0200834
835 cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
836 MCS_GROUP_RATES].streams;
Felix Fietkau965237a2013-03-03 12:49:51 +0100837 sample_dur = minstrel_get_duration(sample_idx);
Thomas Huehn59358392014-09-09 23:22:14 +0200838 if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) &&
839 (cur_max_tp_streams - 1 <
Felix Fietkau965237a2013-03-03 12:49:51 +0100840 minstrel_mcs_groups[sample_group].streams ||
841 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
Felix Fietkauc7317e42010-10-21 02:47:25 +0200842 if (mr->sample_skipped < 20)
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800843 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200844
845 if (mi->sample_slow++ > 2)
Daniel Halperin8d5eab52011-03-09 03:10:18 -0800846 return -1;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200847 }
Felix Fietkau098b8af2013-03-03 12:49:52 +0100848 mi->sample_tries--;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200849
850 return sample_idx;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200851}
852
853static void
Felix Fietkaua0497f92013-02-13 10:51:08 +0100854minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
855 struct minstrel_ht_sta *mi, bool val)
856{
857 u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
858
859 if (!supported || !mi->cck_supported_short)
860 return;
861
862 if (supported & (mi->cck_supported_short << (val * 4)))
863 return;
864
865 supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
866 mi->groups[MINSTREL_CCK_GROUP].supported = supported;
867}
868
869static void
Felix Fietkauec8aa662010-05-13 16:48:03 +0200870minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
871 struct ieee80211_tx_rate_control *txrc)
872{
Felix Fietkaua8566662013-04-22 16:14:42 +0200873 const struct mcs_group *sample_group;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Felix Fietkaua8566662013-04-22 16:14:42 +0200875 struct ieee80211_tx_rate *rate = &info->status.rates[0];
Felix Fietkauec8aa662010-05-13 16:48:03 +0200876 struct minstrel_ht_sta_priv *msp = priv_sta;
877 struct minstrel_ht_sta *mi = &msp->ht;
878 struct minstrel_priv *mp = priv;
879 int sample_idx;
880
881 if (rate_control_send_low(sta, priv_sta, txrc))
882 return;
883
884 if (!msp->is_ht)
885 return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
886
887 info->flags |= mi->tx_flags;
Felix Fietkaua0497f92013-02-13 10:51:08 +0100888 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
Helmut Schaa6de062c2011-08-01 11:32:53 +0200889
Lorenzo Bianconi37feb7e2013-08-27 16:59:47 +0200890#ifdef CONFIG_MAC80211_DEBUGFS
891 if (mp->fixed_rate_idx != -1)
892 return;
893#endif
894
Helmut Schaa6de062c2011-08-01 11:32:53 +0200895 /* Don't use EAPOL frames for sampling on non-mrr hw */
896 if (mp->hw->max_rates == 1 &&
Johannes Bergaf61a162013-07-02 18:09:12 +0200897 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
Helmut Schaa6de062c2011-08-01 11:32:53 +0200898 sample_idx = -1;
899 else
900 sample_idx = minstrel_get_sample_rate(mp, mi);
Zefir Kurtisi24f75802011-05-20 20:29:17 +0200901
Felix Fietkauec8aa662010-05-13 16:48:03 +0200902 mi->total_packets++;
903
904 /* wraparound */
905 if (mi->total_packets == ~0) {
906 mi->total_packets = 0;
907 mi->sample_packets = 0;
908 }
Felix Fietkaua8566662013-04-22 16:14:42 +0200909
910 if (sample_idx < 0)
911 return;
912
913 sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
914 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Felix Fietkau1cd15852013-06-28 21:04:35 +0200915 rate->count = 1;
916
917 if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
918 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
919 rate->idx = mp->cck_rates[idx];
Karl Beldan3ec373c2014-10-20 15:46:01 +0200920 } else {
921 rate->idx = sample_idx % MCS_GROUP_RATES +
922 (sample_group->streams - 1) * 8;
Felix Fietkau1cd15852013-06-28 21:04:35 +0200923 }
924
Karl Beldan3ec373c2014-10-20 15:46:01 +0200925 rate->flags = sample_group->flags;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200926}
927
928static void
Felix Fietkaua0497f92013-02-13 10:51:08 +0100929minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
930 struct ieee80211_supported_band *sband,
931 struct ieee80211_sta *sta)
932{
933 int i;
934
935 if (sband->band != IEEE80211_BAND_2GHZ)
936 return;
937
Felix Fietkau2dfca312013-08-20 19:43:54 +0200938 if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
939 return;
940
Felix Fietkaua0497f92013-02-13 10:51:08 +0100941 mi->cck_supported = 0;
942 mi->cck_supported_short = 0;
943 for (i = 0; i < 4; i++) {
944 if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
945 continue;
946
947 mi->cck_supported |= BIT(i);
948 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
949 mi->cck_supported_short |= BIT(i);
950 }
951
952 mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
953}
954
955static void
Felix Fietkauec8aa662010-05-13 16:48:03 +0200956minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +0200957 struct cfg80211_chan_def *chandef,
Johannes Berg64f68e52012-03-28 10:58:37 +0200958 struct ieee80211_sta *sta, void *priv_sta)
Felix Fietkauec8aa662010-05-13 16:48:03 +0200959{
960 struct minstrel_priv *mp = priv;
961 struct minstrel_ht_sta_priv *msp = priv_sta;
962 struct minstrel_ht_sta *mi = &msp->ht;
963 struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200964 u16 sta_cap = sta->ht_cap.cap;
Felix Fietkau4dc217d2011-03-25 15:30:38 +0100965 int n_supported = 0;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200966 int ack_dur;
967 int stbc;
968 int i;
969
970 /* fall back to the old minstrel for legacy stations */
Felix Fietkau4dc217d2011-03-25 15:30:38 +0100971 if (!sta->ht_cap.ht_supported)
972 goto use_legacy;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200973
Karl Beldan8a0ee4f2014-10-20 15:46:00 +0200974 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
Felix Fietkauec8aa662010-05-13 16:48:03 +0200975
976 msp->is_ht = true;
977 memset(mi, 0, sizeof(*mi));
Felix Fietkaua8566662013-04-22 16:14:42 +0200978
979 mi->sta = sta;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200980 mi->stats_update = jiffies;
981
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200982 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
983 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
984 mi->overhead += ack_dur;
Felix Fietkauec8aa662010-05-13 16:48:03 +0200985 mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
986
987 mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
988
989 /* When using MRR, sample more on the first attempt, without delay */
990 if (mp->has_mrr) {
991 mi->sample_count = 16;
992 mi->sample_wait = 0;
993 } else {
994 mi->sample_count = 8;
995 mi->sample_wait = 8;
996 }
997 mi->sample_tries = 4;
998
999 stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
1000 IEEE80211_HT_CAP_RX_STBC_SHIFT;
1001 mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1002
1003 if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
1004 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1005
Felix Fietkauec8aa662010-05-13 16:48:03 +02001006 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
Karl Beldan3ec373c2014-10-20 15:46:01 +02001007 u32 gflags = minstrel_mcs_groups[i].flags;
1008
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
Karl Beldan3ec373c2014-10-20 15:46:01 +02001015 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1016 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001017 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
Karl Beldan3ec373c2014-10-20 15:46:01 +02001025 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001026 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}