blob: 400eb312b5554064826ab681e5caaad4a102c4b9 [file] [log] [blame]
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001/******************************************************************************
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 *
Reinette Chatre1f447802010-01-15 13:43:41 -08008 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07009 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080028 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070029 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Reinette Chatre1f447802010-01-15 13:43:41 -080033 * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070034 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070064#include <net/mac80211.h>
65
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070066#include "iwl-dev.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070067#include "iwl-core.h"
68#include "iwl-calib.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070069
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080070/*****************************************************************************
71 * INIT calibrations framework
72 *****************************************************************************/
73
Winkler, Tomas34c22cf2008-12-17 16:52:27 +080074struct 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
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080083int iwl_send_calib_results(struct iwl_priv *priv)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080084{
85 int ret = 0;
86 int i = 0;
87
88 struct iwl_host_cmd hcmd = {
89 .id = REPLY_PHY_CALIBRATION_CMD,
Johannes Bergc2acea82009-07-24 11:13:05 -070090 .flags = CMD_SIZE_HUGE,
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080091 };
92
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080093 for (i = 0; i < IWL_CALIB_MAX; i++) {
94 if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
95 priv->calib_results[i].buf) {
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080096 hcmd.len = priv->calib_results[i].buf_len;
97 hcmd.data = priv->calib_results[i].buf;
98 ret = iwl_send_cmd_sync(priv, &hcmd);
Wey-Yi Guy936e8a72010-06-14 17:09:56 -070099 if (ret) {
100 IWL_ERR(priv, "Error %d iteration %d\n",
101 ret, i);
102 break;
103 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800104 }
Tomas Winklerbe5d56e2008-10-08 09:37:27 +0800105 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800106
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800107 return ret;
108}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800109
110int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
111{
112 if (res->buf_len != len) {
113 kfree(res->buf);
114 res->buf = kzalloc(len, GFP_ATOMIC);
115 }
116 if (unlikely(res->buf == NULL))
117 return -ENOMEM;
118
119 res->buf_len = len;
120 memcpy(res->buf, buf, len);
121 return 0;
122}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800123
124void iwl_calib_free_results(struct iwl_priv *priv)
125{
126 int i;
127
128 for (i = 0; i < IWL_CALIB_MAX; i++) {
129 kfree(priv->calib_results[i].buf);
130 priv->calib_results[i].buf = NULL;
131 priv->calib_results[i].buf_len = 0;
132 }
133}
134
135/*****************************************************************************
136 * RUNTIME calibrations framework
137 *****************************************************************************/
138
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700139/* "false alarms" are signals that our DSP tries to lock onto,
140 * but then determines that they are either noise, or transmissions
141 * from a distant wireless network (also "noise", really) that get
142 * "stepped on" by stronger transmissions within our own network.
143 * This algorithm attempts to set a sensitivity level that is high
144 * enough to receive all of our own network traffic, but not so
145 * high that our DSP gets too busy trying to lock onto non-network
146 * activity/noise. */
147static int iwl_sens_energy_cck(struct iwl_priv *priv,
148 u32 norm_fa,
149 u32 rx_enable_time,
150 struct statistics_general_data *rx_info)
151{
152 u32 max_nrg_cck = 0;
153 int i = 0;
154 u8 max_silence_rssi = 0;
155 u32 silence_ref = 0;
156 u8 silence_rssi_a = 0;
157 u8 silence_rssi_b = 0;
158 u8 silence_rssi_c = 0;
159 u32 val;
160
161 /* "false_alarms" values below are cross-multiplications to assess the
162 * numbers of false alarms within the measured period of actual Rx
163 * (Rx is off when we're txing), vs the min/max expected false alarms
164 * (some should be expected if rx is sensitive enough) in a
165 * hypothetical listening period of 200 time units (TU), 204.8 msec:
166 *
167 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
168 *
169 * */
170 u32 false_alarms = norm_fa * 200 * 1024;
171 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
172 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
173 struct iwl_sensitivity_data *data = NULL;
174 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
175
176 data = &(priv->sensitivity_data);
177
178 data->nrg_auto_corr_silence_diff = 0;
179
180 /* Find max silence rssi among all 3 receivers.
181 * This is background noise, which may include transmissions from other
182 * networks, measured during silence before our network's beacon */
183 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
184 ALL_BAND_FILTER) >> 8);
185 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
186 ALL_BAND_FILTER) >> 8);
187 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
188 ALL_BAND_FILTER) >> 8);
189
190 val = max(silence_rssi_b, silence_rssi_c);
191 max_silence_rssi = max(silence_rssi_a, (u8) val);
192
193 /* Store silence rssi in 20-beacon history table */
194 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
195 data->nrg_silence_idx++;
196 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
197 data->nrg_silence_idx = 0;
198
199 /* Find max silence rssi across 20 beacon history */
200 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
201 val = data->nrg_silence_rssi[i];
202 silence_ref = max(silence_ref, val);
203 }
Tomas Winklere1623442009-01-27 14:27:56 -0800204 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700205 silence_rssi_a, silence_rssi_b, silence_rssi_c,
206 silence_ref);
207
208 /* Find max rx energy (min value!) among all 3 receivers,
209 * measured during beacon frame.
210 * Save it in 10-beacon history table. */
211 i = data->nrg_energy_idx;
212 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
213 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
214
215 data->nrg_energy_idx++;
216 if (data->nrg_energy_idx >= 10)
217 data->nrg_energy_idx = 0;
218
219 /* Find min rx energy (max value) across 10 beacon history.
220 * This is the minimum signal level that we want to receive well.
221 * Add backoff (margin so we don't miss slightly lower energy frames).
222 * This establishes an upper bound (min value) for energy threshold. */
223 max_nrg_cck = data->nrg_value[0];
224 for (i = 1; i < 10; i++)
225 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
226 max_nrg_cck += 6;
227
Tomas Winklere1623442009-01-27 14:27:56 -0800228 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700229 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
230 rx_info->beacon_energy_c, max_nrg_cck - 6);
231
232 /* Count number of consecutive beacons with fewer-than-desired
233 * false alarms. */
234 if (false_alarms < min_false_alarms)
235 data->num_in_cck_no_fa++;
236 else
237 data->num_in_cck_no_fa = 0;
Tomas Winklere1623442009-01-27 14:27:56 -0800238 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700239 data->num_in_cck_no_fa);
240
241 /* If we got too many false alarms this time, reduce sensitivity */
242 if ((false_alarms > max_false_alarms) &&
243 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800244 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700245 false_alarms, max_false_alarms);
Tomas Winklere1623442009-01-27 14:27:56 -0800246 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700247 data->nrg_curr_state = IWL_FA_TOO_MANY;
248 /* Store for "fewer than desired" on later beacon */
249 data->nrg_silence_ref = silence_ref;
250
251 /* increase energy threshold (reduce nrg value)
252 * to decrease sensitivity */
Wey-Yi Guyfe6efb42009-06-12 13:22:54 -0700253 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700254 /* Else if we got fewer than desired, increase sensitivity */
255 } else if (false_alarms < min_false_alarms) {
256 data->nrg_curr_state = IWL_FA_TOO_FEW;
257
258 /* Compare silence level with silence level for most recent
259 * healthy number or too many false alarms */
260 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
261 (s32)silence_ref;
262
Tomas Winklere1623442009-01-27 14:27:56 -0800263 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700264 false_alarms, min_false_alarms,
265 data->nrg_auto_corr_silence_diff);
266
267 /* Increase value to increase sensitivity, but only if:
268 * 1a) previous beacon did *not* have *too many* false alarms
269 * 1b) AND there's a significant difference in Rx levels
270 * from a previous beacon with too many, or healthy # FAs
271 * OR 2) We've seen a lot of beacons (100) with too few
272 * false alarms */
273 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
274 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
275 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
276
Tomas Winklere1623442009-01-27 14:27:56 -0800277 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700278 /* Increase nrg value to increase sensitivity */
279 val = data->nrg_th_cck + NRG_STEP_CCK;
280 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
281 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800282 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700283 }
284
285 /* Else we got a healthy number of false alarms, keep status quo */
286 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800287 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700288 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
289
290 /* Store for use in "fewer than desired" with later beacon */
291 data->nrg_silence_ref = silence_ref;
292
293 /* If previous beacon had too many false alarms,
294 * give it some extra margin by reducing sensitivity again
295 * (but don't go below measured energy of desired Rx) */
296 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
Tomas Winklere1623442009-01-27 14:27:56 -0800297 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700298 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
299 data->nrg_th_cck -= NRG_MARGIN;
300 else
301 data->nrg_th_cck = max_nrg_cck;
302 }
303 }
304
305 /* Make sure the energy threshold does not go above the measured
306 * energy of the desired Rx signals (reduced by backoff margin),
307 * or else we might start missing Rx frames.
308 * Lower value is higher energy, so we use max()!
309 */
310 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
Tomas Winklere1623442009-01-27 14:27:56 -0800311 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700312
313 data->nrg_prev_state = data->nrg_curr_state;
314
315 /* Auto-correlation CCK algorithm */
316 if (false_alarms > min_false_alarms) {
317
318 /* increase auto_corr values to decrease sensitivity
319 * so the DSP won't be disturbed by the noise
320 */
321 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
322 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
323 else {
324 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
325 data->auto_corr_cck =
326 min((u32)ranges->auto_corr_max_cck, val);
327 }
328 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
329 data->auto_corr_cck_mrc =
330 min((u32)ranges->auto_corr_max_cck_mrc, val);
331 } else if ((false_alarms < min_false_alarms) &&
332 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
333 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
334
335 /* Decrease auto_corr values to increase sensitivity */
336 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
337 data->auto_corr_cck =
338 max((u32)ranges->auto_corr_min_cck, val);
339 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
340 data->auto_corr_cck_mrc =
341 max((u32)ranges->auto_corr_min_cck_mrc, val);
342 }
343
344 return 0;
345}
346
347
348static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
349 u32 norm_fa,
350 u32 rx_enable_time)
351{
352 u32 val;
353 u32 false_alarms = norm_fa * 200 * 1024;
354 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
355 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
356 struct iwl_sensitivity_data *data = NULL;
357 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
358
359 data = &(priv->sensitivity_data);
360
361 /* If we got too many false alarms this time, reduce sensitivity */
362 if (false_alarms > max_false_alarms) {
363
Tomas Winklere1623442009-01-27 14:27:56 -0800364 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700365 false_alarms, max_false_alarms);
366
367 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
368 data->auto_corr_ofdm =
369 min((u32)ranges->auto_corr_max_ofdm, val);
370
371 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
372 data->auto_corr_ofdm_mrc =
373 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
374
375 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
376 data->auto_corr_ofdm_x1 =
377 min((u32)ranges->auto_corr_max_ofdm_x1, val);
378
379 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
380 data->auto_corr_ofdm_mrc_x1 =
381 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
382 }
383
384 /* Else if we got fewer than desired, increase sensitivity */
385 else if (false_alarms < min_false_alarms) {
386
Tomas Winklere1623442009-01-27 14:27:56 -0800387 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700388 false_alarms, min_false_alarms);
389
390 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
391 data->auto_corr_ofdm =
392 max((u32)ranges->auto_corr_min_ofdm, val);
393
394 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
395 data->auto_corr_ofdm_mrc =
396 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
397
398 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
399 data->auto_corr_ofdm_x1 =
400 max((u32)ranges->auto_corr_min_ofdm_x1, val);
401
402 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
403 data->auto_corr_ofdm_mrc_x1 =
404 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
405 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800406 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700407 min_false_alarms, false_alarms, max_false_alarms);
408 }
409 return 0;
410}
411
412/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
413static int iwl_sensitivity_write(struct iwl_priv *priv)
414{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700415 struct iwl_sensitivity_cmd cmd ;
416 struct iwl_sensitivity_data *data = NULL;
417 struct iwl_host_cmd cmd_out = {
418 .id = SENSITIVITY_CMD,
419 .len = sizeof(struct iwl_sensitivity_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -0700420 .flags = CMD_ASYNC,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700421 .data = &cmd,
422 };
423
424 data = &(priv->sensitivity_data);
425
426 memset(&cmd, 0, sizeof(cmd));
427
428 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
429 cpu_to_le16((u16)data->auto_corr_ofdm);
430 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
431 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
432 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
433 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
434 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
435 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
436
437 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
438 cpu_to_le16((u16)data->auto_corr_cck);
439 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
440 cpu_to_le16((u16)data->auto_corr_cck_mrc);
441
442 cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
443 cpu_to_le16((u16)data->nrg_th_cck);
444 cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
445 cpu_to_le16((u16)data->nrg_th_ofdm);
446
447 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700448 cpu_to_le16(data->barker_corr_th_min);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700449 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700450 cpu_to_le16(data->barker_corr_th_min_mrc);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700451 cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700452 cpu_to_le16(data->nrg_th_cca);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700453
Tomas Winklere1623442009-01-27 14:27:56 -0800454 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700455 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
456 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
457 data->nrg_th_ofdm);
458
Tomas Winklere1623442009-01-27 14:27:56 -0800459 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700460 data->auto_corr_cck, data->auto_corr_cck_mrc,
461 data->nrg_th_cck);
462
463 /* Update uCode's "work" table, and copy it to DSP */
464 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
465
466 /* Don't send command to uCode if nothing has changed */
467 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
468 sizeof(u16)*HD_TABLE_SIZE)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800469 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700470 return 0;
471 }
472
473 /* Copy table for comparison next time */
474 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
475 sizeof(u16)*HD_TABLE_SIZE);
476
Wey-Yi Guy4309af272009-12-14 14:12:21 -0800477 return iwl_send_cmd(priv, &cmd_out);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700478}
479
480void iwl_init_sensitivity(struct iwl_priv *priv)
481{
482 int ret = 0;
483 int i;
484 struct iwl_sensitivity_data *data = NULL;
485 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
486
Tomas Winkler445c2df2008-05-15 13:54:16 +0800487 if (priv->disable_sens_cal)
488 return;
489
Tomas Winklere1623442009-01-27 14:27:56 -0800490 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700491
492 /* Clear driver's sensitivity algo data */
493 data = &(priv->sensitivity_data);
494
495 if (ranges == NULL)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700496 return;
497
498 memset(data, 0, sizeof(struct iwl_sensitivity_data));
499
500 data->num_in_cck_no_fa = 0;
501 data->nrg_curr_state = IWL_FA_TOO_MANY;
502 data->nrg_prev_state = IWL_FA_TOO_MANY;
503 data->nrg_silence_ref = 0;
504 data->nrg_silence_idx = 0;
505 data->nrg_energy_idx = 0;
506
507 for (i = 0; i < 10; i++)
508 data->nrg_value[i] = 0;
509
510 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
511 data->nrg_silence_rssi[i] = 0;
512
Wey-Yi Guyf3a2a422009-09-11 10:38:11 -0700513 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700514 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
515 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
516 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
517 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
518 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
519 data->nrg_th_cck = ranges->nrg_th_cck;
520 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700521 data->barker_corr_th_min = ranges->barker_corr_th_min;
522 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
523 data->nrg_th_cca = ranges->nrg_th_cca;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700524
525 data->last_bad_plcp_cnt_ofdm = 0;
526 data->last_fa_cnt_ofdm = 0;
527 data->last_bad_plcp_cnt_cck = 0;
528 data->last_fa_cnt_cck = 0;
529
530 ret |= iwl_sensitivity_write(priv);
Tomas Winklere1623442009-01-27 14:27:56 -0800531 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700532}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700533
534void iwl_sensitivity_calibration(struct iwl_priv *priv,
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +0800535 struct iwl_notif_statistics *resp)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700536{
537 u32 rx_enable_time;
538 u32 fa_cck;
539 u32 fa_ofdm;
540 u32 bad_plcp_cck;
541 u32 bad_plcp_ofdm;
542 u32 norm_fa_ofdm;
543 u32 norm_fa_cck;
544 struct iwl_sensitivity_data *data = NULL;
545 struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
546 struct statistics_rx *statistics = &(resp->rx);
547 unsigned long flags;
548 struct statistics_general_data statis;
549
Tomas Winkler445c2df2008-05-15 13:54:16 +0800550 if (priv->disable_sens_cal)
551 return;
552
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700553 data = &(priv->sensitivity_data);
554
555 if (!iwl_is_associated(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800556 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700557 return;
558 }
559
560 spin_lock_irqsave(&priv->lock, flags);
561 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800562 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700563 spin_unlock_irqrestore(&priv->lock, flags);
564 return;
565 }
566
567 /* Extract Statistics: */
568 rx_enable_time = le32_to_cpu(rx_info->channel_load);
569 fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
570 fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
571 bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
572 bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
573
574 statis.beacon_silence_rssi_a =
575 le32_to_cpu(statistics->general.beacon_silence_rssi_a);
576 statis.beacon_silence_rssi_b =
577 le32_to_cpu(statistics->general.beacon_silence_rssi_b);
578 statis.beacon_silence_rssi_c =
579 le32_to_cpu(statistics->general.beacon_silence_rssi_c);
580 statis.beacon_energy_a =
581 le32_to_cpu(statistics->general.beacon_energy_a);
582 statis.beacon_energy_b =
583 le32_to_cpu(statistics->general.beacon_energy_b);
584 statis.beacon_energy_c =
585 le32_to_cpu(statistics->general.beacon_energy_c);
586
587 spin_unlock_irqrestore(&priv->lock, flags);
588
Tomas Winklere1623442009-01-27 14:27:56 -0800589 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700590
591 if (!rx_enable_time) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700592 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700593 return;
594 }
595
596 /* These statistics increase monotonically, and do not reset
597 * at each beacon. Calculate difference from last value, or just
598 * use the new statistics value if it has reset or wrapped around. */
599 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
600 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
601 else {
602 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
603 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
604 }
605
606 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
607 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
608 else {
609 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
610 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
611 }
612
613 if (data->last_fa_cnt_ofdm > fa_ofdm)
614 data->last_fa_cnt_ofdm = fa_ofdm;
615 else {
616 fa_ofdm -= data->last_fa_cnt_ofdm;
617 data->last_fa_cnt_ofdm += fa_ofdm;
618 }
619
620 if (data->last_fa_cnt_cck > fa_cck)
621 data->last_fa_cnt_cck = fa_cck;
622 else {
623 fa_cck -= data->last_fa_cnt_cck;
624 data->last_fa_cnt_cck += fa_cck;
625 }
626
627 /* Total aborted signal locks */
628 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
629 norm_fa_cck = fa_cck + bad_plcp_cck;
630
Tomas Winklere1623442009-01-27 14:27:56 -0800631 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700632 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
633
634 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
635 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
636 iwl_sensitivity_write(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700637}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700638
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700639static inline u8 find_first_chain(u8 mask)
640{
641 if (mask & ANT_A)
642 return CHAIN_A;
643 if (mask & ANT_B)
644 return CHAIN_B;
645 return CHAIN_C;
646}
647
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700648/*
649 * Accumulate 20 beacons of signal and noise statistics for each of
650 * 3 receivers/antennas/rx-chains, then figure out:
651 * 1) Which antennas are connected.
652 * 2) Differential rx gain settings to balance the 3 receivers.
653 */
654void iwl_chain_noise_calibration(struct iwl_priv *priv,
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +0800655 struct iwl_notif_statistics *stat_resp)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700656{
657 struct iwl_chain_noise_data *data = NULL;
658
659 u32 chain_noise_a;
660 u32 chain_noise_b;
661 u32 chain_noise_c;
662 u32 chain_sig_a;
663 u32 chain_sig_b;
664 u32 chain_sig_c;
665 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
666 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
667 u32 max_average_sig;
668 u16 max_average_sig_antenna_i;
669 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
670 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
671 u16 i = 0;
672 u16 rxon_chnum = INITIALIZATION_VALUE;
673 u16 stat_chnum = INITIALIZATION_VALUE;
674 u8 rxon_band24;
675 u8 stat_band24;
676 u32 active_chains = 0;
677 u8 num_tx_chains;
678 unsigned long flags;
679 struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700680 u8 first_chain;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700681
Tomas Winkler445c2df2008-05-15 13:54:16 +0800682 if (priv->disable_chain_noise_cal)
683 return;
684
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700685 data = &(priv->chain_noise_data);
686
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700687 /*
688 * Accumulate just the first "chain_noise_num_beacons" after
689 * the first association, then we're done forever.
690 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700691 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
692 if (data->state == IWL_CHAIN_NOISE_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -0800693 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700694 return;
695 }
696
697 spin_lock_irqsave(&priv->lock, flags);
698 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800699 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700700 spin_unlock_irqrestore(&priv->lock, flags);
701 return;
702 }
703
704 rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
705 rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
706 stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
707 stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
708
709 /* Make sure we accumulate data for just the associated channel
710 * (even if scanning). */
711 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800712 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700713 rxon_chnum, rxon_band24);
714 spin_unlock_irqrestore(&priv->lock, flags);
715 return;
716 }
717
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700718 /*
719 * Accumulate beacon statistics values across
720 * "chain_noise_num_beacons"
721 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700722 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
723 IN_BAND_FILTER;
724 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
725 IN_BAND_FILTER;
726 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
727 IN_BAND_FILTER;
728
729 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
730 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
731 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
732
733 spin_unlock_irqrestore(&priv->lock, flags);
734
735 data->beacon_count++;
736
737 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
738 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
739 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
740
741 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
742 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
743 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
744
Tomas Winklere1623442009-01-27 14:27:56 -0800745 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700746 rxon_chnum, rxon_band24, data->beacon_count);
Tomas Winklere1623442009-01-27 14:27:56 -0800747 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700748 chain_sig_a, chain_sig_b, chain_sig_c);
Tomas Winklere1623442009-01-27 14:27:56 -0800749 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700750 chain_noise_a, chain_noise_b, chain_noise_c);
751
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700752 /* If this is the "chain_noise_num_beacons", determine:
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700753 * 1) Disconnected antennas (using signal strengths)
754 * 2) Differential gain (using silence noise) to balance receivers */
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700755 if (data->beacon_count != priv->cfg->chain_noise_num_beacons)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700756 return;
757
758 /* Analyze signal for disconnected antenna */
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700759 average_sig[0] =
760 (data->chain_signal_a) / priv->cfg->chain_noise_num_beacons;
761 average_sig[1] =
762 (data->chain_signal_b) / priv->cfg->chain_noise_num_beacons;
763 average_sig[2] =
764 (data->chain_signal_c) / priv->cfg->chain_noise_num_beacons;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700765
766 if (average_sig[0] >= average_sig[1]) {
767 max_average_sig = average_sig[0];
768 max_average_sig_antenna_i = 0;
769 active_chains = (1 << max_average_sig_antenna_i);
770 } else {
771 max_average_sig = average_sig[1];
772 max_average_sig_antenna_i = 1;
773 active_chains = (1 << max_average_sig_antenna_i);
774 }
775
776 if (average_sig[2] >= max_average_sig) {
777 max_average_sig = average_sig[2];
778 max_average_sig_antenna_i = 2;
779 active_chains = (1 << max_average_sig_antenna_i);
780 }
781
Tomas Winklere1623442009-01-27 14:27:56 -0800782 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700783 average_sig[0], average_sig[1], average_sig[2]);
Tomas Winklere1623442009-01-27 14:27:56 -0800784 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700785 max_average_sig, max_average_sig_antenna_i);
786
787 /* Compare signal strengths for all 3 receivers. */
788 for (i = 0; i < NUM_RX_CHAINS; i++) {
789 if (i != max_average_sig_antenna_i) {
790 s32 rssi_delta = (max_average_sig - average_sig[i]);
791
792 /* If signal is very weak, compared with
793 * strongest, mark it as disconnected. */
794 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
795 data->disconn_array[i] = 1;
796 else
797 active_chains |= (1 << i);
Tomas Winklere1623442009-01-27 14:27:56 -0800798 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700799 "disconn_array[i] = %d\n",
800 i, rssi_delta, data->disconn_array[i]);
801 }
802 }
803
Johannes Berg8b9fce72010-04-01 11:24:23 -0700804 /*
805 * The above algorithm sometimes fails when the ucode
806 * reports 0 for all chains. It's not clear why that
807 * happens to start with, but it is then causing trouble
808 * because this can make us enable more chains than the
809 * hardware really has.
810 *
811 * To be safe, simply mask out any chains that we know
812 * are not on the device.
813 */
814 active_chains &= priv->hw_params.valid_rx_ant;
815
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700816 num_tx_chains = 0;
817 for (i = 0; i < NUM_RX_CHAINS; i++) {
818 /* loops on all the bits of
819 * priv->hw_setting.valid_tx_ant */
820 u8 ant_msk = (1 << i);
821 if (!(priv->hw_params.valid_tx_ant & ant_msk))
822 continue;
823
824 num_tx_chains++;
825 if (data->disconn_array[i] == 0)
826 /* there is a Tx antenna connected */
827 break;
828 if (num_tx_chains == priv->hw_params.tx_chains_num &&
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700829 data->disconn_array[i]) {
830 /*
831 * If all chains are disconnected
832 * connect the first valid tx chain
833 */
834 first_chain =
835 find_first_chain(priv->cfg->valid_tx_ant);
836 data->disconn_array[first_chain] = 0;
837 active_chains |= BIT(first_chain);
838 IWL_DEBUG_CALIB(priv, "All Tx chains are disconnected W/A - declare %d as connected\n",
839 first_chain);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700840 break;
841 }
842 }
843
Johannes Bergaa9746a2010-06-09 01:46:32 -0700844 if (active_chains != priv->hw_params.valid_rx_ant &&
845 active_chains != priv->chain_noise_data.active_chains)
846 IWL_WARN(priv,
847 "Detected that not all antennas are connected! "
848 "Connected: %#x, valid: %#x.\n",
849 active_chains, priv->hw_params.valid_rx_ant);
850
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800851 /* Save for use within RXON, TX, SCAN commands, etc. */
852 priv->chain_noise_data.active_chains = active_chains;
Tomas Winklere1623442009-01-27 14:27:56 -0800853 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700854 active_chains);
855
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700856 /* Analyze noise for rx balance */
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700857 average_noise[0] =
858 ((data->chain_noise_a) / priv->cfg->chain_noise_num_beacons);
859 average_noise[1] =
860 ((data->chain_noise_b) / priv->cfg->chain_noise_num_beacons);
861 average_noise[2] =
862 ((data->chain_noise_c) / priv->cfg->chain_noise_num_beacons);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700863
864 for (i = 0; i < NUM_RX_CHAINS; i++) {
865 if (!(data->disconn_array[i]) &&
866 (average_noise[i] <= min_average_noise)) {
867 /* This means that chain i is active and has
868 * lower noise values so far: */
869 min_average_noise = average_noise[i];
870 min_average_noise_antenna_i = i;
871 }
872 }
873
Tomas Winklere1623442009-01-27 14:27:56 -0800874 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700875 average_noise[0], average_noise[1],
876 average_noise[2]);
877
Tomas Winklere1623442009-01-27 14:27:56 -0800878 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700879 min_average_noise, min_average_noise_antenna_i);
880
Jay Sternberg29f35c12009-01-29 11:09:16 -0800881 if (priv->cfg->ops->utils->gain_computation)
882 priv->cfg->ops->utils->gain_computation(priv, average_noise,
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700883 min_average_noise_antenna_i, min_average_noise,
884 find_first_chain(priv->cfg->valid_rx_ant));
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800885
886 /* Some power changes may have been made during the calibration.
887 * Update and commit the RXON
888 */
889 if (priv->cfg->ops->lib->update_chain_flags)
890 priv->cfg->ops->lib->update_chain_flags(priv);
891
892 data->state = IWL_CHAIN_NOISE_DONE;
Johannes Berge312c242009-08-07 15:41:51 -0700893 iwl_power_update_mode(priv, false);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700894}
Tomas Winkler4a4a9e82008-05-29 16:34:54 +0800895
896void iwl_reset_run_time_calib(struct iwl_priv *priv)
897{
898 int i;
899 memset(&(priv->sensitivity_data), 0,
900 sizeof(struct iwl_sensitivity_data));
901 memset(&(priv->chain_noise_data), 0,
902 sizeof(struct iwl_chain_noise_data));
903 for (i = 0; i < NUM_RX_CHAINS; i++)
904 priv->chain_noise_data.delta_gain_code[i] =
905 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
906
907 /* Ask for statistics now, the uCode will send notification
908 * periodically after association */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800909 iwl_send_statistics_request(priv, CMD_ASYNC, true);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +0800910}