ani.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 (ah->has_hw_phycounters) {
  218. if (aniState->ofdmTrigHigh > AR_PHY_COUNTMAX) {
  219. aniState->ofdmPhyErrBase = 0;
  220. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  221. "OFDM Trigger is too high for hw counters\n");
  222. } else {
  223. aniState->ofdmPhyErrBase =
  224. AR_PHY_COUNTMAX - aniState->ofdmTrigHigh;
  225. }
  226. if (aniState->cckTrigHigh > AR_PHY_COUNTMAX) {
  227. aniState->cckPhyErrBase = 0;
  228. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  229. "CCK Trigger is too high for hw counters\n");
  230. } else {
  231. aniState->cckPhyErrBase =
  232. AR_PHY_COUNTMAX - aniState->cckTrigHigh;
  233. }
  234. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  235. "Writing ofdmbase=%u cckbase=%u\n",
  236. aniState->ofdmPhyErrBase,
  237. aniState->cckPhyErrBase);
  238. REG_WRITE(ah, AR_PHY_ERR_1, aniState->ofdmPhyErrBase);
  239. REG_WRITE(ah, AR_PHY_ERR_2, aniState->cckPhyErrBase);
  240. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  241. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  242. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  243. }
  244. aniState->ofdmPhyErrCount = 0;
  245. aniState->cckPhyErrCount = 0;
  246. }
  247. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hw *ah)
  248. {
  249. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  250. struct ar5416AniState *aniState;
  251. int32_t rssi;
  252. if (!DO_ANI(ah))
  253. return;
  254. aniState = ah->curani;
  255. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  256. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  257. aniState->noiseImmunityLevel + 1)) {
  258. return;
  259. }
  260. }
  261. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  262. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  263. aniState->spurImmunityLevel + 1)) {
  264. return;
  265. }
  266. }
  267. if (ah->opmode == NL80211_IFTYPE_AP) {
  268. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  269. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  270. aniState->firstepLevel + 1);
  271. }
  272. return;
  273. }
  274. rssi = BEACON_RSSI(ah);
  275. if (rssi > aniState->rssiThrHigh) {
  276. if (!aniState->ofdmWeakSigDetectOff) {
  277. if (ath9k_hw_ani_control(ah,
  278. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  279. false)) {
  280. ath9k_hw_ani_control(ah,
  281. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  282. return;
  283. }
  284. }
  285. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  286. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  287. aniState->firstepLevel + 1);
  288. return;
  289. }
  290. } else if (rssi > aniState->rssiThrLow) {
  291. if (aniState->ofdmWeakSigDetectOff)
  292. ath9k_hw_ani_control(ah,
  293. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  294. true);
  295. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  296. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  297. aniState->firstepLevel + 1);
  298. return;
  299. } else {
  300. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  301. if (!aniState->ofdmWeakSigDetectOff)
  302. ath9k_hw_ani_control(ah,
  303. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  304. false);
  305. if (aniState->firstepLevel > 0)
  306. ath9k_hw_ani_control(ah,
  307. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  308. return;
  309. }
  310. }
  311. }
  312. static void ath9k_hw_ani_cck_err_trigger(struct ath_hw *ah)
  313. {
  314. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  315. struct ar5416AniState *aniState;
  316. int32_t rssi;
  317. if (!DO_ANI(ah))
  318. return;
  319. aniState = ah->curani;
  320. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  321. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  322. aniState->noiseImmunityLevel + 1)) {
  323. return;
  324. }
  325. }
  326. if (ah->opmode == NL80211_IFTYPE_AP) {
  327. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  328. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  329. aniState->firstepLevel + 1);
  330. }
  331. return;
  332. }
  333. rssi = BEACON_RSSI(ah);
  334. if (rssi > aniState->rssiThrLow) {
  335. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  336. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  337. aniState->firstepLevel + 1);
  338. } else {
  339. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  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. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  433. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  434. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  435. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  436. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  437. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  438. ATH9K_ANI_CCK_WEAK_SIG_THR);
  439. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
  440. ATH9K_RX_FILTER_PHYERR);
  441. if (ah->opmode == NL80211_IFTYPE_AP) {
  442. ah->curani->ofdmTrigHigh =
  443. ah->config.ofdm_trig_high;
  444. ah->curani->ofdmTrigLow =
  445. ah->config.ofdm_trig_low;
  446. ah->curani->cckTrigHigh =
  447. ah->config.cck_trig_high;
  448. ah->curani->cckTrigLow =
  449. ah->config.cck_trig_low;
  450. }
  451. ath9k_ani_restart(ah);
  452. return;
  453. }
  454. if (aniState->noiseImmunityLevel != 0)
  455. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  456. aniState->noiseImmunityLevel);
  457. if (aniState->spurImmunityLevel != 0)
  458. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  459. aniState->spurImmunityLevel);
  460. if (aniState->ofdmWeakSigDetectOff)
  461. ath9k_hw_ani_control(ah, ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  462. !aniState->ofdmWeakSigDetectOff);
  463. if (aniState->cckWeakSigThreshold)
  464. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  465. aniState->cckWeakSigThreshold);
  466. if (aniState->firstepLevel != 0)
  467. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  468. aniState->firstepLevel);
  469. if (ah->has_hw_phycounters) {
  470. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) &
  471. ~ATH9K_RX_FILTER_PHYERR);
  472. ath9k_ani_restart(ah);
  473. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  474. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  475. } else {
  476. ath9k_ani_restart(ah);
  477. ath9k_hw_setrxfilter(ah, ath9k_hw_getrxfilter(ah) |
  478. ATH9K_RX_FILTER_PHYERR);
  479. }
  480. }
  481. void ath9k_hw_ani_monitor(struct ath_hw *ah,
  482. const struct ath9k_node_stats *stats,
  483. struct ath9k_channel *chan)
  484. {
  485. struct ar5416AniState *aniState;
  486. int32_t listenTime;
  487. if (!DO_ANI(ah))
  488. return;
  489. aniState = ah->curani;
  490. ah->stats.ast_nodestats = *stats;
  491. listenTime = ath9k_hw_ani_get_listen_time(ah);
  492. if (listenTime < 0) {
  493. ah->stats.ast_ani_lneg++;
  494. ath9k_ani_restart(ah);
  495. return;
  496. }
  497. aniState->listenTime += listenTime;
  498. if (ah->has_hw_phycounters) {
  499. u32 phyCnt1, phyCnt2;
  500. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  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,
  511. aniState->ofdmPhyErrBase);
  512. REG_WRITE(ah, AR_PHY_ERR_1,
  513. aniState->ofdmPhyErrBase);
  514. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  515. AR_PHY_ERR_OFDM_TIMING);
  516. }
  517. if (phyCnt2 < aniState->cckPhyErrBase) {
  518. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  519. "phyCnt2 0x%x, resetting "
  520. "counter value to 0x%x\n",
  521. phyCnt2,
  522. 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. }
  539. if (aniState->listenTime > 5 * ah->aniperiod) {
  540. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  541. aniState->ofdmTrigLow / 1000 &&
  542. aniState->cckPhyErrCount <= aniState->listenTime *
  543. aniState->cckTrigLow / 1000)
  544. ath9k_hw_ani_lower_immunity(ah);
  545. ath9k_ani_restart(ah);
  546. } else if (aniState->listenTime > ah->aniperiod) {
  547. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  548. aniState->ofdmTrigHigh / 1000) {
  549. ath9k_hw_ani_ofdm_err_trigger(ah);
  550. ath9k_ani_restart(ah);
  551. } else if (aniState->cckPhyErrCount >
  552. aniState->listenTime * aniState->cckTrigHigh /
  553. 1000) {
  554. ath9k_hw_ani_cck_err_trigger(ah);
  555. ath9k_ani_restart(ah);
  556. }
  557. }
  558. }
  559. bool ath9k_hw_phycounters(struct ath_hw *ah)
  560. {
  561. return ah->has_hw_phycounters ? true : false;
  562. }
  563. void ath9k_enable_mib_counters(struct ath_hw *ah)
  564. {
  565. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Enable MIB counters\n");
  566. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  567. REG_WRITE(ah, AR_FILT_OFDM, 0);
  568. REG_WRITE(ah, AR_FILT_CCK, 0);
  569. REG_WRITE(ah, AR_MIBC,
  570. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  571. & 0x0f);
  572. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  573. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  574. }
  575. /* Freeze the MIB counters, get the stats and then clear them */
  576. void ath9k_hw_disable_mib_counters(struct ath_hw *ah)
  577. {
  578. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disable MIB counters\n");
  579. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
  580. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  581. REG_WRITE(ah, AR_MIBC, AR_MIBC_CMC);
  582. REG_WRITE(ah, AR_FILT_OFDM, 0);
  583. REG_WRITE(ah, AR_FILT_CCK, 0);
  584. }
  585. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hw *ah,
  586. u32 *rxc_pcnt,
  587. u32 *rxf_pcnt,
  588. u32 *txf_pcnt)
  589. {
  590. static u32 cycles, rx_clear, rx_frame, tx_frame;
  591. u32 good = 1;
  592. u32 rc = REG_READ(ah, AR_RCCNT);
  593. u32 rf = REG_READ(ah, AR_RFCNT);
  594. u32 tf = REG_READ(ah, AR_TFCNT);
  595. u32 cc = REG_READ(ah, AR_CCCNT);
  596. if (cycles == 0 || cycles > cc) {
  597. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  598. "cycle counter wrap. ExtBusy = 0\n");
  599. good = 0;
  600. } else {
  601. u32 cc_d = cc - cycles;
  602. u32 rc_d = rc - rx_clear;
  603. u32 rf_d = rf - rx_frame;
  604. u32 tf_d = tf - tx_frame;
  605. if (cc_d != 0) {
  606. *rxc_pcnt = rc_d * 100 / cc_d;
  607. *rxf_pcnt = rf_d * 100 / cc_d;
  608. *txf_pcnt = tf_d * 100 / cc_d;
  609. } else {
  610. good = 0;
  611. }
  612. }
  613. cycles = cc;
  614. rx_frame = rf;
  615. rx_clear = rc;
  616. tx_frame = tf;
  617. return good;
  618. }
  619. /*
  620. * Process a MIB interrupt. We may potentially be invoked because
  621. * any of the MIB counters overflow/trigger so don't assume we're
  622. * here because a PHY error counter triggered.
  623. */
  624. void ath9k_hw_procmibevent(struct ath_hw *ah,
  625. const struct ath9k_node_stats *stats)
  626. {
  627. u32 phyCnt1, phyCnt2;
  628. /* Reset these counters regardless */
  629. REG_WRITE(ah, AR_FILT_OFDM, 0);
  630. REG_WRITE(ah, AR_FILT_CCK, 0);
  631. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  632. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  633. /* Clear the mib counters and save them in the stats */
  634. ath9k_hw_update_mibstats(ah, &ah->ah_mibStats);
  635. ah->stats.ast_nodestats = *stats;
  636. if (!DO_ANI(ah))
  637. return;
  638. /* NB: these are not reset-on-read */
  639. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  640. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  641. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  642. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  643. struct ar5416AniState *aniState = ah->curani;
  644. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  645. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  646. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  647. ah->stats.ast_ani_ofdmerrs +=
  648. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  649. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  650. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  651. ah->stats.ast_ani_cckerrs +=
  652. cckPhyErrCnt - aniState->cckPhyErrCount;
  653. aniState->cckPhyErrCount = cckPhyErrCnt;
  654. /*
  655. * NB: figure out which counter triggered. If both
  656. * trigger we'll only deal with one as the processing
  657. * clobbers the error counter so the trigger threshold
  658. * check will never be true.
  659. */
  660. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  661. ath9k_hw_ani_ofdm_err_trigger(ah);
  662. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  663. ath9k_hw_ani_cck_err_trigger(ah);
  664. /* NB: always restart to insure the h/w counters are reset */
  665. ath9k_ani_restart(ah);
  666. }
  667. }
  668. void ath9k_hw_ani_setup(struct ath_hw *ah)
  669. {
  670. int i;
  671. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  672. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  673. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  674. const int firpwr[] = { -78, -78, -78, -78, -80 };
  675. for (i = 0; i < 5; i++) {
  676. ah->totalSizeDesired[i] = totalSizeDesired[i];
  677. ah->coarse_high[i] = coarseHigh[i];
  678. ah->coarse_low[i] = coarseLow[i];
  679. ah->firpwr[i] = firpwr[i];
  680. }
  681. }
  682. void ath9k_hw_ani_attach(struct ath_hw *ah)
  683. {
  684. int i;
  685. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Attach ANI\n");
  686. ah->has_hw_phycounters = 1;
  687. memset(ah->ani, 0, sizeof(ah->ani));
  688. for (i = 0; i < ARRAY_SIZE(ah->ani); i++) {
  689. ah->ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  690. ah->ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  691. ah->ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  692. ah->ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  693. ah->ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  694. ah->ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  695. ah->ani[i].ofdmWeakSigDetectOff =
  696. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  697. ah->ani[i].cckWeakSigThreshold =
  698. ATH9K_ANI_CCK_WEAK_SIG_THR;
  699. ah->ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  700. ah->ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  701. if (ah->has_hw_phycounters) {
  702. ah->ani[i].ofdmPhyErrBase =
  703. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  704. ah->ani[i].cckPhyErrBase =
  705. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  706. }
  707. }
  708. if (ah->has_hw_phycounters) {
  709. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  710. "Setting OfdmErrBase = 0x%08x\n",
  711. ah->ani[0].ofdmPhyErrBase);
  712. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  713. ah->ani[0].cckPhyErrBase);
  714. REG_WRITE(ah, AR_PHY_ERR_1, ah->ani[0].ofdmPhyErrBase);
  715. REG_WRITE(ah, AR_PHY_ERR_2, ah->ani[0].cckPhyErrBase);
  716. ath9k_enable_mib_counters(ah);
  717. }
  718. ah->aniperiod = ATH9K_ANI_PERIOD;
  719. if (ah->config.enable_ani)
  720. ah->proc_phyerr |= HAL_PROCESS_ANI;
  721. }
  722. void ath9k_hw_ani_detach(struct ath_hw *ah)
  723. {
  724. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Detach ANI\n");
  725. if (ah->has_hw_phycounters) {
  726. ath9k_hw_disable_mib_counters(ah);
  727. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  728. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  729. }
  730. }