This source file includes following definitions.
- fft_max_index
- fft_max_magnitude
- fft_bitmap_weight
- ath9k_get_max_index_ht40
- ath9k_check_chirping
- dur_to_usecs
- ath9k_postprocess_radar_event
- ath9k_dfs_process_radar_pulse
- ath9k_dfs_process_phyerr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include "hw.h"
19 #include "hw-ops.h"
20 #include "ath9k.h"
21 #include "dfs.h"
22 #include "dfs_debug.h"
23
24
25 struct ath_radar_data {
26 u8 pulse_bw_info;
27 u8 rssi;
28 u8 ext_rssi;
29 u8 pulse_length_ext;
30 u8 pulse_length_pri;
31 };
32
33
34
35
36
37
38
39 static const int BIN_DELTA_MIN = 1;
40 static const int BIN_DELTA_MAX = 10;
41
42
43 #define NUM_DIFFS 3
44 #define FFT_NUM_SAMPLES (NUM_DIFFS + 1)
45
46
47 static const int MAX_DIFF = 2;
48
49
50 static const int MIN_CHIRP_PULSE_WIDTH = 20;
51 static const int MAX_CHIRP_PULSE_WIDTH = 110;
52
53 struct ath9k_dfs_fft_20 {
54 u8 bin[28];
55 u8 lower_bins[3];
56 } __packed;
57 struct ath9k_dfs_fft_40 {
58 u8 bin[64];
59 u8 lower_bins[3];
60 u8 upper_bins[3];
61 } __packed;
62
63 static inline int fft_max_index(u8 *bins)
64 {
65 return (bins[2] & 0xfc) >> 2;
66 }
67 static inline int fft_max_magnitude(u8 *bins)
68 {
69 return (bins[0] & 0xc0) >> 6 | bins[1] << 2 | (bins[2] & 0x03) << 10;
70 }
71 static inline u8 fft_bitmap_weight(u8 *bins)
72 {
73 return bins[0] & 0x3f;
74 }
75
76 static int ath9k_get_max_index_ht40(struct ath9k_dfs_fft_40 *fft,
77 bool is_ctl, bool is_ext)
78 {
79 const int DFS_UPPER_BIN_OFFSET = 64;
80
81 if (is_ctl && is_ext) {
82
83 is_ctl = fft_bitmap_weight(fft->lower_bins) != 0;
84 is_ext = fft_bitmap_weight(fft->upper_bins) != 0;
85
86
87 if (is_ctl && is_ext) {
88 int mag_lower = fft_max_magnitude(fft->lower_bins);
89 int mag_upper = fft_max_magnitude(fft->upper_bins);
90 if (mag_upper > mag_lower)
91 is_ctl = false;
92 else
93 is_ext = false;
94 }
95 }
96 if (is_ctl)
97 return fft_max_index(fft->lower_bins);
98 return fft_max_index(fft->upper_bins) + DFS_UPPER_BIN_OFFSET;
99 }
100 static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
101 int datalen, bool is_ctl, bool is_ext)
102 {
103 int i;
104 int max_bin[FFT_NUM_SAMPLES];
105 struct ath_hw *ah = sc->sc_ah;
106 struct ath_common *common = ath9k_hw_common(ah);
107 int prev_delta;
108
109 if (IS_CHAN_HT40(ah->curchan)) {
110 struct ath9k_dfs_fft_40 *fft = (struct ath9k_dfs_fft_40 *) data;
111 int num_fft_packets = datalen / sizeof(*fft);
112 if (num_fft_packets == 0)
113 return false;
114
115 ath_dbg(common, DFS, "HT40: datalen=%d, num_fft_packets=%d\n",
116 datalen, num_fft_packets);
117 if (num_fft_packets < FFT_NUM_SAMPLES) {
118 ath_dbg(common, DFS, "not enough packets for chirp\n");
119 return false;
120 }
121
122 if ((datalen % sizeof(*fft)) == 2) {
123 fft = (struct ath9k_dfs_fft_40 *) (data + 2);
124 ath_dbg(common, DFS, "fixing datalen by 2\n");
125 }
126 if (IS_CHAN_HT40MINUS(ah->curchan))
127 swap(is_ctl, is_ext);
128
129 for (i = 0; i < FFT_NUM_SAMPLES; i++)
130 max_bin[i] = ath9k_get_max_index_ht40(fft + i, is_ctl,
131 is_ext);
132 } else {
133 struct ath9k_dfs_fft_20 *fft = (struct ath9k_dfs_fft_20 *) data;
134 int num_fft_packets = datalen / sizeof(*fft);
135 if (num_fft_packets == 0)
136 return false;
137 ath_dbg(common, DFS, "HT20: datalen=%d, num_fft_packets=%d\n",
138 datalen, num_fft_packets);
139 if (num_fft_packets < FFT_NUM_SAMPLES) {
140 ath_dbg(common, DFS, "not enough packets for chirp\n");
141 return false;
142 }
143
144 for (i = 0; i < FFT_NUM_SAMPLES; i++)
145 max_bin[i] = fft_max_index(fft[i].lower_bins) ^ 0x20;
146 }
147 ath_dbg(common, DFS, "bin_max = [%d, %d, %d, %d]\n",
148 max_bin[0], max_bin[1], max_bin[2], max_bin[3]);
149
150
151
152
153
154 prev_delta = 0;
155 for (i = 0; i < NUM_DIFFS; i++) {
156 int ddelta = -1;
157 int delta = max_bin[i + 1] - max_bin[i];
158
159
160 if (abs(delta) < BIN_DELTA_MIN || abs(delta) > BIN_DELTA_MAX) {
161 ath_dbg(common, DFS, "CHIRP: invalid delta %d "
162 "in sample %d\n", delta, i);
163 return false;
164 }
165 if (i == 0)
166 goto done;
167 ddelta = delta - prev_delta;
168 if (abs(ddelta) > MAX_DIFF) {
169 ath_dbg(common, DFS, "CHIRP: ddelta %d too high\n",
170 ddelta);
171 return false;
172 }
173 done:
174 ath_dbg(common, DFS, "CHIRP - %d: delta=%d, ddelta=%d\n",
175 i, delta, ddelta);
176 prev_delta = delta;
177 }
178 return true;
179 }
180
181
182
183 static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
184 {
185 const u32 AR93X_NSECS_PER_DUR = 800;
186 const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
187 u32 nsecs;
188
189 if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
190 nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
191 else
192 nsecs = dur * AR93X_NSECS_PER_DUR;
193
194 return (nsecs + 500) / 1000;
195 }
196
197 #define PRI_CH_RADAR_FOUND 0x01
198 #define EXT_CH_RADAR_FOUND 0x02
199 static bool
200 ath9k_postprocess_radar_event(struct ath_softc *sc,
201 struct ath_radar_data *ard,
202 struct pulse_event *pe)
203 {
204 u8 rssi;
205 u16 dur;
206
207
208
209
210
211 ard->pulse_bw_info &= 0x03;
212
213 switch (ard->pulse_bw_info) {
214 case PRI_CH_RADAR_FOUND:
215
216 dur = ard->pulse_length_pri;
217 DFS_STAT_INC(sc, pri_phy_errors);
218
219
220
221
222 rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
223 break;
224 case EXT_CH_RADAR_FOUND:
225
226 dur = ard->pulse_length_ext;
227 DFS_STAT_INC(sc, ext_phy_errors);
228
229
230
231
232 rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
233 break;
234 case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
235
236
237
238
239
240
241
242 if (ard->pulse_length_ext >= ard->pulse_length_pri)
243 dur = ard->pulse_length_ext;
244 else
245 dur = ard->pulse_length_pri;
246 DFS_STAT_INC(sc, dc_phy_errors);
247
248
249 rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
250 break;
251 default:
252
253
254
255
256 DFS_STAT_INC(sc, bwinfo_discards);
257 return false;
258 }
259
260 if (rssi == 0) {
261 DFS_STAT_INC(sc, rssi_discards);
262 return false;
263 }
264
265
266 pe->width = dur_to_usecs(sc->sc_ah, dur);
267 pe->rssi = rssi;
268
269 DFS_STAT_INC(sc, pulses_detected);
270 return true;
271 }
272
273 static void
274 ath9k_dfs_process_radar_pulse(struct ath_softc *sc, struct pulse_event *pe)
275 {
276 struct dfs_pattern_detector *pd = sc->dfs_detector;
277 DFS_STAT_INC(sc, pulses_processed);
278 if (pd == NULL)
279 return;
280 if (!pd->add_pulse(pd, pe, NULL))
281 return;
282 DFS_STAT_INC(sc, radar_detected);
283 ieee80211_radar_detected(sc->hw);
284 }
285
286
287
288
289 void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
290 struct ath_rx_status *rs, u64 mactime)
291 {
292 struct ath_radar_data ard;
293 u16 datalen;
294 char *vdata_end;
295 struct pulse_event pe;
296 struct ath_hw *ah = sc->sc_ah;
297 struct ath_common *common = ath9k_hw_common(ah);
298
299 DFS_STAT_INC(sc, pulses_total);
300 if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
301 (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
302 ath_dbg(common, DFS,
303 "Error: rs_phyer=0x%x not a radar error\n",
304 rs->rs_phyerr);
305 DFS_STAT_INC(sc, pulses_no_dfs);
306 return;
307 }
308
309 datalen = rs->rs_datalen;
310 if (datalen == 0) {
311 DFS_STAT_INC(sc, datalen_discards);
312 return;
313 }
314
315 ard.rssi = rs->rs_rssi_ctl[0];
316 ard.ext_rssi = rs->rs_rssi_ext[0];
317
318
319
320
321
322 if (ard.rssi & 0x80)
323 ard.rssi = 0;
324 if (ard.ext_rssi & 0x80)
325 ard.ext_rssi = 0;
326
327 vdata_end = data + datalen;
328 ard.pulse_bw_info = vdata_end[-1];
329 ard.pulse_length_ext = vdata_end[-2];
330 ard.pulse_length_pri = vdata_end[-3];
331 pe.freq = ah->curchan->channel;
332 pe.ts = mactime;
333 if (!ath9k_postprocess_radar_event(sc, &ard, &pe))
334 return;
335
336 if (pe.width > MIN_CHIRP_PULSE_WIDTH &&
337 pe.width < MAX_CHIRP_PULSE_WIDTH) {
338 bool is_ctl = !!(ard.pulse_bw_info & PRI_CH_RADAR_FOUND);
339 bool is_ext = !!(ard.pulse_bw_info & EXT_CH_RADAR_FOUND);
340 int clen = datalen - 3;
341 pe.chirp = ath9k_check_chirping(sc, data, clen, is_ctl, is_ext);
342 } else {
343 pe.chirp = false;
344 }
345
346 ath_dbg(common, DFS,
347 "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
348 "width=%d, rssi=%d, delta_ts=%llu\n",
349 ard.pulse_bw_info, pe.freq, pe.ts, pe.width, pe.rssi,
350 pe.ts - sc->dfs_prev_pulse_ts);
351 sc->dfs_prev_pulse_ts = pe.ts;
352 if (ard.pulse_bw_info & PRI_CH_RADAR_FOUND)
353 ath9k_dfs_process_radar_pulse(sc, &pe);
354 if (IS_CHAN_HT40(ah->curchan) &&
355 ard.pulse_bw_info & EXT_CH_RADAR_FOUND) {
356 pe.freq += IS_CHAN_HT40PLUS(ah->curchan) ? 20 : -20;
357 ath9k_dfs_process_radar_pulse(sc, &pe);
358 }
359 }
360 #undef PRI_CH_RADAR_FOUND
361 #undef EXT_CH_RADAR_FOUND