ani.c 23 KB

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