ssb: activate CONFIG_SSB_BLOCKIO in the kernel and not in compat-wireless.
[openwrt.git] / package / mac80211 / patches / 530-minstrel_ht.patch
1 --- a/net/mac80211/Makefile
2 +++ b/net/mac80211/Makefile
3 @@ -47,8 +47,8 @@ CFLAGS_driver-trace.o := -I$(src)
4 rc80211_pid-y := rc80211_pid_algo.o
5 rc80211_pid-$(CONFIG_MAC80211_DEBUGFS) += rc80211_pid_debugfs.o
6
7 -rc80211_minstrel-y := rc80211_minstrel.o
8 -rc80211_minstrel-$(CONFIG_MAC80211_DEBUGFS) += rc80211_minstrel_debugfs.o
9 +rc80211_minstrel-y := rc80211_minstrel.o rc80211_minstrel_ht.o
10 +rc80211_minstrel-$(CONFIG_MAC80211_DEBUGFS) += rc80211_minstrel_debugfs.o rc80211_minstrel_ht_debugfs.o
11
12 mac80211-$(CONFIG_MAC80211_RC_PID) += $(rc80211_pid-y)
13 mac80211-$(CONFIG_MAC80211_RC_MINSTREL) += $(rc80211_minstrel-y)
14 --- a/net/mac80211/main.c
15 +++ b/net/mac80211/main.c
16 @@ -716,6 +716,10 @@ static int __init ieee80211_init(void)
17 if (ret)
18 return ret;
19
20 + ret = rc80211_minstrel_ht_init();
21 + if (ret)
22 + goto err_minstrel;
23 +
24 ret = rc80211_pid_init();
25 if (ret)
26 goto err_pid;
27 @@ -728,6 +732,8 @@ static int __init ieee80211_init(void)
28 err_netdev:
29 rc80211_pid_exit();
30 err_pid:
31 + rc80211_minstrel_ht_exit();
32 + err_minstrel:
33 rc80211_minstrel_exit();
34
35 return ret;
36 @@ -736,6 +742,7 @@ static int __init ieee80211_init(void)
37 static void __exit ieee80211_exit(void)
38 {
39 rc80211_pid_exit();
40 + rc80211_minstrel_ht_exit();
41 rc80211_minstrel_exit();
42
43 /*
44 --- a/net/mac80211/rate.h
45 +++ b/net/mac80211/rate.h
46 @@ -137,6 +137,8 @@ static inline void rc80211_pid_exit(void
47 #ifdef CONFIG_MAC80211_RC_MINSTREL
48 extern int rc80211_minstrel_init(void);
49 extern void rc80211_minstrel_exit(void);
50 +extern int rc80211_minstrel_ht_init(void);
51 +extern void rc80211_minstrel_ht_exit(void);
52 #else
53 static inline int rc80211_minstrel_init(void)
54 {
55 @@ -145,6 +147,13 @@ static inline int rc80211_minstrel_init(
56 static inline void rc80211_minstrel_exit(void)
57 {
58 }
59 +static inline int rc80211_minstrel_ht_init(void)
60 +{
61 + return 0;
62 +}
63 +static inline void rc80211_minstrel_ht_exit(void)
64 +{
65 +}
66 #endif
67
68
69 --- /dev/null
70 +++ b/net/mac80211/rc80211_minstrel_ht.c
71 @@ -0,0 +1,822 @@
72 +/*
73 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
74 + *
75 + * This program is free software; you can redistribute it and/or modify
76 + * it under the terms of the GNU General Public License version 2 as
77 + * published by the Free Software Foundation.
78 + */
79 +#include <linux/netdevice.h>
80 +#include <linux/types.h>
81 +#include <linux/skbuff.h>
82 +#include <linux/debugfs.h>
83 +#include <linux/random.h>
84 +#include <linux/ieee80211.h>
85 +#include <net/mac80211.h>
86 +#include "rate.h"
87 +#include "rc80211_minstrel.h"
88 +#include "rc80211_minstrel_ht.h"
89 +
90 +#define AVG_PKT_SIZE 1200
91 +#define SAMPLE_COLUMNS 10
92 +#define EWMA_LEVEL 75
93 +
94 +/* Number of bits for an average sized packet */
95 +#define MCS_NBITS (AVG_PKT_SIZE << 3)
96 +
97 +/* Number of symbols for a packet with (bps) bits per symbol */
98 +#define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
99 +
100 +/* Transmission time for a packet containing (syms) symbols */
101 +#define MCS_SYMBOL_TIME(sgi, syms) \
102 + (sgi ? \
103 + ((syms) * 18 + 4) / 5 : /* syms * 3.6 us */ \
104 + (syms) << 2 /* syms * 4 us */ \
105 + )
106 +
107 +/* Transmit duration for the raw data part of an average sized packet */
108 +#define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
109 +
110 +/* MCS rate information for an MCS group */
111 +#define MCS_GROUP(_streams, _sgi, _ht40) { \
112 + .streams = _streams, \
113 + .flags = \
114 + (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
115 + (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
116 + .duration = { \
117 + MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
118 + MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
119 + MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
120 + MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
121 + MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
122 + MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
123 + MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
124 + MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
125 + } \
126 +}
127 +
128 +/*
129 + * To enable sufficiently targeted rate sampling, MCS rates are divided into
130 + * groups, based on the number of streams and flags (HT40, SGI) that they
131 + * use.
132 + */
133 +const struct mcs_group minstrel_mcs_groups[] = {
134 + MCS_GROUP(1, 0, 0),
135 + MCS_GROUP(2, 0, 0),
136 +#if MINSTREL_MAX_STREAMS >= 3
137 + MCS_GROUP(3, 0, 0),
138 +#endif
139 +
140 + MCS_GROUP(1, 1, 0),
141 + MCS_GROUP(2, 1, 0),
142 +#if MINSTREL_MAX_STREAMS >= 3
143 + MCS_GROUP(3, 1, 0),
144 +#endif
145 +
146 + MCS_GROUP(1, 0, 1),
147 + MCS_GROUP(2, 0, 1),
148 +#if MINSTREL_MAX_STREAMS >= 3
149 + MCS_GROUP(3, 0, 1),
150 +#endif
151 +
152 + MCS_GROUP(1, 1, 1),
153 + MCS_GROUP(2, 1, 1),
154 +#if MINSTREL_MAX_STREAMS >= 3
155 + MCS_GROUP(3, 1, 1),
156 +#endif
157 +};
158 +
159 +static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
160 +
161 +/*
162 + * Perform EWMA (Exponentially Weighted Moving Average) calculation
163 + */
164 +static int
165 +minstrel_ewma(int old, int new, int weight)
166 +{
167 + return (new * (100 - weight) + old * weight) / 100;
168 +}
169 +
170 +/*
171 + * Look up an MCS group index based on mac80211 rate information
172 + */
173 +static int
174 +minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
175 +{
176 + int streams = (rate->idx / MCS_GROUP_RATES) + 1;
177 + u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;
178 + int i;
179 +
180 + for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
181 + if (minstrel_mcs_groups[i].streams != streams)
182 + continue;
183 + if (minstrel_mcs_groups[i].flags != (rate->flags & flags))
184 + continue;
185 +
186 + return i;
187 + }
188 +
189 + WARN_ON(1);
190 + return 0;
191 +}
192 +
193 +static inline struct minstrel_rate_stats *
194 +minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
195 +{
196 + return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
197 +}
198 +
199 +
200 +/*
201 + * Recalculate success probabilities and counters for a rate using EWMA
202 + */
203 +static void
204 +minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
205 +{
206 + if (unlikely(mr->attempts > 0)) {
207 + mr->sample_skipped = 0;
208 + mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
209 + if (!mr->att_hist)
210 + mr->probability = mr->cur_prob;
211 + else
212 + mr->probability = minstrel_ewma(mr->probability,
213 + mr->cur_prob, EWMA_LEVEL);
214 + mr->att_hist += mr->attempts;
215 + mr->succ_hist += mr->success;
216 + } else {
217 + mr->sample_skipped++;
218 + }
219 + mr->last_success = mr->success;
220 + mr->last_attempts = mr->attempts;
221 + mr->success = 0;
222 + mr->attempts = 0;
223 +}
224 +
225 +/*
226 + * Calculate throughput based on the average A-MPDU length, taking into account
227 + * the expected number of retransmissions and their expected length
228 + */
229 +static void
230 +minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
231 + int group, int rate)
232 +{
233 + struct minstrel_rate_stats *mr;
234 + unsigned int usecs;
235 +
236 + mr = &mi->groups[group].rates[rate];
237 +
238 + if (mr->probability < MINSTREL_FRAC(1, 10)) {
239 + mr->cur_tp = 0;
240 + return;
241 + }
242 +
243 + usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
244 + usecs += minstrel_mcs_groups[group].duration[rate];
245 + mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
246 +}
247 +
248 +/*
249 + * Update rate statistics and select new primary rates
250 + *
251 + * Rules for rate selection:
252 + * - max_prob_rate must use only one stream, as a tradeoff between delivery
253 + * probability and throughput during strong fluctuations
254 + * - as long as the max prob rate has a probability of more than 3/4, pick
255 + * higher throughput rates, even if the probablity is a bit lower
256 + */
257 +static void
258 +minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
259 +{
260 + struct minstrel_mcs_group_data *mg;
261 + struct minstrel_rate_stats *mr;
262 + int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
263 + int group, i, index;
264 +
265 + if (mi->ampdu_packets > 0) {
266 + mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
267 + MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
268 + mi->ampdu_len = 0;
269 + mi->ampdu_packets = 0;
270 + }
271 +
272 + mi->sample_slow = 0;
273 + mi->sample_count = 0;
274 + mi->max_tp_rate = 0;
275 + mi->max_tp_rate2 = 0;
276 + mi->max_prob_rate = 0;
277 +
278 + for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
279 + cur_prob = 0;
280 + cur_prob_tp = 0;
281 + cur_tp = 0;
282 + cur_tp2 = 0;
283 +
284 + mg = &mi->groups[group];
285 + if (!mg->supported)
286 + continue;
287 +
288 + mg->max_tp_rate = 0;
289 + mg->max_tp_rate2 = 0;
290 + mg->max_prob_rate = 0;
291 + mi->sample_count++;
292 +
293 + for (i = 0; i < MCS_GROUP_RATES; i++) {
294 + if (!(mg->supported & BIT(i)))
295 + continue;
296 +
297 + mr = &mg->rates[i];
298 + mr->retry_updated = false;
299 + index = MCS_GROUP_RATES * group + i;
300 + minstrel_calc_rate_ewma(mp, mr);
301 + minstrel_ht_calc_tp(mp, mi, group, i);
302 +
303 + if (!mr->cur_tp)
304 + continue;
305 +
306 + /* ignore the lowest rate of each single-stream group */
307 + if (!i && minstrel_mcs_groups[group].streams == 1)
308 + continue;
309 +
310 + if ((mr->cur_tp > cur_prob_tp && mr->probability >
311 + MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
312 + mg->max_prob_rate = index;
313 + cur_prob = mr->probability;
314 + }
315 +
316 + if (mr->cur_tp > cur_tp) {
317 + swap(index, mg->max_tp_rate);
318 + cur_tp = mr->cur_tp;
319 + mr = minstrel_get_ratestats(mi, index);
320 + }
321 +
322 + if (index >= mg->max_tp_rate)
323 + continue;
324 +
325 + if (mr->cur_tp > cur_tp2) {
326 + mg->max_tp_rate2 = index;
327 + cur_tp2 = mr->cur_tp;
328 + }
329 + }
330 + }
331 +
332 + /* try to sample up to half of the availble rates during each interval */
333 + mi->sample_count *= 4;
334 +
335 + cur_prob = 0;
336 + cur_prob_tp = 0;
337 + cur_tp = 0;
338 + cur_tp2 = 0;
339 + for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
340 + mg = &mi->groups[group];
341 + if (!mg->supported)
342 + continue;
343 +
344 + mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
345 + if (cur_prob_tp < mr->cur_tp &&
346 + minstrel_mcs_groups[group].streams == 1) {
347 + mi->max_prob_rate = mg->max_prob_rate;
348 + cur_prob = mr->cur_prob;
349 + }
350 +
351 + mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
352 + if (cur_tp < mr->cur_tp) {
353 + mi->max_tp_rate = mg->max_tp_rate;
354 + cur_tp = mr->cur_tp;
355 + }
356 +
357 + mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
358 + if (cur_tp2 < mr->cur_tp) {
359 + mi->max_tp_rate2 = mg->max_tp_rate2;
360 + cur_tp2 = mr->cur_tp;
361 + }
362 + }
363 +
364 + mi->stats_update = jiffies;
365 +}
366 +
367 +static bool
368 +minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
369 +{
370 + if (!rate->count)
371 + return false;
372 +
373 + if (rate->idx < 0)
374 + return false;
375 +
376 + return !!(rate->flags & IEEE80211_TX_RC_MCS);
377 +}
378 +
379 +static void
380 +minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
381 +{
382 + struct minstrel_mcs_group_data *mg;
383 +
384 + for (;;) {
385 + mi->sample_group++;
386 + mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
387 + mg = &mi->groups[mi->sample_group];
388 +
389 + if (!mg->supported)
390 + continue;
391 +
392 + if (++mg->index >= MCS_GROUP_RATES) {
393 + mg->index = 0;
394 + if (++mg->column >= ARRAY_SIZE(sample_table))
395 + mg->column = 0;
396 + }
397 + break;
398 + }
399 +}
400 +
401 +static void
402 +minstrel_downgrade_rate(struct minstrel_ht_sta *mi, int *idx, bool primary)
403 +{
404 + int group, orig_group;
405 +
406 + orig_group = group = *idx / MCS_GROUP_RATES;
407 + while (group > 0) {
408 + group--;
409 +
410 + if (!mi->groups[group].supported)
411 + continue;
412 +
413 + if (minstrel_mcs_groups[group].streams >
414 + minstrel_mcs_groups[orig_group].streams)
415 + continue;
416 +
417 + if (primary)
418 + *idx = mi->groups[group].max_tp_rate;
419 + else
420 + *idx = mi->groups[group].max_tp_rate2;
421 + break;
422 + }
423 +}
424 +
425 +static void
426 +minstrel_aggr_check(struct minstrel_priv *mp, struct ieee80211_sta *pubsta, struct sk_buff *skb)
427 +{
428 + struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
429 + struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
430 + u16 tid;
431 +
432 + if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
433 + return;
434 +
435 + if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
436 + return;
437 +
438 + tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
439 + if (likely(sta->ampdu_mlme.tid_state_tx[tid] != HT_AGG_STATE_IDLE))
440 + return;
441 +
442 + ieee80211_start_tx_ba_session(pubsta, tid);
443 +}
444 +
445 +static void
446 +minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
447 + struct ieee80211_sta *sta, void *priv_sta,
448 + struct sk_buff *skb)
449 +{
450 + struct minstrel_ht_sta_priv *msp = priv_sta;
451 + struct minstrel_ht_sta *mi = &msp->ht;
452 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
453 + struct ieee80211_tx_rate *ar = info->status.rates;
454 + struct minstrel_rate_stats *rate, *rate2;
455 + struct minstrel_priv *mp = priv;
456 + bool last = false;
457 + int group;
458 + int i = 0;
459 +
460 + if (!msp->is_ht)
461 + return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
462 +
463 + /* This packet was aggregated but doesn't carry status info */
464 + if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
465 + !(info->flags & IEEE80211_TX_STAT_AMPDU))
466 + return;
467 +
468 + if (!info->status.ampdu_len) {
469 + info->status.ampdu_ack_len = 1;
470 + info->status.ampdu_len = 1;
471 + }
472 +
473 + mi->ampdu_packets++;
474 + mi->ampdu_len += info->status.ampdu_len;
475 +
476 + if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
477 + mi->sample_wait = 4 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
478 + mi->sample_tries = 3;
479 + mi->sample_count--;
480 + }
481 +
482 + if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) {
483 + mi->sample_packets += info->status.ampdu_len;
484 + minstrel_next_sample_idx(mi);
485 + }
486 +
487 + for (i = 0; !last; i++) {
488 + last = (i == IEEE80211_TX_MAX_RATES - 1) ||
489 + !minstrel_ht_txstat_valid(&ar[i + 1]);
490 +
491 + if (!minstrel_ht_txstat_valid(&ar[i]))
492 + break;
493 +
494 + group = minstrel_ht_get_group_idx(&ar[i]);
495 + rate = &mi->groups[group].rates[ar[i].idx % 8];
496 +
497 + if (last && (info->flags & IEEE80211_TX_STAT_ACK))
498 + rate->success += info->status.ampdu_ack_len;
499 +
500 + rate->attempts += ar[i].count * info->status.ampdu_len;
501 + }
502 +
503 + /*
504 + * check for sudden death of spatial multiplexing,
505 + * downgrade to a lower number of streams if necessary.
506 + */
507 + rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
508 + if (rate->attempts > 30 &&
509 + MINSTREL_FRAC(rate->success, rate->attempts) <
510 + MINSTREL_FRAC(20, 100))
511 + minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
512 +
513 + rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
514 + if (rate->attempts > 30 &&
515 + MINSTREL_FRAC(rate->success, rate->attempts) <
516 + MINSTREL_FRAC(20, 100))
517 + minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
518 +
519 + if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
520 + minstrel_ht_update_stats(mp, mi);
521 + minstrel_aggr_check(mp, sta, skb);
522 + }
523 +}
524 +
525 +static void
526 +minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
527 + int index)
528 +{
529 + struct minstrel_rate_stats *mr;
530 + const struct mcs_group *group;
531 + unsigned int tx_time, tx_time_rtscts, tx_time_data;
532 + unsigned int cw = mp->cw_min;
533 + unsigned int t_slot = 9; /* FIXME */
534 + unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
535 +
536 + mr = minstrel_get_ratestats(mi, index);
537 + if (mr->probability < MINSTREL_FRAC(1, 10)) {
538 + mr->retry_count = 1;
539 + mr->retry_count_rtscts = 1;
540 + return;
541 + }
542 +
543 + mr->retry_count = 2;
544 + mr->retry_count_rtscts = 2;
545 + mr->retry_updated = true;
546 +
547 + group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
548 + tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
549 + tx_time = 2 * (t_slot + mi->overhead + tx_time_data);
550 + tx_time_rtscts = 2 * (t_slot + mi->overhead_rtscts + tx_time_data);
551 + do {
552 + cw = (cw << 1) | 1;
553 + cw = min(cw, mp->cw_max);
554 + tx_time += cw + t_slot + mi->overhead;
555 + tx_time_rtscts += cw + t_slot + mi->overhead_rtscts;
556 + if (tx_time_rtscts < mp->segment_size)
557 + mr->retry_count_rtscts++;
558 + } while ((tx_time < mp->segment_size) &&
559 + (++mr->retry_count < mp->max_retry));
560 +}
561 +
562 +
563 +static void
564 +minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
565 + struct ieee80211_tx_rate *rate, int index,
566 + struct ieee80211_tx_rate_control *txrc,
567 + bool sample, bool rtscts)
568 +{
569 + const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
570 + struct minstrel_rate_stats *mr;
571 +
572 + mr = minstrel_get_ratestats(mi, index);
573 + if (!mr->retry_updated)
574 + minstrel_calc_retransmit(mp, mi, index);
575 +
576 + if (mr->probability < MINSTREL_FRAC(20, 100))
577 + rate->count = 2;
578 + else if (rtscts)
579 + rate->count = mr->retry_count_rtscts;
580 + else
581 + rate->count = mr->retry_count;
582 +
583 + rate->flags = IEEE80211_TX_RC_MCS | group->flags;
584 + if (txrc->short_preamble)
585 + rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
586 + if (txrc->rts || rtscts)
587 + rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
588 + rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
589 +}
590 +
591 +static inline int
592 +minstrel_get_duration(int index)
593 +{
594 + const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
595 + return group->duration[index % MCS_GROUP_RATES];
596 +}
597 +
598 +static int
599 +minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
600 +{
601 + struct minstrel_rate_stats *mr;
602 + struct minstrel_mcs_group_data *mg;
603 + int sample_idx = 0;
604 +
605 + if (mi->sample_wait > 0) {
606 + mi->sample_wait--;
607 + return -1;
608 + }
609 +
610 + if (!mi->sample_tries)
611 + return -1;
612 +
613 + mi->sample_tries--;
614 + mg = &mi->groups[mi->sample_group];
615 + sample_idx = sample_table[mg->column][mg->index];
616 + mr = &mg->rates[sample_idx];
617 + sample_idx += mi->sample_group * MCS_GROUP_RATES;
618 +
619 + /*
620 + * When not using MRR, do not sample if the probability is already
621 + * higher than 95% to avoid wasting airtime
622 + */
623 + if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
624 + goto next;
625 +
626 + /*
627 + * Make sure that lower rates get sampled only occasionally,
628 + * if the link is working perfectly.
629 + */
630 + if (minstrel_get_duration(sample_idx) >
631 + minstrel_get_duration(mi->max_tp_rate)) {
632 + if (mr->sample_skipped < 10)
633 + goto next;
634 +
635 + if (mi->sample_slow++ > 2)
636 + goto next;
637 + }
638 +
639 + return sample_idx;
640 +
641 +next:
642 + minstrel_next_sample_idx(mi);
643 + return -1;
644 +}
645 +
646 +static void
647 +minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
648 + struct ieee80211_tx_rate_control *txrc)
649 +{
650 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
651 + struct ieee80211_tx_rate *ar = info->status.rates;
652 + struct minstrel_ht_sta_priv *msp = priv_sta;
653 + struct minstrel_ht_sta *mi = &msp->ht;
654 + struct minstrel_priv *mp = priv;
655 + int sample_idx;
656 +
657 + if (rate_control_send_low(sta, priv_sta, txrc))
658 + return;
659 +
660 + if (!msp->is_ht)
661 + return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
662 +
663 + info->flags |= mi->tx_flags;
664 + sample_idx = minstrel_get_sample_rate(mp, mi);
665 + if (sample_idx >= 0) {
666 + minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
667 + txrc, true, false);
668 + minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
669 + txrc, false, true);
670 + info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
671 + } else {
672 + minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
673 + txrc, false, false);
674 + minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
675 + txrc, false, true);
676 + }
677 + minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate, txrc, false, true);
678 +
679 + ar[3].count = 0;
680 + ar[3].idx = -1;
681 +
682 + mi->total_packets++;
683 +
684 + /* wraparound */
685 + if (mi->total_packets == ~0) {
686 + mi->total_packets = 0;
687 + mi->sample_packets = 0;
688 + }
689 +}
690 +
691 +static void
692 +minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
693 + struct ieee80211_sta *sta, void *priv_sta,
694 + enum nl80211_channel_type oper_chan_type)
695 +{
696 + struct minstrel_priv *mp = priv;
697 + struct minstrel_ht_sta_priv *msp = priv_sta;
698 + struct minstrel_ht_sta *mi = &msp->ht;
699 + struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
700 + struct ieee80211_local *local = hw_to_local(mp->hw);
701 + u16 sta_cap = sta->ht_cap.cap;
702 + int ack_dur;
703 + int i;
704 +
705 + /* fall back to the old minstrel for legacy stations */
706 + if (sta && !sta->ht_cap.ht_supported) {
707 + msp->is_ht = false;
708 + memset(&msp->legacy, 0, sizeof(msp->legacy));
709 + msp->legacy.r = msp->ratelist;
710 + msp->legacy.sample_table = msp->sample_table;
711 + return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
712 + }
713 +
714 + BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
715 + MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
716 +
717 + msp->is_ht = true;
718 + memset(mi, 0, sizeof(*mi));
719 + mi->stats_update = jiffies;
720 +
721 + ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);
722 + mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;
723 + mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
724 +
725 + mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
726 +
727 + /* When using MRR, sample more on the first attempt, without delay */
728 + if (mp->has_mrr) {
729 + mi->sample_count = 16;
730 + mi->sample_wait = 0;
731 + } else {
732 + mi->sample_count = 8;
733 + mi->sample_wait = 8;
734 + }
735 + mi->sample_tries = 4;
736 +
737 + if (sta_cap & IEEE80211_HT_CAP_TX_STBC)
738 + mi->tx_flags |= IEEE80211_TX_CTL_STBC;
739 +
740 + if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
741 + mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
742 +
743 + if (oper_chan_type != NL80211_CHAN_HT40MINUS &&
744 + oper_chan_type != NL80211_CHAN_HT40PLUS)
745 + sta_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
746 +
747 + for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
748 + u16 req = 0;
749 +
750 + mi->groups[i].supported = 0;
751 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
752 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
753 + req |= IEEE80211_HT_CAP_SGI_40;
754 + else
755 + req |= IEEE80211_HT_CAP_SGI_20;
756 + }
757 +
758 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
759 + req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
760 +
761 + if ((sta_cap & req) != req)
762 + continue;
763 +
764 + mi->groups[i].supported =
765 + mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
766 + }
767 +}
768 +
769 +static void
770 +minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
771 + struct ieee80211_sta *sta, void *priv_sta)
772 +{
773 + struct minstrel_priv *mp = priv;
774 +
775 + minstrel_ht_update_caps(priv, sband, sta, priv_sta, mp->hw->conf.channel_type);
776 +}
777 +
778 +static void
779 +minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
780 + struct ieee80211_sta *sta, void *priv_sta,
781 + u32 changed, enum nl80211_channel_type oper_chan_type)
782 +{
783 + minstrel_ht_update_caps(priv, sband, sta, priv_sta, oper_chan_type);
784 +}
785 +
786 +static void *
787 +minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
788 +{
789 + struct ieee80211_supported_band *sband;
790 + struct minstrel_ht_sta_priv *msp;
791 + struct minstrel_priv *mp = priv;
792 + struct ieee80211_hw *hw = mp->hw;
793 + int max_rates = 0;
794 + int i;
795 +
796 + for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
797 + sband = hw->wiphy->bands[i];
798 + if (sband && sband->n_bitrates > max_rates)
799 + max_rates = sband->n_bitrates;
800 + }
801 +
802 + msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);
803 + if (!msp)
804 + return NULL;
805 +
806 + msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
807 + if (!msp->ratelist)
808 + goto error;
809 +
810 + msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
811 + if (!msp->sample_table)
812 + goto error1;
813 +
814 + return msp;
815 +
816 +error1:
817 + kfree(msp->sample_table);
818 +error:
819 + kfree(msp);
820 + return NULL;
821 +}
822 +
823 +static void
824 +minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
825 +{
826 + struct minstrel_ht_sta_priv *msp = priv_sta;
827 +
828 + kfree(msp->sample_table);
829 + kfree(msp->ratelist);
830 + kfree(msp);
831 +}
832 +
833 +static void *
834 +minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
835 +{
836 + return mac80211_minstrel.alloc(hw, debugfsdir);
837 +}
838 +
839 +static void
840 +minstrel_ht_free(void *priv)
841 +{
842 + mac80211_minstrel.free(priv);
843 +}
844 +
845 +static struct rate_control_ops mac80211_minstrel_ht = {
846 + .name = "minstrel_ht",
847 + .tx_status = minstrel_ht_tx_status,
848 + .get_rate = minstrel_ht_get_rate,
849 + .rate_init = minstrel_ht_rate_init,
850 + .rate_update = minstrel_ht_rate_update,
851 + .alloc_sta = minstrel_ht_alloc_sta,
852 + .free_sta = minstrel_ht_free_sta,
853 + .alloc = minstrel_ht_alloc,
854 + .free = minstrel_ht_free,
855 +#ifdef CONFIG_MAC80211_DEBUGFS
856 + .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
857 + .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
858 +#endif
859 +};
860 +
861 +
862 +static void
863 +init_sample_table(void)
864 +{
865 + int col, i, new_idx;
866 + u8 rnd[MCS_GROUP_RATES];
867 +
868 + memset(sample_table, 0xff, sizeof(sample_table));
869 + for (col = 0; col < SAMPLE_COLUMNS; col++) {
870 + for (i = 0; i < MCS_GROUP_RATES; i++) {
871 + get_random_bytes(rnd, sizeof(rnd));
872 + new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
873 +
874 + while (sample_table[col][new_idx] != 0xff)
875 + new_idx = (new_idx + 1) % MCS_GROUP_RATES;
876 +
877 + sample_table[col][new_idx] = i;
878 + }
879 + }
880 +}
881 +
882 +int __init
883 +rc80211_minstrel_ht_init(void)
884 +{
885 + init_sample_table();
886 + return ieee80211_rate_control_register(&mac80211_minstrel_ht);
887 +}
888 +
889 +void
890 +rc80211_minstrel_ht_exit(void)
891 +{
892 + ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
893 +}
894 --- /dev/null
895 +++ b/net/mac80211/rc80211_minstrel_ht.h
896 @@ -0,0 +1,128 @@
897 +/*
898 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
899 + *
900 + * This program is free software; you can redistribute it and/or modify
901 + * it under the terms of the GNU General Public License version 2 as
902 + * published by the Free Software Foundation.
903 + */
904 +
905 +#ifndef __RC_MINSTREL_HT_H
906 +#define __RC_MINSTREL_HT_H
907 +
908 +/*
909 + * The number of streams can be changed to 2 to reduce code
910 + * size and memory footprint.
911 + */
912 +#define MINSTREL_MAX_STREAMS 3
913 +#define MINSTREL_STREAM_GROUPS 4
914 +
915 +/* scaled fraction values */
916 +#define MINSTREL_SCALE 16
917 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
918 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
919 +
920 +#define MCS_GROUP_RATES 8
921 +
922 +struct mcs_group {
923 + u32 flags;
924 + unsigned int streams;
925 + unsigned int duration[MCS_GROUP_RATES];
926 +};
927 +
928 +struct minstrel_rate_stats {
929 + /* current / last sampling period attempts/success counters */
930 + unsigned int attempts, last_attempts;
931 + unsigned int success, last_success;
932 +
933 + /* total attempts/success counters */
934 + u64 att_hist, succ_hist;
935 +
936 + /* current throughput */
937 + unsigned int cur_tp;
938 +
939 + /* packet delivery probabilities */
940 + unsigned int cur_prob, probability;
941 +
942 + /* maximum retry counts */
943 + unsigned int retry_count;
944 + unsigned int retry_count_rtscts;
945 +
946 + bool retry_updated;
947 + u8 sample_skipped;
948 +};
949 +
950 +struct minstrel_mcs_group_data {
951 + u8 index;
952 + u8 column;
953 +
954 + /* bitfield of supported MCS rates of this group */
955 + u8 supported;
956 +
957 + /* selected primary rates */
958 + unsigned int max_tp_rate;
959 + unsigned int max_tp_rate2;
960 + unsigned int max_prob_rate;
961 +
962 + /* MCS rate statistics */
963 + struct minstrel_rate_stats rates[MCS_GROUP_RATES];
964 +};
965 +
966 +struct minstrel_ht_sta {
967 + /* ampdu length (average, per sampling interval) */
968 + unsigned int ampdu_len;
969 + unsigned int ampdu_packets;
970 +
971 + /* ampdu length (EWMA) */
972 + unsigned int avg_ampdu_len;
973 +
974 + /* best throughput rate */
975 + unsigned int max_tp_rate;
976 +
977 + /* second best throughput rate */
978 + unsigned int max_tp_rate2;
979 +
980 + /* best probability rate */
981 + unsigned int max_prob_rate;
982 +
983 + /* time of last status update */
984 + unsigned long stats_update;
985 +
986 + /* overhead time in usec for each frame */
987 + unsigned int overhead;
988 + unsigned int overhead_rtscts;
989 +
990 + unsigned int total_packets;
991 + unsigned int sample_packets;
992 +
993 + /* tx flags to add for frames for this sta */
994 + u32 tx_flags;
995 +
996 + u8 sample_wait;
997 + u8 sample_tries;
998 + u8 sample_count;
999 + u8 sample_slow;
1000 +
1001 + /* current MCS group to be sampled */
1002 + u8 sample_group;
1003 +
1004 + /* MCS rate group info and statistics */
1005 + struct minstrel_mcs_group_data groups[MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS];
1006 +};
1007 +
1008 +struct minstrel_ht_sta_priv {
1009 + union {
1010 + struct minstrel_ht_sta ht;
1011 + struct minstrel_sta_info legacy;
1012 + };
1013 +#ifdef CONFIG_MAC80211_DEBUGFS
1014 + struct dentry *dbg_stats;
1015 +#endif
1016 + void *ratelist;
1017 + void *sample_table;
1018 + bool is_ht;
1019 +};
1020 +
1021 +void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
1022 +void minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta);
1023 +
1024 +#endif
1025 --- /dev/null
1026 +++ b/net/mac80211/rc80211_minstrel_ht_debugfs.c
1027 @@ -0,0 +1,120 @@
1028 +/*
1029 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
1030 + *
1031 + * This program is free software; you can redistribute it and/or modify
1032 + * it under the terms of the GNU General Public License version 2 as
1033 + * published by the Free Software Foundation.
1034 + */
1035 +#include <linux/netdevice.h>
1036 +#include <linux/types.h>
1037 +#include <linux/skbuff.h>
1038 +#include <linux/debugfs.h>
1039 +#include <linux/ieee80211.h>
1040 +#include <net/mac80211.h>
1041 +#include "rc80211_minstrel.h"
1042 +#include "rc80211_minstrel_ht.h"
1043 +
1044 +extern const struct mcs_group minstrel_mcs_groups[];
1045 +
1046 +static int
1047 +minstrel_ht_stats_open(struct inode *inode, struct file *file)
1048 +{
1049 + struct minstrel_ht_sta_priv *msp = inode->i_private;
1050 + struct minstrel_ht_sta *mi = &msp->ht;
1051 + struct minstrel_debugfs_info *ms;
1052 + unsigned int i, j, tp, prob, eprob;
1053 + char *p;
1054 + int ret;
1055 +
1056 + if (!msp->is_ht) {
1057 + inode->i_private = &msp->legacy;
1058 + ret = minstrel_stats_open(inode, file);
1059 + inode->i_private = msp;
1060 + return ret;
1061 + }
1062 +
1063 + ms = kmalloc(sizeof(*ms) + 8192, GFP_KERNEL);
1064 + if (!ms)
1065 + return -ENOMEM;
1066 +
1067 + file->private_data = ms;
1068 + p = ms->buf;
1069 + p += sprintf(p, "type rate throughput ewma prob this prob "
1070 + "this succ/attempt success attempts\n");
1071 + for (i = 0; i < MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS; i++) {
1072 + char htmode = '2';
1073 + char gimode = 'L';
1074 +
1075 + if (!mi->groups[i].supported)
1076 + continue;
1077 +
1078 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1079 + htmode = '4';
1080 + if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI)
1081 + gimode = 'S';
1082 +
1083 + for (j = 0; j < MCS_GROUP_RATES; j++) {
1084 + struct minstrel_rate_stats *mr = &mi->groups[i].rates[j];
1085 + int idx = i * MCS_GROUP_RATES + j;
1086 +
1087 + if (!(mi->groups[i].supported & BIT(j)))
1088 + continue;
1089 +
1090 + p += sprintf(p, "HT%c0/%cGI ", htmode, gimode);
1091 +
1092 + *(p++) = (idx == mi->max_tp_rate) ? 'T' : ' ';
1093 + *(p++) = (idx == mi->max_tp_rate2) ? 't' : ' ';
1094 + *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' ';
1095 + p += sprintf(p, "MCS%-2u", (minstrel_mcs_groups[i].streams - 1) *
1096 + MCS_GROUP_RATES + j);
1097 +
1098 + tp = mr->cur_tp / 10;
1099 + prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
1100 + eprob = MINSTREL_TRUNC(mr->probability * 1000);
1101 +
1102 + p += sprintf(p, " %6u.%1u %6u.%1u %6u.%1u "
1103 + "%3u(%3u) %8llu %8llu\n",
1104 + tp / 10, tp % 10,
1105 + eprob / 10, eprob % 10,
1106 + prob / 10, prob % 10,
1107 + mr->last_success,
1108 + mr->last_attempts,
1109 + (unsigned long long)mr->succ_hist,
1110 + (unsigned long long)mr->att_hist);
1111 + }
1112 + }
1113 + p += sprintf(p, "\nTotal packet count:: ideal %d "
1114 + "lookaround %d\n",
1115 + max(0, (int) mi->total_packets - (int) mi->sample_packets),
1116 + mi->sample_packets);
1117 + p += sprintf(p, "Average A-MPDU length: %d.%d\n",
1118 + MINSTREL_TRUNC(mi->avg_ampdu_len),
1119 + MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
1120 + ms->len = p - ms->buf;
1121 +
1122 + return 0;
1123 +}
1124 +
1125 +static const struct file_operations minstrel_ht_stat_fops = {
1126 + .owner = THIS_MODULE,
1127 + .open = minstrel_ht_stats_open,
1128 + .read = minstrel_stats_read,
1129 + .release = minstrel_stats_release,
1130 +};
1131 +
1132 +void
1133 +minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
1134 +{
1135 + struct minstrel_ht_sta_priv *msp = priv_sta;
1136 +
1137 + msp->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, msp,
1138 + &minstrel_ht_stat_fops);
1139 +}
1140 +
1141 +void
1142 +minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta)
1143 +{
1144 + struct minstrel_ht_sta_priv *msp = priv_sta;
1145 +
1146 + debugfs_remove(msp->dbg_stats);
1147 +}
This page took 0.109161 seconds and 5 git commands to generate.