File Index
Files and directories
Sign in
FILE

reg.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/ath/ath12k/reg.c C
  1// SPDX-License-Identifier: BSD-3-Clause-Clear
  2/*
  3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
  4 * Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved.
  5 */
  6#include <linux/rtnetlink.h>
  7#include "core.h"
  8#include "debug.h"
  9#include "mac.h"
 10
 11/* World regdom to be used in case default regd from fw is unavailable */
 12#define ATH12K_2GHZ_CH01_11      REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0)
 13#define ATH12K_5GHZ_5150_5350    REG_RULE(5150 - 10, 5350 + 10, 80, 0, 30,\
 14					  NL80211_RRF_NO_IR)
 15#define ATH12K_5GHZ_5725_5850    REG_RULE(5725 - 10, 5850 + 10, 80, 0, 30,\
 16					  NL80211_RRF_NO_IR)
 17
 18#define ETSI_WEATHER_RADAR_BAND_LOW		5590
 19#define ETSI_WEATHER_RADAR_BAND_HIGH		5650
 20#define ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT	600000
 21
 22static const struct ieee80211_regdomain ath12k_world_regd = {
 23	.n_reg_rules = 3,
 24	.alpha2 = "00",
 25	.reg_rules = {
 26		ATH12K_2GHZ_CH01_11,
 27		ATH12K_5GHZ_5150_5350,
 28		ATH12K_5GHZ_5725_5850,
 29	}
 30};
 31
 32static bool ath12k_regdom_changes(struct ieee80211_hw *hw, char *alpha2)
 33{
 34	const struct ieee80211_regdomain *regd;
 35
 36	regd = rcu_dereference_rtnl(hw->wiphy->regd);
 37	/* This can happen during wiphy registration where the previous
 38	 * user request is received before we update the regd received
 39	 * from firmware.
 40	 */
 41	if (!regd)
 42		return true;
 43
 44	return memcmp(regd->alpha2, alpha2, 2) != 0;
 45}
 46
 47static void
 48ath12k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
 49{
 50	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
 51	struct ath12k_wmi_init_country_arg arg;
 52	struct wmi_set_current_country_arg current_arg = {};
 53	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 54	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
 55	int ret, i;
 56
 57	ath12k_dbg(ar->ab, ATH12K_DBG_REG,
 58		   "Regulatory Notification received for %s\n", wiphy_name(wiphy));
 59
 60	if (request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
 61		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
 62			   "driver initiated regd update\n");
 63		if (ah->state != ATH12K_HW_STATE_ON)
 64			return;
 65
 66		for_each_ar(ah, ar, i) {
 67			ret = ath12k_reg_update_chan_list(ar, true);
 68			if (ret && ret != -EINVAL) {
 69				ath12k_warn(ar->ab,
 70					    "failed to update chan list for pdev %u, ret %d\n",
 71					    i, ret);
 72				break;
 73			}
 74		}
 75		return;
 76	}
 77
 78	/* Currently supporting only General User Hints. Cell base user
 79	 * hints to be handled later.
 80	 * Hints from other sources like Core, Beacons are not expected for
 81	 * self managed wiphy's
 82	 */
 83	if (!(request->initiator == NL80211_REGDOM_SET_BY_USER &&
 84	      request->user_reg_hint_type == NL80211_USER_REG_HINT_USER)) {
 85		ath12k_warn(ar->ab, "Unexpected Regulatory event for this wiphy\n");
 86		return;
 87	}
 88
 89	if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS)) {
 90		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
 91			   "Country Setting is not allowed\n");
 92		return;
 93	}
 94
 95	if (!ath12k_regdom_changes(hw, request->alpha2)) {
 96		ath12k_dbg(ar->ab, ATH12K_DBG_REG, "Country is already set\n");
 97		return;
 98	}
 99
