xref: /linux/drivers/net/wireless/ath/ath11k/reg.c (revision f6154d8babbb8a98f0d3ea325aafae2e33bfd8be)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 #include <linux/rtnetlink.h>
6 
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 ATH11K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
12 #define ATH11K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
13 					  NL80211_RRF_NO_IR)
14 #define ATH11K_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 ath11k_world_regd = {
22 	.n_reg_rules = 3,
23 	.alpha2 =  "00",
24 	.reg_rules = {
25 		ATH11K_2GHZ_CH01_11,
26 		ATH11K_5GHZ_5150_5350,
27 		ATH11K_5GHZ_5725_5850,
28 	}
29 };
30 
31 static bool ath11k_regdom_changes(struct ath11k *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 ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
48 {
49 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
50 	struct wmi_init_country_params init_country_param;
51 	struct wmi_set_current_country_params set_current_param = {};
52 	struct ath11k *ar = hw->priv;
53 	int ret;
54 
55 	ath11k_dbg(ar->ab, ATH11K_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 		ath11k_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 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
71 			   "Country Setting is not allowed\n");
72 		return;
73 	}
74 
75 	if (!ath11k_regdom_changes(ar, request->alpha2)) {
76 		ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
77 		return;
78 	}
79 
80 	/* Set the country code to the firmware and will receive
81 	 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
82 	 * reg info
83 	 */
84 	if (ar->ab->hw_params.current_cc_support) {
85 		memcpy(&set_current_param.alpha2, request->alpha2, 2);
86 		memcpy(&ar->alpha2, &set_current_param.alpha2, 2);
87 		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
88 		if (ret)
89 			ath11k_warn(ar->ab,
90 				    "failed set current country code: %d\n", ret);
91 	} else {
92 		init_country_param.flags = ALPHA_IS_SET;
93 		memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
94 		init_country_param.cc_info.alpha2[2] = 0;
95 
96 		ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
97 		if (ret)
98 			ath11k_warn(ar->ab,
99 				    "INIT Country code set to fw failed : %d\n", ret);
100 	}
101 
102 	ath11k_mac_11d_scan_stop(ar);
103 	ar->regdom_set_by_user = true;
104 }
105 
106 int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)
107 {
108 	struct ieee80211_supported_band **bands;
109 	struct scan_chan_list_params *params;
110 	struct ieee80211_channel *channel;
111 	struct ieee80211_hw *hw = ar->hw;
112 	struct channel_param *ch;
113 	enum nl80211_band band;
114 	int num_channels = 0;
115 	int i, ret, left;
116 
117 	if (wait && ar->state_11d != ATH11K_11D_IDLE) {
118 		left = wait_for_completion_timeout(&ar->completed_11d_scan,
119 						   ATH11K_SCAN_TIMEOUT_HZ);
120 		if (!left) {
121 			ath11k_dbg(ar->ab, ATH11K_DBG_REG,
122 				   "failed to receive 11d scan complete: timed out\n");
123 			ar->state_11d = ATH11K_11D_IDLE;
124 		}
125 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
126 			   "11d scan wait left time %d\n", left);
127 	}
128 
129 	if (wait &&
130 	    (ar->scan.state == ATH11K_SCAN_STARTING ||
131 	    ar->scan.state == ATH11K_SCAN_RUNNING)) {
132 		left = wait_for_completion_timeout(&ar->scan.completed,
133 						   ATH11K_SCAN_TIMEOUT_HZ);
134 		if (!left)
135 			ath11k_dbg(ar->ab, ATH11K_DBG_REG,
136 				   "failed to receive hw scan complete: timed out\n");
137 
138 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
139 			   "hw scan wait left time %d\n", left);
140 	}
141 
142 	if (ar->state == ATH11K_STATE_RESTARTING)
143 		return 0;
144 
145 	bands = hw->wiphy->bands;
146 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
147 		if (!bands[band])
148 			continue;
149 
150 		for (i = 0; i < bands[band]->n_channels; i++) {
151 			if (bands[band]->channels[i].flags &
152 			    IEEE80211_CHAN_DISABLED)
153 				continue;
154 
155 			num_channels++;
156 		}
157 	}
158 
159 	if (WARN_ON(!num_channels))
160 		return -EINVAL;
161 
162 	params = kzalloc(struct_size(params, ch_param, num_channels),
163 			 GFP_KERNEL);
164 	if (!params)
165 		return -ENOMEM;
166 
167 	params->pdev_id = ar->pdev->pdev_id;
168 	params->nallchans = num_channels;
169 
170 	ch = params->ch_param;
171 
172 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
173 		if (!bands[band])
174 			continue;
175 
176 		for (i = 0; i < bands[band]->n_channels; i++) {
177 			channel = &bands[band]->channels[i];
178 
179 			if (channel->flags & IEEE80211_CHAN_DISABLED)
180 				continue;
181 
182 			/* TODO: Set to true/false based on some condition? */
183 			ch->allow_ht = true;
184 			ch->allow_vht = true;
185 			ch->allow_he = true;
186 
187 			ch->dfs_set =
188 				!!(channel->flags & IEEE80211_CHAN_RADAR);
189 			ch->is_chan_passive = !!(channel->flags &
190 						IEEE80211_CHAN_NO_IR);
191 			ch->is_chan_passive |= ch->dfs_set;
192 			ch->mhz = channel->center_freq;
193 			ch->cfreq1 = channel->center_freq;
194 			ch->minpower = 0;
195 			ch->maxpower = channel->max_power * 2;
196 			ch->maxregpower = channel->max_reg_power * 2;
197 			ch->antennamax = channel->max_antenna_gain * 2;
198 
199 			/* TODO: Use appropriate phymodes */
200 			if (channel->band == NL80211_BAND_2GHZ)
201 				ch->phy_mode = MODE_11G;
202 			else
203 				ch->phy_mode = MODE_11A;
204 
205 			if (channel->band == NL80211_BAND_6GHZ &&
206 			    cfg80211_channel_is_psc(channel))
207 				ch->psc_channel = true;
208 
209 			ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
210 				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
211 				   i, params->nallchans,
212 				   ch->mhz, ch->maxpower, ch->maxregpower,
213 				   ch->antennamax, ch->phy_mode);
214 
215 			ch++;
216 			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
217 			 * set_agile, reg_class_idx
218 			 */
219 		}
220 	}
221 
222 	ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
223 	kfree(params);
224 
225 	return ret;
226 }
227 
228 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
229 			     struct ieee80211_regdomain *regd_copy)
230 {
231 	u8 i;
232 
233 	/* The caller should have checked error conditions */
234 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
235 
236 	for (i = 0; i < regd_orig->n_reg_rules; i++)
237 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
238 		       sizeof(struct ieee80211_reg_rule));
239 }
240 
241 int ath11k_regd_update(struct ath11k *ar)
242 {
243 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
244 	int ret, regd_len, pdev_id;
245 	struct ath11k_base *ab;
246 
247 	ab = ar->ab;
248 	pdev_id = ar->pdev_idx;
249 
250 	spin_lock_bh(&ab->base_lock);
251 
252 	/* Prefer the latest regd update over default if it's available */
253 	if (ab->new_regd[pdev_id]) {
254 		regd = ab->new_regd[pdev_id];
255 	} else {
256 		/* Apply the regd received during init through
257 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
258 		 * receive the regd, initialize with a default world
259 		 * regulatory.
260 		 */
261 		if (ab->default_regd[pdev_id]) {
262 			regd = ab->default_regd[pdev_id];
263 		} else {
264 			ath11k_warn(ab,
265 				    "failed to receive default regd during init\n");
266 			regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
267 		}
268 	}
269 
270 	if (!regd) {
271 		ret = -EINVAL;
272 		spin_unlock_bh(&ab->base_lock);
273 		goto err;
274 	}
275 
276 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
277 		sizeof(struct ieee80211_reg_rule));
278 
279 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
280 	if (regd_copy)
281 		ath11k_copy_regd(regd, regd_copy);
282 
283 	spin_unlock_bh(&ab->base_lock);
284 
285 	if (!regd_copy) {
286 		ret = -ENOMEM;
287 		goto err;
288 	}
289 
290 	ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);
291 
292 	kfree(regd_copy);
293 
294 	if (ret)
295 		goto err;
296 
297 	if (ar->state == ATH11K_STATE_ON) {
298 		ret = ath11k_reg_update_chan_list(ar, true);
299 		if (ret)
300 			goto err;
301 	}
302 
303 	return 0;
304 err:
305 	ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
306 	return ret;
307 }
308 
309 static enum nl80211_dfs_regions
310 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
311 {
312 	switch (dfs_region) {
313 	case ATH11K_DFS_REG_FCC:
314 	case ATH11K_DFS_REG_CN:
315 		return NL80211_DFS_FCC;
316 	case ATH11K_DFS_REG_ETSI:
317 	case ATH11K_DFS_REG_KR:
318 		return NL80211_DFS_ETSI;
319 	case ATH11K_DFS_REG_MKK:
320 	case ATH11K_DFS_REG_MKK_N:
321 		return NL80211_DFS_JP;
322 	default:
323 		return NL80211_DFS_UNSET;
324 	}
325 }
326 
327 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
328 {
329 	u32 flags = 0;
330 
331 	if (reg_flags & REGULATORY_CHAN_NO_IR)
332 		flags = NL80211_RRF_NO_IR;
333 
334 	if (reg_flags & REGULATORY_CHAN_RADAR)
335 		flags |= NL80211_RRF_DFS;
336 
337 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
338 		flags |= NL80211_RRF_NO_OFDM;
339 
340 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
341 		flags |= NL80211_RRF_NO_OUTDOOR;
342 
343 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
344 		flags |= NL80211_RRF_NO_HT40;
345 
346 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
347 		flags |= NL80211_RRF_NO_80MHZ;
348 
349 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
350 		flags |= NL80211_RRF_NO_160MHZ;
351 
352 	return flags;
353 }
354 
355 static u32 ath11k_map_fw_phy_flags(u32 phy_flags)
356 {
357 	u32 flags = 0;
358 
359 	if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)
360 		flags |= NL80211_RRF_NO_HE;
361 
362 	return flags;
363 }
364 
365 static bool
366 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
367 			 struct ieee80211_reg_rule *rule2)
368 {
369 	u32 start_freq1, end_freq1;
370 	u32 start_freq2, end_freq2;
371 
372 	start_freq1 = rule1->freq_range.start_freq_khz;
373 	start_freq2 = rule2->freq_range.start_freq_khz;
374 
375 	end_freq1 = rule1->freq_range.end_freq_khz;
376 	end_freq2 = rule2->freq_range.end_freq_khz;
377 
378 	if ((start_freq1 >= start_freq2 &&
379 	     start_freq1 < end_freq2) ||
380 	    (start_freq2 > start_freq1 &&
381 	     start_freq2 < end_freq1))
382 		return true;
383 
384 	/* TODO: Should we restrict intersection feasibility
385 	 *  based on min bandwidth of the intersected region also,
386 	 *  say the intersected rule should have a  min bandwidth
387 	 * of 20MHz?
388 	 */
389 
390 	return false;
391 }
392 
393 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
394 				       struct ieee80211_reg_rule *rule2,
395 				       struct ieee80211_reg_rule *new_rule)
396 {
397 	u32 start_freq1, end_freq1;
398 	u32 start_freq2, end_freq2;
399 	u32 freq_diff, max_bw;
400 
401 	start_freq1 = rule1->freq_range.start_freq_khz;
402 	start_freq2 = rule2->freq_range.start_freq_khz;
403 
404 	end_freq1 = rule1->freq_range.end_freq_khz;
405 	end_freq2 = rule2->freq_range.end_freq_khz;
406 
407 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
408 						    start_freq2);
409 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
410 
411 	freq_diff = new_rule->freq_range.end_freq_khz -
412 			new_rule->freq_range.start_freq_khz;
413 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
414 		       rule2->freq_range.max_bandwidth_khz);
415 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
416 
417 	new_rule->power_rule.max_antenna_gain =
418 		min_t(u32, rule1->power_rule.max_antenna_gain,
419 		      rule2->power_rule.max_antenna_gain);
420 
421 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
422 					      rule2->power_rule.max_eirp);
423 
424 	/* Use the flags of both the rules */
425 	new_rule->flags = rule1->flags | rule2->flags;
426 
427 	/* To be safe, lts use the max cac timeout of both rules */
428 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
429 				     rule2->dfs_cac_ms);
430 }
431 
432 static struct ieee80211_regdomain *
433 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
434 		      struct ieee80211_regdomain *curr_regd)
435 {
436 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
437 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
438 	struct ieee80211_regdomain *new_regd = NULL;
439 	u8 i, j, k;
440 
441 	num_old_regd_rules = default_regd->n_reg_rules;
442 	num_curr_regd_rules = curr_regd->n_reg_rules;
443 	num_new_regd_rules = 0;
444 
445 	/* Find the number of intersecting rules to allocate new regd memory */
446 	for (i = 0; i < num_old_regd_rules; i++) {
447 		old_rule = default_regd->reg_rules + i;
448 		for (j = 0; j < num_curr_regd_rules; j++) {
449 			curr_rule = curr_regd->reg_rules + j;
450 
451 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
452 				num_new_regd_rules++;
453 		}
454 	}
455 
456 	if (!num_new_regd_rules)
457 		return NULL;
458 
459 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
460 			sizeof(struct ieee80211_reg_rule)),
461 			GFP_ATOMIC);
462 
463 	if (!new_regd)
464 		return NULL;
465 
466 	/* We set the new country and dfs region directly and only trim
467 	 * the freq, power, antenna gain by intersecting with the
468 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
469 	 */
470 	new_regd->n_reg_rules = num_new_regd_rules;
471 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
472 	new_regd->dfs_region = curr_regd->dfs_region;
473 	new_rule = new_regd->reg_rules;
474 
475 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
476 		old_rule = default_regd->reg_rules + i;
477 		for (j = 0; j < num_curr_regd_rules; j++) {
478 			curr_rule = curr_regd->reg_rules + j;
479 
480 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
481 				ath11k_reg_intersect_rules(old_rule, curr_rule,
482 							   (new_rule + k++));
483 		}
484 	}
485 	return new_regd;
486 }
487 
488 static const char *
489 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
490 {
491 	switch (dfs_region) {
492 	case NL80211_DFS_FCC:
493 		return "FCC";
494 	case NL80211_DFS_ETSI:
495 		return "ETSI";
496 	case NL80211_DFS_JP:
497 		return "JP";
498 	default:
499 		return "UNSET";
500 	}
501 }
502 
503 static u16
504 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
505 {
506 	u16 bw;
507 
508 	if (end_freq <= start_freq)
509 		return 0;
510 
511 	bw = end_freq - start_freq;
512 	bw = min_t(u16, bw, max_bw);
513 
514 	if (bw >= 80 && bw < 160)
515 		bw = 80;
516 	else if (bw >= 40 && bw < 80)
517 		bw = 40;
518 	else if (bw >= 20 && bw < 40)
519 		bw = 20;
520 	else
521 		bw = 0;
522 
523 	return bw;
524 }
525 
526 static void
527 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
528 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
529 		       u32 reg_flags)
530 {
531 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
532 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
533 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
534 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
535 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
536 	reg_rule->flags = reg_flags;
537 }
538 
539 static void
540 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
541 				     struct ieee80211_regdomain *regd,
542 				     struct cur_reg_rule *reg_rule,
543 				     u8 *rule_idx, u32 flags, u16 max_bw)
544 {
545 	u32 start_freq;
546 	u32 end_freq;
547 	u16 bw;
548 	u8 i;
549 
550 	i = *rule_idx;
551 
552 	/* there might be situations when even the input rule must be dropped */
553 	i--;
554 
555 	/* frequencies below weather radar */
556 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
557 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
558 	if (bw > 0) {
559 		i++;
560 
561 		ath11k_reg_update_rule(regd->reg_rules + i,
562 				       reg_rule->start_freq,
563 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
564 				       reg_rule->ant_gain, reg_rule->reg_power,
565 				       flags);
566 
567 		ath11k_dbg(ab, ATH11K_DBG_REG,
568 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
569 			   i + 1, reg_rule->start_freq,
570 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
571 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
572 			   flags);
573 	}
574 
575 	/* weather radar frequencies */
576 	start_freq = max_t(u32, reg_rule->start_freq,
577 			   ETSI_WEATHER_RADAR_BAND_LOW);
578 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
579 
580 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
581 	if (bw > 0) {
582 		i++;
583 
584 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
585 				       end_freq, bw, reg_rule->ant_gain,
586 				       reg_rule->reg_power, flags);
587 
588 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
589 
590 		ath11k_dbg(ab, ATH11K_DBG_REG,
591 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
592 			   i + 1, start_freq, end_freq, bw,
593 			   reg_rule->ant_gain, reg_rule->reg_power,
594 			   regd->reg_rules[i].dfs_cac_ms, flags);
595 	}
596 
597 	/* frequencies above weather radar */
598 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
599 				  reg_rule->end_freq, max_bw);
600 	if (bw > 0) {
601 		i++;
602 
603 		ath11k_reg_update_rule(regd->reg_rules + i,
604 				       ETSI_WEATHER_RADAR_BAND_HIGH,
605 				       reg_rule->end_freq, bw,
606 				       reg_rule->ant_gain, reg_rule->reg_power,
607 				       flags);
608 
609 		ath11k_dbg(ab, ATH11K_DBG_REG,
610 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
611 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
612 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
613 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
614 			   flags);
615 	}
616 
617 	*rule_idx = i;
618 }
619 
620 struct ieee80211_regdomain *
621 ath11k_reg_build_regd(struct ath11k_base *ab,
622 		      struct cur_regulatory_info *reg_info, bool intersect)
623 {
624 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
625 	struct cur_reg_rule *reg_rule;
626 	u8 i = 0, j = 0, k = 0;
627 	u8 num_rules;
628 	u16 max_bw;
629 	u32 flags;
630 	char alpha2[3];
631 
632 	num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
633 
634 	/* FIXME: Currently taking reg rules for 6 GHz only from Indoor AP mode list.
635 	 * This can be updated after complete 6 GHz regulatory support is added.
636 	 */
637 	if (reg_info->is_ext_reg_event)
638 		num_rules += reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
639 
640 	if (!num_rules)
641 		goto ret;
642 
643 	/* Add max additional rules to accommodate weather radar band */
644 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
645 		num_rules += 2;
646 
647 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
648 			(num_rules * sizeof(struct ieee80211_reg_rule)),
649 			GFP_ATOMIC);
650 	if (!tmp_regd)
651 		goto ret;
652 
653 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
654 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
655 	alpha2[2] = '\0';
656 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
657 
658 	ath11k_dbg(ab, ATH11K_DBG_REG,
659 		   "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
660 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
661 		   reg_info->dfs_region, num_rules);
662 	/* Update reg_rules[] below. Firmware is expected to
663 	 * send these rules in order(2 GHz rules first and then 5 GHz)
664 	 */
665 	for (; i < num_rules; i++) {
666 		if (reg_info->num_2ghz_reg_rules &&
667 		    (i < reg_info->num_2ghz_reg_rules)) {
668 			reg_rule = reg_info->reg_rules_2ghz_ptr + i;
669 			max_bw = min_t(u16, reg_rule->max_bw,
670 				       reg_info->max_bw_2ghz);
671 			flags = 0;
672 		} else if (reg_info->num_5ghz_reg_rules &&
673 			   (j < reg_info->num_5ghz_reg_rules)) {
674 			reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
675 			max_bw = min_t(u16, reg_rule->max_bw,
676 				       reg_info->max_bw_5ghz);
677 
678 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
679 			 * BW Auto correction, we can enable this by default
680 			 * for all 5G rules here. The regulatory core performs
681 			 * BW correction if required and applies flags as
682 			 * per other BW rule flags we pass from here
683 			 */
684 			flags = NL80211_RRF_AUTO_BW;
685 		} else if (reg_info->is_ext_reg_event &&
686 			   reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP] &&
687 			   (k < reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP])) {
688 			reg_rule = reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP] +
689 				   k++;
690 			max_bw = min_t(u16, reg_rule->max_bw,
691 				       reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP]);
692 			flags = NL80211_RRF_AUTO_BW;
693 		} else {
694 			break;
695 		}
696 
697 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
698 		flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);
699 
700 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
701 				       reg_rule->start_freq,
702 				       reg_rule->end_freq, max_bw,
703 				       reg_rule->ant_gain, reg_rule->reg_power,
704 				       flags);
705 
706 		/* Update dfs cac timeout if the dfs domain is ETSI and the
707 		 * new rule covers weather radar band.
708 		 * Default value of '0' corresponds to 60s timeout, so no
709 		 * need to update that for other rules.
710 		 */
711 		if (flags & NL80211_RRF_DFS &&
712 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
713 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
714 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
715 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
716 							     reg_rule, &i,
717 							     flags, max_bw);
718 			continue;
719 		}
720 
721 		if (reg_info->is_ext_reg_event) {
722 			ath11k_dbg(ab, ATH11K_DBG_REG,
723 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
724 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
725 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
726 				   tmp_regd->reg_rules[i].dfs_cac_ms, flags,
727 				   reg_rule->psd_flag, reg_rule->psd_eirp);
728 		} else {
729 			ath11k_dbg(ab, ATH11K_DBG_REG,
730 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
731 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
732 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
733 				   tmp_regd->reg_rules[i].dfs_cac_ms,
734 				   flags);
735 		}
736 	}
737 
738 	tmp_regd->n_reg_rules = i;
739 
740 	if (intersect) {
741 		default_regd = ab->default_regd[reg_info->phy_id];
742 
743 		/* Get a new regd by intersecting the received regd with
744 		 * our default regd.
745 		 */
746 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
747 		kfree(tmp_regd);
748 		if (!new_regd) {
749 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
750 			goto ret;
751 		}
752 	} else {
753 		new_regd = tmp_regd;
754 	}
755 
756 ret:
757 	return new_regd;
758 }
759 
760 void ath11k_regd_update_work(struct work_struct *work)
761 {
762 	struct ath11k *ar = container_of(work, struct ath11k,
763 					 regd_update_work);
764 	int ret;
765 
766 	ret = ath11k_regd_update(ar);
767 	if (ret) {
768 		/* Firmware has already moved to the new regd. We need
769 		 * to maintain channel consistency across FW, Host driver
770 		 * and userspace. Hence as a fallback mechanism we can set
771 		 * the prev or default country code to the firmware.
772 		 */
773 		/* TODO: Implement Fallback Mechanism */
774 	}
775 }
776 
777 void ath11k_reg_init(struct ath11k *ar)
778 {
779 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
780 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
781 }
782 
783 void ath11k_reg_free(struct ath11k_base *ab)
784 {
785 	int i;
786 
787 	for (i = 0; i < ab->hw_params.max_radios; i++) {
788 		kfree(ab->default_regd[i]);
789 		kfree(ab->new_regd[i]);
790 	}
791 }
792