Merge branch 'wireless-2.6' into wireless-next-2.6
authorReinette Chatre <reinette.chatre@intel.com>
Fri, 30 Apr 2010 22:13:00 +0000 (15:13 -0700)
committerReinette Chatre <reinette.chatre@intel.com>
Fri, 30 Apr 2010 22:13:00 +0000 (15:13 -0700)
Patch "iwlwifi: work around passive scan issue" was merged into
wireless-2.6, but touched a lot of code since modified (and moved)
in wireless-next-2.6. This caused some conflicts.

Conflicts:
drivers/net/wireless/iwlwifi/iwl-scan.c

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
1  2 
drivers/net/wireless/iwlwifi/iwl-agn-lib.c
drivers/net/wireless/iwlwifi/iwl-commands.h
drivers/net/wireless/iwlwifi/iwl3945-base.c
drivers/net/wireless/p54/p54pci.c
net/mac80211/agg-tx.c
net/mac80211/mlme.c

index 4bd0aec,0000000..a273474
mode 100644,000000..100644
--- /dev/null
@@@ -1,1502 -1,0 +1,1515 @@@
-               scan->good_CRC_th = 0;
 +/******************************************************************************
 + *
 + * GPL LICENSE SUMMARY
 + *
 + * Copyright(c) 2008 - 2010 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
 + * published by the Free Software Foundation.
 + *
 + * This program is distributed in the hope that it will be useful, but
 + * WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 + * General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 + * USA
 + *
 + * The full GNU General Public License is included in this distribution
 + * in the file called LICENSE.GPL.
 + *
 + * Contact Information:
 + *  Intel Linux Wireless <ilw@linux.intel.com>
 + * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 + *
 + *****************************************************************************/
 +#include <linux/etherdevice.h>
 +#include <linux/kernel.h>
 +#include <linux/module.h>
 +#include <linux/init.h>
 +#include <linux/sched.h>
 +
 +#include "iwl-dev.h"
 +#include "iwl-core.h"
 +#include "iwl-io.h"
 +#include "iwl-helpers.h"
 +#include "iwl-agn-hw.h"
 +#include "iwl-agn.h"
 +
 +static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
 +{
 +      return le32_to_cpup((__le32 *)&tx_resp->status +
 +                          tx_resp->frame_count) & MAX_SN;
 +}
 +
 +static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
 +                                    struct iwl_ht_agg *agg,
 +                                    struct iwl5000_tx_resp *tx_resp,
 +                                    int txq_id, u16 start_idx)
 +{
 +      u16 status;
 +      struct agg_tx_status *frame_status = &tx_resp->status;
 +      struct ieee80211_tx_info *info = NULL;
 +      struct ieee80211_hdr *hdr = NULL;
 +      u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
 +      int i, sh, idx;
 +      u16 seq;
 +
 +      if (agg->wait_for_ba)
 +              IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
 +
 +      agg->frame_count = tx_resp->frame_count;
 +      agg->start_idx = start_idx;
 +      agg->rate_n_flags = rate_n_flags;
 +      agg->bitmap = 0;
 +
 +      /* # frames attempted by Tx command */
 +      if (agg->frame_count == 1) {
 +              /* Only one frame was attempted; no block-ack will arrive */
 +              status = le16_to_cpu(frame_status[0].status);
 +              idx = start_idx;
 +
 +              /* FIXME: code repetition */
 +              IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
 +                                 agg->frame_count, agg->start_idx, idx);
 +
 +              info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
 +              info->status.rates[0].count = tx_resp->failure_frame + 1;
 +              info->flags &= ~IEEE80211_TX_CTL_AMPDU;
 +              info->flags |= iwl_tx_status_to_mac80211(status);
 +              iwlagn_hwrate_to_tx_control(priv, rate_n_flags, info);
 +
 +              /* FIXME: code repetition end */
 +
 +              IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
 +                                  status & 0xff, tx_resp->failure_frame);
 +              IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
 +
 +              agg->wait_for_ba = 0;
 +      } else {
 +              /* Two or more frames were attempted; expect block-ack */
 +              u64 bitmap = 0;
 +              int start = agg->start_idx;
 +
 +              /* Construct bit-map of pending frames within Tx window */
 +              for (i = 0; i < agg->frame_count; i++) {
 +                      u16 sc;
 +                      status = le16_to_cpu(frame_status[i].status);
 +                      seq  = le16_to_cpu(frame_status[i].sequence);
 +                      idx = SEQ_TO_INDEX(seq);
 +                      txq_id = SEQ_TO_QUEUE(seq);
 +
 +                      if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
 +                                    AGG_TX_STATE_ABORT_MSK))
 +                              continue;
 +
 +                      IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
 +                                         agg->frame_count, txq_id, idx);
 +
 +                      hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
 +                      if (!hdr) {
 +                              IWL_ERR(priv,
 +                                      "BUG_ON idx doesn't point to valid skb"
 +                                      " idx=%d, txq_id=%d\n", idx, txq_id);
 +                              return -1;
 +                      }
 +
 +                      sc = le16_to_cpu(hdr->seq_ctrl);
 +                      if (idx != (SEQ_TO_SN(sc) & 0xff)) {
 +                              IWL_ERR(priv,
 +                                      "BUG_ON idx doesn't match seq control"
 +                                      " idx=%d, seq_idx=%d, seq=%d\n",
 +                                        idx, SEQ_TO_SN(sc),
 +                                        hdr->seq_ctrl);
 +                              return -1;
 +                      }
 +
 +                      IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
 +                                         i, idx, SEQ_TO_SN(sc));
 +
 +                      sh = idx - start;
 +                      if (sh > 64) {
 +                              sh = (start - idx) + 0xff;
 +                              bitmap = bitmap << sh;
 +                              sh = 0;
 +                              start = idx;
 +                      } else if (sh < -64)
 +                              sh  = 0xff - (start - idx);
 +                      else if (sh < 0) {
 +                              sh = start - idx;
 +                              start = idx;
 +                              bitmap = bitmap << sh;
 +                              sh = 0;
 +                      }
 +                      bitmap |= 1ULL << sh;
 +                      IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
 +                                         start, (unsigned long long)bitmap);
 +              }
 +
 +              agg->bitmap = bitmap;
 +              agg->start_idx = start;
 +              IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
 +                                 agg->frame_count, agg->start_idx,
 +                                 (unsigned long long)agg->bitmap);
 +
 +              if (bitmap)
 +                      agg->wait_for_ba = 1;
 +      }
 +      return 0;
 +}
 +
 +void iwl_check_abort_status(struct iwl_priv *priv,
 +                          u8 frame_count, u32 status)
 +{
 +      if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
 +              IWL_ERR(priv, "TODO: Implement Tx flush command!!!\n");
 +      }
 +}
 +
 +static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
 +                              struct iwl_rx_mem_buffer *rxb)
 +{
 +      struct iwl_rx_packet *pkt = rxb_addr(rxb);
 +      u16 sequence = le16_to_cpu(pkt->hdr.sequence);
 +      int txq_id = SEQ_TO_QUEUE(sequence);
 +      int index = SEQ_TO_INDEX(sequence);
 +      struct iwl_tx_queue *txq = &priv->txq[txq_id];
 +      struct ieee80211_tx_info *info;
 +      struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
 +      u32  status = le16_to_cpu(tx_resp->status.status);
 +      int tid;
 +      int sta_id;
 +      int freed;
 +
 +      if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
 +              IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
 +                        "is out of range [0-%d] %d %d\n", txq_id,
 +                        index, txq->q.n_bd, txq->q.write_ptr,
 +                        txq->q.read_ptr);
 +              return;
 +      }
 +
 +      info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
 +      memset(&info->status, 0, sizeof(info->status));
 +
 +      tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
 +      sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
 +
 +      if (txq->sched_retry) {
 +              const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
 +              struct iwl_ht_agg *agg = NULL;
 +
 +              agg = &priv->stations[sta_id].tid[tid].agg;
 +
 +              iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
 +
 +              /* check if BAR is needed */
 +              if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
 +                      info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
 +
 +              if (txq->q.read_ptr != (scd_ssn & 0xff)) {
 +                      index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
 +                      IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
 +                                      "scd_ssn=%d idx=%d txq=%d swq=%d\n",
 +                                      scd_ssn , index, txq_id, txq->swq_id);
 +
 +                      freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
 +                      iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
 +
 +                      if (priv->mac80211_registered &&
 +                          (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
 +                          (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
 +                              if (agg->state == IWL_AGG_OFF)
 +                                      iwl_wake_queue(priv, txq_id);
 +                              else
 +                                      iwl_wake_queue(priv, txq->swq_id);
 +                      }
 +              }
 +      } else {
 +              BUG_ON(txq_id != txq->swq_id);
 +
 +              info->status.rates[0].count = tx_resp->failure_frame + 1;
 +              info->flags |= iwl_tx_status_to_mac80211(status);
 +              iwlagn_hwrate_to_tx_control(priv,
 +                                      le32_to_cpu(tx_resp->rate_n_flags),
 +                                      info);
 +
 +              IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
 +                                 "0x%x retries %d\n",
 +                                 txq_id,
 +                                 iwl_get_tx_fail_reason(status), status,
 +                                 le32_to_cpu(tx_resp->rate_n_flags),
 +                                 tx_resp->failure_frame);
 +
 +              freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
 +              iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
 +
 +              if (priv->mac80211_registered &&
 +                  (iwl_queue_space(&txq->q) > txq->q.low_mark))
 +                      iwl_wake_queue(priv, txq_id);
 +      }
 +
 +      iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
 +
 +      iwl_check_abort_status(priv, tx_resp->frame_count, status);
 +}
 +
 +void iwlagn_rx_handler_setup(struct iwl_priv *priv)
 +{
 +      /* init calibration handlers */
 +      priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
 +                                      iwlagn_rx_calib_result;
 +      priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
 +                                      iwlagn_rx_calib_complete;
 +      priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
 +}
 +
 +void iwlagn_setup_deferred_work(struct iwl_priv *priv)
 +{
 +      /* in agn, the tx power calibration is done in uCode */
 +      priv->disable_tx_power_cal = 1;
 +}
 +
 +int iwlagn_hw_valid_rtc_data_addr(u32 addr)
 +{
 +      return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
 +              (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
 +}
 +
 +int iwlagn_send_tx_power(struct iwl_priv *priv)
 +{
 +      struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
 +      u8 tx_ant_cfg_cmd;
 +
 +      /* half dBm need to multiply */
 +      tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
 +
 +      if (priv->tx_power_lmt_in_half_dbm &&
 +          priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
 +              /*
 +               * For the newer devices which using enhanced/extend tx power
 +               * table in EEPROM, the format is in half dBm. driver need to
 +               * convert to dBm format before report to mac80211.
 +               * By doing so, there is a possibility of 1/2 dBm resolution
 +               * lost. driver will perform "round-up" operation before
 +               * reporting, but it will cause 1/2 dBm tx power over the
 +               * regulatory limit. Perform the checking here, if the
 +               * "tx_power_user_lmt" is higher than EEPROM value (in
 +               * half-dBm format), lower the tx power based on EEPROM
 +               */
 +              tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
 +      }
 +      tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
 +      tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
 +
 +      if (IWL_UCODE_API(priv->ucode_ver) == 1)
 +              tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
 +      else
 +              tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
 +
 +      return  iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
 +                                     sizeof(tx_power_cmd), &tx_power_cmd,
 +                                     NULL);
 +}
 +
 +void iwlagn_temperature(struct iwl_priv *priv)
 +{
 +      /* store temperature from statistics (in Celsius) */
 +      priv->temperature = le32_to_cpu(priv->statistics.general.temperature);
 +      iwl_tt_handler(priv);
 +}
 +
 +u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
 +{
 +      struct iwl_eeprom_calib_hdr {
 +              u8 version;
 +              u8 pa_type;
 +              u16 voltage;
 +      } *hdr;
 +
 +      hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
 +                                                      EEPROM_CALIB_ALL);
 +      return hdr->version;
 +
 +}
 +
 +/*
 + * EEPROM
 + */
 +static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
 +{
 +      u16 offset = 0;
 +
 +      if ((address & INDIRECT_ADDRESS) == 0)
 +              return address;
 +
 +      switch (address & INDIRECT_TYPE_MSK) {
 +      case INDIRECT_HOST:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
 +              break;
 +      case INDIRECT_GENERAL:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
 +              break;
 +      case INDIRECT_REGULATORY:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
 +              break;
 +      case INDIRECT_CALIBRATION:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
 +              break;
 +      case INDIRECT_PROCESS_ADJST:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
 +              break;
 +      case INDIRECT_OTHERS:
 +              offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
 +              break;
 +      default:
 +              IWL_ERR(priv, "illegal indirect type: 0x%X\n",
 +              address & INDIRECT_TYPE_MSK);
 +              break;
 +      }
 +
 +      /* translate the offset from words to byte */
 +      return (address & ADDRESS_MSK) + (offset << 1);
 +}
 +
 +const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
 +                                         size_t offset)
 +{
 +      u32 address = eeprom_indirect_address(priv, offset);
 +      BUG_ON(address >= priv->cfg->eeprom_size);
 +      return &priv->eeprom[address];
 +}
 +
 +struct iwl_mod_params iwlagn_mod_params = {
 +      .amsdu_size_8K = 1,
 +      .restart_fw = 1,
 +      /* the rest are 0 by default */
 +};
 +
 +void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
 +{
 +      unsigned long flags;
 +      int i;
 +      spin_lock_irqsave(&rxq->lock, flags);
 +      INIT_LIST_HEAD(&rxq->rx_free);
 +      INIT_LIST_HEAD(&rxq->rx_used);
 +      /* Fill the rx_used queue with _all_ of the Rx buffers */
 +      for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
 +              /* In the reset function, these buffers may have been allocated
 +               * to an SKB, so we need to unmap and free potential storage */
 +              if (rxq->pool[i].page != NULL) {
 +                      pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
 +                              PAGE_SIZE << priv->hw_params.rx_page_order,
 +                              PCI_DMA_FROMDEVICE);
 +                      __iwl_free_pages(priv, rxq->pool[i].page);
 +                      rxq->pool[i].page = NULL;
 +              }
 +              list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
 +      }
 +
 +      for (i = 0; i < RX_QUEUE_SIZE; i++)
 +              rxq->queue[i] = NULL;
 +
 +      /* Set us so that we have processed and used all buffers, but have
 +       * not restocked the Rx queue with fresh buffers */
 +      rxq->read = rxq->write = 0;
 +      rxq->write_actual = 0;
 +      rxq->free_count = 0;
 +      spin_unlock_irqrestore(&rxq->lock, flags);
 +}
 +
 +int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
 +{
 +      u32 rb_size;
 +      const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
 +      u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
 +
 +      if (!priv->cfg->use_isr_legacy)
 +              rb_timeout = RX_RB_TIMEOUT;
 +
 +      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 */
 +      iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
 +
 +      /* Reset driver's Rx queue write index */
 +      iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
 +
 +      /* Tell device where to find RBD circular buffer in DRAM */
 +      iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
 +                         (u32)(rxq->dma_addr >> 8));
 +
 +      /* Tell device where in DRAM to update its Rx status */
 +      iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
 +                         rxq->rb_stts_dma >> 4);
 +
 +      /* Enable Rx DMA
 +       * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
 +       *      the credit mechanism in 5000 HW RX FIFO
 +       * Direct rx interrupts to hosts
 +       * Rx buffer size 4 or 8k
 +       * RB timeout 0x10
 +       * 256 RBDs
 +       */
 +      iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
 +                         FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
 +                         FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
 +                         FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
 +                         FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
 +                         rb_size|
 +                         (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
 +                         (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
 +
 +      /* Set interrupt coalescing timer to default (2048 usecs) */
 +      iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
 +
 +      return 0;
 +}
 +
 +int iwlagn_hw_nic_init(struct iwl_priv *priv)
 +{
 +      unsigned long flags;
 +      struct iwl_rx_queue *rxq = &priv->rxq;
 +      int ret;
 +
 +      /* nic_init */
 +      spin_lock_irqsave(&priv->lock, flags);
 +      priv->cfg->ops->lib->apm_ops.init(priv);
 +
 +      /* Set interrupt coalescing calibration timer to default (512 usecs) */
 +      iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
 +
 +      spin_unlock_irqrestore(&priv->lock, flags);
 +
 +      ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
 +
 +      priv->cfg->ops->lib->apm_ops.config(priv);
 +
 +      /* Allocate the RX queue, or reset if it is already allocated */
 +      if (!rxq->bd) {
 +              ret = iwl_rx_queue_alloc(priv);
 +              if (ret) {
 +                      IWL_ERR(priv, "Unable to initialize Rx queue\n");
 +                      return -ENOMEM;
 +              }
 +      } else
 +              iwlagn_rx_queue_reset(priv, rxq);
 +
 +      iwlagn_rx_replenish(priv);
 +
 +      iwlagn_rx_init(priv, rxq);
 +
 +      spin_lock_irqsave(&priv->lock, flags);
 +
 +      rxq->need_update = 1;
 +      iwl_rx_queue_update_write_ptr(priv, rxq);
 +
 +      spin_unlock_irqrestore(&priv->lock, flags);
 +
 +      /* Allocate or reset and init all Tx and Command queues */
 +      if (!priv->txq) {
 +              ret = iwlagn_txq_ctx_alloc(priv);
 +              if (ret)
 +                      return ret;
 +      } else
 +              iwlagn_txq_ctx_reset(priv);
 +
 +      set_bit(STATUS_INIT, &priv->status);
 +
 +      return 0;
 +}
 +
 +/**
 + * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
 + */
 +static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
 +                                        dma_addr_t dma_addr)
 +{
 +      return cpu_to_le32((u32)(dma_addr >> 8));
 +}
 +
 +/**
 + * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
 + *
 + * If there are slots in the RX queue that need to be restocked,
 + * and we have free pre-allocated buffers, fill the ranks as much
 + * as we can, pulling from rx_free.
 + *
 + * This moves the 'write' index forward to catch up with 'processed', and
 + * also updates the memory address in the firmware to reference the new
 + * target buffer.
 + */
 +void iwlagn_rx_queue_restock(struct iwl_priv *priv)
 +{
 +      struct iwl_rx_queue *rxq = &priv->rxq;
 +      struct list_head *element;
 +      struct iwl_rx_mem_buffer *rxb;
 +      unsigned long flags;
 +
 +      spin_lock_irqsave(&rxq->lock, flags);
 +      while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
 +              /* The overwritten rxb must be a used one */
 +              rxb = rxq->queue[rxq->write];
 +              BUG_ON(rxb && rxb->page);
 +
 +              /* Get next free Rx buffer, remove from free list */
 +              element = rxq->rx_free.next;
 +              rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
 +              list_del(element);
 +
 +              /* Point to Rx buffer via next RBD in circular buffer */
 +              rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
 +                                                            rxb->page_dma);
 +              rxq->queue[rxq->write] = rxb;
 +              rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
 +              rxq->free_count--;
 +      }
 +      spin_unlock_irqrestore(&rxq->lock, flags);
 +      /* If the pre-allocated buffer pool is dropping low, schedule to
 +       * refill it */
 +      if (rxq->free_count <= RX_LOW_WATERMARK)
 +              queue_work(priv->workqueue, &priv->rx_replenish);
 +
 +
 +      /* If we've added more space for the firmware to place data, tell it.
 +       * Increment device's write pointer in multiples of 8. */
 +      if (rxq->write_actual != (rxq->write & ~0x7)) {
 +              spin_lock_irqsave(&rxq->lock, flags);
 +              rxq->need_update = 1;
 +              spin_unlock_irqrestore(&rxq->lock, flags);
 +              iwl_rx_queue_update_write_ptr(priv, rxq);
 +      }
 +}
 +
 +/**
 + * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
 + *
 + * When moving to rx_free an SKB is allocated for the slot.
 + *
 + * Also restock the Rx queue via iwl_rx_queue_restock.
 + * This is called as a scheduled work item (except for during initialization)
 + */
 +void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
 +{
 +      struct iwl_rx_queue *rxq = &priv->rxq;
 +      struct list_head *element;
 +      struct iwl_rx_mem_buffer *rxb;
 +      struct page *page;
 +      unsigned long flags;
 +      gfp_t gfp_mask = priority;
 +
 +      while (1) {
 +              spin_lock_irqsave(&rxq->lock, flags);
 +              if (list_empty(&rxq->rx_used)) {
 +                      spin_unlock_irqrestore(&rxq->lock, flags);
 +                      return;
 +              }
 +              spin_unlock_irqrestore(&rxq->lock, flags);
 +
 +              if (rxq->free_count > RX_LOW_WATERMARK)
 +                      gfp_mask |= __GFP_NOWARN;
 +
 +              if (priv->hw_params.rx_page_order > 0)
 +                      gfp_mask |= __GFP_COMP;
 +
 +              /* Alloc a new receive buffer */
 +              page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
 +              if (!page) {
 +                      if (net_ratelimit())
 +                              IWL_DEBUG_INFO(priv, "alloc_pages failed, "
 +                                             "order: %d\n",
 +                                             priv->hw_params.rx_page_order);
 +
 +                      if ((rxq->free_count <= RX_LOW_WATERMARK) &&
 +                          net_ratelimit())
 +                              IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
 +                                       priority == GFP_ATOMIC ?  "GFP_ATOMIC" : "GFP_KERNEL",
 +                                       rxq->free_count);
 +                      /* We don't reschedule replenish work here -- we will
 +                       * call the restock method and if it still needs
 +                       * more buffers it will schedule replenish */
 +                      return;
 +              }
 +
 +              spin_lock_irqsave(&rxq->lock, flags);
 +
 +              if (list_empty(&rxq->rx_used)) {
 +                      spin_unlock_irqrestore(&rxq->lock, flags);
 +                      __free_pages(page, priv->hw_params.rx_page_order);
 +                      return;
 +              }
 +              element = rxq->rx_used.next;
 +              rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
 +              list_del(element);
 +
 +              spin_unlock_irqrestore(&rxq->lock, flags);
 +
 +              BUG_ON(rxb->page);
 +              rxb->page = page;
 +              /* Get physical address of the RB */
 +              rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
 +                              PAGE_SIZE << priv->hw_params.rx_page_order,
 +                              PCI_DMA_FROMDEVICE);
 +              /* dma address must be no more than 36 bits */
 +              BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
 +              /* and also 256 byte aligned! */
 +              BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
 +
 +              spin_lock_irqsave(&rxq->lock, flags);
 +
 +              list_add_tail(&rxb->list, &rxq->rx_free);
 +              rxq->free_count++;
 +              priv->alloc_rxb_page++;
 +
 +              spin_unlock_irqrestore(&rxq->lock, flags);
 +      }
 +}
 +
 +void iwlagn_rx_replenish(struct iwl_priv *priv)
 +{
 +      unsigned long flags;
 +
 +      iwlagn_rx_allocate(priv, GFP_KERNEL);
 +
 +      spin_lock_irqsave(&priv->lock, flags);
 +      iwlagn_rx_queue_restock(priv);
 +      spin_unlock_irqrestore(&priv->lock, flags);
 +}
 +
 +void iwlagn_rx_replenish_now(struct iwl_priv *priv)
 +{
 +      iwlagn_rx_allocate(priv, GFP_ATOMIC);
 +
 +      iwlagn_rx_queue_restock(priv);
 +}
 +
 +/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
 + * If an SKB has been detached, the POOL needs to have its SKB set to NULL
 + * This free routine walks the list of POOL entries and if SKB is set to
 + * non NULL it is unmapped and freed
 + */
 +void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
 +{
 +      int i;
 +      for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
 +              if (rxq->pool[i].page != NULL) {
 +                      pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
 +                              PAGE_SIZE << priv->hw_params.rx_page_order,
 +                              PCI_DMA_FROMDEVICE);
 +                      __iwl_free_pages(priv, rxq->pool[i].page);
 +                      rxq->pool[i].page = NULL;
 +              }
 +      }
 +
 +      dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
 +                        rxq->dma_addr);
 +      dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
 +                        rxq->rb_stts, rxq->rb_stts_dma);
 +      rxq->bd = NULL;
 +      rxq->rb_stts  = NULL;
 +}
 +
 +int iwlagn_rxq_stop(struct iwl_priv *priv)
 +{
 +
 +      /* stop Rx DMA */
 +      iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
 +      iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
 +                          FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
 +
 +      return 0;
 +}
 +
 +int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
 +{
 +      int idx = 0;
 +      int band_offset = 0;
 +
 +      /* HT rate format: mac80211 wants an MCS number, which is just LSB */
 +      if (rate_n_flags & RATE_MCS_HT_MSK) {
 +              idx = (rate_n_flags & 0xff);
 +              return idx;
 +      /* Legacy rate format, search for match in table */
 +      } else {
 +              if (band == IEEE80211_BAND_5GHZ)
 +                      band_offset = IWL_FIRST_OFDM_RATE;
 +              for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
 +                      if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
 +                              return idx - band_offset;
 +      }
 +
 +      return -1;
 +}
 +
 +/* Calc max signal level (dBm) among 3 possible receivers */
 +static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
 +                              struct iwl_rx_phy_res *rx_resp)
 +{
 +      return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
 +}
 +
 +#ifdef CONFIG_IWLWIFI_DEBUG
 +/**
 + * iwlagn_dbg_report_frame - dump frame to syslog during debug sessions
 + *
 + * You may hack this function to show different aspects of received frames,
 + * including selective frame dumps.
 + * group100 parameter selects whether to show 1 out of 100 good data frames.
 + *    All beacon and probe response frames are printed.
 + */
 +static void iwlagn_dbg_report_frame(struct iwl_priv *priv,
 +                    struct iwl_rx_phy_res *phy_res, u16 length,
 +                    struct ieee80211_hdr *header, int group100)
 +{
 +      u32 to_us;
 +      u32 print_summary = 0;
 +      u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
 +      u32 hundred = 0;
 +      u32 dataframe = 0;
 +      __le16 fc;
 +      u16 seq_ctl;
 +      u16 channel;
 +      u16 phy_flags;
 +      u32 rate_n_flags;
 +      u32 tsf_low;
 +      int rssi;
 +
 +      if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
 +              return;
 +
 +      /* MAC header */
 +      fc = header->frame_control;
 +      seq_ctl = le16_to_cpu(header->seq_ctrl);
 +
 +      /* metadata */
 +      channel = le16_to_cpu(phy_res->channel);
 +      phy_flags = le16_to_cpu(phy_res->phy_flags);
 +      rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
 +
 +      /* signal statistics */
 +      rssi = iwlagn_calc_rssi(priv, phy_res);
 +      tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
 +
 +      to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
 +
 +      /* if data frame is to us and all is good,
 +       *   (optionally) print summary for only 1 out of every 100 */
 +      if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
 +          cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
 +              dataframe = 1;
 +              if (!group100)
 +                      print_summary = 1;      /* print each frame */
 +              else if (priv->framecnt_to_us < 100) {
 +                      priv->framecnt_to_us++;
 +                      print_summary = 0;
 +              } else {
 +                      priv->framecnt_to_us = 0;
 +                      print_summary = 1;
 +                      hundred = 1;
 +              }
 +      } else {
 +              /* print summary for all other frames */
 +              print_summary = 1;
 +      }
 +
 +      if (print_summary) {
 +              char *title;
 +              int rate_idx;
 +              u32 bitrate;
 +
 +              if (hundred)
 +                      title = "100Frames";
 +              else if (ieee80211_has_retry(fc))
 +                      title = "Retry";
 +              else if (ieee80211_is_assoc_resp(fc))
 +                      title = "AscRsp";
 +              else if (ieee80211_is_reassoc_resp(fc))
 +                      title = "RasRsp";
 +              else if (ieee80211_is_probe_resp(fc)) {
 +                      title = "PrbRsp";
 +                      print_dump = 1; /* dump frame contents */
 +              } else if (ieee80211_is_beacon(fc)) {
 +                      title = "Beacon";
 +                      print_dump = 1; /* dump frame contents */
 +              } else if (ieee80211_is_atim(fc))
 +                      title = "ATIM";
 +              else if (ieee80211_is_auth(fc))
 +                      title = "Auth";
 +              else if (ieee80211_is_deauth(fc))
 +                      title = "DeAuth";
 +              else if (ieee80211_is_disassoc(fc))
 +                      title = "DisAssoc";
 +              else
 +                      title = "Frame";
 +
 +              rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
 +              if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
 +                      bitrate = 0;
 +                      WARN_ON_ONCE(1);
 +              } else {
 +                      bitrate = iwl_rates[rate_idx].ieee / 2;
 +              }
 +
 +              /* print frame summary.
 +               * MAC addresses show just the last byte (for brevity),
 +               *    but you can hack it to show more, if you'd like to. */
 +              if (dataframe)
 +                      IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
 +                                   "len=%u, rssi=%d, chnl=%d, rate=%u,\n",
 +                                   title, le16_to_cpu(fc), header->addr1[5],
 +                                   length, rssi, channel, bitrate);
 +              else {
 +                      /* src/dst addresses assume managed mode */
 +                      IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
 +                                   "len=%u, rssi=%d, tim=%lu usec, "
 +                                   "phy=0x%02x, chnl=%d\n",
 +                                   title, le16_to_cpu(fc), header->addr1[5],
 +                                   header->addr3[5], length, rssi,
 +                                   tsf_low - priv->scan_start_tsf,
 +                                   phy_flags, channel);
 +              }
 +      }
 +      if (print_dump)
 +              iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
 +}
 +#endif
 +
 +static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, 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(priv, "decrypt_in:0x%x  decrypt_out = 0x%x\n",
 +                                      decrypt_in, decrypt_out);
 +
 +      return decrypt_out;
 +}
 +
 +static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
 +                                      struct ieee80211_hdr *hdr,
 +                                      u16 len,
 +                                      u32 ampdu_status,
 +                                      struct iwl_rx_mem_buffer *rxb,
 +                                      struct ieee80211_rx_status *stats)
 +{
 +      struct sk_buff *skb;
 +      __le16 fc = hdr->frame_control;
 +
 +      /* We only process data packets if the interface is open */
 +      if (unlikely(!priv->is_open)) {
 +              IWL_DEBUG_DROP_LIMIT(priv,
 +                  "Dropping packet while interface is not open.\n");
 +              return;
 +      }
 +
 +      /* In case of HW accelerated crypto and bad decryption, drop */
 +      if (!priv->cfg->mod_params->sw_crypto &&
 +          iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
 +              return;
 +
 +      skb = dev_alloc_skb(128);
 +      if (!skb) {
 +              IWL_ERR(priv, "dev_alloc_skb failed\n");
 +              return;
 +      }
 +
 +      skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
 +
 +      iwl_update_stats(priv, false, fc, len);
 +      memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
 +
 +      ieee80211_rx(priv->hw, skb);
 +      priv->alloc_rxb_page--;
 +      rxb->page = NULL;
 +}
 +
 +/* Called for REPLY_RX (legacy ABG frames), or
 + * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
 +void iwlagn_rx_reply_rx(struct iwl_priv *priv,
 +                              struct iwl_rx_mem_buffer *rxb)
 +{
 +      struct ieee80211_hdr *header;
 +      struct ieee80211_rx_status rx_status;
 +      struct iwl_rx_packet *pkt = rxb_addr(rxb);
 +      struct iwl_rx_phy_res *phy_res;
 +      __le32 rx_pkt_status;
 +      struct iwl4965_rx_mpdu_res_start *amsdu;
 +      u32 len;
 +      u32 ampdu_status;
 +      u32 rate_n_flags;
 +
 +      /**
 +       * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
 +       *      REPLY_RX: physical layer info is in this buffer
 +       *      REPLY_RX_MPDU_CMD: physical layer info was sent in separate
 +       *              command and cached in priv->last_phy_res
 +       *
 +       * Here we set up local variables depending on which command is
 +       * received.
 +       */
 +      if (pkt->hdr.cmd == REPLY_RX) {
 +              phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
 +              header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
 +                              + phy_res->cfg_phy_cnt);
 +
 +              len = le16_to_cpu(phy_res->byte_count);
 +              rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
 +                              phy_res->cfg_phy_cnt + len);
 +              ampdu_status = le32_to_cpu(rx_pkt_status);
 +      } else {
 +              if (!priv->_agn.last_phy_res_valid) {
 +                      IWL_ERR(priv, "MPDU frame without cached PHY data\n");
 +                      return;
 +              }
 +              phy_res = &priv->_agn.last_phy_res;
 +              amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
 +              header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
 +              len = le16_to_cpu(amsdu->byte_count);
 +              rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
 +              ampdu_status = iwlagn_translate_rx_status(priv,
 +                              le32_to_cpu(rx_pkt_status));
 +      }
 +
 +      if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
 +              IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
 +                              phy_res->cfg_phy_cnt);
 +              return;
 +      }
 +
 +      if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
 +          !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
 +              IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
 +                              le32_to_cpu(rx_pkt_status));
 +              return;
 +      }
 +
 +      /* This will be used in several places later */
 +      rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
 +
 +      /* rx_status carries information about the packet to mac80211 */
 +      rx_status.mactime = le64_to_cpu(phy_res->timestamp);
 +      rx_status.freq =
 +              ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
 +      rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
 +                              IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
 +      rx_status.rate_idx =
 +              iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
 +      rx_status.flag = 0;
 +
 +      /* TSF isn't reliable. In order to allow smooth user experience,
 +       * this W/A doesn't propagate it to the mac80211 */
 +      /*rx_status.flag |= RX_FLAG_TSFT;*/
 +
 +      priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
 +
 +      /* Find max signal strength (dBm) among 3 antenna/receiver chains */
 +      rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
 +
 +#ifdef CONFIG_IWLWIFI_DEBUG
 +      /* Set "1" to report good data frames in groups of 100 */
 +      if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
 +              iwlagn_dbg_report_frame(priv, phy_res, len, header, 1);
 +#endif
 +      iwl_dbg_log_rx_data_frame(priv, len, header);
 +      IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
 +              rx_status.signal, (unsigned long long)rx_status.mactime);
 +
 +      /*
 +       * "antenna number"
 +       *
 +       * It seems that the antenna field in the phy flags value
 +       * is actually a bit field. This is undefined by radiotap,
 +       * it wants an actual antenna number but I always get "7"
 +       * for most legacy frames I receive indicating that the
 +       * same frame was received on all three RX chains.
 +       *
 +       * I think this field should be removed in favor of a
 +       * new 802.11n radiotap field "RX chains" that is defined
 +       * as a bitmask.
 +       */
 +      rx_status.antenna =
 +              (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
 +              >> RX_RES_PHY_FLAGS_ANTENNA_POS;
 +
 +      /* set the preamble flag if appropriate */
 +      if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
 +              rx_status.flag |= RX_FLAG_SHORTPRE;
 +
 +      /* Set up the HT phy flags */
 +      if (rate_n_flags & RATE_MCS_HT_MSK)
 +              rx_status.flag |= RX_FLAG_HT;
 +      if (rate_n_flags & RATE_MCS_HT40_MSK)
 +              rx_status.flag |= RX_FLAG_40MHZ;
 +      if (rate_n_flags & RATE_MCS_SGI_MSK)
 +              rx_status.flag |= RX_FLAG_SHORT_GI;
 +
 +      iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
 +                                  rxb, &rx_status);
 +}
 +
 +/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
 + * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
 +void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
 +                          struct iwl_rx_mem_buffer *rxb)
 +{
 +      struct iwl_rx_packet *pkt = rxb_addr(rxb);
 +      priv->_agn.last_phy_res_valid = true;
 +      memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
 +             sizeof(struct iwl_rx_phy_res));
 +}
 +
 +static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
 +                                   enum ieee80211_band band,
 +                                   struct iwl_scan_channel *scan_ch)
 +{
 +      const struct ieee80211_supported_band *sband;
 +      const struct iwl_channel_info *ch_info;
 +      u16 passive_dwell = 0;
 +      u16 active_dwell = 0;
 +      int i, added = 0;
 +      u16 channel = 0;
 +
 +      sband = iwl_get_hw_mode(priv, band);
 +      if (!sband) {
 +              IWL_ERR(priv, "invalid band\n");
 +              return added;
 +      }
 +
 +      active_dwell = iwl_get_active_dwell_time(priv, band, 0);
 +      passive_dwell = iwl_get_passive_dwell_time(priv, band);
 +
 +      if (passive_dwell <= active_dwell)
 +              passive_dwell = active_dwell + 1;
 +
 +      /* only scan single channel, good enough to reset the RF */
 +      /* pick the first valid not in-use channel */
 +      if (band == IEEE80211_BAND_5GHZ) {
 +              for (i = 14; i < priv->channel_count; i++) {
 +                      if (priv->channel_info[i].channel !=
 +                          le16_to_cpu(priv->staging_rxon.channel)) {
 +                              channel = priv->channel_info[i].channel;
 +                              ch_info = iwl_get_channel_info(priv,
 +                                      band, channel);
 +                              if (is_channel_valid(ch_info))
 +                                      break;
 +                      }
 +              }
 +      } else {
 +              for (i = 0; i < 14; i++) {
 +                      if (priv->channel_info[i].channel !=
 +                          le16_to_cpu(priv->staging_rxon.channel)) {
 +                                      channel =
 +                                              priv->channel_info[i].channel;
 +                                      ch_info = iwl_get_channel_info(priv,
 +                                              band, channel);
 +                                      if (is_channel_valid(ch_info))
 +                                              break;
 +                      }
 +              }
 +      }
 +      if (channel) {
 +              scan_ch->channel = cpu_to_le16(channel);
 +              scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
 +              scan_ch->active_dwell = cpu_to_le16(active_dwell);
 +              scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
 +              /* Set txpower levels to defaults */
 +              scan_ch->dsp_atten = 110;
 +              if (band == IEEE80211_BAND_5GHZ)
 +                      scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
 +              else
 +                      scan_ch->tx_gain = ((1 << 5) | (5 << 3));
 +              added++;
 +      } else
 +              IWL_ERR(priv, "no valid channel found\n");
 +      return added;
 +}
 +
 +static int iwl_get_channels_for_scan(struct iwl_priv *priv,
 +                                   enum ieee80211_band band,
 +                                   u8 is_active, u8 n_probes,
 +                                   struct iwl_scan_channel *scan_ch)
 +{
 +      struct ieee80211_channel *chan;
 +      const struct ieee80211_supported_band *sband;
 +      const struct iwl_channel_info *ch_info;
 +      u16 passive_dwell = 0;
 +      u16 active_dwell = 0;
 +      int added, i;
 +      u16 channel;
 +
 +      sband = iwl_get_hw_mode(priv, band);
 +      if (!sband)
 +              return 0;
 +
 +      active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
 +      passive_dwell = iwl_get_passive_dwell_time(priv, band);
 +
 +      if (passive_dwell <= active_dwell)
 +              passive_dwell = active_dwell + 1;
 +
 +      for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
 +              chan = priv->scan_request->channels[i];
 +
 +              if (chan->band != band)
 +                      continue;
 +
 +              channel = ieee80211_frequency_to_channel(chan->center_freq);
 +              scan_ch->channel = cpu_to_le16(channel);
 +
 +              ch_info = iwl_get_channel_info(priv, band, channel);
 +              if (!is_channel_valid(ch_info)) {
 +                      IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
 +                                      channel);
 +                      continue;
 +              }
 +
 +              if (!is_active || is_channel_passive(ch_info) ||
 +                  (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
 +                      scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
 +              else
 +                      scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
 +
 +              if (n_probes)
 +                      scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
 +
 +              scan_ch->active_dwell = cpu_to_le16(active_dwell);
 +              scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
 +
 +              /* Set txpower levels to defaults */
 +              scan_ch->dsp_atten = 110;
 +
 +              /* NOTE: if we were doing 6Mb OFDM for scans we'd use
 +               * power level:
 +               * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
 +               */
 +              if (band == IEEE80211_BAND_5GHZ)
 +                      scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
 +              else
 +                      scan_ch->tx_gain = ((1 << 5) | (5 << 3));
 +
 +              IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
 +                             channel, le32_to_cpu(scan_ch->type),
 +                             (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
 +                              "ACTIVE" : "PASSIVE",
 +                             (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
 +                             active_dwell : passive_dwell);
 +
 +              scan_ch++;
 +              added++;
 +      }
 +
 +      IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
 +      return added;
 +}
 +
 +void iwlagn_request_scan(struct iwl_priv *priv)
 +{
 +      struct iwl_host_cmd cmd = {
 +              .id = REPLY_SCAN_CMD,
 +              .len = sizeof(struct iwl_scan_cmd),
 +              .flags = CMD_SIZE_HUGE,
 +      };
 +      struct iwl_scan_cmd *scan;
 +      struct ieee80211_conf *conf = NULL;
 +      u32 rate_flags = 0;
 +      u16 cmd_len;
 +      u16 rx_chain = 0;
 +      enum ieee80211_band band;
 +      u8 n_probes = 0;
 +      u8 rx_ant = priv->hw_params.valid_rx_ant;
 +      u8 rate;
 +      bool is_active = false;
 +      int  chan_mod;
 +      u8 active_chains;
 +
 +      conf = ieee80211_get_hw_conf(priv->hw);
 +
 +      cancel_delayed_work(&priv->scan_check);
 +
 +      if (!iwl_is_ready(priv)) {
 +              IWL_WARN(priv, "request scan called when driver not ready.\n");
 +              goto done;
 +      }
 +
 +      /* Make sure the scan wasn't canceled before this queued work
 +       * was given the chance to run... */
 +      if (!test_bit(STATUS_SCANNING, &priv->status))
 +              goto done;
 +
 +      /* This should never be called or scheduled if there is currently
 +       * a scan active in the hardware. */
 +      if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 +              IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
 +                             "Ignoring second request.\n");
 +              goto done;
 +      }
 +
 +      if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 +              IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
 +              goto done;
 +      }
 +
 +      if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 +              IWL_DEBUG_HC(priv, "Scan request while abort pending.  Queuing.\n");
 +              goto done;
 +      }
 +
 +      if (iwl_is_rfkill(priv)) {
 +              IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
 +              goto done;
 +      }
 +
 +      if (!test_bit(STATUS_READY, &priv->status)) {
 +              IWL_DEBUG_HC(priv, "Scan request while uninitialized.  Queuing.\n");
 +              goto done;
 +      }
 +
 +      if (!priv->scan_cmd) {
 +              priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
 +                                       IWL_MAX_SCAN_SIZE, GFP_KERNEL);
 +              if (!priv->scan_cmd) {
 +                      IWL_DEBUG_SCAN(priv,
 +                                     "fail to allocate memory for scan\n");
 +                      goto done;
 +              }
 +      }
 +      scan = priv->scan_cmd;
 +      memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
 +
 +      scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
 +      scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
 +
 +      if (iwl_is_associated(priv)) {
 +              u16 interval = 0;
 +              u32 extra;
 +              u32 suspend_time = 100;
 +              u32 scan_suspend_time = 100;
 +              unsigned long flags;
 +
 +              IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
 +              spin_lock_irqsave(&priv->lock, flags);
 +              interval = priv->beacon_int;
 +              spin_unlock_irqrestore(&priv->lock, flags);
 +
 +              scan->suspend_time = 0;
 +              scan->max_out_time = cpu_to_le32(200 * 1024);
 +              if (!interval)
 +                      interval = suspend_time;
 +
 +              extra = (suspend_time / interval) << 22;
 +              scan_suspend_time = (extra |
 +                  ((suspend_time % interval) * 1024));
 +              scan->suspend_time = cpu_to_le32(scan_suspend_time);
 +              IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
 +                             scan_suspend_time, interval);
 +      }
 +
 +      if (priv->is_internal_short_scan) {
 +              IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
 +      } else if (priv->scan_request->n_ssids) {
 +              int i, p = 0;
 +              IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
 +              for (i = 0; i < priv->scan_request->n_ssids; i++) {
 +                      /* always does wildcard anyway */
 +                      if (!priv->scan_request->ssids[i].ssid_len)
 +                              continue;
 +                      scan->direct_scan[p].id = WLAN_EID_SSID;
 +                      scan->direct_scan[p].len =
 +                              priv->scan_request->ssids[i].ssid_len;
 +                      memcpy(scan->direct_scan[p].ssid,
 +                             priv->scan_request->ssids[i].ssid,
 +                             priv->scan_request->ssids[i].ssid_len);
 +                      n_probes++;
 +                      p++;
 +              }
 +              is_active = true;
 +      } else
 +              IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
 +
 +      scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
 +      scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
 +      scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
 +
 +      switch (priv->scan_band) {
 +      case IEEE80211_BAND_2GHZ:
 +              scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
 +              chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
 +                                     >> RXON_FLG_CHANNEL_MODE_POS;
 +              if (chan_mod == CHANNEL_MODE_PURE_40) {
 +                      rate = IWL_RATE_6M_PLCP;
 +              } else {
 +                      rate = IWL_RATE_1M_PLCP;
 +                      rate_flags = RATE_MCS_CCK_MSK;
 +              }
-                * If active scaning is requested but a certain channel
-                * is marked passive, we can do active scanning if we
-                * detect transmissions.
++              scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
 +              break;
 +      case IEEE80211_BAND_5GHZ:
 +              rate = IWL_RATE_6M_PLCP;
 +              /*
-               scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
++               * If active scanning is requested but a certain channel is
++               * marked passive, we can do active scanning if we detect
++               * transmissions.
++               *
++               * There is an issue with some firmware versions that triggers
++               * a sysassert on a "good CRC threshold" of zero (== disabled),
++               * on a radar channel even though this means that we should NOT
++               * send probes.
++               *
++               * The "good CRC threshold" is the number of frames that we
++               * need to receive during our dwell time on a channel before
++               * sending out probes -- setting this to a huge value will
++               * mean we never reach it, but at the same time work around
++               * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
++               * here instead of IWL_GOOD_CRC_TH_DISABLED.
 +               */
++              scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
++                                              IWL_GOOD_CRC_TH_NEVER;
 +              break;
 +      default:
 +              IWL_WARN(priv, "Invalid scan band count\n");
 +              goto done;
 +      }
 +
 +      band = priv->scan_band;
 +
 +      if (priv->cfg->scan_antennas[band])
 +              rx_ant = priv->cfg->scan_antennas[band];
 +
 +      priv->scan_tx_ant[band] =
 +                      iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
 +      rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
 +      scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
 +
 +      /* In power save mode use one chain, otherwise use all chains */
 +      if (test_bit(STATUS_POWER_PMI, &priv->status)) {
 +              /* rx_ant has been set to all valid chains previously */
 +              active_chains = rx_ant &
 +                              ((u8)(priv->chain_noise_data.active_chains));
 +              if (!active_chains)
 +                      active_chains = rx_ant;
 +
 +              IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
 +                              priv->chain_noise_data.active_chains);
 +
 +              rx_ant = first_antenna(active_chains);
 +      }
 +      /* MIMO is not used here, but value is required */
 +      rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
 +      rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
 +      rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
 +      rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
 +      scan->rx_chain = cpu_to_le16(rx_chain);
 +      if (!priv->is_internal_short_scan) {
 +              cmd_len = iwl_fill_probe_req(priv,
 +                                      (struct ieee80211_mgmt *)scan->data,
 +                                      priv->scan_request->ie,
 +                                      priv->scan_request->ie_len,
 +                                      IWL_MAX_SCAN_SIZE - sizeof(*scan));
 +      } else {
 +              cmd_len = iwl_fill_probe_req(priv,
 +                                      (struct ieee80211_mgmt *)scan->data,
 +                                      NULL, 0,
 +                                      IWL_MAX_SCAN_SIZE - sizeof(*scan));
 +
 +      }
 +      scan->tx_cmd.len = cpu_to_le16(cmd_len);
 +
 +      scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
 +                             RXON_FILTER_BCON_AWARE_MSK);
 +
 +      if (priv->is_internal_short_scan) {
 +              scan->channel_count =
 +                      iwl_get_single_channel_for_scan(priv, band,
 +                              (void *)&scan->data[le16_to_cpu(
 +                              scan->tx_cmd.len)]);
 +      } else {
 +              scan->channel_count =
 +                      iwl_get_channels_for_scan(priv, band,
 +                              is_active, n_probes,
 +                              (void *)&scan->data[le16_to_cpu(
 +                              scan->tx_cmd.len)]);
 +      }
 +      if (scan->channel_count == 0) {
 +              IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
 +              goto done;
 +      }
 +
 +      cmd.len += le16_to_cpu(scan->tx_cmd.len) +
 +          scan->channel_count * sizeof(struct iwl_scan_channel);
 +      cmd.data = scan;
 +      scan->len = cpu_to_le16(cmd.len);
 +
 +      set_bit(STATUS_SCAN_HW, &priv->status);
 +      if (iwl_send_cmd_sync(priv, &cmd))
 +              goto done;
 +
 +      queue_delayed_work(priv->workqueue, &priv->scan_check,
 +                         IWL_SCAN_CHECK_WATCHDOG);
 +
 +      return;
 +
 + done:
 +      /* Cannot perform scan. Make sure we clear scanning
 +      * bits from status so next scan request can be performed.
 +      * If we don't clear scanning status bit here all next scan
 +      * will fail
 +      */
 +      clear_bit(STATUS_SCAN_HW, &priv->status);
 +      clear_bit(STATUS_SCANNING, &priv->status);
 +      /* inform mac80211 scan aborted */
 +      queue_work(priv->workqueue, &priv->scan_completed);
 +}
