rc80211_minstrel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * Copyright (C) 2008 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. * Based on minstrel.c:
  9. * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
  10. * Sponsored by Indranet Technologies Ltd
  11. *
  12. * Based on sample.c:
  13. * Copyright (c) 2005 John Bicket
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer,
  21. * without modification.
  22. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  23. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  24. * redistribution must be conditioned upon including a substantially
  25. * similar Disclaimer requirement for further binary redistribution.
  26. * 3. Neither the names of the above-listed copyright holders nor the names
  27. * of any contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * Alternatively, this software may be distributed under the terms of the
  31. * GNU General Public License ("GPL") version 2 as published by the Free
  32. * Software Foundation.
  33. *
  34. * NO WARRANTY
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  38. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  39. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  40. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  41. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  42. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  43. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  45. * THE POSSIBILITY OF SUCH DAMAGES.
  46. */
  47. #include <linux/netdevice.h>
  48. #include <linux/types.h>
  49. #include <linux/skbuff.h>
  50. #include <linux/debugfs.h>
  51. #include <linux/random.h>
  52. #include <linux/ieee80211.h>
  53. #include <linux/slab.h>
  54. #include <net/mac80211.h>
  55. #include "rate.h"
  56. #include "rc80211_minstrel.h"
  57. #define SAMPLE_TBL(_mi, _idx, _col) \
  58. _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
  59. /* convert mac80211 rate index to local array index */
  60. static inline int
  61. rix_to_ndx(struct minstrel_sta_info *mi, int rix)
  62. {
  63. int i = rix;
  64. for (i = rix; i >= 0; i--)
  65. if (mi->r[i].rix == rix)
  66. break;
  67. return i;
  68. }
  69. /* return current EMWA throughput */
  70. int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma)
  71. {
  72. int usecs;
  73. usecs = mr->perfect_tx_time;
  74. if (!usecs)
  75. usecs = 1000000;
  76. /* reset thr. below 10% success */
  77. if (mr->stats.prob_ewma < MINSTREL_FRAC(10, 100))
  78. return 0;
  79. if (prob_ewma > MINSTREL_FRAC(90, 100))
  80. return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs));
  81. else
  82. return MINSTREL_TRUNC(100000 * (prob_ewma / usecs));
  83. }
  84. /* find & sort topmost throughput rates */
  85. static inline void
  86. minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
  87. {
  88. int j;
  89. struct minstrel_rate_stats *tmp_mrs;
  90. struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats;
  91. for (j = MAX_THR_RATES; j > 0; --j) {
  92. tmp_mrs = &mi->r[tp_list[j - 1]].stats;
  93. if (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_ewma) <=
  94. minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_ewma))
  95. break;
  96. }
  97. if (j < MAX_THR_RATES - 1)
  98. memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
  99. if (j < MAX_THR_RATES)
  100. tp_list[j] = i;
  101. }
  102. static void
  103. minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
  104. int offset, int idx)
  105. {
  106. struct minstrel_rate *r = &mi->r[idx];
  107. ratetbl->rate[offset].idx = r->rix;
  108. ratetbl->rate[offset].count = r->adjusted_retry_count;
  109. ratetbl->rate[offset].count_cts = r->retry_count_cts;
  110. ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts;
  111. }
  112. static void
  113. minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
  114. {
  115. struct ieee80211_sta_rates *ratetbl;
  116. int i = 0;
  117. ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
  118. if (!ratetbl)
  119. return;
  120. /* Start with max_tp_rate */
  121. minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
  122. if (mp->hw->max_rates >= 3) {
  123. /* At least 3 tx rates supported, use max_tp_rate2 next */
  124. minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
  125. }
  126. if (mp->hw->max_rates >= 2) {
  127. /* At least 2 tx rates supported, use max_prob_rate next */
  128. minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
  129. }
  130. /* Use lowest rate last */
  131. ratetbl->rate[i].idx = mi->lowest_rix;
  132. ratetbl->rate[i].count = mp->max_retry;
  133. ratetbl->rate[i].count_cts = mp->max_retry;
  134. ratetbl->rate[i].count_rts = mp->max_retry;
  135. rate_control_set_rates(mp->hw, mi->sta, ratetbl);
  136. }
  137. /*
  138. * Recalculate statistics and counters of a given rate
  139. */
  140. void
  141. minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs)
  142. {
  143. unsigned int cur_prob;
  144. if (unlikely(mrs->attempts > 0)) {
  145. mrs->sample_skipped = 0;
  146. cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
  147. if (unlikely(!mrs->att_hist)) {
  148. mrs->prob_ewma = cur_prob;
  149. } else {
  150. /* update exponential weighted moving variance */
  151. mrs->prob_ewmv = minstrel_ewmv(mrs->prob_ewmv,
  152. cur_prob,
  153. mrs->prob_ewma,
  154. EWMA_LEVEL);
  155. /*update exponential weighted moving avarage */
  156. mrs->prob_ewma = minstrel_ewma(mrs->prob_ewma,
  157. cur_prob,
  158. EWMA_LEVEL);
  159. }
  160. mrs->att_hist += mrs->attempts;
  161. mrs->succ_hist += mrs->success;
  162. } else {
  163. mrs->sample_skipped++;
  164. }
  165. mrs->last_success = mrs->success;
  166. mrs->last_attempts = mrs->attempts;
  167. mrs->success = 0;
  168. mrs->attempts = 0;
  169. }
  170. static void
  171. minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
  172. {
  173. u8 tmp_tp_rate[MAX_THR_RATES];
  174. u8 tmp_prob_rate = 0;
  175. int i, tmp_cur_tp, tmp_prob_tp;
  176. for (i = 0; i < MAX_THR_RATES; i++)
  177. tmp_tp_rate[i] = 0;
  178. for (i = 0; i < mi->n_rates; i++) {
  179. struct minstrel_rate *mr = &mi->r[i];
  180. struct minstrel_rate_stats *mrs = &mi->r[i].stats;
  181. struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats;
  182. /* Update statistics of success probability per rate */
  183. minstrel_calc_rate_stats(mrs);
  184. /* Sample less often below the 10% chance of success.
  185. * Sample less often above the 95% chance of success. */
  186. if (mrs->prob_ewma > MINSTREL_FRAC(95, 100) ||
  187. mrs->prob_ewma < MINSTREL_FRAC(10, 100)) {
  188. mr->adjusted_retry_count = mrs->retry_count >> 1;
  189. if (mr->adjusted_retry_count > 2)
  190. mr->adjusted_retry_count = 2;
  191. mr->sample_limit = 4;
  192. } else {
  193. mr->sample_limit = -1;
  194. mr->adjusted_retry_count = mrs->retry_count;
  195. }
  196. if (!mr->adjusted_retry_count)
  197. mr->adjusted_retry_count = 2;
  198. minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
  199. /* To determine the most robust rate (max_prob_rate) used at
  200. * 3rd mmr stage we distinct between two cases:
  201. * (1) if any success probabilitiy >= 95%, out of those rates
  202. * choose the maximum throughput rate as max_prob_rate
  203. * (2) if all success probabilities < 95%, the rate with
  204. * highest success probability is chosen as max_prob_rate */
  205. if (mrs->prob_ewma >= MINSTREL_FRAC(95, 100)) {
  206. tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_ewma);
  207. tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate],
  208. tmp_mrs->prob_ewma);
  209. if (tmp_cur_tp >= tmp_prob_tp)
  210. tmp_prob_rate = i;
  211. } else {
  212. if (mrs->prob_ewma >= tmp_mrs->prob_ewma)
  213. tmp_prob_rate = i;
  214. }
  215. }
  216. /* Assign the new rate set */
  217. memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
  218. mi->max_prob_rate = tmp_prob_rate;
  219. #ifdef CONFIG_MAC80211_DEBUGFS
  220. /* use fixed index if set */
  221. if (mp->fixed_rate_idx != -1) {
  222. mi->max_tp_rate[0] = mp->fixed_rate_idx;
  223. mi->max_tp_rate[1] = mp->fixed_rate_idx;
  224. mi->max_prob_rate = mp->fixed_rate_idx;
  225. }
  226. #endif
  227. /* Reset update timer */
  228. mi->last_stats_update = jiffies;
  229. minstrel_update_rates(mp, mi);
  230. }
  231. static void
  232. minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
  233. void *priv_sta, struct ieee80211_tx_status *st)
  234. {
  235. struct ieee80211_tx_info *info = st->info;
  236. struct minstrel_priv *mp = priv;
  237. struct minstrel_sta_info *mi = priv_sta;
  238. struct ieee80211_tx_rate *ar = info->status.rates;
  239. int i, ndx;
  240. int success;
  241. success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  242. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  243. if (ar[i].idx < 0)
  244. break;
  245. ndx = rix_to_ndx(mi, ar[i].idx);
  246. if (ndx < 0)
  247. continue;
  248. mi->r[ndx].stats.attempts += ar[i].count;
  249. if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
  250. mi->r[ndx].stats.success += success;
  251. }
  252. if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0))
  253. mi->sample_packets++;
  254. if (mi->sample_deferred > 0)
  255. mi->sample_deferred--;
  256. if (time_after(jiffies, mi->last_stats_update +
  257. (mp->update_interval * HZ) / 1000))
  258. minstrel_update_stats(mp, mi);
  259. }
  260. static inline unsigned int
  261. minstrel_get_retry_count(struct minstrel_rate *mr,
  262. struct ieee80211_tx_info *info)
  263. {
  264. u8 retry = mr->adjusted_retry_count;
  265. if (info->control.use_rts)
  266. retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry));
  267. else if (info->control.use_cts_prot)
  268. retry = max_t(u8, 2, min(mr->retry_count_cts, retry));
  269. return retry;
  270. }
  271. static int
  272. minstrel_get_next_sample(struct minstrel_sta_info *mi)
  273. {
  274. unsigned int sample_ndx;
  275. sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
  276. mi->sample_row++;
  277. if ((int) mi->sample_row >= mi->n_rates) {
  278. mi->sample_row = 0;
  279. mi->sample_column++;
  280. if (mi->sample_column >= SAMPLE_COLUMNS)
  281. mi->sample_column = 0;
  282. }
  283. return sample_ndx;
  284. }
  285. static void
  286. minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
  287. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  288. {
  289. struct sk_buff *skb = txrc->skb;
  290. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  291. struct minstrel_sta_info *mi = priv_sta;
  292. struct minstrel_priv *mp = priv;
  293. struct ieee80211_tx_rate *rate = &info->control.rates[0];
  294. struct minstrel_rate *msr, *mr;
  295. unsigned int ndx;
  296. bool mrr_capable;
  297. bool prev_sample;
  298. int delta;
  299. int sampling_ratio;
  300. /* management/no-ack frames do not use rate control */
  301. if (rate_control_send_low(sta, priv_sta, txrc))
  302. return;
  303. /* check multi-rate-retry capabilities & adjust lookaround_rate */
  304. mrr_capable = mp->has_mrr &&
  305. !txrc->rts &&
  306. !txrc->bss_conf->use_cts_prot;
  307. if (mrr_capable)
  308. sampling_ratio = mp->lookaround_rate_mrr;
  309. else
  310. sampling_ratio = mp->lookaround_rate;
  311. /* increase sum packet counter */
  312. mi->total_packets++;
  313. #ifdef CONFIG_MAC80211_DEBUGFS
  314. if (mp->fixed_rate_idx != -1)
  315. return;
  316. #endif
  317. /* Don't use EAPOL frames for sampling on non-mrr hw */
  318. if (mp->hw->max_rates == 1 &&
  319. (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  320. return;
  321. delta = (mi->total_packets * sampling_ratio / 100) -
  322. (mi->sample_packets + mi->sample_deferred / 2);
  323. /* delta < 0: no sampling required */
  324. prev_sample = mi->prev_sample;
  325. mi->prev_sample = false;
  326. if (delta < 0 || (!mrr_capable && prev_sample))
  327. return;
  328. if (mi->total_packets >= 10000) {
  329. mi->sample_deferred = 0;
  330. mi->sample_packets = 0;
  331. mi->total_packets = 0;
  332. } else if (delta > mi->n_rates * 2) {
  333. /* With multi-rate retry, not every planned sample
  334. * attempt actually gets used, due to the way the retry
  335. * chain is set up - [max_tp,sample,prob,lowest] for
  336. * sample_rate < max_tp.
  337. *
  338. * If there's too much sampling backlog and the link
  339. * starts getting worse, minstrel would start bursting
  340. * out lots of sampling frames, which would result
  341. * in a large throughput loss. */
  342. mi->sample_packets += (delta - mi->n_rates * 2);
  343. }
  344. /* get next random rate sample */
  345. ndx = minstrel_get_next_sample(mi);
  346. msr = &mi->r[ndx];
  347. mr = &mi->r[mi->max_tp_rate[0]];
  348. /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
  349. * rate sampling method should be used.
  350. * Respect such rates that are not sampled for 20 interations.
  351. */
  352. if (mrr_capable &&
  353. msr->perfect_tx_time > mr->perfect_tx_time &&
  354. msr->stats.sample_skipped < 20) {
  355. /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
  356. * packets that have the sampling rate deferred to the
  357. * second MRR stage. Increase the sample counter only
  358. * if the deferred sample rate was actually used.
  359. * Use the sample_deferred counter to make sure that
  360. * the sampling is not done in large bursts */
  361. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  362. rate++;
  363. mi->sample_deferred++;
  364. } else {
  365. if (!msr->sample_limit)
  366. return;
  367. mi->sample_packets++;
  368. if (msr->sample_limit > 0)
  369. msr->sample_limit--;
  370. }
  371. /* If we're not using MRR and the sampling rate already
  372. * has a probability of >95%, we shouldn't be attempting
  373. * to use it, as this only wastes precious airtime */
  374. if (!mrr_capable &&
  375. (mi->r[ndx].stats.prob_ewma > MINSTREL_FRAC(95, 100)))
  376. return;
  377. mi->prev_sample = true;
  378. rate->idx = mi->r[ndx].rix;
  379. rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
  380. }
  381. static void
  382. calc_rate_durations(enum nl80211_band band,
  383. struct minstrel_rate *d,
  384. struct ieee80211_rate *rate,
  385. struct cfg80211_chan_def *chandef)
  386. {
  387. int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
  388. int shift = ieee80211_chandef_get_shift(chandef);
  389. d->perfect_tx_time = ieee80211_frame_duration(band, 1200,
  390. DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
  391. shift);
  392. d->ack_time = ieee80211_frame_duration(band, 10,
  393. DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
  394. shift);
  395. }
  396. static void
  397. init_sample_table(struct minstrel_sta_info *mi)
  398. {
  399. unsigned int i, col, new_idx;
  400. u8 rnd[8];
  401. mi->sample_column = 0;
  402. mi->sample_row = 0;
  403. memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
  404. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  405. prandom_bytes(rnd, sizeof(rnd));
  406. for (i = 0; i < mi->n_rates; i++) {
  407. new_idx = (i + rnd[i & 7]) % mi->n_rates;
  408. while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
  409. new_idx = (new_idx + 1) % mi->n_rates;
  410. SAMPLE_TBL(mi, new_idx, col) = i;
  411. }
  412. }
  413. }
  414. static void
  415. minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
  416. struct cfg80211_chan_def *chandef,
  417. struct ieee80211_sta *sta, void *priv_sta)
  418. {
  419. struct minstrel_sta_info *mi = priv_sta;
  420. struct minstrel_priv *mp = priv;
  421. struct ieee80211_rate *ctl_rate;
  422. unsigned int i, n = 0;
  423. unsigned int t_slot = 9; /* FIXME: get real slot time */
  424. u32 rate_flags;
  425. mi->sta = sta;
  426. mi->lowest_rix = rate_lowest_index(sband, sta);
  427. ctl_rate = &sband->bitrates[mi->lowest_rix];
  428. mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
  429. ctl_rate->bitrate,
  430. !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1,
  431. ieee80211_chandef_get_shift(chandef));
  432. rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  433. memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
  434. mi->max_prob_rate = 0;
  435. for (i = 0; i < sband->n_bitrates; i++) {
  436. struct minstrel_rate *mr = &mi->r[n];
  437. struct minstrel_rate_stats *mrs = &mi->r[n].stats;
  438. unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
  439. unsigned int tx_time_single;
  440. unsigned int cw = mp->cw_min;
  441. int shift;
  442. if (!rate_supported(sta, sband->band, i))
  443. continue;
  444. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  445. continue;
  446. n++;
  447. memset(mr, 0, sizeof(*mr));
  448. memset(mrs, 0, sizeof(*mrs));
  449. mr->rix = i;
  450. shift = ieee80211_chandef_get_shift(chandef);
  451. mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  452. (1 << shift) * 5);
  453. calc_rate_durations(sband->band, mr, &sband->bitrates[i],
  454. chandef);
  455. /* calculate maximum number of retransmissions before
  456. * fallback (based on maximum segment size) */
  457. mr->sample_limit = -1;
  458. mrs->retry_count = 1;
  459. mr->retry_count_cts = 1;
  460. mrs->retry_count_rtscts = 1;
  461. tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
  462. do {
  463. /* add one retransmission */
  464. tx_time_single = mr->ack_time + mr->perfect_tx_time;
  465. /* contention window */
  466. tx_time_single += (t_slot * cw) >> 1;
  467. cw = min((cw << 1) | 1, mp->cw_max);
  468. tx_time += tx_time_single;
  469. tx_time_cts += tx_time_single + mi->sp_ack_dur;
  470. tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
  471. if ((tx_time_cts < mp->segment_size) &&
  472. (mr->retry_count_cts < mp->max_retry))
  473. mr->retry_count_cts++;
  474. if ((tx_time_rtscts < mp->segment_size) &&
  475. (mrs->retry_count_rtscts < mp->max_retry))
  476. mrs->retry_count_rtscts++;
  477. } while ((tx_time < mp->segment_size) &&
  478. (++mr->stats.retry_count < mp->max_retry));
  479. mr->adjusted_retry_count = mrs->retry_count;
  480. if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
  481. mr->retry_count_cts = mrs->retry_count;
  482. }
  483. for (i = n; i < sband->n_bitrates; i++) {
  484. struct minstrel_rate *mr = &mi->r[i];
  485. mr->rix = -1;
  486. }
  487. mi->n_rates = n;
  488. mi->last_stats_update = jiffies;
  489. init_sample_table(mi);
  490. minstrel_update_rates(mp, mi);
  491. }
  492. static void *
  493. minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  494. {
  495. struct ieee80211_supported_band *sband;
  496. struct minstrel_sta_info *mi;
  497. struct minstrel_priv *mp = priv;
  498. struct ieee80211_hw *hw = mp->hw;
  499. int max_rates = 0;
  500. int i;
  501. mi = kzalloc(sizeof(struct minstrel_sta_info), gfp);
  502. if (!mi)
  503. return NULL;
  504. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  505. sband = hw->wiphy->bands[i];
  506. if (sband && sband->n_bitrates > max_rates)
  507. max_rates = sband->n_bitrates;
  508. }
  509. mi->r = kcalloc(max_rates, sizeof(struct minstrel_rate), gfp);
  510. if (!mi->r)
  511. goto error;
  512. mi->sample_table = kmalloc_array(max_rates, SAMPLE_COLUMNS, gfp);
  513. if (!mi->sample_table)
  514. goto error1;
  515. mi->last_stats_update = jiffies;
  516. return mi;
  517. error1:
  518. kfree(mi->r);
  519. error:
  520. kfree(mi);
  521. return NULL;
  522. }
  523. static void
  524. minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  525. {
  526. struct minstrel_sta_info *mi = priv_sta;
  527. kfree(mi->sample_table);
  528. kfree(mi->r);
  529. kfree(mi);
  530. }
  531. static void
  532. minstrel_init_cck_rates(struct minstrel_priv *mp)
  533. {
  534. static const int bitrates[4] = { 10, 20, 55, 110 };
  535. struct ieee80211_supported_band *sband;
  536. u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  537. int i, j;
  538. sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ];
  539. if (!sband)
  540. return;
  541. for (i = 0, j = 0; i < sband->n_bitrates; i++) {
  542. struct ieee80211_rate *rate = &sband->bitrates[i];
  543. if (rate->flags & IEEE80211_RATE_ERP_G)
  544. continue;
  545. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  546. continue;
  547. for (j = 0; j < ARRAY_SIZE(bitrates); j++) {
  548. if (rate->bitrate != bitrates[j])
  549. continue;
  550. mp->cck_rates[j] = i;
  551. break;
  552. }
  553. }
  554. }
  555. static void *
  556. minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  557. {
  558. struct minstrel_priv *mp;
  559. mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
  560. if (!mp)
  561. return NULL;
  562. /* contention window settings
  563. * Just an approximation. Using the per-queue values would complicate
  564. * the calculations and is probably unnecessary */
  565. mp->cw_min = 15;
  566. mp->cw_max = 1023;
  567. /* number of packets (in %) to use for sampling other rates
  568. * sample less often for non-mrr packets, because the overhead
  569. * is much higher than with mrr */
  570. mp->lookaround_rate = 5;
  571. mp->lookaround_rate_mrr = 10;
  572. /* maximum time that the hw is allowed to stay in one MRR segment */
  573. mp->segment_size = 6000;
  574. if (hw->max_rate_tries > 0)
  575. mp->max_retry = hw->max_rate_tries;
  576. else
  577. /* safe default, does not necessarily have to match hw properties */
  578. mp->max_retry = 7;
  579. if (hw->max_rates >= 4)
  580. mp->has_mrr = true;
  581. mp->hw = hw;
  582. mp->update_interval = 100;
  583. #ifdef CONFIG_MAC80211_DEBUGFS
  584. mp->fixed_rate_idx = (u32) -1;
  585. mp->dbg_fixed_rate = debugfs_create_u32("fixed_rate_idx",
  586. 0666, debugfsdir, &mp->fixed_rate_idx);
  587. #endif
  588. minstrel_init_cck_rates(mp);
  589. return mp;
  590. }
  591. static void
  592. minstrel_free(void *priv)
  593. {
  594. #ifdef CONFIG_MAC80211_DEBUGFS
  595. debugfs_remove(((struct minstrel_priv *)priv)->dbg_fixed_rate);
  596. #endif
  597. kfree(priv);
  598. }
  599. static u32 minstrel_get_expected_throughput(void *priv_sta)
  600. {
  601. struct minstrel_sta_info *mi = priv_sta;
  602. struct minstrel_rate_stats *tmp_mrs;
  603. int idx = mi->max_tp_rate[0];
  604. int tmp_cur_tp;
  605. /* convert pkt per sec in kbps (1200 is the average pkt size used for
  606. * computing cur_tp
  607. */
  608. tmp_mrs = &mi->r[idx].stats;
  609. tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_ewma) * 10;
  610. tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024;
  611. return tmp_cur_tp;
  612. }
  613. const struct rate_control_ops mac80211_minstrel = {
  614. .name = "minstrel",
  615. .tx_status_ext = minstrel_tx_status,
  616. .get_rate = minstrel_get_rate,
  617. .rate_init = minstrel_rate_init,
  618. .alloc = minstrel_alloc,
  619. .free = minstrel_free,
  620. .alloc_sta = minstrel_alloc_sta,
  621. .free_sta = minstrel_free_sta,
  622. #ifdef CONFIG_MAC80211_DEBUGFS
  623. .add_sta_debugfs = minstrel_add_sta_debugfs,
  624. .remove_sta_debugfs = minstrel_remove_sta_debugfs,
  625. #endif
  626. .get_expected_throughput = minstrel_get_expected_throughput,
  627. };
  628. int __init
  629. rc80211_minstrel_init(void)
  630. {
  631. return ieee80211_rate_control_register(&mac80211_minstrel);
  632. }
  633. void
  634. rc80211_minstrel_exit(void)
  635. {
  636. ieee80211_rate_control_unregister(&mac80211_minstrel);
  637. }