1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved. 4 * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. 5 */ 6 #include <linux/rtnetlink.h> 7 #include "core.h" 8 #include "debug.h" 9 10 /* World regdom to be used in case default regd from fw is unavailable */ 11 #define ATH12K_2GHZ_CH01_11 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0) 12 #define ATH12K_5GHZ_5150_5350 REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\ 13 NL80211_RRF_NO_IR) 14 #define ATH12K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\ 15 NL80211_RRF_NO_IR) 16 17 #define ETSI_WEATHER_RADAR_BAND_LOW 5590 18 #define ETSI_WEATHER_RADAR_BAND_HIGH 5650 19 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT 600000 20 21 static const struct ieee80211_regdomain ath12k_world_regd = { 22 .n_reg_rules = 3, 23 .alpha2 = "00", 24 .reg_rules = { 25 ATH12K_2GHZ_CH01_11, 26 ATH12K_5GHZ_5150_5350, 27 ATH12K_5GHZ_5725_5850, 28 } 29 }; 30 31 static bool ath12k_regdom_changes(struct ieee80211_hw *hw, char *alpha2) 32 { 33 const struct ieee80211_regdomain *regd; 34 35 regd = rcu_dereference_rtnl(hw->wiphy->regd); 36 /* This can happen during wiphy registration where the previous 37 * user request is received before we update the regd received 38 * from firmware. 39 */ 40 if (!regd) 41 return true; 42 43 return memcmp(regd->alpha2, alpha2, 2) != 0; 44 } 45 46 static void 47 ath12k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request) 48 { 49 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 50 struct ath12k_wmi_init_country_arg arg; 51 struct ath12k *ar = hw->priv; 52 int ret; 53 54 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 55 "Regulatory Notification received for %s\n", wiphy_name(wiphy)); 56 57 /* Currently supporting only General User Hints. Cell base user 58 * hints to be handled later. 59 * Hints from other sources like Core, Beacons are not expected for 60 * self managed wiphy's 61 */ 62 if (!(request->initiator == NL80211_REGDOM_SET_BY_USER && 63 request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) { 64 ath12k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n"); 65 return; 66 } 67 68 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) { 69 ath12k_dbg(ar->ab, ATH12K_DBG_REG, 70 "Country Setting is not allowed\n"); 71 return; 72 } 73 74 if (!ath12k_regdom_changes(hw, request->alpha2)) { 75 ath12k_dbg(ar->ab, ATH12K_DBG_REG, "Country is already set\n"); 76 return; 77 } 78 79 /* Set the country code to the firmware and wait for 80 * the WMI_REG_CHAN_LIST_CC EVENT for updating the 81 * reg info 82 */ 83 arg.flags = ALPHA_IS_SET; 84 memcpy(&arg.cc_info.alpha2, request->alpha2, 2); 85 arg.cc_info.alpha2[2] = 0; 86 87 ret = ath12k_wmi_send_init_country_cmd(ar, &arg); 88 if (ret) 89 ath12k_warn(ar->ab, 90 "INIT Country code set to fw failed : %d\n", ret); 91 } 92 93 int ath12k_reg_update_chan_list(struct ath12k *ar) 94 { 95 struct ieee80211_supported_band **bands; 96 struct ath12k_wmi_scan_chan_list_arg *arg; 97 struct ieee80211_channel *channel; 98 struct ieee80211_hw *hw = ar->hw; 99 struct ath12k_wmi_channel_arg *ch; 100 enum nl80211_band band; 101 int num_channels = 0; 102 int i, ret; 103 104 bands = hw->wiphy->bands; 105 for (band = 0; band < NUM_NL80211_BANDS; band++) { 106 if (!bands[band]) 107 continue; 108 109 for (i = 0; i < bands[band]->n_channels; i++) { 110 if (bands[band]->channels[i].flags & 111 IEEE80211_CHAN_DISABLED) 112 continue; 113 114 num_channels++; 115 } 116 } 117 118 if (WARN_ON(!num_channels)) 119 return -EINVAL; 120 121 arg = kzalloc(struct_size(arg, channel, num_channels), GFP_KERNEL); 122 123 if (!arg) 124 return -ENOMEM; 125 126 arg->pdev_id = ar->pdev->pdev_id; 127 arg->nallchans = num_channels; 128 129 ch = arg->channel; 130 131 for (band = 0; band < NUM_NL80211_BANDS; band++) { 132 if (!bands[band]) 133 continue; 134 135 for (i = 0; i < bands[band]->n_channels; i++) { 136 channel = &bands[band]->channels[i]; 137 138 if (channel->flags & IEEE80211_CHAN_DISABLED) 139 continue; 140 141 /* TODO: Set to true/false based on some condition? */ 142 ch->allow_ht = true; 143 ch->allow_vht = true; 144 ch->allow_he = true; 145 146 ch->dfs_set = 147 !!(channel->flags & IEEE80211_CHAN_RADAR); 148 ch->is_chan_passive = !!(channel->flags & 149 IEEE80211_CHAN_NO_IR); 150 ch->is_chan_passive |= ch->dfs_set; 151 ch->mhz = channel->center_freq; 152 ch->cfreq1 = channel->center_freq; 153 ch->minpower = 0; 154 ch->maxpower = channel->max_power * 2; 155 ch->maxregpower = channel->max_reg_power * 2; 156 ch->antennamax = channel->max_antenna_gain * 2; 157 158 /* TODO: Use appropriate phymodes */ 159 if (channel->band == NL80211_BAND_2GHZ) 160 ch->phy_mode = MODE_11G; 161 else 162 ch->phy_mode = MODE_11A; 163 164 if (channel->band == NL80211_BAND_6GHZ && 165 cfg80211_channel_is_psc(channel)) 166 ch->psc_channel = true; 167 168 ath12k_dbg(ar->ab, ATH12K_DBG_WMI, 169 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n", 170 i, arg->nallchans, 171 ch->mhz, ch->maxpower, ch->maxregpower, 172 ch->antennamax, ch->phy_mode); 173 174 ch++; 175 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2 176 * set_agile, reg_class_idx 177 */ 178 } 179 } 180 181 ret = ath12k_wmi_send_scan_chan_list_cmd(ar, arg); 182 kfree(arg); 183 184 return ret; 185 } 186 187 static void ath12k_copy_regd(struct ieee80211_regdomain *regd_orig, 188 struct ieee80211_regdomain *regd_copy) 189 { 190 u8 i; 191 192 /* The caller should have checked error conditions */ 193 memcpy(regd_copy, regd_orig, sizeof(*regd_orig)); 194 195 for (i = 0; i < regd_orig->n_reg_rules; i++) 196 memcpy(®d_copy->reg_rules[i], ®d_orig->reg_rules[i], 197 sizeof(struct ieee80211_reg_rule)); 198 } 199 200 int ath12k_regd_update(struct ath12k *ar, bool init) 201 { 202 struct ieee80211_hw *hw = ar->hw; 203 struct ieee80211_regdomain *regd, *regd_copy = NULL; 204 int ret, regd_len, pdev_id; 205 struct ath12k_base *ab; 206 207 ab = ar->ab; 208 pdev_id = ar->pdev_idx; 209 210 spin_lock_bh(&ab->base_lock); 211 212 if (init) { 213 /* Apply the regd received during init through 214 * WMI_REG_CHAN_LIST_CC event. In case of failure to 215 * receive the regd, initialize with a default world 216 * regulatory. 217 */ 218 if (ab->default_regd[pdev_id]) { 219 regd = ab->default_regd[pdev_id]; 220 } else { 221 ath12k_warn(ab, 222 "failed to receive default regd during init\n"); 223 regd = (struct ieee80211_regdomain *)&ath12k_world_regd; 224 } 225 } else { 226 regd = ab->new_regd[pdev_id]; 227 } 228 229 if (!regd) { 230 ret = -EINVAL; 231 spin_unlock_bh(&ab->base_lock); 232 goto err; 233 } 234 235 regd_len = sizeof(*regd) + (regd->n_reg_rules * 236 sizeof(struct ieee80211_reg_rule)); 237 238 regd_copy = kzalloc(regd_len, GFP_ATOMIC); 239 if (regd_copy) 240 ath12k_copy_regd(regd, regd_copy); 241 242 spin_unlock_bh(&ab->base_lock); 243 244 if (!regd_copy) { 245 ret = -ENOMEM; 246 goto err; 247 } 248 249 rtnl_lock(); 250 wiphy_lock(hw->wiphy); 251 ret = regulatory_set_wiphy_regd_sync(hw->wiphy, regd_copy); 252 wiphy_unlock(hw->wiphy); 253 rtnl_unlock(); 254 255 kfree(regd_copy); 256 257 if (ret) 258 goto err; 259 260 if (ar->state == ATH12K_STATE_ON) { 261 ret = ath12k_reg_update_chan_list(ar); 262 if (ret) 263 goto err; 264 } 265 266 return 0; 267 err: 268 ath12k_warn(ab, "failed to perform regd update : %d\n", ret); 269 return ret; 270 } 271 272 static enum nl80211_dfs_regions 273 ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region) 274 { 275 switch (dfs_region) { 276 case ATH12K_DFS_REG_FCC: 277 case ATH12K_DFS_REG_CN: 278 return NL80211_DFS_FCC; 279 case ATH12K_DFS_REG_ETSI: 280 case ATH12K_DFS_REG_KR: 281 return NL80211_DFS_ETSI; 282 case ATH12K_DFS_REG_MKK: 283 case ATH12K_DFS_REG_MKK_N: 284 return NL80211_DFS_JP; 285 default: 286 return NL80211_DFS_UNSET; 287 } 288 } 289 290 static u32 ath12k_map_fw_reg_flags(u16 reg_flags) 291 { 292 u32 flags = 0; 293 294 if (reg_flags & REGULATORY_CHAN_NO_IR) 295 flags = NL80211_RRF_NO_IR; 296 297 if (reg_flags & REGULATORY_CHAN_RADAR) 298 flags |= NL80211_RRF_DFS; 299 300 if (reg_flags & REGULATORY_CHAN_NO_OFDM) 301 flags |= NL80211_RRF_NO_OFDM; 302 303 if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY) 304 flags |= NL80211_RRF_NO_OUTDOOR; 305 306 if (reg_flags & REGULATORY_CHAN_NO_HT40) 307 flags |= NL80211_RRF_NO_HT40; 308 309 if (reg_flags & REGULATORY_CHAN_NO_80MHZ) 310 flags |= NL80211_RRF_NO_80MHZ; 311 312 if (reg_flags & REGULATORY_CHAN_NO_160MHZ) 313 flags |= NL80211_RRF_NO_160MHZ; 314 315 return flags; 316 } 317 318 static u32 ath12k_map_fw_phy_flags(u32 phy_flags) 319 { 320 u32 flags = 0; 321 322 if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX) 323 flags |= NL80211_RRF_NO_HE; 324 325 if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE) 326 flags |= NL80211_RRF_NO_EHT; 327 328 return flags; 329 } 330 331 static bool 332 ath12k_reg_can_intersect(struct ieee80211_reg_rule *rule1, 333 struct ieee80211_reg_rule *rule2) 334 { 335 u32 start_freq1, end_freq1; 336 u32 start_freq2, end_freq2; 337 338 start_freq1 = rule1->freq_range.start_freq_khz; 339 start_freq2 = rule2->freq_range.start_freq_khz; 340 341 end_freq1 = rule1->freq_range.end_freq_khz; 342 end_freq2 = rule2->freq_range.end_freq_khz; 343 344 if ((start_freq1 >= start_freq2 && 345 start_freq1 < end_freq2) || 346 (start_freq2 > start_freq1 && 347 start_freq2 < end_freq1)) 348 return true; 349 350 /* TODO: Should we restrict intersection feasibility 351 * based on min bandwidth of the intersected region also, 352 * say the intersected rule should have a min bandwidth 353 * of 20MHz? 354 */ 355 356 return false; 357 } 358 359 static void ath12k_reg_intersect_rules(struct ieee80211_reg_rule *rule1, 360 struct ieee80211_reg_rule *rule2, 361 struct ieee80211_reg_rule *new_rule) 362 { 363 u32 start_freq1, end_freq1; 364 u32 start_freq2, end_freq2; 365 u32 freq_diff, max_bw; 366 367 start_freq1 = rule1->freq_range.start_freq_khz; 368 start_freq2 = rule2->freq_range.start_freq_khz; 369 370 end_freq1 = rule1->freq_range.end_freq_khz; 371 end_freq2 = rule2->freq_range.end_freq_khz; 372 373 new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1, 374 start_freq2); 375 new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2); 376 377 freq_diff = new_rule->freq_range.end_freq_khz - 378 new_rule->freq_range.start_freq_khz; 379 max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz, 380 rule2->freq_range.max_bandwidth_khz); 381 new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff); 382 383 new_rule->power_rule.max_antenna_gain = 384 min_t(u32, rule1->power_rule.max_antenna_gain, 385 rule2->power_rule.max_antenna_gain); 386 387 new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp, 388 rule2->power_rule.max_eirp); 389 390 /* Use the flags of both the rules */ 391 new_rule->flags = rule1->flags | rule2->flags; 392 393 /* To be safe, lts use the max cac timeout of both rules */ 394 new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms, 395 rule2->dfs_cac_ms); 396 } 397 398 static struct ieee80211_regdomain * 399 ath12k_regd_intersect(struct ieee80211_regdomain *default_regd, 400 struct ieee80211_regdomain *curr_regd) 401 { 402 u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules; 403 struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule; 404 struct ieee80211_regdomain *new_regd = NULL; 405 u8 i, j, k; 406 407 num_old_regd_rules = default_regd->n_reg_rules; 408 num_curr_regd_rules = curr_regd->n_reg_rules; 409 num_new_regd_rules = 0; 410 411 /* Find the number of intersecting rules to allocate new regd memory */ 412 for (i = 0; i < num_old_regd_rules; i++) { 413 old_rule = default_regd->reg_rules + i; 414 for (j = 0; j < num_curr_regd_rules; j++) { 415 curr_rule = curr_regd->reg_rules + j; 416 417 if (ath12k_reg_can_intersect(old_rule, curr_rule)) 418 num_new_regd_rules++; 419 } 420 } 421 422 if (!num_new_regd_rules) 423 return NULL; 424 425 new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules * 426 sizeof(struct ieee80211_reg_rule)), 427 GFP_ATOMIC); 428 429 if (!new_regd) 430 return NULL; 431 432 /* We set the new country and dfs region directly and only trim 433 * the freq, power, antenna gain by intersecting with the 434 * default regdomain. Also MAX of the dfs cac timeout is selected. 435 */ 436 new_regd->n_reg_rules = num_new_regd_rules; 437 memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2)); 438 new_regd->dfs_region = curr_regd->dfs_region; 439 new_rule = new_regd->reg_rules; 440 441 for (i = 0, k = 0; i < num_old_regd_rules; i++) { 442 old_rule = default_regd->reg_rules + i; 443 for (j = 0; j < num_curr_regd_rules; j++) { 444 curr_rule = curr_regd->reg_rules + j; 445 446 if (ath12k_reg_can_intersect(old_rule, curr_rule)) 447 ath12k_reg_intersect_rules(old_rule, curr_rule, 448 (new_rule + k++)); 449 } 450 } 451 return new_regd; 452 } 453 454 static const char * 455 ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region) 456 { 457 switch (dfs_region) { 458 case NL80211_DFS_FCC: 459 return "FCC"; 460 case NL80211_DFS_ETSI: 461 return "ETSI"; 462 case NL80211_DFS_JP: 463 return "JP"; 464 default: 465 return "UNSET"; 466 } 467 } 468 469 static u16 470 ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw) 471 { 472 u16 bw; 473 474 bw = end_freq - start_freq; 475 bw = min_t(u16, bw, max_bw); 476 477 if (bw >= 80 && bw < 160) 478 bw = 80; 479 else if (bw >= 40 && bw < 80) 480 bw = 40; 481 else if (bw < 40) 482 bw = 20; 483 484 return bw; 485 } 486 487 static void 488 ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq, 489 u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr, 490 u32 reg_flags) 491 { 492 reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq); 493 reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq); 494 reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw); 495 reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain); 496 reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr); 497 reg_rule->flags = reg_flags; 498 } 499 500 static void 501 ath12k_reg_update_weather_radar_band(struct ath12k_base *ab, 502 struct ieee80211_regdomain *regd, 503 struct ath12k_reg_rule *reg_rule, 504 u8 *rule_idx, u32 flags, u16 max_bw) 505 { 506 u32 end_freq; 507 u16 bw; 508 u8 i; 509 510 i = *rule_idx; 511 512 bw = ath12k_reg_adjust_bw(reg_rule->start_freq, 513 ETSI_WEATHER_RADAR_BAND_LOW, max_bw); 514 515 ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq, 516 ETSI_WEATHER_RADAR_BAND_LOW, bw, 517 reg_rule->ant_gain, reg_rule->reg_power, 518 flags); 519 520 ath12k_dbg(ab, ATH12K_DBG_REG, 521 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 522 i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW, 523 bw, reg_rule->ant_gain, reg_rule->reg_power, 524 regd->reg_rules[i].dfs_cac_ms, 525 flags); 526 527 if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH) 528 end_freq = ETSI_WEATHER_RADAR_BAND_HIGH; 529 else 530 end_freq = reg_rule->end_freq; 531 532 bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq, 533 max_bw); 534 535 i++; 536 537 ath12k_reg_update_rule(regd->reg_rules + i, 538 ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw, 539 reg_rule->ant_gain, reg_rule->reg_power, 540 flags); 541 542 regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT; 543 544 ath12k_dbg(ab, ATH12K_DBG_REG, 545 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 546 i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq, 547 bw, reg_rule->ant_gain, reg_rule->reg_power, 548 regd->reg_rules[i].dfs_cac_ms, 549 flags); 550 551 if (end_freq == reg_rule->end_freq) { 552 regd->n_reg_rules--; 553 *rule_idx = i; 554 return; 555 } 556 557 bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH, 558 reg_rule->end_freq, max_bw); 559 560 i++; 561 562 ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH, 563 reg_rule->end_freq, bw, 564 reg_rule->ant_gain, reg_rule->reg_power, 565 flags); 566 567 ath12k_dbg(ab, ATH12K_DBG_REG, 568 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 569 i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq, 570 bw, reg_rule->ant_gain, reg_rule->reg_power, 571 regd->reg_rules[i].dfs_cac_ms, 572 flags); 573 574 *rule_idx = i; 575 } 576 577 struct ieee80211_regdomain * 578 ath12k_reg_build_regd(struct ath12k_base *ab, 579 struct ath12k_reg_info *reg_info, bool intersect) 580 { 581 struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL; 582 struct ath12k_reg_rule *reg_rule; 583 u8 i = 0, j = 0, k = 0; 584 u8 num_rules; 585 u16 max_bw; 586 u32 flags; 587 char alpha2[3]; 588 589 num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules; 590 591 /* FIXME: Currently taking reg rules for 6G only from Indoor AP mode list. 592 * This can be updated to choose the combination dynamically based on AP 593 * type and client type, after complete 6G regulatory support is added. 594 */ 595 if (reg_info->is_ext_reg_event) 596 num_rules += reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP]; 597 598 if (!num_rules) 599 goto ret; 600 601 /* Add max additional rules to accommodate weather radar band */ 602 if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI) 603 num_rules += 2; 604 605 tmp_regd = kzalloc(sizeof(*tmp_regd) + 606 (num_rules * sizeof(struct ieee80211_reg_rule)), 607 GFP_ATOMIC); 608 if (!tmp_regd) 609 goto ret; 610 611 memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1); 612 memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1); 613 alpha2[2] = '\0'; 614 tmp_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region); 615 616 ath12k_dbg(ab, ATH12K_DBG_REG, 617 "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n", 618 alpha2, ath12k_reg_get_regdom_str(tmp_regd->dfs_region), 619 reg_info->dfs_region, num_rules); 620 /* Update reg_rules[] below. Firmware is expected to 621 * send these rules in order(2G rules first and then 5G) 622 */ 623 for (; i < num_rules; i++) { 624 if (reg_info->num_2g_reg_rules && 625 (i < reg_info->num_2g_reg_rules)) { 626 reg_rule = reg_info->reg_rules_2g_ptr + i; 627 max_bw = min_t(u16, reg_rule->max_bw, 628 reg_info->max_bw_2g); 629 flags = 0; 630 } else if (reg_info->num_5g_reg_rules && 631 (j < reg_info->num_5g_reg_rules)) { 632 reg_rule = reg_info->reg_rules_5g_ptr + j++; 633 max_bw = min_t(u16, reg_rule->max_bw, 634 reg_info->max_bw_5g); 635 636 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for 637 * BW Auto correction, we can enable this by default 638 * for all 5G rules here. The regulatory core performs 639 * BW correction if required and applies flags as 640 * per other BW rule flags we pass from here 641 */ 642 flags = NL80211_RRF_AUTO_BW; 643 } else if (reg_info->is_ext_reg_event && 644 reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP] && 645 (k < reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP])) { 646 reg_rule = reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP] + k++; 647 max_bw = min_t(u16, reg_rule->max_bw, 648 reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP]); 649 flags = NL80211_RRF_AUTO_BW; 650 } else { 651 break; 652 } 653 654 flags |= ath12k_map_fw_reg_flags(reg_rule->flags); 655 flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap); 656 657 ath12k_reg_update_rule(tmp_regd->reg_rules + i, 658 reg_rule->start_freq, 659 reg_rule->end_freq, max_bw, 660 reg_rule->ant_gain, reg_rule->reg_power, 661 flags); 662 663 /* Update dfs cac timeout if the dfs domain is ETSI and the 664 * new rule covers weather radar band. 665 * Default value of '0' corresponds to 60s timeout, so no 666 * need to update that for other rules. 667 */ 668 if (flags & NL80211_RRF_DFS && 669 reg_info->dfs_region == ATH12K_DFS_REG_ETSI && 670 (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW && 671 reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){ 672 ath12k_reg_update_weather_radar_band(ab, tmp_regd, 673 reg_rule, &i, 674 flags, max_bw); 675 continue; 676 } 677 678 if (reg_info->is_ext_reg_event) { 679 ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n", 680 i + 1, reg_rule->start_freq, reg_rule->end_freq, 681 max_bw, reg_rule->ant_gain, reg_rule->reg_power, 682 tmp_regd->reg_rules[i].dfs_cac_ms, 683 flags, reg_rule->psd_flag, reg_rule->psd_eirp); 684 } else { 685 ath12k_dbg(ab, ATH12K_DBG_REG, 686 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n", 687 i + 1, reg_rule->start_freq, reg_rule->end_freq, 688 max_bw, reg_rule->ant_gain, reg_rule->reg_power, 689 tmp_regd->reg_rules[i].dfs_cac_ms, 690 flags); 691 } 692 } 693 694 tmp_regd->n_reg_rules = i; 695 696 if (intersect) { 697 default_regd = ab->default_regd[reg_info->phy_id]; 698 699 /* Get a new regd by intersecting the received regd with 700 * our default regd. 701 */ 702 new_regd = ath12k_regd_intersect(default_regd, tmp_regd); 703 kfree(tmp_regd); 704 if (!new_regd) { 705 ath12k_warn(ab, "Unable to create intersected regdomain\n"); 706 goto ret; 707 } 708 } else { 709 new_regd = tmp_regd; 710 } 711 712 ret: 713 return new_regd; 714 } 715 716 void ath12k_regd_update_work(struct work_struct *work) 717 { 718 struct ath12k *ar = container_of(work, struct ath12k, 719 regd_update_work); 720 int ret; 721 722 ret = ath12k_regd_update(ar, false); 723 if (ret) { 724 /* Firmware has already moved to the new regd. We need 725 * to maintain channel consistency across FW, Host driver 726 * and userspace. Hence as a fallback mechanism we can set 727 * the prev or default country code to the firmware. 728 */ 729 /* TODO: Implement Fallback Mechanism */ 730 } 731 } 732 733 void ath12k_reg_init(struct ieee80211_hw *hw) 734 { 735 hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED; 736 hw->wiphy->reg_notifier = ath12k_reg_notifier; 737 } 738 739 void ath12k_reg_free(struct ath12k_base *ab) 740 { 741 int i; 742 743 for (i = 0; i < ab->hw_params->max_radios; i++) { 744 kfree(ab->default_regd[i]); 745 kfree(ab->new_regd[i]); 746 } 747 } 748