iwl-agn-calib.c 33 KB

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