xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/ath/ath11k/spectral.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause-Clear
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/relay.h>
7*4882a593Smuzhiyun #include "core.h"
8*4882a593Smuzhiyun #include "debug.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define ATH11K_SPECTRAL_NUM_RESP_PER_EVENT	2
11*4882a593Smuzhiyun #define ATH11K_SPECTRAL_EVENT_TIMEOUT_MS	1
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define ATH11K_SPECTRAL_DWORD_SIZE		4
14*4882a593Smuzhiyun /* HW bug, expected BIN size is 2 bytes but HW report as 4 bytes */
15*4882a593Smuzhiyun #define ATH11K_SPECTRAL_BIN_SIZE		4
16*4882a593Smuzhiyun #define ATH11K_SPECTRAL_ATH11K_MIN_BINS		64
17*4882a593Smuzhiyun #define ATH11K_SPECTRAL_ATH11K_MIN_IB_BINS	32
18*4882a593Smuzhiyun #define ATH11K_SPECTRAL_ATH11K_MAX_IB_BINS	256
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define ATH11K_SPECTRAL_SCAN_COUNT_MAX		4095
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* Max channel computed by sum of 2g and 5g band channels */
23*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TOTAL_CHANNEL		41
24*4882a593Smuzhiyun #define ATH11K_SPECTRAL_SAMPLES_PER_CHANNEL	70
25*4882a593Smuzhiyun #define ATH11K_SPECTRAL_PER_SAMPLE_SIZE		(sizeof(struct fft_sample_ath11k) + \
26*4882a593Smuzhiyun 						 ATH11K_SPECTRAL_ATH11K_MAX_IB_BINS)
27*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TOTAL_SAMPLE		(ATH11K_SPECTRAL_TOTAL_CHANNEL * \
28*4882a593Smuzhiyun 						 ATH11K_SPECTRAL_SAMPLES_PER_CHANNEL)
29*4882a593Smuzhiyun #define ATH11K_SPECTRAL_SUB_BUFF_SIZE		ATH11K_SPECTRAL_PER_SAMPLE_SIZE
30*4882a593Smuzhiyun #define ATH11K_SPECTRAL_NUM_SUB_BUF		ATH11K_SPECTRAL_TOTAL_SAMPLE
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define ATH11K_SPECTRAL_20MHZ			20
33*4882a593Smuzhiyun #define ATH11K_SPECTRAL_40MHZ			40
34*4882a593Smuzhiyun #define ATH11K_SPECTRAL_80MHZ			80
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define ATH11K_SPECTRAL_SIGNATURE		0xFA
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TAG_RADAR_SUMMARY	0x0
39*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TAG_RADAR_FFT		0x1
40*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TAG_SCAN_SUMMARY	0x2
41*4882a593Smuzhiyun #define ATH11K_SPECTRAL_TAG_SCAN_SEARCH		0x3
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define SPECTRAL_TLV_HDR_LEN				GENMASK(15, 0)
44*4882a593Smuzhiyun #define SPECTRAL_TLV_HDR_TAG				GENMASK(23, 16)
45*4882a593Smuzhiyun #define SPECTRAL_TLV_HDR_SIGN				GENMASK(31, 24)
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_AGC_TOTAL_GAIN		GENMASK(7, 0)
48*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_OB_FLAG			BIT(8)
49*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_GRP_IDX			GENMASK(16, 9)
50*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_RECENT_RFSAT		BIT(17)
51*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_INBAND_PWR_DB		GENMASK(27, 18)
52*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_FALSE_SCAN		BIT(28)
53*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_DETECTOR_ID		GENMASK(30, 29)
54*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO0_PRI80			BIT(31)
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO2_PEAK_SIGNED_IDX		GENMASK(11, 0)
57*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO2_PEAK_MAGNITUDE		GENMASK(21, 12)
58*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO2_NARROWBAND_MASK		GENMASK(29, 22)
59*4882a593Smuzhiyun #define SPECTRAL_SUMMARY_INFO2_GAIN_CHANGE		BIT(30)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun struct spectral_tlv {
62*4882a593Smuzhiyun 	__le32 timestamp;
63*4882a593Smuzhiyun 	__le32 header;
64*4882a593Smuzhiyun } __packed;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun struct spectral_summary_fft_report {
67*4882a593Smuzhiyun 	__le32 timestamp;
68*4882a593Smuzhiyun 	__le32 tlv_header;
69*4882a593Smuzhiyun 	__le32 info0;
70*4882a593Smuzhiyun 	__le32 reserve0;
71*4882a593Smuzhiyun 	__le32 info2;
72*4882a593Smuzhiyun 	__le32 reserve1;
73*4882a593Smuzhiyun } __packed;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun struct ath11k_spectral_summary_report {
76*4882a593Smuzhiyun 	struct wmi_dma_buf_release_meta_data meta;
77*4882a593Smuzhiyun 	u32 timestamp;
78*4882a593Smuzhiyun 	u8 agc_total_gain;
79*4882a593Smuzhiyun 	u8 grp_idx;
80*4882a593Smuzhiyun 	u16 inb_pwr_db;
81*4882a593Smuzhiyun 	s16 peak_idx;
82*4882a593Smuzhiyun 	u16 peak_mag;
83*4882a593Smuzhiyun 	u8 detector_id;
84*4882a593Smuzhiyun 	bool out_of_band_flag;
85*4882a593Smuzhiyun 	bool rf_saturation;
86*4882a593Smuzhiyun 	bool primary80;
87*4882a593Smuzhiyun 	bool gain_change;
88*4882a593Smuzhiyun 	bool false_scan;
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO0_DETECTOR_ID		GENMASK(1, 0)
92*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO0_FFT_NUM		GENMASK(4, 2)
93*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO0_RADAR_CHECK		GENMASK(16, 5)
94*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO0_PEAK_SIGNED_IDX	GENMASK(27, 17)
95*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO0_CHAIN_IDX		GENMASK(30, 28)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO1_BASE_PWR_DB		GENMASK(8, 0)
98*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO1_TOTAL_GAIN_DB		GENMASK(16, 9)
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO2_NUM_STRONG_BINS	GENMASK(7, 0)
101*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO2_PEAK_MAGNITUDE	GENMASK(17, 8)
102*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO2_AVG_PWR_DB		GENMASK(24, 18)
103*4882a593Smuzhiyun #define SPECTRAL_FFT_REPORT_INFO2_REL_PWR_DB		GENMASK(31, 25)
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun struct spectral_search_fft_report {
106*4882a593Smuzhiyun 	__le32 timestamp;
107*4882a593Smuzhiyun 	__le32 tlv_header;
108*4882a593Smuzhiyun 	__le32 info0;
109*4882a593Smuzhiyun 	__le32 info1;
110*4882a593Smuzhiyun 	__le32 info2;
111*4882a593Smuzhiyun 	__le32 reserve0;
112*4882a593Smuzhiyun 	u8 bins[0];
113*4882a593Smuzhiyun } __packed;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun struct ath11k_spectral_search_report {
116*4882a593Smuzhiyun 	u32 timestamp;
117*4882a593Smuzhiyun 	u8 detector_id;
118*4882a593Smuzhiyun 	u8 fft_count;
119*4882a593Smuzhiyun 	u16 radar_check;
120*4882a593Smuzhiyun 	s16 peak_idx;
121*4882a593Smuzhiyun 	u8 chain_idx;
122*4882a593Smuzhiyun 	u16 base_pwr_db;
123*4882a593Smuzhiyun 	u8 total_gain_db;
124*4882a593Smuzhiyun 	u8 strong_bin_count;
125*4882a593Smuzhiyun 	u16 peak_mag;
126*4882a593Smuzhiyun 	u8 avg_pwr_db;
127*4882a593Smuzhiyun 	u8 rel_pwr_db;
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
create_buf_file_handler(const char * filename,struct dentry * parent,umode_t mode,struct rchan_buf * buf,int * is_global)130*4882a593Smuzhiyun static struct dentry *create_buf_file_handler(const char *filename,
131*4882a593Smuzhiyun 					      struct dentry *parent,
132*4882a593Smuzhiyun 					      umode_t mode,
133*4882a593Smuzhiyun 					      struct rchan_buf *buf,
134*4882a593Smuzhiyun 					      int *is_global)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct dentry *buf_file;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	buf_file = debugfs_create_file(filename, mode, parent, buf,
139*4882a593Smuzhiyun 				       &relay_file_operations);
140*4882a593Smuzhiyun 	*is_global = 1;
141*4882a593Smuzhiyun 	return buf_file;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
remove_buf_file_handler(struct dentry * dentry)144*4882a593Smuzhiyun static int remove_buf_file_handler(struct dentry *dentry)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	debugfs_remove(dentry);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun static struct rchan_callbacks rfs_scan_cb = {
152*4882a593Smuzhiyun 	.create_buf_file = create_buf_file_handler,
153*4882a593Smuzhiyun 	.remove_buf_file = remove_buf_file_handler,
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun 
ath11k_spectral_get_vdev(struct ath11k * ar)156*4882a593Smuzhiyun static struct ath11k_vif *ath11k_spectral_get_vdev(struct ath11k *ar)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct ath11k_vif *arvif;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	lockdep_assert_held(&ar->conf_mutex);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (list_empty(&ar->arvifs))
163*4882a593Smuzhiyun 		return NULL;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* if there already is a vif doing spectral, return that. */
166*4882a593Smuzhiyun 	list_for_each_entry(arvif, &ar->arvifs, list)
167*4882a593Smuzhiyun 		if (arvif->spectral_enabled)
168*4882a593Smuzhiyun 			return arvif;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/* otherwise, return the first vif. */
171*4882a593Smuzhiyun 	return list_first_entry(&ar->arvifs, typeof(*arvif), list);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
ath11k_spectral_scan_trigger(struct ath11k * ar)174*4882a593Smuzhiyun static int ath11k_spectral_scan_trigger(struct ath11k *ar)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct ath11k_vif *arvif;
177*4882a593Smuzhiyun 	int ret;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	lockdep_assert_held(&ar->conf_mutex);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	arvif = ath11k_spectral_get_vdev(ar);
182*4882a593Smuzhiyun 	if (!arvif)
183*4882a593Smuzhiyun 		return -ENODEV;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (ar->spectral.mode == ATH11K_SPECTRAL_DISABLED)
186*4882a593Smuzhiyun 		return 0;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	ret = ath11k_wmi_vdev_spectral_enable(ar, arvif->vdev_id,
189*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
190*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_ENABLE_CMD_ENABLE);
191*4882a593Smuzhiyun 	if (ret)
192*4882a593Smuzhiyun 		return ret;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	ret = ath11k_wmi_vdev_spectral_enable(ar, arvif->vdev_id,
195*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_TRIGGER_CMD_TRIGGER,
196*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_ENABLE_CMD_ENABLE);
197*4882a593Smuzhiyun 	if (ret)
198*4882a593Smuzhiyun 		return ret;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	return 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
ath11k_spectral_scan_config(struct ath11k * ar,enum ath11k_spectral_mode mode)203*4882a593Smuzhiyun static int ath11k_spectral_scan_config(struct ath11k *ar,
204*4882a593Smuzhiyun 				       enum ath11k_spectral_mode mode)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct ath11k_wmi_vdev_spectral_conf_param param = { 0 };
207*4882a593Smuzhiyun 	struct ath11k_vif *arvif;
208*4882a593Smuzhiyun 	int ret, count;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	lockdep_assert_held(&ar->conf_mutex);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	arvif = ath11k_spectral_get_vdev(ar);
213*4882a593Smuzhiyun 	if (!arvif)
214*4882a593Smuzhiyun 		return -ENODEV;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	arvif->spectral_enabled = (mode != ATH11K_SPECTRAL_DISABLED);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	spin_lock_bh(&ar->spectral.lock);
219*4882a593Smuzhiyun 	ar->spectral.mode = mode;
220*4882a593Smuzhiyun 	spin_unlock_bh(&ar->spectral.lock);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ret = ath11k_wmi_vdev_spectral_enable(ar, arvif->vdev_id,
223*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
224*4882a593Smuzhiyun 					      ATH11K_WMI_SPECTRAL_ENABLE_CMD_DISABLE);
225*4882a593Smuzhiyun 	if (ret) {
226*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to enable spectral scan: %d\n", ret);
227*4882a593Smuzhiyun 		return ret;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (mode == ATH11K_SPECTRAL_DISABLED)
231*4882a593Smuzhiyun 		return 0;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	if (mode == ATH11K_SPECTRAL_BACKGROUND)
234*4882a593Smuzhiyun 		count = ATH11K_WMI_SPECTRAL_COUNT_DEFAULT;
235*4882a593Smuzhiyun 	else
236*4882a593Smuzhiyun 		count = max_t(u16, 1, ar->spectral.count);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	param.vdev_id = arvif->vdev_id;
239*4882a593Smuzhiyun 	param.scan_count = count;
240*4882a593Smuzhiyun 	param.scan_fft_size = ar->spectral.fft_size;
241*4882a593Smuzhiyun 	param.scan_period = ATH11K_WMI_SPECTRAL_PERIOD_DEFAULT;
242*4882a593Smuzhiyun 	param.scan_priority = ATH11K_WMI_SPECTRAL_PRIORITY_DEFAULT;
243*4882a593Smuzhiyun 	param.scan_gc_ena = ATH11K_WMI_SPECTRAL_GC_ENA_DEFAULT;
244*4882a593Smuzhiyun 	param.scan_restart_ena = ATH11K_WMI_SPECTRAL_RESTART_ENA_DEFAULT;
245*4882a593Smuzhiyun 	param.scan_noise_floor_ref = ATH11K_WMI_SPECTRAL_NOISE_FLOOR_REF_DEFAULT;
246*4882a593Smuzhiyun 	param.scan_init_delay = ATH11K_WMI_SPECTRAL_INIT_DELAY_DEFAULT;
247*4882a593Smuzhiyun 	param.scan_nb_tone_thr = ATH11K_WMI_SPECTRAL_NB_TONE_THR_DEFAULT;
248*4882a593Smuzhiyun 	param.scan_str_bin_thr = ATH11K_WMI_SPECTRAL_STR_BIN_THR_DEFAULT;
249*4882a593Smuzhiyun 	param.scan_wb_rpt_mode = ATH11K_WMI_SPECTRAL_WB_RPT_MODE_DEFAULT;
250*4882a593Smuzhiyun 	param.scan_rssi_rpt_mode = ATH11K_WMI_SPECTRAL_RSSI_RPT_MODE_DEFAULT;
251*4882a593Smuzhiyun 	param.scan_rssi_thr = ATH11K_WMI_SPECTRAL_RSSI_THR_DEFAULT;
252*4882a593Smuzhiyun 	param.scan_pwr_format = ATH11K_WMI_SPECTRAL_PWR_FORMAT_DEFAULT;
253*4882a593Smuzhiyun 	param.scan_rpt_mode = ATH11K_WMI_SPECTRAL_RPT_MODE_DEFAULT;
254*4882a593Smuzhiyun 	param.scan_bin_scale = ATH11K_WMI_SPECTRAL_BIN_SCALE_DEFAULT;
255*4882a593Smuzhiyun 	param.scan_dbm_adj = ATH11K_WMI_SPECTRAL_DBM_ADJ_DEFAULT;
256*4882a593Smuzhiyun 	param.scan_chn_mask = ATH11K_WMI_SPECTRAL_CHN_MASK_DEFAULT;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	ret = ath11k_wmi_vdev_spectral_conf(ar, &param);
259*4882a593Smuzhiyun 	if (ret) {
260*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to configure spectral scan: %d\n", ret);
261*4882a593Smuzhiyun 		return ret;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	return 0;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
ath11k_read_file_spec_scan_ctl(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)267*4882a593Smuzhiyun static ssize_t ath11k_read_file_spec_scan_ctl(struct file *file,
268*4882a593Smuzhiyun 					      char __user *user_buf,
269*4882a593Smuzhiyun 					      size_t count, loff_t *ppos)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
272*4882a593Smuzhiyun 	char *mode = "";
273*4882a593Smuzhiyun 	size_t len;
274*4882a593Smuzhiyun 	enum ath11k_spectral_mode spectral_mode;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
277*4882a593Smuzhiyun 	spectral_mode = ar->spectral.mode;
278*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	switch (spectral_mode) {
281*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_DISABLED:
282*4882a593Smuzhiyun 		mode = "disable";
283*4882a593Smuzhiyun 		break;
284*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_BACKGROUND:
285*4882a593Smuzhiyun 		mode = "background";
286*4882a593Smuzhiyun 		break;
287*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_MANUAL:
288*4882a593Smuzhiyun 		mode = "manual";
289*4882a593Smuzhiyun 		break;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	len = strlen(mode);
293*4882a593Smuzhiyun 	return simple_read_from_buffer(user_buf, count, ppos, mode, len);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
ath11k_write_file_spec_scan_ctl(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)296*4882a593Smuzhiyun static ssize_t ath11k_write_file_spec_scan_ctl(struct file *file,
297*4882a593Smuzhiyun 					       const char __user *user_buf,
298*4882a593Smuzhiyun 					       size_t count, loff_t *ppos)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
301*4882a593Smuzhiyun 	char buf[32];
302*4882a593Smuzhiyun 	ssize_t len;
303*4882a593Smuzhiyun 	int ret;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	len = min(count, sizeof(buf) - 1);
306*4882a593Smuzhiyun 	if (copy_from_user(buf, user_buf, len))
307*4882a593Smuzhiyun 		return -EFAULT;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	buf[len] = '\0';
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (strncmp("trigger", buf, 7) == 0) {
314*4882a593Smuzhiyun 		if (ar->spectral.mode == ATH11K_SPECTRAL_MANUAL ||
315*4882a593Smuzhiyun 		    ar->spectral.mode == ATH11K_SPECTRAL_BACKGROUND) {
316*4882a593Smuzhiyun 			/* reset the configuration to adopt possibly changed
317*4882a593Smuzhiyun 			 * debugfs parameters
318*4882a593Smuzhiyun 			 */
319*4882a593Smuzhiyun 			ret = ath11k_spectral_scan_config(ar, ar->spectral.mode);
320*4882a593Smuzhiyun 			if (ret) {
321*4882a593Smuzhiyun 				ath11k_warn(ar->ab, "failed to reconfigure spectral scan: %d\n",
322*4882a593Smuzhiyun 					    ret);
323*4882a593Smuzhiyun 				goto unlock;
324*4882a593Smuzhiyun 			}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 			ret = ath11k_spectral_scan_trigger(ar);
327*4882a593Smuzhiyun 			if (ret) {
328*4882a593Smuzhiyun 				ath11k_warn(ar->ab, "failed to trigger spectral scan: %d\n",
329*4882a593Smuzhiyun 					    ret);
330*4882a593Smuzhiyun 			}
331*4882a593Smuzhiyun 		} else {
332*4882a593Smuzhiyun 			ret = -EINVAL;
333*4882a593Smuzhiyun 		}
334*4882a593Smuzhiyun 	} else if (strncmp("background", buf, 10) == 0) {
335*4882a593Smuzhiyun 		ret = ath11k_spectral_scan_config(ar, ATH11K_SPECTRAL_BACKGROUND);
336*4882a593Smuzhiyun 	} else if (strncmp("manual", buf, 6) == 0) {
337*4882a593Smuzhiyun 		ret = ath11k_spectral_scan_config(ar, ATH11K_SPECTRAL_MANUAL);
338*4882a593Smuzhiyun 	} else if (strncmp("disable", buf, 7) == 0) {
339*4882a593Smuzhiyun 		ret = ath11k_spectral_scan_config(ar, ATH11K_SPECTRAL_DISABLED);
340*4882a593Smuzhiyun 	} else {
341*4882a593Smuzhiyun 		ret = -EINVAL;
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun unlock:
345*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (ret)
348*4882a593Smuzhiyun 		return ret;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return count;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun static const struct file_operations fops_scan_ctl = {
354*4882a593Smuzhiyun 	.read = ath11k_read_file_spec_scan_ctl,
355*4882a593Smuzhiyun 	.write = ath11k_write_file_spec_scan_ctl,
356*4882a593Smuzhiyun 	.open = simple_open,
357*4882a593Smuzhiyun 	.owner = THIS_MODULE,
358*4882a593Smuzhiyun 	.llseek = default_llseek,
359*4882a593Smuzhiyun };
360*4882a593Smuzhiyun 
ath11k_read_file_spectral_count(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)361*4882a593Smuzhiyun static ssize_t ath11k_read_file_spectral_count(struct file *file,
362*4882a593Smuzhiyun 					       char __user *user_buf,
363*4882a593Smuzhiyun 					       size_t count, loff_t *ppos)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
366*4882a593Smuzhiyun 	char buf[32];
367*4882a593Smuzhiyun 	size_t len;
368*4882a593Smuzhiyun 	u16 spectral_count;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
371*4882a593Smuzhiyun 	spectral_count = ar->spectral.count;
372*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	len = sprintf(buf, "%d\n", spectral_count);
375*4882a593Smuzhiyun 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
ath11k_write_file_spectral_count(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)378*4882a593Smuzhiyun static ssize_t ath11k_write_file_spectral_count(struct file *file,
379*4882a593Smuzhiyun 						const char __user *user_buf,
380*4882a593Smuzhiyun 						size_t count, loff_t *ppos)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
383*4882a593Smuzhiyun 	unsigned long val;
384*4882a593Smuzhiyun 	char buf[32];
385*4882a593Smuzhiyun 	ssize_t len;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	len = min(count, sizeof(buf) - 1);
388*4882a593Smuzhiyun 	if (copy_from_user(buf, user_buf, len))
389*4882a593Smuzhiyun 		return -EFAULT;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	buf[len] = '\0';
392*4882a593Smuzhiyun 	if (kstrtoul(buf, 0, &val))
393*4882a593Smuzhiyun 		return -EINVAL;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (val > ATH11K_SPECTRAL_SCAN_COUNT_MAX)
396*4882a593Smuzhiyun 		return -EINVAL;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
399*4882a593Smuzhiyun 	ar->spectral.count = val;
400*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	return count;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun static const struct file_operations fops_scan_count = {
406*4882a593Smuzhiyun 	.read = ath11k_read_file_spectral_count,
407*4882a593Smuzhiyun 	.write = ath11k_write_file_spectral_count,
408*4882a593Smuzhiyun 	.open = simple_open,
409*4882a593Smuzhiyun 	.owner = THIS_MODULE,
410*4882a593Smuzhiyun 	.llseek = default_llseek,
411*4882a593Smuzhiyun };
412*4882a593Smuzhiyun 
ath11k_read_file_spectral_bins(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)413*4882a593Smuzhiyun static ssize_t ath11k_read_file_spectral_bins(struct file *file,
414*4882a593Smuzhiyun 					      char __user *user_buf,
415*4882a593Smuzhiyun 					      size_t count, loff_t *ppos)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
418*4882a593Smuzhiyun 	char buf[32];
419*4882a593Smuzhiyun 	unsigned int bins, fft_size;
420*4882a593Smuzhiyun 	size_t len;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	fft_size = ar->spectral.fft_size;
425*4882a593Smuzhiyun 	bins = 1 << fft_size;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	len = sprintf(buf, "%d\n", bins);
430*4882a593Smuzhiyun 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
ath11k_write_file_spectral_bins(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)433*4882a593Smuzhiyun static ssize_t ath11k_write_file_spectral_bins(struct file *file,
434*4882a593Smuzhiyun 					       const char __user *user_buf,
435*4882a593Smuzhiyun 					       size_t count, loff_t *ppos)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	struct ath11k *ar = file->private_data;
438*4882a593Smuzhiyun 	unsigned long val;
439*4882a593Smuzhiyun 	char buf[32];
440*4882a593Smuzhiyun 	ssize_t len;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	len = min(count, sizeof(buf) - 1);
443*4882a593Smuzhiyun 	if (copy_from_user(buf, user_buf, len))
444*4882a593Smuzhiyun 		return -EFAULT;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	buf[len] = '\0';
447*4882a593Smuzhiyun 	if (kstrtoul(buf, 0, &val))
448*4882a593Smuzhiyun 		return -EINVAL;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	if (val < ATH11K_SPECTRAL_ATH11K_MIN_BINS ||
451*4882a593Smuzhiyun 	    val > SPECTRAL_ATH11K_MAX_NUM_BINS)
452*4882a593Smuzhiyun 		return -EINVAL;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if (!is_power_of_2(val))
455*4882a593Smuzhiyun 		return -EINVAL;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	mutex_lock(&ar->conf_mutex);
458*4882a593Smuzhiyun 	ar->spectral.fft_size = ilog2(val);
459*4882a593Smuzhiyun 	mutex_unlock(&ar->conf_mutex);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	return count;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun static const struct file_operations fops_scan_bins = {
465*4882a593Smuzhiyun 	.read = ath11k_read_file_spectral_bins,
466*4882a593Smuzhiyun 	.write = ath11k_write_file_spectral_bins,
467*4882a593Smuzhiyun 	.open = simple_open,
468*4882a593Smuzhiyun 	.owner = THIS_MODULE,
469*4882a593Smuzhiyun 	.llseek = default_llseek,
470*4882a593Smuzhiyun };
471*4882a593Smuzhiyun 
ath11k_spectral_pull_summary(struct ath11k * ar,struct wmi_dma_buf_release_meta_data * meta,struct spectral_summary_fft_report * summary,struct ath11k_spectral_summary_report * report)472*4882a593Smuzhiyun static int ath11k_spectral_pull_summary(struct ath11k *ar,
473*4882a593Smuzhiyun 					struct wmi_dma_buf_release_meta_data *meta,
474*4882a593Smuzhiyun 					struct spectral_summary_fft_report *summary,
475*4882a593Smuzhiyun 					struct ath11k_spectral_summary_report *report)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	report->timestamp = __le32_to_cpu(summary->timestamp);
478*4882a593Smuzhiyun 	report->agc_total_gain = FIELD_GET(SPECTRAL_SUMMARY_INFO0_AGC_TOTAL_GAIN,
479*4882a593Smuzhiyun 					   __le32_to_cpu(summary->info0));
480*4882a593Smuzhiyun 	report->out_of_band_flag = FIELD_GET(SPECTRAL_SUMMARY_INFO0_OB_FLAG,
481*4882a593Smuzhiyun 					     __le32_to_cpu(summary->info0));
482*4882a593Smuzhiyun 	report->grp_idx = FIELD_GET(SPECTRAL_SUMMARY_INFO0_GRP_IDX,
483*4882a593Smuzhiyun 				    __le32_to_cpu(summary->info0));
484*4882a593Smuzhiyun 	report->rf_saturation = FIELD_GET(SPECTRAL_SUMMARY_INFO0_RECENT_RFSAT,
485*4882a593Smuzhiyun 					  __le32_to_cpu(summary->info0));
486*4882a593Smuzhiyun 	report->inb_pwr_db = FIELD_GET(SPECTRAL_SUMMARY_INFO0_INBAND_PWR_DB,
487*4882a593Smuzhiyun 				       __le32_to_cpu(summary->info0));
488*4882a593Smuzhiyun 	report->false_scan = FIELD_GET(SPECTRAL_SUMMARY_INFO0_FALSE_SCAN,
489*4882a593Smuzhiyun 				       __le32_to_cpu(summary->info0));
490*4882a593Smuzhiyun 	report->detector_id = FIELD_GET(SPECTRAL_SUMMARY_INFO0_DETECTOR_ID,
491*4882a593Smuzhiyun 					__le32_to_cpu(summary->info0));
492*4882a593Smuzhiyun 	report->primary80 = FIELD_GET(SPECTRAL_SUMMARY_INFO0_PRI80,
493*4882a593Smuzhiyun 				      __le32_to_cpu(summary->info0));
494*4882a593Smuzhiyun 	report->peak_idx = FIELD_GET(SPECTRAL_SUMMARY_INFO2_PEAK_SIGNED_IDX,
495*4882a593Smuzhiyun 				     __le32_to_cpu(summary->info2));
496*4882a593Smuzhiyun 	report->peak_mag = FIELD_GET(SPECTRAL_SUMMARY_INFO2_PEAK_MAGNITUDE,
497*4882a593Smuzhiyun 				     __le32_to_cpu(summary->info2));
498*4882a593Smuzhiyun 	report->gain_change = FIELD_GET(SPECTRAL_SUMMARY_INFO2_GAIN_CHANGE,
499*4882a593Smuzhiyun 					__le32_to_cpu(summary->info2));
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	memcpy(&report->meta, meta, sizeof(*meta));
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	return 0;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
ath11k_spectral_pull_search(struct ath11k * ar,struct spectral_search_fft_report * search,struct ath11k_spectral_search_report * report)506*4882a593Smuzhiyun static int ath11k_spectral_pull_search(struct ath11k *ar,
507*4882a593Smuzhiyun 				       struct spectral_search_fft_report *search,
508*4882a593Smuzhiyun 				       struct ath11k_spectral_search_report *report)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	report->timestamp = __le32_to_cpu(search->timestamp);
511*4882a593Smuzhiyun 	report->detector_id = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_DETECTOR_ID,
512*4882a593Smuzhiyun 					__le32_to_cpu(search->info0));
513*4882a593Smuzhiyun 	report->fft_count = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_FFT_NUM,
514*4882a593Smuzhiyun 				      __le32_to_cpu(search->info0));
515*4882a593Smuzhiyun 	report->radar_check = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_RADAR_CHECK,
516*4882a593Smuzhiyun 					__le32_to_cpu(search->info0));
517*4882a593Smuzhiyun 	report->peak_idx = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_PEAK_SIGNED_IDX,
518*4882a593Smuzhiyun 				     __le32_to_cpu(search->info0));
519*4882a593Smuzhiyun 	report->chain_idx = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_CHAIN_IDX,
520*4882a593Smuzhiyun 				      __le32_to_cpu(search->info0));
521*4882a593Smuzhiyun 	report->base_pwr_db = FIELD_GET(SPECTRAL_FFT_REPORT_INFO1_BASE_PWR_DB,
522*4882a593Smuzhiyun 					__le32_to_cpu(search->info1));
523*4882a593Smuzhiyun 	report->total_gain_db = FIELD_GET(SPECTRAL_FFT_REPORT_INFO1_TOTAL_GAIN_DB,
524*4882a593Smuzhiyun 					  __le32_to_cpu(search->info1));
525*4882a593Smuzhiyun 	report->strong_bin_count = FIELD_GET(SPECTRAL_FFT_REPORT_INFO2_NUM_STRONG_BINS,
526*4882a593Smuzhiyun 					     __le32_to_cpu(search->info2));
527*4882a593Smuzhiyun 	report->peak_mag = FIELD_GET(SPECTRAL_FFT_REPORT_INFO2_PEAK_MAGNITUDE,
528*4882a593Smuzhiyun 				     __le32_to_cpu(search->info2));
529*4882a593Smuzhiyun 	report->avg_pwr_db = FIELD_GET(SPECTRAL_FFT_REPORT_INFO2_AVG_PWR_DB,
530*4882a593Smuzhiyun 				       __le32_to_cpu(search->info2));
531*4882a593Smuzhiyun 	report->rel_pwr_db = FIELD_GET(SPECTRAL_FFT_REPORT_INFO2_REL_PWR_DB,
532*4882a593Smuzhiyun 				       __le32_to_cpu(search->info2));
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	return 0;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun 
ath11k_spectral_get_max_exp(s8 max_index,u8 max_magnitude,int bin_len,u8 * bins)537*4882a593Smuzhiyun static u8 ath11k_spectral_get_max_exp(s8 max_index, u8 max_magnitude,
538*4882a593Smuzhiyun 				      int bin_len, u8 *bins)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	int dc_pos;
541*4882a593Smuzhiyun 	u8 max_exp;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	dc_pos = bin_len / 2;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* peak index outside of bins */
546*4882a593Smuzhiyun 	if (dc_pos <= max_index || -dc_pos >= max_index)
547*4882a593Smuzhiyun 		return 0;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	for (max_exp = 0; max_exp < 8; max_exp++) {
550*4882a593Smuzhiyun 		if (bins[dc_pos + max_index] == (max_magnitude >> max_exp))
551*4882a593Smuzhiyun 			break;
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	/* max_exp not found */
555*4882a593Smuzhiyun 	if (bins[dc_pos + max_index] != (max_magnitude >> max_exp))
556*4882a593Smuzhiyun 		return 0;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	return max_exp;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun 
ath11k_spectral_parse_fft(u8 * outbins,u8 * inbins,int num_bins,u8 fft_sz)561*4882a593Smuzhiyun static void ath11k_spectral_parse_fft(u8 *outbins, u8 *inbins, int num_bins, u8 fft_sz)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	int i, j;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	i = 0;
566*4882a593Smuzhiyun 	j = 0;
567*4882a593Smuzhiyun 	while (i < num_bins) {
568*4882a593Smuzhiyun 		outbins[i] = inbins[j];
569*4882a593Smuzhiyun 		i++;
570*4882a593Smuzhiyun 		j += fft_sz;
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun static
ath11k_spectral_process_fft(struct ath11k * ar,struct ath11k_spectral_summary_report * summary,void * data,struct fft_sample_ath11k * fft_sample,u32 data_len)575*4882a593Smuzhiyun int ath11k_spectral_process_fft(struct ath11k *ar,
576*4882a593Smuzhiyun 				struct ath11k_spectral_summary_report *summary,
577*4882a593Smuzhiyun 				void *data,
578*4882a593Smuzhiyun 				struct fft_sample_ath11k *fft_sample,
579*4882a593Smuzhiyun 				u32 data_len)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct ath11k_base *ab = ar->ab;
582*4882a593Smuzhiyun 	struct spectral_search_fft_report *fft_report = data;
583*4882a593Smuzhiyun 	struct ath11k_spectral_search_report search;
584*4882a593Smuzhiyun 	struct spectral_tlv *tlv;
585*4882a593Smuzhiyun 	int tlv_len, bin_len, num_bins;
586*4882a593Smuzhiyun 	u16 length, freq;
587*4882a593Smuzhiyun 	u8 chan_width_mhz;
588*4882a593Smuzhiyun 	int ret;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	lockdep_assert_held(&ar->spectral.lock);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (!ab->hw_params.spectral_fft_sz) {
593*4882a593Smuzhiyun 		ath11k_warn(ab, "invalid bin size type for hw rev %d\n",
594*4882a593Smuzhiyun 			    ab->hw_rev);
595*4882a593Smuzhiyun 		return -EINVAL;
596*4882a593Smuzhiyun 	}
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	tlv = (struct spectral_tlv *)data;
599*4882a593Smuzhiyun 	tlv_len = FIELD_GET(SPECTRAL_TLV_HDR_LEN, __le32_to_cpu(tlv->header));
600*4882a593Smuzhiyun 	/* convert Dword into bytes */
601*4882a593Smuzhiyun 	tlv_len *= ATH11K_SPECTRAL_DWORD_SIZE;
602*4882a593Smuzhiyun 	bin_len = tlv_len - (sizeof(*fft_report) - sizeof(*tlv));
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	if (data_len < (bin_len + sizeof(*fft_report))) {
605*4882a593Smuzhiyun 		ath11k_warn(ab, "mismatch in expected bin len %d and data len %d\n",
606*4882a593Smuzhiyun 			    bin_len, data_len);
607*4882a593Smuzhiyun 		return -EINVAL;
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	num_bins = bin_len / ATH11K_SPECTRAL_BIN_SIZE;
611*4882a593Smuzhiyun 	/* Only In-band bins are useful to user for visualize */
612*4882a593Smuzhiyun 	num_bins >>= 1;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if (num_bins < ATH11K_SPECTRAL_ATH11K_MIN_IB_BINS ||
615*4882a593Smuzhiyun 	    num_bins > ATH11K_SPECTRAL_ATH11K_MAX_IB_BINS ||
616*4882a593Smuzhiyun 	    !is_power_of_2(num_bins)) {
617*4882a593Smuzhiyun 		ath11k_warn(ab, "Invalid num of bins %d\n", num_bins);
618*4882a593Smuzhiyun 		return -EINVAL;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	ret = ath11k_spectral_pull_search(ar, data, &search);
622*4882a593Smuzhiyun 	if (ret) {
623*4882a593Smuzhiyun 		ath11k_warn(ab, "failed to pull search report %d\n", ret);
624*4882a593Smuzhiyun 		return ret;
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	chan_width_mhz = summary->meta.ch_width;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	switch (chan_width_mhz) {
630*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_20MHZ:
631*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_40MHZ:
632*4882a593Smuzhiyun 	case ATH11K_SPECTRAL_80MHZ:
633*4882a593Smuzhiyun 		fft_sample->chan_width_mhz = chan_width_mhz;
634*4882a593Smuzhiyun 		break;
635*4882a593Smuzhiyun 	default:
636*4882a593Smuzhiyun 		ath11k_warn(ab, "invalid channel width %d\n", chan_width_mhz);
637*4882a593Smuzhiyun 		return -EINVAL;
638*4882a593Smuzhiyun 	}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	length = sizeof(*fft_sample) - sizeof(struct fft_sample_tlv) + num_bins;
641*4882a593Smuzhiyun 	fft_sample->tlv.type = ATH_FFT_SAMPLE_ATH11K;
642*4882a593Smuzhiyun 	fft_sample->tlv.length = __cpu_to_be16(length);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	fft_sample->tsf = __cpu_to_be32(search.timestamp);
645*4882a593Smuzhiyun 	fft_sample->max_magnitude = __cpu_to_be16(search.peak_mag);
646*4882a593Smuzhiyun 	fft_sample->max_index = FIELD_GET(SPECTRAL_FFT_REPORT_INFO0_PEAK_SIGNED_IDX,
647*4882a593Smuzhiyun 					  __le32_to_cpu(fft_report->info0));
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	summary->inb_pwr_db >>= 1;
650*4882a593Smuzhiyun 	fft_sample->rssi = __cpu_to_be16(summary->inb_pwr_db);
651*4882a593Smuzhiyun 	fft_sample->noise = __cpu_to_be32(summary->meta.noise_floor[search.chain_idx]);
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	freq = summary->meta.freq1;
654*4882a593Smuzhiyun 	fft_sample->freq1 = __cpu_to_be16(freq);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	freq = summary->meta.freq2;
657*4882a593Smuzhiyun 	fft_sample->freq2 = __cpu_to_be16(freq);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	ath11k_spectral_parse_fft(fft_sample->data, fft_report->bins, num_bins,
660*4882a593Smuzhiyun 				  ab->hw_params.spectral_fft_sz);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	fft_sample->max_exp = ath11k_spectral_get_max_exp(fft_sample->max_index,
663*4882a593Smuzhiyun 							  search.peak_mag,
664*4882a593Smuzhiyun 							  num_bins,
665*4882a593Smuzhiyun 							  fft_sample->data);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	if (ar->spectral.rfs_scan)
668*4882a593Smuzhiyun 		relay_write(ar->spectral.rfs_scan, fft_sample,
669*4882a593Smuzhiyun 			    length + sizeof(struct fft_sample_tlv));
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	return 0;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
ath11k_spectral_process_data(struct ath11k * ar,struct ath11k_dbring_data * param)674*4882a593Smuzhiyun static int ath11k_spectral_process_data(struct ath11k *ar,
675*4882a593Smuzhiyun 					struct ath11k_dbring_data *param)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	struct ath11k_base *ab = ar->ab;
678*4882a593Smuzhiyun 	struct spectral_tlv *tlv;
679*4882a593Smuzhiyun 	struct spectral_summary_fft_report *summary = NULL;
680*4882a593Smuzhiyun 	struct ath11k_spectral_summary_report summ_rpt;
681*4882a593Smuzhiyun 	struct fft_sample_ath11k *fft_sample = NULL;
682*4882a593Smuzhiyun 	u8 *data;
683*4882a593Smuzhiyun 	u32 data_len, i;
684*4882a593Smuzhiyun 	u8 sign, tag;
685*4882a593Smuzhiyun 	int tlv_len, sample_sz;
686*4882a593Smuzhiyun 	int ret;
687*4882a593Smuzhiyun 	bool quit = false;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	spin_lock_bh(&ar->spectral.lock);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (!ar->spectral.enabled) {
692*4882a593Smuzhiyun 		ret = -EINVAL;
693*4882a593Smuzhiyun 		goto unlock;
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	sample_sz = sizeof(*fft_sample) + ATH11K_SPECTRAL_ATH11K_MAX_IB_BINS;
697*4882a593Smuzhiyun 	fft_sample = kmalloc(sample_sz, GFP_ATOMIC);
698*4882a593Smuzhiyun 	if (!fft_sample) {
699*4882a593Smuzhiyun 		ret = -ENOBUFS;
700*4882a593Smuzhiyun 		goto unlock;
701*4882a593Smuzhiyun 	}
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	data = param->data;
704*4882a593Smuzhiyun 	data_len = param->data_sz;
705*4882a593Smuzhiyun 	i = 0;
706*4882a593Smuzhiyun 	while (!quit && (i < data_len)) {
707*4882a593Smuzhiyun 		if ((i + sizeof(*tlv)) > data_len) {
708*4882a593Smuzhiyun 			ath11k_warn(ab, "failed to parse spectral tlv hdr at bytes %d\n",
709*4882a593Smuzhiyun 				    i);
710*4882a593Smuzhiyun 			ret = -EINVAL;
711*4882a593Smuzhiyun 			goto err;
712*4882a593Smuzhiyun 		}
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 		tlv = (struct spectral_tlv *)&data[i];
715*4882a593Smuzhiyun 		sign = FIELD_GET(SPECTRAL_TLV_HDR_SIGN,
716*4882a593Smuzhiyun 				 __le32_to_cpu(tlv->header));
717*4882a593Smuzhiyun 		if (sign != ATH11K_SPECTRAL_SIGNATURE) {
718*4882a593Smuzhiyun 			ath11k_warn(ab, "Invalid sign 0x%x at bytes %d\n",
719*4882a593Smuzhiyun 				    sign, i);
720*4882a593Smuzhiyun 			ret = -EINVAL;
721*4882a593Smuzhiyun 			goto err;
722*4882a593Smuzhiyun 		}
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 		tlv_len = FIELD_GET(SPECTRAL_TLV_HDR_LEN,
725*4882a593Smuzhiyun 				    __le32_to_cpu(tlv->header));
726*4882a593Smuzhiyun 		/* convert Dword into bytes */
727*4882a593Smuzhiyun 		tlv_len *= ATH11K_SPECTRAL_DWORD_SIZE;
728*4882a593Smuzhiyun 		if ((i + sizeof(*tlv) + tlv_len) > data_len) {
729*4882a593Smuzhiyun 			ath11k_warn(ab, "failed to parse spectral tlv payload at bytes %d tlv_len:%d data_len:%d\n",
730*4882a593Smuzhiyun 				    i, tlv_len, data_len);
731*4882a593Smuzhiyun 			ret = -EINVAL;
732*4882a593Smuzhiyun 			goto err;
733*4882a593Smuzhiyun 		}
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 		tag = FIELD_GET(SPECTRAL_TLV_HDR_TAG,
736*4882a593Smuzhiyun 				__le32_to_cpu(tlv->header));
737*4882a593Smuzhiyun 		switch (tag) {
738*4882a593Smuzhiyun 		case ATH11K_SPECTRAL_TAG_SCAN_SUMMARY:
739*4882a593Smuzhiyun 			/* HW bug in tlv length of summary report,
740*4882a593Smuzhiyun 			 * HW report 3 DWORD size but the data payload
741*4882a593Smuzhiyun 			 * is 4 DWORD size (16 bytes).
742*4882a593Smuzhiyun 			 * Need to remove this workaround once HW bug fixed
743*4882a593Smuzhiyun 			 */
744*4882a593Smuzhiyun 			tlv_len = sizeof(*summary) - sizeof(*tlv);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 			if (tlv_len < (sizeof(*summary) - sizeof(*tlv))) {
747*4882a593Smuzhiyun 				ath11k_warn(ab, "failed to parse spectral summary at bytes %d tlv_len:%d\n",
748*4882a593Smuzhiyun 					    i, tlv_len);
749*4882a593Smuzhiyun 				ret = -EINVAL;
750*4882a593Smuzhiyun 				goto err;
751*4882a593Smuzhiyun 			}
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 			summary = (struct spectral_summary_fft_report *)tlv;
754*4882a593Smuzhiyun 			ath11k_spectral_pull_summary(ar, &param->meta,
755*4882a593Smuzhiyun 						     summary, &summ_rpt);
756*4882a593Smuzhiyun 			break;
757*4882a593Smuzhiyun 		case ATH11K_SPECTRAL_TAG_SCAN_SEARCH:
758*4882a593Smuzhiyun 			if (tlv_len < (sizeof(struct spectral_search_fft_report) -
759*4882a593Smuzhiyun 				       sizeof(*tlv))) {
760*4882a593Smuzhiyun 				ath11k_warn(ab, "failed to parse spectral search fft at bytes %d\n",
761*4882a593Smuzhiyun 					    i);
762*4882a593Smuzhiyun 				ret = -EINVAL;
763*4882a593Smuzhiyun 				goto err;
764*4882a593Smuzhiyun 			}
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 			memset(fft_sample, 0, sample_sz);
767*4882a593Smuzhiyun 			ret = ath11k_spectral_process_fft(ar, &summ_rpt, tlv,
768*4882a593Smuzhiyun 							  fft_sample,
769*4882a593Smuzhiyun 							  data_len - i);
770*4882a593Smuzhiyun 			if (ret) {
771*4882a593Smuzhiyun 				ath11k_warn(ab, "failed to process spectral fft at bytes %d\n",
772*4882a593Smuzhiyun 					    i);
773*4882a593Smuzhiyun 				goto err;
774*4882a593Smuzhiyun 			}
775*4882a593Smuzhiyun 			quit = true;
776*4882a593Smuzhiyun 			break;
777*4882a593Smuzhiyun 		}
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 		i += sizeof(*tlv) + tlv_len;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	ret = 0;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun err:
785*4882a593Smuzhiyun 	kfree(fft_sample);
786*4882a593Smuzhiyun unlock:
787*4882a593Smuzhiyun 	spin_unlock_bh(&ar->spectral.lock);
788*4882a593Smuzhiyun 	return ret;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun 
ath11k_spectral_ring_alloc(struct ath11k * ar,struct ath11k_dbring_cap * db_cap)791*4882a593Smuzhiyun static int ath11k_spectral_ring_alloc(struct ath11k *ar,
792*4882a593Smuzhiyun 				      struct ath11k_dbring_cap *db_cap)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	struct ath11k_spectral *sp = &ar->spectral;
795*4882a593Smuzhiyun 	int ret;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	ret = ath11k_dbring_srng_setup(ar, &sp->rx_ring,
798*4882a593Smuzhiyun 				       0, db_cap->min_elem);
799*4882a593Smuzhiyun 	if (ret) {
800*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to setup db ring\n");
801*4882a593Smuzhiyun 		return ret;
802*4882a593Smuzhiyun 	}
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	ath11k_dbring_set_cfg(ar, &sp->rx_ring,
805*4882a593Smuzhiyun 			      ATH11K_SPECTRAL_NUM_RESP_PER_EVENT,
806*4882a593Smuzhiyun 			      ATH11K_SPECTRAL_EVENT_TIMEOUT_MS,
807*4882a593Smuzhiyun 			      ath11k_spectral_process_data);
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	ret = ath11k_dbring_buf_setup(ar, &sp->rx_ring, db_cap);
810*4882a593Smuzhiyun 	if (ret) {
811*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to setup db ring buffer\n");
812*4882a593Smuzhiyun 		goto srng_cleanup;
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	ret = ath11k_dbring_wmi_cfg_setup(ar, &sp->rx_ring,
816*4882a593Smuzhiyun 					  WMI_DIRECT_BUF_SPECTRAL);
817*4882a593Smuzhiyun 	if (ret) {
818*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to setup db ring cfg\n");
819*4882a593Smuzhiyun 		goto buffer_cleanup;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	return 0;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun buffer_cleanup:
825*4882a593Smuzhiyun 	ath11k_dbring_buf_cleanup(ar, &sp->rx_ring);
826*4882a593Smuzhiyun srng_cleanup:
827*4882a593Smuzhiyun 	ath11k_dbring_srng_cleanup(ar, &sp->rx_ring);
828*4882a593Smuzhiyun 	return ret;
829*4882a593Smuzhiyun }
830*4882a593Smuzhiyun 
ath11k_spectral_ring_free(struct ath11k * ar)831*4882a593Smuzhiyun static inline void ath11k_spectral_ring_free(struct ath11k *ar)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	struct ath11k_spectral *sp = &ar->spectral;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	ath11k_dbring_srng_cleanup(ar, &sp->rx_ring);
836*4882a593Smuzhiyun 	ath11k_dbring_buf_cleanup(ar, &sp->rx_ring);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun 
ath11k_spectral_debug_unregister(struct ath11k * ar)839*4882a593Smuzhiyun static inline void ath11k_spectral_debug_unregister(struct ath11k *ar)
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun 	debugfs_remove(ar->spectral.scan_bins);
842*4882a593Smuzhiyun 	ar->spectral.scan_bins = NULL;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	debugfs_remove(ar->spectral.scan_count);
845*4882a593Smuzhiyun 	ar->spectral.scan_count = NULL;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	debugfs_remove(ar->spectral.scan_ctl);
848*4882a593Smuzhiyun 	ar->spectral.scan_ctl = NULL;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	if (ar->spectral.rfs_scan) {
851*4882a593Smuzhiyun 		relay_close(ar->spectral.rfs_scan);
852*4882a593Smuzhiyun 		ar->spectral.rfs_scan = NULL;
853*4882a593Smuzhiyun 	}
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun 
ath11k_spectral_vif_stop(struct ath11k_vif * arvif)856*4882a593Smuzhiyun int ath11k_spectral_vif_stop(struct ath11k_vif *arvif)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	if (!arvif->spectral_enabled)
859*4882a593Smuzhiyun 		return 0;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	return ath11k_spectral_scan_config(arvif->ar, ATH11K_SPECTRAL_DISABLED);
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun 
ath11k_spectral_reset_buffer(struct ath11k * ar)864*4882a593Smuzhiyun void ath11k_spectral_reset_buffer(struct ath11k *ar)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	if (!ar->spectral.enabled)
867*4882a593Smuzhiyun 		return;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (ar->spectral.rfs_scan)
870*4882a593Smuzhiyun 		relay_reset(ar->spectral.rfs_scan);
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
ath11k_spectral_deinit(struct ath11k_base * ab)873*4882a593Smuzhiyun void ath11k_spectral_deinit(struct ath11k_base *ab)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun 	struct ath11k *ar;
876*4882a593Smuzhiyun 	struct ath11k_spectral *sp;
877*4882a593Smuzhiyun 	int i;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	for (i = 0; i <  ab->num_radios; i++) {
880*4882a593Smuzhiyun 		ar = ab->pdevs[i].ar;
881*4882a593Smuzhiyun 		sp = &ar->spectral;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 		if (!sp->enabled)
884*4882a593Smuzhiyun 			continue;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 		mutex_lock(&ar->conf_mutex);
887*4882a593Smuzhiyun 		ath11k_spectral_scan_config(ar, ATH11K_SPECTRAL_DISABLED);
888*4882a593Smuzhiyun 		mutex_unlock(&ar->conf_mutex);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 		spin_lock_bh(&sp->lock);
891*4882a593Smuzhiyun 		sp->enabled = false;
892*4882a593Smuzhiyun 		spin_unlock_bh(&sp->lock);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 		ath11k_spectral_debug_unregister(ar);
895*4882a593Smuzhiyun 		ath11k_spectral_ring_free(ar);
896*4882a593Smuzhiyun 	}
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
ath11k_spectral_debug_register(struct ath11k * ar)899*4882a593Smuzhiyun static inline int ath11k_spectral_debug_register(struct ath11k *ar)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	int ret;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	ar->spectral.rfs_scan = relay_open("spectral_scan",
904*4882a593Smuzhiyun 					   ar->debug.debugfs_pdev,
905*4882a593Smuzhiyun 					   ATH11K_SPECTRAL_SUB_BUFF_SIZE,
906*4882a593Smuzhiyun 					   ATH11K_SPECTRAL_NUM_SUB_BUF,
907*4882a593Smuzhiyun 					   &rfs_scan_cb, NULL);
908*4882a593Smuzhiyun 	if (!ar->spectral.rfs_scan) {
909*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to open relay in pdev %d\n",
910*4882a593Smuzhiyun 			    ar->pdev_idx);
911*4882a593Smuzhiyun 		return -EINVAL;
912*4882a593Smuzhiyun 	}
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	ar->spectral.scan_ctl = debugfs_create_file("spectral_scan_ctl",
915*4882a593Smuzhiyun 						    0600,
916*4882a593Smuzhiyun 						    ar->debug.debugfs_pdev, ar,
917*4882a593Smuzhiyun 						    &fops_scan_ctl);
918*4882a593Smuzhiyun 	if (!ar->spectral.scan_ctl) {
919*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to open debugfs in pdev %d\n",
920*4882a593Smuzhiyun 			    ar->pdev_idx);
921*4882a593Smuzhiyun 		ret = -EINVAL;
922*4882a593Smuzhiyun 		goto debug_unregister;
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	ar->spectral.scan_count = debugfs_create_file("spectral_count",
926*4882a593Smuzhiyun 						      0600,
927*4882a593Smuzhiyun 						      ar->debug.debugfs_pdev, ar,
928*4882a593Smuzhiyun 						      &fops_scan_count);
929*4882a593Smuzhiyun 	if (!ar->spectral.scan_count) {
930*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to open debugfs in pdev %d\n",
931*4882a593Smuzhiyun 			    ar->pdev_idx);
932*4882a593Smuzhiyun 		ret = -EINVAL;
933*4882a593Smuzhiyun 		goto debug_unregister;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	ar->spectral.scan_bins = debugfs_create_file("spectral_bins",
937*4882a593Smuzhiyun 						     0600,
938*4882a593Smuzhiyun 						     ar->debug.debugfs_pdev, ar,
939*4882a593Smuzhiyun 						     &fops_scan_bins);
940*4882a593Smuzhiyun 	if (!ar->spectral.scan_bins) {
941*4882a593Smuzhiyun 		ath11k_warn(ar->ab, "failed to open debugfs in pdev %d\n",
942*4882a593Smuzhiyun 			    ar->pdev_idx);
943*4882a593Smuzhiyun 		ret = -EINVAL;
944*4882a593Smuzhiyun 		goto debug_unregister;
945*4882a593Smuzhiyun 	}
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	return 0;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun debug_unregister:
950*4882a593Smuzhiyun 	ath11k_spectral_debug_unregister(ar);
951*4882a593Smuzhiyun 	return ret;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
ath11k_spectral_init(struct ath11k_base * ab)954*4882a593Smuzhiyun int ath11k_spectral_init(struct ath11k_base *ab)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun 	struct ath11k *ar;
957*4882a593Smuzhiyun 	struct ath11k_spectral *sp;
958*4882a593Smuzhiyun 	struct ath11k_dbring_cap db_cap;
959*4882a593Smuzhiyun 	int ret;
960*4882a593Smuzhiyun 	int i;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	if (!test_bit(WMI_TLV_SERVICE_FREQINFO_IN_METADATA,
963*4882a593Smuzhiyun 		      ab->wmi_ab.svc_map))
964*4882a593Smuzhiyun 		return 0;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	if (!ab->hw_params.spectral_fft_sz)
967*4882a593Smuzhiyun 		return 0;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	for (i = 0; i < ab->num_radios; i++) {
970*4882a593Smuzhiyun 		ar = ab->pdevs[i].ar;
971*4882a593Smuzhiyun 		sp = &ar->spectral;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		ret = ath11k_dbring_get_cap(ar->ab, ar->pdev_idx,
974*4882a593Smuzhiyun 					    WMI_DIRECT_BUF_SPECTRAL,
975*4882a593Smuzhiyun 					    &db_cap);
976*4882a593Smuzhiyun 		if (ret)
977*4882a593Smuzhiyun 			continue;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 		idr_init(&sp->rx_ring.bufs_idr);
980*4882a593Smuzhiyun 		spin_lock_init(&sp->rx_ring.idr_lock);
981*4882a593Smuzhiyun 		spin_lock_init(&sp->lock);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		ret = ath11k_spectral_ring_alloc(ar, &db_cap);
984*4882a593Smuzhiyun 		if (ret) {
985*4882a593Smuzhiyun 			ath11k_warn(ab, "failed to init spectral ring for pdev %d\n",
986*4882a593Smuzhiyun 				    i);
987*4882a593Smuzhiyun 			goto deinit;
988*4882a593Smuzhiyun 		}
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 		spin_lock_bh(&sp->lock);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 		sp->mode = ATH11K_SPECTRAL_DISABLED;
993*4882a593Smuzhiyun 		sp->count = ATH11K_WMI_SPECTRAL_COUNT_DEFAULT;
994*4882a593Smuzhiyun 		sp->fft_size = ATH11K_WMI_SPECTRAL_FFT_SIZE_DEFAULT;
995*4882a593Smuzhiyun 		sp->enabled = true;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 		spin_unlock_bh(&sp->lock);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 		ret = ath11k_spectral_debug_register(ar);
1000*4882a593Smuzhiyun 		if (ret) {
1001*4882a593Smuzhiyun 			ath11k_warn(ab, "failed to register spectral for pdev %d\n",
1002*4882a593Smuzhiyun 				    i);
1003*4882a593Smuzhiyun 			goto deinit;
1004*4882a593Smuzhiyun 		}
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	return 0;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun deinit:
1010*4882a593Smuzhiyun 	ath11k_spectral_deinit(ab);
1011*4882a593Smuzhiyun 	return ret;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun 
ath11k_spectral_get_mode(struct ath11k * ar)1014*4882a593Smuzhiyun enum ath11k_spectral_mode ath11k_spectral_get_mode(struct ath11k *ar)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	if (ar->spectral.enabled)
1017*4882a593Smuzhiyun 		return ar->spectral.mode;
1018*4882a593Smuzhiyun 	else
1019*4882a593Smuzhiyun 		return ATH11K_SPECTRAL_DISABLED;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
ath11k_spectral_get_dbring(struct ath11k * ar)1022*4882a593Smuzhiyun struct ath11k_dbring *ath11k_spectral_get_dbring(struct ath11k *ar)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	if (ar->spectral.enabled)
1025*4882a593Smuzhiyun 		return &ar->spectral.rx_ring;
1026*4882a593Smuzhiyun 	else
1027*4882a593Smuzhiyun 		return NULL;
1028*4882a593Smuzhiyun }
1029