rc80211_minstrel_ht.c 26 KB

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