blob: be466b0ef7a7725c736114ee439596ca45a4a380 [file] [log] [blame]
Sujithf1dc5602008-10-29 10:16:30 +05301/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2008-2011 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>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040018#include <linux/export.h>
Luis R. Rodriguezcfe8cba2009-09-13 23:39:31 -070019#include "hw.h"
Felix Fietkauc16fcb42010-04-15 17:38:39 -040020#include "hw-ops.h"
Sujithf1dc5602008-10-29 10:16:30 +053021
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040022struct ani_ofdm_level_entry {
23 int spur_immunity_level;
24 int fir_step_level;
25 int ofdm_weak_signal_on;
26};
27
28/* values here are relative to the INI */
29
30/*
31 * Legend:
32 *
33 * SI: Spur immunity
34 * FS: FIR Step
35 * WS: OFDM / CCK Weak Signal detection
36 * MRC-CCK: Maximal Ratio Combining for CCK
37 */
38
39static const struct ani_ofdm_level_entry ofdm_level_table[] = {
40 /* SI FS WS */
41 { 0, 0, 1 }, /* lvl 0 */
42 { 1, 1, 1 }, /* lvl 1 */
43 { 2, 2, 1 }, /* lvl 2 */
44 { 3, 2, 1 }, /* lvl 3 (default) */
45 { 4, 3, 1 }, /* lvl 4 */
46 { 5, 4, 1 }, /* lvl 5 */
47 { 6, 5, 1 }, /* lvl 6 */
48 { 7, 6, 1 }, /* lvl 7 */
Sujith Manoharanb99553f2013-06-04 15:41:32 +053049 { 7, 7, 1 }, /* lvl 8 */
50 { 7, 8, 0 } /* lvl 9 */
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040051};
52#define ATH9K_ANI_OFDM_NUM_LEVEL \
Nikitas Angelinasbbce80e2010-09-08 22:25:42 +010053 ARRAY_SIZE(ofdm_level_table)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040054#define ATH9K_ANI_OFDM_MAX_LEVEL \
55 (ATH9K_ANI_OFDM_NUM_LEVEL-1)
56#define ATH9K_ANI_OFDM_DEF_LEVEL \
57 3 /* default level - matches the INI settings */
58
59/*
60 * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
61 * With OFDM for single stream you just add up all antenna inputs, you're
62 * only interested in what you get after FFT. Signal aligment is also not
63 * required for OFDM because any phase difference adds up in the frequency
64 * domain.
65 *
66 * MRC requires extra work for use with CCK. You need to align the antenna
67 * signals from the different antenna before you can add the signals together.
68 * You need aligment of signals as CCK is in time domain, so addition can cancel
69 * your signal completely if phase is 180 degrees (think of adding sine waves).
70 * You also need to remove noise before the addition and this is where ANI
71 * MRC CCK comes into play. One of the antenna inputs may be stronger but
72 * lower SNR, so just adding after alignment can be dangerous.
73 *
74 * Regardless of alignment in time, the antenna signals add constructively after
75 * FFT and improve your reception. For more information:
76 *
77 * http://en.wikipedia.org/wiki/Maximal-ratio_combining
78 */
79
80struct ani_cck_level_entry {
81 int fir_step_level;
82 int mrc_cck_on;
83};
84
85static const struct ani_cck_level_entry cck_level_table[] = {
86 /* FS MRC-CCK */
87 { 0, 1 }, /* lvl 0 */
88 { 1, 1 }, /* lvl 1 */
89 { 2, 1 }, /* lvl 2 (default) */
90 { 3, 1 }, /* lvl 3 */
91 { 4, 0 }, /* lvl 4 */
92 { 5, 0 }, /* lvl 5 */
93 { 6, 0 }, /* lvl 6 */
Sujith Manoharanb99553f2013-06-04 15:41:32 +053094 { 7, 0 }, /* lvl 7 (only for high rssi) */
95 { 8, 0 } /* lvl 8 (only for high rssi) */
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040096};
97
98#define ATH9K_ANI_CCK_NUM_LEVEL \
Nikitas Angelinasbbce80e2010-09-08 22:25:42 +010099 ARRAY_SIZE(cck_level_table)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400100#define ATH9K_ANI_CCK_MAX_LEVEL \
101 (ATH9K_ANI_CCK_NUM_LEVEL-1)
102#define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
103 (ATH9K_ANI_CCK_NUM_LEVEL-3)
104#define ATH9K_ANI_CCK_DEF_LEVEL \
105 2 /* default level - matches the INI settings */
106
Sujithcbe61d82009-02-09 13:27:12 +0530107static void ath9k_hw_update_mibstats(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530108 struct ath9k_mib_stats *stats)
109{
110 stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
111 stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
112 stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
113 stats->rts_good += REG_READ(ah, AR_RTS_OK);
114 stats->beacons += REG_READ(ah, AR_BEACON_CNT);
115}
116
Felix Fietkau093115b2010-10-04 20:09:47 +0200117static void ath9k_ani_restart(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530118{
Sujithf1dc5602008-10-29 10:16:30 +0530119 struct ar5416AniState *aniState;
120
Sujith Manoharane3233002013-06-03 09:19:26 +0530121 if (!ah->curchan)
Sujithf1dc5602008-10-29 10:16:30 +0530122 return;
123
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530124 aniState = &ah->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530125 aniState->listenTime = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530126
Sujith7d0d0df2010-04-16 11:53:57 +0530127 ENABLE_REGWRITE_BUFFER(ah);
128
Felix Fietkau465dce62012-06-15 15:25:24 +0200129 REG_WRITE(ah, AR_PHY_ERR_1, 0);
130 REG_WRITE(ah, AR_PHY_ERR_2, 0);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400131 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
132 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
133
134 REGWRITE_BUFFER_FLUSH(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400135
136 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
137
138 aniState->ofdmPhyErrCount = 0;
139 aniState->cckPhyErrCount = 0;
140}
141
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400142/* Adjust the OFDM Noise Immunity Level */
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200143static void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel,
144 bool scan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400145{
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530146 struct ar5416AniState *aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400147 struct ath_common *common = ath9k_hw_common(ah);
148 const struct ani_ofdm_level_entry *entry_ofdm;
149 const struct ani_cck_level_entry *entry_cck;
Felix Fietkau0b81cc32012-06-15 15:25:20 +0200150 bool weak_sig;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400151
Joe Perchesd2182b62011-12-15 14:55:53 -0800152 ath_dbg(common, ANI, "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
Joe Perches226afe62010-12-02 19:12:37 -0800153 aniState->ofdmNoiseImmunityLevel,
Felix Fietkau35e808b2012-06-15 15:25:19 +0200154 immunityLevel, BEACON_RSSI(ah),
Felix Fietkau9dbac672013-01-20 18:51:58 +0100155 ATH9K_ANI_RSSI_THR_LOW,
156 ATH9K_ANI_RSSI_THR_HIGH);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400157
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200158 if (!scan)
Felix Fietkau1e8f0a32012-06-15 15:25:26 +0200159 aniState->ofdmNoiseImmunityLevel = immunityLevel;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400160
161 entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
162 entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
163
164 if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
165 ath9k_hw_ani_control(ah,
166 ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
167 entry_ofdm->spur_immunity_level);
168
169 if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
170 entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
171 ath9k_hw_ani_control(ah,
172 ATH9K_ANI_FIRSTEP_LEVEL,
173 entry_ofdm->fir_step_level);
174
Felix Fietkau0b81cc32012-06-15 15:25:20 +0200175 weak_sig = entry_ofdm->ofdm_weak_signal_on;
176 if (ah->opmode == NL80211_IFTYPE_STATION &&
Felix Fietkau9dbac672013-01-20 18:51:58 +0100177 BEACON_RSSI(ah) <= ATH9K_ANI_RSSI_THR_HIGH)
Felix Fietkau0b81cc32012-06-15 15:25:20 +0200178 weak_sig = true;
179
Sujith Manoharan80b42052013-06-04 15:41:30 +0530180 /*
181 * OFDM Weak signal detection is always enabled for AP mode.
182 */
183 if (ah->opmode != NL80211_IFTYPE_AP &&
184 aniState->ofdmWeakSigDetect != weak_sig) {
185 ath9k_hw_ani_control(ah,
186 ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
187 entry_ofdm->ofdm_weak_signal_on);
188 }
Sujith Manoharan55fee982013-06-17 14:24:36 +0530189
190 if (aniState->ofdmNoiseImmunityLevel >= ATH9K_ANI_OFDM_DEF_LEVEL) {
191 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH;
192 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_ABOVE_INI;
193 } else {
194 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_BELOW_INI;
195 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW;
196 }
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400197}
198
Felix Fietkau8eb49802010-10-04 20:09:49 +0200199static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400200{
201 struct ar5416AniState *aniState;
202
Sujith Manoharane3233002013-06-03 09:19:26 +0530203 if (!ah->curchan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400204 return;
205
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530206 aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400207
208 if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200209 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1, false);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400210}
211
212/*
213 * Set the ANI settings to match an CCK level.
214 */
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200215static void ath9k_hw_set_cck_nil(struct ath_hw *ah, u_int8_t immunityLevel,
216 bool scan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400217{
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530218 struct ar5416AniState *aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400219 struct ath_common *common = ath9k_hw_common(ah);
220 const struct ani_ofdm_level_entry *entry_ofdm;
221 const struct ani_cck_level_entry *entry_cck;
222
Joe Perchesd2182b62011-12-15 14:55:53 -0800223 ath_dbg(common, ANI, "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
Joe Perches226afe62010-12-02 19:12:37 -0800224 aniState->cckNoiseImmunityLevel, immunityLevel,
Felix Fietkau9dbac672013-01-20 18:51:58 +0100225 BEACON_RSSI(ah), ATH9K_ANI_RSSI_THR_LOW,
226 ATH9K_ANI_RSSI_THR_HIGH);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400227
Felix Fietkau5330df72012-06-15 15:25:22 +0200228 if (ah->opmode == NL80211_IFTYPE_STATION &&
Felix Fietkau9dbac672013-01-20 18:51:58 +0100229 BEACON_RSSI(ah) <= ATH9K_ANI_RSSI_THR_LOW &&
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400230 immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
231 immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
232
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200233 if (!scan)
Felix Fietkau1e8f0a32012-06-15 15:25:26 +0200234 aniState->cckNoiseImmunityLevel = immunityLevel;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400235
236 entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
237 entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
238
239 if (aniState->firstepLevel != entry_cck->fir_step_level &&
240 entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
241 ath9k_hw_ani_control(ah,
242 ATH9K_ANI_FIRSTEP_LEVEL,
243 entry_cck->fir_step_level);
244
245 /* Skip MRC CCK for pre AR9003 families */
Sujith Manoharana4a29542012-09-10 09:20:03 +0530246 if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah))
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400247 return;
248
Rajkumar Manoharan81b67fd62012-06-21 20:33:59 +0530249 if (aniState->mrcCCK != entry_cck->mrc_cck_on)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400250 ath9k_hw_ani_control(ah,
251 ATH9K_ANI_MRC_CCK,
252 entry_cck->mrc_cck_on);
253}
254
Felix Fietkau8eb49802010-10-04 20:09:49 +0200255static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400256{
257 struct ar5416AniState *aniState;
258
Sujith Manoharane3233002013-06-03 09:19:26 +0530259 if (!ah->curchan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400260 return;
261
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530262 aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400263
264 if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200265 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1,
266 false);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400267}
268
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400269/*
270 * only lower either OFDM or CCK errors per turn
271 * we lower the other one next time
272 */
Felix Fietkau8eb49802010-10-04 20:09:49 +0200273static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400274{
275 struct ar5416AniState *aniState;
276
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530277 aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400278
279 /* lower OFDM noise immunity */
280 if (aniState->ofdmNoiseImmunityLevel > 0 &&
281 (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200282 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1,
283 false);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400284 return;
285 }
286
287 /* lower CCK noise immunity */
288 if (aniState->cckNoiseImmunityLevel > 0)
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200289 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1,
290 false);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400291}
292
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400293/*
294 * Restore the ANI parameters in the HAL and reset the statistics.
295 * This routine should be called for every hardware reset and for
296 * every channel change.
297 */
Felix Fietkau8eb49802010-10-04 20:09:49 +0200298void ath9k_ani_reset(struct ath_hw *ah, bool is_scanning)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400299{
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530300 struct ar5416AniState *aniState = &ah->ani;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400301 struct ath9k_channel *chan = ah->curchan;
302 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau1e8f0a32012-06-15 15:25:26 +0200303 int ofdm_nil, cck_nil;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400304
Sujith Manoharane3233002013-06-03 09:19:26 +0530305 if (!ah->curchan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400306 return;
307
308 BUG_ON(aniState == NULL);
309 ah->stats.ast_ani_reset++;
310
311 /* only allow a subset of functions in AP mode */
312 if (ah->opmode == NL80211_IFTYPE_AP) {
313 if (IS_CHAN_2GHZ(chan)) {
314 ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
315 ATH9K_ANI_FIRSTEP_LEVEL);
316 if (AR_SREV_9300_20_OR_LATER(ah))
317 ah->ani_function |= ATH9K_ANI_MRC_CCK;
318 } else
319 ah->ani_function = 0;
320 }
321
Felix Fietkau1e8f0a32012-06-15 15:25:26 +0200322 ofdm_nil = max_t(int, ATH9K_ANI_OFDM_DEF_LEVEL,
323 aniState->ofdmNoiseImmunityLevel);
324 cck_nil = max_t(int, ATH9K_ANI_CCK_DEF_LEVEL,
325 aniState->cckNoiseImmunityLevel);
326
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400327 if (is_scanning ||
328 (ah->opmode != NL80211_IFTYPE_STATION &&
329 ah->opmode != NL80211_IFTYPE_ADHOC)) {
330 /*
331 * If we're scanning or in AP mode, the defaults (ini)
332 * should be in place. For an AP we assume the historical
333 * levels for this channel are probably outdated so start
334 * from defaults instead.
335 */
336 if (aniState->ofdmNoiseImmunityLevel !=
337 ATH9K_ANI_OFDM_DEF_LEVEL ||
338 aniState->cckNoiseImmunityLevel !=
339 ATH9K_ANI_CCK_DEF_LEVEL) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800340 ath_dbg(common, ANI,
Joe Perches226afe62010-12-02 19:12:37 -0800341 "Restore defaults: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
342 ah->opmode,
343 chan->channel,
344 chan->channelFlags,
345 is_scanning,
346 aniState->ofdmNoiseImmunityLevel,
347 aniState->cckNoiseImmunityLevel);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400348
Felix Fietkau1e8f0a32012-06-15 15:25:26 +0200349 ofdm_nil = ATH9K_ANI_OFDM_DEF_LEVEL;
350 cck_nil = ATH9K_ANI_CCK_DEF_LEVEL;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400351 }
352 } else {
353 /*
354 * restore historical levels for this channel
355 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800356 ath_dbg(common, ANI,
Joe Perches226afe62010-12-02 19:12:37 -0800357 "Restore history: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
358 ah->opmode,
359 chan->channel,
360 chan->channelFlags,
361 is_scanning,
362 aniState->ofdmNoiseImmunityLevel,
363 aniState->cckNoiseImmunityLevel);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400364 }
Felix Fietkau73dc3eb2012-06-27 14:58:19 +0200365 ath9k_hw_set_ofdm_nil(ah, ofdm_nil, is_scanning);
366 ath9k_hw_set_cck_nil(ah, cck_nil, is_scanning);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400367
Felix Fietkau093115b2010-10-04 20:09:47 +0200368 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530369}
370
Felix Fietkaue49f9132010-10-12 16:08:01 +0200371static bool ath9k_hw_ani_read_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530372{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700373 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530374 struct ar5416AniState *aniState = &ah->ani;
Felix Fietkaubfc472b2010-10-04 20:09:48 +0200375 u32 phyCnt1, phyCnt2;
376 int32_t listenTime;
Sujithf1dc5602008-10-29 10:16:30 +0530377
Felix Fietkaub5bfc562010-10-08 22:13:53 +0200378 ath_hw_cycle_counters_update(common);
379 listenTime = ath_hw_get_listen_time(common);
380
Felix Fietkaue49f9132010-10-12 16:08:01 +0200381 if (listenTime <= 0) {
Mohammed Shafi Shajakhan107021c2011-08-26 11:19:57 +0530382 ah->stats.ast_ani_lneg_or_lzero++;
Felix Fietkau093115b2010-10-04 20:09:47 +0200383 ath9k_ani_restart(ah);
Felix Fietkaue49f9132010-10-12 16:08:01 +0200384 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530385 }
386
387 aniState->listenTime += listenTime;
388
Sujith1aa8e842009-08-13 09:34:25 +0530389 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujithf1dc5602008-10-29 10:16:30 +0530390
Sujith1aa8e842009-08-13 09:34:25 +0530391 phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
392 phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
Sujithf1dc5602008-10-29 10:16:30 +0530393
Felix Fietkau465dce62012-06-15 15:25:24 +0200394 ah->stats.ast_ani_ofdmerrs += phyCnt1 - aniState->ofdmPhyErrCount;
395 aniState->ofdmPhyErrCount = phyCnt1;
Sujith1aa8e842009-08-13 09:34:25 +0530396
Felix Fietkau465dce62012-06-15 15:25:24 +0200397 ah->stats.ast_ani_cckerrs += phyCnt2 - aniState->cckPhyErrCount;
398 aniState->cckPhyErrCount = phyCnt2;
399
Felix Fietkaue49f9132010-10-12 16:08:01 +0200400 return true;
Felix Fietkaubfc472b2010-10-04 20:09:48 +0200401}
402
Felix Fietkau95792172010-10-04 20:09:50 +0200403void ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400404{
405 struct ar5416AniState *aniState;
406 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400407 u32 ofdmPhyErrRate, cckPhyErrRate;
408
Sujith Manoharane3233002013-06-03 09:19:26 +0530409 if (!ah->curchan)
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400410 return;
411
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530412 aniState = &ah->ani;
Felix Fietkaue49f9132010-10-12 16:08:01 +0200413 if (!ath9k_hw_ani_read_counters(ah))
414 return;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400415
416 ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
417 aniState->listenTime;
418 cckPhyErrRate = aniState->cckPhyErrCount * 1000 /
419 aniState->listenTime;
420
Joe Perchesd2182b62011-12-15 14:55:53 -0800421 ath_dbg(common, ANI,
Joe Perches226afe62010-12-02 19:12:37 -0800422 "listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
423 aniState->listenTime,
424 aniState->ofdmNoiseImmunityLevel,
425 ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
426 cckPhyErrRate, aniState->ofdmsTurn);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400427
Sujith Manoharan55fee982013-06-17 14:24:36 +0530428 if (aniState->listenTime > ah->aniperiod) {
429 if (cckPhyErrRate < ah->config.cck_trig_low &&
430 ofdmPhyErrRate < ah->config.ofdm_trig_low) {
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400431 ath9k_hw_ani_lower_immunity(ah);
432 aniState->ofdmsTurn = !aniState->ofdmsTurn;
Sujith Manoharan55fee982013-06-17 14:24:36 +0530433 } else if (ofdmPhyErrRate > ah->config.ofdm_trig_high) {
Felix Fietkau8eb49802010-10-04 20:09:49 +0200434 ath9k_hw_ani_ofdm_err_trigger(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400435 aniState->ofdmsTurn = false;
Felix Fietkau093115b2010-10-04 20:09:47 +0200436 } else if (cckPhyErrRate > ah->config.cck_trig_high) {
Felix Fietkau8eb49802010-10-04 20:09:49 +0200437 ath9k_hw_ani_cck_err_trigger(ah);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400438 aniState->ofdmsTurn = true;
Sujithf1dc5602008-10-29 10:16:30 +0530439 }
Rajkumar Manoharan54da20d2012-03-15 05:34:26 +0530440 ath9k_ani_restart(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530441 }
442}
Felix Fietkau95792172010-10-04 20:09:50 +0200443EXPORT_SYMBOL(ath9k_hw_ani_monitor);
Sujithf1dc5602008-10-29 10:16:30 +0530444
Sujithcbe61d82009-02-09 13:27:12 +0530445void ath9k_enable_mib_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530446{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700447 struct ath_common *common = ath9k_hw_common(ah);
448
Joe Perchesd2182b62011-12-15 14:55:53 -0800449 ath_dbg(common, ANI, "Enable MIB counters\n");
Sujithf1dc5602008-10-29 10:16:30 +0530450
Sujithcbe61d82009-02-09 13:27:12 +0530451 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujithf1dc5602008-10-29 10:16:30 +0530452
Sujith7d0d0df2010-04-16 11:53:57 +0530453 ENABLE_REGWRITE_BUFFER(ah);
454
Sujithf1dc5602008-10-29 10:16:30 +0530455 REG_WRITE(ah, AR_FILT_OFDM, 0);
456 REG_WRITE(ah, AR_FILT_CCK, 0);
457 REG_WRITE(ah, AR_MIBC,
458 ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
459 & 0x0f);
460 REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
461 REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
Sujith7d0d0df2010-04-16 11:53:57 +0530462
463 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530464}
465
Sujith0fd06c92009-02-12 10:06:51 +0530466/* Freeze the MIB counters, get the stats and then clear them */
Sujithcbe61d82009-02-09 13:27:12 +0530467void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530468{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700469 struct ath_common *common = ath9k_hw_common(ah);
470
Joe Perchesd2182b62011-12-15 14:55:53 -0800471 ath_dbg(common, ANI, "Disable MIB counters\n");
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700472
Sujith0fd06c92009-02-12 10:06:51 +0530473 REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
Sujithcbe61d82009-02-09 13:27:12 +0530474 ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
Sujith0fd06c92009-02-12 10:06:51 +0530475 REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
Sujithf1dc5602008-10-29 10:16:30 +0530476 REG_WRITE(ah, AR_FILT_OFDM, 0);
477 REG_WRITE(ah, AR_FILT_CCK, 0);
478}
Sujith21d51302010-06-01 15:14:18 +0530479EXPORT_SYMBOL(ath9k_hw_disable_mib_counters);
Sujithf1dc5602008-10-29 10:16:30 +0530480
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700481void ath9k_hw_ani_init(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530482{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700483 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530484 struct ar5416AniState *ani = &ah->ani;
Sujithf1dc5602008-10-29 10:16:30 +0530485
Joe Perchesd2182b62011-12-15 14:55:53 -0800486 ath_dbg(common, ANI, "Initialize ANI\n");
Sujithf1dc5602008-10-29 10:16:30 +0530487
Felix Fietkau465dce62012-06-15 15:25:24 +0200488 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH;
489 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW;
Felix Fietkau465dce62012-06-15 15:25:24 +0200490 ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH;
491 ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW;
Felix Fietkau093115b2010-10-04 20:09:47 +0200492
Sujith Manoharanc24bd362013-06-03 09:19:29 +0530493 ani->spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
494 ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
495 ani->mrcCCK = AR_SREV_9300_20_OR_LATER(ah) ? true : false;
496 ani->ofdmsTurn = true;
497 ani->ofdmWeakSigDetect = true;
498 ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
499 ani->ofdmNoiseImmunityLevel = ATH9K_ANI_OFDM_DEF_LEVEL;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400500
501 /*
502 * since we expect some ongoing maintenance on the tables, let's sanity
503 * check here default level should not modify INI setting.
504 */
Felix Fietkau465dce62012-06-15 15:25:24 +0200505 ah->aniperiod = ATH9K_ANI_PERIOD;
506 ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL;
Sujithf1dc5602008-10-29 10:16:30 +0530507
Felix Fietkau093115b2010-10-04 20:09:47 +0200508 ath9k_ani_restart(ah);
509 ath9k_enable_mib_counters(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530510}