xref: /freebsd/sys/contrib/dev/athk/ath11k/reg.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 #include <linux/rtnetlink.h>
6 
7 #include "core.h"
8 #include "debug.h"
9 
10 /* World regdom to be used in case default regd from fw is unavailable */
11 #define ATH11K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
12 #define ATH11K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
13 					  NL80211_RRF_NO_IR)
14 #define ATH11K_5GHZ_5725_5850    REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
15 					  NL80211_RRF_NO_IR)
16 
17 #define ETSI_WEATHER_RADAR_BAND_LOW		5590
18 #define ETSI_WEATHER_RADAR_BAND_HIGH		5650
19 #define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT	600000
20 
21 static const struct ieee80211_regdomain ath11k_world_regd = {
22 	.n_reg_rules = 3,
23 	.alpha2 =  "00",
24 	.reg_rules = {
25 		ATH11K_2GHZ_CH01_11,
26 		ATH11K_5GHZ_5150_5350,
27 		ATH11K_5GHZ_5725_5850,
28 	}
29 };
30 
31 static bool ath11k_regdom_changes(struct ath11k *ar, char *alpha2)
32 {
33 	const struct ieee80211_regdomain *regd;
34 
35 	regd = rcu_dereference_rtnl(ar->hw->wiphy->regd);
36 	/* This can happen during wiphy registration where the previous
37 	 * user request is received before we update the regd received
38 	 * from firmware.
39 	 */
40 	if (!regd)
41 		return true;
42 
43 	return memcmp(regd->alpha2, alpha2, 2) != 0;
44 }
45 
46 static void
47 ath11k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
48 {
49 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
50 	struct wmi_init_country_params init_country_param;
51 	struct ath11k *ar = hw->priv;
52 	int ret;
53 
54 	ath11k_dbg(ar->ab, ATH11K_DBG_REG,
55 		   "Regulatory Notification received for %s\n", wiphy_name(wiphy));
56 
57 	/* Currently supporting only General User Hints. Cell base user
58 	 * hints to be handled later.
59 	 * Hints from other sources like Core, Beacons are not expected for
60 	 * self managed wiphy's
61 	 */
62 	if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
63 	      request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
64 		ath11k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
65 		return;
66 	}
67 
68 	if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
69 		ath11k_dbg(ar->ab, ATH11K_DBG_REG,
70 			   "Country Setting is not allowed\n");
71 		return;
72 	}
73 
74 	if (!ath11k_regdom_changes(ar, request->alpha2)) {
75 		ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Country is already set\n");
76 		return;
77 	}
78 
79 	/* Set the country code to the firmware and wait for
80 	 * the WMI_REG_CHAN_LIST_CC EVENT for updating the
81 	 * reg info
82 	 */
83 	init_country_param.flags = ALPHA_IS_SET;
84 	memcpy(&init_country_param.cc_info.alpha2, request->alpha2, 2);
85 	init_country_param.cc_info.alpha2[2] = 0;
86 
87 	ret = ath11k_wmi_send_init_country_cmd(ar, init_country_param);
88 	if (ret)
89 		ath11k_warn(ar->ab,
90 			    "INIT Country code set to fw failed : %d\n", ret);
91 
92 	ath11k_mac_11d_scan_stop(ar);
93 	ar->regdom_set_by_user = true;
94 }
95 
96 int ath11k_reg_update_chan_list(struct ath11k *ar)
97 {
98 	struct ieee80211_supported_band **bands;
99 	struct scan_chan_list_params *params;
100 	struct ieee80211_channel *channel;
101 	struct ieee80211_hw *hw = ar->hw;
102 	struct channel_param *ch;
103 	enum nl80211_band band;
104 	int num_channels = 0;
105 	int i, ret;
106 
107 	bands = hw->wiphy->bands;
108 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
109 		if (!bands[band])
110 			continue;
111 
112 		for (i = 0; i < bands[band]->n_channels; i++) {
113 			if (bands[band]->channels[i].flags &
114 			    IEEE80211_CHAN_DISABLED)
115 				continue;
116 
117 			num_channels++;
118 		}
119 	}
120 
121 	if (WARN_ON(!num_channels))
122 		return -EINVAL;
123 
124 	params = kzalloc(struct_size(params, ch_param, num_channels),
125 			 GFP_KERNEL);
126 	if (!params)
127 		return -ENOMEM;
128 
129 	params->pdev_id = ar->pdev->pdev_id;
130 	params->nallchans = num_channels;
131 
132 	ch = params->ch_param;
133 
134 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
135 		if (!bands[band])
136 			continue;
137 
138 		for (i = 0; i < bands[band]->n_channels; i++) {
139 			channel = &bands[band]->channels[i];
140 
141 			if (channel->flags & IEEE80211_CHAN_DISABLED)
142 				continue;
143 
144 			/* TODO: Set to true/false based on some condition? */
145 			ch->allow_ht = true;
146 			ch->allow_vht = true;
147 			ch->allow_he = true;
148 
149 			ch->dfs_set =
150 				!!(channel->flags & IEEE80211_CHAN_RADAR);
151 			ch->is_chan_passive = !!(channel->flags &
152 						IEEE80211_CHAN_NO_IR);
153 			ch->is_chan_passive |= ch->dfs_set;
154 			ch->mhz = channel->center_freq;
155 			ch->cfreq1 = channel->center_freq;
156 			ch->minpower = 0;
157 			ch->maxpower = channel->max_power * 2;
158 			ch->maxregpower = channel->max_reg_power * 2;
159 			ch->antennamax = channel->max_antenna_gain * 2;
160 
161 			/* TODO: Use appropriate phymodes */
162 			if (channel->band == NL80211_BAND_2GHZ)
163 				ch->phy_mode = MODE_11G;
164 			else
165 				ch->phy_mode = MODE_11A;
166 
167 			if (channel->band == NL80211_BAND_6GHZ &&
168 			    cfg80211_channel_is_psc(channel))
169 				ch->psc_channel = true;
170 
171 			ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
172 				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
173 				   i, params->nallchans,
174 				   ch->mhz, ch->maxpower, ch->maxregpower,
175 				   ch->antennamax, ch->phy_mode);
176 
177 			ch++;
178 			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
179 			 * set_agile, reg_class_idx
180 			 */
181 		}
182 	}
183 
184 	ret = ath11k_wmi_send_scan_chan_list_cmd(ar, params);
185 	kfree(params);
186 
187 	if (ar->pending_11d) {
188 		complete(&ar->finish_11d_ch_list);
189 		ar->pending_11d = false;
190 	}
191 
192 	return ret;
193 }
194 
195 #if defined(__linux__)
196 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
197 #elif defined(__FreeBSD__)
198 static void ath11k_copy_regd(const struct ieee80211_regdomain *regd_orig,
199 #endif
200 			     struct ieee80211_regdomain *regd_copy)
201 {
202 	u8 i;
203 
204 	/* The caller should have checked error conditions */
205 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
206 
207 	for (i = 0; i < regd_orig->n_reg_rules; i++)
208 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
209 		       sizeof(struct ieee80211_reg_rule));
210 }
211 
212 int ath11k_regd_update(struct ath11k *ar)
213 {
214 #if defined(__linux__)
215 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
216 #elif defined(__FreeBSD__)
217 	const struct ieee80211_regdomain *regd;
218 	struct ieee80211_regdomain *regd_copy = NULL;
219 #endif
220 	int ret, regd_len, pdev_id;
221 	struct ath11k_base *ab;
222 
223 	ab = ar->ab;
224 	pdev_id = ar->pdev_idx;
225 
226 	spin_lock_bh(&ab->base_lock);
227 
228 	/* Prefer the latest regd update over default if it's available */
229 	if (ab->new_regd[pdev_id]) {
230 		regd = ab->new_regd[pdev_id];
231 	} else {
232 		/* Apply the regd received during init through
233 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
234 		 * receive the regd, initialize with a default world
235 		 * regulatory.
236 		 */
237 		if (ab->default_regd[pdev_id]) {
238 			regd = ab->default_regd[pdev_id];
239 		} else {
240 			ath11k_warn(ab,
241 				    "failed to receive default regd during init\n");
242 #if defined(__linux__)
243 			regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
244 #elif defined(__FreeBSD__)
245 			regd = &ath11k_world_regd;
246 #endif
247 		}
248 	}
249 
250 	if (!regd) {
251 		ret = -EINVAL;
252 		spin_unlock_bh(&ab->base_lock);
253 		goto err;
254 	}
255 
256 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
257 		sizeof(struct ieee80211_reg_rule));
258 
259 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
260 	if (regd_copy)
261 		ath11k_copy_regd(regd, regd_copy);
262 
263 	spin_unlock_bh(&ab->base_lock);
264 
265 	if (!regd_copy) {
266 		ret = -ENOMEM;
267 		goto err;
268 	}
269 
270 	if (ar->pending_11d)
271 		complete(&ar->finish_11d_scan);
272 
273 	rtnl_lock();
274 	wiphy_lock(ar->hw->wiphy);
275 
276 	if (ar->pending_11d)
277 		reinit_completion(&ar->finish_11d_ch_list);
278 
279 	ret = regulatory_set_wiphy_regd_sync(ar->hw->wiphy, regd_copy);
280 	wiphy_unlock(ar->hw->wiphy);
281 	rtnl_unlock();
282 
283 	kfree(regd_copy);
284 
285 	if (ret)
286 		goto err;
287 
288 	if (ar->state == ATH11K_STATE_ON) {
289 		ret = ath11k_reg_update_chan_list(ar);
290 		if (ret)
291 			goto err;
292 	}
293 
294 	return 0;
295 err:
296 	ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
297 	return ret;
298 }
299 
300 static enum nl80211_dfs_regions
301 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
302 {
303 	switch (dfs_region) {
304 	case ATH11K_DFS_REG_FCC:
305 	case ATH11K_DFS_REG_CN:
306 		return NL80211_DFS_FCC;
307 	case ATH11K_DFS_REG_ETSI:
308 	case ATH11K_DFS_REG_KR:
309 		return NL80211_DFS_ETSI;
310 	case ATH11K_DFS_REG_MKK:
311 	case ATH11K_DFS_REG_MKK_N:
312 		return NL80211_DFS_JP;
313 	default:
314 		return NL80211_DFS_UNSET;
315 	}
316 }
317 
318 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
319 {
320 	u32 flags = 0;
321 
322 	if (reg_flags & REGULATORY_CHAN_NO_IR)
323 		flags = NL80211_RRF_NO_IR;
324 
325 	if (reg_flags & REGULATORY_CHAN_RADAR)
326 		flags |= NL80211_RRF_DFS;
327 
328 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
329 		flags |= NL80211_RRF_NO_OFDM;
330 
331 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
332 		flags |= NL80211_RRF_NO_OUTDOOR;
333 
334 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
335 		flags |= NL80211_RRF_NO_HT40;
336 
337 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
338 		flags |= NL80211_RRF_NO_80MHZ;
339 
340 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
341 		flags |= NL80211_RRF_NO_160MHZ;
342 
343 	return flags;
344 }
345 
346 static bool
347 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
348 			 struct ieee80211_reg_rule *rule2)
349 {
350 	u32 start_freq1, end_freq1;
351 	u32 start_freq2, end_freq2;
352 
353 	start_freq1 = rule1->freq_range.start_freq_khz;
354 	start_freq2 = rule2->freq_range.start_freq_khz;
355 
356 	end_freq1 = rule1->freq_range.end_freq_khz;
357 	end_freq2 = rule2->freq_range.end_freq_khz;
358 
359 	if ((start_freq1 >= start_freq2 &&
360 	     start_freq1 < end_freq2) ||
361 	    (start_freq2 > start_freq1 &&
362 	     start_freq2 < end_freq1))
363 		return true;
364 
365 	/* TODO: Should we restrict intersection feasibility
366 	 *  based on min bandwidth of the intersected region also,
367 	 *  say the intersected rule should have a  min bandwidth
368 	 * of 20MHz?
369 	 */
370 
371 	return false;
372 }
373 
374 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
375 				       struct ieee80211_reg_rule *rule2,
376 				       struct ieee80211_reg_rule *new_rule)
377 {
378 	u32 start_freq1, end_freq1;
379 	u32 start_freq2, end_freq2;
380 	u32 freq_diff, max_bw;
381 
382 	start_freq1 = rule1->freq_range.start_freq_khz;
383 	start_freq2 = rule2->freq_range.start_freq_khz;
384 
385 	end_freq1 = rule1->freq_range.end_freq_khz;
386 	end_freq2 = rule2->freq_range.end_freq_khz;
387 
388 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
389 						    start_freq2);
390 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
391 
392 	freq_diff = new_rule->freq_range.end_freq_khz -
393 			new_rule->freq_range.start_freq_khz;
394 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
395 		       rule2->freq_range.max_bandwidth_khz);
396 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
397 
398 	new_rule->power_rule.max_antenna_gain =
399 		min_t(u32, rule1->power_rule.max_antenna_gain,
400 		      rule2->power_rule.max_antenna_gain);
401 
402 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
403 					      rule2->power_rule.max_eirp);
404 
405 	/* Use the flags of both the rules */
406 	new_rule->flags = rule1->flags | rule2->flags;
407 
408 	/* To be safe, lts use the max cac timeout of both rules */
409 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
410 				     rule2->dfs_cac_ms);
411 }
412 
413 static struct ieee80211_regdomain *
414 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
415 		      struct ieee80211_regdomain *curr_regd)
416 {
417 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
418 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
419 	struct ieee80211_regdomain *new_regd = NULL;
420 	u8 i, j, k;
421 
422 	num_old_regd_rules = default_regd->n_reg_rules;
423 	num_curr_regd_rules = curr_regd->n_reg_rules;
424 	num_new_regd_rules = 0;
425 
426 	/* Find the number of intersecting rules to allocate new regd memory */
427 	for (i = 0; i < num_old_regd_rules; i++) {
428 		old_rule = default_regd->reg_rules + i;
429 		for (j = 0; j < num_curr_regd_rules; j++) {
430 			curr_rule = curr_regd->reg_rules + j;
431 
432 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
433 				num_new_regd_rules++;
434 		}
435 	}
436 
437 	if (!num_new_regd_rules)
438 		return NULL;
439 
440 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
441 			sizeof(struct ieee80211_reg_rule)),
442 			GFP_ATOMIC);
443 
444 	if (!new_regd)
445 		return NULL;
446 
447 	/* We set the new country and dfs region directly and only trim
448 	 * the freq, power, antenna gain by intersecting with the
449 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
450 	 */
451 	new_regd->n_reg_rules = num_new_regd_rules;
452 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
453 	new_regd->dfs_region = curr_regd->dfs_region;
454 	new_rule = new_regd->reg_rules;
455 
456 	for (i = 0, k = 0; i < num_old_regd_rules; i++) {
457 		old_rule = default_regd->reg_rules + i;
458 		for (j = 0; j < num_curr_regd_rules; j++) {
459 			curr_rule = curr_regd->reg_rules + j;
460 
461 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
462 				ath11k_reg_intersect_rules(old_rule, curr_rule,
463 							   (new_rule + k++));
464 		}
465 	}
466 	return new_regd;
467 }
468 
469 static const char *
470 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
471 {
472 	switch (dfs_region) {
473 	case NL80211_DFS_FCC:
474 		return "FCC";
475 	case NL80211_DFS_ETSI:
476 		return "ETSI";
477 	case NL80211_DFS_JP:
478 		return "JP";
479 	default:
480 		return "UNSET";
481 	}
482 }
483 
484 static u16
485 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
486 {
487 	u16 bw;
488 
489 	if (end_freq <= start_freq)
490 		return 0;
491 
492 	bw = end_freq - start_freq;
493 	bw = min_t(u16, bw, max_bw);
494 
495 	if (bw >= 80 && bw < 160)
496 		bw = 80;
497 	else if (bw >= 40 && bw < 80)
498 		bw = 40;
499 	else if (bw >= 20 && bw < 40)
500 		bw = 20;
501 	else
502 		bw = 0;
503 
504 	return bw;
505 }
506 
507 static void
508 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
509 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
510 		       u32 reg_flags)
511 {
512 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
513 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
514 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
515 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
516 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
517 	reg_rule->flags = reg_flags;
518 }
519 
520 static void
521 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
522 				     struct ieee80211_regdomain *regd,
523 				     struct cur_reg_rule *reg_rule,
524 				     u8 *rule_idx, u32 flags, u16 max_bw)
525 {
526 	u32 start_freq;
527 	u32 end_freq;
528 	u16 bw;
529 	u8 i;
530 
531 	i = *rule_idx;
532 
533 	/* there might be situations when even the input rule must be dropped */
534 	i--;
535 
536 	/* frequencies below weather radar */
537 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
538 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
539 	if (bw > 0) {
540 		i++;
541 
542 		ath11k_reg_update_rule(regd->reg_rules + i,
543 				       reg_rule->start_freq,
544 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
545 				       reg_rule->ant_gain, reg_rule->reg_power,
546 				       flags);
547 
548 		ath11k_dbg(ab, ATH11K_DBG_REG,
549 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
550 			   i + 1, reg_rule->start_freq,
551 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
552 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
553 			   flags);
554 	}
555 
556 	/* weather radar frequencies */
557 	start_freq = max_t(u32, reg_rule->start_freq,
558 			   ETSI_WEATHER_RADAR_BAND_LOW);
559 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
560 
561 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
562 	if (bw > 0) {
563 		i++;
564 
565 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
566 				       end_freq, bw, reg_rule->ant_gain,
567 				       reg_rule->reg_power, flags);
568 
569 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
570 
571 		ath11k_dbg(ab, ATH11K_DBG_REG,
572 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
573 			   i + 1, start_freq, end_freq, bw,
574 			   reg_rule->ant_gain, reg_rule->reg_power,
575 			   regd->reg_rules[i].dfs_cac_ms, flags);
576 	}
577 
578 	/* frequencies above weather radar */
579 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
580 				  reg_rule->end_freq, max_bw);
581 	if (bw > 0) {
582 		i++;
583 
584 		ath11k_reg_update_rule(regd->reg_rules + i,
585 				       ETSI_WEATHER_RADAR_BAND_HIGH,
586 				       reg_rule->end_freq, bw,
587 				       reg_rule->ant_gain, reg_rule->reg_power,
588 				       flags);
589 
590 		ath11k_dbg(ab, ATH11K_DBG_REG,
591 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
592 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
593 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
594 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
595 			   flags);
596 	}
597 
598 	*rule_idx = i;
599 }
600 
601 struct ieee80211_regdomain *
602 ath11k_reg_build_regd(struct ath11k_base *ab,
603 		      struct cur_regulatory_info *reg_info, bool intersect)
604 {
605 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
606 	struct cur_reg_rule *reg_rule;
607 	u8 i = 0, j = 0;
608 	u8 num_rules;
609 	u16 max_bw;
610 	u32 flags;
611 	char alpha2[3];
612 
613 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
614 
615 	if (!num_rules)
616 		goto ret;
617 
618 	/* Add max additional rules to accommodate weather radar band */
619 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
620 		num_rules += 2;
621 
622 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
623 			(num_rules * sizeof(struct ieee80211_reg_rule)),
624 			GFP_ATOMIC);
625 	if (!tmp_regd)
626 		goto ret;
627 
628 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
629 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
630 	alpha2[2] = '\0';
631 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
632 
633 	ath11k_dbg(ab, ATH11K_DBG_REG,
634 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
635 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
636 		   reg_info->dfs_region, num_rules);
637 	/* Update reg_rules[] below. Firmware is expected to
638 	 * send these rules in order(2G rules first and then 5G)
639 	 */
640 	for (; i < num_rules; i++) {
641 		if (reg_info->num_2g_reg_rules &&
642 		    (i < reg_info->num_2g_reg_rules)) {
643 			reg_rule = reg_info->reg_rules_2g_ptr + i;
644 			max_bw = min_t(u16, reg_rule->max_bw,
645 				       reg_info->max_bw_2g);
646 			flags = 0;
647 		} else if (reg_info->num_5g_reg_rules &&
648 			   (j < reg_info->num_5g_reg_rules)) {
649 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
650 			max_bw = min_t(u16, reg_rule->max_bw,
651 				       reg_info->max_bw_5g);
652 
653 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
654 			 * BW Auto correction, we can enable this by default
655 			 * for all 5G rules here. The regulatory core performs
656 			 * BW correction if required and applies flags as
657 			 * per other BW rule flags we pass from here
658 			 */
659 			flags = NL80211_RRF_AUTO_BW;
660 		} else {
661 			break;
662 		}
663 
664 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
665 
666 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
667 				       reg_rule->start_freq,
668 				       reg_rule->end_freq, max_bw,
669 				       reg_rule->ant_gain, reg_rule->reg_power,
670 				       flags);
671 
672 		/* Update dfs cac timeout if the dfs domain is ETSI and the
673 		 * new rule covers weather radar band.
674 		 * Default value of '0' corresponds to 60s timeout, so no
675 		 * need to update that for other rules.
676 		 */
677 		if (flags & NL80211_RRF_DFS &&
678 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
679 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
680 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
681 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
682 							     reg_rule, &i,
683 							     flags, max_bw);
684 			continue;
685 		}
686 
687 		ath11k_dbg(ab, ATH11K_DBG_REG,
688 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
689 			   i + 1, reg_rule->start_freq, reg_rule->end_freq,
690 			   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
691 			   tmp_regd->reg_rules[i].dfs_cac_ms,
692 			   flags);
693 	}
694 
695 	tmp_regd->n_reg_rules = i;
696 
697 	if (intersect) {
698 		default_regd = ab->default_regd[reg_info->phy_id];
699 
700 		/* Get a new regd by intersecting the received regd with
701 		 * our default regd.
702 		 */
703 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
704 		kfree(tmp_regd);
705 		if (!new_regd) {
706 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
707 			goto ret;
708 		}
709 	} else {
710 		new_regd = tmp_regd;
711 	}
712 
713 ret:
714 	return new_regd;
715 }
716 
717 void ath11k_regd_update_work(struct work_struct *work)
718 {
719 	struct ath11k *ar = container_of(work, struct ath11k,
720 					 regd_update_work);
721 	int ret;
722 
723 	ret = ath11k_regd_update(ar);
724 	if (ret) {
725 		/* Firmware has already moved to the new regd. We need
726 		 * to maintain channel consistency across FW, Host driver
727 		 * and userspace. Hence as a fallback mechanism we can set
728 		 * the prev or default country code to the firmware.
729 		 */
730 		/* TODO: Implement Fallback Mechanism */
731 	}
732 }
733 
734 void ath11k_reg_init(struct ath11k *ar)
735 {
736 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
737 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
738 }
739 
740 void ath11k_reg_free(struct ath11k_base *ab)
741 {
742 	int i;
743 
744 	for (i = 0; i < ab->hw_params.max_radios; i++) {
745 		kfree(ab->default_regd[i]);
746 		kfree(ab->new_regd[i]);
747 	}
748 }
749