iwlwifi: unify tx antenna toggling
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-tx.c
index 98c434c..b047fd1 100644 (file)
@@ -63,7 +63,7 @@ static const u16 default_tid_to_tx_fifo[] = {
  * Does NOT advance any TFD circular buffer read/write indexes
  * Does NOT free the TFD itself (which is within circular buffer)
  */
-int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
+static int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
 {
        struct iwl_tfd_frame *bd_tmp = (struct iwl_tfd_frame *)&txq->bd[0];
        struct iwl_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
@@ -72,10 +72,6 @@ int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
        int counter = 0;
        int index, is_odd;
 
-       /* Host command buffers stay mapped in memory, nothing to clean */
-       if (txq->q.id == IWL_CMD_QUEUE_NUM)
-               return 0;
-
        /* Sanity check on number of chunks */
        counter = IWL_GET_BITS(*bd, num_tbs);
        if (counter > MAX_NUM_OF_TBS) {
@@ -115,10 +111,8 @@ int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
        }
        return 0;
 }
-EXPORT_SYMBOL(iwl_hw_txq_free_tfd);
 
-
-int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
+static int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
                                 dma_addr_t addr, u16 len)
 {
        int index, is_odd;
@@ -126,7 +120,7 @@ int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
        u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
 
        /* Each TFD can point to a maximum 20 Tx buffers */
-       if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
+       if (num_tbs >= MAX_NUM_OF_TBS) {
                IWL_ERROR("Error can not send more than %d chunks\n",
                          MAX_NUM_OF_TBS);
                return -EINVAL;
@@ -151,7 +145,6 @@ int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
 
        return 0;
 }
-EXPORT_SYMBOL(iwl_hw_txq_attach_buf_to_tfd);
 
 /**
  * iwl_txq_update_write_ptr - Send new write index to hardware
@@ -208,11 +201,12 @@ EXPORT_SYMBOL(iwl_txq_update_write_ptr);
  * Free all buffers.
  * 0-fill, but do not free "txq" descriptor structure.
  */
-static void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
+static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
 {
+       struct iwl_tx_queue *txq = &priv->txq[txq_id];
        struct iwl_queue *q = &txq->q;
        struct pci_dev *dev = priv->pci_dev;
-       int len;
+       int i, len;
 
        if (q->n_bd == 0)
                return;
@@ -223,11 +217,10 @@ static void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
                iwl_hw_txq_free_tfd(priv, txq);
 
        len = sizeof(struct iwl_cmd) * q->n_window;
-       if (q->id == IWL_CMD_QUEUE_NUM)
-               len += IWL_MAX_SCAN_SIZE;
 
        /* De-alloc array of command/tx buffers */
-       pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
+       for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
+               kfree(txq->cmd[i]);
 
        /* De-alloc circular buffer of TFDs */
        if (txq->q.n_bd)
@@ -242,6 +235,40 @@ static void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
        memset(txq, 0, sizeof(*txq));
 }
 
+
+/**
+ * iwl_cmd_queue_free - Deallocate DMA queue.
+ * @txq: Transmit queue to deallocate.
+ *
+ * Empty queue by removing and destroying all BD's.
+ * Free all buffers.
+ * 0-fill, but do not free "txq" descriptor structure.
+ */
+static void iwl_cmd_queue_free(struct iwl_priv *priv)
+{
+       struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
+       struct iwl_queue *q = &txq->q;
+       struct pci_dev *dev = priv->pci_dev;
+       int i, len;
+
+       if (q->n_bd == 0)
+               return;
+
+       len = sizeof(struct iwl_cmd) * q->n_window;
+       len += IWL_MAX_SCAN_SIZE;
+
+       /* De-alloc array of command/tx buffers */
+       for (i = 0; i <= TFD_CMD_SLOTS; i++)
+               kfree(txq->cmd[i]);
+
+       /* De-alloc circular buffer of TFDs */
+       if (txq->q.n_bd)
+               pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
+                                   txq->q.n_bd, txq->bd, txq->q.dma_addr);
+
+       /* 0-fill queue descriptor structure */
+       memset(txq, 0, sizeof(*txq));
+}
 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
  * DMA services
  *
@@ -396,13 +423,11 @@ static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
 /**
  * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  */
