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