rate.c 26 KB

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