Merge commit 'v2.6.27-rc3' into x86/xsave
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-calib.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2008 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called LICENSE.GPL.
26  *
27  * Contact Information:
28  * Tomas Winkler <tomas.winkler@intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *****************************************************************************/
62
63 #include <net/mac80211.h>
64
65 #include "iwl-dev.h"
66 #include "iwl-core.h"
67 #include "iwl-calib.h"
68
69 /* "false alarms" are signals that our DSP tries to lock onto,
70  *   but then determines that they are either noise, or transmissions
71  *   from a distant wireless network (also "noise", really) that get
72  *   "stepped on" by stronger transmissions within our own network.
73  * This algorithm attempts to set a sensitivity level that is high
74  *   enough to receive all of our own network traffic, but not so
75  *   high that our DSP gets too busy trying to lock onto non-network
76  *   activity/noise. */
77 static int iwl_sens_energy_cck(struct iwl_priv *priv,
78                                    u32 norm_fa,
79                                    u32 rx_enable_time,
80                                    struct statistics_general_data *rx_info)
81 {
82         u32 max_nrg_cck = 0;
83         int i = 0;
84         u8 max_silence_rssi = 0;
85         u32 silence_ref = 0;
86         u8 silence_rssi_a = 0;
87         u8 silence_rssi_b = 0;
88         u8 silence_rssi_c = 0;
89         u32 val;
90
91         /* "false_alarms" values below are cross-multiplications to assess the
92          *   numbers of false alarms within the measured period of actual Rx
93          *   (Rx is off when we're txing), vs the min/max expected false alarms
94          *   (some should be expected if rx is sensitive enough) in a
95          *   hypothetical listening period of 200 time units (TU), 204.8 msec:
96          *
97          * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
98          *
99          * */
100         u32 false_alarms = norm_fa * 200 * 1024;
101         u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
102         u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
103         struct iwl_sensitivity_data *data = NULL;
104         const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
105
106         data = &(priv->sensitivity_data);
107
108         data->nrg_auto_corr_silence_diff = 0;
109
110         /* Find max silence rssi among all 3 receivers.
111          * This is background noise, which may include transmissions from other
112          *    networks, measured during silence before our network's beacon */
113         silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
114                             ALL_BAND_FILTER) >> 8);
115         silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
116                             ALL_BAND_FILTER) >> 8);
117         silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
118                             ALL_BAND_FILTER) >> 8);
119
120         val = max(silence_rssi_b, silence_rssi_c);
121         max_silence_rssi = max(silence_rssi_a, (u8) val);
122
123         /* Store silence rssi in 20-beacon history table */
124         data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
125         data->nrg_silence_idx++;
126         if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
127                 data->nrg_silence_idx = 0;
128
129         /* Find max silence rssi across 20 beacon history */
130         for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
131                 val = data->nrg_silence_rssi[i];
132                 silence_ref = max(silence_ref, val);
133         }
134         IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
135                         silence_rssi_a, silence_rssi_b, silence_rssi_c,
136                         silence_ref);
137
138         /* Find max rx energy (min value!) among all 3 receivers,
139          *   measured during beacon frame.
140          * Save it in 10-beacon history table. */
141         i = data->nrg_energy_idx;
142         val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
143         data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
144
145         data->nrg_energy_idx++;
146         if (data->nrg_energy_idx >= 10)
147                 data->nrg_energy_idx = 0;
148
149         /* Find min rx energy (max value) across 10 beacon history.
150          * This is the minimum signal level that we want to receive well.
151          * Add backoff (margin so we don't miss slightly lower energy frames).
152          * This establishes an upper bound (min value) for energy threshold. */
153         max_nrg_cck = data->nrg_value[0];
154         for (i = 1; i < 10; i++)
155                 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
156         max_nrg_cck += 6;
157
158         IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
159                         rx_info->beacon_energy_a, rx_info->beacon_energy_b,
160                         rx_info->beacon_energy_c, max_nrg_cck - 6);
161
162         /* Count number of consecutive beacons with fewer-than-desired
163          *   false alarms. */
164         if (false_alarms < min_false_alarms)
165                 data->num_in_cck_no_fa++;
166         else
167                 data->num_in_cck_no_fa = 0;
168         IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
169                         data->num_in_cck_no_fa);
170
171         /* If we got too many false alarms this time, reduce sensitivity */
172         if ((false_alarms > max_false_alarms) &&
173                 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
174                 IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
175                      false_alarms, max_false_alarms);
176                 IWL_DEBUG_CALIB("... reducing sensitivity\n");
177                 data->nrg_curr_state = IWL_FA_TOO_MANY;
178                 /* Store for "fewer than desired" on later beacon */
179                 data->nrg_silence_ref = silence_ref;
180
181                 /* increase energy threshold (reduce nrg value)
182                  *   to decrease sensitivity */
183                 if (data->nrg_th_cck >
184                         (ranges->max_nrg_cck + NRG_STEP_CCK))
185                         data->nrg_th_cck = data->nrg_th_cck
186                                                  - NRG_STEP_CCK;
187                 else
188                         data->nrg_th_cck = ranges->max_nrg_cck;
189         /* Else if we got fewer than desired, increase sensitivity */
190         } else if (false_alarms < min_false_alarms) {
191                 data->nrg_curr_state = IWL_FA_TOO_FEW;
192
193                 /* Compare silence level with silence level for most recent
194                  *   healthy number or too many false alarms */
195                 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
196                                                    (s32)silence_ref;
197
198                 IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
199                          false_alarms, min_false_alarms,
200                          data->nrg_auto_corr_silence_diff);
201
202                 /* Increase value to increase sensitivity, but only if:
203                  * 1a) previous beacon did *not* have *too many* false alarms
204                  * 1b) AND there's a significant difference in Rx levels
205                  *      from a previous beacon with too many, or healthy # FAs
206                  * OR 2) We've seen a lot of beacons (100) with too few
207                  *       false alarms */
208                 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
209                         ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
210                         (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
211
212                         IWL_DEBUG_CALIB("... increasing sensitivity\n");
213                         /* Increase nrg value to increase sensitivity */
214                         val = data->nrg_th_cck + NRG_STEP_CCK;
215                         data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
216                 } else {
217                         IWL_DEBUG_CALIB("... but not changing sensitivity\n");
218                 }
219
220         /* Else we got a healthy number of false alarms, keep status quo */
221         } else {
222                 IWL_DEBUG_CALIB(" FA in safe zone\n");
223                 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
224
225                 /* Store for use in "fewer than desired" with later beacon */
226                 data->nrg_silence_ref = silence_ref;
227
228                 /* If previous beacon had too many false alarms,
229                  *   give it some extra margin by reducing sensitivity again
230                  *   (but don't go below measured energy of desired Rx) */
231                 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
232                         IWL_DEBUG_CALIB("... increasing margin\n");
233                         if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
234                                 data->nrg_th_cck -= NRG_MARGIN;
235                         else
236                                 data->nrg_th_cck = max_nrg_cck;
237                 }
238         }
239
240         /* Make sure the energy threshold does not go above the measured
241          * energy of the desired Rx signals (reduced by backoff margin),
242          * or else we might start missing Rx frames.
243          * Lower value is higher energy, so we use max()!
244          */
245         data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
246         IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
247
248         data->nrg_prev_state = data->nrg_curr_state;
249
250         /* Auto-correlation CCK algorithm */
251         if (false_alarms > min_false_alarms) {
252
253                 /* increase auto_corr values to decrease sensitivity
254                  * so the DSP won't be disturbed by the noise
255                  */
256                 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
257                         data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
258                 else {
259                         val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
260                         data->auto_corr_cck =
261                                 min((u32)ranges->auto_corr_max_cck, val);
262                 }
263                 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
264                 data->auto_corr_cck_mrc =
265                         min((u32)ranges->auto_corr_max_cck_mrc, val);
266         } else if ((false_alarms < min_false_alarms) &&
267            ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
268            (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
269
270                 /* Decrease auto_corr values to increase sensitivity */
271                 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
272                 data->auto_corr_cck =
273                         max((u32)ranges->auto_corr_min_cck, val);
274                 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
275                 data->auto_corr_cck_mrc =
276                         max((u32)ranges->auto_corr_min_cck_mrc, val);
277         }
278
279         return 0;
280 }
281
282
283 static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
284                                        u32 norm_fa,
285                                        u32 rx_enable_time)
286 {
287         u32 val;
288         u32 false_alarms = norm_fa * 200 * 1024;
289         u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
290         u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
291         struct iwl_sensitivity_data *data = NULL;
292         const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
293
294         data = &(priv->sensitivity_data);
295
296         /* If we got too many false alarms this time, reduce sensitivity */
297         if (false_alarms > max_false_alarms) {
298
299                 IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
300                              false_alarms, max_false_alarms);
301
302                 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
303                 data->auto_corr_ofdm =
304                         min((u32)ranges->auto_corr_max_ofdm, val);
305
306                 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
307                 data->auto_corr_ofdm_mrc =
308                         min((u32)ranges->auto_corr_max_ofdm_mrc, val);
309
310                 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
311                 data->auto_corr_ofdm_x1 =
312                         min((u32)ranges->auto_corr_max_ofdm_x1, val);
313
314                 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
315                 data->auto_corr_ofdm_mrc_x1 =
316                         min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
317         }
318
319         /* Else if we got fewer than desired, increase sensitivity */
320         else if (false_alarms < min_false_alarms) {
321
322                 IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
323                              false_alarms, min_false_alarms);
324
325                 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
326                 data->auto_corr_ofdm =
327                         max((u32)ranges->auto_corr_min_ofdm, val);
328
329                 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
330                 data->auto_corr_ofdm_mrc =
331                         max((u32)ranges->auto_corr_min_ofdm_mrc, val);
332
333                 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
334                 data->auto_corr_ofdm_x1 =
335                         max((u32)ranges->auto_corr_min_ofdm_x1, val);
336
337                 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
338                 data->auto_corr_ofdm_mrc_x1 =
339                         max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
340         } else {
341                 IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
342                          min_false_alarms, false_alarms, max_false_alarms);
343         }
344         return 0;
345 }
346
347 /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
348 static int iwl_sensitivity_write(struct iwl_priv *priv)
349 {
350         int ret = 0;
351         struct iwl_sensitivity_cmd cmd ;
352         struct iwl_sensitivity_data *data = NULL;
353         struct iwl_host_cmd cmd_out = {
354                 .id = SENSITIVITY_CMD,
355                 .len = sizeof(struct iwl_sensitivity_cmd),
356                 .meta.flags = CMD_ASYNC,
357                 .data = &cmd,
358         };
359
360         data = &(priv->sensitivity_data);
361
362         memset(&cmd, 0, sizeof(cmd));
363
364         cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
365                                 cpu_to_le16((u16)data->auto_corr_ofdm);
366         cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
367                                 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
368         cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
369                                 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
370         cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
371                                 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
372
373         cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
374                                 cpu_to_le16((u16)data->auto_corr_cck);
375         cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
376                                 cpu_to_le16((u16)data->auto_corr_cck_mrc);
377
378         cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
379                                 cpu_to_le16((u16)data->nrg_th_cck);
380         cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
381                                 cpu_to_le16((u16)data->nrg_th_ofdm);
382
383         cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
384                                 __constant_cpu_to_le16(190);
385         cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
386                                 __constant_cpu_to_le16(390);
387         cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
388                                 __constant_cpu_to_le16(62);
389
390         IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
391                         data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
392                         data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
393                         data->nrg_th_ofdm);
394
395         IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
396                         data->auto_corr_cck, data->auto_corr_cck_mrc,
397                         data->nrg_th_cck);
398
399         /* Update uCode's "work" table, and copy it to DSP */
400         cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
401
402         /* Don't send command to uCode if nothing has changed */
403         if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
404                     sizeof(u16)*HD_TABLE_SIZE)) {
405                 IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
406                 return 0;
407         }
408
409         /* Copy table for comparison next time */
410         memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
411                sizeof(u16)*HD_TABLE_SIZE);
412
413         ret = iwl_send_cmd(priv, &cmd_out);
414         if (ret)
415                 IWL_ERROR("SENSITIVITY_CMD failed\n");
416
417         return ret;
418 }
419
420 void iwl_init_sensitivity(struct iwl_priv *priv)
421 {
422         int ret = 0;
423         int i;
424         struct iwl_sensitivity_data *data = NULL;
425         const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
426
427         if (priv->disable_sens_cal)
428                 return;
429
430         IWL_DEBUG_CALIB("Start iwl_init_sensitivity\n");
431
432         /* Clear driver's sensitivity algo data */
433         data = &(priv->sensitivity_data);
434
435         if (ranges == NULL)
436                 return;
437
438         memset(data, 0, sizeof(struct iwl_sensitivity_data));
439
440         data->num_in_cck_no_fa = 0;
441         data->nrg_curr_state = IWL_FA_TOO_MANY;
442         data->nrg_prev_state = IWL_FA_TOO_MANY;
443         data->nrg_silence_ref = 0;
444         data->nrg_silence_idx = 0;
445         data->nrg_energy_idx = 0;
446
447         for (i = 0; i < 10; i++)
448                 data->nrg_value[i] = 0;
449
450         for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
451                 data->nrg_silence_rssi[i] = 0;
452
453         data->auto_corr_ofdm = 90;
454         data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
455         data->auto_corr_ofdm_x1  = ranges->auto_corr_min_ofdm_x1;
456         data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
457         data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
458         data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
459         data->nrg_th_cck = ranges->nrg_th_cck;
460         data->nrg_th_ofdm = ranges->nrg_th_ofdm;
461
462         data->last_bad_plcp_cnt_ofdm = 0;
463         data->last_fa_cnt_ofdm = 0;
464         data->last_bad_plcp_cnt_cck = 0;
465         data->last_fa_cnt_cck = 0;
466
467         ret |= iwl_sensitivity_write(priv);
468         IWL_DEBUG_CALIB("<<return 0x%X\n", ret);
469 }
470 EXPORT_SYMBOL(iwl_init_sensitivity);
471
472 void iwl_sensitivity_calibration(struct iwl_priv *priv,
473                                     struct iwl_notif_statistics *resp)
474 {
475         u32 rx_enable_time;
476         u32 fa_cck;
477         u32 fa_ofdm;
478         u32 bad_plcp_cck;
479         u32 bad_plcp_ofdm;
480         u32 norm_fa_ofdm;
481         u32 norm_fa_cck;
482         struct iwl_sensitivity_data *data = NULL;
483         struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
484         struct statistics_rx *statistics = &(resp->rx);
485         unsigned long flags;
486         struct statistics_general_data statis;
487
488         if (priv->disable_sens_cal)
489                 return;
490
491         data = &(priv->sensitivity_data);
492
493         if (!iwl_is_associated(priv)) {
494                 IWL_DEBUG_CALIB("<< - not associated\n");
495                 return;
496         }
497
498         spin_lock_irqsave(&priv->lock, flags);
499         if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
500                 IWL_DEBUG_CALIB("<< invalid data.\n");
501                 spin_unlock_irqrestore(&priv->lock, flags);
502                 return;
503         }
504
505         /* Extract Statistics: */
506         rx_enable_time = le32_to_cpu(rx_info->channel_load);
507         fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
508         fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
509         bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
510         bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
511
512         statis.beacon_silence_rssi_a =
513                         le32_to_cpu(statistics->general.beacon_silence_rssi_a);
514         statis.beacon_silence_rssi_b =
515                         le32_to_cpu(statistics->general.beacon_silence_rssi_b);
516         statis.beacon_silence_rssi_c =
517                         le32_to_cpu(statistics->general.beacon_silence_rssi_c);
518         statis.beacon_energy_a =
519                         le32_to_cpu(statistics->general.beacon_energy_a);
520         statis.beacon_energy_b =
521                         le32_to_cpu(statistics->general.beacon_energy_b);
522         statis.beacon_energy_c =
523                         le32_to_cpu(statistics->general.beacon_energy_c);
524
525         spin_unlock_irqrestore(&priv->lock, flags);
526
527         IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
528
529         if (!rx_enable_time) {
530                 IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
531                 return;
532         }
533
534         /* These statistics increase monotonically, and do not reset
535          *   at each beacon.  Calculate difference from last value, or just
536          *   use the new statistics value if it has reset or wrapped around. */
537         if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
538                 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
539         else {
540                 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
541                 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
542         }
543
544         if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
545                 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
546         else {
547                 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
548                 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
549         }
550
551         if (data->last_fa_cnt_ofdm > fa_ofdm)
552                 data->last_fa_cnt_ofdm = fa_ofdm;
553         else {
554                 fa_ofdm -= data->last_fa_cnt_ofdm;
555                 data->last_fa_cnt_ofdm += fa_ofdm;
556         }
557
558         if (data->last_fa_cnt_cck > fa_cck)
559                 data->last_fa_cnt_cck = fa_cck;
560         else {
561                 fa_cck -= data->last_fa_cnt_cck;
562                 data->last_fa_cnt_cck += fa_cck;
563         }
564
565         /* Total aborted signal locks */
566         norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
567         norm_fa_cck = fa_cck + bad_plcp_cck;
568
569         IWL_DEBUG_CALIB("cck: fa %u badp %u  ofdm: fa %u badp %u\n", fa_cck,
570                         bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
571
572         iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
573         iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
574         iwl_sensitivity_write(priv);
575
576         return;
577 }
578 EXPORT_SYMBOL(iwl_sensitivity_calibration);
579
580 /*
581  * Accumulate 20 beacons of signal and noise statistics for each of
582  *   3 receivers/antennas/rx-chains, then figure out:
583  * 1)  Which antennas are connected.
584  * 2)  Differential rx gain settings to balance the 3 receivers.
585  */
586 void iwl_chain_noise_calibration(struct iwl_priv *priv,
587                                  struct iwl_notif_statistics *stat_resp)
588 {
589         struct iwl_chain_noise_data *data = NULL;
590
591         u32 chain_noise_a;
592         u32 chain_noise_b;
593         u32 chain_noise_c;
594         u32 chain_sig_a;
595         u32 chain_sig_b;
596         u32 chain_sig_c;
597         u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
598         u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
599         u32 max_average_sig;
600         u16 max_average_sig_antenna_i;
601         u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
602         u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
603         u16 i = 0;
604         u16 rxon_chnum = INITIALIZATION_VALUE;
605         u16 stat_chnum = INITIALIZATION_VALUE;
606         u8 rxon_band24;
607         u8 stat_band24;
608         u32 active_chains = 0;
609         u8 num_tx_chains;
610         unsigned long flags;
611         struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
612
613         if (priv->disable_chain_noise_cal)
614                 return;
615
616         data = &(priv->chain_noise_data);
617
618         /* Accumulate just the first 20 beacons after the first association,
619          *   then we're done forever. */
620         if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
621                 if (data->state == IWL_CHAIN_NOISE_ALIVE)
622                         IWL_DEBUG_CALIB("Wait for noise calib reset\n");
623                 return;
624         }
625
626         spin_lock_irqsave(&priv->lock, flags);
627         if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
628                 IWL_DEBUG_CALIB(" << Interference data unavailable\n");
629                 spin_unlock_irqrestore(&priv->lock, flags);
630                 return;
631         }
632
633         rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
634         rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
635         stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
636         stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
637
638         /* Make sure we accumulate data for just the associated channel
639          *   (even if scanning). */
640         if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
641                 IWL_DEBUG_CALIB("Stats not from chan=%d, band24=%d\n",
642                                 rxon_chnum, rxon_band24);
643                 spin_unlock_irqrestore(&priv->lock, flags);
644                 return;
645         }
646
647         /* Accumulate beacon statistics values across 20 beacons */
648         chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
649                                 IN_BAND_FILTER;
650         chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
651                                 IN_BAND_FILTER;
652         chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
653                                 IN_BAND_FILTER;
654
655         chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
656         chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
657         chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
658
659         spin_unlock_irqrestore(&priv->lock, flags);
660
661         data->beacon_count++;
662
663         data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
664         data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
665         data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
666
667         data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
668         data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
669         data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
670
671         IWL_DEBUG_CALIB("chan=%d, band24=%d, beacon=%d\n",
672                         rxon_chnum, rxon_band24, data->beacon_count);
673         IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
674                         chain_sig_a, chain_sig_b, chain_sig_c);
675         IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
676                         chain_noise_a, chain_noise_b, chain_noise_c);
677
678         /* If this is the 20th beacon, determine:
679          * 1)  Disconnected antennas (using signal strengths)
680          * 2)  Differential gain (using silence noise) to balance receivers */
681         if (data->beacon_count != CAL_NUM_OF_BEACONS)
682                 return;
683
684         /* Analyze signal for disconnected antenna */
685         average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
686         average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
687         average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
688
689         if (average_sig[0] >= average_sig[1]) {
690                 max_average_sig = average_sig[0];
691                 max_average_sig_antenna_i = 0;
692                 active_chains = (1 << max_average_sig_antenna_i);
693         } else {
694                 max_average_sig = average_sig[1];
695                 max_average_sig_antenna_i = 1;
696                 active_chains = (1 << max_average_sig_antenna_i);
697         }
698
699         if (average_sig[2] >= max_average_sig) {
700                 max_average_sig = average_sig[2];
701                 max_average_sig_antenna_i = 2;
702                 active_chains = (1 << max_average_sig_antenna_i);
703         }
704
705         IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
706                      average_sig[0], average_sig[1], average_sig[2]);
707         IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
708                      max_average_sig, max_average_sig_antenna_i);
709
710         /* Compare signal strengths for all 3 receivers. */
711         for (i = 0; i < NUM_RX_CHAINS; i++) {
712                 if (i != max_average_sig_antenna_i) {
713                         s32 rssi_delta = (max_average_sig - average_sig[i]);
714
715                         /* If signal is very weak, compared with
716                          * strongest, mark it as disconnected. */
717                         if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
718                                 data->disconn_array[i] = 1;
719                         else
720                                 active_chains |= (1 << i);
721                         IWL_DEBUG_CALIB("i = %d  rssiDelta = %d  "
722                              "disconn_array[i] = %d\n",
723                              i, rssi_delta, data->disconn_array[i]);
724                 }
725         }
726
727         num_tx_chains = 0;
728         for (i = 0; i < NUM_RX_CHAINS; i++) {
729                 /* loops on all the bits of
730                  * priv->hw_setting.valid_tx_ant */
731                 u8 ant_msk = (1 << i);
732                 if (!(priv->hw_params.valid_tx_ant & ant_msk))
733                         continue;
734
735                 num_tx_chains++;
736                 if (data->disconn_array[i] == 0)
737                         /* there is a Tx antenna connected */
738                         break;
739                 if (num_tx_chains == priv->hw_params.tx_chains_num &&
740                 data->disconn_array[i]) {
741                         /* This is the last TX antenna and is also
742                          * disconnected connect it anyway */
743                         data->disconn_array[i] = 0;
744                         active_chains |= ant_msk;
745                         IWL_DEBUG_CALIB("All Tx chains are disconnected W/A - "
746                                 "declare %d as connected\n", i);
747                         break;
748                 }
749         }
750
751         IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
752                         active_chains);
753
754         /* Save for use within RXON, TX, SCAN commands, etc. */
755         /*priv->valid_antenna = active_chains;*/
756         /*FIXME: should be reflected in RX chains in RXON */
757
758         /* Analyze noise for rx balance */
759         average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
760         average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
761         average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
762
763         for (i = 0; i < NUM_RX_CHAINS; i++) {
764                 if (!(data->disconn_array[i]) &&
765                    (average_noise[i] <= min_average_noise)) {
766                         /* This means that chain i is active and has
767                          * lower noise values so far: */
768                         min_average_noise = average_noise[i];
769                         min_average_noise_antenna_i = i;
770                 }
771         }
772
773         IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
774                         average_noise[0], average_noise[1],
775                         average_noise[2]);
776
777         IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
778                         min_average_noise, min_average_noise_antenna_i);
779
780         priv->cfg->ops->utils->gain_computation(priv, average_noise,
781                 min_average_noise_antenna_i, min_average_noise);
782 }
783 EXPORT_SYMBOL(iwl_chain_noise_calibration);
784
785
786 void iwl_reset_run_time_calib(struct iwl_priv *priv)
787 {
788         int i;
789         memset(&(priv->sensitivity_data), 0,
790                sizeof(struct iwl_sensitivity_data));
791         memset(&(priv->chain_noise_data), 0,
792                sizeof(struct iwl_chain_noise_data));
793         for (i = 0; i < NUM_RX_CHAINS; i++)
794                 priv->chain_noise_data.delta_gain_code[i] =
795                                 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
796
797         /* Ask for statistics now, the uCode will send notification
798          * periodically after association */
799         iwl_send_statistics_request(priv, CMD_ASYNC);
800 }
801 EXPORT_SYMBOL(iwl_reset_run_time_calib);
802