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