rate.c 19 KB

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