blob: 6bae601c63a39a5c9fda540a096f888f6cd26bc3 [file] [log] [blame]
Sujithf1dc5602008-10-29 10:16:30 +05301/*
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04002 * Copyright (c) 2008-2010 Atheros Communications Inc.
Sujithf1dc5602008-10-29 10:16:30 +05303 *
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
Nikitas Angelinasbbce80e2010-09-08 22:25:42 +010017#include <linux/kernel.h>
Luis R. Rodriguezcfe8cba2009-09-13 23:39:31 -070018#include "hw.h"
Felix Fietkauc16fcb42010-04-15 17:38:39 -040019#include "hw-ops.h"
Sujithf1dc5602008-10-29 10:16:30 +053020
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040021struct ani_ofdm_level_entry {
22 int spur_immunity_level;
23 int fir_step_level;
24 int ofdm_weak_signal_on;
25};
26
27/* values here are relative to the INI */
28
29/*
30 * Legend:
31 *
32 * SI: Spur immunity
33 * FS: FIR Step
34 * WS: OFDM / CCK Weak Signal detection
35 * MRC-CCK: Maximal Ratio Combining for CCK
36 */
37
38static const struct ani_ofdm_level_entry ofdm_level_table[] = {
39 /* SI FS WS */
40 { 0, 0, 1 }, /* lvl 0 */
41 { 1, 1, 1 }, /* lvl 1 */
42 { 2, 2, 1 }, /* lvl 2 */
43 { 3, 2, 1 }, /* lvl 3 (default) */
44 { 4, 3, 1 }, /* lvl 4 */
45 { 5, 4, 1 }, /* lvl 5 */
46 { 6, 5, 1 }, /* lvl 6 */
47 { 7, 6, 1 }, /* lvl 7 */
48 { 7, 7, 1 }, /* lvl 8 */
49 { 7, 8, 0 } /* lvl 9 */
50};
51#define ATH9K_ANI_OFDM_NUM_LEVEL \
Nikitas Angelinasbbce80e2010-09-08 22:25:42 +010052 ARRAY_SIZE(ofdm_level_table)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040053#define ATH9K_ANI_OFDM_MAX_LEVEL \
54 (ATH9K_ANI_OFDM_NUM_LEVEL-1)
55#define ATH9K_ANI_OFDM_DEF_LEVEL \
56 3 /* default level - matches the INI settings */
57
58/*
59 * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
60 * With OFDM for single stream you just add up all antenna inputs, you're
61 * only interested in what you get after FFT. Signal aligment is also not
62 * required for OFDM because any phase difference adds up in the frequency
63 * domain.
64 *
65 * MRC requires extra work for use with CCK. You need to align the antenna
66 * signals from the different antenna before you can add the signals together.
67 * You need aligment of signals as CCK is in time domain, so addition can cancel
68 * your signal completely if phase is 180 degrees (think of adding sine waves).
69 * You also need to remove noise before the addition and this is where ANI
70 * MRC CCK comes into play. One of the antenna inputs may be stronger but
71 * lower SNR, so just adding after alignment can be dangerous.
72 *
73 * Regardless of alignment in time, the antenna signals add constructively after
74 * FFT and improve your reception. For more information:
75 *
76 * http://en.wikipedia.org/wiki/Maximal-ratio_combining
77 */
78
79struct ani_cck_level_entry {
80 int fir_step_level;
81 int mrc_cck_on;
82};
83
84static const struct ani_cck_level_entry cck_level_table[] = {
85 /* FS MRC-CCK */
86 { 0, 1 }, /* lvl 0 */
87 { 1, 1 }, /* lvl 1 */
88 { 2, 1 }, /* lvl 2 (default) */
89 { 3, 1 }, /* lvl 3 */
90 { 4, 0 }, /* lvl 4 */
91 { 5, 0 }, /* lvl 5 */
92 { 6, 0 }, /* lvl 6 */
93 { 7, 0 }, /* lvl 7 (only for high rssi) */
94 { 8, 0 } /* lvl 8 (only for high rssi) */
95};
96
97#define ATH9K_ANI_CCK_NUM_LEVEL \
Nikitas Angelinasbbce80e2010-09-08 22:25:42 +010098 ARRAY_SIZE(cck_level_table)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040099#define ATH9K_ANI_CCK_MAX_LEVEL \
100 (ATH9K_ANI_CCK_NUM_LEVEL-1)
101#define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
102 (ATH9K_ANI_CCK_NUM_LEVEL-3)
103#define ATH9K_ANI_CCK_DEF_LEVEL \
104 2 /* default level - matches the INI settings */
105
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -0400106/* Private to ani.c */
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400107static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -0400108{
109 ath9k_hw_private_ops(ah)->ani_lower_immunity(ah);
110}
111
Felix Fietkau71ea4202010-10-04 20:09:46 +0200112static bool use_new_ani(struct ath_hw *ah)
113{
114 return AR_SREV_9300_20_OR_LATER(ah) || modparam_force_new_ani;
115}
116
Sujithcbe61d82009-02-09 13:27:12 +0530117static void ath9k_hw_update_mibstats(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530118 struct ath9k_mib_stats *stats)
119{
120 stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
121 stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
122 stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
123 stats->rts_good += REG_READ(ah, AR_RTS_OK);
124 stats->beacons += REG_READ(ah, AR_BEACON_CNT);
125}
126
Felix Fietkau093115b2010-10-04 20:09:47 +0200127static void ath9k_ani_restart(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530128{
Sujithf1dc5602008-10-29 10:16:30 +0530129 struct ar5416AniState *aniState;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700130 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200131 u32 ofdm_base = 0, cck_base = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530132
133 if (!DO_ANI(ah))
134 return;
135
Felix Fietkau093115b2010-10-04 20:09:47 +0200136 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530137 aniState->listenTime = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530138
Felix Fietkau093115b2010-10-04 20:09:47 +0200139 if (!use_new_ani(ah)) {
140 ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
141 cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
Sujithf1dc5602008-10-29 10:16:30 +0530142 }
Felix Fietkau093115b2010-10-04 20:09:47 +0200143
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700144 ath_print(common, ATH_DBG_ANI,
Felix Fietkau093115b2010-10-04 20:09:47 +0200145 "Writing ofdmbase=%u cckbase=%u\n", ofdm_base, cck_base);
Sujith7d0d0df2010-04-16 11:53:57 +0530146
147 ENABLE_REGWRITE_BUFFER(ah);
148
Felix Fietkau093115b2010-10-04 20:09:47 +0200149 REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
150 REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400151 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
152 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
153
154 REGWRITE_BUFFER_FLUSH(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400155
156 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
157
158 aniState->ofdmPhyErrCount = 0;
159 aniState->cckPhyErrCount = 0;
160}
161
162static void ath9k_hw_ani_ofdm_err_trigger_old(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530163{
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -0700164 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Sujithf1dc5602008-10-29 10:16:30 +0530165 struct ar5416AniState *aniState;
Sujithf1dc5602008-10-29 10:16:30 +0530166 int32_t rssi;
167
168 if (!DO_ANI(ah))
169 return;
170
Felix Fietkau093115b2010-10-04 20:09:47 +0200171 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530172
173 if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
174 if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
175 aniState->noiseImmunityLevel + 1)) {
176 return;
177 }
178 }
179
180 if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
181 if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
182 aniState->spurImmunityLevel + 1)) {
183 return;
184 }
185 }
186
Sujith2660b812009-02-09 13:27:26 +0530187 if (ah->opmode == NL80211_IFTYPE_AP) {
Sujithf1dc5602008-10-29 10:16:30 +0530188 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
189 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
190 aniState->firstepLevel + 1);
191 }
192 return;
193 }
Sujithcbe61d82009-02-09 13:27:12 +0530194 rssi = BEACON_RSSI(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530195 if (rssi > aniState->rssiThrHigh) {
196 if (!aniState->ofdmWeakSigDetectOff) {
197 if (ath9k_hw_ani_control(ah,
198 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
199 false)) {
200 ath9k_hw_ani_control(ah,
201 ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
202 return;
203 }
204 }
205 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
206 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
207 aniState->firstepLevel + 1);
208 return;
209 }
210 } else if (rssi > aniState->rssiThrLow) {
211 if (aniState->ofdmWeakSigDetectOff)
212 ath9k_hw_ani_control(ah,
213 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
214 true);
215 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
216 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
217 aniState->firstepLevel + 1);
218 return;
219 } else {
Sujithd37b7da2009-09-11 08:30:03 +0530220 if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
221 !conf_is_ht(conf)) {
Sujithf1dc5602008-10-29 10:16:30 +0530222 if (!aniState->ofdmWeakSigDetectOff)
223 ath9k_hw_ani_control(ah,
224 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
225 false);
226 if (aniState->firstepLevel > 0)
227 ath9k_hw_ani_control(ah,
228 ATH9K_ANI_FIRSTEP_LEVEL, 0);
229 return;
230 }
231 }
232}
233
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400234static void ath9k_hw_ani_cck_err_trigger_old(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530235{
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -0700236 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Sujithf1dc5602008-10-29 10:16:30 +0530237 struct ar5416AniState *aniState;
Sujithf1dc5602008-10-29 10:16:30 +0530238 int32_t rssi;
239
240 if (!DO_ANI(ah))
241 return;
242
Felix Fietkau093115b2010-10-04 20:09:47 +0200243 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530244 if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
245 if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
246 aniState->noiseImmunityLevel + 1)) {
247 return;
248 }
249 }
Sujith2660b812009-02-09 13:27:26 +0530250 if (ah->opmode == NL80211_IFTYPE_AP) {
Sujithf1dc5602008-10-29 10:16:30 +0530251 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
252 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
253 aniState->firstepLevel + 1);
254 }
255 return;
256 }
Sujithcbe61d82009-02-09 13:27:12 +0530257 rssi = BEACON_RSSI(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530258 if (rssi > aniState->rssiThrLow) {
259 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
260 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
261 aniState->firstepLevel + 1);
262 } else {
Sujithd37b7da2009-09-11 08:30:03 +0530263 if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
264 !conf_is_ht(conf)) {
Sujithf1dc5602008-10-29 10:16:30 +0530265 if (aniState->firstepLevel > 0)
266 ath9k_hw_ani_control(ah,
267 ATH9K_ANI_FIRSTEP_LEVEL, 0);
268 }
269 }
270}
271
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400272/* Adjust the OFDM Noise Immunity Level */
273static void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel)
274{
Felix Fietkau093115b2010-10-04 20:09:47 +0200275 struct ar5416AniState *aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400276 struct ath_common *common = ath9k_hw_common(ah);
277 const struct ani_ofdm_level_entry *entry_ofdm;
278 const struct ani_cck_level_entry *entry_cck;
279
280 aniState->noiseFloor = BEACON_RSSI(ah);
281
282 ath_print(common, ATH_DBG_ANI,
283 "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
284 aniState->ofdmNoiseImmunityLevel,
285 immunityLevel, aniState->noiseFloor,
286 aniState->rssiThrLow, aniState->rssiThrHigh);
287
288 aniState->ofdmNoiseImmunityLevel = immunityLevel;
289
290 entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
291 entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
292
293 if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
294 ath9k_hw_ani_control(ah,
295 ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
296 entry_ofdm->spur_immunity_level);
297
298 if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
299 entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
300 ath9k_hw_ani_control(ah,
301 ATH9K_ANI_FIRSTEP_LEVEL,
302 entry_ofdm->fir_step_level);
303
304 if ((ah->opmode != NL80211_IFTYPE_STATION &&
305 ah->opmode != NL80211_IFTYPE_ADHOC) ||
306 aniState->noiseFloor <= aniState->rssiThrHigh) {
307 if (aniState->ofdmWeakSigDetectOff)
308 /* force on ofdm weak sig detect */
309 ath9k_hw_ani_control(ah,
310 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
311 true);
312 else if (aniState->ofdmWeakSigDetectOff ==
313 entry_ofdm->ofdm_weak_signal_on)
314 ath9k_hw_ani_control(ah,
315 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
316 entry_ofdm->ofdm_weak_signal_on);
317 }
318}
319
320static void ath9k_hw_ani_ofdm_err_trigger_new(struct ath_hw *ah)
321{
322 struct ar5416AniState *aniState;
323
324 if (!DO_ANI(ah))
325 return;
326
Felix Fietkau093115b2010-10-04 20:09:47 +0200327 aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400328
329 if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
330 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1);
331}
332
333/*
334 * Set the ANI settings to match an CCK level.
335 */
336static void ath9k_hw_set_cck_nil(struct ath_hw *ah, u_int8_t immunityLevel)
337{
Felix Fietkau093115b2010-10-04 20:09:47 +0200338 struct ar5416AniState *aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400339 struct ath_common *common = ath9k_hw_common(ah);
340 const struct ani_ofdm_level_entry *entry_ofdm;
341 const struct ani_cck_level_entry *entry_cck;
342
343 aniState->noiseFloor = BEACON_RSSI(ah);
344 ath_print(common, ATH_DBG_ANI,
345 "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
346 aniState->cckNoiseImmunityLevel, immunityLevel,
347 aniState->noiseFloor, aniState->rssiThrLow,
348 aniState->rssiThrHigh);
349
350 if ((ah->opmode == NL80211_IFTYPE_STATION ||
351 ah->opmode == NL80211_IFTYPE_ADHOC) &&
352 aniState->noiseFloor <= aniState->rssiThrLow &&
353 immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
354 immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
355
356 aniState->cckNoiseImmunityLevel = immunityLevel;
357
358 entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
359 entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
360
361 if (aniState->firstepLevel != entry_cck->fir_step_level &&
362 entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
363 ath9k_hw_ani_control(ah,
364 ATH9K_ANI_FIRSTEP_LEVEL,
365 entry_cck->fir_step_level);
366
367 /* Skip MRC CCK for pre AR9003 families */
368 if (!AR_SREV_9300_20_OR_LATER(ah))
369 return;
370
371 if (aniState->mrcCCKOff == entry_cck->mrc_cck_on)
372 ath9k_hw_ani_control(ah,
373 ATH9K_ANI_MRC_CCK,
374 entry_cck->mrc_cck_on);
375}
376
377static void ath9k_hw_ani_cck_err_trigger_new(struct ath_hw *ah)
378{
379 struct ar5416AniState *aniState;
380
381 if (!DO_ANI(ah))
382 return;
383
Felix Fietkau093115b2010-10-04 20:09:47 +0200384 aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400385
386 if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
387 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1);
388}
389
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -0400390static void ath9k_hw_ani_lower_immunity_old(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530391{
Sujithf1dc5602008-10-29 10:16:30 +0530392 struct ar5416AniState *aniState;
393 int32_t rssi;
394
Felix Fietkau093115b2010-10-04 20:09:47 +0200395 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530396
Sujith2660b812009-02-09 13:27:26 +0530397 if (ah->opmode == NL80211_IFTYPE_AP) {
Sujithf1dc5602008-10-29 10:16:30 +0530398 if (aniState->firstepLevel > 0) {
399 if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
400 aniState->firstepLevel - 1))
401 return;
402 }
403 } else {
Sujithcbe61d82009-02-09 13:27:12 +0530404 rssi = BEACON_RSSI(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530405 if (rssi > aniState->rssiThrHigh) {
406 /* XXX: Handle me */
407 } else if (rssi > aniState->rssiThrLow) {
408 if (aniState->ofdmWeakSigDetectOff) {
409 if (ath9k_hw_ani_control(ah,
410 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
411 true) == true)
412 return;
413 }
414 if (aniState->firstepLevel > 0) {
415 if (ath9k_hw_ani_control(ah,
416 ATH9K_ANI_FIRSTEP_LEVEL,
417 aniState->firstepLevel - 1) == true)
418 return;
419 }
420 } else {
421 if (aniState->firstepLevel > 0) {
422 if (ath9k_hw_ani_control(ah,
423 ATH9K_ANI_FIRSTEP_LEVEL,
424 aniState->firstepLevel - 1) == true)
425 return;
426 }
427 }
428 }
429
430 if (aniState->spurImmunityLevel > 0) {
431 if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
432 aniState->spurImmunityLevel - 1))
433 return;
434 }
435
436 if (aniState->noiseImmunityLevel > 0) {
437 ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
438 aniState->noiseImmunityLevel - 1);
439 return;
440 }
441}
442
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400443/*
444 * only lower either OFDM or CCK errors per turn
445 * we lower the other one next time
446 */
447static void ath9k_hw_ani_lower_immunity_new(struct ath_hw *ah)
448{
449 struct ar5416AniState *aniState;
450
Felix Fietkau093115b2010-10-04 20:09:47 +0200451 aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400452
453 /* lower OFDM noise immunity */
454 if (aniState->ofdmNoiseImmunityLevel > 0 &&
455 (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
456 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1);
457 return;
458 }
459
460 /* lower CCK noise immunity */
461 if (aniState->cckNoiseImmunityLevel > 0)
462 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1);
463}
464
Luis R. Rodriguez37e5bf62010-06-12 00:33:40 -0400465static u8 ath9k_hw_chan_2_clockrate_mhz(struct ath_hw *ah)
466{
467 struct ath9k_channel *chan = ah->curchan;
468 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
469 u8 clockrate; /* in MHz */
470
471 if (!ah->curchan) /* should really check for CCK instead */
472 clockrate = ATH9K_CLOCK_RATE_CCK;
473 else if (conf->channel->band == IEEE80211_BAND_2GHZ)
474 clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
475 else if (IS_CHAN_A_FAST_CLOCK(ah, chan))
476 clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
477 else
478 clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
479
480 if (conf_is_ht40(conf))
481 return clockrate * 2;
482
Felix Fietkau918df622010-10-03 19:07:19 +0200483 return clockrate;
Luis R. Rodriguez37e5bf62010-06-12 00:33:40 -0400484}
485
Sujithcbe61d82009-02-09 13:27:12 +0530486static int32_t ath9k_hw_ani_get_listen_time(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530487{
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200488 int32_t listen_time;
489 int32_t clock_rate;
Sujithf1dc5602008-10-29 10:16:30 +0530490
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200491 ath9k_hw_update_cycle_counters(ah);
492 clock_rate = ath9k_hw_chan_2_clockrate_mhz(ah) * 1000;
493 listen_time = ah->listen_time / clock_rate;
494 ah->listen_time = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530495
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200496 return listen_time;
Sujithf1dc5602008-10-29 10:16:30 +0530497}
498
Luis R. Rodriguez40346b62010-06-12 00:33:44 -0400499static void ath9k_ani_reset_old(struct ath_hw *ah, bool is_scanning)
Sujithf1dc5602008-10-29 10:16:30 +0530500{
Sujithf1dc5602008-10-29 10:16:30 +0530501 struct ar5416AniState *aniState;
Sujith2660b812009-02-09 13:27:26 +0530502 struct ath9k_channel *chan = ah->curchan;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700503 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530504
505 if (!DO_ANI(ah))
506 return;
507
Felix Fietkau093115b2010-10-04 20:09:47 +0200508 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530509
Felix Fietkau093115b2010-10-04 20:09:47 +0200510 if (ah->opmode != NL80211_IFTYPE_STATION
Sujith2660b812009-02-09 13:27:26 +0530511 && ah->opmode != NL80211_IFTYPE_ADHOC) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700512 ath_print(common, ATH_DBG_ANI,
513 "Reset ANI state opmode %u\n", ah->opmode);
Sujith2660b812009-02-09 13:27:26 +0530514 ah->stats.ast_ani_reset++;
Sujithf1dc5602008-10-29 10:16:30 +0530515
Luis R. Rodriguezc66284f2009-07-16 10:17:35 -0700516 if (ah->opmode == NL80211_IFTYPE_AP) {
517 /*
518 * ath9k_hw_ani_control() will only process items set on
519 * ah->ani_function
520 */
521 if (IS_CHAN_2GHZ(chan))
522 ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
523 ATH9K_ANI_FIRSTEP_LEVEL);
524 else
525 ah->ani_function = 0;
526 }
527
Sujithf1dc5602008-10-29 10:16:30 +0530528 ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
529 ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
530 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
531 ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
532 !ATH9K_ANI_USE_OFDM_WEAK_SIG);
533 ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
534 ATH9K_ANI_CCK_WEAK_SIG_THR);
535
536 ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
537 ATH9K_RX_FILTER_PHYERR);
538
Felix Fietkau093115b2010-10-04 20:09:47 +0200539 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530540 return;
541 }
542
543 if (aniState->noiseImmunityLevel != 0)
544 ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
545 aniState->noiseImmunityLevel);
546 if (aniState->spurImmunityLevel != 0)
547 ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
548 aniState->spurImmunityLevel);
549 if (aniState->ofdmWeakSigDetectOff)
550 ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
551 !aniState->ofdmWeakSigDetectOff);
552 if (aniState->cckWeakSigThreshold)
553 ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
554 aniState->cckWeakSigThreshold);
555 if (aniState->firstepLevel != 0)
556 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
557 aniState->firstepLevel);
Sujithf1dc5602008-10-29 10:16:30 +0530558
Sujith1aa8e842009-08-13 09:34:25 +0530559 ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
560 ~ATH9K_RX_FILTER_PHYERR);
Felix Fietkau093115b2010-10-04 20:09:47 +0200561 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400562
563 ENABLE_REGWRITE_BUFFER(ah);
564
565 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
566 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
567
568 REGWRITE_BUFFER_FLUSH(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400569}
570
571/*
572 * Restore the ANI parameters in the HAL and reset the statistics.
573 * This routine should be called for every hardware reset and for
574 * every channel change.
575 */
576static void ath9k_ani_reset_new(struct ath_hw *ah, bool is_scanning)
577{
Felix Fietkau093115b2010-10-04 20:09:47 +0200578 struct ar5416AniState *aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400579 struct ath9k_channel *chan = ah->curchan;
580 struct ath_common *common = ath9k_hw_common(ah);
581
582 if (!DO_ANI(ah))
583 return;
584
585 BUG_ON(aniState == NULL);
586 ah->stats.ast_ani_reset++;
587
588 /* only allow a subset of functions in AP mode */
589 if (ah->opmode == NL80211_IFTYPE_AP) {
590 if (IS_CHAN_2GHZ(chan)) {
591 ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
592 ATH9K_ANI_FIRSTEP_LEVEL);
593 if (AR_SREV_9300_20_OR_LATER(ah))
594 ah->ani_function |= ATH9K_ANI_MRC_CCK;
595 } else
596 ah->ani_function = 0;
597 }
598
599 /* always allow mode (on/off) to be controlled */
600 ah->ani_function |= ATH9K_ANI_MODE;
601
602 if (is_scanning ||
603 (ah->opmode != NL80211_IFTYPE_STATION &&
604 ah->opmode != NL80211_IFTYPE_ADHOC)) {
605 /*
606 * If we're scanning or in AP mode, the defaults (ini)
607 * should be in place. For an AP we assume the historical
608 * levels for this channel are probably outdated so start
609 * from defaults instead.
610 */
611 if (aniState->ofdmNoiseImmunityLevel !=
612 ATH9K_ANI_OFDM_DEF_LEVEL ||
613 aniState->cckNoiseImmunityLevel !=
614 ATH9K_ANI_CCK_DEF_LEVEL) {
615 ath_print(common, ATH_DBG_ANI,
616 "Restore defaults: opmode %u "
617 "chan %d Mhz/0x%x is_scanning=%d "
618 "ofdm:%d cck:%d\n",
619 ah->opmode,
620 chan->channel,
621 chan->channelFlags,
622 is_scanning,
623 aniState->ofdmNoiseImmunityLevel,
624 aniState->cckNoiseImmunityLevel);
625
626 ath9k_hw_set_ofdm_nil(ah, ATH9K_ANI_OFDM_DEF_LEVEL);
627 ath9k_hw_set_cck_nil(ah, ATH9K_ANI_CCK_DEF_LEVEL);
628 }
629 } else {
630 /*
631 * restore historical levels for this channel
632 */
633 ath_print(common, ATH_DBG_ANI,
634 "Restore history: opmode %u "
635 "chan %d Mhz/0x%x is_scanning=%d "
636 "ofdm:%d cck:%d\n",
637 ah->opmode,
638 chan->channel,
639 chan->channelFlags,
640 is_scanning,
641 aniState->ofdmNoiseImmunityLevel,
642 aniState->cckNoiseImmunityLevel);
643
644 ath9k_hw_set_ofdm_nil(ah,
645 aniState->ofdmNoiseImmunityLevel);
646 ath9k_hw_set_cck_nil(ah,
647 aniState->cckNoiseImmunityLevel);
648 }
649
650 /*
651 * enable phy counters if hw supports or if not, enable phy
652 * interrupts (so we can count each one)
653 */
Felix Fietkau093115b2010-10-04 20:09:47 +0200654 ath9k_ani_restart(ah);
Sujith7d0d0df2010-04-16 11:53:57 +0530655
656 ENABLE_REGWRITE_BUFFER(ah);
657
Sujith1aa8e842009-08-13 09:34:25 +0530658 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
659 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
Sujith7d0d0df2010-04-16 11:53:57 +0530660
661 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530662}
663
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -0400664static void ath9k_hw_ani_monitor_old(struct ath_hw *ah,
665 struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530666{
Sujithf1dc5602008-10-29 10:16:30 +0530667 struct ar5416AniState *aniState;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700668 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530669 int32_t listenTime;
Sujith1aa8e842009-08-13 09:34:25 +0530670 u32 phyCnt1, phyCnt2;
671 u32 ofdmPhyErrCnt, cckPhyErrCnt;
Felix Fietkau093115b2010-10-04 20:09:47 +0200672 u32 ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
673 u32 cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
Sujithf1dc5602008-10-29 10:16:30 +0530674
Gabor Juhos99506882009-01-14 20:17:11 +0100675 if (!DO_ANI(ah))
676 return;
677
Felix Fietkau093115b2010-10-04 20:09:47 +0200678 aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530679
680 listenTime = ath9k_hw_ani_get_listen_time(ah);
681 if (listenTime < 0) {
Sujith2660b812009-02-09 13:27:26 +0530682 ah->stats.ast_ani_lneg++;
Felix Fietkau093115b2010-10-04 20:09:47 +0200683 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530684 return;
685 }
686
687 aniState->listenTime += listenTime;
688
Sujith1aa8e842009-08-13 09:34:25 +0530689 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujithf1dc5602008-10-29 10:16:30 +0530690
Sujith1aa8e842009-08-13 09:34:25 +0530691 phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
692 phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
Sujithf1dc5602008-10-29 10:16:30 +0530693
Felix Fietkau093115b2010-10-04 20:09:47 +0200694 if (phyCnt1 < ofdm_base || phyCnt2 < cck_base) {
695 if (phyCnt1 < ofdm_base) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700696 ath_print(common, ATH_DBG_ANI,
697 "phyCnt1 0x%x, resetting "
698 "counter value to 0x%x\n",
Felix Fietkau093115b2010-10-04 20:09:47 +0200699 phyCnt1, ofdm_base);
700 REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
Sujith1aa8e842009-08-13 09:34:25 +0530701 REG_WRITE(ah, AR_PHY_ERR_MASK_1,
702 AR_PHY_ERR_OFDM_TIMING);
Sujithf1dc5602008-10-29 10:16:30 +0530703 }
Felix Fietkau093115b2010-10-04 20:09:47 +0200704 if (phyCnt2 < cck_base) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700705 ath_print(common, ATH_DBG_ANI,
706 "phyCnt2 0x%x, resetting "
707 "counter value to 0x%x\n",
Felix Fietkau093115b2010-10-04 20:09:47 +0200708 phyCnt2, cck_base);
709 REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
Sujith1aa8e842009-08-13 09:34:25 +0530710 REG_WRITE(ah, AR_PHY_ERR_MASK_2,
711 AR_PHY_ERR_CCK_TIMING);
712 }
713 return;
Sujithf1dc5602008-10-29 10:16:30 +0530714 }
715
Felix Fietkau093115b2010-10-04 20:09:47 +0200716 ofdmPhyErrCnt = phyCnt1 - ofdm_base;
Sujith1aa8e842009-08-13 09:34:25 +0530717 ah->stats.ast_ani_ofdmerrs +=
718 ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
719 aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
720
Felix Fietkau093115b2010-10-04 20:09:47 +0200721 cckPhyErrCnt = phyCnt2 - cck_base;
Sujith1aa8e842009-08-13 09:34:25 +0530722 ah->stats.ast_ani_cckerrs +=
723 cckPhyErrCnt - aniState->cckPhyErrCount;
724 aniState->cckPhyErrCount = cckPhyErrCnt;
725
Sujith2660b812009-02-09 13:27:26 +0530726 if (aniState->listenTime > 5 * ah->aniperiod) {
Sujithf1dc5602008-10-29 10:16:30 +0530727 if (aniState->ofdmPhyErrCount <= aniState->listenTime *
Felix Fietkau093115b2010-10-04 20:09:47 +0200728 ah->config.ofdm_trig_low / 1000 &&
Sujithf1dc5602008-10-29 10:16:30 +0530729 aniState->cckPhyErrCount <= aniState->listenTime *
Felix Fietkau093115b2010-10-04 20:09:47 +0200730 ah->config.cck_trig_low / 1000)
Sujithf1dc5602008-10-29 10:16:30 +0530731 ath9k_hw_ani_lower_immunity(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200732 ath9k_ani_restart(ah);
Sujith2660b812009-02-09 13:27:26 +0530733 } else if (aniState->listenTime > ah->aniperiod) {
Sujithf1dc5602008-10-29 10:16:30 +0530734 if (aniState->ofdmPhyErrCount > aniState->listenTime *
Felix Fietkau093115b2010-10-04 20:09:47 +0200735 ah->config.ofdm_trig_high / 1000) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400736 ath9k_hw_ani_ofdm_err_trigger_old(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200737 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530738 } else if (aniState->cckPhyErrCount >
Felix Fietkau093115b2010-10-04 20:09:47 +0200739 aniState->listenTime * ah->config.cck_trig_high /
Sujithf1dc5602008-10-29 10:16:30 +0530740 1000) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400741 ath9k_hw_ani_cck_err_trigger_old(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200742 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400743 }
744 }
745}
746
747static void ath9k_hw_ani_monitor_new(struct ath_hw *ah,
748 struct ath9k_channel *chan)
749{
750 struct ar5416AniState *aniState;
751 struct ath_common *common = ath9k_hw_common(ah);
752 int32_t listenTime;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400753 u32 ofdmPhyErrCnt, cckPhyErrCnt;
754 u32 ofdmPhyErrRate, cckPhyErrRate;
755
756 if (!DO_ANI(ah))
757 return;
758
Felix Fietkau093115b2010-10-04 20:09:47 +0200759 aniState = &ah->curchan->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400760 if (WARN_ON(!aniState))
761 return;
762
763 listenTime = ath9k_hw_ani_get_listen_time(ah);
764 if (listenTime <= 0) {
765 ah->stats.ast_ani_lneg++;
766 /* restart ANI period if listenTime is invalid */
767 ath_print(common, ATH_DBG_ANI,
768 "listenTime=%d - on new ani monitor\n",
769 listenTime);
Felix Fietkau093115b2010-10-04 20:09:47 +0200770 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400771 return;
772 }
773
774 aniState->listenTime += listenTime;
775
776 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
777
Felix Fietkau093115b2010-10-04 20:09:47 +0200778 ofdmPhyErrCnt = REG_READ(ah, AR_PHY_ERR_1);
779 cckPhyErrCnt = REG_READ(ah, AR_PHY_ERR_2);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400780
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400781 ah->stats.ast_ani_ofdmerrs +=
782 ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
783 aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
784
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400785 ah->stats.ast_ani_cckerrs +=
786 cckPhyErrCnt - aniState->cckPhyErrCount;
787 aniState->cckPhyErrCount = cckPhyErrCnt;
788
789 ath_print(common, ATH_DBG_ANI,
Felix Fietkau093115b2010-10-04 20:09:47 +0200790 "Errors: OFDM=%d, CCK=%d\n",
791 ofdmPhyErrCnt, cckPhyErrCnt);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400792
793 ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
794 aniState->listenTime;
795 cckPhyErrRate = aniState->cckPhyErrCount * 1000 /
796 aniState->listenTime;
797
798 ath_print(common, ATH_DBG_ANI,
799 "listenTime=%d OFDM:%d errs=%d/s CCK:%d "
800 "errs=%d/s ofdm_turn=%d\n",
801 listenTime, aniState->ofdmNoiseImmunityLevel,
802 ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
803 cckPhyErrRate, aniState->ofdmsTurn);
804
805 if (aniState->listenTime > 5 * ah->aniperiod) {
Felix Fietkau093115b2010-10-04 20:09:47 +0200806 if (ofdmPhyErrRate <= ah->config.ofdm_trig_low &&
807 cckPhyErrRate <= ah->config.cck_trig_low) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400808 ath_print(common, ATH_DBG_ANI,
809 "1. listenTime=%d OFDM:%d errs=%d/s(<%d) "
810 "CCK:%d errs=%d/s(<%d) -> "
811 "ath9k_hw_ani_lower_immunity()\n",
812 aniState->listenTime,
813 aniState->ofdmNoiseImmunityLevel,
814 ofdmPhyErrRate,
Felix Fietkau093115b2010-10-04 20:09:47 +0200815 ah->config.ofdm_trig_low,
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400816 aniState->cckNoiseImmunityLevel,
817 cckPhyErrRate,
Felix Fietkau093115b2010-10-04 20:09:47 +0200818 ah->config.cck_trig_low);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400819 ath9k_hw_ani_lower_immunity(ah);
820 aniState->ofdmsTurn = !aniState->ofdmsTurn;
821 }
822 ath_print(common, ATH_DBG_ANI,
823 "1 listenTime=%d ofdm=%d/s cck=%d/s - "
Felix Fietkau093115b2010-10-04 20:09:47 +0200824 "calling ath9k_ani_restart()\n",
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400825 aniState->listenTime, ofdmPhyErrRate, cckPhyErrRate);
Felix Fietkau093115b2010-10-04 20:09:47 +0200826 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400827 } else if (aniState->listenTime > ah->aniperiod) {
828 /* check to see if need to raise immunity */
Felix Fietkau093115b2010-10-04 20:09:47 +0200829 if (ofdmPhyErrRate > ah->config.ofdm_trig_high &&
830 (cckPhyErrRate <= ah->config.cck_trig_high ||
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400831 aniState->ofdmsTurn)) {
832 ath_print(common, ATH_DBG_ANI,
833 "2 listenTime=%d OFDM:%d errs=%d/s(>%d) -> "
834 "ath9k_hw_ani_ofdm_err_trigger_new()\n",
835 aniState->listenTime,
836 aniState->ofdmNoiseImmunityLevel,
837 ofdmPhyErrRate,
Felix Fietkau093115b2010-10-04 20:09:47 +0200838 ah->config.ofdm_trig_high);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400839 ath9k_hw_ani_ofdm_err_trigger_new(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200840 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400841 aniState->ofdmsTurn = false;
Felix Fietkau093115b2010-10-04 20:09:47 +0200842 } else if (cckPhyErrRate > ah->config.cck_trig_high) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400843 ath_print(common, ATH_DBG_ANI,
844 "3 listenTime=%d CCK:%d errs=%d/s(>%d) -> "
845 "ath9k_hw_ani_cck_err_trigger_new()\n",
846 aniState->listenTime,
847 aniState->cckNoiseImmunityLevel,
848 cckPhyErrRate,
Felix Fietkau093115b2010-10-04 20:09:47 +0200849 ah->config.cck_trig_high);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400850 ath9k_hw_ani_cck_err_trigger_new(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200851 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400852 aniState->ofdmsTurn = true;
Sujithf1dc5602008-10-29 10:16:30 +0530853 }
854 }
855}
856
Sujithcbe61d82009-02-09 13:27:12 +0530857void ath9k_enable_mib_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530858{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700859 struct ath_common *common = ath9k_hw_common(ah);
860
861 ath_print(common, ATH_DBG_ANI, "Enable MIB counters\n");
Sujithf1dc5602008-10-29 10:16:30 +0530862
Sujithcbe61d82009-02-09 13:27:12 +0530863 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujithf1dc5602008-10-29 10:16:30 +0530864
Sujith7d0d0df2010-04-16 11:53:57 +0530865 ENABLE_REGWRITE_BUFFER(ah);
866
Sujithf1dc5602008-10-29 10:16:30 +0530867 REG_WRITE(ah, AR_FILT_OFDM, 0);
868 REG_WRITE(ah, AR_FILT_CCK, 0);
869 REG_WRITE(ah, AR_MIBC,
870 ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
871 & 0x0f);
872 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
873 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
Sujith7d0d0df2010-04-16 11:53:57 +0530874
875 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530876}
877
Sujith0fd06c92009-02-12 10:06:51 +0530878/* Freeze the MIB counters, get the stats and then clear them */
Sujithcbe61d82009-02-09 13:27:12 +0530879void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530880{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700881 struct ath_common *common = ath9k_hw_common(ah);
882
883 ath_print(common, ATH_DBG_ANI, "Disable MIB counters\n");
884
Sujith0fd06c92009-02-12 10:06:51 +0530885 REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
Sujithcbe61d82009-02-09 13:27:12 +0530886 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujith0fd06c92009-02-12 10:06:51 +0530887 REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
Sujithf1dc5602008-10-29 10:16:30 +0530888 REG_WRITE(ah, AR_FILT_OFDM, 0);
889 REG_WRITE(ah, AR_FILT_CCK, 0);
890}
Sujith21d51302010-06-01 15:14:18 +0530891EXPORT_SYMBOL(ath9k_hw_disable_mib_counters);
Sujithf1dc5602008-10-29 10:16:30 +0530892
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200893void ath9k_hw_update_cycle_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530894{
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200895 struct ath_cycle_counters cc;
896 bool clear;
Sujithf1dc5602008-10-29 10:16:30 +0530897
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200898 memcpy(&cc, &ah->cc, sizeof(cc));
Sujithf1dc5602008-10-29 10:16:30 +0530899
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200900 /* freeze counters */
901 REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
Sujithf1dc5602008-10-29 10:16:30 +0530902
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200903 ah->cc.cycles = REG_READ(ah, AR_CCCNT);
904 if (ah->cc.cycles < cc.cycles) {
905 clear = true;
906 goto skip;
Sujithf1dc5602008-10-29 10:16:30 +0530907 }
908
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200909 ah->cc.rx_clear = REG_READ(ah, AR_RCCNT);
910 ah->cc.rx_frame = REG_READ(ah, AR_RFCNT);
911 ah->cc.tx_frame = REG_READ(ah, AR_TFCNT);
Sujithf1dc5602008-10-29 10:16:30 +0530912
Felix Fietkau9dbebc72010-10-03 19:07:17 +0200913 /* prevent wraparound */
914 if (ah->cc.cycles & BIT(31))
915 clear = true;
916
917#define CC_DELTA(_field, _reg) ah->cc_delta._field += ah->cc._field - cc._field
918 CC_DELTA(cycles, AR_CCCNT);
919 CC_DELTA(rx_frame, AR_RFCNT);
920 CC_DELTA(rx_clear, AR_RCCNT);
921 CC_DELTA(tx_frame, AR_TFCNT);
922#undef CC_DELTA
923
924 ah->listen_time += (ah->cc.cycles - cc.cycles) -
925 ((ah->cc.rx_frame - cc.rx_frame) +
926 (ah->cc.tx_frame - cc.tx_frame));
927
928skip:
929 if (clear) {
930 REG_WRITE(ah, AR_CCCNT, 0);
931 REG_WRITE(ah, AR_RFCNT, 0);
932 REG_WRITE(ah, AR_RCCNT, 0);
933 REG_WRITE(ah, AR_TFCNT, 0);
934 memset(&ah->cc, 0, sizeof(ah->cc));
935 }
936
937 /* unfreeze counters */
938 REG_WRITE(ah, AR_MIBC, 0);
Sujithf1dc5602008-10-29 10:16:30 +0530939}
940
941/*
942 * Process a MIB interrupt. We may potentially be invoked because
943 * any of the MIB counters overflow/trigger so don't assume we're
944 * here because a PHY error counter triggered.
945 */
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -0400946static void ath9k_hw_proc_mib_event_old(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530947{
Felix Fietkau093115b2010-10-04 20:09:47 +0200948 u32 ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
949 u32 cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
Sujithf1dc5602008-10-29 10:16:30 +0530950 u32 phyCnt1, phyCnt2;
951
952 /* Reset these counters regardless */
953 REG_WRITE(ah, AR_FILT_OFDM, 0);
954 REG_WRITE(ah, AR_FILT_CCK, 0);
955 if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
956 REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
957
958 /* Clear the mib counters and save them in the stats */
Sujithcbe61d82009-02-09 13:27:12 +0530959 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujithf1dc5602008-10-29 10:16:30 +0530960
Luis R. Rodriguez6e97f0f2010-06-12 00:33:41 -0400961 if (!DO_ANI(ah)) {
962 /*
963 * We must always clear the interrupt cause by
964 * resetting the phy error regs.
965 */
966 REG_WRITE(ah, AR_PHY_ERR_1, 0);
967 REG_WRITE(ah, AR_PHY_ERR_2, 0);
Sujithf1dc5602008-10-29 10:16:30 +0530968 return;
Luis R. Rodriguez6e97f0f2010-06-12 00:33:41 -0400969 }
Sujithf1dc5602008-10-29 10:16:30 +0530970
971 /* NB: these are not reset-on-read */
972 phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
973 phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
974 if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
975 ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
Felix Fietkau093115b2010-10-04 20:09:47 +0200976 struct ar5416AniState *aniState = &ah->curchan->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530977 u32 ofdmPhyErrCnt, cckPhyErrCnt;
978
979 /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
Felix Fietkau093115b2010-10-04 20:09:47 +0200980 ofdmPhyErrCnt = phyCnt1 - ofdm_base;
Sujith2660b812009-02-09 13:27:26 +0530981 ah->stats.ast_ani_ofdmerrs +=
Sujithf1dc5602008-10-29 10:16:30 +0530982 ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
983 aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
984
Felix Fietkau093115b2010-10-04 20:09:47 +0200985 cckPhyErrCnt = phyCnt2 - cck_base;
Sujith2660b812009-02-09 13:27:26 +0530986 ah->stats.ast_ani_cckerrs +=
Sujithf1dc5602008-10-29 10:16:30 +0530987 cckPhyErrCnt - aniState->cckPhyErrCount;
988 aniState->cckPhyErrCount = cckPhyErrCnt;
989
990 /*
991 * NB: figure out which counter triggered. If both
992 * trigger we'll only deal with one as the processing
993 * clobbers the error counter so the trigger threshold
994 * check will never be true.
995 */
Felix Fietkau093115b2010-10-04 20:09:47 +0200996 if (aniState->ofdmPhyErrCount > ah->config.ofdm_trig_high)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400997 ath9k_hw_ani_ofdm_err_trigger_new(ah);
Felix Fietkau093115b2010-10-04 20:09:47 +0200998 if (aniState->cckPhyErrCount > ah->config.cck_trig_high)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400999 ath9k_hw_ani_cck_err_trigger_old(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301000 /* NB: always restart to insure the h/w counters are reset */
Felix Fietkau093115b2010-10-04 20:09:47 +02001001 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301002 }
1003}
1004
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001005/*
1006 * Process a MIB interrupt. We may potentially be invoked because
1007 * any of the MIB counters overflow/trigger so don't assume we're
1008 * here because a PHY error counter triggered.
1009 */
1010static void ath9k_hw_proc_mib_event_new(struct ath_hw *ah)
1011{
1012 u32 phyCnt1, phyCnt2;
1013
1014 /* Reset these counters regardless */
1015 REG_WRITE(ah, AR_FILT_OFDM, 0);
1016 REG_WRITE(ah, AR_FILT_CCK, 0);
1017 if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
1018 REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
1019
1020 /* Clear the mib counters and save them in the stats */
1021 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
1022
1023 if (!DO_ANI(ah)) {
1024 /*
1025 * We must always clear the interrupt cause by
1026 * resetting the phy error regs.
1027 */
1028 REG_WRITE(ah, AR_PHY_ERR_1, 0);
1029 REG_WRITE(ah, AR_PHY_ERR_2, 0);
1030 return;
1031 }
1032
1033 /* NB: these are not reset-on-read */
1034 phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
1035 phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
1036
1037 /* NB: always restart to insure the h/w counters are reset */
1038 if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
1039 ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK))
Felix Fietkau093115b2010-10-04 20:09:47 +02001040 ath9k_ani_restart(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001041}
1042
Sujithcbe61d82009-02-09 13:27:12 +05301043void ath9k_hw_ani_setup(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301044{
Sujithf1dc5602008-10-29 10:16:30 +05301045 int i;
1046
1047 const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
1048 const int coarseHigh[] = { -14, -14, -14, -14, -12 };
1049 const int coarseLow[] = { -64, -64, -64, -64, -70 };
1050 const int firpwr[] = { -78, -78, -78, -78, -80 };
1051
1052 for (i = 0; i < 5; i++) {
Sujith2660b812009-02-09 13:27:26 +05301053 ah->totalSizeDesired[i] = totalSizeDesired[i];
1054 ah->coarse_high[i] = coarseHigh[i];
1055 ah->coarse_low[i] = coarseLow[i];
1056 ah->firpwr[i] = firpwr[i];
Sujithf1dc5602008-10-29 10:16:30 +05301057 }
1058}
1059
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -07001060void ath9k_hw_ani_init(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301061{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001062 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301063 int i;
1064
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001065 ath_print(common, ATH_DBG_ANI, "Initialize ANI\n");
Sujithf1dc5602008-10-29 10:16:30 +05301066
Felix Fietkau093115b2010-10-04 20:09:47 +02001067 if (use_new_ani(ah)) {
1068 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_NEW;
1069 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_NEW;
1070
1071 ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_NEW;
1072 ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_NEW;
1073 } else {
1074 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
1075 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
1076
1077 ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
1078 ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
1079 }
1080
1081 for (i = 0; i < ARRAY_SIZE(ah->channels); i++) {
1082 struct ath9k_channel *chan = &ah->channels[i];
1083 struct ar5416AniState *ani = &chan->ani;
1084
Felix Fietkau71ea4202010-10-04 20:09:46 +02001085 if (use_new_ani(ah)) {
Felix Fietkau093115b2010-10-04 20:09:47 +02001086 ani->spurImmunityLevel =
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001087 ATH9K_ANI_SPUR_IMMUNE_LVL_NEW;
1088
Felix Fietkau093115b2010-10-04 20:09:47 +02001089 ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_NEW;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001090
1091 if (AR_SREV_9300_20_OR_LATER(ah))
Felix Fietkau093115b2010-10-04 20:09:47 +02001092 ani->mrcCCKOff =
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001093 !ATH9K_ANI_ENABLE_MRC_CCK;
1094 else
Felix Fietkau093115b2010-10-04 20:09:47 +02001095 ani->mrcCCKOff = true;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001096
Felix Fietkau093115b2010-10-04 20:09:47 +02001097 ani->ofdmsTurn = true;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001098 } else {
Felix Fietkau093115b2010-10-04 20:09:47 +02001099 ani->spurImmunityLevel =
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001100 ATH9K_ANI_SPUR_IMMUNE_LVL_OLD;
Felix Fietkau093115b2010-10-04 20:09:47 +02001101 ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_OLD;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001102
Felix Fietkau093115b2010-10-04 20:09:47 +02001103 ani->cckWeakSigThreshold =
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001104 ATH9K_ANI_CCK_WEAK_SIG_THR;
1105 }
1106
Felix Fietkau093115b2010-10-04 20:09:47 +02001107 ani->rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
1108 ani->rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
1109 ani->ofdmWeakSigDetectOff =
Sujithf1dc5602008-10-29 10:16:30 +05301110 !ATH9K_ANI_USE_OFDM_WEAK_SIG;
Felix Fietkau093115b2010-10-04 20:09:47 +02001111 ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001112 }
1113
1114 /*
1115 * since we expect some ongoing maintenance on the tables, let's sanity
1116 * check here default level should not modify INI setting.
1117 */
Felix Fietkau71ea4202010-10-04 20:09:46 +02001118 if (use_new_ani(ah)) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001119 const struct ani_ofdm_level_entry *entry_ofdm;
1120 const struct ani_cck_level_entry *entry_cck;
1121
1122 entry_ofdm = &ofdm_level_table[ATH9K_ANI_OFDM_DEF_LEVEL];
1123 entry_cck = &cck_level_table[ATH9K_ANI_CCK_DEF_LEVEL];
1124
1125 ah->aniperiod = ATH9K_ANI_PERIOD_NEW;
1126 ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_NEW;
1127 } else {
1128 ah->aniperiod = ATH9K_ANI_PERIOD_OLD;
1129 ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_OLD;
Sujithf1dc5602008-10-29 10:16:30 +05301130 }
Sujithf1dc5602008-10-29 10:16:30 +05301131
Sujith2660b812009-02-09 13:27:26 +05301132 if (ah->config.enable_ani)
1133 ah->proc_phyerr |= HAL_PROCESS_ANI;
Felix Fietkau093115b2010-10-04 20:09:47 +02001134
1135 ath9k_ani_restart(ah);
1136 ath9k_enable_mib_counters(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301137}
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -04001138
1139void ath9k_hw_attach_ani_ops_old(struct ath_hw *ah)
1140{
1141 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1142 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1143
1144 priv_ops->ani_reset = ath9k_ani_reset_old;
1145 priv_ops->ani_lower_immunity = ath9k_hw_ani_lower_immunity_old;
1146
1147 ops->ani_proc_mib_event = ath9k_hw_proc_mib_event_old;
1148 ops->ani_monitor = ath9k_hw_ani_monitor_old;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001149
1150 ath_print(ath9k_hw_common(ah), ATH_DBG_ANY, "Using ANI v1\n");
1151}
1152
1153void ath9k_hw_attach_ani_ops_new(struct ath_hw *ah)
1154{
1155 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1156 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1157
1158 priv_ops->ani_reset = ath9k_ani_reset_new;
1159 priv_ops->ani_lower_immunity = ath9k_hw_ani_lower_immunity_new;
1160
1161 ops->ani_proc_mib_event = ath9k_hw_proc_mib_event_new;
1162 ops->ani_monitor = ath9k_hw_ani_monitor_new;
1163
1164 ath_print(ath9k_hw_common(ah), ATH_DBG_ANY, "Using ANI v2\n");
Luis R. Rodriguezac0bb762010-06-12 00:33:42 -04001165}