ani.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 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. static const s8 lo[] = { -52, -56, -60, -64, -70 };
  62. static const s8 hi[] = { -18, -18, -16, -14, -12 };
  63. static const s8 sz[] = { -34, -41, -48, -55, -62 };
  64. static const s8 fr[] = { -70, -72, -75, -78, -80 };
  65. #else
  66. static const s8 lo[] = { -64, -70 };
  67. static const s8 hi[] = { -14, -12 };
  68. static const s8 sz[] = { -55, -62 };
  69. static const s8 fr[] = { -78, -80 };
  70. #endif
  71. if (level < 0 || level >= ARRAY_SIZE(sz)) {
  72. ATH5K_ERR(ah, "noise immunity 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->ani_state.noise_imm_level = level;
  85. ATH5K_DBG_UNLIMIT(ah, 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. static const int val[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  97. if (level < 0 || level >= ARRAY_SIZE(val) ||
  98. level > ah->ani_state.max_spur_level) {
  99. ATH5K_ERR(ah, "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->ani_state.spur_level = level;
  106. ATH5K_DBG_UNLIMIT(ah, 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. static const int val[] = { 0, 4, 8 };
  117. if (level < 0 || level >= ARRAY_SIZE(val)) {
  118. ATH5K_ERR(ah, "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->ani_state.firstep_level = level;
  124. ATH5K_DBG_UNLIMIT(ah, 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. static const int m1l[] = { 127, 50 };
  136. static const int m2l[] = { 127, 40 };
  137. static const int m1[] = { 127, 0x4d };
  138. static const int m2[] = { 127, 0x40 };
  139. static const int m2cnt[] = { 31, 16 };
  140. static 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->ani_state.ofdm_weak_sig = on;
  160. ATH5K_DBG_UNLIMIT(ah, 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. static 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->ani_state.cck_weak_sig = on;
  175. ATH5K_DBG_UNLIMIT(ah, 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 = ewma_read(&ah->ah_beacon_rssi_avg);
  193. ATH5K_DBG_UNLIMIT(ah, 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->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->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, 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, 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, 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 = ewma_read(&ah->ah_beacon_rssi_avg);
  268. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "lower immunity");
  269. if (ah->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() - Update counters and return listening time
  316. *
  317. * Return an approximation of the time spent "listening" in milliseconds (ms)
  318. * since the last call of this function.
  319. * Save a snapshot of the counter values for debugging/statistics.
  320. */
  321. static int
  322. ath5k_hw_ani_get_listen_time(struct ath5k_hw *ah, struct ath5k_ani_state *as)
  323. {
  324. struct ath_common *common = ath5k_hw_common(ah);
  325. int listen;
  326. spin_lock_bh(&common->cc_lock);
  327. ath_hw_cycle_counters_update(common);
  328. memcpy(&as->last_cc, &common->cc_ani, sizeof(as->last_cc));
  329. /* clears common->cc_ani */
  330. listen = ath_hw_get_listen_time(common);
  331. spin_unlock_bh(&common->cc_lock);
  332. return listen;
  333. }
  334. /**
  335. * ath5k_ani_save_and_clear_phy_errors() - Clear and save PHY error counters
  336. *
  337. * Clear the PHY error counters as soon as possible, since this might be called
  338. * from a MIB interrupt and we want to make sure we don't get interrupted again.
  339. * Add the count of CCK and OFDM errors to our internal state, so it can be used
  340. * by the algorithm later.
  341. *
  342. * Will be called from interrupt and tasklet context.
  343. * Returns 0 if both counters are zero.
  344. */
  345. static int
  346. ath5k_ani_save_and_clear_phy_errors(struct ath5k_hw *ah,
  347. struct ath5k_ani_state *as)
  348. {
  349. unsigned int ofdm_err, cck_err;
  350. if (!ah->ah_capabilities.cap_has_phyerr_counters)
  351. return 0;
  352. ofdm_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1);
  353. cck_err = ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2);
  354. /* reset counters first, we might be in a hurry (interrupt) */
  355. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
  356. AR5K_PHYERR_CNT1);
  357. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
  358. AR5K_PHYERR_CNT2);
  359. ofdm_err = ATH5K_ANI_OFDM_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - ofdm_err);
  360. cck_err = ATH5K_ANI_CCK_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX - cck_err);
  361. /* sometimes both can be zero, especially when there is a superfluous
  362. * second interrupt. detect that here and return an error. */
  363. if (ofdm_err <= 0 && cck_err <= 0)
  364. return 0;
  365. /* avoid negative values should one of the registers overflow */
  366. if (ofdm_err > 0) {
  367. as->ofdm_errors += ofdm_err;
  368. as->sum_ofdm_errors += ofdm_err;
  369. }
  370. if (cck_err > 0) {
  371. as->cck_errors += cck_err;
  372. as->sum_cck_errors += cck_err;
  373. }
  374. return 1;
  375. }
  376. /**
  377. * ath5k_ani_period_restart() - Restart ANI period
  378. *
  379. * Just reset counters, so they are clear for the next "ani period".
  380. */
  381. static void
  382. ath5k_ani_period_restart(struct ath5k_hw *ah, struct ath5k_ani_state *as)
  383. {
  384. /* keep last values for debugging */
  385. as->last_ofdm_errors = as->ofdm_errors;
  386. as->last_cck_errors = as->cck_errors;
  387. as->last_listen = as->listen_time;
  388. as->ofdm_errors = 0;
  389. as->cck_errors = 0;
  390. as->listen_time = 0;
  391. }
  392. /**
  393. * ath5k_ani_calibration() - The main ANI calibration function
  394. *
  395. * We count OFDM and CCK errors relative to the time where we did not send or
  396. * receive ("listen" time) and raise or lower immunity accordingly.
  397. * This is called regularly (every second) from the calibration timer, but also
  398. * when an error threshold has been reached.
  399. *
  400. * In order to synchronize access from different contexts, this should be
  401. * called only indirectly by scheduling the ANI tasklet!
  402. */
  403. void
  404. ath5k_ani_calibration(struct ath5k_hw *ah)
  405. {
  406. struct ath5k_ani_state *as = &ah->ani_state;
  407. int listen, ofdm_high, ofdm_low, cck_high, cck_low;
  408. /* get listen time since last call and add it to the counter because we
  409. * might not have restarted the "ani period" last time.
  410. * always do this to calculate the busy time also in manual mode */
  411. listen = ath5k_hw_ani_get_listen_time(ah, as);
  412. as->listen_time += listen;
  413. if (as->ani_mode != ATH5K_ANI_MODE_AUTO)
  414. return;
  415. ath5k_ani_save_and_clear_phy_errors(ah, as);
  416. ofdm_high = as->listen_time * ATH5K_ANI_OFDM_TRIG_HIGH / 1000;
  417. cck_high = as->listen_time * ATH5K_ANI_CCK_TRIG_HIGH / 1000;
  418. ofdm_low = as->listen_time * ATH5K_ANI_OFDM_TRIG_LOW / 1000;
  419. cck_low = as->listen_time * ATH5K_ANI_CCK_TRIG_LOW / 1000;
  420. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
  421. "listen %d (now %d)", as->listen_time, listen);
  422. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
  423. "check high ofdm %d/%d cck %d/%d",
  424. as->ofdm_errors, ofdm_high, as->cck_errors, cck_high);
  425. if (as->ofdm_errors > ofdm_high || as->cck_errors > cck_high) {
  426. /* too many PHY errors - we have to raise immunity */
  427. bool ofdm_flag = as->ofdm_errors > ofdm_high ? true : false;
  428. ath5k_ani_raise_immunity(ah, as, ofdm_flag);
  429. ath5k_ani_period_restart(ah, as);
  430. } else if (as->listen_time > 5 * ATH5K_ANI_LISTEN_PERIOD) {
  431. /* If more than 5 (TODO: why 5?) periods have passed and we got
  432. * relatively little errors we can try to lower immunity */
  433. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
  434. "check low ofdm %d/%d cck %d/%d",
  435. as->ofdm_errors, ofdm_low, as->cck_errors, cck_low);
  436. if (as->ofdm_errors <= ofdm_low && as->cck_errors <= cck_low)
  437. ath5k_ani_lower_immunity(ah, as);
  438. ath5k_ani_period_restart(ah, as);
  439. }
  440. }
  441. /*** INTERRUPT HANDLER ***/
  442. /**
  443. * ath5k_ani_mib_intr() - Interrupt handler for ANI MIB counters
  444. *
  445. * Just read & reset the registers quickly, so they don't generate more
  446. * interrupts, save the counters and schedule the tasklet to decide whether
  447. * to raise immunity or not.
  448. *
  449. * We just need to handle PHY error counters, ath5k_hw_update_mib_counters()
  450. * should take care of all "normal" MIB interrupts.
  451. */
  452. void
  453. ath5k_ani_mib_intr(struct ath5k_hw *ah)
  454. {
  455. struct ath5k_ani_state *as = &ah->ani_state;
  456. /* nothing to do here if HW does not have PHY error counters - they
  457. * can't be the reason for the MIB interrupt then */
  458. if (!ah->ah_capabilities.cap_has_phyerr_counters)
  459. return;
  460. /* not in use but clear anyways */
  461. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  462. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  463. if (ah->ani_state.ani_mode != ATH5K_ANI_MODE_AUTO)
  464. return;
  465. /* If one of the errors triggered, we can get a superfluous second
  466. * interrupt, even though we have already reset the register. The
  467. * function detects that so we can return early. */
  468. if (ath5k_ani_save_and_clear_phy_errors(ah, as) == 0)
  469. return;
  470. if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH ||
  471. as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
  472. tasklet_schedule(&ah->ani_tasklet);
  473. }
  474. /**
  475. * ath5k_ani_phy_error_report() - Used by older HW to report PHY errors
  476. *
  477. * This is used by hardware without PHY error counters to report PHY errors
  478. * on a frame-by-frame basis, instead of the interrupt.
  479. */
  480. void
  481. ath5k_ani_phy_error_report(struct ath5k_hw *ah,
  482. enum ath5k_phy_error_code phyerr)
  483. {
  484. struct ath5k_ani_state *as = &ah->ani_state;
  485. if (phyerr == AR5K_RX_PHY_ERROR_OFDM_TIMING) {
  486. as->ofdm_errors++;
  487. if (as->ofdm_errors > ATH5K_ANI_OFDM_TRIG_HIGH)
  488. tasklet_schedule(&ah->ani_tasklet);
  489. } else if (phyerr == AR5K_RX_PHY_ERROR_CCK_TIMING) {
  490. as->cck_errors++;
  491. if (as->cck_errors > ATH5K_ANI_CCK_TRIG_HIGH)
  492. tasklet_schedule(&ah->ani_tasklet);
  493. }
  494. }
  495. /*** INIT ***/
  496. /**
  497. * ath5k_enable_phy_err_counters() - Enable PHY error counters
  498. *
  499. * Enable PHY error counters for OFDM and CCK timing errors.
  500. */
  501. static void
  502. ath5k_enable_phy_err_counters(struct ath5k_hw *ah)
  503. {
  504. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_OFDM_TRIG_HIGH,
  505. AR5K_PHYERR_CNT1);
  506. ath5k_hw_reg_write(ah, ATH5K_PHYERR_CNT_MAX - ATH5K_ANI_CCK_TRIG_HIGH,
  507. AR5K_PHYERR_CNT2);
  508. ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_OFDM, AR5K_PHYERR_CNT1_MASK);
  509. ath5k_hw_reg_write(ah, AR5K_PHY_ERR_FIL_CCK, AR5K_PHYERR_CNT2_MASK);
  510. /* not in use */
  511. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  512. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  513. }
  514. /**
  515. * ath5k_disable_phy_err_counters() - Disable PHY error counters
  516. *
  517. * Disable PHY error counters for OFDM and CCK timing errors.
  518. */
  519. static void
  520. ath5k_disable_phy_err_counters(struct ath5k_hw *ah)
  521. {
  522. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1);
  523. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2);
  524. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT1_MASK);
  525. ath5k_hw_reg_write(ah, 0, AR5K_PHYERR_CNT2_MASK);
  526. /* not in use */
  527. ath5k_hw_reg_write(ah, 0, AR5K_OFDM_FIL_CNT);
  528. ath5k_hw_reg_write(ah, 0, AR5K_CCK_FIL_CNT);
  529. }
  530. /**
  531. * ath5k_ani_init() - Initialize ANI
  532. * @mode: Which mode to use (auto, manual high, manual low, off)
  533. *
  534. * Initialize ANI according to mode.
  535. */
  536. void
  537. ath5k_ani_init(struct ath5k_hw *ah, enum ath5k_ani_mode mode)
  538. {
  539. /* ANI is only possible on 5212 and newer */
  540. if (ah->ah_version < AR5K_AR5212)
  541. return;
  542. if (mode < ATH5K_ANI_MODE_OFF || mode > ATH5K_ANI_MODE_AUTO) {
  543. ATH5K_ERR(ah, "ANI mode %d out of range", mode);
  544. return;
  545. }
  546. /* clear old state information */
  547. memset(&ah->ani_state, 0, sizeof(ah->ani_state));
  548. /* older hardware has more spur levels than newer */
  549. if (ah->ah_mac_srev < AR5K_SREV_AR2414)
  550. ah->ani_state.max_spur_level = 7;
  551. else
  552. ah->ani_state.max_spur_level = 2;
  553. /* initial values for our ani parameters */
  554. if (mode == ATH5K_ANI_MODE_OFF) {
  555. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "ANI off\n");
  556. } else if (mode == ATH5K_ANI_MODE_MANUAL_LOW) {
  557. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
  558. "ANI manual low -> high sensitivity\n");
  559. ath5k_ani_set_noise_immunity_level(ah, 0);
  560. ath5k_ani_set_spur_immunity_level(ah, 0);
  561. ath5k_ani_set_firstep_level(ah, 0);
  562. ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
  563. ath5k_ani_set_cck_weak_signal_detection(ah, true);
  564. } else if (mode == ATH5K_ANI_MODE_MANUAL_HIGH) {
  565. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI,
  566. "ANI manual high -> low sensitivity\n");
  567. ath5k_ani_set_noise_immunity_level(ah,
  568. ATH5K_ANI_MAX_NOISE_IMM_LVL);
  569. ath5k_ani_set_spur_immunity_level(ah,
  570. ah->ani_state.max_spur_level);
  571. ath5k_ani_set_firstep_level(ah, ATH5K_ANI_MAX_FIRSTEP_LVL);
  572. ath5k_ani_set_ofdm_weak_signal_detection(ah, false);
  573. ath5k_ani_set_cck_weak_signal_detection(ah, false);
  574. } else if (mode == ATH5K_ANI_MODE_AUTO) {
  575. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_ANI, "ANI auto\n");
  576. ath5k_ani_set_noise_immunity_level(ah, 0);
  577. ath5k_ani_set_spur_immunity_level(ah, 0);
  578. ath5k_ani_set_firstep_level(ah, 0);
  579. ath5k_ani_set_ofdm_weak_signal_detection(ah, true);
  580. ath5k_ani_set_cck_weak_signal_detection(ah, false);
  581. }
  582. /* newer hardware has PHY error counter registers which we can use to
  583. * get OFDM and CCK error counts. older hardware has to set rxfilter and
  584. * report every single PHY error by calling ath5k_ani_phy_error_report()
  585. */
  586. if (mode == ATH5K_ANI_MODE_AUTO) {
  587. if (ah->ah_capabilities.cap_has_phyerr_counters)
  588. ath5k_enable_phy_err_counters(ah);
  589. else
  590. ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) |
  591. AR5K_RX_FILTER_PHYERR);
  592. } else {
  593. if (ah->ah_capabilities.cap_has_phyerr_counters)
  594. ath5k_disable_phy_err_counters(ah);
  595. else
  596. ath5k_hw_set_rx_filter(ah, ath5k_hw_get_rx_filter(ah) &
  597. ~AR5K_RX_FILTER_PHYERR);
  598. }
  599. ah->ani_state.ani_mode = mode;
  600. }
  601. /*** DEBUG ***/
  602. #ifdef CONFIG_ATH5K_DEBUG
  603. void
  604. ath5k_ani_print_counters(struct ath5k_hw *ah)
  605. {
  606. /* clears too */
  607. printk(KERN_NOTICE "ACK fail\t%d\n",
  608. ath5k_hw_reg_read(ah, AR5K_ACK_FAIL));
  609. printk(KERN_NOTICE "RTS fail\t%d\n",
  610. ath5k_hw_reg_read(ah, AR5K_RTS_FAIL));
  611. printk(KERN_NOTICE "RTS success\t%d\n",
  612. ath5k_hw_reg_read(ah, AR5K_RTS_OK));
  613. printk(KERN_NOTICE "FCS error\t%d\n",
  614. ath5k_hw_reg_read(ah, AR5K_FCS_FAIL));
  615. /* no clear */
  616. printk(KERN_NOTICE "tx\t%d\n",
  617. ath5k_hw_reg_read(ah, AR5K_PROFCNT_TX));
  618. printk(KERN_NOTICE "rx\t%d\n",
  619. ath5k_hw_reg_read(ah, AR5K_PROFCNT_RX));
  620. printk(KERN_NOTICE "busy\t%d\n",
  621. ath5k_hw_reg_read(ah, AR5K_PROFCNT_RXCLR));
  622. printk(KERN_NOTICE "cycles\t%d\n",
  623. ath5k_hw_reg_read(ah, AR5K_PROFCNT_CYCLE));
  624. printk(KERN_NOTICE "AR5K_PHYERR_CNT1\t%d\n",
  625. ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT1));
  626. printk(KERN_NOTICE "AR5K_PHYERR_CNT2\t%d\n",
  627. ath5k_hw_reg_read(ah, AR5K_PHYERR_CNT2));
  628. printk(KERN_NOTICE "AR5K_OFDM_FIL_CNT\t%d\n",
  629. ath5k_hw_reg_read(ah, AR5K_OFDM_FIL_CNT));
  630. printk(KERN_NOTICE "AR5K_CCK_FIL_CNT\t%d\n",
  631. ath5k_hw_reg_read(ah, AR5K_CCK_FIL_CNT));
  632. }
  633. #endif