iwlwifi: HW dependent run time calibration
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-4965.c
index 0171bb8..be4cc5f 100644 (file)
 #include <asm/unaligned.h>
 
 #include "iwl-eeprom.h"
-#include "iwl-core.h"
 #include "iwl-4965.h"
+#include "iwl-core.h"
 #include "iwl-io.h"
 #include "iwl-helpers.h"
+#include "iwl-calib.h"
 
 /* module parameters */
 static struct iwl_mod_params iwl4965_mod_params = {
-       .num_of_queues = IWL_MAX_NUM_QUEUES,
+       .num_of_queues = IWL4965_MAX_NUM_QUEUES,
        .enable_qos = 1,
        .amsdu_size_8K = 1,
        /* the rest are 0 by default */
@@ -114,6 +115,151 @@ static const u16 default_tid_to_tx_fifo[] = {
 
 #endif /*CONFIG_IWL4965_HT */
 
+/* check contents of special bootstrap uCode SRAM */
+static int iwl4965_verify_bsm(struct iwl_priv *priv)
+{
+       __le32 *image = priv->ucode_boot.v_addr;
+       u32 len = priv->ucode_boot.len;
+       u32 reg;
+       u32 val;
+
+       IWL_DEBUG_INFO("Begin verify bsm\n");
+
+       /* verify BSM SRAM contents */
+       val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
+       for (reg = BSM_SRAM_LOWER_BOUND;
+            reg < BSM_SRAM_LOWER_BOUND + len;
+            reg += sizeof(u32), image++) {
+               val = iwl_read_prph(priv, reg);
+               if (val != le32_to_cpu(*image)) {
+                       IWL_ERROR("BSM uCode verification failed at "
+                                 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
+                                 BSM_SRAM_LOWER_BOUND,
+                                 reg - BSM_SRAM_LOWER_BOUND, len,
+                                 val, le32_to_cpu(*image));
+                       return -EIO;
+               }
+       }
+
+       IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
+
+       return 0;
+}
+
+/**
+ * iwl4965_load_bsm - Load bootstrap instructions
+ *
+ * BSM operation:
+ *
+ * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
+ * in special SRAM that does not power down during RFKILL.  When powering back
+ * up after power-saving sleeps (or during initial uCode load), the BSM loads
+ * the bootstrap program into the on-board processor, and starts it.
+ *
+ * The bootstrap program loads (via DMA) instructions and data for a new
+ * program from host DRAM locations indicated by the host driver in the
+ * BSM_DRAM_* registers.  Once the new program is loaded, it starts
+ * automatically.
+ *
+ * When initializing the NIC, the host driver points the BSM to the
+ * "initialize" uCode image.  This uCode sets up some internal data, then
+ * notifies host via "initialize alive" that it is complete.
+ *
+ * The host then replaces the BSM_DRAM_* pointer values to point to the
+ * normal runtime uCode instructions and a backup uCode data cache buffer
+ * (filled initially with starting data values for the on-board processor),
+ * then triggers the "initialize" uCode to load and launch the runtime uCode,
+ * which begins normal operation.
+ *
+ * When doing a power-save shutdown, runtime uCode saves data SRAM into
+ * the backup data cache in DRAM before SRAM is powered down.
+ *
+ * When powering back up, the BSM loads the bootstrap program.  This reloads
+ * the runtime uCode instructions and the backup data cache into SRAM,
+ * and re-launches the runtime uCode from where it left off.
+ */
+static int iwl4965_load_bsm(struct iwl_priv *priv)
+{
+       __le32 *image = priv->ucode_boot.v_addr;
+       u32 len = priv->ucode_boot.len;
+       dma_addr_t pinst;
+       dma_addr_t pdata;
+       u32 inst_len;
+       u32 data_len;
+       int i;
+       u32 done;
+       u32 reg_offset;
+       int ret;
+
+       IWL_DEBUG_INFO("Begin load bsm\n");
+
+       /* make sure bootstrap program is no larger than BSM's SRAM size */
+       if (len > IWL_MAX_BSM_SIZE)
+               return -EINVAL;
+
+       /* Tell bootstrap uCode where to find the "Initialize" uCode
+        *   in host DRAM ... host DRAM physical address bits 35:4 for 4965.
+        * NOTE:  iwl4965_initialize_alive_start() will replace these values,
+        *        after the "initialize" uCode has run, to point to
+        *        runtime/protocol instructions and backup data cache. */
+       pinst = priv->ucode_init.p_addr >> 4;
+       pdata = priv->ucode_init_data.p_addr >> 4;
+       inst_len = priv->ucode_init.len;
+       data_len = priv->ucode_init_data.len;
+
+       ret = iwl_grab_nic_access(priv);
+       if (ret)
+               return ret;
+
+       iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
+       iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
+       iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
+       iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
+
+       /* Fill BSM memory with bootstrap instructions */
+       for (reg_offset = BSM_SRAM_LOWER_BOUND;
+            reg_offset < BSM_SRAM_LOWER_BOUND + len;
+            reg_offset += sizeof(u32), image++)
+               _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
+
+       ret = iwl4965_verify_bsm(priv);
+       if (ret) {
+               iwl_release_nic_access(priv);
+               return ret;
+       }
+
+       /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
+       iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
+       iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
+       iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
+
+       /* Load bootstrap code into instruction SRAM now,
+        *   to prepare to load "initialize" uCode */
+       iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
+
+       /* Wait for load of bootstrap uCode to finish */
+       for (i = 0; i < 100; i++) {
+               done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
+               if (!(done & BSM_WR_CTRL_REG_BIT_START))
+                       break;
+               udelay(10);
+       }
+       if (i < 100)
+               IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
+       else {
+               IWL_ERROR("BSM write did not complete!\n");
+               return -EIO;
+       }
+
+       /* Enable future boot loads whenever power management unit triggers it
+        *   (e.g. when powering back up after power-save shutdown) */
+       iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
+
+       iwl_release_nic_access(priv);
+
+       return 0;
+}
+
 static int iwl4965_init_drv(struct iwl_priv *priv)
 {
        int ret;
@@ -129,6 +275,18 @@ static int iwl4965_init_drv(struct iwl_priv *priv)
        spin_lock_init(&priv->hcmd_lock);
        spin_lock_init(&priv->lq_mngr.lock);
 
+       priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
+                                       sizeof(struct iwl4965_shared),
+                                       &priv->shared_phys);
+
+       if (!priv->shared_virt) {
+               ret = -ENOMEM;
+               goto err;
+       }
+
+       memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
+
+
        for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
                INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
 
@@ -253,7 +411,7 @@ void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
        int rate_index;
 
        control->antenna_sel_tx =
-               ((rate_n_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_A_POS);
+               ((rate_n_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS);
        if (rate_n_flags & RATE_MCS_HT_MSK)
                control->flags |= IEEE80211_TXCTL_OFDM_HT;
        if (rate_n_flags & RATE_MCS_GF_MSK)
@@ -347,10 +505,10 @@ u8 iwl4965_hw_find_station(struct iwl_priv *priv, const u8 *addr)
                start = IWL_STA_ID;
 
        if (is_broadcast_ether_addr(addr))
-               return priv->hw_setting.bcast_sta_id;
+               return priv->hw_params.bcast_sta_id;
 
        spin_lock_irqsave(&priv->sta_lock, flags);
-       for (i = start; i < priv->hw_setting.max_stations; i++)
+       for (i = start; i < priv->hw_params.max_stations; i++)
                if ((priv->stations[i].used) &&
                    (!compare_ether_addr
                     (priv->stations[i].sta.sta.addr, addr))) {
@@ -401,15 +559,15 @@ static int iwl4965_nic_set_pwr_src(struct iwl_priv *priv, int pwr_max)
 
 static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
 {
-       int rc;
+       int ret;
        unsigned long flags;
        unsigned int rb_size;
 
        spin_lock_irqsave(&priv->lock, flags);
-       rc = iwl_grab_nic_access(priv);
-       if (rc) {
+       ret = iwl_grab_nic_access(priv);
+       if (ret) {
                spin_unlock_irqrestore(&priv->lock, flags);
-               return rc;
+               return ret;
        }
 
        if (priv->cfg->mod_params->amsdu_size_8K)
@@ -429,15 +587,15 @@ static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
 
        /* Tell device where in DRAM to update its Rx status */
        iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
-                          (priv->hw_setting.shared_phys +
-                               offsetof(struct iwl4965_shared, val0)) >> 4);
+                          (priv->shared_phys +
+                           offsetof(struct iwl4965_shared, rb_closed)) >> 4);
 
        /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
        iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
                           FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
                           FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
                           rb_size |
-                            /*0x10 << 4 | */
+                            /* 0x10 << 4 | */
                           (RX_QUEUE_SIZE_LOG <<
                              FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
 
@@ -532,7 +690,7 @@ static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
        }
 
        /* Turn off all Tx DMA channels */
-       iwl_write_prph(priv, KDR_SCD_TXFACT, 0);
+       iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
        iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
@@ -545,7 +703,7 @@ static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
 
        /* Alloc and init all (default 16) Tx queues,
         * including the command queue (#4) */
-       for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
+       for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
                slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
                                        TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
                rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
@@ -751,7 +909,7 @@ void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
        unsigned long flags;
 
        /* Stop each Tx DMA channel, and wait for it to be idle */
-       for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
+       for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
                spin_lock_irqsave(&priv->lock, flags);
                if (iwl_grab_nic_access(priv)) {
                        spin_unlock_irqrestore(&priv->lock, flags);
@@ -819,40 +977,21 @@ int iwl4965_hw_nic_reset(struct iwl_priv *priv)
 /**
  * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
  *
- * This callback is provided in order to queue the statistics_work
- * in work_queue context (v. softirq)
+ * This callback is provided in order to send a statistics request.
  *
  * This timer function is continually reset to execute within
  * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  * was received.  We need to ensure we receive the statistics in order
- * to update the temperature used for calibrating the TXPOWER.  However,
- * we can't send the statistics command from softirq context (which
- * is the context which timers run at) so we have to queue off the
- * statistics_work to actually send the command to the hardware.
+ * to update the temperature used for calibrating the TXPOWER.
  */
 static void iwl4965_bg_statistics_periodic(unsigned long data)
 {
        struct iwl_priv *priv = (struct iwl_priv *)data;
 
-       queue_work(priv->workqueue, &priv->statistics_work);
-}
-
-/**
- * iwl4965_bg_statistics_work - Send the statistics request to the hardware.
- *
- * This is queued by iwl4965_bg_statistics_periodic.
- */
-static void iwl4965_bg_statistics_work(struct work_struct *work)
-{
-       struct iwl_priv *priv = container_of(work, struct iwl_priv,
-                                            statistics_work);
-
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
                return;
 
-       mutex_lock(&priv->mutex);
-       iwl4965_send_statistics_request(priv);
-       mutex_unlock(&priv->mutex);
+       iwl_send_statistics_request(priv, CMD_ASYNC);
 }
 
 #define CT_LIMIT_CONST         259
@@ -894,418 +1033,15 @@ void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
                IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded\n");
 }
 
-#ifdef CONFIG_IWL4965_SENSITIVITY
-
-/* "false alarms" are signals that our DSP tries to lock onto,
- *   but then determines that they are either noise, or transmissions
- *   from a distant wireless network (also "noise", really) that get
- *   "stepped on" by stronger transmissions within our own network.
- * This algorithm attempts to set a sensitivity level that is high
- *   enough to receive all of our own network traffic, but not so
- *   high that our DSP gets too busy trying to lock onto non-network
- *   activity/noise. */
-static int iwl4965_sens_energy_cck(struct iwl_priv *priv,
-                                  u32 norm_fa,
-                                  u32 rx_enable_time,
-                                  struct statistics_general_data *rx_info)
-{
-       u32 max_nrg_cck = 0;
-       int i = 0;
-       u8 max_silence_rssi = 0;
-       u32 silence_ref = 0;
-       u8 silence_rssi_a = 0;
-       u8 silence_rssi_b = 0;
-       u8 silence_rssi_c = 0;
-       u32 val;
-
-       /* "false_alarms" values below are cross-multiplications to assess the
-        *   numbers of false alarms within the measured period of actual Rx
-        *   (Rx is off when we're txing), vs the min/max expected false alarms
-        *   (some should be expected if rx is sensitive enough) in a
-        *   hypothetical listening period of 200 time units (TU), 204.8 msec:
-        *
-        * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
-        *
-        * */
-       u32 false_alarms = norm_fa * 200 * 1024;
-       u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
-       u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
-       struct iwl4965_sensitivity_data *data = NULL;
-
-       data = &(priv->sensitivity_data);
-
-       data->nrg_auto_corr_silence_diff = 0;
-
-       /* Find max silence rssi among all 3 receivers.
-        * This is background noise, which may include transmissions from other
-        *    networks, measured during silence before our network's beacon */
-       silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
-                           ALL_BAND_FILTER) >> 8);
-       silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
-                           ALL_BAND_FILTER) >> 8);
-       silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
-                           ALL_BAND_FILTER) >> 8);
-
-       val = max(silence_rssi_b, silence_rssi_c);
-       max_silence_rssi = max(silence_rssi_a, (u8) val);
-
-       /* Store silence rssi in 20-beacon history table */
-       data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
-       data->nrg_silence_idx++;
-       if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
-               data->nrg_silence_idx = 0;
-
-       /* Find max silence rssi across 20 beacon history */
-       for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
-               val = data->nrg_silence_rssi[i];
-               silence_ref = max(silence_ref, val);
-       }
-       IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
-                       silence_rssi_a, silence_rssi_b, silence_rssi_c,
-                       silence_ref);
-
-       /* Find max rx energy (min value!) among all 3 receivers,
-        *   measured during beacon frame.
-        * Save it in 10-beacon history table. */
-       i = data->nrg_energy_idx;
-       val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
-       data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
-
-       data->nrg_energy_idx++;
-       if (data->nrg_energy_idx >= 10)
-               data->nrg_energy_idx = 0;
-
-       /* Find min rx energy (max value) across 10 beacon history.
-        * This is the minimum signal level that we want to receive well.
-        * Add backoff (margin so we don't miss slightly lower energy frames).
-        * This establishes an upper bound (min value) for energy threshold. */
-       max_nrg_cck = data->nrg_value[0];
-       for (i = 1; i < 10; i++)
-               max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
-       max_nrg_cck += 6;
-
-       IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
-                       rx_info->beacon_energy_a, rx_info->beacon_energy_b,
-                       rx_info->beacon_energy_c, max_nrg_cck - 6);
-
-       /* Count number of consecutive beacons with fewer-than-desired
-        *   false alarms. */
-       if (false_alarms < min_false_alarms)
-               data->num_in_cck_no_fa++;
-       else
-               data->num_in_cck_no_fa = 0;
-       IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
-                       data->num_in_cck_no_fa);
-
-       /* If we got too many false alarms this time, reduce sensitivity */
-       if (false_alarms > max_false_alarms) {
-               IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
-                            false_alarms, max_false_alarms);
-               IWL_DEBUG_CALIB("... reducing sensitivity\n");
-               data->nrg_curr_state = IWL_FA_TOO_MANY;
-
-               if (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK) {
-                       /* Store for "fewer than desired" on later beacon */
-                       data->nrg_silence_ref = silence_ref;
-
-                       /* increase energy threshold (reduce nrg value)
-                        *   to decrease sensitivity */
-                       if (data->nrg_th_cck > (NRG_MAX_CCK + NRG_STEP_CCK))
-                               data->nrg_th_cck = data->nrg_th_cck
-                                                        - NRG_STEP_CCK;
-               }
-
-               /* increase auto_corr values to decrease sensitivity */
-               if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
-                       data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
-               else {
-                       val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
-                       data->auto_corr_cck = min((u32)AUTO_CORR_MAX_CCK, val);
-               }
-               val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
-               data->auto_corr_cck_mrc = min((u32)AUTO_CORR_MAX_CCK_MRC, val);
-
-       /* Else if we got fewer than desired, increase sensitivity */
-       } else if (false_alarms < min_false_alarms) {
-               data->nrg_curr_state = IWL_FA_TOO_FEW;
-
-               /* Compare silence level with silence level for most recent
-                *   healthy number or too many false alarms */
-               data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
-                                                  (s32)silence_ref;
-
-               IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
-                        false_alarms, min_false_alarms,
-                        data->nrg_auto_corr_silence_diff);
-
-               /* Increase value to increase sensitivity, but only if:
-                * 1a) previous beacon did *not* have *too many* false alarms
-                * 1b) AND there's a significant difference in Rx levels
-                *      from a previous beacon with too many, or healthy # FAs
-                * OR 2) We've seen a lot of beacons (100) with too few
-                *       false alarms */
-               if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
-                       ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
-                       (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
-
-                       IWL_DEBUG_CALIB("... increasing sensitivity\n");
-                       /* Increase nrg value to increase sensitivity */
-                       val = data->nrg_th_cck + NRG_STEP_CCK;
-                       data->nrg_th_cck = min((u32)NRG_MIN_CCK, val);
-
-                       /* Decrease auto_corr values to increase sensitivity */
-                       val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
-                       data->auto_corr_cck = max((u32)AUTO_CORR_MIN_CCK, val);
-
-                       val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
-                       data->auto_corr_cck_mrc =
-                                        max((u32)AUTO_CORR_MIN_CCK_MRC, val);
-
-               } else
-                       IWL_DEBUG_CALIB("... but not changing sensitivity\n");
-
-       /* Else we got a healthy number of false alarms, keep status quo */
-       } else {
-               IWL_DEBUG_CALIB(" FA in safe zone\n");
-               data->nrg_curr_state = IWL_FA_GOOD_RANGE;
-
-               /* Store for use in "fewer than desired" with later beacon */
-               data->nrg_silence_ref = silence_ref;
-
-               /* If previous beacon had too many false alarms,
-                *   give it some extra margin by reducing sensitivity again
-                *   (but don't go below measured energy of desired Rx) */
-               if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
-                       IWL_DEBUG_CALIB("... increasing margin\n");
-                       data->nrg_th_cck -= NRG_MARGIN;
-               }
-       }
-
-       /* Make sure the energy threshold does not go above the measured
-        * energy of the desired Rx signals (reduced by backoff margin),
-        * or else we might start missing Rx frames.
-        * Lower value is higher energy, so we use max()!
-        */
-       data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
-       IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
-
-       data->nrg_prev_state = data->nrg_curr_state;
-
-       return 0;
-}
-
-
-static int iwl4965_sens_auto_corr_ofdm(struct iwl_priv *priv,
-                                      u32 norm_fa,
-                                      u32 rx_enable_time)
-{
-       u32 val;
-       u32 false_alarms = norm_fa * 200 * 1024;
-       u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
-       u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
-       struct iwl4965_sensitivity_data *data = NULL;
-
-       data = &(priv->sensitivity_data);
-
-       /* If we got too many false alarms this time, reduce sensitivity */
-       if (false_alarms > max_false_alarms) {
-
-               IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
-                            false_alarms, max_false_alarms);
-
-               val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm =
-                               min((u32)AUTO_CORR_MAX_OFDM, val);
-
-               val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_mrc =
-                               min((u32)AUTO_CORR_MAX_OFDM_MRC, val);
-
-               val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_x1 =
-                               min((u32)AUTO_CORR_MAX_OFDM_X1, val);
-
-               val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_mrc_x1 =
-                               min((u32)AUTO_CORR_MAX_OFDM_MRC_X1, val);
-       }
-
-       /* Else if we got fewer than desired, increase sensitivity */
-       else if (false_alarms < min_false_alarms) {
-
-               IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
-                            false_alarms, min_false_alarms);
-
-               val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm =
-                               max((u32)AUTO_CORR_MIN_OFDM, val);
-
-               val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_mrc =
-                               max((u32)AUTO_CORR_MIN_OFDM_MRC, val);
-
-               val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_x1 =
-                               max((u32)AUTO_CORR_MIN_OFDM_X1, val);
-
-               val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
-               data->auto_corr_ofdm_mrc_x1 =
-                               max((u32)AUTO_CORR_MIN_OFDM_MRC_X1, val);
-       }
-
-       else
-               IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
-                        min_false_alarms, false_alarms, max_false_alarms);
-
-       return 0;
-}
-
-static int iwl4965_sensitivity_callback(struct iwl_priv *priv,
-                                   struct iwl_cmd *cmd, struct sk_buff *skb)
-{
-       /* We didn't cache the SKB; let the caller free it */
-       return 1;
-}
-
-/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
-static int iwl4965_sensitivity_write(struct iwl_priv *priv, u8 flags)
-{
-       struct iwl4965_sensitivity_cmd cmd ;
-       struct iwl4965_sensitivity_data *data = NULL;
-       struct iwl_host_cmd cmd_out = {
-               .id = SENSITIVITY_CMD,
-               .len = sizeof(struct iwl4965_sensitivity_cmd),
-               .meta.flags = flags,
-               .data = &cmd,
-       };
-       int ret;
-
-       data = &(priv->sensitivity_data);
-
-       memset(&cmd, 0, sizeof(cmd));
-
-       cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_ofdm);
-       cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
-       cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_ofdm_x1);
-       cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
-
-       cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_cck);
-       cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
-                               cpu_to_le16((u16)data->auto_corr_cck_mrc);
-
-       cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
-                               cpu_to_le16((u16)data->nrg_th_cck);
-       cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
-                               cpu_to_le16((u16)data->nrg_th_ofdm);
-
-       cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
-                               __constant_cpu_to_le16(190);
-       cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
-                               __constant_cpu_to_le16(390);
-       cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
-                               __constant_cpu_to_le16(62);
-
-       IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
-                       data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
-                       data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
-                       data->nrg_th_ofdm);
-
-       IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
-                       data->auto_corr_cck, data->auto_corr_cck_mrc,
-                       data->nrg_th_cck);
-
-       /* Update uCode's "work" table, and copy it to DSP */
-       cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
-
-       if (flags & CMD_ASYNC)
-               cmd_out.meta.u.callback = iwl4965_sensitivity_callback;
-
-       /* Don't send command to uCode if nothing has changed */
-       if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
-                   sizeof(u16)*HD_TABLE_SIZE)) {
-               IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
-               return 0;
-       }
-
-       /* Copy table for comparison next time */
-       memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
-              sizeof(u16)*HD_TABLE_SIZE);
-
-       ret = iwl_send_cmd(priv, &cmd_out);
-       if (ret)
-               IWL_ERROR("SENSITIVITY_CMD failed\n");
-
-       return ret;
-}
-
-void iwl4965_init_sensitivity(struct iwl_priv *priv, u8 flags, u8 force)
-{
-       struct iwl4965_sensitivity_data *data = NULL;
-       int i;
-       int ret  = 0;
-
-       IWL_DEBUG_CALIB("Start iwl4965_init_sensitivity\n");
-
-       if (force)
-               memset(&(priv->sensitivity_tbl[0]), 0,
-                       sizeof(u16)*HD_TABLE_SIZE);
-
-       /* Clear driver's sensitivity algo data */
-       data = &(priv->sensitivity_data);
-       memset(data, 0, sizeof(struct iwl4965_sensitivity_data));
-
-       data->num_in_cck_no_fa = 0;
-       data->nrg_curr_state = IWL_FA_TOO_MANY;
-       data->nrg_prev_state = IWL_FA_TOO_MANY;
-       data->nrg_silence_ref = 0;
-       data->nrg_silence_idx = 0;
-       data->nrg_energy_idx = 0;
-
-       for (i = 0; i < 10; i++)
-               data->nrg_value[i] = 0;
-
-       for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
-               data->nrg_silence_rssi[i] = 0;
-
-       data->auto_corr_ofdm = 90;
-       data->auto_corr_ofdm_mrc = 170;
-       data->auto_corr_ofdm_x1  = 105;
-       data->auto_corr_ofdm_mrc_x1 = 220;
-       data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
-       data->auto_corr_cck_mrc = 200;
-       data->nrg_th_cck = 100;
-       data->nrg_th_ofdm = 100;
-
-       data->last_bad_plcp_cnt_ofdm = 0;
-       data->last_fa_cnt_ofdm = 0;
-       data->last_bad_plcp_cnt_cck = 0;
-       data->last_fa_cnt_cck = 0;
-
-       /* Clear prior Sensitivity command data to force send to uCode */
-       if (force)
-               memset(&(priv->sensitivity_tbl[0]), 0,
-                   sizeof(u16)*HD_TABLE_SIZE);
-
-       ret |= iwl4965_sensitivity_write(priv, flags);
-       IWL_DEBUG_CALIB("<<return 0x%X\n", ret);
-
-       return;
-}
-
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
 
 /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
  * Called after every association, but this runs only once!
  *  ... once chain noise is calibrated the first time, it's good forever.  */
