1 /******************************************************************************
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.
8 * Copyright(c) 2008 Intel Corporation. All rights reserved.
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.
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.
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,
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
27 * Contact Information:
28 * Tomas Winkler <tomas.winkler@intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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
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.
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 *****************************************************************************/
63 #include <net/mac80211.h>
67 #include "iwl-calib.h"
69 /*****************************************************************************
70 * INIT calibrations framework
71 *****************************************************************************/
73 int iwl_send_calib_results(struct iwl_priv *priv)
78 struct iwl_host_cmd hcmd = {
79 .id = REPLY_PHY_CALIBRATION_CMD,
80 .meta.flags = CMD_SIZE_HUGE,
83 for (i = 0; i < IWL_CALIB_MAX; i++) {
84 if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
85 priv->calib_results[i].buf) {
86 hcmd.len = priv->calib_results[i].buf_len;
87 hcmd.data = priv->calib_results[i].buf;
88 ret = iwl_send_cmd_sync(priv, &hcmd);
96 IWL_ERROR("Error %d iteration %d\n", ret, i);
99 EXPORT_SYMBOL(iwl_send_calib_results);
101 int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
103 if (res->buf_len != len) {
105 res->buf = kzalloc(len, GFP_ATOMIC);
107 if (unlikely(res->buf == NULL))
111 memcpy(res->buf, buf, len);
114 EXPORT_SYMBOL(iwl_calib_set);
116 void iwl_calib_free_results(struct iwl_priv *priv)
120 for (i = 0; i < IWL_CALIB_MAX; i++) {
121 kfree(priv->calib_results[i].buf);
122 priv->calib_results[i].buf = NULL;
123 priv->calib_results[i].buf_len = 0;
127 /*****************************************************************************
128 * RUNTIME calibrations framework
129 *****************************************************************************/
131 /* "false alarms" are signals that our DSP tries to lock onto,
132 * but then determines that they are either noise, or transmissions
133 * from a distant wireless network (also "noise", really) that get
134 * "stepped on" by stronger transmissions within our own network.
135 * This algorithm attempts to set a sensitivity level that is high
136 * enough to receive all of our own network traffic, but not so
137 * high that our DSP gets too busy trying to lock onto non-network
139 static int iwl_sens_energy_cck(struct iwl_priv *priv,
142 struct statistics_general_data *rx_info)
146 u8 max_silence_rssi = 0;
148 u8 silence_rssi_a = 0;
149 u8 silence_rssi_b = 0;
150 u8 silence_rssi_c = 0;
153 /* "false_alarms" values below are cross-multiplications to assess the
154 * numbers of false alarms within the measured period of actual Rx
155 * (Rx is off when we're txing), vs the min/max expected false alarms
156 * (some should be expected if rx is sensitive enough) in a
157 * hypothetical listening period of 200 time units (TU), 204.8 msec:
159 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
162 u32 false_alarms = norm_fa * 200 * 1024;
163 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
164 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
165 struct iwl_sensitivity_data *data = NULL;
166 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
168 data = &(priv->sensitivity_data);
170 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);
182 val = max(silence_rssi_b, silence_rssi_c);
183 max_silence_rssi = max(silence_rssi_a, (u8) val);
185 /* Store silence rssi in 20-beacon history table */
186 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
187 data->nrg_silence_idx++;
188 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
189 data->nrg_silence_idx = 0;
191 /* Find max silence rssi across 20 beacon history */
192 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
193 val = data->nrg_silence_rssi[i];
194 silence_ref = max(silence_ref, val);
196 IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
197 silence_rssi_a, silence_rssi_b, silence_rssi_c,
200 /* Find max rx energy (min value!) among all 3 receivers,
201 * measured during beacon frame.
202 * Save it in 10-beacon history table. */
203 i = data->nrg_energy_idx;
204 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
205 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
207 data->nrg_energy_idx++;
208 if (data->nrg_energy_idx >= 10)
209 data->nrg_energy_idx = 0;
211 /* Find min rx energy (max value) across 10 beacon history.
212 * This is the minimum signal level that we want to receive well.
213 * Add backoff (margin so we don't miss slightly lower energy frames).
214 * This establishes an upper bound (min value) for energy threshold. */
215 max_nrg_cck = data->nrg_value[0];
216 for (i = 1; i < 10; i++)
217 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
220 IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
221 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
222 rx_info->beacon_energy_c, max_nrg_cck - 6);
224 /* Count number of consecutive beacons with fewer-than-desired
226 if (false_alarms < min_false_alarms)
227 data->num_in_cck_no_fa++;
229 data->num_in_cck_no_fa = 0;
230 IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
231 data->num_in_cck_no_fa);
233 /* If we got too many false alarms this time, reduce sensitivity */
234 if ((false_alarms > max_false_alarms) &&
235 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
236 IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
237 false_alarms, max_false_alarms);
238 IWL_DEBUG_CALIB("... reducing sensitivity\n");
239 data->nrg_curr_state = IWL_FA_TOO_MANY;
240 /* Store for "fewer than desired" on later beacon */
241 data->nrg_silence_ref = silence_ref;
243 /* increase energy threshold (reduce nrg value)
244 * to decrease sensitivity */
245 if (data->nrg_th_cck >
246 (ranges->max_nrg_cck + NRG_STEP_CCK))
247 data->nrg_th_cck = data->nrg_th_cck
250 data->nrg_th_cck = ranges->max_nrg_cck;
251 /* Else if we got fewer than desired, increase sensitivity */
252 } else if (false_alarms < min_false_alarms) {
253 data->nrg_curr_state = IWL_FA_TOO_FEW;
255 /* Compare silence level with silence level for most recent
256 * healthy number or too many false alarms */
257 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
260 IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
261 false_alarms, min_false_alarms,
262 data->nrg_auto_corr_silence_diff);
264 /* Increase value to increase sensitivity, but only if:
265 * 1a) previous beacon did *not* have *too many* false alarms
266 * 1b) AND there's a significant difference in Rx levels
267 * from a previous beacon with too many, or healthy # FAs
268 * OR 2) We've seen a lot of beacons (100) with too few
270 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
271 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
272 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
274 IWL_DEBUG_CALIB("... increasing sensitivity\n");
275 /* Increase nrg value to increase sensitivity */
276 val = data->nrg_th_cck + NRG_STEP_CCK;
277 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
279 IWL_DEBUG_CALIB("... but not changing sensitivity\n");
282 /* Else we got a healthy number of false alarms, keep status quo */
284 IWL_DEBUG_CALIB(" FA in safe zone\n");
285 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
287 /* Store for use in "fewer than desired" with later beacon */
288 data->nrg_silence_ref = silence_ref;
290 /* If previous beacon had too many false alarms,
291 * give it some extra margin by reducing sensitivity again
292 * (but don't go below measured energy of desired Rx) */
293 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
294 IWL_DEBUG_CALIB("... increasing margin\n");
295 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
296 data->nrg_th_cck -= NRG_MARGIN;
298 data->nrg_th_cck = max_nrg_cck;
302 /* Make sure the energy threshold does not go above the measured
303 * energy of the desired Rx signals (reduced by backoff margin),
304 * or else we might start missing Rx frames.
305 * Lower value is higher energy, so we use max()!
307 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
308 IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
310 data->nrg_prev_state = data->nrg_curr_state;
312 /* Auto-correlation CCK algorithm */
313 if (false_alarms > min_false_alarms) {
315 /* increase auto_corr values to decrease sensitivity
316 * so the DSP won't be disturbed by the noise
318 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
319 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
321 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
322 data->auto_corr_cck =
323 min((u32)ranges->auto_corr_max_cck, val);
325 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
326 data->auto_corr_cck_mrc =
327 min((u32)ranges->auto_corr_max_cck_mrc, val);
328 } else if ((false_alarms < min_false_alarms) &&
329 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
330 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
332 /* Decrease auto_corr values to increase sensitivity */
333 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
334 data->auto_corr_cck =
335 max((u32)ranges->auto_corr_min_cck, val);
336 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
337 data->auto_corr_cck_mrc =
338 max((u32)ranges->auto_corr_min_cck_mrc, val);
345 static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
350 u32 false_alarms = norm_fa * 200 * 1024;
351 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
352 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
353 struct iwl_sensitivity_data *data = NULL;
354 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
356 data = &(priv->sensitivity_data);
358 /* If we got too many false alarms this time, reduce sensitivity */
359 if (false_alarms > max_false_alarms) {
361 IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
362 false_alarms, max_false_alarms);
364 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
365 data->auto_corr_ofdm =
366 min((u32)ranges->auto_corr_max_ofdm, val);
368 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
369 data->auto_corr_ofdm_mrc =
370 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
372 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
373 data->auto_corr_ofdm_x1 =
374 min((u32)ranges->auto_corr_max_ofdm_x1, val);
376 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
377 data->auto_corr_ofdm_mrc_x1 =
378 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
381 /* Else if we got fewer than desired, increase sensitivity */
382 else if (false_alarms < min_false_alarms) {
384 IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
385 false_alarms, min_false_alarms);
387 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
388 data->auto_corr_ofdm =
389 max((u32)ranges->auto_corr_min_ofdm, val);
391 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
392 data->auto_corr_ofdm_mrc =
393 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
395 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
396 data->auto_corr_ofdm_x1 =
397 max((u32)ranges->auto_corr_min_ofdm_x1, val);
399 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
400 data->auto_corr_ofdm_mrc_x1 =
401 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
403 IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
404 min_false_alarms, false_alarms, max_false_alarms);
409 /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
410 static int iwl_sensitivity_write(struct iwl_priv *priv)
413 struct iwl_sensitivity_cmd cmd ;
414 struct iwl_sensitivity_data *data = NULL;
415 struct iwl_host_cmd cmd_out = {
416 .id = SENSITIVITY_CMD,
417 .len = sizeof(struct iwl_sensitivity_cmd),
418 .meta.flags = CMD_ASYNC,
422 data = &(priv->sensitivity_data);
424 memset(&cmd, 0, sizeof(cmd));
426 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
427 cpu_to_le16((u16)data->auto_corr_ofdm);
428 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
429 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
430 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
431 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
432 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
433 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
435 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
436 cpu_to_le16((u16)data->auto_corr_cck);
437 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
438 cpu_to_le16((u16)data->auto_corr_cck_mrc);
440 cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
441 cpu_to_le16((u16)data->nrg_th_cck);
442 cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
443 cpu_to_le16((u16)data->nrg_th_ofdm);
445 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
446 __constant_cpu_to_le16(190);
447 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
448 __constant_cpu_to_le16(390);
449 cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
450 __constant_cpu_to_le16(62);
452 IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
453 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
454 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
457 IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
458 data->auto_corr_cck, data->auto_corr_cck_mrc,
461 /* Update uCode's "work" table, and copy it to DSP */
462 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
464 /* Don't send command to uCode if nothing has changed */
465 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
466 sizeof(u16)*HD_TABLE_SIZE)) {
467 IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
471 /* Copy table for comparison next time */
472 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
473 sizeof(u16)*HD_TABLE_SIZE);
475 ret = iwl_send_cmd(priv, &cmd_out);
477 IWL_ERROR("SENSITIVITY_CMD failed\n");
482 void iwl_init_sensitivity(struct iwl_priv *priv)
486 struct iwl_sensitivity_data *data = NULL;
487 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
489 if (priv->disable_sens_cal)
492 IWL_DEBUG_CALIB("Start iwl_init_sensitivity\n");
494 /* Clear driver's sensitivity algo data */
495 data = &(priv->sensitivity_data);
500 memset(data, 0, sizeof(struct iwl_sensitivity_data));
502 data->num_in_cck_no_fa = 0;
503 data->nrg_curr_state = IWL_FA_TOO_MANY;
504 data->nrg_prev_state = IWL_FA_TOO_MANY;
505 data->nrg_silence_ref = 0;
506 data->nrg_silence_idx = 0;
507 data->nrg_energy_idx = 0;
509 for (i = 0; i < 10; i++)
510 data->nrg_value[i] = 0;
512 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
513 data->nrg_silence_rssi[i] = 0;
515 data->auto_corr_ofdm = 90;
516 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
517 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
518 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
519 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
520 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
521 data->nrg_th_cck = ranges->nrg_th_cck;
522 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
524 data->last_bad_plcp_cnt_ofdm = 0;
525 data->last_fa_cnt_ofdm = 0;
526 data->last_bad_plcp_cnt_cck = 0;
527 data->last_fa_cnt_cck = 0;
529 ret |= iwl_sensitivity_write(priv);
530 IWL_DEBUG_CALIB("<<return 0x%X\n", ret);
532 EXPORT_SYMBOL(iwl_init_sensitivity);
534 void iwl_sensitivity_calibration(struct iwl_priv *priv,
535 struct iwl_notif_statistics *resp)
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);
548 struct statistics_general_data statis;
550 if (priv->disable_sens_cal)
553 data = &(priv->sensitivity_data);
555 if (!iwl_is_associated(priv)) {
556 IWL_DEBUG_CALIB("<< - not associated\n");
560 spin_lock_irqsave(&priv->lock, flags);
561 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
562 IWL_DEBUG_CALIB("<< invalid data.\n");
563 spin_unlock_irqrestore(&priv->lock, flags);
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);
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);
587 spin_unlock_irqrestore(&priv->lock, flags);
589 IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
591 if (!rx_enable_time) {
592 IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
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;
602 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
603 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
606 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
607 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
609 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
610 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
613 if (data->last_fa_cnt_ofdm > fa_ofdm)
614 data->last_fa_cnt_ofdm = fa_ofdm;
616 fa_ofdm -= data->last_fa_cnt_ofdm;
617 data->last_fa_cnt_ofdm += fa_ofdm;
620 if (data->last_fa_cnt_cck > fa_cck)
621 data->last_fa_cnt_cck = fa_cck;
623 fa_cck -= data->last_fa_cnt_cck;
624 data->last_fa_cnt_cck += fa_cck;
627 /* Total aborted signal locks */
628 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
629 norm_fa_cck = fa_cck + bad_plcp_cck;
631 IWL_DEBUG_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
632 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
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);
640 EXPORT_SYMBOL(iwl_sensitivity_calibration);
643 * Accumulate 20 beacons of signal and noise statistics for each of
644 * 3 receivers/antennas/rx-chains, then figure out:
645 * 1) Which antennas are connected.
646 * 2) Differential rx gain settings to balance the 3 receivers.
648 void iwl_chain_noise_calibration(struct iwl_priv *priv,
649 struct iwl_notif_statistics *stat_resp)
651 struct iwl_chain_noise_data *data = NULL;
659 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
660 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
662 u16 max_average_sig_antenna_i;
663 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
664 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
666 u16 rxon_chnum = INITIALIZATION_VALUE;
667 u16 stat_chnum = INITIALIZATION_VALUE;
670 u32 active_chains = 0;
673 struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
675 if (priv->disable_chain_noise_cal)
678 data = &(priv->chain_noise_data);
680 /* Accumulate just the first 20 beacons after the first association,
681 * then we're done forever. */
682 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
683 if (data->state == IWL_CHAIN_NOISE_ALIVE)
684 IWL_DEBUG_CALIB("Wait for noise calib reset\n");
688 spin_lock_irqsave(&priv->lock, flags);
689 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
690 IWL_DEBUG_CALIB(" << Interference data unavailable\n");
691 spin_unlock_irqrestore(&priv->lock, flags);
695 rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
696 rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
697 stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
698 stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
700 /* Make sure we accumulate data for just the associated channel
701 * (even if scanning). */
702 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
703 IWL_DEBUG_CALIB("Stats not from chan=%d, band24=%d\n",
704 rxon_chnum, rxon_band24);
705 spin_unlock_irqrestore(&priv->lock, flags);
709 /* Accumulate beacon statistics values across 20 beacons */
710 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
712 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
714 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
717 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
718 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
719 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
721 spin_unlock_irqrestore(&priv->lock, flags);
723 data->beacon_count++;
725 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
726 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
727 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
729 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
730 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
731 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
733 IWL_DEBUG_CALIB("chan=%d, band24=%d, beacon=%d\n",
734 rxon_chnum, rxon_band24, data->beacon_count);
735 IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
736 chain_sig_a, chain_sig_b, chain_sig_c);
737 IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
738 chain_noise_a, chain_noise_b, chain_noise_c);
740 /* If this is the 20th beacon, determine:
741 * 1) Disconnected antennas (using signal strengths)
742 * 2) Differential gain (using silence noise) to balance receivers */
743 if (data->beacon_count != CAL_NUM_OF_BEACONS)
746 /* Analyze signal for disconnected antenna */
747 average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
748 average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
749 average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
751 if (average_sig[0] >= average_sig[1]) {
752 max_average_sig = average_sig[0];
753 max_average_sig_antenna_i = 0;
754 active_chains = (1 << max_average_sig_antenna_i);
756 max_average_sig = average_sig[1];
757 max_average_sig_antenna_i = 1;
758 active_chains = (1 << max_average_sig_antenna_i);
761 if (average_sig[2] >= max_average_sig) {
762 max_average_sig = average_sig[2];
763 max_average_sig_antenna_i = 2;
764 active_chains = (1 << max_average_sig_antenna_i);
767 IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
768 average_sig[0], average_sig[1], average_sig[2]);
769 IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
770 max_average_sig, max_average_sig_antenna_i);
772 /* Compare signal strengths for all 3 receivers. */
773 for (i = 0; i < NUM_RX_CHAINS; i++) {
774 if (i != max_average_sig_antenna_i) {
775 s32 rssi_delta = (max_average_sig - average_sig[i]);
777 /* If signal is very weak, compared with
778 * strongest, mark it as disconnected. */
779 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
780 data->disconn_array[i] = 1;
782 active_chains |= (1 << i);
783 IWL_DEBUG_CALIB("i = %d rssiDelta = %d "
784 "disconn_array[i] = %d\n",
785 i, rssi_delta, data->disconn_array[i]);
790 for (i = 0; i < NUM_RX_CHAINS; i++) {
791 /* loops on all the bits of
792 * priv->hw_setting.valid_tx_ant */
793 u8 ant_msk = (1 << i);
794 if (!(priv->hw_params.valid_tx_ant & ant_msk))
798 if (data->disconn_array[i] == 0)
799 /* there is a Tx antenna connected */
801 if (num_tx_chains == priv->hw_params.tx_chains_num &&
802 data->disconn_array[i]) {
803 /* This is the last TX antenna and is also
804 * disconnected connect it anyway */
805 data->disconn_array[i] = 0;
806 active_chains |= ant_msk;
807 IWL_DEBUG_CALIB("All Tx chains are disconnected W/A - "
808 "declare %d as connected\n", i);
813 /* Save for use within RXON, TX, SCAN commands, etc. */
814 priv->chain_noise_data.active_chains = active_chains;
815 IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
818 /* Analyze noise for rx balance */
819 average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
820 average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
821 average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
823 for (i = 0; i < NUM_RX_CHAINS; i++) {
824 if (!(data->disconn_array[i]) &&
825 (average_noise[i] <= min_average_noise)) {
826 /* This means that chain i is active and has
827 * lower noise values so far: */
828 min_average_noise = average_noise[i];
829 min_average_noise_antenna_i = i;
833 IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
834 average_noise[0], average_noise[1],
837 IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
838 min_average_noise, min_average_noise_antenna_i);
840 priv->cfg->ops->utils->gain_computation(priv, average_noise,
841 min_average_noise_antenna_i, min_average_noise);
843 /* Some power changes may have been made during the calibration.
844 * Update and commit the RXON
846 if (priv->cfg->ops->lib->update_chain_flags)
847 priv->cfg->ops->lib->update_chain_flags(priv);
849 data->state = IWL_CHAIN_NOISE_DONE;
850 iwl_power_enable_management(priv);
852 EXPORT_SYMBOL(iwl_chain_noise_calibration);
855 void iwl_reset_run_time_calib(struct iwl_priv *priv)
858 memset(&(priv->sensitivity_data), 0,
859 sizeof(struct iwl_sensitivity_data));
860 memset(&(priv->chain_noise_data), 0,
861 sizeof(struct iwl_chain_noise_data));
862 for (i = 0; i < NUM_RX_CHAINS; i++)
863 priv->chain_noise_data.delta_gain_code[i] =
864 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
866 /* Ask for statistics now, the uCode will send notification
867 * periodically after association */
868 iwl_send_statistics_request(priv, CMD_ASYNC);
870 EXPORT_SYMBOL(iwl_reset_run_time_calib);