rc80211_minstrel_ht.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 / MCS_GROUP_RATES) + 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. * Find & sort topmost throughput rates
  202. *
  203. * If multiple rates provide equal throughput the sorting is based on their
  204. * current success probability. Higher success probability is preferred among
  205. * MCS groups, CCK rates do not provide aggregation and are therefore at last.
  206. */
  207. static void
  208. minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u8 index,
  209. u8 *tp_list)
  210. {
  211. int cur_group, cur_idx, cur_thr, cur_prob;
  212. int tmp_group, tmp_idx, tmp_thr, tmp_prob;
  213. int j = MAX_THR_RATES;
  214. cur_group = index / MCS_GROUP_RATES;
  215. cur_idx = index % MCS_GROUP_RATES;
  216. cur_thr = mi->groups[cur_group].rates[cur_idx].cur_tp;
  217. cur_prob = mi->groups[cur_group].rates[cur_idx].probability;
  218. tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
  219. tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
  220. tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
  221. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
  222. while (j > 0 && (cur_thr > tmp_thr ||
  223. (cur_thr == tmp_thr && cur_prob > tmp_prob))) {
  224. j--;
  225. tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
  226. tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
  227. tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
  228. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
  229. }
  230. if (j < MAX_THR_RATES - 1) {
  231. memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
  232. (MAX_THR_RATES - (j + 1))));
  233. }
  234. if (j < MAX_THR_RATES)
  235. tp_list[j] = index;
  236. }
  237. /*
  238. * Find and set the topmost probability rate per sta and per group
  239. */
  240. static void
  241. minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u8 index)
  242. {
  243. struct minstrel_mcs_group_data *mg;
  244. struct minstrel_rate_stats *mr;
  245. int tmp_group, tmp_idx, tmp_tp, tmp_prob, max_tp_group;
  246. mg = &mi->groups[index / MCS_GROUP_RATES];
  247. mr = &mg->rates[index % MCS_GROUP_RATES];
  248. tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
  249. tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
  250. tmp_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
  251. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability;
  252. /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
  253. * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
  254. max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
  255. if((index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) &&
  256. (max_tp_group != MINSTREL_CCK_GROUP))
  257. return;
  258. if (mr->probability > MINSTREL_FRAC(75, 100)) {
  259. if (mr->cur_tp > tmp_tp)
  260. mi->max_prob_rate = index;
  261. if (mr->cur_tp > mg->rates[mg->max_group_prob_rate].cur_tp)
  262. mg->max_group_prob_rate = index;
  263. } else {
  264. if (mr->probability > tmp_prob)
  265. mi->max_prob_rate = index;
  266. if (mr->probability > mg->rates[mg->max_group_prob_rate].probability)
  267. mg->max_group_prob_rate = index;
  268. }
  269. }
  270. /*
  271. * Assign new rate set per sta and use CCK rates only if the fastest
  272. * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
  273. * rate sets where MCS and CCK rates are mixed, because CCK rates can
  274. * not use aggregation.
  275. */
  276. static void
  277. minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
  278. u8 tmp_mcs_tp_rate[MAX_THR_RATES],
  279. u8 tmp_cck_tp_rate[MAX_THR_RATES])
  280. {
  281. unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp;
  282. int i;
  283. tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES;
  284. tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES;
  285. tmp_cck_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
  286. tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
  287. tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
  288. tmp_mcs_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp;
  289. if (tmp_cck_tp > tmp_mcs_tp) {
  290. for(i = 0; i < MAX_THR_RATES; i++) {
  291. minstrel_ht_sort_best_tp_rates(mi, tmp_cck_tp_rate[i],
  292. tmp_mcs_tp_rate);
  293. }
  294. }
  295. }
  296. /*
  297. * Try to increase robustness of max_prob rate by decrease number of
  298. * streams if possible.
  299. */
  300. static inline void
  301. minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
  302. {
  303. struct minstrel_mcs_group_data *mg;
  304. struct minstrel_rate_stats *mr;
  305. int tmp_max_streams, group;
  306. int tmp_tp = 0;
  307. tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
  308. MCS_GROUP_RATES].streams;
  309. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  310. mg = &mi->groups[group];
  311. if (!mg->supported || group == MINSTREL_CCK_GROUP)
  312. continue;
  313. mr = minstrel_get_ratestats(mi, mg->max_group_prob_rate);
  314. if (tmp_tp < mr->cur_tp &&
  315. (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
  316. mi->max_prob_rate = mg->max_group_prob_rate;
  317. tmp_tp = mr->cur_tp;
  318. }
  319. }
  320. }
  321. /*
  322. * Update rate statistics and select new primary rates
  323. *
  324. * Rules for rate selection:
  325. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  326. * probability and throughput during strong fluctuations
  327. * - as long as the max prob rate has a probability of more than 75%, pick
  328. * higher throughput rates, even if the probablity is a bit lower
  329. */
  330. static void
  331. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  332. {
  333. struct minstrel_mcs_group_data *mg;
  334. struct minstrel_rate_stats *mr;
  335. int group, i, j;
  336. u8 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
  337. u8 tmp_cck_tp_rate[MAX_THR_RATES], index;
  338. if (mi->ampdu_packets > 0) {
  339. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  340. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  341. mi->ampdu_len = 0;
  342. mi->ampdu_packets = 0;
  343. }
  344. mi->sample_slow = 0;
  345. mi->sample_count = 0;
  346. /* Initialize global rate indexes */
  347. for(j = 0; j < MAX_THR_RATES; j++){
  348. tmp_mcs_tp_rate[j] = 0;
  349. tmp_cck_tp_rate[j] = 0;
  350. }
  351. /* Find best rate sets within all MCS groups*/
  352. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  353. mg = &mi->groups[group];
  354. if (!mg->supported)
  355. continue;
  356. mi->sample_count++;
  357. /* (re)Initialize group rate indexes */
  358. for(j = 0; j < MAX_THR_RATES; j++)
  359. tmp_group_tp_rate[j] = group;
  360. for (i = 0; i < MCS_GROUP_RATES; i++) {
  361. if (!(mg->supported & BIT(i)))
  362. continue;
  363. index = MCS_GROUP_RATES * group + i;
  364. mr = &mg->rates[i];
  365. mr->retry_updated = false;
  366. minstrel_calc_rate_ewma(mr);
  367. minstrel_ht_calc_tp(mi, group, i);
  368. if (!mr->cur_tp)
  369. continue;
  370. /* Find max throughput rate set */
  371. if (group != MINSTREL_CCK_GROUP) {
  372. minstrel_ht_sort_best_tp_rates(mi, index,
  373. tmp_mcs_tp_rate);
  374. } else if (group == MINSTREL_CCK_GROUP) {
  375. minstrel_ht_sort_best_tp_rates(mi, index,
  376. tmp_cck_tp_rate);
  377. }
  378. /* Find max throughput rate set within a group */
  379. minstrel_ht_sort_best_tp_rates(mi, index,
  380. tmp_group_tp_rate);
  381. /* Find max probability rate per group and global */
  382. minstrel_ht_set_best_prob_rate(mi, index);
  383. }
  384. memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
  385. sizeof(mg->max_group_tp_rate));
  386. }
  387. /* Assign new rate set per sta */
  388. minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, tmp_cck_tp_rate);
  389. memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
  390. /* Try to increase robustness of max_prob_rate*/
  391. minstrel_ht_prob_rate_reduce_streams(mi);
  392. /* try to sample all available rates during each interval */
  393. mi->sample_count *= 8;
  394. #ifdef CONFIG_MAC80211_DEBUGFS
  395. /* use fixed index if set */
  396. if (mp->fixed_rate_idx != -1) {
  397. for (i = 0; i < 4; i++)
  398. mi->max_tp_rate[i] = mp->fixed_rate_idx;
  399. mi->max_prob_rate = mp->fixed_rate_idx;
  400. }
  401. #endif
  402. /* Reset update timer */
  403. mi->stats_update = jiffies;
  404. }
  405. static bool
  406. minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
  407. {
  408. if (rate->idx < 0)
  409. return false;
  410. if (!rate->count)
  411. return false;
  412. if (rate->flags & IEEE80211_TX_RC_MCS)
  413. return true;
  414. return rate->idx == mp->cck_rates[0] ||
  415. rate->idx == mp->cck_rates[1] ||
  416. rate->idx == mp->cck_rates[2] ||
  417. rate->idx == mp->cck_rates[3];
  418. }
  419. static void
  420. minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
  421. {
  422. struct minstrel_mcs_group_data *mg;
  423. for (;;) {
  424. mi->sample_group++;
  425. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  426. mg = &mi->groups[mi->sample_group];
  427. if (!mg->supported)
  428. continue;
  429. if (++mg->index >= MCS_GROUP_RATES) {
  430. mg->index = 0;
  431. if (++mg->column >= ARRAY_SIZE(sample_table))
  432. mg->column = 0;
  433. }
  434. break;
  435. }
  436. }
  437. static void
  438. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u8 *idx, bool primary)
  439. {
  440. int group, orig_group;
  441. orig_group = group = *idx / MCS_GROUP_RATES;
  442. while (group > 0) {
  443. group--;
  444. if (!mi->groups[group].supported)
  445. continue;
  446. if (minstrel_mcs_groups[group].streams >
  447. minstrel_mcs_groups[orig_group].streams)
  448. continue;
  449. if (primary)
  450. *idx = mi->groups[group].max_group_tp_rate[0];
  451. else
  452. *idx = mi->groups[group].max_group_tp_rate[1];
  453. break;
  454. }
  455. }
  456. static void
  457. minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
  458. {
  459. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  460. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  461. u16 tid;
  462. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  463. return;
  464. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  465. return;
  466. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  467. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  468. return;
  469. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  470. return;
  471. ieee80211_start_tx_ba_session(pubsta, tid, 5000);
  472. }
  473. static void
  474. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  475. struct ieee80211_sta *sta, void *priv_sta,
  476. struct sk_buff *skb)
  477. {
  478. struct minstrel_ht_sta_priv *msp = priv_sta;
  479. struct minstrel_ht_sta *mi = &msp->ht;
  480. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  481. struct ieee80211_tx_rate *ar = info->status.rates;
  482. struct minstrel_rate_stats *rate, *rate2;
  483. struct minstrel_priv *mp = priv;
  484. bool last, update = false;
  485. int i;
  486. if (!msp->is_ht)
  487. return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
  488. /* This packet was aggregated but doesn't carry status info */
  489. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  490. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  491. return;
  492. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  493. info->status.ampdu_ack_len =
  494. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  495. info->status.ampdu_len = 1;
  496. }
  497. mi->ampdu_packets++;
  498. mi->ampdu_len += info->status.ampdu_len;
  499. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  500. mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  501. mi->sample_tries = 1;
  502. mi->sample_count--;
  503. }
  504. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  505. mi->sample_packets += info->status.ampdu_len;
  506. last = !minstrel_ht_txstat_valid(mp, &ar[0]);
  507. for (i = 0; !last; i++) {
  508. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  509. !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
  510. rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
  511. if (last)
  512. rate->success += info->status.ampdu_ack_len;
  513. rate->attempts += ar[i].count * info->status.ampdu_len;
  514. }
  515. /*
  516. * check for sudden death of spatial multiplexing,
  517. * downgrade to a lower number of streams if necessary.
  518. */
  519. rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
  520. if (rate->attempts > 30 &&
  521. MINSTREL_FRAC(rate->success, rate->attempts) <
  522. MINSTREL_FRAC(20, 100)) {
  523. minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
  524. update = true;
  525. }
  526. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
  527. if (rate2->attempts > 30 &&
  528. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  529. MINSTREL_FRAC(20, 100)) {
  530. minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
  531. update = true;
  532. }
  533. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  534. update = true;
  535. minstrel_ht_update_stats(mp, mi);
  536. if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
  537. mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
  538. minstrel_aggr_check(sta, skb);
  539. }
  540. if (update)
  541. minstrel_ht_update_rates(mp, mi);
  542. }
  543. static void
  544. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  545. int index)
  546. {
  547. struct minstrel_rate_stats *mr;
  548. const struct mcs_group *group;
  549. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  550. unsigned int cw = mp->cw_min;
  551. unsigned int ctime = 0;
  552. unsigned int t_slot = 9; /* FIXME */
  553. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  554. unsigned int overhead = 0, overhead_rtscts = 0;
  555. mr = minstrel_get_ratestats(mi, index);
  556. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  557. mr->retry_count = 1;
  558. mr->retry_count_rtscts = 1;
  559. return;
  560. }
  561. mr->retry_count = 2;
  562. mr->retry_count_rtscts = 2;
  563. mr->retry_updated = true;
  564. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  565. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
  566. /* Contention time for first 2 tries */
  567. ctime = (t_slot * cw) >> 1;
  568. cw = min((cw << 1) | 1, mp->cw_max);
  569. ctime += (t_slot * cw) >> 1;
  570. cw = min((cw << 1) | 1, mp->cw_max);
  571. if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
  572. overhead = mi->overhead;
  573. overhead_rtscts = mi->overhead_rtscts;
  574. }
  575. /* Total TX time for data and Contention after first 2 tries */
  576. tx_time = ctime + 2 * (overhead + tx_time_data);
  577. tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
  578. /* See how many more tries we can fit inside segment size */
  579. do {
  580. /* Contention time for this try */
  581. ctime = (t_slot * cw) >> 1;
  582. cw = min((cw << 1) | 1, mp->cw_max);
  583. /* Total TX time after this try */
  584. tx_time += ctime + overhead + tx_time_data;
  585. tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
  586. if (tx_time_rtscts < mp->segment_size)
  587. mr->retry_count_rtscts++;
  588. } while ((tx_time < mp->segment_size) &&
  589. (++mr->retry_count < mp->max_retry));
  590. }
  591. static void
  592. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  593. struct ieee80211_sta_rates *ratetbl, int offset, int index)
  594. {
  595. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  596. struct minstrel_rate_stats *mr;
  597. u8 idx;
  598. u16 flags;
  599. mr = minstrel_get_ratestats(mi, index);
  600. if (!mr->retry_updated)
  601. minstrel_calc_retransmit(mp, mi, index);
  602. if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
  603. ratetbl->rate[offset].count = 2;
  604. ratetbl->rate[offset].count_rts = 2;
  605. ratetbl->rate[offset].count_cts = 2;
  606. } else {
  607. ratetbl->rate[offset].count = mr->retry_count;
  608. ratetbl->rate[offset].count_cts = mr->retry_count;
  609. ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
  610. }
  611. if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  612. idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
  613. flags = 0;
  614. } else {
  615. idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
  616. flags = IEEE80211_TX_RC_MCS | group->flags;
  617. }
  618. if (offset > 0) {
  619. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  620. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  621. }
  622. ratetbl->rate[offset].idx = idx;
  623. ratetbl->rate[offset].flags = flags;
  624. }
  625. static void
  626. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  627. {
  628. struct ieee80211_sta_rates *rates;
  629. int i = 0;
  630. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  631. if (!rates)
  632. return;
  633. /* Start with max_tp_rate[0] */
  634. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
  635. if (mp->hw->max_rates >= 3) {
  636. /* At least 3 tx rates supported, use max_tp_rate[1] next */
  637. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
  638. }
  639. if (mp->hw->max_rates >= 2) {
  640. /*
  641. * At least 2 tx rates supported, use max_prob_rate next */
  642. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  643. }
  644. rates->rate[i].idx = -1;
  645. rate_control_set_rates(mp->hw, mi->sta, rates);
  646. }
  647. static inline int
  648. minstrel_get_duration(int index)
  649. {
  650. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  651. return group->duration[index % MCS_GROUP_RATES];
  652. }
  653. static int
  654. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  655. {
  656. struct minstrel_rate_stats *mr;
  657. struct minstrel_mcs_group_data *mg;
  658. unsigned int sample_dur, sample_group, cur_max_tp_streams;
  659. int sample_idx = 0;
  660. if (mi->sample_wait > 0) {
  661. mi->sample_wait--;
  662. return -1;
  663. }
  664. if (!mi->sample_tries)
  665. return -1;
  666. sample_group = mi->sample_group;
  667. mg = &mi->groups[sample_group];
  668. sample_idx = sample_table[mg->column][mg->index];
  669. minstrel_next_sample_idx(mi);
  670. if (!(mg->supported & BIT(sample_idx)))
  671. return -1;
  672. mr = &mg->rates[sample_idx];
  673. sample_idx += sample_group * MCS_GROUP_RATES;
  674. /*
  675. * Sampling might add some overhead (RTS, no aggregation)
  676. * to the frame. Hence, don't use sampling for the currently
  677. * used rates.
  678. */
  679. if (sample_idx == mi->max_tp_rate[0] ||
  680. sample_idx == mi->max_tp_rate[1] ||
  681. sample_idx == mi->max_prob_rate)
  682. return -1;
  683. /*
  684. * Do not sample if the probability is already higher than 95%
  685. * to avoid wasting airtime.
  686. */
  687. if (mr->probability > MINSTREL_FRAC(95, 100))
  688. return -1;
  689. /*
  690. * Make sure that lower rates get sampled only occasionally,
  691. * if the link is working perfectly.
  692. */
  693. cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
  694. MCS_GROUP_RATES].streams;
  695. sample_dur = minstrel_get_duration(sample_idx);
  696. if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) &&
  697. (cur_max_tp_streams - 1 <
  698. minstrel_mcs_groups[sample_group].streams ||
  699. sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
  700. if (mr->sample_skipped < 20)
  701. return -1;
  702. if (mi->sample_slow++ > 2)
  703. return -1;
  704. }
  705. mi->sample_tries--;
  706. return sample_idx;
  707. }
  708. static void
  709. minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
  710. struct minstrel_ht_sta *mi, bool val)
  711. {
  712. u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
  713. if (!supported || !mi->cck_supported_short)
  714. return;
  715. if (supported & (mi->cck_supported_short << (val * 4)))
  716. return;
  717. supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
  718. mi->groups[MINSTREL_CCK_GROUP].supported = supported;
  719. }
  720. static void
  721. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  722. struct ieee80211_tx_rate_control *txrc)
  723. {
  724. const struct mcs_group *sample_group;
  725. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  726. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  727. struct minstrel_ht_sta_priv *msp = priv_sta;
  728. struct minstrel_ht_sta *mi = &msp->ht;
  729. struct minstrel_priv *mp = priv;
  730. int sample_idx;
  731. if (rate_control_send_low(sta, priv_sta, txrc))
  732. return;
  733. if (!msp->is_ht)
  734. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  735. info->flags |= mi->tx_flags;
  736. minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
  737. #ifdef CONFIG_MAC80211_DEBUGFS
  738. if (mp->fixed_rate_idx != -1)
  739. return;
  740. #endif
  741. /* Don't use EAPOL frames for sampling on non-mrr hw */
  742. if (mp->hw->max_rates == 1 &&
  743. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  744. sample_idx = -1;
  745. else
  746. sample_idx = minstrel_get_sample_rate(mp, mi);
  747. mi->total_packets++;
  748. /* wraparound */
  749. if (mi->total_packets == ~0) {
  750. mi->total_packets = 0;
  751. mi->sample_packets = 0;
  752. }
  753. if (sample_idx < 0)
  754. return;
  755. sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
  756. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  757. rate->count = 1;
  758. if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  759. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  760. rate->idx = mp->cck_rates[idx];
  761. rate->flags = 0;
  762. return;
  763. }
  764. rate->idx = sample_idx % MCS_GROUP_RATES +
  765. (sample_group->streams - 1) * 8;
  766. rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
  767. }
  768. static void
  769. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  770. struct ieee80211_supported_band *sband,
  771. struct ieee80211_sta *sta)
  772. {
  773. int i;
  774. if (sband->band != IEEE80211_BAND_2GHZ)
  775. return;
  776. if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
  777. return;
  778. mi->cck_supported = 0;
  779. mi->cck_supported_short = 0;
  780. for (i = 0; i < 4; i++) {
  781. if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
  782. continue;
  783. mi->cck_supported |= BIT(i);
  784. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  785. mi->cck_supported_short |= BIT(i);
  786. }
  787. mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
  788. }
  789. static void
  790. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  791. struct cfg80211_chan_def *chandef,
  792. struct ieee80211_sta *sta, void *priv_sta)
  793. {
  794. struct minstrel_priv *mp = priv;
  795. struct minstrel_ht_sta_priv *msp = priv_sta;
  796. struct minstrel_ht_sta *mi = &msp->ht;
  797. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  798. u16 sta_cap = sta->ht_cap.cap;
  799. int n_supported = 0;
  800. int ack_dur;
  801. int stbc;
  802. int i;
  803. /* fall back to the old minstrel for legacy stations */
  804. if (!sta->ht_cap.ht_supported)
  805. goto use_legacy;
  806. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  807. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS + 1);
  808. msp->is_ht = true;
  809. memset(mi, 0, sizeof(*mi));
  810. mi->sta = sta;
  811. mi->stats_update = jiffies;
  812. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  813. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  814. mi->overhead += ack_dur;
  815. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  816. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  817. /* When using MRR, sample more on the first attempt, without delay */
  818. if (mp->has_mrr) {
  819. mi->sample_count = 16;
  820. mi->sample_wait = 0;
  821. } else {
  822. mi->sample_count = 8;
  823. mi->sample_wait = 8;
  824. }
  825. mi->sample_tries = 4;
  826. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  827. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  828. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  829. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  830. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  831. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  832. mi->groups[i].supported = 0;
  833. if (i == MINSTREL_CCK_GROUP) {
  834. minstrel_ht_update_cck(mp, mi, sband, sta);
  835. continue;
  836. }
  837. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  838. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  839. if (!(sta_cap & IEEE80211_HT_CAP_SGI_40))
  840. continue;
  841. } else {
  842. if (!(sta_cap & IEEE80211_HT_CAP_SGI_20))
  843. continue;
  844. }
  845. }
  846. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  847. sta->bandwidth < IEEE80211_STA_RX_BW_40)
  848. continue;
  849. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  850. if (sta->smps_mode == IEEE80211_SMPS_STATIC &&
  851. minstrel_mcs_groups[i].streams > 1)
  852. continue;
  853. mi->groups[i].supported =
  854. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  855. if (mi->groups[i].supported)
  856. n_supported++;
  857. }
  858. if (!n_supported)
  859. goto use_legacy;
  860. /* create an initial rate table with the lowest supported rates */
  861. minstrel_ht_update_stats(mp, mi);
  862. minstrel_ht_update_rates(mp, mi);
  863. return;
  864. use_legacy:
  865. msp->is_ht = false;
  866. memset(&msp->legacy, 0, sizeof(msp->legacy));
  867. msp->legacy.r = msp->ratelist;
  868. msp->legacy.sample_table = msp->sample_table;
  869. return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
  870. &msp->legacy);
  871. }
  872. static void
  873. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  874. struct cfg80211_chan_def *chandef,
  875. struct ieee80211_sta *sta, void *priv_sta)
  876. {
  877. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  878. }
  879. static void
  880. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  881. struct cfg80211_chan_def *chandef,
  882. struct ieee80211_sta *sta, void *priv_sta,
  883. u32 changed)
  884. {
  885. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  886. }
  887. static void *
  888. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  889. {
  890. struct ieee80211_supported_band *sband;
  891. struct minstrel_ht_sta_priv *msp;
  892. struct minstrel_priv *mp = priv;
  893. struct ieee80211_hw *hw = mp->hw;
  894. int max_rates = 0;
  895. int i;
  896. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  897. sband = hw->wiphy->bands[i];
  898. if (sband && sband->n_bitrates > max_rates)
  899. max_rates = sband->n_bitrates;
  900. }
  901. msp = kzalloc(sizeof(*msp), gfp);
  902. if (!msp)
  903. return NULL;
  904. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  905. if (!msp->ratelist)
  906. goto error;
  907. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  908. if (!msp->sample_table)
  909. goto error1;
  910. return msp;
  911. error1:
  912. kfree(msp->ratelist);
  913. error:
  914. kfree(msp);
  915. return NULL;
  916. }
  917. static void
  918. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  919. {
  920. struct minstrel_ht_sta_priv *msp = priv_sta;
  921. kfree(msp->sample_table);
  922. kfree(msp->ratelist);
  923. kfree(msp);
  924. }
  925. static void *
  926. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  927. {
  928. return mac80211_minstrel.alloc(hw, debugfsdir);
  929. }
  930. static void
  931. minstrel_ht_free(void *priv)
  932. {
  933. mac80211_minstrel.free(priv);
  934. }
  935. static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
  936. {
  937. struct minstrel_ht_sta_priv *msp = priv_sta;
  938. struct minstrel_ht_sta *mi = &msp->ht;
  939. int i, j;
  940. if (!msp->is_ht)
  941. return mac80211_minstrel.get_expected_throughput(priv_sta);
  942. i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
  943. j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
  944. /* convert cur_tp from pkt per second in kbps */
  945. return mi->groups[i].rates[j].cur_tp * AVG_PKT_SIZE * 8 / 1024;
  946. }
  947. static const struct rate_control_ops mac80211_minstrel_ht = {
  948. .name = "minstrel_ht",
  949. .tx_status = minstrel_ht_tx_status,
  950. .get_rate = minstrel_ht_get_rate,
  951. .rate_init = minstrel_ht_rate_init,
  952. .rate_update = minstrel_ht_rate_update,
  953. .alloc_sta = minstrel_ht_alloc_sta,
  954. .free_sta = minstrel_ht_free_sta,
  955. .alloc = minstrel_ht_alloc,
  956. .free = minstrel_ht_free,
  957. #ifdef CONFIG_MAC80211_DEBUGFS
  958. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  959. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  960. #endif
  961. .get_expected_throughput = minstrel_ht_get_expected_throughput,
  962. };
  963. static void __init init_sample_table(void)
  964. {
  965. int col, i, new_idx;
  966. u8 rnd[MCS_GROUP_RATES];
  967. memset(sample_table, 0xff, sizeof(sample_table));
  968. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  969. prandom_bytes(rnd, sizeof(rnd));
  970. for (i = 0; i < MCS_GROUP_RATES; i++) {
  971. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  972. while (sample_table[col][new_idx] != 0xff)
  973. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  974. sample_table[col][new_idx] = i;
  975. }
  976. }
  977. }
  978. int __init
  979. rc80211_minstrel_ht_init(void)
  980. {
  981. init_sample_table();
  982. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  983. }
  984. void
  985. rc80211_minstrel_ht_exit(void)
  986. {
  987. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  988. }