reg.c 21 KB

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