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