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
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
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,
52 struct ath_radar_data *ard,
53 struct pulse_event *pe)
54{
55 u8 rssi;
56 u16 dur;
57
58
59
60
61
62 ard->pulse_bw_info &= 0x03;
63
64 switch (ard->pulse_bw_info) {
65 case PRI_CH_RADAR_FOUND:
66
67 dur = ard->pulse_length_pri;
68 DFS_STAT_INC(sc, pri_phy_errors);
69
70
71
72
73 rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
74 break;
75 case EXT_CH_RADAR_FOUND:
76
77 dur = ard->pulse_length_ext;
78 DFS_STAT_INC(sc, ext_phy_errors);
79
80
81
82
83 rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
84 break;
85 case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
86
87
88
89
90
91
92
93 if (ard->pulse_length_ext >= ard->pulse_length_pri)
94 dur = ard->pulse_length_ext;
95 else
96 dur = ard->pulse_length_pri;
97 DFS_STAT_INC(sc, dc_phy_errors);
98
99
100 rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
101 break;
102 default:
103
104
105
106
107 DFS_STAT_INC(sc, bwinfo_discards);
108 return false;
109 }
110
111 if (rssi == 0) {
112 DFS_STAT_INC(sc, rssi_discards);
113 return false;
114 }
115
116
117
118
119
120
121
122
123 pe->width = dur_to_usecs(sc->sc_ah, dur);
124 pe->rssi = rssi;
125
126 DFS_STAT_INC(sc, pulses_detected);
127 return true;
128}
129#undef PRI_CH_RADAR_FOUND
130#undef EXT_CH_RADAR_FOUND
131
132
133
134
135void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
136 struct ath_rx_status *rs, u64 mactime)
137{
138 struct ath_radar_data ard;
139 u16 datalen;
140 char *vdata_end;
141 struct pulse_event pe;
142 struct ath_hw *ah = sc->sc_ah;
143 struct ath_common *common = ath9k_hw_common(ah);
144
145 DFS_STAT_INC(sc, pulses_total);
146 if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
147 (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
148 ath_dbg(common, DFS,
149 "Error: rs_phyer=0x%x not a radar error\n",
150 rs->rs_phyerr);
151 DFS_STAT_INC(sc, pulses_no_dfs);
152 return;
153 }
154
155 datalen = rs->rs_datalen;
156 if (datalen == 0) {
157 DFS_STAT_INC(sc, datalen_discards);
158 return;
159 }
160
161 ard.rssi = rs->rs_rssi_ctl0;
162 ard.ext_rssi = rs->rs_rssi_ext0;
163
164
165
166
167
168 if (ard.rssi & 0x80)
169 ard.rssi = 0;
170 if (ard.ext_rssi & 0x80)
171 ard.ext_rssi = 0;
172
173 vdata_end = (char *)data + datalen;
174 ard.pulse_bw_info = vdata_end[-1];
175 ard.pulse_length_ext = vdata_end[-2];
176 ard.pulse_length_pri = vdata_end[-3];
177 pe.freq = ah->curchan->channel;
178 pe.ts = mactime;
179 if (ath9k_postprocess_radar_event(sc, &ard, &pe)) {
180 struct dfs_pattern_detector *pd = sc->dfs_detector;
181 static u64 last_ts;
182 ath_dbg(common, DFS,
183 "ath9k_dfs_process_phyerr: channel=%d, ts=%llu, "
184 "width=%d, rssi=%d, delta_ts=%llu\n",
185 pe.freq, pe.ts, pe.width, pe.rssi, pe.ts-last_ts);
186 last_ts = pe.ts;
187 DFS_STAT_INC(sc, pulses_processed);
188 if (pd != NULL && pd->add_pulse(pd, &pe)) {
189 DFS_STAT_INC(sc, radar_detected);
190 ieee80211_radar_detected(sc->hw);
191 }
192 }
193}
194