blob: 710ada9c41325b6f2816ee23340e322a4af93013 [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"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070070
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080071/*****************************************************************************
72 * INIT calibrations framework
73 *****************************************************************************/
74
Winkler, Tomas34c22cf2008-12-17 16:52:27 +080075struct statistics_general_data {
76 u32 beacon_silence_rssi_a;
77 u32 beacon_silence_rssi_b;
78 u32 beacon_silence_rssi_c;
79 u32 beacon_energy_a;
80 u32 beacon_energy_b;
81 u32 beacon_energy_c;
82};
83
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080084int iwl_send_calib_results(struct iwl_priv *priv)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080085{
86 int ret = 0;
87 int i = 0;
88
89 struct iwl_host_cmd hcmd = {
90 .id = REPLY_PHY_CALIBRATION_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070091 .flags = CMD_SYNC,
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080092 };
93
Tomas Winklerbe5d56e2008-10-08 09:37:27 +080094 for (i = 0; i < IWL_CALIB_MAX; i++) {
95 if ((BIT(i) & priv->hw_params.calib_init_cfg) &&
96 priv->calib_results[i].buf) {
Johannes Berg3fa50732011-05-04 07:50:38 -070097 hcmd.len[0] = priv->calib_results[i].buf_len;
98 hcmd.data[0] = priv->calib_results[i].buf;
Johannes Berg4ce7cc22011-05-13 11:57:40 -070099 hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700100 ret = trans_send_cmd(priv, &hcmd);
Wey-Yi Guy936e8a72010-06-14 17:09:56 -0700101 if (ret) {
102 IWL_ERR(priv, "Error %d iteration %d\n",
103 ret, i);
104 break;
105 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800106 }
Tomas Winklerbe5d56e2008-10-08 09:37:27 +0800107 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800108
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800109 return ret;
110}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800111
112int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len)
113{
114 if (res->buf_len != len) {
115 kfree(res->buf);
116 res->buf = kzalloc(len, GFP_ATOMIC);
117 }
118 if (unlikely(res->buf == NULL))
119 return -ENOMEM;
120
121 res->buf_len = len;
122 memcpy(res->buf, buf, len);
123 return 0;
124}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800125
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}
136
137/*****************************************************************************
138 * RUNTIME calibrations framework
139 *****************************************************************************/
140
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700141/* "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 }
Tomas Winklere1623442009-01-27 14:27:56 -0800206 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700207 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
Tomas Winklere1623442009-01-27 14:27:56 -0800230 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 -0700231 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;
Tomas Winklere1623442009-01-27 14:27:56 -0800240 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700241 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)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800246 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700247 false_alarms, max_false_alarms);
Tomas Winklere1623442009-01-27 14:27:56 -0800248 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700249 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 */
Wey-Yi Guyfe6efb42009-06-12 13:22:54 -0700255 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700256 /* 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
Tomas Winklere1623442009-01-27 14:27:56 -0800265 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700266 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
Tomas Winklere1623442009-01-27 14:27:56 -0800279 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700280 /* 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 {
Tomas Winklere1623442009-01-27 14:27:56 -0800284 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700285 }
286
287 /* Else we got a healthy number of false alarms, keep status quo */
288 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800289 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700290 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) {
Tomas Winklere1623442009-01-27 14:27:56 -0800299 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700300 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);
Tomas Winklere1623442009-01-27 14:27:56 -0800313 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700314
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
Tomas Winklere1623442009-01-27 14:27:56 -0800366 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700367 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
Tomas Winklere1623442009-01-27 14:27:56 -0800389 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700390 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 {
Tomas Winklere1623442009-01-27 14:27:56 -0800408 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700409 min_false_alarms, false_alarms, max_false_alarms);
410 }
411 return 0;
412}
413
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700414static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
415 struct iwl_sensitivity_data *data,
416 __le16 *tbl)
417{
418 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
419 cpu_to_le16((u16)data->auto_corr_ofdm);
420 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
421 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
422 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
423 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
424 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
425 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
426
427 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
428 cpu_to_le16((u16)data->auto_corr_cck);
429 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
430 cpu_to_le16((u16)data->auto_corr_cck_mrc);
431
432 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
433 cpu_to_le16((u16)data->nrg_th_cck);
434 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
435 cpu_to_le16((u16)data->nrg_th_ofdm);
436
437 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
438 cpu_to_le16(data->barker_corr_th_min);
439 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
440 cpu_to_le16(data->barker_corr_th_min_mrc);
441 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
442 cpu_to_le16(data->nrg_th_cca);
443
444 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
445 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
446 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
447 data->nrg_th_ofdm);
448
449 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
450 data->auto_corr_cck, data->auto_corr_cck_mrc,
451 data->nrg_th_cck);
452}
453
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700454/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
455static int iwl_sensitivity_write(struct iwl_priv *priv)
456{
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700457 struct iwl_sensitivity_cmd cmd;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700458 struct iwl_sensitivity_data *data = NULL;
459 struct iwl_host_cmd cmd_out = {
460 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700461 .len = { sizeof(struct iwl_sensitivity_cmd), },
Johannes Bergc2acea82009-07-24 11:13:05 -0700462 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700463 .data = { &cmd, },
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700464 };
465
466 data = &(priv->sensitivity_data);
467
468 memset(&cmd, 0, sizeof(cmd));
469
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700470 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700471
472 /* Update uCode's "work" table, and copy it to DSP */
473 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
474
475 /* Don't send command to uCode if nothing has changed */
476 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
477 sizeof(u16)*HD_TABLE_SIZE)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800478 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700479 return 0;
480 }
481
482 /* Copy table for comparison next time */
483 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
484 sizeof(u16)*HD_TABLE_SIZE);
485
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700486 return trans_send_cmd(priv, &cmd_out);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700487}
488
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700489/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
490static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
491{
492 struct iwl_enhance_sensitivity_cmd cmd;
493 struct iwl_sensitivity_data *data = NULL;
494 struct iwl_host_cmd cmd_out = {
495 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700496 .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700497 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700498 .data = { &cmd, },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700499 };
500
501 data = &(priv->sensitivity_data);
502
503 memset(&cmd, 0, sizeof(cmd));
504
505 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
506
507 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
508 HD_INA_NON_SQUARE_DET_OFDM_DATA;
509 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
510 HD_INA_NON_SQUARE_DET_CCK_DATA;
511 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
512 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA;
513 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
514 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA;
515 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
516 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA;
517 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
518 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA;
519 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
520 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA;
521 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
522 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA;
523 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
524 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA;
525 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
526 HD_CCK_NON_SQUARE_DET_SLOPE_DATA;
527 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
528 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA;
529
530 /* Update uCode's "work" table, and copy it to DSP */
531 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
532
533 /* Don't send command to uCode if nothing has changed */
534 if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
535 sizeof(u16)*HD_TABLE_SIZE) &&
536 !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
537 &(priv->enhance_sensitivity_tbl[0]),
538 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
539 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
540 return 0;
541 }
542
543 /* Copy table for comparison next time */
544 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
545 sizeof(u16)*HD_TABLE_SIZE);
546 memcpy(&(priv->enhance_sensitivity_tbl[0]),
547 &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
548 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
549
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700550 return trans_send_cmd(priv, &cmd_out);
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700551}
552
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700553void iwl_init_sensitivity(struct iwl_priv *priv)
554{
555 int ret = 0;
556 int i;
557 struct iwl_sensitivity_data *data = NULL;
558 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
559
Tomas Winkler445c2df2008-05-15 13:54:16 +0800560 if (priv->disable_sens_cal)
561 return;
562
Tomas Winklere1623442009-01-27 14:27:56 -0800563 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700564
565 /* Clear driver's sensitivity algo data */
566 data = &(priv->sensitivity_data);
567
568 if (ranges == NULL)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700569 return;
570
571 memset(data, 0, sizeof(struct iwl_sensitivity_data));
572
573 data->num_in_cck_no_fa = 0;
574 data->nrg_curr_state = IWL_FA_TOO_MANY;
575 data->nrg_prev_state = IWL_FA_TOO_MANY;
576 data->nrg_silence_ref = 0;
577 data->nrg_silence_idx = 0;
578 data->nrg_energy_idx = 0;
579
580 for (i = 0; i < 10; i++)
581 data->nrg_value[i] = 0;
582
583 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
584 data->nrg_silence_rssi[i] = 0;
585
Wey-Yi Guyf3a2a422009-09-11 10:38:11 -0700586 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700587 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
588 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
589 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
590 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
591 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
592 data->nrg_th_cck = ranges->nrg_th_cck;
593 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700594 data->barker_corr_th_min = ranges->barker_corr_th_min;
595 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
596 data->nrg_th_cca = ranges->nrg_th_cca;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700597
598 data->last_bad_plcp_cnt_ofdm = 0;
599 data->last_fa_cnt_ofdm = 0;
600 data->last_bad_plcp_cnt_cck = 0;
601 data->last_fa_cnt_cck = 0;
602
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700603 if (priv->enhance_sensitivity_table)
604 ret |= iwl_enhance_sensitivity_write(priv);
605 else
606 ret |= iwl_sensitivity_write(priv);
Tomas Winklere1623442009-01-27 14:27:56 -0800607 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700608}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700609
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700610void iwl_sensitivity_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700611{
612 u32 rx_enable_time;
613 u32 fa_cck;
614 u32 fa_ofdm;
615 u32 bad_plcp_cck;
616 u32 bad_plcp_ofdm;
617 u32 norm_fa_ofdm;
618 u32 norm_fa_cck;
619 struct iwl_sensitivity_data *data = NULL;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700620 struct statistics_rx_non_phy *rx_info;
621 struct statistics_rx_phy *ofdm, *cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700622 unsigned long flags;
623 struct statistics_general_data statis;
624
Tomas Winkler445c2df2008-05-15 13:54:16 +0800625 if (priv->disable_sens_cal)
626 return;
627
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700628 data = &(priv->sensitivity_data);
629
Johannes Berg246ed352010-08-23 10:46:32 +0200630 if (!iwl_is_any_associated(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800631 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700632 return;
633 }
634
635 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700636 rx_info = &priv->statistics.rx_non_phy;
637 ofdm = &priv->statistics.rx_ofdm;
638 cck = &priv->statistics.rx_cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700639 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800640 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700641 spin_unlock_irqrestore(&priv->lock, flags);
642 return;
643 }
644
645 /* Extract Statistics: */
646 rx_enable_time = le32_to_cpu(rx_info->channel_load);
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700647 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
648 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
649 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
650 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700651
652 statis.beacon_silence_rssi_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700653 le32_to_cpu(rx_info->beacon_silence_rssi_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700654 statis.beacon_silence_rssi_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700655 le32_to_cpu(rx_info->beacon_silence_rssi_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700656 statis.beacon_silence_rssi_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700657 le32_to_cpu(rx_info->beacon_silence_rssi_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700658 statis.beacon_energy_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700659 le32_to_cpu(rx_info->beacon_energy_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700660 statis.beacon_energy_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700661 le32_to_cpu(rx_info->beacon_energy_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700662 statis.beacon_energy_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700663 le32_to_cpu(rx_info->beacon_energy_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700664
665 spin_unlock_irqrestore(&priv->lock, flags);
666
Tomas Winklere1623442009-01-27 14:27:56 -0800667 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700668
669 if (!rx_enable_time) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700670 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700671 return;
672 }
673
674 /* These statistics increase monotonically, and do not reset
675 * at each beacon. Calculate difference from last value, or just
676 * use the new statistics value if it has reset or wrapped around. */
677 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
678 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
679 else {
680 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
681 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
682 }
683
684 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
685 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
686 else {
687 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
688 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
689 }
690
691 if (data->last_fa_cnt_ofdm > fa_ofdm)
692 data->last_fa_cnt_ofdm = fa_ofdm;
693 else {
694 fa_ofdm -= data->last_fa_cnt_ofdm;
695 data->last_fa_cnt_ofdm += fa_ofdm;
696 }
697
698 if (data->last_fa_cnt_cck > fa_cck)
699 data->last_fa_cnt_cck = fa_cck;
700 else {
701 fa_cck -= data->last_fa_cnt_cck;
702 data->last_fa_cnt_cck += fa_cck;
703 }
704
705 /* Total aborted signal locks */
706 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
707 norm_fa_cck = fa_cck + bad_plcp_cck;
708
Tomas Winklere1623442009-01-27 14:27:56 -0800709 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700710 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
711
712 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
713 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700714 if (priv->enhance_sensitivity_table)
715 iwl_enhance_sensitivity_write(priv);
716 else
717 iwl_sensitivity_write(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700718}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700719
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700720static inline u8 find_first_chain(u8 mask)
721{
722 if (mask & ANT_A)
723 return CHAIN_A;
724 if (mask & ANT_B)
725 return CHAIN_B;
726 return CHAIN_C;
727}
728
Shanyu Zhao30312422010-11-10 09:56:48 -0800729/**
730 * Run disconnected antenna algorithm to find out which antennas are
731 * disconnected.
732 */
733static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
734 struct iwl_chain_noise_data *data)
735{
736 u32 active_chains = 0;
737 u32 max_average_sig;
738 u16 max_average_sig_antenna_i;
739 u8 num_tx_chains;
740 u8 first_chain;
741 u16 i = 0;
742
743 average_sig[0] = data->chain_signal_a /
744 priv->cfg->base_params->chain_noise_num_beacons;
745 average_sig[1] = data->chain_signal_b /
746 priv->cfg->base_params->chain_noise_num_beacons;
747 average_sig[2] = data->chain_signal_c /
748 priv->cfg->base_params->chain_noise_num_beacons;
749
750 if (average_sig[0] >= average_sig[1]) {
751 max_average_sig = average_sig[0];
752 max_average_sig_antenna_i = 0;
753 active_chains = (1 << max_average_sig_antenna_i);
754 } else {
755 max_average_sig = average_sig[1];
756 max_average_sig_antenna_i = 1;
757 active_chains = (1 << max_average_sig_antenna_i);
758 }
759
760 if (average_sig[2] >= max_average_sig) {
761 max_average_sig = average_sig[2];
762 max_average_sig_antenna_i = 2;
763 active_chains = (1 << max_average_sig_antenna_i);
764 }
765
766 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
767 average_sig[0], average_sig[1], average_sig[2]);
768 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
769 max_average_sig, max_average_sig_antenna_i);
770
771 /* Compare signal strengths for all 3 receivers. */
772 for (i = 0; i < NUM_RX_CHAINS; i++) {
773 if (i != max_average_sig_antenna_i) {
774 s32 rssi_delta = (max_average_sig - average_sig[i]);
775
776 /* If signal is very weak, compared with
777 * strongest, mark it as disconnected. */
778 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
779 data->disconn_array[i] = 1;
780 else
781 active_chains |= (1 << i);
782 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
783 "disconn_array[i] = %d\n",
784 i, rssi_delta, data->disconn_array[i]);
785 }
786 }
787
788 /*
789 * The above algorithm sometimes fails when the ucode
790 * reports 0 for all chains. It's not clear why that
791 * happens to start with, but it is then causing trouble
792 * because this can make us enable more chains than the
793 * hardware really has.
794 *
795 * To be safe, simply mask out any chains that we know
796 * are not on the device.
797 */
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -0800798 active_chains &= priv->hw_params.valid_rx_ant;
Shanyu Zhao30312422010-11-10 09:56:48 -0800799
800 num_tx_chains = 0;
801 for (i = 0; i < NUM_RX_CHAINS; i++) {
802 /* loops on all the bits of
803 * priv->hw_setting.valid_tx_ant */
804 u8 ant_msk = (1 << i);
805 if (!(priv->hw_params.valid_tx_ant & ant_msk))
806 continue;
807
808 num_tx_chains++;
809 if (data->disconn_array[i] == 0)
810 /* there is a Tx antenna connected */
811 break;
812 if (num_tx_chains == priv->hw_params.tx_chains_num &&
813 data->disconn_array[i]) {
814 /*
815 * If all chains are disconnected
816 * connect the first valid tx chain
817 */
818 first_chain =
819 find_first_chain(priv->cfg->valid_tx_ant);
820 data->disconn_array[first_chain] = 0;
821 active_chains |= BIT(first_chain);
Joe Perches85ee7a12011-04-23 20:38:19 -0700822 IWL_DEBUG_CALIB(priv,
823 "All Tx chains are disconnected W/A - declare %d as connected\n",
Shanyu Zhao30312422010-11-10 09:56:48 -0800824 first_chain);
825 break;
826 }
827 }
828
829 if (active_chains != priv->hw_params.valid_rx_ant &&
830 active_chains != priv->chain_noise_data.active_chains)
831 IWL_DEBUG_CALIB(priv,
832 "Detected that not all antennas are connected! "
833 "Connected: %#x, valid: %#x.\n",
834 active_chains, priv->hw_params.valid_rx_ant);
835
836 /* Save for use within RXON, TX, SCAN commands, etc. */
837 data->active_chains = active_chains;
838 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
839 active_chains);
840}
841
842
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700843/*
Shanyu Zhao30312422010-11-10 09:56:48 -0800844 * Accumulate 16 beacons of signal and noise statistics for each of
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700845 * 3 receivers/antennas/rx-chains, then figure out:
846 * 1) Which antennas are connected.
847 * 2) Differential rx gain settings to balance the 3 receivers.
848 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700849void iwl_chain_noise_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700850{
851 struct iwl_chain_noise_data *data = NULL;
852
853 u32 chain_noise_a;
854 u32 chain_noise_b;
855 u32 chain_noise_c;
856 u32 chain_sig_a;
857 u32 chain_sig_b;
858 u32 chain_sig_c;
859 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
860 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700861 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
862 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
863 u16 i = 0;
864 u16 rxon_chnum = INITIALIZATION_VALUE;
865 u16 stat_chnum = INITIALIZATION_VALUE;
866 u8 rxon_band24;
867 u8 stat_band24;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700868 unsigned long flags;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700869 struct statistics_rx_non_phy *rx_info;
Shanyu Zhao30312422010-11-10 09:56:48 -0800870
Johannes Berg246ed352010-08-23 10:46:32 +0200871 /*
872 * MULTI-FIXME:
873 * When we support multiple interfaces on different channels,
874 * this must be modified/fixed.
875 */
876 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700877
Tomas Winkler445c2df2008-05-15 13:54:16 +0800878 if (priv->disable_chain_noise_cal)
879 return;
880
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700881 data = &(priv->chain_noise_data);
882
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700883 /*
884 * Accumulate just the first "chain_noise_num_beacons" after
885 * the first association, then we're done forever.
886 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700887 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
888 if (data->state == IWL_CHAIN_NOISE_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -0800889 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700890 return;
891 }
892
893 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700894
895 rx_info = &priv->statistics.rx_non_phy;
896
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700897 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800898 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700899 spin_unlock_irqrestore(&priv->lock, flags);
900 return;
901 }
902
Johannes Berg246ed352010-08-23 10:46:32 +0200903 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
904 rxon_chnum = le16_to_cpu(ctx->staging.channel);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700905 stat_band24 =
906 !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
907 stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700908
909 /* Make sure we accumulate data for just the associated channel
910 * (even if scanning). */
911 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800912 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700913 rxon_chnum, rxon_band24);
914 spin_unlock_irqrestore(&priv->lock, flags);
915 return;
916 }
917
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700918 /*
919 * Accumulate beacon statistics values across
920 * "chain_noise_num_beacons"
921 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700922 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
923 IN_BAND_FILTER;
924 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
925 IN_BAND_FILTER;
926 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
927 IN_BAND_FILTER;
928
929 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
930 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
931 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
932
933 spin_unlock_irqrestore(&priv->lock, flags);
934
935 data->beacon_count++;
936
937 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
938 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
939 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
940
941 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
942 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
943 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
944
Tomas Winklere1623442009-01-27 14:27:56 -0800945 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700946 rxon_chnum, rxon_band24, data->beacon_count);
Tomas Winklere1623442009-01-27 14:27:56 -0800947 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700948 chain_sig_a, chain_sig_b, chain_sig_c);
Tomas Winklere1623442009-01-27 14:27:56 -0800949 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700950 chain_noise_a, chain_noise_b, chain_noise_c);
951
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700952 /* If this is the "chain_noise_num_beacons", determine:
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700953 * 1) Disconnected antennas (using signal strengths)
954 * 2) Differential gain (using silence noise) to balance receivers */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700955 if (data->beacon_count !=
956 priv->cfg->base_params->chain_noise_num_beacons)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700957 return;
958
959 /* Analyze signal for disconnected antenna */
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -0800960 if (priv->cfg->bt_params &&
961 priv->cfg->bt_params->advanced_bt_coexist) {
962 /* Disable disconnected antenna algorithm for advanced
963 bt coex, assuming valid antennas are connected */
964 data->active_chains = priv->hw_params.valid_rx_ant;
965 for (i = 0; i < NUM_RX_CHAINS; i++)
966 if (!(data->active_chains & (1<<i)))
967 data->disconn_array[i] = 1;
968 } else
969 iwl_find_disconn_antenna(priv, average_sig, data);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700970
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700971 /* Analyze noise for rx balance */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700972 average_noise[0] = data->chain_noise_a /
973 priv->cfg->base_params->chain_noise_num_beacons;
974 average_noise[1] = data->chain_noise_b /
975 priv->cfg->base_params->chain_noise_num_beacons;
976 average_noise[2] = data->chain_noise_c /
977 priv->cfg->base_params->chain_noise_num_beacons;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700978
979 for (i = 0; i < NUM_RX_CHAINS; i++) {
980 if (!(data->disconn_array[i]) &&
981 (average_noise[i] <= min_average_noise)) {
982 /* This means that chain i is active and has
983 * lower noise values so far: */
984 min_average_noise = average_noise[i];
985 min_average_noise_antenna_i = i;
986 }
987 }
988
Tomas Winklere1623442009-01-27 14:27:56 -0800989 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700990 average_noise[0], average_noise[1],
991 average_noise[2]);
992
Tomas Winklere1623442009-01-27 14:27:56 -0800993 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700994 min_average_noise, min_average_noise_antenna_i);
995
Jay Sternberg29f35c12009-01-29 11:09:16 -0800996 if (priv->cfg->ops->utils->gain_computation)
997 priv->cfg->ops->utils->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}