blob: 35e93704c4ef53bdbb6b633d2285f2e9c31f9884 [file] [log] [blame]
Bruno Randolf2111ac02010-04-02 18:44:08 +09001/*
2 * Copyright (C) 2010 Bruno Randolf <br1@einfach.org>
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 "ath5k.h"
Bruno Randolf2111ac02010-04-02 18:44:08 +090018#include "reg.h"
19#include "debug.h"
20#include "ani.h"
21
22/**
23 * DOC: Basic ANI Operation
24 *
25 * Adaptive Noise Immunity (ANI) controls five noise immunity parameters
26 * depending on the amount of interference in the environment, increasing
27 * or reducing sensitivity as necessary.
28 *
29 * The parameters are:
Nick Kossifidisc47faa32011-11-25 20:40:25 +020030 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090031 * - "noise immunity"
Nick Kossifidisc47faa32011-11-25 20:40:25 +020032 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090033 * - "spur immunity"
Nick Kossifidisc47faa32011-11-25 20:40:25 +020034 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090035 * - "firstep level"
Nick Kossifidisc47faa32011-11-25 20:40:25 +020036 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090037 * - "OFDM weak signal detection"
Nick Kossifidisc47faa32011-11-25 20:40:25 +020038 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090039 * - "CCK weak signal detection"
40 *
41 * Basically we look at the amount of ODFM and CCK timing errors we get and then
42 * raise or lower immunity accordingly by setting one or more of these
43 * parameters.
Nick Kossifidisc47faa32011-11-25 20:40:25 +020044 *
Bruno Randolf2111ac02010-04-02 18:44:08 +090045 * Newer chipsets have PHY error counters in hardware which will generate a MIB
46 * interrupt when they overflow. Older hardware has too enable PHY error frames
47 * by setting a RX flag and then count every single PHY error. When a specified
48 * threshold of errors has been reached we will raise immunity.
49 * Also we regularly check the amount of errors and lower or raise immunity as
50 * necessary.
51 */
52
53
Nick Kossifidisc47faa32011-11-25 20:40:25 +020054/***********************\
55* ANI parameter control *
56\***********************/
Bruno Randolf2111ac02010-04-02 18:44:08 +090057
58/**
59 * ath5k_ani_set_noise_immunity_level() - Set noise immunity level
Nick Kossifidisc47faa32011-11-25 20:40:25 +020060 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +090061 * @level: level between 0 and @ATH5K_ANI_MAX_NOISE_IMM_LVL
62 */
63void
64ath5k_ani_set_noise_immunity_level(struct ath5k_hw *ah, int level)
65{
66 /* TODO:
67 * ANI documents suggest the following five levels to use, but the HAL
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020068 * and ath9k use only the last two levels, making this
Bruno Randolf2111ac02010-04-02 18:44:08 +090069 * essentially an on/off option. There *may* be a reason for this (???),
70 * so i stick with the HAL version for now...
71 */
72#if 0
Joe Perches8b225232010-11-20 18:38:52 -080073 static const s8 lo[] = { -52, -56, -60, -64, -70 };
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020074 static const s8 hi[] = { -18, -18, -16, -14, -12 };
Joe Perches8b225232010-11-20 18:38:52 -080075 static const s8 sz[] = { -34, -41, -48, -55, -62 };
76 static const s8 fr[] = { -70, -72, -75, -78, -80 };
Bruno Randolf2111ac02010-04-02 18:44:08 +090077#else
Joe Perches8b225232010-11-20 18:38:52 -080078 static const s8 lo[] = { -64, -70 };
79 static const s8 hi[] = { -14, -12 };
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020080 static const s8 sz[] = { -55, -62 };
Joe Perches8b225232010-11-20 18:38:52 -080081 static const s8 fr[] = { -78, -80 };
Bruno Randolf2111ac02010-04-02 18:44:08 +090082#endif
Dan Carpenter05e85942010-05-08 18:24:38 +020083 if (level < 0 || level >= ARRAY_SIZE(sz)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -040084 ATH5K_ERR(ah, "noise immunity level %d out of range",
Bruno Randolf4f424862010-05-19 10:31:42 +090085 level);
Bruno Randolf2111ac02010-04-02 18:44:08 +090086 return;
87 }
88
89 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
90 AR5K_PHY_DESIRED_SIZE_TOT, sz[level]);
91 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_AGCCOARSE,
92 AR5K_PHY_AGCCOARSE_LO, lo[level]);
93 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_AGCCOARSE,
94 AR5K_PHY_AGCCOARSE_HI, hi[level]);
95 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SIG,
96 AR5K_PHY_SIG_FIRPWR, fr[level]);
97
Pavel Roskine0d687b2011-07-14 20:21:55 -040098 ah->ani_state.noise_imm_level = level;
99 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "new level %d", level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900100}
101
Bruno Randolf2111ac02010-04-02 18:44:08 +0900102/**
103 * ath5k_ani_set_spur_immunity_level() - Set spur immunity level
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200104 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900105 * @level: level between 0 and @max_spur_level (the maximum level is dependent
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200106 * on the chip revision).
Bruno Randolf2111ac02010-04-02 18:44:08 +0900107 */
108void
109ath5k_ani_set_spur_immunity_level(struct ath5k_hw *ah, int level)
110{
Joe Perches8b225232010-11-20 18:38:52 -0800111 static const int val[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
Bruno Randolf2111ac02010-04-02 18:44:08 +0900112
Dan Carpenter05e85942010-05-08 18:24:38 +0200113 if (level < 0 || level >= ARRAY_SIZE(val) ||
Pavel Roskine0d687b2011-07-14 20:21:55 -0400114 level > ah->ani_state.max_spur_level) {
115 ATH5K_ERR(ah, "spur immunity level %d out of range",
Bruno Randolf4f424862010-05-19 10:31:42 +0900116 level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900117 return;
118 }
119
120 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
121 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1, val[level]);
122
Pavel Roskine0d687b2011-07-14 20:21:55 -0400123 ah->ani_state.spur_level = level;
124 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "new level %d", level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900125}
126
Bruno Randolf2111ac02010-04-02 18:44:08 +0900127/**
128 * ath5k_ani_set_firstep_level() - Set "firstep" level
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200129 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900130 * @level: level between 0 and @ATH5K_ANI_MAX_FIRSTEP_LVL
131 */
132void
133ath5k_ani_set_firstep_level(struct ath5k_hw *ah, int level)
134{
Joe Perches8b225232010-11-20 18:38:52 -0800135 static const int val[] = { 0, 4, 8 };
Bruno Randolf2111ac02010-04-02 18:44:08 +0900136
Dan Carpenter05e85942010-05-08 18:24:38 +0200137 if (level < 0 || level >= ARRAY_SIZE(val)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400138 ATH5K_ERR(ah, "firstep level %d out of range", level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900139 return;
140 }
141
142 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SIG,
143 AR5K_PHY_SIG_FIRSTEP, val[level]);
144
Pavel Roskine0d687b2011-07-14 20:21:55 -0400145 ah->ani_state.firstep_level = level;
146 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "new level %d", level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900147}
148
Bruno Randolf2111ac02010-04-02 18:44:08 +0900149/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200150 * ath5k_ani_set_ofdm_weak_signal_detection() - Set OFDM weak signal detection
151 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900152 * @on: turn on or off
153 */
154void
155ath5k_ani_set_ofdm_weak_signal_detection(struct ath5k_hw *ah, bool on)
156{
Joe Perches8b225232010-11-20 18:38:52 -0800157 static const int m1l[] = { 127, 50 };
158 static const int m2l[] = { 127, 40 };
159 static const int m1[] = { 127, 0x4d };
160 static const int m2[] = { 127, 0x40 };
161 static const int m2cnt[] = { 31, 16 };
162 static const int m2lcnt[] = { 63, 48 };
Bruno Randolf2111ac02010-04-02 18:44:08 +0900163
164 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
165 AR5K_PHY_WEAK_OFDM_LOW_THR_M1, m1l[on]);
166 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
167 AR5K_PHY_WEAK_OFDM_LOW_THR_M2, m2l[on]);
168 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
169 AR5K_PHY_WEAK_OFDM_HIGH_THR_M1, m1[on]);
170 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
171 AR5K_PHY_WEAK_OFDM_HIGH_THR_M2, m2[on]);
172 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
173 AR5K_PHY_WEAK_OFDM_HIGH_THR_M2_COUNT, m2cnt[on]);
174 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
175 AR5K_PHY_WEAK_OFDM_LOW_THR_M2_COUNT, m2lcnt[on]);
176
177 if (on)
178 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
179 AR5K_PHY_WEAK_OFDM_LOW_THR_SELFCOR_EN);
180 else
181 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
182 AR5K_PHY_WEAK_OFDM_LOW_THR_SELFCOR_EN);
183
Pavel Roskine0d687b2011-07-14 20:21:55 -0400184 ah->ani_state.ofdm_weak_sig = on;
185 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "turned %s",
Bruno Randolf2111ac02010-04-02 18:44:08 +0900186 on ? "on" : "off");
187}
188
Bruno Randolf2111ac02010-04-02 18:44:08 +0900189/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200190 * ath5k_ani_set_cck_weak_signal_detection() - Set CCK weak signal detection
191 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900192 * @on: turn on or off
193 */
194void
195ath5k_ani_set_cck_weak_signal_detection(struct ath5k_hw *ah, bool on)
196{
Joe Perches8b225232010-11-20 18:38:52 -0800197 static const int val[] = { 8, 6 };
Bruno Randolf2111ac02010-04-02 18:44:08 +0900198 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_CCK_CROSSCORR,
199 AR5K_PHY_CCK_CROSSCORR_WEAK_SIG_THR, val[on]);
Pavel Roskine0d687b2011-07-14 20:21:55 -0400200 ah->ani_state.cck_weak_sig = on;
201 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "turned %s",
Bruno Randolf2111ac02010-04-02 18:44:08 +0900202 on ? "on" : "off");
203}
204
205
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200206/***************\
207* ANI algorithm *
208\***************/
Bruno Randolf2111ac02010-04-02 18:44:08 +0900209
210/**
211 * ath5k_ani_raise_immunity() - Increase noise immunity
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200212 * @ah: The &struct ath5k_hw
213 * @as: The &struct ath5k_ani_state
Bruno Randolf2111ac02010-04-02 18:44:08 +0900214 * @ofdm_trigger: If this is true we are called because of too many OFDM errors,
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200215 * the algorithm will tune more parameters then.
Bruno Randolf2111ac02010-04-02 18:44:08 +0900216 *
217 * Try to raise noise immunity (=decrease sensitivity) in several steps
218 * depending on the average RSSI of the beacons we received.
219 */
220static void
221ath5k_ani_raise_immunity(struct ath5k_hw *ah, struct ath5k_ani_state *as,
222 bool ofdm_trigger)
223{
Bruno Randolfeef39be2010-11-16 10:58:43 +0900224 int rssi = ewma_read(&ah->ah_beacon_rssi_avg);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900225
Pavel Roskine0d687b2011-07-14 20:21:55 -0400226 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "raise immunity (%s)",
Bruno Randolf2111ac02010-04-02 18:44:08 +0900227 ofdm_trigger ? "ODFM" : "CCK");
228
229 /* first: raise noise immunity */
230 if (as->noise_imm_level < ATH5K_ANI_MAX_NOISE_IMM_LVL) {
231 ath5k_ani_set_noise_immunity_level(ah, as->noise_imm_level + 1);
232 return;
233 }
234
235 /* only OFDM: raise spur immunity level */
236 if (ofdm_trigger &&
Pavel Roskine0d687b2011-07-14 20:21:55 -0400237 as->spur_level < ah->ani_state.max_spur_level) {
Bruno Randolf2111ac02010-04-02 18:44:08 +0900238 ath5k_ani_set_spur_immunity_level(ah, as->spur_level + 1);
239 return;
240 }
241
242 /* AP mode */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400243 if (ah->opmode == NL80211_IFTYPE_AP) {
Bruno Randolf2111ac02010-04-02 18:44:08 +0900244 if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL)
245 ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
246 return;
247 }
248
249 /* STA and IBSS mode */
250
251 /* TODO: for IBSS mode it would be better to keep a beacon RSSI average
252 * per each neighbour node and use the minimum of these, to make sure we
253 * don't shut out a remote node by raising immunity too high. */
254
255 if (rssi > ATH5K_ANI_RSSI_THR_HIGH) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400256 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900257 "beacon RSSI high");
258 /* only OFDM: beacon RSSI is high, we can disable ODFM weak
259 * signal detection */
Joe Perches23677ce2012-02-09 11:17:23 +0000260 if (ofdm_trigger && as->ofdm_weak_sig) {
Bruno Randolf2111ac02010-04-02 18:44:08 +0900261 ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
262 ath5k_ani_set_spur_immunity_level(ah, 0);
263 return;
264 }
265 /* as a last resort or CCK: raise firstep level */
266 if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL) {
267 ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
268 return;
269 }
270 } else if (rssi > ATH5K_ANI_RSSI_THR_LOW) {
271 /* beacon RSSI in mid range, we need OFDM weak signal detect,
272 * but can raise firstep level */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400273 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900274 "beacon RSSI mid");
Joe Perches23677ce2012-02-09 11:17:23 +0000275 if (ofdm_trigger && !as->ofdm_weak_sig)
Bruno Randolf2111ac02010-04-02 18:44:08 +0900276 ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
277 if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL)
278 ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
279 return;
280 } else if (ah->ah_current_channel->band == IEEE80211_BAND_2GHZ) {
281 /* beacon RSSI is low. in B/G mode turn of OFDM weak signal
282 * detect and zero firstep level to maximize CCK sensitivity */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400283 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900284 "beacon RSSI low, 2GHz");
Joe Perches23677ce2012-02-09 11:17:23 +0000285 if (ofdm_trigger && as->ofdm_weak_sig)
Bruno Randolf2111ac02010-04-02 18:44:08 +0900286 ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
287 if (as->firstep_level > 0)
288 ath5k_ani_set_firstep_level(ah, 0);
289 return;
290 }
291
292 /* TODO: why not?:
293 if (as->cck_weak_sig == true) {
294 ath5k_ani_set_cck_weak_signal_detection(ah, false);
295 }
296 */
297}
298
Bruno Randolf2111ac02010-04-02 18:44:08 +0900299/**
300 * ath5k_ani_lower_immunity() - Decrease noise immunity
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200301 * @ah: The &struct ath5k_hw
302 * @as: The &struct ath5k_ani_state
Bruno Randolf2111ac02010-04-02 18:44:08 +0900303 *
304 * Try to lower noise immunity (=increase sensitivity) in several steps
305 * depending on the average RSSI of the beacons we received.
306 */
307static void
308ath5k_ani_lower_immunity(struct ath5k_hw *ah, struct ath5k_ani_state *as)
309{
Bruno Randolfeef39be2010-11-16 10:58:43 +0900310 int rssi = ewma_read(&ah->ah_beacon_rssi_avg);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900311
Pavel Roskine0d687b2011-07-14 20:21:55 -0400312 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "lower immunity");
Bruno Randolf2111ac02010-04-02 18:44:08 +0900313
Pavel Roskine0d687b2011-07-14 20:21:55 -0400314 if (ah->opmode == NL80211_IFTYPE_AP) {
Bruno Randolf2111ac02010-04-02 18:44:08 +0900315 /* AP mode */
316 if (as->firstep_level > 0) {
317 ath5k_ani_set_firstep_level(ah, as->firstep_level - 1);
318 return;
319 }
320 } else {
321 /* STA and IBSS mode (see TODO above) */
322 if (rssi > ATH5K_ANI_RSSI_THR_HIGH) {
323 /* beacon signal is high, leave OFDM weak signal
324 * detection off or it may oscillate
325 * TODO: who said it's off??? */
326 } else if (rssi > ATH5K_ANI_RSSI_THR_LOW) {
327 /* beacon RSSI is mid-range: turn on ODFM weak signal
328 * detection and next, lower firstep level */
Joe Perches23677ce2012-02-09 11:17:23 +0000329 if (!as->ofdm_weak_sig) {
Bruno Randolf2111ac02010-04-02 18:44:08 +0900330 ath5k_ani_set_ofdm_weak_signal_detection(ah,
331 true);
332 return;
333 }
334 if (as->firstep_level > 0) {
335 ath5k_ani_set_firstep_level(ah,
336 as->firstep_level - 1);
337 return;
338 }
339 } else {
340 /* beacon signal is low: only reduce firstep level */
341 if (as->firstep_level > 0) {
342 ath5k_ani_set_firstep_level(ah,
343 as->firstep_level - 1);
344 return;
345 }
346 }
347 }
348
349 /* all modes */
350 if (as->spur_level > 0) {
351 ath5k_ani_set_spur_immunity_level(ah, as->spur_level - 1);
352 return;
353 }
354
355 /* finally, reduce noise immunity */
356 if (as->noise_imm_level > 0) {
357 ath5k_ani_set_noise_immunity_level(ah, as->noise_imm_level - 1);
358 return;
359 }
360}
361
Bruno Randolf2111ac02010-04-02 18:44:08 +0900362/**
Felix Fietkau7109ca52010-10-08 22:13:54 +0200363 * ath5k_hw_ani_get_listen_time() - Update counters and return listening time
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200364 * @ah: The &struct ath5k_hw
365 * @as: The &struct ath5k_ani_state
Bruno Randolf2111ac02010-04-02 18:44:08 +0900366 *
367 * Return an approximation of the time spent "listening" in milliseconds (ms)
Felix Fietkau7109ca52010-10-08 22:13:54 +0200368 * since the last call of this function.
369 * Save a snapshot of the counter values for debugging/statistics.
Bruno Randolf2111ac02010-04-02 18:44:08 +0900370 */
371static int
372ath5k_hw_ani_get_listen_time(struct ath5k_hw *ah, struct ath5k_ani_state *as)
373{
Felix Fietkau7109ca52010-10-08 22:13:54 +0200374 struct ath_common *common = ath5k_hw_common(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900375 int listen;
376
Felix Fietkau7109ca52010-10-08 22:13:54 +0200377 spin_lock_bh(&common->cc_lock);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900378
Felix Fietkau7109ca52010-10-08 22:13:54 +0200379 ath_hw_cycle_counters_update(common);
380 memcpy(&as->last_cc, &common->cc_ani, sizeof(as->last_cc));
Bruno Randolf2111ac02010-04-02 18:44:08 +0900381
Felix Fietkau7109ca52010-10-08 22:13:54 +0200382 /* clears common->cc_ani */
383 listen = ath_hw_get_listen_time(common);
384
385 spin_unlock_bh(&common->cc_lock);
386
Bruno Randolf2111ac02010-04-02 18:44:08 +0900387 return listen;
388}
389
Bruno Randolf2111ac02010-04-02 18:44:08 +0900390/**
391 * ath5k_ani_save_and_clear_phy_errors() - Clear and save PHY error counters
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200392 * @ah: The &struct ath5k_hw
393 * @as: The &struct ath5k_ani_state
Bruno Randolf2111ac02010-04-02 18:44:08 +0900394 *
395 * Clear the PHY error counters as soon as possible, since this might be called
396 * from a MIB interrupt and we want to make sure we don't get interrupted again.
397 * Add the count of CCK and OFDM errors to our internal state, so it can be used
398 * by the algorithm later.
399 *
400 * Will be called from interrupt and tasklet context.
401 * Returns 0 if both counters are zero.
402 */
403static int
404ath5k_ani_save_and_clear_phy_errors(struct ath5k_hw *ah,
405 struct ath5k_ani_state *as)
406{
407 unsigned int ofdm_err, cck_err;
408
409 if (!ah->ah_capabilities.cap_has_phyerr_counters)
410 return 0;
411
412 ofdm_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1);
413 cck_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2);
414
415 /* reset counters first, we might be in a hurry (interrupt) */
416 ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
417 AR5K_PHYERR_CNT1);
418 ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
419 AR5K_PHYERR_CNT2);
420
421 ofdm_err = ATH5K_ANI_OFDM_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - ofdm_err);
422 cck_err = ATH5K_ANI_CCK_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - cck_err);
423
424 /* sometimes both can be zero, especially when there is a superfluous
425 * second interrupt. detect that here and return an error. */
426 if (ofdm_err <= 0 && cck_err <= 0)
427 return 0;
428
429 /* avoid negative values should one of the registers overflow */
430 if (ofdm_err > 0) {
431 as->ofdm_errors += ofdm_err;
432 as->sum_ofdm_errors += ofdm_err;
433 }
434 if (cck_err > 0) {
435 as->cck_errors += cck_err;
436 as->sum_cck_errors += cck_err;
437 }
438 return 1;
439}
440
Bruno Randolf2111ac02010-04-02 18:44:08 +0900441/**
442 * ath5k_ani_period_restart() - Restart ANI period
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200443 * @as: The &struct ath5k_ani_state
Bruno Randolf2111ac02010-04-02 18:44:08 +0900444 *
445 * Just reset counters, so they are clear for the next "ani period".
446 */
447static void
Nick Kossifidis29355a42011-11-25 20:40:29 +0200448ath5k_ani_period_restart(struct ath5k_ani_state *as)
Bruno Randolf2111ac02010-04-02 18:44:08 +0900449{
450 /* keep last values for debugging */
451 as->last_ofdm_errors = as->ofdm_errors;
452 as->last_cck_errors = as->cck_errors;
453 as->last_listen = as->listen_time;
454
455 as->ofdm_errors = 0;
456 as->cck_errors = 0;
457 as->listen_time = 0;
458}
459
Bruno Randolf2111ac02010-04-02 18:44:08 +0900460/**
461 * ath5k_ani_calibration() - The main ANI calibration function
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200462 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900463 *
464 * We count OFDM and CCK errors relative to the time where we did not send or
465 * receive ("listen" time) and raise or lower immunity accordingly.
466 * This is called regularly (every second) from the calibration timer, but also
467 * when an error threshold has been reached.
468 *
469 * In order to synchronize access from different contexts, this should be
470 * called only indirectly by scheduling the ANI tasklet!
471 */
472void
473ath5k_ani_calibration(struct ath5k_hw *ah)
474{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400475 struct ath5k_ani_state *as = &ah->ani_state;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900476 int listen, ofdm_high, ofdm_low, cck_high, cck_low;
477
Bruno Randolf2111ac02010-04-02 18:44:08 +0900478 /* get listen time since last call and add it to the counter because we
Bruno Randolf9537a162010-05-19 10:31:37 +0900479 * might not have restarted the "ani period" last time.
480 * always do this to calculate the busy time also in manual mode */
Bruno Randolf2111ac02010-04-02 18:44:08 +0900481 listen = ath5k_hw_ani_get_listen_time(ah, as);
482 as->listen_time += listen;
483
Bruno Randolf9537a162010-05-19 10:31:37 +0900484 if (as->ani_mode != ATH5K_ANI_MODE_AUTO)
485 return;
486
Bruno Randolf2111ac02010-04-02 18:44:08 +0900487 ath5k_ani_save_and_clear_phy_errors(ah, as);
488
489 ofdm_high = as->listen_time * ATH5K_ANI_OFDM_TRIG_HIGH / 1000;
490 cck_high = as->listen_time * ATH5K_ANI_CCK_TRIG_HIGH / 1000;
491 ofdm_low = as->listen_time * ATH5K_ANI_OFDM_TRIG_LOW / 1000;
492 cck_low = as->listen_time * ATH5K_ANI_CCK_TRIG_LOW / 1000;
493
Pavel Roskine0d687b2011-07-14 20:21:55 -0400494 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900495 "listen %d (now %d)", as->listen_time, listen);
Pavel Roskine0d687b2011-07-14 20:21:55 -0400496 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900497 "check high ofdm %d/%d cck %d/%d",
498 as->ofdm_errors, ofdm_high, as->cck_errors, cck_high);
499
500 if (as->ofdm_errors > ofdm_high || as->cck_errors > cck_high) {
501 /* too many PHY errors - we have to raise immunity */
502 bool ofdm_flag = as->ofdm_errors > ofdm_high ? true : false;
503 ath5k_ani_raise_immunity(ah, as, ofdm_flag);
Nick Kossifidis29355a42011-11-25 20:40:29 +0200504 ath5k_ani_period_restart(as);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900505
506 } else if (as->listen_time > 5 * ATH5K_ANI_LISTEN_PERIOD) {
507 /* If more than 5 (TODO: why 5?) periods have passed and we got
508 * relatively little errors we can try to lower immunity */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400509 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900510 "check low ofdm %d/%d cck %d/%d",
511 as->ofdm_errors, ofdm_low, as->cck_errors, cck_low);
512
513 if (as->ofdm_errors <= ofdm_low && as->cck_errors <= cck_low)
514 ath5k_ani_lower_immunity(ah, as);
515
Nick Kossifidis29355a42011-11-25 20:40:29 +0200516 ath5k_ani_period_restart(as);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900517 }
518}
519
520
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200521/*******************\
522* Interrupt handler *
523\*******************/
Bruno Randolf2111ac02010-04-02 18:44:08 +0900524
525/**
526 * ath5k_ani_mib_intr() - Interrupt handler for ANI MIB counters
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200527 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900528 *
529 * Just read & reset the registers quickly, so they don't generate more
530 * interrupts, save the counters and schedule the tasklet to decide whether
531 * to raise immunity or not.
532 *
533 * We just need to handle PHY error counters, ath5k_hw_update_mib_counters()
534 * should take care of all "normal" MIB interrupts.
535 */
536void
537ath5k_ani_mib_intr(struct ath5k_hw *ah)
538{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400539 struct ath5k_ani_state *as = &ah->ani_state;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900540
541 /* nothing to do here if HW does not have PHY error counters - they
542 * can't be the reason for the MIB interrupt then */
543 if (!ah->ah_capabilities.cap_has_phyerr_counters)
544 return;
545
546 /* not in use but clear anyways */
547 ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
548 ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
549
Pavel Roskine0d687b2011-07-14 20:21:55 -0400550 if (ah->ani_state.ani_mode != ATH5K_ANI_MODE_AUTO)
Bruno Randolf2111ac02010-04-02 18:44:08 +0900551 return;
552
Bob Copelanda180a132010-08-15 13:03:12 -0400553 /* If one of the errors triggered, we can get a superfluous second
554 * interrupt, even though we have already reset the register. The
555 * function detects that so we can return early. */
Bruno Randolf2111ac02010-04-02 18:44:08 +0900556 if (ath5k_ani_save_and_clear_phy_errors(ah, as) == 0)
557 return;
558
559 if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH ||
560 as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400561 tasklet_schedule(&ah->ani_tasklet);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900562}
563
Bruno Randolf2111ac02010-04-02 18:44:08 +0900564/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200565 * ath5k_ani_phy_error_report - Used by older HW to report PHY errors
566 *
567 * @ah: The &struct ath5k_hw
568 * @phyerr: One of enum ath5k_phy_error_code
Bruno Randolf2111ac02010-04-02 18:44:08 +0900569 *
570 * This is used by hardware without PHY error counters to report PHY errors
571 * on a frame-by-frame basis, instead of the interrupt.
572 */
573void
574ath5k_ani_phy_error_report(struct ath5k_hw *ah,
575 enum ath5k_phy_error_code phyerr)
576{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400577 struct ath5k_ani_state *as = &ah->ani_state;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900578
579 if (phyerr == AR5K_RX_PHY_ERROR_OFDM_TIMING) {
580 as->ofdm_errors++;
581 if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400582 tasklet_schedule(&ah->ani_tasklet);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900583 } else if (phyerr == AR5K_RX_PHY_ERROR_CCK_TIMING) {
584 as->cck_errors++;
585 if (as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400586 tasklet_schedule(&ah->ani_tasklet);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900587 }
588}
589
590
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200591/****************\
592* Initialization *
593\****************/
Bruno Randolf2111ac02010-04-02 18:44:08 +0900594
595/**
596 * ath5k_enable_phy_err_counters() - Enable PHY error counters
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200597 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900598 *
599 * Enable PHY error counters for OFDM and CCK timing errors.
600 */
601static void
602ath5k_enable_phy_err_counters(struct ath5k_hw *ah)
603{
604 ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
605 AR5K_PHYERR_CNT1);
606 ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
607 AR5K_PHYERR_CNT2);
608 ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_OFDM, AR5K_PHYERR_CNT1_MASK);
609 ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_CCK, AR5K_PHYERR_CNT2_MASK);
610
611 /* not in use */
612 ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
613 ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
614}
615
Bruno Randolf2111ac02010-04-02 18:44:08 +0900616/**
617 * ath5k_disable_phy_err_counters() - Disable PHY error counters
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200618 * @ah: The &struct ath5k_hw
Bruno Randolf2111ac02010-04-02 18:44:08 +0900619 *
620 * Disable PHY error counters for OFDM and CCK timing errors.
621 */
622static void
623ath5k_disable_phy_err_counters(struct ath5k_hw *ah)
624{
625 ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1);
626 ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2);
627 ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1_MASK);
628 ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2_MASK);
629
630 /* not in use */
631 ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
632 ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
633}
634
Bruno Randolf2111ac02010-04-02 18:44:08 +0900635/**
636 * ath5k_ani_init() - Initialize ANI
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200637 * @ah: The &struct ath5k_hw
638 * @mode: One of enum ath5k_ani_mode
Bruno Randolf2111ac02010-04-02 18:44:08 +0900639 *
640 * Initialize ANI according to mode.
641 */
642void
643ath5k_ani_init(struct ath5k_hw *ah, enum ath5k_ani_mode mode)
644{
645 /* ANI is only possible on 5212 and newer */
646 if (ah->ah_version < AR5K_AR5212)
647 return;
648
Pavel Roskin86fbe172011-07-09 00:17:45 -0400649 if (mode < ATH5K_ANI_MODE_OFF || mode > ATH5K_ANI_MODE_AUTO) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400650 ATH5K_ERR(ah, "ANI mode %d out of range", mode);
Pavel Roskin86fbe172011-07-09 00:17:45 -0400651 return;
652 }
653
Bruno Randolf2111ac02010-04-02 18:44:08 +0900654 /* clear old state information */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400655 memset(&ah->ani_state, 0, sizeof(ah->ani_state));
Bruno Randolf2111ac02010-04-02 18:44:08 +0900656
657 /* older hardware has more spur levels than newer */
658 if (ah->ah_mac_srev < AR5K_SREV_AR2414)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400659 ah->ani_state.max_spur_level = 7;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900660 else
Pavel Roskine0d687b2011-07-14 20:21:55 -0400661 ah->ani_state.max_spur_level = 2;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900662
663 /* initial values for our ani parameters */
664 if (mode == ATH5K_ANI_MODE_OFF) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400665 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "ANI off\n");
Pavel Roskine4bbf2f2011-07-07 18:14:13 -0400666 } else if (mode == ATH5K_ANI_MODE_MANUAL_LOW) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400667 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900668 "ANI manual low -> high sensitivity\n");
669 ath5k_ani_set_noise_immunity_level(ah, 0);
670 ath5k_ani_set_spur_immunity_level(ah, 0);
671 ath5k_ani_set_firstep_level(ah, 0);
672 ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
673 ath5k_ani_set_cck_weak_signal_detection(ah, true);
674 } else if (mode == ATH5K_ANI_MODE_MANUAL_HIGH) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400675 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
Bruno Randolf2111ac02010-04-02 18:44:08 +0900676 "ANI manual high -> low sensitivity\n");
677 ath5k_ani_set_noise_immunity_level(ah,
678 ATH5K_ANI_MAX_NOISE_IMM_LVL);
679 ath5k_ani_set_spur_immunity_level(ah,
Pavel Roskine0d687b2011-07-14 20:21:55 -0400680 ah->ani_state.max_spur_level);
Bruno Randolf2111ac02010-04-02 18:44:08 +0900681 ath5k_ani_set_firstep_level(ah, ATH5K_ANI_MAX_FIRSTEP_LVL);
682 ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
683 ath5k_ani_set_cck_weak_signal_detection(ah, false);
684 } else if (mode == ATH5K_ANI_MODE_AUTO) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400685 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "ANI auto\n");
Bruno Randolf2111ac02010-04-02 18:44:08 +0900686 ath5k_ani_set_noise_immunity_level(ah, 0);
687 ath5k_ani_set_spur_immunity_level(ah, 0);
688 ath5k_ani_set_firstep_level(ah, 0);
689 ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
690 ath5k_ani_set_cck_weak_signal_detection(ah, false);
691 }
692
693 /* newer hardware has PHY error counter registers which we can use to
694 * get OFDM and CCK error counts. older hardware has to set rxfilter and
695 * report every single PHY error by calling ath5k_ani_phy_error_report()
696 */
697 if (mode == ATH5K_ANI_MODE_AUTO) {
698 if (ah->ah_capabilities.cap_has_phyerr_counters)
699 ath5k_enable_phy_err_counters(ah);
700 else
701 ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) |
702 AR5K_RX_FILTER_PHYERR);
703 } else {
704 if (ah->ah_capabilities.cap_has_phyerr_counters)
705 ath5k_disable_phy_err_counters(ah);
706 else
707 ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) &
708 ~AR5K_RX_FILTER_PHYERR);
709 }
710
Pavel Roskine0d687b2011-07-14 20:21:55 -0400711 ah->ani_state.ani_mode = mode;
Bruno Randolf2111ac02010-04-02 18:44:08 +0900712}
713
714
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200715/**************\
716* Debug output *
717\**************/
Bruno Randolf2111ac02010-04-02 18:44:08 +0900718
719#ifdef CONFIG_ATH5K_DEBUG
720
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200721/**
722 * ath5k_ani_print_counters() - Print ANI counters
723 * @ah: The &struct ath5k_hw
724 *
725 * Used for debugging ANI
726 */
Bruno Randolf2111ac02010-04-02 18:44:08 +0900727void
728ath5k_ani_print_counters(struct ath5k_hw *ah)
729{
730 /* clears too */
731 printk(KERN_NOTICE "ACK fail\t%d\n",
732 ath5k_hw_reg_read(ah, AR5K_ACK_FAIL));
733 printk(KERN_NOTICE "RTS fail\t%d\n",
734 ath5k_hw_reg_read(ah, AR5K_RTS_FAIL));
735 printk(KERN_NOTICE "RTS success\t%d\n",
736 ath5k_hw_reg_read(ah, AR5K_RTS_OK));
737 printk(KERN_NOTICE "FCS error\t%d\n",
738 ath5k_hw_reg_read(ah, AR5K_FCS_FAIL));
739
740 /* no clear */
741 printk(KERN_NOTICE "tx\t%d\n",
742 ath5k_hw_reg_read(ah, AR5K_PROFCNT_TX));
743 printk(KERN_NOTICE "rx\t%d\n",
744 ath5k_hw_reg_read(ah, AR5K_PROFCNT_RX));
745 printk(KERN_NOTICE "busy\t%d\n",
746 ath5k_hw_reg_read(ah, AR5K_PROFCNT_RXCLR));
747 printk(KERN_NOTICE "cycles\t%d\n",
748 ath5k_hw_reg_read(ah, AR5K_PROFCNT_CYCLE));
749
750 printk(KERN_NOTICE "AR5K_PHYERR_CNT1\t%d\n",
751 ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1));
752 printk(KERN_NOTICE "AR5K_PHYERR_CNT2\t%d\n",
753 ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2));
754 printk(KERN_NOTICE "AR5K_OFDM_FIL_CNT\t%d\n",
755 ath5k_hw_reg_read(ah, AR5K_OFDM_FIL_CNT));
756 printk(KERN_NOTICE "AR5K_CCK_FIL_CNT\t%d\n",
757 ath5k_hw_reg_read(ah, AR5K_CCK_FIL_CNT));
758}
759
760#endif