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