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