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