xref: /linux/drivers/net/wireless/ath/ath12k/reg.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 #include <linux/rtnetlink.h>
7 #include "core.h"
8 #include "debug.h"
9 #include "mac.h"
10 
11 /* World regdom to be used in case default regd from fw is unavailable */
12 #define ATH12K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
13 #define ATH12K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
14 					  NL80211_RRF_NO_IR)
15 #define ATH12K_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 ath12k_world_regd = {
23 	.n_reg_rules = 3,
24 	.alpha2 = "00",
25 	.reg_rules = {
26 		ATH12K_2GHZ_CH01_11,
27 		ATH12K_5GHZ_5150_5350,
28 		ATH12K_5GHZ_5725_5850,
29 	}
30 };
31 
32 static bool ath12k_regdom_changes(struct ieee80211_hw *hw, char *alpha2)
33 {
34 	const struct ieee80211_regdomain *regd;
35 
36 	regd = rcu_dereference_rtnl(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 ath12k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
49 {
50 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
51 	struct ath12k_wmi_init_country_arg arg;
52 	struct wmi_set_current_country_arg current_arg = {};
53 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
54 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
55 	int ret, i;
56 
57 	ath12k_dbg(ar->ab, ATH12K_DBG_REG,
58 		   "Regulatory Notification received for %s\n", wiphy_name(wiphy));
59 
60 	if (request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
61 		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
62 			   "driver initiated regd update\n");
63 		if (ah->state != ATH12K_HW_STATE_ON)
64 			return;
65 
66 		for_each_ar(ah, ar, i) {
67 			ret = ath12k_reg_update_chan_list(ar, true);
68 			if (ret) {
69 				ath12k_warn(ar->ab,
70 					    "failed to update chan list for pdev %u, ret %d\n",
71 					    i, ret);
72 				break;
73 			}
74 		}
75 		return;
76 	}
77 
78 	/* Currently supporting only General User Hints. Cell base user
79 	 * hints to be handled later.
80 	 * Hints from other sources like Core, Beacons are not expected for
81 	 * self managed wiphy's
82 	 */
83 	if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
84 	      request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
85 		ath12k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
86 		return;
87 	}
88 
89 	if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
90 		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
91 			   "Country Setting is not allowed\n");
92 		return;
93 	}
94 
95 	if (!ath12k_regdom_changes(hw, request->alpha2)) {
96 		ath12k_dbg(ar->ab, ATH12K_DBG_REG, "Country is already set\n");
97 		return;
98 	}
99 
100 	/* Allow fresh updates to wiphy regd */
101 	ah->regd_updated = false;
102 
103 	/* Send the reg change request to all the radios */
104 	for_each_ar(ah, ar, i) {
105 		if (ar->ab->hw_params->current_cc_support) {
106 			memcpy(&current_arg.alpha2, request->alpha2, 2);
107 			memcpy(&ar->alpha2, &current_arg.alpha2, 2);
108 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_arg);
109 			if (ret)
110 				ath12k_warn(ar->ab,
111 					    "failed set current country code: %d\n", ret);
112 		} else {
113 			arg.flags = ALPHA_IS_SET;
114 			memcpy(&arg.cc_info.alpha2, request->alpha2, 2);
115 			arg.cc_info.alpha2[2] = 0;
116 
117 			ret = ath12k_wmi_send_init_country_cmd(ar, &arg);
118 			if (ret)
119 				ath12k_warn(ar->ab,
120 					    "failed set INIT Country code: %d\n", ret);
121 		}
122 
123 		wiphy_lock(wiphy);
124 		ath12k_mac_11d_scan_stop(ar);
125 		wiphy_unlock(wiphy);
126 
127 		ar->regdom_set_by_user = true;
128 	}
129 }
130 
131 int ath12k_reg_update_chan_list(struct ath12k *ar, bool wait)
132 {
133 	struct ieee80211_supported_band **bands;
134 	struct ath12k_wmi_scan_chan_list_arg *arg;
135 	struct ieee80211_channel *channel;
136 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
137 	struct ath12k_wmi_channel_arg *ch;
138 	enum nl80211_band band;
139 	int num_channels = 0;
140 	int i, ret, left;
141 
142 	if (wait && ar->state_11d == ATH12K_11D_RUNNING) {
143 		left = wait_for_completion_timeout(&ar->completed_11d_scan,
144 						   ATH12K_SCAN_TIMEOUT_HZ);
145 		if (!left) {
146 			ath12k_dbg(ar->ab, ATH12K_DBG_REG,
147 				   "failed to receive 11d scan complete: timed out\n");
148 			ar->state_11d = ATH12K_11D_IDLE;
149 		}
150 		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
151 			   "reg 11d scan wait left time %d\n", left);
152 	}
153 
154 	if (wait &&
155 	    (ar->scan.state == ATH12K_SCAN_STARTING ||
156 	    ar->scan.state == ATH12K_SCAN_RUNNING)) {
157 		left = wait_for_completion_timeout(&ar->scan.completed,
158 						   ATH12K_SCAN_TIMEOUT_HZ);
159 		if (!left)
160 			ath12k_dbg(ar->ab, ATH12K_DBG_REG,
161 				   "failed to receive hw scan complete: timed out\n");
162 
163 		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
164 			   "reg hw scan wait left time %d\n", left);
165 	}
166 
167 	if (ar->ah->state == ATH12K_HW_STATE_RESTARTING)
168 		return 0;
169 
170 	bands = hw->wiphy->bands;
171 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
172 		if (!(ar->mac.sbands[band].channels && bands[band]))
173 			continue;
174 
175 		for (i = 0; i < bands[band]->n_channels; i++) {
176 			if (bands[band]->channels[i].flags &
177 			    IEEE80211_CHAN_DISABLED)
178 				continue;
179 
180 			num_channels++;
181 		}
182 	}
183 
184 	if (WARN_ON(!num_channels))
185 		return -EINVAL;
186 
187 	arg = kzalloc(struct_size(arg, channel, num_channels), GFP_KERNEL);
188 
189 	if (!arg)
190 		return -ENOMEM;
191 
192 	arg->pdev_id = ar->pdev->pdev_id;
193 	arg->nallchans = num_channels;
194 
195 	ch = arg->channel;
196 
197 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
198 		if (!(ar->mac.sbands[band].channels && bands[band]))
199 			continue;
200 
201 		for (i = 0; i < bands[band]->n_channels; i++) {
202 			channel = &bands[band]->channels[i];
203 
204 			if (channel->flags & IEEE80211_CHAN_DISABLED)
205 				continue;
206 
207 			/* TODO: Set to true/false based on some condition? */
208 			ch->allow_ht = true;
209 			ch->allow_vht = true;
210 			ch->allow_he = true;
211 
212 			ch->dfs_set =
213 				!!(channel->flags & IEEE80211_CHAN_RADAR);
214 			ch->is_chan_passive = !!(channel->flags &
215 						IEEE80211_CHAN_NO_IR);
216 			ch->is_chan_passive |= ch->dfs_set;
217 			ch->mhz = channel->center_freq;
218 			ch->cfreq1 = channel->center_freq;
219 			ch->minpower = 0;
220 			ch->maxpower = channel->max_power * 2;
221 			ch->maxregpower = channel->max_reg_power * 2;
222 			ch->antennamax = channel->max_antenna_gain * 2;
223 
224 			/* TODO: Use appropriate phymodes */
225 			if (channel->band == NL80211_BAND_2GHZ)
226 				ch->phy_mode = MODE_11G;
227 			else
228 				ch->phy_mode = MODE_11A;
229 
230 			if (channel->band == NL80211_BAND_6GHZ &&
231 			    cfg80211_channel_is_psc(channel))
232 				ch->psc_channel = true;
233 
234 			ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
235 				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
236 				   i, arg->nallchans,
237 				   ch->mhz, ch->maxpower, ch->maxregpower,
238 				   ch->antennamax, ch->phy_mode);
239 
240 			ch++;
241 			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
242 			 * set_agile, reg_class_idx
243 			 */
244 		}
245 	}
246 
247 	ret = ath12k_wmi_send_scan_chan_list_cmd(ar, arg);
248 	kfree(arg);
249 
250 	return ret;
251 }
252 
253 static void ath12k_copy_regd(struct ieee80211_regdomain *regd_orig,
254 			     struct ieee80211_regdomain *regd_copy)
255 {
256 	u8 i;
257 
258 	/* The caller should have checked error conditions */
259 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
260 
261 	for (i = 0; i < regd_orig->n_reg_rules; i++)
262 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
263 		       sizeof(struct ieee80211_reg_rule));
264 }
265 
266 int ath12k_regd_update(struct ath12k *ar, bool init)
267 {
268 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
269 	u32 phy_id, freq_low, freq_high, supported_bands;
270 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
271 	struct ieee80211_hw *hw = ah->hw;
272 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
273 	int ret, regd_len, pdev_id;
274 	struct ath12k_base *ab;
275 
276 	ab = ar->ab;
277 
278 	supported_bands = ar->pdev->cap.supported_bands;
279 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
280 
281 	/* Possible that due to reg change, current limits for supported
282 	 * frequency changed. Update it. As a first step, reset the
283 	 * previous values and then compute and set the new values.
284 	 */
285 	ar->freq_range.start_freq = 0;
286 	ar->freq_range.end_freq = 0;
287 
288 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
289 		if (ab->hw_params->single_pdev_only) {
290 			phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_2GHZ_CAP].phy_id;
291 			reg_cap = &ab->hal_reg_cap[phy_id];
292 		}
293 
294 		freq_low = max(reg_cap->low_2ghz_chan, ab->reg_freq_2ghz.start_freq);
295 		freq_high = min(reg_cap->high_2ghz_chan, ab->reg_freq_2ghz.end_freq);
296 
297 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
298 	}
299 
300 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && !ar->supports_6ghz) {
301 		if (ab->hw_params->single_pdev_only) {
302 			phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_5GHZ_CAP].phy_id;
303 			reg_cap = &ab->hal_reg_cap[phy_id];
304 		}
305 
306 		freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_5ghz.start_freq);
307 		freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_5ghz.end_freq);
308 
309 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
310 	}
311 
312 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && ar->supports_6ghz) {
313 		freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_6ghz.start_freq);
314 		freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_6ghz.end_freq);
315 
316 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
317 	}
318 
319 	/* If one of the radios within ah has already updated the regd for
320 	 * the wiphy, then avoid setting regd again
321 	 */
322 	if (ah->regd_updated)
323 		return 0;
324 
325 	/* firmware provides reg rules which are similar for 2 GHz and 5 GHz
326 	 * pdev but 6 GHz pdev has superset of all rules including rules for
327 	 * all bands, we prefer 6 GHz pdev's rules to be used for setup of
328 	 * the wiphy regd.
329 	 * If 6 GHz pdev was part of the ath12k_hw, wait for the 6 GHz pdev,
330 	 * else pick the first pdev which calls this function and use its
331 	 * regd to update global hw regd.
332 	 * The regd_updated flag set at the end will not allow any further
333 	 * updates.
334 	 */
335 	if (ah->use_6ghz_regd && !ar->supports_6ghz)
336 		return 0;
337 
338 	pdev_id = ar->pdev_idx;
339 
340 	spin_lock_bh(&ab->base_lock);
341 
342 	if (init) {
343 		/* Apply the regd received during init through
344 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
345 		 * receive the regd, initialize with a default world
346 		 * regulatory.
347 		 */
348 		if (ab->default_regd[pdev_id]) {
349 			regd = ab->default_regd[pdev_id];
350 		} else {
351 			ath12k_warn(ab,
352 				    "failed to receive default regd during init\n");
353 			regd = (struct ieee80211_regdomain *)&ath12k_world_regd;
354 		}
355 	} else {
356 		regd = ab->new_regd[pdev_id];
357 	}
358 
359 	if (!regd) {
360 		ret = -EINVAL;
361 		spin_unlock_bh(&ab->base_lock);
362 		goto err;
363 	}
364 
365 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
366 		sizeof(struct ieee80211_reg_rule));
367 
368 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
369 	if (regd_copy)
370 		ath12k_copy_regd(regd, regd_copy);
371 
372 	spin_unlock_bh(&ab->base_lock);
373 
374 	if (!regd_copy) {
375 		ret = -ENOMEM;
376 		goto err;
377 	}
378 
379 	ret = regulatory_set_wiphy_regd(hw->wiphy, regd_copy);
380 
381 	kfree(regd_copy);
382 
383 	if (ret)
384 		goto err;
385 
386 	if (ah->state != ATH12K_HW_STATE_ON)
387 		goto skip;
388 
389 	ah->regd_updated = true;
390 
391 skip:
392 	return 0;
393 err:
394 	ath12k_warn(ab, "failed to perform regd update : %d\n", ret);
395 	return ret;
396 }
397 
398 static enum nl80211_dfs_regions
399 ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region)
400 {
401 	switch (dfs_region) {
402 	case ATH12K_DFS_REG_FCC:
403 	case ATH12K_DFS_REG_CN:
404 		return NL80211_DFS_FCC;
405 	case ATH12K_DFS_REG_ETSI:
406 	case ATH12K_DFS_REG_KR:
407 		return NL80211_DFS_ETSI;
408 	case ATH12K_DFS_REG_MKK:
409 	case ATH12K_DFS_REG_MKK_N:
410 		return NL80211_DFS_JP;
411 	default:
412 		return NL80211_DFS_UNSET;
413 	}
414 }
415 
416 static u32 ath12k_map_fw_reg_flags(u16 reg_flags)
417 {
418 	u32 flags = 0;
419 
420 	if (reg_flags & REGULATORY_CHAN_NO_IR)
421 		flags = NL80211_RRF_NO_IR;
422 
423 	if (reg_flags & REGULATORY_CHAN_RADAR)
424 		flags |= NL80211_RRF_DFS;
425 
426 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
427 		flags |= NL80211_RRF_NO_OFDM;
428 
429 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
430 		flags |= NL80211_RRF_NO_OUTDOOR;
431 
432 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
433 		flags |= NL80211_RRF_NO_HT40;
434 
435 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
436 		flags |= NL80211_RRF_NO_80MHZ;
437 
438 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
439 		flags |= NL80211_RRF_NO_160MHZ;
440 
441 	return flags;
442 }
443 
444 static u32 ath12k_map_fw_phy_flags(u32 phy_flags)
445 {
446 	u32 flags = 0;
447 
448 	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX)
449 		flags |= NL80211_RRF_NO_HE;
450 
451 	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE)
452 		flags |= NL80211_RRF_NO_EHT;
453 
454 	return flags;
455 }
456 
457 static const char *
458 ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
459 {
460 	switch (dfs_region) {
461 	case NL80211_DFS_FCC:
462 		return "FCC";
463 	case NL80211_DFS_ETSI:
464 		return "ETSI";
465 	case NL80211_DFS_JP:
466 		return "JP";
467 	default:
468 		return "UNSET";
469 	}
470 }
471 
472 static u16
473 ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
474 {
475 	u16 bw;
476 
477 	bw = end_freq - start_freq;
478 	bw = min_t(u16, bw, max_bw);
479 
480 	if (bw >= 80 && bw < 160)
481 		bw = 80;
482 	else if (bw >= 40 && bw < 80)
483 		bw = 40;
484 	else if (bw < 40)
485 		bw = 20;
486 
487 	return bw;
488 }
489 
490 static void
491 ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
492 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
493 		       s8 psd, u32 reg_flags)
494 {
495 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
496 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
497 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
498 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
499 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
500 	reg_rule->psd = psd;
501 	reg_rule->flags = reg_flags;
502 }
503 
504 static void
505 ath12k_reg_update_weather_radar_band(struct ath12k_base *ab,
506 				     struct ieee80211_regdomain *regd,
507 				     struct ath12k_reg_rule *reg_rule,
508 				     u8 *rule_idx, u32 flags, u16 max_bw)
509 {
510 	u32 end_freq;
511 	u16 bw;
512 	u8 i;
513 
514 	i = *rule_idx;
515 
516 	bw = ath12k_reg_adjust_bw(reg_rule->start_freq,
517 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
518 
519 	ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq,
520 			       ETSI_WEATHER_RADAR_BAND_LOW, bw,
521 			       reg_rule->ant_gain, reg_rule->reg_power,
522 			       reg_rule->psd_eirp, flags);
523 
524 	ath12k_dbg(ab, ATH12K_DBG_REG,
525 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
526 		   i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW,
527 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
528 		   regd->reg_rules[i].dfs_cac_ms,
529 		   flags);
530 
531 	if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH)
532 		end_freq = ETSI_WEATHER_RADAR_BAND_HIGH;
533 	else
534 		end_freq = reg_rule->end_freq;
535 
536 	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
537 				  max_bw);
538 
539 	i++;
540 
541 	ath12k_reg_update_rule(regd->reg_rules + i,
542 			       ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw,
543 			       reg_rule->ant_gain, reg_rule->reg_power,
544 			       reg_rule->psd_eirp, flags);
545 
546 	regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
547 
548 	ath12k_dbg(ab, ATH12K_DBG_REG,
549 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
550 		   i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
551 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
552 		   regd->reg_rules[i].dfs_cac_ms,
553 		   flags);
554 
555 	if (end_freq == reg_rule->end_freq) {
556 		regd->n_reg_rules--;
557 		*rule_idx = i;
558 		return;
559 	}
560 
561 	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
562 				  reg_rule->end_freq, max_bw);
563 
564 	i++;
565 
566 	ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH,
567 			       reg_rule->end_freq, bw,
568 			       reg_rule->ant_gain, reg_rule->reg_power,
569 			       reg_rule->psd_eirp, flags);
570 
571 	ath12k_dbg(ab, ATH12K_DBG_REG,
572 		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
573 		   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq,
574 		   bw, reg_rule->ant_gain, reg_rule->reg_power,
575 		   regd->reg_rules[i].dfs_cac_ms,
576 		   flags);
577 
578 	*rule_idx = i;
579 }
580 
581 static void ath12k_reg_update_freq_range(struct ath12k_reg_freq *reg_freq,
582 					 struct ath12k_reg_rule *reg_rule)
583 {
584 	if (reg_freq->start_freq > reg_rule->start_freq)
585 		reg_freq->start_freq = reg_rule->start_freq;
586 
587 	if (reg_freq->end_freq < reg_rule->end_freq)
588 		reg_freq->end_freq = reg_rule->end_freq;
589 }
590 
591 enum wmi_reg_6g_ap_type
592 ath12k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
593 {
594 	switch (power_type) {
595 	case IEEE80211_REG_LPI_AP:
596 		return WMI_REG_INDOOR_AP;
597 	case IEEE80211_REG_SP_AP:
598 		return WMI_REG_STD_POWER_AP;
599 	case IEEE80211_REG_VLP_AP:
600 		return WMI_REG_VLP_AP;
601 	default:
602 		return WMI_REG_MAX_AP_TYPE;
603 	}
604 }
605 
606 struct ieee80211_regdomain *
607 ath12k_reg_build_regd(struct ath12k_base *ab,
608 		      struct ath12k_reg_info *reg_info,
609 		      enum wmi_vdev_type vdev_type,
610 		      enum ieee80211_ap_reg_power power_type)
611 {
612 	struct ieee80211_regdomain *new_regd = NULL;
613 	struct ath12k_reg_rule *reg_rule, *reg_rule_6ghz;
614 	u32 flags, reg_6ghz_number, max_bw_6ghz;
615 	u8 i = 0, j = 0, k = 0;
616 	u8 num_rules;
617 	u16 max_bw;
618 	char alpha2[3];
619 
620 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
621 
622 	if (reg_info->is_ext_reg_event) {
623 		if (vdev_type == WMI_VDEV_TYPE_STA) {
624 			enum wmi_reg_6g_ap_type ap_type;
625 
626 			ap_type = ath12k_reg_ap_pwr_convert(power_type);
627 			if (ap_type == WMI_REG_MAX_AP_TYPE)
628 				ap_type = WMI_REG_INDOOR_AP;
629 
630 			reg_6ghz_number = reg_info->num_6g_reg_rules_cl
631 					[ap_type][WMI_REG_DEFAULT_CLIENT];
632 			if (reg_6ghz_number == 0) {
633 				ap_type = WMI_REG_INDOOR_AP;
634 				reg_6ghz_number = reg_info->num_6g_reg_rules_cl
635 						[ap_type][WMI_REG_DEFAULT_CLIENT];
636 			}
637 
638 			reg_rule_6ghz = reg_info->reg_rules_6g_client_ptr
639 					[ap_type][WMI_REG_DEFAULT_CLIENT];
640 			max_bw_6ghz = reg_info->max_bw_6g_client
641 					[ap_type][WMI_REG_DEFAULT_CLIENT];
642 		} else {
643 			reg_6ghz_number = reg_info->num_6g_reg_rules_ap
644 						[WMI_REG_INDOOR_AP];
645 			reg_rule_6ghz =
646 				reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP];
647 			max_bw_6ghz = reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP];
648 		}
649 
650 		num_rules += reg_6ghz_number;
651 	}
652 
653 	if (!num_rules)
654 		goto ret;
655 
656 	/* Add max additional rules to accommodate weather radar band */
657 	if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI)
658 		num_rules += 2;
659 
660 	new_regd = kzalloc(sizeof(*new_regd) +
661 			   (num_rules * sizeof(struct ieee80211_reg_rule)),
662 			   GFP_ATOMIC);
663 	if (!new_regd)
664 		goto ret;
665 
666 	memcpy(new_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
667 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
668 	alpha2[2] = '\0';
669 	new_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region);
670 
671 	ath12k_dbg(ab, ATH12K_DBG_REG,
672 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
673 		   alpha2, ath12k_reg_get_regdom_str(new_regd->dfs_region),
674 		   reg_info->dfs_region, num_rules);
675 
676 	/* Reset start and end frequency for each band
677 	 */
678 	ab->reg_freq_5ghz.start_freq = INT_MAX;
679 	ab->reg_freq_5ghz.end_freq = 0;
680 	ab->reg_freq_2ghz.start_freq = INT_MAX;
681 	ab->reg_freq_2ghz.end_freq = 0;
682 	ab->reg_freq_6ghz.start_freq = INT_MAX;
683 	ab->reg_freq_6ghz.end_freq = 0;
684 
685 	/* Update reg_rules[] below. Firmware is expected to
686 	 * send these rules in order(2G rules first and then 5G)
687 	 */
688 	for (; i < num_rules; i++) {
689 		if (reg_info->num_2g_reg_rules &&
690 		    (i < reg_info->num_2g_reg_rules)) {
691 			reg_rule = reg_info->reg_rules_2g_ptr + i;
692 			max_bw = min_t(u16, reg_rule->max_bw,
693 				       reg_info->max_bw_2g);
694 			flags = 0;
695 			ath12k_reg_update_freq_range(&ab->reg_freq_2ghz, reg_rule);
696 		} else if (reg_info->num_5g_reg_rules &&
697 			   (j < reg_info->num_5g_reg_rules)) {
698 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
699 			max_bw = min_t(u16, reg_rule->max_bw,
700 				       reg_info->max_bw_5g);
701 
702 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
703 			 * BW Auto correction, we can enable this by default
704 			 * for all 5G rules here. The regulatory core performs
705 			 * BW correction if required and applies flags as
706 			 * per other BW rule flags we pass from here
707 			 */
708 			flags = NL80211_RRF_AUTO_BW;
709 			ath12k_reg_update_freq_range(&ab->reg_freq_5ghz, reg_rule);
710 		} else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
711 			   (k < reg_6ghz_number)) {
712 			reg_rule = reg_rule_6ghz + k++;
713 			max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
714 			flags = NL80211_RRF_AUTO_BW;
715 			if (reg_rule->psd_flag)
716 				flags |= NL80211_RRF_PSD;
717 			ath12k_reg_update_freq_range(&ab->reg_freq_6ghz, reg_rule);
718 		} else {
719 			break;
720 		}
721 
722 		flags |= ath12k_map_fw_reg_flags(reg_rule->flags);
723 		flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap);
724 
725 		ath12k_reg_update_rule(new_regd->reg_rules + i,
726 				       reg_rule->start_freq,
727 				       reg_rule->end_freq, max_bw,
728 				       reg_rule->ant_gain, reg_rule->reg_power,
729 				       reg_rule->psd_eirp, flags);
730 
731 		/* Update dfs cac timeout if the dfs domain is ETSI and the
732 		 * new rule covers weather radar band.
733 		 * Default value of '0' corresponds to 60s timeout, so no
734 		 * need to update that for other rules.
735 		 */
736 		if (flags & NL80211_RRF_DFS &&
737 		    reg_info->dfs_region == ATH12K_DFS_REG_ETSI &&
738 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
739 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
740 			ath12k_reg_update_weather_radar_band(ab, new_regd,
741 							     reg_rule, &i,
742 							     flags, max_bw);
743 			continue;
744 		}
745 
746 		if (reg_info->is_ext_reg_event) {
747 			ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
748 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
749 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
750 				   new_regd->reg_rules[i].dfs_cac_ms,
751 				   flags, reg_rule->psd_flag, reg_rule->psd_eirp);
752 		} else {
753 			ath12k_dbg(ab, ATH12K_DBG_REG,
754 				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
755 				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
756 				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
757 				   new_regd->reg_rules[i].dfs_cac_ms,
758 				   flags);
759 		}
760 	}
761 
762 	new_regd->n_reg_rules = i;
763 ret:
764 	return new_regd;
765 }
766 
767 void ath12k_regd_update_work(struct work_struct *work)
768 {
769 	struct ath12k *ar = container_of(work, struct ath12k,
770 					 regd_update_work);
771 	int ret;
772 
773 	ret = ath12k_regd_update(ar, false);
774 	if (ret) {
775 		/* Firmware has already moved to the new regd. We need
776 		 * to maintain channel consistency across FW, Host driver
777 		 * and userspace. Hence as a fallback mechanism we can set
778 		 * the prev or default country code to the firmware.
779 		 */
780 		/* TODO: Implement Fallback Mechanism */
781 	}
782 }
783 
784 void ath12k_reg_reset_reg_info(struct ath12k_reg_info *reg_info)
785 {
786 	u8 i, j;
787 
788 	if (!reg_info)
789 		return;
790 
791 	kfree(reg_info->reg_rules_2g_ptr);
792 	kfree(reg_info->reg_rules_5g_ptr);
793 
794 	if (reg_info->is_ext_reg_event) {
795 		for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
796 			kfree(reg_info->reg_rules_6g_ap_ptr[i]);
797 
798 			for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
799 				kfree(reg_info->reg_rules_6g_client_ptr[i][j]);
800 		}
801 	}
802 }
803 
804 enum ath12k_reg_status ath12k_reg_validate_reg_info(struct ath12k_base *ab,
805 						    struct ath12k_reg_info *reg_info)
806 {
807 	int pdev_idx = reg_info->phy_id;
808 
809 	if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
810 		/* In case of failure to set the requested country,
811 		 * firmware retains the current regd. We print a failure info
812 		 * and return from here.
813 		 */
814 		ath12k_warn(ab, "Failed to set the requested Country regulatory setting\n");
815 		return ATH12K_REG_STATUS_DROP;
816 	}
817 
818 	if (pdev_idx >= ab->num_radios) {
819 		/* Process the event for phy0 only if single_pdev_only
820 		 * is true. If pdev_idx is valid but not 0, discard the
821 		 * event. Otherwise, it goes to fallback.
822 		 */
823 		if (ab->hw_params->single_pdev_only &&
824 		    pdev_idx < ab->hw_params->num_rxdma_per_pdev)
825 			return ATH12K_REG_STATUS_DROP;
826 		else
827 			return ATH12K_REG_STATUS_FALLBACK;
828 	}
829 
830 	/* Avoid multiple overwrites to default regd, during core
831 	 * stop-start after mac registration.
832 	 */
833 	if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
834 	    !memcmp(ab->default_regd[pdev_idx]->alpha2,
835 		    reg_info->alpha2, 2))
836 		return ATH12K_REG_STATUS_DROP;
837 
838 	return ATH12K_REG_STATUS_VALID;
839 }
840 
841 int ath12k_reg_handle_chan_list(struct ath12k_base *ab,
842 				struct ath12k_reg_info *reg_info,
843 				enum wmi_vdev_type vdev_type,
844 				enum ieee80211_ap_reg_power power_type)
845 {
846 	struct ieee80211_regdomain *regd = NULL;
847 	int pdev_idx = reg_info->phy_id;
848 	struct ath12k *ar;
849 
850 	regd = ath12k_reg_build_regd(ab, reg_info, vdev_type, power_type);
851 	if (!regd)
852 		return -EINVAL;
853 
854 	spin_lock_bh(&ab->base_lock);
855 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) {
856 		/* Once mac is registered, ar is valid and all CC events from
857 		 * firmware is considered to be received due to user requests
858 		 * currently.
859 		 * Free previously built regd before assigning the newly
860 		 * generated regd to ar. NULL pointer handling will be
861 		 * taken care by kfree itself.
862 		 */
863 		ar = ab->pdevs[pdev_idx].ar;
864 		kfree(ab->new_regd[pdev_idx]);
865 		ab->new_regd[pdev_idx] = regd;
866 		queue_work(ab->workqueue, &ar->regd_update_work);
867 	} else {
868 		/* Multiple events for the same *ar is not expected. But we
869 		 * can still clear any previously stored default_regd if we
870 		 * are receiving this event for the same radio by mistake.
871 		 * NULL pointer handling will be taken care by kfree itself.
872 		 */
873 		kfree(ab->default_regd[pdev_idx]);
874 		/* This regd would be applied during mac registration */
875 		ab->default_regd[pdev_idx] = regd;
876 	}
877 	ab->dfs_region = reg_info->dfs_region;
878 	spin_unlock_bh(&ab->base_lock);
879 
880 	return 0;
881 }
882 
883 void ath12k_reg_init(struct ieee80211_hw *hw)
884 {
885 	hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
886 	hw->wiphy->flags |= WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER;
887 	hw->wiphy->reg_notifier = ath12k_reg_notifier;
888 }
889 
890 void ath12k_reg_free(struct ath12k_base *ab)
891 {
892 	int i;
893 
894 	mutex_lock(&ab->core_lock);
895 	for (i = 0; i < MAX_RADIOS; i++) {
896 		ath12k_reg_reset_reg_info(ab->reg_info[i]);
897 		kfree(ab->reg_info[i]);
898 		ab->reg_info[i] = NULL;
899 	}
900 
901 	for (i = 0; i < ab->hw_params->max_radios; i++) {
902 		kfree(ab->default_regd[i]);
903 		kfree(ab->new_regd[i]);
904 		ab->default_regd[i] = NULL;
905 		ab->new_regd[i] = NULL;
906 	}
907 	mutex_unlock(&ab->core_lock);
908 }
909