Path: blob/main/sys/contrib/dev/athk/ath11k/reg.c
105687 views
// SPDX-License-Identifier: BSD-3-Clause-Clear1/*2* Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.3* Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved.4*/5#include <linux/rtnetlink.h>67#include "core.h"8#include "debug.h"910/* World regdom to be used in case default regd from fw is unavailable */11#define ATH11K_2GHZ_CH01_11 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)12#define ATH11K_5GHZ_5150_5350 REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\13NL80211_RRF_NO_IR)14#define ATH11K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\15NL80211_RRF_NO_IR)1617#define ETSI_WEATHER_RADAR_BAND_LOW 559018#define ETSI_WEATHER_RADAR_BAND_HIGH 565019#define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT 6000002021static const struct ieee80211_regdomain ath11k_world_regd = {22.n_reg_rules = 3,23.alpha2 = "00",24.reg_rules = {25ATH11K_2GHZ_CH01_11,26ATH11K_5GHZ_5150_5350,27ATH11K_5GHZ_5725_5850,28}29};3031static bool ath11k_regdom_changes(struct ath11k *ar, char *alpha2)32{33const struct ieee80211_regdomain *regd;3435regd = rcu_dereference_rtnl(ar->hw->wiphy->regd);36/* This can happen during wiphy registration where the previous37* user request is received before we update the regd received38* from firmware.39*/40if (!regd)41return true;4243return memcmp(regd->alpha2, alpha2, 2) != 0;44}4546static void47ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)48{49struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);50struct wmi_init_country_params init_country_param;51struct ath11k *ar = hw->priv;52int ret;5354ath11k_dbg(ar->ab, ATH11K_DBG_REG,55"Regulatory Notification received for %s\n", wiphy_name(wiphy));5657if (request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {58ath11k_dbg(ar->ab, ATH11K_DBG_REG,59"driver initiated regd update\n");60if (ar->state != ATH11K_STATE_ON)61return;6263ret = ath11k_reg_update_chan_list(ar, true);64if (ret)65ath11k_warn(ar->ab, "failed to update channel list: %d\n", ret);6667return;68}6970/* Currently supporting only General User Hints. Cell base user71* hints to be handled later.72* Hints from other sources like Core, Beacons are not expected for73* self managed wiphy's74*/75if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&76request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {77ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");78return;79}8081if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {82ath11k_dbg(ar->ab, ATH11K_DBG_REG,83"Country Setting is not allowed\n");84return;85}8687if (!ath11k_regdom_changes(ar, request->alpha2)) {88ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");89return;90}9192/* Set the country code to the firmware and will receive93* the WMI_REG_CHAN_LIST_CC EVENT for updating the94* reg info95*/96if (ar->ab->hw_params.current_cc_support) {97memcpy(&ar->alpha2, request->alpha2, 2);98ret = ath11k_reg_set_cc(ar);99if (ret)100ath11k_warn(ar->ab,101"failed set current country code: %d\n", ret);102} else {103init_country_param.flags = ALPHA_IS_SET;104memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);105init_country_param.cc_info.alpha2[2] = 0;106107ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);108if (ret)109ath11k_warn(ar->ab,110"INIT Country code set to fw failed : %d\n", ret);111}112113ath11k_mac_11d_scan_stop(ar);114ar->regdom_set_by_user = true;115}116117int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)118{119struct ieee80211_supported_band **bands;120struct scan_chan_list_params *params;121struct ieee80211_channel *channel;122struct ieee80211_hw *hw = ar->hw;123struct channel_param *ch;124enum nl80211_band band;125int num_channels = 0;126int i, ret = 0;127128if (ar->state == ATH11K_STATE_RESTARTING)129return 0;130131bands = hw->wiphy->bands;132for (band = 0; band < NUM_NL80211_BANDS; band++) {133if (!bands[band])134continue;135136for (i = 0; i < bands[band]->n_channels; i++) {137if (bands[band]->channels[i].flags &138IEEE80211_CHAN_DISABLED)139continue;140141num_channels++;142}143}144145if (WARN_ON(!num_channels))146return -EINVAL;147148params = kzalloc(struct_size(params, ch_param, num_channels),149GFP_KERNEL);150if (!params)151return -ENOMEM;152153params->pdev_id = ar->pdev->pdev_id;154params->nallchans = num_channels;155156ch = params->ch_param;157158for (band = 0; band < NUM_NL80211_BANDS; band++) {159if (!bands[band])160continue;161162for (i = 0; i < bands[band]->n_channels; i++) {163channel = &bands[band]->channels[i];164165if (channel->flags & IEEE80211_CHAN_DISABLED)166continue;167168/* TODO: Set to true/false based on some condition? */169ch->allow_ht = true;170ch->allow_vht = true;171ch->allow_he = true;172173ch->dfs_set =174!!(channel->flags & IEEE80211_CHAN_RADAR);175ch->is_chan_passive = !!(channel->flags &176IEEE80211_CHAN_NO_IR);177ch->is_chan_passive |= ch->dfs_set;178ch->mhz = channel->center_freq;179ch->cfreq1 = channel->center_freq;180ch->minpower = 0;181ch->maxpower = channel->max_power * 2;182ch->maxregpower = channel->max_reg_power * 2;183ch->antennamax = channel->max_antenna_gain * 2;184185/* TODO: Use appropriate phymodes */186if (channel->band == NL80211_BAND_2GHZ)187ch->phy_mode = MODE_11G;188else189ch->phy_mode = MODE_11A;190191if (channel->band == NL80211_BAND_6GHZ &&192cfg80211_channel_is_psc(channel))193ch->psc_channel = true;194195ath11k_dbg(ar->ab, ATH11K_DBG_WMI,196"mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",197i, params->nallchans,198ch->mhz, ch->maxpower, ch->maxregpower,199ch->antennamax, ch->phy_mode);200201ch++;202/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2203* set_agile, reg_class_idx204*/205}206}207208if (wait) {209spin_lock_bh(&ar->data_lock);210list_add_tail(¶ms->list, &ar->channel_update_queue);211spin_unlock_bh(&ar->data_lock);212213queue_work(ar->ab->workqueue, &ar->channel_update_work);214215return 0;216}217218ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);219kfree(params);220221return ret;222}223224#if defined(__linux__)225static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,226#elif defined(__FreeBSD__)227static void ath11k_copy_regd(const struct ieee80211_regdomain *regd_orig,228#endif229struct ieee80211_regdomain *regd_copy)230{231u8 i;232233/* The caller should have checked error conditions */234memcpy(regd_copy, regd_orig, sizeof(*regd_orig));235236for (i = 0; i < regd_orig->n_reg_rules; i++)237memcpy(®d_copy->reg_rules[i], ®d_orig->reg_rules[i],238sizeof(struct ieee80211_reg_rule));239}240241int ath11k_regd_update(struct ath11k *ar)242{243#if defined(__linux__)244struct ieee80211_regdomain *regd, *regd_copy = NULL;245#elif defined(__FreeBSD__)246const struct ieee80211_regdomain *regd;247struct ieee80211_regdomain *regd_copy = NULL;248#endif249int ret, regd_len, pdev_id;250struct ath11k_base *ab;251252ab = ar->ab;253pdev_id = ar->pdev_idx;254255spin_lock_bh(&ab->base_lock);256257/* Prefer the latest regd update over default if it's available */258if (ab->new_regd[pdev_id]) {259regd = ab->new_regd[pdev_id];260} else {261/* Apply the regd received during init through262* WMI_REG_CHAN_LIST_CC event. In case of failure to263* receive the regd, initialize with a default world264* regulatory.265*/266if (ab->default_regd[pdev_id]) {267regd = ab->default_regd[pdev_id];268} else {269ath11k_warn(ab,270"failed to receive default regd during init\n");271#if defined(__linux__)272regd = (struct ieee80211_regdomain *)&ath11k_world_regd;273#elif defined(__FreeBSD__)274regd = &ath11k_world_regd;275#endif276}277}278279if (!regd) {280ret = -EINVAL;281spin_unlock_bh(&ab->base_lock);282goto err;283}284285regd_len = sizeof(*regd) + (regd->n_reg_rules *286sizeof(struct ieee80211_reg_rule));287288regd_copy = kzalloc(regd_len, GFP_ATOMIC);289if (regd_copy)290ath11k_copy_regd(regd, regd_copy);291292spin_unlock_bh(&ab->base_lock);293294if (!regd_copy) {295ret = -ENOMEM;296goto err;297}298299ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);300301kfree(regd_copy);302303if (ret)304goto err;305306return 0;307err:308ath11k_warn(ab, "failed to perform regd update : %d\n", ret);309return ret;310}311312static enum nl80211_dfs_regions313ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)314{315switch (dfs_region) {316case ATH11K_DFS_REG_FCC:317case ATH11K_DFS_REG_CN:318return NL80211_DFS_FCC;319case ATH11K_DFS_REG_ETSI:320case ATH11K_DFS_REG_KR:321return NL80211_DFS_ETSI;322case ATH11K_DFS_REG_MKK:323case ATH11K_DFS_REG_MKK_N:324return NL80211_DFS_JP;325default:326return NL80211_DFS_UNSET;327}328}329330static u32 ath11k_map_fw_reg_flags(u16 reg_flags)331{332u32 flags = 0;333334if (reg_flags & REGULATORY_CHAN_NO_IR)335flags = NL80211_RRF_NO_IR;336337if (reg_flags & REGULATORY_CHAN_RADAR)338flags |= NL80211_RRF_DFS;339340if (reg_flags & REGULATORY_CHAN_NO_OFDM)341flags |= NL80211_RRF_NO_OFDM;342343if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)344flags |= NL80211_RRF_NO_OUTDOOR;345346if (reg_flags & REGULATORY_CHAN_NO_HT40)347flags |= NL80211_RRF_NO_HT40;348349if (reg_flags & REGULATORY_CHAN_NO_80MHZ)350flags |= NL80211_RRF_NO_80MHZ;351352if (reg_flags & REGULATORY_CHAN_NO_160MHZ)353flags |= NL80211_RRF_NO_160MHZ;354355return flags;356}357358static u32 ath11k_map_fw_phy_flags(u32 phy_flags)359{360u32 flags = 0;361362if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)363flags |= NL80211_RRF_NO_HE;364365return flags;366}367368static bool369ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,370struct ieee80211_reg_rule *rule2)371{372u32 start_freq1, end_freq1;373u32 start_freq2, end_freq2;374375start_freq1 = rule1->freq_range.start_freq_khz;376start_freq2 = rule2->freq_range.start_freq_khz;377378end_freq1 = rule1->freq_range.end_freq_khz;379end_freq2 = rule2->freq_range.end_freq_khz;380381if ((start_freq1 >= start_freq2 &&382start_freq1 < end_freq2) ||383(start_freq2 > start_freq1 &&384start_freq2 < end_freq1))385return true;386387/* TODO: Should we restrict intersection feasibility388* based on min bandwidth of the intersected region also,389* say the intersected rule should have a min bandwidth390* of 20MHz?391*/392393return false;394}395396static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,397struct ieee80211_reg_rule *rule2,398struct ieee80211_reg_rule *new_rule)399{400u32 start_freq1, end_freq1;401u32 start_freq2, end_freq2;402u32 freq_diff, max_bw;403404start_freq1 = rule1->freq_range.start_freq_khz;405start_freq2 = rule2->freq_range.start_freq_khz;406407end_freq1 = rule1->freq_range.end_freq_khz;408end_freq2 = rule2->freq_range.end_freq_khz;409410new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,411start_freq2);412new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);413414freq_diff = new_rule->freq_range.end_freq_khz -415new_rule->freq_range.start_freq_khz;416max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,417rule2->freq_range.max_bandwidth_khz);418new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);419420new_rule->power_rule.max_antenna_gain =421min_t(u32, rule1->power_rule.max_antenna_gain,422rule2->power_rule.max_antenna_gain);423424new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,425rule2->power_rule.max_eirp);426427/* Use the flags of both the rules */428new_rule->flags = rule1->flags | rule2->flags;429430if ((rule1->flags & NL80211_RRF_PSD) && (rule2->flags & NL80211_RRF_PSD))431new_rule->psd = min_t(s8, rule1->psd, rule2->psd);432else433new_rule->flags &= ~NL80211_RRF_PSD;434435/* To be safe, lts use the max cac timeout of both rules */436new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,437rule2->dfs_cac_ms);438}439440static struct ieee80211_regdomain *441ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,442struct ieee80211_regdomain *curr_regd)443{444u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;445struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;446struct ieee80211_regdomain *new_regd = NULL;447u8 i, j, k;448449num_old_regd_rules = default_regd->n_reg_rules;450num_curr_regd_rules = curr_regd->n_reg_rules;451num_new_regd_rules = 0;452453/* Find the number of intersecting rules to allocate new regd memory */454for (i = 0; i < num_old_regd_rules; i++) {455old_rule = default_regd->reg_rules + i;456for (j = 0; j < num_curr_regd_rules; j++) {457curr_rule = curr_regd->reg_rules + j;458459if (ath11k_reg_can_intersect(old_rule, curr_rule))460num_new_regd_rules++;461}462}463464if (!num_new_regd_rules)465return NULL;466467new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *468sizeof(struct ieee80211_reg_rule)),469GFP_ATOMIC);470471if (!new_regd)472return NULL;473474/* We set the new country and dfs region directly and only trim475* the freq, power, antenna gain by intersecting with the476* default regdomain. Also MAX of the dfs cac timeout is selected.477*/478new_regd->n_reg_rules = num_new_regd_rules;479memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));480new_regd->dfs_region = curr_regd->dfs_region;481new_rule = new_regd->reg_rules;482483for (i = 0, k = 0; i < num_old_regd_rules; i++) {484old_rule = default_regd->reg_rules + i;485for (j = 0; j < num_curr_regd_rules; j++) {486curr_rule = curr_regd->reg_rules + j;487488if (ath11k_reg_can_intersect(old_rule, curr_rule))489ath11k_reg_intersect_rules(old_rule, curr_rule,490(new_rule + k++));491}492}493return new_regd;494}495496static const char *497ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)498{499switch (dfs_region) {500case NL80211_DFS_FCC:501return "FCC";502case NL80211_DFS_ETSI:503return "ETSI";504case NL80211_DFS_JP:505return "JP";506default:507return "UNSET";508}509}510511static u16512ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)513{514u16 bw;515516if (end_freq <= start_freq)517return 0;518519bw = end_freq - start_freq;520bw = min_t(u16, bw, max_bw);521522if (bw >= 80 && bw < 160)523bw = 80;524else if (bw >= 40 && bw < 80)525bw = 40;526else if (bw >= 20 && bw < 40)527bw = 20;528else529bw = 0;530531return bw;532}533534static void535ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,536u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,537s8 psd, u32 reg_flags)538{539reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);540reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);541reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);542reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);543reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);544reg_rule->psd = psd;545reg_rule->flags = reg_flags;546}547548static void549ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,550struct ieee80211_regdomain *regd,551struct cur_reg_rule *reg_rule,552u8 *rule_idx, u32 flags, u16 max_bw)553{554u32 start_freq;555u32 end_freq;556u16 bw;557u8 i;558559i = *rule_idx;560561/* there might be situations when even the input rule must be dropped */562i--;563564/* frequencies below weather radar */565bw = ath11k_reg_adjust_bw(reg_rule->start_freq,566ETSI_WEATHER_RADAR_BAND_LOW, max_bw);567if (bw > 0) {568i++;569570ath11k_reg_update_rule(regd->reg_rules + i,571reg_rule->start_freq,572ETSI_WEATHER_RADAR_BAND_LOW, bw,573reg_rule->ant_gain, reg_rule->reg_power,574reg_rule->psd_eirp, flags);575576ath11k_dbg(ab, ATH11K_DBG_REG,577"\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",578i + 1, reg_rule->start_freq,579ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,580reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,581flags);582}583584/* weather radar frequencies */585start_freq = max_t(u32, reg_rule->start_freq,586ETSI_WEATHER_RADAR_BAND_LOW);587end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);588589bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);590if (bw > 0) {591i++;592593ath11k_reg_update_rule(regd->reg_rules + i, start_freq,594end_freq, bw, reg_rule->ant_gain,595reg_rule->reg_power, reg_rule->psd_eirp, flags);596597regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;598599ath11k_dbg(ab, ATH11K_DBG_REG,600"\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",601i + 1, start_freq, end_freq, bw,602reg_rule->ant_gain, reg_rule->reg_power,603regd->reg_rules[i].dfs_cac_ms, flags);604}605606/* frequencies above weather radar */607bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,608reg_rule->end_freq, max_bw);609if (bw > 0) {610i++;611612ath11k_reg_update_rule(regd->reg_rules + i,613ETSI_WEATHER_RADAR_BAND_HIGH,614reg_rule->end_freq, bw,615reg_rule->ant_gain, reg_rule->reg_power,616reg_rule->psd_eirp, flags);617618ath11k_dbg(ab, ATH11K_DBG_REG,619"\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",620i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,621reg_rule->end_freq, bw, reg_rule->ant_gain,622reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,623flags);624}625626*rule_idx = i;627}628629enum wmi_reg_6ghz_ap_type630ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)631{632switch (power_type) {633case IEEE80211_REG_LPI_AP:634return WMI_REG_INDOOR_AP;635case IEEE80211_REG_SP_AP:636return WMI_REG_STANDARD_POWER_AP;637case IEEE80211_REG_VLP_AP:638return WMI_REG_VERY_LOW_POWER_AP;639default:640return WMI_REG_MAX_AP_TYPE;641}642}643644struct ieee80211_regdomain *645ath11k_reg_build_regd(struct ath11k_base *ab,646struct cur_regulatory_info *reg_info, bool intersect,647enum wmi_vdev_type vdev_type,648enum ieee80211_ap_reg_power power_type)649{650struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;651struct cur_reg_rule *reg_rule, *reg_rule_6ghz;652u8 i = 0, j = 0, k = 0;653u8 num_rules;654u16 max_bw;655u32 flags, reg_6ghz_number, max_bw_6ghz;656char alpha2[3];657658num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;659660if (reg_info->is_ext_reg_event) {661if (vdev_type == WMI_VDEV_TYPE_STA) {662enum wmi_reg_6ghz_ap_type ap_type;663664ap_type = ath11k_reg_ap_pwr_convert(power_type);665666if (ap_type == WMI_REG_MAX_AP_TYPE)667ap_type = WMI_REG_INDOOR_AP;668669reg_6ghz_number = reg_info->num_6ghz_rules_client670[ap_type][WMI_REG_DEFAULT_CLIENT];671672if (reg_6ghz_number == 0) {673ap_type = WMI_REG_INDOOR_AP;674reg_6ghz_number = reg_info->num_6ghz_rules_client675[ap_type][WMI_REG_DEFAULT_CLIENT];676}677678reg_rule_6ghz = reg_info->reg_rules_6ghz_client_ptr679[ap_type][WMI_REG_DEFAULT_CLIENT];680max_bw_6ghz = reg_info->max_bw_6ghz_client681[ap_type][WMI_REG_DEFAULT_CLIENT];682} else {683reg_6ghz_number = reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];684reg_rule_6ghz =685reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP];686max_bw_6ghz = reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP];687}688689num_rules += reg_6ghz_number;690}691692if (!num_rules)693goto ret;694695/* Add max additional rules to accommodate weather radar band */696if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)697num_rules += 2;698699tmp_regd = kzalloc(sizeof(*tmp_regd) +700(num_rules * sizeof(struct ieee80211_reg_rule)),701GFP_ATOMIC);702if (!tmp_regd)703goto ret;704705memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);706memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);707alpha2[2] = '\0';708tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);709710ath11k_dbg(ab, ATH11K_DBG_REG,711"Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",712alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),713reg_info->dfs_region, num_rules);714/* Update reg_rules[] below. Firmware is expected to715* send these rules in order(2 GHz rules first and then 5 GHz)716*/717for (; i < num_rules; i++) {718if (reg_info->num_2ghz_reg_rules &&719(i < reg_info->num_2ghz_reg_rules)) {720reg_rule = reg_info->reg_rules_2ghz_ptr + i;721max_bw = min_t(u16, reg_rule->max_bw,722reg_info->max_bw_2ghz);723flags = 0;724} else if (reg_info->num_5ghz_reg_rules &&725(j < reg_info->num_5ghz_reg_rules)) {726reg_rule = reg_info->reg_rules_5ghz_ptr + j++;727max_bw = min_t(u16, reg_rule->max_bw,728reg_info->max_bw_5ghz);729730/* FW doesn't pass NL80211_RRF_AUTO_BW flag for731* BW Auto correction, we can enable this by default732* for all 5G rules here. The regulatory core performs733* BW correction if required and applies flags as734* per other BW rule flags we pass from here735*/736flags = NL80211_RRF_AUTO_BW;737} else if (reg_info->is_ext_reg_event && reg_6ghz_number &&738k < reg_6ghz_number) {739reg_rule = reg_rule_6ghz + k++;740max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);741flags = NL80211_RRF_AUTO_BW;742if (reg_rule->psd_flag)743flags |= NL80211_RRF_PSD;744} else {745break;746}747748flags |= ath11k_map_fw_reg_flags(reg_rule->flags);749flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);750751ath11k_reg_update_rule(tmp_regd->reg_rules + i,752reg_rule->start_freq,753reg_rule->end_freq, max_bw,754reg_rule->ant_gain, reg_rule->reg_power,755reg_rule->psd_eirp, flags);756757/* Update dfs cac timeout if the dfs domain is ETSI and the758* new rule covers weather radar band.759* Default value of '0' corresponds to 60s timeout, so no760* need to update that for other rules.761*/762if (flags & NL80211_RRF_DFS &&763reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&764(reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&765reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){766ath11k_reg_update_weather_radar_band(ab, tmp_regd,767reg_rule, &i,768flags, max_bw);769continue;770}771772if (reg_info->is_ext_reg_event) {773ath11k_dbg(ab, ATH11K_DBG_REG,774"\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",775i + 1, reg_rule->start_freq, reg_rule->end_freq,776max_bw, reg_rule->ant_gain, reg_rule->reg_power,777tmp_regd->reg_rules[i].dfs_cac_ms, flags,778reg_rule->psd_flag, reg_rule->psd_eirp);779} else {780ath11k_dbg(ab, ATH11K_DBG_REG,781"\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",782i + 1, reg_rule->start_freq, reg_rule->end_freq,783max_bw, reg_rule->ant_gain, reg_rule->reg_power,784tmp_regd->reg_rules[i].dfs_cac_ms,785flags);786}787}788789tmp_regd->n_reg_rules = i;790791if (intersect) {792default_regd = ab->default_regd[reg_info->phy_id];793794/* Get a new regd by intersecting the received regd with795* our default regd.796*/797new_regd = ath11k_regd_intersect(default_regd, tmp_regd);798kfree(tmp_regd);799if (!new_regd) {800ath11k_warn(ab, "Unable to create intersected regdomain\n");801goto ret;802}803} else {804new_regd = tmp_regd;805}806807ret:808return new_regd;809}810811void ath11k_regd_update_chan_list_work(struct work_struct *work)812{813struct ath11k *ar = container_of(work, struct ath11k,814channel_update_work);815struct scan_chan_list_params *params;816struct list_head local_update_list;817int left;818819INIT_LIST_HEAD(&local_update_list);820821spin_lock_bh(&ar->data_lock);822list_splice_tail_init(&ar->channel_update_queue, &local_update_list);823spin_unlock_bh(&ar->data_lock);824825while ((params = list_first_entry_or_null(&local_update_list,826struct scan_chan_list_params,827list))) {828if (ar->state_11d != ATH11K_11D_IDLE) {829left = wait_for_completion_timeout(&ar->completed_11d_scan,830ATH11K_SCAN_TIMEOUT_HZ);831if (!left) {832ath11k_dbg(ar->ab, ATH11K_DBG_REG,833"failed to receive 11d scan complete: timed out\n");834ar->state_11d = ATH11K_11D_IDLE;835}836837ath11k_dbg(ar->ab, ATH11K_DBG_REG,838"reg 11d scan wait left time %d\n", left);839}840841if ((ar->scan.state == ATH11K_SCAN_STARTING ||842ar->scan.state == ATH11K_SCAN_RUNNING)) {843left = wait_for_completion_timeout(&ar->scan.completed,844ATH11K_SCAN_TIMEOUT_HZ);845if (!left)846ath11k_dbg(ar->ab, ATH11K_DBG_REG,847"failed to receive hw scan complete: timed out\n");848849ath11k_dbg(ar->ab, ATH11K_DBG_REG,850"reg hw scan wait left time %d\n", left);851}852853ath11k_wmi_send_scan_chan_list_cmd(ar, params);854list_del(¶ms->list);855kfree(params);856}857}858859static bool ath11k_reg_is_world_alpha(char *alpha)860{861if (alpha[0] == '0' && alpha[1] == '0')862return true;863864if (alpha[0] == 'n' && alpha[1] == 'a')865return true;866867return false;868}869870static enum wmi_vdev_type ath11k_reg_get_ar_vdev_type(struct ath11k *ar)871{872struct ath11k_vif *arvif;873874/* Currently each struct ath11k maps to one struct ieee80211_hw/wiphy875* and one struct ieee80211_regdomain, so it could only store one group876* reg rules. It means multi-interface concurrency in the same ath11k is877* not support for the regdomain. So get the vdev type of the first entry878* now. After concurrency support for the regdomain, this should change.879*/880arvif = list_first_entry_or_null(&ar->arvifs, struct ath11k_vif, list);881if (arvif)882return arvif->vdev_type;883884return WMI_VDEV_TYPE_UNSPEC;885}886887int ath11k_reg_handle_chan_list(struct ath11k_base *ab,888struct cur_regulatory_info *reg_info,889enum ieee80211_ap_reg_power power_type)890{891struct ieee80211_regdomain *regd;892bool intersect = false;893int pdev_idx;894struct ath11k *ar;895enum wmi_vdev_type vdev_type;896897ath11k_dbg(ab, ATH11K_DBG_WMI, "event reg handle chan list");898899if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {900/* In case of failure to set the requested ctry,901* fw retains the current regd. We print a failure info902* and return from here.903*/904ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");905return -EINVAL;906}907908pdev_idx = reg_info->phy_id;909910/* Avoid default reg rule updates sent during FW recovery if911* it is already available912*/913spin_lock_bh(&ab->base_lock);914if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&915ab->default_regd[pdev_idx]) {916spin_unlock_bh(&ab->base_lock);917goto retfail;918}919spin_unlock_bh(&ab->base_lock);920921if (pdev_idx >= ab->num_radios) {922/* Process the event for phy0 only if single_pdev_only923* is true. If pdev_idx is valid but not 0, discard the924* event. Otherwise, it goes to fallback. In either case925* ath11k_reg_reset_info() needs to be called to avoid926* memory leak issue.927*/928ath11k_reg_reset_info(reg_info);929930if (ab->hw_params.single_pdev_only &&931pdev_idx < ab->hw_params.num_rxdma_per_pdev)932return 0;933goto fallback;934}935936/* Avoid multiple overwrites to default regd, during core937* stop-start after mac registration.938*/939if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&940!memcmp((char *)ab->default_regd[pdev_idx]->alpha2,941(char *)reg_info->alpha2, 2))942goto retfail;943944/* Intersect new rules with default regd if a new country setting was945* requested, i.e a default regd was already set during initialization946* and the regd coming from this event has a valid country info.947*/948if (ab->default_regd[pdev_idx] &&949!ath11k_reg_is_world_alpha((char *)950ab->default_regd[pdev_idx]->alpha2) &&951!ath11k_reg_is_world_alpha((char *)reg_info->alpha2))952intersect = true;953954ar = ab->pdevs[pdev_idx].ar;955vdev_type = ath11k_reg_get_ar_vdev_type(ar);956957ath11k_dbg(ab, ATH11K_DBG_WMI,958"wmi handle chan list power type %d vdev type %d intersect %d\n",959power_type, vdev_type, intersect);960961regd = ath11k_reg_build_regd(ab, reg_info, intersect, vdev_type, power_type);962if (!regd) {963ath11k_warn(ab, "failed to build regd from reg_info\n");964goto fallback;965}966967if (power_type == IEEE80211_REG_UNSET_AP) {968ath11k_reg_reset_info(&ab->reg_info_store[pdev_idx]);969ab->reg_info_store[pdev_idx] = *reg_info;970}971972spin_lock_bh(&ab->base_lock);973if (ab->default_regd[pdev_idx]) {974/* The initial rules from FW after WMI Init is to build975* the default regd. From then on, any rules updated for976* the pdev could be due to user reg changes.977* Free previously built regd before assigning the newly978* generated regd to ar. NULL pointer handling will be979* taken care by kfree itself.980*/981ar = ab->pdevs[pdev_idx].ar;982kfree(ab->new_regd[pdev_idx]);983ab->new_regd[pdev_idx] = regd;984queue_work(ab->workqueue, &ar->regd_update_work);985} else {986/* This regd would be applied during mac registration and is987* held constant throughout for regd intersection purpose988*/989ab->default_regd[pdev_idx] = regd;990}991ab->dfs_region = reg_info->dfs_region;992spin_unlock_bh(&ab->base_lock);993994return 0;995996fallback:997/* Fallback to older reg (by sending previous country setting998* again if fw has succeeded and we failed to process here.999* The Regdomain should be uniform across driver and fw. Since the1000* FW has processed the command and sent a success status, we expect1001* this function to succeed as well. If it doesn't, CTRY needs to be1002* reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.1003*/1004/* TODO: This is rare, but still should also be handled */1005WARN_ON(1);10061007retfail:10081009return -EINVAL;1010}10111012void ath11k_regd_update_work(struct work_struct *work)1013{1014struct ath11k *ar = container_of(work, struct ath11k,1015regd_update_work);1016int ret;10171018ret = ath11k_regd_update(ar);1019if (ret) {1020/* Firmware has already moved to the new regd. We need1021* to maintain channel consistency across FW, Host driver1022* and userspace. Hence as a fallback mechanism we can set1023* the prev or default country code to the firmware.1024*/1025/* TODO: Implement Fallback Mechanism */1026}1027}10281029void ath11k_reg_init(struct ath11k *ar)1030{1031ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;1032ar->hw->wiphy->flags |= WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER;1033ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;1034}10351036void ath11k_reg_reset_info(struct cur_regulatory_info *reg_info)1037{1038int i, j;10391040if (!reg_info)1041return;10421043kfree(reg_info->reg_rules_2ghz_ptr);1044kfree(reg_info->reg_rules_5ghz_ptr);10451046for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {1047kfree(reg_info->reg_rules_6ghz_ap_ptr[i]);10481049for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)1050kfree(reg_info->reg_rules_6ghz_client_ptr[i][j]);1051}10521053memset(reg_info, 0, sizeof(*reg_info));1054}10551056void ath11k_reg_free(struct ath11k_base *ab)1057{1058int i;10591060for (i = 0; i < ab->num_radios; i++)1061ath11k_reg_reset_info(&ab->reg_info_store[i]);10621063kfree(ab->reg_info_store);1064ab->reg_info_store = NULL;10651066for (i = 0; i < ab->hw_params.max_radios; i++) {1067kfree(ab->default_regd[i]);1068kfree(ab->new_regd[i]);1069}1070}10711072int ath11k_reg_set_cc(struct ath11k *ar)1073{1074struct wmi_set_current_country_params set_current_param = {};10751076memcpy(&set_current_param.alpha2, ar->alpha2, 2);1077return ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);1078}107910801081