009c8d58c849b44c589028dc708e1d56ad26685a
[openwrt.git] / package / kernel / mac80211 / patches / 300-mac80211-add-an-intermediate-software-queue-implemen.patch
1 From: Felix Fietkau <nbd@openwrt.org>
2 Date: Tue, 18 Nov 2014 23:58:51 +0100
3 Subject: [PATCH] mac80211: add an intermediate software queue implementation
4
5 This allows drivers to request per-vif and per-sta-tid queues from which
6 they can pull frames. This makes it easier to keep the hardware queues
7 short, and to improve fairness between clients and vifs.
8
9 The task of scheduling packet transmission is left up to the driver -
10 queueing is controlled by mac80211. Drivers can only dequeue packets by
11 calling ieee80211_tx_dequeue. This makes it possible to add active queue
12 management later without changing drivers using this code.
13
14 This can also be used as a starting point to implement A-MSDU
15 aggregation in a way that does not add artificially induced latency.
16
17 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
18 ---
19
20 --- a/include/net/mac80211.h
21 +++ b/include/net/mac80211.h
22 @@ -84,6 +84,35 @@
23   *
24   */
25  
26 +/**
27 + * DOC: mac80211 software tx queueing
28 + *
29 + * mac80211 provides an optional intermediate queueing implementation designed
30 + * to allow the driver to keep hardware queues short and provide some fairness
31 + * between different stations/interfaces.
32 + * In this model, the driver pulls data frames from the mac80211 queue instead
33 + * of letting mac80211 push them via drv_tx().
34 + * Other frames (e.g. control or management) are still pushed using drv_tx().
35 + *
36 + * Intermediate queues (struct ieee80211_txq) are kept per-sta per-tid, with a
37 + * single per-vif queue for multicast data frames.
38 + *
39 + * The driver is expected to initialize its private per-queue data for stations
40 + * and interfaces in the .add_interface and .sta_add ops.
41 + *
42 + * The driver can not access the queue directly. To dequeue a frame, it calls
43 + * ieee80211_tx_dequeue(). Whenever mac80211 adds a new frame to a queue, it
44 + * calls the .wake_tx_queue driver op.
45 + *
46 + * For AP powersave TIM handling, the driver only needs to indicate if it has
47 + * buffered packets in the driver specific data structures by calling
48 + * ieee80211_sta_set_buffered(). For frames buffered in the ieee80211_txq
49 + * struct, mac80211 sets the appropriate TIM PVB bits and calls
50 + * .release_buffered_frames().
51 + * That callback is expected to release its own buffered frames and afterwards
52 + * also frames from the ieee80211_txq (obtained via ieee80211_tx_dequeue).
53 + */
54 +
55  struct device;
56  
57  /**
58 @@ -1257,6 +1286,8 @@ struct ieee80211_vif {
59         u8 cab_queue;
60         u8 hw_queue[IEEE80211_NUM_ACS];
61  
62 +       struct ieee80211_txq *txq;
63 +
64         struct ieee80211_chanctx_conf __rcu *chanctx_conf;
65  
66         u32 driver_flags;
67 @@ -1519,6 +1550,8 @@ struct ieee80211_sta {
68         bool tdls_initiator;
69         bool mfp;
70  
71 +       struct ieee80211_txq *txq[IEEE80211_NUM_TIDS];
72 +
73         /* must be last */
74         u8 drv_priv[0] __aligned(sizeof(void *));
75  };
76 @@ -1547,6 +1580,27 @@ struct ieee80211_tx_control {
77  };
78  
79  /**
80 + * struct ieee80211_txq - Software intermediate tx queue
81 + *
82 + * @vif: &struct ieee80211_vif pointer from the add_interface callback.
83 + * @sta: station table entry, %NULL for per-vif queue
84 + * @tid: the TID for this queue (unused for per-vif queue)
85 + * @ac: the AC for this queue
86 + *
87 + * The driver can obtain packets from this queue by calling
88 + * ieee80211_tx_dequeue().
89 + */
90 +struct ieee80211_txq {
91 +       struct ieee80211_vif *vif;
92 +       struct ieee80211_sta *sta;
93 +       u8 tid;
94 +       u8 ac;
95 +
96 +       /* must be last */
97 +       u8 drv_priv[0] __aligned(sizeof(void *));
98 +};
99 +
100 +/**
101   * enum ieee80211_hw_flags - hardware flags
102   *
103   * These flags are used to indicate hardware capabilities to
104 @@ -1770,6 +1824,8 @@ enum ieee80211_hw_flags {
105   *     within &struct ieee80211_sta.
106   * @chanctx_data_size: size (in bytes) of the drv_priv data area
107   *     within &struct ieee80211_chanctx_conf.
108 + * @txq_data_size: size (in bytes) of the drv_priv data area
109 + *     within @struct ieee80211_txq.
110   *
111   * @max_rates: maximum number of alternate rate retry stages the hw
112   *     can handle.
113 @@ -1818,6 +1874,9 @@ enum ieee80211_hw_flags {
114   * @n_cipher_schemes: a size of an array of cipher schemes definitions.
115   * @cipher_schemes: a pointer to an array of cipher scheme definitions
116   *     supported by HW.
117 + *
118 + * @txq_ac_max_pending: maximum number of frames per AC pending in all txq
119 + *     entries for a vif.
120   */
121  struct ieee80211_hw {
122         struct ieee80211_conf conf;
123 @@ -1830,6 +1889,7 @@ struct ieee80211_hw {
124         int vif_data_size;
125         int sta_data_size;
126         int chanctx_data_size;
127 +       int txq_data_size;
128         u16 queues;
129         u16 max_listen_interval;
130         s8 max_signal;
131 @@ -1846,6 +1906,7 @@ struct ieee80211_hw {
132         u8 uapsd_max_sp_len;
133         u8 n_cipher_schemes;
134         const struct ieee80211_cipher_scheme *cipher_schemes;
135 +       int txq_ac_max_pending;
136  };
137  
138  /**
139 @@ -3007,6 +3068,8 @@ enum ieee80211_reconfig_type {
140   *     response template is provided, together with the location of the
141   *     switch-timing IE within the template. The skb can only be used within
142   *     the function call.
143 + *
144 + * @wake_tx_queue: Called when new packets have been added to the queue.
145   */
146  struct ieee80211_ops {
147         void (*tx)(struct ieee80211_hw *hw,
148 @@ -3238,6 +3301,9 @@ struct ieee80211_ops {
149         void (*tdls_recv_channel_switch)(struct ieee80211_hw *hw,
150                                          struct ieee80211_vif *vif,
151                                          struct ieee80211_tdls_ch_sw_params *params);
152 +
153 +       void (*wake_tx_queue)(struct ieee80211_hw *hw,
154 +                             struct ieee80211_txq *txq);
155  };
156  
157  /**
158 @@ -5249,4 +5315,17 @@ void ieee80211_unreserve_tid(struct ieee
159   */
160  size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
161                           const u8 *ids, int n_ids, size_t offset);
162 +
163 +/**
164 + * ieee80211_tx_dequeue - dequeue a packet from a software tx queue
165 + *
166 + * @hw: pointer as obtained from ieee80211_alloc_hw()
167 + * @txq: pointer obtained from .add_tx_queue() call
168 + *
169 + * Returns the skb if successful, %NULL if no frame was available.
170 + */
171 +struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
172 +                                    struct ieee80211_txq *txq);
173 +
174 +
175  #endif /* MAC80211_H */
176 --- a/net/mac80211/driver-ops.h
177 +++ b/net/mac80211/driver-ops.h
178 @@ -1367,4 +1367,16 @@ drv_tdls_recv_channel_switch(struct ieee
179         trace_drv_return_void(local);
180  }
181  
182 +static inline void drv_wake_tx_queue(struct ieee80211_local *local,
183 +                                    struct txq_info *txq)
184 +{
185 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->txq.vif);
186 +
187 +       if (!check_sdata_in_driver(sdata))
188 +               return;
189 +
190 +       trace_drv_wake_tx_queue(local, sdata, txq->txq.sta, txq->txq.tid);
191 +       local->ops->wake_tx_queue(&local->hw, &txq->txq);
192 +}
193 +
194  #endif /* __MAC80211_DRIVER_OPS */
195 --- a/net/mac80211/ieee80211_i.h
196 +++ b/net/mac80211/ieee80211_i.h
197 @@ -809,6 +809,19 @@ struct mac80211_qos_map {
198         struct rcu_head rcu_head;
199  };
200  
201 +enum txq_info_flags {
202 +       IEEE80211_TXQ_STOP,
203 +       IEEE80211_TXQ_AMPDU,
204 +};
205 +
206 +struct txq_info {
207 +       struct sk_buff_head queue;
208 +       unsigned long flags;
209 +
210 +       /* keep last! */
211 +       struct ieee80211_txq txq;
212 +};
213 +
214  struct ieee80211_sub_if_data {
215         struct list_head list;
216  
217 @@ -853,6 +866,7 @@ struct ieee80211_sub_if_data {
218         bool control_port_no_encrypt;
219         int encrypt_headroom;
220  
221 +       atomic_t txqs_len[IEEE80211_NUM_ACS];
222         struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS];
223         struct mac80211_qos_map __rcu *qos_map;
224  
225 @@ -1453,6 +1467,10 @@ static inline struct ieee80211_local *hw
226         return container_of(hw, struct ieee80211_local, hw);
227  }
228  
229 +static inline struct txq_info *to_txq_info(struct ieee80211_txq *txq)
230 +{
231 +       return container_of(txq, struct txq_info, txq);
232 +}
233  
234  static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
235  {
236 @@ -1905,6 +1923,9 @@ static inline bool ieee80211_can_run_wor
237         return true;
238  }
239  
240 +void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
241 +                            struct sta_info *sta,
242 +                            struct txq_info *txq, int tid);
243  void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
244                          u16 transaction, u16 auth_alg, u16 status,
245                          const u8 *extra, size_t extra_len, const u8 *bssid,
246 --- a/net/mac80211/iface.c
247 +++ b/net/mac80211/iface.c
248 @@ -969,6 +969,13 @@ static void ieee80211_do_stop(struct iee
249         }
250         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
251  
252 +       if (sdata->vif.txq) {
253 +               struct txq_info *txqi = to_txq_info(sdata->vif.txq);
254 +
255 +               ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
256 +               atomic_set(&sdata->txqs_len[txqi->txq.ac], 0);
257 +       }
258 +
259         if (local->open_count == 0)
260                 ieee80211_clear_tx_pending(local);
261  
262 @@ -1674,6 +1681,7 @@ int ieee80211_if_add(struct ieee80211_lo
263  {
264         struct net_device *ndev = NULL;
265         struct ieee80211_sub_if_data *sdata = NULL;
266 +       struct txq_info *txqi;
267         int ret, i;
268         int txqs = 1;
269  
270 @@ -1693,10 +1701,18 @@ int ieee80211_if_add(struct ieee80211_lo
271                 ieee80211_assign_perm_addr(local, wdev->address, type);
272                 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
273         } else {
274 +               int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
275 +                                sizeof(void *));
276 +               int txq_size = 0;
277 +
278 +               if (local->ops->wake_tx_queue)
279 +                       txq_size += sizeof(struct txq_info) +
280 +                                   local->hw.txq_data_size;
281 +
282                 if (local->hw.queues >= IEEE80211_NUM_ACS)
283                         txqs = IEEE80211_NUM_ACS;
284  
285 -               ndev = alloc_netdev_mqs(sizeof(*sdata) + local->hw.vif_data_size,
286 +               ndev = alloc_netdev_mqs(size + txq_size,
287                                         name, NET_NAME_UNKNOWN,
288                                         ieee80211_if_setup, txqs, 1);
289                 if (!ndev)
290 @@ -1731,6 +1747,11 @@ int ieee80211_if_add(struct ieee80211_lo
291                 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
292                 memcpy(sdata->name, ndev->name, IFNAMSIZ);
293  
294 +               if (txq_size) {
295 +                       txqi = netdev_priv(ndev) + size;
296 +                       ieee80211_init_tx_queue(sdata, NULL, txqi, 0);
297 +               }
298 +
299                 sdata->dev = ndev;
300         }
301  
302 --- a/net/mac80211/main.c
303 +++ b/net/mac80211/main.c
304 @@ -1019,6 +1019,9 @@ int ieee80211_register_hw(struct ieee802
305  
306         local->dynamic_ps_forced_timeout = -1;
307  
308 +       if (!local->hw.txq_ac_max_pending)
309 +               local->hw.txq_ac_max_pending = 64;
310 +
311         result = ieee80211_wep_init(local);
312         if (result < 0)
313                 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
314 --- a/net/mac80211/sta_info.c
315 +++ b/net/mac80211/sta_info.c
316 @@ -118,6 +118,16 @@ static void __cleanup_single_sta(struct 
317                 atomic_dec(&ps->num_sta_ps);
318         }
319  
320 +       if (sta->sta.txq[0]) {
321 +               for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
322 +                       struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
323 +                       int n = skb_queue_len(&txqi->queue);
324 +
325 +                       ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
326 +                       atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
327 +               }
328 +       }
329 +
330         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
331                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
332                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
333 @@ -234,6 +244,8 @@ void sta_info_free(struct ieee80211_loca
334  
335         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
336  
337 +       if (sta->sta.txq[0])
338 +               kfree(to_txq_info(sta->sta.txq[0]));
339         kfree(rcu_dereference_raw(sta->sta.rates));
340         kfree(sta);
341  }
342 @@ -285,11 +297,12 @@ struct sta_info *sta_info_alloc(struct i
343                                 const u8 *addr, gfp_t gfp)
344  {
345         struct ieee80211_local *local = sdata->local;
346 +       struct ieee80211_hw *hw = &local->hw;
347         struct sta_info *sta;
348         struct timespec uptime;
349         int i;
350  
351 -       sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
352 +       sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
353         if (!sta)
354                 return NULL;
355  
356 @@ -321,11 +334,25 @@ struct sta_info *sta_info_alloc(struct i
357         for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
358                 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
359  
360 -       if (sta_prepare_rate_control(local, sta, gfp)) {
361 -               kfree(sta);
362 -               return NULL;
363 +       if (local->ops->wake_tx_queue) {
364 +               void *txq_data;
365 +               int size = sizeof(struct txq_info) +
366 +                          ALIGN(hw->txq_data_size, sizeof(void *));
367 +
368 +               txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
369 +               if (!txq_data)
370 +                       goto free;
371 +
372 +               for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
373 +                       struct txq_info *txq = txq_data + i * size;
374 +
375 +                       ieee80211_init_tx_queue(sdata, sta, txq, i);
376 +               }
377         }
378  
379 +       if (sta_prepare_rate_control(local, sta, gfp))
380 +               goto free_txq;
381 +
382         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
383                 /*
384                  * timer_to_tid must be initialized with identity mapping
385 @@ -346,7 +373,7 @@ struct sta_info *sta_info_alloc(struct i
386         if (sdata->vif.type == NL80211_IFTYPE_AP ||
387             sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
388                 struct ieee80211_supported_band *sband =
389 -                       local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
390 +                       hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
391                 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
392                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
393                 /*
394 @@ -371,6 +398,13 @@ struct sta_info *sta_info_alloc(struct i
395         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
396  
397         return sta;
398 +
399 +free_txq:
400 +       if (sta->sta.txq[0])
401 +               kfree(to_txq_info(sta->sta.txq[0]));
402 +free:
403 +       kfree(sta);
404 +       return NULL;
405  }
406  
407  static int sta_info_insert_check(struct sta_info *sta)
408 @@ -640,6 +674,8 @@ static void __sta_info_recalc_tim(struct
409  
410                 indicate_tim |=
411                         sta->driver_buffered_tids & tids;
412 +               indicate_tim |=
413 +                       sta->txq_buffered_tids & tids;
414         }
415  
416   done:
417 @@ -1071,7 +1107,7 @@ void ieee80211_sta_ps_deliver_wakeup(str
418         struct ieee80211_sub_if_data *sdata = sta->sdata;
419         struct ieee80211_local *local = sdata->local;
420         struct sk_buff_head pending;
421 -       int filtered = 0, buffered = 0, ac;
422 +       int filtered = 0, buffered = 0, ac, i;
423         unsigned long flags;
424         struct ps_data *ps;
425  
426 @@ -1090,10 +1126,22 @@ void ieee80211_sta_ps_deliver_wakeup(str
427  
428         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
429         sta->driver_buffered_tids = 0;
430 +       sta->txq_buffered_tids = 0;
431  
432         if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
433                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
434  
435 +       if (sta->sta.txq[0]) {
436 +               for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
437 +                       struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
438 +
439 +                       if (!skb_queue_len(&txqi->queue))
440 +                               continue;
441 +
442 +                       drv_wake_tx_queue(local, txqi);
443 +               }
444 +       }
445 +
446         skb_queue_head_init(&pending);
447  
448         /* sync with ieee80211_tx_h_unicast_ps_buf */
449 @@ -1254,7 +1302,7 @@ ieee80211_sta_ps_deliver_response(struct
450         struct ieee80211_sub_if_data *sdata = sta->sdata;
451         struct ieee80211_local *local = sdata->local;
452         bool more_data = false;
453 -       int ac;
454 +       int ac, tid;
455         unsigned long driver_release_tids = 0;
456         struct sk_buff_head frames;
457  
458 @@ -1275,8 +1323,10 @@ ieee80211_sta_ps_deliver_response(struct
459                 /* if we already have frames from software, then we can't also
460                  * release from hardware queues
461                  */
462 -               if (skb_queue_empty(&frames))
463 +               if (skb_queue_empty(&frames)) {
464                         driver_release_tids |= sta->driver_buffered_tids & tids;
465 +                       driver_release_tids |= sta->txq_buffered_tids & tids;
466 +               }
467  
468                 if (driver_release_tids) {
469                         /* If the driver has data on more than one TID then
470 @@ -1447,6 +1497,8 @@ ieee80211_sta_ps_deliver_response(struct
471  
472                 sta_info_recalc_tim(sta);
473         } else {
474 +               unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
475 +
476                 /*
477                  * We need to release a frame that is buffered somewhere in the
478                  * driver ... it'll have to handle that.
479 @@ -1466,8 +1518,22 @@ ieee80211_sta_ps_deliver_response(struct
480                  * that the TID(s) became empty before returning here from the
481                  * release function.
482                  * Either way, however, when the driver tells us that the TID(s)
483 -                * became empty we'll do the TIM recalculation.
484 +                * became empty or we find that a txq became empty, we'll do the
485 +                * TIM recalculation.
486                  */
487 +
488 +               if (!sta->sta.txq[0])
489 +                       return;
490 +
491 +               for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
492 +                       struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
493 +
494 +                       if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
495 +                               continue;
496 +
497 +                       sta_info_recalc_tim(sta);
498 +                       break;
499 +               }
500         }
501  }
502  
503 --- a/net/mac80211/sta_info.h
504 +++ b/net/mac80211/sta_info.h
505 @@ -274,6 +274,7 @@ struct sta_ampdu_mlme {
506   *     entered power saving state, these are also delivered to
507   *     the station when it leaves powersave or polls for frames
508   * @driver_buffered_tids: bitmap of TIDs the driver has data buffered on
509 + * @txq_buffered_tids: bitmap of TIDs that mac80211 has txq data buffered on
510   * @rx_packets: Number of MSDUs received from this STA
511   * @rx_bytes: Number of bytes received from this STA
512   * @last_rx: time (in jiffies) when last frame was received from this STA
513 @@ -368,6 +369,7 @@ struct sta_info {
514         struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
515         struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
516         unsigned long driver_buffered_tids;
517 +       unsigned long txq_buffered_tids;
518  
519         /* Updated from RX path only, no locking requirements */
520         unsigned long rx_packets;
521 --- a/net/mac80211/trace.h
522 +++ b/net/mac80211/trace.h
523 @@ -2312,6 +2312,34 @@ TRACE_EVENT(drv_tdls_recv_channel_switch
524         )
525  );
526  
527 +TRACE_EVENT(drv_wake_tx_queue,
528 +       TP_PROTO(struct ieee80211_local *local,
529 +                struct ieee80211_sub_if_data *sdata,
530 +                struct ieee80211_sta *sta,
531 +                u8 tid),
532 +
533 +       TP_ARGS(local, sdata, sta, tid),
534 +
535 +       TP_STRUCT__entry(
536 +               LOCAL_ENTRY
537 +               VIF_ENTRY
538 +               STA_ENTRY
539 +               __field(u8, tid)
540 +       ),
541 +
542 +       TP_fast_assign(
543 +               LOCAL_ASSIGN;
544 +               VIF_ASSIGN;
545 +               STA_ASSIGN;
546 +               __entry->tid = tid;
547 +       ),
548 +
549 +       TP_printk(
550 +               LOCAL_PR_FMT  VIF_PR_FMT  STA_PR_FMT " tid: 0x%x",
551 +               LOCAL_PR_ARG, VIF_PR_ARG, STA_PR_ARG, __entry->tid
552 +       )
553 +);
554 +
555  #ifdef CPTCFG_MAC80211_MESSAGE_TRACING
556  #undef TRACE_SYSTEM
557  #define TRACE_SYSTEM mac80211_msg
558 --- a/net/mac80211/tx.c
559 +++ b/net/mac80211/tx.c
560 @@ -776,12 +776,23 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
561         return TX_CONTINUE;
562  }
563  
564 +static u16
565 +ieee80211_tx_next_seq(struct sta_info *sta, int tid)
566 +{
567 +       u16 *seq = &sta->tid_seq[tid];
568 +       u16 ret = cpu_to_le16(*seq);
569 +
570 +       /* Increase the sequence number. */
571 +       *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
572 +
573 +       return ret;
574 +}
575 +
576  static ieee80211_tx_result debug_noinline
577  ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
578  {
579         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
580         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
581 -       u16 *seq;
582         u8 *qc;
583         int tid;
584  
585 @@ -832,13 +843,10 @@ ieee80211_tx_h_sequence(struct ieee80211
586  
587         qc = ieee80211_get_qos_ctl(hdr);
588         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
589 -       seq = &tx->sta->tid_seq[tid];
590         tx->sta->tx_msdu[tid]++;
591  
592 -       hdr->seq_ctrl = cpu_to_le16(*seq);
593 -
594 -       /* Increase the sequence number. */
595 -       *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
596 +       if (!tx->sta->sta.txq[0])
597 +               hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
598  
599         return TX_CONTINUE;
600  }
601 @@ -1067,7 +1075,7 @@ static bool ieee80211_tx_prep_agg(struct
602                  * nothing -- this aggregation session is being started
603                  * but that might still fail with the driver
604                  */
605 -       } else {
606 +       } else if (!tx->sta->sta.txq[tid]) {
607                 spin_lock(&tx->sta->lock);
608                 /*
609                  * Need to re-check now, because we may get here
610 @@ -1201,13 +1209,102 @@ ieee80211_tx_prepare(struct ieee80211_su
611         return TX_CONTINUE;
612  }
613  
614 +static void ieee80211_drv_tx(struct ieee80211_local *local,
615 +                            struct ieee80211_vif *vif,
616 +                            struct ieee80211_sta *pubsta,
617 +                            struct sk_buff *skb)
618 +{
619 +       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
620 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
621 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
622 +       struct ieee80211_tx_control control = {
623 +               .sta = pubsta
624 +       };
625 +       struct ieee80211_txq *txq = NULL;
626 +       struct txq_info *txqi;
627 +       u8 ac;
628 +
629 +       if (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE)
630 +               goto tx_normal;
631 +
632 +       if (!ieee80211_is_data(hdr->frame_control))
633 +               goto tx_normal;
634 +
635 +       if (pubsta) {
636 +               u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
637 +
638 +               txq = pubsta->txq[tid];
639 +       } else if (vif) {
640 +               txq = vif->txq;
641 +       }
642 +
643 +       if (!txq)
644 +               goto tx_normal;
645 +
646 +       ac = txq->ac;
647 +       txqi = to_txq_info(txq);
648 +       atomic_inc(&sdata->txqs_len[ac]);
649 +       if (atomic_read(&sdata->txqs_len[ac]) >= local->hw.txq_ac_max_pending)
650 +               netif_stop_subqueue(sdata->dev, ac);
651 +
652 +       skb_queue_tail(&txqi->queue, skb);
653 +       drv_wake_tx_queue(local, txqi);
654 +
655 +       return;
656 +
657 +tx_normal:
658 +       drv_tx(local, &control, skb);
659 +}
660 +
661 +struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
662 +                                    struct ieee80211_txq *txq)
663 +{
664 +       struct ieee80211_local *local = hw_to_local(hw);
665 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
666 +       struct txq_info *txqi = container_of(txq, struct txq_info, txq);
667 +       struct ieee80211_hdr *hdr;
668 +       struct sk_buff *skb = NULL;
669 +       u8 ac = txq->ac;
670 +
671 +       spin_lock_bh(&txqi->queue.lock);
672 +
673 +       if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
674 +               goto out;
675 +
676 +       skb = __skb_dequeue(&txqi->queue);
677 +       if (!skb)
678 +               goto out;
679 +
680 +       atomic_dec(&sdata->txqs_len[ac]);
681 +       if (__netif_subqueue_stopped(sdata->dev, ac))
682 +               ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);
683 +
684 +       hdr = (struct ieee80211_hdr *)skb->data;
685 +       if (txq->sta && ieee80211_is_data_qos(hdr->frame_control)) {
686 +               struct sta_info *sta = container_of(txq->sta, struct sta_info,
687 +                                                   sta);
688 +               struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
689 +
690 +               hdr->seq_ctrl = ieee80211_tx_next_seq(sta, txq->tid);
691 +               if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
692 +                       info->flags |= IEEE80211_TX_CTL_AMPDU;
693 +               else
694 +                       info->flags &= ~IEEE80211_TX_CTL_AMPDU;
695 +       }
696 +
697 +out:
698 +       spin_unlock_bh(&txqi->queue.lock);
699 +
700 +       return skb;
701 +}
702 +EXPORT_SYMBOL(ieee80211_tx_dequeue);
703 +
704  static bool ieee80211_tx_frags(struct ieee80211_local *local,
705                                struct ieee80211_vif *vif,
706                                struct ieee80211_sta *sta,
707                                struct sk_buff_head *skbs,
708                                bool txpending)
709  {
710 -       struct ieee80211_tx_control control;
711         struct sk_buff *skb, *tmp;
712         unsigned long flags;
713  
714 @@ -1265,10 +1362,9 @@ static bool ieee80211_tx_frags(struct ie
715                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
716  
717                 info->control.vif = vif;
718 -               control.sta = sta;
719  
720                 __skb_unlink(skb, skbs);
721 -               drv_tx(local, &control, skb);
722 +               ieee80211_drv_tx(local, vif, sta, skb);
723         }
724  
725         return true;
726 --- a/net/mac80211/util.c
727 +++ b/net/mac80211/util.c
728 @@ -308,6 +308,11 @@ void ieee80211_propagate_queue_wake(stru
729                 for (ac = 0; ac < n_acs; ac++) {
730                         int ac_queue = sdata->vif.hw_queue[ac];
731  
732 +                       if (local->ops->wake_tx_queue &&
733 +                           (atomic_read(&sdata->txqs_len[ac]) >
734 +                            local->hw.txq_ac_max_pending))
735 +                               continue;
736 +
737                         if (ac_queue == queue ||
738                             (sdata->vif.cab_queue == queue &&
739                              local->queue_stop_reasons[ac_queue] == 0 &&
740 @@ -3307,3 +3312,20 @@ u8 *ieee80211_add_wmm_info_ie(u8 *buf, u
741  
742         return buf;
743  }
744 +
745 +void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
746 +                            struct sta_info *sta,
747 +                            struct txq_info *txqi, int tid)
748 +{
749 +       skb_queue_head_init(&txqi->queue);
750 +       txqi->txq.vif = &sdata->vif;
751 +
752 +       if (sta) {
753 +               txqi->txq.sta = &sta->sta;
754 +               sta->sta.txq[tid] = &txqi->txq;
755 +               txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
756 +       } else {
757 +               sdata->vif.txq = &txqi->txq;
758 +               txqi->txq.ac = IEEE80211_AC_BE;
759 +       }
760 +}
761 --- a/net/mac80211/rx.c
762 +++ b/net/mac80211/rx.c
763 @@ -1176,6 +1176,7 @@ static void sta_ps_start(struct sta_info
764         struct ieee80211_sub_if_data *sdata = sta->sdata;
765         struct ieee80211_local *local = sdata->local;
766         struct ps_data *ps;
767 +       int tid;
768  
769         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
770             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
771 @@ -1189,6 +1190,18 @@ static void sta_ps_start(struct sta_info
772                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
773         ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
774                sta->sta.addr, sta->sta.aid);
775 +
776 +       if (!sta->sta.txq[0])
777 +               return;
778 +
779 +       for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
780 +               struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
781 +
782 +               if (!skb_queue_len(&txqi->queue))
783 +                       set_bit(tid, &sta->txq_buffered_tids);
784 +               else
785 +                       clear_bit(tid, &sta->txq_buffered_tids);
786 +       }
787  }
788  
789  static void sta_ps_end(struct sta_info *sta)
790 --- a/net/mac80211/agg-tx.c
791 +++ b/net/mac80211/agg-tx.c
792 @@ -188,6 +188,43 @@ ieee80211_wake_queue_agg(struct ieee8021
793         __release(agg_queue);
794  }
795  
796 +static void
797 +ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
798 +{
799 +       struct ieee80211_txq *txq = sta->sta.txq[tid];
800 +       struct txq_info *txqi;
801 +
802 +       if (!txq)
803 +               return;
804 +
805 +       txqi = to_txq_info(txq);
806 +
807 +       /* Lock here to protect against further seqno updates on dequeue */
808 +       spin_lock_bh(&txqi->queue.lock);
809 +       set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
810 +       spin_unlock_bh(&txqi->queue.lock);
811 +}
812 +
813 +static void
814 +ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
815 +{
816 +       struct ieee80211_txq *txq = sta->sta.txq[tid];
817 +       struct txq_info *txqi;
818 +
819 +       if (!txq)
820 +               return;
821 +
822 +       txqi = to_txq_info(txq);
823 +
824 +       if (enable)
825 +               set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
826 +       else
827 +               clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
828 +
829 +       clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
830 +       drv_wake_tx_queue(sta->sdata->local, txqi);
831 +}
832 +
833  /*
834   * splice packets from the STA's pending to the local pending,
835   * requires a call to ieee80211_agg_splice_finish later
836 @@ -247,6 +284,7 @@ static void ieee80211_remove_tid_tx(stru
837         ieee80211_assign_tid_tx(sta, tid, NULL);
838  
839         ieee80211_agg_splice_finish(sta->sdata, tid);
840 +       ieee80211_agg_start_txq(sta, tid, false);
841  
842         kfree_rcu(tid_tx, rcu_head);
843  }
844 @@ -418,6 +456,8 @@ void ieee80211_tx_ba_session_handle_star
845          */
846         clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
847  
848 +       ieee80211_agg_stop_txq(sta, tid);
849 +
850         /*
851          * Make sure no packets are being processed. This ensures that
852          * we have a valid starting sequence number and that in-flight
853 @@ -440,6 +480,8 @@ void ieee80211_tx_ba_session_handle_star
854                 ieee80211_agg_splice_finish(sdata, tid);
855                 spin_unlock_bh(&sta->lock);
856  
857 +               ieee80211_agg_start_txq(sta, tid, false);
858 +
859                 kfree_rcu(tid_tx, rcu_head);
860                 return;
861         }
862 @@ -666,6 +708,8 @@ static void ieee80211_agg_tx_operational
863         ieee80211_agg_splice_finish(sta->sdata, tid);
864  
865         spin_unlock_bh(&sta->lock);
866 +
867 +       ieee80211_agg_start_txq(sta, tid, true);
868  }
869  
870  void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)