calib.c 36 KB

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