xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/reg.c (revision 1fe3a33b)
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 static void ath11k_copy_regd(struct ieee80211_regdomain *regd_orig,
196 			     struct ieee80211_regdomain *regd_copy)
197 {
198 	u8 i;
199 
200 	/* The caller should have checked error conditions */
201 	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
202 
203 	for (i = 0; i < regd_orig->n_reg_rules; i++)
204 		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
205 		       sizeof(struct ieee80211_reg_rule));
206 }
207 
208 int ath11k_regd_update(struct ath11k *ar)
209 {
210 	struct ieee80211_regdomain *regd, *regd_copy = NULL;
211 	int ret, regd_len, pdev_id;
212 	struct ath11k_base *ab;
213 
214 	ab = ar->ab;
215 	pdev_id = ar->pdev_idx;
216 
217 	spin_lock_bh(&ab->base_lock);
218 
219 	/* Prefer the latest regd update over default if it's available */
220 	if (ab->new_regd[pdev_id]) {
221 		regd = ab->new_regd[pdev_id];
222 	} else {
223 		/* Apply the regd received during init through
224 		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
225 		 * receive the regd, initialize with a default world
226 		 * regulatory.
227 		 */
228 		if (ab->default_regd[pdev_id]) {
229 			regd = ab->default_regd[pdev_id];
230 		} else {
231 			ath11k_warn(ab,
232 				    "failed to receive default regd during init\n");
233 			regd = (struct ieee80211_regdomain *)&ath11k_world_regd;
234 		}
235 	}
236 
237 	if (!regd) {
238 		ret = -EINVAL;
239 		spin_unlock_bh(&ab->base_lock);
240 		goto err;
241 	}
242 
243 	regd_len = sizeof(*regd) + (regd->n_reg_rules *
244 		sizeof(struct ieee80211_reg_rule));
245 
246 	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
247 	if (regd_copy)
248 		ath11k_copy_regd(regd, regd_copy);
249 
250 	spin_unlock_bh(&ab->base_lock);
251 
252 	if (!regd_copy) {
253 		ret = -ENOMEM;
254 		goto err;
255 	}
256 
257 	if (ar->pending_11d)
258 		complete(&ar->finish_11d_scan);
259 
260 	rtnl_lock();
261 	wiphy_lock(ar->hw->wiphy);
262 
263 	if (ar->pending_11d)
264 		reinit_completion(&ar->finish_11d_ch_list);
265 
266 	ret = regulatory_set_wiphy_regd_sync(ar->hw->wiphy, regd_copy);
267 	wiphy_unlock(ar->hw->wiphy);
268 	rtnl_unlock();
269 
270 	kfree(regd_copy);
271 
272 	if (ret)
273 		goto err;
274 
275 	if (ar->state == ATH11K_STATE_ON) {
276 		ret = ath11k_reg_update_chan_list(ar);
277 		if (ret)
278 			goto err;
279 	}
280 
281 	return 0;
282 err:
283 	ath11k_warn(ab, "failed to perform regd update : %d\n", ret);
284 	return ret;
285 }
286 
287 static enum nl80211_dfs_regions
288 ath11k_map_fw_dfs_region(enum ath11k_dfs_region dfs_region)
289 {
290 	switch (dfs_region) {
291 	case ATH11K_DFS_REG_FCC:
292 	case ATH11K_DFS_REG_CN:
293 		return NL80211_DFS_FCC;
294 	case ATH11K_DFS_REG_ETSI:
295 	case ATH11K_DFS_REG_KR:
296 		return NL80211_DFS_ETSI;
297 	case ATH11K_DFS_REG_MKK:
298 	case ATH11K_DFS_REG_MKK_N:
299 		return NL80211_DFS_JP;
300 	default:
301 		return NL80211_DFS_UNSET;
302 	}
303 }
304 
305 static u32 ath11k_map_fw_reg_flags(u16 reg_flags)
306 {
307 	u32 flags = 0;
308 
309 	if (reg_flags & REGULATORY_CHAN_NO_IR)
310 		flags = NL80211_RRF_NO_IR;
311 
312 	if (reg_flags & REGULATORY_CHAN_RADAR)
313 		flags |= NL80211_RRF_DFS;
314 
315 	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
316 		flags |= NL80211_RRF_NO_OFDM;
317 
318 	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
319 		flags |= NL80211_RRF_NO_OUTDOOR;
320 
321 	if (reg_flags & REGULATORY_CHAN_NO_HT40)
322 		flags |= NL80211_RRF_NO_HT40;
323 
324 	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
325 		flags |= NL80211_RRF_NO_80MHZ;
326 
327 	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
328 		flags |= NL80211_RRF_NO_160MHZ;
329 
330 	return flags;
331 }
332 
333 static bool
334 ath11k_reg_can_intersect(struct ieee80211_reg_rule *rule1,
335 			 struct ieee80211_reg_rule *rule2)
336 {
337 	u32 start_freq1, end_freq1;
338 	u32 start_freq2, end_freq2;
339 
340 	start_freq1 = rule1->freq_range.start_freq_khz;
341 	start_freq2 = rule2->freq_range.start_freq_khz;
342 
343 	end_freq1 = rule1->freq_range.end_freq_khz;
344 	end_freq2 = rule2->freq_range.end_freq_khz;
345 
346 	if ((start_freq1 >= start_freq2 &&
347 	     start_freq1 < end_freq2) ||
348 	    (start_freq2 > start_freq1 &&
349 	     start_freq2 < end_freq1))
350 		return true;
351 
352 	/* TODO: Should we restrict intersection feasibility
353 	 *  based on min bandwidth of the intersected region also,
354 	 *  say the intersected rule should have a  min bandwidth
355 	 * of 20MHz?
356 	 */
357 
358 	return false;
359 }
360 
361 static void ath11k_reg_intersect_rules(struct ieee80211_reg_rule *rule1,
362 				       struct ieee80211_reg_rule *rule2,
363 				       struct ieee80211_reg_rule *new_rule)
364 {
365 	u32 start_freq1, end_freq1;
366 	u32 start_freq2, end_freq2;
367 	u32 freq_diff, max_bw;
368 
369 	start_freq1 = rule1->freq_range.start_freq_khz;
370 	start_freq2 = rule2->freq_range.start_freq_khz;
371 
372 	end_freq1 = rule1->freq_range.end_freq_khz;
373 	end_freq2 = rule2->freq_range.end_freq_khz;
374 
375 	new_rule->freq_range.start_freq_khz = max_t(u32, start_freq1,
376 						    start_freq2);
377 	new_rule->freq_range.end_freq_khz = min_t(u32, end_freq1, end_freq2);
378 
379 	freq_diff = new_rule->freq_range.end_freq_khz -
380 			new_rule->freq_range.start_freq_khz;
381 	max_bw = min_t(u32, rule1->freq_range.max_bandwidth_khz,
382 		       rule2->freq_range.max_bandwidth_khz);
383 	new_rule->freq_range.max_bandwidth_khz = min_t(u32, max_bw, freq_diff);
384 
385 	new_rule->power_rule.max_antenna_gain =
386 		min_t(u32, rule1->power_rule.max_antenna_gain,
387 		      rule2->power_rule.max_antenna_gain);
388 
389 	new_rule->power_rule.max_eirp = min_t(u32, rule1->power_rule.max_eirp,
390 					      rule2->power_rule.max_eirp);
391 
392 	/* Use the flags of both the rules */
393 	new_rule->flags = rule1->flags | rule2->flags;
394 
395 	/* To be safe, lts use the max cac timeout of both rules */
396 	new_rule->dfs_cac_ms = max_t(u32, rule1->dfs_cac_ms,
397 				     rule2->dfs_cac_ms);
398 }
399 
400 static struct ieee80211_regdomain *
401 ath11k_regd_intersect(struct ieee80211_regdomain *default_regd,
402 		      struct ieee80211_regdomain *curr_regd)
403 {
404 	u8 num_old_regd_rules, num_curr_regd_rules, num_new_regd_rules;
405 	struct ieee80211_reg_rule *old_rule, *curr_rule, *new_rule;
406 	struct ieee80211_regdomain *new_regd = NULL;
407 	u8 i, j, k;
408 
409 	num_old_regd_rules = default_regd->n_reg_rules;
410 	num_curr_regd_rules = curr_regd->n_reg_rules;
411 	num_new_regd_rules = 0;
412 
413 	/* Find the number of intersecting rules to allocate new regd memory */
414 	for (i = 0; i < num_old_regd_rules; i++) {
415 		old_rule = default_regd->reg_rules + i;
416 		for (j = 0; j < num_curr_regd_rules; j++) {
417 			curr_rule = curr_regd->reg_rules + j;
418 
419 			if (ath11k_reg_can_intersect(old_rule, curr_rule))
420 				num_new_regd_rules++;
421 		}
422 	}
423 
424 	if (!num_new_regd_rules)
425 		return NULL;
426 
427 	new_regd = kzalloc(sizeof(*new_regd) + (num_new_regd_rules *
428 			sizeof(struct ieee80211_reg_rule)),
429 			GFP_ATOMIC);
430 
431 	if (!new_regd)
432 		return NULL;
433 
434 	/* We set the new country and dfs region directly and only trim
435 	 * the freq, power, antenna gain by intersecting with the
436 	 * default regdomain. Also MAX of the dfs cac timeout is selected.
437 	 */
438 	new_regd->n_reg_rules = num_new_regd_rules;
439 	memcpy(new_regd->alpha2, curr_regd->alpha2, sizeof(new_regd->alpha2));
440 	new_regd->dfs_region = curr_regd->dfs_region;
441 	new_rule = new_regd->reg_rules;
442 
443 	for (i = 0, k = 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 				ath11k_reg_intersect_rules(old_rule, curr_rule,
450 							   (new_rule + k++));
451 		}
452 	}
453 	return new_regd;
454 }
455 
456 static const char *
457 ath11k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
458 {
459 	switch (dfs_region) {
460 	case NL80211_DFS_FCC:
461 		return "FCC";
462 	case NL80211_DFS_ETSI:
463 		return "ETSI";
464 	case NL80211_DFS_JP:
465 		return "JP";
466 	default:
467 		return "UNSET";
468 	}
469 }
470 
471 static u16
472 ath11k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
473 {
474 	u16 bw;
475 
476 	if (end_freq <= start_freq)
477 		return 0;
478 
479 	bw = end_freq - start_freq;
480 	bw = min_t(u16, bw, max_bw);
481 
482 	if (bw >= 80 && bw < 160)
483 		bw = 80;
484 	else if (bw >= 40 && bw < 80)
485 		bw = 40;
486 	else if (bw >= 20 && bw < 40)
487 		bw = 20;
488 	else
489 		bw = 0;
490 
491 	return bw;
492 }
493 
494 static void
495 ath11k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
496 		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
497 		       u32 reg_flags)
498 {
499 	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
500 	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
501 	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
502 	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
503 	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
504 	reg_rule->flags = reg_flags;
505 }
506 
507 static void
508 ath11k_reg_update_weather_radar_band(struct ath11k_base *ab,
509 				     struct ieee80211_regdomain *regd,
510 				     struct cur_reg_rule *reg_rule,
511 				     u8 *rule_idx, u32 flags, u16 max_bw)
512 {
513 	u32 start_freq;
514 	u32 end_freq;
515 	u16 bw;
516 	u8 i;
517 
518 	i = *rule_idx;
519 
520 	/* there might be situations when even the input rule must be dropped */
521 	i--;
522 
523 	/* frequencies below weather radar */
524 	bw = ath11k_reg_adjust_bw(reg_rule->start_freq,
525 				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
526 	if (bw > 0) {
527 		i++;
528 
529 		ath11k_reg_update_rule(regd->reg_rules + i,
530 				       reg_rule->start_freq,
531 				       ETSI_WEATHER_RADAR_BAND_LOW, bw,
532 				       reg_rule->ant_gain, reg_rule->reg_power,
533 				       flags);
534 
535 		ath11k_dbg(ab, ATH11K_DBG_REG,
536 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
537 			   i + 1, reg_rule->start_freq,
538 			   ETSI_WEATHER_RADAR_BAND_LOW, bw, reg_rule->ant_gain,
539 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
540 			   flags);
541 	}
542 
543 	/* weather radar frequencies */
544 	start_freq = max_t(u32, reg_rule->start_freq,
545 			   ETSI_WEATHER_RADAR_BAND_LOW);
546 	end_freq = min_t(u32, reg_rule->end_freq, ETSI_WEATHER_RADAR_BAND_HIGH);
547 
548 	bw = ath11k_reg_adjust_bw(start_freq, end_freq, max_bw);
549 	if (bw > 0) {
550 		i++;
551 
552 		ath11k_reg_update_rule(regd->reg_rules + i, start_freq,
553 				       end_freq, bw, reg_rule->ant_gain,
554 				       reg_rule->reg_power, flags);
555 
556 		regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
557 
558 		ath11k_dbg(ab, ATH11K_DBG_REG,
559 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
560 			   i + 1, start_freq, end_freq, bw,
561 			   reg_rule->ant_gain, reg_rule->reg_power,
562 			   regd->reg_rules[i].dfs_cac_ms, flags);
563 	}
564 
565 	/* frequencies above weather radar */
566 	bw = ath11k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
567 				  reg_rule->end_freq, max_bw);
568 	if (bw > 0) {
569 		i++;
570 
571 		ath11k_reg_update_rule(regd->reg_rules + i,
572 				       ETSI_WEATHER_RADAR_BAND_HIGH,
573 				       reg_rule->end_freq, bw,
574 				       reg_rule->ant_gain, reg_rule->reg_power,
575 				       flags);
576 
577 		ath11k_dbg(ab, ATH11K_DBG_REG,
578 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
579 			   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH,
580 			   reg_rule->end_freq, bw, reg_rule->ant_gain,
581 			   reg_rule->reg_power, regd->reg_rules[i].dfs_cac_ms,
582 			   flags);
583 	}
584 
585 	*rule_idx = i;
586 }
587 
588 struct ieee80211_regdomain *
589 ath11k_reg_build_regd(struct ath11k_base *ab,
590 		      struct cur_regulatory_info *reg_info, bool intersect)
591 {
592 	struct ieee80211_regdomain *tmp_regd, *default_regd, *new_regd = NULL;
593 	struct cur_reg_rule *reg_rule;
594 	u8 i = 0, j = 0;
595 	u8 num_rules;
596 	u16 max_bw;
597 	u32 flags;
598 	char alpha2[3];
599 
600 	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
601 
602 	if (!num_rules)
603 		goto ret;
604 
605 	/* Add max additional rules to accommodate weather radar band */
606 	if (reg_info->dfs_region == ATH11K_DFS_REG_ETSI)
607 		num_rules += 2;
608 
609 	tmp_regd =  kzalloc(sizeof(*tmp_regd) +
610 			(num_rules * sizeof(struct ieee80211_reg_rule)),
611 			GFP_ATOMIC);
612 	if (!tmp_regd)
613 		goto ret;
614 
615 	memcpy(tmp_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
616 	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
617 	alpha2[2] = '\0';
618 	tmp_regd->dfs_region = ath11k_map_fw_dfs_region(reg_info->dfs_region);
619 
620 	ath11k_dbg(ab, ATH11K_DBG_REG,
621 		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
622 		   alpha2, ath11k_reg_get_regdom_str(tmp_regd->dfs_region),
623 		   reg_info->dfs_region, num_rules);
624 	/* Update reg_rules[] below. Firmware is expected to
625 	 * send these rules in order(2G rules first and then 5G)
626 	 */
627 	for (; i < num_rules; i++) {
628 		if (reg_info->num_2g_reg_rules &&
629 		    (i < reg_info->num_2g_reg_rules)) {
630 			reg_rule = reg_info->reg_rules_2g_ptr + i;
631 			max_bw = min_t(u16, reg_rule->max_bw,
632 				       reg_info->max_bw_2g);
633 			flags = 0;
634 		} else if (reg_info->num_5g_reg_rules &&
635 			   (j < reg_info->num_5g_reg_rules)) {
636 			reg_rule = reg_info->reg_rules_5g_ptr + j++;
637 			max_bw = min_t(u16, reg_rule->max_bw,
638 				       reg_info->max_bw_5g);
639 
640 			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
641 			 * BW Auto correction, we can enable this by default
642 			 * for all 5G rules here. The regulatory core performs
643 			 * BW correction if required and applies flags as
644 			 * per other BW rule flags we pass from here
645 			 */
646 			flags = NL80211_RRF_AUTO_BW;
647 		} else {
648 			break;
649 		}
650 
651 		flags |= ath11k_map_fw_reg_flags(reg_rule->flags);
652 
653 		ath11k_reg_update_rule(tmp_regd->reg_rules + i,
654 				       reg_rule->start_freq,
655 				       reg_rule->end_freq, max_bw,
656 				       reg_rule->ant_gain, reg_rule->reg_power,
657 				       flags);
658 
659 		/* Update dfs cac timeout if the dfs domain is ETSI and the
660 		 * new rule covers weather radar band.
661 		 * Default value of '0' corresponds to 60s timeout, so no
662 		 * need to update that for other rules.
663 		 */
664 		if (flags & NL80211_RRF_DFS &&
665 		    reg_info->dfs_region == ATH11K_DFS_REG_ETSI &&
666 		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
667 		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
668 			ath11k_reg_update_weather_radar_band(ab, tmp_regd,
669 							     reg_rule, &i,
670 							     flags, max_bw);
671 			continue;
672 		}
673 
674 		ath11k_dbg(ab, ATH11K_DBG_REG,
675 			   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
676 			   i + 1, reg_rule->start_freq, reg_rule->end_freq,
677 			   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
678 			   tmp_regd->reg_rules[i].dfs_cac_ms,
679 			   flags);
680 	}
681 
682 	tmp_regd->n_reg_rules = i;
683 
684 	if (intersect) {
685 		default_regd = ab->default_regd[reg_info->phy_id];
686 
687 		/* Get a new regd by intersecting the received regd with
688 		 * our default regd.
689 		 */
690 		new_regd = ath11k_regd_intersect(default_regd, tmp_regd);
691 		kfree(tmp_regd);
692 		if (!new_regd) {
693 			ath11k_warn(ab, "Unable to create intersected regdomain\n");
694 			goto ret;
695 		}
696 	} else {
697 		new_regd = tmp_regd;
698 	}
699 
700 ret:
701 	return new_regd;
702 }
703 
704 void ath11k_regd_update_work(struct work_struct *work)
705 {
706 	struct ath11k *ar = container_of(work, struct ath11k,
707 					 regd_update_work);
708 	int ret;
709 
710 	ret = ath11k_regd_update(ar);
711 	if (ret) {
712 		/* Firmware has already moved to the new regd. We need
713 		 * to maintain channel consistency across FW, Host driver
714 		 * and userspace. Hence as a fallback mechanism we can set
715 		 * the prev or default country code to the firmware.
716 		 */
717 		/* TODO: Implement Fallback Mechanism */
718 	}
719 }
720 
721 void ath11k_reg_init(struct ath11k *ar)
722 {
723 	ar->hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
724 	ar->hw->wiphy->reg_notifier = ath11k_reg_notifier;
725 }
726 
727 void ath11k_reg_free(struct ath11k_base *ab)
728 {
729 	int i;
730 
731 	for (i = 0; i < ab->hw_params.max_radios; i++) {
732 		kfree(ab->default_regd[i]);
733 		kfree(ab->new_regd[i]);
734 	}
735 }
736