]> err.no Git - linux-2.6/blobdiff - net/mac80211/tx.c
pkt_sched: Remove 'dev' member of struct Qdisc.
[linux-2.6] / net / mac80211 / tx.c
index 6268bbca148e9946c9ab8401108025dbefd3572d..9bd9faac3c3c365848f8bf5d0cd432b32c22d520 100644 (file)
@@ -52,9 +52,8 @@ static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdat
 static void ieee80211_dump_frame(const char *ifname, const char *title,
                                 const struct sk_buff *skb)
 {
-       const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
-       u16 fc;
-       int hdrlen;
+       const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+       unsigned int hdrlen;
        DECLARE_MAC_BUF(mac);
 
        printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
@@ -63,13 +62,12 @@ static void ieee80211_dump_frame(const char *ifname, const char *title,
                return;
        }
 
-       fc = le16_to_cpu(hdr->frame_control);
-       hdrlen = ieee80211_get_hdrlen(fc);
+       hdrlen = ieee80211_hdrlen(hdr->frame_control);
        if (hdrlen > skb->len)
                hdrlen = skb->len;
        if (hdrlen >= 4)
                printk(" FC=0x%04x DUR=0x%04x",
-                      fc, le16_to_cpu(hdr->duration_id));
+                   le16_to_cpu(hdr->frame_control), le16_to_cpu(hdr->duration_id));
        if (hdrlen >= 10)
                printk(" A1=%s", print_mac(mac, hdr->addr1));
        if (hdrlen >= 16)
@@ -87,8 +85,8 @@ static inline void ieee80211_dump_frame(const char *ifname, const char *title,
 }
 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
 
-static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
-                             int next_frag_len)
+static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
+                                int next_frag_len)
 {
        int rate, mrate, erp, dur, i;
        struct ieee80211_rate *txrate;
@@ -140,7 +138,7 @@ static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
 
        /* data/mgmt */
        if (0 /* FIX: data/mgmt during CFP */)
-               return 32768;
+               return cpu_to_le16(32768);
 
        if (group_addr) /* Group address as the destination - no ACK */
                return 0;
@@ -210,19 +208,7 @@ static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
                                tx->sdata->bss_conf.use_short_preamble);
        }
 
-       return dur;
-}
-
-static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
-                                           int queue)
-{
-       return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
-}
-
-static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
-                                           int queue)
-{
-       return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
+       return cpu_to_le16(dur);
 }
 
 static int inline is_ieee80211_device(struct net_device *dev,
@@ -234,7 +220,7 @@ static int inline is_ieee80211_device(struct net_device *dev,
 
 /* tx handlers */
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
 {
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
@@ -288,12 +274,12 @@ ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
 {
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
 
-       if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
+       if (ieee80211_hdrlen(hdr->frame_control) >= 24)
                ieee80211_include_sequence(tx->sdata, hdr);
 
        return TX_CONTINUE;
@@ -341,8 +327,10 @@ static void purge_old_ps_buffers(struct ieee80211_local *local)
        rcu_read_unlock();
 
        local->total_ps_buffered = total;
+#ifdef MAC80211_VERBOSE_PS_DEBUG
        printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
               wiphy_name(local->hw.wiphy), purged);
+#endif
 }
 
 static ieee80211_tx_result
@@ -372,11 +360,13 @@ ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
                        purge_old_ps_buffers(tx->local);
                if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
                    AP_MAX_BC_BUFFER) {
+#ifdef MAC80211_VERBOSE_PS_DEBUG
                        if (net_ratelimit()) {
                                printk(KERN_DEBUG "%s: BC TX buffer full - "
                                       "dropping the oldest frame\n",
                                       tx->dev->name);
                        }
+#endif
                        dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
                } else
                        tx->local->total_ps_buffered++;
@@ -417,11 +407,13 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
                        purge_old_ps_buffers(tx->local);
                if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
                        struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
+#ifdef MAC80211_VERBOSE_PS_DEBUG
                        if (net_ratelimit()) {
                                printk(KERN_DEBUG "%s: STA %s TX "
                                       "buffer full - dropping oldest frame\n",
                                       tx->dev->name, print_mac(mac, sta->addr));
                        }
+#endif
                        dev_kfree_skb(old);
                } else
                        tx->local->total_ps_buffered++;
