blob: 4928f6f52bf1458c518904e3d2df62ece927aac3 [file] [log] [blame]
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08008 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080028 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070029 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -080033 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070064#include <net/mac80211.h>
65
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070066#include "iwl-dev.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070067#include "iwl-core.h"
Johannes Berg0de76732010-09-22 18:02:11 +020068#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070069#include "iwl-trans.h"
Don Fry5c3d29f2011-07-08 08:46:29 -070070#include "iwl-agn.h"
Emmanuel Grumbach637d79252012-02-07 10:55:46 +020071#include "iwl-wifi.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070072
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080073/*****************************************************************************
74 * INIT calibrations framework
75 *****************************************************************************/
76
Johannes Berge1991882012-03-06 13:30:36 -080077/* Opaque calibration results */
78struct iwl_calib_result {
79 struct list_head list;
80 size_t cmd_len;
81 struct iwl_calib_hdr hdr;
82 /* data follows */
83};
84
Winkler, Tomas34c22cf2008-12-17 16:52:27 +080085struct statistics_general_data {
86 u32 beacon_silence_rssi_a;
87 u32 beacon_silence_rssi_b;
88 u32 beacon_silence_rssi_c;
89 u32 beacon_energy_a;
90 u32 beacon_energy_b;
91 u32 beacon_energy_c;
92};
93
Johannes Berge1991882012-03-06 13:30:36 -080094int iwl_send_calib_results(struct iwl_priv *priv)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080095{
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080096 struct iwl_host_cmd hcmd = {
97 .id = REPLY_PHY_CALIBRATION_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070098 .flags = CMD_SYNC,
Tomas Winkler6e21f2c2008-09-03 11:26:37 +080099 };
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800100 struct iwl_calib_result *res;
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800101
Johannes Berge1991882012-03-06 13:30:36 -0800102 list_for_each_entry(res, &priv->calib_results, list) {
Johannes Berg93b64102011-11-17 08:51:53 -0800103 int ret;
104
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800105 hcmd.len[0] = res->cmd_len;
106 hcmd.data[0] = &res->hdr;
Johannes Berg93b64102011-11-17 08:51:53 -0800107 hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
Johannes Berge1991882012-03-06 13:30:36 -0800108 ret = iwl_trans_send_cmd(trans(priv), &hcmd);
Johannes Berg93b64102011-11-17 08:51:53 -0800109 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800110 IWL_ERR(priv, "Error %d on calib cmd %d\n",
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800111 ret, res->hdr.op_code);
Johannes Berg93b64102011-11-17 08:51:53 -0800112 return ret;
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800113 }
Tomas Winklerbe5d56e2008-10-08 09:37:27 +0800114 }
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800115
Johannes Berg93b64102011-11-17 08:51:53 -0800116 return 0;
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800117}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800118
Johannes Berge1991882012-03-06 13:30:36 -0800119int iwl_calib_set(struct iwl_priv *priv,
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800120 const struct iwl_calib_hdr *cmd, int len)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800121{
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800122 struct iwl_calib_result *res, *tmp;
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800123
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800124 res = kmalloc(sizeof(*res) + len - sizeof(struct iwl_calib_hdr),
125 GFP_ATOMIC);
126 if (!res)
127 return -ENOMEM;
128 memcpy(&res->hdr, cmd, len);
129 res->cmd_len = len;
130
Johannes Berge1991882012-03-06 13:30:36 -0800131 list_for_each_entry(tmp, &priv->calib_results, list) {
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800132 if (tmp->hdr.op_code == res->hdr.op_code) {
133 list_replace(&tmp->list, &res->list);
134 kfree(tmp);
135 return 0;
136 }
137 }
138
139 /* wasn't in list already */
Johannes Berge1991882012-03-06 13:30:36 -0800140 list_add_tail(&res->list, &priv->calib_results);
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800141
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800142 return 0;
143}
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800144
Johannes Berge1991882012-03-06 13:30:36 -0800145void iwl_calib_free_results(struct iwl_priv *priv)
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800146{
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800147 struct iwl_calib_result *res, *tmp;
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800148
Johannes Berge1991882012-03-06 13:30:36 -0800149 list_for_each_entry_safe(res, tmp, &priv->calib_results, list) {
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800150 list_del(&res->list);
151 kfree(res);
Tomas Winkler6e21f2c2008-09-03 11:26:37 +0800152 }
153}
154
155/*****************************************************************************
156 * RUNTIME calibrations framework
157 *****************************************************************************/
158
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700159/* "false alarms" are signals that our DSP tries to lock onto,
160 * but then determines that they are either noise, or transmissions
161 * from a distant wireless network (also "noise", really) that get
162 * "stepped on" by stronger transmissions within our own network.
163 * This algorithm attempts to set a sensitivity level that is high
164 * enough to receive all of our own network traffic, but not so
165 * high that our DSP gets too busy trying to lock onto non-network
166 * activity/noise. */
167static int iwl_sens_energy_cck(struct iwl_priv *priv,
168 u32 norm_fa,
169 u32 rx_enable_time,
170 struct statistics_general_data *rx_info)
171{
172 u32 max_nrg_cck = 0;
173 int i = 0;
174 u8 max_silence_rssi = 0;
175 u32 silence_ref = 0;
176 u8 silence_rssi_a = 0;
177 u8 silence_rssi_b = 0;
178 u8 silence_rssi_c = 0;
179 u32 val;
180
181 /* "false_alarms" values below are cross-multiplications to assess the
182 * numbers of false alarms within the measured period of actual Rx
183 * (Rx is off when we're txing), vs the min/max expected false alarms
184 * (some should be expected if rx is sensitive enough) in a
185 * hypothetical listening period of 200 time units (TU), 204.8 msec:
186 *
187 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
188 *
189 * */
190 u32 false_alarms = norm_fa * 200 * 1024;
191 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
192 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
193 struct iwl_sensitivity_data *data = NULL;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700194 const struct iwl_sensitivity_ranges *ranges = hw_params(priv).sens;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700195
196 data = &(priv->sensitivity_data);
197
198 data->nrg_auto_corr_silence_diff = 0;
199
200 /* Find max silence rssi among all 3 receivers.
201 * This is background noise, which may include transmissions from other
202 * networks, measured during silence before our network's beacon */
203 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
204 ALL_BAND_FILTER) >> 8);
205 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
206 ALL_BAND_FILTER) >> 8);
207 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
208 ALL_BAND_FILTER) >> 8);
209
210 val = max(silence_rssi_b, silence_rssi_c);
211 max_silence_rssi = max(silence_rssi_a, (u8) val);
212
213 /* Store silence rssi in 20-beacon history table */
214 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
215 data->nrg_silence_idx++;
216 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
217 data->nrg_silence_idx = 0;
218
219 /* Find max silence rssi across 20 beacon history */
220 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
221 val = data->nrg_silence_rssi[i];
222 silence_ref = max(silence_ref, val);
223 }
Tomas Winklere1623442009-01-27 14:27:56 -0800224 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700225 silence_rssi_a, silence_rssi_b, silence_rssi_c,
226 silence_ref);
227
228 /* Find max rx energy (min value!) among all 3 receivers,
229 * measured during beacon frame.
230 * Save it in 10-beacon history table. */
231 i = data->nrg_energy_idx;
232 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
233 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
234
235 data->nrg_energy_idx++;
236 if (data->nrg_energy_idx >= 10)
237 data->nrg_energy_idx = 0;
238
239 /* Find min rx energy (max value) across 10 beacon history.
240 * This is the minimum signal level that we want to receive well.
241 * Add backoff (margin so we don't miss slightly lower energy frames).
242 * This establishes an upper bound (min value) for energy threshold. */
243 max_nrg_cck = data->nrg_value[0];
244 for (i = 1; i < 10; i++)
245 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
246 max_nrg_cck += 6;
247
Tomas Winklere1623442009-01-27 14:27:56 -0800248 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700249 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
250 rx_info->beacon_energy_c, max_nrg_cck - 6);
251
252 /* Count number of consecutive beacons with fewer-than-desired
253 * false alarms. */
254 if (false_alarms < min_false_alarms)
255 data->num_in_cck_no_fa++;
256 else
257 data->num_in_cck_no_fa = 0;
Tomas Winklere1623442009-01-27 14:27:56 -0800258 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700259 data->num_in_cck_no_fa);
260
261 /* If we got too many false alarms this time, reduce sensitivity */
262 if ((false_alarms > max_false_alarms) &&
263 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800264 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700265 false_alarms, max_false_alarms);
Tomas Winklere1623442009-01-27 14:27:56 -0800266 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700267 data->nrg_curr_state = IWL_FA_TOO_MANY;
268 /* Store for "fewer than desired" on later beacon */
269 data->nrg_silence_ref = silence_ref;
270
271 /* increase energy threshold (reduce nrg value)
272 * to decrease sensitivity */
Wey-Yi Guyfe6efb42009-06-12 13:22:54 -0700273 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700274 /* Else if we got fewer than desired, increase sensitivity */
275 } else if (false_alarms < min_false_alarms) {
276 data->nrg_curr_state = IWL_FA_TOO_FEW;
277
278 /* Compare silence level with silence level for most recent
279 * healthy number or too many false alarms */
280 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
281 (s32)silence_ref;
282
Tomas Winklere1623442009-01-27 14:27:56 -0800283 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700284 false_alarms, min_false_alarms,
285 data->nrg_auto_corr_silence_diff);
286
287 /* Increase value to increase sensitivity, but only if:
288 * 1a) previous beacon did *not* have *too many* false alarms
289 * 1b) AND there's a significant difference in Rx levels
290 * from a previous beacon with too many, or healthy # FAs
291 * OR 2) We've seen a lot of beacons (100) with too few
292 * false alarms */
293 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
294 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
295 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
296
Tomas Winklere1623442009-01-27 14:27:56 -0800297 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700298 /* Increase nrg value to increase sensitivity */
299 val = data->nrg_th_cck + NRG_STEP_CCK;
300 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
301 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800302 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700303 }
304
305 /* Else we got a healthy number of false alarms, keep status quo */
306 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800307 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700308 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
309
310 /* Store for use in "fewer than desired" with later beacon */
311 data->nrg_silence_ref = silence_ref;
312
313 /* If previous beacon had too many false alarms,
314 * give it some extra margin by reducing sensitivity again
315 * (but don't go below measured energy of desired Rx) */
316 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
Tomas Winklere1623442009-01-27 14:27:56 -0800317 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700318 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
319 data->nrg_th_cck -= NRG_MARGIN;
320 else
321 data->nrg_th_cck = max_nrg_cck;
322 }
323 }
324
325 /* Make sure the energy threshold does not go above the measured
326 * energy of the desired Rx signals (reduced by backoff margin),
327 * or else we might start missing Rx frames.
328 * Lower value is higher energy, so we use max()!
329 */
330 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
Tomas Winklere1623442009-01-27 14:27:56 -0800331 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700332
333 data->nrg_prev_state = data->nrg_curr_state;
334
335 /* Auto-correlation CCK algorithm */
336 if (false_alarms > min_false_alarms) {
337
338 /* increase auto_corr values to decrease sensitivity
339 * so the DSP won't be disturbed by the noise
340 */
341 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
342 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
343 else {
344 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
345 data->auto_corr_cck =
346 min((u32)ranges->auto_corr_max_cck, val);
347 }
348 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
349 data->auto_corr_cck_mrc =
350 min((u32)ranges->auto_corr_max_cck_mrc, val);
351 } else if ((false_alarms < min_false_alarms) &&
352 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
353 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
354
355 /* Decrease auto_corr values to increase sensitivity */
356 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
357 data->auto_corr_cck =
358 max((u32)ranges->auto_corr_min_cck, val);
359 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
360 data->auto_corr_cck_mrc =
361 max((u32)ranges->auto_corr_min_cck_mrc, val);
362 }
363
364 return 0;
365}
366
367
368static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
369 u32 norm_fa,
370 u32 rx_enable_time)
371{
372 u32 val;
373 u32 false_alarms = norm_fa * 200 * 1024;
374 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
375 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
376 struct iwl_sensitivity_data *data = NULL;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700377 const struct iwl_sensitivity_ranges *ranges = hw_params(priv).sens;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700378
379 data = &(priv->sensitivity_data);
380
381 /* If we got too many false alarms this time, reduce sensitivity */
382 if (false_alarms > max_false_alarms) {
383
Tomas Winklere1623442009-01-27 14:27:56 -0800384 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700385 false_alarms, max_false_alarms);
386
387 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
388 data->auto_corr_ofdm =
389 min((u32)ranges->auto_corr_max_ofdm, val);
390
391 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
392 data->auto_corr_ofdm_mrc =
393 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
394
395 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
396 data->auto_corr_ofdm_x1 =
397 min((u32)ranges->auto_corr_max_ofdm_x1, val);
398
399 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
400 data->auto_corr_ofdm_mrc_x1 =
401 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
402 }
403
404 /* Else if we got fewer than desired, increase sensitivity */
405 else if (false_alarms < min_false_alarms) {
406
Tomas Winklere1623442009-01-27 14:27:56 -0800407 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700408 false_alarms, min_false_alarms);
409
410 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
411 data->auto_corr_ofdm =
412 max((u32)ranges->auto_corr_min_ofdm, val);
413
414 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
415 data->auto_corr_ofdm_mrc =
416 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
417
418 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
419 data->auto_corr_ofdm_x1 =
420 max((u32)ranges->auto_corr_min_ofdm_x1, val);
421
422 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
423 data->auto_corr_ofdm_mrc_x1 =
424 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
425 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800426 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700427 min_false_alarms, false_alarms, max_false_alarms);
428 }
429 return 0;
430}
431
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700432static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
433 struct iwl_sensitivity_data *data,
434 __le16 *tbl)
435{
436 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
437 cpu_to_le16((u16)data->auto_corr_ofdm);
438 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
439 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
440 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
441 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
442 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
443 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
444
445 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
446 cpu_to_le16((u16)data->auto_corr_cck);
447 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
448 cpu_to_le16((u16)data->auto_corr_cck_mrc);
449
450 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
451 cpu_to_le16((u16)data->nrg_th_cck);
452 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
453 cpu_to_le16((u16)data->nrg_th_ofdm);
454
455 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
456 cpu_to_le16(data->barker_corr_th_min);
457 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
458 cpu_to_le16(data->barker_corr_th_min_mrc);
459 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
460 cpu_to_le16(data->nrg_th_cca);
461
462 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
463 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
464 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
465 data->nrg_th_ofdm);
466
467 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
468 data->auto_corr_cck, data->auto_corr_cck_mrc,
469 data->nrg_th_cck);
470}
471
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700472/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
473static int iwl_sensitivity_write(struct iwl_priv *priv)
474{
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700475 struct iwl_sensitivity_cmd cmd;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700476 struct iwl_sensitivity_data *data = NULL;
477 struct iwl_host_cmd cmd_out = {
478 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700479 .len = { sizeof(struct iwl_sensitivity_cmd), },
Johannes Bergc2acea82009-07-24 11:13:05 -0700480 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700481 .data = { &cmd, },
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700482 };
483
484 data = &(priv->sensitivity_data);
485
486 memset(&cmd, 0, sizeof(cmd));
487
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700488 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700489
490 /* Update uCode's "work" table, and copy it to DSP */
491 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
492
493 /* Don't send command to uCode if nothing has changed */
494 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
495 sizeof(u16)*HD_TABLE_SIZE)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800496 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700497 return 0;
498 }
499
500 /* Copy table for comparison next time */
501 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
502 sizeof(u16)*HD_TABLE_SIZE);
503
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700504 return iwl_trans_send_cmd(trans(priv), &cmd_out);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700505}
506
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700507/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
508static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
509{
510 struct iwl_enhance_sensitivity_cmd cmd;
511 struct iwl_sensitivity_data *data = NULL;
512 struct iwl_host_cmd cmd_out = {
513 .id = SENSITIVITY_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700514 .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700515 .flags = CMD_ASYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700516 .data = { &cmd, },
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700517 };
518
519 data = &(priv->sensitivity_data);
520
521 memset(&cmd, 0, sizeof(cmd));
522
523 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
524
Don Fry38622412011-12-16 07:07:36 -0800525 if (cfg(priv)->base_params->hd_v2) {
Wey-Yi Guyae7f9a72011-07-23 10:24:50 -0700526 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
527 HD_INA_NON_SQUARE_DET_OFDM_DATA_V2;
528 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
529 HD_INA_NON_SQUARE_DET_CCK_DATA_V2;
530 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
531 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V2;
532 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
533 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
534 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
535 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
536 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
537 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V2;
538 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
539 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V2;
540 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
541 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
542 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
543 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
544 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
545 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V2;
546 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
547 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V2;
548 } else {
549 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
550 HD_INA_NON_SQUARE_DET_OFDM_DATA_V1;
551 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
552 HD_INA_NON_SQUARE_DET_CCK_DATA_V1;
553 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
554 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V1;
555 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
556 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
557 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
558 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
559 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
560 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V1;
561 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
562 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V1;
563 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
564 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
565 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
566 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
567 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
568 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V1;
569 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
570 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V1;
571 }
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700572
573 /* Update uCode's "work" table, and copy it to DSP */
574 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
575
576 /* Don't send command to uCode if nothing has changed */
577 if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
578 sizeof(u16)*HD_TABLE_SIZE) &&
579 !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
580 &(priv->enhance_sensitivity_tbl[0]),
581 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
582 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
583 return 0;
584 }
585
586 /* Copy table for comparison next time */
587 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
588 sizeof(u16)*HD_TABLE_SIZE);
589 memcpy(&(priv->enhance_sensitivity_tbl[0]),
590 &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
591 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
592
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700593 return iwl_trans_send_cmd(trans(priv), &cmd_out);
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700594}
595
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700596void iwl_init_sensitivity(struct iwl_priv *priv)
597{
598 int ret = 0;
599 int i;
600 struct iwl_sensitivity_data *data = NULL;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700601 const struct iwl_sensitivity_ranges *ranges = hw_params(priv).sens;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700602
Tomas Winkler445c2df2008-05-15 13:54:16 +0800603 if (priv->disable_sens_cal)
604 return;
605
Tomas Winklere1623442009-01-27 14:27:56 -0800606 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700607
608 /* Clear driver's sensitivity algo data */
609 data = &(priv->sensitivity_data);
610
611 if (ranges == NULL)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700612 return;
613
614 memset(data, 0, sizeof(struct iwl_sensitivity_data));
615
616 data->num_in_cck_no_fa = 0;
617 data->nrg_curr_state = IWL_FA_TOO_MANY;
618 data->nrg_prev_state = IWL_FA_TOO_MANY;
619 data->nrg_silence_ref = 0;
620 data->nrg_silence_idx = 0;
621 data->nrg_energy_idx = 0;
622
623 for (i = 0; i < 10; i++)
624 data->nrg_value[i] = 0;
625
626 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
627 data->nrg_silence_rssi[i] = 0;
628
Wey-Yi Guyf3a2a422009-09-11 10:38:11 -0700629 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700630 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
631 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
632 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
633 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
634 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
635 data->nrg_th_cck = ranges->nrg_th_cck;
636 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
Wey-Yi Guy55036d62009-10-09 13:20:24 -0700637 data->barker_corr_th_min = ranges->barker_corr_th_min;
638 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
639 data->nrg_th_cca = ranges->nrg_th_cca;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700640
641 data->last_bad_plcp_cnt_ofdm = 0;
642 data->last_fa_cnt_ofdm = 0;
643 data->last_bad_plcp_cnt_cck = 0;
644 data->last_fa_cnt_cck = 0;
645
Johannes Berg0692fe42012-03-06 13:30:37 -0800646 if (priv->fw->enhance_sensitivity_table)
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700647 ret |= iwl_enhance_sensitivity_write(priv);
648 else
649 ret |= iwl_sensitivity_write(priv);
Tomas Winklere1623442009-01-27 14:27:56 -0800650 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700651}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700652
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700653void iwl_sensitivity_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700654{
655 u32 rx_enable_time;
656 u32 fa_cck;
657 u32 fa_ofdm;
658 u32 bad_plcp_cck;
659 u32 bad_plcp_ofdm;
660 u32 norm_fa_ofdm;
661 u32 norm_fa_cck;
662 struct iwl_sensitivity_data *data = NULL;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700663 struct statistics_rx_non_phy *rx_info;
664 struct statistics_rx_phy *ofdm, *cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700665 struct statistics_general_data statis;
666
Tomas Winkler445c2df2008-05-15 13:54:16 +0800667 if (priv->disable_sens_cal)
668 return;
669
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700670 data = &(priv->sensitivity_data);
671
Johannes Berg246ed352010-08-23 10:46:32 +0200672 if (!iwl_is_any_associated(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800673 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700674 return;
675 }
676
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800677 spin_lock_bh(&priv->statistics.lock);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700678 rx_info = &priv->statistics.rx_non_phy;
679 ofdm = &priv->statistics.rx_ofdm;
680 cck = &priv->statistics.rx_cck;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700681 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800682 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800683 spin_unlock_bh(&priv->statistics.lock);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700684 return;
685 }
686
687 /* Extract Statistics: */
688 rx_enable_time = le32_to_cpu(rx_info->channel_load);
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700689 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
690 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
691 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
692 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700693
694 statis.beacon_silence_rssi_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700695 le32_to_cpu(rx_info->beacon_silence_rssi_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700696 statis.beacon_silence_rssi_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700697 le32_to_cpu(rx_info->beacon_silence_rssi_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700698 statis.beacon_silence_rssi_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700699 le32_to_cpu(rx_info->beacon_silence_rssi_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700700 statis.beacon_energy_a =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700701 le32_to_cpu(rx_info->beacon_energy_a);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700702 statis.beacon_energy_b =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700703 le32_to_cpu(rx_info->beacon_energy_b);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700704 statis.beacon_energy_c =
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700705 le32_to_cpu(rx_info->beacon_energy_c);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700706
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800707 spin_unlock_bh(&priv->statistics.lock);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700708
Tomas Winklere1623442009-01-27 14:27:56 -0800709 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700710
711 if (!rx_enable_time) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700712 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700713 return;
714 }
715
716 /* These statistics increase monotonically, and do not reset
717 * at each beacon. Calculate difference from last value, or just
718 * use the new statistics value if it has reset or wrapped around. */
719 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
720 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
721 else {
722 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
723 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
724 }
725
726 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
727 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
728 else {
729 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
730 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
731 }
732
733 if (data->last_fa_cnt_ofdm > fa_ofdm)
734 data->last_fa_cnt_ofdm = fa_ofdm;
735 else {
736 fa_ofdm -= data->last_fa_cnt_ofdm;
737 data->last_fa_cnt_ofdm += fa_ofdm;
738 }
739
740 if (data->last_fa_cnt_cck > fa_cck)
741 data->last_fa_cnt_cck = fa_cck;
742 else {
743 fa_cck -= data->last_fa_cnt_cck;
744 data->last_fa_cnt_cck += fa_cck;
745 }
746
747 /* Total aborted signal locks */
748 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
749 norm_fa_cck = fa_cck + bad_plcp_cck;
750
Tomas Winklere1623442009-01-27 14:27:56 -0800751 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700752 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
753
754 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
755 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
Johannes Berg0692fe42012-03-06 13:30:37 -0800756 if (priv->fw->enhance_sensitivity_table)
Wey-Yi Guyc8312fa2010-06-28 13:05:17 -0700757 iwl_enhance_sensitivity_write(priv);
758 else
759 iwl_sensitivity_write(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700760}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700761
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700762static inline u8 find_first_chain(u8 mask)
763{
764 if (mask & ANT_A)
765 return CHAIN_A;
766 if (mask & ANT_B)
767 return CHAIN_B;
768 return CHAIN_C;
769}
770
Shanyu Zhao30312422010-11-10 09:56:48 -0800771/**
772 * Run disconnected antenna algorithm to find out which antennas are
773 * disconnected.
774 */
775static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
776 struct iwl_chain_noise_data *data)
777{
778 u32 active_chains = 0;
779 u32 max_average_sig;
780 u16 max_average_sig_antenna_i;
781 u8 num_tx_chains;
782 u8 first_chain;
783 u16 i = 0;
784
Fry, Donald Hb8c2b052011-09-15 11:46:38 -0700785 average_sig[0] = data->chain_signal_a / IWL_CAL_NUM_BEACONS;
786 average_sig[1] = data->chain_signal_b / IWL_CAL_NUM_BEACONS;
787 average_sig[2] = data->chain_signal_c / IWL_CAL_NUM_BEACONS;
Shanyu Zhao30312422010-11-10 09:56:48 -0800788
789 if (average_sig[0] >= average_sig[1]) {
790 max_average_sig = average_sig[0];
791 max_average_sig_antenna_i = 0;
792 active_chains = (1 << max_average_sig_antenna_i);
793 } else {
794 max_average_sig = average_sig[1];
795 max_average_sig_antenna_i = 1;
796 active_chains = (1 << max_average_sig_antenna_i);
797 }
798
799 if (average_sig[2] >= max_average_sig) {
800 max_average_sig = average_sig[2];
801 max_average_sig_antenna_i = 2;
802 active_chains = (1 << max_average_sig_antenna_i);
803 }
804
805 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
806 average_sig[0], average_sig[1], average_sig[2]);
807 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
808 max_average_sig, max_average_sig_antenna_i);
809
810 /* Compare signal strengths for all 3 receivers. */
811 for (i = 0; i < NUM_RX_CHAINS; i++) {
812 if (i != max_average_sig_antenna_i) {
813 s32 rssi_delta = (max_average_sig - average_sig[i]);
814
815 /* If signal is very weak, compared with
816 * strongest, mark it as disconnected. */
817 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
818 data->disconn_array[i] = 1;
819 else
820 active_chains |= (1 << i);
821 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
822 "disconn_array[i] = %d\n",
823 i, rssi_delta, data->disconn_array[i]);
824 }
825 }
826
827 /*
828 * The above algorithm sometimes fails when the ucode
829 * reports 0 for all chains. It's not clear why that
830 * happens to start with, but it is then causing trouble
831 * because this can make us enable more chains than the
832 * hardware really has.
833 *
834 * To be safe, simply mask out any chains that we know
835 * are not on the device.
836 */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700837 active_chains &= hw_params(priv).valid_rx_ant;
Shanyu Zhao30312422010-11-10 09:56:48 -0800838
839 num_tx_chains = 0;
840 for (i = 0; i < NUM_RX_CHAINS; i++) {
841 /* loops on all the bits of
842 * priv->hw_setting.valid_tx_ant */
843 u8 ant_msk = (1 << i);
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700844 if (!(hw_params(priv).valid_tx_ant & ant_msk))
Shanyu Zhao30312422010-11-10 09:56:48 -0800845 continue;
846
847 num_tx_chains++;
848 if (data->disconn_array[i] == 0)
849 /* there is a Tx antenna connected */
850 break;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700851 if (num_tx_chains == hw_params(priv).tx_chains_num &&
Shanyu Zhao30312422010-11-10 09:56:48 -0800852 data->disconn_array[i]) {
853 /*
854 * If all chains are disconnected
855 * connect the first valid tx chain
856 */
857 first_chain =
Johannes Berg7e79a392012-03-05 11:24:32 -0800858 find_first_chain(hw_params(priv).valid_tx_ant);
Shanyu Zhao30312422010-11-10 09:56:48 -0800859 data->disconn_array[first_chain] = 0;
860 active_chains |= BIT(first_chain);
Joe Perches85ee7a12011-04-23 20:38:19 -0700861 IWL_DEBUG_CALIB(priv,
862 "All Tx chains are disconnected W/A - declare %d as connected\n",
Shanyu Zhao30312422010-11-10 09:56:48 -0800863 first_chain);
864 break;
865 }
866 }
867
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700868 if (active_chains != hw_params(priv).valid_rx_ant &&
Shanyu Zhao30312422010-11-10 09:56:48 -0800869 active_chains != priv->chain_noise_data.active_chains)
870 IWL_DEBUG_CALIB(priv,
871 "Detected that not all antennas are connected! "
872 "Connected: %#x, valid: %#x.\n",
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700873 active_chains,
874 hw_params(priv).valid_rx_ant);
Shanyu Zhao30312422010-11-10 09:56:48 -0800875
876 /* Save for use within RXON, TX, SCAN commands, etc. */
877 data->active_chains = active_chains;
878 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
879 active_chains);
880}
881
Wey-Yi Guye505c432011-07-07 08:27:41 -0700882static void iwlagn_gain_computation(struct iwl_priv *priv,
883 u32 average_noise[NUM_RX_CHAINS],
884 u16 min_average_noise_antenna_i,
885 u32 min_average_noise,
886 u8 default_chain)
887{
888 int i;
889 s32 delta_g;
890 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
891
892 /*
893 * Find Gain Code for the chains based on "default chain"
894 */
895 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
896 if ((data->disconn_array[i])) {
897 data->delta_gain_code[i] = 0;
898 continue;
899 }
900
Don Fry38622412011-12-16 07:07:36 -0800901 delta_g = (cfg(priv)->base_params->chain_noise_scale *
Wey-Yi Guye505c432011-07-07 08:27:41 -0700902 ((s32)average_noise[default_chain] -
903 (s32)average_noise[i])) / 1500;
904
905 /* bound gain by 2 bits value max, 3rd bit is sign */
906 data->delta_gain_code[i] =
907 min(abs(delta_g),
908 (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
909
910 if (delta_g < 0)
911 /*
912 * set negative sign ...
913 * note to Intel developers: This is uCode API format,
914 * not the format of any internal device registers.
915 * Do not change this format for e.g. 6050 or similar
916 * devices. Change format only if more resolution
917 * (i.e. more than 2 bits magnitude) is needed.
918 */
919 data->delta_gain_code[i] |= (1 << 2);
920 }
921
922 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
923 data->delta_gain_code[1], data->delta_gain_code[2]);
924
925 if (!data->radio_write) {
926 struct iwl_calib_chain_noise_gain_cmd cmd;
927
928 memset(&cmd, 0, sizeof(cmd));
929
930 iwl_set_calib_hdr(&cmd.hdr,
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700931 priv->phy_calib_chain_noise_gain_cmd);
Wey-Yi Guye505c432011-07-07 08:27:41 -0700932 cmd.delta_gain_1 = data->delta_gain_code[1];
933 cmd.delta_gain_2 = data->delta_gain_code[2];
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700934 iwl_trans_send_cmd_pdu(trans(priv), REPLY_PHY_CALIBRATION_CMD,
Wey-Yi Guye505c432011-07-07 08:27:41 -0700935 CMD_ASYNC, sizeof(cmd), &cmd);
936
937 data->radio_write = 1;
938 data->state = IWL_CHAIN_NOISE_CALIBRATED;
939 }
940}
Shanyu Zhao30312422010-11-10 09:56:48 -0800941
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700942/*
Shanyu Zhao30312422010-11-10 09:56:48 -0800943 * Accumulate 16 beacons of signal and noise statistics for each of
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700944 * 3 receivers/antennas/rx-chains, then figure out:
945 * 1) Which antennas are connected.
946 * 2) Differential rx gain settings to balance the 3 receivers.
947 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700948void iwl_chain_noise_calibration(struct iwl_priv *priv)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700949{
950 struct iwl_chain_noise_data *data = NULL;
951
952 u32 chain_noise_a;
953 u32 chain_noise_b;
954 u32 chain_noise_c;
955 u32 chain_sig_a;
956 u32 chain_sig_b;
957 u32 chain_sig_c;
958 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
959 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700960 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
961 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
962 u16 i = 0;
963 u16 rxon_chnum = INITIALIZATION_VALUE;
964 u16 stat_chnum = INITIALIZATION_VALUE;
965 u8 rxon_band24;
966 u8 stat_band24;
Wey-Yi Guy7980fba2010-07-14 08:08:57 -0700967 struct statistics_rx_non_phy *rx_info;
Shanyu Zhao30312422010-11-10 09:56:48 -0800968
Johannes Berg246ed352010-08-23 10:46:32 +0200969 /*
970 * MULTI-FIXME:
971 * When we support multiple interfaces on different channels,
972 * this must be modified/fixed.
973 */
974 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700975
Tomas Winkler445c2df2008-05-15 13:54:16 +0800976 if (priv->disable_chain_noise_cal)
977 return;
978
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700979 data = &(priv->chain_noise_data);
980
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -0700981 /*
982 * Accumulate just the first "chain_noise_num_beacons" after
983 * the first association, then we're done forever.
984 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700985 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
986 if (data->state == IWL_CHAIN_NOISE_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -0800987 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700988 return;
989 }
990
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800991 spin_lock_bh(&priv->statistics.lock);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700992
993 rx_info = &priv->statistics.rx_non_phy;
994
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700995 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800996 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800997 spin_unlock_bh(&priv->statistics.lock);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700998 return;
999 }
1000
Johannes Berg246ed352010-08-23 10:46:32 +02001001 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
1002 rxon_chnum = le16_to_cpu(ctx->staging.channel);
Johannes Berg0da0e5b2011-04-08 08:14:56 -07001003 stat_band24 =
1004 !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
1005 stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001006
1007 /* Make sure we accumulate data for just the associated channel
1008 * (even if scanning). */
1009 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001010 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001011 rxon_chnum, rxon_band24);
Johannes Berg4ff70fc2012-03-05 11:24:26 -08001012 spin_unlock_bh(&priv->statistics.lock);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001013 return;
1014 }
1015
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -07001016 /*
1017 * Accumulate beacon statistics values across
1018 * "chain_noise_num_beacons"
1019 */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001020 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
1021 IN_BAND_FILTER;
1022 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
1023 IN_BAND_FILTER;
1024 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
1025 IN_BAND_FILTER;
1026
1027 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
1028 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
1029 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
1030
Johannes Berg4ff70fc2012-03-05 11:24:26 -08001031 spin_unlock_bh(&priv->statistics.lock);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001032
1033 data->beacon_count++;
1034
1035 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
1036 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
1037 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
1038
1039 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
1040 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
1041 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
1042
Tomas Winklere1623442009-01-27 14:27:56 -08001043 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001044 rxon_chnum, rxon_band24, data->beacon_count);
Tomas Winklere1623442009-01-27 14:27:56 -08001045 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001046 chain_sig_a, chain_sig_b, chain_sig_c);
Tomas Winklere1623442009-01-27 14:27:56 -08001047 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001048 chain_noise_a, chain_noise_b, chain_noise_c);
1049
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -07001050 /* If this is the "chain_noise_num_beacons", determine:
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001051 * 1) Disconnected antennas (using signal strengths)
1052 * 2) Differential gain (using silence noise) to balance receivers */
Fry, Donald Hb8c2b052011-09-15 11:46:38 -07001053 if (data->beacon_count != IWL_CAL_NUM_BEACONS)
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001054 return;
1055
1056 /* Analyze signal for disconnected antenna */
Don Fry38622412011-12-16 07:07:36 -08001057 if (cfg(priv)->bt_params &&
1058 cfg(priv)->bt_params->advanced_bt_coexist) {
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -08001059 /* Disable disconnected antenna algorithm for advanced
1060 bt coex, assuming valid antennas are connected */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001061 data->active_chains = hw_params(priv).valid_rx_ant;
Shanyu Zhao6fe8efb2010-11-10 09:56:49 -08001062 for (i = 0; i < NUM_RX_CHAINS; i++)
1063 if (!(data->active_chains & (1<<i)))
1064 data->disconn_array[i] = 1;
1065 } else
1066 iwl_find_disconn_antenna(priv, average_sig, data);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001067
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001068 /* Analyze noise for rx balance */
Fry, Donald Hb8c2b052011-09-15 11:46:38 -07001069 average_noise[0] = data->chain_noise_a / IWL_CAL_NUM_BEACONS;
1070 average_noise[1] = data->chain_noise_b / IWL_CAL_NUM_BEACONS;
1071 average_noise[2] = data->chain_noise_c / IWL_CAL_NUM_BEACONS;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001072
1073 for (i = 0; i < NUM_RX_CHAINS; i++) {
1074 if (!(data->disconn_array[i]) &&
1075 (average_noise[i] <= min_average_noise)) {
1076 /* This means that chain i is active and has
1077 * lower noise values so far: */
1078 min_average_noise = average_noise[i];
1079 min_average_noise_antenna_i = i;
1080 }
1081 }
1082
Tomas Winklere1623442009-01-27 14:27:56 -08001083 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001084 average_noise[0], average_noise[1],
1085 average_noise[2]);
1086
Tomas Winklere1623442009-01-27 14:27:56 -08001087 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001088 min_average_noise, min_average_noise_antenna_i);
1089
Don Fry5c3d29f2011-07-08 08:46:29 -07001090 iwlagn_gain_computation(priv, average_noise,
Wey-Yi Guyd8c07e72009-09-25 14:24:26 -07001091 min_average_noise_antenna_i, min_average_noise,
Johannes Berg7e79a392012-03-05 11:24:32 -08001092 find_first_chain(hw_params(priv).valid_rx_ant));
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08001093
1094 /* Some power changes may have been made during the calibration.
1095 * Update and commit the RXON
1096 */
Fry, Donald H6b6db912011-07-08 08:46:17 -07001097 iwl_update_chain_flags(priv);
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08001098
1099 data->state = IWL_CHAIN_NOISE_DONE;
Johannes Berge312c242009-08-07 15:41:51 -07001100 iwl_power_update_mode(priv, false);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001101}
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001102
1103void iwl_reset_run_time_calib(struct iwl_priv *priv)
1104{
1105 int i;
1106 memset(&(priv->sensitivity_data), 0,
1107 sizeof(struct iwl_sensitivity_data));
1108 memset(&(priv->chain_noise_data), 0,
1109 sizeof(struct iwl_chain_noise_data));
1110 for (i = 0; i < NUM_RX_CHAINS; i++)
1111 priv->chain_noise_data.delta_gain_code[i] =
1112 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
1113
1114 /* Ask for statistics now, the uCode will send notification
1115 * periodically after association */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -08001116 iwl_send_statistics_request(priv, CMD_ASYNC, true);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001117}