rc80211_minstrel_ht.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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/moduleparam.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/mac80211.h>
  16. #include "rate.h"
  17. #include "rc80211_minstrel.h"
  18. #include "rc80211_minstrel_ht.h"
  19. #define AVG_AMPDU_SIZE 16
  20. #define AVG_PKT_SIZE 1200
  21. /* Number of bits for an average sized packet */
  22. #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
  23. /* Number of symbols for a packet with (bps) bits per symbol */
  24. #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
  25. /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
  26. #define MCS_SYMBOL_TIME(sgi, syms) \
  27. (sgi ? \
  28. ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
  29. ((syms) * 1000) << 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) \
  33. (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
  34. #define BW_20 0
  35. #define BW_40 1
  36. #define BW_80 2
  37. /*
  38. * Define group sort order: HT40 -> SGI -> #streams
  39. */
  40. #define GROUP_IDX(_streams, _sgi, _ht40) \
  41. MINSTREL_HT_GROUP_0 + \
  42. MINSTREL_MAX_STREAMS * 2 * _ht40 + \
  43. MINSTREL_MAX_STREAMS * _sgi + \
  44. _streams - 1
  45. /* MCS rate information for an MCS group */
  46. #define MCS_GROUP(_streams, _sgi, _ht40) \
  47. [GROUP_IDX(_streams, _sgi, _ht40)] = { \
  48. .streams = _streams, \
  49. .flags = \
  50. IEEE80211_TX_RC_MCS | \
  51. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  52. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  53. .duration = { \
  54. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
  55. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
  56. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
  57. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
  58. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
  59. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
  60. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
  61. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
  62. } \
  63. }
  64. #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
  65. (MINSTREL_VHT_GROUP_0 + \
  66. MINSTREL_MAX_STREAMS * 2 * (_bw) + \
  67. MINSTREL_MAX_STREAMS * (_sgi) + \
  68. (_streams) - 1)
  69. #define BW2VBPS(_bw, r3, r2, r1) \
  70. (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
  71. #define VHT_GROUP(_streams, _sgi, _bw) \
  72. [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
  73. .streams = _streams, \
  74. .flags = \
  75. IEEE80211_TX_RC_VHT_MCS | \
  76. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  77. (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \
  78. _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  79. .duration = { \
  80. MCS_DURATION(_streams, _sgi, \
  81. BW2VBPS(_bw, 117, 54, 26)), \
  82. MCS_DURATION(_streams, _sgi, \
  83. BW2VBPS(_bw, 234, 108, 52)), \
  84. MCS_DURATION(_streams, _sgi, \
  85. BW2VBPS(_bw, 351, 162, 78)), \
  86. MCS_DURATION(_streams, _sgi, \
  87. BW2VBPS(_bw, 468, 216, 104)), \
  88. MCS_DURATION(_streams, _sgi, \
  89. BW2VBPS(_bw, 702, 324, 156)), \
  90. MCS_DURATION(_streams, _sgi, \
  91. BW2VBPS(_bw, 936, 432, 208)), \
  92. MCS_DURATION(_streams, _sgi, \
  93. BW2VBPS(_bw, 1053, 486, 234)), \
  94. MCS_DURATION(_streams, _sgi, \
  95. BW2VBPS(_bw, 1170, 540, 260)), \
  96. MCS_DURATION(_streams, _sgi, \
  97. BW2VBPS(_bw, 1404, 648, 312)), \
  98. MCS_DURATION(_streams, _sgi, \
  99. BW2VBPS(_bw, 1560, 720, 346)) \
  100. } \
  101. }
  102. #define CCK_DURATION(_bitrate, _short, _len) \
  103. (1000 * (10 /* SIFS */ + \
  104. (_short ? 72 + 24 : 144 + 48) + \
  105. (8 * (_len + 4) * 10) / (_bitrate)))
  106. #define CCK_ACK_DURATION(_bitrate, _short) \
  107. (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \
  108. CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE))
  109. #define CCK_DURATION_LIST(_short) \
  110. CCK_ACK_DURATION(10, _short), \
  111. CCK_ACK_DURATION(20, _short), \
  112. CCK_ACK_DURATION(55, _short), \
  113. CCK_ACK_DURATION(110, _short)
  114. #define CCK_GROUP \
  115. [MINSTREL_CCK_GROUP] = { \
  116. .streams = 0, \
  117. .flags = 0, \
  118. .duration = { \
  119. CCK_DURATION_LIST(false), \
  120. CCK_DURATION_LIST(true) \
  121. } \
  122. }
  123. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  124. static bool minstrel_vht_only = true;
  125. module_param(minstrel_vht_only, bool, 0644);
  126. MODULE_PARM_DESC(minstrel_vht_only,
  127. "Use only VHT rates when VHT is supported by sta.");
  128. #endif
  129. /*
  130. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  131. * groups, based on the number of streams and flags (HT40, SGI) that they
  132. * use.
  133. *
  134. * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
  135. * BW -> SGI -> #streams
  136. */
  137. const struct mcs_group minstrel_mcs_groups[] = {
  138. MCS_GROUP(1, 0, BW_20),
  139. MCS_GROUP(2, 0, BW_20),
  140. #if MINSTREL_MAX_STREAMS >= 3
  141. MCS_GROUP(3, 0, BW_20),
  142. #endif
  143. MCS_GROUP(1, 1, BW_20),
  144. MCS_GROUP(2, 1, BW_20),
  145. #if MINSTREL_MAX_STREAMS >= 3
  146. MCS_GROUP(3, 1, BW_20),
  147. #endif
  148. MCS_GROUP(1, 0, BW_40),
  149. MCS_GROUP(2, 0, BW_40),
  150. #if MINSTREL_MAX_STREAMS >= 3
  151. MCS_GROUP(3, 0, BW_40),
  152. #endif
  153. MCS_GROUP(1, 1, BW_40),
  154. MCS_GROUP(2, 1, BW_40),
  155. #if MINSTREL_MAX_STREAMS >= 3
  156. MCS_GROUP(3, 1, BW_40),
  157. #endif
  158. CCK_GROUP,
  159. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  160. VHT_GROUP(1, 0, BW_20),
  161. VHT_GROUP(2, 0, BW_20),
  162. #if MINSTREL_MAX_STREAMS >= 3
  163. VHT_GROUP(3, 0, BW_20),
  164. #endif
  165. VHT_GROUP(1, 1, BW_20),
  166. VHT_GROUP(2, 1, BW_20),
  167. #if MINSTREL_MAX_STREAMS >= 3
  168. VHT_GROUP(3, 1, BW_20),
  169. #endif
  170. VHT_GROUP(1, 0, BW_40),
  171. VHT_GROUP(2, 0, BW_40),
  172. #if MINSTREL_MAX_STREAMS >= 3
  173. VHT_GROUP(3, 0, BW_40),
  174. #endif
  175. VHT_GROUP(1, 1, BW_40),
  176. VHT_GROUP(2, 1, BW_40),
  177. #if MINSTREL_MAX_STREAMS >= 3
  178. VHT_GROUP(3, 1, BW_40),
  179. #endif
  180. VHT_GROUP(1, 0, BW_80),
  181. VHT_GROUP(2, 0, BW_80),
  182. #if MINSTREL_MAX_STREAMS >= 3
  183. VHT_GROUP(3, 0, BW_80),
  184. #endif
  185. VHT_GROUP(1, 1, BW_80),
  186. VHT_GROUP(2, 1, BW_80),
  187. #if MINSTREL_MAX_STREAMS >= 3
  188. VHT_GROUP(3, 1, BW_80),
  189. #endif
  190. #endif
  191. };
  192. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
  193. static void
  194. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
  195. /*
  196. * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
  197. * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
  198. *
  199. * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
  200. */
  201. static u16
  202. minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
  203. {
  204. u16 mask = 0;
  205. if (bw == BW_20) {
  206. if (nss != 3 && nss != 6)
  207. mask = BIT(9);
  208. } else if (bw == BW_80) {
  209. if (nss == 3 || nss == 7)
  210. mask = BIT(6);
  211. else if (nss == 6)
  212. mask = BIT(9);
  213. } else {
  214. WARN_ON(bw != BW_40);
  215. }
  216. switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
  217. case IEEE80211_VHT_MCS_SUPPORT_0_7:
  218. mask |= 0x300;
  219. break;
  220. case IEEE80211_VHT_MCS_SUPPORT_0_8:
  221. mask |= 0x200;
  222. break;
  223. case IEEE80211_VHT_MCS_SUPPORT_0_9:
  224. break;
  225. default:
  226. mask = 0x3ff;
  227. }
  228. return 0x3ff & ~mask;
  229. }
  230. /*
  231. * Look up an MCS group index based on mac80211 rate information
  232. */
  233. static int
  234. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  235. {
  236. return GROUP_IDX((rate->idx / 8) + 1,
  237. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  238. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
  239. }
  240. static int
  241. minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
  242. {
  243. return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
  244. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  245. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
  246. 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
  247. }
  248. static struct minstrel_rate_stats *
  249. minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  250. struct ieee80211_tx_rate *rate)
  251. {
  252. int group, idx;
  253. if (rate->flags & IEEE80211_TX_RC_MCS) {
  254. group = minstrel_ht_get_group_idx(rate);
  255. idx = rate->idx % 8;
  256. } else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
  257. group = minstrel_vht_get_group_idx(rate);
  258. idx = ieee80211_rate_get_vht_mcs(rate);
  259. } else {
  260. group = MINSTREL_CCK_GROUP;
  261. for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++)
  262. if (rate->idx == mp->cck_rates[idx])
  263. break;
  264. /* short preamble */
  265. if (!(mi->groups[group].supported & BIT(idx)))
  266. idx += 4;
  267. }
  268. return &mi->groups[group].rates[idx];
  269. }
  270. static inline struct minstrel_rate_stats *
  271. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  272. {
  273. return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
  274. }
  275. /*
  276. * Return current throughput based on the average A-MPDU length, taking into
  277. * account the expected number of retransmissions and their expected length
  278. */
  279. int
  280. minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
  281. int prob_ewma)
  282. {
  283. unsigned int nsecs = 0;
  284. /* do not account throughput if sucess prob is below 10% */
  285. if (prob_ewma < MINSTREL_FRAC(10, 100))
  286. return 0;
  287. if (group != MINSTREL_CCK_GROUP)
  288. nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
  289. nsecs += minstrel_mcs_groups[group].duration[rate];
  290. /*
  291. * For the throughput calculation, limit the probability value to 90% to
  292. * account for collision related packet error rate fluctuation
  293. * (prob is scaled - see MINSTREL_FRAC above)
  294. */
  295. if (prob_ewma > MINSTREL_FRAC(90, 100))
  296. return MINSTREL_TRUNC(100000 * ((MINSTREL_FRAC(90, 100) * 1000)
  297. / nsecs));
  298. else
  299. return MINSTREL_TRUNC(100000 * ((prob_ewma * 1000) / nsecs));
  300. }
  301. /*
  302. * Find & sort topmost throughput rates
  303. *
  304. * If multiple rates provide equal throughput the sorting is based on their
  305. * current success probability. Higher success probability is preferred among
  306. * MCS groups, CCK rates do not provide aggregation and are therefore at last.
  307. */
  308. static void
  309. minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
  310. u16 *tp_list)
  311. {
  312. int cur_group, cur_idx, cur_tp_avg, cur_prob;
  313. int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
  314. int j = MAX_THR_RATES;
  315. cur_group = index / MCS_GROUP_RATES;
  316. cur_idx = index % MCS_GROUP_RATES;
  317. cur_prob = mi->groups[cur_group].rates[cur_idx].prob_ewma;
  318. cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
  319. do {
  320. tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
  321. tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
  322. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
  323. tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
  324. tmp_prob);
  325. if (cur_tp_avg < tmp_tp_avg ||
  326. (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
  327. break;
  328. j--;
  329. } while (j > 0);
  330. if (j < MAX_THR_RATES - 1) {
  331. memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
  332. (MAX_THR_RATES - (j + 1))));
  333. }
  334. if (j < MAX_THR_RATES)
  335. tp_list[j] = index;
  336. }
  337. /*
  338. * Find and set the topmost probability rate per sta and per group
  339. */
  340. static void
  341. minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
  342. {
  343. struct minstrel_mcs_group_data *mg;
  344. struct minstrel_rate_stats *mrs;
  345. int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
  346. int max_tp_group, cur_tp_avg, cur_group, cur_idx;
  347. int max_gpr_group, max_gpr_idx;
  348. int max_gpr_tp_avg, max_gpr_prob;
  349. cur_group = index / MCS_GROUP_RATES;
  350. cur_idx = index % MCS_GROUP_RATES;
  351. mg = &mi->groups[index / MCS_GROUP_RATES];
  352. mrs = &mg->rates[index % MCS_GROUP_RATES];
  353. tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
  354. tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
  355. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
  356. tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  357. /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
  358. * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
  359. max_tp_group = mi->max_tp_rate[0] / MCS_GROUP_RATES;
  360. if((index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) &&
  361. (max_tp_group != MINSTREL_CCK_GROUP))
  362. return;
  363. max_gpr_group = mg->max_group_prob_rate / MCS_GROUP_RATES;
  364. max_gpr_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
  365. max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_ewma;
  366. if (mrs->prob_ewma > MINSTREL_FRAC(75, 100)) {
  367. cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
  368. mrs->prob_ewma);
  369. if (cur_tp_avg > tmp_tp_avg)
  370. mi->max_prob_rate = index;
  371. max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
  372. max_gpr_idx,
  373. max_gpr_prob);
  374. if (cur_tp_avg > max_gpr_tp_avg)
  375. mg->max_group_prob_rate = index;
  376. } else {
  377. if (mrs->prob_ewma > tmp_prob)
  378. mi->max_prob_rate = index;
  379. if (mrs->prob_ewma > max_gpr_prob)
  380. mg->max_group_prob_rate = index;
  381. }
  382. }
  383. /*
  384. * Assign new rate set per sta and use CCK rates only if the fastest
  385. * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
  386. * rate sets where MCS and CCK rates are mixed, because CCK rates can
  387. * not use aggregation.
  388. */
  389. static void
  390. minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
  391. u16 tmp_mcs_tp_rate[MAX_THR_RATES],
  392. u16 tmp_cck_tp_rate[MAX_THR_RATES])
  393. {
  394. unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
  395. int i;
  396. tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES;
  397. tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES;
  398. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
  399. tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  400. tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
  401. tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
  402. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
  403. tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  404. if (tmp_cck_tp > tmp_mcs_tp) {
  405. for(i = 0; i < MAX_THR_RATES; i++) {
  406. minstrel_ht_sort_best_tp_rates(mi, tmp_cck_tp_rate[i],
  407. tmp_mcs_tp_rate);
  408. }
  409. }
  410. }
  411. /*
  412. * Try to increase robustness of max_prob rate by decrease number of
  413. * streams if possible.
  414. */
  415. static inline void
  416. minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
  417. {
  418. struct minstrel_mcs_group_data *mg;
  419. int tmp_max_streams, group, tmp_idx, tmp_prob;
  420. int tmp_tp = 0;
  421. tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
  422. MCS_GROUP_RATES].streams;
  423. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  424. mg = &mi->groups[group];
  425. if (!mg->supported || group == MINSTREL_CCK_GROUP)
  426. continue;
  427. tmp_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
  428. tmp_prob = mi->groups[group].rates[tmp_idx].prob_ewma;
  429. if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
  430. (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
  431. mi->max_prob_rate = mg->max_group_prob_rate;
  432. tmp_tp = minstrel_ht_get_tp_avg(mi, group,
  433. tmp_idx,
  434. tmp_prob);
  435. }
  436. }
  437. }
  438. /*
  439. * Update rate statistics and select new primary rates
  440. *
  441. * Rules for rate selection:
  442. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  443. * probability and throughput during strong fluctuations
  444. * - as long as the max prob rate has a probability of more than 75%, pick
  445. * higher throughput rates, even if the probablity is a bit lower
  446. */
  447. static void
  448. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  449. {
  450. struct minstrel_mcs_group_data *mg;
  451. struct minstrel_rate_stats *mrs;
  452. int group, i, j, cur_prob;
  453. u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
  454. u16 tmp_cck_tp_rate[MAX_THR_RATES], index;
  455. if (mi->ampdu_packets > 0) {
  456. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  457. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  458. mi->ampdu_len = 0;
  459. mi->ampdu_packets = 0;
  460. }
  461. mi->sample_slow = 0;
  462. mi->sample_count = 0;
  463. /* Initialize global rate indexes */
  464. for(j = 0; j < MAX_THR_RATES; j++){
  465. tmp_mcs_tp_rate[j] = 0;
  466. tmp_cck_tp_rate[j] = 0;
  467. }
  468. /* Find best rate sets within all MCS groups*/
  469. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  470. mg = &mi->groups[group];
  471. if (!mg->supported)
  472. continue;
  473. mi->sample_count++;
  474. /* (re)Initialize group rate indexes */
  475. for(j = 0; j < MAX_THR_RATES; j++)
  476. tmp_group_tp_rate[j] = group;
  477. for (i = 0; i < MCS_GROUP_RATES; i++) {
  478. if (!(mg->supported & BIT(i)))
  479. continue;
  480. index = MCS_GROUP_RATES * group + i;
  481. mrs = &mg->rates[i];
  482. mrs->retry_updated = false;
  483. minstrel_calc_rate_stats(mrs);
  484. cur_prob = mrs->prob_ewma;
  485. if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
  486. continue;
  487. /* Find max throughput rate set */
  488. if (group != MINSTREL_CCK_GROUP) {
  489. minstrel_ht_sort_best_tp_rates(mi, index,
  490. tmp_mcs_tp_rate);
  491. } else if (group == MINSTREL_CCK_GROUP) {
  492. minstrel_ht_sort_best_tp_rates(mi, index,
  493. tmp_cck_tp_rate);
  494. }
  495. /* Find max throughput rate set within a group */
  496. minstrel_ht_sort_best_tp_rates(mi, index,
  497. tmp_group_tp_rate);
  498. /* Find max probability rate per group and global */
  499. minstrel_ht_set_best_prob_rate(mi, index);
  500. }
  501. memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
  502. sizeof(mg->max_group_tp_rate));
  503. }
  504. /* Assign new rate set per sta */
  505. minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, tmp_cck_tp_rate);
  506. memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
  507. /* Try to increase robustness of max_prob_rate*/
  508. minstrel_ht_prob_rate_reduce_streams(mi);
  509. /* try to sample all available rates during each interval */
  510. mi->sample_count *= 8;
  511. #ifdef CONFIG_MAC80211_DEBUGFS
  512. /* use fixed index if set */
  513. if (mp->fixed_rate_idx != -1) {
  514. for (i = 0; i < 4; i++)
  515. mi->max_tp_rate[i] = mp->fixed_rate_idx;
  516. mi->max_prob_rate = mp->fixed_rate_idx;
  517. }
  518. #endif
  519. /* Reset update timer */
  520. mi->last_stats_update = jiffies;
  521. }
  522. static bool
  523. minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
  524. {
  525. if (rate->idx < 0)
  526. return false;
  527. if (!rate->count)
  528. return false;
  529. if (rate->flags & IEEE80211_TX_RC_MCS ||
  530. rate->flags & IEEE80211_TX_RC_VHT_MCS)
  531. return true;
  532. return rate->idx == mp->cck_rates[0] ||
  533. rate->idx == mp->cck_rates[1] ||
  534. rate->idx == mp->cck_rates[2] ||
  535. rate->idx == mp->cck_rates[3];
  536. }
  537. static void
  538. minstrel_set_next_sample_idx(struct minstrel_ht_sta *mi)
  539. {
  540. struct minstrel_mcs_group_data *mg;
  541. for (;;) {
  542. mi->sample_group++;
  543. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  544. mg = &mi->groups[mi->sample_group];
  545. if (!mg->supported)
  546. continue;
  547. if (++mg->index >= MCS_GROUP_RATES) {
  548. mg->index = 0;
  549. if (++mg->column >= ARRAY_SIZE(sample_table))
  550. mg->column = 0;
  551. }
  552. break;
  553. }
  554. }
  555. static void
  556. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
  557. {
  558. int group, orig_group;
  559. orig_group = group = *idx / MCS_GROUP_RATES;
  560. while (group > 0) {
  561. group--;
  562. if (!mi->groups[group].supported)
  563. continue;
  564. if (minstrel_mcs_groups[group].streams >
  565. minstrel_mcs_groups[orig_group].streams)
  566. continue;
  567. if (primary)
  568. *idx = mi->groups[group].max_group_tp_rate[0];
  569. else
  570. *idx = mi->groups[group].max_group_tp_rate[1];
  571. break;
  572. }
  573. }
  574. static void
  575. minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
  576. {
  577. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  578. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  579. u16 tid;
  580. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  581. return;
  582. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  583. return;
  584. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  585. return;
  586. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  587. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  588. return;
  589. ieee80211_start_tx_ba_session(pubsta, tid, 0);
  590. }
  591. static void
  592. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  593. struct ieee80211_sta *sta, void *priv_sta,
  594. struct ieee80211_tx_info *info)
  595. {
  596. struct minstrel_ht_sta_priv *msp = priv_sta;
  597. struct minstrel_ht_sta *mi = &msp->ht;
  598. struct ieee80211_tx_rate *ar = info->status.rates;
  599. struct minstrel_rate_stats *rate, *rate2;
  600. struct minstrel_priv *mp = priv;
  601. bool last, update = false;
  602. int i;
  603. if (!msp->is_ht)
  604. return mac80211_minstrel.tx_status_noskb(priv, sband, sta,
  605. &msp->legacy, info);
  606. /* This packet was aggregated but doesn't carry status info */
  607. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  608. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  609. return;
  610. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  611. info->status.ampdu_ack_len =
  612. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  613. info->status.ampdu_len = 1;
  614. }
  615. mi->ampdu_packets++;
  616. mi->ampdu_len += info->status.ampdu_len;
  617. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  618. mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  619. mi->sample_tries = 1;
  620. mi->sample_count--;
  621. }
  622. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  623. mi->sample_packets += info->status.ampdu_len;
  624. last = !minstrel_ht_txstat_valid(mp, &ar[0]);
  625. for (i = 0; !last; i++) {
  626. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  627. !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
  628. rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
  629. if (last)
  630. rate->success += info->status.ampdu_ack_len;
  631. rate->attempts += ar[i].count * info->status.ampdu_len;
  632. }
  633. /*
  634. * check for sudden death of spatial multiplexing,
  635. * downgrade to a lower number of streams if necessary.
  636. */
  637. rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
  638. if (rate->attempts > 30 &&
  639. MINSTREL_FRAC(rate->success, rate->attempts) <
  640. MINSTREL_FRAC(20, 100)) {
  641. minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
  642. update = true;
  643. }
  644. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
  645. if (rate2->attempts > 30 &&
  646. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  647. MINSTREL_FRAC(20, 100)) {
  648. minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
  649. update = true;
  650. }
  651. if (time_after(jiffies, mi->last_stats_update +
  652. (mp->update_interval / 2 * HZ) / 1000)) {
  653. update = true;
  654. minstrel_ht_update_stats(mp, mi);
  655. }
  656. if (update)
  657. minstrel_ht_update_rates(mp, mi);
  658. }
  659. static void
  660. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  661. int index)
  662. {
  663. struct minstrel_rate_stats *mrs;
  664. const struct mcs_group *group;
  665. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  666. unsigned int cw = mp->cw_min;
  667. unsigned int ctime = 0;
  668. unsigned int t_slot = 9; /* FIXME */
  669. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  670. unsigned int overhead = 0, overhead_rtscts = 0;
  671. mrs = minstrel_get_ratestats(mi, index);
  672. if (mrs->prob_ewma < MINSTREL_FRAC(1, 10)) {
  673. mrs->retry_count = 1;
  674. mrs->retry_count_rtscts = 1;
  675. return;
  676. }
  677. mrs->retry_count = 2;
  678. mrs->retry_count_rtscts = 2;
  679. mrs->retry_updated = true;
  680. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  681. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
  682. /* Contention time for first 2 tries */
  683. ctime = (t_slot * cw) >> 1;
  684. cw = min((cw << 1) | 1, mp->cw_max);
  685. ctime += (t_slot * cw) >> 1;
  686. cw = min((cw << 1) | 1, mp->cw_max);
  687. if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
  688. overhead = mi->overhead;
  689. overhead_rtscts = mi->overhead_rtscts;
  690. }
  691. /* Total TX time for data and Contention after first 2 tries */
  692. tx_time = ctime + 2 * (overhead + tx_time_data);
  693. tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
  694. /* See how many more tries we can fit inside segment size */
  695. do {
  696. /* Contention time for this try */
  697. ctime = (t_slot * cw) >> 1;
  698. cw = min((cw << 1) | 1, mp->cw_max);
  699. /* Total TX time after this try */
  700. tx_time += ctime + overhead + tx_time_data;
  701. tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
  702. if (tx_time_rtscts < mp->segment_size)
  703. mrs->retry_count_rtscts++;
  704. } while ((tx_time < mp->segment_size) &&
  705. (++mrs->retry_count < mp->max_retry));
  706. }
  707. static void
  708. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  709. struct ieee80211_sta_rates *ratetbl, int offset, int index)
  710. {
  711. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  712. struct minstrel_rate_stats *mrs;
  713. u8 idx;
  714. u16 flags = group->flags;
  715. mrs = minstrel_get_ratestats(mi, index);
  716. if (!mrs->retry_updated)
  717. minstrel_calc_retransmit(mp, mi, index);
  718. if (mrs->prob_ewma < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
  719. ratetbl->rate[offset].count = 2;
  720. ratetbl->rate[offset].count_rts = 2;
  721. ratetbl->rate[offset].count_cts = 2;
  722. } else {
  723. ratetbl->rate[offset].count = mrs->retry_count;
  724. ratetbl->rate[offset].count_cts = mrs->retry_count;
  725. ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
  726. }
  727. if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP)
  728. idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
  729. else if (flags & IEEE80211_TX_RC_VHT_MCS)
  730. idx = ((group->streams - 1) << 4) |
  731. ((index % MCS_GROUP_RATES) & 0xF);
  732. else
  733. idx = index % MCS_GROUP_RATES + (group->streams - 1) * 8;
  734. /* enable RTS/CTS if needed:
  735. * - if station is in dynamic SMPS (and streams > 1)
  736. * - for fallback rates, to increase chances of getting through
  737. */
  738. if (offset > 0 ||
  739. (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC &&
  740. group->streams > 1)) {
  741. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  742. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  743. }
  744. ratetbl->rate[offset].idx = idx;
  745. ratetbl->rate[offset].flags = flags;
  746. }
  747. static inline int
  748. minstrel_ht_get_prob_ewma(struct minstrel_ht_sta *mi, int rate)
  749. {
  750. int group = rate / MCS_GROUP_RATES;
  751. rate %= MCS_GROUP_RATES;
  752. return mi->groups[group].rates[rate].prob_ewma;
  753. }
  754. static int
  755. minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
  756. {
  757. int group = mi->max_prob_rate / MCS_GROUP_RATES;
  758. const struct mcs_group *g = &minstrel_mcs_groups[group];
  759. int rate = mi->max_prob_rate % MCS_GROUP_RATES;
  760. /* Disable A-MSDU if max_prob_rate is bad */
  761. if (mi->groups[group].rates[rate].prob_ewma < MINSTREL_FRAC(50, 100))
  762. return 1;
  763. /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
  764. if (g->duration[rate] > MCS_DURATION(1, 0, 52))
  765. return 500;
  766. /*
  767. * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
  768. * data packet size
  769. */
  770. if (g->duration[rate] > MCS_DURATION(1, 0, 104))
  771. return 1600;
  772. /*
  773. * If the rate is slower than single-stream MCS7, or if the max throughput
  774. * rate success probability is less than 75%, limit A-MSDU to twice the usual
  775. * data packet size
  776. */
  777. if (g->duration[rate] > MCS_DURATION(1, 0, 260) ||
  778. (minstrel_ht_get_prob_ewma(mi, mi->max_tp_rate[0]) <
  779. MINSTREL_FRAC(75, 100)))
  780. return 3200;
  781. /*
  782. * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
  783. * Since aggregation sessions are started/stopped without txq flush, use
  784. * the limit here to avoid the complexity of having to de-aggregate
  785. * packets in the queue.
  786. */
  787. if (!mi->sta->vht_cap.vht_supported)
  788. return IEEE80211_MAX_MPDU_LEN_HT_BA;
  789. /* unlimited */
  790. return 0;
  791. }
  792. static void
  793. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  794. {
  795. struct ieee80211_sta_rates *rates;
  796. int i = 0;
  797. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  798. if (!rates)
  799. return;
  800. /* Start with max_tp_rate[0] */
  801. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
  802. if (mp->hw->max_rates >= 3) {
  803. /* At least 3 tx rates supported, use max_tp_rate[1] next */
  804. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
  805. }
  806. if (mp->hw->max_rates >= 2) {
  807. /*
  808. * At least 2 tx rates supported, use max_prob_rate next */
  809. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  810. }
  811. mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
  812. rates->rate[i].idx = -1;
  813. rate_control_set_rates(mp->hw, mi->sta, rates);
  814. }
  815. static inline int
  816. minstrel_get_duration(int index)
  817. {
  818. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  819. return group->duration[index % MCS_GROUP_RATES];
  820. }
  821. static int
  822. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  823. {
  824. struct minstrel_rate_stats *mrs;
  825. struct minstrel_mcs_group_data *mg;
  826. unsigned int sample_dur, sample_group, cur_max_tp_streams;
  827. int tp_rate1, tp_rate2;
  828. int sample_idx = 0;
  829. if (mi->sample_wait > 0) {
  830. mi->sample_wait--;
  831. return -1;
  832. }
  833. if (!mi->sample_tries)
  834. return -1;
  835. sample_group = mi->sample_group;
  836. mg = &mi->groups[sample_group];
  837. sample_idx = sample_table[mg->column][mg->index];
  838. minstrel_set_next_sample_idx(mi);
  839. if (!(mg->supported & BIT(sample_idx)))
  840. return -1;
  841. mrs = &mg->rates[sample_idx];
  842. sample_idx += sample_group * MCS_GROUP_RATES;
  843. /* Set tp_rate1, tp_rate2 to the highest / second highest max_tp_rate */
  844. if (minstrel_get_duration(mi->max_tp_rate[0]) >
  845. minstrel_get_duration(mi->max_tp_rate[1])) {
  846. tp_rate1 = mi->max_tp_rate[1];
  847. tp_rate2 = mi->max_tp_rate[0];
  848. } else {
  849. tp_rate1 = mi->max_tp_rate[0];
  850. tp_rate2 = mi->max_tp_rate[1];
  851. }
  852. /*
  853. * Sampling might add some overhead (RTS, no aggregation)
  854. * to the frame. Hence, don't use sampling for the highest currently
  855. * used highest throughput or probability rate.
  856. */
  857. if (sample_idx == mi->max_tp_rate[0] || sample_idx == mi->max_prob_rate)
  858. return -1;
  859. /*
  860. * Do not sample if the probability is already higher than 95%
  861. * to avoid wasting airtime.
  862. */
  863. if (mrs->prob_ewma > MINSTREL_FRAC(95, 100))
  864. return -1;
  865. /*
  866. * Make sure that lower rates get sampled only occasionally,
  867. * if the link is working perfectly.
  868. */
  869. cur_max_tp_streams = minstrel_mcs_groups[tp_rate1 /
  870. MCS_GROUP_RATES].streams;
  871. sample_dur = minstrel_get_duration(sample_idx);
  872. if (sample_dur >= minstrel_get_duration(tp_rate2) &&
  873. (cur_max_tp_streams - 1 <
  874. minstrel_mcs_groups[sample_group].streams ||
  875. sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
  876. if (mrs->sample_skipped < 20)
  877. return -1;
  878. if (mi->sample_slow++ > 2)
  879. return -1;
  880. }
  881. mi->sample_tries--;
  882. return sample_idx;
  883. }
  884. static void
  885. minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
  886. struct minstrel_ht_sta *mi, bool val)
  887. {
  888. u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
  889. if (!supported || !mi->cck_supported_short)
  890. return;
  891. if (supported & (mi->cck_supported_short << (val * 4)))
  892. return;
  893. supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
  894. mi->groups[MINSTREL_CCK_GROUP].supported = supported;
  895. }
  896. static void
  897. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  898. struct ieee80211_tx_rate_control *txrc)
  899. {
  900. const struct mcs_group *sample_group;
  901. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  902. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  903. struct minstrel_ht_sta_priv *msp = priv_sta;
  904. struct minstrel_ht_sta *mi = &msp->ht;
  905. struct minstrel_priv *mp = priv;
  906. int sample_idx;
  907. if (rate_control_send_low(sta, priv_sta, txrc))
  908. return;
  909. if (!msp->is_ht)
  910. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  911. if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
  912. mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
  913. minstrel_aggr_check(sta, txrc->skb);
  914. info->flags |= mi->tx_flags;
  915. minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
  916. #ifdef CONFIG_MAC80211_DEBUGFS
  917. if (mp->fixed_rate_idx != -1)
  918. return;
  919. #endif
  920. /* Don't use EAPOL frames for sampling on non-mrr hw */
  921. if (mp->hw->max_rates == 1 &&
  922. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  923. sample_idx = -1;
  924. else
  925. sample_idx = minstrel_get_sample_rate(mp, mi);
  926. mi->total_packets++;
  927. /* wraparound */
  928. if (mi->total_packets == ~0) {
  929. mi->total_packets = 0;
  930. mi->sample_packets = 0;
  931. }
  932. if (sample_idx < 0)
  933. return;
  934. sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
  935. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  936. rate->count = 1;
  937. if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  938. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  939. rate->idx = mp->cck_rates[idx];
  940. } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
  941. ieee80211_rate_set_vht(rate, sample_idx % MCS_GROUP_RATES,
  942. sample_group->streams);
  943. } else {
  944. rate->idx = sample_idx % MCS_GROUP_RATES +
  945. (sample_group->streams - 1) * 8;
  946. }
  947. rate->flags = sample_group->flags;
  948. }
  949. static void
  950. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  951. struct ieee80211_supported_band *sband,
  952. struct ieee80211_sta *sta)
  953. {
  954. int i;
  955. if (sband->band != NL80211_BAND_2GHZ)
  956. return;
  957. if (!ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
  958. return;
  959. mi->cck_supported = 0;
  960. mi->cck_supported_short = 0;
  961. for (i = 0; i < 4; i++) {
  962. if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
  963. continue;
  964. mi->cck_supported |= BIT(i);
  965. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  966. mi->cck_supported_short |= BIT(i);
  967. }
  968. mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
  969. }
  970. static void
  971. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  972. struct cfg80211_chan_def *chandef,
  973. struct ieee80211_sta *sta, void *priv_sta)
  974. {
  975. struct minstrel_priv *mp = priv;
  976. struct minstrel_ht_sta_priv *msp = priv_sta;
  977. struct minstrel_ht_sta *mi = &msp->ht;
  978. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  979. u16 sta_cap = sta->ht_cap.cap;
  980. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  981. int use_vht;
  982. int n_supported = 0;
  983. int ack_dur;
  984. int stbc;
  985. int i;
  986. /* fall back to the old minstrel for legacy stations */
  987. if (!sta->ht_cap.ht_supported)
  988. goto use_legacy;
  989. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
  990. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  991. if (vht_cap->vht_supported)
  992. use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
  993. else
  994. #endif
  995. use_vht = 0;
  996. msp->is_ht = true;
  997. memset(mi, 0, sizeof(*mi));
  998. mi->sta = sta;
  999. mi->last_stats_update = jiffies;
  1000. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  1001. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  1002. mi->overhead += ack_dur;
  1003. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  1004. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  1005. /* When using MRR, sample more on the first attempt, without delay */
  1006. if (mp->has_mrr) {
  1007. mi->sample_count = 16;
  1008. mi->sample_wait = 0;
  1009. } else {
  1010. mi->sample_count = 8;
  1011. mi->sample_wait = 8;
  1012. }
  1013. mi->sample_tries = 4;
  1014. /* TODO tx_flags for vht - ATM the RC API is not fine-grained enough */
  1015. if (!use_vht) {
  1016. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  1017. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  1018. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  1019. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  1020. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  1021. }
  1022. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  1023. u32 gflags = minstrel_mcs_groups[i].flags;
  1024. int bw, nss;
  1025. mi->groups[i].supported = 0;
  1026. if (i == MINSTREL_CCK_GROUP) {
  1027. minstrel_ht_update_cck(mp, mi, sband, sta);
  1028. continue;
  1029. }
  1030. if (gflags & IEEE80211_TX_RC_SHORT_GI) {
  1031. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1032. if (!(sta_cap & IEEE80211_HT_CAP_SGI_40))
  1033. continue;
  1034. } else {
  1035. if (!(sta_cap & IEEE80211_HT_CAP_SGI_20))
  1036. continue;
  1037. }
  1038. }
  1039. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  1040. sta->bandwidth < IEEE80211_STA_RX_BW_40)
  1041. continue;
  1042. nss = minstrel_mcs_groups[i].streams;
  1043. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  1044. if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
  1045. continue;
  1046. /* HT rate */
  1047. if (gflags & IEEE80211_TX_RC_MCS) {
  1048. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  1049. if (use_vht && minstrel_vht_only)
  1050. continue;
  1051. #endif
  1052. mi->groups[i].supported = mcs->rx_mask[nss - 1];
  1053. if (mi->groups[i].supported)
  1054. n_supported++;
  1055. continue;
  1056. }
  1057. /* VHT rate */
  1058. if (!vht_cap->vht_supported ||
  1059. WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
  1060. WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
  1061. continue;
  1062. if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
  1063. if (sta->bandwidth < IEEE80211_STA_RX_BW_80 ||
  1064. ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
  1065. !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
  1066. continue;
  1067. }
  1068. }
  1069. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1070. bw = BW_40;
  1071. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1072. bw = BW_80;
  1073. else
  1074. bw = BW_20;
  1075. mi->groups[i].supported = minstrel_get_valid_vht_rates(bw, nss,
  1076. vht_cap->vht_mcs.tx_mcs_map);
  1077. if (mi->groups[i].supported)
  1078. n_supported++;
  1079. }
  1080. if (!n_supported)
  1081. goto use_legacy;
  1082. /* create an initial rate table with the lowest supported rates */
  1083. minstrel_ht_update_stats(mp, mi);
  1084. minstrel_ht_update_rates(mp, mi);
  1085. return;
  1086. use_legacy:
  1087. msp->is_ht = false;
  1088. memset(&msp->legacy, 0, sizeof(msp->legacy));
  1089. msp->legacy.r = msp->ratelist;
  1090. msp->legacy.sample_table = msp->sample_table;
  1091. return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
  1092. &msp->legacy);
  1093. }
  1094. static void
  1095. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1096. struct cfg80211_chan_def *chandef,
  1097. struct ieee80211_sta *sta, void *priv_sta)
  1098. {
  1099. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1100. }
  1101. static void
  1102. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  1103. struct cfg80211_chan_def *chandef,
  1104. struct ieee80211_sta *sta, void *priv_sta,
  1105. u32 changed)
  1106. {
  1107. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1108. }
  1109. static void *
  1110. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1111. {
  1112. struct ieee80211_supported_band *sband;
  1113. struct minstrel_ht_sta_priv *msp;
  1114. struct minstrel_priv *mp = priv;
  1115. struct ieee80211_hw *hw = mp->hw;
  1116. int max_rates = 0;
  1117. int i;
  1118. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1119. sband = hw->wiphy->bands[i];
  1120. if (sband && sband->n_bitrates > max_rates)
  1121. max_rates = sband->n_bitrates;
  1122. }
  1123. msp = kzalloc(sizeof(*msp), gfp);
  1124. if (!msp)
  1125. return NULL;
  1126. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  1127. if (!msp->ratelist)
  1128. goto error;
  1129. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  1130. if (!msp->sample_table)
  1131. goto error1;
  1132. return msp;
  1133. error1:
  1134. kfree(msp->ratelist);
  1135. error:
  1136. kfree(msp);
  1137. return NULL;
  1138. }
  1139. static void
  1140. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  1141. {
  1142. struct minstrel_ht_sta_priv *msp = priv_sta;
  1143. kfree(msp->sample_table);
  1144. kfree(msp->ratelist);
  1145. kfree(msp);
  1146. }
  1147. static void *
  1148. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  1149. {
  1150. return mac80211_minstrel.alloc(hw, debugfsdir);
  1151. }
  1152. static void
  1153. minstrel_ht_free(void *priv)
  1154. {
  1155. mac80211_minstrel.free(priv);
  1156. }
  1157. static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
  1158. {
  1159. struct minstrel_ht_sta_priv *msp = priv_sta;
  1160. struct minstrel_ht_sta *mi = &msp->ht;
  1161. int i, j, prob, tp_avg;
  1162. if (!msp->is_ht)
  1163. return mac80211_minstrel.get_expected_throughput(priv_sta);
  1164. i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
  1165. j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
  1166. prob = mi->groups[i].rates[j].prob_ewma;
  1167. /* convert tp_avg from pkt per second in kbps */
  1168. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
  1169. tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
  1170. return tp_avg;
  1171. }
  1172. static const struct rate_control_ops mac80211_minstrel_ht = {
  1173. .name = "minstrel_ht",
  1174. .tx_status_noskb = minstrel_ht_tx_status,
  1175. .get_rate = minstrel_ht_get_rate,
  1176. .rate_init = minstrel_ht_rate_init,
  1177. .rate_update = minstrel_ht_rate_update,
  1178. .alloc_sta = minstrel_ht_alloc_sta,
  1179. .free_sta = minstrel_ht_free_sta,
  1180. .alloc = minstrel_ht_alloc,
  1181. .free = minstrel_ht_free,
  1182. #ifdef CONFIG_MAC80211_DEBUGFS
  1183. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  1184. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  1185. #endif
  1186. .get_expected_throughput = minstrel_ht_get_expected_throughput,
  1187. };
  1188. static void __init init_sample_table(void)
  1189. {
  1190. int col, i, new_idx;
  1191. u8 rnd[MCS_GROUP_RATES];
  1192. memset(sample_table, 0xff, sizeof(sample_table));
  1193. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  1194. prandom_bytes(rnd, sizeof(rnd));
  1195. for (i = 0; i < MCS_GROUP_RATES; i++) {
  1196. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  1197. while (sample_table[col][new_idx] != 0xff)
  1198. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  1199. sample_table[col][new_idx] = i;
  1200. }
  1201. }
  1202. }
  1203. int __init
  1204. rc80211_minstrel_ht_init(void)
  1205. {
  1206. init_sample_table();
  1207. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  1208. }
  1209. void
  1210. rc80211_minstrel_ht_exit(void)
  1211. {
  1212. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  1213. }