iwl-agn-calib.c 33 KB

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