rc80211_minstrel_ht.c 37 KB

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