xref: /linux/drivers/net/wireless/ath/ath12k/reg.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
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 ieee80211_hw *hw = ath12k_ar_to_hw(ar);
210 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
211 	struct ath12k_hw *ah = ar->ah;
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 	ah->regd_updated = true;
290 	/* Apply the new regd to all the radios, this is expected to be received only once
291 	 * since we check for ah->regd_updated and allow here only once.
292 	 */
293 	for_each_ar(ah, ar, i) {
294 		if (ar->state == ATH12K_STATE_ON) {
295 			ab = ar->ab;
296 			ret = ath12k_reg_update_chan_list(ar);
297 			if (ret)
298 				goto err;
299 		}
300 	}
301 
302 	return 0;
303 err:
304 	ath12k_warn(ab, "failed to perform regd update : %d\n", ret);
305 	return ret;
306 }
307 
308 static enum nl80211_dfs_regions
309 ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region)
310 {
311 	switch (dfs_region) {
312 	case ATH12K_DFS_REG_FCC:
313 	case ATH12K_DFS_REG_CN:
314 		return NL80211_DFS_FCC;
315 	case ATH12K_DFS_REG_ETSI:
316 	case ATH12K_DFS_REG_KR:
317 		return NL80211_DFS_ETSI;
318 	case ATH12K_DFS_REG_MKK:
319 	case ATH12K_DFS_REG_MKK_N:
320 		return NL80211_DFS_JP;
321 	default:
322 		return NL80211_DFS_UNSET;
323 	}
324 }
325 
326 static u32 ath12k_map_fw_reg_flags(u16 reg_flags)
327 {
328 	u32 flags = 0;
329 
330 	if (reg_flags & REGULATORY_CHAN_NO_IR)
331 		flags = NL80211_RRF_NO_IR;
332 
333 	if (reg_flags & REGULATORY_CHAN_RADAR)
334 		flags |= NL80211_RRF_DFS;
335 
336 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
337 		flags |= NL80211_RRF_NO_OFDM;
338 
339 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
340 		flags |= NL80211_RRF_NO_OUTDOOR;
341 
342 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
343 		flags |= NL80211_RRF_NO_HT40;
344 
345 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
346 		flags |= NL80211_RRF_NO_80MHZ;
347 
348 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
349 		flags |= NL80211_RRF_NO_160MHZ;
350 
351 	return flags;
352 }
353 
354 static u32 ath12k_map_fw_phy_flags(u32 phy_flags)
355 {
356 	u32 flags = 0;
357 
358 	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX)
359 		flags |= NL80211_RRF_NO_HE;
360 
361 	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE)
362 		flags |= NL80211_RRF_NO_EHT;
363 
364 	return flags;
365 }
366 
367 static bool
368 ath12k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
369 			 struct ieee80211_reg_rule *rule2)
370 {
371 	u32 start_freq1, end_freq1;
372 	u32 start_freq2, end_freq2;
373 
374 	start_freq1 = rule1->freq_range.start_freq_khz;
375 	start_freq2 = rule2->freq_range.start_freq_khz;
376 
377 	end_freq1 = rule1->freq_range.end_freq_khz;
378 	end_freq2 = rule2->freq_range.end_freq_khz;
379 
380 	if ((start_freq1 >= start_freq2 &&
381 	     start_freq1 < end_freq2) ||
382 	    (start_freq2 > start_freq1 &&
383 	     start_freq2 < end_freq1))
384 		return true;
385 
386 	/* TODO: Should we restrict intersection feasibility
387 	 *  based on min bandwidth of the intersected region also,
388 	 *  say the intersected rule should have a  min bandwidth
389 	 * of 20MHz?
390 	 */
391 
392 	return false;
393 }
394 
395 static void ath12k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
396 				       struct ieee80211_reg_rule *rule2,
397 				       struct ieee80211_reg_rule *new_rule)
398 {
399 	u32 start_freq1, end_freq1;
400 	u32 start_freq2, end_freq2;
401 	u32 freq_diff, max_bw;
402 
403 	start_freq1 = rule1->freq_range.start_freq_khz;
404 	start_freq2 = rule2->freq_range.start_freq_khz;
405 
406 	end_freq1 = rule1->freq_range.end_freq_khz;
407 	end_freq2 = rule2->freq_range.end_freq_khz;
408 
409 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
410 						    start_freq2);
411 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
412 
413 	freq_diff = new_rule->freq_range.end_freq_khz -
414 			new_rule->freq_range.start_freq_khz;
415 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
416 		       rule2->freq_range.max_bandwidth_khz);
417 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
418 
419 	new_rule->power_rule.max_antenna_gain =
420 		min_t(u32, rule1->power_rule.max_antenna_gain,
421 		      rule2->power_rule.max_antenna_gain);
422 
423 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
424 					      rule2->power_rule.max_eirp);
425 
426 	/* Use the flags of both the rules */
427 	new_rule->flags = rule1->flags | rule2->flags;
428 
429 	/* To be safe, lts use the max cac timeout of both rules */
430 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
431 				     rule2->dfs_cac_ms);
432 }
433 
434 static struct ieee80211_regdomain *
435 ath12k_regd_intersect(struct ieee80211_regdomain *default_regd,
436 		      struct ieee80211_regdomain *curr_regd)
437 {
438 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
439 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
440 	struct ieee80211_regdomain *new_regd = NULL;
441 	u8 i, j, k;
442 
443 	num_old_regd_rules = default_regd->n_reg_rules;
444 	num_curr_regd_rules = curr_regd->n_reg_rules;
445 	num_new_regd_rules = 0;
446 
447 	/* Find the number of intersecting rules to allocate new regd memory */
448 	for (i = 0; i < num_old_regd_rules; i++) {
449 		old_rule = default_regd->reg_rules + i;
450 		for (j = 0; j < num_curr_regd_rules; j++) {
451 			curr_rule = curr_regd->reg_rules + j;
452 
453 			if (ath12k_reg_can_intersect(old_rule, curr_rule))
454 				num_new_regd_rules++;
455 		}
456 	}
457 
458 	if (!num_new_regd_rules)
459 		return NULL;
460 
461 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
462 			sizeof(struct ieee80211_reg_rule)),
463 			GFP_ATOMIC);
464 
465 	if (!new_regd)
466 		return NULL;
467 
468 	/* We set the new country and dfs region directly and only trim
469 	 * the freq, power, antenna gain by intersecting with the
470 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
471 	 */
472 	new_regd->n_reg_rules = num_new_regd_rules;
473 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
474 	new_regd->dfs_region = curr_regd->dfs_region;
475 	new_rule = new_regd->reg_rules;
476 
477 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
478 		old_rule = default_regd->reg_rules + i;
479 		for (j = 0; j < num_curr_regd_rules; j++) {
480 			curr_rule = curr_regd->reg_rules + j;
481 
482 			if (ath12k_reg_can_intersect(old_rule, curr_rule))
483 				ath12k_reg_intersect_rules(old_rule, curr_rule,
484 							   (new_rule + k++));
485 		}
486 	}
487 	return new_regd;
488 }
489 
490 static const char *
491 ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
492 {
493 	switch (dfs_region) {
494 	case NL80211_DFS_FCC:
495 		return "FCC";
496 	case NL80211_DFS_ETSI:
497 		return "ETSI";
498 	case NL80211_DFS_JP:
499 		return "JP";
500 	default:
501 		return "UNSET";
502 	}
503 }
504 
505 static u16
506 ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
507 {
508 	u16 bw;
509 
510 	bw = end_freq - start_freq;
511 	bw = min_t(u16, bw, max_bw);
512 
513 	if (bw >= 80 && bw < 160)
514 		bw = 80;
515 	else if (bw >= 40 && bw < 80)
516 		bw = 40;
517 	else if (bw < 40)
518 		bw = 20;
519 
520 	return bw;
521 }
522 
523 static void
524 ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
525 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
526 		       u32 reg_flags)
527 {
528 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
529 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
530 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
531 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
532 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
533 	reg_rule->flags = reg_flags;
534 }
535 
536 static void
537 ath12k_reg_update_weather_radar_band(struct ath12k_base *ab,
538 				     struct ieee80211_regdomain *regd,
539 				     struct ath12k_reg_rule *reg_rule,
540 				     u8 *rule_idx, u32 flags, u16 max_bw)
541 {
542 	u32 end_freq;
543 	u16 bw;
544 	u8 i;
545 
546 	i = *rule_idx;
547 
548 	bw = ath12k_reg_adjust_bw(reg_rule->start_freq,
549 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
550 
551 	ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq,
552 			       ETSI_WEATHER_RADAR_BAND_LOW, bw,
553 			       reg_rule->ant_gain, reg_rule->reg_power,
554 			       flags);
555 
556 	ath12k_dbg(ab, ATH12K_DBG_REG,
557 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
558 		   i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW,
559 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
560 		   regd->reg_rules[i].dfs_cac_ms,
561 		   flags);
562 
563 	if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH)
564 		end_freq = ETSI_WEATHER_RADAR_BAND_HIGH;
565 	else
566 		end_freq = reg_rule->end_freq;
567 
568 	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
569 				  max_bw);
570 
571 	i++;
572 
573 	ath12k_reg_update_rule(regd->reg_rules + i,
574 			       ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw,
575 			       reg_rule->ant_gain, reg_rule->reg_power,
576 			       flags);
577 
578 	regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
579 
580 	ath12k_dbg(ab, ATH12K_DBG_REG,
581 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
582 		   i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
583 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
584 		   regd->reg_rules[i].dfs_cac_ms,
585 		   flags);
586 
587 	if (end_freq == reg_rule->end_freq) {
588 		regd->n_reg_rules--;
589 		*rule_idx = i;
590 		return;
591 	}
592 
593 	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
594 				  reg_rule->end_freq, max_bw);
595 
596 	i++;
597 
598 	ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH,
599 			       reg_rule->end_freq, bw,
600 			       reg_rule->ant_gain, reg_rule->reg_power,
601 			       flags);
602 
603 	ath12k_dbg(ab, ATH12K_DBG_REG,
604 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
605 		   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq,
606 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
607 		   regd->reg_rules[i].dfs_cac_ms,
608 		   flags);
609 
610 	*rule_idx = i;
611 }
612 
613 struct ieee80211_regdomain *
614 ath12k_reg_build_regd(struct ath12k_base *ab,
615 		      struct ath12k_reg_info *reg_info, bool intersect)
616 {
617 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
618 	struct ath12k_reg_rule *reg_rule;
619 	u8 i = 0, j = 0, k = 0;
620 	u8 num_rules;
621 	u16 max_bw;
622 	u32 flags;
623 	char alpha2[3];
624 
625 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
626 
627 	/* FIXME: Currently taking reg rules for 6G only from Indoor AP mode list.
628 	 * This can be updated to choose the combination dynamically based on AP
629 	 * type and client type, after complete 6G regulatory support is added.
630 	 */
631 	if (reg_info->is_ext_reg_event)
632 		num_rules += reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP];
633 
634 	if (!num_rules)
635 		goto ret;
636 
637 	/* Add max additional rules to accommodate weather radar band */
638 	if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI)
639 		num_rules += 2;
640 
641 	tmp_regd = kzalloc(sizeof(*tmp_regd) +
642 			   (num_rules * sizeof(struct ieee80211_reg_rule)),
643 			   GFP_ATOMIC);
644 	if (!tmp_regd)
645 		goto ret;
646 
647 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
648 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
649 	alpha2[2] = '\0';
650 	tmp_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region);
651 
652 	ath12k_dbg(ab, ATH12K_DBG_REG,
653 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
654 		   alpha2, ath12k_reg_get_regdom_str(tmp_regd->dfs_region),
655 		   reg_info->dfs_region, num_rules);
656 	/* Update reg_rules[] below. Firmware is expected to
657 	 * send these rules in order(2G rules first and then 5G)
658 	 */
659 	for (; i < num_rules; i++) {
660 		if (reg_info->num_2g_reg_rules &&
661 		    (i < reg_info->num_2g_reg_rules)) {
662 			reg_rule = reg_info->reg_rules_2g_ptr + i;
663 			max_bw = min_t(u16, reg_rule->max_bw,
664 				       reg_info->max_bw_2g);
665 			flags = 0;
666 		} else if (reg_info->num_5g_reg_rules &&
667 			   (j < reg_info->num_5g_reg_rules)) {
668 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
669 			max_bw = min_t(u16, reg_rule->max_bw,
670 				       reg_info->max_bw_5g);
671 
672 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
673 			 * BW Auto correction, we can enable this by default
674 			 * for all 5G rules here. The regulatory core performs
675 			 * BW correction if required and applies flags as
676 			 * per other BW rule flags we pass from here
677 			 */
678 			flags = NL80211_RRF_AUTO_BW;
679 		} else if (reg_info->is_ext_reg_event &&
680 			   reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP] &&
681 			(k < reg_info->num_6g_reg_rules_ap[WMI_REG_INDOOR_AP])) {
682 			reg_rule = reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP] + k++;
683 			max_bw = min_t(u16, reg_rule->max_bw,
684 				       reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP]);
685 			flags = NL80211_RRF_AUTO_BW;
686 		} else {
687 			break;
688 		}
689 
690 		flags |= ath12k_map_fw_reg_flags(reg_rule->flags);
691 		flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap);
692 
693 		ath12k_reg_update_rule(tmp_regd->reg_rules + i,
694 				       reg_rule->start_freq,
695 				       reg_rule->end_freq, max_bw,
696 				       reg_rule->ant_gain, reg_rule->reg_power,
697 				       flags);
698 
699 		/* Update dfs cac timeout if the dfs domain is ETSI and the
700 		 * new rule covers weather radar band.
701 		 * Default value of '0' corresponds to 60s timeout, so no
702 		 * need to update that for other rules.
703 		 */
704 		if (flags & NL80211_RRF_DFS &&
705 		    reg_info->dfs_region == ATH12K_DFS_REG_ETSI &&
706 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
707 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
708 			ath12k_reg_update_weather_radar_band(ab, tmp_regd,
709 							     reg_rule, &i,
710 							     flags, max_bw);
711 			continue;
712 		}
713 
714 		if (reg_info->is_ext_reg_event) {
715 			ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
716 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
717 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
718 				   tmp_regd->reg_rules[i].dfs_cac_ms,
719 				   flags, reg_rule->psd_flag, reg_rule->psd_eirp);
720 		} else {
721 			ath12k_dbg(ab, ATH12K_DBG_REG,
722 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
723 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
724 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
725 				   tmp_regd->reg_rules[i].dfs_cac_ms,
726 				   flags);
727 		}
728 	}
729 
730 	tmp_regd->n_reg_rules = i;
731 
732 	if (intersect) {
733 		default_regd = ab->default_regd[reg_info->phy_id];
734 
735 		/* Get a new regd by intersecting the received regd with
736 		 * our default regd.
737 		 */
738 		new_regd = ath12k_regd_intersect(default_regd, tmp_regd);
739 		kfree(tmp_regd);
740 		if (!new_regd) {
741 			ath12k_warn(ab, "Unable to create intersected regdomain\n");
742 			goto ret;
743 		}
744 	} else {
745 		new_regd = tmp_regd;
746 	}
747 
748 ret:
749 	return new_regd;
750 }
751 
752 void ath12k_regd_update_work(struct work_struct *work)
753 {
754 	struct ath12k *ar = container_of(work, struct ath12k,
755 					 regd_update_work);
756 	int ret;
757 
758 	ret = ath12k_regd_update(ar, false);
759 	if (ret) {
760 		/* Firmware has already moved to the new regd. We need
761 		 * to maintain channel consistency across FW, Host driver
762 		 * and userspace. Hence as a fallback mechanism we can set
763 		 * the prev or default country code to the firmware.
764 		 */
765 		/* TODO: Implement Fallback Mechanism */
766 	}
767 }
768 
769 void ath12k_reg_init(struct ieee80211_hw *hw)
770 {
771 	hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
772 	hw->wiphy->reg_notifier = ath12k_reg_notifier;
773 }
774 
775 void ath12k_reg_free(struct ath12k_base *ab)
776 {
777 	int i;
778 
779 	for (i = 0; i < ab->hw_params->max_radios; i++) {
780 		kfree(ab->default_regd[i]);
781 		kfree(ab->new_regd[i]);
782 	}
783 }
784