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