xref: /linux/drivers/net/wireless/ath/ath11k/reg.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 #include <linux/rtnetlink.h>
7 
8 #include "core.h"
9 #include "debug.h"
10 
11 /* World regdom to be used in case default regd from fw is unavailable */
12 #define ATH11K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
13 #define ATH11K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
14 					  NL80211_RRF_NO_IR)
15 #define ATH11K_5GHZ_5725_5850    REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
16 					  NL80211_RRF_NO_IR)
17 
18 #define ETSI_WEATHER_RADAR_BAND_LOW		5590
19 #define ETSI_WEATHER_RADAR_BAND_HIGH		5650
20 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT	600000
21 
22 static const struct ieee80211_regdomain ath11k_world_regd = {
23 	.n_reg_rules = 3,
24 	.alpha2 =  "00",
25 	.reg_rules = {
26 		ATH11K_2GHZ_CH01_11,
27 		ATH11K_5GHZ_5150_5350,
28 		ATH11K_5GHZ_5725_5850,
29 	}
30 };
31 
32 static bool ath11k_regdom_changes(struct ath11k *ar, char *alpha2)
33 {
34 	const struct ieee80211_regdomain *regd;
35 
36 	regd = rcu_dereference_rtnl(ar->hw->wiphy->regd);
37 	/* This can happen during wiphy registration where the previous
38 	 * user request is received before we update the regd received
39 	 * from firmware.
40 	 */
41 	if (!regd)
42 		return true;
43 
44 	return memcmp(regd->alpha2, alpha2, 2) != 0;
45 }
46 
47 static void
48 ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
49 {
50 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
51 	struct wmi_init_country_params init_country_param;
52 	struct wmi_set_current_country_params set_current_param = {};
53 	struct ath11k *ar = hw->priv;
54 	int ret;
55 
56 	ath11k_dbg(ar->ab, ATH11K_DBG_REG,
57 		   "Regulatory Notification received for %s\n", wiphy_name(wiphy));
58 
59 	/* Currently supporting only General User Hints. Cell base user
60 	 * hints to be handled later.
61 	 * Hints from other sources like Core, Beacons are not expected for
62 	 * self managed wiphy's
63 	 */
64 	if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
65 	      request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
66 		ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
67 		return;
68 	}
69 
70 	if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
71 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
72 			   "Country Setting is not allowed\n");
73 		return;
74 	}
75 
76 	if (!ath11k_regdom_changes(ar, request->alpha2)) {
77 		ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
78 		return;
79 	}
80 
81 	/* Set the country code to the firmware and will receive
82 	 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
83 	 * reg info
84 	 */
85 	if (ar->ab->hw_params.current_cc_support) {
86 		memcpy(&set_current_param.alpha2, request->alpha2, 2);
87 		memcpy(&ar->alpha2, &set_current_param.alpha2, 2);
88 		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
89 		if (ret)
90 			ath11k_warn(ar->ab,
91 				    "failed set current country code: %d\n", ret);
92 	} else {
93 		init_country_param.flags = ALPHA_IS_SET;
94 		memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
95 		init_country_param.cc_info.alpha2[2] = 0;
96 
97 		ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
98 		if (ret)
99 			ath11k_warn(ar->ab,
100 				    "INIT Country code set to fw failed : %d\n", ret);
101 	}
102 
103 	ath11k_mac_11d_scan_stop(ar);
104 	ar->regdom_set_by_user = true;
105 }
106 
107 int ath11k_reg_update_chan_list(struct ath11k *ar, bool wait)
108 {
109 	struct ieee80211_supported_band **bands;
110 	struct scan_chan_list_params *params;
111 	struct ieee80211_channel *channel;
112 	struct ieee80211_hw *hw = ar->hw;
113 	struct channel_param *ch;
114 	enum nl80211_band band;
115 	int num_channels = 0;
116 	int i, ret, left;
117 
118 	if (wait && ar->state_11d != ATH11K_11D_IDLE) {
119 		left = wait_for_completion_timeout(&ar->completed_11d_scan,
120 						   ATH11K_SCAN_TIMEOUT_HZ);
121 		if (!left) {
122 			ath11k_dbg(ar->ab, ATH11K_DBG_REG,
123 				   "failed to receive 11d scan complete: timed out\n");
124 			ar->state_11d = ATH11K_11D_IDLE;
125 		}
126 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
127 			   "11d scan wait left time %d\n", left);
128 	}
129 
130 	if (wait &&
131 	    (ar->scan.state == ATH11K_SCAN_STARTING ||
132 	    ar->scan.state == ATH11K_SCAN_RUNNING)) {
133 		left = wait_for_completion_timeout(&ar->scan.completed,
134 						   ATH11K_SCAN_TIMEOUT_HZ);
135 		if (!left)
136 			ath11k_dbg(ar->ab, ATH11K_DBG_REG,
137 				   "failed to receive hw scan complete: timed out\n");
138 
139 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
140 			   "hw scan wait left time %d\n", left);
141 	}
142 
143 	if (ar->state == ATH11K_STATE_RESTARTING)
144 		return 0;
145 
146 	bands = hw->wiphy->bands;
147 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
148 		if (!bands[band])
149 			continue;
150 
151 		for (i = 0; i < bands[band]->n_channels; i++) {
152 			if (bands[band]->channels[i].flags &
153 			    IEEE80211_CHAN_DISABLED)
154 				continue;
155 
156 			num_channels++;
157 		}
158 	}
159 
160 	if (WARN_ON(!num_channels))
161 		return -EINVAL;
162 
163 	params = kzalloc(struct_size(params, ch_param, num_channels),
164 			 GFP_KERNEL);
165 	if (!params)
166 		return -ENOMEM;
167 
168 	params->pdev_id = ar->pdev->pdev_id;
169 	params->nallchans = num_channels;
170 
171 	ch = params->ch_param;
172 
173 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
174 		if (!bands[band])
175 			continue;
176 
177 		for (i = 0; i < bands[band]->n_channels; i++) {
178 			channel = &bands[band]->channels[i];
179 
180 			if (channel->flags & IEEE80211_CHAN_DISABLED)
181 				continue;
182 
183 			/* TODO: Set to true/false based on some condition? */
184 			ch->allow_ht = true;
185 			ch->allow_vht = true;
186 			ch->allow_he = true;
187 
188 			ch->dfs_set =
189 				!!(channel->flags & IEEE80211_CHAN_RADAR);
190 			ch->is_chan_passive = !!(channel->flags &
191 						IEEE80211_CHAN_NO_IR);
192 			ch->is_chan_passive |= ch->dfs_set;
193 			ch->mhz = channel->center_freq;
194 			ch->cfreq1 = channel->center_freq;
195 			ch->minpower = 0;
196 			ch->maxpower = channel->max_power * 2;
197 			ch->maxregpower = channel->max_reg_power * 2;
198 			ch->antennamax = channel->max_antenna_gain * 2;
199 
200 			/* TODO: Use appropriate phymodes */
201 			if (channel->band == NL80211_BAND_2GHZ)
202 				ch->phy_mode = MODE_11G;
203 			else
204 				ch->phy_mode = MODE_11A;
205 
206 			if (channel->band == NL80211_BAND_6GHZ &&
207 			    cfg80211_channel_is_psc(channel))
208 				ch->psc_channel = true;
209 
210 			ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
211 				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
212 				   i, params->nallchans,
213 				   ch->mhz, ch->maxpower, ch->maxregpower,
214 				   ch->antennamax, ch->phy_mode);
215 
216 			ch++;
217 			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
218 			 * set_agile, reg_class_idx
219 			 */
220 		}
221 	}
222 
223 	ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
224 	kfree(params);
225 
226 	return ret;
227 }
228 
229 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
230 			     struct ieee80211_regdomain *regd_copy)
231 {
232 	u8 i;
233 
234 	/* The caller should have checked error conditions */
235 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
236 
237 	for (i = 0; i < regd_orig->n_reg_rules; i++)
238 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
239 		       sizeof(struct ieee80211_reg_rule));
240 }
241 
242 int ath11k_regd_update(struct ath11k *ar)
243 {
244 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
245 	int ret, regd_len, pdev_id;
246 	struct ath11k_base *ab;
247 
248 	ab = ar->ab;
249 	pdev_id = ar->pdev_idx;
250 
251 	spin_lock_bh(&ab->base_lock);
252 
253 	/* Prefer the latest regd update over default if it's available */
254 	if (ab->new_regd[pdev_id]) {
255 		regd = ab->new_regd[pdev_id];
256 	} else {
257 		/* Apply the regd received during init through
258 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
259 		 * receive the regd, initialize with a default world
260 		 * regulatory.
261 		 */
262 		if (ab->default_regd[pdev_id]) {
263 			regd = ab->default_regd[pdev_id];
264 		} else {
265 			ath11k_warn(ab,
266 				    "failed to receive default regd during init\n");
267 			regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
268 		}
269 	}
270 
271 	if (!regd) {
272 		ret = -EINVAL;
273 		spin_unlock_bh(&ab->base_lock);
274 		goto err;
275 	}
276 
277 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
278 		sizeof(struct ieee80211_reg_rule));
279 
280 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
281 	if (regd_copy)
282 		ath11k_copy_regd(regd, regd_copy);
283 
284 	spin_unlock_bh(&ab->base_lock);
285 
286 	if (!regd_copy) {
287 		ret = -ENOMEM;
288 		goto err;
289 	}
290 
291 	ret = regulatory_set_wiphy_regd(ar->hw->wiphy, regd_copy);
292 
293 	kfree(regd_copy);
294 
295 	if (ret)
296 		goto err;
297 
298 	if (ar->state == ATH11K_STATE_ON) {
299 		ret = ath11k_reg_update_chan_list(ar, true);
300 		if (ret)
301 			goto err;
302 	}
303 
304 	return 0;
305 err:
306 	ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
307 	return ret;
308 }
309 
310 static enum nl80211_dfs_regions
311 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
312 {
313 	switch (dfs_region) {
314 	case ATH11K_DFS_REG_FCC:
315 	case ATH11K_DFS_REG_CN:
316 		return NL80211_DFS_FCC;
317 	case ATH11K_DFS_REG_ETSI:
318 	case ATH11K_DFS_REG_KR:
319 		return NL80211_DFS_ETSI;
320 	case ATH11K_DFS_REG_MKK:
321 	case ATH11K_DFS_REG_MKK_N:
322 		return NL80211_DFS_JP;
323 	default:
324 		return NL80211_DFS_UNSET;
325 	}
326 }
327 
328 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
329 {
330 	u32 flags = 0;
331 
332 	if (reg_flags & REGULATORY_CHAN_NO_IR)
333 		flags = NL80211_RRF_NO_IR;
334 
335 	if (reg_flags & REGULATORY_CHAN_RADAR)
336 		flags |= NL80211_RRF_DFS;
337 
338 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
339 		flags |= NL80211_RRF_NO_OFDM;
340 
341 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
342 		flags |= NL80211_RRF_NO_OUTDOOR;
343 
344 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
345 		flags |= NL80211_RRF_NO_HT40;
346 
347 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
348 		flags |= NL80211_RRF_NO_80MHZ;
349 
350 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
351 		flags |= NL80211_RRF_NO_160MHZ;
352 
353 	return flags;
354 }
355 
356 static u32 ath11k_map_fw_phy_flags(u32 phy_flags)
357 {
358 	u32 flags = 0;
359 
360 	if (phy_flags & ATH11K_REG_PHY_BITMAP_NO11AX)
361 		flags |= NL80211_RRF_NO_HE;
362 
363 	return flags;
364 }
365 
366 static bool
367 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
368 			 struct ieee80211_reg_rule *rule2)
369 {
370 	u32 start_freq1, end_freq1;
371 	u32 start_freq2, end_freq2;
372 
373 	start_freq1 = rule1->freq_range.start_freq_khz;
374 	start_freq2 = rule2->freq_range.start_freq_khz;
375 
376 	end_freq1 = rule1->freq_range.end_freq_khz;
377 	end_freq2 = rule2->freq_range.end_freq_khz;
378 
379 	if ((start_freq1 >= start_freq2 &&
380 	     start_freq1 < end_freq2) ||
381 	    (start_freq2 > start_freq1 &&
382 	     start_freq2 < end_freq1))
383 		return true;
384 
385 	/* TODO: Should we restrict intersection feasibility
386 	 *  based on min bandwidth of the intersected region also,
387 	 *  say the intersected rule should have a  min bandwidth
388 	 * of 20MHz?
389 	 */
390 
391 	return false;
392 }
393 
394 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
395 				       struct ieee80211_reg_rule *rule2,
396 				       struct ieee80211_reg_rule *new_rule)
397 {
398 	u32 start_freq1, end_freq1;
399 	u32 start_freq2, end_freq2;
400 	u32 freq_diff, max_bw;
401 
402 	start_freq1 = rule1->freq_range.start_freq_khz;
403 	start_freq2 = rule2->freq_range.start_freq_khz;
404 
405 	end_freq1 = rule1->freq_range.end_freq_khz;
406 	end_freq2 = rule2->freq_range.end_freq_khz;
407 
408 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
409 						    start_freq2);
410 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
411 
412 	freq_diff = new_rule->freq_range.end_freq_khz -
413 			new_rule->freq_range.start_freq_khz;
414 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
415 		       rule2->freq_range.max_bandwidth_khz);
416 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
417 
418 	new_rule->power_rule.max_antenna_gain =
419 		min_t(u32, rule1->power_rule.max_antenna_gain,
420 		      rule2->power_rule.max_antenna_gain);
421 
422 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
423 					      rule2->power_rule.max_eirp);
424 
425 	/* Use the flags of both the rules */
426 	new_rule->flags = rule1->flags | rule2->flags;
427 
428 	if ((rule1->flags & NL80211_RRF_PSD) && (rule2->flags & NL80211_RRF_PSD))
429 		new_rule->psd = min_t(s8, rule1->psd, rule2->psd);
430 	else
431 		new_rule->flags &= ~NL80211_RRF_PSD;
432 
433 	/* To be safe, lts use the max cac timeout of both rules */
434 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
435 				     rule2->dfs_cac_ms);
436 }
437 
438 static struct ieee80211_regdomain *
439 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
440 		      struct ieee80211_regdomain *curr_regd)
441 {
442 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
443 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
444 	struct ieee80211_regdomain *new_regd = NULL;
445 	u8 i, j, k;
446 
447 	num_old_regd_rules = default_regd->n_reg_rules;
448 	num_curr_regd_rules = curr_regd->n_reg_rules;
449 	num_new_regd_rules = 0;
450 
451 	/* Find the number of intersecting rules to allocate new regd memory */
452 	for (i = 0; i < num_old_regd_rules; i++) {
453 		old_rule = default_regd->reg_rules + i;
454 		for (j = 0; j < num_curr_regd_rules; j++) {
455 			curr_rule = curr_regd->reg_rules + j;
456 
457 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
458 				num_new_regd_rules++;
459 		}
460 	}
461 
462 	if (!num_new_regd_rules)
463 		return NULL;
464 
465 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
466 			sizeof(struct ieee80211_reg_rule)),
467 			GFP_ATOMIC);
468 
469 	if (!new_regd)
470 		return NULL;
471 
472 	/* We set the new country and dfs region directly and only trim
473 	 * the freq, power, antenna gain by intersecting with the
474 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
475 	 */
476 	new_regd->n_reg_rules = num_new_regd_rules;
477 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
478 	new_regd->dfs_region = curr_regd->dfs_region;
479 	new_rule = new_regd->reg_rules;
480 
481 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
482 		old_rule = default_regd->reg_rules + i;
483 		for (j = 0; j < num_curr_regd_rules; j++) {
484 			curr_rule = curr_regd->reg_rules + j;
485 
486 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
487 				ath11k_reg_intersect_rules(old_rule, curr_rule,
488 							   (new_rule + k++));
489 		}
490 	}
491 	return new_regd;
492 }
493 
494 static const char *
495 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
496 {
497 	switch (dfs_region) {
498 	case NL80211_DFS_FCC:
499 		return "FCC";
500 	case NL80211_DFS_ETSI:
501 		return "ETSI";
502 	case NL80211_DFS_JP:
503 		return "JP";
504 	default:
505 		return "UNSET";
506 	}
507 }
508 
509 static u16
510 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
511 {
512 	u16 bw;
513 
514 	if (end_freq <= start_freq)
515 		return 0;
516 
517 	bw = end_freq - start_freq;
518 	bw = min_t(u16, bw, max_bw);
519 
520 	if (bw >= 80 && bw < 160)
521 		bw = 80;
522 	else if (bw >= 40 && bw < 80)
523 		bw = 40;
524 	else if (bw >= 20 && bw < 40)
525 		bw = 20;
526 	else
527 		bw = 0;
528 
529 	return bw;
530 }
531 
532 static void
533 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
534 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
535 		       s8 psd, u32 reg_flags)
536 {
537 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
538 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
539 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
540 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
541 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
542 	reg_rule->psd = psd;
543 	reg_rule->flags = reg_flags;
544 }
545 
546 static void
547 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
548 				     struct ieee80211_regdomain *regd,
549 				     struct cur_reg_rule *reg_rule,
550 				     u8 *rule_idx, u32 flags, u16 max_bw)
551 {
552 	u32 start_freq;
553 	u32 end_freq;
554 	u16 bw;
555 	u8 i;
556 
557 	i = *rule_idx;
558 
559 	/* there might be situations when even the input rule must be dropped */
560 	i--;
561 
562 	/* frequencies below weather radar */
563 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
564 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
565 	if (bw > 0) {
566 		i++;
567 
568 		ath11k_reg_update_rule(regd->reg_rules + i,
569 				       reg_rule->start_freq,
570 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
571 				       reg_rule->ant_gain, reg_rule->reg_power,
572 				       reg_rule->psd_eirp, flags);
573 
574 		ath11k_dbg(ab, ATH11K_DBG_REG,
575 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
576 			   i + 1, reg_rule->start_freq,
577 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
578 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
579 			   flags);
580 	}
581 
582 	/* weather radar frequencies */
583 	start_freq = max_t(u32, reg_rule->start_freq,
584 			   ETSI_WEATHER_RADAR_BAND_LOW);
585 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
586 
587 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
588 	if (bw > 0) {
589 		i++;
590 
591 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
592 				       end_freq, bw, reg_rule->ant_gain,
593 				       reg_rule->reg_power, reg_rule->psd_eirp, flags);
594 
595 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
596 
597 		ath11k_dbg(ab, ATH11K_DBG_REG,
598 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
599 			   i + 1, start_freq, end_freq, bw,
600 			   reg_rule->ant_gain, reg_rule->reg_power,
601 			   regd->reg_rules[i].dfs_cac_ms, flags);
602 	}
603 
604 	/* frequencies above weather radar */
605 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
606 				  reg_rule->end_freq, max_bw);
607 	if (bw > 0) {
608 		i++;
609 
610 		ath11k_reg_update_rule(regd->reg_rules + i,
611 				       ETSI_WEATHER_RADAR_BAND_HIGH,
612 				       reg_rule->end_freq, bw,
613 				       reg_rule->ant_gain, reg_rule->reg_power,
614 				       reg_rule->psd_eirp, flags);
615 
616 		ath11k_dbg(ab, ATH11K_DBG_REG,
617 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
618 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
619 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
620 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
621 			   flags);
622 	}
623 
624 	*rule_idx = i;
625 }
626 
627 enum wmi_reg_6ghz_ap_type
628 ath11k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
629 {
630 	switch (power_type) {
631 	case IEEE80211_REG_LPI_AP:
632 		return WMI_REG_INDOOR_AP;
633 	case IEEE80211_REG_SP_AP:
634 		return WMI_REG_STANDARD_POWER_AP;
635 	case IEEE80211_REG_VLP_AP:
636 		return WMI_REG_VERY_LOW_POWER_AP;
637 	default:
638 		return WMI_REG_MAX_AP_TYPE;
639 	}
640 }
641 
642 struct ieee80211_regdomain *
643 ath11k_reg_build_regd(struct ath11k_base *ab,
644 		      struct cur_regulatory_info *reg_info, bool intersect,
645 		      enum wmi_vdev_type vdev_type,
646 		      enum ieee80211_ap_reg_power power_type)
647 {
648 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
649 	struct cur_reg_rule *reg_rule, *reg_rule_6ghz;
650 	u8 i = 0, j = 0, k = 0;
651 	u8 num_rules;
652 	u16 max_bw;
653 	u32 flags, reg_6ghz_number, max_bw_6ghz;
654 	char alpha2[3];
655 
656 	num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
657 
658 	if (reg_info->is_ext_reg_event) {
659 		if (vdev_type == WMI_VDEV_TYPE_STA) {
660 			enum wmi_reg_6ghz_ap_type ap_type;
661 
662 			ap_type = ath11k_reg_ap_pwr_convert(power_type);
663 
664 			if (ap_type == WMI_REG_MAX_AP_TYPE)
665 				ap_type = WMI_REG_INDOOR_AP;
666 
667 			reg_6ghz_number = reg_info->num_6ghz_rules_client
668 					[ap_type][WMI_REG_DEFAULT_CLIENT];
669 
670 			if (reg_6ghz_number == 0) {
671 				ap_type = WMI_REG_INDOOR_AP;
672 				reg_6ghz_number = reg_info->num_6ghz_rules_client
673 						[ap_type][WMI_REG_DEFAULT_CLIENT];
674 			}
675 
676 			reg_rule_6ghz = reg_info->reg_rules_6ghz_client_ptr
677 					[ap_type][WMI_REG_DEFAULT_CLIENT];
678 			max_bw_6ghz = reg_info->max_bw_6ghz_client
679 					[ap_type][WMI_REG_DEFAULT_CLIENT];
680 		} else {
681 			reg_6ghz_number = reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
682 			reg_rule_6ghz =
683 				reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP];
684 			max_bw_6ghz = reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP];
685 		}
686 
687 		num_rules += reg_6ghz_number;
688 	}
689 
690 	if (!num_rules)
691 		goto ret;
692 
693 	/* Add max additional rules to accommodate weather radar band */
694 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
695 		num_rules += 2;
696 
697 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
698 			(num_rules * sizeof(struct ieee80211_reg_rule)),
699 			GFP_ATOMIC);
700 	if (!tmp_regd)
701 		goto ret;
702 
703 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
704 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
705 	alpha2[2] = '\0';
706 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
707 
708 	ath11k_dbg(ab, ATH11K_DBG_REG,
709 		   "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
710 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
711 		   reg_info->dfs_region, num_rules);
712 	/* Update reg_rules[] below. Firmware is expected to
713 	 * send these rules in order(2 GHz rules first and then 5 GHz)
714 	 */
715 	for (; i < num_rules; i++) {
716 		if (reg_info->num_2ghz_reg_rules &&
717 		    (i < reg_info->num_2ghz_reg_rules)) {
718 			reg_rule = reg_info->reg_rules_2ghz_ptr + i;
719 			max_bw = min_t(u16, reg_rule->max_bw,
720 				       reg_info->max_bw_2ghz);
721 			flags = 0;
722 		} else if (reg_info->num_5ghz_reg_rules &&
723 			   (j < reg_info->num_5ghz_reg_rules)) {
724 			reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
725 			max_bw = min_t(u16, reg_rule->max_bw,
726 				       reg_info->max_bw_5ghz);
727 
728 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
729 			 * BW Auto correction, we can enable this by default
730 			 * for all 5G rules here. The regulatory core performs
731 			 * BW correction if required and applies flags as
732 			 * per other BW rule flags we pass from here
733 			 */
734 			flags = NL80211_RRF_AUTO_BW;
735 		} else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
736 			   k < reg_6ghz_number) {
737 			reg_rule = reg_rule_6ghz + k++;
738 			max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
739 			flags = NL80211_RRF_AUTO_BW;
740 			if (reg_rule->psd_flag)
741 				flags |= NL80211_RRF_PSD;
742 		} else {
743 			break;
744 		}
745 
746 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
747 		flags |= ath11k_map_fw_phy_flags(reg_info->phybitmap);
748 
749 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
750 				       reg_rule->start_freq,
751 				       reg_rule->end_freq, max_bw,
752 				       reg_rule->ant_gain, reg_rule->reg_power,
753 				       reg_rule->psd_eirp, flags);
754 
755 		/* Update dfs cac timeout if the dfs domain is ETSI and the
756 		 * new rule covers weather radar band.
757 		 * Default value of '0' corresponds to 60s timeout, so no
758 		 * need to update that for other rules.
759 		 */
760 		if (flags & NL80211_RRF_DFS &&
761 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
762 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
763 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
764 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
765 							     reg_rule, &i,
766 							     flags, max_bw);
767 			continue;
768 		}
769 
770 		if (reg_info->is_ext_reg_event) {
771 			ath11k_dbg(ab, ATH11K_DBG_REG,
772 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
773 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
774 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
775 				   tmp_regd->reg_rules[i].dfs_cac_ms, flags,
776 				   reg_rule->psd_flag, reg_rule->psd_eirp);
777 		} else {
778 			ath11k_dbg(ab, ATH11K_DBG_REG,
779 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
780 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
781 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
782 				   tmp_regd->reg_rules[i].dfs_cac_ms,
783 				   flags);
784 		}
785 	}
786 
787 	tmp_regd->n_reg_rules = i;
788 
789 	if (intersect) {
790 		default_regd = ab->default_regd[reg_info->phy_id];
791 
792 		/* Get a new regd by intersecting the received regd with
793 		 * our default regd.
794 		 */
795 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
796 		kfree(tmp_regd);
797 		if (!new_regd) {
798 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
799 			goto ret;
800 		}
801 	} else {
802 		new_regd = tmp_regd;
803 	}
804 
805 ret:
806 	return new_regd;
807 }
808 
809 static bool ath11k_reg_is_world_alpha(char *alpha)
810 {
811 	if (alpha[0] == '0' && alpha[1] == '0')
812 		return true;
813 
814 	if (alpha[0] == 'n' && alpha[1] == 'a')
815 		return true;
816 
817 	return false;
818 }
819 
820 static enum wmi_vdev_type ath11k_reg_get_ar_vdev_type(struct ath11k *ar)
821 {
822 	struct ath11k_vif *arvif;
823 
824 	/* Currently each struct ath11k maps to one struct ieee80211_hw/wiphy
825 	 * and one struct ieee80211_regdomain, so it could only store one group
826 	 * reg rules. It means multi-interface concurrency in the same ath11k is
827 	 * not support for the regdomain. So get the vdev type of the first entry
828 	 * now. After concurrency support for the regdomain, this should change.
829 	 */
830 	arvif = list_first_entry_or_null(&ar->arvifs, struct ath11k_vif, list);
831 	if (arvif)
832 		return arvif->vdev_type;
833 
834 	return WMI_VDEV_TYPE_UNSPEC;
835 }
836 
837 int ath11k_reg_handle_chan_list(struct ath11k_base *ab,
838 				struct cur_regulatory_info *reg_info,
839 				enum ieee80211_ap_reg_power power_type)
840 {
841 	struct ieee80211_regdomain *regd;
842 	bool intersect = false;
843 	int pdev_idx;
844 	struct ath11k *ar;
845 	enum wmi_vdev_type vdev_type;
846 
847 	ath11k_dbg(ab, ATH11K_DBG_WMI, "event reg handle chan list");
848 
849 	if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
850 		/* In case of failure to set the requested ctry,
851 		 * fw retains the current regd. We print a failure info
852 		 * and return from here.
853 		 */
854 		ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
855 		return -EINVAL;
856 	}
857 
858 	pdev_idx = reg_info->phy_id;
859 
860 	/* Avoid default reg rule updates sent during FW recovery if
861 	 * it is already available
862 	 */
863 	spin_lock_bh(&ab->base_lock);
864 	if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&
865 	    ab->default_regd[pdev_idx]) {
866 		spin_unlock_bh(&ab->base_lock);
867 		goto retfail;
868 	}
869 	spin_unlock_bh(&ab->base_lock);
870 
871 	if (pdev_idx >= ab->num_radios) {
872 		/* Process the event for phy0 only if single_pdev_only
873 		 * is true. If pdev_idx is valid but not 0, discard the
874 		 * event. Otherwise, it goes to fallback. In either case
875 		 * ath11k_reg_reset_info() needs to be called to avoid
876 		 * memory leak issue.
877 		 */
878 		ath11k_reg_reset_info(reg_info);
879 
880 		if (ab->hw_params.single_pdev_only &&
881 		    pdev_idx < ab->hw_params.num_rxmda_per_pdev)
882 			return 0;
883 		goto fallback;
884 	}
885 
886 	/* Avoid multiple overwrites to default regd, during core
887 	 * stop-start after mac registration.
888 	 */
889 	if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
890 	    !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
891 		    (char *)reg_info->alpha2, 2))
892 		goto retfail;
893 
894 	/* Intersect new rules with default regd if a new country setting was
895 	 * requested, i.e a default regd was already set during initialization
896 	 * and the regd coming from this event has a valid country info.
897 	 */
898 	if (ab->default_regd[pdev_idx] &&
899 	    !ath11k_reg_is_world_alpha((char *)
900 		ab->default_regd[pdev_idx]->alpha2) &&
901 	    !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
902 		intersect = true;
903 
904 	ar = ab->pdevs[pdev_idx].ar;
905 	vdev_type = ath11k_reg_get_ar_vdev_type(ar);
906 
907 	ath11k_dbg(ab, ATH11K_DBG_WMI,
908 		   "wmi handle chan list power type %d vdev type %d intersect %d\n",
909 		   power_type, vdev_type, intersect);
910 
911 	regd = ath11k_reg_build_regd(ab, reg_info, intersect, vdev_type, power_type);
912 	if (!regd) {
913 		ath11k_warn(ab, "failed to build regd from reg_info\n");
914 		goto fallback;
915 	}
916 
917 	if (power_type == IEEE80211_REG_UNSET_AP) {
918 		ath11k_reg_reset_info(&ab->reg_info_store[pdev_idx]);
919 		ab->reg_info_store[pdev_idx] = *reg_info;
920 	}
921 
922 	spin_lock_bh(&ab->base_lock);
923 	if (ab->default_regd[pdev_idx]) {
924 		/* The initial rules from FW after WMI Init is to build
925 		 * the default regd. From then on, any rules updated for
926 		 * the pdev could be due to user reg changes.
927 		 * Free previously built regd before assigning the newly
928 		 * generated regd to ar. NULL pointer handling will be
929 		 * taken care by kfree itself.
930 		 */
931 		ar = ab->pdevs[pdev_idx].ar;
932 		kfree(ab->new_regd[pdev_idx]);
933 		ab->new_regd[pdev_idx] = regd;
934 		queue_work(ab->workqueue, &ar->regd_update_work);
935 	} else {
936 		/* This regd would be applied during mac registration and is
937 		 * held constant throughout for regd intersection purpose
938 		 */
939 		ab->default_regd[pdev_idx] = regd;
940 	}
941 	ab->dfs_region = reg_info->dfs_region;
942 	spin_unlock_bh(&ab->base_lock);
943 
944 	return 0;
945 
946 fallback:
947 	/* Fallback to older reg (by sending previous country setting
948 	 * again if fw has succeeded and we failed to process here.
949 	 * The Regdomain should be uniform across driver and fw. Since the
950 	 * FW has processed the command and sent a success status, we expect
951 	 * this function to succeed as well. If it doesn't, CTRY needs to be
952 	 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
953 	 */
954 	/* TODO: This is rare, but still should also be handled */
955 	WARN_ON(1);
956 
957 retfail:
958 
959 	return -EINVAL;
960 }
961 
962 void ath11k_regd_update_work(struct work_struct *work)
963 {
964 	struct ath11k *ar = container_of(work, struct ath11k,
965 					 regd_update_work);
966 	int ret;
967 
968 	ret = ath11k_regd_update(ar);
969 	if (ret) {
970 		/* Firmware has already moved to the new regd. We need
971 		 * to maintain channel consistency across FW, Host driver
972 		 * and userspace. Hence as a fallback mechanism we can set
973 		 * the prev or default country code to the firmware.
974 		 */
975 		/* TODO: Implement Fallback Mechanism */
976 	}
977 }
978 
979 void ath11k_reg_init(struct ath11k *ar)
980 {
981 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
982 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
983 }
984 
985 void ath11k_reg_reset_info(struct cur_regulatory_info *reg_info)
986 {
987 	int i, j;
988 
989 	if (!reg_info)
990 		return;
991 
992 	kfree(reg_info->reg_rules_2ghz_ptr);
993 	kfree(reg_info->reg_rules_5ghz_ptr);
994 
995 	for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
996 		kfree(reg_info->reg_rules_6ghz_ap_ptr[i]);
997 
998 		for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
999 			kfree(reg_info->reg_rules_6ghz_client_ptr[i][j]);
1000 	}
1001 
1002 	memset(reg_info, 0, sizeof(*reg_info));
1003 }
1004 
1005 void ath11k_reg_free(struct ath11k_base *ab)
1006 {
1007 	int i;
1008 
1009 	for (i = 0; i < ab->num_radios; i++)
1010 		ath11k_reg_reset_info(&ab->reg_info_store[i]);
1011 
1012 	kfree(ab->reg_info_store);
1013 	ab->reg_info_store = NULL;
1014 
1015 	for (i = 0; i < ab->hw_params.max_radios; i++) {
1016 		kfree(ab->default_regd[i]);
1017 		kfree(ab->new_regd[i]);
1018 	}
1019 }
1020