Path: blob/main/sys/dev/ath/ath_hal/ar5212/ar5212_ani.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2002-2009 Sam Leffler, Errno Consulting4* Copyright (c) 2002-2008 Atheros Communications, Inc.5*6* Permission to use, copy, modify, and/or distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/18#include "opt_ah.h"1920#include "ah.h"21#include "ah_internal.h"22#include "ah_desc.h"2324#include "ar5212/ar5212.h"25#include "ar5212/ar5212reg.h"26#include "ar5212/ar5212phy.h"2728/*29* Anti noise immunity support. We track phy errors and react30* to excessive errors by adjusting the noise immunity parameters.31*/3233#define HAL_EP_RND(x, mul) \34((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))35#define BEACON_RSSI(ahp) \36HAL_EP_RND(ahp->ah_stats.ast_nodestats.ns_avgbrssi, \37HAL_RSSI_EP_MULTIPLIER)3839/*40* ANI processing tunes radio parameters according to PHY errors41* and related information. This is done for noise and spur42* immunity in all operating modes if the device indicates it's43* capable at attach time. In addition, when there is a reference44* rssi value (e.g. beacon frames from an ap in station mode)45* further tuning is done.46*47* ANI_ENA indicates whether any ANI processing should be done;48* this is specified at attach time.49*50* ANI_ENA_RSSI indicates whether rssi-based processing should51* done, this is enabled based on operating mode and is meaningful52* only if ANI_ENA is true.53*54* ANI parameters are typically controlled only by the hal. The55* AniControl interface however permits manual tuning through the56* diagnostic api.57*/58#define ANI_ENA(ah) \59(AH5212(ah)->ah_procPhyErr & HAL_ANI_ENA)60#define ANI_ENA_RSSI(ah) \61(AH5212(ah)->ah_procPhyErr & HAL_RSSI_ANI_ENA)6263#define ah_mibStats ah_stats.ast_mibstats6465static void66enableAniMIBCounters(struct ath_hal *ah, const struct ar5212AniParams *params)67{68struct ath_hal_5212 *ahp = AH5212(ah);6970HALDEBUG(ah, HAL_DEBUG_ANI, "%s: Enable mib counters: "71"OfdmPhyErrBase 0x%x cckPhyErrBase 0x%x\n",72__func__, params->ofdmPhyErrBase, params->cckPhyErrBase);7374OS_REG_WRITE(ah, AR_FILTOFDM, 0);75OS_REG_WRITE(ah, AR_FILTCCK, 0);7677OS_REG_WRITE(ah, AR_PHYCNT1, params->ofdmPhyErrBase);78OS_REG_WRITE(ah, AR_PHYCNT2, params->cckPhyErrBase);79OS_REG_WRITE(ah, AR_PHYCNTMASK1, AR_PHY_ERR_OFDM_TIMING);80OS_REG_WRITE(ah, AR_PHYCNTMASK2, AR_PHY_ERR_CCK_TIMING);8182ar5212UpdateMibCounters(ah, &ahp->ah_mibStats); /* save+clear counters*/83ar5212EnableMibCounters(ah); /* enable everything */84}8586static void87disableAniMIBCounters(struct ath_hal *ah)88{89struct ath_hal_5212 *ahp = AH5212(ah);9091HALDEBUG(ah, HAL_DEBUG_ANI, "Disable MIB counters\n");9293ar5212UpdateMibCounters(ah, &ahp->ah_mibStats); /* save stats */94ar5212DisableMibCounters(ah); /* disable everything */9596OS_REG_WRITE(ah, AR_PHYCNTMASK1, 0);97OS_REG_WRITE(ah, AR_PHYCNTMASK2, 0);98}99100/*101* Return the current ANI state of the channel we're on102*/103struct ar5212AniState *104ar5212AniGetCurrentState(struct ath_hal *ah)105{106return AH5212(ah)->ah_curani;107}108109/*110* Return the current statistics.111*/112HAL_ANI_STATS *113ar5212AniGetCurrentStats(struct ath_hal *ah)114{115struct ath_hal_5212 *ahp = AH5212(ah);116117/* update mib stats so we return current data */118/* XXX? side-effects to doing this here? */119ar5212UpdateMibCounters(ah, &ahp->ah_mibStats);120return &ahp->ah_stats;121}122123static void124setPhyErrBase(struct ath_hal *ah, struct ar5212AniParams *params)125{126if (params->ofdmTrigHigh >= AR_PHY_COUNTMAX) {127HALDEBUG(ah, HAL_DEBUG_ANY,128"OFDM Trigger %d is too high for hw counters, using max\n",129params->ofdmTrigHigh);130params->ofdmPhyErrBase = 0;131} else132params->ofdmPhyErrBase = AR_PHY_COUNTMAX - params->ofdmTrigHigh;133if (params->cckTrigHigh >= AR_PHY_COUNTMAX) {134HALDEBUG(ah, HAL_DEBUG_ANY,135"CCK Trigger %d is too high for hw counters, using max\n",136params->cckTrigHigh);137params->cckPhyErrBase = 0;138} else139params->cckPhyErrBase = AR_PHY_COUNTMAX - params->cckTrigHigh;140}141142/*143* Setup ANI handling. Sets all thresholds and reset the144* channel statistics. Note that ar5212AniReset should be145* called by ar5212Reset before anything else happens and146* that's where we force initial settings.147*/148void149ar5212AniAttach(struct ath_hal *ah, const struct ar5212AniParams *params24,150const struct ar5212AniParams *params5, HAL_BOOL enable)151{152struct ath_hal_5212 *ahp = AH5212(ah);153154ahp->ah_hasHwPhyCounters =155AH_PRIVATE(ah)->ah_caps.halHwPhyCounterSupport;156157if (params24 != AH_NULL) {158OS_MEMCPY(&ahp->ah_aniParams24, params24, sizeof(*params24));159setPhyErrBase(ah, &ahp->ah_aniParams24);160}161if (params5 != AH_NULL) {162OS_MEMCPY(&ahp->ah_aniParams5, params5, sizeof(*params5));163setPhyErrBase(ah, &ahp->ah_aniParams5);164}165166OS_MEMZERO(ahp->ah_ani, sizeof(ahp->ah_ani));167if (ahp->ah_hasHwPhyCounters) {168/* Enable MIB Counters */169enableAniMIBCounters(ah, &ahp->ah_aniParams24 /*XXX*/);170}171if (enable) { /* Enable ani now */172HALASSERT(params24 != AH_NULL && params5 != AH_NULL);173ahp->ah_procPhyErr |= HAL_ANI_ENA;174} else {175ahp->ah_procPhyErr &= ~HAL_ANI_ENA;176}177}178179HAL_BOOL180ar5212AniSetParams(struct ath_hal *ah, const struct ar5212AniParams *params24,181const struct ar5212AniParams *params5)182{183struct ath_hal_5212 *ahp = AH5212(ah);184HAL_BOOL ena = (ahp->ah_procPhyErr & HAL_ANI_ENA) != 0;185186ar5212AniControl(ah, HAL_ANI_MODE, AH_FALSE);187188OS_MEMCPY(&ahp->ah_aniParams24, params24, sizeof(*params24));189setPhyErrBase(ah, &ahp->ah_aniParams24);190OS_MEMCPY(&ahp->ah_aniParams5, params5, sizeof(*params5));191setPhyErrBase(ah, &ahp->ah_aniParams5);192193OS_MEMZERO(ahp->ah_ani, sizeof(ahp->ah_ani));194ar5212AniReset(ah, AH_PRIVATE(ah)->ah_curchan,195AH_PRIVATE(ah)->ah_opmode, AH_FALSE);196197ar5212AniControl(ah, HAL_ANI_MODE, ena);198199return AH_TRUE;200}201202/*203* Cleanup any ANI state setup.204*/205void206ar5212AniDetach(struct ath_hal *ah)207{208struct ath_hal_5212 *ahp = AH5212(ah);209210HALDEBUG(ah, HAL_DEBUG_ANI, "Detaching Ani\n");211if (ahp->ah_hasHwPhyCounters)212disableAniMIBCounters(ah);213}214215/*216* Control Adaptive Noise Immunity Parameters217*/218HAL_BOOL219ar5212AniControl(struct ath_hal *ah, HAL_ANI_CMD cmd, int param)220{221typedef int TABLE[];222struct ath_hal_5212 *ahp = AH5212(ah);223struct ar5212AniState *aniState = ahp->ah_curani;224const struct ar5212AniParams *params = AH_NULL;225226/*227* This function may be called before there's a current228* channel (eg to disable ANI.)229*/230if (aniState != AH_NULL)231params = aniState->params;232233OS_MARK(ah, AH_MARK_ANI_CONTROL, cmd);234235switch (cmd) {236case HAL_ANI_NOISE_IMMUNITY_LEVEL: {237u_int level = param;238239if (level > params->maxNoiseImmunityLevel) {240HALDEBUG(ah, HAL_DEBUG_ANY,241"%s: level out of range (%u > %u)\n",242__func__, level, params->maxNoiseImmunityLevel);243return AH_FALSE;244}245246OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,247AR_PHY_DESIRED_SZ_TOT_DES, params->totalSizeDesired[level]);248OS_REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,249AR_PHY_AGC_CTL1_COARSE_LOW, params->coarseLow[level]);250OS_REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,251AR_PHY_AGC_CTL1_COARSE_HIGH, params->coarseHigh[level]);252OS_REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,253AR_PHY_FIND_SIG_FIRPWR, params->firpwr[level]);254255if (level > aniState->noiseImmunityLevel)256ahp->ah_stats.ast_ani_niup++;257else if (level < aniState->noiseImmunityLevel)258ahp->ah_stats.ast_ani_nidown++;259aniState->noiseImmunityLevel = level;260break;261}262case HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION: {263static const TABLE m1ThreshLow = { 127, 50 };264static const TABLE m2ThreshLow = { 127, 40 };265static const TABLE m1Thresh = { 127, 0x4d };266static const TABLE m2Thresh = { 127, 0x40 };267static const TABLE m2CountThr = { 31, 16 };268static const TABLE m2CountThrLow = { 63, 48 };269u_int on = param ? 1 : 0;270271OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,272AR_PHY_SFCORR_LOW_M1_THRESH_LOW, m1ThreshLow[on]);273OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,274AR_PHY_SFCORR_LOW_M2_THRESH_LOW, m2ThreshLow[on]);275OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR,276AR_PHY_SFCORR_M1_THRESH, m1Thresh[on]);277OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR,278AR_PHY_SFCORR_M2_THRESH, m2Thresh[on]);279OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR,280AR_PHY_SFCORR_M2COUNT_THR, m2CountThr[on]);281OS_REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,282AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, m2CountThrLow[on]);283284if (on) {285OS_REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,286AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);287ahp->ah_stats.ast_ani_ofdmon++;288} else {289OS_REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,290AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);291ahp->ah_stats.ast_ani_ofdmoff++;292}293aniState->ofdmWeakSigDetectOff = !on;294break;295}296case HAL_ANI_CCK_WEAK_SIGNAL_THR: {297static const TABLE weakSigThrCck = { 8, 6 };298u_int high = param ? 1 : 0;299300OS_REG_RMW_FIELD(ah, AR_PHY_CCK_DETECT,301AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, weakSigThrCck[high]);302if (high)303ahp->ah_stats.ast_ani_cckhigh++;304else305ahp->ah_stats.ast_ani_ccklow++;306aniState->cckWeakSigThreshold = high;307break;308}309case HAL_ANI_FIRSTEP_LEVEL: {310u_int level = param;311312if (level > params->maxFirstepLevel) {313HALDEBUG(ah, HAL_DEBUG_ANY,314"%s: level out of range (%u > %u)\n",315__func__, level, params->maxFirstepLevel);316return AH_FALSE;317}318OS_REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,319AR_PHY_FIND_SIG_FIRSTEP, params->firstep[level]);320if (level > aniState->firstepLevel)321ahp->ah_stats.ast_ani_stepup++;322else if (level < aniState->firstepLevel)323ahp->ah_stats.ast_ani_stepdown++;324aniState->firstepLevel = level;325break;326}327case HAL_ANI_SPUR_IMMUNITY_LEVEL: {328u_int level = param;329330if (level > params->maxSpurImmunityLevel) {331HALDEBUG(ah, HAL_DEBUG_ANY,332"%s: level out of range (%u > %u)\n",333__func__, level, params->maxSpurImmunityLevel);334return AH_FALSE;335}336OS_REG_RMW_FIELD(ah, AR_PHY_TIMING5,337AR_PHY_TIMING5_CYCPWR_THR1, params->cycPwrThr1[level]);338if (level > aniState->spurImmunityLevel)339ahp->ah_stats.ast_ani_spurup++;340else if (level < aniState->spurImmunityLevel)341ahp->ah_stats.ast_ani_spurdown++;342aniState->spurImmunityLevel = level;343break;344}345case HAL_ANI_PRESENT:346break;347case HAL_ANI_MODE:348if (param == 0) {349ahp->ah_procPhyErr &= ~HAL_ANI_ENA;350/* Turn off HW counters if we have them */351ar5212AniDetach(ah);352ah->ah_setRxFilter(ah,353ah->ah_getRxFilter(ah) &~ HAL_RX_FILTER_PHYERR);354} else { /* normal/auto mode */355/* don't mess with state if already enabled */356if (ahp->ah_procPhyErr & HAL_ANI_ENA)357break;358if (ahp->ah_hasHwPhyCounters) {359ar5212SetRxFilter(ah,360ar5212GetRxFilter(ah) &~ HAL_RX_FILTER_PHYERR);361/* Enable MIB Counters */362enableAniMIBCounters(ah,363ahp->ah_curani != AH_NULL ?364ahp->ah_curani->params:365&ahp->ah_aniParams24 /*XXX*/);366} else {367ah->ah_setRxFilter(ah,368ah->ah_getRxFilter(ah) | HAL_RX_FILTER_PHYERR);369}370ahp->ah_procPhyErr |= HAL_ANI_ENA;371}372break;373#ifdef AH_PRIVATE_DIAG374case HAL_ANI_PHYERR_RESET:375ahp->ah_stats.ast_ani_ofdmerrs = 0;376ahp->ah_stats.ast_ani_cckerrs = 0;377break;378#endif /* AH_PRIVATE_DIAG */379default:380HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid cmd %u\n",381__func__, cmd);382return AH_FALSE;383}384return AH_TRUE;385}386387static void388ar5212AniOfdmErrTrigger(struct ath_hal *ah)389{390struct ath_hal_5212 *ahp = AH5212(ah);391const struct ieee80211_channel *chan = AH_PRIVATE(ah)->ah_curchan;392struct ar5212AniState *aniState;393const struct ar5212AniParams *params;394395HALASSERT(chan != AH_NULL);396397if (!ANI_ENA(ah))398return;399400aniState = ahp->ah_curani;401params = aniState->params;402/* First, raise noise immunity level, up to max */403if (aniState->noiseImmunityLevel+1 <= params->maxNoiseImmunityLevel) {404HALDEBUG(ah, HAL_DEBUG_ANI, "%s: raise NI to %u\n", __func__,405aniState->noiseImmunityLevel + 1);406ar5212AniControl(ah, HAL_ANI_NOISE_IMMUNITY_LEVEL,407aniState->noiseImmunityLevel + 1);408return;409}410/* then, raise spur immunity level, up to max */411if (aniState->spurImmunityLevel+1 <= params->maxSpurImmunityLevel) {412HALDEBUG(ah, HAL_DEBUG_ANI, "%s: raise SI to %u\n", __func__,413aniState->spurImmunityLevel + 1);414ar5212AniControl(ah, HAL_ANI_SPUR_IMMUNITY_LEVEL,415aniState->spurImmunityLevel + 1);416return;417}418419if (ANI_ENA_RSSI(ah)) {420int32_t rssi = BEACON_RSSI(ahp);421if (rssi > params->rssiThrHigh) {422/*423* Beacon rssi is high, can turn off ofdm424* weak sig detect.425*/426if (!aniState->ofdmWeakSigDetectOff) {427HALDEBUG(ah, HAL_DEBUG_ANI,428"%s: rssi %d OWSD off\n", __func__, rssi);429ar5212AniControl(ah,430HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,431AH_FALSE);432ar5212AniControl(ah,433HAL_ANI_SPUR_IMMUNITY_LEVEL, 0);434return;435}436/*437* If weak sig detect is already off, as last resort,438* raise firstep level439*/440if (aniState->firstepLevel+1 <= params->maxFirstepLevel) {441HALDEBUG(ah, HAL_DEBUG_ANI,442"%s: rssi %d raise ST %u\n", __func__, rssi,443aniState->firstepLevel+1);444ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,445aniState->firstepLevel + 1);446return;447}448} else if (rssi > params->rssiThrLow) {449/*450* Beacon rssi in mid range, need ofdm weak signal451* detect, but we can raise firststepLevel.452*/453if (aniState->ofdmWeakSigDetectOff) {454HALDEBUG(ah, HAL_DEBUG_ANI,455"%s: rssi %d OWSD on\n", __func__, rssi);456ar5212AniControl(ah,457HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,458AH_TRUE);459}460if (aniState->firstepLevel+1 <= params->maxFirstepLevel) {461HALDEBUG(ah, HAL_DEBUG_ANI,462"%s: rssi %d raise ST %u\n", __func__, rssi,463aniState->firstepLevel+1);464ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,465aniState->firstepLevel + 1);466}467return;468} else {469/*470* Beacon rssi is low, if in 11b/g mode, turn off ofdm471* weak signal detection and zero firstepLevel to472* maximize CCK sensitivity473*/474if (IEEE80211_IS_CHAN_CCK(chan)) {475if (!aniState->ofdmWeakSigDetectOff) {476HALDEBUG(ah, HAL_DEBUG_ANI,477"%s: rssi %d OWSD off\n",478__func__, rssi);479ar5212AniControl(ah,480HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,481AH_FALSE);482}483if (aniState->firstepLevel > 0) {484HALDEBUG(ah, HAL_DEBUG_ANI,485"%s: rssi %d zero ST (was %u)\n",486__func__, rssi,487aniState->firstepLevel);488ar5212AniControl(ah,489HAL_ANI_FIRSTEP_LEVEL, 0);490}491return;492}493}494}495}496497static void498ar5212AniCckErrTrigger(struct ath_hal *ah)499{500struct ath_hal_5212 *ahp = AH5212(ah);501const struct ieee80211_channel *chan = AH_PRIVATE(ah)->ah_curchan;502struct ar5212AniState *aniState;503const struct ar5212AniParams *params;504505HALASSERT(chan != AH_NULL);506507if (!ANI_ENA(ah))508return;509510/* first, raise noise immunity level, up to max */511aniState = ahp->ah_curani;512params = aniState->params;513if (aniState->noiseImmunityLevel+1 <= params->maxNoiseImmunityLevel) {514HALDEBUG(ah, HAL_DEBUG_ANI, "%s: raise NI to %u\n", __func__,515aniState->noiseImmunityLevel + 1);516ar5212AniControl(ah, HAL_ANI_NOISE_IMMUNITY_LEVEL,517aniState->noiseImmunityLevel + 1);518return;519}520521if (ANI_ENA_RSSI(ah)) {522int32_t rssi = BEACON_RSSI(ahp);523if (rssi > params->rssiThrLow) {524/*525* Beacon signal in mid and high range,526* raise firstep level.527*/528if (aniState->firstepLevel+1 <= params->maxFirstepLevel) {529HALDEBUG(ah, HAL_DEBUG_ANI,530"%s: rssi %d raise ST %u\n", __func__, rssi,531aniState->firstepLevel+1);532ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,533aniState->firstepLevel + 1);534}535} else {536/*537* Beacon rssi is low, zero firstep level to maximize538* CCK sensitivity in 11b/g mode.539*/540/* XXX can optimize */541if (IEEE80211_IS_CHAN_B(chan) ||542IEEE80211_IS_CHAN_G(chan)) {543if (aniState->firstepLevel > 0) {544HALDEBUG(ah, HAL_DEBUG_ANI,545"%s: rssi %d zero ST (was %u)\n",546__func__, rssi,547aniState->firstepLevel);548ar5212AniControl(ah,549HAL_ANI_FIRSTEP_LEVEL, 0);550}551}552}553}554}555556static void557ar5212AniRestart(struct ath_hal *ah, struct ar5212AniState *aniState)558{559struct ath_hal_5212 *ahp = AH5212(ah);560561aniState->listenTime = 0;562if (ahp->ah_hasHwPhyCounters) {563const struct ar5212AniParams *params = aniState->params;564/*565* NB: these are written on reset based on the566* ini so we must re-write them!567*/568OS_REG_WRITE(ah, AR_PHYCNT1, params->ofdmPhyErrBase);569OS_REG_WRITE(ah, AR_PHYCNT2, params->cckPhyErrBase);570OS_REG_WRITE(ah, AR_PHYCNTMASK1, AR_PHY_ERR_OFDM_TIMING);571OS_REG_WRITE(ah, AR_PHYCNTMASK2, AR_PHY_ERR_CCK_TIMING);572573/* Clear the mib counters and save them in the stats */574ar5212UpdateMibCounters(ah, &ahp->ah_mibStats);575}576aniState->ofdmPhyErrCount = 0;577aniState->cckPhyErrCount = 0;578}579580/*581* Restore/reset the ANI parameters and reset the statistics.582* This routine must be called for every channel change.583*/584void585ar5212AniReset(struct ath_hal *ah, const struct ieee80211_channel *chan,586HAL_OPMODE opmode, int restore)587{588struct ath_hal_5212 *ahp = AH5212(ah);589HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);590/* XXX bounds check ic_devdata */591struct ar5212AniState *aniState = &ahp->ah_ani[chan->ic_devdata];592uint32_t rxfilter;593594if ((ichan->privFlags & CHANNEL_ANI_INIT) == 0) {595OS_MEMZERO(aniState, sizeof(*aniState));596if (IEEE80211_IS_CHAN_2GHZ(chan))597aniState->params = &ahp->ah_aniParams24;598else599aniState->params = &ahp->ah_aniParams5;600ichan->privFlags |= CHANNEL_ANI_INIT;601HALASSERT((ichan->privFlags & CHANNEL_ANI_SETUP) == 0);602}603ahp->ah_curani = aniState;604#if 0605ath_hal_printf(ah,"%s: chan %u/0x%x restore %d opmode %u%s\n",606__func__, chan->ic_freq, chan->ic_flags, restore, opmode,607ichan->privFlags & CHANNEL_ANI_SETUP ? " setup" : "");608#else609HALDEBUG(ah, HAL_DEBUG_ANI, "%s: chan %u/0x%x restore %d opmode %u%s\n",610__func__, chan->ic_freq, chan->ic_flags, restore, opmode,611ichan->privFlags & CHANNEL_ANI_SETUP ? " setup" : "");612#endif613OS_MARK(ah, AH_MARK_ANI_RESET, opmode);614615/*616* Turn off PHY error frame delivery while we futz with settings.617*/618rxfilter = ah->ah_getRxFilter(ah);619ah->ah_setRxFilter(ah, rxfilter &~ HAL_RX_FILTER_PHYERR);620621/*622* If ANI is disabled at this point, don't set the default623* ANI parameter settings - leave the HAL settings there.624* This is (currently) needed for reliable radar detection.625*/626if (! ANI_ENA(ah)) {627HALDEBUG(ah, HAL_DEBUG_ANI, "%s: ANI disabled\n",628__func__);629goto finish;630}631632/*633* Automatic processing is done only in station mode right now.634*/635if (opmode == HAL_M_STA)636ahp->ah_procPhyErr |= HAL_RSSI_ANI_ENA;637else638ahp->ah_procPhyErr &= ~HAL_RSSI_ANI_ENA;639/*640* Set all ani parameters. We either set them to initial641* values or restore the previous ones for the channel.642* XXX if ANI follows hardware, we don't care what mode we're643* XXX in, we should keep the ani parameters644*/645if (restore && (ichan->privFlags & CHANNEL_ANI_SETUP)) {646ar5212AniControl(ah, HAL_ANI_NOISE_IMMUNITY_LEVEL,647aniState->noiseImmunityLevel);648ar5212AniControl(ah, HAL_ANI_SPUR_IMMUNITY_LEVEL,649aniState->spurImmunityLevel);650ar5212AniControl(ah, HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,651!aniState->ofdmWeakSigDetectOff);652ar5212AniControl(ah, HAL_ANI_CCK_WEAK_SIGNAL_THR,653aniState->cckWeakSigThreshold);654ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,655aniState->firstepLevel);656} else {657ar5212AniControl(ah, HAL_ANI_NOISE_IMMUNITY_LEVEL, 0);658ar5212AniControl(ah, HAL_ANI_SPUR_IMMUNITY_LEVEL, 0);659ar5212AniControl(ah, HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,660AH_TRUE);661ar5212AniControl(ah, HAL_ANI_CCK_WEAK_SIGNAL_THR, AH_FALSE);662ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL, 0);663ichan->privFlags |= CHANNEL_ANI_SETUP;664}665/*666* In case the counters haven't yet been setup; set them up.667*/668enableAniMIBCounters(ah, ahp->ah_curani->params);669ar5212AniRestart(ah, aniState);670671finish:672/* restore RX filter mask */673ah->ah_setRxFilter(ah, rxfilter);674}675676/*677* Process a MIB interrupt. We may potentially be invoked because678* any of the MIB counters overflow/trigger so don't assume we're679* here because a PHY error counter triggered.680*/681void682ar5212ProcessMibIntr(struct ath_hal *ah, const HAL_NODE_STATS *stats)683{684struct ath_hal_5212 *ahp = AH5212(ah);685uint32_t phyCnt1, phyCnt2;686687HALDEBUG(ah, HAL_DEBUG_ANI, "%s: mibc 0x%x phyCnt1 0x%x phyCnt2 0x%x "688"filtofdm 0x%x filtcck 0x%x\n",689__func__, OS_REG_READ(ah, AR_MIBC),690OS_REG_READ(ah, AR_PHYCNT1), OS_REG_READ(ah, AR_PHYCNT2),691OS_REG_READ(ah, AR_FILTOFDM), OS_REG_READ(ah, AR_FILTCCK));692693/*694* First order of business is to clear whatever caused695* the interrupt so we don't keep getting interrupted.696* We have the usual mib counters that are reset-on-read697* and the additional counters that appeared starting in698* Hainan. We collect the mib counters and explicitly699* zero additional counters we are not using. Anything700* else is reset only if it caused the interrupt.701*/702/* NB: these are not reset-on-read */703phyCnt1 = OS_REG_READ(ah, AR_PHYCNT1);704phyCnt2 = OS_REG_READ(ah, AR_PHYCNT2);705/* not used, always reset them in case they are the cause */706OS_REG_WRITE(ah, AR_FILTOFDM, 0);707OS_REG_WRITE(ah, AR_FILTCCK, 0);708709/* Clear the mib counters and save them in the stats */710ar5212UpdateMibCounters(ah, &ahp->ah_mibStats);711ahp->ah_stats.ast_nodestats = *stats;712713/*714* Check for an ani stat hitting the trigger threshold.715* When this happens we get a MIB interrupt and the top716* 2 bits of the counter register will be 0b11, hence717* the mask check of phyCnt?.718*/719if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||720((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {721struct ar5212AniState *aniState = ahp->ah_curani;722const struct ar5212AniParams *params = aniState->params;723uint32_t ofdmPhyErrCnt, cckPhyErrCnt;724725ofdmPhyErrCnt = phyCnt1 - params->ofdmPhyErrBase;726ahp->ah_stats.ast_ani_ofdmerrs +=727ofdmPhyErrCnt - aniState->ofdmPhyErrCount;728aniState->ofdmPhyErrCount = ofdmPhyErrCnt;729730cckPhyErrCnt = phyCnt2 - params->cckPhyErrBase;731ahp->ah_stats.ast_ani_cckerrs +=732cckPhyErrCnt - aniState->cckPhyErrCount;733aniState->cckPhyErrCount = cckPhyErrCnt;734735/*736* NB: figure out which counter triggered. If both737* trigger we'll only deal with one as the processing738* clobbers the error counter so the trigger threshold739* check will never be true.740*/741if (aniState->ofdmPhyErrCount > params->ofdmTrigHigh)742ar5212AniOfdmErrTrigger(ah);743if (aniState->cckPhyErrCount > params->cckTrigHigh)744ar5212AniCckErrTrigger(ah);745/* NB: always restart to insure the h/w counters are reset */746ar5212AniRestart(ah, aniState);747}748}749750void751ar5212AniPhyErrReport(struct ath_hal *ah, const struct ath_rx_status *rs)752{753struct ath_hal_5212 *ahp = AH5212(ah);754struct ar5212AniState *aniState;755const struct ar5212AniParams *params;756757HALASSERT(!ahp->ah_hasHwPhyCounters && rs != AH_NULL);758759aniState = ahp->ah_curani;760params = aniState->params;761if (rs->rs_phyerr == HAL_PHYERR_OFDM_TIMING) {762aniState->ofdmPhyErrCount++;763ahp->ah_stats.ast_ani_ofdmerrs++;764if (aniState->ofdmPhyErrCount > params->ofdmTrigHigh) {765ar5212AniOfdmErrTrigger(ah);766ar5212AniRestart(ah, aniState);767}768} else if (rs->rs_phyerr == HAL_PHYERR_CCK_TIMING) {769aniState->cckPhyErrCount++;770ahp->ah_stats.ast_ani_cckerrs++;771if (aniState->cckPhyErrCount > params->cckTrigHigh) {772ar5212AniCckErrTrigger(ah);773ar5212AniRestart(ah, aniState);774}775}776}777778static void779ar5212AniLowerImmunity(struct ath_hal *ah)780{781struct ath_hal_5212 *ahp = AH5212(ah);782struct ar5212AniState *aniState;783const struct ar5212AniParams *params;784785HALASSERT(ANI_ENA(ah));786787aniState = ahp->ah_curani;788params = aniState->params;789if (ANI_ENA_RSSI(ah)) {790int32_t rssi = BEACON_RSSI(ahp);791if (rssi > params->rssiThrHigh) {792/*793* Beacon signal is high, leave ofdm weak signal794* detection off or it may oscillate. Let it fall795* through.796*/797} else if (rssi > params->rssiThrLow) {798/*799* Beacon rssi in mid range, turn on ofdm weak signal800* detection or lower firstep level.801*/802if (aniState->ofdmWeakSigDetectOff) {803HALDEBUG(ah, HAL_DEBUG_ANI,804"%s: rssi %d OWSD on\n", __func__, rssi);805ar5212AniControl(ah,806HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,807AH_TRUE);808return;809}810if (aniState->firstepLevel > 0) {811HALDEBUG(ah, HAL_DEBUG_ANI,812"%s: rssi %d lower ST %u\n", __func__, rssi,813aniState->firstepLevel-1);814ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,815aniState->firstepLevel - 1);816return;817}818} else {819/*820* Beacon rssi is low, reduce firstep level.821*/822if (aniState->firstepLevel > 0) {823HALDEBUG(ah, HAL_DEBUG_ANI,824"%s: rssi %d lower ST %u\n", __func__, rssi,825aniState->firstepLevel-1);826ar5212AniControl(ah, HAL_ANI_FIRSTEP_LEVEL,827aniState->firstepLevel - 1);828return;829}830}831}832/* then lower spur immunity level, down to zero */833if (aniState->spurImmunityLevel > 0) {834HALDEBUG(ah, HAL_DEBUG_ANI, "%s: lower SI %u\n",835__func__, aniState->spurImmunityLevel-1);836ar5212AniControl(ah, HAL_ANI_SPUR_IMMUNITY_LEVEL,837aniState->spurImmunityLevel - 1);838return;839}840/*841* if all else fails, lower noise immunity level down to a min value842* zero for now843*/844if (aniState->noiseImmunityLevel > 0) {845HALDEBUG(ah, HAL_DEBUG_ANI, "%s: lower NI %u\n",846__func__, aniState->noiseImmunityLevel-1);847ar5212AniControl(ah, HAL_ANI_NOISE_IMMUNITY_LEVEL,848aniState->noiseImmunityLevel - 1);849return;850}851}852853#define CLOCK_RATE 44000 /* XXX use mac_usec or similar */854/* convert HW counter values to ms using 11g clock rate, goo9d enough855for 11a and Turbo */856857/*858* Return an approximation of the time spent ``listening'' by859* deducting the cycles spent tx'ing and rx'ing from the total860* cycle count since our last call. A return value <0 indicates861* an invalid/inconsistent time.862*/863static int32_t864ar5212AniGetListenTime(struct ath_hal *ah)865{866struct ath_hal_5212 *ahp = AH5212(ah);867struct ar5212AniState *aniState = NULL;868int32_t listenTime = 0;869int good;870HAL_SURVEY_SAMPLE hs;871872/*873* We shouldn't see ah_curchan be NULL, but just in case..874*/875if (AH_PRIVATE(ah)->ah_curchan == AH_NULL) {876ath_hal_printf(ah, "%s: ah_curchan = NULL?\n", __func__);877return (0);878}879880/*881* Fetch the current statistics, squirrel away the current882* sample, bump the sequence/sample counter.883*/884OS_MEMZERO(&hs, sizeof(hs));885good = ar5212GetMibCycleCounts(ah, &hs);886ath_hal_survey_add_sample(ah, &hs);887888if (ANI_ENA(ah))889aniState = ahp->ah_curani;890891if (good == AH_FALSE) {892/*893* Cycle counter wrap (or initial call); it's not possible894* to accurately calculate a value because the registers895* right shift rather than wrap--so punt and return 0.896*/897listenTime = 0;898ahp->ah_stats.ast_ani_lzero++;899} else if (ANI_ENA(ah)) {900/*901* Only calculate and update the cycle count if we have902* an ANI state.903*/904int32_t ccdelta =905AH5212(ah)->ah_cycleCount - aniState->cycleCount;906int32_t rfdelta =907AH5212(ah)->ah_rxBusy - aniState->rxFrameCount;908int32_t tfdelta =909AH5212(ah)->ah_txBusy - aniState->txFrameCount;910listenTime = (ccdelta - rfdelta - tfdelta) / CLOCK_RATE;911}912913/*914* Again, only update ANI state if we have it.915*/916if (ANI_ENA(ah)) {917aniState->cycleCount = AH5212(ah)->ah_cycleCount;918aniState->rxFrameCount = AH5212(ah)->ah_rxBusy;919aniState->txFrameCount = AH5212(ah)->ah_txBusy;920}921922return listenTime;923}924925/*926* Update ani stats in preparation for listen time processing.927*/928static void929updateMIBStats(struct ath_hal *ah, struct ar5212AniState *aniState)930{931struct ath_hal_5212 *ahp = AH5212(ah);932const struct ar5212AniParams *params = aniState->params;933uint32_t phyCnt1, phyCnt2;934int32_t ofdmPhyErrCnt, cckPhyErrCnt;935936HALASSERT(ahp->ah_hasHwPhyCounters);937938/* Clear the mib counters and save them in the stats */939ar5212UpdateMibCounters(ah, &ahp->ah_mibStats);940941/* NB: these are not reset-on-read */942phyCnt1 = OS_REG_READ(ah, AR_PHYCNT1);943phyCnt2 = OS_REG_READ(ah, AR_PHYCNT2);944945/* NB: these are spec'd to never roll-over */946ofdmPhyErrCnt = phyCnt1 - params->ofdmPhyErrBase;947if (ofdmPhyErrCnt < 0) {948HALDEBUG(ah, HAL_DEBUG_ANI, "OFDM phyErrCnt %d phyCnt1 0x%x\n",949ofdmPhyErrCnt, phyCnt1);950ofdmPhyErrCnt = AR_PHY_COUNTMAX;951}952ahp->ah_stats.ast_ani_ofdmerrs +=953ofdmPhyErrCnt - aniState->ofdmPhyErrCount;954aniState->ofdmPhyErrCount = ofdmPhyErrCnt;955956cckPhyErrCnt = phyCnt2 - params->cckPhyErrBase;957if (cckPhyErrCnt < 0) {958HALDEBUG(ah, HAL_DEBUG_ANI, "CCK phyErrCnt %d phyCnt2 0x%x\n",959cckPhyErrCnt, phyCnt2);960cckPhyErrCnt = AR_PHY_COUNTMAX;961}962ahp->ah_stats.ast_ani_cckerrs +=963cckPhyErrCnt - aniState->cckPhyErrCount;964aniState->cckPhyErrCount = cckPhyErrCnt;965}966967void968ar5212RxMonitor(struct ath_hal *ah, const HAL_NODE_STATS *stats,969const struct ieee80211_channel *chan)970{971struct ath_hal_5212 *ahp = AH5212(ah);972ahp->ah_stats.ast_nodestats.ns_avgbrssi = stats->ns_avgbrssi;973}974975/*976* Do periodic processing. This routine is called from the977* driver's rx interrupt handler after processing frames.978*/979void980ar5212AniPoll(struct ath_hal *ah, const struct ieee80211_channel *chan)981{982struct ath_hal_5212 *ahp = AH5212(ah);983struct ar5212AniState *aniState = ahp->ah_curani;984const struct ar5212AniParams *params;985int32_t listenTime;986987/* Always update from the MIB, for statistics gathering */988listenTime = ar5212AniGetListenTime(ah);989990/* XXX can aniState be null? */991if (aniState == AH_NULL)992return;993if (!ANI_ENA(ah))994return;995996if (listenTime < 0) {997ahp->ah_stats.ast_ani_lneg++;998/* restart ANI period if listenTime is invalid */999ar5212AniRestart(ah, aniState);10001001/* Don't do any further ANI processing here */1002return;1003}1004/* XXX beware of overflow? */1005aniState->listenTime += listenTime;10061007OS_MARK(ah, AH_MARK_ANI_POLL, aniState->listenTime);10081009params = aniState->params;1010if (aniState->listenTime > 5*params->period) {1011/*1012* Check to see if need to lower immunity if1013* 5 aniPeriods have passed1014*/1015if (ahp->ah_hasHwPhyCounters)1016updateMIBStats(ah, aniState);1017if (aniState->ofdmPhyErrCount <= aniState->listenTime *1018params->ofdmTrigLow/1000 &&1019aniState->cckPhyErrCount <= aniState->listenTime *1020params->cckTrigLow/1000)1021ar5212AniLowerImmunity(ah);1022ar5212AniRestart(ah, aniState);1023} else if (aniState->listenTime > params->period) {1024if (ahp->ah_hasHwPhyCounters)1025updateMIBStats(ah, aniState);1026/* check to see if need to raise immunity */1027if (aniState->ofdmPhyErrCount > aniState->listenTime *1028params->ofdmTrigHigh / 1000) {1029HALDEBUG(ah, HAL_DEBUG_ANI,1030"%s: OFDM err %u listenTime %u\n", __func__,1031aniState->ofdmPhyErrCount, aniState->listenTime);1032ar5212AniOfdmErrTrigger(ah);1033ar5212AniRestart(ah, aniState);1034} else if (aniState->cckPhyErrCount > aniState->listenTime *1035params->cckTrigHigh / 1000) {1036HALDEBUG(ah, HAL_DEBUG_ANI,1037"%s: CCK err %u listenTime %u\n", __func__,1038aniState->cckPhyErrCount, aniState->listenTime);1039ar5212AniCckErrTrigger(ah);1040ar5212AniRestart(ah, aniState);1041}1042}1043}104410451046