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