You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ath9k_ani.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Modified for iPXE by Scott K Logan <logans@cottsay.net> July 2011
  5. * Original from Linux kernel 3.0.1
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include "hw.h"
  20. #include "hw-ops.h"
  21. struct ani_ofdm_level_entry {
  22. int spur_immunity_level;
  23. int fir_step_level;
  24. int ofdm_weak_signal_on;
  25. };
  26. /* values here are relative to the INI */
  27. /*
  28. * Legend:
  29. *
  30. * SI: Spur immunity
  31. * FS: FIR Step
  32. * WS: OFDM / CCK Weak Signal detection
  33. * MRC-CCK: Maximal Ratio Combining for CCK
  34. */
  35. static const struct ani_ofdm_level_entry ofdm_level_table[] = {
  36. /* SI FS WS */
  37. { 0, 0, 1 }, /* lvl 0 */
  38. { 1, 1, 1 }, /* lvl 1 */
  39. { 2, 2, 1 }, /* lvl 2 */
  40. { 3, 2, 1 }, /* lvl 3 (default) */
  41. { 4, 3, 1 }, /* lvl 4 */
  42. { 5, 4, 1 }, /* lvl 5 */
  43. { 6, 5, 1 }, /* lvl 6 */
  44. { 7, 6, 1 }, /* lvl 7 */
  45. { 7, 7, 1 }, /* lvl 8 */
  46. { 7, 8, 0 } /* lvl 9 */
  47. };
  48. #define ATH9K_ANI_OFDM_NUM_LEVEL \
  49. ARRAY_SIZE(ofdm_level_table)
  50. #define ATH9K_ANI_OFDM_MAX_LEVEL \
  51. (ATH9K_ANI_OFDM_NUM_LEVEL-1)
  52. #define ATH9K_ANI_OFDM_DEF_LEVEL \
  53. 3 /* default level - matches the INI settings */
  54. /*
  55. * MRC (Maximal Ratio Combining) has always been used with multi-antenna ofdm.
  56. * With OFDM for single stream you just add up all antenna inputs, you're
  57. * only interested in what you get after FFT. Signal aligment is also not
  58. * required for OFDM because any phase difference adds up in the frequency
  59. * domain.
  60. *
  61. * MRC requires extra work for use with CCK. You need to align the antenna
  62. * signals from the different antenna before you can add the signals together.
  63. * You need aligment of signals as CCK is in time domain, so addition can cancel
  64. * your signal completely if phase is 180 degrees (think of adding sine waves).
  65. * You also need to remove noise before the addition and this is where ANI
  66. * MRC CCK comes into play. One of the antenna inputs may be stronger but
  67. * lower SNR, so just adding after alignment can be dangerous.
  68. *
  69. * Regardless of alignment in time, the antenna signals add constructively after
  70. * FFT and improve your reception. For more information:
  71. *
  72. * http://en.wikipedia.org/wiki/Maximal-ratio_combining
  73. */
  74. struct ani_cck_level_entry {
  75. int fir_step_level;
  76. int mrc_cck_on;
  77. };
  78. static const struct ani_cck_level_entry cck_level_table[] = {
  79. /* FS MRC-CCK */
  80. { 0, 1 }, /* lvl 0 */
  81. { 1, 1 }, /* lvl 1 */
  82. { 2, 1 }, /* lvl 2 (default) */
  83. { 3, 1 }, /* lvl 3 */
  84. { 4, 0 }, /* lvl 4 */
  85. { 5, 0 }, /* lvl 5 */
  86. { 6, 0 }, /* lvl 6 */
  87. { 7, 0 }, /* lvl 7 (only for high rssi) */
  88. { 8, 0 } /* lvl 8 (only for high rssi) */
  89. };
  90. #define ATH9K_ANI_CCK_NUM_LEVEL \
  91. ARRAY_SIZE(cck_level_table)
  92. #define ATH9K_ANI_CCK_MAX_LEVEL \
  93. (ATH9K_ANI_CCK_NUM_LEVEL-1)
  94. #define ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI \
  95. (ATH9K_ANI_CCK_NUM_LEVEL-3)
  96. #define ATH9K_ANI_CCK_DEF_LEVEL \
  97. 2 /* default level - matches the INI settings */
  98. static int use_new_ani(struct ath_hw *ah)
  99. {
  100. return AR_SREV_9300_20_OR_LATER(ah) || modparam_force_new_ani;
  101. }
  102. static void ath9k_hw_update_mibstats(struct ath_hw *ah,
  103. struct ath9k_mib_stats *stats)
  104. {
  105. stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
  106. stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
  107. stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
  108. stats->rts_good += REG_READ(ah, AR_RTS_OK);
  109. stats->beacons += REG_READ(ah, AR_BEACON_CNT);
  110. }
  111. static void ath9k_ani_restart(struct ath_hw *ah)
  112. {
  113. struct ar5416AniState *aniState;
  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. DBG2("ath9k: "
  124. "Writing ofdmbase=%d cckbase=%d\n", 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 ar5416AniState *aniState;
  138. int32_t rssi;
  139. aniState = &ah->curchan->ani;
  140. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  141. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  142. aniState->noiseImmunityLevel + 1)) {
  143. return;
  144. }
  145. }
  146. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  147. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  148. aniState->spurImmunityLevel + 1)) {
  149. return;
  150. }
  151. }
  152. rssi = BEACON_RSSI(ah);
  153. if (rssi > aniState->rssiThrHigh) {
  154. if (!aniState->ofdmWeakSigDetectOff) {
  155. if (ath9k_hw_ani_control(ah,
  156. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  157. 0)) {
  158. ath9k_hw_ani_control(ah,
  159. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  160. return;
  161. }
  162. }
  163. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  164. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  165. aniState->firstepLevel + 1);
  166. return;
  167. }
  168. } else if (rssi > aniState->rssiThrLow) {
  169. if (aniState->ofdmWeakSigDetectOff)
  170. ath9k_hw_ani_control(ah,
  171. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  172. 1);
  173. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  174. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  175. aniState->firstepLevel + 1);
  176. return;
  177. } else {
  178. if ((ah->dev->channels + ah->dev->channel)->band == NET80211_BAND_2GHZ) {
  179. if (!aniState->ofdmWeakSigDetectOff)
  180. ath9k_hw_ani_control(ah,
  181. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  182. 0);
  183. if (aniState->firstepLevel > 0)
  184. ath9k_hw_ani_control(ah,
  185. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  186. return;
  187. }
  188. }
  189. }
  190. static void ath9k_hw_ani_cck_err_trigger_old(struct ath_hw *ah)
  191. {
  192. struct ar5416AniState *aniState;
  193. int32_t rssi;
  194. aniState = &ah->curchan->ani;
  195. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  196. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  197. aniState->noiseImmunityLevel + 1)) {
  198. return;
  199. }
  200. }
  201. rssi = BEACON_RSSI(ah);
  202. if (rssi > aniState->rssiThrLow) {
  203. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  204. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  205. aniState->firstepLevel + 1);
  206. } else {
  207. if ((ah->dev->channels + ah->dev->channel)->band == NET80211_BAND_2GHZ) {
  208. if (aniState->firstepLevel > 0)
  209. ath9k_hw_ani_control(ah,
  210. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  211. }
  212. }
  213. }
  214. /* Adjust the OFDM Noise Immunity Level */
  215. static void ath9k_hw_set_ofdm_nil(struct ath_hw *ah, u8 immunityLevel)
  216. {
  217. struct ar5416AniState *aniState = &ah->curchan->ani;
  218. const struct ani_ofdm_level_entry *entry_ofdm;
  219. const struct ani_cck_level_entry *entry_cck;
  220. aniState->noiseFloor = BEACON_RSSI(ah);
  221. DBG2("ath9k: "
  222. "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  223. aniState->ofdmNoiseImmunityLevel,
  224. immunityLevel, aniState->noiseFloor,
  225. aniState->rssiThrLow, aniState->rssiThrHigh);
  226. aniState->ofdmNoiseImmunityLevel = immunityLevel;
  227. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  228. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  229. if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
  230. ath9k_hw_ani_control(ah,
  231. ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  232. entry_ofdm->spur_immunity_level);
  233. if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
  234. entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
  235. ath9k_hw_ani_control(ah,
  236. ATH9K_ANI_FIRSTEP_LEVEL,
  237. entry_ofdm->fir_step_level);
  238. }
  239. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  240. {
  241. struct ar5416AniState *aniState;
  242. if (!DO_ANI(ah))
  243. return;
  244. if (!use_new_ani(ah)) {
  245. ath9k_hw_ani_ofdm_err_trigger_old(ah);
  246. return;
  247. }
  248. aniState = &ah->curchan->ani;
  249. if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
  250. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1);
  251. }
  252. /*
  253. * Set the ANI settings to match an CCK level.
  254. */
  255. static void ath9k_hw_set_cck_nil(struct ath_hw *ah, uint8_t immunityLevel)
  256. {
  257. struct ar5416AniState *aniState = &ah->curchan->ani;
  258. const struct ani_ofdm_level_entry *entry_ofdm;
  259. const struct ani_cck_level_entry *entry_cck;
  260. aniState->noiseFloor = BEACON_RSSI(ah);
  261. DBG2("ath9k: "
  262. "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  263. aniState->cckNoiseImmunityLevel, immunityLevel,
  264. aniState->noiseFloor, aniState->rssiThrLow,
  265. aniState->rssiThrHigh);
  266. if (aniState->noiseFloor <= (unsigned int)aniState->rssiThrLow &&
  267. immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
  268. immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
  269. aniState->cckNoiseImmunityLevel = immunityLevel;
  270. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  271. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  272. if (aniState->firstepLevel != entry_cck->fir_step_level &&
  273. entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
  274. ath9k_hw_ani_control(ah,
  275. ATH9K_ANI_FIRSTEP_LEVEL,
  276. entry_cck->fir_step_level);
  277. /* Skip MRC CCK for pre AR9003 families */
  278. if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah))
  279. return;
  280. if (aniState->mrcCCKOff == entry_cck->mrc_cck_on)
  281. ath9k_hw_ani_control(ah,
  282. ATH9K_ANI_MRC_CCK,
  283. entry_cck->mrc_cck_on);
  284. }
  285. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  286. {
  287. struct ar5416AniState *aniState;
  288. if (!DO_ANI(ah))
  289. return;
  290. if (!use_new_ani(ah)) {
  291. ath9k_hw_ani_cck_err_trigger_old(ah);
  292. return;
  293. }
  294. aniState = &ah->curchan->ani;
  295. if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
  296. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1);
  297. }
  298. static void ath9k_hw_ani_lower_immunity_old(struct ath_hw *ah)
  299. {
  300. struct ar5416AniState *aniState;
  301. int32_t rssi;
  302. aniState = &ah->curchan->ani;
  303. rssi = BEACON_RSSI(ah);
  304. if (rssi > aniState->rssiThrHigh) {
  305. /* XXX: Handle me */
  306. } else if (rssi > aniState->rssiThrLow) {
  307. if (aniState->ofdmWeakSigDetectOff) {
  308. if (ath9k_hw_ani_control(ah,
  309. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  310. 1) == 1)
  311. return;
  312. }
  313. if (aniState->firstepLevel > 0) {
  314. if (ath9k_hw_ani_control(ah,
  315. ATH9K_ANI_FIRSTEP_LEVEL,
  316. aniState->firstepLevel - 1) == 1)
  317. return;
  318. }
  319. } else {
  320. if (aniState->firstepLevel > 0) {
  321. if (ath9k_hw_ani_control(ah,
  322. ATH9K_ANI_FIRSTEP_LEVEL,
  323. aniState->firstepLevel - 1) == 1)
  324. return;
  325. }
  326. }
  327. if (aniState->spurImmunityLevel > 0) {
  328. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  329. aniState->spurImmunityLevel - 1))
  330. return;
  331. }
  332. if (aniState->noiseImmunityLevel > 0) {
  333. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  334. aniState->noiseImmunityLevel - 1);
  335. return;
  336. }
  337. }
  338. /*
  339. * only lower either OFDM or CCK errors per turn
  340. * we lower the other one next time
  341. */
  342. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  343. {
  344. struct ar5416AniState *aniState;
  345. aniState = &ah->curchan->ani;
  346. if (!use_new_ani(ah)) {
  347. ath9k_hw_ani_lower_immunity_old(ah);
  348. return;
  349. }
  350. /* lower OFDM noise immunity */
  351. if (aniState->ofdmNoiseImmunityLevel > 0 &&
  352. (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
  353. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1);
  354. return;
  355. }
  356. /* lower CCK noise immunity */
  357. if (aniState->cckNoiseImmunityLevel > 0)
  358. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1);
  359. }
  360. static void ath9k_ani_reset_old(struct ath_hw *ah)
  361. {
  362. struct ar5416AniState *aniState;
  363. if (!DO_ANI(ah))
  364. return;
  365. aniState = &ah->curchan->ani;
  366. if (aniState->noiseImmunityLevel != 0)
  367. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  368. aniState->noiseImmunityLevel);
  369. if (aniState->spurImmunityLevel != 0)
  370. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  371. aniState->spurImmunityLevel);
  372. if (aniState->ofdmWeakSigDetectOff)
  373. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  374. !aniState->ofdmWeakSigDetectOff);
  375. if (aniState->cckWeakSigThreshold)
  376. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  377. aniState->cckWeakSigThreshold);
  378. if (aniState->firstepLevel != 0)
  379. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  380. aniState->firstepLevel);
  381. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
  382. ~ATH9K_RX_FILTER_PHYERR);
  383. ath9k_ani_restart(ah);
  384. ENABLE_REGWRITE_BUFFER(ah);
  385. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  386. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  387. REGWRITE_BUFFER_FLUSH(ah);
  388. }
  389. /*
  390. * Restore the ANI parameters in the HAL and reset the statistics.
  391. * This routine should be called for every hardware reset and for
  392. * every channel change.
  393. */
  394. void ath9k_ani_reset(struct ath_hw *ah, int is_scanning)
  395. {
  396. struct ar5416AniState *aniState = &ah->curchan->ani;
  397. struct ath9k_channel *chan = ah->curchan;
  398. if (!DO_ANI(ah))
  399. return;
  400. if (!use_new_ani(ah))
  401. return ath9k_ani_reset_old(ah);
  402. ah->stats.ast_ani_reset++;
  403. /* always allow mode (on/off) to be controlled */
  404. ah->ani_function |= ATH9K_ANI_MODE;
  405. if (is_scanning) {
  406. /*
  407. * If we're scanning or in AP mode, the defaults (ini)
  408. * should be in place. For an AP we assume the historical
  409. * levels for this channel are probably outdated so start
  410. * from defaults instead.
  411. */
  412. if (aniState->ofdmNoiseImmunityLevel !=
  413. ATH9K_ANI_OFDM_DEF_LEVEL ||
  414. aniState->cckNoiseImmunityLevel !=
  415. ATH9K_ANI_CCK_DEF_LEVEL) {
  416. DBG("ath9k: "
  417. "Restore defaults: chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  418. chan->channel,
  419. chan->channelFlags,
  420. is_scanning,
  421. aniState->ofdmNoiseImmunityLevel,
  422. aniState->cckNoiseImmunityLevel);
  423. ath9k_hw_set_ofdm_nil(ah, ATH9K_ANI_OFDM_DEF_LEVEL);
  424. ath9k_hw_set_cck_nil(ah, ATH9K_ANI_CCK_DEF_LEVEL);
  425. }
  426. } else {
  427. /*
  428. * restore historical levels for this channel
  429. */
  430. DBG2("ath9k: "
  431. "Restore history: chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  432. chan->channel,
  433. chan->channelFlags,
  434. is_scanning,
  435. aniState->ofdmNoiseImmunityLevel,
  436. aniState->cckNoiseImmunityLevel);
  437. ath9k_hw_set_ofdm_nil(ah,
  438. aniState->ofdmNoiseImmunityLevel);
  439. ath9k_hw_set_cck_nil(ah,
  440. aniState->cckNoiseImmunityLevel);
  441. }
  442. /*
  443. * enable phy counters if hw supports or if not, enable phy
  444. * interrupts (so we can count each one)
  445. */
  446. ath9k_ani_restart(ah);
  447. ENABLE_REGWRITE_BUFFER(ah);
  448. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  449. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  450. REGWRITE_BUFFER_FLUSH(ah);
  451. }
  452. static int ath9k_hw_ani_read_counters(struct ath_hw *ah)
  453. {
  454. struct ath_common *common = ath9k_hw_common(ah);
  455. struct ar5416AniState *aniState = &ah->curchan->ani;
  456. u32 ofdm_base = 0;
  457. u32 cck_base = 0;
  458. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  459. u32 phyCnt1, phyCnt2;
  460. int32_t listenTime;
  461. ath_hw_cycle_counters_update(common);
  462. listenTime = ath_hw_get_listen_time(common);
  463. if (listenTime <= 0) {
  464. ah->stats.ast_ani_lneg++;
  465. ath9k_ani_restart(ah);
  466. return 0;
  467. }
  468. if (!use_new_ani(ah)) {
  469. ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
  470. cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
  471. }
  472. aniState->listenTime += listenTime;
  473. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  474. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  475. if (!use_new_ani(ah) && (phyCnt1 < ofdm_base || phyCnt2 < cck_base)) {
  476. if (phyCnt1 < ofdm_base) {
  477. DBG2("ath9k: "
  478. "phyCnt1 0x%x, resetting counter value to 0x%x\n",
  479. phyCnt1, ofdm_base);
  480. REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
  481. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  482. AR_PHY_ERR_OFDM_TIMING);
  483. }
  484. if (phyCnt2 < cck_base) {
  485. DBG2("ath9k: "
  486. "phyCnt2 0x%x, resetting counter value to 0x%x\n",
  487. phyCnt2, cck_base);
  488. REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
  489. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  490. AR_PHY_ERR_CCK_TIMING);
  491. }
  492. return 0;
  493. }
  494. ofdmPhyErrCnt = phyCnt1 - ofdm_base;
  495. ah->stats.ast_ani_ofdmerrs +=
  496. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  497. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  498. cckPhyErrCnt = phyCnt2 - cck_base;
  499. ah->stats.ast_ani_cckerrs +=
  500. cckPhyErrCnt - aniState->cckPhyErrCount;
  501. aniState->cckPhyErrCount = cckPhyErrCnt;
  502. return 1;
  503. }
  504. void ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan __unused)
  505. {
  506. struct ar5416AniState *aniState;
  507. u32 ofdmPhyErrRate, cckPhyErrRate;
  508. if (!DO_ANI(ah))
  509. return;
  510. aniState = &ah->curchan->ani;
  511. if (!aniState)
  512. return;
  513. if (!ath9k_hw_ani_read_counters(ah))
  514. return;
  515. ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
  516. aniState->listenTime;
  517. cckPhyErrRate = aniState->cckPhyErrCount * 1000 /
  518. aniState->listenTime;
  519. DBG2("ath9k: "
  520. "listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
  521. aniState->listenTime,
  522. aniState->ofdmNoiseImmunityLevel,
  523. ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
  524. cckPhyErrRate, aniState->ofdmsTurn);
  525. if (aniState->listenTime > 5 * ah->aniperiod) {
  526. if (ofdmPhyErrRate <= ah->config.ofdm_trig_low &&
  527. cckPhyErrRate <= ah->config.cck_trig_low) {
  528. ath9k_hw_ani_lower_immunity(ah);
  529. aniState->ofdmsTurn = !aniState->ofdmsTurn;
  530. }
  531. ath9k_ani_restart(ah);
  532. } else if (aniState->listenTime > ah->aniperiod) {
  533. /* check to see if need to raise immunity */
  534. if (ofdmPhyErrRate > ah->config.ofdm_trig_high &&
  535. (cckPhyErrRate <= ah->config.cck_trig_high ||
  536. aniState->ofdmsTurn)) {
  537. ath9k_hw_ani_ofdm_err_trigger(ah);
  538. ath9k_ani_restart(ah);
  539. aniState->ofdmsTurn = 0;
  540. } else if (cckPhyErrRate > ah->config.cck_trig_high) {
  541. ath9k_hw_ani_cck_err_trigger(ah);
  542. ath9k_ani_restart(ah);
  543. aniState->ofdmsTurn = 1;
  544. }
  545. }
  546. }
  547. void ath9k_hw_ani_setup(struct ath_hw *ah)
  548. {
  549. int i;
  550. static const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  551. static const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  552. static const int coarseLow[] = { -64, -64, -64, -64, -70 };
  553. static const int firpwr[] = { -78, -78, -78, -78, -80 };
  554. for (i = 0; i < 5; i++) {
  555. ah->totalSizeDesired[i] = totalSizeDesired[i];
  556. ah->coarse_high[i] = coarseHigh[i];
  557. ah->coarse_low[i] = coarseLow[i];
  558. ah->firpwr[i] = firpwr[i];
  559. }
  560. }
  561. void ath9k_hw_ani_init(struct ath_hw *ah)
  562. {
  563. unsigned int i;
  564. DBG2("ath9k: Initialize ANI\n");
  565. if (use_new_ani(ah)) {
  566. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_NEW;
  567. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_NEW;
  568. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_NEW;
  569. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_NEW;
  570. } else {
  571. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
  572. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
  573. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
  574. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
  575. }
  576. for (i = 0; i < ARRAY_SIZE(ah->channels); i++) {
  577. struct ath9k_channel *chan = &ah->channels[i];
  578. struct ar5416AniState *ani = &chan->ani;
  579. if (use_new_ani(ah)) {
  580. ani->spurImmunityLevel =
  581. ATH9K_ANI_SPUR_IMMUNE_LVL_NEW;
  582. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_NEW;
  583. if (AR_SREV_9300_20_OR_LATER(ah))
  584. ani->mrcCCKOff =
  585. !ATH9K_ANI_ENABLE_MRC_CCK;
  586. else
  587. ani->mrcCCKOff = 1;
  588. ani->ofdmsTurn = 1;
  589. } else {
  590. ani->spurImmunityLevel =
  591. ATH9K_ANI_SPUR_IMMUNE_LVL_OLD;
  592. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_OLD;
  593. ani->cckWeakSigThreshold =
  594. ATH9K_ANI_CCK_WEAK_SIG_THR;
  595. }
  596. ani->rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  597. ani->rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  598. ani->ofdmWeakSigDetectOff =
  599. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  600. ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
  601. }
  602. /*
  603. * since we expect some ongoing maintenance on the tables, let's sanity
  604. * check here default level should not modify INI setting.
  605. */
  606. if (use_new_ani(ah)) {
  607. ah->aniperiod = ATH9K_ANI_PERIOD_NEW;
  608. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_NEW;
  609. } else {
  610. ah->aniperiod = ATH9K_ANI_PERIOD_OLD;
  611. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_OLD;
  612. }
  613. if (ah->config.enable_ani)
  614. ah->proc_phyerr |= HAL_PROCESS_ANI;
  615. ath9k_ani_restart(ah);
  616. }