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