rc80211_minstrel.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. /* find & sort topmost throughput rates */
  70. static inline void
  71. minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
  72. {
  73. int j = MAX_THR_RATES;
  74. while (j > 0 && mi->r[i].stats.cur_tp > mi->r[tp_list[j - 1]].stats.cur_tp)
  75. j--;
  76. if (j < MAX_THR_RATES - 1)
  77. memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
  78. if (j < MAX_THR_RATES)
  79. tp_list[j] = i;
  80. }
  81. static void
  82. minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
  83. int offset, int idx)
  84. {
  85. struct minstrel_rate *r = &mi->r[idx];
  86. ratetbl->rate[offset].idx = r->rix;
  87. ratetbl->rate[offset].count = r->adjusted_retry_count;
  88. ratetbl->rate[offset].count_cts = r->retry_count_cts;
  89. ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts;
  90. }
  91. static void
  92. minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
  93. {
  94. struct ieee80211_sta_rates *ratetbl;
  95. int i = 0;
  96. ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
  97. if (!ratetbl)
  98. return;
  99. /* Start with max_tp_rate */
  100. minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
  101. if (mp->hw->max_rates >= 3) {
  102. /* At least 3 tx rates supported, use max_tp_rate2 next */
  103. minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
  104. }
  105. if (mp->hw->max_rates >= 2) {
  106. /* At least 2 tx rates supported, use max_prob_rate next */
  107. minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
  108. }
  109. /* Use lowest rate last */
  110. ratetbl->rate[i].idx = mi->lowest_rix;
  111. ratetbl->rate[i].count = mp->max_retry;
  112. ratetbl->rate[i].count_cts = mp->max_retry;
  113. ratetbl->rate[i].count_rts = mp->max_retry;
  114. rate_control_set_rates(mp->hw, mi->sta, ratetbl);
  115. }
  116. static void
  117. minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
  118. {
  119. u8 tmp_tp_rate[MAX_THR_RATES];
  120. u8 tmp_prob_rate = 0;
  121. u32 usecs;
  122. int i;
  123. for (i = 0; i < MAX_THR_RATES; i++)
  124. tmp_tp_rate[i] = 0;
  125. for (i = 0; i < mi->n_rates; i++) {
  126. struct minstrel_rate *mr = &mi->r[i];
  127. struct minstrel_rate_stats *mrs = &mi->r[i].stats;
  128. usecs = mr->perfect_tx_time;
  129. if (!usecs)
  130. usecs = 1000000;
  131. if (unlikely(mrs->attempts > 0)) {
  132. mrs->sample_skipped = 0;
  133. mrs->cur_prob = MINSTREL_FRAC(mrs->success,
  134. mrs->attempts);
  135. mrs->succ_hist += mrs->success;
  136. mrs->att_hist += mrs->attempts;
  137. mrs->probability = minstrel_ewma(mrs->probability,
  138. mrs->cur_prob,
  139. EWMA_LEVEL);
  140. } else
  141. mrs->sample_skipped++;
  142. mrs->last_success = mrs->success;
  143. mrs->last_attempts = mrs->attempts;
  144. mrs->success = 0;
  145. mrs->attempts = 0;
  146. /* Update throughput per rate, reset thr. below 10% success */
  147. if (mrs->probability < MINSTREL_FRAC(10, 100))
  148. mrs->cur_tp = 0;
  149. else
  150. mrs->cur_tp = mrs->probability * (1000000 / usecs);
  151. /* Sample less often below the 10% chance of success.
  152. * Sample less often above the 95% chance of success. */
  153. if (mrs->probability > MINSTREL_FRAC(95, 100) ||
  154. mrs->probability < MINSTREL_FRAC(10, 100)) {
  155. mr->adjusted_retry_count = mrs->retry_count >> 1;
  156. if (mr->adjusted_retry_count > 2)
  157. mr->adjusted_retry_count = 2;
  158. mr->sample_limit = 4;
  159. } else {
  160. mr->sample_limit = -1;
  161. mr->adjusted_retry_count = mrs->retry_count;
  162. }
  163. if (!mr->adjusted_retry_count)
  164. mr->adjusted_retry_count = 2;
  165. minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
  166. /* To determine the most robust rate (max_prob_rate) used at
  167. * 3rd mmr stage we distinct between two cases:
  168. * (1) if any success probabilitiy >= 95%, out of those rates
  169. * choose the maximum throughput rate as max_prob_rate
  170. * (2) if all success probabilities < 95%, the rate with
  171. * highest success probability is chosen as max_prob_rate */
  172. if (mrs->probability >= MINSTREL_FRAC(95, 100)) {
  173. if (mrs->cur_tp >= mi->r[tmp_prob_rate].stats.cur_tp)
  174. tmp_prob_rate = i;
  175. } else {
  176. if (mrs->probability >= mi->r[tmp_prob_rate].stats.probability)
  177. tmp_prob_rate = i;
  178. }
  179. }
  180. /* Assign the new rate set */
  181. memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
  182. mi->max_prob_rate = tmp_prob_rate;
  183. #ifdef CONFIG_MAC80211_DEBUGFS
  184. /* use fixed index if set */
  185. if (mp->fixed_rate_idx != -1) {
  186. mi->max_tp_rate[0] = mp->fixed_rate_idx;
  187. mi->max_tp_rate[1] = mp->fixed_rate_idx;
  188. mi->max_prob_rate = mp->fixed_rate_idx;
  189. }
  190. #endif
  191. /* Reset update timer */
  192. mi->stats_update = jiffies;
  193. minstrel_update_rates(mp, mi);
  194. }
  195. static void
  196. minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
  197. struct ieee80211_sta *sta, void *priv_sta,
  198. struct ieee80211_tx_info *info)
  199. {
  200. struct minstrel_priv *mp = priv;
  201. struct minstrel_sta_info *mi = priv_sta;
  202. struct ieee80211_tx_rate *ar = info->status.rates;
  203. int i, ndx;
  204. int success;
  205. success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  206. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  207. if (ar[i].idx < 0)
  208. break;
  209. ndx = rix_to_ndx(mi, ar[i].idx);
  210. if (ndx < 0)
  211. continue;
  212. mi->r[ndx].stats.attempts += ar[i].count;
  213. if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
  214. mi->r[ndx].stats.success += success;
  215. }
  216. if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0))
  217. mi->sample_packets++;
  218. if (mi->sample_deferred > 0)
  219. mi->sample_deferred--;
  220. if (time_after(jiffies, mi->stats_update +
  221. (mp->update_interval * HZ) / 1000))
  222. minstrel_update_stats(mp, mi);
  223. }
  224. static inline unsigned int
  225. minstrel_get_retry_count(struct minstrel_rate *mr,
  226. struct ieee80211_tx_info *info)
  227. {
  228. u8 retry = mr->adjusted_retry_count;
  229. if (info->control.use_rts)
  230. retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry));
  231. else if (info->control.use_cts_prot)
  232. retry = max_t(u8, 2, min(mr->retry_count_cts, retry));
  233. return retry;
  234. }
  235. static int
  236. minstrel_get_next_sample(struct minstrel_sta_info *mi)
  237. {
  238. unsigned int sample_ndx;
  239. sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
  240. mi->sample_row++;
  241. if ((int) mi->sample_row >= mi->n_rates) {
  242. mi->sample_row = 0;
  243. mi->sample_column++;
  244. if (mi->sample_column >= SAMPLE_COLUMNS)
  245. mi->sample_column = 0;
  246. }
  247. return sample_ndx;
  248. }
  249. static void
  250. minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
  251. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  252. {
  253. struct sk_buff *skb = txrc->skb;
  254. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  255. struct minstrel_sta_info *mi = priv_sta;
  256. struct minstrel_priv *mp = priv;
  257. struct ieee80211_tx_rate *rate = &info->control.rates[0];
  258. struct minstrel_rate *msr, *mr;
  259. unsigned int ndx;
  260. bool mrr_capable;
  261. bool prev_sample;
  262. int delta;
  263. int sampling_ratio;
  264. /* management/no-ack frames do not use rate control */
  265. if (rate_control_send_low(sta, priv_sta, txrc))
  266. return;
  267. /* check multi-rate-retry capabilities & adjust lookaround_rate */
  268. mrr_capable = mp->has_mrr &&
  269. !txrc->rts &&
  270. !txrc->bss_conf->use_cts_prot;
  271. if (mrr_capable)
  272. sampling_ratio = mp->lookaround_rate_mrr;
  273. else
  274. sampling_ratio = mp->lookaround_rate;
  275. /* increase sum packet counter */
  276. mi->total_packets++;
  277. #ifdef CONFIG_MAC80211_DEBUGFS
  278. if (mp->fixed_rate_idx != -1)
  279. return;
  280. #endif
  281. delta = (mi->total_packets * sampling_ratio / 100) -
  282. (mi->sample_packets + mi->sample_deferred / 2);
  283. /* delta < 0: no sampling required */
  284. prev_sample = mi->prev_sample;
  285. mi->prev_sample = false;
  286. if (delta < 0 || (!mrr_capable && prev_sample))
  287. return;
  288. if (mi->total_packets >= 10000) {
  289. mi->sample_deferred = 0;
  290. mi->sample_packets = 0;
  291. mi->total_packets = 0;
  292. } else if (delta > mi->n_rates * 2) {
  293. /* With multi-rate retry, not every planned sample
  294. * attempt actually gets used, due to the way the retry
  295. * chain is set up - [max_tp,sample,prob,lowest] for
  296. * sample_rate < max_tp.
  297. *
  298. * If there's too much sampling backlog and the link
  299. * starts getting worse, minstrel would start bursting
  300. * out lots of sampling frames, which would result
  301. * in a large throughput loss. */
  302. mi->sample_packets += (delta - mi->n_rates * 2);
  303. }
  304. /* get next random rate sample */
  305. ndx = minstrel_get_next_sample(mi);
  306. msr = &mi->r[ndx];
  307. mr = &mi->r[mi->max_tp_rate[0]];
  308. /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
  309. * rate sampling method should be used.
  310. * Respect such rates that are not sampled for 20 interations.
  311. */
  312. if (mrr_capable &&
  313. msr->perfect_tx_time > mr->perfect_tx_time &&
  314. msr->stats.sample_skipped < 20) {
  315. /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
  316. * packets that have the sampling rate deferred to the
  317. * second MRR stage. Increase the sample counter only
  318. * if the deferred sample rate was actually used.
  319. * Use the sample_deferred counter to make sure that
  320. * the sampling is not done in large bursts */
  321. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  322. rate++;
  323. mi->sample_deferred++;
  324. } else {
  325. if (!msr->sample_limit)
  326. return;
  327. mi->sample_packets++;
  328. if (msr->sample_limit > 0)
  329. msr->sample_limit--;
  330. }
  331. /* If we're not using MRR and the sampling rate already
  332. * has a probability of >95%, we shouldn't be attempting
  333. * to use it, as this only wastes precious airtime */
  334. if (!mrr_capable &&
  335. (mi->r[ndx].stats.probability > MINSTREL_FRAC(95, 100)))
  336. return;
  337. mi->prev_sample = true;
  338. rate->idx = mi->r[ndx].rix;
  339. rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
  340. }
  341. static void
  342. calc_rate_durations(enum ieee80211_band band,
  343. struct minstrel_rate *d,
  344. struct ieee80211_rate *rate,
  345. struct cfg80211_chan_def *chandef)
  346. {
  347. int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
  348. int shift = ieee80211_chandef_get_shift(chandef);
  349. d->perfect_tx_time = ieee80211_frame_duration(band, 1200,
  350. DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
  351. shift);
  352. d->ack_time = ieee80211_frame_duration(band, 10,
  353. DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
  354. shift);
  355. }
  356. static void
  357. init_sample_table(struct minstrel_sta_info *mi)
  358. {
  359. unsigned int i, col, new_idx;
  360. u8 rnd[8];
  361. mi->sample_column = 0;
  362. mi->sample_row = 0;
  363. memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
  364. for (col = 0; col < SAMPLE_COLUMNS; col++) {
  365. prandom_bytes(rnd, sizeof(rnd));
  366. for (i = 0; i < mi->n_rates; i++) {
  367. new_idx = (i + rnd[i & 7]) % mi->n_rates;
  368. while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
  369. new_idx = (new_idx + 1) % mi->n_rates;
  370. SAMPLE_TBL(mi, new_idx, col) = i;
  371. }
  372. }
  373. }
  374. static void
  375. minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
  376. struct cfg80211_chan_def *chandef,
  377. struct ieee80211_sta *sta, void *priv_sta)
  378. {
  379. struct minstrel_sta_info *mi = priv_sta;
  380. struct minstrel_priv *mp = priv;
  381. struct ieee80211_rate *ctl_rate;
  382. unsigned int i, n = 0;
  383. unsigned int t_slot = 9; /* FIXME: get real slot time */
  384. u32 rate_flags;
  385. mi->sta = sta;
  386. mi->lowest_rix = rate_lowest_index(sband, sta);
  387. ctl_rate = &sband->bitrates[mi->lowest_rix];
  388. mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
  389. ctl_rate->bitrate,
  390. !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1,
  391. ieee80211_chandef_get_shift(chandef));
  392. rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  393. memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
  394. mi->max_prob_rate = 0;
  395. for (i = 0; i < sband->n_bitrates; i++) {
  396. struct minstrel_rate *mr = &mi->r[n];
  397. struct minstrel_rate_stats *mrs = &mi->r[n].stats;
  398. unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
  399. unsigned int tx_time_single;
  400. unsigned int cw = mp->cw_min;
  401. int shift;
  402. if (!rate_supported(sta, sband->band, i))
  403. continue;
  404. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  405. continue;
  406. n++;
  407. memset(mr, 0, sizeof(*mr));
  408. memset(mrs, 0, sizeof(*mrs));
  409. mr->rix = i;
  410. shift = ieee80211_chandef_get_shift(chandef);
  411. mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  412. (1 << shift) * 5);
  413. calc_rate_durations(sband->band, mr, &sband->bitrates[i],
  414. chandef);
  415. /* calculate maximum number of retransmissions before
  416. * fallback (based on maximum segment size) */
  417. mr->sample_limit = -1;
  418. mrs->retry_count = 1;
  419. mr->retry_count_cts = 1;
  420. mrs->retry_count_rtscts = 1;
  421. tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
  422. do {
  423. /* add one retransmission */
  424. tx_time_single = mr->ack_time + mr->perfect_tx_time;
  425. /* contention window */
  426. tx_time_single += (t_slot * cw) >> 1;
  427. cw = min((cw << 1) | 1, mp->cw_max);
  428. tx_time += tx_time_single;
  429. tx_time_cts += tx_time_single + mi->sp_ack_dur;
  430. tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
  431. if ((tx_time_cts < mp->segment_size) &&
  432. (mr->retry_count_cts < mp->max_retry))
  433. mr->retry_count_cts++;
  434. if ((tx_time_rtscts < mp->segment_size) &&
  435. (mrs->retry_count_rtscts < mp->max_retry))
  436. mrs->retry_count_rtscts++;
  437. } while ((tx_time < mp->segment_size) &&
  438. (++mr->stats.retry_count < mp->max_retry));
  439. mr->adjusted_retry_count = mrs->retry_count;
  440. if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
  441. mr->retry_count_cts = mrs->retry_count;
  442. }
  443. for (i = n; i < sband->n_bitrates; i++) {
  444. struct minstrel_rate *mr = &mi->r[i];
  445. mr->rix = -1;
  446. }
  447. mi->n_rates = n;
  448. mi->stats_update = jiffies;
  449. init_sample_table(mi);
  450. minstrel_update_rates(mp, mi);
  451. }
  452. static void *
  453. minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  454. {
  455. struct ieee80211_supported_band *sband;
  456. struct minstrel_sta_info *mi;
  457. struct minstrel_priv *mp = priv;
  458. struct ieee80211_hw *hw = mp->hw;
  459. int max_rates = 0;
  460. int i;
  461. mi = kzalloc(sizeof(struct minstrel_sta_info), gfp);
  462. if (!mi)
  463. return NULL;
  464. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  465. sband = hw->wiphy->bands[i];
  466. if (sband && sband->n_bitrates > max_rates)
  467. max_rates = sband->n_bitrates;
  468. }
  469. mi->r = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
  470. if (!mi->r)
  471. goto error;
  472. mi->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
  473. if (!mi->sample_table)
  474. goto error1;
  475. mi->stats_update = jiffies;
  476. return mi;
  477. error1:
  478. kfree(mi->r);
  479. error:
  480. kfree(mi);
  481. return NULL;
  482. }
  483. static void
  484. minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
  485. {
  486. struct minstrel_sta_info *mi = priv_sta;
  487. kfree(mi->sample_table);
  488. kfree(mi->r);
  489. kfree(mi);
  490. }
  491. static void
  492. minstrel_init_cck_rates(struct minstrel_priv *mp)
  493. {
  494. static const int bitrates[4] = { 10, 20, 55, 110 };
  495. struct ieee80211_supported_band *sband;
  496. u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
  497. int i, j;
  498. sband = mp->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  499. if (!sband)
  500. return;
  501. for (i = 0, j = 0; i < sband->n_bitrates; i++) {
  502. struct ieee80211_rate *rate = &sband->bitrates[i];
  503. if (rate->flags & IEEE80211_RATE_ERP_G)
  504. continue;
  505. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  506. continue;
  507. for (j = 0; j < ARRAY_SIZE(bitrates); j++) {
  508. if (rate->bitrate != bitrates[j])
  509. continue;
  510. mp->cck_rates[j] = i;
  511. break;
  512. }
  513. }
  514. }
  515. static void *
  516. minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  517. {
  518. struct minstrel_priv *mp;
  519. mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
  520. if (!mp)
  521. return NULL;
  522. /* contention window settings
  523. * Just an approximation. Using the per-queue values would complicate
  524. * the calculations and is probably unnecessary */
  525. mp->cw_min = 15;
  526. mp->cw_max = 1023;
  527. /* number of packets (in %) to use for sampling other rates
  528. * sample less often for non-mrr packets, because the overhead
  529. * is much higher than with mrr */
  530. mp->lookaround_rate = 5;
  531. mp->lookaround_rate_mrr = 10;
  532. /* maximum time that the hw is allowed to stay in one MRR segment */
  533. mp->segment_size = 6000;
  534. if (hw->max_rate_tries > 0)
  535. mp->max_retry = hw->max_rate_tries;
  536. else
  537. /* safe default, does not necessarily have to match hw properties */
  538. mp->max_retry = 7;
  539. if (hw->max_rates >= 4)
  540. mp->has_mrr = true;
  541. mp->hw = hw;
  542. mp->update_interval = 100;
  543. #ifdef CONFIG_MAC80211_DEBUGFS
  544. mp->fixed_rate_idx = (u32) -1;
  545. mp->dbg_fixed_rate = debugfs_create_u32("fixed_rate_idx",
  546. S_IRUGO | S_IWUGO, debugfsdir, &mp->fixed_rate_idx);
  547. #endif
  548. minstrel_init_cck_rates(mp);
  549. return mp;
  550. }
  551. static void
  552. minstrel_free(void *priv)
  553. {
  554. #ifdef CONFIG_MAC80211_DEBUGFS
  555. debugfs_remove(((struct minstrel_priv *)priv)->dbg_fixed_rate);
  556. #endif
  557. kfree(priv);
  558. }
  559. static u32 minstrel_get_expected_throughput(void *priv_sta)
  560. {
  561. struct minstrel_sta_info *mi = priv_sta;
  562. int idx = mi->max_tp_rate[0];
  563. /* convert pkt per sec in kbps (1200 is the average pkt size used for
  564. * computing cur_tp
  565. */
  566. return MINSTREL_TRUNC(mi->r[idx].stats.cur_tp) * 1200 * 8 / 1024;
  567. }
  568. const struct rate_control_ops mac80211_minstrel = {
  569. .name = "minstrel",
  570. .tx_status_noskb = minstrel_tx_status,
  571. .get_rate = minstrel_get_rate,
  572. .rate_init = minstrel_rate_init,
  573. .alloc = minstrel_alloc,
  574. .free = minstrel_free,
  575. .alloc_sta = minstrel_alloc_sta,
  576. .free_sta = minstrel_free_sta,
  577. #ifdef CONFIG_MAC80211_DEBUGFS
  578. .add_sta_debugfs = minstrel_add_sta_debugfs,
  579. .remove_sta_debugfs = minstrel_remove_sta_debugfs,
  580. #endif
  581. .get_expected_throughput = minstrel_get_expected_throughput,
  582. };
  583. int __init
  584. rc80211_minstrel_init(void)
  585. {
  586. return ieee80211_rate_control_register(&mac80211_minstrel);
  587. }
  588. void
  589. rc80211_minstrel_exit(void)
  590. {
  591. ieee80211_rate_control_unregister(&mac80211_minstrel);
  592. }