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