iwl-calib.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. * Intel Linux Wireless <ilw@linux.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 <net/mac80211.h>
  63. #include "iwl-dev.h"
  64. #include "iwl-core.h"
  65. #include "iwl-calib.h"
  66. /*****************************************************************************
  67. * INIT calibrations framework
  68. *****************************************************************************/
  69. int iwl_send_calib_results(struct iwl_priv *priv)
  70. {
  71. int ret = 0;
  72. int i = 0;
  73. struct iwl_host_cmd hcmd = {
  74. .id = REPLY_PHY_CALIBRATION_CMD,
  75. .meta.flags = CMD_SIZE_HUGE,
  76. };
  77. for (i = 0; i < IWL_CALIB_MAX; i++) {
  78. if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
  79. priv->calib_results[i].buf) {
  80. hcmd.len = priv->calib_results[i].buf_len;
  81. hcmd.data = priv->calib_results[i].buf;
  82. ret = iwl_send_cmd_sync(priv, &hcmd);
  83. if (ret)
  84. goto err;
  85. }
  86. }
  87. return 0;
  88. err:
  89. IWL_ERROR("Error %d iteration %d\n", ret, i);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(iwl_send_calib_results);
  93. int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
  94. {
  95. if (res->buf_len != len) {
  96. kfree(res->buf);
  97. res->buf = kzalloc(len, GFP_ATOMIC);
  98. }
  99. if (unlikely(res->buf == NULL))
  100. return -ENOMEM;
  101. res->buf_len = len;
  102. memcpy(res->buf, buf, len);
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(iwl_calib_set);
  106. void iwl_calib_free_results(struct iwl_priv *priv)
  107. {
  108. int i;
  109. for (i = 0; i < IWL_CALIB_MAX; i++) {
  110. kfree(priv->calib_results[i].buf);
  111. priv->calib_results[i].buf = NULL;
  112. priv->calib_results[i].buf_len = 0;
  113. }
  114. }
  115. /*****************************************************************************
  116. * RUNTIME calibrations framework
  117. *****************************************************************************/
  118. /* "false alarms" are signals that our DSP tries to lock onto,
  119. * but then determines that they are either noise, or transmissions
  120. * from a distant wireless network (also "noise", really) that get
  121. * "stepped on" by stronger transmissions within our own network.
  122. * This algorithm attempts to set a sensitivity level that is high
  123. * enough to receive all of our own network traffic, but not so
  124. * high that our DSP gets too busy trying to lock onto non-network
  125. * activity/noise. */
  126. static int iwl_sens_energy_cck(struct iwl_priv *priv,
  127. u32 norm_fa,
  128. u32 rx_enable_time,
  129. struct statistics_general_data *rx_info)
  130. {
  131. u32 max_nrg_cck = 0;
  132. int i = 0;
  133. u8 max_silence_rssi = 0;
  134. u32 silence_ref = 0;
  135. u8 silence_rssi_a = 0;
  136. u8 silence_rssi_b = 0;
  137. u8 silence_rssi_c = 0;
  138. u32 val;
  139. /* "false_alarms" values below are cross-multiplications to assess the
  140. * numbers of false alarms within the measured period of actual Rx
  141. * (Rx is off when we're txing), vs the min/max expected false alarms
  142. * (some should be expected if rx is sensitive enough) in a
  143. * hypothetical listening period of 200 time units (TU), 204.8 msec:
  144. *
  145. * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
  146. *
  147. * */
  148. u32 false_alarms = norm_fa * 200 * 1024;
  149. u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
  150. u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
  151. struct iwl_sensitivity_data *data = NULL;
  152. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  153. data = &(priv->sensitivity_data);
  154. data->nrg_auto_corr_silence_diff = 0;
  155. /* Find max silence rssi among all 3 receivers.
  156. * This is background noise, which may include transmissions from other
  157. * networks, measured during silence before our network's beacon */
  158. silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
  159. ALL_BAND_FILTER) >> 8);
  160. silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
  161. ALL_BAND_FILTER) >> 8);
  162. silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
  163. ALL_BAND_FILTER) >> 8);
  164. val = max(silence_rssi_b, silence_rssi_c);
  165. max_silence_rssi = max(silence_rssi_a, (u8) val);
  166. /* Store silence rssi in 20-beacon history table */
  167. data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
  168. data->nrg_silence_idx++;
  169. if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
  170. data->nrg_silence_idx = 0;
  171. /* Find max silence rssi across 20 beacon history */
  172. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
  173. val = data->nrg_silence_rssi[i];
  174. silence_ref = max(silence_ref, val);
  175. }
  176. IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
  177. silence_rssi_a, silence_rssi_b, silence_rssi_c,
  178. silence_ref);
  179. /* Find max rx energy (min value!) among all 3 receivers,
  180. * measured during beacon frame.
  181. * Save it in 10-beacon history table. */
  182. i = data->nrg_energy_idx;
  183. val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
  184. data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
  185. data->nrg_energy_idx++;
  186. if (data->nrg_energy_idx >= 10)
  187. data->nrg_energy_idx = 0;
  188. /* Find min rx energy (max value) across 10 beacon history.
  189. * This is the minimum signal level that we want to receive well.
  190. * Add backoff (margin so we don't miss slightly lower energy frames).
  191. * This establishes an upper bound (min value) for energy threshold. */
  192. max_nrg_cck = data->nrg_value[0];
  193. for (i = 1; i < 10; i++)
  194. max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
  195. max_nrg_cck += 6;
  196. IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
  197. rx_info->beacon_energy_a, rx_info->beacon_energy_b,
  198. rx_info->beacon_energy_c, max_nrg_cck - 6);
  199. /* Count number of consecutive beacons with fewer-than-desired
  200. * false alarms. */
  201. if (false_alarms < min_false_alarms)
  202. data->num_in_cck_no_fa++;
  203. else
  204. data->num_in_cck_no_fa = 0;
  205. IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
  206. data->num_in_cck_no_fa);
  207. /* If we got too many false alarms this time, reduce sensitivity */
  208. if ((false_alarms > max_false_alarms) &&
  209. (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
  210. IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
  211. false_alarms, max_false_alarms);
  212. IWL_DEBUG_CALIB("... reducing sensitivity\n");
  213. data->nrg_curr_state = IWL_FA_TOO_MANY;
  214. /* Store for "fewer than desired" on later beacon */
  215. data->nrg_silence_ref = silence_ref;
  216. /* increase energy threshold (reduce nrg value)
  217. * to decrease sensitivity */
  218. if (data->nrg_th_cck >
  219. (ranges->max_nrg_cck + NRG_STEP_CCK))
  220. data->nrg_th_cck = data->nrg_th_cck
  221. - NRG_STEP_CCK;
  222. else
  223. data->nrg_th_cck = ranges->max_nrg_cck;
  224. /* Else if we got fewer than desired, increase sensitivity */
  225. } else if (false_alarms < min_false_alarms) {
  226. data->nrg_curr_state = IWL_FA_TOO_FEW;
  227. /* Compare silence level with silence level for most recent
  228. * healthy number or too many false alarms */
  229. data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
  230. (s32)silence_ref;
  231. IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
  232. false_alarms, min_false_alarms,
  233. data->nrg_auto_corr_silence_diff);
  234. /* Increase value to increase sensitivity, but only if:
  235. * 1a) previous beacon did *not* have *too many* false alarms
  236. * 1b) AND there's a significant difference in Rx levels
  237. * from a previous beacon with too many, or healthy # FAs
  238. * OR 2) We've seen a lot of beacons (100) with too few
  239. * false alarms */
  240. if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
  241. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  242. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  243. IWL_DEBUG_CALIB("... increasing sensitivity\n");
  244. /* Increase nrg value to increase sensitivity */
  245. val = data->nrg_th_cck + NRG_STEP_CCK;
  246. data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
  247. } else {
  248. IWL_DEBUG_CALIB("... but not changing sensitivity\n");
  249. }
  250. /* Else we got a healthy number of false alarms, keep status quo */
  251. } else {
  252. IWL_DEBUG_CALIB(" FA in safe zone\n");
  253. data->nrg_curr_state = IWL_FA_GOOD_RANGE;
  254. /* Store for use in "fewer than desired" with later beacon */
  255. data->nrg_silence_ref = silence_ref;
  256. /* If previous beacon had too many false alarms,
  257. * give it some extra margin by reducing sensitivity again
  258. * (but don't go below measured energy of desired Rx) */
  259. if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
  260. IWL_DEBUG_CALIB("... increasing margin\n");
  261. if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
  262. data->nrg_th_cck -= NRG_MARGIN;
  263. else
  264. data->nrg_th_cck = max_nrg_cck;
  265. }
  266. }
  267. /* Make sure the energy threshold does not go above the measured
  268. * energy of the desired Rx signals (reduced by backoff margin),
  269. * or else we might start missing Rx frames.
  270. * Lower value is higher energy, so we use max()!
  271. */
  272. data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
  273. IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
  274. data->nrg_prev_state = data->nrg_curr_state;
  275. /* Auto-correlation CCK algorithm */
  276. if (false_alarms > min_false_alarms) {
  277. /* increase auto_corr values to decrease sensitivity
  278. * so the DSP won't be disturbed by the noise
  279. */
  280. if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
  281. data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
  282. else {
  283. val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
  284. data->auto_corr_cck =
  285. min((u32)ranges->auto_corr_max_cck, val);
  286. }
  287. val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
  288. data->auto_corr_cck_mrc =
  289. min((u32)ranges->auto_corr_max_cck_mrc, val);
  290. } else if ((false_alarms < min_false_alarms) &&
  291. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  292. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  293. /* Decrease auto_corr values to increase sensitivity */
  294. val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
  295. data->auto_corr_cck =
  296. max((u32)ranges->auto_corr_min_cck, val);
  297. val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
  298. data->auto_corr_cck_mrc =
  299. max((u32)ranges->auto_corr_min_cck_mrc, val);
  300. }
  301. return 0;
  302. }
  303. static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
  304. u32 norm_fa,
  305. u32 rx_enable_time)
  306. {
  307. u32 val;
  308. u32 false_alarms = norm_fa * 200 * 1024;
  309. u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
  310. u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
  311. struct iwl_sensitivity_data *data = NULL;
  312. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  313. data = &(priv->sensitivity_data);
  314. /* If we got too many false alarms this time, reduce sensitivity */
  315. if (false_alarms > max_false_alarms) {
  316. IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
  317. false_alarms, max_false_alarms);
  318. val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
  319. data->auto_corr_ofdm =
  320. min((u32)ranges->auto_corr_max_ofdm, val);
  321. val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
  322. data->auto_corr_ofdm_mrc =
  323. min((u32)ranges->auto_corr_max_ofdm_mrc, val);
  324. val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
  325. data->auto_corr_ofdm_x1 =
  326. min((u32)ranges->auto_corr_max_ofdm_x1, val);
  327. val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
  328. data->auto_corr_ofdm_mrc_x1 =
  329. min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
  330. }
  331. /* Else if we got fewer than desired, increase sensitivity */
  332. else if (false_alarms < min_false_alarms) {
  333. IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
  334. false_alarms, min_false_alarms);
  335. val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
  336. data->auto_corr_ofdm =
  337. max((u32)ranges->auto_corr_min_ofdm, val);
  338. val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
  339. data->auto_corr_ofdm_mrc =
  340. max((u32)ranges->auto_corr_min_ofdm_mrc, val);
  341. val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
  342. data->auto_corr_ofdm_x1 =
  343. max((u32)ranges->auto_corr_min_ofdm_x1, val);
  344. val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
  345. data->auto_corr_ofdm_mrc_x1 =
  346. max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
  347. } else {
  348. IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
  349. min_false_alarms, false_alarms, max_false_alarms);
  350. }
  351. return 0;
  352. }
  353. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  354. static int iwl_sensitivity_write(struct iwl_priv *priv)
  355. {
  356. int ret = 0;
  357. struct iwl_sensitivity_cmd cmd ;
  358. struct iwl_sensitivity_data *data = NULL;
  359. struct iwl_host_cmd cmd_out = {
  360. .id = SENSITIVITY_CMD,
  361. .len = sizeof(struct iwl_sensitivity_cmd),
  362. .meta.flags = CMD_ASYNC,
  363. .data = &cmd,
  364. };
  365. data = &(priv->sensitivity_data);
  366. memset(&cmd, 0, sizeof(cmd));
  367. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  368. cpu_to_le16((u16)data->auto_corr_ofdm);
  369. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  370. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  371. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  372. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  373. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  374. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  375. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  376. cpu_to_le16((u16)data->auto_corr_cck);
  377. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  378. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  379. cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
  380. cpu_to_le16((u16)data->nrg_th_cck);
  381. cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  382. cpu_to_le16((u16)data->nrg_th_ofdm);
  383. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  384. __constant_cpu_to_le16(190);
  385. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  386. __constant_cpu_to_le16(390);
  387. cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
  388. __constant_cpu_to_le16(62);
  389. IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  390. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  391. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  392. data->nrg_th_ofdm);
  393. IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
  394. data->auto_corr_cck, data->auto_corr_cck_mrc,
  395. data->nrg_th_cck);
  396. /* Update uCode's "work" table, and copy it to DSP */
  397. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  398. /* Don't send command to uCode if nothing has changed */
  399. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  400. sizeof(u16)*HD_TABLE_SIZE)) {
  401. IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
  402. return 0;
  403. }
  404. /* Copy table for comparison next time */
  405. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  406. sizeof(u16)*HD_TABLE_SIZE);
  407. ret = iwl_send_cmd(priv, &cmd_out);
  408. if (ret)
  409. IWL_ERROR("SENSITIVITY_CMD failed\n");
  410. return ret;
  411. }
  412. void iwl_init_sensitivity(struct iwl_priv *priv)
  413. {
  414. int ret = 0;
  415. int i;
  416. struct iwl_sensitivity_data *data = NULL;
  417. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  418. if (priv->disable_sens_cal)
  419. return;
  420. IWL_DEBUG_CALIB("Start iwl_init_sensitivity\n");
  421. /* Clear driver's sensitivity algo data */
  422. data = &(priv->sensitivity_data);
  423. if (ranges == NULL)
  424. return;
  425. memset(data, 0, sizeof(struct iwl_sensitivity_data));
  426. data->num_in_cck_no_fa = 0;
  427. data->nrg_curr_state = IWL_FA_TOO_MANY;
  428. data->nrg_prev_state = IWL_FA_TOO_MANY;
  429. data->nrg_silence_ref = 0;
  430. data->nrg_silence_idx = 0;
  431. data->nrg_energy_idx = 0;
  432. for (i = 0; i < 10; i++)
  433. data->nrg_value[i] = 0;
  434. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  435. data->nrg_silence_rssi[i] = 0;
  436. data->auto_corr_ofdm = 90;
  437. data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
  438. data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
  439. data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
  440. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  441. data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
  442. data->nrg_th_cck = ranges->nrg_th_cck;
  443. data->nrg_th_ofdm = ranges->nrg_th_ofdm;
  444. data->last_bad_plcp_cnt_ofdm = 0;
  445. data->last_fa_cnt_ofdm = 0;
  446. data->last_bad_plcp_cnt_cck = 0;
  447. data->last_fa_cnt_cck = 0;
  448. ret |= iwl_sensitivity_write(priv);
  449. IWL_DEBUG_CALIB("<<return 0x%X\n", ret);
  450. }
  451. EXPORT_SYMBOL(iwl_init_sensitivity);
  452. void iwl_sensitivity_calibration(struct iwl_priv *priv,
  453. struct iwl_notif_statistics *resp)
  454. {
  455. u32 rx_enable_time;
  456. u32 fa_cck;
  457. u32 fa_ofdm;
  458. u32 bad_plcp_cck;
  459. u32 bad_plcp_ofdm;
  460. u32 norm_fa_ofdm;
  461. u32 norm_fa_cck;
  462. struct iwl_sensitivity_data *data = NULL;
  463. struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
  464. struct statistics_rx *statistics = &(resp->rx);
  465. unsigned long flags;
  466. struct statistics_general_data statis;
  467. if (priv->disable_sens_cal)
  468. return;
  469. data = &(priv->sensitivity_data);
  470. if (!iwl_is_associated(priv)) {
  471. IWL_DEBUG_CALIB("<< - not associated\n");
  472. return;
  473. }
  474. spin_lock_irqsave(&priv->lock, flags);
  475. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  476. IWL_DEBUG_CALIB("<< invalid data.\n");
  477. spin_unlock_irqrestore(&priv->lock, flags);
  478. return;
  479. }
  480. /* Extract Statistics: */
  481. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  482. fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
  483. fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
  484. bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
  485. bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
  486. statis.beacon_silence_rssi_a =
  487. le32_to_cpu(statistics->general.beacon_silence_rssi_a);
  488. statis.beacon_silence_rssi_b =
  489. le32_to_cpu(statistics->general.beacon_silence_rssi_b);
  490. statis.beacon_silence_rssi_c =
  491. le32_to_cpu(statistics->general.beacon_silence_rssi_c);
  492. statis.beacon_energy_a =
  493. le32_to_cpu(statistics->general.beacon_energy_a);
  494. statis.beacon_energy_b =
  495. le32_to_cpu(statistics->general.beacon_energy_b);
  496. statis.beacon_energy_c =
  497. le32_to_cpu(statistics->general.beacon_energy_c);
  498. spin_unlock_irqrestore(&priv->lock, flags);
  499. IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
  500. if (!rx_enable_time) {
  501. IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
  502. return;
  503. }
  504. /* These statistics increase monotonically, and do not reset
  505. * at each beacon. Calculate difference from last value, or just
  506. * use the new statistics value if it has reset or wrapped around. */
  507. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  508. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  509. else {
  510. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  511. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  512. }
  513. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  514. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  515. else {
  516. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  517. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  518. }
  519. if (data->last_fa_cnt_ofdm > fa_ofdm)
  520. data->last_fa_cnt_ofdm = fa_ofdm;
  521. else {
  522. fa_ofdm -= data->last_fa_cnt_ofdm;
  523. data->last_fa_cnt_ofdm += fa_ofdm;
  524. }
  525. if (data->last_fa_cnt_cck > fa_cck)
  526. data->last_fa_cnt_cck = fa_cck;
  527. else {
  528. fa_cck -= data->last_fa_cnt_cck;
  529. data->last_fa_cnt_cck += fa_cck;
  530. }
  531. /* Total aborted signal locks */
  532. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  533. norm_fa_cck = fa_cck + bad_plcp_cck;
  534. IWL_DEBUG_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  535. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  536. iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  537. iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  538. iwl_sensitivity_write(priv);
  539. return;
  540. }
  541. EXPORT_SYMBOL(iwl_sensitivity_calibration);
  542. /*
  543. * Accumulate 20 beacons of signal and noise statistics for each of
  544. * 3 receivers/antennas/rx-chains, then figure out:
  545. * 1) Which antennas are connected.
  546. * 2) Differential rx gain settings to balance the 3 receivers.
  547. */
  548. void iwl_chain_noise_calibration(struct iwl_priv *priv,
  549. struct iwl_notif_statistics *stat_resp)
  550. {
  551. struct iwl_chain_noise_data *data = NULL;
  552. u32 chain_noise_a;
  553. u32 chain_noise_b;
  554. u32 chain_noise_c;
  555. u32 chain_sig_a;
  556. u32 chain_sig_b;
  557. u32 chain_sig_c;
  558. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  559. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  560. u32 max_average_sig;
  561. u16 max_average_sig_antenna_i;
  562. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  563. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  564. u16 i = 0;
  565. u16 rxon_chnum = INITIALIZATION_VALUE;
  566. u16 stat_chnum = INITIALIZATION_VALUE;
  567. u8 rxon_band24;
  568. u8 stat_band24;
  569. u32 active_chains = 0;
  570. u8 num_tx_chains;
  571. unsigned long flags;
  572. struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
  573. if (priv->disable_chain_noise_cal)
  574. return;
  575. data = &(priv->chain_noise_data);
  576. /* Accumulate just the first 20 beacons after the first association,
  577. * then we're done forever. */
  578. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  579. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  580. IWL_DEBUG_CALIB("Wait for noise calib reset\n");
  581. return;
  582. }
  583. spin_lock_irqsave(&priv->lock, flags);
  584. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  585. IWL_DEBUG_CALIB(" << Interference data unavailable\n");
  586. spin_unlock_irqrestore(&priv->lock, flags);
  587. return;
  588. }
  589. rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
  590. rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
  591. stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
  592. stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
  593. /* Make sure we accumulate data for just the associated channel
  594. * (even if scanning). */
  595. if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
  596. IWL_DEBUG_CALIB("Stats not from chan=%d, band24=%d\n",
  597. rxon_chnum, rxon_band24);
  598. spin_unlock_irqrestore(&priv->lock, flags);
  599. return;
  600. }
  601. /* Accumulate beacon statistics values across 20 beacons */
  602. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  603. IN_BAND_FILTER;
  604. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  605. IN_BAND_FILTER;
  606. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  607. IN_BAND_FILTER;
  608. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  609. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  610. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  611. spin_unlock_irqrestore(&priv->lock, flags);
  612. data->beacon_count++;
  613. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  614. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  615. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  616. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  617. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  618. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  619. IWL_DEBUG_CALIB("chan=%d, band24=%d, beacon=%d\n",
  620. rxon_chnum, rxon_band24, data->beacon_count);
  621. IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
  622. chain_sig_a, chain_sig_b, chain_sig_c);
  623. IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
  624. chain_noise_a, chain_noise_b, chain_noise_c);
  625. /* If this is the 20th beacon, determine:
  626. * 1) Disconnected antennas (using signal strengths)
  627. * 2) Differential gain (using silence noise) to balance receivers */
  628. if (data->beacon_count != CAL_NUM_OF_BEACONS)
  629. return;
  630. /* Analyze signal for disconnected antenna */
  631. average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
  632. average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
  633. average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
  634. if (average_sig[0] >= average_sig[1]) {
  635. max_average_sig = average_sig[0];
  636. max_average_sig_antenna_i = 0;
  637. active_chains = (1 << max_average_sig_antenna_i);
  638. } else {
  639. max_average_sig = average_sig[1];
  640. max_average_sig_antenna_i = 1;
  641. active_chains = (1 << max_average_sig_antenna_i);
  642. }
  643. if (average_sig[2] >= max_average_sig) {
  644. max_average_sig = average_sig[2];
  645. max_average_sig_antenna_i = 2;
  646. active_chains = (1 << max_average_sig_antenna_i);
  647. }
  648. IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
  649. average_sig[0], average_sig[1], average_sig[2]);
  650. IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
  651. max_average_sig, max_average_sig_antenna_i);
  652. /* Compare signal strengths for all 3 receivers. */
  653. for (i = 0; i < NUM_RX_CHAINS; i++) {
  654. if (i != max_average_sig_antenna_i) {
  655. s32 rssi_delta = (max_average_sig - average_sig[i]);
  656. /* If signal is very weak, compared with
  657. * strongest, mark it as disconnected. */
  658. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  659. data->disconn_array[i] = 1;
  660. else
  661. active_chains |= (1 << i);
  662. IWL_DEBUG_CALIB("i = %d rssiDelta = %d "
  663. "disconn_array[i] = %d\n",
  664. i, rssi_delta, data->disconn_array[i]);
  665. }
  666. }
  667. num_tx_chains = 0;
  668. for (i = 0; i < NUM_RX_CHAINS; i++) {
  669. /* loops on all the bits of
  670. * priv->hw_setting.valid_tx_ant */
  671. u8 ant_msk = (1 << i);
  672. if (!(priv->hw_params.valid_tx_ant & ant_msk))
  673. continue;
  674. num_tx_chains++;
  675. if (data->disconn_array[i] == 0)
  676. /* there is a Tx antenna connected */
  677. break;
  678. if (num_tx_chains == priv->hw_params.tx_chains_num &&
  679. data->disconn_array[i]) {
  680. /* This is the last TX antenna and is also
  681. * disconnected connect it anyway */
  682. data->disconn_array[i] = 0;
  683. active_chains |= ant_msk;
  684. IWL_DEBUG_CALIB("All Tx chains are disconnected W/A - "
  685. "declare %d as connected\n", i);
  686. break;
  687. }
  688. }
  689. /* Save for use within RXON, TX, SCAN commands, etc. */
  690. priv->chain_noise_data.active_chains = active_chains;
  691. IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
  692. active_chains);
  693. /* Analyze noise for rx balance */
  694. average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
  695. average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
  696. average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
  697. for (i = 0; i < NUM_RX_CHAINS; i++) {
  698. if (!(data->disconn_array[i]) &&
  699. (average_noise[i] <= min_average_noise)) {
  700. /* This means that chain i is active and has
  701. * lower noise values so far: */
  702. min_average_noise = average_noise[i];
  703. min_average_noise_antenna_i = i;
  704. }
  705. }
  706. IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
  707. average_noise[0], average_noise[1],
  708. average_noise[2]);
  709. IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
  710. min_average_noise, min_average_noise_antenna_i);
  711. priv->cfg->ops->utils->gain_computation(priv, average_noise,
  712. min_average_noise_antenna_i, min_average_noise);
  713. /* Some power changes may have been made during the calibration.
  714. * Update and commit the RXON
  715. */
  716. if (priv->cfg->ops->lib->update_chain_flags)
  717. priv->cfg->ops->lib->update_chain_flags(priv);
  718. data->state = IWL_CHAIN_NOISE_DONE;
  719. iwl_power_enable_management(priv);
  720. }
  721. EXPORT_SYMBOL(iwl_chain_noise_calibration);
  722. void iwl_reset_run_time_calib(struct iwl_priv *priv)
  723. {
  724. int i;
  725. memset(&(priv->sensitivity_data), 0,
  726. sizeof(struct iwl_sensitivity_data));
  727. memset(&(priv->chain_noise_data), 0,
  728. sizeof(struct iwl_chain_noise_data));
  729. for (i = 0; i < NUM_RX_CHAINS; i++)
  730. priv->chain_noise_data.delta_gain_code[i] =
  731. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  732. /* Ask for statistics now, the uCode will send notification
  733. * periodically after association */
  734. iwl_send_statistics_request(priv, CMD_ASYNC);
  735. }
  736. EXPORT_SYMBOL(iwl_reset_run_time_calib);