GNU Linux-libre 4.9.337-gnu1
[releases.git] / net / mac80211 / rc80211_minstrel.c
1 /*
2  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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  * Based on minstrel.c:
9  *   Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
10  *   Sponsored by Indranet Technologies Ltd
11  *
12  * Based on sample.c:
13  *   Copyright (c) 2005 John Bicket
14  *   All rights reserved.
15  *
16  *   Redistribution and use in source and binary forms, with or without
17  *   modification, are permitted provided that the following conditions
18  *   are met:
19  *   1. Redistributions of source code must retain the above copyright
20  *      notice, this list of conditions and the following disclaimer,
21  *      without modification.
22  *   2. Redistributions in binary form must reproduce at minimum a disclaimer
23  *      similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
24  *      redistribution must be conditioned upon including a substantially
25  *      similar Disclaimer requirement for further binary redistribution.
26  *   3. Neither the names of the above-listed copyright holders nor the names
27  *      of any contributors may be used to endorse or promote products derived
28  *      from this software without specific prior written permission.
29  *
30  *   Alternatively, this software may be distributed under the terms of the
31  *   GNU General Public License ("GPL") version 2 as published by the Free
32  *   Software Foundation.
33  *
34  *   NO WARRANTY
35  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  *   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  *   LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
38  *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39  *   THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
40  *   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41  *   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
43  *   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44  *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
45  *   THE POSSIBILITY OF SUCH DAMAGES.
46  */
47 #include <linux/netdevice.h>
48 #include <linux/types.h>
49 #include <linux/skbuff.h>
50 #include <linux/debugfs.h>
51 #include <linux/random.h>
52 #include <linux/ieee80211.h>
53 #include <linux/slab.h>
54 #include <net/mac80211.h>
55 #include "rate.h"
56 #include "rc80211_minstrel.h"
57
58 #define SAMPLE_TBL(_mi, _idx, _col) \
59                 _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
60
61 /* convert mac80211 rate index to local array index */
62 static inline int
63 rix_to_ndx(struct minstrel_sta_info *mi, int rix)
64 {
65         int i = rix;
66         for (i = rix; i >= 0; i--)
67                 if (mi->r[i].rix == rix)
68                         break;
69         return i;
70 }
71
72 /* return current EMWA throughput */
73 int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma)
74 {
75         int usecs;
76
77         usecs = mr->perfect_tx_time;
78         if (!usecs)
79                 usecs = 1000000;
80
81         /* reset thr. below 10% success */
82         if (mr->stats.prob_ewma < MINSTREL_FRAC(10, 100))
83                 return 0;
84
85         if (prob_ewma > MINSTREL_FRAC(90, 100))
86                 return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs));
87         else
88                 return MINSTREL_TRUNC(100000 * (prob_ewma / usecs));
89 }
90
91 /* find & sort topmost throughput rates */
92 static inline void
93 minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
94 {
95         int j;
96         struct minstrel_rate_stats *tmp_mrs;
97         struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats;
98
99         for (j = MAX_THR_RATES; j > 0; --j) {
100                 tmp_mrs = &mi->r[tp_list[j - 1]].stats;
101                 if (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_ewma) <=
102                     minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_ewma))
103                         break;
104         }
105
106         if (j < MAX_THR_RATES - 1)
107                 memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
108         if (j < MAX_THR_RATES)
109                 tp_list[j] = i;
110 }
111
112 static void
113 minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
114                   int offset, int idx)
115 {
116         struct minstrel_rate *r = &mi->r[idx];
117
118         ratetbl->rate[offset].idx = r->rix;
119         ratetbl->rate[offset].count = r->adjusted_retry_count;
120         ratetbl->rate[offset].count_cts = r->retry_count_cts;
121         ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts;
122 }
123
124 static void
125 minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
126 {
127         struct ieee80211_sta_rates *ratetbl;
128         int i = 0;
129
130         ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
131         if (!ratetbl)
132                 return;
133
134         /* Start with max_tp_rate */
135         minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
136
137         if (mp->hw->max_rates >= 3) {
138                 /* At least 3 tx rates supported, use max_tp_rate2 next */
139                 minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
140         }
141
142         if (mp->hw->max_rates >= 2) {
143                 /* At least 2 tx rates supported, use max_prob_rate next */
144                 minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
145         }
146
147         /* Use lowest rate last */
148         ratetbl->rate[i].idx = mi->lowest_rix;
149         ratetbl->rate[i].count = mp->max_retry;
150         ratetbl->rate[i].count_cts = mp->max_retry;
151         ratetbl->rate[i].count_rts = mp->max_retry;
152
153         rate_control_set_rates(mp->hw, mi->sta, ratetbl);
154 }
155
156 /*
157 * Recalculate statistics and counters of a given rate
158 */
159 void
160 minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs)
161 {
162         if (unlikely(mrs->attempts > 0)) {
163                 mrs->sample_skipped = 0;
164                 mrs->cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
165                 if (unlikely(!mrs->att_hist)) {
166                         mrs->prob_ewma = mrs->cur_prob;
167                 } else {
168                         /* update exponential weighted moving variance */
169                         mrs->prob_ewmsd = minstrel_ewmsd(mrs->prob_ewmsd,
170                                                          mrs->cur_prob,
171                                                          mrs->prob_ewma,
172                                                          EWMA_LEVEL);
173
174                         /*update exponential weighted moving avarage */
175                         mrs->prob_ewma = minstrel_ewma(mrs->prob_ewma,
176                                                        mrs->cur_prob,
177                                                        EWMA_LEVEL);
178                 }
179                 mrs->att_hist += mrs->attempts;
180                 mrs->succ_hist += mrs->success;
181         } else {
182                 mrs->sample_skipped++;
183         }
184
185         mrs->last_success = mrs->success;
186         mrs->last_attempts = mrs->attempts;
187         mrs->success = 0;
188         mrs->attempts = 0;
189 }
190
191 static void
192 minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
193 {
194         u8 tmp_tp_rate[MAX_THR_RATES];
195         u8 tmp_prob_rate = 0;
196         int i, tmp_cur_tp, tmp_prob_tp;
197
198         for (i = 0; i < MAX_THR_RATES; i++)
199             tmp_tp_rate[i] = 0;
200
201         for (i = 0; i < mi->n_rates; i++) {
202                 struct minstrel_rate *mr = &mi->r[i];
203                 struct minstrel_rate_stats *mrs = &mi->r[i].stats;
204                 struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats;
205
206                 /* Update statistics of success probability per rate */
207                 minstrel_calc_rate_stats(mrs);
208
209                 /* Sample less often below the 10% chance of success.
210                  * Sample less often above the 95% chance of success. */
211                 if (mrs->prob_ewma > MINSTREL_FRAC(95, 100) ||
212                     mrs->prob_ewma < MINSTREL_FRAC(10, 100)) {
213                         mr->adjusted_retry_count = mrs->retry_count >> 1;
214                         if (mr->adjusted_retry_count > 2)
215                                 mr->adjusted_retry_count = 2;
216                         mr->sample_limit = 4;
217                 } else {
218                         mr->sample_limit = -1;
219                         mr->adjusted_retry_count = mrs->retry_count;
220                 }
221                 if (!mr->adjusted_retry_count)
222                         mr->adjusted_retry_count = 2;
223
224                 minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
225
226                 /* To determine the most robust rate (max_prob_rate) used at
227                  * 3rd mmr stage we distinct between two cases:
228                  * (1) if any success probabilitiy >= 95%, out of those rates
229                  * choose the maximum throughput rate as max_prob_rate
230                  * (2) if all success probabilities < 95%, the rate with
231                  * highest success probability is chosen as max_prob_rate */
232                 if (mrs->prob_ewma >= MINSTREL_FRAC(95, 100)) {
233                         tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_ewma);
234                         tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate],
235                                                           tmp_mrs->prob_ewma);
236                         if (tmp_cur_tp >= tmp_prob_tp)
237                                 tmp_prob_rate = i;
238                 } else {
239                         if (mrs->prob_ewma >= tmp_mrs->prob_ewma)
240                                 tmp_prob_rate = i;
241                 }
242         }
243
244         /* Assign the new rate set */
245         memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
246         mi->max_prob_rate = tmp_prob_rate;
247
248 #ifdef CONFIG_MAC80211_DEBUGFS
249         /* use fixed index if set */
250         if (mp->fixed_rate_idx != -1) {
251                 mi->max_tp_rate[0] = mp->fixed_rate_idx;
252                 mi->max_tp_rate[1] = mp->fixed_rate_idx;
253                 mi->max_prob_rate = mp->fixed_rate_idx;
254         }
255 #endif
256
257         /* Reset update timer */
258         mi->last_stats_update = jiffies;
259
260         minstrel_update_rates(mp, mi);
261 }
262
263 static void
264 minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
265                    struct ieee80211_sta *sta, void *priv_sta,
266                    struct ieee80211_tx_info *info)
267 {
268         struct minstrel_priv *mp = priv;
269         struct minstrel_sta_info *mi = priv_sta;
270         struct ieee80211_tx_rate *ar = info->status.rates;
271         int i, ndx;
272         int success;
273
274         success = !!(info->flags & IEEE80211_TX_STAT_ACK);
275
276         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
277                 if (ar[i].idx < 0 || !ar[i].count)
278                         break;
279
280                 ndx = rix_to_ndx(mi, ar[i].idx);
281                 if (ndx < 0)
282                         continue;
283
284                 mi->r[ndx].stats.attempts += ar[i].count;
285
286                 if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
287                         mi->r[ndx].stats.success += success;
288         }
289
290         if (time_after(jiffies, mi->last_stats_update +
291                                 (mp->update_interval * HZ) / 1000))
292                 minstrel_update_stats(mp, mi);
293 }
294
295
296 static inline unsigned int
297 minstrel_get_retry_count(struct minstrel_rate *mr,
298                          struct ieee80211_tx_info *info)
299 {
300         u8 retry = mr->adjusted_retry_count;
301
302         if (info->control.use_rts)
303                 retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry));
304         else if (info->control.use_cts_prot)
305                 retry = max_t(u8, 2, min(mr->retry_count_cts, retry));
306         return retry;
307 }
308
309
310 static int
311 minstrel_get_next_sample(struct minstrel_sta_info *mi)
312 {
313         unsigned int sample_ndx;
314         sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
315         mi->sample_row++;
316         if ((int) mi->sample_row >= mi->n_rates) {
317                 mi->sample_row = 0;
318                 mi->sample_column++;
319                 if (mi->sample_column >= SAMPLE_COLUMNS)
320                         mi->sample_column = 0;
321         }
322         return sample_ndx;
323 }
324
325 static void
326 minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
327                   void *priv_sta, struct ieee80211_tx_rate_control *txrc)
328 {
329         struct sk_buff *skb = txrc->skb;
330         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
331         struct minstrel_sta_info *mi = priv_sta;
332         struct minstrel_priv *mp = priv;
333         struct ieee80211_tx_rate *rate = &info->control.rates[0];
334         struct minstrel_rate *msr, *mr;
335         unsigned int ndx;
336         bool mrr_capable;
337         bool prev_sample;
338         int delta;
339         int sampling_ratio;
340
341         /* management/no-ack frames do not use rate control */
342         if (rate_control_send_low(sta, priv_sta, txrc))
343                 return;
344
345         /* check multi-rate-retry capabilities & adjust lookaround_rate */
346         mrr_capable = mp->has_mrr &&
347                       !txrc->rts &&
348                       !txrc->bss_conf->use_cts_prot;
349         if (mrr_capable)
350                 sampling_ratio = mp->lookaround_rate_mrr;
351         else
352                 sampling_ratio = mp->lookaround_rate;
353
354         /* increase sum packet counter */
355         mi->total_packets++;
356
357 #ifdef CONFIG_MAC80211_DEBUGFS
358         if (mp->fixed_rate_idx != -1)
359                 return;
360 #endif
361
362         delta = (mi->total_packets * sampling_ratio / 100) -
363                         mi->sample_packets;
364
365         /* delta < 0: no sampling required */
366         prev_sample = mi->prev_sample;
367         mi->prev_sample = false;
368         if (delta < 0 || (!mrr_capable && prev_sample))
369                 return;
370
371         if (mi->total_packets >= 10000) {
372                 mi->sample_packets = 0;
373                 mi->total_packets = 0;
374         } else if (delta > mi->n_rates * 2) {
375                 /* With multi-rate retry, not every planned sample
376                  * attempt actually gets used, due to the way the retry
377                  * chain is set up - [max_tp,sample,prob,lowest] for
378                  * sample_rate < max_tp.
379                  *
380                  * If there's too much sampling backlog and the link
381                  * starts getting worse, minstrel would start bursting
382                  * out lots of sampling frames, which would result
383                  * in a large throughput loss. */
384                 mi->sample_packets += (delta - mi->n_rates * 2);
385         }
386
387         /* get next random rate sample */
388         ndx = minstrel_get_next_sample(mi);
389         msr = &mi->r[ndx];
390         mr = &mi->r[mi->max_tp_rate[0]];
391
392         /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
393          * rate sampling method should be used.
394          * Respect such rates that are not sampled for 20 interations.
395          */
396         if (msr->perfect_tx_time < mr->perfect_tx_time ||
397             msr->stats.sample_skipped >= 20) {
398                 if (!msr->sample_limit)
399                         return;
400
401                 mi->sample_packets++;
402                 if (msr->sample_limit > 0)
403                         msr->sample_limit--;
404         }
405
406         /* If we're not using MRR and the sampling rate already
407          * has a probability of >95%, we shouldn't be attempting
408          * to use it, as this only wastes precious airtime */
409         if (!mrr_capable &&
410            (mi->r[ndx].stats.prob_ewma > MINSTREL_FRAC(95, 100)))
411                 return;
412
413         mi->prev_sample = true;
414
415         rate->idx = mi->r[ndx].rix;
416         rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
417         info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
418 }
419
420
421 static void
422 calc_rate_durations(enum nl80211_band band,
423                     struct minstrel_rate *d,
424                     struct ieee80211_rate *rate,
425                     struct cfg80211_chan_def *chandef)
426 {
427         int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
428         int shift = ieee80211_chandef_get_shift(chandef);
429
430         d->perfect_tx_time = ieee80211_frame_duration(band, 1200,
431                         DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
432                         shift);
433         d->ack_time = ieee80211_frame_duration(band, 10,
434                         DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
435                         shift);
436 }
437
438 static void
439 init_sample_table(struct minstrel_sta_info *mi)
440 {
441         unsigned int i, col, new_idx;
442         u8 rnd[8];
443
444         mi->sample_column = 0;
445         mi->sample_row = 0;
446         memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
447
448         for (col = 0; col < SAMPLE_COLUMNS; col++) {
449                 prandom_bytes(rnd, sizeof(rnd));
450                 for (i = 0; i < mi->n_rates; i++) {
451                         new_idx = (i + rnd[i & 7]) % mi->n_rates;
452                         while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
453                                 new_idx = (new_idx + 1) % mi->n_rates;
454
455                         SAMPLE_TBL(mi, new_idx, col) = i;
456                 }
457         }
458 }
459
460 static void
461 minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
462                    struct cfg80211_chan_def *chandef,
463                    struct ieee80211_sta *sta, void *priv_sta)
464 {
465         struct minstrel_sta_info *mi = priv_sta;
466         struct minstrel_priv *mp = priv;
467         struct ieee80211_rate *ctl_rate;
468         unsigned int i, n = 0;
469         unsigned int t_slot = 9; /* FIXME: get real slot time */
470         u32 rate_flags;
471
472         mi->sta = sta;
473         mi->lowest_rix = rate_lowest_index(sband, sta);
474         ctl_rate = &sband->bitrates[mi->lowest_rix];
475         mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
476                                 ctl_rate->bitrate,
477                                 !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1,
478                                 ieee80211_chandef_get_shift(chandef));
479
480         rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
481         memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
482         mi->max_prob_rate = 0;
483
484         for (i = 0; i < sband->n_bitrates; i++) {
485                 struct minstrel_rate *mr = &mi->r[n];
486                 struct minstrel_rate_stats *mrs = &mi->r[n].stats;
487                 unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
488                 unsigned int tx_time_single;
489                 unsigned int cw = mp->cw_min;
490                 int shift;
491
492                 if (!rate_supported(sta, sband->band, i))
493                         continue;
494                 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
495                         continue;
496
497                 n++;
498                 memset(mr, 0, sizeof(*mr));
499                 memset(mrs, 0, sizeof(*mrs));
500
501                 mr->rix = i;
502                 shift = ieee80211_chandef_get_shift(chandef);
503                 mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
504                                            (1 << shift) * 5);
505                 calc_rate_durations(sband->band, mr, &sband->bitrates[i],
506                                     chandef);
507
508                 /* calculate maximum number of retransmissions before
509                  * fallback (based on maximum segment size) */
510                 mr->sample_limit = -1;
511                 mrs->retry_count = 1;
512                 mr->retry_count_cts = 1;
513                 mrs->retry_count_rtscts = 1;
514                 tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
515                 do {
516                         /* add one retransmission */
517                         tx_time_single = mr->ack_time + mr->perfect_tx_time;
518
519                         /* contention window */
520                         tx_time_single += (t_slot * cw) >> 1;
521                         cw = min((cw << 1) | 1, mp->cw_max);
522
523                         tx_time += tx_time_single;
524                         tx_time_cts += tx_time_single + mi->sp_ack_dur;
525                         tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
526                         if ((tx_time_cts < mp->segment_size) &&
527                                 (mr->retry_count_cts < mp->max_retry))
528                                 mr->retry_count_cts++;
529                         if ((tx_time_rtscts < mp->segment_size) &&
530                                 (mrs->retry_count_rtscts < mp->max_retry))
531                                 mrs->retry_count_rtscts++;
532                 } while ((tx_time < mp->segment_size) &&
533                                 (++mr->stats.retry_count < mp->max_retry));
534                 mr->adjusted_retry_count = mrs->retry_count;
535                 if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
536                         mr->retry_count_cts = mrs->retry_count;
537         }
538
539         for (i = n; i < sband->n_bitrates; i++) {
540                 struct minstrel_rate *mr = &mi->r[i];
541                 mr->rix = -1;
542         }
543
544         mi->n_rates = n;
545         mi->last_stats_update = jiffies;
546
547         init_sample_table(mi);
548         minstrel_update_rates(mp, mi);
549 }
550
551 static void *
552 minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
553 {
554         struct ieee80211_supported_band *sband;
555         struct minstrel_sta_info *mi;
556         struct minstrel_priv *mp = priv;
557         struct ieee80211_hw *hw = mp->hw;
558         int max_rates = 0;
559         int i;
560
561         mi = kzalloc(sizeof(struct minstrel_sta_info), gfp);
562         if (!mi)
563                 return NULL;
564
565         for (i = 0; i < NUM_NL80211_BANDS; i++) {
566                 sband = hw->wiphy->bands[i];
567                 if (sband && sband->n_bitrates > max_rates)
568                         max_rates = sband->n_bitrates;
569         }
570
571         mi->r = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
572         if (!mi->r)
573                 goto error;
574
575         mi->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
576         if (!mi->sample_table)
577                 goto error1;
578
579         mi->last_stats_update = jiffies;
580         return mi;
581
582 error1:
583         kfree(mi->r);
584 error:
585         kfree(mi);
586         return NULL;
587 }
588
589 static void
590 minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
591 {
592         struct minstrel_sta_info *mi = priv_sta;
593
594         kfree(mi->sample_table);
595         kfree(mi->r);
596         kfree(mi);
597 }
598
599 static void
600 minstrel_init_cck_rates(struct minstrel_priv *mp)
601 {
602         static const int bitrates[4] = { 10, 20, 55, 110 };
603         struct ieee80211_supported_band *sband;
604         u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
605         int i, j;
606
607         sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
608         if (!sband)
609                 return;
610
611         for (i = 0, j = 0; i < sband->n_bitrates; i++) {
612                 struct ieee80211_rate *rate = &sband->bitrates[i];
613
614                 if (rate->flags & IEEE80211_RATE_ERP_G)
615                         continue;
616
617                 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
618                         continue;
619
620                 for (j = 0; j < ARRAY_SIZE(bitrates); j++) {
621                         if (rate->bitrate != bitrates[j])
622                                 continue;
623
624                         mp->cck_rates[j] = i;
625                         break;
626                 }
627         }
628 }
629
630 static void *
631 minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
632 {
633         struct minstrel_priv *mp;
634
635         mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
636         if (!mp)
637                 return NULL;
638
639         /* contention window settings
640          * Just an approximation. Using the per-queue values would complicate
641          * the calculations and is probably unnecessary */
642         mp->cw_min = 15;
643         mp->cw_max = 1023;
644
645         /* number of packets (in %) to use for sampling other rates
646          * sample less often for non-mrr packets, because the overhead
647          * is much higher than with mrr */
648         mp->lookaround_rate = 5;
649         mp->lookaround_rate_mrr = 10;
650
651         /* maximum time that the hw is allowed to stay in one MRR segment */
652         mp->segment_size = 6000;
653
654         if (hw->max_rate_tries > 0)
655                 mp->max_retry = hw->max_rate_tries;
656         else
657                 /* safe default, does not necessarily have to match hw properties */
658                 mp->max_retry = 7;
659
660         if (hw->max_rates >= 4)
661                 mp->has_mrr = true;
662
663         mp->hw = hw;
664         mp->update_interval = 100;
665
666 #ifdef CONFIG_MAC80211_DEBUGFS
667         mp->fixed_rate_idx = (u32) -1;
668         mp->dbg_fixed_rate = debugfs_create_u32("fixed_rate_idx",
669                         S_IRUGO | S_IWUGO, debugfsdir, &mp->fixed_rate_idx);
670 #endif
671
672         minstrel_init_cck_rates(mp);
673
674         return mp;
675 }
676
677 static void
678 minstrel_free(void *priv)
679 {
680 #ifdef CONFIG_MAC80211_DEBUGFS
681         debugfs_remove(((struct minstrel_priv *)priv)->dbg_fixed_rate);
682 #endif
683         kfree(priv);
684 }
685
686 static u32 minstrel_get_expected_throughput(void *priv_sta)
687 {
688         struct minstrel_sta_info *mi = priv_sta;
689         struct minstrel_rate_stats *tmp_mrs;
690         int idx = mi->max_tp_rate[0];
691         int tmp_cur_tp;
692
693         /* convert pkt per sec in kbps (1200 is the average pkt size used for
694          * computing cur_tp
695          */
696         tmp_mrs = &mi->r[idx].stats;
697         tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_ewma) * 10;
698         tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024;
699
700         return tmp_cur_tp;
701 }
702
703 const struct rate_control_ops mac80211_minstrel = {
704         .name = "minstrel",
705         .tx_status_noskb = minstrel_tx_status,
706         .get_rate = minstrel_get_rate,
707         .rate_init = minstrel_rate_init,
708         .alloc = minstrel_alloc,
709         .free = minstrel_free,
710         .alloc_sta = minstrel_alloc_sta,
711         .free_sta = minstrel_free_sta,
712 #ifdef CONFIG_MAC80211_DEBUGFS
713         .add_sta_debugfs = minstrel_add_sta_debugfs,
714         .remove_sta_debugfs = minstrel_remove_sta_debugfs,
715 #endif
716         .get_expected_throughput = minstrel_get_expected_throughput,
717 };
718
719 int __init
720 rc80211_minstrel_init(void)
721 {
722         return ieee80211_rate_control_register(&mac80211_minstrel);
723 }
724
725 void
726 rc80211_minstrel_exit(void)
727 {
728         ieee80211_rate_control_unregister(&mac80211_minstrel);
729 }
730