1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
8 /* World regdom to be used in case default regd from fw is unavailable */
9 #define ATH11K_2GHZ_CH01_11 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
10 #define ATH11K_5GHZ_5150_5350 REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
12 #define ATH11K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
15 #define ETSI_WEATHER_RADAR_BAND_LOW 5590
16 #define ETSI_WEATHER_RADAR_BAND_HIGH 5650
17 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT 600000
19 static const struct ieee80211_regdomain ath11k_world_regd
= {
24 ATH11K_5GHZ_5150_5350
,
25 ATH11K_5GHZ_5725_5850
,
29 static bool ath11k_regdom_changes(struct ath11k
*ar
, char *alpha2
)
31 const struct ieee80211_regdomain
*regd
;
33 regd
= rcu_dereference_rtnl(ar
->hw
->wiphy
->regd
);
34 /* This can happen during wiphy registration where the previous
35 * user request is received before we update the regd received
41 return memcmp(regd
->alpha2
, alpha2
, 2) != 0;
45 ath11k_reg_notifier(struct wiphy
*wiphy
, struct regulatory_request
*request
)
47 struct ieee80211_hw
*hw
= wiphy_to_ieee80211_hw(wiphy
);
48 struct wmi_init_country_params init_country_param
;
49 struct ath11k
*ar
= hw
->priv
;
52 ath11k_dbg(ar
->ab
, ATH11K_DBG_REG
,
53 "Regulatory Notification received for %s\n", wiphy_name(wiphy
));
55 /* Currently supporting only General User Hints. Cell base user
56 * hints to be handled later.
57 * Hints from other sources like Core, Beacons are not expected for
58 * self managed wiphy's
60 if (!(request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
61 request
->user_reg_hint_type
== NL80211_USER_REG_HINT_USER
)) {
62 ath11k_warn(ar
->ab
, "Unexpected Regulatory event for this wiphy\n");
66 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS
)) {
67 ath11k_dbg(ar
->ab
, ATH11K_DBG_REG
,
68 "Country Setting is not allowed\n");
72 if (!ath11k_regdom_changes(ar
, request
->alpha2
)) {
73 ath11k_dbg(ar
->ab
, ATH11K_DBG_REG
, "Country is already set\n");
77 /* Set the country code to the firmware and wait for
78 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
81 init_country_param
.flags
= ALPHA_IS_SET
;
82 memcpy(&init_country_param
.cc_info
.alpha2
, request
->alpha2
, 2);
84 ret
= ath11k_wmi_send_init_country_cmd(ar
, init_country_param
);
87 "INIT Country code set to fw failed : %d\n", ret
);
90 int ath11k_reg_update_chan_list(struct ath11k
*ar
)
92 struct ieee80211_supported_band
**bands
;
93 struct scan_chan_list_params
*params
;
94 struct ieee80211_channel
*channel
;
95 struct ieee80211_hw
*hw
= ar
->hw
;
96 struct channel_param
*ch
;
97 enum nl80211_band band
;
102 bands
= hw
->wiphy
->bands
;
103 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
107 for (i
= 0; i
< bands
[band
]->n_channels
; i
++) {
108 if (bands
[band
]->channels
[i
].flags
&
109 IEEE80211_CHAN_DISABLED
)
116 if (WARN_ON(!num_channels
))
119 params_len
= sizeof(struct scan_chan_list_params
) +
120 num_channels
* sizeof(struct channel_param
);
121 params
= kzalloc(params_len
, GFP_KERNEL
);
126 params
->pdev_id
= ar
->pdev
->pdev_id
;
127 params
->nallchans
= num_channels
;
129 ch
= params
->ch_param
;
131 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
135 for (i
= 0; i
< bands
[band
]->n_channels
; i
++) {
136 channel
= &bands
[band
]->channels
[i
];
138 if (channel
->flags
& IEEE80211_CHAN_DISABLED
)
141 /* TODO: Set to true/false based on some condition? */
143 ch
->allow_vht
= true;
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
;
154 ch
->maxpower
= channel
->max_power
* 2;
155 ch
->maxregpower
= channel
->max_reg_power
* 2;
156 ch
->antennamax
= channel
->max_antenna_gain
* 2;
158 /* TODO: Use appropriate phymodes */
159 if (channel
->band
== NL80211_BAND_2GHZ
)
160 ch
->phy_mode
= MODE_11G
;
162 ch
->phy_mode
= MODE_11A
;
164 ath11k_dbg(ar
->ab
, ATH11K_DBG_WMI
,
165 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
166 i
, params
->nallchans
,
167 ch
->mhz
, ch
->maxpower
, ch
->maxregpower
,
168 ch
->antennamax
, ch
->phy_mode
);
171 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
172 * set_agile, reg_class_idx
177 ret
= ath11k_wmi_send_scan_chan_list_cmd(ar
, params
);
183 static void ath11k_copy_regd(struct ieee80211_regdomain
*regd_orig
,
184 struct ieee80211_regdomain
*regd_copy
)
188 /* The caller should have checked error conditions */
189 memcpy(regd_copy
, regd_orig
, sizeof(*regd_orig
));
191 for (i
= 0; i
< regd_orig
->n_reg_rules
; i
++)
192 memcpy(®d_copy
->reg_rules
[i
], ®d_orig
->reg_rules
[i
],
193 sizeof(struct ieee80211_reg_rule
));
196 int ath11k_regd_update(struct ath11k
*ar
, bool init
)
198 struct ieee80211_regdomain
*regd
, *regd_copy
= NULL
;
199 int ret
, regd_len
, pdev_id
;
200 struct ath11k_base
*ab
;
203 pdev_id
= ar
->pdev_idx
;
205 spin_lock(&ab
->base_lock
);
208 /* Apply the regd received during init through
209 * WMI_REG_CHAN_LIST_CC event. In case of failure to
210 * receive the regd, initialize with a default world
213 if (ab
->default_regd
[pdev_id
]) {
214 regd
= ab
->default_regd
[pdev_id
];
217 "failed to receive default regd during init\n");
218 regd
= (struct ieee80211_regdomain
*)&ath11k_world_regd
;
221 regd
= ab
->new_regd
[pdev_id
];
226 spin_unlock(&ab
->base_lock
);
230 regd_len
= sizeof(*regd
) + (regd
->n_reg_rules
*
231 sizeof(struct ieee80211_reg_rule
));
233 regd_copy
= kzalloc(regd_len
, GFP_ATOMIC
);
235 ath11k_copy_regd(regd
, regd_copy
);
237 spin_unlock(&ab
->base_lock
);
245 ret
= regulatory_set_wiphy_regd_sync_rtnl(ar
->hw
->wiphy
, regd_copy
);
253 if (ar
->state
== ATH11K_STATE_ON
) {
254 ret
= ath11k_reg_update_chan_list(ar
);
261 ath11k_warn(ab
, "failed to perform regd update : %d\n", ret
);
265 static enum nl80211_dfs_regions
266 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region
)
268 switch (dfs_region
) {
269 case ATH11K_DFS_REG_FCC
:
270 case ATH11K_DFS_REG_CN
:
271 return NL80211_DFS_FCC
;
272 case ATH11K_DFS_REG_ETSI
:
273 case ATH11K_DFS_REG_KR
:
274 return NL80211_DFS_ETSI
;
275 case ATH11K_DFS_REG_MKK
:
276 return NL80211_DFS_JP
;
278 return NL80211_DFS_UNSET
;
282 static u32
ath11k_map_fw_reg_flags(u16 reg_flags
)
286 if (reg_flags
& REGULATORY_CHAN_NO_IR
)
287 flags
= NL80211_RRF_NO_IR
;
289 if (reg_flags
& REGULATORY_CHAN_RADAR
)
290 flags
|= NL80211_RRF_DFS
;
292 if (reg_flags
& REGULATORY_CHAN_NO_OFDM
)
293 flags
|= NL80211_RRF_NO_OFDM
;
295 if (reg_flags
& REGULATORY_CHAN_INDOOR_ONLY
)
296 flags
|= NL80211_RRF_NO_OUTDOOR
;
298 if (reg_flags
& REGULATORY_CHAN_NO_HT40
)
299 flags
|= NL80211_RRF_NO_HT40
;
301 if (reg_flags
& REGULATORY_CHAN_NO_80MHZ
)
302 flags
|= NL80211_RRF_NO_80MHZ
;
304 if (reg_flags
& REGULATORY_CHAN_NO_160MHZ
)
305 flags
|= NL80211_RRF_NO_160MHZ
;
311 ath11k_reg_can_intersect(struct ieee80211_reg_rule
*rule1
,
312 struct ieee80211_reg_rule
*rule2
)
314 u32 start_freq1
, end_freq1
;
315 u32 start_freq2
, end_freq2
;
317 start_freq1
= rule1
->freq_range
.start_freq_khz
;
318 start_freq2
= rule2
->freq_range
.start_freq_khz
;
320 end_freq1
= rule1
->freq_range
.end_freq_khz
;
321 end_freq2
= rule2
->freq_range
.end_freq_khz
;
323 if ((start_freq1
>= start_freq2
&&
324 start_freq1
< end_freq2
) ||
325 (start_freq2
> start_freq1
&&
326 start_freq2
< end_freq1
))
329 /* TODO: Should we restrict intersection feasibility
330 * based on min bandwidth of the intersected region also,
331 * say the intersected rule should have a min bandwidth
338 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule
*rule1
,
339 struct ieee80211_reg_rule
*rule2
,
340 struct ieee80211_reg_rule
*new_rule
)
342 u32 start_freq1
, end_freq1
;
343 u32 start_freq2
, end_freq2
;
344 u32 freq_diff
, max_bw
;
346 start_freq1
= rule1
->freq_range
.start_freq_khz
;
347 start_freq2
= rule2
->freq_range
.start_freq_khz
;
349 end_freq1
= rule1
->freq_range
.end_freq_khz
;
350 end_freq2
= rule2
->freq_range
.end_freq_khz
;
352 new_rule
->freq_range
.start_freq_khz
= max_t(u32
, start_freq1
,
354 new_rule
->freq_range
.end_freq_khz
= min_t(u32
, end_freq1
, end_freq2
);
356 freq_diff
= new_rule
->freq_range
.end_freq_khz
-
357 new_rule
->freq_range
.start_freq_khz
;
358 max_bw
= min_t(u32
, rule1
->freq_range
.max_bandwidth_khz
,
359 rule2
->freq_range
.max_bandwidth_khz
);
360 new_rule
->freq_range
.max_bandwidth_khz
= min_t(u32
, max_bw
, freq_diff
);
362 new_rule
->power_rule
.max_antenna_gain
=
363 min_t(u32
, rule1
->power_rule
.max_antenna_gain
,
364 rule2
->power_rule
.max_antenna_gain
);
366 new_rule
->power_rule
.max_eirp
= min_t(u32
, rule1
->power_rule
.max_eirp
,
367 rule2
->power_rule
.max_eirp
);
369 /* Use the flags of both the rules */
370 new_rule
->flags
= rule1
->flags
| rule2
->flags
;
372 /* To be safe, lts use the max cac timeout of both rules */
373 new_rule
->dfs_cac_ms
= max_t(u32
, rule1
->dfs_cac_ms
,
377 static struct ieee80211_regdomain
*
378 ath11k_regd_intersect(struct ieee80211_regdomain
*default_regd
,
379 struct ieee80211_regdomain
*curr_regd
)
381 u8 num_old_regd_rules
, num_curr_regd_rules
, num_new_regd_rules
;
382 struct ieee80211_reg_rule
*old_rule
, *curr_rule
, *new_rule
;
383 struct ieee80211_regdomain
*new_regd
= NULL
;
386 num_old_regd_rules
= default_regd
->n_reg_rules
;
387 num_curr_regd_rules
= curr_regd
->n_reg_rules
;
388 num_new_regd_rules
= 0;
390 /* Find the number of intersecting rules to allocate new regd memory */
391 for (i
= 0; i
< num_old_regd_rules
; i
++) {
392 old_rule
= default_regd
->reg_rules
+ i
;
393 for (j
= 0; j
< num_curr_regd_rules
; j
++) {
394 curr_rule
= curr_regd
->reg_rules
+ j
;
396 if (ath11k_reg_can_intersect(old_rule
, curr_rule
))
397 num_new_regd_rules
++;
401 if (!num_new_regd_rules
)
404 new_regd
= kzalloc(sizeof(*new_regd
) + (num_new_regd_rules
*
405 sizeof(struct ieee80211_reg_rule
)),
411 /* We set the new country and dfs region directly and only trim
412 * the freq, power, antenna gain by intersecting with the
413 * default regdomain. Also MAX of the dfs cac timeout is selected.
415 new_regd
->n_reg_rules
= num_new_regd_rules
;
416 memcpy(new_regd
->alpha2
, curr_regd
->alpha2
, sizeof(new_regd
->alpha2
));
417 new_regd
->dfs_region
= curr_regd
->dfs_region
;
418 new_rule
= new_regd
->reg_rules
;
420 for (i
= 0, k
= 0; i
< num_old_regd_rules
; i
++) {
421 old_rule
= default_regd
->reg_rules
+ i
;
422 for (j
= 0; j
< num_curr_regd_rules
; j
++) {
423 curr_rule
= curr_regd
->reg_rules
+ j
;
425 if (ath11k_reg_can_intersect(old_rule
, curr_rule
))
426 ath11k_reg_intersect_rules(old_rule
, curr_rule
,
434 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region
)
436 switch (dfs_region
) {
437 case NL80211_DFS_FCC
:
439 case NL80211_DFS_ETSI
:
449 ath11k_reg_adjust_bw(u16 start_freq
, u16 end_freq
, u16 max_bw
)
453 bw
= end_freq
- start_freq
;
454 bw
= min_t(u16
, bw
, max_bw
);
456 if (bw
>= 80 && bw
< 160)
458 else if (bw
>= 40 && bw
< 80)
467 ath11k_reg_update_rule(struct ieee80211_reg_rule
*reg_rule
, u32 start_freq
,
468 u32 end_freq
, u32 bw
, u32 ant_gain
, u32 reg_pwr
,
471 reg_rule
->freq_range
.start_freq_khz
= MHZ_TO_KHZ(start_freq
);
472 reg_rule
->freq_range
.end_freq_khz
= MHZ_TO_KHZ(end_freq
);
473 reg_rule
->freq_range
.max_bandwidth_khz
= MHZ_TO_KHZ(bw
);
474 reg_rule
->power_rule
.max_antenna_gain
= DBI_TO_MBI(ant_gain
);
475 reg_rule
->power_rule
.max_eirp
= DBM_TO_MBM(reg_pwr
);
476 reg_rule
->flags
= reg_flags
;
480 ath11k_reg_update_weather_radar_band(struct ath11k_base
*ab
,
481 struct ieee80211_regdomain
*regd
,
482 struct cur_reg_rule
*reg_rule
,
483 u8
*rule_idx
, u32 flags
, u16 max_bw
)
491 bw
= ath11k_reg_adjust_bw(reg_rule
->start_freq
,
492 ETSI_WEATHER_RADAR_BAND_LOW
, max_bw
);
494 ath11k_reg_update_rule(regd
->reg_rules
+ i
, reg_rule
->start_freq
,
495 ETSI_WEATHER_RADAR_BAND_LOW
, bw
,
496 reg_rule
->ant_gain
, reg_rule
->reg_power
,
499 ath11k_dbg(ab
, ATH11K_DBG_REG
,
500 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
501 i
+ 1, reg_rule
->start_freq
, ETSI_WEATHER_RADAR_BAND_LOW
,
502 bw
, reg_rule
->ant_gain
, reg_rule
->reg_power
,
503 regd
->reg_rules
[i
].dfs_cac_ms
,
506 if (reg_rule
->end_freq
> ETSI_WEATHER_RADAR_BAND_HIGH
)
507 end_freq
= ETSI_WEATHER_RADAR_BAND_HIGH
;
509 end_freq
= reg_rule
->end_freq
;
511 bw
= ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW
, end_freq
,
516 ath11k_reg_update_rule(regd
->reg_rules
+ i
,
517 ETSI_WEATHER_RADAR_BAND_LOW
, end_freq
, bw
,
518 reg_rule
->ant_gain
, reg_rule
->reg_power
,
521 regd
->reg_rules
[i
].dfs_cac_ms
= ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT
;
523 ath11k_dbg(ab
, ATH11K_DBG_REG
,
524 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
525 i
+ 1, ETSI_WEATHER_RADAR_BAND_LOW
, end_freq
,
526 bw
, reg_rule
->ant_gain
, reg_rule
->reg_power
,
527 regd
->reg_rules
[i
].dfs_cac_ms
,
530 if (end_freq
== reg_rule
->end_freq
) {
536 bw
= ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH
,
537 reg_rule
->end_freq
, max_bw
);
541 ath11k_reg_update_rule(regd
->reg_rules
+ i
, ETSI_WEATHER_RADAR_BAND_HIGH
,
542 reg_rule
->end_freq
, bw
,
543 reg_rule
->ant_gain
, reg_rule
->reg_power
,
546 ath11k_dbg(ab
, ATH11K_DBG_REG
,
547 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
548 i
+ 1, ETSI_WEATHER_RADAR_BAND_HIGH
, reg_rule
->end_freq
,
549 bw
, reg_rule
->ant_gain
, reg_rule
->reg_power
,
550 regd
->reg_rules
[i
].dfs_cac_ms
,
556 struct ieee80211_regdomain
*
557 ath11k_reg_build_regd(struct ath11k_base
*ab
,
558 struct cur_regulatory_info
*reg_info
, bool intersect
)
560 struct ieee80211_regdomain
*tmp_regd
, *default_regd
, *new_regd
= NULL
;
561 struct cur_reg_rule
*reg_rule
;
568 num_rules
= reg_info
->num_5g_reg_rules
+ reg_info
->num_2g_reg_rules
;
573 /* Add max additional rules to accommodate weather radar band */
574 if (reg_info
->dfs_region
== ATH11K_DFS_REG_ETSI
)
577 tmp_regd
= kzalloc(sizeof(*tmp_regd
) +
578 (num_rules
* sizeof(struct ieee80211_reg_rule
)),
583 tmp_regd
->n_reg_rules
= num_rules
;
584 memcpy(tmp_regd
->alpha2
, reg_info
->alpha2
, REG_ALPHA2_LEN
+ 1);
585 memcpy(alpha2
, reg_info
->alpha2
, REG_ALPHA2_LEN
+ 1);
587 tmp_regd
->dfs_region
= ath11k_map_fw_dfs_region(reg_info
->dfs_region
);
589 ath11k_dbg(ab
, ATH11K_DBG_REG
,
590 "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
591 alpha2
, ath11k_reg_get_regdom_str(tmp_regd
->dfs_region
),
592 reg_info
->dfs_region
, num_rules
);
593 /* Update reg_rules[] below. Firmware is expected to
594 * send these rules in order(2G rules first and then 5G)
596 for (; i
< tmp_regd
->n_reg_rules
; i
++) {
597 if (reg_info
->num_2g_reg_rules
&&
598 (i
< reg_info
->num_2g_reg_rules
)) {
599 reg_rule
= reg_info
->reg_rules_2g_ptr
+ i
;
600 max_bw
= min_t(u16
, reg_rule
->max_bw
,
601 reg_info
->max_bw_2g
);
603 } else if (reg_info
->num_5g_reg_rules
&&
604 (j
< reg_info
->num_5g_reg_rules
)) {
605 reg_rule
= reg_info
->reg_rules_5g_ptr
+ j
++;
606 max_bw
= min_t(u16
, reg_rule
->max_bw
,
607 reg_info
->max_bw_5g
);
609 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for
610 * BW Auto correction, we can enable this by default
611 * for all 5G rules here. The regulatory core performs
612 * BW correction if required and applies flags as
613 * per other BW rule flags we pass from here
615 flags
= NL80211_RRF_AUTO_BW
;
620 flags
|= ath11k_map_fw_reg_flags(reg_rule
->flags
);
622 ath11k_reg_update_rule(tmp_regd
->reg_rules
+ i
,
623 reg_rule
->start_freq
,
624 reg_rule
->end_freq
, max_bw
,
625 reg_rule
->ant_gain
, reg_rule
->reg_power
,
628 /* Update dfs cac timeout if the dfs domain is ETSI and the
629 * new rule covers weather radar band.
630 * Default value of '0' corresponds to 60s timeout, so no
631 * need to update that for other rules.
633 if (flags
& NL80211_RRF_DFS
&&
634 reg_info
->dfs_region
== ATH11K_DFS_REG_ETSI
&&
635 (reg_rule
->end_freq
> ETSI_WEATHER_RADAR_BAND_LOW
&&
636 reg_rule
->start_freq
< ETSI_WEATHER_RADAR_BAND_HIGH
)){
637 ath11k_reg_update_weather_radar_band(ab
, tmp_regd
,
643 ath11k_dbg(ab
, ATH11K_DBG_REG
,
644 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
645 i
+ 1, reg_rule
->start_freq
, reg_rule
->end_freq
,
646 max_bw
, reg_rule
->ant_gain
, reg_rule
->reg_power
,
647 tmp_regd
->reg_rules
[i
].dfs_cac_ms
,
652 default_regd
= ab
->default_regd
[reg_info
->phy_id
];
654 /* Get a new regd by intersecting the received regd with
657 new_regd
= ath11k_regd_intersect(default_regd
, tmp_regd
);
660 ath11k_warn(ab
, "Unable to create intersected regdomain\n");
671 void ath11k_regd_update_work(struct work_struct
*work
)
673 struct ath11k
*ar
= container_of(work
, struct ath11k
,
677 ret
= ath11k_regd_update(ar
, false);
679 /* Firmware has already moved to the new regd. We need
680 * to maintain channel consistency across FW, Host driver
681 * and userspace. Hence as a fallback mechanism we can set
682 * the prev or default country code to the firmware.
684 /* TODO: Implement Fallback Mechanism */
688 void ath11k_reg_init(struct ath11k
*ar
)
690 ar
->hw
->wiphy
->regulatory_flags
= REGULATORY_WIPHY_SELF_MANAGED
;
691 ar
->hw
->wiphy
->reg_notifier
= ath11k_reg_notifier
;
694 void ath11k_reg_free(struct ath11k_base
*ab
)
698 for (i
= 0; i
< MAX_RADIOS
; i
++) {
699 kfree(ab
->default_regd
[i
]);
700 kfree(ab
->new_regd
[i
]);