rate.c 24 KB

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