iwl-agn-calib.c 36 KB

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