ani.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  257. struct ar5416AniState *aniState;
  258. int32_t rssi;
  259. if (!DO_ANI(ah))
  260. return;
  261. aniState = ahp->ah_curani;
  262. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  263. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  264. aniState->noiseImmunityLevel + 1)) {
  265. return;
  266. }
  267. }
  268. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  269. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  270. aniState->spurImmunityLevel + 1)) {
  271. return;
  272. }
  273. }
  274. if (ah->ah_opmode == NL80211_IFTYPE_AP) {
  275. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  276. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  277. aniState->firstepLevel + 1);
  278. }
  279. return;
  280. }
  281. rssi = BEACON_RSSI(ahp);
  282. if (rssi > aniState->rssiThrHigh) {
  283. if (!aniState->ofdmWeakSigDetectOff) {
  284. if (ath9k_hw_ani_control(ah,
  285. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  286. false)) {
  287. ath9k_hw_ani_control(ah,
  288. ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  289. return;
  290. }
  291. }
  292. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  293. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  294. aniState->firstepLevel + 1);
  295. return;
  296. }
  297. } else if (rssi > aniState->rssiThrLow) {
  298. if (aniState->ofdmWeakSigDetectOff)
  299. ath9k_hw_ani_control(ah,
  300. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  301. true);
  302. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  303. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  304. aniState->firstepLevel + 1);
  305. return;
  306. } else {
  307. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  308. if (!aniState->ofdmWeakSigDetectOff)
  309. ath9k_hw_ani_control(ah,
  310. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  311. false);
  312. if (aniState->firstepLevel > 0)
  313. ath9k_hw_ani_control(ah,
  314. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  315. return;
  316. }
  317. }
  318. }
  319. static void ath9k_hw_ani_cck_err_trigger(struct ath_hal *ah)
  320. {
  321. struct ath_hal_5416 *ahp = AH5416(ah);
  322. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  323. struct ar5416AniState *aniState;
  324. int32_t rssi;
  325. if (!DO_ANI(ah))
  326. return;
  327. aniState = ahp->ah_curani;
  328. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  329. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  330. aniState->noiseImmunityLevel + 1)) {
  331. return;
  332. }
  333. }
  334. if (ah->ah_opmode == NL80211_IFTYPE_AP) {
  335. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  336. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  337. aniState->firstepLevel + 1);
  338. }
  339. return;
  340. }
  341. rssi = BEACON_RSSI(ahp);
  342. if (rssi > aniState->rssiThrLow) {
  343. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  344. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  345. aniState->firstepLevel + 1);
  346. } else {
  347. if (conf->channel->band == IEEE80211_BAND_2GHZ) {
  348. if (aniState->firstepLevel > 0)
  349. ath9k_hw_ani_control(ah,
  350. ATH9K_ANI_FIRSTEP_LEVEL, 0);
  351. }
  352. }
  353. }
  354. static void ath9k_hw_ani_lower_immunity(struct ath_hal *ah)
  355. {
  356. struct ath_hal_5416 *ahp = AH5416(ah);
  357. struct ar5416AniState *aniState;
  358. int32_t rssi;
  359. aniState = ahp->ah_curani;
  360. if (ah->ah_opmode == NL80211_IFTYPE_AP) {
  361. if (aniState->firstepLevel > 0) {
  362. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  363. aniState->firstepLevel - 1))
  364. return;
  365. }
  366. } else {
  367. rssi = BEACON_RSSI(ahp);
  368. if (rssi > aniState->rssiThrHigh) {
  369. /* XXX: Handle me */
  370. } else if (rssi > aniState->rssiThrLow) {
  371. if (aniState->ofdmWeakSigDetectOff) {
  372. if (ath9k_hw_ani_control(ah,
  373. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  374. true) == true)
  375. return;
  376. }
  377. if (aniState->firstepLevel > 0) {
  378. if (ath9k_hw_ani_control(ah,
  379. ATH9K_ANI_FIRSTEP_LEVEL,
  380. aniState->firstepLevel - 1) == true)
  381. return;
  382. }
  383. } else {
  384. if (aniState->firstepLevel > 0) {
  385. if (ath9k_hw_ani_control(ah,
  386. ATH9K_ANI_FIRSTEP_LEVEL,
  387. aniState->firstepLevel - 1) == true)
  388. return;
  389. }
  390. }
  391. }
  392. if (aniState->spurImmunityLevel > 0) {
  393. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  394. aniState->spurImmunityLevel - 1))
  395. return;
  396. }
  397. if (aniState->noiseImmunityLevel > 0) {
  398. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  399. aniState->noiseImmunityLevel - 1);
  400. return;
  401. }
  402. }
  403. static int32_t ath9k_hw_ani_get_listen_time(struct ath_hal *ah)
  404. {
  405. struct ath_hal_5416 *ahp = AH5416(ah);
  406. struct ar5416AniState *aniState;
  407. u32 txFrameCount, rxFrameCount, cycleCount;
  408. int32_t listenTime;
  409. txFrameCount = REG_READ(ah, AR_TFCNT);
  410. rxFrameCount = REG_READ(ah, AR_RFCNT);
  411. cycleCount = REG_READ(ah, AR_CCCNT);
  412. aniState = ahp->ah_curani;
  413. if (aniState->cycleCount == 0 || aniState->cycleCount > cycleCount) {
  414. listenTime = 0;
  415. ahp->ah_stats.ast_ani_lzero++;
  416. } else {
  417. int32_t ccdelta = cycleCount - aniState->cycleCount;
  418. int32_t rfdelta = rxFrameCount - aniState->rxFrameCount;
  419. int32_t tfdelta = txFrameCount - aniState->txFrameCount;
  420. listenTime = (ccdelta - rfdelta - tfdelta) / 44000;
  421. }
  422. aniState->cycleCount = cycleCount;
  423. aniState->txFrameCount = txFrameCount;
  424. aniState->rxFrameCount = rxFrameCount;
  425. return listenTime;
  426. }
  427. void ath9k_ani_reset(struct ath_hal *ah)
  428. {
  429. struct ath_hal_5416 *ahp = AH5416(ah);
  430. struct ar5416AniState *aniState;
  431. struct ath9k_channel *chan = ah->ah_curchan;
  432. int index;
  433. if (!DO_ANI(ah))
  434. return;
  435. index = ath9k_hw_get_ani_channel_idx(ah, chan);
  436. aniState = &ahp->ah_ani[index];
  437. ahp->ah_curani = aniState;
  438. if (DO_ANI(ah) && ah->ah_opmode != NL80211_IFTYPE_STATION
  439. && ah->ah_opmode != NL80211_IFTYPE_ADHOC) {
  440. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  441. "Reset ANI state opmode %u\n", ah->ah_opmode);
  442. ahp->ah_stats.ast_ani_reset++;
  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->ah_opmode == NL80211_IFTYPE_AP) {
  453. ahp->ah_curani->ofdmTrigHigh =
  454. ah->ah_config.ofdm_trig_high;
  455. ahp->ah_curani->ofdmTrigLow =
  456. ah->ah_config.ofdm_trig_low;
  457. ahp->ah_curani->cckTrigHigh =
  458. ah->ah_config.cck_trig_high;
  459. ahp->ah_curani->cckTrigLow =
  460. ah->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 (ahp->ah_hasHwPhyCounters) {
  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_hal *ah,
  493. const struct ath9k_node_stats *stats,
  494. struct ath9k_channel *chan)
  495. {
  496. struct ath_hal_5416 *ahp = AH5416(ah);
  497. struct ar5416AniState *aniState;
  498. int32_t listenTime;
  499. if (!DO_ANI(ah))
  500. return;
  501. aniState = ahp->ah_curani;
  502. ahp->ah_stats.ast_nodestats = *stats;
  503. listenTime = ath9k_hw_ani_get_listen_time(ah);
  504. if (listenTime < 0) {
  505. ahp->ah_stats.ast_ani_lneg++;
  506. ath9k_ani_restart(ah);
  507. return;
  508. }
  509. aniState->listenTime += listenTime;
  510. if (ahp->ah_hasHwPhyCounters) {
  511. u32 phyCnt1, phyCnt2;
  512. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  513. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  514. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  515. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  516. if (phyCnt1 < aniState->ofdmPhyErrBase ||
  517. phyCnt2 < aniState->cckPhyErrBase) {
  518. if (phyCnt1 < aniState->ofdmPhyErrBase) {
  519. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  520. "phyCnt1 0x%x, resetting "
  521. "counter value to 0x%x\n",
  522. phyCnt1,
  523. aniState->ofdmPhyErrBase);
  524. REG_WRITE(ah, AR_PHY_ERR_1,
  525. aniState->ofdmPhyErrBase);
  526. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  527. AR_PHY_ERR_OFDM_TIMING);
  528. }
  529. if (phyCnt2 < aniState->cckPhyErrBase) {
  530. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  531. "phyCnt2 0x%x, resetting "
  532. "counter value to 0x%x\n",
  533. phyCnt2,
  534. aniState->cckPhyErrBase);
  535. REG_WRITE(ah, AR_PHY_ERR_2,
  536. aniState->cckPhyErrBase);
  537. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  538. AR_PHY_ERR_CCK_TIMING);
  539. }
  540. return;
  541. }
  542. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  543. ahp->ah_stats.ast_ani_ofdmerrs +=
  544. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  545. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  546. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  547. ahp->ah_stats.ast_ani_cckerrs +=
  548. cckPhyErrCnt - aniState->cckPhyErrCount;
  549. aniState->cckPhyErrCount = cckPhyErrCnt;
  550. }
  551. if (aniState->listenTime > 5 * ahp->ah_aniPeriod) {
  552. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  553. aniState->ofdmTrigLow / 1000 &&
  554. aniState->cckPhyErrCount <= aniState->listenTime *
  555. aniState->cckTrigLow / 1000)
  556. ath9k_hw_ani_lower_immunity(ah);
  557. ath9k_ani_restart(ah);
  558. } else if (aniState->listenTime > ahp->ah_aniPeriod) {
  559. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  560. aniState->ofdmTrigHigh / 1000) {
  561. ath9k_hw_ani_ofdm_err_trigger(ah);
  562. ath9k_ani_restart(ah);
  563. } else if (aniState->cckPhyErrCount >
  564. aniState->listenTime * aniState->cckTrigHigh /
  565. 1000) {
  566. ath9k_hw_ani_cck_err_trigger(ah);
  567. ath9k_ani_restart(ah);
  568. }
  569. }
  570. }
  571. bool ath9k_hw_phycounters(struct ath_hal *ah)
  572. {
  573. struct ath_hal_5416 *ahp = AH5416(ah);
  574. return ahp->ah_hasHwPhyCounters ? true : false;
  575. }
  576. void ath9k_enable_mib_counters(struct ath_hal *ah)
  577. {
  578. struct ath_hal_5416 *ahp = AH5416(ah);
  579. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Enable MIB counters\n");
  580. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  581. REG_WRITE(ah, AR_FILT_OFDM, 0);
  582. REG_WRITE(ah, AR_FILT_CCK, 0);
  583. REG_WRITE(ah, AR_MIBC,
  584. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  585. & 0x0f);
  586. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  587. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  588. }
  589. void ath9k_hw_disable_mib_counters(struct ath_hal *ah)
  590. {
  591. struct ath_hal_5416 *ahp = AH5416(ah);
  592. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disable MIB counters\n");
  593. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC | AR_MIBC_CMC);
  594. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  595. REG_WRITE(ah, AR_FILT_OFDM, 0);
  596. REG_WRITE(ah, AR_FILT_CCK, 0);
  597. }
  598. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hal *ah,
  599. u32 *rxc_pcnt,
  600. u32 *rxf_pcnt,
  601. u32 *txf_pcnt)
  602. {
  603. static u32 cycles, rx_clear, rx_frame, tx_frame;
  604. u32 good = 1;
  605. u32 rc = REG_READ(ah, AR_RCCNT);
  606. u32 rf = REG_READ(ah, AR_RFCNT);
  607. u32 tf = REG_READ(ah, AR_TFCNT);
  608. u32 cc = REG_READ(ah, AR_CCCNT);
  609. if (cycles == 0 || cycles > cc) {
  610. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  611. "cycle counter wrap. ExtBusy = 0\n");
  612. good = 0;
  613. } else {
  614. u32 cc_d = cc - cycles;
  615. u32 rc_d = rc - rx_clear;
  616. u32 rf_d = rf - rx_frame;
  617. u32 tf_d = tf - tx_frame;
  618. if (cc_d != 0) {
  619. *rxc_pcnt = rc_d * 100 / cc_d;
  620. *rxf_pcnt = rf_d * 100 / cc_d;
  621. *txf_pcnt = tf_d * 100 / cc_d;
  622. } else {
  623. good = 0;
  624. }
  625. }
  626. cycles = cc;
  627. rx_frame = rf;
  628. rx_clear = rc;
  629. tx_frame = tf;
  630. return good;
  631. }
  632. /*
  633. * Process a MIB interrupt. We may potentially be invoked because
  634. * any of the MIB counters overflow/trigger so don't assume we're
  635. * here because a PHY error counter triggered.
  636. */
  637. void ath9k_hw_procmibevent(struct ath_hal *ah,
  638. const struct ath9k_node_stats *stats)
  639. {
  640. struct ath_hal_5416 *ahp = AH5416(ah);
  641. u32 phyCnt1, phyCnt2;
  642. /* Reset these counters regardless */
  643. REG_WRITE(ah, AR_FILT_OFDM, 0);
  644. REG_WRITE(ah, AR_FILT_CCK, 0);
  645. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  646. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  647. /* Clear the mib counters and save them in the stats */
  648. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  649. ahp->ah_stats.ast_nodestats = *stats;
  650. if (!DO_ANI(ah))
  651. return;
  652. /* NB: these are not reset-on-read */
  653. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  654. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  655. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  656. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  657. struct ar5416AniState *aniState = ahp->ah_curani;
  658. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  659. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  660. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  661. ahp->ah_stats.ast_ani_ofdmerrs +=
  662. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  663. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  664. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  665. ahp->ah_stats.ast_ani_cckerrs +=
  666. cckPhyErrCnt - aniState->cckPhyErrCount;
  667. aniState->cckPhyErrCount = cckPhyErrCnt;
  668. /*
  669. * NB: figure out which counter triggered. If both
  670. * trigger we'll only deal with one as the processing
  671. * clobbers the error counter so the trigger threshold
  672. * check will never be true.
  673. */
  674. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  675. ath9k_hw_ani_ofdm_err_trigger(ah);
  676. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  677. ath9k_hw_ani_cck_err_trigger(ah);
  678. /* NB: always restart to insure the h/w counters are reset */
  679. ath9k_ani_restart(ah);
  680. }
  681. }
  682. void ath9k_hw_ani_setup(struct ath_hal *ah)
  683. {
  684. struct ath_hal_5416 *ahp = AH5416(ah);
  685. int i;
  686. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  687. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  688. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  689. const int firpwr[] = { -78, -78, -78, -78, -80 };
  690. for (i = 0; i < 5; i++) {
  691. ahp->ah_totalSizeDesired[i] = totalSizeDesired[i];
  692. ahp->ah_coarseHigh[i] = coarseHigh[i];
  693. ahp->ah_coarseLow[i] = coarseLow[i];
  694. ahp->ah_firpwr[i] = firpwr[i];
  695. }
  696. }
  697. void ath9k_hw_ani_attach(struct ath_hal *ah)
  698. {
  699. struct ath_hal_5416 *ahp = AH5416(ah);
  700. int i;
  701. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Attach ANI\n");
  702. ahp->ah_hasHwPhyCounters = 1;
  703. memset(ahp->ah_ani, 0, sizeof(ahp->ah_ani));
  704. for (i = 0; i < ARRAY_SIZE(ahp->ah_ani); i++) {
  705. ahp->ah_ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  706. ahp->ah_ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  707. ahp->ah_ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  708. ahp->ah_ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  709. ahp->ah_ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  710. ahp->ah_ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  711. ahp->ah_ani[i].ofdmWeakSigDetectOff =
  712. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  713. ahp->ah_ani[i].cckWeakSigThreshold =
  714. ATH9K_ANI_CCK_WEAK_SIG_THR;
  715. ahp->ah_ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  716. ahp->ah_ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  717. if (ahp->ah_hasHwPhyCounters) {
  718. ahp->ah_ani[i].ofdmPhyErrBase =
  719. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  720. ahp->ah_ani[i].cckPhyErrBase =
  721. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  722. }
  723. }
  724. if (ahp->ah_hasHwPhyCounters) {
  725. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  726. "Setting OfdmErrBase = 0x%08x\n",
  727. ahp->ah_ani[0].ofdmPhyErrBase);
  728. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  729. ahp->ah_ani[0].cckPhyErrBase);
  730. REG_WRITE(ah, AR_PHY_ERR_1, ahp->ah_ani[0].ofdmPhyErrBase);
  731. REG_WRITE(ah, AR_PHY_ERR_2, ahp->ah_ani[0].cckPhyErrBase);
  732. ath9k_enable_mib_counters(ah);
  733. }
  734. ahp->ah_aniPeriod = ATH9K_ANI_PERIOD;
  735. if (ah->ah_config.enable_ani)
  736. ahp->ah_procPhyErr |= HAL_PROCESS_ANI;
  737. }
  738. void ath9k_hw_ani_detach(struct ath_hal *ah)
  739. {
  740. struct ath_hal_5416 *ahp = AH5416(ah);
  741. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Detach ANI\n");
  742. if (ahp->ah_hasHwPhyCounters) {
  743. ath9k_hw_disable_mib_counters(ah);
  744. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  745. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  746. }
  747. }