-void iwl4965_chain_noise_reset(struct iwl_priv *priv)
+static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
 {
-       struct iwl4965_chain_noise_data *data = NULL;
+       struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
 
-       data = &(priv->chain_noise_data);
        if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
                struct iwl4965_calibration_cmd cmd;
 
@@ -1314,357 +1050,76 @@ void iwl4965_chain_noise_reset(struct iwl_priv *priv)
                cmd.diff_gain_a = 0;
                cmd.diff_gain_b = 0;
                cmd.diff_gain_c = 0;
-               iwl_send_cmd_pdu_async(priv, REPLY_PHY_CALIBRATION_CMD,
-                                sizeof(cmd), &cmd, NULL);
-               msleep(4);
+               if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
+                                sizeof(cmd), &cmd))
+                       IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
                data->state = IWL_CHAIN_NOISE_ACCUMULATE;
                IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
        }
-       return;
 }
 
-/*
- * Accumulate 20 beacons of signal and noise statistics for each of
- *   3 receivers/antennas/rx-chains, then figure out:
- * 1)  Which antennas are connected.
- * 2)  Differential rx gain settings to balance the 3 receivers.
- */
-static void iwl4965_noise_calibration(struct iwl_priv *priv,
-                                     struct iwl4965_notif_statistics *stat_resp)
+static void iwl4965_gain_computation(struct iwl_priv *priv,
+               u32 *average_noise,
+               u16 min_average_noise_antenna_i,
+               u32 min_average_noise)
 {
-       struct iwl4965_chain_noise_data *data = NULL;
-       int ret = 0;
-
-       u32 chain_noise_a;
-       u32 chain_noise_b;
-       u32 chain_noise_c;
-       u32 chain_sig_a;
-       u32 chain_sig_b;
-       u32 chain_sig_c;
-       u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
-       u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
-       u32 max_average_sig;
-       u16 max_average_sig_antenna_i;
-       u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
-       u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
-       u16 i = 0;
-       u16 chan_num = INITIALIZATION_VALUE;
-       u32 band = INITIALIZATION_VALUE;
-       u32 active_chains = 0;
-       unsigned long flags;
-       struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
-
-       data = &(priv->chain_noise_data);
-
-       /* Accumulate just the first 20 beacons after the first association,
-        *   then we're done forever. */
-       if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
-               if (data->state == IWL_CHAIN_NOISE_ALIVE)
-                       IWL_DEBUG_CALIB("Wait for noise calib reset\n");
-               return;
-       }
-
-       spin_lock_irqsave(&priv->lock, flags);
-       if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
-               IWL_DEBUG_CALIB(" << Interference data unavailable\n");
-               spin_unlock_irqrestore(&priv->lock, flags);
-               return;
-       }
-
-       band = (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) ? 0 : 1;
-       chan_num = le16_to_cpu(priv->staging_rxon.channel);
-
-       /* Make sure we accumulate data for just the associated channel
-        *   (even if scanning). */
-       if ((chan_num != (le32_to_cpu(stat_resp->flag) >> 16)) ||
-           ((STATISTICS_REPLY_FLG_BAND_24G_MSK ==
-            (stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK)) && band)) {
-               IWL_DEBUG_CALIB("Stats not from chan=%d, band=%d\n",
-                               chan_num, band);
-               spin_unlock_irqrestore(&priv->lock, flags);
-               return;
-       }
-
-       /* Accumulate beacon statistics values across 20 beacons */
-       chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
-                               IN_BAND_FILTER;
-       chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
-                               IN_BAND_FILTER;
-       chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
-                               IN_BAND_FILTER;
-
-       chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
-       chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
-       chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
+       int i, ret;
+       struct iwl_chain_noise_data *data = &priv->chain_noise_data;
 
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       data->beacon_count++;
-
-       data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
-       data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
-       data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
-
-       data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
-       data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
-       data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
-
-       IWL_DEBUG_CALIB("chan=%d, band=%d, beacon=%d\n", chan_num, band,
-                       data->beacon_count);
-       IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
-                       chain_sig_a, chain_sig_b, chain_sig_c);
-       IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
-                       chain_noise_a, chain_noise_b, chain_noise_c);
-
-       /* If this is the 20th beacon, determine:
-        * 1)  Disconnected antennas (using signal strengths)
-        * 2)  Differential gain (using silence noise) to balance receivers */
-       if (data->beacon_count == CAL_NUM_OF_BEACONS) {
-
-               /* Analyze signal for disconnected antenna */
-               average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
-               average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
-               average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
-
-               if (average_sig[0] >= average_sig[1]) {
-                       max_average_sig = average_sig[0];
-                       max_average_sig_antenna_i = 0;
-                       active_chains = (1 << max_average_sig_antenna_i);
-               } else {
-                       max_average_sig = average_sig[1];
-                       max_average_sig_antenna_i = 1;
-                       active_chains = (1 << max_average_sig_antenna_i);
-               }
-
-               if (average_sig[2] >= max_average_sig) {
-                       max_average_sig = average_sig[2];
-                       max_average_sig_antenna_i = 2;
-                       active_chains = (1 << max_average_sig_antenna_i);
-               }
-
-               IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
-                            average_sig[0], average_sig[1], average_sig[2]);
-               IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
-                            max_average_sig, max_average_sig_antenna_i);
-
-               /* Compare signal strengths for all 3 receivers. */
-               for (i = 0; i < NUM_RX_CHAINS; i++) {
-                       if (i != max_average_sig_antenna_i) {
-                               s32 rssi_delta = (max_average_sig -
-                                                 average_sig[i]);
-
-                               /* If signal is very weak, compared with
-                                * strongest, mark it as disconnected. */
-                               if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
-                                       data->disconn_array[i] = 1;
-                               else
-                                       active_chains |= (1 << i);
-                       IWL_DEBUG_CALIB("i = %d  rssiDelta = %d  "
-                                    "disconn_array[i] = %d\n",
-                                    i, rssi_delta, data->disconn_array[i]);
-                       }
-               }
-
-               /*If both chains A & B are disconnected -
-                * connect B and leave A as is */
-               if (data->disconn_array[CHAIN_A] &&
-                   data->disconn_array[CHAIN_B]) {
-                       data->disconn_array[CHAIN_B] = 0;
-                       active_chains |= (1 << CHAIN_B);
-                       IWL_DEBUG_CALIB("both A & B chains are disconnected! "
-                                    "W/A - declare B as connected\n");
-               }
-
-               IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
-                               active_chains);
-
-               /* Save for use within RXON, TX, SCAN commands, etc. */
-               priv->valid_antenna = active_chains;
-
-               /* Analyze noise for rx balance */
-               average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
-               average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
-               average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
-
-               for (i = 0; i < NUM_RX_CHAINS; i++) {
-                       if (!(data->disconn_array[i]) &&
-                          (average_noise[i] <= min_average_noise)) {
-                               /* This means that chain i is active and has
-                                * lower noise values so far: */
-                               min_average_noise = average_noise[i];
-                               min_average_noise_antenna_i = i;
-                       }
-               }
-
-               data->delta_gain_code[min_average_noise_antenna_i] = 0;
+       data->delta_gain_code[min_average_noise_antenna_i] = 0;
 
