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