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