rate.c 25 KB

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