Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / xmit.c
1 /*
2  * Copyright (c) 2008-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "ath9k.h"
18 #include "ar9003_mac.h"
19
20 #define BITS_PER_BYTE           8
21 #define OFDM_PLCP_BITS          22
22 #define HT_RC_2_STREAMS(_rc)    ((((_rc) & 0x78) >> 3) + 1)
23 #define L_STF                   8
24 #define L_LTF                   8
25 #define L_SIG                   4
26 #define HT_SIG                  8
27 #define HT_STF                  4
28 #define HT_LTF(_ns)             (4 * (_ns))
29 #define SYMBOL_TIME(_ns)        ((_ns) << 2) /* ns * 4 us */
30 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)  /* ns * 3.6 us */
31 #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
32 #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
33
34
35 static u16 bits_per_symbol[][2] = {
36         /* 20MHz 40MHz */
37         {    26,   54 },     /*  0: BPSK */
38         {    52,  108 },     /*  1: QPSK 1/2 */
39         {    78,  162 },     /*  2: QPSK 3/4 */
40         {   104,  216 },     /*  3: 16-QAM 1/2 */
41         {   156,  324 },     /*  4: 16-QAM 3/4 */
42         {   208,  432 },     /*  5: 64-QAM 2/3 */
43         {   234,  486 },     /*  6: 64-QAM 3/4 */
44         {   260,  540 },     /*  7: 64-QAM 5/6 */
45 };
46
47 #define IS_HT_RATE(_rate)     ((_rate) & 0x80)
48
49 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
50                                struct ath_atx_tid *tid,
51                                struct list_head *bf_head);
52 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
53                                 struct ath_txq *txq, struct list_head *bf_q,
54                                 struct ath_tx_status *ts, int txok, int sendbar);
55 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
56                              struct list_head *head, bool internal);
57 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf, int len);
58 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
59                              struct ath_tx_status *ts, int nframes, int nbad,
60                              int txok, bool update_rc);
61 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
62                               int seqno);
63
64 enum {
65         MCS_HT20,
66         MCS_HT20_SGI,
67         MCS_HT40,
68         MCS_HT40_SGI,
69 };
70
71 static int ath_max_4ms_framelen[4][32] = {
72         [MCS_HT20] = {
73                 3212,  6432,  9648,  12864,  19300,  25736,  28952,  32172,
74                 6424,  12852, 19280, 25708,  38568,  51424,  57852,  64280,
75                 9628,  19260, 28896, 38528,  57792,  65532,  65532,  65532,
76                 12828, 25656, 38488, 51320,  65532,  65532,  65532,  65532,
77         },
78         [MCS_HT20_SGI] = {
79                 3572,  7144,  10720,  14296,  21444,  28596,  32172,  35744,
80                 7140,  14284, 21428,  28568,  42856,  57144,  64288,  65532,
81                 10700, 21408, 32112,  42816,  64228,  65532,  65532,  65532,
82                 14256, 28516, 42780,  57040,  65532,  65532,  65532,  65532,
83         },
84         [MCS_HT40] = {
85                 6680,  13360,  20044,  26724,  40092,  53456,  60140,  65532,
86                 13348, 26700,  40052,  53400,  65532,  65532,  65532,  65532,
87                 20004, 40008,  60016,  65532,  65532,  65532,  65532,  65532,
88                 26644, 53292,  65532,  65532,  65532,  65532,  65532,  65532,
89         },
90         [MCS_HT40_SGI] = {
91                 7420,  14844,  22272,  29696,  44544,  59396,  65532,  65532,
92                 14832, 29668,  44504,  59340,  65532,  65532,  65532,  65532,
93                 22232, 44464,  65532,  65532,  65532,  65532,  65532,  65532,
94                 29616, 59232,  65532,  65532,  65532,  65532,  65532,  65532,
95         }
96 };
97
98 /*********************/
99 /* Aggregation logic */
100 /*********************/
101
102 static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
103 {
104         struct ath_atx_ac *ac = tid->ac;
105
106         if (tid->paused)
107                 return;
108
109         if (tid->sched)
110                 return;
111
112         tid->sched = true;
113         list_add_tail(&tid->list, &ac->tid_q);
114
115         if (ac->sched)
116                 return;
117
118         ac->sched = true;
119         list_add_tail(&ac->list, &txq->axq_acq);
120 }
121
122 static void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
123 {
124         struct ath_txq *txq = tid->ac->txq;
125
126         WARN_ON(!tid->paused);
127
128         spin_lock_bh(&txq->axq_lock);
129         tid->paused = false;
130
131         if (list_empty(&tid->buf_q))
132                 goto unlock;
133
134         ath_tx_queue_tid(txq, tid);
135         ath_txq_schedule(sc, txq);
136 unlock:
137         spin_unlock_bh(&txq->axq_lock);
138 }
139
140 static struct ath_frame_info *get_frame_info(struct sk_buff *skb)
141 {
142         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
143         BUILD_BUG_ON(sizeof(struct ath_frame_info) >
144                      sizeof(tx_info->rate_driver_data));
145         return (struct ath_frame_info *) &tx_info->rate_driver_data[0];
146 }
147
148 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
149 {
150         struct ath_txq *txq = tid->ac->txq;
151         struct ath_buf *bf;
152         struct list_head bf_head;
153         struct ath_tx_status ts;
154         struct ath_frame_info *fi;
155
156         INIT_LIST_HEAD(&bf_head);
157
158         memset(&ts, 0, sizeof(ts));
159         spin_lock_bh(&txq->axq_lock);
160
161         while (!list_empty(&tid->buf_q)) {
162                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
163                 list_move_tail(&bf->list, &bf_head);
164
165                 spin_unlock_bh(&txq->axq_lock);
166                 fi = get_frame_info(bf->bf_mpdu);
167                 if (fi->retries) {
168                         ath_tx_update_baw(sc, tid, fi->seqno);
169                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 1);
170                 } else {
171                         ath_tx_send_normal(sc, txq, NULL, &bf_head);
172                 }
173                 spin_lock_bh(&txq->axq_lock);
174         }
175
176         spin_unlock_bh(&txq->axq_lock);
177 }
178
179 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
180                               int seqno)
181 {
182         int index, cindex;
183
184         index  = ATH_BA_INDEX(tid->seq_start, seqno);
185         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
186
187         __clear_bit(cindex, tid->tx_buf);
188
189         while (tid->baw_head != tid->baw_tail && !test_bit(tid->baw_head, tid->tx_buf)) {
190                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
191                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
192         }
193 }
194
195 static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
196                              u16 seqno)
197 {
198         int index, cindex;
199
200         index  = ATH_BA_INDEX(tid->seq_start, seqno);
201         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
202         __set_bit(cindex, tid->tx_buf);
203
204         if (index >= ((tid->baw_tail - tid->baw_head) &
205                 (ATH_TID_MAX_BUFS - 1))) {
206                 tid->baw_tail = cindex;
207                 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
208         }
209 }
210
211 /*
212  * TODO: For frame(s) that are in the retry state, we will reuse the
213  * sequence number(s) without setting the retry bit. The
214  * alternative is to give up on these and BAR the receiver's window
215  * forward.
216  */
217 static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
218                           struct ath_atx_tid *tid)
219
220 {
221         struct ath_buf *bf;
222         struct list_head bf_head;
223         struct ath_tx_status ts;
224         struct ath_frame_info *fi;
225
226         memset(&ts, 0, sizeof(ts));
227         INIT_LIST_HEAD(&bf_head);
228
229         for (;;) {
230                 if (list_empty(&tid->buf_q))
231                         break;
232
233                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
234                 list_move_tail(&bf->list, &bf_head);
235
236                 fi = get_frame_info(bf->bf_mpdu);
237                 if (fi->retries)
238                         ath_tx_update_baw(sc, tid, fi->seqno);
239
240                 spin_unlock(&txq->axq_lock);
241                 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 0);
242                 spin_lock(&txq->axq_lock);
243         }
244
245         tid->seq_next = tid->seq_start;
246         tid->baw_tail = tid->baw_head;
247 }
248
249 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_txq *txq,
250                              struct sk_buff *skb)
251 {
252         struct ath_frame_info *fi = get_frame_info(skb);
253         struct ieee80211_hdr *hdr;
254
255         TX_STAT_INC(txq->axq_qnum, a_retries);
256         if (fi->retries++ > 0)
257                 return;
258
259         hdr = (struct ieee80211_hdr *)skb->data;
260         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
261 }
262
263 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
264 {
265         struct ath_buf *bf = NULL;
266
267         spin_lock_bh(&sc->tx.txbuflock);
268
269         if (unlikely(list_empty(&sc->tx.txbuf))) {
270                 spin_unlock_bh(&sc->tx.txbuflock);
271                 return NULL;
272         }
273
274         bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
275         list_del(&bf->list);
276
277         spin_unlock_bh(&sc->tx.txbuflock);
278
279         return bf;
280 }
281
282 static void ath_tx_return_buffer(struct ath_softc *sc, struct ath_buf *bf)
283 {
284         spin_lock_bh(&sc->tx.txbuflock);
285         list_add_tail(&bf->list, &sc->tx.txbuf);
286         spin_unlock_bh(&sc->tx.txbuflock);
287 }
288
289 static struct ath_buf* ath_clone_txbuf(struct ath_softc *sc, struct ath_buf *bf)
290 {
291         struct ath_buf *tbf;
292
293         tbf = ath_tx_get_buffer(sc);
294         if (WARN_ON(!tbf))
295                 return NULL;
296
297         ATH_TXBUF_RESET(tbf);
298
299         tbf->bf_mpdu = bf->bf_mpdu;
300         tbf->bf_buf_addr = bf->bf_buf_addr;
301         memcpy(tbf->bf_desc, bf->bf_desc, sc->sc_ah->caps.tx_desc_len);
302         tbf->bf_state = bf->bf_state;
303
304         return tbf;
305 }
306
307 static void ath_tx_count_frames(struct ath_softc *sc, struct ath_buf *bf,
308                                 struct ath_tx_status *ts, int txok,
309                                 int *nframes, int *nbad)
310 {
311         struct ath_frame_info *fi;
312         u16 seq_st = 0;
313         u32 ba[WME_BA_BMP_SIZE >> 5];
314         int ba_index;
315         int isaggr = 0;
316
317         *nbad = 0;
318         *nframes = 0;
319
320         isaggr = bf_isaggr(bf);
321         if (isaggr) {
322                 seq_st = ts->ts_seqnum;
323                 memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
324         }
325
326         while (bf) {
327                 fi = get_frame_info(bf->bf_mpdu);
328                 ba_index = ATH_BA_INDEX(seq_st, fi->seqno);
329
330                 (*nframes)++;
331                 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
332                         (*nbad)++;
333
334                 bf = bf->bf_next;
335         }
336 }
337
338
339 static void ath_tx_complete_aggr(struct ath_softc *sc, struct ath_txq *txq,
340                                  struct ath_buf *bf, struct list_head *bf_q,
341                                  struct ath_tx_status *ts, int txok, bool retry)
342 {
343         struct ath_node *an = NULL;
344         struct sk_buff *skb;
345         struct ieee80211_sta *sta;
346         struct ieee80211_hw *hw = sc->hw;
347         struct ieee80211_hdr *hdr;
348         struct ieee80211_tx_info *tx_info;
349         struct ath_atx_tid *tid = NULL;
350         struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
351         struct list_head bf_head, bf_pending;
352         u16 seq_st = 0, acked_cnt = 0, txfail_cnt = 0;
353         u32 ba[WME_BA_BMP_SIZE >> 5];
354         int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
355         bool rc_update = true;
356         struct ieee80211_tx_rate rates[4];
357         struct ath_frame_info *fi;
358         int nframes;
359         u8 tidno;
360         bool clear_filter;
361
362         skb = bf->bf_mpdu;
363         hdr = (struct ieee80211_hdr *)skb->data;
364
365         tx_info = IEEE80211_SKB_CB(skb);
366
367         memcpy(rates, tx_info->control.rates, sizeof(rates));
368
369         rcu_read_lock();
370
371         sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr1, hdr->addr2);
372         if (!sta) {
373                 rcu_read_unlock();
374
375                 INIT_LIST_HEAD(&bf_head);
376                 while (bf) {
377                         bf_next = bf->bf_next;
378
379                         bf->bf_state.bf_type |= BUF_XRETRY;
380                         if (!bf->bf_stale || bf_next != NULL)
381                                 list_move_tail(&bf->list, &bf_head);
382
383                         ath_tx_rc_status(sc, bf, ts, 1, 1, 0, false);
384                         ath_tx_complete_buf(sc, bf, txq, &bf_head, ts,
385                                 0, 0);
386
387                         bf = bf_next;
388                 }
389                 return;
390         }
391
392         an = (struct ath_node *)sta->drv_priv;
393         tidno = ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
394         tid = ATH_AN_2_TID(an, tidno);
395
396         /*
397          * The hardware occasionally sends a tx status for the wrong TID.
398          * In this case, the BA status cannot be considered valid and all
399          * subframes need to be retransmitted
400          */
401         if (tidno != ts->tid)
402                 txok = false;
403
404         isaggr = bf_isaggr(bf);
405         memset(ba, 0, WME_BA_BMP_SIZE >> 3);
406
407         if (isaggr && txok) {
408                 if (ts->ts_flags & ATH9K_TX_BA) {
409                         seq_st = ts->ts_seqnum;
410                         memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
411                 } else {
412                         /*
413                          * AR5416 can become deaf/mute when BA
414                          * issue happens. Chip needs to be reset.
415                          * But AP code may have sychronization issues
416                          * when perform internal reset in this routine.
417                          * Only enable reset in STA mode for now.
418                          */
419                         if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION)
420                                 needreset = 1;
421                 }
422         }
423
424         INIT_LIST_HEAD(&bf_pending);
425         INIT_LIST_HEAD(&bf_head);
426
427         ath_tx_count_frames(sc, bf, ts, txok, &nframes, &nbad);
428         while (bf) {
429                 txfail = txpending = sendbar = 0;
430                 bf_next = bf->bf_next;
431
432                 skb = bf->bf_mpdu;
433                 tx_info = IEEE80211_SKB_CB(skb);
434                 fi = get_frame_info(skb);
435
436                 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, fi->seqno))) {
437                         /* transmit completion, subframe is
438                          * acked by block ack */
439                         acked_cnt++;
440                 } else if (!isaggr && txok) {
441                         /* transmit completion */
442                         acked_cnt++;
443                 } else {
444                         if ((tid->state & AGGR_CLEANUP) || !retry) {
445                                 /*
446                                  * cleanup in progress, just fail
447                                  * the un-acked sub-frames
448                                  */
449                                 txfail = 1;
450                         } else if (fi->retries < ATH_MAX_SW_RETRIES) {
451                                 if (!(ts->ts_status & ATH9K_TXERR_FILT) ||
452                                     !an->sleeping)
453                                         ath_tx_set_retry(sc, txq, bf->bf_mpdu);
454
455                                 clear_filter = true;
456                                 txpending = 1;
457                         } else {
458                                 bf->bf_state.bf_type |= BUF_XRETRY;
459                                 txfail = 1;
460                                 sendbar = 1;
461                                 txfail_cnt++;
462                         }
463                 }
464
465                 /*
466                  * Make sure the last desc is reclaimed if it
467                  * not a holding desc.
468                  */
469                 if (!bf_last->bf_stale || bf_next != NULL)
470                         list_move_tail(&bf->list, &bf_head);
471                 else
472                         INIT_LIST_HEAD(&bf_head);
473
474                 if (!txpending || (tid->state & AGGR_CLEANUP)) {
475                         /*
476                          * complete the acked-ones/xretried ones; update
477                          * block-ack window
478                          */
479                         spin_lock_bh(&txq->axq_lock);
480                         ath_tx_update_baw(sc, tid, fi->seqno);
481                         spin_unlock_bh(&txq->axq_lock);
482
483                         if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
484                                 memcpy(tx_info->control.rates, rates, sizeof(rates));
485                                 ath_tx_rc_status(sc, bf, ts, nframes, nbad, txok, true);
486                                 rc_update = false;
487                         } else {
488                                 ath_tx_rc_status(sc, bf, ts, nframes, nbad, txok, false);
489                         }
490
491                         ath_tx_complete_buf(sc, bf, txq, &bf_head, ts,
492                                 !txfail, sendbar);
493                 } else {
494                         /* retry the un-acked ones */
495                         ath9k_hw_set_clrdmask(sc->sc_ah, bf->bf_desc, false);
496                         if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)) {
497                                 if (bf->bf_next == NULL && bf_last->bf_stale) {
498                                         struct ath_buf *tbf;
499
500                                         tbf = ath_clone_txbuf(sc, bf_last);
501                                         /*
502                                          * Update tx baw and complete the
503                                          * frame with failed status if we
504                                          * run out of tx buf.
505                                          */
506                                         if (!tbf) {
507                                                 spin_lock_bh(&txq->axq_lock);
508                                                 ath_tx_update_baw(sc, tid, fi->seqno);
509                                                 spin_unlock_bh(&txq->axq_lock);
510
511                                                 bf->bf_state.bf_type |=
512                                                         BUF_XRETRY;
513                                                 ath_tx_rc_status(sc, bf, ts, nframes,
514                                                                 nbad, 0, false);
515                                                 ath_tx_complete_buf(sc, bf, txq,
516                                                                     &bf_head,
517                                                                     ts, 0, 0);
518                                                 break;
519                                         }
520
521                                         ath9k_hw_cleartxdesc(sc->sc_ah,
522                                                              tbf->bf_desc);
523                                         list_add_tail(&tbf->list, &bf_head);
524                                 } else {
525                                         /*
526                                          * Clear descriptor status words for
527                                          * software retry
528                                          */
529                                         ath9k_hw_cleartxdesc(sc->sc_ah,
530                                                              bf->bf_desc);
531                                 }
532                         }
533
534                         /*
535                          * Put this buffer to the temporary pending
536                          * queue to retain ordering
537                          */
538                         list_splice_tail_init(&bf_head, &bf_pending);
539                 }
540
541                 bf = bf_next;
542         }
543
544         /* prepend un-acked frames to the beginning of the pending frame queue */
545         if (!list_empty(&bf_pending)) {
546                 if (an->sleeping)
547                         ieee80211_sta_set_tim(sta);
548
549                 spin_lock_bh(&txq->axq_lock);
550                 if (clear_filter)
551                         tid->ac->clear_ps_filter = true;
552                 list_splice(&bf_pending, &tid->buf_q);
553                 ath_tx_queue_tid(txq, tid);
554                 spin_unlock_bh(&txq->axq_lock);
555         }
556
557         if (tid->state & AGGR_CLEANUP) {
558                 ath_tx_flush_tid(sc, tid);
559
560                 if (tid->baw_head == tid->baw_tail) {
561                         tid->state &= ~AGGR_ADDBA_COMPLETE;
562                         tid->state &= ~AGGR_CLEANUP;
563                 }
564         }
565
566         rcu_read_unlock();
567
568         if (needreset)
569                 ath_reset(sc, false);
570 }
571
572 static u32 ath_lookup_rate(struct ath_softc *sc, struct ath_buf *bf,
573                            struct ath_atx_tid *tid)
574 {
575         struct sk_buff *skb;
576         struct ieee80211_tx_info *tx_info;
577         struct ieee80211_tx_rate *rates;
578         u32 max_4ms_framelen, frmlen;
579         u16 aggr_limit, legacy = 0;
580         int i;
581
582         skb = bf->bf_mpdu;
583         tx_info = IEEE80211_SKB_CB(skb);
584         rates = tx_info->control.rates;
585
586         /*
587          * Find the lowest frame length among the rate series that will have a
588          * 4ms transmit duration.
589          * TODO - TXOP limit needs to be considered.
590          */
591         max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
592
593         for (i = 0; i < 4; i++) {
594                 if (rates[i].count) {
595                         int modeidx;
596                         if (!(rates[i].flags & IEEE80211_TX_RC_MCS)) {
597                                 legacy = 1;
598                                 break;
599                         }
600
601                         if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
602                                 modeidx = MCS_HT40;
603                         else
604                                 modeidx = MCS_HT20;
605
606                         if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
607                                 modeidx++;
608
609                         frmlen = ath_max_4ms_framelen[modeidx][rates[i].idx];
610                         max_4ms_framelen = min(max_4ms_framelen, frmlen);
611                 }
612         }
613
614         /*
615          * limit aggregate size by the minimum rate if rate selected is
616          * not a probe rate, if rate selected is a probe rate then
617          * avoid aggregation of this packet.
618          */
619         if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
620                 return 0;
621
622         if (sc->sc_flags & SC_OP_BT_PRIORITY_DETECTED)
623                 aggr_limit = min((max_4ms_framelen * 3) / 8,
624                                  (u32)ATH_AMPDU_LIMIT_MAX);
625         else
626                 aggr_limit = min(max_4ms_framelen,
627                                  (u32)ATH_AMPDU_LIMIT_MAX);
628
629         /*
630          * h/w can accept aggregates up to 16 bit lengths (65535).
631          * The IE, however can hold up to 65536, which shows up here
632          * as zero. Ignore 65536 since we  are constrained by hw.
633          */
634         if (tid->an->maxampdu)
635                 aggr_limit = min(aggr_limit, tid->an->maxampdu);
636
637         return aggr_limit;
638 }
639
640 /*
641  * Returns the number of delimiters to be added to
642  * meet the minimum required mpdudensity.
643  */
644 static int ath_compute_num_delims(struct ath_softc *sc, struct ath_atx_tid *tid,
645                                   struct ath_buf *bf, u16 frmlen)
646 {
647         struct sk_buff *skb = bf->bf_mpdu;
648         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
649         u32 nsymbits, nsymbols;
650         u16 minlen;
651         u8 flags, rix;
652         int width, streams, half_gi, ndelim, mindelim;
653         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
654
655         /* Select standard number of delimiters based on frame length alone */
656         ndelim = ATH_AGGR_GET_NDELIM(frmlen);
657
658         /*
659          * If encryption enabled, hardware requires some more padding between
660          * subframes.
661          * TODO - this could be improved to be dependent on the rate.
662          *      The hardware can keep up at lower rates, but not higher rates
663          */
664         if (fi->keyix != ATH9K_TXKEYIX_INVALID)
665                 ndelim += ATH_AGGR_ENCRYPTDELIM;
666
667         /*
668          * Convert desired mpdu density from microeconds to bytes based
669          * on highest rate in rate series (i.e. first rate) to determine
670          * required minimum length for subframe. Take into account
671          * whether high rate is 20 or 40Mhz and half or full GI.
672          *
673          * If there is no mpdu density restriction, no further calculation
674          * is needed.
675          */
676
677         if (tid->an->mpdudensity == 0)
678                 return ndelim;
679
680         rix = tx_info->control.rates[0].idx;
681         flags = tx_info->control.rates[0].flags;
682         width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
683         half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
684
685         if (half_gi)
686                 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(tid->an->mpdudensity);
687         else
688                 nsymbols = NUM_SYMBOLS_PER_USEC(tid->an->mpdudensity);
689
690         if (nsymbols == 0)
691                 nsymbols = 1;
692
693         streams = HT_RC_2_STREAMS(rix);
694         nsymbits = bits_per_symbol[rix % 8][width] * streams;
695         minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
696
697         if (frmlen < minlen) {
698                 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
699                 ndelim = max(mindelim, ndelim);
700         }
701
702         return ndelim;
703 }
704
705 static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
706                                              struct ath_txq *txq,
707                                              struct ath_atx_tid *tid,
708                                              struct list_head *bf_q,
709                                              int *aggr_len)
710 {
711 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
712         struct ath_buf *bf, *bf_first, *bf_prev = NULL;
713         int rl = 0, nframes = 0, ndelim, prev_al = 0;
714         u16 aggr_limit = 0, al = 0, bpad = 0,
715                 al_delta, h_baw = tid->baw_size / 2;
716         enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
717         struct ieee80211_tx_info *tx_info;
718         struct ath_frame_info *fi;
719
720         bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
721
722         do {
723                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
724                 fi = get_frame_info(bf->bf_mpdu);
725
726                 /* do not step over block-ack window */
727                 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, fi->seqno)) {
728                         status = ATH_AGGR_BAW_CLOSED;
729                         break;
730                 }
731
732                 if (!rl) {
733                         aggr_limit = ath_lookup_rate(sc, bf, tid);
734                         rl = 1;
735                 }
736
737                 /* do not exceed aggregation limit */
738                 al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
739
740                 if (nframes &&
741                     (aggr_limit < (al + bpad + al_delta + prev_al))) {
742                         status = ATH_AGGR_LIMITED;
743                         break;
744                 }
745
746                 tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
747                 if (nframes && ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
748                         !(tx_info->control.rates[0].flags & IEEE80211_TX_RC_MCS)))
749                         break;
750
751                 /* do not exceed subframe limit */
752                 if (nframes >= min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
753                         status = ATH_AGGR_LIMITED;
754                         break;
755                 }
756                 nframes++;
757
758                 /* add padding for previous frame to aggregation length */
759                 al += bpad + al_delta;
760
761                 /*
762                  * Get the delimiters needed to meet the MPDU
763                  * density for this node.
764                  */
765                 ndelim = ath_compute_num_delims(sc, tid, bf_first, fi->framelen);
766                 bpad = PADBYTES(al_delta) + (ndelim << 2);
767
768                 bf->bf_next = NULL;
769                 ath9k_hw_set_desc_link(sc->sc_ah, bf->bf_desc, 0);
770
771                 /* link buffers of this frame to the aggregate */
772                 if (!fi->retries)
773                         ath_tx_addto_baw(sc, tid, fi->seqno);
774                 ath9k_hw_set11n_aggr_middle(sc->sc_ah, bf->bf_desc, ndelim);
775                 list_move_tail(&bf->list, bf_q);
776                 if (bf_prev) {
777                         bf_prev->bf_next = bf;
778                         ath9k_hw_set_desc_link(sc->sc_ah, bf_prev->bf_desc,
779                                                bf->bf_daddr);
780                 }
781                 bf_prev = bf;
782
783         } while (!list_empty(&tid->buf_q));
784
785         *aggr_len = al;
786
787         return status;
788 #undef PADBYTES
789 }
790
791 static void ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
792                               struct ath_atx_tid *tid)
793 {
794         struct ath_buf *bf;
795         enum ATH_AGGR_STATUS status;
796         struct ath_frame_info *fi;
797         struct list_head bf_q;
798         int aggr_len;
799
800         do {
801                 if (list_empty(&tid->buf_q))
802                         return;
803
804                 INIT_LIST_HEAD(&bf_q);
805
806                 status = ath_tx_form_aggr(sc, txq, tid, &bf_q, &aggr_len);
807
808                 /*
809                  * no frames picked up to be aggregated;
810                  * block-ack window is not open.
811                  */
812                 if (list_empty(&bf_q))
813                         break;
814
815                 bf = list_first_entry(&bf_q, struct ath_buf, list);
816                 bf->bf_lastbf = list_entry(bf_q.prev, struct ath_buf, list);
817
818                 if (tid->ac->clear_ps_filter) {
819                         tid->ac->clear_ps_filter = false;
820                         ath9k_hw_set_clrdmask(sc->sc_ah, bf->bf_desc, true);
821                 }
822
823                 /* if only one frame, send as non-aggregate */
824                 if (bf == bf->bf_lastbf) {
825                         fi = get_frame_info(bf->bf_mpdu);
826
827                         bf->bf_state.bf_type &= ~BUF_AGGR;
828                         ath9k_hw_clr11n_aggr(sc->sc_ah, bf->bf_desc);
829                         ath_buf_set_rate(sc, bf, fi->framelen);
830                         ath_tx_txqaddbuf(sc, txq, &bf_q, false);
831                         continue;
832                 }
833
834                 /* setup first desc of aggregate */
835                 bf->bf_state.bf_type |= BUF_AGGR;
836                 ath_buf_set_rate(sc, bf, aggr_len);
837                 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, aggr_len);
838
839                 /* anchor last desc of aggregate */
840                 ath9k_hw_set11n_aggr_last(sc->sc_ah, bf->bf_lastbf->bf_desc);
841
842                 ath_tx_txqaddbuf(sc, txq, &bf_q, false);
843                 TX_STAT_INC(txq->axq_qnum, a_aggr);
844
845         } while (txq->axq_ampdu_depth < ATH_AGGR_MIN_QDEPTH &&
846                  status != ATH_AGGR_BAW_CLOSED);
847 }
848
849 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
850                       u16 tid, u16 *ssn)
851 {
852         struct ath_atx_tid *txtid;
853         struct ath_node *an;
854
855         an = (struct ath_node *)sta->drv_priv;
856         txtid = ATH_AN_2_TID(an, tid);
857
858         if (txtid->state & (AGGR_CLEANUP | AGGR_ADDBA_COMPLETE))
859                 return -EAGAIN;
860
861         txtid->state |= AGGR_ADDBA_PROGRESS;
862         txtid->paused = true;
863         *ssn = txtid->seq_start = txtid->seq_next;
864
865         memset(txtid->tx_buf, 0, sizeof(txtid->tx_buf));
866         txtid->baw_head = txtid->baw_tail = 0;
867
868         return 0;
869 }
870
871 void ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
872 {
873         struct ath_node *an = (struct ath_node *)sta->drv_priv;
874         struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
875         struct ath_txq *txq = txtid->ac->txq;
876
877         if (txtid->state & AGGR_CLEANUP)
878                 return;
879
880         if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
881                 txtid->state &= ~AGGR_ADDBA_PROGRESS;
882                 return;
883         }
884
885         spin_lock_bh(&txq->axq_lock);
886         txtid->paused = true;
887
888         /*
889          * If frames are still being transmitted for this TID, they will be
890          * cleaned up during tx completion. To prevent race conditions, this
891          * TID can only be reused after all in-progress subframes have been
892          * completed.
893          */
894         if (txtid->baw_head != txtid->baw_tail)
895                 txtid->state |= AGGR_CLEANUP;
896         else
897                 txtid->state &= ~AGGR_ADDBA_COMPLETE;
898         spin_unlock_bh(&txq->axq_lock);
899
900         ath_tx_flush_tid(sc, txtid);
901 }
902
903 bool ath_tx_aggr_sleep(struct ath_softc *sc, struct ath_node *an)
904 {
905         struct ath_atx_tid *tid;
906         struct ath_atx_ac *ac;
907         struct ath_txq *txq;
908         bool buffered = false;
909         int tidno;
910
911         for (tidno = 0, tid = &an->tid[tidno];
912              tidno < WME_NUM_TID; tidno++, tid++) {
913
914                 if (!tid->sched)
915                         continue;
916
917                 ac = tid->ac;
918                 txq = ac->txq;
919
920                 spin_lock_bh(&txq->axq_lock);
921
922                 if (!list_empty(&tid->buf_q))
923                         buffered = true;
924
925                 tid->sched = false;
926                 list_del(&tid->list);
927
928                 if (ac->sched) {
929                         ac->sched = false;
930                         list_del(&ac->list);
931                 }
932
933                 spin_unlock_bh(&txq->axq_lock);
934         }
935
936         return buffered;
937 }
938
939 void ath_tx_aggr_wakeup(struct ath_softc *sc, struct ath_node *an)
940 {
941         struct ath_atx_tid *tid;
942         struct ath_atx_ac *ac;
943         struct ath_txq *txq;
944         int tidno;
945
946         for (tidno = 0, tid = &an->tid[tidno];
947              tidno < WME_NUM_TID; tidno++, tid++) {
948
949                 ac = tid->ac;
950                 txq = ac->txq;
951
952                 spin_lock_bh(&txq->axq_lock);
953                 ac->clear_ps_filter = true;
954
955                 if (!list_empty(&tid->buf_q) && !tid->paused) {
956                         ath_tx_queue_tid(txq, tid);
957                         ath_txq_schedule(sc, txq);
958                 }
959
960                 spin_unlock_bh(&txq->axq_lock);
961         }
962 }
963
964 void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
965 {
966         struct ath_atx_tid *txtid;
967         struct ath_node *an;
968
969         an = (struct ath_node *)sta->drv_priv;
970
971         if (sc->sc_flags & SC_OP_TXAGGR) {
972                 txtid = ATH_AN_2_TID(an, tid);
973                 txtid->baw_size =
974                         IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
975                 txtid->state |= AGGR_ADDBA_COMPLETE;
976                 txtid->state &= ~AGGR_ADDBA_PROGRESS;
977                 ath_tx_resume_tid(sc, txtid);
978         }
979 }
980
981 /********************/
982 /* Queue Management */
983 /********************/
984
985 static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
986                                           struct ath_txq *txq)
987 {
988         struct ath_atx_ac *ac, *ac_tmp;
989         struct ath_atx_tid *tid, *tid_tmp;
990
991         list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
992                 list_del(&ac->list);
993                 ac->sched = false;
994                 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
995                         list_del(&tid->list);
996                         tid->sched = false;
997                         ath_tid_drain(sc, txq, tid);
998                 }
999         }
1000 }
1001
1002 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1003 {
1004         struct ath_hw *ah = sc->sc_ah;
1005         struct ath_common *common = ath9k_hw_common(ah);
1006         struct ath9k_tx_queue_info qi;
1007         static const int subtype_txq_to_hwq[] = {
1008                 [WME_AC_BE] = ATH_TXQ_AC_BE,
1009                 [WME_AC_BK] = ATH_TXQ_AC_BK,
1010                 [WME_AC_VI] = ATH_TXQ_AC_VI,
1011                 [WME_AC_VO] = ATH_TXQ_AC_VO,
1012         };
1013         int axq_qnum, i;
1014
1015         memset(&qi, 0, sizeof(qi));
1016         qi.tqi_subtype = subtype_txq_to_hwq[subtype];
1017         qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1018         qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1019         qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1020         qi.tqi_physCompBuf = 0;
1021
1022         /*
1023          * Enable interrupts only for EOL and DESC conditions.
1024          * We mark tx descriptors to receive a DESC interrupt
1025          * when a tx queue gets deep; otherwise waiting for the
1026          * EOL to reap descriptors.  Note that this is done to
1027          * reduce interrupt load and this only defers reaping
1028          * descriptors, never transmitting frames.  Aside from
1029          * reducing interrupts this also permits more concurrency.
1030          * The only potential downside is if the tx queue backs
1031          * up in which case the top half of the kernel may backup
1032          * due to a lack of tx descriptors.
1033          *
1034          * The UAPSD queue is an exception, since we take a desc-
1035          * based intr on the EOSP frames.
1036          */
1037         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1038                 qi.tqi_qflags = TXQ_FLAG_TXOKINT_ENABLE |
1039                                 TXQ_FLAG_TXERRINT_ENABLE;
1040         } else {
1041                 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1042                         qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1043                 else
1044                         qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1045                                         TXQ_FLAG_TXDESCINT_ENABLE;
1046         }
1047         axq_qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1048         if (axq_qnum == -1) {
1049                 /*
1050                  * NB: don't print a message, this happens
1051                  * normally on parts with too few tx queues
1052                  */
1053                 return NULL;
1054         }
1055         if (axq_qnum >= ARRAY_SIZE(sc->tx.txq)) {
1056                 ath_err(common, "qnum %u out of range, max %zu!\n",
1057                         axq_qnum, ARRAY_SIZE(sc->tx.txq));
1058                 ath9k_hw_releasetxqueue(ah, axq_qnum);
1059                 return NULL;
1060         }
1061         if (!ATH_TXQ_SETUP(sc, axq_qnum)) {
1062                 struct ath_txq *txq = &sc->tx.txq[axq_qnum];
1063
1064                 txq->axq_qnum = axq_qnum;
1065                 txq->mac80211_qnum = -1;
1066                 txq->axq_link = NULL;
1067                 INIT_LIST_HEAD(&txq->axq_q);
1068                 INIT_LIST_HEAD(&txq->axq_acq);
1069                 spin_lock_init(&txq->axq_lock);
1070                 txq->axq_depth = 0;
1071                 txq->axq_ampdu_depth = 0;
1072                 txq->axq_tx_inprogress = false;
1073                 sc->tx.txqsetup |= 1<<axq_qnum;
1074
1075                 txq->txq_headidx = txq->txq_tailidx = 0;
1076                 for (i = 0; i < ATH_TXFIFO_DEPTH; i++)
1077                         INIT_LIST_HEAD(&txq->txq_fifo[i]);
1078         }
1079         return &sc->tx.txq[axq_qnum];
1080 }
1081
1082 int ath_txq_update(struct ath_softc *sc, int qnum,
1083                    struct ath9k_tx_queue_info *qinfo)
1084 {
1085         struct ath_hw *ah = sc->sc_ah;
1086         int error = 0;
1087         struct ath9k_tx_queue_info qi;
1088
1089         if (qnum == sc->beacon.beaconq) {
1090                 /*
1091                  * XXX: for beacon queue, we just save the parameter.
1092                  * It will be picked up by ath_beaconq_config when
1093                  * it's necessary.
1094                  */
1095                 sc->beacon.beacon_qi = *qinfo;
1096                 return 0;
1097         }
1098
1099         BUG_ON(sc->tx.txq[qnum].axq_qnum != qnum);
1100
1101         ath9k_hw_get_txq_props(ah, qnum, &qi);
1102         qi.tqi_aifs = qinfo->tqi_aifs;
1103         qi.tqi_cwmin = qinfo->tqi_cwmin;
1104         qi.tqi_cwmax = qinfo->tqi_cwmax;
1105         qi.tqi_burstTime = qinfo->tqi_burstTime;
1106         qi.tqi_readyTime = qinfo->tqi_readyTime;
1107
1108         if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
1109                 ath_err(ath9k_hw_common(sc->sc_ah),
1110                         "Unable to update hardware queue %u!\n", qnum);
1111                 error = -EIO;
1112         } else {
1113                 ath9k_hw_resettxqueue(ah, qnum);
1114         }
1115
1116         return error;
1117 }
1118
1119 int ath_cabq_update(struct ath_softc *sc)
1120 {
1121         struct ath9k_tx_queue_info qi;
1122         struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
1123         int qnum = sc->beacon.cabq->axq_qnum;
1124
1125         ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
1126         /*
1127          * Ensure the readytime % is within the bounds.
1128          */
1129         if (sc->config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
1130                 sc->config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
1131         else if (sc->config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
1132                 sc->config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
1133
1134         qi.tqi_readyTime = (cur_conf->beacon_interval *
1135                             sc->config.cabqReadytime) / 100;
1136         ath_txq_update(sc, qnum, &qi);
1137
1138         return 0;
1139 }
1140
1141 static bool bf_is_ampdu_not_probing(struct ath_buf *bf)
1142 {
1143     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(bf->bf_mpdu);
1144     return bf_isampdu(bf) && !(info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
1145 }
1146
1147 static void ath_drain_txq_list(struct ath_softc *sc, struct ath_txq *txq,
1148                                struct list_head *list, bool retry_tx)
1149 {
1150         struct ath_buf *bf, *lastbf;
1151         struct list_head bf_head;
1152         struct ath_tx_status ts;
1153
1154         memset(&ts, 0, sizeof(ts));
1155         INIT_LIST_HEAD(&bf_head);
1156
1157         while (!list_empty(list)) {
1158                 bf = list_first_entry(list, struct ath_buf, list);
1159
1160                 if (bf->bf_stale) {
1161                         list_del(&bf->list);
1162
1163                         ath_tx_return_buffer(sc, bf);
1164                         continue;
1165                 }
1166
1167                 lastbf = bf->bf_lastbf;
1168                 list_cut_position(&bf_head, list, &lastbf->list);
1169
1170                 txq->axq_depth--;
1171                 if (bf_is_ampdu_not_probing(bf))
1172                         txq->axq_ampdu_depth--;
1173
1174                 spin_unlock_bh(&txq->axq_lock);
1175                 if (bf_isampdu(bf))
1176                         ath_tx_complete_aggr(sc, txq, bf, &bf_head, &ts, 0,
1177                                              retry_tx);
1178                 else
1179                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 0);
1180                 spin_lock_bh(&txq->axq_lock);
1181         }
1182 }
1183
1184 /*
1185  * Drain a given TX queue (could be Beacon or Data)
1186  *
1187  * This assumes output has been stopped and
1188  * we do not need to block ath_tx_tasklet.
1189  */
1190 void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq, bool retry_tx)
1191 {
1192         spin_lock_bh(&txq->axq_lock);
1193         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1194                 int idx = txq->txq_tailidx;
1195
1196                 while (!list_empty(&txq->txq_fifo[idx])) {
1197                         ath_drain_txq_list(sc, txq, &txq->txq_fifo[idx],
1198                                            retry_tx);
1199
1200                         INCR(idx, ATH_TXFIFO_DEPTH);
1201                 }
1202                 txq->txq_tailidx = idx;
1203         }
1204
1205         txq->axq_link = NULL;
1206         txq->axq_tx_inprogress = false;
1207         ath_drain_txq_list(sc, txq, &txq->axq_q, retry_tx);
1208
1209         /* flush any pending frames if aggregation is enabled */
1210         if ((sc->sc_flags & SC_OP_TXAGGR) && !retry_tx)
1211                 ath_txq_drain_pending_buffers(sc, txq);
1212
1213         spin_unlock_bh(&txq->axq_lock);
1214 }
1215
1216 bool ath_drain_all_txq(struct ath_softc *sc, bool retry_tx)
1217 {
1218         struct ath_hw *ah = sc->sc_ah;
1219         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1220         struct ath_txq *txq;
1221         int i, npend = 0;
1222
1223         if (sc->sc_flags & SC_OP_INVALID)
1224                 return true;
1225
1226         ath9k_hw_abort_tx_dma(ah);
1227
1228         /* Check if any queue remains active */
1229         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1230                 if (!ATH_TXQ_SETUP(sc, i))
1231                         continue;
1232
1233                 npend += ath9k_hw_numtxpending(ah, sc->tx.txq[i].axq_qnum);
1234         }
1235
1236         if (npend)
1237                 ath_err(common, "Failed to stop TX DMA!\n");
1238
1239         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1240                 if (!ATH_TXQ_SETUP(sc, i))
1241                         continue;
1242
1243                 /*
1244                  * The caller will resume queues with ieee80211_wake_queues.
1245                  * Mark the queue as not stopped to prevent ath_tx_complete
1246                  * from waking the queue too early.
1247                  */
1248                 txq = &sc->tx.txq[i];
1249                 txq->stopped = false;
1250                 ath_draintxq(sc, txq, retry_tx);
1251         }
1252
1253         return !npend;
1254 }
1255
1256 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1257 {
1258         ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1259         sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1260 }
1261
1262 /* For each axq_acq entry, for each tid, try to schedule packets
1263  * for transmit until ampdu_depth has reached min Q depth.
1264  */
1265 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
1266 {
1267         struct ath_atx_ac *ac, *ac_tmp, *last_ac;
1268         struct ath_atx_tid *tid, *last_tid;
1269
1270         if (list_empty(&txq->axq_acq) ||
1271             txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1272                 return;
1273
1274         ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
1275         last_ac = list_entry(txq->axq_acq.prev, struct ath_atx_ac, list);
1276
1277         list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1278                 last_tid = list_entry(ac->tid_q.prev, struct ath_atx_tid, list);
1279                 list_del(&ac->list);
1280                 ac->sched = false;
1281
1282                 while (!list_empty(&ac->tid_q)) {
1283                         tid = list_first_entry(&ac->tid_q, struct ath_atx_tid,
1284                                                list);
1285                         list_del(&tid->list);
1286                         tid->sched = false;
1287
1288                         if (tid->paused)
1289                                 continue;
1290
1291                         ath_tx_sched_aggr(sc, txq, tid);
1292
1293                         /*
1294                          * add tid to round-robin queue if more frames
1295                          * are pending for the tid
1296                          */
1297                         if (!list_empty(&tid->buf_q))
1298                                 ath_tx_queue_tid(txq, tid);
1299
1300                         if (tid == last_tid ||
1301                             txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1302                                 break;
1303                 }
1304
1305                 if (!list_empty(&ac->tid_q)) {
1306                         if (!ac->sched) {
1307                                 ac->sched = true;
1308                                 list_add_tail(&ac->list, &txq->axq_acq);
1309                         }
1310                 }
1311
1312                 if (ac == last_ac ||
1313                     txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1314                         return;
1315         }
1316 }
1317
1318 /***********/
1319 /* TX, DMA */
1320 /***********/
1321
1322 /*
1323  * Insert a chain of ath_buf (descriptors) on a txq and
1324  * assume the descriptors are already chained together by caller.
1325  */
1326 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
1327                              struct list_head *head, bool internal)
1328 {
1329         struct ath_hw *ah = sc->sc_ah;
1330         struct ath_common *common = ath9k_hw_common(ah);
1331         struct ath_buf *bf, *bf_last;
1332         bool puttxbuf = false;
1333         bool edma;
1334
1335         /*
1336          * Insert the frame on the outbound list and
1337          * pass it on to the hardware.
1338          */
1339
1340         if (list_empty(head))
1341                 return;
1342
1343         edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1344         bf = list_first_entry(head, struct ath_buf, list);
1345         bf_last = list_entry(head->prev, struct ath_buf, list);
1346
1347         ath_dbg(common, ATH_DBG_QUEUE,
1348                 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
1349
1350         if (edma && list_empty(&txq->txq_fifo[txq->txq_headidx])) {
1351                 list_splice_tail_init(head, &txq->txq_fifo[txq->txq_headidx]);
1352                 INCR(txq->txq_headidx, ATH_TXFIFO_DEPTH);
1353                 puttxbuf = true;
1354         } else {
1355                 list_splice_tail_init(head, &txq->axq_q);
1356
1357                 if (txq->axq_link) {
1358                         ath9k_hw_set_desc_link(ah, txq->axq_link, bf->bf_daddr);
1359                         ath_dbg(common, ATH_DBG_XMIT,
1360                                 "link[%u] (%p)=%llx (%p)\n",
1361                                 txq->axq_qnum, txq->axq_link,
1362                                 ito64(bf->bf_daddr), bf->bf_desc);
1363                 } else if (!edma)
1364                         puttxbuf = true;
1365
1366                 txq->axq_link = bf_last->bf_desc;
1367         }
1368
1369         if (puttxbuf) {
1370                 TX_STAT_INC(txq->axq_qnum, puttxbuf);
1371                 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
1372                 ath_dbg(common, ATH_DBG_XMIT, "TXDP[%u] = %llx (%p)\n",
1373                         txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
1374         }
1375
1376         if (!edma) {
1377                 TX_STAT_INC(txq->axq_qnum, txstart);
1378                 ath9k_hw_txstart(ah, txq->axq_qnum);
1379         }
1380
1381         if (!internal) {
1382                 txq->axq_depth++;
1383                 if (bf_is_ampdu_not_probing(bf))
1384                         txq->axq_ampdu_depth++;
1385         }
1386 }
1387
1388 static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_atx_tid *tid,
1389                               struct ath_buf *bf, struct ath_tx_control *txctl)
1390 {
1391         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
1392         struct list_head bf_head;
1393
1394         bf->bf_state.bf_type |= BUF_AMPDU;
1395
1396         /*
1397          * Do not queue to h/w when any of the following conditions is true:
1398          * - there are pending frames in software queue
1399          * - the TID is currently paused for ADDBA/BAR request
1400          * - seqno is not within block-ack window
1401          * - h/w queue depth exceeds low water mark
1402          */
1403         if (!list_empty(&tid->buf_q) || tid->paused ||
1404             !BAW_WITHIN(tid->seq_start, tid->baw_size, fi->seqno) ||
1405             txctl->txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) {
1406                 /*
1407                  * Add this frame to software queue for scheduling later
1408                  * for aggregation.
1409                  */
1410                 TX_STAT_INC(txctl->txq->axq_qnum, a_queued_sw);
1411                 list_add_tail(&bf->list, &tid->buf_q);
1412                 ath_tx_queue_tid(txctl->txq, tid);
1413                 return;
1414         }
1415
1416         INIT_LIST_HEAD(&bf_head);
1417         list_add(&bf->list, &bf_head);
1418
1419         /* Add sub-frame to BAW */
1420         if (!fi->retries)
1421                 ath_tx_addto_baw(sc, tid, fi->seqno);
1422
1423         /* Queue to h/w without aggregation */
1424         TX_STAT_INC(txctl->txq->axq_qnum, a_queued_hw);
1425         bf->bf_lastbf = bf;
1426         ath_buf_set_rate(sc, bf, fi->framelen);
1427         ath_tx_txqaddbuf(sc, txctl->txq, &bf_head, false);
1428 }
1429
1430 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
1431                                struct ath_atx_tid *tid,
1432                                struct list_head *bf_head)
1433 {
1434         struct ath_frame_info *fi;
1435         struct ath_buf *bf;
1436
1437         bf = list_first_entry(bf_head, struct ath_buf, list);
1438         bf->bf_state.bf_type &= ~BUF_AMPDU;
1439
1440         /* update starting sequence number for subsequent ADDBA request */
1441         if (tid)
1442                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
1443
1444         bf->bf_lastbf = bf;
1445         fi = get_frame_info(bf->bf_mpdu);
1446         ath_buf_set_rate(sc, bf, fi->framelen);
1447         ath_tx_txqaddbuf(sc, txq, bf_head, false);
1448         TX_STAT_INC(txq->axq_qnum, queued);
1449 }
1450
1451 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
1452 {
1453         struct ieee80211_hdr *hdr;
1454         enum ath9k_pkt_type htype;
1455         __le16 fc;
1456
1457         hdr = (struct ieee80211_hdr *)skb->data;
1458         fc = hdr->frame_control;
1459
1460         if (ieee80211_is_beacon(fc))
1461                 htype = ATH9K_PKT_TYPE_BEACON;
1462         else if (ieee80211_is_probe_resp(fc))
1463                 htype = ATH9K_PKT_TYPE_PROBE_RESP;
1464         else if (ieee80211_is_atim(fc))
1465                 htype = ATH9K_PKT_TYPE_ATIM;
1466         else if (ieee80211_is_pspoll(fc))
1467                 htype = ATH9K_PKT_TYPE_PSPOLL;
1468         else
1469                 htype = ATH9K_PKT_TYPE_NORMAL;
1470
1471         return htype;
1472 }
1473
1474 static void setup_frame_info(struct ieee80211_hw *hw, struct sk_buff *skb,
1475                              int framelen)
1476 {
1477         struct ath_softc *sc = hw->priv;
1478         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1479         struct ieee80211_sta *sta = tx_info->control.sta;
1480         struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
1481         struct ieee80211_hdr *hdr;
1482         struct ath_frame_info *fi = get_frame_info(skb);
1483         struct ath_node *an = NULL;
1484         struct ath_atx_tid *tid;
1485         enum ath9k_key_type keytype;
1486         u16 seqno = 0;
1487         u8 tidno;
1488
1489         keytype = ath9k_cmn_get_hw_crypto_keytype(skb);
1490
1491         if (sta)
1492                 an = (struct ath_node *) sta->drv_priv;
1493
1494         hdr = (struct ieee80211_hdr *)skb->data;
1495         if (an && ieee80211_is_data_qos(hdr->frame_control) &&
1496                 conf_is_ht(&hw->conf) && (sc->sc_flags & SC_OP_TXAGGR)) {
1497
1498                 tidno = ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1499
1500                 /*
1501                  * Override seqno set by upper layer with the one
1502                  * in tx aggregation state.
1503                  */
1504                 tid = ATH_AN_2_TID(an, tidno);
1505                 seqno = tid->seq_next;
1506                 hdr->seq_ctrl = cpu_to_le16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1507                 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
1508         }
1509
1510         memset(fi, 0, sizeof(*fi));
1511         if (hw_key)
1512                 fi->keyix = hw_key->hw_key_idx;
1513         else if (an && ieee80211_is_data(hdr->frame_control) && an->ps_key > 0)
1514                 fi->keyix = an->ps_key;
1515         else
1516                 fi->keyix = ATH9K_TXKEYIX_INVALID;
1517         fi->keytype = keytype;
1518         fi->framelen = framelen;
1519         fi->seqno = seqno;
1520 }
1521
1522 static int setup_tx_flags(struct sk_buff *skb)
1523 {
1524         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1525         int flags = 0;
1526
1527         flags |= ATH9K_TXDESC_INTREQ;
1528
1529         if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
1530                 flags |= ATH9K_TXDESC_NOACK;
1531
1532         if (tx_info->flags & IEEE80211_TX_CTL_LDPC)
1533                 flags |= ATH9K_TXDESC_LDPC;
1534
1535         return flags;
1536 }
1537
1538 /*
1539  * rix - rate index
1540  * pktlen - total bytes (delims + data + fcs + pads + pad delims)
1541  * width  - 0 for 20 MHz, 1 for 40 MHz
1542  * half_gi - to use 4us v/s 3.6 us for symbol time
1543  */
1544 static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, int pktlen,
1545                             int width, int half_gi, bool shortPreamble)
1546 {
1547         u32 nbits, nsymbits, duration, nsymbols;
1548         int streams;
1549
1550         /* find number of symbols: PLCP + data */
1551         streams = HT_RC_2_STREAMS(rix);
1552         nbits = (pktlen << 3) + OFDM_PLCP_BITS;
1553         nsymbits = bits_per_symbol[rix % 8][width] * streams;
1554         nsymbols = (nbits + nsymbits - 1) / nsymbits;
1555
1556         if (!half_gi)
1557                 duration = SYMBOL_TIME(nsymbols);
1558         else
1559                 duration = SYMBOL_TIME_HALFGI(nsymbols);
1560
1561         /* addup duration for legacy/ht training and signal fields */
1562         duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
1563
1564         return duration;
1565 }
1566
1567 u8 ath_txchainmask_reduction(struct ath_softc *sc, u8 chainmask, u32 rate)
1568 {
1569         struct ath_hw *ah = sc->sc_ah;
1570         struct ath9k_channel *curchan = ah->curchan;
1571         if ((sc->sc_flags & SC_OP_ENABLE_APM) &&
1572                         (curchan->channelFlags & CHANNEL_5GHZ) &&
1573                         (chainmask == 0x7) && (rate < 0x90))
1574                 return 0x3;
1575         else
1576                 return chainmask;
1577 }
1578
1579 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf, int len)
1580 {
1581         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1582         struct ath9k_11n_rate_series series[4];
1583         struct sk_buff *skb;
1584         struct ieee80211_tx_info *tx_info;
1585         struct ieee80211_tx_rate *rates;
1586         const struct ieee80211_rate *rate;
1587         struct ieee80211_hdr *hdr;
1588         int i, flags = 0;
1589         u8 rix = 0, ctsrate = 0;
1590         bool is_pspoll;
1591
1592         memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
1593
1594         skb = bf->bf_mpdu;
1595         tx_info = IEEE80211_SKB_CB(skb);
1596         rates = tx_info->control.rates;
1597         hdr = (struct ieee80211_hdr *)skb->data;
1598         is_pspoll = ieee80211_is_pspoll(hdr->frame_control);
1599
1600         /*
1601          * We check if Short Preamble is needed for the CTS rate by
1602          * checking the BSS's global flag.
1603          * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
1604          */
1605         rate = ieee80211_get_rts_cts_rate(sc->hw, tx_info);
1606         ctsrate = rate->hw_value;
1607         if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
1608                 ctsrate |= rate->hw_value_short;
1609
1610         for (i = 0; i < 4; i++) {
1611                 bool is_40, is_sgi, is_sp;
1612                 int phy;
1613
1614                 if (!rates[i].count || (rates[i].idx < 0))
1615                         continue;
1616
1617                 rix = rates[i].idx;
1618                 series[i].Tries = rates[i].count;
1619
1620                     if (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
1621                         series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1622                         flags |= ATH9K_TXDESC_RTSENA;
1623                 } else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
1624                         series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1625                         flags |= ATH9K_TXDESC_CTSENA;
1626                 }
1627
1628                 if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1629                         series[i].RateFlags |= ATH9K_RATESERIES_2040;
1630                 if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
1631                         series[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
1632
1633                 is_sgi = !!(rates[i].flags & IEEE80211_TX_RC_SHORT_GI);
1634                 is_40 = !!(rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
1635                 is_sp = !!(rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
1636
1637                 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
1638                         /* MCS rates */
1639                         series[i].Rate = rix | 0x80;
1640                         series[i].ChSel = ath_txchainmask_reduction(sc,
1641                                         common->tx_chainmask, series[i].Rate);
1642                         series[i].PktDuration = ath_pkt_duration(sc, rix, len,
1643                                  is_40, is_sgi, is_sp);
1644                         if (rix < 8 && (tx_info->flags & IEEE80211_TX_CTL_STBC))
1645                                 series[i].RateFlags |= ATH9K_RATESERIES_STBC;
1646                         continue;
1647                 }
1648
1649                 /* legacy rates */
1650                 if ((tx_info->band == IEEE80211_BAND_2GHZ) &&
1651                     !(rate->flags & IEEE80211_RATE_ERP_G))
1652                         phy = WLAN_RC_PHY_CCK;
1653                 else
1654                         phy = WLAN_RC_PHY_OFDM;
1655
1656                 rate = &sc->sbands[tx_info->band].bitrates[rates[i].idx];
1657                 series[i].Rate = rate->hw_value;
1658                 if (rate->hw_value_short) {
1659                         if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1660                                 series[i].Rate |= rate->hw_value_short;
1661                 } else {
1662                         is_sp = false;
1663                 }
1664
1665                 if (bf->bf_state.bfs_paprd)
1666                         series[i].ChSel = common->tx_chainmask;
1667                 else
1668                         series[i].ChSel = ath_txchainmask_reduction(sc,
1669                                         common->tx_chainmask, series[i].Rate);
1670
1671                 series[i].PktDuration = ath9k_hw_computetxtime(sc->sc_ah,
1672                         phy, rate->bitrate * 100, len, rix, is_sp);
1673         }
1674
1675         /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
1676         if (bf_isaggr(bf) && (len > sc->sc_ah->caps.rts_aggr_limit))
1677                 flags &= ~ATH9K_TXDESC_RTSENA;
1678
1679         /* ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive. */
1680         if (flags & ATH9K_TXDESC_RTSENA)
1681                 flags &= ~ATH9K_TXDESC_CTSENA;
1682
1683         /* set dur_update_en for l-sig computation except for PS-Poll frames */
1684         ath9k_hw_set11n_ratescenario(sc->sc_ah, bf->bf_desc,
1685                                      bf->bf_lastbf->bf_desc,
1686                                      !is_pspoll, ctsrate,
1687                                      0, series, 4, flags);
1688
1689 }
1690
1691 static struct ath_buf *ath_tx_setup_buffer(struct ieee80211_hw *hw,
1692                                            struct ath_txq *txq,
1693                                            struct sk_buff *skb)
1694 {
1695         struct ath_softc *sc = hw->priv;
1696         struct ath_hw *ah = sc->sc_ah;
1697         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1698         struct ath_frame_info *fi = get_frame_info(skb);
1699         struct ath_buf *bf;
1700         struct ath_desc *ds;
1701         int frm_type;
1702
1703         bf = ath_tx_get_buffer(sc);
1704         if (!bf) {
1705                 ath_dbg(common, ATH_DBG_XMIT, "TX buffers are full\n");
1706                 return NULL;
1707         }
1708
1709         ATH_TXBUF_RESET(bf);
1710
1711         bf->bf_flags = setup_tx_flags(skb);
1712         bf->bf_mpdu = skb;
1713
1714         bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
1715                                          skb->len, DMA_TO_DEVICE);
1716         if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
1717                 bf->bf_mpdu = NULL;
1718                 bf->bf_buf_addr = 0;
1719                 ath_err(ath9k_hw_common(sc->sc_ah),
1720                         "dma_mapping_error() on TX\n");
1721                 ath_tx_return_buffer(sc, bf);
1722                 return NULL;
1723         }
1724
1725         frm_type = get_hw_packet_type(skb);
1726
1727         ds = bf->bf_desc;
1728         ath9k_hw_set_desc_link(ah, ds, 0);
1729
1730         ath9k_hw_set11n_txdesc(ah, ds, fi->framelen, frm_type, MAX_RATE_POWER,
1731                                fi->keyix, fi->keytype, bf->bf_flags);
1732
1733         ath9k_hw_filltxdesc(ah, ds,
1734                             skb->len,   /* segment length */
1735                             true,       /* first segment */
1736                             true,       /* last segment */
1737                             ds,         /* first descriptor */
1738                             bf->bf_buf_addr,
1739                             txq->axq_qnum);
1740
1741
1742         return bf;
1743 }
1744
1745 /* FIXME: tx power */
1746 static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
1747                              struct ath_tx_control *txctl)
1748 {
1749         struct sk_buff *skb = bf->bf_mpdu;
1750         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1751         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1752         struct list_head bf_head;
1753         struct ath_atx_tid *tid = NULL;
1754         u8 tidno;
1755
1756         spin_lock_bh(&txctl->txq->axq_lock);
1757         if ((sc->sc_flags & SC_OP_TXAGGR) && txctl->an &&
1758                 ieee80211_is_data_qos(hdr->frame_control)) {
1759                 tidno = ieee80211_get_qos_ctl(hdr)[0] &
1760                         IEEE80211_QOS_CTL_TID_MASK;
1761                 tid = ATH_AN_2_TID(txctl->an, tidno);
1762
1763                 WARN_ON(tid->ac->txq != txctl->txq);
1764         }
1765
1766         if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
1767                 /*
1768                  * Try aggregation if it's a unicast data frame
1769                  * and the destination is HT capable.
1770                  */
1771                 ath_tx_send_ampdu(sc, tid, bf, txctl);
1772         } else {
1773                 INIT_LIST_HEAD(&bf_head);
1774                 list_add_tail(&bf->list, &bf_head);
1775
1776                 bf->bf_state.bfs_ftype = txctl->frame_type;
1777                 bf->bf_state.bfs_paprd = txctl->paprd;
1778
1779                 if (bf->bf_state.bfs_paprd)
1780                         ar9003_hw_set_paprd_txdesc(sc->sc_ah, bf->bf_desc,
1781                                                    bf->bf_state.bfs_paprd);
1782
1783                 if (txctl->paprd)
1784                         bf->bf_state.bfs_paprd_timestamp = jiffies;
1785
1786                 if (tx_info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
1787                         ath9k_hw_set_clrdmask(sc->sc_ah, bf->bf_desc, true);
1788
1789                 ath_tx_send_normal(sc, txctl->txq, tid, &bf_head);
1790         }
1791
1792         spin_unlock_bh(&txctl->txq->axq_lock);
1793 }
1794
1795 /* Upon failure caller should free skb */
1796 int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
1797                  struct ath_tx_control *txctl)
1798 {
1799         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1800         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1801         struct ieee80211_sta *sta = info->control.sta;
1802         struct ieee80211_vif *vif = info->control.vif;
1803         struct ath_softc *sc = hw->priv;
1804         struct ath_txq *txq = txctl->txq;
1805         struct ath_buf *bf;
1806         int padpos, padsize;
1807         int frmlen = skb->len + FCS_LEN;
1808         int q;
1809
1810         /* NOTE:  sta can be NULL according to net/mac80211.h */
1811         if (sta)
1812                 txctl->an = (struct ath_node *)sta->drv_priv;
1813
1814         if (info->control.hw_key)
1815                 frmlen += info->control.hw_key->icv_len;
1816
1817         /*
1818          * As a temporary workaround, assign seq# here; this will likely need
1819          * to be cleaned up to work better with Beacon transmission and virtual
1820          * BSSes.
1821          */
1822         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1823                 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1824                         sc->tx.seq_no += 0x10;
1825                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1826                 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
1827         }
1828
1829         /* Add the padding after the header if this is not already done */
1830         padpos = ath9k_cmn_padpos(hdr->frame_control);
1831         padsize = padpos & 3;
1832         if (padsize && skb->len > padpos) {
1833                 if (skb_headroom(skb) < padsize)
1834                         return -ENOMEM;
1835
1836                 skb_push(skb, padsize);
1837                 memmove(skb->data, skb->data + padsize, padpos);
1838         }
1839
1840         if ((vif && vif->type != NL80211_IFTYPE_AP &&
1841                     vif->type != NL80211_IFTYPE_AP_VLAN) ||
1842             !ieee80211_is_data(hdr->frame_control))
1843                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1844
1845         setup_frame_info(hw, skb, frmlen);
1846
1847         /*
1848          * At this point, the vif, hw_key and sta pointers in the tx control
1849          * info are no longer valid (overwritten by the ath_frame_info data.
1850          */
1851
1852         bf = ath_tx_setup_buffer(hw, txctl->txq, skb);
1853         if (unlikely(!bf))
1854                 return -ENOMEM;
1855
1856         q = skb_get_queue_mapping(skb);
1857         spin_lock_bh(&txq->axq_lock);
1858         if (txq == sc->tx.txq_map[q] &&
1859             ++txq->pending_frames > ATH_MAX_QDEPTH && !txq->stopped) {
1860                 ieee80211_stop_queue(sc->hw, q);
1861                 txq->stopped = 1;
1862         }
1863         spin_unlock_bh(&txq->axq_lock);
1864
1865         ath_tx_start_dma(sc, bf, txctl);
1866
1867         return 0;
1868 }
1869
1870 /*****************/
1871 /* TX Completion */
1872 /*****************/
1873
1874 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
1875                             int tx_flags, int ftype, struct ath_txq *txq)
1876 {
1877         struct ieee80211_hw *hw = sc->hw;
1878         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1879         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1880         struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1881         int q, padpos, padsize;
1882
1883         ath_dbg(common, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
1884
1885         if (tx_flags & ATH_TX_BAR)
1886                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1887
1888         if (!(tx_flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
1889                 /* Frame was ACKed */
1890                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
1891         }
1892
1893         padpos = ath9k_cmn_padpos(hdr->frame_control);
1894         padsize = padpos & 3;
1895         if (padsize && skb->len>padpos+padsize) {
1896                 /*
1897                  * Remove MAC header padding before giving the frame back to
1898                  * mac80211.
1899                  */
1900                 memmove(skb->data + padsize, skb->data, padpos);
1901                 skb_pull(skb, padsize);
1902         }
1903
1904         if (sc->ps_flags & PS_WAIT_FOR_TX_ACK) {
1905                 sc->ps_flags &= ~PS_WAIT_FOR_TX_ACK;
1906                 ath_dbg(common, ATH_DBG_PS,
1907                         "Going back to sleep after having received TX status (0x%lx)\n",
1908                         sc->ps_flags & (PS_WAIT_FOR_BEACON |
1909                                         PS_WAIT_FOR_CAB |
1910                                         PS_WAIT_FOR_PSPOLL_DATA |
1911                                         PS_WAIT_FOR_TX_ACK));
1912         }
1913
1914         q = skb_get_queue_mapping(skb);
1915         if (txq == sc->tx.txq_map[q]) {
1916                 spin_lock_bh(&txq->axq_lock);
1917                 if (WARN_ON(--txq->pending_frames < 0))
1918                         txq->pending_frames = 0;
1919
1920                 if (txq->stopped && txq->pending_frames < ATH_MAX_QDEPTH) {
1921                         ieee80211_wake_queue(sc->hw, q);
1922                         txq->stopped = 0;
1923                 }
1924                 spin_unlock_bh(&txq->axq_lock);
1925         }
1926
1927         ieee80211_tx_status(hw, skb);
1928 }
1929
1930 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
1931                                 struct ath_txq *txq, struct list_head *bf_q,
1932                                 struct ath_tx_status *ts, int txok, int sendbar)
1933 {
1934         struct sk_buff *skb = bf->bf_mpdu;
1935         unsigned long flags;
1936         int tx_flags = 0;
1937
1938         if (sendbar)
1939                 tx_flags = ATH_TX_BAR;
1940
1941         if (!txok) {
1942                 tx_flags |= ATH_TX_ERROR;
1943
1944                 if (bf_isxretried(bf))
1945                         tx_flags |= ATH_TX_XRETRY;
1946         }
1947
1948         dma_unmap_single(sc->dev, bf->bf_buf_addr, skb->len, DMA_TO_DEVICE);
1949         bf->bf_buf_addr = 0;
1950
1951         if (bf->bf_state.bfs_paprd) {
1952                 if (time_after(jiffies,
1953                                 bf->bf_state.bfs_paprd_timestamp +
1954                                 msecs_to_jiffies(ATH_PAPRD_TIMEOUT)))
1955                         dev_kfree_skb_any(skb);
1956                 else
1957                         complete(&sc->paprd_complete);
1958         } else {
1959                 ath_debug_stat_tx(sc, bf, ts, txq);
1960                 ath_tx_complete(sc, skb, tx_flags,
1961                                 bf->bf_state.bfs_ftype, txq);
1962         }
1963         /* At this point, skb (bf->bf_mpdu) is consumed...make sure we don't
1964          * accidentally reference it later.
1965          */
1966         bf->bf_mpdu = NULL;
1967
1968         /*
1969          * Return the list of ath_buf of this mpdu to free queue
1970          */
1971         spin_lock_irqsave(&sc->tx.txbuflock, flags);
1972         list_splice_tail_init(bf_q, &sc->tx.txbuf);
1973         spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
1974 }
1975
1976 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
1977                              struct ath_tx_status *ts, int nframes, int nbad,
1978                              int txok, bool update_rc)
1979 {
1980         struct sk_buff *skb = bf->bf_mpdu;
1981         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1982         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1983         struct ieee80211_hw *hw = sc->hw;
1984         struct ath_hw *ah = sc->sc_ah;
1985         u8 i, tx_rateindex;
1986
1987         if (txok)
1988                 tx_info->status.ack_signal = ts->ts_rssi;
1989
1990         tx_rateindex = ts->ts_rateindex;
1991         WARN_ON(tx_rateindex >= hw->max_rates);
1992
1993         if (ts->ts_status & ATH9K_TXERR_FILT)
1994                 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1995         if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && update_rc) {
1996                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
1997
1998                 BUG_ON(nbad > nframes);
1999
2000                 tx_info->status.ampdu_len = nframes;
2001                 tx_info->status.ampdu_ack_len = nframes - nbad;
2002         }
2003
2004         if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
2005             (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0 && update_rc) {
2006                 /*
2007                  * If an underrun error is seen assume it as an excessive
2008                  * retry only if max frame trigger level has been reached
2009                  * (2 KB for single stream, and 4 KB for dual stream).
2010                  * Adjust the long retry as if the frame was tried
2011                  * hw->max_rate_tries times to affect how rate control updates
2012                  * PER for the failed rate.
2013                  * In case of congestion on the bus penalizing this type of
2014                  * underruns should help hardware actually transmit new frames
2015                  * successfully by eventually preferring slower rates.
2016                  * This itself should also alleviate congestion on the bus.
2017                  */
2018                 if (ieee80211_is_data(hdr->frame_control) &&
2019                     (ts->ts_flags & (ATH9K_TX_DATA_UNDERRUN |
2020                                      ATH9K_TX_DELIM_UNDERRUN)) &&
2021                     ah->tx_trig_level >= sc->sc_ah->config.max_txtrig_level)
2022                         tx_info->status.rates[tx_rateindex].count =
2023                                 hw->max_rate_tries;
2024         }
2025
2026         for (i = tx_rateindex + 1; i < hw->max_rates; i++) {
2027                 tx_info->status.rates[i].count = 0;
2028                 tx_info->status.rates[i].idx = -1;
2029         }
2030
2031         tx_info->status.rates[tx_rateindex].count = ts->ts_longretry + 1;
2032 }
2033
2034 static void ath_tx_process_buffer(struct ath_softc *sc, struct ath_txq *txq,
2035                                   struct ath_tx_status *ts, struct ath_buf *bf,
2036                                   struct list_head *bf_head)
2037 {
2038         int txok;
2039
2040         txq->axq_depth--;
2041         txok = !(ts->ts_status & ATH9K_TXERR_MASK);
2042         txq->axq_tx_inprogress = false;
2043         if (bf_is_ampdu_not_probing(bf))
2044                 txq->axq_ampdu_depth--;
2045
2046         spin_unlock_bh(&txq->axq_lock);
2047
2048         if (!bf_isampdu(bf)) {
2049                 /*
2050                  * This frame is sent out as a single frame.
2051                  * Use hardware retry status for this frame.
2052                  */
2053                 if (ts->ts_status & ATH9K_TXERR_XRETRY)
2054                         bf->bf_state.bf_type |= BUF_XRETRY;
2055                 ath_tx_rc_status(sc, bf, ts, 1, txok ? 0 : 1, txok, true);
2056                 ath_tx_complete_buf(sc, bf, txq, bf_head, ts, txok, 0);
2057         } else
2058                 ath_tx_complete_aggr(sc, txq, bf, bf_head, ts, txok, true);
2059
2060         spin_lock_bh(&txq->axq_lock);
2061
2062         if (sc->sc_flags & SC_OP_TXAGGR)
2063                 ath_txq_schedule(sc, txq);
2064 }
2065
2066 static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
2067 {
2068         struct ath_hw *ah = sc->sc_ah;
2069         struct ath_common *common = ath9k_hw_common(ah);
2070         struct ath_buf *bf, *lastbf, *bf_held = NULL;
2071         struct list_head bf_head;
2072         struct ath_desc *ds;
2073         struct ath_tx_status ts;
2074         int status;
2075
2076         ath_dbg(common, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
2077                 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
2078                 txq->axq_link);
2079
2080         spin_lock_bh(&txq->axq_lock);
2081         for (;;) {
2082                 if (list_empty(&txq->axq_q)) {
2083                         txq->axq_link = NULL;
2084                         if (sc->sc_flags & SC_OP_TXAGGR)
2085                                 ath_txq_schedule(sc, txq);
2086                         break;
2087                 }
2088                 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2089
2090                 /*
2091                  * There is a race condition that a BH gets scheduled
2092                  * after sw writes TxE and before hw re-load the last
2093                  * descriptor to get the newly chained one.
2094                  * Software must keep the last DONE descriptor as a
2095                  * holding descriptor - software does so by marking
2096                  * it with the STALE flag.
2097                  */
2098                 bf_held = NULL;
2099                 if (bf->bf_stale) {
2100                         bf_held = bf;
2101                         if (list_is_last(&bf_held->list, &txq->axq_q))
2102                                 break;
2103
2104                         bf = list_entry(bf_held->list.next, struct ath_buf,
2105                                         list);
2106                 }
2107
2108                 lastbf = bf->bf_lastbf;
2109                 ds = lastbf->bf_desc;
2110
2111                 memset(&ts, 0, sizeof(ts));
2112                 status = ath9k_hw_txprocdesc(ah, ds, &ts);
2113                 if (status == -EINPROGRESS)
2114                         break;
2115
2116                 TX_STAT_INC(txq->axq_qnum, txprocdesc);
2117
2118                 /*
2119                  * Remove ath_buf's of the same transmit unit from txq,
2120                  * however leave the last descriptor back as the holding
2121                  * descriptor for hw.
2122                  */
2123                 lastbf->bf_stale = true;
2124                 INIT_LIST_HEAD(&bf_head);
2125                 if (!list_is_singular(&lastbf->list))
2126                         list_cut_position(&bf_head,
2127                                 &txq->axq_q, lastbf->list.prev);
2128
2129                 if (bf_held) {
2130                         list_del(&bf_held->list);
2131                         ath_tx_return_buffer(sc, bf_held);
2132                 }
2133
2134                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2135         }
2136         spin_unlock_bh(&txq->axq_lock);
2137 }
2138
2139 static void ath_tx_complete_poll_work(struct work_struct *work)
2140 {
2141         struct ath_softc *sc = container_of(work, struct ath_softc,
2142                         tx_complete_work.work);
2143         struct ath_txq *txq;
2144         int i;
2145         bool needreset = false;
2146 #ifdef CONFIG_ATH9K_DEBUGFS
2147         sc->tx_complete_poll_work_seen++;
2148 #endif
2149
2150         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
2151                 if (ATH_TXQ_SETUP(sc, i)) {
2152                         txq = &sc->tx.txq[i];
2153                         spin_lock_bh(&txq->axq_lock);
2154                         if (txq->axq_depth) {
2155                                 if (txq->axq_tx_inprogress) {
2156                                         needreset = true;
2157                                         spin_unlock_bh(&txq->axq_lock);
2158                                         break;
2159                                 } else {
2160                                         txq->axq_tx_inprogress = true;
2161                                 }
2162                         }
2163                         spin_unlock_bh(&txq->axq_lock);
2164                 }
2165
2166         if (needreset) {
2167                 ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_RESET,
2168                         "tx hung, resetting the chip\n");
2169                 spin_lock_bh(&sc->sc_pcu_lock);
2170                 ath_reset(sc, true);
2171                 spin_unlock_bh(&sc->sc_pcu_lock);
2172         }
2173
2174         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2175                         msecs_to_jiffies(ATH_TX_COMPLETE_POLL_INT));
2176 }
2177
2178
2179
2180 void ath_tx_tasklet(struct ath_softc *sc)
2181 {
2182         int i;
2183         u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2184
2185         ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2186
2187         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2188                 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2189                         ath_tx_processq(sc, &sc->tx.txq[i]);
2190         }
2191 }
2192
2193 void ath_tx_edma_tasklet(struct ath_softc *sc)
2194 {
2195         struct ath_tx_status ts;
2196         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2197         struct ath_hw *ah = sc->sc_ah;
2198         struct ath_txq *txq;
2199         struct ath_buf *bf, *lastbf;
2200         struct list_head bf_head;
2201         int status;
2202
2203         for (;;) {
2204                 status = ath9k_hw_txprocdesc(ah, NULL, (void *)&ts);
2205                 if (status == -EINPROGRESS)
2206                         break;
2207                 if (status == -EIO) {
2208                         ath_dbg(common, ATH_DBG_XMIT,
2209                                 "Error processing tx status\n");
2210                         break;
2211                 }
2212
2213                 /* Skip beacon completions */
2214                 if (ts.qid == sc->beacon.beaconq)
2215                         continue;
2216
2217                 txq = &sc->tx.txq[ts.qid];
2218
2219                 spin_lock_bh(&txq->axq_lock);
2220
2221                 if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
2222                         spin_unlock_bh(&txq->axq_lock);
2223                         return;
2224                 }
2225
2226                 bf = list_first_entry(&txq->txq_fifo[txq->txq_tailidx],
2227                                       struct ath_buf, list);
2228                 lastbf = bf->bf_lastbf;
2229
2230                 INIT_LIST_HEAD(&bf_head);
2231                 list_cut_position(&bf_head, &txq->txq_fifo[txq->txq_tailidx],
2232                                   &lastbf->list);
2233
2234                 if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
2235                         INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
2236
2237                         if (!list_empty(&txq->axq_q)) {
2238                                 struct list_head bf_q;
2239
2240                                 INIT_LIST_HEAD(&bf_q);
2241                                 txq->axq_link = NULL;
2242                                 list_splice_tail_init(&txq->axq_q, &bf_q);
2243                                 ath_tx_txqaddbuf(sc, txq, &bf_q, true);
2244                         }
2245                 }
2246
2247                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2248                 spin_unlock_bh(&txq->axq_lock);
2249         }
2250 }
2251
2252 /*****************/
2253 /* Init, Cleanup */
2254 /*****************/
2255
2256 static int ath_txstatus_setup(struct ath_softc *sc, int size)
2257 {
2258         struct ath_descdma *dd = &sc->txsdma;
2259         u8 txs_len = sc->sc_ah->caps.txs_len;
2260
2261         dd->dd_desc_len = size * txs_len;
2262         dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
2263                                          &dd->dd_desc_paddr, GFP_KERNEL);
2264         if (!dd->dd_desc)
2265                 return -ENOMEM;
2266
2267         return 0;
2268 }
2269
2270 static int ath_tx_edma_init(struct ath_softc *sc)
2271 {
2272         int err;
2273
2274         err = ath_txstatus_setup(sc, ATH_TXSTATUS_RING_SIZE);
2275         if (!err)
2276                 ath9k_hw_setup_statusring(sc->sc_ah, sc->txsdma.dd_desc,
2277                                           sc->txsdma.dd_desc_paddr,
2278                                           ATH_TXSTATUS_RING_SIZE);
2279
2280         return err;
2281 }
2282
2283 static void ath_tx_edma_cleanup(struct ath_softc *sc)
2284 {
2285         struct ath_descdma *dd = &sc->txsdma;
2286
2287         dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
2288                           dd->dd_desc_paddr);
2289 }
2290
2291 int ath_tx_init(struct ath_softc *sc, int nbufs)
2292 {
2293         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2294         int error = 0;
2295
2296         spin_lock_init(&sc->tx.txbuflock);
2297
2298         error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
2299                                   "tx", nbufs, 1, 1);
2300         if (error != 0) {
2301                 ath_err(common,
2302                         "Failed to allocate tx descriptors: %d\n", error);
2303                 goto err;
2304         }
2305
2306         error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
2307                                   "beacon", ATH_BCBUF, 1, 1);
2308         if (error != 0) {
2309                 ath_err(common,
2310                         "Failed to allocate beacon descriptors: %d\n", error);
2311                 goto err;
2312         }
2313
2314         INIT_DELAYED_WORK(&sc->tx_complete_work, ath_tx_complete_poll_work);
2315
2316         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
2317                 error = ath_tx_edma_init(sc);
2318                 if (error)
2319                         goto err;
2320         }
2321
2322 err:
2323         if (error != 0)
2324                 ath_tx_cleanup(sc);
2325
2326         return error;
2327 }
2328
2329 void ath_tx_cleanup(struct ath_softc *sc)
2330 {
2331         if (sc->beacon.bdma.dd_desc_len != 0)
2332                 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
2333
2334         if (sc->tx.txdma.dd_desc_len != 0)
2335                 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
2336
2337         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
2338                 ath_tx_edma_cleanup(sc);
2339 }
2340
2341 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2342 {
2343         struct ath_atx_tid *tid;
2344         struct ath_atx_ac *ac;
2345         int tidno, acno;
2346
2347         for (tidno = 0, tid = &an->tid[tidno];
2348              tidno < WME_NUM_TID;
2349              tidno++, tid++) {
2350                 tid->an        = an;
2351                 tid->tidno     = tidno;
2352                 tid->seq_start = tid->seq_next = 0;
2353                 tid->baw_size  = WME_MAX_BA;
2354                 tid->baw_head  = tid->baw_tail = 0;
2355                 tid->sched     = false;
2356                 tid->paused    = false;
2357                 tid->state &= ~AGGR_CLEANUP;
2358                 INIT_LIST_HEAD(&tid->buf_q);
2359                 acno = TID_TO_WME_AC(tidno);
2360                 tid->ac = &an->ac[acno];
2361                 tid->state &= ~AGGR_ADDBA_COMPLETE;
2362                 tid->state &= ~AGGR_ADDBA_PROGRESS;
2363         }
2364
2365         for (acno = 0, ac = &an->ac[acno];
2366              acno < WME_NUM_AC; acno++, ac++) {
2367                 ac->sched    = false;
2368                 ac->txq = sc->tx.txq_map[acno];
2369                 INIT_LIST_HEAD(&ac->tid_q);
2370         }
2371 }
2372
2373 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2374 {
2375         struct ath_atx_ac *ac;
2376         struct ath_atx_tid *tid;
2377         struct ath_txq *txq;
2378         int tidno;
2379
2380         for (tidno = 0, tid = &an->tid[tidno];
2381              tidno < WME_NUM_TID; tidno++, tid++) {
2382
2383                 ac = tid->ac;
2384                 txq = ac->txq;
2385
2386                 spin_lock_bh(&txq->axq_lock);
2387
2388                 if (tid->sched) {
2389                         list_del(&tid->list);
2390                         tid->sched = false;
2391                 }
2392
2393                 if (ac->sched) {
2394                         list_del(&ac->list);
2395                         tid->ac->sched = false;
2396                 }
2397
2398                 ath_tid_drain(sc, txq, tid);
2399                 tid->state &= ~AGGR_ADDBA_COMPLETE;
2400                 tid->state &= ~AGGR_CLEANUP;
2401
2402                 spin_unlock_bh(&txq->axq_lock);
2403         }
2404 }