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