-static int iwl_tx_queue_init(struct iwl_priv *priv,
-                            struct iwl_tx_queue *txq,
+static int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
                             int slots_num, u32 txq_id)
 {
-       struct pci_dev *dev = priv->pci_dev;
-       int len;
-       int rc = 0;
+       int i, len;
+       int ret;
 
        /*
         * Alloc buffer array for commands (Tx or other types of commands).
@@ -412,20 +437,25 @@ static int iwl_tx_queue_init(struct iwl_priv *priv,
         * For normal Tx queues (all other queues), no super-size command
         * space is needed.
         */
-       len = sizeof(struct iwl_cmd) * slots_num;
-       if (txq_id == IWL_CMD_QUEUE_NUM)
-               len +=  IWL_MAX_SCAN_SIZE;
-       txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
-       if (!txq->cmd)
-               return -ENOMEM;
+       len = sizeof(struct iwl_cmd);
+       for (i = 0; i <= slots_num; i++) {
+               if (i == slots_num) {
+                       if (txq_id == IWL_CMD_QUEUE_NUM)
+                               len += IWL_MAX_SCAN_SIZE;
+                       else
+                               continue;
+               }
+
+               txq->cmd[i] = kmalloc(len, GFP_KERNEL);
+               if (!txq->cmd[i])
+                       goto err;
+       }
 
        /* Alloc driver data array and TFD circular buffer */
-       rc = iwl_tx_queue_alloc(priv, txq, txq_id);
-       if (rc) {
-               pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
+       ret = iwl_tx_queue_alloc(priv, txq, txq_id);
+       if (ret)
+               goto err;
 
-               return -ENOMEM;
-       }
        txq->need_update = 0;
 
        /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
@@ -439,6 +469,17 @@ static int iwl_tx_queue_init(struct iwl_priv *priv,
        iwl_hw_tx_queue_init(priv, txq);
 
        return 0;
+err:
+       for (i = 0; i < slots_num; i++) {
+               kfree(txq->cmd[i]);
+               txq->cmd[i] = NULL;
+       }
+
+       if (txq_id == IWL_CMD_QUEUE_NUM) {
+               kfree(txq->cmd[slots_num]);
+               txq->cmd[slots_num] = NULL;
+       }
+       return -ENOMEM;
 }
 /**
  * iwl_hw_txq_ctx_free - Free TXQ Context
@@ -451,14 +492,16 @@ void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
 
        /* Tx queues */
        for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
-               iwl_tx_queue_free(priv, &priv->txq[txq_id]);
+               if (txq_id == IWL_CMD_QUEUE_NUM)
+                       iwl_cmd_queue_free(priv);
+               else
+                       iwl_tx_queue_free(priv, txq_id);
 
        /* Keep-warm buffer */
        iwl_kw_free(priv);
 }
 EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
 
-
 /**
  * iwl_txq_ctx_reset - Reset TX queue context
  * Destroys all DMA structures and initialise them again
@@ -480,7 +523,7 @@ int iwl_txq_ctx_reset(struct iwl_priv *priv)
        /* Alloc keep-warm buffer */
        ret = iwl_kw_alloc(priv);
        if (ret) {
-               IWL_ERROR("Keep Warm allocation failed");
+               IWL_ERROR("Keep Warm allocation failed\n");
                goto error_kw;
        }
        spin_lock_irqsave(&priv->lock, flags);
@@ -525,6 +568,7 @@ int iwl_txq_ctx_reset(struct iwl_priv *priv)
  error_kw:
        return ret;
 }
+
 /**
  * iwl_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  */
@@ -590,7 +634,7 @@ static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
 
 
        tx_cmd->sta_id = std_id;
-       if (ieee80211_get_morefrag(hdr))
+       if (ieee80211_has_morefrags(fc))
                tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
 
        if (ieee80211_is_data_qos(fc)) {
@@ -601,13 +645,7 @@ static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
                tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
        }
 
-       if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
-               tx_flags |= TX_CMD_FLG_RTS_MSK;
-               tx_flags &= ~TX_CMD_FLG_CTS_MSK;
-       } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
-               tx_flags &= ~TX_CMD_FLG_RTS_MSK;
-               tx_flags |= TX_CMD_FLG_CTS_MSK;
-       }
+       priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags);
 
        if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
                tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
@@ -636,11 +674,11 @@ static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
                              __le16 fc, int sta_id,
                              int is_hcca)
 {
+       u32 rate_flags = 0;
+       int rate_idx;
        u8 rts_retry_limit = 0;
        u8 data_retry_limit = 0;
        u8 rate_plcp;
-       u16 rate_flags = 0;
-       int rate_idx;
 
        rate_idx = min(ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xffff,
                        IWL_RATE_COUNT - 1);
@@ -683,14 +721,8 @@ static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
                        break;
                }
 
