iwl-calib.c 30 KB

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