rc80211_minstrel.c 18 KB

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