rc80211_minstrel_ht.c 39 KB

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