-               IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
-                               average_noise[0], average_noise[1],
-                               average_noise[2]);
+       for (i = 0; i < NUM_RX_CHAINS; i++) {
+               s32 delta_g = 0;
 
-               IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
-                               min_average_noise, min_average_noise_antenna_i);
-
-               for (i = 0; i < NUM_RX_CHAINS; i++) {
-                       s32 delta_g = 0;
-
-                       if (!(data->disconn_array[i]) &&
-                           (data->delta_gain_code[i] ==
+               if (!(data->disconn_array[i]) &&
+                   (data->delta_gain_code[i] ==
                             CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
-                               delta_g = average_noise[i] - min_average_noise;
-                               data->delta_gain_code[i] = (u8)((delta_g *
-                                                                   10) / 15);
-                               if (CHAIN_NOISE_MAX_DELTA_GAIN_CODE <
-                                  data->delta_gain_code[i])
-                                       data->delta_gain_code[i] =
-                                         CHAIN_NOISE_MAX_DELTA_GAIN_CODE;
-
-                               data->delta_gain_code[i] =
-                                       (data->delta_gain_code[i] | (1 << 2));
-                       } else
-                               data->delta_gain_code[i] = 0;
-               }
-               IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
-                            data->delta_gain_code[0],
-                            data->delta_gain_code[1],
-                            data->delta_gain_code[2]);
-
-               /* Differential gain gets sent to uCode only once */
-               if (!data->radio_write) {
-                       struct iwl4965_calibration_cmd cmd;
-                       data->radio_write = 1;
-
-                       memset(&cmd, 0, sizeof(cmd));
-                       cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
-                       cmd.diff_gain_a = data->delta_gain_code[0];
-                       cmd.diff_gain_b = data->delta_gain_code[1];
-                       cmd.diff_gain_c = data->delta_gain_code[2];
-                       ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
-                                             sizeof(cmd), &cmd);
-                       if (ret)
-                               IWL_DEBUG_CALIB("fail sending cmd "
-                                            "REPLY_PHY_CALIBRATION_CMD \n");
-
-                       /* TODO we might want recalculate
-                        * rx_chain in rxon cmd */
-
-                       /* Mark so we run this algo only once! */
-                       data->state = IWL_CHAIN_NOISE_CALIBRATED;
+                       delta_g = average_noise[i] - min_average_noise;
+                       data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
+                       data->delta_gain_code[i] =
+                               min(data->delta_gain_code[i],
+                               (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
+
+                       data->delta_gain_code[i] =
+                               (data->delta_gain_code[i] | (1 << 2));
+               } else {
+                       data->delta_gain_code[i] = 0;
                }
-               data->chain_noise_a = 0;
-               data->chain_noise_b = 0;
-               data->chain_noise_c = 0;
-               data->chain_signal_a = 0;
-               data->chain_signal_b = 0;
-               data->chain_signal_c = 0;
-               data->beacon_count = 0;
-       }
-       return;
-}
-
-static void iwl4965_sensitivity_calibration(struct iwl_priv *priv,
-                                           struct iwl4965_notif_statistics *resp)
-{
-       u32 rx_enable_time;
-       u32 fa_cck;
-       u32 fa_ofdm;
-       u32 bad_plcp_cck;
-       u32 bad_plcp_ofdm;
-       u32 norm_fa_ofdm;
-       u32 norm_fa_cck;
-       struct iwl4965_sensitivity_data *data = NULL;
-       struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
-       struct statistics_rx *statistics = &(resp->rx);
-       unsigned long flags;
-       struct statistics_general_data statis;
-       int ret;
-
-       data = &(priv->sensitivity_data);
-
-       if (!iwl_is_associated(priv)) {
-               IWL_DEBUG_CALIB("<< - not associated\n");
-               return;
-       }
-
-       spin_lock_irqsave(&priv->lock, flags);
-       if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
-               IWL_DEBUG_CALIB("<< invalid data.\n");
-               spin_unlock_irqrestore(&priv->lock, flags);
-               return;
-       }
-
-       /* Extract Statistics: */
-       rx_enable_time = le32_to_cpu(rx_info->channel_load);
-       fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
-       fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
-       bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
-       bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
-
-       statis.beacon_silence_rssi_a =
-                       le32_to_cpu(statistics->general.beacon_silence_rssi_a);
-       statis.beacon_silence_rssi_b =
-                       le32_to_cpu(statistics->general.beacon_silence_rssi_b);
-       statis.beacon_silence_rssi_c =
-                       le32_to_cpu(statistics->general.beacon_silence_rssi_c);
-       statis.beacon_energy_a =
-                       le32_to_cpu(statistics->general.beacon_energy_a);
-       statis.beacon_energy_b =
-                       le32_to_cpu(statistics->general.beacon_energy_b);
-       statis.beacon_energy_c =
-                       le32_to_cpu(statistics->general.beacon_energy_c);
-
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
-
-       if (!rx_enable_time) {
-               IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
-               return;
-       }
-
-       /* These statistics increase monotonically, and do not reset
-        *   at each beacon.  Calculate difference from last value, or just
-        *   use the new statistics value if it has reset or wrapped around. */
-       if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
-               data->last_bad_plcp_cnt_cck = bad_plcp_cck;
-       else {
-               bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
-               data->last_bad_plcp_cnt_cck += bad_plcp_cck;
-       }
-
-       if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
-               data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
-       else {
-               bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
-               data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
        }
+       IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
+                    data->delta_gain_code[0],
+                    data->delta_gain_code[1],
+                    data->delta_gain_code[2]);
 
