rate.c 26 KB

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