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