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


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