rc80211_minstrel_ht.c 39 KB

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