Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / iwl-calib.c
CommitLineData
f0832f13
EG
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 *
1f447808 8 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
f0832f13
EG
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:
759ef89f 28 * Intel Linux Wireless <ilw@linux.intel.com>
f0832f13
EG
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
1f447808 33 * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved.
f0832f13
EG
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
f0832f13
EG
63#include <net/mac80211.h>
64
3e0d4cb1 65#include "iwl-dev.h"
f0832f13
EG
66#include "iwl-core.h"
67#include "iwl-calib.h"
f0832f13 68
6e21f2c1
TW
69/*****************************************************************************
70 * INIT calibrations framework
71 *****************************************************************************/
72
34c22cf9
WT
73struct 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
be5d56ed 82int iwl_send_calib_results(struct iwl_priv *priv)
6e21f2c1
TW
83{
84 int ret = 0;
85 int i = 0;
86
87 struct iwl_host_cmd hcmd = {
88 .id = REPLY_PHY_CALIBRATION_CMD,
c2acea8e 89 .flags = CMD_SIZE_HUGE,
6e21f2c1
TW
90 };
91
be5d56ed
TW
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) {
6e21f2c1
TW
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 }
be5d56ed 101 }
6e21f2c1
TW
102
103 return 0;
104err:
15b1687c 105 IWL_ERR(priv, "Error %d iteration %d\n", ret, i);
6e21f2c1
TW
106 return ret;
107}
108EXPORT_SYMBOL(iwl_send_calib_results);
109
110int 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}
123EXPORT_SYMBOL(iwl_calib_set);
124
125void 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}
89f186a8 135EXPORT_SYMBOL(iwl_calib_free_results);
6e21f2c1
TW
136
137/*****************************************************************************
138 * RUNTIME calibrations framework
139 *****************************************************************************/
140
f0832f13
EG
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. */
149static 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 }
e1623446 206 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
f0832f13
EG
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
e1623446 230 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
f0832f13
EG
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;
e1623446 240 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
f0832f13
EG
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)) {
e1623446 246 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
f0832f13 247 false_alarms, max_false_alarms);
e1623446 248 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
f0832f13
EG
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 */
fe6efb4b 255 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
f0832f13
EG
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
e1623446 265 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
f0832f13
EG
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
e1623446 279 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
f0832f13
EG
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 {
e1623446 284 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
f0832f13
EG
285 }
286
287 /* Else we got a healthy number of false alarms, keep status quo */
288 } else {
e1623446 289 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
f0832f13
EG
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) {
e1623446 299 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
f0832f13
EG
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);
e1623446 313 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
f0832f13
EG
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
350static 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
e1623446 366 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
f0832f13
EG
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
e1623446 389 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
f0832f13
EG
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 {
e1623446 408 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
f0832f13
EG
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 */
415static int iwl_sensitivity_write(struct iwl_priv *priv)
416{
f0832f13
EG
417 struct iwl_sensitivity_cmd cmd ;
418 struct iwl_sensitivity_data *data = NULL;
419 struct iwl_host_cmd cmd_out = {
420 .id = SENSITIVITY_CMD,
421 .len = sizeof(struct iwl_sensitivity_cmd),
c2acea8e 422 .flags = CMD_ASYNC,
f0832f13
EG
423 .data = &cmd,
424 };
425
426 data = &(priv->sensitivity_data);
427
428 memset(&cmd, 0, sizeof(cmd));
429
430 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
431 cpu_to_le16((u16)data->auto_corr_ofdm);
432 cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
433 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
434 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
435 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
436 cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
437 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
438
439 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
440 cpu_to_le16((u16)data->auto_corr_cck);
441 cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
442 cpu_to_le16((u16)data->auto_corr_cck_mrc);
443
444 cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
445 cpu_to_le16((u16)data->nrg_th_cck);
446 cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
447 cpu_to_le16((u16)data->nrg_th_ofdm);
448
449 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
55036d66 450 cpu_to_le16(data->barker_corr_th_min);
f0832f13 451 cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
55036d66 452 cpu_to_le16(data->barker_corr_th_min_mrc);
f0832f13 453 cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
55036d66 454 cpu_to_le16(data->nrg_th_cca);
f0832f13 455
e1623446 456 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
f0832f13
EG
457 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
458 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
459 data->nrg_th_ofdm);
460
e1623446 461 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
f0832f13
EG
462 data->auto_corr_cck, data->auto_corr_cck_mrc,
463 data->nrg_th_cck);
464
465 /* Update uCode's "work" table, and copy it to DSP */
466 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
467
468 /* Don't send command to uCode if nothing has changed */
469 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
470 sizeof(u16)*HD_TABLE_SIZE)) {
e1623446 471 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
f0832f13
EG
472 return 0;
473 }
474
475 /* Copy table for comparison next time */
476 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
477 sizeof(u16)*HD_TABLE_SIZE);
478
4309af27 479 return iwl_send_cmd(priv, &cmd_out);
f0832f13
EG
480}
481
482void 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
445c2dff
TW
489 if (priv->disable_sens_cal)
490 return;
491
e1623446 492 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
f0832f13
EG
493
494 /* Clear driver's sensitivity algo data */
495 data = &(priv->sensitivity_data);
496
497 if (ranges == NULL)
f0832f13
EG
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
f3a2a424 515 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
f0832f13
EG
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;
55036d66
WYG
523 data->barker_corr_th_min = ranges->barker_corr_th_min;
524 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
525 data->nrg_th_cca = ranges->nrg_th_cca;
f0832f13
EG
526
527 data->last_bad_plcp_cnt_ofdm = 0;
528 data->last_fa_cnt_ofdm = 0;
529 data->last_bad_plcp_cnt_cck = 0;
530 data->last_fa_cnt_cck = 0;
531
532 ret |= iwl_sensitivity_write(priv);
e1623446 533 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
f0832f13
EG
534}
535EXPORT_SYMBOL(iwl_init_sensitivity);
536
537void iwl_sensitivity_calibration(struct iwl_priv *priv,
8f91aecb 538 struct iwl_notif_statistics *resp)
f0832f13
EG
539{
540 u32 rx_enable_time;
541 u32 fa_cck;
542 u32 fa_ofdm;
543 u32 bad_plcp_cck;
544 u32 bad_plcp_ofdm;
545 u32 norm_fa_ofdm;
546 u32 norm_fa_cck;
547 struct iwl_sensitivity_data *data = NULL;
548 struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
549 struct statistics_rx *statistics = &(resp->rx);
550 unsigned long flags;
551 struct statistics_general_data statis;
552
445c2dff
TW
553 if (priv->disable_sens_cal)
554 return;
555
f0832f13
EG
556 data = &(priv->sensitivity_data);
557
558 if (!iwl_is_associated(priv)) {
e1623446 559 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
f0832f13
EG
560 return;
561 }
562
563 spin_lock_irqsave(&priv->lock, flags);
564 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
e1623446 565 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
f0832f13
EG
566 spin_unlock_irqrestore(&priv->lock, flags);
567 return;
568 }
569
570 /* Extract Statistics: */
571 rx_enable_time = le32_to_cpu(rx_info->channel_load);
572 fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
573 fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
574 bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
575 bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
576
577 statis.beacon_silence_rssi_a =
578 le32_to_cpu(statistics->general.beacon_silence_rssi_a);
579 statis.beacon_silence_rssi_b =
580 le32_to_cpu(statistics->general.beacon_silence_rssi_b);
581 statis.beacon_silence_rssi_c =
582 le32_to_cpu(statistics->general.beacon_silence_rssi_c);
583 statis.beacon_energy_a =
584 le32_to_cpu(statistics->general.beacon_energy_a);
585 statis.beacon_energy_b =
586 le32_to_cpu(statistics->general.beacon_energy_b);
587 statis.beacon_energy_c =
588 le32_to_cpu(statistics->general.beacon_energy_c);
589
590 spin_unlock_irqrestore(&priv->lock, flags);
591
e1623446 592 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
f0832f13
EG
593
594 if (!rx_enable_time) {
e1623446 595 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0! \n");
f0832f13
EG
596 return;
597 }
598
599 /* These statistics increase monotonically, and do not reset
600 * at each beacon. Calculate difference from last value, or just
601 * use the new statistics value if it has reset or wrapped around. */
602 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
603 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
604 else {
605 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
606 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
607 }
608
609 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
610 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
611 else {
612 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
613 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
614 }
615
616 if (data->last_fa_cnt_ofdm > fa_ofdm)
617 data->last_fa_cnt_ofdm = fa_ofdm;
618 else {
619 fa_ofdm -= data->last_fa_cnt_ofdm;
620 data->last_fa_cnt_ofdm += fa_ofdm;
621 }
622
623 if (data->last_fa_cnt_cck > fa_cck)
624 data->last_fa_cnt_cck = fa_cck;
625 else {
626 fa_cck -= data->last_fa_cnt_cck;
627 data->last_fa_cnt_cck += fa_cck;
628 }
629
630 /* Total aborted signal locks */
631 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
632 norm_fa_cck = fa_cck + bad_plcp_cck;
633
e1623446 634 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
f0832f13
EG
635 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
636
637 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
638 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
639 iwl_sensitivity_write(priv);
640
641 return;
642}
643EXPORT_SYMBOL(iwl_sensitivity_calibration);
644
d8c07e7a
WYG
645static inline u8 find_first_chain(u8 mask)
646{
647 if (mask & ANT_A)
648 return CHAIN_A;
649 if (mask & ANT_B)
650 return CHAIN_B;
651 return CHAIN_C;
652}
653
f0832f13
EG
654/*
655 * Accumulate 20 beacons of signal and noise statistics for each of
656 * 3 receivers/antennas/rx-chains, then figure out:
657 * 1) Which antennas are connected.
658 * 2) Differential rx gain settings to balance the 3 receivers.
659 */
660void iwl_chain_noise_calibration(struct iwl_priv *priv,
8f91aecb 661 struct iwl_notif_statistics *stat_resp)
f0832f13
EG
662{
663 struct iwl_chain_noise_data *data = NULL;
664
665 u32 chain_noise_a;
666 u32 chain_noise_b;
667 u32 chain_noise_c;
668 u32 chain_sig_a;
669 u32 chain_sig_b;
670 u32 chain_sig_c;
671 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
672 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
673 u32 max_average_sig;
674 u16 max_average_sig_antenna_i;
675 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
676 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
677 u16 i = 0;
678 u16 rxon_chnum = INITIALIZATION_VALUE;
679 u16 stat_chnum = INITIALIZATION_VALUE;
680 u8 rxon_band24;
681 u8 stat_band24;
682 u32 active_chains = 0;
683 u8 num_tx_chains;
684 unsigned long flags;
685 struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
d8c07e7a 686 u8 first_chain;
f0832f13 687
445c2dff
TW
688 if (priv->disable_chain_noise_cal)
689 return;
690
f0832f13
EG
691 data = &(priv->chain_noise_data);
692
d8c07e7a
WYG
693 /*
694 * Accumulate just the first "chain_noise_num_beacons" after
695 * the first association, then we're done forever.
696 */
f0832f13
EG
697 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
698 if (data->state == IWL_CHAIN_NOISE_ALIVE)
e1623446 699 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
f0832f13
EG
700 return;
701 }
702
703 spin_lock_irqsave(&priv->lock, flags);
704 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
e1623446 705 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
f0832f13
EG
706 spin_unlock_irqrestore(&priv->lock, flags);
707 return;
708 }
709
710 rxon_band24 = !!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK);
711 rxon_chnum = le16_to_cpu(priv->staging_rxon.channel);
712 stat_band24 = !!(stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
713 stat_chnum = le32_to_cpu(stat_resp->flag) >> 16;
714
715 /* Make sure we accumulate data for just the associated channel
716 * (even if scanning). */
717 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
e1623446 718 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
f0832f13
EG
719 rxon_chnum, rxon_band24);
720 spin_unlock_irqrestore(&priv->lock, flags);
721 return;
722 }
723
d8c07e7a
WYG
724 /*
725 * Accumulate beacon statistics values across
726 * "chain_noise_num_beacons"
727 */
f0832f13
EG
728 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
729 IN_BAND_FILTER;
730 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
731 IN_BAND_FILTER;
732 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
733 IN_BAND_FILTER;
734
735 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
736 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
737 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
738
739 spin_unlock_irqrestore(&priv->lock, flags);
740
741 data->beacon_count++;
742
743 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
744 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
745 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
746
747 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
748 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
749 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
750
e1623446 751 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
f0832f13 752 rxon_chnum, rxon_band24, data->beacon_count);
e1623446 753 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
f0832f13 754 chain_sig_a, chain_sig_b, chain_sig_c);
e1623446 755 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
f0832f13
EG
756 chain_noise_a, chain_noise_b, chain_noise_c);
757
d8c07e7a 758 /* If this is the "chain_noise_num_beacons", determine:
f0832f13
EG
759 * 1) Disconnected antennas (using signal strengths)
760 * 2) Differential gain (using silence noise) to balance receivers */
d8c07e7a 761 if (data->beacon_count != priv->cfg->chain_noise_num_beacons)
f0832f13
EG
762 return;
763
764 /* Analyze signal for disconnected antenna */
d8c07e7a
WYG
765 average_sig[0] =
766 (data->chain_signal_a) / priv->cfg->chain_noise_num_beacons;
767 average_sig[1] =
768 (data->chain_signal_b) / priv->cfg->chain_noise_num_beacons;
769 average_sig[2] =
770 (data->chain_signal_c) / priv->cfg->chain_noise_num_beacons;
f0832f13
EG
771
772 if (average_sig[0] >= average_sig[1]) {
773 max_average_sig = average_sig[0];
774 max_average_sig_antenna_i = 0;
775 active_chains = (1 << max_average_sig_antenna_i);
776 } else {
777 max_average_sig = average_sig[1];
778 max_average_sig_antenna_i = 1;
779 active_chains = (1 << max_average_sig_antenna_i);
780 }
781
782 if (average_sig[2] >= max_average_sig) {
783 max_average_sig = average_sig[2];
784 max_average_sig_antenna_i = 2;
785 active_chains = (1 << max_average_sig_antenna_i);
786 }
787
e1623446 788 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
f0832f13 789 average_sig[0], average_sig[1], average_sig[2]);
e1623446 790 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
f0832f13
EG
791 max_average_sig, max_average_sig_antenna_i);
792
793 /* Compare signal strengths for all 3 receivers. */
794 for (i = 0; i < NUM_RX_CHAINS; i++) {
795 if (i != max_average_sig_antenna_i) {
796 s32 rssi_delta = (max_average_sig - average_sig[i]);
797
798 /* If signal is very weak, compared with
799 * strongest, mark it as disconnected. */
800 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
801 data->disconn_array[i] = 1;
802 else
803 active_chains |= (1 << i);
e1623446 804 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
f0832f13
EG
805 "disconn_array[i] = %d\n",
806 i, rssi_delta, data->disconn_array[i]);
807 }
808 }
809
810 num_tx_chains = 0;
811 for (i = 0; i < NUM_RX_CHAINS; i++) {
812 /* loops on all the bits of
813 * priv->hw_setting.valid_tx_ant */
814 u8 ant_msk = (1 << i);
815 if (!(priv->hw_params.valid_tx_ant & ant_msk))
816 continue;
817
818 num_tx_chains++;
819 if (data->disconn_array[i] == 0)
820 /* there is a Tx antenna connected */
821 break;
822 if (num_tx_chains == priv->hw_params.tx_chains_num &&
d8c07e7a
WYG
823 data->disconn_array[i]) {
824 /*
825 * If all chains are disconnected
826 * connect the first valid tx chain
827 */
828 first_chain =
829 find_first_chain(priv->cfg->valid_tx_ant);
830 data->disconn_array[first_chain] = 0;
831 active_chains |= BIT(first_chain);
832 IWL_DEBUG_CALIB(priv, "All Tx chains are disconnected W/A - declare %d as connected\n",
833 first_chain);
f0832f13
EG
834 break;
835 }
836 }
837
04816448
GE
838 /* Save for use within RXON, TX, SCAN commands, etc. */
839 priv->chain_noise_data.active_chains = active_chains;
e1623446 840 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
f0832f13
EG
841 active_chains);
842
f0832f13 843 /* Analyze noise for rx balance */
d8c07e7a
WYG
844 average_noise[0] =
845 ((data->chain_noise_a) / priv->cfg->chain_noise_num_beacons);
846 average_noise[1] =
847 ((data->chain_noise_b) / priv->cfg->chain_noise_num_beacons);
848 average_noise[2] =
849 ((data->chain_noise_c) / priv->cfg->chain_noise_num_beacons);
f0832f13
EG
850
851 for (i = 0; i < NUM_RX_CHAINS; i++) {
852 if (!(data->disconn_array[i]) &&
853 (average_noise[i] <= min_average_noise)) {
854 /* This means that chain i is active and has
855 * lower noise values so far: */
856 min_average_noise = average_noise[i];
857 min_average_noise_antenna_i = i;
858 }
859 }
860
e1623446 861 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
f0832f13
EG
862 average_noise[0], average_noise[1],
863 average_noise[2]);
864
e1623446 865 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
f0832f13
EG
866 min_average_noise, min_average_noise_antenna_i);
867
29f35c14
JS
868 if (priv->cfg->ops->utils->gain_computation)
869 priv->cfg->ops->utils->gain_computation(priv, average_noise,
d8c07e7a
WYG
870 min_average_noise_antenna_i, min_average_noise,
871 find_first_chain(priv->cfg->valid_rx_ant));
04816448
GE
872
873 /* Some power changes may have been made during the calibration.
874 * Update and commit the RXON
875 */
876 if (priv->cfg->ops->lib->update_chain_flags)
877 priv->cfg->ops->lib->update_chain_flags(priv);
878
879 data->state = IWL_CHAIN_NOISE_DONE;
e312c24c 880 iwl_power_update_mode(priv, false);
f0832f13
EG
881}
882EXPORT_SYMBOL(iwl_chain_noise_calibration);
883
4a4a9e81
TW
884
885void iwl_reset_run_time_calib(struct iwl_priv *priv)
886{
887 int i;
888 memset(&(priv->sensitivity_data), 0,
889 sizeof(struct iwl_sensitivity_data));
890 memset(&(priv->chain_noise_data), 0,
891 sizeof(struct iwl_chain_noise_data));
892 for (i = 0; i < NUM_RX_CHAINS; i++)
893 priv->chain_noise_data.delta_gain_code[i] =
894 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
895
896 /* Ask for statistics now, the uCode will send notification
897 * periodically after association */
ef8d5529 898 iwl_send_statistics_request(priv, CMD_ASYNC, true);
4a4a9e81
TW
899}
900EXPORT_SYMBOL(iwl_reset_run_time_calib);
901