iwl-agn-calib.c 36 KB

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