-               /* Alternate between antenna A and B for successive frames */
-               if (priv->use_ant_b_for_management_frame) {
-                       priv->use_ant_b_for_management_frame = 0;
-                       rate_flags |= RATE_MCS_ANT_B_MSK;
-               } else {
-                       priv->use_ant_b_for_management_frame = 1;
-                       rate_flags |= RATE_MCS_ANT_A_MSK;
-               }
+               priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant);
+               rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
        }
 
        tx_cmd->rts_retry_limit = rts_retry_limit;
@@ -757,20 +789,19 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
        struct iwl_tfd_frame *tfd;
-       u32 *control_flags;
-       int txq_id = skb_get_queue_mapping(skb);
-       struct iwl_tx_queue *txq = NULL;
-       struct iwl_queue *q = NULL;
+       struct iwl_tx_queue *txq;
+       struct iwl_queue *q;
+       struct iwl_cmd *out_cmd;
+       struct iwl_tx_cmd *tx_cmd;
+       int swq_id, txq_id;
        dma_addr_t phys_addr;
        dma_addr_t txcmd_phys;
        dma_addr_t scratch_phys;
-       struct iwl_cmd *out_cmd = NULL;
-       struct iwl_tx_cmd *tx_cmd;
        u16 len, idx, len_org;
        u16 seq_number = 0;
-       u8 id, hdr_len, unicast;
-       u8 sta_id;
        __le16 fc;
+       u8 hdr_len, unicast;
+       u8 sta_id;
        u8 wait_write_ptr = 0;
        u8 tid = 0;
        u8 *qc = NULL;
@@ -783,11 +814,6 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
                goto drop_unlock;
        }
 
-       if (!priv->vif) {
-               IWL_DEBUG_DROP("Dropping - !priv->vif\n");
-               goto drop_unlock;
-       }
-
        if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) ==
             IWL_INVALID_RATE) {
                IWL_ERROR("ERROR: No TX rate available.\n");
@@ -795,7 +821,6 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        }
 
        unicast = !is_multicast_ether_addr(hdr->addr1);
-       id = 0;
 
        fc = hdr->frame_control;
 
@@ -810,37 +835,39 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
 
        /* drop all data frame if we are not associated */
        if (ieee80211_is_data(fc) &&
-          (!iwl_is_associated(priv) ||
-           ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
-           !priv->assoc_station_added)) {
+           (priv->iw_mode != NL80211_IFTYPE_MONITOR ||
+           !(info->flags & IEEE80211_TX_CTL_INJECTED)) && /* packet injection */
+           (!iwl_is_associated(priv) ||
+            ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id) ||
+            !priv->assoc_station_added)) {
                IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
                goto drop_unlock;
        }
 
        spin_unlock_irqrestore(&priv->lock, flags);
 
-       hdr_len = ieee80211_get_hdrlen(le16_to_cpu(fc));
+       hdr_len = ieee80211_hdrlen(fc);
 
        /* Find (or create) index into station table for destination station */
        sta_id = iwl_get_sta_id(priv, hdr);
        if (sta_id == IWL_INVALID_STATION) {
-               DECLARE_MAC_BUF(mac);
-
-               IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
-                              print_mac(mac, hdr->addr1));
+               IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
+                              hdr->addr1);
                goto drop;
        }
 
        IWL_DEBUG_TX("station Id %d\n", sta_id);
 
