ani.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. struct ath9k_channel *chan)
  486. {
  487. struct ar5416AniState *aniState;
  488. int32_t listenTime;
  489. u32 phyCnt1, phyCnt2;
  490. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  491. if (!DO_ANI(ah))
  492. return;
  493. aniState = ah->curani;
  494. listenTime = ath9k_hw_ani_get_listen_time(ah);
  495. if (listenTime < 0) {
  496. ah->stats.ast_ani_lneg++;
  497. ath9k_ani_restart(ah);
  498. return;
  499. }
  500. aniState->listenTime += listenTime;
  501. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  502. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  503. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  504. if (phyCnt1 < aniState->ofdmPhyErrBase ||
  505. phyCnt2 < aniState->cckPhyErrBase) {
  506. if (phyCnt1 < aniState->ofdmPhyErrBase) {
  507. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  508. "phyCnt1 0x%x, resetting "
  509. "counter value to 0x%x\n",
  510. phyCnt1, aniState->ofdmPhyErrBase);
  511. REG_WRITE(ah, AR_PHY_ERR_1,
  512. aniState->ofdmPhyErrBase);
  513. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  514. AR_PHY_ERR_OFDM_TIMING);
  515. }
  516. if (phyCnt2 < aniState->cckPhyErrBase) {
  517. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  518. "phyCnt2 0x%x, resetting "
  519. "counter value to 0x%x\n",
  520. phyCnt2, aniState->cckPhyErrBase);
  521. REG_WRITE(ah, AR_PHY_ERR_2,
  522. aniState->cckPhyErrBase);
  523. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  524. AR_PHY_ERR_CCK_TIMING);
  525. }
  526. return;
  527. }
  528. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  529. ah->stats.ast_ani_ofdmerrs +=
  530. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  531. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  532. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  533. ah->stats.ast_ani_cckerrs +=
  534. cckPhyErrCnt - aniState->cckPhyErrCount;
  535. aniState->cckPhyErrCount = cckPhyErrCnt;
  536. if (aniState->listenTime > 5 * ah->aniperiod) {
  537. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  538. aniState->ofdmTrigLow / 1000 &&
  539. aniState->cckPhyErrCount <= aniState->listenTime *
  540. aniState->cckTrigLow / 1000)
  541. ath9k_hw_ani_lower_immunity(ah);
  542. ath9k_ani_restart(ah);
  543. } else if (aniState->listenTime > ah->aniperiod) {
  544. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  545. aniState->ofdmTrigHigh / 1000) {
  546. ath9k_hw_ani_ofdm_err_trigger(ah);
  547. ath9k_ani_restart(ah);
  548. } else if (aniState->cckPhyErrCount >
  549. aniState->listenTime * aniState->cckTrigHigh /
  550. 1000) {
  551. ath9k_hw_ani_cck_err_trigger(ah);
  552. ath9k_ani_restart(ah);
  553. }
  554. }
  555. }
  556. void ath9k_enable_mib_counters(struct ath_hw *ah)
  557. {
  558. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Enable MIB counters\n");
  559. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  560. REG_WRITE(ah, AR_FILT_OFDM, 0);
  561. REG_WRITE(ah, AR_FILT_CCK, 0);
  562. REG_WRITE(ah, AR_MIBC,
  563. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  564. & 0x0f);
  565. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  566. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  567. }
  568. /* Freeze the MIB counters, get the stats and then clear them */
  569. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  570. {
  571. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disable MIB counters\n");
  572. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  573. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  574. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  575. REG_WRITE(ah, AR_FILT_OFDM, 0);
  576. REG_WRITE(ah, AR_FILT_CCK, 0);
  577. }
  578. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hw *ah,
  579. u32 *rxc_pcnt,
  580. u32 *rxf_pcnt,
  581. u32 *txf_pcnt)
  582. {
  583. static u32 cycles, rx_clear, rx_frame, tx_frame;
  584. u32 good = 1;
  585. u32 rc = REG_READ(ah, AR_RCCNT);
  586. u32 rf = REG_READ(ah, AR_RFCNT);
  587. u32 tf = REG_READ(ah, AR_TFCNT);
  588. u32 cc = REG_READ(ah, AR_CCCNT);
  589. if (cycles == 0 || cycles > cc) {
  590. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  591. "cycle counter wrap. ExtBusy = 0\n");
  592. good = 0;
  593. } else {
  594. u32 cc_d = cc - cycles;
  595. u32 rc_d = rc - rx_clear;
  596. u32 rf_d = rf - rx_frame;
  597. u32 tf_d = tf - tx_frame;
  598. if (cc_d != 0) {
  599. *rxc_pcnt = rc_d * 100 / cc_d;
  600. *rxf_pcnt = rf_d * 100 / cc_d;
  601. *txf_pcnt = tf_d * 100 / cc_d;
  602. } else {
  603. good = 0;
  604. }
  605. }
  606. cycles = cc;
  607. rx_frame = rf;
  608. rx_clear = rc;
  609. tx_frame = tf;
  610. return good;
  611. }
  612. /*
  613. * Process a MIB interrupt. We may potentially be invoked because
  614. * any of the MIB counters overflow/trigger so don't assume we're
  615. * here because a PHY error counter triggered.
  616. */
  617. void ath9k_hw_procmibevent(struct ath_hw *ah)
  618. {
  619. u32 phyCnt1, phyCnt2;
  620. /* Reset these counters regardless */
  621. REG_WRITE(ah, AR_FILT_OFDM, 0);
  622. REG_WRITE(ah, AR_FILT_CCK, 0);
  623. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  624. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  625. /* Clear the mib counters and save them in the stats */
  626. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  627. if (!DO_ANI(ah))
  628. return;
  629. /* NB: these are not reset-on-read */
  630. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  631. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  632. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  633. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  634. struct ar5416AniState *aniState = ah->curani;
  635. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  636. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  637. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  638. ah->stats.ast_ani_ofdmerrs +=
  639. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  640. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  641. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  642. ah->stats.ast_ani_cckerrs +=
  643. cckPhyErrCnt - aniState->cckPhyErrCount;
  644. aniState->cckPhyErrCount = cckPhyErrCnt;
  645. /*
  646. * NB: figure out which counter triggered. If both
  647. * trigger we'll only deal with one as the processing
  648. * clobbers the error counter so the trigger threshold
  649. * check will never be true.
  650. */
  651. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  652. ath9k_hw_ani_ofdm_err_trigger(ah);
  653. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  654. ath9k_hw_ani_cck_err_trigger(ah);
  655. /* NB: always restart to insure the h/w counters are reset */
  656. ath9k_ani_restart(ah);
  657. }
  658. }
  659. void ath9k_hw_ani_setup(struct ath_hw *ah)
  660. {
  661. int i;
  662. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  663. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  664. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  665. const int firpwr[] = { -78, -78, -78, -78, -80 };
  666. for (i = 0; i < 5; i++) {
  667. ah->totalSizeDesired[i] = totalSizeDesired[i];
  668. ah->coarse_high[i] = coarseHigh[i];
  669. ah->coarse_low[i] = coarseLow[i];
  670. ah->firpwr[i] = firpwr[i];
  671. }
  672. }
  673. void ath9k_hw_ani_init(struct ath_hw *ah)
  674. {
  675. int i;
  676. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Initialize ANI\n");
  677. memset(ah->ani, 0, sizeof(ah->ani));
  678. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  679. ah->ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  680. ah->ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  681. ah->ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  682. ah->ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  683. ah->ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  684. ah->ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  685. ah->ani[i].ofdmWeakSigDetectOff =
  686. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  687. ah->ani[i].cckWeakSigThreshold =
  688. ATH9K_ANI_CCK_WEAK_SIG_THR;
  689. ah->ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  690. ah->ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  691. ah->ani[i].ofdmPhyErrBase =
  692. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  693. ah->ani[i].cckPhyErrBase =
  694. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  695. }
  696. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  697. "Setting OfdmErrBase = 0x%08x\n",
  698. ah->ani[0].ofdmPhyErrBase);
  699. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  700. ah->ani[0].cckPhyErrBase);
  701. REG_WRITE(ah, AR_PHY_ERR_1, ah->ani[0].ofdmPhyErrBase);
  702. REG_WRITE(ah, AR_PHY_ERR_2, ah->ani[0].cckPhyErrBase);
  703. ath9k_enable_mib_counters(ah);
  704. ah->aniperiod = ATH9K_ANI_PERIOD;
  705. if (ah->config.enable_ani)
  706. ah->proc_phyerr |= HAL_PROCESS_ANI;
  707. }
  708. void ath9k_hw_ani_disable(struct ath_hw *ah)
  709. {
  710. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disabling ANI\n");
  711. ath9k_hw_disable_mib_counters(ah);
  712. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  713. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  714. }