0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/rtnetlink.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include "rate.h"
0015 #include "ieee80211_i.h"
0016 #include "debugfs.h"
0017
0018 struct rate_control_alg {
0019 struct list_head list;
0020 const struct rate_control_ops *ops;
0021 };
0022
0023 static LIST_HEAD(rate_ctrl_algs);
0024 static DEFINE_MUTEX(rate_ctrl_mutex);
0025
0026 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
0027 module_param(ieee80211_default_rc_algo, charp, 0644);
0028 MODULE_PARM_DESC(ieee80211_default_rc_algo,
0029 "Default rate control algorithm for mac80211 to use");
0030
0031 void rate_control_rate_init(struct sta_info *sta)
0032 {
0033 struct ieee80211_local *local = sta->sdata->local;
0034 struct rate_control_ref *ref = sta->rate_ctrl;
0035 struct ieee80211_sta *ista = &sta->sta;
0036 void *priv_sta = sta->rate_ctrl_priv;
0037 struct ieee80211_supported_band *sband;
0038 struct ieee80211_chanctx_conf *chanctx_conf;
0039
0040 ieee80211_sta_set_rx_nss(&sta->deflink);
0041
0042 if (!ref)
0043 return;
0044
0045 rcu_read_lock();
0046
0047 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
0048 if (WARN_ON(!chanctx_conf)) {
0049 rcu_read_unlock();
0050 return;
0051 }
0052
0053 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
0054
0055
0056 if (sband->band == NL80211_BAND_S1GHZ) {
0057 ieee80211_s1g_sta_rate_init(sta);
0058 rcu_read_unlock();
0059 return;
0060 }
0061
0062 spin_lock_bh(&sta->rate_ctrl_lock);
0063 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
0064 priv_sta);
0065 spin_unlock_bh(&sta->rate_ctrl_lock);
0066 rcu_read_unlock();
0067 set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
0068 }
0069
0070 void rate_control_tx_status(struct ieee80211_local *local,
0071 struct ieee80211_tx_status *st)
0072 {
0073 struct rate_control_ref *ref = local->rate_ctrl;
0074 struct sta_info *sta = container_of(st->sta, struct sta_info, sta);
0075 void *priv_sta = sta->rate_ctrl_priv;
0076 struct ieee80211_supported_band *sband;
0077
0078 if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
0079 return;
0080
0081 sband = local->hw.wiphy->bands[st->info->band];
0082
0083 spin_lock_bh(&sta->rate_ctrl_lock);
0084 if (ref->ops->tx_status_ext)
0085 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st);
0086 else if (st->skb)
0087 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb);
0088 else
0089 WARN_ON_ONCE(1);
0090
0091 spin_unlock_bh(&sta->rate_ctrl_lock);
0092 }
0093
0094 void rate_control_rate_update(struct ieee80211_local *local,
0095 struct ieee80211_supported_band *sband,
0096 struct sta_info *sta, unsigned int link_id,
0097 u32 changed)
0098 {
0099 struct rate_control_ref *ref = local->rate_ctrl;
0100 struct ieee80211_sta *ista = &sta->sta;
0101 void *priv_sta = sta->rate_ctrl_priv;
0102 struct ieee80211_chanctx_conf *chanctx_conf;
0103
0104 WARN_ON(link_id != 0);
0105
0106 if (ref && ref->ops->rate_update) {
0107 rcu_read_lock();
0108
0109 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
0110 if (WARN_ON(!chanctx_conf)) {
0111 rcu_read_unlock();
0112 return;
0113 }
0114
0115 spin_lock_bh(&sta->rate_ctrl_lock);
0116 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
0117 ista, priv_sta, changed);
0118 spin_unlock_bh(&sta->rate_ctrl_lock);
0119 rcu_read_unlock();
0120 }
0121
0122 drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
0123 }
0124
0125 int ieee80211_rate_control_register(const struct rate_control_ops *ops)
0126 {
0127 struct rate_control_alg *alg;
0128
0129 if (!ops->name)
0130 return -EINVAL;
0131
0132 mutex_lock(&rate_ctrl_mutex);
0133 list_for_each_entry(alg, &rate_ctrl_algs, list) {
0134 if (!strcmp(alg->ops->name, ops->name)) {
0135
0136 WARN_ON(1);
0137 mutex_unlock(&rate_ctrl_mutex);
0138 return -EALREADY;
0139 }
0140 }
0141
0142 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
0143 if (alg == NULL) {
0144 mutex_unlock(&rate_ctrl_mutex);
0145 return -ENOMEM;
0146 }
0147 alg->ops = ops;
0148
0149 list_add_tail(&alg->list, &rate_ctrl_algs);
0150 mutex_unlock(&rate_ctrl_mutex);
0151
0152 return 0;
0153 }
0154 EXPORT_SYMBOL(ieee80211_rate_control_register);
0155
0156 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
0157 {
0158 struct rate_control_alg *alg;
0159
0160 mutex_lock(&rate_ctrl_mutex);
0161 list_for_each_entry(alg, &rate_ctrl_algs, list) {
0162 if (alg->ops == ops) {
0163 list_del(&alg->list);
0164 kfree(alg);
0165 break;
0166 }
0167 }
0168 mutex_unlock(&rate_ctrl_mutex);
0169 }
0170 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
0171
0172 static const struct rate_control_ops *
0173 ieee80211_try_rate_control_ops_get(const char *name)
0174 {
0175 struct rate_control_alg *alg;
0176 const struct rate_control_ops *ops = NULL;
0177
0178 if (!name)
0179 return NULL;
0180
0181 mutex_lock(&rate_ctrl_mutex);
0182 list_for_each_entry(alg, &rate_ctrl_algs, list) {
0183 if (!strcmp(alg->ops->name, name)) {
0184 ops = alg->ops;
0185 break;
0186 }
0187 }
0188 mutex_unlock(&rate_ctrl_mutex);
0189 return ops;
0190 }
0191
0192
0193 static const struct rate_control_ops *
0194 ieee80211_rate_control_ops_get(const char *name)
0195 {
0196 const struct rate_control_ops *ops;
0197 const char *alg_name;
0198
0199 kernel_param_lock(THIS_MODULE);
0200 if (!name)
0201 alg_name = ieee80211_default_rc_algo;
0202 else
0203 alg_name = name;
0204
0205 ops = ieee80211_try_rate_control_ops_get(alg_name);
0206 if (!ops && name)
0207
0208 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
0209
0210
0211 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
0212
0213 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
0214
0215 kernel_param_unlock(THIS_MODULE);
0216
0217 return ops;
0218 }
0219
0220 #ifdef CONFIG_MAC80211_DEBUGFS
0221 static ssize_t rcname_read(struct file *file, char __user *userbuf,
0222 size_t count, loff_t *ppos)
0223 {
0224 struct rate_control_ref *ref = file->private_data;
0225 int len = strlen(ref->ops->name);
0226
0227 return simple_read_from_buffer(userbuf, count, ppos,
0228 ref->ops->name, len);
0229 }
0230
0231 const struct file_operations rcname_ops = {
0232 .read = rcname_read,
0233 .open = simple_open,
0234 .llseek = default_llseek,
0235 };
0236 #endif
0237
0238 static struct rate_control_ref *
0239 rate_control_alloc(const char *name, struct ieee80211_local *local)
0240 {
0241 struct rate_control_ref *ref;
0242
0243 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
0244 if (!ref)
0245 return NULL;
0246 ref->ops = ieee80211_rate_control_ops_get(name);
0247 if (!ref->ops)
0248 goto free;
0249
0250 ref->priv = ref->ops->alloc(&local->hw);
0251 if (!ref->priv)
0252 goto free;
0253 return ref;
0254
0255 free:
0256 kfree(ref);
0257 return NULL;
0258 }
0259
0260 static void rate_control_free(struct ieee80211_local *local,
0261 struct rate_control_ref *ctrl_ref)
0262 {
0263 ctrl_ref->ops->free(ctrl_ref->priv);
0264
0265 #ifdef CONFIG_MAC80211_DEBUGFS
0266 debugfs_remove_recursive(local->debugfs.rcdir);
0267 local->debugfs.rcdir = NULL;
0268 #endif
0269
0270 kfree(ctrl_ref);
0271 }
0272
0273 void ieee80211_check_rate_mask(struct ieee80211_link_data *link)
0274 {
0275 struct ieee80211_sub_if_data *sdata = link->sdata;
0276 struct ieee80211_local *local = sdata->local;
0277 struct ieee80211_supported_band *sband;
0278 u32 user_mask, basic_rates = link->conf->basic_rates;
0279 enum nl80211_band band;
0280
0281 if (WARN_ON(!link->conf->chandef.chan))
0282 return;
0283
0284 band = link->conf->chandef.chan->band;
0285 if (band == NL80211_BAND_S1GHZ) {
0286
0287 return;
0288 }
0289
0290 if (WARN_ON_ONCE(!basic_rates))
0291 return;
0292
0293 user_mask = sdata->rc_rateidx_mask[band];
0294 sband = local->hw.wiphy->bands[band];
0295
0296 if (user_mask & basic_rates)
0297 return;
0298
0299 sdata_dbg(sdata,
0300 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
0301 basic_rates, user_mask, band);
0302 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
0303 }
0304
0305 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
0306 {
0307 struct sk_buff *skb = txrc->skb;
0308 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0309
0310 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
0311 IEEE80211_TX_CTL_USE_MINRATE)) ||
0312 !ieee80211_is_tx_data(skb);
0313 }
0314
0315 static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
0316 u32 basic_rates,
0317 struct ieee80211_supported_band *sband)
0318 {
0319 u8 i;
0320
0321 if (sband->band == NL80211_BAND_S1GHZ) {
0322
0323 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
0324 rate->idx = 0;
0325 return;
0326 }
0327
0328 if (basic_rates == 0)
0329 return;
0330 if (rate->idx < 0)
0331 return;
0332 if (basic_rates & (1 << rate->idx))
0333 return;
0334
0335 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
0336 if (basic_rates & (1 << i)) {
0337 rate->idx = i;
0338 return;
0339 }
0340 }
0341
0342
0343 }
0344
0345 static void __rate_control_send_low(struct ieee80211_hw *hw,
0346 struct ieee80211_supported_band *sband,
0347 struct ieee80211_sta *sta,
0348 struct ieee80211_tx_info *info,
0349 u32 rate_mask)
0350 {
0351 int i;
0352 u32 rate_flags =
0353 ieee80211_chandef_rate_flags(&hw->conf.chandef);
0354
0355 if (sband->band == NL80211_BAND_S1GHZ) {
0356 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
0357 info->control.rates[0].idx = 0;
0358 return;
0359 }
0360
0361 if ((sband->band == NL80211_BAND_2GHZ) &&
0362 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
0363 rate_flags |= IEEE80211_RATE_ERP_G;
0364
0365 info->control.rates[0].idx = 0;
0366 for (i = 0; i < sband->n_bitrates; i++) {
0367 if (!(rate_mask & BIT(i)))
0368 continue;
0369
0370 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
0371 continue;
0372
0373 if (!rate_supported(sta, sband->band, i))
0374 continue;
0375
0376 info->control.rates[0].idx = i;
0377 break;
0378 }
0379 WARN_ONCE(i == sband->n_bitrates,
0380 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
0381 sta ? sta->addr : NULL,
0382 sta ? sta->deflink.supp_rates[sband->band] : -1,
0383 sband->band,
0384 rate_mask, rate_flags);
0385
0386 info->control.rates[0].count =
0387 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
0388 1 : hw->max_rate_tries;
0389
0390 info->control.skip_table = 1;
0391 }
0392
0393
0394 static bool rate_control_send_low(struct ieee80211_sta *pubsta,
0395 struct ieee80211_tx_rate_control *txrc)
0396 {
0397 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
0398 struct ieee80211_supported_band *sband = txrc->sband;
0399 struct sta_info *sta;
0400 int mcast_rate;
0401 bool use_basicrate = false;
0402
0403 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
0404 __rate_control_send_low(txrc->hw, sband, pubsta, info,
0405 txrc->rate_idx_mask);
0406
0407 if (!pubsta && txrc->bss) {
0408 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
0409 if (mcast_rate > 0) {
0410 info->control.rates[0].idx = mcast_rate - 1;
0411 return true;
0412 }
0413 use_basicrate = true;
0414 } else if (pubsta) {
0415 sta = container_of(pubsta, struct sta_info, sta);
0416 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
0417 use_basicrate = true;
0418 }
0419
0420 if (use_basicrate)
0421 rc_send_low_basicrate(&info->control.rates[0],
0422 txrc->bss_conf->basic_rates,
0423 sband);
0424
0425 return true;
0426 }
0427 return false;
0428 }
0429
0430 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
0431 {
0432 int j;
0433
0434
0435 for (j = *rate_idx; j >= 0; j--) {
0436 if (mask & (1 << j)) {
0437
0438 *rate_idx = j;
0439 return true;
0440 }
0441 }
0442
0443
0444 for (j = *rate_idx + 1; j < n_bitrates; j++) {
0445 if (mask & (1 << j)) {
0446
0447 *rate_idx = j;
0448 return true;
0449 }
0450 }
0451 return false;
0452 }
0453
0454 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
0455 {
0456 int i, j;
0457 int ridx, rbit;
0458
0459 ridx = *rate_idx / 8;
0460 rbit = *rate_idx % 8;
0461
0462
0463 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
0464 return false;
0465
0466
0467 for (i = ridx; i >= 0; i--) {
0468 for (j = rbit; j >= 0; j--)
0469 if (mcs_mask[i] & BIT(j)) {
0470 *rate_idx = i * 8 + j;
0471 return true;
0472 }
0473 rbit = 7;
0474 }
0475
0476
0477 ridx = (*rate_idx + 1) / 8;
0478 rbit = (*rate_idx + 1) % 8;
0479
0480 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
0481 for (j = rbit; j < 8; j++)
0482 if (mcs_mask[i] & BIT(j)) {
0483 *rate_idx = i * 8 + j;
0484 return true;
0485 }
0486 rbit = 0;
0487 }
0488 return false;
0489 }
0490
0491 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
0492 {
0493 int i, j;
0494 int ridx, rbit;
0495
0496 ridx = *rate_idx >> 4;
0497 rbit = *rate_idx & 0xf;
0498
0499 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
0500 return false;
0501
0502
0503 for (i = ridx; i >= 0; i--) {
0504 for (j = rbit; j >= 0; j--) {
0505 if (vht_mask[i] & BIT(j)) {
0506 *rate_idx = (i << 4) | j;
0507 return true;
0508 }
0509 }
0510 rbit = 15;
0511 }
0512
0513
0514 ridx = (*rate_idx + 1) >> 4;
0515 rbit = (*rate_idx + 1) & 0xf;
0516
0517 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
0518 for (j = rbit; j < 16; j++) {
0519 if (vht_mask[i] & BIT(j)) {
0520 *rate_idx = (i << 4) | j;
0521 return true;
0522 }
0523 }
0524 rbit = 0;
0525 }
0526 return false;
0527 }
0528
0529 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
0530 struct ieee80211_supported_band *sband,
0531 enum nl80211_chan_width chan_width,
0532 u32 mask,
0533 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
0534 u16 vht_mask[NL80211_VHT_NSS_MAX])
0535 {
0536 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
0537
0538 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
0539 return;
0540
0541 *rate_idx = 0;
0542
0543 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
0544 IEEE80211_TX_RC_USE_CTS_PROTECT |
0545 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
0546
0547 *rate_flags |= IEEE80211_TX_RC_MCS;
0548 if (chan_width == NL80211_CHAN_WIDTH_40)
0549 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
0550
0551 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
0552 return;
0553
0554
0555 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
0556 IEEE80211_TX_RC_40_MHZ_WIDTH);
0557 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
0558 mask))
0559 return;
0560 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
0561
0562 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
0563 return;
0564
0565
0566 *rate_idx = 0;
0567
0568 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
0569 IEEE80211_TX_RC_USE_CTS_PROTECT |
0570 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
0571 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
0572 mask))
0573 return;
0574 } else {
0575
0576 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
0577 mask))
0578 return;
0579
0580
0581 switch (chan_width) {
0582 case NL80211_CHAN_WIDTH_20_NOHT:
0583 case NL80211_CHAN_WIDTH_5:
0584 case NL80211_CHAN_WIDTH_10:
0585 return;
0586 default:
0587 break;
0588 }
0589
0590 *rate_idx = 0;
0591
0592 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
0593 IEEE80211_TX_RC_USE_CTS_PROTECT |
0594 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
0595
0596 *rate_flags |= IEEE80211_TX_RC_MCS;
0597
0598 if (chan_width == NL80211_CHAN_WIDTH_40)
0599 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
0600
0601 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
0602 return;
0603 }
0604
0605
0606
0607
0608
0609
0610
0611
0612 }
0613
0614 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
0615 struct ieee80211_supported_band *sband,
0616 struct ieee80211_tx_info *info,
0617 struct ieee80211_tx_rate *rates,
0618 int max_rates)
0619 {
0620 struct ieee80211_rate *rate;
0621 bool inval = false;
0622 int i;
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632 if (!(rates[0].flags &
0633 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
0634 u32 basic_rates = vif->bss_conf.basic_rates;
0635 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
0636
0637 rate = &sband->bitrates[rates[0].idx];
0638
0639 for (i = 0; i < sband->n_bitrates; i++) {
0640
0641 if (!(basic_rates & BIT(i)))
0642 continue;
0643
0644 if (sband->bitrates[i].bitrate > rate->bitrate)
0645 continue;
0646
0647 if (sband->bitrates[baserate].bitrate <
0648 sband->bitrates[i].bitrate)
0649 baserate = i;
0650 }
0651
0652 info->control.rts_cts_rate_idx = baserate;
0653 }
0654
0655 for (i = 0; i < max_rates; i++) {
0656
0657
0658
0659
0660
0661 if (inval) {
0662 rates[i].idx = -1;
0663 continue;
0664 }
0665 if (rates[i].idx < 0) {
0666 inval = true;
0667 continue;
0668 }
0669
0670
0671
0672
0673
0674 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
0675 WARN_ON(rates[i].idx > 76);
0676
0677 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
0678 info->control.use_cts_prot)
0679 rates[i].flags |=
0680 IEEE80211_TX_RC_USE_CTS_PROTECT;
0681 continue;
0682 }
0683
0684 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
0685 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
0686 continue;
0687 }
0688
0689
0690 if (info->control.use_rts) {
0691 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
0692 info->control.use_cts_prot = false;
0693 }
0694
0695
0696 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
0697 rates[i].idx = -1;
0698 continue;
0699 }
0700
0701 rate = &sband->bitrates[rates[i].idx];
0702
0703
0704 if (info->control.short_preamble &&
0705 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
0706 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
0707
0708
0709 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
0710 info->control.use_cts_prot &&
0711 rate->flags & IEEE80211_RATE_ERP_G)
0712 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
0713 }
0714 }
0715
0716
0717 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
0718 struct ieee80211_tx_info *info,
0719 struct ieee80211_tx_rate *rates,
0720 int max_rates)
0721 {
0722 struct ieee80211_sta_rates *ratetbl = NULL;
0723 int i;
0724
0725 if (sta && !info->control.skip_table)
0726 ratetbl = rcu_dereference(sta->rates);
0727
0728
0729 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
0730 for (i = 0; i < max_rates; i++) {
0731 if (i < ARRAY_SIZE(info->control.rates) &&
0732 info->control.rates[i].idx >= 0 &&
0733 info->control.rates[i].count) {
0734 if (rates != info->control.rates)
0735 rates[i] = info->control.rates[i];
0736 } else if (ratetbl) {
0737 rates[i].idx = ratetbl->rate[i].idx;
0738 rates[i].flags = ratetbl->rate[i].flags;
0739 if (info->control.use_rts)
0740 rates[i].count = ratetbl->rate[i].count_rts;
0741 else if (info->control.use_cts_prot)
0742 rates[i].count = ratetbl->rate[i].count_cts;
0743 else
0744 rates[i].count = ratetbl->rate[i].count;
0745 } else {
0746 rates[i].idx = -1;
0747 rates[i].count = 0;
0748 }
0749
0750 if (rates[i].idx < 0 || !rates[i].count)
0751 break;
0752 }
0753 }
0754
0755 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
0756 struct ieee80211_supported_band *sband,
0757 struct ieee80211_sta *sta, u32 *mask,
0758 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
0759 u16 vht_mask[NL80211_VHT_NSS_MAX])
0760 {
0761 u32 i, flags;
0762
0763 *mask = sdata->rc_rateidx_mask[sband->band];
0764 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
0765 for (i = 0; i < sband->n_bitrates; i++) {
0766 if ((flags & sband->bitrates[i].flags) != flags)
0767 *mask &= ~BIT(i);
0768 }
0769
0770 if (*mask == (1 << sband->n_bitrates) - 1 &&
0771 !sdata->rc_has_mcs_mask[sband->band] &&
0772 !sdata->rc_has_vht_mcs_mask[sband->band])
0773 return false;
0774
0775 if (sdata->rc_has_mcs_mask[sband->band])
0776 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
0777 IEEE80211_HT_MCS_MASK_LEN);
0778 else
0779 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
0780
0781 if (sdata->rc_has_vht_mcs_mask[sband->band])
0782 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
0783 sizeof(u16) * NL80211_VHT_NSS_MAX);
0784 else
0785 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
0786
0787 if (sta) {
0788 __le16 sta_vht_cap;
0789 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
0790
0791
0792 *mask &= sta->deflink.supp_rates[sband->band];
0793 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
0794 mcs_mask[i] &= sta->deflink.ht_cap.mcs.rx_mask[i];
0795
0796 sta_vht_cap = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
0797 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
0798 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
0799 vht_mask[i] &= sta_vht_mask[i];
0800 }
0801
0802 return true;
0803 }
0804
0805 static void
0806 rate_control_apply_mask_ratetbl(struct sta_info *sta,
0807 struct ieee80211_supported_band *sband,
0808 struct ieee80211_sta_rates *rates)
0809 {
0810 int i;
0811 u32 mask;
0812 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
0813 u16 vht_mask[NL80211_VHT_NSS_MAX];
0814 enum nl80211_chan_width chan_width;
0815
0816 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
0817 mcs_mask, vht_mask))
0818 return;
0819
0820 chan_width = sta->sdata->vif.bss_conf.chandef.width;
0821 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
0822 if (rates->rate[i].idx < 0)
0823 break;
0824
0825 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
0826 sband, chan_width, mask, mcs_mask,
0827 vht_mask);
0828 }
0829 }
0830
0831 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
0832 struct ieee80211_sta *sta,
0833 struct ieee80211_supported_band *sband,
0834 struct ieee80211_tx_rate *rates,
0835 int max_rates)
0836 {
0837 enum nl80211_chan_width chan_width;
0838 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
0839 u32 mask;
0840 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
0841 int i;
0842
0843
0844
0845
0846
0847
0848 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
0849 vht_mask))
0850 return;
0851
0852
0853
0854
0855
0856
0857 chan_width = sdata->vif.bss_conf.chandef.width;
0858 for (i = 0; i < max_rates; i++) {
0859
0860 if (rates[i].idx < 0)
0861 break;
0862
0863 rate_flags = rates[i].flags;
0864 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
0865 chan_width, mask, mcs_mask, vht_mask);
0866 rates[i].flags = rate_flags;
0867 }
0868 }
0869
0870 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
0871 struct ieee80211_sta *sta,
0872 struct sk_buff *skb,
0873 struct ieee80211_tx_rate *dest,
0874 int max_rates)
0875 {
0876 struct ieee80211_sub_if_data *sdata;
0877 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0878 struct ieee80211_supported_band *sband;
0879
0880 rate_control_fill_sta_table(sta, info, dest, max_rates);
0881
0882 if (!vif)
0883 return;
0884
0885 sdata = vif_to_sdata(vif);
0886 sband = sdata->local->hw.wiphy->bands[info->band];
0887
0888 if (ieee80211_is_tx_data(skb))
0889 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
0890
0891 if (dest[0].idx < 0)
0892 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
0893 sdata->rc_rateidx_mask[info->band]);
0894
0895 if (sta)
0896 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
0897 }
0898 EXPORT_SYMBOL(ieee80211_get_tx_rates);
0899
0900 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
0901 struct sta_info *sta,
0902 struct ieee80211_tx_rate_control *txrc)
0903 {
0904 struct rate_control_ref *ref = sdata->local->rate_ctrl;
0905 void *priv_sta = NULL;
0906 struct ieee80211_sta *ista = NULL;
0907 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
0908 int i;
0909
0910 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
0911 info->control.rates[i].idx = -1;
0912 info->control.rates[i].flags = 0;
0913 info->control.rates[i].count = 0;
0914 }
0915
0916 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc))
0917 return;
0918
0919 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
0920 return;
0921
0922 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
0923 ista = &sta->sta;
0924 priv_sta = sta->rate_ctrl_priv;
0925 }
0926
0927 if (ista) {
0928 spin_lock_bh(&sta->rate_ctrl_lock);
0929 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
0930 spin_unlock_bh(&sta->rate_ctrl_lock);
0931 } else {
0932 rate_control_send_low(NULL, txrc);
0933 }
0934
0935 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
0936 return;
0937
0938 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
0939 info->control.rates,
0940 ARRAY_SIZE(info->control.rates));
0941 }
0942
0943 int rate_control_set_rates(struct ieee80211_hw *hw,
0944 struct ieee80211_sta *pubsta,
0945 struct ieee80211_sta_rates *rates)
0946 {
0947 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
0948 struct ieee80211_sta_rates *old;
0949 struct ieee80211_supported_band *sband;
0950
0951 sband = ieee80211_get_sband(sta->sdata);
0952 if (!sband)
0953 return -EINVAL;
0954 rate_control_apply_mask_ratetbl(sta, sband, rates);
0955
0956
0957
0958
0959
0960
0961 old = rcu_dereference_protected(pubsta->rates, true);
0962 rcu_assign_pointer(pubsta->rates, rates);
0963 if (old)
0964 kfree_rcu(old, rcu_head);
0965
0966 if (sta->uploaded)
0967 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
0968
0969 ieee80211_sta_set_expected_throughput(pubsta, sta_get_expected_throughput(sta));
0970
0971 return 0;
0972 }
0973 EXPORT_SYMBOL(rate_control_set_rates);
0974
0975 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
0976 const char *name)
0977 {
0978 struct rate_control_ref *ref;
0979
0980 ASSERT_RTNL();
0981
0982 if (local->open_count)
0983 return -EBUSY;
0984
0985 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
0986 if (WARN_ON(!local->ops->set_rts_threshold))
0987 return -EINVAL;
0988 return 0;
0989 }
0990
0991 ref = rate_control_alloc(name, local);
0992 if (!ref) {
0993 wiphy_warn(local->hw.wiphy,
0994 "Failed to select rate control algorithm\n");
0995 return -ENOENT;
0996 }
0997
0998 WARN_ON(local->rate_ctrl);
0999 local->rate_ctrl = ref;
1000
1001 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
1002 ref->ops->name);
1003
1004 return 0;
1005 }
1006
1007 void rate_control_deinitialize(struct ieee80211_local *local)
1008 {
1009 struct rate_control_ref *ref;
1010
1011 ref = local->rate_ctrl;
1012
1013 if (!ref)
1014 return;
1015
1016 local->rate_ctrl = NULL;
1017 rate_control_free(local, ref);
1018 }