ani.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Copyright (C) 2010 Bruno Randolf <br1@einfach.org>
  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 "ath5k.h"
  17. #include "base.h"
  18. #include "reg.h"
  19. #include "debug.h"
  20. #include "ani.h"
  21. /**
  22. * DOC: Basic ANI Operation
  23. *
  24. * Adaptive Noise Immunity (ANI) controls five noise immunity parameters
  25. * depending on the amount of interference in the environment, increasing
  26. * or reducing sensitivity as necessary.
  27. *
  28. * The parameters are:
  29. * - "noise immunity"
  30. * - "spur immunity"
  31. * - "firstep level"
  32. * - "OFDM weak signal detection"
  33. * - "CCK weak signal detection"
  34. *
  35. * Basically we look at the amount of ODFM and CCK timing errors we get and then
  36. * raise or lower immunity accordingly by setting one or more of these
  37. * parameters.
  38. * Newer chipsets have PHY error counters in hardware which will generate a MIB
  39. * interrupt when they overflow. Older hardware has too enable PHY error frames
  40. * by setting a RX flag and then count every single PHY error. When a specified
  41. * threshold of errors has been reached we will raise immunity.
  42. * Also we regularly check the amount of errors and lower or raise immunity as
  43. * necessary.
  44. */
  45. /*** ANI parameter control ***/
  46. /**
  47. * ath5k_ani_set_noise_immunity_level() - Set noise immunity level
  48. *
  49. * @level: level between 0 and @ATH5K_ANI_MAX_NOISE_IMM_LVL
  50. */
  51. void
  52. ath5k_ani_set_noise_immunity_level(struct ath5k_hw *ah, int level)
  53. {
  54. /* TODO:
  55. * ANI documents suggest the following five levels to use, but the HAL
  56. * and ath9k use only use the last two levels, making this
  57. * essentially an on/off option. There *may* be a reason for this (???),
  58. * so i stick with the HAL version for now...
  59. */
  60. #if 0
  61. const s8 hi[] = { -18, -18, -16, -14, -12 };
  62. const s8 lo[] = { -52, -56, -60, -64, -70 };
  63. const s8 sz[] = { -34, -41, -48, -55, -62 };
  64. const s8 fr[] = { -70, -72, -75, -78, -80 };
  65. #else
  66. const s8 sz[] = { -55, -62 };
  67. const s8 lo[] = { -64, -70 };
  68. const s8 hi[] = { -14, -12 };
  69. const s8 fr[] = { -78, -80 };
  70. #endif
  71. if (level < 0 || level >= ARRAY_SIZE(sz)) {
  72. ATH5K_ERR(ah->ah_sc, "noise immuniy level %d out of range",
  73. level);
  74. return;
  75. }
  76. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  77. AR5K_PHY_DESIRED_SIZE_TOT, sz[level]);
  78. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_AGCCOARSE,
  79. AR5K_PHY_AGCCOARSE_LO, lo[level]);
  80. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_AGCCOARSE,
  81. AR5K_PHY_AGCCOARSE_HI, hi[level]);
  82. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SIG,
  83. AR5K_PHY_SIG_FIRPWR, fr[level]);
  84. ah->ah_sc->ani_state.noise_imm_level = level;
  85. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "new level %d", level);
  86. }
  87. /**
  88. * ath5k_ani_set_spur_immunity_level() - Set spur immunity level
  89. *
  90. * @level: level between 0 and @max_spur_level (the maximum level is dependent
  91. * on the chip revision).
  92. */
  93. void
  94. ath5k_ani_set_spur_immunity_level(struct ath5k_hw *ah, int level)
  95. {
  96. const int val[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  97. if (level < 0 || level >= ARRAY_SIZE(val) ||
  98. level > ah->ah_sc->ani_state.max_spur_level) {
  99. ATH5K_ERR(ah->ah_sc, "spur immunity level %d out of range",
  100. level);
  101. return;
  102. }
  103. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  104. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1, val[level]);
  105. ah->ah_sc->ani_state.spur_level = level;
  106. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "new level %d", level);
  107. }
  108. /**
  109. * ath5k_ani_set_firstep_level() - Set "firstep" level
  110. *
  111. * @level: level between 0 and @ATH5K_ANI_MAX_FIRSTEP_LVL
  112. */
  113. void
  114. ath5k_ani_set_firstep_level(struct ath5k_hw *ah, int level)
  115. {
  116. const int val[] = { 0, 4, 8 };
  117. if (level < 0 || level >= ARRAY_SIZE(val)) {
  118. ATH5K_ERR(ah->ah_sc, "firstep level %d out of range", level);
  119. return;
  120. }
  121. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SIG,
  122. AR5K_PHY_SIG_FIRSTEP, val[level]);
  123. ah->ah_sc->ani_state.firstep_level = level;
  124. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "new level %d", level);
  125. }
  126. /**
  127. * ath5k_ani_set_ofdm_weak_signal_detection() - Control OFDM weak signal
  128. * detection
  129. *
  130. * @on: turn on or off
  131. */
  132. void
  133. ath5k_ani_set_ofdm_weak_signal_detection(struct ath5k_hw *ah, bool on)
  134. {
  135. const int m1l[] = { 127, 50 };
  136. const int m2l[] = { 127, 40 };
  137. const int m1[] = { 127, 0x4d };
  138. const int m2[] = { 127, 0x40 };
  139. const int m2cnt[] = { 31, 16 };
  140. const int m2lcnt[] = { 63, 48 };
  141. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
  142. AR5K_PHY_WEAK_OFDM_LOW_THR_M1, m1l[on]);
  143. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
  144. AR5K_PHY_WEAK_OFDM_LOW_THR_M2, m2l[on]);
  145. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
  146. AR5K_PHY_WEAK_OFDM_HIGH_THR_M1, m1[on]);
  147. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
  148. AR5K_PHY_WEAK_OFDM_HIGH_THR_M2, m2[on]);
  149. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_HIGH_THR,
  150. AR5K_PHY_WEAK_OFDM_HIGH_THR_M2_COUNT, m2cnt[on]);
  151. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
  152. AR5K_PHY_WEAK_OFDM_LOW_THR_M2_COUNT, m2lcnt[on]);
  153. if (on)
  154. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
  155. AR5K_PHY_WEAK_OFDM_LOW_THR_SELFCOR_EN);
  156. else
  157. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_WEAK_OFDM_LOW_THR,
  158. AR5K_PHY_WEAK_OFDM_LOW_THR_SELFCOR_EN);
  159. ah->ah_sc->ani_state.ofdm_weak_sig = on;
  160. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "turned %s",
  161. on ? "on" : "off");
  162. }
  163. /**
  164. * ath5k_ani_set_cck_weak_signal_detection() - control CCK weak signal detection
  165. *
  166. * @on: turn on or off
  167. */
  168. void
  169. ath5k_ani_set_cck_weak_signal_detection(struct ath5k_hw *ah, bool on)
  170. {
  171. const int val[] = { 8, 6 };
  172. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_CCK_CROSSCORR,
  173. AR5K_PHY_CCK_CROSSCORR_WEAK_SIG_THR, val[on]);
  174. ah->ah_sc->ani_state.cck_weak_sig = on;
  175. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "turned %s",
  176. on ? "on" : "off");
  177. }
  178. /*** ANI algorithm ***/
  179. /**
  180. * ath5k_ani_raise_immunity() - Increase noise immunity
  181. *
  182. * @ofdm_trigger: If this is true we are called because of too many OFDM errors,
  183. * the algorithm will tune more parameters then.
  184. *
  185. * Try to raise noise immunity (=decrease sensitivity) in several steps
  186. * depending on the average RSSI of the beacons we received.
  187. */
  188. static void
  189. ath5k_ani_raise_immunity(struct ath5k_hw *ah, struct ath5k_ani_state *as,
  190. bool ofdm_trigger)
  191. {
  192. int rssi = ah->ah_beacon_rssi_avg.avg;
  193. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "raise immunity (%s)",
  194. ofdm_trigger ? "ODFM" : "CCK");
  195. /* first: raise noise immunity */
  196. if (as->noise_imm_level < ATH5K_ANI_MAX_NOISE_IMM_LVL) {
  197. ath5k_ani_set_noise_immunity_level(ah, as->noise_imm_level + 1);
  198. return;
  199. }
  200. /* only OFDM: raise spur immunity level */
  201. if (ofdm_trigger &&
  202. as->spur_level < ah->ah_sc->ani_state.max_spur_level) {
  203. ath5k_ani_set_spur_immunity_level(ah, as->spur_level + 1);
  204. return;
  205. }
  206. /* AP mode */
  207. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP) {
  208. if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL)
  209. ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
  210. return;
  211. }
  212. /* STA and IBSS mode */
  213. /* TODO: for IBSS mode it would be better to keep a beacon RSSI average
  214. * per each neighbour node and use the minimum of these, to make sure we
  215. * don't shut out a remote node by raising immunity too high. */
  216. if (rssi > ATH5K_ANI_RSSI_THR_HIGH) {
  217. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  218. "beacon RSSI high");
  219. /* only OFDM: beacon RSSI is high, we can disable ODFM weak
  220. * signal detection */
  221. if (ofdm_trigger && as->ofdm_weak_sig == true) {
  222. ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
  223. ath5k_ani_set_spur_immunity_level(ah, 0);
  224. return;
  225. }
  226. /* as a last resort or CCK: raise firstep level */
  227. if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL) {
  228. ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
  229. return;
  230. }
  231. } else if (rssi > ATH5K_ANI_RSSI_THR_LOW) {
  232. /* beacon RSSI in mid range, we need OFDM weak signal detect,
  233. * but can raise firstep level */
  234. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  235. "beacon RSSI mid");
  236. if (ofdm_trigger && as->ofdm_weak_sig == false)
  237. ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
  238. if (as->firstep_level < ATH5K_ANI_MAX_FIRSTEP_LVL)
  239. ath5k_ani_set_firstep_level(ah, as->firstep_level + 1);
  240. return;
  241. } else if (ah->ah_current_channel->band == IEEE80211_BAND_2GHZ) {
  242. /* beacon RSSI is low. in B/G mode turn of OFDM weak signal
  243. * detect and zero firstep level to maximize CCK sensitivity */
  244. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  245. "beacon RSSI low, 2GHz");
  246. if (ofdm_trigger && as->ofdm_weak_sig == true)
  247. ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
  248. if (as->firstep_level > 0)
  249. ath5k_ani_set_firstep_level(ah, 0);
  250. return;
  251. }
  252. /* TODO: why not?:
  253. if (as->cck_weak_sig == true) {
  254. ath5k_ani_set_cck_weak_signal_detection(ah, false);
  255. }
  256. */
  257. }
  258. /**
  259. * ath5k_ani_lower_immunity() - Decrease noise immunity
  260. *
  261. * Try to lower noise immunity (=increase sensitivity) in several steps
  262. * depending on the average RSSI of the beacons we received.
  263. */
  264. static void
  265. ath5k_ani_lower_immunity(struct ath5k_hw *ah, struct ath5k_ani_state *as)
  266. {
  267. int rssi = ah->ah_beacon_rssi_avg.avg;
  268. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "lower immunity");
  269. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP) {
  270. /* AP mode */
  271. if (as->firstep_level > 0) {
  272. ath5k_ani_set_firstep_level(ah, as->firstep_level - 1);
  273. return;
  274. }
  275. } else {
  276. /* STA and IBSS mode (see TODO above) */
  277. if (rssi > ATH5K_ANI_RSSI_THR_HIGH) {
  278. /* beacon signal is high, leave OFDM weak signal
  279. * detection off or it may oscillate
  280. * TODO: who said it's off??? */
  281. } else if (rssi > ATH5K_ANI_RSSI_THR_LOW) {
  282. /* beacon RSSI is mid-range: turn on ODFM weak signal
  283. * detection and next, lower firstep level */
  284. if (as->ofdm_weak_sig == false) {
  285. ath5k_ani_set_ofdm_weak_signal_detection(ah,
  286. true);
  287. return;
  288. }
  289. if (as->firstep_level > 0) {
  290. ath5k_ani_set_firstep_level(ah,
  291. as->firstep_level - 1);
  292. return;
  293. }
  294. } else {
  295. /* beacon signal is low: only reduce firstep level */
  296. if (as->firstep_level > 0) {
  297. ath5k_ani_set_firstep_level(ah,
  298. as->firstep_level - 1);
  299. return;
  300. }
  301. }
  302. }
  303. /* all modes */
  304. if (as->spur_level > 0) {
  305. ath5k_ani_set_spur_immunity_level(ah, as->spur_level - 1);
  306. return;
  307. }
  308. /* finally, reduce noise immunity */
  309. if (as->noise_imm_level > 0) {
  310. ath5k_ani_set_noise_immunity_level(ah, as->noise_imm_level - 1);
  311. return;
  312. }
  313. }
  314. /**
  315. * ath5k_hw_ani_get_listen_time() - Calculate time spent listening
  316. *
  317. * Return an approximation of the time spent "listening" in milliseconds (ms)
  318. * since the last call of this function by deducting the cycles spent
  319. * transmitting and receiving from the total cycle count.
  320. * Save profile count values for debugging/statistics and because we might want
  321. * to use them later.
  322. *
  323. * We assume no one else clears these registers!
  324. */
  325. static int
  326. ath5k_hw_ani_get_listen_time(struct ath5k_hw *ah, struct ath5k_ani_state *as)
  327. {
  328. int listen;
  329. /* freeze */
  330. ath5k_hw_reg_write(ah, AR5K_MIBC_FMC, AR5K_MIBC);
  331. /* read */
  332. as->pfc_cycles = ath5k_hw_reg_read(ah, AR5K_PROFCNT_CYCLE);
  333. as->pfc_busy = ath5k_hw_reg_read(ah, AR5K_PROFCNT_RXCLR);
  334. as->pfc_tx = ath5k_hw_reg_read(ah, AR5K_PROFCNT_TX);
  335. as->pfc_rx = ath5k_hw_reg_read(ah, AR5K_PROFCNT_RX);
  336. /* clear */
  337. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
  338. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
  339. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
  340. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
  341. /* un-freeze */
  342. ath5k_hw_reg_write(ah, 0, AR5K_MIBC);
  343. /* TODO: where does 44000 come from? (11g clock rate?) */
  344. listen = (as->pfc_cycles - as->pfc_rx - as->pfc_tx) / 44000;
  345. if (as->pfc_cycles == 0 || listen < 0)
  346. return 0;
  347. return listen;
  348. }
  349. /**
  350. * ath5k_ani_save_and_clear_phy_errors() - Clear and save PHY error counters
  351. *
  352. * Clear the PHY error counters as soon as possible, since this might be called
  353. * from a MIB interrupt and we want to make sure we don't get interrupted again.
  354. * Add the count of CCK and OFDM errors to our internal state, so it can be used
  355. * by the algorithm later.
  356. *
  357. * Will be called from interrupt and tasklet context.
  358. * Returns 0 if both counters are zero.
  359. */
  360. static int
  361. ath5k_ani_save_and_clear_phy_errors(struct ath5k_hw *ah,
  362. struct ath5k_ani_state *as)
  363. {
  364. unsigned int ofdm_err, cck_err;
  365. if (!ah->ah_capabilities.cap_has_phyerr_counters)
  366. return 0;
  367. ofdm_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1);
  368. cck_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2);
  369. /* reset counters first, we might be in a hurry (interrupt) */
  370. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
  371. AR5K_PHYERR_CNT1);
  372. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
  373. AR5K_PHYERR_CNT2);
  374. ofdm_err = ATH5K_ANI_OFDM_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - ofdm_err);
  375. cck_err = ATH5K_ANI_CCK_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - cck_err);
  376. /* sometimes both can be zero, especially when there is a superfluous
  377. * second interrupt. detect that here and return an error. */
  378. if (ofdm_err <= 0 && cck_err <= 0)
  379. return 0;
  380. /* avoid negative values should one of the registers overflow */
  381. if (ofdm_err > 0) {
  382. as->ofdm_errors += ofdm_err;
  383. as->sum_ofdm_errors += ofdm_err;
  384. }
  385. if (cck_err > 0) {
  386. as->cck_errors += cck_err;
  387. as->sum_cck_errors += cck_err;
  388. }
  389. return 1;
  390. }
  391. /**
  392. * ath5k_ani_period_restart() - Restart ANI period
  393. *
  394. * Just reset counters, so they are clear for the next "ani period".
  395. */
  396. static void
  397. ath5k_ani_period_restart(struct ath5k_hw *ah, struct ath5k_ani_state *as)
  398. {
  399. /* keep last values for debugging */
  400. as->last_ofdm_errors = as->ofdm_errors;
  401. as->last_cck_errors = as->cck_errors;
  402. as->last_listen = as->listen_time;
  403. as->ofdm_errors = 0;
  404. as->cck_errors = 0;
  405. as->listen_time = 0;
  406. }
  407. /**
  408. * ath5k_ani_calibration() - The main ANI calibration function
  409. *
  410. * We count OFDM and CCK errors relative to the time where we did not send or
  411. * receive ("listen" time) and raise or lower immunity accordingly.
  412. * This is called regularly (every second) from the calibration timer, but also
  413. * when an error threshold has been reached.
  414. *
  415. * In order to synchronize access from different contexts, this should be
  416. * called only indirectly by scheduling the ANI tasklet!
  417. */
  418. void
  419. ath5k_ani_calibration(struct ath5k_hw *ah)
  420. {
  421. struct ath5k_ani_state *as = &ah->ah_sc->ani_state;
  422. int listen, ofdm_high, ofdm_low, cck_high, cck_low;
  423. /* get listen time since last call and add it to the counter because we
  424. * might not have restarted the "ani period" last time.
  425. * always do this to calculate the busy time also in manual mode */
  426. listen = ath5k_hw_ani_get_listen_time(ah, as);
  427. as->listen_time += listen;
  428. if (as->ani_mode != ATH5K_ANI_MODE_AUTO)
  429. return;
  430. ath5k_ani_save_and_clear_phy_errors(ah, as);
  431. ofdm_high = as->listen_time * ATH5K_ANI_OFDM_TRIG_HIGH / 1000;
  432. cck_high = as->listen_time * ATH5K_ANI_CCK_TRIG_HIGH / 1000;
  433. ofdm_low = as->listen_time * ATH5K_ANI_OFDM_TRIG_LOW / 1000;
  434. cck_low = as->listen_time * ATH5K_ANI_CCK_TRIG_LOW / 1000;
  435. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  436. "listen %d (now %d)", as->listen_time, listen);
  437. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  438. "check high ofdm %d/%d cck %d/%d",
  439. as->ofdm_errors, ofdm_high, as->cck_errors, cck_high);
  440. if (as->ofdm_errors > ofdm_high || as->cck_errors > cck_high) {
  441. /* too many PHY errors - we have to raise immunity */
  442. bool ofdm_flag = as->ofdm_errors > ofdm_high ? true : false;
  443. ath5k_ani_raise_immunity(ah, as, ofdm_flag);
  444. ath5k_ani_period_restart(ah, as);
  445. } else if (as->listen_time > 5 * ATH5K_ANI_LISTEN_PERIOD) {
  446. /* If more than 5 (TODO: why 5?) periods have passed and we got
  447. * relatively little errors we can try to lower immunity */
  448. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  449. "check low ofdm %d/%d cck %d/%d",
  450. as->ofdm_errors, ofdm_low, as->cck_errors, cck_low);
  451. if (as->ofdm_errors <= ofdm_low && as->cck_errors <= cck_low)
  452. ath5k_ani_lower_immunity(ah, as);
  453. ath5k_ani_period_restart(ah, as);
  454. }
  455. }
  456. /*** INTERRUPT HANDLER ***/
  457. /**
  458. * ath5k_ani_mib_intr() - Interrupt handler for ANI MIB counters
  459. *
  460. * Just read & reset the registers quickly, so they don't generate more
  461. * interrupts, save the counters and schedule the tasklet to decide whether
  462. * to raise immunity or not.
  463. *
  464. * We just need to handle PHY error counters, ath5k_hw_update_mib_counters()
  465. * should take care of all "normal" MIB interrupts.
  466. */
  467. void
  468. ath5k_ani_mib_intr(struct ath5k_hw *ah)
  469. {
  470. struct ath5k_ani_state *as = &ah->ah_sc->ani_state;
  471. /* nothing to do here if HW does not have PHY error counters - they
  472. * can't be the reason for the MIB interrupt then */
  473. if (!ah->ah_capabilities.cap_has_phyerr_counters)
  474. return;
  475. /* not in use but clear anyways */
  476. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  477. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  478. if (ah->ah_sc->ani_state.ani_mode != ATH5K_ANI_MODE_AUTO)
  479. return;
  480. /* If one of the errors triggered, we can get a superfluous second
  481. * interrupt, even though we have already reset the register. The
  482. * function detects that so we can return early. */
  483. if (ath5k_ani_save_and_clear_phy_errors(ah, as) == 0)
  484. return;
  485. if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH ||
  486. as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
  487. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  488. }
  489. /**
  490. * ath5k_ani_phy_error_report() - Used by older HW to report PHY errors
  491. *
  492. * This is used by hardware without PHY error counters to report PHY errors
  493. * on a frame-by-frame basis, instead of the interrupt.
  494. */
  495. void
  496. ath5k_ani_phy_error_report(struct ath5k_hw *ah,
  497. enum ath5k_phy_error_code phyerr)
  498. {
  499. struct ath5k_ani_state *as = &ah->ah_sc->ani_state;
  500. if (phyerr == AR5K_RX_PHY_ERROR_OFDM_TIMING) {
  501. as->ofdm_errors++;
  502. if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH)
  503. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  504. } else if (phyerr == AR5K_RX_PHY_ERROR_CCK_TIMING) {
  505. as->cck_errors++;
  506. if (as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
  507. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  508. }
  509. }
  510. /*** INIT ***/
  511. /**
  512. * ath5k_enable_phy_err_counters() - Enable PHY error counters
  513. *
  514. * Enable PHY error counters for OFDM and CCK timing errors.
  515. */
  516. static void
  517. ath5k_enable_phy_err_counters(struct ath5k_hw *ah)
  518. {
  519. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
  520. AR5K_PHYERR_CNT1);
  521. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
  522. AR5K_PHYERR_CNT2);
  523. ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_OFDM, AR5K_PHYERR_CNT1_MASK);
  524. ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_CCK, AR5K_PHYERR_CNT2_MASK);
  525. /* not in use */
  526. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  527. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  528. }
  529. /**
  530. * ath5k_disable_phy_err_counters() - Disable PHY error counters
  531. *
  532. * Disable PHY error counters for OFDM and CCK timing errors.
  533. */
  534. static void
  535. ath5k_disable_phy_err_counters(struct ath5k_hw *ah)
  536. {
  537. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1);
  538. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2);
  539. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1_MASK);
  540. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2_MASK);
  541. /* not in use */
  542. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  543. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  544. }
  545. /**
  546. * ath5k_ani_init() - Initialize ANI
  547. * @mode: Which mode to use (auto, manual high, manual low, off)
  548. *
  549. * Initialize ANI according to mode.
  550. */
  551. void
  552. ath5k_ani_init(struct ath5k_hw *ah, enum ath5k_ani_mode mode)
  553. {
  554. /* ANI is only possible on 5212 and newer */
  555. if (ah->ah_version < AR5K_AR5212)
  556. return;
  557. /* clear old state information */
  558. memset(&ah->ah_sc->ani_state, 0, sizeof(ah->ah_sc->ani_state));
  559. /* older hardware has more spur levels than newer */
  560. if (ah->ah_mac_srev < AR5K_SREV_AR2414)
  561. ah->ah_sc->ani_state.max_spur_level = 7;
  562. else
  563. ah->ah_sc->ani_state.max_spur_level = 2;
  564. /* initial values for our ani parameters */
  565. if (mode == ATH5K_ANI_MODE_OFF) {
  566. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "ANI off\n");
  567. } else if (mode == ATH5K_ANI_MODE_MANUAL_LOW) {
  568. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  569. "ANI manual low -> high sensitivity\n");
  570. ath5k_ani_set_noise_immunity_level(ah, 0);
  571. ath5k_ani_set_spur_immunity_level(ah, 0);
  572. ath5k_ani_set_firstep_level(ah, 0);
  573. ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
  574. ath5k_ani_set_cck_weak_signal_detection(ah, true);
  575. } else if (mode == ATH5K_ANI_MODE_MANUAL_HIGH) {
  576. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI,
  577. "ANI manual high -> low sensitivity\n");
  578. ath5k_ani_set_noise_immunity_level(ah,
  579. ATH5K_ANI_MAX_NOISE_IMM_LVL);
  580. ath5k_ani_set_spur_immunity_level(ah,
  581. ah->ah_sc->ani_state.max_spur_level);
  582. ath5k_ani_set_firstep_level(ah, ATH5K_ANI_MAX_FIRSTEP_LVL);
  583. ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
  584. ath5k_ani_set_cck_weak_signal_detection(ah, false);
  585. } else if (mode == ATH5K_ANI_MODE_AUTO) {
  586. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_ANI, "ANI auto\n");
  587. ath5k_ani_set_noise_immunity_level(ah, 0);
  588. ath5k_ani_set_spur_immunity_level(ah, 0);
  589. ath5k_ani_set_firstep_level(ah, 0);
  590. ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
  591. ath5k_ani_set_cck_weak_signal_detection(ah, false);
  592. }
  593. /* newer hardware has PHY error counter registers which we can use to
  594. * get OFDM and CCK error counts. older hardware has to set rxfilter and
  595. * report every single PHY error by calling ath5k_ani_phy_error_report()
  596. */
  597. if (mode == ATH5K_ANI_MODE_AUTO) {
  598. if (ah->ah_capabilities.cap_has_phyerr_counters)
  599. ath5k_enable_phy_err_counters(ah);
  600. else
  601. ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) |
  602. AR5K_RX_FILTER_PHYERR);
  603. } else {
  604. if (ah->ah_capabilities.cap_has_phyerr_counters)
  605. ath5k_disable_phy_err_counters(ah);
  606. else
  607. ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) &
  608. ~AR5K_RX_FILTER_PHYERR);
  609. }
  610. ah->ah_sc->ani_state.ani_mode = mode;
  611. }
  612. /*** DEBUG ***/
  613. #ifdef CONFIG_ATH5K_DEBUG
  614. void
  615. ath5k_ani_print_counters(struct ath5k_hw *ah)
  616. {
  617. /* clears too */
  618. printk(KERN_NOTICE "ACK fail\t%d\n",
  619. ath5k_hw_reg_read(ah, AR5K_ACK_FAIL));
  620. printk(KERN_NOTICE "RTS fail\t%d\n",
  621. ath5k_hw_reg_read(ah, AR5K_RTS_FAIL));
  622. printk(KERN_NOTICE "RTS success\t%d\n",
  623. ath5k_hw_reg_read(ah, AR5K_RTS_OK));
  624. printk(KERN_NOTICE "FCS error\t%d\n",
  625. ath5k_hw_reg_read(ah, AR5K_FCS_FAIL));
  626. /* no clear */
  627. printk(KERN_NOTICE "tx\t%d\n",
  628. ath5k_hw_reg_read(ah, AR5K_PROFCNT_TX));
  629. printk(KERN_NOTICE "rx\t%d\n",
  630. ath5k_hw_reg_read(ah, AR5K_PROFCNT_RX));
  631. printk(KERN_NOTICE "busy\t%d\n",
  632. ath5k_hw_reg_read(ah, AR5K_PROFCNT_RXCLR));
  633. printk(KERN_NOTICE "cycles\t%d\n",
  634. ath5k_hw_reg_read(ah, AR5K_PROFCNT_CYCLE));
  635. printk(KERN_NOTICE "AR5K_PHYERR_CNT1\t%d\n",
  636. ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1));
  637. printk(KERN_NOTICE "AR5K_PHYERR_CNT2\t%d\n",
  638. ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2));
  639. printk(KERN_NOTICE "AR5K_OFDM_FIL_CNT\t%d\n",
  640. ath5k_hw_reg_read(ah, AR5K_OFDM_FIL_CNT));
  641. printk(KERN_NOTICE "AR5K_CCK_FIL_CNT\t%d\n",
  642. ath5k_hw_reg_read(ah, AR5K_CCK_FIL_CNT));
  643. }
  644. #endif