+       swq_id = skb_get_queue_mapping(skb);
+       txq_id = swq_id;
        if (ieee80211_is_data_qos(fc)) {
                qc = ieee80211_get_qos_ctl(hdr);
-               tid = qc[0] & 0xf;
-               seq_number = priv->stations[sta_id].tid[tid].seq_number &
-                               IEEE80211_SCTL_SEQ;
-               hdr->seq_ctrl = cpu_to_le16(seq_number) |
-                       (hdr->seq_ctrl &
-                               __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
+               tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
+               seq_number = priv->stations[sta_id].tid[tid].seq_number;
+               seq_number &= IEEE80211_SCTL_SEQ;
+               hdr->seq_ctrl = hdr->seq_ctrl &
+                               __constant_cpu_to_le16(IEEE80211_SCTL_FRAG);
+               hdr->seq_ctrl |= cpu_to_le16(seq_number);
                seq_number += 0x10;
                /* aggregation is on for this <sta,tid> */
                if (info->flags & IEEE80211_TX_CTL_AMPDU)
@@ -857,7 +884,6 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        /* Set up first empty TFD within this queue's circular TFD buffer */
        tfd = &txq->bd[q->write_ptr];
        memset(tfd, 0, sizeof(*tfd));
-       control_flags = (u32 *) tfd;
        idx = get_cmd_index(q, q->write_ptr, 0);
 
        /* Set up driver data for this TFD */
@@ -865,7 +891,7 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        txq->txb[q->write_ptr].skb[0] = skb;
 
        /* Set up first empty entry in queue's array of Tx/cmd buffers */
-       out_cmd = &txq->cmd[idx];
+       out_cmd = txq->cmd[idx];
        tx_cmd = &out_cmd->cmd.tx;
        memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
        memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
@@ -905,14 +931,15 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
 
        /* Physical address of this Tx command's header (not MAC header!),
         * within command buffer array. */
-       txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
-                    offsetof(struct iwl_cmd, hdr);
+       txcmd_phys = pci_map_single(priv->pci_dev, out_cmd,
+                               sizeof(struct iwl_cmd), PCI_DMA_TODEVICE);
+       txcmd_phys += offsetof(struct iwl_cmd, hdr);
 
        /* Add buffer containing Tx command and MAC(!) header to TFD's
         * first entry */
        iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
 
-       if (!(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
+       if (info->control.hw_key)
                iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
 
        /* Set up TFD's 2nd entry to point directly to remainder of skb,
@@ -944,7 +971,7 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
        tx_cmd->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
 
-       if (!ieee80211_get_morefrag(hdr)) {
+       if (!ieee80211_has_morefrags(hdr->frame_control)) {
                txq->need_update = 1;
                if (qc)
                        priv->stations[sta_id].tid[tid].seq_number = seq_number;
@@ -968,16 +995,15 @@ int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
        if (ret)
                return ret;
 
-       if ((iwl_queue_space(q) < q->high_mark)
-           && priv->mac80211_registered) {
+       if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
                if (wait_write_ptr) {
                        spin_lock_irqsave(&priv->lock, flags);
                        txq->need_update = 1;
                        iwl_txq_update_write_ptr(priv, txq);
                        spin_unlock_irqrestore(&priv->lock, flags);
+               } else {
+                       ieee80211_stop_queue(priv->hw, swq_id);
                }
-
-               ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
        }
 
        return 0;
@@ -1005,13 +1031,12 @@ int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
        struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
        struct iwl_queue *q = &txq->q;
        struct iwl_tfd_frame *tfd;
-       u32 *control_flags;
        struct iwl_cmd *out_cmd;
-       u32 idx;
-       u16 fix_size;
        dma_addr_t phys_addr;
-       int ret;
        unsigned long flags;
+       int len, ret;
+       u32 idx;
+       u16 fix_size;
 
        cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
        fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
@@ -1037,10 +1062,9 @@ int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
        tfd = &txq->bd[q->write_ptr];
        memset(tfd, 0, sizeof(*tfd));
 
-       control_flags = (u32 *) tfd;
 
        idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
-       out_cmd = &txq->cmd[idx];
+       out_cmd = txq->cmd[idx];
 
        out_cmd->hdr.cmd = cmd->id;
        memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
@@ -1053,18 +1077,34 @@ int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
        out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
                        INDEX_TO_SEQ(q->write_ptr));
        if (out_cmd->meta.flags & CMD_SIZE_HUGE)
-               out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
-
-       phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
-                       offsetof(struct iwl_cmd, hdr);
+               out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
+       len = (idx == TFD_CMD_SLOTS) ?
+                       IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
+       phys_addr = pci_map_single(priv->pci_dev, out_cmd, len,
+                                               PCI_DMA_TODEVICE);
+       phys_addr += offsetof(struct iwl_cmd, hdr);
        iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
 
-       IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
-                    "%d bytes at %d[%d]:%d\n",
-                    get_cmd_string(out_cmd->hdr.cmd),
-                    out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
-                    fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
-
+#ifdef CONFIG_IWLWIFI_DEBUG
+       switch (out_cmd->hdr.cmd) {
+       case REPLY_TX_LINK_QUALITY_CMD:
+       case SENSITIVITY_CMD:
+               IWL_DEBUG_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
+                               "%d bytes at %d[%d]:%d\n",
+                               get_cmd_string(out_cmd->hdr.cmd),
+                               out_cmd->hdr.cmd,
+                               le16_to_cpu(out_cmd->hdr.sequence), fix_size,
+                               q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
+                               break;
+       default:
+               IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
+                               "%d bytes at %d[%d]:%d\n",
+                               get_cmd_string(out_cmd->hdr.cmd),
+                               out_cmd->hdr.cmd,
+                               le16_to_cpu(out_cmd->hdr.sequence), fix_size,
+                               q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
+       }
+#endif
        txq->need_update = 1;
 
        /* Set up entry in queue's byte count circular buffer */
@@ -1121,6 +1161,9 @@ static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
 {
        struct iwl_tx_queue *txq = &priv->txq[txq_id];
        struct iwl_queue *q = &txq->q;
+       struct iwl_tfd_frame *bd = &txq->bd[index];
+       dma_addr_t dma_addr;
+       int is_odd, buf_len;
        int nfreed = 0;
 
        if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
@@ -1138,6 +1181,19 @@ static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
                                        q->write_ptr, q->read_ptr);
                        queue_work(priv->workqueue, &priv->restart);
                }
+               is_odd = (index/2) & 0x1;
+               if (is_odd) {
+                       dma_addr = IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
+                                       (IWL_GET_BITS(bd->pa[index],
+                                                       tb2_addr_hi20) << 16);
+                       buf_len = IWL_GET_BITS(bd->pa[index], tb2_len);
+               } else {
+                       dma_addr = le32_to_cpu(bd->pa[index].tb1_addr);
+                       buf_len = IWL_GET_BITS(bd->pa[index], tb1_len);
+               }
+
+               pci_unmap_single(priv->pci_dev, dma_addr, buf_len,
+                                PCI_DMA_TODEVICE);
                nfreed++;
        }
 }
@@ -1156,20 +1212,19 @@ void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
        u16 sequence = le16_to_cpu(pkt->hdr.sequence);
        int txq_id = SEQ_TO_QUEUE(sequence);
        int index = SEQ_TO_INDEX(sequence);
-       int huge = sequence & SEQ_HUGE_FRAME;
        int cmd_index;
+       bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
        struct iwl_cmd *cmd;
 
        /* If a Tx command is being handled and it isn't in the actual
         * command queue then there a command routing bug has been introduced
         * in the queue management code. */
-       if (txq_id != IWL_CMD_QUEUE_NUM)
-               IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
-                         txq_id, pkt->hdr.cmd);
-       BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
+       if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
+                "wrong command queue %d, command id 0x%X\n", txq_id, pkt->hdr.cmd))
+               return;
 
        cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
