rc80211_minstrel_ht.c 37 KB

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