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