xref: /openbmc/linux/net/mac80211/chan.c (revision 939c4c7e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * mac80211 - channel management
4  * Copyright 2020 - 2022 Intel Corporation
5  */
6 
7 #include <linux/nl80211.h>
8 #include <linux/export.h>
9 #include <linux/rtnetlink.h>
10 #include <net/cfg80211.h>
11 #include "ieee80211_i.h"
12 #include "driver-ops.h"
13 #include "rate.h"
14 
15 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
16 					  struct ieee80211_chanctx *ctx)
17 {
18 	struct ieee80211_link_data *link;
19 	int num = 0;
20 
21 	lockdep_assert_held(&local->chanctx_mtx);
22 
23 	list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list)
24 		num++;
25 
26 	return num;
27 }
28 
29 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
30 					  struct ieee80211_chanctx *ctx)
31 {
32 	struct ieee80211_link_data *link;
33 	int num = 0;
34 
35 	lockdep_assert_held(&local->chanctx_mtx);
36 
37 	list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
38 		num++;
39 
40 	return num;
41 }
42 
43 int ieee80211_chanctx_refcount(struct ieee80211_local *local,
44 			       struct ieee80211_chanctx *ctx)
45 {
46 	return ieee80211_chanctx_num_assigned(local, ctx) +
47 	       ieee80211_chanctx_num_reserved(local, ctx);
48 }
49 
50 static int ieee80211_num_chanctx(struct ieee80211_local *local)
51 {
52 	struct ieee80211_chanctx *ctx;
53 	int num = 0;
54 
55 	lockdep_assert_held(&local->chanctx_mtx);
56 
57 	list_for_each_entry(ctx, &local->chanctx_list, list)
58 		num++;
59 
60 	return num;
61 }
62 
63 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
64 {
65 	lockdep_assert_held(&local->chanctx_mtx);
66 	return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
67 }
68 
69 static struct ieee80211_chanctx *
70 ieee80211_link_get_chanctx(struct ieee80211_link_data *link)
71 {
72 	struct ieee80211_local *local __maybe_unused = link->sdata->local;
73 	struct ieee80211_chanctx_conf *conf;
74 
75 	conf = rcu_dereference_protected(link->conf->chanctx_conf,
76 					 lockdep_is_held(&local->chanctx_mtx));
77 	if (!conf)
78 		return NULL;
79 
80 	return container_of(conf, struct ieee80211_chanctx, conf);
81 }
82 
83 static const struct cfg80211_chan_def *
84 ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
85 				   struct ieee80211_chanctx *ctx,
86 				   const struct cfg80211_chan_def *compat)
87 {
88 	struct ieee80211_link_data *link;
89 
90 	lockdep_assert_held(&local->chanctx_mtx);
91 
92 	list_for_each_entry(link, &ctx->reserved_links,
93 			    reserved_chanctx_list) {
94 		if (!compat)
95 			compat = &link->reserved_chandef;
96 
97 		compat = cfg80211_chandef_compatible(&link->reserved_chandef,
98 						     compat);
99 		if (!compat)
100 			break;
101 	}
102 
103 	return compat;
104 }
105 
106 static const struct cfg80211_chan_def *
107 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
108 				       struct ieee80211_chanctx *ctx,
109 				       const struct cfg80211_chan_def *compat)
110 {
111 	struct ieee80211_link_data *link;
112 
113 	lockdep_assert_held(&local->chanctx_mtx);
114 
115 	list_for_each_entry(link, &ctx->assigned_links,
116 			    assigned_chanctx_list) {
117 		struct ieee80211_bss_conf *link_conf = link->conf;
118 
119 		if (link->reserved_chanctx)
120 			continue;
121 
122 		if (!compat)
123 			compat = &link_conf->chandef;
124 
125 		compat = cfg80211_chandef_compatible(
126 				&link_conf->chandef, compat);
127 		if (!compat)
128 			break;
129 	}
130 
131 	return compat;
132 }
133 
134 static const struct cfg80211_chan_def *
135 ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
136 				   struct ieee80211_chanctx *ctx,
137 				   const struct cfg80211_chan_def *compat)
138 {
139 	lockdep_assert_held(&local->chanctx_mtx);
140 
141 	compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
142 	if (!compat)
143 		return NULL;
144 
145 	compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
146 	if (!compat)
147 		return NULL;
148 
149 	return compat;
150 }
151 
152 static bool
153 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
154 				      struct ieee80211_chanctx *ctx,
155 				      const struct cfg80211_chan_def *def)
156 {
157 	lockdep_assert_held(&local->chanctx_mtx);
158 
159 	if (ieee80211_chanctx_combined_chandef(local, ctx, def))
160 		return true;
161 
162 	if (!list_empty(&ctx->reserved_links) &&
163 	    ieee80211_chanctx_reserved_chandef(local, ctx, def))
164 		return true;
165 
166 	return false;
167 }
168 
169 static struct ieee80211_chanctx *
170 ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
171 				   const struct cfg80211_chan_def *chandef,
172 				   enum ieee80211_chanctx_mode mode)
173 {
174 	struct ieee80211_chanctx *ctx;
175 
176 	lockdep_assert_held(&local->chanctx_mtx);
177 
178 	if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
179 		return NULL;
180 
181 	list_for_each_entry(ctx, &local->chanctx_list, list) {
182 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
183 			continue;
184 
185 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
186 			continue;
187 
188 		if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
189 							   chandef))
190 			continue;
191 
192 		return ctx;
193 	}
194 
195 	return NULL;
196 }
197 
198 static enum nl80211_chan_width ieee80211_get_sta_bw(struct sta_info *sta,
199 						    unsigned int link_id)
200 {
201 	enum ieee80211_sta_rx_bandwidth width;
202 	struct link_sta_info *link_sta;
203 
204 	link_sta = rcu_dereference(sta->link[link_id]);
205 
206 	/* no effect if this STA has no presence on this link */
207 	if (!link_sta)
208 		return NL80211_CHAN_WIDTH_20_NOHT;
209 
210 	width = ieee80211_sta_cap_rx_bw(link_sta);
211 
212 	switch (width) {
213 	case IEEE80211_STA_RX_BW_20:
214 		if (link_sta->pub->ht_cap.ht_supported)
215 			return NL80211_CHAN_WIDTH_20;
216 		else
217 			return NL80211_CHAN_WIDTH_20_NOHT;
218 	case IEEE80211_STA_RX_BW_40:
219 		return NL80211_CHAN_WIDTH_40;
220 	case IEEE80211_STA_RX_BW_80:
221 		return NL80211_CHAN_WIDTH_80;
222 	case IEEE80211_STA_RX_BW_160:
223 		/*
224 		 * This applied for both 160 and 80+80. since we use
225 		 * the returned value to consider degradation of
226 		 * ctx->conf.min_def, we have to make sure to take
227 		 * the bigger one (NL80211_CHAN_WIDTH_160).
228 		 * Otherwise we might try degrading even when not
229 		 * needed, as the max required sta_bw returned (80+80)
230 		 * might be smaller than the configured bw (160).
231 		 */
232 		return NL80211_CHAN_WIDTH_160;
233 	case IEEE80211_STA_RX_BW_320:
234 		return NL80211_CHAN_WIDTH_320;
235 	default:
236 		WARN_ON(1);
237 		return NL80211_CHAN_WIDTH_20;
238 	}
239 }
240 
241 static enum nl80211_chan_width
242 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata,
243 			      unsigned int link_id)
244 {
245 	enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
246 	struct sta_info *sta;
247 
248 	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
249 		if (sdata != sta->sdata &&
250 		    !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
251 			continue;
252 
253 		max_bw = max(max_bw, ieee80211_get_sta_bw(sta, link_id));
254 	}
255 
256 	return max_bw;
257 }
258 
259 static enum nl80211_chan_width
260 ieee80211_get_chanctx_vif_max_required_bw(struct ieee80211_sub_if_data *sdata,
261 					  struct ieee80211_chanctx_conf *conf)
262 {
263 	enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
264 	struct ieee80211_vif *vif = &sdata->vif;
265 	int link_id;
266 
267 	rcu_read_lock();
268 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
269 		enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
270 		struct ieee80211_bss_conf *link_conf =
271 			rcu_dereference(sdata->vif.link_conf[link_id]);
272 
273 		if (!link_conf)
274 			continue;
275 
276 		if (rcu_access_pointer(link_conf->chanctx_conf) != conf)
277 			continue;
278 
279 		switch (vif->type) {
280 		case NL80211_IFTYPE_AP:
281 		case NL80211_IFTYPE_AP_VLAN:
282 			width = ieee80211_get_max_required_bw(sdata, link_id);
283 			break;
284 		case NL80211_IFTYPE_STATION:
285 			/*
286 			 * The ap's sta->bandwidth is not set yet at this
287 			 * point, so take the width from the chandef, but
288 			 * account also for TDLS peers
289 			 */
290 			width = max(link_conf->chandef.width,
291 				    ieee80211_get_max_required_bw(sdata, link_id));
292 			break;
293 		case NL80211_IFTYPE_P2P_DEVICE:
294 		case NL80211_IFTYPE_NAN:
295 			continue;
296 		case NL80211_IFTYPE_ADHOC:
297 		case NL80211_IFTYPE_MESH_POINT:
298 		case NL80211_IFTYPE_OCB:
299 			width = link_conf->chandef.width;
300 			break;
301 		case NL80211_IFTYPE_WDS:
302 		case NL80211_IFTYPE_UNSPECIFIED:
303 		case NUM_NL80211_IFTYPES:
304 		case NL80211_IFTYPE_MONITOR:
305 		case NL80211_IFTYPE_P2P_CLIENT:
306 		case NL80211_IFTYPE_P2P_GO:
307 			WARN_ON_ONCE(1);
308 		}
309 
310 		max_bw = max(max_bw, width);
311 	}
312 	rcu_read_unlock();
313 
314 	return max_bw;
315 }
316 
317 static enum nl80211_chan_width
318 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
319 				      struct ieee80211_chanctx_conf *conf)
320 {
321 	struct ieee80211_sub_if_data *sdata;
322 	enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
323 
324 	rcu_read_lock();
325 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
326 		enum nl80211_chan_width width;
327 
328 		if (!ieee80211_sdata_running(sdata))
329 			continue;
330 
331 		width = ieee80211_get_chanctx_vif_max_required_bw(sdata, conf);
332 
333 		max_bw = max(max_bw, width);
334 	}
335 
336 	/* use the configured bandwidth in case of monitor interface */
337 	sdata = rcu_dereference(local->monitor_sdata);
338 	if (sdata &&
339 	    rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == conf)
340 		max_bw = max(max_bw, conf->def.width);
341 
342 	rcu_read_unlock();
343 
344 	return max_bw;
345 }
346 
347 /*
348  * recalc the min required chan width of the channel context, which is
349  * the max of min required widths of all the interfaces bound to this
350  * channel context.
351  */
352 static u32 _ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
353 					     struct ieee80211_chanctx *ctx)
354 {
355 	enum nl80211_chan_width max_bw;
356 	struct cfg80211_chan_def min_def;
357 
358 	lockdep_assert_held(&local->chanctx_mtx);
359 
360 	/* don't optimize non-20MHz based and radar_enabled confs */
361 	if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
362 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
363 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
364 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
365 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
366 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
367 	    ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
368 	    ctx->conf.radar_enabled) {
369 		ctx->conf.min_def = ctx->conf.def;
370 		return 0;
371 	}
372 
373 	max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
374 
375 	/* downgrade chandef up to max_bw */
376 	min_def = ctx->conf.def;
377 	while (min_def.width > max_bw)
378 		ieee80211_chandef_downgrade(&min_def);
379 
380 	if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
381 		return 0;
382 
383 	ctx->conf.min_def = min_def;
384 	if (!ctx->driver_present)
385 		return 0;
386 
387 	return IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
388 }
389 
390 /* calling this function is assuming that station vif is updated to
391  * lates changes by calling ieee80211_link_update_chandef
392  */
393 static void ieee80211_chan_bw_change(struct ieee80211_local *local,
394 				     struct ieee80211_chanctx *ctx,
395 				     bool narrowed)
396 {
397 	struct sta_info *sta;
398 	struct ieee80211_supported_band *sband =
399 		local->hw.wiphy->bands[ctx->conf.def.chan->band];
400 
401 	rcu_read_lock();
402 	list_for_each_entry_rcu(sta, &local->sta_list,
403 				list) {
404 		struct ieee80211_sub_if_data *sdata = sta->sdata;
405 		enum ieee80211_sta_rx_bandwidth new_sta_bw;
406 		unsigned int link_id;
407 
408 		if (!ieee80211_sdata_running(sta->sdata))
409 			continue;
410 
411 		for (link_id = 0; link_id < ARRAY_SIZE(sta->sdata->link); link_id++) {
412 			struct ieee80211_bss_conf *link_conf =
413 				rcu_dereference(sdata->vif.link_conf[link_id]);
414 			struct link_sta_info *link_sta;
415 
416 			if (!link_conf)
417 				continue;
418 
419 			if (rcu_access_pointer(link_conf->chanctx_conf) != &ctx->conf)
420 				continue;
421 
422 			link_sta = rcu_dereference(sta->link[link_id]);
423 			if (!link_sta)
424 				continue;
425 
426 			new_sta_bw = ieee80211_sta_cur_vht_bw(link_sta);
427 
428 			/* nothing change */
429 			if (new_sta_bw == link_sta->pub->bandwidth)
430 				continue;
431 
432 			/* vif changed to narrow BW and narrow BW for station wasn't
433 			 * requested or vise versa */
434 			if ((new_sta_bw < link_sta->pub->bandwidth) == !narrowed)
435 				continue;
436 
437 			link_sta->pub->bandwidth = new_sta_bw;
438 			rate_control_rate_update(local, sband, sta, link_id,
439 						 IEEE80211_RC_BW_CHANGED);
440 		}
441 	}
442 	rcu_read_unlock();
443 }
444 
445 /*
446  * recalc the min required chan width of the channel context, which is
447  * the max of min required widths of all the interfaces bound to this
448  * channel context.
449  */
450 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
451 				      struct ieee80211_chanctx *ctx)
452 {
453 	u32 changed = _ieee80211_recalc_chanctx_min_def(local, ctx);
454 
455 	if (!changed)
456 		return;
457 
458 	/* check is BW narrowed */
459 	ieee80211_chan_bw_change(local, ctx, true);
460 
461 	drv_change_chanctx(local, ctx, changed);
462 
463 	/* check is BW wider */
464 	ieee80211_chan_bw_change(local, ctx, false);
465 }
466 
467 static void ieee80211_change_chanctx(struct ieee80211_local *local,
468 				     struct ieee80211_chanctx *ctx,
469 				     struct ieee80211_chanctx *old_ctx,
470 				     const struct cfg80211_chan_def *chandef)
471 {
472 	u32 changed;
473 
474 	/* expected to handle only 20/40/80/160/320 channel widths */
475 	switch (chandef->width) {
476 	case NL80211_CHAN_WIDTH_20_NOHT:
477 	case NL80211_CHAN_WIDTH_20:
478 	case NL80211_CHAN_WIDTH_40:
479 	case NL80211_CHAN_WIDTH_80:
480 	case NL80211_CHAN_WIDTH_80P80:
481 	case NL80211_CHAN_WIDTH_160:
482 	case NL80211_CHAN_WIDTH_320:
483 		break;
484 	default:
485 		WARN_ON(1);
486 	}
487 
488 	/* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def
489 	 * due to maybe not returning from it, e.g in case new context was added
490 	 * first time with all parameters up to date.
491 	 */
492 	ieee80211_chan_bw_change(local, old_ctx, true);
493 
494 	if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
495 		ieee80211_recalc_chanctx_min_def(local, ctx);
496 		return;
497 	}
498 
499 	WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
500 
501 	ctx->conf.def = *chandef;
502 
503 	/* check if min chanctx also changed */
504 	changed = IEEE80211_CHANCTX_CHANGE_WIDTH |
505 		  _ieee80211_recalc_chanctx_min_def(local, ctx);
506 	drv_change_chanctx(local, ctx, changed);
507 
508 	if (!local->use_chanctx) {
509 		local->_oper_chandef = *chandef;
510 		ieee80211_hw_config(local, 0);
511 	}
512 
513 	/* check is BW wider */
514 	ieee80211_chan_bw_change(local, old_ctx, false);
515 }
516 
517 static struct ieee80211_chanctx *
518 ieee80211_find_chanctx(struct ieee80211_local *local,
519 		       const struct cfg80211_chan_def *chandef,
520 		       enum ieee80211_chanctx_mode mode)
521 {
522 	struct ieee80211_chanctx *ctx;
523 
524 	lockdep_assert_held(&local->chanctx_mtx);
525 
526 	if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
527 		return NULL;
528 
529 	list_for_each_entry(ctx, &local->chanctx_list, list) {
530 		const struct cfg80211_chan_def *compat;
531 
532 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
533 			continue;
534 
535 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
536 			continue;
537 
538 		compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
539 		if (!compat)
540 			continue;
541 
542 		compat = ieee80211_chanctx_reserved_chandef(local, ctx,
543 							    compat);
544 		if (!compat)
545 			continue;
546 
547 		ieee80211_change_chanctx(local, ctx, ctx, compat);
548 
549 		return ctx;
550 	}
551 
552 	return NULL;
553 }
554 
555 bool ieee80211_is_radar_required(struct ieee80211_local *local)
556 {
557 	struct ieee80211_sub_if_data *sdata;
558 
559 	lockdep_assert_held(&local->mtx);
560 
561 	rcu_read_lock();
562 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
563 		unsigned int link_id;
564 
565 		for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
566 			struct ieee80211_link_data *link;
567 
568 			link = rcu_dereference(sdata->link[link_id]);
569 
570 			if (link && link->radar_required) {
571 				rcu_read_unlock();
572 				return true;
573 			}
574 		}
575 	}
576 	rcu_read_unlock();
577 
578 	return false;
579 }
580 
581 static bool
582 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
583 				 struct ieee80211_chanctx *ctx)
584 {
585 	struct ieee80211_chanctx_conf *conf = &ctx->conf;
586 	struct ieee80211_sub_if_data *sdata;
587 	bool required = false;
588 
589 	lockdep_assert_held(&local->chanctx_mtx);
590 	lockdep_assert_held(&local->mtx);
591 
592 	rcu_read_lock();
593 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
594 		unsigned int link_id;
595 
596 		if (!ieee80211_sdata_running(sdata))
597 			continue;
598 		for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
599 			struct ieee80211_link_data *link;
600 
601 			link = rcu_dereference(sdata->link[link_id]);
602 			if (!link)
603 				continue;
604 
605 			if (rcu_access_pointer(link->conf->chanctx_conf) != conf)
606 				continue;
607 			if (!link->radar_required)
608 				continue;
609 			required = true;
610 			break;
611 		}
612 
613 		if (required)
614 			break;
615 	}
616 	rcu_read_unlock();
617 
618 	return required;
619 }
620 
621 static struct ieee80211_chanctx *
622 ieee80211_alloc_chanctx(struct ieee80211_local *local,
623 			const struct cfg80211_chan_def *chandef,
624 			enum ieee80211_chanctx_mode mode)
625 {
626 	struct ieee80211_chanctx *ctx;
627 
628 	lockdep_assert_held(&local->chanctx_mtx);
629 
630 	ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
631 	if (!ctx)
632 		return NULL;
633 
634 	INIT_LIST_HEAD(&ctx->assigned_links);
635 	INIT_LIST_HEAD(&ctx->reserved_links);
636 	ctx->conf.def = *chandef;
637 	ctx->conf.rx_chains_static = 1;
638 	ctx->conf.rx_chains_dynamic = 1;
639 	ctx->mode = mode;
640 	ctx->conf.radar_enabled = false;
641 	ieee80211_recalc_chanctx_min_def(local, ctx);
642 
643 	return ctx;
644 }
645 
646 static int ieee80211_add_chanctx(struct ieee80211_local *local,
647 				 struct ieee80211_chanctx *ctx)
648 {
649 	u32 changed;
650 	int err;
651 
652 	lockdep_assert_held(&local->mtx);
653 	lockdep_assert_held(&local->chanctx_mtx);
654 
655 	if (!local->use_chanctx)
656 		local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
657 
658 	/* turn idle off *before* setting channel -- some drivers need that */
659 	changed = ieee80211_idle_off(local);
660 	if (changed)
661 		ieee80211_hw_config(local, changed);
662 
663 	if (!local->use_chanctx) {
664 		local->_oper_chandef = ctx->conf.def;
665 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
666 	} else {
667 		err = drv_add_chanctx(local, ctx);
668 		if (err) {
669 			ieee80211_recalc_idle(local);
670 			return err;
671 		}
672 	}
673 
674 	return 0;
675 }
676 
677 static struct ieee80211_chanctx *
678 ieee80211_new_chanctx(struct ieee80211_local *local,
679 		      const struct cfg80211_chan_def *chandef,
680 		      enum ieee80211_chanctx_mode mode)
681 {
682 	struct ieee80211_chanctx *ctx;
683 	int err;
684 
685 	lockdep_assert_held(&local->mtx);
686 	lockdep_assert_held(&local->chanctx_mtx);
687 
688 	ctx = ieee80211_alloc_chanctx(local, chandef, mode);
689 	if (!ctx)
690 		return ERR_PTR(-ENOMEM);
691 
692 	err = ieee80211_add_chanctx(local, ctx);
693 	if (err) {
694 		kfree(ctx);
695 		return ERR_PTR(err);
696 	}
697 
698 	list_add_rcu(&ctx->list, &local->chanctx_list);
699 	return ctx;
700 }
701 
702 static void ieee80211_del_chanctx(struct ieee80211_local *local,
703 				  struct ieee80211_chanctx *ctx)
704 {
705 	lockdep_assert_held(&local->chanctx_mtx);
706 
707 	if (!local->use_chanctx) {
708 		struct cfg80211_chan_def *chandef = &local->_oper_chandef;
709 		/* S1G doesn't have 20MHz, so get the correct width for the
710 		 * current channel.
711 		 */
712 		if (chandef->chan->band == NL80211_BAND_S1GHZ)
713 			chandef->width =
714 				ieee80211_s1g_channel_width(chandef->chan);
715 		else
716 			chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
717 		chandef->center_freq1 = chandef->chan->center_freq;
718 		chandef->freq1_offset = chandef->chan->freq_offset;
719 		chandef->center_freq2 = 0;
720 
721 		/* NOTE: Disabling radar is only valid here for
722 		 * single channel context. To be sure, check it ...
723 		 */
724 		WARN_ON(local->hw.conf.radar_enabled &&
725 			!list_empty(&local->chanctx_list));
726 
727 		local->hw.conf.radar_enabled = false;
728 
729 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
730 	} else {
731 		drv_remove_chanctx(local, ctx);
732 	}
733 
734 	ieee80211_recalc_idle(local);
735 }
736 
737 static void ieee80211_free_chanctx(struct ieee80211_local *local,
738 				   struct ieee80211_chanctx *ctx)
739 {
740 	lockdep_assert_held(&local->chanctx_mtx);
741 
742 	WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
743 
744 	list_del_rcu(&ctx->list);
745 	ieee80211_del_chanctx(local, ctx);
746 	kfree_rcu(ctx, rcu_head);
747 }
748 
749 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
750 				       struct ieee80211_chanctx *ctx)
751 {
752 	struct ieee80211_chanctx_conf *conf = &ctx->conf;
753 	struct ieee80211_sub_if_data *sdata;
754 	const struct cfg80211_chan_def *compat = NULL;
755 	struct sta_info *sta;
756 
757 	lockdep_assert_held(&local->chanctx_mtx);
758 
759 	rcu_read_lock();
760 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
761 		int link_id;
762 
763 		if (!ieee80211_sdata_running(sdata))
764 			continue;
765 
766 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
767 			continue;
768 
769 		for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
770 			struct ieee80211_bss_conf *link_conf =
771 				rcu_dereference(sdata->vif.link_conf[link_id]);
772 
773 			if (!link_conf)
774 				continue;
775 
776 			if (rcu_access_pointer(link_conf->chanctx_conf) != conf)
777 				continue;
778 
779 			if (!compat)
780 				compat = &link_conf->chandef;
781 
782 			compat = cfg80211_chandef_compatible(&link_conf->chandef,
783 							     compat);
784 			if (WARN_ON_ONCE(!compat))
785 				break;
786 		}
787 	}
788 
789 	/* TDLS peers can sometimes affect the chandef width */
790 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
791 		if (!sta->uploaded ||
792 		    !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
793 		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
794 		    !sta->tdls_chandef.chan)
795 			continue;
796 
797 		compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
798 						     compat);
799 		if (WARN_ON_ONCE(!compat))
800 			break;
801 	}
802 	rcu_read_unlock();
803 
804 	if (!compat)
805 		return;
806 
807 	ieee80211_change_chanctx(local, ctx, ctx, compat);
808 }
809 
810 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
811 					   struct ieee80211_chanctx *chanctx)
812 {
813 	bool radar_enabled;
814 
815 	lockdep_assert_held(&local->chanctx_mtx);
816 	/* for ieee80211_is_radar_required */
817 	lockdep_assert_held(&local->mtx);
818 
819 	radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
820 
821 	if (radar_enabled == chanctx->conf.radar_enabled)
822 		return;
823 
824 	chanctx->conf.radar_enabled = radar_enabled;
825 
826 	if (!local->use_chanctx) {
827 		local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
828 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
829 	}
830 
831 	drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
832 }
833 
834 static int ieee80211_assign_link_chanctx(struct ieee80211_link_data *link,
835 					 struct ieee80211_chanctx *new_ctx)
836 {
837 	struct ieee80211_sub_if_data *sdata = link->sdata;
838 	unsigned int link_id = link->link_id;
839 	struct ieee80211_local *local = sdata->local;
840 	struct ieee80211_chanctx_conf *conf;
841 	struct ieee80211_chanctx *curr_ctx = NULL;
842 	int ret = 0;
843 
844 	if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
845 		return -ENOTSUPP;
846 
847 	conf = rcu_dereference_protected(link->conf->chanctx_conf,
848 					 lockdep_is_held(&local->chanctx_mtx));
849 
850 	if (conf) {
851 		curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
852 
853 		drv_unassign_vif_chanctx(local, sdata, link_id, curr_ctx);
854 		conf = NULL;
855 		list_del(&link->assigned_chanctx_list);
856 	}
857 
858 	if (new_ctx) {
859 		ret = drv_assign_vif_chanctx(local, sdata, link_id, new_ctx);
860 		if (ret)
861 			goto out;
862 
863 		conf = &new_ctx->conf;
864 		list_add(&link->assigned_chanctx_list,
865 			 &new_ctx->assigned_links);
866 	}
867 
868 out:
869 	rcu_assign_pointer(link->conf->chanctx_conf, conf);
870 
871 	sdata->vif.cfg.idle = !conf;
872 
873 	if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
874 		ieee80211_recalc_chanctx_chantype(local, curr_ctx);
875 		ieee80211_recalc_smps_chanctx(local, curr_ctx);
876 		ieee80211_recalc_radar_chanctx(local, curr_ctx);
877 		ieee80211_recalc_chanctx_min_def(local, curr_ctx);
878 	}
879 
880 	if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
881 		ieee80211_recalc_txpower(sdata, false);
882 		ieee80211_recalc_chanctx_min_def(local, new_ctx);
883 	}
884 
885 	if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
886 	    sdata->vif.type != NL80211_IFTYPE_MONITOR)
887 		ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_IDLE);
888 
889 	ieee80211_check_fast_xmit_iface(sdata);
890 
891 	return ret;
892 }
893 
894 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
895 				   struct ieee80211_chanctx *chanctx)
896 {
897 	struct ieee80211_sub_if_data *sdata;
898 	u8 rx_chains_static, rx_chains_dynamic;
899 
900 	lockdep_assert_held(&local->chanctx_mtx);
901 
902 	rx_chains_static = 1;
903 	rx_chains_dynamic = 1;
904 
905 	rcu_read_lock();
906 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
907 		u8 needed_static, needed_dynamic;
908 		unsigned int link_id;
909 
910 		if (!ieee80211_sdata_running(sdata))
911 			continue;
912 
913 		switch (sdata->vif.type) {
914 		case NL80211_IFTYPE_STATION:
915 			if (!sdata->u.mgd.associated)
916 				continue;
917 			break;
918 		case NL80211_IFTYPE_AP:
919 		case NL80211_IFTYPE_ADHOC:
920 		case NL80211_IFTYPE_MESH_POINT:
921 		case NL80211_IFTYPE_OCB:
922 			break;
923 		default:
924 			continue;
925 		}
926 
927 		for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
928 			struct ieee80211_link_data *link;
929 
930 			link = rcu_dereference(sdata->link[link_id]);
931 
932 			if (!link)
933 				continue;
934 
935 			if (rcu_access_pointer(link->conf->chanctx_conf) != &chanctx->conf)
936 				continue;
937 
938 			switch (link->smps_mode) {
939 			default:
940 				WARN_ONCE(1, "Invalid SMPS mode %d\n",
941 					  link->smps_mode);
942 				fallthrough;
943 			case IEEE80211_SMPS_OFF:
944 				needed_static = link->needed_rx_chains;
945 				needed_dynamic = link->needed_rx_chains;
946 				break;
947 			case IEEE80211_SMPS_DYNAMIC:
948 				needed_static = 1;
949 				needed_dynamic = link->needed_rx_chains;
950 				break;
951 			case IEEE80211_SMPS_STATIC:
952 				needed_static = 1;
953 				needed_dynamic = 1;
954 				break;
955 			}
956 
957 			rx_chains_static = max(rx_chains_static, needed_static);
958 			rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
959 		}
960 	}
961 
962 	/* Disable SMPS for the monitor interface */
963 	sdata = rcu_dereference(local->monitor_sdata);
964 	if (sdata &&
965 	    rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &chanctx->conf)
966 		rx_chains_dynamic = rx_chains_static = local->rx_chains;
967 
968 	rcu_read_unlock();
969 
970 	if (!local->use_chanctx) {
971 		if (rx_chains_static > 1)
972 			local->smps_mode = IEEE80211_SMPS_OFF;
973 		else if (rx_chains_dynamic > 1)
974 			local->smps_mode = IEEE80211_SMPS_DYNAMIC;
975 		else
976 			local->smps_mode = IEEE80211_SMPS_STATIC;
977 		ieee80211_hw_config(local, 0);
978 	}
979 
980 	if (rx_chains_static == chanctx->conf.rx_chains_static &&
981 	    rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
982 		return;
983 
984 	chanctx->conf.rx_chains_static = rx_chains_static;
985 	chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
986 	drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
987 }
988 
989 static void
990 __ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
991 				       bool clear)
992 {
993 	struct ieee80211_sub_if_data *sdata = link->sdata;
994 	unsigned int link_id = link->link_id;
995 	struct ieee80211_bss_conf *link_conf = link->conf;
996 	struct ieee80211_local *local __maybe_unused = sdata->local;
997 	struct ieee80211_sub_if_data *vlan;
998 	struct ieee80211_chanctx_conf *conf;
999 
1000 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
1001 		return;
1002 
1003 	lockdep_assert_held(&local->mtx);
1004 
1005 	/* Check that conf exists, even when clearing this function
1006 	 * must be called with the AP's channel context still there
1007 	 * as it would otherwise cause VLANs to have an invalid
1008 	 * channel context pointer for a while, possibly pointing
1009 	 * to a channel context that has already been freed.
1010 	 */
1011 	conf = rcu_dereference_protected(link_conf->chanctx_conf,
1012 					 lockdep_is_held(&local->chanctx_mtx));
1013 	WARN_ON(!conf);
1014 
1015 	if (clear)
1016 		conf = NULL;
1017 
1018 	rcu_read_lock();
1019 	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1020 		struct ieee80211_bss_conf *vlan_conf;
1021 
1022 		vlan_conf = rcu_dereference(vlan->vif.link_conf[link_id]);
1023 		if (WARN_ON(!vlan_conf))
1024 			continue;
1025 
1026 		rcu_assign_pointer(vlan_conf->chanctx_conf, conf);
1027 	}
1028 	rcu_read_unlock();
1029 }
1030 
1031 void ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
1032 					  bool clear)
1033 {
1034 	struct ieee80211_local *local = link->sdata->local;
1035 
1036 	mutex_lock(&local->chanctx_mtx);
1037 
1038 	__ieee80211_link_copy_chanctx_to_vlans(link, clear);
1039 
1040 	mutex_unlock(&local->chanctx_mtx);
1041 }
1042 
1043 int ieee80211_link_unreserve_chanctx(struct ieee80211_link_data *link)
1044 {
1045 	struct ieee80211_sub_if_data *sdata = link->sdata;
1046 	struct ieee80211_chanctx *ctx = link->reserved_chanctx;
1047 
1048 	lockdep_assert_held(&sdata->local->chanctx_mtx);
1049 
1050 	if (WARN_ON(!ctx))
1051 		return -EINVAL;
1052 
1053 	list_del(&link->reserved_chanctx_list);
1054 	link->reserved_chanctx = NULL;
1055 
1056 	if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
1057 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1058 			if (WARN_ON(!ctx->replace_ctx))
1059 				return -EINVAL;
1060 
1061 			WARN_ON(ctx->replace_ctx->replace_state !=
1062 			        IEEE80211_CHANCTX_WILL_BE_REPLACED);
1063 			WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
1064 
1065 			ctx->replace_ctx->replace_ctx = NULL;
1066 			ctx->replace_ctx->replace_state =
1067 					IEEE80211_CHANCTX_REPLACE_NONE;
1068 
1069 			list_del_rcu(&ctx->list);
1070 			kfree_rcu(ctx, rcu_head);
1071 		} else {
1072 			ieee80211_free_chanctx(sdata->local, ctx);
1073 		}
1074 	}
1075 
1076 	return 0;
1077 }
1078 
1079 int ieee80211_link_reserve_chanctx(struct ieee80211_link_data *link,
1080 				   const struct cfg80211_chan_def *chandef,
1081 				   enum ieee80211_chanctx_mode mode,
1082 				   bool radar_required)
1083 {
1084 	struct ieee80211_sub_if_data *sdata = link->sdata;
1085 	struct ieee80211_local *local = sdata->local;
1086 	struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
1087 
1088 	lockdep_assert_held(&local->chanctx_mtx);
1089 
1090 	curr_ctx = ieee80211_link_get_chanctx(link);
1091 	if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
1092 		return -ENOTSUPP;
1093 
1094 	new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
1095 	if (!new_ctx) {
1096 		if (ieee80211_can_create_new_chanctx(local)) {
1097 			new_ctx = ieee80211_new_chanctx(local, chandef, mode);
1098 			if (IS_ERR(new_ctx))
1099 				return PTR_ERR(new_ctx);
1100 		} else {
1101 			if (!curr_ctx ||
1102 			    (curr_ctx->replace_state ==
1103 			     IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1104 			    !list_empty(&curr_ctx->reserved_links)) {
1105 				/*
1106 				 * Another link already requested this context
1107 				 * for a reservation. Find another one hoping
1108 				 * all links assigned to it will also switch
1109 				 * soon enough.
1110 				 *
1111 				 * TODO: This needs a little more work as some
1112 				 * cases (more than 2 chanctx capable devices)
1113 				 * may fail which could otherwise succeed
1114 				 * provided some channel context juggling was
1115 				 * performed.
1116 				 *
1117 				 * Consider ctx1..3, link1..6, each ctx has 2
1118 				 * links. link1 and link2 from ctx1 request new
1119 				 * different chandefs starting 2 in-place
1120 				 * reserations with ctx4 and ctx5 replacing
1121 				 * ctx1 and ctx2 respectively. Next link5 and
1122 				 * link6 from ctx3 reserve ctx4. If link3 and
1123 				 * link4 remain on ctx2 as they are then this
1124 				 * fails unless `replace_ctx` from ctx5 is
1125 				 * replaced with ctx3.
1126 				 */
1127 				list_for_each_entry(ctx, &local->chanctx_list,
1128 						    list) {
1129 					if (ctx->replace_state !=
1130 					    IEEE80211_CHANCTX_REPLACE_NONE)
1131 						continue;
1132 
1133 					if (!list_empty(&ctx->reserved_links))
1134 						continue;
1135 
1136 					curr_ctx = ctx;
1137 					break;
1138 				}
1139 			}
1140 
1141 			/*
1142 			 * If that's true then all available contexts already
1143 			 * have reservations and cannot be used.
1144 			 */
1145 			if (!curr_ctx ||
1146 			    (curr_ctx->replace_state ==
1147 			     IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1148 			    !list_empty(&curr_ctx->reserved_links))
1149 				return -EBUSY;
1150 
1151 			new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
1152 			if (!new_ctx)
1153 				return -ENOMEM;
1154 
1155 			new_ctx->replace_ctx = curr_ctx;
1156 			new_ctx->replace_state =
1157 					IEEE80211_CHANCTX_REPLACES_OTHER;
1158 
1159 			curr_ctx->replace_ctx = new_ctx;
1160 			curr_ctx->replace_state =
1161 					IEEE80211_CHANCTX_WILL_BE_REPLACED;
1162 
1163 			list_add_rcu(&new_ctx->list, &local->chanctx_list);
1164 		}
1165 	}
1166 
1167 	list_add(&link->reserved_chanctx_list, &new_ctx->reserved_links);
1168 	link->reserved_chanctx = new_ctx;
1169 	link->reserved_chandef = *chandef;
1170 	link->reserved_radar_required = radar_required;
1171 	link->reserved_ready = false;
1172 
1173 	return 0;
1174 }
1175 
1176 static void
1177 ieee80211_link_chanctx_reservation_complete(struct ieee80211_link_data *link)
1178 {
1179 	struct ieee80211_sub_if_data *sdata = link->sdata;
1180 
1181 	switch (sdata->vif.type) {
1182 	case NL80211_IFTYPE_ADHOC:
1183 	case NL80211_IFTYPE_AP:
1184 	case NL80211_IFTYPE_MESH_POINT:
1185 	case NL80211_IFTYPE_OCB:
1186 		ieee80211_queue_work(&sdata->local->hw,
1187 				     &link->csa_finalize_work);
1188 		break;
1189 	case NL80211_IFTYPE_STATION:
1190 		ieee80211_queue_work(&sdata->local->hw,
1191 				     &link->u.mgd.chswitch_work);
1192 		break;
1193 	case NL80211_IFTYPE_UNSPECIFIED:
1194 	case NL80211_IFTYPE_AP_VLAN:
1195 	case NL80211_IFTYPE_WDS:
1196 	case NL80211_IFTYPE_MONITOR:
1197 	case NL80211_IFTYPE_P2P_CLIENT:
1198 	case NL80211_IFTYPE_P2P_GO:
1199 	case NL80211_IFTYPE_P2P_DEVICE:
1200 	case NL80211_IFTYPE_NAN:
1201 	case NUM_NL80211_IFTYPES:
1202 		WARN_ON(1);
1203 		break;
1204 	}
1205 }
1206 
1207 static void
1208 ieee80211_link_update_chandef(struct ieee80211_link_data *link,
1209 			      const struct cfg80211_chan_def *chandef)
1210 {
1211 	struct ieee80211_sub_if_data *sdata = link->sdata;
1212 	unsigned int link_id = link->link_id;
1213 	struct ieee80211_sub_if_data *vlan;
1214 
1215 	link->conf->chandef = *chandef;
1216 
1217 	if (sdata->vif.type != NL80211_IFTYPE_AP)
1218 		return;
1219 
1220 	rcu_read_lock();
1221 	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1222 		struct ieee80211_bss_conf *vlan_conf;
1223 
1224 		vlan_conf = rcu_dereference(vlan->vif.link_conf[link_id]);
1225 		if (WARN_ON(!vlan_conf))
1226 			continue;
1227 
1228 		vlan_conf->chandef = *chandef;
1229 	}
1230 	rcu_read_unlock();
1231 }
1232 
1233 static int
1234 ieee80211_link_use_reserved_reassign(struct ieee80211_link_data *link)
1235 {
1236 	struct ieee80211_sub_if_data *sdata = link->sdata;
1237 	struct ieee80211_bss_conf *link_conf = link->conf;
1238 	struct ieee80211_local *local = sdata->local;
1239 	struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1240 	struct ieee80211_chanctx *old_ctx, *new_ctx;
1241 	const struct cfg80211_chan_def *chandef;
1242 	u32 changed = 0;
1243 	int err;
1244 
1245 	lockdep_assert_held(&local->mtx);
1246 	lockdep_assert_held(&local->chanctx_mtx);
1247 
1248 	new_ctx = link->reserved_chanctx;
1249 	old_ctx = ieee80211_link_get_chanctx(link);
1250 
1251 	if (WARN_ON(!link->reserved_ready))
1252 		return -EBUSY;
1253 
1254 	if (WARN_ON(!new_ctx))
1255 		return -EINVAL;
1256 
1257 	if (WARN_ON(!old_ctx))
1258 		return -EINVAL;
1259 
1260 	if (WARN_ON(new_ctx->replace_state ==
1261 		    IEEE80211_CHANCTX_REPLACES_OTHER))
1262 		return -EINVAL;
1263 
1264 	chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1265 				&link->reserved_chandef);
1266 	if (WARN_ON(!chandef))
1267 		return -EINVAL;
1268 
1269 	if (link_conf->chandef.width != link->reserved_chandef.width)
1270 		changed = BSS_CHANGED_BANDWIDTH;
1271 
1272 	ieee80211_link_update_chandef(link, &link->reserved_chandef);
1273 
1274 	ieee80211_change_chanctx(local, new_ctx, old_ctx, chandef);
1275 
1276 	vif_chsw[0].vif = &sdata->vif;
1277 	vif_chsw[0].old_ctx = &old_ctx->conf;
1278 	vif_chsw[0].new_ctx = &new_ctx->conf;
1279 	vif_chsw[0].link_id = link->link_id;
1280 
1281 	list_del(&link->reserved_chanctx_list);
1282 	link->reserved_chanctx = NULL;
1283 
1284 	err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1285 				     CHANCTX_SWMODE_REASSIGN_VIF);
1286 	if (err) {
1287 		if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1288 			ieee80211_free_chanctx(local, new_ctx);
1289 
1290 		goto out;
1291 	}
1292 
1293 	list_move(&link->assigned_chanctx_list, &new_ctx->assigned_links);
1294 	rcu_assign_pointer(link_conf->chanctx_conf, &new_ctx->conf);
1295 
1296 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1297 		__ieee80211_link_copy_chanctx_to_vlans(link, false);
1298 
1299 	ieee80211_check_fast_xmit_iface(sdata);
1300 
1301 	if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1302 		ieee80211_free_chanctx(local, old_ctx);
1303 
1304 	ieee80211_recalc_chanctx_min_def(local, new_ctx);
1305 	ieee80211_recalc_smps_chanctx(local, new_ctx);
1306 	ieee80211_recalc_radar_chanctx(local, new_ctx);
1307 
1308 	if (changed)
1309 		ieee80211_link_info_change_notify(sdata, link, changed);
1310 
1311 out:
1312 	ieee80211_link_chanctx_reservation_complete(link);
1313 	return err;
1314 }
1315 
1316 static int
1317 ieee80211_link_use_reserved_assign(struct ieee80211_link_data *link)
1318 {
1319 	struct ieee80211_sub_if_data *sdata = link->sdata;
1320 	struct ieee80211_local *local = sdata->local;
1321 	struct ieee80211_chanctx *old_ctx, *new_ctx;
1322 	const struct cfg80211_chan_def *chandef;
1323 	int err;
1324 
1325 	old_ctx = ieee80211_link_get_chanctx(link);
1326 	new_ctx = link->reserved_chanctx;
1327 
1328 	if (WARN_ON(!link->reserved_ready))
1329 		return -EINVAL;
1330 
1331 	if (WARN_ON(old_ctx))
1332 		return -EINVAL;
1333 
1334 	if (WARN_ON(!new_ctx))
1335 		return -EINVAL;
1336 
1337 	if (WARN_ON(new_ctx->replace_state ==
1338 		    IEEE80211_CHANCTX_REPLACES_OTHER))
1339 		return -EINVAL;
1340 
1341 	chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1342 				&link->reserved_chandef);
1343 	if (WARN_ON(!chandef))
1344 		return -EINVAL;
1345 
1346 	ieee80211_change_chanctx(local, new_ctx, new_ctx, chandef);
1347 
1348 	list_del(&link->reserved_chanctx_list);
1349 	link->reserved_chanctx = NULL;
1350 
1351 	err = ieee80211_assign_link_chanctx(link, new_ctx);
1352 	if (err) {
1353 		if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1354 			ieee80211_free_chanctx(local, new_ctx);
1355 
1356 		goto out;
1357 	}
1358 
1359 out:
1360 	ieee80211_link_chanctx_reservation_complete(link);
1361 	return err;
1362 }
1363 
1364 static bool
1365 ieee80211_link_has_in_place_reservation(struct ieee80211_link_data *link)
1366 {
1367 	struct ieee80211_sub_if_data *sdata = link->sdata;
1368 	struct ieee80211_chanctx *old_ctx, *new_ctx;
1369 
1370 	lockdep_assert_held(&sdata->local->chanctx_mtx);
1371 
1372 	new_ctx = link->reserved_chanctx;
1373 	old_ctx = ieee80211_link_get_chanctx(link);
1374 
1375 	if (!old_ctx)
1376 		return false;
1377 
1378 	if (WARN_ON(!new_ctx))
1379 		return false;
1380 
1381 	if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1382 		return false;
1383 
1384 	if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1385 		return false;
1386 
1387 	return true;
1388 }
1389 
1390 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1391 					struct ieee80211_chanctx *new_ctx)
1392 {
1393 	const struct cfg80211_chan_def *chandef;
1394 
1395 	lockdep_assert_held(&local->mtx);
1396 	lockdep_assert_held(&local->chanctx_mtx);
1397 
1398 	chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1399 	if (WARN_ON(!chandef))
1400 		return -EINVAL;
1401 
1402 	local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1403 	local->_oper_chandef = *chandef;
1404 	ieee80211_hw_config(local, 0);
1405 
1406 	return 0;
1407 }
1408 
1409 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1410 				      int n_vifs)
1411 {
1412 	struct ieee80211_vif_chanctx_switch *vif_chsw;
1413 	struct ieee80211_link_data *link;
1414 	struct ieee80211_chanctx *ctx, *old_ctx;
1415 	int i, err;
1416 
1417 	lockdep_assert_held(&local->mtx);
1418 	lockdep_assert_held(&local->chanctx_mtx);
1419 
1420 	vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1421 	if (!vif_chsw)
1422 		return -ENOMEM;
1423 
1424 	i = 0;
1425 	list_for_each_entry(ctx, &local->chanctx_list, list) {
1426 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1427 			continue;
1428 
1429 		if (WARN_ON(!ctx->replace_ctx)) {
1430 			err = -EINVAL;
1431 			goto out;
1432 		}
1433 
1434 		list_for_each_entry(link, &ctx->reserved_links,
1435 				    reserved_chanctx_list) {
1436 			if (!ieee80211_link_has_in_place_reservation(link))
1437 				continue;
1438 
1439 			old_ctx = ieee80211_link_get_chanctx(link);
1440 			vif_chsw[i].vif = &link->sdata->vif;
1441 			vif_chsw[i].old_ctx = &old_ctx->conf;
1442 			vif_chsw[i].new_ctx = &ctx->conf;
1443 			vif_chsw[i].link_id = link->link_id;
1444 
1445 			i++;
1446 		}
1447 	}
1448 
1449 	err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1450 				     CHANCTX_SWMODE_SWAP_CONTEXTS);
1451 
1452 out:
1453 	kfree(vif_chsw);
1454 	return err;
1455 }
1456 
1457 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1458 {
1459 	struct ieee80211_chanctx *ctx;
1460 	int err;
1461 
1462 	lockdep_assert_held(&local->mtx);
1463 	lockdep_assert_held(&local->chanctx_mtx);
1464 
1465 	list_for_each_entry(ctx, &local->chanctx_list, list) {
1466 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1467 			continue;
1468 
1469 		if (!list_empty(&ctx->replace_ctx->assigned_links))
1470 			continue;
1471 
1472 		ieee80211_del_chanctx(local, ctx->replace_ctx);
1473 		err = ieee80211_add_chanctx(local, ctx);
1474 		if (err)
1475 			goto err;
1476 	}
1477 
1478 	return 0;
1479 
1480 err:
1481 	WARN_ON(ieee80211_add_chanctx(local, ctx));
1482 	list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1483 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1484 			continue;
1485 
1486 		if (!list_empty(&ctx->replace_ctx->assigned_links))
1487 			continue;
1488 
1489 		ieee80211_del_chanctx(local, ctx);
1490 		WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1491 	}
1492 
1493 	return err;
1494 }
1495 
1496 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1497 {
1498 	struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1499 	struct ieee80211_chanctx *new_ctx = NULL;
1500 	int err, n_assigned, n_reserved, n_ready;
1501 	int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1502 
1503 	lockdep_assert_held(&local->mtx);
1504 	lockdep_assert_held(&local->chanctx_mtx);
1505 
1506 	/*
1507 	 * If there are 2 independent pairs of channel contexts performing
1508 	 * cross-switch of their vifs this code will still wait until both are
1509 	 * ready even though it could be possible to switch one before the
1510 	 * other is ready.
1511 	 *
1512 	 * For practical reasons and code simplicity just do a single huge
1513 	 * switch.
1514 	 */
1515 
1516 	/*
1517 	 * Verify if the reservation is still feasible.
1518 	 *  - if it's not then disconnect
1519 	 *  - if it is but not all vifs necessary are ready then defer
1520 	 */
1521 
1522 	list_for_each_entry(ctx, &local->chanctx_list, list) {
1523 		struct ieee80211_link_data *link;
1524 
1525 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1526 			continue;
1527 
1528 		if (WARN_ON(!ctx->replace_ctx)) {
1529 			err = -EINVAL;
1530 			goto err;
1531 		}
1532 
1533 		if (!local->use_chanctx)
1534 			new_ctx = ctx;
1535 
1536 		n_ctx++;
1537 
1538 		n_assigned = 0;
1539 		n_reserved = 0;
1540 		n_ready = 0;
1541 
1542 		list_for_each_entry(link, &ctx->replace_ctx->assigned_links,
1543 				    assigned_chanctx_list) {
1544 			n_assigned++;
1545 			if (link->reserved_chanctx) {
1546 				n_reserved++;
1547 				if (link->reserved_ready)
1548 					n_ready++;
1549 			}
1550 		}
1551 
1552 		if (n_assigned != n_reserved) {
1553 			if (n_ready == n_reserved) {
1554 				wiphy_info(local->hw.wiphy,
1555 					   "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1556 				err = -EBUSY;
1557 				goto err;
1558 			}
1559 
1560 			return -EAGAIN;
1561 		}
1562 
1563 		ctx->conf.radar_enabled = false;
1564 		list_for_each_entry(link, &ctx->reserved_links,
1565 				    reserved_chanctx_list) {
1566 			if (ieee80211_link_has_in_place_reservation(link) &&
1567 			    !link->reserved_ready)
1568 				return -EAGAIN;
1569 
1570 			old_ctx = ieee80211_link_get_chanctx(link);
1571 			if (old_ctx) {
1572 				if (old_ctx->replace_state ==
1573 				    IEEE80211_CHANCTX_WILL_BE_REPLACED)
1574 					n_vifs_switch++;
1575 				else
1576 					n_vifs_assign++;
1577 			} else {
1578 				n_vifs_ctxless++;
1579 			}
1580 
1581 			if (link->reserved_radar_required)
1582 				ctx->conf.radar_enabled = true;
1583 		}
1584 	}
1585 
1586 	if (WARN_ON(n_ctx == 0) ||
1587 	    WARN_ON(n_vifs_switch == 0 &&
1588 		    n_vifs_assign == 0 &&
1589 		    n_vifs_ctxless == 0) ||
1590 	    WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1591 	    WARN_ON(!new_ctx && !local->use_chanctx)) {
1592 		err = -EINVAL;
1593 		goto err;
1594 	}
1595 
1596 	/*
1597 	 * All necessary vifs are ready. Perform the switch now depending on
1598 	 * reservations and driver capabilities.
1599 	 */
1600 
1601 	if (local->use_chanctx) {
1602 		if (n_vifs_switch > 0) {
1603 			err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1604 			if (err)
1605 				goto err;
1606 		}
1607 
1608 		if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1609 			err = ieee80211_chsw_switch_ctxs(local);
1610 			if (err)
1611 				goto err;
1612 		}
1613 	} else {
1614 		err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1615 		if (err)
1616 			goto err;
1617 	}
1618 
1619 	/*
1620 	 * Update all structures, values and pointers to point to new channel
1621 	 * context(s).
1622 	 */
1623 	list_for_each_entry(ctx, &local->chanctx_list, list) {
1624 		struct ieee80211_link_data *link, *link_tmp;
1625 
1626 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1627 			continue;
1628 
1629 		if (WARN_ON(!ctx->replace_ctx)) {
1630 			err = -EINVAL;
1631 			goto err;
1632 		}
1633 
1634 		list_for_each_entry(link, &ctx->reserved_links,
1635 				    reserved_chanctx_list) {
1636 			struct ieee80211_sub_if_data *sdata = link->sdata;
1637 			struct ieee80211_bss_conf *link_conf = link->conf;
1638 			u32 changed = 0;
1639 
1640 			if (!ieee80211_link_has_in_place_reservation(link))
1641 				continue;
1642 
1643 			rcu_assign_pointer(link_conf->chanctx_conf,
1644 					   &ctx->conf);
1645 
1646 			if (sdata->vif.type == NL80211_IFTYPE_AP)
1647 				__ieee80211_link_copy_chanctx_to_vlans(link,
1648 								       false);
1649 
1650 			ieee80211_check_fast_xmit_iface(sdata);
1651 
1652 			link->radar_required = link->reserved_radar_required;
1653 
1654 			if (link_conf->chandef.width != link->reserved_chandef.width)
1655 				changed = BSS_CHANGED_BANDWIDTH;
1656 
1657 			ieee80211_link_update_chandef(link, &link->reserved_chandef);
1658 			if (changed)
1659 				ieee80211_link_info_change_notify(sdata,
1660 								  link,
1661 								  changed);
1662 
1663 			ieee80211_recalc_txpower(sdata, false);
1664 		}
1665 
1666 		ieee80211_recalc_chanctx_chantype(local, ctx);
1667 		ieee80211_recalc_smps_chanctx(local, ctx);
1668 		ieee80211_recalc_radar_chanctx(local, ctx);
1669 		ieee80211_recalc_chanctx_min_def(local, ctx);
1670 
1671 		list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1672 					 reserved_chanctx_list) {
1673 			if (ieee80211_link_get_chanctx(link) != ctx)
1674 				continue;
1675 
1676 			list_del(&link->reserved_chanctx_list);
1677 			list_move(&link->assigned_chanctx_list,
1678 				  &ctx->assigned_links);
1679 			link->reserved_chanctx = NULL;
1680 
1681 			ieee80211_link_chanctx_reservation_complete(link);
1682 		}
1683 
1684 		/*
1685 		 * This context might have been a dependency for an already
1686 		 * ready re-assign reservation interface that was deferred. Do
1687 		 * not propagate error to the caller though. The in-place
1688 		 * reservation for originally requested interface has already
1689 		 * succeeded at this point.
1690 		 */
1691 		list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1692 					 reserved_chanctx_list) {
1693 			if (WARN_ON(ieee80211_link_has_in_place_reservation(link)))
1694 				continue;
1695 
1696 			if (WARN_ON(link->reserved_chanctx != ctx))
1697 				continue;
1698 
1699 			if (!link->reserved_ready)
1700 				continue;
1701 
1702 			if (ieee80211_link_get_chanctx(link))
1703 				err = ieee80211_link_use_reserved_reassign(link);
1704 			else
1705 				err = ieee80211_link_use_reserved_assign(link);
1706 
1707 			if (err) {
1708 				link_info(link,
1709 					  "failed to finalize (re-)assign reservation (err=%d)\n",
1710 					  err);
1711 				ieee80211_link_unreserve_chanctx(link);
1712 				cfg80211_stop_iface(local->hw.wiphy,
1713 						    &link->sdata->wdev,
1714 						    GFP_KERNEL);
1715 			}
1716 		}
1717 	}
1718 
1719 	/*
1720 	 * Finally free old contexts
1721 	 */
1722 
1723 	list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1724 		if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1725 			continue;
1726 
1727 		ctx->replace_ctx->replace_ctx = NULL;
1728 		ctx->replace_ctx->replace_state =
1729 				IEEE80211_CHANCTX_REPLACE_NONE;
1730 
1731 		list_del_rcu(&ctx->list);
1732 		kfree_rcu(ctx, rcu_head);
1733 	}
1734 
1735 	return 0;
1736 
1737 err:
1738 	list_for_each_entry(ctx, &local->chanctx_list, list) {
1739 		struct ieee80211_link_data *link, *link_tmp;
1740 
1741 		if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1742 			continue;
1743 
1744 		list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1745 					 reserved_chanctx_list) {
1746 			ieee80211_link_unreserve_chanctx(link);
1747 			ieee80211_link_chanctx_reservation_complete(link);
1748 		}
1749 	}
1750 
1751 	return err;
1752 }
1753 
1754 static void __ieee80211_link_release_channel(struct ieee80211_link_data *link)
1755 {
1756 	struct ieee80211_sub_if_data *sdata = link->sdata;
1757 	struct ieee80211_bss_conf *link_conf = link->conf;
1758 	struct ieee80211_local *local = sdata->local;
1759 	struct ieee80211_chanctx_conf *conf;
1760 	struct ieee80211_chanctx *ctx;
1761 	bool use_reserved_switch = false;
1762 
1763 	lockdep_assert_held(&local->chanctx_mtx);
1764 
1765 	conf = rcu_dereference_protected(link_conf->chanctx_conf,
1766 					 lockdep_is_held(&local->chanctx_mtx));
1767 	if (!conf)
1768 		return;
1769 
1770 	ctx = container_of(conf, struct ieee80211_chanctx, conf);
1771 
1772 	if (link->reserved_chanctx) {
1773 		if (link->reserved_chanctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
1774 		    ieee80211_chanctx_num_reserved(local, link->reserved_chanctx) > 1)
1775 			use_reserved_switch = true;
1776 
1777 		ieee80211_link_unreserve_chanctx(link);
1778 	}
1779 
1780 	ieee80211_assign_link_chanctx(link, NULL);
1781 	if (ieee80211_chanctx_refcount(local, ctx) == 0)
1782 		ieee80211_free_chanctx(local, ctx);
1783 
1784 	link->radar_required = false;
1785 
1786 	/* Unreserving may ready an in-place reservation. */
1787 	if (use_reserved_switch)
1788 		ieee80211_vif_use_reserved_switch(local);
1789 }
1790 
1791 int ieee80211_link_use_channel(struct ieee80211_link_data *link,
1792 			       const struct cfg80211_chan_def *chandef,
1793 			       enum ieee80211_chanctx_mode mode)
1794 {
1795 	struct ieee80211_sub_if_data *sdata = link->sdata;
1796 	struct ieee80211_local *local = sdata->local;
1797 	struct ieee80211_chanctx *ctx;
1798 	u8 radar_detect_width = 0;
1799 	int ret;
1800 
1801 	lockdep_assert_held(&local->mtx);
1802 
1803 	WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1804 
1805 	mutex_lock(&local->chanctx_mtx);
1806 
1807 	ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1808 					    chandef,
1809 					    sdata->wdev.iftype);
1810 	if (ret < 0)
1811 		goto out;
1812 	if (ret > 0)
1813 		radar_detect_width = BIT(chandef->width);
1814 
1815 	link->radar_required = ret;
1816 
1817 	ret = ieee80211_check_combinations(sdata, chandef, mode,
1818 					   radar_detect_width);
1819 	if (ret < 0)
1820 		goto out;
1821 
1822 	__ieee80211_link_release_channel(link);
1823 
1824 	ctx = ieee80211_find_chanctx(local, chandef, mode);
1825 	if (!ctx)
1826 		ctx = ieee80211_new_chanctx(local, chandef, mode);
1827 	if (IS_ERR(ctx)) {
1828 		ret = PTR_ERR(ctx);
1829 		goto out;
1830 	}
1831 
1832 	ieee80211_link_update_chandef(link, chandef);
1833 
1834 	ret = ieee80211_assign_link_chanctx(link, ctx);
1835 	if (ret) {
1836 		/* if assign fails refcount stays the same */
1837 		if (ieee80211_chanctx_refcount(local, ctx) == 0)
1838 			ieee80211_free_chanctx(local, ctx);
1839 		goto out;
1840 	}
1841 
1842 	ieee80211_recalc_smps_chanctx(local, ctx);
1843 	ieee80211_recalc_radar_chanctx(local, ctx);
1844  out:
1845 	if (ret)
1846 		link->radar_required = false;
1847 
1848 	mutex_unlock(&local->chanctx_mtx);
1849 	return ret;
1850 }
1851 
1852 int ieee80211_link_use_reserved_context(struct ieee80211_link_data *link)
1853 {
1854 	struct ieee80211_sub_if_data *sdata = link->sdata;
1855 	struct ieee80211_local *local = sdata->local;
1856 	struct ieee80211_chanctx *new_ctx;
1857 	struct ieee80211_chanctx *old_ctx;
1858 	int err;
1859 
1860 	lockdep_assert_held(&local->mtx);
1861 	lockdep_assert_held(&local->chanctx_mtx);
1862 
1863 	new_ctx = link->reserved_chanctx;
1864 	old_ctx = ieee80211_link_get_chanctx(link);
1865 
1866 	if (WARN_ON(!new_ctx))
1867 		return -EINVAL;
1868 
1869 	if (WARN_ON(new_ctx->replace_state ==
1870 		    IEEE80211_CHANCTX_WILL_BE_REPLACED))
1871 		return -EINVAL;
1872 
1873 	if (WARN_ON(link->reserved_ready))
1874 		return -EINVAL;
1875 
1876 	link->reserved_ready = true;
1877 
1878 	if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1879 		if (old_ctx)
1880 			return ieee80211_link_use_reserved_reassign(link);
1881 
1882 		return ieee80211_link_use_reserved_assign(link);
1883 	}
1884 
1885 	/*
1886 	 * In-place reservation may need to be finalized now either if:
1887 	 *  a) sdata is taking part in the swapping itself and is the last one
1888 	 *  b) sdata has switched with a re-assign reservation to an existing
1889 	 *     context readying in-place switching of old_ctx
1890 	 *
1891 	 * In case of (b) do not propagate the error up because the requested
1892 	 * sdata already switched successfully. Just spill an extra warning.
1893 	 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1894 	 * interfaces upon failure.
1895 	 */
1896 	if ((old_ctx &&
1897 	     old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1898 	    new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1899 		err = ieee80211_vif_use_reserved_switch(local);
1900 		if (err && err != -EAGAIN) {
1901 			if (new_ctx->replace_state ==
1902 			    IEEE80211_CHANCTX_REPLACES_OTHER)
1903 				return err;
1904 
1905 			wiphy_info(local->hw.wiphy,
1906 				   "depending in-place reservation failed (err=%d)\n",
1907 				   err);
1908 		}
1909 	}
1910 
1911 	return 0;
1912 }
1913 
1914 int ieee80211_link_change_bandwidth(struct ieee80211_link_data *link,
1915 				    const struct cfg80211_chan_def *chandef,
1916 				    u32 *changed)
1917 {
1918 	struct ieee80211_sub_if_data *sdata = link->sdata;
1919 	struct ieee80211_bss_conf *link_conf = link->conf;
1920 	struct ieee80211_local *local = sdata->local;
1921 	struct ieee80211_chanctx_conf *conf;
1922 	struct ieee80211_chanctx *ctx;
1923 	const struct cfg80211_chan_def *compat;
1924 	int ret;
1925 
1926 	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1927 				     IEEE80211_CHAN_DISABLED))
1928 		return -EINVAL;
1929 
1930 	mutex_lock(&local->chanctx_mtx);
1931 	if (cfg80211_chandef_identical(chandef, &link_conf->chandef)) {
1932 		ret = 0;
1933 		goto out;
1934 	}
1935 
1936 	if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1937 	    link_conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1938 		ret = -EINVAL;
1939 		goto out;
1940 	}
1941 
1942 	conf = rcu_dereference_protected(link_conf->chanctx_conf,
1943 					 lockdep_is_held(&local->chanctx_mtx));
1944 	if (!conf) {
1945 		ret = -EINVAL;
1946 		goto out;
1947 	}
1948 
1949 	ctx = container_of(conf, struct ieee80211_chanctx, conf);
1950 
1951 	compat = cfg80211_chandef_compatible(&conf->def, chandef);
1952 	if (!compat) {
1953 		ret = -EINVAL;
1954 		goto out;
1955 	}
1956 
1957 	switch (ctx->replace_state) {
1958 	case IEEE80211_CHANCTX_REPLACE_NONE:
1959 		if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1960 			ret = -EBUSY;
1961 			goto out;
1962 		}
1963 		break;
1964 	case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1965 		/* TODO: Perhaps the bandwidth change could be treated as a
1966 		 * reservation itself? */
1967 		ret = -EBUSY;
1968 		goto out;
1969 	case IEEE80211_CHANCTX_REPLACES_OTHER:
1970 		/* channel context that is going to replace another channel
1971 		 * context doesn't really exist and shouldn't be assigned
1972 		 * anywhere yet */
1973 		WARN_ON(1);
1974 		break;
1975 	}
1976 
1977 	ieee80211_link_update_chandef(link, chandef);
1978 
1979 	ieee80211_recalc_chanctx_chantype(local, ctx);
1980 
1981 	*changed |= BSS_CHANGED_BANDWIDTH;
1982 	ret = 0;
1983  out:
1984 	mutex_unlock(&local->chanctx_mtx);
1985 	return ret;
1986 }
1987 
1988 void ieee80211_link_release_channel(struct ieee80211_link_data *link)
1989 {
1990 	struct ieee80211_sub_if_data *sdata = link->sdata;
1991 
1992 	WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1993 
1994 	mutex_lock(&sdata->local->chanctx_mtx);
1995 	if (rcu_access_pointer(link->conf->chanctx_conf)) {
1996 		lockdep_assert_held(&sdata->local->mtx);
1997 		__ieee80211_link_release_channel(link);
1998 	}
1999 	mutex_unlock(&sdata->local->chanctx_mtx);
2000 }
2001 
2002 void ieee80211_link_vlan_copy_chanctx(struct ieee80211_link_data *link)
2003 {
2004 	struct ieee80211_sub_if_data *sdata = link->sdata;
2005 	unsigned int link_id = link->link_id;
2006 	struct ieee80211_bss_conf *link_conf = link->conf;
2007 	struct ieee80211_bss_conf *ap_conf;
2008 	struct ieee80211_local *local = sdata->local;
2009 	struct ieee80211_sub_if_data *ap;
2010 	struct ieee80211_chanctx_conf *conf;
2011 
2012 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
2013 		return;
2014 
2015 	ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
2016 
2017 	mutex_lock(&local->chanctx_mtx);
2018 
2019 	rcu_read_lock();
2020 	ap_conf = rcu_dereference(ap->vif.link_conf[link_id]);
2021 	conf = rcu_dereference_protected(ap_conf->chanctx_conf,
2022 					 lockdep_is_held(&local->chanctx_mtx));
2023 	rcu_assign_pointer(link_conf->chanctx_conf, conf);
2024 	rcu_read_unlock();
2025 	mutex_unlock(&local->chanctx_mtx);
2026 }
2027 
2028 void ieee80211_iter_chan_contexts_atomic(
2029 	struct ieee80211_hw *hw,
2030 	void (*iter)(struct ieee80211_hw *hw,
2031 		     struct ieee80211_chanctx_conf *chanctx_conf,
2032 		     void *data),
2033 	void *iter_data)
2034 {
2035 	struct ieee80211_local *local = hw_to_local(hw);
2036 	struct ieee80211_chanctx *ctx;
2037 
2038 	rcu_read_lock();
2039 	list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
2040 		if (ctx->driver_present)
2041 			iter(hw, &ctx->conf, iter_data);
2042 	rcu_read_unlock();
2043 }
2044 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);
2045