rc80211_minstrel_ht.c 37 KB

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