ani.c 23 KB

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