xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/reg.c (revision 586c7fb1)
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 
ath11k_regdom_changes(struct ath11k * ar,char * alpha2)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
ath11k_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)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 
ath11k_reg_update_chan_list(struct ath11k * ar,bool wait)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 
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 			     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 
ath11k_regd_update(struct ath11k * ar)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
ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)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 
ath11k_map_fw_reg_flags(u16 reg_flags)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 bool
ath11k_reg_can_intersect(struct ieee80211_reg_rule * rule1,struct ieee80211_reg_rule * rule2)357 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
358 			 struct ieee80211_reg_rule *rule2)
359 {
360 	u32 start_freq1, end_freq1;
361 	u32 start_freq2, end_freq2;
362 
363 	start_freq1 = rule1->freq_range.start_freq_khz;
364 	start_freq2 = rule2->freq_range.start_freq_khz;
365 
366 	end_freq1 = rule1->freq_range.end_freq_khz;
367 	end_freq2 = rule2->freq_range.end_freq_khz;
368 
369 	if ((start_freq1 >= start_freq2 &&
370 	     start_freq1 < end_freq2) ||
371 	    (start_freq2 > start_freq1 &&
372 	     start_freq2 < end_freq1))
373 		return true;
374 
375 	/* TODO: Should we restrict intersection feasibility
376 	 *  based on min bandwidth of the intersected region also,
377 	 *  say the intersected rule should have a  min bandwidth
378 	 * of 20MHz?
379 	 */
380 
381 	return false;
382 }
383 
ath11k_reg_intersect_rules(struct ieee80211_reg_rule * rule1,struct ieee80211_reg_rule * rule2,struct ieee80211_reg_rule * new_rule)384 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
385 				       struct ieee80211_reg_rule *rule2,
386 				       struct ieee80211_reg_rule *new_rule)
387 {
388 	u32 start_freq1, end_freq1;
389 	u32 start_freq2, end_freq2;
390 	u32 freq_diff, max_bw;
391 
392 	start_freq1 = rule1->freq_range.start_freq_khz;
393 	start_freq2 = rule2->freq_range.start_freq_khz;
394 
395 	end_freq1 = rule1->freq_range.end_freq_khz;
396 	end_freq2 = rule2->freq_range.end_freq_khz;
397 
398 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
399 						    start_freq2);
400 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
401 
402 	freq_diff = new_rule->freq_range.end_freq_khz -
403 			new_rule->freq_range.start_freq_khz;
404 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
405 		       rule2->freq_range.max_bandwidth_khz);
406 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
407 
408 	new_rule->power_rule.max_antenna_gain =
409 		min_t(u32, rule1->power_rule.max_antenna_gain,
410 		      rule2->power_rule.max_antenna_gain);
411 
412 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
413 					      rule2->power_rule.max_eirp);
414 
415 	/* Use the flags of both the rules */
416 	new_rule->flags = rule1->flags | rule2->flags;
417 
418 	/* To be safe, lts use the max cac timeout of both rules */
419 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
420 				     rule2->dfs_cac_ms);
421 }
422 
423 static struct ieee80211_regdomain *
ath11k_regd_intersect(struct ieee80211_regdomain * default_regd,struct ieee80211_regdomain * curr_regd)424 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
425 		      struct ieee80211_regdomain *curr_regd)
426 {
427 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
428 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
429 	struct ieee80211_regdomain *new_regd = NULL;
430 	u8 i, j, k;
431 
432 	num_old_regd_rules = default_regd->n_reg_rules;
433 	num_curr_regd_rules = curr_regd->n_reg_rules;
434 	num_new_regd_rules = 0;
435 
436 	/* Find the number of intersecting rules to allocate new regd memory */
437 	for (i = 0; i < num_old_regd_rules; i++) {
438 		old_rule = default_regd->reg_rules + i;
439 		for (j = 0; j < num_curr_regd_rules; j++) {
440 			curr_rule = curr_regd->reg_rules + j;
441 
442 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
443 				num_new_regd_rules++;
444 		}
445 	}
446 
447 	if (!num_new_regd_rules)
448 		return NULL;
449 
450 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
451 			sizeof(struct ieee80211_reg_rule)),
452 			GFP_ATOMIC);
453 
454 	if (!new_regd)
455 		return NULL;
456 
457 	/* We set the new country and dfs region directly and only trim
458 	 * the freq, power, antenna gain by intersecting with the
459 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
460 	 */
461 	new_regd->n_reg_rules = num_new_regd_rules;
462 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
463 	new_regd->dfs_region = curr_regd->dfs_region;
464 	new_rule = new_regd->reg_rules;
465 
466 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
467 		old_rule = default_regd->reg_rules + i;
468 		for (j = 0; j < num_curr_regd_rules; j++) {
469 			curr_rule = curr_regd->reg_rules + j;
470 
471 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
472 				ath11k_reg_intersect_rules(old_rule, curr_rule,
473 							   (new_rule + k++));
474 		}
475 	}
476 	return new_regd;
477 }
478 
479 static const char *
ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)480 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
481 {
482 	switch (dfs_region) {
483 	case NL80211_DFS_FCC:
484 		return "FCC";
485 	case NL80211_DFS_ETSI:
486 		return "ETSI";
487 	case NL80211_DFS_JP:
488 		return "JP";
489 	default:
490 		return "UNSET";
491 	}
492 }
493 
494 static u16
ath11k_reg_adjust_bw(u16 start_freq,u16 end_freq,u16 max_bw)495 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
496 {
497 	u16 bw;
498 
499 	if (end_freq <= start_freq)
500 		return 0;
501 
502 	bw = end_freq - start_freq;
503 	bw = min_t(u16, bw, max_bw);
504 
505 	if (bw >= 80 && bw < 160)
506 		bw = 80;
507 	else if (bw >= 40 && bw < 80)
508 		bw = 40;
509 	else if (bw >= 20 && bw < 40)
510 		bw = 20;
511 	else
512 		bw = 0;
513 
514 	return bw;
515 }
516 
517 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)518 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
519 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
520 		       u32 reg_flags)
521 {
522 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
523 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
524 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
525 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
526 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
527 	reg_rule->flags = reg_flags;
528 }
529 
530 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)531 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
532 				     struct ieee80211_regdomain *regd,
533 				     struct cur_reg_rule *reg_rule,
534 				     u8 *rule_idx, u32 flags, u16 max_bw)
535 {
536 	u32 start_freq;
537 	u32 end_freq;
538 	u16 bw;
539 	u8 i;
540 
541 	i = *rule_idx;
542 
543 	/* there might be situations when even the input rule must be dropped */
544 	i--;
545 
546 	/* frequencies below weather radar */
547 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
548 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
549 	if (bw > 0) {
550 		i++;
551 
552 		ath11k_reg_update_rule(regd->reg_rules + i,
553 				       reg_rule->start_freq,
554 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
555 				       reg_rule->ant_gain, reg_rule->reg_power,
556 				       flags);
557 
558 		ath11k_dbg(ab, ATH11K_DBG_REG,
559 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
560 			   i + 1, reg_rule->start_freq,
561 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
562 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
563 			   flags);
564 	}
565 
566 	/* weather radar frequencies */
567 	start_freq = max_t(u32, reg_rule->start_freq,
568 			   ETSI_WEATHER_RADAR_BAND_LOW);
569 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
570 
571 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
572 	if (bw > 0) {
573 		i++;
574 
575 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
576 				       end_freq, bw, reg_rule->ant_gain,
577 				       reg_rule->reg_power, flags);
578 
579 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
580 
581 		ath11k_dbg(ab, ATH11K_DBG_REG,
582 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
583 			   i + 1, start_freq, end_freq, bw,
584 			   reg_rule->ant_gain, reg_rule->reg_power,
585 			   regd->reg_rules[i].dfs_cac_ms, flags);
586 	}
587 
588 	/* frequencies above weather radar */
589 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
590 				  reg_rule->end_freq, max_bw);
591 	if (bw > 0) {
592 		i++;
593 
594 		ath11k_reg_update_rule(regd->reg_rules + i,
595 				       ETSI_WEATHER_RADAR_BAND_HIGH,
596 				       reg_rule->end_freq, bw,
597 				       reg_rule->ant_gain, reg_rule->reg_power,
598 				       flags);
599 
600 		ath11k_dbg(ab, ATH11K_DBG_REG,
601 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
602 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
603 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
604 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
605 			   flags);
606 	}
607 
608 	*rule_idx = i;
609 }
610 
611 struct ieee80211_regdomain *
ath11k_reg_build_regd(struct ath11k_base * ab,struct cur_regulatory_info * reg_info,bool intersect)612 ath11k_reg_build_regd(struct ath11k_base *ab,
613 		      struct cur_regulatory_info *reg_info, bool intersect)
614 {
615 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
616 	struct cur_reg_rule *reg_rule;
617 	u8 i = 0, j = 0, k = 0;
618 	u8 num_rules;
619 	u16 max_bw;
620 	u32 flags;
621 	char alpha2[3];
622 
623 	num_rules = reg_info->num_5ghz_reg_rules + reg_info->num_2ghz_reg_rules;
624 
625 	/* FIXME: Currently taking reg rules for 6 GHz only from Indoor AP mode list.
626 	 * This can be updated after complete 6 GHz regulatory support is added.
627 	 */
628 	if (reg_info->is_ext_reg_event)
629 		num_rules += reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP];
630 
631 	if (!num_rules)
632 		goto ret;
633 
634 	/* Add max additional rules to accommodate weather radar band */
635 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
636 		num_rules += 2;
637 
638 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
639 			(num_rules * sizeof(struct ieee80211_reg_rule)),
640 			GFP_ATOMIC);
641 	if (!tmp_regd)
642 		goto ret;
643 
644 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
645 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
646 	alpha2[2] = '\0';
647 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
648 
649 	ath11k_dbg(ab, ATH11K_DBG_REG,
650 		   "Country %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
651 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
652 		   reg_info->dfs_region, num_rules);
653 	/* Update reg_rules[] below. Firmware is expected to
654 	 * send these rules in order(2 GHz rules first and then 5 GHz)
655 	 */
656 	for (; i < num_rules; i++) {
657 		if (reg_info->num_2ghz_reg_rules &&
658 		    (i < reg_info->num_2ghz_reg_rules)) {
659 			reg_rule = reg_info->reg_rules_2ghz_ptr + i;
660 			max_bw = min_t(u16, reg_rule->max_bw,
661 				       reg_info->max_bw_2ghz);
662 			flags = 0;
663 		} else if (reg_info->num_5ghz_reg_rules &&
664 			   (j < reg_info->num_5ghz_reg_rules)) {
665 			reg_rule = reg_info->reg_rules_5ghz_ptr + j++;
666 			max_bw = min_t(u16, reg_rule->max_bw,
667 				       reg_info->max_bw_5ghz);
668 
669 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
670 			 * BW Auto correction, we can enable this by default
671 			 * for all 5G rules here. The regulatory core performs
672 			 * BW correction if required and applies flags as
673 			 * per other BW rule flags we pass from here
674 			 */
675 			flags = NL80211_RRF_AUTO_BW;
676 		} else if (reg_info->is_ext_reg_event &&
677 			   reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP] &&
678 			   (k < reg_info->num_6ghz_rules_ap[WMI_REG_INDOOR_AP])) {
679 			reg_rule = reg_info->reg_rules_6ghz_ap_ptr[WMI_REG_INDOOR_AP] +
680 				   k++;
681 			max_bw = min_t(u16, reg_rule->max_bw,
682 				       reg_info->max_bw_6ghz_ap[WMI_REG_INDOOR_AP]);
683 			flags = NL80211_RRF_AUTO_BW;
684 		} else {
685 			break;
686 		}
687 
688 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
689 
690 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
691 				       reg_rule->start_freq,
692 				       reg_rule->end_freq, max_bw,
693 				       reg_rule->ant_gain, reg_rule->reg_power,
694 				       flags);
695 
696 		/* Update dfs cac timeout if the dfs domain is ETSI and the
697 		 * new rule covers weather radar band.
698 		 * Default value of '0' corresponds to 60s timeout, so no
699 		 * need to update that for other rules.
700 		 */
701 		if (flags & NL80211_RRF_DFS &&
702 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
703 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
704 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
705 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
706 							     reg_rule, &i,
707 							     flags, max_bw);
708 			continue;
709 		}
710 
711 		if (reg_info->is_ext_reg_event) {
712 			ath11k_dbg(ab, ATH11K_DBG_REG,
713 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
714 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
715 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
716 				   tmp_regd->reg_rules[i].dfs_cac_ms, flags,
717 				   reg_rule->psd_flag, reg_rule->psd_eirp);
718 		} else {
719 			ath11k_dbg(ab, ATH11K_DBG_REG,
720 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
721 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
722 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
723 				   tmp_regd->reg_rules[i].dfs_cac_ms,
724 				   flags);
725 		}
726 	}
727 
728 	tmp_regd->n_reg_rules = i;
729 
730 	if (intersect) {
731 		default_regd = ab->default_regd[reg_info->phy_id];
732 
733 		/* Get a new regd by intersecting the received regd with
734 		 * our default regd.
735 		 */
736 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
737 		kfree(tmp_regd);
738 		if (!new_regd) {
739 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
740 			goto ret;
741 		}
742 	} else {
743 		new_regd = tmp_regd;
744 	}
745 
746 ret:
747 	return new_regd;
748 }
749 
ath11k_regd_update_work(struct work_struct * work)750 void ath11k_regd_update_work(struct work_struct *work)
751 {
752 	struct ath11k *ar = container_of(work, struct ath11k,
753 					 regd_update_work);
754 	int ret;
755 
756 	ret = ath11k_regd_update(ar);
757 	if (ret) {
758 		/* Firmware has already moved to the new regd. We need
759 		 * to maintain channel consistency across FW, Host driver
760 		 * and userspace. Hence as a fallback mechanism we can set
761 		 * the prev or default country code to the firmware.
762 		 */
763 		/* TODO: Implement Fallback Mechanism */
764 	}
765 }
766 
ath11k_reg_init(struct ath11k * ar)767 void ath11k_reg_init(struct ath11k *ar)
768 {
769 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
770 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
771 }
772 
ath11k_reg_free(struct ath11k_base * ab)773 void ath11k_reg_free(struct ath11k_base *ab)
774 {
775 	int i;
776 
777 	for (i = 0; i < ab->hw_params.max_radios; i++) {
778 		kfree(ab->default_regd[i]);
779 		kfree(ab->new_regd[i]);
780 	}
781 }
782