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