-       if (data->last_fa_cnt_ofdm > fa_ofdm)
-               data->last_fa_cnt_ofdm = fa_ofdm;
-       else {
-               fa_ofdm -= data->last_fa_cnt_ofdm;
-               data->last_fa_cnt_ofdm += fa_ofdm;
-       }
+       /* Differential gain gets sent to uCode only once */
+       if (!data->radio_write) {
+               struct iwl4965_calibration_cmd cmd;
+               data->radio_write = 1;
 
-       if (data->last_fa_cnt_cck > fa_cck)
-               data->last_fa_cnt_cck = fa_cck;
-       else {
-               fa_cck -= data->last_fa_cnt_cck;
-               data->last_fa_cnt_cck += fa_cck;
+               memset(&cmd, 0, sizeof(cmd));
+               cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
+               cmd.diff_gain_a = data->delta_gain_code[0];
+               cmd.diff_gain_b = data->delta_gain_code[1];
+               cmd.diff_gain_c = data->delta_gain_code[2];
+               ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
+                                     sizeof(cmd), &cmd);
+               if (ret)
+                       IWL_DEBUG_CALIB("fail sending cmd "
+                                    "REPLY_PHY_CALIBRATION_CMD \n");
+
+               /* TODO we might want recalculate
+                * rx_chain in rxon cmd */
+
+               /* Mark so we run this algo only once! */
+               data->state = IWL_CHAIN_NOISE_CALIBRATED;
        }
