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