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