-
-       /* Total aborted signal locks */
-       norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
-       norm_fa_cck = fa_cck + bad_plcp_cck;
-
-       IWL_DEBUG_CALIB("cck: fa %u badp %u  ofdm: fa %u badp %u\n", fa_cck,
-                       bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
-
-       iwl4965_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
-       iwl4965_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
-       ret = iwl4965_sensitivity_write(priv, CMD_ASYNC);
-
-       return;
+       data->chain_noise_a = 0;
+       data->chain_noise_b = 0;
+       data->chain_noise_c = 0;
+       data->chain_signal_a = 0;
+       data->chain_signal_b = 0;
+       data->chain_signal_c = 0;
+       data->beacon_count = 0;
 }
 
 static void iwl4965_bg_sensitivity_work(struct work_struct *work)
@@ -1681,21 +1136,15 @@ static void iwl4965_bg_sensitivity_work(struct work_struct *work)
        }
 
        if (priv->start_calib) {
-               iwl4965_noise_calibration(priv, &priv->statistics);
-
-               if (priv->sensitivity_data.state ==
-                                       IWL_SENS_CALIB_NEED_REINIT) {
-                       iwl4965_init_sensitivity(priv, CMD_ASYNC, 0);
-                       priv->sensitivity_data.state = IWL_SENS_CALIB_ALLOWED;
-               } else
-                       iwl4965_sensitivity_calibration(priv,
-                                       &priv->statistics);
+               iwl_chain_noise_calibration(priv, &priv->statistics);
+
+               iwl_sensitivity_calibration(priv, &priv->statistics);
        }
 
        mutex_unlock(&priv->mutex);
        return;
 }
