blob: 405032a9b36ea4066be5ab01a44c3e051b5703ac [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
Stanislaw Gruszka98613be2011-11-15 14:19:34 +010066#include "common.h"
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +020067#include "4965.h"
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080068
69/*****************************************************************************
70 * INIT calibrations framework
71 *****************************************************************************/
72
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +020073struct stats_general_data {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080074 u32 beacon_silence_rssi_a;
75 u32 beacon_silence_rssi_b;
76 u32 beacon_silence_rssi_c;
77 u32 beacon_energy_a;
78 u32 beacon_energy_b;
79 u32 beacon_energy_c;
80};
81
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +020082void il4965_calib_free_results(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080083{
84 int i;
85
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020086 for (i = 0; i < IL_CALIB_MAX; i++) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +020087 kfree(il->calib_results[i].buf);
88 il->calib_results[i].buf = NULL;
89 il->calib_results[i].buf_len = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080090 }
91}
92
93/*****************************************************************************
94 * RUNTIME calibrations framework
95 *****************************************************************************/
96
97/* "false alarms" are signals that our DSP tries to lock onto,
98 * but then determines that they are either noise, or transmissions
99 * from a distant wireless network (also "noise", really) that get
100 * "stepped on" by stronger transmissions within our own network.
101 * This algorithm attempts to set a sensitivity level that is high
102 * enough to receive all of our own network traffic, but not so
103 * high that our DSP gets too busy trying to lock onto non-network
104 * activity/noise. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200105static int il4965_sens_energy_cck(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800106 u32 norm_fa,
107 u32 rx_enable_time,
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200108 struct stats_general_data *rx_info)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800109{
110 u32 max_nrg_cck = 0;
111 int i = 0;
112 u8 max_silence_rssi = 0;
113 u32 silence_ref = 0;
114 u8 silence_rssi_a = 0;
115 u8 silence_rssi_b = 0;
116 u8 silence_rssi_c = 0;
117 u32 val;
118
119 /* "false_alarms" values below are cross-multiplications to assess the
120 * numbers of false alarms within the measured period of actual Rx
121 * (Rx is off when we're txing), vs the min/max expected false alarms
122 * (some should be expected if rx is sensitive enough) in a
123 * hypothetical listening period of 200 time units (TU), 204.8 msec:
124 *
125 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
126 *
127 * */
128 u32 false_alarms = norm_fa * 200 * 1024;
129 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
130 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200131 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200132 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800133
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200134 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800135
136 data->nrg_auto_corr_silence_diff = 0;
137
138 /* Find max silence rssi among all 3 receivers.
139 * This is background noise, which may include transmissions from other
140 * networks, measured during silence before our network's beacon */
141 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
142 ALL_BAND_FILTER) >> 8);
143 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
144 ALL_BAND_FILTER) >> 8);
145 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
146 ALL_BAND_FILTER) >> 8);
147
148 val = max(silence_rssi_b, silence_rssi_c);
149 max_silence_rssi = max(silence_rssi_a, (u8) val);
150
151 /* Store silence rssi in 20-beacon history table */
152 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
153 data->nrg_silence_idx++;
154 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
155 data->nrg_silence_idx = 0;
156
157 /* Find max silence rssi across 20 beacon history */
158 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
159 val = data->nrg_silence_rssi[i];
160 silence_ref = max(silence_ref, val);
161 }
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100162 D_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800163 silence_rssi_a, silence_rssi_b, silence_rssi_c,
164 silence_ref);
165
166 /* Find max rx energy (min value!) among all 3 receivers,
167 * measured during beacon frame.
168 * Save it in 10-beacon history table. */
169 i = data->nrg_energy_idx;
170 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
171 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
172
173 data->nrg_energy_idx++;
174 if (data->nrg_energy_idx >= 10)
175 data->nrg_energy_idx = 0;
176
177 /* Find min rx energy (max value) across 10 beacon history.
178 * This is the minimum signal level that we want to receive well.
179 * Add backoff (margin so we don't miss slightly lower energy frames).
180 * This establishes an upper bound (min value) for energy threshold. */
181 max_nrg_cck = data->nrg_value[0];
182 for (i = 1; i < 10; i++)
183 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
184 max_nrg_cck += 6;
185
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100186 D_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800187 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
188 rx_info->beacon_energy_c, max_nrg_cck - 6);
189
190 /* Count number of consecutive beacons with fewer-than-desired
191 * false alarms. */
192 if (false_alarms < min_false_alarms)
193 data->num_in_cck_no_fa++;
194 else
195 data->num_in_cck_no_fa = 0;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100196 D_CALIB("consecutive bcns with few false alarms = %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800197 data->num_in_cck_no_fa);
198
199 /* If we got too many false alarms this time, reduce sensitivity */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200200 if (false_alarms > max_false_alarms &&
201 data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100202 D_CALIB("norm FA %u > max FA %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800203 false_alarms, max_false_alarms);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100204 D_CALIB("... reducing sensitivity\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200205 data->nrg_curr_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800206 /* Store for "fewer than desired" on later beacon */
207 data->nrg_silence_ref = silence_ref;
208
209 /* increase energy threshold (reduce nrg value)
210 * to decrease sensitivity */
211 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
212 /* Else if we got fewer than desired, increase sensitivity */
213 } else if (false_alarms < min_false_alarms) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200214 data->nrg_curr_state = IL_FA_TOO_FEW;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800215
216 /* Compare silence level with silence level for most recent
217 * healthy number or too many false alarms */
218 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
219 (s32)silence_ref;
220
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100221 D_CALIB(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800222 "norm FA %u < min FA %u, silence diff %d\n",
223 false_alarms, min_false_alarms,
224 data->nrg_auto_corr_silence_diff);
225
226 /* Increase value to increase sensitivity, but only if:
227 * 1a) previous beacon did *not* have *too many* false alarms
228 * 1b) AND there's a significant difference in Rx levels
229 * from a previous beacon with too many, or healthy # FAs
230 * OR 2) We've seen a lot of beacons (100) with too few
231 * false alarms */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200232 if (data->nrg_prev_state != IL_FA_TOO_MANY &&
233 (data->nrg_auto_corr_silence_diff > NRG_DIFF ||
234 data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800235
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100236 D_CALIB("... increasing sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800237 /* Increase nrg value to increase sensitivity */
238 val = data->nrg_th_cck + NRG_STEP_CCK;
239 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
240 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100241 D_CALIB(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800242 "... but not changing sensitivity\n");
243 }
244
245 /* Else we got a healthy number of false alarms, keep status quo */
246 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100247 D_CALIB(" FA in safe zone\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200248 data->nrg_curr_state = IL_FA_GOOD_RANGE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800249
250 /* Store for use in "fewer than desired" with later beacon */
251 data->nrg_silence_ref = silence_ref;
252
253 /* If previous beacon had too many false alarms,
254 * give it some extra margin by reducing sensitivity again
255 * (but don't go below measured energy of desired Rx) */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200256 if (IL_FA_TOO_MANY == data->nrg_prev_state) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100257 D_CALIB("... increasing margin\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800258 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
259 data->nrg_th_cck -= NRG_MARGIN;
260 else
261 data->nrg_th_cck = max_nrg_cck;
262 }
263 }
264
265 /* Make sure the energy threshold does not go above the measured
266 * energy of the desired Rx signals (reduced by backoff margin),
267 * or else we might start missing Rx frames.
268 * Lower value is higher energy, so we use max()!
269 */
270 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100271 D_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800272
273 data->nrg_prev_state = data->nrg_curr_state;
274
275 /* Auto-correlation CCK algorithm */
276 if (false_alarms > min_false_alarms) {
277
278 /* increase auto_corr values to decrease sensitivity
279 * so the DSP won't be disturbed by the noise
280 */
281 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
282 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
283 else {
284 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
285 data->auto_corr_cck =
286 min((u32)ranges->auto_corr_max_cck, val);
287 }
288 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
289 data->auto_corr_cck_mrc =
290 min((u32)ranges->auto_corr_max_cck_mrc, val);
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200291 } else if (false_alarms < min_false_alarms &&
292 (data->nrg_auto_corr_silence_diff > NRG_DIFF ||
293 data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800294
295 /* Decrease auto_corr values to increase sensitivity */
296 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
297 data->auto_corr_cck =
298 max((u32)ranges->auto_corr_min_cck, val);
299 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
300 data->auto_corr_cck_mrc =
301 max((u32)ranges->auto_corr_min_cck_mrc, val);
302 }
303
304 return 0;
305}
306
307
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200308static int il4965_sens_auto_corr_ofdm(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800309 u32 norm_fa,
310 u32 rx_enable_time)
311{
312 u32 val;
313 u32 false_alarms = norm_fa * 200 * 1024;
314 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
315 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200316 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200317 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800318
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200319 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800320
321 /* If we got too many false alarms this time, reduce sensitivity */
322 if (false_alarms > max_false_alarms) {
323
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100324 D_CALIB("norm FA %u > max FA %u)\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800325 false_alarms, max_false_alarms);
326
327 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
328 data->auto_corr_ofdm =
329 min((u32)ranges->auto_corr_max_ofdm, val);
330
331 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
332 data->auto_corr_ofdm_mrc =
333 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
334
335 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
336 data->auto_corr_ofdm_x1 =
337 min((u32)ranges->auto_corr_max_ofdm_x1, val);
338
339 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
340 data->auto_corr_ofdm_mrc_x1 =
341 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
342 }
343
344 /* Else if we got fewer than desired, increase sensitivity */
345 else if (false_alarms < min_false_alarms) {
346
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100347 D_CALIB("norm FA %u < min FA %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800348 false_alarms, min_false_alarms);
349
350 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
351 data->auto_corr_ofdm =
352 max((u32)ranges->auto_corr_min_ofdm, val);
353
354 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
355 data->auto_corr_ofdm_mrc =
356 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
357
358 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
359 data->auto_corr_ofdm_x1 =
360 max((u32)ranges->auto_corr_min_ofdm_x1, val);
361
362 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
363 data->auto_corr_ofdm_mrc_x1 =
364 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
365 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100366 D_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800367 min_false_alarms, false_alarms, max_false_alarms);
368 }
369 return 0;
370}
371
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200372static void il4965_prepare_legacy_sensitivity_tbl(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200373 struct il_sensitivity_data *data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800374 __le16 *tbl)
375{
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200376 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800377 cpu_to_le16((u16)data->auto_corr_ofdm);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200378 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800379 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200380 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800381 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200382 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800383 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
384
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200385 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800386 cpu_to_le16((u16)data->auto_corr_cck);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200387 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800388 cpu_to_le16((u16)data->auto_corr_cck_mrc);
389
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200390 tbl[HD_MIN_ENERGY_CCK_DET_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800391 cpu_to_le16((u16)data->nrg_th_cck);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200392 tbl[HD_MIN_ENERGY_OFDM_DET_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800393 cpu_to_le16((u16)data->nrg_th_ofdm);
394
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200395 tbl[HD_BARKER_CORR_TH_ADD_MIN_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800396 cpu_to_le16(data->barker_corr_th_min);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200397 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800398 cpu_to_le16(data->barker_corr_th_min_mrc);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200399 tbl[HD_OFDM_ENERGY_TH_IN_IDX] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800400 cpu_to_le16(data->nrg_th_cca);
401
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100402 D_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800403 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
404 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
405 data->nrg_th_ofdm);
406
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100407 D_CALIB("cck: ac %u mrc %u thresh %u\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800408 data->auto_corr_cck, data->auto_corr_cck_mrc,
409 data->nrg_th_cck);
410}
411
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200412/* Prepare a C_SENSITIVITY, send to uCode if values have changed */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200413static int il4965_sensitivity_write(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800414{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200415 struct il_sensitivity_cmd cmd;
416 struct il_sensitivity_data *data = NULL;
417 struct il_host_cmd cmd_out = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200418 .id = C_SENSITIVITY,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200419 .len = sizeof(struct il_sensitivity_cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800420 .flags = CMD_ASYNC,
421 .data = &cmd,
422 };
423
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200424 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800425
426 memset(&cmd, 0, sizeof(cmd));
427
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200428 il4965_prepare_legacy_sensitivity_tbl(il, data, &cmd.table[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800429
430 /* Update uCode's "work" table, and copy it to DSP */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200431 cmd.control = C_SENSITIVITY_CONTROL_WORK_TBL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800432
433 /* Don't send command to uCode if nothing has changed */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200434 if (!memcmp(&cmd.table[0], &(il->sensitivity_tbl[0]),
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +0200435 sizeof(u16)*HD_TBL_SIZE)) {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200436 D_CALIB("No change in C_SENSITIVITY\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800437 return 0;
438 }
439
440 /* Copy table for comparison next time */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200441 memcpy(&(il->sensitivity_tbl[0]), &(cmd.table[0]),
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +0200442 sizeof(u16)*HD_TBL_SIZE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800443
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200444 return il_send_cmd(il, &cmd_out);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800445}
446
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200447void il4965_init_sensitivity(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800448{
449 int ret = 0;
450 int i;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200451 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200452 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800453
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200454 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800455 return;
456
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100457 D_CALIB("Start il4965_init_sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800458
459 /* Clear driver's sensitivity algo data */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200460 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800461
462 if (ranges == NULL)
463 return;
464
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200465 memset(data, 0, sizeof(struct il_sensitivity_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800466
467 data->num_in_cck_no_fa = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200468 data->nrg_curr_state = IL_FA_TOO_MANY;
469 data->nrg_prev_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800470 data->nrg_silence_ref = 0;
471 data->nrg_silence_idx = 0;
472 data->nrg_energy_idx = 0;
473
474 for (i = 0; i < 10; i++)
475 data->nrg_value[i] = 0;
476
477 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
478 data->nrg_silence_rssi[i] = 0;
479
480 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
481 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
482 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
483 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
484 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
485 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
486 data->nrg_th_cck = ranges->nrg_th_cck;
487 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
488 data->barker_corr_th_min = ranges->barker_corr_th_min;
489 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
490 data->nrg_th_cca = ranges->nrg_th_cca;
491
492 data->last_bad_plcp_cnt_ofdm = 0;
493 data->last_fa_cnt_ofdm = 0;
494 data->last_bad_plcp_cnt_cck = 0;
495 data->last_fa_cnt_cck = 0;
496
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200497 ret |= il4965_sensitivity_write(il);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100498 D_CALIB("<<return 0x%X\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800499}
500
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200501void il4965_sensitivity_calibration(struct il_priv *il, void *resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800502{
503 u32 rx_enable_time;
504 u32 fa_cck;
505 u32 fa_ofdm;
506 u32 bad_plcp_cck;
507 u32 bad_plcp_ofdm;
508 u32 norm_fa_ofdm;
509 u32 norm_fa_cck;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200510 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200511 struct stats_rx_non_phy *rx_info;
512 struct stats_rx_phy *ofdm, *cck;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800513 unsigned long flags;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200514 struct stats_general_data statis;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800515
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200516 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800517 return;
518
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200519 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800520
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200521 if (!il_is_any_associated(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100522 D_CALIB("<< - not associated\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800523 return;
524 }
525
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200526 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800527
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200528 rx_info = &(((struct il_notif_stats *)resp)->rx.general);
529 ofdm = &(((struct il_notif_stats *)resp)->rx.ofdm);
530 cck = &(((struct il_notif_stats *)resp)->rx.cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800531
532 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100533 D_CALIB("<< invalid data.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200534 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800535 return;
536 }
537
538 /* Extract Statistics: */
539 rx_enable_time = le32_to_cpu(rx_info->channel_load);
540 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
541 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
542 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
543 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
544
545 statis.beacon_silence_rssi_a =
546 le32_to_cpu(rx_info->beacon_silence_rssi_a);
547 statis.beacon_silence_rssi_b =
548 le32_to_cpu(rx_info->beacon_silence_rssi_b);
549 statis.beacon_silence_rssi_c =
550 le32_to_cpu(rx_info->beacon_silence_rssi_c);
551 statis.beacon_energy_a =
552 le32_to_cpu(rx_info->beacon_energy_a);
553 statis.beacon_energy_b =
554 le32_to_cpu(rx_info->beacon_energy_b);
555 statis.beacon_energy_c =
556 le32_to_cpu(rx_info->beacon_energy_c);
557
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200558 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800559
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100560 D_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800561
562 if (!rx_enable_time) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100563 D_CALIB("<< RX Enable Time == 0!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800564 return;
565 }
566
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200567 /* These stats increase monotonically, and do not reset
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800568 * at each beacon. Calculate difference from last value, or just
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200569 * use the new stats value if it has reset or wrapped around. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800570 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
571 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
572 else {
573 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
574 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
575 }
576
577 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
578 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
579 else {
580 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
581 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
582 }
583
584 if (data->last_fa_cnt_ofdm > fa_ofdm)
585 data->last_fa_cnt_ofdm = fa_ofdm;
586 else {
587 fa_ofdm -= data->last_fa_cnt_ofdm;
588 data->last_fa_cnt_ofdm += fa_ofdm;
589 }
590
591 if (data->last_fa_cnt_cck > fa_cck)
592 data->last_fa_cnt_cck = fa_cck;
593 else {
594 fa_cck -= data->last_fa_cnt_cck;
595 data->last_fa_cnt_cck += fa_cck;
596 }
597
598 /* Total aborted signal locks */
599 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
600 norm_fa_cck = fa_cck + bad_plcp_cck;
601
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100602 D_CALIB(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800603 "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
604 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
605
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200606 il4965_sens_auto_corr_ofdm(il, norm_fa_ofdm, rx_enable_time);
607 il4965_sens_energy_cck(il, norm_fa_cck, rx_enable_time, &statis);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800608
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200609 il4965_sensitivity_write(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800610}
611
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200612static inline u8 il4965_find_first_chain(u8 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800613{
614 if (mask & ANT_A)
615 return CHAIN_A;
616 if (mask & ANT_B)
617 return CHAIN_B;
618 return CHAIN_C;
619}
620
621/**
622 * Run disconnected antenna algorithm to find out which antennas are
623 * disconnected.
624 */
625static void
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200626il4965_find_disconn_antenna(struct il_priv *il, u32* average_sig,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200627 struct il_chain_noise_data *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800628{
629 u32 active_chains = 0;
630 u32 max_average_sig;
631 u16 max_average_sig_antenna_i;
632 u8 num_tx_chains;
633 u8 first_chain;
634 u16 i = 0;
635
636 average_sig[0] = data->chain_signal_a /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200637 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800638 average_sig[1] = data->chain_signal_b /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200639 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800640 average_sig[2] = data->chain_signal_c /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200641 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800642
643 if (average_sig[0] >= average_sig[1]) {
644 max_average_sig = average_sig[0];
645 max_average_sig_antenna_i = 0;
646 active_chains = (1 << max_average_sig_antenna_i);
647 } else {
648 max_average_sig = average_sig[1];
649 max_average_sig_antenna_i = 1;
650 active_chains = (1 << max_average_sig_antenna_i);
651 }
652
653 if (average_sig[2] >= max_average_sig) {
654 max_average_sig = average_sig[2];
655 max_average_sig_antenna_i = 2;
656 active_chains = (1 << max_average_sig_antenna_i);
657 }
658
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100659 D_CALIB("average_sig: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800660 average_sig[0], average_sig[1], average_sig[2]);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100661 D_CALIB("max_average_sig = %d, antenna %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800662 max_average_sig, max_average_sig_antenna_i);
663
664 /* Compare signal strengths for all 3 receivers. */
665 for (i = 0; i < NUM_RX_CHAINS; i++) {
666 if (i != max_average_sig_antenna_i) {
667 s32 rssi_delta = (max_average_sig - average_sig[i]);
668
669 /* If signal is very weak, compared with
670 * strongest, mark it as disconnected. */
671 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
672 data->disconn_array[i] = 1;
673 else
674 active_chains |= (1 << i);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100675 D_CALIB("i = %d rssiDelta = %d "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800676 "disconn_array[i] = %d\n",
677 i, rssi_delta, data->disconn_array[i]);
678 }
679 }
680
681 /*
682 * The above algorithm sometimes fails when the ucode
683 * reports 0 for all chains. It's not clear why that
684 * happens to start with, but it is then causing trouble
685 * because this can make us enable more chains than the
686 * hardware really has.
687 *
688 * To be safe, simply mask out any chains that we know
689 * are not on the device.
690 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200691 active_chains &= il->hw_params.valid_rx_ant;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800692
693 num_tx_chains = 0;
694 for (i = 0; i < NUM_RX_CHAINS; i++) {
695 /* loops on all the bits of
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200696 * il->hw_setting.valid_tx_ant */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800697 u8 ant_msk = (1 << i);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200698 if (!(il->hw_params.valid_tx_ant & ant_msk))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800699 continue;
700
701 num_tx_chains++;
702 if (data->disconn_array[i] == 0)
703 /* there is a Tx antenna connected */
704 break;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200705 if (num_tx_chains == il->hw_params.tx_chains_num &&
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800706 data->disconn_array[i]) {
707 /*
708 * If all chains are disconnected
709 * connect the first valid tx chain
710 */
711 first_chain =
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200712 il4965_find_first_chain(il->cfg->valid_tx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800713 data->disconn_array[first_chain] = 0;
714 active_chains |= BIT(first_chain);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100715 D_CALIB(
Joe Perches85ee7a12011-04-23 20:38:19 -0700716 "All Tx chains are disconnected W/A - declare %d as connected\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800717 first_chain);
718 break;
719 }
720 }
721
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200722 if (active_chains != il->hw_params.valid_rx_ant &&
723 active_chains != il->chain_noise_data.active_chains)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100724 D_CALIB(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800725 "Detected that not all antennas are connected! "
726 "Connected: %#x, valid: %#x.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200727 active_chains, il->hw_params.valid_rx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800728
729 /* Save for use within RXON, TX, SCAN commands, etc. */
730 data->active_chains = active_chains;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100731 D_CALIB("active_chains (bitwise) = 0x%x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800732 active_chains);
733}
734
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200735static void il4965_gain_computation(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800736 u32 *average_noise,
737 u16 min_average_noise_antenna_i,
738 u32 min_average_noise,
739 u8 default_chain)
740{
741 int i, ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200742 struct il_chain_noise_data *data = &il->chain_noise_data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800743
744 data->delta_gain_code[min_average_noise_antenna_i] = 0;
745
746 for (i = default_chain; i < NUM_RX_CHAINS; i++) {
747 s32 delta_g = 0;
748
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200749 if (!data->disconn_array[i] &&
750 data->delta_gain_code[i] == CHAIN_NOISE_DELTA_GAIN_INIT_VAL) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800751 delta_g = average_noise[i] - min_average_noise;
752 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
753 data->delta_gain_code[i] =
754 min(data->delta_gain_code[i],
755 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
756
757 data->delta_gain_code[i] =
758 (data->delta_gain_code[i] | (1 << 2));
759 } else {
760 data->delta_gain_code[i] = 0;
761 }
762 }
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100763 D_CALIB("delta_gain_codes: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800764 data->delta_gain_code[0],
765 data->delta_gain_code[1],
766 data->delta_gain_code[2]);
767
768 /* Differential gain gets sent to uCode only once */
769 if (!data->radio_write) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200770 struct il_calib_diff_gain_cmd cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800771 data->radio_write = 1;
772
773 memset(&cmd, 0, sizeof(cmd));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200774 cmd.hdr.op_code = IL_PHY_CALIBRATE_DIFF_GAIN_CMD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800775 cmd.diff_gain_a = data->delta_gain_code[0];
776 cmd.diff_gain_b = data->delta_gain_code[1];
777 cmd.diff_gain_c = data->delta_gain_code[2];
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200778 ret = il_send_cmd_pdu(il, C_PHY_CALIBRATION,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800779 sizeof(cmd), &cmd);
780 if (ret)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100781 D_CALIB("fail sending cmd "
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200782 "C_PHY_CALIBRATION\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800783
784 /* TODO we might want recalculate
785 * rx_chain in rxon cmd */
786
787 /* Mark so we run this algo only once! */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200788 data->state = IL_CHAIN_NOISE_CALIBRATED;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800789 }
790}
791
792
793
794/*
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200795 * Accumulate 16 beacons of signal and noise stats for each of
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800796 * 3 receivers/antennas/rx-chains, then figure out:
797 * 1) Which antennas are connected.
798 * 2) Differential rx gain settings to balance the 3 receivers.
799 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200800void il4965_chain_noise_calibration(struct il_priv *il, void *stat_resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800801{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200802 struct il_chain_noise_data *data = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800803
804 u32 chain_noise_a;
805 u32 chain_noise_b;
806 u32 chain_noise_c;
807 u32 chain_sig_a;
808 u32 chain_sig_b;
809 u32 chain_sig_c;
810 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
811 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
812 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
813 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
814 u16 i = 0;
815 u16 rxon_chnum = INITIALIZATION_VALUE;
816 u16 stat_chnum = INITIALIZATION_VALUE;
817 u8 rxon_band24;
818 u8 stat_band24;
819 unsigned long flags;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200820 struct stats_rx_non_phy *rx_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800821
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +0100822 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800823
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200824 if (il->disable_chain_noise_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800825 return;
826
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200827 data = &(il->chain_noise_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800828
829 /*
830 * Accumulate just the first "chain_noise_num_beacons" after
831 * the first association, then we're done forever.
832 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200833 if (data->state != IL_CHAIN_NOISE_ACCUMULATE) {
834 if (data->state == IL_CHAIN_NOISE_ALIVE)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100835 D_CALIB("Wait for noise calib reset\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800836 return;
837 }
838
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200839 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800840
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200841 rx_info = &(((struct il_notif_stats *)stat_resp)->
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800842 rx.general);
843
844 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100845 D_CALIB(" << Interference data unavailable\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200846 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800847 return;
848 }
849
850 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
851 rxon_chnum = le16_to_cpu(ctx->staging.channel);
852
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200853 stat_band24 = !!(((struct il_notif_stats *)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800854 stat_resp)->flag &
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +0100855 STATS_REPLY_FLG_BAND_24G_MSK);
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200856 stat_chnum = le32_to_cpu(((struct il_notif_stats *)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800857 stat_resp)->flag) >> 16;
858
859 /* Make sure we accumulate data for just the associated channel
860 * (even if scanning). */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200861 if (rxon_chnum != stat_chnum || rxon_band24 != stat_band24) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100862 D_CALIB("Stats not from chan=%d, band24=%d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800863 rxon_chnum, rxon_band24);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200864 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800865 return;
866 }
867
868 /*
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200869 * Accumulate beacon stats values across
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800870 * "chain_noise_num_beacons"
871 */
872 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
873 IN_BAND_FILTER;
874 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
875 IN_BAND_FILTER;
876 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
877 IN_BAND_FILTER;
878
879 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
880 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
881 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
882
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200883 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800884
885 data->beacon_count++;
886
887 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
888 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
889 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
890
891 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
892 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
893 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
894
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100895 D_CALIB("chan=%d, band24=%d, beacon=%d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800896 rxon_chnum, rxon_band24, data->beacon_count);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100897 D_CALIB("chain_sig: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800898 chain_sig_a, chain_sig_b, chain_sig_c);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100899 D_CALIB("chain_noise: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800900 chain_noise_a, chain_noise_b, chain_noise_c);
901
902 /* If this is the "chain_noise_num_beacons", determine:
903 * 1) Disconnected antennas (using signal strengths)
904 * 2) Differential gain (using silence noise) to balance receivers */
905 if (data->beacon_count !=
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200906 il->cfg->base_params->chain_noise_num_beacons)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800907 return;
908
909 /* Analyze signal for disconnected antenna */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200910 il4965_find_disconn_antenna(il, average_sig, data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800911
912 /* Analyze noise for rx balance */
913 average_noise[0] = data->chain_noise_a /
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200914 il->cfg->base_params->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800915 average_noise[1] = data->chain_noise_b /
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[2] = data->chain_noise_c /
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
920 for (i = 0; i < NUM_RX_CHAINS; i++) {
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200921 if (!data->disconn_array[i] &&
922 average_noise[i] <= min_average_noise) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800923 /* This means that chain i is active and has
924 * lower noise values so far: */
925 min_average_noise = average_noise[i];
926 min_average_noise_antenna_i = i;
927 }
928 }
929
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100930 D_CALIB("average_noise: a %d b %d c %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800931 average_noise[0], average_noise[1],
932 average_noise[2]);
933
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100934 D_CALIB("min_average_noise = %d, antenna %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800935 min_average_noise, min_average_noise_antenna_i);
936
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200937 il4965_gain_computation(il, average_noise,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800938 min_average_noise_antenna_i, min_average_noise,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200939 il4965_find_first_chain(il->cfg->valid_rx_ant));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800940
941 /* Some power changes may have been made during the calibration.
942 * Update and commit the RXON
943 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200944 if (il->cfg->ops->lib->update_chain_flags)
945 il->cfg->ops->lib->update_chain_flags(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800946
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200947 data->state = IL_CHAIN_NOISE_DONE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200948 il_power_update_mode(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800949}
950
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200951void il4965_reset_run_time_calib(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800952{
953 int i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200954 memset(&(il->sensitivity_data), 0,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200955 sizeof(struct il_sensitivity_data));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200956 memset(&(il->chain_noise_data), 0,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200957 sizeof(struct il_chain_noise_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800958 for (i = 0; i < NUM_RX_CHAINS; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200959 il->chain_noise_data.delta_gain_code[i] =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800960 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
961
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200962 /* Ask for stats now, the uCode will send notification
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800963 * periodically after association */
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200964 il_send_stats_request(il, CMD_ASYNC, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800965}