100	/* Allow fresh updates to wiphy regd */
101	ah->regd_updated = false;
102
103	/* Send the reg change request to all the radios */
104	for_each_ar(ah, ar, i) {
105		reinit_completion(&ar->regd_update_completed);
106
107		if (ar->ab->hw_params->current_cc_support) {
108			memcpy(&current_arg.alpha2, request->alpha2, 2);
109			memcpy(&ar->alpha2, &current_arg.alpha2, 2);
110			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_arg);
111			if (ret)
112				ath12k_warn(ar->ab,
113					    "failed set current country code: %d\n", ret);
114		} else {
115			arg.flags = ALPHA_IS_SET;
116			memcpy(&arg.cc_info.alpha2, request->alpha2, 2);
117			arg.cc_info.alpha2[2] = 0;
118
119			ret = ath12k_wmi_send_init_country_cmd(ar, &arg);
120			if (ret)
121				ath12k_warn(ar->ab,
122					    "failed set INIT Country code: %d\n", ret);
123		}
124
125		wiphy_lock(wiphy);
126		ath12k_mac_11d_scan_stop(ar);
127		wiphy_unlock(wiphy);
128
129		ar->regdom_set_by_user = true;
130	}
131}
132
133int ath12k_reg_update_chan_list(struct ath12k *ar, bool wait)
134{
135	struct ieee80211_supported_band **bands;
136	struct ath12k_wmi_scan_chan_list_arg *arg;
137	struct ieee80211_channel *channel;
138	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
139	struct ath12k_wmi_channel_arg *ch;
140	enum nl80211_band band;
141	int num_channels = 0;
142	int i, ret = 0;
143
144	if (ar->ah->state == ATH12K_HW_STATE_RESTARTING)
145		return 0;
146
147	bands = hw->wiphy->bands;
148	for (band = 0; band < NUM_NL80211_BANDS; band++) {
149		if (!(ar->mac.sbands[band].channels && bands[band]))
150			continue;
151
152		for (i = 0; i < bands[band]->n_channels; i++) {
153			if (bands[band]->channels[i].flags &
154			    IEEE80211_CHAN_DISABLED)
155				continue;
156			/* Skip Channels that are not in current radio's range */
157			if (bands[band]->channels[i].center_freq <
158			    KHZ_TO_MHZ(ar->freq_range.start_freq) ||
159			    bands[band]->channels[i].center_freq >
160			    KHZ_TO_MHZ(ar->freq_range.end_freq))
161				continue;
162
163			num_channels++;
164		}
165	}
166
167	if (!num_channels) {
168		ath12k_dbg(ar->ab, ATH12K_DBG_REG,
169			   "pdev is not supported for this country\n");
170		return -EINVAL;
171	}
172
173	arg = kzalloc(struct_size(arg, channel, num_channels), GFP_KERNEL);
174
175	if (!arg)
176		return -ENOMEM;
177
178	arg->pdev_id = ar->pdev->pdev_id;
179	arg->nallchans = num_channels;
180
181	ch = arg->channel;
182
183	for (band = 0; band < NUM_NL80211_BANDS; band++) {
184		if (!(ar->mac.sbands[band].channels && bands[band]))
185			continue;
186
187		for (i = 0; i < bands[band]->n_channels; i++) {
188			channel = &bands[band]->channels[i];
189
190			if (channel->flags & IEEE80211_CHAN_DISABLED)
191				continue;
192
193			/* Skip Channels that are not in current radio's range */
194			if (bands[band]->channels[i].center_freq <
195			    KHZ_TO_MHZ(ar->freq_range.start_freq) ||
196			    bands[band]->channels[i].center_freq >
197			    KHZ_TO_MHZ(ar->freq_range.end_freq))
198				continue;
199
200			/* TODO: Set to true/false based on some condition? */
201			ch->allow_ht = true;
202			ch->allow_vht = true;
203			ch->allow_he = true;
204
205			ch->dfs_set =
206				!!(channel->flags & IEEE80211_CHAN_RADAR);
207			ch->is_chan_passive = !!(channel->flags &
208						IEEE80211_CHAN_NO_IR);
209			ch->is_chan_passive |= ch->dfs_set;
210			ch->mhz = channel->center_freq;
211			ch->cfreq1 = channel->center_freq;
212			ch->minpower = 0;
213			ch->maxpower = channel->max_power * 2;
214			ch->maxregpower = channel->max_reg_power * 2;
215			ch->antennamax = channel->max_antenna_gain * 2;
216
217			/* TODO: Use appropriate phymodes */
218			if (channel->band == NL80211_BAND_2GHZ)
219				ch->phy_mode = MODE_11G;
220			else
221				ch->phy_mode = MODE_11A;
222
223			if (channel->band == NL80211_BAND_6GHZ &&
224			    cfg80211_channel_is_psc(channel))
225				ch->psc_channel = true;
226
227			ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
228				   "mac channel [%d/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
229				   i, arg->nallchans,
230				   ch->mhz, ch->maxpower, ch->maxregpower,
231				   ch->antennamax, ch->phy_mode);
232
233			ch++;
234			/* TODO: use quarrter/half rate, cfreq12, dfs_cfreq2
235			 * set_agile, reg_class_idx
236			 */
237		}
238	}
239
240	if (wait) {
241		spin_lock_bh(&ar->data_lock);
242		list_add_tail(&arg->list, &ar->regd_channel_update_queue);
243		spin_unlock_bh(&ar->data_lock);
244
245		queue_work(ar->ab->workqueue, &ar->regd_channel_update_work);
246
247		return 0;
248	}
249
250	ret = ath12k_wmi_send_scan_chan_list_cmd(ar, arg);
251	kfree(arg);
252
253	return ret;
254}
255
256static void ath12k_copy_regd(struct ieee80211_regdomain *regd_orig,
257			     struct ieee80211_regdomain *regd_copy)
258{
259	u8 i;
260
261	/* The caller should have checked error conditions */
262	memcpy(regd_copy, regd_orig, sizeof(*regd_orig));
263
264	for (i = 0; i < regd_orig->n_reg_rules; i++)
265		memcpy(&regd_copy->reg_rules[i], &regd_orig->reg_rules[i],
266		       sizeof(struct ieee80211_reg_rule));
267}
268
269int ath12k_regd_update(struct ath12k *ar, bool init)
270{
271	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
272	u32 phy_id, freq_low, freq_high, supported_bands;
273	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
274	struct ieee80211_hw *hw = ah->hw;
275	struct ieee80211_regdomain *regd, *regd_copy = NULL;
276	int ret, regd_len, pdev_id;
277	struct ath12k_base *ab;
278	long time_left;
279
280	ab = ar->ab;
281
282	time_left = wait_for_completion_timeout(&ar->regd_update_completed,
283						ATH12K_REG_UPDATE_TIMEOUT_HZ);
284	if (time_left == 0) {
285		ath12k_warn(ab, "Timeout while waiting for regulatory update");
286		/* Even though timeout has occurred, still continue since at least boot
287		 * time data would be there to process
288		 */
289	}
290
291	supported_bands = ar->pdev->cap.supported_bands;
292	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
293
294	/* Possible that due to reg change, current limits for supported
295	 * frequency changed. Update it. As a first step, reset the
296	 * previous values and then compute and set the new values.
297	 */
298	ar->freq_range.start_freq = 0;
299	ar->freq_range.end_freq = 0;
300
301	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
302		if (ab->hw_params->single_pdev_only) {
303			phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_2GHZ_CAP].phy_id;
304			reg_cap = &ab->hal_reg_cap[phy_id];
305		}
306
307		freq_low = max(reg_cap->low_2ghz_chan, ab->reg_freq_2ghz.start_freq);
308		freq_high = min(reg_cap->high_2ghz_chan, ab->reg_freq_2ghz.end_freq);
309
310		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
311	}
312
313	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && !ar->supports_6ghz) {
314		if (ab->hw_params->single_pdev_only) {
315			phy_id = ar->pdev->cap.band[WMI_HOST_WLAN_5GHZ_CAP].phy_id;
316			reg_cap = &ab->hal_reg_cap[phy_id];
317		}
318
319		freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_5ghz.start_freq);
320		freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_5ghz.end_freq);
321
322		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
323	}
324
325	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP && ar->supports_6ghz) {
326		freq_low = max(reg_cap->low_5ghz_chan, ab->reg_freq_6ghz.start_freq);
327		freq_high = min(reg_cap->high_5ghz_chan, ab->reg_freq_6ghz.end_freq);
328
329		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
330	}
331
332	/* If one of the radios within ah has already updated the regd for
333	 * the wiphy, then avoid setting regd again
334	 */
335	if (ah->regd_updated)
336		return 0;
337
338	/* firmware provides reg rules which are similar for 2 GHz and 5 GHz
339	 * pdev but 6 GHz pdev has superset of all rules including rules for
340	 * all bands, we prefer 6 GHz pdev's rules to be used for setup of
341	 * the wiphy regd.
342	 * If 6 GHz pdev was part of the ath12k_hw, wait for the 6 GHz pdev,
343	 * else pick the first pdev which calls this function and use its
344	 * regd to update global hw regd.
345	 * The regd_updated flag set at the end will not allow any further
346	 * updates.
347	 */
348	if (ah->use_6ghz_regd && !ar->supports_6ghz)
349		return 0;
350
351	pdev_id = ar->pdev_idx;
352
353	spin_lock_bh(&ab->base_lock);
354
355	if (init) {
356		/* Apply the regd received during init through
357		 * WMI_REG_CHAN_LIST_CC event. In case of failure to
358		 * receive the regd, initialize with a default world
359		 * regulatory.
360		 */
361		if (ab->default_regd[pdev_id]) {
362			regd = ab->default_regd[pdev_id];
363		} else {
364			ath12k_warn(ab,
365				    "failed to receive default regd during init\n");
366			regd = (struct ieee80211_regdomain *)&ath12k_world_regd;
367		}
368	} else {
369		regd = ab->new_regd[pdev_id];
370	}
371
372	if (!regd) {
373		ret = -EINVAL;
374		spin_unlock_bh(&ab->base_lock);
375		goto err;
376	}
377
378	regd_len = sizeof(*regd) + (regd->n_reg_rules *
379		sizeof(struct ieee80211_reg_rule));
380
381	regd_copy = kzalloc(regd_len, GFP_ATOMIC);
382	if (regd_copy)
383		ath12k_copy_regd(regd, regd_copy);
384
385	spin_unlock_bh(&ab->base_lock);
386
387	if (!regd_copy) {
388		ret = -ENOMEM;
389		goto err;
390	}
391
392	ret = regulatory_set_wiphy_regd(hw->wiphy, regd_copy);
393
394	kfree(regd_copy);
395
396	if (ret)
397		goto err;
398
399	if (ah->state != ATH12K_HW_STATE_ON)
400		goto skip;
401
402	ah->regd_updated = true;
403
404skip:
405	return 0;
406err:
407	ath12k_warn(ab, "failed to perform regd update : %d\n", ret);
408	return ret;
409}
410
411static enum nl80211_dfs_regions
412ath12k_map_fw_dfs_region(enum ath12k_dfs_region dfs_region)
413{
414	switch (dfs_region) {
415	case ATH12K_DFS_REG_FCC:
416	case ATH12K_DFS_REG_CN:
417		return NL80211_DFS_FCC;
418	case ATH12K_DFS_REG_ETSI:
419	case ATH12K_DFS_REG_KR:
420		return NL80211_DFS_ETSI;
421	case ATH12K_DFS_REG_MKK:
422	case ATH12K_DFS_REG_MKK_N:
423		return NL80211_DFS_JP;
424	default:
425		return NL80211_DFS_UNSET;
426	}
427}
428
429static u32 ath12k_get_bw_reg_flags(u16 max_bw)
430{
431	switch (max_bw) {
432	case 20:
433		return NL80211_RRF_NO_HT40 |
434			NL80211_RRF_NO_80MHZ |
435			NL80211_RRF_NO_160MHZ |
436			NL80211_RRF_NO_320MHZ;
437	case 40:
438		return NL80211_RRF_NO_80MHZ |
439			NL80211_RRF_NO_160MHZ |
440			NL80211_RRF_NO_320MHZ;
441	case 80:
442		return NL80211_RRF_NO_160MHZ |
443			NL80211_RRF_NO_320MHZ;
444	case 160:
445		return NL80211_RRF_NO_320MHZ;
446	case 320:
447	default:
448		return 0;
449	}
450}
451
452static u32 ath12k_map_fw_reg_flags(u16 reg_flags)
453{
454	u32 flags = 0;
455
456	if (reg_flags & REGULATORY_CHAN_NO_IR)
457		flags = NL80211_RRF_NO_IR;
458
459	if (reg_flags & REGULATORY_CHAN_RADAR)
460		flags |= NL80211_RRF_DFS;
461
462	if (reg_flags & REGULATORY_CHAN_NO_OFDM)
463		flags |= NL80211_RRF_NO_OFDM;
464
465	if (reg_flags & REGULATORY_CHAN_INDOOR_ONLY)
466		flags |= NL80211_RRF_NO_OUTDOOR;
467
468	if (reg_flags & REGULATORY_CHAN_NO_HT40)
469		flags |= NL80211_RRF_NO_HT40;
470
471	if (reg_flags & REGULATORY_CHAN_NO_80MHZ)
472		flags |= NL80211_RRF_NO_80MHZ;
473
474	if (reg_flags & REGULATORY_CHAN_NO_160MHZ)
475		flags |= NL80211_RRF_NO_160MHZ;
476
477	return flags;
478}
479
480static u32 ath12k_map_fw_phy_flags(u32 phy_flags)
481{
482	u32 flags = 0;
483
484	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11AX)
485		flags |= NL80211_RRF_NO_HE;
486
487	if (phy_flags & ATH12K_REG_PHY_BITMAP_NO11BE)
488		flags |= NL80211_RRF_NO_EHT;
489
490	return flags;
491}
492
493static const char *
494ath12k_reg_get_regdom_str(enum nl80211_dfs_regions dfs_region)
495{
496	switch (dfs_region) {
497	case NL80211_DFS_FCC:
498		return "FCC";
499	case NL80211_DFS_ETSI:
500		return "ETSI";
501	case NL80211_DFS_JP:
502		return "JP";
503	default:
504		return "UNSET";
505	}
506}
507
508static u16
509ath12k_reg_adjust_bw(u16 start_freq, u16 end_freq, u16 max_bw)
510{
511	u16 bw;
512
513	bw = end_freq - start_freq;
514	bw = min_t(u16, bw, max_bw);
515
516	if (bw >= 80 && bw < 160)
517		bw = 80;
518	else if (bw >= 40 && bw < 80)
519		bw = 40;
520	else if (bw < 40)
521		bw = 20;
522
523	return bw;
524}
525
526static void
527ath12k_reg_update_rule(struct ieee80211_reg_rule *reg_rule, u32 start_freq,
528		       u32 end_freq, u32 bw, u32 ant_gain, u32 reg_pwr,
529		       s8 psd, u32 reg_flags)
530{
531	reg_rule->freq_range.start_freq_khz = MHZ_TO_KHZ(start_freq);
532	reg_rule->freq_range.end_freq_khz = MHZ_TO_KHZ(end_freq);
533	reg_rule->freq_range.max_bandwidth_khz = MHZ_TO_KHZ(bw);
534	reg_rule->power_rule.max_antenna_gain = DBI_TO_MBI(ant_gain);
535	reg_rule->power_rule.max_eirp = DBM_TO_MBM(reg_pwr);
536	reg_rule->psd = psd;
537	reg_rule->flags = reg_flags;
538}
539
540static void
541ath12k_reg_update_weather_radar_band(struct ath12k_base *ab,
542				     struct ieee80211_regdomain *regd,
543				     struct ath12k_reg_rule *reg_rule,
544				     u8 *rule_idx, u32 flags, u16 max_bw)
545{
546	u32 end_freq;
547	u16 bw;
548	u8 i;
549
550	i = *rule_idx;
551
552	bw = ath12k_reg_adjust_bw(reg_rule->start_freq,
553				  ETSI_WEATHER_RADAR_BAND_LOW, max_bw);
554
555	ath12k_reg_update_rule(regd->reg_rules + i, reg_rule->start_freq,
556			       ETSI_WEATHER_RADAR_BAND_LOW, bw,
557			       reg_rule->ant_gain, reg_rule->reg_power,
558			       reg_rule->psd_eirp, flags);
559
560	ath12k_dbg(ab, ATH12K_DBG_REG,
561		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
562		   i + 1, reg_rule->start_freq, ETSI_WEATHER_RADAR_BAND_LOW,
563		   bw, reg_rule->ant_gain, reg_rule->reg_power,
564		   regd->reg_rules[i].dfs_cac_ms,
565		   flags);
566
567	if (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_HIGH)
568		end_freq = ETSI_WEATHER_RADAR_BAND_HIGH;
569	else
570		end_freq = reg_rule->end_freq;
571
572	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
573				  max_bw);
574
575	i++;
576
577	ath12k_reg_update_rule(regd->reg_rules + i,
578			       ETSI_WEATHER_RADAR_BAND_LOW, end_freq, bw,
579			       reg_rule->ant_gain, reg_rule->reg_power,
580			       reg_rule->psd_eirp, flags);
581
582	regd->reg_rules[i].dfs_cac_ms = ETSI_WEATHER_RADAR_BAND_CAC_TIMEOUT;
583
584	ath12k_dbg(ab, ATH12K_DBG_REG,
585		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
586		   i + 1, ETSI_WEATHER_RADAR_BAND_LOW, end_freq,
587		   bw, reg_rule->ant_gain, reg_rule->reg_power,
588		   regd->reg_rules[i].dfs_cac_ms,
589		   flags);
590
591	if (end_freq == reg_rule->end_freq) {
592		regd->n_reg_rules--;
593		*rule_idx = i;
594		return;
595	}
596
597	bw = ath12k_reg_adjust_bw(ETSI_WEATHER_RADAR_BAND_HIGH,
598				  reg_rule->end_freq, max_bw);
599
600	i++;
601
602	ath12k_reg_update_rule(regd->reg_rules + i, ETSI_WEATHER_RADAR_BAND_HIGH,
603			       reg_rule->end_freq, bw,
604			       reg_rule->ant_gain, reg_rule->reg_power,
605			       reg_rule->psd_eirp, flags);
606
607	ath12k_dbg(ab, ATH12K_DBG_REG,
608		   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
609		   i + 1, ETSI_WEATHER_RADAR_BAND_HIGH, reg_rule->end_freq,
610		   bw, reg_rule->ant_gain, reg_rule->reg_power,
611		   regd->reg_rules[i].dfs_cac_ms,
612		   flags);
613
614	*rule_idx = i;
615}
616
617static void ath12k_reg_update_freq_range(struct ath12k_reg_freq *reg_freq,
618					 struct ath12k_reg_rule *reg_rule)
619{
620	if (reg_freq->start_freq > reg_rule->start_freq)
621		reg_freq->start_freq = reg_rule->start_freq;
622
623	if (reg_freq->end_freq < reg_rule->end_freq)
624		reg_freq->end_freq = reg_rule->end_freq;
625}
626
627enum wmi_reg_6g_ap_type
628ath12k_reg_ap_pwr_convert(enum ieee80211_ap_reg_power power_type)
629{
630	switch (power_type) {
631	case IEEE80211_REG_LPI_AP:
632		return WMI_REG_INDOOR_AP;
633	case IEEE80211_REG_SP_AP:
634		return WMI_REG_STD_POWER_AP;
635	case IEEE80211_REG_VLP_AP:
636		return WMI_REG_VLP_AP;
637	default:
638		return WMI_REG_MAX_AP_TYPE;
639	}
640}
641
642struct ieee80211_regdomain *
643ath12k_reg_build_regd(struct ath12k_base *ab,
644		      struct ath12k_reg_info *reg_info,
645		      enum wmi_vdev_type vdev_type,
646		      enum ieee80211_ap_reg_power power_type)
647{
648	struct ieee80211_regdomain *new_regd = NULL;
649	struct ath12k_reg_rule *reg_rule, *reg_rule_6ghz;
650	u32 flags, reg_6ghz_number, max_bw_6ghz;
651	u8 i = 0, j = 0, k = 0;
652	u8 num_rules;
653	u16 max_bw;
654	char alpha2[3];
655
656	num_rules = reg_info->num_5g_reg_rules + reg_info->num_2g_reg_rules;
657
658	if (reg_info->is_ext_reg_event) {
659		if (vdev_type == WMI_VDEV_TYPE_STA) {
660			enum wmi_reg_6g_ap_type ap_type;
661
662			ap_type = ath12k_reg_ap_pwr_convert(power_type);
663			if (ap_type == WMI_REG_MAX_AP_TYPE)
664				ap_type = WMI_REG_INDOOR_AP;
665
666			reg_6ghz_number = reg_info->num_6g_reg_rules_cl
667					[ap_type][WMI_REG_DEFAULT_CLIENT];
668			if (reg_6ghz_number == 0) {
669				ap_type = WMI_REG_INDOOR_AP;
670				reg_6ghz_number = reg_info->num_6g_reg_rules_cl
671						[ap_type][WMI_REG_DEFAULT_CLIENT];
672			}
673
674			reg_rule_6ghz = reg_info->reg_rules_6g_client_ptr
675					[ap_type][WMI_REG_DEFAULT_CLIENT];
676			max_bw_6ghz = reg_info->max_bw_6g_client
677					[ap_type][WMI_REG_DEFAULT_CLIENT];
678		} else {
679			reg_6ghz_number = reg_info->num_6g_reg_rules_ap
680						[WMI_REG_INDOOR_AP];
681			reg_rule_6ghz =
682				reg_info->reg_rules_6g_ap_ptr[WMI_REG_INDOOR_AP];
683			max_bw_6ghz = reg_info->max_bw_6g_ap[WMI_REG_INDOOR_AP];
684		}
685
686		num_rules += reg_6ghz_number;
687	}
688
689	if (!num_rules)
690		goto ret;
691
692	/* Add max additional rules to accommodate weather radar band */
693	if (reg_info->dfs_region == ATH12K_DFS_REG_ETSI)
694		num_rules += 2;
695
696	new_regd = kzalloc(sizeof(*new_regd) +
697			   (num_rules * sizeof(struct ieee80211_reg_rule)),
698			   GFP_ATOMIC);
699	if (!new_regd)
700		goto ret;
701
702	memcpy(new_regd->alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
703	memcpy(alpha2, reg_info->alpha2, REG_ALPHA2_LEN + 1);
704	alpha2[2] = '\0';
705	new_regd->dfs_region = ath12k_map_fw_dfs_region(reg_info->dfs_region);
706
707	ath12k_dbg(ab, ATH12K_DBG_REG,
708		   "\r\nCountry %s, CFG Regdomain %s FW Regdomain %d, num_reg_rules %d\n",
709		   alpha2, ath12k_reg_get_regdom_str(new_regd->dfs_region),
710		   reg_info->dfs_region, num_rules);
711
712	/* Reset start and end frequency for each band
713	 */
714	ab->reg_freq_5ghz.start_freq = INT_MAX;
715	ab->reg_freq_5ghz.end_freq = 0;
716	ab->reg_freq_2ghz.start_freq = INT_MAX;
717	ab->reg_freq_2ghz.end_freq = 0;
718	ab->reg_freq_6ghz.start_freq = INT_MAX;
719	ab->reg_freq_6ghz.end_freq = 0;
720
721	/* Update reg_rules[] below. Firmware is expected to
722	 * send these rules in order(2G rules first and then 5G)
723	 */
724	for (; i < num_rules; i++) {
725		if (reg_info->num_2g_reg_rules &&
726		    (i < reg_info->num_2g_reg_rules)) {
727			reg_rule = reg_info->reg_rules_2g_ptr + i;
728			max_bw = min_t(u16, reg_rule->max_bw,
729				       reg_info->max_bw_2g);
730			flags = ath12k_get_bw_reg_flags(reg_info->max_bw_2g);
731			ath12k_reg_update_freq_range(&ab->reg_freq_2ghz, reg_rule);
732		} else if (reg_info->num_5g_reg_rules &&
733			   (j < reg_info->num_5g_reg_rules)) {
734			reg_rule = reg_info->reg_rules_5g_ptr + j++;
735			max_bw = min_t(u16, reg_rule->max_bw,
736				       reg_info->max_bw_5g);
737
738			/* FW doesn't pass NL80211_RRF_AUTO_BW flag for
739			 * BW Auto correction, we can enable this by default
740			 * for all 5G rules here. The regulatory core performs
741			 * BW correction if required and applies flags as
742			 * per other BW rule flags we pass from here
743			 */
744			flags = NL80211_RRF_AUTO_BW |
745				ath12k_get_bw_reg_flags(reg_info->max_bw_5g);
746			ath12k_reg_update_freq_range(&ab->reg_freq_5ghz, reg_rule);
747		} else if (reg_info->is_ext_reg_event && reg_6ghz_number &&
748			   (k < reg_6ghz_number)) {
749			reg_rule = reg_rule_6ghz + k++;
750			max_bw = min_t(u16, reg_rule->max_bw, max_bw_6ghz);
751			flags = NL80211_RRF_AUTO_BW |
752				ath12k_get_bw_reg_flags(max_bw_6ghz);
753			if (reg_rule->psd_flag)
754				flags |= NL80211_RRF_PSD;
755			ath12k_reg_update_freq_range(&ab->reg_freq_6ghz, reg_rule);
756		} else {
757			break;
758		}
759
760		flags |= ath12k_map_fw_reg_flags(reg_rule->flags);
761		flags |= ath12k_map_fw_phy_flags(reg_info->phybitmap);
762
763		ath12k_reg_update_rule(new_regd->reg_rules + i,
764				       reg_rule->start_freq,
765				       reg_rule->end_freq, max_bw,
766				       reg_rule->ant_gain, reg_rule->reg_power,
767				       reg_rule->psd_eirp, flags);
768
769		/* Update dfs cac timeout if the dfs domain is ETSI and the
770		 * new rule covers weather radar band.
771		 * Default value of '0' corresponds to 60s timeout, so no
772		 * need to update that for other rules.
773		 */
774		if (flags & NL80211_RRF_DFS &&
775		    reg_info->dfs_region == ATH12K_DFS_REG_ETSI &&
776		    (reg_rule->end_freq > ETSI_WEATHER_RADAR_BAND_LOW &&
777		    reg_rule->start_freq < ETSI_WEATHER_RADAR_BAND_HIGH)){
778			ath12k_reg_update_weather_radar_band(ab, new_regd,
779							     reg_rule, &i,
780							     flags, max_bw);
781			continue;
782		}
783
784		if (reg_info->is_ext_reg_event) {
785			ath12k_dbg(ab, ATH12K_DBG_REG, "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d) (%d, %d)\n",
786				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
787				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
788				   new_regd->reg_rules[i].dfs_cac_ms,
789				   flags, reg_rule->psd_flag, reg_rule->psd_eirp);
790		} else {
791			ath12k_dbg(ab, ATH12K_DBG_REG,
792				   "\t%d. (%d - %d @ %d) (%d, %d) (%d ms) (FLAGS %d)\n",
793				   i + 1, reg_rule->start_freq, reg_rule->end_freq,
794				   max_bw, reg_rule->ant_gain, reg_rule->reg_power,
795				   new_regd->reg_rules[i].dfs_cac_ms,
796				   flags);
797		}
798	}
799
800	new_regd->n_reg_rules = i;
801ret:
802	return new_regd;
803}
804
805void ath12k_regd_update_chan_list_work(struct work_struct *work)
806{
807	struct ath12k *ar = container_of(work, struct ath12k,
808					 regd_channel_update_work);
809	struct ath12k_wmi_scan_chan_list_arg *arg;
810	struct list_head local_update_list;
811	int left;
812
813	INIT_LIST_HEAD(&local_update_list);
814
815	spin_lock_bh(&ar->data_lock);
816	list_splice_tail_init(&ar->regd_channel_update_queue, &local_update_list);
817	spin_unlock_bh(&ar->data_lock);
818
819	while ((arg = list_first_entry_or_null(&local_update_list,
820					       struct ath12k_wmi_scan_chan_list_arg,
821					       list))) {
822		if (ar->state_11d != ATH12K_11D_IDLE) {
823			left = wait_for_completion_timeout(&ar->completed_11d_scan,
824							   ATH12K_SCAN_TIMEOUT_HZ);
825			if (!left) {
826				ath12k_dbg(ar->ab, ATH12K_DBG_REG,
827					   "failed to receive 11d scan complete: timed out\n");
828				ar->state_11d = ATH12K_11D_IDLE;
829			}
830
831			ath12k_dbg(ar->ab, ATH12K_DBG_REG,
832				   "reg 11d scan wait left time %d\n", left);
833		}
834
835		if ((ar->scan.state == ATH12K_SCAN_STARTING ||
836		     ar->scan.state == ATH12K_SCAN_RUNNING)) {
837			left = wait_for_completion_timeout(&ar->scan.completed,
838							   ATH12K_SCAN_TIMEOUT_HZ);
839			if (!left)
840				ath12k_dbg(ar->ab, ATH12K_DBG_REG,
841					   "failed to receive hw scan complete: timed out\n");
842
843			ath12k_dbg(ar->ab, ATH12K_DBG_REG,
844				   "reg hw scan wait left time %d\n", left);
845		}
846
847		ath12k_wmi_send_scan_chan_list_cmd(ar, arg);
848		list_del(&arg->list);
849		kfree(arg);
850	}
851}
852
853void ath12k_regd_update_work(struct work_struct *work)
854{
855	struct ath12k *ar = container_of(work, struct ath12k,
856					 regd_update_work);
857	int ret;
858
859	ret = ath12k_regd_update(ar, false);
860	if (ret) {
861		/* Firmware has already moved to the new regd. We need
862		 * to maintain channel consistency across FW, Host driver
863		 * and userspace. Hence as a fallback mechanism we can set
864		 * the prev or default country code to the firmware.
865		 */
866		/* TODO: Implement Fallback Mechanism */
867	}
868}
869
870void ath12k_reg_reset_reg_info(struct ath12k_reg_info *reg_info)
871{
872	u8 i, j;
873
874	if (!reg_info)
875		return;
876
877	kfree(reg_info->reg_rules_2g_ptr);
878	kfree(reg_info->reg_rules_5g_ptr);
879
880	if (reg_info->is_ext_reg_event) {
881		for (i = 0; i < WMI_REG_CURRENT_MAX_AP_TYPE; i++) {
882			kfree(reg_info->reg_rules_6g_ap_ptr[i]);
883
884			for (j = 0; j < WMI_REG_MAX_CLIENT_TYPE; j++)
885				kfree(reg_info->reg_rules_6g_client_ptr[i][j]);
886		}
887	}
888}
889
890enum ath12k_reg_status ath12k_reg_validate_reg_info(struct ath12k_base *ab,
891						    struct ath12k_reg_info *reg_info)
892{
893	int pdev_idx = reg_info->phy_id;
894
895	if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
896		/* In case of failure to set the requested country,
897		 * firmware retains the current regd. We print a failure info
898		 * and return from here.
899		 */
900		ath12k_warn(ab, "Failed to set the requested Country regulatory setting\n");
901		return ATH12K_REG_STATUS_DROP;
902	}
903
904	if (pdev_idx >= ab->num_radios) {
905		/* Process the event for phy0 only if single_pdev_only
906		 * is true. If pdev_idx is valid but not 0, discard the
907		 * event. Otherwise, it goes to fallback.
908		 */
909		if (ab->hw_params->single_pdev_only &&
910		    pdev_idx < ab->hw_params->num_rxdma_per_pdev)
911			return ATH12K_REG_STATUS_DROP;
912		else
913			return ATH12K_REG_STATUS_FALLBACK;
914	}
915
916	/* Avoid multiple overwrites to default regd, during core
917	 * stop-start after mac registration.
918	 */
919	if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
920	    !memcmp(ab->default_regd[pdev_idx]->alpha2,
921		    reg_info->alpha2, 2))
922		return ATH12K_REG_STATUS_DROP;
923
924	return ATH12K_REG_STATUS_VALID;
925}
926
927int ath12k_reg_handle_chan_list(struct ath12k_base *ab,
928				struct ath12k_reg_info *reg_info,
929				enum wmi_vdev_type vdev_type,
930				enum ieee80211_ap_reg_power power_type)
931{
932	struct ieee80211_regdomain *regd = NULL;
933	int pdev_idx = reg_info->phy_id;
934	struct ath12k *ar;
935
936	regd = ath12k_reg_build_regd(ab, reg_info, vdev_type, power_type);
937	if (!regd)
938		return -EINVAL;
939
940	spin_lock_bh(&ab->base_lock);
941	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) {
942		/* Once mac is registered, ar is valid and all CC events from
943		 * firmware is considered to be received due to user requests
944		 * currently.
945		 * Free previously built regd before assigning the newly
946		 * generated regd to ar. NULL pointer handling will be
947		 * taken care by kfree itself.
948		 */
949		ar = ab->pdevs[pdev_idx].ar;
950		kfree(ab->new_regd[pdev_idx]);
951		ab->new_regd[pdev_idx] = regd;
952		queue_work(ab->workqueue, &ar->regd_update_work);
953	} else {
954		/* Multiple events for the same *ar is not expected. But we
955		 * can still clear any previously stored default_regd if we
956		 * are receiving this event for the same radio by mistake.
957		 * NULL pointer handling will be taken care by kfree itself.
958		 */
959		kfree(ab->default_regd[pdev_idx]);
960		/* This regd would be applied during mac registration */
961		ab->default_regd[pdev_idx] = regd;
962	}
963	ab->dfs_region = reg_info->dfs_region;
964	spin_unlock_bh(&ab->base_lock);
965
966	return 0;
967}
968
969void ath12k_reg_init(struct ieee80211_hw *hw)
970{
971	hw->wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
972	hw->wiphy->flags |= WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER;
973	hw->wiphy->reg_notifier = ath12k_reg_notifier;
974}
975
976void ath12k_reg_free(struct ath12k_base *ab)
977{
978	int i;
979
980	mutex_lock(&ab->core_lock);
981	for (i = 0; i < MAX_RADIOS; i++) {
982		ath12k_reg_reset_reg_info(ab->reg_info[i]);
983		kfree(ab->reg_info[i]);
984		ab->reg_info[i] = NULL;
985	}
986
987	for (i = 0; i < ab->hw_params->max_radios; i++) {
988		kfree(ab->default_regd[i]);
989		kfree(ab->new_regd[i]);
990		ab->default_regd[i] = NULL;
991		ab->new_regd[i] = NULL;
992	}
993	mutex_unlock(&ab->core_lock);
994}
~/ Read-only file preview.