@@@ -106,7 -106,7 +106,7 @@@ enum 
        REPLY_TX = 0x1c,
        REPLY_RATE_SCALE = 0x47,        /* 3945 only */
        REPLY_LEDS_CMD = 0x48,
 -      REPLY_TX_LINK_QUALITY_CMD = 0x4e, /* 4965 only */
 +      REPLY_TX_LINK_QUALITY_CMD = 0x4e, /* for 4965 and up */
  
        /* WiMAX coexistence */
        COEX_PRIORITY_TABLE_CMD = 0x5a, /* for 5000 series and up */
@@@ -512,9 -512,8 +512,9 @@@ struct iwl_init_alive_resp 
   *
   *     Entries without timestamps contain only event_id and data.
   *
 + *
   * 2)  error_event_table_ptr indicates base of the error log.  This contains
 - *     information about any uCode error that occurs.  For 4965, the format
 + *     information about any uCode error that occurs.  For agn, the format
   *     of the error log is:
   *
   *    __le32 valid;        (nonzero) valid, (0) log is empty
   *    __le32 bcon_time;    beacon timer
   *    __le32 tsf_low;      network timestamp function timer
   *    __le32 tsf_hi;       network timestamp function timer
 + *    __le32 gp1;          GP1 timer register
 + *    __le32 gp2;          GP2 timer register
 + *    __le32 gp3;          GP3 timer register
 + *    __le32 ucode_ver;    uCode version
 + *    __le32 hw_ver;       HW Silicon version
 + *    __le32 brd_ver;      HW board version
 + *    __le32 log_pc;       log program counter
 + *    __le32 frame_ptr;    frame pointer
 + *    __le32 stack_ptr;    stack pointer
 + *    __le32 hcmd;         last host command
 + *    __le32 isr0;         isr status register LMPM_NIC_ISR0: rxtx_flag
 + *    __le32 isr1;         isr status register LMPM_NIC_ISR1: host_flag
 + *    __le32 isr2;         isr status register LMPM_NIC_ISR2: enc_flag
 + *    __le32 isr3;         isr status register LMPM_NIC_ISR3: time_flag
 + *    __le32 isr4;         isr status register LMPM_NIC_ISR4: wico interrupt
 + *    __le32 isr_pref;     isr status register LMPM_NIC_PREF_STAT
 + *    __le32 wait_event;   wait event() caller address
 + *    __le32 l2p_control;  L2pControlField
 + *    __le32 l2p_duration; L2pDurationField
 + *    __le32 l2p_mhvalid;  L2pMhValidBits
 + *    __le32 l2p_addr_match; L2pAddrMatchStat
 + *    __le32 lmpm_pmg_sel; indicate which clocks are turned on (LMPM_PMG_SEL)
 + *    __le32 u_timestamp;  indicate when the date and time of the compilation
 + *    __le32 reserved;
   *
   * The Linux driver can print both logs to the system log when a uCode error
   * occurs.
