ani.c 24 KB

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