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