@@@ -1443,7 -1418,7 +1443,7 @@@ struct iwl4965_rx_mpdu_res_start 
  
  /* 1: Ignore Bluetooth priority for this frame.
   * 0: Delay Tx until Bluetooth device is done (normal usage). */
 -#define TX_CMD_FLG_BT_DIS_MSK cpu_to_le32(1 << 12)
 +#define TX_CMD_FLG_IGNORE_BT cpu_to_le32(1 << 12)
  
  /* 1: uCode overrides sequence control field in MAC header.
   * 0: Driver provides sequence control field in MAC header.
@@@ -1662,7 -1637,7 +1662,7 @@@ struct iwl_tx_cmd 
        struct ieee80211_hdr hdr[0];
  } __attribute__ ((packed));
  
 -/* TX command response is sent after *all* transmission attempts.
 +/* TX command response is sent after *3945* transmission attempts.
   *
   * NOTES:
   *
   * control line.  Receiving is still allowed in this case.
   */
  enum {
 +      TX_3945_STATUS_SUCCESS = 0x01,
 +      TX_3945_STATUS_DIRECT_DONE = 0x02,
 +      TX_3945_STATUS_FAIL_SHORT_LIMIT = 0x82,
 +      TX_3945_STATUS_FAIL_LONG_LIMIT = 0x83,
 +      TX_3945_STATUS_FAIL_FIFO_UNDERRUN = 0x84,
 +      TX_3945_STATUS_FAIL_MGMNT_ABORT = 0x85,
 +      TX_3945_STATUS_FAIL_NEXT_FRAG = 0x86,
 +      TX_3945_STATUS_FAIL_LIFE_EXPIRE = 0x87,
 +      TX_3945_STATUS_FAIL_DEST_PS = 0x88,
 +      TX_3945_STATUS_FAIL_ABORTED = 0x89,
 +      TX_3945_STATUS_FAIL_BT_RETRY = 0x8a,
 +      TX_3945_STATUS_FAIL_STA_INVALID = 0x8b,
 +      TX_3945_STATUS_FAIL_FRAG_DROPPED = 0x8c,
 +      TX_3945_STATUS_FAIL_TID_DISABLE = 0x8d,
 +      TX_3945_STATUS_FAIL_FRAME_FLUSHED = 0x8e,
 +      TX_3945_STATUS_FAIL_INSUFFICIENT_CF_POLL = 0x8f,
 +      TX_3945_STATUS_FAIL_TX_LOCKED = 0x90,
 +      TX_3945_STATUS_FAIL_NO_BEACON_ON_RADAR = 0x91,
 +};
 +
 +/*
 + * TX command response is sent after *agn* transmission attempts.
 + *
 + * both postpone and abort status are expected behavior from uCode. there is
 + * no special operation required from driver; except for RFKILL_FLUSH,
 + * which required tx flush host command to flush all the tx frames in queues
 + */
 +enum {
        TX_STATUS_SUCCESS = 0x01,
        TX_STATUS_DIRECT_DONE = 0x02,
 +      /* postpone TX */
 +      TX_STATUS_POSTPONE_DELAY = 0x40,
 +      TX_STATUS_POSTPONE_FEW_BYTES = 0x41,
 +      TX_STATUS_POSTPONE_BT_PRIO = 0x42,
 +      TX_STATUS_POSTPONE_QUIET_PERIOD = 0x43,
 +      TX_STATUS_POSTPONE_CALC_TTAK = 0x44,
 +      /* abort TX */
 +      TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY = 0x81,
        TX_STATUS_FAIL_SHORT_LIMIT = 0x82,
        TX_STATUS_FAIL_LONG_LIMIT = 0x83,
        TX_STATUS_FAIL_FIFO_UNDERRUN = 0x84,
 -      TX_STATUS_FAIL_MGMNT_ABORT = 0x85,
 -      TX_STATUS_FAIL_NEXT_FRAG = 0x86,
 +      TX_STATUS_FAIL_DRAIN_FLOW = 0x85,
 +      TX_STATUS_FAIL_RFKILL_FLUSH = 0x86,
        TX_STATUS_FAIL_LIFE_EXPIRE = 0x87,
        TX_STATUS_FAIL_DEST_PS = 0x88,
 -      TX_STATUS_FAIL_ABORTED = 0x89,
 +      TX_STATUS_FAIL_HOST_ABORTED = 0x89,
        TX_STATUS_FAIL_BT_RETRY = 0x8a,
        TX_STATUS_FAIL_STA_INVALID = 0x8b,
        TX_STATUS_FAIL_FRAG_DROPPED = 0x8c,
        TX_STATUS_FAIL_TID_DISABLE = 0x8d,
 -      TX_STATUS_FAIL_FRAME_FLUSHED = 0x8e,
 +      TX_STATUS_FAIL_FIFO_FLUSHED = 0x8e,
        TX_STATUS_FAIL_INSUFFICIENT_CF_POLL = 0x8f,
 -      TX_STATUS_FAIL_TX_LOCKED = 0x90,
 -      TX_STATUS_FAIL_NO_BEACON_ON_RADAR = 0x91,
 +      /* uCode drop due to FW drop request */
 +      TX_STATUS_FAIL_FW_DROP = 0x90,
 +      /*
 +       * uCode drop due to station color mismatch
 +       * between tx command and station table
 +       */
 +      TX_STATUS_FAIL_STA_COLOR_MISMATCH_DROP = 0x91,
  };
  
  #define       TX_PACKET_MODE_REGULAR          0x0000
