rc80211_minstrel_ht.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010-2013 Felix Fietkau <[email protected]>
  4. * Copyright (C) 2019-2022 Intel Corporation
  5. */
  6. #include <linux/netdevice.h>
  7. #include <linux/types.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/random.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/ieee80211.h>
  13. #include <linux/minmax.h>
  14. #include <net/mac80211.h>
  15. #include "rate.h"
  16. #include "sta_info.h"
  17. #include "rc80211_minstrel_ht.h"
  18. #define AVG_AMPDU_SIZE 16
  19. #define AVG_PKT_SIZE 1200
  20. /* Number of bits for an average sized packet */
  21. #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_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) \
  32. (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
  33. #define BW_20 0
  34. #define BW_40 1
  35. #define BW_80 2
  36. /*
  37. * Define group sort order: HT40 -> SGI -> #streams
  38. */
  39. #define GROUP_IDX(_streams, _sgi, _ht40) \
  40. MINSTREL_HT_GROUP_0 + \
  41. MINSTREL_MAX_STREAMS * 2 * _ht40 + \
  42. MINSTREL_MAX_STREAMS * _sgi + \
  43. _streams - 1
  44. #define _MAX(a, b) (((a)>(b))?(a):(b))
  45. #define GROUP_SHIFT(duration) \
  46. _MAX(0, 16 - __builtin_clz(duration))
  47. /* MCS rate information for an MCS group */
  48. #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \
  49. [GROUP_IDX(_streams, _sgi, _ht40)] = { \
  50. .streams = _streams, \
  51. .shift = _s, \
  52. .bw = _ht40, \
  53. .flags = \
  54. IEEE80211_TX_RC_MCS | \
  55. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  56. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  57. .duration = { \
  58. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s, \
  59. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s, \
  60. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s, \
  61. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s, \
  62. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s, \
  63. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s, \
  64. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s, \
  65. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s \
  66. } \
  67. }
  68. #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \
  69. GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
  70. #define MCS_GROUP(_streams, _sgi, _ht40) \
  71. __MCS_GROUP(_streams, _sgi, _ht40, \
  72. MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
  73. #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
  74. (MINSTREL_VHT_GROUP_0 + \
  75. MINSTREL_MAX_STREAMS * 2 * (_bw) + \
  76. MINSTREL_MAX_STREAMS * (_sgi) + \
  77. (_streams) - 1)
  78. #define BW2VBPS(_bw, r3, r2, r1) \
  79. (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
  80. #define __VHT_GROUP(_streams, _sgi, _bw, _s) \
  81. [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
  82. .streams = _streams, \
  83. .shift = _s, \
  84. .bw = _bw, \
  85. .flags = \
  86. IEEE80211_TX_RC_VHT_MCS | \
  87. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  88. (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \
  89. _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  90. .duration = { \
  91. MCS_DURATION(_streams, _sgi, \
  92. BW2VBPS(_bw, 117, 54, 26)) >> _s, \
  93. MCS_DURATION(_streams, _sgi, \
  94. BW2VBPS(_bw, 234, 108, 52)) >> _s, \
  95. MCS_DURATION(_streams, _sgi, \
  96. BW2VBPS(_bw, 351, 162, 78)) >> _s, \
  97. MCS_DURATION(_streams, _sgi, \
  98. BW2VBPS(_bw, 468, 216, 104)) >> _s, \
  99. MCS_DURATION(_streams, _sgi, \
  100. BW2VBPS(_bw, 702, 324, 156)) >> _s, \
  101. MCS_DURATION(_streams, _sgi, \
  102. BW2VBPS(_bw, 936, 432, 208)) >> _s, \
  103. MCS_DURATION(_streams, _sgi, \
  104. BW2VBPS(_bw, 1053, 486, 234)) >> _s, \
  105. MCS_DURATION(_streams, _sgi, \
  106. BW2VBPS(_bw, 1170, 540, 260)) >> _s, \
  107. MCS_DURATION(_streams, _sgi, \
  108. BW2VBPS(_bw, 1404, 648, 312)) >> _s, \
  109. MCS_DURATION(_streams, _sgi, \
  110. BW2VBPS(_bw, 1560, 720, 346)) >> _s \
  111. } \
  112. }
  113. #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \
  114. GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \
  115. BW2VBPS(_bw, 117, 54, 26)))
  116. #define VHT_GROUP(_streams, _sgi, _bw) \
  117. __VHT_GROUP(_streams, _sgi, _bw, \
  118. VHT_GROUP_SHIFT(_streams, _sgi, _bw))
  119. #define CCK_DURATION(_bitrate, _short) \
  120. (1000 * (10 /* SIFS */ + \
  121. (_short ? 72 + 24 : 144 + 48) + \
  122. (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate)))
  123. #define CCK_DURATION_LIST(_short, _s) \
  124. CCK_DURATION(10, _short) >> _s, \
  125. CCK_DURATION(20, _short) >> _s, \
  126. CCK_DURATION(55, _short) >> _s, \
  127. CCK_DURATION(110, _short) >> _s
  128. #define __CCK_GROUP(_s) \
  129. [MINSTREL_CCK_GROUP] = { \
  130. .streams = 1, \
  131. .flags = 0, \
  132. .shift = _s, \
  133. .duration = { \
  134. CCK_DURATION_LIST(false, _s), \
  135. CCK_DURATION_LIST(true, _s) \
  136. } \
  137. }
  138. #define CCK_GROUP_SHIFT \
  139. GROUP_SHIFT(CCK_DURATION(10, false))
  140. #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT)
  141. #define OFDM_DURATION(_bitrate) \
  142. (1000 * (16 /* SIFS + signal ext */ + \
  143. 16 /* T_PREAMBLE */ + \
  144. 4 /* T_SIGNAL */ + \
  145. 4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) / \
  146. ((_bitrate) * 4)))))
  147. #define OFDM_DURATION_LIST(_s) \
  148. OFDM_DURATION(60) >> _s, \
  149. OFDM_DURATION(90) >> _s, \
  150. OFDM_DURATION(120) >> _s, \
  151. OFDM_DURATION(180) >> _s, \
  152. OFDM_DURATION(240) >> _s, \
  153. OFDM_DURATION(360) >> _s, \
  154. OFDM_DURATION(480) >> _s, \
  155. OFDM_DURATION(540) >> _s
  156. #define __OFDM_GROUP(_s) \
  157. [MINSTREL_OFDM_GROUP] = { \
  158. .streams = 1, \
  159. .flags = 0, \
  160. .shift = _s, \
  161. .duration = { \
  162. OFDM_DURATION_LIST(_s), \
  163. } \
  164. }
  165. #define OFDM_GROUP_SHIFT \
  166. GROUP_SHIFT(OFDM_DURATION(60))
  167. #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT)
  168. static bool minstrel_vht_only = true;
  169. module_param(minstrel_vht_only, bool, 0644);
  170. MODULE_PARM_DESC(minstrel_vht_only,
  171. "Use only VHT rates when VHT is supported by sta.");
  172. /*
  173. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  174. * groups, based on the number of streams and flags (HT40, SGI) that they
  175. * use.
  176. *
  177. * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
  178. * BW -> SGI -> #streams
  179. */
  180. const struct mcs_group minstrel_mcs_groups[] = {
  181. MCS_GROUP(1, 0, BW_20),
  182. MCS_GROUP(2, 0, BW_20),
  183. MCS_GROUP(3, 0, BW_20),
  184. MCS_GROUP(4, 0, BW_20),
  185. MCS_GROUP(1, 1, BW_20),
  186. MCS_GROUP(2, 1, BW_20),
  187. MCS_GROUP(3, 1, BW_20),
  188. MCS_GROUP(4, 1, BW_20),
  189. MCS_GROUP(1, 0, BW_40),
  190. MCS_GROUP(2, 0, BW_40),
  191. MCS_GROUP(3, 0, BW_40),
  192. MCS_GROUP(4, 0, BW_40),
  193. MCS_GROUP(1, 1, BW_40),
  194. MCS_GROUP(2, 1, BW_40),
  195. MCS_GROUP(3, 1, BW_40),
  196. MCS_GROUP(4, 1, BW_40),
  197. CCK_GROUP,
  198. OFDM_GROUP,
  199. VHT_GROUP(1, 0, BW_20),
  200. VHT_GROUP(2, 0, BW_20),
  201. VHT_GROUP(3, 0, BW_20),
  202. VHT_GROUP(4, 0, BW_20),
  203. VHT_GROUP(1, 1, BW_20),
  204. VHT_GROUP(2, 1, BW_20),
  205. VHT_GROUP(3, 1, BW_20),
  206. VHT_GROUP(4, 1, BW_20),
  207. VHT_GROUP(1, 0, BW_40),
  208. VHT_GROUP(2, 0, BW_40),
  209. VHT_GROUP(3, 0, BW_40),
  210. VHT_GROUP(4, 0, BW_40),
  211. VHT_GROUP(1, 1, BW_40),
  212. VHT_GROUP(2, 1, BW_40),
  213. VHT_GROUP(3, 1, BW_40),
  214. VHT_GROUP(4, 1, BW_40),
  215. VHT_GROUP(1, 0, BW_80),
  216. VHT_GROUP(2, 0, BW_80),
  217. VHT_GROUP(3, 0, BW_80),
  218. VHT_GROUP(4, 0, BW_80),
  219. VHT_GROUP(1, 1, BW_80),
  220. VHT_GROUP(2, 1, BW_80),
  221. VHT_GROUP(3, 1, BW_80),
  222. VHT_GROUP(4, 1, BW_80),
  223. };
  224. const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 };
  225. const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
  226. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly;
  227. static const u8 minstrel_sample_seq[] = {
  228. MINSTREL_SAMPLE_TYPE_INC,
  229. MINSTREL_SAMPLE_TYPE_JUMP,
  230. MINSTREL_SAMPLE_TYPE_INC,
  231. MINSTREL_SAMPLE_TYPE_JUMP,
  232. MINSTREL_SAMPLE_TYPE_INC,
  233. MINSTREL_SAMPLE_TYPE_SLOW,
  234. };
  235. static void
  236. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
  237. /*
  238. * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer)
  239. * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1
  240. *
  241. * Returns the valid mcs map for struct minstrel_mcs_group_data.supported
  242. */
  243. static u16
  244. minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map)
  245. {
  246. u16 mask = 0;
  247. if (bw == BW_20) {
  248. if (nss != 3 && nss != 6)
  249. mask = BIT(9);
  250. } else if (bw == BW_80) {
  251. if (nss == 3 || nss == 7)
  252. mask = BIT(6);
  253. else if (nss == 6)
  254. mask = BIT(9);
  255. } else {
  256. WARN_ON(bw != BW_40);
  257. }
  258. switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) {
  259. case IEEE80211_VHT_MCS_SUPPORT_0_7:
  260. mask |= 0x300;
  261. break;
  262. case IEEE80211_VHT_MCS_SUPPORT_0_8:
  263. mask |= 0x200;
  264. break;
  265. case IEEE80211_VHT_MCS_SUPPORT_0_9:
  266. break;
  267. default:
  268. mask = 0x3ff;
  269. }
  270. return 0x3ff & ~mask;
  271. }
  272. static bool
  273. minstrel_ht_is_legacy_group(int group)
  274. {
  275. return group == MINSTREL_CCK_GROUP ||
  276. group == MINSTREL_OFDM_GROUP;
  277. }
  278. /*
  279. * Look up an MCS group index based on mac80211 rate information
  280. */
  281. static int
  282. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  283. {
  284. return GROUP_IDX((rate->idx / 8) + 1,
  285. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  286. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
  287. }
  288. /*
  289. * Look up an MCS group index based on new cfg80211 rate_info.
  290. */
  291. static int
  292. minstrel_ht_ri_get_group_idx(struct rate_info *rate)
  293. {
  294. return GROUP_IDX((rate->mcs / 8) + 1,
  295. !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
  296. !!(rate->bw & RATE_INFO_BW_40));
  297. }
  298. static int
  299. minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate)
  300. {
  301. return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate),
  302. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  303. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) +
  304. 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH));
  305. }
  306. /*
  307. * Look up an MCS group index based on new cfg80211 rate_info.
  308. */
  309. static int
  310. minstrel_vht_ri_get_group_idx(struct rate_info *rate)
  311. {
  312. return VHT_GROUP_IDX(rate->nss,
  313. !!(rate->flags & RATE_INFO_FLAGS_SHORT_GI),
  314. !!(rate->bw & RATE_INFO_BW_40) +
  315. 2*!!(rate->bw & RATE_INFO_BW_80));
  316. }
  317. static struct minstrel_rate_stats *
  318. minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  319. struct ieee80211_tx_rate *rate)
  320. {
  321. int group, idx;
  322. if (rate->flags & IEEE80211_TX_RC_MCS) {
  323. group = minstrel_ht_get_group_idx(rate);
  324. idx = rate->idx % 8;
  325. goto out;
  326. }
  327. if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
  328. group = minstrel_vht_get_group_idx(rate);
  329. idx = ieee80211_rate_get_vht_mcs(rate);
  330. goto out;
  331. }
  332. group = MINSTREL_CCK_GROUP;
  333. for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
  334. if (!(mi->supported[group] & BIT(idx)))
  335. continue;
  336. if (rate->idx != mp->cck_rates[idx])
  337. continue;
  338. /* short preamble */
  339. if ((mi->supported[group] & BIT(idx + 4)) &&
  340. (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
  341. idx += 4;
  342. goto out;
  343. }
  344. group = MINSTREL_OFDM_GROUP;
  345. for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
  346. if (rate->idx == mp->ofdm_rates[mi->band][idx])
  347. goto out;
  348. idx = 0;
  349. out:
  350. return &mi->groups[group].rates[idx];
  351. }
  352. /*
  353. * Get the minstrel rate statistics for specified STA and rate info.
  354. */
  355. static struct minstrel_rate_stats *
  356. minstrel_ht_ri_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  357. struct ieee80211_rate_status *rate_status)
  358. {
  359. int group, idx;
  360. struct rate_info *rate = &rate_status->rate_idx;
  361. if (rate->flags & RATE_INFO_FLAGS_MCS) {
  362. group = minstrel_ht_ri_get_group_idx(rate);
  363. idx = rate->mcs % 8;
  364. goto out;
  365. }
  366. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) {
  367. group = minstrel_vht_ri_get_group_idx(rate);
  368. idx = rate->mcs;
  369. goto out;
  370. }
  371. group = MINSTREL_CCK_GROUP;
  372. for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) {
  373. if (rate->legacy != minstrel_cck_bitrates[ mp->cck_rates[idx] ])
  374. continue;
  375. /* short preamble */
  376. if ((mi->supported[group] & BIT(idx + 4)) &&
  377. mi->use_short_preamble)
  378. idx += 4;
  379. goto out;
  380. }
  381. group = MINSTREL_OFDM_GROUP;
  382. for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++)
  383. if (rate->legacy == minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][idx] ])
  384. goto out;
  385. idx = 0;
  386. out:
  387. return &mi->groups[group].rates[idx];
  388. }
  389. static inline struct minstrel_rate_stats *
  390. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  391. {
  392. return &mi->groups[MI_RATE_GROUP(index)].rates[MI_RATE_IDX(index)];
  393. }
  394. static inline int minstrel_get_duration(int index)
  395. {
  396. const struct mcs_group *group = &minstrel_mcs_groups[MI_RATE_GROUP(index)];
  397. unsigned int duration = group->duration[MI_RATE_IDX(index)];
  398. return duration << group->shift;
  399. }
  400. static unsigned int
  401. minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi)
  402. {
  403. int duration;
  404. if (mi->avg_ampdu_len)
  405. return MINSTREL_TRUNC(mi->avg_ampdu_len);
  406. if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(mi->max_tp_rate[0])))
  407. return 1;
  408. duration = minstrel_get_duration(mi->max_tp_rate[0]);
  409. if (duration > 400 * 1000)
  410. return 2;
  411. if (duration > 250 * 1000)
  412. return 4;
  413. if (duration > 150 * 1000)
  414. return 8;
  415. return 16;
  416. }
  417. /*
  418. * Return current throughput based on the average A-MPDU length, taking into
  419. * account the expected number of retransmissions and their expected length
  420. */
  421. int
  422. minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
  423. int prob_avg)
  424. {
  425. unsigned int nsecs = 0, overhead = mi->overhead;
  426. unsigned int ampdu_len = 1;
  427. /* do not account throughput if success prob is below 10% */
  428. if (prob_avg < MINSTREL_FRAC(10, 100))
  429. return 0;
  430. if (minstrel_ht_is_legacy_group(group))
  431. overhead = mi->overhead_legacy;
  432. else
  433. ampdu_len = minstrel_ht_avg_ampdu_len(mi);
  434. nsecs = 1000 * overhead / ampdu_len;
  435. nsecs += minstrel_mcs_groups[group].duration[rate] <<
  436. minstrel_mcs_groups[group].shift;
  437. /*
  438. * For the throughput calculation, limit the probability value to 90% to
  439. * account for collision related packet error rate fluctuation
  440. * (prob is scaled - see MINSTREL_FRAC above)
  441. */
  442. if (prob_avg > MINSTREL_FRAC(90, 100))
  443. prob_avg = MINSTREL_FRAC(90, 100);
  444. return MINSTREL_TRUNC(100 * ((prob_avg * 1000000) / nsecs));
  445. }
  446. /*
  447. * Find & sort topmost throughput rates
  448. *
  449. * If multiple rates provide equal throughput the sorting is based on their
  450. * current success probability. Higher success probability is preferred among
  451. * MCS groups, CCK rates do not provide aggregation and are therefore at last.
  452. */
  453. static void
  454. minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
  455. u16 *tp_list)
  456. {
  457. int cur_group, cur_idx, cur_tp_avg, cur_prob;
  458. int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
  459. int j = MAX_THR_RATES;
  460. cur_group = MI_RATE_GROUP(index);
  461. cur_idx = MI_RATE_IDX(index);
  462. cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg;
  463. cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
  464. do {
  465. tmp_group = MI_RATE_GROUP(tp_list[j - 1]);
  466. tmp_idx = MI_RATE_IDX(tp_list[j - 1]);
  467. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
  468. tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
  469. tmp_prob);
  470. if (cur_tp_avg < tmp_tp_avg ||
  471. (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
  472. break;
  473. j--;
  474. } while (j > 0);
  475. if (j < MAX_THR_RATES - 1) {
  476. memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) *
  477. (MAX_THR_RATES - (j + 1))));
  478. }
  479. if (j < MAX_THR_RATES)
  480. tp_list[j] = index;
  481. }
  482. /*
  483. * Find and set the topmost probability rate per sta and per group
  484. */
  485. static void
  486. minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 *dest, u16 index)
  487. {
  488. struct minstrel_mcs_group_data *mg;
  489. struct minstrel_rate_stats *mrs;
  490. int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
  491. int max_tp_group, max_tp_idx, max_tp_prob;
  492. int cur_tp_avg, cur_group, cur_idx;
  493. int max_gpr_group, max_gpr_idx;
  494. int max_gpr_tp_avg, max_gpr_prob;
  495. cur_group = MI_RATE_GROUP(index);
  496. cur_idx = MI_RATE_IDX(index);
  497. mg = &mi->groups[cur_group];
  498. mrs = &mg->rates[cur_idx];
  499. tmp_group = MI_RATE_GROUP(*dest);
  500. tmp_idx = MI_RATE_IDX(*dest);
  501. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
  502. tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  503. /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
  504. * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
  505. max_tp_group = MI_RATE_GROUP(mi->max_tp_rate[0]);
  506. max_tp_idx = MI_RATE_IDX(mi->max_tp_rate[0]);
  507. max_tp_prob = mi->groups[max_tp_group].rates[max_tp_idx].prob_avg;
  508. if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index)) &&
  509. !minstrel_ht_is_legacy_group(max_tp_group))
  510. return;
  511. /* skip rates faster than max tp rate with lower prob */
  512. if (minstrel_get_duration(mi->max_tp_rate[0]) > minstrel_get_duration(index) &&
  513. mrs->prob_avg < max_tp_prob)
  514. return;
  515. max_gpr_group = MI_RATE_GROUP(mg->max_group_prob_rate);
  516. max_gpr_idx = MI_RATE_IDX(mg->max_group_prob_rate);
  517. max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg;
  518. if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) {
  519. cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
  520. mrs->prob_avg);
  521. if (cur_tp_avg > tmp_tp_avg)
  522. *dest = index;
  523. max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
  524. max_gpr_idx,
  525. max_gpr_prob);
  526. if (cur_tp_avg > max_gpr_tp_avg)
  527. mg->max_group_prob_rate = index;
  528. } else {
  529. if (mrs->prob_avg > tmp_prob)
  530. *dest = index;
  531. if (mrs->prob_avg > max_gpr_prob)
  532. mg->max_group_prob_rate = index;
  533. }
  534. }
  535. /*
  536. * Assign new rate set per sta and use CCK rates only if the fastest
  537. * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted
  538. * rate sets where MCS and CCK rates are mixed, because CCK rates can
  539. * not use aggregation.
  540. */
  541. static void
  542. minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
  543. u16 tmp_mcs_tp_rate[MAX_THR_RATES],
  544. u16 tmp_legacy_tp_rate[MAX_THR_RATES])
  545. {
  546. unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
  547. int i;
  548. tmp_group = MI_RATE_GROUP(tmp_legacy_tp_rate[0]);
  549. tmp_idx = MI_RATE_IDX(tmp_legacy_tp_rate[0]);
  550. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
  551. tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  552. tmp_group = MI_RATE_GROUP(tmp_mcs_tp_rate[0]);
  553. tmp_idx = MI_RATE_IDX(tmp_mcs_tp_rate[0]);
  554. tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg;
  555. tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
  556. if (tmp_cck_tp > tmp_mcs_tp) {
  557. for(i = 0; i < MAX_THR_RATES; i++) {
  558. minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i],
  559. tmp_mcs_tp_rate);
  560. }
  561. }
  562. }
  563. /*
  564. * Try to increase robustness of max_prob rate by decrease number of
  565. * streams if possible.
  566. */
  567. static inline void
  568. minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
  569. {
  570. struct minstrel_mcs_group_data *mg;
  571. int tmp_max_streams, group, tmp_idx, tmp_prob;
  572. int tmp_tp = 0;
  573. if (!mi->sta->deflink.ht_cap.ht_supported)
  574. return;
  575. group = MI_RATE_GROUP(mi->max_tp_rate[0]);
  576. tmp_max_streams = minstrel_mcs_groups[group].streams;
  577. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  578. mg = &mi->groups[group];
  579. if (!mi->supported[group] || group == MINSTREL_CCK_GROUP)
  580. continue;
  581. tmp_idx = MI_RATE_IDX(mg->max_group_prob_rate);
  582. tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg;
  583. if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
  584. (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
  585. mi->max_prob_rate = mg->max_group_prob_rate;
  586. tmp_tp = minstrel_ht_get_tp_avg(mi, group,
  587. tmp_idx,
  588. tmp_prob);
  589. }
  590. }
  591. }
  592. static u16
  593. __minstrel_ht_get_sample_rate(struct minstrel_ht_sta *mi,
  594. enum minstrel_sample_type type)
  595. {
  596. u16 *rates = mi->sample[type].sample_rates;
  597. u16 cur;
  598. int i;
  599. for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
  600. if (!rates[i])
  601. continue;
  602. cur = rates[i];
  603. rates[i] = 0;
  604. return cur;
  605. }
  606. return 0;
  607. }
  608. static inline int
  609. minstrel_ewma(int old, int new, int weight)
  610. {
  611. int diff, incr;
  612. diff = new - old;
  613. incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
  614. return old + incr;
  615. }
  616. static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
  617. {
  618. s32 out_1 = *prev_1;
  619. s32 out_2 = *prev_2;
  620. s32 val;
  621. if (!in)
  622. in += 1;
  623. if (!out_1) {
  624. val = out_1 = in;
  625. goto out;
  626. }
  627. val = MINSTREL_AVG_COEFF1 * in;
  628. val += MINSTREL_AVG_COEFF2 * out_1;
  629. val += MINSTREL_AVG_COEFF3 * out_2;
  630. val >>= MINSTREL_SCALE;
  631. if (val > 1 << MINSTREL_SCALE)
  632. val = 1 << MINSTREL_SCALE;
  633. if (val < 0)
  634. val = 1;
  635. out:
  636. *prev_2 = out_1;
  637. *prev_1 = val;
  638. return val;
  639. }
  640. /*
  641. * Recalculate statistics and counters of a given rate
  642. */
  643. static void
  644. minstrel_ht_calc_rate_stats(struct minstrel_priv *mp,
  645. struct minstrel_rate_stats *mrs)
  646. {
  647. unsigned int cur_prob;
  648. if (unlikely(mrs->attempts > 0)) {
  649. cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
  650. minstrel_filter_avg_add(&mrs->prob_avg,
  651. &mrs->prob_avg_1, cur_prob);
  652. mrs->att_hist += mrs->attempts;
  653. mrs->succ_hist += mrs->success;
  654. }
  655. mrs->last_success = mrs->success;
  656. mrs->last_attempts = mrs->attempts;
  657. mrs->success = 0;
  658. mrs->attempts = 0;
  659. }
  660. static bool
  661. minstrel_ht_find_sample_rate(struct minstrel_ht_sta *mi, int type, int idx)
  662. {
  663. int i;
  664. for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) {
  665. u16 cur = mi->sample[type].sample_rates[i];
  666. if (cur == idx)
  667. return true;
  668. if (!cur)
  669. break;
  670. }
  671. return false;
  672. }
  673. static int
  674. minstrel_ht_move_sample_rates(struct minstrel_ht_sta *mi, int type,
  675. u32 fast_rate_dur, u32 slow_rate_dur)
  676. {
  677. u16 *rates = mi->sample[type].sample_rates;
  678. int i, j;
  679. for (i = 0, j = 0; i < MINSTREL_SAMPLE_RATES; i++) {
  680. u32 duration;
  681. bool valid = false;
  682. u16 cur;
  683. cur = rates[i];
  684. if (!cur)
  685. continue;
  686. duration = minstrel_get_duration(cur);
  687. switch (type) {
  688. case MINSTREL_SAMPLE_TYPE_SLOW:
  689. valid = duration > fast_rate_dur &&
  690. duration < slow_rate_dur;
  691. break;
  692. case MINSTREL_SAMPLE_TYPE_INC:
  693. case MINSTREL_SAMPLE_TYPE_JUMP:
  694. valid = duration < fast_rate_dur;
  695. break;
  696. default:
  697. valid = false;
  698. break;
  699. }
  700. if (!valid) {
  701. rates[i] = 0;
  702. continue;
  703. }
  704. if (i == j)
  705. continue;
  706. rates[j++] = cur;
  707. rates[i] = 0;
  708. }
  709. return j;
  710. }
  711. static int
  712. minstrel_ht_group_min_rate_offset(struct minstrel_ht_sta *mi, int group,
  713. u32 max_duration)
  714. {
  715. u16 supported = mi->supported[group];
  716. int i;
  717. for (i = 0; i < MCS_GROUP_RATES && supported; i++, supported >>= 1) {
  718. if (!(supported & BIT(0)))
  719. continue;
  720. if (minstrel_get_duration(MI_RATE(group, i)) >= max_duration)
  721. continue;
  722. return i;
  723. }
  724. return -1;
  725. }
  726. /*
  727. * Incremental update rates:
  728. * Flip through groups and pick the first group rate that is faster than the
  729. * highest currently selected rate
  730. */
  731. static u16
  732. minstrel_ht_next_inc_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur)
  733. {
  734. u8 type = MINSTREL_SAMPLE_TYPE_INC;
  735. int i, index = 0;
  736. u8 group;
  737. group = mi->sample[type].sample_group;
  738. for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
  739. group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
  740. index = minstrel_ht_group_min_rate_offset(mi, group,
  741. fast_rate_dur);
  742. if (index < 0)
  743. continue;
  744. index = MI_RATE(group, index & 0xf);
  745. if (!minstrel_ht_find_sample_rate(mi, type, index))
  746. goto out;
  747. }
  748. index = 0;
  749. out:
  750. mi->sample[type].sample_group = group;
  751. return index;
  752. }
  753. static int
  754. minstrel_ht_next_group_sample_rate(struct minstrel_ht_sta *mi, int group,
  755. u16 supported, int offset)
  756. {
  757. struct minstrel_mcs_group_data *mg = &mi->groups[group];
  758. u16 idx;
  759. int i;
  760. for (i = 0; i < MCS_GROUP_RATES; i++) {
  761. idx = sample_table[mg->column][mg->index];
  762. if (++mg->index >= MCS_GROUP_RATES) {
  763. mg->index = 0;
  764. if (++mg->column >= ARRAY_SIZE(sample_table))
  765. mg->column = 0;
  766. }
  767. if (idx < offset)
  768. continue;
  769. if (!(supported & BIT(idx)))
  770. continue;
  771. return MI_RATE(group, idx);
  772. }
  773. return -1;
  774. }
  775. /*
  776. * Jump rates:
  777. * Sample random rates, use those that are faster than the highest
  778. * currently selected rate. Rates between the fastest and the slowest
  779. * get sorted into the slow sample bucket, but only if it has room
  780. */
  781. static u16
  782. minstrel_ht_next_jump_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur,
  783. u32 slow_rate_dur, int *slow_rate_ofs)
  784. {
  785. struct minstrel_rate_stats *mrs;
  786. u32 max_duration = slow_rate_dur;
  787. int i, index, offset;
  788. u16 *slow_rates;
  789. u16 supported;
  790. u32 duration;
  791. u8 group;
  792. if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
  793. max_duration = fast_rate_dur;
  794. slow_rates = mi->sample[MINSTREL_SAMPLE_TYPE_SLOW].sample_rates;
  795. group = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group;
  796. for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
  797. u8 type;
  798. group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups);
  799. supported = mi->supported[group];
  800. if (!supported)
  801. continue;
  802. offset = minstrel_ht_group_min_rate_offset(mi, group,
  803. max_duration);
  804. if (offset < 0)
  805. continue;
  806. index = minstrel_ht_next_group_sample_rate(mi, group, supported,
  807. offset);
  808. if (index < 0)
  809. continue;
  810. duration = minstrel_get_duration(index);
  811. if (duration < fast_rate_dur)
  812. type = MINSTREL_SAMPLE_TYPE_JUMP;
  813. else
  814. type = MINSTREL_SAMPLE_TYPE_SLOW;
  815. if (minstrel_ht_find_sample_rate(mi, type, index))
  816. continue;
  817. if (type == MINSTREL_SAMPLE_TYPE_JUMP)
  818. goto found;
  819. if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
  820. continue;
  821. if (duration >= slow_rate_dur)
  822. continue;
  823. /* skip slow rates with high success probability */
  824. mrs = minstrel_get_ratestats(mi, index);
  825. if (mrs->prob_avg > MINSTREL_FRAC(95, 100))
  826. continue;
  827. slow_rates[(*slow_rate_ofs)++] = index;
  828. if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES)
  829. max_duration = fast_rate_dur;
  830. }
  831. index = 0;
  832. found:
  833. mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group = group;
  834. return index;
  835. }
  836. static void
  837. minstrel_ht_refill_sample_rates(struct minstrel_ht_sta *mi)
  838. {
  839. u32 prob_dur = minstrel_get_duration(mi->max_prob_rate);
  840. u32 tp_dur = minstrel_get_duration(mi->max_tp_rate[0]);
  841. u32 tp2_dur = minstrel_get_duration(mi->max_tp_rate[1]);
  842. u32 fast_rate_dur = min(min(tp_dur, tp2_dur), prob_dur);
  843. u32 slow_rate_dur = max(max(tp_dur, tp2_dur), prob_dur);
  844. u16 *rates;
  845. int i, j;
  846. rates = mi->sample[MINSTREL_SAMPLE_TYPE_INC].sample_rates;
  847. i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_INC,
  848. fast_rate_dur, slow_rate_dur);
  849. while (i < MINSTREL_SAMPLE_RATES) {
  850. rates[i] = minstrel_ht_next_inc_rate(mi, tp_dur);
  851. if (!rates[i])
  852. break;
  853. i++;
  854. }
  855. rates = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_rates;
  856. i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_JUMP,
  857. fast_rate_dur, slow_rate_dur);
  858. j = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_SLOW,
  859. fast_rate_dur, slow_rate_dur);
  860. while (i < MINSTREL_SAMPLE_RATES) {
  861. rates[i] = minstrel_ht_next_jump_rate(mi, fast_rate_dur,
  862. slow_rate_dur, &j);
  863. if (!rates[i])
  864. break;
  865. i++;
  866. }
  867. for (i = 0; i < ARRAY_SIZE(mi->sample); i++)
  868. memcpy(mi->sample[i].cur_sample_rates, mi->sample[i].sample_rates,
  869. sizeof(mi->sample[i].cur_sample_rates));
  870. }
  871. /*
  872. * Update rate statistics and select new primary rates
  873. *
  874. * Rules for rate selection:
  875. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  876. * probability and throughput during strong fluctuations
  877. * - as long as the max prob rate has a probability of more than 75%, pick
  878. * higher throughput rates, even if the probablity is a bit lower
  879. */
  880. static void
  881. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  882. {
  883. struct minstrel_mcs_group_data *mg;
  884. struct minstrel_rate_stats *mrs;
  885. int group, i, j, cur_prob;
  886. u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
  887. u16 tmp_legacy_tp_rate[MAX_THR_RATES], tmp_max_prob_rate;
  888. u16 index;
  889. bool ht_supported = mi->sta->deflink.ht_cap.ht_supported;
  890. if (mi->ampdu_packets > 0) {
  891. if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN))
  892. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  893. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets),
  894. EWMA_LEVEL);
  895. else
  896. mi->avg_ampdu_len = 0;
  897. mi->ampdu_len = 0;
  898. mi->ampdu_packets = 0;
  899. }
  900. if (mi->supported[MINSTREL_CCK_GROUP])
  901. group = MINSTREL_CCK_GROUP;
  902. else if (mi->supported[MINSTREL_OFDM_GROUP])
  903. group = MINSTREL_OFDM_GROUP;
  904. else
  905. group = 0;
  906. index = MI_RATE(group, 0);
  907. for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++)
  908. tmp_legacy_tp_rate[j] = index;
  909. if (mi->supported[MINSTREL_VHT_GROUP_0])
  910. group = MINSTREL_VHT_GROUP_0;
  911. else if (ht_supported)
  912. group = MINSTREL_HT_GROUP_0;
  913. else if (mi->supported[MINSTREL_CCK_GROUP])
  914. group = MINSTREL_CCK_GROUP;
  915. else
  916. group = MINSTREL_OFDM_GROUP;
  917. index = MI_RATE(group, 0);
  918. tmp_max_prob_rate = index;
  919. for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++)
  920. tmp_mcs_tp_rate[j] = index;
  921. /* Find best rate sets within all MCS groups*/
  922. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  923. u16 *tp_rate = tmp_mcs_tp_rate;
  924. u16 last_prob = 0;
  925. mg = &mi->groups[group];
  926. if (!mi->supported[group])
  927. continue;
  928. /* (re)Initialize group rate indexes */
  929. for(j = 0; j < MAX_THR_RATES; j++)
  930. tmp_group_tp_rate[j] = MI_RATE(group, 0);
  931. if (group == MINSTREL_CCK_GROUP && ht_supported)
  932. tp_rate = tmp_legacy_tp_rate;
  933. for (i = MCS_GROUP_RATES - 1; i >= 0; i--) {
  934. if (!(mi->supported[group] & BIT(i)))
  935. continue;
  936. index = MI_RATE(group, i);
  937. mrs = &mg->rates[i];
  938. mrs->retry_updated = false;
  939. minstrel_ht_calc_rate_stats(mp, mrs);
  940. if (mrs->att_hist)
  941. last_prob = max(last_prob, mrs->prob_avg);
  942. else
  943. mrs->prob_avg = max(last_prob, mrs->prob_avg);
  944. cur_prob = mrs->prob_avg;
  945. if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
  946. continue;
  947. /* Find max throughput rate set */
  948. minstrel_ht_sort_best_tp_rates(mi, index, tp_rate);
  949. /* Find max throughput rate set within a group */
  950. minstrel_ht_sort_best_tp_rates(mi, index,
  951. tmp_group_tp_rate);
  952. }
  953. memcpy(mg->max_group_tp_rate, tmp_group_tp_rate,
  954. sizeof(mg->max_group_tp_rate));
  955. }
  956. /* Assign new rate set per sta */
  957. minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate,
  958. tmp_legacy_tp_rate);
  959. memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate));
  960. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  961. if (!mi->supported[group])
  962. continue;
  963. mg = &mi->groups[group];
  964. mg->max_group_prob_rate = MI_RATE(group, 0);
  965. for (i = 0; i < MCS_GROUP_RATES; i++) {
  966. if (!(mi->supported[group] & BIT(i)))
  967. continue;
  968. index = MI_RATE(group, i);
  969. /* Find max probability rate per group and global */
  970. minstrel_ht_set_best_prob_rate(mi, &tmp_max_prob_rate,
  971. index);
  972. }
  973. }
  974. mi->max_prob_rate = tmp_max_prob_rate;
  975. /* Try to increase robustness of max_prob_rate*/
  976. minstrel_ht_prob_rate_reduce_streams(mi);
  977. minstrel_ht_refill_sample_rates(mi);
  978. #ifdef CONFIG_MAC80211_DEBUGFS
  979. /* use fixed index if set */
  980. if (mp->fixed_rate_idx != -1) {
  981. for (i = 0; i < 4; i++)
  982. mi->max_tp_rate[i] = mp->fixed_rate_idx;
  983. mi->max_prob_rate = mp->fixed_rate_idx;
  984. }
  985. #endif
  986. /* Reset update timer */
  987. mi->last_stats_update = jiffies;
  988. mi->sample_time = jiffies;
  989. }
  990. static bool
  991. minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  992. struct ieee80211_tx_rate *rate)
  993. {
  994. int i;
  995. if (rate->idx < 0)
  996. return false;
  997. if (!rate->count)
  998. return false;
  999. if (rate->flags & IEEE80211_TX_RC_MCS ||
  1000. rate->flags & IEEE80211_TX_RC_VHT_MCS)
  1001. return true;
  1002. for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++)
  1003. if (rate->idx == mp->cck_rates[i])
  1004. return true;
  1005. for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++)
  1006. if (rate->idx == mp->ofdm_rates[mi->band][i])
  1007. return true;
  1008. return false;
  1009. }
  1010. /*
  1011. * Check whether rate_status contains valid information.
  1012. */
  1013. static bool
  1014. minstrel_ht_ri_txstat_valid(struct minstrel_priv *mp,
  1015. struct minstrel_ht_sta *mi,
  1016. struct ieee80211_rate_status *rate_status)
  1017. {
  1018. int i;
  1019. if (!rate_status)
  1020. return false;
  1021. if (!rate_status->try_count)
  1022. return false;
  1023. if (rate_status->rate_idx.flags & RATE_INFO_FLAGS_MCS ||
  1024. rate_status->rate_idx.flags & RATE_INFO_FLAGS_VHT_MCS)
  1025. return true;
  1026. for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++) {
  1027. if (rate_status->rate_idx.legacy ==
  1028. minstrel_cck_bitrates[ mp->cck_rates[i] ])
  1029. return true;
  1030. }
  1031. for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates); i++) {
  1032. if (rate_status->rate_idx.legacy ==
  1033. minstrel_ofdm_bitrates[ mp->ofdm_rates[mi->band][i] ])
  1034. return true;
  1035. }
  1036. return false;
  1037. }
  1038. static void
  1039. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary)
  1040. {
  1041. int group, orig_group;
  1042. orig_group = group = MI_RATE_GROUP(*idx);
  1043. while (group > 0) {
  1044. group--;
  1045. if (!mi->supported[group])
  1046. continue;
  1047. if (minstrel_mcs_groups[group].streams >
  1048. minstrel_mcs_groups[orig_group].streams)
  1049. continue;
  1050. if (primary)
  1051. *idx = mi->groups[group].max_group_tp_rate[0];
  1052. else
  1053. *idx = mi->groups[group].max_group_tp_rate[1];
  1054. break;
  1055. }
  1056. }
  1057. static void
  1058. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  1059. void *priv_sta, struct ieee80211_tx_status *st)
  1060. {
  1061. struct ieee80211_tx_info *info = st->info;
  1062. struct minstrel_ht_sta *mi = priv_sta;
  1063. struct ieee80211_tx_rate *ar = info->status.rates;
  1064. struct minstrel_rate_stats *rate, *rate2;
  1065. struct minstrel_priv *mp = priv;
  1066. u32 update_interval = mp->update_interval;
  1067. bool last, update = false;
  1068. int i;
  1069. /* Ignore packet that was sent with noAck flag */
  1070. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1071. return;
  1072. /* This packet was aggregated but doesn't carry status info */
  1073. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  1074. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  1075. return;
  1076. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  1077. info->status.ampdu_ack_len =
  1078. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  1079. info->status.ampdu_len = 1;
  1080. }
  1081. /* wraparound */
  1082. if (mi->total_packets >= ~0 - info->status.ampdu_len) {
  1083. mi->total_packets = 0;
  1084. mi->sample_packets = 0;
  1085. }
  1086. mi->total_packets += info->status.ampdu_len;
  1087. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  1088. mi->sample_packets += info->status.ampdu_len;
  1089. mi->ampdu_packets++;
  1090. mi->ampdu_len += info->status.ampdu_len;
  1091. if (st->rates && st->n_rates) {
  1092. last = !minstrel_ht_ri_txstat_valid(mp, mi, &(st->rates[0]));
  1093. for (i = 0; !last; i++) {
  1094. last = (i == st->n_rates - 1) ||
  1095. !minstrel_ht_ri_txstat_valid(mp, mi,
  1096. &(st->rates[i + 1]));
  1097. rate = minstrel_ht_ri_get_stats(mp, mi,
  1098. &(st->rates[i]));
  1099. if (last)
  1100. rate->success += info->status.ampdu_ack_len;
  1101. rate->attempts += st->rates[i].try_count *
  1102. info->status.ampdu_len;
  1103. }
  1104. } else {
  1105. last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]);
  1106. for (i = 0; !last; i++) {
  1107. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  1108. !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]);
  1109. rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
  1110. if (last)
  1111. rate->success += info->status.ampdu_ack_len;
  1112. rate->attempts += ar[i].count * info->status.ampdu_len;
  1113. }
  1114. }
  1115. if (mp->hw->max_rates > 1) {
  1116. /*
  1117. * check for sudden death of spatial multiplexing,
  1118. * downgrade to a lower number of streams if necessary.
  1119. */
  1120. rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]);
  1121. if (rate->attempts > 30 &&
  1122. rate->success < rate->attempts / 4) {
  1123. minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true);
  1124. update = true;
  1125. }
  1126. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]);
  1127. if (rate2->attempts > 30 &&
  1128. rate2->success < rate2->attempts / 4) {
  1129. minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false);
  1130. update = true;
  1131. }
  1132. }
  1133. if (time_after(jiffies, mi->last_stats_update + update_interval)) {
  1134. update = true;
  1135. minstrel_ht_update_stats(mp, mi);
  1136. }
  1137. if (update)
  1138. minstrel_ht_update_rates(mp, mi);
  1139. }
  1140. static void
  1141. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  1142. int index)
  1143. {
  1144. struct minstrel_rate_stats *mrs;
  1145. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  1146. unsigned int cw = mp->cw_min;
  1147. unsigned int ctime = 0;
  1148. unsigned int t_slot = 9; /* FIXME */
  1149. unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi);
  1150. unsigned int overhead = 0, overhead_rtscts = 0;
  1151. mrs = minstrel_get_ratestats(mi, index);
  1152. if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) {
  1153. mrs->retry_count = 1;
  1154. mrs->retry_count_rtscts = 1;
  1155. return;
  1156. }
  1157. mrs->retry_count = 2;
  1158. mrs->retry_count_rtscts = 2;
  1159. mrs->retry_updated = true;
  1160. tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000;
  1161. /* Contention time for first 2 tries */
  1162. ctime = (t_slot * cw) >> 1;
  1163. cw = min((cw << 1) | 1, mp->cw_max);
  1164. ctime += (t_slot * cw) >> 1;
  1165. cw = min((cw << 1) | 1, mp->cw_max);
  1166. if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index))) {
  1167. overhead = mi->overhead_legacy;
  1168. overhead_rtscts = mi->overhead_legacy_rtscts;
  1169. } else {
  1170. overhead = mi->overhead;
  1171. overhead_rtscts = mi->overhead_rtscts;
  1172. }
  1173. /* Total TX time for data and Contention after first 2 tries */
  1174. tx_time = ctime + 2 * (overhead + tx_time_data);
  1175. tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
  1176. /* See how many more tries we can fit inside segment size */
  1177. do {
  1178. /* Contention time for this try */
  1179. ctime = (t_slot * cw) >> 1;
  1180. cw = min((cw << 1) | 1, mp->cw_max);
  1181. /* Total TX time after this try */
  1182. tx_time += ctime + overhead + tx_time_data;
  1183. tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
  1184. if (tx_time_rtscts < mp->segment_size)
  1185. mrs->retry_count_rtscts++;
  1186. } while ((tx_time < mp->segment_size) &&
  1187. (++mrs->retry_count < mp->max_retry));
  1188. }
  1189. static void
  1190. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  1191. struct ieee80211_sta_rates *ratetbl, int offset, int index)
  1192. {
  1193. int group_idx = MI_RATE_GROUP(index);
  1194. const struct mcs_group *group = &minstrel_mcs_groups[group_idx];
  1195. struct minstrel_rate_stats *mrs;
  1196. u8 idx;
  1197. u16 flags = group->flags;
  1198. mrs = minstrel_get_ratestats(mi, index);
  1199. if (!mrs->retry_updated)
  1200. minstrel_calc_retransmit(mp, mi, index);
  1201. if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
  1202. ratetbl->rate[offset].count = 2;
  1203. ratetbl->rate[offset].count_rts = 2;
  1204. ratetbl->rate[offset].count_cts = 2;
  1205. } else {
  1206. ratetbl->rate[offset].count = mrs->retry_count;
  1207. ratetbl->rate[offset].count_cts = mrs->retry_count;
  1208. ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
  1209. }
  1210. index = MI_RATE_IDX(index);
  1211. if (group_idx == MINSTREL_CCK_GROUP)
  1212. idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
  1213. else if (group_idx == MINSTREL_OFDM_GROUP)
  1214. idx = mp->ofdm_rates[mi->band][index %
  1215. ARRAY_SIZE(mp->ofdm_rates[0])];
  1216. else if (flags & IEEE80211_TX_RC_VHT_MCS)
  1217. idx = ((group->streams - 1) << 4) |
  1218. (index & 0xF);
  1219. else
  1220. idx = index + (group->streams - 1) * 8;
  1221. /* enable RTS/CTS if needed:
  1222. * - if station is in dynamic SMPS (and streams > 1)
  1223. * - for fallback rates, to increase chances of getting through
  1224. */
  1225. if (offset > 0 ||
  1226. (mi->sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC &&
  1227. group->streams > 1)) {
  1228. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  1229. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  1230. }
  1231. ratetbl->rate[offset].idx = idx;
  1232. ratetbl->rate[offset].flags = flags;
  1233. }
  1234. static inline int
  1235. minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate)
  1236. {
  1237. int group = MI_RATE_GROUP(rate);
  1238. rate = MI_RATE_IDX(rate);
  1239. return mi->groups[group].rates[rate].prob_avg;
  1240. }
  1241. static int
  1242. minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi)
  1243. {
  1244. int group = MI_RATE_GROUP(mi->max_prob_rate);
  1245. const struct mcs_group *g = &minstrel_mcs_groups[group];
  1246. int rate = MI_RATE_IDX(mi->max_prob_rate);
  1247. unsigned int duration;
  1248. /* Disable A-MSDU if max_prob_rate is bad */
  1249. if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100))
  1250. return 1;
  1251. duration = g->duration[rate];
  1252. duration <<= g->shift;
  1253. /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */
  1254. if (duration > MCS_DURATION(1, 0, 52))
  1255. return 500;
  1256. /*
  1257. * If the rate is slower than single-stream MCS4, limit A-MSDU to usual
  1258. * data packet size
  1259. */
  1260. if (duration > MCS_DURATION(1, 0, 104))
  1261. return 1600;
  1262. /*
  1263. * If the rate is slower than single-stream MCS7, or if the max throughput
  1264. * rate success probability is less than 75%, limit A-MSDU to twice the usual
  1265. * data packet size
  1266. */
  1267. if (duration > MCS_DURATION(1, 0, 260) ||
  1268. (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) <
  1269. MINSTREL_FRAC(75, 100)))
  1270. return 3200;
  1271. /*
  1272. * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes.
  1273. * Since aggregation sessions are started/stopped without txq flush, use
  1274. * the limit here to avoid the complexity of having to de-aggregate
  1275. * packets in the queue.
  1276. */
  1277. if (!mi->sta->deflink.vht_cap.vht_supported)
  1278. return IEEE80211_MAX_MPDU_LEN_HT_BA;
  1279. /* unlimited */
  1280. return 0;
  1281. }
  1282. static void
  1283. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  1284. {
  1285. struct ieee80211_sta_rates *rates;
  1286. int i = 0;
  1287. int max_rates = min_t(int, mp->hw->max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
  1288. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  1289. if (!rates)
  1290. return;
  1291. /* Start with max_tp_rate[0] */
  1292. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]);
  1293. /* Fill up remaining, keep one entry for max_probe_rate */
  1294. for (; i < (max_rates - 1); i++)
  1295. minstrel_ht_set_rate(mp, mi, rates, i, mi->max_tp_rate[i]);
  1296. if (i < max_rates)
  1297. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  1298. if (i < IEEE80211_TX_RATE_TABLE_SIZE)
  1299. rates->rate[i].idx = -1;
  1300. mi->sta->deflink.agg.max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi);
  1301. ieee80211_sta_recalc_aggregates(mi->sta);
  1302. rate_control_set_rates(mp->hw, mi->sta, rates);
  1303. }
  1304. static u16
  1305. minstrel_ht_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  1306. {
  1307. u8 seq;
  1308. if (mp->hw->max_rates > 1) {
  1309. seq = mi->sample_seq;
  1310. mi->sample_seq = (seq + 1) % ARRAY_SIZE(minstrel_sample_seq);
  1311. seq = minstrel_sample_seq[seq];
  1312. } else {
  1313. seq = MINSTREL_SAMPLE_TYPE_INC;
  1314. }
  1315. return __minstrel_ht_get_sample_rate(mi, seq);
  1316. }
  1317. static void
  1318. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  1319. struct ieee80211_tx_rate_control *txrc)
  1320. {
  1321. const struct mcs_group *sample_group;
  1322. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  1323. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  1324. struct minstrel_ht_sta *mi = priv_sta;
  1325. struct minstrel_priv *mp = priv;
  1326. u16 sample_idx;
  1327. info->flags |= mi->tx_flags;
  1328. #ifdef CONFIG_MAC80211_DEBUGFS
  1329. if (mp->fixed_rate_idx != -1)
  1330. return;
  1331. #endif
  1332. /* Don't use EAPOL frames for sampling on non-mrr hw */
  1333. if (mp->hw->max_rates == 1 &&
  1334. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  1335. return;
  1336. if (time_is_after_jiffies(mi->sample_time))
  1337. return;
  1338. mi->sample_time = jiffies + MINSTREL_SAMPLE_INTERVAL;
  1339. sample_idx = minstrel_ht_get_sample_rate(mp, mi);
  1340. if (!sample_idx)
  1341. return;
  1342. sample_group = &minstrel_mcs_groups[MI_RATE_GROUP(sample_idx)];
  1343. sample_idx = MI_RATE_IDX(sample_idx);
  1344. if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] &&
  1345. (sample_idx >= 4) != txrc->short_preamble)
  1346. return;
  1347. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  1348. rate->count = 1;
  1349. if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) {
  1350. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  1351. rate->idx = mp->cck_rates[idx];
  1352. } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) {
  1353. int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]);
  1354. rate->idx = mp->ofdm_rates[mi->band][idx];
  1355. } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) {
  1356. ieee80211_rate_set_vht(rate, MI_RATE_IDX(sample_idx),
  1357. sample_group->streams);
  1358. } else {
  1359. rate->idx = sample_idx + (sample_group->streams - 1) * 8;
  1360. }
  1361. rate->flags = sample_group->flags;
  1362. }
  1363. static void
  1364. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  1365. struct ieee80211_supported_band *sband,
  1366. struct ieee80211_sta *sta)
  1367. {
  1368. int i;
  1369. if (sband->band != NL80211_BAND_2GHZ)
  1370. return;
  1371. if (sta->deflink.ht_cap.ht_supported &&
  1372. !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES))
  1373. return;
  1374. for (i = 0; i < 4; i++) {
  1375. if (mp->cck_rates[i] == 0xff ||
  1376. !rate_supported(sta, sband->band, mp->cck_rates[i]))
  1377. continue;
  1378. mi->supported[MINSTREL_CCK_GROUP] |= BIT(i);
  1379. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  1380. mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4);
  1381. }
  1382. }
  1383. static void
  1384. minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  1385. struct ieee80211_supported_band *sband,
  1386. struct ieee80211_sta *sta)
  1387. {
  1388. const u8 *rates;
  1389. int i;
  1390. if (sta->deflink.ht_cap.ht_supported)
  1391. return;
  1392. rates = mp->ofdm_rates[sband->band];
  1393. for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) {
  1394. if (rates[i] == 0xff ||
  1395. !rate_supported(sta, sband->band, rates[i]))
  1396. continue;
  1397. mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i);
  1398. }
  1399. }
  1400. static void
  1401. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  1402. struct cfg80211_chan_def *chandef,
  1403. struct ieee80211_sta *sta, void *priv_sta)
  1404. {
  1405. struct minstrel_priv *mp = priv;
  1406. struct minstrel_ht_sta *mi = priv_sta;
  1407. struct ieee80211_mcs_info *mcs = &sta->deflink.ht_cap.mcs;
  1408. u16 ht_cap = sta->deflink.ht_cap.cap;
  1409. struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
  1410. const struct ieee80211_rate *ctl_rate;
  1411. struct sta_info *sta_info;
  1412. bool ldpc, erp;
  1413. int use_vht;
  1414. int n_supported = 0;
  1415. int ack_dur;
  1416. int stbc;
  1417. int i;
  1418. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB);
  1419. if (vht_cap->vht_supported)
  1420. use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0);
  1421. else
  1422. use_vht = 0;
  1423. memset(mi, 0, sizeof(*mi));
  1424. mi->sta = sta;
  1425. mi->band = sband->band;
  1426. mi->last_stats_update = jiffies;
  1427. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  1428. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  1429. mi->overhead += ack_dur;
  1430. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  1431. ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)];
  1432. erp = ctl_rate->flags & IEEE80211_RATE_ERP_G;
  1433. ack_dur = ieee80211_frame_duration(sband->band, 10,
  1434. ctl_rate->bitrate, erp, 1,
  1435. ieee80211_chandef_get_shift(chandef));
  1436. mi->overhead_legacy = ack_dur;
  1437. mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur;
  1438. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  1439. if (!use_vht) {
  1440. stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >>
  1441. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  1442. ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING;
  1443. } else {
  1444. stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >>
  1445. IEEE80211_VHT_CAP_RXSTBC_SHIFT;
  1446. ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC;
  1447. }
  1448. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  1449. if (ldpc)
  1450. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  1451. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  1452. u32 gflags = minstrel_mcs_groups[i].flags;
  1453. int bw, nss;
  1454. mi->supported[i] = 0;
  1455. if (minstrel_ht_is_legacy_group(i))
  1456. continue;
  1457. if (gflags & IEEE80211_TX_RC_SHORT_GI) {
  1458. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1459. if (!(ht_cap & IEEE80211_HT_CAP_SGI_40))
  1460. continue;
  1461. } else {
  1462. if (!(ht_cap & IEEE80211_HT_CAP_SGI_20))
  1463. continue;
  1464. }
  1465. }
  1466. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  1467. sta->deflink.bandwidth < IEEE80211_STA_RX_BW_40)
  1468. continue;
  1469. nss = minstrel_mcs_groups[i].streams;
  1470. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  1471. if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC && nss > 1)
  1472. continue;
  1473. /* HT rate */
  1474. if (gflags & IEEE80211_TX_RC_MCS) {
  1475. if (use_vht && minstrel_vht_only)
  1476. continue;
  1477. mi->supported[i] = mcs->rx_mask[nss - 1];
  1478. if (mi->supported[i])
  1479. n_supported++;
  1480. continue;
  1481. }
  1482. /* VHT rate */
  1483. if (!vht_cap->vht_supported ||
  1484. WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) ||
  1485. WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH))
  1486. continue;
  1487. if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) {
  1488. if (sta->deflink.bandwidth < IEEE80211_STA_RX_BW_80 ||
  1489. ((gflags & IEEE80211_TX_RC_SHORT_GI) &&
  1490. !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) {
  1491. continue;
  1492. }
  1493. }
  1494. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1495. bw = BW_40;
  1496. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1497. bw = BW_80;
  1498. else
  1499. bw = BW_20;
  1500. mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss,
  1501. vht_cap->vht_mcs.tx_mcs_map);
  1502. if (mi->supported[i])
  1503. n_supported++;
  1504. }
  1505. sta_info = container_of(sta, struct sta_info, sta);
  1506. mi->use_short_preamble = test_sta_flag(sta_info, WLAN_STA_SHORT_PREAMBLE) &&
  1507. sta_info->sdata->vif.bss_conf.use_short_preamble;
  1508. minstrel_ht_update_cck(mp, mi, sband, sta);
  1509. minstrel_ht_update_ofdm(mp, mi, sband, sta);
  1510. /* create an initial rate table with the lowest supported rates */
  1511. minstrel_ht_update_stats(mp, mi);
  1512. minstrel_ht_update_rates(mp, mi);
  1513. }
  1514. static void
  1515. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1516. struct cfg80211_chan_def *chandef,
  1517. struct ieee80211_sta *sta, void *priv_sta)
  1518. {
  1519. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1520. }
  1521. static void
  1522. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  1523. struct cfg80211_chan_def *chandef,
  1524. struct ieee80211_sta *sta, void *priv_sta,
  1525. u32 changed)
  1526. {
  1527. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  1528. }
  1529. static void *
  1530. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1531. {
  1532. struct ieee80211_supported_band *sband;
  1533. struct minstrel_ht_sta *mi;
  1534. struct minstrel_priv *mp = priv;
  1535. struct ieee80211_hw *hw = mp->hw;
  1536. int max_rates = 0;
  1537. int i;
  1538. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1539. sband = hw->wiphy->bands[i];
  1540. if (sband && sband->n_bitrates > max_rates)
  1541. max_rates = sband->n_bitrates;
  1542. }
  1543. return kzalloc(sizeof(*mi), gfp);
  1544. }
  1545. static void
  1546. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  1547. {
  1548. kfree(priv_sta);
  1549. }
  1550. static void
  1551. minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband,
  1552. const s16 *bitrates, int n_rates, u32 rate_flags)
  1553. {
  1554. int i, j;
  1555. for (i = 0; i < sband->n_bitrates; i++) {
  1556. struct ieee80211_rate *rate = &sband->bitrates[i];
  1557. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1558. continue;
  1559. for (j = 0; j < n_rates; j++) {
  1560. if (rate->bitrate != bitrates[j])
  1561. continue;
  1562. dest[j] = i;
  1563. break;
  1564. }
  1565. }
  1566. }
  1567. static void
  1568. minstrel_ht_init_cck_rates(struct minstrel_priv *mp)
  1569. {
  1570. static const s16 bitrates[4] = { 10, 20, 55, 110 };
  1571. struct ieee80211_supported_band *sband;
  1572. u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  1573. memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates));
  1574. sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
  1575. if (!sband)
  1576. return;
  1577. BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates));
  1578. minstrel_ht_fill_rate_array(mp->cck_rates, sband,
  1579. minstrel_cck_bitrates,
  1580. ARRAY_SIZE(minstrel_cck_bitrates),
  1581. rate_flags);
  1582. }
  1583. static void
  1584. minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band)
  1585. {
  1586. static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 };
  1587. struct ieee80211_supported_band *sband;
  1588. u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  1589. memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band]));
  1590. sband = mp->hw->wiphy->bands[band];
  1591. if (!sband)
  1592. return;
  1593. BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates));
  1594. minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband,
  1595. minstrel_ofdm_bitrates,
  1596. ARRAY_SIZE(minstrel_ofdm_bitrates),
  1597. rate_flags);
  1598. }
  1599. static void *
  1600. minstrel_ht_alloc(struct ieee80211_hw *hw)
  1601. {
  1602. struct minstrel_priv *mp;
  1603. int i;
  1604. mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
  1605. if (!mp)
  1606. return NULL;
  1607. /* contention window settings
  1608. * Just an approximation. Using the per-queue values would complicate
  1609. * the calculations and is probably unnecessary */
  1610. mp->cw_min = 15;
  1611. mp->cw_max = 1023;
  1612. /* maximum time that the hw is allowed to stay in one MRR segment */
  1613. mp->segment_size = 6000;
  1614. if (hw->max_rate_tries > 0)
  1615. mp->max_retry = hw->max_rate_tries;
  1616. else
  1617. /* safe default, does not necessarily have to match hw properties */
  1618. mp->max_retry = 7;
  1619. if (hw->max_rates >= 4)
  1620. mp->has_mrr = true;
  1621. mp->hw = hw;
  1622. mp->update_interval = HZ / 20;
  1623. minstrel_ht_init_cck_rates(mp);
  1624. for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++)
  1625. minstrel_ht_init_ofdm_rates(mp, i);
  1626. return mp;
  1627. }
  1628. #ifdef CONFIG_MAC80211_DEBUGFS
  1629. static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv,
  1630. struct dentry *debugfsdir)
  1631. {
  1632. struct minstrel_priv *mp = priv;
  1633. mp->fixed_rate_idx = (u32) -1;
  1634. debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir,
  1635. &mp->fixed_rate_idx);
  1636. }
  1637. #endif
  1638. static void
  1639. minstrel_ht_free(void *priv)
  1640. {
  1641. kfree(priv);
  1642. }
  1643. static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
  1644. {
  1645. struct minstrel_ht_sta *mi = priv_sta;
  1646. int i, j, prob, tp_avg;
  1647. i = MI_RATE_GROUP(mi->max_tp_rate[0]);
  1648. j = MI_RATE_IDX(mi->max_tp_rate[0]);
  1649. prob = mi->groups[i].rates[j].prob_avg;
  1650. /* convert tp_avg from pkt per second in kbps */
  1651. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10;
  1652. tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024;
  1653. return tp_avg;
  1654. }
  1655. static const struct rate_control_ops mac80211_minstrel_ht = {
  1656. .name = "minstrel_ht",
  1657. .capa = RATE_CTRL_CAPA_AMPDU_TRIGGER,
  1658. .tx_status_ext = minstrel_ht_tx_status,
  1659. .get_rate = minstrel_ht_get_rate,
  1660. .rate_init = minstrel_ht_rate_init,
  1661. .rate_update = minstrel_ht_rate_update,
  1662. .alloc_sta = minstrel_ht_alloc_sta,
  1663. .free_sta = minstrel_ht_free_sta,
  1664. .alloc = minstrel_ht_alloc,
  1665. .free = minstrel_ht_free,
  1666. #ifdef CONFIG_MAC80211_DEBUGFS
  1667. .add_debugfs = minstrel_ht_add_debugfs,
  1668. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  1669. #endif
  1670. .get_expected_throughput = minstrel_ht_get_expected_throughput,
  1671. };
  1672. static void __init init_sample_table(void)
  1673. {
  1674. int col, i, new_idx;
  1675. u8 rnd[MCS_GROUP_RATES];
  1676. memset(sample_table, 0xff, sizeof(sample_table));
  1677. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  1678. get_random_bytes(rnd, sizeof(rnd));
  1679. for (i = 0; i < MCS_GROUP_RATES; i++) {
  1680. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  1681. while (sample_table[col][new_idx] != 0xff)
  1682. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  1683. sample_table[col][new_idx] = i;
  1684. }
  1685. }
  1686. }
  1687. int __init
  1688. rc80211_minstrel_init(void)
  1689. {
  1690. init_sample_table();
  1691. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  1692. }
  1693. void
  1694. rc80211_minstrel_exit(void)
  1695. {
  1696. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  1697. }