iwlwifi: HW dependent run time calibration
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-4965.c
index 2f01a49..be4cc5f 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
+ * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of version 2 of the GNU General Public License as
 #include <linux/etherdevice.h>
 #include <asm/unaligned.h>
 
-#include "iwl-core.h"
+#include "iwl-eeprom.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 = IWL4965_MAX_NUM_QUEUES,
+       .enable_qos = 1,
+       .amsdu_size_8K = 1,
+       /* the rest are 0 by default */
+};
 
-static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv);
+static void iwl4965_hw_card_show_info(struct iwl_priv *priv);
 
 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
        [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,      \
@@ -104,13 +115,253 @@ 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;
+       int i;
+
+       priv->antenna = (enum iwl4965_antenna)priv->cfg->mod_params->antenna;
+       priv->retry_rate = 1;
+       priv->ibss_beacon = NULL;
+
+       spin_lock_init(&priv->lock);
+       spin_lock_init(&priv->power_data.lock);
+       spin_lock_init(&priv->sta_lock);
+       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]);
+
+       INIT_LIST_HEAD(&priv->free_frames);
+
+       mutex_init(&priv->mutex);
+
+       /* Clear the driver's (not device's) station table */
+       iwlcore_clear_stations_table(priv);
+
+       priv->data_retry_limit = -1;
+       priv->ieee_channels = NULL;
+       priv->ieee_rates = NULL;
+       priv->band = IEEE80211_BAND_2GHZ;
+
+       priv->iw_mode = IEEE80211_IF_TYPE_STA;
+
+       priv->use_ant_b_for_management_frame = 1; /* start with ant B */
+       priv->valid_antenna = 0x7;      /* assume all 3 connected */
+       priv->ps_mode = IWL_MIMO_PS_NONE;
+
+       /* Choose which receivers/antennas to use */
+       iwl4965_set_rxon_chain(priv);
+
+       iwlcore_reset_qos(priv);
+
+       priv->qos_data.qos_active = 0;
+       priv->qos_data.qos_cap.val = 0;
+
+       iwlcore_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
+
+       priv->rates_mask = IWL_RATES_MASK;
+       /* If power management is turned on, default to AC mode */
+       priv->power_mode = IWL_POWER_AC;
+       priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
+
+       ret = iwl_init_channel_map(priv);
+       if (ret) {
+               IWL_ERROR("initializing regulatory failed: %d\n", ret);
+               goto err;
+       }
+
+       ret = iwl4965_init_geos(priv);
+       if (ret) {
+               IWL_ERROR("initializing geos failed: %d\n", ret);
+               goto err_free_channel_map;
+       }
+
+       ret = ieee80211_register_hw(priv->hw);
+       if (ret) {
+               IWL_ERROR("Failed to register network device (error %d)\n",
+                               ret);
+               goto err_free_geos;
+       }
+
+       priv->hw->conf.beacon_int = 100;
+       priv->mac80211_registered = 1;
+
+       return 0;
+
+err_free_geos:
+       iwl4965_free_geos(priv);
+err_free_channel_map:
+       iwl_free_channel_map(priv);
+err:
+       return ret;
+}
+
 static int is_fat_channel(__le32 rxon_flags)
 {
        return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
                (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
 }
 
-static u8 is_single_stream(struct iwl4965_priv *priv)
+static u8 is_single_stream(struct iwl_priv *priv)
 {
 #ifdef CONFIG_IWL4965_HT
        if (!priv->current_ht_config.is_ht ||
@@ -154,13 +405,13 @@ int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
 /**
  * translate ucode response to mac80211 tx status control values
  */
-void iwl4965_hwrate_to_tx_control(struct iwl4965_priv *priv, u32 rate_n_flags,
+void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
                                  struct ieee80211_tx_control *control)
 {
        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)
@@ -187,7 +438,7 @@ void iwl4965_hwrate_to_tx_control(struct iwl4965_priv *priv, u32 rate_n_flags,
  * MIMO (dual stream) requires at least 2, but works better with 3.
  * This does not determine *which* chains to use, just how many.
  */
-static int iwl4965_get_rx_chain_counter(struct iwl4965_priv *priv,
+static int iwl4965_get_rx_chain_counter(struct iwl_priv *priv,
                                        u8 *idle_state, u8 *rx_state)
 {
        u8 is_single = is_single_stream(priv);
@@ -216,32 +467,32 @@ static int iwl4965_get_rx_chain_counter(struct iwl4965_priv *priv,
        return 0;
 }
 
-int iwl4965_hw_rxq_stop(struct iwl4965_priv *priv)
+int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
 {
        int rc;
        unsigned long flags;
 
        spin_lock_irqsave(&priv->lock, flags);
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc) {
                spin_unlock_irqrestore(&priv->lock, flags);
                return rc;
        }
 
        /* stop Rx DMA */
-       iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
-       rc = iwl4965_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
+       iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
+       rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
                                     (1 << 24), 1000);
        if (rc < 0)
                IWL_ERROR("Can't stop Rx DMA.\n");
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
 }
 
-u8 iwl4965_hw_find_station(struct iwl4965_priv *priv, const u8 *addr)
+u8 iwl4965_hw_find_station(struct iwl_priv *priv, const u8 *addr)
 {
        int i;
        int start = 0;
@@ -254,10 +505,10 @@ u8 iwl4965_hw_find_station(struct iwl4965_priv *priv, const u8 *addr)
                start = IWL_STA_ID;
 
        if (is_broadcast_ether_addr(addr))
-               return IWL4965_BROADCAST_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))) {
@@ -273,13 +524,13 @@ u8 iwl4965_hw_find_station(struct iwl4965_priv *priv, const u8 *addr)
        return ret;
 }
 
-static int iwl4965_nic_set_pwr_src(struct iwl4965_priv *priv, int pwr_max)
+static int iwl4965_nic_set_pwr_src(struct iwl_priv *priv, int pwr_max)
 {
        int ret;
        unsigned long flags;
 
        spin_lock_irqsave(&priv->lock, flags);
-       ret = iwl4965_grab_nic_access(priv);
+       ret = iwl_grab_nic_access(priv);
        if (ret) {
                spin_unlock_irqrestore(&priv->lock, flags);
                return ret;
@@ -292,92 +543,92 @@ static int iwl4965_nic_set_pwr_src(struct iwl4965_priv *priv, int pwr_max)
                                           &val);
 
                if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT)
-                       iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
+                       iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
                                APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
                                ~APMG_PS_CTRL_MSK_PWR_SRC);
        } else
-               iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
+               iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
                        APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
                        ~APMG_PS_CTRL_MSK_PWR_SRC);
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        return ret;
 }
 
-static int iwl4965_rx_init(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
+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 = iwl4965_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 (iwl4965_param_amsdu_size_8K)
+       if (priv->cfg->mod_params->amsdu_size_8K)
                rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
        else
                rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
 
        /* Stop Rx DMA */
-       iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
+       iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
 
        /* Reset driver's Rx queue write index */
-       iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
+       iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
 
        /* Tell device where to find RBD circular buffer in DRAM */
-       iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
-                            rxq->dma_addr >> 8);
+       iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
+                          rxq->dma_addr >> 8);
 
        /* Tell device where in DRAM to update its Rx status */
-       iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
-                            (priv->hw_setting.shared_phys +
-                             offsetof(struct iwl4965_shared, val0)) >> 4);
+       iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
+                          (priv->shared_phys +
+                           offsetof(struct iwl4965_shared, rb_closed)) >> 4);
 
        /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
-       iwl4965_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 | */
-                            (RX_QUEUE_SIZE_LOG <<
+       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 | */
+                          (RX_QUEUE_SIZE_LOG <<
                              FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
 
        /*
-        * iwl4965_write32(priv,CSR_INT_COAL_REG,0);
+        * iwl_write32(priv,CSR_INT_COAL_REG,0);
         */
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
 }
 
 /* Tell 4965 where to find the "keep warm" buffer */
-static int iwl4965_kw_init(struct iwl4965_priv *priv)
+static int iwl4965_kw_init(struct iwl_priv *priv)
 {
        unsigned long flags;
        int rc;
 
        spin_lock_irqsave(&priv->lock, flags);
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc)
                goto out;
 
-       iwl4965_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
+       iwl_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
                             priv->kw.dma_addr >> 4);
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
 out:
        spin_unlock_irqrestore(&priv->lock, flags);
        return rc;
 }
 
