iwl-agn-calib.c 34 KB

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