xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/reg.c (revision 55fd7e02)
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 			ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
165 				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
166 				   i, params->nallchans,
167 				   ch->mhz, ch->maxpower, ch->maxregpower,
168 				   ch->antennamax, ch->phy_mode);
169 
170 			ch++;
171 			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
172 			 * set_agile, reg_class_idx
173 			 */
174 		}
175 	}
176 
177 	ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
178 	kfree(params);
179 
180 	return ret;
181 }
182 
183 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
184 			     struct ieee80211_regdomain *regd_copy)
185 {
186 	u8 i;
187 
188 	/* The caller should have checked error conditions */
189 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
190 
191 	for (i = 0; i < regd_orig->n_reg_rules; i++)
192 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
193 		       sizeof(struct ieee80211_reg_rule));
194 }
195 
196 int ath11k_regd_update(struct ath11k *ar, bool init)
197 {
198 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
199 	int ret, regd_len, pdev_id;
200 	struct ath11k_base *ab;
201 
202 	ab = ar->ab;
203 	pdev_id = ar->pdev_idx;
204 
205 	spin_lock(&ab->base_lock);
206 
207 	if (init) {
208 		/* Apply the regd received during init through
209 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
210 		 * receive the regd, initialize with a default world
211 		 * regulatory.
212 		 */
213 		if (ab->default_regd[pdev_id]) {
214 			regd = ab->default_regd[pdev_id];
215 		} else {
216 			ath11k_warn(ab,
217 				    "failed to receive default regd during init\n");
218 			regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
219 		}
220 	} else {
221 		regd = ab->new_regd[pdev_id];
222 	}
223 
224 	if (!regd) {
225 		ret = -EINVAL;
226 		spin_unlock(&ab->base_lock);
227 		goto err;
228 	}
229 
230 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
231 		sizeof(struct ieee80211_reg_rule));
232 
233 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
234 	if (regd_copy)
235 		ath11k_copy_regd(regd, regd_copy);
236 
237 	spin_unlock(&ab->base_lock);
238 
239 	if (!regd_copy) {
240 		ret = -ENOMEM;
241 		goto err;
242 	}
243 
244 	rtnl_lock();
245 	ret = regulatory_set_wiphy_regd_sync_rtnl(ar->hw->wiphy, regd_copy);
246 	rtnl_unlock();
247 
248 	kfree(regd_copy);
249 
250 	if (ret)
251 		goto err;
252 
253 	if (ar->state == ATH11K_STATE_ON) {
254 		ret = ath11k_reg_update_chan_list(ar);
255 		if (ret)
256 			goto err;
257 	}
258 
259 	return 0;
260 err:
261 	ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
262 	return ret;
263 }
264 
265 static enum nl80211_dfs_regions
266 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
267 {
268 	switch (dfs_region) {
269 	case ATH11K_DFS_REG_FCC:
270 	case ATH11K_DFS_REG_CN:
271 		return NL80211_DFS_FCC;
272 	case ATH11K_DFS_REG_ETSI:
273 	case ATH11K_DFS_REG_KR:
274 		return NL80211_DFS_ETSI;
275 	case ATH11K_DFS_REG_MKK:
276 		return NL80211_DFS_JP;
277 	default:
278 		return NL80211_DFS_UNSET;
279 	}
280 }
281 
282 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
283 {
284 	u32 flags = 0;
285 
286 	if (reg_flags & REGULATORY_CHAN_NO_IR)
287 		flags = NL80211_RRF_NO_IR;
288 
289 	if (reg_flags & REGULATORY_CHAN_RADAR)
290 		flags |= NL80211_RRF_DFS;
291 
292 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
293 		flags |= NL80211_RRF_NO_OFDM;
294 
295 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
296 		flags |= NL80211_RRF_NO_OUTDOOR;
297 
298 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
299 		flags |= NL80211_RRF_NO_HT40;
300 
301 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
302 		flags |= NL80211_RRF_NO_80MHZ;
303 
304 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
305 		flags |= NL80211_RRF_NO_160MHZ;
306 
307 	return flags;
308 }
309 
310 static bool
311 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
312 			 struct ieee80211_reg_rule *rule2)
313 {
314 	u32 start_freq1, end_freq1;
315 	u32 start_freq2, end_freq2;
316 
317 	start_freq1 = rule1->freq_range.start_freq_khz;
318 	start_freq2 = rule2->freq_range.start_freq_khz;
319 
320 	end_freq1 = rule1->freq_range.end_freq_khz;
321 	end_freq2 = rule2->freq_range.end_freq_khz;
322 
323 	if ((start_freq1 >= start_freq2 &&
324 	     start_freq1 < end_freq2) ||
325 	    (start_freq2 > start_freq1 &&
326 	     start_freq2 < end_freq1))
327 		return true;
328 
329 	/* TODO: Should we restrict intersection feasibility
330 	 *  based on min bandwidth of the intersected region also,
331 	 *  say the intersected rule should have a  min bandwidth
332 	 * of 20MHz?
333 	 */
334 
335 	return false;
336 }
337 
338 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
339 				       struct ieee80211_reg_rule *rule2,
340 				       struct ieee80211_reg_rule *new_rule)
341 {
342 	u32 start_freq1, end_freq1;
343 	u32 start_freq2, end_freq2;
344 	u32 freq_diff, max_bw;
345 
346 	start_freq1 = rule1->freq_range.start_freq_khz;
347 	start_freq2 = rule2->freq_range.start_freq_khz;
348 
349 	end_freq1 = rule1->freq_range.end_freq_khz;
350 	end_freq2 = rule2->freq_range.end_freq_khz;
351 
352 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
353 						    start_freq2);
354 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
355 
356 	freq_diff = new_rule->freq_range.end_freq_khz -
357 			new_rule->freq_range.start_freq_khz;
358 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
359 		       rule2->freq_range.max_bandwidth_khz);
360 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
361 
362 	new_rule->power_rule.max_antenna_gain =
363 		min_t(u32, rule1->power_rule.max_antenna_gain,
364 		      rule2->power_rule.max_antenna_gain);
365 
366 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
367 					      rule2->power_rule.max_eirp);
368 
369 	/* Use the flags of both the rules */
370 	new_rule->flags = rule1->flags | rule2->flags;
371 
372 	/* To be safe, lts use the max cac timeout of both rules */
373 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
374 				     rule2->dfs_cac_ms);
375 }
376 
377 static struct ieee80211_regdomain *
378 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
379 		      struct ieee80211_regdomain *curr_regd)
380 {
381 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
382 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
383 	struct ieee80211_regdomain *new_regd = NULL;
384 	u8 i, j, k;
385 
386 	num_old_regd_rules = default_regd->n_reg_rules;
387 	num_curr_regd_rules = curr_regd->n_reg_rules;
388 	num_new_regd_rules = 0;
389 
390 	/* Find the number of intersecting rules to allocate new regd memory */
391 	for (i = 0; i < num_old_regd_rules; i++) {
392 		old_rule = default_regd->reg_rules + i;
393 		for (j = 0; j < num_curr_regd_rules; j++) {
394 			curr_rule = curr_regd->reg_rules + j;
395 
396 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
397 				num_new_regd_rules++;
398 		}
399 	}
400 
401 	if (!num_new_regd_rules)
402 		return NULL;
403 
404 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
405 			sizeof(struct ieee80211_reg_rule)),
406 			GFP_ATOMIC);
407 
408 	if (!new_regd)
409 		return NULL;
410 
411 	/* We set the new country and dfs region directly and only trim
412 	 * the freq, power, antenna gain by intersecting with the
413 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
414 	 */
415 	new_regd->n_reg_rules = num_new_regd_rules;
416 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
417 	new_regd->dfs_region = curr_regd->dfs_region;
418 	new_rule = new_regd->reg_rules;
419 
420 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
421 		old_rule = default_regd->reg_rules + i;
422 		for (j = 0; j < num_curr_regd_rules; j++) {
423 			curr_rule = curr_regd->reg_rules + j;
424 
425 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
426 				ath11k_reg_intersect_rules(old_rule, curr_rule,
427 							   (new_rule + k++));
428 		}
429 	}
430 	return new_regd;
431 }
432 
433 static const char *
434 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
435 {
436 	switch (dfs_region) {
437 	case NL80211_DFS_FCC:
438 		return "FCC";
439 	case NL80211_DFS_ETSI:
440 		return "ETSI";
441 	case NL80211_DFS_JP:
442 		return "JP";
443 	default:
444 		return "UNSET";
445 	}
446 }
447 
448 static u16
449 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
450 {
451 	u16 bw;
452 
453 	bw = end_freq - start_freq;
454 	bw = min_t(u16, bw, max_bw);
455 
456 	if (bw >= 80 && bw < 160)
457 		bw = 80;
458 	else if (bw >= 40 && bw < 80)
459 		bw = 40;
460 	else if (bw < 40)
461 		bw = 20;
462 
463 	return bw;
464 }
465 
466 static void
467 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
468 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
469 		       u32 reg_flags)
470 {
471 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
472 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
473 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
474 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
475 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
476 	reg_rule->flags = reg_flags;
477 }
478 
479 static void
480 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
481 				     struct ieee80211_regdomain *regd,
482 				     struct cur_reg_rule *reg_rule,
483 				     u8 *rule_idx, u32 flags, u16 max_bw)
484 {
485 	u32 end_freq;
486 	u16 bw;
487 	u8 i;
488 
489 	i = *rule_idx;
490 
491 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
492 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
493 
494 	ath11k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq,
495 			       ETSI_WEATHER_RADAR_BAND_LOW, bw,
496 			       reg_rule->ant_gain, reg_rule->reg_power,
497 			       flags);
498 
499 	ath11k_dbg(ab, ATH11K_DBG_REG,
500 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
501 		   i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW,
502 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
503 		   regd->reg_rules[i].dfs_cac_ms,
504 		   flags);
505 
506 	if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH)
507 		end_freq = ETSI_WEATHER_RADAR_BAND_HIGH;
508 	else
509 		end_freq = reg_rule->end_freq;
510 
511 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
512 				  max_bw);
513 
514 	i++;
515 
516 	ath11k_reg_update_rule(regd->reg_rules + i,
517 			       ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw,
518 			       reg_rule->ant_gain, reg_rule->reg_power,
519 			       flags);
520 
521 	regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
522 
523 	ath11k_dbg(ab, ATH11K_DBG_REG,
524 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
525 		   i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
526 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
527 		   regd->reg_rules[i].dfs_cac_ms,
528 		   flags);
529 
530 	if (end_freq == reg_rule->end_freq) {
531 		regd->n_reg_rules--;
532 		*rule_idx = i;
533 		return;
534 	}
535 
536 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
537 				  reg_rule->end_freq, max_bw);
538 
539 	i++;
540 
541 	ath11k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH,
542 			       reg_rule->end_freq, bw,
543 			       reg_rule->ant_gain, reg_rule->reg_power,
544 			       flags);
545 
546 	ath11k_dbg(ab, ATH11K_DBG_REG,
547 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
548 		   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq,
549 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
550 		   regd->reg_rules[i].dfs_cac_ms,
551 		   flags);
552 
553 	*rule_idx = i;
554 }
555 
556 struct ieee80211_regdomain *
557 ath11k_reg_build_regd(struct ath11k_base *ab,
558 		      struct cur_regulatory_info *reg_info, bool intersect)
559 {
560 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
561 	struct cur_reg_rule *reg_rule;
562 	u8 i = 0, j = 0;
563 	u8 num_rules;
564 	u16 max_bw;
565 	u32 flags;
566 	char alpha2[3];
567 
568 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
569 
570 	if (!num_rules)
571 		goto ret;
572 
573 	/* Add max additional rules to accommodate weather radar band */
574 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
575 		num_rules += 2;
576 
577 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
578 			(num_rules * sizeof(struct ieee80211_reg_rule)),
579 			GFP_ATOMIC);
580 	if (!tmp_regd)
581 		goto ret;
582 
583 	tmp_regd->n_reg_rules = num_rules;
584 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
585 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
586 	alpha2[2] = '\0';
587 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
588 
589 	ath11k_dbg(ab, ATH11K_DBG_REG,
590 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
591 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
592 		   reg_info->dfs_region, num_rules);
593 	/* Update reg_rules[] below. Firmware is expected to
594 	 * send these rules in order(2G rules first and then 5G)
595 	 */
596 	for (; i < tmp_regd->n_reg_rules; i++) {
597 		if (reg_info->num_2g_reg_rules &&
598 		    (i < reg_info->num_2g_reg_rules)) {
599 			reg_rule = reg_info->reg_rules_2g_ptr + i;
600 			max_bw = min_t(u16, reg_rule->max_bw,
601 				       reg_info->max_bw_2g);
602 			flags = 0;
603 		} else if (reg_info->num_5g_reg_rules &&
604 			   (j < reg_info->num_5g_reg_rules)) {
605 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
606 			max_bw = min_t(u16, reg_rule->max_bw,
607 				       reg_info->max_bw_5g);
608 
609 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
610 			 * BW Auto correction, we can enable this by default
611 			 * for all 5G rules here. The regulatory core performs
612 			 * BW correction if required and applies flags as
613 			 * per other BW rule flags we pass from here
614 			 */
615 			flags = NL80211_RRF_AUTO_BW;
616 		} else {
617 			break;
618 		}
619 
620 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
621 
622 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
623 				       reg_rule->start_freq,
624 				       reg_rule->end_freq, max_bw,
625 				       reg_rule->ant_gain, reg_rule->reg_power,
626 				       flags);
627 
628 		/* Update dfs cac timeout if the dfs domain is ETSI and the
629 		 * new rule covers weather radar band.
630 		 * Default value of '0' corresponds to 60s timeout, so no
631 		 * need to update that for other rules.
632 		 */
633 		if (flags & NL80211_RRF_DFS &&
634 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
635 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
636 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
637 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
638 							     reg_rule, &i,
639 							     flags, max_bw);
640 			continue;
641 		}
642 
643 		ath11k_dbg(ab, ATH11K_DBG_REG,
644 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
645 			   i + 1, reg_rule->start_freq, reg_rule->end_freq,
646 			   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
647 			   tmp_regd->reg_rules[i].dfs_cac_ms,
648 			   flags);
649 	}
650 
651 	if (intersect) {
652 		default_regd = ab->default_regd[reg_info->phy_id];
653 
654 		/* Get a new regd by intersecting the received regd with
655 		 * our default regd.
656 		 */
657 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
658 		kfree(tmp_regd);
659 		if (!new_regd) {
660 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
661 			goto ret;
662 		}
663 	} else {
664 		new_regd = tmp_regd;
665 	}
666 
667 ret:
668 	return new_regd;
669 }
670 
671 void ath11k_regd_update_work(struct work_struct *work)
672 {
673 	struct ath11k *ar = container_of(work, struct ath11k,
674 					 regd_update_work);
675 	int ret;
676 
677 	ret = ath11k_regd_update(ar, false);
678 	if (ret) {
679 		/* Firmware has already moved to the new regd. We need
680 		 * to maintain channel consistency across FW, Host driver
681 		 * and userspace. Hence as a fallback mechanism we can set
682 		 * the prev or default country code to the firmware.
683 		 */
684 		/* TODO: Implement Fallback Mechanism */
685 	}
686 }
687 
688 void ath11k_reg_init(struct ath11k *ar)
689 {
690 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
691 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
692 }
693 
694 void ath11k_reg_free(struct ath11k_base *ab)
695 {
696 	int i;
697 
698 	for (i = 0; i < MAX_RADIOS; i++) {
699 		kfree(ab->default_regd[i]);
700 		kfree(ab->new_regd[i]);
701 	}
702 }
703