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