xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/reg.c (revision 979ac5ef)
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;
617 	u8 num_rules;
618 	u16 max_bw;
619 	u32 flags;
620 	char alpha2[3];
621 
622 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
623 
624 	if (!num_rules)
625 		goto ret;
626 
627 	/* Add max additional rules to accommodate weather radar band */
628 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
629 		num_rules += 2;
630 
631 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
632 			(num_rules * sizeof(struct ieee80211_reg_rule)),
633 			GFP_ATOMIC);
634 	if (!tmp_regd)
635 		goto ret;
636 
637 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
638 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
639 	alpha2[2] = '\0';
640 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
641 
642 	ath11k_dbg(ab, ATH11K_DBG_REG,
643 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
644 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
645 		   reg_info->dfs_region, num_rules);
646 	/* Update reg_rules[] below. Firmware is expected to
647 	 * send these rules in order(2G rules first and then 5G)
648 	 */
649 	for (; i < num_rules; i++) {
650 		if (reg_info->num_2g_reg_rules &&
651 		    (i < reg_info->num_2g_reg_rules)) {
652 			reg_rule = reg_info->reg_rules_2g_ptr + i;
653 			max_bw = min_t(u16, reg_rule->max_bw,
654 				       reg_info->max_bw_2g);
655 			flags = 0;
656 		} else if (reg_info->num_5g_reg_rules &&
657 			   (j < reg_info->num_5g_reg_rules)) {
658 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
659 			max_bw = min_t(u16, reg_rule->max_bw,
660 				       reg_info->max_bw_5g);
661 
662 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
663 			 * BW Auto correction, we can enable this by default
664 			 * for all 5G rules here. The regulatory core performs
665 			 * BW correction if required and applies flags as
666 			 * per other BW rule flags we pass from here
667 			 */
668 			flags = NL80211_RRF_AUTO_BW;
669 		} else {
670 			break;
671 		}
672 
673 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
674 
675 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
676 				       reg_rule->start_freq,
677 				       reg_rule->end_freq, max_bw,
678 				       reg_rule->ant_gain, reg_rule->reg_power,
679 				       flags);
680 
681 		/* Update dfs cac timeout if the dfs domain is ETSI and the
682 		 * new rule covers weather radar band.
683 		 * Default value of '0' corresponds to 60s timeout, so no
684 		 * need to update that for other rules.
685 		 */
686 		if (flags & NL80211_RRF_DFS &&
687 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
688 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
689 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
690 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
691 							     reg_rule, &i,
692 							     flags, max_bw);
693 			continue;
694 		}
695 
696 		ath11k_dbg(ab, ATH11K_DBG_REG,
697 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
698 			   i + 1, reg_rule->start_freq, reg_rule->end_freq,
699 			   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
700 			   tmp_regd->reg_rules[i].dfs_cac_ms,
701 			   flags);
702 	}
703 
704 	tmp_regd->n_reg_rules = i;
705 
706 	if (intersect) {
707 		default_regd = ab->default_regd[reg_info->phy_id];
708 
709 		/* Get a new regd by intersecting the received regd with
710 		 * our default regd.
711 		 */
712 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
713 		kfree(tmp_regd);
714 		if (!new_regd) {
715 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
716 			goto ret;
717 		}
718 	} else {
719 		new_regd = tmp_regd;
720 	}
721 
722 ret:
723 	return new_regd;
724 }
725 
726 void ath11k_regd_update_work(struct work_struct *work)
727 {
728 	struct ath11k *ar = container_of(work, struct ath11k,
729 					 regd_update_work);
730 	int ret;
731 
732 	ret = ath11k_regd_update(ar);
733 	if (ret) {
734 		/* Firmware has already moved to the new regd. We need
735 		 * to maintain channel consistency across FW, Host driver
736 		 * and userspace. Hence as a fallback mechanism we can set
737 		 * the prev or default country code to the firmware.
738 		 */
739 		/* TODO: Implement Fallback Mechanism */
740 	}
741 }
742 
743 void ath11k_reg_init(struct ath11k *ar)
744 {
745 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
746 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
747 }
748 
749 void ath11k_reg_free(struct ath11k_base *ab)
750 {
751 	int i;
752 
753 	for (i = 0; i < ab->hw_params.max_radios; i++) {
754 		kfree(ab->default_regd[i]);
755 		kfree(ab->new_regd[i]);
756 	}
757 }
758