ani.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. !conf_is_ht(conf)) {
  300. if (!aniState->ofdmWeakSigDetectOff)
  301. ath9k_hw_ani_control(ah,
  302. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  303. false);
  304. if (aniState->firstepLevel > 0)
  305. ath9k_hw_ani_control(ah,
  306. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  307. return;
  308. }
  309. }
  310. }
  311. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  312. {
  313. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  314. struct ar5416AniState *aniState;
  315. int32_t rssi;
  316. if (!DO_ANI(ah))
  317. return;
  318. aniState = ah->curani;
  319. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  320. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  321. aniState->noiseImmunityLevel + 1)) {
  322. return;
  323. }
  324. }
  325. if (ah->opmode == NL80211_IFTYPE_AP) {
  326. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  327. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  328. aniState->firstepLevel + 1);
  329. }
  330. return;
  331. }
  332. rssi = BEACON_RSSI(ah);
  333. if (rssi > aniState->rssiThrLow) {
  334. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  335. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  336. aniState->firstepLevel + 1);
  337. } else {
  338. if ((conf->channel->band == IEEE80211_BAND_2GHZ) &&
  339. !conf_is_ht(conf)) {
  340. if (aniState->firstepLevel > 0)
  341. ath9k_hw_ani_control(ah,
  342. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  343. }
  344. }
  345. }
  346. static void ath9k_hw_ani_lower_immunity(struct ath_hw *ah)
  347. {
  348. struct ar5416AniState *aniState;
  349. int32_t rssi;
  350. aniState = ah->curani;
  351. if (ah->opmode == NL80211_IFTYPE_AP) {
  352. if (aniState->firstepLevel > 0) {
  353. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  354. aniState->firstepLevel - 1))
  355. return;
  356. }
  357. } else {
  358. rssi = BEACON_RSSI(ah);
  359. if (rssi > aniState->rssiThrHigh) {
  360. /* XXX: Handle me */
  361. } else if (rssi > aniState->rssiThrLow) {
  362. if (aniState->ofdmWeakSigDetectOff) {
  363. if (ath9k_hw_ani_control(ah,
  364. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  365. true) == true)
  366. return;
  367. }
  368. if (aniState->firstepLevel > 0) {
  369. if (ath9k_hw_ani_control(ah,
  370. ATH9K_ANI_FIRSTEP_LEVEL,
  371. aniState->firstepLevel - 1) == true)
  372. return;
  373. }
  374. } else {
  375. if (aniState->firstepLevel > 0) {
  376. if (ath9k_hw_ani_control(ah,
  377. ATH9K_ANI_FIRSTEP_LEVEL,
  378. aniState->firstepLevel - 1) == true)
  379. return;
  380. }
  381. }
  382. }
  383. if (aniState->spurImmunityLevel > 0) {
  384. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  385. aniState->spurImmunityLevel - 1))
  386. return;
  387. }
  388. if (aniState->noiseImmunityLevel > 0) {
  389. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  390. aniState->noiseImmunityLevel - 1);
  391. return;
  392. }
  393. }
  394. static int32_t ath9k_hw_ani_get_listen_time(struct ath_hw *ah)
  395. {
  396. struct ar5416AniState *aniState;
  397. u32 txFrameCount, rxFrameCount, cycleCount;
  398. int32_t listenTime;
  399. txFrameCount = REG_READ(ah, AR_TFCNT);
  400. rxFrameCount = REG_READ(ah, AR_RFCNT);
  401. cycleCount = REG_READ(ah, AR_CCCNT);
  402. aniState = ah->curani;
  403. if (aniState->cycleCount == 0 || aniState->cycleCount > cycleCount) {
  404. listenTime = 0;
  405. ah->stats.ast_ani_lzero++;
  406. } else {
  407. int32_t ccdelta = cycleCount - aniState->cycleCount;
  408. int32_t rfdelta = rxFrameCount - aniState->rxFrameCount;
  409. int32_t tfdelta = txFrameCount - aniState->txFrameCount;
  410. listenTime = (ccdelta - rfdelta - tfdelta) / 44000;
  411. }
  412. aniState->cycleCount = cycleCount;
  413. aniState->txFrameCount = txFrameCount;
  414. aniState->rxFrameCount = rxFrameCount;
  415. return listenTime;
  416. }
  417. void ath9k_ani_reset(struct ath_hw *ah)
  418. {
  419. struct ar5416AniState *aniState;
  420. struct ath9k_channel *chan = ah->curchan;
  421. int index;
  422. if (!DO_ANI(ah))
  423. return;
  424. index = ath9k_hw_get_ani_channel_idx(ah, chan);
  425. aniState = &ah->ani[index];
  426. ah->curani = aniState;
  427. if (DO_ANI(ah) && ah->opmode != NL80211_IFTYPE_STATION
  428. && ah->opmode != NL80211_IFTYPE_ADHOC) {
  429. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  430. "Reset ANI state opmode %u\n", ah->opmode);
  431. ah->stats.ast_ani_reset++;
  432. if (ah->opmode == NL80211_IFTYPE_AP) {
  433. /*
  434. * ath9k_hw_ani_control() will only process items set on
  435. * ah->ani_function
  436. */
  437. if (IS_CHAN_2GHZ(chan))
  438. ah->ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  439. ATH9K_ANI_FIRSTEP_LEVEL);
  440. else
  441. ah->ani_function = 0;
  442. }
  443. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  444. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  445. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  446. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  447. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  448. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  449. ATH9K_ANI_CCK_WEAK_SIG_THR);
  450. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
  451. ATH9K_RX_FILTER_PHYERR);
  452. if (ah->opmode == NL80211_IFTYPE_AP) {
  453. ah->curani->ofdmTrigHigh =
  454. ah->config.ofdm_trig_high;
  455. ah->curani->ofdmTrigLow =
  456. ah->config.ofdm_trig_low;
  457. ah->curani->cckTrigHigh =
  458. ah->config.cck_trig_high;
  459. ah->curani->cckTrigLow =
  460. ah->config.cck_trig_low;
  461. }
  462. ath9k_ani_restart(ah);
  463. return;
  464. }
  465. if (aniState->noiseImmunityLevel != 0)
  466. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  467. aniState->noiseImmunityLevel);
  468. if (aniState->spurImmunityLevel != 0)
  469. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  470. aniState->spurImmunityLevel);
  471. if (aniState->ofdmWeakSigDetectOff)
  472. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  473. !aniState->ofdmWeakSigDetectOff);
  474. if (aniState->cckWeakSigThreshold)
  475. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  476. aniState->cckWeakSigThreshold);
  477. if (aniState->firstepLevel != 0)
  478. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  479. aniState->firstepLevel);
  480. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
  481. ~ATH9K_RX_FILTER_PHYERR);
  482. ath9k_ani_restart(ah);
  483. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  484. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  485. }
  486. void ath9k_hw_ani_monitor(struct ath_hw *ah,
  487. struct ath9k_channel *chan)
  488. {
  489. struct ar5416AniState *aniState;
  490. int32_t listenTime;
  491. u32 phyCnt1, phyCnt2;
  492. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  493. if (!DO_ANI(ah))
  494. return;
  495. aniState = ah->curani;
  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. {
  621. u32 phyCnt1, phyCnt2;
  622. /* Reset these counters regardless */
  623. REG_WRITE(ah, AR_FILT_OFDM, 0);
  624. REG_WRITE(ah, AR_FILT_CCK, 0);
  625. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  626. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  627. /* Clear the mib counters and save them in the stats */
  628. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  629. if (!DO_ANI(ah))
  630. return;
  631. /* NB: these are not reset-on-read */
  632. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  633. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  634. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  635. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  636. struct ar5416AniState *aniState = ah->curani;
  637. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  638. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  639. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  640. ah->stats.ast_ani_ofdmerrs +=
  641. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  642. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  643. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  644. ah->stats.ast_ani_cckerrs +=
  645. cckPhyErrCnt - aniState->cckPhyErrCount;
  646. aniState->cckPhyErrCount = cckPhyErrCnt;
  647. /*
  648. * NB: figure out which counter triggered. If both
  649. * trigger we'll only deal with one as the processing
  650. * clobbers the error counter so the trigger threshold
  651. * check will never be true.
  652. */
  653. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  654. ath9k_hw_ani_ofdm_err_trigger(ah);
  655. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  656. ath9k_hw_ani_cck_err_trigger(ah);
  657. /* NB: always restart to insure the h/w counters are reset */
  658. ath9k_ani_restart(ah);
  659. }
  660. }
  661. void ath9k_hw_ani_setup(struct ath_hw *ah)
  662. {
  663. int i;
  664. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  665. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  666. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  667. const int firpwr[] = { -78, -78, -78, -78, -80 };
  668. for (i = 0; i < 5; i++) {
  669. ah->totalSizeDesired[i] = totalSizeDesired[i];
  670. ah->coarse_high[i] = coarseHigh[i];
  671. ah->coarse_low[i] = coarseLow[i];
  672. ah->firpwr[i] = firpwr[i];
  673. }
  674. }
  675. void ath9k_hw_ani_init(struct ath_hw *ah)
  676. {
  677. int i;
  678. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Initialize ANI\n");
  679. memset(ah->ani, 0, sizeof(ah->ani));
  680. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  681. ah->ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  682. ah->ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  683. ah->ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  684. ah->ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  685. ah->ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  686. ah->ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  687. ah->ani[i].ofdmWeakSigDetectOff =
  688. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  689. ah->ani[i].cckWeakSigThreshold =
  690. ATH9K_ANI_CCK_WEAK_SIG_THR;
  691. ah->ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  692. ah->ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  693. ah->ani[i].ofdmPhyErrBase =
  694. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  695. ah->ani[i].cckPhyErrBase =
  696. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  697. }
  698. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  699. "Setting OfdmErrBase = 0x%08x\n",
  700. ah->ani[0].ofdmPhyErrBase);
  701. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  702. ah->ani[0].cckPhyErrBase);
  703. REG_WRITE(ah, AR_PHY_ERR_1, ah->ani[0].ofdmPhyErrBase);
  704. REG_WRITE(ah, AR_PHY_ERR_2, ah->ani[0].cckPhyErrBase);
  705. ath9k_enable_mib_counters(ah);
  706. ah->aniperiod = ATH9K_ANI_PERIOD;
  707. if (ah->config.enable_ani)
  708. ah->proc_phyerr |= HAL_PROCESS_ANI;
  709. }
  710. void ath9k_hw_ani_disable(struct ath_hw *ah)
  711. {
  712. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disabling ANI\n");
  713. ath9k_hw_disable_mib_counters(ah);
  714. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  715. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  716. }