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