4965-calib.c 30 KB

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