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