ath9k: fix several issues in the tx queueing rework
[openwrt.git] / package / kernel / mac80211 / patches / 570-ath9k_tid_queue_non_aggr.patch
1 --- a/drivers/net/wireless/ath/ath9k/xmit.c
2 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
3 @@ -198,6 +198,41 @@ static struct sk_buff *ath_tid_dequeue(s
4         return skb;
5  }
6  
7 +/*
8 + * ath_tx_tid_change_state:
9 + * - clears a-mpdu flag of previous session
10 + * - force sequence number allocation to fix next BlockAck Window
11 + */
12 +static void
13 +ath_tx_tid_change_state(struct ath_softc *sc, struct ath_atx_tid *tid)
14 +{
15 +       struct ath_txq *txq = tid->ac->txq;
16 +       struct ieee80211_tx_info *tx_info;
17 +       struct sk_buff *skb, *tskb;
18 +       struct ath_buf *bf;
19 +       struct ath_frame_info *fi;
20 +
21 +       skb_queue_walk_safe(&tid->buf_q, skb, tskb) {
22 +               fi = get_frame_info(skb);
23 +               bf = fi->bf;
24 +
25 +               tx_info = IEEE80211_SKB_CB(skb);
26 +               tx_info->flags &= ~IEEE80211_TX_CTL_AMPDU;
27 +
28 +               if (bf)
29 +                       continue;
30 +
31 +               bf = ath_tx_setup_buffer(sc, txq, tid, skb);
32 +               if (!bf) {
33 +                       __skb_unlink(skb, &tid->buf_q);
34 +                       ath_txq_skb_done(sc, txq, skb);
35 +                       ieee80211_free_txskb(sc->hw, skb);
36 +                       continue;
37 +               }
38 +       }
39 +
40 +}
41 +
42  static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
43  {
44         struct ath_txq *txq = tid->ac->txq;
45 @@ -212,28 +247,22 @@ static void ath_tx_flush_tid(struct ath_
46  
47         memset(&ts, 0, sizeof(ts));
48  
49 -       while ((skb = ath_tid_dequeue(tid))) {
50 +       while ((skb = __skb_dequeue(&tid->retry_q))) {
51                 fi = get_frame_info(skb);
52                 bf = fi->bf;
53 -
54                 if (!bf) {
55 -                       bf = ath_tx_setup_buffer(sc, txq, tid, skb);
56 -                       if (!bf) {
57 -                               ath_txq_skb_done(sc, txq, skb);
58 -                               ieee80211_free_txskb(sc->hw, skb);
59 -                               continue;
60 -                       }
61 +                       ath_txq_skb_done(sc, txq, skb);
62 +                       ieee80211_free_txskb(sc->hw, skb);
63 +                       continue;
64                 }
65  
66                 if (fi->baw_tracked) {
67 -                       list_add_tail(&bf->list, &bf_head);
68                         ath_tx_update_baw(sc, tid, bf->bf_state.seqno);
69 -                       ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
70                         sendbar = true;
71 -               } else {
72 -                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
73 -                       ath_tx_send_normal(sc, txq, NULL, skb);
74                 }
75 +
76 +               list_add_tail(&bf->list, &bf_head);
77 +               ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
78         }
79  
80         if (sendbar) {
81 @@ -911,50 +940,39 @@ ath_tx_get_tid_subframe(struct ath_softc
82         return NULL;
83  }
84  
85 -static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
86 -                                            struct ath_txq *txq,
87 -                                            struct ath_atx_tid *tid,
88 -                                            struct list_head *bf_q,
89 -                                            int *aggr_len)
90 +static bool
91 +ath_tx_form_aggr(struct ath_softc *sc, struct ath_txq *txq,
92 +                struct ath_atx_tid *tid, struct list_head *bf_q,
93 +                struct ath_buf *bf_first, struct sk_buff_head *tid_q,
94 +                int *aggr_len)
95  {
96  #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
97 -       struct ath_buf *bf, *bf_first = NULL, *bf_prev = NULL;
98 +       struct ath_buf *bf = bf_first, *bf_prev = NULL;
99         int nframes = 0, ndelim;
100         u16 aggr_limit = 0, al = 0, bpad = 0,
101             al_delta, h_baw = tid->baw_size / 2;
102 -       enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
103         struct ieee80211_tx_info *tx_info;
104         struct ath_frame_info *fi;
105         struct sk_buff *skb;
106 -       struct sk_buff_head *tid_q;
107 +       bool closed = false;
108  
109 -       do {
110 -               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
111 -               if (!bf) {
112 -                       status = ATH_AGGR_BAW_CLOSED;
113 -                       break;
114 -               }
115 +       bf = bf_first;
116 +       aggr_limit = ath_lookup_rate(sc, bf, tid);
117  
118 +       do {
119                 skb = bf->bf_mpdu;
120                 fi = get_frame_info(skb);
121  
122 -               if (!bf_first) {
123 -                       bf_first = bf;
124 -                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
125 -                       aggr_limit = ath_lookup_rate(sc, bf, tid);
126 -               }
127 -
128                 /* do not exceed aggregation limit */
129                 al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
130                 if (nframes) {
131                         if (aggr_limit < al + bpad + al_delta ||
132 -                           ath_lookup_legacy(bf) || nframes >= h_baw) {
133 -                               status = ATH_AGGR_LIMITED;
134 +                           ath_lookup_legacy(bf) || nframes >= h_baw)
135                                 break;
136 -                       }
137  
138                         tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
139 -                       if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
140 +                       if ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
141 +                           !(tx_info->flags & IEEE80211_TX_CTL_AMPDU))
142                                 break;
143                 }
144  
145 @@ -984,11 +1002,26 @@ static enum ATH_AGGR_STATUS ath_tx_form_
146  
147                 bf_prev = bf;
148  
149 +               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
150 +               if (!bf) {
151 +                       closed = true;
152 +                       break;
153 +               }
154         } while (ath_tid_has_buffered(tid));
155  
156 +       bf = bf_first;
157 +       bf->bf_lastbf = bf_prev;
158 +
159 +       if (bf == bf_prev) {
160 +               al = get_frame_info(bf->bf_mpdu)->framelen;
161 +               bf->bf_state.bf_type = BUF_AMPDU;
162 +       } else {
163 +               TX_STAT_INC(txq->axq_qnum, a_aggr);
164 +       }
165 +
166         *aggr_len = al;
167  
168 -       return status;
169 +       return closed;
170  #undef PADBYTES
171  }
172  
173 @@ -1249,14 +1282,50 @@ static void ath_tx_fill_desc(struct ath_
174         }
175  }
176  
177 +static void
178 +ath_tx_form_burst(struct ath_softc *sc, struct ath_txq *txq,
179 +                 struct ath_atx_tid *tid, struct list_head *bf_q,
180 +                 struct ath_buf *bf_first, struct sk_buff_head *tid_q)
181 +{
182 +       struct ath_buf *bf = bf_first, *bf_prev = NULL;
183 +       struct sk_buff *skb;
184 +       int nframes = 0;
185 +
186 +       do {
187 +               struct ieee80211_tx_info *tx_info;
188 +               skb = bf->bf_mpdu;
189 +
190 +               nframes++;
191 +               __skb_unlink(skb, tid_q);
192 +               list_add_tail(&bf->list, bf_q);
193 +               if (bf_prev)
194 +                       bf_prev->bf_next = bf;
195 +               bf_prev = bf;
196 +
197 +               if (nframes >= 2)
198 +                       break;
199 +
200 +               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
201 +               if (!bf)
202 +                       break;
203 +
204 +               tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
205 +               if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
206 +                       break;
207 +
208 +               ath_set_rates(tid->an->vif, tid->an->sta, bf);
209 +       } while (1);
210 +}
211 +
212  static void ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
213                               struct ath_atx_tid *tid)
214  {
215         struct ath_buf *bf;
216 -       enum ATH_AGGR_STATUS status;
217         struct ieee80211_tx_info *tx_info;
218 +       struct sk_buff_head *tid_q;
219         struct list_head bf_q;
220 -       int aggr_len;
221 +       int aggr_len = 0;
222 +       bool aggr, last = true;
223  
224         do {
225                 if (!ath_tid_has_buffered(tid))
226 @@ -1264,38 +1333,34 @@ static void ath_tx_sched_aggr(struct ath
227  
228                 INIT_LIST_HEAD(&bf_q);
229  
230 -               status = ath_tx_form_aggr(sc, txq, tid, &bf_q, &aggr_len);
231 -
232 -               /*
233 -                * no frames picked up to be aggregated;
234 -                * block-ack window is not open.
235 -                */
236 -               if (list_empty(&bf_q))
237 +               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
238 +               if (!bf)
239                         break;
240  
241 -               bf = list_first_entry(&bf_q, struct ath_buf, list);
242 -               bf->bf_lastbf = list_entry(bf_q.prev, struct ath_buf, list);
243                 tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
244 +               aggr = !!(tx_info->flags & IEEE80211_TX_CTL_AMPDU);
245 +               if ((aggr && txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) ||
246 +                   (!aggr && txq->axq_depth >= ATH_NON_AGGR_MIN_QDEPTH))
247 +                       break;
248 +
249 +               ath_set_rates(tid->an->vif, tid->an->sta, bf);
250 +               if (aggr)
251 +                       last = ath_tx_form_aggr(sc, txq, tid, &bf_q, bf,
252 +                                               tid_q, &aggr_len);
253 +               else
254 +                       ath_tx_form_burst(sc, txq, tid, &bf_q, bf, tid_q);
255 +
256 +               if (list_empty(&bf_q))
257 +                       return;
258  
259                 if (tid->ac->clear_ps_filter) {
260                         tid->ac->clear_ps_filter = false;
261                         tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
262 -               } else {
263 -                       tx_info->flags &= ~IEEE80211_TX_CTL_CLEAR_PS_FILT;
264 -               }
265 -
266 -               /* if only one frame, send as non-aggregate */
267 -               if (bf == bf->bf_lastbf) {
268 -                       aggr_len = get_frame_info(bf->bf_mpdu)->framelen;
269 -                       bf->bf_state.bf_type = BUF_AMPDU;
270 -               } else {
271 -                       TX_STAT_INC(txq->axq_qnum, a_aggr);
272                 }
273  
274                 ath_tx_fill_desc(sc, bf, txq, aggr_len);
275                 ath_tx_txqaddbuf(sc, txq, &bf_q, false);
276 -       } while (txq->axq_ampdu_depth < ATH_AGGR_MIN_QDEPTH &&
277 -                status != ATH_AGGR_BAW_CLOSED);
278 +       } while (!last);
279  }
280  
281  int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
282 @@ -1319,6 +1384,9 @@ int ath_tx_aggr_start(struct ath_softc *
283                 an->mpdudensity = density;
284         }
285  
286 +       /* force sequence number allocation for pending frames */
287 +       ath_tx_tid_change_state(sc, txtid);
288 +
289         txtid->active = true;
290         txtid->paused = true;
291         *ssn = txtid->seq_start = txtid->seq_next;
292 @@ -1340,6 +1408,7 @@ void ath_tx_aggr_stop(struct ath_softc *
293         txtid->active = false;
294         txtid->paused = false;
295         ath_tx_flush_tid(sc, txtid);
296 +       ath_tx_tid_change_state(sc, txtid);
297         ath_txq_unlock_complete(sc, txq);
298  }
299  
300 @@ -1854,58 +1923,6 @@ static void ath_tx_txqaddbuf(struct ath_
301         }
302  }
303  
304 -static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_txq *txq,
305 -                             struct ath_atx_tid *tid, struct sk_buff *skb,
306 -                             struct ath_tx_control *txctl)
307 -{
308 -       struct ath_frame_info *fi = get_frame_info(skb);
309 -       struct list_head bf_head;
310 -       struct ath_buf *bf;
311 -
312 -       /*
313 -        * Do not queue to h/w when any of the following conditions is true:
314 -        * - there are pending frames in software queue
315 -        * - the TID is currently paused for ADDBA/BAR request
316 -        * - seqno is not within block-ack window
317 -        * - h/w queue depth exceeds low water mark
318 -        */
319 -       if ((ath_tid_has_buffered(tid) || tid->paused ||
320 -            !BAW_WITHIN(tid->seq_start, tid->baw_size, tid->seq_next) ||
321 -            txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) &&
322 -           txq != sc->tx.uapsdq) {
323 -               /*
324 -                * Add this frame to software queue for scheduling later
325 -                * for aggregation.
326 -                */
327 -               TX_STAT_INC(txq->axq_qnum, a_queued_sw);
328 -               __skb_queue_tail(&tid->buf_q, skb);
329 -               if (!txctl->an || !txctl->an->sleeping)
330 -                       ath_tx_queue_tid(txq, tid);
331 -               return;
332 -       }
333 -
334 -       bf = ath_tx_setup_buffer(sc, txq, tid, skb);
335 -       if (!bf) {
336 -               ath_txq_skb_done(sc, txq, skb);
337 -               ieee80211_free_txskb(sc->hw, skb);
338 -               return;
339 -       }
340 -
341 -       ath_set_rates(tid->an->vif, tid->an->sta, bf);
342 -       bf->bf_state.bf_type = BUF_AMPDU;
343 -       INIT_LIST_HEAD(&bf_head);
344 -       list_add(&bf->list, &bf_head);
345 -
346 -       /* Add sub-frame to BAW */
347 -       ath_tx_addto_baw(sc, tid, bf);
348 -
349 -       /* Queue to h/w without aggregation */
350 -       TX_STAT_INC(txq->axq_qnum, a_queued_hw);
351 -       bf->bf_lastbf = bf;
352 -       ath_tx_fill_desc(sc, bf, txq, fi->framelen);
353 -       ath_tx_txqaddbuf(sc, txq, &bf_head, false);
354 -}
355 -
356  static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
357                                struct ath_atx_tid *tid, struct sk_buff *skb)
358  {
359 @@ -2131,20 +2148,25 @@ int ath_tx_start(struct ieee80211_hw *hw
360                 ath_txq_unlock(sc, txq);
361                 txq = sc->tx.uapsdq;
362                 ath_txq_lock(sc, txq);
363 -       }
364 -
365 -       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
366 +       } else if (txctl->an &&
367 +                  ieee80211_is_data_present(hdr->frame_control)) {
368                 tid = ath_get_skb_tid(sc, txctl->an, skb);
369  
370                 WARN_ON(tid->ac->txq != txctl->txq);
371 -       }
372  
373 -       if ((info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
374 +               if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
375 +                       tid->ac->clear_ps_filter = true;
376 +
377                 /*
378 -                * Try aggregation if it's a unicast data frame
379 -                * and the destination is HT capable.
380 +                * Add this frame to software queue for scheduling later
381 +                * for aggregation.
382                  */
383 -               ath_tx_send_ampdu(sc, txq, tid, skb, txctl);
384 +               TX_STAT_INC(txq->axq_qnum, a_queued_sw);
385 +               __skb_queue_tail(&tid->buf_q, skb);
386 +               if (!txctl->an->sleeping)
387 +                       ath_tx_queue_tid(txq, tid);
388 +
389 +               ath_txq_schedule(sc, txq);
390                 goto out;
391         }
392  
393 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
394 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
395 @@ -137,6 +137,8 @@ int ath_descdma_setup(struct ath_softc *
396  #define ATH_AGGR_ENCRYPTDELIM      10
397  /* minimum h/w qdepth to be sustained to maximize aggregation */
398  #define ATH_AGGR_MIN_QDEPTH        2
399 +/* minimum h/w qdepth for non-aggregated traffic */
400 +#define ATH_NON_AGGR_MIN_QDEPTH    8
401  
402  #define IEEE80211_SEQ_SEQ_SHIFT    4
403  #define IEEE80211_SEQ_MAX          4096
404 @@ -173,12 +175,6 @@ int ath_descdma_setup(struct ath_softc *
405  
406  #define ATH_TX_COMPLETE_POLL_INT       1000
407  
408 -enum ATH_AGGR_STATUS {
409 -       ATH_AGGR_DONE,
410 -       ATH_AGGR_BAW_CLOSED,
411 -       ATH_AGGR_LIMITED,
412 -};
413 -
414  #define ATH_TXFIFO_DEPTH 8
415  struct ath_txq {
416         int mac80211_qnum; /* mac80211 queue number, -1 means not mac80211 Q */