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