iwl-calib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2008 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Tomas Winkler <tomas.winkler@intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *****************************************************************************/
  62. #include <linux/kernel.h>
  63. #include <net/mac80211.h>
  64. #include "iwl-dev.h"
  65. #include "iwl-core.h"
  66. #include "iwl-calib.h"
  67. #include "iwl-eeprom.h"
  68. /* "false alarms" are signals that our DSP tries to lock onto,
  69. * but then determines that they are either noise, or transmissions
  70. * from a distant wireless network (also "noise", really) that get
  71. * "stepped on" by stronger transmissions within our own network.
  72. * This algorithm attempts to set a sensitivity level that is high
  73. * enough to receive all of our own network traffic, but not so
  74. * high that our DSP gets too busy trying to lock onto non-network
  75. * activity/noise. */
  76. static int iwl_sens_energy_cck(struct iwl_priv *priv,
  77. u32 norm_fa,
  78. u32 rx_enable_time,
  79. struct statistics_general_data *rx_info)
  80. {
  81. u32 max_nrg_cck = 0;
  82. int i = 0;
  83. u8 max_silence_rssi = 0;
  84. u32 silence_ref = 0;
  85. u8 silence_rssi_a = 0;
  86. u8 silence_rssi_b = 0;
  87. u8 silence_rssi_c = 0;
  88. u32 val;
  89. /* "false_alarms" values below are cross-multiplications to assess the
  90. * numbers of false alarms within the measured period of actual Rx
  91. * (Rx is off when we're txing), vs the min/max expected false alarms
  92. * (some should be expected if rx is sensitive enough) in a
  93. * hypothetical listening period of 200 time units (TU), 204.8 msec:
  94. *
  95. * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
  96. *
  97. * */
  98. u32 false_alarms = norm_fa * 200 * 1024;
  99. u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
  100. u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
  101. struct iwl_sensitivity_data *data = NULL;
  102. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  103. data = &(priv->sensitivity_data);
  104. data->nrg_auto_corr_silence_diff = 0;
  105. /* Find max silence rssi among all 3 receivers.
  106. * This is background noise, which may include transmissions from other
  107. * networks, measured during silence before our network's beacon */
  108. silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
  109. ALL_BAND_FILTER) >> 8);
  110. silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
  111. ALL_BAND_FILTER) >> 8);
  112. silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
  113. ALL_BAND_FILTER) >> 8);
  114. val = max(silence_rssi_b, silence_rssi_c);
  115. max_silence_rssi = max(silence_rssi_a, (u8) val);
  116. /* Store silence rssi in 20-beacon history table */
  117. data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
  118. data->nrg_silence_idx++;
  119. if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
  120. data->nrg_silence_idx = 0;
  121. /* Find max silence rssi across 20 beacon history */
  122. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
  123. val = data->nrg_silence_rssi[i];
  124. silence_ref = max(silence_ref, val);
  125. }
  126. IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
  127. silence_rssi_a, silence_rssi_b, silence_rssi_c,
  128. silence_ref);
  129. /* Find max rx energy (min value!) among all 3 receivers,
  130. * measured during beacon frame.
  131. * Save it in 10-beacon history table. */
  132. i = data->nrg_energy_idx;
  133. val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
  134. data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
  135. data->nrg_energy_idx++;
  136. if (data->nrg_energy_idx >= 10)
  137. data->nrg_energy_idx = 0;
  138. /* Find min rx energy (max value) across 10 beacon history.
  139. * This is the minimum signal level that we want to receive well.
  140. * Add backoff (margin so we don't miss slightly lower energy frames).
  141. * This establishes an upper bound (min value) for energy threshold. */
  142. max_nrg_cck = data->nrg_value[0];
  143. for (i = 1; i < 10; i++)
  144. max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
  145. max_nrg_cck += 6;
  146. IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
  147. rx_info->beacon_energy_a, rx_info->beacon_energy_b,
  148. rx_info->beacon_energy_c, max_nrg_cck - 6);
  149. /* Count number of consecutive beacons with fewer-than-desired
  150. * false alarms. */
  151. if (false_alarms < min_false_alarms)
  152. data->num_in_cck_no_fa++;
  153. else
  154. data->num_in_cck_no_fa = 0;
  155. IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
  156. data->num_in_cck_no_fa);
  157. /* If we got too many false alarms this time, reduce sensitivity */
  158. if ((false_alarms > max_false_alarms) &&
  159. (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
  160. IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
  161. false_alarms, max_false_alarms);
  162. IWL_DEBUG_CALIB("... reducing sensitivity\n");
  163. data->nrg_curr_state = IWL_FA_TOO_MANY;
  164. /* Store for "fewer than desired" on later beacon */
  165. data->nrg_silence_ref = silence_ref;
  166. /* increase energy threshold (reduce nrg value)
  167. * to decrease sensitivity */
  168. if (data->nrg_th_cck >
  169. (ranges->max_nrg_cck + NRG_STEP_CCK))
  170. data->nrg_th_cck = data->nrg_th_cck
  171. - NRG_STEP_CCK;
  172. else
  173. data->nrg_th_cck = ranges->max_nrg_cck;
  174. /* Else if we got fewer than desired, increase sensitivity */
  175. } else if (false_alarms < min_false_alarms) {
  176. data->nrg_curr_state = IWL_FA_TOO_FEW;
  177. /* Compare silence level with silence level for most recent
  178. * healthy number or too many false alarms */
  179. data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
  180. (s32)silence_ref;
  181. IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
  182. false_alarms, min_false_alarms,
  183. data->nrg_auto_corr_silence_diff);
  184. /* Increase value to increase sensitivity, but only if:
  185. * 1a) previous beacon did *not* have *too many* false alarms
  186. * 1b) AND there's a significant difference in Rx levels
  187. * from a previous beacon with too many, or healthy # FAs
  188. * OR 2) We've seen a lot of beacons (100) with too few
  189. * false alarms */
  190. if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
  191. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  192. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  193. IWL_DEBUG_CALIB("... increasing sensitivity\n");
  194. /* Increase nrg value to increase sensitivity */
  195. val = data->nrg_th_cck + NRG_STEP_CCK;
  196. data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
  197. } else {
  198. IWL_DEBUG_CALIB("... but not changing sensitivity\n");
  199. }
  200. /* Else we got a healthy number of false alarms, keep status quo */
  201. } else {
  202. IWL_DEBUG_CALIB(" FA in safe zone\n");
  203. data->nrg_curr_state = IWL_FA_GOOD_RANGE;
  204. /* Store for use in "fewer than desired" with later beacon */
  205. data->nrg_silence_ref = silence_ref;
  206. /* If previous beacon had too many false alarms,
  207. * give it some extra margin by reducing sensitivity again
  208. * (but don't go below measured energy of desired Rx) */
  209. if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
  210. IWL_DEBUG_CALIB("... increasing margin\n");
  211. if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
  212. data->nrg_th_cck -= NRG_MARGIN;
  213. else
  214. data->nrg_th_cck = max_nrg_cck;
  215. }
  216. }
  217. /* Make sure the energy threshold does not go above the measured
  218. * energy of the desired Rx signals (reduced by backoff margin),
  219. * or else we might start missing Rx frames.
  220. * Lower value is higher energy, so we use max()!
  221. */
  222. data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
  223. IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
  224. data->nrg_prev_state = data->nrg_curr_state;
  225. /* Auto-correlation CCK algorithm */
  226. if (false_alarms > min_false_alarms) {
  227. /* increase auto_corr values to decrease sensitivity
  228. * so the DSP won't be disturbed by the noise
  229. */
  230. if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
  231. data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
  232. else {
  233. val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
  234. data->auto_corr_cck =
  235. min((u32)ranges->auto_corr_max_cck, val);
  236. }
  237. val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
  238. data->auto_corr_cck_mrc =
  239. min((u32)ranges->auto_corr_max_cck_mrc, val);
  240. } else if ((false_alarms < min_false_alarms) &&
  241. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  242. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  243. /* Decrease auto_corr values to increase sensitivity */
  244. val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
  245. data->auto_corr_cck =
  246. max((u32)ranges->auto_corr_min_cck, val);
  247. val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
  248. data->auto_corr_cck_mrc =
  249. max((u32)ranges->auto_corr_min_cck_mrc, val);
  250. }
  251. return 0;
  252. }
  253. static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
  254. u32 norm_fa,
  255. u32 rx_enable_time)
  256. {
  257. u32 val;
  258. u32 false_alarms = norm_fa * 200 * 1024;
  259. u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
  260. u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
  261. struct iwl_sensitivity_data *data = NULL;
  262. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  263. data = &(priv->sensitivity_data);
  264. /* If we got too many false alarms this time, reduce sensitivity */
  265. if (false_alarms > max_false_alarms) {
  266. IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
  267. false_alarms, max_false_alarms);
  268. val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
  269. data->auto_corr_ofdm =
  270. min((u32)ranges->auto_corr_max_ofdm, val);
  271. val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
  272. data->auto_corr_ofdm_mrc =
  273. min((u32)ranges->auto_corr_max_ofdm_mrc, val);
  274. val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
  275. data->auto_corr_ofdm_x1 =
  276. min((u32)ranges->auto_corr_max_ofdm_x1, val);
  277. val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
  278. data->auto_corr_ofdm_mrc_x1 =
  279. min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
  280. }
  281. /* Else if we got fewer than desired, increase sensitivity */
  282. else if (false_alarms < min_false_alarms) {
  283. IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
  284. false_alarms, min_false_alarms);
  285. val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
  286. data->auto_corr_ofdm =
  287. max((u32)ranges->auto_corr_min_ofdm, val);
  288. val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
  289. data->auto_corr_ofdm_mrc =
  290. max((u32)ranges->auto_corr_min_ofdm_mrc, val);
  291. val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
  292. data->auto_corr_ofdm_x1 =
  293. max((u32)ranges->auto_corr_min_ofdm_x1, val);
  294. val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
  295. data->auto_corr_ofdm_mrc_x1 =
  296. max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
  297. } else {
  298. IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
  299. min_false_alarms, false_alarms, max_false_alarms);
  300. }
  301. return 0;
  302. }
  303. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  304. static int iwl_sensitivity_write(struct iwl_priv *priv)
  305. {
  306. int ret = 0;
  307. struct iwl_sensitivity_cmd cmd ;
  308. struct iwl_sensitivity_data *data = NULL;
  309. struct iwl_host_cmd cmd_out = {
  310. .id = SENSITIVITY_CMD,
  311. .len = sizeof(struct iwl_sensitivity_cmd),
  312. .meta.flags = CMD_ASYNC,
  313. .data = &cmd,
  314. };
  315. data = &(priv->sensitivity_data);
  316. memset(&cmd, 0, sizeof(cmd));
  317. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  318. cpu_to_le16((u16)data->auto_corr_ofdm);
  319. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  320. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  321. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  322. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  323. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  324. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  325. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  326. cpu_to_le16((u16)data->auto_corr_cck);
  327. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  328. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  329. cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
  330. cpu_to_le16((u16)data->nrg_th_cck);
  331. cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  332. cpu_to_le16((u16)data->nrg_th_ofdm);
  333. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  334. __constant_cpu_to_le16(190);
  335. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  336. __constant_cpu_to_le16(390);
  337. cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
  338. __constant_cpu_to_le16(62);
  339. IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  340. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  341. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  342. data->nrg_th_ofdm);
  343. IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
  344. data->auto_corr_cck, data->auto_corr_cck_mrc,
  345. data->nrg_th_cck);
  346. /* Update uCode's "work" table, and copy it to DSP */
  347. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  348. /* Don't send command to uCode if nothing has changed */
  349. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  350. sizeof(u16)*HD_TABLE_SIZE)) {
  351. IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
  352. return 0;
  353. }
  354. /* Copy table for comparison next time */
  355. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  356. sizeof(u16)*HD_TABLE_SIZE);
  357. ret = iwl_send_cmd(priv, &cmd_out);
  358. if (ret)
  359. IWL_ERROR("SENSITIVITY_CMD failed\n");
  360. return ret;
  361. }
  362. void iwl_init_sensitivity(struct iwl_priv *priv)
  363. {
  364. int ret = 0;
  365. int i;
  366. struct iwl_sensitivity_data *data = NULL;
  367. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  368. if (priv->disable_sens_cal)
  369. return;
  370. IWL_DEBUG_CALIB("Start iwl_init_sensitivity\n");
  371. /* Clear driver's sensitivity algo data */
  372. data = &(priv->sensitivity_data);
  373. if (ranges == NULL)
  374. /* can happen if IWLWIFI_RUN_TIME_CALIB is selected
  375. * but no IWLXXXX_RUN_TIME_CALIB for specific is selected */
  376. return;
  377. memset(data, 0, sizeof(struct iwl_sensitivity_data));
  378. data->num_in_cck_no_fa = 0;
  379. data->nrg_curr_state = IWL_FA_TOO_MANY;
  380. data->nrg_prev_state = IWL_FA_TOO_MANY;
  381. data->nrg_silence_ref = 0;
  382. data->nrg_silence_idx = 0;
  383. data->nrg_energy_idx = 0;
  384. for (i = 0; i < 10; i++)
  385. data->nrg_value[i] = 0;
  386. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  387. data->nrg_silence_rssi[i] = 0;
  388. data->auto_corr_ofdm = 90;
  389. data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
  390. data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
  391. data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
  392. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  393. data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
  394. data->nrg_th_cck = ranges->nrg_th_cck;
  395. data->nrg_th_ofdm = ranges->nrg_th_ofdm;
  396. data->last_bad_plcp_cnt_ofdm = 0;
  397. data->last_fa_cnt_ofdm = 0;
  398. data->last_bad_plcp_cnt_cck = 0;
  399. data->last_fa_cnt_cck = 0;
  400. ret |= iwl_sensitivity_write(priv);
  401. IWL_DEBUG_CALIB("<<return 0x%X\n", ret);
  402. }
  403. EXPORT_SYMBOL(iwl_init_sensitivity);
  404. void iwl_sensitivity_calibration(struct iwl_priv *priv,
  405. struct iwl4965_notif_statistics *resp)
  406. {
  407. u32 rx_enable_time;
  408. u32 fa_cck;
  409. u32 fa_ofdm;
  410. u32 bad_plcp_cck;
  411. u32 bad_plcp_ofdm;
  412. u32 norm_fa_ofdm;
  413. u32 norm_fa_cck;
  414. struct iwl_sensitivity_data *data = NULL;
  415. struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
  416. struct statistics_rx *statistics = &(resp->rx);
  417. unsigned long flags;
  418. struct statistics_general_data statis;
  419. if (priv->disable_sens_cal)
  420. return;
  421. data = &(priv->sensitivity_data);
  422. if (!iwl_is_associated(priv)) {
  423. IWL_DEBUG_CALIB("<< - not associated\n");
  424. return;
  425. }
  426. spin_lock_irqsave(&priv->lock, flags);
  427. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  428. IWL_DEBUG_CALIB("<< invalid data.\n");
  429. spin_unlock_irqrestore(&priv->lock, flags);
  430. return;
  431. }
  432. /* Extract Statistics: */
  433. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  434. fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
  435. fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
  436. bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
  437. bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
  438. statis.beacon_silence_rssi_a =
  439. le32_to_cpu(statistics->general.beacon_silence_rssi_a);
  440. statis.beacon_silence_rssi_b =
  441. le32_to_cpu(statistics->general.beacon_silence_rssi_b);
  442. statis.beacon_silence_rssi_c =
  443. le32_to_cpu(statistics->general.beacon_silence_rssi_c);
  444. statis.beacon_energy_a =
  445. le32_to_cpu(statistics->general.beacon_energy_a);
  446. statis.beacon_energy_b =
  447. le32_to_cpu(statistics->general.beacon_energy_b);
  448. statis.beacon_energy_c =
  449. le32_to_cpu(statistics->general.beacon_energy_c);
  450. spin_unlock_irqrestore(&priv->lock, flags);
  451. IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
  452. if (!rx_enable_time) {
  453. IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
  454. return;
  455. }
  456. /* These statistics increase monotonically, and do not reset
  457. * at each beacon. Calculate difference from last value, or just
  458. * use the new statistics value if it has reset or wrapped around. */
  459. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  460. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  461. else {
  462. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  463. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  464. }
  465. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  466. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  467. else {
  468. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  469. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  470. }
  471. if (data->last_fa_cnt_ofdm > fa_ofdm)
  472. data->last_fa_cnt_ofdm = fa_ofdm;
  473. else {
  474. fa_ofdm -= data->last_fa_cnt_ofdm;
  475. data->last_fa_cnt_ofdm += fa_ofdm;
  476. }
  477. if (data->last_fa_cnt_cck > fa_cck)
  478. data->last_fa_cnt_cck = fa_cck;
  479. else {
  480. fa_cck -= data->last_fa_cnt_cck;
  481. data->last_fa_cnt_cck += fa_cck;
  482. }
  483. /* Total aborted signal locks */
  484. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  485. norm_fa_cck = fa_cck + bad_plcp_cck;
  486. IWL_DEBUG_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  487. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  488. iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  489. iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  490. iwl_sensitivity_write(priv);
  491. return;
  492. }
  493. EXPORT_SYMBOL(iwl_sensitivity_calibration);
  494. /*
  495. * Accumulate 20 beacons of signal and noise statistics for each of
  496. * 3 receivers/antennas/rx-chains, then figure out:
  497. * 1) Which antennas are connected.
  498. * 2) Differential rx gain settings to balance the 3 receivers.
  499. */
  500. void iwl_chain_noise_calibration(struct iwl_priv *priv,
  501. struct iwl4965_notif_statistics *stat_resp)
  502. {
  503. struct iwl_chain_noise_data *data = NULL;
  504. u32 chain_noise_a;
  505. u32 chain_noise_b;
  506. u32 chain_noise_c;
  507. u32 chain_sig_a;
  508. u32 chain_sig_b;
  509. u32 chain_sig_c;
  510. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  511. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  512. u32 max_average_sig;
  513. u16 max_average_sig_antenna_i;
  514. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  515. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  516. u16 i = 0;
  517. u16 rxon_chnum = INITIALIZATION_VALUE;
  518. u16 stat_chnum = INITIALIZATION_VALUE;
  519. u8 rxon_band24;
  520. u8 stat_band24;
  521. u32 active_chains = 0;
  522. u8 num_tx_chains;
  523. unsigned long flags;
  524. struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
  525. if (priv->disable_chain_noise_cal)
  526. return;
  527. data = &(priv->chain_noise_data);
  528. /* Accumulate just the first 20 beacons after the first association,
  529. * then we're done forever. */
  530. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  531. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  532. IWL_DEBUG_CALIB("Wait for noise calib reset\n");
  533. return;
  534. }
  535. spin_lock_irqsave(&priv->lock, flags);
  536. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  537. IWL_DEBUG_CALIB(" << Interference data unavailable\n");
  538. spin_unlock_irqrestore(&priv->lock, flags);
  539. return;
  540. }
  541. rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
  542. rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
  543. stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
  544. stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
  545. /* Make sure we accumulate data for just the associated channel
  546. * (even if scanning). */
  547. if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
  548. IWL_DEBUG_CALIB("Stats not from chan=%d, band24=%d\n",
  549. rxon_chnum, rxon_band24);
  550. spin_unlock_irqrestore(&priv->lock, flags);
  551. return;
  552. }
  553. /* Accumulate beacon statistics values across 20 beacons */
  554. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  555. IN_BAND_FILTER;
  556. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  557. IN_BAND_FILTER;
  558. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  559. IN_BAND_FILTER;
  560. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  561. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  562. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  563. spin_unlock_irqrestore(&priv->lock, flags);
  564. data->beacon_count++;
  565. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  566. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  567. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  568. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  569. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  570. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  571. IWL_DEBUG_CALIB("chan=%d, band24=%d, beacon=%d\n",
  572. rxon_chnum, rxon_band24, data->beacon_count);
  573. IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
  574. chain_sig_a, chain_sig_b, chain_sig_c);
  575. IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
  576. chain_noise_a, chain_noise_b, chain_noise_c);
  577. /* If this is the 20th beacon, determine:
  578. * 1) Disconnected antennas (using signal strengths)
  579. * 2) Differential gain (using silence noise) to balance receivers */
  580. if (data->beacon_count != CAL_NUM_OF_BEACONS)
  581. return;
  582. /* Analyze signal for disconnected antenna */
  583. average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
  584. average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
  585. average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
  586. if (average_sig[0] >= average_sig[1]) {
  587. max_average_sig = average_sig[0];
  588. max_average_sig_antenna_i = 0;
  589. active_chains = (1 << max_average_sig_antenna_i);
  590. } else {
  591. max_average_sig = average_sig[1];
  592. max_average_sig_antenna_i = 1;
  593. active_chains = (1 << max_average_sig_antenna_i);
  594. }
  595. if (average_sig[2] >= max_average_sig) {
  596. max_average_sig = average_sig[2];
  597. max_average_sig_antenna_i = 2;
  598. active_chains = (1 << max_average_sig_antenna_i);
  599. }
  600. IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
  601. average_sig[0], average_sig[1], average_sig[2]);
  602. IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
  603. max_average_sig, max_average_sig_antenna_i);
  604. /* Compare signal strengths for all 3 receivers. */
  605. for (i = 0; i < NUM_RX_CHAINS; i++) {
  606. if (i != max_average_sig_antenna_i) {
  607. s32 rssi_delta = (max_average_sig - average_sig[i]);
  608. /* If signal is very weak, compared with
  609. * strongest, mark it as disconnected. */
  610. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  611. data->disconn_array[i] = 1;
  612. else
  613. active_chains |= (1 << i);
  614. IWL_DEBUG_CALIB("i = %d rssiDelta = %d "
  615. "disconn_array[i] = %d\n",
  616. i, rssi_delta, data->disconn_array[i]);
  617. }
  618. }
  619. num_tx_chains = 0;
  620. for (i = 0; i < NUM_RX_CHAINS; i++) {
  621. /* loops on all the bits of
  622. * priv->hw_setting.valid_tx_ant */
  623. u8 ant_msk = (1 << i);
  624. if (!(priv->hw_params.valid_tx_ant & ant_msk))
  625. continue;
  626. num_tx_chains++;
  627. if (data->disconn_array[i] == 0)
  628. /* there is a Tx antenna connected */
  629. break;
  630. if (num_tx_chains == priv->hw_params.tx_chains_num &&
  631. data->disconn_array[i]) {
  632. /* This is the last TX antenna and is also
  633. * disconnected connect it anyway */
  634. data->disconn_array[i] = 0;
  635. active_chains |= ant_msk;
  636. IWL_DEBUG_CALIB("All Tx chains are disconnected W/A - "
  637. "declare %d as connected\n", i);
  638. break;
  639. }
  640. }
  641. IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
  642. active_chains);
  643. /* Save for use within RXON, TX, SCAN commands, etc. */
  644. /*priv->valid_antenna = active_chains;*/
  645. /*FIXME: should be reflected in RX chains in RXON */
  646. /* Analyze noise for rx balance */
  647. average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
  648. average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
  649. average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
  650. for (i = 0; i < NUM_RX_CHAINS; i++) {
  651. if (!(data->disconn_array[i]) &&
  652. (average_noise[i] <= min_average_noise)) {
  653. /* This means that chain i is active and has
  654. * lower noise values so far: */
  655. min_average_noise = average_noise[i];
  656. min_average_noise_antenna_i = i;
  657. }
  658. }
  659. IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
  660. average_noise[0], average_noise[1],
  661. average_noise[2]);
  662. IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
  663. min_average_noise, min_average_noise_antenna_i);
  664. priv->cfg->ops->utils->gain_computation(priv, average_noise,
  665. min_average_noise_antenna_i, min_average_noise);
  666. }
  667. EXPORT_SYMBOL(iwl_chain_noise_calibration);
  668. void iwl_reset_run_time_calib(struct iwl_priv *priv)
  669. {
  670. int i;
  671. memset(&(priv->sensitivity_data), 0,
  672. sizeof(struct iwl_sensitivity_data));
  673. memset(&(priv->chain_noise_data), 0,
  674. sizeof(struct iwl_chain_noise_data));
  675. for (i = 0; i < NUM_RX_CHAINS; i++)
  676. priv->chain_noise_data.delta_gain_code[i] =
  677. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  678. /* Ask for statistics now, the uCode will send notification
  679. * periodically after association */
  680. iwl_send_statistics_request(priv, CMD_ASYNC);
  681. }
  682. EXPORT_SYMBOL(iwl_reset_run_time_calib);