rc80211_minstrel_ht.c 41 KB

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