iPXE
ath9k_ani.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2011 Atheros Communications Inc.
00003  *
00004  * Modified for iPXE by Scott K Logan <logans@cottsay.net> July 2011
00005  * Original from Linux kernel 3.0.1
00006  *
00007  * Permission to use, copy, modify, and/or distribute this software for any
00008  * purpose with or without fee is hereby granted, provided that the above
00009  * copyright notice and this permission notice appear in all copies.
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00012  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00013  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00014  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00015  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00016  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00017  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00018  */
00019 
00020 #include "hw.h"
00021 #include "hw-ops.h"
00022 
00023 struct ani_ofdm_level_entry {
00024         int spur_immunity_level;
00025         int fir_step_level;
00026         int ofdm_weak_signal_on;
00027 };
00028 
00029 /* values here are relative to the INI */
00030 
00031 /*
00032  * Legend:
00033  *
00034  * SI: Spur immunity
00035  * FS: FIR Step
00036  * WS: OFDM / CCK Weak Signal detection
00037  * MRC-CCK: Maximal Ratio Combining for CCK
00038  */
00039 
00040 static const struct ani_ofdm_level_entry ofdm_level_table[] = {
00041         /* SI  FS  WS */
00042         {  0,  0,  1  }, /* lvl 0 */
00043         {  1,  1,  1  }, /* lvl 1 */
00044         {  2,  2,  1  }, /* lvl 2 */
00045         {  3,  2,  1  }, /* lvl 3  (default) */
00046         {  4,  3,  1  }, /* lvl 4 */
00047         {  5,  4,  1  }, /* lvl 5 */
00048         {  6,  5,  1  }, /* lvl 6 */
00049         {  7,  6,  1  }, /* lvl 7 */
00050         {  7,  7,  1  }, /* lvl 8 */
00051         {  7,  8,  0  }  /* lvl 9 */
00052 };
00053 #define ATH9K_ANI_OFDM_NUM_LEVEL \
00054         ARRAY_SIZE(ofdm_level_table)
00055 #define ATH9K_ANI_OFDM_MAX_LEVEL \
00056         (ATH9K_ANI_OFDM_NUM_LEVEL-1)
00057 #define ATH9K_ANI_OFDM_DEF_LEVEL \
00058         3 /* default level - matches the INI settings */
00059 
00060 /*
00061  * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
00062  * With OFDM for single stream you just add up all antenna inputs, you're
00063  * only interested in what you get after FFT. Signal aligment is also not
00064  * required for OFDM because any phase difference adds up in the frequency
00065  * domain.
00066  *
00067  * MRC requires extra work for use with CCK. You need to align the antenna
00068  * signals from the different antenna before you can add the signals together.
00069  * You need aligment of signals as CCK is in time domain, so addition can cancel
00070  * your signal completely if phase is 180 degrees (think of adding sine waves).
00071  * You also need to remove noise before the addition and this is where ANI
00072  * MRC CCK comes into play. One of the antenna inputs may be stronger but
00073  * lower SNR, so just adding after alignment can be dangerous.
00074  *
00075  * Regardless of alignment in time, the antenna signals add constructively after
00076  * FFT and improve your reception. For more information:
00077  *
00078  * http://en.wikipedia.org/wiki/Maximal-ratio_combining
00079  */
00080 
00081 struct ani_cck_level_entry {
00082         int fir_step_level;
00083         int mrc_cck_on;
00084 };
00085 
00086 static const struct ani_cck_level_entry cck_level_table[] = {
00087         /* FS  MRC-CCK  */
00088         {  0,  1  }, /* lvl 0 */
00089         {  1,  1  }, /* lvl 1 */
00090         {  2,  1  }, /* lvl 2  (default) */
00091         {  3,  1  }, /* lvl 3 */
00092         {  4,  0  }, /* lvl 4 */
00093         {  5,  0  }, /* lvl 5 */
00094         {  6,  0  }, /* lvl 6 */
00095         {  7,  0  }, /* lvl 7 (only for high rssi) */
00096         {  8,  0  }  /* lvl 8 (only for high rssi) */
00097 };
00098 
00099 #define ATH9K_ANI_CCK_NUM_LEVEL \
00100         ARRAY_SIZE(cck_level_table)
00101 #define ATH9K_ANI_CCK_MAX_LEVEL \
00102         (ATH9K_ANI_CCK_NUM_LEVEL-1)
00103 #define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
00104         (ATH9K_ANI_CCK_NUM_LEVEL-3)
00105 #define ATH9K_ANI_CCK_DEF_LEVEL \
00106         2 /* default level - matches the INI settings */
00107 
00108 static int use_new_ani(struct ath_hw *ah)
00109 {
00110         return AR_SREV_9300_20_OR_LATER(ah) || modparam_force_new_ani;
00111 }
00112 
00113 static void ath9k_hw_update_mibstats(struct ath_hw *ah,
00114                                      struct ath9k_mib_stats *stats)
00115 {
00116         stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
00117         stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
00118         stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
00119         stats->rts_good += REG_READ(ah, AR_RTS_OK);
00120         stats->beacons += REG_READ(ah, AR_BEACON_CNT);
00121 }
00122 
00123 static void ath9k_ani_restart(struct ath_hw *ah)
00124 {
00125         struct ar5416AniState *aniState;
00126         u32 ofdm_base = 0, cck_base = 0;
00127 
00128         if (!DO_ANI(ah))
00129                 return;
00130 
00131         aniState = &ah->curchan->ani;
00132         aniState->listenTime = 0;
00133 
00134         if (!use_new_ani(ah)) {
00135                 ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
00136                 cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
00137         }
00138 
00139         DBG2("ath9k: "
00140                 "Writing ofdmbase=%d   cckbase=%d\n", ofdm_base, cck_base);
00141 
00142         ENABLE_REGWRITE_BUFFER(ah);
00143 
00144         REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
00145         REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
00146         REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
00147         REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
00148 
00149         REGWRITE_BUFFER_FLUSH(ah);
00150 
00151         ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
00152 
00153         aniState->ofdmPhyErrCount = 0;
00154         aniState->cckPhyErrCount = 0;
00155 }
00156 
00157 static void ath9k_hw_ani_ofdm_err_trigger_old(struct ath_hw *ah)
00158 {
00159         struct ar5416AniState *aniState;
00160         int32_t rssi;
00161 
00162         aniState = &ah->curchan->ani;
00163 
00164         if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
00165                 if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
00166                                          aniState->noiseImmunityLevel + 1)) {
00167                         return;
00168                 }
00169         }
00170 
00171         if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
00172                 if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
00173                                          aniState->spurImmunityLevel + 1)) {
00174                         return;
00175                 }
00176         }
00177 
00178         rssi = BEACON_RSSI(ah);
00179         if (rssi > aniState->rssiThrHigh) {
00180                 if (aniState->ofdmWeakSigDetect) {
00181                         if (ath9k_hw_ani_control(ah,
00182                                          ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
00183                                          0)) {
00184                                 ath9k_hw_ani_control(ah,
00185                                         ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
00186                                 return;
00187                         }
00188                 }
00189                 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
00190                         ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
00191                                              aniState->firstepLevel + 1);
00192                         return;
00193                 }
00194         } else if (rssi > aniState->rssiThrLow) {
00195                 if (!aniState->ofdmWeakSigDetect)
00196                         ath9k_hw_ani_control(ah,
00197                                      ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
00198                                      1);
00199                 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
00200                         ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
00201                                              aniState->firstepLevel + 1);
00202                 return;
00203         } else {
00204                 if ((ah->dev->channels + ah->dev->channel)->band == NET80211_BAND_2GHZ) {
00205                         if (aniState->ofdmWeakSigDetect)
00206                                 ath9k_hw_ani_control(ah,
00207                                      ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
00208                                      0);
00209                         if (aniState->firstepLevel > 0)
00210                                 ath9k_hw_ani_control(ah,
00211                                              ATH9K_ANI_FIRSTEP_LEVEL, 0);
00212                         return;
00213                 }
00214         }
00215 }
00216 
00217 static void ath9k_hw_ani_cck_err_trigger_old(struct ath_hw *ah)
00218 {
00219         struct ar5416AniState *aniState;
00220         int32_t rssi;
00221 
00222         aniState = &ah->curchan->ani;
00223         if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
00224                 if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
00225                                          aniState->noiseImmunityLevel + 1)) {
00226                         return;
00227                 }
00228         }
00229         rssi = BEACON_RSSI(ah);
00230         if (rssi > aniState->rssiThrLow) {
00231                 if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
00232                         ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
00233                                              aniState->firstepLevel + 1);
00234         } else {
00235                 if ((ah->dev->channels + ah->dev->channel)->band == NET80211_BAND_2GHZ) {
00236                         if (aniState->firstepLevel > 0)
00237                                 ath9k_hw_ani_control(ah,
00238                                              ATH9K_ANI_FIRSTEP_LEVEL, 0);
00239                 }
00240         }
00241 }
00242 
00243 /* Adjust the OFDM Noise Immunity Level */
00244 static void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel)
00245 {
00246         struct ar5416AniState *aniState = &ah->curchan->ani;
00247         const struct ani_ofdm_level_entry *entry_ofdm;
00248         const struct ani_cck_level_entry *entry_cck;
00249 
00250         aniState->noiseFloor = BEACON_RSSI(ah);
00251 
00252         DBG2("ath9k: "
00253                 "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
00254                 aniState->ofdmNoiseImmunityLevel,
00255                 immunityLevel, aniState->noiseFloor,
00256                 aniState->rssiThrLow, aniState->rssiThrHigh);
00257 
00258         aniState->ofdmNoiseImmunityLevel = immunityLevel;
00259 
00260         entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
00261         entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
00262 
00263         if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
00264                 ath9k_hw_ani_control(ah,
00265                                      ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
00266                                      entry_ofdm->spur_immunity_level);
00267 
00268         if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
00269             entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
00270                 ath9k_hw_ani_control(ah,
00271                                      ATH9K_ANI_FIRSTEP_LEVEL,
00272                                      entry_ofdm->fir_step_level);
00273 }
00274 
00275 static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
00276 {
00277         struct ar5416AniState *aniState;
00278 
00279         if (!DO_ANI(ah))
00280                 return;
00281 
00282         if (!use_new_ani(ah)) {
00283                 ath9k_hw_ani_ofdm_err_trigger_old(ah);
00284                 return;
00285         }
00286 
00287         aniState = &ah->curchan->ani;
00288 
00289         if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
00290                 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1);
00291 }
00292 
00293 /*
00294  * Set the ANI settings to match an CCK level.
00295  */
00296 static void ath9k_hw_set_cck_nil(struct ath_hw *ah, uint8_t immunityLevel)
00297 {
00298         struct ar5416AniState *aniState = &ah->curchan->ani;
00299         const struct ani_ofdm_level_entry *entry_ofdm;
00300         const struct ani_cck_level_entry *entry_cck;
00301 
00302         aniState->noiseFloor = BEACON_RSSI(ah);
00303         DBG2("ath9k: "
00304                 "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
00305                 aniState->cckNoiseImmunityLevel, immunityLevel,
00306                 aniState->noiseFloor, aniState->rssiThrLow,
00307                 aniState->rssiThrHigh);
00308 
00309         if (aniState->noiseFloor <= (unsigned int)aniState->rssiThrLow &&
00310             immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
00311                 immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
00312 
00313         aniState->cckNoiseImmunityLevel = immunityLevel;
00314 
00315         entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
00316         entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
00317 
00318         if (aniState->firstepLevel != entry_cck->fir_step_level &&
00319             entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
00320                 ath9k_hw_ani_control(ah,
00321                                      ATH9K_ANI_FIRSTEP_LEVEL,
00322                                      entry_cck->fir_step_level);
00323 
00324         /* Skip MRC CCK for pre AR9003 families */
00325         if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah))
00326                 return;
00327 
00328         if (aniState->mrcCCKOff == entry_cck->mrc_cck_on)
00329                 ath9k_hw_ani_control(ah,
00330                                      ATH9K_ANI_MRC_CCK,
00331                                      entry_cck->mrc_cck_on);
00332 }
00333 
00334 static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
00335 {
00336         struct ar5416AniState *aniState;
00337 
00338         if (!DO_ANI(ah))
00339                 return;
00340 
00341         if (!use_new_ani(ah)) {
00342                 ath9k_hw_ani_cck_err_trigger_old(ah);
00343                 return;
00344         }
00345 
00346         aniState = &ah->curchan->ani;
00347 
00348         if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
00349                 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1);
00350 }
00351 
00352 static void ath9k_hw_ani_lower_immunity_old(struct ath_hw *ah)
00353 {
00354         struct ar5416AniState *aniState;
00355         int32_t rssi;
00356 
00357         aniState = &ah->curchan->ani;
00358 
00359         rssi = BEACON_RSSI(ah);
00360         if (rssi > aniState->rssiThrHigh) {
00361                 /* XXX: Handle me */
00362         } else if (rssi > aniState->rssiThrLow) {
00363                 if (!aniState->ofdmWeakSigDetect) {
00364                         if (ath9k_hw_ani_control(ah,
00365                                  ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
00366                                  1) == 1)
00367                                 return;
00368                 }
00369                 if (aniState->firstepLevel > 0) {
00370                         if (ath9k_hw_ani_control(ah,
00371                                  ATH9K_ANI_FIRSTEP_LEVEL,
00372                                  aniState->firstepLevel - 1) == 1)
00373                                 return;
00374                 }
00375         } else {
00376                 if (aniState->firstepLevel > 0) {
00377                         if (ath9k_hw_ani_control(ah,
00378                                  ATH9K_ANI_FIRSTEP_LEVEL,
00379                                  aniState->firstepLevel - 1) == 1)
00380                                 return;
00381                 }
00382         }
00383 
00384         if (aniState->spurImmunityLevel > 0) {
00385                 if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
00386                                          aniState->spurImmunityLevel - 1))
00387                         return;
00388         }
00389 
00390         if (aniState->noiseImmunityLevel > 0) {
00391                 ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
00392                                      aniState->noiseImmunityLevel - 1);
00393                 return;
00394         }
00395 }
00396 
00397 /*
00398  * only lower either OFDM or CCK errors per turn
00399  * we lower the other one next time
00400  */
00401 static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
00402 {
00403         struct ar5416AniState *aniState;
00404 
00405         aniState = &ah->curchan->ani;
00406 
00407         if (!use_new_ani(ah)) {
00408                 ath9k_hw_ani_lower_immunity_old(ah);
00409                 return;
00410         }
00411 
00412         /* lower OFDM noise immunity */
00413         if (aniState->ofdmNoiseImmunityLevel > 0 &&
00414             (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
00415                 ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1);
00416                 return;
00417         }
00418 
00419         /* lower CCK noise immunity */
00420         if (aniState->cckNoiseImmunityLevel > 0)
00421                 ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1);
00422 }
00423 
00424 static void ath9k_ani_reset_old(struct ath_hw *ah)
00425 {
00426         struct ar5416AniState *aniState;
00427 
00428         if (!DO_ANI(ah))
00429                 return;
00430 
00431         aniState = &ah->curchan->ani;
00432 
00433         if (aniState->noiseImmunityLevel != 0)
00434                 ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
00435                                      aniState->noiseImmunityLevel);
00436         if (aniState->spurImmunityLevel != 0)
00437                 ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
00438                                      aniState->spurImmunityLevel);
00439         if (!aniState->ofdmWeakSigDetect)
00440                 ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
00441                                      aniState->ofdmWeakSigDetect);
00442         if (aniState->cckWeakSigThreshold)
00443                 ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
00444                                      aniState->cckWeakSigThreshold);
00445         if (aniState->firstepLevel != 0)
00446                 ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
00447                                      aniState->firstepLevel);
00448 
00449         ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
00450                              ~ATH9K_RX_FILTER_PHYERR);
00451         ath9k_ani_restart(ah);
00452 
00453         ENABLE_REGWRITE_BUFFER(ah);
00454 
00455         REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
00456         REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
00457 
00458         REGWRITE_BUFFER_FLUSH(ah);
00459 }
00460 
00461 /*
00462  * Restore the ANI parameters in the HAL and reset the statistics.
00463  * This routine should be called for every hardware reset and for
00464  * every channel change.
00465  */
00466 void ath9k_ani_reset(struct ath_hw *ah, int is_scanning)
00467 {
00468         struct ar5416AniState *aniState = &ah->curchan->ani;
00469         struct ath9k_channel *chan = ah->curchan;
00470 
00471         if (!DO_ANI(ah))
00472                 return;
00473 
00474         if (!use_new_ani(ah))
00475                 return ath9k_ani_reset_old(ah);
00476 
00477         ah->stats.ast_ani_reset++;
00478 
00479         /* always allow mode (on/off) to be controlled */
00480         ah->ani_function |= ATH9K_ANI_MODE;
00481 
00482         if (is_scanning) {
00483                 /*
00484                  * If we're scanning or in AP mode, the defaults (ini)
00485                  * should be in place. For an AP we assume the historical
00486                  * levels for this channel are probably outdated so start
00487                  * from defaults instead.
00488                  */
00489                 if (aniState->ofdmNoiseImmunityLevel !=
00490                     ATH9K_ANI_OFDM_DEF_LEVEL ||
00491                     aniState->cckNoiseImmunityLevel !=
00492                     ATH9K_ANI_CCK_DEF_LEVEL) {
00493                         DBG("ath9k: "
00494                                 "Restore defaults: chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
00495                                 chan->channel,
00496                                 chan->channelFlags,
00497                                 is_scanning,
00498                                 aniState->ofdmNoiseImmunityLevel,
00499                                 aniState->cckNoiseImmunityLevel);
00500 
00501                         ath9k_hw_set_ofdm_nil(ah, ATH9K_ANI_OFDM_DEF_LEVEL);
00502                         ath9k_hw_set_cck_nil(ah, ATH9K_ANI_CCK_DEF_LEVEL);
00503                 }
00504         } else {
00505                 /*
00506                  * restore historical levels for this channel
00507                  */
00508                 DBG2("ath9k: "
00509                         "Restore history: chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
00510                         chan->channel,
00511                         chan->channelFlags,
00512                         is_scanning,
00513                         aniState->ofdmNoiseImmunityLevel,
00514                         aniState->cckNoiseImmunityLevel);
00515 
00516                         ath9k_hw_set_ofdm_nil(ah,
00517                                               aniState->ofdmNoiseImmunityLevel);
00518                         ath9k_hw_set_cck_nil(ah,
00519                                              aniState->cckNoiseImmunityLevel);
00520         }
00521 
00522         /*
00523          * enable phy counters if hw supports or if not, enable phy
00524          * interrupts (so we can count each one)
00525          */
00526         ath9k_ani_restart(ah);
00527 
00528         ENABLE_REGWRITE_BUFFER(ah);
00529 
00530         REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
00531         REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
00532 
00533         REGWRITE_BUFFER_FLUSH(ah);
00534 }
00535 
00536 static int ath9k_hw_ani_read_counters(struct ath_hw *ah)
00537 {
00538         struct ath_common *common = ath9k_hw_common(ah);
00539         struct ar5416AniState *aniState = &ah->curchan->ani;
00540         u32 ofdm_base = 0;
00541         u32 cck_base = 0;
00542         u32 ofdmPhyErrCnt, cckPhyErrCnt;
00543         u32 phyCnt1, phyCnt2;
00544         int32_t listenTime;
00545 
00546         ath_hw_cycle_counters_update(common);
00547         listenTime = ath_hw_get_listen_time(common);
00548 
00549         if (listenTime <= 0) {
00550                 ah->stats.ast_ani_lneg++;
00551                 ath9k_ani_restart(ah);
00552                 return 0;
00553         }
00554 
00555         if (!use_new_ani(ah)) {
00556                 ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
00557                 cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
00558         }
00559 
00560         aniState->listenTime += listenTime;
00561 
00562         phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
00563         phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
00564 
00565         if (!use_new_ani(ah) && (phyCnt1 < ofdm_base || phyCnt2 < cck_base)) {
00566                 if (phyCnt1 < ofdm_base) {
00567                         DBG2("ath9k: "
00568                                 "phyCnt1 0x%x, resetting counter value to 0x%x\n",
00569                                 phyCnt1, ofdm_base);
00570                         REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
00571                         REG_WRITE(ah, AR_PHY_ERR_MASK_1,
00572                                   AR_PHY_ERR_OFDM_TIMING);
00573                 }
00574                 if (phyCnt2 < cck_base) {
00575                         DBG2("ath9k: "
00576                                 "phyCnt2 0x%x, resetting counter value to 0x%x\n",
00577                                 phyCnt2, cck_base);
00578                         REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
00579                         REG_WRITE(ah, AR_PHY_ERR_MASK_2,
00580                                   AR_PHY_ERR_CCK_TIMING);
00581                 }
00582                 return 0;
00583         }
00584 
00585         ofdmPhyErrCnt = phyCnt1 - ofdm_base;
00586         ah->stats.ast_ani_ofdmerrs +=
00587                 ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
00588         aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
00589 
00590         cckPhyErrCnt = phyCnt2 - cck_base;
00591         ah->stats.ast_ani_cckerrs +=
00592                 cckPhyErrCnt - aniState->cckPhyErrCount;
00593         aniState->cckPhyErrCount = cckPhyErrCnt;
00594         return 1;
00595 }
00596 
00597 void ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan __unused)
00598 {
00599         struct ar5416AniState *aniState;
00600         u32 ofdmPhyErrRate, cckPhyErrRate;
00601 
00602         if (!DO_ANI(ah))
00603                 return;
00604 
00605         aniState = &ah->curchan->ani;
00606         if (!aniState)
00607                 return;
00608 
00609         if (!ath9k_hw_ani_read_counters(ah))
00610                 return;
00611 
00612         ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
00613                          aniState->listenTime;
00614         cckPhyErrRate =  aniState->cckPhyErrCount * 1000 /
00615                          aniState->listenTime;
00616 
00617         DBG2("ath9k: "
00618                 "listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
00619                 aniState->listenTime,
00620                 aniState->ofdmNoiseImmunityLevel,
00621                 ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
00622                 cckPhyErrRate, aniState->ofdmsTurn);
00623 
00624         if (aniState->listenTime > 5 * ah->aniperiod) {
00625                 if (ofdmPhyErrRate <= ah->config.ofdm_trig_low &&
00626                     cckPhyErrRate <= ah->config.cck_trig_low) {
00627                         ath9k_hw_ani_lower_immunity(ah);
00628                         aniState->ofdmsTurn = !aniState->ofdmsTurn;
00629                 }
00630                 ath9k_ani_restart(ah);
00631         } else if (aniState->listenTime > ah->aniperiod) {
00632                 /* check to see if need to raise immunity */
00633                 if (ofdmPhyErrRate > ah->config.ofdm_trig_high &&
00634                     (cckPhyErrRate <= ah->config.cck_trig_high ||
00635                      aniState->ofdmsTurn)) {
00636                         ath9k_hw_ani_ofdm_err_trigger(ah);
00637                         ath9k_ani_restart(ah);
00638                         aniState->ofdmsTurn = 0;
00639                 } else if (cckPhyErrRate > ah->config.cck_trig_high) {
00640                         ath9k_hw_ani_cck_err_trigger(ah);
00641                         ath9k_ani_restart(ah);
00642                         aniState->ofdmsTurn = 1;
00643                 }
00644         }
00645 }
00646 
00647 void ath9k_hw_ani_setup(struct ath_hw *ah)
00648 {
00649         int i;
00650 
00651         static const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
00652         static const int coarseHigh[] = { -14, -14, -14, -14, -12 };
00653         static const int coarseLow[] = { -64, -64, -64, -64, -70 };
00654         static const int firpwr[] = { -78, -78, -78, -78, -80 };
00655 
00656         for (i = 0; i < 5; i++) {
00657                 ah->totalSizeDesired[i] = totalSizeDesired[i];
00658                 ah->coarse_high[i] = coarseHigh[i];
00659                 ah->coarse_low[i] = coarseLow[i];
00660                 ah->firpwr[i] = firpwr[i];
00661         }
00662 }
00663 
00664 void ath9k_hw_ani_init(struct ath_hw *ah)
00665 {
00666         unsigned int i;
00667 
00668         DBG2("ath9k: Initialize ANI\n");
00669 
00670         if (use_new_ani(ah)) {
00671                 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_NEW;
00672                 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_NEW;
00673 
00674                 ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_NEW;
00675                 ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_NEW;
00676         } else {
00677                 ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
00678                 ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
00679 
00680                 ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
00681                 ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
00682         }
00683 
00684         for (i = 0; i < ARRAY_SIZE(ah->channels); i++) {
00685                 struct ath9k_channel *chan = &ah->channels[i];
00686                 struct ar5416AniState *ani = &chan->ani;
00687 
00688                 if (use_new_ani(ah)) {
00689                         ani->spurImmunityLevel =
00690                                 ATH9K_ANI_SPUR_IMMUNE_LVL_NEW;
00691 
00692                         ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_NEW;
00693 
00694                         if (AR_SREV_9300_20_OR_LATER(ah))
00695                                 ani->mrcCCKOff =
00696                                         !ATH9K_ANI_ENABLE_MRC_CCK;
00697                         else
00698                                 ani->mrcCCKOff = 1;
00699 
00700                         ani->ofdmsTurn = 1;
00701                 } else {
00702                         ani->spurImmunityLevel =
00703                                 ATH9K_ANI_SPUR_IMMUNE_LVL_OLD;
00704                         ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_OLD;
00705 
00706                         ani->cckWeakSigThreshold =
00707                                 ATH9K_ANI_CCK_WEAK_SIG_THR;
00708                 }
00709 
00710                 ani->rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
00711                 ani->rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
00712                 ani->ofdmWeakSigDetect =
00713                         ATH9K_ANI_USE_OFDM_WEAK_SIG;
00714                 ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
00715         }
00716 
00717         /*
00718          * since we expect some ongoing maintenance on the tables, let's sanity
00719          * check here default level should not modify INI setting.
00720          */
00721         if (use_new_ani(ah)) {
00722                 ah->aniperiod = ATH9K_ANI_PERIOD_NEW;
00723                 ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_NEW;
00724         } else {
00725                 ah->aniperiod = ATH9K_ANI_PERIOD_OLD;
00726                 ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_OLD;
00727         }
00728 
00729         if (ah->config.enable_ani)
00730                 ah->proc_phyerr |= HAL_PROCESS_ANI;
00731 
00732         ath9k_ani_restart(ah);
00733 }