iwl-agn-calib.c 36 KB

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