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