Merge branch 'akpm/master'
[deliverable/linux.git] / drivers / net / wireless / intel / iwlwifi / dvm / calib.c
CommitLineData
f0832f13
EG
1/******************************************************************************
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 *
51368bf7 8 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
f0832f13
EG
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
410dc5aa 25 * in the file called COPYING.
f0832f13
EG
26 *
27 * Contact Information:
d01c5366 28 * Intel Linux Wireless <linuxwifi@intel.com>
f0832f13
EG
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
51368bf7 33 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
f0832f13
EG
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
5a0e3ad6 63#include <linux/slab.h>
f0832f13
EG
64#include <net/mac80211.h>
65
bdfbf092 66#include "iwl-trans.h"
1023fdc4
JB
67
68#include "dev.h"
69#include "calib.h"
70#include "agn.h"
f0832f13 71
6e21f2c1
TW
72/*****************************************************************************
73 * INIT calibrations framework
74 *****************************************************************************/
75
e1991885
JB
76/* Opaque calibration results */
77struct iwl_calib_result {
78 struct list_head list;
79 size_t cmd_len;
80 struct iwl_calib_hdr hdr;
81 /* data follows */
82};
83
34c22cf9
WT
84struct statistics_general_data {
85 u32 beacon_silence_rssi_a;
86 u32 beacon_silence_rssi_b;
87 u32 beacon_silence_rssi_c;
88 u32 beacon_energy_a;
89 u32 beacon_energy_b;
90 u32 beacon_energy_c;
91};
92
e1991885 93int iwl_send_calib_results(struct iwl_priv *priv)
6e21f2c1 94{
6e21f2c1
TW
95 struct iwl_host_cmd hcmd = {
96 .id = REPLY_PHY_CALIBRATION_CMD,
6e21f2c1 97 };
f02c2fd3 98 struct iwl_calib_result *res;
6e21f2c1 99
e1991885 100 list_for_each_entry(res, &priv->calib_results, list) {
93b64105
JB
101 int ret;
102
f02c2fd3
JB
103 hcmd.len[0] = res->cmd_len;
104 hcmd.data[0] = &res->hdr;
93b64105 105 hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
e10a0533 106 ret = iwl_dvm_send_cmd(priv, &hcmd);
93b64105 107 if (ret) {
e1991885 108 IWL_ERR(priv, "Error %d on calib cmd %d\n",
f02c2fd3 109 ret, res->hdr.op_code);
93b64105 110 return ret;
6e21f2c1 111 }
be5d56ed 112 }
6e21f2c1 113
93b64105 114 return 0;
6e21f2c1 115}
6e21f2c1 116
e1991885 117int iwl_calib_set(struct iwl_priv *priv,
f02c2fd3 118 const struct iwl_calib_hdr *cmd, int len)
6e21f2c1 119{
f02c2fd3
JB
120 struct iwl_calib_result *res, *tmp;
121
122 res = kmalloc(sizeof(*res) + len - sizeof(struct iwl_calib_hdr),
123 GFP_ATOMIC);
124 if (!res)
6e21f2c1 125 return -ENOMEM;
f02c2fd3
JB
126 memcpy(&res->hdr, cmd, len);
127 res->cmd_len = len;
128
e1991885 129 list_for_each_entry(tmp, &priv->calib_results, list) {
f02c2fd3
JB
130 if (tmp->hdr.op_code == res->hdr.op_code) {
131 list_replace(&tmp->list, &res->list);
132 kfree(tmp);
133 return 0;
134 }
135 }
136
137 /* wasn't in list already */
e1991885 138 list_add_tail(&res->list, &priv->calib_results);
6e21f2c1 139
6e21f2c1
TW
140 return 0;
141}
6e21f2c1 142
e1991885 143void iwl_calib_free_results(struct iwl_priv *priv)
6e21f2c1 144{
f02c2fd3 145 struct iwl_calib_result *res, *tmp;
6e21f2c1 146
e1991885 147 list_for_each_entry_safe(res, tmp, &priv->calib_results, list) {
f02c2fd3
JB
148 list_del(&res->list);
149 kfree(res);
6e21f2c1
TW
150 }
151}
152
153/*****************************************************************************
154 * RUNTIME calibrations framework
155 *****************************************************************************/
156
f0832f13
EG
157/* "false alarms" are signals that our DSP tries to lock onto,
158 * but then determines that they are either noise, or transmissions
159 * from a distant wireless network (also "noise", really) that get
160 * "stepped on" by stronger transmissions within our own network.
161 * This algorithm attempts to set a sensitivity level that is high
162 * enough to receive all of our own network traffic, but not so
163 * high that our DSP gets too busy trying to lock onto non-network
164 * activity/noise. */
165static int iwl_sens_energy_cck(struct iwl_priv *priv,
166 u32 norm_fa,
167 u32 rx_enable_time,
168 struct statistics_general_data *rx_info)
169{
170 u32 max_nrg_cck = 0;
171 int i = 0;
172 u8 max_silence_rssi = 0;
173 u32 silence_ref = 0;
174 u8 silence_rssi_a = 0;
175 u8 silence_rssi_b = 0;
176 u8 silence_rssi_c = 0;
177 u32 val;
178
179 /* "false_alarms" values below are cross-multiplications to assess the
180 * numbers of false alarms within the measured period of actual Rx
181 * (Rx is off when we're txing), vs the min/max expected false alarms
182 * (some should be expected if rx is sensitive enough) in a
183 * hypothetical listening period of 200 time units (TU), 204.8 msec:
184 *
185 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
186 *
187 * */
188 u32 false_alarms = norm_fa * 200 * 1024;
189 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
190 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
191 struct iwl_sensitivity_data *data = NULL;
9e295116 192 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
f0832f13
EG
193
194 data = &(priv->sensitivity_data);
195
196 data->nrg_auto_corr_silence_diff = 0;
197
198 /* Find max silence rssi among all 3 receivers.
199 * This is background noise, which may include transmissions from other
200 * networks, measured during silence before our network's beacon */
201 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
202 ALL_BAND_FILTER) >> 8);
203 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
204 ALL_BAND_FILTER) >> 8);
205 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
206 ALL_BAND_FILTER) >> 8);
207
208 val = max(silence_rssi_b, silence_rssi_c);
209 max_silence_rssi = max(silence_rssi_a, (u8) val);
210
211 /* Store silence rssi in 20-beacon history table */
212 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
213 data->nrg_silence_idx++;
214 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
215 data->nrg_silence_idx = 0;
216
217 /* Find max silence rssi across 20 beacon history */
218 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
219 val = data->nrg_silence_rssi[i];
220 silence_ref = max(silence_ref, val);
221 }
e1623446 222 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
f0832f13
EG
223 silence_rssi_a, silence_rssi_b, silence_rssi_c,
224 silence_ref);
225
226 /* Find max rx energy (min value!) among all 3 receivers,
227 * measured during beacon frame.
228 * Save it in 10-beacon history table. */
229 i = data->nrg_energy_idx;
230 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
231 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
232
233 data->nrg_energy_idx++;
234 if (data->nrg_energy_idx >= 10)
235 data->nrg_energy_idx = 0;
236
237 /* Find min rx energy (max value) across 10 beacon history.
238 * This is the minimum signal level that we want to receive well.
239 * Add backoff (margin so we don't miss slightly lower energy frames).
240 * This establishes an upper bound (min value) for energy threshold. */
241 max_nrg_cck = data->nrg_value[0];
242 for (i = 1; i < 10; i++)
243 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
244 max_nrg_cck += 6;
245
e1623446 246 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
f0832f13
EG
247 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
248 rx_info->beacon_energy_c, max_nrg_cck - 6);
249
250 /* Count number of consecutive beacons with fewer-than-desired
251 * false alarms. */
252 if (false_alarms < min_false_alarms)
253 data->num_in_cck_no_fa++;
254 else
255 data->num_in_cck_no_fa = 0;
e1623446 256 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
f0832f13
EG
257 data->num_in_cck_no_fa);
258
259 /* If we got too many false alarms this time, reduce sensitivity */
260 if ((false_alarms > max_false_alarms) &&
261 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
e1623446 262 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
f0832f13 263 false_alarms, max_false_alarms);
e1623446 264 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
f0832f13
EG
265 data->nrg_curr_state = IWL_FA_TOO_MANY;
266 /* Store for "fewer than desired" on later beacon */
267 data->nrg_silence_ref = silence_ref;
268
269 /* increase energy threshold (reduce nrg value)
270 * to decrease sensitivity */
fe6efb4b 271 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
f0832f13
EG
272 /* Else if we got fewer than desired, increase sensitivity */
273 } else if (false_alarms < min_false_alarms) {
274 data->nrg_curr_state = IWL_FA_TOO_FEW;
275
276 /* Compare silence level with silence level for most recent
277 * healthy number or too many false alarms */
278 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
279 (s32)silence_ref;
280
e1623446 281 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
f0832f13
EG
282 false_alarms, min_false_alarms,
283 data->nrg_auto_corr_silence_diff);
284
285 /* Increase value to increase sensitivity, but only if:
286 * 1a) previous beacon did *not* have *too many* false alarms
287 * 1b) AND there's a significant difference in Rx levels
288 * from a previous beacon with too many, or healthy # FAs
289 * OR 2) We've seen a lot of beacons (100) with too few
290 * false alarms */
291 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
292 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
293 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
294
e1623446 295 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
f0832f13
EG
296 /* Increase nrg value to increase sensitivity */
297 val = data->nrg_th_cck + NRG_STEP_CCK;
298 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
299 } else {
e1623446 300 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
f0832f13
EG
301 }
302
303 /* Else we got a healthy number of false alarms, keep status quo */
304 } else {
e1623446 305 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
f0832f13
EG
306 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
307
308 /* Store for use in "fewer than desired" with later beacon */
309 data->nrg_silence_ref = silence_ref;
310
311 /* If previous beacon had too many false alarms,
312 * give it some extra margin by reducing sensitivity again
313 * (but don't go below measured energy of desired Rx) */
e70d41b5 314 if (data->nrg_prev_state == IWL_FA_TOO_MANY) {
e1623446 315 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
f0832f13
EG
316 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
317 data->nrg_th_cck -= NRG_MARGIN;
318 else
319 data->nrg_th_cck = max_nrg_cck;
320 }
321 }
322
323 /* Make sure the energy threshold does not go above the measured
324 * energy of the desired Rx signals (reduced by backoff margin),
325 * or else we might start missing Rx frames.
326 * Lower value is higher energy, so we use max()!
327 */
328 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
e1623446 329 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
f0832f13
EG
330
331 data->nrg_prev_state = data->nrg_curr_state;
332
333 /* Auto-correlation CCK algorithm */
334 if (false_alarms > min_false_alarms) {
335
336 /* increase auto_corr values to decrease sensitivity
337 * so the DSP won't be disturbed by the noise
338 */
339 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
340 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
341 else {
342 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
343 data->auto_corr_cck =
344 min((u32)ranges->auto_corr_max_cck, val);
345 }
346 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
347 data->auto_corr_cck_mrc =
348 min((u32)ranges->auto_corr_max_cck_mrc, val);
349 } else if ((false_alarms < min_false_alarms) &&
350 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
351 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
352
353 /* Decrease auto_corr values to increase sensitivity */
354 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
355 data->auto_corr_cck =
356 max((u32)ranges->auto_corr_min_cck, val);
357 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
358 data->auto_corr_cck_mrc =
359 max((u32)ranges->auto_corr_min_cck_mrc, val);
360 }
361
362 return 0;
363}
364
365
366static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
367 u32 norm_fa,
368 u32 rx_enable_time)
369{
370 u32 val;
371 u32 false_alarms = norm_fa * 200 * 1024;
372 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
373 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
374 struct iwl_sensitivity_data *data = NULL;
9e295116 375 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
f0832f13
EG
376
377 data = &(priv->sensitivity_data);
378
379 /* If we got too many false alarms this time, reduce sensitivity */
380 if (false_alarms > max_false_alarms) {
381
e1623446 382 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
f0832f13
EG
383 false_alarms, max_false_alarms);
384
385 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
386 data->auto_corr_ofdm =
387 min((u32)ranges->auto_corr_max_ofdm, val);
388
389 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
390 data->auto_corr_ofdm_mrc =
391 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
392
393 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
394 data->auto_corr_ofdm_x1 =
395 min((u32)ranges->auto_corr_max_ofdm_x1, val);
396
397 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
398 data->auto_corr_ofdm_mrc_x1 =
399 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
400 }
401
402 /* Else if we got fewer than desired, increase sensitivity */
403 else if (false_alarms < min_false_alarms) {
404
e1623446 405 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
f0832f13
EG
406 false_alarms, min_false_alarms);
407
408 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
409 data->auto_corr_ofdm =
410 max((u32)ranges->auto_corr_min_ofdm, val);
411
412 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
413 data->auto_corr_ofdm_mrc =
414 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
415
416 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
417 data->auto_corr_ofdm_x1 =
418 max((u32)ranges->auto_corr_min_ofdm_x1, val);
419
420 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
421 data->auto_corr_ofdm_mrc_x1 =
422 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
423 } else {
e1623446 424 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
f0832f13
EG
425 min_false_alarms, false_alarms, max_false_alarms);
426 }
427 return 0;
428}
429
c8312fac
WYG
430static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
431 struct iwl_sensitivity_data *data,
432 __le16 *tbl)
f0832f13 433{
c8312fac 434 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
f0832f13 435 cpu_to_le16((u16)data->auto_corr_ofdm);
c8312fac 436 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
f0832f13 437 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
c8312fac 438 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
f0832f13 439 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
c8312fac 440 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
f0832f13
EG
441 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
442
c8312fac 443 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
f0832f13 444 cpu_to_le16((u16)data->auto_corr_cck);
c8312fac 445 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
f0832f13
EG
446 cpu_to_le16((u16)data->auto_corr_cck_mrc);
447
c8312fac 448 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
f0832f13 449 cpu_to_le16((u16)data->nrg_th_cck);
c8312fac 450 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
f0832f13
EG
451 cpu_to_le16((u16)data->nrg_th_ofdm);
452
c8312fac 453 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
55036d66 454 cpu_to_le16(data->barker_corr_th_min);
c8312fac 455 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
55036d66 456 cpu_to_le16(data->barker_corr_th_min_mrc);
c8312fac 457 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
55036d66 458 cpu_to_le16(data->nrg_th_cca);
f0832f13 459
e1623446 460 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
f0832f13
EG
461 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
462 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
463 data->nrg_th_ofdm);
464
e1623446 465 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
f0832f13
EG
466 data->auto_corr_cck, data->auto_corr_cck_mrc,
467 data->nrg_th_cck);
c8312fac
WYG
468}
469
470/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
471static int iwl_sensitivity_write(struct iwl_priv *priv)
472{
473 struct iwl_sensitivity_cmd cmd;
474 struct iwl_sensitivity_data *data = NULL;
475 struct iwl_host_cmd cmd_out = {
476 .id = SENSITIVITY_CMD,
3fa50738 477 .len = { sizeof(struct iwl_sensitivity_cmd), },
c8312fac 478 .flags = CMD_ASYNC,
3fa50738 479 .data = { &cmd, },
c8312fac
WYG
480 };
481
482 data = &(priv->sensitivity_data);
483
484 memset(&cmd, 0, sizeof(cmd));
485
486 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
f0832f13
EG
487
488 /* Update uCode's "work" table, and copy it to DSP */
489 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
490
491 /* Don't send command to uCode if nothing has changed */
492 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
493 sizeof(u16)*HD_TABLE_SIZE)) {
e1623446 494 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
f0832f13
EG
495 return 0;
496 }
497
498 /* Copy table for comparison next time */
499 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
500 sizeof(u16)*HD_TABLE_SIZE);
501
e10a0533 502 return iwl_dvm_send_cmd(priv, &cmd_out);
f0832f13
EG
503}
504
c8312fac
WYG
505/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
506static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
507{
508 struct iwl_enhance_sensitivity_cmd cmd;
509 struct iwl_sensitivity_data *data = NULL;
510 struct iwl_host_cmd cmd_out = {
511 .id = SENSITIVITY_CMD,
3fa50738 512 .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
c8312fac 513 .flags = CMD_ASYNC,
3fa50738 514 .data = { &cmd, },
c8312fac
WYG
515 };
516
517 data = &(priv->sensitivity_data);
518
519 memset(&cmd, 0, sizeof(cmd));
520
521 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
522
0d8877a1 523 if (priv->lib->hd_v2) {
ae7f9a74
WYG
524 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
525 HD_INA_NON_SQUARE_DET_OFDM_DATA_V2;
526 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
527 HD_INA_NON_SQUARE_DET_CCK_DATA_V2;
528 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
529 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V2;
530 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
531 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
532 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
533 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
534 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
535 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V2;
536 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
537 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V2;
538 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
539 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
540 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
541 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
542 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
543 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V2;
544 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
545 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V2;
546 } else {
547 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
548 HD_INA_NON_SQUARE_DET_OFDM_DATA_V1;
549 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
550 HD_INA_NON_SQUARE_DET_CCK_DATA_V1;
551 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
552 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V1;
553 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
554 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
555 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
556 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
557 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
558 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V1;
559 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
560 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V1;
561 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
562 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
563 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
564 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
565 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
566 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V1;
567 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
568 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V1;
569 }
c8312fac
WYG
570
571 /* Update uCode's "work" table, and copy it to DSP */
572 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
573
574 /* Don't send command to uCode if nothing has changed */
575 if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
576 sizeof(u16)*HD_TABLE_SIZE) &&
577 !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
578 &(priv->enhance_sensitivity_tbl[0]),
579 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
580 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
581 return 0;
582 }
583
584 /* Copy table for comparison next time */
585 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
586 sizeof(u16)*HD_TABLE_SIZE);
587 memcpy(&(priv->enhance_sensitivity_tbl[0]),
588 &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
589 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
590
e10a0533 591 return iwl_dvm_send_cmd(priv, &cmd_out);
c8312fac
WYG
592}
593
f0832f13
EG
594void iwl_init_sensitivity(struct iwl_priv *priv)
595{
596 int ret = 0;
597 int i;
598 struct iwl_sensitivity_data *data = NULL;
9e295116 599 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
f0832f13 600
bfb45f54 601 if (priv->calib_disabled & IWL_SENSITIVITY_CALIB_DISABLED)
445c2dff
TW
602 return;
603
e1623446 604 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
f0832f13
EG
605
606 /* Clear driver's sensitivity algo data */
607 data = &(priv->sensitivity_data);
608
609 if (ranges == NULL)
f0832f13
EG
610 return;
611
612 memset(data, 0, sizeof(struct iwl_sensitivity_data));
613
614 data->num_in_cck_no_fa = 0;
615 data->nrg_curr_state = IWL_FA_TOO_MANY;
616 data->nrg_prev_state = IWL_FA_TOO_MANY;
617 data->nrg_silence_ref = 0;
618 data->nrg_silence_idx = 0;
619 data->nrg_energy_idx = 0;
620
621 for (i = 0; i < 10; i++)
622 data->nrg_value[i] = 0;
623
624 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
625 data->nrg_silence_rssi[i] = 0;
626
f3a2a424 627 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
f0832f13
EG
628 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
629 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
630 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
631 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
632 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
633 data->nrg_th_cck = ranges->nrg_th_cck;
634 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
55036d66
WYG
635 data->barker_corr_th_min = ranges->barker_corr_th_min;
636 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
637 data->nrg_th_cca = ranges->nrg_th_cca;
f0832f13
EG
638
639 data->last_bad_plcp_cnt_ofdm = 0;
640 data->last_fa_cnt_ofdm = 0;
641 data->last_bad_plcp_cnt_cck = 0;
642 data->last_fa_cnt_cck = 0;
643
0692fe41 644 if (priv->fw->enhance_sensitivity_table)
c8312fac
WYG
645 ret |= iwl_enhance_sensitivity_write(priv);
646 else
647 ret |= iwl_sensitivity_write(priv);
e1623446 648 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
f0832f13 649}
f0832f13 650
0da0e5bf 651void iwl_sensitivity_calibration(struct iwl_priv *priv)
f0832f13
EG
652{
653 u32 rx_enable_time;
654 u32 fa_cck;
655 u32 fa_ofdm;
656 u32 bad_plcp_cck;
657 u32 bad_plcp_ofdm;
658 u32 norm_fa_ofdm;
659 u32 norm_fa_cck;
660 struct iwl_sensitivity_data *data = NULL;
7980fba5
WYG
661 struct statistics_rx_non_phy *rx_info;
662 struct statistics_rx_phy *ofdm, *cck;
f0832f13
EG
663 struct statistics_general_data statis;
664
bfb45f54 665 if (priv->calib_disabled & IWL_SENSITIVITY_CALIB_DISABLED)
445c2dff
TW
666 return;
667
f0832f13
EG
668 data = &(priv->sensitivity_data);
669
246ed355 670 if (!iwl_is_any_associated(priv)) {
e1623446 671 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
f0832f13
EG
672 return;
673 }
674
4ff70fcd 675 spin_lock_bh(&priv->statistics.lock);
0da0e5bf
JB
676 rx_info = &priv->statistics.rx_non_phy;
677 ofdm = &priv->statistics.rx_ofdm;
678 cck = &priv->statistics.rx_cck;
f0832f13 679 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
e1623446 680 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
4ff70fcd 681 spin_unlock_bh(&priv->statistics.lock);
f0832f13
EG
682 return;
683 }
684
685 /* Extract Statistics: */
686 rx_enable_time = le32_to_cpu(rx_info->channel_load);
7980fba5
WYG
687 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
688 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
689 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
690 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
f0832f13
EG
691
692 statis.beacon_silence_rssi_a =
7980fba5 693 le32_to_cpu(rx_info->beacon_silence_rssi_a);
f0832f13 694 statis.beacon_silence_rssi_b =
7980fba5 695 le32_to_cpu(rx_info->beacon_silence_rssi_b);
f0832f13 696 statis.beacon_silence_rssi_c =
7980fba5 697 le32_to_cpu(rx_info->beacon_silence_rssi_c);
f0832f13 698 statis.beacon_energy_a =
7980fba5 699 le32_to_cpu(rx_info->beacon_energy_a);
f0832f13 700 statis.beacon_energy_b =
7980fba5 701 le32_to_cpu(rx_info->beacon_energy_b);
f0832f13 702 statis.beacon_energy_c =
7980fba5 703 le32_to_cpu(rx_info->beacon_energy_c);
f0832f13 704
4ff70fcd 705 spin_unlock_bh(&priv->statistics.lock);
f0832f13 706
e1623446 707 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
f0832f13
EG
708
709 if (!rx_enable_time) {
91dd6c27 710 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
f0832f13
EG
711 return;
712 }
713
714 /* These statistics increase monotonically, and do not reset
715 * at each beacon. Calculate difference from last value, or just
716 * use the new statistics value if it has reset or wrapped around. */
717 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
718 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
719 else {
720 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
721 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
722 }
723
724 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
725 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
726 else {
727 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
728 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
729 }
730
731 if (data->last_fa_cnt_ofdm > fa_ofdm)
732 data->last_fa_cnt_ofdm = fa_ofdm;
733 else {
734 fa_ofdm -= data->last_fa_cnt_ofdm;
735 data->last_fa_cnt_ofdm += fa_ofdm;
736 }
737
738 if (data->last_fa_cnt_cck > fa_cck)
739 data->last_fa_cnt_cck = fa_cck;
740 else {
741 fa_cck -= data->last_fa_cnt_cck;
742 data->last_fa_cnt_cck += fa_cck;
743 }
744
745 /* Total aborted signal locks */
746 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
747 norm_fa_cck = fa_cck + bad_plcp_cck;
748
e1623446 749 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
f0832f13
EG
750 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
751
752 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
753 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
0692fe41 754 if (priv->fw->enhance_sensitivity_table)
c8312fac
WYG
755 iwl_enhance_sensitivity_write(priv);
756 else
757 iwl_sensitivity_write(priv);
f0832f13 758}
f0832f13 759
d8c07e7a
WYG
760static inline u8 find_first_chain(u8 mask)
761{
762 if (mask & ANT_A)
763 return CHAIN_A;
764 if (mask & ANT_B)
765 return CHAIN_B;
766 return CHAIN_C;
767}
768
3031242b
SZ
769/**
770 * Run disconnected antenna algorithm to find out which antennas are
771 * disconnected.
772 */
773static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
774 struct iwl_chain_noise_data *data)
775{
776 u32 active_chains = 0;
777 u32 max_average_sig;
778 u16 max_average_sig_antenna_i;
779 u8 num_tx_chains;
780 u8 first_chain;
781 u16 i = 0;
782
b8c2b05e
FD
783 average_sig[0] = data->chain_signal_a / IWL_CAL_NUM_BEACONS;
784 average_sig[1] = data->chain_signal_b / IWL_CAL_NUM_BEACONS;
785 average_sig[2] = data->chain_signal_c / IWL_CAL_NUM_BEACONS;
3031242b
SZ
786
787 if (average_sig[0] >= average_sig[1]) {
788 max_average_sig = average_sig[0];
789 max_average_sig_antenna_i = 0;
790 active_chains = (1 << max_average_sig_antenna_i);
791 } else {
792 max_average_sig = average_sig[1];
793 max_average_sig_antenna_i = 1;
794 active_chains = (1 << max_average_sig_antenna_i);
795 }
796
797 if (average_sig[2] >= max_average_sig) {
798 max_average_sig = average_sig[2];
799 max_average_sig_antenna_i = 2;
800 active_chains = (1 << max_average_sig_antenna_i);
801 }
802
803 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
804 average_sig[0], average_sig[1], average_sig[2]);
805 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
806 max_average_sig, max_average_sig_antenna_i);
807
808 /* Compare signal strengths for all 3 receivers. */
809 for (i = 0; i < NUM_RX_CHAINS; i++) {
810 if (i != max_average_sig_antenna_i) {
811 s32 rssi_delta = (max_average_sig - average_sig[i]);
812
813 /* If signal is very weak, compared with
814 * strongest, mark it as disconnected. */
815 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
816 data->disconn_array[i] = 1;
817 else
818 active_chains |= (1 << i);
819 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
820 "disconn_array[i] = %d\n",
821 i, rssi_delta, data->disconn_array[i]);
822 }
823 }
824
825 /*
826 * The above algorithm sometimes fails when the ucode
827 * reports 0 for all chains. It's not clear why that
828 * happens to start with, but it is then causing trouble
829 * because this can make us enable more chains than the
830 * hardware really has.
831 *
832 * To be safe, simply mask out any chains that we know
833 * are not on the device.
834 */
b7998c8b 835 active_chains &= priv->nvm_data->valid_rx_ant;
3031242b
SZ
836
837 num_tx_chains = 0;
838 for (i = 0; i < NUM_RX_CHAINS; i++) {
839 /* loops on all the bits of
840 * priv->hw_setting.valid_tx_ant */
841 u8 ant_msk = (1 << i);
b7998c8b 842 if (!(priv->nvm_data->valid_tx_ant & ant_msk))
3031242b
SZ
843 continue;
844
845 num_tx_chains++;
846 if (data->disconn_array[i] == 0)
847 /* there is a Tx antenna connected */
848 break;
9e295116 849 if (num_tx_chains == priv->hw_params.tx_chains_num &&
3031242b
SZ
850 data->disconn_array[i]) {
851 /*
852 * If all chains are disconnected
853 * connect the first valid tx chain
854 */
855 first_chain =
b7998c8b 856 find_first_chain(priv->nvm_data->valid_tx_ant);
3031242b
SZ
857 data->disconn_array[first_chain] = 0;
858 active_chains |= BIT(first_chain);
85ee7a1d
JP
859 IWL_DEBUG_CALIB(priv,
860 "All Tx chains are disconnected W/A - declare %d as connected\n",
3031242b
SZ
861 first_chain);
862 break;
863 }
864 }
865
b7998c8b 866 if (active_chains != priv->nvm_data->valid_rx_ant &&
3031242b
SZ
867 active_chains != priv->chain_noise_data.active_chains)
868 IWL_DEBUG_CALIB(priv,
869 "Detected that not all antennas are connected! "
870 "Connected: %#x, valid: %#x.\n",
d6189124 871 active_chains,
b7998c8b 872 priv->nvm_data->valid_rx_ant);
3031242b
SZ
873
874 /* Save for use within RXON, TX, SCAN commands, etc. */
875 data->active_chains = active_chains;
876 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
877 active_chains);
878}
879
e505c433 880static void iwlagn_gain_computation(struct iwl_priv *priv,
fdeff465
JB
881 u32 average_noise[NUM_RX_CHAINS],
882 u8 default_chain)
e505c433
WYG
883{
884 int i;
885 s32 delta_g;
886 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
887
888 /*
889 * Find Gain Code for the chains based on "default chain"
890 */
891 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
892 if ((data->disconn_array[i])) {
893 data->delta_gain_code[i] = 0;
894 continue;
895 }
896
0d8877a1 897 delta_g = (priv->lib->chain_noise_scale *
e505c433
WYG
898 ((s32)average_noise[default_chain] -
899 (s32)average_noise[i])) / 1500;
900
901 /* bound gain by 2 bits value max, 3rd bit is sign */
902 data->delta_gain_code[i] =
83ec9a85 903 min(abs(delta_g), CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
e505c433
WYG
904
905 if (delta_g < 0)
906 /*
907 * set negative sign ...
908 * note to Intel developers: This is uCode API format,
909 * not the format of any internal device registers.
910 * Do not change this format for e.g. 6050 or similar
911 * devices. Change format only if more resolution
912 * (i.e. more than 2 bits magnitude) is needed.
913 */
914 data->delta_gain_code[i] |= (1 << 2);
915 }
916
917 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
918 data->delta_gain_code[1], data->delta_gain_code[2]);
919
920 if (!data->radio_write) {
921 struct iwl_calib_chain_noise_gain_cmd cmd;
922
923 memset(&cmd, 0, sizeof(cmd));
924
925 iwl_set_calib_hdr(&cmd.hdr,
898ed67b 926 priv->phy_calib_chain_noise_gain_cmd);
e505c433
WYG
927 cmd.delta_gain_1 = data->delta_gain_code[1];
928 cmd.delta_gain_2 = data->delta_gain_code[2];
e10a0533 929 iwl_dvm_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
e505c433
WYG
930 CMD_ASYNC, sizeof(cmd), &cmd);
931
932 data->radio_write = 1;
933 data->state = IWL_CHAIN_NOISE_CALIBRATED;
934 }
935}
3031242b 936
f0832f13 937/*
3031242b 938 * Accumulate 16 beacons of signal and noise statistics for each of
f0832f13
EG
939 * 3 receivers/antennas/rx-chains, then figure out:
940 * 1) Which antennas are connected.
941 * 2) Differential rx gain settings to balance the 3 receivers.
942 */
0da0e5bf 943void iwl_chain_noise_calibration(struct iwl_priv *priv)
f0832f13
EG
944{
945 struct iwl_chain_noise_data *data = NULL;
946
947 u32 chain_noise_a;
948 u32 chain_noise_b;
949 u32 chain_noise_c;
950 u32 chain_sig_a;
951 u32 chain_sig_b;
952 u32 chain_sig_c;
953 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
954 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
f0832f13
EG
955 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
956 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
957 u16 i = 0;
958 u16 rxon_chnum = INITIALIZATION_VALUE;
959 u16 stat_chnum = INITIALIZATION_VALUE;
960 u8 rxon_band24;
961 u8 stat_band24;
7980fba5 962 struct statistics_rx_non_phy *rx_info;
3031242b 963
246ed355
JB
964 /*
965 * MULTI-FIXME:
966 * When we support multiple interfaces on different channels,
967 * this must be modified/fixed.
968 */
969 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
f0832f13 970
bfb45f54 971 if (priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED)
445c2dff
TW
972 return;
973
f0832f13
EG
974 data = &(priv->chain_noise_data);
975
d8c07e7a
WYG
976 /*
977 * Accumulate just the first "chain_noise_num_beacons" after
978 * the first association, then we're done forever.
979 */
f0832f13
EG
980 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
981 if (data->state == IWL_CHAIN_NOISE_ALIVE)
e1623446 982 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
f0832f13
EG
983 return;
984 }
985
4ff70fcd 986 spin_lock_bh(&priv->statistics.lock);
0da0e5bf
JB
987
988 rx_info = &priv->statistics.rx_non_phy;
989
f0832f13 990 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
e1623446 991 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
4ff70fcd 992 spin_unlock_bh(&priv->statistics.lock);
f0832f13
EG
993 return;
994 }
995
246ed355
JB
996 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
997 rxon_chnum = le16_to_cpu(ctx->staging.channel);
0da0e5bf
JB
998 stat_band24 =
999 !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
1000 stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
f0832f13
EG
1001
1002 /* Make sure we accumulate data for just the associated channel
1003 * (even if scanning). */
1004 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
e1623446 1005 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
f0832f13 1006 rxon_chnum, rxon_band24);
4ff70fcd 1007 spin_unlock_bh(&priv->statistics.lock);
f0832f13
EG
1008 return;
1009 }
1010
d8c07e7a
WYG
1011 /*
1012 * Accumulate beacon statistics values across
1013 * "chain_noise_num_beacons"
1014 */
f0832f13
EG
1015 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
1016 IN_BAND_FILTER;
1017 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
1018 IN_BAND_FILTER;
1019 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
1020 IN_BAND_FILTER;
1021
1022 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
1023 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
1024 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
1025
4ff70fcd 1026 spin_unlock_bh(&priv->statistics.lock);
f0832f13
EG
1027
1028 data->beacon_count++;
1029
1030 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
1031 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
1032 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
1033
1034 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
1035 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
1036 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
1037
e1623446 1038 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
f0832f13 1039 rxon_chnum, rxon_band24, data->beacon_count);
e1623446 1040 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
f0832f13 1041 chain_sig_a, chain_sig_b, chain_sig_c);
e1623446 1042 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
f0832f13
EG
1043 chain_noise_a, chain_noise_b, chain_noise_c);
1044
d8c07e7a 1045 /* If this is the "chain_noise_num_beacons", determine:
f0832f13
EG
1046 * 1) Disconnected antennas (using signal strengths)
1047 * 2) Differential gain (using silence noise) to balance receivers */
b8c2b05e 1048 if (data->beacon_count != IWL_CAL_NUM_BEACONS)
f0832f13
EG
1049 return;
1050
1051 /* Analyze signal for disconnected antenna */
0d8877a1
JB
1052 if (priv->lib->bt_params &&
1053 priv->lib->bt_params->advanced_bt_coexist) {
6fe8efb2
SZ
1054 /* Disable disconnected antenna algorithm for advanced
1055 bt coex, assuming valid antennas are connected */
b7998c8b 1056 data->active_chains = priv->nvm_data->valid_rx_ant;
6fe8efb2
SZ
1057 for (i = 0; i < NUM_RX_CHAINS; i++)
1058 if (!(data->active_chains & (1<<i)))
1059 data->disconn_array[i] = 1;
1060 } else
1061 iwl_find_disconn_antenna(priv, average_sig, data);
f0832f13 1062
f0832f13 1063 /* Analyze noise for rx balance */
b8c2b05e
FD
1064 average_noise[0] = data->chain_noise_a / IWL_CAL_NUM_BEACONS;
1065 average_noise[1] = data->chain_noise_b / IWL_CAL_NUM_BEACONS;
1066 average_noise[2] = data->chain_noise_c / IWL_CAL_NUM_BEACONS;
f0832f13
EG
1067
1068 for (i = 0; i < NUM_RX_CHAINS; i++) {
1069 if (!(data->disconn_array[i]) &&
1070 (average_noise[i] <= min_average_noise)) {
1071 /* This means that chain i is active and has
1072 * lower noise values so far: */
1073 min_average_noise = average_noise[i];
1074 min_average_noise_antenna_i = i;
1075 }
1076 }
1077
e1623446 1078 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
f0832f13
EG
1079 average_noise[0], average_noise[1],
1080 average_noise[2]);
1081
e1623446 1082 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
f0832f13
EG
1083 min_average_noise, min_average_noise_antenna_i);
1084
26a7ca9a
JB
1085 iwlagn_gain_computation(
1086 priv, average_noise,
b7998c8b 1087 find_first_chain(priv->nvm_data->valid_rx_ant));
04816448
GE
1088
1089 /* Some power changes may have been made during the calibration.
1090 * Update and commit the RXON
1091 */
6b6db91c 1092 iwl_update_chain_flags(priv);
04816448
GE
1093
1094 data->state = IWL_CHAIN_NOISE_DONE;
e312c24c 1095 iwl_power_update_mode(priv, false);
f0832f13 1096}
4a4a9e81
TW
1097
1098void iwl_reset_run_time_calib(struct iwl_priv *priv)
1099{
1100 int i;
1101 memset(&(priv->sensitivity_data), 0,
1102 sizeof(struct iwl_sensitivity_data));
1103 memset(&(priv->chain_noise_data), 0,
1104 sizeof(struct iwl_chain_noise_data));
1105 for (i = 0; i < NUM_RX_CHAINS; i++)
1106 priv->chain_noise_data.delta_gain_code[i] =
1107 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
1108
1109 /* Ask for statistics now, the uCode will send notification
1110 * periodically after association */
ef8d5529 1111 iwl_send_statistics_request(priv, CMD_ASYNC, true);
4a4a9e81 1112}
This page took 0.981057 seconds and 5 git commands to generate.