blob: 02c7c65ee86a53c77604996fdbf6c6b9b16950c3 [file] [log] [blame]
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001/******************************************************************************
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 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07008 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07009 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080028 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070029 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -070033 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070034 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070064#include <net/mac80211.h>
65
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070066#include "iwl-dev.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070067#include "iwl-core.h"
Johannes Berg0de76732010-09-22 18:02:11 +020068#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070069#include "iwl-trans.h"
Don Fry5c3d29f2011-07-08 08:46:29 -070070#include "iwl-agn.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070071
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080072/*****************************************************************************
73 * INIT calibrations framework
74 *****************************************************************************/
75
Winkler, Tomas34c22cf2008-12-17 16:52:27 +080076struct statistics_general_data {
77 u32 beacon_silence_rssi_a;
78 u32 beacon_silence_rssi_b;
79 u32 beacon_silence_rssi_c;
80 u32 beacon_energy_a;
81 u32 beacon_energy_b;
82 u32 beacon_energy_c;
83};
84
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080085int iwl_send_calib_results(struct iwl_priv *priv)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080086{
87 int ret = 0;
88 int i = 0;
89
90 struct iwl_host_cmd hcmd = {
91 .id = REPLY_PHY_CALIBRATION_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070092 .flags = CMD_SYNC,
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080093 };
94
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080095 for (i = 0; i < IWL_CALIB_MAX; i++) {
96 if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
97 priv->calib_results[i].buf) {
Johannes Berg3fa50732011-05-04 07:50:38 -070098 hcmd.len[0] = priv->calib_results[i].buf_len;
99 hcmd.data[0] = priv->calib_results[i].buf;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700100 hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700101 ret = trans_send_cmd(priv, &hcmd);
Wey-Yi Guy936e8a72010-06-14 17:09:56 -0700102 if (ret) {
103 IWL_ERR(priv, "Error %d iteration %d\n",
104 ret, i);
105 break;
106 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800107 }
Tomas Winklerbe5d56e2008-10-08 09:37:27 +0800108 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800109
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800110 return ret;
111}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800112
113int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
114{
115 if (res->buf_len != len) {
116 kfree(res->buf);
117 res->buf = kzalloc(len, GFP_ATOMIC);
118 }
119 if (unlikely(res->buf == NULL))
120 return -ENOMEM;
121
122 res->buf_len = len;
123 memcpy(res->buf, buf, len);
124 return 0;
125}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800126
127void iwl_calib_free_results(struct iwl_priv *priv)
128{
129 int i;
130
131 for (i = 0; i < IWL_CALIB_MAX; i++) {
132 kfree(priv->calib_results[i].buf);
133 priv->calib_results[i].buf = NULL;
134 priv->calib_results[i].buf_len = 0;
135 }
136}
137
138/*****************************************************************************
139 * RUNTIME calibrations framework
140 *****************************************************************************/
141
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700142/* "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 }
Tomas Winklere1623442009-01-27 14:27:56 -0800207 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700208 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
Tomas Winklere1623442009-01-27 14:27:56 -0800231 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700232 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;
Tomas Winklere1623442009-01-27 14:27:56 -0800241 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700242 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)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800247 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700248 false_alarms, max_false_alarms);
Tomas Winklere1623442009-01-27 14:27:56 -0800249 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700250 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 */
Wey-Yi Guyfe6efb42009-06-12 13:22:54 -0700256 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700257 /* 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
Tomas Winklere1623442009-01-27 14:27:56 -0800266 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700267 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
Tomas Winklere1623442009-01-27 14:27:56 -0800280 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700281 /* 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 {
Tomas Winklere1623442009-01-27 14:27:56 -0800285 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700286 }
287
288 /* Else we got a healthy number of false alarms, keep status quo */
289 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800290 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700291 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) {
Tomas Winklere1623442009-01-27 14:27:56 -0800300 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700301 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);
Tomas Winklere1623442009-01-27 14:27:56 -0800314 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700315
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
Tomas Winklere1623442009-01-27 14:27:56 -0800367 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700368 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
Tomas Winklere1623442009-01-27 14:27:56 -0800390 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700391 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 {
Tomas Winklere1623442009-01-27 14:27:56 -0800409 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700410 min_false_alarms, false_alarms, max_false_alarms);
411 }
412 return 0;
413}
414
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700415static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
416 struct iwl_sensitivity_data *data,
417 __le16 *tbl)
418{
419 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
420 cpu_to_le16((u16)data->auto_corr_ofdm);
421 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
422 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
423 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
424 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
425 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
426 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
427
428 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
429 cpu_to_le16((u16)data->auto_corr_cck);
430 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
431 cpu_to_le16((u16)data->auto_corr_cck_mrc);
432
433 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
434 cpu_to_le16((u16)data->nrg_th_cck);
435 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
436 cpu_to_le16((u16)data->nrg_th_ofdm);
437
438 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
439 cpu_to_le16(data->barker_corr_th_min);
440 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
441 cpu_to_le16(data->barker_corr_th_min_mrc);
442 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
443 cpu_to_le16(data->nrg_th_cca);
444
445 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
446 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
447 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
448 data->nrg_th_ofdm);
449
450 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
451 data->auto_corr_cck, data->auto_corr_cck_mrc,
452 data->nrg_th_cck);
453}
454
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700455/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
456static int iwl_sensitivity_write(struct iwl_priv *priv)
457{
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700458 struct iwl_sensitivity_cmd cmd;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700459 struct iwl_sensitivity_data *data = NULL;
460 struct iwl_host_cmd cmd_out = {
461 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700462 .len = { sizeof(struct iwl_sensitivity_cmd), },
Johannes Bergc2acea82009-07-24 11:13:05 -0700463 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700464 .data = { &cmd, },
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700465 };
466
467 data = &(priv->sensitivity_data);
468
469 memset(&cmd, 0, sizeof(cmd));
470
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700471 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700472
473 /* Update uCode's "work" table, and copy it to DSP */
474 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
475
476 /* Don't send command to uCode if nothing has changed */
477 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
478 sizeof(u16)*HD_TABLE_SIZE)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800479 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700480 return 0;
481 }
482
483 /* Copy table for comparison next time */
484 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
485 sizeof(u16)*HD_TABLE_SIZE);
486
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700487 return trans_send_cmd(priv, &cmd_out);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700488}
489
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700490/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
491static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
492{
493 struct iwl_enhance_sensitivity_cmd cmd;
494 struct iwl_sensitivity_data *data = NULL;
495 struct iwl_host_cmd cmd_out = {
496 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700497 .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700498 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700499 .data = { &cmd, },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700500 };
501
502 data = &(priv->sensitivity_data);
503
504 memset(&cmd, 0, sizeof(cmd));
505
506 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
507
508 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
509 HD_INA_NON_SQUARE_DET_OFDM_DATA;
510 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
511 HD_INA_NON_SQUARE_DET_CCK_DATA;
512 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
513 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA;
514 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
515 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA;
516 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
517 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA;
518 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
519 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA;
520 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
521 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA;
522 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
523 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA;
524 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
525 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA;
526 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
527 HD_CCK_NON_SQUARE_DET_SLOPE_DATA;
528 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
529 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA;
530
531 /* Update uCode's "work" table, and copy it to DSP */
532 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
533
534 /* Don't send command to uCode if nothing has changed */
535 if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
536 sizeof(u16)*HD_TABLE_SIZE) &&
537 !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
538 &(priv->enhance_sensitivity_tbl[0]),
539 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
540 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
541 return 0;
542 }
543
544 /* Copy table for comparison next time */
545 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
546 sizeof(u16)*HD_TABLE_SIZE);
547 memcpy(&(priv->enhance_sensitivity_tbl[0]),
548 &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
549 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
550
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700551 return trans_send_cmd(priv, &cmd_out);
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700552}
553
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700554void iwl_init_sensitivity(struct iwl_priv *priv)
555{
556 int ret = 0;
557 int i;
558 struct iwl_sensitivity_data *data = NULL;
559 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
560
Tomas Winkler445c2df2008-05-15 13:54:16 +0800561 if (priv->disable_sens_cal)
562 return;
563
Tomas Winklere1623442009-01-27 14:27:56 -0800564 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700565
566 /* Clear driver's sensitivity algo data */
567 data = &(priv->sensitivity_data);
568
569 if (ranges == NULL)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700570 return;
571
572 memset(data, 0, sizeof(struct iwl_sensitivity_data));
573
574 data->num_in_cck_no_fa = 0;
575 data->nrg_curr_state = IWL_FA_TOO_MANY;
576 data->nrg_prev_state = IWL_FA_TOO_MANY;
577 data->nrg_silence_ref = 0;
578 data->nrg_silence_idx = 0;
579 data->nrg_energy_idx = 0;
580
581 for (i = 0; i < 10; i++)
582 data->nrg_value[i] = 0;
583
584 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
585 data->nrg_silence_rssi[i] = 0;
586
Wey-Yi Guyf3a2a422009-09-11 10:38:11 -0700587 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700588 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
589 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
590 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
591 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
592 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
593 data->nrg_th_cck = ranges->nrg_th_cck;
594 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700595 data->barker_corr_th_min = ranges->barker_corr_th_min;
596 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
597 data->nrg_th_cca = ranges->nrg_th_cca;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700598
599 data->last_bad_plcp_cnt_ofdm = 0;
600 data->last_fa_cnt_ofdm = 0;
601 data->last_bad_plcp_cnt_cck = 0;
602 data->last_fa_cnt_cck = 0;
603
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700604 if (priv->enhance_sensitivity_table)
605 ret |= iwl_enhance_sensitivity_write(priv);
606 else
607 ret |= iwl_sensitivity_write(priv);
Tomas Winklere1623442009-01-27 14:27:56 -0800608 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700609}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700610
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700611void iwl_sensitivity_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700612{
613 u32 rx_enable_time;
614 u32 fa_cck;
615 u32 fa_ofdm;
616 u32 bad_plcp_cck;
617 u32 bad_plcp_ofdm;
618 u32 norm_fa_ofdm;
619 u32 norm_fa_cck;
620 struct iwl_sensitivity_data *data = NULL;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700621 struct statistics_rx_non_phy *rx_info;
622 struct statistics_rx_phy *ofdm, *cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700623 unsigned long flags;
624 struct statistics_general_data statis;
625
Tomas Winkler445c2df2008-05-15 13:54:16 +0800626 if (priv->disable_sens_cal)
627 return;
628
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700629 data = &(priv->sensitivity_data);
630
Johannes Berg246ed352010-08-23 10:46:32 +0200631 if (!iwl_is_any_associated(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800632 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700633 return;
634 }
635
636 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700637 rx_info = &priv->statistics.rx_non_phy;
638 ofdm = &priv->statistics.rx_ofdm;
639 cck = &priv->statistics.rx_cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700640 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800641 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700642 spin_unlock_irqrestore(&priv->lock, flags);
643 return;
644 }
645
646 /* Extract Statistics: */
647 rx_enable_time = le32_to_cpu(rx_info->channel_load);
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700648 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
649 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
650 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
651 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700652
653 statis.beacon_silence_rssi_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700654 le32_to_cpu(rx_info->beacon_silence_rssi_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700655 statis.beacon_silence_rssi_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700656 le32_to_cpu(rx_info->beacon_silence_rssi_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700657 statis.beacon_silence_rssi_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700658 le32_to_cpu(rx_info->beacon_silence_rssi_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700659 statis.beacon_energy_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700660 le32_to_cpu(rx_info->beacon_energy_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700661 statis.beacon_energy_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700662 le32_to_cpu(rx_info->beacon_energy_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700663 statis.beacon_energy_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700664 le32_to_cpu(rx_info->beacon_energy_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700665
666 spin_unlock_irqrestore(&priv->lock, flags);
667
Tomas Winklere1623442009-01-27 14:27:56 -0800668 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700669
670 if (!rx_enable_time) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700671 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700672 return;
673 }
674
675 /* These statistics increase monotonically, and do not reset
676 * at each beacon. Calculate difference from last value, or just
677 * use the new statistics value if it has reset or wrapped around. */
678 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
679 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
680 else {
681 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
682 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
683 }
684
685 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
686 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
687 else {
688 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
689 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
690 }
691
692 if (data->last_fa_cnt_ofdm > fa_ofdm)
693 data->last_fa_cnt_ofdm = fa_ofdm;
694 else {
695 fa_ofdm -= data->last_fa_cnt_ofdm;
696 data->last_fa_cnt_ofdm += fa_ofdm;
697 }
698
699 if (data->last_fa_cnt_cck > fa_cck)
700 data->last_fa_cnt_cck = fa_cck;
701 else {
702 fa_cck -= data->last_fa_cnt_cck;
703 data->last_fa_cnt_cck += fa_cck;
704 }
705
706 /* Total aborted signal locks */
707 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
708 norm_fa_cck = fa_cck + bad_plcp_cck;
709
Tomas Winklere1623442009-01-27 14:27:56 -0800710 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700711 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
712
713 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
714 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700715 if (priv->enhance_sensitivity_table)
716 iwl_enhance_sensitivity_write(priv);
717 else
718 iwl_sensitivity_write(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700719}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700720
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700721static inline u8 find_first_chain(u8 mask)
722{
723 if (mask & ANT_A)
724 return CHAIN_A;
725 if (mask & ANT_B)
726 return CHAIN_B;
727 return CHAIN_C;
728}
729
Shanyu Zhao30312422010-11-10 09:56:48 -0800730/**
731 * Run disconnected antenna algorithm to find out which antennas are
732 * disconnected.
733 */
734static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
735 struct iwl_chain_noise_data *data)
736{
737 u32 active_chains = 0;
738 u32 max_average_sig;
739 u16 max_average_sig_antenna_i;
740 u8 num_tx_chains;
741 u8 first_chain;
742 u16 i = 0;
743
744 average_sig[0] = data->chain_signal_a /
745 priv->cfg->base_params->chain_noise_num_beacons;
746 average_sig[1] = data->chain_signal_b /
747 priv->cfg->base_params->chain_noise_num_beacons;
748 average_sig[2] = data->chain_signal_c /
749 priv->cfg->base_params->chain_noise_num_beacons;
750
751 if (average_sig[0] >= average_sig[1]) {
752 max_average_sig = average_sig[0];
753 max_average_sig_antenna_i = 0;
754 active_chains = (1 << max_average_sig_antenna_i);
755 } else {
756 max_average_sig = average_sig[1];
757 max_average_sig_antenna_i = 1;
758 active_chains = (1 << max_average_sig_antenna_i);
759 }
760
761 if (average_sig[2] >= max_average_sig) {
762 max_average_sig = average_sig[2];
763 max_average_sig_antenna_i = 2;
764 active_chains = (1 << max_average_sig_antenna_i);
765 }
766
767 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
768 average_sig[0], average_sig[1], average_sig[2]);
769 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
770 max_average_sig, max_average_sig_antenna_i);
771
772 /* Compare signal strengths for all 3 receivers. */
773 for (i = 0; i < NUM_RX_CHAINS; i++) {
774 if (i != max_average_sig_antenna_i) {
775 s32 rssi_delta = (max_average_sig - average_sig[i]);
776
777 /* If signal is very weak, compared with
778 * strongest, mark it as disconnected. */
779 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
780 data->disconn_array[i] = 1;
781 else
782 active_chains |= (1 << i);
783 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
784 "disconn_array[i] = %d\n",
785 i, rssi_delta, data->disconn_array[i]);
786 }
787 }
788
789 /*
790 * The above algorithm sometimes fails when the ucode
791 * reports 0 for all chains. It's not clear why that
792 * happens to start with, but it is then causing trouble
793 * because this can make us enable more chains than the
794 * hardware really has.
795 *
796 * To be safe, simply mask out any chains that we know
797 * are not on the device.
798 */
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -0800799 active_chains &= priv->hw_params.valid_rx_ant;
Shanyu Zhao30312422010-11-10 09:56:48 -0800800
801 num_tx_chains = 0;
802 for (i = 0; i < NUM_RX_CHAINS; i++) {
803 /* loops on all the bits of
804 * priv->hw_setting.valid_tx_ant */
805 u8 ant_msk = (1 << i);
806 if (!(priv->hw_params.valid_tx_ant & ant_msk))
807 continue;
808
809 num_tx_chains++;
810 if (data->disconn_array[i] == 0)
811 /* there is a Tx antenna connected */
812 break;
813 if (num_tx_chains == priv->hw_params.tx_chains_num &&
814 data->disconn_array[i]) {
815 /*
816 * If all chains are disconnected
817 * connect the first valid tx chain
818 */
819 first_chain =
820 find_first_chain(priv->cfg->valid_tx_ant);
821 data->disconn_array[first_chain] = 0;
822 active_chains |= BIT(first_chain);
Joe Perches85ee7a12011-04-23 20:38:19 -0700823 IWL_DEBUG_CALIB(priv,
824 "All Tx chains are disconnected W/A - declare %d as connected\n",
Shanyu Zhao30312422010-11-10 09:56:48 -0800825 first_chain);
826 break;
827 }
828 }
829
830 if (active_chains != priv->hw_params.valid_rx_ant &&
831 active_chains != priv->chain_noise_data.active_chains)
832 IWL_DEBUG_CALIB(priv,
833 "Detected that not all antennas are connected! "
834 "Connected: %#x, valid: %#x.\n",
835 active_chains, priv->hw_params.valid_rx_ant);
836
837 /* Save for use within RXON, TX, SCAN commands, etc. */
838 data->active_chains = active_chains;
839 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
840 active_chains);
841}
842
843
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700844/*
Shanyu Zhao30312422010-11-10 09:56:48 -0800845 * Accumulate 16 beacons of signal and noise statistics for each of
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700846 * 3 receivers/antennas/rx-chains, then figure out:
847 * 1) Which antennas are connected.
848 * 2) Differential rx gain settings to balance the 3 receivers.
849 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700850void iwl_chain_noise_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700851{
852 struct iwl_chain_noise_data *data = NULL;
853
854 u32 chain_noise_a;
855 u32 chain_noise_b;
856 u32 chain_noise_c;
857 u32 chain_sig_a;
858 u32 chain_sig_b;
859 u32 chain_sig_c;
860 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
861 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700862 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
863 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
864 u16 i = 0;
865 u16 rxon_chnum = INITIALIZATION_VALUE;
866 u16 stat_chnum = INITIALIZATION_VALUE;
867 u8 rxon_band24;
868 u8 stat_band24;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700869 unsigned long flags;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700870 struct statistics_rx_non_phy *rx_info;
Shanyu Zhao30312422010-11-10 09:56:48 -0800871
Johannes Berg246ed352010-08-23 10:46:32 +0200872 /*
873 * MULTI-FIXME:
874 * When we support multiple interfaces on different channels,
875 * this must be modified/fixed.
876 */
877 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700878
Tomas Winkler445c2df2008-05-15 13:54:16 +0800879 if (priv->disable_chain_noise_cal)
880 return;
881
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700882 data = &(priv->chain_noise_data);
883
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700884 /*
885 * Accumulate just the first "chain_noise_num_beacons" after
886 * the first association, then we're done forever.
887 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700888 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
889 if (data->state == IWL_CHAIN_NOISE_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -0800890 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700891 return;
892 }
893
894 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700895
896 rx_info = &priv->statistics.rx_non_phy;
897
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700898 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800899 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700900 spin_unlock_irqrestore(&priv->lock, flags);
901 return;
902 }
903
Johannes Berg246ed352010-08-23 10:46:32 +0200904 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
905 rxon_chnum = le16_to_cpu(ctx->staging.channel);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700906 stat_band24 =
907 !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
908 stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700909
910 /* Make sure we accumulate data for just the associated channel
911 * (even if scanning). */
912 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800913 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700914 rxon_chnum, rxon_band24);
915 spin_unlock_irqrestore(&priv->lock, flags);
916 return;
917 }
918
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700919 /*
920 * Accumulate beacon statistics values across
921 * "chain_noise_num_beacons"
922 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700923 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
924 IN_BAND_FILTER;
925 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
926 IN_BAND_FILTER;
927 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
928 IN_BAND_FILTER;
929
930 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
931 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
932 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
933
934 spin_unlock_irqrestore(&priv->lock, flags);
935
936 data->beacon_count++;
937
938 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
939 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
940 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
941
942 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
943 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
944 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
945
Tomas Winklere1623442009-01-27 14:27:56 -0800946 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700947 rxon_chnum, rxon_band24, data->beacon_count);
Tomas Winklere1623442009-01-27 14:27:56 -0800948 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700949 chain_sig_a, chain_sig_b, chain_sig_c);
Tomas Winklere1623442009-01-27 14:27:56 -0800950 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700951 chain_noise_a, chain_noise_b, chain_noise_c);
952
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700953 /* If this is the "chain_noise_num_beacons", determine:
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700954 * 1) Disconnected antennas (using signal strengths)
955 * 2) Differential gain (using silence noise) to balance receivers */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700956 if (data->beacon_count !=
957 priv->cfg->base_params->chain_noise_num_beacons)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700958 return;
959
960 /* Analyze signal for disconnected antenna */
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -0800961 if (priv->cfg->bt_params &&
962 priv->cfg->bt_params->advanced_bt_coexist) {
963 /* Disable disconnected antenna algorithm for advanced
964 bt coex, assuming valid antennas are connected */
965 data->active_chains = priv->hw_params.valid_rx_ant;
966 for (i = 0; i < NUM_RX_CHAINS; i++)
967 if (!(data->active_chains & (1<<i)))
968 data->disconn_array[i] = 1;
969 } else
970 iwl_find_disconn_antenna(priv, average_sig, data);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700971
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700972 /* Analyze noise for rx balance */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700973 average_noise[0] = data->chain_noise_a /
974 priv->cfg->base_params->chain_noise_num_beacons;
975 average_noise[1] = data->chain_noise_b /
976 priv->cfg->base_params->chain_noise_num_beacons;
977 average_noise[2] = data->chain_noise_c /
978 priv->cfg->base_params->chain_noise_num_beacons;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700979
980 for (i = 0; i < NUM_RX_CHAINS; i++) {
981 if (!(data->disconn_array[i]) &&
982 (average_noise[i] <= min_average_noise)) {
983 /* This means that chain i is active and has
984 * lower noise values so far: */
985 min_average_noise = average_noise[i];
986 min_average_noise_antenna_i = i;
987 }
988 }
989
Tomas Winklere1623442009-01-27 14:27:56 -0800990 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700991 average_noise[0], average_noise[1],
992 average_noise[2]);
993
Tomas Winklere1623442009-01-27 14:27:56 -0800994 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700995 min_average_noise, min_average_noise_antenna_i);
996
Don Fry5c3d29f2011-07-08 08:46:29 -0700997 iwlagn_gain_computation(priv, average_noise,
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700998 min_average_noise_antenna_i, min_average_noise,
999 find_first_chain(priv->cfg->valid_rx_ant));
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08001000
1001 /* Some power changes may have been made during the calibration.
1002 * Update and commit the RXON
1003 */
Fry, Donald H6b6db912011-07-08 08:46:17 -07001004 iwl_update_chain_flags(priv);
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08001005
1006 data->state = IWL_CHAIN_NOISE_DONE;
Johannes Berge312c242009-08-07 15:41:51 -07001007 iwl_power_update_mode(priv, false);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001008}
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001009
1010void iwl_reset_run_time_calib(struct iwl_priv *priv)
1011{
1012 int i;
1013 memset(&(priv->sensitivity_data), 0,
1014 sizeof(struct iwl_sensitivity_data));
1015 memset(&(priv->chain_noise_data), 0,
1016 sizeof(struct iwl_chain_noise_data));
1017 for (i = 0; i < NUM_RX_CHAINS; i++)
1018 priv->chain_noise_data.delta_gain_code[i] =
1019 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
1020
1021 /* Ask for statistics now, the uCode will send notification
1022 * periodically after association */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -08001023 iwl_send_statistics_request(priv, CMD_ASYNC, true);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001024}