1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2024 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 ath11k *ar = hw->priv;
55 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
56 "Regulatory Notification received for %s\n", wiphy_name(wiphy));
58 /* Currently supporting only General User Hints. Cell base user
59 * hints to be handled later.
60 * Hints from other sources like Core, Beacons are not expected for
61 * self managed wiphy's
63 if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
64 request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
65 ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
69 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
70 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
71 "Country Setting is not allowed\n");
75 if (!ath11k_regdom_changes(ar, request->alpha2)) {
76 ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
80 /* Set the country code to the firmware and will receive
81 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
84 if (ar->ab->hw_params.current_cc_support) {
85 memcpy(&ar->alpha2, request->alpha2, 2);
86 ret = ath11k_reg_set_cc(ar);
89 "failed set current country code: %d\n", ret);
91 init_country_param.flags = ALPHA_IS_SET;
92 memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
93 init_country_param.cc_info.alpha2[2] = 0;
95 ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
98 "INIT Country code set to fw failed : %d\n", ret);
101 ath11k_mac_11d_scan_stop(ar);
102 ar->regdom_set_by_user = true;
105 int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)
107 struct ieee80211_supported_band **bands;
108 struct scan_chan_list_params *params;
109 struct ieee80211_channel *channel;
110 struct ieee80211_hw *hw = ar->hw;
111 struct channel_param *ch;
112 enum nl80211_band band;
113 int num_channels = 0;
116 if (wait && ar->state_11d != ATH11K_11D_IDLE) {
117 left = wait_for_completion_timeout(&ar->completed_11d_scan,
118 ATH11K_SCAN_TIMEOUT_HZ);
120 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
121 "failed to receive 11d scan complete: timed out\n");
122 ar->state_11d = ATH11K_11D_IDLE;
124 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
125 "11d scan wait left time %d\n", left);
129 (ar->scan.state == ATH11K_SCAN_STARTING ||
130 ar->scan.state == ATH11K_SCAN_RUNNING)) {
131 left = wait_for_completion_timeout(&ar->scan.completed,
132 ATH11K_SCAN_TIMEOUT_HZ);
134 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
135 "failed to receive hw scan complete: timed out\n");
137 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
138 "hw scan wait left time %d\n", left);
141 if (ar->state == ATH11K_STATE_RESTARTING)
144 bands = hw->wiphy->bands;
145 for (band = 0; band < NUM_NL80211_BANDS; band++) {
149 for (i = 0; i < bands[band]->n_channels; i++) {
150 if (bands[band]->channels[i].flags &
151 IEEE80211_CHAN_DISABLED)
158 if (WARN_ON(!num_channels))
161 params = kzalloc(struct_size(params, ch_param, num_channels),
166 params->pdev_id = ar->pdev->pdev_id;
167 params->nallchans = num_channels;
169 ch = params->ch_param;
171 for (band = 0; band < NUM_NL80211_BANDS; band++) {
175 for (i = 0; i < bands[band]->n_channels; i++) {
176 channel = &bands[band]->channels[i];
178 if (channel->flags & IEEE80211_CHAN_DISABLED)
181 /* TODO: Set to true/false based on some condition? */
183 ch->allow_vht = true;
187 !!(channel->flags & IEEE80211_CHAN_RADAR);
188 ch->is_chan_passive = !!(channel->flags &
189 IEEE80211_CHAN_NO_IR);
190 ch->is_chan_passive |= ch->dfs_set;
191 ch->mhz = channel->center_freq;
192 ch->cfreq1 = channel->center_freq;
194 ch->maxpower = channel->max_power * 2;
195 ch->maxregpower = channel->max_reg_power * 2;
196 ch->antennamax = channel->max_antenna_gain * 2;
198 /* TODO: Use appropriate phymodes */
199 if (channel->band == NL80211_BAND_2GHZ)
200 ch->phy_mode = MODE_11G;
202 ch->phy_mode = MODE_11A;
204 if (channel->band == NL80211_BAND_6GHZ &&
205 cfg80211_channel_is_psc(channel))
206 ch->psc_channel = true;
208 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
209 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
210 i, params->nallchans,
211 ch->mhz, ch->maxpower, ch->maxregpower,
212 ch->antennamax, ch->phy_mode);
215 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
216 * set_agile, reg_class_idx
221 ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
227 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
228 struct ieee80211_regdomain *regd_copy)
232 /* The caller should have checked error conditions */
233 memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
235 for (i = 0; i < regd_orig->n_reg_rules; i++)
236 memcpy(®d_copy->reg_rules[i], ®d_orig->reg_rules[i],
237 sizeof(struct ieee80211_reg_rule));
240 int ath11k_regd_update(struct ath11k *ar)
242 struct ieee80211_regdomain *regd, *regd_copy = NULL;
243 int ret, regd_len, pdev_id;
244 struct ath11k_base *ab;
247 pdev_id = ar->pdev_idx;
249 spin_lock_bh(&ab->base_lock);
251 /* Prefer the latest regd update over default if it's available */
252 if (ab->new_regd[pdev_id]) {
253 regd = ab->new_regd[pdev_id];
255 /* Apply the regd received during init through
256 * WMI_REG_CHAN_LIST_CC event. In case of failure to
257 * receive the regd, initialize with a default world
260 if (ab->default_regd[pdev_id]) {
261 regd = ab->default_regd[pdev_id];
264 "failed to receive default regd during init\n");
265 regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
271 spin_unlock_bh(&ab->base_lock);
275 regd_len = sizeof(*regd) + (regd->n_reg_rules *
276 sizeof(struct ieee80211_reg_rule));
278 regd_copy = kzalloc(regd_len, GFP_ATOMIC);
280 ath11k_copy_regd(regd, regd_copy);
282 spin_unlock_bh(&ab->base_lock);
289 ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);
296 if (ar->state == ATH11K_STATE_ON) {
297 ret = ath11k_reg_update_chan_list(ar, true);
304 ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
308 static enum nl80211_dfs_regions
309 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
311 switch (dfs_region) {
312 case ATH11K_DFS_REG_FCC:
313 case ATH11K_DFS_REG_CN:
314 return NL80211_DFS_FCC;
315 case ATH11K_DFS_REG_ETSI:
316 case ATH11K_DFS_REG_KR:
317 return NL80211_DFS_ETSI;
318 case ATH11K_DFS_REG_MKK:
319 case ATH11K_DFS_REG_MKK_N:
320 return NL80211_DFS_JP;
322 return NL80211_DFS_UNSET;
326 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
330 if (reg_flags & REGULATORY_CHAN_NO_IR)
331 flags = NL80211_RRF_NO_IR;
333 if (reg_flags & REGULATORY_CHAN_RADAR)
334 flags |= NL80211_RRF_DFS;
336 if (reg_flags & REGULATORY_CHAN_NO_OFDM)
337 flags |= NL80211_RRF_NO_OFDM;
339 if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
340 flags |= NL80211_RRF_NO_OUTDOOR;
342 if (reg_flags & REGULATORY_CHAN_NO_HT40)
343 flags |= NL80211_RRF_NO_HT40;
345 if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
346 flags |= NL80211_RRF_NO_80MHZ;
348 if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
349 flags |= NL80211_RRF_NO_160MHZ;
354 static u32 ath11k_map_fw_phy_flags(u32 phy_flags)
358 if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)
359 flags |= NL80211_RRF_NO_HE;
365 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
366 struct ieee80211_reg_rule *rule2)
368 u32 start_freq1, end_freq1;
369 u32 start_freq2, end_freq2;
371 start_freq1 = rule1->freq_range.start_freq_khz;
372 start_freq2 = rule2->freq_range.start_freq_khz;
374 end_freq1 = rule1->freq_range.end_freq_khz;
375 end_freq2 = rule2->freq_range.end_freq_khz;
377 if ((start_freq1 >= start_freq2 &&
378 start_freq1 < end_freq2) ||
379 (start_freq2 > start_freq1 &&
380 start_freq2 < end_freq1))
383 /* TODO: Should we restrict intersection feasibility
384 * based on min bandwidth of the intersected region also,
385 * say the intersected rule should have a min bandwidth
392 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
393 struct ieee80211_reg_rule *rule2,
394 struct ieee80211_reg_rule *new_rule)
396 u32 start_freq1, end_freq1;
397 u32 start_freq2, end_freq2;
398 u32 freq_diff, max_bw;
400 start_freq1 = rule1->freq_range.start_freq_khz;
401 start_freq2 = rule2->freq_range.start_freq_khz;
403 end_freq1 = rule1->freq_range.end_freq_khz;
404 end_freq2 = rule2->freq_range.end_freq_khz;
406 new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
408 new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
410 freq_diff = new_rule->freq_range.end_freq_khz -
411 new_rule->freq_range.start_freq_khz;
412 max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
413 rule2->freq_range.max_bandwidth_khz);
414 new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
416 new_rule->power_rule.max_antenna_gain =
417 min_t(u32, rule1->power_rule.max_antenna_gain,
418 rule2->power_rule.max_antenna_gain);
420 new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
421 rule2->power_rule.max_eirp);
423 /* Use the flags of both the rules */
424 new_rule->flags = rule1->flags | rule2->flags;
426 if ((rule1->flags & NL80211_RRF_PSD) && (rule2->flags & NL80211_RRF_PSD))
427 new_rule->psd = min_t(s8, rule1->psd, rule2->psd);
429 new_rule->flags &= ~NL80211_RRF_PSD;
431 /* To be safe, lts use the max cac timeout of both rules */
432 new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
436 static struct ieee80211_regdomain *
437 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
438 struct ieee80211_regdomain *curr_regd)
440 u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
441 struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
442 struct ieee80211_regdomain *new_regd = NULL;
445 num_old_regd_rules = default_regd->n_reg_rules;
446 num_curr_regd_rules = curr_regd->n_reg_rules;
447 num_new_regd_rules = 0;
449 /* Find the number of intersecting rules to allocate new regd memory */
450 for (i = 0; i < num_old_regd_rules; i++) {
451 old_rule = default_regd->reg_rules + i;
452 for (j = 0; j < num_curr_regd_rules; j++) {
453 curr_rule = curr_regd->reg_rules + j;
455 if (ath11k_reg_can_intersect(old_rule, curr_rule))
456 num_new_regd_rules++;
460 if (!num_new_regd_rules)
463 new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
464 sizeof(struct ieee80211_reg_rule)),
470 /* We set the new country and dfs region directly and only trim
471 * the freq, power, antenna gain by intersecting with the
472 * default regdomain. Also MAX of the dfs cac timeout is selected.
474 new_regd->n_reg_rules = num_new_regd_rules;
475 memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
476 new_regd->dfs_region = curr_regd->dfs_region;
477 new_rule = new_regd->reg_rules;
479 for (i = 0, k = 0; i < num_old_regd_rules; i++) {
480 old_rule = default_regd->reg_rules + i;
481 for (j = 0; j < num_curr_regd_rules; j++) {
482 curr_rule = curr_regd->reg_rules + j;
484 if (ath11k_reg_can_intersect(old_rule, curr_rule))
485 ath11k_reg_intersect_rules(old_rule, curr_rule,
493 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
495 switch (dfs_region) {
496 case NL80211_DFS_FCC:
498 case NL80211_DFS_ETSI:
508 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
512 if (end_freq <= start_freq)
515 bw = end_freq - start_freq;
516 bw = min_t(u16, bw, max_bw);
518 if (bw >= 80 && bw < 160)
520 else if (bw >= 40 && bw < 80)
522 else if (bw >= 20 && bw < 40)
531 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
532 u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
533 s8 psd, u32 reg_flags)
535 reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
536 reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
537 reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
538 reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
539 reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
541 reg_rule->flags = reg_flags;
545 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
546 struct ieee80211_regdomain *regd,
547 struct cur_reg_rule *reg_rule,
548 u8 *rule_idx, u32 flags, u16 max_bw)
557 /* there might be situations when even the input rule must be dropped */
560 /* frequencies below weather radar */
561 bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
562 ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
566 ath11k_reg_update_rule(regd->reg_rules + i,
567 reg_rule->start_freq,
568 ETSI_WEATHER_RADAR_BAND_LOW, bw,
569 reg_rule->ant_gain, reg_rule->reg_power,
570 reg_rule->psd_eirp, flags);
572 ath11k_dbg(ab, ATH11K_DBG_REG,
573 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
574 i + 1, reg_rule->start_freq,
575 ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
576 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
580 /* weather radar frequencies */
581 start_freq = max_t(u32, reg_rule->start_freq,
582 ETSI_WEATHER_RADAR_BAND_LOW);
583 end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
585 bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
589 ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
590 end_freq, bw, reg_rule->ant_gain,
591 reg_rule->reg_power, reg_rule->psd_eirp, flags);
593 regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
595 ath11k_dbg(ab, ATH11K_DBG_REG,
596 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
597 i + 1, start_freq, end_freq, bw,
598 reg_rule->ant_gain, reg_rule->reg_power,
599 regd->reg_rules[i].dfs_cac_ms, flags);
602 /* frequencies above weather radar */
603 bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
604 reg_rule->end_freq, max_bw);
608 ath11k_reg_update_rule(regd->reg_rules + i,
609 ETSI_WEATHER_RADAR_BAND_HIGH,
610 reg_rule->end_freq, bw,
611 reg_rule->ant_gain, reg_rule->reg_power,
612 reg_rule->psd_eirp, flags);
614 ath11k_dbg(ab, ATH11K_DBG_REG,
615 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
616 i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
617 reg_rule->end_freq, bw, reg_rule->ant_gain,
618 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
625 enum wmi_reg_6ghz_ap_type
626 ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
628 switch (power_type) {
629 case IEEE80211_REG_LPI_AP:
630 return WMI_REG_INDOOR_AP;
631 case IEEE80211_REG_SP_AP:
632 return WMI_REG_STANDARD_POWER_AP;
633 case IEEE80211_REG_VLP_AP:
634 return WMI_REG_VERY_LOW_POWER_AP;
636 return WMI_REG_MAX_AP_TYPE;
640 struct ieee80211_regdomain *
641 ath11k_reg_build_regd(struct ath11k_base *ab,
642 struct cur_regulatory_info *reg_info, bool intersect,
643 enum wmi_vdev_type vdev_type,
644 enum ieee80211_ap_reg_power power_type)
646 struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
647 struct cur_reg_rule *reg_rule, *reg_rule_6ghz;
648 u8 i = 0, j = 0, k = 0;
651 u32 flags, reg_6ghz_number, max_bw_6ghz;
654 num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
656 if (reg_info->is_ext_reg_event) {
657 if (vdev_type == WMI_VDEV_TYPE_STA) {
658 enum wmi_reg_6ghz_ap_type ap_type;
660 ap_type = ath11k_reg_ap_pwr_convert(power_type);
662 if (ap_type == WMI_REG_MAX_AP_TYPE)
663 ap_type = WMI_REG_INDOOR_AP;
665 reg_6ghz_number = reg_info->num_6ghz_rules_client
666 [ap_type][WMI_REG_DEFAULT_CLIENT];
668 if (reg_6ghz_number == 0) {
669 ap_type = WMI_REG_INDOOR_AP;
670 reg_6ghz_number = reg_info->num_6ghz_rules_client
671 [ap_type][WMI_REG_DEFAULT_CLIENT];
674 reg_rule_6ghz = reg_info->reg_rules_6ghz_client_ptr
675 [ap_type][WMI_REG_DEFAULT_CLIENT];
676 max_bw_6ghz = reg_info->max_bw_6ghz_client
677 [ap_type][WMI_REG_DEFAULT_CLIENT];
679 reg_6ghz_number = reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
681 reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP];
682 max_bw_6ghz = reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP];
685 num_rules += reg_6ghz_number;
691 /* Add max additional rules to accommodate weather radar band */
692 if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
695 tmp_regd = kzalloc(sizeof(*tmp_regd) +
696 (num_rules * sizeof(struct ieee80211_reg_rule)),
701 memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
702 memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
704 tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
706 ath11k_dbg(ab, ATH11K_DBG_REG,
707 "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
708 alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
709 reg_info->dfs_region, num_rules);
710 /* Update reg_rules[] below. Firmware is expected to
711 * send these rules in order(2 GHz rules first and then 5 GHz)
713 for (; i < num_rules; i++) {
714 if (reg_info->num_2ghz_reg_rules &&
715 (i < reg_info->num_2ghz_reg_rules)) {
716 reg_rule = reg_info->reg_rules_2ghz_ptr + i;
717 max_bw = min_t(u16, reg_rule->max_bw,
718 reg_info->max_bw_2ghz);
720 } else if (reg_info->num_5ghz_reg_rules &&
721 (j < reg_info->num_5ghz_reg_rules)) {
722 reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
723 max_bw = min_t(u16, reg_rule->max_bw,
724 reg_info->max_bw_5ghz);
726 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for
727 * BW Auto correction, we can enable this by default
728 * for all 5G rules here. The regulatory core performs
729 * BW correction if required and applies flags as
730 * per other BW rule flags we pass from here
732 flags = NL80211_RRF_AUTO_BW;
733 } else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
734 k < reg_6ghz_number) {
735 reg_rule = reg_rule_6ghz + k++;
736 max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
737 flags = NL80211_RRF_AUTO_BW;
738 if (reg_rule->psd_flag)
739 flags |= NL80211_RRF_PSD;
744 flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
745 flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);
747 ath11k_reg_update_rule(tmp_regd->reg_rules + i,
748 reg_rule->start_freq,
749 reg_rule->end_freq, max_bw,
750 reg_rule->ant_gain, reg_rule->reg_power,
751 reg_rule->psd_eirp, flags);
753 /* Update dfs cac timeout if the dfs domain is ETSI and the
754 * new rule covers weather radar band.
755 * Default value of '0' corresponds to 60s timeout, so no
756 * need to update that for other rules.
758 if (flags & NL80211_RRF_DFS &&
759 reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
760 (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
761 reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
762 ath11k_reg_update_weather_radar_band(ab, tmp_regd,
768 if (reg_info->is_ext_reg_event) {
769 ath11k_dbg(ab, ATH11K_DBG_REG,
770 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
771 i + 1, reg_rule->start_freq, reg_rule->end_freq,
772 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
773 tmp_regd->reg_rules[i].dfs_cac_ms, flags,
774 reg_rule->psd_flag, reg_rule->psd_eirp);
776 ath11k_dbg(ab, ATH11K_DBG_REG,
777 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
778 i + 1, reg_rule->start_freq, reg_rule->end_freq,
779 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
780 tmp_regd->reg_rules[i].dfs_cac_ms,
785 tmp_regd->n_reg_rules = i;
788 default_regd = ab->default_regd[reg_info->phy_id];
790 /* Get a new regd by intersecting the received regd with
793 new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
796 ath11k_warn(ab, "Unable to create intersected regdomain\n");
807 static bool ath11k_reg_is_world_alpha(char *alpha)
809 if (alpha[0] == '0' && alpha[1] == '0')
812 if (alpha[0] == 'n' && alpha[1] == 'a')
818 static enum wmi_vdev_type ath11k_reg_get_ar_vdev_type(struct ath11k *ar)
820 struct ath11k_vif *arvif;
822 /* Currently each struct ath11k maps to one struct ieee80211_hw/wiphy
823 * and one struct ieee80211_regdomain, so it could only store one group
824 * reg rules. It means multi-interface concurrency in the same ath11k is
825 * not support for the regdomain. So get the vdev type of the first entry
826 * now. After concurrency support for the regdomain, this should change.
828 arvif = list_first_entry_or_null(&ar->arvifs, struct ath11k_vif, list);
830 return arvif->vdev_type;
832 return WMI_VDEV_TYPE_UNSPEC;
835 int ath11k_reg_handle_chan_list(struct ath11k_base *ab,
836 struct cur_regulatory_info *reg_info,
837 enum ieee80211_ap_reg_power power_type)
839 struct ieee80211_regdomain *regd;
840 bool intersect = false;
843 enum wmi_vdev_type vdev_type;
845 ath11k_dbg(ab, ATH11K_DBG_WMI, "event reg handle chan list");
847 if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
848 /* In case of failure to set the requested ctry,
849 * fw retains the current regd. We print a failure info
850 * and return from here.
852 ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
856 pdev_idx = reg_info->phy_id;
858 /* Avoid default reg rule updates sent during FW recovery if
859 * it is already available
861 spin_lock_bh(&ab->base_lock);
862 if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&
863 ab->default_regd[pdev_idx]) {
864 spin_unlock_bh(&ab->base_lock);
867 spin_unlock_bh(&ab->base_lock);
869 if (pdev_idx >= ab->num_radios) {
870 /* Process the event for phy0 only if single_pdev_only
871 * is true. If pdev_idx is valid but not 0, discard the
872 * event. Otherwise, it goes to fallback. In either case
873 * ath11k_reg_reset_info() needs to be called to avoid
876 ath11k_reg_reset_info(reg_info);
878 if (ab->hw_params.single_pdev_only &&
879 pdev_idx < ab->hw_params.num_rxdma_per_pdev)
884 /* Avoid multiple overwrites to default regd, during core
885 * stop-start after mac registration.
887 if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
888 !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
889 (char *)reg_info->alpha2, 2))
892 /* Intersect new rules with default regd if a new country setting was
893 * requested, i.e a default regd was already set during initialization
894 * and the regd coming from this event has a valid country info.
896 if (ab->default_regd[pdev_idx] &&
897 !ath11k_reg_is_world_alpha((char *)
898 ab->default_regd[pdev_idx]->alpha2) &&
899 !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
902 ar = ab->pdevs[pdev_idx].ar;
903 vdev_type = ath11k_reg_get_ar_vdev_type(ar);
905 ath11k_dbg(ab, ATH11K_DBG_WMI,
906 "wmi handle chan list power type %d vdev type %d intersect %d\n",
907 power_type, vdev_type, intersect);
909 regd = ath11k_reg_build_regd(ab, reg_info, intersect, vdev_type, power_type);
911 ath11k_warn(ab, "failed to build regd from reg_info\n");
915 if (power_type == IEEE80211_REG_UNSET_AP) {
916 ath11k_reg_reset_info(&ab->reg_info_store[pdev_idx]);
917 ab->reg_info_store[pdev_idx] = *reg_info;
920 spin_lock_bh(&ab->base_lock);
921 if (ab->default_regd[pdev_idx]) {
922 /* The initial rules from FW after WMI Init is to build
923 * the default regd. From then on, any rules updated for
924 * the pdev could be due to user reg changes.
925 * Free previously built regd before assigning the newly
926 * generated regd to ar. NULL pointer handling will be
927 * taken care by kfree itself.
929 ar = ab->pdevs[pdev_idx].ar;
930 kfree(ab->new_regd[pdev_idx]);
931 ab->new_regd[pdev_idx] = regd;
932 queue_work(ab->workqueue, &ar->regd_update_work);
934 /* This regd would be applied during mac registration and is
935 * held constant throughout for regd intersection purpose
937 ab->default_regd[pdev_idx] = regd;
939 ab->dfs_region = reg_info->dfs_region;
940 spin_unlock_bh(&ab->base_lock);
945 /* Fallback to older reg (by sending previous country setting
946 * again if fw has succeeded and we failed to process here.
947 * The Regdomain should be uniform across driver and fw. Since the
948 * FW has processed the command and sent a success status, we expect
949 * this function to succeed as well. If it doesn't, CTRY needs to be
950 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
952 /* TODO: This is rare, but still should also be handled */
960 void ath11k_regd_update_work(struct work_struct *work)
962 struct ath11k *ar = container_of(work, struct ath11k,
966 ret = ath11k_regd_update(ar);
968 /* Firmware has already moved to the new regd. We need
969 * to maintain channel consistency across FW, Host driver
970 * and userspace. Hence as a fallback mechanism we can set
971 * the prev or default country code to the firmware.
973 /* TODO: Implement Fallback Mechanism */
977 void ath11k_reg_init(struct ath11k *ar)
979 ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
980 ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
983 void ath11k_reg_reset_info(struct cur_regulatory_info *reg_info)
990 kfree(reg_info->reg_rules_2ghz_ptr);
991 kfree(reg_info->reg_rules_5ghz_ptr);
993 for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
994 kfree(reg_info->reg_rules_6ghz_ap_ptr[i]);
996 for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
997 kfree(reg_info->reg_rules_6ghz_client_ptr[i][j]);
1000 memset(reg_info, 0, sizeof(*reg_info));
1003 void ath11k_reg_free(struct ath11k_base *ab)
1007 for (i = 0; i < ab->num_radios; i++)
1008 ath11k_reg_reset_info(&ab->reg_info_store[i]);
1010 kfree(ab->reg_info_store);
1011 ab->reg_info_store = NULL;
1013 for (i = 0; i < ab->hw_params.max_radios; i++) {
1014 kfree(ab->default_regd[i]);
1015 kfree(ab->new_regd[i]);
1019 int ath11k_reg_set_cc(struct ath11k *ar)
1021 struct wmi_set_current_country_params set_current_param = {};
1023 memcpy(&set_current_param.alpha2, ar->alpha2, 2);
1024 return ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);