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