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