iwl-agn-calib.c 36 KB

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