ani.c 22 KB

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