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