ath9k: Fix incorrect sequence numbering for unaggregated QoS Frame.
[safe/jmp/linux-2.6] / drivers / net / wireless / ath9k / xmit.c
1 /*
2  * Copyright (c) 2008 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 "core.h"
18
19 #define BITS_PER_BYTE           8
20 #define OFDM_PLCP_BITS          22
21 #define HT_RC_2_MCS(_rc)        ((_rc) & 0x0f)
22 #define HT_RC_2_STREAMS(_rc)    ((((_rc) & 0x78) >> 3) + 1)
23 #define L_STF                   8
24 #define L_LTF                   8
25 #define L_SIG                   4
26 #define HT_SIG                  8
27 #define HT_STF                  4
28 #define HT_LTF(_ns)             (4 * (_ns))
29 #define SYMBOL_TIME(_ns)        ((_ns) << 2) /* ns * 4 us */
30 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)  /* ns * 3.6 us */
31 #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
32 #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
33
34 #define OFDM_SIFS_TIME              16
35
36 static u32 bits_per_symbol[][2] = {
37         /* 20MHz 40MHz */
38         {    26,   54 },     /*  0: BPSK */
39         {    52,  108 },     /*  1: QPSK 1/2 */
40         {    78,  162 },     /*  2: QPSK 3/4 */
41         {   104,  216 },     /*  3: 16-QAM 1/2 */
42         {   156,  324 },     /*  4: 16-QAM 3/4 */
43         {   208,  432 },     /*  5: 64-QAM 2/3 */
44         {   234,  486 },     /*  6: 64-QAM 3/4 */
45         {   260,  540 },     /*  7: 64-QAM 5/6 */
46         {    52,  108 },     /*  8: BPSK */
47         {   104,  216 },     /*  9: QPSK 1/2 */
48         {   156,  324 },     /* 10: QPSK 3/4 */
49         {   208,  432 },     /* 11: 16-QAM 1/2 */
50         {   312,  648 },     /* 12: 16-QAM 3/4 */
51         {   416,  864 },     /* 13: 64-QAM 2/3 */
52         {   468,  972 },     /* 14: 64-QAM 3/4 */
53         {   520, 1080 },     /* 15: 64-QAM 5/6 */
54 };
55
56 #define IS_HT_RATE(_rate)     ((_rate) & 0x80)
57
58 /*
59  * Insert a chain of ath_buf (descriptors) on a txq and
60  * assume the descriptors are already chained together by caller.
61  * NB: must be called with txq lock held
62  */
63
64 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
65                              struct list_head *head)
66 {
67         struct ath_hal *ah = sc->sc_ah;
68         struct ath_buf *bf;
69
70         /*
71          * Insert the frame on the outbound list and
72          * pass it on to the hardware.
73          */
74
75         if (list_empty(head))
76                 return;
77
78         bf = list_first_entry(head, struct ath_buf, list);
79
80         list_splice_tail_init(head, &txq->axq_q);
81         txq->axq_depth++;
82         txq->axq_totalqueued++;
83         txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
84
85         DPRINTF(sc, ATH_DBG_QUEUE,
86                 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
87
88         if (txq->axq_link == NULL) {
89                 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
90                 DPRINTF(sc, ATH_DBG_XMIT,
91                         "TXDP[%u] = %llx (%p)\n",
92                         txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
93         } else {
94                 *txq->axq_link = bf->bf_daddr;
95                 DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
96                         txq->axq_qnum, txq->axq_link,
97                         ito64(bf->bf_daddr), bf->bf_desc);
98         }
99         txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
100         ath9k_hw_txstart(ah, txq->axq_qnum);
101 }
102
103 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
104                             struct ath_xmit_status *tx_status)
105 {
106         struct ieee80211_hw *hw = sc->hw;
107         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
108         struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
109         int hdrlen, padsize;
110
111         DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
112
113         if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
114             tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
115                 kfree(tx_info_priv);
116                 tx_info->rate_driver_data[0] = NULL;
117         }
118
119         if (tx_status->flags & ATH_TX_BAR) {
120                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
121                 tx_status->flags &= ~ATH_TX_BAR;
122         }
123
124         if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
125                 /* Frame was ACKed */
126                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
127         }
128
129         tx_info->status.rates[0].count = tx_status->retries;
130         if (tx_info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
131                 /* Change idx from internal table index to MCS index */
132                 int idx = tx_info->status.rates[0].idx;
133                 struct ath_rate_table *rate_table = sc->cur_rate_table;
134                 if (idx >= 0 && idx < rate_table->rate_cnt)
135                         tx_info->status.rates[0].idx =
136                                 rate_table->info[idx].ratecode & 0x7f;
137         }
138
139         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
140         padsize = hdrlen & 3;
141         if (padsize && hdrlen >= 24) {
142                 /*
143                  * Remove MAC header padding before giving the frame back to
144                  * mac80211.
145                  */
146                 memmove(skb->data + padsize, skb->data, hdrlen);
147                 skb_pull(skb, padsize);
148         }
149
150         ieee80211_tx_status(hw, skb);
151 }
152
153 /* Check if it's okay to send out aggregates */
154
155 static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
156 {
157         struct ath_atx_tid *tid;
158         tid = ATH_AN_2_TID(an, tidno);
159
160         if (tid->state & AGGR_ADDBA_COMPLETE ||
161             tid->state & AGGR_ADDBA_PROGRESS)
162                 return 1;
163         else
164                 return 0;
165 }
166
167 static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
168                                  struct ath_beacon_config *conf)
169 {
170         struct ieee80211_hw *hw = sc->hw;
171
172         /* fill in beacon config data */
173
174         conf->beacon_interval = hw->conf.beacon_int;
175         conf->listen_interval = 100;
176         conf->dtim_count = 1;
177         conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
178 }
179
180 /* Calculate Atheros packet type from IEEE80211 packet header */
181
182 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
183 {
184         struct ieee80211_hdr *hdr;
185         enum ath9k_pkt_type htype;
186         __le16 fc;
187
188         hdr = (struct ieee80211_hdr *)skb->data;
189         fc = hdr->frame_control;
190
191         if (ieee80211_is_beacon(fc))
192                 htype = ATH9K_PKT_TYPE_BEACON;
193         else if (ieee80211_is_probe_resp(fc))
194                 htype = ATH9K_PKT_TYPE_PROBE_RESP;
195         else if (ieee80211_is_atim(fc))
196                 htype = ATH9K_PKT_TYPE_ATIM;
197         else if (ieee80211_is_pspoll(fc))
198                 htype = ATH9K_PKT_TYPE_PSPOLL;
199         else
200                 htype = ATH9K_PKT_TYPE_NORMAL;
201
202         return htype;
203 }
204
205 static bool is_pae(struct sk_buff *skb)
206 {
207         struct ieee80211_hdr *hdr;
208         __le16 fc;
209
210         hdr = (struct ieee80211_hdr *)skb->data;
211         fc = hdr->frame_control;
212
213         if (ieee80211_is_data(fc)) {
214                 if (ieee80211_is_nullfunc(fc) ||
215                     /* Port Access Entity (IEEE 802.1X) */
216                     (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
217                         return true;
218                 }
219         }
220
221         return false;
222 }
223
224 static int get_hw_crypto_keytype(struct sk_buff *skb)
225 {
226         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
227
228         if (tx_info->control.hw_key) {
229                 if (tx_info->control.hw_key->alg == ALG_WEP)
230                         return ATH9K_KEY_TYPE_WEP;
231                 else if (tx_info->control.hw_key->alg == ALG_TKIP)
232                         return ATH9K_KEY_TYPE_TKIP;
233                 else if (tx_info->control.hw_key->alg == ALG_CCMP)
234                         return ATH9K_KEY_TYPE_AES;
235         }
236
237         return ATH9K_KEY_TYPE_CLEAR;
238 }
239
240 /* Called only when tx aggregation is enabled and HT is supported */
241
242 static void assign_aggr_tid_seqno(struct sk_buff *skb,
243                                   struct ath_buf *bf)
244 {
245         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
246         struct ieee80211_hdr *hdr;
247         struct ath_node *an;
248         struct ath_atx_tid *tid;
249         __le16 fc;
250         u8 *qc;
251
252         if (!tx_info->control.sta)
253                 return;
254
255         an = (struct ath_node *)tx_info->control.sta->drv_priv;
256         hdr = (struct ieee80211_hdr *)skb->data;
257         fc = hdr->frame_control;
258
259         /* Get tidno */
260
261         if (ieee80211_is_data_qos(fc)) {
262                 qc = ieee80211_get_qos_ctl(hdr);
263                 bf->bf_tidno = qc[0] & 0xf;
264         }
265
266         /* Get seqno */
267         /* For HT capable stations, we save tidno for later use.
268          * We also override seqno set by upper layer with the one
269          * in tx aggregation state.
270          *
271          * If fragmentation is on, the sequence number is
272          * not overridden, since it has been
273          * incremented by the fragmentation routine.
274          *
275          * FIXME: check if the fragmentation threshold exceeds
276          * IEEE80211 max.
277          */
278         tid = ATH_AN_2_TID(an, bf->bf_tidno);
279         hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
280                         IEEE80211_SEQ_SEQ_SHIFT);
281         bf->bf_seqno = tid->seq_next;
282         INCR(tid->seq_next, IEEE80211_SEQ_MAX);
283 }
284
285 static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
286                           struct ath_txq *txq)
287 {
288         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
289         int flags = 0;
290
291         flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
292         flags |= ATH9K_TXDESC_INTREQ;
293
294         if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
295                 flags |= ATH9K_TXDESC_NOACK;
296         if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
297                 flags |= ATH9K_TXDESC_RTSENA;
298
299         return flags;
300 }
301
302 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
303 {
304         struct ath_buf *bf = NULL;
305
306         spin_lock_bh(&sc->tx.txbuflock);
307
308         if (unlikely(list_empty(&sc->tx.txbuf))) {
309                 spin_unlock_bh(&sc->tx.txbuflock);
310                 return NULL;
311         }
312
313         bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
314         list_del(&bf->list);
315
316         spin_unlock_bh(&sc->tx.txbuflock);
317
318         return bf;
319 }
320
321 /* To complete a chain of buffers associated a frame */
322
323 static void ath_tx_complete_buf(struct ath_softc *sc,
324                                 struct ath_buf *bf,
325                                 struct list_head *bf_q,
326                                 int txok, int sendbar)
327 {
328         struct sk_buff *skb = bf->bf_mpdu;
329         struct ath_xmit_status tx_status;
330         unsigned long flags;
331
332         /*
333          * Set retry information.
334          * NB: Don't use the information in the descriptor, because the frame
335          * could be software retried.
336          */
337         tx_status.retries = bf->bf_retries;
338         tx_status.flags = 0;
339
340         if (sendbar)
341                 tx_status.flags = ATH_TX_BAR;
342
343         if (!txok) {
344                 tx_status.flags |= ATH_TX_ERROR;
345
346                 if (bf_isxretried(bf))
347                         tx_status.flags |= ATH_TX_XRETRY;
348         }
349
350         /* Unmap this frame */
351         pci_unmap_single(sc->pdev,
352                          bf->bf_dmacontext,
353                          skb->len,
354                          PCI_DMA_TODEVICE);
355         /* complete this frame */
356         ath_tx_complete(sc, skb, &tx_status);
357
358         /*
359          * Return the list of ath_buf of this mpdu to free queue
360          */
361         spin_lock_irqsave(&sc->tx.txbuflock, flags);
362         list_splice_tail_init(bf_q, &sc->tx.txbuf);
363         spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
364 }
365
366 /*
367  * queue up a dest/ac pair for tx scheduling
368  * NB: must be called with txq lock held
369  */
370
371 static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
372 {
373         struct ath_atx_ac *ac = tid->ac;
374
375         /*
376          * if tid is paused, hold off
377          */
378         if (tid->paused)
379                 return;
380
381         /*
382          * add tid to ac atmost once
383          */
384         if (tid->sched)
385                 return;
386
387         tid->sched = true;
388         list_add_tail(&tid->list, &ac->tid_q);
389
390         /*
391          * add node ac to txq atmost once
392          */
393         if (ac->sched)
394                 return;
395
396         ac->sched = true;
397         list_add_tail(&ac->list, &txq->axq_acq);
398 }
399
400 /* pause a tid */
401
402 static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
403 {
404         struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
405
406         spin_lock_bh(&txq->axq_lock);
407
408         tid->paused++;
409
410         spin_unlock_bh(&txq->axq_lock);
411 }
412
413 /* resume a tid and schedule aggregate */
414
415 void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
416 {
417         struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
418
419         ASSERT(tid->paused > 0);
420         spin_lock_bh(&txq->axq_lock);
421
422         tid->paused--;
423
424         if (tid->paused > 0)
425                 goto unlock;
426
427         if (list_empty(&tid->buf_q))
428                 goto unlock;
429
430         /*
431          * Add this TID to scheduler and try to send out aggregates
432          */
433         ath_tx_queue_tid(txq, tid);
434         ath_txq_schedule(sc, txq);
435 unlock:
436         spin_unlock_bh(&txq->axq_lock);
437 }
438
439 /* Compute the number of bad frames */
440
441 static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
442                               int txok)
443 {
444         struct ath_buf *bf_last = bf->bf_lastbf;
445         struct ath_desc *ds = bf_last->bf_desc;
446         u16 seq_st = 0;
447         u32 ba[WME_BA_BMP_SIZE >> 5];
448         int ba_index;
449         int nbad = 0;
450         int isaggr = 0;
451
452         if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
453                 return 0;
454
455         isaggr = bf_isaggr(bf);
456         if (isaggr) {
457                 seq_st = ATH_DS_BA_SEQ(ds);
458                 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
459         }
460
461         while (bf) {
462                 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
463                 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
464                         nbad++;
465
466                 bf = bf->bf_next;
467         }
468
469         return nbad;
470 }
471
472 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
473 {
474         struct sk_buff *skb;
475         struct ieee80211_hdr *hdr;
476
477         bf->bf_state.bf_type |= BUF_RETRY;
478         bf->bf_retries++;
479
480         skb = bf->bf_mpdu;
481         hdr = (struct ieee80211_hdr *)skb->data;
482         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
483 }
484
485 /* Update block ack window */
486
487 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
488                               int seqno)
489 {
490         int index, cindex;
491
492         index  = ATH_BA_INDEX(tid->seq_start, seqno);
493         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
494
495         tid->tx_buf[cindex] = NULL;
496
497         while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
498                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
499                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
500         }
501 }
502
503 /*
504  * ath_pkt_dur - compute packet duration (NB: not NAV)
505  *
506  * rix - rate index
507  * pktlen - total bytes (delims + data + fcs + pads + pad delims)
508  * width  - 0 for 20 MHz, 1 for 40 MHz
509  * half_gi - to use 4us v/s 3.6 us for symbol time
510  */
511 static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
512                             int width, int half_gi, bool shortPreamble)
513 {
514         struct ath_rate_table *rate_table = sc->cur_rate_table;
515         u32 nbits, nsymbits, duration, nsymbols;
516         u8 rc;
517         int streams, pktlen;
518
519         pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
520         rc = rate_table->info[rix].ratecode;
521
522         /* for legacy rates, use old function to compute packet duration */
523         if (!IS_HT_RATE(rc))
524                 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
525                                               rix, shortPreamble);
526
527         /* find number of symbols: PLCP + data */
528         nbits = (pktlen << 3) + OFDM_PLCP_BITS;
529         nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
530         nsymbols = (nbits + nsymbits - 1) / nsymbits;
531
532         if (!half_gi)
533                 duration = SYMBOL_TIME(nsymbols);
534         else
535                 duration = SYMBOL_TIME_HALFGI(nsymbols);
536
537         /* addup duration for legacy/ht training and signal fields */
538         streams = HT_RC_2_STREAMS(rc);
539         duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
540
541         return duration;
542 }
543
544 /* Rate module function to set rate related fields in tx descriptor */
545
546 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
547 {
548         struct ath_hal *ah = sc->sc_ah;
549         struct ath_rate_table *rt;
550         struct ath_desc *ds = bf->bf_desc;
551         struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
552         struct ath9k_11n_rate_series series[4];
553         struct sk_buff *skb;
554         struct ieee80211_tx_info *tx_info;
555         struct ieee80211_tx_rate *rates;
556         struct ieee80211_hdr *hdr;
557         int i, flags, rtsctsena = 0;
558         u32 ctsduration = 0;
559         u8 rix = 0, cix, ctsrate = 0;
560         __le16 fc;
561
562         memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
563
564         skb = (struct sk_buff *)bf->bf_mpdu;
565         hdr = (struct ieee80211_hdr *)skb->data;
566         fc = hdr->frame_control;
567         tx_info = IEEE80211_SKB_CB(skb);
568         rates = tx_info->control.rates;
569
570         if (ieee80211_has_morefrags(fc) ||
571             (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
572                 rates[1].count = rates[2].count = rates[3].count = 0;
573                 rates[1].idx = rates[2].idx = rates[3].idx = 0;
574                 rates[0].count = ATH_TXMAXTRY;
575         }
576
577         /* get the cix for the lowest valid rix */
578         rt = sc->cur_rate_table;
579         for (i = 3; i >= 0; i--) {
580                 if (rates[i].count && (rates[i].idx >= 0)) {
581                         rix = rates[i].idx;
582                         break;
583                 }
584         }
585
586         flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
587         cix = rt->info[rix].ctrl_rate;
588
589         /*
590          * If 802.11g protection is enabled, determine whether to use RTS/CTS or
591          * just CTS.  Note that this is only done for OFDM/HT unicast frames.
592          */
593         if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
594             && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
595                 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
596                 if (sc->sc_protmode == PROT_M_RTSCTS)
597                         flags = ATH9K_TXDESC_RTSENA;
598                 else if (sc->sc_protmode == PROT_M_CTSONLY)
599                         flags = ATH9K_TXDESC_CTSENA;
600
601                 cix = rt->info[sc->sc_protrix].ctrl_rate;
602                 rtsctsena = 1;
603         }
604
605         /* For 11n, the default behavior is to enable RTS for hw retried frames.
606          * We enable the global flag here and let rate series flags determine
607          * which rates will actually use RTS.
608          */
609         if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
610                 /* 802.11g protection not needed, use our default behavior */
611                 if (!rtsctsena)
612                         flags = ATH9K_TXDESC_RTSENA;
613         }
614
615         /* Set protection if aggregate protection on */
616         if (sc->sc_config.ath_aggr_prot &&
617             (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
618                 flags = ATH9K_TXDESC_RTSENA;
619                 cix = rt->info[sc->sc_protrix].ctrl_rate;
620                 rtsctsena = 1;
621         }
622
623         /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
624         if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
625                 flags &= ~(ATH9K_TXDESC_RTSENA);
626
627         /*
628          * CTS transmit rate is derived from the transmit rate by looking in the
629          * h/w rate table.  We must also factor in whether or not a short
630          * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
631          */
632         ctsrate = rt->info[cix].ratecode |
633                 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
634
635         for (i = 0; i < 4; i++) {
636                 if (!rates[i].count || (rates[i].idx < 0))
637                         continue;
638
639                 rix = rates[i].idx;
640
641                 series[i].Rate = rt->info[rix].ratecode |
642                         (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
643
644                 series[i].Tries = rates[i].count;
645
646                 series[i].RateFlags = (
647                         (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
648                                 ATH9K_RATESERIES_RTS_CTS : 0) |
649                         ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
650                                 ATH9K_RATESERIES_2040 : 0) |
651                         ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
652                                 ATH9K_RATESERIES_HALFGI : 0);
653
654                 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
655                          (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
656                          (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
657                          bf_isshpreamble(bf));
658
659                 series[i].ChSel = sc->sc_tx_chainmask;
660
661                 if (rtsctsena)
662                         series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
663         }
664
665         /* set dur_update_en for l-sig computation except for PS-Poll frames */
666         ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
667                                      ctsrate, ctsduration,
668                                      series, 4, flags);
669
670         if (sc->sc_config.ath_aggr_prot && flags)
671                 ath9k_hw_set11n_burstduration(ah, ds, 8192);
672 }
673
674 /*
675  * Function to send a normal HT (non-AMPDU) frame
676  * NB: must be called with txq lock held
677  */
678 static int ath_tx_send_normal(struct ath_softc *sc,
679                               struct ath_txq *txq,
680                               struct ath_atx_tid *tid,
681                               struct list_head *bf_head)
682 {
683         struct ath_buf *bf;
684
685         BUG_ON(list_empty(bf_head));
686
687         bf = list_first_entry(bf_head, struct ath_buf, list);
688         bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
689
690         /* update starting sequence number for subsequent ADDBA request */
691         INCR(tid->seq_start, IEEE80211_SEQ_MAX);
692
693         /* Queue to h/w without aggregation */
694         bf->bf_nframes = 1;
695         bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
696         ath_buf_set_rate(sc, bf);
697         ath_tx_txqaddbuf(sc, txq, bf_head);
698
699         return 0;
700 }
701
702 /* flush tid's software queue and send frames as non-ampdu's */
703
704 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
705 {
706         struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
707         struct ath_buf *bf;
708         struct list_head bf_head;
709         INIT_LIST_HEAD(&bf_head);
710
711         ASSERT(tid->paused > 0);
712         spin_lock_bh(&txq->axq_lock);
713
714         tid->paused--;
715
716         if (tid->paused > 0) {
717                 spin_unlock_bh(&txq->axq_lock);
718                 return;
719         }
720
721         while (!list_empty(&tid->buf_q)) {
722                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
723                 ASSERT(!bf_isretried(bf));
724                 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
725                 ath_tx_send_normal(sc, txq, tid, &bf_head);
726         }
727
728         spin_unlock_bh(&txq->axq_lock);
729 }
730
731 /* Completion routine of an aggregate */
732
733 static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
734                                       struct ath_txq *txq,
735                                       struct ath_buf *bf,
736                                       struct list_head *bf_q,
737                                       int txok)
738 {
739         struct ath_node *an = NULL;
740         struct sk_buff *skb;
741         struct ieee80211_tx_info *tx_info;
742         struct ath_atx_tid *tid = NULL;
743         struct ath_buf *bf_last = bf->bf_lastbf;
744         struct ath_desc *ds = bf_last->bf_desc;
745         struct ath_buf *bf_next, *bf_lastq = NULL;
746         struct list_head bf_head, bf_pending;
747         u16 seq_st = 0;
748         u32 ba[WME_BA_BMP_SIZE >> 5];
749         int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
750
751         skb = (struct sk_buff *)bf->bf_mpdu;
752         tx_info = IEEE80211_SKB_CB(skb);
753
754         if (tx_info->control.sta) {
755                 an = (struct ath_node *)tx_info->control.sta->drv_priv;
756                 tid = ATH_AN_2_TID(an, bf->bf_tidno);
757         }
758
759         isaggr = bf_isaggr(bf);
760         if (isaggr) {
761                 if (txok) {
762                         if (ATH_DS_TX_BA(ds)) {
763                                 /*
764                                  * extract starting sequence and
765                                  * block-ack bitmap
766                                  */
767                                 seq_st = ATH_DS_BA_SEQ(ds);
768                                 memcpy(ba,
769                                         ATH_DS_BA_BITMAP(ds),
770                                         WME_BA_BMP_SIZE >> 3);
771                         } else {
772                                 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
773
774                                 /*
775                                  * AR5416 can become deaf/mute when BA
776                                  * issue happens. Chip needs to be reset.
777                                  * But AP code may have sychronization issues
778                                  * when perform internal reset in this routine.
779                                  * Only enable reset in STA mode for now.
780                                  */
781                                 if (sc->sc_ah->ah_opmode ==
782                                             NL80211_IFTYPE_STATION)
783                                         needreset = 1;
784                         }
785                 } else {
786                         memset(ba, 0, WME_BA_BMP_SIZE >> 3);
787                 }
788         }
789
790         INIT_LIST_HEAD(&bf_pending);
791         INIT_LIST_HEAD(&bf_head);
792
793         while (bf) {
794                 txfail = txpending = 0;
795                 bf_next = bf->bf_next;
796
797                 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
798                         /* transmit completion, subframe is
799                          * acked by block ack */
800                 } else if (!isaggr && txok) {
801                         /* transmit completion */
802                 } else {
803
804                         if (!(tid->state & AGGR_CLEANUP) &&
805                             ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
806                                 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
807                                         ath_tx_set_retry(sc, bf);
808                                         txpending = 1;
809                                 } else {
810                                         bf->bf_state.bf_type |= BUF_XRETRY;
811                                         txfail = 1;
812                                         sendbar = 1;
813                                 }
814                         } else {
815                                 /*
816                                  * cleanup in progress, just fail
817                                  * the un-acked sub-frames
818                                  */
819                                 txfail = 1;
820                         }
821                 }
822                 /*
823                  * Remove ath_buf's of this sub-frame from aggregate queue.
824                  */
825                 if (bf_next == NULL) {  /* last subframe in the aggregate */
826                         ASSERT(bf->bf_lastfrm == bf_last);
827
828                         /*
829                          * The last descriptor of the last sub frame could be
830                          * a holding descriptor for h/w. If that's the case,
831                          * bf->bf_lastfrm won't be in the bf_q.
832                          * Make sure we handle bf_q properly here.
833                          */
834
835                         if (!list_empty(bf_q)) {
836                                 bf_lastq = list_entry(bf_q->prev,
837                                         struct ath_buf, list);
838                                 list_cut_position(&bf_head,
839                                         bf_q, &bf_lastq->list);
840                         } else {
841                                 /*
842                                  * XXX: if the last subframe only has one
843                                  * descriptor which is also being used as
844                                  * a holding descriptor. Then the ath_buf
845                                  * is not in the bf_q at all.
846                                  */
847                                 INIT_LIST_HEAD(&bf_head);
848                         }
849                 } else {
850                         ASSERT(!list_empty(bf_q));
851                         list_cut_position(&bf_head,
852                                 bf_q, &bf->bf_lastfrm->list);
853                 }
854
855                 if (!txpending) {
856                         /*
857                          * complete the acked-ones/xretried ones; update
858                          * block-ack window
859                          */
860                         spin_lock_bh(&txq->axq_lock);
861                         ath_tx_update_baw(sc, tid, bf->bf_seqno);
862                         spin_unlock_bh(&txq->axq_lock);
863
864                         /* complete this sub-frame */
865                         ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
866                 } else {
867                         /*
868                          * retry the un-acked ones
869                          */
870                         /*
871                          * XXX: if the last descriptor is holding descriptor,
872                          * in order to requeue the frame to software queue, we
873                          * need to allocate a new descriptor and
874                          * copy the content of holding descriptor to it.
875                          */
876                         if (bf->bf_next == NULL &&
877                             bf_last->bf_status & ATH_BUFSTATUS_STALE) {
878                                 struct ath_buf *tbf;
879
880                                 /* allocate new descriptor */
881                                 spin_lock_bh(&sc->tx.txbuflock);
882                                 ASSERT(!list_empty((&sc->tx.txbuf)));
883                                 tbf = list_first_entry(&sc->tx.txbuf,
884                                                 struct ath_buf, list);
885                                 list_del(&tbf->list);
886                                 spin_unlock_bh(&sc->tx.txbuflock);
887
888                                 ATH_TXBUF_RESET(tbf);
889
890                                 /* copy descriptor content */
891                                 tbf->bf_mpdu = bf_last->bf_mpdu;
892                                 tbf->bf_buf_addr = bf_last->bf_buf_addr;
893                                 *(tbf->bf_desc) = *(bf_last->bf_desc);
894
895                                 /* link it to the frame */
896                                 if (bf_lastq) {
897                                         bf_lastq->bf_desc->ds_link =
898                                                 tbf->bf_daddr;
899                                         bf->bf_lastfrm = tbf;
900                                         ath9k_hw_cleartxdesc(sc->sc_ah,
901                                                 bf->bf_lastfrm->bf_desc);
902                                 } else {
903                                         tbf->bf_state = bf_last->bf_state;
904                                         tbf->bf_lastfrm = tbf;
905                                         ath9k_hw_cleartxdesc(sc->sc_ah,
906                                                 tbf->bf_lastfrm->bf_desc);
907
908                                         /* copy the DMA context */
909                                         tbf->bf_dmacontext =
910                                                 bf_last->bf_dmacontext;
911                                 }
912                                 list_add_tail(&tbf->list, &bf_head);
913                         } else {
914                                 /*
915                                  * Clear descriptor status words for
916                                  * software retry
917                                  */
918                                 ath9k_hw_cleartxdesc(sc->sc_ah,
919                                                      bf->bf_lastfrm->bf_desc);
920                         }
921
922                         /*
923                          * Put this buffer to the temporary pending
924                          * queue to retain ordering
925                          */
926                         list_splice_tail_init(&bf_head, &bf_pending);
927                 }
928
929                 bf = bf_next;
930         }
931
932         if (tid->state & AGGR_CLEANUP) {
933                 /* check to see if we're done with cleaning the h/w queue */
934                 spin_lock_bh(&txq->axq_lock);
935
936                 if (tid->baw_head == tid->baw_tail) {
937                         tid->state &= ~AGGR_ADDBA_COMPLETE;
938                         tid->addba_exchangeattempts = 0;
939                         spin_unlock_bh(&txq->axq_lock);
940
941                         tid->state &= ~AGGR_CLEANUP;
942
943                         /* send buffered frames as singles */
944                         ath_tx_flush_tid(sc, tid);
945                 } else
946                         spin_unlock_bh(&txq->axq_lock);
947
948                 return;
949         }
950
951         /*
952          * prepend un-acked frames to the beginning of the pending frame queue
953          */
954         if (!list_empty(&bf_pending)) {
955                 spin_lock_bh(&txq->axq_lock);
956                 /* Note: we _prepend_, we _do_not_ at to
957                  * the end of the queue ! */
958                 list_splice(&bf_pending, &tid->buf_q);
959                 ath_tx_queue_tid(txq, tid);
960                 spin_unlock_bh(&txq->axq_lock);
961         }
962
963         if (needreset)
964                 ath_reset(sc, false);
965
966         return;
967 }
968
969 static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
970 {
971         struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
972         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
973         struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
974
975         tx_info_priv->update_rc = false;
976         if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
977                 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
978
979         if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
980             (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
981                 if (bf_isdata(bf)) {
982                         memcpy(&tx_info_priv->tx, &ds->ds_txstat,
983                                sizeof(tx_info_priv->tx));
984                         tx_info_priv->n_frames = bf->bf_nframes;
985                         tx_info_priv->n_bad_frames = nbad;
986                         tx_info_priv->update_rc = true;
987                 }
988         }
989 }
990
991 /* Process completed xmit descriptors from the specified queue */
992
993 static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
994 {
995         struct ath_hal *ah = sc->sc_ah;
996         struct ath_buf *bf, *lastbf, *bf_held = NULL;
997         struct list_head bf_head;
998         struct ath_desc *ds;
999         int txok, nbad = 0;
1000         int status;
1001
1002         DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
1003                 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
1004                 txq->axq_link);
1005
1006         for (;;) {
1007                 spin_lock_bh(&txq->axq_lock);
1008                 if (list_empty(&txq->axq_q)) {
1009                         txq->axq_link = NULL;
1010                         txq->axq_linkbuf = NULL;
1011                         spin_unlock_bh(&txq->axq_lock);
1012                         break;
1013                 }
1014                 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1015
1016                 /*
1017                  * There is a race condition that a BH gets scheduled
1018                  * after sw writes TxE and before hw re-load the last
1019                  * descriptor to get the newly chained one.
1020                  * Software must keep the last DONE descriptor as a
1021                  * holding descriptor - software does so by marking
1022                  * it with the STALE flag.
1023                  */
1024                 bf_held = NULL;
1025                 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1026                         bf_held = bf;
1027                         if (list_is_last(&bf_held->list, &txq->axq_q)) {
1028                                 /* FIXME:
1029                                  * The holding descriptor is the last
1030                                  * descriptor in queue. It's safe to remove
1031                                  * the last holding descriptor in BH context.
1032                                  */
1033                                 spin_unlock_bh(&txq->axq_lock);
1034                                 break;
1035                         } else {
1036                                 /* Lets work with the next buffer now */
1037                                 bf = list_entry(bf_held->list.next,
1038                                         struct ath_buf, list);
1039                         }
1040                 }
1041
1042                 lastbf = bf->bf_lastbf;
1043                 ds = lastbf->bf_desc;    /* NB: last decriptor */
1044
1045                 status = ath9k_hw_txprocdesc(ah, ds);
1046                 if (status == -EINPROGRESS) {
1047                         spin_unlock_bh(&txq->axq_lock);
1048                         break;
1049                 }
1050                 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1051                         txq->axq_lastdsWithCTS = NULL;
1052                 if (ds == txq->axq_gatingds)
1053                         txq->axq_gatingds = NULL;
1054
1055                 /*
1056                  * Remove ath_buf's of the same transmit unit from txq,
1057                  * however leave the last descriptor back as the holding
1058                  * descriptor for hw.
1059                  */
1060                 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1061                 INIT_LIST_HEAD(&bf_head);
1062
1063                 if (!list_is_singular(&lastbf->list))
1064                         list_cut_position(&bf_head,
1065                                 &txq->axq_q, lastbf->list.prev);
1066
1067                 txq->axq_depth--;
1068
1069                 if (bf_isaggr(bf))
1070                         txq->axq_aggr_depth--;
1071
1072                 txok = (ds->ds_txstat.ts_status == 0);
1073
1074                 spin_unlock_bh(&txq->axq_lock);
1075
1076                 if (bf_held) {
1077                         list_del(&bf_held->list);
1078                         spin_lock_bh(&sc->tx.txbuflock);
1079                         list_add_tail(&bf_held->list, &sc->tx.txbuf);
1080                         spin_unlock_bh(&sc->tx.txbuflock);
1081                 }
1082
1083                 if (!bf_isampdu(bf)) {
1084                         /*
1085                          * This frame is sent out as a single frame.
1086                          * Use hardware retry status for this frame.
1087                          */
1088                         bf->bf_retries = ds->ds_txstat.ts_longretry;
1089                         if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
1090                                 bf->bf_state.bf_type |= BUF_XRETRY;
1091                         nbad = 0;
1092                 } else {
1093                         nbad = ath_tx_num_badfrms(sc, bf, txok);
1094                 }
1095
1096                 ath_tx_rc_status(bf, ds, nbad);
1097
1098                 /*
1099                  * Complete this transmit unit
1100                  */
1101                 if (bf_isampdu(bf))
1102                         ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1103                 else
1104                         ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1105
1106                 /* Wake up mac80211 queue */
1107
1108                 spin_lock_bh(&txq->axq_lock);
1109                 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1110                                 (ATH_TXBUF - 20)) {
1111                         int qnum;
1112                         qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1113                         if (qnum != -1) {
1114                                 ieee80211_wake_queue(sc->hw, qnum);
1115                                 txq->stopped = 0;
1116                         }
1117
1118                 }
1119
1120                 /*
1121                  * schedule any pending packets if aggregation is enabled
1122                  */
1123                 if (sc->sc_flags & SC_OP_TXAGGR)
1124                         ath_txq_schedule(sc, txq);
1125                 spin_unlock_bh(&txq->axq_lock);
1126         }
1127 }
1128
1129 static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1130 {
1131         struct ath_hal *ah = sc->sc_ah;
1132
1133         (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
1134         DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1135                 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1136                 txq->axq_link);
1137 }
1138
1139 /* Drain only the data queues */
1140
1141 static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1142 {
1143         struct ath_hal *ah = sc->sc_ah;
1144         int i, status, npend = 0;
1145
1146         if (!(sc->sc_flags & SC_OP_INVALID)) {
1147                 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1148                         if (ATH_TXQ_SETUP(sc, i)) {
1149                                 ath_tx_stopdma(sc, &sc->tx.txq[i]);
1150                                 /* The TxDMA may not really be stopped.
1151                                  * Double check the hal tx pending count */
1152                                 npend += ath9k_hw_numtxpending(ah,
1153                                                        sc->tx.txq[i].axq_qnum);
1154                         }
1155                 }
1156         }
1157
1158         if (npend) {
1159                 /* TxDMA not stopped, reset the hal */
1160                 DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
1161
1162                 spin_lock_bh(&sc->sc_resetlock);
1163                 if (!ath9k_hw_reset(ah,
1164                                     sc->sc_ah->ah_curchan,
1165                                     sc->tx_chan_width,
1166                                     sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1167                                     sc->sc_ht_extprotspacing, true, &status)) {
1168
1169                         DPRINTF(sc, ATH_DBG_FATAL,
1170                                 "Unable to reset hardware; hal status %u\n",
1171                                 status);
1172                 }
1173                 spin_unlock_bh(&sc->sc_resetlock);
1174         }
1175
1176         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1177                 if (ATH_TXQ_SETUP(sc, i))
1178                         ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
1179         }
1180 }
1181
1182 /* Add a sub-frame to block ack window */
1183
1184 static void ath_tx_addto_baw(struct ath_softc *sc,
1185                              struct ath_atx_tid *tid,
1186                              struct ath_buf *bf)
1187 {
1188         int index, cindex;
1189
1190         if (bf_isretried(bf))
1191                 return;
1192
1193         index  = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1194         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1195
1196         ASSERT(tid->tx_buf[cindex] == NULL);
1197         tid->tx_buf[cindex] = bf;
1198
1199         if (index >= ((tid->baw_tail - tid->baw_head) &
1200                 (ATH_TID_MAX_BUFS - 1))) {
1201                 tid->baw_tail = cindex;
1202                 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1203         }
1204 }
1205
1206 /*
1207  * Function to send an A-MPDU
1208  * NB: must be called with txq lock held
1209  */
1210 static int ath_tx_send_ampdu(struct ath_softc *sc,
1211                              struct ath_atx_tid *tid,
1212                              struct list_head *bf_head,
1213                              struct ath_tx_control *txctl)
1214 {
1215         struct ath_buf *bf;
1216
1217         BUG_ON(list_empty(bf_head));
1218
1219         bf = list_first_entry(bf_head, struct ath_buf, list);
1220         bf->bf_state.bf_type |= BUF_AMPDU;
1221
1222         /*
1223          * Do not queue to h/w when any of the following conditions is true:
1224          * - there are pending frames in software queue
1225          * - the TID is currently paused for ADDBA/BAR request
1226          * - seqno is not within block-ack window
1227          * - h/w queue depth exceeds low water mark
1228          */
1229         if (!list_empty(&tid->buf_q) || tid->paused ||
1230             !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
1231             txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
1232                 /*
1233                  * Add this frame to software queue for scheduling later
1234                  * for aggregation.
1235                  */
1236                 list_splice_tail_init(bf_head, &tid->buf_q);
1237                 ath_tx_queue_tid(txctl->txq, tid);
1238                 return 0;
1239         }
1240
1241         /* Add sub-frame to BAW */
1242         ath_tx_addto_baw(sc, tid, bf);
1243
1244         /* Queue to h/w without aggregation */
1245         bf->bf_nframes = 1;
1246         bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1247         ath_buf_set_rate(sc, bf);
1248         ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
1249
1250         return 0;
1251 }
1252
1253 /*
1254  * looks up the rate
1255  * returns aggr limit based on lowest of the rates
1256  */
1257 static u32 ath_lookup_rate(struct ath_softc *sc,
1258                            struct ath_buf *bf,
1259                            struct ath_atx_tid *tid)
1260 {
1261         struct ath_rate_table *rate_table = sc->cur_rate_table;
1262         struct sk_buff *skb;
1263         struct ieee80211_tx_info *tx_info;
1264         struct ieee80211_tx_rate *rates;
1265         struct ath_tx_info_priv *tx_info_priv;
1266         u32 max_4ms_framelen, frame_length;
1267         u16 aggr_limit, legacy = 0, maxampdu;
1268         int i;
1269
1270         skb = (struct sk_buff *)bf->bf_mpdu;
1271         tx_info = IEEE80211_SKB_CB(skb);
1272         rates = tx_info->control.rates;
1273         tx_info_priv =
1274                 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
1275
1276         /*
1277          * Find the lowest frame length among the rate series that will have a
1278          * 4ms transmit duration.
1279          * TODO - TXOP limit needs to be considered.
1280          */
1281         max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1282
1283         for (i = 0; i < 4; i++) {
1284                 if (rates[i].count) {
1285                         if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
1286                                 legacy = 1;
1287                                 break;
1288                         }
1289
1290                         frame_length =
1291                                 rate_table->info[rates[i].idx].max_4ms_framelen;
1292                         max_4ms_framelen = min(max_4ms_framelen, frame_length);
1293                 }
1294         }
1295
1296         /*
1297          * limit aggregate size by the minimum rate if rate selected is
1298          * not a probe rate, if rate selected is a probe rate then
1299          * avoid aggregation of this packet.
1300          */
1301         if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1302                 return 0;
1303
1304         aggr_limit = min(max_4ms_framelen,
1305                 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1306
1307         /*
1308          * h/w can accept aggregates upto 16 bit lengths (65535).
1309          * The IE, however can hold upto 65536, which shows up here
1310          * as zero. Ignore 65536 since we  are constrained by hw.
1311          */
1312         maxampdu = tid->an->maxampdu;
1313         if (maxampdu)
1314                 aggr_limit = min(aggr_limit, maxampdu);
1315
1316         return aggr_limit;
1317 }
1318
1319 /*
1320  * returns the number of delimiters to be added to
1321  * meet the minimum required mpdudensity.
1322  * caller should make sure that the rate is  HT rate .
1323  */
1324 static int ath_compute_num_delims(struct ath_softc *sc,
1325                                   struct ath_atx_tid *tid,
1326                                   struct ath_buf *bf,
1327                                   u16 frmlen)
1328 {
1329         struct ath_rate_table *rt = sc->cur_rate_table;
1330         struct sk_buff *skb = bf->bf_mpdu;
1331         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1332         u32 nsymbits, nsymbols, mpdudensity;
1333         u16 minlen;
1334         u8 rc, flags, rix;
1335         int width, half_gi, ndelim, mindelim;
1336
1337         /* Select standard number of delimiters based on frame length alone */
1338         ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1339
1340         /*
1341          * If encryption enabled, hardware requires some more padding between
1342          * subframes.
1343          * TODO - this could be improved to be dependent on the rate.
1344          *      The hardware can keep up at lower rates, but not higher rates
1345          */
1346         if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1347                 ndelim += ATH_AGGR_ENCRYPTDELIM;
1348
1349         /*
1350          * Convert desired mpdu density from microeconds to bytes based
1351          * on highest rate in rate series (i.e. first rate) to determine
1352          * required minimum length for subframe. Take into account
1353          * whether high rate is 20 or 40Mhz and half or full GI.
1354          */
1355         mpdudensity = tid->an->mpdudensity;
1356
1357         /*
1358          * If there is no mpdu density restriction, no further calculation
1359          * is needed.
1360          */
1361         if (mpdudensity == 0)
1362                 return ndelim;
1363
1364         rix = tx_info->control.rates[0].idx;
1365         flags = tx_info->control.rates[0].flags;
1366         rc = rt->info[rix].ratecode;
1367         width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1368         half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
1369
1370         if (half_gi)
1371                 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1372         else
1373                 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1374
1375         if (nsymbols == 0)
1376                 nsymbols = 1;
1377
1378         nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1379         minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1380
1381         /* Is frame shorter than required minimum length? */
1382         if (frmlen < minlen) {
1383                 /* Get the minimum number of delimiters required. */
1384                 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1385                 ndelim = max(mindelim, ndelim);
1386         }
1387
1388         return ndelim;
1389 }
1390
1391 /*
1392  * For aggregation from software buffer queue.
1393  * NB: must be called with txq lock held
1394  */
1395 static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1396                                         struct ath_atx_tid *tid,
1397                                         struct list_head *bf_q,
1398                                         struct ath_buf **bf_last,
1399                                         struct aggr_rifs_param *param,
1400                                         int *prev_frames)
1401 {
1402 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1403         struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1404         struct list_head bf_head;
1405         int rl = 0, nframes = 0, ndelim;
1406         u16 aggr_limit = 0, al = 0, bpad = 0,
1407                 al_delta, h_baw = tid->baw_size / 2;
1408         enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
1409         int prev_al = 0;
1410         INIT_LIST_HEAD(&bf_head);
1411
1412         BUG_ON(list_empty(&tid->buf_q));
1413
1414         bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1415
1416         do {
1417                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1418
1419                 /*
1420                  * do not step over block-ack window
1421                  */
1422                 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1423                         status = ATH_AGGR_BAW_CLOSED;
1424                         break;
1425                 }
1426
1427                 if (!rl) {
1428                         aggr_limit = ath_lookup_rate(sc, bf, tid);
1429                         rl = 1;
1430                 }
1431
1432                 /*
1433                  * do not exceed aggregation limit
1434                  */
1435                 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1436
1437                 if (nframes && (aggr_limit <
1438                         (al + bpad + al_delta + prev_al))) {
1439                         status = ATH_AGGR_LIMITED;
1440                         break;
1441                 }
1442
1443                 /*
1444                  * do not exceed subframe limit
1445                  */
1446                 if ((nframes + *prev_frames) >=
1447                     min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1448                         status = ATH_AGGR_LIMITED;
1449                         break;
1450                 }
1451
1452                 /*
1453                  * add padding for previous frame to aggregation length
1454                  */
1455                 al += bpad + al_delta;
1456
1457                 /*
1458                  * Get the delimiters needed to meet the MPDU
1459                  * density for this node.
1460                  */
1461                 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
1462
1463                 bpad = PADBYTES(al_delta) + (ndelim << 2);
1464
1465                 bf->bf_next = NULL;
1466                 bf->bf_lastfrm->bf_desc->ds_link = 0;
1467
1468                 /*
1469                  * this packet is part of an aggregate
1470                  * - remove all descriptors belonging to this frame from
1471                  *   software queue
1472                  * - add it to block ack window
1473                  * - set up descriptors for aggregation
1474                  */
1475                 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1476                 ath_tx_addto_baw(sc, tid, bf);
1477
1478                 list_for_each_entry(tbf, &bf_head, list) {
1479                         ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1480                                 tbf->bf_desc, ndelim);
1481                 }
1482
1483                 /*
1484                  * link buffers of this frame to the aggregate
1485                  */
1486                 list_splice_tail_init(&bf_head, bf_q);
1487                 nframes++;
1488
1489                 if (bf_prev) {
1490                         bf_prev->bf_next = bf;
1491                         bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1492                 }
1493                 bf_prev = bf;
1494
1495 #ifdef AGGR_NOSHORT
1496                 /*
1497                  * terminate aggregation on a small packet boundary
1498                  */
1499                 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1500                         status = ATH_AGGR_SHORTPKT;
1501                         break;
1502                 }
1503 #endif
1504         } while (!list_empty(&tid->buf_q));
1505
1506         bf_first->bf_al = al;
1507         bf_first->bf_nframes = nframes;
1508         *bf_last = bf_prev;
1509         return status;
1510 #undef PADBYTES
1511 }
1512
1513 /*
1514  * process pending frames possibly doing a-mpdu aggregation
1515  * NB: must be called with txq lock held
1516  */
1517 static void ath_tx_sched_aggr(struct ath_softc *sc,
1518         struct ath_txq *txq, struct ath_atx_tid *tid)
1519 {
1520         struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1521         enum ATH_AGGR_STATUS status;
1522         struct list_head bf_q;
1523         struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1524         int prev_frames = 0;
1525
1526         do {
1527                 if (list_empty(&tid->buf_q))
1528                         return;
1529
1530                 INIT_LIST_HEAD(&bf_q);
1531
1532                 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
1533                                           &prev_frames);
1534
1535                 /*
1536                  * no frames picked up to be aggregated; block-ack
1537                  * window is not open
1538                  */
1539                 if (list_empty(&bf_q))
1540                         break;
1541
1542                 bf = list_first_entry(&bf_q, struct ath_buf, list);
1543                 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1544                 bf->bf_lastbf = bf_last;
1545
1546                 /*
1547                  * if only one frame, send as non-aggregate
1548                  */
1549                 if (bf->bf_nframes == 1) {
1550                         ASSERT(bf->bf_lastfrm == bf_last);
1551
1552                         bf->bf_state.bf_type &= ~BUF_AGGR;
1553                         /*
1554                          * clear aggr bits for every descriptor
1555                          * XXX TODO: is there a way to optimize it?
1556                          */
1557                         list_for_each_entry(tbf, &bf_q, list) {
1558                                 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1559                         }
1560
1561                         ath_buf_set_rate(sc, bf);
1562                         ath_tx_txqaddbuf(sc, txq, &bf_q);
1563                         continue;
1564                 }
1565
1566                 /*
1567                  * setup first desc with rate and aggr info
1568                  */
1569                 bf->bf_state.bf_type |= BUF_AGGR;
1570                 ath_buf_set_rate(sc, bf);
1571                 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1572
1573                 /*
1574                  * anchor last frame of aggregate correctly
1575                  */
1576                 ASSERT(bf_lastaggr);
1577                 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1578                 tbf = bf_lastaggr;
1579                 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1580
1581                 /* XXX: We don't enter into this loop, consider removing this */
1582                 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1583                         tbf = list_entry(tbf->list.next, struct ath_buf, list);
1584                         ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1585                 }
1586
1587                 txq->axq_aggr_depth++;
1588
1589                 /*
1590                  * Normal aggregate, queue to hardware
1591                  */
1592                 ath_tx_txqaddbuf(sc, txq, &bf_q);
1593
1594         } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1595                  status != ATH_AGGR_BAW_CLOSED);
1596 }
1597
1598 /* Called with txq lock held */
1599
1600 static void ath_tid_drain(struct ath_softc *sc,
1601                           struct ath_txq *txq,
1602                           struct ath_atx_tid *tid)
1603
1604 {
1605         struct ath_buf *bf;
1606         struct list_head bf_head;
1607         INIT_LIST_HEAD(&bf_head);
1608
1609         for (;;) {
1610                 if (list_empty(&tid->buf_q))
1611                         break;
1612                 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1613
1614                 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1615
1616                 /* update baw for software retried frame */
1617                 if (bf_isretried(bf))
1618                         ath_tx_update_baw(sc, tid, bf->bf_seqno);
1619
1620                 /*
1621                  * do not indicate packets while holding txq spinlock.
1622                  * unlock is intentional here
1623                  */
1624                 spin_unlock(&txq->axq_lock);
1625
1626                 /* complete this sub-frame */
1627                 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1628
1629                 spin_lock(&txq->axq_lock);
1630         }
1631
1632         /*
1633          * TODO: For frame(s) that are in the retry state, we will reuse the
1634          * sequence number(s) without setting the retry bit. The
1635          * alternative is to give up on these and BAR the receiver's window
1636          * forward.
1637          */
1638         tid->seq_next = tid->seq_start;
1639         tid->baw_tail = tid->baw_head;
1640 }
1641
1642 /*
1643  * Drain all pending buffers
1644  * NB: must be called with txq lock held
1645  */
1646 static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
1647                                           struct ath_txq *txq)
1648 {
1649         struct ath_atx_ac *ac, *ac_tmp;
1650         struct ath_atx_tid *tid, *tid_tmp;
1651
1652         list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1653                 list_del(&ac->list);
1654                 ac->sched = false;
1655                 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1656                         list_del(&tid->list);
1657                         tid->sched = false;
1658                         ath_tid_drain(sc, txq, tid);
1659                 }
1660         }
1661 }
1662
1663 static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
1664                                 struct sk_buff *skb,
1665                                 struct ath_tx_control *txctl)
1666 {
1667         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1668         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1669         struct ath_tx_info_priv *tx_info_priv;
1670         int hdrlen;
1671         __le16 fc;
1672
1673         tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1674         if (unlikely(!tx_info_priv))
1675                 return -ENOMEM;
1676         tx_info->rate_driver_data[0] = tx_info_priv;
1677         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1678         fc = hdr->frame_control;
1679
1680         ATH_TXBUF_RESET(bf);
1681
1682         /* Frame type */
1683
1684         bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
1685
1686         ieee80211_is_data(fc) ?
1687                 (bf->bf_state.bf_type |= BUF_DATA) :
1688                 (bf->bf_state.bf_type &= ~BUF_DATA);
1689         ieee80211_is_back_req(fc) ?
1690                 (bf->bf_state.bf_type |= BUF_BAR) :
1691                 (bf->bf_state.bf_type &= ~BUF_BAR);
1692         ieee80211_is_pspoll(fc) ?
1693                 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1694                 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
1695         (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
1696                 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1697                 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
1698         (sc->hw->conf.ht.enabled && !is_pae(skb) &&
1699          (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1700                 (bf->bf_state.bf_type |= BUF_HT) :
1701                 (bf->bf_state.bf_type &= ~BUF_HT);
1702
1703         bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1704
1705         /* Crypto */
1706
1707         bf->bf_keytype = get_hw_crypto_keytype(skb);
1708
1709         if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1710                 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1711                 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1712         } else {
1713                 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1714         }
1715
1716         /* Assign seqno, tidno */
1717
1718         if (ieee80211_is_data_qos(fc) && (sc->sc_flags & SC_OP_TXAGGR))
1719                 assign_aggr_tid_seqno(skb, bf);
1720
1721         /* DMA setup */
1722         bf->bf_mpdu = skb;
1723
1724         bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1725                                            skb->len, PCI_DMA_TODEVICE);
1726         if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_dmacontext))) {
1727                 bf->bf_mpdu = NULL;
1728                 DPRINTF(sc, ATH_DBG_CONFIG,
1729                         "pci_dma_mapping_error() on TX\n");
1730                 return -ENOMEM;
1731         }
1732
1733         bf->bf_buf_addr = bf->bf_dmacontext;
1734         return 0;
1735 }
1736
1737 /* FIXME: tx power */
1738 static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
1739                              struct ath_tx_control *txctl)
1740 {
1741         struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1742         struct ieee80211_tx_info *tx_info =  IEEE80211_SKB_CB(skb);
1743         struct ath_node *an = NULL;
1744         struct list_head bf_head;
1745         struct ath_desc *ds;
1746         struct ath_atx_tid *tid;
1747         struct ath_hal *ah = sc->sc_ah;
1748         int frm_type;
1749
1750         frm_type = get_hw_packet_type(skb);
1751
1752         INIT_LIST_HEAD(&bf_head);
1753         list_add_tail(&bf->list, &bf_head);
1754
1755         /* setup descriptor */
1756
1757         ds = bf->bf_desc;
1758         ds->ds_link = 0;
1759         ds->ds_data = bf->bf_buf_addr;
1760
1761         /* Formulate first tx descriptor with tx controls */
1762
1763         ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1764                                bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1765
1766         ath9k_hw_filltxdesc(ah, ds,
1767                             skb->len,   /* segment length */
1768                             true,       /* first segment */
1769                             true,       /* last segment */
1770                             ds);        /* first descriptor */
1771
1772         bf->bf_lastfrm = bf;
1773
1774         spin_lock_bh(&txctl->txq->axq_lock);
1775
1776         if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1777             tx_info->control.sta) {
1778                 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1779                 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1780
1781                 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
1782                         /*
1783                          * Try aggregation if it's a unicast data frame
1784                          * and the destination is HT capable.
1785                          */
1786                         ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
1787                 } else {
1788                         /*
1789                          * Send this frame as regular when ADDBA
1790                          * exchange is neither complete nor pending.
1791                          */
1792                         ath_tx_send_normal(sc, txctl->txq,
1793                                            tid, &bf_head);
1794                 }
1795         } else {
1796                 bf->bf_lastbf = bf;
1797                 bf->bf_nframes = 1;
1798
1799                 ath_buf_set_rate(sc, bf);
1800                 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
1801         }
1802
1803         spin_unlock_bh(&txctl->txq->axq_lock);
1804 }
1805
1806 /* Upon failure caller should free skb */
1807 int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1808                  struct ath_tx_control *txctl)
1809 {
1810         struct ath_buf *bf;
1811         int r;
1812
1813         /* Check if a tx buffer is available */
1814
1815         bf = ath_tx_get_buffer(sc);
1816         if (!bf) {
1817                 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
1818                 return -1;
1819         }
1820
1821         r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1822         if (unlikely(r)) {
1823                 struct ath_txq *txq = txctl->txq;
1824
1825                 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
1826
1827                 /* upon ath_tx_processq() this TX queue will be resumed, we
1828                  * guarantee this will happen by knowing beforehand that
1829                  * we will at least have to run TX completionon one buffer
1830                  * on the queue */
1831                 spin_lock_bh(&txq->axq_lock);
1832                 if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
1833                         ieee80211_stop_queue(sc->hw,
1834                                 skb_get_queue_mapping(skb));
1835                         txq->stopped = 1;
1836                 }
1837                 spin_unlock_bh(&txq->axq_lock);
1838
1839                 spin_lock_bh(&sc->tx.txbuflock);
1840                 list_add_tail(&bf->list, &sc->tx.txbuf);
1841                 spin_unlock_bh(&sc->tx.txbuflock);
1842
1843                 return r;
1844         }
1845
1846         ath_tx_start_dma(sc, bf, txctl);
1847
1848         return 0;
1849 }
1850
1851 /* Initialize TX queue and h/w */
1852
1853 int ath_tx_init(struct ath_softc *sc, int nbufs)
1854 {
1855         int error = 0;
1856
1857         do {
1858                 spin_lock_init(&sc->tx.txbuflock);
1859
1860                 /* Setup tx descriptors */
1861                 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
1862                         "tx", nbufs, 1);
1863                 if (error != 0) {
1864                         DPRINTF(sc, ATH_DBG_FATAL,
1865                                 "Failed to allocate tx descriptors: %d\n",
1866                                 error);
1867                         break;
1868                 }
1869
1870                 /* XXX allocate beacon state together with vap */
1871                 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
1872                                           "beacon", ATH_BCBUF, 1);
1873                 if (error != 0) {
1874                         DPRINTF(sc, ATH_DBG_FATAL,
1875                                 "Failed to allocate beacon descriptors: %d\n",
1876                                 error);
1877                         break;
1878                 }
1879
1880         } while (0);
1881
1882         if (error != 0)
1883                 ath_tx_cleanup(sc);
1884
1885         return error;
1886 }
1887
1888 /* Reclaim all tx queue resources */
1889
1890 int ath_tx_cleanup(struct ath_softc *sc)
1891 {
1892         /* cleanup beacon descriptors */
1893         if (sc->beacon.bdma.dd_desc_len != 0)
1894                 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
1895
1896         /* cleanup tx descriptors */
1897         if (sc->tx.txdma.dd_desc_len != 0)
1898                 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
1899
1900         return 0;
1901 }
1902
1903 /* Setup a h/w transmit queue */
1904
1905 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1906 {
1907         struct ath_hal *ah = sc->sc_ah;
1908         struct ath9k_tx_queue_info qi;
1909         int qnum;
1910
1911         memset(&qi, 0, sizeof(qi));
1912         qi.tqi_subtype = subtype;
1913         qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1914         qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1915         qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1916         qi.tqi_physCompBuf = 0;
1917
1918         /*
1919          * Enable interrupts only for EOL and DESC conditions.
1920          * We mark tx descriptors to receive a DESC interrupt
1921          * when a tx queue gets deep; otherwise waiting for the
1922          * EOL to reap descriptors.  Note that this is done to
1923          * reduce interrupt load and this only defers reaping
1924          * descriptors, never transmitting frames.  Aside from
1925          * reducing interrupts this also permits more concurrency.
1926          * The only potential downside is if the tx queue backs
1927          * up in which case the top half of the kernel may backup
1928          * due to a lack of tx descriptors.
1929          *
1930          * The UAPSD queue is an exception, since we take a desc-
1931          * based intr on the EOSP frames.
1932          */
1933         if (qtype == ATH9K_TX_QUEUE_UAPSD)
1934                 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1935         else
1936                 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1937                         TXQ_FLAG_TXDESCINT_ENABLE;
1938         qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1939         if (qnum == -1) {
1940                 /*
1941                  * NB: don't print a message, this happens
1942                  * normally on parts with too few tx queues
1943                  */
1944                 return NULL;
1945         }
1946         if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
1947                 DPRINTF(sc, ATH_DBG_FATAL,
1948                         "qnum %u out of range, max %u!\n",
1949                         qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
1950                 ath9k_hw_releasetxqueue(ah, qnum);
1951                 return NULL;
1952         }
1953         if (!ATH_TXQ_SETUP(sc, qnum)) {
1954                 struct ath_txq *txq = &sc->tx.txq[qnum];
1955
1956                 txq->axq_qnum = qnum;
1957                 txq->axq_link = NULL;
1958                 INIT_LIST_HEAD(&txq->axq_q);
1959                 INIT_LIST_HEAD(&txq->axq_acq);
1960                 spin_lock_init(&txq->axq_lock);
1961                 txq->axq_depth = 0;
1962                 txq->axq_aggr_depth = 0;
1963                 txq->axq_totalqueued = 0;
1964                 txq->axq_linkbuf = NULL;
1965                 sc->tx.txqsetup |= 1<<qnum;
1966         }
1967         return &sc->tx.txq[qnum];
1968 }
1969
1970 /* Reclaim resources for a setup queue */
1971
1972 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1973 {
1974         ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1975         sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1976 }
1977
1978 /*
1979  * Setup a hardware data transmit queue for the specified
1980  * access control.  The hal may not support all requested
1981  * queues in which case it will return a reference to a
1982  * previously setup queue.  We record the mapping from ac's
1983  * to h/w queues for use by ath_tx_start and also track
1984  * the set of h/w queues being used to optimize work in the
1985  * transmit interrupt handler and related routines.
1986  */
1987
1988 int ath_tx_setup(struct ath_softc *sc, int haltype)
1989 {
1990         struct ath_txq *txq;
1991
1992         if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
1993                 DPRINTF(sc, ATH_DBG_FATAL,
1994                         "HAL AC %u out of range, max %zu!\n",
1995                          haltype, ARRAY_SIZE(sc->tx.hwq_map));
1996                 return 0;
1997         }
1998         txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1999         if (txq != NULL) {
2000                 sc->tx.hwq_map[haltype] = txq->axq_qnum;
2001                 return 1;
2002         } else
2003                 return 0;
2004 }
2005
2006 int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
2007 {
2008         int qnum;
2009
2010         switch (qtype) {
2011         case ATH9K_TX_QUEUE_DATA:
2012                 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
2013                         DPRINTF(sc, ATH_DBG_FATAL,
2014                                 "HAL AC %u out of range, max %zu!\n",
2015                                 haltype, ARRAY_SIZE(sc->tx.hwq_map));
2016                         return -1;
2017                 }
2018                 qnum = sc->tx.hwq_map[haltype];
2019                 break;
2020         case ATH9K_TX_QUEUE_BEACON:
2021                 qnum = sc->beacon.beaconq;
2022                 break;
2023         case ATH9K_TX_QUEUE_CAB:
2024                 qnum = sc->beacon.cabq->axq_qnum;
2025                 break;
2026         default:
2027                 qnum = -1;
2028         }
2029         return qnum;
2030 }
2031
2032 /* Get a transmit queue, if available */
2033
2034 struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2035 {
2036         struct ath_txq *txq = NULL;
2037         int qnum;
2038
2039         qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
2040         txq = &sc->tx.txq[qnum];
2041
2042         spin_lock_bh(&txq->axq_lock);
2043
2044         /* Try to avoid running out of descriptors */
2045         if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2046                 DPRINTF(sc, ATH_DBG_FATAL,
2047                         "TX queue: %d is full, depth: %d\n",
2048                         qnum, txq->axq_depth);
2049                 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2050                 txq->stopped = 1;
2051                 spin_unlock_bh(&txq->axq_lock);
2052                 return NULL;
2053         }
2054
2055         spin_unlock_bh(&txq->axq_lock);
2056
2057         return txq;
2058 }
2059
2060 /* Update parameters for a transmit queue */
2061
2062 int ath_txq_update(struct ath_softc *sc, int qnum,
2063                    struct ath9k_tx_queue_info *qinfo)
2064 {
2065         struct ath_hal *ah = sc->sc_ah;
2066         int error = 0;
2067         struct ath9k_tx_queue_info qi;
2068
2069         if (qnum == sc->beacon.beaconq) {
2070                 /*
2071                  * XXX: for beacon queue, we just save the parameter.
2072                  * It will be picked up by ath_beaconq_config when
2073                  * it's necessary.
2074                  */
2075                 sc->beacon.beacon_qi = *qinfo;
2076                 return 0;
2077         }
2078
2079         ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
2080
2081         ath9k_hw_get_txq_props(ah, qnum, &qi);
2082         qi.tqi_aifs = qinfo->tqi_aifs;
2083         qi.tqi_cwmin = qinfo->tqi_cwmin;
2084         qi.tqi_cwmax = qinfo->tqi_cwmax;
2085         qi.tqi_burstTime = qinfo->tqi_burstTime;
2086         qi.tqi_readyTime = qinfo->tqi_readyTime;
2087
2088         if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
2089                 DPRINTF(sc, ATH_DBG_FATAL,
2090                         "Unable to update hardware queue %u!\n", qnum);
2091                 error = -EIO;
2092         } else {
2093                 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2094         }
2095
2096         return error;
2097 }
2098
2099 int ath_cabq_update(struct ath_softc *sc)
2100 {
2101         struct ath9k_tx_queue_info qi;
2102         int qnum = sc->beacon.cabq->axq_qnum;
2103         struct ath_beacon_config conf;
2104
2105         ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
2106         /*
2107          * Ensure the readytime % is within the bounds.
2108          */
2109         if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2110                 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2111         else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2112                 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2113
2114         ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2115         qi.tqi_readyTime =
2116                 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2117         ath_txq_update(sc, qnum, &qi);
2118
2119         return 0;
2120 }
2121
2122 /* Deferred processing of transmit interrupt */
2123
2124 void ath_tx_tasklet(struct ath_softc *sc)
2125 {
2126         int i;
2127         u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2128
2129         ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2130
2131         /*
2132          * Process each active queue.
2133          */
2134         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2135                 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2136                         ath_tx_processq(sc, &sc->tx.txq[i]);
2137         }
2138 }
2139
2140 void ath_tx_draintxq(struct ath_softc *sc,
2141         struct ath_txq *txq, bool retry_tx)
2142 {
2143         struct ath_buf *bf, *lastbf;
2144         struct list_head bf_head;
2145
2146         INIT_LIST_HEAD(&bf_head);
2147
2148         /*
2149          * NB: this assumes output has been stopped and
2150          *     we do not need to block ath_tx_tasklet
2151          */
2152         for (;;) {
2153                 spin_lock_bh(&txq->axq_lock);
2154
2155                 if (list_empty(&txq->axq_q)) {
2156                         txq->axq_link = NULL;
2157                         txq->axq_linkbuf = NULL;
2158                         spin_unlock_bh(&txq->axq_lock);
2159                         break;
2160                 }
2161
2162                 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2163
2164                 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2165                         list_del(&bf->list);
2166                         spin_unlock_bh(&txq->axq_lock);
2167
2168                         spin_lock_bh(&sc->tx.txbuflock);
2169                         list_add_tail(&bf->list, &sc->tx.txbuf);
2170                         spin_unlock_bh(&sc->tx.txbuflock);
2171                         continue;
2172                 }
2173
2174                 lastbf = bf->bf_lastbf;
2175                 if (!retry_tx)
2176                         lastbf->bf_desc->ds_txstat.ts_flags =
2177                                 ATH9K_TX_SW_ABORTED;
2178
2179                 /* remove ath_buf's of the same mpdu from txq */
2180                 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2181                 txq->axq_depth--;
2182
2183                 spin_unlock_bh(&txq->axq_lock);
2184
2185                 if (bf_isampdu(bf))
2186                         ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2187                 else
2188                         ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2189         }
2190
2191         /* flush any pending frames if aggregation is enabled */
2192         if (sc->sc_flags & SC_OP_TXAGGR) {
2193                 if (!retry_tx) {
2194                         spin_lock_bh(&txq->axq_lock);
2195                         ath_txq_drain_pending_buffers(sc, txq);
2196                         spin_unlock_bh(&txq->axq_lock);
2197                 }
2198         }
2199 }
2200
2201 /* Drain the transmit queues and reclaim resources */
2202
2203 void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2204 {
2205         /* stop beacon queue. The beacon will be freed when
2206          * we go to INIT state */
2207         if (!(sc->sc_flags & SC_OP_INVALID)) {
2208                 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
2209                 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
2210                         ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
2211         }
2212
2213         ath_drain_txdataq(sc, retry_tx);
2214 }
2215
2216 u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2217 {
2218         return sc->tx.txq[qnum].axq_depth;
2219 }
2220
2221 u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2222 {
2223         return sc->tx.txq[qnum].axq_aggr_depth;
2224 }
2225
2226 bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
2227 {
2228         struct ath_atx_tid *txtid;
2229
2230         if (!(sc->sc_flags & SC_OP_TXAGGR))
2231                 return false;
2232
2233         txtid = ATH_AN_2_TID(an, tidno);
2234
2235         if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2236                 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
2237                     (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2238                         txtid->addba_exchangeattempts++;
2239                         return true;
2240                 }
2241         }
2242
2243         return false;
2244 }
2245
2246 /* Start TX aggregation */
2247
2248 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2249                       u16 tid, u16 *ssn)
2250 {
2251         struct ath_atx_tid *txtid;
2252         struct ath_node *an;
2253
2254         an = (struct ath_node *)sta->drv_priv;
2255
2256         if (sc->sc_flags & SC_OP_TXAGGR) {
2257                 txtid = ATH_AN_2_TID(an, tid);
2258                 txtid->state |= AGGR_ADDBA_PROGRESS;
2259                 ath_tx_pause_tid(sc, txtid);
2260         }
2261
2262         return 0;
2263 }
2264
2265 /* Stop tx aggregation */
2266
2267 int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2268 {
2269         struct ath_node *an = (struct ath_node *)sta->drv_priv;
2270
2271         ath_tx_aggr_teardown(sc, an, tid);
2272         return 0;
2273 }
2274
2275 /* Resume tx aggregation */
2276
2277 void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2278 {
2279         struct ath_atx_tid *txtid;
2280         struct ath_node *an;
2281
2282         an = (struct ath_node *)sta->drv_priv;
2283
2284         if (sc->sc_flags & SC_OP_TXAGGR) {
2285                 txtid = ATH_AN_2_TID(an, tid);
2286                 txtid->baw_size =
2287                         IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2288                 txtid->state |= AGGR_ADDBA_COMPLETE;
2289                 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2290                 ath_tx_resume_tid(sc, txtid);
2291         }
2292 }
2293
2294 /*
2295  * Performs transmit side cleanup when TID changes from aggregated to
2296  * unaggregated.
2297  * - Pause the TID and mark cleanup in progress
2298  * - Discard all retry frames from the s/w queue.
2299  */
2300
2301 void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
2302 {
2303         struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
2304         struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
2305         struct ath_buf *bf;
2306         struct list_head bf_head;
2307         INIT_LIST_HEAD(&bf_head);
2308
2309         if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
2310                 return;
2311
2312         if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2313                 txtid->addba_exchangeattempts = 0;
2314                 return;
2315         }
2316
2317         /* TID must be paused first */
2318         ath_tx_pause_tid(sc, txtid);
2319
2320         /* drop all software retried frames and mark this TID */
2321         spin_lock_bh(&txq->axq_lock);
2322         while (!list_empty(&txtid->buf_q)) {
2323                 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
2324                 if (!bf_isretried(bf)) {
2325                         /*
2326                          * NB: it's based on the assumption that
2327                          * software retried frame will always stay
2328                          * at the head of software queue.
2329                          */
2330                         break;
2331                 }
2332                 list_cut_position(&bf_head,
2333                         &txtid->buf_q, &bf->bf_lastfrm->list);
2334                 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2335
2336                 /* complete this sub-frame */
2337                 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2338         }
2339
2340         if (txtid->baw_head != txtid->baw_tail) {
2341                 spin_unlock_bh(&txq->axq_lock);
2342                 txtid->state |= AGGR_CLEANUP;
2343         } else {
2344                 txtid->state &= ~AGGR_ADDBA_COMPLETE;
2345                 txtid->addba_exchangeattempts = 0;
2346                 spin_unlock_bh(&txq->axq_lock);
2347                 ath_tx_flush_tid(sc, txtid);
2348         }
2349 }
2350
2351 /*
2352  * Tx scheduling logic
2353  * NB: must be called with txq lock held
2354  */
2355
2356 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2357 {
2358         struct ath_atx_ac *ac;
2359         struct ath_atx_tid *tid;
2360
2361         /* nothing to schedule */
2362         if (list_empty(&txq->axq_acq))
2363                 return;
2364         /*
2365          * get the first node/ac pair on the queue
2366          */
2367         ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2368         list_del(&ac->list);
2369         ac->sched = false;
2370
2371         /*
2372          * process a single tid per destination
2373          */
2374         do {
2375                 /* nothing to schedule */
2376                 if (list_empty(&ac->tid_q))
2377                         return;
2378
2379                 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2380                 list_del(&tid->list);
2381                 tid->sched = false;
2382
2383                 if (tid->paused)    /* check next tid to keep h/w busy */
2384                         continue;
2385
2386                 if ((txq->axq_depth % 2) == 0)
2387                         ath_tx_sched_aggr(sc, txq, tid);
2388
2389                 /*
2390                  * add tid to round-robin queue if more frames
2391                  * are pending for the tid
2392                  */
2393                 if (!list_empty(&tid->buf_q))
2394                         ath_tx_queue_tid(txq, tid);
2395
2396                 /* only schedule one TID at a time */
2397                 break;
2398         } while (!list_empty(&ac->tid_q));
2399
2400         /*
2401          * schedule AC if more TIDs need processing
2402          */
2403         if (!list_empty(&ac->tid_q)) {
2404                 /*
2405                  * add dest ac to txq if not already added
2406                  */
2407                 if (!ac->sched) {
2408                         ac->sched = true;
2409                         list_add_tail(&ac->list, &txq->axq_acq);
2410                 }
2411         }
2412 }
2413
2414 /* Initialize per-node transmit state */
2415
2416 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2417 {
2418         struct ath_atx_tid *tid;
2419         struct ath_atx_ac *ac;
2420         int tidno, acno;
2421
2422         /*
2423          * Init per tid tx state
2424          */
2425         for (tidno = 0, tid = &an->tid[tidno];
2426              tidno < WME_NUM_TID;
2427              tidno++, tid++) {
2428                 tid->an        = an;
2429                 tid->tidno     = tidno;
2430                 tid->seq_start = tid->seq_next = 0;
2431                 tid->baw_size  = WME_MAX_BA;
2432                 tid->baw_head  = tid->baw_tail = 0;
2433                 tid->sched     = false;
2434                 tid->paused = false;
2435                 tid->state &= ~AGGR_CLEANUP;
2436                 INIT_LIST_HEAD(&tid->buf_q);
2437
2438                 acno = TID_TO_WME_AC(tidno);
2439                 tid->ac = &an->ac[acno];
2440
2441                 /* ADDBA state */
2442                 tid->state &= ~AGGR_ADDBA_COMPLETE;
2443                 tid->state &= ~AGGR_ADDBA_PROGRESS;
2444                 tid->addba_exchangeattempts = 0;
2445         }
2446
2447         /*
2448          * Init per ac tx state
2449          */
2450         for (acno = 0, ac = &an->ac[acno];
2451              acno < WME_NUM_AC; acno++, ac++) {
2452                 ac->sched    = false;
2453                 INIT_LIST_HEAD(&ac->tid_q);
2454
2455                 switch (acno) {
2456                 case WME_AC_BE:
2457                         ac->qnum = ath_tx_get_qnum(sc,
2458                                    ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2459                         break;
2460                 case WME_AC_BK:
2461                         ac->qnum = ath_tx_get_qnum(sc,
2462                                    ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2463                         break;
2464                 case WME_AC_VI:
2465                         ac->qnum = ath_tx_get_qnum(sc,
2466                                    ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2467                         break;
2468                 case WME_AC_VO:
2469                         ac->qnum = ath_tx_get_qnum(sc,
2470                                    ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2471                         break;
2472                 }
2473         }
2474 }
2475
2476 /* Cleanupthe pending buffers for the node. */
2477
2478 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2479 {
2480         int i;
2481         struct ath_atx_ac *ac, *ac_tmp;
2482         struct ath_atx_tid *tid, *tid_tmp;
2483         struct ath_txq *txq;
2484         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2485                 if (ATH_TXQ_SETUP(sc, i)) {
2486                         txq = &sc->tx.txq[i];
2487
2488                         spin_lock(&txq->axq_lock);
2489
2490                         list_for_each_entry_safe(ac,
2491                                         ac_tmp, &txq->axq_acq, list) {
2492                                 tid = list_first_entry(&ac->tid_q,
2493                                                 struct ath_atx_tid, list);
2494                                 if (tid && tid->an != an)
2495                                         continue;
2496                                 list_del(&ac->list);
2497                                 ac->sched = false;
2498
2499                                 list_for_each_entry_safe(tid,
2500                                                 tid_tmp, &ac->tid_q, list) {
2501                                         list_del(&tid->list);
2502                                         tid->sched = false;
2503                                         ath_tid_drain(sc, txq, tid);
2504                                         tid->state &= ~AGGR_ADDBA_COMPLETE;
2505                                         tid->addba_exchangeattempts = 0;
2506                                         tid->state &= ~AGGR_CLEANUP;
2507                                 }
2508                         }
2509
2510                         spin_unlock(&txq->axq_lock);
2511                 }
2512         }
2513 }
2514
2515 void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2516 {
2517         int hdrlen, padsize;
2518         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2519         struct ath_tx_control txctl;
2520
2521         memset(&txctl, 0, sizeof(struct ath_tx_control));
2522
2523         /*
2524          * As a temporary workaround, assign seq# here; this will likely need
2525          * to be cleaned up to work better with Beacon transmission and virtual
2526          * BSSes.
2527          */
2528         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2529                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2530                 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2531                         sc->tx.seq_no += 0x10;
2532                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2533                 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
2534         }
2535
2536         /* Add the padding after the header if this is not already done */
2537         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2538         if (hdrlen & 3) {
2539                 padsize = hdrlen % 4;
2540                 if (skb_headroom(skb) < padsize) {
2541                         DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
2542                         dev_kfree_skb_any(skb);
2543                         return;
2544                 }
2545                 skb_push(skb, padsize);
2546                 memmove(skb->data, skb->data + padsize, hdrlen);
2547         }
2548
2549         txctl.txq = sc->beacon.cabq;
2550
2551         DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
2552
2553         if (ath_tx_start(sc, skb, &txctl) != 0) {
2554                 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
2555                 goto exit;
2556         }
2557
2558         return;
2559 exit:
2560         dev_kfree_skb_any(skb);
2561 }