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