18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2008-2011 Atheros Communications Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
118c2ecf20Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
148c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/export.h>
198c2ecf20Sopenharmony_ci#include "hw.h"
208c2ecf20Sopenharmony_ci#include "hw-ops.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct ani_ofdm_level_entry {
238c2ecf20Sopenharmony_ci	int spur_immunity_level;
248c2ecf20Sopenharmony_ci	int fir_step_level;
258c2ecf20Sopenharmony_ci	int ofdm_weak_signal_on;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* values here are relative to the INI */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Legend:
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * SI: Spur immunity
348c2ecf20Sopenharmony_ci * FS: FIR Step
358c2ecf20Sopenharmony_ci * WS: OFDM / CCK Weak Signal detection
368c2ecf20Sopenharmony_ci * MRC-CCK: Maximal Ratio Combining for CCK
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const struct ani_ofdm_level_entry ofdm_level_table[] = {
408c2ecf20Sopenharmony_ci	/* SI  FS  WS */
418c2ecf20Sopenharmony_ci	{  0,  0,  1  }, /* lvl 0 */
428c2ecf20Sopenharmony_ci	{  1,  1,  1  }, /* lvl 1 */
438c2ecf20Sopenharmony_ci	{  2,  2,  1  }, /* lvl 2 */
448c2ecf20Sopenharmony_ci	{  3,  2,  1  }, /* lvl 3  (default) */
458c2ecf20Sopenharmony_ci	{  4,  3,  1  }, /* lvl 4 */
468c2ecf20Sopenharmony_ci	{  5,  4,  1  }, /* lvl 5 */
478c2ecf20Sopenharmony_ci	{  6,  5,  1  }, /* lvl 6 */
488c2ecf20Sopenharmony_ci	{  7,  6,  1  }, /* lvl 7 */
498c2ecf20Sopenharmony_ci	{  7,  7,  1  }, /* lvl 8 */
508c2ecf20Sopenharmony_ci	{  7,  8,  0  }  /* lvl 9 */
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci#define ATH9K_ANI_OFDM_NUM_LEVEL \
538c2ecf20Sopenharmony_ci	ARRAY_SIZE(ofdm_level_table)
548c2ecf20Sopenharmony_ci#define ATH9K_ANI_OFDM_MAX_LEVEL \
558c2ecf20Sopenharmony_ci	(ATH9K_ANI_OFDM_NUM_LEVEL-1)
568c2ecf20Sopenharmony_ci#define ATH9K_ANI_OFDM_DEF_LEVEL \
578c2ecf20Sopenharmony_ci	3 /* default level - matches the INI settings */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
618c2ecf20Sopenharmony_ci * With OFDM for single stream you just add up all antenna inputs, you're
628c2ecf20Sopenharmony_ci * only interested in what you get after FFT. Signal alignment is also not
638c2ecf20Sopenharmony_ci * required for OFDM because any phase difference adds up in the frequency
648c2ecf20Sopenharmony_ci * domain.
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * MRC requires extra work for use with CCK. You need to align the antenna
678c2ecf20Sopenharmony_ci * signals from the different antenna before you can add the signals together.
688c2ecf20Sopenharmony_ci * You need alignment of signals as CCK is in time domain, so addition can cancel
698c2ecf20Sopenharmony_ci * your signal completely if phase is 180 degrees (think of adding sine waves).
708c2ecf20Sopenharmony_ci * You also need to remove noise before the addition and this is where ANI
718c2ecf20Sopenharmony_ci * MRC CCK comes into play. One of the antenna inputs may be stronger but
728c2ecf20Sopenharmony_ci * lower SNR, so just adding after alignment can be dangerous.
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Regardless of alignment in time, the antenna signals add constructively after
758c2ecf20Sopenharmony_ci * FFT and improve your reception. For more information:
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * https://en.wikipedia.org/wiki/Maximal-ratio_combining
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistruct ani_cck_level_entry {
818c2ecf20Sopenharmony_ci	int fir_step_level;
828c2ecf20Sopenharmony_ci	int mrc_cck_on;
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic const struct ani_cck_level_entry cck_level_table[] = {
868c2ecf20Sopenharmony_ci	/* FS  MRC-CCK  */
878c2ecf20Sopenharmony_ci	{  0,  1  }, /* lvl 0 */
888c2ecf20Sopenharmony_ci	{  1,  1  }, /* lvl 1 */
898c2ecf20Sopenharmony_ci	{  2,  1  }, /* lvl 2  (default) */
908c2ecf20Sopenharmony_ci	{  3,  1  }, /* lvl 3 */
918c2ecf20Sopenharmony_ci	{  4,  0  }, /* lvl 4 */
928c2ecf20Sopenharmony_ci	{  5,  0  }, /* lvl 5 */
938c2ecf20Sopenharmony_ci	{  6,  0  }, /* lvl 6 */
948c2ecf20Sopenharmony_ci	{  7,  0  }, /* lvl 7 (only for high rssi) */
958c2ecf20Sopenharmony_ci	{  8,  0  }  /* lvl 8 (only for high rssi) */
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define ATH9K_ANI_CCK_NUM_LEVEL \
998c2ecf20Sopenharmony_ci	ARRAY_SIZE(cck_level_table)
1008c2ecf20Sopenharmony_ci#define ATH9K_ANI_CCK_MAX_LEVEL \
1018c2ecf20Sopenharmony_ci	(ATH9K_ANI_CCK_NUM_LEVEL-1)
1028c2ecf20Sopenharmony_ci#define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
1038c2ecf20Sopenharmony_ci	(ATH9K_ANI_CCK_NUM_LEVEL-3)
1048c2ecf20Sopenharmony_ci#define ATH9K_ANI_CCK_DEF_LEVEL \
1058c2ecf20Sopenharmony_ci	2 /* default level - matches the INI settings */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void ath9k_hw_update_mibstats(struct ath_hw *ah,
1088c2ecf20Sopenharmony_ci				     struct ath9k_mib_stats *stats)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	u32 addr[5] = {AR_RTS_OK, AR_RTS_FAIL, AR_ACK_FAIL,
1118c2ecf20Sopenharmony_ci		       AR_FCS_FAIL, AR_BEACON_CNT};
1128c2ecf20Sopenharmony_ci	u32 data[5];
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	REG_READ_MULTI(ah, &addr[0], &data[0], 5);
1158c2ecf20Sopenharmony_ci	/* AR_RTS_OK */
1168c2ecf20Sopenharmony_ci	stats->rts_good += data[0];
1178c2ecf20Sopenharmony_ci	/* AR_RTS_FAIL */
1188c2ecf20Sopenharmony_ci	stats->rts_bad += data[1];
1198c2ecf20Sopenharmony_ci	/* AR_ACK_FAIL */
1208c2ecf20Sopenharmony_ci	stats->ackrcv_bad += data[2];
1218c2ecf20Sopenharmony_ci	/* AR_FCS_FAIL */
1228c2ecf20Sopenharmony_ci	stats->fcs_bad += data[3];
1238c2ecf20Sopenharmony_ci	/* AR_BEACON_CNT */
1248c2ecf20Sopenharmony_ci	stats->beacons += data[4];
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void ath9k_ani_restart(struct ath_hw *ah)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	aniState->listenTime = 0;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	ENABLE_REGWRITE_BUFFER(ah);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_1, 0);
1368c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_2, 0);
1378c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
1388c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	REGWRITE_BUFFER_FLUSH(ah);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	aniState->ofdmPhyErrCount = 0;
1458c2ecf20Sopenharmony_ci	aniState->cckPhyErrCount = 0;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* Adjust the OFDM Noise Immunity Level */
1498c2ecf20Sopenharmony_cistatic void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel,
1508c2ecf20Sopenharmony_ci				  bool scan)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
1538c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
1548c2ecf20Sopenharmony_ci	const struct ani_ofdm_level_entry *entry_ofdm;
1558c2ecf20Sopenharmony_ci	const struct ani_cck_level_entry *entry_cck;
1568c2ecf20Sopenharmony_ci	bool weak_sig;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	ath_dbg(common, ANI, "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
1598c2ecf20Sopenharmony_ci		aniState->ofdmNoiseImmunityLevel,
1608c2ecf20Sopenharmony_ci		immunityLevel, BEACON_RSSI(ah),
1618c2ecf20Sopenharmony_ci		ATH9K_ANI_RSSI_THR_LOW,
1628c2ecf20Sopenharmony_ci		ATH9K_ANI_RSSI_THR_HIGH);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (AR_SREV_9100(ah) && immunityLevel < ATH9K_ANI_OFDM_DEF_LEVEL)
1658c2ecf20Sopenharmony_ci		immunityLevel = ATH9K_ANI_OFDM_DEF_LEVEL;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (!scan)
1688c2ecf20Sopenharmony_ci		aniState->ofdmNoiseImmunityLevel = immunityLevel;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
1718c2ecf20Sopenharmony_ci	entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
1748c2ecf20Sopenharmony_ci		ath9k_hw_ani_control(ah,
1758c2ecf20Sopenharmony_ci				     ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
1768c2ecf20Sopenharmony_ci				     entry_ofdm->spur_immunity_level);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
1798c2ecf20Sopenharmony_ci	    entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
1808c2ecf20Sopenharmony_ci		ath9k_hw_ani_control(ah,
1818c2ecf20Sopenharmony_ci				     ATH9K_ANI_FIRSTEP_LEVEL,
1828c2ecf20Sopenharmony_ci				     entry_ofdm->fir_step_level);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	weak_sig = entry_ofdm->ofdm_weak_signal_on;
1858c2ecf20Sopenharmony_ci	if (ah->opmode == NL80211_IFTYPE_STATION &&
1868c2ecf20Sopenharmony_ci	    BEACON_RSSI(ah) <= ATH9K_ANI_RSSI_THR_HIGH)
1878c2ecf20Sopenharmony_ci		weak_sig = true;
1888c2ecf20Sopenharmony_ci	/*
1898c2ecf20Sopenharmony_ci	 * Newer chipsets are better at dealing with high PHY error counts -
1908c2ecf20Sopenharmony_ci	 * keep weak signal detection enabled when no RSSI threshold is
1918c2ecf20Sopenharmony_ci	 * available to determine if it is needed (mode != STA)
1928c2ecf20Sopenharmony_ci	 */
1938c2ecf20Sopenharmony_ci	else if (AR_SREV_9300_20_OR_LATER(ah) &&
1948c2ecf20Sopenharmony_ci		 ah->opmode != NL80211_IFTYPE_STATION)
1958c2ecf20Sopenharmony_ci		weak_sig = true;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Older chipsets are more sensitive to high PHY error counts */
1988c2ecf20Sopenharmony_ci	else if (!AR_SREV_9300_20_OR_LATER(ah) &&
1998c2ecf20Sopenharmony_ci		 aniState->ofdmNoiseImmunityLevel >= 8)
2008c2ecf20Sopenharmony_ci		weak_sig = false;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (aniState->ofdmWeakSigDetect != weak_sig)
2038c2ecf20Sopenharmony_ci		ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
2048c2ecf20Sopenharmony_ci				     weak_sig);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (!AR_SREV_9300_20_OR_LATER(ah))
2078c2ecf20Sopenharmony_ci		return;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (aniState->ofdmNoiseImmunityLevel >= ATH9K_ANI_OFDM_DEF_LEVEL) {
2108c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH;
2118c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_ABOVE_INI;
2128c2ecf20Sopenharmony_ci	} else {
2138c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_BELOW_INI;
2148c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
2238c2ecf20Sopenharmony_ci		ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1, false);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * Set the ANI settings to match an CCK level.
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_cistatic void ath9k_hw_set_cck_nil(struct ath_hw *ah, u_int8_t immunityLevel,
2308c2ecf20Sopenharmony_ci				 bool scan)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
2338c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
2348c2ecf20Sopenharmony_ci	const struct ani_ofdm_level_entry *entry_ofdm;
2358c2ecf20Sopenharmony_ci	const struct ani_cck_level_entry *entry_cck;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	ath_dbg(common, ANI, "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
2388c2ecf20Sopenharmony_ci		aniState->cckNoiseImmunityLevel, immunityLevel,
2398c2ecf20Sopenharmony_ci		BEACON_RSSI(ah), ATH9K_ANI_RSSI_THR_LOW,
2408c2ecf20Sopenharmony_ci		ATH9K_ANI_RSSI_THR_HIGH);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	if (AR_SREV_9100(ah) && immunityLevel < ATH9K_ANI_CCK_DEF_LEVEL)
2438c2ecf20Sopenharmony_ci		immunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	if (ah->opmode == NL80211_IFTYPE_STATION &&
2468c2ecf20Sopenharmony_ci	    BEACON_RSSI(ah) <= ATH9K_ANI_RSSI_THR_LOW &&
2478c2ecf20Sopenharmony_ci	    immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
2488c2ecf20Sopenharmony_ci		immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!scan)
2518c2ecf20Sopenharmony_ci		aniState->cckNoiseImmunityLevel = immunityLevel;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
2548c2ecf20Sopenharmony_ci	entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (aniState->firstepLevel != entry_cck->fir_step_level &&
2578c2ecf20Sopenharmony_ci	    entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
2588c2ecf20Sopenharmony_ci		ath9k_hw_ani_control(ah,
2598c2ecf20Sopenharmony_ci				     ATH9K_ANI_FIRSTEP_LEVEL,
2608c2ecf20Sopenharmony_ci				     entry_cck->fir_step_level);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* Skip MRC CCK for pre AR9003 families */
2638c2ecf20Sopenharmony_ci	if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah) ||
2648c2ecf20Sopenharmony_ci	    AR_SREV_9565(ah) || AR_SREV_9561(ah))
2658c2ecf20Sopenharmony_ci		return;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (aniState->mrcCCK != entry_cck->mrc_cck_on)
2688c2ecf20Sopenharmony_ci		ath9k_hw_ani_control(ah,
2698c2ecf20Sopenharmony_ci				     ATH9K_ANI_MRC_CCK,
2708c2ecf20Sopenharmony_ci				     entry_cck->mrc_cck_on);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
2788c2ecf20Sopenharmony_ci		ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1,
2798c2ecf20Sopenharmony_ci				     false);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci/*
2838c2ecf20Sopenharmony_ci * only lower either OFDM or CCK errors per turn
2848c2ecf20Sopenharmony_ci * we lower the other one next time
2858c2ecf20Sopenharmony_ci */
2868c2ecf20Sopenharmony_cistatic void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* lower OFDM noise immunity */
2918c2ecf20Sopenharmony_ci	if (aniState->ofdmNoiseImmunityLevel > 0 &&
2928c2ecf20Sopenharmony_ci	    (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
2938c2ecf20Sopenharmony_ci		ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1,
2948c2ecf20Sopenharmony_ci				      false);
2958c2ecf20Sopenharmony_ci		return;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* lower CCK noise immunity */
2998c2ecf20Sopenharmony_ci	if (aniState->cckNoiseImmunityLevel > 0)
3008c2ecf20Sopenharmony_ci		ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1,
3018c2ecf20Sopenharmony_ci				     false);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/*
3058c2ecf20Sopenharmony_ci * Restore the ANI parameters in the HAL and reset the statistics.
3068c2ecf20Sopenharmony_ci * This routine should be called for every hardware reset and for
3078c2ecf20Sopenharmony_ci * every channel change.
3088c2ecf20Sopenharmony_ci */
3098c2ecf20Sopenharmony_civoid ath9k_ani_reset(struct ath_hw *ah, bool is_scanning)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
3128c2ecf20Sopenharmony_ci	struct ath9k_channel *chan = ah->curchan;
3138c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
3148c2ecf20Sopenharmony_ci	int ofdm_nil, cck_nil;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (!chan)
3178c2ecf20Sopenharmony_ci		return;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	BUG_ON(aniState == NULL);
3208c2ecf20Sopenharmony_ci	ah->stats.ast_ani_reset++;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	ofdm_nil = max_t(int, ATH9K_ANI_OFDM_DEF_LEVEL,
3238c2ecf20Sopenharmony_ci			 aniState->ofdmNoiseImmunityLevel);
3248c2ecf20Sopenharmony_ci	cck_nil = max_t(int, ATH9K_ANI_CCK_DEF_LEVEL,
3258c2ecf20Sopenharmony_ci			 aniState->cckNoiseImmunityLevel);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (is_scanning ||
3288c2ecf20Sopenharmony_ci	    (ah->opmode != NL80211_IFTYPE_STATION &&
3298c2ecf20Sopenharmony_ci	     ah->opmode != NL80211_IFTYPE_ADHOC)) {
3308c2ecf20Sopenharmony_ci		/*
3318c2ecf20Sopenharmony_ci		 * If we're scanning or in AP mode, the defaults (ini)
3328c2ecf20Sopenharmony_ci		 * should be in place. For an AP we assume the historical
3338c2ecf20Sopenharmony_ci		 * levels for this channel are probably outdated so start
3348c2ecf20Sopenharmony_ci		 * from defaults instead.
3358c2ecf20Sopenharmony_ci		 */
3368c2ecf20Sopenharmony_ci		if (aniState->ofdmNoiseImmunityLevel !=
3378c2ecf20Sopenharmony_ci		    ATH9K_ANI_OFDM_DEF_LEVEL ||
3388c2ecf20Sopenharmony_ci		    aniState->cckNoiseImmunityLevel !=
3398c2ecf20Sopenharmony_ci		    ATH9K_ANI_CCK_DEF_LEVEL) {
3408c2ecf20Sopenharmony_ci			ath_dbg(common, ANI,
3418c2ecf20Sopenharmony_ci				"Restore defaults: opmode %u chan %d Mhz is_scanning=%d ofdm:%d cck:%d\n",
3428c2ecf20Sopenharmony_ci				ah->opmode,
3438c2ecf20Sopenharmony_ci				chan->channel,
3448c2ecf20Sopenharmony_ci				is_scanning,
3458c2ecf20Sopenharmony_ci				aniState->ofdmNoiseImmunityLevel,
3468c2ecf20Sopenharmony_ci				aniState->cckNoiseImmunityLevel);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci			ofdm_nil = ATH9K_ANI_OFDM_DEF_LEVEL;
3498c2ecf20Sopenharmony_ci			cck_nil = ATH9K_ANI_CCK_DEF_LEVEL;
3508c2ecf20Sopenharmony_ci		}
3518c2ecf20Sopenharmony_ci	} else {
3528c2ecf20Sopenharmony_ci		/*
3538c2ecf20Sopenharmony_ci		 * restore historical levels for this channel
3548c2ecf20Sopenharmony_ci		 */
3558c2ecf20Sopenharmony_ci		ath_dbg(common, ANI,
3568c2ecf20Sopenharmony_ci			"Restore history: opmode %u chan %d Mhz is_scanning=%d ofdm:%d cck:%d\n",
3578c2ecf20Sopenharmony_ci			ah->opmode,
3588c2ecf20Sopenharmony_ci			chan->channel,
3598c2ecf20Sopenharmony_ci			is_scanning,
3608c2ecf20Sopenharmony_ci			aniState->ofdmNoiseImmunityLevel,
3618c2ecf20Sopenharmony_ci			aniState->cckNoiseImmunityLevel);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci	ath9k_hw_set_ofdm_nil(ah, ofdm_nil, is_scanning);
3648c2ecf20Sopenharmony_ci	ath9k_hw_set_cck_nil(ah, cck_nil, is_scanning);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	ath9k_ani_restart(ah);
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic bool ath9k_hw_ani_read_counters(struct ath_hw *ah)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
3728c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
3738c2ecf20Sopenharmony_ci	u32 phyCnt1, phyCnt2;
3748c2ecf20Sopenharmony_ci	int32_t listenTime;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	ath_hw_cycle_counters_update(common);
3778c2ecf20Sopenharmony_ci	listenTime = ath_hw_get_listen_time(common);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (listenTime <= 0) {
3808c2ecf20Sopenharmony_ci		ah->stats.ast_ani_lneg_or_lzero++;
3818c2ecf20Sopenharmony_ci		ath9k_ani_restart(ah);
3828c2ecf20Sopenharmony_ci		return false;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	aniState->listenTime += listenTime;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
3908c2ecf20Sopenharmony_ci	phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	ah->stats.ast_ani_ofdmerrs += phyCnt1 - aniState->ofdmPhyErrCount;
3938c2ecf20Sopenharmony_ci	aniState->ofdmPhyErrCount = phyCnt1;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	ah->stats.ast_ani_cckerrs += phyCnt2 - aniState->cckPhyErrCount;
3968c2ecf20Sopenharmony_ci	aniState->cckPhyErrCount = phyCnt2;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return true;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_civoid ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	struct ar5416AniState *aniState = &ah->ani;
4048c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
4058c2ecf20Sopenharmony_ci	u32 ofdmPhyErrRate, cckPhyErrRate;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (!ath9k_hw_ani_read_counters(ah))
4088c2ecf20Sopenharmony_ci		return;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
4118c2ecf20Sopenharmony_ci			 aniState->listenTime;
4128c2ecf20Sopenharmony_ci	cckPhyErrRate =  aniState->cckPhyErrCount * 1000 /
4138c2ecf20Sopenharmony_ci			 aniState->listenTime;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	ath_dbg(common, ANI,
4168c2ecf20Sopenharmony_ci		"listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
4178c2ecf20Sopenharmony_ci		aniState->listenTime,
4188c2ecf20Sopenharmony_ci		aniState->ofdmNoiseImmunityLevel,
4198c2ecf20Sopenharmony_ci		ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
4208c2ecf20Sopenharmony_ci		cckPhyErrRate, aniState->ofdmsTurn);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	if (aniState->listenTime > ah->aniperiod) {
4238c2ecf20Sopenharmony_ci		if (cckPhyErrRate < ah->config.cck_trig_low &&
4248c2ecf20Sopenharmony_ci		    ofdmPhyErrRate < ah->config.ofdm_trig_low) {
4258c2ecf20Sopenharmony_ci			ath9k_hw_ani_lower_immunity(ah);
4268c2ecf20Sopenharmony_ci			aniState->ofdmsTurn = !aniState->ofdmsTurn;
4278c2ecf20Sopenharmony_ci		} else if (ofdmPhyErrRate > ah->config.ofdm_trig_high) {
4288c2ecf20Sopenharmony_ci			ath9k_hw_ani_ofdm_err_trigger(ah);
4298c2ecf20Sopenharmony_ci			aniState->ofdmsTurn = false;
4308c2ecf20Sopenharmony_ci		} else if (cckPhyErrRate > ah->config.cck_trig_high) {
4318c2ecf20Sopenharmony_ci			ath9k_hw_ani_cck_err_trigger(ah);
4328c2ecf20Sopenharmony_ci			aniState->ofdmsTurn = true;
4338c2ecf20Sopenharmony_ci		} else
4348c2ecf20Sopenharmony_ci			return;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		ath9k_ani_restart(ah);
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ath9k_hw_ani_monitor);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_civoid ath9k_enable_mib_counters(struct ath_hw *ah)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	ath_dbg(common, ANI, "Enable MIB counters\n");
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	ENABLE_REGWRITE_BUFFER(ah);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_FILT_OFDM, 0);
4528c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_FILT_CCK, 0);
4538c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_MIBC,
4548c2ecf20Sopenharmony_ci		  ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
4558c2ecf20Sopenharmony_ci		  & 0x0f);
4568c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
4578c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	REGWRITE_BUFFER_FLUSH(ah);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci/* Freeze the MIB counters, get the stats and then clear them */
4638c2ecf20Sopenharmony_civoid ath9k_hw_disable_mib_counters(struct ath_hw *ah)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	ath_dbg(common, ANI, "Disable MIB counters\n");
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
4708c2ecf20Sopenharmony_ci	ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
4718c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
4728c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_FILT_OFDM, 0);
4738c2ecf20Sopenharmony_ci	REG_WRITE(ah, AR_FILT_CCK, 0);
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ath9k_hw_disable_mib_counters);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_civoid ath9k_hw_ani_init(struct ath_hw *ah)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
4808c2ecf20Sopenharmony_ci	struct ar5416AniState *ani = &ah->ani;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	ath_dbg(common, ANI, "Initialize ANI\n");
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	if (AR_SREV_9300_20_OR_LATER(ah)) {
4858c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH;
4868c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW;
4878c2ecf20Sopenharmony_ci		ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH;
4888c2ecf20Sopenharmony_ci		ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW;
4898c2ecf20Sopenharmony_ci	} else {
4908c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
4918c2ecf20Sopenharmony_ci		ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
4928c2ecf20Sopenharmony_ci		ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
4938c2ecf20Sopenharmony_ci		ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	ani->spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
4978c2ecf20Sopenharmony_ci	ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
4988c2ecf20Sopenharmony_ci	ani->mrcCCK = AR_SREV_9300_20_OR_LATER(ah) ? true : false;
4998c2ecf20Sopenharmony_ci	ani->ofdmsTurn = true;
5008c2ecf20Sopenharmony_ci	ani->ofdmWeakSigDetect = true;
5018c2ecf20Sopenharmony_ci	ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
5028c2ecf20Sopenharmony_ci	ani->ofdmNoiseImmunityLevel = ATH9K_ANI_OFDM_DEF_LEVEL;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/*
5058c2ecf20Sopenharmony_ci	 * since we expect some ongoing maintenance on the tables, let's sanity
5068c2ecf20Sopenharmony_ci	 * check here default level should not modify INI setting.
5078c2ecf20Sopenharmony_ci	 */
5088c2ecf20Sopenharmony_ci	ah->aniperiod = ATH9K_ANI_PERIOD;
5098c2ecf20Sopenharmony_ci	ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	ath9k_ani_restart(ah);
5128c2ecf20Sopenharmony_ci	ath9k_enable_mib_counters(ah);
5138c2ecf20Sopenharmony_ci}
514