rc80211_minstrel_ht.c 23 KB

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