rc80211_minstrel_ht.c 26 KB

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