@@@ -1770,6 -1704,30 +1770,6 @@@ enum 
        TX_ABORT_REQUIRED_MSK = 0x80000000,     /* bits 31:31 */
  };
  
 -static inline u32 iwl_tx_status_to_mac80211(u32 status)
 -{
 -      status &= TX_STATUS_MSK;
 -
 -      switch (status) {
 -      case TX_STATUS_SUCCESS:
 -      case TX_STATUS_DIRECT_DONE:
 -              return IEEE80211_TX_STAT_ACK;
 -      case TX_STATUS_FAIL_DEST_PS:
 -              return IEEE80211_TX_STAT_TX_FILTERED;
 -      default:
 -              return 0;
 -      }
 -}
 -
 -static inline bool iwl_is_tx_success(u32 status)
 -{
 -      status &= TX_STATUS_MSK;
 -      return (status == TX_STATUS_SUCCESS) ||
 -             (status == TX_STATUS_DIRECT_DONE);
 -}
 -
 -
 -
  /* *******************************
   * TX aggregation status
   ******************************* */
@@@ -2663,7 -2621,9 +2663,9 @@@ struct iwl_ssid_ie 
  #define PROBE_OPTION_MAX_3945         4
  #define PROBE_OPTION_MAX              20
  #define TX_CMD_LIFE_TIME_INFINITE     cpu_to_le32(0xFFFFFFFF)
