calib.c 36 KB

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