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