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