-#endif /*CONFIG_IWL4965_SENSITIVITY*/
+#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
 
 static void iwl4965_bg_txpower_work(struct work_struct *work)
 {
@@ -1731,7 +1180,7 @@ static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
 {
        iwl_write_direct32(priv, HBUS_TARG_WRPTR,
                             (index & 0xff) | (txq_id << 8));
-       iwl_write_prph(priv, KDR_SCD_QUEUE_RDPTR(txq_id), index);
+       iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
 }
 
 /**
@@ -1751,7 +1200,7 @@ static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
        int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
 
        /* Set up and activate */
-       iwl_write_prph(priv, KDR_SCD_QUEUE_STATUS_BITS(txq_id),
+       iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
                                 (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
                                 (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
                                 (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
@@ -1794,15 +1243,15 @@ int iwl4965_alive_notify(struct iwl_priv *priv)
 
        spin_lock_irqsave(&priv->lock, flags);
 
-#ifdef CONFIG_IWL4965_SENSITIVITY
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
        memset(&(priv->sensitivity_data), 0,
-              sizeof(struct iwl4965_sensitivity_data));
+              sizeof(struct iwl_sensitivity_data));
        memset(&(priv->chain_noise_data), 0,
-              sizeof(struct iwl4965_chain_noise_data));
+              sizeof(struct iwl_chain_noise_data));
        for (i = 0; i < NUM_RX_CHAINS; i++)
                priv->chain_noise_data.delta_gain_code[i] =
                                CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
-#endif /* CONFIG_IWL4965_SENSITIVITY*/
+#endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
        ret = iwl_grab_nic_access(priv);
        if (ret) {
                spin_unlock_irqrestore(&priv->lock, flags);
@@ -1810,28 +1259,28 @@ int iwl4965_alive_notify(struct iwl_priv *priv)
        }
 
        /* Clear 4965's internal Tx Scheduler data base */
-       priv->scd_base_addr = iwl_read_prph(priv, KDR_SCD_SRAM_BASE_ADDR);
+       priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
        a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
        for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
                iwl_write_targ_mem(priv, a, 0);
        for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
                iwl_write_targ_mem(priv, a, 0);
-       for (; a < sizeof(u16) * priv->hw_setting.max_txq_num; a += 4)
+       for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
                iwl_write_targ_mem(priv, a, 0);
 
        /* Tel 4965 where to find Tx byte count tables */
-       iwl_write_prph(priv, KDR_SCD_DRAM_BASE_ADDR,
-               (priv->hw_setting.shared_phys +
+       iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
+               (priv->shared_phys +
                 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
 
        /* Disable chain mode for all queues */
-       iwl_write_prph(priv, KDR_SCD_QUEUECHAIN_SEL, 0);
+       iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
 
        /* Initialize each Tx queue (including the command queue) */
-       for (i = 0; i < priv->hw_setting.max_txq_num; i++) {
+       for (i = 0; i < priv->hw_params.max_txq_num; i++) {
 
                /* TFD circular buffer read/write indexes */
-               iwl_write_prph(priv, KDR_SCD_QUEUE_RDPTR(i), 0);
+               iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
                iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
 
                /* Max Tx Window size for Scheduler-ACK mode */
@@ -1850,11 +1299,11 @@ int iwl4965_alive_notify(struct iwl_priv *priv)
                                        SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
 
        }
-       iwl_write_prph(priv, KDR_SCD_INTERRUPT_MASK,
-                                (1 << priv->hw_setting.max_txq_num) - 1);
+       iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
+                                (1 << priv->hw_params.max_txq_num) - 1);
 
        /* Activate all Tx DMA/FIFO channels */
-       iwl_write_prph(priv, KDR_SCD_TXFACT,
+       iwl_write_prph(priv, IWL49_SCD_TXFACT,
                                 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
 
        iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
@@ -1869,55 +1318,73 @@ int iwl4965_alive_notify(struct iwl_priv *priv)
        iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
+       /* Ask for statistics now, the uCode will send statistics notification
+        * periodically after association */
+       iwl_send_statistics_request(priv, CMD_ASYNC);
        return ret;
 }
 
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
+static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
+       .min_nrg_cck = 97,
+       .max_nrg_cck = 0,
+
+       .auto_corr_min_ofdm = 85,
+       .auto_corr_min_ofdm_mrc = 170,
+       .auto_corr_min_ofdm_x1 = 105,
+       .auto_corr_min_ofdm_mrc_x1 = 220,
+
+       .auto_corr_max_ofdm = 120,
+       .auto_corr_max_ofdm_mrc = 210,
+       .auto_corr_max_ofdm_x1 = 140,
+       .auto_corr_max_ofdm_mrc_x1 = 270,
+
+       .auto_corr_min_cck = 125,
+       .auto_corr_max_cck = 200,
+       .auto_corr_min_cck_mrc = 200,
+       .auto_corr_max_cck_mrc = 400,
+
+       .nrg_th_cck = 100,
+       .nrg_th_ofdm = 100,
+};
+#endif
+
 /**
- * iwl4965_hw_set_hw_setting
+ * iwl4965_hw_set_hw_params
  *
  * Called when initializing driver
  */
-int iwl4965_hw_set_hw_setting(struct iwl_priv *priv)
+int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
 {
-       int ret = 0;
 
-       if ((priv->cfg->mod_params->num_of_queues > IWL_MAX_NUM_QUEUES) ||
+       if ((priv->cfg->mod_params->num_of_queues > IWL4965_MAX_NUM_QUEUES) ||
            (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
                IWL_ERROR("invalid queues_num, should be between %d and %d\n",
-                         IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
-               ret = -EINVAL;
-               goto out;
-       }
-
-       /* Allocate area for Tx byte count tables and Rx queue status */
-       priv->hw_setting.shared_virt =
-           pci_alloc_consistent(priv->pci_dev,
-                                sizeof(struct iwl4965_shared),
-                                &priv->hw_setting.shared_phys);
-
-       if (!priv->hw_setting.shared_virt) {
-               ret = -ENOMEM;
-               goto out;
+                         IWL_MIN_NUM_QUEUES, IWL4965_MAX_NUM_QUEUES);
+               return -EINVAL;
        }
 
-       memset(priv->hw_setting.shared_virt, 0, sizeof(struct iwl4965_shared));
-
-       priv->hw_setting.max_txq_num = priv->cfg->mod_params->num_of_queues;
-       priv->hw_setting.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
-       priv->hw_setting.max_rxq_size = RX_QUEUE_SIZE;
-       priv->hw_setting.max_rxq_log = RX_QUEUE_SIZE_LOG;
+       priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
+       priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
+       priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
+       priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
        if (priv->cfg->mod_params->amsdu_size_8K)
-               priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE_8K;
+               priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
        else
-               priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE_4K;
-       priv->hw_setting.max_pkt_size = priv->hw_setting.rx_buf_size - 256;
-       priv->hw_setting.max_stations = IWL4965_STATION_COUNT;
-       priv->hw_setting.bcast_sta_id = IWL4965_BROADCAST_ID;
-
-       priv->hw_setting.tx_ant_num = 2;
+               priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
+       priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
+       priv->hw_params.max_stations = IWL4965_STATION_COUNT;
+       priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
+
+       priv->hw_params.tx_chains_num = 2;
+       priv->hw_params.rx_chains_num = 2;
+       priv->hw_params.valid_tx_ant = (IWL_ANTENNA_MAIN | IWL_ANTENNA_AUX);
+       priv->hw_params.valid_rx_ant = (IWL_ANTENNA_MAIN | IWL_ANTENNA_AUX);
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
+       priv->hw_params.sens = &iwl4965_sensitivity;
+#endif
 
-out:
-       return ret;
+       return 0;
 }
 
 /**
@@ -1930,7 +1397,7 @@ void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
        int txq_id;
 
        /* Tx queues */
-       for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
+       for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
                iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
 
        /* Keep-warm buffer */
@@ -2756,6 +2223,46 @@ out:
        return ret;
 }
 
+static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
+{
+       int ret = 0;
+       struct iwl4965_rxon_assoc_cmd rxon_assoc;
+       const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
+       const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
+
+       if ((rxon1->flags == rxon2->flags) &&
+           (rxon1->filter_flags == rxon2->filter_flags) &&
+           (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
+           (rxon1->ofdm_ht_single_stream_basic_rates ==
+            rxon2->ofdm_ht_single_stream_basic_rates) &&
+           (rxon1->ofdm_ht_dual_stream_basic_rates ==
+            rxon2->ofdm_ht_dual_stream_basic_rates) &&
+           (rxon1->rx_chain == rxon2->rx_chain) &&
+           (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
+               IWL_DEBUG_INFO("Using current RXON_ASSOC.  Not resending.\n");
+               return 0;
+       }
+
+       rxon_assoc.flags = priv->staging_rxon.flags;
+       rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
+       rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
+       rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
+       rxon_assoc.reserved = 0;
+       rxon_assoc.ofdm_ht_single_stream_basic_rates =
+           priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
+       rxon_assoc.ofdm_ht_dual_stream_basic_rates =
+           priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
+       rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
+
+       ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
+                                    sizeof(rxon_assoc), &rxon_assoc, NULL);
+       if (ret)
+               return ret;
+
+       return ret;
+}
+
+
 int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
 {
        int rc;
@@ -2869,9 +2376,8 @@ void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
 
 int iwl4965_hw_get_rx_read(struct iwl_priv *priv)
 {
-       struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
-
-       return IWL_GET_BITS(*shared_data, rb_closed_stts_rb_num);
+       struct iwl4965_shared *s = priv->shared_virt;
+       return le32_to_cpu(s->rb_closed) & 0xFFF;
 }
 
 int iwl4965_hw_get_temperature(struct iwl_priv *priv)
@@ -2888,7 +2394,7 @@ unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
        tx_beacon_cmd = &frame->u.beacon;
        memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
 
-       tx_beacon_cmd->tx.sta_id = priv->hw_setting.bcast_sta_id;
+       tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
        tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
 
        frame_size = iwl4965_fill_beacon_frame(priv,
@@ -2996,17 +2502,15 @@ static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
 #define IWL_TX_DELIMITER_SIZE  4
 
 /**
- * iwl4965_tx_queue_update_wr_ptr - Set up entry in Tx byte-count array
+ * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  */
-int iwl4965_tx_queue_update_wr_ptr(struct iwl_priv *priv,
-                                  struct iwl4965_tx_queue *txq, u16 byte_cnt)
+static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
+                                           struct iwl4965_tx_queue *txq,
+                                           u16 byte_cnt)
 {
        int len;
        int txq_id = txq->q.id;
-       struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
-
-       if (txq->need_update == 0)
-               return 0;
+       struct iwl4965_shared *shared_data = priv->shared_virt;
 
        len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
 
@@ -3019,8 +2523,6 @@ int iwl4965_tx_queue_update_wr_ptr(struct iwl_priv *priv,
                IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
                        tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
                        byte_cnt, len);
-
-       return 0;
 }
 
 /**
@@ -3039,7 +2541,7 @@ void iwl4965_set_rxon_chain(struct iwl_priv *priv)
 
        /* Tell uCode which antennas are actually connected.
         * Before first association, we assume all antennas are connected.
-        * Just after first association, iwl4965_noise_calibration()
+        * Just after first association, iwl_chain_noise_calibration()
         *    checks which antennas actually *are* connected. */
        priv->staging_rxon.rx_chain |=
            cpu_to_le16(priv->valid_antenna << RXON_RX_CHAIN_VALID_POS);
@@ -3249,7 +2751,7 @@ void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffe
        if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
            (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
                iwl4965_rx_calc_noise(priv);
-#ifdef CONFIG_IWL4965_SENSITIVITY
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
                queue_work(priv->workqueue, &priv->sensitivity_work);
 #endif
        }
@@ -3296,6 +2798,7 @@ static void iwl4965_add_radiotap(struct iwl_priv *priv,
        s8 noise = 0;
        int rate = stats->rate_idx;
        u64 tsf = stats->mactime;
+       __le16 antenna;
        __le16 phy_flags_hw = rx_start->phy_flags;
        struct iwl4965_rt_rx_hdr {
                struct ieee80211_radiotap_header rt_hdr;
@@ -3380,8 +2883,8 @@ static void iwl4965_add_radiotap(struct iwl_priv *priv,
         * new 802.11n radiotap field "RX chains" that is defined
         * as a bitmask.
         */
-       iwl4965_rt->rt_antenna =
-               le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
+       antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
+       iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
 
        /* set the preamble flag if appropriate */
        if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
@@ -3499,7 +3002,7 @@ static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
                rx_start->byte_count = amsdu->byte_count;
                rx_end = (__le32 *) (((u8 *) hdr) + len);
        }
-       if (len > priv->hw_setting.max_pkt_size || len < 16) {
+       if (len > priv->hw_params.max_pkt_size || len < 16) {
                IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
                return;
        }
@@ -3527,7 +3030,7 @@ static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
        stats->flag = 0;
        hdr = (struct ieee80211_hdr *)rxb->skb->data;
 
-       if (priv->cfg->mod_params->hw_crypto)
+       if (!priv->cfg->mod_params->sw_crypto)
                iwl4965_set_decrypted_flag(priv, rxb->skb, ampdu_status, stats);
 
        if (priv->add_radiotap)
@@ -3813,12 +3316,12 @@ static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
        u8 network_packet;
 
        rx_status.mactime = le64_to_cpu(rx_start->timestamp);
-       rx_status.freq = ieee80211chan2mhz(le16_to_cpu(rx_start->channel));
+       rx_status.freq =
+               ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
        rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
                                IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
-       rx_status.rate_idx = iwl4965_hwrate_to_plcp_idx(
-                                       le32_to_cpu(rx_start->rate_n_flags));
-
+       rx_status.rate_idx =
+               iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
        if (rx_status.band == IEEE80211_BAND_5GHZ)
                rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
 
@@ -3826,9 +3329,8 @@ static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
        rx_status.flag = 0;
 
        if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
-               IWL_DEBUG_DROP
-                       ("dsp size out of range [0,20]: "
-                        "%d/n", rx_start->cfg_phy_cnt);
+               IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
+                               rx_start->cfg_phy_cnt);
                return;
        }
 
@@ -3976,7 +3478,7 @@ static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
                                           struct iwl4965_rx_mem_buffer *rxb)
 
 {
-#ifdef CONFIG_IWL4965_SENSITIVITY
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
        struct iwl4965_missed_beacon_notif *missed_beacon;
 
@@ -3987,11 +3489,10 @@ static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
                    le32_to_cpu(missed_beacon->total_missed_becons),
                    le32_to_cpu(missed_beacon->num_recvd_beacons),
                    le32_to_cpu(missed_beacon->num_expected_beacons));
-               priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
-               if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)))
-                       queue_work(priv->workqueue, &priv->sensitivity_work);
+               if (!test_bit(STATUS_SCANNING, &priv->status))
+                       iwl_init_sensitivity(priv);
        }
-#endif /*CONFIG_IWL4965_SENSITIVITY*/
+#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
 }
 #ifdef CONFIG_IWL4965_HT
 
@@ -4090,7 +3591,7 @@ static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
        /* Simply stop the queue, but don't change any configuration;
         * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
        iwl_write_prph(priv,
-               KDR_SCD_QUEUE_STATUS_BITS(txq_id),
+               IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
                (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
                (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
 }
@@ -4116,14 +3617,14 @@ static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
 
        iwl4965_tx_queue_stop_scheduler(priv, txq_id);
 
-       iwl_clear_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1 << txq_id));
+       iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
 
        priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
        priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
        /* supposes that ssn_idx is valid (!= 0xFFF) */
        iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
 
-       iwl_clear_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
+       iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
        iwl4965_txq_ctx_deactivate(priv, txq_id);
        iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
 
@@ -4199,7 +3700,7 @@ static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
         * (in Tx queue's circular buffer) of first TFD/frame in window */
        u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
 
-       if (scd_flow >= ARRAY_SIZE(priv->txq)) {
+       if (scd_flow >= priv->hw_params.max_txq_num) {
                IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
                return;
        }
@@ -4312,7 +3813,7 @@ static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
        iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
 
        /* Set this queue as a chain-building queue */
-       iwl_set_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1 << txq_id));
+       iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
 
        /* Place first TFD at index corresponding to start sequence number.
         * Assumes that ssn_idx is valid (!= 0xFFF) */
@@ -4331,7 +3832,7 @@ static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
                        (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
                        & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
 
-       iwl_set_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
+       iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
 
        /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
        iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
@@ -4361,7 +3862,7 @@ static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
 void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
 {
        int i, r;
-       struct iwl4965_link_quality_cmd link_cmd = {
+       struct iwl_link_quality_cmd link_cmd = {
                .reserved1 = 0,
        };
        u16 rate_flags;
@@ -4395,7 +3896,7 @@ void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
        link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
 
        /* Update the rate scaling for control frame Tx to AP */
-       link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_setting.bcast_sta_id;
+       link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
 
        iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
                               sizeof(link_cmd), &link_cmd, NULL);
@@ -4584,7 +4085,7 @@ static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
 {
        int txq_id;
 
-       for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
+       for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
                if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
                        return txq_id;
        return -1;
@@ -4767,8 +4268,7 @@ void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
 void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
 {
        INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
-       INIT_WORK(&priv->statistics_work, iwl4965_bg_statistics_work);
-#ifdef CONFIG_IWL4965_SENSITIVITY
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
        INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
 #endif
        init_timer(&priv->statistics_periodic);
@@ -4783,12 +4283,27 @@ void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
        cancel_delayed_work(&priv->init_alive_start);
 }
 
+
+static struct iwl_hcmd_ops iwl4965_hcmd = {
+       .rxon_assoc = iwl4965_send_rxon_assoc,
+};
+
 static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
        .enqueue_hcmd = iwl4965_enqueue_hcmd,
+#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
+       .chain_noise_reset = iwl4965_chain_noise_reset,
+       .gain_computation = iwl4965_gain_computation,
+#endif
 };
 
 static struct iwl_lib_ops iwl4965_lib = {
        .init_drv = iwl4965_init_drv,
+       .set_hw_params = iwl4965_hw_set_hw_params,
+       .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
+       .hw_nic_init = iwl4965_hw_nic_init,
+       .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
+       .alive_notify = iwl4965_alive_notify,
+       .load_ucode = iwl4965_load_bsm,
        .eeprom_ops = {
                .verify_signature  = iwlcore_eeprom_verify_signature,
                .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
@@ -4799,10 +4314,11 @@ static struct iwl_lib_ops iwl4965_lib = {
 
 static struct iwl_ops iwl4965_ops = {
        .lib = &iwl4965_lib,
+       .hcmd = &iwl4965_hcmd,
        .utils = &iwl4965_hcmd_utils,
 };
 
-static struct iwl_cfg iwl4965_agn_cfg = {
+struct iwl_cfg iwl4965_agn_cfg = {
        .name = "4965AGN",
        .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
        .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
@@ -4810,21 +4326,12 @@ static struct iwl_cfg iwl4965_agn_cfg = {
        .mod_params = &iwl4965_mod_params,
 };
 
-struct pci_device_id iwl4965_hw_card_ids[] = {
-       {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
-       {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
-       {0}
-};
-
-MODULE_DEVICE_TABLE(pci, iwl4965_hw_card_ids);
-
 module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
 module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
-module_param_named(hwcrypto, iwl4965_mod_params.hw_crypto, int, 0444);
-MODULE_PARM_DESC(hwcrypto,
-                "using hardware crypto engine (default 0 [software])\n");
+module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
+MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
 module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
 MODULE_PARM_DESC(debug, "debug output mask");
 module_param_named(