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