rate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include "rate.h"
  15. #include "ieee80211_i.h"
  16. #include "debugfs.h"
  17. struct rate_control_alg {
  18. struct list_head list;
  19. const struct rate_control_ops *ops;
  20. };
  21. static LIST_HEAD(rate_ctrl_algs);
  22. static DEFINE_MUTEX(rate_ctrl_mutex);
  23. static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
  24. module_param(ieee80211_default_rc_algo, charp, 0644);
  25. MODULE_PARM_DESC(ieee80211_default_rc_algo,
  26. "Default rate control algorithm for mac80211 to use");
  27. int ieee80211_rate_control_register(const struct rate_control_ops *ops)
  28. {
  29. struct rate_control_alg *alg;
  30. if (!ops->name)
  31. return -EINVAL;
  32. mutex_lock(&rate_ctrl_mutex);
  33. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  34. if (!strcmp(alg->ops->name, ops->name)) {
  35. /* don't register an algorithm twice */
  36. WARN_ON(1);
  37. mutex_unlock(&rate_ctrl_mutex);
  38. return -EALREADY;
  39. }
  40. }
  41. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  42. if (alg == NULL) {
  43. mutex_unlock(&rate_ctrl_mutex);
  44. return -ENOMEM;
  45. }
  46. alg->ops = ops;
  47. list_add_tail(&alg->list, &rate_ctrl_algs);
  48. mutex_unlock(&rate_ctrl_mutex);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(ieee80211_rate_control_register);
  52. void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
  53. {
  54. struct rate_control_alg *alg;
  55. mutex_lock(&rate_ctrl_mutex);
  56. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  57. if (alg->ops == ops) {
  58. list_del(&alg->list);
  59. kfree(alg);
  60. break;
  61. }
  62. }
  63. mutex_unlock(&rate_ctrl_mutex);
  64. }
  65. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  66. static const struct rate_control_ops *
  67. ieee80211_try_rate_control_ops_get(const char *name)
  68. {
  69. struct rate_control_alg *alg;
  70. const struct rate_control_ops *ops = NULL;
  71. if (!name)
  72. return NULL;
  73. mutex_lock(&rate_ctrl_mutex);
  74. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  75. if (!strcmp(alg->ops->name, name)) {
  76. ops = alg->ops;
  77. break;
  78. }
  79. }
  80. mutex_unlock(&rate_ctrl_mutex);
  81. return ops;
  82. }
  83. /* Get the rate control algorithm. */
  84. static const struct rate_control_ops *
  85. ieee80211_rate_control_ops_get(const char *name)
  86. {
  87. const struct rate_control_ops *ops;
  88. const char *alg_name;
  89. kernel_param_lock(THIS_MODULE);
  90. if (!name)
  91. alg_name = ieee80211_default_rc_algo;
  92. else
  93. alg_name = name;
  94. ops = ieee80211_try_rate_control_ops_get(alg_name);
  95. if (!ops && name)
  96. /* try default if specific alg requested but not found */
  97. ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
  98. /* try built-in one if specific alg requested but not found */
  99. if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
  100. ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
  101. kernel_param_unlock(THIS_MODULE);
  102. return ops;
  103. }
  104. #ifdef CONFIG_MAC80211_DEBUGFS
  105. static ssize_t rcname_read(struct file *file, char __user *userbuf,
  106. size_t count, loff_t *ppos)
  107. {
  108. struct rate_control_ref *ref = file->private_data;
  109. int len = strlen(ref->ops->name);
  110. return simple_read_from_buffer(userbuf, count, ppos,
  111. ref->ops->name, len);
  112. }
  113. static const struct file_operations rcname_ops = {
  114. .read = rcname_read,
  115. .open = simple_open,
  116. .llseek = default_llseek,
  117. };
  118. #endif
  119. static struct rate_control_ref *rate_control_alloc(const char *name,
  120. struct ieee80211_local *local)
  121. {
  122. struct dentry *debugfsdir = NULL;
  123. struct rate_control_ref *ref;
  124. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  125. if (!ref)
  126. return NULL;
  127. ref->local = local;
  128. ref->ops = ieee80211_rate_control_ops_get(name);
  129. if (!ref->ops)
  130. goto free;
  131. #ifdef CONFIG_MAC80211_DEBUGFS
  132. debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
  133. local->debugfs.rcdir = debugfsdir;
  134. debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
  135. #endif
  136. ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
  137. if (!ref->priv)
  138. goto free;
  139. return ref;
  140. free:
  141. kfree(ref);
  142. return NULL;
  143. }
  144. static void rate_control_free(struct rate_control_ref *ctrl_ref)
  145. {
  146. ctrl_ref->ops->free(ctrl_ref->priv);
  147. #ifdef CONFIG_MAC80211_DEBUGFS
  148. debugfs_remove_recursive(ctrl_ref->local->debugfs.rcdir);
  149. ctrl_ref->local->debugfs.rcdir = NULL;
  150. #endif
  151. kfree(ctrl_ref);
  152. }
  153. static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
  154. {
  155. struct sk_buff *skb = txrc->skb;
  156. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  157. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  158. __le16 fc;
  159. fc = hdr->frame_control;
  160. return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
  161. IEEE80211_TX_CTL_USE_MINRATE)) ||
  162. !ieee80211_is_data(fc);
  163. }
  164. static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
  165. struct ieee80211_supported_band *sband)
  166. {
  167. u8 i;
  168. if (basic_rates == 0)
  169. return; /* assume basic rates unknown and accept rate */
  170. if (*idx < 0)
  171. return;
  172. if (basic_rates & (1 << *idx))
  173. return; /* selected rate is a basic rate */
  174. for (i = *idx + 1; i <= sband->n_bitrates; i++) {
  175. if (basic_rates & (1 << i)) {
  176. *idx = i;
  177. return;
  178. }
  179. }
  180. /* could not find a basic rate; use original selection */
  181. }
  182. static void __rate_control_send_low(struct ieee80211_hw *hw,
  183. struct ieee80211_supported_band *sband,
  184. struct ieee80211_sta *sta,
  185. struct ieee80211_tx_info *info,
  186. u32 rate_mask)
  187. {
  188. int i;
  189. u32 rate_flags =
  190. ieee80211_chandef_rate_flags(&hw->conf.chandef);
  191. if ((sband->band == IEEE80211_BAND_2GHZ) &&
  192. (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
  193. rate_flags |= IEEE80211_RATE_ERP_G;
  194. info->control.rates[0].idx = 0;
  195. for (i = 0; i < sband->n_bitrates; i++) {
  196. if (!(rate_mask & BIT(i)))
  197. continue;
  198. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  199. continue;
  200. if (!rate_supported(sta, sband->band, i))
  201. continue;
  202. info->control.rates[0].idx = i;
  203. break;
  204. }
  205. WARN_ON_ONCE(i == sband->n_bitrates);
  206. info->control.rates[0].count =
  207. (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  208. 1 : hw->max_rate_tries;
  209. info->control.skip_table = 1;
  210. }
  211. bool rate_control_send_low(struct ieee80211_sta *pubsta,
  212. void *priv_sta,
  213. struct ieee80211_tx_rate_control *txrc)
  214. {
  215. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  216. struct ieee80211_supported_band *sband = txrc->sband;
  217. struct sta_info *sta;
  218. int mcast_rate;
  219. bool use_basicrate = false;
  220. if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
  221. __rate_control_send_low(txrc->hw, sband, pubsta, info,
  222. txrc->rate_idx_mask);
  223. if (!pubsta && txrc->bss) {
  224. mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
  225. if (mcast_rate > 0) {
  226. info->control.rates[0].idx = mcast_rate - 1;
  227. return true;
  228. }
  229. use_basicrate = true;
  230. } else if (pubsta) {
  231. sta = container_of(pubsta, struct sta_info, sta);
  232. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  233. use_basicrate = true;
  234. }
  235. if (use_basicrate)
  236. rc_send_low_basicrate(&info->control.rates[0].idx,
  237. txrc->bss_conf->basic_rates,
  238. sband);
  239. return true;
  240. }
  241. return false;
  242. }
  243. EXPORT_SYMBOL(rate_control_send_low);
  244. static bool rate_idx_match_legacy_mask(struct ieee80211_tx_rate *rate,
  245. int n_bitrates, u32 mask)
  246. {
  247. int j;
  248. /* See whether the selected rate or anything below it is allowed. */
  249. for (j = rate->idx; j >= 0; j--) {
  250. if (mask & (1 << j)) {
  251. /* Okay, found a suitable rate. Use it. */
  252. rate->idx = j;
  253. return true;
  254. }
  255. }
  256. /* Try to find a higher rate that would be allowed */
  257. for (j = rate->idx + 1; j < n_bitrates; j++) {
  258. if (mask & (1 << j)) {
  259. /* Okay, found a suitable rate. Use it. */
  260. rate->idx = j;
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. static bool rate_idx_match_mcs_mask(struct ieee80211_tx_rate *rate,
  267. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  268. {
  269. int i, j;
  270. int ridx, rbit;
  271. ridx = rate->idx / 8;
  272. rbit = rate->idx % 8;
  273. /* sanity check */
  274. if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
  275. return false;
  276. /* See whether the selected rate or anything below it is allowed. */
  277. for (i = ridx; i >= 0; i--) {
  278. for (j = rbit; j >= 0; j--)
  279. if (mcs_mask[i] & BIT(j)) {
  280. rate->idx = i * 8 + j;
  281. return true;
  282. }
  283. rbit = 7;
  284. }
  285. /* Try to find a higher rate that would be allowed */
  286. ridx = (rate->idx + 1) / 8;
  287. rbit = (rate->idx + 1) % 8;
  288. for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  289. for (j = rbit; j < 8; j++)
  290. if (mcs_mask[i] & BIT(j)) {
  291. rate->idx = i * 8 + j;
  292. return true;
  293. }
  294. rbit = 0;
  295. }
  296. return false;
  297. }
  298. static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
  299. struct ieee80211_supported_band *sband,
  300. enum nl80211_chan_width chan_width,
  301. u32 mask,
  302. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  303. {
  304. struct ieee80211_tx_rate alt_rate;
  305. /* handle HT rates */
  306. if (rate->flags & IEEE80211_TX_RC_MCS) {
  307. if (rate_idx_match_mcs_mask(rate, mcs_mask))
  308. return;
  309. /* also try the legacy rates. */
  310. alt_rate.idx = 0;
  311. /* keep protection flags */
  312. alt_rate.flags = rate->flags &
  313. (IEEE80211_TX_RC_USE_RTS_CTS |
  314. IEEE80211_TX_RC_USE_CTS_PROTECT |
  315. IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  316. alt_rate.count = rate->count;
  317. if (rate_idx_match_legacy_mask(&alt_rate,
  318. sband->n_bitrates, mask)) {
  319. *rate = alt_rate;
  320. return;
  321. }
  322. } else if (!(rate->flags & IEEE80211_TX_RC_VHT_MCS)) {
  323. /* handle legacy rates */
  324. if (rate_idx_match_legacy_mask(rate, sband->n_bitrates, mask))
  325. return;
  326. /* if HT BSS, and we handle a data frame, also try HT rates */
  327. switch (chan_width) {
  328. case NL80211_CHAN_WIDTH_20_NOHT:
  329. case NL80211_CHAN_WIDTH_5:
  330. case NL80211_CHAN_WIDTH_10:
  331. return;
  332. default:
  333. break;
  334. }
  335. alt_rate.idx = 0;
  336. /* keep protection flags */
  337. alt_rate.flags = rate->flags &
  338. (IEEE80211_TX_RC_USE_RTS_CTS |
  339. IEEE80211_TX_RC_USE_CTS_PROTECT |
  340. IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  341. alt_rate.count = rate->count;
  342. alt_rate.flags |= IEEE80211_TX_RC_MCS;
  343. if (chan_width == NL80211_CHAN_WIDTH_40)
  344. alt_rate.flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  345. if (rate_idx_match_mcs_mask(&alt_rate, mcs_mask)) {
  346. *rate = alt_rate;
  347. return;
  348. }
  349. }
  350. /*
  351. * Uh.. No suitable rate exists. This should not really happen with
  352. * sane TX rate mask configurations. However, should someone manage to
  353. * configure supported rates and TX rate mask in incompatible way,
  354. * allow the frame to be transmitted with whatever the rate control
  355. * selected.
  356. */
  357. }
  358. static void rate_fixup_ratelist(struct ieee80211_vif *vif,
  359. struct ieee80211_supported_band *sband,
  360. struct ieee80211_tx_info *info,
  361. struct ieee80211_tx_rate *rates,
  362. int max_rates)
  363. {
  364. struct ieee80211_rate *rate;
  365. bool inval = false;
  366. int i;
  367. /*
  368. * Set up the RTS/CTS rate as the fastest basic rate
  369. * that is not faster than the data rate unless there
  370. * is no basic rate slower than the data rate, in which
  371. * case we pick the slowest basic rate
  372. *
  373. * XXX: Should this check all retry rates?
  374. */
  375. if (!(rates[0].flags &
  376. (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
  377. u32 basic_rates = vif->bss_conf.basic_rates;
  378. s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
  379. rate = &sband->bitrates[rates[0].idx];
  380. for (i = 0; i < sband->n_bitrates; i++) {
  381. /* must be a basic rate */
  382. if (!(basic_rates & BIT(i)))
  383. continue;
  384. /* must not be faster than the data rate */
  385. if (sband->bitrates[i].bitrate > rate->bitrate)
  386. continue;
  387. /* maximum */
  388. if (sband->bitrates[baserate].bitrate <
  389. sband->bitrates[i].bitrate)
  390. baserate = i;
  391. }
  392. info->control.rts_cts_rate_idx = baserate;
  393. }
  394. for (i = 0; i < max_rates; i++) {
  395. /*
  396. * make sure there's no valid rate following
  397. * an invalid one, just in case drivers don't
  398. * take the API seriously to stop at -1.
  399. */
  400. if (inval) {
  401. rates[i].idx = -1;
  402. continue;
  403. }
  404. if (rates[i].idx < 0) {
  405. inval = true;
  406. continue;
  407. }
  408. /*
  409. * For now assume MCS is already set up correctly, this
  410. * needs to be fixed.
  411. */
  412. if (rates[i].flags & IEEE80211_TX_RC_MCS) {
  413. WARN_ON(rates[i].idx > 76);
  414. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  415. info->control.use_cts_prot)
  416. rates[i].flags |=
  417. IEEE80211_TX_RC_USE_CTS_PROTECT;
  418. continue;
  419. }
  420. if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
  421. WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
  422. continue;
  423. }
  424. /* set up RTS protection if desired */
  425. if (info->control.use_rts) {
  426. rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  427. info->control.use_cts_prot = false;
  428. }
  429. /* RC is busted */
  430. if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
  431. rates[i].idx = -1;
  432. continue;
  433. }
  434. rate = &sband->bitrates[rates[i].idx];
  435. /* set up short preamble */
  436. if (info->control.short_preamble &&
  437. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  438. rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  439. /* set up G protection */
  440. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  441. info->control.use_cts_prot &&
  442. rate->flags & IEEE80211_RATE_ERP_G)
  443. rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
  444. }
  445. }
  446. static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
  447. struct ieee80211_tx_info *info,
  448. struct ieee80211_tx_rate *rates,
  449. int max_rates)
  450. {
  451. struct ieee80211_sta_rates *ratetbl = NULL;
  452. int i;
  453. if (sta && !info->control.skip_table)
  454. ratetbl = rcu_dereference(sta->rates);
  455. /* Fill remaining rate slots with data from the sta rate table. */
  456. max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
  457. for (i = 0; i < max_rates; i++) {
  458. if (i < ARRAY_SIZE(info->control.rates) &&
  459. info->control.rates[i].idx >= 0 &&
  460. info->control.rates[i].count) {
  461. if (rates != info->control.rates)
  462. rates[i] = info->control.rates[i];
  463. } else if (ratetbl) {
  464. rates[i].idx = ratetbl->rate[i].idx;
  465. rates[i].flags = ratetbl->rate[i].flags;
  466. if (info->control.use_rts)
  467. rates[i].count = ratetbl->rate[i].count_rts;
  468. else if (info->control.use_cts_prot)
  469. rates[i].count = ratetbl->rate[i].count_cts;
  470. else
  471. rates[i].count = ratetbl->rate[i].count;
  472. } else {
  473. rates[i].idx = -1;
  474. rates[i].count = 0;
  475. }
  476. if (rates[i].idx < 0 || !rates[i].count)
  477. break;
  478. }
  479. }
  480. static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
  481. struct ieee80211_sta *sta,
  482. struct ieee80211_supported_band *sband,
  483. struct ieee80211_tx_info *info,
  484. struct ieee80211_tx_rate *rates,
  485. int max_rates)
  486. {
  487. enum nl80211_chan_width chan_width;
  488. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
  489. bool has_mcs_mask;
  490. u32 mask;
  491. u32 rate_flags;
  492. int i;
  493. /*
  494. * Try to enforce the rateidx mask the user wanted. skip this if the
  495. * default mask (allow all rates) is used to save some processing for
  496. * the common case.
  497. */
  498. mask = sdata->rc_rateidx_mask[info->band];
  499. has_mcs_mask = sdata->rc_has_mcs_mask[info->band];
  500. rate_flags =
  501. ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  502. for (i = 0; i < sband->n_bitrates; i++)
  503. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  504. mask &= ~BIT(i);
  505. if (mask == (1 << sband->n_bitrates) - 1 && !has_mcs_mask)
  506. return;
  507. if (has_mcs_mask)
  508. memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
  509. sizeof(mcs_mask));
  510. else
  511. memset(mcs_mask, 0xff, sizeof(mcs_mask));
  512. if (sta) {
  513. /* Filter out rates that the STA does not support */
  514. mask &= sta->supp_rates[info->band];
  515. for (i = 0; i < sizeof(mcs_mask); i++)
  516. mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
  517. }
  518. /*
  519. * Make sure the rate index selected for each TX rate is
  520. * included in the configured mask and change the rate indexes
  521. * if needed.
  522. */
  523. chan_width = sdata->vif.bss_conf.chandef.width;
  524. for (i = 0; i < max_rates; i++) {
  525. /* Skip invalid rates */
  526. if (rates[i].idx < 0)
  527. break;
  528. rate_idx_match_mask(&rates[i], sband, chan_width, mask,
  529. mcs_mask);
  530. }
  531. }
  532. void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
  533. struct ieee80211_sta *sta,
  534. struct sk_buff *skb,
  535. struct ieee80211_tx_rate *dest,
  536. int max_rates)
  537. {
  538. struct ieee80211_sub_if_data *sdata;
  539. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  540. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  541. struct ieee80211_supported_band *sband;
  542. rate_control_fill_sta_table(sta, info, dest, max_rates);
  543. if (!vif)
  544. return;
  545. sdata = vif_to_sdata(vif);
  546. sband = sdata->local->hw.wiphy->bands[info->band];
  547. if (ieee80211_is_data(hdr->frame_control))
  548. rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates);
  549. if (dest[0].idx < 0)
  550. __rate_control_send_low(&sdata->local->hw, sband, sta, info,
  551. sdata->rc_rateidx_mask[info->band]);
  552. if (sta)
  553. rate_fixup_ratelist(vif, sband, info, dest, max_rates);
  554. }
  555. EXPORT_SYMBOL(ieee80211_get_tx_rates);
  556. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  557. struct sta_info *sta,
  558. struct ieee80211_tx_rate_control *txrc)
  559. {
  560. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  561. void *priv_sta = NULL;
  562. struct ieee80211_sta *ista = NULL;
  563. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  564. int i;
  565. if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
  566. ista = &sta->sta;
  567. priv_sta = sta->rate_ctrl_priv;
  568. }
  569. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  570. info->control.rates[i].idx = -1;
  571. info->control.rates[i].flags = 0;
  572. info->control.rates[i].count = 0;
  573. }
  574. if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
  575. return;
  576. if (ista) {
  577. spin_lock_bh(&sta->rate_ctrl_lock);
  578. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  579. spin_unlock_bh(&sta->rate_ctrl_lock);
  580. } else {
  581. ref->ops->get_rate(ref->priv, NULL, NULL, txrc);
  582. }
  583. if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
  584. return;
  585. ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
  586. info->control.rates,
  587. ARRAY_SIZE(info->control.rates));
  588. }
  589. int rate_control_set_rates(struct ieee80211_hw *hw,
  590. struct ieee80211_sta *pubsta,
  591. struct ieee80211_sta_rates *rates)
  592. {
  593. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  594. struct ieee80211_sta_rates *old;
  595. /*
  596. * mac80211 guarantees that this function will not be called
  597. * concurrently, so the following RCU access is safe, even without
  598. * extra locking. This can not be checked easily, so we just set
  599. * the condition to true.
  600. */
  601. old = rcu_dereference_protected(pubsta->rates, true);
  602. rcu_assign_pointer(pubsta->rates, rates);
  603. if (old)
  604. kfree_rcu(old, rcu_head);
  605. drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
  606. return 0;
  607. }
  608. EXPORT_SYMBOL(rate_control_set_rates);
  609. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  610. const char *name)
  611. {
  612. struct rate_control_ref *ref;
  613. ASSERT_RTNL();
  614. if (local->open_count)
  615. return -EBUSY;
  616. if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
  617. if (WARN_ON(!local->ops->set_rts_threshold))
  618. return -EINVAL;
  619. return 0;
  620. }
  621. ref = rate_control_alloc(name, local);
  622. if (!ref) {
  623. wiphy_warn(local->hw.wiphy,
  624. "Failed to select rate control algorithm\n");
  625. return -ENOENT;
  626. }
  627. WARN_ON(local->rate_ctrl);
  628. local->rate_ctrl = ref;
  629. wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
  630. ref->ops->name);
  631. return 0;
  632. }
  633. void rate_control_deinitialize(struct ieee80211_local *local)
  634. {
  635. struct rate_control_ref *ref;
  636. ref = local->rate_ctrl;
  637. if (!ref)
  638. return;
  639. local->rate_ctrl = NULL;
  640. rate_control_free(ref);
  641. }