1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mac80211 - channel management 4 */ 5 6 #include <linux/nl80211.h> 7 #include <linux/export.h> 8 #include <linux/rtnetlink.h> 9 #include <net/cfg80211.h> 10 #include "ieee80211_i.h" 11 #include "driver-ops.h" 12 13 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local, 14 struct ieee80211_chanctx *ctx) 15 { 16 struct ieee80211_sub_if_data *sdata; 17 int num = 0; 18 19 lockdep_assert_held(&local->chanctx_mtx); 20 21 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list) 22 num++; 23 24 return num; 25 } 26 27 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local, 28 struct ieee80211_chanctx *ctx) 29 { 30 struct ieee80211_sub_if_data *sdata; 31 int num = 0; 32 33 lockdep_assert_held(&local->chanctx_mtx); 34 35 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list) 36 num++; 37 38 return num; 39 } 40 41 int ieee80211_chanctx_refcount(struct ieee80211_local *local, 42 struct ieee80211_chanctx *ctx) 43 { 44 return ieee80211_chanctx_num_assigned(local, ctx) + 45 ieee80211_chanctx_num_reserved(local, ctx); 46 } 47 48 static int ieee80211_num_chanctx(struct ieee80211_local *local) 49 { 50 struct ieee80211_chanctx *ctx; 51 int num = 0; 52 53 lockdep_assert_held(&local->chanctx_mtx); 54 55 list_for_each_entry(ctx, &local->chanctx_list, list) 56 num++; 57 58 return num; 59 } 60 61 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local) 62 { 63 lockdep_assert_held(&local->chanctx_mtx); 64 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local); 65 } 66 67 static struct ieee80211_chanctx * 68 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata) 69 { 70 struct ieee80211_local *local __maybe_unused = sdata->local; 71 struct ieee80211_chanctx_conf *conf; 72 73 conf = rcu_dereference_protected(sdata->vif.chanctx_conf, 74 lockdep_is_held(&local->chanctx_mtx)); 75 if (!conf) 76 return NULL; 77 78 return container_of(conf, struct ieee80211_chanctx, conf); 79 } 80 81 static const struct cfg80211_chan_def * 82 ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local, 83 struct ieee80211_chanctx *ctx, 84 const struct cfg80211_chan_def *compat) 85 { 86 struct ieee80211_sub_if_data *sdata; 87 88 lockdep_assert_held(&local->chanctx_mtx); 89 90 list_for_each_entry(sdata, &ctx->reserved_vifs, 91 reserved_chanctx_list) { 92 if (!compat) 93 compat = &sdata->reserved_chandef; 94 95 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef, 96 compat); 97 if (!compat) 98 break; 99 } 100 101 return compat; 102 } 103 104 static const struct cfg80211_chan_def * 105 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local, 106 struct ieee80211_chanctx *ctx, 107 const struct cfg80211_chan_def *compat) 108 { 109 struct ieee80211_sub_if_data *sdata; 110 111 lockdep_assert_held(&local->chanctx_mtx); 112 113 list_for_each_entry(sdata, &ctx->assigned_vifs, 114 assigned_chanctx_list) { 115 if (sdata->reserved_chanctx != NULL) 116 continue; 117 118 if (!compat) 119 compat = &sdata->vif.bss_conf.chandef; 120 121 compat = cfg80211_chandef_compatible( 122 &sdata->vif.bss_conf.chandef, compat); 123 if (!compat) 124 break; 125 } 126 127 return compat; 128 } 129 130 static const struct cfg80211_chan_def * 131 ieee80211_chanctx_combined_chandef(struct ieee80211_local *local, 132 struct ieee80211_chanctx *ctx, 133 const struct cfg80211_chan_def *compat) 134 { 135 lockdep_assert_held(&local->chanctx_mtx); 136 137 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat); 138 if (!compat) 139 return NULL; 140 141 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat); 142 if (!compat) 143 return NULL; 144 145 return compat; 146 } 147 148 static bool 149 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local, 150 struct ieee80211_chanctx *ctx, 151 const struct cfg80211_chan_def *def) 152 { 153 lockdep_assert_held(&local->chanctx_mtx); 154 155 if (ieee80211_chanctx_combined_chandef(local, ctx, def)) 156 return true; 157 158 if (!list_empty(&ctx->reserved_vifs) && 159 ieee80211_chanctx_reserved_chandef(local, ctx, def)) 160 return true; 161 162 return false; 163 } 164 165 static struct ieee80211_chanctx * 166 ieee80211_find_reservation_chanctx(struct ieee80211_local *local, 167 const struct cfg80211_chan_def *chandef, 168 enum ieee80211_chanctx_mode mode) 169 { 170 struct ieee80211_chanctx *ctx; 171 172 lockdep_assert_held(&local->chanctx_mtx); 173 174 if (mode == IEEE80211_CHANCTX_EXCLUSIVE) 175 return NULL; 176 177 list_for_each_entry(ctx, &local->chanctx_list, list) { 178 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) 179 continue; 180 181 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) 182 continue; 183 184 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx, 185 chandef)) 186 continue; 187 188 return ctx; 189 } 190 191 return NULL; 192 } 193 194 enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta) 195 { 196 switch (sta->bandwidth) { 197 case IEEE80211_STA_RX_BW_20: 198 if (sta->ht_cap.ht_supported) 199 return NL80211_CHAN_WIDTH_20; 200 else 201 return NL80211_CHAN_WIDTH_20_NOHT; 202 case IEEE80211_STA_RX_BW_40: 203 return NL80211_CHAN_WIDTH_40; 204 case IEEE80211_STA_RX_BW_80: 205 return NL80211_CHAN_WIDTH_80; 206 case IEEE80211_STA_RX_BW_160: 207 /* 208 * This applied for both 160 and 80+80. since we use 209 * the returned value to consider degradation of 210 * ctx->conf.min_def, we have to make sure to take 211 * the bigger one (NL80211_CHAN_WIDTH_160). 212 * Otherwise we might try degrading even when not 213 * needed, as the max required sta_bw returned (80+80) 214 * might be smaller than the configured bw (160). 215 */ 216 return NL80211_CHAN_WIDTH_160; 217 default: 218 WARN_ON(1); 219 return NL80211_CHAN_WIDTH_20; 220 } 221 } 222 223 static enum nl80211_chan_width 224 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata) 225 { 226 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; 227 struct sta_info *sta; 228 229 rcu_read_lock(); 230 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) { 231 if (sdata != sta->sdata && 232 !(sta->sdata->bss && sta->sdata->bss == sdata->bss)) 233 continue; 234 235 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta)); 236 } 237 rcu_read_unlock(); 238 239 return max_bw; 240 } 241 242 static enum nl80211_chan_width 243 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local, 244 struct ieee80211_chanctx_conf *conf) 245 { 246 struct ieee80211_sub_if_data *sdata; 247 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; 248 249 rcu_read_lock(); 250 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 251 struct ieee80211_vif *vif = &sdata->vif; 252 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT; 253 254 if (!ieee80211_sdata_running(sdata)) 255 continue; 256 257 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) 258 continue; 259 260 switch (vif->type) { 261 case NL80211_IFTYPE_AP: 262 case NL80211_IFTYPE_AP_VLAN: 263 width = ieee80211_get_max_required_bw(sdata); 264 break; 265 case NL80211_IFTYPE_STATION: 266 /* 267 * The ap's sta->bandwidth is not set yet at this 268 * point, so take the width from the chandef, but 269 * account also for TDLS peers 270 */ 271 width = max(vif->bss_conf.chandef.width, 272 ieee80211_get_max_required_bw(sdata)); 273 break; 274 case NL80211_IFTYPE_P2P_DEVICE: 275 case NL80211_IFTYPE_NAN: 276 continue; 277 case NL80211_IFTYPE_ADHOC: 278 case NL80211_IFTYPE_MESH_POINT: 279 case NL80211_IFTYPE_OCB: 280 width = vif->bss_conf.chandef.width; 281 break; 282 case NL80211_IFTYPE_WDS: 283 case NL80211_IFTYPE_UNSPECIFIED: 284 case NUM_NL80211_IFTYPES: 285 case NL80211_IFTYPE_MONITOR: 286 case NL80211_IFTYPE_P2P_CLIENT: 287 case NL80211_IFTYPE_P2P_GO: 288 WARN_ON_ONCE(1); 289 } 290 max_bw = max(max_bw, width); 291 } 292 293 /* use the configured bandwidth in case of monitor interface */ 294 sdata = rcu_dereference(local->monitor_sdata); 295 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf) 296 max_bw = max(max_bw, conf->def.width); 297 298 rcu_read_unlock(); 299 300 return max_bw; 301 } 302 303 /* 304 * recalc the min required chan width of the channel context, which is 305 * the max of min required widths of all the interfaces bound to this 306 * channel context. 307 */ 308 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local, 309 struct ieee80211_chanctx *ctx) 310 { 311 enum nl80211_chan_width max_bw; 312 struct cfg80211_chan_def min_def; 313 314 lockdep_assert_held(&local->chanctx_mtx); 315 316 /* don't optimize non-20MHz based and radar_enabled confs */ 317 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 || 318 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 || 319 ctx->conf.def.width == NL80211_CHAN_WIDTH_1 || 320 ctx->conf.def.width == NL80211_CHAN_WIDTH_2 || 321 ctx->conf.def.width == NL80211_CHAN_WIDTH_4 || 322 ctx->conf.def.width == NL80211_CHAN_WIDTH_8 || 323 ctx->conf.def.width == NL80211_CHAN_WIDTH_16 || 324 ctx->conf.radar_enabled) { 325 ctx->conf.min_def = ctx->conf.def; 326 return; 327 } 328 329 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf); 330 331 /* downgrade chandef up to max_bw */ 332 min_def = ctx->conf.def; 333 while (min_def.width > max_bw) 334 ieee80211_chandef_downgrade(&min_def); 335 336 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def)) 337 return; 338 339 ctx->conf.min_def = min_def; 340 if (!ctx->driver_present) 341 return; 342 343 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH); 344 } 345 346 static void ieee80211_change_chanctx(struct ieee80211_local *local, 347 struct ieee80211_chanctx *ctx, 348 const struct cfg80211_chan_def *chandef) 349 { 350 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) { 351 ieee80211_recalc_chanctx_min_def(local, ctx); 352 return; 353 } 354 355 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef)); 356 357 ctx->conf.def = *chandef; 358 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH); 359 ieee80211_recalc_chanctx_min_def(local, ctx); 360 361 if (!local->use_chanctx) { 362 local->_oper_chandef = *chandef; 363 ieee80211_hw_config(local, 0); 364 } 365 } 366 367 static struct ieee80211_chanctx * 368 ieee80211_find_chanctx(struct ieee80211_local *local, 369 const struct cfg80211_chan_def *chandef, 370 enum ieee80211_chanctx_mode mode) 371 { 372 struct ieee80211_chanctx *ctx; 373 374 lockdep_assert_held(&local->chanctx_mtx); 375 376 if (mode == IEEE80211_CHANCTX_EXCLUSIVE) 377 return NULL; 378 379 list_for_each_entry(ctx, &local->chanctx_list, list) { 380 const struct cfg80211_chan_def *compat; 381 382 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE) 383 continue; 384 385 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) 386 continue; 387 388 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef); 389 if (!compat) 390 continue; 391 392 compat = ieee80211_chanctx_reserved_chandef(local, ctx, 393 compat); 394 if (!compat) 395 continue; 396 397 ieee80211_change_chanctx(local, ctx, compat); 398 399 return ctx; 400 } 401 402 return NULL; 403 } 404 405 bool ieee80211_is_radar_required(struct ieee80211_local *local) 406 { 407 struct ieee80211_sub_if_data *sdata; 408 409 lockdep_assert_held(&local->mtx); 410 411 rcu_read_lock(); 412 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 413 if (sdata->radar_required) { 414 rcu_read_unlock(); 415 return true; 416 } 417 } 418 rcu_read_unlock(); 419 420 return false; 421 } 422 423 static bool 424 ieee80211_chanctx_radar_required(struct ieee80211_local *local, 425 struct ieee80211_chanctx *ctx) 426 { 427 struct ieee80211_chanctx_conf *conf = &ctx->conf; 428 struct ieee80211_sub_if_data *sdata; 429 bool required = false; 430 431 lockdep_assert_held(&local->chanctx_mtx); 432 lockdep_assert_held(&local->mtx); 433 434 rcu_read_lock(); 435 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 436 if (!ieee80211_sdata_running(sdata)) 437 continue; 438 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) 439 continue; 440 if (!sdata->radar_required) 441 continue; 442 443 required = true; 444 break; 445 } 446 rcu_read_unlock(); 447 448 return required; 449 } 450 451 static struct ieee80211_chanctx * 452 ieee80211_alloc_chanctx(struct ieee80211_local *local, 453 const struct cfg80211_chan_def *chandef, 454 enum ieee80211_chanctx_mode mode) 455 { 456 struct ieee80211_chanctx *ctx; 457 458 lockdep_assert_held(&local->chanctx_mtx); 459 460 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); 461 if (!ctx) 462 return NULL; 463 464 INIT_LIST_HEAD(&ctx->assigned_vifs); 465 INIT_LIST_HEAD(&ctx->reserved_vifs); 466 ctx->conf.def = *chandef; 467 ctx->conf.rx_chains_static = 1; 468 ctx->conf.rx_chains_dynamic = 1; 469 ctx->mode = mode; 470 ctx->conf.radar_enabled = false; 471 ieee80211_recalc_chanctx_min_def(local, ctx); 472 473 return ctx; 474 } 475 476 static int ieee80211_add_chanctx(struct ieee80211_local *local, 477 struct ieee80211_chanctx *ctx) 478 { 479 u32 changed; 480 int err; 481 482 lockdep_assert_held(&local->mtx); 483 lockdep_assert_held(&local->chanctx_mtx); 484 485 if (!local->use_chanctx) 486 local->hw.conf.radar_enabled = ctx->conf.radar_enabled; 487 488 /* turn idle off *before* setting channel -- some drivers need that */ 489 changed = ieee80211_idle_off(local); 490 if (changed) 491 ieee80211_hw_config(local, changed); 492 493 if (!local->use_chanctx) { 494 local->_oper_chandef = ctx->conf.def; 495 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 496 } else { 497 err = drv_add_chanctx(local, ctx); 498 if (err) { 499 ieee80211_recalc_idle(local); 500 return err; 501 } 502 } 503 504 return 0; 505 } 506 507 static struct ieee80211_chanctx * 508 ieee80211_new_chanctx(struct ieee80211_local *local, 509 const struct cfg80211_chan_def *chandef, 510 enum ieee80211_chanctx_mode mode) 511 { 512 struct ieee80211_chanctx *ctx; 513 int err; 514 515 lockdep_assert_held(&local->mtx); 516 lockdep_assert_held(&local->chanctx_mtx); 517 518 ctx = ieee80211_alloc_chanctx(local, chandef, mode); 519 if (!ctx) 520 return ERR_PTR(-ENOMEM); 521 522 err = ieee80211_add_chanctx(local, ctx); 523 if (err) { 524 kfree(ctx); 525 return ERR_PTR(err); 526 } 527 528 list_add_rcu(&ctx->list, &local->chanctx_list); 529 return ctx; 530 } 531 532 static void ieee80211_del_chanctx(struct ieee80211_local *local, 533 struct ieee80211_chanctx *ctx) 534 { 535 lockdep_assert_held(&local->chanctx_mtx); 536 537 if (!local->use_chanctx) { 538 struct cfg80211_chan_def *chandef = &local->_oper_chandef; 539 /* S1G doesn't have 20MHz, so get the correct width for the 540 * current channel. 541 */ 542 if (chandef->chan->band == NL80211_BAND_S1GHZ) 543 chandef->width = 544 ieee80211_s1g_channel_width(chandef->chan); 545 else 546 chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 547 chandef->center_freq1 = chandef->chan->center_freq; 548 chandef->freq1_offset = chandef->chan->freq_offset; 549 chandef->center_freq2 = 0; 550 551 /* NOTE: Disabling radar is only valid here for 552 * single channel context. To be sure, check it ... 553 */ 554 WARN_ON(local->hw.conf.radar_enabled && 555 !list_empty(&local->chanctx_list)); 556 557 local->hw.conf.radar_enabled = false; 558 559 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 560 } else { 561 drv_remove_chanctx(local, ctx); 562 } 563 564 ieee80211_recalc_idle(local); 565 } 566 567 static void ieee80211_free_chanctx(struct ieee80211_local *local, 568 struct ieee80211_chanctx *ctx) 569 { 570 lockdep_assert_held(&local->chanctx_mtx); 571 572 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0); 573 574 list_del_rcu(&ctx->list); 575 ieee80211_del_chanctx(local, ctx); 576 kfree_rcu(ctx, rcu_head); 577 } 578 579 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, 580 struct ieee80211_chanctx *ctx) 581 { 582 struct ieee80211_chanctx_conf *conf = &ctx->conf; 583 struct ieee80211_sub_if_data *sdata; 584 const struct cfg80211_chan_def *compat = NULL; 585 struct sta_info *sta; 586 587 lockdep_assert_held(&local->chanctx_mtx); 588 589 rcu_read_lock(); 590 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 591 592 if (!ieee80211_sdata_running(sdata)) 593 continue; 594 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) 595 continue; 596 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 597 continue; 598 599 if (!compat) 600 compat = &sdata->vif.bss_conf.chandef; 601 602 compat = cfg80211_chandef_compatible( 603 &sdata->vif.bss_conf.chandef, compat); 604 if (WARN_ON_ONCE(!compat)) 605 break; 606 } 607 608 /* TDLS peers can sometimes affect the chandef width */ 609 list_for_each_entry_rcu(sta, &local->sta_list, list) { 610 if (!sta->uploaded || 611 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) || 612 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) || 613 !sta->tdls_chandef.chan) 614 continue; 615 616 compat = cfg80211_chandef_compatible(&sta->tdls_chandef, 617 compat); 618 if (WARN_ON_ONCE(!compat)) 619 break; 620 } 621 rcu_read_unlock(); 622 623 if (!compat) 624 return; 625 626 ieee80211_change_chanctx(local, ctx, compat); 627 } 628 629 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, 630 struct ieee80211_chanctx *chanctx) 631 { 632 bool radar_enabled; 633 634 lockdep_assert_held(&local->chanctx_mtx); 635 /* for ieee80211_is_radar_required */ 636 lockdep_assert_held(&local->mtx); 637 638 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx); 639 640 if (radar_enabled == chanctx->conf.radar_enabled) 641 return; 642 643 chanctx->conf.radar_enabled = radar_enabled; 644 645 if (!local->use_chanctx) { 646 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled; 647 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 648 } 649 650 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); 651 } 652 653 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, 654 struct ieee80211_chanctx *new_ctx) 655 { 656 struct ieee80211_local *local = sdata->local; 657 struct ieee80211_chanctx_conf *conf; 658 struct ieee80211_chanctx *curr_ctx = NULL; 659 int ret = 0; 660 661 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN)) 662 return -ENOTSUPP; 663 664 conf = rcu_dereference_protected(sdata->vif.chanctx_conf, 665 lockdep_is_held(&local->chanctx_mtx)); 666 667 if (conf) { 668 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); 669 670 drv_unassign_vif_chanctx(local, sdata, curr_ctx); 671 conf = NULL; 672 list_del(&sdata->assigned_chanctx_list); 673 } 674 675 if (new_ctx) { 676 ret = drv_assign_vif_chanctx(local, sdata, new_ctx); 677 if (ret) 678 goto out; 679 680 conf = &new_ctx->conf; 681 list_add(&sdata->assigned_chanctx_list, 682 &new_ctx->assigned_vifs); 683 } 684 685 out: 686 rcu_assign_pointer(sdata->vif.chanctx_conf, conf); 687 688 sdata->vif.bss_conf.idle = !conf; 689 690 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) { 691 ieee80211_recalc_chanctx_chantype(local, curr_ctx); 692 ieee80211_recalc_smps_chanctx(local, curr_ctx); 693 ieee80211_recalc_radar_chanctx(local, curr_ctx); 694 ieee80211_recalc_chanctx_min_def(local, curr_ctx); 695 } 696 697 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) { 698 ieee80211_recalc_txpower(sdata, false); 699 ieee80211_recalc_chanctx_min_def(local, new_ctx); 700 } 701 702 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && 703 sdata->vif.type != NL80211_IFTYPE_MONITOR) 704 ieee80211_bss_info_change_notify(sdata, 705 BSS_CHANGED_IDLE); 706 707 ieee80211_check_fast_xmit_iface(sdata); 708 709 return ret; 710 } 711 712 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, 713 struct ieee80211_chanctx *chanctx) 714 { 715 struct ieee80211_sub_if_data *sdata; 716 u8 rx_chains_static, rx_chains_dynamic; 717 718 lockdep_assert_held(&local->chanctx_mtx); 719 720 rx_chains_static = 1; 721 rx_chains_dynamic = 1; 722 723 rcu_read_lock(); 724 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 725 u8 needed_static, needed_dynamic; 726 727 if (!ieee80211_sdata_running(sdata)) 728 continue; 729 730 if (rcu_access_pointer(sdata->vif.chanctx_conf) != 731 &chanctx->conf) 732 continue; 733 734 switch (sdata->vif.type) { 735 case NL80211_IFTYPE_P2P_DEVICE: 736 case NL80211_IFTYPE_NAN: 737 continue; 738 case NL80211_IFTYPE_STATION: 739 if (!sdata->u.mgd.associated) 740 continue; 741 break; 742 case NL80211_IFTYPE_AP_VLAN: 743 continue; 744 case NL80211_IFTYPE_AP: 745 case NL80211_IFTYPE_ADHOC: 746 case NL80211_IFTYPE_MESH_POINT: 747 case NL80211_IFTYPE_OCB: 748 break; 749 default: 750 WARN_ON_ONCE(1); 751 } 752 753 switch (sdata->smps_mode) { 754 default: 755 WARN_ONCE(1, "Invalid SMPS mode %d\n", 756 sdata->smps_mode); 757 fallthrough; 758 case IEEE80211_SMPS_OFF: 759 needed_static = sdata->needed_rx_chains; 760 needed_dynamic = sdata->needed_rx_chains; 761 break; 762 case IEEE80211_SMPS_DYNAMIC: 763 needed_static = 1; 764 needed_dynamic = sdata->needed_rx_chains; 765 break; 766 case IEEE80211_SMPS_STATIC: 767 needed_static = 1; 768 needed_dynamic = 1; 769 break; 770 } 771 772 rx_chains_static = max(rx_chains_static, needed_static); 773 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); 774 } 775 776 /* Disable SMPS for the monitor interface */ 777 sdata = rcu_dereference(local->monitor_sdata); 778 if (sdata && 779 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf) 780 rx_chains_dynamic = rx_chains_static = local->rx_chains; 781 782 rcu_read_unlock(); 783 784 if (!local->use_chanctx) { 785 if (rx_chains_static > 1) 786 local->smps_mode = IEEE80211_SMPS_OFF; 787 else if (rx_chains_dynamic > 1) 788 local->smps_mode = IEEE80211_SMPS_DYNAMIC; 789 else 790 local->smps_mode = IEEE80211_SMPS_STATIC; 791 ieee80211_hw_config(local, 0); 792 } 793 794 if (rx_chains_static == chanctx->conf.rx_chains_static && 795 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) 796 return; 797 798 chanctx->conf.rx_chains_static = rx_chains_static; 799 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; 800 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); 801 } 802 803 static void 804 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, 805 bool clear) 806 { 807 struct ieee80211_local *local __maybe_unused = sdata->local; 808 struct ieee80211_sub_if_data *vlan; 809 struct ieee80211_chanctx_conf *conf; 810 811 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) 812 return; 813 814 lockdep_assert_held(&local->mtx); 815 816 /* Check that conf exists, even when clearing this function 817 * must be called with the AP's channel context still there 818 * as it would otherwise cause VLANs to have an invalid 819 * channel context pointer for a while, possibly pointing 820 * to a channel context that has already been freed. 821 */ 822 conf = rcu_dereference_protected(sdata->vif.chanctx_conf, 823 lockdep_is_held(&local->chanctx_mtx)); 824 WARN_ON(!conf); 825 826 if (clear) 827 conf = NULL; 828 829 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 830 rcu_assign_pointer(vlan->vif.chanctx_conf, conf); 831 } 832 833 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, 834 bool clear) 835 { 836 struct ieee80211_local *local = sdata->local; 837 838 mutex_lock(&local->chanctx_mtx); 839 840 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear); 841 842 mutex_unlock(&local->chanctx_mtx); 843 } 844 845 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata) 846 { 847 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx; 848 849 lockdep_assert_held(&sdata->local->chanctx_mtx); 850 851 if (WARN_ON(!ctx)) 852 return -EINVAL; 853 854 list_del(&sdata->reserved_chanctx_list); 855 sdata->reserved_chanctx = NULL; 856 857 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) { 858 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { 859 if (WARN_ON(!ctx->replace_ctx)) 860 return -EINVAL; 861 862 WARN_ON(ctx->replace_ctx->replace_state != 863 IEEE80211_CHANCTX_WILL_BE_REPLACED); 864 WARN_ON(ctx->replace_ctx->replace_ctx != ctx); 865 866 ctx->replace_ctx->replace_ctx = NULL; 867 ctx->replace_ctx->replace_state = 868 IEEE80211_CHANCTX_REPLACE_NONE; 869 870 list_del_rcu(&ctx->list); 871 kfree_rcu(ctx, rcu_head); 872 } else { 873 ieee80211_free_chanctx(sdata->local, ctx); 874 } 875 } 876 877 return 0; 878 } 879 880 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata, 881 const struct cfg80211_chan_def *chandef, 882 enum ieee80211_chanctx_mode mode, 883 bool radar_required) 884 { 885 struct ieee80211_local *local = sdata->local; 886 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx; 887 888 lockdep_assert_held(&local->chanctx_mtx); 889 890 curr_ctx = ieee80211_vif_get_chanctx(sdata); 891 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx) 892 return -ENOTSUPP; 893 894 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode); 895 if (!new_ctx) { 896 if (ieee80211_can_create_new_chanctx(local)) { 897 new_ctx = ieee80211_new_chanctx(local, chandef, mode); 898 if (IS_ERR(new_ctx)) 899 return PTR_ERR(new_ctx); 900 } else { 901 if (!curr_ctx || 902 (curr_ctx->replace_state == 903 IEEE80211_CHANCTX_WILL_BE_REPLACED) || 904 !list_empty(&curr_ctx->reserved_vifs)) { 905 /* 906 * Another vif already requested this context 907 * for a reservation. Find another one hoping 908 * all vifs assigned to it will also switch 909 * soon enough. 910 * 911 * TODO: This needs a little more work as some 912 * cases (more than 2 chanctx capable devices) 913 * may fail which could otherwise succeed 914 * provided some channel context juggling was 915 * performed. 916 * 917 * Consider ctx1..3, vif1..6, each ctx has 2 918 * vifs. vif1 and vif2 from ctx1 request new 919 * different chandefs starting 2 in-place 920 * reserations with ctx4 and ctx5 replacing 921 * ctx1 and ctx2 respectively. Next vif5 and 922 * vif6 from ctx3 reserve ctx4. If vif3 and 923 * vif4 remain on ctx2 as they are then this 924 * fails unless `replace_ctx` from ctx5 is 925 * replaced with ctx3. 926 */ 927 list_for_each_entry(ctx, &local->chanctx_list, 928 list) { 929 if (ctx->replace_state != 930 IEEE80211_CHANCTX_REPLACE_NONE) 931 continue; 932 933 if (!list_empty(&ctx->reserved_vifs)) 934 continue; 935 936 curr_ctx = ctx; 937 break; 938 } 939 } 940 941 /* 942 * If that's true then all available contexts already 943 * have reservations and cannot be used. 944 */ 945 if (!curr_ctx || 946 (curr_ctx->replace_state == 947 IEEE80211_CHANCTX_WILL_BE_REPLACED) || 948 !list_empty(&curr_ctx->reserved_vifs)) 949 return -EBUSY; 950 951 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode); 952 if (!new_ctx) 953 return -ENOMEM; 954 955 new_ctx->replace_ctx = curr_ctx; 956 new_ctx->replace_state = 957 IEEE80211_CHANCTX_REPLACES_OTHER; 958 959 curr_ctx->replace_ctx = new_ctx; 960 curr_ctx->replace_state = 961 IEEE80211_CHANCTX_WILL_BE_REPLACED; 962 963 list_add_rcu(&new_ctx->list, &local->chanctx_list); 964 } 965 } 966 967 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs); 968 sdata->reserved_chanctx = new_ctx; 969 sdata->reserved_chandef = *chandef; 970 sdata->reserved_radar_required = radar_required; 971 sdata->reserved_ready = false; 972 973 return 0; 974 } 975 976 static void 977 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata) 978 { 979 switch (sdata->vif.type) { 980 case NL80211_IFTYPE_ADHOC: 981 case NL80211_IFTYPE_AP: 982 case NL80211_IFTYPE_MESH_POINT: 983 case NL80211_IFTYPE_OCB: 984 ieee80211_queue_work(&sdata->local->hw, 985 &sdata->csa_finalize_work); 986 break; 987 case NL80211_IFTYPE_STATION: 988 ieee80211_queue_work(&sdata->local->hw, 989 &sdata->u.mgd.chswitch_work); 990 break; 991 case NL80211_IFTYPE_UNSPECIFIED: 992 case NL80211_IFTYPE_AP_VLAN: 993 case NL80211_IFTYPE_WDS: 994 case NL80211_IFTYPE_MONITOR: 995 case NL80211_IFTYPE_P2P_CLIENT: 996 case NL80211_IFTYPE_P2P_GO: 997 case NL80211_IFTYPE_P2P_DEVICE: 998 case NL80211_IFTYPE_NAN: 999 case NUM_NL80211_IFTYPES: 1000 WARN_ON(1); 1001 break; 1002 } 1003 } 1004 1005 static void 1006 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata, 1007 const struct cfg80211_chan_def *chandef) 1008 { 1009 struct ieee80211_sub_if_data *vlan; 1010 1011 sdata->vif.bss_conf.chandef = *chandef; 1012 1013 if (sdata->vif.type != NL80211_IFTYPE_AP) 1014 return; 1015 1016 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1017 vlan->vif.bss_conf.chandef = *chandef; 1018 } 1019 1020 static int 1021 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata) 1022 { 1023 struct ieee80211_local *local = sdata->local; 1024 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {}; 1025 struct ieee80211_chanctx *old_ctx, *new_ctx; 1026 const struct cfg80211_chan_def *chandef; 1027 u32 changed = 0; 1028 int err; 1029 1030 lockdep_assert_held(&local->mtx); 1031 lockdep_assert_held(&local->chanctx_mtx); 1032 1033 new_ctx = sdata->reserved_chanctx; 1034 old_ctx = ieee80211_vif_get_chanctx(sdata); 1035 1036 if (WARN_ON(!sdata->reserved_ready)) 1037 return -EBUSY; 1038 1039 if (WARN_ON(!new_ctx)) 1040 return -EINVAL; 1041 1042 if (WARN_ON(!old_ctx)) 1043 return -EINVAL; 1044 1045 if (WARN_ON(new_ctx->replace_state == 1046 IEEE80211_CHANCTX_REPLACES_OTHER)) 1047 return -EINVAL; 1048 1049 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, 1050 &sdata->reserved_chandef); 1051 if (WARN_ON(!chandef)) 1052 return -EINVAL; 1053 1054 ieee80211_change_chanctx(local, new_ctx, chandef); 1055 1056 vif_chsw[0].vif = &sdata->vif; 1057 vif_chsw[0].old_ctx = &old_ctx->conf; 1058 vif_chsw[0].new_ctx = &new_ctx->conf; 1059 1060 list_del(&sdata->reserved_chanctx_list); 1061 sdata->reserved_chanctx = NULL; 1062 1063 err = drv_switch_vif_chanctx(local, vif_chsw, 1, 1064 CHANCTX_SWMODE_REASSIGN_VIF); 1065 if (err) { 1066 if (ieee80211_chanctx_refcount(local, new_ctx) == 0) 1067 ieee80211_free_chanctx(local, new_ctx); 1068 1069 goto out; 1070 } 1071 1072 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs); 1073 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf); 1074 1075 if (sdata->vif.type == NL80211_IFTYPE_AP) 1076 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false); 1077 1078 ieee80211_check_fast_xmit_iface(sdata); 1079 1080 if (ieee80211_chanctx_refcount(local, old_ctx) == 0) 1081 ieee80211_free_chanctx(local, old_ctx); 1082 1083 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width) 1084 changed = BSS_CHANGED_BANDWIDTH; 1085 1086 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef); 1087 1088 ieee80211_recalc_smps_chanctx(local, new_ctx); 1089 ieee80211_recalc_radar_chanctx(local, new_ctx); 1090 ieee80211_recalc_chanctx_min_def(local, new_ctx); 1091 1092 if (changed) 1093 ieee80211_bss_info_change_notify(sdata, changed); 1094 1095 out: 1096 ieee80211_vif_chanctx_reservation_complete(sdata); 1097 return err; 1098 } 1099 1100 static int 1101 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata) 1102 { 1103 struct ieee80211_local *local = sdata->local; 1104 struct ieee80211_chanctx *old_ctx, *new_ctx; 1105 const struct cfg80211_chan_def *chandef; 1106 int err; 1107 1108 old_ctx = ieee80211_vif_get_chanctx(sdata); 1109 new_ctx = sdata->reserved_chanctx; 1110 1111 if (WARN_ON(!sdata->reserved_ready)) 1112 return -EINVAL; 1113 1114 if (WARN_ON(old_ctx)) 1115 return -EINVAL; 1116 1117 if (WARN_ON(!new_ctx)) 1118 return -EINVAL; 1119 1120 if (WARN_ON(new_ctx->replace_state == 1121 IEEE80211_CHANCTX_REPLACES_OTHER)) 1122 return -EINVAL; 1123 1124 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, 1125 &sdata->reserved_chandef); 1126 if (WARN_ON(!chandef)) 1127 return -EINVAL; 1128 1129 ieee80211_change_chanctx(local, new_ctx, chandef); 1130 1131 list_del(&sdata->reserved_chanctx_list); 1132 sdata->reserved_chanctx = NULL; 1133 1134 err = ieee80211_assign_vif_chanctx(sdata, new_ctx); 1135 if (err) { 1136 if (ieee80211_chanctx_refcount(local, new_ctx) == 0) 1137 ieee80211_free_chanctx(local, new_ctx); 1138 1139 goto out; 1140 } 1141 1142 out: 1143 ieee80211_vif_chanctx_reservation_complete(sdata); 1144 return err; 1145 } 1146 1147 static bool 1148 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata) 1149 { 1150 struct ieee80211_chanctx *old_ctx, *new_ctx; 1151 1152 lockdep_assert_held(&sdata->local->chanctx_mtx); 1153 1154 new_ctx = sdata->reserved_chanctx; 1155 old_ctx = ieee80211_vif_get_chanctx(sdata); 1156 1157 if (!old_ctx) 1158 return false; 1159 1160 if (WARN_ON(!new_ctx)) 1161 return false; 1162 1163 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) 1164 return false; 1165 1166 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1167 return false; 1168 1169 return true; 1170 } 1171 1172 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local, 1173 struct ieee80211_chanctx *new_ctx) 1174 { 1175 const struct cfg80211_chan_def *chandef; 1176 1177 lockdep_assert_held(&local->mtx); 1178 lockdep_assert_held(&local->chanctx_mtx); 1179 1180 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL); 1181 if (WARN_ON(!chandef)) 1182 return -EINVAL; 1183 1184 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled; 1185 local->_oper_chandef = *chandef; 1186 ieee80211_hw_config(local, 0); 1187 1188 return 0; 1189 } 1190 1191 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local, 1192 int n_vifs) 1193 { 1194 struct ieee80211_vif_chanctx_switch *vif_chsw; 1195 struct ieee80211_sub_if_data *sdata; 1196 struct ieee80211_chanctx *ctx, *old_ctx; 1197 int i, err; 1198 1199 lockdep_assert_held(&local->mtx); 1200 lockdep_assert_held(&local->chanctx_mtx); 1201 1202 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL); 1203 if (!vif_chsw) 1204 return -ENOMEM; 1205 1206 i = 0; 1207 list_for_each_entry(ctx, &local->chanctx_list, list) { 1208 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1209 continue; 1210 1211 if (WARN_ON(!ctx->replace_ctx)) { 1212 err = -EINVAL; 1213 goto out; 1214 } 1215 1216 list_for_each_entry(sdata, &ctx->reserved_vifs, 1217 reserved_chanctx_list) { 1218 if (!ieee80211_vif_has_in_place_reservation( 1219 sdata)) 1220 continue; 1221 1222 old_ctx = ieee80211_vif_get_chanctx(sdata); 1223 vif_chsw[i].vif = &sdata->vif; 1224 vif_chsw[i].old_ctx = &old_ctx->conf; 1225 vif_chsw[i].new_ctx = &ctx->conf; 1226 1227 i++; 1228 } 1229 } 1230 1231 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs, 1232 CHANCTX_SWMODE_SWAP_CONTEXTS); 1233 1234 out: 1235 kfree(vif_chsw); 1236 return err; 1237 } 1238 1239 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local) 1240 { 1241 struct ieee80211_chanctx *ctx; 1242 int err; 1243 1244 lockdep_assert_held(&local->mtx); 1245 lockdep_assert_held(&local->chanctx_mtx); 1246 1247 list_for_each_entry(ctx, &local->chanctx_list, list) { 1248 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1249 continue; 1250 1251 if (!list_empty(&ctx->replace_ctx->assigned_vifs)) 1252 continue; 1253 1254 ieee80211_del_chanctx(local, ctx->replace_ctx); 1255 err = ieee80211_add_chanctx(local, ctx); 1256 if (err) 1257 goto err; 1258 } 1259 1260 return 0; 1261 1262 err: 1263 WARN_ON(ieee80211_add_chanctx(local, ctx)); 1264 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) { 1265 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1266 continue; 1267 1268 if (!list_empty(&ctx->replace_ctx->assigned_vifs)) 1269 continue; 1270 1271 ieee80211_del_chanctx(local, ctx); 1272 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx)); 1273 } 1274 1275 return err; 1276 } 1277 1278 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local) 1279 { 1280 struct ieee80211_sub_if_data *sdata, *sdata_tmp; 1281 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx; 1282 struct ieee80211_chanctx *new_ctx = NULL; 1283 int err, n_assigned, n_reserved, n_ready; 1284 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0; 1285 1286 lockdep_assert_held(&local->mtx); 1287 lockdep_assert_held(&local->chanctx_mtx); 1288 1289 /* 1290 * If there are 2 independent pairs of channel contexts performing 1291 * cross-switch of their vifs this code will still wait until both are 1292 * ready even though it could be possible to switch one before the 1293 * other is ready. 1294 * 1295 * For practical reasons and code simplicity just do a single huge 1296 * switch. 1297 */ 1298 1299 /* 1300 * Verify if the reservation is still feasible. 1301 * - if it's not then disconnect 1302 * - if it is but not all vifs necessary are ready then defer 1303 */ 1304 1305 list_for_each_entry(ctx, &local->chanctx_list, list) { 1306 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1307 continue; 1308 1309 if (WARN_ON(!ctx->replace_ctx)) { 1310 err = -EINVAL; 1311 goto err; 1312 } 1313 1314 if (!local->use_chanctx) 1315 new_ctx = ctx; 1316 1317 n_ctx++; 1318 1319 n_assigned = 0; 1320 n_reserved = 0; 1321 n_ready = 0; 1322 1323 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs, 1324 assigned_chanctx_list) { 1325 n_assigned++; 1326 if (sdata->reserved_chanctx) { 1327 n_reserved++; 1328 if (sdata->reserved_ready) 1329 n_ready++; 1330 } 1331 } 1332 1333 if (n_assigned != n_reserved) { 1334 if (n_ready == n_reserved) { 1335 wiphy_info(local->hw.wiphy, 1336 "channel context reservation cannot be finalized because some interfaces aren't switching\n"); 1337 err = -EBUSY; 1338 goto err; 1339 } 1340 1341 return -EAGAIN; 1342 } 1343 1344 ctx->conf.radar_enabled = false; 1345 list_for_each_entry(sdata, &ctx->reserved_vifs, 1346 reserved_chanctx_list) { 1347 if (ieee80211_vif_has_in_place_reservation(sdata) && 1348 !sdata->reserved_ready) 1349 return -EAGAIN; 1350 1351 old_ctx = ieee80211_vif_get_chanctx(sdata); 1352 if (old_ctx) { 1353 if (old_ctx->replace_state == 1354 IEEE80211_CHANCTX_WILL_BE_REPLACED) 1355 n_vifs_switch++; 1356 else 1357 n_vifs_assign++; 1358 } else { 1359 n_vifs_ctxless++; 1360 } 1361 1362 if (sdata->reserved_radar_required) 1363 ctx->conf.radar_enabled = true; 1364 } 1365 } 1366 1367 if (WARN_ON(n_ctx == 0) || 1368 WARN_ON(n_vifs_switch == 0 && 1369 n_vifs_assign == 0 && 1370 n_vifs_ctxless == 0) || 1371 WARN_ON(n_ctx > 1 && !local->use_chanctx) || 1372 WARN_ON(!new_ctx && !local->use_chanctx)) { 1373 err = -EINVAL; 1374 goto err; 1375 } 1376 1377 /* 1378 * All necessary vifs are ready. Perform the switch now depending on 1379 * reservations and driver capabilities. 1380 */ 1381 1382 if (local->use_chanctx) { 1383 if (n_vifs_switch > 0) { 1384 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch); 1385 if (err) 1386 goto err; 1387 } 1388 1389 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) { 1390 err = ieee80211_chsw_switch_ctxs(local); 1391 if (err) 1392 goto err; 1393 } 1394 } else { 1395 err = ieee80211_chsw_switch_hwconf(local, new_ctx); 1396 if (err) 1397 goto err; 1398 } 1399 1400 /* 1401 * Update all structures, values and pointers to point to new channel 1402 * context(s). 1403 */ 1404 list_for_each_entry(ctx, &local->chanctx_list, list) { 1405 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1406 continue; 1407 1408 if (WARN_ON(!ctx->replace_ctx)) { 1409 err = -EINVAL; 1410 goto err; 1411 } 1412 1413 list_for_each_entry(sdata, &ctx->reserved_vifs, 1414 reserved_chanctx_list) { 1415 u32 changed = 0; 1416 1417 if (!ieee80211_vif_has_in_place_reservation(sdata)) 1418 continue; 1419 1420 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf); 1421 1422 if (sdata->vif.type == NL80211_IFTYPE_AP) 1423 __ieee80211_vif_copy_chanctx_to_vlans(sdata, 1424 false); 1425 1426 ieee80211_check_fast_xmit_iface(sdata); 1427 1428 sdata->radar_required = sdata->reserved_radar_required; 1429 1430 if (sdata->vif.bss_conf.chandef.width != 1431 sdata->reserved_chandef.width) 1432 changed = BSS_CHANGED_BANDWIDTH; 1433 1434 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef); 1435 if (changed) 1436 ieee80211_bss_info_change_notify(sdata, 1437 changed); 1438 1439 ieee80211_recalc_txpower(sdata, false); 1440 } 1441 1442 ieee80211_recalc_chanctx_chantype(local, ctx); 1443 ieee80211_recalc_smps_chanctx(local, ctx); 1444 ieee80211_recalc_radar_chanctx(local, ctx); 1445 ieee80211_recalc_chanctx_min_def(local, ctx); 1446 1447 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, 1448 reserved_chanctx_list) { 1449 if (ieee80211_vif_get_chanctx(sdata) != ctx) 1450 continue; 1451 1452 list_del(&sdata->reserved_chanctx_list); 1453 list_move(&sdata->assigned_chanctx_list, 1454 &ctx->assigned_vifs); 1455 sdata->reserved_chanctx = NULL; 1456 1457 ieee80211_vif_chanctx_reservation_complete(sdata); 1458 } 1459 1460 /* 1461 * This context might have been a dependency for an already 1462 * ready re-assign reservation interface that was deferred. Do 1463 * not propagate error to the caller though. The in-place 1464 * reservation for originally requested interface has already 1465 * succeeded at this point. 1466 */ 1467 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, 1468 reserved_chanctx_list) { 1469 if (WARN_ON(ieee80211_vif_has_in_place_reservation( 1470 sdata))) 1471 continue; 1472 1473 if (WARN_ON(sdata->reserved_chanctx != ctx)) 1474 continue; 1475 1476 if (!sdata->reserved_ready) 1477 continue; 1478 1479 if (ieee80211_vif_get_chanctx(sdata)) 1480 err = ieee80211_vif_use_reserved_reassign( 1481 sdata); 1482 else 1483 err = ieee80211_vif_use_reserved_assign(sdata); 1484 1485 if (err) { 1486 sdata_info(sdata, 1487 "failed to finalize (re-)assign reservation (err=%d)\n", 1488 err); 1489 ieee80211_vif_unreserve_chanctx(sdata); 1490 cfg80211_stop_iface(local->hw.wiphy, 1491 &sdata->wdev, 1492 GFP_KERNEL); 1493 } 1494 } 1495 } 1496 1497 /* 1498 * Finally free old contexts 1499 */ 1500 1501 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) { 1502 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) 1503 continue; 1504 1505 ctx->replace_ctx->replace_ctx = NULL; 1506 ctx->replace_ctx->replace_state = 1507 IEEE80211_CHANCTX_REPLACE_NONE; 1508 1509 list_del_rcu(&ctx->list); 1510 kfree_rcu(ctx, rcu_head); 1511 } 1512 1513 return 0; 1514 1515 err: 1516 list_for_each_entry(ctx, &local->chanctx_list, list) { 1517 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) 1518 continue; 1519 1520 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, 1521 reserved_chanctx_list) { 1522 ieee80211_vif_unreserve_chanctx(sdata); 1523 ieee80211_vif_chanctx_reservation_complete(sdata); 1524 } 1525 } 1526 1527 return err; 1528 } 1529 1530 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) 1531 { 1532 struct ieee80211_local *local = sdata->local; 1533 struct ieee80211_chanctx_conf *conf; 1534 struct ieee80211_chanctx *ctx; 1535 bool use_reserved_switch = false; 1536 1537 lockdep_assert_held(&local->chanctx_mtx); 1538 1539 conf = rcu_dereference_protected(sdata->vif.chanctx_conf, 1540 lockdep_is_held(&local->chanctx_mtx)); 1541 if (!conf) 1542 return; 1543 1544 ctx = container_of(conf, struct ieee80211_chanctx, conf); 1545 1546 if (sdata->reserved_chanctx) { 1547 if (sdata->reserved_chanctx->replace_state == 1548 IEEE80211_CHANCTX_REPLACES_OTHER && 1549 ieee80211_chanctx_num_reserved(local, 1550 sdata->reserved_chanctx) > 1) 1551 use_reserved_switch = true; 1552 1553 ieee80211_vif_unreserve_chanctx(sdata); 1554 } 1555 1556 ieee80211_assign_vif_chanctx(sdata, NULL); 1557 if (ieee80211_chanctx_refcount(local, ctx) == 0) 1558 ieee80211_free_chanctx(local, ctx); 1559 1560 sdata->radar_required = false; 1561 1562 /* Unreserving may ready an in-place reservation. */ 1563 if (use_reserved_switch) 1564 ieee80211_vif_use_reserved_switch(local); 1565 } 1566 1567 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, 1568 const struct cfg80211_chan_def *chandef, 1569 enum ieee80211_chanctx_mode mode) 1570 { 1571 struct ieee80211_local *local = sdata->local; 1572 struct ieee80211_chanctx *ctx; 1573 u8 radar_detect_width = 0; 1574 int ret; 1575 1576 lockdep_assert_held(&local->mtx); 1577 1578 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); 1579 1580 mutex_lock(&local->chanctx_mtx); 1581 1582 ret = cfg80211_chandef_dfs_required(local->hw.wiphy, 1583 chandef, 1584 sdata->wdev.iftype); 1585 if (ret < 0) 1586 goto out; 1587 if (ret > 0) 1588 radar_detect_width = BIT(chandef->width); 1589 1590 sdata->radar_required = ret; 1591 1592 ret = ieee80211_check_combinations(sdata, chandef, mode, 1593 radar_detect_width); 1594 if (ret < 0) 1595 goto out; 1596 1597 __ieee80211_vif_release_channel(sdata); 1598 1599 ctx = ieee80211_find_chanctx(local, chandef, mode); 1600 if (!ctx) 1601 ctx = ieee80211_new_chanctx(local, chandef, mode); 1602 if (IS_ERR(ctx)) { 1603 ret = PTR_ERR(ctx); 1604 goto out; 1605 } 1606 1607 ieee80211_vif_update_chandef(sdata, chandef); 1608 1609 ret = ieee80211_assign_vif_chanctx(sdata, ctx); 1610 if (ret) { 1611 /* if assign fails refcount stays the same */ 1612 if (ieee80211_chanctx_refcount(local, ctx) == 0) 1613 ieee80211_free_chanctx(local, ctx); 1614 goto out; 1615 } 1616 1617 ieee80211_recalc_smps_chanctx(local, ctx); 1618 ieee80211_recalc_radar_chanctx(local, ctx); 1619 out: 1620 if (ret) 1621 sdata->radar_required = false; 1622 1623 mutex_unlock(&local->chanctx_mtx); 1624 return ret; 1625 } 1626 1627 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata) 1628 { 1629 struct ieee80211_local *local = sdata->local; 1630 struct ieee80211_chanctx *new_ctx; 1631 struct ieee80211_chanctx *old_ctx; 1632 int err; 1633 1634 lockdep_assert_held(&local->mtx); 1635 lockdep_assert_held(&local->chanctx_mtx); 1636 1637 new_ctx = sdata->reserved_chanctx; 1638 old_ctx = ieee80211_vif_get_chanctx(sdata); 1639 1640 if (WARN_ON(!new_ctx)) 1641 return -EINVAL; 1642 1643 if (WARN_ON(new_ctx->replace_state == 1644 IEEE80211_CHANCTX_WILL_BE_REPLACED)) 1645 return -EINVAL; 1646 1647 if (WARN_ON(sdata->reserved_ready)) 1648 return -EINVAL; 1649 1650 sdata->reserved_ready = true; 1651 1652 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) { 1653 if (old_ctx) 1654 err = ieee80211_vif_use_reserved_reassign(sdata); 1655 else 1656 err = ieee80211_vif_use_reserved_assign(sdata); 1657 1658 if (err) 1659 return err; 1660 } 1661 1662 /* 1663 * In-place reservation may need to be finalized now either if: 1664 * a) sdata is taking part in the swapping itself and is the last one 1665 * b) sdata has switched with a re-assign reservation to an existing 1666 * context readying in-place switching of old_ctx 1667 * 1668 * In case of (b) do not propagate the error up because the requested 1669 * sdata already switched successfully. Just spill an extra warning. 1670 * The ieee80211_vif_use_reserved_switch() already stops all necessary 1671 * interfaces upon failure. 1672 */ 1673 if ((old_ctx && 1674 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) || 1675 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { 1676 err = ieee80211_vif_use_reserved_switch(local); 1677 if (err && err != -EAGAIN) { 1678 if (new_ctx->replace_state == 1679 IEEE80211_CHANCTX_REPLACES_OTHER) 1680 return err; 1681 1682 wiphy_info(local->hw.wiphy, 1683 "depending in-place reservation failed (err=%d)\n", 1684 err); 1685 } 1686 } 1687 1688 return 0; 1689 } 1690 1691 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata, 1692 const struct cfg80211_chan_def *chandef, 1693 u32 *changed) 1694 { 1695 struct ieee80211_local *local = sdata->local; 1696 struct ieee80211_chanctx_conf *conf; 1697 struct ieee80211_chanctx *ctx; 1698 const struct cfg80211_chan_def *compat; 1699 int ret; 1700 1701 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, 1702 IEEE80211_CHAN_DISABLED)) 1703 return -EINVAL; 1704 1705 mutex_lock(&local->chanctx_mtx); 1706 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) { 1707 ret = 0; 1708 goto out; 1709 } 1710 1711 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || 1712 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) { 1713 ret = -EINVAL; 1714 goto out; 1715 } 1716 1717 conf = rcu_dereference_protected(sdata->vif.chanctx_conf, 1718 lockdep_is_held(&local->chanctx_mtx)); 1719 if (!conf) { 1720 ret = -EINVAL; 1721 goto out; 1722 } 1723 1724 ctx = container_of(conf, struct ieee80211_chanctx, conf); 1725 1726 compat = cfg80211_chandef_compatible(&conf->def, chandef); 1727 if (!compat) { 1728 ret = -EINVAL; 1729 goto out; 1730 } 1731 1732 switch (ctx->replace_state) { 1733 case IEEE80211_CHANCTX_REPLACE_NONE: 1734 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) { 1735 ret = -EBUSY; 1736 goto out; 1737 } 1738 break; 1739 case IEEE80211_CHANCTX_WILL_BE_REPLACED: 1740 /* TODO: Perhaps the bandwidth change could be treated as a 1741 * reservation itself? */ 1742 ret = -EBUSY; 1743 goto out; 1744 case IEEE80211_CHANCTX_REPLACES_OTHER: 1745 /* channel context that is going to replace another channel 1746 * context doesn't really exist and shouldn't be assigned 1747 * anywhere yet */ 1748 WARN_ON(1); 1749 break; 1750 } 1751 1752 ieee80211_vif_update_chandef(sdata, chandef); 1753 1754 ieee80211_recalc_chanctx_chantype(local, ctx); 1755 1756 *changed |= BSS_CHANGED_BANDWIDTH; 1757 ret = 0; 1758 out: 1759 mutex_unlock(&local->chanctx_mtx); 1760 return ret; 1761 } 1762 1763 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) 1764 { 1765 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); 1766 1767 lockdep_assert_held(&sdata->local->mtx); 1768 1769 mutex_lock(&sdata->local->chanctx_mtx); 1770 __ieee80211_vif_release_channel(sdata); 1771 mutex_unlock(&sdata->local->chanctx_mtx); 1772 } 1773 1774 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata) 1775 { 1776 struct ieee80211_local *local = sdata->local; 1777 struct ieee80211_sub_if_data *ap; 1778 struct ieee80211_chanctx_conf *conf; 1779 1780 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) 1781 return; 1782 1783 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); 1784 1785 mutex_lock(&local->chanctx_mtx); 1786 1787 conf = rcu_dereference_protected(ap->vif.chanctx_conf, 1788 lockdep_is_held(&local->chanctx_mtx)); 1789 rcu_assign_pointer(sdata->vif.chanctx_conf, conf); 1790 mutex_unlock(&local->chanctx_mtx); 1791 } 1792 1793 void ieee80211_iter_chan_contexts_atomic( 1794 struct ieee80211_hw *hw, 1795 void (*iter)(struct ieee80211_hw *hw, 1796 struct ieee80211_chanctx_conf *chanctx_conf, 1797 void *data), 1798 void *iter_data) 1799 { 1800 struct ieee80211_local *local = hw_to_local(hw); 1801 struct ieee80211_chanctx *ctx; 1802 1803 rcu_read_lock(); 1804 list_for_each_entry_rcu(ctx, &local->chanctx_list, list) 1805 if (ctx->driver_present) 1806 iter(hw, &ctx->conf, iter_data); 1807 rcu_read_unlock(); 1808 } 1809 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); 1810