-static int iwl4965_kw_alloc(struct iwl4965_priv *priv)
+static int iwl4965_kw_alloc(struct iwl_priv *priv)
 {
        struct pci_dev *dev = priv->pci_dev;
        struct iwl4965_kw *kw = &priv->kw;
@@ -390,59 +641,10 @@ static int iwl4965_kw_alloc(struct iwl4965_priv *priv)
        return 0;
 }
 
-#define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
-                           ? # x " " : "")
-
-/**
- * iwl4965_set_fat_chan_info - Copy fat channel info into driver's priv.
- *
- * Does not set up a command, or touch hardware.
- */
-int iwl4965_set_fat_chan_info(struct iwl4965_priv *priv,
-                             enum ieee80211_band band, u16 channel,
-                             const struct iwl4965_eeprom_channel *eeprom_ch,
-                             u8 fat_extension_channel)
-{
-       struct iwl4965_channel_info *ch_info;
-
-       ch_info = (struct iwl4965_channel_info *)
-                       iwl4965_get_channel_info(priv, band, channel);
-
-       if (!is_channel_valid(ch_info))
-               return -1;
-
-       IWL_DEBUG_INFO("FAT Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
-                       " %ddBm): Ad-Hoc %ssupported\n",
-                       ch_info->channel,
-                       is_channel_a_band(ch_info) ?
-                       "5.2" : "2.4",
-                       CHECK_AND_PRINT(IBSS),
-                       CHECK_AND_PRINT(ACTIVE),
-                       CHECK_AND_PRINT(RADAR),
-                       CHECK_AND_PRINT(WIDE),
-                       CHECK_AND_PRINT(NARROW),
-                       CHECK_AND_PRINT(DFS),
-                       eeprom_ch->flags,
-                       eeprom_ch->max_power_avg,
-                       ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
-                        && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
-                       "" : "not ");
-
-       ch_info->fat_eeprom = *eeprom_ch;
-       ch_info->fat_max_power_avg = eeprom_ch->max_power_avg;
-       ch_info->fat_curr_txpow = eeprom_ch->max_power_avg;
-       ch_info->fat_min_power = 0;
-       ch_info->fat_scan_power = eeprom_ch->max_power_avg;
-       ch_info->fat_flags = eeprom_ch->flags;
-       ch_info->fat_extension_channel = fat_extension_channel;
-
-       return 0;
-}
-
 /**
  * iwl4965_kw_free - Free the "keep warm" buffer
  */
