iwl-agn-calib.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. #include "iwl-trans.h"
  68. #include "iwl-agn.h"
  69. /*****************************************************************************
  70. * INIT calibrations framework
  71. *****************************************************************************/
  72. struct statistics_general_data {
  73. u32 beacon_silence_rssi_a;
  74. u32 beacon_silence_rssi_b;
  75. u32 beacon_silence_rssi_c;
  76. u32 beacon_energy_a;
  77. u32 beacon_energy_b;
  78. u32 beacon_energy_c;
  79. };
  80. int iwl_send_calib_results(struct iwl_priv *priv)
  81. {
  82. int ret = 0;
  83. int i = 0;
  84. struct iwl_host_cmd hcmd = {
  85. .id = REPLY_PHY_CALIBRATION_CMD,
  86. .flags = CMD_SYNC,
  87. };
  88. for (i = 0; i < IWL_CALIB_MAX; i++) {
  89. if ((BIT(i) & hw_params(priv).calib_init_cfg) &&
  90. priv->calib_results[i].buf) {
  91. hcmd.len[0] = priv->calib_results[i].buf_len;
  92. hcmd.data[0] = priv->calib_results[i].buf;
  93. hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  94. ret = iwl_trans_send_cmd(trans(priv), &hcmd);
  95. if (ret) {
  96. IWL_ERR(priv, "Error %d iteration %d\n",
  97. ret, i);
  98. break;
  99. }
  100. }
  101. }
  102. return ret;
  103. }
  104. int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
  105. {
  106. if (res->buf_len != len) {
  107. kfree(res->buf);
  108. res->buf = kzalloc(len, GFP_ATOMIC);
  109. }
  110. if (unlikely(res->buf == NULL))
  111. return -ENOMEM;
  112. res->buf_len = len;
  113. memcpy(res->buf, buf, len);
  114. return 0;
  115. }
  116. void iwl_calib_free_results(struct iwl_priv *priv)
  117. {
  118. int i;
  119. for (i = 0; i < IWL_CALIB_MAX; i++) {
  120. kfree(priv->calib_results[i].buf);
  121. priv->calib_results[i].buf = NULL;
  122. priv->calib_results[i].buf_len = 0;
  123. }
  124. }
  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 = hw_params(priv).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 = hw_params(priv).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. static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
  359. struct iwl_sensitivity_data *data,
  360. __le16 *tbl)
  361. {
  362. tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  363. cpu_to_le16((u16)data->auto_corr_ofdm);
  364. tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  365. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  366. tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  367. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  368. tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  369. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  370. tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  371. cpu_to_le16((u16)data->auto_corr_cck);
  372. tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  373. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  374. tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
  375. cpu_to_le16((u16)data->nrg_th_cck);
  376. tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  377. cpu_to_le16((u16)data->nrg_th_ofdm);
  378. tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  379. cpu_to_le16(data->barker_corr_th_min);
  380. tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  381. cpu_to_le16(data->barker_corr_th_min_mrc);
  382. tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
  383. cpu_to_le16(data->nrg_th_cca);
  384. IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  385. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  386. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  387. data->nrg_th_ofdm);
  388. IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
  389. data->auto_corr_cck, data->auto_corr_cck_mrc,
  390. data->nrg_th_cck);
  391. }
  392. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  393. static int iwl_sensitivity_write(struct iwl_priv *priv)
  394. {
  395. struct iwl_sensitivity_cmd cmd;
  396. struct iwl_sensitivity_data *data = NULL;
  397. struct iwl_host_cmd cmd_out = {
  398. .id = SENSITIVITY_CMD,
  399. .len = { sizeof(struct iwl_sensitivity_cmd), },
  400. .flags = CMD_ASYNC,
  401. .data = { &cmd, },
  402. };
  403. data = &(priv->sensitivity_data);
  404. memset(&cmd, 0, sizeof(cmd));
  405. iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
  406. /* Update uCode's "work" table, and copy it to DSP */
  407. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  408. /* Don't send command to uCode if nothing has changed */
  409. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  410. sizeof(u16)*HD_TABLE_SIZE)) {
  411. IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
  412. return 0;
  413. }
  414. /* Copy table for comparison next time */
  415. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  416. sizeof(u16)*HD_TABLE_SIZE);
  417. return iwl_trans_send_cmd(trans(priv), &cmd_out);
  418. }
  419. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  420. static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
  421. {
  422. struct iwl_enhance_sensitivity_cmd cmd;
  423. struct iwl_sensitivity_data *data = NULL;
  424. struct iwl_host_cmd cmd_out = {
  425. .id = SENSITIVITY_CMD,
  426. .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
  427. .flags = CMD_ASYNC,
  428. .data = { &cmd, },
  429. };
  430. data = &(priv->sensitivity_data);
  431. memset(&cmd, 0, sizeof(cmd));
  432. iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
  433. if (priv->cfg->base_params->hd_v2) {
  434. cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
  435. HD_INA_NON_SQUARE_DET_OFDM_DATA_V2;
  436. cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
  437. HD_INA_NON_SQUARE_DET_CCK_DATA_V2;
  438. cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
  439. HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V2;
  440. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
  441. HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
  442. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
  443. HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
  444. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
  445. HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V2;
  446. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
  447. HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V2;
  448. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
  449. HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
  450. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
  451. HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
  452. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
  453. HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V2;
  454. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
  455. HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V2;
  456. } else {
  457. cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
  458. HD_INA_NON_SQUARE_DET_OFDM_DATA_V1;
  459. cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
  460. HD_INA_NON_SQUARE_DET_CCK_DATA_V1;
  461. cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
  462. HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V1;
  463. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
  464. HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
  465. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
  466. HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
  467. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
  468. HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V1;
  469. cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
  470. HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V1;
  471. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
  472. HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
  473. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
  474. HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
  475. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
  476. HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V1;
  477. cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
  478. HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V1;
  479. }
  480. /* Update uCode's "work" table, and copy it to DSP */
  481. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  482. /* Don't send command to uCode if nothing has changed */
  483. if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
  484. sizeof(u16)*HD_TABLE_SIZE) &&
  485. !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
  486. &(priv->enhance_sensitivity_tbl[0]),
  487. sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
  488. IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
  489. return 0;
  490. }
  491. /* Copy table for comparison next time */
  492. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
  493. sizeof(u16)*HD_TABLE_SIZE);
  494. memcpy(&(priv->enhance_sensitivity_tbl[0]),
  495. &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
  496. sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
  497. return iwl_trans_send_cmd(trans(priv), &cmd_out);
  498. }
  499. void iwl_init_sensitivity(struct iwl_priv *priv)
  500. {
  501. int ret = 0;
  502. int i;
  503. struct iwl_sensitivity_data *data = NULL;
  504. const struct iwl_sensitivity_ranges *ranges = hw_params(priv).sens;
  505. if (priv->disable_sens_cal)
  506. return;
  507. IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
  508. /* Clear driver's sensitivity algo data */
  509. data = &(priv->sensitivity_data);
  510. if (ranges == NULL)
  511. return;
  512. memset(data, 0, sizeof(struct iwl_sensitivity_data));
  513. data->num_in_cck_no_fa = 0;
  514. data->nrg_curr_state = IWL_FA_TOO_MANY;
  515. data->nrg_prev_state = IWL_FA_TOO_MANY;
  516. data->nrg_silence_ref = 0;
  517. data->nrg_silence_idx = 0;
  518. data->nrg_energy_idx = 0;
  519. for (i = 0; i < 10; i++)
  520. data->nrg_value[i] = 0;
  521. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  522. data->nrg_silence_rssi[i] = 0;
  523. data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
  524. data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
  525. data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
  526. data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
  527. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  528. data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
  529. data->nrg_th_cck = ranges->nrg_th_cck;
  530. data->nrg_th_ofdm = ranges->nrg_th_ofdm;
  531. data->barker_corr_th_min = ranges->barker_corr_th_min;
  532. data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
  533. data->nrg_th_cca = ranges->nrg_th_cca;
  534. data->last_bad_plcp_cnt_ofdm = 0;
  535. data->last_fa_cnt_ofdm = 0;
  536. data->last_bad_plcp_cnt_cck = 0;
  537. data->last_fa_cnt_cck = 0;
  538. if (priv->enhance_sensitivity_table)
  539. ret |= iwl_enhance_sensitivity_write(priv);
  540. else
  541. ret |= iwl_sensitivity_write(priv);
  542. IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
  543. }
  544. void iwl_sensitivity_calibration(struct iwl_priv *priv)
  545. {
  546. u32 rx_enable_time;
  547. u32 fa_cck;
  548. u32 fa_ofdm;
  549. u32 bad_plcp_cck;
  550. u32 bad_plcp_ofdm;
  551. u32 norm_fa_ofdm;
  552. u32 norm_fa_cck;
  553. struct iwl_sensitivity_data *data = NULL;
  554. struct statistics_rx_non_phy *rx_info;
  555. struct statistics_rx_phy *ofdm, *cck;
  556. unsigned long flags;
  557. struct statistics_general_data statis;
  558. if (priv->disable_sens_cal)
  559. return;
  560. data = &(priv->sensitivity_data);
  561. if (!iwl_is_any_associated(priv)) {
  562. IWL_DEBUG_CALIB(priv, "<< - not associated\n");
  563. return;
  564. }
  565. spin_lock_irqsave(&priv->shrd->lock, flags);
  566. rx_info = &priv->statistics.rx_non_phy;
  567. ofdm = &priv->statistics.rx_ofdm;
  568. cck = &priv->statistics.rx_cck;
  569. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  570. IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
  571. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  572. return;
  573. }
  574. /* Extract Statistics: */
  575. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  576. fa_cck = le32_to_cpu(cck->false_alarm_cnt);
  577. fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
  578. bad_plcp_cck = le32_to_cpu(cck->plcp_err);
  579. bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
  580. statis.beacon_silence_rssi_a =
  581. le32_to_cpu(rx_info->beacon_silence_rssi_a);
  582. statis.beacon_silence_rssi_b =
  583. le32_to_cpu(rx_info->beacon_silence_rssi_b);
  584. statis.beacon_silence_rssi_c =
  585. le32_to_cpu(rx_info->beacon_silence_rssi_c);
  586. statis.beacon_energy_a =
  587. le32_to_cpu(rx_info->beacon_energy_a);
  588. statis.beacon_energy_b =
  589. le32_to_cpu(rx_info->beacon_energy_b);
  590. statis.beacon_energy_c =
  591. le32_to_cpu(rx_info->beacon_energy_c);
  592. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  593. IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
  594. if (!rx_enable_time) {
  595. IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
  596. return;
  597. }
  598. /* These statistics increase monotonically, and do not reset
  599. * at each beacon. Calculate difference from last value, or just
  600. * use the new statistics value if it has reset or wrapped around. */
  601. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  602. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  603. else {
  604. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  605. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  606. }
  607. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  608. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  609. else {
  610. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  611. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  612. }
  613. if (data->last_fa_cnt_ofdm > fa_ofdm)
  614. data->last_fa_cnt_ofdm = fa_ofdm;
  615. else {
  616. fa_ofdm -= data->last_fa_cnt_ofdm;
  617. data->last_fa_cnt_ofdm += fa_ofdm;
  618. }
  619. if (data->last_fa_cnt_cck > fa_cck)
  620. data->last_fa_cnt_cck = fa_cck;
  621. else {
  622. fa_cck -= data->last_fa_cnt_cck;
  623. data->last_fa_cnt_cck += fa_cck;
  624. }
  625. /* Total aborted signal locks */
  626. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  627. norm_fa_cck = fa_cck + bad_plcp_cck;
  628. IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  629. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  630. iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  631. iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  632. if (priv->enhance_sensitivity_table)
  633. iwl_enhance_sensitivity_write(priv);
  634. else
  635. iwl_sensitivity_write(priv);
  636. }
  637. static inline u8 find_first_chain(u8 mask)
  638. {
  639. if (mask & ANT_A)
  640. return CHAIN_A;
  641. if (mask & ANT_B)
  642. return CHAIN_B;
  643. return CHAIN_C;
  644. }
  645. /**
  646. * Run disconnected antenna algorithm to find out which antennas are
  647. * disconnected.
  648. */
  649. static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
  650. struct iwl_chain_noise_data *data)
  651. {
  652. u32 active_chains = 0;
  653. u32 max_average_sig;
  654. u16 max_average_sig_antenna_i;
  655. u8 num_tx_chains;
  656. u8 first_chain;
  657. u16 i = 0;
  658. average_sig[0] = data->chain_signal_a / IWL_CAL_NUM_BEACONS;
  659. average_sig[1] = data->chain_signal_b / IWL_CAL_NUM_BEACONS;
  660. average_sig[2] = data->chain_signal_c / IWL_CAL_NUM_BEACONS;
  661. if (average_sig[0] >= average_sig[1]) {
  662. max_average_sig = average_sig[0];
  663. max_average_sig_antenna_i = 0;
  664. active_chains = (1 << max_average_sig_antenna_i);
  665. } else {
  666. max_average_sig = average_sig[1];
  667. max_average_sig_antenna_i = 1;
  668. active_chains = (1 << max_average_sig_antenna_i);
  669. }
  670. if (average_sig[2] >= max_average_sig) {
  671. max_average_sig = average_sig[2];
  672. max_average_sig_antenna_i = 2;
  673. active_chains = (1 << max_average_sig_antenna_i);
  674. }
  675. IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
  676. average_sig[0], average_sig[1], average_sig[2]);
  677. IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
  678. max_average_sig, max_average_sig_antenna_i);
  679. /* Compare signal strengths for all 3 receivers. */
  680. for (i = 0; i < NUM_RX_CHAINS; i++) {
  681. if (i != max_average_sig_antenna_i) {
  682. s32 rssi_delta = (max_average_sig - average_sig[i]);
  683. /* If signal is very weak, compared with
  684. * strongest, mark it as disconnected. */
  685. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  686. data->disconn_array[i] = 1;
  687. else
  688. active_chains |= (1 << i);
  689. IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
  690. "disconn_array[i] = %d\n",
  691. i, rssi_delta, data->disconn_array[i]);
  692. }
  693. }
  694. /*
  695. * The above algorithm sometimes fails when the ucode
  696. * reports 0 for all chains. It's not clear why that
  697. * happens to start with, but it is then causing trouble
  698. * because this can make us enable more chains than the
  699. * hardware really has.
  700. *
  701. * To be safe, simply mask out any chains that we know
  702. * are not on the device.
  703. */
  704. active_chains &= hw_params(priv).valid_rx_ant;
  705. num_tx_chains = 0;
  706. for (i = 0; i < NUM_RX_CHAINS; i++) {
  707. /* loops on all the bits of
  708. * priv->hw_setting.valid_tx_ant */
  709. u8 ant_msk = (1 << i);
  710. if (!(hw_params(priv).valid_tx_ant & ant_msk))
  711. continue;
  712. num_tx_chains++;
  713. if (data->disconn_array[i] == 0)
  714. /* there is a Tx antenna connected */
  715. break;
  716. if (num_tx_chains == hw_params(priv).tx_chains_num &&
  717. data->disconn_array[i]) {
  718. /*
  719. * If all chains are disconnected
  720. * connect the first valid tx chain
  721. */
  722. first_chain =
  723. find_first_chain(priv->cfg->valid_tx_ant);
  724. data->disconn_array[first_chain] = 0;
  725. active_chains |= BIT(first_chain);
  726. IWL_DEBUG_CALIB(priv,
  727. "All Tx chains are disconnected W/A - declare %d as connected\n",
  728. first_chain);
  729. break;
  730. }
  731. }
  732. if (active_chains != hw_params(priv).valid_rx_ant &&
  733. active_chains != priv->chain_noise_data.active_chains)
  734. IWL_DEBUG_CALIB(priv,
  735. "Detected that not all antennas are connected! "
  736. "Connected: %#x, valid: %#x.\n",
  737. active_chains,
  738. hw_params(priv).valid_rx_ant);
  739. /* Save for use within RXON, TX, SCAN commands, etc. */
  740. data->active_chains = active_chains;
  741. IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
  742. active_chains);
  743. }
  744. static void iwlagn_gain_computation(struct iwl_priv *priv,
  745. u32 average_noise[NUM_RX_CHAINS],
  746. u16 min_average_noise_antenna_i,
  747. u32 min_average_noise,
  748. u8 default_chain)
  749. {
  750. int i;
  751. s32 delta_g;
  752. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  753. /*
  754. * Find Gain Code for the chains based on "default chain"
  755. */
  756. for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
  757. if ((data->disconn_array[i])) {
  758. data->delta_gain_code[i] = 0;
  759. continue;
  760. }
  761. delta_g = (priv->cfg->base_params->chain_noise_scale *
  762. ((s32)average_noise[default_chain] -
  763. (s32)average_noise[i])) / 1500;
  764. /* bound gain by 2 bits value max, 3rd bit is sign */
  765. data->delta_gain_code[i] =
  766. min(abs(delta_g),
  767. (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
  768. if (delta_g < 0)
  769. /*
  770. * set negative sign ...
  771. * note to Intel developers: This is uCode API format,
  772. * not the format of any internal device registers.
  773. * Do not change this format for e.g. 6050 or similar
  774. * devices. Change format only if more resolution
  775. * (i.e. more than 2 bits magnitude) is needed.
  776. */
  777. data->delta_gain_code[i] |= (1 << 2);
  778. }
  779. IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
  780. data->delta_gain_code[1], data->delta_gain_code[2]);
  781. if (!data->radio_write) {
  782. struct iwl_calib_chain_noise_gain_cmd cmd;
  783. memset(&cmd, 0, sizeof(cmd));
  784. iwl_set_calib_hdr(&cmd.hdr,
  785. priv->phy_calib_chain_noise_gain_cmd);
  786. cmd.delta_gain_1 = data->delta_gain_code[1];
  787. cmd.delta_gain_2 = data->delta_gain_code[2];
  788. iwl_trans_send_cmd_pdu(trans(priv), REPLY_PHY_CALIBRATION_CMD,
  789. CMD_ASYNC, sizeof(cmd), &cmd);
  790. data->radio_write = 1;
  791. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  792. }
  793. }
  794. /*
  795. * Accumulate 16 beacons of signal and noise statistics for each of
  796. * 3 receivers/antennas/rx-chains, then figure out:
  797. * 1) Which antennas are connected.
  798. * 2) Differential rx gain settings to balance the 3 receivers.
  799. */
  800. void iwl_chain_noise_calibration(struct iwl_priv *priv)
  801. {
  802. struct iwl_chain_noise_data *data = NULL;
  803. u32 chain_noise_a;
  804. u32 chain_noise_b;
  805. u32 chain_noise_c;
  806. u32 chain_sig_a;
  807. u32 chain_sig_b;
  808. u32 chain_sig_c;
  809. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  810. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  811. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  812. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  813. u16 i = 0;
  814. u16 rxon_chnum = INITIALIZATION_VALUE;
  815. u16 stat_chnum = INITIALIZATION_VALUE;
  816. u8 rxon_band24;
  817. u8 stat_band24;
  818. unsigned long flags;
  819. struct statistics_rx_non_phy *rx_info;
  820. /*
  821. * MULTI-FIXME:
  822. * When we support multiple interfaces on different channels,
  823. * this must be modified/fixed.
  824. */
  825. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  826. if (priv->disable_chain_noise_cal)
  827. return;
  828. data = &(priv->chain_noise_data);
  829. /*
  830. * Accumulate just the first "chain_noise_num_beacons" after
  831. * the first association, then we're done forever.
  832. */
  833. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  834. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  835. IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
  836. return;
  837. }
  838. spin_lock_irqsave(&priv->shrd->lock, flags);
  839. rx_info = &priv->statistics.rx_non_phy;
  840. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  841. IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
  842. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  843. return;
  844. }
  845. rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
  846. rxon_chnum = le16_to_cpu(ctx->staging.channel);
  847. stat_band24 =
  848. !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
  849. stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
  850. /* Make sure we accumulate data for just the associated channel
  851. * (even if scanning). */
  852. if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
  853. IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
  854. rxon_chnum, rxon_band24);
  855. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  856. return;
  857. }
  858. /*
  859. * Accumulate beacon statistics values across
  860. * "chain_noise_num_beacons"
  861. */
  862. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  863. IN_BAND_FILTER;
  864. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  865. IN_BAND_FILTER;
  866. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  867. IN_BAND_FILTER;
  868. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  869. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  870. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  871. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  872. data->beacon_count++;
  873. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  874. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  875. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  876. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  877. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  878. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  879. IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
  880. rxon_chnum, rxon_band24, data->beacon_count);
  881. IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
  882. chain_sig_a, chain_sig_b, chain_sig_c);
  883. IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
  884. chain_noise_a, chain_noise_b, chain_noise_c);
  885. /* If this is the "chain_noise_num_beacons", determine:
  886. * 1) Disconnected antennas (using signal strengths)
  887. * 2) Differential gain (using silence noise) to balance receivers */
  888. if (data->beacon_count != IWL_CAL_NUM_BEACONS)
  889. return;
  890. /* Analyze signal for disconnected antenna */
  891. if (priv->cfg->bt_params &&
  892. priv->cfg->bt_params->advanced_bt_coexist) {
  893. /* Disable disconnected antenna algorithm for advanced
  894. bt coex, assuming valid antennas are connected */
  895. data->active_chains = hw_params(priv).valid_rx_ant;
  896. for (i = 0; i < NUM_RX_CHAINS; i++)
  897. if (!(data->active_chains & (1<<i)))
  898. data->disconn_array[i] = 1;
  899. } else
  900. iwl_find_disconn_antenna(priv, average_sig, data);
  901. /* Analyze noise for rx balance */
  902. average_noise[0] = data->chain_noise_a / IWL_CAL_NUM_BEACONS;
  903. average_noise[1] = data->chain_noise_b / IWL_CAL_NUM_BEACONS;
  904. average_noise[2] = data->chain_noise_c / IWL_CAL_NUM_BEACONS;
  905. for (i = 0; i < NUM_RX_CHAINS; i++) {
  906. if (!(data->disconn_array[i]) &&
  907. (average_noise[i] <= min_average_noise)) {
  908. /* This means that chain i is active and has
  909. * lower noise values so far: */
  910. min_average_noise = average_noise[i];
  911. min_average_noise_antenna_i = i;
  912. }
  913. }
  914. IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
  915. average_noise[0], average_noise[1],
  916. average_noise[2]);
  917. IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
  918. min_average_noise, min_average_noise_antenna_i);
  919. iwlagn_gain_computation(priv, average_noise,
  920. min_average_noise_antenna_i, min_average_noise,
  921. find_first_chain(priv->cfg->valid_rx_ant));
  922. /* Some power changes may have been made during the calibration.
  923. * Update and commit the RXON
  924. */
  925. iwl_update_chain_flags(priv);
  926. data->state = IWL_CHAIN_NOISE_DONE;
  927. iwl_power_update_mode(priv, false);
  928. }
  929. void iwl_reset_run_time_calib(struct iwl_priv *priv)
  930. {
  931. int i;
  932. memset(&(priv->sensitivity_data), 0,
  933. sizeof(struct iwl_sensitivity_data));
  934. memset(&(priv->chain_noise_data), 0,
  935. sizeof(struct iwl_chain_noise_data));
  936. for (i = 0; i < NUM_RX_CHAINS; i++)
  937. priv->chain_noise_data.delta_gain_code[i] =
  938. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  939. /* Ask for statistics now, the uCode will send notification
  940. * periodically after association */
  941. iwl_send_statistics_request(priv, CMD_ASYNC, true);
  942. }