]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/ath/ath9k/xmit.c
5aaed39459d24fd21678ce61497d1aa774989ba1
[linux.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 <linux/dma-mapping.h>
18 #include "ath9k.h"
19 #include "ar9003_mac.h"
20
21 #define BITS_PER_BYTE           8
22 #define OFDM_PLCP_BITS          22
23 #define HT_RC_2_STREAMS(_rc)    ((((_rc) & 0x78) >> 3) + 1)
24 #define L_STF                   8
25 #define L_LTF                   8
26 #define L_SIG                   4
27 #define HT_SIG                  8
28 #define HT_STF                  4
29 #define HT_LTF(_ns)             (4 * (_ns))
30 #define SYMBOL_TIME(_ns)        ((_ns) << 2) /* ns * 4 us */
31 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)  /* ns * 3.6 us */
32 #define TIME_SYMBOLS(t)         ((t) >> 2)
33 #define TIME_SYMBOLS_HALFGI(t)  (((t) * 5 - 4) / 18)
34 #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
35 #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
36
37
38 static u16 bits_per_symbol[][2] = {
39         /* 20MHz 40MHz */
40         {    26,   54 },     /*  0: BPSK */
41         {    52,  108 },     /*  1: QPSK 1/2 */
42         {    78,  162 },     /*  2: QPSK 3/4 */
43         {   104,  216 },     /*  3: 16-QAM 1/2 */
44         {   156,  324 },     /*  4: 16-QAM 3/4 */
45         {   208,  432 },     /*  5: 64-QAM 2/3 */
46         {   234,  486 },     /*  6: 64-QAM 3/4 */
47         {   260,  540 },     /*  7: 64-QAM 5/6 */
48 };
49
50 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
51                                struct ath_atx_tid *tid, struct sk_buff *skb);
52 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
53                             int tx_flags, struct ath_txq *txq);
54 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
55                                 struct ath_txq *txq, struct list_head *bf_q,
56                                 struct ath_tx_status *ts, int txok);
57 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
58                              struct list_head *head, bool internal);
59 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
60                              struct ath_tx_status *ts, int nframes, int nbad,
61                              int txok);
62 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
63                               int seqno);
64 static struct ath_buf *ath_tx_setup_buffer(struct ath_softc *sc,
65                                            struct ath_txq *txq,
66                                            struct ath_atx_tid *tid,
67                                            struct sk_buff *skb);
68
69 enum {
70         MCS_HT20,
71         MCS_HT20_SGI,
72         MCS_HT40,
73         MCS_HT40_SGI,
74 };
75
76 /*********************/
77 /* Aggregation logic */
78 /*********************/
79
80 void ath_txq_lock(struct ath_softc *sc, struct ath_txq *txq)
81         __acquires(&txq->axq_lock)
82 {
83         spin_lock_bh(&txq->axq_lock);
84 }
85
86 void ath_txq_unlock(struct ath_softc *sc, struct ath_txq *txq)
87         __releases(&txq->axq_lock)
88 {
89         spin_unlock_bh(&txq->axq_lock);
90 }
91
92 void ath_txq_unlock_complete(struct ath_softc *sc, struct ath_txq *txq)
93         __releases(&txq->axq_lock)
94 {
95         struct sk_buff_head q;
96         struct sk_buff *skb;
97
98         __skb_queue_head_init(&q);
99         skb_queue_splice_init(&txq->complete_q, &q);
100         spin_unlock_bh(&txq->axq_lock);
101
102         while ((skb = __skb_dequeue(&q)))
103                 ieee80211_tx_status(sc->hw, skb);
104 }
105
106 static void ath_tx_queue_tid(struct ath_softc *sc, struct ath_txq *txq,
107                              struct ath_atx_tid *tid)
108 {
109         struct ath_atx_ac *ac = tid->ac;
110         struct list_head *list;
111         struct ath_vif *avp = (struct ath_vif *) tid->an->vif->drv_priv;
112         struct ath_chanctx *ctx = avp->chanctx;
113
114         if (!ctx)
115                 return;
116
117         if (tid->sched)
118                 return;
119
120         tid->sched = true;
121         list_add_tail(&tid->list, &ac->tid_q);
122
123         if (ac->sched)
124                 return;
125
126         ac->sched = true;
127
128         list = &ctx->acq[TID_TO_WME_AC(tid->tidno)];
129         list_add_tail(&ac->list, list);
130 }
131
132 static struct ath_frame_info *get_frame_info(struct sk_buff *skb)
133 {
134         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
135         BUILD_BUG_ON(sizeof(struct ath_frame_info) >
136                      sizeof(tx_info->rate_driver_data));
137         return (struct ath_frame_info *) &tx_info->rate_driver_data[0];
138 }
139
140 static void ath_send_bar(struct ath_atx_tid *tid, u16 seqno)
141 {
142         if (!tid->an->sta)
143                 return;
144
145         ieee80211_send_bar(tid->an->vif, tid->an->sta->addr, tid->tidno,
146                            seqno << IEEE80211_SEQ_SEQ_SHIFT);
147 }
148
149 static void ath_set_rates(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
150                           struct ath_buf *bf)
151 {
152         ieee80211_get_tx_rates(vif, sta, bf->bf_mpdu, bf->rates,
153                                ARRAY_SIZE(bf->rates));
154 }
155
156 static void ath_txq_skb_done(struct ath_softc *sc, struct ath_txq *txq,
157                              struct sk_buff *skb)
158 {
159         int q;
160
161         q = skb_get_queue_mapping(skb);
162         if (txq == sc->tx.uapsdq)
163                 txq = sc->tx.txq_map[q];
164
165         if (txq != sc->tx.txq_map[q])
166                 return;
167
168         if (WARN_ON(--txq->pending_frames < 0))
169                 txq->pending_frames = 0;
170
171         if (txq->stopped &&
172             txq->pending_frames < sc->tx.txq_max_pending[q]) {
173                 ieee80211_wake_queue(sc->hw, q);
174                 txq->stopped = false;
175         }
176 }
177
178 static struct ath_atx_tid *
179 ath_get_skb_tid(struct ath_softc *sc, struct ath_node *an, struct sk_buff *skb)
180 {
181         u8 tidno = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
182         return ATH_AN_2_TID(an, tidno);
183 }
184
185 static bool ath_tid_has_buffered(struct ath_atx_tid *tid)
186 {
187         return !skb_queue_empty(&tid->buf_q) || !skb_queue_empty(&tid->retry_q);
188 }
189
190 static struct sk_buff *ath_tid_dequeue(struct ath_atx_tid *tid)
191 {
192         struct sk_buff *skb;
193
194         skb = __skb_dequeue(&tid->retry_q);
195         if (!skb)
196                 skb = __skb_dequeue(&tid->buf_q);
197
198         return skb;
199 }
200
201 /*
202  * ath_tx_tid_change_state:
203  * - clears a-mpdu flag of previous session
204  * - force sequence number allocation to fix next BlockAck Window
205  */
206 static void
207 ath_tx_tid_change_state(struct ath_softc *sc, struct ath_atx_tid *tid)
208 {
209         struct ath_txq *txq = tid->ac->txq;
210         struct ieee80211_tx_info *tx_info;
211         struct sk_buff *skb, *tskb;
212         struct ath_buf *bf;
213         struct ath_frame_info *fi;
214
215         skb_queue_walk_safe(&tid->buf_q, skb, tskb) {
216                 fi = get_frame_info(skb);
217                 bf = fi->bf;
218
219                 tx_info = IEEE80211_SKB_CB(skb);
220                 tx_info->flags &= ~IEEE80211_TX_CTL_AMPDU;
221
222                 if (bf)
223                         continue;
224
225                 bf = ath_tx_setup_buffer(sc, txq, tid, skb);
226                 if (!bf) {
227                         __skb_unlink(skb, &tid->buf_q);
228                         ath_txq_skb_done(sc, txq, skb);
229                         ieee80211_free_txskb(sc->hw, skb);
230                         continue;
231                 }
232         }
233
234 }
235
236 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
237 {
238         struct ath_txq *txq = tid->ac->txq;
239         struct sk_buff *skb;
240         struct ath_buf *bf;
241         struct list_head bf_head;
242         struct ath_tx_status ts;
243         struct ath_frame_info *fi;
244         bool sendbar = false;
245
246         INIT_LIST_HEAD(&bf_head);
247
248         memset(&ts, 0, sizeof(ts));
249
250         while ((skb = __skb_dequeue(&tid->retry_q))) {
251                 fi = get_frame_info(skb);
252                 bf = fi->bf;
253                 if (!bf) {
254                         ath_txq_skb_done(sc, txq, skb);
255                         ieee80211_free_txskb(sc->hw, skb);
256                         continue;
257                 }
258
259                 if (fi->baw_tracked) {
260                         ath_tx_update_baw(sc, tid, bf->bf_state.seqno);
261                         sendbar = true;
262                 }
263
264                 list_add_tail(&bf->list, &bf_head);
265                 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
266         }
267
268         if (sendbar) {
269                 ath_txq_unlock(sc, txq);
270                 ath_send_bar(tid, tid->seq_start);
271                 ath_txq_lock(sc, txq);
272         }
273 }
274
275 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
276                               int seqno)
277 {
278         int index, cindex;
279
280         index  = ATH_BA_INDEX(tid->seq_start, seqno);
281         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
282
283         __clear_bit(cindex, tid->tx_buf);
284
285         while (tid->baw_head != tid->baw_tail && !test_bit(tid->baw_head, tid->tx_buf)) {
286                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
287                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
288                 if (tid->bar_index >= 0)
289                         tid->bar_index--;
290         }
291 }
292
293 static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
294                              struct ath_buf *bf)
295 {
296         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
297         u16 seqno = bf->bf_state.seqno;
298         int index, cindex;
299
300         index  = ATH_BA_INDEX(tid->seq_start, seqno);
301         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
302         __set_bit(cindex, tid->tx_buf);
303         fi->baw_tracked = 1;
304
305         if (index >= ((tid->baw_tail - tid->baw_head) &
306                 (ATH_TID_MAX_BUFS - 1))) {
307                 tid->baw_tail = cindex;
308                 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
309         }
310 }
311
312 static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
313                           struct ath_atx_tid *tid)
314
315 {
316         struct sk_buff *skb;
317         struct ath_buf *bf;
318         struct list_head bf_head;
319         struct ath_tx_status ts;
320         struct ath_frame_info *fi;
321
322         memset(&ts, 0, sizeof(ts));
323         INIT_LIST_HEAD(&bf_head);
324
325         while ((skb = ath_tid_dequeue(tid))) {
326                 fi = get_frame_info(skb);
327                 bf = fi->bf;
328
329                 if (!bf) {
330                         ath_tx_complete(sc, skb, ATH_TX_ERROR, txq);
331                         continue;
332                 }
333
334                 list_add_tail(&bf->list, &bf_head);
335                 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
336         }
337 }
338
339 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_txq *txq,
340                              struct sk_buff *skb, int count)
341 {
342         struct ath_frame_info *fi = get_frame_info(skb);
343         struct ath_buf *bf = fi->bf;
344         struct ieee80211_hdr *hdr;
345         int prev = fi->retries;
346
347         TX_STAT_INC(txq->axq_qnum, a_retries);
348         fi->retries += count;
349
350         if (prev > 0)
351                 return;
352
353         hdr = (struct ieee80211_hdr *)skb->data;
354         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
355         dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
356                 sizeof(*hdr), DMA_TO_DEVICE);
357 }
358
359 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
360 {
361         struct ath_buf *bf = NULL;
362
363         spin_lock_bh(&sc->tx.txbuflock);
364
365         if (unlikely(list_empty(&sc->tx.txbuf))) {
366                 spin_unlock_bh(&sc->tx.txbuflock);
367                 return NULL;
368         }
369
370         bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
371         list_del(&bf->list);
372
373         spin_unlock_bh(&sc->tx.txbuflock);
374
375         return bf;
376 }
377
378 static void ath_tx_return_buffer(struct ath_softc *sc, struct ath_buf *bf)
379 {
380         spin_lock_bh(&sc->tx.txbuflock);
381         list_add_tail(&bf->list, &sc->tx.txbuf);
382         spin_unlock_bh(&sc->tx.txbuflock);
383 }
384
385 static struct ath_buf* ath_clone_txbuf(struct ath_softc *sc, struct ath_buf *bf)
386 {
387         struct ath_buf *tbf;
388
389         tbf = ath_tx_get_buffer(sc);
390         if (WARN_ON(!tbf))
391                 return NULL;
392
393         ATH_TXBUF_RESET(tbf);
394
395         tbf->bf_mpdu = bf->bf_mpdu;
396         tbf->bf_buf_addr = bf->bf_buf_addr;
397         memcpy(tbf->bf_desc, bf->bf_desc, sc->sc_ah->caps.tx_desc_len);
398         tbf->bf_state = bf->bf_state;
399         tbf->bf_state.stale = false;
400
401         return tbf;
402 }
403
404 static void ath_tx_count_frames(struct ath_softc *sc, struct ath_buf *bf,
405                                 struct ath_tx_status *ts, int txok,
406                                 int *nframes, int *nbad)
407 {
408         struct ath_frame_info *fi;
409         u16 seq_st = 0;
410         u32 ba[WME_BA_BMP_SIZE >> 5];
411         int ba_index;
412         int isaggr = 0;
413
414         *nbad = 0;
415         *nframes = 0;
416
417         isaggr = bf_isaggr(bf);
418         if (isaggr) {
419                 seq_st = ts->ts_seqnum;
420                 memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
421         }
422
423         while (bf) {
424                 fi = get_frame_info(bf->bf_mpdu);
425                 ba_index = ATH_BA_INDEX(seq_st, bf->bf_state.seqno);
426
427                 (*nframes)++;
428                 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
429                         (*nbad)++;
430
431                 bf = bf->bf_next;
432         }
433 }
434
435
436 static void ath_tx_complete_aggr(struct ath_softc *sc, struct ath_txq *txq,
437                                  struct ath_buf *bf, struct list_head *bf_q,
438                                  struct ath_tx_status *ts, int txok)
439 {
440         struct ath_node *an = NULL;
441         struct sk_buff *skb;
442         struct ieee80211_sta *sta;
443         struct ieee80211_hw *hw = sc->hw;
444         struct ieee80211_hdr *hdr;
445         struct ieee80211_tx_info *tx_info;
446         struct ath_atx_tid *tid = NULL;
447         struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
448         struct list_head bf_head;
449         struct sk_buff_head bf_pending;
450         u16 seq_st = 0, acked_cnt = 0, txfail_cnt = 0, seq_first;
451         u32 ba[WME_BA_BMP_SIZE >> 5];
452         int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
453         bool rc_update = true, isba;
454         struct ieee80211_tx_rate rates[4];
455         struct ath_frame_info *fi;
456         int nframes;
457         bool flush = !!(ts->ts_status & ATH9K_TX_FLUSH);
458         int i, retries;
459         int bar_index = -1;
460
461         skb = bf->bf_mpdu;
462         hdr = (struct ieee80211_hdr *)skb->data;
463
464         tx_info = IEEE80211_SKB_CB(skb);
465
466         memcpy(rates, bf->rates, sizeof(rates));
467
468         retries = ts->ts_longretry + 1;
469         for (i = 0; i < ts->ts_rateindex; i++)
470                 retries += rates[i].count;
471
472         rcu_read_lock();
473
474         sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr1, hdr->addr2);
475         if (!sta) {
476                 rcu_read_unlock();
477
478                 INIT_LIST_HEAD(&bf_head);
479                 while (bf) {
480                         bf_next = bf->bf_next;
481
482                         if (!bf->bf_state.stale || bf_next != NULL)
483                                 list_move_tail(&bf->list, &bf_head);
484
485                         ath_tx_complete_buf(sc, bf, txq, &bf_head, ts, 0);
486
487                         bf = bf_next;
488                 }
489                 return;
490         }
491
492         an = (struct ath_node *)sta->drv_priv;
493         tid = ath_get_skb_tid(sc, an, skb);
494         seq_first = tid->seq_start;
495         isba = ts->ts_flags & ATH9K_TX_BA;
496
497         /*
498          * The hardware occasionally sends a tx status for the wrong TID.
499          * In this case, the BA status cannot be considered valid and all
500          * subframes need to be retransmitted
501          *
502          * Only BlockAcks have a TID and therefore normal Acks cannot be
503          * checked
504          */
505         if (isba && tid->tidno != ts->tid)
506                 txok = false;
507
508         isaggr = bf_isaggr(bf);
509         memset(ba, 0, WME_BA_BMP_SIZE >> 3);
510
511         if (isaggr && txok) {
512                 if (ts->ts_flags & ATH9K_TX_BA) {
513                         seq_st = ts->ts_seqnum;
514                         memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
515                 } else {
516                         /*
517                          * AR5416 can become deaf/mute when BA
518                          * issue happens. Chip needs to be reset.
519                          * But AP code may have sychronization issues
520                          * when perform internal reset in this routine.
521                          * Only enable reset in STA mode for now.
522                          */
523                         if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION)
524                                 needreset = 1;
525                 }
526         }
527
528         __skb_queue_head_init(&bf_pending);
529
530         ath_tx_count_frames(sc, bf, ts, txok, &nframes, &nbad);
531         while (bf) {
532                 u16 seqno = bf->bf_state.seqno;
533
534                 txfail = txpending = sendbar = 0;
535                 bf_next = bf->bf_next;
536
537                 skb = bf->bf_mpdu;
538                 tx_info = IEEE80211_SKB_CB(skb);
539                 fi = get_frame_info(skb);
540
541                 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno) ||
542                     !tid->active) {
543                         /*
544                          * Outside of the current BlockAck window,
545                          * maybe part of a previous session
546                          */
547                         txfail = 1;
548                 } else if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, seqno))) {
549                         /* transmit completion, subframe is
550                          * acked by block ack */
551                         acked_cnt++;
552                 } else if (!isaggr && txok) {
553                         /* transmit completion */
554                         acked_cnt++;
555                 } else if (flush) {
556                         txpending = 1;
557                 } else if (fi->retries < ATH_MAX_SW_RETRIES) {
558                         if (txok || !an->sleeping)
559                                 ath_tx_set_retry(sc, txq, bf->bf_mpdu,
560                                                  retries);
561
562                         txpending = 1;
563                 } else {
564                         txfail = 1;
565                         txfail_cnt++;
566                         bar_index = max_t(int, bar_index,
567                                 ATH_BA_INDEX(seq_first, seqno));
568                 }
569
570                 /*
571                  * Make sure the last desc is reclaimed if it
572                  * not a holding desc.
573                  */
574                 INIT_LIST_HEAD(&bf_head);
575                 if (bf_next != NULL || !bf_last->bf_state.stale)
576                         list_move_tail(&bf->list, &bf_head);
577
578                 if (!txpending) {
579                         /*
580                          * complete the acked-ones/xretried ones; update
581                          * block-ack window
582                          */
583                         ath_tx_update_baw(sc, tid, seqno);
584
585                         if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
586                                 memcpy(tx_info->control.rates, rates, sizeof(rates));
587                                 ath_tx_rc_status(sc, bf, ts, nframes, nbad, txok);
588                                 rc_update = false;
589                         }
590
591                         ath_tx_complete_buf(sc, bf, txq, &bf_head, ts,
592                                 !txfail);
593                 } else {
594                         if (tx_info->flags & IEEE80211_TX_STATUS_EOSP) {
595                                 tx_info->flags &= ~IEEE80211_TX_STATUS_EOSP;
596                                 ieee80211_sta_eosp(sta);
597                         }
598                         /* retry the un-acked ones */
599                         if (bf->bf_next == NULL && bf_last->bf_state.stale) {
600                                 struct ath_buf *tbf;
601
602                                 tbf = ath_clone_txbuf(sc, bf_last);
603                                 /*
604                                  * Update tx baw and complete the
605                                  * frame with failed status if we
606                                  * run out of tx buf.
607                                  */
608                                 if (!tbf) {
609                                         ath_tx_update_baw(sc, tid, seqno);
610
611                                         ath_tx_complete_buf(sc, bf, txq,
612                                                             &bf_head, ts, 0);
613                                         bar_index = max_t(int, bar_index,
614                                                 ATH_BA_INDEX(seq_first, seqno));
615                                         break;
616                                 }
617
618                                 fi->bf = tbf;
619                         }
620
621                         /*
622                          * Put this buffer to the temporary pending
623                          * queue to retain ordering
624                          */
625                         __skb_queue_tail(&bf_pending, skb);
626                 }
627
628                 bf = bf_next;
629         }
630
631         /* prepend un-acked frames to the beginning of the pending frame queue */
632         if (!skb_queue_empty(&bf_pending)) {
633                 if (an->sleeping)
634                         ieee80211_sta_set_buffered(sta, tid->tidno, true);
635
636                 skb_queue_splice_tail(&bf_pending, &tid->retry_q);
637                 if (!an->sleeping) {
638                         ath_tx_queue_tid(sc, txq, tid);
639
640                         if (ts->ts_status & (ATH9K_TXERR_FILT | ATH9K_TXERR_XRETRY))
641                                 tid->ac->clear_ps_filter = true;
642                 }
643         }
644
645         if (bar_index >= 0) {
646                 u16 bar_seq = ATH_BA_INDEX2SEQ(seq_first, bar_index);
647
648                 if (BAW_WITHIN(tid->seq_start, tid->baw_size, bar_seq))
649                         tid->bar_index = ATH_BA_INDEX(tid->seq_start, bar_seq);
650
651                 ath_txq_unlock(sc, txq);
652                 ath_send_bar(tid, ATH_BA_INDEX2SEQ(seq_first, bar_index + 1));
653                 ath_txq_lock(sc, txq);
654         }
655
656         rcu_read_unlock();
657
658         if (needreset)
659                 ath9k_queue_reset(sc, RESET_TYPE_TX_ERROR);
660 }
661
662 static bool bf_is_ampdu_not_probing(struct ath_buf *bf)
663 {
664     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(bf->bf_mpdu);
665     return bf_isampdu(bf) && !(info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
666 }
667
668 static void ath_tx_process_buffer(struct ath_softc *sc, struct ath_txq *txq,
669                                   struct ath_tx_status *ts, struct ath_buf *bf,
670                                   struct list_head *bf_head)
671 {
672         struct ieee80211_tx_info *info;
673         bool txok, flush;
674
675         txok = !(ts->ts_status & ATH9K_TXERR_MASK);
676         flush = !!(ts->ts_status & ATH9K_TX_FLUSH);
677         txq->axq_tx_inprogress = false;
678
679         txq->axq_depth--;
680         if (bf_is_ampdu_not_probing(bf))
681                 txq->axq_ampdu_depth--;
682
683         if (!bf_isampdu(bf)) {
684                 if (!flush) {
685                         info = IEEE80211_SKB_CB(bf->bf_mpdu);
686                         memcpy(info->control.rates, bf->rates,
687                                sizeof(info->control.rates));
688                         ath_tx_rc_status(sc, bf, ts, 1, txok ? 0 : 1, txok);
689                 }
690                 ath_tx_complete_buf(sc, bf, txq, bf_head, ts, txok);
691         } else
692                 ath_tx_complete_aggr(sc, txq, bf, bf_head, ts, txok);
693
694         if (!flush)
695                 ath_txq_schedule(sc, txq);
696 }
697
698 static bool ath_lookup_legacy(struct ath_buf *bf)
699 {
700         struct sk_buff *skb;
701         struct ieee80211_tx_info *tx_info;
702         struct ieee80211_tx_rate *rates;
703         int i;
704
705         skb = bf->bf_mpdu;
706         tx_info = IEEE80211_SKB_CB(skb);
707         rates = tx_info->control.rates;
708
709         for (i = 0; i < 4; i++) {
710                 if (!rates[i].count || rates[i].idx < 0)
711                         break;
712
713                 if (!(rates[i].flags & IEEE80211_TX_RC_MCS))
714                         return true;
715         }
716
717         return false;
718 }
719
720 static u32 ath_lookup_rate(struct ath_softc *sc, struct ath_buf *bf,
721                            struct ath_atx_tid *tid)
722 {
723         struct sk_buff *skb;
724         struct ieee80211_tx_info *tx_info;
725         struct ieee80211_tx_rate *rates;
726         u32 max_4ms_framelen, frmlen;
727         u16 aggr_limit, bt_aggr_limit, legacy = 0;
728         int q = tid->ac->txq->mac80211_qnum;
729         int i;
730
731         skb = bf->bf_mpdu;
732         tx_info = IEEE80211_SKB_CB(skb);
733         rates = bf->rates;
734
735         /*
736          * Find the lowest frame length among the rate series that will have a
737          * 4ms (or TXOP limited) transmit duration.
738          */
739         max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
740
741         for (i = 0; i < 4; i++) {
742                 int modeidx;
743
744                 if (!rates[i].count)
745                         continue;
746
747                 if (!(rates[i].flags & IEEE80211_TX_RC_MCS)) {
748                         legacy = 1;
749                         break;
750                 }
751
752                 if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
753                         modeidx = MCS_HT40;
754                 else
755                         modeidx = MCS_HT20;
756
757                 if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
758                         modeidx++;
759
760                 frmlen = sc->tx.max_aggr_framelen[q][modeidx][rates[i].idx];
761                 max_4ms_framelen = min(max_4ms_framelen, frmlen);
762         }
763
764         /*
765          * limit aggregate size by the minimum rate if rate selected is
766          * not a probe rate, if rate selected is a probe rate then
767          * avoid aggregation of this packet.
768          */
769         if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
770                 return 0;
771
772         aggr_limit = min(max_4ms_framelen, (u32)ATH_AMPDU_LIMIT_MAX);
773
774         /*
775          * Override the default aggregation limit for BTCOEX.
776          */
777         bt_aggr_limit = ath9k_btcoex_aggr_limit(sc, max_4ms_framelen);
778         if (bt_aggr_limit)
779                 aggr_limit = bt_aggr_limit;
780
781         if (tid->an->maxampdu)
782                 aggr_limit = min(aggr_limit, tid->an->maxampdu);
783
784         return aggr_limit;
785 }
786
787 /*
788  * Returns the number of delimiters to be added to
789  * meet the minimum required mpdudensity.
790  */
791 static int ath_compute_num_delims(struct ath_softc *sc, struct ath_atx_tid *tid,
792                                   struct ath_buf *bf, u16 frmlen,
793                                   bool first_subfrm)
794 {
795 #define FIRST_DESC_NDELIMS 60
796         u32 nsymbits, nsymbols;
797         u16 minlen;
798         u8 flags, rix;
799         int width, streams, half_gi, ndelim, mindelim;
800         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
801
802         /* Select standard number of delimiters based on frame length alone */
803         ndelim = ATH_AGGR_GET_NDELIM(frmlen);
804
805         /*
806          * If encryption enabled, hardware requires some more padding between
807          * subframes.
808          * TODO - this could be improved to be dependent on the rate.
809          *      The hardware can keep up at lower rates, but not higher rates
810          */
811         if ((fi->keyix != ATH9K_TXKEYIX_INVALID) &&
812             !(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA))
813                 ndelim += ATH_AGGR_ENCRYPTDELIM;
814
815         /*
816          * Add delimiter when using RTS/CTS with aggregation
817          * and non enterprise AR9003 card
818          */
819         if (first_subfrm && !AR_SREV_9580_10_OR_LATER(sc->sc_ah) &&
820             (sc->sc_ah->ent_mode & AR_ENT_OTP_MIN_PKT_SIZE_DISABLE))
821                 ndelim = max(ndelim, FIRST_DESC_NDELIMS);
822
823         /*
824          * Convert desired mpdu density from microeconds to bytes based
825          * on highest rate in rate series (i.e. first rate) to determine
826          * required minimum length for subframe. Take into account
827          * whether high rate is 20 or 40Mhz and half or full GI.
828          *
829          * If there is no mpdu density restriction, no further calculation
830          * is needed.
831          */
832
833         if (tid->an->mpdudensity == 0)
834                 return ndelim;
835
836         rix = bf->rates[0].idx;
837         flags = bf->rates[0].flags;
838         width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
839         half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
840
841         if (half_gi)
842                 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(tid->an->mpdudensity);
843         else
844                 nsymbols = NUM_SYMBOLS_PER_USEC(tid->an->mpdudensity);
845
846         if (nsymbols == 0)
847                 nsymbols = 1;
848
849         streams = HT_RC_2_STREAMS(rix);
850         nsymbits = bits_per_symbol[rix % 8][width] * streams;
851         minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
852
853         if (frmlen < minlen) {
854                 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
855                 ndelim = max(mindelim, ndelim);
856         }
857
858         return ndelim;
859 }
860
861 static struct ath_buf *
862 ath_tx_get_tid_subframe(struct ath_softc *sc, struct ath_txq *txq,
863                         struct ath_atx_tid *tid, struct sk_buff_head **q)
864 {
865         struct ieee80211_tx_info *tx_info;
866         struct ath_frame_info *fi;
867         struct sk_buff *skb;
868         struct ath_buf *bf;
869         u16 seqno;
870
871         while (1) {
872                 *q = &tid->retry_q;
873                 if (skb_queue_empty(*q))
874                         *q = &tid->buf_q;
875
876                 skb = skb_peek(*q);
877                 if (!skb)
878                         break;
879
880                 fi = get_frame_info(skb);
881                 bf = fi->bf;
882                 if (!fi->bf)
883                         bf = ath_tx_setup_buffer(sc, txq, tid, skb);
884                 else
885                         bf->bf_state.stale = false;
886
887                 if (!bf) {
888                         __skb_unlink(skb, *q);
889                         ath_txq_skb_done(sc, txq, skb);
890                         ieee80211_free_txskb(sc->hw, skb);
891                         continue;
892                 }
893
894                 bf->bf_next = NULL;
895                 bf->bf_lastbf = bf;
896
897                 tx_info = IEEE80211_SKB_CB(skb);
898                 tx_info->flags &= ~IEEE80211_TX_CTL_CLEAR_PS_FILT;
899                 if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
900                         bf->bf_state.bf_type = 0;
901                         return bf;
902                 }
903
904                 bf->bf_state.bf_type = BUF_AMPDU | BUF_AGGR;
905                 seqno = bf->bf_state.seqno;
906
907                 /* do not step over block-ack window */
908                 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno))
909                         break;
910
911                 if (tid->bar_index > ATH_BA_INDEX(tid->seq_start, seqno)) {
912                         struct ath_tx_status ts = {};
913                         struct list_head bf_head;
914
915                         INIT_LIST_HEAD(&bf_head);
916                         list_add(&bf->list, &bf_head);
917                         __skb_unlink(skb, *q);
918                         ath_tx_update_baw(sc, tid, seqno);
919                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
920                         continue;
921                 }
922
923                 return bf;
924         }
925
926         return NULL;
927 }
928
929 static bool
930 ath_tx_form_aggr(struct ath_softc *sc, struct ath_txq *txq,
931                  struct ath_atx_tid *tid, struct list_head *bf_q,
932                  struct ath_buf *bf_first, struct sk_buff_head *tid_q,
933                  int *aggr_len)
934 {
935 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
936         struct ath_buf *bf = bf_first, *bf_prev = NULL;
937         int nframes = 0, ndelim;
938         u16 aggr_limit = 0, al = 0, bpad = 0,
939             al_delta, h_baw = tid->baw_size / 2;
940         struct ieee80211_tx_info *tx_info;
941         struct ath_frame_info *fi;
942         struct sk_buff *skb;
943         bool closed = false;
944
945         bf = bf_first;
946         aggr_limit = ath_lookup_rate(sc, bf, tid);
947
948         do {
949                 skb = bf->bf_mpdu;
950                 fi = get_frame_info(skb);
951
952                 /* do not exceed aggregation limit */
953                 al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
954                 if (nframes) {
955                         if (aggr_limit < al + bpad + al_delta ||
956                             ath_lookup_legacy(bf) || nframes >= h_baw)
957                                 break;
958
959                         tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
960                         if ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
961                             !(tx_info->flags & IEEE80211_TX_CTL_AMPDU))
962                                 break;
963                 }
964
965                 /* add padding for previous frame to aggregation length */
966                 al += bpad + al_delta;
967
968                 /*
969                  * Get the delimiters needed to meet the MPDU
970                  * density for this node.
971                  */
972                 ndelim = ath_compute_num_delims(sc, tid, bf_first, fi->framelen,
973                                                 !nframes);
974                 bpad = PADBYTES(al_delta) + (ndelim << 2);
975
976                 nframes++;
977                 bf->bf_next = NULL;
978
979                 /* link buffers of this frame to the aggregate */
980                 if (!fi->baw_tracked)
981                         ath_tx_addto_baw(sc, tid, bf);
982                 bf->bf_state.ndelim = ndelim;
983
984                 __skb_unlink(skb, tid_q);
985                 list_add_tail(&bf->list, bf_q);
986                 if (bf_prev)
987                         bf_prev->bf_next = bf;
988
989                 bf_prev = bf;
990
991                 bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
992                 if (!bf) {
993                         closed = true;
994                         break;
995                 }
996         } while (ath_tid_has_buffered(tid));
997
998         bf = bf_first;
999         bf->bf_lastbf = bf_prev;
1000
1001         if (bf == bf_prev) {
1002                 al = get_frame_info(bf->bf_mpdu)->framelen;
1003                 bf->bf_state.bf_type = BUF_AMPDU;
1004         } else {
1005                 TX_STAT_INC(txq->axq_qnum, a_aggr);
1006         }
1007
1008         *aggr_len = al;
1009
1010         return closed;
1011 #undef PADBYTES
1012 }
1013
1014 /*
1015  * rix - rate index
1016  * pktlen - total bytes (delims + data + fcs + pads + pad delims)
1017  * width  - 0 for 20 MHz, 1 for 40 MHz
1018  * half_gi - to use 4us v/s 3.6 us for symbol time
1019  */
1020 static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, int pktlen,
1021                             int width, int half_gi, bool shortPreamble)
1022 {
1023         u32 nbits, nsymbits, duration, nsymbols;
1024         int streams;
1025
1026         /* find number of symbols: PLCP + data */
1027         streams = HT_RC_2_STREAMS(rix);
1028         nbits = (pktlen << 3) + OFDM_PLCP_BITS;
1029         nsymbits = bits_per_symbol[rix % 8][width] * streams;
1030         nsymbols = (nbits + nsymbits - 1) / nsymbits;
1031
1032         if (!half_gi)
1033                 duration = SYMBOL_TIME(nsymbols);
1034         else
1035                 duration = SYMBOL_TIME_HALFGI(nsymbols);
1036
1037         /* addup duration for legacy/ht training and signal fields */
1038         duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
1039
1040         return duration;
1041 }
1042
1043 static int ath_max_framelen(int usec, int mcs, bool ht40, bool sgi)
1044 {
1045         int streams = HT_RC_2_STREAMS(mcs);
1046         int symbols, bits;
1047         int bytes = 0;
1048
1049         usec -= L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
1050         symbols = sgi ? TIME_SYMBOLS_HALFGI(usec) : TIME_SYMBOLS(usec);
1051         bits = symbols * bits_per_symbol[mcs % 8][ht40] * streams;
1052         bits -= OFDM_PLCP_BITS;
1053         bytes = bits / 8;
1054         if (bytes > 65532)
1055                 bytes = 65532;
1056
1057         return bytes;
1058 }
1059
1060 void ath_update_max_aggr_framelen(struct ath_softc *sc, int queue, int txop)
1061 {
1062         u16 *cur_ht20, *cur_ht20_sgi, *cur_ht40, *cur_ht40_sgi;
1063         int mcs;
1064
1065         /* 4ms is the default (and maximum) duration */
1066         if (!txop || txop > 4096)
1067                 txop = 4096;
1068
1069         cur_ht20 = sc->tx.max_aggr_framelen[queue][MCS_HT20];
1070         cur_ht20_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT20_SGI];
1071         cur_ht40 = sc->tx.max_aggr_framelen[queue][MCS_HT40];
1072         cur_ht40_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT40_SGI];
1073         for (mcs = 0; mcs < 32; mcs++) {
1074                 cur_ht20[mcs] = ath_max_framelen(txop, mcs, false, false);
1075                 cur_ht20_sgi[mcs] = ath_max_framelen(txop, mcs, false, true);
1076                 cur_ht40[mcs] = ath_max_framelen(txop, mcs, true, false);
1077                 cur_ht40_sgi[mcs] = ath_max_framelen(txop, mcs, true, true);
1078         }
1079 }
1080
1081 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf,
1082                              struct ath_tx_info *info, int len, bool rts)
1083 {
1084         struct ath_hw *ah = sc->sc_ah;
1085         struct ath_common *common = ath9k_hw_common(ah);
1086         struct sk_buff *skb;
1087         struct ieee80211_tx_info *tx_info;
1088         struct ieee80211_tx_rate *rates;
1089         const struct ieee80211_rate *rate;
1090         struct ieee80211_hdr *hdr;
1091         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
1092         u32 rts_thresh = sc->hw->wiphy->rts_threshold;
1093         int i;
1094         u8 rix = 0;
1095
1096         skb = bf->bf_mpdu;
1097         tx_info = IEEE80211_SKB_CB(skb);
1098         rates = bf->rates;
1099         hdr = (struct ieee80211_hdr *)skb->data;
1100
1101         /* set dur_update_en for l-sig computation except for PS-Poll frames */
1102         info->dur_update = !ieee80211_is_pspoll(hdr->frame_control);
1103         info->rtscts_rate = fi->rtscts_rate;
1104
1105         for (i = 0; i < ARRAY_SIZE(bf->rates); i++) {
1106                 bool is_40, is_sgi, is_sp;
1107                 int phy;
1108
1109                 if (!rates[i].count || (rates[i].idx < 0))
1110                         continue;
1111
1112                 rix = rates[i].idx;
1113                 info->rates[i].Tries = rates[i].count;
1114
1115                 /*
1116                  * Handle RTS threshold for unaggregated HT frames.
1117                  */
1118                 if (bf_isampdu(bf) && !bf_isaggr(bf) &&
1119                     (rates[i].flags & IEEE80211_TX_RC_MCS) &&
1120                     unlikely(rts_thresh != (u32) -1)) {
1121                         if (!rts_thresh || (len > rts_thresh))
1122                                 rts = true;
1123                 }
1124
1125                 if (rts || rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
1126                         info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1127                         info->flags |= ATH9K_TXDESC_RTSENA;
1128                 } else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
1129                         info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1130                         info->flags |= ATH9K_TXDESC_CTSENA;
1131                 }
1132
1133                 if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1134                         info->rates[i].RateFlags |= ATH9K_RATESERIES_2040;
1135                 if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
1136                         info->rates[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
1137
1138                 is_sgi = !!(rates[i].flags & IEEE80211_TX_RC_SHORT_GI);
1139                 is_40 = !!(rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
1140                 is_sp = !!(rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
1141
1142                 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
1143                         /* MCS rates */
1144                         info->rates[i].Rate = rix | 0x80;
1145                         info->rates[i].ChSel = ath_txchainmask_reduction(sc,
1146                                         ah->txchainmask, info->rates[i].Rate);
1147                         info->rates[i].PktDuration = ath_pkt_duration(sc, rix, len,
1148                                  is_40, is_sgi, is_sp);
1149                         if (rix < 8 && (tx_info->flags & IEEE80211_TX_CTL_STBC))
1150                                 info->rates[i].RateFlags |= ATH9K_RATESERIES_STBC;
1151                         continue;
1152                 }
1153
1154                 /* legacy rates */
1155                 rate = &common->sbands[tx_info->band].bitrates[rates[i].idx];
1156                 if ((tx_info->band == IEEE80211_BAND_2GHZ) &&
1157                     !(rate->flags & IEEE80211_RATE_ERP_G))
1158                         phy = WLAN_RC_PHY_CCK;
1159                 else
1160                         phy = WLAN_RC_PHY_OFDM;
1161
1162                 info->rates[i].Rate = rate->hw_value;
1163                 if (rate->hw_value_short) {
1164                         if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1165                                 info->rates[i].Rate |= rate->hw_value_short;
1166                 } else {
1167                         is_sp = false;
1168                 }
1169
1170                 if (bf->bf_state.bfs_paprd)
1171                         info->rates[i].ChSel = ah->txchainmask;
1172                 else
1173                         info->rates[i].ChSel = ath_txchainmask_reduction(sc,
1174                                         ah->txchainmask, info->rates[i].Rate);
1175
1176                 info->rates[i].PktDuration = ath9k_hw_computetxtime(sc->sc_ah,
1177                         phy, rate->bitrate * 100, len, rix, is_sp);
1178         }
1179
1180         /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
1181         if (bf_isaggr(bf) && (len > sc->sc_ah->caps.rts_aggr_limit))
1182                 info->flags &= ~ATH9K_TXDESC_RTSENA;
1183
1184         /* ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive. */
1185         if (info->flags & ATH9K_TXDESC_RTSENA)
1186                 info->flags &= ~ATH9K_TXDESC_CTSENA;
1187 }
1188
1189 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
1190 {
1191         struct ieee80211_hdr *hdr;
1192         enum ath9k_pkt_type htype;
1193         __le16 fc;
1194
1195         hdr = (struct ieee80211_hdr *)skb->data;
1196         fc = hdr->frame_control;
1197
1198         if (ieee80211_is_beacon(fc))
1199                 htype = ATH9K_PKT_TYPE_BEACON;
1200         else if (ieee80211_is_probe_resp(fc))
1201                 htype = ATH9K_PKT_TYPE_PROBE_RESP;
1202         else if (ieee80211_is_atim(fc))
1203                 htype = ATH9K_PKT_TYPE_ATIM;
1204         else if (ieee80211_is_pspoll(fc))
1205                 htype = ATH9K_PKT_TYPE_PSPOLL;
1206         else
1207                 htype = ATH9K_PKT_TYPE_NORMAL;
1208
1209         return htype;
1210 }
1211
1212 static void ath_tx_fill_desc(struct ath_softc *sc, struct ath_buf *bf,
1213                              struct ath_txq *txq, int len)
1214 {
1215         struct ath_hw *ah = sc->sc_ah;
1216         struct ath_buf *bf_first = NULL;
1217         struct ath_tx_info info;
1218         u32 rts_thresh = sc->hw->wiphy->rts_threshold;
1219         bool rts = false;
1220
1221         memset(&info, 0, sizeof(info));
1222         info.is_first = true;
1223         info.is_last = true;
1224         info.txpower = MAX_RATE_POWER;
1225         info.qcu = txq->axq_qnum;
1226
1227         while (bf) {
1228                 struct sk_buff *skb = bf->bf_mpdu;
1229                 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1230                 struct ath_frame_info *fi = get_frame_info(skb);
1231                 bool aggr = !!(bf->bf_state.bf_type & BUF_AGGR);
1232
1233                 info.type = get_hw_packet_type(skb);
1234                 if (bf->bf_next)
1235                         info.link = bf->bf_next->bf_daddr;
1236                 else
1237                         info.link = (sc->tx99_state) ? bf->bf_daddr : 0;
1238
1239                 if (!bf_first) {
1240                         bf_first = bf;
1241
1242                         if (!sc->tx99_state)
1243                                 info.flags = ATH9K_TXDESC_INTREQ;
1244                         if ((tx_info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) ||
1245                             txq == sc->tx.uapsdq)
1246                                 info.flags |= ATH9K_TXDESC_CLRDMASK;
1247
1248                         if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
1249                                 info.flags |= ATH9K_TXDESC_NOACK;
1250                         if (tx_info->flags & IEEE80211_TX_CTL_LDPC)
1251                                 info.flags |= ATH9K_TXDESC_LDPC;
1252
1253                         if (bf->bf_state.bfs_paprd)
1254                                 info.flags |= (u32) bf->bf_state.bfs_paprd <<
1255                                               ATH9K_TXDESC_PAPRD_S;
1256
1257                         /*
1258                          * mac80211 doesn't handle RTS threshold for HT because
1259                          * the decision has to be taken based on AMPDU length
1260                          * and aggregation is done entirely inside ath9k.
1261                          * Set the RTS/CTS flag for the first subframe based
1262                          * on the threshold.
1263                          */
1264                         if (aggr && (bf == bf_first) &&
1265                             unlikely(rts_thresh != (u32) -1)) {
1266                                 /*
1267                                  * "len" is the size of the entire AMPDU.
1268                                  */
1269                                 if (!rts_thresh || (len > rts_thresh))
1270                                         rts = true;
1271                         }
1272
1273                         if (!aggr)
1274                                 len = fi->framelen;
1275
1276                         ath_buf_set_rate(sc, bf, &info, len, rts);
1277                 }
1278
1279                 info.buf_addr[0] = bf->bf_buf_addr;
1280                 info.buf_len[0] = skb->len;
1281                 info.pkt_len = fi->framelen;
1282                 info.keyix = fi->keyix;
1283                 info.keytype = fi->keytype;
1284
1285                 if (aggr) {
1286                         if (bf == bf_first)
1287                                 info.aggr = AGGR_BUF_FIRST;
1288                         else if (bf == bf_first->bf_lastbf)
1289                                 info.aggr = AGGR_BUF_LAST;
1290                         else
1291                                 info.aggr = AGGR_BUF_MIDDLE;
1292
1293                         info.ndelim = bf->bf_state.ndelim;
1294                         info.aggr_len = len;
1295                 }
1296
1297                 if (bf == bf_first->bf_lastbf)
1298                         bf_first = NULL;
1299
1300                 ath9k_hw_set_txdesc(ah, bf->bf_desc, &info);
1301                 bf = bf->bf_next;
1302         }
1303 }
1304
1305 static void
1306 ath_tx_form_burst(struct ath_softc *sc, struct ath_txq *txq,
1307                   struct ath_atx_tid *tid, struct list_head *bf_q,
1308                   struct ath_buf *bf_first, struct sk_buff_head *tid_q)
1309 {
1310         struct ath_buf *bf = bf_first, *bf_prev = NULL;
1311         struct sk_buff *skb;
1312         int nframes = 0;
1313
1314         do {
1315                 struct ieee80211_tx_info *tx_info;
1316                 skb = bf->bf_mpdu;
1317
1318                 nframes++;
1319                 __skb_unlink(skb, tid_q);
1320                 list_add_tail(&bf->list, bf_q);
1321                 if (bf_prev)
1322                         bf_prev->bf_next = bf;
1323                 bf_prev = bf;
1324
1325                 if (nframes >= 2)
1326                         break;
1327
1328                 bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
1329                 if (!bf)
1330                         break;
1331
1332                 tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1333                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1334                         break;
1335
1336                 ath_set_rates(tid->an->vif, tid->an->sta, bf);
1337         } while (1);
1338 }
1339
1340 static bool ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
1341                               struct ath_atx_tid *tid, bool *stop)
1342 {
1343         struct ath_buf *bf;
1344         struct ieee80211_tx_info *tx_info;
1345         struct sk_buff_head *tid_q;
1346         struct list_head bf_q;
1347         int aggr_len = 0;
1348         bool aggr, last = true;
1349
1350         if (!ath_tid_has_buffered(tid))
1351                 return false;
1352
1353         INIT_LIST_HEAD(&bf_q);
1354
1355         bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
1356         if (!bf)
1357                 return false;
1358
1359         tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1360         aggr = !!(tx_info->flags & IEEE80211_TX_CTL_AMPDU);
1361         if ((aggr && txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) ||
1362                 (!aggr && txq->axq_depth >= ATH_NON_AGGR_MIN_QDEPTH)) {
1363                 *stop = true;
1364                 return false;
1365         }
1366
1367         ath_set_rates(tid->an->vif, tid->an->sta, bf);
1368         if (aggr)
1369                 last = ath_tx_form_aggr(sc, txq, tid, &bf_q, bf,
1370                                         tid_q, &aggr_len);
1371         else
1372                 ath_tx_form_burst(sc, txq, tid, &bf_q, bf, tid_q);
1373
1374         if (list_empty(&bf_q))
1375                 return false;
1376
1377         if (tid->ac->clear_ps_filter || tid->an->no_ps_filter) {
1378                 tid->ac->clear_ps_filter = false;
1379                 tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1380         }
1381
1382         ath_tx_fill_desc(sc, bf, txq, aggr_len);
1383         ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1384         return true;
1385 }
1386
1387 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
1388                       u16 tid, u16 *ssn)
1389 {
1390         struct ath_atx_tid *txtid;
1391         struct ath_txq *txq;
1392         struct ath_node *an;
1393         u8 density;
1394
1395         an = (struct ath_node *)sta->drv_priv;
1396         txtid = ATH_AN_2_TID(an, tid);
1397         txq = txtid->ac->txq;
1398
1399         ath_txq_lock(sc, txq);
1400
1401         /* update ampdu factor/density, they may have changed. This may happen
1402          * in HT IBSS when a beacon with HT-info is received after the station
1403          * has already been added.
1404          */
1405         if (sta->ht_cap.ht_supported) {
1406                 an->maxampdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1407                                       sta->ht_cap.ampdu_factor)) - 1;
1408                 density = ath9k_parse_mpdudensity(sta->ht_cap.ampdu_density);
1409                 an->mpdudensity = density;
1410         }
1411
1412         /* force sequence number allocation for pending frames */
1413         ath_tx_tid_change_state(sc, txtid);
1414
1415         txtid->active = true;
1416         *ssn = txtid->seq_start = txtid->seq_next;
1417         txtid->bar_index = -1;
1418
1419         memset(txtid->tx_buf, 0, sizeof(txtid->tx_buf));
1420         txtid->baw_head = txtid->baw_tail = 0;
1421
1422         ath_txq_unlock_complete(sc, txq);
1423
1424         return 0;
1425 }
1426
1427 void ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
1428 {
1429         struct ath_node *an = (struct ath_node *)sta->drv_priv;
1430         struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
1431         struct ath_txq *txq = txtid->ac->txq;
1432
1433         ath_txq_lock(sc, txq);
1434         txtid->active = false;
1435         ath_tx_flush_tid(sc, txtid);
1436         ath_tx_tid_change_state(sc, txtid);
1437         ath_txq_unlock_complete(sc, txq);
1438 }
1439
1440 void ath_tx_aggr_sleep(struct ieee80211_sta *sta, struct ath_softc *sc,
1441                        struct ath_node *an)
1442 {
1443         struct ath_atx_tid *tid;
1444         struct ath_atx_ac *ac;
1445         struct ath_txq *txq;
1446         bool buffered;
1447         int tidno;
1448
1449         for (tidno = 0, tid = &an->tid[tidno];
1450              tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
1451
1452                 ac = tid->ac;
1453                 txq = ac->txq;
1454
1455                 ath_txq_lock(sc, txq);
1456
1457                 if (!tid->sched) {
1458                         ath_txq_unlock(sc, txq);
1459                         continue;
1460                 }
1461
1462                 buffered = ath_tid_has_buffered(tid);
1463
1464                 tid->sched = false;
1465                 list_del(&tid->list);
1466
1467                 if (ac->sched) {
1468                         ac->sched = false;
1469                         list_del(&ac->list);
1470                 }
1471
1472                 ath_txq_unlock(sc, txq);
1473
1474                 ieee80211_sta_set_buffered(sta, tidno, buffered);
1475         }
1476 }
1477
1478 void ath_tx_aggr_wakeup(struct ath_softc *sc, struct ath_node *an)
1479 {
1480         struct ath_atx_tid *tid;
1481         struct ath_atx_ac *ac;
1482         struct ath_txq *txq;
1483         int tidno;
1484
1485         for (tidno = 0, tid = &an->tid[tidno];
1486              tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
1487
1488                 ac = tid->ac;
1489                 txq = ac->txq;
1490
1491                 ath_txq_lock(sc, txq);
1492                 ac->clear_ps_filter = true;
1493
1494                 if (ath_tid_has_buffered(tid)) {
1495                         ath_tx_queue_tid(sc, txq, tid);
1496                         ath_txq_schedule(sc, txq);
1497                 }
1498
1499                 ath_txq_unlock_complete(sc, txq);
1500         }
1501 }
1502
1503 void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta,
1504                         u16 tidno)
1505 {
1506         struct ath_atx_tid *tid;
1507         struct ath_node *an;
1508         struct ath_txq *txq;
1509
1510         an = (struct ath_node *)sta->drv_priv;
1511         tid = ATH_AN_2_TID(an, tidno);
1512         txq = tid->ac->txq;
1513
1514         ath_txq_lock(sc, txq);
1515
1516         tid->baw_size = IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
1517
1518         if (ath_tid_has_buffered(tid)) {
1519                 ath_tx_queue_tid(sc, txq, tid);
1520                 ath_txq_schedule(sc, txq);
1521         }
1522
1523         ath_txq_unlock_complete(sc, txq);
1524 }
1525
1526 void ath9k_release_buffered_frames(struct ieee80211_hw *hw,
1527                                    struct ieee80211_sta *sta,
1528                                    u16 tids, int nframes,
1529                                    enum ieee80211_frame_release_type reason,
1530                                    bool more_data)
1531 {
1532         struct ath_softc *sc = hw->priv;
1533         struct ath_node *an = (struct ath_node *)sta->drv_priv;
1534         struct ath_txq *txq = sc->tx.uapsdq;
1535         struct ieee80211_tx_info *info;
1536         struct list_head bf_q;
1537         struct ath_buf *bf_tail = NULL, *bf;
1538         struct sk_buff_head *tid_q;
1539         int sent = 0;
1540         int i;
1541
1542         INIT_LIST_HEAD(&bf_q);
1543         for (i = 0; tids && nframes; i++, tids >>= 1) {
1544                 struct ath_atx_tid *tid;
1545
1546                 if (!(tids & 1))
1547                         continue;
1548
1549                 tid = ATH_AN_2_TID(an, i);
1550
1551                 ath_txq_lock(sc, tid->ac->txq);
1552                 while (nframes > 0) {
1553                         bf = ath_tx_get_tid_subframe(sc, sc->tx.uapsdq, tid, &tid_q);
1554                         if (!bf)
1555                                 break;
1556
1557                         __skb_unlink(bf->bf_mpdu, tid_q);
1558                         list_add_tail(&bf->list, &bf_q);
1559                         ath_set_rates(tid->an->vif, tid->an->sta, bf);
1560                         if (bf_isampdu(bf)) {
1561                                 ath_tx_addto_baw(sc, tid, bf);
1562                                 bf->bf_state.bf_type &= ~BUF_AGGR;
1563                         }
1564                         if (bf_tail)
1565                                 bf_tail->bf_next = bf;
1566
1567                         bf_tail = bf;
1568                         nframes--;
1569                         sent++;
1570                         TX_STAT_INC(txq->axq_qnum, a_queued_hw);
1571
1572                         if (an->sta && !ath_tid_has_buffered(tid))
1573                                 ieee80211_sta_set_buffered(an->sta, i, false);
1574                 }
1575                 ath_txq_unlock_complete(sc, tid->ac->txq);
1576         }
1577
1578         if (list_empty(&bf_q))
1579                 return;
1580
1581         info = IEEE80211_SKB_CB(bf_tail->bf_mpdu);
1582         info->flags |= IEEE80211_TX_STATUS_EOSP;
1583
1584         bf = list_first_entry(&bf_q, struct ath_buf, list);
1585         ath_txq_lock(sc, txq);
1586         ath_tx_fill_desc(sc, bf, txq, 0);
1587         ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1588         ath_txq_unlock(sc, txq);
1589 }
1590
1591 /********************/
1592 /* Queue Management */
1593 /********************/
1594
1595 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1596 {
1597         struct ath_hw *ah = sc->sc_ah;
1598         struct ath9k_tx_queue_info qi;
1599         static const int subtype_txq_to_hwq[] = {
1600                 [IEEE80211_AC_BE] = ATH_TXQ_AC_BE,
1601                 [IEEE80211_AC_BK] = ATH_TXQ_AC_BK,
1602                 [IEEE80211_AC_VI] = ATH_TXQ_AC_VI,
1603                 [IEEE80211_AC_VO] = ATH_TXQ_AC_VO,
1604         };
1605         int axq_qnum, i;
1606
1607         memset(&qi, 0, sizeof(qi));
1608         qi.tqi_subtype = subtype_txq_to_hwq[subtype];
1609         qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1610         qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1611         qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1612         qi.tqi_physCompBuf = 0;
1613
1614         /*
1615          * Enable interrupts only for EOL and DESC conditions.
1616          * We mark tx descriptors to receive a DESC interrupt
1617          * when a tx queue gets deep; otherwise waiting for the
1618          * EOL to reap descriptors.  Note that this is done to
1619          * reduce interrupt load and this only defers reaping
1620          * descriptors, never transmitting frames.  Aside from
1621          * reducing interrupts this also permits more concurrency.
1622          * The only potential downside is if the tx queue backs
1623          * up in which case the top half of the kernel may backup
1624          * due to a lack of tx descriptors.
1625          *
1626          * The UAPSD queue is an exception, since we take a desc-
1627          * based intr on the EOSP frames.
1628          */
1629         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1630                 qi.tqi_qflags = TXQ_FLAG_TXINT_ENABLE;
1631         } else {
1632                 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1633                         qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1634                 else
1635                         qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1636                                         TXQ_FLAG_TXDESCINT_ENABLE;
1637         }
1638         axq_qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1639         if (axq_qnum == -1) {
1640                 /*
1641                  * NB: don't print a message, this happens
1642                  * normally on parts with too few tx queues
1643                  */
1644                 return NULL;
1645         }
1646         if (!ATH_TXQ_SETUP(sc, axq_qnum)) {
1647                 struct ath_txq *txq = &sc->tx.txq[axq_qnum];
1648
1649                 txq->axq_qnum = axq_qnum;
1650                 txq->mac80211_qnum = -1;
1651                 txq->axq_link = NULL;
1652                 __skb_queue_head_init(&txq->complete_q);
1653                 INIT_LIST_HEAD(&txq->axq_q);
1654                 spin_lock_init(&txq->axq_lock);
1655                 txq->axq_depth = 0;
1656                 txq->axq_ampdu_depth = 0;
1657                 txq->axq_tx_inprogress = false;
1658                 sc->tx.txqsetup |= 1<<axq_qnum;
1659
1660                 txq->txq_headidx = txq->txq_tailidx = 0;
1661                 for (i = 0; i < ATH_TXFIFO_DEPTH; i++)
1662                         INIT_LIST_HEAD(&txq->txq_fifo[i]);
1663         }
1664         return &sc->tx.txq[axq_qnum];
1665 }
1666
1667 int ath_txq_update(struct ath_softc *sc, int qnum,
1668                    struct ath9k_tx_queue_info *qinfo)
1669 {
1670         struct ath_hw *ah = sc->sc_ah;
1671         int error = 0;
1672         struct ath9k_tx_queue_info qi;
1673
1674         BUG_ON(sc->tx.txq[qnum].axq_qnum != qnum);
1675
1676         ath9k_hw_get_txq_props(ah, qnum, &qi);
1677         qi.tqi_aifs = qinfo->tqi_aifs;
1678         qi.tqi_cwmin = qinfo->tqi_cwmin;
1679         qi.tqi_cwmax = qinfo->tqi_cwmax;
1680         qi.tqi_burstTime = qinfo->tqi_burstTime;
1681         qi.tqi_readyTime = qinfo->tqi_readyTime;
1682
1683         if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
1684                 ath_err(ath9k_hw_common(sc->sc_ah),
1685                         "Unable to update hardware queue %u!\n", qnum);
1686                 error = -EIO;
1687         } else {
1688                 ath9k_hw_resettxqueue(ah, qnum);
1689         }
1690
1691         return error;
1692 }
1693
1694 int ath_cabq_update(struct ath_softc *sc)
1695 {
1696         struct ath9k_tx_queue_info qi;
1697         struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
1698         int qnum = sc->beacon.cabq->axq_qnum;
1699
1700         ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
1701
1702         qi.tqi_readyTime = (TU_TO_USEC(cur_conf->beacon_interval) *
1703                             ATH_CABQ_READY_TIME) / 100;
1704         ath_txq_update(sc, qnum, &qi);
1705
1706         return 0;
1707 }
1708
1709 static void ath_drain_txq_list(struct ath_softc *sc, struct ath_txq *txq,
1710                                struct list_head *list)
1711 {
1712         struct ath_buf *bf, *lastbf;
1713         struct list_head bf_head;
1714         struct ath_tx_status ts;
1715
1716         memset(&ts, 0, sizeof(ts));
1717         ts.ts_status = ATH9K_TX_FLUSH;
1718         INIT_LIST_HEAD(&bf_head);
1719
1720         while (!list_empty(list)) {
1721                 bf = list_first_entry(list, struct ath_buf, list);
1722
1723                 if (bf->bf_state.stale) {
1724                         list_del(&bf->list);
1725
1726                         ath_tx_return_buffer(sc, bf);
1727                         continue;
1728                 }
1729
1730                 lastbf = bf->bf_lastbf;
1731                 list_cut_position(&bf_head, list, &lastbf->list);
1732                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
1733         }
1734 }
1735
1736 /*
1737  * Drain a given TX queue (could be Beacon or Data)
1738  *
1739  * This assumes output has been stopped and
1740  * we do not need to block ath_tx_tasklet.
1741  */
1742 void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq)
1743 {
1744         ath_txq_lock(sc, txq);
1745
1746         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1747                 int idx = txq->txq_tailidx;
1748
1749                 while (!list_empty(&txq->txq_fifo[idx])) {
1750                         ath_drain_txq_list(sc, txq, &txq->txq_fifo[idx]);
1751
1752                         INCR(idx, ATH_TXFIFO_DEPTH);
1753                 }
1754                 txq->txq_tailidx = idx;
1755         }
1756
1757         txq->axq_link = NULL;
1758         txq->axq_tx_inprogress = false;
1759         ath_drain_txq_list(sc, txq, &txq->axq_q);
1760
1761         ath_txq_unlock_complete(sc, txq);
1762 }
1763
1764 bool ath_drain_all_txq(struct ath_softc *sc)
1765 {
1766         struct ath_hw *ah = sc->sc_ah;
1767         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1768         struct ath_txq *txq;
1769         int i;
1770         u32 npend = 0;
1771
1772         if (test_bit(ATH_OP_INVALID, &common->op_flags))
1773                 return true;
1774
1775         ath9k_hw_abort_tx_dma(ah);
1776
1777         /* Check if any queue remains active */
1778         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1779                 if (!ATH_TXQ_SETUP(sc, i))
1780                         continue;
1781
1782                 if (!sc->tx.txq[i].axq_depth)
1783                         continue;
1784
1785                 if (ath9k_hw_numtxpending(ah, sc->tx.txq[i].axq_qnum))
1786                         npend |= BIT(i);
1787         }
1788
1789         if (npend)
1790                 ath_err(common, "Failed to stop TX DMA, queues=0x%03x!\n", npend);
1791
1792         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1793                 if (!ATH_TXQ_SETUP(sc, i))
1794                         continue;
1795
1796                 /*
1797                  * The caller will resume queues with ieee80211_wake_queues.
1798                  * Mark the queue as not stopped to prevent ath_tx_complete
1799                  * from waking the queue too early.
1800                  */
1801                 txq = &sc->tx.txq[i];
1802                 txq->stopped = false;
1803                 ath_draintxq(sc, txq);
1804         }
1805
1806         return !npend;
1807 }
1808
1809 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1810 {
1811         ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1812         sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1813 }
1814
1815 /* For each acq entry, for each tid, try to schedule packets
1816  * for transmit until ampdu_depth has reached min Q depth.
1817  */
1818 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
1819 {
1820         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1821         struct ath_atx_ac *ac, *last_ac;
1822         struct ath_atx_tid *tid, *last_tid;
1823         struct list_head *ac_list;
1824         bool sent = false;
1825
1826         if (txq->mac80211_qnum < 0)
1827                 return;
1828
1829         spin_lock_bh(&sc->chan_lock);
1830         ac_list = &sc->cur_chan->acq[txq->mac80211_qnum];
1831         spin_unlock_bh(&sc->chan_lock);
1832
1833         if (test_bit(ATH_OP_HW_RESET, &common->op_flags) ||
1834             list_empty(ac_list))
1835                 return;
1836
1837         spin_lock_bh(&sc->chan_lock);
1838         rcu_read_lock();
1839
1840         last_ac = list_entry(ac_list->prev, struct ath_atx_ac, list);
1841         while (!list_empty(ac_list)) {
1842                 bool stop = false;
1843
1844                 if (sc->cur_chan->stopped)
1845                         break;
1846
1847                 ac = list_first_entry(ac_list, struct ath_atx_ac, list);
1848                 last_tid = list_entry(ac->tid_q.prev, struct ath_atx_tid, list);
1849                 list_del(&ac->list);
1850                 ac->sched = false;
1851
1852                 while (!list_empty(&ac->tid_q)) {
1853
1854                         tid = list_first_entry(&ac->tid_q, struct ath_atx_tid,
1855                                                list);
1856                         list_del(&tid->list);
1857                         tid->sched = false;
1858
1859                         if (ath_tx_sched_aggr(sc, txq, tid, &stop))
1860                                 sent = true;
1861
1862                         /*
1863                          * add tid to round-robin queue if more frames
1864                          * are pending for the tid
1865                          */
1866                         if (ath_tid_has_buffered(tid))
1867                                 ath_tx_queue_tid(sc, txq, tid);
1868
1869                         if (stop || tid == last_tid)
1870                                 break;
1871                 }
1872
1873                 if (!list_empty(&ac->tid_q) && !ac->sched) {
1874                         ac->sched = true;
1875                         list_add_tail(&ac->list, ac_list);
1876                 }
1877
1878                 if (stop)
1879                         break;
1880
1881                 if (ac == last_ac) {
1882                         if (!sent)
1883                                 break;
1884
1885                         sent = false;
1886                         last_ac = list_entry(ac_list->prev,
1887                                              struct ath_atx_ac, list);
1888                 }
1889         }
1890
1891         rcu_read_unlock();
1892         spin_unlock_bh(&sc->chan_lock);
1893 }
1894
1895 void ath_txq_schedule_all(struct ath_softc *sc)
1896 {
1897         struct ath_txq *txq;
1898         int i;
1899
1900         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
1901                 txq = sc->tx.txq_map[i];
1902
1903                 spin_lock_bh(&txq->axq_lock);
1904                 ath_txq_schedule(sc, txq);
1905                 spin_unlock_bh(&txq->axq_lock);
1906         }
1907 }
1908
1909 /***********/
1910 /* TX, DMA */
1911 /***********/
1912
1913 /*
1914  * Insert a chain of ath_buf (descriptors) on a txq and
1915  * assume the descriptors are already chained together by caller.
1916  */
1917 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
1918                              struct list_head *head, bool internal)
1919 {
1920         struct ath_hw *ah = sc->sc_ah;
1921         struct ath_common *common = ath9k_hw_common(ah);
1922         struct ath_buf *bf, *bf_last;
1923         bool puttxbuf = false;
1924         bool edma;
1925
1926         /*
1927          * Insert the frame on the outbound list and
1928          * pass it on to the hardware.
1929          */
1930
1931         if (list_empty(head))
1932                 return;
1933
1934         edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1935         bf = list_first_entry(head, struct ath_buf, list);
1936         bf_last = list_entry(head->prev, struct ath_buf, list);
1937
1938         ath_dbg(common, QUEUE, "qnum: %d, txq depth: %d\n",
1939                 txq->axq_qnum, txq->axq_depth);
1940
1941         if (edma && list_empty(&txq->txq_fifo[txq->txq_headidx])) {
1942                 list_splice_tail_init(head, &txq->txq_fifo[txq->txq_headidx]);
1943                 INCR(txq->txq_headidx, ATH_TXFIFO_DEPTH);
1944                 puttxbuf = true;
1945         } else {
1946                 list_splice_tail_init(head, &txq->axq_q);
1947
1948                 if (txq->axq_link) {
1949                         ath9k_hw_set_desc_link(ah, txq->axq_link, bf->bf_daddr);
1950                         ath_dbg(common, XMIT, "link[%u] (%p)=%llx (%p)\n",
1951                                 txq->axq_qnum, txq->axq_link,
1952                                 ito64(bf->bf_daddr), bf->bf_desc);
1953                 } else if (!edma)
1954                         puttxbuf = true;
1955
1956                 txq->axq_link = bf_last->bf_desc;
1957         }
1958
1959         if (puttxbuf) {
1960                 TX_STAT_INC(txq->axq_qnum, puttxbuf);
1961                 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
1962                 ath_dbg(common, XMIT, "TXDP[%u] = %llx (%p)\n",
1963                         txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
1964         }
1965
1966         if (!edma || sc->tx99_state) {
1967                 TX_STAT_INC(txq->axq_qnum, txstart);
1968                 ath9k_hw_txstart(ah, txq->axq_qnum);
1969         }
1970
1971         if (!internal) {
1972                 while (bf) {
1973                         txq->axq_depth++;
1974                         if (bf_is_ampdu_not_probing(bf))
1975                                 txq->axq_ampdu_depth++;
1976
1977                         bf_last = bf->bf_lastbf;
1978                         bf = bf_last->bf_next;
1979                         bf_last->bf_next = NULL;
1980                 }
1981         }
1982 }
1983
1984 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
1985                                struct ath_atx_tid *tid, struct sk_buff *skb)
1986 {
1987         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1988         struct ath_frame_info *fi = get_frame_info(skb);
1989         struct list_head bf_head;
1990         struct ath_buf *bf = fi->bf;
1991
1992         INIT_LIST_HEAD(&bf_head);
1993         list_add_tail(&bf->list, &bf_head);
1994         bf->bf_state.bf_type = 0;
1995         if (tid && (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
1996                 bf->bf_state.bf_type = BUF_AMPDU;
1997                 ath_tx_addto_baw(sc, tid, bf);
1998         }
1999
2000         bf->bf_next = NULL;
2001         bf->bf_lastbf = bf;
2002         ath_tx_fill_desc(sc, bf, txq, fi->framelen);
2003         ath_tx_txqaddbuf(sc, txq, &bf_head, false);
2004         TX_STAT_INC(txq->axq_qnum, queued);
2005 }
2006
2007 static void setup_frame_info(struct ieee80211_hw *hw,
2008                              struct ieee80211_sta *sta,
2009                              struct sk_buff *skb,
2010                              int framelen)
2011 {
2012         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2013         struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
2014         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2015         const struct ieee80211_rate *rate;
2016         struct ath_frame_info *fi = get_frame_info(skb);
2017         struct ath_node *an = NULL;
2018         enum ath9k_key_type keytype;
2019         bool short_preamble = false;
2020
2021         /*
2022          * We check if Short Preamble is needed for the CTS rate by
2023          * checking the BSS's global flag.
2024          * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
2025          */
2026         if (tx_info->control.vif &&
2027             tx_info->control.vif->bss_conf.use_short_preamble)
2028                 short_preamble = true;
2029
2030         rate = ieee80211_get_rts_cts_rate(hw, tx_info);
2031         keytype = ath9k_cmn_get_hw_crypto_keytype(skb);
2032
2033         if (sta)
2034                 an = (struct ath_node *) sta->drv_priv;
2035
2036         memset(fi, 0, sizeof(*fi));
2037         if (hw_key)
2038                 fi->keyix = hw_key->hw_key_idx;
2039         else if (an && ieee80211_is_data(hdr->frame_control) && an->ps_key > 0)
2040                 fi->keyix = an->ps_key;
2041         else
2042                 fi->keyix = ATH9K_TXKEYIX_INVALID;
2043         fi->keytype = keytype;
2044         fi->framelen = framelen;
2045
2046         if (!rate)
2047                 return;
2048         fi->rtscts_rate = rate->hw_value;
2049         if (short_preamble)
2050                 fi->rtscts_rate |= rate->hw_value_short;
2051 }
2052
2053 u8 ath_txchainmask_reduction(struct ath_softc *sc, u8 chainmask, u32 rate)
2054 {
2055         struct ath_hw *ah = sc->sc_ah;
2056         struct ath9k_channel *curchan = ah->curchan;
2057
2058         if ((ah->caps.hw_caps & ATH9K_HW_CAP_APM) && IS_CHAN_5GHZ(curchan) &&
2059             (chainmask == 0x7) && (rate < 0x90))
2060                 return 0x3;
2061         else if (AR_SREV_9462(ah) && ath9k_hw_btcoex_is_enabled(ah) &&
2062                  IS_CCK_RATE(rate))
2063                 return 0x2;
2064         else
2065                 return chainmask;
2066 }
2067
2068 /*
2069  * Assign a descriptor (and sequence number if necessary,
2070  * and map buffer for DMA. Frees skb on error
2071  */
2072 static struct ath_buf *ath_tx_setup_buffer(struct ath_softc *sc,
2073                                            struct ath_txq *txq,
2074                                            struct ath_atx_tid *tid,
2075                                            struct sk_buff *skb)
2076 {
2077         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2078         struct ath_frame_info *fi = get_frame_info(skb);
2079         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2080         struct ath_buf *bf;
2081         int fragno;
2082         u16 seqno;
2083
2084         bf = ath_tx_get_buffer(sc);
2085         if (!bf) {
2086                 ath_dbg(common, XMIT, "TX buffers are full\n");
2087                 return NULL;
2088         }
2089
2090         ATH_TXBUF_RESET(bf);
2091
2092         if (tid && ieee80211_is_data_present(hdr->frame_control)) {
2093                 fragno = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
2094                 seqno = tid->seq_next;
2095                 hdr->seq_ctrl = cpu_to_le16(tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
2096
2097                 if (fragno)
2098                         hdr->seq_ctrl |= cpu_to_le16(fragno);
2099
2100                 if (!ieee80211_has_morefrags(hdr->frame_control))
2101                         INCR(tid->seq_next, IEEE80211_SEQ_MAX);
2102
2103                 bf->bf_state.seqno = seqno;
2104         }
2105
2106         bf->bf_mpdu = skb;
2107
2108         bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
2109                                          skb->len, DMA_TO_DEVICE);
2110         if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
2111                 bf->bf_mpdu = NULL;
2112                 bf->bf_buf_addr = 0;
2113                 ath_err(ath9k_hw_common(sc->sc_ah),
2114                         "dma_mapping_error() on TX\n");
2115                 ath_tx_return_buffer(sc, bf);
2116                 return NULL;
2117         }
2118
2119         fi->bf = bf;
2120
2121         return bf;
2122 }
2123
2124 static int ath_tx_prepare(struct ieee80211_hw *hw, struct sk_buff *skb,
2125                           struct ath_tx_control *txctl)
2126 {
2127         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2128         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2129         struct ieee80211_sta *sta = txctl->sta;
2130         struct ieee80211_vif *vif = info->control.vif;
2131         struct ath_vif *avp;
2132         struct ath_softc *sc = hw->priv;
2133         int frmlen = skb->len + FCS_LEN;
2134         int padpos, padsize;
2135
2136         /* NOTE:  sta can be NULL according to net/mac80211.h */
2137         if (sta)
2138                 txctl->an = (struct ath_node *)sta->drv_priv;
2139         else if (vif && ieee80211_is_data(hdr->frame_control)) {
2140                 avp = (void *)vif->drv_priv;
2141                 txctl->an = &avp->mcast_node;
2142         }
2143
2144         if (info->control.hw_key)
2145                 frmlen += info->control.hw_key->icv_len;
2146
2147         /*
2148          * As a temporary workaround, assign seq# here; this will likely need
2149          * to be cleaned up to work better with Beacon transmission and virtual
2150          * BSSes.
2151          */
2152         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2153                 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2154                         sc->tx.seq_no += 0x10;
2155                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2156                 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
2157         }
2158
2159         if ((vif && vif->type != NL80211_IFTYPE_AP &&
2160                     vif->type != NL80211_IFTYPE_AP_VLAN) ||
2161             !ieee80211_is_data(hdr->frame_control))
2162                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
2163
2164         /* Add the padding after the header if this is not already done */
2165         padpos = ieee80211_hdrlen(hdr->frame_control);
2166         padsize = padpos & 3;
2167         if (padsize && skb->len > padpos) {
2168                 if (skb_headroom(skb) < padsize)
2169                         return -ENOMEM;
2170
2171                 skb_push(skb, padsize);
2172                 memmove(skb->data, skb->data + padsize, padpos);
2173         }
2174
2175         setup_frame_info(hw, sta, skb, frmlen);
2176         return 0;
2177 }
2178
2179
2180 /* Upon failure caller should free skb */
2181 int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
2182                  struct ath_tx_control *txctl)
2183 {
2184         struct ieee80211_hdr *hdr;
2185         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2186         struct ieee80211_sta *sta = txctl->sta;
2187         struct ieee80211_vif *vif = info->control.vif;
2188         struct ath_softc *sc = hw->priv;
2189         struct ath_txq *txq = txctl->txq;
2190         struct ath_atx_tid *tid = NULL;
2191         struct ath_buf *bf;
2192         int q;
2193         int ret;
2194
2195         ret = ath_tx_prepare(hw, skb, txctl);
2196         if (ret)
2197             return ret;
2198
2199         hdr = (struct ieee80211_hdr *) skb->data;
2200         /*
2201          * At this point, the vif, hw_key and sta pointers in the tx control
2202          * info are no longer valid (overwritten by the ath_frame_info data.
2203          */
2204
2205         q = skb_get_queue_mapping(skb);
2206
2207         ath_txq_lock(sc, txq);
2208         if (txq == sc->tx.txq_map[q] &&
2209             ++txq->pending_frames > sc->tx.txq_max_pending[q] &&
2210             !txq->stopped) {
2211                 ieee80211_stop_queue(sc->hw, q);
2212                 txq->stopped = true;
2213         }
2214
2215         if (txctl->an && ieee80211_is_data_present(hdr->frame_control))
2216                 tid = ath_get_skb_tid(sc, txctl->an, skb);
2217
2218         if (info->flags & IEEE80211_TX_CTL_PS_RESPONSE) {
2219                 ath_txq_unlock(sc, txq);
2220                 txq = sc->tx.uapsdq;
2221                 ath_txq_lock(sc, txq);
2222         } else if (txctl->an &&
2223                    ieee80211_is_data_present(hdr->frame_control)) {
2224                 WARN_ON(tid->ac->txq != txctl->txq);
2225
2226                 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
2227                         tid->ac->clear_ps_filter = true;
2228
2229                 /*
2230                  * Add this frame to software queue for scheduling later
2231                  * for aggregation.
2232                  */
2233                 TX_STAT_INC(txq->axq_qnum, a_queued_sw);
2234                 __skb_queue_tail(&tid->buf_q, skb);
2235                 if (!txctl->an->sleeping)
2236                         ath_tx_queue_tid(sc, txq, tid);
2237
2238                 ath_txq_schedule(sc, txq);
2239                 goto out;
2240         }
2241
2242         bf = ath_tx_setup_buffer(sc, txq, tid, skb);
2243         if (!bf) {
2244                 ath_txq_skb_done(sc, txq, skb);
2245                 if (txctl->paprd)
2246                         dev_kfree_skb_any(skb);
2247                 else
2248                         ieee80211_free_txskb(sc->hw, skb);
2249                 goto out;
2250         }
2251
2252         bf->bf_state.bfs_paprd = txctl->paprd;
2253
2254         if (txctl->paprd)
2255                 bf->bf_state.bfs_paprd_timestamp = jiffies;
2256
2257         ath_set_rates(vif, sta, bf);
2258         ath_tx_send_normal(sc, txq, tid, skb);
2259
2260 out:
2261         ath_txq_unlock(sc, txq);
2262
2263         return 0;
2264 }
2265
2266 void ath_tx_cabq(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2267                  struct sk_buff *skb)
2268 {
2269         struct ath_softc *sc = hw->priv;
2270         struct ath_tx_control txctl = {
2271                 .txq = sc->beacon.cabq
2272         };
2273         struct ath_tx_info info = {};
2274         struct ieee80211_hdr *hdr;
2275         struct ath_buf *bf_tail = NULL;
2276         struct ath_buf *bf;
2277         LIST_HEAD(bf_q);
2278         int duration = 0;
2279         int max_duration;
2280
2281         max_duration =
2282                 sc->cur_beacon_conf.beacon_interval * 1000 *
2283                 sc->cur_beacon_conf.dtim_period / ATH_BCBUF;
2284
2285         do {
2286                 struct ath_frame_info *fi = get_frame_info(skb);
2287
2288                 if (ath_tx_prepare(hw, skb, &txctl))
2289                         break;
2290
2291                 bf = ath_tx_setup_buffer(sc, txctl.txq, NULL, skb);
2292                 if (!bf)
2293                         break;
2294
2295                 bf->bf_lastbf = bf;
2296                 ath_set_rates(vif, NULL, bf);
2297                 ath_buf_set_rate(sc, bf, &info, fi->framelen, false);
2298                 duration += info.rates[0].PktDuration;
2299                 if (bf_tail)
2300                         bf_tail->bf_next = bf;
2301
2302                 list_add_tail(&bf->list, &bf_q);
2303                 bf_tail = bf;
2304                 skb = NULL;
2305
2306                 if (duration > max_duration)
2307                         break;
2308
2309                 skb = ieee80211_get_buffered_bc(hw, vif);
2310         } while(skb);
2311
2312         if (skb)
2313                 ieee80211_free_txskb(hw, skb);
2314
2315         if (list_empty(&bf_q))
2316                 return;
2317
2318         bf = list_first_entry(&bf_q, struct ath_buf, list);
2319         hdr = (struct ieee80211_hdr *) bf->bf_mpdu->data;
2320
2321         if (hdr->frame_control & IEEE80211_FCTL_MOREDATA) {
2322                 hdr->frame_control &= ~IEEE80211_FCTL_MOREDATA;
2323                 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
2324                         sizeof(*hdr), DMA_TO_DEVICE);
2325         }
2326
2327         ath_txq_lock(sc, txctl.txq);
2328         ath_tx_fill_desc(sc, bf, txctl.txq, 0);
2329         ath_tx_txqaddbuf(sc, txctl.txq, &bf_q, false);
2330         TX_STAT_INC(txctl.txq->axq_qnum, queued);
2331         ath_txq_unlock(sc, txctl.txq);
2332 }
2333
2334 /*****************/
2335 /* TX Completion */
2336 /*****************/
2337
2338 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
2339                             int tx_flags, struct ath_txq *txq)
2340 {
2341         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2342         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2343         struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
2344         int padpos, padsize;
2345         unsigned long flags;
2346
2347         ath_dbg(common, XMIT, "TX complete: skb: %p\n", skb);
2348
2349         if (sc->sc_ah->caldata)
2350                 set_bit(PAPRD_PACKET_SENT, &sc->sc_ah->caldata->cal_flags);
2351
2352         if (!(tx_flags & ATH_TX_ERROR))
2353                 /* Frame was ACKed */
2354                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
2355
2356         padpos = ieee80211_hdrlen(hdr->frame_control);
2357         padsize = padpos & 3;
2358         if (padsize && skb->len>padpos+padsize) {
2359                 /*
2360                  * Remove MAC header padding before giving the frame back to
2361                  * mac80211.
2362                  */
2363                 memmove(skb->data + padsize, skb->data, padpos);
2364                 skb_pull(skb, padsize);
2365         }
2366
2367         spin_lock_irqsave(&sc->sc_pm_lock, flags);
2368         if ((sc->ps_flags & PS_WAIT_FOR_TX_ACK) && !txq->axq_depth) {
2369                 sc->ps_flags &= ~PS_WAIT_FOR_TX_ACK;
2370                 ath_dbg(common, PS,
2371                         "Going back to sleep after having received TX status (0x%lx)\n",
2372                         sc->ps_flags & (PS_WAIT_FOR_BEACON |
2373                                         PS_WAIT_FOR_CAB |
2374                                         PS_WAIT_FOR_PSPOLL_DATA |
2375                                         PS_WAIT_FOR_TX_ACK));
2376         }
2377         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
2378
2379         __skb_queue_tail(&txq->complete_q, skb);
2380         ath_txq_skb_done(sc, txq, skb);
2381 }
2382
2383 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
2384                                 struct ath_txq *txq, struct list_head *bf_q,
2385                                 struct ath_tx_status *ts, int txok)
2386 {
2387         struct sk_buff *skb = bf->bf_mpdu;
2388         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2389         unsigned long flags;
2390         int tx_flags = 0;
2391
2392         if (!txok)
2393                 tx_flags |= ATH_TX_ERROR;
2394
2395         if (ts->ts_status & ATH9K_TXERR_FILT)
2396                 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
2397
2398         dma_unmap_single(sc->dev, bf->bf_buf_addr, skb->len, DMA_TO_DEVICE);
2399         bf->bf_buf_addr = 0;
2400         if (sc->tx99_state)
2401                 goto skip_tx_complete;
2402
2403         if (bf->bf_state.bfs_paprd) {
2404                 if (time_after(jiffies,
2405                                 bf->bf_state.bfs_paprd_timestamp +
2406                                 msecs_to_jiffies(ATH_PAPRD_TIMEOUT)))
2407                         dev_kfree_skb_any(skb);
2408                 else
2409                         complete(&sc->paprd_complete);
2410         } else {
2411                 ath_debug_stat_tx(sc, bf, ts, txq, tx_flags);
2412                 ath_tx_complete(sc, skb, tx_flags, txq);
2413         }
2414 skip_tx_complete:
2415         /* At this point, skb (bf->bf_mpdu) is consumed...make sure we don't
2416          * accidentally reference it later.
2417          */
2418         bf->bf_mpdu = NULL;
2419
2420         /*
2421          * Return the list of ath_buf of this mpdu to free queue
2422          */
2423         spin_lock_irqsave(&sc->tx.txbuflock, flags);
2424         list_splice_tail_init(bf_q, &sc->tx.txbuf);
2425         spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
2426 }
2427
2428 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
2429                              struct ath_tx_status *ts, int nframes, int nbad,
2430                              int txok)
2431 {
2432         struct sk_buff *skb = bf->bf_mpdu;
2433         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2434         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2435         struct ieee80211_hw *hw = sc->hw;
2436         struct ath_hw *ah = sc->sc_ah;
2437         u8 i, tx_rateindex;
2438
2439         if (txok)
2440                 tx_info->status.ack_signal = ts->ts_rssi;
2441
2442         tx_rateindex = ts->ts_rateindex;
2443         WARN_ON(tx_rateindex >= hw->max_rates);
2444
2445         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
2446                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
2447
2448                 BUG_ON(nbad > nframes);
2449         }
2450         tx_info->status.ampdu_len = nframes;
2451         tx_info->status.ampdu_ack_len = nframes - nbad;
2452
2453         if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
2454             (tx_info->flags & IEEE80211_TX_CTL_NO_ACK) == 0) {
2455                 /*
2456                  * If an underrun error is seen assume it as an excessive
2457                  * retry only if max frame trigger level has been reached
2458                  * (2 KB for single stream, and 4 KB for dual stream).
2459                  * Adjust the long retry as if the frame was tried
2460                  * hw->max_rate_tries times to affect how rate control updates
2461                  * PER for the failed rate.
2462                  * In case of congestion on the bus penalizing this type of
2463                  * underruns should help hardware actually transmit new frames
2464                  * successfully by eventually preferring slower rates.
2465                  * This itself should also alleviate congestion on the bus.
2466                  */
2467                 if (unlikely(ts->ts_flags & (ATH9K_TX_DATA_UNDERRUN |
2468                                              ATH9K_TX_DELIM_UNDERRUN)) &&
2469                     ieee80211_is_data(hdr->frame_control) &&
2470                     ah->tx_trig_level >= sc->sc_ah->config.max_txtrig_level)
2471                         tx_info->status.rates[tx_rateindex].count =
2472                                 hw->max_rate_tries;
2473         }
2474
2475         for (i = tx_rateindex + 1; i < hw->max_rates; i++) {
2476                 tx_info->status.rates[i].count = 0;
2477                 tx_info->status.rates[i].idx = -1;
2478         }
2479
2480         tx_info->status.rates[tx_rateindex].count = ts->ts_longretry + 1;
2481 }
2482
2483 static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
2484 {
2485         struct ath_hw *ah = sc->sc_ah;
2486         struct ath_common *common = ath9k_hw_common(ah);
2487         struct ath_buf *bf, *lastbf, *bf_held = NULL;
2488         struct list_head bf_head;
2489         struct ath_desc *ds;
2490         struct ath_tx_status ts;
2491         int status;
2492
2493         ath_dbg(common, QUEUE, "tx queue %d (%x), link %p\n",
2494                 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
2495                 txq->axq_link);
2496
2497         ath_txq_lock(sc, txq);
2498         for (;;) {
2499                 if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
2500                         break;
2501
2502                 if (list_empty(&txq->axq_q)) {
2503                         txq->axq_link = NULL;
2504                         ath_txq_schedule(sc, txq);
2505                         break;
2506                 }
2507                 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2508
2509                 /*
2510                  * There is a race condition that a BH gets scheduled
2511                  * after sw writes TxE and before hw re-load the last
2512                  * descriptor to get the newly chained one.
2513                  * Software must keep the last DONE descriptor as a
2514                  * holding descriptor - software does so by marking
2515                  * it with the STALE flag.
2516                  */
2517                 bf_held = NULL;
2518                 if (bf->bf_state.stale) {
2519                         bf_held = bf;
2520                         if (list_is_last(&bf_held->list, &txq->axq_q))
2521                                 break;
2522
2523                         bf = list_entry(bf_held->list.next, struct ath_buf,
2524                                         list);
2525                 }
2526
2527                 lastbf = bf->bf_lastbf;
2528                 ds = lastbf->bf_desc;
2529
2530                 memset(&ts, 0, sizeof(ts));
2531                 status = ath9k_hw_txprocdesc(ah, ds, &ts);
2532                 if (status == -EINPROGRESS)
2533                         break;
2534
2535                 TX_STAT_INC(txq->axq_qnum, txprocdesc);
2536
2537                 /*
2538                  * Remove ath_buf's of the same transmit unit from txq,
2539                  * however leave the last descriptor back as the holding
2540                  * descriptor for hw.
2541                  */
2542                 lastbf->bf_state.stale = true;
2543                 INIT_LIST_HEAD(&bf_head);
2544                 if (!list_is_singular(&lastbf->list))
2545                         list_cut_position(&bf_head,
2546                                 &txq->axq_q, lastbf->list.prev);
2547
2548                 if (bf_held) {
2549                         list_del(&bf_held->list);
2550                         ath_tx_return_buffer(sc, bf_held);
2551                 }
2552
2553                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2554         }
2555         ath_txq_unlock_complete(sc, txq);
2556 }
2557
2558 void ath_tx_tasklet(struct ath_softc *sc)
2559 {
2560         struct ath_hw *ah = sc->sc_ah;
2561         u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1) & ah->intr_txqs;
2562         int i;
2563
2564         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2565                 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2566                         ath_tx_processq(sc, &sc->tx.txq[i]);
2567         }
2568 }
2569
2570 void ath_tx_edma_tasklet(struct ath_softc *sc)
2571 {
2572         struct ath_tx_status ts;
2573         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2574         struct ath_hw *ah = sc->sc_ah;
2575         struct ath_txq *txq;
2576         struct ath_buf *bf, *lastbf;
2577         struct list_head bf_head;
2578         struct list_head *fifo_list;
2579         int status;
2580
2581         for (;;) {
2582                 if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
2583                         break;
2584
2585                 status = ath9k_hw_txprocdesc(ah, NULL, (void *)&ts);
2586                 if (status == -EINPROGRESS)
2587                         break;
2588                 if (status == -EIO) {
2589                         ath_dbg(common, XMIT, "Error processing tx status\n");
2590                         break;
2591                 }
2592
2593                 /* Process beacon completions separately */
2594                 if (ts.qid == sc->beacon.beaconq) {
2595                         sc->beacon.tx_processed = true;
2596                         sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2597
2598                         ath9k_csa_update(sc);
2599                         continue;
2600                 }
2601
2602                 txq = &sc->tx.txq[ts.qid];
2603
2604                 ath_txq_lock(sc, txq);
2605
2606                 TX_STAT_INC(txq->axq_qnum, txprocdesc);
2607
2608                 fifo_list = &txq->txq_fifo[txq->txq_tailidx];
2609                 if (list_empty(fifo_list)) {
2610                         ath_txq_unlock(sc, txq);
2611                         return;
2612                 }
2613
2614                 bf = list_first_entry(fifo_list, struct ath_buf, list);
2615                 if (bf->bf_state.stale) {
2616                         list_del(&bf->list);
2617                         ath_tx_return_buffer(sc, bf);
2618                         bf = list_first_entry(fifo_list, struct ath_buf, list);
2619                 }
2620
2621                 lastbf = bf->bf_lastbf;
2622
2623                 INIT_LIST_HEAD(&bf_head);
2624                 if (list_is_last(&lastbf->list, fifo_list)) {
2625                         list_splice_tail_init(fifo_list, &bf_head);
2626                         INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
2627
2628                         if (!list_empty(&txq->axq_q)) {
2629                                 struct list_head bf_q;
2630
2631                                 INIT_LIST_HEAD(&bf_q);
2632                                 txq->axq_link = NULL;
2633                                 list_splice_tail_init(&txq->axq_q, &bf_q);
2634                                 ath_tx_txqaddbuf(sc, txq, &bf_q, true);
2635                         }
2636                 } else {
2637                         lastbf->bf_state.stale = true;
2638                         if (bf != lastbf)
2639                                 list_cut_position(&bf_head, fifo_list,
2640                                                   lastbf->list.prev);
2641                 }
2642
2643                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2644                 ath_txq_unlock_complete(sc, txq);
2645         }
2646 }
2647
2648 /*****************/
2649 /* Init, Cleanup */
2650 /*****************/
2651
2652 static int ath_txstatus_setup(struct ath_softc *sc, int size)
2653 {
2654         struct ath_descdma *dd = &sc->txsdma;
2655         u8 txs_len = sc->sc_ah->caps.txs_len;
2656
2657         dd->dd_desc_len = size * txs_len;
2658         dd->dd_desc = dmam_alloc_coherent(sc->dev, dd->dd_desc_len,
2659                                           &dd->dd_desc_paddr, GFP_KERNEL);
2660         if (!dd->dd_desc)
2661                 return -ENOMEM;
2662
2663         return 0;
2664 }
2665
2666 static int ath_tx_edma_init(struct ath_softc *sc)
2667 {
2668         int err;
2669
2670         err = ath_txstatus_setup(sc, ATH_TXSTATUS_RING_SIZE);
2671         if (!err)
2672                 ath9k_hw_setup_statusring(sc->sc_ah, sc->txsdma.dd_desc,
2673                                           sc->txsdma.dd_desc_paddr,
2674                                           ATH_TXSTATUS_RING_SIZE);
2675
2676         return err;
2677 }
2678
2679 int ath_tx_init(struct ath_softc *sc, int nbufs)
2680 {
2681         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2682         int error = 0;
2683
2684         spin_lock_init(&sc->tx.txbuflock);
2685
2686         error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
2687                                   "tx", nbufs, 1, 1);
2688         if (error != 0) {
2689                 ath_err(common,
2690                         "Failed to allocate tx descriptors: %d\n", error);
2691                 return error;
2692         }
2693
2694         error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
2695                                   "beacon", ATH_BCBUF, 1, 1);
2696         if (error != 0) {
2697                 ath_err(common,
2698                         "Failed to allocate beacon descriptors: %d\n", error);
2699                 return error;
2700         }
2701
2702         INIT_DELAYED_WORK(&sc->tx_complete_work, ath_tx_complete_poll_work);
2703
2704         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
2705                 error = ath_tx_edma_init(sc);
2706
2707         return error;
2708 }
2709
2710 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2711 {
2712         struct ath_atx_tid *tid;
2713         struct ath_atx_ac *ac;
2714         int tidno, acno;
2715
2716         for (tidno = 0, tid = &an->tid[tidno];
2717              tidno < IEEE80211_NUM_TIDS;
2718              tidno++, tid++) {
2719                 tid->an        = an;
2720                 tid->tidno     = tidno;
2721                 tid->seq_start = tid->seq_next = 0;
2722                 tid->baw_size  = WME_MAX_BA;
2723                 tid->baw_head  = tid->baw_tail = 0;
2724                 tid->sched     = false;
2725                 tid->active        = false;
2726                 __skb_queue_head_init(&tid->buf_q);
2727                 __skb_queue_head_init(&tid->retry_q);
2728                 acno = TID_TO_WME_AC(tidno);
2729                 tid->ac = &an->ac[acno];
2730         }
2731
2732         for (acno = 0, ac = &an->ac[acno];
2733              acno < IEEE80211_NUM_ACS; acno++, ac++) {
2734                 ac->sched    = false;
2735                 ac->clear_ps_filter = true;
2736                 ac->txq = sc->tx.txq_map[acno];
2737                 INIT_LIST_HEAD(&ac->tid_q);
2738         }
2739 }
2740
2741 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2742 {
2743         struct ath_atx_ac *ac;
2744         struct ath_atx_tid *tid;
2745         struct ath_txq *txq;
2746         int tidno;
2747
2748         for (tidno = 0, tid = &an->tid[tidno];
2749              tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
2750
2751                 ac = tid->ac;
2752                 txq = ac->txq;
2753
2754                 ath_txq_lock(sc, txq);
2755
2756                 if (tid->sched) {
2757                         list_del(&tid->list);
2758                         tid->sched = false;
2759                 }
2760
2761                 if (ac->sched) {
2762                         list_del(&ac->list);
2763                         tid->ac->sched = false;
2764                 }
2765
2766                 ath_tid_drain(sc, txq, tid);
2767                 tid->active = false;
2768
2769                 ath_txq_unlock(sc, txq);
2770         }
2771 }
2772
2773 #ifdef CONFIG_ATH9K_TX99
2774
2775 int ath9k_tx99_send(struct ath_softc *sc, struct sk_buff *skb,
2776                     struct ath_tx_control *txctl)
2777 {
2778         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2779         struct ath_frame_info *fi = get_frame_info(skb);
2780         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2781         struct ath_buf *bf;
2782         int padpos, padsize;
2783
2784         padpos = ieee80211_hdrlen(hdr->frame_control);
2785         padsize = padpos & 3;
2786
2787         if (padsize && skb->len > padpos) {
2788                 if (skb_headroom(skb) < padsize) {
2789                         ath_dbg(common, XMIT,
2790                                 "tx99 padding failed\n");
2791                 return -EINVAL;
2792                 }
2793
2794                 skb_push(skb, padsize);
2795                 memmove(skb->data, skb->data + padsize, padpos);
2796         }
2797
2798         fi->keyix = ATH9K_TXKEYIX_INVALID;
2799         fi->framelen = skb->len + FCS_LEN;
2800         fi->keytype = ATH9K_KEY_TYPE_CLEAR;
2801
2802         bf = ath_tx_setup_buffer(sc, txctl->txq, NULL, skb);
2803         if (!bf) {
2804                 ath_dbg(common, XMIT, "tx99 buffer setup failed\n");
2805                 return -EINVAL;
2806         }
2807
2808         ath_set_rates(sc->tx99_vif, NULL, bf);
2809
2810         ath9k_hw_set_desc_link(sc->sc_ah, bf->bf_desc, bf->bf_daddr);
2811         ath9k_hw_tx99_start(sc->sc_ah, txctl->txq->axq_qnum);
2812
2813         ath_tx_send_normal(sc, txctl->txq, NULL, skb);
2814
2815         return 0;
2816 }
2817
2818 #endif /* CONFIG_ATH9K_TX99 */