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