-       cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
+       cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
 
        /* Input error checking is done when commands are added to queue. */
        if (cmd->meta.flags & CMD_WANT_SKB) {
@@ -1212,15 +1267,14 @@ int iwl_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn)
        int ret;
        unsigned long flags;
        struct iwl_tid_data *tid_data;
-       DECLARE_MAC_BUF(mac);
 
        if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
                tx_fifo = default_tid_to_tx_fifo[tid];
        else
                return -EINVAL;
 
-       IWL_WARNING("%s on ra = %s tid = %d\n",
-                       __func__, print_mac(mac, ra), tid);
+       IWL_WARNING("%s on ra = %pM tid = %d\n",
+                       __func__, ra, tid);
 
        sta_id = iwl_find_station(priv, ra);
        if (sta_id == IWL_INVALID_STATION)
@@ -1265,7 +1319,6 @@ int iwl_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid)
        struct iwl_tid_data *tid_data;
        int ret, write_ptr, read_ptr;
        unsigned long flags;
-       DECLARE_MAC_BUF(mac);
 
        if (!ra) {
                IWL_ERROR("ra = NULL\n");
@@ -1350,7 +1403,149 @@ int iwl_txq_check_empty(struct iwl_priv *priv, int sta_id, u8 tid, int txq_id)
 }
 EXPORT_SYMBOL(iwl_txq_check_empty);
 
