xref: /wlan-dirver/qca-wifi-host-cmn/spectral/dispatcher/inc/wlan_spectral_public_structs.h (revision 11f5a63a6cbdda84849a730de22f0a71e635d58c)
1 /*
2  * Copyright (c) 2011,2017-2019 The Linux Foundation. All rights reserved.
3  *
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <qdf_types.h>
21 #include "wlan_dfs_ioctl.h"
22 #include <spectral_ioctl.h>
23 
24 #ifndef __KERNEL__
25 #include <math.h>
26 #endif /*  __KERNEL__  */
27 
28 #ifndef _WLAN_SPECTRAL_PUBLIC_STRUCTS_H_
29 #define _WLAN_SPECTRAL_PUBLIC_STRUCTS_H_
30 
31 #ifndef AH_MAX_CHAINS
32 #define AH_MAX_CHAINS 3
33 #endif
34 
35 #define MAX_NUM_CHANNELS             255
36 #define SPECTRAL_PHYERR_PARAM_NOVAL  65535
37 
38 #ifdef SPECTRAL_USE_EMU_DEFAULTS
39 /* Use defaults from emulation */
40 #define SPECTRAL_SCAN_ACTIVE_DEFAULT           (0x0)
41 #define SPECTRAL_SCAN_ENABLE_DEFAULT           (0x0)
42 #define SPECTRAL_SCAN_COUNT_DEFAULT            (0x0)
43 #define SPECTRAL_SCAN_PERIOD_DEFAULT           (250)
44 #define SPECTRAL_SCAN_PRIORITY_DEFAULT         (0x1)
45 #define SPECTRAL_SCAN_FFT_SIZE_DEFAULT         (0x7)
46 #define SPECTRAL_SCAN_GC_ENA_DEFAULT           (0x1)
47 #define SPECTRAL_SCAN_RESTART_ENA_DEFAULT      (0x0)
48 #define SPECTRAL_SCAN_NOISE_FLOOR_REF_DEFAULT  (0xa0)
49 #define SPECTRAL_SCAN_INIT_DELAY_DEFAULT       (0x50)
50 #define SPECTRAL_SCAN_NB_TONE_THR_DEFAULT      (0xc)
51 #define SPECTRAL_SCAN_STR_BIN_THR_DEFAULT      (0x7)
52 #define SPECTRAL_SCAN_WB_RPT_MODE_DEFAULT      (0x0)
53 #define SPECTRAL_SCAN_RSSI_RPT_MODE_DEFAULT    (0x1)
54 #define SPECTRAL_SCAN_RSSI_THR_DEFAULT         (0xf)
55 #define SPECTRAL_SCAN_PWR_FORMAT_DEFAULT       (0x1)
56 #define SPECTRAL_SCAN_RPT_MODE_DEFAULT         (0x2)
57 #define SPECTRAL_SCAN_BIN_SCALE_DEFAULT        (0x1)
58 #define SPECTRAL_SCAN_DBM_ADJ_DEFAULT          (0x0)
59 #define SPECTRAL_SCAN_CHN_MASK_DEFAULT         (0x1)
60 #else
61 /*
62  * Static default values for spectral state and configuration.
63  * These definitions should be treated as temporary. Ideally,
64  * we should get the defaults from firmware - this will be discussed.
65  *
66  * Use defaults from Spectral Hardware Micro-Architecture
67  * document (v1.0)
68  */
69 #define SPECTRAL_SCAN_ACTIVE_DEFAULT           (0)
70 #define SPECTRAL_SCAN_ENABLE_DEFAULT           (0)
71 #define SPECTRAL_SCAN_COUNT_DEFAULT            (0)
72 #define SPECTRAL_SCAN_PERIOD_GEN_I_DEFAULT     (35)
73 #define SPECTRAL_SCAN_PERIOD_GEN_II_DEFAULT    (35)
74 #define SPECTRAL_SCAN_PERIOD_GEN_III_DEFAULT   (224)
75 #define SPECTRAL_SCAN_PRIORITY_DEFAULT         (1)
76 #define SPECTRAL_SCAN_FFT_SIZE_DEFAULT         (7)
77 #define SPECTRAL_SCAN_GC_ENA_DEFAULT           (1)
78 #define SPECTRAL_SCAN_RESTART_ENA_DEFAULT      (0)
79 #define SPECTRAL_SCAN_NOISE_FLOOR_REF_DEFAULT  (-96)
80 #define SPECTRAL_SCAN_INIT_DELAY_DEFAULT       (80)
81 #define SPECTRAL_SCAN_NB_TONE_THR_DEFAULT      (12)
82 #define SPECTRAL_SCAN_STR_BIN_THR_DEFAULT      (8)
83 #define SPECTRAL_SCAN_WB_RPT_MODE_DEFAULT      (0)
84 #define SPECTRAL_SCAN_RSSI_RPT_MODE_DEFAULT    (0)
85 #define SPECTRAL_SCAN_RSSI_THR_DEFAULT         (0xf0)
86 #define SPECTRAL_SCAN_PWR_FORMAT_DEFAULT       (0)
87 #define SPECTRAL_SCAN_RPT_MODE_DEFAULT         (2)
88 #define SPECTRAL_SCAN_BIN_SCALE_DEFAULT        (1)
89 #define SPECTRAL_SCAN_DBM_ADJ_DEFAULT          (1)
90 #define SPECTRAL_SCAN_CHN_MASK_DEFAULT         (1)
91 #define SPECTRAL_SCAN_FREQUENCY_DEFAULT        (0)
92 #endif				/* SPECTRAL_USE_EMU_DEFAULTS */
93 
94 /* The below two definitions apply only to pre-11ac chipsets */
95 #define SPECTRAL_SCAN_SHORT_REPORT_DEFAULT     (1)
96 #define SPECTRAL_SCAN_FFT_PERIOD_DEFAULT       (1)
97 
98 /*
99  * Definitions to help in scaling of gen3 linear format Spectral bins to values
100  * similar to those from gen2 chipsets.
101  */
102 
103 /*
104  * Max gain for QCA9984. Since this chipset is a prime representative of gen2
105  * chipsets, it is chosen for this value.
106  */
107 #define SPECTRAL_QCA9984_MAX_GAIN                               (78)
108 
109 /* Temporary section for hard-coded values. These need to come from FW. */
110 
111 /* Max gain for IPQ8074 */
112 #define SPECTRAL_IPQ8074_DEFAULT_MAX_GAIN_HARDCODE              (62)
113 
114 /*
115  * Section for values needing tuning per customer platform. These too may need
116  * to come from FW. To be considered as hard-coded for now.
117  */
118 
119 /*
120  * If customers have a different gain line up than QCA reference designs for
121  * IPQ8074 and/or QCA9984, they may have to tune the low level threshold and
122  * the RSSI threshold.
123  */
124 #define SPECTRAL_SCALING_LOW_LEVEL_OFFSET                       (7)
125 #define SPECTRAL_SCALING_RSSI_THRESH                            (5)
126 
127 /*
128  * If customers set the AGC backoff differently, they may have to tune the high
129  * level threshold.
130  */
131 #define SPECTRAL_SCALING_HIGH_LEVEL_OFFSET                      (5)
132 
133 /* End of section for values needing fine tuning. */
134 /* End of temporary section for hard-coded values */
135 
136 /**
137  * enum spectral_msg_buf_type - Spectral message buffer type
138  * @SPECTRAL_MSG_BUF_NEW: Allocate new buffer
139  * @SPECTRAL_MSG_BUF_SAVED: Reuse last buffer, used for secondary segment report
140  *                          in case of 160 MHz.
141  */
142 enum spectral_msg_buf_type {
143 	SPECTRAL_MSG_BUF_NEW,
144 	SPECTRAL_MSG_BUF_SAVED,
145 	SPECTRAL_MSG_BUF_TYPE_MAX,
146 };
147 
148 /**
149  * enum spectral_msg_type - Spectral SAMP message type
150  * @SPECTRAL_MSG_NORMAL_MODE: Normal mode Spectral SAMP message
151  * @SPECTRAL_MSG_AGILE_MODE: Agile mode Spectral SAMP message
152  * @SPECTRAL_MSG_INTERFERENCE_NOTIFICATION: Interference notification to
153  *                                          external auto channel selection
154  *                                          entity
155  * @SPECTRAL_MSG_TYPE_MAX: Spectral SAMP message type max
156  */
157 enum spectral_msg_type {
158 	SPECTRAL_MSG_NORMAL_MODE,
159 	SPECTRAL_MSG_AGILE_MODE,
160 	SPECTRAL_MSG_INTERFERENCE_NOTIFICATION,
161 	SPECTRAL_MSG_TYPE_MAX,
162 };
163 
164 /**
165  * enum wlan_cfg80211_spectral_vendorcmd_handler_idx - Indices to cfg80211
166  * spectral vendor command handlers
167  * @SPECTRAL_SCAN_START_HANDLER_IDX:  Index to SPECTRAL_SCAN_START handler
168  * @SPECTRAL_SCAN_STOP_HANDLER_IDX:  Index to SPECTRAL_SCAN_STOP handler
169  * @SPECTRAL_SCAN_GET_CONFIG_HANDLER_IDX: Index to SPECTRAL_SCAN_GET_CONFIG
170  * handler
171  * @SPECTRAL_SCAN_GET_DIAG_STATS_HANDLER_IDX: Index to
172  * SPECTRAL_SCAN_GET_DIAG_STATS handler
173  * @SPECTRAL_SCAN_GET_CAP_HANDLER_IDX: Index to SPECTRAL_SCAN_GET_CAP handler
174  * @SPECTRAL_SCAN_GET_STATUS_HANDLER_IDX: Index to SPECTRAL_SCAN_GET_STATUS
175  * handler
176  * @SPECTRAL_SCAN_VENDOR_CMD_HANDLER_MAX: Number of cfg80211 spectral
177  * vendor command handlers supported
178  */
179 enum wlan_cfg80211_spectral_vendorcmd_handler_idx {
180 	SPECTRAL_SCAN_START_HANDLER_IDX,
181 	SPECTRAL_SCAN_STOP_HANDLER_IDX,
182 	SPECTRAL_SCAN_GET_CONFIG_HANDLER_IDX,
183 	SPECTRAL_SCAN_GET_DIAG_STATS_HANDLER_IDX,
184 	SPECTRAL_SCAN_GET_CAP_HANDLER_IDX,
185 	SPECTRAL_SCAN_GET_STATUS_HANDLER_IDX,
186 	SPECTRAL_SCAN_VENDOR_CMD_HANDLER_MAX,
187 };
188 
189 /**
190  * enum spectral_debug - Spectral debug level
191  * @DEBUG_SPECTRAL:  Minimal SPECTRAL debug
192  * @DEBUG_SPECTRAL1: Normal SPECTRAL debug
193  * @DEBUG_SPECTRAL2: Maximal SPECTRAL debug
194  * @DEBUG_SPECTRAL3: Matched filterID display
195  * @DEBUG_SPECTRAL4: One time dump of FFT report
196  */
197 enum spectral_debug {
198 	DEBUG_SPECTRAL = 0x00000100,
199 	DEBUG_SPECTRAL1 = 0x00000200,
200 	DEBUG_SPECTRAL2 = 0x00000400,
201 	DEBUG_SPECTRAL3 = 0x00000800,
202 	DEBUG_SPECTRAL4 = 0x00001000,
203 };
204 
205 /**
206  * enum spectral_capability_type - Spectral capability type
207  * @SPECTRAL_CAP_PHYDIAG:              Phydiag capability
208  * @SPECTRAL_CAP_RADAR:                Radar detection capability
209  * @SPECTRAL_CAP_SPECTRAL_SCAN:        Spectral capability
210  * @SPECTRAL_CAP_ADVNCD_SPECTRAL_SCAN: Advanced spectral capability
211  */
212 enum spectral_capability_type {
213 	SPECTRAL_CAP_PHYDIAG,
214 	SPECTRAL_CAP_RADAR,
215 	SPECTRAL_CAP_SPECTRAL_SCAN,
216 	SPECTRAL_CAP_ADVNCD_SPECTRAL_SCAN,
217 };
218 
219 /**
220  * enum spectral_cp_error_code - Spectral control path response code
221  * @SPECTRAL_SCAN_RESP_ERR_INVALID: Invalid error identifier
222  * @SPECTRAL_SCAN_RESP_ERR_PARAM_UNSUPPORTED: parameter unsupported
223  * @SPECTRAL_SCAN_RESP_ERR_MODE_UNSUPPORTED: mode unsupported
224  * @SPECTRAL_SCAN_RESP_ERR_PARAM_INVALID_VALUE: invalid parameter value
225  * @SPECTRAL_SCAN_RESP_ERR_PARAM_NOT_INITIALIZED: parameter uninitialized
226  */
227 enum spectral_cp_error_code {
228 	SPECTRAL_SCAN_ERR_INVALID,
229 	SPECTRAL_SCAN_ERR_PARAM_UNSUPPORTED,
230 	SPECTRAL_SCAN_ERR_MODE_UNSUPPORTED,
231 	SPECTRAL_SCAN_ERR_PARAM_INVALID_VALUE,
232 	SPECTRAL_SCAN_ERR_PARAM_NOT_INITIALIZED,
233 };
234 
235 /**
236  * struct spectral_chan_stats - channel status info
237  * @cycle_count:         Cycle count
238  * @channel_load:        Channel load
239  * @per:                 Period
240  * @noisefloor:          Noise floor
241  * @comp_usablity:       Computed usability
242  * @maxregpower:         Maximum allowed regulatory power
243  * @comp_usablity_sec80: Computed usability of secondary 80 Mhz
244  * @maxregpower_sec80:   Max regulatory power of secondary 80 Mhz
245  */
246 struct spectral_chan_stats {
247 	int cycle_count;
248 	int channel_load;
249 	int per;
250 	int noisefloor;
251 	uint16_t comp_usablity;
252 	int8_t maxregpower;
253 	uint16_t comp_usablity_sec80;
254 	int8_t maxregpower_sec80;
255 };
256 
257 /**
258  * struct spectral_diag_stats - spectral diag stats
259  * @spectral_mismatch:             Spectral TLV signature mismatches
260  * @spectral_sec80_sfft_insufflen: Insufficient length when parsing for
261  *                                 Secondary 80 Search FFT report
262  * @spectral_no_sec80_sfft:        Secondary 80 Search FFT report
263  *                                 TLV not found
264  * @spectral_vhtseg1id_mismatch:   VHT Operation Segment 1 ID
265  *                                 mismatches in Search FFT report
266  * @spectral_vhtseg2id_mismatch:   VHT Operation Segment 2 ID
267  *                                 mismatches in Search FFT report
268  * @spectral_invalid_detector_id:  Invalid detector id
269  */
270 struct spectral_diag_stats {
271 	uint64_t spectral_mismatch;
272 	uint64_t spectral_sec80_sfft_insufflen;
273 	uint64_t spectral_no_sec80_sfft;
274 	uint64_t spectral_vhtseg1id_mismatch;
275 	uint64_t spectral_vhtseg2id_mismatch;
276 	uint64_t spectral_invalid_detector_id;
277 };
278 
279 /**
280  * struct spectral_scan_state - State of spectral scan
281  * @is_active:  Is spectral scan active
282  * @is_enabled: Is spectral scan enabled
283  */
284 struct spectral_scan_state {
285 	uint8_t is_active;
286 	uint8_t is_enabled;
287 };
288 
289 /* Forward declarations */
290 struct wlan_objmgr_pdev;
291 
292 /**
293  * struct spectral_nl_cb - Spectral Netlink callbacks
294  * @get_sbuff:      Get the socket buffer to send the data to the application
295  * @send_nl_bcast:  Send data to the application using netlink broadcast
296  * @send_nl_unicast:  Send data to the application using netlink unicast
297  * @free_sbuff: Free the socket buffer for a particular message type
298  */
299 struct spectral_nl_cb {
300 	void *(*get_sbuff)(struct wlan_objmgr_pdev *pdev,
301 			   enum spectral_msg_type smsg_type,
302 			   enum spectral_msg_buf_type buf_type);
303 	int (*send_nl_bcast)(struct wlan_objmgr_pdev *pdev,
304 			     enum spectral_msg_type smsg_type);
305 	int (*send_nl_unicast)(struct wlan_objmgr_pdev *pdev,
306 			       enum spectral_msg_type smsg_type);
307 	void (*free_sbuff)(struct wlan_objmgr_pdev *pdev,
308 			   enum spectral_msg_type smsg_type);
309 };
310 
311 /**
312  * struct spectral_scan_config_request - Config request
313  * @sscan_config: Spectral parameters
314  * @sscan_err_code: Spectral scan error code
315  */
316 struct spectral_scan_config_request {
317 	struct spectral_config sscan_config;
318 	enum spectral_cp_error_code sscan_err_code;
319 };
320 
321 /**
322  * struct spectral_scan_action_request - Action request
323  * @sscan_err_code: Spectral scan error code
324  */
325 struct spectral_scan_action_request {
326 	enum spectral_cp_error_code sscan_err_code;
327 };
328 
329 /**
330  * struct spectral_scan_get_caps_request - Get caps request
331  * @sscan_caps: Spectral capabilities
332  * @sscan_err_code: Spectral scan error code
333  */
334 struct spectral_scan_get_caps_request {
335 	struct spectral_caps sscan_caps;
336 	enum spectral_cp_error_code sscan_err_code;
337 };
338 
339 /**
340  * struct spectral_scan_get_diag_request - Get diag request
341  * @sscan_diag: Spectral diag stats
342  * @sscan_err_code: Spectral scan error code
343  */
344 struct spectral_scan_get_diag_request {
345 	struct spectral_diag_stats sscan_diag;
346 	enum spectral_cp_error_code sscan_err_code;
347 };
348 
349 /**
350  * struct spectral_scan_get_chan_width_request - Get channel width request
351  * @chan_width: Channel width
352  * @sscan_err_code: Spectral scan error code
353  */
354 struct spectral_scan_get_chan_width_request {
355 	uint32_t chan_width;
356 	enum spectral_cp_error_code sscan_err_code;
357 };
358 
359 /**
360  * struct spectral_scan_get_status_request - Get status request
361  * @is_active: is Spectral scan active
362  * @is_enabled: is Spectral scan enabled
363  * @sscan_err_code: Spectral scan error code
364  */
365 struct spectral_scan_get_status_request {
366 	bool is_active;
367 	bool is_enabled;
368 	enum spectral_cp_error_code sscan_err_code;
369 };
370 
371 /**
372  * struct spectral_scan_debug_request - Get/set debug level request
373  * @spectral_dbg_level: Spectral debug level
374  * @sscan_err_code: Spectral scan error code
375  */
376 struct spectral_scan_debug_request {
377 	uint32_t spectral_dbg_level;
378 	enum spectral_cp_error_code sscan_err_code;
379 };
380 
381 /**
382  * struct spectral_cp_request - Spectral control path request
383  *                              Creating request and extracting response has to
384  *                              be atomic.
385  * @ss_mode: Spectral scan mode
386  * @req_id: Request identifier
387  */
388 struct spectral_cp_request {
389 	enum spectral_scan_mode ss_mode;
390 	uint8_t req_id;
391 	union {
392 		struct spectral_scan_config_request config_req;
393 		struct spectral_scan_action_request action_req;
394 		struct spectral_scan_get_caps_request caps_req;
395 		struct spectral_scan_get_diag_request diag_req;
396 		struct spectral_scan_get_chan_width_request chan_width_req;
397 		struct spectral_scan_get_status_request status_req;
398 		struct spectral_scan_debug_request debug_req;
399 	};
400 };
401 
402 #ifndef __KERNEL__
403 
404 static inline int16_t
405 spectral_pwfactor_max(int16_t pwfactor1,
406 		      int16_t pwfactor2)
407 {
408 	return ((pwfactor1 > pwfactor2) ? pwfactor1 : pwfactor2);
409 }
410 
411 /**
412  * get_spectral_scale_rssi_corr() - Compute RSSI correction factor for scaling
413  * @agc_total_gain_db: AGC total gain in dB steps
414  * @gen3_defmaxgain: Default max gain value of the gen III chipset
415  * @gen2_maxgain: Max gain value used by the reference gen II chipset
416  * @lowlevel_offset: Low level offset for scaling
417  * @inband_pwr: In band power in dB steps
418  * @rssi_thr: RSSI threshold for scaling
419  *
420  * Helper function to compute RSSI correction factor for Gen III linear format
421  * Spectral scaling. It is the responsibility of the caller to ensure that
422  * correct values are passed.
423  *
424  * Return: RSSI correction factor
425  */
426 static inline int16_t
427 get_spectral_scale_rssi_corr(u_int8_t agc_total_gain_db,
428 			     u_int8_t gen3_defmaxgain, u_int8_t gen2_maxgain,
429 			     int16_t lowlevel_offset, int16_t inband_pwr,
430 			     int16_t rssi_thr)
431 {
432 	return ((agc_total_gain_db < gen3_defmaxgain) ?
433 		(gen2_maxgain - gen3_defmaxgain + lowlevel_offset) :
434 		spectral_pwfactor_max((inband_pwr - rssi_thr), 0));
435 }
436 
437 /**
438  * spectral_scale_linear_to_gen2() - Scale linear bin value to gen II equivalent
439  * @gen3_binmag: Captured FFT bin value from the Spectral Search FFT report
440  * generated by the Gen III chipset
441  * @gen2_maxgain: Max gain value used by the reference gen II chipset
442  * @gen3_defmaxgain: Default max gain value of the gen III chipset
443  * @lowlevel_offset: Low level offset for scaling
444  * @inband_pwr: In band power in dB steps
445  * @rssi_thr: RSSI threshold for scaling
446  * @agc_total_gain_db: AGC total gain in dB steps
447  * @highlevel_offset: High level offset for scaling
448  * @gen2_bin_scale: Bin scale value used on reference gen II chipset
449  * @gen3_bin_scale: Bin scale value used on gen III chipset
450  *
451  * Helper function to scale a given gen III linear format bin value into an
452  * approximately equivalent gen II value. The scaled value can possibly be
453  * higher than 8 bits.  If the caller is incapable of handling values larger
454  * than 8 bits, the caller can saturate the value at 255. This function does not
455  * carry out this saturation for the sake of flexibility so that callers
456  * interested in the larger values can avail of this. Also note it is the
457  * responsibility of the caller to ensure that correct values are passed.
458  *
459  * Return: Scaled bin value
460  */
461 static inline u_int32_t
462 spectral_scale_linear_to_gen2(u_int8_t gen3_binmag,
463 			      u_int8_t gen2_maxgain, u_int8_t gen3_defmaxgain,
464 			      int16_t lowlevel_offset, int16_t inband_pwr,
465 			      int16_t rssi_thr, u_int8_t agc_total_gain_db,
466 			      int16_t highlevel_offset, u_int8_t gen2_bin_scale,
467 			      u_int8_t gen3_bin_scale)
468 {
469 	return (gen3_binmag *
470 		sqrt(pow(10, (((double)spectral_pwfactor_max(gen2_maxgain -
471 			gen3_defmaxgain + lowlevel_offset -
472 			get_spectral_scale_rssi_corr(agc_total_gain_db,
473 						     gen3_defmaxgain,
474 						     gen2_maxgain,
475 						     lowlevel_offset,
476 						     inband_pwr,
477 						     rssi_thr),
478 			(agc_total_gain_db < gen3_defmaxgain) *
479 				highlevel_offset)) / 10))) *
480 		 pow(2, (gen3_bin_scale - gen2_bin_scale)));
481 }
482 
483 #endif /*  __KERNEL__  */
484 
485 #endif				/* _WLAN_SPECTRAL_PUBLIC_STRUCTS_H_ */
486