- #define IWL_GOOD_CRC_TH                       cpu_to_le16(1)
+ #define IWL_GOOD_CRC_TH_DISABLED      0
+ #define IWL_GOOD_CRC_TH_DEFAULT               cpu_to_le16(1)
+ #define IWL_GOOD_CRC_TH_NEVER         cpu_to_le16(0xffff)
  #define IWL_MAX_SCAN_SIZE 1024
  #define IWL_MAX_CMD_SIZE 4096
  #define IWL_MAX_PROBE_REQUEST         200
@@@ -351,11 -351,11 +351,11 @@@ static int iwl3945_send_beacon_cmd(stru
  
  static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  {
 -      if (priv->shared_virt)
 +      if (priv->_3945.shared_virt)
                dma_free_coherent(&priv->pci_dev->dev,
                                  sizeof(struct iwl3945_shared),
 -                                priv->shared_virt,
 -                                priv->shared_phys);
 +                                priv->_3945.shared_virt,
 +                                priv->_3945.shared_phys);
  }
  
  static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
@@@ -504,6 -504,15 +504,6 @@@ static int iwl3945_tx_skb(struct iwl_pr
                IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  #endif
  
 -      /* drop all non-injected data frame if we are not associated */
 -      if (ieee80211_is_data(fc) &&
 -          !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
 -          (!iwl_is_associated(priv) ||
 -           ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
 -              IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
 -              goto drop_unlock;
 -      }
 -
        spin_unlock_irqrestore(&priv->lock, flags);
  
        hdr_len = ieee80211_hdrlen(fc);
                txq->need_update = 0;
        }
  
 -      IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
 +      IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
                     le16_to_cpu(out_cmd->hdr.sequence));
 -      IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx_cmd->tx_flags));
 +      IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
        iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
        iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
                           ieee80211_hdrlen(fc));
@@@ -744,7 -753,7 +744,7 @@@ static int iwl3945_get_measurement(stru
        if (iwl_is_associated(priv))
                add_time =
                    iwl3945_usecs_to_beacons(
 -                      le64_to_cpu(params->start_time) - priv->last_tsf,
 +                      le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
                        le16_to_cpu(priv->rxon_timing.beacon_interval));
  
        memset(&spectrum, 0, sizeof(spectrum));
  
        if (iwl_is_associated(priv))
                spectrum.start_time =
 -                  iwl3945_add_beacon_time(priv->last_beacon_time,
 +                  iwl3945_add_beacon_time(priv->_3945.last_beacon_time,
                                add_time,
                                le16_to_cpu(priv->rxon_timing.beacon_interval));
        else
@@@ -1603,6 -1612,9 +1603,6 @@@ static int iwl3945_print_last_event_log
        return pos;
  }
  
 -/* For sanity check only.  Actual size is determined by uCode, typ. 512 */
 -#define IWL3945_MAX_EVENT_LOG_SIZE (512)
 -
  #define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
  
  int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
        num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
        next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  
 -      if (capacity > IWL3945_MAX_EVENT_LOG_SIZE) {
 +      if (capacity > priv->cfg->max_event_log_size) {
                IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
 -                      capacity, IWL3945_MAX_EVENT_LOG_SIZE);
 -              capacity = IWL3945_MAX_EVENT_LOG_SIZE;
 +                      capacity, priv->cfg->max_event_log_size);
 +              capacity = priv->cfg->max_event_log_size;
        }
  
 -      if (next_entry > IWL3945_MAX_EVENT_LOG_SIZE) {
 +      if (next_entry > priv->cfg->max_event_log_size) {
                IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
 -                      next_entry, IWL3945_MAX_EVENT_LOG_SIZE);
 -              next_entry = IWL3945_MAX_EVENT_LOG_SIZE;
 +                      next_entry, priv->cfg->max_event_log_size);
 +              next_entry = priv->cfg->max_event_log_size;
        }
  
        size = num_wraps ? capacity : next_entry;
