mac80211: minstrel_ht: add support for OFDM rates on non-HT clients
[linux-2.6-microblaze.git] / net / mac80211 / rc80211_minstrel_ht.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
4  * Copyright (C) 2019-2020 Intel Corporation
5  */
6 #include <linux/netdevice.h>
7 #include <linux/types.h>
8 #include <linux/skbuff.h>
9 #include <linux/debugfs.h>
10 #include <linux/random.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ieee80211.h>
13 #include <net/mac80211.h>
14 #include "rate.h"
15 #include "sta_info.h"
16 #include "rc80211_minstrel.h"
17 #include "rc80211_minstrel_ht.h"
18
19 #define AVG_AMPDU_SIZE  16
20 #define AVG_PKT_SIZE    1200
21
22 #define SAMPLE_SWITCH_THR       100
23
24 /* Number of bits for an average sized packet */
25 #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
26
27 /* Number of symbols for a packet with (bps) bits per symbol */
28 #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
29
30 /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
31 #define MCS_SYMBOL_TIME(sgi, syms)                                      \
32         (sgi ?                                                          \
33           ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */             \
34           ((syms) * 1000) << 2          /* syms * 4 us */               \
35         )
36
37 /* Transmit duration for the raw data part of an average sized packet */
38 #define MCS_DURATION(streams, sgi, bps) \
39         (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
40
41 #define BW_20                   0
42 #define BW_40                   1
43 #define BW_80                   2
44
45 /*
46  * Define group sort order: HT40 -> SGI -> #streams
47  */
48 #define GROUP_IDX(_streams, _sgi, _ht40)        \
49         MINSTREL_HT_GROUP_0 +                   \
50         MINSTREL_MAX_STREAMS * 2 * _ht40 +      \
51         MINSTREL_MAX_STREAMS * _sgi +   \
52         _streams - 1
53
54 #define _MAX(a, b) (((a)>(b))?(a):(b))
55
56 #define GROUP_SHIFT(duration)                                           \
57         _MAX(0, 16 - __builtin_clz(duration))
58
59 /* MCS rate information for an MCS group */
60 #define __MCS_GROUP(_streams, _sgi, _ht40, _s)                          \
61         [GROUP_IDX(_streams, _sgi, _ht40)] = {                          \
62         .streams = _streams,                                            \
63         .shift = _s,                                                    \
64         .bw = _ht40,                                                    \
65         .flags =                                                        \
66                 IEEE80211_TX_RC_MCS |                                   \
67                 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |                 \
68                 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),             \
69         .duration = {                                                   \
70                 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s,    \
71                 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s,   \
72                 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s,   \
73                 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s,  \
74                 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s,  \
75                 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s,  \
76                 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s,  \
77                 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s   \
78         }                                                               \
79 }
80
81 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40)                          \
82         GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
83
84 #define MCS_GROUP(_streams, _sgi, _ht40)                                \
85         __MCS_GROUP(_streams, _sgi, _ht40,                              \
86                     MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
87
88 #define VHT_GROUP_IDX(_streams, _sgi, _bw)                              \
89         (MINSTREL_VHT_GROUP_0 +                                         \
90          MINSTREL_MAX_STREAMS * 2 * (_bw) +                             \
91          MINSTREL_MAX_STREAMS * (_sgi) +                                \
92          (_streams) - 1)
93
94 #define BW2VBPS(_bw, r3, r2, r1)                                        \
95         (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
96
97 #define __VHT_GROUP(_streams, _sgi, _bw, _s)                            \
98         [VHT_GROUP_IDX(_streams, _sgi, _bw)] = {                        \
99         .streams = _streams,                                            \
100         .shift = _s,                                                    \
101         .bw = _bw,                                                      \
102         .flags =                                                        \
103                 IEEE80211_TX_RC_VHT_MCS |                               \
104                 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |                 \
105                 (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH :          \
106                  _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),      \
107         .duration = {                                                   \
108                 MCS_DURATION(_streams, _sgi,                            \
109                              BW2VBPS(_bw,  117,  54,  26)) >> _s,       \
110                 MCS_DURATION(_streams, _sgi,                            \
111                              BW2VBPS(_bw,  234, 108,  52)) >> _s,       \
112                 MCS_DURATION(_streams, _sgi,                            \
113                              BW2VBPS(_bw,  351, 162,  78)) >> _s,       \
114                 MCS_DURATION(_streams, _sgi,                            \
115                              BW2VBPS(_bw,  468, 216, 104)) >> _s,       \
116                 MCS_DURATION(_streams, _sgi,                            \
117                              BW2VBPS(_bw,  702, 324, 156)) >> _s,       \
118                 MCS_DURATION(_streams, _sgi,                            \
119                              BW2VBPS(_bw,  936, 432, 208)) >> _s,       \
120                 MCS_DURATION(_streams, _sgi,                            \
121                              BW2VBPS(_bw, 1053, 486, 234)) >> _s,       \
122                 MCS_DURATION(_streams, _sgi,                            \
123                              BW2VBPS(_bw, 1170, 540, 260)) >> _s,       \
124                 MCS_DURATION(_streams, _sgi,                            \
125                              BW2VBPS(_bw, 1404, 648, 312)) >> _s,       \
126                 MCS_DURATION(_streams, _sgi,                            \
127                              BW2VBPS(_bw, 1560, 720, 346)) >> _s        \
128         }                                                               \
129 }
130
131 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw)                            \
132         GROUP_SHIFT(MCS_DURATION(_streams, _sgi,                        \
133                                  BW2VBPS(_bw,  117,  54,  26)))
134
135 #define VHT_GROUP(_streams, _sgi, _bw)                                  \
136         __VHT_GROUP(_streams, _sgi, _bw,                                \
137                     VHT_GROUP_SHIFT(_streams, _sgi, _bw))
138
139 #define CCK_DURATION(_bitrate, _short)                  \
140         (1000 * (10 /* SIFS */ +                        \
141          (_short ? 72 + 24 : 144 + 48) +                \
142          (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate)))
143
144 #define CCK_DURATION_LIST(_short, _s)                   \
145         CCK_DURATION(10, _short) >> _s,                 \
146         CCK_DURATION(20, _short) >> _s,                 \
147         CCK_DURATION(55, _short) >> _s,                 \
148         CCK_DURATION(110, _short) >> _s
149
150 #define __CCK_GROUP(_s)                                 \
151         [MINSTREL_CCK_GROUP] = {                        \
152                 .streams = 1,                           \
153                 .flags = 0,                             \
154                 .shift = _s,                            \
155                 .duration = {                           \
156                         CCK_DURATION_LIST(false, _s),   \
157                         CCK_DURATION_LIST(true, _s)     \
158                 }                                       \
159         }
160
161 #define CCK_GROUP_SHIFT                                 \
162         GROUP_SHIFT(CCK_DURATION(10, false))
163
164 #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT)
165
166 #define OFDM_DURATION(_bitrate)                         \
167         (1000 * (16 /* SIFS + signal ext */ +           \
168          16 /* T_PREAMBLE */ +                          \
169          4 /* T_SIGNAL */ +                             \
170          4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) /     \
171               ((_bitrate) * 4)))))
172
173 #define OFDM_DURATION_LIST(_s)                          \
174         OFDM_DURATION(60) >> _s,                        \
175         OFDM_DURATION(90) >> _s,                        \
176         OFDM_DURATION(120) >> _s,                       \
177         OFDM_DURATION(180) >> _s,                       \
178         OFDM_DURATION(240) >> _s,                       \
179         OFDM_DURATION(360) >> _s,                       \
180         OFDM_DURATION(480) >> _s,                       \
181         OFDM_DURATION(540) >> _s
182
183 #define __OFDM_GROUP(_s)                                \
184         [MINSTREL_OFDM_GROUP] = {                       \
185                 .streams = 1,                           \
186                 .flags = 0,                             \
187                 .shift = _s,                            \
188                 .duration = {                           \
189                         OFDM_DURATION_LIST(_s),         \
190                 }                                       \
191         }
192
193 #define OFDM_GROUP_SHIFT                                \
194         GROUP_SHIFT(OFDM_DURATION(60))
195
196 #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT)
197
198
199 static bool minstrel_vht_only = true;
200 module_param(minstrel_vht_only, bool, 0644);
201 MODULE_PARM_DESC(minstrel_vht_only,
202                  "Use only VHT rates when VHT is supported by sta.");
203
204 /*
205  * To enable sufficiently targeted rate sampling, MCS rates are divided into
206  * groups, based on the number of streams and flags (HT40, SGI) that they
207  * use.
208  *
209  * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
210  * BW -> SGI -> #streams
211  */
212 const struct mcs_group minstrel_mcs_groups[] = {
213         MCS_GROUP(1, 0, BW_20),
214         MCS_GROUP(2, 0, BW_20),
215         MCS_GROUP(3, 0, BW_20),
216         MCS_GROUP(4, 0, BW_20),
217
218         MCS_GROUP(1, 1, BW_20),
219         MCS_GROUP(2, 1, BW_20),
220         MCS_GROUP(3, 1, BW_20),
221         MCS_GROUP(4, 1, BW_20),
222
223         MCS_GROUP(1, 0, BW_40),
224         MCS_GROUP(2, 0, BW_40),
225         MCS_GROUP(3, 0, BW_40),
226         MCS_GROUP(4, 0, BW_40),
227
228         MCS_GROUP(1, 1, BW_40),
229         MCS_GROUP(2, 1, BW_40),
230         MCS_GROUP(3, 1, BW_40),
231         MCS_GROUP(4, 1, BW_40),
232
233         CCK_GROUP,
234         OFDM_GROUP,
235
236         VHT_GROUP(1, 0, BW_20),
237         VHT_GROUP(2, 0, BW_20),
238         VHT_GROUP(3, 0, BW_20),
239         VHT_GROUP(4, 0, BW_20),
240
241         VHT_GROUP(1, 1, BW_20),
242         VHT_GROUP(2, 1, BW_20),
243         VHT_GROUP(3, 1, BW_20),
244         VHT_GROUP(4, 1, BW_20),
245
246         VHT_GROUP(1, 0, BW_40),
247         VHT_GROUP(2, 0, BW_40),
248         VHT_GROUP(3, 0, BW_40),
249         VHT_GROUP(4, 0, BW_40),
250
251         VHT_GROUP(1, 1, BW_40),
252         VHT_GROUP(2, 1, BW_40),
253         VHT_GROUP(3, 1, BW_40),
254         VHT_GROUP(4, 1, BW_40),
255
256         VHT_GROUP(1, 0, BW_80),
257         VHT_GROUP(2, 0, BW_80),
258         VHT_GROUP(3, 0, BW_80),
259         VHT_GROUP(4, 0, BW_80),
260
261         VHT_GROUP(1, 1, BW_80),
262         VHT_GROUP(2, 1, BW_80),
263         VHT_GROUP(3, 1, BW_80),
264         VHT_GROUP(4, 1, BW_80),
265 };
266
267 const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 };
268 const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
269 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
270
271 static void
272 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
273
274 /*
275  * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
276  * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
277  *
278  * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
279  */
280 static u16
281 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
282 {
283         u16 mask = 0;
284
285         if (bw == BW_20) {
286                 if (nss != 3 && nss != 6)
287                         mask = BIT(9);
288         } else if (bw == BW_80) {
289                 if (nss == 3 || nss == 7)
290                         mask = BIT(6);
291                 else if (nss == 6)
292                         mask = BIT(9);
293         } else {
294                 WARN_ON(bw != BW_40);
295         }
296
297         switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
298         case IEEE80211_VHT_MCS_SUPPORT_0_7:
299                 mask |= 0x300;
300                 break;
301         case IEEE80211_VHT_MCS_SUPPORT_0_8:
302                 mask |= 0x200;
303                 break;
304         case IEEE80211_VHT_MCS_SUPPORT_0_9:
305                 break;
306         default:
307                 mask = 0x3ff;
308         }
309
310         return 0x3ff & ~mask;
311 }
312
313 static bool
314 minstrel_ht_is_legacy_group(int group)
315 {
316         return group == MINSTREL_CCK_GROUP ||
317                group == MINSTREL_OFDM_GROUP;
318 }
319
320 /*
321  * Look up an MCS group index based on mac80211 rate information
322  */
323 static int
324 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
325 {
326         return GROUP_IDX((rate->idx / 8) + 1,
327                          !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
328                          !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
329 }
330
331 static int
332 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
333 {
334         return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
335                              !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
336                              !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
337                              2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
338 }
339
340 static struct minstrel_rate_stats *
341 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
342                       struct ieee80211_tx_rate *rate)
343 {
344         int group, idx;
345
346         if (rate->flags & IEEE80211_TX_RC_MCS) {
347                 group = minstrel_ht_get_group_idx(rate);
348                 idx = rate->idx % 8;
349                 goto out;
350         }
351
352         if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
353                 group = minstrel_vht_get_group_idx(rate);
354                 idx = ieee80211_rate_get_vht_mcs(rate);
355                 goto out;
356         }
357
358         group = MINSTREL_CCK_GROUP;
359         for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
360                 if (rate->idx != mp->cck_rates[idx])
361                         continue;
362
363                 /* short preamble */
364                 if ((mi->supported[group] & BIT(idx + 4)) &&
365                     (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
366                                 idx += 4;
367                 goto out;
368         }
369
370         group = MINSTREL_OFDM_GROUP;
371         for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
372                 if (rate->idx == mp->ofdm_rates[mi->band][idx])
373                         goto out;
374
375         idx = 0;
376 out:
377         return &mi->groups[group].rates[idx];
378 }
379
380 static inline struct minstrel_rate_stats *
381 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
382 {
383         return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
384 }
385
386 static unsigned int
387 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
388 {
389         if (!mi->avg_ampdu_len)
390                 return AVG_AMPDU_SIZE;
391
392         return MINSTREL_TRUNC(mi->avg_ampdu_len);
393 }
394
395 /*
396  * Return current throughput based on the average A-MPDU length, taking into
397  * account the expected number of retransmissions and their expected length
398  */
399 int
400 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
401                        int prob_avg)
402 {
403         unsigned int nsecs = 0, overhead = mi->overhead;
404         unsigned int ampdu_len = 1;
405
406         /* do not account throughput if sucess prob is below 10% */
407         if (prob_avg < MINSTREL_FRAC(10, 100))
408                 return 0;
409
410         if (minstrel_ht_is_legacy_group(group))
411                 overhead = mi->overhead_legacy;
412         else
413                 ampdu_len = minstrel_ht_avg_ampdu_len(mi);
414
415         nsecs = 1000 * overhead / ampdu_len;
416         nsecs += minstrel_mcs_groups[group].duration[rate] <<
417                  minstrel_mcs_groups[group].shift;
418
419         /*
420          * For the throughput calculation, limit the probability value to 90% to
421          * account for collision related packet error rate fluctuation
422          * (prob is scaled - see MINSTREL_FRAC above)
423          */
424         if (prob_avg > MINSTREL_FRAC(90, 100))
425                 return MINSTREL_TRUNC(100000 * ((MINSTREL_FRAC(90, 100) * 1000)
426                                                                       / nsecs));
427         else
428                 return MINSTREL_TRUNC(100000 * ((prob_avg * 1000) / nsecs));
429 }
430
431 /*
432  * Find & sort topmost throughput rates
433  *
434  * If multiple rates provide equal throughput the sorting is based on their
435  * current success probability. Higher success probability is preferred among
436  * MCS groups, CCK rates do not provide aggregation and are therefore at last.
437  */
438 static void
439 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
440                                u16 *tp_list)
441 {
442         int cur_group, cur_idx, cur_tp_avg, cur_prob;
443         int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
444         int j = MAX_THR_RATES;
445
446         cur_group = index / MCS_GROUP_RATES;
447         cur_idx = index  % MCS_GROUP_RATES;
448         cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg;
449         cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
450
451         do {
452                 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
453                 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
454                 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
455                 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
456                                                     tmp_prob);
457                 if (cur_tp_avg < tmp_tp_avg ||
458                     (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
459                         break;
460                 j--;
461         } while (j > 0);
462
463         if (j < MAX_THR_RATES - 1) {
464                 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
465                        (MAX_THR_RATES - (j + 1))));
466         }
467         if (j < MAX_THR_RATES)
468                 tp_list[j] = index;
469 }
470
471 /*
472  * Find and set the topmost probability rate per sta and per group
473  */
474 static void
475 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
476 {
477         struct minstrel_mcs_group_data *mg;
478         struct minstrel_rate_stats *mrs;
479         int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
480         int max_tp_group, cur_tp_avg, cur_group, cur_idx;
481         int max_gpr_group, max_gpr_idx;
482         int max_gpr_tp_avg, max_gpr_prob;
483
484         cur_group = index / MCS_GROUP_RATES;
485         cur_idx = index % MCS_GROUP_RATES;
486         mg = &mi->groups[index / MCS_GROUP_RATES];
487         mrs = &mg->rates[index % MCS_GROUP_RATES];
488
489         tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
490         tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
491         tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
492         tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
493
494         /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
495          * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
496         max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
497         if (minstrel_ht_is_legacy_group(index / MCS_GROUP_RATES) &&
498             !minstrel_ht_is_legacy_group(max_tp_group))
499                 return;
500
501         max_gpr_group = mg->max_group_prob_rate / MCS_GROUP_RATES;
502         max_gpr_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
503         max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg;
504
505         if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) {
506                 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
507                                                     mrs->prob_avg);
508                 if (cur_tp_avg > tmp_tp_avg)
509                         mi->max_prob_rate = index;
510
511                 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
512                                                         max_gpr_idx,
513                                                         max_gpr_prob);
514                 if (cur_tp_avg > max_gpr_tp_avg)
515                         mg->max_group_prob_rate = index;
516         } else {
517                 if (mrs->prob_avg > tmp_prob)
518                         mi->max_prob_rate = index;
519                 if (mrs->prob_avg > max_gpr_prob)
520                         mg->max_group_prob_rate = index;
521         }
522 }
523
524
525 /*
526  * Assign new rate set per sta and use CCK rates only if the fastest
527  * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
528  * rate sets where MCS and CCK rates are mixed, because CCK rates can
529  * not use aggregation.
530  */
531 static void
532 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
533                                  u16 tmp_mcs_tp_rate[MAX_THR_RATES],
534                                  u16 tmp_legacy_tp_rate[MAX_THR_RATES])
535 {
536         unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
537         int i;
538
539         tmp_group = tmp_legacy_tp_rate[0] / MCS_GROUP_RATES;
540         tmp_idx = tmp_legacy_tp_rate[0] % MCS_GROUP_RATES;
541         tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
542         tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
543
544         tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
545         tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
546         tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
547         tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
548
549         if (tmp_cck_tp > tmp_mcs_tp) {
550                 for(i = 0; i < MAX_THR_RATES; i++) {
551                         minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i],
552                                                        tmp_mcs_tp_rate);
553                 }
554         }
555
556 }
557
558 /*
559  * Try to increase robustness of max_prob rate by decrease number of
560  * streams if possible.
561  */
562 static inline void
563 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
564 {
565         struct minstrel_mcs_group_data *mg;
566         int tmp_max_streams, group, tmp_idx, tmp_prob;
567         int tmp_tp = 0;
568
569         if (!mi->sta->ht_cap.ht_supported)
570                 return;
571
572         tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
573                           MCS_GROUP_RATES].streams;
574         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
575                 mg = &mi->groups[group];
576                 if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
577                         continue;
578
579                 tmp_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
580                 tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg;
581
582                 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
583                    (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
584                                 mi->max_prob_rate = mg->max_group_prob_rate;
585                                 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
586                                                                 tmp_idx,
587                                                                 tmp_prob);
588                 }
589         }
590 }
591
592 static inline int
593 minstrel_get_duration(int index)
594 {
595         const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
596         unsigned int duration = group->duration[index % MCS_GROUP_RATES];
597         return duration << group->shift;
598 }
599
600 static bool
601 minstrel_ht_probe_group(struct minstrel_ht_sta *mi, const struct mcs_group *tp_group,
602                                                 int tp_idx, const struct mcs_group *group)
603 {
604         if (group->bw < tp_group->bw)
605                 return false;
606
607         if (group->streams == tp_group->streams)
608                 return true;
609
610         if (tp_idx < 4 && group->streams == tp_group->streams - 1)
611                 return true;
612
613         return group->streams == tp_group->streams + 1;
614 }
615
616 static void
617 minstrel_ht_find_probe_rates(struct minstrel_ht_sta *mi, u16 *rates, int *n_rates,
618                              bool faster_rate)
619 {
620         const struct mcs_group *group, *tp_group;
621         int i, g, max_dur;
622         int tp_idx;
623
624         tp_group = &minstrel_mcs_groups[mi->max_tp_rate[0] / MCS_GROUP_RATES];
625         tp_idx = mi->max_tp_rate[0] % MCS_GROUP_RATES;
626
627         max_dur = minstrel_get_duration(mi->max_tp_rate[0]);
628         if (faster_rate)
629                 max_dur -= max_dur / 16;
630
631         for (g = 0; g < MINSTREL_GROUPS_NB; g++) {
632                 u16 supported = mi->supported[g];
633
634                 if (!supported)
635                         continue;
636
637                 group = &minstrel_mcs_groups[g];
638                 if (!minstrel_ht_probe_group(mi, tp_group, tp_idx, group))
639                         continue;
640
641                 for (i = 0; supported; supported >>= 1, i++) {
642                         int idx;
643
644                         if (!(supported & 1))
645                                 continue;
646
647                         if ((group->duration[i] << group->shift) > max_dur)
648                                 continue;
649
650                         idx = g * MCS_GROUP_RATES + i;
651                         if (idx == mi->max_tp_rate[0])
652                                 continue;
653
654                         rates[(*n_rates)++] = idx;
655                         break;
656                 }
657         }
658 }
659
660 static void
661 minstrel_ht_rate_sample_switch(struct minstrel_priv *mp,
662                                struct minstrel_ht_sta *mi)
663 {
664         struct minstrel_rate_stats *mrs;
665         u16 rates[MINSTREL_GROUPS_NB];
666         int n_rates = 0;
667         int probe_rate = 0;
668         bool faster_rate;
669         int i;
670         u8 random;
671
672         /*
673          * Use rate switching instead of probing packets for devices with
674          * little control over retry fallback behavior
675          */
676         if (mp->hw->max_rates > 1)
677                 return;
678
679         /*
680          * If the current EWMA prob is >75%, look for a rate that's 6.25%
681          * faster than the max tp rate.
682          * If that fails, look again for a rate that is at least as fast
683          */
684         mrs = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
685         faster_rate = mrs->prob_avg > MINSTREL_FRAC(75, 100);
686         minstrel_ht_find_probe_rates(mi, rates, &n_rates, faster_rate);
687         if (!n_rates && faster_rate)
688                 minstrel_ht_find_probe_rates(mi, rates, &n_rates, false);
689
690         /* If no suitable rate was found, try to pick the next one in the group */
691         if (!n_rates) {
692                 int g_idx = mi->max_tp_rate[0] / MCS_GROUP_RATES;
693                 u16 supported = mi->supported[g_idx];
694
695                 supported >>= mi->max_tp_rate[0] % MCS_GROUP_RATES;
696                 for (i = 0; supported; supported >>= 1, i++) {
697                         if (!(supported & 1))
698                                 continue;
699
700                         probe_rate = mi->max_tp_rate[0] + i;
701                         goto out;
702                 }
703
704                 return;
705         }
706
707         i = 0;
708         if (n_rates > 1) {
709                 random = prandom_u32();
710                 i = random % n_rates;
711         }
712         probe_rate = rates[i];
713
714 out:
715         mi->sample_rate = probe_rate;
716         mi->sample_mode = MINSTREL_SAMPLE_ACTIVE;
717 }
718
719 /*
720  * Update rate statistics and select new primary rates
721  *
722  * Rules for rate selection:
723  *  - max_prob_rate must use only one stream, as a tradeoff between delivery
724  *    probability and throughput during strong fluctuations
725  *  - as long as the max prob rate has a probability of more than 75%, pick
726  *    higher throughput rates, even if the probablity is a bit lower
727  */
728 static void
729 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
730                          bool sample)
731 {
732         struct minstrel_mcs_group_data *mg;
733         struct minstrel_rate_stats *mrs;
734         int group, i, j, cur_prob;
735         u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
736         u16 tmp_legacy_tp_rate[MAX_THR_RATES], index;
737         bool ht_supported = mi->sta->ht_cap.ht_supported;
738
739         mi->sample_mode = MINSTREL_SAMPLE_IDLE;
740
741         if (sample) {
742                 mi->total_packets_cur = mi->total_packets -
743                                         mi->total_packets_last;
744                 mi->total_packets_last = mi->total_packets;
745         }
746         if (!mp->sample_switch)
747                 sample = false;
748         if (mi->total_packets_cur < SAMPLE_SWITCH_THR && mp->sample_switch != 1)
749             sample = false;
750
751         if (mi->ampdu_packets > 0) {
752                 if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
753                         mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
754                                 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
755                                               EWMA_LEVEL);
756                 else
757                         mi->avg_ampdu_len = 0;
758                 mi->ampdu_len = 0;
759                 mi->ampdu_packets = 0;
760         }
761
762         mi->sample_slow = 0;
763         mi->sample_count = 0;
764
765         memset(tmp_mcs_tp_rate, 0, sizeof(tmp_mcs_tp_rate));
766         memset(tmp_legacy_tp_rate, 0, sizeof(tmp_legacy_tp_rate));
767         if (mi->supported[MINSTREL_CCK_GROUP])
768                 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
769                         tmp_legacy_tp_rate[j] = MINSTREL_CCK_GROUP * MCS_GROUP_RATES;
770         else if (mi->supported[MINSTREL_OFDM_GROUP])
771                 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
772                         tmp_legacy_tp_rate[j] = MINSTREL_OFDM_GROUP * MCS_GROUP_RATES;
773
774         if (mi->supported[MINSTREL_VHT_GROUP_0])
775                 index = MINSTREL_VHT_GROUP_0 * MCS_GROUP_RATES;
776         else if (ht_supported)
777                 index = MINSTREL_HT_GROUP_0 * MCS_GROUP_RATES;
778         else if (mi->supported[MINSTREL_CCK_GROUP])
779                 index = MINSTREL_CCK_GROUP * MCS_GROUP_RATES;
780         else
781                 index = MINSTREL_OFDM_GROUP * MCS_GROUP_RATES;
782
783         for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++)
784                 tmp_mcs_tp_rate[j] = index;
785
786         /* Find best rate sets within all MCS groups*/
787         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
788                 u16 *tp_rate = tmp_mcs_tp_rate;
789
790                 mg = &mi->groups[group];
791                 if (!mi->supported[group])
792                         continue;
793
794                 mi->sample_count++;
795
796                 /* (re)Initialize group rate indexes */
797                 for(j = 0; j < MAX_THR_RATES; j++)
798                         tmp_group_tp_rate[j] = MCS_GROUP_RATES * group;
799
800                 if (group == MINSTREL_CCK_GROUP && ht_supported)
801                         tp_rate = tmp_legacy_tp_rate;
802
803                 for (i = 0; i < MCS_GROUP_RATES; i++) {
804                         if (!(mi->supported[group] & BIT(i)))
805                                 continue;
806
807                         index = MCS_GROUP_RATES * group + i;
808
809                         mrs = &mg->rates[i];
810                         mrs->retry_updated = false;
811                         minstrel_calc_rate_stats(mp, mrs);
812                         cur_prob = mrs->prob_avg;
813
814                         if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
815                                 continue;
816
817                         /* Find max throughput rate set */
818                         minstrel_ht_sort_best_tp_rates(mi, index, tp_rate);
819
820                         /* Find max throughput rate set within a group */
821                         minstrel_ht_sort_best_tp_rates(mi, index,
822                                                        tmp_group_tp_rate);
823
824                         /* Find max probability rate per group and global */
825                         minstrel_ht_set_best_prob_rate(mi, index);
826                 }
827
828                 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
829                        sizeof(mg->max_group_tp_rate));
830         }
831
832         /* Assign new rate set per sta */
833         minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate,
834                                          tmp_legacy_tp_rate);
835         memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
836
837         /* Try to increase robustness of max_prob_rate*/
838         minstrel_ht_prob_rate_reduce_streams(mi);
839
840         /* try to sample all available rates during each interval */
841         mi->sample_count *= 8;
842         if (mp->new_avg)
843                 mi->sample_count /= 2;
844
845         if (sample)
846                 minstrel_ht_rate_sample_switch(mp, mi);
847
848 #ifdef CONFIG_MAC80211_DEBUGFS
849         /* use fixed index if set */
850         if (mp->fixed_rate_idx != -1) {
851                 for (i = 0; i < 4; i++)
852                         mi->max_tp_rate[i] = mp->fixed_rate_idx;
853                 mi->max_prob_rate = mp->fixed_rate_idx;
854                 mi->sample_mode = MINSTREL_SAMPLE_IDLE;
855         }
856 #endif
857
858         /* Reset update timer */
859         mi->last_stats_update = jiffies;
860 }
861
862 static bool
863 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
864                          struct ieee80211_tx_rate *rate)
865 {
866         int i;
867
868         if (rate->idx < 0)
869                 return false;
870
871         if (!rate->count)
872                 return false;
873
874         if (rate->flags & IEEE80211_TX_RC_MCS ||
875             rate->flags & IEEE80211_TX_RC_VHT_MCS)
876                 return true;
877
878         for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++)
879                 if (rate->idx == mp->cck_rates[i])
880                         return true;
881
882         for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++)
883                 if (rate->idx == mp->ofdm_rates[mi->band][i])
884                         return true;
885
886         return false;
887 }
888
889 static void
890 minstrel_set_next_sample_idx(struct minstrel_ht_sta *mi)
891 {
892         struct minstrel_mcs_group_data *mg;
893
894         for (;;) {
895                 mi->sample_group++;
896                 mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
897                 mg = &mi->groups[mi->sample_group];
898
899                 if (!mi->supported[mi->sample_group])
900                         continue;
901
902                 if (++mg->index >= MCS_GROUP_RATES) {
903                         mg->index = 0;
904                         if (++mg->column >= ARRAY_SIZE(sample_table))
905                                 mg->column = 0;
906                 }
907                 break;
908         }
909 }
910
911 static void
912 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
913 {
914         int group, orig_group;
915
916         orig_group = group = *idx / MCS_GROUP_RATES;
917         while (group > 0) {
918                 group--;
919
920                 if (!mi->supported[group])
921                         continue;
922
923                 if (minstrel_mcs_groups[group].streams >
924                     minstrel_mcs_groups[orig_group].streams)
925                         continue;
926
927                 if (primary)
928                         *idx = mi->groups[group].max_group_tp_rate[0];
929                 else
930                         *idx = mi->groups[group].max_group_tp_rate[1];
931                 break;
932         }
933 }
934
935 static void
936 minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
937 {
938         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
939         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
940         u16 tid;
941
942         if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
943                 return;
944
945         if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
946                 return;
947
948         if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
949                 return;
950
951         tid = ieee80211_get_tid(hdr);
952         if (likely(sta->ampdu_mlme.tid_tx[tid]))
953                 return;
954
955         ieee80211_start_tx_ba_session(pubsta, tid, 0);
956 }
957
958 static void
959 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
960                       void *priv_sta, struct ieee80211_tx_status *st)
961 {
962         struct ieee80211_tx_info *info = st->info;
963         struct minstrel_ht_sta_priv *msp = priv_sta;
964         struct minstrel_ht_sta *mi = &msp->ht;
965         struct ieee80211_tx_rate *ar = info->status.rates;
966         struct minstrel_rate_stats *rate, *rate2, *rate_sample = NULL;
967         struct minstrel_priv *mp = priv;
968         u32 update_interval = mp->update_interval / 2;
969         bool last, update = false;
970         bool sample_status = false;
971         int i;
972
973         /* This packet was aggregated but doesn't carry status info */
974         if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
975             !(info->flags & IEEE80211_TX_STAT_AMPDU))
976                 return;
977
978         if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
979                 info->status.ampdu_ack_len =
980                         (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
981                 info->status.ampdu_len = 1;
982         }
983
984         mi->ampdu_packets++;
985         mi->ampdu_len += info->status.ampdu_len;
986
987         if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
988                 int avg_ampdu_len = minstrel_ht_avg_ampdu_len(mi);
989
990                 mi->sample_wait = 16 + 2 * avg_ampdu_len;
991                 mi->sample_tries = 1;
992                 mi->sample_count--;
993         }
994
995         if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
996                 mi->sample_packets += info->status.ampdu_len;
997
998         if (mi->sample_mode != MINSTREL_SAMPLE_IDLE)
999                 rate_sample = minstrel_get_ratestats(mi, mi->sample_rate);
1000
1001         last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]);
1002         for (i = 0; !last; i++) {
1003                 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
1004                        !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]);
1005
1006                 rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
1007                 if (rate == rate_sample)
1008                         sample_status = true;
1009
1010                 if (last)
1011                         rate->success += info->status.ampdu_ack_len;
1012
1013                 rate->attempts += ar[i].count * info->status.ampdu_len;
1014         }
1015
1016         switch (mi->sample_mode) {
1017         case MINSTREL_SAMPLE_IDLE:
1018                 if (mp->new_avg &&
1019                     (mp->hw->max_rates > 1 ||
1020                      mi->total_packets_cur < SAMPLE_SWITCH_THR))
1021                         update_interval /= 2;
1022                 break;
1023
1024         case MINSTREL_SAMPLE_ACTIVE:
1025                 if (!sample_status)
1026                         break;
1027
1028                 mi->sample_mode = MINSTREL_SAMPLE_PENDING;
1029                 update = true;
1030                 break;
1031
1032         case MINSTREL_SAMPLE_PENDING:
1033                 if (sample_status)
1034                         break;
1035
1036                 update = true;
1037                 minstrel_ht_update_stats(mp, mi, false);
1038                 break;
1039         }
1040
1041
1042         if (mp->hw->max_rates > 1) {
1043                 /*
1044                  * check for sudden death of spatial multiplexing,
1045                  * downgrade to a lower number of streams if necessary.
1046                  */
1047                 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
1048                 if (rate->attempts > 30 &&
1049                     rate->success < rate->attempts / 4) {
1050                         minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
1051                         update = true;
1052                 }
1053
1054                 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
1055                 if (rate2->attempts > 30 &&
1056                     rate2->success < rate2->attempts / 4) {
1057                         minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
1058                         update = true;
1059                 }
1060         }
1061
1062         if (time_after(jiffies, mi->last_stats_update + update_interval)) {
1063                 update = true;
1064                 minstrel_ht_update_stats(mp, mi, true);
1065         }
1066
1067         if (update)
1068                 minstrel_ht_update_rates(mp, mi);
1069 }
1070
1071 static void
1072 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1073                          int index)
1074 {
1075         struct minstrel_rate_stats *mrs;
1076         unsigned int tx_time, tx_time_rtscts, tx_time_data;
1077         unsigned int cw = mp->cw_min;
1078         unsigned int ctime = 0;
1079         unsigned int t_slot = 9; /* FIXME */
1080         unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
1081         unsigned int overhead = 0, overhead_rtscts = 0;
1082
1083         mrs = minstrel_get_ratestats(mi, index);
1084         if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) {
1085                 mrs->retry_count = 1;
1086                 mrs->retry_count_rtscts = 1;
1087                 return;
1088         }
1089
1090         mrs->retry_count = 2;
1091         mrs->retry_count_rtscts = 2;
1092         mrs->retry_updated = true;
1093
1094         tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
1095
1096         /* Contention time for first 2 tries */
1097         ctime = (t_slot * cw) >> 1;
1098         cw = min((cw << 1) | 1, mp->cw_max);
1099         ctime += (t_slot * cw) >> 1;
1100         cw = min((cw << 1) | 1, mp->cw_max);
1101
1102         if (minstrel_ht_is_legacy_group(index / MCS_GROUP_RATES)) {
1103                 overhead = mi->overhead_legacy;
1104                 overhead_rtscts = mi->overhead_legacy_rtscts;
1105         } else {
1106                 overhead = mi->overhead;
1107                 overhead_rtscts = mi->overhead_rtscts;
1108         }
1109
1110         /* Total TX time for data and Contention after first 2 tries */
1111         tx_time = ctime + 2 * (overhead + tx_time_data);
1112         tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
1113
1114         /* See how many more tries we can fit inside segment size */
1115         do {
1116                 /* Contention time for this try */
1117                 ctime = (t_slot * cw) >> 1;
1118                 cw = min((cw << 1) | 1, mp->cw_max);
1119
1120                 /* Total TX time after this try */
1121                 tx_time += ctime + overhead + tx_time_data;
1122                 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
1123
1124                 if (tx_time_rtscts < mp->segment_size)
1125                         mrs->retry_count_rtscts++;
1126         } while ((tx_time < mp->segment_size) &&
1127                  (++mrs->retry_count < mp->max_retry));
1128 }
1129
1130
1131 static void
1132 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1133                      struct ieee80211_sta_rates *ratetbl, int offset, int index)
1134 {
1135         int group_idx = index / MCS_GROUP_RATES;
1136         const struct mcs_group *group = &minstrel_mcs_groups[group_idx];
1137         struct minstrel_rate_stats *mrs;
1138         u8 idx;
1139         u16 flags = group->flags;
1140
1141         mrs = minstrel_get_ratestats(mi, index);
1142         if (!mrs->retry_updated)
1143                 minstrel_calc_retransmit(mp, mi, index);
1144
1145         if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
1146                 ratetbl->rate[offset].count = 2;
1147                 ratetbl->rate[offset].count_rts = 2;
1148                 ratetbl->rate[offset].count_cts = 2;
1149         } else {
1150                 ratetbl->rate[offset].count = mrs->retry_count;
1151                 ratetbl->rate[offset].count_cts = mrs->retry_count;
1152                 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
1153         }
1154
1155         index %= MCS_GROUP_RATES;
1156         if (group_idx == MINSTREL_CCK_GROUP)
1157                 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
1158         else if (group_idx == MINSTREL_OFDM_GROUP)
1159                 idx = mp->ofdm_rates[mi->band][index %
1160                                                ARRAY_SIZE(mp->ofdm_rates[0])];
1161         else if (flags & IEEE80211_TX_RC_VHT_MCS)
1162                 idx = ((group->streams - 1) << 4) |
1163                       (index & 0xF);
1164         else
1165                 idx = index + (group->streams - 1) * 8;
1166
1167         /* enable RTS/CTS if needed:
1168          *  - if station is in dynamic SMPS (and streams > 1)
1169          *  - for fallback rates, to increase chances of getting through
1170          */
1171         if (offset > 0 ||
1172             (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
1173              group->streams > 1)) {
1174                 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
1175                 flags |= IEEE80211_TX_RC_USE_RTS_CTS;
1176         }
1177
1178         ratetbl->rate[offset].idx = idx;
1179         ratetbl->rate[offset].flags = flags;
1180 }
1181
1182 static inline int
1183 minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate)
1184 {
1185         int group = rate / MCS_GROUP_RATES;
1186         rate %= MCS_GROUP_RATES;
1187         return mi->groups[group].rates[rate].prob_avg;
1188 }
1189
1190 static int
1191 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
1192 {
1193         int group = mi->max_prob_rate / MCS_GROUP_RATES;
1194         const struct mcs_group *g = &minstrel_mcs_groups[group];
1195         int rate = mi->max_prob_rate % MCS_GROUP_RATES;
1196         unsigned int duration;
1197
1198         /* Disable A-MSDU if max_prob_rate is bad */
1199         if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100))
1200                 return 1;
1201
1202         duration = g->duration[rate];
1203         duration <<= g->shift;
1204
1205         /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
1206         if (duration > MCS_DURATION(1, 0, 52))
1207                 return 500;
1208
1209         /*
1210          * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
1211          * data packet size
1212          */
1213         if (duration > MCS_DURATION(1, 0, 104))
1214                 return 1600;
1215
1216         /*
1217          * If the rate is slower than single-stream MCS7, or if the max throughput
1218          * rate success probability is less than 75%, limit A-MSDU to twice the usual
1219          * data packet size
1220          */
1221         if (duration > MCS_DURATION(1, 0, 260) ||
1222             (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) <
1223              MINSTREL_FRAC(75, 100)))
1224                 return 3200;
1225
1226         /*
1227          * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
1228          * Since aggregation sessions are started/stopped without txq flush, use
1229          * the limit here to avoid the complexity of having to de-aggregate
1230          * packets in the queue.
1231          */
1232         if (!mi->sta->vht_cap.vht_supported)
1233                 return IEEE80211_MAX_MPDU_LEN_HT_BA;
1234
1235         /* unlimited */
1236         return 0;
1237 }
1238
1239 static void
1240 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1241 {
1242         struct ieee80211_sta_rates *rates;
1243         u16 first_rate = mi->max_tp_rate[0];
1244         int i = 0;
1245
1246         if (mi->sample_mode == MINSTREL_SAMPLE_ACTIVE)
1247                 first_rate = mi->sample_rate;
1248
1249         rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
1250         if (!rates)
1251                 return;
1252
1253         /* Start with max_tp_rate[0] */
1254         minstrel_ht_set_rate(mp, mi, rates, i++, first_rate);
1255
1256         if (mp->hw->max_rates >= 3) {
1257                 /* At least 3 tx rates supported, use max_tp_rate[1] next */
1258                 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
1259         }
1260
1261         if (mp->hw->max_rates >= 2) {
1262                 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
1263         }
1264
1265         mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
1266         rates->rate[i].idx = -1;
1267         rate_control_set_rates(mp->hw, mi->sta, rates);
1268 }
1269
1270 static int
1271 minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1272 {
1273         struct minstrel_rate_stats *mrs;
1274         struct minstrel_mcs_group_data *mg;
1275         unsigned int sample_dur, sample_group, cur_max_tp_streams;
1276         int tp_rate1, tp_rate2;
1277         int sample_idx = 0;
1278
1279         if (mp->hw->max_rates == 1 && mp->sample_switch &&
1280             (mi->total_packets_cur >= SAMPLE_SWITCH_THR ||
1281              mp->sample_switch == 1))
1282                 return -1;
1283
1284         if (mi->sample_wait > 0) {
1285                 mi->sample_wait--;
1286                 return -1;
1287         }
1288
1289         if (!mi->sample_tries)
1290                 return -1;
1291
1292         sample_group = mi->sample_group;
1293         mg = &mi->groups[sample_group];
1294         sample_idx = sample_table[mg->column][mg->index];
1295         minstrel_set_next_sample_idx(mi);
1296
1297         if (!(mi->supported[sample_group] & BIT(sample_idx)))
1298                 return -1;
1299
1300         mrs = &mg->rates[sample_idx];
1301         sample_idx += sample_group * MCS_GROUP_RATES;
1302
1303         /* Set tp_rate1, tp_rate2 to the highest / second highest max_tp_rate */
1304         if (minstrel_get_duration(mi->max_tp_rate[0]) >
1305             minstrel_get_duration(mi->max_tp_rate[1])) {
1306                 tp_rate1 = mi->max_tp_rate[1];
1307                 tp_rate2 = mi->max_tp_rate[0];
1308         } else {
1309                 tp_rate1 = mi->max_tp_rate[0];
1310                 tp_rate2 = mi->max_tp_rate[1];
1311         }
1312
1313         /*
1314          * Sampling might add some overhead (RTS, no aggregation)
1315          * to the frame. Hence, don't use sampling for the highest currently
1316          * used highest throughput or probability rate.
1317          */
1318         if (sample_idx == mi->max_tp_rate[0] || sample_idx == mi->max_prob_rate)
1319                 return -1;
1320
1321         /*
1322          * Do not sample if the probability is already higher than 95%,
1323          * or if the rate is 3 times slower than the current max probability
1324          * rate, to avoid wasting airtime.
1325          */
1326         sample_dur = minstrel_get_duration(sample_idx);
1327         if (mrs->prob_avg > MINSTREL_FRAC(95, 100) ||
1328             minstrel_get_duration(mi->max_prob_rate) * 3 < sample_dur)
1329                 return -1;
1330
1331
1332         /*
1333          * For devices with no configurable multi-rate retry, skip sampling
1334          * below the per-group max throughput rate, and only use one sampling
1335          * attempt per rate
1336          */
1337         if (mp->hw->max_rates == 1 &&
1338             (minstrel_get_duration(mg->max_group_tp_rate[0]) < sample_dur ||
1339              mrs->attempts))
1340                 return -1;
1341
1342         /* Skip already sampled slow rates */
1343         if (sample_dur >= minstrel_get_duration(tp_rate1) && mrs->attempts)
1344                 return -1;
1345
1346         /*
1347          * Make sure that lower rates get sampled only occasionally,
1348          * if the link is working perfectly.
1349          */
1350
1351         cur_max_tp_streams = minstrel_mcs_groups[tp_rate1 /
1352                 MCS_GROUP_RATES].streams;
1353         if (sample_dur >= minstrel_get_duration(tp_rate2) &&
1354             (cur_max_tp_streams - 1 <
1355              minstrel_mcs_groups[sample_group].streams ||
1356              sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
1357                 if (mrs->sample_skipped < 20)
1358                         return -1;
1359
1360                 if (mi->sample_slow++ > 2)
1361                         return -1;
1362         }
1363         mi->sample_tries--;
1364
1365         return sample_idx;
1366 }
1367
1368 static void
1369 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1370                      struct ieee80211_tx_rate_control *txrc)
1371 {
1372         const struct mcs_group *sample_group;
1373         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1374         struct ieee80211_tx_rate *rate = &info->status.rates[0];
1375         struct minstrel_ht_sta_priv *msp = priv_sta;
1376         struct minstrel_ht_sta *mi = &msp->ht;
1377         struct minstrel_priv *mp = priv;
1378         int sample_idx;
1379
1380         if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
1381             !minstrel_ht_is_legacy_group(mi->max_prob_rate / MCS_GROUP_RATES))
1382                 minstrel_aggr_check(sta, txrc->skb);
1383
1384         info->flags |= mi->tx_flags;
1385
1386 #ifdef CONFIG_MAC80211_DEBUGFS
1387         if (mp->fixed_rate_idx != -1)
1388                 return;
1389 #endif
1390
1391         /* Don't use EAPOL frames for sampling on non-mrr hw */
1392         if (mp->hw->max_rates == 1 &&
1393             (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
1394                 sample_idx = -1;
1395         else
1396                 sample_idx = minstrel_get_sample_rate(mp, mi);
1397
1398         mi->total_packets++;
1399
1400         /* wraparound */
1401         if (mi->total_packets == ~0) {
1402                 mi->total_packets = 0;
1403                 mi->sample_packets = 0;
1404         }
1405
1406         if (sample_idx < 0)
1407                 return;
1408
1409         sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
1410         sample_idx %= MCS_GROUP_RATES;
1411
1412         if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
1413             (sample_idx >= 4) != txrc->short_preamble)
1414                 return;
1415
1416         info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1417         rate->count = 1;
1418
1419         if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
1420                 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
1421                 rate->idx = mp->cck_rates[idx];
1422         } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) {
1423                 int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]);
1424                 rate->idx = mp->ofdm_rates[mi->band][idx];
1425         } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
1426                 ieee80211_rate_set_vht(rate, sample_idx % MCS_GROUP_RATES,
1427                                        sample_group->streams);
1428         } else {
1429                 rate->idx = sample_idx + (sample_group->streams - 1) * 8;
1430         }
1431
1432         rate->flags = sample_group->flags;
1433 }
1434
1435 static void
1436 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1437                        struct ieee80211_supported_band *sband,
1438                        struct ieee80211_sta *sta)
1439 {
1440         int i;
1441
1442         if (sband->band != NL80211_BAND_2GHZ)
1443                 return;
1444
1445         if (sta->ht_cap.ht_supported &&
1446             !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
1447                 return;
1448
1449         for (i = 0; i < 4; i++) {
1450                 if (mp->cck_rates[i] == 0xff ||
1451                     !rate_supported(sta, sband->band, mp->cck_rates[i]))
1452                         continue;
1453
1454                 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i);
1455                 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1456                         mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4);
1457         }
1458 }
1459
1460 static void
1461 minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1462                         struct ieee80211_supported_band *sband,
1463                         struct ieee80211_sta *sta)
1464 {
1465         const u8 *rates;
1466         int i;
1467
1468         if (sta->ht_cap.ht_supported)
1469                 return;
1470
1471         rates = mp->ofdm_rates[sband->band];
1472         for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) {
1473                 if (rates[i] == 0xff ||
1474                     !rate_supported(sta, sband->band, rates[i]))
1475                         continue;
1476
1477                 mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i);
1478         }
1479 }
1480
1481 static void
1482 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1483                         struct cfg80211_chan_def *chandef,
1484                         struct ieee80211_sta *sta, void *priv_sta)
1485 {
1486         struct minstrel_priv *mp = priv;
1487         struct minstrel_ht_sta_priv *msp = priv_sta;
1488         struct minstrel_ht_sta *mi = &msp->ht;
1489         struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
1490         u16 ht_cap = sta->ht_cap.cap;
1491         struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1492         const struct ieee80211_rate *ctl_rate;
1493         bool ldpc, erp;
1494         int use_vht;
1495         int n_supported = 0;
1496         int ack_dur;
1497         int stbc;
1498         int i;
1499
1500         BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
1501
1502         if (vht_cap->vht_supported)
1503                 use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
1504         else
1505                 use_vht = 0;
1506
1507         memset(mi, 0, sizeof(*mi));
1508
1509         mi->sta = sta;
1510         mi->band = sband->band;
1511         mi->last_stats_update = jiffies;
1512
1513         ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1514         mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
1515         mi->overhead += ack_dur;
1516         mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
1517
1518         ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)];
1519         erp = ctl_rate->flags & IEEE80211_RATE_ERP_G;
1520         ack_dur = ieee80211_frame_duration(sband->band, 10,
1521                                            ctl_rate->bitrate, erp, 1,
1522                                            ieee80211_chandef_get_shift(chandef));
1523         mi->overhead_legacy = ack_dur;
1524         mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur;
1525
1526         mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
1527
1528         /* When using MRR, sample more on the first attempt, without delay */
1529         if (mp->has_mrr) {
1530                 mi->sample_count = 16;
1531                 mi->sample_wait = 0;
1532         } else {
1533                 mi->sample_count = 8;
1534                 mi->sample_wait = 8;
1535         }
1536         mi->sample_tries = 4;
1537
1538         if (!use_vht) {
1539                 stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
1540                         IEEE80211_HT_CAP_RX_STBC_SHIFT;
1541
1542                 ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
1543         } else {
1544                 stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
1545                         IEEE80211_VHT_CAP_RXSTBC_SHIFT;
1546
1547                 ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
1548         }
1549
1550         mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1551         if (ldpc)
1552                 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1553
1554         for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
1555                 u32 gflags = minstrel_mcs_groups[i].flags;
1556                 int bw, nss;
1557
1558                 mi->supported[i] = 0;
1559                 if (minstrel_ht_is_legacy_group(i))
1560                         continue;
1561
1562                 if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1563                         if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1564                                 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
1565                                         continue;
1566                         } else {
1567                                 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
1568                                         continue;
1569                         }
1570                 }
1571
1572                 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1573                     sta->bandwidth < IEEE80211_STA_RX_BW_40)
1574                         continue;
1575
1576                 nss = minstrel_mcs_groups[i].streams;
1577
1578                 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
1579                 if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
1580                         continue;
1581
1582                 /* HT rate */
1583                 if (gflags & IEEE80211_TX_RC_MCS) {
1584                         if (use_vht && minstrel_vht_only)
1585                                 continue;
1586
1587                         mi->supported[i] = mcs->rx_mask[nss - 1];
1588                         if (mi->supported[i])
1589                                 n_supported++;
1590                         continue;
1591                 }
1592
1593                 /* VHT rate */
1594                 if (!vht_cap->vht_supported ||
1595                     WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
1596                     WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
1597                         continue;
1598
1599                 if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
1600                         if (sta->bandwidth < IEEE80211_STA_RX_BW_80 ||
1601                             ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
1602                              !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
1603                                 continue;
1604                         }
1605                 }
1606
1607                 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1608                         bw = BW_40;
1609                 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1610                         bw = BW_80;
1611                 else
1612                         bw = BW_20;
1613
1614                 mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
1615                                 vht_cap->vht_mcs.tx_mcs_map);
1616
1617                 if (mi->supported[i])
1618                         n_supported++;
1619         }
1620
1621         minstrel_ht_update_cck(mp, mi, sband, sta);
1622         minstrel_ht_update_ofdm(mp, mi, sband, sta);
1623
1624         /* create an initial rate table with the lowest supported rates */
1625         minstrel_ht_update_stats(mp, mi, true);
1626         minstrel_ht_update_rates(mp, mi);
1627 }
1628
1629 static void
1630 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
1631                       struct cfg80211_chan_def *chandef,
1632                       struct ieee80211_sta *sta, void *priv_sta)
1633 {
1634         minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1635 }
1636
1637 static void
1638 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
1639                         struct cfg80211_chan_def *chandef,
1640                         struct ieee80211_sta *sta, void *priv_sta,
1641                         u32 changed)
1642 {
1643         minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1644 }
1645
1646 static void *
1647 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1648 {
1649         struct ieee80211_supported_band *sband;
1650         struct minstrel_ht_sta_priv *msp;
1651         struct minstrel_priv *mp = priv;
1652         struct ieee80211_hw *hw = mp->hw;
1653         int max_rates = 0;
1654         int i;
1655
1656         for (i = 0; i < NUM_NL80211_BANDS; i++) {
1657                 sband = hw->wiphy->bands[i];
1658                 if (sband && sband->n_bitrates > max_rates)
1659                         max_rates = sband->n_bitrates;
1660         }
1661
1662         msp = kzalloc(sizeof(*msp), gfp);
1663         if (!msp)
1664                 return NULL;
1665
1666         msp->ratelist = kcalloc(max_rates, sizeof(struct minstrel_rate), gfp);
1667         if (!msp->ratelist)
1668                 goto error;
1669
1670         msp->sample_table = kmalloc_array(max_rates, SAMPLE_COLUMNS, gfp);
1671         if (!msp->sample_table)
1672                 goto error1;
1673
1674         return msp;
1675
1676 error1:
1677         kfree(msp->ratelist);
1678 error:
1679         kfree(msp);
1680         return NULL;
1681 }
1682
1683 static void
1684 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1685 {
1686         struct minstrel_ht_sta_priv *msp = priv_sta;
1687
1688         kfree(msp->sample_table);
1689         kfree(msp->ratelist);
1690         kfree(msp);
1691 }
1692
1693 static void
1694 minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband,
1695                             const s16 *bitrates, int n_rates, u32 rate_flags)
1696 {
1697         int i, j;
1698
1699         for (i = 0; i < sband->n_bitrates; i++) {
1700                 struct ieee80211_rate *rate = &sband->bitrates[i];
1701
1702                 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1703                         continue;
1704
1705                 for (j = 0; j < n_rates; j++) {
1706                         if (rate->bitrate != bitrates[j])
1707                                 continue;
1708
1709                         dest[j] = i;
1710                         break;
1711                 }
1712         }
1713 }
1714
1715 static void
1716 minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
1717 {
1718         static const s16 bitrates[4] = { 10, 20, 55, 110 };
1719         struct ieee80211_supported_band *sband;
1720         u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1721
1722         memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates));
1723         sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
1724         if (!sband)
1725                 return;
1726
1727         BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates));
1728         minstrel_ht_fill_rate_array(mp->cck_rates, sband,
1729                                     minstrel_cck_bitrates,
1730                                     ARRAY_SIZE(minstrel_cck_bitrates),
1731                                     rate_flags);
1732 }
1733
1734 static void
1735 minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band)
1736 {
1737         static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
1738         struct ieee80211_supported_band *sband;
1739         u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1740
1741         memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band]));
1742         sband = mp->hw->wiphy->bands[band];
1743         if (!sband)
1744                 return;
1745
1746         BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates));
1747         minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband,
1748                                     minstrel_ofdm_bitrates,
1749                                     ARRAY_SIZE(minstrel_ofdm_bitrates),
1750                                     rate_flags);
1751 }
1752
1753 static void *
1754 minstrel_ht_alloc(struct ieee80211_hw *hw)
1755 {
1756         struct minstrel_priv *mp;
1757         int i;
1758
1759         mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
1760         if (!mp)
1761                 return NULL;
1762
1763         mp->sample_switch = -1;
1764
1765         /* contention window settings
1766          * Just an approximation. Using the per-queue values would complicate
1767          * the calculations and is probably unnecessary */
1768         mp->cw_min = 15;
1769         mp->cw_max = 1023;
1770
1771         /* number of packets (in %) to use for sampling other rates
1772          * sample less often for non-mrr packets, because the overhead
1773          * is much higher than with mrr */
1774         mp->lookaround_rate = 5;
1775         mp->lookaround_rate_mrr = 10;
1776
1777         /* maximum time that the hw is allowed to stay in one MRR segment */
1778         mp->segment_size = 6000;
1779
1780         if (hw->max_rate_tries > 0)
1781                 mp->max_retry = hw->max_rate_tries;
1782         else
1783                 /* safe default, does not necessarily have to match hw properties */
1784                 mp->max_retry = 7;
1785
1786         if (hw->max_rates >= 4)
1787                 mp->has_mrr = true;
1788
1789         mp->hw = hw;
1790         mp->update_interval = HZ / 10;
1791         mp->new_avg = true;
1792
1793         minstrel_ht_init_cck_rates(mp);
1794         for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++)
1795             minstrel_ht_init_ofdm_rates(mp, i);
1796
1797         return mp;
1798 }
1799
1800 #ifdef CONFIG_MAC80211_DEBUGFS
1801 static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv,
1802                                     struct dentry *debugfsdir)
1803 {
1804         struct minstrel_priv *mp = priv;
1805
1806         mp->fixed_rate_idx = (u32) -1;
1807         debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
1808                            &mp->fixed_rate_idx);
1809         debugfs_create_u32("sample_switch", S_IRUGO | S_IWUSR, debugfsdir,
1810                            &mp->sample_switch);
1811         debugfs_create_bool("new_avg", S_IRUGO | S_IWUSR, debugfsdir,
1812                            &mp->new_avg);
1813 }
1814 #endif
1815
1816 static void
1817 minstrel_ht_free(void *priv)
1818 {
1819         kfree(priv);
1820 }
1821
1822 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1823 {
1824         struct minstrel_ht_sta_priv *msp = priv_sta;
1825         struct minstrel_ht_sta *mi = &msp->ht;
1826         int i, j, prob, tp_avg;
1827
1828         i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1829         j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
1830         prob = mi->groups[i].rates[j].prob_avg;
1831
1832         /* convert tp_avg from pkt per second in kbps */
1833         tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
1834         tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
1835
1836         return tp_avg;
1837 }
1838
1839 static const struct rate_control_ops mac80211_minstrel_ht = {
1840         .name = "minstrel_ht",
1841         .tx_status_ext = minstrel_ht_tx_status,
1842         .get_rate = minstrel_ht_get_rate,
1843         .rate_init = minstrel_ht_rate_init,
1844         .rate_update = minstrel_ht_rate_update,
1845         .alloc_sta = minstrel_ht_alloc_sta,
1846         .free_sta = minstrel_ht_free_sta,
1847         .alloc = minstrel_ht_alloc,
1848         .free = minstrel_ht_free,
1849 #ifdef CONFIG_MAC80211_DEBUGFS
1850         .add_debugfs = minstrel_ht_add_debugfs,
1851         .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
1852 #endif
1853         .get_expected_throughput = minstrel_ht_get_expected_throughput,
1854 };
1855
1856
1857 static void __init init_sample_table(void)
1858 {
1859         int col, i, new_idx;
1860         u8 rnd[MCS_GROUP_RATES];
1861
1862         memset(sample_table, 0xff, sizeof(sample_table));
1863         for (col = 0; col < SAMPLE_COLUMNS; col++) {
1864                 prandom_bytes(rnd, sizeof(rnd));
1865                 for (i = 0; i < MCS_GROUP_RATES; i++) {
1866                         new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
1867                         while (sample_table[col][new_idx] != 0xff)
1868                                 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
1869
1870                         sample_table[col][new_idx] = i;
1871                 }
1872         }
1873 }
1874
1875 int __init
1876 rc80211_minstrel_init(void)
1877 {
1878         init_sample_table();
1879         return ieee80211_rate_control_register(&mac80211_minstrel_ht);
1880 }
1881
1882 void
1883 rc80211_minstrel_exit(void)
1884 {
1885         ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
1886 }