@@ -446,7 +438,7 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
 {
        if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
@@ -458,7 +450,7 @@ ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
                return ieee80211_tx_h_multicast_ps_buf(tx);
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
 {
        struct ieee80211_key *key;
@@ -507,7 +499,7 @@ ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
 {
        struct rate_selection rsel;
@@ -551,12 +543,10 @@ ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
 {
-       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
-       u16 fc = le16_to_cpu(hdr->frame_control);
-       u16 dur;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
        struct ieee80211_supported_band *sband;
 
@@ -607,30 +597,19 @@ ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
        /* Transmit data frames using short preambles if the driver supports
         * short preambles at the selected rate and short preambles are
         * available on the network at the current point in time. */
-       if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
+       if (ieee80211_is_data(hdr->frame_control) &&
            (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
            tx->sdata->bss_conf.use_short_preamble &&
            (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
                info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
        }
 
-       /* Setup duration field for the first fragment of the frame. Duration
-        * for remaining fragments will be updated when they are being sent
-        * to low-level driver in ieee80211_tx(). */
-       dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
-                                (tx->flags & IEEE80211_TX_FRAGMENTED) ?
-                                tx->extra_frag[0]->len : 0);
-       hdr->duration_id = cpu_to_le16(dur);
-
        if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) ||
            (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)) {
-               struct ieee80211_supported_band *sband;
                struct ieee80211_rate *rate;
                s8 baserate = -1;
                int idx;
 
-               sband = tx->local->hw.wiphy->bands[tx->channel->band];
-
                /* Do not use multiple retry rates when using RTS/CTS */
                info->control.alt_retry_rate_idx = -1;
 
@@ -659,10 +638,10 @@ ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
 {
-       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
        size_t hdrlen, per_fragm, num_fragm, payload_len, left;
        struct sk_buff **frags, *first, *frag;
        int i;
@@ -675,17 +654,17 @@ ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
 
        /*
         * Warn when submitting a fragmented A-MPDU frame and drop it.
-        * This is an error and needs to be fixed elsewhere, but when
-        * done needs to take care of monitor interfaces (injection)
-        * etc.
+        * This scenario is handled in __ieee80211_tx_prepare but extra
+        * caution taken here as fragmented ampdu may cause Tx stop.
         */
        if (WARN_ON(tx->flags & IEEE80211_TX_CTL_AMPDU ||
-                   IEEE80211_SKB_CB(tx->skb)->queue >= tx->local->hw.queues))
+                   skb_get_queue_mapping(tx->skb) >=
+                       ieee80211_num_regular_queues(&tx->local->hw)))
                return TX_DROP;
 
        first = tx->skb;
 
-       hdrlen = ieee80211_get_hdrlen(tx->fc);
+       hdrlen = ieee80211_hdrlen(hdr->frame_control);
        payload_len = first->len - hdrlen;
        per_fragm = frag_threshold - hdrlen - FCS_LEN;
        num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
@@ -726,6 +705,8 @@ ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
                fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
                copylen = left > per_fragm ? per_fragm : left;
                memcpy(skb_put(frag, copylen), pos, copylen);
+               memcpy(frag->cb, first->cb, sizeof(frag->cb));
+               skb_copy_queue_mapping(frag, first);
 
                pos += copylen;
                left -= copylen;
@@ -738,7 +719,6 @@ ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
        return TX_CONTINUE;
 
  fail:
-       printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
        if (frags) {
                for (i = 0; i < num_fragm - 1; i++)
                        if (frags[i])
@@ -749,7 +729,7 @@ ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
        return TX_DROP;
 }
 
-static ieee80211_tx_result
+static ieee80211_tx_result debug_noinline
 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
 {
        if (!tx->key)
@@ -769,99 +749,57 @@ ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
        return TX_DROP;
 }
 
-static ieee80211_tx_result
-ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
+static ieee80211_tx_result debug_noinline
+ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
 {
-       struct ieee80211_local *local = tx->local;
-       struct sk_buff *skb = tx->skb;
-       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
-       u32 load = 0, hdrtime;
-       struct ieee80211_rate *rate;
-       struct ieee80211_supported_band *sband;
-       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
-
-       sband = tx->local->hw.wiphy->bands[tx->channel->band];
-       rate = &sband->bitrates[tx->rate_idx];
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
+       int next_len, i;
+       int group_addr = is_multicast_ether_addr(hdr->addr1);
 
-       /* TODO: this could be part of tx_status handling, so that the number
-        * of retries would be known; TX rate should in that case be stored
-        * somewhere with the packet */
+       if (!(tx->flags & IEEE80211_TX_FRAGMENTED)) {
+               hdr->duration_id = ieee80211_duration(tx, group_addr, 0);
+               return TX_CONTINUE;
+       }
 
-       /* Estimate total channel use caused by this frame */
+       hdr->duration_id = ieee80211_duration(tx, group_addr,
+                                             tx->extra_frag[0]->len);
 
-       /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
-        * 1 usec = 1/8 * (1080 / 10) = 13.5 */
+       for (i = 0; i < tx->num_extra_frag; i++) {
+               if (i + 1 < tx->num_extra_frag) {
+                       next_len = tx->extra_frag[i + 1]->len;
+               } else {
+                       next_len = 0;
+                       tx->rate_idx = tx->last_frag_rate_idx;
+               }
 
-       if (tx->channel->band == IEEE80211_BAND_5GHZ ||
-           (tx->channel->band == IEEE80211_BAND_2GHZ &&
-            rate->flags & IEEE80211_RATE_ERP_G))
-               hdrtime = CHAN_UTIL_HDR_SHORT;
-       else
-               hdrtime = CHAN_UTIL_HDR_LONG;
+               hdr = (struct ieee80211_hdr *)tx->extra_frag[i]->data;
+               hdr->duration_id = ieee80211_duration(tx, 0, next_len);
+       }
 
-       load = hdrtime;
-       if (!is_multicast_ether_addr(hdr->addr1))
-               load += hdrtime;
+       return TX_CONTINUE;
+}
 
-       if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
-               load += 2 * hdrtime;
-       else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
-               load += hdrtime;
+static ieee80211_tx_result debug_noinline
+ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
+{
+       int i;
 
-       /* TODO: optimise again */
-       load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
+       if (!tx->sta)
+               return TX_CONTINUE;
 
+       tx->sta->tx_packets++;
+       tx->sta->tx_fragments++;
+       tx->sta->tx_bytes += tx->skb->len;
        if (tx->extra_frag) {
-               int i;
-               for (i = 0; i < tx->num_extra_frag; i++) {
-                       load += 2 * hdrtime;
-                       load += tx->extra_frag[i]->len *
-                               rate->bitrate;
-               }
-       }
-
-       /* Divide channel_use by 8 to avoid wrapping around the counter */
-       load >>= CHAN_UTIL_SHIFT;
-       local->channel_use_raw += load;
-       if (tx->sta)
-               tx->sta->channel_use_raw += load;
-       tx->sdata->channel_use_raw += load;
-
-       if (tx->sta) {
-               tx->sta->tx_packets++;
-               tx->sta->tx_fragments++;
-               tx->sta->tx_bytes += tx->skb->len;
-               if (tx->extra_frag) {
-                       int i;
-                       tx->sta->tx_fragments += tx->num_extra_frag;
-                       for (i = 0; i < tx->num_extra_frag; i++) {
-                               tx->sta->tx_bytes +=
-                                       tx->extra_frag[i]->len;
-                       }
-               }
+               tx->sta->tx_fragments += tx->num_extra_frag;
+               for (i = 0; i < tx->num_extra_frag; i++)
+                       tx->sta->tx_bytes += tx->extra_frag[i]->len;
        }
 
        return TX_CONTINUE;
 }
 
 
-typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
-static ieee80211_tx_handler ieee80211_tx_handlers[] =
-{
-       ieee80211_tx_h_check_assoc,
-       ieee80211_tx_h_sequence,
-       ieee80211_tx_h_ps_buf,
-       ieee80211_tx_h_select_key,
-       ieee80211_tx_h_michael_mic_add,
-       ieee80211_tx_h_rate_ctrl,
-       ieee80211_tx_h_misc,
-       ieee80211_tx_h_fragment,
-       /* handlers after fragment must be aware of tx info fragmentation! */
-       ieee80211_tx_h_encrypt,
-       ieee80211_tx_h_stats,
-       NULL
-};
-
 /* actual transmit path */
 
 /*
@@ -1013,6 +951,8 @@ __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
        tx->local = local;
        tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
        tx->channel = local->hw.conf.channel;
+       tx->rate_idx = -1;
+       tx->last_frag_rate_idx = -1;
        /*
         * Set this flag (used below to indicate "automatic fragmentation"),
         * it will be cleared/left by radiotap as desired.
@@ -1048,7 +988,8 @@ __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
        if (tx->flags & IEEE80211_TX_FRAGMENTED) {
                if ((tx->flags & IEEE80211_TX_UNICAST) &&
                    skb->len + FCS_LEN > local->fragmentation_threshold &&
-                   !local->ops->set_frag_threshold)
+                   !local->ops->set_frag_threshold &&
+                   !(info->flags & IEEE80211_TX_CTL_AMPDU))
                        tx->flags |= IEEE80211_TX_FRAGMENTED;
                else
                        tx->flags &= ~IEEE80211_TX_FRAGMENTED;
@@ -1098,11 +1039,9 @@ static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
        int ret, i;
 
-       if (!ieee80211_qdisc_installed(local->mdev) &&
-           __ieee80211_queue_stopped(local, 0)) {
-               netif_stop_queue(local->mdev);
+       if (netif_subqueue_stopped(local->mdev, skb))
                return IEEE80211_TX_AGAIN;
-       }
+
        if (skb) {
                ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
                                     "TX to low-level driver", skb);
@@ -1121,7 +1060,8 @@ static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
                                         IEEE80211_TX_CTL_USE_CTS_PROTECT |
                                         IEEE80211_TX_CTL_CLEAR_PS_FILT |
                                         IEEE80211_TX_CTL_FIRST_FRAGMENT);
-                       if (__ieee80211_queue_stopped(local, info->queue))
+                       if (netif_subqueue_stopped(local->mdev,
+                                                  tx->extra_frag[i]))
                                return IEEE80211_TX_FRAG_AGAIN;
                        if (i == tx->num_extra_frag) {
                                info->tx_rate_idx = tx->last_frag_rate_idx;
@@ -1151,18 +1091,65 @@ static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
        return IEEE80211_TX_OK;
 }
 
+/*
+ * Invoke TX handlers, return 0 on success and non-zero if the
+ * frame was dropped or queued.
+ */
+static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
+{
+       struct sk_buff *skb = tx->skb;
+       ieee80211_tx_result res = TX_DROP;
+       int i;
+
+#define CALL_TXH(txh)          \
+       res = txh(tx);          \
+       if (res != TX_CONTINUE) \
+               goto txh_done;
+
+       CALL_TXH(ieee80211_tx_h_check_assoc)
+       CALL_TXH(ieee80211_tx_h_sequence)
+       CALL_TXH(ieee80211_tx_h_ps_buf)
+       CALL_TXH(ieee80211_tx_h_select_key)
+       CALL_TXH(ieee80211_tx_h_michael_mic_add)
+       CALL_TXH(ieee80211_tx_h_rate_ctrl)
+       CALL_TXH(ieee80211_tx_h_misc)
+       CALL_TXH(ieee80211_tx_h_fragment)
+       /* handlers after fragment must be aware of tx info fragmentation! */
+       CALL_TXH(ieee80211_tx_h_encrypt)
+       CALL_TXH(ieee80211_tx_h_calculate_duration)
+       CALL_TXH(ieee80211_tx_h_stats)
+#undef CALL_TXH
+
+ txh_done:
+       if (unlikely(res == TX_DROP)) {
+               I802_DEBUG_INC(tx->local->tx_handlers_drop);
+               dev_kfree_skb(skb);
+               for (i = 0; i < tx->num_extra_frag; i++)
+                       if (tx->extra_frag[i])
+                               dev_kfree_skb(tx->extra_frag[i]);
+               kfree(tx->extra_frag);
+               return -1;
+       } else if (unlikely(res == TX_QUEUED)) {
+               I802_DEBUG_INC(tx->local->tx_handlers_queued);
+               return -1;
+       }
+
+       return 0;
+}
+
 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
 {
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
        struct sta_info *sta;
-       ieee80211_tx_handler *handler;
        struct ieee80211_tx_data tx;
-       ieee80211_tx_result res = TX_DROP, res_prepare;
+       ieee80211_tx_result res_prepare;
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
        int ret, i;
-       int queue = info->queue;
+       u16 queue;
 
-       WARN_ON(__ieee80211_queue_pending(local, queue));
+       queue = skb_get_queue_mapping(skb);
+
+       WARN_ON(test_bit(queue, local->queues_pending));
 
        if (unlikely(skb->len < 10)) {
                dev_kfree_skb(skb);
@@ -1184,44 +1171,8 @@ static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
        tx.channel = local->hw.conf.channel;
        info->band = tx.channel->band;
 
-       for (handler = ieee80211_tx_handlers; *handler != NULL;
-            handler++) {
-               res = (*handler)(&tx);
-               if (res != TX_CONTINUE)
-                       break;
-       }
-
-       if (WARN_ON(tx.skb != skb))
-               goto drop;
-
-       if (unlikely(res == TX_DROP)) {
-               I802_DEBUG_INC(local->tx_handlers_drop);
-               goto drop;
-       }
-
-       if (unlikely(res == TX_QUEUED)) {
-               I802_DEBUG_INC(local->tx_handlers_queued);
-               rcu_read_unlock();
-               return 0;
-       }
-
-       if (tx.extra_frag) {
-               for (i = 0; i < tx.num_extra_frag; i++) {
-                       int next_len, dur;
-                       struct ieee80211_hdr *hdr =
-                               (struct ieee80211_hdr *)
-                               tx.extra_frag[i]->data;
-
-                       if (i + 1 < tx.num_extra_frag) {
-                               next_len = tx.extra_frag[i + 1]->len;
-                       } else {
-                               next_len = 0;
-                               tx.rate_idx = tx.last_frag_rate_idx;
-                       }
-                       dur = ieee80211_duration(&tx, 0, next_len);
-                       hdr->duration_id = cpu_to_le16(dur);
-               }
-       }
+       if (invoke_tx_handlers(&tx))
+               goto out;
 
 retry:
        ret = __ieee80211_tx(local, skb, &tx);
@@ -1233,28 +1184,28 @@ retry:
                 * queues, there's no reason for a driver to reject
                 * a frame there, warn and drop it.
                 */
-               if (WARN_ON(queue >= local->hw.queues))
+               if (WARN_ON(queue >= ieee80211_num_regular_queues(&local->hw)))
                        goto drop;
 
                store = &local->pending_packet[queue];
 
                if (ret == IEEE80211_TX_FRAG_AGAIN)
                        skb = NULL;
-               set_bit(IEEE80211_LINK_STATE_PENDING,
-                       &local->state[queue]);
+               set_bit(queue, local->queues_pending);
                smp_mb();
-               /* When the driver gets out of buffers during sending of
-                * fragments and calls ieee80211_stop_queue, there is
-                * a small window between IEEE80211_LINK_STATE_XOFF and
-                * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
+               /*
+                * When the driver gets out of buffers during sending of
+                * fragments and calls ieee80211_stop_queue, the netif
+                * subqueue is stopped. There is, however, a small window
+                * in which the PENDING bit is not yet set. If a buffer
                 * gets available in that window (i.e. driver calls
                 * ieee80211_wake_queue), we would end up with ieee80211_tx
-                * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
+                * called with the PENDING bit still set. Prevent this by
                 * continuing transmitting here when that situation is
-                * possible to have happened. */
-               if (!__ieee80211_queue_stopped(local, queue)) {
-                       clear_bit(IEEE80211_LINK_STATE_PENDING,
-                                 &local->state[queue]);
+                * possible to have happened.
+                */
+               if (!__netif_subqueue_stopped(local->mdev, queue)) {
+                       clear_bit(queue, local->queues_pending);
                        goto retry;
                }
                store->skb = skb;
@@ -1264,6 +1215,7 @@ retry:
                store->last_frag_rate_ctrl_probe =
                        !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
        }
+ out:
        rcu_read_unlock();
        return 0;
 
@@ -1280,6 +1232,45 @@ retry:
 
 /* device xmit handlers */
 
+static int ieee80211_skb_resize(struct ieee80211_local *local,
+                               struct sk_buff *skb,
+                               int head_need, bool may_encrypt)
+{
+       int tail_need = 0;
+
+       /*
+        * This could be optimised, devices that do full hardware
+        * crypto (including TKIP MMIC) need no tailroom... But we
+        * have no drivers for such devices currently.
+        */
+       if (may_encrypt) {
+               tail_need = IEEE80211_ENCRYPT_TAILROOM;
+               tail_need -= skb_tailroom(skb);
+               tail_need = max_t(int, tail_need, 0);
+       }
+
+       if (head_need || tail_need) {
+               /* Sorry. Can't account for this any more */
+               skb_orphan(skb);
+       }
+
+       if (skb_header_cloned(skb))
+               I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
+       else
+               I802_DEBUG_INC(local->tx_expand_skb_head);
+
+       if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
+               printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
+                      wiphy_name(local->hw.wiphy));
+               return -ENOMEM;
+       }
+
+       /* update truesize too */
+       skb->truesize += head_need + tail_need;
+
+       return 0;
+}
+
 int ieee80211_master_start_xmit(struct sk_buff *skb,
                                struct net_device *dev)
 {
@@ -1287,6 +1278,7 @@ int ieee80211_master_start_xmit(struct sk_buff *skb,
        struct net_device *odev = NULL;
        struct ieee80211_sub_if_data *osdata;
        int headroom;
+       bool may_encrypt;
        int ret;
 
        if (info->control.ifindex)
@@ -1306,13 +1298,18 @@ int ieee80211_master_start_xmit(struct sk_buff *skb,
 
        osdata = IEEE80211_DEV_TO_SUB_IF(odev);
 
-       headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
-       if (skb_headroom(skb) < headroom) {
-               if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
-                       dev_kfree_skb(skb);
-                       dev_put(odev);
-                       return 0;
-               }
+       may_encrypt = !(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT);
+
+       headroom = osdata->local->tx_headroom;
+       if (may_encrypt)
+               headroom += IEEE80211_ENCRYPT_HEADROOM;
+       headroom -= skb_headroom(skb);
+       headroom = max_t(int, 0, headroom);
+
+       if (ieee80211_skb_resize(osdata->local, skb, headroom, may_encrypt)) {
+               dev_kfree_skb(skb);
+               dev_put(odev);
+               return 0;
        }
 
        info->control.vif = &osdata->vif;
@@ -1400,7 +1397,8 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
        struct ieee80211_tx_info *info;
        struct ieee80211_sub_if_data *sdata;
        int ret = 1, head_need;
-       u16 ethertype, hdrlen,  meshhdrlen = 0, fc;
+       u16 ethertype, hdrlen,  meshhdrlen = 0;
+       __le16 fc;
        struct ieee80211_hdr hdr;
        struct ieee80211s_hdr mesh_hdr;
        const u8 *encaps_data;
@@ -1411,8 +1409,6 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
 
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
        if (unlikely(skb->len < ETH_HLEN)) {
-               printk(KERN_DEBUG "%s: short skb (len=%d)\n",
-                      dev->name, skb->len);
                ret = 0;
                goto fail;
        }
@@ -1423,12 +1419,12 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
        /* convert Ethernet header to proper 802.11 header (based on
         * operation mode) */
        ethertype = (skb->data[12] << 8) | skb->data[13];
-       fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
+       fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
 
        switch (sdata->vif.type) {
        case IEEE80211_IF_TYPE_AP:
        case IEEE80211_IF_TYPE_VLAN:
-               fc |= IEEE80211_FCTL_FROMDS;
+               fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
                /* DA BSSID SA */
                memcpy(hdr.addr1, skb->data, ETH_ALEN);
                memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
@@ -1436,7 +1432,7 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
                hdrlen = 24;
                break;
        case IEEE80211_IF_TYPE_WDS:
-               fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
+               fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
                /* RA TA DA SA */
                memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
                memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
@@ -1446,7 +1442,7 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
                break;
 #ifdef CONFIG_MAC80211_MESH
        case IEEE80211_IF_TYPE_MESH_POINT:
-               fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
+               fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
                /* RA TA DA SA */
                if (is_multicast_ether_addr(skb->data))
                        memcpy(hdr.addr1, skb->data, ETH_ALEN);
@@ -1476,7 +1472,7 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
                break;
 #endif
        case IEEE80211_IF_TYPE_STA:
-               fc |= IEEE80211_FCTL_TODS;
+               fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
                /* BSSID SA DA */
                memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
@@ -1509,8 +1505,9 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
        }
 
        /* receiver and we are QoS enabled, use a QoS type frame */
-       if (sta_flags & WLAN_STA_WME && local->hw.queues >= 4) {
-               fc |= IEEE80211_STYPE_QOS_DATA;
+       if (sta_flags & WLAN_STA_WME &&
+           ieee80211_num_regular_queues(&local->hw) >= 4) {
+               fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
                hdrlen += 2;
        }
 
@@ -1538,7 +1535,7 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
                goto fail;
        }
 
-       hdr.frame_control = cpu_to_le16(fc);
+       hdr.frame_control = fc;
        hdr.duration_id = 0;
        hdr.seq_ctrl = 0;
 
@@ -1573,32 +1570,26 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
         * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
         * alloc_skb() (net/core/skbuff.c)
         */
-       head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
-       head_need -= skb_headroom(skb);
+       head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
 
-       /* We are going to modify skb data, so make a copy of it if happens to
-        * be cloned. This could happen, e.g., with Linux bridge code passing
-        * us broadcast frames. */
-
-       if (head_need > 0 || skb_header_cloned(skb)) {
-#if 0
-               printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
-                      "of headroom\n", dev->name, head_need);
-#endif
+       /*
+        * So we need to modify the skb header and hence need a copy of
+        * that. The head_need variable above doesn't, so far, include
+        * the needed header space that we don't need right away. If we
+        * can, then we don't reallocate right now but only after the
+        * frame arrives at the master device (if it does...)
+        *
+        * If we cannot, however, then we will reallocate to include all
+        * the ever needed space. Also, if we need to reallocate it anyway,
+        * make it big enough for everything we may ever need.
+        */
 
-               if (skb_header_cloned(skb))
-                       I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
-               else
-                       I802_DEBUG_INC(local->tx_expand_skb_head);
-               /* Since we have to reallocate the buffer, make sure that there
-                * is enough room for possible WEP IV/ICV and TKIP (8 bytes
-                * before payload and 12 after). */
-               if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
-                                    12, GFP_ATOMIC)) {
-                       printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
-                              "\n", dev->name);
+       if (head_need > 0 || skb_cloned(skb)) {
+               head_need += IEEE80211_ENCRYPT_HEADROOM;
+               head_need += local->tx_headroom;
+               head_need = max_t(int, 0, head_need);
+               if (ieee80211_skb_resize(local, skb, head_need, true))
                        goto fail;
-               }
        }
 
        if (encaps_data) {
@@ -1613,7 +1604,7 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
                h_pos += meshhdrlen;
        }
 
-       if (fc & IEEE80211_STYPE_QOS_DATA) {
+       if (ieee80211_is_data_qos(fc)) {
                __le16 *qos_control;
 
                qos_control = (__le16*) skb_push(skb, 2);
@@ -1661,41 +1652,51 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
        return ret;
 }
 
-/* helper functions for pending packets for when queues are stopped */
 
+/*
+ * ieee80211_clear_tx_pending may not be called in a context where
+ * it is possible that it packets could come in again.
+ */
 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
 {
        int i, j;
        struct ieee80211_tx_stored_packet *store;
 
-       for (i = 0; i < local->hw.queues; i++) {
-               if (!__ieee80211_queue_pending(local, i))
+       for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
+               if (!test_bit(i, local->queues_pending))
                        continue;
                store = &local->pending_packet[i];
                kfree_skb(store->skb);
                for (j = 0; j < store->num_extra_frag; j++)
                        kfree_skb(store->extra_frag[j]);
                kfree(store->extra_frag);
-               clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
+               clear_bit(i, local->queues_pending);
        }
 }
 
+/*
+ * Transmit all pending packets. Called from tasklet, locks master device
+ * TX lock so that no new packets can come in.
+ */
 void ieee80211_tx_pending(unsigned long data)
 {
        struct ieee80211_local *local = (struct ieee80211_local *)data;
        struct net_device *dev = local->mdev;
        struct ieee80211_tx_stored_packet *store;
        struct ieee80211_tx_data tx;
-       int i, ret, reschedule = 0;
+       int i, ret;
 
        netif_tx_lock_bh(dev);
-       for (i = 0; i < local->hw.queues; i++) {
-               if (__ieee80211_queue_stopped(local, i))
+       for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
+               /* Check that this queue is ok */
+               if (__netif_subqueue_stopped(local->mdev, i))
                        continue;
-               if (!__ieee80211_queue_pending(local, i)) {
-                       reschedule = 1;
+
+               if (!test_bit(i, local->queues_pending)) {
+                       ieee80211_wake_queue(&local->hw, i);
                        continue;
                }
+
                store = &local->pending_packet[i];
                tx.extra_frag = store->extra_frag;
                tx.num_extra_frag = store->num_extra_frag;
@@ -1708,19 +1709,11 @@ void ieee80211_tx_pending(unsigned long data)
                        if (ret == IEEE80211_TX_FRAG_AGAIN)
                                store->skb = NULL;
                } else {
-                       clear_bit(IEEE80211_LINK_STATE_PENDING,
-                                 &local->state[i]);
-                       reschedule = 1;
+                       clear_bit(i, local->queues_pending);
+                       ieee80211_wake_queue(&local->hw, i);
                }
        }
        netif_tx_unlock_bh(dev);
-       if (reschedule) {
-               if (!ieee80211_qdisc_installed(dev)) {
-                       if (!__ieee80211_queue_stopped(local, 0))
-                               netif_wake_queue(dev);
-               } else
-                       netif_schedule(dev);
-       }
 }
 
 /* functions for drivers to get certain frames */
@@ -1869,8 +1862,8 @@ struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
                mgmt = (struct ieee80211_mgmt *)
                        skb_put(skb, 24 + sizeof(mgmt->u.beacon));
                memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
-               mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
-                                                  IEEE80211_STYPE_BEACON);
+               mgmt->frame_control =
+                   cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
                memset(mgmt->da, 0xff, ETH_ALEN);
                memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
                /* BSSID is left zeroed, wildcard value */
@@ -1938,10 +1931,9 @@ void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
                       struct ieee80211_rts *rts)
 {
        const struct ieee80211_hdr *hdr = frame;
-       u16 fctl;
 
-       fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
-       rts->frame_control = cpu_to_le16(fctl);
+       rts->frame_control =
+           cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
        rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
                                               frame_txctl);
        memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
@@ -1955,10 +1947,9 @@ void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
                             struct ieee80211_cts *cts)
 {
        const struct ieee80211_hdr *hdr = frame;
-       u16 fctl;
 
-       fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
-       cts->frame_control = cpu_to_le16(fctl);
+       cts->frame_control =
+           cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
        cts->duration = ieee80211_ctstoself_duration(hw, vif,
                                                     frame_len, frame_txctl);
        memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
@@ -1970,11 +1961,9 @@ ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
                          struct ieee80211_vif *vif)
 {
        struct ieee80211_local *local = hw_to_local(hw);
-       struct sk_buff *skb;
+       struct sk_buff *skb = NULL;
        struct sta_info *sta;
-       ieee80211_tx_handler *handler;
        struct ieee80211_tx_data tx;
-       ieee80211_tx_result res = TX_DROP;
        struct net_device *bdev;
        struct ieee80211_sub_if_data *sdata;
        struct ieee80211_if_ap *bss = NULL;
@@ -1983,7 +1972,7 @@ ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
 
        sdata = vif_to_sdata(vif);
        bdev = sdata->dev;
-
+       bss = &sdata->u.ap;
 
        if (!bss)
                return NULL;
@@ -1991,19 +1980,16 @@ ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
        rcu_read_lock();
        beacon = rcu_dereference(bss->beacon);
 
-       if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
-           !beacon->head) {
-               rcu_read_unlock();
-               return NULL;
-       }
+       if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon || !beacon->head)
+               goto out;
 
        if (bss->dtim_count != 0)
-               return NULL; /* send buffered bc/mc only after DTIM beacon */
+               goto out; /* send buffered bc/mc only after DTIM beacon */
 
        while (1) {
                skb = skb_dequeue(&bss->ps_bc_buf);
                if (!skb)
-                       return NULL;
+                       goto out;
                local->total_ps_buffered--;
 
                if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
@@ -2028,24 +2014,9 @@ ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
        tx.channel = local->hw.conf.channel;
        info->band = tx.channel->band;
 
-       for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
-               res = (*handler)(&tx);
-               if (res == TX_DROP || res == TX_QUEUED)
-                       break;
-       }
-
-       if (WARN_ON(tx.skb != skb))
-               return NULL;
-
-       if (res == TX_DROP) {
-               I802_DEBUG_INC(local->tx_handlers_drop);
-               dev_kfree_skb(skb);
-               skb = NULL;
-       } else if (res == TX_QUEUED) {
-               I802_DEBUG_INC(local->tx_handlers_queued);
+       if (invoke_tx_handlers(&tx))
                skb = NULL;
-       }
-
+ out:
        rcu_read_unlock();
 
        return skb;