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