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