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