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