b1d806fe567f1b2085108227f4fb45a1b735ad7b
[open-ath9k-htc-firmware.git] / target_firmware / wlan / if_owl.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <adf_os_types.h>
37 #include <adf_os_dma.h>
38 #include <adf_os_timer.h>
39 #include <adf_os_lock.h>
40 #include <adf_os_io.h>
41 #include <adf_os_mem.h>
42 #include <adf_os_util.h>
43 #include <adf_os_stdtypes.h>
44 #include <adf_os_defer.h>
45 #include <adf_os_atomic.h>
46 #include <adf_nbuf.h>
47 #include <adf_net.h>
48 #include <adf_net_wcmd.h>
49
50 #include "if_llc.h"
51
52 #ifdef USE_HEADERLEN_RESV
53 #include <if_llc.h>
54 #endif
55
56 #include <ieee80211_var.h>
57 #include "if_athrate.h"
58 #include "if_athvar.h"
59 #include "ah_desc.h"
60
61 #define ath_tgt_free_skb  adf_nbuf_free
62
63 #define OFDM_PLCP_BITS          22
64 #define HT_RC_2_MCS(_rc)        ((_rc) & 0x0f)
65 #define HT_RC_2_STREAMS(_rc)    ((((_rc) & 0x78) >> 3) + 1)
66 #define L_STF                   8
67 #define L_LTF                   8
68 #define L_SIG                   4
69 #define HT_SIG                  8
70 #define HT_STF                  4
71 #define HT_LTF(_ns)             (4 * (_ns))
72 #define SYMBOL_TIME(_ns)        ((_ns) << 2)            // ns * 4 us
73 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)  // ns * 3.6 us
74
75 static a_uint16_t bits_per_symbol[][2] = {
76         /* 20MHz 40MHz */
77         {    26,   54 },     //  0: BPSK
78         {    52,  108 },     //  1: QPSK 1/2
79         {    78,  162 },     //  2: QPSK 3/4
80         {   104,  216 },     //  3: 16-QAM 1/2
81         {   156,  324 },     //  4: 16-QAM 3/4
82         {   208,  432 },     //  5: 64-QAM 2/3
83         {   234,  486 },     //  6: 64-QAM 3/4
84         {   260,  540 },     //  7: 64-QAM 5/6
85         {    52,  108 },     //  8: BPSK
86         {   104,  216 },     //  9: QPSK 1/2
87         {   156,  324 },     // 10: QPSK 3/4
88         {   208,  432 },     // 11: 16-QAM 1/2
89         {   312,  648 },     // 12: 16-QAM 3/4
90         {   416,  864 },     // 13: 64-QAM 2/3
91         {   468,  972 },     // 14: 64-QAM 3/4
92         {   520, 1080 },     // 15: 64-QAM 5/6
93 };
94
95 void owltgt_tx_processq(struct ath_softc_tgt *sc, struct ath_txq *txq,
96                         owl_txq_state_t txqstate);
97 static void ath_tgt_txqaddbuf(struct ath_softc_tgt *sc, struct ath_txq *txq,
98                               struct ath_tx_buf *bf,  struct ath_tx_desc *lastds);
99 void ath_rate_findrate_11n_Hardcoded(struct ath_softc_tgt *sc,
100                                      struct ath_rc_series series[]);
101 void ath_buf_set_rate_Hardcoded(struct ath_softc_tgt *sc,
102                                 struct ath_tx_buf *bf) ;
103 static a_int32_t ath_tgt_txbuf_setup(struct ath_softc_tgt *sc,
104                                      struct ath_tx_buf *bf, ath_data_hdr_t *dh);
105 static void ath_tx_freebuf(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
106 static void ath_tx_uc_comp(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
107 static void ath_update_stats(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
108 void adf_print_buf(adf_nbuf_t buf);
109 static void ath_tgt_tx_enqueue(struct ath_txq *txq, struct ath_atx_tid  *tid);
110
111 void ath_tgt_tx_comp_aggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
112 struct ieee80211_frame *ATH_SKB_2_WH(adf_nbuf_t skb);
113
114 void ath_tgt_tx_send_normal(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
115
116 static void ath_tgt_tx_sched_normal(struct ath_softc_tgt *sc, ath_atx_tid_t *tid);
117 static void ath_tgt_tx_sched_aggr(struct ath_softc_tgt *sc, ath_atx_tid_t *tid);
118
119 extern a_int32_t ath_chainmask_sel_logic(void *);
120 static a_int32_t ath_get_pktlen(struct ath_tx_buf *bf, a_int32_t hdrlen);
121 static void ath_tgt_txq_schedule(struct ath_softc_tgt *sc, struct ath_txq *txq);
122
123 typedef void (*ath_ft_set_atype_t)(struct ath_softc_tgt *sc, struct ath_buf *bf);
124
125 static void
126 ath_tx_set_retry(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
127
128 static void
129 ath_bar_tx(struct ath_softc_tgt *sc, ath_atx_tid_t *tid, struct ath_tx_buf *bf);
130 static void
131 ath_tx_update_baw(ath_atx_tid_t *tid, int seqno);
132 static  void
133 ath_tx_retry_subframe(struct ath_softc_tgt *sc, struct ath_tx_buf *bf,
134                       ath_tx_bufhead *bf_q, struct ath_tx_buf **bar);
135
136 static void
137 ath_tx_comp_aggr_error(struct ath_softc_tgt *sc, struct ath_tx_buf *bf, ath_atx_tid_t *tid);
138
139 void ath_tx_addto_baw(ath_atx_tid_t *tid, struct ath_tx_buf *bf);
140 static inline void ath_tx_retry_unaggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
141 static void ath_tx_comp_unaggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
142 static void ath_update_aggr_stats(struct ath_softc_tgt *sc, struct ath_tx_desc *ds,
143                                   int nframes, int nbad);
144 static inline void ath_aggr_resume_tid(struct ath_softc_tgt *sc, ath_atx_tid_t *tid);
145 static void ath_tx_comp_cleanup(struct ath_softc_tgt *sc, struct ath_tx_buf *bf);
146
147 int ath_tgt_tx_add_to_aggr(struct ath_softc_tgt *sc,
148                            struct ath_buf *bf,int datatype,
149                            ath_atx_tid_t *tid, int is_burst);
150
151 struct ieee80211_frame *ATH_SKB_2_WH(adf_nbuf_t skb)
152 {
153         a_uint8_t *anbdata;
154         a_uint32_t anblen;
155
156         adf_nbuf_peek_header(skb, &anbdata, &anblen);
157
158         return((struct ieee80211_frame *)anbdata);
159 }
160
161 #undef adf_os_cpu_to_le16
162
163 static a_uint16_t adf_os_cpu_to_le16(a_uint16_t x)
164 {
165         return ((((x) & 0xff00) >> 8) |   (((x) & 0x00ff) << 8));
166 }
167
168 static inline void
169 ath_aggr_resume_tid(struct ath_softc_tgt *sc, ath_atx_tid_t *tid)
170 {
171         struct ath_txq *txq;
172
173         txq = TID_TO_ACTXQ(tid->tidno);
174         tid->paused = 0;
175
176         if (asf_tailq_empty(&tid->buf_q))
177                 return;
178
179         ath_tgt_tx_enqueue(txq, tid);
180         ath_tgt_txq_schedule(sc, txq);
181 }
182
183 static inline void
184 ath_aggr_pause_tid(struct ath_softc_tgt *sc, ath_atx_tid_t *tid)
185 {
186         tid->paused =1;
187 }
188
189 static a_uint32_t ath_pkt_duration(struct ath_softc_tgt *sc,
190                                    a_uint8_t rix, struct ath_tx_buf *bf,
191                                    a_int32_t width, a_int32_t half_gi)
192 {
193         const HAL_RATE_TABLE *rt = sc->sc_currates;
194         a_uint32_t nbits, nsymbits, duration, nsymbols;
195         a_uint8_t rc;
196         a_int32_t streams;
197         a_int32_t pktlen;
198
199         pktlen = bf->bf_isaggr ? bf->bf_al : bf->bf_pktlen;
200         rc = rt->info[rix].rateCode;
201
202         if (!IS_HT_RATE(rc))
203                 return ath_hal_computetxtime(sc->sc_ah, rt, pktlen, rix,
204                                              bf->bf_shpream);
205
206         nbits = (pktlen << 3) + OFDM_PLCP_BITS;
207         nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
208         nsymbols = (nbits + nsymbits - 1) / nsymbits;
209
210         if (!half_gi)
211                 duration = SYMBOL_TIME(nsymbols);
212         else
213                 duration = SYMBOL_TIME_HALFGI(nsymbols);
214
215         streams = HT_RC_2_STREAMS(rc);
216         duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
217
218         return duration;
219 }
220
221 static void ath_dma_map(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
222 {
223         adf_nbuf_t skb = bf->bf_skb;
224
225         skb = adf_nbuf_queue_first(&bf->bf_skbhead);
226         adf_nbuf_map(sc->sc_dev, bf->bf_dmamap, skb, ADF_OS_DMA_TO_DEVICE);
227 }
228
229 static void ath_dma_unmap(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
230 {
231         adf_nbuf_t skb = bf->bf_skb;
232
233         skb = adf_nbuf_queue_first(&bf->bf_skbhead);
234         adf_nbuf_unmap( sc->sc_dev, bf->bf_dmamap, ADF_OS_DMA_TO_DEVICE);
235 }
236
237 static void ath_filltxdesc(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
238 {
239         struct ath_tx_desc *ds0, *ds = bf->bf_desc;
240         struct ath_hal *ah = sc->sc_ah;
241         a_uint8_t i;
242
243         ds0 = ds;
244         adf_nbuf_dmamap_info(bf->bf_dmamap, &bf->bf_dmamap_info);
245
246         for (i = 0; i < bf->bf_dmamap_info.nsegs; i++, ds++) {
247
248                 ds->ds_data = bf->bf_dmamap_info.dma_segs[i].paddr;
249
250                 if (i == (bf->bf_dmamap_info.nsegs - 1)) {
251                         ds->ds_link = 0;
252                         bf->bf_lastds = ds;
253                 } else
254                         ds->ds_link = ATH_BUF_GET_DESC_PHY_ADDR_WITH_IDX(bf, i+1);
255
256                 ah->ah_fillTxDesc(ds
257                                    , bf->bf_dmamap_info.dma_segs[i].len
258                                    , i == 0
259                                    , i == (bf->bf_dmamap_info.nsegs - 1)
260                                    , ds0);
261         }
262 }
263
264 static void ath_tx_tgt_setds(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
265 {
266         struct ath_tx_desc *ds = bf->bf_desc;
267         struct ath_hal *ah = sc->sc_ah;
268
269         switch (bf->bf_protmode) {
270         case IEEE80211_PROT_RTSCTS:
271                 bf->bf_flags |= HAL_TXDESC_RTSENA;
272                 break;
273         case IEEE80211_PROT_CTSONLY:
274                 bf->bf_flags |= HAL_TXDESC_CTSENA;
275                 break;
276         default:
277                 break;
278         }
279
280         ah->ah_set11nTxDesc(ah, ds
281                               , bf->bf_pktlen
282                               , bf->bf_atype
283                               , 60
284                               , bf->bf_keyix
285                               , bf->bf_keytype
286                               , bf->bf_flags | HAL_TXDESC_INTREQ);
287
288         ath_filltxdesc(sc, bf);
289 }
290
291 static struct ath_tx_buf *ath_buf_toggle(struct ath_softc_tgt *sc,
292                                       struct ath_tx_buf *bf,
293                                       a_uint8_t retry)
294 {
295         struct ath_tx_buf *tmp = NULL;
296         adf_nbuf_t buf = NULL;
297
298         adf_os_assert(sc->sc_txbuf_held != NULL);
299
300         tmp = sc->sc_txbuf_held;
301
302         if (retry) {
303                 ath_dma_unmap(sc, bf);
304                 adf_nbuf_queue_init(&tmp->bf_skbhead);
305                 buf = adf_nbuf_queue_remove(&bf->bf_skbhead);
306                 adf_os_assert(buf);
307                 adf_nbuf_queue_add(&tmp->bf_skbhead, buf);
308
309                 adf_os_assert(adf_nbuf_queue_len(&bf->bf_skbhead) == 0);
310
311                 tmp->bf_next = bf->bf_next;
312                 tmp->bf_endpt = bf->bf_endpt;
313                 tmp->bf_tidno = bf->bf_tidno;
314                 tmp->bf_skb = bf->bf_skb;
315                 tmp->bf_node = bf->bf_node;
316                 tmp->bf_isaggr = bf->bf_isaggr;
317                 tmp->bf_flags = bf->bf_flags;
318                 tmp->bf_state = bf->bf_state;
319                 tmp->bf_retries = bf->bf_retries;
320                 tmp->bf_comp = bf->bf_comp;
321                 tmp->bf_nframes = bf->bf_nframes;
322                 tmp->bf_cookie = bf->bf_cookie;
323
324                 bf->bf_isaggr = 0;
325                 bf->bf_next = NULL;
326                 bf->bf_skb = NULL;
327                 bf->bf_node = NULL;
328                 bf->bf_flags = 0;
329                 bf->bf_comp = NULL;
330
331                 bf->bf_retries = 0;
332                 bf->bf_nframes = 0;
333
334                 ath_dma_map(sc, tmp);
335                 ath_tx_tgt_setds(sc, tmp);
336         }
337
338         sc->sc_txbuf_held = bf;
339
340         return tmp;
341 }
342
343 static void ath_tgt_skb_free(struct ath_softc_tgt *sc,
344                              adf_nbuf_queue_t *head,
345                              HTC_ENDPOINT_ID endpt)
346 {
347         adf_nbuf_t tskb;
348
349         while (adf_nbuf_queue_len(head) != 0) {
350                 tskb = adf_nbuf_queue_remove(head);
351                 ath_free_tx_skb(sc->tgt_htc_handle,endpt,tskb);
352         }
353 }
354
355 static void ath_buf_comp(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
356 {
357         ath_dma_unmap(sc, bf);
358         ath_tgt_skb_free(sc, &bf->bf_skbhead,bf->bf_endpt);
359         bf->bf_skb = NULL;
360         bf->bf_node = NULL;
361         bf = ath_buf_toggle(sc, bf, 0);
362         if (bf != NULL) {
363                 asf_tailq_insert_tail(&sc->sc_txbuf, bf, bf_list);
364         }
365 }
366
367 static void ath_buf_set_rate(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
368 {
369     struct ath_hal *ah = sc->sc_ah;
370     const HAL_RATE_TABLE *rt;
371     struct ath_tx_desc *ds = bf->bf_desc;
372     HAL_11N_RATE_SERIES series[4];
373     a_int32_t i, flags;
374     a_uint8_t rix, cix, rtsctsrate;
375     a_uint32_t ctsduration = 0;
376     a_int32_t prot_mode = AH_FALSE;
377
378     rt = sc->sc_currates;
379     rix = bf->bf_rcs[0].rix;
380     flags = (bf->bf_flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA));
381     cix = rt->info[sc->sc_protrix].controlRate;
382
383     if (bf->bf_protmode != IEEE80211_PROT_NONE &&
384         (rt->info[rix].phy == IEEE80211_T_OFDM ||
385          rt->info[rix].phy == IEEE80211_T_HT) &&
386         (bf->bf_flags & HAL_TXDESC_NOACK) == 0) {
387             cix = rt->info[sc->sc_protrix].controlRate;
388             prot_mode = AH_TRUE;
389     } else {
390             if (ath_hal_getcapability(ah, HAL_CAP_HT) && (!bf->bf_ismcast))
391                     flags = HAL_TXDESC_RTSENA;
392
393             for (i = 4; i--;) {
394                     if (bf->bf_rcs[i].tries) {
395                             cix = rt->info[bf->bf_rcs[i].rix].controlRate;
396                             break;
397                     }
398
399             }
400     }
401
402     adf_os_mem_set(series, 0, sizeof(HAL_11N_RATE_SERIES) * 4);
403
404     for (i = 0; i < 4; i++) {
405             if (!bf->bf_rcs[i].tries)
406                     continue;
407
408             rix = bf->bf_rcs[i].rix;
409
410             series[i].Rate = rt->info[rix].rateCode |
411                     (bf->bf_shpream ? rt->info[rix].shortPreamble : 0);
412
413             series[i].Tries = bf->bf_rcs[i].tries;
414 #ifdef MAGPIE_MERLIN
415             series[i].RateFlags = ((bf->bf_rcs[i].flags & ATH_RC_RTSCTS_FLAG) ?
416                                    HAL_RATESERIES_RTS_CTS : 0 ) |
417                     ((bf->bf_rcs[i].flags & ATH_RC_CW40_FLAG) ?
418                      HAL_RATESERIES_2040 : 0 ) |
419                     ((bf->bf_rcs[i].flags & ATH_RC_HT40_SGI_FLAG) ?
420                      HAL_RATESERIES_HALFGI : 0 ) |
421                     ((bf->bf_rcs[i].flags & ATH_RC_TX_STBC_FLAG) ?
422                      HAL_RATESERIES_STBC: 0);
423 #else
424             series[i].RateFlags = ((bf->bf_rcs[i].flags & ATH_RC_RTSCTS_FLAG) ?
425                                    HAL_RATESERIES_RTS_CTS : 0 ) |
426                     ((bf->bf_rcs[i].flags & ATH_RC_CW40_FLAG) ?
427                      HAL_RATESERIES_2040 : 0 ) |
428                     ((bf->bf_rcs[i].flags & ATH_RC_HT40_SGI_FLAG) ?
429                      HAL_RATESERIES_HALFGI : 0 );
430 #endif
431             series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
432                                      (bf->bf_rcs[i].flags & ATH_RC_CW40_FLAG) != 0,
433                                      (bf->bf_rcs[i].flags & ATH_RC_HT40_SGI_FLAG));
434
435             series[i].ChSel = sc->sc_ic.ic_tx_chainmask;
436
437             if (prot_mode)
438                     series[i].RateFlags |= HAL_RATESERIES_RTS_CTS;
439
440             if (bf->bf_rcs[i].flags & ATH_RC_DS_FLAG)
441                     series[i].RateFlags |= HAL_RATESERIES_RTS_CTS;
442     }
443
444     rtsctsrate = rt->info[cix].rateCode |
445             (bf->bf_shpream ? rt->info[cix].shortPreamble : 0);
446
447     ah->ah_set11nRateScenario(ah, ds, 1,
448                                 rtsctsrate, ctsduration,
449                                 series, 4,
450                                 flags);
451 }
452
453 static void ath_tgt_rate_findrate(struct ath_softc_tgt *sc,
454                                   struct ath_node_target *an,
455                                   a_int32_t shortPreamble,
456                                   size_t frameLen,
457                                   a_int32_t numTries,
458                                   a_int32_t numRates,
459                                   a_int32_t stepDnInc,
460                                   a_uint32_t rcflag,
461                                   struct ath_rc_series series[],
462                                   a_int32_t *isProbe)
463 {
464         ath_rate_findrate(sc, an, 1, frameLen, 10, 4, 1,
465                           ATH_RC_PROBE_ALLOWED, series, isProbe);
466 }
467
468 static void owl_tgt_tid_init(struct ath_atx_tid *tid)
469 {
470         int i;
471
472         tid->seq_start  = tid->seq_next = 0;
473         tid->baw_size   = WME_MAX_BA;
474         tid->baw_head   = tid->baw_tail = 0;
475         tid->paused     = 0;
476         tid->flag       = 0;
477         tid->sched      = AH_FALSE;
478
479         asf_tailq_init(&tid->buf_q);
480
481         for (i = 0; i < ATH_TID_MAX_BUFS; i++) {
482                 TX_BUF_BITMAP_CLR(tid->tx_buf_bitmap, i);
483         }
484 }
485
486 static void owl_tgt_tid_cleanup(struct ath_softc_tgt *sc,
487                                 struct ath_atx_tid *tid)
488 {
489         tid->incomp--;
490         if (tid->incomp) {
491                 return;
492         }
493
494         tid->flag &= ~TID_CLEANUP_INPROGRES;
495
496         if (tid->flag & TID_REINITIALIZE) {
497                 adf_os_print("TID REINIT DONE for tid %p\n", tid);
498                 tid->flag  &= ~TID_REINITIALIZE;
499                 owl_tgt_tid_init(tid);
500         } else {
501                 ath_aggr_resume_tid(sc, tid);
502         }
503 }
504
505 void owl_tgt_node_init(struct ath_node_target * an)
506 {
507         struct ath_atx_tid *tid;
508         int tidno;
509
510         for (tidno = 0, tid = &an->tid[tidno]; tidno < WME_NUM_TID;tidno++, tid++) {
511                 tid->tidno = tidno;
512                 tid->an = an;
513
514                 if ( tid->flag & TID_CLEANUP_INPROGRES ) {
515                         tid->flag |= TID_REINITIALIZE;
516                         adf_os_print("tid[%p]->incomp is not 0: %d\n",
517                                      tid, tid->incomp);
518                 } else {
519                         owl_tgt_tid_init(tid);
520                 }
521         }
522 }
523
524 void ath_tx_status_clear(struct ath_softc_tgt *sc)
525 {
526         int i;
527
528         for (i = 0; i < 2; i++) {
529                 sc->tx_status[i].cnt = 0;
530         }
531 }
532
533 static WMI_TXSTATUS_EVENT *ath_tx_status_get(struct ath_softc_tgt *sc)
534 {
535         WMI_TXSTATUS_EVENT *txs = NULL;
536         int i;
537
538         for (i = 0; i < 2; i++) {
539                 if (sc->tx_status[i].cnt < HTC_MAX_TX_STATUS) {
540                         txs = &sc->tx_status[i];
541                         break;
542                 }
543         }
544
545         return txs;
546 }
547
548 void ath_tx_status_update(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
549 {
550         struct ath_tx_desc *ds = bf->bf_lastds;
551         WMI_TXSTATUS_EVENT *txs;
552
553         if (sc->sc_tx_draining)
554                 return;
555
556         txs = ath_tx_status_get(sc);
557         if (txs == NULL)
558                 return;
559
560         txs->txstatus[txs->cnt].cookie = bf->bf_cookie;
561         txs->txstatus[txs->cnt].ts_rate = SM(bf->bf_endpt, ATH9K_HTC_TXSTAT_EPID);
562
563         if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
564                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_FILT;
565
566         if (!(ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) &&
567             !(ds->ds_txstat.ts_status & HAL_TXERR_FIFO) &&
568             !(ds->ds_txstat.ts_status & HAL_TXERR_TIMER_EXPIRED) &&
569             !(ds->ds_txstat.ts_status & HAL_TXERR_FILT))
570                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_ACK;
571
572         ath_tx_status_update_rate(sc, bf->bf_rcs, ds->ds_txstat.ts_rate, txs);
573
574         txs->cnt++;
575 }
576
577 void ath_tx_status_update_aggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf,
578                                struct ath_tx_desc *ds, struct ath_rc_series rcs[],
579                                int txok)
580 {
581         WMI_TXSTATUS_EVENT *txs;
582
583         if (sc->sc_tx_draining)
584                 return;
585
586         txs = ath_tx_status_get(sc);
587         if (txs == NULL)
588                 return;
589
590         txs->txstatus[txs->cnt].cookie = bf->bf_cookie;
591         txs->txstatus[txs->cnt].ts_rate = SM(bf->bf_endpt, ATH9K_HTC_TXSTAT_EPID);
592
593         if (txok)
594                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_ACK;
595
596         if (rcs)
597                 ath_tx_status_update_rate(sc, rcs, ds->ds_txstat.ts_rate, txs);
598
599         txs->cnt++;
600 }
601
602 void ath_tx_status_send(struct ath_softc_tgt *sc)
603 {
604         int i;
605
606         if (sc->sc_tx_draining)
607                 return;
608
609         for (i = 0; i < 2; i++) {
610                 if (sc->tx_status[i].cnt) {
611                         wmi_event(sc->tgt_wmi_handle, WMI_TXSTATUS_EVENTID,
612                                   &sc->tx_status[i], sizeof(WMI_TXSTATUS_EVENT));
613                         /* FIXME: Handle failures. */
614                         sc->tx_status[i].cnt = 0;
615                 }
616         }
617 }
618
619 static void owltgt_tx_process_cabq(struct ath_softc_tgt *sc, struct ath_txq *txq)
620 {
621         struct ath_hal *ah = sc->sc_ah;
622         ah->ah_setInterrupts(ah, sc->sc_imask & ~HAL_INT_SWBA);
623         owltgt_tx_processq(sc, txq, OWL_TXQ_ACTIVE);
624         ah->ah_setInterrupts(ah, sc->sc_imask);
625 }
626
627 void owl_tgt_tx_tasklet(TQUEUE_ARG data)
628 {
629         struct ath_softc_tgt *sc = (struct ath_softc_tgt *)data;
630         a_int32_t i;
631         struct ath_txq *txq;
632
633         ath_tx_status_clear(sc);
634
635         for (i = 0; i < (HAL_NUM_TX_QUEUES - 6); i++) {
636                 txq = ATH_TXQ(sc, i);
637
638                 if (ATH_TXQ_SETUP(sc, i)) {
639                         if (txq == sc->sc_cabq)
640                                 owltgt_tx_process_cabq(sc, txq);
641                         else
642                                 owltgt_tx_processq(sc, txq, OWL_TXQ_ACTIVE);
643                 }
644         }
645
646         ath_tx_status_send(sc);
647 }
648
649 void owltgt_tx_processq(struct ath_softc_tgt *sc, struct ath_txq *txq,
650                         owl_txq_state_t txqstate)
651 {
652         struct ath_tx_buf *bf;
653         struct ath_tx_desc *ds;
654         struct ath_hal *ah = sc->sc_ah;
655         HAL_STATUS status;
656
657         for (;;) {
658                 if (asf_tailq_empty(&txq->axq_q)) {
659                         txq->axq_link = NULL;
660                         txq->axq_linkbuf = NULL;
661                         break;
662                 }
663
664                 bf = asf_tailq_first(&txq->axq_q);
665
666                 ds = bf->bf_lastds;
667                 status = ah->ah_procTxDesc(ah, ds);
668
669                 if (status == HAL_EINPROGRESS) {
670                         if (txqstate == OWL_TXQ_ACTIVE)
671                                 break;
672                         else if (txqstate == OWL_TXQ_STOPPED) {
673                                 __stats(sc, tx_stopfiltered);
674                                 ds->ds_txstat.ts_flags  = 0;
675                                 ds->ds_txstat.ts_status = HAL_OK;
676                         } else {
677                                 ds->ds_txstat.ts_flags  = HAL_TX_SW_FILTERED;
678                         }
679                 }
680
681                 ATH_TXQ_REMOVE_HEAD(txq, bf, bf_list);
682                 if ((asf_tailq_empty(&txq->axq_q))) {
683                         __stats(sc, tx_qnull);
684                         txq->axq_link = NULL;
685                         txq->axq_linkbuf = NULL;
686                 }
687
688                 if (bf->bf_comp) {
689                         bf->bf_comp(sc, bf);
690                 } else {
691                         ath_tx_status_update(sc, bf);
692                         ath_buf_comp(sc, bf);
693                 }
694
695                 if (txqstate == OWL_TXQ_ACTIVE) {
696                         ath_tgt_txq_schedule(sc, txq);
697                 }
698         }
699 }
700
701 static struct ieee80211_frame* ATH_SKB2_WH(adf_nbuf_t skb)
702 {
703         a_uint8_t *anbdata;
704         a_uint32_t anblen;
705
706         adf_nbuf_peek_header(skb, &anbdata, &anblen);
707         return((struct ieee80211_frame *)anbdata);
708 }
709
710 void
711 ath_tgt_tid_drain(struct ath_softc_tgt *sc, struct ath_atx_tid *tid)
712 {
713         struct ath_tx_buf *bf;
714
715         while (!asf_tailq_empty(&tid->buf_q)) {
716                 TAILQ_DEQ(&tid->buf_q, bf, bf_list);
717                 ath_tx_freebuf(sc, bf);
718         }
719
720         tid->seq_next = tid->seq_start;
721         tid->baw_tail = tid->baw_head;
722 }
723
724 static void ath_tgt_tx_comp_normal(struct ath_softc_tgt *sc,
725                                    struct ath_tx_buf *bf)
726 {
727         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
728         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
729
730         if (tid->flag & TID_CLEANUP_INPROGRES) {
731                 owl_tgt_tid_cleanup(sc, tid);
732         }
733
734         if (bf->bf_node) {
735                 ath_tx_uc_comp(sc, bf);
736         }
737
738         ath_tx_freebuf(sc, bf);
739 }
740
741 static struct ieee80211_node_target * ath_tgt_find_node(struct ath_softc_tgt *sc,
742                                                         a_int32_t node_index)
743 {
744         struct ath_node_target *an;
745         struct ieee80211_node_target *ni;
746
747         if (node_index > TARGET_NODE_MAX)
748                 return NULL;
749
750         an = &sc->sc_sta[node_index];
751         ni = &an->ni;
752
753         if (an->an_valid) {
754                 if (ni->ni_vap == NULL) {
755                         return NULL;
756                 }
757                 return  ni;
758         }
759
760         return NULL;
761 }
762
763 static struct ath_tx_buf* ath_tx_buf_alloc(struct ath_softc_tgt *sc)
764 {
765         struct ath_tx_buf *bf = NULL;
766
767         bf = asf_tailq_first(&sc->sc_txbuf);
768         if (bf !=  NULL) {
769                 adf_os_mem_set(&bf->bf_state, 0, sizeof(struct ath_buf_state));
770                 asf_tailq_remove(&sc->sc_txbuf, bf, bf_list);
771         } else {
772                 adf_os_assert(0);
773         }
774
775         return  bf;
776 }
777
778 struct ath_tx_buf* ath_tgt_tx_prepare(struct ath_softc_tgt *sc,
779                                    adf_nbuf_t skb, ath_data_hdr_t *dh)
780 {
781         struct ath_tx_buf *bf;
782         struct ieee80211_node_target *ni;
783         struct ath_atx_tid *tid;
784
785         ni = ath_tgt_find_node(sc, dh->ni_index);
786         if (ni == NULL)
787                 return NULL;
788
789         tid = ATH_AN_2_TID(ATH_NODE_TARGET(ni), dh->tidno);
790         if (tid->flag & TID_REINITIALIZE) {
791                 adf_os_print("drop frame due to TID reinit\n");
792                 return NULL;
793         }
794
795         bf = ath_tx_buf_alloc(sc);
796         if (!bf) {
797                 __stats(sc, tx_nobufs);
798                 return NULL;
799         }
800
801         bf->bf_tidno = dh->tidno;
802         bf->bf_txq = TID_TO_ACTXQ(bf->bf_tidno);
803         bf->bf_keytype = dh->keytype;
804         bf->bf_keyix = dh->keyix;
805         bf->bf_protmode = dh->flags & (IEEE80211_PROT_RTSCTS | IEEE80211_PROT_CTSONLY);
806         bf->bf_node = ni;
807
808         adf_nbuf_queue_add(&bf->bf_skbhead, skb);
809         skb = adf_nbuf_queue_first(&(bf->bf_skbhead));
810
811         if (adf_nbuf_queue_len(&(bf->bf_skbhead)) == 0) {
812                 __stats(sc, tx_noskbs);
813                 return NULL;
814         }
815
816         adf_os_assert(skb);
817
818         bf->bf_skb = skb;
819
820         ath_tgt_txbuf_setup(sc, bf, dh);
821         ath_dma_map(sc, bf);
822         ath_tx_tgt_setds(sc, bf);
823
824         return bf;
825 }
826
827 static void ath_tgt_tx_seqno_normal(struct ath_tx_buf *bf)
828 {
829         struct ieee80211_node_target *ni = bf->bf_node;
830         struct ath_node_target *an = ATH_NODE_TARGET(ni);
831         struct ieee80211_frame *wh = ATH_SKB_2_WH(bf->bf_skb);
832         struct ath_atx_tid *tid = ATH_AN_2_TID(an, bf->bf_tidno);
833
834         u_int8_t fragno = (wh->i_seq[0] & 0xf);
835
836         INCR(ni->ni_txseqmgmt, IEEE80211_SEQ_MAX);
837
838         bf->bf_seqno = (tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
839
840         *(u_int16_t *)wh->i_seq = adf_os_cpu_to_le16(bf->bf_seqno);
841         wh->i_seq[0] |= fragno;
842
843         if (!(wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG))
844                 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
845 }
846
847 static a_int32_t ath_key_setup(struct ieee80211_node_target *ni,
848                                struct ath_tx_buf *bf)
849 {
850         struct ieee80211_frame *wh = ATH_SKB_2_WH(bf->bf_skb);
851
852         if (!(wh->i_fc[1] & IEEE80211_FC1_WEP)) {
853                 bf->bf_keytype = HAL_KEY_TYPE_CLEAR;
854                 bf->bf_keyix = HAL_TXKEYIX_INVALID;
855                 return 0;
856         }
857
858         switch (bf->bf_keytype) {
859         case HAL_KEY_TYPE_WEP:
860                 bf->bf_pktlen += IEEE80211_WEP_ICVLEN;
861                 break;
862         case HAL_KEY_TYPE_AES:
863                 bf->bf_pktlen += IEEE80211_WEP_MICLEN;
864                 break;
865         case HAL_KEY_TYPE_TKIP:
866                 bf->bf_pktlen += IEEE80211_WEP_ICVLEN;
867                 break;
868         default:
869                 break;
870         }
871
872         if (bf->bf_keytype == HAL_KEY_TYPE_AES ||
873             bf->bf_keytype == HAL_KEY_TYPE_TKIP)
874                 ieee80211_tgt_crypto_encap(wh, ni, bf->bf_keytype);
875
876         return 0;
877 }
878
879 static void ath_tgt_txq_add_ucast(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
880 {
881         struct ath_hal *ah = sc->sc_ah;
882         struct ath_txq *txq;
883         HAL_STATUS status;
884         volatile a_int32_t txe_val;
885
886         adf_os_assert(bf);
887
888         txq = bf->bf_txq;
889
890         status = ah->ah_procTxDesc(ah, bf->bf_lastds);
891
892         ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
893
894         if (txq->axq_link == NULL) {
895                 ah->ah_setTxDP(ah, txq->axq_qnum, ATH_BUF_GET_DESC_PHY_ADDR(bf));
896         } else {
897                 *txq->axq_link = ATH_BUF_GET_DESC_PHY_ADDR(bf);
898
899                 txe_val = OS_REG_READ(ah, 0x840);
900                 if (!(txe_val & (1<< txq->axq_qnum)))
901                         ah->ah_setTxDP(ah, txq->axq_qnum, ATH_BUF_GET_DESC_PHY_ADDR(bf));
902         }
903
904         txq->axq_link = &bf->bf_lastds->ds_link;
905         ah->ah_startTxDma(ah, txq->axq_qnum);
906 }
907
908 static a_int32_t ath_tgt_txbuf_setup(struct ath_softc_tgt *sc,
909                                      struct ath_tx_buf *bf,
910                                      ath_data_hdr_t *dh)
911
912 {
913         struct ieee80211_frame *wh = ATH_SKB2_WH(bf->bf_skb);
914         a_int32_t retval;
915         a_uint32_t flags = adf_os_ntohl(dh->flags);
916
917         ath_tgt_tx_seqno_normal(bf);
918
919         bf->bf_txq_add  = ath_tgt_txq_add_ucast;
920         bf->bf_hdrlen   = ieee80211_anyhdrsize(wh);
921         bf->bf_pktlen   = ath_get_pktlen(bf, bf->bf_hdrlen);
922         bf->bf_ismcast  = IEEE80211_IS_MULTICAST(wh->i_addr1);
923
924         if ((retval = ath_key_setup(bf->bf_node, bf)) < 0)
925                 return retval;
926
927         if (flags & ATH_SHORT_PREAMBLE)
928                 bf->bf_shpream = AH_TRUE;
929         else
930                 bf->bf_shpream = AH_FALSE;
931
932         bf->bf_flags = HAL_TXDESC_CLRDMASK;
933         bf->bf_atype = HAL_PKT_TYPE_NORMAL;
934
935         return 0;
936 }
937
938 static a_int32_t
939 ath_get_pktlen(struct ath_tx_buf *bf, a_int32_t hdrlen)
940 {
941         adf_nbuf_t skb = bf->bf_skb;
942         a_int32_t pktlen;
943
944         skb = adf_nbuf_queue_first(&bf->bf_skbhead);
945         pktlen = adf_nbuf_len(skb);
946
947         pktlen -= (hdrlen & 3);
948         pktlen += IEEE80211_CRC_LEN;
949
950         return pktlen;
951 }
952
953 void
954 ath_tgt_tx_send_normal(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
955 {
956         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
957         struct ath_rc_series rcs[4];
958         struct ath_rc_series mrcs[4];
959         a_int32_t shortPreamble = 0;
960         a_int32_t isProbe = 0;
961
962         adf_os_mem_set(rcs, 0, sizeof(struct ath_rc_series)*4 );
963         adf_os_mem_set(mrcs, 0, sizeof(struct ath_rc_series)*4 );
964
965         if (!bf->bf_ismcast) {
966                 ath_tgt_rate_findrate(sc, an, shortPreamble,
967                                       0, 0, 0, 0, 0,
968                                       rcs, &isProbe);
969                 ath_hal_memcpy(bf->bf_rcs, rcs, sizeof(rcs));
970         } else {
971                 mrcs[1].tries = mrcs[2].tries = mrcs[3].tries = 0;
972                 mrcs[1].rix = mrcs[2].rix = mrcs[3].rix = 0;
973                 mrcs[0].rix   = 0;
974                 mrcs[0].tries = 1;
975                 mrcs[0].flags = 0;
976                 ath_hal_memcpy(bf->bf_rcs, mrcs, sizeof(mrcs));
977         }
978
979         ath_buf_set_rate(sc, bf);
980         bf->bf_txq_add(sc, bf);
981 }
982
983 static void
984 ath_tx_freebuf(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
985 {
986         a_int32_t i ;
987         struct ath_tx_desc *bfd = NULL;
988         struct ath_hal *ah = sc->sc_ah;
989
990         for (bfd = bf->bf_desc, i = 0; i < bf->bf_dmamap_info.nsegs; bfd++, i++) {
991                 ah->ah_clr11nAggr(ah, bfd);
992                 ah->ah_set11nBurstDuration(ah, bfd, 0);
993                 ah->ah_set11nVirtualMoreFrag(ah, bfd, 0);
994         }
995
996         ath_dma_unmap(sc, bf);
997
998         ath_tgt_skb_free(sc, &bf->bf_skbhead,bf->bf_endpt);
999
1000         bf->bf_skb = NULL;
1001         bf->bf_comp = NULL;
1002         bf->bf_node = NULL;
1003         bf->bf_next = NULL;
1004         bf = ath_buf_toggle(sc, bf, 0);
1005         bf->bf_retries = 0;
1006         bf->bf_isretried = 0;
1007
1008         if (bf != NULL)
1009                 asf_tailq_insert_tail(&sc->sc_txbuf, bf, bf_list);
1010 }
1011
1012 static void
1013 ath_tx_uc_comp(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1014 {
1015         ath_tx_status_update(sc, bf);
1016         ath_update_stats(sc, bf);
1017         ath_rate_tx_complete(sc, ATH_NODE_TARGET(bf->bf_node),
1018                              bf->bf_lastds, bf->bf_rcs, 1, 0);
1019 }
1020
1021 static void
1022 ath_update_stats(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1023 {
1024         struct ath_tx_desc *ds = bf->bf_desc;
1025         u_int32_t sr, lr;
1026
1027         if (ds->ds_txstat.ts_status == 0) {
1028                 if (ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)
1029                         sc->sc_tx_stats.ast_tx_altrate++;
1030         } else {
1031                 if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
1032                         sc->sc_tx_stats.ast_tx_xretries++;
1033                 if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
1034                         sc->sc_tx_stats.ast_tx_fifoerr++;
1035                 if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
1036                         sc->sc_tx_stats.ast_tx_filtered++;
1037                 if (ds->ds_txstat.ts_status & HAL_TXERR_TIMER_EXPIRED)
1038                         sc->sc_tx_stats.ast_tx_timer_exp++;
1039         }
1040         sr = ds->ds_txstat.ts_shortretry;
1041         lr = ds->ds_txstat.ts_longretry;
1042         sc->sc_tx_stats.ast_tx_shortretry += sr;
1043         sc->sc_tx_stats.ast_tx_longretry += lr;
1044 }
1045
1046 void
1047 ath_tgt_send_mgt(struct ath_softc_tgt *sc,adf_nbuf_t hdr_buf, adf_nbuf_t skb,
1048                  HTC_ENDPOINT_ID endpt)
1049 {
1050         struct ieee80211_node_target *ni;
1051         struct ieee80211vap_target *vap;
1052         struct ath_vap_target *avp;
1053         struct ath_hal *ah = sc->sc_ah;
1054         a_uint8_t rix, txrate, ctsrate, cix = 0xff, *data;
1055         a_uint32_t subtype, flags, ctsduration;
1056         a_int32_t i, iswep, ismcast, hdrlen, pktlen, try0, len;
1057         struct ath_tx_desc *ds=NULL;
1058         struct ath_txq *txq=NULL;
1059         struct ath_tx_buf *bf;
1060         HAL_PKT_TYPE atype;
1061         const HAL_RATE_TABLE *rt;
1062         HAL_BOOL shortPreamble;
1063         struct ieee80211_frame *wh;
1064         struct ath_rc_series rcs[4];
1065         HAL_11N_RATE_SERIES series[4];
1066         ath_mgt_hdr_t *mh;
1067         a_int8_t keyix;
1068
1069         if (!hdr_buf) {
1070                 adf_nbuf_peek_header(skb, &data, &len);
1071                 adf_nbuf_pull_head(skb, sizeof(ath_mgt_hdr_t));
1072         } else {
1073                 adf_nbuf_peek_header(hdr_buf, &data, &len);
1074         }
1075
1076         adf_os_assert(len >= sizeof(ath_mgt_hdr_t));
1077
1078         mh = (ath_mgt_hdr_t *)data;
1079         adf_nbuf_peek_header(skb, &data, &len);
1080         wh = (struct ieee80211_frame *)data;
1081
1082         adf_os_mem_set(rcs, 0, sizeof(struct ath_rc_series)*4);
1083         adf_os_mem_set(series, 0, sizeof(HAL_11N_RATE_SERIES)*4);
1084
1085         bf = asf_tailq_first(&sc->sc_txbuf);
1086         if (!bf)
1087                 goto fail;
1088
1089         asf_tailq_remove(&sc->sc_txbuf, bf, bf_list);
1090
1091         ni = ath_tgt_find_node(sc, mh->ni_index);
1092         if (!ni)
1093                 goto fail;
1094
1095         bf->bf_endpt = endpt;
1096         bf->bf_cookie = mh->cookie;
1097         bf->bf_protmode = mh->flags & (IEEE80211_PROT_RTSCTS | IEEE80211_PROT_CTSONLY);
1098         txq = &sc->sc_txq[1];
1099         iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1100         ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1101         hdrlen = ieee80211_anyhdrsize(wh);
1102         pktlen = len;
1103         keyix = HAL_TXKEYIX_INVALID;
1104         pktlen -= (hdrlen & 3);
1105         pktlen += IEEE80211_CRC_LEN;
1106
1107         if (iswep)
1108                 keyix = mh->keyix;
1109
1110         adf_nbuf_map(sc->sc_dev, bf->bf_dmamap, skb, ADF_OS_DMA_TO_DEVICE);
1111
1112         bf->bf_skb = skb;
1113         adf_nbuf_queue_add(&bf->bf_skbhead, skb);
1114
1115         ds = bf->bf_desc;
1116         rt = sc->sc_currates;
1117         adf_os_assert(rt != NULL);
1118
1119         if (mh->flags == ATH_SHORT_PREAMBLE)
1120                 shortPreamble = AH_TRUE;
1121         else
1122                 shortPreamble = AH_FALSE;
1123
1124         flags = HAL_TXDESC_CLRDMASK;
1125
1126         switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1127         case IEEE80211_FC0_TYPE_MGT:
1128                 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1129
1130                 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1131                         atype = HAL_PKT_TYPE_PROBE_RESP;
1132                 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1133                         atype = HAL_PKT_TYPE_ATIM;
1134                 else
1135                         atype = HAL_PKT_TYPE_NORMAL;
1136
1137                 break;
1138         default:
1139                 atype = HAL_PKT_TYPE_NORMAL;
1140                 break;
1141         }
1142
1143         avp = &sc->sc_vap[mh->vap_index];
1144
1145         rcs[0].rix = ath_get_minrateidx(sc, avp);
1146         rcs[0].tries = ATH_TXMAXTRY;
1147         rcs[0].flags = 0;
1148
1149         adf_os_mem_copy(bf->bf_rcs, rcs, sizeof(rcs));
1150         rix = rcs[0].rix;
1151         try0 = rcs[0].tries;
1152         txrate = rt->info[rix].rateCode;
1153
1154         if (shortPreamble){
1155                 txrate |= rt->info[rix].shortPreamble;
1156         }
1157
1158         vap = ni->ni_vap;
1159         bf->bf_node = ni;
1160
1161         if (ismcast) {
1162                 flags |= HAL_TXDESC_NOACK;
1163                 try0 = 1;
1164         } else if (pktlen > vap->iv_rtsthreshold) {
1165                 flags |= HAL_TXDESC_RTSENA;
1166                 cix = rt->info[rix].controlRate;
1167         }
1168
1169         if ((bf->bf_protmode != IEEE80211_PROT_NONE) &&
1170             rt->info[rix].phy == IEEE80211_T_OFDM &&
1171             (flags & HAL_TXDESC_NOACK) == 0) {
1172                 cix = rt->info[sc->sc_protrix].controlRate;
1173                 sc->sc_tx_stats.ast_tx_protect++;
1174         }
1175
1176         *(a_uint16_t *)&wh->i_seq[0] =  adf_os_cpu_to_le16(ni->ni_txseqmgmt <<
1177                                                            IEEE80211_SEQ_SEQ_SHIFT);
1178         INCR(ni->ni_txseqmgmt, IEEE80211_SEQ_MAX);
1179
1180         ctsduration = 0;
1181         if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
1182                 adf_os_assert(cix != 0xff);
1183                 ctsrate = rt->info[cix].rateCode;
1184                 if (shortPreamble) {
1185                         ctsrate |= rt->info[cix].shortPreamble;
1186                         if (flags & HAL_TXDESC_RTSENA)      /* SIFS + CTS */
1187                                 ctsduration += rt->info[cix].spAckDuration;
1188                         if ((flags & HAL_TXDESC_NOACK) == 0)    /* SIFS + ACK */
1189                                 ctsduration += rt->info[cix].spAckDuration;
1190                 } else {
1191                         if (flags & HAL_TXDESC_RTSENA)      /* SIFS + CTS */
1192                                 ctsduration += rt->info[cix].lpAckDuration;
1193                         if ((flags & HAL_TXDESC_NOACK) == 0)    /* SIFS + ACK */
1194                                 ctsduration += rt->info[cix].lpAckDuration;
1195                 }
1196                 ctsduration += ath_hal_computetxtime(ah,
1197                                                      rt, pktlen, rix, shortPreamble);
1198                 try0 = 1;
1199         } else
1200                 ctsrate = 0;
1201
1202         flags |= HAL_TXDESC_INTREQ;
1203
1204         ah->ah_setupTxDesc(ds
1205                             , pktlen
1206                             , hdrlen
1207                             , atype
1208                             , 60
1209                             , txrate, try0
1210                             , keyix
1211                             , flags
1212                             , ctsrate
1213                             , ctsduration);
1214
1215         bf->bf_flags = flags;
1216
1217         /*
1218          * Set key type in tx desc while sending the encrypted challenge to AP
1219          * in Auth frame 3 of Shared Authentication, owl needs this.
1220          */
1221         if (iswep && (keyix != HAL_TXKEYIX_INVALID) &&
1222                         (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_AUTH)
1223                 ah->ah_fillKeyTxDesc(ds, mh->keytype);
1224
1225         ath_filltxdesc(sc, bf);
1226
1227         for (i=0; i<4; i++) {
1228                 series[i].Tries = 2;
1229                 series[i].Rate = txrate;
1230                 series[i].ChSel = sc->sc_ic.ic_tx_chainmask;
1231                 series[i].RateFlags = 0;
1232         }
1233         ah->ah_set11nRateScenario(ah, ds, 0, ctsrate, ctsduration, series, 4, 0);
1234         ath_tgt_txqaddbuf(sc, txq, bf, bf->bf_lastds);
1235
1236         return;
1237 fail:
1238         HTC_ReturnBuffers(sc->tgt_htc_handle, endpt, skb);
1239         return;
1240 }
1241
1242 static void
1243 ath_tgt_txqaddbuf(struct ath_softc_tgt *sc,
1244                   struct ath_txq *txq, struct ath_tx_buf *bf,
1245                   struct ath_tx_desc *lastds)
1246 {
1247         struct ath_hal *ah = sc->sc_ah;
1248
1249         ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
1250
1251         if (txq->axq_link == NULL) {
1252                 ah->ah_setTxDP(ah, txq->axq_qnum, ATH_BUF_GET_DESC_PHY_ADDR(bf));
1253         } else {
1254                 *txq->axq_link = ATH_BUF_GET_DESC_PHY_ADDR(bf);
1255         }
1256
1257         txq->axq_link = &lastds->ds_link;
1258         ah->ah_startTxDma(ah, txq->axq_qnum);
1259 }
1260
1261 void ath_tgt_handle_normal(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1262 {
1263         ath_atx_tid_t *tid;
1264         struct ath_node_target *an;
1265
1266         an = (struct ath_node_target *)bf->bf_node;
1267         adf_os_assert(an);
1268
1269         tid = &an->tid[bf->bf_tidno];
1270         adf_os_assert(tid);
1271
1272         bf->bf_comp = ath_tgt_tx_comp_normal;
1273         INCR(tid->seq_start, IEEE80211_SEQ_MAX);
1274         ath_tgt_tx_send_normal(sc, bf);
1275 }
1276
1277 static void
1278 ath_tgt_tx_enqueue(struct ath_txq *txq, struct ath_atx_tid  *tid)
1279 {
1280         if (tid->paused)
1281                 return;
1282
1283         if (tid->sched)
1284                 return;
1285
1286         tid->sched = AH_TRUE;
1287         asf_tailq_insert_tail(&txq->axq_tidq, tid, tid_qelem);
1288 }
1289
1290 static void
1291 ath_tgt_txq_schedule(struct ath_softc_tgt *sc, struct ath_txq *txq)
1292 {
1293         struct ath_atx_tid  *tid;
1294         u_int8_t bdone;
1295
1296         bdone = AH_FALSE;
1297
1298         do {
1299                 TAILQ_DEQ(&txq->axq_tidq, tid, tid_qelem);
1300
1301                 if (tid == NULL)
1302                         return;
1303
1304                 tid->sched = AH_FALSE;
1305
1306                 if (tid->paused)
1307                         continue;
1308
1309                 if (!(tid->flag & TID_AGGR_ENABLED))
1310                         ath_tgt_tx_sched_normal(sc,tid);
1311                 else
1312                         ath_tgt_tx_sched_aggr(sc,tid);
1313
1314                 bdone = AH_TRUE;
1315
1316                 if (!asf_tailq_empty(&tid->buf_q)) {
1317                         ath_tgt_tx_enqueue(txq, tid);
1318                 }
1319
1320         } while (!asf_tailq_empty(&txq->axq_tidq) && !bdone);
1321 }
1322
1323 void
1324 ath_tgt_handle_aggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1325 {
1326         ath_atx_tid_t *tid;
1327         struct ath_node_target *an;
1328         struct ath_txq *txq = bf->bf_txq;
1329         a_bool_t queue_frame, within_baw;
1330
1331         an = (struct ath_node_target *)bf->bf_node;
1332         adf_os_assert(an);
1333
1334         tid = &an->tid[bf->bf_tidno];
1335         adf_os_assert(tid);
1336
1337         bf->bf_comp = ath_tgt_tx_comp_aggr;
1338
1339         within_baw = BAW_WITHIN(tid->seq_start, tid->baw_size,
1340                                 SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1341
1342         queue_frame = ( (txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) ||
1343                         (!asf_tailq_empty(&tid->buf_q)) ||
1344                         (tid->paused) || (!within_baw) );
1345
1346         if (queue_frame) {
1347                 asf_tailq_insert_tail(&tid->buf_q, bf, bf_list);
1348                 ath_tgt_tx_enqueue(txq, tid);
1349         } else {
1350                 ath_tx_addto_baw(tid, bf);
1351                 __stats(sc, txaggr_nframes);
1352                 ath_tgt_tx_send_normal(sc, bf);
1353         }
1354 }
1355
1356 static void
1357 ath_tgt_tx_sched_normal(struct ath_softc_tgt *sc, ath_atx_tid_t *tid)
1358 {
1359         struct ath_tx_buf *bf;
1360         struct ath_txq *txq =TID_TO_ACTXQ(tid->tidno);;
1361
1362         do {
1363                 if (asf_tailq_empty(&tid->buf_q))
1364                         break;
1365
1366                 bf = asf_tailq_first(&tid->buf_q);
1367                 asf_tailq_remove(&tid->buf_q, bf, bf_list);
1368                 ath_tgt_tx_send_normal(sc, bf);
1369
1370         } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH);
1371 }
1372
1373 static void
1374 ath_tgt_tx_sched_aggr(struct ath_softc_tgt *sc, ath_atx_tid_t *tid)
1375 {
1376         struct ath_tx_buf  *bf, *bf_last;
1377         ATH_AGGR_STATUS status;
1378         ath_tx_bufhead bf_q;
1379         struct ath_txq *txq = TID_TO_ACTXQ(tid->tidno);
1380         struct ath_tx_desc *ds = NULL;
1381         struct ath_hal *ah = sc->sc_ah;
1382         int i;
1383
1384
1385         if (asf_tailq_empty(&tid->buf_q))
1386                 return;
1387
1388         do {
1389                 if (asf_tailq_empty(&tid->buf_q))
1390                         break;
1391
1392                 asf_tailq_init(&bf_q);
1393
1394                 status = ath_tgt_tx_form_aggr(sc, tid, &bf_q);
1395
1396                 if (asf_tailq_empty(&bf_q))
1397                         break;
1398
1399                 bf = asf_tailq_first(&bf_q);
1400                 bf_last = asf_tailq_last(&bf_q, ath_tx_bufhead_s);
1401
1402                 if (bf->bf_nframes == 1) {
1403
1404                         if(bf->bf_retries == 0)
1405                                 __stats(sc, txaggr_single);
1406                         bf->bf_isaggr = 0;
1407                         bf->bf_lastds = &(bf->bf_descarr[bf->bf_dmamap_info.nsegs -1]);
1408                         bf->bf_lastds->ds_link = 0;
1409                         bf->bf_next = NULL;
1410
1411                         for(ds = bf->bf_desc; ds <= bf->bf_lastds; ds++)
1412                                 ah->ah_clr11nAggr(ah, ds);
1413
1414                         ath_buf_set_rate(sc, bf);
1415                         bf->bf_txq_add(sc, bf);
1416
1417                         continue;
1418                 }
1419
1420                 bf_last->bf_next = NULL;
1421                 bf_last->bf_lastds->ds_link = 0;
1422                 bf_last->bf_ndelim = 0;
1423
1424                 bf->bf_isaggr  = 1;
1425                 ath_buf_set_rate(sc, bf);
1426                 ah->ah_set11nAggrFirst(ah, bf->bf_desc, bf->bf_al,
1427                                           bf->bf_ndelim);
1428                 bf->bf_lastds = bf_last->bf_lastds;
1429
1430                 for (i = 0; i < bf_last->bf_dmamap_info.nsegs; i++)
1431                         ah->ah_set11nAggrLast(ah, &bf_last->bf_descarr[i]);
1432
1433                 if (status == ATH_AGGR_8K_LIMITED) {
1434                         adf_os_assert(0);
1435                         break;
1436                 }
1437
1438                 bf->bf_txq_add(sc, bf);
1439         } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1440                  status != ATH_TGT_AGGR_BAW_CLOSED);
1441 }
1442
1443 static u_int32_t ath_lookup_rate(struct ath_softc_tgt *sc,
1444                                  struct ath_node_target *an,
1445                                  struct ath_tx_buf *bf)
1446 {
1447         int i, prate;
1448         u_int32_t max4msframelen, frame_length;
1449         u_int16_t aggr_limit, legacy=0;
1450         const HAL_RATE_TABLE  *rt = sc->sc_currates;
1451         struct ieee80211_node_target *ieee_node = (struct ieee80211_node_target *)an;
1452
1453         if (bf->bf_ismcast) {
1454                 bf->bf_rcs[1].tries = bf->bf_rcs[2].tries = bf->bf_rcs[3].tries = 0;
1455                 bf->bf_rcs[0].rix   = 0xb;
1456                 bf->bf_rcs[0].tries = ATH_TXMAXTRY - 1;
1457                 bf->bf_rcs[0].flags = 0;
1458         } else {
1459                 ath_tgt_rate_findrate(sc, an, AH_TRUE, 0, ATH_TXMAXTRY-1, 4, 1,
1460                                       ATH_RC_PROBE_ALLOWED, bf->bf_rcs, &prate);
1461         }
1462
1463         max4msframelen = IEEE80211_AMPDU_LIMIT_MAX;
1464
1465         for (i = 0; i < 4; i++) {
1466                 if (bf->bf_rcs[i].tries) {
1467                         frame_length = bf->bf_rcs[i].max4msframelen;
1468
1469                         if (rt->info[bf->bf_rcs[i].rix].phy != IEEE80211_T_HT) {
1470                                 legacy = 1;
1471                                 break;
1472                         }
1473
1474                         max4msframelen = ATH_MIN(max4msframelen, frame_length);
1475                 }
1476         }
1477
1478         if (prate || legacy)
1479                 return 0;
1480
1481         if (sc->sc_ic.ic_enable_coex)
1482                 aggr_limit = ATH_MIN((max4msframelen*3)/8, sc->sc_ic.ic_ampdu_limit);
1483         else
1484                 aggr_limit = ATH_MIN(max4msframelen, sc->sc_ic.ic_ampdu_limit);
1485
1486         if (ieee_node->ni_maxampdu)
1487                 aggr_limit = ATH_MIN(aggr_limit, ieee_node->ni_maxampdu);
1488
1489         return aggr_limit;
1490 }
1491
1492 int ath_tgt_tx_form_aggr(struct ath_softc_tgt *sc, ath_atx_tid_t *tid,
1493                          ath_tx_bufhead *bf_q)
1494 {
1495         struct ath_tx_buf *bf_first ,*bf_prev = NULL;
1496         int nframes = 0, rl = 0;;
1497         struct ath_tx_desc *ds = NULL;
1498         struct ath_tx_buf *bf;
1499         struct ath_hal *ah = sc->sc_ah;
1500         u_int16_t aggr_limit =  (64*1024 -1), al = 0, bpad = 0, al_delta;
1501         u_int16_t h_baw = tid->baw_size/2, prev_al = 0, prev_frames = 0;
1502
1503         bf_first = asf_tailq_first(&tid->buf_q);
1504
1505         do {
1506                 bf = asf_tailq_first(&tid->buf_q);
1507                 adf_os_assert(bf);
1508
1509                 if (!BAW_WITHIN(tid->seq_start, tid->baw_size,
1510                                 SEQNO_FROM_BF_SEQNO(bf->bf_seqno))) {
1511
1512                         bf_first->bf_al= al;
1513                         bf_first->bf_nframes = nframes;
1514                         return ATH_TGT_AGGR_BAW_CLOSED;
1515                 }
1516
1517                 if (!rl) {
1518                         aggr_limit = ath_lookup_rate(sc, tid->an, bf);
1519                         rl = 1;
1520                 }
1521
1522                 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_pktlen;
1523
1524                 if (nframes && (aggr_limit < (al + bpad + al_delta + prev_al))) {
1525                         bf_first->bf_al= al;
1526                         bf_first->bf_nframes = nframes;
1527                         return ATH_TGT_AGGR_LIMITED;
1528                 }
1529
1530 #ifdef PROJECT_K2
1531                 if ((nframes + prev_frames) >= ATH_MIN((h_baw), 17)) {
1532 #else
1533                 if ((nframes + prev_frames) >= ATH_MIN((h_baw), 22)) {
1534 #endif
1535                         bf_first->bf_al= al;
1536                         bf_first->bf_nframes = nframes;
1537                         return ATH_TGT_AGGR_LIMITED;
1538                 }
1539
1540                 ath_tx_addto_baw(tid, bf);
1541                 asf_tailq_remove(&tid->buf_q, bf, bf_list);
1542                 asf_tailq_insert_tail(bf_q, bf, bf_list);
1543                 nframes ++;
1544
1545                 adf_os_assert(bf);
1546
1547                 adf_os_assert(bf->bf_comp == ath_tgt_tx_comp_aggr);
1548
1549                 al += bpad + al_delta;
1550                 bf->bf_ndelim = ATH_AGGR_GET_NDELIM(bf->bf_pktlen);
1551
1552                 switch (bf->bf_keytype) {
1553                 case HAL_KEY_TYPE_AES:
1554                         bf->bf_ndelim += ATH_AGGR_ENCRYPTDELIM;
1555                         break;
1556                 case HAL_KEY_TYPE_WEP:
1557                 case HAL_KEY_TYPE_TKIP:
1558                         bf->bf_ndelim += 64;
1559                         break;
1560                 case HAL_KEY_TYPE_WAPI:
1561                         bf->bf_ndelim += 12;
1562                         break;
1563                 default:
1564                         break;
1565                 }
1566
1567                 bpad = PADBYTES(al_delta) + (bf->bf_ndelim << 2);
1568
1569                 if (bf_prev) {
1570                         bf_prev->bf_next = bf;
1571                         bf_prev->bf_lastds->ds_link = ATH_BUF_GET_DESC_PHY_ADDR(bf);
1572                 }
1573                 bf_prev = bf;
1574
1575                 for(ds = bf->bf_desc; ds <= bf->bf_lastds; ds++)
1576                         ah->ah_set11nAggrMiddle(ah, ds, bf->bf_ndelim);
1577
1578         } while (!asf_tailq_empty(&tid->buf_q));
1579
1580         bf_first->bf_al= al;
1581         bf_first->bf_nframes = nframes;
1582
1583         return ATH_TGT_AGGR_DONE;
1584 }
1585
1586 void ath_tx_addto_baw(ath_atx_tid_t *tid, struct ath_tx_buf *bf)
1587 {
1588         int index, cindex;
1589
1590         if (bf->bf_isretried) {
1591                 return;
1592         }
1593
1594         index  = ATH_BA_INDEX(tid->seq_start, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1595         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1596
1597         TX_BUF_BITMAP_SET(tid->tx_buf_bitmap, cindex);
1598
1599         if (index >= ((tid->baw_tail - tid->baw_head) & (ATH_TID_MAX_BUFS - 1))) {
1600                 tid->baw_tail = cindex;
1601                 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1602         }
1603 }
1604
1605 void ath_tgt_tx_comp_aggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1606 {
1607         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
1608         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
1609         struct ath_tx_desc lastds;
1610         struct ath_tx_desc *ds = &lastds;
1611         struct ath_rc_series rcs[4];
1612         u_int16_t seq_st;
1613         u_int32_t *ba;
1614         int ba_index;
1615         int nbad = 0;
1616         int nframes = bf->bf_nframes;
1617         struct ath_tx_buf *bf_next;
1618         ath_tx_bufhead bf_q;
1619         int tx_ok = 1;
1620         struct ath_tx_buf *bar = NULL;
1621         struct ath_txq *txq;
1622
1623         txq = bf->bf_txq;
1624
1625         if (tid->flag & TID_CLEANUP_INPROGRES) {
1626                 ath_tx_comp_cleanup(sc, bf);
1627                 return;
1628         }
1629
1630         adf_os_mem_copy(ds, bf->bf_lastds, sizeof (struct ath_tx_desc));
1631         adf_os_mem_copy(rcs, bf->bf_rcs, sizeof(rcs));
1632
1633         if (ds->ds_txstat.ts_flags == HAL_TX_SW_FILTERED) {
1634                 adf_os_assert(0);
1635                 return;
1636         }
1637
1638         if (!bf->bf_isaggr) {
1639                 ath_tx_comp_unaggr(sc, bf);
1640                 return;
1641         }
1642
1643         __stats(sc, tx_compaggr);
1644
1645         asf_tailq_init(&bf_q);
1646
1647         seq_st =  ATH_DS_BA_SEQ(ds);
1648         ba     =  ATH_DS_BA_BITMAP(ds);
1649         tx_ok  =  (ATH_DS_TX_STATUS(ds) == HAL_OK);
1650
1651         if (ATH_DS_TX_STATUS(ds) & HAL_TXERR_XRETRY) {
1652                 ath_tx_comp_aggr_error(sc, bf, tid);
1653                 return;
1654         }
1655
1656         if (tx_ok && !ATH_DS_TX_BA(ds)) {
1657                 __stats(sc, txaggr_babug);
1658                 adf_os_print("BA Bug?\n");
1659                 ath_tx_comp_aggr_error(sc, bf, tid);
1660                 return;
1661         }
1662
1663         while (bf) {
1664                 ba_index = ATH_BA_INDEX(seq_st, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1665                 bf_next  = bf->bf_next;
1666
1667                 if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
1668                         __stats(sc, txaggr_compgood);
1669                         ath_tx_update_baw(tid, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1670                         ath_tx_status_update_aggr(sc, bf, ds, rcs, 1);
1671                         ath_tx_freebuf(sc, bf);
1672                 } else {
1673                         ath_tx_retry_subframe(sc, bf, &bf_q, &bar);
1674                         nbad ++;
1675                 }
1676                 bf = bf_next;
1677         }
1678
1679         ath_update_aggr_stats(sc, ds, nframes, nbad);
1680         ath_rate_tx_complete(sc, an, ds, rcs, nframes, nbad);
1681
1682         if (bar) {
1683                 ath_bar_tx(sc, tid, bar);
1684         }
1685
1686         if (!asf_tailq_empty(&bf_q)) {
1687                 __stats(sc, txaggr_prepends);
1688                 TAILQ_INSERTQ_HEAD(&tid->buf_q, &bf_q, bf_list);
1689                 ath_tgt_tx_enqueue(txq, tid);
1690         }
1691 }
1692
1693 static void
1694 ath_tx_comp_aggr_error(struct ath_softc_tgt *sc, struct ath_tx_buf *bf,
1695                        ath_atx_tid_t *tid)
1696 {
1697
1698
1699         struct ath_tx_desc lastds;
1700         struct ath_tx_desc *ds = &lastds;
1701         struct ath_rc_series rcs[4];
1702         struct ath_tx_buf *bar = NULL;
1703         struct ath_tx_buf *bf_next;
1704         int nframes = bf->bf_nframes;
1705         ath_tx_bufhead bf_q;
1706         struct ath_txq *txq;
1707
1708         asf_tailq_init(&bf_q);
1709         txq = bf->bf_txq;
1710
1711         adf_os_mem_copy(ds, bf->bf_lastds, sizeof (struct ath_tx_desc));
1712         adf_os_mem_copy(rcs, bf->bf_rcs, sizeof(rcs));
1713
1714         while (bf) {
1715                 bf_next = bf->bf_next;
1716                 ath_tx_retry_subframe(sc, bf, &bf_q, &bar);
1717                 bf = bf_next;
1718         }
1719
1720         ath_update_aggr_stats(sc, ds, nframes, nframes);
1721         ath_rate_tx_complete(sc, tid->an, ds, rcs, nframes, nframes);
1722
1723         if (bar) {
1724                 ath_bar_tx(sc, tid, bar);
1725         }
1726
1727         if (!asf_tailq_empty(&bf_q)) {
1728                 __stats(sc, txaggr_prepends);
1729                 TAILQ_INSERTQ_HEAD(&tid->buf_q, &bf_q, bf_list);
1730                 ath_tgt_tx_enqueue(txq, tid);
1731         }
1732 }
1733
1734 static void
1735 ath_tx_comp_cleanup(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1736 {
1737
1738         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
1739         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
1740         struct ath_tx_desc lastds;
1741         struct ath_tx_desc *ds = &lastds;
1742         struct ath_rc_series rcs[4];
1743         u_int16_t seq_st;
1744         u_int32_t *ba;
1745         int ba_index;
1746         int nbad = 0;
1747         int nframes = bf->bf_nframes;
1748         struct ath_tx_buf *bf_next;
1749         int tx_ok = 1;
1750
1751         adf_os_mem_copy(ds, bf->bf_lastds, sizeof (struct ath_tx_desc));
1752         adf_os_mem_copy(rcs, bf->bf_rcs, sizeof(rcs));
1753
1754         seq_st = ATH_DS_BA_SEQ(ds);
1755         ba     = ATH_DS_BA_BITMAP(ds);
1756         tx_ok  = (ATH_DS_TX_STATUS(ds) == HAL_OK);
1757
1758         if (!bf->bf_isaggr) {
1759                 ath_update_stats(sc, bf);
1760
1761                 __stats(sc, tx_compunaggr);
1762
1763                 ath_tx_status_update(sc, bf);
1764
1765                 ath_tx_freebuf(sc, bf);
1766
1767                 if (tid->flag & TID_CLEANUP_INPROGRES) {
1768                         owl_tgt_tid_cleanup(sc, tid);
1769
1770                 }
1771
1772                 return;
1773         }
1774
1775         while (bf) {
1776                 ba_index = ATH_BA_INDEX(seq_st, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1777                 bf_next  = bf->bf_next;
1778
1779                 ath_tx_status_update_aggr(sc, bf, ds, rcs, 0);
1780
1781                 ath_tx_freebuf(sc, bf);
1782
1783                 tid->incomp--;
1784                 if (!tid->incomp) {
1785                         tid->flag  &= ~TID_CLEANUP_INPROGRES;
1786                         ath_aggr_resume_tid(sc, tid);
1787                         break;
1788                 }
1789
1790                 bf = bf_next;
1791         }
1792
1793         ath_update_aggr_stats(sc, ds, nframes, nbad);
1794         ath_rate_tx_complete(sc, an, ds, rcs, nframes, nbad);
1795 }
1796
1797 static void
1798 ath_tx_retry_subframe(struct ath_softc_tgt *sc, struct ath_tx_buf *bf,
1799                       ath_tx_bufhead *bf_q, struct ath_tx_buf **bar)
1800 {
1801
1802         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
1803         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
1804         struct ath_tx_desc *ds = NULL;
1805         struct ath_hal *ah = sc->sc_ah;
1806         int i = 0;
1807
1808         __stats(sc, txaggr_compretries);
1809
1810         for(ds = bf->bf_desc, i = 0; i < bf->bf_dmamap_info.nsegs; ds++, i++) {
1811                 ah->ah_clr11nAggr(ah, ds);
1812                 ah->ah_set11nBurstDuration(ah, ds, 0);
1813                 ah->ah_set11nVirtualMoreFrag(ah, ds, 0);
1814         }
1815
1816         if (bf->bf_retries >= OWLMAX_RETRIES) {
1817                 __stats(sc, txaggr_xretries);
1818                 ath_tx_update_baw(tid, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1819                 ath_tx_status_update_aggr(sc, bf, bf->bf_lastds, NULL, 0);
1820
1821                 if (!*bar)
1822                         *bar = bf;
1823                 else
1824                         ath_tx_freebuf(sc, bf);
1825                 return;
1826         }
1827
1828         if (!bf->bf_next) {
1829                 __stats(sc, txaggr_errlast);
1830                 bf = ath_buf_toggle(sc, bf, 1);
1831         } else
1832                 bf->bf_lastds = &(bf->bf_descarr[bf->bf_dmamap_info.nsegs - 1]);
1833
1834         ath_tx_set_retry(sc, bf);
1835         asf_tailq_insert_tail(bf_q, bf, bf_list);
1836 }
1837
1838 static void
1839 ath_update_aggr_stats(struct ath_softc_tgt *sc,
1840                       struct ath_tx_desc *ds, int nframes,
1841                       int nbad)
1842 {
1843
1844         u_int8_t status = ATH_DS_TX_STATUS(ds);
1845         u_int8_t txflags = ATH_DS_TX_FLAGS(ds);
1846
1847         __statsn(sc, txaggr_longretries, ds->ds_txstat.ts_longretry);
1848         __statsn(sc, txaggr_shortretries, ds->ds_txstat.ts_shortretry);
1849
1850         if (txflags & HAL_TX_DESC_CFG_ERR)
1851                 __stats(sc, txaggr_desc_cfgerr);
1852
1853         if (txflags & HAL_TX_DATA_UNDERRUN)
1854                 __stats(sc, txaggr_data_urun);
1855
1856         if (txflags & HAL_TX_DELIM_UNDERRUN)
1857                 __stats(sc, txaggr_delim_urun);
1858
1859         if (!status) {
1860                 return;
1861         }
1862
1863         if (status & HAL_TXERR_XRETRY)
1864                 __stats(sc, txaggr_compxretry);
1865
1866         if (status & HAL_TXERR_FILT)
1867                 __stats(sc, txaggr_filtered);
1868
1869         if (status & HAL_TXERR_FIFO)
1870                 __stats(sc, txaggr_fifo);
1871
1872         if (status & HAL_TXERR_XTXOP)
1873                 __stats(sc, txaggr_xtxop);
1874
1875         if (status & HAL_TXERR_TIMER_EXPIRED)
1876                 __stats(sc, txaggr_timer_exp);
1877 }
1878
1879 static void
1880 ath_tx_comp_unaggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1881 {
1882         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
1883         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
1884         struct ath_tx_desc *ds  = bf->bf_lastds;
1885
1886         ath_update_stats(sc, bf);
1887         ath_rate_tx_complete(sc, an, ds, bf->bf_rcs, 1, 0);
1888
1889         if (ATH_DS_TX_STATUS(ds) & HAL_TXERR_XRETRY) {
1890                 ath_tx_retry_unaggr(sc, bf);
1891                 return;
1892         }
1893         __stats(sc, tx_compunaggr);
1894
1895         ath_tx_update_baw(tid, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1896         ath_tx_status_update(sc, bf);
1897         ath_tx_freebuf(sc, bf);
1898 }
1899
1900 static inline void
1901 ath_tx_retry_unaggr(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1902 {
1903         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
1904         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
1905         struct ath_txq *txq;
1906
1907         txq = bf->bf_txq;
1908
1909         if (bf->bf_retries >= OWLMAX_RETRIES) {
1910                 __stats(sc, txunaggr_xretry);
1911                 ath_tx_update_baw(tid, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1912                 ath_tx_status_update(sc, bf);
1913                 ath_bar_tx(sc, tid, bf);
1914                 return;
1915         }
1916
1917         __stats(sc, txunaggr_compretries);
1918         if (!bf->bf_lastds->ds_link) {
1919                 __stats(sc, txunaggr_errlast);
1920                 bf = ath_buf_toggle(sc, bf, 1);
1921         }
1922
1923         ath_tx_set_retry(sc, bf);
1924         asf_tailq_insert_head(&tid->buf_q, bf, bf_list);
1925         ath_tgt_tx_enqueue(txq, tid);
1926 }
1927
1928 static void
1929 ath_tx_update_baw(ath_atx_tid_t *tid, int seqno)
1930 {
1931         int index;
1932         int cindex;
1933
1934         index  = ATH_BA_INDEX(tid->seq_start, seqno);
1935         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1936
1937         TX_BUF_BITMAP_CLR(tid->tx_buf_bitmap, cindex);
1938
1939         while (tid->baw_head != tid->baw_tail &&
1940                (!TX_BUF_BITMAP_IS_SET(tid->tx_buf_bitmap, tid->baw_head))) {
1941                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
1942                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
1943         }
1944 }
1945
1946 static void ath_tx_set_retry(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
1947 {
1948         struct ieee80211_frame *wh;
1949
1950         __stats(sc, txaggr_retries);
1951
1952         bf->bf_isretried = 1;
1953         bf->bf_retries ++;
1954         wh = ATH_SKB_2_WH(bf->bf_skb);
1955         wh->i_fc[1] |= IEEE80211_FC1_RETRY;
1956 }
1957
1958 void ath_tgt_tx_cleanup(struct ath_softc_tgt *sc, struct ath_node_target *an,
1959                         ath_atx_tid_t *tid, a_uint8_t discard_all)
1960 {
1961         struct ath_tx_buf *bf;
1962         struct ath_tx_buf *bf_next;
1963         struct ath_txq *txq;
1964
1965         txq = TID_TO_ACTXQ(tid->tidno);
1966
1967         bf = asf_tailq_first(&tid->buf_q);
1968
1969         while (bf) {
1970                 if (discard_all || bf->bf_isretried) {
1971                         bf_next = asf_tailq_next(bf, bf_list);
1972                         TAILQ_DEQ(&tid->buf_q, bf, bf_list);
1973                         if (bf->bf_isretried)
1974                                 ath_tx_update_baw(tid, SEQNO_FROM_BF_SEQNO(bf->bf_seqno));
1975                         ath_tx_freebuf(sc, bf);
1976                         bf = bf_next;
1977                         continue;
1978                 }
1979                 bf->bf_comp = ath_tgt_tx_comp_normal;
1980                 bf = asf_tailq_next(bf, bf_list);
1981         }
1982
1983         ath_aggr_pause_tid(sc, tid);
1984
1985         while (tid->baw_head != tid->baw_tail) {
1986                 if (TX_BUF_BITMAP_IS_SET(tid->tx_buf_bitmap, tid->baw_head)) {
1987                         tid->incomp++;
1988                         tid->flag |= TID_CLEANUP_INPROGRES;
1989                         TX_BUF_BITMAP_CLR(tid->tx_buf_bitmap, tid->baw_head);
1990                 }
1991                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
1992                 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
1993         }
1994
1995         if (!(tid->flag & TID_CLEANUP_INPROGRES)) {
1996                 ath_aggr_resume_tid(sc, tid);
1997         }
1998 }
1999
2000 /******************/
2001 /* BAR Management */
2002 /******************/
2003
2004 static void ath_tgt_delba_send(struct ath_softc_tgt *sc,
2005                                struct ieee80211_node_target *ni,
2006                                a_uint8_t tidno, a_uint8_t initiator,
2007                                a_uint16_t reasoncode)
2008 {
2009         struct ath_node_target *an = ATH_NODE_TARGET(ni);
2010         ath_atx_tid_t *tid = ATH_AN_2_TID(an, tidno);
2011         struct wmi_data_delba wmi_delba;
2012
2013         tid->flag &= ~TID_AGGR_ENABLED;
2014
2015         ath_tgt_tx_cleanup(sc, an, tid, 1);
2016
2017         wmi_delba.ni_nodeindex = ni->ni_nodeindex;
2018         wmi_delba.tidno = tid->tidno;
2019         wmi_delba.initiator = 1;
2020         wmi_delba.reasoncode = IEEE80211_REASON_UNSPECIFIED;
2021
2022         __stats(sc, txbar_xretry);
2023         wmi_event(sc->tgt_wmi_handle,
2024                   WMI_DELBA_EVENTID,
2025                   &wmi_delba,
2026                   sizeof(wmi_delba));
2027 }
2028
2029 static void ath_bar_retry(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
2030 {
2031         struct ath_node_target *an = ATH_NODE_TARGET(bf->bf_node);
2032         ath_atx_tid_t *tid = ATH_AN_2_TID(an, bf->bf_tidno);
2033
2034         if (bf->bf_retries >= OWLMAX_BAR_RETRIES) {
2035                 ath_tgt_delba_send(sc, bf->bf_node, tid->tidno, 1,
2036                                    IEEE80211_REASON_UNSPECIFIED);
2037                 ath_tgt_tid_drain(sc, tid);
2038
2039                 bf->bf_comp = NULL;
2040                 ath_buf_comp(sc, bf);
2041                 return;
2042         }
2043
2044         __stats(sc, txbar_compretries);
2045
2046         if (!bf->bf_lastds->ds_link) {
2047                 __stats(sc, txbar_errlast);
2048                 bf = ath_buf_toggle(sc, bf, 1);
2049         }
2050
2051         bf->bf_lastds->ds_link = 0;
2052
2053         ath_tx_set_retry(sc, bf);
2054         ath_tgt_txq_add_ucast(sc, bf);
2055 }
2056
2057 static void ath_bar_tx_comp(struct ath_softc_tgt *sc, struct ath_tx_buf *bf)
2058 {
2059         struct ath_tx_desc *ds = bf->bf_lastds;
2060         struct ath_node_target *an;
2061         ath_atx_tid_t *tid;
2062         struct ath_txq *txq;
2063
2064         an = (struct ath_node_target *)bf->bf_node;
2065         tid = &an->tid[bf->bf_tidno];
2066         txq = TID_TO_ACTXQ(tid->tidno);
2067
2068         if (ATH_DS_TX_STATUS(ds) & HAL_TXERR_XRETRY) {
2069                 ath_bar_retry(sc, bf);
2070                 return;
2071         }
2072
2073         ath_aggr_resume_tid(sc, tid);
2074
2075         bf->bf_comp = NULL;
2076         ath_buf_comp(sc, bf);
2077 }
2078
2079 static void ath_bar_tx(struct ath_softc_tgt *sc,
2080                        ath_atx_tid_t *tid, struct ath_tx_buf *bf)
2081 {
2082         adf_nbuf_t skb;
2083         struct ieee80211_frame_bar *bar;
2084         u_int8_t min_rate;
2085         struct ath_tx_desc *ds, *ds0;
2086         struct ath_hal *ah = sc->sc_ah;
2087         HAL_11N_RATE_SERIES series[4];
2088         int i = 0;
2089         adf_nbuf_queue_t skbhead;
2090         a_uint8_t *anbdata;
2091         a_uint32_t anblen;
2092
2093         __stats(sc, tx_bars);
2094
2095         adf_os_mem_set(&series, 0, sizeof(series));
2096
2097         ath_aggr_pause_tid(sc, tid);
2098
2099         skb = adf_nbuf_queue_remove(&bf->bf_skbhead);
2100         adf_nbuf_peek_header(skb, &anbdata, &anblen);
2101         adf_nbuf_trim_tail(skb, anblen);
2102         bar = (struct ieee80211_frame_bar *) anbdata;
2103
2104         min_rate =  0x0b;
2105
2106         ath_dma_unmap(sc, bf);
2107         adf_nbuf_queue_add(&bf->bf_skbhead, skb);
2108
2109         bar->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2110         bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2111                 IEEE80211_FC0_TYPE_CTL |
2112                 IEEE80211_FC0_SUBTYPE_BAR;
2113         bar->i_ctl = tid->tidno << IEEE80211_BAR_CTL_TID_S |
2114                 IEEE80211_BAR_CTL_COMBA;
2115         bar->i_seq = adf_os_cpu_to_le16(tid->seq_start << IEEE80211_SEQ_SEQ_SHIFT);
2116
2117         bf->bf_seqno = tid->seq_start << IEEE80211_SEQ_SEQ_SHIFT;
2118
2119         adf_nbuf_put_tail(skb, sizeof(struct ieee80211_frame_bar));
2120
2121         bf->bf_comp = ath_bar_tx_comp;
2122         bf->bf_tidno = tid->tidno;
2123         bf->bf_node = &tid->an->ni;
2124         ath_dma_map(sc, bf);
2125         adf_nbuf_dmamap_info(bf->bf_dmamap, &bf->bf_dmamap_info);
2126
2127         ds = bf->bf_desc;
2128         ah->ah_setupTxDesc(ds
2129                             , adf_nbuf_len(skb) + IEEE80211_CRC_LEN
2130                             , 0
2131                             , HAL_PKT_TYPE_NORMAL
2132                             , ATH_MIN(60, 60)
2133                             , min_rate
2134                             , ATH_TXMAXTRY
2135                             , bf->bf_keyix
2136                             , HAL_TXDESC_INTREQ
2137                             | HAL_TXDESC_CLRDMASK
2138                             , 0, 0);
2139
2140         skbhead = bf->bf_skbhead;
2141         bf->bf_isaggr = 0;
2142         bf->bf_next = NULL;
2143
2144         for (ds0 = ds, i=0; i < bf->bf_dmamap_info.nsegs; ds0++, i++) {
2145                 ah->ah_clr11nAggr(ah, ds0);
2146         }
2147
2148         ath_filltxdesc(sc, bf);
2149
2150         for (i = 0 ; i < 4; i++) {
2151                 series[i].Tries = ATH_TXMAXTRY;
2152                 series[i].Rate = min_rate;
2153                 series[i].ChSel = sc->sc_ic.ic_tx_chainmask;
2154         }
2155
2156         ah->ah_set11nRateScenario(ah, bf->bf_desc, 0, 0, 0, series, 4, 4);
2157         ath_tgt_txq_add_ucast(sc, bf);
2158 }