rc80211_minstrel_ht.c 28 KB

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