@@@ -1934,7 -1946,7 +1934,7 @@@ static int iwl3945_get_channels_for_sca
                added++;
        }
  
 -      IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
 +      IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
        return added;
  }
  
@@@ -2477,6 -2489,8 +2477,6 @@@ static void iwl3945_alive_start(struct 
                goto restart;
        }
  
 -      iwl_clear_stations_table(priv);
 -
        rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
        IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
  
        /* After the ALIVE response, we can send commands to 3945 uCode */
        set_bit(STATUS_ALIVE, &priv->status);
  
 +      if (priv->cfg->ops->lib->recover_from_tx_stall) {
 +              /* Enable timer to monitor the driver queues */
 +              mod_timer(&priv->monitor_recover,
 +                      jiffies +
 +                      msecs_to_jiffies(priv->cfg->monitor_recover_period));
 +      }
 +
        if (iwl_is_rfkill(priv))
                return;
  
        ieee80211_wake_queues(priv->hw);
  
 -      priv->active_rate = priv->rates_mask;
 -      priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
 +      priv->active_rate = IWL_RATES_MASK;
  
        iwl_power_update_mode(priv, true);
  
        }
  
        /* Configure Bluetooth device coexistence support */
 -      iwl_send_bt_config(priv);
 +      priv->cfg->ops->hcmd->send_bt_config(priv);
  
        /* Configure the adapter for unassociated operation */
        iwlcore_commit_rxon(priv);
        set_bit(STATUS_READY, &priv->status);
        wake_up_interruptible(&priv->wait_command_queue);
  
 -      /* reassociate for ADHOC mode */
 -      if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
 -              struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
 -                                                              priv->vif);
 -              if (beacon)
 -                      iwl_mac_beacon_update(priv->hw, beacon);
 -      }
 -
 -      if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
 -              iwl_set_mode(priv, priv->iw_mode);
 -
        return;
  
   restart:
