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