ani.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 "ath9k.h"
  17. static int ath9k_hw_get_ani_channel_idx(struct ath_hw *ah,
  18. struct ath9k_channel *chan)
  19. {
  20. int i;
  21. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  22. if (ah->ani[i].c &&
  23. ah->ani[i].c->channel == chan->channel)
  24. return i;
  25. if (ah->ani[i].c == NULL) {
  26. ah->ani[i].c = chan;
  27. return i;
  28. }
  29. }
  30. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  31. "No more channel states left. Using channel 0\n");
  32. return 0;
  33. }
  34. static bool ath9k_hw_ani_control(struct ath_hw *ah,
  35. enum ath9k_ani_cmd cmd, int param)
  36. {
  37. struct ar5416AniState *aniState = ah->curani;
  38. switch (cmd & ah->ani_function) {
  39. case ATH9K_ANI_NOISE_IMMUNITY_LEVEL:{
  40. u32 level = param;
  41. if (level >= ARRAY_SIZE(ah->totalSizeDesired)) {
  42. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  43. "level out of range (%u > %u)\n",
  44. level,
  45. (unsigned)ARRAY_SIZE(ah->totalSizeDesired));
  46. return false;
  47. }
  48. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
  49. AR_PHY_DESIRED_SZ_TOT_DES,
  50. ah->totalSizeDesired[level]);
  51. REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,
  52. AR_PHY_AGC_CTL1_COARSE_LOW,
  53. ah->coarse_low[level]);
  54. REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,
  55. AR_PHY_AGC_CTL1_COARSE_HIGH,
  56. ah->coarse_high[level]);
  57. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  58. AR_PHY_FIND_SIG_FIRPWR,
  59. ah->firpwr[level]);
  60. if (level > aniState->noiseImmunityLevel)
  61. ah->stats.ast_ani_niup++;
  62. else if (level < aniState->noiseImmunityLevel)
  63. ah->stats.ast_ani_nidown++;
  64. aniState->noiseImmunityLevel = level;
  65. break;
  66. }
  67. case ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION:{
  68. const int m1ThreshLow[] = { 127, 50 };
  69. const int m2ThreshLow[] = { 127, 40 };
  70. const int m1Thresh[] = { 127, 0x4d };
  71. const int m2Thresh[] = { 127, 0x40 };
  72. const int m2CountThr[] = { 31, 16 };
  73. const int m2CountThrLow[] = { 63, 48 };
  74. u32 on = param ? 1 : 0;
  75. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  76. AR_PHY_SFCORR_LOW_M1_THRESH_LOW,
  77. m1ThreshLow[on]);
  78. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  79. AR_PHY_SFCORR_LOW_M2_THRESH_LOW,
  80. m2ThreshLow[on]);
  81. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  82. AR_PHY_SFCORR_M1_THRESH,
  83. m1Thresh[on]);
  84. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  85. AR_PHY_SFCORR_M2_THRESH,
  86. m2Thresh[on]);
  87. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  88. AR_PHY_SFCORR_M2COUNT_THR,
  89. m2CountThr[on]);
  90. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  91. AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW,
  92. m2CountThrLow[on]);
  93. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  94. AR_PHY_SFCORR_EXT_M1_THRESH_LOW,
  95. m1ThreshLow[on]);
  96. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  97. AR_PHY_SFCORR_EXT_M2_THRESH_LOW,
  98. m2ThreshLow[on]);
  99. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  100. AR_PHY_SFCORR_EXT_M1_THRESH,
  101. m1Thresh[on]);
  102. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  103. AR_PHY_SFCORR_EXT_M2_THRESH,
  104. m2Thresh[on]);
  105. if (on)
  106. REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,
  107. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  108. else
  109. REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,
  110. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  111. if (!on != aniState->ofdmWeakSigDetectOff) {
  112. if (on)
  113. ah->stats.ast_ani_ofdmon++;
  114. else
  115. ah->stats.ast_ani_ofdmoff++;
  116. aniState->ofdmWeakSigDetectOff = !on;
  117. }
  118. break;
  119. }
  120. case ATH9K_ANI_CCK_WEAK_SIGNAL_THR:{
  121. const int weakSigThrCck[] = { 8, 6 };
  122. u32 high = param ? 1 : 0;
  123. REG_RMW_FIELD(ah, AR_PHY_CCK_DETECT,
  124. AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK,
  125. weakSigThrCck[high]);
  126. if (high != aniState->cckWeakSigThreshold) {
  127. if (high)
  128. ah->stats.ast_ani_cckhigh++;
  129. else
  130. ah->stats.ast_ani_ccklow++;
  131. aniState->cckWeakSigThreshold = high;
  132. }
  133. break;
  134. }
  135. case ATH9K_ANI_FIRSTEP_LEVEL:{
  136. const int firstep[] = { 0, 4, 8 };
  137. u32 level = param;
  138. if (level >= ARRAY_SIZE(firstep)) {
  139. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  140. "level out of range (%u > %u)\n",
  141. level,
  142. (unsigned) ARRAY_SIZE(firstep));
  143. return false;
  144. }
  145. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  146. AR_PHY_FIND_SIG_FIRSTEP,
  147. firstep[level]);
  148. if (level > aniState->firstepLevel)
  149. ah->stats.ast_ani_stepup++;
  150. else if (level < aniState->firstepLevel)
  151. ah->stats.ast_ani_stepdown++;
  152. aniState->firstepLevel = level;
  153. break;
  154. }
  155. case ATH9K_ANI_SPUR_IMMUNITY_LEVEL:{
  156. const int cycpwrThr1[] =
  157. { 2, 4, 6, 8, 10, 12, 14, 16 };
  158. u32 level = param;
  159. if (level >= ARRAY_SIZE(cycpwrThr1)) {
  160. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  161. "level out of range (%u > %u)\n",
  162. level,
  163. (unsigned)
  164. ARRAY_SIZE(cycpwrThr1));
  165. return false;
  166. }
  167. REG_RMW_FIELD(ah, AR_PHY_TIMING5,
  168. AR_PHY_TIMING5_CYCPWR_THR1,
  169. cycpwrThr1[level]);
  170. if (level > aniState->spurImmunityLevel)
  171. ah->stats.ast_ani_spurup++;
  172. else if (level < aniState->spurImmunityLevel)
  173. ah->stats.ast_ani_spurdown++;
  174. aniState->spurImmunityLevel = level;
  175. break;
  176. }
  177. case ATH9K_ANI_PRESENT:
  178. break;
  179. default:
  180. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  181. "invalid cmd %u\n", cmd);
  182. return false;
  183. }
  184. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "ANI parameters:\n");
  185. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  186. "noiseImmunityLevel=%d, spurImmunityLevel=%d, "
  187. "ofdmWeakSigDetectOff=%d\n",
  188. aniState->noiseImmunityLevel, aniState->spurImmunityLevel,
  189. !aniState->ofdmWeakSigDetectOff);
  190. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  191. "cckWeakSigThreshold=%d, "
  192. "firstepLevel=%d, listenTime=%d\n",
  193. aniState->cckWeakSigThreshold, aniState->firstepLevel,
  194. aniState->listenTime);
  195. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  196. "cycleCount=%d, ofdmPhyErrCount=%d, cckPhyErrCount=%d\n\n",
  197. aniState->cycleCount, aniState->ofdmPhyErrCount,
  198. aniState->cckPhyErrCount);
  199. return true;
  200. }
  201. static void ath9k_hw_update_mibstats(struct ath_hw *ah,
  202. struct ath9k_mib_stats *stats)
  203. {
  204. stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
  205. stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
  206. stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
  207. stats->rts_good += REG_READ(ah, AR_RTS_OK);
  208. stats->beacons += REG_READ(ah, AR_BEACON_CNT);
  209. }
  210. static void ath9k_ani_restart(struct ath_hw *ah)
  211. {
  212. struct ar5416AniState *aniState;
  213. if (!DO_ANI(ah))
  214. return;
  215. aniState = ah->curani;
  216. aniState->listenTime = 0;
  217. if (aniState->ofdmTrigHigh > AR_PHY_COUNTMAX) {
  218. aniState->ofdmPhyErrBase = 0;
  219. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  220. "OFDM Trigger is too high for hw counters\n");
  221. } else {
  222. aniState->ofdmPhyErrBase =
  223. AR_PHY_COUNTMAX - aniState->ofdmTrigHigh;
  224. }
  225. if (aniState->cckTrigHigh > AR_PHY_COUNTMAX) {
  226. aniState->cckPhyErrBase = 0;
  227. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  228. "CCK Trigger is too high for hw counters\n");
  229. } else {
  230. aniState->cckPhyErrBase =
  231. AR_PHY_COUNTMAX - aniState->cckTrigHigh;
  232. }
  233. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  234. "Writing ofdmbase=%u cckbase=%u\n",
  235. aniState->ofdmPhyErrBase,
  236. aniState->cckPhyErrBase);
  237. REG_WRITE(ah, AR_PHY_ERR_1, aniState->ofdmPhyErrBase);
  238. REG_WRITE(ah, AR_PHY_ERR_2, aniState->cckPhyErrBase);
  239. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  240. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  241. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  242. aniState->ofdmPhyErrCount = 0;
  243. aniState->cckPhyErrCount = 0;
  244. }
  245. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  246. {
  247. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  248. struct ar5416AniState *aniState;
  249. int32_t rssi;
  250. if (!DO_ANI(ah))
  251. return;
  252. aniState = ah->curani;
  253. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  254. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  255. aniState->noiseImmunityLevel + 1)) {
  256. return;
  257. }
  258. }
  259. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  260. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  261. aniState->spurImmunityLevel + 1)) {
  262. return;
  263. }
  264. }
  265. if (ah->opmode == NL80211_IFTYPE_AP) {
  266. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  267. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  268. aniState->firstepLevel + 1);
  269. }
  270. return;
  271. }
  272. rssi = BEACON_RSSI(ah);
  273. if (rssi > aniState->rssiThrHigh) {
  274. if (!aniState->ofdmWeakSigDetectOff) {
  275. if (ath9k_hw_ani_control(ah,
  276. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  277. false)) {
  278. ath9k_hw_ani_control(ah,
  279. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  280. return;
  281. }
  282. }
  283. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  284. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  285. aniState->firstepLevel + 1);
  286. return;
  287. }
  288. } else if (rssi > aniState->rssiThrLow) {
  289. if (aniState->ofdmWeakSigDetectOff)
  290. ath9k_hw_ani_control(ah,
  291. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  292. true);
  293. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  294. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  295. aniState->firstepLevel + 1);
  296. return;
  297. } else {
  298. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  299. if (!aniState->ofdmWeakSigDetectOff)
  300. ath9k_hw_ani_control(ah,
  301. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  302. false);
  303. if (aniState->firstepLevel > 0)
  304. ath9k_hw_ani_control(ah,
  305. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  306. return;
  307. }
  308. }
  309. }
  310. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  311. {
  312. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  313. struct ar5416AniState *aniState;
  314. int32_t rssi;
  315. if (!DO_ANI(ah))
  316. return;
  317. aniState = ah->curani;
  318. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  319. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  320. aniState->noiseImmunityLevel + 1)) {
  321. return;
  322. }
  323. }
  324. if (ah->opmode == NL80211_IFTYPE_AP) {
  325. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  326. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  327. aniState->firstepLevel + 1);
  328. }
  329. return;
  330. }
  331. rssi = BEACON_RSSI(ah);
  332. if (rssi > aniState->rssiThrLow) {
  333. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  334. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  335. aniState->firstepLevel + 1);
  336. } else {
  337. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  338. if (aniState->firstepLevel > 0)
  339. ath9k_hw_ani_control(ah,
  340. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  341. }
  342. }
  343. }
  344. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  345. {
  346. struct ar5416AniState *aniState;
  347. int32_t rssi;
  348. aniState = ah->curani;
  349. if (ah->opmode == NL80211_IFTYPE_AP) {
  350. if (aniState->firstepLevel > 0) {
  351. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  352. aniState->firstepLevel - 1))
  353. return;
  354. }
  355. } else {
  356. rssi = BEACON_RSSI(ah);
  357. if (rssi > aniState->rssiThrHigh) {
  358. /* XXX: Handle me */
  359. } else if (rssi > aniState->rssiThrLow) {
  360. if (aniState->ofdmWeakSigDetectOff) {
  361. if (ath9k_hw_ani_control(ah,
  362. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  363. true) == true)
  364. return;
  365. }
  366. if (aniState->firstepLevel > 0) {
  367. if (ath9k_hw_ani_control(ah,
  368. ATH9K_ANI_FIRSTEP_LEVEL,
  369. aniState->firstepLevel - 1) == true)
  370. return;
  371. }
  372. } else {
  373. if (aniState->firstepLevel > 0) {
  374. if (ath9k_hw_ani_control(ah,
  375. ATH9K_ANI_FIRSTEP_LEVEL,
  376. aniState->firstepLevel - 1) == true)
  377. return;
  378. }
  379. }
  380. }
  381. if (aniState->spurImmunityLevel > 0) {
  382. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  383. aniState->spurImmunityLevel - 1))
  384. return;
  385. }
  386. if (aniState->noiseImmunityLevel > 0) {
  387. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  388. aniState->noiseImmunityLevel - 1);
  389. return;
  390. }
  391. }
  392. static int32_t ath9k_hw_ani_get_listen_time(struct ath_hw *ah)
  393. {
  394. struct ar5416AniState *aniState;
  395. u32 txFrameCount, rxFrameCount, cycleCount;
  396. int32_t listenTime;
  397. txFrameCount = REG_READ(ah, AR_TFCNT);
  398. rxFrameCount = REG_READ(ah, AR_RFCNT);
  399. cycleCount = REG_READ(ah, AR_CCCNT);
  400. aniState = ah->curani;
  401. if (aniState->cycleCount == 0 || aniState->cycleCount > cycleCount) {
  402. listenTime = 0;
  403. ah->stats.ast_ani_lzero++;
  404. } else {
  405. int32_t ccdelta = cycleCount - aniState->cycleCount;
  406. int32_t rfdelta = rxFrameCount - aniState->rxFrameCount;
  407. int32_t tfdelta = txFrameCount - aniState->txFrameCount;
  408. listenTime = (ccdelta - rfdelta - tfdelta) / 44000;
  409. }
  410. aniState->cycleCount = cycleCount;
  411. aniState->txFrameCount = txFrameCount;
  412. aniState->rxFrameCount = rxFrameCount;
  413. return listenTime;
  414. }
  415. void ath9k_ani_reset(struct ath_hw *ah)
  416. {
  417. struct ar5416AniState *aniState;
  418. struct ath9k_channel *chan = ah->curchan;
  419. int index;
  420. if (!DO_ANI(ah))
  421. return;
  422. index = ath9k_hw_get_ani_channel_idx(ah, chan);
  423. aniState = &ah->ani[index];
  424. ah->curani = aniState;
  425. if (DO_ANI(ah) && ah->opmode != NL80211_IFTYPE_STATION
  426. && ah->opmode != NL80211_IFTYPE_ADHOC) {
  427. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  428. "Reset ANI state opmode %u\n", ah->opmode);
  429. ah->stats.ast_ani_reset++;
  430. if (ah->opmode == NL80211_IFTYPE_AP) {
  431. /*
  432. * ath9k_hw_ani_control() will only process items set on
  433. * ah->ani_function
  434. */
  435. if (IS_CHAN_2GHZ(chan))
  436. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  437. ATH9K_ANI_FIRSTEP_LEVEL);
  438. else
  439. ah->ani_function = 0;
  440. }
  441. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  442. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  443. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  444. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  445. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  446. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  447. ATH9K_ANI_CCK_WEAK_SIG_THR);
  448. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
  449. ATH9K_RX_FILTER_PHYERR);
  450. if (ah->opmode == NL80211_IFTYPE_AP) {
  451. ah->curani->ofdmTrigHigh =
  452. ah->config.ofdm_trig_high;
  453. ah->curani->ofdmTrigLow =
  454. ah->config.ofdm_trig_low;
  455. ah->curani->cckTrigHigh =
  456. ah->config.cck_trig_high;
  457. ah->curani->cckTrigLow =
  458. ah->config.cck_trig_low;
  459. }
  460. ath9k_ani_restart(ah);
  461. return;
  462. }
  463. if (aniState->noiseImmunityLevel != 0)
  464. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  465. aniState->noiseImmunityLevel);
  466. if (aniState->spurImmunityLevel != 0)
  467. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  468. aniState->spurImmunityLevel);
  469. if (aniState->ofdmWeakSigDetectOff)
  470. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  471. !aniState->ofdmWeakSigDetectOff);
  472. if (aniState->cckWeakSigThreshold)
  473. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  474. aniState->cckWeakSigThreshold);
  475. if (aniState->firstepLevel != 0)
  476. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  477. aniState->firstepLevel);
  478. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
  479. ~ATH9K_RX_FILTER_PHYERR);
  480. ath9k_ani_restart(ah);
  481. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  482. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  483. }
  484. void ath9k_hw_ani_monitor(struct ath_hw *ah,
  485. const struct ath9k_node_stats *stats,
  486. struct ath9k_channel *chan)
  487. {
  488. struct ar5416AniState *aniState;
  489. int32_t listenTime;
  490. u32 phyCnt1, phyCnt2;
  491. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  492. if (!DO_ANI(ah))
  493. return;
  494. aniState = ah->curani;
  495. ah->stats.ast_nodestats = *stats;
  496. listenTime = ath9k_hw_ani_get_listen_time(ah);
  497. if (listenTime < 0) {
  498. ah->stats.ast_ani_lneg++;
  499. ath9k_ani_restart(ah);
  500. return;
  501. }
  502. aniState->listenTime += listenTime;
  503. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  504. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  505. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  506. if (phyCnt1 < aniState->ofdmPhyErrBase ||
  507. phyCnt2 < aniState->cckPhyErrBase) {
  508. if (phyCnt1 < aniState->ofdmPhyErrBase) {
  509. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  510. "phyCnt1 0x%x, resetting "
  511. "counter value to 0x%x\n",
  512. phyCnt1, aniState->ofdmPhyErrBase);
  513. REG_WRITE(ah, AR_PHY_ERR_1,
  514. aniState->ofdmPhyErrBase);
  515. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  516. AR_PHY_ERR_OFDM_TIMING);
  517. }
  518. if (phyCnt2 < aniState->cckPhyErrBase) {
  519. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  520. "phyCnt2 0x%x, resetting "
  521. "counter value to 0x%x\n",
  522. phyCnt2, aniState->cckPhyErrBase);
  523. REG_WRITE(ah, AR_PHY_ERR_2,
  524. aniState->cckPhyErrBase);
  525. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  526. AR_PHY_ERR_CCK_TIMING);
  527. }
  528. return;
  529. }
  530. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  531. ah->stats.ast_ani_ofdmerrs +=
  532. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  533. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  534. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  535. ah->stats.ast_ani_cckerrs +=
  536. cckPhyErrCnt - aniState->cckPhyErrCount;
  537. aniState->cckPhyErrCount = cckPhyErrCnt;
  538. if (aniState->listenTime > 5 * ah->aniperiod) {
  539. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  540. aniState->ofdmTrigLow / 1000 &&
  541. aniState->cckPhyErrCount <= aniState->listenTime *
  542. aniState->cckTrigLow / 1000)
  543. ath9k_hw_ani_lower_immunity(ah);
  544. ath9k_ani_restart(ah);
  545. } else if (aniState->listenTime > ah->aniperiod) {
  546. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  547. aniState->ofdmTrigHigh / 1000) {
  548. ath9k_hw_ani_ofdm_err_trigger(ah);
  549. ath9k_ani_restart(ah);
  550. } else if (aniState->cckPhyErrCount >
  551. aniState->listenTime * aniState->cckTrigHigh /
  552. 1000) {
  553. ath9k_hw_ani_cck_err_trigger(ah);
  554. ath9k_ani_restart(ah);
  555. }
  556. }
  557. }
  558. void ath9k_enable_mib_counters(struct ath_hw *ah)
  559. {
  560. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Enable MIB counters\n");
  561. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  562. REG_WRITE(ah, AR_FILT_OFDM, 0);
  563. REG_WRITE(ah, AR_FILT_CCK, 0);
  564. REG_WRITE(ah, AR_MIBC,
  565. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  566. & 0x0f);
  567. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  568. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  569. }
  570. /* Freeze the MIB counters, get the stats and then clear them */
  571. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  572. {
  573. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disable MIB counters\n");
  574. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  575. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  576. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  577. REG_WRITE(ah, AR_FILT_OFDM, 0);
  578. REG_WRITE(ah, AR_FILT_CCK, 0);
  579. }
  580. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hw *ah,
  581. u32 *rxc_pcnt,
  582. u32 *rxf_pcnt,
  583. u32 *txf_pcnt)
  584. {
  585. static u32 cycles, rx_clear, rx_frame, tx_frame;
  586. u32 good = 1;
  587. u32 rc = REG_READ(ah, AR_RCCNT);
  588. u32 rf = REG_READ(ah, AR_RFCNT);
  589. u32 tf = REG_READ(ah, AR_TFCNT);
  590. u32 cc = REG_READ(ah, AR_CCCNT);
  591. if (cycles == 0 || cycles > cc) {
  592. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  593. "cycle counter wrap. ExtBusy = 0\n");
  594. good = 0;
  595. } else {
  596. u32 cc_d = cc - cycles;
  597. u32 rc_d = rc - rx_clear;
  598. u32 rf_d = rf - rx_frame;
  599. u32 tf_d = tf - tx_frame;
  600. if (cc_d != 0) {
  601. *rxc_pcnt = rc_d * 100 / cc_d;
  602. *rxf_pcnt = rf_d * 100 / cc_d;
  603. *txf_pcnt = tf_d * 100 / cc_d;
  604. } else {
  605. good = 0;
  606. }
  607. }
  608. cycles = cc;
  609. rx_frame = rf;
  610. rx_clear = rc;
  611. tx_frame = tf;
  612. return good;
  613. }
  614. /*
  615. * Process a MIB interrupt. We may potentially be invoked because
  616. * any of the MIB counters overflow/trigger so don't assume we're
  617. * here because a PHY error counter triggered.
  618. */
  619. void ath9k_hw_procmibevent(struct ath_hw *ah,
  620. const struct ath9k_node_stats *stats)
  621. {
  622. u32 phyCnt1, phyCnt2;
  623. /* Reset these counters regardless */
  624. REG_WRITE(ah, AR_FILT_OFDM, 0);
  625. REG_WRITE(ah, AR_FILT_CCK, 0);
  626. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  627. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  628. /* Clear the mib counters and save them in the stats */
  629. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  630. ah->stats.ast_nodestats = *stats;
  631. if (!DO_ANI(ah))
  632. return;
  633. /* NB: these are not reset-on-read */
  634. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  635. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  636. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  637. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  638. struct ar5416AniState *aniState = ah->curani;
  639. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  640. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  641. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  642. ah->stats.ast_ani_ofdmerrs +=
  643. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  644. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  645. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  646. ah->stats.ast_ani_cckerrs +=
  647. cckPhyErrCnt - aniState->cckPhyErrCount;
  648. aniState->cckPhyErrCount = cckPhyErrCnt;
  649. /*
  650. * NB: figure out which counter triggered. If both
  651. * trigger we'll only deal with one as the processing
  652. * clobbers the error counter so the trigger threshold
  653. * check will never be true.
  654. */
  655. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  656. ath9k_hw_ani_ofdm_err_trigger(ah);
  657. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  658. ath9k_hw_ani_cck_err_trigger(ah);
  659. /* NB: always restart to insure the h/w counters are reset */
  660. ath9k_ani_restart(ah);
  661. }
  662. }
  663. void ath9k_hw_ani_setup(struct ath_hw *ah)
  664. {
  665. int i;
  666. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  667. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  668. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  669. const int firpwr[] = { -78, -78, -78, -78, -80 };
  670. for (i = 0; i < 5; i++) {
  671. ah->totalSizeDesired[i] = totalSizeDesired[i];
  672. ah->coarse_high[i] = coarseHigh[i];
  673. ah->coarse_low[i] = coarseLow[i];
  674. ah->firpwr[i] = firpwr[i];
  675. }
  676. }
  677. void ath9k_hw_ani_init(struct ath_hw *ah)
  678. {
  679. int i;
  680. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Initialize ANI\n");
  681. memset(ah->ani, 0, sizeof(ah->ani));
  682. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  683. ah->ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  684. ah->ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  685. ah->ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  686. ah->ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  687. ah->ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  688. ah->ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  689. ah->ani[i].ofdmWeakSigDetectOff =
  690. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  691. ah->ani[i].cckWeakSigThreshold =
  692. ATH9K_ANI_CCK_WEAK_SIG_THR;
  693. ah->ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  694. ah->ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  695. ah->ani[i].ofdmPhyErrBase =
  696. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  697. ah->ani[i].cckPhyErrBase =
  698. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  699. }
  700. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  701. "Setting OfdmErrBase = 0x%08x\n",
  702. ah->ani[0].ofdmPhyErrBase);
  703. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  704. ah->ani[0].cckPhyErrBase);
  705. REG_WRITE(ah, AR_PHY_ERR_1, ah->ani[0].ofdmPhyErrBase);
  706. REG_WRITE(ah, AR_PHY_ERR_2, ah->ani[0].cckPhyErrBase);
  707. ath9k_enable_mib_counters(ah);
  708. ah->aniperiod = ATH9K_ANI_PERIOD;
  709. if (ah->config.enable_ani)
  710. ah->proc_phyerr |= HAL_PROCESS_ANI;
  711. }
  712. void ath9k_hw_ani_disable(struct ath_hw *ah)
  713. {
  714. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disabling ANI\n");
  715. ath9k_hw_disable_mib_counters(ah);
  716. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  717. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  718. }