rc80211_minstrel_ht.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. if ((mr->cur_tp > cur_prob_tp && mr->probability >
  204. MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
  205. mg->max_prob_rate = index;
  206. cur_prob = mr->probability;
  207. cur_prob_tp = mr->cur_tp;
  208. }
  209. if (mr->cur_tp > cur_tp) {
  210. swap(index, mg->max_tp_rate);
  211. cur_tp = mr->cur_tp;
  212. mr = minstrel_get_ratestats(mi, index);
  213. }
  214. if (index >= mg->max_tp_rate)
  215. continue;
  216. if (mr->cur_tp > cur_tp2) {
  217. mg->max_tp_rate2 = index;
  218. cur_tp2 = mr->cur_tp;
  219. }
  220. }
  221. }
  222. /* try to sample up to half of the available rates during each interval */
  223. mi->sample_count *= 4;
  224. cur_prob = 0;
  225. cur_prob_tp = 0;
  226. cur_tp = 0;
  227. cur_tp2 = 0;
  228. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  229. mg = &mi->groups[group];
  230. if (!mg->supported)
  231. continue;
  232. mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
  233. if (cur_prob_tp < mr->cur_tp &&
  234. minstrel_mcs_groups[group].streams == 1) {
  235. mi->max_prob_rate = mg->max_prob_rate;
  236. cur_prob = mr->cur_prob;
  237. cur_prob_tp = mr->cur_tp;
  238. }
  239. mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
  240. if (cur_tp < mr->cur_tp) {
  241. mi->max_tp_rate2 = mi->max_tp_rate;
  242. cur_tp2 = cur_tp;
  243. mi->max_tp_rate = mg->max_tp_rate;
  244. cur_tp = mr->cur_tp;
  245. }
  246. mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
  247. if (cur_tp2 < mr->cur_tp) {
  248. mi->max_tp_rate2 = mg->max_tp_rate2;
  249. cur_tp2 = mr->cur_tp;
  250. }
  251. }
  252. mi->stats_update = jiffies;
  253. }
  254. static bool
  255. minstrel_ht_txstat_valid(struct ieee80211_tx_rate *rate)
  256. {
  257. if (rate->idx < 0)
  258. return false;
  259. if (!rate->count)
  260. return false;
  261. return !!(rate->flags & IEEE80211_TX_RC_MCS);
  262. }
  263. static void
  264. minstrel_next_sample_idx(struct minstrel_ht_sta *mi)
  265. {
  266. struct minstrel_mcs_group_data *mg;
  267. for (;;) {
  268. mi->sample_group++;
  269. mi->sample_group %= ARRAY_SIZE(minstrel_mcs_groups);
  270. mg = &mi->groups[mi->sample_group];
  271. if (!mg->supported)
  272. continue;
  273. if (++mg->index >= MCS_GROUP_RATES) {
  274. mg->index = 0;
  275. if (++mg->column >= ARRAY_SIZE(sample_table))
  276. mg->column = 0;
  277. }
  278. break;
  279. }
  280. }
  281. static void
  282. minstrel_downgrade_rate(struct minstrel_ht_sta *mi, unsigned int *idx,
  283. bool primary)
  284. {
  285. int group, orig_group;
  286. orig_group = group = *idx / MCS_GROUP_RATES;
  287. while (group > 0) {
  288. group--;
  289. if (!mi->groups[group].supported)
  290. continue;
  291. if (minstrel_mcs_groups[group].streams >
  292. minstrel_mcs_groups[orig_group].streams)
  293. continue;
  294. if (primary)
  295. *idx = mi->groups[group].max_tp_rate;
  296. else
  297. *idx = mi->groups[group].max_tp_rate2;
  298. break;
  299. }
  300. }
  301. static void
  302. minstrel_aggr_check(struct ieee80211_sta *pubsta, struct sk_buff *skb)
  303. {
  304. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  305. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  306. u16 tid;
  307. if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
  308. return;
  309. if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
  310. return;
  311. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  312. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  313. return;
  314. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  315. return;
  316. ieee80211_start_tx_ba_session(pubsta, tid, 5000);
  317. }
  318. static void
  319. minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
  320. struct ieee80211_sta *sta, void *priv_sta,
  321. struct sk_buff *skb)
  322. {
  323. struct minstrel_ht_sta_priv *msp = priv_sta;
  324. struct minstrel_ht_sta *mi = &msp->ht;
  325. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  326. struct ieee80211_tx_rate *ar = info->status.rates;
  327. struct minstrel_rate_stats *rate, *rate2;
  328. struct minstrel_priv *mp = priv;
  329. bool last;
  330. int group;
  331. int i;
  332. if (!msp->is_ht)
  333. return mac80211_minstrel.tx_status(priv, sband, sta, &msp->legacy, skb);
  334. /* This packet was aggregated but doesn't carry status info */
  335. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  336. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  337. return;
  338. if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  339. info->status.ampdu_ack_len =
  340. (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  341. info->status.ampdu_len = 1;
  342. }
  343. mi->ampdu_packets++;
  344. mi->ampdu_len += info->status.ampdu_len;
  345. if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
  346. mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
  347. mi->sample_tries = 2;
  348. mi->sample_count--;
  349. }
  350. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  351. mi->sample_packets += info->status.ampdu_len;
  352. last = !minstrel_ht_txstat_valid(&ar[0]);
  353. for (i = 0; !last; i++) {
  354. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  355. !minstrel_ht_txstat_valid(&ar[i + 1]);
  356. group = minstrel_ht_get_group_idx(&ar[i]);
  357. rate = &mi->groups[group].rates[ar[i].idx % 8];
  358. if (last)
  359. rate->success += info->status.ampdu_ack_len;
  360. rate->attempts += ar[i].count * info->status.ampdu_len;
  361. }
  362. /*
  363. * check for sudden death of spatial multiplexing,
  364. * downgrade to a lower number of streams if necessary.
  365. */
  366. rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
  367. if (rate->attempts > 30 &&
  368. MINSTREL_FRAC(rate->success, rate->attempts) <
  369. MINSTREL_FRAC(20, 100))
  370. minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
  371. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
  372. if (rate2->attempts > 30 &&
  373. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  374. MINSTREL_FRAC(20, 100))
  375. minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
  376. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  377. minstrel_ht_update_stats(mp, mi);
  378. if (!(info->flags & IEEE80211_TX_CTL_AMPDU))
  379. minstrel_aggr_check(sta, skb);
  380. }
  381. }
  382. static void
  383. minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  384. int index)
  385. {
  386. struct minstrel_rate_stats *mr;
  387. const struct mcs_group *group;
  388. unsigned int tx_time, tx_time_rtscts, tx_time_data;
  389. unsigned int cw = mp->cw_min;
  390. unsigned int ctime = 0;
  391. unsigned int t_slot = 9; /* FIXME */
  392. unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
  393. mr = minstrel_get_ratestats(mi, index);
  394. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  395. mr->retry_count = 1;
  396. mr->retry_count_rtscts = 1;
  397. return;
  398. }
  399. mr->retry_count = 2;
  400. mr->retry_count_rtscts = 2;
  401. mr->retry_updated = true;
  402. group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  403. tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
  404. /* Contention time for first 2 tries */
  405. ctime = (t_slot * cw) >> 1;
  406. cw = min((cw << 1) | 1, mp->cw_max);
  407. ctime += (t_slot * cw) >> 1;
  408. cw = min((cw << 1) | 1, mp->cw_max);
  409. /* Total TX time for data and Contention after first 2 tries */
  410. tx_time = ctime + 2 * (mi->overhead + tx_time_data);
  411. tx_time_rtscts = ctime + 2 * (mi->overhead_rtscts + tx_time_data);
  412. /* See how many more tries we can fit inside segment size */
  413. do {
  414. /* Contention time for this try */
  415. ctime = (t_slot * cw) >> 1;
  416. cw = min((cw << 1) | 1, mp->cw_max);
  417. /* Total TX time after this try */
  418. tx_time += ctime + mi->overhead + tx_time_data;
  419. tx_time_rtscts += ctime + mi->overhead_rtscts + tx_time_data;
  420. if (tx_time_rtscts < mp->segment_size)
  421. mr->retry_count_rtscts++;
  422. } while ((tx_time < mp->segment_size) &&
  423. (++mr->retry_count < mp->max_retry));
  424. }
  425. static void
  426. minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  427. struct ieee80211_tx_rate *rate, int index,
  428. bool sample, bool rtscts)
  429. {
  430. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  431. struct minstrel_rate_stats *mr;
  432. mr = minstrel_get_ratestats(mi, index);
  433. if (!mr->retry_updated)
  434. minstrel_calc_retransmit(mp, mi, index);
  435. if (sample)
  436. rate->count = 1;
  437. else if (mr->probability < MINSTREL_FRAC(20, 100))
  438. rate->count = 2;
  439. else if (rtscts)
  440. rate->count = mr->retry_count_rtscts;
  441. else
  442. rate->count = mr->retry_count;
  443. rate->flags = IEEE80211_TX_RC_MCS | group->flags;
  444. if (rtscts)
  445. rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  446. rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
  447. }
  448. static inline int
  449. minstrel_get_duration(int index)
  450. {
  451. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  452. return group->duration[index % MCS_GROUP_RATES];
  453. }
  454. static int
  455. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  456. {
  457. struct minstrel_rate_stats *mr;
  458. struct minstrel_mcs_group_data *mg;
  459. int sample_idx = 0;
  460. if (mi->sample_wait > 0) {
  461. mi->sample_wait--;
  462. return -1;
  463. }
  464. if (!mi->sample_tries)
  465. return -1;
  466. mi->sample_tries--;
  467. mg = &mi->groups[mi->sample_group];
  468. sample_idx = sample_table[mg->column][mg->index];
  469. mr = &mg->rates[sample_idx];
  470. sample_idx += mi->sample_group * MCS_GROUP_RATES;
  471. minstrel_next_sample_idx(mi);
  472. /*
  473. * Sampling might add some overhead (RTS, no aggregation)
  474. * to the frame. Hence, don't use sampling for the currently
  475. * used max TP rate.
  476. */
  477. if (sample_idx == mi->max_tp_rate)
  478. return -1;
  479. /*
  480. * When not using MRR, do not sample if the probability is already
  481. * higher than 95% to avoid wasting airtime
  482. */
  483. if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
  484. return -1;
  485. /*
  486. * Make sure that lower rates get sampled only occasionally,
  487. * if the link is working perfectly.
  488. */
  489. if (minstrel_get_duration(sample_idx) >
  490. minstrel_get_duration(mi->max_tp_rate)) {
  491. if (mr->sample_skipped < 20)
  492. return -1;
  493. if (mi->sample_slow++ > 2)
  494. return -1;
  495. }
  496. return sample_idx;
  497. }
  498. static void
  499. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  500. struct ieee80211_tx_rate_control *txrc)
  501. {
  502. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  503. struct ieee80211_tx_rate *ar = info->status.rates;
  504. struct minstrel_ht_sta_priv *msp = priv_sta;
  505. struct minstrel_ht_sta *mi = &msp->ht;
  506. struct minstrel_priv *mp = priv;
  507. int sample_idx;
  508. bool sample = false;
  509. if (rate_control_send_low(sta, priv_sta, txrc))
  510. return;
  511. if (!msp->is_ht)
  512. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  513. info->flags |= mi->tx_flags;
  514. /* Don't use EAPOL frames for sampling on non-mrr hw */
  515. if (mp->hw->max_rates == 1 &&
  516. txrc->skb->protocol == cpu_to_be16(ETH_P_PAE))
  517. sample_idx = -1;
  518. else
  519. sample_idx = minstrel_get_sample_rate(mp, mi);
  520. #ifdef CONFIG_MAC80211_DEBUGFS
  521. /* use fixed index if set */
  522. if (mp->fixed_rate_idx != -1) {
  523. mi->max_tp_rate = mp->fixed_rate_idx;
  524. mi->max_tp_rate2 = mp->fixed_rate_idx;
  525. mi->max_prob_rate = mp->fixed_rate_idx;
  526. sample_idx = -1;
  527. }
  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. {
  580. struct minstrel_priv *mp = priv;
  581. struct minstrel_ht_sta_priv *msp = priv_sta;
  582. struct minstrel_ht_sta *mi = &msp->ht;
  583. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  584. u16 sta_cap = sta->ht_cap.cap;
  585. int n_supported = 0;
  586. int ack_dur;
  587. int stbc;
  588. int i;
  589. unsigned int smps;
  590. /* fall back to the old minstrel for legacy stations */
  591. if (!sta->ht_cap.ht_supported)
  592. goto use_legacy;
  593. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  594. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
  595. msp->is_ht = true;
  596. memset(mi, 0, sizeof(*mi));
  597. mi->stats_update = jiffies;
  598. ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1);
  599. mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1) + ack_dur;
  600. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  601. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  602. /* When using MRR, sample more on the first attempt, without delay */
  603. if (mp->has_mrr) {
  604. mi->sample_count = 16;
  605. mi->sample_wait = 0;
  606. } else {
  607. mi->sample_count = 8;
  608. mi->sample_wait = 8;
  609. }
  610. mi->sample_tries = 4;
  611. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  612. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  613. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  614. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  615. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  616. smps = (sta_cap & IEEE80211_HT_CAP_SM_PS) >>
  617. IEEE80211_HT_CAP_SM_PS_SHIFT;
  618. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  619. u16 req = 0;
  620. mi->groups[i].supported = 0;
  621. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  622. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  623. req |= IEEE80211_HT_CAP_SGI_40;
  624. else
  625. req |= IEEE80211_HT_CAP_SGI_20;
  626. }
  627. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  628. req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  629. if ((sta_cap & req) != req)
  630. continue;
  631. /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */
  632. if (smps == WLAN_HT_CAP_SM_PS_STATIC &&
  633. minstrel_mcs_groups[i].streams > 1)
  634. continue;
  635. mi->groups[i].supported =
  636. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  637. if (mi->groups[i].supported)
  638. n_supported++;
  639. }
  640. if (!n_supported)
  641. goto use_legacy;
  642. return;
  643. use_legacy:
  644. msp->is_ht = false;
  645. memset(&msp->legacy, 0, sizeof(msp->legacy));
  646. msp->legacy.r = msp->ratelist;
  647. msp->legacy.sample_table = msp->sample_table;
  648. return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
  649. }
  650. static void
  651. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  652. struct ieee80211_sta *sta, void *priv_sta)
  653. {
  654. minstrel_ht_update_caps(priv, sband, sta, priv_sta);
  655. }
  656. static void
  657. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  658. struct ieee80211_sta *sta, void *priv_sta,
  659. u32 changed)
  660. {
  661. minstrel_ht_update_caps(priv, sband, sta, priv_sta);
  662. }
  663. static void *
  664. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  665. {
  666. struct ieee80211_supported_band *sband;
  667. struct minstrel_ht_sta_priv *msp;
  668. struct minstrel_priv *mp = priv;
  669. struct ieee80211_hw *hw = mp->hw;
  670. int max_rates = 0;
  671. int i;
  672. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  673. sband = hw->wiphy->bands[i];
  674. if (sband && sband->n_bitrates > max_rates)
  675. max_rates = sband->n_bitrates;
  676. }
  677. msp = kzalloc(sizeof(*msp), gfp);
  678. if (!msp)
  679. return NULL;
  680. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  681. if (!msp->ratelist)
  682. goto error;
  683. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  684. if (!msp->sample_table)
  685. goto error1;
  686. return msp;
  687. error1:
  688. kfree(msp->ratelist);
  689. error:
  690. kfree(msp);
  691. return NULL;
  692. }
  693. static void
  694. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  695. {
  696. struct minstrel_ht_sta_priv *msp = priv_sta;
  697. kfree(msp->sample_table);
  698. kfree(msp->ratelist);
  699. kfree(msp);
  700. }
  701. static void *
  702. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  703. {
  704. return mac80211_minstrel.alloc(hw, debugfsdir);
  705. }
  706. static void
  707. minstrel_ht_free(void *priv)
  708. {
  709. mac80211_minstrel.free(priv);
  710. }
  711. static struct rate_control_ops mac80211_minstrel_ht = {
  712. .name = "minstrel_ht",
  713. .tx_status = minstrel_ht_tx_status,
  714. .get_rate = minstrel_ht_get_rate,
  715. .rate_init = minstrel_ht_rate_init,
  716. .rate_update = minstrel_ht_rate_update,
  717. .alloc_sta = minstrel_ht_alloc_sta,
  718. .free_sta = minstrel_ht_free_sta,
  719. .alloc = minstrel_ht_alloc,
  720. .free = minstrel_ht_free,
  721. #ifdef CONFIG_MAC80211_DEBUGFS
  722. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  723. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  724. #endif
  725. };
  726. static void
  727. init_sample_table(void)
  728. {
  729. int col, i, new_idx;
  730. u8 rnd[MCS_GROUP_RATES];
  731. memset(sample_table, 0xff, sizeof(sample_table));
  732. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  733. for (i = 0; i < MCS_GROUP_RATES; i++) {
  734. get_random_bytes(rnd, sizeof(rnd));
  735. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  736. while (sample_table[col][new_idx] != 0xff)
  737. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  738. sample_table[col][new_idx] = i;
  739. }
  740. }
  741. }
  742. int __init
  743. rc80211_minstrel_ht_init(void)
  744. {
  745. init_sample_table();
  746. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  747. }
  748. void
  749. rc80211_minstrel_ht_exit(void)
  750. {
  751. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  752. }