iwl-calib.c 30 KB

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