]> Git Repo - J-linux.git/blob - drivers/net/wireless/ath/ath12k/reg.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / net / wireless / ath / ath12k / reg.c
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 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 #include "core.h"
8 #include "debug.h"
9
10 /* World regdom to be used in case default regd from fw is unavailable */
11 #define ATH12K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
12 #define ATH12K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
13                                           NL80211_RRF_NO_IR)
14 #define ATH12K_5GHZ_5725_5850    REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
15                                           NL80211_RRF_NO_IR)
16
17 #define ETSI_WEATHER_RADAR_BAND_LOW             5590
18 #define ETSI_WEATHER_RADAR_BAND_HIGH            5650
19 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT     600000
20
21 static const struct ieee80211_regdomain ath12k_world_regd = {
22         .n_reg_rules = 3,
23         .alpha2 = "00",
24         .reg_rules = {
25                 ATH12K_2GHZ_CH01_11,
26                 ATH12K_5GHZ_5150_5350,
27                 ATH12K_5GHZ_5725_5850,
28         }
29 };
30
31 static bool ath12k_regdom_changes(struct ieee80211_hw *hw, char *alpha2)
32 {
33         const struct ieee80211_regdomain *regd;
34
35         regd = rcu_dereference_rtnl(hw->wiphy->regd);
36         /* This can happen during wiphy registration where the previous
37          * user request is received before we update the regd received
38          * from firmware.
39          */
40         if (!regd)
41                 return true;
42
43         return memcmp(regd->alpha2, alpha2, 2) != 0;
44 }
45
46 static void
47 ath12k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
48 {
49         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
50         struct ath12k_wmi_init_country_arg arg;
51         struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
52         struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
53         int ret, i;
54
55         ath12k_dbg(ar->ab, ATH12K_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                 ath12k_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                 ath12k_dbg(ar->ab, ATH12K_DBG_REG,
71                            "Country Setting is not allowed\n");
72                 return;
73         }
74
75         if (!ath12k_regdom_changes(hw, request->alpha2)) {
76                 ath12k_dbg(ar->ab, ATH12K_DBG_REG, "Country is already set\n");
77                 return;
78         }
79
80         /* Set the country code to the firmware and wait for
81          * the WMI_REG_CHAN_LIST_CC EVENT for updating the
82          * reg info
83          */
84         arg.flags = ALPHA_IS_SET;
85         memcpy(&arg.cc_info.alpha2, request->alpha2, 2);
86         arg.cc_info.alpha2[2] = 0;
87
88         /* Allow fresh updates to wiphy regd */
89         ah->regd_updated = false;
90
91         /* Send the reg change request to all the radios */
92         for_each_ar(ah, ar, i) {
93                 ret = ath12k_wmi_send_init_country_cmd(ar, &arg);
94                 if (ret)
95                         ath12k_warn(ar->ab,
96                                     "INIT Country code set to fw failed : %d\n", ret);
97         }
98 }
99
100 int ath12k_reg_update_chan_list(struct ath12k *ar)
101 {
102         struct ieee80211_supported_band **bands;
103         struct ath12k_wmi_scan_chan_list_arg *arg;
104         struct ieee80211_channel *channel;
105         struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
106         struct ath12k_wmi_channel_arg *ch;
107         enum nl80211_band band;
108         int num_channels = 0;
109         int i, ret;
110
111         bands = hw->wiphy->bands;
112         for (band = 0; band < NUM_NL80211_BANDS; band++) {
113                 if (!(ar->mac.sbands[band].channels && bands[band]))
114                         continue;
115
116                 for (i = 0; i < bands[band]->n_channels; i++) {
117                         if (bands[band]->channels[i].flags &
118                             IEEE80211_CHAN_DISABLED)
119                                 continue;
120
121                         num_channels++;
122                 }
123         }
124
125         if (WARN_ON(!num_channels))
126                 return -EINVAL;
127
128         arg = kzalloc(struct_size(arg, channel, num_channels), GFP_KERNEL);
129
130         if (!arg)
131                 return -ENOMEM;
132
133         arg->pdev_id = ar->pdev->pdev_id;
134         arg->nallchans = num_channels;
135
136         ch = arg->channel;
137
138         for (band = 0; band < NUM_NL80211_BANDS; band++) {
139                 if (!(ar->mac.sbands[band].channels && bands[band]))
140                         continue;
141
142                 for (i = 0; i < bands[band]->n_channels; i++) {
143                         channel = &bands[band]->channels[i];
144
145                         if (channel->flags & IEEE80211_CHAN_DISABLED)
146                                 continue;
147
148                         /* TODO: Set to true/false based on some condition? */
149                         ch->allow_ht = true;
150                         ch->allow_vht = true;
151                         ch->allow_he = true;
152
153                         ch->dfs_set =
154                                 !!(channel->flags & IEEE80211_CHAN_RADAR);
155                         ch->is_chan_passive = !!(channel->flags &
156                                                 IEEE80211_CHAN_NO_IR);
157                         ch->is_chan_passive |= ch->dfs_set;
158                         ch->mhz = channel->center_freq;
159                         ch->cfreq1 = channel->center_freq;
160                         ch->minpower = 0;
161                         ch->maxpower = channel->max_power * 2;
162                         ch->maxregpower = channel->max_reg_power * 2;
163                         ch->antennamax = channel->max_antenna_gain * 2;
164
165                         /* TODO: Use appropriate phymodes */
166                         if (channel->band == NL80211_BAND_2GHZ)
167                                 ch->phy_mode = MODE_11G;
168                         else
169                                 ch->phy_mode = MODE_11A;
170
171                         if (channel->band == NL80211_BAND_6GHZ &&
172                             cfg80211_channel_is_psc(channel))
173                                 ch->psc_channel = true;
174
175                         ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
176                                    "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
177                                    i, arg->nallchans,
178                                    ch->mhz, ch->maxpower, ch->maxregpower,
179                                    ch->antennamax, ch->phy_mode);
180
181                         ch++;
182                         /* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
183                          * set_agile, reg_class_idx
184                          */
185                 }
186         }
187
188         ret = ath12k_wmi_send_scan_chan_list_cmd(ar, arg);
189         kfree(arg);
190
191         return ret;
192 }
193
194 static void ath12k_copy_regd(struct ieee80211_regdomain *regd_orig,
195                              struct ieee80211_regdomain *regd_copy)
196 {
197         u8 i;
198
199         /* The caller should have checked error conditions */
200         memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
201
202         for (i = 0; i < regd_orig->n_reg_rules; i++)
203                 memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
204                        sizeof(struct ieee80211_reg_rule));
205 }
206
207 int ath12k_regd_update(struct ath12k *ar, bool init)
208 {
209         struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
210         struct ieee80211_hw *hw = ah->hw;
211         struct ieee80211_regdomain *regd, *regd_copy = NULL;
212         int ret, regd_len, pdev_id;
213         struct ath12k_base *ab;
214         int i;
215
216         ab = ar->ab;
217
218         /* If one of the radios within ah has already updated the regd for
219          * the wiphy, then avoid setting regd again
220          */
221         if (ah->regd_updated)
222                 return 0;
223
224         /* firmware provides reg rules which are similar for 2 GHz and 5 GHz
225          * pdev but 6 GHz pdev has superset of all rules including rules for
226          * all bands, we prefer 6 GHz pdev's rules to be used for setup of
227          * the wiphy regd.
228          * If 6 GHz pdev was part of the ath12k_hw, wait for the 6 GHz pdev,
229          * else pick the first pdev which calls this function and use its
230          * regd to update global hw regd.
231          * The regd_updated flag set at the end will not allow any further
232          * updates.
233          */
234         if (ah->use_6ghz_regd && !ar->supports_6ghz)
235                 return 0;
236
237         pdev_id = ar->pdev_idx;
238
239         spin_lock_bh(&ab->base_lock);
240
241         if (init) {
242                 /* Apply the regd received during init through
243                  * WMI_REG_CHAN_LIST_CC event. In case of failure to
244                  * receive the regd, initialize with a default world
245                  * regulatory.
246                  */
247                 if (ab->default_regd[pdev_id]) {
248                         regd = ab->default_regd[pdev_id];
249                 } else {
250                         ath12k_warn(ab,
251                                     "failed to receive default regd during init\n");
252                         regd = (struct ieee80211_regdomain *)&ath12k_world_regd;
253                 }
254         } else {
255                 regd = ab->new_regd[pdev_id];
256         }
257
258         if (!regd) {
259                 ret = -EINVAL;
260                 spin_unlock_bh(&ab->base_lock);
261                 goto err;
262         }
263
264         regd_len = sizeof(*regd) + (regd->n_reg_rules *
265                 sizeof(struct ieee80211_reg_rule));
266
267         regd_copy = kzalloc(regd_len, GFP_ATOMIC);
268         if (regd_copy)
269                 ath12k_copy_regd(regd, regd_copy);
270
271         spin_unlock_bh(&ab->base_lock);
272
273         if (!regd_copy) {
274                 ret = -ENOMEM;
275                 goto err;
276         }
277
278         rtnl_lock();
279         wiphy_lock(hw->wiphy);
280         ret = regulatory_set_wiphy_regd_sync(hw->wiphy, regd_copy);
281         wiphy_unlock(hw->wiphy);
282         rtnl_unlock();
283
284         kfree(regd_copy);
285
286         if (ret)
287                 goto err;
288
289         if (ah->state != ATH12K_HW_STATE_ON)
290                 goto skip;
291
292         ah->regd_updated = true;
293         /* Apply the new regd to all the radios, this is expected to be received only once
294          * since we check for ah->regd_updated and allow here only once.
295          */
296         for_each_ar(ah, ar, i) {
297                 ab = ar->ab;
298                 ret = ath12k_reg_update_chan_list(ar);
299                 if (ret)
300                         goto err;
301         }
302 skip:
303         return 0;
304 err:
305         ath12k_warn(ab, "failed to perform regd update : %d\n", ret);
306         return ret;
307 }
308
309 static enum nl80211_dfs_regions
310 ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region)
311 {
312         switch (dfs_region) {
313         case ATH12K_DFS_REG_FCC:
314         case ATH12K_DFS_REG_CN:
315                 return NL80211_DFS_FCC;
316         case ATH12K_DFS_REG_ETSI:
317         case ATH12K_DFS_REG_KR:
318                 return NL80211_DFS_ETSI;
319         case ATH12K_DFS_REG_MKK:
320         case ATH12K_DFS_REG_MKK_N:
321                 return NL80211_DFS_JP;
322         default:
323                 return NL80211_DFS_UNSET;
324         }
325 }
326
327 static u32 ath12k_map_fw_reg_flags(u16 reg_flags)
328 {
329         u32 flags = 0;
330
331         if (reg_flags & REGULATORY_CHAN_NO_IR)
332                 flags = NL80211_RRF_NO_IR;
333
334         if (reg_flags & REGULATORY_CHAN_RADAR)
335                 flags |= NL80211_RRF_DFS;
336
337         if (reg_flags & REGULATORY_CHAN_NO_OFDM)
338                 flags |= NL80211_RRF_NO_OFDM;
339
340         if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
341                 flags |= NL80211_RRF_NO_OUTDOOR;
342
343         if (reg_flags & REGULATORY_CHAN_NO_HT40)
344                 flags |= NL80211_RRF_NO_HT40;
345
346         if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
347                 flags |= NL80211_RRF_NO_80MHZ;
348
349         if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
350                 flags |= NL80211_RRF_NO_160MHZ;
351
352         return flags;
353 }
354
355 static u32 ath12k_map_fw_phy_flags(u32 phy_flags)
356 {
357         u32 flags = 0;
358
359         if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX)
360                 flags |= NL80211_RRF_NO_HE;
361
362         if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE)
363                 flags |= NL80211_RRF_NO_EHT;
364
365         return flags;
366 }
367
368 static bool
369 ath12k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
370                          struct ieee80211_reg_rule *rule2)
371 {
372         u32 start_freq1, end_freq1;
373         u32 start_freq2, end_freq2;
374
375         start_freq1 = rule1->freq_range.start_freq_khz;
376         start_freq2 = rule2->freq_range.start_freq_khz;
377
378         end_freq1 = rule1->freq_range.end_freq_khz;
379         end_freq2 = rule2->freq_range.end_freq_khz;
380
381         if ((start_freq1 >= start_freq2 &&
382              start_freq1 < end_freq2) ||
383             (start_freq2 > start_freq1 &&
384              start_freq2 < end_freq1))
385                 return true;
386
387         /* TODO: Should we restrict intersection feasibility
388          *  based on min bandwidth of the intersected region also,
389          *  say the intersected rule should have a  min bandwidth
390          * of 20MHz?
391          */
392
393         return false;
394 }
395
396 static void ath12k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
397                                        struct ieee80211_reg_rule *rule2,
398                                        struct ieee80211_reg_rule *new_rule)
399 {
400         u32 start_freq1, end_freq1;
401         u32 start_freq2, end_freq2;
402         u32 freq_diff, max_bw;
403
404         start_freq1 = rule1->freq_range.start_freq_khz;
405         start_freq2 = rule2->freq_range.start_freq_khz;
406
407         end_freq1 = rule1->freq_range.end_freq_khz;
408         end_freq2 = rule2->freq_range.end_freq_khz;
409
410         new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
411                                                     start_freq2);
412         new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
413
414         freq_diff = new_rule->freq_range.end_freq_khz -
415                         new_rule->freq_range.start_freq_khz;
416         max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
417                        rule2->freq_range.max_bandwidth_khz);
418         new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
419
420         new_rule->power_rule.max_antenna_gain =
421                 min_t(u32, rule1->power_rule.max_antenna_gain,
422                       rule2->power_rule.max_antenna_gain);
423
424         new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
425                                               rule2->power_rule.max_eirp);
426
427         /* Use the flags of both the rules */
428         new_rule->flags = rule1->flags | rule2->flags;
429
430         /* To be safe, lts use the max cac timeout of both rules */
431         new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
432                                      rule2->dfs_cac_ms);
433 }
434
435 static struct ieee80211_regdomain *
436 ath12k_regd_intersect(struct ieee80211_regdomain *default_regd,
437                       struct ieee80211_regdomain *curr_regd)
438 {
439         u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
440         struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
441         struct ieee80211_regdomain *new_regd = NULL;
442         u8 i, j, k;
443
444         num_old_regd_rules = default_regd->n_reg_rules;
445         num_curr_regd_rules = curr_regd->n_reg_rules;
446         num_new_regd_rules = 0;
447
448         /* Find the number of intersecting rules to allocate new regd memory */
449         for (i = 0; i < num_old_regd_rules; i++) {
450                 old_rule = default_regd->reg_rules + i;
451                 for (j = 0; j < num_curr_regd_rules; j++) {
452                         curr_rule = curr_regd->reg_rules + j;
453
454                         if (ath12k_reg_can_intersect(old_rule, curr_rule))
455                                 num_new_regd_rules++;
456                 }
457         }
458
459         if (!num_new_regd_rules)
460                 return NULL;
461
462         new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
463                         sizeof(struct ieee80211_reg_rule)),
464                         GFP_ATOMIC);
465
466         if (!new_regd)
467                 return NULL;
468
469         /* We set the new country and dfs region directly and only trim
470          * the freq, power, antenna gain by intersecting with the
471          * default regdomain. Also MAX of the dfs cac timeout is selected.
472          */
473         new_regd->n_reg_rules = num_new_regd_rules;
474         memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
475         new_regd->dfs_region = curr_regd->dfs_region;
476         new_rule = new_regd->reg_rules;
477
478         for (i = 0, k = 0; i < num_old_regd_rules; i++) {
479                 old_rule = default_regd->reg_rules + i;
480                 for (j = 0; j < num_curr_regd_rules; j++) {
481                         curr_rule = curr_regd->reg_rules + j;
482
483                         if (ath12k_reg_can_intersect(old_rule, curr_rule))
484                                 ath12k_reg_intersect_rules(old_rule, curr_rule,
485                                                            (new_rule + k++));
486                 }
487         }
488         return new_regd;
489 }
490
491 static const char *
492 ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
493 {
494         switch (dfs_region) {
495         case NL80211_DFS_FCC:
496                 return "FCC";
497         case NL80211_DFS_ETSI:
498                 return "ETSI";
499         case NL80211_DFS_JP:
500                 return "JP";
501         default:
502                 return "UNSET";
503         }
504 }
505
506 static u16
507 ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
508 {
509         u16 bw;
510
511         bw = end_freq - start_freq;
512         bw = min_t(u16, bw, max_bw);
513
514         if (bw >= 80 && bw < 160)
515                 bw = 80;
516         else if (bw >= 40 && bw < 80)
517                 bw = 40;
518         else if (bw < 40)
519                 bw = 20;
520
521         return bw;
522 }
523
524 static void
525 ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
526                        u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
527                        u32 reg_flags)
528 {
529         reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
530         reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
531         reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
532         reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
533         reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
534         reg_rule->flags = reg_flags;
535 }
536
537 static void
538 ath12k_reg_update_weather_radar_band(struct ath12k_base *ab,
539                                      struct ieee80211_regdomain *regd,
540                                      struct ath12k_reg_rule *reg_rule,
541                                      u8 *rule_idx, u32 flags, u16 max_bw)
542 {
543         u32 end_freq;
544         u16 bw;
545         u8 i;
546
547         i = *rule_idx;
548
549         bw = ath12k_reg_adjust_bw(reg_rule->start_freq,
550                                   ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
551
552         ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq,
553                                ETSI_WEATHER_RADAR_BAND_LOW, bw,
554                                reg_rule->ant_gain, reg_rule->reg_power,
555                                flags);
556
557         ath12k_dbg(ab, ATH12K_DBG_REG,
558                    "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
559                    i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW,
560                    bw, reg_rule->ant_gain, reg_rule->reg_power,
561                    regd->reg_rules[i].dfs_cac_ms,
562                    flags);
563
564         if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH)
565                 end_freq = ETSI_WEATHER_RADAR_BAND_HIGH;
566         else
567                 end_freq = reg_rule->end_freq;
568
569         bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
570                                   max_bw);
571
572         i++;
573
574         ath12k_reg_update_rule(regd->reg_rules + i,
575                                ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw,
576                                reg_rule->ant_gain, reg_rule->reg_power,
577                                flags);
578
579         regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
580
581         ath12k_dbg(ab, ATH12K_DBG_REG,
582                    "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
583                    i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
584                    bw, reg_rule->ant_gain, reg_rule->reg_power,
585                    regd->reg_rules[i].dfs_cac_ms,
586                    flags);
587
588         if (end_freq == reg_rule->end_freq) {
589                 regd->n_reg_rules--;
590                 *rule_idx = i;
591                 return;
592         }
593
594         bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
595                                   reg_rule->end_freq, max_bw);
596
597         i++;
598
599         ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH,
600                                reg_rule->end_freq, bw,
601                                reg_rule->ant_gain, reg_rule->reg_power,
602                                flags);
603
604         ath12k_dbg(ab, ATH12K_DBG_REG,
605                    "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
606                    i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq,
607                    bw, reg_rule->ant_gain, reg_rule->reg_power,
608                    regd->reg_rules[i].dfs_cac_ms,
609                    flags);
610
611         *rule_idx = i;
612 }
613
614 struct ieee80211_regdomain *
615 ath12k_reg_build_regd(struct ath12k_base *ab,
616                       struct ath12k_reg_info *reg_info, bool intersect)
617 {
618         struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
619         struct ath12k_reg_rule *reg_rule;
620         u8 i = 0, j = 0, k = 0;
621         u8 num_rules;
622         u16 max_bw;
623         u32 flags;
624         char alpha2[3];
625
626         num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
627
628         /* FIXME: Currently taking reg rules for 6G only from Indoor AP mode list.
629          * This can be updated to choose the combination dynamically based on AP
630          * type and client type, after complete 6G regulatory support is added.
631          */
632         if (reg_info->is_ext_reg_event)
633                 num_rules += reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP];
634
635         if (!num_rules)
636                 goto ret;
637
638         /* Add max additional rules to accommodate weather radar band */
639         if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI)
640                 num_rules += 2;
641
642         tmp_regd = kzalloc(sizeof(*tmp_regd) +
643                            (num_rules * sizeof(struct ieee80211_reg_rule)),
644                            GFP_ATOMIC);
645         if (!tmp_regd)
646                 goto ret;
647
648         memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
649         memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
650         alpha2[2] = '\0';
651         tmp_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region);
652
653         ath12k_dbg(ab, ATH12K_DBG_REG,
654                    "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
655                    alpha2, ath12k_reg_get_regdom_str(tmp_regd->dfs_region),
656                    reg_info->dfs_region, num_rules);
657         /* Update reg_rules[] below. Firmware is expected to
658          * send these rules in order(2G rules first and then 5G)
659          */
660         for (; i < num_rules; i++) {
661                 if (reg_info->num_2g_reg_rules &&
662                     (i < reg_info->num_2g_reg_rules)) {
663                         reg_rule = reg_info->reg_rules_2g_ptr + i;
664                         max_bw = min_t(u16, reg_rule->max_bw,
665                                        reg_info->max_bw_2g);
666                         flags = 0;
667                 } else if (reg_info->num_5g_reg_rules &&
668                            (j < reg_info->num_5g_reg_rules)) {
669                         reg_rule = reg_info->reg_rules_5g_ptr + j++;
670                         max_bw = min_t(u16, reg_rule->max_bw,
671                                        reg_info->max_bw_5g);
672
673                         /* FW doesn't pass NL80211_RRF_AUTO_BW flag for
674                          * BW Auto correction, we can enable this by default
675                          * for all 5G rules here. The regulatory core performs
676                          * BW correction if required and applies flags as
677                          * per other BW rule flags we pass from here
678                          */
679                         flags = NL80211_RRF_AUTO_BW;
680                 } else if (reg_info->is_ext_reg_event &&
681                            reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP] &&
682                         (k < reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP])) {
683                         reg_rule = reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP] + k++;
684                         max_bw = min_t(u16, reg_rule->max_bw,
685                                        reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP]);
686                         flags = NL80211_RRF_AUTO_BW;
687                 } else {
688                         break;
689                 }
690
691                 flags |= ath12k_map_fw_reg_flags(reg_rule->flags);
692                 flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap);
693
694                 ath12k_reg_update_rule(tmp_regd->reg_rules + i,
695                                        reg_rule->start_freq,
696                                        reg_rule->end_freq, max_bw,
697                                        reg_rule->ant_gain, reg_rule->reg_power,
698                                        flags);
699
700                 /* Update dfs cac timeout if the dfs domain is ETSI and the
701                  * new rule covers weather radar band.
702                  * Default value of '0' corresponds to 60s timeout, so no
703                  * need to update that for other rules.
704                  */
705                 if (flags & NL80211_RRF_DFS &&
706                     reg_info->dfs_region == ATH12K_DFS_REG_ETSI &&
707                     (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
708                     reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
709                         ath12k_reg_update_weather_radar_band(ab, tmp_regd,
710                                                              reg_rule, &i,
711                                                              flags, max_bw);
712                         continue;
713                 }
714
715                 if (reg_info->is_ext_reg_event) {
716                         ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
717                                    i + 1, reg_rule->start_freq, reg_rule->end_freq,
718                                    max_bw, reg_rule->ant_gain, reg_rule->reg_power,
719                                    tmp_regd->reg_rules[i].dfs_cac_ms,
720                                    flags, reg_rule->psd_flag, reg_rule->psd_eirp);
721                 } else {
722                         ath12k_dbg(ab, ATH12K_DBG_REG,
723                                    "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
724                                    i + 1, reg_rule->start_freq, reg_rule->end_freq,
725                                    max_bw, reg_rule->ant_gain, reg_rule->reg_power,
726                                    tmp_regd->reg_rules[i].dfs_cac_ms,
727                                    flags);
728                 }
729         }
730
731         tmp_regd->n_reg_rules = i;
732
733         if (intersect) {
734                 default_regd = ab->default_regd[reg_info->phy_id];
735
736                 /* Get a new regd by intersecting the received regd with
737                  * our default regd.
738                  */
739                 new_regd = ath12k_regd_intersect(default_regd, tmp_regd);
740                 kfree(tmp_regd);
741                 if (!new_regd) {
742                         ath12k_warn(ab, "Unable to create intersected regdomain\n");
743                         goto ret;
744                 }
745         } else {
746                 new_regd = tmp_regd;
747         }
748
749 ret:
750         return new_regd;
751 }
752
753 void ath12k_regd_update_work(struct work_struct *work)
754 {
755         struct ath12k *ar = container_of(work, struct ath12k,
756                                          regd_update_work);
757         int ret;
758
759         ret = ath12k_regd_update(ar, false);
760         if (ret) {
761                 /* Firmware has already moved to the new regd. We need
762                  * to maintain channel consistency across FW, Host driver
763                  * and userspace. Hence as a fallback mechanism we can set
764                  * the prev or default country code to the firmware.
765                  */
766                 /* TODO: Implement Fallback Mechanism */
767         }
768 }
769
770 void ath12k_reg_init(struct ieee80211_hw *hw)
771 {
772         hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
773         hw->wiphy->reg_notifier = ath12k_reg_notifier;
774 }
775
776 void ath12k_reg_free(struct ath12k_base *ab)
777 {
778         int i;
779
780         for (i = 0; i < ab->hw_params->max_radios; i++) {
781                 kfree(ab->default_regd[i]);
782                 kfree(ab->new_regd[i]);
783         }
784 }
This page took 0.071555 seconds and 4 git commands to generate.