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 = __ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for, 755 check_reserved); 756 757 /* check is BW narrowed */ 758 ieee80211_chan_bw_change(local, ctx, false, true); 759 760 if (changed) 761 drv_change_chanctx(local, ctx, changed); 762 763 /* check is BW wider */ 764 ieee80211_chan_bw_change(local, ctx, false, false); 765 } 766 767 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local, 768 struct ieee80211_chanctx *ctx) 769 { 770 _ieee80211_recalc_chanctx_min_def(local, ctx, NULL, false); 771 } 772 773 static void 774 ieee80211_chanctx_update_npca_links(struct ieee80211_local *local, 775 struct ieee80211_chanctx *ctx, 776 bool enable) 777 { 778 struct ieee80211_chanctx_user_iter iter; 779 780 if (!!ctx->conf.def.npca_chan != enable) 781 return; 782 783 for_each_chanctx_user_assigned(local, ctx, &iter) { 784 if (!iter.link) 785 continue; 786 if (!iter.sdata->vif.cfg.assoc) 787 continue; 788 789 if (enable) { 790 if (!iter.link->conf->chanreq.oper.npca_chan) 791 continue; 792 } else { 793 if (!iter.link->conf->npca.enabled) 794 continue; 795 } 796 797 iter.link->conf->npca.enabled = enable; 798 drv_link_info_changed(local, iter.sdata, 799 iter.link->conf, 800 iter.link->link_id, 801 BSS_CHANGED_NPCA); 802 } 803 } 804 805 static void _ieee80211_change_chanctx(struct ieee80211_local *local, 806 struct ieee80211_chanctx *ctx, 807 struct ieee80211_chanctx *old_ctx, 808 const struct ieee80211_chan_req *chanreq, 809 struct ieee80211_link_data *rsvd_for) 810 { 811 struct ieee80211_chan_req ctx_req = { 812 .oper = ctx->conf.def, 813 .ap = ctx->conf.ap, 814 }; 815 u32 changed = 0; 816 817 /* 5/10 MHz not handled here */ 818 switch (chanreq->oper.width) { 819 case NL80211_CHAN_WIDTH_1: 820 case NL80211_CHAN_WIDTH_2: 821 case NL80211_CHAN_WIDTH_4: 822 case NL80211_CHAN_WIDTH_8: 823 case NL80211_CHAN_WIDTH_16: 824 /* 825 * mac80211 currently only supports sharing identical 826 * chanctx's for S1G interfaces. 827 */ 828 WARN_ON(!ieee80211_chanreq_identical(&ctx_req, chanreq)); 829 return; 830 case NL80211_CHAN_WIDTH_20_NOHT: 831 case NL80211_CHAN_WIDTH_20: 832 case NL80211_CHAN_WIDTH_40: 833 case NL80211_CHAN_WIDTH_80: 834 case NL80211_CHAN_WIDTH_80P80: 835 case NL80211_CHAN_WIDTH_160: 836 case NL80211_CHAN_WIDTH_320: 837 break; 838 default: 839 WARN_ON(1); 840 } 841 842 /* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def 843 * due to maybe not returning from it, e.g in case new context was added 844 * first time with all parameters up to date. 845 */ 846 ieee80211_chan_bw_change(local, old_ctx, false, true); 847 848 if (ieee80211_chanreq_identical(&ctx_req, chanreq)) { 849 _ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for, false); 850 return; 851 } 852 853 WARN_ON(ieee80211_chanctx_refcount(local, ctx) > 1 && 854 !cfg80211_chandef_compatible(&ctx->conf.def, &chanreq->oper)); 855 856 ieee80211_remove_wbrf(local, &ctx->conf.def); 857 858 if (!cfg80211_chandef_identical(&ctx->conf.def, &chanreq->oper)) { 859 if (ctx->conf.def.width != chanreq->oper.width) 860 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH; 861 if (ctx->conf.def.punctured != chanreq->oper.punctured) 862 changed |= IEEE80211_CHANCTX_CHANGE_PUNCTURING; 863 if (ctx->conf.def.npca_chan != chanreq->oper.npca_chan) 864 changed |= IEEE80211_CHANCTX_CHANGE_NPCA; 865 if (chanreq->oper.npca_chan && 866 ctx->conf.def.npca_punctured != chanreq->oper.npca_punctured) 867 changed |= IEEE80211_CHANCTX_CHANGE_NPCA_PUNCT; 868 } 869 if (!cfg80211_chandef_identical(&ctx->conf.ap, &chanreq->ap)) 870 changed |= IEEE80211_CHANCTX_CHANGE_AP; 871 ctx->conf.def = chanreq->oper; 872 ctx->conf.ap = chanreq->ap; 873 874 /* check if min chanctx also changed */ 875 changed |= __ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for, 876 false); 877 878 ieee80211_add_wbrf(local, &ctx->conf.def); 879 880 /* disable NPCA on the link using it */ 881 ieee80211_chanctx_update_npca_links(local, ctx, false); 882 883 drv_change_chanctx(local, ctx, changed); 884 885 /* check if BW is wider */ 886 ieee80211_chan_bw_change(local, old_ctx, false, false); 887 888 /* enable NPCA on the link that requested it */ 889 ieee80211_chanctx_update_npca_links(local, ctx, true); 890 } 891 892 static void ieee80211_change_chanctx(struct ieee80211_local *local, 893 struct ieee80211_chanctx *ctx, 894 struct ieee80211_chanctx *old_ctx, 895 const struct ieee80211_chan_req *chanreq) 896 { 897 _ieee80211_change_chanctx(local, ctx, old_ctx, chanreq, NULL); 898 } 899 900 /* Note: if successful, the returned chanctx will_be_used flag is set */ 901 static struct ieee80211_chanctx * 902 ieee80211_find_chanctx(struct ieee80211_local *local, 903 const struct ieee80211_chan_req *chanreq, 904 enum ieee80211_chanctx_mode mode) 905 { 906 struct ieee80211_chan_req tmp; 907 struct ieee80211_chanctx *ctx; 908 909 lockdep_assert_wiphy(local->hw.wiphy); 910 911 if (mode == IEEE80211_CHANCTX_EXCLUSIVE) 912 return NULL; 913 914 list_for_each_entry(ctx, &local->chanctx_list, list) { 915 const struct ieee80211_chan_req *compat; 916 917 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE) 918 continue; 919 920 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) 921 continue; 922 923 compat = ieee80211_chanctx_compatible(local, ctx, chanreq, 924 &tmp); 925 if (!compat) 926 continue; 927 928 compat = ieee80211_chanctx_reserved_chanreq(local, ctx, 929 compat, &tmp); 930 if (!compat) 931 continue; 932 933 /* 934 * Mark the chanctx as will be used, as the driver might change 935 * active links during callbacks we make into it below and/or 936 * later during assignment, which could (otherwise) cause the 937 * context to actually be removed. 938 */ 939 ctx->will_be_used = true; 940 941 ieee80211_change_chanctx(local, ctx, ctx, compat); 942 943 return ctx; 944 } 945 946 return NULL; 947 } 948 949 bool ieee80211_is_radar_required(struct ieee80211_local *local, 950 struct cfg80211_scan_request *req) 951 { 952 struct wiphy *wiphy = local->hw.wiphy; 953 struct ieee80211_link_data *link; 954 struct ieee80211_channel *chan; 955 int radio_idx; 956 957 lockdep_assert_wiphy(local->hw.wiphy); 958 959 if (!req) 960 return false; 961 962 for_each_sdata_link(local, link) { 963 if (link->radar_required) { 964 chan = link->conf->chanreq.oper.chan; 965 radio_idx = cfg80211_get_radio_idx_by_chan(wiphy, chan); 966 967 if (ieee80211_is_radio_idx_in_scan_req(wiphy, req, 968 radio_idx)) 969 return true; 970 } 971 } 972 973 return false; 974 } 975 976 static bool 977 ieee80211_chanctx_radar_required(struct ieee80211_local *local, 978 struct ieee80211_chanctx *ctx) 979 { 980 struct ieee80211_chanctx_user_iter iter; 981 982 lockdep_assert_wiphy(local->hw.wiphy); 983 984 for_each_chanctx_user_assigned(local, ctx, &iter) { 985 if (iter.radar_required) 986 return true; 987 } 988 989 return false; 990 } 991 992 static struct ieee80211_chanctx * 993 ieee80211_alloc_chanctx(struct ieee80211_local *local, 994 const struct ieee80211_chan_req *chanreq, 995 enum ieee80211_chanctx_mode mode, 996 int radio_idx) 997 { 998 struct ieee80211_chanctx *ctx; 999 1000 lockdep_assert_wiphy(local->hw.wiphy); 1001 1002 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); 1003 if (!ctx) 1004 return NULL; 1005 1006 ctx->conf.def = chanreq->oper; 1007 ctx->conf.ap = chanreq->ap; 1008 ctx->conf.rx_chains_static = 1; 1009 ctx->conf.rx_chains_dynamic = 1; 1010 ctx->mode = mode; 1011 ctx->conf.radar_enabled = false; 1012 ctx->conf.radio_idx = radio_idx; 1013 ctx->radar_detected = false; 1014 __ieee80211_recalc_chanctx_min_def(local, ctx, NULL, false); 1015 1016 return ctx; 1017 } 1018 1019 static int ieee80211_add_chanctx(struct ieee80211_local *local, 1020 struct ieee80211_chanctx *ctx) 1021 { 1022 u32 changed; 1023 int err; 1024 1025 lockdep_assert_wiphy(local->hw.wiphy); 1026 1027 ieee80211_add_wbrf(local, &ctx->conf.def); 1028 1029 /* turn idle off *before* setting channel -- some drivers need that */ 1030 changed = ieee80211_idle_off(local); 1031 if (changed) 1032 ieee80211_hw_config(local, -1, changed); 1033 1034 err = drv_add_chanctx(local, ctx); 1035 if (err) { 1036 ieee80211_recalc_idle(local); 1037 return err; 1038 } 1039 1040 return 0; 1041 } 1042 1043 static struct ieee80211_chanctx * 1044 ieee80211_new_chanctx(struct ieee80211_local *local, 1045 const struct ieee80211_chan_req *chanreq, 1046 enum ieee80211_chanctx_mode mode, 1047 bool assign_on_failure, 1048 int radio_idx) 1049 { 1050 struct ieee80211_chanctx *ctx; 1051 int err; 1052 1053 lockdep_assert_wiphy(local->hw.wiphy); 1054 1055 ctx = ieee80211_alloc_chanctx(local, chanreq, mode, radio_idx); 1056 if (!ctx) 1057 return ERR_PTR(-ENOMEM); 1058 1059 err = ieee80211_add_chanctx(local, ctx); 1060 if (!assign_on_failure && err) { 1061 kfree(ctx); 1062 return ERR_PTR(err); 1063 } 1064 /* 1065 * We ignored a driver error, see _ieee80211_set_active_links and/or 1066 * ieee80211_nan_set_local_sched 1067 */ 1068 WARN_ON_ONCE(err && !local->in_reconfig); 1069 1070 list_add_rcu(&ctx->list, &local->chanctx_list); 1071 return ctx; 1072 } 1073 1074 static void ieee80211_del_chanctx(struct ieee80211_local *local, 1075 struct ieee80211_chanctx *ctx, 1076 bool skip_idle_recalc) 1077 { 1078 lockdep_assert_wiphy(local->hw.wiphy); 1079 1080 drv_remove_chanctx(local, ctx); 1081 1082 if (!skip_idle_recalc) 1083 ieee80211_recalc_idle(local); 1084 1085 ieee80211_remove_wbrf(local, &ctx->conf.def); 1086 } 1087 1088 void ieee80211_free_chanctx(struct ieee80211_local *local, 1089 struct ieee80211_chanctx *ctx, 1090 bool skip_idle_recalc) 1091 { 1092 lockdep_assert_wiphy(local->hw.wiphy); 1093 1094 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0); 1095 1096 list_del_rcu(&ctx->list); 1097 ieee80211_del_chanctx(local, ctx, skip_idle_recalc); 1098 kfree_rcu(ctx, rcu_head); 1099 } 1100 1101 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, 1102 struct ieee80211_chanctx *ctx) 1103 { 1104 struct ieee80211_chanctx_conf *conf = &ctx->conf; 1105 const struct ieee80211_chan_req *compat = NULL; 1106 struct ieee80211_chanctx_user_iter iter; 1107 struct ieee80211_chan_req tmp; 1108 struct sta_info *sta; 1109 1110 lockdep_assert_wiphy(local->hw.wiphy); 1111 1112 for_each_chanctx_user_assigned(local, ctx, &iter) { 1113 if (!compat) 1114 compat = iter.chanreq; 1115 1116 compat = ieee80211_chanreq_compatible(iter.chanreq, 1117 compat, &tmp); 1118 if (WARN_ON_ONCE(!compat)) 1119 return; 1120 } 1121 1122 if (WARN_ON_ONCE(!compat)) 1123 return; 1124 1125 /* TDLS peers can sometimes affect the chandef width */ 1126 list_for_each_entry(sta, &local->sta_list, list) { 1127 struct ieee80211_sub_if_data *sdata = sta->sdata; 1128 struct ieee80211_chan_req tdls_chanreq = {}; 1129 struct ieee80211_link_data *link; 1130 int tdls_link_id; 1131 1132 if (!sta->uploaded || 1133 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) || 1134 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) || 1135 !sta->tdls_chandef.chan) 1136 continue; 1137 1138 tdls_link_id = ieee80211_tdls_sta_link_id(sta); 1139 link = sdata_dereference(sdata->link[tdls_link_id], sdata); 1140 if (!link) 1141 continue; 1142 1143 if (rcu_access_pointer(link->conf->chanctx_conf) != conf) 1144 continue; 1145 1146 tdls_chanreq.oper = sta->tdls_chandef; 1147 1148 /* note this always fills and returns &tmp if compat */ 1149 compat = ieee80211_chanreq_compatible(&tdls_chanreq, 1150 compat, &tmp); 1151 if (WARN_ON_ONCE(!compat)) 1152 return; 1153 } 1154 1155 ieee80211_change_chanctx(local, ctx, ctx, compat); 1156 } 1157 1158 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, 1159 struct ieee80211_chanctx *chanctx) 1160 { 1161 bool radar_enabled; 1162 1163 lockdep_assert_wiphy(local->hw.wiphy); 1164 1165 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx); 1166 1167 if (radar_enabled == chanctx->conf.radar_enabled) 1168 return; 1169 1170 chanctx->conf.radar_enabled = radar_enabled; 1171 1172 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); 1173 } 1174 1175 static int ieee80211_assign_link_chanctx(struct ieee80211_link_data *link, 1176 struct ieee80211_chanctx *new_ctx, 1177 bool assign_on_failure) 1178 { 1179 struct ieee80211_sub_if_data *sdata = link->sdata; 1180 struct ieee80211_local *local = sdata->local; 1181 struct ieee80211_chanctx_conf *conf; 1182 struct ieee80211_chanctx *curr_ctx = NULL; 1183 bool new_idle; 1184 int ret; 1185 1186 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN)) 1187 return -EOPNOTSUPP; 1188 1189 conf = rcu_dereference_protected(link->conf->chanctx_conf, 1190 lockdep_is_held(&local->hw.wiphy->mtx)); 1191 1192 if (conf && !local->in_reconfig) { 1193 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); 1194 1195 drv_unassign_vif_chanctx(local, sdata, link->conf, curr_ctx); 1196 conf = NULL; 1197 } 1198 1199 if (new_ctx) { 1200 /* recalc considering the link we'll use it for now */ 1201 _ieee80211_recalc_chanctx_min_def(local, new_ctx, link, false); 1202 1203 ret = drv_assign_vif_chanctx(local, sdata, link->conf, new_ctx); 1204 if (assign_on_failure || !ret) { 1205 /* Need to continue, see _ieee80211_set_active_links */ 1206 WARN_ON_ONCE(ret && !local->in_reconfig); 1207 ret = 0; 1208 1209 /* succeeded, so commit it to the data structures */ 1210 conf = &new_ctx->conf; 1211 } 1212 } else { 1213 ret = 0; 1214 } 1215 1216 rcu_assign_pointer(link->conf->chanctx_conf, conf); 1217 1218 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) { 1219 ieee80211_recalc_chanctx_chantype(local, curr_ctx); 1220 ieee80211_recalc_smps_chanctx(local, curr_ctx); 1221 ieee80211_recalc_radar_chanctx(local, curr_ctx); 1222 ieee80211_recalc_chanctx_min_def(local, curr_ctx); 1223 } 1224 1225 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) { 1226 ieee80211_recalc_txpower(link, false); 1227 ieee80211_recalc_chanctx_min_def(local, new_ctx); 1228 } 1229 1230 if (conf) { 1231 new_idle = false; 1232 } else { 1233 struct ieee80211_link_data *tmp; 1234 1235 new_idle = true; 1236 for_each_sdata_link(local, tmp) { 1237 if (rcu_access_pointer(tmp->conf->chanctx_conf)) { 1238 new_idle = false; 1239 break; 1240 } 1241 } 1242 } 1243 1244 if (new_idle != sdata->vif.cfg.idle) { 1245 sdata->vif.cfg.idle = new_idle; 1246 1247 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && 1248 sdata->vif.type != NL80211_IFTYPE_MONITOR) 1249 ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_IDLE); 1250 } 1251 1252 ieee80211_check_fast_xmit_iface(sdata); 1253 1254 return ret; 1255 } 1256 1257 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, 1258 struct ieee80211_chanctx *chanctx) 1259 { 1260 struct ieee80211_chanctx_user_iter iter; 1261 struct ieee80211_sub_if_data *sdata; 1262 u8 rx_chains_static, rx_chains_dynamic; 1263 1264 lockdep_assert_wiphy(local->hw.wiphy); 1265 1266 rx_chains_static = 1; 1267 rx_chains_dynamic = 1; 1268 1269 for_each_chanctx_user_assigned(local, chanctx, &iter) { 1270 u8 needed_static, needed_dynamic; 1271 1272 switch (iter.iftype) { 1273 case NL80211_IFTYPE_STATION: 1274 if (!iter.sdata->u.mgd.associated) 1275 continue; 1276 break; 1277 case NL80211_IFTYPE_MONITOR: 1278 if (!ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) 1279 continue; 1280 break; 1281 case NL80211_IFTYPE_AP: 1282 case NL80211_IFTYPE_ADHOC: 1283 case NL80211_IFTYPE_MESH_POINT: 1284 case NL80211_IFTYPE_OCB: 1285 case NL80211_IFTYPE_NAN: 1286 break; 1287 default: 1288 continue; 1289 } 1290 1291 if (iter.iftype == NL80211_IFTYPE_MONITOR) { 1292 rx_chains_dynamic = rx_chains_static = local->rx_chains; 1293 break; 1294 } 1295 1296 if (iter.nan_channel) { 1297 rx_chains_dynamic = rx_chains_static = 1298 iter.nan_channel->needed_rx_chains; 1299 break; 1300 } 1301 1302 if (!iter.link) 1303 continue; 1304 1305 switch (iter.link->smps_mode) { 1306 default: 1307 WARN_ONCE(1, "Invalid SMPS mode %d\n", 1308 iter.link->smps_mode); 1309 fallthrough; 1310 case IEEE80211_SMPS_OFF: 1311 needed_static = iter.link->needed_rx_chains; 1312 needed_dynamic = iter.link->needed_rx_chains; 1313 break; 1314 case IEEE80211_SMPS_DYNAMIC: 1315 needed_static = 1; 1316 needed_dynamic = iter.link->needed_rx_chains; 1317 break; 1318 case IEEE80211_SMPS_STATIC: 1319 needed_static = 1; 1320 needed_dynamic = 1; 1321 break; 1322 } 1323 1324 rx_chains_static = max(rx_chains_static, needed_static); 1325 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); 1326 } 1327 1328 /* Disable SMPS for the monitor interface */ 1329 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata); 1330 if (sdata && 1331 rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &chanctx->conf) 1332 rx_chains_dynamic = rx_chains_static = local->rx_chains; 1333 1334 if (rx_chains_static == chanctx->conf.rx_chains_static && 1335 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) 1336 return; 1337 1338 chanctx->conf.rx_chains_static = rx_chains_static; 1339 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; 1340 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); 1341 } 1342 1343 static void 1344 __ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link, 1345 bool clear) 1346 { 1347 struct ieee80211_sub_if_data *sdata = link->sdata; 1348 unsigned int link_id = link->link_id; 1349 struct ieee80211_bss_conf *link_conf = link->conf; 1350 struct ieee80211_local *local __maybe_unused = sdata->local; 1351 struct ieee80211_sub_if_data *vlan; 1352 struct ieee80211_chanctx_conf *conf; 1353 1354 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) 1355 return; 1356 1357 lockdep_assert_wiphy(local->hw.wiphy); 1358 1359 /* Check that conf exists, even when clearing this function 1360 * must be called with the AP's channel context still there 1361 * as it would otherwise cause VLANs to have an invalid 1362 * channel context pointer for a while, possibly pointing 1363 * to a channel context that has already been freed. 1364 */ 1365 conf = rcu_dereference_protected(link_conf->chanctx_conf, 1366 lockdep_is_held(&local->hw.wiphy->mtx)); 1367 WARN_ON(!conf); 1368 1369 if (clear) 1370 conf = NULL; 1371 1372 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) { 1373 struct ieee80211_bss_conf *vlan_conf; 1374 1375 if (vlan->vif.valid_links && 1376 !(vlan->vif.valid_links & BIT(link_id))) 1377 continue; 1378 1379 vlan_conf = wiphy_dereference(local->hw.wiphy, 1380 vlan->vif.link_conf[link_id]); 1381 if (WARN_ON(!vlan_conf)) 1382 continue; 1383 1384 rcu_assign_pointer(vlan_conf->chanctx_conf, conf); 1385 } 1386 } 1387 1388 void ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link, 1389 bool clear) 1390 { 1391 struct ieee80211_local *local = link->sdata->local; 1392 1393 lockdep_assert_wiphy(local->hw.wiphy); 1394 1395 __ieee80211_link_copy_chanctx_to_vlans(link, clear); 1396 } 1397 1398 void ieee80211_link_unreserve_chanctx(struct ieee80211_link_data *link) 1399 { 1400 struct ieee80211_sub_if_data *sdata = link->sdata; 1401 struct ieee80211_chanctx *ctx = link->reserved_chanctx; 1402 1403 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1404 1405 if (WARN_ON(!ctx)) 1406 return; 1407 1408 link->reserved_chanctx = NULL; 1409 1410 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) { 1411 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { 1412 if (WARN_ON(!ctx->replace_ctx)) 1413 return; 1414 1415 WARN_ON(ctx->replace_ctx->replace_state != 1416 IEEE80211_CHANCTX_WILL_BE_REPLACED); 1417 WARN_ON(ctx->replace_ctx->replace_ctx != ctx); 1418 1419 ctx->replace_ctx->replace_ctx = NULL; 1420 ctx->replace_ctx->replace_state = 1421 IEEE80211_CHANCTX_REPLACE_NONE; 1422 1423 list_del_rcu(&ctx->list); 1424 kfree_rcu(ctx, rcu_head); 1425 } else { 1426 ieee80211_free_chanctx(sdata->local, ctx, false); 1427 } 1428 } 1429 } 1430 1431 static struct ieee80211_chanctx * 1432 ieee80211_replace_chanctx(struct ieee80211_local *local, 1433 const struct ieee80211_chan_req *chanreq, 1434 enum ieee80211_chanctx_mode mode, 1435 struct ieee80211_chanctx *curr_ctx) 1436 { 1437 struct ieee80211_chanctx *new_ctx, *ctx; 1438 struct wiphy *wiphy = local->hw.wiphy; 1439 const struct wiphy_radio *radio; 1440 1441 if (!curr_ctx || 1442 curr_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED || 1443 ieee80211_chanctx_num_reserved(local, curr_ctx) != 0) { 1444 /* 1445 * Another link already requested this context for a 1446 * reservation. Find another one hoping all links assigned 1447 * to it will also switch soon enough. 1448 * 1449 * TODO: This needs a little more work as some cases 1450 * (more than 2 chanctx capable devices) may fail which could 1451 * otherwise succeed provided some channel context juggling was 1452 * performed. 1453 * 1454 * Consider ctx1..3, link1..6, each ctx has 2 links. link1 and 1455 * link2 from ctx1 request new different chandefs starting 2 1456 * in-place reservations with ctx4 and ctx5 replacing ctx1 and 1457 * ctx2 respectively. Next link5 and link6 from ctx3 reserve 1458 * ctx4. If link3 and link4 remain on ctx2 as they are then this 1459 * fails unless `replace_ctx` from ctx5 is replaced with ctx3. 1460 */ 1461 list_for_each_entry(ctx, &local->chanctx_list, list) { 1462 if (ctx->replace_state != 1463 IEEE80211_CHANCTX_REPLACE_NONE) 1464 continue; 1465 1466 if (ieee80211_chanctx_num_reserved(local, ctx) != 0) 1467 continue; 1468 1469 if (ctx->conf.radio_idx >= 0) { 1470 radio = &wiphy->radio[ctx->conf.radio_idx]; 1471 if (!cfg80211_radio_chandef_valid(radio, &chanreq->oper)) 1472 continue; 1473 } 1474 1475 curr_ctx = ctx; 1476 break; 1477 } 1478 } 1479 1480 /* 1481 * If that's true then all available contexts already have reservations 1482 * and cannot be used. 1483 */ 1484 if (!curr_ctx || 1485 curr_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED || 1486 ieee80211_chanctx_num_reserved(local, curr_ctx) != 0) 1487 return ERR_PTR(-EBUSY); 1488 1489 new_ctx = ieee80211_alloc_chanctx(local, chanreq, mode, -1); 1490 if (!new_ctx) 1491 return ERR_PTR(-ENOMEM); 1492 1493 new_ctx->replace_ctx = curr_ctx; 1494 new_ctx->replace_state = IEEE80211_CHANCTX_REPLACES_OTHER; 1495 1496 curr_ctx->replace_ctx = new_ctx; 1497 curr_ctx->replace_state = IEEE80211_CHANCTX_WILL_BE_REPLACED; 1498 1499 list_add_rcu(&new_ctx->list, &local->chanctx_list); 1500 1501 return new_ctx; 1502 } 1503 1504 static bool 1505 ieee80211_find_available_radio(struct ieee80211_local *local, 1506 const struct ieee80211_chan_req *chanreq, 1507 u32 radio_mask, int *radio_idx) 1508 { 1509 struct wiphy *wiphy = local->hw.wiphy; 1510 const struct wiphy_radio *radio; 1511 int i; 1512 1513 *radio_idx = -1; 1514 if (!wiphy->n_radio) 1515 return true; 1516 1517 for (i = 0; i < wiphy->n_radio; i++) { 1518 if (!(radio_mask & BIT(i))) 1519 continue; 1520 1521 radio = &wiphy->radio[i]; 1522 if (!cfg80211_radio_chandef_valid(radio, &chanreq->oper)) 1523 continue; 1524 1525 if (!ieee80211_can_create_new_chanctx(local, i)) 1526 continue; 1527 1528 *radio_idx = i; 1529 return true; 1530 } 1531 1532 return false; 1533 } 1534 1535 int ieee80211_link_reserve_chanctx(struct ieee80211_link_data *link, 1536 const struct ieee80211_chan_req *chanreq, 1537 enum ieee80211_chanctx_mode mode, 1538 bool radar_required) 1539 { 1540 struct ieee80211_sub_if_data *sdata = link->sdata; 1541 struct ieee80211_local *local = sdata->local; 1542 struct ieee80211_chanctx *new_ctx, *curr_ctx; 1543 int radio_idx; 1544 1545 lockdep_assert_wiphy(local->hw.wiphy); 1546 1547 curr_ctx = ieee80211_link_get_chanctx(link); 1548 if (curr_ctx && !local->ops->switch_vif_chanctx) 1549 return -EOPNOTSUPP; 1550 1551 new_ctx = ieee80211_find_reservation_chanctx(local, chanreq, mode); 1552 if (!new_ctx) { 1553 if (ieee80211_can_create_new_chanctx(local, -1) && 1554 ieee80211_find_available_radio(local, chanreq, 1555 sdata->wdev.radio_mask, 1556 &radio_idx)) 1557 new_ctx = ieee80211_new_chanctx(local, chanreq, mode, 1558 false, radio_idx); 1559 else 1560 new_ctx = ieee80211_replace_chanctx(local, chanreq, 1561 mode, curr_ctx); 1562 if (IS_ERR(new_ctx)) 1563 return PTR_ERR(new_ctx); 1564 } 1565 1566 link->reserved_chanctx = new_ctx; 1567 link->reserved = *chanreq; 1568 link->reserved_radar_required = radar_required; 1569 link->reserved_ready = false; 1570 1571 return 0; 1572 } 1573 1574 static void 1575 ieee80211_link_chanctx_reservation_complete(struct ieee80211_link_data *link) 1576 { 1577 struct ieee80211_sub_if_data *sdata = link->sdata; 1578 1579 switch (sdata->vif.type) { 1580 case NL80211_IFTYPE_ADHOC: 1581 case NL80211_IFTYPE_AP: 1582 case NL80211_IFTYPE_MESH_POINT: 1583 case NL80211_IFTYPE_OCB: 1584 wiphy_work_queue(sdata->local->hw.wiphy, 1585 &link->csa.finalize_work); 1586 break; 1587 case NL80211_IFTYPE_STATION: 1588 wiphy_hrtimer_work_queue(sdata->local->hw.wiphy, 1589 &link->u.mgd.csa.switch_work, 0); 1590 break; 1591 case NL80211_IFTYPE_UNSPECIFIED: 1592 case NL80211_IFTYPE_AP_VLAN: 1593 case NL80211_IFTYPE_WDS: 1594 case NL80211_IFTYPE_MONITOR: 1595 case NL80211_IFTYPE_P2P_CLIENT: 1596 case NL80211_IFTYPE_P2P_GO: 1597 case NL80211_IFTYPE_P2P_DEVICE: 1598 case NL80211_IFTYPE_NAN: 1599 case NL80211_IFTYPE_NAN_DATA: 1600 case NL80211_IFTYPE_PD: 1601 case NUM_NL80211_IFTYPES: 1602 WARN_ON(1); 1603 break; 1604 } 1605 } 1606 1607 static void 1608 ieee80211_link_update_chanreq(struct ieee80211_link_data *link, 1609 const struct ieee80211_chan_req *chanreq) 1610 { 1611 struct ieee80211_sub_if_data *sdata = link->sdata; 1612 unsigned int link_id = link->link_id; 1613 struct ieee80211_sub_if_data *vlan; 1614 1615 link->conf->chanreq = *chanreq; 1616 1617 if (sdata->vif.type != NL80211_IFTYPE_AP) 1618 return; 1619 1620 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) { 1621 struct ieee80211_bss_conf *vlan_conf; 1622 1623 if (vlan->vif.valid_links && 1624 !(vlan->vif.valid_links & BIT(link_id))) 1625 continue; 1626 1627 vlan_conf = wiphy_dereference(sdata->local->hw.wiphy, 1628 vlan->vif.link_conf[link_id]); 1629 if (WARN_ON(!vlan_conf)) 1630 continue; 1631 1632 vlan_conf->chanreq = *chanreq; 1633 } 1634 } 1635 1636 static int 1637 ieee80211_link_use_reserved_reassign(struct ieee80211_link_data *link) 1638 { 1639 struct ieee80211_sub_if_data *sdata = link->sdata; 1640 struct ieee80211_bss_conf *link_conf = link->conf; 1641 struct ieee80211_local *local = sdata->local; 1642 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {}; 1643 struct ieee80211_chanctx *old_ctx, *new_ctx; 1644 const struct ieee80211_chan_req *chanreq; 1645 struct ieee80211_chan_req tmp; 1646 u64 changed = 0; 1647 int err; 1648 1649 lockdep_assert_wiphy(local->hw.wiphy); 1650 1651 new_ctx = link->reserved_chanctx; 1652 old_ctx = ieee80211_link_get_chanctx(link); 1653 1654 if (WARN_ON(!link->reserved_ready)) 1655 return -EBUSY; 1656 1657 if (WARN_ON(!new_ctx)) 1658 return -EINVAL; 1659 1660 if (WARN_ON(!old_ctx)) 1661 return -EINVAL; 1662 1663 if (WARN_ON(new_ctx->replace_state == 1664 IEEE80211_CHANCTX_REPLACES_OTHER)) 1665 return -EINVAL; 1666 1667 chanreq = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, 1668 &link->reserved, 1669 &tmp); 1670 if (WARN_ON(!chanreq)) 1671 return -EINVAL; 1672 1673 if (link_conf->chanreq.oper.width != link->reserved.oper.width) 1674 changed = BSS_CHANGED_BANDWIDTH; 1675 1676 ieee80211_link_update_chanreq(link, &link->reserved); 1677 1678 _ieee80211_change_chanctx(local, new_ctx, old_ctx, chanreq, link); 1679 1680 vif_chsw[0].vif = &sdata->vif; 1681 vif_chsw[0].old_ctx = &old_ctx->conf; 1682 vif_chsw[0].new_ctx = &new_ctx->conf; 1683 vif_chsw[0].link_conf = link->conf; 1684 1685 link->reserved_chanctx = NULL; 1686 1687 err = drv_switch_vif_chanctx(local, vif_chsw, 1, 1688 CHANCTX_SWMODE_REASSIGN_VIF); 1689 if (err) { 1690 if (ieee80211_chanctx_refcount(local, new_ctx) == 0) 1691 ieee80211_free_chanctx(local, new_ctx, false); 1692 1693 goto out; 1694 } 1695 1696 link->radar_required = link->reserved_radar_required; 1697 rcu_assign_pointer(link_conf->chanctx_conf, &new_ctx->conf); 1698 1699 if (sdata->vif.type == NL80211_IFTYPE_AP) 1700 __ieee80211_link_copy_chanctx_to_vlans(link, false); 1701 1702 ieee80211_check_fast_xmit_iface(sdata); 1703 1704 if (ieee80211_chanctx_refcount(local, old_ctx) == 0) 1705 ieee80211_free_chanctx(local, old_ctx, false); 1706 1707 ieee80211_recalc_chanctx_min_def(local, new_ctx); 1708 ieee80211_recalc_smps_chanctx(local, new_ctx); 1709 ieee80211_recalc_radar_chanctx(local, new_ctx); 1710 1711 if (changed) 1712 ieee80211_link_info_change_notify(sdata, link, changed); 1713 1714 out: 1715 ieee80211_link_chanctx_reservation_complete(link); 1716 return err; 1717 } 1718 1719 static int 1720 ieee80211_link_use_reserved_assign(struct ieee80211_link_data *link) 1721 { 1722 struct ieee80211_sub_if_data *sdata = link->sdata; 1723 struct ieee80211_local *local = sdata->local; 1724 struct ieee80211_chanctx *old_ctx, *new_ctx; 1725 const struct ieee80211_chan_req *chanreq; 1726 struct ieee80211_chan_req tmp; 1727 int err; 1728 1729 old_ctx = ieee80211_link_get_chanctx(link); 1730 new_ctx = link->reserved_chanctx; 1731 1732 if (WARN_ON(!link->reserved_ready)) 1733 return -EINVAL; 1734 1735 if (WARN_ON(old_ctx)) 1736 return -EINVAL; 1737 1738 if (WARN_ON(!new_ctx)) 1739 return -EINVAL; 1740 1741 if (WARN_ON(new_ctx->replace_state == 1742 IEEE80211_CHANCTX_REPLACES_OTHER)) 1743 return -EINVAL; 1744 1745 chanreq = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, 1746 &link->reserved, 1747 &tmp); 1748 if (WARN_ON(!chanreq)) 1749 return -EINVAL; 1750 1751 ieee80211_change_chanctx(local, new_ctx, new_ctx, chanreq); 1752 1753 link->reserved_chanctx = NULL; 1754 1755 err = ieee80211_assign_link_chanctx(link, new_ctx, false); 1756 if (err) { 1757 if (ieee80211_chanctx_refcount(local, new_ctx) == 0) 1758 ieee80211_free_chanctx(local, new_ctx, false); 1759 1760 goto out; 1761 } 1762 1763 out: 1764 ieee80211_link_chanctx_reservation_complete(link); 1765 return err; 1766 } 1767 1768 static bool 1769 ieee80211_link_has_in_place_reservation(struct ieee80211_link_data *link) 1770 { 1771 struct ieee80211_sub_if_data *sdata = link->sdata; 1772 struct ieee80211_chanctx *old_ctx, *new_ctx; 1773 1774 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1775 1776 new_ctx = link->reserved_chanctx; 1777 old_ctx = ieee80211_link_get_chanctx(link); 1778 1779 if (!old_ctx) 1780 return false; 1781 1782 if (WARN_ON(!new_ctx)) 1783 return false; 1784 1785 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) 1786 return false; 1787 1788 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1789 return false; 1790 1791 return true; 1792 } 1793 1794 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local, 1795 int n_vifs) 1796 { 1797 struct ieee80211_vif_chanctx_switch *vif_chsw; 1798 struct ieee80211_chanctx *ctx, *old_ctx; 1799 int i, err; 1800 1801 lockdep_assert_wiphy(local->hw.wiphy); 1802 1803 vif_chsw = kzalloc_objs(vif_chsw[0], n_vifs); 1804 if (!vif_chsw) 1805 return -ENOMEM; 1806 1807 i = 0; 1808 list_for_each_entry(ctx, &local->chanctx_list, list) { 1809 struct ieee80211_chanctx_user_iter iter; 1810 1811 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1812 continue; 1813 1814 if (WARN_ON(!ctx->replace_ctx)) { 1815 err = -EINVAL; 1816 goto out; 1817 } 1818 1819 for_each_chanctx_user_reserved(local, ctx, &iter) { 1820 if (!ieee80211_link_has_in_place_reservation(iter.link)) 1821 continue; 1822 1823 old_ctx = ieee80211_link_get_chanctx(iter.link); 1824 vif_chsw[i].vif = &iter.sdata->vif; 1825 vif_chsw[i].old_ctx = &old_ctx->conf; 1826 vif_chsw[i].new_ctx = &ctx->conf; 1827 vif_chsw[i].link_conf = iter.link->conf; 1828 1829 i++; 1830 } 1831 } 1832 1833 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs, 1834 CHANCTX_SWMODE_SWAP_CONTEXTS); 1835 1836 out: 1837 kfree(vif_chsw); 1838 return err; 1839 } 1840 1841 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local) 1842 { 1843 struct ieee80211_chanctx *ctx; 1844 int err; 1845 1846 lockdep_assert_wiphy(local->hw.wiphy); 1847 1848 list_for_each_entry(ctx, &local->chanctx_list, list) { 1849 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1850 continue; 1851 1852 if (ieee80211_chanctx_num_assigned(local, ctx) != 0) 1853 continue; 1854 1855 ieee80211_del_chanctx(local, ctx->replace_ctx, false); 1856 err = ieee80211_add_chanctx(local, ctx); 1857 if (err) 1858 goto err; 1859 } 1860 1861 return 0; 1862 1863 err: 1864 WARN_ON(ieee80211_add_chanctx(local, ctx)); 1865 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) { 1866 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1867 continue; 1868 1869 if (ieee80211_chanctx_num_assigned(local, ctx) != 0) 1870 continue; 1871 1872 ieee80211_del_chanctx(local, ctx, false); 1873 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx)); 1874 } 1875 1876 return err; 1877 } 1878 1879 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local) 1880 { 1881 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx; 1882 int err, n_assigned, n_reserved, n_ready; 1883 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0; 1884 1885 lockdep_assert_wiphy(local->hw.wiphy); 1886 1887 /* 1888 * If there are 2 independent pairs of channel contexts performing 1889 * cross-switch of their vifs this code will still wait until both are 1890 * ready even though it could be possible to switch one before the 1891 * other is ready. 1892 * 1893 * For practical reasons and code simplicity just do a single huge 1894 * switch. 1895 */ 1896 1897 /* 1898 * Verify if the reservation is still feasible. 1899 * - if it's not then disconnect 1900 * - if it is but not all vifs necessary are ready then defer 1901 */ 1902 1903 list_for_each_entry(ctx, &local->chanctx_list, list) { 1904 struct ieee80211_chanctx_user_iter iter; 1905 1906 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1907 continue; 1908 1909 if (WARN_ON(!ctx->replace_ctx)) { 1910 err = -EINVAL; 1911 goto err; 1912 } 1913 1914 n_ctx++; 1915 1916 n_assigned = 0; 1917 n_reserved = 0; 1918 n_ready = 0; 1919 1920 for_each_chanctx_user_assigned(local, ctx->replace_ctx, &iter) { 1921 n_assigned++; 1922 if (iter.link && iter.link->reserved_chanctx) { 1923 n_reserved++; 1924 if (iter.link->reserved_ready) 1925 n_ready++; 1926 } 1927 } 1928 1929 if (n_assigned != n_reserved) { 1930 if (n_ready != n_reserved) 1931 return -EAGAIN; 1932 1933 if (n_assigned == n_reserved + 1 && 1934 ieee80211_nan_try_evacuate(&local->hw, 1935 &ctx->replace_ctx->conf)) 1936 goto use_reserved; 1937 1938 wiphy_info(local->hw.wiphy, 1939 "channel context reservation cannot be finalized because some interfaces aren't switching\n"); 1940 err = -EBUSY; 1941 goto err; 1942 } 1943 1944 use_reserved: 1945 ctx->conf.radar_enabled = false; 1946 for_each_chanctx_user_reserved(local, ctx, &iter) { 1947 if (ieee80211_link_has_in_place_reservation(iter.link) && 1948 !iter.link->reserved_ready) 1949 return -EAGAIN; 1950 1951 old_ctx = ieee80211_link_get_chanctx(iter.link); 1952 if (old_ctx) { 1953 if (old_ctx->replace_state == 1954 IEEE80211_CHANCTX_WILL_BE_REPLACED) 1955 n_vifs_switch++; 1956 else 1957 n_vifs_assign++; 1958 } else { 1959 n_vifs_ctxless++; 1960 } 1961 1962 if (iter.radar_required) 1963 ctx->conf.radar_enabled = true; 1964 } 1965 } 1966 1967 if (WARN_ON(n_ctx == 0) || 1968 WARN_ON(n_vifs_switch == 0 && 1969 n_vifs_assign == 0 && 1970 n_vifs_ctxless == 0)) { 1971 err = -EINVAL; 1972 goto err; 1973 } 1974 1975 /* update station rate control and min width before switch */ 1976 list_for_each_entry(ctx, &local->chanctx_list, list) { 1977 struct ieee80211_chanctx_user_iter iter; 1978 1979 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1980 continue; 1981 1982 if (WARN_ON(!ctx->replace_ctx)) { 1983 err = -EINVAL; 1984 goto err; 1985 } 1986 1987 for_each_chanctx_user_reserved(local, ctx, &iter) { 1988 if (!ieee80211_link_has_in_place_reservation(iter.link)) 1989 continue; 1990 1991 ieee80211_chan_bw_change(local, 1992 ieee80211_link_get_chanctx(iter.link), 1993 true, true); 1994 } 1995 1996 _ieee80211_recalc_chanctx_min_def(local, ctx, NULL, true); 1997 } 1998 1999 /* 2000 * All necessary vifs are ready. Perform the switch now depending on 2001 * reservations and driver capabilities. 2002 */ 2003 2004 if (n_vifs_switch > 0) { 2005 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch); 2006 if (err) 2007 goto err; 2008 } 2009 2010 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) { 2011 err = ieee80211_chsw_switch_ctxs(local); 2012 if (err) 2013 goto err; 2014 } 2015 2016 /* 2017 * Update all structures, values and pointers to point to new channel 2018 * context(s). 2019 */ 2020 list_for_each_entry(ctx, &local->chanctx_list, list) { 2021 struct ieee80211_chanctx_user_iter iter; 2022 2023 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 2024 continue; 2025 2026 if (WARN_ON(!ctx->replace_ctx)) { 2027 err = -EINVAL; 2028 goto err; 2029 } 2030 2031 for_each_chanctx_user_reserved(local, ctx, &iter) { 2032 struct ieee80211_link_data *link = iter.link; 2033 struct ieee80211_sub_if_data *sdata = iter.sdata; 2034 struct ieee80211_bss_conf *link_conf = link->conf; 2035 u64 changed = 0; 2036 2037 if (!ieee80211_link_has_in_place_reservation(link)) 2038 continue; 2039 2040 rcu_assign_pointer(link_conf->chanctx_conf, 2041 &ctx->conf); 2042 2043 if (sdata->vif.type == NL80211_IFTYPE_AP) 2044 __ieee80211_link_copy_chanctx_to_vlans(link, 2045 false); 2046 2047 ieee80211_check_fast_xmit_iface(sdata); 2048 2049 link->radar_required = iter.radar_required; 2050 2051 if (link_conf->chanreq.oper.width != iter.chanreq->oper.width) 2052 changed = BSS_CHANGED_BANDWIDTH; 2053 2054 ieee80211_link_update_chanreq(link, &link->reserved); 2055 if (changed) 2056 ieee80211_link_info_change_notify(sdata, 2057 link, 2058 changed); 2059 2060 ieee80211_recalc_txpower(link, false); 2061 } 2062 2063 ieee80211_recalc_chanctx_chantype(local, ctx); 2064 ieee80211_recalc_smps_chanctx(local, ctx); 2065 ieee80211_recalc_radar_chanctx(local, ctx); 2066 ieee80211_recalc_chanctx_min_def(local, ctx); 2067 2068 for_each_chanctx_user_reserved(local, ctx, &iter) { 2069 if (ieee80211_link_get_chanctx(iter.link) != ctx) 2070 continue; 2071 2072 iter.link->reserved_chanctx = NULL; 2073 2074 ieee80211_link_chanctx_reservation_complete(iter.link); 2075 ieee80211_chan_bw_change(local, ctx, false, false); 2076 } 2077 2078 /* 2079 * This context might have been a dependency for an already 2080 * ready re-assign reservation interface that was deferred. Do 2081 * not propagate error to the caller though. The in-place 2082 * reservation for originally requested interface has already 2083 * succeeded at this point. 2084 */ 2085 for_each_chanctx_user_reserved(local, ctx, &iter) { 2086 struct ieee80211_link_data *link = iter.link; 2087 2088 if (WARN_ON(ieee80211_link_has_in_place_reservation(link))) 2089 continue; 2090 2091 if (!link->reserved_ready) 2092 continue; 2093 2094 if (ieee80211_link_get_chanctx(link)) 2095 err = ieee80211_link_use_reserved_reassign(link); 2096 else 2097 err = ieee80211_link_use_reserved_assign(link); 2098 2099 if (err) { 2100 link_info(link, 2101 "failed to finalize (re-)assign reservation (err=%d)\n", 2102 err); 2103 ieee80211_link_unreserve_chanctx(link); 2104 cfg80211_stop_iface(local->hw.wiphy, 2105 &link->sdata->wdev, 2106 GFP_KERNEL); 2107 } 2108 } 2109 } 2110 2111 /* 2112 * Finally free old contexts 2113 */ 2114 2115 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) { 2116 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) 2117 continue; 2118 2119 ctx->replace_ctx->replace_ctx = NULL; 2120 ctx->replace_ctx->replace_state = 2121 IEEE80211_CHANCTX_REPLACE_NONE; 2122 2123 list_del_rcu(&ctx->list); 2124 kfree_rcu(ctx, rcu_head); 2125 } 2126 2127 return 0; 2128 2129 err: 2130 list_for_each_entry(ctx, &local->chanctx_list, list) { 2131 struct ieee80211_chanctx_user_iter iter; 2132 2133 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 2134 continue; 2135 2136 for_each_chanctx_user_reserved(local, ctx, &iter) { 2137 ieee80211_link_unreserve_chanctx(iter.link); 2138 ieee80211_link_chanctx_reservation_complete(iter.link); 2139 } 2140 } 2141 2142 return err; 2143 } 2144 2145 void __ieee80211_link_release_channel(struct ieee80211_link_data *link, 2146 bool skip_idle_recalc) 2147 { 2148 struct ieee80211_sub_if_data *sdata = link->sdata; 2149 struct ieee80211_bss_conf *link_conf = link->conf; 2150 struct ieee80211_local *local = sdata->local; 2151 struct ieee80211_chanctx_conf *conf; 2152 struct ieee80211_chanctx *ctx; 2153 bool use_reserved_switch = false; 2154 2155 lockdep_assert_wiphy(local->hw.wiphy); 2156 2157 conf = rcu_dereference_protected(link_conf->chanctx_conf, 2158 lockdep_is_held(&local->hw.wiphy->mtx)); 2159 if (!conf) 2160 return; 2161 2162 ctx = container_of(conf, struct ieee80211_chanctx, conf); 2163 2164 if (link->reserved_chanctx) { 2165 if (link->reserved_chanctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER && 2166 ieee80211_chanctx_num_reserved(local, link->reserved_chanctx) > 1) 2167 use_reserved_switch = true; 2168 2169 ieee80211_link_unreserve_chanctx(link); 2170 } 2171 2172 ieee80211_assign_link_chanctx(link, NULL, false); 2173 if (ieee80211_chanctx_refcount(local, ctx) == 0) 2174 ieee80211_free_chanctx(local, ctx, skip_idle_recalc); 2175 2176 link->radar_required = false; 2177 2178 /* Unreserving may ready an in-place reservation. */ 2179 if (use_reserved_switch) 2180 ieee80211_vif_use_reserved_switch(local); 2181 } 2182 2183 struct ieee80211_chanctx * 2184 ieee80211_find_or_create_chanctx(struct ieee80211_sub_if_data *sdata, 2185 const struct ieee80211_chan_req *chanreq, 2186 enum ieee80211_chanctx_mode mode, 2187 bool assign_on_failure, 2188 bool *reused_ctx) 2189 { 2190 struct ieee80211_local *local = sdata->local; 2191 struct ieee80211_chanctx *ctx; 2192 int radio_idx; 2193 2194 lockdep_assert_wiphy(local->hw.wiphy); 2195 2196 ctx = ieee80211_find_chanctx(local, chanreq, mode); 2197 if (ctx) { 2198 *reused_ctx = true; 2199 return ctx; 2200 } 2201 2202 *reused_ctx = false; 2203 2204 if (!ieee80211_find_available_radio(local, chanreq, 2205 sdata->wdev.radio_mask, 2206 &radio_idx)) 2207 return ERR_PTR(-EBUSY); 2208 2209 return ieee80211_new_chanctx(local, chanreq, mode, 2210 assign_on_failure, radio_idx); 2211 } 2212 2213 int _ieee80211_link_use_channel(struct ieee80211_link_data *link, 2214 const struct ieee80211_chan_req *chanreq, 2215 enum ieee80211_chanctx_mode mode, 2216 bool assign_on_failure) 2217 { 2218 struct ieee80211_sub_if_data *sdata = link->sdata; 2219 struct ieee80211_local *local = sdata->local; 2220 struct ieee80211_chanctx *ctx; 2221 u8 radar_detect_width = 0; 2222 bool reused_ctx = false; 2223 int ret; 2224 2225 lockdep_assert_wiphy(local->hw.wiphy); 2226 2227 if (!ieee80211_vif_link_active(&sdata->vif, link->link_id)) { 2228 ieee80211_link_update_chanreq(link, chanreq); 2229 return 0; 2230 } 2231 2232 ret = cfg80211_chandef_dfs_required(local->hw.wiphy, 2233 &chanreq->oper, 2234 sdata->wdev.iftype); 2235 if (ret < 0) 2236 goto out; 2237 if (ret > 0) 2238 radar_detect_width = BIT(chanreq->oper.width); 2239 2240 link->radar_required = ret; 2241 2242 ret = ieee80211_check_combinations(sdata, &chanreq->oper, mode, 2243 radar_detect_width, -1); 2244 if (ret < 0) 2245 goto out; 2246 2247 if (!local->in_reconfig) 2248 __ieee80211_link_release_channel(link, false); 2249 2250 ctx = ieee80211_find_or_create_chanctx(sdata, chanreq, mode, 2251 assign_on_failure, &reused_ctx); 2252 if (IS_ERR(ctx)) { 2253 /* Try to evacuate a NAN channel to free up a chanctx */ 2254 if (ieee80211_nan_try_evacuate(&local->hw, NULL)) 2255 ctx = ieee80211_find_or_create_chanctx(sdata, chanreq, 2256 mode, 2257 assign_on_failure, 2258 &reused_ctx); 2259 } 2260 2261 if (IS_ERR(ctx)) { 2262 ret = PTR_ERR(ctx); 2263 goto out; 2264 } 2265 2266 ieee80211_link_update_chanreq(link, chanreq); 2267 2268 ret = ieee80211_assign_link_chanctx(link, ctx, assign_on_failure); 2269 2270 /* 2271 * In case an existing channel context is being used, we marked it as 2272 * will_be_used, now that it is assigned - clear this indication 2273 */ 2274 if (reused_ctx) { 2275 WARN_ON(!ctx->will_be_used); 2276 ctx->will_be_used = false; 2277 } 2278 2279 if (ret) { 2280 /* if assign fails refcount stays the same */ 2281 if (ieee80211_chanctx_refcount(local, ctx) == 0) 2282 ieee80211_free_chanctx(local, ctx, false); 2283 goto out; 2284 } 2285 2286 ieee80211_recalc_smps_chanctx(local, ctx); 2287 ieee80211_recalc_radar_chanctx(local, ctx); 2288 out: 2289 if (ret) 2290 link->radar_required = false; 2291 2292 return ret; 2293 } 2294 2295 int ieee80211_link_use_reserved_context(struct ieee80211_link_data *link) 2296 { 2297 struct ieee80211_sub_if_data *sdata = link->sdata; 2298 struct ieee80211_local *local = sdata->local; 2299 struct ieee80211_chanctx *new_ctx; 2300 struct ieee80211_chanctx *old_ctx; 2301 int err; 2302 2303 lockdep_assert_wiphy(local->hw.wiphy); 2304 2305 new_ctx = link->reserved_chanctx; 2306 old_ctx = ieee80211_link_get_chanctx(link); 2307 2308 if (WARN_ON(!new_ctx)) 2309 return -EINVAL; 2310 2311 if (WARN_ON(new_ctx->replace_state == 2312 IEEE80211_CHANCTX_WILL_BE_REPLACED)) 2313 return -EINVAL; 2314 2315 if (WARN_ON(link->reserved_ready)) 2316 return -EINVAL; 2317 2318 link->reserved_ready = true; 2319 2320 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) { 2321 if (old_ctx) 2322 return ieee80211_link_use_reserved_reassign(link); 2323 2324 return ieee80211_link_use_reserved_assign(link); 2325 } 2326 2327 /* 2328 * In-place reservation may need to be finalized now either if: 2329 * a) sdata is taking part in the swapping itself and is the last one 2330 * b) sdata has switched with a re-assign reservation to an existing 2331 * context readying in-place switching of old_ctx 2332 * 2333 * In case of (b) do not propagate the error up because the requested 2334 * sdata already switched successfully. Just spill an extra warning. 2335 * The ieee80211_vif_use_reserved_switch() already stops all necessary 2336 * interfaces upon failure. 2337 */ 2338 if ((old_ctx && 2339 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) || 2340 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { 2341 err = ieee80211_vif_use_reserved_switch(local); 2342 if (err && err != -EAGAIN) { 2343 if (new_ctx->replace_state == 2344 IEEE80211_CHANCTX_REPLACES_OTHER) 2345 return err; 2346 2347 wiphy_info(local->hw.wiphy, 2348 "depending in-place reservation failed (err=%d)\n", 2349 err); 2350 } 2351 } 2352 2353 return 0; 2354 } 2355 2356 int ieee80211_link_change_chanreq(struct ieee80211_link_data *link, 2357 const struct ieee80211_chan_req *chanreq, 2358 u64 *changed) 2359 { 2360 struct ieee80211_sub_if_data *sdata = link->sdata; 2361 struct ieee80211_bss_conf *link_conf = link->conf; 2362 struct ieee80211_local *local = sdata->local; 2363 struct ieee80211_chanctx_conf *conf; 2364 struct ieee80211_chanctx *ctx; 2365 const struct ieee80211_chan_req *compat; 2366 struct ieee80211_chan_req tmp; 2367 2368 lockdep_assert_wiphy(local->hw.wiphy); 2369 2370 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, 2371 &chanreq->oper, 2372 IEEE80211_CHAN_DISABLED)) 2373 return -EINVAL; 2374 2375 /* for non-HT 20 MHz the rest doesn't matter */ 2376 if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT && 2377 cfg80211_chandef_identical(&chanreq->oper, &link_conf->chanreq.oper)) 2378 return 0; 2379 2380 /* but you cannot switch to/from it */ 2381 if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT || 2382 link_conf->chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT) 2383 return -EINVAL; 2384 2385 conf = rcu_dereference_protected(link_conf->chanctx_conf, 2386 lockdep_is_held(&local->hw.wiphy->mtx)); 2387 if (!conf) 2388 return -EINVAL; 2389 2390 ctx = container_of(conf, struct ieee80211_chanctx, conf); 2391 2392 compat = _ieee80211_chanctx_compatible(local, link, ctx, chanreq, &tmp); 2393 if (!compat) 2394 return -EINVAL; 2395 2396 switch (ctx->replace_state) { 2397 case IEEE80211_CHANCTX_REPLACE_NONE: 2398 if (!ieee80211_chanctx_reserved_chanreq(local, ctx, compat, 2399 &tmp)) 2400 return -EBUSY; 2401 break; 2402 case IEEE80211_CHANCTX_WILL_BE_REPLACED: 2403 /* TODO: Perhaps the bandwidth change could be treated as a 2404 * reservation itself? */ 2405 return -EBUSY; 2406 case IEEE80211_CHANCTX_REPLACES_OTHER: 2407 /* channel context that is going to replace another channel 2408 * context doesn't really exist and shouldn't be assigned 2409 * anywhere yet */ 2410 WARN_ON(1); 2411 break; 2412 } 2413 2414 ieee80211_link_update_chanreq(link, chanreq); 2415 2416 ieee80211_recalc_chanctx_chantype(local, ctx); 2417 2418 *changed |= BSS_CHANGED_BANDWIDTH; 2419 return 0; 2420 } 2421 2422 void ieee80211_link_release_channel(struct ieee80211_link_data *link) 2423 { 2424 struct ieee80211_sub_if_data *sdata = link->sdata; 2425 2426 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 2427 return; 2428 2429 lockdep_assert_wiphy(sdata->local->hw.wiphy); 2430 2431 if (rcu_access_pointer(link->conf->chanctx_conf)) 2432 __ieee80211_link_release_channel(link, false); 2433 } 2434 2435 void ieee80211_link_vlan_copy_chanctx(struct ieee80211_link_data *link) 2436 { 2437 struct ieee80211_sub_if_data *sdata = link->sdata; 2438 unsigned int link_id = link->link_id; 2439 struct ieee80211_bss_conf *link_conf = link->conf; 2440 struct ieee80211_bss_conf *ap_conf; 2441 struct ieee80211_local *local = sdata->local; 2442 struct ieee80211_sub_if_data *ap; 2443 struct ieee80211_chanctx_conf *conf; 2444 2445 lockdep_assert_wiphy(local->hw.wiphy); 2446 2447 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) 2448 return; 2449 2450 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); 2451 2452 ap_conf = wiphy_dereference(local->hw.wiphy, 2453 ap->vif.link_conf[link_id]); 2454 conf = wiphy_dereference(local->hw.wiphy, 2455 ap_conf->chanctx_conf); 2456 rcu_assign_pointer(link_conf->chanctx_conf, conf); 2457 } 2458 2459 void ieee80211_iter_chan_contexts_atomic( 2460 struct ieee80211_hw *hw, 2461 void (*iter)(struct ieee80211_hw *hw, 2462 struct ieee80211_chanctx_conf *chanctx_conf, 2463 void *data), 2464 void *iter_data) 2465 { 2466 struct ieee80211_local *local = hw_to_local(hw); 2467 struct ieee80211_chanctx *ctx; 2468 2469 rcu_read_lock(); 2470 list_for_each_entry_rcu(ctx, &local->chanctx_list, list) 2471 if (ctx->driver_present) 2472 iter(hw, &ctx->conf, iter_data); 2473 rcu_read_unlock(); 2474 } 2475 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); 2476 2477 void ieee80211_iter_chan_contexts_mtx( 2478 struct ieee80211_hw *hw, 2479 void (*iter)(struct ieee80211_hw *hw, 2480 struct ieee80211_chanctx_conf *chanctx_conf, 2481 void *data), 2482 void *iter_data) 2483 { 2484 struct ieee80211_local *local = hw_to_local(hw); 2485 struct ieee80211_chanctx *ctx; 2486 2487 lockdep_assert_wiphy(hw->wiphy); 2488 2489 list_for_each_entry(ctx, &local->chanctx_list, list) 2490 if (ctx->driver_present) 2491 iter(hw, &ctx->conf, iter_data); 2492 } 2493 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_mtx); 2494