ani.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include "hw.h"
  19. #include "hw-ops.h"
  20. struct ani_ofdm_level_entry {
  21. int spur_immunity_level;
  22. int fir_step_level;
  23. int ofdm_weak_signal_on;
  24. };
  25. /* values here are relative to the INI */
  26. /*
  27. * Legend:
  28. *
  29. * SI: Spur immunity
  30. * FS: FIR Step
  31. * WS: OFDM / CCK Weak Signal detection
  32. * MRC-CCK: Maximal Ratio Combining for CCK
  33. */
  34. static const struct ani_ofdm_level_entry ofdm_level_table[] = {
  35. /* SI FS WS */
  36. { 0, 0, 1 }, /* lvl 0 */
  37. { 1, 1, 1 }, /* lvl 1 */
  38. { 2, 2, 1 }, /* lvl 2 */
  39. { 3, 2, 1 }, /* lvl 3 (default) */
  40. { 4, 3, 1 }, /* lvl 4 */
  41. { 5, 4, 1 }, /* lvl 5 */
  42. { 6, 5, 1 }, /* lvl 6 */
  43. { 7, 6, 1 }, /* lvl 7 */
  44. { 7, 7, 1 }, /* lvl 8 */
  45. { 7, 8, 0 } /* lvl 9 */
  46. };
  47. #define ATH9K_ANI_OFDM_NUM_LEVEL \
  48. ARRAY_SIZE(ofdm_level_table)
  49. #define ATH9K_ANI_OFDM_MAX_LEVEL \
  50. (ATH9K_ANI_OFDM_NUM_LEVEL-1)
  51. #define ATH9K_ANI_OFDM_DEF_LEVEL \
  52. 3 /* default level - matches the INI settings */
  53. /*
  54. * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
  55. * With OFDM for single stream you just add up all antenna inputs, you're
  56. * only interested in what you get after FFT. Signal aligment is also not
  57. * required for OFDM because any phase difference adds up in the frequency
  58. * domain.
  59. *
  60. * MRC requires extra work for use with CCK. You need to align the antenna
  61. * signals from the different antenna before you can add the signals together.
  62. * You need aligment of signals as CCK is in time domain, so addition can cancel
  63. * your signal completely if phase is 180 degrees (think of adding sine waves).
  64. * You also need to remove noise before the addition and this is where ANI
  65. * MRC CCK comes into play. One of the antenna inputs may be stronger but
  66. * lower SNR, so just adding after alignment can be dangerous.
  67. *
  68. * Regardless of alignment in time, the antenna signals add constructively after
  69. * FFT and improve your reception. For more information:
  70. *
  71. * http://en.wikipedia.org/wiki/Maximal-ratio_combining
  72. */
  73. struct ani_cck_level_entry {
  74. int fir_step_level;
  75. int mrc_cck_on;
  76. };
  77. static const struct ani_cck_level_entry cck_level_table[] = {
  78. /* FS MRC-CCK */
  79. { 0, 1 }, /* lvl 0 */
  80. { 1, 1 }, /* lvl 1 */
  81. { 2, 1 }, /* lvl 2 (default) */
  82. { 3, 1 }, /* lvl 3 */
  83. { 4, 0 }, /* lvl 4 */
  84. { 5, 0 }, /* lvl 5 */
  85. { 6, 0 }, /* lvl 6 */
  86. { 7, 0 }, /* lvl 7 (only for high rssi) */
  87. { 8, 0 } /* lvl 8 (only for high rssi) */
  88. };
  89. #define ATH9K_ANI_CCK_NUM_LEVEL \
  90. ARRAY_SIZE(cck_level_table)
  91. #define ATH9K_ANI_CCK_MAX_LEVEL \
  92. (ATH9K_ANI_CCK_NUM_LEVEL-1)
  93. #define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
  94. (ATH9K_ANI_CCK_NUM_LEVEL-3)
  95. #define ATH9K_ANI_CCK_DEF_LEVEL \
  96. 2 /* default level - matches the INI settings */
  97. static bool use_new_ani(struct ath_hw *ah)
  98. {
  99. return AR_SREV_9300_20_OR_LATER(ah) || modparam_force_new_ani;
  100. }
  101. static void ath9k_hw_update_mibstats(struct ath_hw *ah,
  102. struct ath9k_mib_stats *stats)
  103. {
  104. stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
  105. stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
  106. stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
  107. stats->rts_good += REG_READ(ah, AR_RTS_OK);
  108. stats->beacons += REG_READ(ah, AR_BEACON_CNT);
  109. }
  110. static void ath9k_ani_restart(struct ath_hw *ah)
  111. {
  112. struct ar5416AniState *aniState;
  113. struct ath_common *common = ath9k_hw_common(ah);
  114. u32 ofdm_base = 0, cck_base = 0;
  115. if (!DO_ANI(ah))
  116. return;
  117. aniState = &ah->curchan->ani;
  118. aniState->listenTime = 0;
  119. if (!use_new_ani(ah)) {
  120. ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
  121. cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
  122. }
  123. ath_dbg(common, ANI, "Writing ofdmbase=%u cckbase=%u\n",
  124. ofdm_base, cck_base);
  125. ENABLE_REGWRITE_BUFFER(ah);
  126. REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
  127. REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
  128. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  129. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  130. REGWRITE_BUFFER_FLUSH(ah);
  131. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  132. aniState->ofdmPhyErrCount = 0;
  133. aniState->cckPhyErrCount = 0;
  134. }
  135. static void ath9k_hw_ani_ofdm_err_trigger_old(struct ath_hw *ah)
  136. {
  137. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  138. struct ar5416AniState *aniState;
  139. int32_t rssi;
  140. aniState = &ah->curchan->ani;
  141. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  142. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  143. aniState->noiseImmunityLevel + 1)) {
  144. return;
  145. }
  146. }
  147. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  148. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  149. aniState->spurImmunityLevel + 1)) {
  150. return;
  151. }
  152. }
  153. if (ah->opmode == NL80211_IFTYPE_AP) {
  154. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  155. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  156. aniState->firstepLevel + 1);
  157. }
  158. return;
  159. }
  160. rssi = BEACON_RSSI(ah);
  161. if (rssi > aniState->rssiThrHigh) {
  162. if (!aniState->ofdmWeakSigDetectOff) {
  163. if (ath9k_hw_ani_control(ah,
  164. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  165. false)) {
  166. ath9k_hw_ani_control(ah,
  167. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  168. return;
  169. }
  170. }
  171. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  172. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  173. aniState->firstepLevel + 1);
  174. return;
  175. }
  176. } else if (rssi > aniState->rssiThrLow) {
  177. if (aniState->ofdmWeakSigDetectOff)
  178. ath9k_hw_ani_control(ah,
  179. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  180. true);
  181. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  182. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  183. aniState->firstepLevel + 1);
  184. return;
  185. } else {
  186. if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
  187. !conf_is_ht(conf)) {
  188. if (!aniState->ofdmWeakSigDetectOff)
  189. ath9k_hw_ani_control(ah,
  190. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  191. false);
  192. if (aniState->firstepLevel > 0)
  193. ath9k_hw_ani_control(ah,
  194. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  195. return;
  196. }
  197. }
  198. }
  199. static void ath9k_hw_ani_cck_err_trigger_old(struct ath_hw *ah)
  200. {
  201. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  202. struct ar5416AniState *aniState;
  203. int32_t rssi;
  204. aniState = &ah->curchan->ani;
  205. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  206. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  207. aniState->noiseImmunityLevel + 1)) {
  208. return;
  209. }
  210. }
  211. if (ah->opmode == NL80211_IFTYPE_AP) {
  212. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  213. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  214. aniState->firstepLevel + 1);
  215. }
  216. return;
  217. }
  218. rssi = BEACON_RSSI(ah);
  219. if (rssi > aniState->rssiThrLow) {
  220. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  221. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  222. aniState->firstepLevel + 1);
  223. } else {
  224. if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
  225. !conf_is_ht(conf)) {
  226. if (aniState->firstepLevel > 0)
  227. ath9k_hw_ani_control(ah,
  228. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  229. }
  230. }
  231. }
  232. /* Adjust the OFDM Noise Immunity Level */
  233. static void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel)
  234. {
  235. struct ar5416AniState *aniState = &ah->curchan->ani;
  236. struct ath_common *common = ath9k_hw_common(ah);
  237. const struct ani_ofdm_level_entry *entry_ofdm;
  238. const struct ani_cck_level_entry *entry_cck;
  239. aniState->noiseFloor = BEACON_RSSI(ah);
  240. ath_dbg(common, ANI, "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  241. aniState->ofdmNoiseImmunityLevel,
  242. immunityLevel, aniState->noiseFloor,
  243. aniState->rssiThrLow, aniState->rssiThrHigh);
  244. if (aniState->update_ani)
  245. aniState->ofdmNoiseImmunityLevel = immunityLevel;
  246. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  247. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  248. if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
  249. ath9k_hw_ani_control(ah,
  250. ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  251. entry_ofdm->spur_immunity_level);
  252. if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
  253. entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
  254. ath9k_hw_ani_control(ah,
  255. ATH9K_ANI_FIRSTEP_LEVEL,
  256. entry_ofdm->fir_step_level);
  257. if ((ah->opmode != NL80211_IFTYPE_STATION &&
  258. ah->opmode != NL80211_IFTYPE_ADHOC) ||
  259. aniState->noiseFloor <= aniState->rssiThrHigh) {
  260. if (aniState->ofdmWeakSigDetectOff)
  261. /* force on ofdm weak sig detect */
  262. ath9k_hw_ani_control(ah,
  263. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  264. true);
  265. else if (aniState->ofdmWeakSigDetectOff ==
  266. entry_ofdm->ofdm_weak_signal_on)
  267. ath9k_hw_ani_control(ah,
  268. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  269. entry_ofdm->ofdm_weak_signal_on);
  270. }
  271. }
  272. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  273. {
  274. struct ar5416AniState *aniState;
  275. if (!DO_ANI(ah))
  276. return;
  277. if (!use_new_ani(ah)) {
  278. ath9k_hw_ani_ofdm_err_trigger_old(ah);
  279. return;
  280. }
  281. aniState = &ah->curchan->ani;
  282. if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
  283. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1);
  284. }
  285. /*
  286. * Set the ANI settings to match an CCK level.
  287. */
  288. static void ath9k_hw_set_cck_nil(struct ath_hw *ah, u_int8_t immunityLevel)
  289. {
  290. struct ar5416AniState *aniState = &ah->curchan->ani;
  291. struct ath_common *common = ath9k_hw_common(ah);
  292. const struct ani_ofdm_level_entry *entry_ofdm;
  293. const struct ani_cck_level_entry *entry_cck;
  294. aniState->noiseFloor = BEACON_RSSI(ah);
  295. ath_dbg(common, ANI, "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  296. aniState->cckNoiseImmunityLevel, immunityLevel,
  297. aniState->noiseFloor, aniState->rssiThrLow,
  298. aniState->rssiThrHigh);
  299. if ((ah->opmode == NL80211_IFTYPE_STATION ||
  300. ah->opmode == NL80211_IFTYPE_ADHOC) &&
  301. aniState->noiseFloor <= aniState->rssiThrLow &&
  302. immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
  303. immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
  304. if (aniState->update_ani)
  305. aniState->cckNoiseImmunityLevel = immunityLevel;
  306. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  307. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  308. if (aniState->firstepLevel != entry_cck->fir_step_level &&
  309. entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
  310. ath9k_hw_ani_control(ah,
  311. ATH9K_ANI_FIRSTEP_LEVEL,
  312. entry_cck->fir_step_level);
  313. /* Skip MRC CCK for pre AR9003 families */
  314. if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah))
  315. return;
  316. if (aniState->mrcCCKOff == entry_cck->mrc_cck_on)
  317. ath9k_hw_ani_control(ah,
  318. ATH9K_ANI_MRC_CCK,
  319. entry_cck->mrc_cck_on);
  320. }
  321. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  322. {
  323. struct ar5416AniState *aniState;
  324. if (!DO_ANI(ah))
  325. return;
  326. if (!use_new_ani(ah)) {
  327. ath9k_hw_ani_cck_err_trigger_old(ah);
  328. return;
  329. }
  330. aniState = &ah->curchan->ani;
  331. if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
  332. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1);
  333. }
  334. static void ath9k_hw_ani_lower_immunity_old(struct ath_hw *ah)
  335. {
  336. struct ar5416AniState *aniState;
  337. int32_t rssi;
  338. aniState = &ah->curchan->ani;
  339. if (ah->opmode == NL80211_IFTYPE_AP) {
  340. if (aniState->firstepLevel > 0) {
  341. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  342. aniState->firstepLevel - 1))
  343. return;
  344. }
  345. } else {
  346. rssi = BEACON_RSSI(ah);
  347. if (rssi > aniState->rssiThrHigh) {
  348. /* XXX: Handle me */
  349. } else if (rssi > aniState->rssiThrLow) {
  350. if (aniState->ofdmWeakSigDetectOff) {
  351. if (ath9k_hw_ani_control(ah,
  352. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  353. true) == true)
  354. return;
  355. }
  356. if (aniState->firstepLevel > 0) {
  357. if (ath9k_hw_ani_control(ah,
  358. ATH9K_ANI_FIRSTEP_LEVEL,
  359. aniState->firstepLevel - 1) == true)
  360. return;
  361. }
  362. } else {
  363. if (aniState->firstepLevel > 0) {
  364. if (ath9k_hw_ani_control(ah,
  365. ATH9K_ANI_FIRSTEP_LEVEL,
  366. aniState->firstepLevel - 1) == true)
  367. return;
  368. }
  369. }
  370. }
  371. if (aniState->spurImmunityLevel > 0) {
  372. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  373. aniState->spurImmunityLevel - 1))
  374. return;
  375. }
  376. if (aniState->noiseImmunityLevel > 0) {
  377. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  378. aniState->noiseImmunityLevel - 1);
  379. return;
  380. }
  381. }
  382. /*
  383. * only lower either OFDM or CCK errors per turn
  384. * we lower the other one next time
  385. */
  386. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  387. {
  388. struct ar5416AniState *aniState;
  389. aniState = &ah->curchan->ani;
  390. if (!use_new_ani(ah)) {
  391. ath9k_hw_ani_lower_immunity_old(ah);
  392. return;
  393. }
  394. /* lower OFDM noise immunity */
  395. if (aniState->ofdmNoiseImmunityLevel > 0 &&
  396. (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
  397. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1);
  398. return;
  399. }
  400. /* lower CCK noise immunity */
  401. if (aniState->cckNoiseImmunityLevel > 0)
  402. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1);
  403. }
  404. static void ath9k_ani_reset_old(struct ath_hw *ah, bool is_scanning)
  405. {
  406. struct ar5416AniState *aniState;
  407. struct ath9k_channel *chan = ah->curchan;
  408. struct ath_common *common = ath9k_hw_common(ah);
  409. if (!DO_ANI(ah))
  410. return;
  411. aniState = &ah->curchan->ani;
  412. if (ah->opmode != NL80211_IFTYPE_STATION
  413. && ah->opmode != NL80211_IFTYPE_ADHOC) {
  414. ath_dbg(common, ANI, "Reset ANI state opmode %u\n", ah->opmode);
  415. ah->stats.ast_ani_reset++;
  416. if (ah->opmode == NL80211_IFTYPE_AP) {
  417. /*
  418. * ath9k_hw_ani_control() will only process items set on
  419. * ah->ani_function
  420. */
  421. if (IS_CHAN_2GHZ(chan))
  422. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  423. ATH9K_ANI_FIRSTEP_LEVEL);
  424. else
  425. ah->ani_function = 0;
  426. }
  427. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  428. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  429. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  430. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  431. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  432. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  433. ATH9K_ANI_CCK_WEAK_SIG_THR);
  434. ath9k_ani_restart(ah);
  435. return;
  436. }
  437. if (aniState->noiseImmunityLevel != 0)
  438. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  439. aniState->noiseImmunityLevel);
  440. if (aniState->spurImmunityLevel != 0)
  441. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  442. aniState->spurImmunityLevel);
  443. if (aniState->ofdmWeakSigDetectOff)
  444. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  445. !aniState->ofdmWeakSigDetectOff);
  446. if (aniState->cckWeakSigThreshold)
  447. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  448. aniState->cckWeakSigThreshold);
  449. if (aniState->firstepLevel != 0)
  450. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  451. aniState->firstepLevel);
  452. ath9k_ani_restart(ah);
  453. ENABLE_REGWRITE_BUFFER(ah);
  454. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  455. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  456. REGWRITE_BUFFER_FLUSH(ah);
  457. }
  458. /*
  459. * Restore the ANI parameters in the HAL and reset the statistics.
  460. * This routine should be called for every hardware reset and for
  461. * every channel change.
  462. */
  463. void ath9k_ani_reset(struct ath_hw *ah, bool is_scanning)
  464. {
  465. struct ar5416AniState *aniState = &ah->curchan->ani;
  466. struct ath9k_channel *chan = ah->curchan;
  467. struct ath_common *common = ath9k_hw_common(ah);
  468. if (!DO_ANI(ah))
  469. return;
  470. if (!use_new_ani(ah))
  471. return ath9k_ani_reset_old(ah, is_scanning);
  472. BUG_ON(aniState == NULL);
  473. ah->stats.ast_ani_reset++;
  474. /* only allow a subset of functions in AP mode */
  475. if (ah->opmode == NL80211_IFTYPE_AP) {
  476. if (IS_CHAN_2GHZ(chan)) {
  477. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  478. ATH9K_ANI_FIRSTEP_LEVEL);
  479. if (AR_SREV_9300_20_OR_LATER(ah))
  480. ah->ani_function |= ATH9K_ANI_MRC_CCK;
  481. } else
  482. ah->ani_function = 0;
  483. }
  484. /* always allow mode (on/off) to be controlled */
  485. ah->ani_function |= ATH9K_ANI_MODE;
  486. if (is_scanning ||
  487. (ah->opmode != NL80211_IFTYPE_STATION &&
  488. ah->opmode != NL80211_IFTYPE_ADHOC)) {
  489. /*
  490. * If we're scanning or in AP mode, the defaults (ini)
  491. * should be in place. For an AP we assume the historical
  492. * levels for this channel are probably outdated so start
  493. * from defaults instead.
  494. */
  495. if (aniState->ofdmNoiseImmunityLevel !=
  496. ATH9K_ANI_OFDM_DEF_LEVEL ||
  497. aniState->cckNoiseImmunityLevel !=
  498. ATH9K_ANI_CCK_DEF_LEVEL) {
  499. ath_dbg(common, ANI,
  500. "Restore defaults: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  501. ah->opmode,
  502. chan->channel,
  503. chan->channelFlags,
  504. is_scanning,
  505. aniState->ofdmNoiseImmunityLevel,
  506. aniState->cckNoiseImmunityLevel);
  507. aniState->update_ani = false;
  508. ath9k_hw_set_ofdm_nil(ah, ATH9K_ANI_OFDM_DEF_LEVEL);
  509. ath9k_hw_set_cck_nil(ah, ATH9K_ANI_CCK_DEF_LEVEL);
  510. }
  511. } else {
  512. /*
  513. * restore historical levels for this channel
  514. */
  515. ath_dbg(common, ANI,
  516. "Restore history: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  517. ah->opmode,
  518. chan->channel,
  519. chan->channelFlags,
  520. is_scanning,
  521. aniState->ofdmNoiseImmunityLevel,
  522. aniState->cckNoiseImmunityLevel);
  523. aniState->update_ani = true;
  524. ath9k_hw_set_ofdm_nil(ah,
  525. aniState->ofdmNoiseImmunityLevel);
  526. ath9k_hw_set_cck_nil(ah,
  527. aniState->cckNoiseImmunityLevel);
  528. }
  529. /*
  530. * enable phy counters if hw supports or if not, enable phy
  531. * interrupts (so we can count each one)
  532. */
  533. ath9k_ani_restart(ah);
  534. ENABLE_REGWRITE_BUFFER(ah);
  535. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  536. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  537. REGWRITE_BUFFER_FLUSH(ah);
  538. }
  539. static bool ath9k_hw_ani_read_counters(struct ath_hw *ah)
  540. {
  541. struct ath_common *common = ath9k_hw_common(ah);
  542. struct ar5416AniState *aniState = &ah->curchan->ani;
  543. u32 ofdm_base = 0;
  544. u32 cck_base = 0;
  545. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  546. u32 phyCnt1, phyCnt2;
  547. int32_t listenTime;
  548. ath_hw_cycle_counters_update(common);
  549. listenTime = ath_hw_get_listen_time(common);
  550. if (listenTime <= 0) {
  551. ah->stats.ast_ani_lneg_or_lzero++;
  552. ath9k_ani_restart(ah);
  553. return false;
  554. }
  555. if (!use_new_ani(ah)) {
  556. ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
  557. cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
  558. }
  559. aniState->listenTime += listenTime;
  560. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  561. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  562. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  563. if (!use_new_ani(ah) && (phyCnt1 < ofdm_base || phyCnt2 < cck_base)) {
  564. if (phyCnt1 < ofdm_base) {
  565. ath_dbg(common, ANI,
  566. "phyCnt1 0x%x, resetting counter value to 0x%x\n",
  567. phyCnt1, ofdm_base);
  568. REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
  569. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  570. AR_PHY_ERR_OFDM_TIMING);
  571. }
  572. if (phyCnt2 < cck_base) {
  573. ath_dbg(common, ANI,
  574. "phyCnt2 0x%x, resetting counter value to 0x%x\n",
  575. phyCnt2, cck_base);
  576. REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
  577. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  578. AR_PHY_ERR_CCK_TIMING);
  579. }
  580. return false;
  581. }
  582. ofdmPhyErrCnt = phyCnt1 - ofdm_base;
  583. ah->stats.ast_ani_ofdmerrs +=
  584. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  585. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  586. cckPhyErrCnt = phyCnt2 - cck_base;
  587. ah->stats.ast_ani_cckerrs +=
  588. cckPhyErrCnt - aniState->cckPhyErrCount;
  589. aniState->cckPhyErrCount = cckPhyErrCnt;
  590. return true;
  591. }
  592. void ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan)
  593. {
  594. struct ar5416AniState *aniState;
  595. struct ath_common *common = ath9k_hw_common(ah);
  596. u32 ofdmPhyErrRate, cckPhyErrRate;
  597. if (!DO_ANI(ah))
  598. return;
  599. aniState = &ah->curchan->ani;
  600. if (WARN_ON(!aniState))
  601. return;
  602. if (!ath9k_hw_ani_read_counters(ah))
  603. return;
  604. ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
  605. aniState->listenTime;
  606. cckPhyErrRate = aniState->cckPhyErrCount * 1000 /
  607. aniState->listenTime;
  608. ath_dbg(common, ANI,
  609. "listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
  610. aniState->listenTime,
  611. aniState->ofdmNoiseImmunityLevel,
  612. ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
  613. cckPhyErrRate, aniState->ofdmsTurn);
  614. if (aniState->listenTime > 5 * ah->aniperiod) {
  615. if (ofdmPhyErrRate <= ah->config.ofdm_trig_low &&
  616. cckPhyErrRate <= ah->config.cck_trig_low) {
  617. ath9k_hw_ani_lower_immunity(ah);
  618. aniState->ofdmsTurn = !aniState->ofdmsTurn;
  619. }
  620. ath9k_ani_restart(ah);
  621. } else if (aniState->listenTime > ah->aniperiod) {
  622. /* check to see if need to raise immunity */
  623. if (ofdmPhyErrRate > ah->config.ofdm_trig_high &&
  624. (cckPhyErrRate <= ah->config.cck_trig_high ||
  625. aniState->ofdmsTurn)) {
  626. ath9k_hw_ani_ofdm_err_trigger(ah);
  627. ath9k_ani_restart(ah);
  628. aniState->ofdmsTurn = false;
  629. } else if (cckPhyErrRate > ah->config.cck_trig_high) {
  630. ath9k_hw_ani_cck_err_trigger(ah);
  631. ath9k_ani_restart(ah);
  632. aniState->ofdmsTurn = true;
  633. }
  634. }
  635. }
  636. EXPORT_SYMBOL(ath9k_hw_ani_monitor);
  637. void ath9k_enable_mib_counters(struct ath_hw *ah)
  638. {
  639. struct ath_common *common = ath9k_hw_common(ah);
  640. ath_dbg(common, ANI, "Enable MIB counters\n");
  641. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  642. ENABLE_REGWRITE_BUFFER(ah);
  643. REG_WRITE(ah, AR_FILT_OFDM, 0);
  644. REG_WRITE(ah, AR_FILT_CCK, 0);
  645. REG_WRITE(ah, AR_MIBC,
  646. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  647. & 0x0f);
  648. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  649. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  650. REGWRITE_BUFFER_FLUSH(ah);
  651. }
  652. /* Freeze the MIB counters, get the stats and then clear them */
  653. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  654. {
  655. struct ath_common *common = ath9k_hw_common(ah);
  656. ath_dbg(common, ANI, "Disable MIB counters\n");
  657. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  658. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  659. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  660. REG_WRITE(ah, AR_FILT_OFDM, 0);
  661. REG_WRITE(ah, AR_FILT_CCK, 0);
  662. }
  663. EXPORT_SYMBOL(ath9k_hw_disable_mib_counters);
  664. /*
  665. * Process a MIB interrupt. We may potentially be invoked because
  666. * any of the MIB counters overflow/trigger so don't assume we're
  667. * here because a PHY error counter triggered.
  668. */
  669. void ath9k_hw_proc_mib_event(struct ath_hw *ah)
  670. {
  671. u32 phyCnt1, phyCnt2;
  672. /* Reset these counters regardless */
  673. REG_WRITE(ah, AR_FILT_OFDM, 0);
  674. REG_WRITE(ah, AR_FILT_CCK, 0);
  675. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  676. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  677. /* Clear the mib counters and save them in the stats */
  678. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  679. if (!DO_ANI(ah)) {
  680. /*
  681. * We must always clear the interrupt cause by
  682. * resetting the phy error regs.
  683. */
  684. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  685. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  686. return;
  687. }
  688. /* NB: these are not reset-on-read */
  689. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  690. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  691. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  692. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  693. if (!use_new_ani(ah))
  694. ath9k_hw_ani_read_counters(ah);
  695. /* NB: always restart to insure the h/w counters are reset */
  696. ath9k_ani_restart(ah);
  697. }
  698. }
  699. EXPORT_SYMBOL(ath9k_hw_proc_mib_event);
  700. void ath9k_hw_ani_setup(struct ath_hw *ah)
  701. {
  702. int i;
  703. static const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  704. static const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  705. static const int coarseLow[] = { -64, -64, -64, -64, -70 };
  706. static const int firpwr[] = { -78, -78, -78, -78, -80 };
  707. for (i = 0; i < 5; i++) {
  708. ah->totalSizeDesired[i] = totalSizeDesired[i];
  709. ah->coarse_high[i] = coarseHigh[i];
  710. ah->coarse_low[i] = coarseLow[i];
  711. ah->firpwr[i] = firpwr[i];
  712. }
  713. }
  714. void ath9k_hw_ani_init(struct ath_hw *ah)
  715. {
  716. struct ath_common *common = ath9k_hw_common(ah);
  717. int i;
  718. ath_dbg(common, ANI, "Initialize ANI\n");
  719. if (use_new_ani(ah)) {
  720. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_NEW;
  721. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_NEW;
  722. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_NEW;
  723. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_NEW;
  724. } else {
  725. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
  726. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
  727. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
  728. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
  729. }
  730. for (i = 0; i < ARRAY_SIZE(ah->channels); i++) {
  731. struct ath9k_channel *chan = &ah->channels[i];
  732. struct ar5416AniState *ani = &chan->ani;
  733. if (use_new_ani(ah)) {
  734. ani->spurImmunityLevel =
  735. ATH9K_ANI_SPUR_IMMUNE_LVL_NEW;
  736. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_NEW;
  737. if (AR_SREV_9300_20_OR_LATER(ah))
  738. ani->mrcCCKOff =
  739. !ATH9K_ANI_ENABLE_MRC_CCK;
  740. else
  741. ani->mrcCCKOff = true;
  742. ani->ofdmsTurn = true;
  743. } else {
  744. ani->spurImmunityLevel =
  745. ATH9K_ANI_SPUR_IMMUNE_LVL_OLD;
  746. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_OLD;
  747. ani->cckWeakSigThreshold =
  748. ATH9K_ANI_CCK_WEAK_SIG_THR;
  749. }
  750. ani->rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  751. ani->rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  752. ani->ofdmWeakSigDetectOff =
  753. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  754. ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
  755. ani->ofdmNoiseImmunityLevel = ATH9K_ANI_OFDM_DEF_LEVEL;
  756. ani->update_ani = false;
  757. }
  758. /*
  759. * since we expect some ongoing maintenance on the tables, let's sanity
  760. * check here default level should not modify INI setting.
  761. */
  762. if (use_new_ani(ah)) {
  763. ah->aniperiod = ATH9K_ANI_PERIOD_NEW;
  764. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_NEW;
  765. } else {
  766. ah->aniperiod = ATH9K_ANI_PERIOD_OLD;
  767. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_OLD;
  768. }
  769. if (ah->config.enable_ani)
  770. ah->proc_phyerr |= HAL_PROCESS_ANI;
  771. ath9k_ani_restart(ah);
  772. ath9k_enable_mib_counters(ah);
  773. }