1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/rtnetlink.h>
11 /* World regdom to be used in case default regd from fw is unavailable */
12 #define ATH11K_2GHZ_CH01_11 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
13 #define ATH11K_5GHZ_5150_5350 REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
15 #define ATH11K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
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
22 static const struct ieee80211_regdomain ath11k_world_regd = {
27 ATH11K_5GHZ_5150_5350,
28 ATH11K_5GHZ_5725_5850,
32 static bool ath11k_regdom_changes(struct ath11k *ar, char *alpha2)
34 const struct ieee80211_regdomain *regd;
36 regd = rcu_dereference_rtnl(ar->hw->wiphy->regd);
37 /* This can happen during wiphy registration where the previous
38 * user request is received before we update the regd received
44 return memcmp(regd->alpha2, alpha2, 2) != 0;
48 ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
50 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
51 struct wmi_init_country_params init_country_param;
52 struct wmi_set_current_country_params set_current_param = {};
53 struct ath11k *ar = hw->priv;
56 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
57 "Regulatory Notification received for %s\n", wiphy_name(wiphy));
59 /* Currently supporting only General User Hints. Cell base user
60 * hints to be handled later.
61 * Hints from other sources like Core, Beacons are not expected for
62 * self managed wiphy's
64 if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
65 request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
66 ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
70 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
71 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
72 "Country Setting is not allowed\n");
76 if (!ath11k_regdom_changes(ar, request->alpha2)) {
77 ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
81 /* Set the country code to the firmware and will receive
82 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
85 if (ar->ab->hw_params.current_cc_support) {
86 memcpy(&set_current_param.alpha2, request->alpha2, 2);
87 memcpy(&ar->alpha2, &set_current_param.alpha2, 2);
88 ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
91 "failed set current country code: %d\n", ret);
93 init_country_param.flags = ALPHA_IS_SET;
94 memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
95 init_country_param.cc_info.alpha2[2] = 0;
97 ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
100 "INIT Country code set to fw failed : %d\n", ret);
103 ath11k_mac_11d_scan_stop(ar);
104 ar->regdom_set_by_user = true;
107 int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)
109 struct ieee80211_supported_band **bands;
110 struct scan_chan_list_params *params;
111 struct ieee80211_channel *channel;
112 struct ieee80211_hw *hw = ar->hw;
113 struct channel_param *ch;
114 enum nl80211_band band;
115 int num_channels = 0;
118 if (wait && ar->state_11d != ATH11K_11D_IDLE) {
119 left = wait_for_completion_timeout(&ar->completed_11d_scan,
120 ATH11K_SCAN_TIMEOUT_HZ);
122 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
123 "failed to receive 11d scan complete: timed out\n");
124 ar->state_11d = ATH11K_11D_IDLE;
126 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
127 "11d scan wait left time %d\n", left);
131 (ar->scan.state == ATH11K_SCAN_STARTING ||
132 ar->scan.state == ATH11K_SCAN_RUNNING)) {
133 left = wait_for_completion_timeout(&ar->scan.completed,
134 ATH11K_SCAN_TIMEOUT_HZ);
136 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
137 "failed to receive hw scan complete: timed out\n");
139 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
140 "hw scan wait left time %d\n", left);
143 if (ar->state == ATH11K_STATE_RESTARTING)
146 bands = hw->wiphy->bands;
147 for (band = 0; band < NUM_NL80211_BANDS; band++) {
151 for (i = 0; i < bands[band]->n_channels; i++) {
152 if (bands[band]->channels[i].flags &
153 IEEE80211_CHAN_DISABLED)
160 if (WARN_ON(!num_channels))
163 params = kzalloc(struct_size(params, ch_param, num_channels),
168 params->pdev_id = ar->pdev->pdev_id;
169 params->nallchans = num_channels;
171 ch = params->ch_param;
173 for (band = 0; band < NUM_NL80211_BANDS; band++) {
177 for (i = 0; i < bands[band]->n_channels; i++) {
178 channel = &bands[band]->channels[i];
180 if (channel->flags & IEEE80211_CHAN_DISABLED)
183 /* TODO: Set to true/false based on some condition? */
185 ch->allow_vht = true;
189 !!(channel->flags & IEEE80211_CHAN_RADAR);
190 ch->is_chan_passive = !!(channel->flags &
191 IEEE80211_CHAN_NO_IR);
192 ch->is_chan_passive |= ch->dfs_set;
193 ch->mhz = channel->center_freq;
194 ch->cfreq1 = channel->center_freq;
196 ch->maxpower = channel->max_power * 2;
197 ch->maxregpower = channel->max_reg_power * 2;
198 ch->antennamax = channel->max_antenna_gain * 2;
200 /* TODO: Use appropriate phymodes */
201 if (channel->band == NL80211_BAND_2GHZ)
202 ch->phy_mode = MODE_11G;
204 ch->phy_mode = MODE_11A;
206 if (channel->band == NL80211_BAND_6GHZ &&
207 cfg80211_channel_is_psc(channel))
208 ch->psc_channel = true;
210 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
211 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
212 i, params->nallchans,
213 ch->mhz, ch->maxpower, ch->maxregpower,
214 ch->antennamax, ch->phy_mode);
217 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
218 * set_agile, reg_class_idx
223 ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
229 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
230 struct ieee80211_regdomain *regd_copy)
234 /* The caller should have checked error conditions */
235 memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
237 for (i = 0; i < regd_orig->n_reg_rules; i++)
238 memcpy(®d_copy->reg_rules[i], ®d_orig->reg_rules[i],
239 sizeof(struct ieee80211_reg_rule));
242 int ath11k_regd_update(struct ath11k *ar)
244 struct ieee80211_regdomain *regd, *regd_copy = NULL;
245 int ret, regd_len, pdev_id;
246 struct ath11k_base *ab;
249 pdev_id = ar->pdev_idx;
251 spin_lock_bh(&ab->base_lock);
253 /* Prefer the latest regd update over default if it's available */
254 if (ab->new_regd[pdev_id]) {
255 regd = ab->new_regd[pdev_id];
257 /* Apply the regd received during init through
258 * WMI_REG_CHAN_LIST_CC event. In case of failure to
259 * receive the regd, initialize with a default world
262 if (ab->default_regd[pdev_id]) {
263 regd = ab->default_regd[pdev_id];
266 "failed to receive default regd during init\n");
267 regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
273 spin_unlock_bh(&ab->base_lock);
277 regd_len = sizeof(*regd) + (regd->n_reg_rules *
278 sizeof(struct ieee80211_reg_rule));
280 regd_copy = kzalloc(regd_len, GFP_ATOMIC);
282 ath11k_copy_regd(regd, regd_copy);
284 spin_unlock_bh(&ab->base_lock);
291 ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);
298 if (ar->state == ATH11K_STATE_ON) {
299 ret = ath11k_reg_update_chan_list(ar, true);
306 ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
310 static enum nl80211_dfs_regions
311 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
313 switch (dfs_region) {
314 case ATH11K_DFS_REG_FCC:
315 case ATH11K_DFS_REG_CN:
316 return NL80211_DFS_FCC;
317 case ATH11K_DFS_REG_ETSI:
318 case ATH11K_DFS_REG_KR:
319 return NL80211_DFS_ETSI;
320 case ATH11K_DFS_REG_MKK:
321 case ATH11K_DFS_REG_MKK_N:
322 return NL80211_DFS_JP;
324 return NL80211_DFS_UNSET;
328 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
332 if (reg_flags & REGULATORY_CHAN_NO_IR)
333 flags = NL80211_RRF_NO_IR;
335 if (reg_flags & REGULATORY_CHAN_RADAR)
336 flags |= NL80211_RRF_DFS;
338 if (reg_flags & REGULATORY_CHAN_NO_OFDM)
339 flags |= NL80211_RRF_NO_OFDM;
341 if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
342 flags |= NL80211_RRF_NO_OUTDOOR;
344 if (reg_flags & REGULATORY_CHAN_NO_HT40)
345 flags |= NL80211_RRF_NO_HT40;
347 if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
348 flags |= NL80211_RRF_NO_80MHZ;
350 if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
351 flags |= NL80211_RRF_NO_160MHZ;
356 static u32 ath11k_map_fw_phy_flags(u32 phy_flags)
360 if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)
361 flags |= NL80211_RRF_NO_HE;
367 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
368 struct ieee80211_reg_rule *rule2)
370 u32 start_freq1, end_freq1;
371 u32 start_freq2, end_freq2;
373 start_freq1 = rule1->freq_range.start_freq_khz;
374 start_freq2 = rule2->freq_range.start_freq_khz;
376 end_freq1 = rule1->freq_range.end_freq_khz;
377 end_freq2 = rule2->freq_range.end_freq_khz;
379 if ((start_freq1 >= start_freq2 &&
380 start_freq1 < end_freq2) ||
381 (start_freq2 > start_freq1 &&
382 start_freq2 < end_freq1))
385 /* TODO: Should we restrict intersection feasibility
386 * based on min bandwidth of the intersected region also,
387 * say the intersected rule should have a min bandwidth
394 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
395 struct ieee80211_reg_rule *rule2,
396 struct ieee80211_reg_rule *new_rule)
398 u32 start_freq1, end_freq1;
399 u32 start_freq2, end_freq2;
400 u32 freq_diff, max_bw;
402 start_freq1 = rule1->freq_range.start_freq_khz;
403 start_freq2 = rule2->freq_range.start_freq_khz;
405 end_freq1 = rule1->freq_range.end_freq_khz;
406 end_freq2 = rule2->freq_range.end_freq_khz;
408 new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
410 new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
412 freq_diff = new_rule->freq_range.end_freq_khz -
413 new_rule->freq_range.start_freq_khz;
414 max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
415 rule2->freq_range.max_bandwidth_khz);
416 new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
418 new_rule->power_rule.max_antenna_gain =
419 min_t(u32, rule1->power_rule.max_antenna_gain,
420 rule2->power_rule.max_antenna_gain);
422 new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
423 rule2->power_rule.max_eirp);
425 /* Use the flags of both the rules */
426 new_rule->flags = rule1->flags | rule2->flags;
428 /* To be safe, lts use the max cac timeout of both rules */
429 new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
433 static struct ieee80211_regdomain *
434 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
435 struct ieee80211_regdomain *curr_regd)
437 u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
438 struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
439 struct ieee80211_regdomain *new_regd = NULL;
442 num_old_regd_rules = default_regd->n_reg_rules;
443 num_curr_regd_rules = curr_regd->n_reg_rules;
444 num_new_regd_rules = 0;
446 /* Find the number of intersecting rules to allocate new regd memory */
447 for (i = 0; i < num_old_regd_rules; i++) {
448 old_rule = default_regd->reg_rules + i;
449 for (j = 0; j < num_curr_regd_rules; j++) {
450 curr_rule = curr_regd->reg_rules + j;
452 if (ath11k_reg_can_intersect(old_rule, curr_rule))
453 num_new_regd_rules++;
457 if (!num_new_regd_rules)
460 new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
461 sizeof(struct ieee80211_reg_rule)),
467 /* We set the new country and dfs region directly and only trim
468 * the freq, power, antenna gain by intersecting with the
469 * default regdomain. Also MAX of the dfs cac timeout is selected.
471 new_regd->n_reg_rules = num_new_regd_rules;
472 memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
473 new_regd->dfs_region = curr_regd->dfs_region;
474 new_rule = new_regd->reg_rules;
476 for (i = 0, k = 0; i < num_old_regd_rules; i++) {
477 old_rule = default_regd->reg_rules + i;
478 for (j = 0; j < num_curr_regd_rules; j++) {
479 curr_rule = curr_regd->reg_rules + j;
481 if (ath11k_reg_can_intersect(old_rule, curr_rule))
482 ath11k_reg_intersect_rules(old_rule, curr_rule,
490 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
492 switch (dfs_region) {
493 case NL80211_DFS_FCC:
495 case NL80211_DFS_ETSI:
505 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
509 if (end_freq <= start_freq)
512 bw = end_freq - start_freq;
513 bw = min_t(u16, bw, max_bw);
515 if (bw >= 80 && bw < 160)
517 else if (bw >= 40 && bw < 80)
519 else if (bw >= 20 && bw < 40)
528 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
529 u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
532 reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
533 reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
534 reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
535 reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
536 reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
537 reg_rule->flags = reg_flags;
541 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
542 struct ieee80211_regdomain *regd,
543 struct cur_reg_rule *reg_rule,
544 u8 *rule_idx, u32 flags, u16 max_bw)
553 /* there might be situations when even the input rule must be dropped */
556 /* frequencies below weather radar */
557 bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
558 ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
562 ath11k_reg_update_rule(regd->reg_rules + i,
563 reg_rule->start_freq,
564 ETSI_WEATHER_RADAR_BAND_LOW, bw,
565 reg_rule->ant_gain, reg_rule->reg_power,
568 ath11k_dbg(ab, ATH11K_DBG_REG,
569 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
570 i + 1, reg_rule->start_freq,
571 ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
572 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
576 /* weather radar frequencies */
577 start_freq = max_t(u32, reg_rule->start_freq,
578 ETSI_WEATHER_RADAR_BAND_LOW);
579 end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
581 bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
585 ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
586 end_freq, bw, reg_rule->ant_gain,
587 reg_rule->reg_power, flags);
589 regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
591 ath11k_dbg(ab, ATH11K_DBG_REG,
592 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
593 i + 1, start_freq, end_freq, bw,
594 reg_rule->ant_gain, reg_rule->reg_power,
595 regd->reg_rules[i].dfs_cac_ms, flags);
598 /* frequencies above weather radar */
599 bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
600 reg_rule->end_freq, max_bw);
604 ath11k_reg_update_rule(regd->reg_rules + i,
605 ETSI_WEATHER_RADAR_BAND_HIGH,
606 reg_rule->end_freq, bw,
607 reg_rule->ant_gain, reg_rule->reg_power,
610 ath11k_dbg(ab, ATH11K_DBG_REG,
611 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
612 i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
613 reg_rule->end_freq, bw, reg_rule->ant_gain,
614 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
621 enum wmi_reg_6ghz_ap_type
622 ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
624 switch (power_type) {
625 case IEEE80211_REG_LPI_AP:
626 return WMI_REG_INDOOR_AP;
627 case IEEE80211_REG_SP_AP:
628 return WMI_REG_STANDARD_POWER_AP;
629 case IEEE80211_REG_VLP_AP:
630 return WMI_REG_VERY_LOW_POWER_AP;
632 return WMI_REG_MAX_AP_TYPE;
636 struct ieee80211_regdomain *
637 ath11k_reg_build_regd(struct ath11k_base *ab,
638 struct cur_regulatory_info *reg_info, bool intersect,
639 enum wmi_vdev_type vdev_type,
640 enum ieee80211_ap_reg_power power_type)
642 struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
643 struct cur_reg_rule *reg_rule, *reg_rule_6ghz;
644 u8 i = 0, j = 0, k = 0;
647 u32 flags, reg_6ghz_number, max_bw_6ghz;
650 num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
652 if (reg_info->is_ext_reg_event) {
653 if (vdev_type == WMI_VDEV_TYPE_STA) {
654 enum wmi_reg_6ghz_ap_type ap_type;
656 ap_type = ath11k_reg_ap_pwr_convert(power_type);
658 if (ap_type == WMI_REG_MAX_AP_TYPE)
659 ap_type = WMI_REG_INDOOR_AP;
661 reg_6ghz_number = reg_info->num_6ghz_rules_client
662 [ap_type][WMI_REG_DEFAULT_CLIENT];
664 if (reg_6ghz_number == 0) {
665 ap_type = WMI_REG_INDOOR_AP;
666 reg_6ghz_number = reg_info->num_6ghz_rules_client
667 [ap_type][WMI_REG_DEFAULT_CLIENT];
670 reg_rule_6ghz = reg_info->reg_rules_6ghz_client_ptr
671 [ap_type][WMI_REG_DEFAULT_CLIENT];
672 max_bw_6ghz = reg_info->max_bw_6ghz_client
673 [ap_type][WMI_REG_DEFAULT_CLIENT];
675 reg_6ghz_number = reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
677 reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP];
678 max_bw_6ghz = reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP];
681 num_rules += reg_6ghz_number;
687 /* Add max additional rules to accommodate weather radar band */
688 if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
691 tmp_regd = kzalloc(sizeof(*tmp_regd) +
692 (num_rules * sizeof(struct ieee80211_reg_rule)),
697 memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
698 memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
700 tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
702 ath11k_dbg(ab, ATH11K_DBG_REG,
703 "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
704 alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
705 reg_info->dfs_region, num_rules);
706 /* Update reg_rules[] below. Firmware is expected to
707 * send these rules in order(2 GHz rules first and then 5 GHz)
709 for (; i < num_rules; i++) {
710 if (reg_info->num_2ghz_reg_rules &&
711 (i < reg_info->num_2ghz_reg_rules)) {
712 reg_rule = reg_info->reg_rules_2ghz_ptr + i;
713 max_bw = min_t(u16, reg_rule->max_bw,
714 reg_info->max_bw_2ghz);
716 } else if (reg_info->num_5ghz_reg_rules &&
717 (j < reg_info->num_5ghz_reg_rules)) {
718 reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
719 max_bw = min_t(u16, reg_rule->max_bw,
720 reg_info->max_bw_5ghz);
722 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for
723 * BW Auto correction, we can enable this by default
724 * for all 5G rules here. The regulatory core performs
725 * BW correction if required and applies flags as
726 * per other BW rule flags we pass from here
728 flags = NL80211_RRF_AUTO_BW;
729 } else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
730 k < reg_6ghz_number) {
731 reg_rule = reg_rule_6ghz + k++;
732 max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
733 flags = NL80211_RRF_AUTO_BW;
738 flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
739 flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);
741 ath11k_reg_update_rule(tmp_regd->reg_rules + i,
742 reg_rule->start_freq,
743 reg_rule->end_freq, max_bw,
744 reg_rule->ant_gain, reg_rule->reg_power,
747 /* Update dfs cac timeout if the dfs domain is ETSI and the
748 * new rule covers weather radar band.
749 * Default value of '0' corresponds to 60s timeout, so no
750 * need to update that for other rules.
752 if (flags & NL80211_RRF_DFS &&
753 reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
754 (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
755 reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
756 ath11k_reg_update_weather_radar_band(ab, tmp_regd,
762 if (reg_info->is_ext_reg_event) {
763 ath11k_dbg(ab, ATH11K_DBG_REG,
764 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
765 i + 1, reg_rule->start_freq, reg_rule->end_freq,
766 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
767 tmp_regd->reg_rules[i].dfs_cac_ms, flags,
768 reg_rule->psd_flag, reg_rule->psd_eirp);
770 ath11k_dbg(ab, ATH11K_DBG_REG,
771 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
772 i + 1, reg_rule->start_freq, reg_rule->end_freq,
773 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
774 tmp_regd->reg_rules[i].dfs_cac_ms,
779 tmp_regd->n_reg_rules = i;
782 default_regd = ab->default_regd[reg_info->phy_id];
784 /* Get a new regd by intersecting the received regd with
787 new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
790 ath11k_warn(ab, "Unable to create intersected regdomain\n");
801 static bool ath11k_reg_is_world_alpha(char *alpha)
803 if (alpha[0] == '0' && alpha[1] == '0')
806 if (alpha[0] == 'n' && alpha[1] == 'a')
812 static enum wmi_vdev_type ath11k_reg_get_ar_vdev_type(struct ath11k *ar)
814 struct ath11k_vif *arvif;
816 /* Currently each struct ath11k maps to one struct ieee80211_hw/wiphy
817 * and one struct ieee80211_regdomain, so it could only store one group
818 * reg rules. It means multi-interface concurrency in the same ath11k is
819 * not support for the regdomain. So get the vdev type of the first entry
820 * now. After concurrency support for the regdomain, this should change.
822 arvif = list_first_entry_or_null(&ar->arvifs, struct ath11k_vif, list);
824 return arvif->vdev_type;
826 return WMI_VDEV_TYPE_UNSPEC;
829 int ath11k_reg_handle_chan_list(struct ath11k_base *ab,
830 struct cur_regulatory_info *reg_info,
831 enum ieee80211_ap_reg_power power_type)
833 struct ieee80211_regdomain *regd;
834 bool intersect = false;
837 enum wmi_vdev_type vdev_type;
839 ath11k_dbg(ab, ATH11K_DBG_WMI, "event reg handle chan list");
841 if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
842 /* In case of failure to set the requested ctry,
843 * fw retains the current regd. We print a failure info
844 * and return from here.
846 ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
850 pdev_idx = reg_info->phy_id;
852 /* Avoid default reg rule updates sent during FW recovery if
853 * it is already available
855 spin_lock_bh(&ab->base_lock);
856 if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&
857 ab->default_regd[pdev_idx]) {
858 spin_unlock_bh(&ab->base_lock);
861 spin_unlock_bh(&ab->base_lock);
863 if (pdev_idx >= ab->num_radios) {
864 /* Process the event for phy0 only if single_pdev_only
865 * is true. If pdev_idx is valid but not 0, discard the
866 * event. Otherwise, it goes to fallback. In either case
867 * ath11k_reg_reset_info() needs to be called to avoid
870 ath11k_reg_reset_info(reg_info);
872 if (ab->hw_params.single_pdev_only &&
873 pdev_idx < ab->hw_params.num_rxmda_per_pdev)
878 /* Avoid multiple overwrites to default regd, during core
879 * stop-start after mac registration.
881 if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
882 !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
883 (char *)reg_info->alpha2, 2))
886 /* Intersect new rules with default regd if a new country setting was
887 * requested, i.e a default regd was already set during initialization
888 * and the regd coming from this event has a valid country info.
890 if (ab->default_regd[pdev_idx] &&
891 !ath11k_reg_is_world_alpha((char *)
892 ab->default_regd[pdev_idx]->alpha2) &&
893 !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
896 ar = ab->pdevs[pdev_idx].ar;
897 vdev_type = ath11k_reg_get_ar_vdev_type(ar);
899 ath11k_dbg(ab, ATH11K_DBG_WMI,
900 "wmi handle chan list power type %d vdev type %d intersect %d\n",
901 power_type, vdev_type, intersect);
903 regd = ath11k_reg_build_regd(ab, reg_info, intersect, vdev_type, power_type);
905 ath11k_warn(ab, "failed to build regd from reg_info\n");
909 if (power_type == IEEE80211_REG_UNSET_AP) {
910 ath11k_reg_reset_info(&ab->reg_info_store[pdev_idx]);
911 ab->reg_info_store[pdev_idx] = *reg_info;
914 spin_lock_bh(&ab->base_lock);
915 if (ab->default_regd[pdev_idx]) {
916 /* The initial rules from FW after WMI Init is to build
917 * the default regd. From then on, any rules updated for
918 * the pdev could be due to user reg changes.
919 * Free previously built regd before assigning the newly
920 * generated regd to ar. NULL pointer handling will be
921 * taken care by kfree itself.
923 ar = ab->pdevs[pdev_idx].ar;
924 kfree(ab->new_regd[pdev_idx]);
925 ab->new_regd[pdev_idx] = regd;
926 queue_work(ab->workqueue, &ar->regd_update_work);
928 /* This regd would be applied during mac registration and is
929 * held constant throughout for regd intersection purpose
931 ab->default_regd[pdev_idx] = regd;
933 ab->dfs_region = reg_info->dfs_region;
934 spin_unlock_bh(&ab->base_lock);
939 /* Fallback to older reg (by sending previous country setting
940 * again if fw has succeeded and we failed to process here.
941 * The Regdomain should be uniform across driver and fw. Since the
942 * FW has processed the command and sent a success status, we expect
943 * this function to succeed as well. If it doesn't, CTRY needs to be
944 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
946 /* TODO: This is rare, but still should also be handled */
954 void ath11k_regd_update_work(struct work_struct *work)
956 struct ath11k *ar = container_of(work, struct ath11k,
960 ret = ath11k_regd_update(ar);
962 /* Firmware has already moved to the new regd. We need
963 * to maintain channel consistency across FW, Host driver
964 * and userspace. Hence as a fallback mechanism we can set
965 * the prev or default country code to the firmware.
967 /* TODO: Implement Fallback Mechanism */
971 void ath11k_reg_init(struct ath11k *ar)
973 ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
974 ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
977 void ath11k_reg_reset_info(struct cur_regulatory_info *reg_info)
984 kfree(reg_info->reg_rules_2ghz_ptr);
985 kfree(reg_info->reg_rules_5ghz_ptr);
987 for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
988 kfree(reg_info->reg_rules_6ghz_ap_ptr[i]);
990 for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
991 kfree(reg_info->reg_rules_6ghz_client_ptr[i][j]);
994 memset(reg_info, 0, sizeof(*reg_info));
997 void ath11k_reg_free(struct ath11k_base *ab)
1001 for (i = 0; i < ab->num_radios; i++)
1002 ath11k_reg_reset_info(&ab->reg_info_store[i]);
1004 kfree(ab->reg_info_store);
1005 ab->reg_info_store = NULL;
1007 for (i = 0; i < ab->hw_params.max_radios; i++) {
1008 kfree(ab->default_regd[i]);
1009 kfree(ab->new_regd[i]);