rate.c 24 KB

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