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