xref: /openbmc/linux/drivers/net/wireless/ath/dfs_pattern_detector.c (revision eb3fcf007fffe5830d815e713591f3e858f2a365)
1 /*
2  * Copyright (c) 2012 Neratec Solutions AG
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/slab.h>
18 #include <linux/export.h>
19 
20 #include "dfs_pattern_detector.h"
21 #include "dfs_pri_detector.h"
22 #include "ath.h"
23 
24 /*
25  * tolerated deviation of radar time stamp in usecs on both sides
26  * TODO: this might need to be HW-dependent
27  */
28 #define PRI_TOLERANCE	16
29 
30 /**
31  * struct radar_types - contains array of patterns defined for one DFS domain
32  * @domain: DFS regulatory domain
33  * @num_radar_types: number of radar types to follow
34  * @radar_types: radar types array
35  */
36 struct radar_types {
37 	enum nl80211_dfs_regions region;
38 	u32 num_radar_types;
39 	const struct radar_detector_specs *radar_types;
40 };
41 
42 /* percentage on ppb threshold to trigger detection */
43 #define MIN_PPB_THRESH	50
44 #define PPB_THRESH_RATE(PPB, RATE) ((PPB * RATE + 100 - RATE) / 100)
45 #define PPB_THRESH(PPB) PPB_THRESH_RATE(PPB, MIN_PPB_THRESH)
46 #define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
47 /* percentage of pulse width tolerance */
48 #define WIDTH_TOLERANCE 5
49 #define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
50 #define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
51 
52 #define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
53 {								\
54 	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
55 	(PRF2PRI(PMAX) - PRI_TOLERANCE),			\
56 	(PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,	\
57 	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
58 }
59 
60 /* radar types as defined by ETSI EN-301-893 v1.5.1 */
61 static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
62 	ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18, false),
63 	ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10, false),
64 	ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15, false),
65 	ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25, false),
66 	ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20, false),
67 	ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10, false),
68 	ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15, false),
69 };
70 
71 static const struct radar_types etsi_radar_types_v15 = {
72 	.region			= NL80211_DFS_ETSI,
73 	.num_radar_types	= ARRAY_SIZE(etsi_radar_ref_types_v15),
74 	.radar_types		= etsi_radar_ref_types_v15,
75 };
76 
77 #define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
78 {								\
79 	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
80 	PMIN - PRI_TOLERANCE,					\
81 	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
82 	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
83 }
84 
85 /* radar types released on August 14, 2014
86  * type 1 PRI values randomly selected within the range of 518 and 3066.
87  * divide it to 3 groups is good enough for both of radar detection and
88  * avoiding false detection based on practical test results
89  * collected for more than a year.
90  */
91 static const struct radar_detector_specs fcc_radar_ref_types[] = {
92 	FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18, false),
93 	FCC_PATTERN(101, 0, 1, 518, 938, 1, 57, false),
94 	FCC_PATTERN(102, 0, 1, 938, 2000, 1, 27, false),
95 	FCC_PATTERN(103, 0, 1, 2000, 3066, 1, 18, false),
96 	FCC_PATTERN(2, 0, 5, 150, 230, 1, 23, false),
97 	FCC_PATTERN(3, 6, 10, 200, 500, 1, 16, false),
98 	FCC_PATTERN(4, 11, 20, 200, 500, 1, 12, false),
99 	FCC_PATTERN(5, 50, 100, 1000, 2000, 1, 1, true),
100 	FCC_PATTERN(6, 0, 1, 333, 333, 1, 9, false),
101 };
102 
103 static const struct radar_types fcc_radar_types = {
104 	.region			= NL80211_DFS_FCC,
105 	.num_radar_types	= ARRAY_SIZE(fcc_radar_ref_types),
106 	.radar_types		= fcc_radar_ref_types,
107 };
108 
109 #define JP_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, RATE, CHIRP)	\
110 {								\
111 	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
112 	PMIN - PRI_TOLERANCE,					\
113 	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
114 	PPB_THRESH_RATE(PPB, RATE), PRI_TOLERANCE, CHIRP	\
115 }
116 static const struct radar_detector_specs jp_radar_ref_types[] = {
117 	JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18, 29, false),
118 	JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18, 29, false),
119 	JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18, 50, false),
120 	JP_PATTERN(3, 1, 2, 4000, 4000, 1, 18, 50, false),
121 	JP_PATTERN(4, 0, 5, 150, 230, 1, 23, 50, false),
122 	JP_PATTERN(5, 6, 10, 200, 500, 1, 16, 50, false),
123 	JP_PATTERN(6, 11, 20, 200, 500, 1, 12, 50, false),
124 	JP_PATTERN(7, 50, 100, 1000, 2000, 1, 20, 50, false),
125 	JP_PATTERN(5, 0, 1, 333, 333, 1, 9, 50, false),
126 };
127 
128 static const struct radar_types jp_radar_types = {
129 	.region			= NL80211_DFS_JP,
130 	.num_radar_types	= ARRAY_SIZE(jp_radar_ref_types),
131 	.radar_types		= jp_radar_ref_types,
132 };
133 
134 static const struct radar_types *dfs_domains[] = {
135 	&etsi_radar_types_v15,
136 	&fcc_radar_types,
137 	&jp_radar_types,
138 };
139 
140 /**
141  * get_dfs_domain_radar_types() - get radar types for a given DFS domain
142  * @param domain DFS domain
143  * @return radar_types ptr on success, NULL if DFS domain is not supported
144  */
145 static const struct radar_types *
146 get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
147 {
148 	u32 i;
149 	for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
150 		if (dfs_domains[i]->region == region)
151 			return dfs_domains[i];
152 	}
153 	return NULL;
154 }
155 
156 /**
157  * struct channel_detector - detector elements for a DFS channel
158  * @head: list_head
159  * @freq: frequency for this channel detector in MHz
160  * @detectors: array of dynamically created detector elements for this freq
161  *
162  * Channel detectors are required to provide multi-channel DFS detection, e.g.
163  * to support off-channel scanning. A pattern detector has a list of channels
164  * radar pulses have been reported for in the past.
165  */
166 struct channel_detector {
167 	struct list_head head;
168 	u16 freq;
169 	struct pri_detector **detectors;
170 };
171 
172 /* channel_detector_reset() - reset detector lines for a given channel */
173 static void channel_detector_reset(struct dfs_pattern_detector *dpd,
174 				   struct channel_detector *cd)
175 {
176 	u32 i;
177 	if (cd == NULL)
178 		return;
179 	for (i = 0; i < dpd->num_radar_types; i++)
180 		cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
181 }
182 
183 /* channel_detector_exit() - destructor */
184 static void channel_detector_exit(struct dfs_pattern_detector *dpd,
185 				  struct channel_detector *cd)
186 {
187 	u32 i;
188 	if (cd == NULL)
189 		return;
190 	list_del(&cd->head);
191 	for (i = 0; i < dpd->num_radar_types; i++) {
192 		struct pri_detector *de = cd->detectors[i];
193 		if (de != NULL)
194 			de->exit(de);
195 	}
196 	kfree(cd->detectors);
197 	kfree(cd);
198 }
199 
200 static struct channel_detector *
201 channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
202 {
203 	u32 sz, i;
204 	struct channel_detector *cd;
205 
206 	cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
207 	if (cd == NULL)
208 		goto fail;
209 
210 	INIT_LIST_HEAD(&cd->head);
211 	cd->freq = freq;
212 	sz = sizeof(cd->detectors) * dpd->num_radar_types;
213 	cd->detectors = kzalloc(sz, GFP_ATOMIC);
214 	if (cd->detectors == NULL)
215 		goto fail;
216 
217 	for (i = 0; i < dpd->num_radar_types; i++) {
218 		const struct radar_detector_specs *rs = &dpd->radar_spec[i];
219 		struct pri_detector *de = pri_detector_init(rs);
220 		if (de == NULL)
221 			goto fail;
222 		cd->detectors[i] = de;
223 	}
224 	list_add(&cd->head, &dpd->channel_detectors);
225 	return cd;
226 
227 fail:
228 	ath_dbg(dpd->common, DFS,
229 		"failed to allocate channel_detector for freq=%d\n", freq);
230 	channel_detector_exit(dpd, cd);
231 	return NULL;
232 }
233 
234 /**
235  * channel_detector_get() - get channel detector for given frequency
236  * @param dpd instance pointer
237  * @param freq frequency in MHz
238  * @return pointer to channel detector on success, NULL otherwise
239  *
240  * Return existing channel detector for the given frequency or return a
241  * newly create one.
242  */
243 static struct channel_detector *
244 channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
245 {
246 	struct channel_detector *cd;
247 	list_for_each_entry(cd, &dpd->channel_detectors, head) {
248 		if (cd->freq == freq)
249 			return cd;
250 	}
251 	return channel_detector_create(dpd, freq);
252 }
253 
254 /*
255  * DFS Pattern Detector
256  */
257 
258 /* dpd_reset(): reset all channel detectors */
259 static void dpd_reset(struct dfs_pattern_detector *dpd)
260 {
261 	struct channel_detector *cd;
262 	if (!list_empty(&dpd->channel_detectors))
263 		list_for_each_entry(cd, &dpd->channel_detectors, head)
264 			channel_detector_reset(dpd, cd);
265 
266 }
267 static void dpd_exit(struct dfs_pattern_detector *dpd)
268 {
269 	struct channel_detector *cd, *cd0;
270 	if (!list_empty(&dpd->channel_detectors))
271 		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
272 			channel_detector_exit(dpd, cd);
273 	kfree(dpd);
274 }
275 
276 static bool
277 dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
278 {
279 	u32 i;
280 	struct channel_detector *cd;
281 
282 	/*
283 	 * pulses received for a non-supported or un-initialized
284 	 * domain are treated as detected radars for fail-safety
285 	 */
286 	if (dpd->region == NL80211_DFS_UNSET)
287 		return true;
288 
289 	cd = channel_detector_get(dpd, event->freq);
290 	if (cd == NULL)
291 		return false;
292 
293 	dpd->last_pulse_ts = event->ts;
294 	/* reset detector on time stamp wraparound, caused by TSF reset */
295 	if (event->ts < dpd->last_pulse_ts)
296 		dpd_reset(dpd);
297 
298 	/* do type individual pattern matching */
299 	for (i = 0; i < dpd->num_radar_types; i++) {
300 		struct pri_detector *pd = cd->detectors[i];
301 		struct pri_sequence *ps = pd->add_pulse(pd, event);
302 		if (ps != NULL) {
303 			ath_dbg(dpd->common, DFS,
304 				"DFS: radar found on freq=%d: id=%d, pri=%d, "
305 				"count=%d, count_false=%d\n",
306 				event->freq, pd->rs->type_id,
307 				ps->pri, ps->count, ps->count_falses);
308 			pd->reset(pd, dpd->last_pulse_ts);
309 			return true;
310 		}
311 	}
312 	return false;
313 }
314 
315 static struct ath_dfs_pool_stats
316 dpd_get_stats(struct dfs_pattern_detector *dpd)
317 {
318 	return global_dfs_pool_stats;
319 }
320 
321 static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
322 			   enum nl80211_dfs_regions region)
323 {
324 	const struct radar_types *rt;
325 	struct channel_detector *cd, *cd0;
326 
327 	if (dpd->region == region)
328 		return true;
329 
330 	dpd->region = NL80211_DFS_UNSET;
331 
332 	rt = get_dfs_domain_radar_types(region);
333 	if (rt == NULL)
334 		return false;
335 
336 	/* delete all channel detectors for previous DFS domain */
337 	if (!list_empty(&dpd->channel_detectors))
338 		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
339 			channel_detector_exit(dpd, cd);
340 	dpd->radar_spec = rt->radar_types;
341 	dpd->num_radar_types = rt->num_radar_types;
342 
343 	dpd->region = region;
344 	return true;
345 }
346 
347 static struct dfs_pattern_detector default_dpd = {
348 	.exit		= dpd_exit,
349 	.set_dfs_domain	= dpd_set_domain,
350 	.add_pulse	= dpd_add_pulse,
351 	.get_stats	= dpd_get_stats,
352 	.region		= NL80211_DFS_UNSET,
353 };
354 
355 struct dfs_pattern_detector *
356 dfs_pattern_detector_init(struct ath_common *common,
357 			  enum nl80211_dfs_regions region)
358 {
359 	struct dfs_pattern_detector *dpd;
360 
361 	if (!config_enabled(CONFIG_CFG80211_CERTIFICATION_ONUS))
362 		return NULL;
363 
364 	dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
365 	if (dpd == NULL)
366 		return NULL;
367 
368 	*dpd = default_dpd;
369 	INIT_LIST_HEAD(&dpd->channel_detectors);
370 
371 	dpd->common = common;
372 	if (dpd->set_dfs_domain(dpd, region))
373 		return dpd;
374 
375 	ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
376 	kfree(dpd);
377 	return NULL;
378 }
379 EXPORT_SYMBOL(dfs_pattern_detector_init);
380