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