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