@@@ -2560,8 -2579,7 +2560,8 @@@ static void __iwl3945_down(struct iwl_p
        if (!exit_pending)
                set_bit(STATUS_EXIT_PENDING, &priv->status);
  
 -      iwl_clear_stations_table(priv);
 +      /* Station information will now be cleared in device */
 +      iwl_clear_ucode_stations(priv, true);
  
        /* Unblock any waiting calls */
        wake_up_interruptible_all(&priv->wait_command_queue);
@@@ -2695,10 -2713,12 +2695,10 @@@ static int __iwl3945_up(struct iwl_pri
  
        for (i = 0; i < MAX_HW_RESTARTS; i++) {
  
 -              iwl_clear_stations_table(priv);
 -
                /* load bootstrap state machine,
                 * load bootstrap program into processor's memory,
                 * prepare to load the "initialize" uCode */
 -              priv->cfg->ops->lib->load_ucode(priv);
 +              rc = priv->cfg->ops->lib->load_ucode(priv);
  
                if (rc) {
                        IWL_ERR(priv,
@@@ -2766,7 -2786,7 +2766,7 @@@ static void iwl3945_bg_alive_start(stru
  static void iwl3945_rfkill_poll(struct work_struct *data)
  {
        struct iwl_priv *priv =
 -          container_of(data, struct iwl_priv, rfkill_poll.work);
 +          container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
        bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
        bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
                        & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  
        /* Keep this running, even if radio now enabled.  This will be
         * cancelled in mac_start() if system decides to start again */
 -      queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
 +      queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
                           round_jiffies_relative(2 * HZ));
  
  }
  
 -#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
 -static void iwl3945_bg_request_scan(struct work_struct *data)
 +void iwl3945_request_scan(struct iwl_priv *priv)
  {
 -      struct iwl_priv *priv =
 -          container_of(data, struct iwl_priv, request_scan);
        struct iwl_host_cmd cmd = {
                .id = REPLY_SCAN_CMD,
                .len = sizeof(struct iwl3945_scan_cmd),
                .flags = CMD_SIZE_HUGE,
        };
 -      int rc = 0;
        struct iwl3945_scan_cmd *scan;
        struct ieee80211_conf *conf = NULL;
        u8 n_probes = 0;
  
        conf = ieee80211_get_hw_conf(priv->hw);
  
 -      mutex_lock(&priv->mutex);
 -
        cancel_delayed_work(&priv->scan_check);
  
        if (!iwl_is_ready(priv)) {
        if (test_bit(STATUS_SCAN_HW, &priv->status)) {
                IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests  "
                                "Ignoring second request.\n");
 -              rc = -EIO;
                goto done;
        }
  
                goto done;
        }
  
 -      if (!priv->scan_bands) {
 -              IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
 -              goto done;
 -      }
 -
 -      if (!priv->scan) {
 -              priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
 -                                   IWL_MAX_SCAN_SIZE, GFP_KERNEL);
 -              if (!priv->scan) {
 -                      rc = -ENOMEM;
 +      if (!priv->scan_cmd) {
 +              priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
 +                                       IWL_MAX_SCAN_SIZE, GFP_KERNEL);
 +              if (!priv->scan_cmd) {
 +                      IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
                        goto done;
                }
        }
 -      scan = priv->scan;
 +      scan = priv->scan_cmd;
        memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  
        scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
                               scan_suspend_time, interval);
        }
  
 -      if (priv->scan_request->n_ssids) {
 +      if (priv->is_internal_short_scan) {
 +              IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
 +      } else if (priv->scan_request->n_ssids) {
                int i, p = 0;
                IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
                for (i = 0; i < priv->scan_request->n_ssids; i++) {
  
        /* flags + rate selection */
  
 -      if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
 +      switch (priv->scan_band) {
 +      case IEEE80211_BAND_2GHZ:
                scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
                scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
                scan->good_CRC_th = 0;
                band = IEEE80211_BAND_2GHZ;
 -      } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
 +              break;
 +      case IEEE80211_BAND_5GHZ:
                scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
                /*
                 * If active scaning is requested but a certain channel
                 * is marked passive, we can do active scanning if we
                 * detect transmissions.
                 */
-               scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
+               scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
+                                               IWL_GOOD_CRC_TH_DISABLED;
                band = IEEE80211_BAND_5GHZ;
 -      } else {
 -              IWL_WARN(priv, "Invalid scan band count\n");
 +              break;
 +      default:
 +              IWL_WARN(priv, "Invalid scan band\n");
                goto done;
        }
  
 -      scan->tx_cmd.len = cpu_to_le16(
 +      if (!priv->is_internal_short_scan) {
 +              scan->tx_cmd.len = cpu_to_le16(
                        iwl_fill_probe_req(priv,
                                (struct ieee80211_mgmt *)scan->data,
                                priv->scan_request->ie,
                                priv->scan_request->ie_len,
                                IWL_MAX_SCAN_SIZE - sizeof(*scan)));
 -
 +      } else {
 +              scan->tx_cmd.len = cpu_to_le16(
 +                      iwl_fill_probe_req(priv,
 +                              (struct ieee80211_mgmt *)scan->data,
 +                              NULL, 0,
 +                              IWL_MAX_SCAN_SIZE - sizeof(*scan)));
 +      }
        /* select Rx antennas */
        scan->flags |= iwl3945_get_antenna_flags(priv);
  
 -      if (iwl_is_monitor_mode(priv))
 -              scan->filter_flags = RXON_FILTER_PROMISC_MSK;
 -
        scan->channel_count =
                iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
                        (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
        scan->len = cpu_to_le16(cmd.len);
  
        set_bit(STATUS_SCAN_HW, &priv->status);
 -      rc = iwl_send_cmd_sync(priv, &cmd);
 -      if (rc)
 +      if (iwl_send_cmd_sync(priv, &cmd))
                goto done;
  
        queue_delayed_work(priv->workqueue, &priv->scan_check,
                           IWL_SCAN_CHECK_WATCHDOG);
  
 -      mutex_unlock(&priv->mutex);
        return;
  
   done:
  
        /* inform mac80211 scan aborted */
        queue_work(priv->workqueue, &priv->scan_completed);
 -      mutex_unlock(&priv->mutex);
  }
  
  static void iwl3945_bg_restart(struct work_struct *data)
@@@ -3038,6 -3065,8 +3039,6 @@@ static void iwl3945_bg_rx_replenish(str
        mutex_unlock(&priv->mutex);
  }
  
 -#define IWL_DELAY_NEXT_SCAN (HZ*2)
 -
  void iwl3945_post_associate(struct iwl_priv *priv)
  {
        int rc = 0;
        case NL80211_IFTYPE_ADHOC:
  
                priv->assoc_id = 1;
 -              iwl_add_station(priv, priv->bssid, 0, CMD_SYNC, NULL);
 +              iwl_add_local_station(priv, priv->bssid, false);
                iwl3945_sync_sta(priv, IWL_STA_ID,
 -                               (priv->band == IEEE80211_BAND_5GHZ) ?
 -                               IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
 +                              (priv->band == IEEE80211_BAND_5GHZ) ?
 +                              IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
                                 CMD_ASYNC);
                iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
 +
                iwl3945_send_beacon_cmd(priv);
  
                break;
                           __func__, priv->iw_mode);
                break;
        }
 -
 -      iwl_activate_qos(priv, 0);
 -
 -      /* we have just associated, don't start scan too early */
 -      priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  }
  
  /*****************************************************************************
@@@ -3180,7 -3213,7 +3181,7 @@@ static int iwl3945_mac_start(struct iee
  
        /* ucode is running and will send rfkill notifications,
         * no need to poll the killswitch state anymore */
 -      cancel_delayed_work(&priv->rfkill_poll);
 +      cancel_delayed_work(&priv->_3945.rfkill_poll);
  
        iwl_led_start(priv);
  
@@@ -3221,7 -3254,7 +3222,7 @@@ static void iwl3945_mac_stop(struct iee
        flush_workqueue(priv->workqueue);
  
        /* start polling the killswitch state again */
 -      queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
 +      queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
                           round_jiffies_relative(2 * HZ));
  
        IWL_DEBUG_MAC80211(priv, "leave\n");
@@@ -3292,7 -3325,7 +3293,7 @@@ void iwl3945_config_ap(struct iwl_priv 
                /* restore RXON assoc */
                priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
                iwlcore_commit_rxon(priv);
 -              iwl_add_station(priv, iwl_bcast_addr, 0, CMD_SYNC, NULL);
 +              iwl_add_local_station(priv, iwl_bcast_addr, false);
        }
        iwl3945_send_beacon_cmd(priv);
  
@@@ -3333,6 -3366,7 +3334,6 @@@ static int iwl3945_mac_set_key(struct i
  
        mutex_lock(&priv->mutex);
        iwl_scan_cancel_timeout(priv, 100);
 -      mutex_unlock(&priv->mutex);
  
        switch (cmd) {
        case SET_KEY:
                ret = -EINVAL;
        }
  
 +      mutex_unlock(&priv->mutex);
        IWL_DEBUG_MAC80211(priv, "leave\n");
  
        return ret;
  }
  
 +static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
 +                             struct ieee80211_vif *vif,
 +                             struct ieee80211_sta *sta)
 +{
 +      struct iwl_priv *priv = hw->priv;
 +      int ret;
 +      bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
 +      u8 sta_id;
 +
 +      IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
 +                      sta->addr);
 +
 +      ret = iwl_add_station_common(priv, sta->addr, is_ap, &sta->ht_cap,
 +                                   &sta_id);
 +      if (ret) {
 +              IWL_ERR(priv, "Unable to add station %pM (%d)\n",
 +                      sta->addr, ret);
 +              /* Should we return success if return code is EEXIST ? */
 +              return ret;
 +      }
 +
 +      /* Initialize rate scaling */
 +      IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
 +                     sta->addr);
 +      iwl3945_rs_rate_init(priv, sta, sta_id);
 +
 +      return 0;
 +
 +
 +
 +      return ret;
 +}
  /*****************************************************************************
   *
   * sysfs attributes
@@@ -3590,7 -3591,7 +3591,7 @@@ static ssize_t store_measurement(struc
        struct iwl_priv *priv = dev_get_drvdata(d);
        struct ieee80211_measurement_params params = {
                .channel = le16_to_cpu(priv->active_rxon.channel),
 -              .start_time = cpu_to_le64(priv->last_tsf),
 +              .start_time = cpu_to_le64(priv->_3945.last_tsf),
                .duration = cpu_to_le16(1),
        };
        u8 type = IWL_MEASURE_BASIC;
@@@ -3654,6 -3655,44 +3655,6 @@@ static ssize_t show_channels(struct dev
  
  static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  
 -static ssize_t show_statistics(struct device *d,
 -                             struct device_attribute *attr, char *buf)
 -{
 -      struct iwl_priv *priv = dev_get_drvdata(d);
 -      u32 size = sizeof(struct iwl3945_notif_statistics);
 -      u32 len = 0, ofs = 0;
 -      u8 *data = (u8 *)&priv->statistics_39;
 -      int rc = 0;
 -
 -      if (!iwl_is_alive(priv))
 -              return -EAGAIN;
 -
 -      mutex_lock(&priv->mutex);
 -      rc = iwl_send_statistics_request(priv, CMD_SYNC, false);
 -      mutex_unlock(&priv->mutex);
 -
 -      if (rc) {
 -              len = sprintf(buf,
 -                            "Error sending statistics request: 0x%08X\n", rc);
 -              return len;
 -      }
 -
 -      while (size && (PAGE_SIZE - len)) {
 -              hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
 -                                 PAGE_SIZE - len, 1);
 -              len = strlen(buf);
 -              if (PAGE_SIZE - len)
 -                      buf[len++] = '\n';
 -
 -              ofs += 16;
 -              size -= min(size, 16U);
 -      }
 -
 -      return len;
 -}
 -
 -static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
 -
  static ssize_t show_antenna(struct device *d,
                            struct device_attribute *attr, char *buf)
  {
@@@ -3735,20 -3774,14 +3736,20 @@@ static void iwl3945_setup_deferred_work
        INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
        INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
        INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
 -      INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
 +      INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
        INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
 -      INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
        INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
        INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  
        iwl3945_hw_setup_deferred_work(priv);
  
 +      if (priv->cfg->ops->lib->recover_from_tx_stall) {
 +              init_timer(&priv->monitor_recover);
 +              priv->monitor_recover.data = (unsigned long)priv;
 +              priv->monitor_recover.function =
 +                      priv->cfg->ops->lib->recover_from_tx_stall;
 +      }
 +
        tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
                     iwl3945_irq_tasklet, (unsigned long)priv);
  }
@@@ -3761,8 -3794,6 +3762,8 @@@ static void iwl3945_cancel_deferred_wor
        cancel_delayed_work(&priv->scan_check);
        cancel_delayed_work(&priv->alive_start);
        cancel_work_sync(&priv->beacon_update);
 +      if (priv->cfg->ops->lib->recover_from_tx_stall)
 +              del_timer_sync(&priv->monitor_recover);
  }
  
  static struct attribute *iwl3945_sysfs_entries[] = {
        &dev_attr_filter_flags.attr,
        &dev_attr_measurement.attr,
        &dev_attr_retry_rate.attr,
 -      &dev_attr_statistics.attr,
        &dev_attr_status.attr,
        &dev_attr_temperature.attr,
        &dev_attr_tx_power.attr,
@@@ -3799,9 -3831,7 +3800,9 @@@ static struct ieee80211_ops iwl3945_hw_
        .conf_tx = iwl_mac_conf_tx,
        .reset_tsf = iwl_mac_reset_tsf,
        .bss_info_changed = iwl_bss_info_changed,
 -      .hw_scan = iwl_mac_hw_scan
 +      .hw_scan = iwl_mac_hw_scan,
 +      .sta_add = iwl3945_mac_sta_add,
 +      .sta_remove = iwl_mac_sta_remove,
  };
  
  static int iwl3945_init_drv(struct iwl_priv *priv)
        mutex_init(&priv->mutex);
        mutex_init(&priv->sync_cmd_mutex);
  
 -      /* Clear the driver's (not device's) station table */
 -      iwl_clear_stations_table(priv);
 -
        priv->ieee_channels = NULL;
        priv->ieee_rates = NULL;
        priv->band = IEEE80211_BAND_2GHZ;
        priv->iw_mode = NL80211_IFTYPE_STATION;
        priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  
 -      iwl_reset_qos(priv);
 -
 -      priv->qos_data.qos_active = 0;
 -      priv->qos_data.qos_cap.val = 0;
 -
 -      priv->rates_mask = IWL_RATES_MASK;
        priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
  
        if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
@@@ -4091,7 -4130,7 +4092,7 @@@ static int iwl3945_pci_probe(struct pci
                IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  
        /* Start monitoring the killswitch */
 -      queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
 +      queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
                           2 * HZ);
  
        return 0;
@@@ -4165,7 -4204,7 +4166,7 @@@ static void __devexit iwl3945_pci_remov
  
        sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  
 -      cancel_delayed_work_sync(&priv->rfkill_poll);
 +      cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
  
        iwl3945_dealloc_ucode_pci(priv);
  
        iwl3945_hw_txq_ctx_free(priv);
  
        iwl3945_unset_hw_params(priv);
 -      iwl_clear_stations_table(priv);
  
        /*netif_stop_queue(dev); */
        flush_workqueue(priv->workqueue);
  
        iwl_free_channel_map(priv);
        iwlcore_free_geos(priv);
 -      kfree(priv->scan);
 +      kfree(priv->scan_cmd);
        if (priv->ibss_beacon)
                dev_kfree_skb(priv->ibss_beacon);
  
@@@ -131,7 -131,7 +131,7 @@@ static int p54p_upload_firmware(struct 
  
  static void p54p_refill_rx_ring(struct ieee80211_hw *dev,
        int ring_index, struct p54p_desc *ring, u32 ring_limit,
 -      struct sk_buff **rx_buf)
 +      struct sk_buff **rx_buf, u32 index)
  {
        struct p54p_priv *priv = dev->priv;
        struct p54p_ring_control *ring_control = priv->ring_control;
  
        idx = le32_to_cpu(ring_control->host_idx[ring_index]);
        limit = idx;
 -      limit -= le32_to_cpu(ring_control->device_idx[ring_index]);
 +      limit -= index;
        limit = ring_limit - limit;
  
        i = idx % ring_limit;
@@@ -231,7 -231,7 +231,7 @@@ static void p54p_check_rx_ring(struct i
                i %= ring_limit;
        }
  
 -      p54p_refill_rx_ring(dev, ring_index, ring, ring_limit, rx_buf);
 +      p54p_refill_rx_ring(dev, ring_index, ring, ring_limit, rx_buf, *index);
  }
  
  static void p54p_check_tx_ring(struct ieee80211_hw *dev, u32 *index,
        u32 idx, i;
  
        i = (*index) % ring_limit;
-       (*index) = idx = le32_to_cpu(ring_control->device_idx[1]);
+       (*index) = idx = le32_to_cpu(ring_control->device_idx[ring_index]);
        idx %= ring_limit;
  
        while (i != idx) {
@@@ -444,10 -444,10 +444,10 @@@ static int p54p_open(struct ieee80211_h
        priv->rx_idx_mgmt = priv->tx_idx_mgmt = 0;
  
        p54p_refill_rx_ring(dev, 0, priv->ring_control->rx_data,
 -              ARRAY_SIZE(priv->ring_control->rx_data), priv->rx_buf_data);
 +              ARRAY_SIZE(priv->ring_control->rx_data), priv->rx_buf_data, 0);
  
        p54p_refill_rx_ring(dev, 2, priv->ring_control->rx_mgmt,
 -              ARRAY_SIZE(priv->ring_control->rx_mgmt), priv->rx_buf_mgmt);
 +              ARRAY_SIZE(priv->ring_control->rx_mgmt), priv->rx_buf_mgmt, 0);
  
        P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
        P54P_READ(ring_control_base);
diff --combined net/mac80211/agg-tx.c
@@@ -183,10 -183,9 +183,9 @@@ static void sta_addba_resp_timer_expire
                       HT_AGG_STATE_REQ_STOP_BA_MSK)) !=
                                                HT_ADDBA_REQUESTED_MSK) {
                spin_unlock_bh(&sta->lock);
-               *state = HT_AGG_STATE_IDLE;
  #ifdef CONFIG_MAC80211_HT_DEBUG
                printk(KERN_DEBUG "timer expired on tid %d but we are not "
 -                              "(or no longer) expecting addBA response there",
 +                              "(or no longer) expecting addBA response there\n",
                        tid);
  #endif
                return;
@@@ -214,8 -213,6 +213,8 @@@ int ieee80211_start_tx_ba_session(struc
        int ret = 0;
        u16 start_seq_num;
  
 +      trace_api_start_tx_ba_session(pubsta, tid);
 +
        if (WARN_ON(!local->ops->ampdu_action))
                return -EINVAL;
  
                return -EINVAL;
        }
  
 -      if (test_sta_flags(sta, WLAN_STA_SUSPEND)) {
 +      if (test_sta_flags(sta, WLAN_STA_BLOCK_BA)) {
  #ifdef CONFIG_MAC80211_HT_DEBUG
                printk(KERN_DEBUG "Suspend in progress. "
                       "Denying BA session request\n");
@@@ -416,7 -413,7 +415,7 @@@ static void ieee80211_agg_tx_operationa
                                         struct sta_info *sta, u16 tid)
  {
  #ifdef CONFIG_MAC80211_HT_DEBUG
 -      printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
 +      printk(KERN_DEBUG "Aggregation is on for tid %d\n", tid);
  #endif
  
        spin_lock(&local->ampdu_lock);
@@@ -442,8 -439,6 +441,8 @@@ void ieee80211_start_tx_ba_cb(struct ie
        struct sta_info *sta;
        u8 *state;
  
 +      trace_api_start_tx_ba_cb(sdata, ra, tid);
 +
        if (tid >= STA_TID_NUM) {
  #ifdef CONFIG_MAC80211_HT_DEBUG
                printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
@@@ -545,8 -540,6 +544,8 @@@ int ieee80211_stop_tx_ba_session(struc
        struct ieee80211_sub_if_data *sdata = sta->sdata;
        struct ieee80211_local *local = sdata->local;
  
 +      trace_api_stop_tx_ba_session(pubsta, tid, initiator);
 +
        if (!local->ops->ampdu_action)
                return -EINVAL;
  
@@@ -564,8 -557,6 +563,8 @@@ void ieee80211_stop_tx_ba_cb(struct iee
        struct sta_info *sta;
        u8 *state;
  
 +      trace_api_stop_tx_ba_cb(sdata, ra, tid);
 +
        if (tid >= STA_TID_NUM) {
  #ifdef CONFIG_MAC80211_HT_DEBUG
                printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
@@@ -682,7 -673,7 +681,7 @@@ void ieee80211_process_addba_resp(struc
        del_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
  
  #ifdef CONFIG_MAC80211_HT_DEBUG
 -      printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
 +      printk(KERN_DEBUG "switched off addBA timer for tid %d\n", tid);
  #endif /* CONFIG_MAC80211_HT_DEBUG */
  
        if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
diff --combined net/mac80211/mlme.c
   */
  #define IEEE80211_PROBE_WAIT          (HZ / 2)
  
 +/*
 + * Weight given to the latest Beacon frame when calculating average signal
 + * strength for Beacon frames received in the current BSS. This must be
 + * between 1 and 15.
 + */
 +#define IEEE80211_SIGNAL_AVE_WEIGHT   3
 +
  #define TMR_RUNNING_TIMER     0
  #define TMR_RUNNING_CHANSW    1
  
@@@ -174,6 -167,8 +174,8 @@@ static u32 ieee80211_enable_ht(struct i
        ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
                     channel_type != local->hw.conf.channel_type;
  
+       if (local->tmp_channel)
+               local->tmp_channel_type = channel_type;
        local->oper_channel_type = channel_type;
  
        if (ht_changed) {
  
  static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
                                           const u8 *bssid, u16 stype, u16 reason,
 -                                         void *cookie)
 +                                         void *cookie, bool send_frame)
  {
        struct ieee80211_local *local = sdata->local;
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
                        cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
        if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
                IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
 -      ieee80211_tx_skb(sdata, skb);
 +
 +      if (send_frame)
 +              ieee80211_tx_skb(sdata, skb);
 +      else
 +              kfree_skb(skb);
  }
  
  void ieee80211_send_pspoll(struct ieee80211_local *local,
@@@ -475,7 -466,6 +477,7 @@@ void ieee80211_recalc_ps(struct ieee802
  {
        struct ieee80211_sub_if_data *sdata, *found = NULL;
        int count = 0;
 +      int timeout;
  
        if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
                local->ps_sdata = NULL;
                beaconint_us = ieee80211_tu_to_usec(
                                        found->vif.bss_conf.beacon_int);
  
 +              timeout = local->hw.conf.dynamic_ps_forced_timeout;
 +              if (timeout < 0) {
 +                      /*
 +                       * The 2 second value is there for compatibility until
 +                       * the PM_QOS_NETWORK_LATENCY is configured with real
 +                       * values.
 +                       */
 +                      if (latency == 2000000000)
 +                              timeout = 100;
 +                      else if (latency <= 50000)
 +                              timeout = 300;
 +                      else if (latency <= 100000)
 +                              timeout = 100;
 +                      else if (latency <= 500000)
 +                              timeout = 50;
 +                      else
 +                              timeout = 0;
 +              }
 +              local->hw.conf.dynamic_ps_timeout = timeout;
 +
                if (beaconint_us > latency) {
                        local->ps_sdata = NULL;
                } else {
@@@ -621,9 -591,6 +623,9 @@@ static void ieee80211_sta_wmm_params(st
        int count;
        u8 *pos, uapsd_queues = 0;
  
 +      if (!local->ops->conf_tx)
 +              return;
 +
        if (local->hw.queues < 4)
                return;
  
                       params.aifs, params.cw_min, params.cw_max, params.txop,
                       params.uapsd);
  #endif
 -              if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
 +              if (drv_conf_tx(local, queue, &params))
                        printk(KERN_DEBUG "%s: failed to set TX queue "
                               "parameters for queue %d\n",
                               wiphy_name(local->hw.wiphy), queue);
        }
 +
 +      /* enable WMM or activate new settings */
 +      local->hw.conf.flags |= IEEE80211_CONF_QOS;
 +      drv_config(local, IEEE80211_CONF_CHANGE_QOS);
  }
  
  static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
@@@ -767,8 -730,6 +769,8 @@@ static void ieee80211_set_associated(st
        sdata->u.mgd.associated = cbss;
        memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  
 +      sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
 +
        /* just to be sure */
        sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
                                IEEE80211_STA_BEACON_POLL);
        /* And the BSSID changed - we're associated now */
        bss_info_changed |= BSS_CHANGED_BSSID;
  
 +      /* Tell the driver to monitor connection quality (if supported) */
 +      if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
 +          sdata->vif.bss_conf.cqm_rssi_thold)
 +              bss_info_changed |= BSS_CHANGED_CQM;
 +
        ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  
        mutex_lock(&local->iflist_mtx);
        netif_carrier_on(sdata->dev);
  }
  
 -static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
 +static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
 +                                 bool remove_sta)
  {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_local *local = sdata->local;
        changed |= BSS_CHANGED_BSSID;
        ieee80211_bss_info_change_notify(sdata, changed);
  
 -      sta_info_destroy_addr(sdata, bssid);
 +      if (remove_sta)
 +              sta_info_destroy_addr(sdata, bssid);
  }
  
  void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
        if (is_multicast_ether_addr(hdr->addr1))
                return;
  
 +      if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
 +              return;
 +
        mod_timer(&sdata->u.mgd.conn_mon_timer,
                  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  }
@@@ -982,72 -933,23 +984,72 @@@ static void ieee80211_mgd_probe_ap(stru
        mutex_unlock(&ifmgd->mtx);
  }
  
 -void ieee80211_beacon_loss_work(struct work_struct *work)
 +static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
 +{
 +      struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 +      struct ieee80211_local *local = sdata->local;
 +      u8 bssid[ETH_ALEN];
 +
 +      mutex_lock(&ifmgd->mtx);
 +      if (!ifmgd->associated) {
 +              mutex_unlock(&ifmgd->mtx);
 +              return;
 +      }
 +
 +      memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
 +
 +      printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
 +
 +      ieee80211_set_disassoc(sdata, true);
 +      ieee80211_recalc_idle(local);
 +      mutex_unlock(&ifmgd->mtx);
 +      /*
 +       * must be outside lock due to cfg80211,
 +       * but that's not a problem.
 +       */
 +      ieee80211_send_deauth_disassoc(sdata, bssid,
 +                                     IEEE80211_STYPE_DEAUTH,
 +                                     WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
 +                                     NULL, true);
 +}
 +
 +void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  {
        struct ieee80211_sub_if_data *sdata =
                container_of(work, struct ieee80211_sub_if_data,
 -                           u.mgd.beacon_loss_work);
 +                           u.mgd.beacon_connection_loss_work);
  
 -      ieee80211_mgd_probe_ap(sdata, true);
 +      if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
 +              __ieee80211_connection_loss(sdata);
 +      else
 +              ieee80211_mgd_probe_ap(sdata, true);
  }
  
  void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  {
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 +      struct ieee80211_hw *hw = &sdata->local->hw;
  
 -      ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
 +      trace_api_beacon_loss(sdata);
 +
 +      WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
 +      ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  }
  EXPORT_SYMBOL(ieee80211_beacon_loss);
  
 +void ieee80211_connection_loss(struct ieee80211_vif *vif)
 +{
 +      struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 +      struct ieee80211_hw *hw = &sdata->local->hw;
 +
 +      trace_api_connection_loss(sdata);
 +
 +      WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
 +      ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
 +}
 +EXPORT_SYMBOL(ieee80211_connection_loss);
 +
 +
  static enum rx_mgmt_action __must_check
  ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
                         struct ieee80211_mgmt *mgmt, size_t len)
        printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
                        sdata->name, bssid, reason_code);
  
 -      ieee80211_set_disassoc(sdata);
 +      ieee80211_set_disassoc(sdata, true);
        ieee80211_recalc_idle(sdata->local);
  
        return RX_MGMT_CFG80211_DEAUTH;
@@@ -1098,7 -1000,7 +1100,7 @@@ ieee80211_rx_mgmt_disassoc(struct ieee8
        printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
                        sdata->name, mgmt->sa, reason_code);
  
 -      ieee80211_set_disassoc(sdata);
 +      ieee80211_set_disassoc(sdata, true);
        ieee80211_recalc_idle(sdata->local);
        return RX_MGMT_CFG80211_DISASSOC;
  }
@@@ -1351,17 -1253,12 +1353,17 @@@ static void ieee80211_rx_mgmt_probe_res
                mutex_lock(&sdata->local->iflist_mtx);
                ieee80211_recalc_ps(sdata->local, -1);
                mutex_unlock(&sdata->local->iflist_mtx);
 +
 +              if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
 +                      return;
 +
                /*
                 * We've received a probe response, but are not sure whether
                 * we have or will be receiving any beacons or data, so let's
                 * schedule the timers again, just in case.
                 */
                mod_beacon_timer(sdata);
 +
                mod_timer(&ifmgd->conn_mon_timer,
                          round_jiffies_up(jiffies +
                                           IEEE80211_CONNECTION_IDLE_TIME));
@@@ -1395,7 -1292,6 +1397,7 @@@ static void ieee80211_rx_mgmt_beacon(st
                                     struct ieee80211_rx_status *rx_status)
  {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 +      struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
        size_t baselen;
        struct ieee802_11_elems elems;
        struct ieee80211_local *local = sdata->local;
        if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
                return;
  
 +      /* Track average RSSI from the Beacon frames of the current AP */
 +      ifmgd->last_beacon_signal = rx_status->signal;
 +      if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
 +              ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
 +              ifmgd->ave_beacon_signal = rx_status->signal;
 +              ifmgd->last_cqm_event_signal = 0;
 +      } else {
 +              ifmgd->ave_beacon_signal =
 +                      (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
 +                       (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
 +                       ifmgd->ave_beacon_signal) / 16;
 +      }
 +      if (bss_conf->cqm_rssi_thold &&
 +          !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
 +              int sig = ifmgd->ave_beacon_signal / 16;
 +              int last_event = ifmgd->last_cqm_event_signal;
 +              int thold = bss_conf->cqm_rssi_thold;
 +              int hyst = bss_conf->cqm_rssi_hyst;
 +              if (sig < thold &&
 +                  (last_event == 0 || sig < last_event - hyst)) {
 +                      ifmgd->last_cqm_event_signal = sig;
 +                      ieee80211_cqm_rssi_notify(
 +                              &sdata->vif,
 +                              NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
 +                              GFP_KERNEL);
 +              } else if (sig > thold &&
 +                         (last_event == 0 || sig > last_event + hyst)) {
 +                      ifmgd->last_cqm_event_signal = sig;
 +                      ieee80211_cqm_rssi_notify(
 +                              &sdata->vif,
 +                              NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
 +                              GFP_KERNEL);
 +              }
 +      }
 +
        if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
                if (net_ratelimit()) {
@@@ -1751,7 -1612,7 +1753,7 @@@ static void ieee80211_sta_work(struct w
                        printk(KERN_DEBUG "No probe response from AP %pM"
                                " after %dms, disconnecting.\n",
                                bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
 -                      ieee80211_set_disassoc(sdata);
 +                      ieee80211_set_disassoc(sdata, true);
                        ieee80211_recalc_idle(local);
                        mutex_unlock(&ifmgd->mtx);
                        /*
                        ieee80211_send_deauth_disassoc(sdata, bssid,
                                        IEEE80211_STYPE_DEAUTH,
                                        WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
 -                                      NULL);
 +                                      NULL, true);
                        mutex_lock(&ifmgd->mtx);
                }
        }
@@@ -1778,8 -1639,7 +1780,8 @@@ static void ieee80211_sta_bcn_mon_timer
        if (local->quiescing)
                return;
  
 -      ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
 +      ieee80211_queue_work(&sdata->local->hw,
 +                           &sdata->u.mgd.beacon_connection_loss_work);
  }
  
  static void ieee80211_sta_conn_mon_timer(unsigned long data)
@@@ -1831,7 -1691,7 +1833,7 @@@ void ieee80211_sta_quiesce(struct ieee8
         */
  
        cancel_work_sync(&ifmgd->work);
 -      cancel_work_sync(&ifmgd->beacon_loss_work);
 +      cancel_work_sync(&ifmgd->beacon_connection_loss_work);
        if (del_timer_sync(&ifmgd->timer))
                set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  
@@@ -1865,8 -1725,7 +1867,8 @@@ void ieee80211_sta_setup_sdata(struct i
        INIT_WORK(&ifmgd->work, ieee80211_sta_work);
        INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
        INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
 -      INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
 +      INIT_WORK(&ifmgd->beacon_connection_loss_work,
 +                ieee80211_beacon_connection_loss_work);
        setup_timer(&ifmgd->timer, ieee80211_sta_timer,
                    (unsigned long) sdata);
        setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
@@@ -1945,9 -1804,6 +1947,9 @@@ int ieee80211_mgd_auth(struct ieee80211
        struct ieee80211_work *wk;
        u16 auth_alg;
  
 +      if (req->local_state_change)
 +              return 0; /* no need to update mac80211 state */
 +
        switch (req->auth_type) {
        case NL80211_AUTHTYPE_OPEN_SYSTEM:
                auth_alg = WLAN_AUTH_OPEN;
@@@ -2056,7 -1912,7 +2058,7 @@@ int ieee80211_mgd_assoc(struct ieee8021
                }
  
                /* Trying to reassociate - clear previous association state */
 -              ieee80211_set_disassoc(sdata);
 +              ieee80211_set_disassoc(sdata, true);
        }
        mutex_unlock(&ifmgd->mtx);
  
@@@ -2160,7 -2016,7 +2162,7 @@@ int ieee80211_mgd_deauth(struct ieee802
  
        if (ifmgd->associated == req->bss) {
                bssid = req->bss->bssid;
 -              ieee80211_set_disassoc(sdata);
 +              ieee80211_set_disassoc(sdata, true);
                mutex_unlock(&ifmgd->mtx);
        } else {
                bool not_auth_yet = false;
        printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
               sdata->name, bssid, req->reason_code);
  
 -      ieee80211_send_deauth_disassoc(sdata, bssid,
 -                      IEEE80211_STYPE_DEAUTH, req->reason_code,
 -                      cookie);
 +      ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
 +                                     req->reason_code, cookie,
 +                                     !req->local_state_change);
  
        ieee80211_recalc_idle(sdata->local);
  
@@@ -2217,7 -2073,6 +2219,7 @@@ int ieee80211_mgd_disassoc(struct ieee8
                           void *cookie)
  {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 +      u8 bssid[ETH_ALEN];
  
        mutex_lock(&ifmgd->mtx);
  
        printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
               sdata->name, req->bss->bssid, req->reason_code);
  
 -      ieee80211_set_disassoc(sdata);
 +      memcpy(bssid, req->bss->bssid, ETH_ALEN);
 +      ieee80211_set_disassoc(sdata, false);
  
        mutex_unlock(&ifmgd->mtx);
  
        ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
                        IEEE80211_STYPE_DISASSOC, req->reason_code,
 -                      cookie);
 +                      cookie, !req->local_state_change);
 +      sta_info_destroy_addr(sdata, bssid);
  
        ieee80211_recalc_idle(sdata->local);
  
@@@ -2284,15 -2137,3 +2286,15 @@@ int ieee80211_mgd_action(struct ieee802
        *cookie = (unsigned long) skb;
        return 0;
  }
 +
 +void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
 +                             enum nl80211_cqm_rssi_threshold_event rssi_event,
 +                             gfp_t gfp)
 +{
 +      struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 +
 +      trace_api_cqm_rssi_notify(sdata, rssi_event);
 +
 +      cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
 +}
 +EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);