rc80211_minstrel_ht.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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, 5000);
  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. if (offset > 0) {
  735. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  736. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  737. }
  738. ratetbl->rate[offset].idx = idx;
  739. ratetbl->rate[offset].flags = flags;
  740. }
  741. static void
  742. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  743. {
  744. struct ieee80211_sta_rates *rates;
  745. int i = 0;
  746. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  747. if (!rates)
  748. return;
  749. /* Start with max_tp_rate[0] */
  750. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
  751. if (mp->hw->max_rates >= 3) {
  752. /* At least 3 tx rates supported, use max_tp_rate[1] next */
  753. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]);
  754. }
  755. if (mp->hw->max_rates >= 2) {
  756. /*
  757. * At least 2 tx rates supported, use max_prob_rate next */
  758. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  759. }
  760. rates->rate[i].idx = -1;
  761. rate_control_set_rates(mp->hw, mi->sta, rates);
  762. }
  763. static inline int
  764. minstrel_get_duration(int index)
  765. {
  766. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  767. return group->duration[index % MCS_GROUP_RATES];
  768. }
  769. static int
  770. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  771. {
  772. struct minstrel_rate_stats *mrs;
  773. struct minstrel_mcs_group_data *mg;
  774. unsigned int sample_dur, sample_group, cur_max_tp_streams;
  775. int sample_idx = 0;
  776. if (mi->sample_wait > 0) {
  777. mi->sample_wait--;
  778. return -1;
  779. }
  780. if (!mi->sample_tries)
  781. return -1;
  782. sample_group = mi->sample_group;
  783. mg = &mi->groups[sample_group];
  784. sample_idx = sample_table[mg->column][mg->index];
  785. minstrel_set_next_sample_idx(mi);
  786. if (!(mg->supported & BIT(sample_idx)))
  787. return -1;
  788. mrs = &mg->rates[sample_idx];
  789. sample_idx += sample_group * MCS_GROUP_RATES;
  790. /*
  791. * Sampling might add some overhead (RTS, no aggregation)
  792. * to the frame. Hence, don't use sampling for the currently
  793. * used rates.
  794. */
  795. if (sample_idx == mi->max_tp_rate[0] ||
  796. sample_idx == mi->max_tp_rate[1] ||
  797. sample_idx == mi->max_prob_rate)
  798. return -1;
  799. /*
  800. * Do not sample if the probability is already higher than 95%
  801. * to avoid wasting airtime.
  802. */
  803. if (mrs->prob_ewma > MINSTREL_FRAC(95, 100))
  804. return -1;
  805. /*
  806. * Make sure that lower rates get sampled only occasionally,
  807. * if the link is working perfectly.
  808. */
  809. cur_max_tp_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
  810. MCS_GROUP_RATES].streams;
  811. sample_dur = minstrel_get_duration(sample_idx);
  812. if (sample_dur >= minstrel_get_duration(mi->max_tp_rate[1]) &&
  813. (cur_max_tp_streams - 1 <
  814. minstrel_mcs_groups[sample_group].streams ||
  815. sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
  816. if (mrs->sample_skipped < 20)
  817. return -1;
  818. if (mi->sample_slow++ > 2)
  819. return -1;
  820. }
  821. mi->sample_tries--;
  822. return sample_idx;
  823. }
  824. static void
  825. minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
  826. struct minstrel_ht_sta *mi, bool val)
  827. {
  828. u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
  829. if (!supported || !mi->cck_supported_short)
  830. return;
  831. if (supported & (mi->cck_supported_short << (val * 4)))
  832. return;
  833. supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
  834. mi->groups[MINSTREL_CCK_GROUP].supported = supported;
  835. }
  836. static void
  837. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  838. struct ieee80211_tx_rate_control *txrc)
  839. {
  840. const struct mcs_group *sample_group;
  841. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  842. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  843. struct minstrel_ht_sta_priv *msp = priv_sta;
  844. struct minstrel_ht_sta *mi = &msp->ht;
  845. struct minstrel_priv *mp = priv;
  846. int sample_idx;
  847. if (rate_control_send_low(sta, priv_sta, txrc))
  848. return;
  849. if (!msp->is_ht)
  850. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  851. if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
  852. mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
  853. minstrel_aggr_check(sta, txrc->skb);
  854. info->flags |= mi->tx_flags;
  855. minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
  856. #ifdef CONFIG_MAC80211_DEBUGFS
  857. if (mp->fixed_rate_idx != -1)
  858. return;
  859. #endif
  860. /* Don't use EAPOL frames for sampling on non-mrr hw */
  861. if (mp->hw->max_rates == 1 &&
  862. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  863. sample_idx = -1;
  864. else
  865. sample_idx = minstrel_get_sample_rate(mp, mi);
  866. mi->total_packets++;
  867. /* wraparound */
  868. if (mi->total_packets == ~0) {
  869. mi->total_packets = 0;
  870. mi->sample_packets = 0;
  871. }
  872. if (sample_idx < 0)
  873. return;
  874. sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
  875. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  876. rate->count = 1;
  877. if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  878. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  879. rate->idx = mp->cck_rates[idx];
  880. } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
  881. ieee80211_rate_set_vht(rate, sample_idx % MCS_GROUP_RATES,
  882. sample_group->streams);
  883. } else {
  884. rate->idx = sample_idx % MCS_GROUP_RATES +
  885. (sample_group->streams - 1) * 8;
  886. }
  887. rate->flags = sample_group->flags;
  888. }
  889. static void
  890. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  891. struct ieee80211_supported_band *sband,
  892. struct ieee80211_sta *sta)
  893. {
  894. int i;
  895. if (sband->band != IEEE80211_BAND_2GHZ)
  896. return;
  897. if (!ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
  898. return;
  899. mi->cck_supported = 0;
  900. mi->cck_supported_short = 0;
  901. for (i = 0; i < 4; i++) {
  902. if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
  903. continue;
  904. mi->cck_supported |= BIT(i);
  905. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  906. mi->cck_supported_short |= BIT(i);
  907. }
  908. mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
  909. }
  910. static void
  911. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  912. struct cfg80211_chan_def *chandef,
  913. struct ieee80211_sta *sta, void *priv_sta)
  914. {
  915. struct minstrel_priv *mp = priv;
  916. struct minstrel_ht_sta_priv *msp = priv_sta;
  917. struct minstrel_ht_sta *mi = &msp->ht;
  918. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  919. u16 sta_cap = sta->ht_cap.cap;
  920. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  921. int use_vht;
  922. int n_supported = 0;
  923. int ack_dur;
  924. int stbc;
  925. int i;
  926. /* fall back to the old minstrel for legacy stations */
  927. if (!sta->ht_cap.ht_supported)
  928. goto use_legacy;
  929. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
  930. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  931. if (vht_cap->vht_supported)
  932. use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
  933. else
  934. #endif
  935. use_vht = 0;
  936. msp->is_ht = true;
  937. memset(mi, 0, sizeof(*mi));
  938. mi->sta = sta;
  939. mi->last_stats_update = jiffies;
  940. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  941. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  942. mi->overhead += ack_dur;
  943. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  944. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  945. /* When using MRR, sample more on the first attempt, without delay */
  946. if (mp->has_mrr) {
  947. mi->sample_count = 16;
  948. mi->sample_wait = 0;
  949. } else {
  950. mi->sample_count = 8;
  951. mi->sample_wait = 8;
  952. }
  953. mi->sample_tries = 4;
  954. /* TODO tx_flags for vht - ATM the RC API is not fine-grained enough */
  955. if (!use_vht) {
  956. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  957. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  958. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  959. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  960. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  961. }
  962. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  963. u32 gflags = minstrel_mcs_groups[i].flags;
  964. int bw, nss;
  965. mi->groups[i].supported = 0;
  966. if (i == MINSTREL_CCK_GROUP) {
  967. minstrel_ht_update_cck(mp, mi, sband, sta);
  968. continue;
  969. }
  970. if (gflags & IEEE80211_TX_RC_SHORT_GI) {
  971. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  972. if (!(sta_cap & IEEE80211_HT_CAP_SGI_40))
  973. continue;
  974. } else {
  975. if (!(sta_cap & IEEE80211_HT_CAP_SGI_20))
  976. continue;
  977. }
  978. }
  979. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  980. sta->bandwidth < IEEE80211_STA_RX_BW_40)
  981. continue;
  982. nss = minstrel_mcs_groups[i].streams;
  983. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  984. if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
  985. continue;
  986. /* HT rate */
  987. if (gflags & IEEE80211_TX_RC_MCS) {
  988. #ifdef CONFIG_MAC80211_RC_MINSTREL_VHT
  989. if (use_vht && minstrel_vht_only)
  990. continue;
  991. #endif
  992. mi->groups[i].supported = mcs->rx_mask[nss - 1];
  993. if (mi->groups[i].supported)
  994. n_supported++;
  995. continue;
  996. }
  997. /* VHT rate */
  998. if (!vht_cap->vht_supported ||
  999. WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
  1000. WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
  1001. continue;
  1002. if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
  1003. if (sta->bandwidth < IEEE80211_STA_RX_BW_80 ||
  1004. ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
  1005. !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
  1006. continue;
  1007. }
  1008. }
  1009. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1010. bw = BW_40;
  1011. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1012. bw = BW_80;
  1013. else
  1014. bw = BW_20;
  1015. mi->groups[i].supported = minstrel_get_valid_vht_rates(bw, nss,
  1016. vht_cap->vht_mcs.tx_mcs_map);
  1017. if (mi->groups[i].supported)
  1018. n_supported++;
  1019. }
  1020. if (!n_supported)
  1021. goto use_legacy;
  1022. /* create an initial rate table with the lowest supported rates */
  1023. minstrel_ht_update_stats(mp, mi);
  1024. minstrel_ht_update_rates(mp, mi);
  1025. return;
  1026. use_legacy:
  1027. msp->is_ht = false;
  1028. memset(&msp->legacy, 0, sizeof(msp->legacy));
  1029. msp->legacy.r = msp->ratelist;
  1030. msp->legacy.sample_table = msp->sample_table;
  1031. return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
  1032. &msp->legacy);
  1033. }
  1034. static void
  1035. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1036. struct cfg80211_chan_def *chandef,
  1037. struct ieee80211_sta *sta, void *priv_sta)
  1038. {
  1039. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1040. }
  1041. static void
  1042. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  1043. struct cfg80211_chan_def *chandef,
  1044. struct ieee80211_sta *sta, void *priv_sta,
  1045. u32 changed)
  1046. {
  1047. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1048. }
  1049. static void *
  1050. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1051. {
  1052. struct ieee80211_supported_band *sband;
  1053. struct minstrel_ht_sta_priv *msp;
  1054. struct minstrel_priv *mp = priv;
  1055. struct ieee80211_hw *hw = mp->hw;
  1056. int max_rates = 0;
  1057. int i;
  1058. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1059. sband = hw->wiphy->bands[i];
  1060. if (sband && sband->n_bitrates > max_rates)
  1061. max_rates = sband->n_bitrates;
  1062. }
  1063. msp = kzalloc(sizeof(*msp), gfp);
  1064. if (!msp)
  1065. return NULL;
  1066. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  1067. if (!msp->ratelist)
  1068. goto error;
  1069. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  1070. if (!msp->sample_table)
  1071. goto error1;
  1072. return msp;
  1073. error1:
  1074. kfree(msp->ratelist);
  1075. error:
  1076. kfree(msp);
  1077. return NULL;
  1078. }
  1079. static void
  1080. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  1081. {
  1082. struct minstrel_ht_sta_priv *msp = priv_sta;
  1083. kfree(msp->sample_table);
  1084. kfree(msp->ratelist);
  1085. kfree(msp);
  1086. }
  1087. static void *
  1088. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  1089. {
  1090. return mac80211_minstrel.alloc(hw, debugfsdir);
  1091. }
  1092. static void
  1093. minstrel_ht_free(void *priv)
  1094. {
  1095. mac80211_minstrel.free(priv);
  1096. }
  1097. static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
  1098. {
  1099. struct minstrel_ht_sta_priv *msp = priv_sta;
  1100. struct minstrel_ht_sta *mi = &msp->ht;
  1101. int i, j, prob, tp_avg;
  1102. if (!msp->is_ht)
  1103. return mac80211_minstrel.get_expected_throughput(priv_sta);
  1104. i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
  1105. j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
  1106. prob = mi->groups[i].rates[j].prob_ewma;
  1107. /* convert tp_avg from pkt per second in kbps */
  1108. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * AVG_PKT_SIZE * 8 / 1024;
  1109. return tp_avg;
  1110. }
  1111. static const struct rate_control_ops mac80211_minstrel_ht = {
  1112. .name = "minstrel_ht",
  1113. .tx_status_noskb = minstrel_ht_tx_status,
  1114. .get_rate = minstrel_ht_get_rate,
  1115. .rate_init = minstrel_ht_rate_init,
  1116. .rate_update = minstrel_ht_rate_update,
  1117. .alloc_sta = minstrel_ht_alloc_sta,
  1118. .free_sta = minstrel_ht_free_sta,
  1119. .alloc = minstrel_ht_alloc,
  1120. .free = minstrel_ht_free,
  1121. #ifdef CONFIG_MAC80211_DEBUGFS
  1122. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  1123. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  1124. #endif
  1125. .get_expected_throughput = minstrel_ht_get_expected_throughput,
  1126. };
  1127. static void __init init_sample_table(void)
  1128. {
  1129. int col, i, new_idx;
  1130. u8 rnd[MCS_GROUP_RATES];
  1131. memset(sample_table, 0xff, sizeof(sample_table));
  1132. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  1133. prandom_bytes(rnd, sizeof(rnd));
  1134. for (i = 0; i < MCS_GROUP_RATES; i++) {
  1135. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  1136. while (sample_table[col][new_idx] != 0xff)
  1137. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  1138. sample_table[col][new_idx] = i;
  1139. }
  1140. }
  1141. }
  1142. int __init
  1143. rc80211_minstrel_ht_init(void)
  1144. {
  1145. init_sample_table();
  1146. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  1147. }
  1148. void
  1149. rc80211_minstrel_ht_exit(void)
  1150. {
  1151. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  1152. }