-#ifdef CONFIG_IWLWIF_DEBUG
+/**
+ * iwl_tx_status_reply_compressed_ba - Update tx status from block-ack
+ *
+ * Go through block-ack's bitmap of ACK'd frames, update driver's record of
+ * ACK vs. not.  This gets sent to mac80211, then to rate scaling algo.
+ */
+static int iwl_tx_status_reply_compressed_ba(struct iwl_priv *priv,
+                                struct iwl_ht_agg *agg,
+                                struct iwl_compressed_ba_resp *ba_resp)
+
+{
+       int i, sh, ack;
+       u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
+       u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
+       u64 bitmap;
+       int successes = 0;
+       struct ieee80211_tx_info *info;
+
+       if (unlikely(!agg->wait_for_ba))  {
+               IWL_ERROR("Received BA when not expected\n");
+               return -EINVAL;
+       }
+
+       /* Mark that the expected block-ack response arrived */
+       agg->wait_for_ba = 0;
+       IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
+
+       /* Calculate shift to align block-ack bits with our Tx window bits */
+       sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
+       if (sh < 0) /* tbw something is wrong with indices */
+               sh += 0x100;
+
+       /* don't use 64-bit values for now */
+       bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
+
+       if (agg->frame_count > (64 - sh)) {
+               IWL_DEBUG_TX_REPLY("more frames than bitmap size");
+               return -1;
+       }
+
+       /* check for success or failure according to the
+        * transmitted bitmap and block-ack bitmap */
+       bitmap &= agg->bitmap;
+
+       /* For each frame attempted in aggregation,
+        * update driver's record of tx frame's status. */
+       for (i = 0; i < agg->frame_count ; i++) {
+               ack = bitmap & (1ULL << i);
+               successes += !!ack;
+               IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
+                       ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
+                       agg->start_idx + i);
+       }
+
+       info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
+       memset(&info->status, 0, sizeof(info->status));
+       info->flags = IEEE80211_TX_STAT_ACK;
+       info->flags |= IEEE80211_TX_STAT_AMPDU;
+       info->status.ampdu_ack_map = successes;
+       info->status.ampdu_ack_len = agg->frame_count;
+       iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
+
+       IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
+
+       return 0;
+}
+
+/**
+ * iwl_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
+ *
+ * Handles block-acknowledge notification from device, which reports success
+ * of frames sent via aggregation.
+ */
+void iwl_rx_reply_compressed_ba(struct iwl_priv *priv,
+                                          struct iwl_rx_mem_buffer *rxb)
+{
+       struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
+       struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
+       int index;
+       struct iwl_tx_queue *txq = NULL;
+       struct iwl_ht_agg *agg;
+
+       /* "flow" corresponds to Tx queue */
+       u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
+
+       /* "ssn" is start of block-ack Tx window, corresponds to index
+        * (in Tx queue's circular buffer) of first TFD/frame in window */
+       u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
+
+       if (scd_flow >= priv->hw_params.max_txq_num) {
+               IWL_ERROR("BUG_ON scd_flow is bigger than number of queues\n");
+               return;
+       }
+
+       txq = &priv->txq[scd_flow];
+       agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
+
+       /* Find index just before block-ack window */
+       index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
+
+       /* TODO: Need to get this copy more safely - now good for debug */
+
+       IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %pM, "
+                          "sta_id = %d\n",
+                          agg->wait_for_ba,
+                          (u8 *) &ba_resp->sta_addr_lo32,
+                          ba_resp->sta_id);
+       IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
+                          "%d, scd_ssn = %d\n",
+                          ba_resp->tid,
+                          ba_resp->seq_ctl,
+                          (unsigned long long)le64_to_cpu(ba_resp->bitmap),
+                          ba_resp->scd_flow,
+                          ba_resp->scd_ssn);
+       IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
+                          agg->start_idx,
+                          (unsigned long long)agg->bitmap);
+
+       /* Update driver's record of ACK vs. not for each frame in window */
+       iwl_tx_status_reply_compressed_ba(priv, agg, ba_resp);
+
+       /* Release all TFDs before the SSN, i.e. all TFDs in front of
+        * block-ack window (we assume that they've been successfully
+        * transmitted ... if not, it's too late anyway). */
+       if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
+               /* calculate mac80211 ampdu sw queue to wake */
+               int ampdu_q =
+                  scd_flow - priv->hw_params.first_ampdu_q + priv->hw->queues;
+               int freed = iwl_tx_queue_reclaim(priv, scd_flow, index);
+               priv->stations[ba_resp->sta_id].
+                       tid[ba_resp->tid].tfds_in_queue -= freed;
+               if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
+                       priv->mac80211_registered &&
+                       agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
+                       ieee80211_wake_queue(priv->hw, ampdu_q);
+
+               iwl_txq_check_empty(priv, ba_resp->sta_id,
+                                   ba_resp->tid, scd_flow);
+       }
+}
+EXPORT_SYMBOL(iwl_rx_reply_compressed_ba);
+
+#ifdef CONFIG_IWLWIFI_DEBUG
 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
 
 const char *iwl_get_tx_fail_reason(u32 status)