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