rc80211_minstrel_ht.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/netdevice.h>
  9. #include <linux/types.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/random.h>
  13. #include <linux/ieee80211.h>
  14. #include <net/mac80211.h>
  15. #include "rate.h"
  16. #include "rc80211_minstrel.h"
  17. #include "rc80211_minstrel_ht.h"
  18. #define AVG_PKT_SIZE 1200
  19. #define SAMPLE_COLUMNS 10
  20. #define EWMA_LEVEL 75
  21. /* Number of bits for an average sized packet */
  22. #define MCS_NBITS (AVG_PKT_SIZE << 3)
  23. /* Number of symbols for a packet with (bps) bits per symbol */
  24. #define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
  25. /* Transmission time for a packet containing (syms) symbols */
  26. #define MCS_SYMBOL_TIME(sgi, syms) \
  27. (sgi ? \
  28. ((syms) * 18 + 4) / 5 : /* syms * 3.6 us */ \
  29. (syms) << 2 /* syms * 4 us */ \
  30. )
  31. /* Transmit duration for the raw data part of an average sized packet */
  32. #define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
  33. /* MCS rate information for an MCS group */
  34. #define MCS_GROUP(_streams, _sgi, _ht40) { \
  35. .streams = _streams, \
  36. .flags = \
  37. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  38. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  39. .duration = { \
  40. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
  41. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
  42. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
  43. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
  44. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
  45. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
  46. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
  47. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
  48. } \
  49. }
  50. /*
  51. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  52. * groups, based on the number of streams and flags (HT40, SGI) that they
  53. * use.
  54. */
  55. const struct mcs_group minstrel_mcs_groups[] = {
  56. MCS_GROUP(1, 0, 0),
  57. MCS_GROUP(2, 0, 0),
  58. #if MINSTREL_MAX_STREAMS >= 3
  59. MCS_GROUP(3, 0, 0),
  60. #endif
  61. MCS_GROUP(1, 1, 0),
  62. MCS_GROUP(2, 1, 0),
  63. #if MINSTREL_MAX_STREAMS >= 3
  64. MCS_GROUP(3, 1, 0),
  65. #endif
  66. MCS_GROUP(1, 0, 1),
  67. MCS_GROUP(2, 0, 1),
  68. #if MINSTREL_MAX_STREAMS >= 3
  69. MCS_GROUP(3, 0, 1),
  70. #endif
  71. MCS_GROUP(1, 1, 1),
  72. MCS_GROUP(2, 1, 1),
  73. #if MINSTREL_MAX_STREAMS >= 3
  74. MCS_GROUP(3, 1, 1),
  75. #endif
  76. };
  77. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
  78. /*
  79. * Perform EWMA (Exponentially Weighted Moving Average) calculation
  80. */
  81. static int
  82. minstrel_ewma(int old, int new, int weight)
  83. {
  84. return (new * (100 - weight) + old * weight) / 100;
  85. }
  86. /*
  87. * Look up an MCS group index based on mac80211 rate information
  88. */
  89. static int
  90. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  91. {
  92. int streams = (rate->idx / MCS_GROUP_RATES) + 1;
  93. u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
  96. if (minstrel_mcs_groups[i].streams != streams)
  97. continue;
  98. if (minstrel_mcs_groups[i].flags != (rate->flags & flags))
  99. continue;
  100. return i;
  101. }
  102. WARN_ON(1);
  103. return 0;
  104. }
  105. static inline struct minstrel_rate_stats *
  106. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  107. {
  108. return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
  109. }
  110. /*
  111. * Recalculate success probabilities and counters for a rate using EWMA
  112. */
  113. static void
  114. minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
  115. {
  116. if (unlikely(mr->attempts > 0)) {
  117. mr->sample_skipped = 0;
  118. mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
  119. if (!mr->att_hist)
  120. mr->probability = mr->cur_prob;
  121. else
  122. mr->probability = minstrel_ewma(mr->probability,
  123. mr->cur_prob, EWMA_LEVEL);
  124. mr->att_hist += mr->attempts;
  125. mr->succ_hist += mr->success;
  126. } else {
  127. mr->sample_skipped++;
  128. }
  129. mr->last_success = mr->success;
  130. mr->last_attempts = mr->attempts;
  131. mr->success = 0;
  132. mr->attempts = 0;
  133. }
  134. /*
  135. * Calculate throughput based on the average A-MPDU length, taking into account
  136. * the expected number of retransmissions and their expected length
  137. */
  138. static void
  139. minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  140. int group, int rate)
  141. {
  142. struct minstrel_rate_stats *mr;
  143. unsigned int usecs;
  144. mr = &mi->groups[group].rates[rate];
  145. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  146. mr->cur_tp = 0;
  147. return;
  148. }
  149. usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
  150. usecs += minstrel_mcs_groups[group].duration[rate];
  151. mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
  152. }
  153. /*
  154. * Update rate statistics and select new primary rates
  155. *
  156. * Rules for rate selection:
  157. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  158. * probability and throughput during strong fluctuations
  159. * - as long as the max prob rate has a probability of more than 3/4, pick
  160. * higher throughput rates, even if the probablity is a bit lower
  161. */
  162. static void
  163. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  164. {
  165. struct minstrel_mcs_group_data *mg;
  166. struct minstrel_rate_stats *mr;
  167. int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
  168. int group, i, index;
  169. if (mi->ampdu_packets > 0) {
  170. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  171. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  172. mi->ampdu_len = 0;
  173. mi->ampdu_packets = 0;
  174. }
  175. mi->sample_slow = 0;
  176. mi->sample_count = 0;
  177. mi->max_tp_rate = 0;
  178. mi->max_tp_rate2 = 0;
  179. mi->max_prob_rate = 0;
  180. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  181. cur_prob = 0;
  182. cur_prob_tp = 0;
  183. cur_tp = 0;
  184. cur_tp2 = 0;
  185. mg = &mi->groups[group];
  186. if (!mg->supported)
  187. continue;
  188. mg->max_tp_rate = 0;
  189. mg->max_tp_rate2 = 0;
  190. mg->max_prob_rate = 0;
  191. mi->sample_count++;
  192. for (i = 0; i < MCS_GROUP_RATES; i++) {
  193. if (!(mg->supported & BIT(i)))
  194. continue;
  195. mr = &mg->rates[i];
  196. mr->retry_updated = false;
  197. index = MCS_GROUP_RATES * group + i;
  198. minstrel_calc_rate_ewma(mp, mr);
  199. minstrel_ht_calc_tp(mp, mi, group, i);
  200. if (!mr->cur_tp)
  201. continue;
  202. /* ignore the lowest rate of each single-stream group */
  203. if (!i && minstrel_mcs_groups[group].streams == 1)
  204. continue;
  205. if ((mr->cur_tp > cur_prob_tp && mr->probability >
  206. MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
  207. mg->max_prob_rate = index;
  208. cur_prob = mr->probability;
  209. cur_prob_tp = mr->cur_tp;
  210. }
  211. if (mr->cur_tp > cur_tp) {
  212. swap(index, mg->max_tp_rate);
  213. cur_tp = mr->cur_tp;
  214. mr = minstrel_get_ratestats(mi, index);
  215. }
  216. if (index >= mg->max_tp_rate)
  217. continue;
  218. if (mr->cur_tp > cur_tp2) {
  219. mg->max_tp_rate2 = index;
  220. cur_tp2 = mr->cur_tp;
  221. }
  222. }
  223. }
  224. /* try to sample up to half of the availble rates during each interval */
  225. mi->sample_count *= 4;
  226. cur_prob = 0;
  227. cur_prob_tp = 0;
  228. cur_tp = 0;
  229. cur_tp2 = 0;
  230. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  231. mg = &mi->groups[group];
  232. if (!mg->supported)
  233. continue;
  234. mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
  235. if (cur_prob_tp < mr->cur_tp &&
  236. minstrel_mcs_groups[group].streams == 1) {
  237. mi->max_prob_rate = mg->max_prob_rate;
  238. cur_prob = mr->cur_prob;
  239. cur_prob_tp = mr->cur_tp;
  240. }
  241. mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
  242. if (cur_tp < mr->cur_tp) {
  243. mi->max_tp_rate = mg->max_tp_rate;
  244. cur_tp = mr->cur_tp;
  245. }
  246. mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
  247. if (cur_tp2 < mr->cur_tp) {
  248. mi->max_tp_rate2 = mg->max_tp_rate2;
  249. cur_tp2 = mr->cur_tp;
  250. }
  251. }
  252. mi->stats_update = jiffies;
  253. }
  254. static bool
  255. minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
  256. {
  257. if (!rate->count)
  258. return false;
  259. if (rate->idx < 0)
  260. return false;
  261. return !!(rate->flags & IEEE80211_TX_RC_MCS);
  262. }
  263. static void
  264. minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
  265. {
  266. struct minstrel_mcs_group_data *mg;
  267. for (;;) {
  268. mi->sample_group++;
  269. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  270. mg = &mi->groups[mi->sample_group];
  271. if (!mg->supported)
  272. continue;
  273. if (++mg->index >= MCS_GROUP_RATES) {
  274. mg->index = 0;
  275. if (++mg->column >= ARRAY_SIZE(sample_table))
  276. mg->column = 0;
  277. }
  278. break;
  279. }
  280. }
  281. static void
  282. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,
  283. bool primary)
  284. {
  285. int group, orig_group;
  286. orig_group = group = *idx / MCS_GROUP_RATES;
  287. while (group > 0) {
  288. group--;
  289. if (!mi->groups[group].supported)
  290. continue;
  291. if (minstrel_mcs_groups[group].streams >
  292. minstrel_mcs_groups[orig_group].streams)
  293. continue;
  294. if (primary)
  295. *idx = mi->groups[group].max_tp_rate;
  296. else
  297. *idx = mi->groups[group].max_tp_rate2;
  298. break;
  299. }
  300. }
  301. static void
  302. minstrel_aggr_check(struct minstrel_priv *mp, struct ieee80211_sta *pubsta, struct sk_buff *skb)
  303. {
  304. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  305. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  306. u16 tid;
  307. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  308. return;
  309. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  310. return;
  311. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  312. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  313. return;
  314. ieee80211_start_tx_ba_session(pubsta, tid);
  315. }
  316. static void
  317. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  318. struct ieee80211_sta *sta, void *priv_sta,
  319. struct sk_buff *skb)
  320. {
  321. struct minstrel_ht_sta_priv *msp = priv_sta;
  322. struct minstrel_ht_sta *mi = &msp->ht;
  323. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  324. struct ieee80211_tx_rate *ar = info->status.rates;
  325. struct minstrel_rate_stats *rate, *rate2;
  326. struct minstrel_priv *mp = priv;
  327. bool last = false;
  328. int group;
  329. int i = 0;
  330. if (!msp->is_ht)
  331. return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
  332. /* This packet was aggregated but doesn't carry status info */
  333. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  334. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  335. return;
  336. if (!info->status.ampdu_len) {
  337. info->status.ampdu_ack_len = 1;
  338. info->status.ampdu_len = 1;
  339. }
  340. mi->ampdu_packets++;
  341. mi->ampdu_len += info->status.ampdu_len;
  342. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  343. mi->sample_wait = 4 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  344. mi->sample_tries = 3;
  345. mi->sample_count--;
  346. }
  347. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) {
  348. mi->sample_packets += info->status.ampdu_len;
  349. minstrel_next_sample_idx(mi);
  350. }
  351. for (i = 0; !last; i++) {
  352. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  353. !minstrel_ht_txstat_valid(&ar[i + 1]);
  354. if (!minstrel_ht_txstat_valid(&ar[i]))
  355. break;
  356. group = minstrel_ht_get_group_idx(&ar[i]);
  357. rate = &mi->groups[group].rates[ar[i].idx % 8];
  358. if (last && (info->flags & IEEE80211_TX_STAT_ACK))
  359. rate->success += info->status.ampdu_ack_len;
  360. rate->attempts += ar[i].count * info->status.ampdu_len;
  361. }
  362. /*
  363. * check for sudden death of spatial multiplexing,
  364. * downgrade to a lower number of streams if necessary.
  365. */
  366. rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
  367. if (rate->attempts > 30 &&
  368. MINSTREL_FRAC(rate->success, rate->attempts) <
  369. MINSTREL_FRAC(20, 100))
  370. minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
  371. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
  372. if (rate2->attempts > 30 &&
  373. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  374. MINSTREL_FRAC(20, 100))
  375. minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
  376. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  377. minstrel_ht_update_stats(mp, mi);
  378. minstrel_aggr_check(mp, sta, skb);
  379. }
  380. }
  381. static void
  382. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  383. int index)
  384. {
  385. struct minstrel_rate_stats *mr;
  386. const struct mcs_group *group;
  387. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  388. unsigned int cw = mp->cw_min;
  389. unsigned int t_slot = 9; /* FIXME */
  390. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  391. mr = minstrel_get_ratestats(mi, index);
  392. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  393. mr->retry_count = 1;
  394. mr->retry_count_rtscts = 1;
  395. return;
  396. }
  397. mr->retry_count = 2;
  398. mr->retry_count_rtscts = 2;
  399. mr->retry_updated = true;
  400. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  401. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
  402. tx_time = 2 * (t_slot + mi->overhead + tx_time_data);
  403. tx_time_rtscts = 2 * (t_slot + mi->overhead_rtscts + tx_time_data);
  404. do {
  405. cw = (cw << 1) | 1;
  406. cw = min(cw, mp->cw_max);
  407. tx_time += cw + t_slot + mi->overhead;
  408. tx_time_rtscts += cw + t_slot + mi->overhead_rtscts;
  409. if (tx_time_rtscts < mp->segment_size)
  410. mr->retry_count_rtscts++;
  411. } while ((tx_time < mp->segment_size) &&
  412. (++mr->retry_count < mp->max_retry));
  413. }
  414. static void
  415. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  416. struct ieee80211_tx_rate *rate, int index,
  417. struct ieee80211_tx_rate_control *txrc,
  418. bool sample, bool rtscts)
  419. {
  420. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  421. struct minstrel_rate_stats *mr;
  422. mr = minstrel_get_ratestats(mi, index);
  423. if (!mr->retry_updated)
  424. minstrel_calc_retransmit(mp, mi, index);
  425. if (mr->probability < MINSTREL_FRAC(20, 100))
  426. rate->count = 2;
  427. else if (rtscts)
  428. rate->count = mr->retry_count_rtscts;
  429. else
  430. rate->count = mr->retry_count;
  431. rate->flags = IEEE80211_TX_RC_MCS | group->flags;
  432. if (txrc->short_preamble)
  433. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  434. if (txrc->rts || rtscts)
  435. rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  436. rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
  437. }
  438. static inline int
  439. minstrel_get_duration(int index)
  440. {
  441. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  442. return group->duration[index % MCS_GROUP_RATES];
  443. }
  444. static int
  445. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  446. {
  447. struct minstrel_rate_stats *mr;
  448. struct minstrel_mcs_group_data *mg;
  449. int sample_idx = 0;
  450. if (mi->sample_wait > 0) {
  451. mi->sample_wait--;
  452. return -1;
  453. }
  454. if (!mi->sample_tries)
  455. return -1;
  456. mi->sample_tries--;
  457. mg = &mi->groups[mi->sample_group];
  458. sample_idx = sample_table[mg->column][mg->index];
  459. mr = &mg->rates[sample_idx];
  460. sample_idx += mi->sample_group * MCS_GROUP_RATES;
  461. /*
  462. * When not using MRR, do not sample if the probability is already
  463. * higher than 95% to avoid wasting airtime
  464. */
  465. if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
  466. goto next;
  467. /*
  468. * Make sure that lower rates get sampled only occasionally,
  469. * if the link is working perfectly.
  470. */
  471. if (minstrel_get_duration(sample_idx) >
  472. minstrel_get_duration(mi->max_tp_rate)) {
  473. if (mr->sample_skipped < 10)
  474. goto next;
  475. if (mi->sample_slow++ > 2)
  476. goto next;
  477. }
  478. return sample_idx;
  479. next:
  480. minstrel_next_sample_idx(mi);
  481. return -1;
  482. }
  483. static void
  484. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  485. struct ieee80211_tx_rate_control *txrc)
  486. {
  487. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  488. struct ieee80211_tx_rate *ar = info->status.rates;
  489. struct minstrel_ht_sta_priv *msp = priv_sta;
  490. struct minstrel_ht_sta *mi = &msp->ht;
  491. struct minstrel_priv *mp = priv;
  492. int sample_idx;
  493. if (rate_control_send_low(sta, priv_sta, txrc))
  494. return;
  495. if (!msp->is_ht)
  496. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  497. info->flags |= mi->tx_flags;
  498. sample_idx = minstrel_get_sample_rate(mp, mi);
  499. if (sample_idx >= 0) {
  500. minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
  501. txrc, true, false);
  502. minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
  503. txrc, false, true);
  504. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  505. } else {
  506. minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
  507. txrc, false, false);
  508. minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
  509. txrc, false, true);
  510. }
  511. minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate, txrc, false, true);
  512. ar[3].count = 0;
  513. ar[3].idx = -1;
  514. mi->total_packets++;
  515. /* wraparound */
  516. if (mi->total_packets == ~0) {
  517. mi->total_packets = 0;
  518. mi->sample_packets = 0;
  519. }
  520. }
  521. static void
  522. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  523. struct ieee80211_sta *sta, void *priv_sta,
  524. enum nl80211_channel_type oper_chan_type)
  525. {
  526. struct minstrel_priv *mp = priv;
  527. struct minstrel_ht_sta_priv *msp = priv_sta;
  528. struct minstrel_ht_sta *mi = &msp->ht;
  529. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  530. struct ieee80211_local *local = hw_to_local(mp->hw);
  531. u16 sta_cap = sta->ht_cap.cap;
  532. int ack_dur;
  533. int stbc;
  534. int i;
  535. /* fall back to the old minstrel for legacy stations */
  536. if (!sta->ht_cap.ht_supported) {
  537. msp->is_ht = false;
  538. memset(&msp->legacy, 0, sizeof(msp->legacy));
  539. msp->legacy.r = msp->ratelist;
  540. msp->legacy.sample_table = msp->sample_table;
  541. return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
  542. }
  543. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  544. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
  545. msp->is_ht = true;
  546. memset(mi, 0, sizeof(*mi));
  547. mi->stats_update = jiffies;
  548. ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);
  549. mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;
  550. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  551. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  552. /* When using MRR, sample more on the first attempt, without delay */
  553. if (mp->has_mrr) {
  554. mi->sample_count = 16;
  555. mi->sample_wait = 0;
  556. } else {
  557. mi->sample_count = 8;
  558. mi->sample_wait = 8;
  559. }
  560. mi->sample_tries = 4;
  561. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  562. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  563. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  564. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  565. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  566. if (oper_chan_type != NL80211_CHAN_HT40MINUS &&
  567. oper_chan_type != NL80211_CHAN_HT40PLUS)
  568. sta_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  569. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  570. u16 req = 0;
  571. mi->groups[i].supported = 0;
  572. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  573. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  574. req |= IEEE80211_HT_CAP_SGI_40;
  575. else
  576. req |= IEEE80211_HT_CAP_SGI_20;
  577. }
  578. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  579. req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  580. if ((sta_cap & req) != req)
  581. continue;
  582. mi->groups[i].supported =
  583. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  584. }
  585. }
  586. static void
  587. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  588. struct ieee80211_sta *sta, void *priv_sta)
  589. {
  590. struct minstrel_priv *mp = priv;
  591. minstrel_ht_update_caps(priv, sband, sta, priv_sta, mp->hw->conf.channel_type);
  592. }
  593. static void
  594. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  595. struct ieee80211_sta *sta, void *priv_sta,
  596. u32 changed, enum nl80211_channel_type oper_chan_type)
  597. {
  598. minstrel_ht_update_caps(priv, sband, sta, priv_sta, oper_chan_type);
  599. }
  600. static void *
  601. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  602. {
  603. struct ieee80211_supported_band *sband;
  604. struct minstrel_ht_sta_priv *msp;
  605. struct minstrel_priv *mp = priv;
  606. struct ieee80211_hw *hw = mp->hw;
  607. int max_rates = 0;
  608. int i;
  609. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  610. sband = hw->wiphy->bands[i];
  611. if (sband && sband->n_bitrates > max_rates)
  612. max_rates = sband->n_bitrates;
  613. }
  614. msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);
  615. if (!msp)
  616. return NULL;
  617. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  618. if (!msp->ratelist)
  619. goto error;
  620. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  621. if (!msp->sample_table)
  622. goto error1;
  623. return msp;
  624. error1:
  625. kfree(msp->ratelist);
  626. error:
  627. kfree(msp);
  628. return NULL;
  629. }
  630. static void
  631. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  632. {
  633. struct minstrel_ht_sta_priv *msp = priv_sta;
  634. kfree(msp->sample_table);
  635. kfree(msp->ratelist);
  636. kfree(msp);
  637. }
  638. static void *
  639. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  640. {
  641. return mac80211_minstrel.alloc(hw, debugfsdir);
  642. }
  643. static void
  644. minstrel_ht_free(void *priv)
  645. {
  646. mac80211_minstrel.free(priv);
  647. }
  648. static struct rate_control_ops mac80211_minstrel_ht = {
  649. .name = "minstrel_ht",
  650. .tx_status = minstrel_ht_tx_status,
  651. .get_rate = minstrel_ht_get_rate,
  652. .rate_init = minstrel_ht_rate_init,
  653. .rate_update = minstrel_ht_rate_update,
  654. .alloc_sta = minstrel_ht_alloc_sta,
  655. .free_sta = minstrel_ht_free_sta,
  656. .alloc = minstrel_ht_alloc,
  657. .free = minstrel_ht_free,
  658. #ifdef CONFIG_MAC80211_DEBUGFS
  659. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  660. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  661. #endif
  662. };
  663. static void
  664. init_sample_table(void)
  665. {
  666. int col, i, new_idx;
  667. u8 rnd[MCS_GROUP_RATES];
  668. memset(sample_table, 0xff, sizeof(sample_table));
  669. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  670. for (i = 0; i < MCS_GROUP_RATES; i++) {
  671. get_random_bytes(rnd, sizeof(rnd));
  672. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  673. while (sample_table[col][new_idx] != 0xff)
  674. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  675. sample_table[col][new_idx] = i;
  676. }
  677. }
  678. }
  679. int __init
  680. rc80211_minstrel_ht_init(void)
  681. {
  682. init_sample_table();
  683. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  684. }
  685. void
  686. rc80211_minstrel_ht_exit(void)
  687. {
  688. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  689. }