rate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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_ONCE(i == sband->n_bitrates,
  254. "no supported rates (0x%x) in rate_mask 0x%x with flags 0x%x\n",
  255. sta ? sta->supp_rates[sband->band] : -1,
  256. rate_mask, rate_flags);
  257. info->control.rates[0].count =
  258. (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  259. 1 : hw->max_rate_tries;
  260. info->control.skip_table = 1;
  261. }
  262. bool rate_control_send_low(struct ieee80211_sta *pubsta,
  263. void *priv_sta,
  264. struct ieee80211_tx_rate_control *txrc)
  265. {
  266. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  267. struct ieee80211_supported_band *sband = txrc->sband;
  268. struct sta_info *sta;
  269. int mcast_rate;
  270. bool use_basicrate = false;
  271. if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
  272. __rate_control_send_low(txrc->hw, sband, pubsta, info,
  273. txrc->rate_idx_mask);
  274. if (!pubsta && txrc->bss) {
  275. mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
  276. if (mcast_rate > 0) {
  277. info->control.rates[0].idx = mcast_rate - 1;
  278. return true;
  279. }
  280. use_basicrate = true;
  281. } else if (pubsta) {
  282. sta = container_of(pubsta, struct sta_info, sta);
  283. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  284. use_basicrate = true;
  285. }
  286. if (use_basicrate)
  287. rc_send_low_basicrate(&info->control.rates[0].idx,
  288. txrc->bss_conf->basic_rates,
  289. sband);
  290. return true;
  291. }
  292. return false;
  293. }
  294. EXPORT_SYMBOL(rate_control_send_low);
  295. static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
  296. {
  297. int j;
  298. /* See whether the selected rate or anything below it is allowed. */
  299. for (j = *rate_idx; j >= 0; j--) {
  300. if (mask & (1 << j)) {
  301. /* Okay, found a suitable rate. Use it. */
  302. *rate_idx = j;
  303. return true;
  304. }
  305. }
  306. /* Try to find a higher rate that would be allowed */
  307. for (j = *rate_idx + 1; j < n_bitrates; j++) {
  308. if (mask & (1 << j)) {
  309. /* Okay, found a suitable rate. Use it. */
  310. *rate_idx = j;
  311. return true;
  312. }
  313. }
  314. return false;
  315. }
  316. static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
  317. {
  318. int i, j;
  319. int ridx, rbit;
  320. ridx = *rate_idx / 8;
  321. rbit = *rate_idx % 8;
  322. /* sanity check */
  323. if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
  324. return false;
  325. /* See whether the selected rate or anything below it is allowed. */
  326. for (i = ridx; i >= 0; i--) {
  327. for (j = rbit; j >= 0; j--)
  328. if (mcs_mask[i] & BIT(j)) {
  329. *rate_idx = i * 8 + j;
  330. return true;
  331. }
  332. rbit = 7;
  333. }
  334. /* Try to find a higher rate that would be allowed */
  335. ridx = (*rate_idx + 1) / 8;
  336. rbit = (*rate_idx + 1) % 8;
  337. for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  338. for (j = rbit; j < 8; j++)
  339. if (mcs_mask[i] & BIT(j)) {
  340. *rate_idx = i * 8 + j;
  341. return true;
  342. }
  343. rbit = 0;
  344. }
  345. return false;
  346. }
  347. static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
  348. {
  349. int i, j;
  350. int ridx, rbit;
  351. ridx = *rate_idx >> 4;
  352. rbit = *rate_idx & 0xf;
  353. if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
  354. return false;
  355. /* See whether the selected rate or anything below it is allowed. */
  356. for (i = ridx; i >= 0; i--) {
  357. for (j = rbit; j >= 0; j--) {
  358. if (vht_mask[i] & BIT(j)) {
  359. *rate_idx = (i << 4) | j;
  360. return true;
  361. }
  362. }
  363. rbit = 15;
  364. }
  365. /* Try to find a higher rate that would be allowed */
  366. ridx = (*rate_idx + 1) >> 4;
  367. rbit = (*rate_idx + 1) & 0xf;
  368. for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
  369. for (j = rbit; j < 16; j++) {
  370. if (vht_mask[i] & BIT(j)) {
  371. *rate_idx = (i << 4) | j;
  372. return true;
  373. }
  374. }
  375. rbit = 0;
  376. }
  377. return false;
  378. }
  379. static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
  380. struct ieee80211_supported_band *sband,
  381. enum nl80211_chan_width chan_width,
  382. u32 mask,
  383. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
  384. u16 vht_mask[NL80211_VHT_NSS_MAX])
  385. {
  386. if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
  387. /* handle VHT rates */
  388. if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
  389. return;
  390. *rate_idx = 0;
  391. /* keep protection flags */
  392. *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
  393. IEEE80211_TX_RC_USE_CTS_PROTECT |
  394. IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  395. *rate_flags |= IEEE80211_TX_RC_MCS;
  396. if (chan_width == NL80211_CHAN_WIDTH_40)
  397. *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  398. if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
  399. return;
  400. /* also try the legacy rates. */
  401. *rate_flags &= ~(IEEE80211_TX_RC_MCS |
  402. IEEE80211_TX_RC_40_MHZ_WIDTH);
  403. if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
  404. mask))
  405. return;
  406. } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
  407. /* handle HT rates */
  408. if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
  409. return;
  410. /* also try the legacy rates. */
  411. *rate_idx = 0;
  412. /* keep protection flags */
  413. *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
  414. IEEE80211_TX_RC_USE_CTS_PROTECT |
  415. IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  416. if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
  417. mask))
  418. return;
  419. } else {
  420. /* handle legacy rates */
  421. if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
  422. mask))
  423. return;
  424. /* if HT BSS, and we handle a data frame, also try HT rates */
  425. switch (chan_width) {
  426. case NL80211_CHAN_WIDTH_20_NOHT:
  427. case NL80211_CHAN_WIDTH_5:
  428. case NL80211_CHAN_WIDTH_10:
  429. return;
  430. default:
  431. break;
  432. }
  433. *rate_idx = 0;
  434. /* keep protection flags */
  435. *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
  436. IEEE80211_TX_RC_USE_CTS_PROTECT |
  437. IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  438. *rate_flags |= IEEE80211_TX_RC_MCS;
  439. if (chan_width == NL80211_CHAN_WIDTH_40)
  440. *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  441. if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
  442. return;
  443. }
  444. /*
  445. * Uh.. No suitable rate exists. This should not really happen with
  446. * sane TX rate mask configurations. However, should someone manage to
  447. * configure supported rates and TX rate mask in incompatible way,
  448. * allow the frame to be transmitted with whatever the rate control
  449. * selected.
  450. */
  451. }
  452. static void rate_fixup_ratelist(struct ieee80211_vif *vif,
  453. struct ieee80211_supported_band *sband,
  454. struct ieee80211_tx_info *info,
  455. struct ieee80211_tx_rate *rates,
  456. int max_rates)
  457. {
  458. struct ieee80211_rate *rate;
  459. bool inval = false;
  460. int i;
  461. /*
  462. * Set up the RTS/CTS rate as the fastest basic rate
  463. * that is not faster than the data rate unless there
  464. * is no basic rate slower than the data rate, in which
  465. * case we pick the slowest basic rate
  466. *
  467. * XXX: Should this check all retry rates?
  468. */
  469. if (!(rates[0].flags &
  470. (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
  471. u32 basic_rates = vif->bss_conf.basic_rates;
  472. s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
  473. rate = &sband->bitrates[rates[0].idx];
  474. for (i = 0; i < sband->n_bitrates; i++) {
  475. /* must be a basic rate */
  476. if (!(basic_rates & BIT(i)))
  477. continue;
  478. /* must not be faster than the data rate */
  479. if (sband->bitrates[i].bitrate > rate->bitrate)
  480. continue;
  481. /* maximum */
  482. if (sband->bitrates[baserate].bitrate <
  483. sband->bitrates[i].bitrate)
  484. baserate = i;
  485. }
  486. info->control.rts_cts_rate_idx = baserate;
  487. }
  488. for (i = 0; i < max_rates; i++) {
  489. /*
  490. * make sure there's no valid rate following
  491. * an invalid one, just in case drivers don't
  492. * take the API seriously to stop at -1.
  493. */
  494. if (inval) {
  495. rates[i].idx = -1;
  496. continue;
  497. }
  498. if (rates[i].idx < 0) {
  499. inval = true;
  500. continue;
  501. }
  502. /*
  503. * For now assume MCS is already set up correctly, this
  504. * needs to be fixed.
  505. */
  506. if (rates[i].flags & IEEE80211_TX_RC_MCS) {
  507. WARN_ON(rates[i].idx > 76);
  508. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  509. info->control.use_cts_prot)
  510. rates[i].flags |=
  511. IEEE80211_TX_RC_USE_CTS_PROTECT;
  512. continue;
  513. }
  514. if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
  515. WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
  516. continue;
  517. }
  518. /* set up RTS protection if desired */
  519. if (info->control.use_rts) {
  520. rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  521. info->control.use_cts_prot = false;
  522. }
  523. /* RC is busted */
  524. if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
  525. rates[i].idx = -1;
  526. continue;
  527. }
  528. rate = &sband->bitrates[rates[i].idx];
  529. /* set up short preamble */
  530. if (info->control.short_preamble &&
  531. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  532. rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  533. /* set up G protection */
  534. if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
  535. info->control.use_cts_prot &&
  536. rate->flags & IEEE80211_RATE_ERP_G)
  537. rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
  538. }
  539. }
  540. static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
  541. struct ieee80211_tx_info *info,
  542. struct ieee80211_tx_rate *rates,
  543. int max_rates)
  544. {
  545. struct ieee80211_sta_rates *ratetbl = NULL;
  546. int i;
  547. if (sta && !info->control.skip_table)
  548. ratetbl = rcu_dereference(sta->rates);
  549. /* Fill remaining rate slots with data from the sta rate table. */
  550. max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
  551. for (i = 0; i < max_rates; i++) {
  552. if (i < ARRAY_SIZE(info->control.rates) &&
  553. info->control.rates[i].idx >= 0 &&
  554. info->control.rates[i].count) {
  555. if (rates != info->control.rates)
  556. rates[i] = info->control.rates[i];
  557. } else if (ratetbl) {
  558. rates[i].idx = ratetbl->rate[i].idx;
  559. rates[i].flags = ratetbl->rate[i].flags;
  560. if (info->control.use_rts)
  561. rates[i].count = ratetbl->rate[i].count_rts;
  562. else if (info->control.use_cts_prot)
  563. rates[i].count = ratetbl->rate[i].count_cts;
  564. else
  565. rates[i].count = ratetbl->rate[i].count;
  566. } else {
  567. rates[i].idx = -1;
  568. rates[i].count = 0;
  569. }
  570. if (rates[i].idx < 0 || !rates[i].count)
  571. break;
  572. }
  573. }
  574. static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
  575. struct ieee80211_supported_band *sband,
  576. struct ieee80211_sta *sta, u32 *mask,
  577. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
  578. u16 vht_mask[NL80211_VHT_NSS_MAX])
  579. {
  580. u32 i, flags;
  581. *mask = sdata->rc_rateidx_mask[sband->band];
  582. flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  583. for (i = 0; i < sband->n_bitrates; i++) {
  584. if ((flags & sband->bitrates[i].flags) != flags)
  585. *mask &= ~BIT(i);
  586. }
  587. if (*mask == (1 << sband->n_bitrates) - 1 &&
  588. !sdata->rc_has_mcs_mask[sband->band] &&
  589. !sdata->rc_has_vht_mcs_mask[sband->band])
  590. return false;
  591. if (sdata->rc_has_mcs_mask[sband->band])
  592. memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
  593. IEEE80211_HT_MCS_MASK_LEN);
  594. else
  595. memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
  596. if (sdata->rc_has_vht_mcs_mask[sband->band])
  597. memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
  598. sizeof(u16) * NL80211_VHT_NSS_MAX);
  599. else
  600. memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
  601. if (sta) {
  602. __le16 sta_vht_cap;
  603. u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
  604. /* Filter out rates that the STA does not support */
  605. *mask &= sta->supp_rates[sband->band];
  606. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  607. mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
  608. sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
  609. ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
  610. for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
  611. vht_mask[i] &= sta_vht_mask[i];
  612. }
  613. return true;
  614. }
  615. static void
  616. rate_control_apply_mask_ratetbl(struct sta_info *sta,
  617. struct ieee80211_supported_band *sband,
  618. struct ieee80211_sta_rates *rates)
  619. {
  620. int i;
  621. u32 mask;
  622. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
  623. u16 vht_mask[NL80211_VHT_NSS_MAX];
  624. enum nl80211_chan_width chan_width;
  625. if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
  626. mcs_mask, vht_mask))
  627. return;
  628. chan_width = sta->sdata->vif.bss_conf.chandef.width;
  629. for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
  630. if (rates->rate[i].idx < 0)
  631. break;
  632. rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
  633. sband, chan_width, mask, mcs_mask,
  634. vht_mask);
  635. }
  636. }
  637. static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
  638. struct ieee80211_sta *sta,
  639. struct ieee80211_supported_band *sband,
  640. struct ieee80211_tx_rate *rates,
  641. int max_rates)
  642. {
  643. enum nl80211_chan_width chan_width;
  644. u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
  645. u32 mask;
  646. u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
  647. int i;
  648. /*
  649. * Try to enforce the rateidx mask the user wanted. skip this if the
  650. * default mask (allow all rates) is used to save some processing for
  651. * the common case.
  652. */
  653. if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
  654. vht_mask))
  655. return;
  656. /*
  657. * Make sure the rate index selected for each TX rate is
  658. * included in the configured mask and change the rate indexes
  659. * if needed.
  660. */
  661. chan_width = sdata->vif.bss_conf.chandef.width;
  662. for (i = 0; i < max_rates; i++) {
  663. /* Skip invalid rates */
  664. if (rates[i].idx < 0)
  665. break;
  666. rate_flags = rates[i].flags;
  667. rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
  668. chan_width, mask, mcs_mask, vht_mask);
  669. rates[i].flags = rate_flags;
  670. }
  671. }
  672. void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
  673. struct ieee80211_sta *sta,
  674. struct sk_buff *skb,
  675. struct ieee80211_tx_rate *dest,
  676. int max_rates)
  677. {
  678. struct ieee80211_sub_if_data *sdata;
  679. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  680. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  681. struct ieee80211_supported_band *sband;
  682. rate_control_fill_sta_table(sta, info, dest, max_rates);
  683. if (!vif)
  684. return;
  685. sdata = vif_to_sdata(vif);
  686. sband = sdata->local->hw.wiphy->bands[info->band];
  687. if (ieee80211_is_data(hdr->frame_control))
  688. rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
  689. if (dest[0].idx < 0)
  690. __rate_control_send_low(&sdata->local->hw, sband, sta, info,
  691. sdata->rc_rateidx_mask[info->band]);
  692. if (sta)
  693. rate_fixup_ratelist(vif, sband, info, dest, max_rates);
  694. }
  695. EXPORT_SYMBOL(ieee80211_get_tx_rates);
  696. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  697. struct sta_info *sta,
  698. struct ieee80211_tx_rate_control *txrc)
  699. {
  700. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  701. void *priv_sta = NULL;
  702. struct ieee80211_sta *ista = NULL;
  703. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  704. int i;
  705. if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
  706. ista = &sta->sta;
  707. priv_sta = sta->rate_ctrl_priv;
  708. }
  709. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  710. info->control.rates[i].idx = -1;
  711. info->control.rates[i].flags = 0;
  712. info->control.rates[i].count = 0;
  713. }
  714. if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
  715. return;
  716. if (ista) {
  717. spin_lock_bh(&sta->rate_ctrl_lock);
  718. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  719. spin_unlock_bh(&sta->rate_ctrl_lock);
  720. } else {
  721. ref->ops->get_rate(ref->priv, NULL, NULL, txrc);
  722. }
  723. if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
  724. return;
  725. ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
  726. info->control.rates,
  727. ARRAY_SIZE(info->control.rates));
  728. }
  729. int rate_control_set_rates(struct ieee80211_hw *hw,
  730. struct ieee80211_sta *pubsta,
  731. struct ieee80211_sta_rates *rates)
  732. {
  733. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  734. struct ieee80211_sta_rates *old;
  735. struct ieee80211_supported_band *sband;
  736. sband = hw->wiphy->bands[ieee80211_get_sdata_band(sta->sdata)];
  737. rate_control_apply_mask_ratetbl(sta, sband, rates);
  738. /*
  739. * mac80211 guarantees that this function will not be called
  740. * concurrently, so the following RCU access is safe, even without
  741. * extra locking. This can not be checked easily, so we just set
  742. * the condition to true.
  743. */
  744. old = rcu_dereference_protected(pubsta->rates, true);
  745. rcu_assign_pointer(pubsta->rates, rates);
  746. if (old)
  747. kfree_rcu(old, rcu_head);
  748. drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
  749. return 0;
  750. }
  751. EXPORT_SYMBOL(rate_control_set_rates);
  752. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  753. const char *name)
  754. {
  755. struct rate_control_ref *ref;
  756. ASSERT_RTNL();
  757. if (local->open_count)
  758. return -EBUSY;
  759. if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
  760. if (WARN_ON(!local->ops->set_rts_threshold))
  761. return -EINVAL;
  762. return 0;
  763. }
  764. ref = rate_control_alloc(name, local);
  765. if (!ref) {
  766. wiphy_warn(local->hw.wiphy,
  767. "Failed to select rate control algorithm\n");
  768. return -ENOENT;
  769. }
  770. WARN_ON(local->rate_ctrl);
  771. local->rate_ctrl = ref;
  772. wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
  773. ref->ops->name);
  774. return 0;
  775. }
  776. void rate_control_deinitialize(struct ieee80211_local *local)
  777. {
  778. struct rate_control_ref *ref;
  779. ref = local->rate_ctrl;
  780. if (!ref)
  781. return;
  782. local->rate_ctrl = NULL;
  783. rate_control_free(ref);
  784. }