iwl-agn-calib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 - 2010 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 - 2010 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-calib.h"
  67. /*****************************************************************************
  68. * INIT calibrations framework
  69. *****************************************************************************/
  70. struct statistics_general_data {
  71. u32 beacon_silence_rssi_a;
  72. u32 beacon_silence_rssi_b;
  73. u32 beacon_silence_rssi_c;
  74. u32 beacon_energy_a;
  75. u32 beacon_energy_b;
  76. u32 beacon_energy_c;
  77. };
  78. int iwl_send_calib_results(struct iwl_priv *priv)
  79. {
  80. int ret = 0;
  81. int i = 0;
  82. struct iwl_host_cmd hcmd = {
  83. .id = REPLY_PHY_CALIBRATION_CMD,
  84. .flags = CMD_SIZE_HUGE,
  85. };
  86. for (i = 0; i < IWL_CALIB_MAX; i++) {
  87. if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
  88. priv->calib_results[i].buf) {
  89. hcmd.len = priv->calib_results[i].buf_len;
  90. hcmd.data = priv->calib_results[i].buf;
  91. ret = iwl_send_cmd_sync(priv, &hcmd);
  92. if (ret) {
  93. IWL_ERR(priv, "Error %d iteration %d\n",
  94. ret, i);
  95. break;
  96. }
  97. }
  98. }
  99. return ret;
  100. }
  101. int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
  102. {
  103. if (res->buf_len != len) {
  104. kfree(res->buf);
  105. res->buf = kzalloc(len, GFP_ATOMIC);
  106. }
  107. if (unlikely(res->buf == NULL))
  108. return -ENOMEM;
  109. res->buf_len = len;
  110. memcpy(res->buf, buf, len);
  111. return 0;
  112. }
  113. void iwl_calib_free_results(struct iwl_priv *priv)
  114. {
  115. int i;
  116. for (i = 0; i < IWL_CALIB_MAX; i++) {
  117. kfree(priv->calib_results[i].buf);
  118. priv->calib_results[i].buf = NULL;
  119. priv->calib_results[i].buf_len = 0;
  120. }
  121. }
  122. /*****************************************************************************
  123. * RUNTIME calibrations framework
  124. *****************************************************************************/
  125. /* "false alarms" are signals that our DSP tries to lock onto,
  126. * but then determines that they are either noise, or transmissions
  127. * from a distant wireless network (also "noise", really) that get
  128. * "stepped on" by stronger transmissions within our own network.
  129. * This algorithm attempts to set a sensitivity level that is high
  130. * enough to receive all of our own network traffic, but not so
  131. * high that our DSP gets too busy trying to lock onto non-network
  132. * activity/noise. */
  133. static int iwl_sens_energy_cck(struct iwl_priv *priv,
  134. u32 norm_fa,
  135. u32 rx_enable_time,
  136. struct statistics_general_data *rx_info)
  137. {
  138. u32 max_nrg_cck = 0;
  139. int i = 0;
  140. u8 max_silence_rssi = 0;
  141. u32 silence_ref = 0;
  142. u8 silence_rssi_a = 0;
  143. u8 silence_rssi_b = 0;
  144. u8 silence_rssi_c = 0;
  145. u32 val;
  146. /* "false_alarms" values below are cross-multiplications to assess the
  147. * numbers of false alarms within the measured period of actual Rx
  148. * (Rx is off when we're txing), vs the min/max expected false alarms
  149. * (some should be expected if rx is sensitive enough) in a
  150. * hypothetical listening period of 200 time units (TU), 204.8 msec:
  151. *
  152. * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
  153. *
  154. * */
  155. u32 false_alarms = norm_fa * 200 * 1024;
  156. u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
  157. u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
  158. struct iwl_sensitivity_data *data = NULL;
  159. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  160. data = &(priv->sensitivity_data);
  161. data->nrg_auto_corr_silence_diff = 0;
  162. /* Find max silence rssi among all 3 receivers.
  163. * This is background noise, which may include transmissions from other
  164. * networks, measured during silence before our network's beacon */
  165. silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
  166. ALL_BAND_FILTER) >> 8);
  167. silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
  168. ALL_BAND_FILTER) >> 8);
  169. silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
  170. ALL_BAND_FILTER) >> 8);
  171. val = max(silence_rssi_b, silence_rssi_c);
  172. max_silence_rssi = max(silence_rssi_a, (u8) val);
  173. /* Store silence rssi in 20-beacon history table */
  174. data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
  175. data->nrg_silence_idx++;
  176. if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
  177. data->nrg_silence_idx = 0;
  178. /* Find max silence rssi across 20 beacon history */
  179. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
  180. val = data->nrg_silence_rssi[i];
  181. silence_ref = max(silence_ref, val);
  182. }
  183. IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
  184. silence_rssi_a, silence_rssi_b, silence_rssi_c,
  185. silence_ref);
  186. /* Find max rx energy (min value!) among all 3 receivers,
  187. * measured during beacon frame.
  188. * Save it in 10-beacon history table. */
  189. i = data->nrg_energy_idx;
  190. val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
  191. data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
  192. data->nrg_energy_idx++;
  193. if (data->nrg_energy_idx >= 10)
  194. data->nrg_energy_idx = 0;
  195. /* Find min rx energy (max value) across 10 beacon history.
  196. * This is the minimum signal level that we want to receive well.
  197. * Add backoff (margin so we don't miss slightly lower energy frames).
  198. * This establishes an upper bound (min value) for energy threshold. */
  199. max_nrg_cck = data->nrg_value[0];
  200. for (i = 1; i < 10; i++)
  201. max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
  202. max_nrg_cck += 6;
  203. IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
  204. rx_info->beacon_energy_a, rx_info->beacon_energy_b,
  205. rx_info->beacon_energy_c, max_nrg_cck - 6);
  206. /* Count number of consecutive beacons with fewer-than-desired
  207. * false alarms. */
  208. if (false_alarms < min_false_alarms)
  209. data->num_in_cck_no_fa++;
  210. else
  211. data->num_in_cck_no_fa = 0;
  212. IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
  213. data->num_in_cck_no_fa);
  214. /* If we got too many false alarms this time, reduce sensitivity */
  215. if ((false_alarms > max_false_alarms) &&
  216. (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
  217. IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
  218. false_alarms, max_false_alarms);
  219. IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
  220. data->nrg_curr_state = IWL_FA_TOO_MANY;
  221. /* Store for "fewer than desired" on later beacon */
  222. data->nrg_silence_ref = silence_ref;
  223. /* increase energy threshold (reduce nrg value)
  224. * to decrease sensitivity */
  225. data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
  226. /* Else if we got fewer than desired, increase sensitivity */
  227. } else if (false_alarms < min_false_alarms) {
  228. data->nrg_curr_state = IWL_FA_TOO_FEW;
  229. /* Compare silence level with silence level for most recent
  230. * healthy number or too many false alarms */
  231. data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
  232. (s32)silence_ref;
  233. IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
  234. false_alarms, min_false_alarms,
  235. data->nrg_auto_corr_silence_diff);
  236. /* Increase value to increase sensitivity, but only if:
  237. * 1a) previous beacon did *not* have *too many* false alarms
  238. * 1b) AND there's a significant difference in Rx levels
  239. * from a previous beacon with too many, or healthy # FAs
  240. * OR 2) We've seen a lot of beacons (100) with too few
  241. * false alarms */
  242. if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
  243. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  244. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  245. IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
  246. /* Increase nrg value to increase sensitivity */
  247. val = data->nrg_th_cck + NRG_STEP_CCK;
  248. data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
  249. } else {
  250. IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
  251. }
  252. /* Else we got a healthy number of false alarms, keep status quo */
  253. } else {
  254. IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
  255. data->nrg_curr_state = IWL_FA_GOOD_RANGE;
  256. /* Store for use in "fewer than desired" with later beacon */
  257. data->nrg_silence_ref = silence_ref;
  258. /* If previous beacon had too many false alarms,
  259. * give it some extra margin by reducing sensitivity again
  260. * (but don't go below measured energy of desired Rx) */
  261. if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
  262. IWL_DEBUG_CALIB(priv, "... increasing margin\n");
  263. if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
  264. data->nrg_th_cck -= NRG_MARGIN;
  265. else
  266. data->nrg_th_cck = max_nrg_cck;
  267. }
  268. }
  269. /* Make sure the energy threshold does not go above the measured
  270. * energy of the desired Rx signals (reduced by backoff margin),
  271. * or else we might start missing Rx frames.
  272. * Lower value is higher energy, so we use max()!
  273. */
  274. data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
  275. IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
  276. data->nrg_prev_state = data->nrg_curr_state;
  277. /* Auto-correlation CCK algorithm */
  278. if (false_alarms > min_false_alarms) {
  279. /* increase auto_corr values to decrease sensitivity
  280. * so the DSP won't be disturbed by the noise
  281. */
  282. if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
  283. data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
  284. else {
  285. val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
  286. data->auto_corr_cck =
  287. min((u32)ranges->auto_corr_max_cck, val);
  288. }
  289. val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
  290. data->auto_corr_cck_mrc =
  291. min((u32)ranges->auto_corr_max_cck_mrc, val);
  292. } else if ((false_alarms < min_false_alarms) &&
  293. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  294. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  295. /* Decrease auto_corr values to increase sensitivity */
  296. val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
  297. data->auto_corr_cck =
  298. max((u32)ranges->auto_corr_min_cck, val);
  299. val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
  300. data->auto_corr_cck_mrc =
  301. max((u32)ranges->auto_corr_min_cck_mrc, val);
  302. }
  303. return 0;
  304. }
  305. static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
  306. u32 norm_fa,
  307. u32 rx_enable_time)
  308. {
  309. u32 val;
  310. u32 false_alarms = norm_fa * 200 * 1024;
  311. u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
  312. u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
  313. struct iwl_sensitivity_data *data = NULL;
  314. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  315. data = &(priv->sensitivity_data);
  316. /* If we got too many false alarms this time, reduce sensitivity */
  317. if (false_alarms > max_false_alarms) {
  318. IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
  319. false_alarms, max_false_alarms);
  320. val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
  321. data->auto_corr_ofdm =
  322. min((u32)ranges->auto_corr_max_ofdm, val);
  323. val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
  324. data->auto_corr_ofdm_mrc =
  325. min((u32)ranges->auto_corr_max_ofdm_mrc, val);
  326. val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
  327. data->auto_corr_ofdm_x1 =
  328. min((u32)ranges->auto_corr_max_ofdm_x1, val);
  329. val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
  330. data->auto_corr_ofdm_mrc_x1 =
  331. min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
  332. }
  333. /* Else if we got fewer than desired, increase sensitivity */
  334. else if (false_alarms < min_false_alarms) {
  335. IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
  336. false_alarms, min_false_alarms);
  337. val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
  338. data->auto_corr_ofdm =
  339. max((u32)ranges->auto_corr_min_ofdm, val);
  340. val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
  341. data->auto_corr_ofdm_mrc =
  342. max((u32)ranges->auto_corr_min_ofdm_mrc, val);
  343. val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
  344. data->auto_corr_ofdm_x1 =
  345. max((u32)ranges->auto_corr_min_ofdm_x1, val);
  346. val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
  347. data->auto_corr_ofdm_mrc_x1 =
  348. max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
  349. } else {
  350. IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
  351. min_false_alarms, false_alarms, max_false_alarms);
  352. }
  353. return 0;
  354. }
  355. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  356. static int iwl_sensitivity_write(struct iwl_priv *priv)
  357. {
  358. struct iwl_sensitivity_cmd cmd ;
  359. struct iwl_sensitivity_data *data = NULL;
  360. struct iwl_host_cmd cmd_out = {
  361. .id = SENSITIVITY_CMD,
  362. .len = sizeof(struct iwl_sensitivity_cmd),
  363. .flags = CMD_ASYNC,
  364. .data = &cmd,
  365. };
  366. data = &(priv->sensitivity_data);
  367. memset(&cmd, 0, sizeof(cmd));
  368. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  369. cpu_to_le16((u16)data->auto_corr_ofdm);
  370. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  371. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  372. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  373. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  374. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  375. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  376. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  377. cpu_to_le16((u16)data->auto_corr_cck);
  378. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  379. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  380. cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
  381. cpu_to_le16((u16)data->nrg_th_cck);
  382. cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  383. cpu_to_le16((u16)data->nrg_th_ofdm);
  384. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  385. cpu_to_le16(data->barker_corr_th_min);
  386. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  387. cpu_to_le16(data->barker_corr_th_min_mrc);
  388. cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
  389. cpu_to_le16(data->nrg_th_cca);
  390. IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  391. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  392. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  393. data->nrg_th_ofdm);
  394. IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
  395. data->auto_corr_cck, data->auto_corr_cck_mrc,
  396. data->nrg_th_cck);
  397. /* Update uCode's "work" table, and copy it to DSP */
  398. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  399. /* Don't send command to uCode if nothing has changed */
  400. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  401. sizeof(u16)*HD_TABLE_SIZE)) {
  402. IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
  403. return 0;
  404. }
  405. /* Copy table for comparison next time */
  406. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  407. sizeof(u16)*HD_TABLE_SIZE);
  408. return iwl_send_cmd(priv, &cmd_out);
  409. }
  410. void iwl_init_sensitivity(struct iwl_priv *priv)
  411. {
  412. int ret = 0;
  413. int i;
  414. struct iwl_sensitivity_data *data = NULL;
  415. const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
  416. if (priv->disable_sens_cal)
  417. return;
  418. IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
  419. /* Clear driver's sensitivity algo data */
  420. data = &(priv->sensitivity_data);
  421. if (ranges == NULL)
  422. return;
  423. memset(data, 0, sizeof(struct iwl_sensitivity_data));
  424. data->num_in_cck_no_fa = 0;
  425. data->nrg_curr_state = IWL_FA_TOO_MANY;
  426. data->nrg_prev_state = IWL_FA_TOO_MANY;
  427. data->nrg_silence_ref = 0;
  428. data->nrg_silence_idx = 0;
  429. data->nrg_energy_idx = 0;
  430. for (i = 0; i < 10; i++)
  431. data->nrg_value[i] = 0;
  432. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  433. data->nrg_silence_rssi[i] = 0;
  434. data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
  435. data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
  436. data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
  437. data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
  438. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  439. data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
  440. data->nrg_th_cck = ranges->nrg_th_cck;
  441. data->nrg_th_ofdm = ranges->nrg_th_ofdm;
  442. data->barker_corr_th_min = ranges->barker_corr_th_min;
  443. data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
  444. data->nrg_th_cca = ranges->nrg_th_cca;
  445. data->last_bad_plcp_cnt_ofdm = 0;
  446. data->last_fa_cnt_ofdm = 0;
  447. data->last_bad_plcp_cnt_cck = 0;
  448. data->last_fa_cnt_cck = 0;
  449. ret |= iwl_sensitivity_write(priv);
  450. IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
  451. }
  452. void iwl_sensitivity_calibration(struct iwl_priv *priv,
  453. struct iwl_notif_statistics *resp)
  454. {
  455. u32 rx_enable_time;
  456. u32 fa_cck;
  457. u32 fa_ofdm;
  458. u32 bad_plcp_cck;
  459. u32 bad_plcp_ofdm;
  460. u32 norm_fa_ofdm;
  461. u32 norm_fa_cck;
  462. struct iwl_sensitivity_data *data = NULL;
  463. struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
  464. struct statistics_rx *statistics = &(resp->rx);
  465. unsigned long flags;
  466. struct statistics_general_data statis;
  467. if (priv->disable_sens_cal)
  468. return;
  469. data = &(priv->sensitivity_data);
  470. if (!iwl_is_associated(priv)) {
  471. IWL_DEBUG_CALIB(priv, "<< - not associated\n");
  472. return;
  473. }
  474. spin_lock_irqsave(&priv->lock, flags);
  475. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  476. IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
  477. spin_unlock_irqrestore(&priv->lock, flags);
  478. return;
  479. }
  480. /* Extract Statistics: */
  481. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  482. fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
  483. fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
  484. bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
  485. bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
  486. statis.beacon_silence_rssi_a =
  487. le32_to_cpu(statistics->general.beacon_silence_rssi_a);
  488. statis.beacon_silence_rssi_b =
  489. le32_to_cpu(statistics->general.beacon_silence_rssi_b);
  490. statis.beacon_silence_rssi_c =
  491. le32_to_cpu(statistics->general.beacon_silence_rssi_c);
  492. statis.beacon_energy_a =
  493. le32_to_cpu(statistics->general.beacon_energy_a);
  494. statis.beacon_energy_b =
  495. le32_to_cpu(statistics->general.beacon_energy_b);
  496. statis.beacon_energy_c =
  497. le32_to_cpu(statistics->general.beacon_energy_c);
  498. spin_unlock_irqrestore(&priv->lock, flags);
  499. IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
  500. if (!rx_enable_time) {
  501. IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
  502. return;
  503. }
  504. /* These statistics increase monotonically, and do not reset
  505. * at each beacon. Calculate difference from last value, or just
  506. * use the new statistics value if it has reset or wrapped around. */
  507. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  508. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  509. else {
  510. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  511. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  512. }
  513. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  514. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  515. else {
  516. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  517. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  518. }
  519. if (data->last_fa_cnt_ofdm > fa_ofdm)
  520. data->last_fa_cnt_ofdm = fa_ofdm;
  521. else {
  522. fa_ofdm -= data->last_fa_cnt_ofdm;
  523. data->last_fa_cnt_ofdm += fa_ofdm;
  524. }
  525. if (data->last_fa_cnt_cck > fa_cck)
  526. data->last_fa_cnt_cck = fa_cck;
  527. else {
  528. fa_cck -= data->last_fa_cnt_cck;
  529. data->last_fa_cnt_cck += fa_cck;
  530. }
  531. /* Total aborted signal locks */
  532. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  533. norm_fa_cck = fa_cck + bad_plcp_cck;
  534. IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  535. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  536. iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  537. iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  538. iwl_sensitivity_write(priv);
  539. }
  540. static inline u8 find_first_chain(u8 mask)
  541. {
  542. if (mask & ANT_A)
  543. return CHAIN_A;
  544. if (mask & ANT_B)
  545. return CHAIN_B;
  546. return CHAIN_C;
  547. }
  548. /*
  549. * Accumulate 20 beacons of signal and noise statistics for each of
  550. * 3 receivers/antennas/rx-chains, then figure out:
  551. * 1) Which antennas are connected.
  552. * 2) Differential rx gain settings to balance the 3 receivers.
  553. */
  554. void iwl_chain_noise_calibration(struct iwl_priv *priv,
  555. struct iwl_notif_statistics *stat_resp)
  556. {
  557. struct iwl_chain_noise_data *data = NULL;
  558. u32 chain_noise_a;
  559. u32 chain_noise_b;
  560. u32 chain_noise_c;
  561. u32 chain_sig_a;
  562. u32 chain_sig_b;
  563. u32 chain_sig_c;
  564. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  565. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  566. u32 max_average_sig;
  567. u16 max_average_sig_antenna_i;
  568. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  569. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  570. u16 i = 0;
  571. u16 rxon_chnum = INITIALIZATION_VALUE;
  572. u16 stat_chnum = INITIALIZATION_VALUE;
  573. u8 rxon_band24;
  574. u8 stat_band24;
  575. u32 active_chains = 0;
  576. u8 num_tx_chains;
  577. unsigned long flags;
  578. struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
  579. u8 first_chain;
  580. if (priv->disable_chain_noise_cal)
  581. return;
  582. data = &(priv->chain_noise_data);
  583. /*
  584. * Accumulate just the first "chain_noise_num_beacons" after
  585. * the first association, then we're done forever.
  586. */
  587. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  588. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  589. IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
  590. return;
  591. }
  592. spin_lock_irqsave(&priv->lock, flags);
  593. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  594. IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
  595. spin_unlock_irqrestore(&priv->lock, flags);
  596. return;
  597. }
  598. rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
  599. rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
  600. stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
  601. stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
  602. /* Make sure we accumulate data for just the associated channel
  603. * (even if scanning). */
  604. if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
  605. IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
  606. rxon_chnum, rxon_band24);
  607. spin_unlock_irqrestore(&priv->lock, flags);
  608. return;
  609. }
  610. /*
  611. * Accumulate beacon statistics values across
  612. * "chain_noise_num_beacons"
  613. */
  614. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  615. IN_BAND_FILTER;
  616. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  617. IN_BAND_FILTER;
  618. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  619. IN_BAND_FILTER;
  620. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  621. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  622. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  623. spin_unlock_irqrestore(&priv->lock, flags);
  624. data->beacon_count++;
  625. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  626. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  627. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  628. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  629. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  630. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  631. IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
  632. rxon_chnum, rxon_band24, data->beacon_count);
  633. IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
  634. chain_sig_a, chain_sig_b, chain_sig_c);
  635. IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
  636. chain_noise_a, chain_noise_b, chain_noise_c);
  637. /* If this is the "chain_noise_num_beacons", determine:
  638. * 1) Disconnected antennas (using signal strengths)
  639. * 2) Differential gain (using silence noise) to balance receivers */
  640. if (data->beacon_count != priv->cfg->chain_noise_num_beacons)
  641. return;
  642. /* Analyze signal for disconnected antenna */
  643. average_sig[0] =
  644. (data->chain_signal_a) / priv->cfg->chain_noise_num_beacons;
  645. average_sig[1] =
  646. (data->chain_signal_b) / priv->cfg->chain_noise_num_beacons;
  647. average_sig[2] =
  648. (data->chain_signal_c) / priv->cfg->chain_noise_num_beacons;
  649. if (average_sig[0] >= average_sig[1]) {
  650. max_average_sig = average_sig[0];
  651. max_average_sig_antenna_i = 0;
  652. active_chains = (1 << max_average_sig_antenna_i);
  653. } else {
  654. max_average_sig = average_sig[1];
  655. max_average_sig_antenna_i = 1;
  656. active_chains = (1 << max_average_sig_antenna_i);
  657. }
  658. if (average_sig[2] >= max_average_sig) {
  659. max_average_sig = average_sig[2];
  660. max_average_sig_antenna_i = 2;
  661. active_chains = (1 << max_average_sig_antenna_i);
  662. }
  663. IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
  664. average_sig[0], average_sig[1], average_sig[2]);
  665. IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
  666. max_average_sig, max_average_sig_antenna_i);
  667. /* Compare signal strengths for all 3 receivers. */
  668. for (i = 0; i < NUM_RX_CHAINS; i++) {
  669. if (i != max_average_sig_antenna_i) {
  670. s32 rssi_delta = (max_average_sig - average_sig[i]);
  671. /* If signal is very weak, compared with
  672. * strongest, mark it as disconnected. */
  673. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  674. data->disconn_array[i] = 1;
  675. else
  676. active_chains |= (1 << i);
  677. IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
  678. "disconn_array[i] = %d\n",
  679. i, rssi_delta, data->disconn_array[i]);
  680. }
  681. }
  682. /*
  683. * The above algorithm sometimes fails when the ucode
  684. * reports 0 for all chains. It's not clear why that
  685. * happens to start with, but it is then causing trouble
  686. * because this can make us enable more chains than the
  687. * hardware really has.
  688. *
  689. * To be safe, simply mask out any chains that we know
  690. * are not on the device.
  691. */
  692. active_chains &= priv->hw_params.valid_rx_ant;
  693. num_tx_chains = 0;
  694. for (i = 0; i < NUM_RX_CHAINS; i++) {
  695. /* loops on all the bits of
  696. * priv->hw_setting.valid_tx_ant */
  697. u8 ant_msk = (1 << i);
  698. if (!(priv->hw_params.valid_tx_ant & ant_msk))
  699. continue;
  700. num_tx_chains++;
  701. if (data->disconn_array[i] == 0)
  702. /* there is a Tx antenna connected */
  703. break;
  704. if (num_tx_chains == priv->hw_params.tx_chains_num &&
  705. data->disconn_array[i]) {
  706. /*
  707. * If all chains are disconnected
  708. * connect the first valid tx chain
  709. */
  710. first_chain =
  711. find_first_chain(priv->cfg->valid_tx_ant);
  712. data->disconn_array[first_chain] = 0;
  713. active_chains |= BIT(first_chain);
  714. IWL_DEBUG_CALIB(priv, "All Tx chains are disconnected W/A - declare %d as connected\n",
  715. first_chain);
  716. break;
  717. }
  718. }
  719. if (active_chains != priv->hw_params.valid_rx_ant &&
  720. active_chains != priv->chain_noise_data.active_chains)
  721. IWL_DEBUG_CALIB(priv,
  722. "Detected that not all antennas are connected! "
  723. "Connected: %#x, valid: %#x.\n",
  724. active_chains, priv->hw_params.valid_rx_ant);
  725. /* Save for use within RXON, TX, SCAN commands, etc. */
  726. priv->chain_noise_data.active_chains = active_chains;
  727. IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
  728. active_chains);
  729. /* Analyze noise for rx balance */
  730. average_noise[0] =
  731. ((data->chain_noise_a) / priv->cfg->chain_noise_num_beacons);
  732. average_noise[1] =
  733. ((data->chain_noise_b) / priv->cfg->chain_noise_num_beacons);
  734. average_noise[2] =
  735. ((data->chain_noise_c) / priv->cfg->chain_noise_num_beacons);
  736. for (i = 0; i < NUM_RX_CHAINS; i++) {
  737. if (!(data->disconn_array[i]) &&
  738. (average_noise[i] <= min_average_noise)) {
  739. /* This means that chain i is active and has
  740. * lower noise values so far: */
  741. min_average_noise = average_noise[i];
  742. min_average_noise_antenna_i = i;
  743. }
  744. }
  745. IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
  746. average_noise[0], average_noise[1],
  747. average_noise[2]);
  748. IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
  749. min_average_noise, min_average_noise_antenna_i);
  750. if (priv->cfg->ops->utils->gain_computation)
  751. priv->cfg->ops->utils->gain_computation(priv, average_noise,
  752. min_average_noise_antenna_i, min_average_noise,
  753. find_first_chain(priv->cfg->valid_rx_ant));
  754. /* Some power changes may have been made during the calibration.
  755. * Update and commit the RXON
  756. */
  757. if (priv->cfg->ops->lib->update_chain_flags)
  758. priv->cfg->ops->lib->update_chain_flags(priv);
  759. data->state = IWL_CHAIN_NOISE_DONE;
  760. iwl_power_update_mode(priv, false);
  761. }
  762. void iwl_reset_run_time_calib(struct iwl_priv *priv)
  763. {
  764. int i;
  765. memset(&(priv->sensitivity_data), 0,
  766. sizeof(struct iwl_sensitivity_data));
  767. memset(&(priv->chain_noise_data), 0,
  768. sizeof(struct iwl_chain_noise_data));
  769. for (i = 0; i < NUM_RX_CHAINS; i++)
  770. priv->chain_noise_data.delta_gain_code[i] =
  771. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  772. /* Ask for statistics now, the uCode will send notification
  773. * periodically after association */
  774. iwl_send_statistics_request(priv, CMD_ASYNC, true);
  775. }