1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved. 4 * Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved. 5 */ 6 #include <linux/rtnetlink.h> 7 #include "core.h" 8 #include "debug.h" 9 #include "mac.h" 10 11 /* World regdom to be used in case default regd from fw is unavailable */ 12 #define ATH12K_2GHZ_CH01_11 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0) 13 #define ATH12K_5GHZ_5150_5350 REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\ 14 NL80211_RRF_NO_IR) 15 #define ATH12K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\ 16 NL80211_RRF_NO_IR) 17 18 #define ETSI_WEATHER_RADAR_BAND_LOW 5590 19 #define ETSI_WEATHER_RADAR_BAND_HIGH 5650 20 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT 600000 21 22 static const struct ieee80211_regdomain ath12k_world_regd = { 23 .n_reg_rules = 3, 24 .alpha2 = "00", 25 .reg_rules = { 26 ATH12K_2GHZ_CH01_11, 27 ATH12K_5GHZ_5150_5350, 28 ATH12K_5GHZ_5725_5850, 29 } 30 }; 31 32 static bool ath12k_regdom_changes(struct ieee80211_hw *hw, char *alpha2) 33 { 34 const struct ieee80211_regdomain *regd; 35 36 regd = rcu_dereference_rtnl(hw->wiphy->regd); 37 /* This can happen during wiphy registration where the previous 38 * user request is received before we update the regd received 39 * from firmware. 40 */ 41 if (!regd) 42 return true; 43 44 return memcmp(regd->alpha2, alpha2, 2) != 0; 45 } 46 47 static void 48 ath12k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request) 49 { 50 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 51 struct ath12k_wmi_init_country_arg arg; 52 struct wmi_set_current_country_arg current_arg = {}; 53 struct ath12k_hw *ah = ath12k_hw_to_ah(hw); 54 struct ath12k *ar = ath12k_ah_to_ar(ah, 0); 55 int ret, i; 56 57 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 58 "Regulatory Notification received for %s\n", wiphy_name(wiphy)); 59 60 if (request->initiator == NL80211_REGDOM_SET_BY_DRIVER) { 61 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 62 "driver initiated regd update\n"); 63 if (ah->state != ATH12K_HW_STATE_ON) 64 return; 65 66 for_each_ar(ah, ar, i) { 67 ret = ath12k_reg_update_chan_list(ar, true); 68 if (ret && ret != -EINVAL) { 69 ath12k_warn(ar->ab, 70 "failed to update chan list for pdev %u, ret %d\n", 71 i, ret); 72 break; 73 } 74 } 75 return; 76 } 77 78 /* Currently supporting only General User Hints. Cell base user 79 * hints to be handled later. 80 * Hints from other sources like Core, Beacons are not expected for 81 * self managed wiphy's 82 */ 83 if (!(request->initiator == NL80211_REGDOM_SET_BY_USER && 84 request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) { 85 ath12k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n"); 86 return; 87 } 88 89 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) { 90 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 91 "Country Setting is not allowed\n"); 92 return; 93 } 94 95 if (!ath12k_regdom_changes(hw, request->alpha2)) { 96 ath12k_dbg(ar->ab, ATH12K_DBG_REG, "Country is already set\n"); 97 return; 98 } 99 100 /* Allow fresh updates to wiphy regd */ 101 ah->regd_updated = false; 102 103 /* Send the reg change request to all the radios */ 104 for_each_ar(ah, ar, i) { 105 reinit_completion(&ar->regd_update_completed); 106 107 if (ar->ab->hw_params->current_cc_support) { 108 memcpy(¤t_arg.alpha2, request->alpha2, 2); 109 memcpy(&ar->alpha2, ¤t_arg.alpha2, 2); 110 ret = ath12k_wmi_send_set_current_country_cmd(ar, ¤t_arg); 111 if (ret) 112 ath12k_warn(ar->ab, 113 "failed set current country code: %d\n", ret); 114 } else { 115 arg.flags = ALPHA_IS_SET; 116 memcpy(&arg.cc_info.alpha2, request->alpha2, 2); 117 arg.cc_info.alpha2[2] = 0; 118 119 ret = ath12k_wmi_send_init_country_cmd(ar, &arg); 120 if (ret) 121 ath12k_warn(ar->ab, 122 "failed set INIT Country code: %d\n", ret); 123 } 124 125 wiphy_lock(wiphy); 126 ath12k_mac_11d_scan_stop(ar); 127 wiphy_unlock(wiphy); 128 129 ar->regdom_set_by_user = true; 130 } 131 } 132 133 int ath12k_reg_update_chan_list(struct ath12k *ar, bool wait) 134 { 135 struct ieee80211_supported_band **bands; 136 struct ath12k_wmi_scan_chan_list_arg *arg; 137 struct ieee80211_channel *channel; 138 struct ieee80211_hw *hw = ath12k_ar_to_hw(ar); 139 struct ath12k_wmi_channel_arg *ch; 140 enum nl80211_band band; 141 int num_channels = 0; 142 int i, ret = 0; 143 144 if (ar->ah->state == ATH12K_HW_STATE_RESTARTING) 145 return 0; 146 147 bands = hw->wiphy->bands; 148 for (band = 0; band < NUM_NL80211_BANDS; band++) { 149 if (!(ar->mac.sbands[band].channels && bands[band])) 150 continue; 151 152 for (i = 0; i < bands[band]->n_channels; i++) { 153 if (bands[band]->channels[i].flags & 154 IEEE80211_CHAN_DISABLED) 155 continue; 156 /* Skip Channels that are not in current radio's range */ 157 if (bands[band]->channels[i].center_freq < 158 KHZ_TO_MHZ(ar->freq_range.start_freq) || 159 bands[band]->channels[i].center_freq > 160 KHZ_TO_MHZ(ar->freq_range.end_freq)) 161 continue; 162 163 num_channels++; 164 } 165 } 166 167 if (!num_channels) { 168 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 169 "pdev is not supported for this country\n"); 170 return -EINVAL; 171 } 172 173 arg = kzalloc(struct_size(arg, channel, num_channels), GFP_KERNEL); 174 175 if (!arg) 176 return -ENOMEM; 177 178 arg->pdev_id = ar->pdev->pdev_id; 179 arg->nallchans = num_channels; 180 181 ch = arg->channel; 182 183 for (band = 0; band < NUM_NL80211_BANDS; band++) { 184 if (!(ar->mac.sbands[band].channels && bands[band])) 185 continue; 186 187 for (i = 0; i < bands[band]->n_channels; i++) { 188 channel = &bands[band]->channels[i]; 189 190 if (channel->flags & IEEE80211_CHAN_DISABLED) 191 continue; 192 193 /* Skip Channels that are not in current radio's range */ 194 if (bands[band]->channels[i].center_freq < 195 KHZ_TO_MHZ(ar->freq_range.start_freq) || 196 bands[band]->channels[i].center_freq > 197 KHZ_TO_MHZ(ar->freq_range.end_freq)) 198 continue; 199 200 /* TODO: Set to true/false based on some condition? */ 201 ch->allow_ht = true; 202 ch->allow_vht = true; 203 ch->allow_he = true; 204 205 ch->dfs_set = 206 !!(channel->flags & IEEE80211_CHAN_RADAR); 207 ch->is_chan_passive = !!(channel->flags & 208 IEEE80211_CHAN_NO_IR); 209 ch->is_chan_passive |= ch->dfs_set; 210 ch->mhz = channel->center_freq; 211 ch->cfreq1 = channel->center_freq; 212 ch->minpower = 0; 213 ch->maxpower = channel->max_power * 2; 214 ch->maxregpower = channel->max_reg_power * 2; 215 ch->antennamax = channel->max_antenna_gain * 2; 216 217 /* TODO: Use appropriate phymodes */ 218 if (channel->band == NL80211_BAND_2GHZ) 219 ch->phy_mode = MODE_11G; 220 else 221 ch->phy_mode = MODE_11A; 222 223 if (channel->band == NL80211_BAND_6GHZ && 224 cfg80211_channel_is_psc(channel)) 225 ch->psc_channel = true; 226 227 ath12k_dbg(ar->ab, ATH12K_DBG_WMI, 228 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n", 229 i, arg->nallchans, 230 ch->mhz, ch->maxpower, ch->maxregpower, 231 ch->antennamax, ch->phy_mode); 232 233 ch++; 234 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2 235 * set_agile, reg_class_idx 236 */ 237 } 238 } 239 240 if (wait) { 241 spin_lock_bh(&ar->data_lock); 242 list_add_tail(&arg->list, &ar->regd_channel_update_queue); 243 spin_unlock_bh(&ar->data_lock); 244 245 queue_work(ar->ab->workqueue, &ar->regd_channel_update_work); 246 247 return 0; 248 } 249 250 ret = ath12k_wmi_send_scan_chan_list_cmd(ar, arg); 251 kfree(arg); 252 253 return ret; 254 } 255 256 static void ath12k_copy_regd(struct ieee80211_regdomain *regd_orig, 257 struct ieee80211_regdomain *regd_copy) 258 { 259 u8 i; 260 261 /* The caller should have checked error conditions */ 262 memcpy(regd_copy, regd_orig, sizeof(*regd_orig)); 263 264 for (i = 0; i < regd_orig->n_reg_rules; i++) 265 memcpy(®d_copy->reg_rules[i], ®d_orig->reg_rules[i], 266 sizeof(struct ieee80211_reg_rule)); 267 } 268 269 int ath12k_regd_update(struct ath12k *ar, bool init) 270 { 271 struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap; 272 u32 phy_id, freq_low, freq_high, supported_bands; 273 struct ath12k_hw *ah = ath12k_ar_to_ah(ar); 274 struct ieee80211_hw *hw = ah->hw; 275 struct ieee80211_regdomain *regd, *regd_copy = NULL; 276 int ret, regd_len, pdev_id; 277 struct ath12k_base *ab; 278 long time_left; 279 280 ab = ar->ab; 281 282 time_left = wait_for_completion_timeout(&ar->regd_update_completed, 283 ATH12K_REG_UPDATE_TIMEOUT_HZ); 284 if (time_left == 0) { 285 ath12k_warn(ab, "Timeout while waiting for regulatory update"); 286 /* Even though timeout has occurred, still continue since at least boot 287 * time data would be there to process 288 */ 289 } 290 291 supported_bands = ar->pdev->cap.supported_bands; 292 reg_cap = &ab->hal_reg_cap[ar->pdev_idx]; 293 294 /* Possible that due to reg change, current limits for supported 295 * frequency changed. Update it. As a first step, reset the 296 * previous values and then compute and set the new values. 297 */ 298 ar->freq_range.start_freq = 0; 299 ar->freq_range.end_freq = 0; 300 301 if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) { 302 if (ab->hw_params->single_pdev_only) { 303 phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_2GHZ_CAP].phy_id; 304 reg_cap = &ab->hal_reg_cap[phy_id]; 305 } 306 307 freq_low = max(reg_cap->low_2ghz_chan, ab->reg_freq_2ghz.start_freq); 308 freq_high = min(reg_cap->high_2ghz_chan, ab->reg_freq_2ghz.end_freq); 309 310 ath12k_mac_update_freq_range(ar, freq_low, freq_high); 311 } 312 313 if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && !ar->supports_6ghz) { 314 if (ab->hw_params->single_pdev_only) { 315 phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_5GHZ_CAP].phy_id; 316 reg_cap = &ab->hal_reg_cap[phy_id]; 317 } 318 319 freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_5ghz.start_freq); 320 freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_5ghz.end_freq); 321 322 ath12k_mac_update_freq_range(ar, freq_low, freq_high); 323 } 324 325 if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && ar->supports_6ghz) { 326 freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_6ghz.start_freq); 327 freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_6ghz.end_freq); 328 329 ath12k_mac_update_freq_range(ar, freq_low, freq_high); 330 } 331 332 /* If one of the radios within ah has already updated the regd for 333 * the wiphy, then avoid setting regd again 334 */ 335 if (ah->regd_updated) 336 return 0; 337 338 /* firmware provides reg rules which are similar for 2 GHz and 5 GHz 339 * pdev but 6 GHz pdev has superset of all rules including rules for 340 * all bands, we prefer 6 GHz pdev's rules to be used for setup of 341 * the wiphy regd. 342 * If 6 GHz pdev was part of the ath12k_hw, wait for the 6 GHz pdev, 343 * else pick the first pdev which calls this function and use its 344 * regd to update global hw regd. 345 * The regd_updated flag set at the end will not allow any further 346 * updates. 347 */ 348 if (ah->use_6ghz_regd && !ar->supports_6ghz) 349 return 0; 350 351 pdev_id = ar->pdev_idx; 352 353 spin_lock_bh(&ab->base_lock); 354 355 if (init) { 356 /* Apply the regd received during init through 357 * WMI_REG_CHAN_LIST_CC event. In case of failure to 358 * receive the regd, initialize with a default world 359 * regulatory. 360 */ 361 if (ab->default_regd[pdev_id]) { 362 regd = ab->default_regd[pdev_id]; 363 } else { 364 ath12k_warn(ab, 365 "failed to receive default regd during init\n"); 366 regd = (struct ieee80211_regdomain *)&ath12k_world_regd; 367 } 368 } else { 369 regd = ab->new_regd[pdev_id]; 370 } 371 372 if (!regd) { 373 ret = -EINVAL; 374 spin_unlock_bh(&ab->base_lock); 375 goto err; 376 } 377 378 regd_len = sizeof(*regd) + (regd->n_reg_rules * 379 sizeof(struct ieee80211_reg_rule)); 380 381 regd_copy = kzalloc(regd_len, GFP_ATOMIC); 382 if (regd_copy) 383 ath12k_copy_regd(regd, regd_copy); 384 385 spin_unlock_bh(&ab->base_lock); 386 387 if (!regd_copy) { 388 ret = -ENOMEM; 389 goto err; 390 } 391 392 ret = regulatory_set_wiphy_regd(hw->wiphy, regd_copy); 393 394 kfree(regd_copy); 395 396 if (ret) 397 goto err; 398 399 if (ah->state != ATH12K_HW_STATE_ON) 400 goto skip; 401 402 ah->regd_updated = true; 403 404 skip: 405 return 0; 406 err: 407 ath12k_warn(ab, "failed to perform regd update : %d\n", ret); 408 return ret; 409 } 410 411 static enum nl80211_dfs_regions 412 ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region) 413 { 414 switch (dfs_region) { 415 case ATH12K_DFS_REG_FCC: 416 case ATH12K_DFS_REG_CN: 417 return NL80211_DFS_FCC; 418 case ATH12K_DFS_REG_ETSI: 419 case ATH12K_DFS_REG_KR: 420 return NL80211_DFS_ETSI; 421 case ATH12K_DFS_REG_MKK: 422 case ATH12K_DFS_REG_MKK_N: 423 return NL80211_DFS_JP; 424 default: 425 return NL80211_DFS_UNSET; 426 } 427 } 428 429 static u32 ath12k_map_fw_reg_flags(u16 reg_flags) 430 { 431 u32 flags = 0; 432 433 if (reg_flags & REGULATORY_CHAN_NO_IR) 434 flags = NL80211_RRF_NO_IR; 435 436 if (reg_flags & REGULATORY_CHAN_RADAR) 437 flags |= NL80211_RRF_DFS; 438 439 if (reg_flags & REGULATORY_CHAN_NO_OFDM) 440 flags |= NL80211_RRF_NO_OFDM; 441 442 if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY) 443 flags |= NL80211_RRF_NO_OUTDOOR; 444 445 if (reg_flags & REGULATORY_CHAN_NO_HT40) 446 flags |= NL80211_RRF_NO_HT40; 447 448 if (reg_flags & REGULATORY_CHAN_NO_80MHZ) 449 flags |= NL80211_RRF_NO_80MHZ; 450 451 if (reg_flags & REGULATORY_CHAN_NO_160MHZ) 452 flags |= NL80211_RRF_NO_160MHZ; 453 454 return flags; 455 } 456 457 static u32 ath12k_map_fw_phy_flags(u32 phy_flags) 458 { 459 u32 flags = 0; 460 461 if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX) 462 flags |= NL80211_RRF_NO_HE; 463 464 if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE) 465 flags |= NL80211_RRF_NO_EHT; 466 467 return flags; 468 } 469 470 static const char * 471 ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region) 472 { 473 switch (dfs_region) { 474 case NL80211_DFS_FCC: 475 return "FCC"; 476 case NL80211_DFS_ETSI: 477 return "ETSI"; 478 case NL80211_DFS_JP: 479 return "JP"; 480 default: 481 return "UNSET"; 482 } 483 } 484 485 static u16 486 ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw) 487 { 488 u16 bw; 489 490 bw = end_freq - start_freq; 491 bw = min_t(u16, bw, max_bw); 492 493 if (bw >= 80 && bw < 160) 494 bw = 80; 495 else if (bw >= 40 && bw < 80) 496 bw = 40; 497 else if (bw < 40) 498 bw = 20; 499 500 return bw; 501 } 502 503 static void 504 ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq, 505 u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr, 506 s8 psd, u32 reg_flags) 507 { 508 reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq); 509 reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq); 510 reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw); 511 reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain); 512 reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr); 513 reg_rule->psd = psd; 514 reg_rule->flags = reg_flags; 515 } 516 517 static void 518 ath12k_reg_update_weather_radar_band(struct ath12k_base *ab, 519 struct ieee80211_regdomain *regd, 520 struct ath12k_reg_rule *reg_rule, 521 u8 *rule_idx, u32 flags, u16 max_bw) 522 { 523 u32 end_freq; 524 u16 bw; 525 u8 i; 526 527 i = *rule_idx; 528 529 bw = ath12k_reg_adjust_bw(reg_rule->start_freq, 530 ETSI_WEATHER_RADAR_BAND_LOW, max_bw); 531 532 ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq, 533 ETSI_WEATHER_RADAR_BAND_LOW, bw, 534 reg_rule->ant_gain, reg_rule->reg_power, 535 reg_rule->psd_eirp, flags); 536 537 ath12k_dbg(ab, ATH12K_DBG_REG, 538 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 539 i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW, 540 bw, reg_rule->ant_gain, reg_rule->reg_power, 541 regd->reg_rules[i].dfs_cac_ms, 542 flags); 543 544 if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH) 545 end_freq = ETSI_WEATHER_RADAR_BAND_HIGH; 546 else 547 end_freq = reg_rule->end_freq; 548 549 bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq, 550 max_bw); 551 552 i++; 553 554 ath12k_reg_update_rule(regd->reg_rules + i, 555 ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw, 556 reg_rule->ant_gain, reg_rule->reg_power, 557 reg_rule->psd_eirp, flags); 558 559 regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT; 560 561 ath12k_dbg(ab, ATH12K_DBG_REG, 562 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 563 i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq, 564 bw, reg_rule->ant_gain, reg_rule->reg_power, 565 regd->reg_rules[i].dfs_cac_ms, 566 flags); 567 568 if (end_freq == reg_rule->end_freq) { 569 regd->n_reg_rules--; 570 *rule_idx = i; 571 return; 572 } 573 574 bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH, 575 reg_rule->end_freq, max_bw); 576 577 i++; 578 579 ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH, 580 reg_rule->end_freq, bw, 581 reg_rule->ant_gain, reg_rule->reg_power, 582 reg_rule->psd_eirp, flags); 583 584 ath12k_dbg(ab, ATH12K_DBG_REG, 585 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 586 i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq, 587 bw, reg_rule->ant_gain, reg_rule->reg_power, 588 regd->reg_rules[i].dfs_cac_ms, 589 flags); 590 591 *rule_idx = i; 592 } 593 594 static void ath12k_reg_update_freq_range(struct ath12k_reg_freq *reg_freq, 595 struct ath12k_reg_rule *reg_rule) 596 { 597 if (reg_freq->start_freq > reg_rule->start_freq) 598 reg_freq->start_freq = reg_rule->start_freq; 599 600 if (reg_freq->end_freq < reg_rule->end_freq) 601 reg_freq->end_freq = reg_rule->end_freq; 602 } 603 604 enum wmi_reg_6g_ap_type 605 ath12k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type) 606 { 607 switch (power_type) { 608 case IEEE80211_REG_LPI_AP: 609 return WMI_REG_INDOOR_AP; 610 case IEEE80211_REG_SP_AP: 611 return WMI_REG_STD_POWER_AP; 612 case IEEE80211_REG_VLP_AP: 613 return WMI_REG_VLP_AP; 614 default: 615 return WMI_REG_MAX_AP_TYPE; 616 } 617 } 618 619 struct ieee80211_regdomain * 620 ath12k_reg_build_regd(struct ath12k_base *ab, 621 struct ath12k_reg_info *reg_info, 622 enum wmi_vdev_type vdev_type, 623 enum ieee80211_ap_reg_power power_type) 624 { 625 struct ieee80211_regdomain *new_regd = NULL; 626 struct ath12k_reg_rule *reg_rule, *reg_rule_6ghz; 627 u32 flags, reg_6ghz_number, max_bw_6ghz; 628 u8 i = 0, j = 0, k = 0; 629 u8 num_rules; 630 u16 max_bw; 631 char alpha2[3]; 632 633 num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules; 634 635 if (reg_info->is_ext_reg_event) { 636 if (vdev_type == WMI_VDEV_TYPE_STA) { 637 enum wmi_reg_6g_ap_type ap_type; 638 639 ap_type = ath12k_reg_ap_pwr_convert(power_type); 640 if (ap_type == WMI_REG_MAX_AP_TYPE) 641 ap_type = WMI_REG_INDOOR_AP; 642 643 reg_6ghz_number = reg_info->num_6g_reg_rules_cl 644 [ap_type][WMI_REG_DEFAULT_CLIENT]; 645 if (reg_6ghz_number == 0) { 646 ap_type = WMI_REG_INDOOR_AP; 647 reg_6ghz_number = reg_info->num_6g_reg_rules_cl 648 [ap_type][WMI_REG_DEFAULT_CLIENT]; 649 } 650 651 reg_rule_6ghz = reg_info->reg_rules_6g_client_ptr 652 [ap_type][WMI_REG_DEFAULT_CLIENT]; 653 max_bw_6ghz = reg_info->max_bw_6g_client 654 [ap_type][WMI_REG_DEFAULT_CLIENT]; 655 } else { 656 reg_6ghz_number = reg_info->num_6g_reg_rules_ap 657 [WMI_REG_INDOOR_AP]; 658 reg_rule_6ghz = 659 reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP]; 660 max_bw_6ghz = reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP]; 661 } 662 663 num_rules += reg_6ghz_number; 664 } 665 666 if (!num_rules) 667 goto ret; 668 669 /* Add max additional rules to accommodate weather radar band */ 670 if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI) 671 num_rules += 2; 672 673 new_regd = kzalloc(sizeof(*new_regd) + 674 (num_rules * sizeof(struct ieee80211_reg_rule)), 675 GFP_ATOMIC); 676 if (!new_regd) 677 goto ret; 678 679 memcpy(new_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1); 680 memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1); 681 alpha2[2] = '\0'; 682 new_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region); 683 684 ath12k_dbg(ab, ATH12K_DBG_REG, 685 "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n", 686 alpha2, ath12k_reg_get_regdom_str(new_regd->dfs_region), 687 reg_info->dfs_region, num_rules); 688 689 /* Reset start and end frequency for each band 690 */ 691 ab->reg_freq_5ghz.start_freq = INT_MAX; 692 ab->reg_freq_5ghz.end_freq = 0; 693 ab->reg_freq_2ghz.start_freq = INT_MAX; 694 ab->reg_freq_2ghz.end_freq = 0; 695 ab->reg_freq_6ghz.start_freq = INT_MAX; 696 ab->reg_freq_6ghz.end_freq = 0; 697 698 /* Update reg_rules[] below. Firmware is expected to 699 * send these rules in order(2G rules first and then 5G) 700 */ 701 for (; i < num_rules; i++) { 702 if (reg_info->num_2g_reg_rules && 703 (i < reg_info->num_2g_reg_rules)) { 704 reg_rule = reg_info->reg_rules_2g_ptr + i; 705 max_bw = min_t(u16, reg_rule->max_bw, 706 reg_info->max_bw_2g); 707 flags = 0; 708 ath12k_reg_update_freq_range(&ab->reg_freq_2ghz, reg_rule); 709 } else if (reg_info->num_5g_reg_rules && 710 (j < reg_info->num_5g_reg_rules)) { 711 reg_rule = reg_info->reg_rules_5g_ptr + j++; 712 max_bw = min_t(u16, reg_rule->max_bw, 713 reg_info->max_bw_5g); 714 715 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for 716 * BW Auto correction, we can enable this by default 717 * for all 5G rules here. The regulatory core performs 718 * BW correction if required and applies flags as 719 * per other BW rule flags we pass from here 720 */ 721 flags = NL80211_RRF_AUTO_BW; 722 ath12k_reg_update_freq_range(&ab->reg_freq_5ghz, reg_rule); 723 } else if (reg_info->is_ext_reg_event && reg_6ghz_number && 724 (k < reg_6ghz_number)) { 725 reg_rule = reg_rule_6ghz + k++; 726 max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz); 727 flags = NL80211_RRF_AUTO_BW; 728 if (reg_rule->psd_flag) 729 flags |= NL80211_RRF_PSD; 730 ath12k_reg_update_freq_range(&ab->reg_freq_6ghz, reg_rule); 731 } else { 732 break; 733 } 734 735 flags |= ath12k_map_fw_reg_flags(reg_rule->flags); 736 flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap); 737 738 ath12k_reg_update_rule(new_regd->reg_rules + i, 739 reg_rule->start_freq, 740 reg_rule->end_freq, max_bw, 741 reg_rule->ant_gain, reg_rule->reg_power, 742 reg_rule->psd_eirp, flags); 743 744 /* Update dfs cac timeout if the dfs domain is ETSI and the 745 * new rule covers weather radar band. 746 * Default value of '0' corresponds to 60s timeout, so no 747 * need to update that for other rules. 748 */ 749 if (flags & NL80211_RRF_DFS && 750 reg_info->dfs_region == ATH12K_DFS_REG_ETSI && 751 (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW && 752 reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){ 753 ath12k_reg_update_weather_radar_band(ab, new_regd, 754 reg_rule, &i, 755 flags, max_bw); 756 continue; 757 } 758 759 if (reg_info->is_ext_reg_event) { 760 ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n", 761 i + 1, reg_rule->start_freq, reg_rule->end_freq, 762 max_bw, reg_rule->ant_gain, reg_rule->reg_power, 763 new_regd->reg_rules[i].dfs_cac_ms, 764 flags, reg_rule->psd_flag, reg_rule->psd_eirp); 765 } else { 766 ath12k_dbg(ab, ATH12K_DBG_REG, 767 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 768 i + 1, reg_rule->start_freq, reg_rule->end_freq, 769 max_bw, reg_rule->ant_gain, reg_rule->reg_power, 770 new_regd->reg_rules[i].dfs_cac_ms, 771 flags); 772 } 773 } 774 775 new_regd->n_reg_rules = i; 776 ret: 777 return new_regd; 778 } 779 780 void ath12k_regd_update_chan_list_work(struct work_struct *work) 781 { 782 struct ath12k *ar = container_of(work, struct ath12k, 783 regd_channel_update_work); 784 struct ath12k_wmi_scan_chan_list_arg *arg; 785 struct list_head local_update_list; 786 int left; 787 788 INIT_LIST_HEAD(&local_update_list); 789 790 spin_lock_bh(&ar->data_lock); 791 list_splice_tail_init(&ar->regd_channel_update_queue, &local_update_list); 792 spin_unlock_bh(&ar->data_lock); 793 794 while ((arg = list_first_entry_or_null(&local_update_list, 795 struct ath12k_wmi_scan_chan_list_arg, 796 list))) { 797 if (ar->state_11d != ATH12K_11D_IDLE) { 798 left = wait_for_completion_timeout(&ar->completed_11d_scan, 799 ATH12K_SCAN_TIMEOUT_HZ); 800 if (!left) { 801 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 802 "failed to receive 11d scan complete: timed out\n"); 803 ar->state_11d = ATH12K_11D_IDLE; 804 } 805 806 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 807 "reg 11d scan wait left time %d\n", left); 808 } 809 810 if ((ar->scan.state == ATH12K_SCAN_STARTING || 811 ar->scan.state == ATH12K_SCAN_RUNNING)) { 812 left = wait_for_completion_timeout(&ar->scan.completed, 813 ATH12K_SCAN_TIMEOUT_HZ); 814 if (!left) 815 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 816 "failed to receive hw scan complete: timed out\n"); 817 818 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 819 "reg hw scan wait left time %d\n", left); 820 } 821 822 ath12k_wmi_send_scan_chan_list_cmd(ar, arg); 823 list_del(&arg->list); 824 kfree(arg); 825 } 826 } 827 828 void ath12k_regd_update_work(struct work_struct *work) 829 { 830 struct ath12k *ar = container_of(work, struct ath12k, 831 regd_update_work); 832 int ret; 833 834 ret = ath12k_regd_update(ar, false); 835 if (ret) { 836 /* Firmware has already moved to the new regd. We need 837 * to maintain channel consistency across FW, Host driver 838 * and userspace. Hence as a fallback mechanism we can set 839 * the prev or default country code to the firmware. 840 */ 841 /* TODO: Implement Fallback Mechanism */ 842 } 843 } 844 845 void ath12k_reg_reset_reg_info(struct ath12k_reg_info *reg_info) 846 { 847 u8 i, j; 848 849 if (!reg_info) 850 return; 851 852 kfree(reg_info->reg_rules_2g_ptr); 853 kfree(reg_info->reg_rules_5g_ptr); 854 855 if (reg_info->is_ext_reg_event) { 856 for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) { 857 kfree(reg_info->reg_rules_6g_ap_ptr[i]); 858 859 for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++) 860 kfree(reg_info->reg_rules_6g_client_ptr[i][j]); 861 } 862 } 863 } 864 865 enum ath12k_reg_status ath12k_reg_validate_reg_info(struct ath12k_base *ab, 866 struct ath12k_reg_info *reg_info) 867 { 868 int pdev_idx = reg_info->phy_id; 869 870 if (reg_info->status_code != REG_SET_CC_STATUS_PASS) { 871 /* In case of failure to set the requested country, 872 * firmware retains the current regd. We print a failure info 873 * and return from here. 874 */ 875 ath12k_warn(ab, "Failed to set the requested Country regulatory setting\n"); 876 return ATH12K_REG_STATUS_DROP; 877 } 878 879 if (pdev_idx >= ab->num_radios) { 880 /* Process the event for phy0 only if single_pdev_only 881 * is true. If pdev_idx is valid but not 0, discard the 882 * event. Otherwise, it goes to fallback. 883 */ 884 if (ab->hw_params->single_pdev_only && 885 pdev_idx < ab->hw_params->num_rxdma_per_pdev) 886 return ATH12K_REG_STATUS_DROP; 887 else 888 return ATH12K_REG_STATUS_FALLBACK; 889 } 890 891 /* Avoid multiple overwrites to default regd, during core 892 * stop-start after mac registration. 893 */ 894 if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] && 895 !memcmp(ab->default_regd[pdev_idx]->alpha2, 896 reg_info->alpha2, 2)) 897 return ATH12K_REG_STATUS_DROP; 898 899 return ATH12K_REG_STATUS_VALID; 900 } 901 902 int ath12k_reg_handle_chan_list(struct ath12k_base *ab, 903 struct ath12k_reg_info *reg_info, 904 enum wmi_vdev_type vdev_type, 905 enum ieee80211_ap_reg_power power_type) 906 { 907 struct ieee80211_regdomain *regd = NULL; 908 int pdev_idx = reg_info->phy_id; 909 struct ath12k *ar; 910 911 regd = ath12k_reg_build_regd(ab, reg_info, vdev_type, power_type); 912 if (!regd) 913 return -EINVAL; 914 915 spin_lock_bh(&ab->base_lock); 916 if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) { 917 /* Once mac is registered, ar is valid and all CC events from 918 * firmware is considered to be received due to user requests 919 * currently. 920 * Free previously built regd before assigning the newly 921 * generated regd to ar. NULL pointer handling will be 922 * taken care by kfree itself. 923 */ 924 ar = ab->pdevs[pdev_idx].ar; 925 kfree(ab->new_regd[pdev_idx]); 926 ab->new_regd[pdev_idx] = regd; 927 queue_work(ab->workqueue, &ar->regd_update_work); 928 } else { 929 /* Multiple events for the same *ar is not expected. But we 930 * can still clear any previously stored default_regd if we 931 * are receiving this event for the same radio by mistake. 932 * NULL pointer handling will be taken care by kfree itself. 933 */ 934 kfree(ab->default_regd[pdev_idx]); 935 /* This regd would be applied during mac registration */ 936 ab->default_regd[pdev_idx] = regd; 937 } 938 ab->dfs_region = reg_info->dfs_region; 939 spin_unlock_bh(&ab->base_lock); 940 941 return 0; 942 } 943 944 void ath12k_reg_init(struct ieee80211_hw *hw) 945 { 946 hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED; 947 hw->wiphy->flags |= WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER; 948 hw->wiphy->reg_notifier = ath12k_reg_notifier; 949 } 950 951 void ath12k_reg_free(struct ath12k_base *ab) 952 { 953 int i; 954 955 mutex_lock(&ab->core_lock); 956 for (i = 0; i < MAX_RADIOS; i++) { 957 ath12k_reg_reset_reg_info(ab->reg_info[i]); 958 kfree(ab->reg_info[i]); 959 ab->reg_info[i] = NULL; 960 } 961 962 for (i = 0; i < ab->hw_params->max_radios; i++) { 963 kfree(ab->default_regd[i]); 964 kfree(ab->new_regd[i]); 965 ab->default_regd[i] = NULL; 966 ab->new_regd[i] = NULL; 967 } 968 mutex_unlock(&ab->core_lock); 969 } 970