Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
0004  * Copyright (C) 2019-2021 Intel Corporation
0005  */
0006 #include <linux/netdevice.h>
0007 #include <linux/types.h>
0008 #include <linux/skbuff.h>
0009 #include <linux/debugfs.h>
0010 #include <linux/random.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/ieee80211.h>
0013 #include <linux/minmax.h>
0014 #include <net/mac80211.h>
0015 #include "rate.h"
0016 #include "sta_info.h"
0017 #include "rc80211_minstrel_ht.h"
0018 
0019 #define AVG_AMPDU_SIZE  16
0020 #define AVG_PKT_SIZE    1200
0021 
0022 /* Number of bits for an average sized packet */
0023 #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
0024 
0025 /* Number of symbols for a packet with (bps) bits per symbol */
0026 #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
0027 
0028 /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
0029 #define MCS_SYMBOL_TIME(sgi, syms)                  \
0030     (sgi ?                              \
0031       ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */     \
0032       ((syms) * 1000) << 2      /* syms * 4 us */       \
0033     )
0034 
0035 /* Transmit duration for the raw data part of an average sized packet */
0036 #define MCS_DURATION(streams, sgi, bps) \
0037     (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
0038 
0039 #define BW_20           0
0040 #define BW_40           1
0041 #define BW_80           2
0042 
0043 /*
0044  * Define group sort order: HT40 -> SGI -> #streams
0045  */
0046 #define GROUP_IDX(_streams, _sgi, _ht40)    \
0047     MINSTREL_HT_GROUP_0 +           \
0048     MINSTREL_MAX_STREAMS * 2 * _ht40 +  \
0049     MINSTREL_MAX_STREAMS * _sgi +   \
0050     _streams - 1
0051 
0052 #define _MAX(a, b) (((a)>(b))?(a):(b))
0053 
0054 #define GROUP_SHIFT(duration)                       \
0055     _MAX(0, 16 - __builtin_clz(duration))
0056 
0057 /* MCS rate information for an MCS group */
0058 #define __MCS_GROUP(_streams, _sgi, _ht40, _s)              \
0059     [GROUP_IDX(_streams, _sgi, _ht40)] = {              \
0060     .streams = _streams,                        \
0061     .shift = _s,                            \
0062     .bw = _ht40,                            \
0063     .flags =                            \
0064         IEEE80211_TX_RC_MCS |                   \
0065         (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |         \
0066         (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),     \
0067     .duration = {                           \
0068         MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s,    \
0069         MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s,   \
0070         MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s,   \
0071         MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s,  \
0072         MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s,  \
0073         MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s,  \
0074         MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s,  \
0075         MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s   \
0076     }                               \
0077 }
0078 
0079 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40)              \
0080     GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
0081 
0082 #define MCS_GROUP(_streams, _sgi, _ht40)                \
0083     __MCS_GROUP(_streams, _sgi, _ht40,              \
0084             MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
0085 
0086 #define VHT_GROUP_IDX(_streams, _sgi, _bw)              \
0087     (MINSTREL_VHT_GROUP_0 +                     \
0088      MINSTREL_MAX_STREAMS * 2 * (_bw) +             \
0089      MINSTREL_MAX_STREAMS * (_sgi) +                \
0090      (_streams) - 1)
0091 
0092 #define BW2VBPS(_bw, r3, r2, r1)                    \
0093     (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
0094 
0095 #define __VHT_GROUP(_streams, _sgi, _bw, _s)                \
0096     [VHT_GROUP_IDX(_streams, _sgi, _bw)] = {            \
0097     .streams = _streams,                        \
0098     .shift = _s,                            \
0099     .bw = _bw,                          \
0100     .flags =                            \
0101         IEEE80211_TX_RC_VHT_MCS |               \
0102         (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |         \
0103         (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH :      \
0104          _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),  \
0105     .duration = {                           \
0106         MCS_DURATION(_streams, _sgi,                \
0107                  BW2VBPS(_bw,  117,  54,  26)) >> _s,   \
0108         MCS_DURATION(_streams, _sgi,                \
0109                  BW2VBPS(_bw,  234, 108,  52)) >> _s,   \
0110         MCS_DURATION(_streams, _sgi,                \
0111                  BW2VBPS(_bw,  351, 162,  78)) >> _s,   \
0112         MCS_DURATION(_streams, _sgi,                \
0113                  BW2VBPS(_bw,  468, 216, 104)) >> _s,   \
0114         MCS_DURATION(_streams, _sgi,                \
0115                  BW2VBPS(_bw,  702, 324, 156)) >> _s,   \
0116         MCS_DURATION(_streams, _sgi,                \
0117                  BW2VBPS(_bw,  936, 432, 208)) >> _s,   \
0118         MCS_DURATION(_streams, _sgi,                \
0119                  BW2VBPS(_bw, 1053, 486, 234)) >> _s,   \
0120         MCS_DURATION(_streams, _sgi,                \
0121                  BW2VBPS(_bw, 1170, 540, 260)) >> _s,   \
0122         MCS_DURATION(_streams, _sgi,                \
0123                  BW2VBPS(_bw, 1404, 648, 312)) >> _s,   \
0124         MCS_DURATION(_streams, _sgi,                \
0125                  BW2VBPS(_bw, 1560, 720, 346)) >> _s    \
0126     }                               \
0127 }
0128 
0129 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw)                \
0130     GROUP_SHIFT(MCS_DURATION(_streams, _sgi,            \
0131                  BW2VBPS(_bw,  117,  54,  26)))
0132 
0133 #define VHT_GROUP(_streams, _sgi, _bw)                  \
0134     __VHT_GROUP(_streams, _sgi, _bw,                \
0135             VHT_GROUP_SHIFT(_streams, _sgi, _bw))
0136 
0137 #define CCK_DURATION(_bitrate, _short)          \
0138     (1000 * (10 /* SIFS */ +            \
0139      (_short ? 72 + 24 : 144 + 48) +        \
0140      (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate)))
0141 
0142 #define CCK_DURATION_LIST(_short, _s)           \
0143     CCK_DURATION(10, _short) >> _s,         \
0144     CCK_DURATION(20, _short) >> _s,         \
0145     CCK_DURATION(55, _short) >> _s,         \
0146     CCK_DURATION(110, _short) >> _s
0147 
0148 #define __CCK_GROUP(_s)                 \
0149     [MINSTREL_CCK_GROUP] = {            \
0150         .streams = 1,               \
0151         .flags = 0,             \
0152         .shift = _s,                \
0153         .duration = {               \
0154             CCK_DURATION_LIST(false, _s),   \
0155             CCK_DURATION_LIST(true, _s) \
0156         }                   \
0157     }
0158 
0159 #define CCK_GROUP_SHIFT                 \
0160     GROUP_SHIFT(CCK_DURATION(10, false))
0161 
0162 #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT)
0163 
0164 #define OFDM_DURATION(_bitrate)             \
0165     (1000 * (16 /* SIFS + signal ext */ +       \
0166      16 /* T_PREAMBLE */ +              \
0167      4 /* T_SIGNAL */ +             \
0168      4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) / \
0169           ((_bitrate) * 4)))))
0170 
0171 #define OFDM_DURATION_LIST(_s)              \
0172     OFDM_DURATION(60) >> _s,            \
0173     OFDM_DURATION(90) >> _s,            \
0174     OFDM_DURATION(120) >> _s,           \
0175     OFDM_DURATION(180) >> _s,           \
0176     OFDM_DURATION(240) >> _s,           \
0177     OFDM_DURATION(360) >> _s,           \
0178     OFDM_DURATION(480) >> _s,           \
0179     OFDM_DURATION(540) >> _s
0180 
0181 #define __OFDM_GROUP(_s)                \
0182     [MINSTREL_OFDM_GROUP] = {           \
0183         .streams = 1,               \
0184         .flags = 0,             \
0185         .shift = _s,                \
0186         .duration = {               \
0187             OFDM_DURATION_LIST(_s),     \
0188         }                   \
0189     }
0190 
0191 #define OFDM_GROUP_SHIFT                \
0192     GROUP_SHIFT(OFDM_DURATION(60))
0193 
0194 #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT)
0195 
0196 
0197 static bool minstrel_vht_only = true;
0198 module_param(minstrel_vht_only, bool, 0644);
0199 MODULE_PARM_DESC(minstrel_vht_only,
0200          "Use only VHT rates when VHT is supported by sta.");
0201 
0202 /*
0203  * To enable sufficiently targeted rate sampling, MCS rates are divided into
0204  * groups, based on the number of streams and flags (HT40, SGI) that they
0205  * use.
0206  *
0207  * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
0208  * BW -> SGI -> #streams
0209  */
0210 const struct mcs_group minstrel_mcs_groups[] = {
0211     MCS_GROUP(1, 0, BW_20),
0212     MCS_GROUP(2, 0, BW_20),
0213     MCS_GROUP(3, 0, BW_20),
0214     MCS_GROUP(4, 0, BW_20),
0215 
0216     MCS_GROUP(1, 1, BW_20),
0217     MCS_GROUP(2, 1, BW_20),
0218     MCS_GROUP(3, 1, BW_20),
0219     MCS_GROUP(4, 1, BW_20),
0220 
0221     MCS_GROUP(1, 0, BW_40),
0222     MCS_GROUP(2, 0, BW_40),
0223     MCS_GROUP(3, 0, BW_40),
0224     MCS_GROUP(4, 0, BW_40),
0225 
0226     MCS_GROUP(1, 1, BW_40),
0227     MCS_GROUP(2, 1, BW_40),
0228     MCS_GROUP(3, 1, BW_40),
0229     MCS_GROUP(4, 1, BW_40),
0230 
0231     CCK_GROUP,
0232     OFDM_GROUP,
0233 
0234     VHT_GROUP(1, 0, BW_20),
0235     VHT_GROUP(2, 0, BW_20),
0236     VHT_GROUP(3, 0, BW_20),
0237     VHT_GROUP(4, 0, BW_20),
0238 
0239     VHT_GROUP(1, 1, BW_20),
0240     VHT_GROUP(2, 1, BW_20),
0241     VHT_GROUP(3, 1, BW_20),
0242     VHT_GROUP(4, 1, BW_20),
0243 
0244     VHT_GROUP(1, 0, BW_40),
0245     VHT_GROUP(2, 0, BW_40),
0246     VHT_GROUP(3, 0, BW_40),
0247     VHT_GROUP(4, 0, BW_40),
0248 
0249     VHT_GROUP(1, 1, BW_40),
0250     VHT_GROUP(2, 1, BW_40),
0251     VHT_GROUP(3, 1, BW_40),
0252     VHT_GROUP(4, 1, BW_40),
0253 
0254     VHT_GROUP(1, 0, BW_80),
0255     VHT_GROUP(2, 0, BW_80),
0256     VHT_GROUP(3, 0, BW_80),
0257     VHT_GROUP(4, 0, BW_80),
0258 
0259     VHT_GROUP(1, 1, BW_80),
0260     VHT_GROUP(2, 1, BW_80),
0261     VHT_GROUP(3, 1, BW_80),
0262     VHT_GROUP(4, 1, BW_80),
0263 };
0264 
0265 const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 };
0266 const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
0267 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
0268 static const u8 minstrel_sample_seq[] = {
0269     MINSTREL_SAMPLE_TYPE_INC,
0270     MINSTREL_SAMPLE_TYPE_JUMP,
0271     MINSTREL_SAMPLE_TYPE_INC,
0272     MINSTREL_SAMPLE_TYPE_JUMP,
0273     MINSTREL_SAMPLE_TYPE_INC,
0274     MINSTREL_SAMPLE_TYPE_SLOW,
0275 };
0276 
0277 static void
0278 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
0279 
0280 /*
0281  * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
0282  * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
0283  *
0284  * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
0285  */
0286 static u16
0287 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
0288 {
0289     u16 mask = 0;
0290 
0291     if (bw == BW_20) {
0292         if (nss != 3 && nss != 6)
0293             mask = BIT(9);
0294     } else if (bw == BW_80) {
0295         if (nss == 3 || nss == 7)
0296             mask = BIT(6);
0297         else if (nss == 6)
0298             mask = BIT(9);
0299     } else {
0300         WARN_ON(bw != BW_40);
0301     }
0302 
0303     switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
0304     case IEEE80211_VHT_MCS_SUPPORT_0_7:
0305         mask |= 0x300;
0306         break;
0307     case IEEE80211_VHT_MCS_SUPPORT_0_8:
0308         mask |= 0x200;
0309         break;
0310     case IEEE80211_VHT_MCS_SUPPORT_0_9:
0311         break;
0312     default:
0313         mask = 0x3ff;
0314     }
0315 
0316     return 0x3ff & ~mask;
0317 }
0318 
0319 static bool
0320 minstrel_ht_is_legacy_group(int group)
0321 {
0322     return group == MINSTREL_CCK_GROUP ||
0323            group == MINSTREL_OFDM_GROUP;
0324 }
0325 
0326 /*
0327  * Look up an MCS group index based on mac80211 rate information
0328  */
0329 static int
0330 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
0331 {
0332     return GROUP_IDX((rate->idx / 8) + 1,
0333              !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
0334              !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
0335 }
0336 
0337 /*
0338  * Look up an MCS group index based on new cfg80211 rate_info.
0339  */
0340 static int
0341 minstrel_ht_ri_get_group_idx(struct rate_info *rate)
0342 {
0343     return GROUP_IDX((rate->mcs / 8) + 1,
0344              !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
0345              !!(rate->bw & RATE_INFO_BW_40));
0346 }
0347 
0348 static int
0349 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
0350 {
0351     return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
0352                  !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
0353                  !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
0354                  2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
0355 }
0356 
0357 /*
0358  * Look up an MCS group index based on new cfg80211 rate_info.
0359  */
0360 static int
0361 minstrel_vht_ri_get_group_idx(struct rate_info *rate)
0362 {
0363     return VHT_GROUP_IDX(rate->nss,
0364                  !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
0365                  !!(rate->bw & RATE_INFO_BW_40) +
0366                  2*!!(rate->bw & RATE_INFO_BW_80));
0367 }
0368 
0369 static struct minstrel_rate_stats *
0370 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
0371               struct ieee80211_tx_rate *rate)
0372 {
0373     int group, idx;
0374 
0375     if (rate->flags & IEEE80211_TX_RC_MCS) {
0376         group = minstrel_ht_get_group_idx(rate);
0377         idx = rate->idx % 8;
0378         goto out;
0379     }
0380 
0381     if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
0382         group = minstrel_vht_get_group_idx(rate);
0383         idx = ieee80211_rate_get_vht_mcs(rate);
0384         goto out;
0385     }
0386 
0387     group = MINSTREL_CCK_GROUP;
0388     for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
0389         if (!(mi->supported[group] & BIT(idx)))
0390             continue;
0391 
0392         if (rate->idx != mp->cck_rates[idx])
0393             continue;
0394 
0395         /* short preamble */
0396         if ((mi->supported[group] & BIT(idx + 4)) &&
0397             (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
0398             idx += 4;
0399         goto out;
0400     }
0401 
0402     group = MINSTREL_OFDM_GROUP;
0403     for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
0404         if (rate->idx == mp->ofdm_rates[mi->band][idx])
0405             goto out;
0406 
0407     idx = 0;
0408 out:
0409     return &mi->groups[group].rates[idx];
0410 }
0411 
0412 /*
0413  * Get the minstrel rate statistics for specified STA and rate info.
0414  */
0415 static struct minstrel_rate_stats *
0416 minstrel_ht_ri_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
0417               struct ieee80211_rate_status *rate_status)
0418 {
0419     int group, idx;
0420     struct rate_info *rate = &rate_status->rate_idx;
0421 
0422     if (rate->flags & RATE_INFO_FLAGS_MCS) {
0423         group = minstrel_ht_ri_get_group_idx(rate);
0424         idx = rate->mcs % 8;
0425         goto out;
0426     }
0427 
0428     if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) {
0429         group = minstrel_vht_ri_get_group_idx(rate);
0430         idx = rate->mcs;
0431         goto out;
0432     }
0433 
0434     group = MINSTREL_CCK_GROUP;
0435     for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
0436         if (rate->legacy != minstrel_cck_bitrates[ mp->cck_rates[idx] ])
0437             continue;
0438 
0439         /* short preamble */
0440         if ((mi->supported[group] & BIT(idx + 4)) &&
0441                             mi->use_short_preamble)
0442             idx += 4;
0443         goto out;
0444     }
0445 
0446     group = MINSTREL_OFDM_GROUP;
0447     for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
0448         if (rate->legacy == minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][idx] ])
0449             goto out;
0450 
0451     idx = 0;
0452 out:
0453     return &mi->groups[group].rates[idx];
0454 }
0455 
0456 static inline struct minstrel_rate_stats *
0457 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
0458 {
0459     return &mi->groups[MI_RATE_GROUP(index)].rates[MI_RATE_IDX(index)];
0460 }
0461 
0462 static inline int minstrel_get_duration(int index)
0463 {
0464     const struct mcs_group *group = &minstrel_mcs_groups[MI_RATE_GROUP(index)];
0465     unsigned int duration = group->duration[MI_RATE_IDX(index)];
0466 
0467     return duration << group->shift;
0468 }
0469 
0470 static unsigned int
0471 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
0472 {
0473     int duration;
0474 
0475     if (mi->avg_ampdu_len)
0476         return MINSTREL_TRUNC(mi->avg_ampdu_len);
0477 
0478     if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(mi->max_tp_rate[0])))
0479         return 1;
0480 
0481     duration = minstrel_get_duration(mi->max_tp_rate[0]);
0482 
0483     if (duration > 400 * 1000)
0484         return 2;
0485 
0486     if (duration > 250 * 1000)
0487         return 4;
0488 
0489     if (duration > 150 * 1000)
0490         return 8;
0491 
0492     return 16;
0493 }
0494 
0495 /*
0496  * Return current throughput based on the average A-MPDU length, taking into
0497  * account the expected number of retransmissions and their expected length
0498  */
0499 int
0500 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
0501                int prob_avg)
0502 {
0503     unsigned int nsecs = 0, overhead = mi->overhead;
0504     unsigned int ampdu_len = 1;
0505 
0506     /* do not account throughput if success prob is below 10% */
0507     if (prob_avg < MINSTREL_FRAC(10, 100))
0508         return 0;
0509 
0510     if (minstrel_ht_is_legacy_group(group))
0511         overhead = mi->overhead_legacy;
0512     else
0513         ampdu_len = minstrel_ht_avg_ampdu_len(mi);
0514 
0515     nsecs = 1000 * overhead / ampdu_len;
0516     nsecs += minstrel_mcs_groups[group].duration[rate] <<
0517          minstrel_mcs_groups[group].shift;
0518 
0519     /*
0520      * For the throughput calculation, limit the probability value to 90% to
0521      * account for collision related packet error rate fluctuation
0522      * (prob is scaled - see MINSTREL_FRAC above)
0523      */
0524     if (prob_avg > MINSTREL_FRAC(90, 100))
0525         prob_avg = MINSTREL_FRAC(90, 100);
0526 
0527     return MINSTREL_TRUNC(100 * ((prob_avg * 1000000) / nsecs));
0528 }
0529 
0530 /*
0531  * Find & sort topmost throughput rates
0532  *
0533  * If multiple rates provide equal throughput the sorting is based on their
0534  * current success probability. Higher success probability is preferred among
0535  * MCS groups, CCK rates do not provide aggregation and are therefore at last.
0536  */
0537 static void
0538 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
0539                    u16 *tp_list)
0540 {
0541     int cur_group, cur_idx, cur_tp_avg, cur_prob;
0542     int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
0543     int j = MAX_THR_RATES;
0544 
0545     cur_group = MI_RATE_GROUP(index);
0546     cur_idx = MI_RATE_IDX(index);
0547     cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg;
0548     cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
0549 
0550     do {
0551         tmp_group = MI_RATE_GROUP(tp_list[j - 1]);
0552         tmp_idx = MI_RATE_IDX(tp_list[j - 1]);
0553         tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
0554         tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
0555                             tmp_prob);
0556         if (cur_tp_avg < tmp_tp_avg ||
0557             (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
0558             break;
0559         j--;
0560     } while (j > 0);
0561 
0562     if (j < MAX_THR_RATES - 1) {
0563         memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
0564                (MAX_THR_RATES - (j + 1))));
0565     }
0566     if (j < MAX_THR_RATES)
0567         tp_list[j] = index;
0568 }
0569 
0570 /*
0571  * Find and set the topmost probability rate per sta and per group
0572  */
0573 static void
0574 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 *dest, u16 index)
0575 {
0576     struct minstrel_mcs_group_data *mg;
0577     struct minstrel_rate_stats *mrs;
0578     int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
0579     int max_tp_group, max_tp_idx, max_tp_prob;
0580     int cur_tp_avg, cur_group, cur_idx;
0581     int max_gpr_group, max_gpr_idx;
0582     int max_gpr_tp_avg, max_gpr_prob;
0583 
0584     cur_group = MI_RATE_GROUP(index);
0585     cur_idx = MI_RATE_IDX(index);
0586     mg = &mi->groups[cur_group];
0587     mrs = &mg->rates[cur_idx];
0588 
0589     tmp_group = MI_RATE_GROUP(*dest);
0590     tmp_idx = MI_RATE_IDX(*dest);
0591     tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
0592     tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
0593 
0594     /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
0595      * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
0596     max_tp_group = MI_RATE_GROUP(mi->max_tp_rate[0]);
0597     max_tp_idx = MI_RATE_IDX(mi->max_tp_rate[0]);
0598     max_tp_prob = mi->groups[max_tp_group].rates[max_tp_idx].prob_avg;
0599 
0600     if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index)) &&
0601         !minstrel_ht_is_legacy_group(max_tp_group))
0602         return;
0603 
0604     /* skip rates faster than max tp rate with lower prob */
0605     if (minstrel_get_duration(mi->max_tp_rate[0]) > minstrel_get_duration(index) &&
0606         mrs->prob_avg < max_tp_prob)
0607         return;
0608 
0609     max_gpr_group = MI_RATE_GROUP(mg->max_group_prob_rate);
0610     max_gpr_idx = MI_RATE_IDX(mg->max_group_prob_rate);
0611     max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg;
0612 
0613     if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) {
0614         cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
0615                             mrs->prob_avg);
0616         if (cur_tp_avg > tmp_tp_avg)
0617             *dest = index;
0618 
0619         max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
0620                             max_gpr_idx,
0621                             max_gpr_prob);
0622         if (cur_tp_avg > max_gpr_tp_avg)
0623             mg->max_group_prob_rate = index;
0624     } else {
0625         if (mrs->prob_avg > tmp_prob)
0626             *dest = index;
0627         if (mrs->prob_avg > max_gpr_prob)
0628             mg->max_group_prob_rate = index;
0629     }
0630 }
0631 
0632 
0633 /*
0634  * Assign new rate set per sta and use CCK rates only if the fastest
0635  * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
0636  * rate sets where MCS and CCK rates are mixed, because CCK rates can
0637  * not use aggregation.
0638  */
0639 static void
0640 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
0641                  u16 tmp_mcs_tp_rate[MAX_THR_RATES],
0642                  u16 tmp_legacy_tp_rate[MAX_THR_RATES])
0643 {
0644     unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
0645     int i;
0646 
0647     tmp_group = MI_RATE_GROUP(tmp_legacy_tp_rate[0]);
0648     tmp_idx = MI_RATE_IDX(tmp_legacy_tp_rate[0]);
0649     tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
0650     tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
0651 
0652     tmp_group = MI_RATE_GROUP(tmp_mcs_tp_rate[0]);
0653     tmp_idx = MI_RATE_IDX(tmp_mcs_tp_rate[0]);
0654     tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
0655     tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
0656 
0657     if (tmp_cck_tp > tmp_mcs_tp) {
0658         for(i = 0; i < MAX_THR_RATES; i++) {
0659             minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i],
0660                                tmp_mcs_tp_rate);
0661         }
0662     }
0663 
0664 }
0665 
0666 /*
0667  * Try to increase robustness of max_prob rate by decrease number of
0668  * streams if possible.
0669  */
0670 static inline void
0671 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
0672 {
0673     struct minstrel_mcs_group_data *mg;
0674     int tmp_max_streams, group, tmp_idx, tmp_prob;
0675     int tmp_tp = 0;
0676 
0677     if (!mi->sta->deflink.ht_cap.ht_supported)
0678         return;
0679 
0680     group = MI_RATE_GROUP(mi->max_tp_rate[0]);
0681     tmp_max_streams = minstrel_mcs_groups[group].streams;
0682     for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
0683         mg = &mi->groups[group];
0684         if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
0685             continue;
0686 
0687         tmp_idx = MI_RATE_IDX(mg->max_group_prob_rate);
0688         tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg;
0689 
0690         if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
0691            (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
0692                 mi->max_prob_rate = mg->max_group_prob_rate;
0693                 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
0694                                 tmp_idx,
0695                                 tmp_prob);
0696         }
0697     }
0698 }
0699 
0700 static u16
0701 __minstrel_ht_get_sample_rate(struct minstrel_ht_sta *mi,
0702                   enum minstrel_sample_type type)
0703 {
0704     u16 *rates = mi->sample[type].sample_rates;
0705     u16 cur;
0706     int i;
0707 
0708     for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
0709         if (!rates[i])
0710             continue;
0711 
0712         cur = rates[i];
0713         rates[i] = 0;
0714         return cur;
0715     }
0716 
0717     return 0;
0718 }
0719 
0720 static inline int
0721 minstrel_ewma(int old, int new, int weight)
0722 {
0723     int diff, incr;
0724 
0725     diff = new - old;
0726     incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
0727 
0728     return old + incr;
0729 }
0730 
0731 static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
0732 {
0733     s32 out_1 = *prev_1;
0734     s32 out_2 = *prev_2;
0735     s32 val;
0736 
0737     if (!in)
0738         in += 1;
0739 
0740     if (!out_1) {
0741         val = out_1 = in;
0742         goto out;
0743     }
0744 
0745     val = MINSTREL_AVG_COEFF1 * in;
0746     val += MINSTREL_AVG_COEFF2 * out_1;
0747     val += MINSTREL_AVG_COEFF3 * out_2;
0748     val >>= MINSTREL_SCALE;
0749 
0750     if (val > 1 << MINSTREL_SCALE)
0751         val = 1 << MINSTREL_SCALE;
0752     if (val < 0)
0753         val = 1;
0754 
0755 out:
0756     *prev_2 = out_1;
0757     *prev_1 = val;
0758 
0759     return val;
0760 }
0761 
0762 /*
0763 * Recalculate statistics and counters of a given rate
0764 */
0765 static void
0766 minstrel_ht_calc_rate_stats(struct minstrel_priv *mp,
0767                 struct minstrel_rate_stats *mrs)
0768 {
0769     unsigned int cur_prob;
0770 
0771     if (unlikely(mrs->attempts > 0)) {
0772         cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
0773         minstrel_filter_avg_add(&mrs->prob_avg,
0774                     &mrs->prob_avg_1, cur_prob);
0775         mrs->att_hist += mrs->attempts;
0776         mrs->succ_hist += mrs->success;
0777     }
0778 
0779     mrs->last_success = mrs->success;
0780     mrs->last_attempts = mrs->attempts;
0781     mrs->success = 0;
0782     mrs->attempts = 0;
0783 }
0784 
0785 static bool
0786 minstrel_ht_find_sample_rate(struct minstrel_ht_sta *mi, int type, int idx)
0787 {
0788     int i;
0789 
0790     for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
0791         u16 cur = mi->sample[type].sample_rates[i];
0792 
0793         if (cur == idx)
0794             return true;
0795 
0796         if (!cur)
0797             break;
0798     }
0799 
0800     return false;
0801 }
0802 
0803 static int
0804 minstrel_ht_move_sample_rates(struct minstrel_ht_sta *mi, int type,
0805                   u32 fast_rate_dur, u32 slow_rate_dur)
0806 {
0807     u16 *rates = mi->sample[type].sample_rates;
0808     int i, j;
0809 
0810     for (i = 0, j = 0; i < MINSTREL_SAMPLE_RATES; i++) {
0811         u32 duration;
0812         bool valid = false;
0813         u16 cur;
0814 
0815         cur = rates[i];
0816         if (!cur)
0817             continue;
0818 
0819         duration = minstrel_get_duration(cur);
0820         switch (type) {
0821         case MINSTREL_SAMPLE_TYPE_SLOW:
0822             valid = duration > fast_rate_dur &&
0823                 duration < slow_rate_dur;
0824             break;
0825         case MINSTREL_SAMPLE_TYPE_INC:
0826         case MINSTREL_SAMPLE_TYPE_JUMP:
0827             valid = duration < fast_rate_dur;
0828             break;
0829         default:
0830             valid = false;
0831             break;
0832         }
0833 
0834         if (!valid) {
0835             rates[i] = 0;
0836             continue;
0837         }
0838 
0839         if (i == j)
0840             continue;
0841 
0842         rates[j++] = cur;
0843         rates[i] = 0;
0844     }
0845 
0846     return j;
0847 }
0848 
0849 static int
0850 minstrel_ht_group_min_rate_offset(struct minstrel_ht_sta *mi, int group,
0851                   u32 max_duration)
0852 {
0853     u16 supported = mi->supported[group];
0854     int i;
0855 
0856     for (i = 0; i < MCS_GROUP_RATES && supported; i++, supported >>= 1) {
0857         if (!(supported & BIT(0)))
0858             continue;
0859 
0860         if (minstrel_get_duration(MI_RATE(group, i)) >= max_duration)
0861             continue;
0862 
0863         return i;
0864     }
0865 
0866     return -1;
0867 }
0868 
0869 /*
0870  * Incremental update rates:
0871  * Flip through groups and pick the first group rate that is faster than the
0872  * highest currently selected rate
0873  */
0874 static u16
0875 minstrel_ht_next_inc_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur)
0876 {
0877     u8 type = MINSTREL_SAMPLE_TYPE_INC;
0878     int i, index = 0;
0879     u8 group;
0880 
0881     group = mi->sample[type].sample_group;
0882     for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
0883         group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
0884 
0885         index = minstrel_ht_group_min_rate_offset(mi, group,
0886                               fast_rate_dur);
0887         if (index < 0)
0888             continue;
0889 
0890         index = MI_RATE(group, index & 0xf);
0891         if (!minstrel_ht_find_sample_rate(mi, type, index))
0892             goto out;
0893     }
0894     index = 0;
0895 
0896 out:
0897     mi->sample[type].sample_group = group;
0898 
0899     return index;
0900 }
0901 
0902 static int
0903 minstrel_ht_next_group_sample_rate(struct minstrel_ht_sta *mi, int group,
0904                    u16 supported, int offset)
0905 {
0906     struct minstrel_mcs_group_data *mg = &mi->groups[group];
0907     u16 idx;
0908     int i;
0909 
0910     for (i = 0; i < MCS_GROUP_RATES; i++) {
0911         idx = sample_table[mg->column][mg->index];
0912         if (++mg->index >= MCS_GROUP_RATES) {
0913             mg->index = 0;
0914             if (++mg->column >= ARRAY_SIZE(sample_table))
0915                 mg->column = 0;
0916         }
0917 
0918         if (idx < offset)
0919             continue;
0920 
0921         if (!(supported & BIT(idx)))
0922             continue;
0923 
0924         return MI_RATE(group, idx);
0925     }
0926 
0927     return -1;
0928 }
0929 
0930 /*
0931  * Jump rates:
0932  * Sample random rates, use those that are faster than the highest
0933  * currently selected rate. Rates between the fastest and the slowest
0934  * get sorted into the slow sample bucket, but only if it has room
0935  */
0936 static u16
0937 minstrel_ht_next_jump_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur,
0938                u32 slow_rate_dur, int *slow_rate_ofs)
0939 {
0940     struct minstrel_rate_stats *mrs;
0941     u32 max_duration = slow_rate_dur;
0942     int i, index, offset;
0943     u16 *slow_rates;
0944     u16 supported;
0945     u32 duration;
0946     u8 group;
0947 
0948     if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
0949         max_duration = fast_rate_dur;
0950 
0951     slow_rates = mi->sample[MINSTREL_SAMPLE_TYPE_SLOW].sample_rates;
0952     group = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group;
0953     for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
0954         u8 type;
0955 
0956         group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
0957 
0958         supported = mi->supported[group];
0959         if (!supported)
0960             continue;
0961 
0962         offset = minstrel_ht_group_min_rate_offset(mi, group,
0963                                max_duration);
0964         if (offset < 0)
0965             continue;
0966 
0967         index = minstrel_ht_next_group_sample_rate(mi, group, supported,
0968                                offset);
0969         if (index < 0)
0970             continue;
0971 
0972         duration = minstrel_get_duration(index);
0973         if (duration < fast_rate_dur)
0974             type = MINSTREL_SAMPLE_TYPE_JUMP;
0975         else
0976             type = MINSTREL_SAMPLE_TYPE_SLOW;
0977 
0978         if (minstrel_ht_find_sample_rate(mi, type, index))
0979             continue;
0980 
0981         if (type == MINSTREL_SAMPLE_TYPE_JUMP)
0982             goto found;
0983 
0984         if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
0985             continue;
0986 
0987         if (duration >= slow_rate_dur)
0988             continue;
0989 
0990         /* skip slow rates with high success probability */
0991         mrs = minstrel_get_ratestats(mi, index);
0992         if (mrs->prob_avg > MINSTREL_FRAC(95, 100))
0993             continue;
0994 
0995         slow_rates[(*slow_rate_ofs)++] = index;
0996         if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
0997             max_duration = fast_rate_dur;
0998     }
0999     index = 0;
1000 
1001 found:
1002     mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group = group;
1003 
1004     return index;
1005 }
1006 
1007 static void
1008 minstrel_ht_refill_sample_rates(struct minstrel_ht_sta *mi)
1009 {
1010     u32 prob_dur = minstrel_get_duration(mi->max_prob_rate);
1011     u32 tp_dur = minstrel_get_duration(mi->max_tp_rate[0]);
1012     u32 tp2_dur = minstrel_get_duration(mi->max_tp_rate[1]);
1013     u32 fast_rate_dur = min(min(tp_dur, tp2_dur), prob_dur);
1014     u32 slow_rate_dur = max(max(tp_dur, tp2_dur), prob_dur);
1015     u16 *rates;
1016     int i, j;
1017 
1018     rates = mi->sample[MINSTREL_SAMPLE_TYPE_INC].sample_rates;
1019     i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_INC,
1020                       fast_rate_dur, slow_rate_dur);
1021     while (i < MINSTREL_SAMPLE_RATES) {
1022         rates[i] = minstrel_ht_next_inc_rate(mi, tp_dur);
1023         if (!rates[i])
1024             break;
1025 
1026         i++;
1027     }
1028 
1029     rates = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_rates;
1030     i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_JUMP,
1031                       fast_rate_dur, slow_rate_dur);
1032     j = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_SLOW,
1033                       fast_rate_dur, slow_rate_dur);
1034     while (i < MINSTREL_SAMPLE_RATES) {
1035         rates[i] = minstrel_ht_next_jump_rate(mi, fast_rate_dur,
1036                               slow_rate_dur, &j);
1037         if (!rates[i])
1038             break;
1039 
1040         i++;
1041     }
1042 
1043     for (i = 0; i < ARRAY_SIZE(mi->sample); i++)
1044         memcpy(mi->sample[i].cur_sample_rates, mi->sample[i].sample_rates,
1045                sizeof(mi->sample[i].cur_sample_rates));
1046 }
1047 
1048 
1049 /*
1050  * Update rate statistics and select new primary rates
1051  *
1052  * Rules for rate selection:
1053  *  - max_prob_rate must use only one stream, as a tradeoff between delivery
1054  *    probability and throughput during strong fluctuations
1055  *  - as long as the max prob rate has a probability of more than 75%, pick
1056  *    higher throughput rates, even if the probablity is a bit lower
1057  */
1058 static void
1059 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1060 {
1061     struct minstrel_mcs_group_data *mg;
1062     struct minstrel_rate_stats *mrs;
1063     int group, i, j, cur_prob;
1064     u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
1065     u16 tmp_legacy_tp_rate[MAX_THR_RATES], tmp_max_prob_rate;
1066     u16 index;
1067     bool ht_supported = mi->sta->deflink.ht_cap.ht_supported;
1068 
1069     if (mi->ampdu_packets > 0) {
1070         if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
1071             mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
1072                 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
1073                           EWMA_LEVEL);
1074         else
1075             mi->avg_ampdu_len = 0;
1076         mi->ampdu_len = 0;
1077         mi->ampdu_packets = 0;
1078     }
1079 
1080     if (mi->supported[MINSTREL_CCK_GROUP])
1081         group = MINSTREL_CCK_GROUP;
1082     else if (mi->supported[MINSTREL_OFDM_GROUP])
1083         group = MINSTREL_OFDM_GROUP;
1084     else
1085         group = 0;
1086 
1087     index = MI_RATE(group, 0);
1088     for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
1089         tmp_legacy_tp_rate[j] = index;
1090 
1091     if (mi->supported[MINSTREL_VHT_GROUP_0])
1092         group = MINSTREL_VHT_GROUP_0;
1093     else if (ht_supported)
1094         group = MINSTREL_HT_GROUP_0;
1095     else if (mi->supported[MINSTREL_CCK_GROUP])
1096         group = MINSTREL_CCK_GROUP;
1097     else
1098         group = MINSTREL_OFDM_GROUP;
1099 
1100     index = MI_RATE(group, 0);
1101     tmp_max_prob_rate = index;
1102     for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++)
1103         tmp_mcs_tp_rate[j] = index;
1104 
1105     /* Find best rate sets within all MCS groups*/
1106     for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
1107         u16 *tp_rate = tmp_mcs_tp_rate;
1108         u16 last_prob = 0;
1109 
1110         mg = &mi->groups[group];
1111         if (!mi->supported[group])
1112             continue;
1113 
1114         /* (re)Initialize group rate indexes */
1115         for(j = 0; j < MAX_THR_RATES; j++)
1116             tmp_group_tp_rate[j] = MI_RATE(group, 0);
1117 
1118         if (group == MINSTREL_CCK_GROUP && ht_supported)
1119             tp_rate = tmp_legacy_tp_rate;
1120 
1121         for (i = MCS_GROUP_RATES - 1; i >= 0; i--) {
1122             if (!(mi->supported[group] & BIT(i)))
1123                 continue;
1124 
1125             index = MI_RATE(group, i);
1126 
1127             mrs = &mg->rates[i];
1128             mrs->retry_updated = false;
1129             minstrel_ht_calc_rate_stats(mp, mrs);
1130 
1131             if (mrs->att_hist)
1132                 last_prob = max(last_prob, mrs->prob_avg);
1133             else
1134                 mrs->prob_avg = max(last_prob, mrs->prob_avg);
1135             cur_prob = mrs->prob_avg;
1136 
1137             if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
1138                 continue;
1139 
1140             /* Find max throughput rate set */
1141             minstrel_ht_sort_best_tp_rates(mi, index, tp_rate);
1142 
1143             /* Find max throughput rate set within a group */
1144             minstrel_ht_sort_best_tp_rates(mi, index,
1145                                tmp_group_tp_rate);
1146         }
1147 
1148         memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
1149                sizeof(mg->max_group_tp_rate));
1150     }
1151 
1152     /* Assign new rate set per sta */
1153     minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate,
1154                      tmp_legacy_tp_rate);
1155     memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
1156 
1157     for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
1158         if (!mi->supported[group])
1159             continue;
1160 
1161         mg = &mi->groups[group];
1162         mg->max_group_prob_rate = MI_RATE(group, 0);
1163 
1164         for (i = 0; i < MCS_GROUP_RATES; i++) {
1165             if (!(mi->supported[group] & BIT(i)))
1166                 continue;
1167 
1168             index = MI_RATE(group, i);
1169 
1170             /* Find max probability rate per group and global */
1171             minstrel_ht_set_best_prob_rate(mi, &tmp_max_prob_rate,
1172                                index);
1173         }
1174     }
1175 
1176     mi->max_prob_rate = tmp_max_prob_rate;
1177 
1178     /* Try to increase robustness of max_prob_rate*/
1179     minstrel_ht_prob_rate_reduce_streams(mi);
1180     minstrel_ht_refill_sample_rates(mi);
1181 
1182 #ifdef CONFIG_MAC80211_DEBUGFS
1183     /* use fixed index if set */
1184     if (mp->fixed_rate_idx != -1) {
1185         for (i = 0; i < 4; i++)
1186             mi->max_tp_rate[i] = mp->fixed_rate_idx;
1187         mi->max_prob_rate = mp->fixed_rate_idx;
1188     }
1189 #endif
1190 
1191     /* Reset update timer */
1192     mi->last_stats_update = jiffies;
1193     mi->sample_time = jiffies;
1194 }
1195 
1196 static bool
1197 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1198              struct ieee80211_tx_rate *rate)
1199 {
1200     int i;
1201 
1202     if (rate->idx < 0)
1203         return false;
1204 
1205     if (!rate->count)
1206         return false;
1207 
1208     if (rate->flags & IEEE80211_TX_RC_MCS ||
1209         rate->flags & IEEE80211_TX_RC_VHT_MCS)
1210         return true;
1211 
1212     for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++)
1213         if (rate->idx == mp->cck_rates[i])
1214             return true;
1215 
1216     for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++)
1217         if (rate->idx == mp->ofdm_rates[mi->band][i])
1218             return true;
1219 
1220     return false;
1221 }
1222 
1223 /*
1224  * Check whether rate_status contains valid information.
1225  */
1226 static bool
1227 minstrel_ht_ri_txstat_valid(struct minstrel_priv *mp,
1228                 struct minstrel_ht_sta *mi,
1229                 struct ieee80211_rate_status *rate_status)
1230 {
1231     int i;
1232 
1233     if (!rate_status)
1234         return false;
1235     if (!rate_status->try_count)
1236         return false;
1237 
1238     if (rate_status->rate_idx.flags & RATE_INFO_FLAGS_MCS ||
1239         rate_status->rate_idx.flags & RATE_INFO_FLAGS_VHT_MCS)
1240         return true;
1241 
1242     for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++) {
1243         if (rate_status->rate_idx.legacy ==
1244             minstrel_cck_bitrates[ mp->cck_rates[i] ])
1245             return true;
1246     }
1247 
1248     for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates); i++) {
1249         if (rate_status->rate_idx.legacy ==
1250             minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][i] ])
1251             return true;
1252     }
1253 
1254     return false;
1255 }
1256 
1257 static void
1258 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
1259 {
1260     int group, orig_group;
1261 
1262     orig_group = group = MI_RATE_GROUP(*idx);
1263     while (group > 0) {
1264         group--;
1265 
1266         if (!mi->supported[group])
1267             continue;
1268 
1269         if (minstrel_mcs_groups[group].streams >
1270             minstrel_mcs_groups[orig_group].streams)
1271             continue;
1272 
1273         if (primary)
1274             *idx = mi->groups[group].max_group_tp_rate[0];
1275         else
1276             *idx = mi->groups[group].max_group_tp_rate[1];
1277         break;
1278     }
1279 }
1280 
1281 static void
1282 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
1283                       void *priv_sta, struct ieee80211_tx_status *st)
1284 {
1285     struct ieee80211_tx_info *info = st->info;
1286     struct minstrel_ht_sta *mi = priv_sta;
1287     struct ieee80211_tx_rate *ar = info->status.rates;
1288     struct minstrel_rate_stats *rate, *rate2;
1289     struct minstrel_priv *mp = priv;
1290     u32 update_interval = mp->update_interval;
1291     bool last, update = false;
1292     int i;
1293 
1294     /* Ignore packet that was sent with noAck flag */
1295     if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1296         return;
1297 
1298     /* This packet was aggregated but doesn't carry status info */
1299     if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1300         !(info->flags & IEEE80211_TX_STAT_AMPDU))
1301         return;
1302 
1303     if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
1304         info->status.ampdu_ack_len =
1305             (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
1306         info->status.ampdu_len = 1;
1307     }
1308 
1309     /* wraparound */
1310     if (mi->total_packets >= ~0 - info->status.ampdu_len) {
1311         mi->total_packets = 0;
1312         mi->sample_packets = 0;
1313     }
1314 
1315     mi->total_packets += info->status.ampdu_len;
1316     if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
1317         mi->sample_packets += info->status.ampdu_len;
1318 
1319     mi->ampdu_packets++;
1320     mi->ampdu_len += info->status.ampdu_len;
1321 
1322     if (st->rates && st->n_rates) {
1323         last = !minstrel_ht_ri_txstat_valid(mp, mi, &(st->rates[0]));
1324         for (i = 0; !last; i++) {
1325             last = (i == st->n_rates - 1) ||
1326                 !minstrel_ht_ri_txstat_valid(mp, mi,
1327                             &(st->rates[i + 1]));
1328 
1329             rate = minstrel_ht_ri_get_stats(mp, mi,
1330                             &(st->rates[i]));
1331 
1332             if (last)
1333                 rate->success += info->status.ampdu_ack_len;
1334 
1335             rate->attempts += st->rates[i].try_count *
1336                       info->status.ampdu_len;
1337         }
1338     } else {
1339         last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]);
1340         for (i = 0; !last; i++) {
1341             last = (i == IEEE80211_TX_MAX_RATES - 1) ||
1342                 !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]);
1343 
1344             rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
1345             if (last)
1346                 rate->success += info->status.ampdu_ack_len;
1347 
1348             rate->attempts += ar[i].count * info->status.ampdu_len;
1349         }
1350     }
1351 
1352     if (mp->hw->max_rates > 1) {
1353         /*
1354          * check for sudden death of spatial multiplexing,
1355          * downgrade to a lower number of streams if necessary.
1356          */
1357         rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
1358         if (rate->attempts > 30 &&
1359             rate->success < rate->attempts / 4) {
1360             minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
1361             update = true;
1362         }
1363 
1364         rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
1365         if (rate2->attempts > 30 &&
1366             rate2->success < rate2->attempts / 4) {
1367             minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
1368             update = true;
1369         }
1370     }
1371 
1372     if (time_after(jiffies, mi->last_stats_update + update_interval)) {
1373         update = true;
1374         minstrel_ht_update_stats(mp, mi);
1375     }
1376 
1377     if (update)
1378         minstrel_ht_update_rates(mp, mi);
1379 }
1380 
1381 static void
1382 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1383                          int index)
1384 {
1385     struct minstrel_rate_stats *mrs;
1386     unsigned int tx_time, tx_time_rtscts, tx_time_data;
1387     unsigned int cw = mp->cw_min;
1388     unsigned int ctime = 0;
1389     unsigned int t_slot = 9; /* FIXME */
1390     unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
1391     unsigned int overhead = 0, overhead_rtscts = 0;
1392 
1393     mrs = minstrel_get_ratestats(mi, index);
1394     if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) {
1395         mrs->retry_count = 1;
1396         mrs->retry_count_rtscts = 1;
1397         return;
1398     }
1399 
1400     mrs->retry_count = 2;
1401     mrs->retry_count_rtscts = 2;
1402     mrs->retry_updated = true;
1403 
1404     tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
1405 
1406     /* Contention time for first 2 tries */
1407     ctime = (t_slot * cw) >> 1;
1408     cw = min((cw << 1) | 1, mp->cw_max);
1409     ctime += (t_slot * cw) >> 1;
1410     cw = min((cw << 1) | 1, mp->cw_max);
1411 
1412     if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index))) {
1413         overhead = mi->overhead_legacy;
1414         overhead_rtscts = mi->overhead_legacy_rtscts;
1415     } else {
1416         overhead = mi->overhead;
1417         overhead_rtscts = mi->overhead_rtscts;
1418     }
1419 
1420     /* Total TX time for data and Contention after first 2 tries */
1421     tx_time = ctime + 2 * (overhead + tx_time_data);
1422     tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
1423 
1424     /* See how many more tries we can fit inside segment size */
1425     do {
1426         /* Contention time for this try */
1427         ctime = (t_slot * cw) >> 1;
1428         cw = min((cw << 1) | 1, mp->cw_max);
1429 
1430         /* Total TX time after this try */
1431         tx_time += ctime + overhead + tx_time_data;
1432         tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
1433 
1434         if (tx_time_rtscts < mp->segment_size)
1435             mrs->retry_count_rtscts++;
1436     } while ((tx_time < mp->segment_size) &&
1437              (++mrs->retry_count < mp->max_retry));
1438 }
1439 
1440 
1441 static void
1442 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1443                      struct ieee80211_sta_rates *ratetbl, int offset, int index)
1444 {
1445     int group_idx = MI_RATE_GROUP(index);
1446     const struct mcs_group *group = &minstrel_mcs_groups[group_idx];
1447     struct minstrel_rate_stats *mrs;
1448     u8 idx;
1449     u16 flags = group->flags;
1450 
1451     mrs = minstrel_get_ratestats(mi, index);
1452     if (!mrs->retry_updated)
1453         minstrel_calc_retransmit(mp, mi, index);
1454 
1455     if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
1456         ratetbl->rate[offset].count = 2;
1457         ratetbl->rate[offset].count_rts = 2;
1458         ratetbl->rate[offset].count_cts = 2;
1459     } else {
1460         ratetbl->rate[offset].count = mrs->retry_count;
1461         ratetbl->rate[offset].count_cts = mrs->retry_count;
1462         ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
1463     }
1464 
1465     index = MI_RATE_IDX(index);
1466     if (group_idx == MINSTREL_CCK_GROUP)
1467         idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
1468     else if (group_idx == MINSTREL_OFDM_GROUP)
1469         idx = mp->ofdm_rates[mi->band][index %
1470                            ARRAY_SIZE(mp->ofdm_rates[0])];
1471     else if (flags & IEEE80211_TX_RC_VHT_MCS)
1472         idx = ((group->streams - 1) << 4) |
1473               (index & 0xF);
1474     else
1475         idx = index + (group->streams - 1) * 8;
1476 
1477     /* enable RTS/CTS if needed:
1478      *  - if station is in dynamic SMPS (and streams > 1)
1479      *  - for fallback rates, to increase chances of getting through
1480      */
1481     if (offset > 0 ||
1482         (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
1483          group->streams > 1)) {
1484         ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
1485         flags |= IEEE80211_TX_RC_USE_RTS_CTS;
1486     }
1487 
1488     ratetbl->rate[offset].idx = idx;
1489     ratetbl->rate[offset].flags = flags;
1490 }
1491 
1492 static inline int
1493 minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate)
1494 {
1495     int group = MI_RATE_GROUP(rate);
1496     rate = MI_RATE_IDX(rate);
1497     return mi->groups[group].rates[rate].prob_avg;
1498 }
1499 
1500 static int
1501 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
1502 {
1503     int group = MI_RATE_GROUP(mi->max_prob_rate);
1504     const struct mcs_group *g = &minstrel_mcs_groups[group];
1505     int rate = MI_RATE_IDX(mi->max_prob_rate);
1506     unsigned int duration;
1507 
1508     /* Disable A-MSDU if max_prob_rate is bad */
1509     if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100))
1510         return 1;
1511 
1512     duration = g->duration[rate];
1513     duration <<= g->shift;
1514 
1515     /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
1516     if (duration > MCS_DURATION(1, 0, 52))
1517         return 500;
1518 
1519     /*
1520      * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
1521      * data packet size
1522      */
1523     if (duration > MCS_DURATION(1, 0, 104))
1524         return 1600;
1525 
1526     /*
1527      * If the rate is slower than single-stream MCS7, or if the max throughput
1528      * rate success probability is less than 75%, limit A-MSDU to twice the usual
1529      * data packet size
1530      */
1531     if (duration > MCS_DURATION(1, 0, 260) ||
1532         (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) <
1533          MINSTREL_FRAC(75, 100)))
1534         return 3200;
1535 
1536     /*
1537      * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
1538      * Since aggregation sessions are started/stopped without txq flush, use
1539      * the limit here to avoid the complexity of having to de-aggregate
1540      * packets in the queue.
1541      */
1542     if (!mi->sta->deflink.vht_cap.vht_supported)
1543         return IEEE80211_MAX_MPDU_LEN_HT_BA;
1544 
1545     /* unlimited */
1546     return 0;
1547 }
1548 
1549 static void
1550 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1551 {
1552     struct ieee80211_sta_rates *rates;
1553     int i = 0;
1554     int max_rates = min_t(int, mp->hw->max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
1555 
1556     rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
1557     if (!rates)
1558         return;
1559 
1560     /* Start with max_tp_rate[0] */
1561     minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
1562 
1563     /* Fill up remaining, keep one entry for max_probe_rate */
1564     for (; i < (max_rates - 1); i++)
1565         minstrel_ht_set_rate(mp, mi, rates, i, mi->max_tp_rate[i]);
1566 
1567     if (i < max_rates)
1568         minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
1569 
1570     if (i < IEEE80211_TX_RATE_TABLE_SIZE)
1571         rates->rate[i].idx = -1;
1572 
1573     mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
1574     rate_control_set_rates(mp->hw, mi->sta, rates);
1575 }
1576 
1577 static u16
1578 minstrel_ht_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
1579 {
1580     u8 seq;
1581 
1582     if (mp->hw->max_rates > 1) {
1583         seq = mi->sample_seq;
1584         mi->sample_seq = (seq + 1) % ARRAY_SIZE(minstrel_sample_seq);
1585         seq = minstrel_sample_seq[seq];
1586     } else {
1587         seq = MINSTREL_SAMPLE_TYPE_INC;
1588     }
1589 
1590     return __minstrel_ht_get_sample_rate(mi, seq);
1591 }
1592 
1593 static void
1594 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1595                      struct ieee80211_tx_rate_control *txrc)
1596 {
1597     const struct mcs_group *sample_group;
1598     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1599     struct ieee80211_tx_rate *rate = &info->status.rates[0];
1600     struct minstrel_ht_sta *mi = priv_sta;
1601     struct minstrel_priv *mp = priv;
1602     u16 sample_idx;
1603 
1604     info->flags |= mi->tx_flags;
1605 
1606 #ifdef CONFIG_MAC80211_DEBUGFS
1607     if (mp->fixed_rate_idx != -1)
1608         return;
1609 #endif
1610 
1611     /* Don't use EAPOL frames for sampling on non-mrr hw */
1612     if (mp->hw->max_rates == 1 &&
1613         (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
1614         return;
1615 
1616     if (time_is_after_jiffies(mi->sample_time))
1617         return;
1618 
1619     mi->sample_time = jiffies + MINSTREL_SAMPLE_INTERVAL;
1620     sample_idx = minstrel_ht_get_sample_rate(mp, mi);
1621     if (!sample_idx)
1622         return;
1623 
1624     sample_group = &minstrel_mcs_groups[MI_RATE_GROUP(sample_idx)];
1625     sample_idx = MI_RATE_IDX(sample_idx);
1626 
1627     if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
1628         (sample_idx >= 4) != txrc->short_preamble)
1629         return;
1630 
1631     info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1632     rate->count = 1;
1633 
1634     if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
1635         int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
1636         rate->idx = mp->cck_rates[idx];
1637     } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) {
1638         int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]);
1639         rate->idx = mp->ofdm_rates[mi->band][idx];
1640     } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
1641         ieee80211_rate_set_vht(rate, MI_RATE_IDX(sample_idx),
1642                        sample_group->streams);
1643     } else {
1644         rate->idx = sample_idx + (sample_group->streams - 1) * 8;
1645     }
1646 
1647     rate->flags = sample_group->flags;
1648 }
1649 
1650 static void
1651 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1652                struct ieee80211_supported_band *sband,
1653                struct ieee80211_sta *sta)
1654 {
1655     int i;
1656 
1657     if (sband->band != NL80211_BAND_2GHZ)
1658         return;
1659 
1660     if (sta->deflink.ht_cap.ht_supported &&
1661         !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
1662         return;
1663 
1664     for (i = 0; i < 4; i++) {
1665         if (mp->cck_rates[i] == 0xff ||
1666             !rate_supported(sta, sband->band, mp->cck_rates[i]))
1667             continue;
1668 
1669         mi->supported[MINSTREL_CCK_GROUP] |= BIT(i);
1670         if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1671             mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4);
1672     }
1673 }
1674 
1675 static void
1676 minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
1677             struct ieee80211_supported_band *sband,
1678             struct ieee80211_sta *sta)
1679 {
1680     const u8 *rates;
1681     int i;
1682 
1683     if (sta->deflink.ht_cap.ht_supported)
1684         return;
1685 
1686     rates = mp->ofdm_rates[sband->band];
1687     for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) {
1688         if (rates[i] == 0xff ||
1689             !rate_supported(sta, sband->band, rates[i]))
1690             continue;
1691 
1692         mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i);
1693     }
1694 }
1695 
1696 static void
1697 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1698             struct cfg80211_chan_def *chandef,
1699             struct ieee80211_sta *sta, void *priv_sta)
1700 {
1701     struct minstrel_priv *mp = priv;
1702     struct minstrel_ht_sta *mi = priv_sta;
1703     struct ieee80211_mcs_info *mcs = &sta->deflink.ht_cap.mcs;
1704     u16 ht_cap = sta->deflink.ht_cap.cap;
1705     struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
1706     const struct ieee80211_rate *ctl_rate;
1707     struct sta_info *sta_info;
1708     bool ldpc, erp;
1709     int use_vht;
1710     int n_supported = 0;
1711     int ack_dur;
1712     int stbc;
1713     int i;
1714 
1715     BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
1716 
1717     if (vht_cap->vht_supported)
1718         use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
1719     else
1720         use_vht = 0;
1721 
1722     memset(mi, 0, sizeof(*mi));
1723 
1724     mi->sta = sta;
1725     mi->band = sband->band;
1726     mi->last_stats_update = jiffies;
1727 
1728     ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1729     mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
1730     mi->overhead += ack_dur;
1731     mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
1732 
1733     ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)];
1734     erp = ctl_rate->flags & IEEE80211_RATE_ERP_G;
1735     ack_dur = ieee80211_frame_duration(sband->band, 10,
1736                        ctl_rate->bitrate, erp, 1,
1737                        ieee80211_chandef_get_shift(chandef));
1738     mi->overhead_legacy = ack_dur;
1739     mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur;
1740 
1741     mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
1742 
1743     if (!use_vht) {
1744         stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
1745             IEEE80211_HT_CAP_RX_STBC_SHIFT;
1746 
1747         ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
1748     } else {
1749         stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
1750             IEEE80211_VHT_CAP_RXSTBC_SHIFT;
1751 
1752         ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
1753     }
1754 
1755     mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
1756     if (ldpc)
1757         mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
1758 
1759     for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
1760         u32 gflags = minstrel_mcs_groups[i].flags;
1761         int bw, nss;
1762 
1763         mi->supported[i] = 0;
1764         if (minstrel_ht_is_legacy_group(i))
1765             continue;
1766 
1767         if (gflags & IEEE80211_TX_RC_SHORT_GI) {
1768             if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1769                 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
1770                     continue;
1771             } else {
1772                 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
1773                     continue;
1774             }
1775         }
1776 
1777         if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
1778             sta->deflink.bandwidth < IEEE80211_STA_RX_BW_40)
1779             continue;
1780 
1781         nss = minstrel_mcs_groups[i].streams;
1782 
1783         /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
1784         if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
1785             continue;
1786 
1787         /* HT rate */
1788         if (gflags & IEEE80211_TX_RC_MCS) {
1789             if (use_vht && minstrel_vht_only)
1790                 continue;
1791 
1792             mi->supported[i] = mcs->rx_mask[nss - 1];
1793             if (mi->supported[i])
1794                 n_supported++;
1795             continue;
1796         }
1797 
1798         /* VHT rate */
1799         if (!vht_cap->vht_supported ||
1800             WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
1801             WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
1802             continue;
1803 
1804         if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
1805             if (sta->deflink.bandwidth < IEEE80211_STA_RX_BW_80 ||
1806                 ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
1807                  !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
1808                 continue;
1809             }
1810         }
1811 
1812         if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1813             bw = BW_40;
1814         else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1815             bw = BW_80;
1816         else
1817             bw = BW_20;
1818 
1819         mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
1820                 vht_cap->vht_mcs.tx_mcs_map);
1821 
1822         if (mi->supported[i])
1823             n_supported++;
1824     }
1825 
1826     sta_info = container_of(sta, struct sta_info, sta);
1827     mi->use_short_preamble = test_sta_flag(sta_info, WLAN_STA_SHORT_PREAMBLE) &&
1828                  sta_info->sdata->vif.bss_conf.use_short_preamble;
1829 
1830     minstrel_ht_update_cck(mp, mi, sband, sta);
1831     minstrel_ht_update_ofdm(mp, mi, sband, sta);
1832 
1833     /* create an initial rate table with the lowest supported rates */
1834     minstrel_ht_update_stats(mp, mi);
1835     minstrel_ht_update_rates(mp, mi);
1836 }
1837 
1838 static void
1839 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
1840               struct cfg80211_chan_def *chandef,
1841                       struct ieee80211_sta *sta, void *priv_sta)
1842 {
1843     minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1844 }
1845 
1846 static void
1847 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
1848             struct cfg80211_chan_def *chandef,
1849                         struct ieee80211_sta *sta, void *priv_sta,
1850                         u32 changed)
1851 {
1852     minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
1853 }
1854 
1855 static void *
1856 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1857 {
1858     struct ieee80211_supported_band *sband;
1859     struct minstrel_ht_sta *mi;
1860     struct minstrel_priv *mp = priv;
1861     struct ieee80211_hw *hw = mp->hw;
1862     int max_rates = 0;
1863     int i;
1864 
1865     for (i = 0; i < NUM_NL80211_BANDS; i++) {
1866         sband = hw->wiphy->bands[i];
1867         if (sband && sband->n_bitrates > max_rates)
1868             max_rates = sband->n_bitrates;
1869     }
1870 
1871     return kzalloc(sizeof(*mi), gfp);
1872 }
1873 
1874 static void
1875 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1876 {
1877     kfree(priv_sta);
1878 }
1879 
1880 static void
1881 minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband,
1882                 const s16 *bitrates, int n_rates, u32 rate_flags)
1883 {
1884     int i, j;
1885 
1886     for (i = 0; i < sband->n_bitrates; i++) {
1887         struct ieee80211_rate *rate = &sband->bitrates[i];
1888 
1889         if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1890             continue;
1891 
1892         for (j = 0; j < n_rates; j++) {
1893             if (rate->bitrate != bitrates[j])
1894                 continue;
1895 
1896             dest[j] = i;
1897             break;
1898         }
1899     }
1900 }
1901 
1902 static void
1903 minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
1904 {
1905     static const s16 bitrates[4] = { 10, 20, 55, 110 };
1906     struct ieee80211_supported_band *sband;
1907     u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1908 
1909     memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates));
1910     sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
1911     if (!sband)
1912         return;
1913 
1914     BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates));
1915     minstrel_ht_fill_rate_array(mp->cck_rates, sband,
1916                     minstrel_cck_bitrates,
1917                     ARRAY_SIZE(minstrel_cck_bitrates),
1918                     rate_flags);
1919 }
1920 
1921 static void
1922 minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band)
1923 {
1924     static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
1925     struct ieee80211_supported_band *sband;
1926     u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
1927 
1928     memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band]));
1929     sband = mp->hw->wiphy->bands[band];
1930     if (!sband)
1931         return;
1932 
1933     BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates));
1934     minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband,
1935                     minstrel_ofdm_bitrates,
1936                     ARRAY_SIZE(minstrel_ofdm_bitrates),
1937                     rate_flags);
1938 }
1939 
1940 static void *
1941 minstrel_ht_alloc(struct ieee80211_hw *hw)
1942 {
1943     struct minstrel_priv *mp;
1944     int i;
1945 
1946     mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
1947     if (!mp)
1948         return NULL;
1949 
1950     /* contention window settings
1951      * Just an approximation. Using the per-queue values would complicate
1952      * the calculations and is probably unnecessary */
1953     mp->cw_min = 15;
1954     mp->cw_max = 1023;
1955 
1956     /* maximum time that the hw is allowed to stay in one MRR segment */
1957     mp->segment_size = 6000;
1958 
1959     if (hw->max_rate_tries > 0)
1960         mp->max_retry = hw->max_rate_tries;
1961     else
1962         /* safe default, does not necessarily have to match hw properties */
1963         mp->max_retry = 7;
1964 
1965     if (hw->max_rates >= 4)
1966         mp->has_mrr = true;
1967 
1968     mp->hw = hw;
1969     mp->update_interval = HZ / 20;
1970 
1971     minstrel_ht_init_cck_rates(mp);
1972     for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++)
1973         minstrel_ht_init_ofdm_rates(mp, i);
1974 
1975     return mp;
1976 }
1977 
1978 #ifdef CONFIG_MAC80211_DEBUGFS
1979 static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv,
1980                     struct dentry *debugfsdir)
1981 {
1982     struct minstrel_priv *mp = priv;
1983 
1984     mp->fixed_rate_idx = (u32) -1;
1985     debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
1986                &mp->fixed_rate_idx);
1987 }
1988 #endif
1989 
1990 static void
1991 minstrel_ht_free(void *priv)
1992 {
1993     kfree(priv);
1994 }
1995 
1996 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1997 {
1998     struct minstrel_ht_sta *mi = priv_sta;
1999     int i, j, prob, tp_avg;
2000 
2001     i = MI_RATE_GROUP(mi->max_tp_rate[0]);
2002     j = MI_RATE_IDX(mi->max_tp_rate[0]);
2003     prob = mi->groups[i].rates[j].prob_avg;
2004 
2005     /* convert tp_avg from pkt per second in kbps */
2006     tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
2007     tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
2008 
2009     return tp_avg;
2010 }
2011 
2012 static const struct rate_control_ops mac80211_minstrel_ht = {
2013     .name = "minstrel_ht",
2014     .capa = RATE_CTRL_CAPA_AMPDU_TRIGGER,
2015     .tx_status_ext = minstrel_ht_tx_status,
2016     .get_rate = minstrel_ht_get_rate,
2017     .rate_init = minstrel_ht_rate_init,
2018     .rate_update = minstrel_ht_rate_update,
2019     .alloc_sta = minstrel_ht_alloc_sta,
2020     .free_sta = minstrel_ht_free_sta,
2021     .alloc = minstrel_ht_alloc,
2022     .free = minstrel_ht_free,
2023 #ifdef CONFIG_MAC80211_DEBUGFS
2024     .add_debugfs = minstrel_ht_add_debugfs,
2025     .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
2026 #endif
2027     .get_expected_throughput = minstrel_ht_get_expected_throughput,
2028 };
2029 
2030 
2031 static void __init init_sample_table(void)
2032 {
2033     int col, i, new_idx;
2034     u8 rnd[MCS_GROUP_RATES];
2035 
2036     memset(sample_table, 0xff, sizeof(sample_table));
2037     for (col = 0; col < SAMPLE_COLUMNS; col++) {
2038         prandom_bytes(rnd, sizeof(rnd));
2039         for (i = 0; i < MCS_GROUP_RATES; i++) {
2040             new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
2041             while (sample_table[col][new_idx] != 0xff)
2042                 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
2043 
2044             sample_table[col][new_idx] = i;
2045         }
2046     }
2047 }
2048 
2049 int __init
2050 rc80211_minstrel_init(void)
2051 {
2052     init_sample_table();
2053     return ieee80211_rate_control_register(&mac80211_minstrel_ht);
2054 }
2055 
2056 void
2057 rc80211_minstrel_exit(void)
2058 {
2059     ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
2060 }