rc80211_minstrel_ht.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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/ieee80211.h>
  14. #include <net/mac80211.h>
  15. #include "rate.h"
  16. #include "rc80211_minstrel.h"
  17. #include "rc80211_minstrel_ht.h"
  18. #define AVG_PKT_SIZE 1200
  19. /* Number of bits for an average sized packet */
  20. #define MCS_NBITS (AVG_PKT_SIZE << 3)
  21. /* Number of symbols for a packet with (bps) bits per symbol */
  22. #define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
  23. /* Transmission time (nanoseconds) for a packet containing (syms) symbols */
  24. #define MCS_SYMBOL_TIME(sgi, syms) \
  25. (sgi ? \
  26. ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \
  27. ((syms) * 1000) << 2 /* syms * 4 us */ \
  28. )
  29. /* Transmit duration for the raw data part of an average sized packet */
  30. #define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps)))
  31. /*
  32. * Define group sort order: HT40 -> SGI -> #streams
  33. */
  34. #define GROUP_IDX(_streams, _sgi, _ht40) \
  35. MINSTREL_MAX_STREAMS * 2 * _ht40 + \
  36. MINSTREL_MAX_STREAMS * _sgi + \
  37. _streams - 1
  38. /* MCS rate information for an MCS group */
  39. #define MCS_GROUP(_streams, _sgi, _ht40) \
  40. [GROUP_IDX(_streams, _sgi, _ht40)] = { \
  41. .streams = _streams, \
  42. .flags = \
  43. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  44. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  45. .duration = { \
  46. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
  47. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
  48. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
  49. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
  50. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
  51. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
  52. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
  53. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
  54. } \
  55. }
  56. #define CCK_DURATION(_bitrate, _short, _len) \
  57. (1000 * (10 /* SIFS */ + \
  58. (_short ? 72 + 24 : 144 + 48 ) + \
  59. (8 * (_len + 4) * 10) / (_bitrate)))
  60. #define CCK_ACK_DURATION(_bitrate, _short) \
  61. (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) + \
  62. CCK_DURATION(_bitrate, _short, AVG_PKT_SIZE))
  63. #define CCK_DURATION_LIST(_short) \
  64. CCK_ACK_DURATION(10, _short), \
  65. CCK_ACK_DURATION(20, _short), \
  66. CCK_ACK_DURATION(55, _short), \
  67. CCK_ACK_DURATION(110, _short)
  68. #define CCK_GROUP \
  69. [MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS] = { \
  70. .streams = 0, \
  71. .duration = { \
  72. CCK_DURATION_LIST(false), \
  73. CCK_DURATION_LIST(true) \
  74. } \
  75. }
  76. /*
  77. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  78. * groups, based on the number of streams and flags (HT40, SGI) that they
  79. * use.
  80. *
  81. * Sortorder has to be fixed for GROUP_IDX macro to be applicable:
  82. * HT40 -> SGI -> #streams
  83. */
  84. const struct mcs_group minstrel_mcs_groups[] = {
  85. MCS_GROUP(1, 0, 0),
  86. MCS_GROUP(2, 0, 0),
  87. #if MINSTREL_MAX_STREAMS >= 3
  88. MCS_GROUP(3, 0, 0),
  89. #endif
  90. MCS_GROUP(1, 1, 0),
  91. MCS_GROUP(2, 1, 0),
  92. #if MINSTREL_MAX_STREAMS >= 3
  93. MCS_GROUP(3, 1, 0),
  94. #endif
  95. MCS_GROUP(1, 0, 1),
  96. MCS_GROUP(2, 0, 1),
  97. #if MINSTREL_MAX_STREAMS >= 3
  98. MCS_GROUP(3, 0, 1),
  99. #endif
  100. MCS_GROUP(1, 1, 1),
  101. MCS_GROUP(2, 1, 1),
  102. #if MINSTREL_MAX_STREAMS >= 3
  103. MCS_GROUP(3, 1, 1),
  104. #endif
  105. /* must be last */
  106. CCK_GROUP
  107. };
  108. #define MINSTREL_CCK_GROUP (ARRAY_SIZE(minstrel_mcs_groups) - 1)
  109. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
  110. static void
  111. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
  112. /*
  113. * Look up an MCS group index based on mac80211 rate information
  114. */
  115. static int
  116. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  117. {
  118. return GROUP_IDX((rate->idx / MCS_GROUP_RATES) + 1,
  119. !!(rate->flags & IEEE80211_TX_RC_SHORT_GI),
  120. !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH));
  121. }
  122. static struct minstrel_rate_stats *
  123. minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  124. struct ieee80211_tx_rate *rate)
  125. {
  126. int group, idx;
  127. if (rate->flags & IEEE80211_TX_RC_MCS) {
  128. group = minstrel_ht_get_group_idx(rate);
  129. idx = rate->idx % MCS_GROUP_RATES;
  130. } else {
  131. group = MINSTREL_CCK_GROUP;
  132. for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++)
  133. if (rate->idx == mp->cck_rates[idx])
  134. break;
  135. /* short preamble */
  136. if (!(mi->groups[group].supported & BIT(idx)))
  137. idx += 4;
  138. }
  139. return &mi->groups[group].rates[idx];
  140. }
  141. static inline struct minstrel_rate_stats *
  142. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  143. {
  144. return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
  145. }
  146. /*
  147. * Recalculate success probabilities and counters for a rate using EWMA
  148. */
  149. static void
  150. minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr)
  151. {
  152. if (unlikely(mr->attempts > 0)) {
  153. mr->sample_skipped = 0;
  154. mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
  155. if (!mr->att_hist)
  156. mr->probability = mr->cur_prob;
  157. else
  158. mr->probability = minstrel_ewma(mr->probability,
  159. mr->cur_prob, EWMA_LEVEL);
  160. mr->att_hist += mr->attempts;
  161. mr->succ_hist += mr->success;
  162. } else {
  163. mr->sample_skipped++;
  164. }
  165. mr->last_success = mr->success;
  166. mr->last_attempts = mr->attempts;
  167. mr->success = 0;
  168. mr->attempts = 0;
  169. }
  170. /*
  171. * Calculate throughput based on the average A-MPDU length, taking into account
  172. * the expected number of retransmissions and their expected length
  173. */
  174. static void
  175. minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
  176. {
  177. struct minstrel_rate_stats *mr;
  178. unsigned int nsecs = 0;
  179. unsigned int tp;
  180. unsigned int prob;
  181. mr = &mi->groups[group].rates[rate];
  182. prob = mr->probability;
  183. if (prob < MINSTREL_FRAC(1, 10)) {
  184. mr->cur_tp = 0;
  185. return;
  186. }
  187. /*
  188. * For the throughput calculation, limit the probability value to 90% to
  189. * account for collision related packet error rate fluctuation
  190. */
  191. if (prob > MINSTREL_FRAC(9, 10))
  192. prob = MINSTREL_FRAC(9, 10);
  193. if (group != MINSTREL_CCK_GROUP)
  194. nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
  195. nsecs += minstrel_mcs_groups[group].duration[rate];
  196. tp = 1000000 * ((mr->probability * 1000) / nsecs);
  197. mr->cur_tp = MINSTREL_TRUNC(tp);
  198. }
  199. /*
  200. * Update rate statistics and select new primary rates
  201. *
  202. * Rules for rate selection:
  203. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  204. * probability and throughput during strong fluctuations
  205. * - as long as the max prob rate has a probability of more than 3/4, pick
  206. * higher throughput rates, even if the probablity is a bit lower
  207. */
  208. static void
  209. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  210. {
  211. struct minstrel_mcs_group_data *mg;
  212. struct minstrel_rate_stats *mr;
  213. int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
  214. int group, i, index;
  215. bool mi_rates_valid = false;
  216. if (mi->ampdu_packets > 0) {
  217. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  218. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  219. mi->ampdu_len = 0;
  220. mi->ampdu_packets = 0;
  221. }
  222. mi->sample_slow = 0;
  223. mi->sample_count = 0;
  224. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  225. bool mg_rates_valid = false;
  226. cur_prob = 0;
  227. cur_prob_tp = 0;
  228. cur_tp = 0;
  229. cur_tp2 = 0;
  230. mg = &mi->groups[group];
  231. if (!mg->supported)
  232. continue;
  233. mi->sample_count++;
  234. for (i = 0; i < MCS_GROUP_RATES; i++) {
  235. if (!(mg->supported & BIT(i)))
  236. continue;
  237. /* initialize rates selections starting indexes */
  238. if (!mg_rates_valid) {
  239. mg->max_tp_rate = mg->max_tp_rate2 =
  240. mg->max_prob_rate = i;
  241. if (!mi_rates_valid) {
  242. mi->max_tp_rate = mi->max_tp_rate2 =
  243. mi->max_prob_rate = i;
  244. mi_rates_valid = true;
  245. }
  246. mg_rates_valid = true;
  247. }
  248. mr = &mg->rates[i];
  249. mr->retry_updated = false;
  250. index = MCS_GROUP_RATES * group + i;
  251. minstrel_calc_rate_ewma(mr);
  252. minstrel_ht_calc_tp(mi, group, i);
  253. if (!mr->cur_tp)
  254. continue;
  255. if ((mr->cur_tp > cur_prob_tp && mr->probability >
  256. MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
  257. mg->max_prob_rate = index;
  258. cur_prob = mr->probability;
  259. cur_prob_tp = mr->cur_tp;
  260. }
  261. if (mr->cur_tp > cur_tp) {
  262. swap(index, mg->max_tp_rate);
  263. cur_tp = mr->cur_tp;
  264. mr = minstrel_get_ratestats(mi, index);
  265. }
  266. if (index >= mg->max_tp_rate)
  267. continue;
  268. if (mr->cur_tp > cur_tp2) {
  269. mg->max_tp_rate2 = index;
  270. cur_tp2 = mr->cur_tp;
  271. }
  272. }
  273. }
  274. /* try to sample all available rates during each interval */
  275. mi->sample_count *= 8;
  276. cur_prob = 0;
  277. cur_prob_tp = 0;
  278. cur_tp = 0;
  279. cur_tp2 = 0;
  280. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  281. mg = &mi->groups[group];
  282. if (!mg->supported)
  283. continue;
  284. mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
  285. if (cur_tp < mr->cur_tp) {
  286. mi->max_tp_rate2 = mi->max_tp_rate;
  287. cur_tp2 = cur_tp;
  288. mi->max_tp_rate = mg->max_tp_rate;
  289. cur_tp = mr->cur_tp;
  290. mi->max_prob_streams = minstrel_mcs_groups[group].streams - 1;
  291. }
  292. mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
  293. if (cur_tp2 < mr->cur_tp) {
  294. mi->max_tp_rate2 = mg->max_tp_rate2;
  295. cur_tp2 = mr->cur_tp;
  296. }
  297. }
  298. if (mi->max_prob_streams < 1)
  299. mi->max_prob_streams = 1;
  300. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  301. mg = &mi->groups[group];
  302. if (!mg->supported)
  303. continue;
  304. mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
  305. if (cur_prob_tp < mr->cur_tp &&
  306. minstrel_mcs_groups[group].streams <= mi->max_prob_streams) {
  307. mi->max_prob_rate = mg->max_prob_rate;
  308. cur_prob = mr->cur_prob;
  309. cur_prob_tp = mr->cur_tp;
  310. }
  311. }
  312. #ifdef CONFIG_MAC80211_DEBUGFS
  313. /* use fixed index if set */
  314. if (mp->fixed_rate_idx != -1) {
  315. mi->max_tp_rate = mp->fixed_rate_idx;
  316. mi->max_tp_rate2 = mp->fixed_rate_idx;
  317. mi->max_prob_rate = mp->fixed_rate_idx;
  318. }
  319. #endif
  320. mi->stats_update = jiffies;
  321. }
  322. static bool
  323. minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rate)
  324. {
  325. if (rate->idx < 0)
  326. return false;
  327. if (!rate->count)
  328. return false;
  329. if (rate->flags & IEEE80211_TX_RC_MCS)
  330. return true;
  331. return rate->idx == mp->cck_rates[0] ||
  332. rate->idx == mp->cck_rates[1] ||
  333. rate->idx == mp->cck_rates[2] ||
  334. rate->idx == mp->cck_rates[3];
  335. }
  336. static void
  337. minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
  338. {
  339. struct minstrel_mcs_group_data *mg;
  340. for (;;) {
  341. mi->sample_group++;
  342. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  343. mg = &mi->groups[mi->sample_group];
  344. if (!mg->supported)
  345. continue;
  346. if (++mg->index >= MCS_GROUP_RATES) {
  347. mg->index = 0;
  348. if (++mg->column >= ARRAY_SIZE(sample_table))
  349. mg->column = 0;
  350. }
  351. break;
  352. }
  353. }
  354. static void
  355. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,
  356. bool primary)
  357. {
  358. int group, orig_group;
  359. orig_group = group = *idx / MCS_GROUP_RATES;
  360. while (group > 0) {
  361. group--;
  362. if (!mi->groups[group].supported)
  363. continue;
  364. if (minstrel_mcs_groups[group].streams >
  365. minstrel_mcs_groups[orig_group].streams)
  366. continue;
  367. if (primary)
  368. *idx = mi->groups[group].max_tp_rate;
  369. else
  370. *idx = mi->groups[group].max_tp_rate2;
  371. break;
  372. }
  373. }
  374. static void
  375. minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
  376. {
  377. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  378. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  379. u16 tid;
  380. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  381. return;
  382. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  383. return;
  384. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  385. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  386. return;
  387. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  388. return;
  389. ieee80211_start_tx_ba_session(pubsta, tid, 5000);
  390. }
  391. static void
  392. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  393. struct ieee80211_sta *sta, void *priv_sta,
  394. struct sk_buff *skb)
  395. {
  396. struct minstrel_ht_sta_priv *msp = priv_sta;
  397. struct minstrel_ht_sta *mi = &msp->ht;
  398. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  399. struct ieee80211_tx_rate *ar = info->status.rates;
  400. struct minstrel_rate_stats *rate, *rate2;
  401. struct minstrel_priv *mp = priv;
  402. bool last, update = false;
  403. int i;
  404. if (!msp->is_ht)
  405. return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
  406. /* This packet was aggregated but doesn't carry status info */
  407. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  408. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  409. return;
  410. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  411. info->status.ampdu_ack_len =
  412. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  413. info->status.ampdu_len = 1;
  414. }
  415. mi->ampdu_packets++;
  416. mi->ampdu_len += info->status.ampdu_len;
  417. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  418. mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  419. mi->sample_tries = 1;
  420. mi->sample_count--;
  421. }
  422. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  423. mi->sample_packets += info->status.ampdu_len;
  424. last = !minstrel_ht_txstat_valid(mp, &ar[0]);
  425. for (i = 0; !last; i++) {
  426. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  427. !minstrel_ht_txstat_valid(mp, &ar[i + 1]);
  428. rate = minstrel_ht_get_stats(mp, mi, &ar[i]);
  429. if (last)
  430. rate->success += info->status.ampdu_ack_len;
  431. rate->attempts += ar[i].count * info->status.ampdu_len;
  432. }
  433. /*
  434. * check for sudden death of spatial multiplexing,
  435. * downgrade to a lower number of streams if necessary.
  436. */
  437. rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
  438. if (rate->attempts > 30 &&
  439. MINSTREL_FRAC(rate->success, rate->attempts) <
  440. MINSTREL_FRAC(20, 100)) {
  441. minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
  442. update = true;
  443. }
  444. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
  445. if (rate2->attempts > 30 &&
  446. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  447. MINSTREL_FRAC(20, 100)) {
  448. minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
  449. update = true;
  450. }
  451. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  452. update = true;
  453. minstrel_ht_update_stats(mp, mi);
  454. if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
  455. mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
  456. minstrel_aggr_check(sta, skb);
  457. }
  458. if (update)
  459. minstrel_ht_update_rates(mp, mi);
  460. }
  461. static void
  462. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  463. int index)
  464. {
  465. struct minstrel_rate_stats *mr;
  466. const struct mcs_group *group;
  467. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  468. unsigned int cw = mp->cw_min;
  469. unsigned int ctime = 0;
  470. unsigned int t_slot = 9; /* FIXME */
  471. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  472. unsigned int overhead = 0, overhead_rtscts = 0;
  473. mr = minstrel_get_ratestats(mi, index);
  474. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  475. mr->retry_count = 1;
  476. mr->retry_count_rtscts = 1;
  477. return;
  478. }
  479. mr->retry_count = 2;
  480. mr->retry_count_rtscts = 2;
  481. mr->retry_updated = true;
  482. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  483. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
  484. /* Contention time for first 2 tries */
  485. ctime = (t_slot * cw) >> 1;
  486. cw = min((cw << 1) | 1, mp->cw_max);
  487. ctime += (t_slot * cw) >> 1;
  488. cw = min((cw << 1) | 1, mp->cw_max);
  489. if (index / MCS_GROUP_RATES != MINSTREL_CCK_GROUP) {
  490. overhead = mi->overhead;
  491. overhead_rtscts = mi->overhead_rtscts;
  492. }
  493. /* Total TX time for data and Contention after first 2 tries */
  494. tx_time = ctime + 2 * (overhead + tx_time_data);
  495. tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data);
  496. /* See how many more tries we can fit inside segment size */
  497. do {
  498. /* Contention time for this try */
  499. ctime = (t_slot * cw) >> 1;
  500. cw = min((cw << 1) | 1, mp->cw_max);
  501. /* Total TX time after this try */
  502. tx_time += ctime + overhead + tx_time_data;
  503. tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
  504. if (tx_time_rtscts < mp->segment_size)
  505. mr->retry_count_rtscts++;
  506. } while ((tx_time < mp->segment_size) &&
  507. (++mr->retry_count < mp->max_retry));
  508. }
  509. static void
  510. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  511. struct ieee80211_sta_rates *ratetbl, int offset, int index)
  512. {
  513. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  514. struct minstrel_rate_stats *mr;
  515. u8 idx;
  516. u16 flags;
  517. mr = minstrel_get_ratestats(mi, index);
  518. if (!mr->retry_updated)
  519. minstrel_calc_retransmit(mp, mi, index);
  520. if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
  521. ratetbl->rate[offset].count = 2;
  522. ratetbl->rate[offset].count_rts = 2;
  523. ratetbl->rate[offset].count_cts = 2;
  524. } else {
  525. ratetbl->rate[offset].count = mr->retry_count;
  526. ratetbl->rate[offset].count_cts = mr->retry_count;
  527. ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
  528. }
  529. if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  530. idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
  531. flags = 0;
  532. } else {
  533. idx = index % MCS_GROUP_RATES +
  534. (group->streams - 1) * MCS_GROUP_RATES;
  535. flags = IEEE80211_TX_RC_MCS | group->flags;
  536. }
  537. if (offset > 0) {
  538. ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
  539. flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  540. }
  541. ratetbl->rate[offset].idx = idx;
  542. ratetbl->rate[offset].flags = flags;
  543. }
  544. static void
  545. minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  546. {
  547. struct ieee80211_sta_rates *rates;
  548. int i = 0;
  549. rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
  550. if (!rates)
  551. return;
  552. /* Start with max_tp_rate */
  553. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate);
  554. if (mp->hw->max_rates >= 3) {
  555. /* At least 3 tx rates supported, use max_tp_rate2 next */
  556. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate2);
  557. }
  558. if (mp->hw->max_rates >= 2) {
  559. /*
  560. * At least 2 tx rates supported, use max_prob_rate next */
  561. minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
  562. }
  563. rates->rate[i].idx = -1;
  564. rate_control_set_rates(mp->hw, mi->sta, rates);
  565. }
  566. static inline int
  567. minstrel_get_duration(int index)
  568. {
  569. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  570. return group->duration[index % MCS_GROUP_RATES];
  571. }
  572. static int
  573. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  574. {
  575. struct minstrel_rate_stats *mr;
  576. struct minstrel_mcs_group_data *mg;
  577. unsigned int sample_dur, sample_group;
  578. int sample_idx = 0;
  579. if (mi->sample_wait > 0) {
  580. mi->sample_wait--;
  581. return -1;
  582. }
  583. if (!mi->sample_tries)
  584. return -1;
  585. mg = &mi->groups[mi->sample_group];
  586. sample_idx = sample_table[mg->column][mg->index];
  587. mr = &mg->rates[sample_idx];
  588. sample_group = mi->sample_group;
  589. sample_idx += sample_group * MCS_GROUP_RATES;
  590. minstrel_next_sample_idx(mi);
  591. /*
  592. * Sampling might add some overhead (RTS, no aggregation)
  593. * to the frame. Hence, don't use sampling for the currently
  594. * used rates.
  595. */
  596. if (sample_idx == mi->max_tp_rate ||
  597. sample_idx == mi->max_tp_rate2 ||
  598. sample_idx == mi->max_prob_rate)
  599. return -1;
  600. /*
  601. * Do not sample if the probability is already higher than 95%
  602. * to avoid wasting airtime.
  603. */
  604. if (mr->probability > MINSTREL_FRAC(95, 100))
  605. return -1;
  606. /*
  607. * Make sure that lower rates get sampled only occasionally,
  608. * if the link is working perfectly.
  609. */
  610. sample_dur = minstrel_get_duration(sample_idx);
  611. if (sample_dur >= minstrel_get_duration(mi->max_tp_rate2) &&
  612. (mi->max_prob_streams <
  613. minstrel_mcs_groups[sample_group].streams ||
  614. sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
  615. if (mr->sample_skipped < 20)
  616. return -1;
  617. if (mi->sample_slow++ > 2)
  618. return -1;
  619. }
  620. mi->sample_tries--;
  621. return sample_idx;
  622. }
  623. static void
  624. minstrel_ht_check_cck_shortpreamble(struct minstrel_priv *mp,
  625. struct minstrel_ht_sta *mi, bool val)
  626. {
  627. u8 supported = mi->groups[MINSTREL_CCK_GROUP].supported;
  628. if (!supported || !mi->cck_supported_short)
  629. return;
  630. if (supported & (mi->cck_supported_short << (val * 4)))
  631. return;
  632. supported ^= mi->cck_supported_short | (mi->cck_supported_short << 4);
  633. mi->groups[MINSTREL_CCK_GROUP].supported = supported;
  634. }
  635. static void
  636. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  637. struct ieee80211_tx_rate_control *txrc)
  638. {
  639. const struct mcs_group *sample_group;
  640. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  641. struct ieee80211_tx_rate *rate = &info->status.rates[0];
  642. struct minstrel_ht_sta_priv *msp = priv_sta;
  643. struct minstrel_ht_sta *mi = &msp->ht;
  644. struct minstrel_priv *mp = priv;
  645. int sample_idx;
  646. if (rate_control_send_low(sta, priv_sta, txrc))
  647. return;
  648. if (!msp->is_ht)
  649. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  650. info->flags |= mi->tx_flags;
  651. minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
  652. #ifdef CONFIG_MAC80211_DEBUGFS
  653. if (mp->fixed_rate_idx != -1)
  654. return;
  655. #endif
  656. /* Don't use EAPOL frames for sampling on non-mrr hw */
  657. if (mp->hw->max_rates == 1 &&
  658. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  659. sample_idx = -1;
  660. else
  661. sample_idx = minstrel_get_sample_rate(mp, mi);
  662. mi->total_packets++;
  663. /* wraparound */
  664. if (mi->total_packets == ~0) {
  665. mi->total_packets = 0;
  666. mi->sample_packets = 0;
  667. }
  668. if (sample_idx < 0)
  669. return;
  670. sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
  671. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  672. rate->count = 1;
  673. if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
  674. int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
  675. rate->idx = mp->cck_rates[idx];
  676. rate->flags = 0;
  677. return;
  678. }
  679. rate->idx = sample_idx % MCS_GROUP_RATES +
  680. (sample_group->streams - 1) * MCS_GROUP_RATES;
  681. rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
  682. }
  683. static void
  684. minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  685. struct ieee80211_supported_band *sband,
  686. struct ieee80211_sta *sta)
  687. {
  688. int i;
  689. if (sband->band != IEEE80211_BAND_2GHZ)
  690. return;
  691. if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
  692. return;
  693. mi->cck_supported = 0;
  694. mi->cck_supported_short = 0;
  695. for (i = 0; i < 4; i++) {
  696. if (!rate_supported(sta, sband->band, mp->cck_rates[i]))
  697. continue;
  698. mi->cck_supported |= BIT(i);
  699. if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  700. mi->cck_supported_short |= BIT(i);
  701. }
  702. mi->groups[MINSTREL_CCK_GROUP].supported = mi->cck_supported;
  703. }
  704. static void
  705. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  706. struct cfg80211_chan_def *chandef,
  707. struct ieee80211_sta *sta, void *priv_sta)
  708. {
  709. struct minstrel_priv *mp = priv;
  710. struct minstrel_ht_sta_priv *msp = priv_sta;
  711. struct minstrel_ht_sta *mi = &msp->ht;
  712. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  713. u16 sta_cap = sta->ht_cap.cap;
  714. int n_supported = 0;
  715. int ack_dur;
  716. int stbc;
  717. int i;
  718. /* fall back to the old minstrel for legacy stations */
  719. if (!sta->ht_cap.ht_supported)
  720. goto use_legacy;
  721. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  722. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS + 1);
  723. msp->is_ht = true;
  724. memset(mi, 0, sizeof(*mi));
  725. mi->sta = sta;
  726. mi->stats_update = jiffies;
  727. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
  728. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
  729. mi->overhead += ack_dur;
  730. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  731. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  732. /* When using MRR, sample more on the first attempt, without delay */
  733. if (mp->has_mrr) {
  734. mi->sample_count = 16;
  735. mi->sample_wait = 0;
  736. } else {
  737. mi->sample_count = 8;
  738. mi->sample_wait = 8;
  739. }
  740. mi->sample_tries = 4;
  741. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  742. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  743. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  744. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  745. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  746. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  747. mi->groups[i].supported = 0;
  748. if (i == MINSTREL_CCK_GROUP) {
  749. minstrel_ht_update_cck(mp, mi, sband, sta);
  750. continue;
  751. }
  752. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  753. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  754. if (!(sta_cap & IEEE80211_HT_CAP_SGI_40))
  755. continue;
  756. } else {
  757. if (!(sta_cap & IEEE80211_HT_CAP_SGI_20))
  758. continue;
  759. }
  760. }
  761. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH &&
  762. sta->bandwidth < IEEE80211_STA_RX_BW_40)
  763. continue;
  764. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  765. if (sta->smps_mode == IEEE80211_SMPS_STATIC &&
  766. minstrel_mcs_groups[i].streams > 1)
  767. continue;
  768. mi->groups[i].supported =
  769. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  770. if (mi->groups[i].supported)
  771. n_supported++;
  772. }
  773. if (!n_supported)
  774. goto use_legacy;
  775. /* create an initial rate table with the lowest supported rates */
  776. minstrel_ht_update_stats(mp, mi);
  777. minstrel_ht_update_rates(mp, mi);
  778. return;
  779. use_legacy:
  780. msp->is_ht = false;
  781. memset(&msp->legacy, 0, sizeof(msp->legacy));
  782. msp->legacy.r = msp->ratelist;
  783. msp->legacy.sample_table = msp->sample_table;
  784. return mac80211_minstrel.rate_init(priv, sband, chandef, sta,
  785. &msp->legacy);
  786. }
  787. static void
  788. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  789. struct cfg80211_chan_def *chandef,
  790. struct ieee80211_sta *sta, void *priv_sta)
  791. {
  792. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  793. }
  794. static void
  795. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  796. struct cfg80211_chan_def *chandef,
  797. struct ieee80211_sta *sta, void *priv_sta,
  798. u32 changed)
  799. {
  800. minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta);
  801. }
  802. static void *
  803. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  804. {
  805. struct ieee80211_supported_band *sband;
  806. struct minstrel_ht_sta_priv *msp;
  807. struct minstrel_priv *mp = priv;
  808. struct ieee80211_hw *hw = mp->hw;
  809. int max_rates = 0;
  810. int i;
  811. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  812. sband = hw->wiphy->bands[i];
  813. if (sband && sband->n_bitrates > max_rates)
  814. max_rates = sband->n_bitrates;
  815. }
  816. msp = kzalloc(sizeof(*msp), gfp);
  817. if (!msp)
  818. return NULL;
  819. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  820. if (!msp->ratelist)
  821. goto error;
  822. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  823. if (!msp->sample_table)
  824. goto error1;
  825. return msp;
  826. error1:
  827. kfree(msp->ratelist);
  828. error:
  829. kfree(msp);
  830. return NULL;
  831. }
  832. static void
  833. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  834. {
  835. struct minstrel_ht_sta_priv *msp = priv_sta;
  836. kfree(msp->sample_table);
  837. kfree(msp->ratelist);
  838. kfree(msp);
  839. }
  840. static void *
  841. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  842. {
  843. return mac80211_minstrel.alloc(hw, debugfsdir);
  844. }
  845. static void
  846. minstrel_ht_free(void *priv)
  847. {
  848. mac80211_minstrel.free(priv);
  849. }
  850. static struct rate_control_ops mac80211_minstrel_ht = {
  851. .name = "minstrel_ht",
  852. .tx_status = minstrel_ht_tx_status,
  853. .get_rate = minstrel_ht_get_rate,
  854. .rate_init = minstrel_ht_rate_init,
  855. .rate_update = minstrel_ht_rate_update,
  856. .alloc_sta = minstrel_ht_alloc_sta,
  857. .free_sta = minstrel_ht_free_sta,
  858. .alloc = minstrel_ht_alloc,
  859. .free = minstrel_ht_free,
  860. #ifdef CONFIG_MAC80211_DEBUGFS
  861. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  862. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  863. #endif
  864. };
  865. static void
  866. init_sample_table(void)
  867. {
  868. int col, i, new_idx;
  869. u8 rnd[MCS_GROUP_RATES];
  870. memset(sample_table, 0xff, sizeof(sample_table));
  871. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  872. for (i = 0; i < MCS_GROUP_RATES; i++) {
  873. get_random_bytes(rnd, sizeof(rnd));
  874. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  875. while (sample_table[col][new_idx] != 0xff)
  876. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  877. sample_table[col][new_idx] = i;
  878. }
  879. }
  880. }
  881. int __init
  882. rc80211_minstrel_ht_init(void)
  883. {
  884. init_sample_table();
  885. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  886. }
  887. void
  888. rc80211_minstrel_ht_exit(void)
  889. {
  890. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  891. }