Path: blob/master/net/mac80211/rc80211_minstrel_ht.c
15111 views
/*1* Copyright (C) 2010 Felix Fietkau <[email protected]>2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License version 2 as5* published by the Free Software Foundation.6*/7#include <linux/netdevice.h>8#include <linux/types.h>9#include <linux/skbuff.h>10#include <linux/debugfs.h>11#include <linux/random.h>12#include <linux/ieee80211.h>13#include <net/mac80211.h>14#include "rate.h"15#include "rc80211_minstrel.h"16#include "rc80211_minstrel_ht.h"1718#define AVG_PKT_SIZE 120019#define SAMPLE_COLUMNS 1020#define EWMA_LEVEL 752122/* Number of bits for an average sized packet */23#define MCS_NBITS (AVG_PKT_SIZE << 3)2425/* Number of symbols for a packet with (bps) bits per symbol */26#define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))2728/* Transmission time for a packet containing (syms) symbols */29#define MCS_SYMBOL_TIME(sgi, syms) \30(sgi ? \31((syms) * 18 + 4) / 5 : /* syms * 3.6 us */ \32(syms) << 2 /* syms * 4 us */ \33)3435/* Transmit duration for the raw data part of an average sized packet */36#define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))3738/* MCS rate information for an MCS group */39#define MCS_GROUP(_streams, _sgi, _ht40) { \40.streams = _streams, \41.flags = \42(_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \43(_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \44.duration = { \45MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \46MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \47MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \48MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \49MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \50MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \51MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \52MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \53} \54}5556/*57* To enable sufficiently targeted rate sampling, MCS rates are divided into58* groups, based on the number of streams and flags (HT40, SGI) that they59* use.60*/61const struct mcs_group minstrel_mcs_groups[] = {62MCS_GROUP(1, 0, 0),63MCS_GROUP(2, 0, 0),64#if MINSTREL_MAX_STREAMS >= 365MCS_GROUP(3, 0, 0),66#endif6768MCS_GROUP(1, 1, 0),69MCS_GROUP(2, 1, 0),70#if MINSTREL_MAX_STREAMS >= 371MCS_GROUP(3, 1, 0),72#endif7374MCS_GROUP(1, 0, 1),75MCS_GROUP(2, 0, 1),76#if MINSTREL_MAX_STREAMS >= 377MCS_GROUP(3, 0, 1),78#endif7980MCS_GROUP(1, 1, 1),81MCS_GROUP(2, 1, 1),82#if MINSTREL_MAX_STREAMS >= 383MCS_GROUP(3, 1, 1),84#endif85};8687static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];8889/*90* Perform EWMA (Exponentially Weighted Moving Average) calculation91*/92static int93minstrel_ewma(int old, int new, int weight)94{95return (new * (100 - weight) + old * weight) / 100;96}9798/*99* Look up an MCS group index based on mac80211 rate information100*/101static int102minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)103{104int streams = (rate->idx / MCS_GROUP_RATES) + 1;105u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;106int i;107108for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {109if (minstrel_mcs_groups[i].streams != streams)110continue;111if (minstrel_mcs_groups[i].flags != (rate->flags & flags))112continue;113114return i;115}116117WARN_ON(1);118return 0;119}120121static inline struct minstrel_rate_stats *122minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)123{124return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];125}126127128/*129* Recalculate success probabilities and counters for a rate using EWMA130*/131static void132minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)133{134if (unlikely(mr->attempts > 0)) {135mr->sample_skipped = 0;136mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);137if (!mr->att_hist)138mr->probability = mr->cur_prob;139else140mr->probability = minstrel_ewma(mr->probability,141mr->cur_prob, EWMA_LEVEL);142mr->att_hist += mr->attempts;143mr->succ_hist += mr->success;144} else {145mr->sample_skipped++;146}147mr->last_success = mr->success;148mr->last_attempts = mr->attempts;149mr->success = 0;150mr->attempts = 0;151}152153/*154* Calculate throughput based on the average A-MPDU length, taking into account155* the expected number of retransmissions and their expected length156*/157static void158minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,159int group, int rate)160{161struct minstrel_rate_stats *mr;162unsigned int usecs;163164mr = &mi->groups[group].rates[rate];165166if (mr->probability < MINSTREL_FRAC(1, 10)) {167mr->cur_tp = 0;168return;169}170171usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);172usecs += minstrel_mcs_groups[group].duration[rate];173mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);174}175176/*177* Update rate statistics and select new primary rates178*179* Rules for rate selection:180* - max_prob_rate must use only one stream, as a tradeoff between delivery181* probability and throughput during strong fluctuations182* - as long as the max prob rate has a probability of more than 3/4, pick183* higher throughput rates, even if the probablity is a bit lower184*/185static void186minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)187{188struct minstrel_mcs_group_data *mg;189struct minstrel_rate_stats *mr;190int cur_prob, cur_prob_tp, cur_tp, cur_tp2;191int group, i, index;192193if (mi->ampdu_packets > 0) {194mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,195MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);196mi->ampdu_len = 0;197mi->ampdu_packets = 0;198}199200mi->sample_slow = 0;201mi->sample_count = 0;202mi->max_tp_rate = 0;203mi->max_tp_rate2 = 0;204mi->max_prob_rate = 0;205206for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {207cur_prob = 0;208cur_prob_tp = 0;209cur_tp = 0;210cur_tp2 = 0;211212mg = &mi->groups[group];213if (!mg->supported)214continue;215216mg->max_tp_rate = 0;217mg->max_tp_rate2 = 0;218mg->max_prob_rate = 0;219mi->sample_count++;220221for (i = 0; i < MCS_GROUP_RATES; i++) {222if (!(mg->supported & BIT(i)))223continue;224225mr = &mg->rates[i];226mr->retry_updated = false;227index = MCS_GROUP_RATES * group + i;228minstrel_calc_rate_ewma(mp, mr);229minstrel_ht_calc_tp(mp, mi, group, i);230231if (!mr->cur_tp)232continue;233234/* ignore the lowest rate of each single-stream group */235if (!i && minstrel_mcs_groups[group].streams == 1)236continue;237238if ((mr->cur_tp > cur_prob_tp && mr->probability >239MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {240mg->max_prob_rate = index;241cur_prob = mr->probability;242cur_prob_tp = mr->cur_tp;243}244245if (mr->cur_tp > cur_tp) {246swap(index, mg->max_tp_rate);247cur_tp = mr->cur_tp;248mr = minstrel_get_ratestats(mi, index);249}250251if (index >= mg->max_tp_rate)252continue;253254if (mr->cur_tp > cur_tp2) {255mg->max_tp_rate2 = index;256cur_tp2 = mr->cur_tp;257}258}259}260261/* try to sample up to half of the available rates during each interval */262mi->sample_count *= 4;263264cur_prob = 0;265cur_prob_tp = 0;266cur_tp = 0;267cur_tp2 = 0;268for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {269mg = &mi->groups[group];270if (!mg->supported)271continue;272273mr = minstrel_get_ratestats(mi, mg->max_prob_rate);274if (cur_prob_tp < mr->cur_tp &&275minstrel_mcs_groups[group].streams == 1) {276mi->max_prob_rate = mg->max_prob_rate;277cur_prob = mr->cur_prob;278cur_prob_tp = mr->cur_tp;279}280281mr = minstrel_get_ratestats(mi, mg->max_tp_rate);282if (cur_tp < mr->cur_tp) {283mi->max_tp_rate = mg->max_tp_rate;284cur_tp = mr->cur_tp;285}286287mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);288if (cur_tp2 < mr->cur_tp) {289mi->max_tp_rate2 = mg->max_tp_rate2;290cur_tp2 = mr->cur_tp;291}292}293294mi->stats_update = jiffies;295}296297static bool298minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)299{300if (!rate->count)301return false;302303if (rate->idx < 0)304return false;305306return !!(rate->flags & IEEE80211_TX_RC_MCS);307}308309static void310minstrel_next_sample_idx(struct minstrel_ht_sta *mi)311{312struct minstrel_mcs_group_data *mg;313314for (;;) {315mi->sample_group++;316mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);317mg = &mi->groups[mi->sample_group];318319if (!mg->supported)320continue;321322if (++mg->index >= MCS_GROUP_RATES) {323mg->index = 0;324if (++mg->column >= ARRAY_SIZE(sample_table))325mg->column = 0;326}327break;328}329}330331static void332minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,333bool primary)334{335int group, orig_group;336337orig_group = group = *idx / MCS_GROUP_RATES;338while (group > 0) {339group--;340341if (!mi->groups[group].supported)342continue;343344if (minstrel_mcs_groups[group].streams >345minstrel_mcs_groups[orig_group].streams)346continue;347348if (primary)349*idx = mi->groups[group].max_tp_rate;350else351*idx = mi->groups[group].max_tp_rate2;352break;353}354}355356static void357minstrel_aggr_check(struct minstrel_priv *mp, struct ieee80211_sta *pubsta, struct sk_buff *skb)358{359struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;360struct sta_info *sta = container_of(pubsta, struct sta_info, sta);361u16 tid;362363if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))364return;365366if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))367return;368369tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;370if (likely(sta->ampdu_mlme.tid_tx[tid]))371return;372373if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)374return;375376ieee80211_start_tx_ba_session(pubsta, tid, 5000);377}378379static void380minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,381struct ieee80211_sta *sta, void *priv_sta,382struct sk_buff *skb)383{384struct minstrel_ht_sta_priv *msp = priv_sta;385struct minstrel_ht_sta *mi = &msp->ht;386struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);387struct ieee80211_tx_rate *ar = info->status.rates;388struct minstrel_rate_stats *rate, *rate2;389struct minstrel_priv *mp = priv;390bool last = false;391int group;392int i = 0;393394if (!msp->is_ht)395return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);396397/* This packet was aggregated but doesn't carry status info */398if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&399!(info->flags & IEEE80211_TX_STAT_AMPDU))400return;401402if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {403info->status.ampdu_ack_len =404(info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);405info->status.ampdu_len = 1;406}407408mi->ampdu_packets++;409mi->ampdu_len += info->status.ampdu_len;410411if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {412mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);413mi->sample_tries = 2;414mi->sample_count--;415}416417if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)418mi->sample_packets += info->status.ampdu_len;419420for (i = 0; !last; i++) {421last = (i == IEEE80211_TX_MAX_RATES - 1) ||422!minstrel_ht_txstat_valid(&ar[i + 1]);423424if (!minstrel_ht_txstat_valid(&ar[i]))425break;426427group = minstrel_ht_get_group_idx(&ar[i]);428rate = &mi->groups[group].rates[ar[i].idx % 8];429430if (last)431rate->success += info->status.ampdu_ack_len;432433rate->attempts += ar[i].count * info->status.ampdu_len;434}435436/*437* check for sudden death of spatial multiplexing,438* downgrade to a lower number of streams if necessary.439*/440rate = minstrel_get_ratestats(mi, mi->max_tp_rate);441if (rate->attempts > 30 &&442MINSTREL_FRAC(rate->success, rate->attempts) <443MINSTREL_FRAC(20, 100))444minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);445446rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);447if (rate2->attempts > 30 &&448MINSTREL_FRAC(rate2->success, rate2->attempts) <449MINSTREL_FRAC(20, 100))450minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);451452if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {453minstrel_ht_update_stats(mp, mi);454minstrel_aggr_check(mp, sta, skb);455}456}457458static void459minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,460int index)461{462struct minstrel_rate_stats *mr;463const struct mcs_group *group;464unsigned int tx_time, tx_time_rtscts, tx_time_data;465unsigned int cw = mp->cw_min;466unsigned int ctime = 0;467unsigned int t_slot = 9; /* FIXME */468unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);469470mr = minstrel_get_ratestats(mi, index);471if (mr->probability < MINSTREL_FRAC(1, 10)) {472mr->retry_count = 1;473mr->retry_count_rtscts = 1;474return;475}476477mr->retry_count = 2;478mr->retry_count_rtscts = 2;479mr->retry_updated = true;480481group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];482tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;483484/* Contention time for first 2 tries */485ctime = (t_slot * cw) >> 1;486cw = min((cw << 1) | 1, mp->cw_max);487ctime += (t_slot * cw) >> 1;488cw = min((cw << 1) | 1, mp->cw_max);489490/* Total TX time for data and Contention after first 2 tries */491tx_time = ctime + 2 * (mi->overhead + tx_time_data);492tx_time_rtscts = ctime + 2 * (mi->overhead_rtscts + tx_time_data);493494/* See how many more tries we can fit inside segment size */495do {496/* Contention time for this try */497ctime = (t_slot * cw) >> 1;498cw = min((cw << 1) | 1, mp->cw_max);499500/* Total TX time after this try */501tx_time += ctime + mi->overhead + tx_time_data;502tx_time_rtscts += ctime + mi->overhead_rtscts + tx_time_data;503504if (tx_time_rtscts < mp->segment_size)505mr->retry_count_rtscts++;506} while ((tx_time < mp->segment_size) &&507(++mr->retry_count < mp->max_retry));508}509510511static void512minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,513struct ieee80211_tx_rate *rate, int index,514struct ieee80211_tx_rate_control *txrc,515bool sample, bool rtscts)516{517const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];518struct minstrel_rate_stats *mr;519520mr = minstrel_get_ratestats(mi, index);521if (!mr->retry_updated)522minstrel_calc_retransmit(mp, mi, index);523524if (sample)525rate->count = 1;526else if (mr->probability < MINSTREL_FRAC(20, 100))527rate->count = 2;528else if (rtscts)529rate->count = mr->retry_count_rtscts;530else531rate->count = mr->retry_count;532533rate->flags = IEEE80211_TX_RC_MCS | group->flags;534if (rtscts)535rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;536rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;537}538539static inline int540minstrel_get_duration(int index)541{542const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];543return group->duration[index % MCS_GROUP_RATES];544}545546static int547minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)548{549struct minstrel_rate_stats *mr;550struct minstrel_mcs_group_data *mg;551int sample_idx = 0;552553if (mi->sample_wait > 0) {554mi->sample_wait--;555return -1;556}557558if (!mi->sample_tries)559return -1;560561mi->sample_tries--;562mg = &mi->groups[mi->sample_group];563sample_idx = sample_table[mg->column][mg->index];564mr = &mg->rates[sample_idx];565sample_idx += mi->sample_group * MCS_GROUP_RATES;566minstrel_next_sample_idx(mi);567568/*569* When not using MRR, do not sample if the probability is already570* higher than 95% to avoid wasting airtime571*/572if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))573return -1;574575/*576* Make sure that lower rates get sampled only occasionally,577* if the link is working perfectly.578*/579if (minstrel_get_duration(sample_idx) >580minstrel_get_duration(mi->max_tp_rate)) {581if (mr->sample_skipped < 20)582return -1;583584if (mi->sample_slow++ > 2)585return -1;586}587588return sample_idx;589}590591static void592minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,593struct ieee80211_tx_rate_control *txrc)594{595struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);596struct ieee80211_tx_rate *ar = info->status.rates;597struct minstrel_ht_sta_priv *msp = priv_sta;598struct minstrel_ht_sta *mi = &msp->ht;599struct minstrel_priv *mp = priv;600int sample_idx;601bool sample = false;602603if (rate_control_send_low(sta, priv_sta, txrc))604return;605606if (!msp->is_ht)607return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);608609info->flags |= mi->tx_flags;610sample_idx = minstrel_get_sample_rate(mp, mi);611if (sample_idx >= 0) {612sample = true;613minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,614txrc, true, false);615info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;616} else {617minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,618txrc, false, false);619}620621if (mp->hw->max_rates >= 3) {622/*623* At least 3 tx rates supported, use624* sample_rate -> max_tp_rate -> max_prob_rate for sampling and625* max_tp_rate -> max_tp_rate2 -> max_prob_rate by default.626*/627if (sample_idx >= 0)628minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,629txrc, false, false);630else631minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,632txrc, false, true);633634minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate,635txrc, false, !sample);636637ar[3].count = 0;638ar[3].idx = -1;639} else if (mp->hw->max_rates == 2) {640/*641* Only 2 tx rates supported, use642* sample_rate -> max_prob_rate for sampling and643* max_tp_rate -> max_prob_rate by default.644*/645minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_prob_rate,646txrc, false, !sample);647648ar[2].count = 0;649ar[2].idx = -1;650} else {651/* Not using MRR, only use the first rate */652ar[1].count = 0;653ar[1].idx = -1;654}655656mi->total_packets++;657658/* wraparound */659if (mi->total_packets == ~0) {660mi->total_packets = 0;661mi->sample_packets = 0;662}663}664665static void666minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,667struct ieee80211_sta *sta, void *priv_sta,668enum nl80211_channel_type oper_chan_type)669{670struct minstrel_priv *mp = priv;671struct minstrel_ht_sta_priv *msp = priv_sta;672struct minstrel_ht_sta *mi = &msp->ht;673struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;674struct ieee80211_local *local = hw_to_local(mp->hw);675u16 sta_cap = sta->ht_cap.cap;676int n_supported = 0;677int ack_dur;678int stbc;679int i;680681/* fall back to the old minstrel for legacy stations */682if (!sta->ht_cap.ht_supported)683goto use_legacy;684685BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=686MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);687688msp->is_ht = true;689memset(mi, 0, sizeof(*mi));690mi->stats_update = jiffies;691692ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);693mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;694mi->overhead_rtscts = mi->overhead + 2 * ack_dur;695696mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);697698/* When using MRR, sample more on the first attempt, without delay */699if (mp->has_mrr) {700mi->sample_count = 16;701mi->sample_wait = 0;702} else {703mi->sample_count = 8;704mi->sample_wait = 8;705}706mi->sample_tries = 4;707708stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>709IEEE80211_HT_CAP_RX_STBC_SHIFT;710mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;711712if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)713mi->tx_flags |= IEEE80211_TX_CTL_LDPC;714715if (oper_chan_type != NL80211_CHAN_HT40MINUS &&716oper_chan_type != NL80211_CHAN_HT40PLUS)717sta_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;718719for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {720u16 req = 0;721722mi->groups[i].supported = 0;723if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {724if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)725req |= IEEE80211_HT_CAP_SGI_40;726else727req |= IEEE80211_HT_CAP_SGI_20;728}729730if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)731req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;732733if ((sta_cap & req) != req)734continue;735736mi->groups[i].supported =737mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];738739if (mi->groups[i].supported)740n_supported++;741}742743if (!n_supported)744goto use_legacy;745746return;747748use_legacy:749msp->is_ht = false;750memset(&msp->legacy, 0, sizeof(msp->legacy));751msp->legacy.r = msp->ratelist;752msp->legacy.sample_table = msp->sample_table;753return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);754}755756static void757minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,758struct ieee80211_sta *sta, void *priv_sta)759{760struct minstrel_priv *mp = priv;761762minstrel_ht_update_caps(priv, sband, sta, priv_sta, mp->hw->conf.channel_type);763}764765static void766minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,767struct ieee80211_sta *sta, void *priv_sta,768u32 changed, enum nl80211_channel_type oper_chan_type)769{770minstrel_ht_update_caps(priv, sband, sta, priv_sta, oper_chan_type);771}772773static void *774minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)775{776struct ieee80211_supported_band *sband;777struct minstrel_ht_sta_priv *msp;778struct minstrel_priv *mp = priv;779struct ieee80211_hw *hw = mp->hw;780int max_rates = 0;781int i;782783for (i = 0; i < IEEE80211_NUM_BANDS; i++) {784sband = hw->wiphy->bands[i];785if (sband && sband->n_bitrates > max_rates)786max_rates = sband->n_bitrates;787}788789msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);790if (!msp)791return NULL;792793msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);794if (!msp->ratelist)795goto error;796797msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);798if (!msp->sample_table)799goto error1;800801return msp;802803error1:804kfree(msp->ratelist);805error:806kfree(msp);807return NULL;808}809810static void811minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)812{813struct minstrel_ht_sta_priv *msp = priv_sta;814815kfree(msp->sample_table);816kfree(msp->ratelist);817kfree(msp);818}819820static void *821minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)822{823return mac80211_minstrel.alloc(hw, debugfsdir);824}825826static void827minstrel_ht_free(void *priv)828{829mac80211_minstrel.free(priv);830}831832static struct rate_control_ops mac80211_minstrel_ht = {833.name = "minstrel_ht",834.tx_status = minstrel_ht_tx_status,835.get_rate = minstrel_ht_get_rate,836.rate_init = minstrel_ht_rate_init,837.rate_update = minstrel_ht_rate_update,838.alloc_sta = minstrel_ht_alloc_sta,839.free_sta = minstrel_ht_free_sta,840.alloc = minstrel_ht_alloc,841.free = minstrel_ht_free,842#ifdef CONFIG_MAC80211_DEBUGFS843.add_sta_debugfs = minstrel_ht_add_sta_debugfs,844.remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,845#endif846};847848849static void850init_sample_table(void)851{852int col, i, new_idx;853u8 rnd[MCS_GROUP_RATES];854855memset(sample_table, 0xff, sizeof(sample_table));856for (col = 0; col < SAMPLE_COLUMNS; col++) {857for (i = 0; i < MCS_GROUP_RATES; i++) {858get_random_bytes(rnd, sizeof(rnd));859new_idx = (i + rnd[i]) % MCS_GROUP_RATES;860861while (sample_table[col][new_idx] != 0xff)862new_idx = (new_idx + 1) % MCS_GROUP_RATES;863864sample_table[col][new_idx] = i;865}866}867}868869int __init870rc80211_minstrel_ht_init(void)871{872init_sample_table();873return ieee80211_rate_control_register(&mac80211_minstrel_ht);874}875876void877rc80211_minstrel_ht_exit(void)878{879ieee80211_rate_control_unregister(&mac80211_minstrel_ht);880}881882883