-static void iwl4965_kw_free(struct iwl4965_priv *priv)
+static void iwl4965_kw_free(struct iwl_priv *priv)
 {
        struct pci_dev *dev = priv->pci_dev;
        struct iwl4965_kw *kw = &priv->kw;
@@ -460,7 +662,7 @@ static void iwl4965_kw_free(struct iwl4965_priv *priv)
  * @param priv
  * @return error code
  */
-static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
+static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
 {
        int rc = 0;
        int txq_id, slots_num;
@@ -480,7 +682,7 @@ static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
 
        spin_lock_irqsave(&priv->lock, flags);
 
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (unlikely(rc)) {
                IWL_ERROR("TX reset failed");
                spin_unlock_irqrestore(&priv->lock, flags);
@@ -488,8 +690,8 @@ static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
        }
 
        /* Turn off all Tx DMA channels */
-       iwl4965_write_prph(priv, KDR_SCD_TXFACT, 0);
-       iwl4965_release_nic_access(priv);
+       iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        /* Tell 4965 where to find the keep-warm buffer */
@@ -501,7 +703,7 @@ static int iwl4965_txq_ctx_reset(struct iwl4965_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,
@@ -522,7 +724,7 @@ static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
        return rc;
 }
 
-int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
+int iwl4965_hw_nic_init(struct iwl_priv *priv)
 {
        int rc;
        unsigned long flags;
@@ -536,11 +738,11 @@ int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
        /* nic_init */
        spin_lock_irqsave(&priv->lock, flags);
 
-       iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
+       iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
                    CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
 
-       iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
-       rc = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
+       iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
+       rc = iwl_poll_bit(priv, CSR_GP_CNTRL,
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
        if (rc < 0) {
@@ -549,26 +751,25 @@ int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
                return rc;
        }
 
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc) {
                spin_unlock_irqrestore(&priv->lock, flags);
                return rc;
        }
 
-       iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
+       iwl_read_prph(priv, APMG_CLK_CTRL_REG);
 
-       iwl4965_write_prph(priv, APMG_CLK_CTRL_REG,
-                                APMG_CLK_VAL_DMA_CLK_RQT |
-                                APMG_CLK_VAL_BSM_CLK_RQT);
-       iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
+       iwl_write_prph(priv, APMG_CLK_CTRL_REG,
+                       APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
+       iwl_read_prph(priv, APMG_CLK_CTRL_REG);
 
        udelay(20);
 
-       iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
-                                   APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
+       iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
+                               APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
 
-       iwl4965_release_nic_access(priv);
-       iwl4965_write32(priv, CSR_INT_COALESCING, 512 / 32);
+       iwl_release_nic_access(priv);
+       iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        /* Determine HW type */
@@ -604,26 +805,24 @@ int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
 
        /* set CSR_HW_CONFIG_REG for uCode use */
 
-       iwl4965_set_bit(priv, CSR_HW_IF_CONFIG_REG,
-                       CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
-                       CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
-                       CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
+       iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
+                   CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
+                   CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
+                   CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
 
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc < 0) {
                spin_unlock_irqrestore(&priv->lock, flags);
                IWL_DEBUG_INFO("Failed to init the card\n");
                return rc;
        }
 
-       iwl4965_read_prph(priv, APMG_PS_CTRL_REG);
-       iwl4965_set_bits_prph(priv, APMG_PS_CTRL_REG,
-                                   APMG_PS_CTRL_VAL_RESET_REQ);
+       iwl_read_prph(priv, APMG_PS_CTRL_REG);
+       iwl_set_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
        udelay(5);
-       iwl4965_clear_bits_prph(priv, APMG_PS_CTRL_REG,
-                                     APMG_PS_CTRL_VAL_RESET_REQ);
+       iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        iwl4965_hw_card_show_info(priv);
@@ -667,7 +866,7 @@ int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
        return 0;
 }
 
-int iwl4965_hw_nic_stop_master(struct iwl4965_priv *priv)
+int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
 {
        int rc = 0;
        u32 reg_val;
@@ -676,16 +875,16 @@ int iwl4965_hw_nic_stop_master(struct iwl4965_priv *priv)
        spin_lock_irqsave(&priv->lock, flags);
 
        /* set stop master bit */
-       iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
+       iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
 
-       reg_val = iwl4965_read32(priv, CSR_GP_CNTRL);
+       reg_val = iwl_read32(priv, CSR_GP_CNTRL);
 
        if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
            (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
                IWL_DEBUG_INFO("Card in power save, master is already "
                               "stopped\n");
        else {
-               rc = iwl4965_poll_bit(priv, CSR_RESET,
+               rc = iwl_poll_bit(priv, CSR_RESET,
                                  CSR_RESET_REG_FLAG_MASTER_DISABLED,
                                  CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
                if (rc < 0) {
@@ -703,27 +902,26 @@ int iwl4965_hw_nic_stop_master(struct iwl4965_priv *priv)
 /**
  * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  */
-void iwl4965_hw_txq_ctx_stop(struct iwl4965_priv *priv)
+void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
 {
 
        int txq_id;
        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 (iwl4965_grab_nic_access(priv)) {
+               if (iwl_grab_nic_access(priv)) {
                        spin_unlock_irqrestore(&priv->lock, flags);
                        continue;
                }
 
-               iwl4965_write_direct32(priv,
-                                    IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
-                                    0x0);
-               iwl4965_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
-                                       IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
-                                       (txq_id), 200);
-               iwl4965_release_nic_access(priv);
+               iwl_write_direct32(priv,
+                                  IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
+               iwl_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
+                                   IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
+                                   (txq_id), 200);
+               iwl_release_nic_access(priv);
                spin_unlock_irqrestore(&priv->lock, flags);
        }
 
@@ -731,7 +929,7 @@ void iwl4965_hw_txq_ctx_stop(struct iwl4965_priv *priv)
        iwl4965_hw_txq_ctx_free(priv);
 }
 
-int iwl4965_hw_nic_reset(struct iwl4965_priv *priv)
+int iwl4965_hw_nic_reset(struct iwl_priv *priv)
 {
        int rc = 0;
        unsigned long flags;
@@ -740,29 +938,29 @@ int iwl4965_hw_nic_reset(struct iwl4965_priv *priv)
 
        spin_lock_irqsave(&priv->lock, flags);
 
-       iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
+       iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
 
        udelay(10);
 
-       iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
-       rc = iwl4965_poll_bit(priv, CSR_RESET,
+       iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
+       rc = iwl_poll_bit(priv, CSR_RESET,
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
 
        udelay(10);
 
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (!rc) {
-               iwl4965_write_prph(priv, APMG_CLK_EN_REG,
-                                        APMG_CLK_VAL_DMA_CLK_RQT |
-                                        APMG_CLK_VAL_BSM_CLK_RQT);
+               iwl_write_prph(priv, APMG_CLK_EN_REG,
+                               APMG_CLK_VAL_DMA_CLK_RQT |
+                               APMG_CLK_VAL_BSM_CLK_RQT);
 
                udelay(10);
 
-               iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
-                               APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
+               iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
+                                       APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
 
-               iwl4965_release_nic_access(priv);
+               iwl_release_nic_access(priv);
        }
 
        clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
@@ -779,56 +977,37 @@ int iwl4965_hw_nic_reset(struct iwl4965_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 iwl4965_priv *priv = (struct iwl4965_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 iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
-                                            statistics_work);
+       struct iwl_priv *priv = (struct iwl_priv *)data;
 
        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
 #define TM_CT_KILL_THRESHOLD   110
 
-void iwl4965_rf_kill_ct_config(struct iwl4965_priv *priv)
+void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
 {
        struct iwl4965_ct_kill_config cmd;
        u32 R1, R2, R3;
        u32 temp_th;
        u32 crit_temperature;
        unsigned long flags;
-       int rc = 0;
+       int ret = 0;
 
        spin_lock_irqsave(&priv->lock, flags);
-       iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
+       iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
                    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
        spin_unlock_irqrestore(&priv->lock, flags);
 
@@ -846,430 +1025,24 @@ void iwl4965_rf_kill_ct_config(struct iwl4965_priv *priv)
 
        crit_temperature = ((temp_th * (R3-R1))/CT_LIMIT_CONST) + R2;
        cmd.critical_temperature_R =  cpu_to_le32(crit_temperature);
-       rc = iwl4965_send_cmd_pdu(priv,
-                             REPLY_CT_KILL_CONFIG_CMD, sizeof(cmd), &cmd);
-       if (rc)
+       ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
+                              sizeof(cmd), &cmd);
+       if (ret)
                IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
        else
                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 iwl4965_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 iwl4965_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 iwl4965_priv *priv,
-                                   struct iwl4965_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 iwl4965_priv *priv, u8 flags)
-{
-       int rc = 0;
-       struct iwl4965_sensitivity_cmd cmd ;
-       struct iwl4965_sensitivity_data *data = NULL;
-       struct iwl4965_host_cmd cmd_out = {
-               .id = SENSITIVITY_CMD,
-               .len = sizeof(struct iwl4965_sensitivity_cmd),
-               .meta.flags = flags,
-               .data = &cmd,
-       };
-
-       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);
-
-       rc = iwl4965_send_cmd(priv, &cmd_out);
-       if (!rc) {
-               IWL_DEBUG_CALIB("SENSITIVITY_CMD succeeded\n");
-               return rc;
-       }
-
-       return 0;
-}
-
-void iwl4965_init_sensitivity(struct iwl4965_priv *priv, u8 flags, u8 force)
-{
-       int rc = 0;
-       int i;
-       struct iwl4965_sensitivity_data *data = NULL;
-
-       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);
-
-       rc |= iwl4965_sensitivity_write(priv, flags);
-       IWL_DEBUG_CALIB("<<return 0x%X\n", rc);
-
-       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 iwl4965_priv *priv)
+static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
 {
-       struct iwl4965_chain_noise_data *data = NULL;
-       int rc = 0;
+       struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
 
-       data = &(priv->chain_noise_data);
-       if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl4965_is_associated(priv)) {
+       if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
                struct iwl4965_calibration_cmd cmd;
 
                memset(&cmd, 0, sizeof(cmd));
@@ -1277,362 +1050,81 @@ void iwl4965_chain_noise_reset(struct iwl4965_priv *priv)
                cmd.diff_gain_a = 0;
                cmd.diff_gain_b = 0;
                cmd.diff_gain_c = 0;
-               rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
-                                sizeof(cmd), &cmd);
-               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 iwl4965_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 rc = 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;
-
-       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");
-               }
+       int i, ret;
+       struct iwl_chain_noise_data *data = &priv->chain_noise_data;
 
-               IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
-                               active_chains);
+       data->delta_gain_code[min_average_noise_antenna_i] = 0;
 
-               /* Save for use within RXON, TX, SCAN commands, etc. */
-               priv->valid_antenna = active_chains;
+       for (i = 0; i < NUM_RX_CHAINS; i++) {
+               s32 delta_g = 0;
 
-               /* 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;
-
-               IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
-                               average_noise[0], average_noise[1],
-                               average_noise[2]);
-
-               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];
-                       rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
-                                             sizeof(cmd), &cmd);
-                       if (rc)
-                               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 iwl4965_priv *priv,
-                                           struct iwl4965_notif_statistics *resp)
-{
-       int rc = 0;
-       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;
-
-       data = &(priv->sensitivity_data);
+       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 (!iwl4965_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;
-       }
-
-       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);
-       rc |= 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)
 {
-       struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
+       struct iwl_priv *priv = container_of(work, struct iwl_priv,
                        sensitivity_work);
 
        mutex_lock(&priv->mutex);
@@ -1644,25 +1136,19 @@ 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)
 {
-       struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
+       struct iwl_priv *priv = container_of(work, struct iwl_priv,
                        txpower_work);
 
        /* If a scan happened to start before we got here
@@ -1690,11 +1176,11 @@ static void iwl4965_bg_txpower_work(struct work_struct *work)
 /*
  * Acquire priv->lock before calling this function !
  */
-static void iwl4965_set_wr_ptrs(struct iwl4965_priv *priv, int txq_id, u32 index)
+static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
 {
-       iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
+       iwl_write_direct32(priv, HBUS_TARG_WRPTR,
                             (index & 0xff) | (txq_id << 8));
-       iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(txq_id), index);
+       iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
 }
 
 /**
@@ -1704,7 +1190,7 @@ static void iwl4965_set_wr_ptrs(struct iwl4965_priv *priv, int txq_id, u32 index
  *
  * NOTE:  Acquire priv->lock before calling this function !
  */
-static void iwl4965_tx_queue_set_status(struct iwl4965_priv *priv,
+static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
                                        struct iwl4965_tx_queue *txq,
                                        int tx_fifo_id, int scd_retry)
 {
@@ -1714,7 +1200,7 @@ static void iwl4965_tx_queue_set_status(struct iwl4965_priv *priv,
        int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
 
        /* Set up and activate */
-       iwl4965_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) |
@@ -1738,74 +1224,74 @@ static const u16 default_queue_to_tx_fifo[] = {
        IWL_TX_FIFO_HCCA_2
 };
 
-static inline void iwl4965_txq_ctx_activate(struct iwl4965_priv *priv, int txq_id)
+static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
 {
        set_bit(txq_id, &priv->txq_ctx_active_msk);
 }
 
-static inline void iwl4965_txq_ctx_deactivate(struct iwl4965_priv *priv, int txq_id)
+static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
 {
        clear_bit(txq_id, &priv->txq_ctx_active_msk);
 }
 
-int iwl4965_alive_notify(struct iwl4965_priv *priv)
+int iwl4965_alive_notify(struct iwl_priv *priv)
 {
        u32 a;
        int i = 0;
        unsigned long flags;
-       int rc;
+       int ret;
 
        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*/
-       rc = iwl4965_grab_nic_access(priv);
-       if (rc) {
+#endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
+       ret = iwl_grab_nic_access(priv);
+       if (ret) {
                spin_unlock_irqrestore(&priv->lock, flags);
-               return rc;
+               return ret;
        }
 
        /* Clear 4965's internal Tx Scheduler data base */
-       priv->scd_base_addr = iwl4965_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)
-               iwl4965_write_targ_mem(priv, a, 0);
+               iwl_write_targ_mem(priv, a, 0);
        for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
-               iwl4965_write_targ_mem(priv, a, 0);
-       for (; a < sizeof(u16) * priv->hw_setting.max_txq_num; a += 4)
-               iwl4965_write_targ_mem(priv, a, 0);
+               iwl_write_targ_mem(priv, a, 0);
+       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 */
-       iwl4965_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 */
-       iwl4965_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 */
-               iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(i), 0);
-               iwl4965_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
+               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 */
-               iwl4965_write_targ_mem(priv, priv->scd_base_addr +
+               iwl_write_targ_mem(priv, priv->scd_base_addr +
                                        SCD_CONTEXT_QUEUE_OFFSET(i),
                                        (SCD_WIN_SIZE <<
                                        SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
                                        SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
 
                /* Frame limit */
-               iwl4965_write_targ_mem(priv, priv->scd_base_addr +
+               iwl_write_targ_mem(priv, priv->scd_base_addr +
                                        SCD_CONTEXT_QUEUE_OFFSET(i) +
                                        sizeof(u32),
                                        (SCD_FRAME_LIMIT <<
@@ -1813,11 +1299,11 @@ int iwl4965_alive_notify(struct iwl4965_priv *priv)
                                        SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
 
        }
-       iwl4965_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 */
-       iwl4965_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);
@@ -1829,43 +1315,74 @@ int iwl4965_alive_notify(struct iwl4965_priv *priv)
                iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
        }
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
-       return 0;
+       /* 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 iwl4965_priv *priv)
+int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
 {
-       /* 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)
-               return -1;
 
-       memset(priv->hw_setting.shared_virt, 0, sizeof(struct iwl4965_shared));
+       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, IWL4965_MAX_NUM_QUEUES);
+               return -EINVAL;
+       }
 
-       priv->hw_setting.max_txq_num = iwl4965_param_queues_num;
-       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;
-       if (iwl4965_param_amsdu_size_8K)
-               priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE_8K;
+       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_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
 
        return 0;
 }
@@ -1875,12 +1392,12 @@ int iwl4965_hw_set_hw_setting(struct iwl4965_priv *priv)
  *
  * Destroy all TX DMA queues and structures
  */
-void iwl4965_hw_txq_ctx_free(struct iwl4965_priv *priv)
+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 */
@@ -1893,7 +1410,7 @@ void iwl4965_hw_txq_ctx_free(struct iwl4965_priv *priv)
  * Does NOT advance any TFD circular buffer read/write indexes
  * Does NOT free the TFD itself (which is within circular buffer)
  */
-int iwl4965_hw_txq_free_tfd(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
+int iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
 {
        struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
        struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
@@ -1946,7 +1463,7 @@ int iwl4965_hw_txq_free_tfd(struct iwl4965_priv *priv, struct iwl4965_tx_queue *
        return 0;
 }
 
-int iwl4965_hw_reg_set_txpower(struct iwl4965_priv *priv, s8 power)
+int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
 {
        IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
        return -EINVAL;
@@ -2001,13 +1518,13 @@ static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
        return comp;
 }
 
-static const struct iwl4965_channel_info *
-iwl4965_get_channel_txpower_info(struct iwl4965_priv *priv,
+static const struct iwl_channel_info *
+iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
                                 enum ieee80211_band band, u16 channel)
 {
-       const struct iwl4965_channel_info *ch_info;
+       const struct iwl_channel_info *ch_info;
 
-       ch_info = iwl4965_get_channel_info(priv, band, channel);
+       ch_info = iwl_get_channel_info(priv, band, channel);
 
        if (!is_channel_valid(ch_info))
                return NULL;
@@ -2041,7 +1558,7 @@ static s32 iwl4965_get_tx_atten_grp(u16 channel)
        return -1;
 }
 
-static u32 iwl4965_get_sub_band(const struct iwl4965_priv *priv, u32 channel)
+static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
 {
        s32 b = -1;
 
@@ -2077,7 +1594,7 @@ static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
  * differences in channel frequencies, which is proportional to differences
  * in channel number.
  */
-static int iwl4965_interpolate_chan(struct iwl4965_priv *priv, u32 channel,
+static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
                                    struct iwl4965_eeprom_calib_ch_info *chan_info)
 {
        s32 s = -1;
@@ -2410,7 +1927,7 @@ static const struct gain_entry gain_table[2][108] = {
         }
 };
 
-static int iwl4965_fill_txpower_tbl(struct iwl4965_priv *priv, u8 band, u16 channel,
+static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
                                    u8 is_fat, u8 ctrl_chan_high,
                                    struct iwl4965_tx_power_db *tx_power_tbl)
 {
@@ -2424,7 +1941,7 @@ static int iwl4965_fill_txpower_tbl(struct iwl4965_priv *priv, u8 band, u16 chan
        s32 txatten_grp = CALIB_CH_GROUP_MAX;
        int i;
        int c;
-       const struct iwl4965_channel_info *ch_info = NULL;
+       const struct iwl_channel_info *ch_info = NULL;
        struct iwl4965_eeprom_calib_ch_info ch_eeprom_info;
        const struct iwl4965_eeprom_calib_measure *measurement;
        s16 voltage;
@@ -2667,10 +2184,10 @@ static int iwl4965_fill_txpower_tbl(struct iwl4965_priv *priv, u8 band, u16 chan
  * Uses the active RXON for channel, band, and characteristics (fat, high)
  * The power limit is taken from priv->user_txpower_limit.
  */
-int iwl4965_hw_reg_send_txpower(struct iwl4965_priv *priv)
+int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
 {
        struct iwl4965_txpowertable_cmd cmd = { 0 };
-       int rc = 0;
+       int ret;
        u8 band = 0;
        u8 is_fat = 0;
        u8 ctrl_chan_high = 0;
@@ -2694,28 +2211,70 @@ int iwl4965_hw_reg_send_txpower(struct iwl4965_priv *priv)
        cmd.band = band;
        cmd.channel = priv->active_rxon.channel;
 
-       rc = iwl4965_fill_txpower_tbl(priv, band,
+       ret = iwl4965_fill_txpower_tbl(priv, band,
                                le16_to_cpu(priv->active_rxon.channel),
                                is_fat, ctrl_chan_high, &cmd.tx_power);
-       if (rc)
-               return rc;
+       if (ret)
+               goto out;
 
-       rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
-       return rc;
+       ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
+
+out:
+       return ret;
 }
 
-int iwl4965_hw_channel_switch(struct iwl4965_priv *priv, u16 channel)
+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;
        u8 band = 0;
        u8 is_fat = 0;
        u8 ctrl_chan_high = 0;
        struct iwl4965_channel_switch_cmd cmd = { 0 };
-       const struct iwl4965_channel_info *ch_info;
+       const struct iwl_channel_info *ch_info;
 
        band = priv->band == IEEE80211_BAND_2GHZ;
 
-       ch_info = iwl4965_get_channel_info(priv, priv->band, channel);
+       ch_info = iwl_get_channel_info(priv, priv->band, channel);
 
        is_fat = is_fat_channel(priv->staging_rxon.flags);
 
@@ -2741,15 +2300,15 @@ int iwl4965_hw_channel_switch(struct iwl4965_priv *priv, u16 channel)
                return rc;
        }
 
-       rc = iwl4965_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
+       rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
        return rc;
 }
 
 #define RTS_HCCA_RETRY_LIMIT           3
 #define RTS_DFAULT_RETRY_LIMIT         60
 
-void iwl4965_hw_build_tx_cmd_rate(struct iwl4965_priv *priv,
-                             struct iwl4965_cmd *cmd,
+void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
+                             struct iwl_cmd *cmd,
                              struct ieee80211_tx_control *ctrl,
                              struct ieee80211_hdr *hdr, int sta_id,
                              int is_hcca)
@@ -2815,19 +2374,18 @@ void iwl4965_hw_build_tx_cmd_rate(struct iwl4965_priv *priv,
        tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
 }
 
-int iwl4965_hw_get_rx_read(struct iwl4965_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 iwl4965_priv *priv)
+int iwl4965_hw_get_temperature(struct iwl_priv *priv)
 {
        return priv->temperature;
 }
 
-unsigned int iwl4965_hw_get_beacon_cmd(struct iwl4965_priv *priv,
+unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
                          struct iwl4965_frame *frame, u8 rate)
 {
        struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
@@ -2836,7 +2394,7 @@ unsigned int iwl4965_hw_get_beacon_cmd(struct iwl4965_priv *priv,
        tx_beacon_cmd = &frame->u.beacon;
        memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
 
-       tx_beacon_cmd->tx.sta_id = IWL4965_BROADCAST_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,
@@ -2866,35 +2424,35 @@ unsigned int iwl4965_hw_get_beacon_cmd(struct iwl4965_priv *priv,
  * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  * channels supported in hardware.
  */
-int iwl4965_hw_tx_queue_init(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
+int iwl4965_hw_tx_queue_init(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
 {
        int rc;
        unsigned long flags;
        int txq_id = txq->q.id;
 
        spin_lock_irqsave(&priv->lock, flags);
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc) {
                spin_unlock_irqrestore(&priv->lock, flags);
                return rc;
        }
 
        /* Circular buffer (TFD queue in DRAM) physical base address */
-       iwl4965_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
+       iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
                             txq->q.dma_addr >> 8);
 
        /* Enable DMA channel, using same id as for TFD queue */
-       iwl4965_write_direct32(
+       iwl_write_direct32(
                priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
                IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
                IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
 }
 
-int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl4965_priv *priv, void *ptr,
+int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
                                 dma_addr_t addr, u16 len)
 {
        int index, is_odd;
@@ -2928,7 +2486,7 @@ int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl4965_priv *priv, void *ptr,
        return 0;
 }
 
-static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv)
+static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
 {
        u16 hw_version = priv->eeprom.board_revision_4965;
 
@@ -2944,17 +2502,15 @@ static void iwl4965_hw_card_show_info(struct iwl4965_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 iwl4965_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;
 
@@ -2967,8 +2523,6 @@ int iwl4965_tx_queue_update_wr_ptr(struct iwl4965_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;
 }
 
 /**
@@ -2977,7 +2531,7 @@ int iwl4965_tx_queue_update_wr_ptr(struct iwl4965_priv *priv,
  * Selects how many and which Rx receivers/antennas/chains to use.
  * This should not be used for scan command ... it puts data in wrong place.
  */
-void iwl4965_set_rxon_chain(struct iwl4965_priv *priv)
+void iwl4965_set_rxon_chain(struct iwl_priv *priv)
 {
        u8 is_single = is_single_stream(priv);
        u8 idle_state, rx_state;
@@ -2987,7 +2541,7 @@ void iwl4965_set_rxon_chain(struct iwl4965_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);
@@ -3030,7 +2584,7 @@ static s32 sign_extend(u32 oper, int index)
  *
  * A return of <0 indicates bogus data in the statistics
  */
-int iwl4965_get_temperature(const struct iwl4965_priv *priv)
+int iwl4965_get_temperature(const struct iwl_priv *priv)
 {
        s32 temperature;
        s32 vt;
@@ -3098,7 +2652,7 @@ int iwl4965_get_temperature(const struct iwl4965_priv *priv)
  * Assumes caller will replace priv->last_temperature once calibration
  * executed.
  */
-static int iwl4965_is_temp_calib_needed(struct iwl4965_priv *priv)
+static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
 {
        int temp_diff;
 
@@ -3131,7 +2685,7 @@ static int iwl4965_is_temp_calib_needed(struct iwl4965_priv *priv)
 /* Calculate noise level, based on measurements during network silence just
  *   before arriving beacon.  This measurement can be done only if we know
  *   exactly when to expect beacons, therefore only when we're associated. */
-static void iwl4965_rx_calc_noise(struct iwl4965_priv *priv)
+static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
 {
        struct statistics_rx_non_phy *rx_info
                                = &(priv->statistics.rx.general);
@@ -3168,7 +2722,7 @@ static void iwl4965_rx_calc_noise(struct iwl4965_priv *priv)
                        priv->last_rx_noise);
 }
 
-void iwl4965_hw_rx_statistics(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
+void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
 {
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
        int change;
@@ -3197,11 +2751,13 @@ void iwl4965_hw_rx_statistics(struct iwl4965_priv *priv, struct iwl4965_rx_mem_b
        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
        }
 
+       iwl_leds_background(priv);
+
        /* If the hardware hasn't reported a change in
         * temperature then don't bother computing a
         * calibrated temperature value */
@@ -3232,7 +2788,7 @@ void iwl4965_hw_rx_statistics(struct iwl4965_priv *priv, struct iwl4965_rx_mem_b
                queue_work(priv->workqueue, &priv->txpower_work);
 }
 
-static void iwl4965_add_radiotap(struct iwl4965_priv *priv,
+static void iwl4965_add_radiotap(struct iwl_priv *priv,
                                 struct sk_buff *skb,
                                 struct iwl4965_rx_phy_res *rx_start,
                                 struct ieee80211_rx_status *stats,
@@ -3242,6 +2798,7 @@ static void iwl4965_add_radiotap(struct iwl4965_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;
@@ -3326,8 +2883,8 @@ static void iwl4965_add_radiotap(struct iwl4965_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)
@@ -3336,7 +2893,74 @@ static void iwl4965_add_radiotap(struct iwl4965_priv *priv,
        stats->flag |= RX_FLAG_RADIOTAP;
 }
 
-static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
+static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
+{
+       /* 0 - mgmt, 1 - cnt, 2 - data */
+       int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
+       priv->rx_stats[idx].cnt++;
+       priv->rx_stats[idx].bytes += len;
+}
+
+static u32 iwl4965_translate_rx_status(u32 decrypt_in)
+{
+       u32 decrypt_out = 0;
+
+       if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
+                                       RX_RES_STATUS_STATION_FOUND)
+               decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
+                               RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
+
+       decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
+
+       /* packet was not encrypted */
+       if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
+                                       RX_RES_STATUS_SEC_TYPE_NONE)
+               return decrypt_out;
+
+       /* packet was encrypted with unknown alg */
+       if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
+                                       RX_RES_STATUS_SEC_TYPE_ERR)
+               return decrypt_out;
+
+       /* decryption was not done in HW */
+       if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
+                                       RX_MPDU_RES_STATUS_DEC_DONE_MSK)
+               return decrypt_out;
+
+       switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
+
+       case RX_RES_STATUS_SEC_TYPE_CCMP:
+               /* alg is CCM: check MIC only */
+               if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
+                       /* Bad MIC */
+                       decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
+               else
+                       decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
+
+               break;
+
+       case RX_RES_STATUS_SEC_TYPE_TKIP:
+               if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
+                       /* Bad TTAK */
+                       decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
+                       break;
+               }
+               /* fall through if TTAK OK */
+       default:
+               if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
+                       decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
+               else
+                       decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
+               break;
+       };
+
+       IWL_DEBUG_RX("decrypt_in:0x%x  decrypt_out = 0x%x\n",
+                                       decrypt_in, decrypt_out);
+
+       return decrypt_out;
+}
+
+static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
                                       int include_phy,
                                       struct iwl4965_rx_mem_buffer *rxb,
                                       struct ieee80211_rx_status *stats)
@@ -3349,6 +2973,7 @@ static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
        __le32 *rx_end;
        unsigned int skblen;
        u32 ampdu_status;
+       u32 ampdu_status_legacy;
 
        if (!include_phy && priv->last_phy_res[0])
                rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
@@ -3377,7 +3002,7 @@ static void iwl4965_handle_data_packet(struct iwl4965_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;
        }
@@ -3385,6 +3010,12 @@ static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
        ampdu_status = le32_to_cpu(*rx_end);
        skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
 
+       if (!include_phy) {
+               /* New status scheme, need to translate */
+               ampdu_status_legacy = ampdu_status;
+               ampdu_status = iwl4965_translate_rx_status(ampdu_status);
+       }
+
        /* start from MAC */
        skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
        skb_put(rxb->skb, len); /* end where data ends */
@@ -3399,19 +3030,16 @@ static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
        stats->flag = 0;
        hdr = (struct ieee80211_hdr *)rxb->skb->data;
 
-       if (iwl4965_param_hwcrypto)
+       if (!priv->cfg->mod_params->sw_crypto)
                iwl4965_set_decrypted_flag(priv, rxb->skb, ampdu_status, stats);
 
        if (priv->add_radiotap)
                iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
 
+       iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
        ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
        priv->alloc_rxb_skb--;
        rxb->skb = NULL;
-#ifdef LED
-       priv->led_packets += len;
-       iwl4965_setup_activity_timer(priv);
-#endif
 }
 
 /* Calc max signal level (dBm) among 3 possible receivers */
@@ -3450,78 +3078,8 @@ static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
 
 #ifdef CONFIG_IWL4965_HT
 
-/* Parsed Information Elements */
-struct ieee802_11_elems {
-       u8 *ds_params;
-       u8 ds_params_len;
-       u8 *tim;
-       u8 tim_len;
-       u8 *ibss_params;
-       u8 ibss_params_len;
-       u8 *erp_info;
-       u8 erp_info_len;
-       u8 *ht_cap_param;
-       u8 ht_cap_param_len;
-       u8 *ht_extra_param;
-       u8 ht_extra_param_len;
-};
-
-static int parse_elems(u8 *start, size_t len, struct ieee802_11_elems *elems)
-{
-       size_t left = len;
-       u8 *pos = start;
-       int unknown = 0;
-
-       memset(elems, 0, sizeof(*elems));
-
-       while (left >= 2) {
-               u8 id, elen;
-
-               id = *pos++;
-               elen = *pos++;
-               left -= 2;
-
-               if (elen > left)
-                       return -1;
-
-               switch (id) {
-               case WLAN_EID_DS_PARAMS:
-                       elems->ds_params = pos;
-                       elems->ds_params_len = elen;
-                       break;
-               case WLAN_EID_TIM:
-                       elems->tim = pos;
-                       elems->tim_len = elen;
-                       break;
-               case WLAN_EID_IBSS_PARAMS:
-                       elems->ibss_params = pos;
-                       elems->ibss_params_len = elen;
-                       break;
-               case WLAN_EID_ERP_INFO:
-                       elems->erp_info = pos;
-                       elems->erp_info_len = elen;
-                       break;
-               case WLAN_EID_HT_CAPABILITY:
-                       elems->ht_cap_param = pos;
-                       elems->ht_cap_param_len = elen;
-                       break;
-               case WLAN_EID_HT_EXTRA_INFO:
-                       elems->ht_extra_param = pos;
-                       elems->ht_extra_param_len = elen;
-                       break;
-               default:
-                       unknown++;
-                       break;
-               }
-
-               left -= elen;
-               pos += elen;
-       }
-
-       return 0;
-}
-
-void iwl4965_init_ht_hw_capab(struct ieee80211_ht_info *ht_info,
+void iwl4965_init_ht_hw_capab(struct iwl_priv *priv,
+                             struct ieee80211_ht_info *ht_info,
                              enum ieee80211_band band)
 {
        ht_info->cap = 0;
@@ -3538,10 +3096,9 @@ void iwl4965_init_ht_hw_capab(struct ieee80211_ht_info *ht_info,
        ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
        ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
                             (IWL_MIMO_PS_NONE << 2));
-       if (iwl4965_param_amsdu_size_8K) {
-               printk(KERN_DEBUG "iwl4965 in A-MSDU 8K support mode\n");
+
+       if (priv->cfg->mod_params->amsdu_size_8K)
                ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
-       }
 
        ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
        ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
@@ -3551,7 +3108,7 @@ void iwl4965_init_ht_hw_capab(struct ieee80211_ht_info *ht_info,
 }
 #endif /* CONFIG_IWL4965_HT */
 
-static void iwl4965_sta_modify_ps_wake(struct iwl4965_priv *priv, int sta_id)
+static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
 {
        unsigned long flags;
 
@@ -3565,7 +3122,7 @@ static void iwl4965_sta_modify_ps_wake(struct iwl4965_priv *priv, int sta_id)
        iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
 }
 
-static void iwl4965_update_ps_mode(struct iwl4965_priv *priv, u16 ps_bit, u8 *addr)
+static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
 {
        /* FIXME: need locking over ps_status ??? */
        u8 sta_id = iwl4965_hw_find_station(priv, addr);
@@ -3582,7 +3139,7 @@ static void iwl4965_update_ps_mode(struct iwl4965_priv *priv, u16 ps_bit, u8 *ad
                }
        }
 }
-#ifdef CONFIG_IWL4965_DEBUG
+#ifdef CONFIG_IWLWIFI_DEBUG
 
 /**
  * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
@@ -3594,7 +3151,7 @@ static void iwl4965_update_ps_mode(struct iwl4965_priv *priv, u16 ps_bit, u8 *ad
  * TODO:  This was originally written for 3945, need to audit for
  *        proper operation with 4965.
  */
-static void iwl4965_dbg_report_frame(struct iwl4965_priv *priv,
+static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
                      struct iwl4965_rx_packet *pkt,
                      struct ieee80211_hdr *header, int group100)
 {
@@ -3622,7 +3179,7 @@ static void iwl4965_dbg_report_frame(struct iwl4965_priv *priv,
        struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
        u8 *data = IWL_RX_DATA(pkt);
 
-       if (likely(!(iwl4965_debug_level & IWL_DL_RX)))
+       if (likely(!(iwl_debug_level & IWL_DL_RX)))
                return;
 
        /* MAC header */
@@ -3725,10 +3282,10 @@ static void iwl4965_dbg_report_frame(struct iwl4965_priv *priv,
                }
        }
        if (print_dump)
-               iwl4965_print_hex_dump(IWL_DL_RX, data, length);
+               iwl_print_hex_dump(IWL_DL_RX, data, length);
 }
 #else
-static inline void iwl4965_dbg_report_frame(struct iwl4965_priv *priv,
+static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
                                            struct iwl4965_rx_packet *pkt,
                                            struct ieee80211_hdr *header,
                                            int group100)
@@ -3737,11 +3294,10 @@ static inline void iwl4965_dbg_report_frame(struct iwl4965_priv *priv,
 #endif
 
 
-#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
 
-/* Called for REPLY_4965_RX (legacy ABG frames), or
+/* Called for REPLY_RX (legacy ABG frames), or
  * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
-static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
+static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
                                struct iwl4965_rx_mem_buffer *rxb)
 {
        struct ieee80211_hdr *header;
@@ -3750,7 +3306,7 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
        /* Use phy data (Rx signal strength, etc.) contained within
         *   this rx packet for legacy frames,
         *   or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
-       int include_phy = (pkt->hdr.cmd == REPLY_4965_RX);
+       int include_phy = (pkt->hdr.cmd == REPLY_RX);
        struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
                (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
                (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
@@ -3760,12 +3316,12 @@ static void iwl4965_rx_reply_rx(struct iwl4965_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;
 
@@ -3773,9 +3329,8 @@ static void iwl4965_rx_reply_rx(struct iwl4965_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;
        }
 
@@ -3826,7 +3381,7 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
         *   which are gathered only when associated, and indicate noise
         *   only for the associated network channel ...
         * Ignore these noise values while scanning (other channels) */
-       if (iwl4965_is_associated(priv) &&
+       if (iwl_is_associated(priv) &&
            !test_bit(STATUS_SCANNING, &priv->status)) {
                rx_status.noise = priv->last_rx_noise;
                rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
@@ -3837,7 +3392,7 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
        }
 
        /* Reset beacon noise level if not associated. */
-       if (!iwl4965_is_associated(priv))
+       if (!iwl_is_associated(priv))
                priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
 
        /* Set "1" to report good data frames in groups of 100 */
@@ -3846,7 +3401,7 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
 
        IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
                              rx_status.ssi, rx_status.noise, rx_status.signal,
-                             rx_status.mactime);
+                             (unsigned long long)rx_status.mactime);
 
        network_packet = iwl4965_is_network_packet(priv, header);
        if (network_packet) {
@@ -3858,101 +3413,9 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
        fc = le16_to_cpu(header->frame_control);
        switch (fc & IEEE80211_FCTL_FTYPE) {
        case IEEE80211_FTYPE_MGMT:
-
                if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
                        iwl4965_update_ps_mode(priv, fc  & IEEE80211_FCTL_PM,
                                                header->addr2);
-               switch (fc & IEEE80211_FCTL_STYPE) {
-               case IEEE80211_STYPE_PROBE_RESP:
-               case IEEE80211_STYPE_BEACON:
-                       if ((priv->iw_mode == IEEE80211_IF_TYPE_STA &&
-                            !compare_ether_addr(header->addr2, priv->bssid)) ||
-                           (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
-                            !compare_ether_addr(header->addr3, priv->bssid))) {
-                               struct ieee80211_mgmt *mgmt =
-                                       (struct ieee80211_mgmt *)header;
-                               u64 timestamp =
-                                       le64_to_cpu(mgmt->u.beacon.timestamp);
-
-                               priv->timestamp0 = timestamp & 0xFFFFFFFF;
-                               priv->timestamp1 =
-                                       (timestamp >> 32) & 0xFFFFFFFF;
-                               priv->beacon_int = le16_to_cpu(
-                                   mgmt->u.beacon.beacon_int);
-                               if (priv->call_post_assoc_from_beacon &&
-                                   (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
-                                       priv->call_post_assoc_from_beacon = 0;
-                                       queue_work(priv->workqueue,
-                                           &priv->post_associate.work);
-                               }
-                       }
-                       break;
-
-               case IEEE80211_STYPE_ACTION:
-                       break;
-
-                       /*
-                        * TODO: Use the new callback function from
-                        * mac80211 instead of sniffing these packets.
-                        */
-               case IEEE80211_STYPE_ASSOC_RESP:
-               case IEEE80211_STYPE_REASSOC_RESP:
-                       if (network_packet) {
-#ifdef CONFIG_IWL4965_HT
-                               u8 *pos = NULL;
-                               struct ieee802_11_elems elems;
-#endif                         /*CONFIG_IWL4965_HT */
-                               struct ieee80211_mgmt *mgnt =
-                                       (struct ieee80211_mgmt *)header;
-
-                               /* We have just associated, give some
-                                * time for the 4-way handshake if
-                                * any. Don't start scan too early. */
-                               priv->next_scan_jiffies = jiffies +
-                                       IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
-
-                               priv->assoc_id = (~((1 << 15) | (1 << 14))
-                                       & le16_to_cpu(mgnt->u.assoc_resp.aid));
-                               priv->assoc_capability =
-                                       le16_to_cpu(
-                                               mgnt->u.assoc_resp.capab_info);
-#ifdef CONFIG_IWL4965_HT
-                               pos = mgnt->u.assoc_resp.variable;
-                               if (!parse_elems(pos,
-                                                len - (pos - (u8 *) mgnt),
-                                                &elems)) {
-                                       if (elems.ht_extra_param &&
-                                           elems.ht_cap_param)
-                                               break;
-                               }
-#endif                         /*CONFIG_IWL4965_HT */
-                               /* assoc_id is 0 no association */
-                               if (!priv->assoc_id)
-                                       break;
-                               if (priv->beacon_int)
-                                       queue_work(priv->workqueue,
-                                           &priv->post_associate.work);
-                               else
-                                       priv->call_post_assoc_from_beacon = 1;
-                       }
-
-                       break;
-
-               case IEEE80211_STYPE_PROBE_REQ:
-                       if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
-                           !iwl4965_is_associated(priv)) {
-                               DECLARE_MAC_BUF(mac1);
-                               DECLARE_MAC_BUF(mac2);
-                               DECLARE_MAC_BUF(mac3);
-
-                               IWL_DEBUG_DROP("Dropping (non network): "
-                                              "%s, %s, %s\n",
-                                              print_mac(mac1, header->addr1),
-                                              print_mac(mac2, header->addr2),
-                                              print_mac(mac3, header->addr3));
-                               return;
-                       }
-               }
                iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
                break;
 
@@ -4003,7 +3466,7 @@ static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
 
 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
-static void iwl4965_rx_reply_rx_phy(struct iwl4965_priv *priv,
+static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
                                    struct iwl4965_rx_mem_buffer *rxb)
 {
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
@@ -4011,12 +3474,11 @@ static void iwl4965_rx_reply_rx_phy(struct iwl4965_priv *priv,
        memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
               sizeof(struct iwl4965_rx_phy_res));
 }
-
-static void iwl4965_rx_missed_beacon_notif(struct iwl4965_priv *priv,
+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;
 
@@ -4027,19 +3489,17 @@ static void iwl4965_rx_missed_beacon_notif(struct iwl4965_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
 
 /**
  * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
  */
-static void iwl4965_sta_modify_enable_tid_tx(struct iwl4965_priv *priv,
+static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
                                         int sta_id, int tid)
 {
        unsigned long flags;
@@ -4060,7 +3520,7 @@ static void iwl4965_sta_modify_enable_tid_tx(struct iwl4965_priv *priv,
  * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  * ACK vs. not.  This gets sent to mac80211, then to rate scaling algo.
  */
-static int iwl4965_tx_status_reply_compressed_ba(struct iwl4965_priv *priv,
+static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
                                                 struct iwl4965_ht_agg *agg,
                                                 struct iwl4965_compressed_ba_resp*
                                                 ba_resp)
@@ -4125,13 +3585,13 @@ static int iwl4965_tx_status_reply_compressed_ba(struct iwl4965_priv *priv,
 /**
  * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  */
-static void iwl4965_tx_queue_stop_scheduler(struct iwl4965_priv *priv,
+static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
                                            u16 txq_id)
 {
        /* Simply stop the queue, but don't change any configuration;
         * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
-       iwl4965_write_prph(priv,
-               KDR_SCD_QUEUE_STATUS_BITS(txq_id),
+       iwl_write_prph(priv,
+               IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
                (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
                (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
 }
@@ -4140,7 +3600,7 @@ static void iwl4965_tx_queue_stop_scheduler(struct iwl4965_priv *priv,
  * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
  * priv->lock must be held by the caller
  */
-static int iwl4965_tx_queue_agg_disable(struct iwl4965_priv *priv, u16 txq_id,
+static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
                                        u16 ssn_idx, u8 tx_fifo)
 {
        int ret = 0;
@@ -4151,29 +3611,29 @@ static int iwl4965_tx_queue_agg_disable(struct iwl4965_priv *priv, u16 txq_id,
                return -EINVAL;
        }
 
-       ret = iwl4965_grab_nic_access(priv);
+       ret = iwl_grab_nic_access(priv);
        if (ret)
                return ret;
 
        iwl4965_tx_queue_stop_scheduler(priv, txq_id);
 
-       iwl4965_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);
 
-       iwl4965_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);
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
 
        return 0;
 }
 
-int iwl4965_check_empty_hw_queue(struct iwl4965_priv *priv, int sta_id,
+int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
                                         u8 tid, int txq_id)
 {
        struct iwl4965_queue *q = &priv->txq[txq_id].q;
@@ -4223,7 +3683,7 @@ static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
  * Handles block-acknowledge notification from device, which reports success
  * of frames sent via aggregation.
  */
-static void iwl4965_rx_reply_compressed_ba(struct iwl4965_priv *priv,
+static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
                                           struct iwl4965_rx_mem_buffer *rxb)
 {
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
@@ -4240,7 +3700,7 @@ static void iwl4965_rx_reply_compressed_ba(struct iwl4965_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;
        }
@@ -4262,7 +3722,7 @@ static void iwl4965_rx_reply_compressed_ba(struct iwl4965_priv *priv,
                           "%d, scd_ssn = %d\n",
                           ba_resp->tid,
                           ba_resp->seq_ctl,
-                          (unsigned long long)ba_resp->bitmap,
+                          (unsigned long long)le64_to_cpu(ba_resp->bitmap),
                           ba_resp->scd_flow,
                           ba_resp->scd_ssn);
        IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
@@ -4291,7 +3751,7 @@ static void iwl4965_rx_reply_compressed_ba(struct iwl4965_priv *priv,
 /**
  * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  */
-static int iwl4965_tx_queue_set_q2ratid(struct iwl4965_priv *priv, u16 ra_tid,
+static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
                                        u16 txq_id)
 {
        u32 tbl_dw_addr;
@@ -4303,14 +3763,14 @@ static int iwl4965_tx_queue_set_q2ratid(struct iwl4965_priv *priv, u16 ra_tid,
        tbl_dw_addr = priv->scd_base_addr +
                        SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
 
-       tbl_dw = iwl4965_read_targ_mem(priv, tbl_dw_addr);
+       tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
 
        if (txq_id & 0x1)
                tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
        else
                tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
 
-       iwl4965_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
+       iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
 
        return 0;
 }
@@ -4322,7 +3782,7 @@ static int iwl4965_tx_queue_set_q2ratid(struct iwl4965_priv *priv, u16 ra_tid,
  * NOTE:  txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
  *        i.e. it must be one of the higher queues used for aggregation
  */
-static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
+static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
                                       int tx_fifo, int sta_id, int tid,
                                       u16 ssn_idx)
 {
@@ -4340,7 +3800,7 @@ static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
        iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
 
        spin_lock_irqsave(&priv->lock, flags);
-       rc = iwl4965_grab_nic_access(priv);
+       rc = iwl_grab_nic_access(priv);
        if (rc) {
                spin_unlock_irqrestore(&priv->lock, flags);
                return rc;
@@ -4353,7 +3813,7 @@ static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
        iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
 
        /* Set this queue as a chain-building queue */
-       iwl4965_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) */
@@ -4362,22 +3822,22 @@ static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
        iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
 
        /* Set up Tx window size and frame limit for this queue */
-       iwl4965_write_targ_mem(priv,
+       iwl_write_targ_mem(priv,
                        priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
                        (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
                        SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
 
-       iwl4965_write_targ_mem(priv, priv->scd_base_addr +
+       iwl_write_targ_mem(priv, priv->scd_base_addr +
                        SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
                        (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
                        & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
 
-       iwl4965_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);
 
-       iwl4965_release_nic_access(priv);
+       iwl_release_nic_access(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
@@ -4399,10 +3859,10 @@ static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  *       which requires station table entry to exist).
  */
-void iwl4965_add_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
+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;
@@ -4436,21 +3896,21 @@ void iwl4965_add_station(struct iwl4965_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 : IWL4965_BROADCAST_ID;
+       link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
 
-       iwl4965_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD, sizeof(link_cmd),
-                        &link_cmd);
+       iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
+                              sizeof(link_cmd), &link_cmd, NULL);
 }
 
 #ifdef CONFIG_IWL4965_HT
 
-static u8 iwl4965_is_channel_extension(struct iwl4965_priv *priv,
+static u8 iwl4965_is_channel_extension(struct iwl_priv *priv,
                                       enum ieee80211_band band,
                                       u16 channel, u8 extension_chan_offset)
 {
-       const struct iwl4965_channel_info *ch_info;
+       const struct iwl_channel_info *ch_info;
 
-       ch_info = iwl4965_get_channel_info(priv, band, channel);
+       ch_info = iwl_get_channel_info(priv, band, channel);
        if (!is_channel_valid(ch_info))
                return 0;
 
@@ -4464,7 +3924,7 @@ static u8 iwl4965_is_channel_extension(struct iwl4965_priv *priv,
        return 0;
 }
 
-static u8 iwl4965_is_fat_tx_allowed(struct iwl4965_priv *priv,
+static u8 iwl4965_is_fat_tx_allowed(struct iwl_priv *priv,
                                struct ieee80211_ht_info *sta_ht_inf)
 {
        struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
@@ -4485,7 +3945,7 @@ static u8 iwl4965_is_fat_tx_allowed(struct iwl4965_priv *priv,
                                         iwl_ht_conf->extension_chan_offset));
 }
 
-void iwl4965_set_rxon_ht(struct iwl4965_priv *priv, struct iwl_ht_info *ht_info)
+void iwl4965_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
 {
        struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
        u32 val;
@@ -4539,7 +3999,7 @@ void iwl4965_set_rxon_ht(struct iwl4965_priv *priv, struct iwl_ht_info *ht_info)
        return;
 }
 
-void iwl4965_set_ht_add_station(struct iwl4965_priv *priv, u8 index,
+void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
                                struct ieee80211_ht_info *sta_ht_inf)
 {
        __le32 sta_flags;
@@ -4584,7 +4044,7 @@ void iwl4965_set_ht_add_station(struct iwl4965_priv *priv, u8 index,
        return;
 }
 
-static void iwl4965_sta_modify_add_ba_tid(struct iwl4965_priv *priv,
+static void iwl4965_sta_modify_add_ba_tid(struct iwl_priv *priv,
                                          int sta_id, int tid, u16 ssn)
 {
        unsigned long flags;
@@ -4600,7 +4060,7 @@ static void iwl4965_sta_modify_add_ba_tid(struct iwl4965_priv *priv,
        iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
 }
 
-static void iwl4965_sta_modify_del_ba_tid(struct iwl4965_priv *priv,
+static void iwl4965_sta_modify_del_ba_tid(struct iwl_priv *priv,
                                          int sta_id, int tid)
 {
        unsigned long flags;
@@ -4621,11 +4081,11 @@ static void iwl4965_sta_modify_del_ba_tid(struct iwl4965_priv *priv,
  * Should never return anything < 7, because they should already
  * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
  */
-static int iwl4965_txq_ctx_activate_free(struct iwl4965_priv *priv)
+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;
@@ -4634,7 +4094,7 @@ static int iwl4965_txq_ctx_activate_free(struct iwl4965_priv *priv)
 static int iwl4965_mac_ht_tx_agg_start(struct ieee80211_hw *hw, const u8 *da,
                                       u16 tid, u16 *start_seq_num)
 {
-       struct iwl4965_priv *priv = hw->priv;
+       struct iwl_priv *priv = hw->priv;
        int sta_id;
        int tx_fifo;
        int txq_id;
@@ -4694,7 +4154,7 @@ static int iwl4965_mac_ht_tx_agg_stop(struct ieee80211_hw *hw, const u8 *da,
                                      u16 tid)
 {
 
-       struct iwl4965_priv *priv = hw->priv;
+       struct iwl_priv *priv = hw->priv;
        int tx_fifo_id, txq_id, sta_id, ssn = -1;
        struct iwl4965_tid_data *tid_data;
        int ret, write_ptr, read_ptr;
@@ -4755,7 +4215,7 @@ int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
                             enum ieee80211_ampdu_mlme_action action,
                             const u8 *addr, u16 tid, u16 *ssn)
 {
-       struct iwl4965_priv *priv = hw->priv;
+       struct iwl_priv *priv = hw->priv;
        int sta_id;
        DECLARE_MAC_BUF(mac);
 
@@ -4788,10 +4248,10 @@ int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
 #endif /* CONFIG_IWL4965_HT */
 
 /* Set up 4965-specific Rx frame reply handlers */
-void iwl4965_hw_rx_handler_setup(struct iwl4965_priv *priv)
+void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
 {
        /* Legacy Rx frames */
-       priv->rx_handlers[REPLY_4965_RX] = iwl4965_rx_reply_rx;
+       priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
 
        /* High-throughput (HT) Rx frames */
        priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
@@ -4805,11 +4265,10 @@ void iwl4965_hw_rx_handler_setup(struct iwl4965_priv *priv)
 #endif /* CONFIG_IWL4965_HT */
 }
 
-void iwl4965_hw_setup_deferred_work(struct iwl4965_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);
@@ -4817,54 +4276,74 @@ void iwl4965_hw_setup_deferred_work(struct iwl4965_priv *priv)
        priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
 }
 
-void iwl4965_hw_cancel_deferred_work(struct iwl4965_priv *priv)
+void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
 {
        del_timer_sync(&priv->statistics_periodic);
 
        cancel_delayed_work(&priv->init_alive_start);
 }
 
-static 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,
+
+static struct iwl_hcmd_ops iwl4965_hcmd = {
+       .rxon_assoc = iwl4965_send_rxon_assoc,
 };
 
-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}
+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
 };
 
-/*
- * The device's EEPROM semaphore prevents conflicts between driver and uCode
- * when accessing the EEPROM; each access is a series of pulses to/from the
- * EEPROM chip, not a single event, so even reads could conflict if they
- * weren't arbitrated by the semaphore.
- */
-int iwl4965_eeprom_acquire_semaphore(struct iwl4965_priv *priv)
-{
-       u16 count;
-       int rc;
+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,
+               .release_semaphore = iwlcore_eeprom_release_semaphore,
+       },
+       .radio_kill_sw = iwl4965_radio_kill_sw,
+};
 
-       for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
-               /* Request semaphore */
-               iwl4965_set_bit(priv, CSR_HW_IF_CONFIG_REG,
-                       CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
-
-               /* See if we got it */
-               rc = iwl4965_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
-                                       CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
-                                       CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
-                                       EEPROM_SEM_TIMEOUT);
-               if (rc >= 0) {
-                       IWL_DEBUG_IO("Acquired semaphore after %d tries.\n",
-                               count+1);
-                       return rc;
-               }
-       }
+static struct iwl_ops iwl4965_ops = {
+       .lib = &iwl4965_lib,
+       .hcmd = &iwl4965_hcmd,
+       .utils = &iwl4965_hcmd_utils,
+};
 
-       return rc;
-}
+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,
+       .ops = &iwl4965_ops,
+       .mod_params = &iwl4965_mod_params,
+};
+
+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(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(
+       disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
+MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
+
+module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
+MODULE_PARM_DESC(queues_num, "number of hw queues.");
+
+/* QoS */
+module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
+MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
+module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
+MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
 
-MODULE_DEVICE_TABLE(pci, iwl4965_hw_card_ids);