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