rc80211_minstrel_ht.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 2 as
00006  * published by the Free Software Foundation.
00007  */
00008 #include <linux/netdevice.h>
00009 #include <linux/types.h>
00010 #include <linux/skbuff.h>
00011 #include <linux/debugfs.h>
00012 #include <linux/random.h>
00013 #include <linux/ieee80211.h>
00014 #include <net/mac80211.h>
00015 #include "rate.h"
00016 #include "rc80211_minstrel.h"
00017 #include "rc80211_minstrel_ht.h"
00018 
00019 #define AVG_PKT_SIZE    1200
00020 #define SAMPLE_COLUMNS  10
00021 #define EWMA_LEVEL              75
00022 
00023 /* Number of bits for an average sized packet */
00024 #define MCS_NBITS (AVG_PKT_SIZE << 3)
00025 
00026 /* Number of symbols for a packet with (bps) bits per symbol */
00027 #define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
00028 
00029 /* Transmission time for a packet containing (syms) symbols */
00030 #define MCS_SYMBOL_TIME(sgi, syms)                                      \
00031         (sgi ?                                                          \
00032           ((syms) * 18 + 4) / 5 :       /* syms * 3.6 us */             \
00033           (syms) << 2                   /* syms * 4 us */               \
00034         )
00035 
00036 /* Transmit duration for the raw data part of an average sized packet */
00037 #define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
00038 
00039 /*
00040  * Define group sort order: HT40 -> SGI -> #streams
00041  */
00042 #define GROUP_IDX(_streams, _sgi, _ht40)        \
00043         MINSTREL_MAX_STREAMS * 2 * _ht40 +      \
00044         MINSTREL_MAX_STREAMS * _sgi +           \
00045         _streams - 1
00046 
00047 /* MCS rate information for an MCS group */
00048 #define MCS_GROUP(_streams, _sgi, _ht40)                                \
00049         [GROUP_IDX(_streams, _sgi, _ht40)] = {                          \
00050         .streams = _streams,                                            \
00051         .flags =                                                        \
00052                 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) |                 \
00053                 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0),             \
00054         .duration = {                                                   \
00055                 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26),          \
00056                 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52),         \
00057                 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78),         \
00058                 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104),        \
00059                 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156),        \
00060                 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208),        \
00061                 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234),        \
00062                 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260)         \
00063         }                                                               \
00064 }
00065 
00066 /*
00067  * To enable sufficiently targeted rate sampling, MCS rates are divided into
00068  * groups, based on the number of streams and flags (HT40, SGI) that they
00069  * use.
00070  *
00071  * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
00072  * HT40 -> SGI -> #streams
00073  */
00074 const struct mcs_group minstrel_mcs_groups[] = {
00075         MCS_GROUP(1, 0, 0),
00076         MCS_GROUP(2, 0, 0),
00077 #if MINSTREL_MAX_STREAMS >= 3
00078         MCS_GROUP(3, 0, 0),
00079 #endif
00080 
00081         MCS_GROUP(1, 1, 0),
00082         MCS_GROUP(2, 1, 0),
00083 #if MINSTREL_MAX_STREAMS >= 3
00084         MCS_GROUP(3, 1, 0),
00085 #endif
00086 
00087         MCS_GROUP(1, 0, 1),
00088         MCS_GROUP(2, 0, 1),
00089 #if MINSTREL_MAX_STREAMS >= 3
00090         MCS_GROUP(3, 0, 1),
00091 #endif
00092 
00093         MCS_GROUP(1, 1, 1),
00094         MCS_GROUP(2, 1, 1),
00095 #if MINSTREL_MAX_STREAMS >= 3
00096         MCS_GROUP(3, 1, 1),
00097 #endif
00098 };
00099 
00100 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
00101 
00102 /*
00103  * Perform EWMA (Exponentially Weighted Moving Average) calculation
00104  */
00105 static int
00106 minstrel_ewma(int old, int new, int weight)
00107 {
00108         return (new * (100 - weight) + old * weight) / 100;
00109 }
00110 
00111 /*
00112  * Look up an MCS group index based on mac80211 rate information
00113  */
00114 static int
00115 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
00116 {
00117         return GROUP_IDX((rate->idx / MCS_GROUP_RATES) + 1,
00118                          !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
00119                          !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
00120 }
00121 
00122 static inline struct minstrel_rate_stats *
00123 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
00124 {
00125         return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
00126 }
00127 
00128 
00129 /*
00130  * Recalculate success probabilities and counters for a rate using EWMA
00131  */
00132 static void
00133 minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
00134 {
00135         if (unlikely(mr->attempts > 0)) {
00136                 mr->sample_skipped = 0;
00137                 mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
00138                 if (!mr->att_hist)
00139                         mr->probability = mr->cur_prob;
00140                 else
00141                         mr->probability = minstrel_ewma(mr->probability,
00142                                 mr->cur_prob, EWMA_LEVEL);
00143                 mr->att_hist += mr->attempts;
00144                 mr->succ_hist += mr->success;
00145         } else {
00146                 mr->sample_skipped++;
00147         }
00148         mr->last_success = mr->success;
00149         mr->last_attempts = mr->attempts;
00150         mr->success = 0;
00151         mr->attempts = 0;
00152 }
00153 
00154 /*
00155  * Calculate throughput based on the average A-MPDU length, taking into account
00156  * the expected number of retransmissions and their expected length
00157  */
00158 static void
00159 minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
00160 {
00161         struct minstrel_rate_stats *mr;
00162         unsigned int usecs;
00163 
00164         mr = &mi->groups[group].rates[rate];
00165 
00166         if (mr->probability < MINSTREL_FRAC(1, 10)) {
00167                 mr->cur_tp = 0;
00168                 return;
00169         }
00170 
00171         usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
00172         usecs += minstrel_mcs_groups[group].duration[rate];
00173         mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
00174 }
00175 
00176 /*
00177  * Update rate statistics and select new primary rates
00178  *
00179  * Rules for rate selection:
00180  *  - max_prob_rate must use only one stream, as a tradeoff between delivery
00181  *    probability and throughput during strong fluctuations
00182  *  - as long as the max prob rate has a probability of more than 3/4, pick
00183  *    higher throughput rates, even if the probablity is a bit lower
00184  */
00185 static void
00186 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
00187 {
00188         struct minstrel_mcs_group_data *mg;
00189         struct minstrel_rate_stats *mr;
00190         int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
00191         int group, i, index;
00192 
00193         if (mi->ampdu_packets > 0) {
00194                 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
00195                         MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
00196                 mi->ampdu_len = 0;
00197                 mi->ampdu_packets = 0;
00198         }
00199 
00200         mi->sample_slow = 0;
00201         mi->sample_count = 0;
00202         mi->max_tp_rate = 0;
00203         mi->max_tp_rate2 = 0;
00204         mi->max_prob_rate = 0;
00205 
00206         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
00207                 cur_prob = 0;
00208                 cur_prob_tp = 0;
00209                 cur_tp = 0;
00210                 cur_tp2 = 0;
00211 
00212                 mg = &mi->groups[group];
00213                 if (!mg->supported)
00214                         continue;
00215 
00216                 mg->max_tp_rate = 0;
00217                 mg->max_tp_rate2 = 0;
00218                 mg->max_prob_rate = 0;
00219                 mi->sample_count++;
00220 
00221                 for (i = 0; i < MCS_GROUP_RATES; i++) {
00222                         if (!(mg->supported & BIT(i)))
00223                                 continue;
00224 
00225                         mr = &mg->rates[i];
00226                         mr->retry_updated = false;
00227                         index = MCS_GROUP_RATES * group + i;
00228                         minstrel_calc_rate_ewma(mr);
00229                         minstrel_ht_calc_tp(mi, group, i);
00230 
00231                         if (!mr->cur_tp)
00232                                 continue;
00233 
00234                         /* ignore the lowest rate of each single-stream group */
00235                         if (!i && minstrel_mcs_groups[group].streams == 1)
00236                                 continue;
00237 
00238                         if ((mr->cur_tp > cur_prob_tp && mr->probability >
00239                              MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
00240                                 mg->max_prob_rate = index;
00241                                 cur_prob = mr->probability;
00242                                 cur_prob_tp = mr->cur_tp;
00243                         }
00244 
00245                         if (mr->cur_tp > cur_tp) {
00246                                 swap(index, mg->max_tp_rate);
00247                                 cur_tp = mr->cur_tp;
00248                                 mr = minstrel_get_ratestats(mi, index);
00249                         }
00250 
00251                         if (index >= mg->max_tp_rate)
00252                                 continue;
00253 
00254                         if (mr->cur_tp > cur_tp2) {
00255                                 mg->max_tp_rate2 = index;
00256                                 cur_tp2 = mr->cur_tp;
00257                         }
00258                 }
00259         }
00260 
00261         /* try to sample up to half of the available rates during each interval */
00262         mi->sample_count *= 4;
00263 
00264         cur_prob = 0;
00265         cur_prob_tp = 0;
00266         cur_tp = 0;
00267         cur_tp2 = 0;
00268         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
00269                 mg = &mi->groups[group];
00270                 if (!mg->supported)
00271                         continue;
00272 
00273                 mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
00274                 if (cur_prob_tp < mr->cur_tp &&
00275                     minstrel_mcs_groups[group].streams == 1) {
00276                         mi->max_prob_rate = mg->max_prob_rate;
00277                         cur_prob = mr->cur_prob;
00278                         cur_prob_tp = mr->cur_tp;
00279                 }
00280 
00281                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
00282                 if (cur_tp < mr->cur_tp) {
00283                         mi->max_tp_rate2 = mi->max_tp_rate;
00284                         cur_tp2 = cur_tp;
00285                         mi->max_tp_rate = mg->max_tp_rate;
00286                         cur_tp = mr->cur_tp;
00287                 }
00288 
00289                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
00290                 if (cur_tp2 < mr->cur_tp) {
00291                         mi->max_tp_rate2 = mg->max_tp_rate2;
00292                         cur_tp2 = mr->cur_tp;
00293                 }
00294         }
00295 
00296         mi->stats_update = jiffies;
00297 }
00298 
00299 static bool
00300 minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
00301 {
00302         if (rate->idx < 0)
00303                 return false;
00304 
00305         if (!rate->count)
00306                 return false;
00307 
00308         return !!(rate->flags & IEEE80211_TX_RC_MCS);
00309 }
00310 
00311 static void
00312 minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
00313 {
00314         struct minstrel_mcs_group_data *mg;
00315 
00316         for (;;) {
00317                 mi->sample_group++;
00318                 mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
00319                 mg = &mi->groups[mi->sample_group];
00320 
00321                 if (!mg->supported)
00322                         continue;
00323 
00324                 if (++mg->index >= MCS_GROUP_RATES) {
00325                         mg->index = 0;
00326                         if (++mg->column >= ARRAY_SIZE(sample_table))
00327                                 mg->column = 0;
00328                 }
00329                 break;
00330         }
00331 }
00332 
00333 static void
00334 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,
00335                         bool primary)
00336 {
00337         int group, orig_group;
00338 
00339         orig_group = group = *idx / MCS_GROUP_RATES;
00340         while (group > 0) {
00341                 group--;
00342 
00343                 if (!mi->groups[group].supported)
00344                         continue;
00345 
00346                 if (minstrel_mcs_groups[group].streams >
00347                     minstrel_mcs_groups[orig_group].streams)
00348                         continue;
00349 
00350                 if (primary)
00351                         *idx = mi->groups[group].max_tp_rate;
00352                 else
00353                         *idx = mi->groups[group].max_tp_rate2;
00354                 break;
00355         }
00356 }
00357 
00358 static void
00359 minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
00360 {
00361         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00362         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
00363         u16 tid;
00364 
00365         if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
00366                 return;
00367 
00368         if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
00369                 return;
00370 
00371         tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
00372         if (likely(sta->ampdu_mlme.tid_tx[tid]))
00373                 return;
00374 
00375         if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
00376                 return;
00377 
00378         ieee80211_start_tx_ba_session(pubsta, tid, 5000);
00379 }
00380 
00381 static void
00382 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
00383                       struct ieee80211_sta *sta, void *priv_sta,
00384                       struct sk_buff *skb)
00385 {
00386         struct minstrel_ht_sta_priv *msp = priv_sta;
00387         struct minstrel_ht_sta *mi = &msp->ht;
00388         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
00389         struct ieee80211_tx_rate *ar = info->status.rates;
00390         struct minstrel_rate_stats *rate, *rate2;
00391         struct minstrel_priv *mp = priv;
00392         bool last = false;
00393         int group;
00394         int i = 0;
00395 
00396         if (!msp->is_ht)
00397                 return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
00398 
00399         /* This packet was aggregated but doesn't carry status info */
00400         if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
00401             !(info->flags & IEEE80211_TX_STAT_AMPDU))
00402                 return;
00403 
00404         if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
00405                 info->status.ampdu_ack_len =
00406                         (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
00407                 info->status.ampdu_len = 1;
00408         }
00409 
00410         mi->ampdu_packets++;
00411         mi->ampdu_len += info->status.ampdu_len;
00412 
00413         if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
00414                 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
00415                 mi->sample_tries = 2;
00416                 mi->sample_count--;
00417         }
00418 
00419         if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
00420                 mi->sample_packets += info->status.ampdu_len;
00421 
00422         for (i = 0; !last; i++) {
00423                 last = (i == IEEE80211_TX_MAX_RATES - 1) ||
00424                        !minstrel_ht_txstat_valid(&ar[i + 1]);
00425 
00426                 if (!minstrel_ht_txstat_valid(&ar[i]))
00427                         break;
00428 
00429                 group = minstrel_ht_get_group_idx(&ar[i]);
00430                 rate = &mi->groups[group].rates[ar[i].idx % 8];
00431 
00432                 if (last)
00433                         rate->success += info->status.ampdu_ack_len;
00434 
00435                 rate->attempts += ar[i].count * info->status.ampdu_len;
00436         }
00437 
00438         /*
00439          * check for sudden death of spatial multiplexing,
00440          * downgrade to a lower number of streams if necessary.
00441          */
00442         rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
00443         if (rate->attempts > 30 &&
00444             MINSTREL_FRAC(rate->success, rate->attempts) <
00445             MINSTREL_FRAC(20, 100))
00446                 minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
00447 
00448         rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
00449         if (rate2->attempts > 30 &&
00450             MINSTREL_FRAC(rate2->success, rate2->attempts) <
00451             MINSTREL_FRAC(20, 100))
00452                 minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
00453 
00454         if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
00455                 minstrel_ht_update_stats(mp, mi);
00456                 if (!(info->flags & IEEE80211_TX_CTL_AMPDU))
00457                         minstrel_aggr_check(sta, skb);
00458         }
00459 }
00460 
00461 static void
00462 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
00463                          int index)
00464 {
00465         struct minstrel_rate_stats *mr;
00466         const struct mcs_group *group;
00467         unsigned int tx_time, tx_time_rtscts, tx_time_data;
00468         unsigned int cw = mp->cw_min;
00469         unsigned int ctime = 0;
00470         unsigned int t_slot = 9; /* FIXME */
00471         unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
00472 
00473         mr = minstrel_get_ratestats(mi, index);
00474         if (mr->probability < MINSTREL_FRAC(1, 10)) {
00475                 mr->retry_count = 1;
00476                 mr->retry_count_rtscts = 1;
00477                 return;
00478         }
00479 
00480         mr->retry_count = 2;
00481         mr->retry_count_rtscts = 2;
00482         mr->retry_updated = true;
00483 
00484         group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
00485         tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
00486 
00487         /* Contention time for first 2 tries */
00488         ctime = (t_slot * cw) >> 1;
00489         cw = min((cw << 1) | 1, mp->cw_max);
00490         ctime += (t_slot * cw) >> 1;
00491         cw = min((cw << 1) | 1, mp->cw_max);
00492 
00493         /* Total TX time for data and Contention after first 2 tries */
00494         tx_time = ctime + 2 * (mi->overhead + tx_time_data);
00495         tx_time_rtscts = ctime + 2 * (mi->overhead_rtscts + tx_time_data);
00496 
00497         /* See how many more tries we can fit inside segment size */
00498         do {
00499                 /* Contention time for this try */
00500                 ctime = (t_slot * cw) >> 1;
00501                 cw = min((cw << 1) | 1, mp->cw_max);
00502 
00503                 /* Total TX time after this try */
00504                 tx_time += ctime + mi->overhead + tx_time_data;
00505                 tx_time_rtscts += ctime + mi->overhead_rtscts + tx_time_data;
00506 
00507                 if (tx_time_rtscts < mp->segment_size)
00508                         mr->retry_count_rtscts++;
00509         } while ((tx_time < mp->segment_size) &&
00510                  (++mr->retry_count < mp->max_retry));
00511 }
00512 
00513 
00514 static void
00515 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
00516                      struct ieee80211_tx_rate *rate, int index,
00517                      bool sample, bool rtscts)
00518 {
00519         const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
00520         struct minstrel_rate_stats *mr;
00521 
00522         mr = minstrel_get_ratestats(mi, index);
00523         if (!mr->retry_updated)
00524                 minstrel_calc_retransmit(mp, mi, index);
00525 
00526         if (sample)
00527                 rate->count = 1;
00528         else if (mr->probability < MINSTREL_FRAC(20, 100))
00529                 rate->count = 2;
00530         else if (rtscts)
00531                 rate->count = mr->retry_count_rtscts;
00532         else
00533                 rate->count = mr->retry_count;
00534 
00535         rate->flags = IEEE80211_TX_RC_MCS | group->flags;
00536         if (rtscts)
00537                 rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
00538         rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
00539 }
00540 
00541 static inline int
00542 minstrel_get_duration(int index)
00543 {
00544         const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
00545         return group->duration[index % MCS_GROUP_RATES];
00546 }
00547 
00548 static int
00549 minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
00550 {
00551         struct minstrel_rate_stats *mr;
00552         struct minstrel_mcs_group_data *mg;
00553         int sample_idx = 0;
00554 
00555         if (mi->sample_wait > 0) {
00556                 mi->sample_wait--;
00557                 return -1;
00558         }
00559 
00560         if (!mi->sample_tries)
00561                 return -1;
00562 
00563         mi->sample_tries--;
00564         mg = &mi->groups[mi->sample_group];
00565         sample_idx = sample_table[mg->column][mg->index];
00566         mr = &mg->rates[sample_idx];
00567         sample_idx += mi->sample_group * MCS_GROUP_RATES;
00568         minstrel_next_sample_idx(mi);
00569 
00570         /*
00571          * Sampling might add some overhead (RTS, no aggregation)
00572          * to the frame. Hence, don't use sampling for the currently
00573          * used max TP rate.
00574          */
00575         if (sample_idx == mi->max_tp_rate)
00576                 return -1;
00577         /*
00578          * When not using MRR, do not sample if the probability is already
00579          * higher than 95% to avoid wasting airtime
00580          */
00581         if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
00582                 return -1;
00583 
00584         /*
00585          * Make sure that lower rates get sampled only occasionally,
00586          * if the link is working perfectly.
00587          */
00588         if (minstrel_get_duration(sample_idx) >
00589             minstrel_get_duration(mi->max_tp_rate)) {
00590                 if (mr->sample_skipped < 20)
00591                         return -1;
00592 
00593                 if (mi->sample_slow++ > 2)
00594                         return -1;
00595         }
00596 
00597         return sample_idx;
00598 }
00599 
00600 static void
00601 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
00602                      struct ieee80211_tx_rate_control *txrc)
00603 {
00604         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
00605         struct ieee80211_tx_rate *ar = info->status.rates;
00606         struct minstrel_ht_sta_priv *msp = priv_sta;
00607         struct minstrel_ht_sta *mi = &msp->ht;
00608         struct minstrel_priv *mp = priv;
00609         int sample_idx;
00610         bool sample = false;
00611 
00612         if (rate_control_send_low(sta, priv_sta, txrc))
00613                 return;
00614 
00615         if (!msp->is_ht)
00616                 return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
00617 
00618         info->flags |= mi->tx_flags;
00619 
00620         /* Don't use EAPOL frames for sampling on non-mrr hw */
00621         if (mp->hw->max_rates == 1 &&
00622             txrc->skb->protocol == cpu_to_be16(ETH_P_PAE))
00623                 sample_idx = -1;
00624         else
00625                 sample_idx = minstrel_get_sample_rate(mp, mi);
00626 
00627 #ifdef CONFIG_MAC80211_DEBUGFS
00628         /* use fixed index if set */
00629         if (mp->fixed_rate_idx != -1)
00630                 sample_idx = mp->fixed_rate_idx;
00631 #endif
00632 
00633         if (sample_idx >= 0) {
00634                 sample = true;
00635                 minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
00636                         true, false);
00637                 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
00638         } else {
00639                 minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
00640                         false, false);
00641         }
00642 
00643         if (mp->hw->max_rates >= 3) {
00644                 /*
00645                  * At least 3 tx rates supported, use
00646                  * sample_rate -> max_tp_rate -> max_prob_rate for sampling and
00647                  * max_tp_rate -> max_tp_rate2 -> max_prob_rate by default.
00648                  */
00649                 if (sample_idx >= 0)
00650                         minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
00651                                 false, false);
00652                 else
00653                         minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
00654                                 false, true);
00655 
00656                 minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate,
00657                                      false, !sample);
00658 
00659                 ar[3].count = 0;
00660                 ar[3].idx = -1;
00661         } else if (mp->hw->max_rates == 2) {
00662                 /*
00663                  * Only 2 tx rates supported, use
00664                  * sample_rate -> max_prob_rate for sampling and
00665                  * max_tp_rate -> max_prob_rate by default.
00666                  */
00667                 minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_prob_rate,
00668                                      false, !sample);
00669 
00670                 ar[2].count = 0;
00671                 ar[2].idx = -1;
00672         } else {
00673                 /* Not using MRR, only use the first rate */
00674                 ar[1].count = 0;
00675                 ar[1].idx = -1;
00676         }
00677 
00678         mi->total_packets++;
00679 
00680         /* wraparound */
00681         if (mi->total_packets == ~0) {
00682                 mi->total_packets = 0;
00683                 mi->sample_packets = 0;
00684         }
00685 }
00686 
00687 static void
00688 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
00689                         struct ieee80211_sta *sta, void *priv_sta)
00690 {
00691         struct minstrel_priv *mp = priv;
00692         struct minstrel_ht_sta_priv *msp = priv_sta;
00693         struct minstrel_ht_sta *mi = &msp->ht;
00694         struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
00695         u16 sta_cap = sta->ht_cap.cap;
00696         int n_supported = 0;
00697         int ack_dur;
00698         int stbc;
00699         int i;
00700         unsigned int smps;
00701 
00702         /* fall back to the old minstrel for legacy stations */
00703         if (!sta->ht_cap.ht_supported)
00704                 goto use_legacy;
00705 
00706         BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
00707                 MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
00708 
00709         msp->is_ht = true;
00710         memset(mi, 0, sizeof(*mi));
00711         mi->stats_update = jiffies;
00712 
00713         ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1);
00714         mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1) + ack_dur;
00715         mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
00716 
00717         mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
00718 
00719         /* When using MRR, sample more on the first attempt, without delay */
00720         if (mp->has_mrr) {
00721                 mi->sample_count = 16;
00722                 mi->sample_wait = 0;
00723         } else {
00724                 mi->sample_count = 8;
00725                 mi->sample_wait = 8;
00726         }
00727         mi->sample_tries = 4;
00728 
00729         stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
00730                 IEEE80211_HT_CAP_RX_STBC_SHIFT;
00731         mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
00732 
00733         if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
00734                 mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
00735 
00736         smps = (sta_cap & IEEE80211_HT_CAP_SM_PS) >>
00737                 IEEE80211_HT_CAP_SM_PS_SHIFT;
00738 
00739         for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
00740                 u16 req = 0;
00741 
00742                 mi->groups[i].supported = 0;
00743                 if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
00744                         if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
00745                                 req |= IEEE80211_HT_CAP_SGI_40;
00746                         else
00747                                 req |= IEEE80211_HT_CAP_SGI_20;
00748                 }
00749 
00750                 if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
00751                         req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
00752 
00753                 if ((sta_cap & req) != req)
00754                         continue;
00755 
00756                 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
00757                 if (smps == WLAN_HT_CAP_SM_PS_STATIC &&
00758                     minstrel_mcs_groups[i].streams > 1)
00759                         continue;
00760 
00761                 mi->groups[i].supported =
00762                         mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
00763 
00764                 if (mi->groups[i].supported)
00765                         n_supported++;
00766         }
00767 
00768         if (!n_supported)
00769                 goto use_legacy;
00770 
00771         return;
00772 
00773 use_legacy:
00774         msp->is_ht = false;
00775         memset(&msp->legacy, 0, sizeof(msp->legacy));
00776         msp->legacy.r = msp->ratelist;
00777         msp->legacy.sample_table = msp->sample_table;
00778         return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
00779 }
00780 
00781 static void
00782 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
00783                       struct ieee80211_sta *sta, void *priv_sta)
00784 {
00785         minstrel_ht_update_caps(priv, sband, sta, priv_sta);
00786 }
00787 
00788 static void
00789 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
00790                         struct ieee80211_sta *sta, void *priv_sta,
00791                         u32 changed)
00792 {
00793         minstrel_ht_update_caps(priv, sband, sta, priv_sta);
00794 }
00795 
00796 static void *
00797 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
00798 {
00799         struct ieee80211_supported_band *sband;
00800         struct minstrel_ht_sta_priv *msp;
00801         struct minstrel_priv *mp = priv;
00802         struct ieee80211_hw *hw = mp->hw;
00803         int max_rates = 0;
00804         int i;
00805 
00806         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
00807                 sband = hw->wiphy->bands[i];
00808                 if (sband && sband->n_bitrates > max_rates)
00809                         max_rates = sband->n_bitrates;
00810         }
00811 
00812         msp = kzalloc(sizeof(*msp), gfp);
00813         if (!msp)
00814                 return NULL;
00815 
00816         msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
00817         if (!msp->ratelist)
00818                 goto error;
00819 
00820         msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
00821         if (!msp->sample_table)
00822                 goto error1;
00823 
00824         return msp;
00825 
00826 error1:
00827         kfree(msp->ratelist);
00828 error:
00829         kfree(msp);
00830         return NULL;
00831 }
00832 
00833 static void
00834 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
00835 {
00836         struct minstrel_ht_sta_priv *msp = priv_sta;
00837 
00838         kfree(msp->sample_table);
00839         kfree(msp->ratelist);
00840         kfree(msp);
00841 }
00842 
00843 static void *
00844 minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
00845 {
00846         return mac80211_minstrel.alloc(hw, debugfsdir);
00847 }
00848 
00849 static void
00850 minstrel_ht_free(void *priv)
00851 {
00852         mac80211_minstrel.free(priv);
00853 }
00854 
00855 static struct rate_control_ops mac80211_minstrel_ht = {
00856         .name = "minstrel_ht",
00857         .tx_status = minstrel_ht_tx_status,
00858         .get_rate = minstrel_ht_get_rate,
00859         .rate_init = minstrel_ht_rate_init,
00860         .rate_update = minstrel_ht_rate_update,
00861         .alloc_sta = minstrel_ht_alloc_sta,
00862         .free_sta = minstrel_ht_free_sta,
00863         .alloc = minstrel_ht_alloc,
00864         .free = minstrel_ht_free,
00865 #ifdef CONFIG_MAC80211_DEBUGFS
00866         .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
00867         .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
00868 #endif
00869 };
00870 
00871 
00872 static void
00873 init_sample_table(void)
00874 {
00875         int col, i, new_idx;
00876         u8 rnd[MCS_GROUP_RATES];
00877 
00878         memset(sample_table, 0xff, sizeof(sample_table));
00879         for (col = 0; col < SAMPLE_COLUMNS; col++) {
00880                 for (i = 0; i < MCS_GROUP_RATES; i++) {
00881                         get_random_bytes(rnd, sizeof(rnd));
00882                         new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
00883 
00884                         while (sample_table[col][new_idx] != 0xff)
00885                                 new_idx = (new_idx + 1) % MCS_GROUP_RATES;
00886 
00887                         sample_table[col][new_idx] = i;
00888                 }
00889         }
00890 }
00891 
00892 int __init
00893 rc80211_minstrel_ht_init(void)
00894 {
00895         init_sample_table();
00896         return ieee80211_rate_control_register(&mac80211_minstrel_ht);
00897 }
00898 
00899 void
00900 rc80211_minstrel_ht_exit(void)
00901 {
00902         ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
00903 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:11