ath9k: dfs move ath_dfs_pool_stats
[deliverable/linux.git] / drivers / net / wireless / ath / ath9k / dfs_pattern_detector.c
CommitLineData
6ee159e2
ZK
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"
95a5992e 22#include "../ath.h"
6ee159e2
ZK
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 */
36struct 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)
a6952287
ZK
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)
6ee159e2
ZK
50
51#define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB) \
52{ \
a6952287
ZK
53 ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
54 (PRF2PRI(PMAX) - PRI_TOLERANCE), \
6ee159e2
ZK
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 */
60static 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
70static 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/* for now, we support ETSI radar types, FCC and JP are TODO */
77static const struct radar_types *dfs_domains[] = {
78 &etsi_radar_types_v15,
79};
80
81/**
82 * get_dfs_domain_radar_types() - get radar types for a given DFS domain
83 * @param domain DFS domain
84 * @return radar_types ptr on success, NULL if DFS domain is not supported
85 */
86static const struct radar_types *
87get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
88{
89 u32 i;
90 for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
91 if (dfs_domains[i]->region == region)
92 return dfs_domains[i];
93 }
94 return NULL;
95}
96
97/**
98 * struct channel_detector - detector elements for a DFS channel
99 * @head: list_head
100 * @freq: frequency for this channel detector in MHz
101 * @detectors: array of dynamically created detector elements for this freq
102 *
103 * Channel detectors are required to provide multi-channel DFS detection, e.g.
104 * to support off-channel scanning. A pattern detector has a list of channels
105 * radar pulses have been reported for in the past.
106 */
107struct channel_detector {
108 struct list_head head;
109 u16 freq;
110 struct pri_detector **detectors;
111};
112
113/* channel_detector_reset() - reset detector lines for a given channel */
114static void channel_detector_reset(struct dfs_pattern_detector *dpd,
115 struct channel_detector *cd)
116{
117 u32 i;
118 if (cd == NULL)
119 return;
120 for (i = 0; i < dpd->num_radar_types; i++)
121 cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
122}
123
124/* channel_detector_exit() - destructor */
125static void channel_detector_exit(struct dfs_pattern_detector *dpd,
126 struct channel_detector *cd)
127{
128 u32 i;
129 if (cd == NULL)
130 return;
131 list_del(&cd->head);
132 for (i = 0; i < dpd->num_radar_types; i++) {
133 struct pri_detector *de = cd->detectors[i];
134 if (de != NULL)
135 de->exit(de);
136 }
137 kfree(cd->detectors);
138 kfree(cd);
139}
140
141static struct channel_detector *
142channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
143{
144 u32 sz, i;
145 struct channel_detector *cd;
146
5d8cd3b1 147 cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
6ee159e2
ZK
148 if (cd == NULL)
149 goto fail;
150
151 INIT_LIST_HEAD(&cd->head);
152 cd->freq = freq;
153 sz = sizeof(cd->detectors) * dpd->num_radar_types;
5d8cd3b1 154 cd->detectors = kzalloc(sz, GFP_ATOMIC);
6ee159e2
ZK
155 if (cd->detectors == NULL)
156 goto fail;
157
158 for (i = 0; i < dpd->num_radar_types; i++) {
159 const struct radar_detector_specs *rs = &dpd->radar_spec[i];
160 struct pri_detector *de = pri_detector_init(rs);
161 if (de == NULL)
162 goto fail;
163 cd->detectors[i] = de;
164 }
165 list_add(&cd->head, &dpd->channel_detectors);
166 return cd;
167
168fail:
95a5992e 169 ath_dbg(dpd->common, DFS,
ca21cfde 170 "failed to allocate channel_detector for freq=%d\n", freq);
6ee159e2
ZK
171 channel_detector_exit(dpd, cd);
172 return NULL;
173}
174
175/**
176 * channel_detector_get() - get channel detector for given frequency
177 * @param dpd instance pointer
178 * @param freq frequency in MHz
179 * @return pointer to channel detector on success, NULL otherwise
180 *
181 * Return existing channel detector for the given frequency or return a
182 * newly create one.
183 */
184static struct channel_detector *
185channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
186{
187 struct channel_detector *cd;
188 list_for_each_entry(cd, &dpd->channel_detectors, head) {
189 if (cd->freq == freq)
190 return cd;
191 }
192 return channel_detector_create(dpd, freq);
193}
194
195/*
196 * DFS Pattern Detector
197 */
198
199/* dpd_reset(): reset all channel detectors */
200static void dpd_reset(struct dfs_pattern_detector *dpd)
201{
202 struct channel_detector *cd;
203 if (!list_empty(&dpd->channel_detectors))
204 list_for_each_entry(cd, &dpd->channel_detectors, head)
205 channel_detector_reset(dpd, cd);
206
207}
208static void dpd_exit(struct dfs_pattern_detector *dpd)
209{
210 struct channel_detector *cd, *cd0;
211 if (!list_empty(&dpd->channel_detectors))
212 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
213 channel_detector_exit(dpd, cd);
214 kfree(dpd);
215}
216
217static bool
218dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
219{
220 u32 i;
6ee159e2
ZK
221 struct channel_detector *cd;
222
ca21cfde
ZK
223 /*
224 * pulses received for a non-supported or un-initialized
225 * domain are treated as detected radars for fail-safety
226 */
227 if (dpd->region == NL80211_DFS_UNSET)
6ee159e2 228 return true;
6ee159e2
ZK
229
230 cd = channel_detector_get(dpd, event->freq);
231 if (cd == NULL)
232 return false;
233
6ee159e2 234 dpd->last_pulse_ts = event->ts;
ca21cfde
ZK
235 /* reset detector on time stamp wraparound, caused by TSF reset */
236 if (event->ts < dpd->last_pulse_ts)
6ee159e2 237 dpd_reset(dpd);
ca21cfde 238
6ee159e2
ZK
239 /* do type individual pattern matching */
240 for (i = 0; i < dpd->num_radar_types; i++) {
ca21cfde
ZK
241 struct pri_detector *pd = cd->detectors[i];
242 struct pri_sequence *ps = pd->add_pulse(pd, event);
243 if (ps != NULL) {
95a5992e 244 ath_dbg(dpd->common, DFS,
ca21cfde
ZK
245 "DFS: radar found on freq=%d: id=%d, pri=%d, "
246 "count=%d, count_false=%d\n",
247 event->freq, pd->rs->type_id,
248 ps->pri, ps->count, ps->count_falses);
6ee159e2
ZK
249 channel_detector_reset(dpd, cd);
250 return true;
251 }
252 }
253 return false;
254}
255
d265214b
JD
256static struct ath_dfs_pool_stats
257dpd_get_stats(struct dfs_pattern_detector *dpd)
258{
259 return global_dfs_pool_stats;
260}
261
6ee159e2
ZK
262static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
263 enum nl80211_dfs_regions region)
264{
265 const struct radar_types *rt;
266 struct channel_detector *cd, *cd0;
267
268 if (dpd->region == region)
269 return true;
270
271 dpd->region = NL80211_DFS_UNSET;
272
273 rt = get_dfs_domain_radar_types(region);
274 if (rt == NULL)
275 return false;
276
277 /* delete all channel detectors for previous DFS domain */
278 if (!list_empty(&dpd->channel_detectors))
279 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
280 channel_detector_exit(dpd, cd);
281 dpd->radar_spec = rt->radar_types;
282 dpd->num_radar_types = rt->num_radar_types;
283
284 dpd->region = region;
285 return true;
286}
287
288static struct dfs_pattern_detector default_dpd = {
289 .exit = dpd_exit,
259bcf87 290 .set_dfs_domain = dpd_set_domain,
6ee159e2 291 .add_pulse = dpd_add_pulse,
d265214b 292 .get_stats = dpd_get_stats,
6ee159e2
ZK
293 .region = NL80211_DFS_UNSET,
294};
295
296struct dfs_pattern_detector *
95a5992e
JD
297dfs_pattern_detector_init(struct ath_common *common,
298 enum nl80211_dfs_regions region)
6ee159e2
ZK
299{
300 struct dfs_pattern_detector *dpd;
0d2e7a5c 301
6ee159e2 302 dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
0d2e7a5c 303 if (dpd == NULL)
6ee159e2 304 return NULL;
0d2e7a5c 305
6ee159e2
ZK
306 *dpd = default_dpd;
307 INIT_LIST_HEAD(&dpd->channel_detectors);
308
95a5992e 309 dpd->common = common;
259bcf87 310 if (dpd->set_dfs_domain(dpd, region))
6ee159e2
ZK
311 return dpd;
312
ca21cfde 313 ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
70bf870b 314 kfree(dpd);
6ee159e2
ZK
315 return NULL;
316}
317EXPORT_SYMBOL(dfs_pattern_detector_init);
This page took 0.156214 seconds and 5 git commands to generate.