rc80211_minstrel_ht.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. /* MCS rate information for an MCS group */
  34. #define MCS_GROUP(_streams, _sgi, _ht40) { \
  35. .streams = _streams, \
  36. .flags = \
  37. (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \
  38. (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \
  39. .duration = { \
  40. MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26), \
  41. MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52), \
  42. MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78), \
  43. MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104), \
  44. MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156), \
  45. MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208), \
  46. MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234), \
  47. MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) \
  48. } \
  49. }
  50. /*
  51. * To enable sufficiently targeted rate sampling, MCS rates are divided into
  52. * groups, based on the number of streams and flags (HT40, SGI) that they
  53. * use.
  54. */
  55. const struct mcs_group minstrel_mcs_groups[] = {
  56. MCS_GROUP(1, 0, 0),
  57. MCS_GROUP(2, 0, 0),
  58. #if MINSTREL_MAX_STREAMS >= 3
  59. MCS_GROUP(3, 0, 0),
  60. #endif
  61. MCS_GROUP(1, 1, 0),
  62. MCS_GROUP(2, 1, 0),
  63. #if MINSTREL_MAX_STREAMS >= 3
  64. MCS_GROUP(3, 1, 0),
  65. #endif
  66. MCS_GROUP(1, 0, 1),
  67. MCS_GROUP(2, 0, 1),
  68. #if MINSTREL_MAX_STREAMS >= 3
  69. MCS_GROUP(3, 0, 1),
  70. #endif
  71. MCS_GROUP(1, 1, 1),
  72. MCS_GROUP(2, 1, 1),
  73. #if MINSTREL_MAX_STREAMS >= 3
  74. MCS_GROUP(3, 1, 1),
  75. #endif
  76. };
  77. static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
  78. /*
  79. * Perform EWMA (Exponentially Weighted Moving Average) calculation
  80. */
  81. static int
  82. minstrel_ewma(int old, int new, int weight)
  83. {
  84. return (new * (100 - weight) + old * weight) / 100;
  85. }
  86. /*
  87. * Look up an MCS group index based on mac80211 rate information
  88. */
  89. static int
  90. minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate)
  91. {
  92. int streams = (rate->idx / MCS_GROUP_RATES) + 1;
  93. u32 flags = IEEE80211_TX_RC_SHORT_GI | IEEE80211_TX_RC_40_MHZ_WIDTH;
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) {
  96. if (minstrel_mcs_groups[i].streams != streams)
  97. continue;
  98. if (minstrel_mcs_groups[i].flags != (rate->flags & flags))
  99. continue;
  100. return i;
  101. }
  102. WARN_ON(1);
  103. return 0;
  104. }
  105. static inline struct minstrel_rate_stats *
  106. minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
  107. {
  108. return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
  109. }
  110. /*
  111. * Recalculate success probabilities and counters for a rate using EWMA
  112. */
  113. static void
  114. minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
  115. {
  116. if (unlikely(mr->attempts > 0)) {
  117. mr->sample_skipped = 0;
  118. mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
  119. if (!mr->att_hist)
  120. mr->probability = mr->cur_prob;
  121. else
  122. mr->probability = minstrel_ewma(mr->probability,
  123. mr->cur_prob, EWMA_LEVEL);
  124. mr->att_hist += mr->attempts;
  125. mr->succ_hist += mr->success;
  126. } else {
  127. mr->sample_skipped++;
  128. }
  129. mr->last_success = mr->success;
  130. mr->last_attempts = mr->attempts;
  131. mr->success = 0;
  132. mr->attempts = 0;
  133. }
  134. /*
  135. * Calculate throughput based on the average A-MPDU length, taking into account
  136. * the expected number of retransmissions and their expected length
  137. */
  138. static void
  139. minstrel_ht_calc_tp(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
  140. int group, int rate)
  141. {
  142. struct minstrel_rate_stats *mr;
  143. unsigned int usecs;
  144. mr = &mi->groups[group].rates[rate];
  145. if (mr->probability < MINSTREL_FRAC(1, 10)) {
  146. mr->cur_tp = 0;
  147. return;
  148. }
  149. usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
  150. usecs += minstrel_mcs_groups[group].duration[rate];
  151. mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
  152. }
  153. /*
  154. * Update rate statistics and select new primary rates
  155. *
  156. * Rules for rate selection:
  157. * - max_prob_rate must use only one stream, as a tradeoff between delivery
  158. * probability and throughput during strong fluctuations
  159. * - as long as the max prob rate has a probability of more than 3/4, pick
  160. * higher throughput rates, even if the probablity is a bit lower
  161. */
  162. static void
  163. minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  164. {
  165. struct minstrel_mcs_group_data *mg;
  166. struct minstrel_rate_stats *mr;
  167. int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
  168. int group, i, index;
  169. if (mi->ampdu_packets > 0) {
  170. mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
  171. MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), EWMA_LEVEL);
  172. mi->ampdu_len = 0;
  173. mi->ampdu_packets = 0;
  174. }
  175. mi->sample_slow = 0;
  176. mi->sample_count = 0;
  177. mi->max_tp_rate = 0;
  178. mi->max_tp_rate2 = 0;
  179. mi->max_prob_rate = 0;
  180. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  181. cur_prob = 0;
  182. cur_prob_tp = 0;
  183. cur_tp = 0;
  184. cur_tp2 = 0;
  185. mg = &mi->groups[group];
  186. if (!mg->supported)
  187. continue;
  188. mg->max_tp_rate = 0;
  189. mg->max_tp_rate2 = 0;
  190. mg->max_prob_rate = 0;
  191. mi->sample_count++;
  192. for (i = 0; i < MCS_GROUP_RATES; i++) {
  193. if (!(mg->supported & BIT(i)))
  194. continue;
  195. mr = &mg->rates[i];
  196. mr->retry_updated = false;
  197. index = MCS_GROUP_RATES * group + i;
  198. minstrel_calc_rate_ewma(mp, mr);
  199. minstrel_ht_calc_tp(mp, mi, group, i);
  200. if (!mr->cur_tp)
  201. continue;
  202. /* ignore the lowest rate of each single-stream group */
  203. if (!i && minstrel_mcs_groups[group].streams == 1)
  204. continue;
  205. if ((mr->cur_tp > cur_prob_tp && mr->probability >
  206. MINSTREL_FRAC(3, 4)) || mr->probability > cur_prob) {
  207. mg->max_prob_rate = index;
  208. cur_prob = mr->probability;
  209. cur_prob_tp = mr->cur_tp;
  210. }
  211. if (mr->cur_tp > cur_tp) {
  212. swap(index, mg->max_tp_rate);
  213. cur_tp = mr->cur_tp;
  214. mr = minstrel_get_ratestats(mi, index);
  215. }
  216. if (index >= mg->max_tp_rate)
  217. continue;
  218. if (mr->cur_tp > cur_tp2) {
  219. mg->max_tp_rate2 = index;
  220. cur_tp2 = mr->cur_tp;
  221. }
  222. }
  223. }
  224. /* try to sample up to half of the available rates during each interval */
  225. mi->sample_count *= 4;
  226. cur_prob = 0;
  227. cur_prob_tp = 0;
  228. cur_tp = 0;
  229. cur_tp2 = 0;
  230. for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
  231. mg = &mi->groups[group];
  232. if (!mg->supported)
  233. continue;
  234. mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
  235. if (cur_prob_tp < mr->cur_tp &&
  236. minstrel_mcs_groups[group].streams == 1) {
  237. mi->max_prob_rate = mg->max_prob_rate;
  238. cur_prob = mr->cur_prob;
  239. cur_prob_tp = mr->cur_tp;
  240. }
  241. mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
  242. if (cur_tp < mr->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->count)
  258. return false;
  259. if (rate->idx < 0)
  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 minstrel_priv *mp, 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 = false;
  330. int group;
  331. int i = 0;
  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. for (i = 0; !last; i++) {
  353. last = (i == IEEE80211_TX_MAX_RATES - 1) ||
  354. !minstrel_ht_txstat_valid(&ar[i + 1]);
  355. if (!minstrel_ht_txstat_valid(&ar[i]))
  356. break;
  357. group = minstrel_ht_get_group_idx(&ar[i]);
  358. rate = &mi->groups[group].rates[ar[i].idx % 8];
  359. if (last)
  360. rate->success += info->status.ampdu_ack_len;
  361. rate->attempts += ar[i].count * info->status.ampdu_len;
  362. }
  363. /*
  364. * check for sudden death of spatial multiplexing,
  365. * downgrade to a lower number of streams if necessary.
  366. */
  367. rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
  368. if (rate->attempts > 30 &&
  369. MINSTREL_FRAC(rate->success, rate->attempts) <
  370. MINSTREL_FRAC(20, 100))
  371. minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
  372. rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
  373. if (rate2->attempts > 30 &&
  374. MINSTREL_FRAC(rate2->success, rate2->attempts) <
  375. MINSTREL_FRAC(20, 100))
  376. minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
  377. if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
  378. minstrel_ht_update_stats(mp, mi);
  379. minstrel_aggr_check(mp, 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. struct ieee80211_tx_rate_control *txrc,
  429. bool sample, bool rtscts)
  430. {
  431. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  432. struct minstrel_rate_stats *mr;
  433. mr = minstrel_get_ratestats(mi, index);
  434. if (!mr->retry_updated)
  435. minstrel_calc_retransmit(mp, mi, index);
  436. if (sample)
  437. rate->count = 1;
  438. else if (mr->probability < MINSTREL_FRAC(20, 100))
  439. rate->count = 2;
  440. else if (rtscts)
  441. rate->count = mr->retry_count_rtscts;
  442. else
  443. rate->count = mr->retry_count;
  444. rate->flags = IEEE80211_TX_RC_MCS | group->flags;
  445. if (rtscts)
  446. rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  447. rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
  448. }
  449. static inline int
  450. minstrel_get_duration(int index)
  451. {
  452. const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
  453. return group->duration[index % MCS_GROUP_RATES];
  454. }
  455. static int
  456. minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
  457. {
  458. struct minstrel_rate_stats *mr;
  459. struct minstrel_mcs_group_data *mg;
  460. int sample_idx = 0;
  461. if (mi->sample_wait > 0) {
  462. mi->sample_wait--;
  463. return -1;
  464. }
  465. if (!mi->sample_tries)
  466. return -1;
  467. mi->sample_tries--;
  468. mg = &mi->groups[mi->sample_group];
  469. sample_idx = sample_table[mg->column][mg->index];
  470. mr = &mg->rates[sample_idx];
  471. sample_idx += mi->sample_group * MCS_GROUP_RATES;
  472. minstrel_next_sample_idx(mi);
  473. /*
  474. * When not using MRR, do not sample if the probability is already
  475. * higher than 95% to avoid wasting airtime
  476. */
  477. if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
  478. return -1;
  479. /*
  480. * Make sure that lower rates get sampled only occasionally,
  481. * if the link is working perfectly.
  482. */
  483. if (minstrel_get_duration(sample_idx) >
  484. minstrel_get_duration(mi->max_tp_rate)) {
  485. if (mr->sample_skipped < 20)
  486. return -1;
  487. if (mi->sample_slow++ > 2)
  488. return -1;
  489. }
  490. return sample_idx;
  491. }
  492. static void
  493. minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  494. struct ieee80211_tx_rate_control *txrc)
  495. {
  496. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  497. struct ieee80211_tx_rate *ar = info->status.rates;
  498. struct minstrel_ht_sta_priv *msp = priv_sta;
  499. struct minstrel_ht_sta *mi = &msp->ht;
  500. struct minstrel_priv *mp = priv;
  501. int sample_idx;
  502. bool sample = false;
  503. if (rate_control_send_low(sta, priv_sta, txrc))
  504. return;
  505. if (!msp->is_ht)
  506. return mac80211_minstrel.get_rate(priv, sta, &msp->legacy, txrc);
  507. info->flags |= mi->tx_flags;
  508. sample_idx = minstrel_get_sample_rate(mp, mi);
  509. if (sample_idx >= 0) {
  510. sample = true;
  511. minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
  512. txrc, true, false);
  513. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  514. } else {
  515. minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
  516. txrc, false, false);
  517. }
  518. if (mp->hw->max_rates >= 3) {
  519. /*
  520. * At least 3 tx rates supported, use
  521. * sample_rate -> max_tp_rate -> max_prob_rate for sampling and
  522. * max_tp_rate -> max_tp_rate2 -> max_prob_rate by default.
  523. */
  524. if (sample_idx >= 0)
  525. minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
  526. txrc, false, false);
  527. else
  528. minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
  529. txrc, false, true);
  530. minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate,
  531. txrc, false, !sample);
  532. ar[3].count = 0;
  533. ar[3].idx = -1;
  534. } else if (mp->hw->max_rates == 2) {
  535. /*
  536. * Only 2 tx rates supported, use
  537. * sample_rate -> max_prob_rate for sampling and
  538. * max_tp_rate -> max_prob_rate by default.
  539. */
  540. minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_prob_rate,
  541. txrc, false, !sample);
  542. ar[2].count = 0;
  543. ar[2].idx = -1;
  544. } else {
  545. /* Not using MRR, only use the first rate */
  546. ar[1].count = 0;
  547. ar[1].idx = -1;
  548. }
  549. mi->total_packets++;
  550. /* wraparound */
  551. if (mi->total_packets == ~0) {
  552. mi->total_packets = 0;
  553. mi->sample_packets = 0;
  554. }
  555. }
  556. static void
  557. minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
  558. struct ieee80211_sta *sta, void *priv_sta,
  559. enum nl80211_channel_type oper_chan_type)
  560. {
  561. struct minstrel_priv *mp = priv;
  562. struct minstrel_ht_sta_priv *msp = priv_sta;
  563. struct minstrel_ht_sta *mi = &msp->ht;
  564. struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
  565. struct ieee80211_local *local = hw_to_local(mp->hw);
  566. u16 sta_cap = sta->ht_cap.cap;
  567. int n_supported = 0;
  568. int ack_dur;
  569. int stbc;
  570. int i;
  571. /* fall back to the old minstrel for legacy stations */
  572. if (!sta->ht_cap.ht_supported)
  573. goto use_legacy;
  574. BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) !=
  575. MINSTREL_MAX_STREAMS * MINSTREL_STREAM_GROUPS);
  576. msp->is_ht = true;
  577. memset(mi, 0, sizeof(*mi));
  578. mi->stats_update = jiffies;
  579. ack_dur = ieee80211_frame_duration(local, 10, 60, 1, 1);
  580. mi->overhead = ieee80211_frame_duration(local, 0, 60, 1, 1) + ack_dur;
  581. mi->overhead_rtscts = mi->overhead + 2 * ack_dur;
  582. mi->avg_ampdu_len = MINSTREL_FRAC(1, 1);
  583. /* When using MRR, sample more on the first attempt, without delay */
  584. if (mp->has_mrr) {
  585. mi->sample_count = 16;
  586. mi->sample_wait = 0;
  587. } else {
  588. mi->sample_count = 8;
  589. mi->sample_wait = 8;
  590. }
  591. mi->sample_tries = 4;
  592. stbc = (sta_cap & IEEE80211_HT_CAP_RX_STBC) >>
  593. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  594. mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT;
  595. if (sta_cap & IEEE80211_HT_CAP_LDPC_CODING)
  596. mi->tx_flags |= IEEE80211_TX_CTL_LDPC;
  597. if (oper_chan_type != NL80211_CHAN_HT40MINUS &&
  598. oper_chan_type != NL80211_CHAN_HT40PLUS)
  599. sta_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  600. for (i = 0; i < ARRAY_SIZE(mi->groups); i++) {
  601. u16 req = 0;
  602. mi->groups[i].supported = 0;
  603. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_SHORT_GI) {
  604. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  605. req |= IEEE80211_HT_CAP_SGI_40;
  606. else
  607. req |= IEEE80211_HT_CAP_SGI_20;
  608. }
  609. if (minstrel_mcs_groups[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  610. req |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  611. if ((sta_cap & req) != req)
  612. continue;
  613. mi->groups[i].supported =
  614. mcs->rx_mask[minstrel_mcs_groups[i].streams - 1];
  615. if (mi->groups[i].supported)
  616. n_supported++;
  617. }
  618. if (!n_supported)
  619. goto use_legacy;
  620. return;
  621. use_legacy:
  622. msp->is_ht = false;
  623. memset(&msp->legacy, 0, sizeof(msp->legacy));
  624. msp->legacy.r = msp->ratelist;
  625. msp->legacy.sample_table = msp->sample_table;
  626. return mac80211_minstrel.rate_init(priv, sband, sta, &msp->legacy);
  627. }
  628. static void
  629. minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband,
  630. struct ieee80211_sta *sta, void *priv_sta)
  631. {
  632. struct minstrel_priv *mp = priv;
  633. minstrel_ht_update_caps(priv, sband, sta, priv_sta, mp->hw->conf.channel_type);
  634. }
  635. static void
  636. minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband,
  637. struct ieee80211_sta *sta, void *priv_sta,
  638. u32 changed, enum nl80211_channel_type oper_chan_type)
  639. {
  640. minstrel_ht_update_caps(priv, sband, sta, priv_sta, oper_chan_type);
  641. }
  642. static void *
  643. minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  644. {
  645. struct ieee80211_supported_band *sband;
  646. struct minstrel_ht_sta_priv *msp;
  647. struct minstrel_priv *mp = priv;
  648. struct ieee80211_hw *hw = mp->hw;
  649. int max_rates = 0;
  650. int i;
  651. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  652. sband = hw->wiphy->bands[i];
  653. if (sband && sband->n_bitrates > max_rates)
  654. max_rates = sband->n_bitrates;
  655. }
  656. msp = kzalloc(sizeof(struct minstrel_ht_sta), gfp);
  657. if (!msp)
  658. return NULL;
  659. msp->ratelist = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  660. if (!msp->ratelist)
  661. goto error;
  662. msp->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  663. if (!msp->sample_table)
  664. goto error1;
  665. return msp;
  666. error1:
  667. kfree(msp->ratelist);
  668. error:
  669. kfree(msp);
  670. return NULL;
  671. }
  672. static void
  673. minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  674. {
  675. struct minstrel_ht_sta_priv *msp = priv_sta;
  676. kfree(msp->sample_table);
  677. kfree(msp->ratelist);
  678. kfree(msp);
  679. }
  680. static void *
  681. minstrel_ht_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  682. {
  683. return mac80211_minstrel.alloc(hw, debugfsdir);
  684. }
  685. static void
  686. minstrel_ht_free(void *priv)
  687. {
  688. mac80211_minstrel.free(priv);
  689. }
  690. static struct rate_control_ops mac80211_minstrel_ht = {
  691. .name = "minstrel_ht",
  692. .tx_status = minstrel_ht_tx_status,
  693. .get_rate = minstrel_ht_get_rate,
  694. .rate_init = minstrel_ht_rate_init,
  695. .rate_update = minstrel_ht_rate_update,
  696. .alloc_sta = minstrel_ht_alloc_sta,
  697. .free_sta = minstrel_ht_free_sta,
  698. .alloc = minstrel_ht_alloc,
  699. .free = minstrel_ht_free,
  700. #ifdef CONFIG_MAC80211_DEBUGFS
  701. .add_sta_debugfs = minstrel_ht_add_sta_debugfs,
  702. .remove_sta_debugfs = minstrel_ht_remove_sta_debugfs,
  703. #endif
  704. };
  705. static void
  706. init_sample_table(void)
  707. {
  708. int col, i, new_idx;
  709. u8 rnd[MCS_GROUP_RATES];
  710. memset(sample_table, 0xff, sizeof(sample_table));
  711. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  712. for (i = 0; i < MCS_GROUP_RATES; i++) {
  713. get_random_bytes(rnd, sizeof(rnd));
  714. new_idx = (i + rnd[i]) % MCS_GROUP_RATES;
  715. while (sample_table[col][new_idx] != 0xff)
  716. new_idx = (new_idx + 1) % MCS_GROUP_RATES;
  717. sample_table[col][new_idx] = i;
  718. }
  719. }
  720. }
  721. int __init
  722. rc80211_minstrel_ht_init(void)
  723. {
  724. init_sample_table();
  725. return ieee80211_rate_control_register(&mac80211_minstrel_ht);
  726. }
  727. void
  728. rc80211_minstrel_ht_exit(void)
  729. {
  730. ieee80211_rate_control_unregister(&mac80211_minstrel_ht);
  731. }