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