blob: 7807dc59e9c5cc7ab62e6f5dd495b8a665a38990 [file] [log] [blame]
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
63#include <linux/slab.h>
64#include <net/mac80211.h>
65
66#include "iwl-dev.h"
67#include "iwl-core.h"
68#include "iwl-4965-calib.h"
69
70/*****************************************************************************
71 * INIT calibrations framework
72 *****************************************************************************/
73
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
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +020083void il4965_calib_free_results(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080084{
85 int i;
86
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020087 for (i = 0; i < IL_CALIB_MAX; i++) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +020088 kfree(il->calib_results[i].buf);
89 il->calib_results[i].buf = NULL;
90 il->calib_results[i].buf_len = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080091 }
92}
93
94/*****************************************************************************
95 * RUNTIME calibrations framework
96 *****************************************************************************/
97
98/* "false alarms" are signals that our DSP tries to lock onto,
99 * but then determines that they are either noise, or transmissions
100 * from a distant wireless network (also "noise", really) that get
101 * "stepped on" by stronger transmissions within our own network.
102 * This algorithm attempts to set a sensitivity level that is high
103 * enough to receive all of our own network traffic, but not so
104 * high that our DSP gets too busy trying to lock onto non-network
105 * activity/noise. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200106static int il4965_sens_energy_cck(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800107 u32 norm_fa,
108 u32 rx_enable_time,
109 struct statistics_general_data *rx_info)
110{
111 u32 max_nrg_cck = 0;
112 int i = 0;
113 u8 max_silence_rssi = 0;
114 u32 silence_ref = 0;
115 u8 silence_rssi_a = 0;
116 u8 silence_rssi_b = 0;
117 u8 silence_rssi_c = 0;
118 u32 val;
119
120 /* "false_alarms" values below are cross-multiplications to assess the
121 * numbers of false alarms within the measured period of actual Rx
122 * (Rx is off when we're txing), vs the min/max expected false alarms
123 * (some should be expected if rx is sensitive enough) in a
124 * hypothetical listening period of 200 time units (TU), 204.8 msec:
125 *
126 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
127 *
128 * */
129 u32 false_alarms = norm_fa * 200 * 1024;
130 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
131 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200132 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200133 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800134
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200135 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800136
137 data->nrg_auto_corr_silence_diff = 0;
138
139 /* Find max silence rssi among all 3 receivers.
140 * This is background noise, which may include transmissions from other
141 * networks, measured during silence before our network's beacon */
142 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
143 ALL_BAND_FILTER) >> 8);
144 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
145 ALL_BAND_FILTER) >> 8);
146 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
147 ALL_BAND_FILTER) >> 8);
148
149 val = max(silence_rssi_b, silence_rssi_c);
150 max_silence_rssi = max(silence_rssi_a, (u8) val);
151
152 /* Store silence rssi in 20-beacon history table */
153 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
154 data->nrg_silence_idx++;
155 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
156 data->nrg_silence_idx = 0;
157
158 /* Find max silence rssi across 20 beacon history */
159 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
160 val = data->nrg_silence_rssi[i];
161 silence_ref = max(silence_ref, val);
162 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200163 IL_DEBUG_CALIB(il, "silence a %u, b %u, c %u, 20-bcn max %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800164 silence_rssi_a, silence_rssi_b, silence_rssi_c,
165 silence_ref);
166
167 /* Find max rx energy (min value!) among all 3 receivers,
168 * measured during beacon frame.
169 * Save it in 10-beacon history table. */
170 i = data->nrg_energy_idx;
171 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
172 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
173
174 data->nrg_energy_idx++;
175 if (data->nrg_energy_idx >= 10)
176 data->nrg_energy_idx = 0;
177
178 /* Find min rx energy (max value) across 10 beacon history.
179 * This is the minimum signal level that we want to receive well.
180 * Add backoff (margin so we don't miss slightly lower energy frames).
181 * This establishes an upper bound (min value) for energy threshold. */
182 max_nrg_cck = data->nrg_value[0];
183 for (i = 1; i < 10; i++)
184 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
185 max_nrg_cck += 6;
186
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200187 IL_DEBUG_CALIB(il, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800188 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
189 rx_info->beacon_energy_c, max_nrg_cck - 6);
190
191 /* Count number of consecutive beacons with fewer-than-desired
192 * false alarms. */
193 if (false_alarms < min_false_alarms)
194 data->num_in_cck_no_fa++;
195 else
196 data->num_in_cck_no_fa = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200197 IL_DEBUG_CALIB(il, "consecutive bcns with few false alarms = %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800198 data->num_in_cck_no_fa);
199
200 /* If we got too many false alarms this time, reduce sensitivity */
201 if ((false_alarms > max_false_alarms) &&
202 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200203 IL_DEBUG_CALIB(il, "norm FA %u > max FA %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800204 false_alarms, max_false_alarms);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200205 IL_DEBUG_CALIB(il, "... reducing sensitivity\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200206 data->nrg_curr_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800207 /* Store for "fewer than desired" on later beacon */
208 data->nrg_silence_ref = silence_ref;
209
210 /* increase energy threshold (reduce nrg value)
211 * to decrease sensitivity */
212 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
213 /* Else if we got fewer than desired, increase sensitivity */
214 } else if (false_alarms < min_false_alarms) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200215 data->nrg_curr_state = IL_FA_TOO_FEW;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800216
217 /* Compare silence level with silence level for most recent
218 * healthy number or too many false alarms */
219 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
220 (s32)silence_ref;
221
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200222 IL_DEBUG_CALIB(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800223 "norm FA %u < min FA %u, silence diff %d\n",
224 false_alarms, min_false_alarms,
225 data->nrg_auto_corr_silence_diff);
226
227 /* Increase value to increase sensitivity, but only if:
228 * 1a) previous beacon did *not* have *too many* false alarms
229 * 1b) AND there's a significant difference in Rx levels
230 * from a previous beacon with too many, or healthy # FAs
231 * OR 2) We've seen a lot of beacons (100) with too few
232 * false alarms */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200233 if ((data->nrg_prev_state != IL_FA_TOO_MANY) &&
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800234 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
235 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
236
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200237 IL_DEBUG_CALIB(il, "... increasing sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800238 /* Increase nrg value to increase sensitivity */
239 val = data->nrg_th_cck + NRG_STEP_CCK;
240 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
241 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200242 IL_DEBUG_CALIB(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800243 "... but not changing sensitivity\n");
244 }
245
246 /* Else we got a healthy number of false alarms, keep status quo */
247 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200248 IL_DEBUG_CALIB(il, " FA in safe zone\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200249 data->nrg_curr_state = IL_FA_GOOD_RANGE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800250
251 /* Store for use in "fewer than desired" with later beacon */
252 data->nrg_silence_ref = silence_ref;
253
254 /* If previous beacon had too many false alarms,
255 * give it some extra margin by reducing sensitivity again
256 * (but don't go below measured energy of desired Rx) */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200257 if (IL_FA_TOO_MANY == data->nrg_prev_state) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200258 IL_DEBUG_CALIB(il, "... increasing margin\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800259 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
260 data->nrg_th_cck -= NRG_MARGIN;
261 else
262 data->nrg_th_cck = max_nrg_cck;
263 }
264 }
265
266 /* Make sure the energy threshold does not go above the measured
267 * energy of the desired Rx signals (reduced by backoff margin),
268 * or else we might start missing Rx frames.
269 * Lower value is higher energy, so we use max()!
270 */
271 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200272 IL_DEBUG_CALIB(il, "new nrg_th_cck %u\n", data->nrg_th_cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800273
274 data->nrg_prev_state = data->nrg_curr_state;
275
276 /* Auto-correlation CCK algorithm */
277 if (false_alarms > min_false_alarms) {
278
279 /* increase auto_corr values to decrease sensitivity
280 * so the DSP won't be disturbed by the noise
281 */
282 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
283 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
284 else {
285 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
286 data->auto_corr_cck =
287 min((u32)ranges->auto_corr_max_cck, val);
288 }
289 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
290 data->auto_corr_cck_mrc =
291 min((u32)ranges->auto_corr_max_cck_mrc, val);
292 } else if ((false_alarms < min_false_alarms) &&
293 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
294 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
295
296 /* Decrease auto_corr values to increase sensitivity */
297 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
298 data->auto_corr_cck =
299 max((u32)ranges->auto_corr_min_cck, val);
300 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
301 data->auto_corr_cck_mrc =
302 max((u32)ranges->auto_corr_min_cck_mrc, val);
303 }
304
305 return 0;
306}
307
308
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200309static int il4965_sens_auto_corr_ofdm(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800310 u32 norm_fa,
311 u32 rx_enable_time)
312{
313 u32 val;
314 u32 false_alarms = norm_fa * 200 * 1024;
315 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
316 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200317 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200318 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800319
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200320 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800321
322 /* If we got too many false alarms this time, reduce sensitivity */
323 if (false_alarms > max_false_alarms) {
324
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200325 IL_DEBUG_CALIB(il, "norm FA %u > max FA %u)\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800326 false_alarms, max_false_alarms);
327
328 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
329 data->auto_corr_ofdm =
330 min((u32)ranges->auto_corr_max_ofdm, val);
331
332 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
333 data->auto_corr_ofdm_mrc =
334 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
335
336 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
337 data->auto_corr_ofdm_x1 =
338 min((u32)ranges->auto_corr_max_ofdm_x1, val);
339
340 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
341 data->auto_corr_ofdm_mrc_x1 =
342 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
343 }
344
345 /* Else if we got fewer than desired, increase sensitivity */
346 else if (false_alarms < min_false_alarms) {
347
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200348 IL_DEBUG_CALIB(il, "norm FA %u < min FA %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800349 false_alarms, min_false_alarms);
350
351 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
352 data->auto_corr_ofdm =
353 max((u32)ranges->auto_corr_min_ofdm, val);
354
355 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
356 data->auto_corr_ofdm_mrc =
357 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
358
359 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
360 data->auto_corr_ofdm_x1 =
361 max((u32)ranges->auto_corr_min_ofdm_x1, val);
362
363 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
364 data->auto_corr_ofdm_mrc_x1 =
365 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
366 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200367 IL_DEBUG_CALIB(il, "min FA %u < norm FA %u < max FA %u OK\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800368 min_false_alarms, false_alarms, max_false_alarms);
369 }
370 return 0;
371}
372
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200373static void il4965_prepare_legacy_sensitivity_tbl(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200374 struct il_sensitivity_data *data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800375 __le16 *tbl)
376{
377 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
378 cpu_to_le16((u16)data->auto_corr_ofdm);
379 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
380 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
381 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
382 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
383 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
384 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
385
386 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
387 cpu_to_le16((u16)data->auto_corr_cck);
388 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
389 cpu_to_le16((u16)data->auto_corr_cck_mrc);
390
391 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
392 cpu_to_le16((u16)data->nrg_th_cck);
393 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
394 cpu_to_le16((u16)data->nrg_th_ofdm);
395
396 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
397 cpu_to_le16(data->barker_corr_th_min);
398 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
399 cpu_to_le16(data->barker_corr_th_min_mrc);
400 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
401 cpu_to_le16(data->nrg_th_cca);
402
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200403 IL_DEBUG_CALIB(il, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800404 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
405 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
406 data->nrg_th_ofdm);
407
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200408 IL_DEBUG_CALIB(il, "cck: ac %u mrc %u thresh %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800409 data->auto_corr_cck, data->auto_corr_cck_mrc,
410 data->nrg_th_cck);
411}
412
413/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200414static int il4965_sensitivity_write(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800415{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200416 struct il_sensitivity_cmd cmd;
417 struct il_sensitivity_data *data = NULL;
418 struct il_host_cmd cmd_out = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800419 .id = SENSITIVITY_CMD,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200420 .len = sizeof(struct il_sensitivity_cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800421 .flags = CMD_ASYNC,
422 .data = &cmd,
423 };
424
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200425 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800426
427 memset(&cmd, 0, sizeof(cmd));
428
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200429 il4965_prepare_legacy_sensitivity_tbl(il, data, &cmd.table[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800430
431 /* Update uCode's "work" table, and copy it to DSP */
432 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
433
434 /* Don't send command to uCode if nothing has changed */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200435 if (!memcmp(&cmd.table[0], &(il->sensitivity_tbl[0]),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800436 sizeof(u16)*HD_TABLE_SIZE)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200437 IL_DEBUG_CALIB(il, "No change in SENSITIVITY_CMD\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800438 return 0;
439 }
440
441 /* Copy table for comparison next time */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200442 memcpy(&(il->sensitivity_tbl[0]), &(cmd.table[0]),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800443 sizeof(u16)*HD_TABLE_SIZE);
444
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200445 return il_send_cmd(il, &cmd_out);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800446}
447
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200448void il4965_init_sensitivity(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800449{
450 int ret = 0;
451 int i;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200452 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200453 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800454
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200455 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800456 return;
457
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200458 IL_DEBUG_CALIB(il, "Start il4965_init_sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800459
460 /* Clear driver's sensitivity algo data */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200461 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800462
463 if (ranges == NULL)
464 return;
465
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200466 memset(data, 0, sizeof(struct il_sensitivity_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800467
468 data->num_in_cck_no_fa = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200469 data->nrg_curr_state = IL_FA_TOO_MANY;
470 data->nrg_prev_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800471 data->nrg_silence_ref = 0;
472 data->nrg_silence_idx = 0;
473 data->nrg_energy_idx = 0;
474
475 for (i = 0; i < 10; i++)
476 data->nrg_value[i] = 0;
477
478 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
479 data->nrg_silence_rssi[i] = 0;
480
481 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
482 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
483 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
484 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
485 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
486 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
487 data->nrg_th_cck = ranges->nrg_th_cck;
488 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
489 data->barker_corr_th_min = ranges->barker_corr_th_min;
490 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
491 data->nrg_th_cca = ranges->nrg_th_cca;
492
493 data->last_bad_plcp_cnt_ofdm = 0;
494 data->last_fa_cnt_ofdm = 0;
495 data->last_bad_plcp_cnt_cck = 0;
496 data->last_fa_cnt_cck = 0;
497
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200498 ret |= il4965_sensitivity_write(il);
499 IL_DEBUG_CALIB(il, "<<return 0x%X\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800500}
501
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200502void il4965_sensitivity_calibration(struct il_priv *il, void *resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800503{
504 u32 rx_enable_time;
505 u32 fa_cck;
506 u32 fa_ofdm;
507 u32 bad_plcp_cck;
508 u32 bad_plcp_ofdm;
509 u32 norm_fa_ofdm;
510 u32 norm_fa_cck;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200511 struct il_sensitivity_data *data = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800512 struct statistics_rx_non_phy *rx_info;
513 struct statistics_rx_phy *ofdm, *cck;
514 unsigned long flags;
515 struct statistics_general_data statis;
516
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200517 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800518 return;
519
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200520 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800521
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200522 if (!il_is_any_associated(il)) {
523 IL_DEBUG_CALIB(il, "<< - not associated\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800524 return;
525 }
526
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200527 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800528
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200529 rx_info = &(((struct il_notif_statistics *)resp)->rx.general);
530 ofdm = &(((struct il_notif_statistics *)resp)->rx.ofdm);
531 cck = &(((struct il_notif_statistics *)resp)->rx.cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800532
533 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200534 IL_DEBUG_CALIB(il, "<< invalid data.\n");
535 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800536 return;
537 }
538
539 /* Extract Statistics: */
540 rx_enable_time = le32_to_cpu(rx_info->channel_load);
541 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
542 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
543 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
544 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
545
546 statis.beacon_silence_rssi_a =
547 le32_to_cpu(rx_info->beacon_silence_rssi_a);
548 statis.beacon_silence_rssi_b =
549 le32_to_cpu(rx_info->beacon_silence_rssi_b);
550 statis.beacon_silence_rssi_c =
551 le32_to_cpu(rx_info->beacon_silence_rssi_c);
552 statis.beacon_energy_a =
553 le32_to_cpu(rx_info->beacon_energy_a);
554 statis.beacon_energy_b =
555 le32_to_cpu(rx_info->beacon_energy_b);
556 statis.beacon_energy_c =
557 le32_to_cpu(rx_info->beacon_energy_c);
558
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200559 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800560
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200561 IL_DEBUG_CALIB(il, "rx_enable_time = %u usecs\n", rx_enable_time);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800562
563 if (!rx_enable_time) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200564 IL_DEBUG_CALIB(il, "<< RX Enable Time == 0!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800565 return;
566 }
567
568 /* These statistics increase monotonically, and do not reset
569 * at each beacon. Calculate difference from last value, or just
570 * use the new statistics value if it has reset or wrapped around. */
571 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
572 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
573 else {
574 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
575 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
576 }
577
578 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
579 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
580 else {
581 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
582 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
583 }
584
585 if (data->last_fa_cnt_ofdm > fa_ofdm)
586 data->last_fa_cnt_ofdm = fa_ofdm;
587 else {
588 fa_ofdm -= data->last_fa_cnt_ofdm;
589 data->last_fa_cnt_ofdm += fa_ofdm;
590 }
591
592 if (data->last_fa_cnt_cck > fa_cck)
593 data->last_fa_cnt_cck = fa_cck;
594 else {
595 fa_cck -= data->last_fa_cnt_cck;
596 data->last_fa_cnt_cck += fa_cck;
597 }
598
599 /* Total aborted signal locks */
600 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
601 norm_fa_cck = fa_cck + bad_plcp_cck;
602
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200603 IL_DEBUG_CALIB(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800604 "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
605 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
606
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200607 il4965_sens_auto_corr_ofdm(il, norm_fa_ofdm, rx_enable_time);
608 il4965_sens_energy_cck(il, norm_fa_cck, rx_enable_time, &statis);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800609
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200610 il4965_sensitivity_write(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800611}
612
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200613static inline u8 il4965_find_first_chain(u8 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800614{
615 if (mask & ANT_A)
616 return CHAIN_A;
617 if (mask & ANT_B)
618 return CHAIN_B;
619 return CHAIN_C;
620}
621
622/**
623 * Run disconnected antenna algorithm to find out which antennas are
624 * disconnected.
625 */
626static void
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200627il4965_find_disconn_antenna(struct il_priv *il, u32* average_sig,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200628 struct il_chain_noise_data *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800629{
630 u32 active_chains = 0;
631 u32 max_average_sig;
632 u16 max_average_sig_antenna_i;
633 u8 num_tx_chains;
634 u8 first_chain;
635 u16 i = 0;
636
637 average_sig[0] = data->chain_signal_a /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200638 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800639 average_sig[1] = data->chain_signal_b /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200640 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800641 average_sig[2] = data->chain_signal_c /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200642 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800643
644 if (average_sig[0] >= average_sig[1]) {
645 max_average_sig = average_sig[0];
646 max_average_sig_antenna_i = 0;
647 active_chains = (1 << max_average_sig_antenna_i);
648 } else {
649 max_average_sig = average_sig[1];
650 max_average_sig_antenna_i = 1;
651 active_chains = (1 << max_average_sig_antenna_i);
652 }
653
654 if (average_sig[2] >= max_average_sig) {
655 max_average_sig = average_sig[2];
656 max_average_sig_antenna_i = 2;
657 active_chains = (1 << max_average_sig_antenna_i);
658 }
659
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200660 IL_DEBUG_CALIB(il, "average_sig: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800661 average_sig[0], average_sig[1], average_sig[2]);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200662 IL_DEBUG_CALIB(il, "max_average_sig = %d, antenna %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800663 max_average_sig, max_average_sig_antenna_i);
664
665 /* Compare signal strengths for all 3 receivers. */
666 for (i = 0; i < NUM_RX_CHAINS; i++) {
667 if (i != max_average_sig_antenna_i) {
668 s32 rssi_delta = (max_average_sig - average_sig[i]);
669
670 /* If signal is very weak, compared with
671 * strongest, mark it as disconnected. */
672 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
673 data->disconn_array[i] = 1;
674 else
675 active_chains |= (1 << i);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200676 IL_DEBUG_CALIB(il, "i = %d rssiDelta = %d "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800677 "disconn_array[i] = %d\n",
678 i, rssi_delta, data->disconn_array[i]);
679 }
680 }
681
682 /*
683 * The above algorithm sometimes fails when the ucode
684 * reports 0 for all chains. It's not clear why that
685 * happens to start with, but it is then causing trouble
686 * because this can make us enable more chains than the
687 * hardware really has.
688 *
689 * To be safe, simply mask out any chains that we know
690 * are not on the device.
691 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200692 active_chains &= il->hw_params.valid_rx_ant;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800693
694 num_tx_chains = 0;
695 for (i = 0; i < NUM_RX_CHAINS; i++) {
696 /* loops on all the bits of
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200697 * il->hw_setting.valid_tx_ant */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800698 u8 ant_msk = (1 << i);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200699 if (!(il->hw_params.valid_tx_ant & ant_msk))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800700 continue;
701
702 num_tx_chains++;
703 if (data->disconn_array[i] == 0)
704 /* there is a Tx antenna connected */
705 break;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200706 if (num_tx_chains == il->hw_params.tx_chains_num &&
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800707 data->disconn_array[i]) {
708 /*
709 * If all chains are disconnected
710 * connect the first valid tx chain
711 */
712 first_chain =
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200713 il4965_find_first_chain(il->cfg->valid_tx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800714 data->disconn_array[first_chain] = 0;
715 active_chains |= BIT(first_chain);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200716 IL_DEBUG_CALIB(il,
Joe Perches85ee7a12011-04-23 20:38:19 -0700717 "All Tx chains are disconnected W/A - declare %d as connected\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800718 first_chain);
719 break;
720 }
721 }
722
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200723 if (active_chains != il->hw_params.valid_rx_ant &&
724 active_chains != il->chain_noise_data.active_chains)
725 IL_DEBUG_CALIB(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800726 "Detected that not all antennas are connected! "
727 "Connected: %#x, valid: %#x.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200728 active_chains, il->hw_params.valid_rx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800729
730 /* Save for use within RXON, TX, SCAN commands, etc. */
731 data->active_chains = active_chains;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200732 IL_DEBUG_CALIB(il, "active_chains (bitwise) = 0x%x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800733 active_chains);
734}
735
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200736static void il4965_gain_computation(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800737 u32 *average_noise,
738 u16 min_average_noise_antenna_i,
739 u32 min_average_noise,
740 u8 default_chain)
741{
742 int i, ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200743 struct il_chain_noise_data *data = &il->chain_noise_data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800744
745 data->delta_gain_code[min_average_noise_antenna_i] = 0;
746
747 for (i = default_chain; i < NUM_RX_CHAINS; i++) {
748 s32 delta_g = 0;
749
750 if (!(data->disconn_array[i]) &&
751 (data->delta_gain_code[i] ==
752 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
753 delta_g = average_noise[i] - min_average_noise;
754 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
755 data->delta_gain_code[i] =
756 min(data->delta_gain_code[i],
757 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
758
759 data->delta_gain_code[i] =
760 (data->delta_gain_code[i] | (1 << 2));
761 } else {
762 data->delta_gain_code[i] = 0;
763 }
764 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200765 IL_DEBUG_CALIB(il, "delta_gain_codes: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800766 data->delta_gain_code[0],
767 data->delta_gain_code[1],
768 data->delta_gain_code[2]);
769
770 /* Differential gain gets sent to uCode only once */
771 if (!data->radio_write) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200772 struct il_calib_diff_gain_cmd cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800773 data->radio_write = 1;
774
775 memset(&cmd, 0, sizeof(cmd));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200776 cmd.hdr.op_code = IL_PHY_CALIBRATE_DIFF_GAIN_CMD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800777 cmd.diff_gain_a = data->delta_gain_code[0];
778 cmd.diff_gain_b = data->delta_gain_code[1];
779 cmd.diff_gain_c = data->delta_gain_code[2];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200780 ret = il_send_cmd_pdu(il, REPLY_PHY_CALIBRATION_CMD,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800781 sizeof(cmd), &cmd);
782 if (ret)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200783 IL_DEBUG_CALIB(il, "fail sending cmd "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800784 "REPLY_PHY_CALIBRATION_CMD\n");
785
786 /* TODO we might want recalculate
787 * rx_chain in rxon cmd */
788
789 /* Mark so we run this algo only once! */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200790 data->state = IL_CHAIN_NOISE_CALIBRATED;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800791 }
792}
793
794
795
796/*
797 * Accumulate 16 beacons of signal and noise statistics for each of
798 * 3 receivers/antennas/rx-chains, then figure out:
799 * 1) Which antennas are connected.
800 * 2) Differential rx gain settings to balance the 3 receivers.
801 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200802void il4965_chain_noise_calibration(struct il_priv *il, void *stat_resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800803{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200804 struct il_chain_noise_data *data = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800805
806 u32 chain_noise_a;
807 u32 chain_noise_b;
808 u32 chain_noise_c;
809 u32 chain_sig_a;
810 u32 chain_sig_b;
811 u32 chain_sig_c;
812 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
813 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
814 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
815 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
816 u16 i = 0;
817 u16 rxon_chnum = INITIALIZATION_VALUE;
818 u16 stat_chnum = INITIALIZATION_VALUE;
819 u8 rxon_band24;
820 u8 stat_band24;
821 unsigned long flags;
822 struct statistics_rx_non_phy *rx_info;
823
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200824 struct il_rxon_context *ctx = &il->contexts[IL_RXON_CTX_BSS];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800825
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200826 if (il->disable_chain_noise_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800827 return;
828
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200829 data = &(il->chain_noise_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800830
831 /*
832 * Accumulate just the first "chain_noise_num_beacons" after
833 * the first association, then we're done forever.
834 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200835 if (data->state != IL_CHAIN_NOISE_ACCUMULATE) {
836 if (data->state == IL_CHAIN_NOISE_ALIVE)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200837 IL_DEBUG_CALIB(il, "Wait for noise calib reset\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800838 return;
839 }
840
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200841 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800842
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200843 rx_info = &(((struct il_notif_statistics *)stat_resp)->
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800844 rx.general);
845
846 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200847 IL_DEBUG_CALIB(il, " << Interference data unavailable\n");
848 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800849 return;
850 }
851
852 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
853 rxon_chnum = le16_to_cpu(ctx->staging.channel);
854
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200855 stat_band24 = !!(((struct il_notif_statistics *)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800856 stat_resp)->flag &
857 STATISTICS_REPLY_FLG_BAND_24G_MSK);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200858 stat_chnum = le32_to_cpu(((struct il_notif_statistics *)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800859 stat_resp)->flag) >> 16;
860
861 /* Make sure we accumulate data for just the associated channel
862 * (even if scanning). */
863 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200864 IL_DEBUG_CALIB(il, "Stats not from chan=%d, band24=%d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800865 rxon_chnum, rxon_band24);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200866 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800867 return;
868 }
869
870 /*
871 * Accumulate beacon statistics values across
872 * "chain_noise_num_beacons"
873 */
874 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
875 IN_BAND_FILTER;
876 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
877 IN_BAND_FILTER;
878 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
879 IN_BAND_FILTER;
880
881 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
882 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
883 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
884
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200885 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800886
887 data->beacon_count++;
888
889 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
890 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
891 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
892
893 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
894 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
895 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
896
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200897 IL_DEBUG_CALIB(il, "chan=%d, band24=%d, beacon=%d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800898 rxon_chnum, rxon_band24, data->beacon_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200899 IL_DEBUG_CALIB(il, "chain_sig: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800900 chain_sig_a, chain_sig_b, chain_sig_c);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200901 IL_DEBUG_CALIB(il, "chain_noise: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800902 chain_noise_a, chain_noise_b, chain_noise_c);
903
904 /* If this is the "chain_noise_num_beacons", determine:
905 * 1) Disconnected antennas (using signal strengths)
906 * 2) Differential gain (using silence noise) to balance receivers */
907 if (data->beacon_count !=
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200908 il->cfg->base_params->chain_noise_num_beacons)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800909 return;
910
911 /* Analyze signal for disconnected antenna */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200912 il4965_find_disconn_antenna(il, average_sig, data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800913
914 /* Analyze noise for rx balance */
915 average_noise[0] = data->chain_noise_a /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200916 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800917 average_noise[1] = data->chain_noise_b /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200918 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800919 average_noise[2] = data->chain_noise_c /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200920 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800921
922 for (i = 0; i < NUM_RX_CHAINS; i++) {
923 if (!(data->disconn_array[i]) &&
924 (average_noise[i] <= min_average_noise)) {
925 /* This means that chain i is active and has
926 * lower noise values so far: */
927 min_average_noise = average_noise[i];
928 min_average_noise_antenna_i = i;
929 }
930 }
931
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200932 IL_DEBUG_CALIB(il, "average_noise: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800933 average_noise[0], average_noise[1],
934 average_noise[2]);
935
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200936 IL_DEBUG_CALIB(il, "min_average_noise = %d, antenna %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800937 min_average_noise, min_average_noise_antenna_i);
938
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200939 il4965_gain_computation(il, average_noise,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800940 min_average_noise_antenna_i, min_average_noise,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200941 il4965_find_first_chain(il->cfg->valid_rx_ant));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800942
943 /* Some power changes may have been made during the calibration.
944 * Update and commit the RXON
945 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200946 if (il->cfg->ops->lib->update_chain_flags)
947 il->cfg->ops->lib->update_chain_flags(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800948
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200949 data->state = IL_CHAIN_NOISE_DONE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200950 il_power_update_mode(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800951}
952
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200953void il4965_reset_run_time_calib(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800954{
955 int i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200956 memset(&(il->sensitivity_data), 0,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200957 sizeof(struct il_sensitivity_data));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200958 memset(&(il->chain_noise_data), 0,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200959 sizeof(struct il_chain_noise_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800960 for (i = 0; i < NUM_RX_CHAINS; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200961 il->chain_noise_data.delta_gain_code[i] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800962 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
963
964 /* Ask for statistics now, the uCode will send notification
965 * periodically after association */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200966 il_send_statistics_request(il, CMD_ASYNC, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800967}