ani.c 23 KB

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