rc80211_minstrel_ht.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. * Copyright (C) 2010-2013 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. /* Number of bits for an average sized packet */
  20. #define MCS_NBITS (AVG_PKT_SIZE << 3)
  21. /* Number of symbols for a packet with (bps) bits per symbol */
  22. #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
  23. /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
  24. #define MCS_SYMBOL_TIME(sgi, syms) \
  25. (sgi ? \
  26. ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
  27. ((syms) * 1000) << 2 /* syms * 4 us */ \
  28. )
  29. /* Transmit duration for the raw data part of an average sized packet */
  30. #define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
  31. /*
  32. * Define group sort order: HT40 -> SGI -> #streams
  33. */
  34. #define GROUP_IDX(_streams, _sgi, _ht40) \
  35. MINSTREL_MAX_STREAMS * 2 * _ht40 + \
  36. MINSTREL_MAX_STREAMS * _sgi + \
  37. _streams - 1
  38. /* MCS rate information for an MCS group */
  39. #define MCS_GROUP(_streams, _sgi, _ht40) \
  40. [GROUP_IDX(_streams, _sgi, _ht40)] = { \
  41. .streams = _streams, \
  42. .flags = \
  43. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  44. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  45. .duration = { \
  46. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
  47. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
  48. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
  49. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
  50. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
  51. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
  52. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
  53. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
  54. } \
  55. }
  56. #define CCK_DURATION(_bitrate, _short, _len) \
  57. (1000 * (10 /* SIFS */ + \
  58. (_short ? 72 + 24 : 144 + 48) + \
  59. (8 * (_len + 4) * 10) / (_bitrate)))
  60. #define CCK_ACK_DURATION(_bitrate, _short) \
  61. (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \
  62. CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE))
  63. #define CCK_DURATION_LIST(_short) \
  64. CCK_ACK_DURATION(10, _short), \
  65. CCK_ACK_DURATION(20, _short), \
  66. CCK_ACK_DURATION(55, _short), \
  67. CCK_ACK_DURATION(110, _short)
  68. #define CCK_GROUP \
  69. [MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS] = { \
  70. .streams = 0, \
  71. .duration = { \
  72. CCK_DURATION_LIST(false), \
  73. CCK_DURATION_LIST(true) \
  74. } \
  75. }
  76. /*
  77. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  78. * groups, based on the number of streams and flags (HT40, SGI) that they
  79. * use.
  80. *
  81. * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
  82. * HT40 -> SGI -> #streams
  83. */
  84. const struct mcs_group minstrel_mcs_groups[] = {
  85. MCS_GROUP(1, 0, 0),
  86. MCS_GROUP(2, 0, 0),
  87. #if MINSTREL_MAX_STREAMS >= 3
  88. MCS_GROUP(3, 0, 0),
  89. #endif
  90. MCS_GROUP(1, 1, 0),
  91. MCS_GROUP(2, 1, 0),
  92. #if MINSTREL_MAX_STREAMS >= 3
  93. MCS_GROUP(3, 1, 0),
  94. #endif
  95. MCS_GROUP(1, 0, 1),
  96. MCS_GROUP(2, 0, 1),
  97. #if MINSTREL_MAX_STREAMS >= 3
  98. MCS_GROUP(3, 0, 1),
  99. #endif
  100. MCS_GROUP(1, 1, 1),
  101. MCS_GROUP(2, 1, 1),
  102. #if MINSTREL_MAX_STREAMS >= 3
  103. MCS_GROUP(3, 1, 1),
  104. #endif
  105. /* must be last */
  106. CCK_GROUP
  107. };
  108. #define MINSTREL_CCK_GROUP (ARRAY_SIZE(minstrel_mcs_groups) - 1)
  109. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
  110. static void
  111. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
  112. /*
  113. * Look up an MCS group index based on mac80211 rate information
  114. */
  115. static int
  116. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  117. {
  118. return GROUP_IDX((rate->idx / 8) + 1,
  119. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  120. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
  121. }
  122. static struct minstrel_rate_stats *
  123. minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  124. struct ieee80211_tx_rate *rate)
  125. {
  126. int group, idx;
  127. if (rate->flags & IEEE80211_TX_RC_MCS) {
  128. group = minstrel_ht_get_group_idx(rate);
  129. idx = rate->idx % 8;
  130. } else {
  131. group = MINSTREL_CCK_GROUP;
  132. for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++)
  133. if (rate->idx == mp->cck_rates[idx])
  134. break;
  135. /* short preamble */
  136. if (!(mi->groups[group].supported & BIT(idx)))
  137. idx += 4;
  138. }
  139. return &mi->groups[group].rates[idx];
  140. }
  141. static inline struct minstrel_rate_stats *
  142. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  143. {
  144. return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
  145. }
  146. /*
  147. * Recalculate success probabilities and counters for a rate using EWMA
  148. */
  149. static void
  150. minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
  151. {
  152. if (unlikely(mr->attempts > 0)) {
  153. mr->sample_skipped = 0;
  154. mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
  155. if (!mr->att_hist)
  156. mr->probability = mr->cur_prob;
  157. else
  158. mr->probability = minstrel_ewma(mr->probability,
  159. mr->cur_prob, EWMA_LEVEL);
  160. mr->att_hist += mr->attempts;
  161. mr->succ_hist += mr->success;
  162. } else {
  163. mr->sample_skipped++;
  164. }
  165. mr->last_success = mr->success;
  166. mr->last_attempts = mr->attempts;
  167. mr->success = 0;
  168. mr->attempts = 0;
  169. }
  170. /*
  171. * Calculate throughput based on the average A-MPDU length, taking into account
  172. * the expected number of retransmissions and their expected length
  173. */
  174. static void
  175. minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
  176. {
  177. struct minstrel_rate_stats *mr;
  178. unsigned int nsecs = 0;
  179. unsigned int tp;
  180. unsigned int prob;
  181. mr = &mi->groups[group].rates[rate];
  182. prob = mr->probability;
  183. if (prob < MINSTREL_FRAC(1, 10)) {
  184. mr->cur_tp = 0;
  185. return;
  186. }
  187. /*
  188. * For the throughput calculation, limit the probability value to 90% to
  189. * account for collision related packet error rate fluctuation
  190. */
  191. if (prob > MINSTREL_FRAC(9, 10))
  192. prob = MINSTREL_FRAC(9, 10);
  193. if (group != MINSTREL_CCK_GROUP)
  194. nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
  195. nsecs += minstrel_mcs_groups[group].duration[rate];
  196. /* prob is scaled - see MINSTREL_FRAC above */
  197. tp = 1000000 * ((prob * 1000) / nsecs);
  198. mr->cur_tp = MINSTREL_TRUNC(tp);
  199. }
  200. /*
  201. * Update rate statistics and select new primary rates
  202. *
  203. * Rules for rate selection:
  204. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  205. * probability and throughput during strong fluctuations
  206. * - as long as the max prob rate has a probability of more than 3/4, pick
  207. * higher throughput rates, even if the probablity is a bit lower
  208. */
  209. static void
  210. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  211. {
  212. struct minstrel_mcs_group_data *mg;
  213. struct minstrel_rate_stats *mr;
  214. int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
  215. int group, i, index;
  216. bool mi_rates_valid = false;
  217. if (mi->ampdu_packets > 0) {
  218. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  219. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  220. mi->ampdu_len = 0;
  221. mi->ampdu_packets = 0;
  222. }
  223. mi->sample_slow = 0;
  224. mi->sample_count = 0;
  225. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  226. bool mg_rates_valid = false;
  227. cur_prob = 0;
  228. cur_prob_tp = 0;
  229. cur_tp = 0;
  230. cur_tp2 = 0;
  231. mg = &mi->groups[group];
  232. if (!mg->supported)
  233. continue;
  234. mi->sample_count++;
  235. for (i = 0; i < MCS_GROUP_RATES; i++) {
  236. if (!(mg->supported & BIT(i)))
  237. continue;
  238. index = MCS_GROUP_RATES * group + i;
  239. /* initialize rates selections starting indexes */
  240. if (!mg_rates_valid) {
  241. mg->max_tp_rate = mg->max_tp_rate2 =
  242. mg->max_prob_rate = i;
  243. if (!mi_rates_valid) {
  244. mi->max_tp_rate = mi->max_tp_rate2 =
  245. mi->max_prob_rate = index;
  246. mi_rates_valid = true;
  247. }
  248. mg_rates_valid = true;
  249. }
  250. mr = &mg->rates[i];
  251. mr->retry_updated = false;
  252. minstrel_calc_rate_ewma(mr);
  253. minstrel_ht_calc_tp(mi, group, i);
  254. if (!mr->cur_tp)
  255. continue;
  256. if ((mr->cur_tp > cur_prob_tp && mr->probability >
  257. MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
  258. mg->max_prob_rate = index;
  259. cur_prob = mr->probability;
  260. cur_prob_tp = mr->cur_tp;
  261. }
  262. if (mr->cur_tp > cur_tp) {
  263. swap(index, mg->max_tp_rate);
  264. cur_tp = mr->cur_tp;
  265. mr = minstrel_get_ratestats(mi, index);
  266. }
  267. if (index >= mg->max_tp_rate)
  268. continue;
  269. if (mr->cur_tp > cur_tp2) {
  270. mg->max_tp_rate2 = index;
  271. cur_tp2 = mr->cur_tp;
  272. }
  273. }
  274. }
  275. /* try to sample all available rates during each interval */
  276. mi->sample_count *= 8;
  277. cur_prob = 0;
  278. cur_prob_tp = 0;
  279. cur_tp = 0;
  280. cur_tp2 = 0;
  281. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  282. mg = &mi->groups[group];
  283. if (!mg->supported)
  284. continue;
  285. mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
  286. if (cur_tp < mr->cur_tp) {
  287. mi->max_tp_rate2 = mi->max_tp_rate;
  288. cur_tp2 = cur_tp;
  289. mi->max_tp_rate = mg->max_tp_rate;
  290. cur_tp = mr->cur_tp;
  291. mi->max_prob_streams = minstrel_mcs_groups[group].streams - 1;
  292. }
  293. mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
  294. if (cur_tp2 < mr->cur_tp) {
  295. mi->max_tp_rate2 = mg->max_tp_rate2;
  296. cur_tp2 = mr->cur_tp;
  297. }
  298. }
  299. if (mi->max_prob_streams < 1)
  300. mi->max_prob_streams = 1;
  301. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  302. mg = &mi->groups[group];
  303. if (!mg->supported)
  304. continue;
  305. mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
  306. if (cur_prob_tp < mr->cur_tp &&
  307. minstrel_mcs_groups[group].streams <= mi->max_prob_streams) {
  308. mi->max_prob_rate = mg->max_prob_rate;
  309. cur_prob = mr->cur_prob;
  310. cur_prob_tp = mr->cur_tp;
  311. }
  312. }
  313. #ifdef CONFIG_MAC80211_DEBUGFS
  314. /* use fixed index if set */
  315. if (mp->fixed_rate_idx != -1) {
  316. mi->max_tp_rate = mp->fixed_rate_idx;
  317. mi->max_tp_rate2 = mp->fixed_rate_idx;
  318. mi->max_prob_rate = mp->fixed_rate_idx;
  319. }
  320. #endif
  321. mi->stats_update = jiffies;
  322. }
  323. static bool
  324. minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
  325. {
  326. if (rate->idx < 0)
  327. return false;
  328. if (!rate->count)
  329. return false;
  330. if (rate->flags & IEEE80211_TX_RC_MCS)
  331. return true;
  332. return rate->idx == mp->cck_rates[0] ||
  333. rate->idx == mp->cck_rates[1] ||
  334. rate->idx == mp->cck_rates[2] ||
  335. rate->idx == mp->cck_rates[3];
  336. }
  337. static void
  338. minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
  339. {
  340. struct minstrel_mcs_group_data *mg;
  341. for (;;) {
  342. mi->sample_group++;
  343. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  344. mg = &mi->groups[mi->sample_group];
  345. if (!mg->supported)
  346. continue;
  347. if (++mg->index >= MCS_GROUP_RATES) {
  348. mg->index = 0;
  349. if (++mg->column >= ARRAY_SIZE(sample_table))
  350. mg->column = 0;
  351. }
  352. break;
  353. }
  354. }
  355. static void
  356. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,
  357. bool primary)
  358. {
  359. int group, orig_group;
  360. orig_group = group = *idx / MCS_GROUP_RATES;
  361. while (group > 0) {
  362. group--;
  363. if (!mi->groups[group].supported)
  364. continue;
  365. if (minstrel_mcs_groups[group].streams >
  366. minstrel_mcs_groups[orig_group].streams)
  367. continue;
  368. if (primary)
  369. *idx = mi->groups[group].max_tp_rate;
  370. else
  371. *idx = mi->groups[group].max_tp_rate2;
  372. break;
  373. }
  374. }
  375. static void
  376. minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
  377. {
  378. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  379. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  380. u16 tid;
  381. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  382. return;
  383. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  384. return;
  385. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  386. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  387. return;
  388. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  389. return;
  390. ieee80211_start_tx_ba_session(pubsta, tid, 5000);
  391. }
  392. static void
  393. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  394. struct ieee80211_sta *sta, void *priv_sta,
  395. struct sk_buff *skb)
  396. {
  397. struct minstrel_ht_sta_priv *msp = priv_sta;
  398. struct minstrel_ht_sta *mi = &msp->ht;
  399. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  400. struct ieee80211_tx_rate *ar = info->status.rates;
  401. struct minstrel_rate_stats *rate, *rate2;
  402. struct minstrel_priv *mp = priv;
  403. bool last, update = false;
  404. int i;
  405. if (!msp->is_ht)
  406. return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
  407. /* This packet was aggregated but doesn't carry status info */
  408. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  409. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  410. return;
  411. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  412. info->status.ampdu_ack_len =
  413. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  414. info->status.ampdu_len = 1;
  415. }
  416. mi->ampdu_packets++;
  417. mi->ampdu_len += info->status.ampdu_len;
  418. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  419. mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  420. mi->sample_tries = 1;
  421. mi->sample_count--;
  422. }
  423. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  424. mi->sample_packets += info->status.ampdu_len;
  425. last = !minstrel_ht_txstat_valid(mp, &ar[0]);
  426. for (i = 0; !last; i++) {
  427. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  428. !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
  429. rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
  430. if (last)
  431. rate->success += info->status.ampdu_ack_len;
  432. rate->attempts += ar[i].count * info->status.ampdu_len;
  433. }
  434. /*
  435. * check for sudden death of spatial multiplexing,
  436. * downgrade to a lower number of streams if necessary.
  437. */
  438. rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
  439. if (rate->attempts > 30 &&
  440. MINSTREL_FRAC(rate->success, rate->attempts) <
  441. MINSTREL_FRAC(20, 100)) {
  442. minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
  443. update = true;
  444. }
  445. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
  446. if (rate2->attempts > 30 &&
  447. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  448. MINSTREL_FRAC(20, 100)) {
  449. minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
  450. update = true;
  451. }
  452. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  453. update = true;
  454. minstrel_ht_update_stats(mp, mi);
  455. if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
  456. mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
  457. minstrel_aggr_check(sta, skb);
  458. }
  459. if (update)
  460. minstrel_ht_update_rates(mp, mi);
  461. }
  462. static void
  463. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  464. int index)
  465. {
  466. struct minstrel_rate_stats *mr;
  467. const struct mcs_group *group;
  468. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  469. unsigned int cw = mp->cw_min;
  470. unsigned int ctime = 0;
  471. unsigned int t_slot = 9; /* FIXME */
  472. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  473. unsigned int overhead = 0, overhead_rtscts = 0;
  474. mr = minstrel_get_ratestats(mi, index);
  475. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  476. mr->retry_count = 1;
  477. mr->retry_count_rtscts = 1;
  478. return;
  479. }
  480. mr->retry_count = 2;
  481. mr->retry_count_rtscts = 2;
  482. mr->retry_updated = true;
  483. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  484. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
  485. /* Contention time for first 2 tries */
  486. ctime = (t_slot * cw) >> 1;
  487. cw = min((cw << 1) | 1, mp->cw_max);
  488. ctime += (t_slot * cw) >> 1;
  489. cw = min((cw << 1) | 1, mp->cw_max);
  490. if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
  491. overhead = mi->overhead;
  492. overhead_rtscts = mi->overhead_rtscts;
  493. }
  494. /* Total TX time for data and Contention after first 2 tries */
  495. tx_time = ctime + 2 * (overhead + tx_time_data);
  496. tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
  497. /* See how many more tries we can fit inside segment size */
  498. do {
  499. /* Contention time for this try */
  500. ctime = (t_slot * cw) >> 1;
  501. cw = min((cw << 1) | 1, mp->cw_max);
  502. /* Total TX time after this try */
  503. tx_time += ctime + overhead + tx_time_data;
  504. tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
  505. if (tx_time_rtscts < mp->segment_size)
  506. mr->retry_count_rtscts++;
  507. } while ((tx_time < mp->segment_size) &&
  508. (++mr->retry_count < mp->max_retry));
  509. }
  510. static void
  511. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  512. struct ieee80211_sta_rates *ratetbl, int offset, int index)
  513. {
  514. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  515. struct minstrel_rate_stats *mr;
  516. u8 idx;
  517. u16 flags;
  518. mr = minstrel_get_ratestats(mi, index);
  519. if (!mr->retry_updated)
  520. minstrel_calc_retransmit(mp, mi, index);
  521. if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
  522. ratetbl->rate[offset].count = 2;
  523. ratetbl->rate[offset].count_rts = 2;
  524. ratetbl->rate[offset].count_cts = 2;
  525. } else {
  526. ratetbl->rate[offset].count = mr->retry_count;
  527. ratetbl->rate[offset].count_cts = mr->retry_count;
  528. ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
  529. }
  530. if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  531. idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
  532. flags = 0;
  533. } else {
  534. idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
  535. flags = IEEE80211_TX_RC_MCS | group->flags;
  536. }
  537. if (offset > 0) {
  538. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  539. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  540. }
  541. ratetbl->rate[offset].idx = idx;
  542. ratetbl->rate[offset].flags = flags;
  543. }
  544. static void
  545. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  546. {
  547. struct ieee80211_sta_rates *rates;
  548. int i = 0;
  549. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  550. if (!rates)
  551. return;
  552. /* Start with max_tp_rate */
  553. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate);
  554. if (mp->hw->max_rates >= 3) {
  555. /* At least 3 tx rates supported, use max_tp_rate2 next */
  556. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate2);
  557. }
  558. if (mp->hw->max_rates >= 2) {
  559. /*
  560. * At least 2 tx rates supported, use max_prob_rate next */
  561. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  562. }
  563. rates->rate[i].idx = -1;
  564. rate_control_set_rates(mp->hw, mi->sta, rates);
  565. }
  566. static inline int
  567. minstrel_get_duration(int index)
  568. {
  569. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  570. return group->duration[index % MCS_GROUP_RATES];
  571. }
  572. static int
  573. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  574. {
  575. struct minstrel_rate_stats *mr;
  576. struct minstrel_mcs_group_data *mg;
  577. unsigned int sample_dur, sample_group;
  578. int sample_idx = 0;
  579. if (mi->sample_wait > 0) {
  580. mi->sample_wait--;
  581. return -1;
  582. }
  583. if (!mi->sample_tries)
  584. return -1;
  585. sample_group = mi->sample_group;
  586. mg = &mi->groups[sample_group];
  587. sample_idx = sample_table[mg->column][mg->index];
  588. minstrel_next_sample_idx(mi);
  589. if (!(mg->supported & BIT(sample_idx)))
  590. return -1;
  591. mr = &mg->rates[sample_idx];
  592. sample_idx += sample_group * MCS_GROUP_RATES;
  593. /*
  594. * Sampling might add some overhead (RTS, no aggregation)
  595. * to the frame. Hence, don't use sampling for the currently
  596. * used rates.
  597. */
  598. if (sample_idx == mi->max_tp_rate ||
  599. sample_idx == mi->max_tp_rate2 ||
  600. sample_idx == mi->max_prob_rate)
  601. return -1;
  602. /*
  603. * Do not sample if the probability is already higher than 95%
  604. * to avoid wasting airtime.
  605. */
  606. if (mr->probability > MINSTREL_FRAC(95, 100))
  607. return -1;
  608. /*
  609. * Make sure that lower rates get sampled only occasionally,
  610. * if the link is working perfectly.
  611. */
  612. sample_dur = minstrel_get_duration(sample_idx);
  613. if (sample_dur >= minstrel_get_duration(mi->max_tp_rate2) &&
  614. (mi->max_prob_streams <
  615. minstrel_mcs_groups[sample_group].streams ||
  616. sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
  617. if (mr->sample_skipped < 20)
  618. return -1;
  619. if (mi->sample_slow++ > 2)
  620. return -1;
  621. }
  622. mi->sample_tries--;
  623. return sample_idx;
  624. }
  625. static void
  626. minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
  627. struct minstrel_ht_sta *mi, bool val)
  628. {
  629. u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
  630. if (!supported || !mi->cck_supported_short)
  631. return;
  632. if (supported & (mi->cck_supported_short << (val * 4)))
  633. return;
  634. supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
  635. mi->groups[MINSTREL_CCK_GROUP].supported = supported;
  636. }
  637. static void
  638. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  639. struct ieee80211_tx_rate_control *txrc)
  640. {
  641. const struct mcs_group *sample_group;
  642. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  643. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  644. struct minstrel_ht_sta_priv *msp = priv_sta;
  645. struct minstrel_ht_sta *mi = &msp->ht;
  646. struct minstrel_priv *mp = priv;
  647. int sample_idx;
  648. if (rate_control_send_low(sta, priv_sta, txrc))
  649. return;
  650. if (!msp->is_ht)
  651. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  652. info->flags |= mi->tx_flags;
  653. minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
  654. #ifdef CONFIG_MAC80211_DEBUGFS
  655. if (mp->fixed_rate_idx != -1)
  656. return;
  657. #endif
  658. /* Don't use EAPOL frames for sampling on non-mrr hw */
  659. if (mp->hw->max_rates == 1 &&
  660. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  661. sample_idx = -1;
  662. else
  663. sample_idx = minstrel_get_sample_rate(mp, mi);
  664. mi->total_packets++;
  665. /* wraparound */
  666. if (mi->total_packets == ~0) {
  667. mi->total_packets = 0;
  668. mi->sample_packets = 0;
  669. }
  670. if (sample_idx < 0)
  671. return;
  672. sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
  673. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  674. rate->count = 1;
  675. if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  676. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  677. rate->idx = mp->cck_rates[idx];
  678. rate->flags = 0;
  679. return;
  680. }
  681. rate->idx = sample_idx % MCS_GROUP_RATES +
  682. (sample_group->streams - 1) * 8;
  683. rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
  684. }
  685. static void
  686. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  687. struct ieee80211_supported_band *sband,
  688. struct ieee80211_sta *sta)
  689. {
  690. int i;
  691. if (sband->band != IEEE80211_BAND_2GHZ)
  692. return;
  693. if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
  694. return;
  695. mi->cck_supported = 0;
  696. mi->cck_supported_short = 0;
  697. for (i = 0; i < 4; i++) {
  698. if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
  699. continue;
  700. mi->cck_supported |= BIT(i);
  701. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  702. mi->cck_supported_short |= BIT(i);
  703. }
  704. mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
  705. }
  706. static void
  707. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  708. struct cfg80211_chan_def *chandef,
  709. struct ieee80211_sta *sta, void *priv_sta)
  710. {
  711. struct minstrel_priv *mp = priv;
  712. struct minstrel_ht_sta_priv *msp = priv_sta;
  713. struct minstrel_ht_sta *mi = &msp->ht;
  714. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  715. u16 sta_cap = sta->ht_cap.cap;
  716. int n_supported = 0;
  717. int ack_dur;
  718. int stbc;
  719. int i;
  720. /* fall back to the old minstrel for legacy stations */
  721. if (!sta->ht_cap.ht_supported)
  722. goto use_legacy;
  723. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  724. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS + 1);
  725. msp->is_ht = true;
  726. memset(mi, 0, sizeof(*mi));
  727. mi->sta = sta;
  728. mi->stats_update = jiffies;
  729. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  730. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  731. mi->overhead += ack_dur;
  732. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  733. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  734. /* When using MRR, sample more on the first attempt, without delay */
  735. if (mp->has_mrr) {
  736. mi->sample_count = 16;
  737. mi->sample_wait = 0;
  738. } else {
  739. mi->sample_count = 8;
  740. mi->sample_wait = 8;
  741. }
  742. mi->sample_tries = 4;
  743. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  744. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  745. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  746. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  747. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  748. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  749. mi->groups[i].supported = 0;
  750. if (i == MINSTREL_CCK_GROUP) {
  751. minstrel_ht_update_cck(mp, mi, sband, sta);
  752. continue;
  753. }
  754. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  755. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  756. if (!(sta_cap & IEEE80211_HT_CAP_SGI_40))
  757. continue;
  758. } else {
  759. if (!(sta_cap & IEEE80211_HT_CAP_SGI_20))
  760. continue;
  761. }
  762. }
  763. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  764. sta->bandwidth < IEEE80211_STA_RX_BW_40)
  765. continue;
  766. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  767. if (sta->smps_mode == IEEE80211_SMPS_STATIC &&
  768. minstrel_mcs_groups[i].streams > 1)
  769. continue;
  770. mi->groups[i].supported =
  771. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  772. if (mi->groups[i].supported)
  773. n_supported++;
  774. }
  775. if (!n_supported)
  776. goto use_legacy;
  777. /* create an initial rate table with the lowest supported rates */
  778. minstrel_ht_update_stats(mp, mi);
  779. minstrel_ht_update_rates(mp, mi);
  780. return;
  781. use_legacy:
  782. msp->is_ht = false;
  783. memset(&msp->legacy, 0, sizeof(msp->legacy));
  784. msp->legacy.r = msp->ratelist;
  785. msp->legacy.sample_table = msp->sample_table;
  786. return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
  787. &msp->legacy);
  788. }
  789. static void
  790. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  791. struct cfg80211_chan_def *chandef,
  792. struct ieee80211_sta *sta, void *priv_sta)
  793. {
  794. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  795. }
  796. static void
  797. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  798. struct cfg80211_chan_def *chandef,
  799. struct ieee80211_sta *sta, void *priv_sta,
  800. u32 changed)
  801. {
  802. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  803. }
  804. static void *
  805. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  806. {
  807. struct ieee80211_supported_band *sband;
  808. struct minstrel_ht_sta_priv *msp;
  809. struct minstrel_priv *mp = priv;
  810. struct ieee80211_hw *hw = mp->hw;
  811. int max_rates = 0;
  812. int i;
  813. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  814. sband = hw->wiphy->bands[i];
  815. if (sband && sband->n_bitrates > max_rates)
  816. max_rates = sband->n_bitrates;
  817. }
  818. msp = kzalloc(sizeof(*msp), gfp);
  819. if (!msp)
  820. return NULL;
  821. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  822. if (!msp->ratelist)
  823. goto error;
  824. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  825. if (!msp->sample_table)
  826. goto error1;
  827. return msp;
  828. error1:
  829. kfree(msp->ratelist);
  830. error:
  831. kfree(msp);
  832. return NULL;
  833. }
  834. static void
  835. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  836. {
  837. struct minstrel_ht_sta_priv *msp = priv_sta;
  838. kfree(msp->sample_table);
  839. kfree(msp->ratelist);
  840. kfree(msp);
  841. }
  842. static void *
  843. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  844. {
  845. return mac80211_minstrel.alloc(hw, debugfsdir);
  846. }
  847. static void
  848. minstrel_ht_free(void *priv)
  849. {
  850. mac80211_minstrel.free(priv);
  851. }
  852. static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
  853. {
  854. struct minstrel_ht_sta_priv *msp = priv_sta;
  855. struct minstrel_ht_sta *mi = &msp->ht;
  856. int i, j;
  857. if (!msp->is_ht)
  858. return mac80211_minstrel.get_expected_throughput(priv_sta);
  859. i = mi->max_tp_rate / MCS_GROUP_RATES;
  860. j = mi->max_tp_rate % MCS_GROUP_RATES;
  861. /* convert cur_tp from pkt per second in kbps */
  862. return mi->groups[i].rates[j].cur_tp * AVG_PKT_SIZE * 8 / 1024;
  863. }
  864. static const struct rate_control_ops mac80211_minstrel_ht = {
  865. .name = "minstrel_ht",
  866. .tx_status = minstrel_ht_tx_status,
  867. .get_rate = minstrel_ht_get_rate,
  868. .rate_init = minstrel_ht_rate_init,
  869. .rate_update = minstrel_ht_rate_update,
  870. .alloc_sta = minstrel_ht_alloc_sta,
  871. .free_sta = minstrel_ht_free_sta,
  872. .alloc = minstrel_ht_alloc,
  873. .free = minstrel_ht_free,
  874. #ifdef CONFIG_MAC80211_DEBUGFS
  875. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  876. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  877. #endif
  878. .get_expected_throughput = minstrel_ht_get_expected_throughput,
  879. };
  880. static void __init init_sample_table(void)
  881. {
  882. int col, i, new_idx;
  883. u8 rnd[MCS_GROUP_RATES];
  884. memset(sample_table, 0xff, sizeof(sample_table));
  885. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  886. prandom_bytes(rnd, sizeof(rnd));
  887. for (i = 0; i < MCS_GROUP_RATES; i++) {
  888. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  889. while (sample_table[col][new_idx] != 0xff)
  890. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  891. sample_table[col][new_idx] = i;
  892. }
  893. }
  894. }
  895. int __init
  896. rc80211_minstrel_ht_init(void)
  897. {
  898. init_sample_table();
  899. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  900. }
  901. void
  902. rc80211_minstrel_ht_exit(void)
  903. {
  904. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  905. }