rc80211_minstrel_ht.c 23 KB

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