ani.c 24 KB

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