ani.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (c) 2008-2009 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 "hw.h"
  17. #include "hw-ops.h"
  18. static int ath9k_hw_get_ani_channel_idx(struct ath_hw *ah,
  19. struct ath9k_channel *chan)
  20. {
  21. int i;
  22. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  23. if (ah->ani[i].c &&
  24. ah->ani[i].c->channel == chan->channel)
  25. return i;
  26. if (ah->ani[i].c == NULL) {
  27. ah->ani[i].c = chan;
  28. return i;
  29. }
  30. }
  31. ath_print(ath9k_hw_common(ah), ATH_DBG_ANI,
  32. "No more channel states left. Using channel 0\n");
  33. return 0;
  34. }
  35. static void ath9k_hw_update_mibstats(struct ath_hw *ah,
  36. struct ath9k_mib_stats *stats)
  37. {
  38. stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
  39. stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
  40. stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
  41. stats->rts_good += REG_READ(ah, AR_RTS_OK);
  42. stats->beacons += REG_READ(ah, AR_BEACON_CNT);
  43. }
  44. static void ath9k_ani_restart(struct ath_hw *ah)
  45. {
  46. struct ar5416AniState *aniState;
  47. struct ath_common *common = ath9k_hw_common(ah);
  48. if (!DO_ANI(ah))
  49. return;
  50. aniState = ah->curani;
  51. aniState->listenTime = 0;
  52. if (aniState->ofdmTrigHigh > AR_PHY_COUNTMAX) {
  53. aniState->ofdmPhyErrBase = 0;
  54. ath_print(common, ATH_DBG_ANI,
  55. "OFDM Trigger is too high for hw counters\n");
  56. } else {
  57. aniState->ofdmPhyErrBase =
  58. AR_PHY_COUNTMAX - aniState->ofdmTrigHigh;
  59. }
  60. if (aniState->cckTrigHigh > AR_PHY_COUNTMAX) {
  61. aniState->cckPhyErrBase = 0;
  62. ath_print(common, ATH_DBG_ANI,
  63. "CCK Trigger is too high for hw counters\n");
  64. } else {
  65. aniState->cckPhyErrBase =
  66. AR_PHY_COUNTMAX - aniState->cckTrigHigh;
  67. }
  68. ath_print(common, ATH_DBG_ANI,
  69. "Writing ofdmbase=%u cckbase=%u\n",
  70. aniState->ofdmPhyErrBase,
  71. aniState->cckPhyErrBase);
  72. REG_WRITE(ah, AR_PHY_ERR_1, aniState->ofdmPhyErrBase);
  73. REG_WRITE(ah, AR_PHY_ERR_2, aniState->cckPhyErrBase);
  74. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  75. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  76. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  77. aniState->ofdmPhyErrCount = 0;
  78. aniState->cckPhyErrCount = 0;
  79. }
  80. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  81. {
  82. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  83. struct ar5416AniState *aniState;
  84. int32_t rssi;
  85. if (!DO_ANI(ah))
  86. return;
  87. aniState = ah->curani;
  88. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  89. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  90. aniState->noiseImmunityLevel + 1)) {
  91. return;
  92. }
  93. }
  94. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  95. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  96. aniState->spurImmunityLevel + 1)) {
  97. return;
  98. }
  99. }
  100. if (ah->opmode == NL80211_IFTYPE_AP) {
  101. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  102. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  103. aniState->firstepLevel + 1);
  104. }
  105. return;
  106. }
  107. rssi = BEACON_RSSI(ah);
  108. if (rssi > aniState->rssiThrHigh) {
  109. if (!aniState->ofdmWeakSigDetectOff) {
  110. if (ath9k_hw_ani_control(ah,
  111. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  112. false)) {
  113. ath9k_hw_ani_control(ah,
  114. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  115. return;
  116. }
  117. }
  118. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  119. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  120. aniState->firstepLevel + 1);
  121. return;
  122. }
  123. } else if (rssi > aniState->rssiThrLow) {
  124. if (aniState->ofdmWeakSigDetectOff)
  125. ath9k_hw_ani_control(ah,
  126. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  127. true);
  128. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  129. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  130. aniState->firstepLevel + 1);
  131. return;
  132. } else {
  133. if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
  134. !conf_is_ht(conf)) {
  135. if (!aniState->ofdmWeakSigDetectOff)
  136. ath9k_hw_ani_control(ah,
  137. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  138. false);
  139. if (aniState->firstepLevel > 0)
  140. ath9k_hw_ani_control(ah,
  141. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  142. return;
  143. }
  144. }
  145. }
  146. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  147. {
  148. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  149. struct ar5416AniState *aniState;
  150. int32_t rssi;
  151. if (!DO_ANI(ah))
  152. return;
  153. aniState = ah->curani;
  154. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  155. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  156. aniState->noiseImmunityLevel + 1)) {
  157. return;
  158. }
  159. }
  160. if (ah->opmode == NL80211_IFTYPE_AP) {
  161. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  162. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  163. aniState->firstepLevel + 1);
  164. }
  165. return;
  166. }
  167. rssi = BEACON_RSSI(ah);
  168. if (rssi > aniState->rssiThrLow) {
  169. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  170. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  171. aniState->firstepLevel + 1);
  172. } else {
  173. if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
  174. !conf_is_ht(conf)) {
  175. if (aniState->firstepLevel > 0)
  176. ath9k_hw_ani_control(ah,
  177. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  178. }
  179. }
  180. }
  181. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  182. {
  183. struct ar5416AniState *aniState;
  184. int32_t rssi;
  185. aniState = ah->curani;
  186. if (ah->opmode == NL80211_IFTYPE_AP) {
  187. if (aniState->firstepLevel > 0) {
  188. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  189. aniState->firstepLevel - 1))
  190. return;
  191. }
  192. } else {
  193. rssi = BEACON_RSSI(ah);
  194. if (rssi > aniState->rssiThrHigh) {
  195. /* XXX: Handle me */
  196. } else if (rssi > aniState->rssiThrLow) {
  197. if (aniState->ofdmWeakSigDetectOff) {
  198. if (ath9k_hw_ani_control(ah,
  199. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  200. true) == true)
  201. return;
  202. }
  203. if (aniState->firstepLevel > 0) {
  204. if (ath9k_hw_ani_control(ah,
  205. ATH9K_ANI_FIRSTEP_LEVEL,
  206. aniState->firstepLevel - 1) == true)
  207. return;
  208. }
  209. } else {
  210. if (aniState->firstepLevel > 0) {
  211. if (ath9k_hw_ani_control(ah,
  212. ATH9K_ANI_FIRSTEP_LEVEL,
  213. aniState->firstepLevel - 1) == true)
  214. return;
  215. }
  216. }
  217. }
  218. if (aniState->spurImmunityLevel > 0) {
  219. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  220. aniState->spurImmunityLevel - 1))
  221. return;
  222. }
  223. if (aniState->noiseImmunityLevel > 0) {
  224. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  225. aniState->noiseImmunityLevel - 1);
  226. return;
  227. }
  228. }
  229. static int32_t ath9k_hw_ani_get_listen_time(struct ath_hw *ah)
  230. {
  231. struct ar5416AniState *aniState;
  232. u32 txFrameCount, rxFrameCount, cycleCount;
  233. int32_t listenTime;
  234. txFrameCount = REG_READ(ah, AR_TFCNT);
  235. rxFrameCount = REG_READ(ah, AR_RFCNT);
  236. cycleCount = REG_READ(ah, AR_CCCNT);
  237. aniState = ah->curani;
  238. if (aniState->cycleCount == 0 || aniState->cycleCount > cycleCount) {
  239. listenTime = 0;
  240. ah->stats.ast_ani_lzero++;
  241. } else {
  242. int32_t ccdelta = cycleCount - aniState->cycleCount;
  243. int32_t rfdelta = rxFrameCount - aniState->rxFrameCount;
  244. int32_t tfdelta = txFrameCount - aniState->txFrameCount;
  245. listenTime = (ccdelta - rfdelta - tfdelta) / 44000;
  246. }
  247. aniState->cycleCount = cycleCount;
  248. aniState->txFrameCount = txFrameCount;
  249. aniState->rxFrameCount = rxFrameCount;
  250. return listenTime;
  251. }
  252. void ath9k_ani_reset(struct ath_hw *ah)
  253. {
  254. struct ar5416AniState *aniState;
  255. struct ath9k_channel *chan = ah->curchan;
  256. struct ath_common *common = ath9k_hw_common(ah);
  257. int index;
  258. if (!DO_ANI(ah))
  259. return;
  260. index = ath9k_hw_get_ani_channel_idx(ah, chan);
  261. aniState = &ah->ani[index];
  262. ah->curani = aniState;
  263. if (DO_ANI(ah) && ah->opmode != NL80211_IFTYPE_STATION
  264. && ah->opmode != NL80211_IFTYPE_ADHOC) {
  265. ath_print(common, ATH_DBG_ANI,
  266. "Reset ANI state opmode %u\n", ah->opmode);
  267. ah->stats.ast_ani_reset++;
  268. if (ah->opmode == NL80211_IFTYPE_AP) {
  269. /*
  270. * ath9k_hw_ani_control() will only process items set on
  271. * ah->ani_function
  272. */
  273. if (IS_CHAN_2GHZ(chan))
  274. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  275. ATH9K_ANI_FIRSTEP_LEVEL);
  276. else
  277. ah->ani_function = 0;
  278. }
  279. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  280. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  281. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  282. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  283. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  284. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  285. ATH9K_ANI_CCK_WEAK_SIG_THR);
  286. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
  287. ATH9K_RX_FILTER_PHYERR);
  288. if (ah->opmode == NL80211_IFTYPE_AP) {
  289. ah->curani->ofdmTrigHigh =
  290. ah->config.ofdm_trig_high;
  291. ah->curani->ofdmTrigLow =
  292. ah->config.ofdm_trig_low;
  293. ah->curani->cckTrigHigh =
  294. ah->config.cck_trig_high;
  295. ah->curani->cckTrigLow =
  296. ah->config.cck_trig_low;
  297. }
  298. ath9k_ani_restart(ah);
  299. return;
  300. }
  301. if (aniState->noiseImmunityLevel != 0)
  302. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  303. aniState->noiseImmunityLevel);
  304. if (aniState->spurImmunityLevel != 0)
  305. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  306. aniState->spurImmunityLevel);
  307. if (aniState->ofdmWeakSigDetectOff)
  308. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  309. !aniState->ofdmWeakSigDetectOff);
  310. if (aniState->cckWeakSigThreshold)
  311. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  312. aniState->cckWeakSigThreshold);
  313. if (aniState->firstepLevel != 0)
  314. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  315. aniState->firstepLevel);
  316. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
  317. ~ATH9K_RX_FILTER_PHYERR);
  318. ath9k_ani_restart(ah);
  319. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  320. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  321. }
  322. void ath9k_hw_ani_monitor(struct ath_hw *ah,
  323. struct ath9k_channel *chan)
  324. {
  325. struct ar5416AniState *aniState;
  326. struct ath_common *common = ath9k_hw_common(ah);
  327. int32_t listenTime;
  328. u32 phyCnt1, phyCnt2;
  329. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  330. if (!DO_ANI(ah))
  331. return;
  332. aniState = ah->curani;
  333. listenTime = ath9k_hw_ani_get_listen_time(ah);
  334. if (listenTime < 0) {
  335. ah->stats.ast_ani_lneg++;
  336. ath9k_ani_restart(ah);
  337. return;
  338. }
  339. aniState->listenTime += listenTime;
  340. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  341. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  342. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  343. if (phyCnt1 < aniState->ofdmPhyErrBase ||
  344. phyCnt2 < aniState->cckPhyErrBase) {
  345. if (phyCnt1 < aniState->ofdmPhyErrBase) {
  346. ath_print(common, ATH_DBG_ANI,
  347. "phyCnt1 0x%x, resetting "
  348. "counter value to 0x%x\n",
  349. phyCnt1,
  350. aniState->ofdmPhyErrBase);
  351. REG_WRITE(ah, AR_PHY_ERR_1,
  352. aniState->ofdmPhyErrBase);
  353. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  354. AR_PHY_ERR_OFDM_TIMING);
  355. }
  356. if (phyCnt2 < aniState->cckPhyErrBase) {
  357. ath_print(common, ATH_DBG_ANI,
  358. "phyCnt2 0x%x, resetting "
  359. "counter value to 0x%x\n",
  360. phyCnt2,
  361. aniState->cckPhyErrBase);
  362. REG_WRITE(ah, AR_PHY_ERR_2,
  363. aniState->cckPhyErrBase);
  364. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  365. AR_PHY_ERR_CCK_TIMING);
  366. }
  367. return;
  368. }
  369. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  370. ah->stats.ast_ani_ofdmerrs +=
  371. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  372. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  373. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  374. ah->stats.ast_ani_cckerrs +=
  375. cckPhyErrCnt - aniState->cckPhyErrCount;
  376. aniState->cckPhyErrCount = cckPhyErrCnt;
  377. if (aniState->listenTime > 5 * ah->aniperiod) {
  378. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  379. aniState->ofdmTrigLow / 1000 &&
  380. aniState->cckPhyErrCount <= aniState->listenTime *
  381. aniState->cckTrigLow / 1000)
  382. ath9k_hw_ani_lower_immunity(ah);
  383. ath9k_ani_restart(ah);
  384. } else if (aniState->listenTime > ah->aniperiod) {
  385. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  386. aniState->ofdmTrigHigh / 1000) {
  387. ath9k_hw_ani_ofdm_err_trigger(ah);
  388. ath9k_ani_restart(ah);
  389. } else if (aniState->cckPhyErrCount >
  390. aniState->listenTime * aniState->cckTrigHigh /
  391. 1000) {
  392. ath9k_hw_ani_cck_err_trigger(ah);
  393. ath9k_ani_restart(ah);
  394. }
  395. }
  396. }
  397. EXPORT_SYMBOL(ath9k_hw_ani_monitor);
  398. void ath9k_enable_mib_counters(struct ath_hw *ah)
  399. {
  400. struct ath_common *common = ath9k_hw_common(ah);
  401. ath_print(common, ATH_DBG_ANI, "Enable MIB counters\n");
  402. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  403. REG_WRITE(ah, AR_FILT_OFDM, 0);
  404. REG_WRITE(ah, AR_FILT_CCK, 0);
  405. REG_WRITE(ah, AR_MIBC,
  406. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  407. & 0x0f);
  408. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  409. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  410. }
  411. /* Freeze the MIB counters, get the stats and then clear them */
  412. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  413. {
  414. struct ath_common *common = ath9k_hw_common(ah);
  415. ath_print(common, ATH_DBG_ANI, "Disable MIB counters\n");
  416. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  417. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  418. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  419. REG_WRITE(ah, AR_FILT_OFDM, 0);
  420. REG_WRITE(ah, AR_FILT_CCK, 0);
  421. }
  422. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hw *ah,
  423. u32 *rxc_pcnt,
  424. u32 *rxf_pcnt,
  425. u32 *txf_pcnt)
  426. {
  427. struct ath_common *common = ath9k_hw_common(ah);
  428. static u32 cycles, rx_clear, rx_frame, tx_frame;
  429. u32 good = 1;
  430. u32 rc = REG_READ(ah, AR_RCCNT);
  431. u32 rf = REG_READ(ah, AR_RFCNT);
  432. u32 tf = REG_READ(ah, AR_TFCNT);
  433. u32 cc = REG_READ(ah, AR_CCCNT);
  434. if (cycles == 0 || cycles > cc) {
  435. ath_print(common, ATH_DBG_ANI,
  436. "cycle counter wrap. ExtBusy = 0\n");
  437. good = 0;
  438. } else {
  439. u32 cc_d = cc - cycles;
  440. u32 rc_d = rc - rx_clear;
  441. u32 rf_d = rf - rx_frame;
  442. u32 tf_d = tf - tx_frame;
  443. if (cc_d != 0) {
  444. *rxc_pcnt = rc_d * 100 / cc_d;
  445. *rxf_pcnt = rf_d * 100 / cc_d;
  446. *txf_pcnt = tf_d * 100 / cc_d;
  447. } else {
  448. good = 0;
  449. }
  450. }
  451. cycles = cc;
  452. rx_frame = rf;
  453. rx_clear = rc;
  454. tx_frame = tf;
  455. return good;
  456. }
  457. /*
  458. * Process a MIB interrupt. We may potentially be invoked because
  459. * any of the MIB counters overflow/trigger so don't assume we're
  460. * here because a PHY error counter triggered.
  461. */
  462. void ath9k_hw_procmibevent(struct ath_hw *ah)
  463. {
  464. u32 phyCnt1, phyCnt2;
  465. /* Reset these counters regardless */
  466. REG_WRITE(ah, AR_FILT_OFDM, 0);
  467. REG_WRITE(ah, AR_FILT_CCK, 0);
  468. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  469. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  470. /* Clear the mib counters and save them in the stats */
  471. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  472. if (!DO_ANI(ah))
  473. return;
  474. /* NB: these are not reset-on-read */
  475. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  476. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  477. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  478. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  479. struct ar5416AniState *aniState = ah->curani;
  480. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  481. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  482. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  483. ah->stats.ast_ani_ofdmerrs +=
  484. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  485. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  486. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  487. ah->stats.ast_ani_cckerrs +=
  488. cckPhyErrCnt - aniState->cckPhyErrCount;
  489. aniState->cckPhyErrCount = cckPhyErrCnt;
  490. /*
  491. * NB: figure out which counter triggered. If both
  492. * trigger we'll only deal with one as the processing
  493. * clobbers the error counter so the trigger threshold
  494. * check will never be true.
  495. */
  496. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  497. ath9k_hw_ani_ofdm_err_trigger(ah);
  498. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  499. ath9k_hw_ani_cck_err_trigger(ah);
  500. /* NB: always restart to insure the h/w counters are reset */
  501. ath9k_ani_restart(ah);
  502. }
  503. }
  504. EXPORT_SYMBOL(ath9k_hw_procmibevent);
  505. void ath9k_hw_ani_setup(struct ath_hw *ah)
  506. {
  507. int i;
  508. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  509. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  510. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  511. const int firpwr[] = { -78, -78, -78, -78, -80 };
  512. for (i = 0; i < 5; i++) {
  513. ah->totalSizeDesired[i] = totalSizeDesired[i];
  514. ah->coarse_high[i] = coarseHigh[i];
  515. ah->coarse_low[i] = coarseLow[i];
  516. ah->firpwr[i] = firpwr[i];
  517. }
  518. }
  519. void ath9k_hw_ani_init(struct ath_hw *ah)
  520. {
  521. struct ath_common *common = ath9k_hw_common(ah);
  522. int i;
  523. ath_print(common, ATH_DBG_ANI, "Initialize ANI\n");
  524. memset(ah->ani, 0, sizeof(ah->ani));
  525. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  526. ah->ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  527. ah->ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  528. ah->ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  529. ah->ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  530. ah->ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  531. ah->ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  532. ah->ani[i].ofdmWeakSigDetectOff =
  533. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  534. ah->ani[i].cckWeakSigThreshold =
  535. ATH9K_ANI_CCK_WEAK_SIG_THR;
  536. ah->ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  537. ah->ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  538. ah->ani[i].ofdmPhyErrBase =
  539. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  540. ah->ani[i].cckPhyErrBase =
  541. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  542. }
  543. ath_print(common, ATH_DBG_ANI,
  544. "Setting OfdmErrBase = 0x%08x\n",
  545. ah->ani[0].ofdmPhyErrBase);
  546. ath_print(common, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  547. ah->ani[0].cckPhyErrBase);
  548. REG_WRITE(ah, AR_PHY_ERR_1, ah->ani[0].ofdmPhyErrBase);
  549. REG_WRITE(ah, AR_PHY_ERR_2, ah->ani[0].cckPhyErrBase);
  550. ath9k_enable_mib_counters(ah);
  551. ah->aniperiod = ATH9K_ANI_PERIOD;
  552. if (ah->config.enable_ani)
  553. ah->proc_phyerr |= HAL_PROCESS_ANI;
  554. }
  555. void ath9k_hw_ani_disable(struct ath_hw *ah)
  556. {
  557. ath_print(ath9k_hw_common(ah), ATH_DBG_ANI, "Disabling ANI\n");
  558. ath9k_hw_disable_mib_counters(ah);
  559. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  560. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  561. }