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