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