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