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