ani.c 18 KB

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