ani.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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, ATH_DBG_ANI,
  124. "Writing ofdmbase=%u cckbase=%u\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 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, ATH_DBG_ANI,
  241. "**** ofdmlevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  242. aniState->ofdmNoiseImmunityLevel,
  243. immunityLevel, aniState->noiseFloor,
  244. aniState->rssiThrLow, aniState->rssiThrHigh);
  245. if (aniState->update_ani)
  246. aniState->ofdmNoiseImmunityLevel = immunityLevel;
  247. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  248. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  249. if (aniState->spurImmunityLevel != entry_ofdm->spur_immunity_level)
  250. ath9k_hw_ani_control(ah,
  251. ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  252. entry_ofdm->spur_immunity_level);
  253. if (aniState->firstepLevel != entry_ofdm->fir_step_level &&
  254. entry_ofdm->fir_step_level >= entry_cck->fir_step_level)
  255. ath9k_hw_ani_control(ah,
  256. ATH9K_ANI_FIRSTEP_LEVEL,
  257. entry_ofdm->fir_step_level);
  258. if ((ah->opmode != NL80211_IFTYPE_STATION &&
  259. ah->opmode != NL80211_IFTYPE_ADHOC) ||
  260. aniState->noiseFloor <= aniState->rssiThrHigh) {
  261. if (aniState->ofdmWeakSigDetectOff)
  262. /* force on ofdm weak sig detect */
  263. ath9k_hw_ani_control(ah,
  264. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  265. true);
  266. else if (aniState->ofdmWeakSigDetectOff ==
  267. entry_ofdm->ofdm_weak_signal_on)
  268. ath9k_hw_ani_control(ah,
  269. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  270. entry_ofdm->ofdm_weak_signal_on);
  271. }
  272. }
  273. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  274. {
  275. struct ar5416AniState *aniState;
  276. if (!DO_ANI(ah))
  277. return;
  278. if (!use_new_ani(ah)) {
  279. ath9k_hw_ani_ofdm_err_trigger_old(ah);
  280. return;
  281. }
  282. aniState = &ah->curchan->ani;
  283. if (aniState->ofdmNoiseImmunityLevel < ATH9K_ANI_OFDM_MAX_LEVEL)
  284. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel + 1);
  285. }
  286. /*
  287. * Set the ANI settings to match an CCK level.
  288. */
  289. static void ath9k_hw_set_cck_nil(struct ath_hw *ah, u_int8_t immunityLevel)
  290. {
  291. struct ar5416AniState *aniState = &ah->curchan->ani;
  292. struct ath_common *common = ath9k_hw_common(ah);
  293. const struct ani_ofdm_level_entry *entry_ofdm;
  294. const struct ani_cck_level_entry *entry_cck;
  295. aniState->noiseFloor = BEACON_RSSI(ah);
  296. ath_dbg(common, ATH_DBG_ANI,
  297. "**** ccklevel %d=>%d, rssi=%d[lo=%d hi=%d]\n",
  298. aniState->cckNoiseImmunityLevel, immunityLevel,
  299. aniState->noiseFloor, aniState->rssiThrLow,
  300. aniState->rssiThrHigh);
  301. if ((ah->opmode == NL80211_IFTYPE_STATION ||
  302. ah->opmode == NL80211_IFTYPE_ADHOC) &&
  303. aniState->noiseFloor <= aniState->rssiThrLow &&
  304. immunityLevel > ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI)
  305. immunityLevel = ATH9K_ANI_CCK_MAX_LEVEL_LOW_RSSI;
  306. if (aniState->update_ani)
  307. aniState->cckNoiseImmunityLevel = immunityLevel;
  308. entry_ofdm = &ofdm_level_table[aniState->ofdmNoiseImmunityLevel];
  309. entry_cck = &cck_level_table[aniState->cckNoiseImmunityLevel];
  310. if (aniState->firstepLevel != entry_cck->fir_step_level &&
  311. entry_cck->fir_step_level >= entry_ofdm->fir_step_level)
  312. ath9k_hw_ani_control(ah,
  313. ATH9K_ANI_FIRSTEP_LEVEL,
  314. entry_cck->fir_step_level);
  315. /* Skip MRC CCK for pre AR9003 families */
  316. if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9485(ah))
  317. return;
  318. if (aniState->mrcCCKOff == entry_cck->mrc_cck_on)
  319. ath9k_hw_ani_control(ah,
  320. ATH9K_ANI_MRC_CCK,
  321. entry_cck->mrc_cck_on);
  322. }
  323. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  324. {
  325. struct ar5416AniState *aniState;
  326. if (!DO_ANI(ah))
  327. return;
  328. if (!use_new_ani(ah)) {
  329. ath9k_hw_ani_cck_err_trigger_old(ah);
  330. return;
  331. }
  332. aniState = &ah->curchan->ani;
  333. if (aniState->cckNoiseImmunityLevel < ATH9K_ANI_CCK_MAX_LEVEL)
  334. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel + 1);
  335. }
  336. static void ath9k_hw_ani_lower_immunity_old(struct ath_hw *ah)
  337. {
  338. struct ar5416AniState *aniState;
  339. int32_t rssi;
  340. aniState = &ah->curchan->ani;
  341. if (ah->opmode == NL80211_IFTYPE_AP) {
  342. if (aniState->firstepLevel > 0) {
  343. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  344. aniState->firstepLevel - 1))
  345. return;
  346. }
  347. } else {
  348. rssi = BEACON_RSSI(ah);
  349. if (rssi > aniState->rssiThrHigh) {
  350. /* XXX: Handle me */
  351. } else if (rssi > aniState->rssiThrLow) {
  352. if (aniState->ofdmWeakSigDetectOff) {
  353. if (ath9k_hw_ani_control(ah,
  354. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  355. true) == true)
  356. return;
  357. }
  358. if (aniState->firstepLevel > 0) {
  359. if (ath9k_hw_ani_control(ah,
  360. ATH9K_ANI_FIRSTEP_LEVEL,
  361. aniState->firstepLevel - 1) == true)
  362. return;
  363. }
  364. } else {
  365. if (aniState->firstepLevel > 0) {
  366. if (ath9k_hw_ani_control(ah,
  367. ATH9K_ANI_FIRSTEP_LEVEL,
  368. aniState->firstepLevel - 1) == true)
  369. return;
  370. }
  371. }
  372. }
  373. if (aniState->spurImmunityLevel > 0) {
  374. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  375. aniState->spurImmunityLevel - 1))
  376. return;
  377. }
  378. if (aniState->noiseImmunityLevel > 0) {
  379. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  380. aniState->noiseImmunityLevel - 1);
  381. return;
  382. }
  383. }
  384. /*
  385. * only lower either OFDM or CCK errors per turn
  386. * we lower the other one next time
  387. */
  388. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  389. {
  390. struct ar5416AniState *aniState;
  391. aniState = &ah->curchan->ani;
  392. if (!use_new_ani(ah)) {
  393. ath9k_hw_ani_lower_immunity_old(ah);
  394. return;
  395. }
  396. /* lower OFDM noise immunity */
  397. if (aniState->ofdmNoiseImmunityLevel > 0 &&
  398. (aniState->ofdmsTurn || aniState->cckNoiseImmunityLevel == 0)) {
  399. ath9k_hw_set_ofdm_nil(ah, aniState->ofdmNoiseImmunityLevel - 1);
  400. return;
  401. }
  402. /* lower CCK noise immunity */
  403. if (aniState->cckNoiseImmunityLevel > 0)
  404. ath9k_hw_set_cck_nil(ah, aniState->cckNoiseImmunityLevel - 1);
  405. }
  406. static void ath9k_ani_reset_old(struct ath_hw *ah, bool is_scanning)
  407. {
  408. struct ar5416AniState *aniState;
  409. struct ath9k_channel *chan = ah->curchan;
  410. struct ath_common *common = ath9k_hw_common(ah);
  411. if (!DO_ANI(ah))
  412. return;
  413. aniState = &ah->curchan->ani;
  414. if (ah->opmode != NL80211_IFTYPE_STATION
  415. && ah->opmode != NL80211_IFTYPE_ADHOC) {
  416. ath_dbg(common, ATH_DBG_ANI,
  417. "Reset ANI state opmode %u\n", ah->opmode);
  418. ah->stats.ast_ani_reset++;
  419. if (ah->opmode == NL80211_IFTYPE_AP) {
  420. /*
  421. * ath9k_hw_ani_control() will only process items set on
  422. * ah->ani_function
  423. */
  424. if (IS_CHAN_2GHZ(chan))
  425. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  426. ATH9K_ANI_FIRSTEP_LEVEL);
  427. else
  428. ah->ani_function = 0;
  429. }
  430. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  431. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  432. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  433. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  434. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  435. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  436. ATH9K_ANI_CCK_WEAK_SIG_THR);
  437. ath9k_ani_restart(ah);
  438. return;
  439. }
  440. if (aniState->noiseImmunityLevel != 0)
  441. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  442. aniState->noiseImmunityLevel);
  443. if (aniState->spurImmunityLevel != 0)
  444. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  445. aniState->spurImmunityLevel);
  446. if (aniState->ofdmWeakSigDetectOff)
  447. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  448. !aniState->ofdmWeakSigDetectOff);
  449. if (aniState->cckWeakSigThreshold)
  450. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  451. aniState->cckWeakSigThreshold);
  452. if (aniState->firstepLevel != 0)
  453. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  454. aniState->firstepLevel);
  455. ath9k_ani_restart(ah);
  456. ENABLE_REGWRITE_BUFFER(ah);
  457. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  458. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  459. REGWRITE_BUFFER_FLUSH(ah);
  460. }
  461. /*
  462. * Restore the ANI parameters in the HAL and reset the statistics.
  463. * This routine should be called for every hardware reset and for
  464. * every channel change.
  465. */
  466. void ath9k_ani_reset(struct ath_hw *ah, bool is_scanning)
  467. {
  468. struct ar5416AniState *aniState = &ah->curchan->ani;
  469. struct ath9k_channel *chan = ah->curchan;
  470. struct ath_common *common = ath9k_hw_common(ah);
  471. if (!DO_ANI(ah))
  472. return;
  473. if (!use_new_ani(ah))
  474. return ath9k_ani_reset_old(ah, is_scanning);
  475. BUG_ON(aniState == NULL);
  476. ah->stats.ast_ani_reset++;
  477. /* only allow a subset of functions in AP mode */
  478. if (ah->opmode == NL80211_IFTYPE_AP) {
  479. if (IS_CHAN_2GHZ(chan)) {
  480. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  481. ATH9K_ANI_FIRSTEP_LEVEL);
  482. if (AR_SREV_9300_20_OR_LATER(ah))
  483. ah->ani_function |= ATH9K_ANI_MRC_CCK;
  484. } else
  485. ah->ani_function = 0;
  486. }
  487. /* always allow mode (on/off) to be controlled */
  488. ah->ani_function |= ATH9K_ANI_MODE;
  489. if (is_scanning ||
  490. (ah->opmode != NL80211_IFTYPE_STATION &&
  491. ah->opmode != NL80211_IFTYPE_ADHOC)) {
  492. /*
  493. * If we're scanning or in AP mode, the defaults (ini)
  494. * should be in place. For an AP we assume the historical
  495. * levels for this channel are probably outdated so start
  496. * from defaults instead.
  497. */
  498. if (aniState->ofdmNoiseImmunityLevel !=
  499. ATH9K_ANI_OFDM_DEF_LEVEL ||
  500. aniState->cckNoiseImmunityLevel !=
  501. ATH9K_ANI_CCK_DEF_LEVEL) {
  502. ath_dbg(common, ATH_DBG_ANI,
  503. "Restore defaults: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  504. ah->opmode,
  505. chan->channel,
  506. chan->channelFlags,
  507. is_scanning,
  508. aniState->ofdmNoiseImmunityLevel,
  509. aniState->cckNoiseImmunityLevel);
  510. aniState->update_ani = false;
  511. ath9k_hw_set_ofdm_nil(ah, ATH9K_ANI_OFDM_DEF_LEVEL);
  512. ath9k_hw_set_cck_nil(ah, ATH9K_ANI_CCK_DEF_LEVEL);
  513. }
  514. } else {
  515. /*
  516. * restore historical levels for this channel
  517. */
  518. ath_dbg(common, ATH_DBG_ANI,
  519. "Restore history: opmode %u chan %d Mhz/0x%x is_scanning=%d ofdm:%d cck:%d\n",
  520. ah->opmode,
  521. chan->channel,
  522. chan->channelFlags,
  523. is_scanning,
  524. aniState->ofdmNoiseImmunityLevel,
  525. aniState->cckNoiseImmunityLevel);
  526. aniState->update_ani = true;
  527. ath9k_hw_set_ofdm_nil(ah,
  528. aniState->ofdmNoiseImmunityLevel);
  529. ath9k_hw_set_cck_nil(ah,
  530. aniState->cckNoiseImmunityLevel);
  531. }
  532. /*
  533. * enable phy counters if hw supports or if not, enable phy
  534. * interrupts (so we can count each one)
  535. */
  536. ath9k_ani_restart(ah);
  537. ENABLE_REGWRITE_BUFFER(ah);
  538. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  539. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  540. REGWRITE_BUFFER_FLUSH(ah);
  541. }
  542. static bool ath9k_hw_ani_read_counters(struct ath_hw *ah)
  543. {
  544. struct ath_common *common = ath9k_hw_common(ah);
  545. struct ar5416AniState *aniState = &ah->curchan->ani;
  546. u32 ofdm_base = 0;
  547. u32 cck_base = 0;
  548. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  549. u32 phyCnt1, phyCnt2;
  550. int32_t listenTime;
  551. ath_hw_cycle_counters_update(common);
  552. listenTime = ath_hw_get_listen_time(common);
  553. if (listenTime <= 0) {
  554. ah->stats.ast_ani_lneg_or_lzero++;
  555. ath9k_ani_restart(ah);
  556. return false;
  557. }
  558. if (!use_new_ani(ah)) {
  559. ofdm_base = AR_PHY_COUNTMAX - ah->config.ofdm_trig_high;
  560. cck_base = AR_PHY_COUNTMAX - ah->config.cck_trig_high;
  561. }
  562. aniState->listenTime += listenTime;
  563. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  564. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  565. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  566. if (!use_new_ani(ah) && (phyCnt1 < ofdm_base || phyCnt2 < cck_base)) {
  567. if (phyCnt1 < ofdm_base) {
  568. ath_dbg(common, ATH_DBG_ANI,
  569. "phyCnt1 0x%x, resetting counter value to 0x%x\n",
  570. phyCnt1, ofdm_base);
  571. REG_WRITE(ah, AR_PHY_ERR_1, ofdm_base);
  572. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  573. AR_PHY_ERR_OFDM_TIMING);
  574. }
  575. if (phyCnt2 < cck_base) {
  576. ath_dbg(common, ATH_DBG_ANI,
  577. "phyCnt2 0x%x, resetting counter value to 0x%x\n",
  578. phyCnt2, cck_base);
  579. REG_WRITE(ah, AR_PHY_ERR_2, cck_base);
  580. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  581. AR_PHY_ERR_CCK_TIMING);
  582. }
  583. return false;
  584. }
  585. ofdmPhyErrCnt = phyCnt1 - ofdm_base;
  586. ah->stats.ast_ani_ofdmerrs +=
  587. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  588. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  589. cckPhyErrCnt = phyCnt2 - cck_base;
  590. ah->stats.ast_ani_cckerrs +=
  591. cckPhyErrCnt - aniState->cckPhyErrCount;
  592. aniState->cckPhyErrCount = cckPhyErrCnt;
  593. return true;
  594. }
  595. void ath9k_hw_ani_monitor(struct ath_hw *ah, struct ath9k_channel *chan)
  596. {
  597. struct ar5416AniState *aniState;
  598. struct ath_common *common = ath9k_hw_common(ah);
  599. u32 ofdmPhyErrRate, cckPhyErrRate;
  600. if (!DO_ANI(ah))
  601. return;
  602. aniState = &ah->curchan->ani;
  603. if (WARN_ON(!aniState))
  604. return;
  605. if (!ath9k_hw_ani_read_counters(ah))
  606. return;
  607. ofdmPhyErrRate = aniState->ofdmPhyErrCount * 1000 /
  608. aniState->listenTime;
  609. cckPhyErrRate = aniState->cckPhyErrCount * 1000 /
  610. aniState->listenTime;
  611. ath_dbg(common, ATH_DBG_ANI,
  612. "listenTime=%d OFDM:%d errs=%d/s CCK:%d errs=%d/s ofdm_turn=%d\n",
  613. aniState->listenTime,
  614. aniState->ofdmNoiseImmunityLevel,
  615. ofdmPhyErrRate, aniState->cckNoiseImmunityLevel,
  616. cckPhyErrRate, aniState->ofdmsTurn);
  617. if (aniState->listenTime > 5 * ah->aniperiod) {
  618. if (ofdmPhyErrRate <= ah->config.ofdm_trig_low &&
  619. cckPhyErrRate <= ah->config.cck_trig_low) {
  620. ath9k_hw_ani_lower_immunity(ah);
  621. aniState->ofdmsTurn = !aniState->ofdmsTurn;
  622. }
  623. ath9k_ani_restart(ah);
  624. } else if (aniState->listenTime > ah->aniperiod) {
  625. /* check to see if need to raise immunity */
  626. if (ofdmPhyErrRate > ah->config.ofdm_trig_high &&
  627. (cckPhyErrRate <= ah->config.cck_trig_high ||
  628. aniState->ofdmsTurn)) {
  629. ath9k_hw_ani_ofdm_err_trigger(ah);
  630. ath9k_ani_restart(ah);
  631. aniState->ofdmsTurn = false;
  632. } else if (cckPhyErrRate > ah->config.cck_trig_high) {
  633. ath9k_hw_ani_cck_err_trigger(ah);
  634. ath9k_ani_restart(ah);
  635. aniState->ofdmsTurn = true;
  636. }
  637. }
  638. }
  639. EXPORT_SYMBOL(ath9k_hw_ani_monitor);
  640. void ath9k_enable_mib_counters(struct ath_hw *ah)
  641. {
  642. struct ath_common *common = ath9k_hw_common(ah);
  643. ath_dbg(common, ATH_DBG_ANI, "Enable MIB counters\n");
  644. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  645. ENABLE_REGWRITE_BUFFER(ah);
  646. REG_WRITE(ah, AR_FILT_OFDM, 0);
  647. REG_WRITE(ah, AR_FILT_CCK, 0);
  648. REG_WRITE(ah, AR_MIBC,
  649. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  650. & 0x0f);
  651. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  652. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  653. REGWRITE_BUFFER_FLUSH(ah);
  654. }
  655. /* Freeze the MIB counters, get the stats and then clear them */
  656. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  657. {
  658. struct ath_common *common = ath9k_hw_common(ah);
  659. ath_dbg(common, ATH_DBG_ANI, "Disable MIB counters\n");
  660. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  661. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  662. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  663. REG_WRITE(ah, AR_FILT_OFDM, 0);
  664. REG_WRITE(ah, AR_FILT_CCK, 0);
  665. }
  666. EXPORT_SYMBOL(ath9k_hw_disable_mib_counters);
  667. /*
  668. * Process a MIB interrupt. We may potentially be invoked because
  669. * any of the MIB counters overflow/trigger so don't assume we're
  670. * here because a PHY error counter triggered.
  671. */
  672. void ath9k_hw_proc_mib_event(struct ath_hw *ah)
  673. {
  674. u32 phyCnt1, phyCnt2;
  675. /* Reset these counters regardless */
  676. REG_WRITE(ah, AR_FILT_OFDM, 0);
  677. REG_WRITE(ah, AR_FILT_CCK, 0);
  678. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  679. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  680. /* Clear the mib counters and save them in the stats */
  681. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  682. if (!DO_ANI(ah)) {
  683. /*
  684. * We must always clear the interrupt cause by
  685. * resetting the phy error regs.
  686. */
  687. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  688. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  689. return;
  690. }
  691. /* NB: these are not reset-on-read */
  692. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  693. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  694. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  695. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  696. if (!use_new_ani(ah))
  697. ath9k_hw_ani_read_counters(ah);
  698. /* NB: always restart to insure the h/w counters are reset */
  699. ath9k_ani_restart(ah);
  700. }
  701. }
  702. EXPORT_SYMBOL(ath9k_hw_proc_mib_event);
  703. void ath9k_hw_ani_setup(struct ath_hw *ah)
  704. {
  705. int i;
  706. static const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  707. static const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  708. static const int coarseLow[] = { -64, -64, -64, -64, -70 };
  709. static const int firpwr[] = { -78, -78, -78, -78, -80 };
  710. for (i = 0; i < 5; i++) {
  711. ah->totalSizeDesired[i] = totalSizeDesired[i];
  712. ah->coarse_high[i] = coarseHigh[i];
  713. ah->coarse_low[i] = coarseLow[i];
  714. ah->firpwr[i] = firpwr[i];
  715. }
  716. }
  717. void ath9k_hw_ani_init(struct ath_hw *ah)
  718. {
  719. struct ath_common *common = ath9k_hw_common(ah);
  720. int i;
  721. ath_dbg(common, ATH_DBG_ANI, "Initialize ANI\n");
  722. if (use_new_ani(ah)) {
  723. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_NEW;
  724. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_NEW;
  725. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_NEW;
  726. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_NEW;
  727. } else {
  728. ah->config.ofdm_trig_high = ATH9K_ANI_OFDM_TRIG_HIGH_OLD;
  729. ah->config.ofdm_trig_low = ATH9K_ANI_OFDM_TRIG_LOW_OLD;
  730. ah->config.cck_trig_high = ATH9K_ANI_CCK_TRIG_HIGH_OLD;
  731. ah->config.cck_trig_low = ATH9K_ANI_CCK_TRIG_LOW_OLD;
  732. }
  733. for (i = 0; i < ARRAY_SIZE(ah->channels); i++) {
  734. struct ath9k_channel *chan = &ah->channels[i];
  735. struct ar5416AniState *ani = &chan->ani;
  736. if (use_new_ani(ah)) {
  737. ani->spurImmunityLevel =
  738. ATH9K_ANI_SPUR_IMMUNE_LVL_NEW;
  739. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_NEW;
  740. if (AR_SREV_9300_20_OR_LATER(ah))
  741. ani->mrcCCKOff =
  742. !ATH9K_ANI_ENABLE_MRC_CCK;
  743. else
  744. ani->mrcCCKOff = true;
  745. ani->ofdmsTurn = true;
  746. } else {
  747. ani->spurImmunityLevel =
  748. ATH9K_ANI_SPUR_IMMUNE_LVL_OLD;
  749. ani->firstepLevel = ATH9K_ANI_FIRSTEP_LVL_OLD;
  750. ani->cckWeakSigThreshold =
  751. ATH9K_ANI_CCK_WEAK_SIG_THR;
  752. }
  753. ani->rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  754. ani->rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  755. ani->ofdmWeakSigDetectOff =
  756. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  757. ani->cckNoiseImmunityLevel = ATH9K_ANI_CCK_DEF_LEVEL;
  758. ani->ofdmNoiseImmunityLevel = ATH9K_ANI_OFDM_DEF_LEVEL;
  759. ani->update_ani = false;
  760. }
  761. /*
  762. * since we expect some ongoing maintenance on the tables, let's sanity
  763. * check here default level should not modify INI setting.
  764. */
  765. if (use_new_ani(ah)) {
  766. ah->aniperiod = ATH9K_ANI_PERIOD_NEW;
  767. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_NEW;
  768. } else {
  769. ah->aniperiod = ATH9K_ANI_PERIOD_OLD;
  770. ah->config.ani_poll_interval = ATH9K_ANI_POLLINTERVAL_OLD;
  771. }
  772. if (ah->config.enable_ani)
  773. ah->proc_phyerr |= HAL_PROCESS_ANI;
  774. ath9k_ani_restart(ah);
  775. ath9k_enable_mib_counters(ah);
  776. }