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