xref: /linux/drivers/net/wireless/ath/ath11k/reg.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
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 			   "reg 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 			   "reg 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 bool
356 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
357 			 struct ieee80211_reg_rule *rule2)
358 {
359 	u32 start_freq1, end_freq1;
360 	u32 start_freq2, end_freq2;
361 
362 	start_freq1 = rule1->freq_range.start_freq_khz;
363 	start_freq2 = rule2->freq_range.start_freq_khz;
364 
365 	end_freq1 = rule1->freq_range.end_freq_khz;
366 	end_freq2 = rule2->freq_range.end_freq_khz;
367 
368 	if ((start_freq1 >= start_freq2 &&
369 	     start_freq1 < end_freq2) ||
370 	    (start_freq2 > start_freq1 &&
371 	     start_freq2 < end_freq1))
372 		return true;
373 
374 	/* TODO: Should we restrict intersection feasibility
375 	 *  based on min bandwidth of the intersected region also,
376 	 *  say the intersected rule should have a  min bandwidth
377 	 * of 20MHz?
378 	 */
379 
380 	return false;
381 }
382 
383 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
384 				       struct ieee80211_reg_rule *rule2,
385 				       struct ieee80211_reg_rule *new_rule)
386 {
387 	u32 start_freq1, end_freq1;
388 	u32 start_freq2, end_freq2;
389 	u32 freq_diff, max_bw;
390 
391 	start_freq1 = rule1->freq_range.start_freq_khz;
392 	start_freq2 = rule2->freq_range.start_freq_khz;
393 
394 	end_freq1 = rule1->freq_range.end_freq_khz;
395 	end_freq2 = rule2->freq_range.end_freq_khz;
396 
397 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
398 						    start_freq2);
399 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
400 
401 	freq_diff = new_rule->freq_range.end_freq_khz -
402 			new_rule->freq_range.start_freq_khz;
403 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
404 		       rule2->freq_range.max_bandwidth_khz);
405 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
406 
407 	new_rule->power_rule.max_antenna_gain =
408 		min_t(u32, rule1->power_rule.max_antenna_gain,
409 		      rule2->power_rule.max_antenna_gain);
410 
411 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
412 					      rule2->power_rule.max_eirp);
413 
414 	/* Use the flags of both the rules */
415 	new_rule->flags = rule1->flags | rule2->flags;
416 
417 	/* To be safe, lts use the max cac timeout of both rules */
418 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
419 				     rule2->dfs_cac_ms);
420 }
421 
422 static struct ieee80211_regdomain *
423 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
424 		      struct ieee80211_regdomain *curr_regd)
425 {
426 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
427 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
428 	struct ieee80211_regdomain *new_regd = NULL;
429 	u8 i, j, k;
430 
431 	num_old_regd_rules = default_regd->n_reg_rules;
432 	num_curr_regd_rules = curr_regd->n_reg_rules;
433 	num_new_regd_rules = 0;
434 
435 	/* Find the number of intersecting rules to allocate new regd memory */
436 	for (i = 0; i < num_old_regd_rules; i++) {
437 		old_rule = default_regd->reg_rules + i;
438 		for (j = 0; j < num_curr_regd_rules; j++) {
439 			curr_rule = curr_regd->reg_rules + j;
440 
441 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
442 				num_new_regd_rules++;
443 		}
444 	}
445 
446 	if (!num_new_regd_rules)
447 		return NULL;
448 
449 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
450 			sizeof(struct ieee80211_reg_rule)),
451 			GFP_ATOMIC);
452 
453 	if (!new_regd)
454 		return NULL;
455 
456 	/* We set the new country and dfs region directly and only trim
457 	 * the freq, power, antenna gain by intersecting with the
458 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
459 	 */
460 	new_regd->n_reg_rules = num_new_regd_rules;
461 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
462 	new_regd->dfs_region = curr_regd->dfs_region;
463 	new_rule = new_regd->reg_rules;
464 
465 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
466 		old_rule = default_regd->reg_rules + i;
467 		for (j = 0; j < num_curr_regd_rules; j++) {
468 			curr_rule = curr_regd->reg_rules + j;
469 
470 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
471 				ath11k_reg_intersect_rules(old_rule, curr_rule,
472 							   (new_rule + k++));
473 		}
474 	}
475 	return new_regd;
476 }
477 
478 static const char *
479 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
480 {
481 	switch (dfs_region) {
482 	case NL80211_DFS_FCC:
483 		return "FCC";
484 	case NL80211_DFS_ETSI:
485 		return "ETSI";
486 	case NL80211_DFS_JP:
487 		return "JP";
488 	default:
489 		return "UNSET";
490 	}
491 }
492 
493 static u16
494 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
495 {
496 	u16 bw;
497 
498 	if (end_freq <= start_freq)
499 		return 0;
500 
501 	bw = end_freq - start_freq;
502 	bw = min_t(u16, bw, max_bw);
503 
504 	if (bw >= 80 && bw < 160)
505 		bw = 80;
506 	else if (bw >= 40 && bw < 80)
507 		bw = 40;
508 	else if (bw >= 20 && bw < 40)
509 		bw = 20;
510 	else
511 		bw = 0;
512 
513 	return bw;
514 }
515 
516 static void
517 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
518 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
519 		       u32 reg_flags)
520 {
521 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
522 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
523 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
524 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
525 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
526 	reg_rule->flags = reg_flags;
527 }
528 
529 static void
530 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
531 				     struct ieee80211_regdomain *regd,
532 				     struct cur_reg_rule *reg_rule,
533 				     u8 *rule_idx, u32 flags, u16 max_bw)
534 {
535 	u32 start_freq;
536 	u32 end_freq;
537 	u16 bw;
538 	u8 i;
539 
540 	i = *rule_idx;
541 
542 	/* there might be situations when even the input rule must be dropped */
543 	i--;
544 
545 	/* frequencies below weather radar */
546 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
547 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
548 	if (bw > 0) {
549 		i++;
550 
551 		ath11k_reg_update_rule(regd->reg_rules + i,
552 				       reg_rule->start_freq,
553 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
554 				       reg_rule->ant_gain, reg_rule->reg_power,
555 				       flags);
556 
557 		ath11k_dbg(ab, ATH11K_DBG_REG,
558 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
559 			   i + 1, reg_rule->start_freq,
560 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
561 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
562 			   flags);
563 	}
564 
565 	/* weather radar frequencies */
566 	start_freq = max_t(u32, reg_rule->start_freq,
567 			   ETSI_WEATHER_RADAR_BAND_LOW);
568 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
569 
570 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
571 	if (bw > 0) {
572 		i++;
573 
574 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
575 				       end_freq, bw, reg_rule->ant_gain,
576 				       reg_rule->reg_power, flags);
577 
578 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
579 
580 		ath11k_dbg(ab, ATH11K_DBG_REG,
581 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
582 			   i + 1, start_freq, end_freq, bw,
583 			   reg_rule->ant_gain, reg_rule->reg_power,
584 			   regd->reg_rules[i].dfs_cac_ms, flags);
585 	}
586 
587 	/* frequencies above weather radar */
588 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
589 				  reg_rule->end_freq, max_bw);
590 	if (bw > 0) {
591 		i++;
592 
593 		ath11k_reg_update_rule(regd->reg_rules + i,
594 				       ETSI_WEATHER_RADAR_BAND_HIGH,
595 				       reg_rule->end_freq, bw,
596 				       reg_rule->ant_gain, reg_rule->reg_power,
597 				       flags);
598 
599 		ath11k_dbg(ab, ATH11K_DBG_REG,
600 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
601 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
602 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
603 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
604 			   flags);
605 	}
606 
607 	*rule_idx = i;
608 }
609 
610 struct ieee80211_regdomain *
611 ath11k_reg_build_regd(struct ath11k_base *ab,
612 		      struct cur_regulatory_info *reg_info, bool intersect)
613 {
614 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
615 	struct cur_reg_rule *reg_rule;
616 	u8 i = 0, j = 0, k = 0;
617 	u8 num_rules;
618 	u16 max_bw;
619 	u32 flags;
620 	char alpha2[3];
621 
622 	num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
623 
624 	/* FIXME: Currently taking reg rules for 6 GHz only from Indoor AP mode list.
625 	 * This can be updated after complete 6 GHz regulatory support is added.
626 	 */
627 	if (reg_info->is_ext_reg_event)
628 		num_rules += reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
629 
630 	if (!num_rules)
631 		goto ret;
632 
633 	/* Add max additional rules to accommodate weather radar band */
634 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
635 		num_rules += 2;
636 
637 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
638 			(num_rules * sizeof(struct ieee80211_reg_rule)),
639 			GFP_ATOMIC);
640 	if (!tmp_regd)
641 		goto ret;
642 
643 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
644 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
645 	alpha2[2] = '\0';
646 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
647 
648 	ath11k_dbg(ab, ATH11K_DBG_REG,
649 		   "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
650 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
651 		   reg_info->dfs_region, num_rules);
652 	/* Update reg_rules[] below. Firmware is expected to
653 	 * send these rules in order(2 GHz rules first and then 5 GHz)
654 	 */
655 	for (; i < num_rules; i++) {
656 		if (reg_info->num_2ghz_reg_rules &&
657 		    (i < reg_info->num_2ghz_reg_rules)) {
658 			reg_rule = reg_info->reg_rules_2ghz_ptr + i;
659 			max_bw = min_t(u16, reg_rule->max_bw,
660 				       reg_info->max_bw_2ghz);
661 			flags = 0;
662 		} else if (reg_info->num_5ghz_reg_rules &&
663 			   (j < reg_info->num_5ghz_reg_rules)) {
664 			reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
665 			max_bw = min_t(u16, reg_rule->max_bw,
666 				       reg_info->max_bw_5ghz);
667 
668 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
669 			 * BW Auto correction, we can enable this by default
670 			 * for all 5G rules here. The regulatory core performs
671 			 * BW correction if required and applies flags as
672 			 * per other BW rule flags we pass from here
673 			 */
674 			flags = NL80211_RRF_AUTO_BW;
675 		} else if (reg_info->is_ext_reg_event &&
676 			   reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP] &&
677 			   (k < reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP])) {
678 			reg_rule = reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP] +
679 				   k++;
680 			max_bw = min_t(u16, reg_rule->max_bw,
681 				       reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP]);
682 			flags = NL80211_RRF_AUTO_BW;
683 		} else {
684 			break;
685 		}
686 
687 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
688 
689 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
690 				       reg_rule->start_freq,
691 				       reg_rule->end_freq, max_bw,
692 				       reg_rule->ant_gain, reg_rule->reg_power,
693 				       flags);
694 
695 		/* Update dfs cac timeout if the dfs domain is ETSI and the
696 		 * new rule covers weather radar band.
697 		 * Default value of '0' corresponds to 60s timeout, so no
698 		 * need to update that for other rules.
699 		 */
700 		if (flags & NL80211_RRF_DFS &&
701 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
702 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
703 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
704 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
705 							     reg_rule, &i,
706 							     flags, max_bw);
707 			continue;
708 		}
709 
710 		if (reg_info->is_ext_reg_event) {
711 			ath11k_dbg(ab, ATH11K_DBG_REG,
712 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
713 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
714 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
715 				   tmp_regd->reg_rules[i].dfs_cac_ms, flags,
716 				   reg_rule->psd_flag, reg_rule->psd_eirp);
717 		} else {
718 			ath11k_dbg(ab, ATH11K_DBG_REG,
719 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
720 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
721 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
722 				   tmp_regd->reg_rules[i].dfs_cac_ms,
723 				   flags);
724 		}
725 	}
726 
727 	tmp_regd->n_reg_rules = i;
728 
729 	if (intersect) {
730 		default_regd = ab->default_regd[reg_info->phy_id];
731 
732 		/* Get a new regd by intersecting the received regd with
733 		 * our default regd.
734 		 */
735 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
736 		kfree(tmp_regd);
737 		if (!new_regd) {
738 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
739 			goto ret;
740 		}
741 	} else {
742 		new_regd = tmp_regd;
743 	}
744 
745 ret:
746 	return new_regd;
747 }
748 
749 void ath11k_regd_update_work(struct work_struct *work)
750 {
751 	struct ath11k *ar = container_of(work, struct ath11k,
752 					 regd_update_work);
753 	int ret;
754 
755 	ret = ath11k_regd_update(ar);
756 	if (ret) {
757 		/* Firmware has already moved to the new regd. We need
758 		 * to maintain channel consistency across FW, Host driver
759 		 * and userspace. Hence as a fallback mechanism we can set
760 		 * the prev or default country code to the firmware.
761 		 */
762 		/* TODO: Implement Fallback Mechanism */
763 	}
764 }
765 
766 void ath11k_reg_init(struct ath11k *ar)
767 {
768 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
769 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
770 }
771 
772 void ath11k_reg_free(struct ath11k_base *ab)
773 {
774 	int i;
775 
776 	for (i = 0; i < ab->hw_params.max_radios; i++) {
777 		kfree(ab->default_regd[i]);
778 		kfree(ab->new_regd[i]);
779 	}
780 }
781