blob: e78bdefb8952ca605ae8eb1d3243274b80bdd63f [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
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080082/*****************************************************************************
83 * RUNTIME calibrations framework
84 *****************************************************************************/
85
86/* "false alarms" are signals that our DSP tries to lock onto,
87 * but then determines that they are either noise, or transmissions
88 * from a distant wireless network (also "noise", really) that get
89 * "stepped on" by stronger transmissions within our own network.
90 * This algorithm attempts to set a sensitivity level that is high
91 * enough to receive all of our own network traffic, but not so
92 * high that our DSP gets too busy trying to lock onto non-network
93 * activity/noise. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +010094static int
95il4965_sens_energy_cck(struct il_priv *il, u32 norm_fa, u32 rx_enable_time,
96 struct stats_general_data *rx_info)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080097{
98 u32 max_nrg_cck = 0;
99 int i = 0;
100 u8 max_silence_rssi = 0;
101 u32 silence_ref = 0;
102 u8 silence_rssi_a = 0;
103 u8 silence_rssi_b = 0;
104 u8 silence_rssi_c = 0;
105 u32 val;
106
107 /* "false_alarms" values below are cross-multiplications to assess the
108 * numbers of false alarms within the measured period of actual Rx
109 * (Rx is off when we're txing), vs the min/max expected false alarms
110 * (some should be expected if rx is sensitive enough) in a
111 * hypothetical listening period of 200 time units (TU), 204.8 msec:
112 *
113 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
114 *
115 * */
116 u32 false_alarms = norm_fa * 200 * 1024;
117 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
118 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200119 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200120 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800121
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200122 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800123
124 data->nrg_auto_corr_silence_diff = 0;
125
126 /* Find max silence rssi among all 3 receivers.
127 * This is background noise, which may include transmissions from other
128 * networks, measured during silence before our network's beacon */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100129 silence_rssi_a =
130 (u8) ((rx_info->beacon_silence_rssi_a & ALL_BAND_FILTER) >> 8);
131 silence_rssi_b =
132 (u8) ((rx_info->beacon_silence_rssi_b & ALL_BAND_FILTER) >> 8);
133 silence_rssi_c =
134 (u8) ((rx_info->beacon_silence_rssi_c & ALL_BAND_FILTER) >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800135
136 val = max(silence_rssi_b, silence_rssi_c);
137 max_silence_rssi = max(silence_rssi_a, (u8) val);
138
139 /* Store silence rssi in 20-beacon history table */
140 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
141 data->nrg_silence_idx++;
142 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
143 data->nrg_silence_idx = 0;
144
145 /* Find max silence rssi across 20 beacon history */
146 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
147 val = data->nrg_silence_rssi[i];
148 silence_ref = max(silence_ref, val);
149 }
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100150 D_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n", silence_rssi_a,
151 silence_rssi_b, silence_rssi_c, silence_ref);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800152
153 /* Find max rx energy (min value!) among all 3 receivers,
154 * measured during beacon frame.
155 * Save it in 10-beacon history table. */
156 i = data->nrg_energy_idx;
157 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
158 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
159
160 data->nrg_energy_idx++;
161 if (data->nrg_energy_idx >= 10)
162 data->nrg_energy_idx = 0;
163
164 /* Find min rx energy (max value) across 10 beacon history.
165 * This is the minimum signal level that we want to receive well.
166 * Add backoff (margin so we don't miss slightly lower energy frames).
167 * This establishes an upper bound (min value) for energy threshold. */
168 max_nrg_cck = data->nrg_value[0];
169 for (i = 1; i < 10; i++)
170 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
171 max_nrg_cck += 6;
172
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100173 D_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100174 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
175 rx_info->beacon_energy_c, max_nrg_cck - 6);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800176
177 /* Count number of consecutive beacons with fewer-than-desired
178 * false alarms. */
179 if (false_alarms < min_false_alarms)
180 data->num_in_cck_no_fa++;
181 else
182 data->num_in_cck_no_fa = 0;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100183 D_CALIB("consecutive bcns with few false alarms = %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100184 data->num_in_cck_no_fa);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800185
186 /* If we got too many false alarms this time, reduce sensitivity */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200187 if (false_alarms > max_false_alarms &&
188 data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100189 D_CALIB("norm FA %u > max FA %u\n", false_alarms,
190 max_false_alarms);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100191 D_CALIB("... reducing sensitivity\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200192 data->nrg_curr_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800193 /* Store for "fewer than desired" on later beacon */
194 data->nrg_silence_ref = silence_ref;
195
196 /* increase energy threshold (reduce nrg value)
197 * to decrease sensitivity */
198 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100199 /* Else if we got fewer than desired, increase sensitivity */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800200 } else if (false_alarms < min_false_alarms) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200201 data->nrg_curr_state = IL_FA_TOO_FEW;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800202
203 /* Compare silence level with silence level for most recent
204 * healthy number or too many false alarms */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100205 data->nrg_auto_corr_silence_diff =
206 (s32) data->nrg_silence_ref - (s32) silence_ref;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800207
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100208 D_CALIB("norm FA %u < min FA %u, silence diff %d\n",
209 false_alarms, min_false_alarms,
210 data->nrg_auto_corr_silence_diff);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800211
212 /* Increase value to increase sensitivity, but only if:
213 * 1a) previous beacon did *not* have *too many* false alarms
214 * 1b) AND there's a significant difference in Rx levels
215 * from a previous beacon with too many, or healthy # FAs
216 * OR 2) We've seen a lot of beacons (100) with too few
217 * false alarms */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200218 if (data->nrg_prev_state != IL_FA_TOO_MANY &&
219 (data->nrg_auto_corr_silence_diff > NRG_DIFF ||
220 data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800221
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100222 D_CALIB("... increasing sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800223 /* Increase nrg value to increase sensitivity */
224 val = data->nrg_th_cck + NRG_STEP_CCK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100225 data->nrg_th_cck = min((u32) ranges->min_nrg_cck, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800226 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100227 D_CALIB("... but not changing sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800228 }
229
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100230 /* Else we got a healthy number of false alarms, keep status quo */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800231 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100232 D_CALIB(" FA in safe zone\n");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200233 data->nrg_curr_state = IL_FA_GOOD_RANGE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800234
235 /* Store for use in "fewer than desired" with later beacon */
236 data->nrg_silence_ref = silence_ref;
237
238 /* If previous beacon had too many false alarms,
239 * give it some extra margin by reducing sensitivity again
240 * (but don't go below measured energy of desired Rx) */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200241 if (IL_FA_TOO_MANY == data->nrg_prev_state) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100242 D_CALIB("... increasing margin\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800243 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
244 data->nrg_th_cck -= NRG_MARGIN;
245 else
246 data->nrg_th_cck = max_nrg_cck;
247 }
248 }
249
250 /* Make sure the energy threshold does not go above the measured
251 * energy of the desired Rx signals (reduced by backoff margin),
252 * or else we might start missing Rx frames.
253 * Lower value is higher energy, so we use max()!
254 */
255 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100256 D_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800257
258 data->nrg_prev_state = data->nrg_curr_state;
259
260 /* Auto-correlation CCK algorithm */
261 if (false_alarms > min_false_alarms) {
262
263 /* increase auto_corr values to decrease sensitivity
264 * so the DSP won't be disturbed by the noise
265 */
266 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
267 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
268 else {
269 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
270 data->auto_corr_cck =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100271 min((u32) ranges->auto_corr_max_cck, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800272 }
273 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
274 data->auto_corr_cck_mrc =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100275 min((u32) ranges->auto_corr_max_cck_mrc, val);
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200276 } else if (false_alarms < min_false_alarms &&
277 (data->nrg_auto_corr_silence_diff > NRG_DIFF ||
278 data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800279
280 /* Decrease auto_corr values to increase sensitivity */
281 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100282 data->auto_corr_cck = max((u32) ranges->auto_corr_min_cck, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800283 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
284 data->auto_corr_cck_mrc =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100285 max((u32) ranges->auto_corr_min_cck_mrc, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800286 }
287
288 return 0;
289}
290
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100291static int
292il4965_sens_auto_corr_ofdm(struct il_priv *il, u32 norm_fa, u32 rx_enable_time)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800293{
294 u32 val;
295 u32 false_alarms = norm_fa * 200 * 1024;
296 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
297 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200298 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200299 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800300
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200301 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800302
303 /* If we got too many false alarms this time, reduce sensitivity */
304 if (false_alarms > max_false_alarms) {
305
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100306 D_CALIB("norm FA %u > max FA %u)\n", false_alarms,
307 max_false_alarms);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800308
309 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
310 data->auto_corr_ofdm =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100311 min((u32) ranges->auto_corr_max_ofdm, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800312
313 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
314 data->auto_corr_ofdm_mrc =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100315 min((u32) ranges->auto_corr_max_ofdm_mrc, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800316
317 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
318 data->auto_corr_ofdm_x1 =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100319 min((u32) ranges->auto_corr_max_ofdm_x1, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800320
321 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
322 data->auto_corr_ofdm_mrc_x1 =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100323 min((u32) ranges->auto_corr_max_ofdm_mrc_x1, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800324 }
325
326 /* Else if we got fewer than desired, increase sensitivity */
327 else if (false_alarms < min_false_alarms) {
328
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100329 D_CALIB("norm FA %u < min FA %u\n", false_alarms,
330 min_false_alarms);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800331
332 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
333 data->auto_corr_ofdm =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100334 max((u32) ranges->auto_corr_min_ofdm, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800335
336 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
337 data->auto_corr_ofdm_mrc =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100338 max((u32) ranges->auto_corr_min_ofdm_mrc, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800339
340 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
341 data->auto_corr_ofdm_x1 =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100342 max((u32) ranges->auto_corr_min_ofdm_x1, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800343
344 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
345 data->auto_corr_ofdm_mrc_x1 =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100346 max((u32) ranges->auto_corr_min_ofdm_mrc_x1, val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800347 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100348 D_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100349 min_false_alarms, false_alarms, max_false_alarms);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800350 }
351 return 0;
352}
353
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100354static void
355il4965_prepare_legacy_sensitivity_tbl(struct il_priv *il,
356 struct il_sensitivity_data *data,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100357 __le16 *tbl)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800358{
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200359 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100360 cpu_to_le16((u16) data->auto_corr_ofdm);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200361 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100362 cpu_to_le16((u16) data->auto_corr_ofdm_mrc);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200363 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100364 cpu_to_le16((u16) data->auto_corr_ofdm_x1);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200365 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100366 cpu_to_le16((u16) data->auto_corr_ofdm_mrc_x1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800367
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200368 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100369 cpu_to_le16((u16) data->auto_corr_cck);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200370 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100371 cpu_to_le16((u16) data->auto_corr_cck_mrc);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800372
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100373 tbl[HD_MIN_ENERGY_CCK_DET_IDX] = cpu_to_le16((u16) data->nrg_th_cck);
374 tbl[HD_MIN_ENERGY_OFDM_DET_IDX] = cpu_to_le16((u16) data->nrg_th_ofdm);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800375
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200376 tbl[HD_BARKER_CORR_TH_ADD_MIN_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100377 cpu_to_le16(data->barker_corr_th_min);
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +0200378 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_IDX] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100379 cpu_to_le16(data->barker_corr_th_min_mrc);
380 tbl[HD_OFDM_ENERGY_TH_IN_IDX] = cpu_to_le16(data->nrg_th_cca);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800381
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100382 D_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100383 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
384 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
385 data->nrg_th_ofdm);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800386
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100387 D_CALIB("cck: ac %u mrc %u thresh %u\n", data->auto_corr_cck,
388 data->auto_corr_cck_mrc, data->nrg_th_cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800389}
390
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200391/* Prepare a C_SENSITIVITY, send to uCode if values have changed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100392static int
393il4965_sensitivity_write(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800394{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200395 struct il_sensitivity_cmd cmd;
396 struct il_sensitivity_data *data = NULL;
397 struct il_host_cmd cmd_out = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200398 .id = C_SENSITIVITY,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200399 .len = sizeof(struct il_sensitivity_cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800400 .flags = CMD_ASYNC,
401 .data = &cmd,
402 };
403
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200404 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800405
406 memset(&cmd, 0, sizeof(cmd));
407
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200408 il4965_prepare_legacy_sensitivity_tbl(il, data, &cmd.table[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800409
410 /* Update uCode's "work" table, and copy it to DSP */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200411 cmd.control = C_SENSITIVITY_CONTROL_WORK_TBL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800412
413 /* Don't send command to uCode if nothing has changed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100414 if (!memcmp
415 (&cmd.table[0], &(il->sensitivity_tbl[0]),
416 sizeof(u16) * HD_TBL_SIZE)) {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200417 D_CALIB("No change in C_SENSITIVITY\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800418 return 0;
419 }
420
421 /* Copy table for comparison next time */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200422 memcpy(&(il->sensitivity_tbl[0]), &(cmd.table[0]),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100423 sizeof(u16) * HD_TBL_SIZE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800424
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200425 return il_send_cmd(il, &cmd_out);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800426}
427
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100428void
429il4965_init_sensitivity(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800430{
431 int ret = 0;
432 int i;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200433 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200434 const struct il_sensitivity_ranges *ranges = il->hw_params.sens;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800435
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200436 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800437 return;
438
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100439 D_CALIB("Start il4965_init_sensitivity\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800440
441 /* Clear driver's sensitivity algo data */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200442 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800443
444 if (ranges == NULL)
445 return;
446
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200447 memset(data, 0, sizeof(struct il_sensitivity_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800448
449 data->num_in_cck_no_fa = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200450 data->nrg_curr_state = IL_FA_TOO_MANY;
451 data->nrg_prev_state = IL_FA_TOO_MANY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800452 data->nrg_silence_ref = 0;
453 data->nrg_silence_idx = 0;
454 data->nrg_energy_idx = 0;
455
456 for (i = 0; i < 10; i++)
457 data->nrg_value[i] = 0;
458
459 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
460 data->nrg_silence_rssi[i] = 0;
461
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100462 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800463 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100464 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800465 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
466 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
467 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
468 data->nrg_th_cck = ranges->nrg_th_cck;
469 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
470 data->barker_corr_th_min = ranges->barker_corr_th_min;
471 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
472 data->nrg_th_cca = ranges->nrg_th_cca;
473
474 data->last_bad_plcp_cnt_ofdm = 0;
475 data->last_fa_cnt_ofdm = 0;
476 data->last_bad_plcp_cnt_cck = 0;
477 data->last_fa_cnt_cck = 0;
478
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200479 ret |= il4965_sensitivity_write(il);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100480 D_CALIB("<<return 0x%X\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800481}
482
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100483void
484il4965_sensitivity_calibration(struct il_priv *il, void *resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800485{
486 u32 rx_enable_time;
487 u32 fa_cck;
488 u32 fa_ofdm;
489 u32 bad_plcp_cck;
490 u32 bad_plcp_ofdm;
491 u32 norm_fa_ofdm;
492 u32 norm_fa_cck;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200493 struct il_sensitivity_data *data = NULL;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200494 struct stats_rx_non_phy *rx_info;
495 struct stats_rx_phy *ofdm, *cck;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800496 unsigned long flags;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200497 struct stats_general_data statis;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800498
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200499 if (il->disable_sens_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800500 return;
501
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200502 data = &(il->sensitivity_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800503
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200504 if (!il_is_any_associated(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100505 D_CALIB("<< - not associated\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800506 return;
507 }
508
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200509 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800510
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200511 rx_info = &(((struct il_notif_stats *)resp)->rx.general);
512 ofdm = &(((struct il_notif_stats *)resp)->rx.ofdm);
513 cck = &(((struct il_notif_stats *)resp)->rx.cck);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800514
515 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100516 D_CALIB("<< invalid data.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200517 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800518 return;
519 }
520
521 /* Extract Statistics: */
522 rx_enable_time = le32_to_cpu(rx_info->channel_load);
523 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
524 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
525 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
526 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
527
528 statis.beacon_silence_rssi_a =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100529 le32_to_cpu(rx_info->beacon_silence_rssi_a);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800530 statis.beacon_silence_rssi_b =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100531 le32_to_cpu(rx_info->beacon_silence_rssi_b);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800532 statis.beacon_silence_rssi_c =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100533 le32_to_cpu(rx_info->beacon_silence_rssi_c);
534 statis.beacon_energy_a = le32_to_cpu(rx_info->beacon_energy_a);
535 statis.beacon_energy_b = le32_to_cpu(rx_info->beacon_energy_b);
536 statis.beacon_energy_c = le32_to_cpu(rx_info->beacon_energy_c);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800537
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200538 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800539
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100540 D_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800541
542 if (!rx_enable_time) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100543 D_CALIB("<< RX Enable Time == 0!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800544 return;
545 }
546
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200547 /* These stats increase monotonically, and do not reset
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800548 * at each beacon. Calculate difference from last value, or just
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200549 * use the new stats value if it has reset or wrapped around. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800550 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
551 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
552 else {
553 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
554 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
555 }
556
557 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
558 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
559 else {
560 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
561 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
562 }
563
564 if (data->last_fa_cnt_ofdm > fa_ofdm)
565 data->last_fa_cnt_ofdm = fa_ofdm;
566 else {
567 fa_ofdm -= data->last_fa_cnt_ofdm;
568 data->last_fa_cnt_ofdm += fa_ofdm;
569 }
570
571 if (data->last_fa_cnt_cck > fa_cck)
572 data->last_fa_cnt_cck = fa_cck;
573 else {
574 fa_cck -= data->last_fa_cnt_cck;
575 data->last_fa_cnt_cck += fa_cck;
576 }
577
578 /* Total aborted signal locks */
579 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
580 norm_fa_cck = fa_cck + bad_plcp_cck;
581
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100582 D_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
583 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800584
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200585 il4965_sens_auto_corr_ofdm(il, norm_fa_ofdm, rx_enable_time);
586 il4965_sens_energy_cck(il, norm_fa_cck, rx_enable_time, &statis);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800587
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200588 il4965_sensitivity_write(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800589}
590
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100591static inline u8
592il4965_find_first_chain(u8 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800593{
594 if (mask & ANT_A)
595 return CHAIN_A;
596 if (mask & ANT_B)
597 return CHAIN_B;
598 return CHAIN_C;
599}
600
601/**
602 * Run disconnected antenna algorithm to find out which antennas are
603 * disconnected.
604 */
605static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100606il4965_find_disconn_antenna(struct il_priv *il, u32 * average_sig,
607 struct il_chain_noise_data *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800608{
609 u32 active_chains = 0;
610 u32 max_average_sig;
611 u16 max_average_sig_antenna_i;
612 u8 num_tx_chains;
613 u8 first_chain;
614 u16 i = 0;
615
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100616 average_sig[0] =
617 data->chain_signal_a /
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100618 il->cfg->chain_noise_num_beacons;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100619 average_sig[1] =
620 data->chain_signal_b /
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100621 il->cfg->chain_noise_num_beacons;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100622 average_sig[2] =
623 data->chain_signal_c /
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100624 il->cfg->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800625
626 if (average_sig[0] >= average_sig[1]) {
627 max_average_sig = average_sig[0];
628 max_average_sig_antenna_i = 0;
629 active_chains = (1 << max_average_sig_antenna_i);
630 } else {
631 max_average_sig = average_sig[1];
632 max_average_sig_antenna_i = 1;
633 active_chains = (1 << max_average_sig_antenna_i);
634 }
635
636 if (average_sig[2] >= max_average_sig) {
637 max_average_sig = average_sig[2];
638 max_average_sig_antenna_i = 2;
639 active_chains = (1 << max_average_sig_antenna_i);
640 }
641
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100642 D_CALIB("average_sig: a %d b %d c %d\n", average_sig[0], average_sig[1],
643 average_sig[2]);
644 D_CALIB("max_average_sig = %d, antenna %d\n", max_average_sig,
645 max_average_sig_antenna_i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800646
647 /* Compare signal strengths for all 3 receivers. */
648 for (i = 0; i < NUM_RX_CHAINS; i++) {
649 if (i != max_average_sig_antenna_i) {
650 s32 rssi_delta = (max_average_sig - average_sig[i]);
651
652 /* If signal is very weak, compared with
653 * strongest, mark it as disconnected. */
654 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
655 data->disconn_array[i] = 1;
656 else
657 active_chains |= (1 << i);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100658 D_CALIB("i = %d rssiDelta = %d "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100659 "disconn_array[i] = %d\n", i, rssi_delta,
660 data->disconn_array[i]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800661 }
662 }
663
664 /*
665 * The above algorithm sometimes fails when the ucode
666 * reports 0 for all chains. It's not clear why that
667 * happens to start with, but it is then causing trouble
668 * because this can make us enable more chains than the
669 * hardware really has.
670 *
671 * To be safe, simply mask out any chains that we know
672 * are not on the device.
673 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200674 active_chains &= il->hw_params.valid_rx_ant;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800675
676 num_tx_chains = 0;
677 for (i = 0; i < NUM_RX_CHAINS; i++) {
678 /* loops on all the bits of
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200679 * il->hw_setting.valid_tx_ant */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800680 u8 ant_msk = (1 << i);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200681 if (!(il->hw_params.valid_tx_ant & ant_msk))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800682 continue;
683
684 num_tx_chains++;
685 if (data->disconn_array[i] == 0)
686 /* there is a Tx antenna connected */
687 break;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200688 if (num_tx_chains == il->hw_params.tx_chains_num &&
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800689 data->disconn_array[i]) {
690 /*
691 * If all chains are disconnected
692 * connect the first valid tx chain
693 */
694 first_chain =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100695 il4965_find_first_chain(il->cfg->valid_tx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800696 data->disconn_array[first_chain] = 0;
697 active_chains |= BIT(first_chain);
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100698 D_CALIB("All Tx chains are disconnected"
699 "- declare %d as connected\n", first_chain);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800700 break;
701 }
702 }
703
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200704 if (active_chains != il->hw_params.valid_rx_ant &&
705 active_chains != il->chain_noise_data.active_chains)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100706 D_CALIB("Detected that not all antennas are connected! "
707 "Connected: %#x, valid: %#x.\n", active_chains,
708 il->hw_params.valid_rx_ant);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800709
710 /* Save for use within RXON, TX, SCAN commands, etc. */
711 data->active_chains = active_chains;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100712 D_CALIB("active_chains (bitwise) = 0x%x\n", active_chains);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800713}
714
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100715static void
716il4965_gain_computation(struct il_priv *il, u32 * average_noise,
717 u16 min_average_noise_antenna_i, u32 min_average_noise,
718 u8 default_chain)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800719{
720 int i, ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200721 struct il_chain_noise_data *data = &il->chain_noise_data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800722
723 data->delta_gain_code[min_average_noise_antenna_i] = 0;
724
725 for (i = default_chain; i < NUM_RX_CHAINS; i++) {
726 s32 delta_g = 0;
727
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200728 if (!data->disconn_array[i] &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100729 data->delta_gain_code[i] ==
730 CHAIN_NOISE_DELTA_GAIN_INIT_VAL) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800731 delta_g = average_noise[i] - min_average_noise;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100732 data->delta_gain_code[i] = (u8) ((delta_g * 10) / 15);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800733 data->delta_gain_code[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100734 min(data->delta_gain_code[i],
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800735 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
736
737 data->delta_gain_code[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100738 (data->delta_gain_code[i] | (1 << 2));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800739 } else {
740 data->delta_gain_code[i] = 0;
741 }
742 }
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100743 D_CALIB("delta_gain_codes: a %d b %d c %d\n", data->delta_gain_code[0],
744 data->delta_gain_code[1], data->delta_gain_code[2]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800745
746 /* Differential gain gets sent to uCode only once */
747 if (!data->radio_write) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200748 struct il_calib_diff_gain_cmd cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800749 data->radio_write = 1;
750
751 memset(&cmd, 0, sizeof(cmd));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200752 cmd.hdr.op_code = IL_PHY_CALIBRATE_DIFF_GAIN_CMD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800753 cmd.diff_gain_a = data->delta_gain_code[0];
754 cmd.diff_gain_b = data->delta_gain_code[1];
755 cmd.diff_gain_c = data->delta_gain_code[2];
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100756 ret = il_send_cmd_pdu(il, C_PHY_CALIBRATION, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800757 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100758 D_CALIB("fail sending cmd " "C_PHY_CALIBRATION\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800759
760 /* TODO we might want recalculate
761 * rx_chain in rxon cmd */
762
763 /* Mark so we run this algo only once! */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200764 data->state = IL_CHAIN_NOISE_CALIBRATED;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800765 }
766}
767
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800768/*
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200769 * Accumulate 16 beacons of signal and noise stats for each of
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800770 * 3 receivers/antennas/rx-chains, then figure out:
771 * 1) Which antennas are connected.
772 * 2) Differential rx gain settings to balance the 3 receivers.
773 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100774void
775il4965_chain_noise_calibration(struct il_priv *il, void *stat_resp)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800776{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200777 struct il_chain_noise_data *data = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800778
779 u32 chain_noise_a;
780 u32 chain_noise_b;
781 u32 chain_noise_c;
782 u32 chain_sig_a;
783 u32 chain_sig_b;
784 u32 chain_sig_c;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100785 u32 average_sig[NUM_RX_CHAINS] = { INITIALIZATION_VALUE };
786 u32 average_noise[NUM_RX_CHAINS] = { INITIALIZATION_VALUE };
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800787 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
788 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
789 u16 i = 0;
790 u16 rxon_chnum = INITIALIZATION_VALUE;
791 u16 stat_chnum = INITIALIZATION_VALUE;
792 u8 rxon_band24;
793 u8 stat_band24;
794 unsigned long flags;
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200795 struct stats_rx_non_phy *rx_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800796
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200797 if (il->disable_chain_noise_cal)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800798 return;
799
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200800 data = &(il->chain_noise_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800801
802 /*
803 * Accumulate just the first "chain_noise_num_beacons" after
804 * the first association, then we're done forever.
805 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200806 if (data->state != IL_CHAIN_NOISE_ACCUMULATE) {
807 if (data->state == IL_CHAIN_NOISE_ALIVE)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100808 D_CALIB("Wait for noise calib reset\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800809 return;
810 }
811
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200812 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800813
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100814 rx_info = &(((struct il_notif_stats *)stat_resp)->rx.general);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800815
816 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100817 D_CALIB(" << Interference data unavailable\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200818 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800819 return;
820 }
821
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +0100822 rxon_band24 = !!(il->staging.flags & RXON_FLG_BAND_24G_MSK);
823 rxon_chnum = le16_to_cpu(il->staging.channel);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800824
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100825 stat_band24 =
826 !!(((struct il_notif_stats *)stat_resp)->
827 flag & STATS_REPLY_FLG_BAND_24G_MSK);
828 stat_chnum =
829 le32_to_cpu(((struct il_notif_stats *)stat_resp)->flag) >> 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800830
831 /* Make sure we accumulate data for just the associated channel
832 * (even if scanning). */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200833 if (rxon_chnum != stat_chnum || rxon_band24 != stat_band24) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100834 D_CALIB("Stats not from chan=%d, band24=%d\n", rxon_chnum,
835 rxon_band24);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200836 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800837 return;
838 }
839
840 /*
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200841 * Accumulate beacon stats values across
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800842 * "chain_noise_num_beacons"
843 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100844 chain_noise_a =
845 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
846 chain_noise_b =
847 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
848 chain_noise_c =
849 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800850
851 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
852 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
853 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
854
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200855 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800856
857 data->beacon_count++;
858
859 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
860 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
861 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
862
863 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
864 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
865 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
866
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100867 D_CALIB("chan=%d, band24=%d, beacon=%d\n", rxon_chnum, rxon_band24,
868 data->beacon_count);
869 D_CALIB("chain_sig: a %d b %d c %d\n", chain_sig_a, chain_sig_b,
870 chain_sig_c);
871 D_CALIB("chain_noise: a %d b %d c %d\n", chain_noise_a, chain_noise_b,
872 chain_noise_c);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800873
874 /* If this is the "chain_noise_num_beacons", determine:
875 * 1) Disconnected antennas (using signal strengths)
876 * 2) Differential gain (using silence noise) to balance receivers */
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100877 if (data->beacon_count != il->cfg->chain_noise_num_beacons)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800878 return;
879
880 /* Analyze signal for disconnected antenna */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200881 il4965_find_disconn_antenna(il, average_sig, data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800882
883 /* Analyze noise for rx balance */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100884 average_noise[0] =
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100885 data->chain_noise_a / il->cfg->chain_noise_num_beacons;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100886 average_noise[1] =
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100887 data->chain_noise_b / il->cfg->chain_noise_num_beacons;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100888 average_noise[2] =
Stanislaw Gruszka89ef1ed2012-02-03 17:31:59 +0100889 data->chain_noise_c / il->cfg->chain_noise_num_beacons;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800890
891 for (i = 0; i < NUM_RX_CHAINS; i++) {
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200892 if (!data->disconn_array[i] &&
893 average_noise[i] <= min_average_noise) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800894 /* This means that chain i is active and has
895 * lower noise values so far: */
896 min_average_noise = average_noise[i];
897 min_average_noise_antenna_i = i;
898 }
899 }
900
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100901 D_CALIB("average_noise: a %d b %d c %d\n", average_noise[0],
902 average_noise[1], average_noise[2]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800903
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100904 D_CALIB("min_average_noise = %d, antenna %d\n", min_average_noise,
905 min_average_noise_antenna_i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800906
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100907 il4965_gain_computation(il, average_noise, min_average_noise_antenna_i,
908 min_average_noise,
909 il4965_find_first_chain(il->cfg->valid_rx_ant));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800910
911 /* Some power changes may have been made during the calibration.
912 * Update and commit the RXON
913 */
Stanislaw Gruszka1600b872012-02-13 11:23:18 +0100914 if (il->ops->update_chain_flags)
915 il->ops->update_chain_flags(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800916
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200917 data->state = IL_CHAIN_NOISE_DONE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200918 il_power_update_mode(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800919}
920
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100921void
922il4965_reset_run_time_calib(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800923{
924 int i;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100925 memset(&(il->sensitivity_data), 0, sizeof(struct il_sensitivity_data));
926 memset(&(il->chain_noise_data), 0, sizeof(struct il_chain_noise_data));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800927 for (i = 0; i < NUM_RX_CHAINS; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200928 il->chain_noise_data.delta_gain_code[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100929 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800930
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200931 /* Ask for stats now, the uCode will send notification
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800932 * periodically after association */
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200933 il_send_stats_request(il, CMD_ASYNC, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800934}