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(PPB) ((PPB * MIN_PPB_THRESH + 50) / 100)
45 #define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
46 /* percentage of pulse width tolerance */
47 #define WIDTH_TOLERANCE 5
48 #define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
49 #define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
50 
51 #define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB)	\
52 {								\
53 	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
54 	(PRF2PRI(PMAX) - PRI_TOLERANCE),			\
55 	(PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,	\
56 	PPB_THRESH(PPB), PRI_TOLERANCE,				\
57 }
58 
59 /* radar types as defined by ETSI EN-301-893 v1.5.1 */
60 static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
61 	ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18),
62 	ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10),
63 	ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15),
64 	ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25),
65 	ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20),
66 	ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10),
67 	ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15),
68 };
69 
70 static const struct radar_types etsi_radar_types_v15 = {
71 	.region			= NL80211_DFS_ETSI,
72 	.num_radar_types	= ARRAY_SIZE(etsi_radar_ref_types_v15),
73 	.radar_types		= etsi_radar_ref_types_v15,
74 };
75 
76 #define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB)	\
77 {								\
78 	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
79 	PMIN - PRI_TOLERANCE,					\
80 	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
81 	PPB_THRESH(PPB), PRI_TOLERANCE,				\
82 }
83 
84 static const struct radar_detector_specs fcc_radar_ref_types[] = {
85 	FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18),
86 	FCC_PATTERN(1, 0, 5, 150, 230, 1, 23),
87 	FCC_PATTERN(2, 6, 10, 200, 500, 1, 16),
88 	FCC_PATTERN(3, 11, 20, 200, 500, 1, 12),
89 	FCC_PATTERN(4, 50, 100, 1000, 2000, 1, 20),
90 	FCC_PATTERN(5, 0, 1, 333, 333, 1, 9),
91 };
92 
93 static const struct radar_types fcc_radar_types = {
94 	.region			= NL80211_DFS_FCC,
95 	.num_radar_types	= ARRAY_SIZE(fcc_radar_ref_types),
96 	.radar_types		= fcc_radar_ref_types,
97 };
98 
99 #define JP_PATTERN FCC_PATTERN
100 static const struct radar_detector_specs jp_radar_ref_types[] = {
101 	JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18),
102 	JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18),
103 	JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18),
104 	JP_PATTERN(3, 1, 2, 4000, 4000, 1, 18),
105 	JP_PATTERN(4, 0, 5, 150, 230, 1, 23),
106 	JP_PATTERN(5, 6, 10, 200, 500, 1, 16),
107 	JP_PATTERN(6, 11, 20, 200, 500, 1, 12),
108 	JP_PATTERN(7, 50, 100, 1000, 2000, 1, 20),
109 	JP_PATTERN(5, 0, 1, 333, 333, 1, 9),
110 };
111 
112 static const struct radar_types jp_radar_types = {
113 	.region			= NL80211_DFS_JP,
114 	.num_radar_types	= ARRAY_SIZE(jp_radar_ref_types),
115 	.radar_types		= jp_radar_ref_types,
116 };
117 
118 static const struct radar_types *dfs_domains[] = {
119 	&etsi_radar_types_v15,
120 	&fcc_radar_types,
121 	&jp_radar_types,
122 };
123 
124 /**
125  * get_dfs_domain_radar_types() - get radar types for a given DFS domain
126  * @param domain DFS domain
127  * @return radar_types ptr on success, NULL if DFS domain is not supported
128  */
129 static const struct radar_types *
130 get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
131 {
132 	u32 i;
133 	for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
134 		if (dfs_domains[i]->region == region)
135 			return dfs_domains[i];
136 	}
137 	return NULL;
138 }
139 
140 /**
141  * struct channel_detector - detector elements for a DFS channel
142  * @head: list_head
143  * @freq: frequency for this channel detector in MHz
144  * @detectors: array of dynamically created detector elements for this freq
145  *
146  * Channel detectors are required to provide multi-channel DFS detection, e.g.
147  * to support off-channel scanning. A pattern detector has a list of channels
148  * radar pulses have been reported for in the past.
149  */
150 struct channel_detector {
151 	struct list_head head;
152 	u16 freq;
153 	struct pri_detector **detectors;
154 };
155 
156 /* channel_detector_reset() - reset detector lines for a given channel */
157 static void channel_detector_reset(struct dfs_pattern_detector *dpd,
158 				   struct channel_detector *cd)
159 {
160 	u32 i;
161 	if (cd == NULL)
162 		return;
163 	for (i = 0; i < dpd->num_radar_types; i++)
164 		cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
165 }
166 
167 /* channel_detector_exit() - destructor */
168 static void channel_detector_exit(struct dfs_pattern_detector *dpd,
169 				  struct channel_detector *cd)
170 {
171 	u32 i;
172 	if (cd == NULL)
173 		return;
174 	list_del(&cd->head);
175 	for (i = 0; i < dpd->num_radar_types; i++) {
176 		struct pri_detector *de = cd->detectors[i];
177 		if (de != NULL)
178 			de->exit(de);
179 	}
180 	kfree(cd->detectors);
181 	kfree(cd);
182 }
183 
184 static struct channel_detector *
185 channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
186 {
187 	u32 sz, i;
188 	struct channel_detector *cd;
189 
190 	cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
191 	if (cd == NULL)
192 		goto fail;
193 
194 	INIT_LIST_HEAD(&cd->head);
195 	cd->freq = freq;
196 	sz = sizeof(cd->detectors) * dpd->num_radar_types;
197 	cd->detectors = kzalloc(sz, GFP_ATOMIC);
198 	if (cd->detectors == NULL)
199 		goto fail;
200 
201 	for (i = 0; i < dpd->num_radar_types; i++) {
202 		const struct radar_detector_specs *rs = &dpd->radar_spec[i];
203 		struct pri_detector *de = pri_detector_init(rs);
204 		if (de == NULL)
205 			goto fail;
206 		cd->detectors[i] = de;
207 	}
208 	list_add(&cd->head, &dpd->channel_detectors);
209 	return cd;
210 
211 fail:
212 	ath_dbg(dpd->common, DFS,
213 		"failed to allocate channel_detector for freq=%d\n", freq);
214 	channel_detector_exit(dpd, cd);
215 	return NULL;
216 }
217 
218 /**
219  * channel_detector_get() - get channel detector for given frequency
220  * @param dpd instance pointer
221  * @param freq frequency in MHz
222  * @return pointer to channel detector on success, NULL otherwise
223  *
224  * Return existing channel detector for the given frequency or return a
225  * newly create one.
226  */
227 static struct channel_detector *
228 channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
229 {
230 	struct channel_detector *cd;
231 	list_for_each_entry(cd, &dpd->channel_detectors, head) {
232 		if (cd->freq == freq)
233 			return cd;
234 	}
235 	return channel_detector_create(dpd, freq);
236 }
237 
238 /*
239  * DFS Pattern Detector
240  */
241 
242 /* dpd_reset(): reset all channel detectors */
243 static void dpd_reset(struct dfs_pattern_detector *dpd)
244 {
245 	struct channel_detector *cd;
246 	if (!list_empty(&dpd->channel_detectors))
247 		list_for_each_entry(cd, &dpd->channel_detectors, head)
248 			channel_detector_reset(dpd, cd);
249 
250 }
251 static void dpd_exit(struct dfs_pattern_detector *dpd)
252 {
253 	struct channel_detector *cd, *cd0;
254 	if (!list_empty(&dpd->channel_detectors))
255 		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
256 			channel_detector_exit(dpd, cd);
257 	kfree(dpd);
258 }
259 
260 static bool
261 dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
262 {
263 	u32 i;
264 	struct channel_detector *cd;
265 
266 	/*
267 	 * pulses received for a non-supported or un-initialized
268 	 * domain are treated as detected radars for fail-safety
269 	 */
270 	if (dpd->region == NL80211_DFS_UNSET)
271 		return true;
272 
273 	cd = channel_detector_get(dpd, event->freq);
274 	if (cd == NULL)
275 		return false;
276 
277 	dpd->last_pulse_ts = event->ts;
278 	/* reset detector on time stamp wraparound, caused by TSF reset */
279 	if (event->ts < dpd->last_pulse_ts)
280 		dpd_reset(dpd);
281 
282 	/* do type individual pattern matching */
283 	for (i = 0; i < dpd->num_radar_types; i++) {
284 		struct pri_detector *pd = cd->detectors[i];
285 		struct pri_sequence *ps = pd->add_pulse(pd, event);
286 		if (ps != NULL) {
287 			ath_dbg(dpd->common, DFS,
288 				"DFS: radar found on freq=%d: id=%d, pri=%d, "
289 				"count=%d, count_false=%d\n",
290 				event->freq, pd->rs->type_id,
291 				ps->pri, ps->count, ps->count_falses);
292 			channel_detector_reset(dpd, cd);
293 			return true;
294 		}
295 	}
296 	return false;
297 }
298 
299 static struct ath_dfs_pool_stats
300 dpd_get_stats(struct dfs_pattern_detector *dpd)
301 {
302 	return global_dfs_pool_stats;
303 }
304 
305 static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
306 			   enum nl80211_dfs_regions region)
307 {
308 	const struct radar_types *rt;
309 	struct channel_detector *cd, *cd0;
310 
311 	if (dpd->region == region)
312 		return true;
313 
314 	dpd->region = NL80211_DFS_UNSET;
315 
316 	rt = get_dfs_domain_radar_types(region);
317 	if (rt == NULL)
318 		return false;
319 
320 	/* delete all channel detectors for previous DFS domain */
321 	if (!list_empty(&dpd->channel_detectors))
322 		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
323 			channel_detector_exit(dpd, cd);
324 	dpd->radar_spec = rt->radar_types;
325 	dpd->num_radar_types = rt->num_radar_types;
326 
327 	dpd->region = region;
328 	return true;
329 }
330 
331 static struct dfs_pattern_detector default_dpd = {
332 	.exit		= dpd_exit,
333 	.set_dfs_domain	= dpd_set_domain,
334 	.add_pulse	= dpd_add_pulse,
335 	.get_stats	= dpd_get_stats,
336 	.region		= NL80211_DFS_UNSET,
337 };
338 
339 struct dfs_pattern_detector *
340 dfs_pattern_detector_init(struct ath_common *common,
341 			  enum nl80211_dfs_regions region)
342 {
343 	struct dfs_pattern_detector *dpd;
344 
345 	if (!config_enabled(CONFIG_CFG80211_CERTIFICATION_ONUS))
346 		return NULL;
347 
348 	dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
349 	if (dpd == NULL)
350 		return NULL;
351 
352 	*dpd = default_dpd;
353 	INIT_LIST_HEAD(&dpd->channel_detectors);
354 
355 	dpd->common = common;
356 	if (dpd->set_dfs_domain(dpd, region))
357 		return dpd;
358 
359 	ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
360 	kfree(dpd);
361 	return NULL;
362 }
363 EXPORT_SYMBOL(dfs_pattern_detector_init);
364