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