1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved.
5 */
6 #include <linux/rtnetlink.h>
7
8 #include "core.h"
9 #include "debug.h"
10
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,\
14 NL80211_RRF_NO_IR)
15 #define ATH11K_5GHZ_5725_5850 REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
16 NL80211_RRF_NO_IR)
17
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
21
22 static const struct ieee80211_regdomain ath11k_world_regd = {
23 .n_reg_rules = 3,
24 .alpha2 = "00",
25 .reg_rules = {
26 ATH11K_2GHZ_CH01_11,
27 ATH11K_5GHZ_5150_5350,
28 ATH11K_5GHZ_5725_5850,
29 }
30 };
31
ath11k_regdom_changes(struct ath11k * ar,char * alpha2)32 static bool ath11k_regdom_changes(struct ath11k *ar, char *alpha2)
33 {
34 const struct ieee80211_regdomain *regd;
35
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
39 * from firmware.
40 */
41 if (!regd)
42 return true;
43
44 return memcmp(regd->alpha2, alpha2, 2) != 0;
45 }
46
47 static void
ath11k_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)48 ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
49 {
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;
53 int ret;
54
55 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
56 "Regulatory Notification received for %s\n", wiphy_name(wiphy));
57
58 if (request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
59 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
60 "driver initiated regd update\n");
61 if (ar->state != ATH11K_STATE_ON)
62 return;
63
64 ret = ath11k_reg_update_chan_list(ar, true);
65 if (ret)
66 ath11k_warn(ar->ab, "failed to update channel list: %d\n", ret);
67
68 return;
69 }
70
71 /* Currently supporting only General User Hints. Cell base user
72 * hints to be handled later.
73 * Hints from other sources like Core, Beacons are not expected for
74 * self managed wiphy's
75 */
76 if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
77 request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
78 ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
79 return;
80 }
81
82 if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
83 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
84 "Country Setting is not allowed\n");
85 return;
86 }
87
88 if (!ath11k_regdom_changes(ar, request->alpha2)) {
89 ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
90 return;
91 }
92
93 /* Set the country code to the firmware and will receive
94 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
95 * reg info
96 */
97 if (ar->ab->hw_params.current_cc_support) {
98 memcpy(&ar->alpha2, request->alpha2, 2);
99 ret = ath11k_reg_set_cc(ar);
100 if (ret)
101 ath11k_warn(ar->ab,
102 "failed set current country code: %d\n", ret);
103 } else {
104 init_country_param.flags = ALPHA_IS_SET;
105 memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
106 init_country_param.cc_info.alpha2[2] = 0;
107
108 ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
109 if (ret)
110 ath11k_warn(ar->ab,
111 "INIT Country code set to fw failed : %d\n", ret);
112 }
113
114 ath11k_mac_11d_scan_stop(ar);
115 ar->regdom_set_by_user = true;
116 }
117
ath11k_reg_update_chan_list(struct ath11k * ar,bool wait)118 int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)
119 {
120 struct ieee80211_supported_band **bands;
121 struct scan_chan_list_params *params;
122 struct ieee80211_channel *channel;
123 struct ieee80211_hw *hw = ar->hw;
124 struct channel_param *ch;
125 enum nl80211_band band;
126 int num_channels = 0;
127 int i, ret = 0;
128
129 if (ar->state == ATH11K_STATE_RESTARTING)
130 return 0;
131
132 bands = hw->wiphy->bands;
133 for (band = 0; band < NUM_NL80211_BANDS; band++) {
134 if (!bands[band])
135 continue;
136
137 for (i = 0; i < bands[band]->n_channels; i++) {
138 if (bands[band]->channels[i].flags &
139 IEEE80211_CHAN_DISABLED)
140 continue;
141
142 num_channels++;
143 }
144 }
145
146 if (WARN_ON(!num_channels))
147 return -EINVAL;
148
149 params = kzalloc(struct_size(params, ch_param, num_channels),
150 GFP_KERNEL);
151 if (!params)
152 return -ENOMEM;
153
154 params->pdev_id = ar->pdev->pdev_id;
155 params->nallchans = num_channels;
156
157 ch = params->ch_param;
158
159 for (band = 0; band < NUM_NL80211_BANDS; band++) {
160 if (!bands[band])
161 continue;
162
163 for (i = 0; i < bands[band]->n_channels; i++) {
164 channel = &bands[band]->channels[i];
165
166 if (channel->flags & IEEE80211_CHAN_DISABLED)
167 continue;
168
169 /* TODO: Set to true/false based on some condition? */
170 ch->allow_ht = true;
171 ch->allow_vht = true;
172 ch->allow_he = true;
173
174 ch->dfs_set =
175 !!(channel->flags & IEEE80211_CHAN_RADAR);
176 ch->is_chan_passive = !!(channel->flags &
177 IEEE80211_CHAN_NO_IR);
178 ch->is_chan_passive |= ch->dfs_set;
179 ch->mhz = channel->center_freq;
180 ch->cfreq1 = channel->center_freq;
181 ch->minpower = 0;
182 ch->maxpower = channel->max_power * 2;
183 ch->maxregpower = channel->max_reg_power * 2;
184 ch->antennamax = channel->max_antenna_gain * 2;
185
186 /* TODO: Use appropriate phymodes */
187 if (channel->band == NL80211_BAND_2GHZ)
188 ch->phy_mode = MODE_11G;
189 else
190 ch->phy_mode = MODE_11A;
191
192 if (channel->band == NL80211_BAND_6GHZ &&
193 cfg80211_channel_is_psc(channel))
194 ch->psc_channel = true;
195
196 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
197 "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
198 i, params->nallchans,
199 ch->mhz, ch->maxpower, ch->maxregpower,
200 ch->antennamax, ch->phy_mode);
201
202 ch++;
203 /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
204 * set_agile, reg_class_idx
205 */
206 }
207 }
208
209 if (wait) {
210 spin_lock_bh(&ar->data_lock);
211 list_add_tail(¶ms->list, &ar->channel_update_queue);
212 spin_unlock_bh(&ar->data_lock);
213
214 queue_work(ar->ab->workqueue, &ar->channel_update_work);
215
216 return 0;
217 }
218
219 ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
220 kfree(params);
221
222 return ret;
223 }
224
225 #if defined(__linux__)
ath11k_copy_regd(struct ieee80211_regdomain * regd_orig,struct ieee80211_regdomain * regd_copy)226 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
227 #elif defined(__FreeBSD__)
228 static void ath11k_copy_regd(const struct ieee80211_regdomain *regd_orig,
229 #endif
230 struct ieee80211_regdomain *regd_copy)
231 {
232 u8 i;
233
234 /* The caller should have checked error conditions */
235 memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
236
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));
240 }
241
ath11k_regd_update(struct ath11k * ar)242 int ath11k_regd_update(struct ath11k *ar)
243 {
244 #if defined(__linux__)
245 struct ieee80211_regdomain *regd, *regd_copy = NULL;
246 #elif defined(__FreeBSD__)
247 const struct ieee80211_regdomain *regd;
248 struct ieee80211_regdomain *regd_copy = NULL;
249 #endif
250 int ret, regd_len, pdev_id;
251 struct ath11k_base *ab;
252
253 ab = ar->ab;
254 pdev_id = ar->pdev_idx;
255
256 spin_lock_bh(&ab->base_lock);
257
258 /* Prefer the latest regd update over default if it's available */
259 if (ab->new_regd[pdev_id]) {
260 regd = ab->new_regd[pdev_id];
261 } else {
262 /* Apply the regd received during init through
263 * WMI_REG_CHAN_LIST_CC event. In case of failure to
264 * receive the regd, initialize with a default world
265 * regulatory.
266 */
267 if (ab->default_regd[pdev_id]) {
268 regd = ab->default_regd[pdev_id];
269 } else {
270 ath11k_warn(ab,
271 "failed to receive default regd during init\n");
272 #if defined(__linux__)
273 regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
274 #elif defined(__FreeBSD__)
275 regd = &ath11k_world_regd;
276 #endif
277 }
278 }
279
280 if (!regd) {
281 ret = -EINVAL;
282 spin_unlock_bh(&ab->base_lock);
283 goto err;
284 }
285
286 regd_len = sizeof(*regd) + (regd->n_reg_rules *
287 sizeof(struct ieee80211_reg_rule));
288
289 regd_copy = kzalloc(regd_len, GFP_ATOMIC);
290 if (regd_copy)
291 ath11k_copy_regd(regd, regd_copy);
292
293 spin_unlock_bh(&ab->base_lock);
294
295 if (!regd_copy) {
296 ret = -ENOMEM;
297 goto err;
298 }
299
300 ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);
301
302 kfree(regd_copy);
303
304 if (ret)
305 goto err;
306
307 return 0;
308 err:
309 ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
310 return ret;
311 }
312
313 static enum nl80211_dfs_regions
ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)314 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
315 {
316 switch (dfs_region) {
317 case ATH11K_DFS_REG_FCC:
318 case ATH11K_DFS_REG_CN:
319 return NL80211_DFS_FCC;
320 case ATH11K_DFS_REG_ETSI:
321 case ATH11K_DFS_REG_KR:
322 return NL80211_DFS_ETSI;
323 case ATH11K_DFS_REG_MKK:
324 case ATH11K_DFS_REG_MKK_N:
325 return NL80211_DFS_JP;
326 default:
327 return NL80211_DFS_UNSET;
328 }
329 }
330
ath11k_map_fw_reg_flags(u16 reg_flags)331 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
332 {
333 u32 flags = 0;
334
335 if (reg_flags & REGULATORY_CHAN_NO_IR)
336 flags = NL80211_RRF_NO_IR;
337
338 if (reg_flags & REGULATORY_CHAN_RADAR)
339 flags |= NL80211_RRF_DFS;
340
341 if (reg_flags & REGULATORY_CHAN_NO_OFDM)
342 flags |= NL80211_RRF_NO_OFDM;
343
344 if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
345 flags |= NL80211_RRF_NO_OUTDOOR;
346
347 if (reg_flags & REGULATORY_CHAN_NO_HT40)
348 flags |= NL80211_RRF_NO_HT40;
349
350 if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
351 flags |= NL80211_RRF_NO_80MHZ;
352
353 if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
354 flags |= NL80211_RRF_NO_160MHZ;
355
356 return flags;
357 }
358
ath11k_map_fw_phy_flags(u32 phy_flags)359 static u32 ath11k_map_fw_phy_flags(u32 phy_flags)
360 {
361 u32 flags = 0;
362
363 if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)
364 flags |= NL80211_RRF_NO_HE;
365
366 return flags;
367 }
368
369 static bool
ath11k_reg_can_intersect(struct ieee80211_reg_rule * rule1,struct ieee80211_reg_rule * rule2)370 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
371 struct ieee80211_reg_rule *rule2)
372 {
373 u32 start_freq1, end_freq1;
374 u32 start_freq2, end_freq2;
375
376 start_freq1 = rule1->freq_range.start_freq_khz;
377 start_freq2 = rule2->freq_range.start_freq_khz;
378
379 end_freq1 = rule1->freq_range.end_freq_khz;
380 end_freq2 = rule2->freq_range.end_freq_khz;
381
382 if ((start_freq1 >= start_freq2 &&
383 start_freq1 < end_freq2) ||
384 (start_freq2 > start_freq1 &&
385 start_freq2 < end_freq1))
386 return true;
387
388 /* TODO: Should we restrict intersection feasibility
389 * based on min bandwidth of the intersected region also,
390 * say the intersected rule should have a min bandwidth
391 * of 20MHz?
392 */
393
394 return false;
395 }
396
ath11k_reg_intersect_rules(struct ieee80211_reg_rule * rule1,struct ieee80211_reg_rule * rule2,struct ieee80211_reg_rule * new_rule)397 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
398 struct ieee80211_reg_rule *rule2,
399 struct ieee80211_reg_rule *new_rule)
400 {
401 u32 start_freq1, end_freq1;
402 u32 start_freq2, end_freq2;
403 u32 freq_diff, max_bw;
404
405 start_freq1 = rule1->freq_range.start_freq_khz;
406 start_freq2 = rule2->freq_range.start_freq_khz;
407
408 end_freq1 = rule1->freq_range.end_freq_khz;
409 end_freq2 = rule2->freq_range.end_freq_khz;
410
411 new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
412 start_freq2);
413 new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
414
415 freq_diff = new_rule->freq_range.end_freq_khz -
416 new_rule->freq_range.start_freq_khz;
417 max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
418 rule2->freq_range.max_bandwidth_khz);
419 new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
420
421 new_rule->power_rule.max_antenna_gain =
422 min_t(u32, rule1->power_rule.max_antenna_gain,
423 rule2->power_rule.max_antenna_gain);
424
425 new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
426 rule2->power_rule.max_eirp);
427
428 /* Use the flags of both the rules */
429 new_rule->flags = rule1->flags | rule2->flags;
430
431 if ((rule1->flags & NL80211_RRF_PSD) && (rule2->flags & NL80211_RRF_PSD))
432 new_rule->psd = min_t(s8, rule1->psd, rule2->psd);
433 else
434 new_rule->flags &= ~NL80211_RRF_PSD;
435
436 /* To be safe, lts use the max cac timeout of both rules */
437 new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
438 rule2->dfs_cac_ms);
439 }
440
441 static struct ieee80211_regdomain *
ath11k_regd_intersect(struct ieee80211_regdomain * default_regd,struct ieee80211_regdomain * curr_regd)442 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
443 struct ieee80211_regdomain *curr_regd)
444 {
445 u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
446 struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
447 struct ieee80211_regdomain *new_regd = NULL;
448 u8 i, j, k;
449
450 num_old_regd_rules = default_regd->n_reg_rules;
451 num_curr_regd_rules = curr_regd->n_reg_rules;
452 num_new_regd_rules = 0;
453
454 /* Find the number of intersecting rules to allocate new regd memory */
455 for (i = 0; i < num_old_regd_rules; i++) {
456 old_rule = default_regd->reg_rules + i;
457 for (j = 0; j < num_curr_regd_rules; j++) {
458 curr_rule = curr_regd->reg_rules + j;
459
460 if (ath11k_reg_can_intersect(old_rule, curr_rule))
461 num_new_regd_rules++;
462 }
463 }
464
465 if (!num_new_regd_rules)
466 return NULL;
467
468 new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
469 sizeof(struct ieee80211_reg_rule)),
470 GFP_ATOMIC);
471
472 if (!new_regd)
473 return NULL;
474
475 /* We set the new country and dfs region directly and only trim
476 * the freq, power, antenna gain by intersecting with the
477 * default regdomain. Also MAX of the dfs cac timeout is selected.
478 */
479 new_regd->n_reg_rules = num_new_regd_rules;
480 memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
481 new_regd->dfs_region = curr_regd->dfs_region;
482 new_rule = new_regd->reg_rules;
483
484 for (i = 0, k = 0; i < num_old_regd_rules; i++) {
485 old_rule = default_regd->reg_rules + i;
486 for (j = 0; j < num_curr_regd_rules; j++) {
487 curr_rule = curr_regd->reg_rules + j;
488
489 if (ath11k_reg_can_intersect(old_rule, curr_rule))
490 ath11k_reg_intersect_rules(old_rule, curr_rule,
491 (new_rule + k++));
492 }
493 }
494 return new_regd;
495 }
496
497 static const char *
ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)498 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
499 {
500 switch (dfs_region) {
501 case NL80211_DFS_FCC:
502 return "FCC";
503 case NL80211_DFS_ETSI:
504 return "ETSI";
505 case NL80211_DFS_JP:
506 return "JP";
507 default:
508 return "UNSET";
509 }
510 }
511
512 static u16
ath11k_reg_adjust_bw(u16 start_freq,u16 end_freq,u16 max_bw)513 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
514 {
515 u16 bw;
516
517 if (end_freq <= start_freq)
518 return 0;
519
520 bw = end_freq - start_freq;
521 bw = min_t(u16, bw, max_bw);
522
523 if (bw >= 80 && bw < 160)
524 bw = 80;
525 else if (bw >= 40 && bw < 80)
526 bw = 40;
527 else if (bw >= 20 && bw < 40)
528 bw = 20;
529 else
530 bw = 0;
531
532 return bw;
533 }
534
535 static void
ath11k_reg_update_rule(struct ieee80211_reg_rule * reg_rule,u32 start_freq,u32 end_freq,u32 bw,u32 ant_gain,u32 reg_pwr,s8 psd,u32 reg_flags)536 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
537 u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
538 s8 psd, u32 reg_flags)
539 {
540 reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
541 reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
542 reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
543 reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
544 reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
545 reg_rule->psd = psd;
546 reg_rule->flags = reg_flags;
547 }
548
549 static void
ath11k_reg_update_weather_radar_band(struct ath11k_base * ab,struct ieee80211_regdomain * regd,struct cur_reg_rule * reg_rule,u8 * rule_idx,u32 flags,u16 max_bw)550 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
551 struct ieee80211_regdomain *regd,
552 struct cur_reg_rule *reg_rule,
553 u8 *rule_idx, u32 flags, u16 max_bw)
554 {
555 u32 start_freq;
556 u32 end_freq;
557 u16 bw;
558 u8 i;
559
560 i = *rule_idx;
561
562 /* there might be situations when even the input rule must be dropped */
563 i--;
564
565 /* frequencies below weather radar */
566 bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
567 ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
568 if (bw > 0) {
569 i++;
570
571 ath11k_reg_update_rule(regd->reg_rules + i,
572 reg_rule->start_freq,
573 ETSI_WEATHER_RADAR_BAND_LOW, bw,
574 reg_rule->ant_gain, reg_rule->reg_power,
575 reg_rule->psd_eirp, flags);
576
577 ath11k_dbg(ab, ATH11K_DBG_REG,
578 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
579 i + 1, reg_rule->start_freq,
580 ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
581 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
582 flags);
583 }
584
585 /* weather radar frequencies */
586 start_freq = max_t(u32, reg_rule->start_freq,
587 ETSI_WEATHER_RADAR_BAND_LOW);
588 end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
589
590 bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
591 if (bw > 0) {
592 i++;
593
594 ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
595 end_freq, bw, reg_rule->ant_gain,
596 reg_rule->reg_power, reg_rule->psd_eirp, flags);
597
598 regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
599
600 ath11k_dbg(ab, ATH11K_DBG_REG,
601 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
602 i + 1, start_freq, end_freq, bw,
603 reg_rule->ant_gain, reg_rule->reg_power,
604 regd->reg_rules[i].dfs_cac_ms, flags);
605 }
606
607 /* frequencies above weather radar */
608 bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
609 reg_rule->end_freq, max_bw);
610 if (bw > 0) {
611 i++;
612
613 ath11k_reg_update_rule(regd->reg_rules + i,
614 ETSI_WEATHER_RADAR_BAND_HIGH,
615 reg_rule->end_freq, bw,
616 reg_rule->ant_gain, reg_rule->reg_power,
617 reg_rule->psd_eirp, flags);
618
619 ath11k_dbg(ab, ATH11K_DBG_REG,
620 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
621 i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
622 reg_rule->end_freq, bw, reg_rule->ant_gain,
623 reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
624 flags);
625 }
626
627 *rule_idx = i;
628 }
629
630 enum wmi_reg_6ghz_ap_type
ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)631 ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
632 {
633 switch (power_type) {
634 case IEEE80211_REG_LPI_AP:
635 return WMI_REG_INDOOR_AP;
636 case IEEE80211_REG_SP_AP:
637 return WMI_REG_STANDARD_POWER_AP;
638 case IEEE80211_REG_VLP_AP:
639 return WMI_REG_VERY_LOW_POWER_AP;
640 default:
641 return WMI_REG_MAX_AP_TYPE;
642 }
643 }
644
645 struct ieee80211_regdomain *
ath11k_reg_build_regd(struct ath11k_base * ab,struct cur_regulatory_info * reg_info,bool intersect,enum wmi_vdev_type vdev_type,enum ieee80211_ap_reg_power power_type)646 ath11k_reg_build_regd(struct ath11k_base *ab,
647 struct cur_regulatory_info *reg_info, bool intersect,
648 enum wmi_vdev_type vdev_type,
649 enum ieee80211_ap_reg_power power_type)
650 {
651 struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
652 struct cur_reg_rule *reg_rule, *reg_rule_6ghz;
653 u8 i = 0, j = 0, k = 0;
654 u8 num_rules;
655 u16 max_bw;
656 u32 flags, reg_6ghz_number, max_bw_6ghz;
657 char alpha2[3];
658
659 num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
660
661 if (reg_info->is_ext_reg_event) {
662 if (vdev_type == WMI_VDEV_TYPE_STA) {
663 enum wmi_reg_6ghz_ap_type ap_type;
664
665 ap_type = ath11k_reg_ap_pwr_convert(power_type);
666
667 if (ap_type == WMI_REG_MAX_AP_TYPE)
668 ap_type = WMI_REG_INDOOR_AP;
669
670 reg_6ghz_number = reg_info->num_6ghz_rules_client
671 [ap_type][WMI_REG_DEFAULT_CLIENT];
672
673 if (reg_6ghz_number == 0) {
674 ap_type = WMI_REG_INDOOR_AP;
675 reg_6ghz_number = reg_info->num_6ghz_rules_client
676 [ap_type][WMI_REG_DEFAULT_CLIENT];
677 }
678
679 reg_rule_6ghz = reg_info->reg_rules_6ghz_client_ptr
680 [ap_type][WMI_REG_DEFAULT_CLIENT];
681 max_bw_6ghz = reg_info->max_bw_6ghz_client
682 [ap_type][WMI_REG_DEFAULT_CLIENT];
683 } else {
684 reg_6ghz_number = reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
685 reg_rule_6ghz =
686 reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP];
687 max_bw_6ghz = reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP];
688 }
689
690 num_rules += reg_6ghz_number;
691 }
692
693 if (!num_rules)
694 goto ret;
695
696 /* Add max additional rules to accommodate weather radar band */
697 if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
698 num_rules += 2;
699
700 tmp_regd = kzalloc(sizeof(*tmp_regd) +
701 (num_rules * sizeof(struct ieee80211_reg_rule)),
702 GFP_ATOMIC);
703 if (!tmp_regd)
704 goto ret;
705
706 memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
707 memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
708 alpha2[2] = '\0';
709 tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
710
711 ath11k_dbg(ab, ATH11K_DBG_REG,
712 "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
713 alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
714 reg_info->dfs_region, num_rules);
715 /* Update reg_rules[] below. Firmware is expected to
716 * send these rules in order(2 GHz rules first and then 5 GHz)
717 */
718 for (; i < num_rules; i++) {
719 if (reg_info->num_2ghz_reg_rules &&
720 (i < reg_info->num_2ghz_reg_rules)) {
721 reg_rule = reg_info->reg_rules_2ghz_ptr + i;
722 max_bw = min_t(u16, reg_rule->max_bw,
723 reg_info->max_bw_2ghz);
724 flags = 0;
725 } else if (reg_info->num_5ghz_reg_rules &&
726 (j < reg_info->num_5ghz_reg_rules)) {
727 reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
728 max_bw = min_t(u16, reg_rule->max_bw,
729 reg_info->max_bw_5ghz);
730
731 /* FW doesn't pass NL80211_RRF_AUTO_BW flag for
732 * BW Auto correction, we can enable this by default
733 * for all 5G rules here. The regulatory core performs
734 * BW correction if required and applies flags as
735 * per other BW rule flags we pass from here
736 */
737 flags = NL80211_RRF_AUTO_BW;
738 } else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
739 k < reg_6ghz_number) {
740 reg_rule = reg_rule_6ghz + k++;
741 max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
742 flags = NL80211_RRF_AUTO_BW;
743 if (reg_rule->psd_flag)
744 flags |= NL80211_RRF_PSD;
745 } else {
746 break;
747 }
748
749 flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
750 flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);
751
752 ath11k_reg_update_rule(tmp_regd->reg_rules + i,
753 reg_rule->start_freq,
754 reg_rule->end_freq, max_bw,
755 reg_rule->ant_gain, reg_rule->reg_power,
756 reg_rule->psd_eirp, flags);
757
758 /* Update dfs cac timeout if the dfs domain is ETSI and the
759 * new rule covers weather radar band.
760 * Default value of '0' corresponds to 60s timeout, so no
761 * need to update that for other rules.
762 */
763 if (flags & NL80211_RRF_DFS &&
764 reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
765 (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
766 reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
767 ath11k_reg_update_weather_radar_band(ab, tmp_regd,
768 reg_rule, &i,
769 flags, max_bw);
770 continue;
771 }
772
773 if (reg_info->is_ext_reg_event) {
774 ath11k_dbg(ab, ATH11K_DBG_REG,
775 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
776 i + 1, reg_rule->start_freq, reg_rule->end_freq,
777 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
778 tmp_regd->reg_rules[i].dfs_cac_ms, flags,
779 reg_rule->psd_flag, reg_rule->psd_eirp);
780 } else {
781 ath11k_dbg(ab, ATH11K_DBG_REG,
782 "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
783 i + 1, reg_rule->start_freq, reg_rule->end_freq,
784 max_bw, reg_rule->ant_gain, reg_rule->reg_power,
785 tmp_regd->reg_rules[i].dfs_cac_ms,
786 flags);
787 }
788 }
789
790 tmp_regd->n_reg_rules = i;
791
792 if (intersect) {
793 default_regd = ab->default_regd[reg_info->phy_id];
794
795 /* Get a new regd by intersecting the received regd with
796 * our default regd.
797 */
798 new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
799 kfree(tmp_regd);
800 if (!new_regd) {
801 ath11k_warn(ab, "Unable to create intersected regdomain\n");
802 goto ret;
803 }
804 } else {
805 new_regd = tmp_regd;
806 }
807
808 ret:
809 return new_regd;
810 }
811
ath11k_regd_update_chan_list_work(struct work_struct * work)812 void ath11k_regd_update_chan_list_work(struct work_struct *work)
813 {
814 struct ath11k *ar = container_of(work, struct ath11k,
815 channel_update_work);
816 struct scan_chan_list_params *params;
817 struct list_head local_update_list;
818 int left;
819
820 INIT_LIST_HEAD(&local_update_list);
821
822 spin_lock_bh(&ar->data_lock);
823 list_splice_tail_init(&ar->channel_update_queue, &local_update_list);
824 spin_unlock_bh(&ar->data_lock);
825
826 while ((params = list_first_entry_or_null(&local_update_list,
827 struct scan_chan_list_params,
828 list))) {
829 if (ar->state_11d != ATH11K_11D_IDLE) {
830 left = wait_for_completion_timeout(&ar->completed_11d_scan,
831 ATH11K_SCAN_TIMEOUT_HZ);
832 if (!left) {
833 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
834 "failed to receive 11d scan complete: timed out\n");
835 ar->state_11d = ATH11K_11D_IDLE;
836 }
837
838 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
839 "reg 11d scan wait left time %d\n", left);
840 }
841
842 if ((ar->scan.state == ATH11K_SCAN_STARTING ||
843 ar->scan.state == ATH11K_SCAN_RUNNING)) {
844 left = wait_for_completion_timeout(&ar->scan.completed,
845 ATH11K_SCAN_TIMEOUT_HZ);
846 if (!left)
847 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
848 "failed to receive hw scan complete: timed out\n");
849
850 ath11k_dbg(ar->ab, ATH11K_DBG_REG,
851 "reg hw scan wait left time %d\n", left);
852 }
853
854 ath11k_wmi_send_scan_chan_list_cmd(ar, params);
855 list_del(¶ms->list);
856 kfree(params);
857 }
858 }
859
ath11k_reg_is_world_alpha(char * alpha)860 static bool ath11k_reg_is_world_alpha(char *alpha)
861 {
862 if (alpha[0] == '0' && alpha[1] == '0')
863 return true;
864
865 if (alpha[0] == 'n' && alpha[1] == 'a')
866 return true;
867
868 return false;
869 }
870
ath11k_reg_get_ar_vdev_type(struct ath11k * ar)871 static enum wmi_vdev_type ath11k_reg_get_ar_vdev_type(struct ath11k *ar)
872 {
873 struct ath11k_vif *arvif;
874
875 /* Currently each struct ath11k maps to one struct ieee80211_hw/wiphy
876 * and one struct ieee80211_regdomain, so it could only store one group
877 * reg rules. It means multi-interface concurrency in the same ath11k is
878 * not support for the regdomain. So get the vdev type of the first entry
879 * now. After concurrency support for the regdomain, this should change.
880 */
881 arvif = list_first_entry_or_null(&ar->arvifs, struct ath11k_vif, list);
882 if (arvif)
883 return arvif->vdev_type;
884
885 return WMI_VDEV_TYPE_UNSPEC;
886 }
887
ath11k_reg_handle_chan_list(struct ath11k_base * ab,struct cur_regulatory_info * reg_info,enum ieee80211_ap_reg_power power_type)888 int ath11k_reg_handle_chan_list(struct ath11k_base *ab,
889 struct cur_regulatory_info *reg_info,
890 enum ieee80211_ap_reg_power power_type)
891 {
892 struct ieee80211_regdomain *regd;
893 bool intersect = false;
894 int pdev_idx;
895 struct ath11k *ar;
896 enum wmi_vdev_type vdev_type;
897
898 ath11k_dbg(ab, ATH11K_DBG_WMI, "event reg handle chan list");
899
900 if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
901 /* In case of failure to set the requested ctry,
902 * fw retains the current regd. We print a failure info
903 * and return from here.
904 */
905 ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
906 return -EINVAL;
907 }
908
909 pdev_idx = reg_info->phy_id;
910
911 /* Avoid default reg rule updates sent during FW recovery if
912 * it is already available
913 */
914 spin_lock_bh(&ab->base_lock);
915 if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&
916 ab->default_regd[pdev_idx]) {
917 spin_unlock_bh(&ab->base_lock);
918 goto retfail;
919 }
920 spin_unlock_bh(&ab->base_lock);
921
922 if (pdev_idx >= ab->num_radios) {
923 /* Process the event for phy0 only if single_pdev_only
924 * is true. If pdev_idx is valid but not 0, discard the
925 * event. Otherwise, it goes to fallback. In either case
926 * ath11k_reg_reset_info() needs to be called to avoid
927 * memory leak issue.
928 */
929 ath11k_reg_reset_info(reg_info);
930
931 if (ab->hw_params.single_pdev_only &&
932 pdev_idx < ab->hw_params.num_rxdma_per_pdev)
933 return 0;
934 goto fallback;
935 }
936
937 /* Avoid multiple overwrites to default regd, during core
938 * stop-start after mac registration.
939 */
940 if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
941 !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
942 (char *)reg_info->alpha2, 2))
943 goto retfail;
944
945 /* Intersect new rules with default regd if a new country setting was
946 * requested, i.e a default regd was already set during initialization
947 * and the regd coming from this event has a valid country info.
948 */
949 if (ab->default_regd[pdev_idx] &&
950 !ath11k_reg_is_world_alpha((char *)
951 ab->default_regd[pdev_idx]->alpha2) &&
952 !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
953 intersect = true;
954
955 ar = ab->pdevs[pdev_idx].ar;
956 vdev_type = ath11k_reg_get_ar_vdev_type(ar);
957
958 ath11k_dbg(ab, ATH11K_DBG_WMI,
959 "wmi handle chan list power type %d vdev type %d intersect %d\n",
960 power_type, vdev_type, intersect);
961
962 regd = ath11k_reg_build_regd(ab, reg_info, intersect, vdev_type, power_type);
963 if (!regd) {
964 ath11k_warn(ab, "failed to build regd from reg_info\n");
965 goto fallback;
966 }
967
968 if (power_type == IEEE80211_REG_UNSET_AP) {
969 ath11k_reg_reset_info(&ab->reg_info_store[pdev_idx]);
970 ab->reg_info_store[pdev_idx] = *reg_info;
971 }
972
973 spin_lock_bh(&ab->base_lock);
974 if (ab->default_regd[pdev_idx]) {
975 /* The initial rules from FW after WMI Init is to build
976 * the default regd. From then on, any rules updated for
977 * the pdev could be due to user reg changes.
978 * Free previously built regd before assigning the newly
979 * generated regd to ar. NULL pointer handling will be
980 * taken care by kfree itself.
981 */
982 ar = ab->pdevs[pdev_idx].ar;
983 kfree(ab->new_regd[pdev_idx]);
984 ab->new_regd[pdev_idx] = regd;
985 queue_work(ab->workqueue, &ar->regd_update_work);
986 } else {
987 /* This regd would be applied during mac registration and is
988 * held constant throughout for regd intersection purpose
989 */
990 ab->default_regd[pdev_idx] = regd;
991 }
992 ab->dfs_region = reg_info->dfs_region;
993 spin_unlock_bh(&ab->base_lock);
994
995 return 0;
996
997 fallback:
998 /* Fallback to older reg (by sending previous country setting
999 * again if fw has succeeded and we failed to process here.
1000 * The Regdomain should be uniform across driver and fw. Since the
1001 * FW has processed the command and sent a success status, we expect
1002 * this function to succeed as well. If it doesn't, CTRY needs to be
1003 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
1004 */
1005 /* TODO: This is rare, but still should also be handled */
1006 WARN_ON(1);
1007
1008 retfail:
1009
1010 return -EINVAL;
1011 }
1012
ath11k_regd_update_work(struct work_struct * work)1013 void ath11k_regd_update_work(struct work_struct *work)
1014 {
1015 struct ath11k *ar = container_of(work, struct ath11k,
1016 regd_update_work);
1017 int ret;
1018
1019 ret = ath11k_regd_update(ar);
1020 if (ret) {
1021 /* Firmware has already moved to the new regd. We need
1022 * to maintain channel consistency across FW, Host driver
1023 * and userspace. Hence as a fallback mechanism we can set
1024 * the prev or default country code to the firmware.
1025 */
1026 /* TODO: Implement Fallback Mechanism */
1027 }
1028 }
1029
ath11k_reg_init(struct ath11k * ar)1030 void ath11k_reg_init(struct ath11k *ar)
1031 {
1032 ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
1033 ar->hw->wiphy->flags |= WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER;
1034 ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
1035 }
1036
ath11k_reg_reset_info(struct cur_regulatory_info * reg_info)1037 void ath11k_reg_reset_info(struct cur_regulatory_info *reg_info)
1038 {
1039 int i, j;
1040
1041 if (!reg_info)
1042 return;
1043
1044 kfree(reg_info->reg_rules_2ghz_ptr);
1045 kfree(reg_info->reg_rules_5ghz_ptr);
1046
1047 for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
1048 kfree(reg_info->reg_rules_6ghz_ap_ptr[i]);
1049
1050 for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
1051 kfree(reg_info->reg_rules_6ghz_client_ptr[i][j]);
1052 }
1053
1054 memset(reg_info, 0, sizeof(*reg_info));
1055 }
1056
ath11k_reg_free(struct ath11k_base * ab)1057 void ath11k_reg_free(struct ath11k_base *ab)
1058 {
1059 int i;
1060
1061 for (i = 0; i < ab->num_radios; i++)
1062 ath11k_reg_reset_info(&ab->reg_info_store[i]);
1063
1064 kfree(ab->reg_info_store);
1065 ab->reg_info_store = NULL;
1066
1067 for (i = 0; i < ab->hw_params.max_radios; i++) {
1068 kfree(ab->default_regd[i]);
1069 kfree(ab->new_regd[i]);
1070 }
1071 }
1072
ath11k_reg_set_cc(struct ath11k * ar)1073 int ath11k_reg_set_cc(struct ath11k *ar)
1074 {
1075 struct wmi_set_current_country_params set_current_param = {};
1076
1077 memcpy(&set_current_param.alpha2, ar->alpha2, 2);
1078 return ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
1079 }
1080