mac80211: minstrel_ht: include type (cck/ht) in rates flag
[deliverable/linux.git] / net / mac80211 / rc80211_minstrel_ht.c
CommitLineData
ec8aa669 1/*
a0497f9f 2 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
ec8aa669
FF
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/netdevice.h>
9#include <linux/types.h>
10#include <linux/skbuff.h>
11#include <linux/debugfs.h>
12#include <linux/random.h>
13#include <linux/ieee80211.h>
14#include <net/mac80211.h>
15#include "rate.h"
16#include "rc80211_minstrel.h"
17#include "rc80211_minstrel_ht.h"
18
19#define AVG_PKT_SIZE 1200
ec8aa669
FF
20
21/* Number of bits for an average sized packet */
22#define MCS_NBITS (AVG_PKT_SIZE << 3)
23
24/* Number of symbols for a packet with (bps) bits per symbol */
00591cea 25#define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
ec8aa669 26
ed97a13c 27/* Transmission time (nanoseconds) for a packet containing (syms) symbols */
ec8aa669
FF
28#define MCS_SYMBOL_TIME(sgi, syms) \
29 (sgi ? \
ed97a13c
FF
30 ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
31 ((syms) * 1000) << 2 /* syms * 4 us */ \
ec8aa669
FF
32 )
33
34/* Transmit duration for the raw data part of an average sized packet */
35#define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
36
8a0ee4fe
KB
37#define BW_20 0
38#define BW_40 1
39
a5f69d94
HS
40/*
41 * Define group sort order: HT40 -> SGI -> #streams
42 */
43#define GROUP_IDX(_streams, _sgi, _ht40) \
8a0ee4fe 44 MINSTREL_HT_GROUP_0 + \
a5f69d94 45 MINSTREL_MAX_STREAMS * 2 * _ht40 + \
8a0ee4fe 46 MINSTREL_MAX_STREAMS * _sgi + \
a5f69d94
HS
47 _streams - 1
48
ec8aa669 49/* MCS rate information for an MCS group */
a5f69d94
HS
50#define MCS_GROUP(_streams, _sgi, _ht40) \
51 [GROUP_IDX(_streams, _sgi, _ht40)] = { \
ec8aa669
FF
52 .streams = _streams, \
53 .flags = \
3ec373c4 54 IEEE80211_TX_RC_MCS | \
ec8aa669
FF
55 (_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
a0497f9f 69#define CCK_DURATION(_bitrate, _short, _len) \
ed97a13c 70 (1000 * (10 /* SIFS */ + \
f359d3fe 71 (_short ? 72 + 24 : 144 + 48) + \
ed97a13c 72 (8 * (_len + 4) * 10) / (_bitrate)))
a0497f9f
FF
73
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
8a0ee4fe
KB
84#define CCK_GROUP \
85 [MINSTREL_CCK_GROUP] = { \
86 .streams = 0, \
3ec373c4 87 .flags = 0, \
8a0ee4fe
KB
88 .duration = { \
89 CCK_DURATION_LIST(false), \
90 CCK_DURATION_LIST(true) \
91 } \
a0497f9f
FF
92 }
93
ec8aa669
FF
94/*
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.
a5f69d94
HS
98 *
99 * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
8a0ee4fe 100 * BW -> SGI -> #streams
ec8aa669
FF
101 */
102const struct mcs_group minstrel_mcs_groups[] = {
8a0ee4fe
KB
103 MCS_GROUP(1, 0, BW_20),
104 MCS_GROUP(2, 0, BW_20),
ec8aa669 105#if MINSTREL_MAX_STREAMS >= 3
8a0ee4fe 106 MCS_GROUP(3, 0, BW_20),
ec8aa669
FF
107#endif
108
8a0ee4fe
KB
109 MCS_GROUP(1, 1, BW_20),
110 MCS_GROUP(2, 1, BW_20),
ec8aa669 111#if MINSTREL_MAX_STREAMS >= 3
8a0ee4fe 112 MCS_GROUP(3, 1, BW_20),
ec8aa669
FF
113#endif
114
8a0ee4fe
KB
115 MCS_GROUP(1, 0, BW_40),
116 MCS_GROUP(2, 0, BW_40),
ec8aa669 117#if MINSTREL_MAX_STREAMS >= 3
8a0ee4fe 118 MCS_GROUP(3, 0, BW_40),
ec8aa669
FF
119#endif
120
8a0ee4fe
KB
121 MCS_GROUP(1, 1, BW_40),
122 MCS_GROUP(2, 1, BW_40),
ec8aa669 123#if MINSTREL_MAX_STREAMS >= 3
8a0ee4fe 124 MCS_GROUP(3, 1, BW_40),
ec8aa669 125#endif
a0497f9f 126
a0497f9f 127 CCK_GROUP
ec8aa669
FF
128};
129
a0497f9f 130
f6e1a73b 131static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
ec8aa669 132
a8566662
FF
133static void
134minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
135
ec8aa669
FF
136/*
137 * 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{
cc61d8df 142 return GROUP_IDX((rate->idx / 8) + 1,
a5f69d94
HS
143 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
144 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
ec8aa669
FF
145}
146
a0497f9f
FF
147static 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);
7a5e3fa2 155 idx = rate->idx % 8;
a0497f9f
FF
156 } 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
ec8aa669
FF
170static 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
6048d763 181minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
ec8aa669
FF
182{
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
6048d763 207minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
ec8aa669
FF
208{
209 struct minstrel_rate_stats *mr;
ed97a13c
FF
210 unsigned int nsecs = 0;
211 unsigned int tp;
3e8b1eb2 212 unsigned int prob;
ec8aa669
FF
213
214 mr = &mi->groups[group].rates[rate];
3e8b1eb2 215 prob = mr->probability;
ec8aa669 216
3e8b1eb2 217 if (prob < MINSTREL_FRAC(1, 10)) {
ec8aa669
FF
218 mr->cur_tp = 0;
219 return;
220 }
221
3e8b1eb2
FF
222 /*
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
a0497f9f 229 if (group != MINSTREL_CCK_GROUP)
ed97a13c 230 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
a0497f9f 231
ed97a13c 232 nsecs += minstrel_mcs_groups[group].duration[rate];
ed97a13c 233
00591cea
JB
234 /* prob is scaled - see MINSTREL_FRAC above */
235 tp = 1000000 * ((prob * 1000) / nsecs);
ed97a13c 236 mr->cur_tp = MINSTREL_TRUNC(tp);
ec8aa669
FF
237}
238
5935839a
TH
239/*
240 * 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
d4d141ca
KB
247minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
248 u16 *tp_list)
5935839a
TH
249{
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
d4d141ca 285minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
5935839a
TH
286{
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,
d4d141ca
KB
328 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
329 u16 tmp_cck_tp_rate[MAX_THR_RATES])
5935839a
TH
330{
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
ec8aa669
FF
378/*
379 * 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
5935839a 384 * - as long as the max prob rate has a probability of more than 75%, pick
ec8aa669
FF
385 * 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;
5935839a 392 int group, i, j;
d4d141ca
KB
393 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;
ec8aa669
FF
395
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;
ec8aa669 405
5935839a
TH
406 /* 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 }
c2eb5b0f 411
5935839a
TH
412 /* Find best rate sets within all MCS groups*/
413 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
ec8aa669
FF
414
415 mg = &mi->groups[group];
416 if (!mg->supported)
417 continue;
418
ec8aa669
FF
419 mi->sample_count++;
420
5935839a
TH
421 /* (re)Initialize group rate indexes */
422 for(j = 0; j < MAX_THR_RATES; j++)
423 tmp_group_tp_rate[j] = group;
424
ec8aa669
FF
425 for (i = 0; i < MCS_GROUP_RATES; i++) {
426 if (!(mg->supported & BIT(i)))
427 continue;
428
351df099
KB
429 index = MCS_GROUP_RATES * group + i;
430
ec8aa669
FF
431 mr = &mg->rates[i];
432 mr->retry_updated = false;
6048d763
PK
433 minstrel_calc_rate_ewma(mr);
434 minstrel_ht_calc_tp(mi, group, i);
ec8aa669
FF
435
436 if (!mr->cur_tp)
437 continue;
438
5935839a
TH
439 /* 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);
ec8aa669 446 }
ec8aa669 447
5935839a
TH
448 /* Find max throughput rate set within a group */
449 minstrel_ht_sort_best_tp_rates(mi, index,
450 tmp_group_tp_rate);
ec8aa669 451
5935839a
TH
452 /* Find max probability rate per group and global */
453 minstrel_ht_set_best_prob_rate(mi, index);
ec8aa669
FF
454 }
455
5935839a
TH
456 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
457 sizeof(mg->max_group_tp_rate));
ec8aa669
FF
458 }
459
5935839a
TH
460 /* 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));
a299c6d5 463
5935839a
TH
464 /* Try to increase robustness of max_prob_rate*/
465 minstrel_ht_prob_rate_reduce_streams(mi);
466
467 /* try to sample all available rates during each interval */
468 mi->sample_count *= 8;
a299c6d5 469
37feb7e2
LB
470#ifdef CONFIG_MAC80211_DEBUGFS
471 /* use fixed index if set */
472 if (mp->fixed_rate_idx != -1) {
5935839a
TH
473 for (i = 0; i < 4; i++)
474 mi->max_tp_rate[i] = mp->fixed_rate_idx;
37feb7e2
LB
475 mi->max_prob_rate = mp->fixed_rate_idx;
476 }
477#endif
a299c6d5 478
5935839a 479 /* Reset update timer */
ec8aa669
FF
480 mi->stats_update = jiffies;
481}
482
483static bool
a0497f9f 484minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
ec8aa669 485{
b79296be 486 if (rate->idx < 0)
ec8aa669
FF
487 return false;
488
b79296be 489 if (!rate->count)
ec8aa669
FF
490 return false;
491
a0497f9f
FF
492 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];
ec8aa669
FF
499}
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
d4d141ca 524minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
ec8aa669
FF
525{
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)
5935839a 540 *idx = mi->groups[group].max_group_tp_rate[0];
ec8aa669 541 else
5935839a 542 *idx = mi->groups[group].max_group_tp_rate[1];
ec8aa669
FF
543 break;
544 }
545}
546
547static void
6048d763 548minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
ec8aa669
FF
549{
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
e133fae2 557 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
ec8aa669
FF
558 return;
559
560 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
a622ab72 561 if (likely(sta->ampdu_mlme.tid_tx[tid]))
ec8aa669
FF
562 return;
563
48124d1a
LR
564 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
565 return;
566
bd2ce6e4 567 ieee80211_start_tx_ba_session(pubsta, tid, 5000);
ec8aa669
FF
568}
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;
a8566662 581 bool last, update = false;
a544ab7f 582 int i;
ec8aa669
FF
583
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
15d46f38
BS
592 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
593 info->status.ampdu_ack_len =
594 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
ec8aa669
FF
595 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) {
c7317e41 602 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
52c00a37 603 mi->sample_tries = 1;
ec8aa669
FF
604 mi->sample_count--;
605 }
606
8d5eab5a 607 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
ec8aa669 608 mi->sample_packets += info->status.ampdu_len;
ec8aa669 609
a0497f9f 610 last = !minstrel_ht_txstat_valid(mp, &ar[0]);
ec8aa669
FF
611 for (i = 0; !last; i++) {
612 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
a0497f9f 613 !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
ec8aa669 614
a0497f9f 615 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
ec8aa669 616
15d46f38 617 if (last)
ec8aa669
FF
618 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 */
5935839a 627 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
ec8aa669
FF
628 if (rate->attempts > 30 &&
629 MINSTREL_FRAC(rate->success, rate->attempts) <
a8566662 630 MINSTREL_FRAC(20, 100)) {
5935839a 631 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
a8566662
FF
632 update = true;
633 }
ec8aa669 634
5935839a 635 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
ecc3d5ae
ML
636 if (rate2->attempts > 30 &&
637 MINSTREL_FRAC(rate2->success, rate2->attempts) <
a8566662 638 MINSTREL_FRAC(20, 100)) {
5935839a 639 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
a8566662
FF
640 update = true;
641 }
ec8aa669
FF
642
643 if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
a8566662 644 update = true;
ec8aa669 645 minstrel_ht_update_stats(mp, mi);
a0497f9f
FF
646 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
647 mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
6048d763 648 minstrel_aggr_check(sta, skb);
ec8aa669 649 }
a8566662
FF
650
651 if (update)
652 minstrel_ht_update_rates(mp, mi);
ec8aa669
FF
653}
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;
8fddddff 663 unsigned int ctime = 0;
ec8aa669
FF
664 unsigned int t_slot = 9; /* FIXME */
665 unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
a0497f9f 666 unsigned int overhead = 0, overhead_rtscts = 0;
ec8aa669
FF
667
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];
ed97a13c 680 tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
8fddddff
DH
681
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
a0497f9f
FF
688 if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
689 overhead = mi->overhead;
690 overhead_rtscts = mi->overhead_rtscts;
691 }
692
8fddddff 693 /* Total TX time for data and Contention after first 2 tries */
a0497f9f
FF
694 tx_time = ctime + 2 * (overhead + tx_time_data);
695 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
8fddddff
DH
696
697 /* See how many more tries we can fit inside segment size */
ec8aa669 698 do {
8fddddff
DH
699 /* 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 */
a0497f9f
FF
704 tx_time += ctime + overhead + tx_time_data;
705 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
8fddddff 706
ec8aa669
FF
707 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,
a8566662 716 struct ieee80211_sta_rates *ratetbl, int offset, int index)
ec8aa669
FF
717{
718 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
719 struct minstrel_rate_stats *mr;
a8566662 720 u8 idx;
3ec373c4 721 u16 flags = group->flags;
ec8aa669
FF
722
723 mr = minstrel_get_ratestats(mi, index);
724 if (!mr->retry_updated)
725 minstrel_calc_retransmit(mp, mi, index);
726
a8566662
FF
727 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;
735 }
a0497f9f 736
3ec373c4 737 if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP)
a8566662 738 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
3ec373c4 739 else
7a5e3fa2 740 idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
a8566662
FF
741
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)
a0497f9f 759 return;
a8566662 760
5935839a
TH
761 /* Start with max_tp_rate[0] */
762 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
a8566662
FF
763
764 if (mp->hw->max_rates >= 3) {
5935839a
TH
765 /* 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]);
a8566662
FF
767 }
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);
a0497f9f
FF
773 }
774
a8566662
FF
775 rates->rate[i].idx = -1;
776 rate_control_set_rates(mp->hw, mi->sta, rates);
ec8aa669
FF
777}
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;
5935839a 791 unsigned int sample_dur, sample_group, cur_max_tp_streams;
ec8aa669
FF
792 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
f12140c0
KB
802 sample_group = mi->sample_group;
803 mg = &mi->groups[sample_group];
ec8aa669 804 sample_idx = sample_table[mg->column][mg->index];
f12140c0
KB
805 minstrel_next_sample_idx(mi);
806
807 if (!(mg->supported & BIT(sample_idx)))
808 return -1;
809
ec8aa669 810 mr = &mg->rates[sample_idx];
965237ab 811 sample_idx += sample_group * MCS_GROUP_RATES;
ec8aa669 812
ba6fa29c
HS
813 /*
814 * Sampling might add some overhead (RTS, no aggregation)
815 * to the frame. Hence, don't use sampling for the currently
a0ca796c 816 * used rates.
ba6fa29c 817 */
5935839a
TH
818 if (sample_idx == mi->max_tp_rate[0] ||
819 sample_idx == mi->max_tp_rate[1] ||
a0ca796c 820 sample_idx == mi->max_prob_rate)
ba6fa29c 821 return -1;
a0ca796c 822
ec8aa669 823 /*
bc96f242
FF
824 * Do not sample if the probability is already higher than 95%
825 * to avoid wasting airtime.
ec8aa669 826 */
bc96f242 827 if (mr->probability > MINSTREL_FRAC(95, 100))
8d5eab5a 828 return -1;
ec8aa669
FF
829
830 /*
831 * Make sure that lower rates get sampled only occasionally,
832 * if the link is working perfectly.
833 */
5935839a
TH
834
835 cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
836 MCS_GROUP_RATES].streams;
965237ab 837 sample_dur = minstrel_get_duration(sample_idx);
5935839a
TH
838 if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) &&
839 (cur_max_tp_streams - 1 <
965237ab
FF
840 minstrel_mcs_groups[sample_group].streams ||
841 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
c7317e41 842 if (mr->sample_skipped < 20)
8d5eab5a 843 return -1;
ec8aa669
FF
844
845 if (mi->sample_slow++ > 2)
8d5eab5a 846 return -1;
ec8aa669 847 }
098b8afb 848 mi->sample_tries--;
ec8aa669
FF
849
850 return sample_idx;
ec8aa669
FF
851}
852
a0497f9f
FF
853static void
854minstrel_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
ec8aa669
FF
869static void
870minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
871 struct ieee80211_tx_rate_control *txrc)
872{
a8566662 873 const struct mcs_group *sample_group;
ec8aa669 874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
a8566662 875 struct ieee80211_tx_rate *rate = &info->status.rates[0];
ec8aa669
FF
876 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;
a0497f9f 888 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
6de062ce 889
37feb7e2
LB
890#ifdef CONFIG_MAC80211_DEBUGFS
891 if (mp->fixed_rate_idx != -1)
892 return;
893#endif
894
6de062ce
HS
895 /* Don't use EAPOL frames for sampling on non-mrr hw */
896 if (mp->hw->max_rates == 1 &&
af61a165 897 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
6de062ce
HS
898 sample_idx = -1;
899 else
900 sample_idx = minstrel_get_sample_rate(mp, mi);
24f7580e 901
ec8aa669
FF
902 mi->total_packets++;
903
904 /* wraparound */
905 if (mi->total_packets == ~0) {
906 mi->total_packets = 0;
907 mi->sample_packets = 0;
908 }
a8566662
FF
909
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;
1cd15857
FF
915 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];
3ec373c4
KB
920 } else {
921 rate->idx = sample_idx % MCS_GROUP_RATES +
922 (sample_group->streams - 1) * 8;
1cd15857
FF
923 }
924
3ec373c4 925 rate->flags = sample_group->flags;
ec8aa669
FF
926}
927
a0497f9f
FF
928static void
929minstrel_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
2dfca312
FF
938 if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
939 return;
940
a0497f9f
FF
941 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
ec8aa669
FF
955static void
956minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
3de805cf 957 struct cfg80211_chan_def *chandef,
64f68e5d 958 struct ieee80211_sta *sta, void *priv_sta)
ec8aa669
FF
959{
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;
ec8aa669 964 u16 sta_cap = sta->ht_cap.cap;
4dc217df 965 int n_supported = 0;
ec8aa669
FF
966 int ack_dur;
967 int stbc;
968 int i;
969
970 /* fall back to the old minstrel for legacy stations */
4dc217df
FF
971 if (!sta->ht_cap.ht_supported)
972 goto use_legacy;
ec8aa669 973
8a0ee4fe 974 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
ec8aa669
FF
975
976 msp->is_ht = true;
977 memset(mi, 0, sizeof(*mi));
a8566662
FF
978
979 mi->sta = sta;
ec8aa669
FF
980 mi->stats_update = jiffies;
981
438b61b7
SW
982 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;
ec8aa669
FF
985 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
ec8aa669 1006 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
3ec373c4
KB
1007 u32 gflags = minstrel_mcs_groups[i].flags;
1008
ec8aa669 1009 mi->groups[i].supported = 0;
a0497f9f
FF
1010 if (i == MINSTREL_CCK_GROUP) {
1011 minstrel_ht_update_cck(mp, mi, sband, sta);
1012 continue;
1013 }
1014
3ec373c4
KB
1015 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1016 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
e1a0c6b3
JB
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 }
ec8aa669
FF
1023 }
1024
3ec373c4 1025 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
e1a0c6b3 1026 sta->bandwidth < IEEE80211_STA_RX_BW_40)
ec8aa669
FF
1027 continue;
1028
e9219779 1029 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
af0ed69b 1030 if (sta->smps_mode == IEEE80211_SMPS_STATIC &&
e9219779
HS
1031 minstrel_mcs_groups[i].streams > 1)
1032 continue;
1033
ec8aa669
FF
1034 mi->groups[i].supported =
1035 mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
4dc217df
FF
1036
1037 if (mi->groups[i].supported)
1038 n_supported++;
ec8aa669 1039 }
4dc217df
FF
1040
1041 if (!n_supported)
1042 goto use_legacy;
1043
a8566662 1044 /* create an initial rate table with the lowest supported rates */
a3647362 1045 minstrel_ht_update_stats(mp, mi);
a8566662 1046 minstrel_ht_update_rates(mp, mi);
a3647362 1047
4dc217df
FF
1048 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;
3de805cf
SW
1055 return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
1056 &msp->legacy);
ec8aa669
FF
1057}
1058
1059static void
1060minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
3de805cf 1061 struct cfg80211_chan_def *chandef,
ec8aa669
FF
1062 struct ieee80211_sta *sta, void *priv_sta)
1063{
3de805cf 1064 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
ec8aa669
FF
1065}
1066
1067static void
1068minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
3de805cf 1069 struct cfg80211_chan_def *chandef,
ec8aa669 1070 struct ieee80211_sta *sta, void *priv_sta,
64f68e5d 1071 u32 changed)
ec8aa669 1072{
3de805cf 1073 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
ec8aa669
FF
1074}
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
472dd35c 1092 msp = kzalloc(sizeof(*msp), gfp);
ec8aa669
FF
1093 if (!msp)
1094 return NULL;
1095
1096 msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
1097 if (!msp->ratelist)
1098 goto error;
1099
1100 msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
1101 if (!msp->sample_table)
1102 goto error1;
1103
1104 return msp;
1105
1106error1:
7e988014 1107 kfree(msp->ratelist);
ec8aa669
FF
1108error:
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
cca674d4
AQ
1135static 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
5935839a
TH
1144 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1145 j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
cca674d4
AQ
1146
1147 /* convert cur_tp from pkt per second in kbps */
1148 return mi->groups[i].rates[j].cur_tp * AVG_PKT_SIZE * 8 / 1024;
1149}
1150
631ad703 1151static const struct rate_control_ops mac80211_minstrel_ht = {
ec8aa669
FF
1152 .name = "minstrel_ht",
1153 .tx_status = minstrel_ht_tx_status,
1154 .get_rate = minstrel_ht_get_rate,
1155 .rate_init = minstrel_ht_rate_init,
1156 .rate_update = minstrel_ht_rate_update,
1157 .alloc_sta = minstrel_ht_alloc_sta,
1158 .free_sta = minstrel_ht_free_sta,
1159 .alloc = minstrel_ht_alloc,
1160 .free = minstrel_ht_free,
1161#ifdef CONFIG_MAC80211_DEBUGFS
1162 .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
1163 .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
1164#endif
cca674d4 1165 .get_expected_throughput = minstrel_ht_get_expected_throughput,
ec8aa669
FF
1166};
1167
1168
f6e1a73b 1169static void __init init_sample_table(void)
ec8aa669
FF
1170{
1171 int col, i, new_idx;
1172 u8 rnd[MCS_GROUP_RATES];
1173
1174 memset(sample_table, 0xff, sizeof(sample_table));
1175 for (col = 0; col < SAMPLE_COLUMNS; col++) {
f7d8ad81 1176 prandom_bytes(rnd, sizeof(rnd));
ec8aa669 1177 for (i = 0; i < MCS_GROUP_RATES; i++) {
ec8aa669 1178 new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
ec8aa669
FF
1179 while (sample_table[col][new_idx] != 0xff)
1180 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
1181
1182 sample_table[col][new_idx] = i;
1183 }
1184 }
1185}
1186
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}
This page took 0.280365 seconds and 5 git commands to generate.