rate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. kparam_block_sysfs_write(ieee80211_default_rc_algo);
  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. kparam_unblock_sysfs_write(ieee80211_default_rc_algo);
  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 {
  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 & IEEE80211_TX_RC_MCS)) {
  376. u32 basic_rates = vif->bss_conf.basic_rates;
  377. s8 baserate = basic_rates ? ffs(basic_rates - 1) : 0;
  378. rate = &sband->bitrates[rates[0].idx];
  379. for (i = 0; i < sband->n_bitrates; i++) {
  380. /* must be a basic rate */
  381. if (!(basic_rates & BIT(i)))
  382. continue;
  383. /* must not be faster than the data rate */
  384. if (sband->bitrates[i].bitrate > rate->bitrate)
  385. continue;
  386. /* maximum */
  387. if (sband->bitrates[baserate].bitrate <
  388. sband->bitrates[i].bitrate)
  389. baserate = i;
  390. }
  391. info->control.rts_cts_rate_idx = baserate;
  392. }
  393. for (i = 0; i < max_rates; i++) {
  394. /*
  395. * make sure there's no valid rate following
  396. * an invalid one, just in case drivers don't
  397. * take the API seriously to stop at -1.
  398. */
  399. if (inval) {
  400. rates[i].idx = -1;
  401. continue;
  402. }
  403. if (rates[i].idx < 0) {
  404. inval = true;
  405. continue;
  406. }
  407. /*
  408. * For now assume MCS is already set up correctly, this
  409. * needs to be fixed.
  410. */
  411. if (rates[i].flags & IEEE80211_TX_RC_MCS) {
  412. WARN_ON(rates[i].idx > 76);
  413. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  414. info->control.use_cts_prot)
  415. rates[i].flags |=
  416. IEEE80211_TX_RC_USE_CTS_PROTECT;
  417. continue;
  418. }
  419. if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
  420. WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
  421. continue;
  422. }
  423. /* set up RTS protection if desired */
  424. if (info->control.use_rts) {
  425. rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  426. info->control.use_cts_prot = false;
  427. }
  428. /* RC is busted */
  429. if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
  430. rates[i].idx = -1;
  431. continue;
  432. }
  433. rate = &sband->bitrates[rates[i].idx];
  434. /* set up short preamble */
  435. if (info->control.short_preamble &&
  436. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  437. rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  438. /* set up G protection */
  439. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  440. info->control.use_cts_prot &&
  441. rate->flags & IEEE80211_RATE_ERP_G)
  442. rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
  443. }
  444. }
  445. static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
  446. struct ieee80211_tx_info *info,
  447. struct ieee80211_tx_rate *rates,
  448. int max_rates)
  449. {
  450. struct ieee80211_sta_rates *ratetbl = NULL;
  451. int i;
  452. if (sta && !info->control.skip_table)
  453. ratetbl = rcu_dereference(sta->rates);
  454. /* Fill remaining rate slots with data from the sta rate table. */
  455. max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
  456. for (i = 0; i < max_rates; i++) {
  457. if (i < ARRAY_SIZE(info->control.rates) &&
  458. info->control.rates[i].idx >= 0 &&
  459. info->control.rates[i].count) {
  460. if (rates != info->control.rates)
  461. rates[i] = info->control.rates[i];
  462. } else if (ratetbl) {
  463. rates[i].idx = ratetbl->rate[i].idx;
  464. rates[i].flags = ratetbl->rate[i].flags;
  465. if (info->control.use_rts)
  466. rates[i].count = ratetbl->rate[i].count_rts;
  467. else if (info->control.use_cts_prot)
  468. rates[i].count = ratetbl->rate[i].count_cts;
  469. else
  470. rates[i].count = ratetbl->rate[i].count;
  471. } else {
  472. rates[i].idx = -1;
  473. rates[i].count = 0;
  474. }
  475. if (rates[i].idx < 0 || !rates[i].count)
  476. break;
  477. }
  478. }
  479. static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
  480. struct ieee80211_sta *sta,
  481. struct ieee80211_supported_band *sband,
  482. struct ieee80211_tx_info *info,
  483. struct ieee80211_tx_rate *rates,
  484. int max_rates)
  485. {
  486. enum nl80211_chan_width chan_width;
  487. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
  488. bool has_mcs_mask;
  489. u32 mask;
  490. u32 rate_flags;
  491. int i;
  492. /*
  493. * Try to enforce the rateidx mask the user wanted. skip this if the
  494. * default mask (allow all rates) is used to save some processing for
  495. * the common case.
  496. */
  497. mask = sdata->rc_rateidx_mask[info->band];
  498. has_mcs_mask = sdata->rc_has_mcs_mask[info->band];
  499. rate_flags =
  500. ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  501. for (i = 0; i < sband->n_bitrates; i++)
  502. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  503. mask &= ~BIT(i);
  504. if (mask == (1 << sband->n_bitrates) - 1 && !has_mcs_mask)
  505. return;
  506. if (has_mcs_mask)
  507. memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
  508. sizeof(mcs_mask));
  509. else
  510. memset(mcs_mask, 0xff, sizeof(mcs_mask));
  511. if (sta) {
  512. /* Filter out rates that the STA does not support */
  513. mask &= sta->supp_rates[info->band];
  514. for (i = 0; i < sizeof(mcs_mask); i++)
  515. mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
  516. }
  517. /*
  518. * Make sure the rate index selected for each TX rate is
  519. * included in the configured mask and change the rate indexes
  520. * if needed.
  521. */
  522. chan_width = sdata->vif.bss_conf.chandef.width;
  523. for (i = 0; i < max_rates; i++) {
  524. /* Skip invalid rates */
  525. if (rates[i].idx < 0)
  526. break;
  527. rate_idx_match_mask(&rates[i], sband, chan_width, mask,
  528. mcs_mask);
  529. }
  530. }
  531. void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
  532. struct ieee80211_sta *sta,
  533. struct sk_buff *skb,
  534. struct ieee80211_tx_rate *dest,
  535. int max_rates)
  536. {
  537. struct ieee80211_sub_if_data *sdata;
  538. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  539. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  540. struct ieee80211_supported_band *sband;
  541. rate_control_fill_sta_table(sta, info, dest, max_rates);
  542. if (!vif)
  543. return;
  544. sdata = vif_to_sdata(vif);
  545. sband = sdata->local->hw.wiphy->bands[info->band];
  546. if (ieee80211_is_data(hdr->frame_control))
  547. rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates);
  548. if (dest[0].idx < 0)
  549. __rate_control_send_low(&sdata->local->hw, sband, sta, info,
  550. sdata->rc_rateidx_mask[info->band]);
  551. if (sta)
  552. rate_fixup_ratelist(vif, sband, info, dest, max_rates);
  553. }
  554. EXPORT_SYMBOL(ieee80211_get_tx_rates);
  555. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  556. struct sta_info *sta,
  557. struct ieee80211_tx_rate_control *txrc)
  558. {
  559. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  560. void *priv_sta = NULL;
  561. struct ieee80211_sta *ista = NULL;
  562. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  563. int i;
  564. if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
  565. ista = &sta->sta;
  566. priv_sta = sta->rate_ctrl_priv;
  567. }
  568. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  569. info->control.rates[i].idx = -1;
  570. info->control.rates[i].flags = 0;
  571. info->control.rates[i].count = 0;
  572. }
  573. if (sdata->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  574. return;
  575. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  576. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
  577. return;
  578. ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
  579. info->control.rates,
  580. ARRAY_SIZE(info->control.rates));
  581. }
  582. int rate_control_set_rates(struct ieee80211_hw *hw,
  583. struct ieee80211_sta *pubsta,
  584. struct ieee80211_sta_rates *rates)
  585. {
  586. struct ieee80211_sta_rates *old;
  587. /*
  588. * mac80211 guarantees that this function will not be called
  589. * concurrently, so the following RCU access is safe, even without
  590. * extra locking. This can not be checked easily, so we just set
  591. * the condition to true.
  592. */
  593. old = rcu_dereference_protected(pubsta->rates, true);
  594. rcu_assign_pointer(pubsta->rates, rates);
  595. if (old)
  596. kfree_rcu(old, rcu_head);
  597. return 0;
  598. }
  599. EXPORT_SYMBOL(rate_control_set_rates);
  600. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  601. const char *name)
  602. {
  603. struct rate_control_ref *ref;
  604. ASSERT_RTNL();
  605. if (local->open_count)
  606. return -EBUSY;
  607. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
  608. if (WARN_ON(!local->ops->set_rts_threshold))
  609. return -EINVAL;
  610. return 0;
  611. }
  612. ref = rate_control_alloc(name, local);
  613. if (!ref) {
  614. wiphy_warn(local->hw.wiphy,
  615. "Failed to select rate control algorithm\n");
  616. return -ENOENT;
  617. }
  618. WARN_ON(local->rate_ctrl);
  619. local->rate_ctrl = ref;
  620. wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
  621. ref->ops->name);
  622. return 0;
  623. }
  624. void rate_control_deinitialize(struct ieee80211_local *local)
  625. {
  626. struct rate_control_ref *ref;
  627. ref = local->rate_ctrl;
  628. if (!ref)
  629. return;
  630. local->rate_ctrl = NULL;
  631. rate_control_free(ref);
  632. }