blob: ecc81792f2dca53571006fc84ff51a8353358fe7 [file] [log] [blame]
Zefir Kurtisi29942bc2011-12-14 20:16:34 -08001/*
2 * Copyright (c) 2008-2011 Atheros Communications Inc.
3 * Copyright (c) 2011 Neratec Solutions AG
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080024/* internal struct to pass radar data */
25struct 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/* convert pulse duration to usecs, considering clock mode */
34static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
35{
36 const u32 AR93X_NSECS_PER_DUR = 800;
37 const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
38 u32 nsecs;
39
40 if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
41 nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
42 else
43 nsecs = dur * AR93X_NSECS_PER_DUR;
44
45 return (nsecs + 500) / 1000;
46}
47
48#define PRI_CH_RADAR_FOUND 0x01
49#define EXT_CH_RADAR_FOUND 0x02
50static bool
51ath9k_postprocess_radar_event(struct ath_softc *sc,
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020052 struct ath_radar_data *ard,
53 struct pulse_event *pe)
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080054{
55 u8 rssi;
56 u16 dur;
57
Luis R. Rodriguezec9a5702011-12-19 19:30:43 -080058 ath_dbg(ath9k_hw_common(sc->sc_ah), DFS,
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080059 "pulse_bw_info=0x%x, pri,ext len/rssi=(%u/%u, %u/%u)\n",
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020060 ard->pulse_bw_info,
61 ard->pulse_length_pri, ard->rssi,
62 ard->pulse_length_ext, ard->ext_rssi);
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080063
64 /*
65 * Only the last 2 bits of the BW info are relevant, they indicate
66 * which channel the radar was detected in.
67 */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020068 ard->pulse_bw_info &= 0x03;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080069
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020070 switch (ard->pulse_bw_info) {
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080071 case PRI_CH_RADAR_FOUND:
72 /* radar in ctrl channel */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020073 dur = ard->pulse_length_pri;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080074 DFS_STAT_INC(sc, pri_phy_errors);
75 /*
76 * cannot use ctrl channel RSSI
77 * if extension channel is stronger
78 */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020079 rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080080 break;
81 case EXT_CH_RADAR_FOUND:
82 /* radar in extension channel */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020083 dur = ard->pulse_length_ext;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080084 DFS_STAT_INC(sc, ext_phy_errors);
85 /*
86 * cannot use extension channel RSSI
87 * if control channel is stronger
88 */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020089 rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -080090 break;
91 case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
92 /*
93 * Conducted testing, when pulse is on DC, both pri and ext
94 * durations are reported to be same
95 *
96 * Radiated testing, when pulse is on DC, different pri and
97 * ext durations are reported, so take the larger of the two
98 */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +020099 if (ard->pulse_length_ext >= ard->pulse_length_pri)
100 dur = ard->pulse_length_ext;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800101 else
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200102 dur = ard->pulse_length_pri;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800103 DFS_STAT_INC(sc, dc_phy_errors);
104
105 /* when both are present use stronger one */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200106 rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800107 break;
108 default:
109 /*
110 * Bogus bandwidth info was received in descriptor,
111 * so ignore this PHY error
112 */
113 DFS_STAT_INC(sc, bwinfo_discards);
114 return false;
115 }
116
117 if (rssi == 0) {
118 DFS_STAT_INC(sc, rssi_discards);
119 return false;
120 }
121
122 /*
123 * TODO: check chirping pulses
124 * checks for chirping are dependent on the DFS regulatory domain
125 * used, which is yet TBD
126 */
127
128 /* convert duration to usecs */
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200129 pe->width = dur_to_usecs(sc->sc_ah, dur);
130 pe->rssi = rssi;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800131
132 DFS_STAT_INC(sc, pulses_detected);
133 return true;
134}
135#undef PRI_CH_RADAR_FOUND
136#undef EXT_CH_RADAR_FOUND
137
138/*
139 * DFS: check PHY-error for radar pulse and feed the detector
140 */
141void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
142 struct ath_rx_status *rs, u64 mactime)
143{
144 struct ath_radar_data ard;
145 u16 datalen;
146 char *vdata_end;
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200147 struct pulse_event pe;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800148 struct ath_hw *ah = sc->sc_ah;
149 struct ath_common *common = ath9k_hw_common(ah);
150
Zefir Kurtisib96f20b2012-04-20 17:20:34 +0200151 DFS_STAT_INC(sc, pulses_total);
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200152 if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
153 (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
Luis R. Rodriguezec9a5702011-12-19 19:30:43 -0800154 ath_dbg(common, DFS,
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800155 "Error: rs_phyer=0x%x not a radar error\n",
156 rs->rs_phyerr);
Zefir Kurtisib96f20b2012-04-20 17:20:34 +0200157 DFS_STAT_INC(sc, pulses_no_dfs);
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800158 return;
159 }
160
161 datalen = rs->rs_datalen;
162 if (datalen == 0) {
163 DFS_STAT_INC(sc, datalen_discards);
164 return;
165 }
166
167 ard.rssi = rs->rs_rssi_ctl0;
168 ard.ext_rssi = rs->rs_rssi_ext0;
169
170 /*
171 * hardware stores this as 8 bit signed value.
172 * we will cap it at 0 if it is a negative number
173 */
174 if (ard.rssi & 0x80)
175 ard.rssi = 0;
176 if (ard.ext_rssi & 0x80)
177 ard.ext_rssi = 0;
178
179 vdata_end = (char *)data + datalen;
180 ard.pulse_bw_info = vdata_end[-1];
181 ard.pulse_length_ext = vdata_end[-2];
182 ard.pulse_length_pri = vdata_end[-3];
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200183 pe.freq = ah->curchan->channel;
184 pe.ts = mactime;
185 if (ath9k_postprocess_radar_event(sc, &ard, &pe)) {
186 struct dfs_pattern_detector *pd = sc->dfs_detector;
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800187 static u64 last_ts;
Luis R. Rodriguezec9a5702011-12-19 19:30:43 -0800188 ath_dbg(common, DFS,
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800189 "ath9k_dfs_process_phyerr: channel=%d, ts=%llu, "
190 "width=%d, rssi=%d, delta_ts=%llu\n",
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200191 pe.freq, pe.ts, pe.width, pe.rssi, pe.ts-last_ts);
192 last_ts = pe.ts;
Zefir Kurtisib96f20b2012-04-20 17:20:34 +0200193 DFS_STAT_INC(sc, pulses_processed);
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200194 if (pd != NULL && pd->add_pulse(pd, &pe)) {
Zefir Kurtisib96f20b2012-04-20 17:20:34 +0200195 DFS_STAT_INC(sc, radar_detected);
Zefir Kurtisi56dc3892012-04-03 17:15:51 +0200196 /*
197 * TODO: forward radar event to DFS management layer
198 */
199 }
Zefir Kurtisi29942bc2011-12-14 20:16:34 -0800200 }
201}