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