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 - 2009 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 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2005 - 2009 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 struct statistics_general_data {
74 u32 beacon_silence_rssi_a;
75 u32 beacon_silence_rssi_b;
76 u32 beacon_silence_rssi_c;
82 int iwl_send_calib_results(struct iwl_priv *priv)
87 struct iwl_host_cmd hcmd = {
88 .id = REPLY_PHY_CALIBRATION_CMD,
89 .flags = CMD_SIZE_HUGE,
92 for (i = 0; i < IWL_CALIB_MAX; i++) {
93 if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
94 priv->calib_results[i].buf) {
95 hcmd.len = priv->calib_results[i].buf_len;
96 hcmd.data = priv->calib_results[i].buf;
97 ret = iwl_send_cmd_sync(priv, &hcmd);
105 IWL_ERR(priv, "Error %d iteration %d\n", ret, i);
108 EXPORT_SYMBOL(iwl_send_calib_results);
110 int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
112 if (res->buf_len != len) {
114 res->buf = kzalloc(len, GFP_ATOMIC);
116 if (unlikely(res->buf == NULL))
120 memcpy(res->buf, buf, len);
123 EXPORT_SYMBOL(iwl_calib_set);
125 void iwl_calib_free_results(struct iwl_priv *priv)
129 for (i = 0; i < IWL_CALIB_MAX; i++) {
130 kfree(priv->calib_results[i].buf);
131 priv->calib_results[i].buf = NULL;
132 priv->calib_results[i].buf_len = 0;
135 EXPORT_SYMBOL(iwl_calib_free_results);
137 /*****************************************************************************
138 * RUNTIME calibrations framework
139 *****************************************************************************/
141 /* "false alarms" are signals that our DSP tries to lock onto,
142 * but then determines that they are either noise, or transmissions
143 * from a distant wireless network (also "noise", really) that get
144 * "stepped on" by stronger transmissions within our own network.
145 * This algorithm attempts to set a sensitivity level that is high
146 * enough to receive all of our own network traffic, but not so
147 * high that our DSP gets too busy trying to lock onto non-network
149 static int iwl_sens_energy_cck(struct iwl_priv *priv,
152 struct statistics_general_data *rx_info)
156 u8 max_silence_rssi = 0;
158 u8 silence_rssi_a = 0;
159 u8 silence_rssi_b = 0;
160 u8 silence_rssi_c = 0;
163 /* "false_alarms" values below are cross-multiplications to assess the
164 * numbers of false alarms within the measured period of actual Rx
165 * (Rx is off when we're txing), vs the min/max expected false alarms
166 * (some should be expected if rx is sensitive enough) in a
167 * hypothetical listening period of 200 time units (TU), 204.8 msec:
169 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
172 u32 false_alarms = norm_fa * 200 * 1024;
173 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
174 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
175 struct iwl_sensitivity_data *data = NULL;
176 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
178 data = &(priv->sensitivity_data);
180 data->nrg_auto_corr_silence_diff = 0;
182 /* Find max silence rssi among all 3 receivers.
183 * This is background noise, which may include transmissions from other
184 * networks, measured during silence before our network's beacon */
185 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
186 ALL_BAND_FILTER) >> 8);
187 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
188 ALL_BAND_FILTER) >> 8);
189 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
190 ALL_BAND_FILTER) >> 8);
192 val = max(silence_rssi_b, silence_rssi_c);
193 max_silence_rssi = max(silence_rssi_a, (u8) val);
195 /* Store silence rssi in 20-beacon history table */
196 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
197 data->nrg_silence_idx++;
198 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
199 data->nrg_silence_idx = 0;
201 /* Find max silence rssi across 20 beacon history */
202 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
203 val = data->nrg_silence_rssi[i];
204 silence_ref = max(silence_ref, val);
206 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
207 silence_rssi_a, silence_rssi_b, silence_rssi_c,
210 /* Find max rx energy (min value!) among all 3 receivers,
211 * measured during beacon frame.
212 * Save it in 10-beacon history table. */
213 i = data->nrg_energy_idx;
214 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
215 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
217 data->nrg_energy_idx++;
218 if (data->nrg_energy_idx >= 10)
219 data->nrg_energy_idx = 0;
221 /* Find min rx energy (max value) across 10 beacon history.
222 * This is the minimum signal level that we want to receive well.
223 * Add backoff (margin so we don't miss slightly lower energy frames).
224 * This establishes an upper bound (min value) for energy threshold. */
225 max_nrg_cck = data->nrg_value[0];
226 for (i = 1; i < 10; i++)
227 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
230 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
231 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
232 rx_info->beacon_energy_c, max_nrg_cck - 6);
234 /* Count number of consecutive beacons with fewer-than-desired
236 if (false_alarms < min_false_alarms)
237 data->num_in_cck_no_fa++;
239 data->num_in_cck_no_fa = 0;
240 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
241 data->num_in_cck_no_fa);
243 /* If we got too many false alarms this time, reduce sensitivity */
244 if ((false_alarms > max_false_alarms) &&
245 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
246 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
247 false_alarms, max_false_alarms);
248 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
249 data->nrg_curr_state = IWL_FA_TOO_MANY;
250 /* Store for "fewer than desired" on later beacon */
251 data->nrg_silence_ref = silence_ref;
253 /* increase energy threshold (reduce nrg value)
254 * to decrease sensitivity */
255 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
256 /* Else if we got fewer than desired, increase sensitivity */
257 } else if (false_alarms < min_false_alarms) {
258 data->nrg_curr_state = IWL_FA_TOO_FEW;
260 /* Compare silence level with silence level for most recent
261 * healthy number or too many false alarms */
262 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
265 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
266 false_alarms, min_false_alarms,
267 data->nrg_auto_corr_silence_diff);
269 /* Increase value to increase sensitivity, but only if:
270 * 1a) previous beacon did *not* have *too many* false alarms
271 * 1b) AND there's a significant difference in Rx levels
272 * from a previous beacon with too many, or healthy # FAs
273 * OR 2) We've seen a lot of beacons (100) with too few
275 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
276 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
277 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
279 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
280 /* Increase nrg value to increase sensitivity */
281 val = data->nrg_th_cck + NRG_STEP_CCK;
282 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
284 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
287 /* Else we got a healthy number of false alarms, keep status quo */
289 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
290 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
292 /* Store for use in "fewer than desired" with later beacon */
293 data->nrg_silence_ref = silence_ref;
295 /* If previous beacon had too many false alarms,
296 * give it some extra margin by reducing sensitivity again
297 * (but don't go below measured energy of desired Rx) */
298 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
299 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
300 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
301 data->nrg_th_cck -= NRG_MARGIN;
303 data->nrg_th_cck = max_nrg_cck;
307 /* Make sure the energy threshold does not go above the measured
308 * energy of the desired Rx signals (reduced by backoff margin),
309 * or else we might start missing Rx frames.
310 * Lower value is higher energy, so we use max()!
312 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
313 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
315 data->nrg_prev_state = data->nrg_curr_state;
317 /* Auto-correlation CCK algorithm */
318 if (false_alarms > min_false_alarms) {
320 /* increase auto_corr values to decrease sensitivity
321 * so the DSP won't be disturbed by the noise
323 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
324 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
326 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
327 data->auto_corr_cck =
328 min((u32)ranges->auto_corr_max_cck, val);
330 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
331 data->auto_corr_cck_mrc =
332 min((u32)ranges->auto_corr_max_cck_mrc, val);
333 } else if ((false_alarms < min_false_alarms) &&
334 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
335 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
337 /* Decrease auto_corr values to increase sensitivity */
338 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
339 data->auto_corr_cck =
340 max((u32)ranges->auto_corr_min_cck, val);
341 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
342 data->auto_corr_cck_mrc =
343 max((u32)ranges->auto_corr_min_cck_mrc, val);
350 static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
355 u32 false_alarms = norm_fa * 200 * 1024;
356 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
357 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
358 struct iwl_sensitivity_data *data = NULL;
359 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
361 data = &(priv->sensitivity_data);
363 /* If we got too many false alarms this time, reduce sensitivity */
364 if (false_alarms > max_false_alarms) {
366 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
367 false_alarms, max_false_alarms);
369 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
370 data->auto_corr_ofdm =
371 min((u32)ranges->auto_corr_max_ofdm, val);
373 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
374 data->auto_corr_ofdm_mrc =
375 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
377 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
378 data->auto_corr_ofdm_x1 =
379 min((u32)ranges->auto_corr_max_ofdm_x1, val);
381 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
382 data->auto_corr_ofdm_mrc_x1 =
383 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
386 /* Else if we got fewer than desired, increase sensitivity */
387 else if (false_alarms < min_false_alarms) {
389 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
390 false_alarms, min_false_alarms);
392 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
393 data->auto_corr_ofdm =
394 max((u32)ranges->auto_corr_min_ofdm, val);
396 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
397 data->auto_corr_ofdm_mrc =
398 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
400 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
401 data->auto_corr_ofdm_x1 =
402 max((u32)ranges->auto_corr_min_ofdm_x1, val);
404 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
405 data->auto_corr_ofdm_mrc_x1 =
406 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
408 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
409 min_false_alarms, false_alarms, max_false_alarms);
414 /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
415 static int iwl_sensitivity_write(struct iwl_priv *priv)
418 struct iwl_sensitivity_cmd cmd ;
419 struct iwl_sensitivity_data *data = NULL;
420 struct iwl_host_cmd cmd_out = {
421 .id = SENSITIVITY_CMD,
422 .len = sizeof(struct iwl_sensitivity_cmd),
427 data = &(priv->sensitivity_data);
429 memset(&cmd, 0, sizeof(cmd));
431 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
432 cpu_to_le16((u16)data->auto_corr_ofdm);
433 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
434 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
435 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
436 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
437 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
438 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
440 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
441 cpu_to_le16((u16)data->auto_corr_cck);
442 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
443 cpu_to_le16((u16)data->auto_corr_cck_mrc);
445 cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
446 cpu_to_le16((u16)data->nrg_th_cck);
447 cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
448 cpu_to_le16((u16)data->nrg_th_ofdm);
450 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
451 cpu_to_le16(data->barker_corr_th_min);
452 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
453 cpu_to_le16(data->barker_corr_th_min_mrc);
454 cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
455 cpu_to_le16(data->nrg_th_cca);
457 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
458 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
459 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
462 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
463 data->auto_corr_cck, data->auto_corr_cck_mrc,
466 /* Update uCode's "work" table, and copy it to DSP */
467 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
469 /* Don't send command to uCode if nothing has changed */
470 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
471 sizeof(u16)*HD_TABLE_SIZE)) {
472 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
476 /* Copy table for comparison next time */
477 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
478 sizeof(u16)*HD_TABLE_SIZE);
480 ret = iwl_send_cmd(priv, &cmd_out);
482 IWL_ERR(priv, "SENSITIVITY_CMD failed\n");
487 void iwl_init_sensitivity(struct iwl_priv *priv)
491 struct iwl_sensitivity_data *data = NULL;
492 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
494 if (priv->disable_sens_cal)
497 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
499 /* Clear driver's sensitivity algo data */
500 data = &(priv->sensitivity_data);
505 memset(data, 0, sizeof(struct iwl_sensitivity_data));
507 data->num_in_cck_no_fa = 0;
508 data->nrg_curr_state = IWL_FA_TOO_MANY;
509 data->nrg_prev_state = IWL_FA_TOO_MANY;
510 data->nrg_silence_ref = 0;
511 data->nrg_silence_idx = 0;
512 data->nrg_energy_idx = 0;
514 for (i = 0; i < 10; i++)
515 data->nrg_value[i] = 0;
517 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
518 data->nrg_silence_rssi[i] = 0;
520 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
521 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
522 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
523 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
524 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
525 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
526 data->nrg_th_cck = ranges->nrg_th_cck;
527 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
528 data->barker_corr_th_min = ranges->barker_corr_th_min;
529 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
530 data->nrg_th_cca = ranges->nrg_th_cca;
532 data->last_bad_plcp_cnt_ofdm = 0;
533 data->last_fa_cnt_ofdm = 0;
534 data->last_bad_plcp_cnt_cck = 0;
535 data->last_fa_cnt_cck = 0;
537 ret |= iwl_sensitivity_write(priv);
538 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
540 EXPORT_SYMBOL(iwl_init_sensitivity);
542 void iwl_sensitivity_calibration(struct iwl_priv *priv,
543 struct iwl_notif_statistics *resp)
552 struct iwl_sensitivity_data *data = NULL;
553 struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
554 struct statistics_rx *statistics = &(resp->rx);
556 struct statistics_general_data statis;
558 if (priv->disable_sens_cal)
561 data = &(priv->sensitivity_data);
563 if (!iwl_is_associated(priv)) {
564 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
568 spin_lock_irqsave(&priv->lock, flags);
569 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
570 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
571 spin_unlock_irqrestore(&priv->lock, flags);
575 /* Extract Statistics: */
576 rx_enable_time = le32_to_cpu(rx_info->channel_load);
577 fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
578 fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
579 bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
580 bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
582 statis.beacon_silence_rssi_a =
583 le32_to_cpu(statistics->general.beacon_silence_rssi_a);
584 statis.beacon_silence_rssi_b =
585 le32_to_cpu(statistics->general.beacon_silence_rssi_b);
586 statis.beacon_silence_rssi_c =
587 le32_to_cpu(statistics->general.beacon_silence_rssi_c);
588 statis.beacon_energy_a =
589 le32_to_cpu(statistics->general.beacon_energy_a);
590 statis.beacon_energy_b =
591 le32_to_cpu(statistics->general.beacon_energy_b);
592 statis.beacon_energy_c =
593 le32_to_cpu(statistics->general.beacon_energy_c);
595 spin_unlock_irqrestore(&priv->lock, flags);
597 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
599 if (!rx_enable_time) {
600 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0! \n");
604 /* These statistics increase monotonically, and do not reset
605 * at each beacon. Calculate difference from last value, or just
606 * use the new statistics value if it has reset or wrapped around. */
607 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
608 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
610 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
611 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
614 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
615 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
617 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
618 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
621 if (data->last_fa_cnt_ofdm > fa_ofdm)
622 data->last_fa_cnt_ofdm = fa_ofdm;
624 fa_ofdm -= data->last_fa_cnt_ofdm;
625 data->last_fa_cnt_ofdm += fa_ofdm;
628 if (data->last_fa_cnt_cck > fa_cck)
629 data->last_fa_cnt_cck = fa_cck;
631 fa_cck -= data->last_fa_cnt_cck;
632 data->last_fa_cnt_cck += fa_cck;
635 /* Total aborted signal locks */
636 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
637 norm_fa_cck = fa_cck + bad_plcp_cck;
639 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
640 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
642 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
643 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
644 iwl_sensitivity_write(priv);
648 EXPORT_SYMBOL(iwl_sensitivity_calibration);
650 static inline u8 find_first_chain(u8 mask)
660 * Accumulate 20 beacons of signal and noise statistics for each of
661 * 3 receivers/antennas/rx-chains, then figure out:
662 * 1) Which antennas are connected.
663 * 2) Differential rx gain settings to balance the 3 receivers.
665 void iwl_chain_noise_calibration(struct iwl_priv *priv,
666 struct iwl_notif_statistics *stat_resp)
668 struct iwl_chain_noise_data *data = NULL;
676 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
677 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
679 u16 max_average_sig_antenna_i;
680 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
681 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
683 u16 rxon_chnum = INITIALIZATION_VALUE;
684 u16 stat_chnum = INITIALIZATION_VALUE;
687 u32 active_chains = 0;
690 struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
693 if (priv->disable_chain_noise_cal)
696 data = &(priv->chain_noise_data);
699 * Accumulate just the first "chain_noise_num_beacons" after
700 * the first association, then we're done forever.
702 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
703 if (data->state == IWL_CHAIN_NOISE_ALIVE)
704 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
708 spin_lock_irqsave(&priv->lock, flags);
709 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
710 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
711 spin_unlock_irqrestore(&priv->lock, flags);
715 rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
716 rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
717 stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
718 stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
720 /* Make sure we accumulate data for just the associated channel
721 * (even if scanning). */
722 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
723 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
724 rxon_chnum, rxon_band24);
725 spin_unlock_irqrestore(&priv->lock, flags);
730 * Accumulate beacon statistics values across
731 * "chain_noise_num_beacons"
733 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
735 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
737 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
740 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
741 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
742 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
744 spin_unlock_irqrestore(&priv->lock, flags);
746 data->beacon_count++;
748 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
749 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
750 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
752 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
753 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
754 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
756 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
757 rxon_chnum, rxon_band24, data->beacon_count);
758 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
759 chain_sig_a, chain_sig_b, chain_sig_c);
760 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
761 chain_noise_a, chain_noise_b, chain_noise_c);
763 /* If this is the "chain_noise_num_beacons", determine:
764 * 1) Disconnected antennas (using signal strengths)
765 * 2) Differential gain (using silence noise) to balance receivers */
766 if (data->beacon_count != priv->cfg->chain_noise_num_beacons)
769 /* Analyze signal for disconnected antenna */
771 (data->chain_signal_a) / priv->cfg->chain_noise_num_beacons;
773 (data->chain_signal_b) / priv->cfg->chain_noise_num_beacons;
775 (data->chain_signal_c) / priv->cfg->chain_noise_num_beacons;
777 if (average_sig[0] >= average_sig[1]) {
778 max_average_sig = average_sig[0];
779 max_average_sig_antenna_i = 0;
780 active_chains = (1 << max_average_sig_antenna_i);
782 max_average_sig = average_sig[1];
783 max_average_sig_antenna_i = 1;
784 active_chains = (1 << max_average_sig_antenna_i);
787 if (average_sig[2] >= max_average_sig) {
788 max_average_sig = average_sig[2];
789 max_average_sig_antenna_i = 2;
790 active_chains = (1 << max_average_sig_antenna_i);
793 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
794 average_sig[0], average_sig[1], average_sig[2]);
795 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
796 max_average_sig, max_average_sig_antenna_i);
798 /* Compare signal strengths for all 3 receivers. */
799 for (i = 0; i < NUM_RX_CHAINS; i++) {
800 if (i != max_average_sig_antenna_i) {
801 s32 rssi_delta = (max_average_sig - average_sig[i]);
803 /* If signal is very weak, compared with
804 * strongest, mark it as disconnected. */
805 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
806 data->disconn_array[i] = 1;
808 active_chains |= (1 << i);
809 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
810 "disconn_array[i] = %d\n",
811 i, rssi_delta, data->disconn_array[i]);
816 for (i = 0; i < NUM_RX_CHAINS; i++) {
817 /* loops on all the bits of
818 * priv->hw_setting.valid_tx_ant */
819 u8 ant_msk = (1 << i);
820 if (!(priv->hw_params.valid_tx_ant & ant_msk))
824 if (data->disconn_array[i] == 0)
825 /* there is a Tx antenna connected */
827 if (num_tx_chains == priv->hw_params.tx_chains_num &&
828 data->disconn_array[i]) {
830 * If all chains are disconnected
831 * connect the first valid tx chain
834 find_first_chain(priv->cfg->valid_tx_ant);
835 data->disconn_array[first_chain] = 0;
836 active_chains |= BIT(first_chain);
837 IWL_DEBUG_CALIB(priv, "All Tx chains are disconnected W/A - declare %d as connected\n",
843 /* Save for use within RXON, TX, SCAN commands, etc. */
844 priv->chain_noise_data.active_chains = active_chains;
845 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
848 /* Analyze noise for rx balance */
850 ((data->chain_noise_a) / priv->cfg->chain_noise_num_beacons);
852 ((data->chain_noise_b) / priv->cfg->chain_noise_num_beacons);
854 ((data->chain_noise_c) / priv->cfg->chain_noise_num_beacons);
856 for (i = 0; i < NUM_RX_CHAINS; i++) {
857 if (!(data->disconn_array[i]) &&
858 (average_noise[i] <= min_average_noise)) {
859 /* This means that chain i is active and has
860 * lower noise values so far: */
861 min_average_noise = average_noise[i];
862 min_average_noise_antenna_i = i;
866 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
867 average_noise[0], average_noise[1],
870 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
871 min_average_noise, min_average_noise_antenna_i);
873 if (priv->cfg->ops->utils->gain_computation)
874 priv->cfg->ops->utils->gain_computation(priv, average_noise,
875 min_average_noise_antenna_i, min_average_noise,
876 find_first_chain(priv->cfg->valid_rx_ant));
878 /* Some power changes may have been made during the calibration.
879 * Update and commit the RXON
881 if (priv->cfg->ops->lib->update_chain_flags)
882 priv->cfg->ops->lib->update_chain_flags(priv);
884 data->state = IWL_CHAIN_NOISE_DONE;
885 iwl_power_update_mode(priv, false);
887 EXPORT_SYMBOL(iwl_chain_noise_calibration);
890 void iwl_reset_run_time_calib(struct iwl_priv *priv)
893 memset(&(priv->sensitivity_data), 0,
894 sizeof(struct iwl_sensitivity_data));
895 memset(&(priv->chain_noise_data), 0,
896 sizeof(struct iwl_chain_noise_data));
897 for (i = 0; i < NUM_RX_CHAINS; i++)
898 priv->chain_noise_data.delta_gain_code[i] =
899 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
901 /* Ask for statistics now, the uCode will send notification
902 * periodically after association */
903 iwl_send_statistics_request(priv, CMD_ASYNC, true);
905 EXPORT_SYMBOL(iwl_reset_run_time_calib);