ani.c 23 KB

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