rc80211_minstrel_ht.c 26 KB

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