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