33e34a9dbec1fe4cd133ace2cadd6ee641e7f075
[dragonfly.git] / sys / dev / netif / ath / ath / if_ath_tx.c
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGES.
29  */
30
31 #include <sys/cdefs.h>
32
33 /*
34  * Driver for the Atheros Wireless LAN controller.
35  *
36  * This software is derived from work of Atsushi Onoe; his contribution
37  * is greatly appreciated.
38  */
39
40 #include "opt_inet.h"
41 #include "opt_ath.h"
42 #include "opt_wlan.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/errno.h>
55 #include <sys/callout.h>
56 #include <sys/bus.h>
57 #include <sys/endian.h>
58 #include <sys/kthread.h>
59 #include <sys/taskqueue.h>
60 #include <sys/priv.h>
61 #include <sys/ktr.h>
62
63 #include <machine/bus.h>
64
65 #include <net/if.h>
66 #include <net/if_var.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/if_arp.h>
71 #include <net/ethernet.h>
72 #include <net/if_llc.h>
73
74 #include <net80211/ieee80211_var.h>
75 #include <net80211/ieee80211_regdomain.h>
76 #ifdef IEEE80211_SUPPORT_SUPERG
77 #include <net80211/ieee80211_superg.h>
78 #endif
79 #ifdef IEEE80211_SUPPORT_TDMA
80 #include <net80211/ieee80211_tdma.h>
81 #endif
82 #include <net80211/ieee80211_ht.h>
83
84 #include <net/bpf.h>
85
86 #ifdef INET
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89 #endif
90
91 #include <dev/ath/if_athvar.h>
92 #include <dev/ath/ath_hal/ah_devid.h>           /* XXX for softled */
93 #include <dev/ath/ath_hal/ah_diagcodes.h>
94
95 #include <dev/ath/if_ath_debug.h>
96
97 #ifdef ATH_TX99_DIAG
98 #include <dev/ath/ath_tx99/ath_tx99.h>
99 #endif
100
101 #include <dev/ath/if_ath_misc.h>
102 #include <dev/ath/if_ath_tx.h>
103 #include <dev/ath/if_ath_tx_ht.h>
104
105 #ifdef  ATH_DEBUG_ALQ
106 #include <dev/ath/if_ath_alq.h>
107 #endif
108
109 /*
110  * How many retries to perform in software
111  */
112 #define SWMAX_RETRIES           10
113
114 /*
115  * What queue to throw the non-QoS TID traffic into
116  */
117 #define ATH_NONQOS_TID_AC       WME_AC_VO
118
119 #if 0
120 static int ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an);
121 #endif
122 static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
123     int tid);
124 static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
125     int tid);
126 static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
127     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
128 static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
129     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
130 static struct ath_buf *
131 ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
132     struct ath_tid *tid, struct ath_buf *bf);
133
134 #ifdef  ATH_DEBUG_ALQ
135 void
136 ath_tx_alq_post(struct ath_softc *sc, struct ath_buf *bf_first)
137 {
138         struct ath_buf *bf;
139         int i, n;
140         const char *ds;
141
142         /* XXX we should skip out early if debugging isn't enabled! */
143         bf = bf_first;
144
145         while (bf != NULL) {
146                 /* XXX should ensure bf_nseg > 0! */
147                 if (bf->bf_nseg == 0)
148                         break;
149                 n = ((bf->bf_nseg - 1) / sc->sc_tx_nmaps) + 1;
150                 for (i = 0, ds = (const char *) bf->bf_desc;
151                     i < n;
152                     i++, ds += sc->sc_tx_desclen) {
153                         if_ath_alq_post(&sc->sc_alq,
154                             ATH_ALQ_EDMA_TXDESC,
155                             sc->sc_tx_desclen,
156                             ds);
157                 }
158                 bf = bf->bf_next;
159         }
160 }
161 #endif /* ATH_DEBUG_ALQ */
162
163 /*
164  * Whether to use the 11n rate scenario functions or not
165  */
166 static inline int
167 ath_tx_is_11n(struct ath_softc *sc)
168 {
169         return ((sc->sc_ah->ah_magic == 0x20065416) ||
170                     (sc->sc_ah->ah_magic == 0x19741014));
171 }
172
173 /*
174  * Obtain the current TID from the given frame.
175  *
176  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
177  * This has implications for which AC/priority the packet is placed
178  * in.
179  */
180 static int
181 ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
182 {
183         const struct ieee80211_frame *wh;
184         int pri = M_WME_GETAC(m0);
185
186         wh = mtod(m0, const struct ieee80211_frame *);
187         if (! IEEE80211_QOS_HAS_SEQ(wh))
188                 return IEEE80211_NONQOS_TID;
189         else
190                 return WME_AC_TO_TID(pri);
191 }
192
193 static void
194 ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
195 {
196         struct ieee80211_frame *wh;
197
198         wh = mtod(bf->bf_m, struct ieee80211_frame *);
199         /* Only update/resync if needed */
200         if (bf->bf_state.bfs_isretried == 0) {
201                 wh->i_fc[1] |= IEEE80211_FC1_RETRY;
202                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
203                     BUS_DMASYNC_PREWRITE);
204         }
205         bf->bf_state.bfs_isretried = 1;
206         bf->bf_state.bfs_retries ++;
207 }
208
209 /*
210  * Determine what the correct AC queue for the given frame
211  * should be.
212  *
213  * This code assumes that the TIDs map consistently to
214  * the underlying hardware (or software) ath_txq.
215  * Since the sender may try to set an AC which is
216  * arbitrary, non-QoS TIDs may end up being put on
217  * completely different ACs. There's no way to put a
218  * TID into multiple ath_txq's for scheduling, so
219  * for now we override the AC/TXQ selection and set
220  * non-QOS TID frames into the BE queue.
221  *
222  * This may be completely incorrect - specifically,
223  * some management frames may end up out of order
224  * compared to the QoS traffic they're controlling.
225  * I'll look into this later.
226  */
227 static int
228 ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
229 {
230         const struct ieee80211_frame *wh;
231         int pri = M_WME_GETAC(m0);
232         wh = mtod(m0, const struct ieee80211_frame *);
233         if (IEEE80211_QOS_HAS_SEQ(wh))
234                 return pri;
235
236         return ATH_NONQOS_TID_AC;
237 }
238
239 void
240 ath_txfrag_cleanup(struct ath_softc *sc,
241         ath_bufhead *frags, struct ieee80211_node *ni)
242 {
243         struct ath_buf *bf, *next;
244
245         ATH_TXBUF_LOCK_ASSERT(sc);
246
247         TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
248                 /* NB: bf assumed clean */
249                 TAILQ_REMOVE(frags, bf, bf_list);
250                 ath_returnbuf_head(sc, bf);
251                 ieee80211_node_decref(ni);
252         }
253 }
254
255 /*
256  * Setup xmit of a fragmented frame.  Allocate a buffer
257  * for each frag and bump the node reference count to
258  * reflect the held reference to be setup by ath_tx_start.
259  */
260 int
261 ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
262         struct mbuf *m0, struct ieee80211_node *ni)
263 {
264         struct mbuf *m;
265         struct ath_buf *bf;
266
267         ATH_TXBUF_LOCK(sc);
268         for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
269                 /* XXX non-management? */
270                 bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
271                 if (bf == NULL) {       /* out of buffers, cleanup */
272                         DPRINTF(sc, ATH_DEBUG_XMIT, "%s: no buffer?\n",
273                             __func__);
274                         ath_txfrag_cleanup(sc, frags, ni);
275                         break;
276                 }
277                 ieee80211_node_incref(ni);
278                 TAILQ_INSERT_TAIL(frags, bf, bf_list);
279         }
280         ATH_TXBUF_UNLOCK(sc);
281
282         return !TAILQ_EMPTY(frags);
283 }
284
285 /*
286  * Reclaim mbuf resources.  For fragmented frames we
287  * need to claim each frag chained with m_nextpkt.
288  */
289 void
290 ath_freetx(struct mbuf *m)
291 {
292         struct mbuf *next;
293
294         do {
295                 next = m->m_nextpkt;
296                 m->m_nextpkt = NULL;
297                 m_freem(m);
298         } while ((m = next) != NULL);
299 }
300
301 static int
302 ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
303 {
304         struct mbuf *m;
305         int error;
306
307         /*
308          * Load the DMA map so any coalescing is done.  This
309          * also calculates the number of descriptors we need.
310          */
311         error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
312                                      bf->bf_segs, &bf->bf_nseg,
313                                      BUS_DMA_NOWAIT);
314         if (error == EFBIG) {
315                 /* XXX packet requires too many descriptors */
316                 bf->bf_nseg = ATH_MAX_SCATTER + 1;
317         } else if (error != 0) {
318                 sc->sc_stats.ast_tx_busdma++;
319                 ath_freetx(m0);
320                 return error;
321         }
322         /*
323          * Discard null packets and check for packets that
324          * require too many TX descriptors.  We try to convert
325          * the latter to a cluster.
326          */
327         if (bf->bf_nseg > ATH_MAX_SCATTER) {            /* too many desc's, linearize */
328                 sc->sc_stats.ast_tx_linear++;
329                 m = m_collapse(m0, M_NOWAIT, ATH_MAX_SCATTER);
330                 if (m == NULL) {
331                         ath_freetx(m0);
332                         sc->sc_stats.ast_tx_nombuf++;
333                         return ENOMEM;
334                 }
335                 m0 = m;
336                 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
337                                              bf->bf_segs, &bf->bf_nseg,
338                                              BUS_DMA_NOWAIT);
339                 if (error != 0) {
340                         sc->sc_stats.ast_tx_busdma++;
341                         ath_freetx(m0);
342                         return error;
343                 }
344                 KASSERT(bf->bf_nseg <= ATH_MAX_SCATTER,
345                     ("too many segments after defrag; nseg %u", bf->bf_nseg));
346         } else if (bf->bf_nseg == 0) {          /* null packet, discard */
347                 sc->sc_stats.ast_tx_nodata++;
348                 ath_freetx(m0);
349                 return EIO;
350         }
351         DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
352                 __func__, m0, m0->m_pkthdr.len);
353         bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
354         bf->bf_m = m0;
355
356         return 0;
357 }
358
359 /*
360  * Chain together segments+descriptors for a frame - 11n or otherwise.
361  *
362  * For aggregates, this is called on each frame in the aggregate.
363  */
364 static void
365 ath_tx_chaindesclist(struct ath_softc *sc, struct ath_desc *ds0,
366     struct ath_buf *bf, int is_aggr, int is_first_subframe,
367     int is_last_subframe)
368 {
369         struct ath_hal *ah = sc->sc_ah;
370         char *ds;
371         int i, bp, dsp;
372         HAL_DMA_ADDR bufAddrList[4];
373         uint32_t segLenList[4];
374         int numTxMaps = 1;
375         int isFirstDesc = 1;
376
377         /*
378          * XXX There's txdma and txdma_mgmt; the descriptor
379          * sizes must match.
380          */
381         struct ath_descdma *dd = &sc->sc_txdma;
382
383         /*
384          * Fillin the remainder of the descriptor info.
385          */
386
387         /*
388          * We need the number of TX data pointers in each descriptor.
389          * EDMA and later chips support 4 TX buffers per descriptor;
390          * previous chips just support one.
391          */
392         numTxMaps = sc->sc_tx_nmaps;
393
394         /*
395          * For EDMA and later chips ensure the TX map is fully populated
396          * before advancing to the next descriptor.
397          */
398         ds = (char *) bf->bf_desc;
399         bp = dsp = 0;
400         bzero(bufAddrList, sizeof(bufAddrList));
401         bzero(segLenList, sizeof(segLenList));
402         for (i = 0; i < bf->bf_nseg; i++) {
403                 bufAddrList[bp] = bf->bf_segs[i].ds_addr;
404                 segLenList[bp] = bf->bf_segs[i].ds_len;
405                 bp++;
406
407                 /*
408                  * Go to the next segment if this isn't the last segment
409                  * and there's space in the current TX map.
410                  */
411                 if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
412                         continue;
413
414                 /*
415                  * Last segment or we're out of buffer pointers.
416                  */
417                 bp = 0;
418
419                 if (i == bf->bf_nseg - 1)
420                         ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
421                 else
422                         ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
423                             bf->bf_daddr + dd->dd_descsize * (dsp + 1));
424
425                 /*
426                  * XXX This assumes that bfs_txq is the actual destination
427                  * hardware queue at this point.  It may not have been
428                  * assigned, it may actually be pointing to the multicast
429                  * software TXQ id.  These must be fixed!
430                  */
431                 ath_hal_filltxdesc(ah, (struct ath_desc *) ds
432                         , bufAddrList
433                         , segLenList
434                         , bf->bf_descid         /* XXX desc id */
435                         , bf->bf_state.bfs_tx_queue
436                         , isFirstDesc           /* first segment */
437                         , i == bf->bf_nseg - 1  /* last segment */
438                         , (struct ath_desc *) ds0       /* first descriptor */
439                 );
440
441                 /*
442                  * Make sure the 11n aggregate fields are cleared.
443                  *
444                  * XXX TODO: this doesn't need to be called for
445                  * aggregate frames; as it'll be called on all
446                  * sub-frames.  Since the descriptors are in
447                  * non-cacheable memory, this leads to some
448                  * rather slow writes on MIPS/ARM platforms.
449                  */
450                 if (ath_tx_is_11n(sc))
451                         ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
452
453                 /*
454                  * If 11n is enabled, set it up as if it's an aggregate
455                  * frame.
456                  */
457                 if (is_last_subframe) {
458                         ath_hal_set11n_aggr_last(sc->sc_ah,
459                             (struct ath_desc *) ds);
460                 } else if (is_aggr) {
461                         /*
462                          * This clears the aggrlen field; so
463                          * the caller needs to call set_aggr_first()!
464                          *
465                          * XXX TODO: don't call this for the first
466                          * descriptor in the first frame in an
467                          * aggregate!
468                          */
469                         ath_hal_set11n_aggr_middle(sc->sc_ah,
470                             (struct ath_desc *) ds,
471                             bf->bf_state.bfs_ndelim);
472                 }
473                 isFirstDesc = 0;
474                 bf->bf_lastds = (struct ath_desc *) ds;
475
476                 /*
477                  * Don't forget to skip to the next descriptor.
478                  */
479                 ds += sc->sc_tx_desclen;
480                 dsp++;
481
482                 /*
483                  * .. and don't forget to blank these out!
484                  */
485                 bzero(bufAddrList, sizeof(bufAddrList));
486                 bzero(segLenList, sizeof(segLenList));
487         }
488         bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
489 }
490
491 /*
492  * Set the rate control fields in the given descriptor based on
493  * the bf_state fields and node state.
494  *
495  * The bfs fields should already be set with the relevant rate
496  * control information, including whether MRR is to be enabled.
497  *
498  * Since the FreeBSD HAL currently sets up the first TX rate
499  * in ath_hal_setuptxdesc(), this will setup the MRR
500  * conditionally for the pre-11n chips, and call ath_buf_set_rate
501  * unconditionally for 11n chips. These require the 11n rate
502  * scenario to be set if MCS rates are enabled, so it's easier
503  * to just always call it. The caller can then only set rates 2, 3
504  * and 4 if multi-rate retry is needed.
505  */
506 static void
507 ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
508     struct ath_buf *bf)
509 {
510         struct ath_rc_series *rc = bf->bf_state.bfs_rc;
511
512         /* If mrr is disabled, blank tries 1, 2, 3 */
513         if (! bf->bf_state.bfs_ismrr)
514                 rc[1].tries = rc[2].tries = rc[3].tries = 0;
515
516 #if 0
517         /*
518          * If NOACK is set, just set ntries=1.
519          */
520         else if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
521                 rc[1].tries = rc[2].tries = rc[3].tries = 0;
522                 rc[0].tries = 1;
523         }
524 #endif
525
526         /*
527          * Always call - that way a retried descriptor will
528          * have the MRR fields overwritten.
529          *
530          * XXX TODO: see if this is really needed - setting up
531          * the first descriptor should set the MRR fields to 0
532          * for us anyway.
533          */
534         if (ath_tx_is_11n(sc)) {
535                 ath_buf_set_rate(sc, ni, bf);
536         } else {
537                 ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
538                         , rc[1].ratecode, rc[1].tries
539                         , rc[2].ratecode, rc[2].tries
540                         , rc[3].ratecode, rc[3].tries
541                 );
542         }
543 }
544
545 /*
546  * Setup segments+descriptors for an 11n aggregate.
547  * bf_first is the first buffer in the aggregate.
548  * The descriptor list must already been linked together using
549  * bf->bf_next.
550  */
551 static void
552 ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
553 {
554         struct ath_buf *bf, *bf_prev = NULL;
555         struct ath_desc *ds0 = bf_first->bf_desc;
556
557         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
558             __func__, bf_first->bf_state.bfs_nframes,
559             bf_first->bf_state.bfs_al);
560
561         bf = bf_first;
562
563         if (bf->bf_state.bfs_txrate0 == 0)
564                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, txrate0=%d\n",
565                     __func__, bf, 0);
566         if (bf->bf_state.bfs_rc[0].ratecode == 0)
567                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, rix0=%d\n",
568                     __func__, bf, 0);
569
570         /*
571          * Setup all descriptors of all subframes - this will
572          * call ath_hal_set11naggrmiddle() on every frame.
573          */
574         while (bf != NULL) {
575                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
576                     "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
577                     __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
578                     SEQNO(bf->bf_state.bfs_seqno));
579
580                 /*
581                  * Setup the initial fields for the first descriptor - all
582                  * the non-11n specific stuff.
583                  */
584                 ath_hal_setuptxdesc(sc->sc_ah, bf->bf_desc
585                         , bf->bf_state.bfs_pktlen       /* packet length */
586                         , bf->bf_state.bfs_hdrlen       /* header length */
587                         , bf->bf_state.bfs_atype        /* Atheros packet type */
588                         , bf->bf_state.bfs_txpower      /* txpower */
589                         , bf->bf_state.bfs_txrate0
590                         , bf->bf_state.bfs_try0         /* series 0 rate/tries */
591                         , bf->bf_state.bfs_keyix        /* key cache index */
592                         , bf->bf_state.bfs_txantenna    /* antenna mode */
593                         , bf->bf_state.bfs_txflags | HAL_TXDESC_INTREQ  /* flags */
594                         , bf->bf_state.bfs_ctsrate      /* rts/cts rate */
595                         , bf->bf_state.bfs_ctsduration  /* rts/cts duration */
596                 );
597
598                 /*
599                  * First descriptor? Setup the rate control and initial
600                  * aggregate header information.
601                  */
602                 if (bf == bf_first) {
603                         /*
604                          * setup first desc with rate and aggr info
605                          */
606                         ath_tx_set_ratectrl(sc, bf->bf_node, bf);
607                 }
608
609                 /*
610                  * Setup the descriptors for a multi-descriptor frame.
611                  * This is both aggregate and non-aggregate aware.
612                  */
613                 ath_tx_chaindesclist(sc, ds0, bf,
614                     1, /* is_aggr */
615                     !! (bf == bf_first), /* is_first_subframe */
616                     !! (bf->bf_next == NULL) /* is_last_subframe */
617                     );
618
619                 if (bf == bf_first) {
620                         /*
621                          * Initialise the first 11n aggregate with the
622                          * aggregate length and aggregate enable bits.
623                          */
624                         ath_hal_set11n_aggr_first(sc->sc_ah,
625                             ds0,
626                             bf->bf_state.bfs_al,
627                             bf->bf_state.bfs_ndelim);
628                 }
629
630                 /*
631                  * Link the last descriptor of the previous frame
632                  * to the beginning descriptor of this frame.
633                  */
634                 if (bf_prev != NULL)
635                         ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
636                             bf->bf_daddr);
637
638                 /* Save a copy so we can link the next descriptor in */
639                 bf_prev = bf;
640                 bf = bf->bf_next;
641         }
642
643         /*
644          * Set the first descriptor bf_lastds field to point to
645          * the last descriptor in the last subframe, that's where
646          * the status update will occur.
647          */
648         bf_first->bf_lastds = bf_prev->bf_lastds;
649
650         /*
651          * And bf_last in the first descriptor points to the end of
652          * the aggregate list.
653          */
654         bf_first->bf_last = bf_prev;
655
656         /*
657          * For non-AR9300 NICs, which require the rate control
658          * in the final descriptor - let's set that up now.
659          *
660          * This is because the filltxdesc() HAL call doesn't
661          * populate the last segment with rate control information
662          * if firstSeg is also true.  For non-aggregate frames
663          * that is fine, as the first frame already has rate control
664          * info.  But if the last frame in an aggregate has one
665          * descriptor, both firstseg and lastseg will be true and
666          * the rate info isn't copied.
667          *
668          * This is inefficient on MIPS/ARM platforms that have
669          * non-cachable memory for TX descriptors, but we'll just
670          * make do for now.
671          *
672          * As to why the rate table is stashed in the last descriptor
673          * rather than the first descriptor?  Because proctxdesc()
674          * is called on the final descriptor in an MPDU or A-MPDU -
675          * ie, the one that gets updated by the hardware upon
676          * completion.  That way proctxdesc() doesn't need to know
677          * about the first _and_ last TX descriptor.
678          */
679         ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_lastds, ds0);
680
681         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
682 }
683
684 /*
685  * Hand-off a frame to the multicast TX queue.
686  *
687  * This is a software TXQ which will be appended to the CAB queue
688  * during the beacon setup code.
689  *
690  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
691  * as part of the TX descriptor, bf_state.bfs_tx_queue must be updated
692  * with the actual hardware txq, or all of this will fall apart.
693  *
694  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
695  * and retire bfs_tx_queue; then make sure the CABQ QCU ID is populated
696  * correctly.
697  */
698 static void
699 ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
700     struct ath_buf *bf)
701 {
702         ATH_TX_LOCK_ASSERT(sc);
703
704         KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
705              ("%s: busy status 0x%x", __func__, bf->bf_flags));
706
707         /*
708          * Ensure that the tx queue is the cabq, so things get
709          * mapped correctly.
710          */
711         if (bf->bf_state.bfs_tx_queue != sc->sc_cabq->axq_qnum) {
712                 DPRINTF(sc, ATH_DEBUG_XMIT,
713                     "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
714                     __func__, bf, bf->bf_state.bfs_tx_queue,
715                     txq->axq_qnum);
716         }
717
718         ATH_TXQ_LOCK(txq);
719         if (ATH_TXQ_LAST(txq, axq_q_s) != NULL) {
720                 struct ath_buf *bf_last = ATH_TXQ_LAST(txq, axq_q_s);
721                 struct ieee80211_frame *wh;
722
723                 /* mark previous frame */
724                 wh = mtod(bf_last->bf_m, struct ieee80211_frame *);
725                 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
726                 bus_dmamap_sync(sc->sc_dmat, bf_last->bf_dmamap,
727                     BUS_DMASYNC_PREWRITE);
728
729                 /* link descriptor */
730                 ath_hal_settxdesclink(sc->sc_ah,
731                     bf_last->bf_lastds,
732                     bf->bf_daddr);
733         }
734         ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
735         ATH_TXQ_UNLOCK(txq);
736 }
737
738 /*
739  * Hand-off packet to a hardware queue.
740  */
741 static void
742 ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
743     struct ath_buf *bf)
744 {
745         struct ath_hal *ah = sc->sc_ah;
746         struct ath_buf *bf_first;
747
748         /*
749          * Insert the frame on the outbound list and pass it on
750          * to the hardware.  Multicast frames buffered for power
751          * save stations and transmit from the CAB queue are stored
752          * on a s/w only queue and loaded on to the CAB queue in
753          * the SWBA handler since frames only go out on DTIM and
754          * to avoid possible races.
755          */
756         ATH_TX_LOCK_ASSERT(sc);
757         KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
758              ("%s: busy status 0x%x", __func__, bf->bf_flags));
759         KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
760              ("ath_tx_handoff_hw called for mcast queue"));
761
762         /*
763          * XXX racy, should hold the PCU lock when checking this,
764          * and also should ensure that the TX counter is >0!
765          */
766         KASSERT((sc->sc_inreset_cnt == 0),
767             ("%s: TX during reset?\n", __func__));
768
769 #if 0
770         /*
771          * This causes a LOR. Find out where the PCU lock is being
772          * held whilst the TXQ lock is grabbed - that shouldn't
773          * be occuring.
774          */
775         ATH_PCU_LOCK(sc);
776         if (sc->sc_inreset_cnt) {
777                 ATH_PCU_UNLOCK(sc);
778                 DPRINTF(sc, ATH_DEBUG_RESET,
779                     "%s: called with sc_in_reset != 0\n",
780                     __func__);
781                 DPRINTF(sc, ATH_DEBUG_XMIT,
782                     "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
783                     __func__, txq->axq_qnum,
784                     (caddr_t)bf->bf_daddr, bf->bf_desc,
785                     txq->axq_depth);
786                 /* XXX axq_link needs to be set and updated! */
787                 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
788                 if (bf->bf_state.bfs_aggr)
789                         txq->axq_aggr_depth++;
790                 return;
791                 }
792         ATH_PCU_UNLOCK(sc);
793 #endif
794
795         ATH_TXQ_LOCK(txq);
796
797         /*
798          * XXX TODO: if there's a holdingbf, then
799          * ATH_TXQ_PUTRUNNING should be clear.
800          *
801          * If there is a holdingbf and the list is empty,
802          * then axq_link should be pointing to the holdingbf.
803          *
804          * Otherwise it should point to the last descriptor
805          * in the last ath_buf.
806          *
807          * In any case, we should really ensure that we
808          * update the previous descriptor link pointer to
809          * this descriptor, regardless of all of the above state.
810          *
811          * For now this is captured by having axq_link point
812          * to either the holdingbf (if the TXQ list is empty)
813          * or the end of the list (if the TXQ list isn't empty.)
814          * I'd rather just kill axq_link here and do it as above.
815          */
816
817         /*
818          * Append the frame to the TX queue.
819          */
820         ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
821         ATH_KTR(sc, ATH_KTR_TX, 3,
822             "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
823             "depth=%d",
824             txq->axq_qnum,
825             bf,
826             txq->axq_depth);
827
828         /*
829          * If there's a link pointer, update it.
830          *
831          * XXX we should replace this with the above logic, just
832          * to kill axq_link with fire.
833          */
834         if (txq->axq_link != NULL) {
835                 *txq->axq_link = bf->bf_daddr;
836                 DPRINTF(sc, ATH_DEBUG_XMIT,
837                     "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
838                     txq->axq_qnum, txq->axq_link,
839                     (caddr_t)bf->bf_daddr, bf->bf_desc,
840                     txq->axq_depth);
841                 ATH_KTR(sc, ATH_KTR_TX, 5,
842                     "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
843                     "lastds=%d",
844                     txq->axq_qnum, txq->axq_link,
845                     (caddr_t)bf->bf_daddr, bf->bf_desc,
846                     bf->bf_lastds);
847         }
848
849         /*
850          * If we've not pushed anything into the hardware yet,
851          * push the head of the queue into the TxDP.
852          *
853          * Once we've started DMA, there's no guarantee that
854          * updating the TxDP with a new value will actually work.
855          * So we just don't do that - if we hit the end of the list,
856          * we keep that buffer around (the "holding buffer") and
857          * re-start DMA by updating the link pointer of _that_
858          * descriptor and then restart DMA.
859          */
860         if (! (txq->axq_flags & ATH_TXQ_PUTRUNNING)) {
861                 bf_first = TAILQ_FIRST(&txq->axq_q);
862                 txq->axq_flags |= ATH_TXQ_PUTRUNNING;
863                 ath_hal_puttxbuf(ah, txq->axq_qnum, bf_first->bf_daddr);
864                 DPRINTF(sc, ATH_DEBUG_XMIT,
865                     "%s: TXDP[%u] = %p (%p) depth %d\n",
866                     __func__, txq->axq_qnum,
867                     (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
868                     txq->axq_depth);
869                 ATH_KTR(sc, ATH_KTR_TX, 5,
870                     "ath_tx_handoff: TXDP[%u] = %p (%p) "
871                     "lastds=%p depth %d",
872                     txq->axq_qnum,
873                     (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
874                     bf_first->bf_lastds,
875                     txq->axq_depth);
876         }
877
878         /*
879          * Ensure that the bf TXQ matches this TXQ, so later
880          * checking and holding buffer manipulation is sane.
881          */
882         if (bf->bf_state.bfs_tx_queue != txq->axq_qnum) {
883                 DPRINTF(sc, ATH_DEBUG_XMIT,
884                     "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
885                     __func__, bf, bf->bf_state.bfs_tx_queue,
886                     txq->axq_qnum);
887         }
888
889         /*
890          * Track aggregate queue depth.
891          */
892         if (bf->bf_state.bfs_aggr)
893                 txq->axq_aggr_depth++;
894
895         /*
896          * Update the link pointer.
897          */
898         ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
899
900         /*
901          * Start DMA.
902          *
903          * If we wrote a TxDP above, DMA will start from here.
904          *
905          * If DMA is running, it'll do nothing.
906          *
907          * If the DMA engine hit the end of the QCU list (ie LINK=NULL,
908          * or VEOL) then it stops at the last transmitted write.
909          * We then append a new frame by updating the link pointer
910          * in that descriptor and then kick TxE here; it will re-read
911          * that last descriptor and find the new descriptor to transmit.
912          *
913          * This is why we keep the holding descriptor around.
914          */
915         ath_hal_txstart(ah, txq->axq_qnum);
916         ATH_TXQ_UNLOCK(txq);
917         ATH_KTR(sc, ATH_KTR_TX, 1,
918             "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
919 }
920
921 /*
922  * Restart TX DMA for the given TXQ.
923  *
924  * This must be called whether the queue is empty or not.
925  */
926 static void
927 ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
928 {
929         struct ath_buf *bf, *bf_last;
930
931         ATH_TXQ_LOCK_ASSERT(txq);
932
933         /* XXX make this ATH_TXQ_FIRST */
934         bf = TAILQ_FIRST(&txq->axq_q);
935         bf_last = ATH_TXQ_LAST(txq, axq_q_s);
936
937         if (bf == NULL)
938                 return;
939
940         DPRINTF(sc, ATH_DEBUG_RESET,
941             "%s: Q%d: bf=%p, bf_last=%p, daddr=0x%08x\n",
942             __func__,
943             txq->axq_qnum,
944             bf,
945             bf_last,
946             (uint32_t) bf->bf_daddr);
947
948 #ifdef  ATH_DEBUG
949         if (sc->sc_debug & ATH_DEBUG_RESET)
950                 ath_tx_dump(sc, txq);
951 #endif
952
953         /*
954          * This is called from a restart, so DMA is known to be
955          * completely stopped.
956          */
957         KASSERT((!(txq->axq_flags & ATH_TXQ_PUTRUNNING)),
958             ("%s: Q%d: called with PUTRUNNING=1\n",
959             __func__,
960             txq->axq_qnum));
961
962         ath_hal_puttxbuf(sc->sc_ah, txq->axq_qnum, bf->bf_daddr);
963         txq->axq_flags |= ATH_TXQ_PUTRUNNING;
964
965         ath_hal_gettxdesclinkptr(sc->sc_ah, bf_last->bf_lastds,
966             &txq->axq_link);
967         ath_hal_txstart(sc->sc_ah, txq->axq_qnum);
968 }
969
970 /*
971  * Hand off a packet to the hardware (or mcast queue.)
972  *
973  * The relevant hardware txq should be locked.
974  */
975 static void
976 ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
977     struct ath_buf *bf)
978 {
979         ATH_TX_LOCK_ASSERT(sc);
980
981 #ifdef  ATH_DEBUG_ALQ
982         if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_TXDESC))
983                 ath_tx_alq_post(sc, bf);
984 #endif
985
986         if (txq->axq_qnum == ATH_TXQ_SWQ)
987                 ath_tx_handoff_mcast(sc, txq, bf);
988         else
989                 ath_tx_handoff_hw(sc, txq, bf);
990 }
991
992 static int
993 ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
994     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
995     int *keyix)
996 {
997         DPRINTF(sc, ATH_DEBUG_XMIT,
998             "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
999             __func__,
1000             *hdrlen,
1001             *pktlen,
1002             isfrag,
1003             iswep,
1004             m0);
1005
1006         if (iswep) {
1007                 const struct ieee80211_cipher *cip;
1008                 struct ieee80211_key *k;
1009
1010                 /*
1011                  * Construct the 802.11 header+trailer for an encrypted
1012                  * frame. The only reason this can fail is because of an
1013                  * unknown or unsupported cipher/key type.
1014                  */
1015                 k = ieee80211_crypto_encap(ni, m0);
1016                 if (k == NULL) {
1017                         /*
1018                          * This can happen when the key is yanked after the
1019                          * frame was queued.  Just discard the frame; the
1020                          * 802.11 layer counts failures and provides
1021                          * debugging/diagnostics.
1022                          */
1023                         return (0);
1024                 }
1025                 /*
1026                  * Adjust the packet + header lengths for the crypto
1027                  * additions and calculate the h/w key index.  When
1028                  * a s/w mic is done the frame will have had any mic
1029                  * added to it prior to entry so m0->m_pkthdr.len will
1030                  * account for it. Otherwise we need to add it to the
1031                  * packet length.
1032                  */
1033                 cip = k->wk_cipher;
1034                 (*hdrlen) += cip->ic_header;
1035                 (*pktlen) += cip->ic_header + cip->ic_trailer;
1036                 /* NB: frags always have any TKIP MIC done in s/w */
1037                 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
1038                         (*pktlen) += cip->ic_miclen;
1039                 (*keyix) = k->wk_keyix;
1040         } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
1041                 /*
1042                  * Use station key cache slot, if assigned.
1043                  */
1044                 (*keyix) = ni->ni_ucastkey.wk_keyix;
1045                 if ((*keyix) == IEEE80211_KEYIX_NONE)
1046                         (*keyix) = HAL_TXKEYIX_INVALID;
1047         } else
1048                 (*keyix) = HAL_TXKEYIX_INVALID;
1049
1050         return (1);
1051 }
1052
1053 /*
1054  * Calculate whether interoperability protection is required for
1055  * this frame.
1056  *
1057  * This requires the rate control information be filled in,
1058  * as the protection requirement depends upon the current
1059  * operating mode / PHY.
1060  */
1061 static void
1062 ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
1063 {
1064         struct ieee80211_frame *wh;
1065         uint8_t rix;
1066         uint16_t flags;
1067         int shortPreamble;
1068         const HAL_RATE_TABLE *rt = sc->sc_currates;
1069         struct ifnet *ifp = sc->sc_ifp;
1070         struct ieee80211com *ic = ifp->if_l2com;
1071
1072         flags = bf->bf_state.bfs_txflags;
1073         rix = bf->bf_state.bfs_rc[0].rix;
1074         shortPreamble = bf->bf_state.bfs_shpream;
1075         wh = mtod(bf->bf_m, struct ieee80211_frame *);
1076
1077         /*
1078          * If 802.11g protection is enabled, determine whether
1079          * to use RTS/CTS or just CTS.  Note that this is only
1080          * done for OFDM unicast frames.
1081          */
1082         if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1083             rt->info[rix].phy == IEEE80211_T_OFDM &&
1084             (flags & HAL_TXDESC_NOACK) == 0) {
1085                 bf->bf_state.bfs_doprot = 1;
1086                 /* XXX fragments must use CCK rates w/ protection */
1087                 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1088                         flags |= HAL_TXDESC_RTSENA;
1089                 } else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1090                         flags |= HAL_TXDESC_CTSENA;
1091                 }
1092                 /*
1093                  * For frags it would be desirable to use the
1094                  * highest CCK rate for RTS/CTS.  But stations
1095                  * farther away may detect it at a lower CCK rate
1096                  * so use the configured protection rate instead
1097                  * (for now).
1098                  */
1099                 sc->sc_stats.ast_tx_protect++;
1100         }
1101
1102         /*
1103          * If 11n protection is enabled and it's a HT frame,
1104          * enable RTS.
1105          *
1106          * XXX ic_htprotmode or ic_curhtprotmode?
1107          * XXX should it_htprotmode only matter if ic_curhtprotmode 
1108          * XXX indicates it's not a HT pure environment?
1109          */
1110         if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1111             rt->info[rix].phy == IEEE80211_T_HT &&
1112             (flags & HAL_TXDESC_NOACK) == 0) {
1113                 flags |= HAL_TXDESC_RTSENA;
1114                 sc->sc_stats.ast_tx_htprotect++;
1115         }
1116         bf->bf_state.bfs_txflags = flags;
1117 }
1118
1119 /*
1120  * Update the frame duration given the currently selected rate.
1121  *
1122  * This also updates the frame duration value, so it will require
1123  * a DMA flush.
1124  */
1125 static void
1126 ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1127 {
1128         struct ieee80211_frame *wh;
1129         uint8_t rix;
1130         uint16_t flags;
1131         int shortPreamble;
1132         struct ath_hal *ah = sc->sc_ah;
1133         const HAL_RATE_TABLE *rt = sc->sc_currates;
1134         int isfrag = bf->bf_m->m_flags & M_FRAG;
1135
1136         flags = bf->bf_state.bfs_txflags;
1137         rix = bf->bf_state.bfs_rc[0].rix;
1138         shortPreamble = bf->bf_state.bfs_shpream;
1139         wh = mtod(bf->bf_m, struct ieee80211_frame *);
1140
1141         /*
1142          * Calculate duration.  This logically belongs in the 802.11
1143          * layer but it lacks sufficient information to calculate it.
1144          */
1145         if ((flags & HAL_TXDESC_NOACK) == 0 &&
1146             (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1147                 u_int16_t dur;
1148                 if (shortPreamble)
1149                         dur = rt->info[rix].spAckDuration;
1150                 else
1151                         dur = rt->info[rix].lpAckDuration;
1152                 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1153                         dur += dur;             /* additional SIFS+ACK */
1154                         /*
1155                          * Include the size of next fragment so NAV is
1156                          * updated properly.  The last fragment uses only
1157                          * the ACK duration
1158                          *
1159                          * XXX TODO: ensure that the rate lookup for each
1160                          * fragment is the same as the rate used by the
1161                          * first fragment!
1162                          */
1163                         dur += ath_hal_computetxtime(ah,
1164                             rt,
1165                             bf->bf_nextfraglen,
1166                             rix, shortPreamble);
1167                 }
1168                 if (isfrag) {
1169                         /*
1170                          * Force hardware to use computed duration for next
1171                          * fragment by disabling multi-rate retry which updates
1172                          * duration based on the multi-rate duration table.
1173                          */
1174                         bf->bf_state.bfs_ismrr = 0;
1175                         bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1176                         /* XXX update bfs_rc[0].try? */
1177                 }
1178
1179                 /* Update the duration field itself */
1180                 *(u_int16_t *)wh->i_dur = htole16(dur);
1181         }
1182 }
1183
1184 static uint8_t
1185 ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1186     int cix, int shortPreamble)
1187 {
1188         uint8_t ctsrate;
1189
1190         /*
1191          * CTS transmit rate is derived from the transmit rate
1192          * by looking in the h/w rate table.  We must also factor
1193          * in whether or not a short preamble is to be used.
1194          */
1195         /* NB: cix is set above where RTS/CTS is enabled */
1196         KASSERT(cix != 0xff, ("cix not setup"));
1197         ctsrate = rt->info[cix].rateCode;
1198
1199         /* XXX this should only matter for legacy rates */
1200         if (shortPreamble)
1201                 ctsrate |= rt->info[cix].shortPreamble;
1202
1203         return (ctsrate);
1204 }
1205
1206 /*
1207  * Calculate the RTS/CTS duration for legacy frames.
1208  */
1209 static int
1210 ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1211     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1212     int flags)
1213 {
1214         int ctsduration = 0;
1215
1216         /* This mustn't be called for HT modes */
1217         if (rt->info[cix].phy == IEEE80211_T_HT) {
1218                 printf("%s: HT rate where it shouldn't be (0x%x)\n",
1219                     __func__, rt->info[cix].rateCode);
1220                 return (-1);
1221         }
1222
1223         /*
1224          * Compute the transmit duration based on the frame
1225          * size and the size of an ACK frame.  We call into the
1226          * HAL to do the computation since it depends on the
1227          * characteristics of the actual PHY being used.
1228          *
1229          * NB: CTS is assumed the same size as an ACK so we can
1230          *     use the precalculated ACK durations.
1231          */
1232         if (shortPreamble) {
1233                 if (flags & HAL_TXDESC_RTSENA)          /* SIFS + CTS */
1234                         ctsduration += rt->info[cix].spAckDuration;
1235                 ctsduration += ath_hal_computetxtime(ah,
1236                         rt, pktlen, rix, AH_TRUE);
1237                 if ((flags & HAL_TXDESC_NOACK) == 0)    /* SIFS + ACK */
1238                         ctsduration += rt->info[rix].spAckDuration;
1239         } else {
1240                 if (flags & HAL_TXDESC_RTSENA)          /* SIFS + CTS */
1241                         ctsduration += rt->info[cix].lpAckDuration;
1242                 ctsduration += ath_hal_computetxtime(ah,
1243                         rt, pktlen, rix, AH_FALSE);
1244                 if ((flags & HAL_TXDESC_NOACK) == 0)    /* SIFS + ACK */
1245                         ctsduration += rt->info[rix].lpAckDuration;
1246         }
1247
1248         return (ctsduration);
1249 }
1250
1251 /*
1252  * Update the given ath_buf with updated rts/cts setup and duration
1253  * values.
1254  *
1255  * To support rate lookups for each software retry, the rts/cts rate
1256  * and cts duration must be re-calculated.
1257  *
1258  * This function assumes the RTS/CTS flags have been set as needed;
1259  * mrr has been disabled; and the rate control lookup has been done.
1260  *
1261  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1262  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1263  */
1264 static void
1265 ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1266 {
1267         uint16_t ctsduration = 0;
1268         uint8_t ctsrate = 0;
1269         uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1270         uint8_t cix = 0;
1271         const HAL_RATE_TABLE *rt = sc->sc_currates;
1272
1273         /*
1274          * No RTS/CTS enabled? Don't bother.
1275          */
1276         if ((bf->bf_state.bfs_txflags &
1277             (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1278                 /* XXX is this really needed? */
1279                 bf->bf_state.bfs_ctsrate = 0;
1280                 bf->bf_state.bfs_ctsduration = 0;
1281                 return;
1282         }
1283
1284         /*
1285          * If protection is enabled, use the protection rix control
1286          * rate. Otherwise use the rate0 control rate.
1287          */
1288         if (bf->bf_state.bfs_doprot)
1289                 rix = sc->sc_protrix;
1290         else
1291                 rix = bf->bf_state.bfs_rc[0].rix;
1292
1293         /*
1294          * If the raw path has hard-coded ctsrate0 to something,
1295          * use it.
1296          */
1297         if (bf->bf_state.bfs_ctsrate0 != 0)
1298                 cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1299         else
1300                 /* Control rate from above */
1301                 cix = rt->info[rix].controlRate;
1302
1303         /* Calculate the rtscts rate for the given cix */
1304         ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1305             bf->bf_state.bfs_shpream);
1306
1307         /* The 11n chipsets do ctsduration calculations for you */
1308         if (! ath_tx_is_11n(sc))
1309                 ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1310                     bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1311                     rt, bf->bf_state.bfs_txflags);
1312
1313         /* Squirrel away in ath_buf */
1314         bf->bf_state.bfs_ctsrate = ctsrate;
1315         bf->bf_state.bfs_ctsduration = ctsduration;
1316         
1317         /*
1318          * Must disable multi-rate retry when using RTS/CTS.
1319          */
1320         if (!sc->sc_mrrprot) {
1321                 bf->bf_state.bfs_ismrr = 0;
1322                 bf->bf_state.bfs_try0 =
1323                     bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1324         }
1325 }
1326
1327 /*
1328  * Setup the descriptor chain for a normal or fast-frame
1329  * frame.
1330  *
1331  * XXX TODO: extend to include the destination hardware QCU ID.
1332  * Make sure that is correct.  Make sure that when being added
1333  * to the mcastq, the CABQ QCUID is set or things will get a bit
1334  * odd.
1335  */
1336 static void
1337 ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1338 {
1339         struct ath_desc *ds = bf->bf_desc;
1340         struct ath_hal *ah = sc->sc_ah;
1341
1342         if (bf->bf_state.bfs_txrate0 == 0)
1343                 DPRINTF(sc, ATH_DEBUG_XMIT, 
1344                     "%s: bf=%p, txrate0=%d\n", __func__, bf, 0);
1345
1346         ath_hal_setuptxdesc(ah, ds
1347                 , bf->bf_state.bfs_pktlen       /* packet length */
1348                 , bf->bf_state.bfs_hdrlen       /* header length */
1349                 , bf->bf_state.bfs_atype        /* Atheros packet type */
1350                 , bf->bf_state.bfs_txpower      /* txpower */
1351                 , bf->bf_state.bfs_txrate0
1352                 , bf->bf_state.bfs_try0         /* series 0 rate/tries */
1353                 , bf->bf_state.bfs_keyix        /* key cache index */
1354                 , bf->bf_state.bfs_txantenna    /* antenna mode */
1355                 , bf->bf_state.bfs_txflags      /* flags */
1356                 , bf->bf_state.bfs_ctsrate      /* rts/cts rate */
1357                 , bf->bf_state.bfs_ctsduration  /* rts/cts duration */
1358         );
1359
1360         /*
1361          * This will be overriden when the descriptor chain is written.
1362          */
1363         bf->bf_lastds = ds;
1364         bf->bf_last = bf;
1365
1366         /* Set rate control and descriptor chain for this frame */
1367         ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1368         ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1369 }
1370
1371 /*
1372  * Do a rate lookup.
1373  *
1374  * This performs a rate lookup for the given ath_buf only if it's required.
1375  * Non-data frames and raw frames don't require it.
1376  *
1377  * This populates the primary and MRR entries; MRR values are
1378  * then disabled later on if something requires it (eg RTS/CTS on
1379  * pre-11n chipsets.
1380  *
1381  * This needs to be done before the RTS/CTS fields are calculated
1382  * as they may depend upon the rate chosen.
1383  */
1384 static void
1385 ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1386 {
1387         uint8_t rate, rix;
1388         int try0;
1389
1390         if (! bf->bf_state.bfs_doratelookup)
1391                 return;
1392
1393         /* Get rid of any previous state */
1394         bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1395
1396         ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1397         ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1398             bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1399
1400         /* In case MRR is disabled, make sure rc[0] is setup correctly */
1401         bf->bf_state.bfs_rc[0].rix = rix;
1402         bf->bf_state.bfs_rc[0].ratecode = rate;
1403         bf->bf_state.bfs_rc[0].tries = try0;
1404
1405         if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1406                 ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1407                     bf->bf_state.bfs_rc);
1408         ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1409
1410         sc->sc_txrix = rix;     /* for LED blinking */
1411         sc->sc_lastdatarix = rix;       /* for fast frames */
1412         bf->bf_state.bfs_try0 = try0;
1413         bf->bf_state.bfs_txrate0 = rate;
1414 }
1415
1416 /*
1417  * Update the CLRDMASK bit in the ath_buf if it needs to be set.
1418  */
1419 static void
1420 ath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
1421     struct ath_buf *bf)
1422 {
1423         struct ath_node *an = ATH_NODE(bf->bf_node);
1424
1425         ATH_TX_LOCK_ASSERT(sc);
1426
1427         if (an->clrdmask == 1) {
1428                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1429                 an->clrdmask = 0;
1430         }
1431 }
1432
1433 /*
1434  * Return whether this frame should be software queued or
1435  * direct dispatched.
1436  *
1437  * When doing powersave, BAR frames should be queued but other management
1438  * frames should be directly sent.
1439  *
1440  * When not doing powersave, stick BAR frames into the hardware queue
1441  * so it goes out even though the queue is paused.
1442  *
1443  * For now, management frames are also software queued by default.
1444  */
1445 static int
1446 ath_tx_should_swq_frame(struct ath_softc *sc, struct ath_node *an,
1447     struct mbuf *m0, int *queue_to_head)
1448 {
1449         struct ieee80211_node *ni = &an->an_node;
1450         struct ieee80211_frame *wh;
1451         uint8_t type, subtype;
1452
1453         wh = mtod(m0, struct ieee80211_frame *);
1454         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1455         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1456
1457         (*queue_to_head) = 0;
1458
1459         /* If it's not in powersave - direct-dispatch BAR */
1460         if ((ATH_NODE(ni)->an_is_powersave == 0)
1461             && type == IEEE80211_FC0_TYPE_CTL &&
1462             subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1463                 DPRINTF(sc, ATH_DEBUG_SW_TX,
1464                     "%s: BAR: TX'ing direct\n", __func__);
1465                 return (0);
1466         } else if ((ATH_NODE(ni)->an_is_powersave == 1)
1467             && type == IEEE80211_FC0_TYPE_CTL &&
1468             subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1469                 /* BAR TX whilst asleep; queue */
1470                 DPRINTF(sc, ATH_DEBUG_SW_TX,
1471                     "%s: swq: TX'ing\n", __func__);
1472                 (*queue_to_head) = 1;
1473                 return (1);
1474         } else if ((ATH_NODE(ni)->an_is_powersave == 1)
1475             && (type == IEEE80211_FC0_TYPE_MGT ||
1476                 type == IEEE80211_FC0_TYPE_CTL)) {
1477                 /*
1478                  * Other control/mgmt frame; bypass software queuing
1479                  * for now!
1480                  */
1481                 DPRINTF(sc, ATH_DEBUG_XMIT, 
1482                     "%s: %6D: Node is asleep; sending mgmt "
1483                     "(type=%d, subtype=%d)\n",
1484                     __func__, ni->ni_macaddr, ":", type, subtype);
1485                 return (0);
1486         } else {
1487                 return (1);
1488         }
1489 }
1490
1491
1492 /*
1493  * Transmit the given frame to the hardware.
1494  *
1495  * The frame must already be setup; rate control must already have
1496  * been done.
1497  *
1498  * XXX since the TXQ lock is being held here (and I dislike holding
1499  * it for this long when not doing software aggregation), later on
1500  * break this function into "setup_normal" and "xmit_normal". The
1501  * lock only needs to be held for the ath_tx_handoff call.
1502  *
1503  * XXX we don't update the leak count here - if we're doing
1504  * direct frame dispatch, we need to be able to do it without
1505  * decrementing the leak count (eg multicast queue frames.)
1506  */
1507 static void
1508 ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1509     struct ath_buf *bf)
1510 {
1511         struct ath_node *an = ATH_NODE(bf->bf_node);
1512         struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1513
1514         ATH_TX_LOCK_ASSERT(sc);
1515
1516         /*
1517          * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
1518          * set a completion handler however it doesn't (yet) properly
1519          * handle the strict ordering requirements needed for normal,
1520          * non-aggregate session frames.
1521          *
1522          * Once this is implemented, only set CLRDMASK like this for
1523          * frames that must go out - eg management/raw frames.
1524          */
1525         bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1526
1527         /* Setup the descriptor before handoff */
1528         ath_tx_do_ratelookup(sc, bf);
1529         ath_tx_calc_duration(sc, bf);
1530         ath_tx_calc_protection(sc, bf);
1531         ath_tx_set_rtscts(sc, bf);
1532         ath_tx_rate_fill_rcflags(sc, bf);
1533         ath_tx_setds(sc, bf);
1534
1535         /* Track per-TID hardware queue depth correctly */
1536         tid->hwq_depth++;
1537
1538         /* Assign the completion handler */
1539         bf->bf_comp = ath_tx_normal_comp;
1540
1541         /* Hand off to hardware */
1542         ath_tx_handoff(sc, txq, bf);
1543 }
1544
1545 /*
1546  * Do the basic frame setup stuff that's required before the frame
1547  * is added to a software queue.
1548  *
1549  * All frames get mostly the same treatment and it's done once.
1550  * Retransmits fiddle with things like the rate control setup,
1551  * setting the retransmit bit in the packet; doing relevant DMA/bus
1552  * syncing and relinking it (back) into the hardware TX queue.
1553  *
1554  * Note that this may cause the mbuf to be reallocated, so
1555  * m0 may not be valid.
1556  */
1557 static int
1558 ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1559     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1560 {
1561         struct ieee80211vap *vap = ni->ni_vap;
1562         struct ath_hal *ah = sc->sc_ah;
1563         struct ifnet *ifp = sc->sc_ifp;
1564         struct ieee80211com *ic = ifp->if_l2com;
1565         const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1566         int error, iswep, ismcast, isfrag, ismrr;
1567         int keyix, hdrlen, pktlen, try0 = 0;
1568         u_int8_t rix = 0, txrate = 0;
1569         struct ath_desc *ds;
1570         struct ieee80211_frame *wh;
1571         u_int subtype, flags;
1572         HAL_PKT_TYPE atype;
1573         const HAL_RATE_TABLE *rt;
1574         HAL_BOOL shortPreamble;
1575         struct ath_node *an;
1576         u_int pri;
1577
1578         /*
1579          * To ensure that both sequence numbers and the CCMP PN handling
1580          * is "correct", make sure that the relevant TID queue is locked.
1581          * Otherwise the CCMP PN and seqno may appear out of order, causing
1582          * re-ordered frames to have out of order CCMP PN's, resulting
1583          * in many, many frame drops.
1584          */
1585         ATH_TX_LOCK_ASSERT(sc);
1586
1587         wh = mtod(m0, struct ieee80211_frame *);
1588         iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1589         ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1590         isfrag = m0->m_flags & M_FRAG;
1591         hdrlen = ieee80211_anyhdrsize(wh);
1592         /*
1593          * Packet length must not include any
1594          * pad bytes; deduct them here.
1595          */
1596         pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1597
1598         /* Handle encryption twiddling if needed */
1599         if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1600             &pktlen, &keyix)) {
1601                 ath_freetx(m0);
1602                 return EIO;
1603         }
1604
1605         /* packet header may have moved, reset our local pointer */
1606         wh = mtod(m0, struct ieee80211_frame *);
1607
1608         pktlen += IEEE80211_CRC_LEN;
1609
1610         /*
1611          * Load the DMA map so any coalescing is done.  This
1612          * also calculates the number of descriptors we need.
1613          */
1614         error = ath_tx_dmasetup(sc, bf, m0);
1615         if (error != 0)
1616                 return error;
1617         bf->bf_node = ni;                       /* NB: held reference */
1618         m0 = bf->bf_m;                          /* NB: may have changed */
1619         wh = mtod(m0, struct ieee80211_frame *);
1620
1621         /* setup descriptors */
1622         ds = bf->bf_desc;
1623         rt = sc->sc_currates;
1624         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1625
1626         /*
1627          * NB: the 802.11 layer marks whether or not we should
1628          * use short preamble based on the current mode and
1629          * negotiated parameters.
1630          */
1631         if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1632             (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1633                 shortPreamble = AH_TRUE;
1634                 sc->sc_stats.ast_tx_shortpre++;
1635         } else {
1636                 shortPreamble = AH_FALSE;
1637         }
1638
1639         an = ATH_NODE(ni);
1640         //flags = HAL_TXDESC_CLRDMASK;          /* XXX needed for crypto errs */
1641         flags = 0;
1642         ismrr = 0;                              /* default no multi-rate retry*/
1643         pri = M_WME_GETAC(m0);                  /* honor classification */
1644         /* XXX use txparams instead of fixed values */
1645         /*
1646          * Calculate Atheros packet type from IEEE80211 packet header,
1647          * setup for rate calculations, and select h/w transmit queue.
1648          */
1649         switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1650         case IEEE80211_FC0_TYPE_MGT:
1651                 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1652                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1653                         atype = HAL_PKT_TYPE_BEACON;
1654                 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1655                         atype = HAL_PKT_TYPE_PROBE_RESP;
1656                 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1657                         atype = HAL_PKT_TYPE_ATIM;
1658                 else
1659                         atype = HAL_PKT_TYPE_NORMAL;    /* XXX */
1660                 rix = an->an_mgmtrix;
1661                 txrate = rt->info[rix].rateCode;
1662                 if (shortPreamble)
1663                         txrate |= rt->info[rix].shortPreamble;
1664                 try0 = ATH_TXMGTTRY;
1665                 flags |= HAL_TXDESC_INTREQ;     /* force interrupt */
1666                 break;
1667         case IEEE80211_FC0_TYPE_CTL:
1668                 atype = HAL_PKT_TYPE_PSPOLL;    /* stop setting of duration */
1669                 rix = an->an_mgmtrix;
1670                 txrate = rt->info[rix].rateCode;
1671                 if (shortPreamble)
1672                         txrate |= rt->info[rix].shortPreamble;
1673                 try0 = ATH_TXMGTTRY;
1674                 flags |= HAL_TXDESC_INTREQ;     /* force interrupt */
1675                 break;
1676         case IEEE80211_FC0_TYPE_DATA:
1677                 atype = HAL_PKT_TYPE_NORMAL;            /* default */
1678                 /*
1679                  * Data frames: multicast frames go out at a fixed rate,
1680                  * EAPOL frames use the mgmt frame rate; otherwise consult
1681                  * the rate control module for the rate to use.
1682                  */
1683                 if (ismcast) {
1684                         rix = an->an_mcastrix;
1685                         txrate = rt->info[rix].rateCode;
1686                         if (shortPreamble)
1687                                 txrate |= rt->info[rix].shortPreamble;
1688                         try0 = 1;
1689                 } else if (m0->m_flags & M_EAPOL) {
1690                         /* XXX? maybe always use long preamble? */
1691                         rix = an->an_mgmtrix;
1692                         txrate = rt->info[rix].rateCode;
1693                         if (shortPreamble)
1694                                 txrate |= rt->info[rix].shortPreamble;
1695                         try0 = ATH_TXMAXTRY;    /* XXX?too many? */
1696                 } else {
1697                         /*
1698                          * Do rate lookup on each TX, rather than using
1699                          * the hard-coded TX information decided here.
1700                          */
1701                         ismrr = 1;
1702                         bf->bf_state.bfs_doratelookup = 1;
1703                 }
1704                 if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1705                         flags |= HAL_TXDESC_NOACK;
1706                 break;
1707         default:
1708                 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1709                         wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1710                 /* XXX statistic */
1711                 /* XXX free tx dmamap */
1712                 ath_freetx(m0);
1713                 return EIO;
1714         }
1715
1716         /*
1717          * There are two known scenarios where the frame AC doesn't match
1718          * what the destination TXQ is.
1719          *
1720          * + non-QoS frames (eg management?) that the net80211 stack has
1721          *   assigned a higher AC to, but since it's a non-QoS TID, it's
1722          *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1723          *   It's quite possible that management frames should just be
1724          *   direct dispatched to hardware rather than go via the software
1725          *   queue; that should be investigated in the future.  There are
1726          *   some specific scenarios where this doesn't make sense, mostly
1727          *   surrounding ADDBA request/response - hence why that is special
1728          *   cased.
1729          *
1730          * + Multicast frames going into the VAP mcast queue.  That shows up
1731          *   as "TXQ 11".
1732          *
1733          * This driver should eventually support separate TID and TXQ locking,
1734          * allowing for arbitrary AC frames to appear on arbitrary software
1735          * queues, being queued to the "correct" hardware queue when needed.
1736          */
1737 #if 0
1738         if (txq != sc->sc_ac2q[pri]) {
1739                 DPRINTF(sc, ATH_DEBUG_XMIT, 
1740                     "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
1741                     __func__,
1742                     txq,
1743                     txq->axq_qnum,
1744                     pri,
1745                     sc->sc_ac2q[pri],
1746                     sc->sc_ac2q[pri]->axq_qnum);
1747         }
1748 #endif
1749
1750         /*
1751          * Calculate miscellaneous flags.
1752          */
1753         if (ismcast) {
1754                 flags |= HAL_TXDESC_NOACK;      /* no ack on broad/multicast */
1755         } else if (pktlen > vap->iv_rtsthreshold &&
1756             (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1757                 flags |= HAL_TXDESC_RTSENA;     /* RTS based on frame length */
1758                 sc->sc_stats.ast_tx_rts++;
1759         }
1760         if (flags & HAL_TXDESC_NOACK)           /* NB: avoid double counting */
1761                 sc->sc_stats.ast_tx_noack++;
1762 #ifdef IEEE80211_SUPPORT_TDMA
1763         if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1764                 DPRINTF(sc, ATH_DEBUG_TDMA,
1765                     "%s: discard frame, ACK required w/ TDMA\n", __func__);
1766                 sc->sc_stats.ast_tdma_ack++;
1767                 /* XXX free tx dmamap */
1768                 ath_freetx(m0);
1769                 return EIO;
1770         }
1771 #endif
1772
1773         /*
1774          * Determine if a tx interrupt should be generated for
1775          * this descriptor.  We take a tx interrupt to reap
1776          * descriptors when the h/w hits an EOL condition or
1777          * when the descriptor is specifically marked to generate
1778          * an interrupt.  We periodically mark descriptors in this
1779          * way to insure timely replenishing of the supply needed
1780          * for sending frames.  Defering interrupts reduces system
1781          * load and potentially allows more concurrent work to be
1782          * done but if done to aggressively can cause senders to
1783          * backup.
1784          *
1785          * NB: use >= to deal with sc_txintrperiod changing
1786          *     dynamically through sysctl.
1787          */
1788         if (flags & HAL_TXDESC_INTREQ) {
1789                 txq->axq_intrcnt = 0;
1790         } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1791                 flags |= HAL_TXDESC_INTREQ;
1792                 txq->axq_intrcnt = 0;
1793         }
1794
1795         /* This point forward is actual TX bits */
1796
1797         /*
1798          * At this point we are committed to sending the frame
1799          * and we don't need to look at m_nextpkt; clear it in
1800          * case this frame is part of frag chain.
1801          */
1802         m0->m_nextpkt = NULL;
1803
1804         if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1805                 ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1806                     sc->sc_hwmap[rix].ieeerate, -1);
1807
1808         if (ieee80211_radiotap_active_vap(vap)) {
1809                 u_int64_t tsf = ath_hal_gettsf64(ah);
1810
1811                 sc->sc_tx_th.wt_tsf = htole64(tsf);
1812                 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1813                 if (iswep)
1814                         sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1815                 if (isfrag)
1816                         sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1817                 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1818                 sc->sc_tx_th.wt_txpower = ieee80211_get_node_txpower(ni);
1819                 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1820
1821                 ieee80211_radiotap_tx(vap, m0);
1822         }
1823
1824         /* Blank the legacy rate array */
1825         bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1826
1827         /*
1828          * ath_buf_set_rate needs at least one rate/try to setup
1829          * the rate scenario.
1830          */
1831         bf->bf_state.bfs_rc[0].rix = rix;
1832         bf->bf_state.bfs_rc[0].tries = try0;
1833         bf->bf_state.bfs_rc[0].ratecode = txrate;
1834
1835         /* Store the decided rate index values away */
1836         bf->bf_state.bfs_pktlen = pktlen;
1837         bf->bf_state.bfs_hdrlen = hdrlen;
1838         bf->bf_state.bfs_atype = atype;
1839         bf->bf_state.bfs_txpower = ieee80211_get_node_txpower(ni);
1840         bf->bf_state.bfs_txrate0 = txrate;
1841         bf->bf_state.bfs_try0 = try0;
1842         bf->bf_state.bfs_keyix = keyix;
1843         bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1844         bf->bf_state.bfs_txflags = flags;
1845         bf->bf_state.bfs_shpream = shortPreamble;
1846
1847         /* XXX this should be done in ath_tx_setrate() */
1848         bf->bf_state.bfs_ctsrate0 = 0;  /* ie, no hard-coded ctsrate */
1849         bf->bf_state.bfs_ctsrate = 0;   /* calculated later */
1850         bf->bf_state.bfs_ctsduration = 0;
1851         bf->bf_state.bfs_ismrr = ismrr;
1852
1853         return 0;
1854 }
1855
1856 /*
1857  * Queue a frame to the hardware or software queue.
1858  *
1859  * This can be called by the net80211 code.
1860  *
1861  * XXX what about locking? Or, push the seqno assign into the
1862  * XXX aggregate scheduler so its serialised?
1863  *
1864  * XXX When sending management frames via ath_raw_xmit(),
1865  *     should CLRDMASK be set unconditionally?
1866  */
1867 int
1868 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1869     struct ath_buf *bf, struct mbuf *m0)
1870 {
1871         struct ieee80211vap *vap = ni->ni_vap;
1872         struct ath_vap *avp = ATH_VAP(vap);
1873         int r = 0;
1874         u_int pri;
1875         int tid;
1876         struct ath_txq *txq;
1877         int ismcast;
1878         const struct ieee80211_frame *wh;
1879         int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1880         ieee80211_seq seqno;
1881         uint8_t type, subtype;
1882         int queue_to_head;
1883
1884         ATH_TX_LOCK_ASSERT(sc);
1885
1886         /*
1887          * Determine the target hardware queue.
1888          *
1889          * For multicast frames, the txq gets overridden appropriately
1890          * depending upon the state of PS.
1891          *
1892          * For any other frame, we do a TID/QoS lookup inside the frame
1893          * to see what the TID should be. If it's a non-QoS frame, the
1894          * AC and TID are overridden. The TID/TXQ code assumes the
1895          * TID is on a predictable hardware TXQ, so we don't support
1896          * having a node TID queued to multiple hardware TXQs.
1897          * This may change in the future but would require some locking
1898          * fudgery.
1899          */
1900         pri = ath_tx_getac(sc, m0);
1901         tid = ath_tx_gettid(sc, m0);
1902
1903         txq = sc->sc_ac2q[pri];
1904         wh = mtod(m0, struct ieee80211_frame *);
1905         ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1906         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1907         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1908
1909         /*
1910          * Enforce how deep the multicast queue can grow.
1911          *
1912          * XXX duplicated in ath_raw_xmit().
1913          */
1914         if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1915                 if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
1916                     > sc->sc_txq_mcastq_maxdepth) {
1917                         sc->sc_stats.ast_tx_mcastq_overflow++;
1918                         m_freem(m0);
1919                         return (ENOBUFS);
1920                 }
1921         }
1922
1923         /*
1924          * Enforce how deep the unicast queue can grow.
1925          *
1926          * If the node is in power save then we don't want
1927          * the software queue to grow too deep, or a node may
1928          * end up consuming all of the ath_buf entries.
1929          *
1930          * For now, only do this for DATA frames.
1931          *
1932          * We will want to cap how many management/control
1933          * frames get punted to the software queue so it doesn't
1934          * fill up.  But the correct solution isn't yet obvious.
1935          * In any case, this check should at least let frames pass
1936          * that we are direct-dispatching.
1937          *
1938          * XXX TODO: duplicate this to the raw xmit path!
1939          */
1940         if (type == IEEE80211_FC0_TYPE_DATA &&
1941             ATH_NODE(ni)->an_is_powersave &&
1942             ATH_NODE(ni)->an_swq_depth >
1943              sc->sc_txq_node_psq_maxdepth) {
1944                 sc->sc_stats.ast_tx_node_psq_overflow++;
1945                 m_freem(m0);
1946                 return (ENOBUFS);
1947         }
1948
1949         /* A-MPDU TX */
1950         is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1951         is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1952         is_ampdu = is_ampdu_tx | is_ampdu_pending;
1953
1954         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1955             __func__, tid, pri, is_ampdu);
1956
1957         /* Set local packet state, used to queue packets to hardware */
1958         bf->bf_state.bfs_tid = tid;
1959         bf->bf_state.bfs_tx_queue = txq->axq_qnum;
1960         bf->bf_state.bfs_pri = pri;
1961
1962 #if 1
1963         /*
1964          * When servicing one or more stations in power-save mode
1965          * (or) if there is some mcast data waiting on the mcast
1966          * queue (to prevent out of order delivery) multicast frames
1967          * must be bufferd until after the beacon.
1968          *
1969          * TODO: we should lock the mcastq before we check the length.
1970          */
1971         if (sc->sc_cabq_enable && ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1972                 txq = &avp->av_mcastq;
1973                 /*
1974                  * Mark the frame as eventually belonging on the CAB
1975                  * queue, so the descriptor setup functions will
1976                  * correctly initialise the descriptor 'qcuId' field.
1977                  */
1978                 bf->bf_state.bfs_tx_queue = sc->sc_cabq->axq_qnum;
1979         }
1980 #endif
1981
1982         /* Do the generic frame setup */
1983         /* XXX should just bzero the bf_state? */
1984         bf->bf_state.bfs_dobaw = 0;
1985
1986         /* A-MPDU TX? Manually set sequence number */
1987         /*
1988          * Don't do it whilst pending; the net80211 layer still
1989          * assigns them.
1990          */
1991         if (is_ampdu_tx) {
1992                 /*
1993                  * Always call; this function will
1994                  * handle making sure that null data frames
1995                  * don't get a sequence number from the current
1996                  * TID and thus mess with the BAW.
1997                  */
1998                 seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
1999
2000                 /*
2001                  * Don't add QoS NULL frames to the BAW.
2002                  */
2003                 if (IEEE80211_QOS_HAS_SEQ(wh) &&
2004                     subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2005                         bf->bf_state.bfs_dobaw = 1;
2006                 }
2007         }
2008
2009         /*
2010          * If needed, the sequence number has been assigned.
2011          * Squirrel it away somewhere easy to get to.
2012          */
2013         bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
2014
2015         /* Is ampdu pending? fetch the seqno and print it out */
2016         if (is_ampdu_pending)
2017                 DPRINTF(sc, ATH_DEBUG_SW_TX,
2018                     "%s: tid %d: ampdu pending, seqno %d\n",
2019                     __func__, tid, M_SEQNO_GET(m0));
2020
2021         /* This also sets up the DMA map */
2022         r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
2023
2024         if (r != 0)
2025                 goto done;
2026
2027         /* At this point m0 could have changed! */
2028         m0 = bf->bf_m;
2029
2030 #if 1
2031         /*
2032          * If it's a multicast frame, do a direct-dispatch to the
2033          * destination hardware queue. Don't bother software
2034          * queuing it.
2035          */
2036         /*
2037          * If it's a BAR frame, do a direct dispatch to the
2038          * destination hardware queue. Don't bother software
2039          * queuing it, as the TID will now be paused.
2040          * Sending a BAR frame can occur from the net80211 txa timer
2041          * (ie, retries) or from the ath txtask (completion call.)
2042          * It queues directly to hardware because the TID is paused
2043          * at this point (and won't be unpaused until the BAR has
2044          * either been TXed successfully or max retries has been
2045          * reached.)
2046          */
2047         /*
2048          * Until things are better debugged - if this node is asleep
2049          * and we're sending it a non-BAR frame, direct dispatch it.
2050          * Why? Because we need to figure out what's actually being
2051          * sent - eg, during reassociation/reauthentication after
2052          * the node (last) disappeared whilst asleep, the driver should
2053          * have unpaused/unsleep'ed the node.  So until that is
2054          * sorted out, use this workaround.
2055          */
2056         if (txq == &avp->av_mcastq) {
2057                 DPRINTF(sc, ATH_DEBUG_SW_TX,
2058                     "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
2059                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2060                 ath_tx_xmit_normal(sc, txq, bf);
2061         } else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
2062             &queue_to_head)) {
2063                 ath_tx_swq(sc, ni, txq, queue_to_head, bf);
2064         } else {
2065                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2066                 ath_tx_xmit_normal(sc, txq, bf);
2067         }
2068 #else
2069         /*
2070          * For now, since there's no software queue,
2071          * direct-dispatch to the hardware.
2072          */
2073         bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2074         /*
2075          * Update the current leak count if
2076          * we're leaking frames; and set the
2077          * MORE flag as appropriate.
2078          */
2079         ath_tx_leak_count_update(sc, tid, bf);
2080         ath_tx_xmit_normal(sc, txq, bf);
2081 #endif
2082 done:
2083         return 0;
2084 }
2085
2086 static int
2087 ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
2088         struct ath_buf *bf, struct mbuf *m0,
2089         const struct ieee80211_bpf_params *params)
2090 {
2091         struct ifnet *ifp = sc->sc_ifp;
2092         struct ieee80211com *ic = ifp->if_l2com;
2093         struct ath_hal *ah = sc->sc_ah;
2094         struct ieee80211vap *vap = ni->ni_vap;
2095         int error, ismcast, ismrr;
2096         int keyix, hdrlen, pktlen, try0, txantenna;
2097         u_int8_t rix, txrate;
2098         struct ieee80211_frame *wh;
2099         u_int flags;
2100         HAL_PKT_TYPE atype;
2101         const HAL_RATE_TABLE *rt;
2102         struct ath_desc *ds;
2103         u_int pri;
2104         int o_tid = -1;
2105         int do_override;
2106         uint8_t type, subtype;
2107         int queue_to_head;
2108
2109         ATH_TX_LOCK_ASSERT(sc);
2110
2111         wh = mtod(m0, struct ieee80211_frame *);
2112         ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2113         hdrlen = ieee80211_anyhdrsize(wh);
2114         /*
2115          * Packet length must not include any
2116          * pad bytes; deduct them here.
2117          */
2118         /* XXX honor IEEE80211_BPF_DATAPAD */
2119         pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
2120
2121         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2122         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2123
2124         ATH_KTR(sc, ATH_KTR_TX, 2,
2125              "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
2126
2127         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
2128             __func__, ismcast);
2129
2130         pri = params->ibp_pri & 3;
2131         /* Override pri if the frame isn't a QoS one */
2132         if (! IEEE80211_QOS_HAS_SEQ(wh))
2133                 pri = ath_tx_getac(sc, m0);
2134
2135         /* XXX If it's an ADDBA, override the correct queue */
2136         do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
2137
2138         /* Map ADDBA to the correct priority */
2139         if (do_override) {
2140 #if 0
2141                 DPRINTF(sc, ATH_DEBUG_XMIT, 
2142                     "%s: overriding tid %d pri %d -> %d\n",
2143                     __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
2144 #endif
2145                 pri = TID_TO_WME_AC(o_tid);
2146         }
2147
2148         /* Handle encryption twiddling if needed */
2149         if (! ath_tx_tag_crypto(sc, ni,
2150             m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
2151             &hdrlen, &pktlen, &keyix)) {
2152                 ath_freetx(m0);
2153                 return EIO;
2154         }
2155         /* packet header may have moved, reset our local pointer */
2156         wh = mtod(m0, struct ieee80211_frame *);
2157
2158         /* Do the generic frame setup */
2159         /* XXX should just bzero the bf_state? */
2160         bf->bf_state.bfs_dobaw = 0;
2161
2162         error = ath_tx_dmasetup(sc, bf, m0);
2163         if (error != 0)
2164                 return error;
2165         m0 = bf->bf_m;                          /* NB: may have changed */
2166         wh = mtod(m0, struct ieee80211_frame *);
2167         bf->bf_node = ni;                       /* NB: held reference */
2168
2169         /* Always enable CLRDMASK for raw frames for now.. */
2170         flags = HAL_TXDESC_CLRDMASK;            /* XXX needed for crypto errs */
2171         flags |= HAL_TXDESC_INTREQ;             /* force interrupt */
2172         if (params->ibp_flags & IEEE80211_BPF_RTS)
2173                 flags |= HAL_TXDESC_RTSENA;
2174         else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2175                 /* XXX assume 11g/11n protection? */
2176                 bf->bf_state.bfs_doprot = 1;
2177                 flags |= HAL_TXDESC_CTSENA;
2178         }
2179         /* XXX leave ismcast to injector? */
2180         if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2181                 flags |= HAL_TXDESC_NOACK;
2182
2183         rt = sc->sc_currates;
2184         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2185         rix = ath_tx_findrix(sc, params->ibp_rate0);
2186         txrate = rt->info[rix].rateCode;
2187         if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2188                 txrate |= rt->info[rix].shortPreamble;
2189         sc->sc_txrix = rix;
2190         try0 = params->ibp_try0;
2191         ismrr = (params->ibp_try1 != 0);
2192         txantenna = params->ibp_pri >> 2;
2193         if (txantenna == 0)                     /* XXX? */
2194                 txantenna = sc->sc_txantenna;
2195
2196         /*
2197          * Since ctsrate is fixed, store it away for later
2198          * use when the descriptor fields are being set.
2199          */
2200         if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2201                 bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
2202
2203         /*
2204          * NB: we mark all packets as type PSPOLL so the h/w won't
2205          * set the sequence number, duration, etc.
2206          */
2207         atype = HAL_PKT_TYPE_PSPOLL;
2208
2209         if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2210                 ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2211                     sc->sc_hwmap[rix].ieeerate, -1);
2212
2213         if (ieee80211_radiotap_active_vap(vap)) {
2214                 u_int64_t tsf = ath_hal_gettsf64(ah);
2215
2216                 sc->sc_tx_th.wt_tsf = htole64(tsf);
2217                 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
2218                 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2219                         sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2220                 if (m0->m_flags & M_FRAG)
2221                         sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2222                 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
2223                 sc->sc_tx_th.wt_txpower = MIN(params->ibp_power,
2224                     ieee80211_get_node_txpower(ni));
2225                 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2226
2227                 ieee80211_radiotap_tx(vap, m0);
2228         }
2229
2230         /*
2231          * Formulate first tx descriptor with tx controls.
2232          */
2233         ds = bf->bf_desc;
2234         /* XXX check return value? */
2235
2236         /* Store the decided rate index values away */
2237         bf->bf_state.bfs_pktlen = pktlen;
2238         bf->bf_state.bfs_hdrlen = hdrlen;
2239         bf->bf_state.bfs_atype = atype;
2240         bf->bf_state.bfs_txpower = MIN(params->ibp_power,
2241             ieee80211_get_node_txpower(ni));
2242         bf->bf_state.bfs_txrate0 = txrate;
2243         bf->bf_state.bfs_try0 = try0;
2244         bf->bf_state.bfs_keyix = keyix;
2245         bf->bf_state.bfs_txantenna = txantenna;
2246         bf->bf_state.bfs_txflags = flags;
2247         bf->bf_state.bfs_shpream =
2248             !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2249
2250         /* Set local packet state, used to queue packets to hardware */
2251         bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
2252         bf->bf_state.bfs_tx_queue = sc->sc_ac2q[pri]->axq_qnum;
2253         bf->bf_state.bfs_pri = pri;
2254
2255         /* XXX this should be done in ath_tx_setrate() */
2256         bf->bf_state.bfs_ctsrate = 0;
2257         bf->bf_state.bfs_ctsduration = 0;
2258         bf->bf_state.bfs_ismrr = ismrr;
2259
2260         /* Blank the legacy rate array */
2261         bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2262
2263         bf->bf_state.bfs_rc[0].rix =
2264             ath_tx_findrix(sc, params->ibp_rate0);
2265         bf->bf_state.bfs_rc[0].tries = try0;
2266         bf->bf_state.bfs_rc[0].ratecode = txrate;
2267
2268         if (ismrr) {
2269                 int rix;
2270
2271                 rix = ath_tx_findrix(sc, params->ibp_rate1);
2272                 bf->bf_state.bfs_rc[1].rix = rix;
2273                 bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2274
2275                 rix = ath_tx_findrix(sc, params->ibp_rate2);
2276                 bf->bf_state.bfs_rc[2].rix = rix;
2277                 bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2278
2279                 rix = ath_tx_findrix(sc, params->ibp_rate3);
2280                 bf->bf_state.bfs_rc[3].rix = rix;
2281                 bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2282         }
2283         /*
2284          * All the required rate control decisions have been made;
2285          * fill in the rc flags.
2286          */
2287         ath_tx_rate_fill_rcflags(sc, bf);
2288
2289         /* NB: no buffered multicast in power save support */
2290
2291         /*
2292          * If we're overiding the ADDBA destination, dump directly
2293          * into the hardware queue, right after any pending
2294          * frames to that node are.
2295          */
2296         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2297             __func__, do_override);
2298
2299 #if 1
2300         /*
2301          * Put addba frames in the right place in the right TID/HWQ.
2302          */
2303         if (do_override) {
2304                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2305                 /*
2306                  * XXX if it's addba frames, should we be leaking
2307                  * them out via the frame leak method?
2308                  * XXX for now let's not risk it; but we may wish
2309                  * to investigate this later.
2310                  */
2311                 ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2312         } else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
2313             &queue_to_head)) {
2314                 /* Queue to software queue */
2315                 ath_tx_swq(sc, ni, sc->sc_ac2q[pri], queue_to_head, bf);
2316         } else {
2317                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2318                 ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2319         }
2320 #else
2321         /* Direct-dispatch to the hardware */
2322         bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2323         /*
2324          * Update the current leak count if
2325          * we're leaking frames; and set the
2326          * MORE flag as appropriate.
2327          */
2328         ath_tx_leak_count_update(sc, tid, bf);
2329         ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2330 #endif
2331         return 0;
2332 }
2333
2334 /*
2335  * Send a raw frame.
2336  *
2337  * This can be called by net80211.
2338  */
2339 int
2340 ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2341         const struct ieee80211_bpf_params *params)
2342 {
2343         struct ieee80211com *ic = ni->ni_ic;
2344         struct ifnet *ifp = ic->ic_ifp;
2345         struct ath_softc *sc = ifp->if_softc;
2346         struct ath_buf *bf;
2347         struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
2348         int error = 0;
2349
2350         ATH_PCU_LOCK(sc);
2351         if (sc->sc_inreset_cnt > 0) {
2352                 DPRINTF(sc, ATH_DEBUG_XMIT, 
2353                     "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2354                 error = EIO;
2355                 ATH_PCU_UNLOCK(sc);
2356                 goto bad0;
2357         }
2358         sc->sc_txstart_cnt++;
2359         ATH_PCU_UNLOCK(sc);
2360
2361         ATH_TX_LOCK(sc);
2362
2363         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2364                 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2365                     (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2366                         "!running" : "invalid");
2367                 m_freem(m);
2368                 error = ENETDOWN;
2369                 goto bad;
2370         }
2371
2372         /*
2373          * Enforce how deep the multicast queue can grow.
2374          *
2375          * XXX duplicated in ath_tx_start().
2376          */
2377         if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2378                 if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
2379                     > sc->sc_txq_mcastq_maxdepth) {
2380                         sc->sc_stats.ast_tx_mcastq_overflow++;
2381                         error = ENOBUFS;
2382                 }
2383
2384                 if (error != 0) {
2385                         m_freem(m);
2386                         goto bad;
2387                 }
2388         }
2389
2390         /*
2391          * Grab a TX buffer and associated resources.
2392          */
2393         bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2394         if (bf == NULL) {
2395                 sc->sc_stats.ast_tx_nobuf++;
2396                 m_freem(m);
2397                 error = ENOBUFS;
2398                 goto bad;
2399         }
2400         ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
2401             m, params,  bf);
2402
2403         if (params == NULL) {
2404                 /*
2405                  * Legacy path; interpret frame contents to decide
2406                  * precisely how to send the frame.
2407                  */
2408                 if (ath_tx_start(sc, ni, bf, m)) {
2409                         error = EIO;            /* XXX */
2410                         goto bad2;
2411                 }
2412         } else {
2413                 /*
2414                  * Caller supplied explicit parameters to use in
2415                  * sending the frame.
2416                  */
2417                 if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2418                         error = EIO;            /* XXX */
2419                         goto bad2;
2420                 }
2421         }
2422         sc->sc_wd_timer = 5;
2423         ifp->if_opackets++;
2424         sc->sc_stats.ast_tx_raw++;
2425
2426         /*
2427          * Update the TIM - if there's anything queued to the
2428          * software queue and power save is enabled, we should
2429          * set the TIM.
2430          */
2431         ath_tx_update_tim(sc, ni, 1);
2432
2433         ATH_TX_UNLOCK(sc);
2434
2435         ATH_PCU_LOCK(sc);
2436         sc->sc_txstart_cnt--;
2437         ATH_PCU_UNLOCK(sc);
2438
2439         return 0;
2440 bad2:
2441         ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
2442             "bf=%p",
2443             m,
2444             params,
2445             bf);
2446         ATH_TXBUF_LOCK(sc);
2447         ath_returnbuf_head(sc, bf);
2448         ATH_TXBUF_UNLOCK(sc);
2449 bad:
2450
2451         ATH_TX_UNLOCK(sc);
2452
2453         ATH_PCU_LOCK(sc);
2454         sc->sc_txstart_cnt--;
2455         ATH_PCU_UNLOCK(sc);
2456 bad0:
2457         ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
2458             m, params);
2459         ifp->if_oerrors++;
2460         sc->sc_stats.ast_tx_raw_fail++;
2461         ieee80211_free_node(ni);
2462
2463         return error;
2464 }
2465
2466 /* Some helper functions */
2467
2468 /*
2469  * ADDBA (and potentially others) need to be placed in the same
2470  * hardware queue as the TID/node it's relating to. This is so
2471  * it goes out after any pending non-aggregate frames to the
2472  * same node/TID.
2473  *
2474  * If this isn't done, the ADDBA can go out before the frames
2475  * queued in hardware. Even though these frames have a sequence
2476  * number -earlier- than the ADDBA can be transmitted (but
2477  * no frames whose sequence numbers are after the ADDBA should
2478  * be!) they'll arrive after the ADDBA - and the receiving end
2479  * will simply drop them as being out of the BAW.
2480  *
2481  * The frames can't be appended to the TID software queue - it'll
2482  * never be sent out. So these frames have to be directly
2483  * dispatched to the hardware, rather than queued in software.
2484  * So if this function returns true, the TXQ has to be
2485  * overridden and it has to be directly dispatched.
2486  *
2487  * It's a dirty hack, but someone's gotta do it.
2488  */
2489
2490 /*
2491  * XXX doesn't belong here!
2492  */
2493 static int
2494 ieee80211_is_action(struct ieee80211_frame *wh)
2495 {
2496         /* Type: Management frame? */
2497         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2498             IEEE80211_FC0_TYPE_MGT)
2499                 return 0;
2500
2501         /* Subtype: Action frame? */
2502         if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2503             IEEE80211_FC0_SUBTYPE_ACTION)
2504                 return 0;
2505
2506         return 1;
2507 }
2508
2509 #define MS(_v, _f)      (((_v) & _f) >> _f##_S)
2510 /*
2511  * Return an alternate TID for ADDBA request frames.
2512  *
2513  * Yes, this likely should be done in the net80211 layer.
2514  */
2515 static int
2516 ath_tx_action_frame_override_queue(struct ath_softc *sc,
2517     struct ieee80211_node *ni,
2518     struct mbuf *m0, int *tid)
2519 {
2520         struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2521         struct ieee80211_action_ba_addbarequest *ia;
2522         uint8_t *frm;
2523         uint16_t baparamset;
2524
2525         /* Not action frame? Bail */
2526         if (! ieee80211_is_action(wh))
2527                 return 0;
2528
2529         /* XXX Not needed for frames we send? */
2530 #if 0
2531         /* Correct length? */
2532         if (! ieee80211_parse_action(ni, m))
2533                 return 0;
2534 #endif
2535
2536         /* Extract out action frame */
2537         frm = (u_int8_t *)&wh[1];
2538         ia = (struct ieee80211_action_ba_addbarequest *) frm;
2539
2540         /* Not ADDBA? Bail */
2541         if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2542                 return 0;
2543         if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2544                 return 0;
2545
2546         /* Extract TID, return it */
2547         baparamset = le16toh(ia->rq_baparamset);
2548         *tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2549
2550         return 1;
2551 }
2552 #undef  MS
2553
2554 /* Per-node software queue operations */
2555
2556 /*
2557  * Add the current packet to the given BAW.
2558  * It is assumed that the current packet
2559  *
2560  * + fits inside the BAW;
2561  * + already has had a sequence number allocated.
2562  *
2563  * Since the BAW status may be modified by both the ath task and
2564  * the net80211/ifnet contexts, the TID must be locked.
2565  */
2566 void
2567 ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2568     struct ath_tid *tid, struct ath_buf *bf)
2569 {
2570         int index, cindex;
2571         struct ieee80211_tx_ampdu *tap;
2572
2573         ATH_TX_LOCK_ASSERT(sc);
2574
2575         if (bf->bf_state.bfs_isretried)
2576                 return;
2577
2578         tap = ath_tx_get_tx_tid(an, tid->tid);
2579
2580         if (! bf->bf_state.bfs_dobaw) {
2581                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2582                     "%s: dobaw=0, seqno=%d, window %d:%d\n",
2583                     __func__, SEQNO(bf->bf_state.bfs_seqno),
2584                     tap->txa_start, tap->txa_wnd);
2585         }
2586
2587         if (bf->bf_state.bfs_addedbaw)
2588                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2589                     "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2590                     "baw head=%d tail=%d\n",
2591                     __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2592                     tap->txa_start, tap->txa_wnd, tid->baw_head,
2593                     tid->baw_tail);
2594
2595         /*
2596          * Verify that the given sequence number is not outside of the
2597          * BAW.  Complain loudly if that's the case.
2598          */
2599         if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2600             SEQNO(bf->bf_state.bfs_seqno))) {
2601                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2602                     "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
2603                     "baw head=%d tail=%d\n",
2604                     __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2605                     tap->txa_start, tap->txa_wnd, tid->baw_head,
2606                     tid->baw_tail);
2607         }
2608
2609         /*
2610          * ni->ni_txseqs[] is the currently allocated seqno.
2611          * the txa state contains the current baw start.
2612          */
2613         index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2614         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2615         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2616             "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2617             "baw head=%d tail=%d\n",
2618             __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2619             tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2620             tid->baw_tail);
2621
2622
2623 #if 0
2624         assert(tid->tx_buf[cindex] == NULL);
2625 #endif
2626         if (tid->tx_buf[cindex] != NULL) {
2627                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2628                     "%s: ba packet dup (index=%d, cindex=%d, "
2629                     "head=%d, tail=%d)\n",
2630                     __func__, index, cindex, tid->baw_head, tid->baw_tail);
2631                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2632                     "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2633                     __func__,
2634                     tid->tx_buf[cindex],
2635                     SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2636                     bf,
2637                     SEQNO(bf->bf_state.bfs_seqno)
2638                 );
2639         }
2640         tid->tx_buf[cindex] = bf;
2641
2642         if (index >= ((tid->baw_tail - tid->baw_head) &
2643             (ATH_TID_MAX_BUFS - 1))) {
2644                 tid->baw_tail = cindex;
2645                 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2646         }
2647 }
2648
2649 /*
2650  * Flip the BAW buffer entry over from the existing one to the new one.
2651  *
2652  * When software retransmitting a (sub-)frame, it is entirely possible that
2653  * the frame ath_buf is marked as BUSY and can't be immediately reused.
2654  * In that instance the buffer is cloned and the new buffer is used for
2655  * retransmit. We thus need to update the ath_buf slot in the BAW buf
2656  * tracking array to maintain consistency.
2657  */
2658 static void
2659 ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
2660     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
2661 {
2662         int index, cindex;
2663         struct ieee80211_tx_ampdu *tap;
2664         int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
2665
2666         ATH_TX_LOCK_ASSERT(sc);
2667
2668         tap = ath_tx_get_tx_tid(an, tid->tid);
2669         index  = ATH_BA_INDEX(tap->txa_start, seqno);
2670         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2671
2672         /*
2673          * Just warn for now; if it happens then we should find out
2674          * about it. It's highly likely the aggregation session will
2675          * soon hang.
2676          */
2677         if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
2678                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2679                     "%s: retransmitted buffer"
2680                     " has mismatching seqno's, BA session may hang.\n",
2681                     __func__);
2682                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2683                     "%s: old seqno=%d, new_seqno=%d\n", __func__,
2684                     old_bf->bf_state.bfs_seqno, new_bf->bf_state.bfs_seqno);
2685         }
2686
2687         if (tid->tx_buf[cindex] != old_bf) {
2688                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2689                     "%s: ath_buf pointer incorrect; "
2690                     " has m BA session may hang.\n", __func__);
2691                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2692                     "%s: old bf=%p, new bf=%p\n", __func__, old_bf, new_bf);
2693         }
2694
2695         tid->tx_buf[cindex] = new_bf;
2696 }
2697
2698 /*
2699  * seq_start - left edge of BAW
2700  * seq_next - current/next sequence number to allocate
2701  *
2702  * Since the BAW status may be modified by both the ath task and
2703  * the net80211/ifnet contexts, the TID must be locked.
2704  */
2705 static void
2706 ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2707     struct ath_tid *tid, const struct ath_buf *bf)
2708 {
2709         int index, cindex;
2710         struct ieee80211_tx_ampdu *tap;
2711         int seqno = SEQNO(bf->bf_state.bfs_seqno);
2712
2713         ATH_TX_LOCK_ASSERT(sc);
2714
2715         tap = ath_tx_get_tx_tid(an, tid->tid);
2716         index  = ATH_BA_INDEX(tap->txa_start, seqno);
2717         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2718
2719         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2720             "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2721             "baw head=%d, tail=%d\n",
2722             __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2723             cindex, tid->baw_head, tid->baw_tail);
2724
2725         /*
2726          * If this occurs then we have a big problem - something else
2727          * has slid tap->txa_start along without updating the BAW
2728          * tracking start/end pointers. Thus the TX BAW state is now
2729          * completely busted.
2730          *
2731          * But for now, since I haven't yet fixed TDMA and buffer cloning,
2732          * it's quite possible that a cloned buffer is making its way
2733          * here and causing it to fire off. Disable TDMA for now.
2734          */
2735         if (tid->tx_buf[cindex] != bf) {
2736                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2737                     "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2738                     __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
2739                     tid->tx_buf[cindex],
2740                     (tid->tx_buf[cindex] != NULL) ?
2741                       SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno) : -1);
2742         }
2743
2744         tid->tx_buf[cindex] = NULL;
2745
2746         while (tid->baw_head != tid->baw_tail &&
2747             !tid->tx_buf[tid->baw_head]) {
2748                 INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2749                 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2750         }
2751         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2752             "%s: baw is now %d:%d, baw head=%d\n",
2753             __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2754 }
2755
2756 static void
2757 ath_tx_leak_count_update(struct ath_softc *sc, struct ath_tid *tid,
2758     struct ath_buf *bf)
2759 {
2760         struct ieee80211_frame *wh;
2761
2762         ATH_TX_LOCK_ASSERT(sc);
2763
2764         if (tid->an->an_leak_count > 0) {
2765                 wh = mtod(bf->bf_m, struct ieee80211_frame *);
2766
2767                 /*
2768                  * Update MORE based on the software/net80211 queue states.
2769                  */
2770                 if ((tid->an->an_stack_psq > 0)
2771                     || (tid->an->an_swq_depth > 0))
2772                         wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
2773                 else
2774                         wh->i_fc[1] &= ~IEEE80211_FC1_MORE_DATA;
2775
2776                 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
2777                     "%s: %6D: leak count = %d, psq=%d, swq=%d, MORE=%d\n",
2778                     __func__,
2779                     tid->an->an_node.ni_macaddr,
2780                     ":",
2781                     tid->an->an_leak_count,
2782                     tid->an->an_stack_psq,
2783                     tid->an->an_swq_depth,
2784                     !! (wh->i_fc[1] & IEEE80211_FC1_MORE_DATA));
2785
2786                 /*
2787                  * Re-sync the underlying buffer.
2788                  */
2789                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
2790                     BUS_DMASYNC_PREWRITE);
2791
2792                 tid->an->an_leak_count --;
2793         }
2794 }
2795
2796 static int
2797 ath_tx_tid_can_tx_or_sched(struct ath_softc *sc, struct ath_tid *tid)
2798 {
2799
2800         ATH_TX_LOCK_ASSERT(sc);
2801
2802         if (tid->an->an_leak_count > 0) {
2803                 return (1);
2804         }
2805         if (tid->paused)
2806                 return (0);
2807         return (1);
2808 }
2809
2810 /*
2811  * Mark the current node/TID as ready to TX.
2812  *
2813  * This is done to make it easy for the software scheduler to
2814  * find which nodes have data to send.
2815  *
2816  * The TXQ lock must be held.
2817  */
2818 void
2819 ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2820 {
2821         struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2822
2823         ATH_TX_LOCK_ASSERT(sc);
2824
2825         /*
2826          * If we are leaking out a frame to this destination
2827          * for PS-POLL, ensure that we allow scheduling to
2828          * occur.
2829          */
2830         if (! ath_tx_tid_can_tx_or_sched(sc, tid))
2831                 return;         /* paused, can't schedule yet */
2832
2833         if (tid->sched)
2834                 return;         /* already scheduled */
2835
2836         tid->sched = 1;
2837
2838 #if 0
2839         /*
2840          * If this is a sleeping node we're leaking to, given
2841          * it a higher priority.  This is so bad for QoS it hurts.
2842          */
2843         if (tid->an->an_leak_count) {
2844                 TAILQ_INSERT_HEAD(&txq->axq_tidq, tid, axq_qelem);
2845         } else {
2846                 TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2847         }
2848 #endif
2849
2850         /*
2851          * We can't do the above - it'll confuse the TXQ software
2852          * scheduler which will keep checking the _head_ TID
2853          * in the list to see if it has traffic.  If we queue
2854          * a TID to the head of the list and it doesn't transmit,
2855          * we'll check it again.
2856          *
2857          * So, get the rest of this leaking frames support working
2858          * and reliable first and _then_ optimise it so they're
2859          * pushed out in front of any other pending software
2860          * queued nodes.
2861          */
2862         TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2863 }
2864
2865 /*
2866  * Mark the current node as no longer needing to be polled for
2867  * TX packets.
2868  *
2869  * The TXQ lock must be held.
2870  */
2871 static void
2872 ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2873 {
2874         struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2875
2876         ATH_TX_LOCK_ASSERT(sc);
2877
2878         if (tid->sched == 0)
2879                 return;
2880
2881         tid->sched = 0;
2882         TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2883 }
2884
2885 /*
2886  * Assign a sequence number manually to the given frame.
2887  *
2888  * This should only be called for A-MPDU TX frames.
2889  */
2890 static ieee80211_seq
2891 ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2892     struct ath_buf *bf, struct mbuf *m0)
2893 {
2894         struct ieee80211_frame *wh;
2895         int tid, pri;
2896         ieee80211_seq seqno;
2897         uint8_t subtype;
2898
2899         /* TID lookup */
2900         wh = mtod(m0, struct ieee80211_frame *);
2901         pri = M_WME_GETAC(m0);                  /* honor classification */
2902         tid = WME_AC_TO_TID(pri);
2903         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2904             __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2905
2906         /* XXX Is it a control frame? Ignore */
2907
2908         /* Does the packet require a sequence number? */
2909         if (! IEEE80211_QOS_HAS_SEQ(wh))
2910                 return -1;
2911
2912         ATH_TX_LOCK_ASSERT(sc);
2913
2914         /*
2915          * Is it a QOS NULL Data frame? Give it a sequence number from
2916          * the default TID (IEEE80211_NONQOS_TID.)
2917          *
2918          * The RX path of everything I've looked at doesn't include the NULL
2919          * data frame sequence number in the aggregation state updates, so
2920          * assigning it a sequence number there will cause a BAW hole on the
2921          * RX side.
2922          */
2923         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2924         if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2925                 /* XXX no locking for this TID? This is a bit of a problem. */
2926                 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2927                 INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2928         } else {
2929                 /* Manually assign sequence number */
2930                 seqno = ni->ni_txseqs[tid];
2931                 INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2932         }
2933         *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2934         M_SEQNO_SET(m0, seqno);
2935
2936         /* Return so caller can do something with it if needed */
2937         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2938         return seqno;
2939 }
2940
2941 /*
2942  * Attempt to direct dispatch an aggregate frame to hardware.
2943  * If the frame is out of BAW, queue.
2944  * Otherwise, schedule it as a single frame.
2945  */
2946 static void
2947 ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
2948     struct ath_txq *txq, struct ath_buf *bf)
2949 {
2950         struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2951         struct ieee80211_tx_ampdu *tap;
2952
2953         ATH_TX_LOCK_ASSERT(sc);
2954
2955         tap = ath_tx_get_tx_tid(an, tid->tid);
2956
2957         /* paused? queue */
2958         if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
2959                 ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2960                 /* XXX don't sched - we're paused! */
2961                 return;
2962         }
2963
2964         /* outside baw? queue */
2965         if (bf->bf_state.bfs_dobaw &&
2966             (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2967             SEQNO(bf->bf_state.bfs_seqno)))) {
2968                 ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2969                 ath_tx_tid_sched(sc, tid);
2970                 return;
2971         }
2972
2973         /*
2974          * This is a temporary check and should be removed once
2975          * all the relevant code paths have been fixed.
2976          *
2977          * During aggregate retries, it's possible that the head
2978          * frame will fail (which has the bfs_aggr and bfs_nframes
2979          * fields set for said aggregate) and will be retried as
2980          * a single frame.  In this instance, the values should
2981          * be reset or the completion code will get upset with you.
2982          */
2983         if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
2984                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
2985                     "%s: bfs_aggr=%d, bfs_nframes=%d\n", __func__,
2986                     bf->bf_state.bfs_aggr, bf->bf_state.bfs_nframes);
2987                 bf->bf_state.bfs_aggr = 0;
2988                 bf->bf_state.bfs_nframes = 1;
2989         }
2990
2991         /* Update CLRDMASK just before this frame is queued */
2992         ath_tx_update_clrdmask(sc, tid, bf);
2993
2994         /* Direct dispatch to hardware */
2995         ath_tx_do_ratelookup(sc, bf);
2996         ath_tx_calc_duration(sc, bf);
2997         ath_tx_calc_protection(sc, bf);
2998         ath_tx_set_rtscts(sc, bf);
2999         ath_tx_rate_fill_rcflags(sc, bf);
3000         ath_tx_setds(sc, bf);
3001
3002         /* Statistics */
3003         sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
3004
3005         /* Track per-TID hardware queue depth correctly */
3006         tid->hwq_depth++;
3007
3008         /* Add to BAW */
3009         if (bf->bf_state.bfs_dobaw) {
3010                 ath_tx_addto_baw(sc, an, tid, bf);
3011                 bf->bf_state.bfs_addedbaw = 1;
3012         }
3013
3014         /* Set completion handler, multi-frame aggregate or not */
3015         bf->bf_comp = ath_tx_aggr_comp;
3016
3017         /*
3018          * Update the current leak count if
3019          * we're leaking frames; and set the
3020          * MORE flag as appropriate.
3021          */
3022         ath_tx_leak_count_update(sc, tid, bf);
3023
3024         /* Hand off to hardware */
3025         ath_tx_handoff(sc, txq, bf);
3026 }
3027
3028 /*
3029  * Attempt to send the packet.
3030  * If the queue isn't busy, direct-dispatch.
3031  * If the queue is busy enough, queue the given packet on the
3032  *  relevant software queue.
3033  */
3034 void
3035 ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni,
3036     struct ath_txq *txq, int queue_to_head, struct ath_buf *bf)
3037 {
3038         struct ath_node *an = ATH_NODE(ni);
3039         struct ieee80211_frame *wh;
3040         struct ath_tid *atid;
3041         int pri, tid;
3042         struct mbuf *m0 = bf->bf_m;
3043
3044         ATH_TX_LOCK_ASSERT(sc);
3045
3046         /* Fetch the TID - non-QoS frames get assigned to TID 16 */
3047         wh = mtod(m0, struct ieee80211_frame *);
3048         pri = ath_tx_getac(sc, m0);
3049         tid = ath_tx_gettid(sc, m0);
3050         atid = &an->an_tid[tid];
3051
3052         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
3053             __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
3054
3055         /* Set local packet state, used to queue packets to hardware */
3056         /* XXX potentially duplicate info, re-check */
3057         bf->bf_state.bfs_tid = tid;
3058         bf->bf_state.bfs_tx_queue = txq->axq_qnum;
3059         bf->bf_state.bfs_pri = pri;
3060
3061         /*
3062          * If the hardware queue isn't busy, queue it directly.
3063          * If the hardware queue is busy, queue it.
3064          * If the TID is paused or the traffic it outside BAW, software
3065          * queue it.
3066          *
3067          * If the node is in power-save and we're leaking a frame,
3068          * leak a single frame.
3069          */
3070         if (! ath_tx_tid_can_tx_or_sched(sc, atid)) {
3071                 /* TID is paused, queue */
3072                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
3073                 /*
3074                  * If the caller requested that it be sent at a high
3075                  * priority, queue it at the head of the list.
3076                  */
3077                 if (queue_to_head)
3078                         ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3079                 else
3080                         ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3081         } else if (ath_tx_ampdu_pending(sc, an, tid)) {
3082                 /* AMPDU pending; queue */
3083                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
3084                 ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3085                 /* XXX sched? */
3086         } else if (ath_tx_ampdu_running(sc, an, tid)) {
3087                 /* AMPDU running, attempt direct dispatch if possible */
3088
3089                 /*
3090                  * Always queue the frame to the tail of the list.
3091                  */
3092                 ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3093
3094                 /*
3095                  * If the hardware queue isn't busy, direct dispatch
3096                  * the head frame in the list.  Don't schedule the
3097                  * TID - let it build some more frames first?
3098                  *
3099                  * When running A-MPDU, always just check the hardware
3100                  * queue depth against the aggregate frame limit.
3101                  * We don't want to burst a large number of single frames
3102                  * out to the hardware; we want to aggressively hold back.
3103                  *
3104                  * Otherwise, schedule the TID.
3105                  */
3106                 /* XXX TXQ locking */
3107                 if (txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit_aggr) {
3108                         bf = ATH_TID_FIRST(atid);
3109                         ATH_TID_REMOVE(atid, bf, bf_list);
3110
3111                         /*
3112                          * Ensure it's definitely treated as a non-AMPDU
3113                          * frame - this information may have been left
3114                          * over from a previous attempt.
3115                          */
3116                         bf->bf_state.bfs_aggr = 0;
3117                         bf->bf_state.bfs_nframes = 1;
3118
3119                         /* Queue to the hardware */
3120                         ath_tx_xmit_aggr(sc, an, txq, bf);
3121                         DPRINTF(sc, ATH_DEBUG_SW_TX,
3122                             "%s: xmit_aggr\n",
3123                             __func__);
3124                 } else {
3125                         DPRINTF(sc, ATH_DEBUG_SW_TX,
3126                             "%s: ampdu; swq'ing\n",
3127                             __func__);
3128
3129                         ath_tx_tid_sched(sc, atid);
3130                 }
3131         /*
3132          * If we're not doing A-MPDU, be prepared to direct dispatch
3133          * up to both limits if possible.  This particular corner
3134          * case may end up with packet starvation between aggregate
3135          * traffic and non-aggregate traffic: we wnat to ensure
3136          * that non-aggregate stations get a few frames queued to the
3137          * hardware before the aggregate station(s) get their chance.
3138          *
3139          * So if you only ever see a couple of frames direct dispatched
3140          * to the hardware from a non-AMPDU client, check both here
3141          * and in the software queue dispatcher to ensure that those
3142          * non-AMPDU stations get a fair chance to transmit.
3143          */
3144         /* XXX TXQ locking */
3145         } else if ((txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit_nonaggr) &&
3146                     (txq->axq_aggr_depth < sc->sc_hwq_limit_aggr)) {
3147                 /* AMPDU not running, attempt direct dispatch */
3148                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
3149                 /* See if clrdmask needs to be set */
3150                 ath_tx_update_clrdmask(sc, atid, bf);
3151
3152                 /*
3153                  * Update the current leak count if
3154                  * we're leaking frames; and set the
3155                  * MORE flag as appropriate.
3156                  */
3157                 ath_tx_leak_count_update(sc, atid, bf);
3158
3159                 /*
3160                  * Dispatch the frame.
3161                  */
3162                 ath_tx_xmit_normal(sc, txq, bf);
3163         } else {
3164                 /* Busy; queue */
3165                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
3166                 ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3167                 ath_tx_tid_sched(sc, atid);
3168         }
3169 }
3170
3171 /*
3172  * Only set the clrdmask bit if none of the nodes are currently
3173  * filtered.
3174  *
3175  * XXX TODO: go through all the callers and check to see
3176  * which are being called in the context of looping over all
3177  * TIDs (eg, if all tids are being paused, resumed, etc.)
3178  * That'll avoid O(n^2) complexity here.
3179  */
3180 static void
3181 ath_tx_set_clrdmask(struct ath_softc *sc, struct ath_node *an)
3182 {
3183         int i;
3184
3185         ATH_TX_LOCK_ASSERT(sc);
3186
3187         for (i = 0; i < IEEE80211_TID_SIZE; i++) {
3188                 if (an->an_tid[i].isfiltered == 1)
3189                         return;
3190         }
3191         an->clrdmask = 1;
3192 }
3193
3194 /*
3195  * Configure the per-TID node state.
3196  *
3197  * This likely belongs in if_ath_node.c but I can't think of anywhere
3198  * else to put it just yet.
3199  *
3200  * This sets up the SLISTs and the mutex as appropriate.
3201  */
3202 void
3203 ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
3204 {
3205         int i, j;
3206         struct ath_tid *atid;
3207
3208         for (i = 0; i < IEEE80211_TID_SIZE; i++) {
3209                 atid = &an->an_tid[i];
3210
3211                 /* XXX now with this bzer(), is the field 0'ing needed? */
3212                 bzero(atid, sizeof(*atid));
3213
3214                 TAILQ_INIT(&atid->tid_q);
3215                 TAILQ_INIT(&atid->filtq.tid_q);
3216                 atid->tid = i;
3217                 atid->an = an;
3218                 for (j = 0; j < ATH_TID_MAX_BUFS; j++)
3219                         atid->tx_buf[j] = NULL;
3220                 atid->baw_head = atid->baw_tail = 0;
3221                 atid->paused = 0;
3222                 atid->sched = 0;
3223                 atid->hwq_depth = 0;
3224                 atid->cleanup_inprogress = 0;
3225                 if (i == IEEE80211_NONQOS_TID)
3226                         atid->ac = ATH_NONQOS_TID_AC;
3227                 else
3228                         atid->ac = TID_TO_WME_AC(i);
3229         }
3230         an->clrdmask = 1;       /* Always start by setting this bit */
3231 }
3232
3233 /*
3234  * Pause the current TID. This stops packets from being transmitted
3235  * on it.
3236  *
3237  * Since this is also called from upper layers as well as the driver,
3238  * it will get the TID lock.
3239  */
3240 static void
3241 ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
3242 {
3243
3244         ATH_TX_LOCK_ASSERT(sc);
3245         tid->paused++;
3246         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
3247             __func__, tid->paused);
3248 }
3249
3250 /*
3251  * Unpause the current TID, and schedule it if needed.
3252  */
3253 static void
3254 ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
3255 {
3256         ATH_TX_LOCK_ASSERT(sc);
3257
3258         /*
3259          * There's some odd places where ath_tx_tid_resume() is called
3260          * when it shouldn't be; this works around that particular issue
3261          * until it's actually resolved.
3262          */
3263         if (tid->paused == 0) {
3264                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3265                     "%s: %6D: paused=0?\n", __func__,
3266                     tid->an->an_node.ni_macaddr, ":");
3267         } else {
3268                 tid->paused--;
3269         }
3270
3271         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
3272             __func__, tid->paused);
3273
3274         if (tid->paused)
3275                 return;
3276
3277         /*
3278          * Override the clrdmask configuration for the next frame
3279          * from this TID, just to get the ball rolling.
3280          */
3281         ath_tx_set_clrdmask(sc, tid->an);
3282
3283         if (tid->axq_depth == 0)
3284                 return;
3285
3286         /* XXX isfiltered shouldn't ever be 0 at this point */
3287         if (tid->isfiltered == 1) {
3288                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: filtered?!\n",
3289                     __func__);
3290                 return;
3291         }
3292
3293         ath_tx_tid_sched(sc, tid);
3294
3295         /*
3296          * Queue the software TX scheduler.
3297          */
3298         ath_tx_swq_kick(sc);
3299 }
3300
3301 /*
3302  * Add the given ath_buf to the TID filtered frame list.
3303  * This requires the TID be filtered.
3304  */
3305 static void
3306 ath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
3307     struct ath_buf *bf)
3308 {
3309
3310         ATH_TX_LOCK_ASSERT(sc);
3311
3312         if (!tid->isfiltered)
3313                 DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: not filtered?!\n",
3314                     __func__);
3315
3316         DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
3317
3318         /* Set the retry bit and bump the retry counter */
3319         ath_tx_set_retry(sc, bf);
3320         sc->sc_stats.ast_tx_swfiltered++;
3321
3322         ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
3323 }
3324
3325 /*
3326  * Handle a completed filtered frame from the given TID.
3327  * This just enables/pauses the filtered frame state if required
3328  * and appends the filtered frame to the filtered queue.
3329  */
3330 static void
3331 ath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3332     struct ath_buf *bf)
3333 {
3334
3335         ATH_TX_LOCK_ASSERT(sc);
3336
3337         if (! tid->isfiltered) {
3338                 DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
3339                     __func__);
3340                 tid->isfiltered = 1;
3341                 ath_tx_tid_pause(sc, tid);
3342         }
3343
3344         /* Add the frame to the filter queue */
3345         ath_tx_tid_filt_addbuf(sc, tid, bf);
3346 }
3347
3348 /*
3349  * Complete the filtered frame TX completion.
3350  *
3351  * If there are no more frames in the hardware queue, unpause/unfilter
3352  * the TID if applicable.  Otherwise we will wait for a node PS transition
3353  * to unfilter.
3354  */
3355 static void
3356 ath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3357 {
3358         struct ath_buf *bf;
3359
3360         ATH_TX_LOCK_ASSERT(sc);
3361
3362         if (tid->hwq_depth != 0)
3363                 return;
3364
3365         DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
3366             __func__);
3367         tid->isfiltered = 0;
3368         /* XXX ath_tx_tid_resume() also calls ath_tx_set_clrdmask()! */
3369         ath_tx_set_clrdmask(sc, tid->an);
3370
3371         /* XXX this is really quite inefficient */
3372         while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
3373                 ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3374                 ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3375         }
3376
3377         ath_tx_tid_resume(sc, tid);
3378 }
3379
3380 /*
3381  * Called when a single (aggregate or otherwise) frame is completed.
3382  *
3383  * Returns 1 if the buffer could be added to the filtered list
3384  * (cloned or otherwise), 0 if the buffer couldn't be added to the
3385  * filtered list (failed clone; expired retry) and the caller should
3386  * free it and handle it like a failure (eg by sending a BAR.)
3387  */
3388 static int
3389 ath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3390     struct ath_buf *bf)
3391 {
3392         struct ath_buf *nbf;
3393         int retval;
3394
3395         ATH_TX_LOCK_ASSERT(sc);
3396
3397         /*
3398          * Don't allow a filtered frame to live forever.
3399          */
3400         if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3401                 sc->sc_stats.ast_tx_swretrymax++;
3402                 DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3403                     "%s: bf=%p, seqno=%d, exceeded retries\n",
3404                     __func__,
3405                     bf,
3406                     bf->bf_state.bfs_seqno);
3407                 return (0);
3408         }
3409
3410         /*
3411          * A busy buffer can't be added to the retry list.
3412          * It needs to be cloned.
3413          */
3414         if (bf->bf_flags & ATH_BUF_BUSY) {
3415                 nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3416                 DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3417                     "%s: busy buffer clone: %p -> %p\n",
3418                     __func__, bf, nbf);
3419         } else {
3420                 nbf = bf;
3421         }
3422
3423         if (nbf == NULL) {
3424                 DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3425                     "%s: busy buffer couldn't be cloned (%p)!\n",
3426                     __func__, bf);
3427                 retval = 1;
3428         } else {
3429                 ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3430                 retval = 0;
3431         }
3432         ath_tx_tid_filt_comp_complete(sc, tid);
3433
3434         return (retval);
3435 }
3436
3437 static void
3438 ath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3439     struct ath_buf *bf_first, ath_bufhead *bf_q)
3440 {
3441         struct ath_buf *bf, *bf_next, *nbf;
3442
3443         ATH_TX_LOCK_ASSERT(sc);
3444
3445         bf = bf_first;
3446         while (bf) {
3447                 bf_next = bf->bf_next;
3448                 bf->bf_next = NULL;     /* Remove it from the aggr list */
3449
3450                 /*
3451                  * Don't allow a filtered frame to live forever.
3452                  */
3453                 if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3454                         sc->sc_stats.ast_tx_swretrymax++;
3455                         DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3456                             "%s: bf=%p, seqno=%d, exceeded retries\n",
3457                             __func__,
3458                             bf,
3459                             bf->bf_state.bfs_seqno);
3460                         TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3461                         goto next;
3462                 }
3463
3464                 if (bf->bf_flags & ATH_BUF_BUSY) {
3465                         nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3466                         DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3467                             "%s: busy buffer cloned: %p -> %p",
3468                             __func__, bf, nbf);
3469                 } else {
3470                         nbf = bf;
3471                 }
3472
3473                 /*
3474                  * If the buffer couldn't be cloned, add it to bf_q;
3475                  * the caller will free the buffer(s) as required.
3476                  */
3477                 if (nbf == NULL) {
3478                         DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3479                             "%s: buffer couldn't be cloned! (%p)\n",
3480                             __func__, bf);
3481                         TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3482                 } else {
3483                         ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3484                 }
3485 next:
3486                 bf = bf_next;
3487         }
3488
3489         ath_tx_tid_filt_comp_complete(sc, tid);
3490 }
3491
3492 /*
3493  * Suspend the queue because we need to TX a BAR.
3494  */
3495 static void
3496 ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
3497 {
3498
3499         ATH_TX_LOCK_ASSERT(sc);
3500
3501         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3502             "%s: tid=%d, bar_wait=%d, bar_tx=%d, called\n",
3503             __func__,
3504             tid->tid,
3505             tid->bar_wait,
3506             tid->bar_tx);
3507
3508         /* We shouldn't be called when bar_tx is 1 */
3509         if (tid->bar_tx) {
3510                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3511                     "%s: bar_tx is 1?!\n", __func__);
3512         }
3513
3514         /* If we've already been called, just be patient. */
3515         if (tid->bar_wait)
3516                 return;
3517
3518         /* Wait! */
3519         tid->bar_wait = 1;
3520
3521         /* Only one pause, no matter how many frames fail */
3522         ath_tx_tid_pause(sc, tid);
3523 }
3524
3525 /*
3526  * We've finished with BAR handling - either we succeeded or
3527  * failed. Either way, unsuspend TX.
3528  */
3529 static void
3530 ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
3531 {
3532
3533         ATH_TX_LOCK_ASSERT(sc);
3534
3535         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3536             "%s: %6D: TID=%d, called\n",
3537             __func__,
3538             tid->an->an_node.ni_macaddr,
3539             ":",
3540             tid->tid);
3541
3542         if (tid->bar_tx == 0 || tid->bar_wait == 0) {
3543                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3544                     "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
3545                     __func__, tid->an->an_node.ni_macaddr, ":",
3546                     tid->tid, tid->bar_tx, tid->bar_wait);
3547         }
3548
3549         tid->bar_tx = tid->bar_wait = 0;
3550         ath_tx_tid_resume(sc, tid);
3551 }
3552
3553 /*
3554  * Return whether we're ready to TX a BAR frame.
3555  *
3556  * Requires the TID lock be held.
3557  */
3558 static int
3559 ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
3560 {
3561
3562         ATH_TX_LOCK_ASSERT(sc);
3563
3564         if (tid->bar_wait == 0 || tid->hwq_depth > 0)
3565                 return (0);
3566
3567         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3568             "%s: %6D: TID=%d, bar ready\n",
3569             __func__,
3570             tid->an->an_node.ni_macaddr,
3571             ":",
3572             tid->tid);
3573
3574         return (1);
3575 }
3576
3577 /*
3578  * Check whether the current TID is ready to have a BAR
3579  * TXed and if so, do the TX.
3580  *
3581  * Since the TID/TXQ lock can't be held during a call to
3582  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
3583  * sending the BAR and locking it again.
3584  *
3585  * Eventually, the code to send the BAR should be broken out
3586  * from this routine so the lock doesn't have to be reacquired
3587  * just to be immediately dropped by the caller.
3588  */
3589 static void
3590 ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
3591 {
3592         struct ieee80211_tx_ampdu *tap;
3593
3594         ATH_TX_LOCK_ASSERT(sc);
3595
3596         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3597             "%s: %6D: TID=%d, called\n",
3598             __func__,
3599             tid->an->an_node.ni_macaddr,
3600             ":",
3601             tid->tid);
3602
3603         tap = ath_tx_get_tx_tid(tid->an, tid->tid);
3604
3605         /*
3606          * This is an error condition!
3607          */
3608         if (tid->bar_wait == 0 || tid->bar_tx == 1) {
3609                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3610                     "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
3611                     __func__, tid->an->an_node.ni_macaddr, ":",
3612                     tid->tid, tid->bar_tx, tid->bar_wait);
3613                 return;
3614         }
3615
3616         /* Don't do anything if we still have pending frames */
3617         if (tid->hwq_depth > 0) {
3618                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3619                     "%s: %6D: TID=%d, hwq_depth=%d, waiting\n",
3620                     __func__,
3621                     tid->an->an_node.ni_macaddr,
3622                     ":",
3623                     tid->tid,
3624                     tid->hwq_depth);
3625                 return;
3626         }
3627
3628         /* We're now about to TX */
3629         tid->bar_tx = 1;
3630
3631         /*
3632          * Override the clrdmask configuration for the next frame,
3633          * just to get the ball rolling.
3634          */
3635         ath_tx_set_clrdmask(sc, tid->an);
3636
3637         /*
3638          * Calculate new BAW left edge, now that all frames have either
3639          * succeeded or failed.
3640          *
3641          * XXX verify this is _actually_ the valid value to begin at!
3642          */
3643         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3644             "%s: %6D: TID=%d, new BAW left edge=%d\n",
3645             __func__,
3646             tid->an->an_node.ni_macaddr,
3647             ":",
3648             tid->tid,
3649             tap->txa_start);
3650
3651         /* Try sending the BAR frame */
3652         /* We can't hold the lock here! */
3653
3654         ATH_TX_UNLOCK(sc);
3655         if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
3656                 /* Success? Now we wait for notification that it's done */
3657                 ATH_TX_LOCK(sc);
3658                 return;
3659         }
3660
3661         /* Failure? For now, warn loudly and continue */
3662         ATH_TX_LOCK(sc);
3663         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3664             "%s: %6D: TID=%d, failed to TX BAR, continue!\n",
3665             __func__, tid->an->an_node.ni_macaddr, ":",
3666             tid->tid);
3667         ath_tx_tid_bar_unsuspend(sc, tid);
3668 }
3669
3670 static void
3671 ath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3672     struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3673 {
3674
3675         ATH_TX_LOCK_ASSERT(sc);
3676
3677         /*
3678          * If the current TID is running AMPDU, update
3679          * the BAW.
3680          */
3681         if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3682             bf->bf_state.bfs_dobaw) {
3683                 /*
3684                  * Only remove the frame from the BAW if it's
3685                  * been transmitted at least once; this means
3686                  * the frame was in the BAW to begin with.
3687                  */
3688                 if (bf->bf_state.bfs_retries > 0) {
3689                         ath_tx_update_baw(sc, an, tid, bf);
3690                         bf->bf_state.bfs_dobaw = 0;
3691                 }
3692 #if 0
3693                 /*
3694                  * This has become a non-fatal error now
3695                  */
3696                 if (! bf->bf_state.bfs_addedbaw)
3697                         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW
3698                             "%s: wasn't added: seqno %d\n",
3699                             __func__, SEQNO(bf->bf_state.bfs_seqno));
3700 #endif
3701         }
3702
3703         /* Strip it out of an aggregate list if it was in one */
3704         bf->bf_next = NULL;
3705
3706         /* Insert on the free queue to be freed by the caller */
3707         TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3708 }
3709
3710 static void
3711 ath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
3712     const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3713 {
3714         struct ieee80211_node *ni = &an->an_node;
3715         struct ath_txq *txq;
3716         struct ieee80211_tx_ampdu *tap;
3717
3718         txq = sc->sc_ac2q[tid->ac];
3719         tap = ath_tx_get_tx_tid(an, tid->tid);
3720
3721         DPRINTF(sc, ATH_DEBUG_SW_TX,
3722             "%s: %s: %6D: bf=%p: addbaw=%d, dobaw=%d, "
3723             "seqno=%d, retry=%d\n",
3724             __func__,
3725             pfx,
3726             ni->ni_macaddr,
3727             ":",
3728             bf,
3729             bf->bf_state.bfs_addedbaw,
3730             bf->bf_state.bfs_dobaw,
3731             SEQNO(bf->bf_state.bfs_seqno),
3732             bf->bf_state.bfs_retries);
3733         DPRINTF(sc, ATH_DEBUG_SW_TX,
3734             "%s: %s: %6D: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
3735             __func__,
3736             pfx,
3737             ni->ni_macaddr,
3738             ":",
3739             bf,
3740             txq->axq_qnum,
3741             txq->axq_depth,
3742             txq->axq_aggr_depth);
3743         DPRINTF(sc, ATH_DEBUG_SW_TX,
3744             "%s: %s: %6D: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, "
3745               "isfiltered=%d\n",
3746             __func__,
3747             pfx,
3748             ni->ni_macaddr,
3749             ":",
3750             bf,
3751             tid->axq_depth,
3752             tid->hwq_depth,
3753             tid->bar_wait,
3754             tid->isfiltered);
3755         DPRINTF(sc, ATH_DEBUG_SW_TX,
3756             "%s: %s: %6D: tid %d: "
3757             "sched=%d, paused=%d, "
3758             "incomp=%d, baw_head=%d, "
3759             "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3760              __func__,
3761              pfx,
3762              ni->ni_macaddr,
3763              ":",
3764              tid->tid,
3765              tid->sched, tid->paused,
3766              tid->incomp, tid->baw_head,
3767              tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3768              ni->ni_txseqs[tid->tid]);
3769
3770         /* XXX Dump the frame, see what it is? */
3771         ieee80211_dump_pkt(ni->ni_ic,
3772             mtod(bf->bf_m, const uint8_t *),
3773             bf->bf_m->m_len, 0, -1);
3774 }
3775
3776 /*
3777  * Free any packets currently pending in the software TX queue.
3778  *
3779  * This will be called when a node is being deleted.
3780  *
3781  * It can also be called on an active node during an interface
3782  * reset or state transition.
3783  *
3784  * (From Linux/reference):
3785  *
3786  * TODO: For frame(s) that are in the retry state, we will reuse the
3787  * sequence number(s) without setting the retry bit. The
3788  * alternative is to give up on these and BAR the receiver's window
3789  * forward.
3790  */
3791 static void
3792 ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3793     struct ath_tid *tid, ath_bufhead *bf_cq)
3794 {
3795         struct ath_buf *bf;
3796         struct ieee80211_tx_ampdu *tap;
3797         struct ieee80211_node *ni = &an->an_node;
3798         int t;
3799
3800         tap = ath_tx_get_tx_tid(an, tid->tid);
3801
3802         ATH_TX_LOCK_ASSERT(sc);
3803
3804         /* Walk the queue, free frames */
3805         t = 0;
3806         for (;;) {
3807                 bf = ATH_TID_FIRST(tid);
3808                 if (bf == NULL) {
3809                         break;
3810                 }
3811
3812                 if (t == 0) {
3813                         ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
3814                         t = 1;
3815                 }
3816
3817                 ATH_TID_REMOVE(tid, bf, bf_list);
3818                 ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3819         }
3820
3821         /* And now, drain the filtered frame queue */
3822         t = 0;
3823         for (;;) {
3824                 bf = ATH_TID_FILT_FIRST(tid);
3825                 if (bf == NULL)
3826                         break;
3827
3828                 if (t == 0) {
3829                         ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
3830                         t = 1;
3831                 }
3832
3833                 ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3834                 ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3835         }
3836
3837         /*
3838          * Override the clrdmask configuration for the next frame
3839          * in case there is some future transmission, just to get
3840          * the ball rolling.
3841          *
3842          * This won't hurt things if the TID is about to be freed.
3843          */
3844         ath_tx_set_clrdmask(sc, tid->an);
3845
3846         /*
3847          * Now that it's completed, grab the TID lock and update
3848          * the sequence number and BAW window.
3849          * Because sequence numbers have been assigned to frames
3850          * that haven't been sent yet, it's entirely possible
3851          * we'll be called with some pending frames that have not
3852          * been transmitted.
3853          *
3854          * The cleaner solution is to do the sequence number allocation
3855          * when the packet is first transmitted - and thus the "retries"
3856          * check above would be enough to update the BAW/seqno.
3857          */
3858
3859         /* But don't do it for non-QoS TIDs */
3860         if (tap) {
3861 #if 1
3862                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3863                     "%s: %6D: node %p: TID %d: sliding BAW left edge to %d\n",
3864                     __func__,
3865                     ni->ni_macaddr,
3866                     ":",
3867                     an,
3868                     tid->tid,
3869                     tap->txa_start);
3870 #endif
3871                 ni->ni_txseqs[tid->tid] = tap->txa_start;
3872                 tid->baw_tail = tid->baw_head;
3873         }
3874 }
3875
3876 /*
3877  * Reset the TID state.  This must be only called once the node has
3878  * had its frames flushed from this TID, to ensure that no other
3879  * pause / unpause logic can kick in.
3880  */
3881 static void
3882 ath_tx_tid_reset(struct ath_softc *sc, struct ath_tid *tid)
3883 {
3884
3885 #if 0
3886         tid->bar_wait = tid->bar_tx = tid->isfiltered = 0;
3887         tid->paused = tid->sched = tid->addba_tx_pending = 0;
3888         tid->incomp = tid->cleanup_inprogress = 0;
3889 #endif
3890
3891         /*
3892          * If we have a bar_wait set, we need to unpause the TID
3893          * here.  Otherwise once cleanup has finished, the TID won't
3894          * have the right paused counter.
3895          *
3896          * XXX I'm not going through resume here - I don't want the
3897          * node to be rescheuled just yet.  This however should be
3898          * methodized!
3899          */
3900         if (tid->bar_wait) {
3901                 if (tid->paused > 0) {
3902                         tid->paused --;
3903                 }
3904         }
3905
3906         /*
3907          * XXX same with a currently filtered TID.
3908          *
3909          * Since this is being called during a flush, we assume that
3910          * the filtered frame list is actually empty.
3911          *
3912          * XXX TODO: add in a check to ensure that the filtered queue
3913          * depth is actually 0!
3914          */
3915         if (tid->isfiltered) {
3916                 if (tid->paused > 0) {
3917                         tid->paused --;
3918                 }
3919         }
3920
3921         /*
3922          * Clear BAR, filtered frames, scheduled and ADDBA pending.
3923          * The TID may be going through cleanup from the last association
3924          * where things in the BAW are still in the hardware queue.
3925          */
3926         tid->bar_wait = 0;
3927         tid->bar_tx = 0;
3928         tid->isfiltered = 0;
3929         tid->sched = 0;
3930         tid->addba_tx_pending = 0;
3931
3932         /*
3933          * XXX TODO: it may just be enough to walk the HWQs and mark
3934          * frames for that node as non-aggregate; or mark the ath_node
3935          * with something that indicates that aggregation is no longer
3936          * occuring.  Then we can just toss the BAW complaints and
3937          * do a complete hard reset of state here - no pause, no
3938          * complete counter, etc.
3939          */
3940
3941 }
3942
3943 /*
3944  * Flush all software queued packets for the given node.
3945  *
3946  * This occurs when a completion handler frees the last buffer
3947  * for a node, and the node is thus freed. This causes the node
3948  * to be cleaned up, which ends up calling ath_tx_node_flush.
3949  */
3950 void
3951 ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3952 {
3953         int tid;
3954         ath_bufhead bf_cq;
3955         struct ath_buf *bf;
3956
3957         TAILQ_INIT(&bf_cq);
3958
3959         ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
3960             &an->an_node);
3961
3962         ATH_TX_LOCK(sc);
3963         DPRINTF(sc, ATH_DEBUG_NODE,
3964             "%s: %6D: flush; is_powersave=%d, stack_psq=%d, tim=%d, "
3965             "swq_depth=%d, clrdmask=%d, leak_count=%d\n",
3966             __func__,
3967             an->an_node.ni_macaddr,
3968             ":",
3969             an->an_is_powersave,
3970             an->an_stack_psq,
3971             an->an_tim_set,
3972             an->an_swq_depth,
3973             an->clrdmask,
3974             an->an_leak_count);
3975
3976         for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3977                 struct ath_tid *atid = &an->an_tid[tid];
3978
3979                 /* Free packets */
3980                 ath_tx_tid_drain(sc, an, atid, &bf_cq);
3981
3982                 /* Remove this tid from the list of active tids */
3983                 ath_tx_tid_unsched(sc, atid);
3984
3985                 /* Reset the per-TID pause, BAR, etc state */
3986                 ath_tx_tid_reset(sc, atid);
3987         }
3988
3989         /*
3990          * Clear global leak count
3991          */
3992         an->an_leak_count = 0;
3993         ATH_TX_UNLOCK(sc);
3994
3995         /* Handle completed frames */
3996         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3997                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
3998                 ath_tx_default_comp(sc, bf, 0);
3999         }
4000 }
4001
4002 /*
4003  * Drain all the software TXQs currently with traffic queued.
4004  */
4005 void
4006 ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
4007 {
4008         struct ath_tid *tid;
4009         ath_bufhead bf_cq;
4010         struct ath_buf *bf;
4011
4012         TAILQ_INIT(&bf_cq);
4013         ATH_TX_LOCK(sc);
4014
4015         /*
4016          * Iterate over all active tids for the given txq,
4017          * flushing and unsched'ing them
4018          */
4019         while (! TAILQ_EMPTY(&txq->axq_tidq)) {
4020                 tid = TAILQ_FIRST(&txq->axq_tidq);
4021                 ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
4022                 ath_tx_tid_unsched(sc, tid);
4023         }
4024
4025         ATH_TX_UNLOCK(sc);
4026
4027         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4028                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
4029                 ath_tx_default_comp(sc, bf, 0);
4030         }
4031 }
4032
4033 /*
4034  * Handle completion of non-aggregate session frames.
4035  *
4036  * This (currently) doesn't implement software retransmission of
4037  * non-aggregate frames!
4038  *
4039  * Software retransmission of non-aggregate frames needs to obey
4040  * the strict sequence number ordering, and drop any frames that
4041  * will fail this.
4042  *
4043  * For now, filtered frames and frame transmission will cause
4044  * all kinds of issues.  So we don't support them.
4045  *
4046  * So anyone queuing frames via ath_tx_normal_xmit() or
4047  * ath_tx_hw_queue_norm() must override and set CLRDMASK.
4048  */
4049 void
4050 ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4051 {
4052         struct ieee80211_node *ni = bf->bf_node;
4053         struct ath_node *an = ATH_NODE(ni);
4054         int tid = bf->bf_state.bfs_tid;
4055         struct ath_tid *atid = &an->an_tid[tid];
4056         struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4057
4058         /* The TID state is protected behind the TXQ lock */
4059         ATH_TX_LOCK(sc);
4060
4061         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
4062             __func__, bf, fail, atid->hwq_depth - 1);
4063
4064         atid->hwq_depth--;
4065
4066 #if 0
4067         /*
4068          * If the frame was filtered, stick it on the filter frame
4069          * queue and complain about it.  It shouldn't happen!
4070          */
4071         if ((ts->ts_status & HAL_TXERR_FILT) ||
4072             (ts->ts_status != 0 && atid->isfiltered)) {
4073                 DPRINTF(sc, ATH_DEBUG_SW_TX,
4074                     "%s: isfiltered=%d, ts_status=%d: huh?\n",
4075                     __func__,
4076                     atid->isfiltered,
4077                     ts->ts_status);
4078                 ath_tx_tid_filt_comp_buf(sc, atid, bf);
4079         }
4080 #endif
4081         if (atid->isfiltered)
4082                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: filtered?!\n", __func__);
4083         if (atid->hwq_depth < 0)
4084                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
4085                     __func__, atid->hwq_depth);
4086
4087         /*
4088          * If the queue is filtered, potentially mark it as complete
4089          * and reschedule it as needed.
4090          *
4091          * This is required as there may be a subsequent TX descriptor
4092          * for this end-node that has CLRDMASK set, so it's quite possible
4093          * that a filtered frame will be followed by a non-filtered
4094          * (complete or otherwise) frame.
4095          *
4096          * XXX should we do this before we complete the frame?
4097          */
4098         if (atid->isfiltered)
4099                 ath_tx_tid_filt_comp_complete(sc, atid);
4100         ATH_TX_UNLOCK(sc);
4101
4102         /*
4103          * punt to rate control if we're not being cleaned up
4104          * during a hw queue drain and the frame wanted an ACK.
4105          */
4106         if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4107                 ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4108                     ts, bf->bf_state.bfs_pktlen,
4109                     1, (ts->ts_status == 0) ? 0 : 1);
4110
4111         ath_tx_default_comp(sc, bf, fail);
4112 }
4113
4114 /*
4115  * Handle cleanup of aggregate session packets that aren't
4116  * an A-MPDU.
4117  *
4118  * There's no need to update the BAW here - the session is being
4119  * torn down.
4120  */
4121 static void
4122 ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4123 {
4124         struct ieee80211_node *ni = bf->bf_node;
4125         struct ath_node *an = ATH_NODE(ni);
4126         int tid = bf->bf_state.bfs_tid;
4127         struct ath_tid *atid = &an->an_tid[tid];
4128
4129         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
4130             __func__, tid, atid->incomp);
4131
4132         ATH_TX_LOCK(sc);
4133         atid->incomp--;
4134         if (atid->incomp == 0) {
4135                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4136                     "%s: TID %d: cleaned up! resume!\n",
4137                     __func__, tid);
4138                 atid->cleanup_inprogress = 0;
4139                 ath_tx_tid_resume(sc, atid);
4140         }
4141         ATH_TX_UNLOCK(sc);
4142
4143         ath_tx_default_comp(sc, bf, 0);
4144 }
4145
4146 /*
4147  * Performs transmit side cleanup when TID changes from aggregated to
4148  * unaggregated.
4149  *
4150  * - Discard all retry frames from the s/w queue.
4151  * - Fix the tx completion function for all buffers in s/w queue.
4152  * - Count the number of unacked frames, and let transmit completion
4153  *   handle it later.
4154  *
4155  * The caller is responsible for pausing the TID and unpausing the
4156  * TID if no cleanup was required. Otherwise the cleanup path will
4157  * unpause the TID once the last hardware queued frame is completed.
4158  */
4159 static void
4160 ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid,
4161     ath_bufhead *bf_cq)
4162 {
4163         struct ath_tid *atid = &an->an_tid[tid];
4164         struct ieee80211_tx_ampdu *tap;
4165         struct ath_buf *bf, *bf_next;
4166
4167         ATH_TX_LOCK_ASSERT(sc);
4168
4169         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4170             "%s: TID %d: called\n", __func__, tid);
4171
4172         /*
4173          * Move the filtered frames to the TX queue, before
4174          * we run off and discard/process things.
4175          */
4176         /* XXX this is really quite inefficient */
4177         while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
4178                 ATH_TID_FILT_REMOVE(atid, bf, bf_list);
4179                 ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4180         }
4181
4182         /*
4183          * Update the frames in the software TX queue:
4184          *
4185          * + Discard retry frames in the queue
4186          * + Fix the completion function to be non-aggregate
4187          */
4188         bf = ATH_TID_FIRST(atid);
4189         while (bf) {
4190                 if (bf->bf_state.bfs_isretried) {
4191                         bf_next = TAILQ_NEXT(bf, bf_list);
4192                         ATH_TID_REMOVE(atid, bf, bf_list);
4193                         if (bf->bf_state.bfs_dobaw) {
4194                                 ath_tx_update_baw(sc, an, atid, bf);
4195                                 if (!bf->bf_state.bfs_addedbaw)
4196                                         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4197                                             "%s: wasn't added: seqno %d\n",
4198                                             __func__,
4199                                             SEQNO(bf->bf_state.bfs_seqno));
4200                         }
4201                         bf->bf_state.bfs_dobaw = 0;
4202                         /*
4203                          * Call the default completion handler with "fail" just
4204                          * so upper levels are suitably notified about this.
4205                          */
4206                         TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
4207                         bf = bf_next;
4208                         continue;
4209                 }
4210                 /* Give these the default completion handler */
4211                 bf->bf_comp = ath_tx_normal_comp;
4212                 bf = TAILQ_NEXT(bf, bf_list);
4213         }
4214
4215         /*
4216          * Calculate what hardware-queued frames exist based
4217          * on the current BAW size. Ie, what frames have been
4218          * added to the TX hardware queue for this TID but
4219          * not yet ACKed.
4220          */
4221         tap = ath_tx_get_tx_tid(an, tid);
4222         /* Need the lock - fiddling with BAW */
4223         while (atid->baw_head != atid->baw_tail) {
4224                 if (atid->tx_buf[atid->baw_head]) {
4225                         atid->incomp++;
4226                         atid->cleanup_inprogress = 1;
4227                         atid->tx_buf[atid->baw_head] = NULL;
4228                 }
4229                 INCR(atid->baw_head, ATH_TID_MAX_BUFS);
4230                 INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
4231         }
4232
4233         if (atid->cleanup_inprogress)
4234                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4235                     "%s: TID %d: cleanup needed: %d packets\n",
4236                     __func__, tid, atid->incomp);
4237
4238         /* Owner now must free completed frames */
4239 }
4240
4241 static struct ath_buf *
4242 ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
4243     struct ath_tid *tid, struct ath_buf *bf)
4244 {
4245         struct ath_buf *nbf;
4246         int error;
4247
4248         /*
4249          * Clone the buffer.  This will handle the dma unmap and
4250          * copy the node reference to the new buffer.  If this
4251          * works out, 'bf' will have no DMA mapping, no mbuf
4252          * pointer and no node reference.
4253          */
4254         nbf = ath_buf_clone(sc, bf);
4255
4256 #if 0
4257         DPRINTF(sc, ATH_DEBUG_XMIT, "%s: ATH_BUF_BUSY; cloning\n",
4258             __func__);
4259 #endif
4260
4261         if (nbf == NULL) {
4262                 /* Failed to clone */
4263                 DPRINTF(sc, ATH_DEBUG_XMIT,
4264                     "%s: failed to clone a busy buffer\n",
4265                     __func__);
4266                 return NULL;
4267         }
4268
4269         /* Setup the dma for the new buffer */
4270         error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
4271         if (error != 0) {
4272                 DPRINTF(sc, ATH_DEBUG_XMIT,
4273                     "%s: failed to setup dma for clone\n",
4274                     __func__);
4275                 /*
4276                  * Put this at the head of the list, not tail;
4277                  * that way it doesn't interfere with the
4278                  * busy buffer logic (which uses the tail of
4279                  * the list.)
4280                  */
4281                 ATH_TXBUF_LOCK(sc);
4282                 ath_returnbuf_head(sc, nbf);
4283                 ATH_TXBUF_UNLOCK(sc);
4284                 return NULL;
4285         }
4286
4287         /* Update BAW if required, before we free the original buf */
4288         if (bf->bf_state.bfs_dobaw)
4289                 ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
4290
4291         /* Free original buffer; return new buffer */
4292         ath_freebuf(sc, bf);
4293
4294         return nbf;
4295 }
4296
4297 /*
4298  * Handle retrying an unaggregate frame in an aggregate
4299  * session.
4300  *
4301  * If too many retries occur, pause the TID, wait for
4302  * any further retransmits (as there's no reason why
4303  * non-aggregate frames in an aggregate session are
4304  * transmitted in-order; they just have to be in-BAW)
4305  * and then queue a BAR.
4306  */
4307 static void
4308 ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4309 {
4310         struct ieee80211_node *ni = bf->bf_node;
4311         struct ath_node *an = ATH_NODE(ni);
4312         int tid = bf->bf_state.bfs_tid;
4313         struct ath_tid *atid = &an->an_tid[tid];
4314         struct ieee80211_tx_ampdu *tap;
4315
4316         ATH_TX_LOCK(sc);
4317
4318         tap = ath_tx_get_tx_tid(an, tid);
4319
4320         /*
4321          * If the buffer is marked as busy, we can't directly
4322          * reuse it. Instead, try to clone the buffer.
4323          * If the clone is successful, recycle the old buffer.
4324          * If the clone is unsuccessful, set bfs_retries to max
4325          * to force the next bit of code to free the buffer
4326          * for us.
4327          */
4328         if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4329             (bf->bf_flags & ATH_BUF_BUSY)) {
4330                 struct ath_buf *nbf;
4331                 nbf = ath_tx_retry_clone(sc, an, atid, bf);
4332                 if (nbf)
4333                         /* bf has been freed at this point */
4334                         bf = nbf;
4335                 else
4336                         bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4337         }
4338
4339         if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4340                 DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4341                     "%s: exceeded retries; seqno %d\n",
4342                     __func__, SEQNO(bf->bf_state.bfs_seqno));
4343                 sc->sc_stats.ast_tx_swretrymax++;
4344
4345                 /* Update BAW anyway */
4346                 if (bf->bf_state.bfs_dobaw) {
4347                         ath_tx_update_baw(sc, an, atid, bf);
4348                         if (! bf->bf_state.bfs_addedbaw)
4349                                 DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4350                                     "%s: wasn't added: seqno %d\n",
4351                                     __func__, SEQNO(bf->bf_state.bfs_seqno));
4352                 }
4353                 bf->bf_state.bfs_dobaw = 0;
4354
4355                 /* Suspend the TX queue and get ready to send the BAR */
4356                 ath_tx_tid_bar_suspend(sc, atid);
4357
4358                 /* Send the BAR if there are no other frames waiting */
4359                 if (ath_tx_tid_bar_tx_ready(sc, atid))
4360                         ath_tx_tid_bar_tx(sc, atid);
4361
4362                 ATH_TX_UNLOCK(sc);
4363
4364                 /* Free buffer, bf is free after this call */
4365                 ath_tx_default_comp(sc, bf, 0);
4366                 return;
4367         }
4368
4369         /*
4370          * This increments the retry counter as well as
4371          * sets the retry flag in the ath_buf and packet
4372          * body.
4373          */
4374         ath_tx_set_retry(sc, bf);
4375         sc->sc_stats.ast_tx_swretries++;
4376
4377         /*
4378          * Insert this at the head of the queue, so it's
4379          * retried before any current/subsequent frames.
4380          */
4381         ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4382         ath_tx_tid_sched(sc, atid);
4383         /* Send the BAR if there are no other frames waiting */
4384         if (ath_tx_tid_bar_tx_ready(sc, atid))
4385                 ath_tx_tid_bar_tx(sc, atid);
4386
4387         ATH_TX_UNLOCK(sc);
4388 }
4389
4390 /*
4391  * Common code for aggregate excessive retry/subframe retry.
4392  * If retrying, queues buffers to bf_q. If not, frees the
4393  * buffers.
4394  *
4395  * XXX should unify this with ath_tx_aggr_retry_unaggr()
4396  */
4397 static int
4398 ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
4399     ath_bufhead *bf_q)
4400 {
4401         struct ieee80211_node *ni = bf->bf_node;
4402         struct ath_node *an = ATH_NODE(ni);
4403         int tid = bf->bf_state.bfs_tid;
4404         struct ath_tid *atid = &an->an_tid[tid];
4405
4406         ATH_TX_LOCK_ASSERT(sc);
4407
4408         /* XXX clr11naggr should be done for all subframes */
4409         ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4410         ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
4411
4412         /* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
4413
4414         /*
4415          * If the buffer is marked as busy, we can't directly
4416          * reuse it. Instead, try to clone the buffer.
4417          * If the clone is successful, recycle the old buffer.
4418          * If the clone is unsuccessful, set bfs_retries to max
4419          * to force the next bit of code to free the buffer
4420          * for us.
4421          */
4422         if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4423             (bf->bf_flags & ATH_BUF_BUSY)) {
4424                 struct ath_buf *nbf;
4425                 nbf = ath_tx_retry_clone(sc, an, atid, bf);
4426                 if (nbf)
4427                         /* bf has been freed at this point */
4428                         bf = nbf;
4429                 else
4430                         bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4431         }
4432
4433         if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4434                 sc->sc_stats.ast_tx_swretrymax++;
4435                 DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4436                     "%s: max retries: seqno %d\n",
4437                     __func__, SEQNO(bf->bf_state.bfs_seqno));
4438                 ath_tx_update_baw(sc, an, atid, bf);
4439                 if (!bf->bf_state.bfs_addedbaw)
4440                         DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4441                             "%s: wasn't added: seqno %d\n",
4442                             __func__, SEQNO(bf->bf_state.bfs_seqno));
4443                 bf->bf_state.bfs_dobaw = 0;
4444                 return 1;
4445         }
4446
4447         ath_tx_set_retry(sc, bf);
4448         sc->sc_stats.ast_tx_swretries++;
4449         bf->bf_next = NULL;             /* Just to make sure */
4450
4451         /* Clear the aggregate state */
4452         bf->bf_state.bfs_aggr = 0;
4453         bf->bf_state.bfs_ndelim = 0;    /* ??? needed? */
4454         bf->bf_state.bfs_nframes = 1;
4455
4456         TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4457         return 0;
4458 }
4459
4460 /*
4461  * error pkt completion for an aggregate destination
4462  */
4463 static void
4464 ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4465     struct ath_tid *tid)
4466 {
4467         struct ieee80211_node *ni = bf_first->bf_node;
4468         struct ath_node *an = ATH_NODE(ni);
4469         struct ath_buf *bf_next, *bf;
4470         ath_bufhead bf_q;
4471         int drops = 0;
4472         struct ieee80211_tx_ampdu *tap;
4473         ath_bufhead bf_cq;
4474
4475         TAILQ_INIT(&bf_q);
4476         TAILQ_INIT(&bf_cq);
4477
4478         /*
4479          * Update rate control - all frames have failed.
4480          *
4481          * XXX use the length in the first frame in the series;
4482          * XXX just so things are consistent for now.
4483          */
4484         ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4485             &bf_first->bf_status.ds_txstat,
4486             bf_first->bf_state.bfs_pktlen,
4487             bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4488
4489         ATH_TX_LOCK(sc);
4490         tap = ath_tx_get_tx_tid(an, tid->tid);
4491         sc->sc_stats.ast_tx_aggr_failall++;
4492
4493         /* Retry all subframes */
4494         bf = bf_first;
4495         while (bf) {
4496                 bf_next = bf->bf_next;
4497                 bf->bf_next = NULL;     /* Remove it from the aggr list */
4498                 sc->sc_stats.ast_tx_aggr_fail++;
4499                 if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4500                         drops++;
4501                         bf->bf_next = NULL;
4502                         TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4503                 }
4504                 bf = bf_next;
4505         }
4506
4507         /* Prepend all frames to the beginning of the queue */
4508         while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4509                 TAILQ_REMOVE(&bf_q, bf, bf_list);
4510                 ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4511         }
4512
4513         /*
4514          * Schedule the TID to be re-tried.
4515          */
4516         ath_tx_tid_sched(sc, tid);
4517
4518         /*
4519          * send bar if we dropped any frames
4520          *
4521          * Keep the txq lock held for now, as we need to ensure
4522          * that ni_txseqs[] is consistent (as it's being updated
4523          * in the ifnet TX context or raw TX context.)
4524          */
4525         if (drops) {
4526                 /* Suspend the TX queue and get ready to send the BAR */
4527                 ath_tx_tid_bar_suspend(sc, tid);
4528         }
4529
4530         /*
4531          * Send BAR if required
4532          */
4533         if (ath_tx_tid_bar_tx_ready(sc, tid))
4534                 ath_tx_tid_bar_tx(sc, tid);
4535
4536         ATH_TX_UNLOCK(sc);
4537
4538         /* Complete frames which errored out */
4539         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4540                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
4541                 ath_tx_default_comp(sc, bf, 0);
4542         }
4543 }
4544
4545 /*
4546  * Handle clean-up of packets from an aggregate list.
4547  *
4548  * There's no need to update the BAW here - the session is being
4549  * torn down.
4550  */
4551 static void
4552 ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4553 {
4554         struct ath_buf *bf, *bf_next;
4555         struct ieee80211_node *ni = bf_first->bf_node;
4556         struct ath_node *an = ATH_NODE(ni);
4557         int tid = bf_first->bf_state.bfs_tid;
4558         struct ath_tid *atid = &an->an_tid[tid];
4559
4560         ATH_TX_LOCK(sc);
4561
4562         /* update incomp */
4563         bf = bf_first;
4564         while (bf) {
4565                 atid->incomp--;
4566                 bf = bf->bf_next;
4567         }
4568
4569         if (atid->incomp == 0) {
4570                 DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4571                     "%s: TID %d: cleaned up! resume!\n",
4572                     __func__, tid);
4573                 atid->cleanup_inprogress = 0;
4574                 ath_tx_tid_resume(sc, atid);
4575         }
4576
4577         /* Send BAR if required */
4578         /* XXX why would we send a BAR when transitioning to non-aggregation? */
4579         /*
4580          * XXX TODO: we should likely just tear down the BAR state here,
4581          * rather than sending a BAR.
4582          */
4583         if (ath_tx_tid_bar_tx_ready(sc, atid))
4584                 ath_tx_tid_bar_tx(sc, atid);
4585
4586         ATH_TX_UNLOCK(sc);
4587
4588         /* Handle frame completion */
4589         bf = bf_first;
4590         while (bf) {
4591                 bf_next = bf->bf_next;
4592                 ath_tx_default_comp(sc, bf, 1);
4593                 bf = bf_next;
4594         }
4595 }
4596
4597 /*
4598  * Handle completion of an set of aggregate frames.
4599  *
4600  * Note: the completion handler is the last descriptor in the aggregate,
4601  * not the last descriptor in the first frame.
4602  */
4603 static void
4604 ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4605     int fail)
4606 {
4607         //struct ath_desc *ds = bf->bf_lastds;
4608         struct ieee80211_node *ni = bf_first->bf_node;
4609         struct ath_node *an = ATH_NODE(ni);
4610         int tid = bf_first->bf_state.bfs_tid;
4611         struct ath_tid *atid = &an->an_tid[tid];
4612         struct ath_tx_status ts;
4613         struct ieee80211_tx_ampdu *tap;
4614         ath_bufhead bf_q;
4615         ath_bufhead bf_cq;
4616         int seq_st, tx_ok;
4617         int hasba, isaggr;
4618         uint32_t ba[2];
4619         struct ath_buf *bf, *bf_next;
4620         int ba_index;
4621         int drops = 0;
4622         int nframes = 0, nbad = 0, nf;
4623         int pktlen;
4624         /* XXX there's too much on the stack? */
4625         struct ath_rc_series rc[ATH_RC_NUM];
4626         int txseq;
4627
4628         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4629             __func__, atid->hwq_depth);
4630
4631         /*
4632          * Take a copy; this may be needed -after- bf_first
4633          * has been completed and freed.
4634          */
4635         ts = bf_first->bf_status.ds_txstat;
4636
4637         TAILQ_INIT(&bf_q);
4638         TAILQ_INIT(&bf_cq);
4639
4640         /* The TID state is kept behind the TXQ lock */
4641         ATH_TX_LOCK(sc);
4642
4643         atid->hwq_depth--;
4644         if (atid->hwq_depth < 0)
4645                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: hwq_depth < 0: %d\n",
4646                     __func__, atid->hwq_depth);
4647
4648         /*
4649          * If the TID is filtered, handle completing the filter
4650          * transition before potentially kicking it to the cleanup
4651          * function.
4652          *
4653          * XXX this is duplicate work, ew.
4654          */
4655         if (atid->isfiltered)
4656                 ath_tx_tid_filt_comp_complete(sc, atid);
4657
4658         /*
4659          * Punt cleanup to the relevant function, not our problem now
4660          */
4661         if (atid->cleanup_inprogress) {
4662                 if (atid->isfiltered)
4663                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4664                             "%s: isfiltered=1, normal_comp?\n",
4665                             __func__);
4666                 ATH_TX_UNLOCK(sc);
4667                 ath_tx_comp_cleanup_aggr(sc, bf_first);
4668                 return;
4669         }
4670
4671         /*
4672          * If the frame is filtered, transition to filtered frame
4673          * mode and add this to the filtered frame list.
4674          *
4675          * XXX TODO: figure out how this interoperates with
4676          * BAR, pause and cleanup states.
4677          */
4678         if ((ts.ts_status & HAL_TXERR_FILT) ||
4679             (ts.ts_status != 0 && atid->isfiltered)) {
4680                 if (fail != 0)
4681                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4682                             "%s: isfiltered=1, fail=%d\n", __func__, fail);
4683                 ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4684
4685                 /* Remove from BAW */
4686                 TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4687                         if (bf->bf_state.bfs_addedbaw)
4688                                 drops++;
4689                         if (bf->bf_state.bfs_dobaw) {
4690                                 ath_tx_update_baw(sc, an, atid, bf);
4691                                 if (!bf->bf_state.bfs_addedbaw)
4692                                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4693                                             "%s: wasn't added: seqno %d\n",
4694                                             __func__,
4695                                             SEQNO(bf->bf_state.bfs_seqno));
4696                         }
4697                         bf->bf_state.bfs_dobaw = 0;
4698                 }
4699                 /*
4700                  * If any intermediate frames in the BAW were dropped when
4701                  * handling filtering things, send a BAR.
4702                  */
4703                 if (drops)
4704                         ath_tx_tid_bar_suspend(sc, atid);
4705
4706                 /*
4707                  * Finish up by sending a BAR if required and freeing
4708                  * the frames outside of the TX lock.
4709                  */
4710                 goto finish_send_bar;
4711         }
4712
4713         /*
4714          * XXX for now, use the first frame in the aggregate for
4715          * XXX rate control completion; it's at least consistent.
4716          */
4717         pktlen = bf_first->bf_state.bfs_pktlen;
4718
4719         /*
4720          * Handle errors first!
4721          *
4722          * Here, handle _any_ error as a "exceeded retries" error.
4723          * Later on (when filtered frames are to be specially handled)
4724          * it'll have to be expanded.
4725          */
4726 #if 0
4727         if (ts.ts_status & HAL_TXERR_XRETRY) {
4728 #endif
4729         if (ts.ts_status != 0) {
4730                 ATH_TX_UNLOCK(sc);
4731                 ath_tx_comp_aggr_error(sc, bf_first, atid);
4732                 return;
4733         }
4734
4735         tap = ath_tx_get_tx_tid(an, tid);
4736
4737         /*
4738          * extract starting sequence and block-ack bitmap
4739          */
4740         /* XXX endian-ness of seq_st, ba? */
4741         seq_st = ts.ts_seqnum;
4742         hasba = !! (ts.ts_flags & HAL_TX_BA);
4743         tx_ok = (ts.ts_status == 0);
4744         isaggr = bf_first->bf_state.bfs_aggr;
4745         ba[0] = ts.ts_ba_low;
4746         ba[1] = ts.ts_ba_high;
4747
4748         /*
4749          * Copy the TX completion status and the rate control
4750          * series from the first descriptor, as it may be freed
4751          * before the rate control code can get its grubby fingers
4752          * into things.
4753          */
4754         memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4755
4756         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4757             "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4758             "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4759             __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4760             isaggr, seq_st, hasba, ba[0], ba[1]);
4761
4762         /*
4763          * The reference driver doesn't do this; it simply ignores
4764          * this check in its entirety.
4765          *
4766          * I've seen this occur when using iperf to send traffic
4767          * out tid 1 - the aggregate frames are all marked as TID 1,
4768          * but the TXSTATUS has TID=0.  So, let's just ignore this
4769          * check.
4770          */
4771 #if 0
4772         /* Occasionally, the MAC sends a tx status for the wrong TID. */
4773         if (tid != ts.ts_tid) {
4774                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: tid %d != hw tid %d\n",
4775                     __func__, tid, ts.ts_tid);
4776                 tx_ok = 0;
4777         }
4778 #endif
4779
4780         /* AR5416 BA bug; this requires an interface reset */
4781         if (isaggr && tx_ok && (! hasba)) {
4782                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4783                     "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4784                     "seq_st=%d\n",
4785                     __func__, hasba, tx_ok, isaggr, seq_st);
4786                 /* XXX TODO: schedule an interface reset */
4787 #ifdef ATH_DEBUG
4788                 ath_printtxbuf(sc, bf_first,
4789                     sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
4790 #endif
4791         }
4792
4793         /*
4794          * Walk the list of frames, figure out which ones were correctly
4795          * sent and which weren't.
4796          */
4797         bf = bf_first;
4798         nf = bf_first->bf_state.bfs_nframes;
4799
4800         /* bf_first is going to be invalid once this list is walked */
4801         bf_first = NULL;
4802
4803         /*
4804          * Walk the list of completed frames and determine
4805          * which need to be completed and which need to be
4806          * retransmitted.
4807          *
4808          * For completed frames, the completion functions need
4809          * to be called at the end of this function as the last
4810          * node reference may free the node.
4811          *
4812          * Finally, since the TXQ lock can't be held during the
4813          * completion callback (to avoid lock recursion),
4814          * the completion calls have to be done outside of the
4815          * lock.
4816          */
4817         while (bf) {
4818                 nframes++;
4819                 ba_index = ATH_BA_INDEX(seq_st,
4820                     SEQNO(bf->bf_state.bfs_seqno));
4821                 bf_next = bf->bf_next;
4822                 bf->bf_next = NULL;     /* Remove it from the aggr list */
4823
4824                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4825                     "%s: checking bf=%p seqno=%d; ack=%d\n",
4826                     __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4827                     ATH_BA_ISSET(ba, ba_index));
4828
4829                 if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
4830                         sc->sc_stats.ast_tx_aggr_ok++;
4831                         ath_tx_update_baw(sc, an, atid, bf);
4832                         bf->bf_state.bfs_dobaw = 0;
4833                         if (!bf->bf_state.bfs_addedbaw)
4834                                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4835                                     "%s: wasn't added: seqno %d\n",
4836                                     __func__, SEQNO(bf->bf_state.bfs_seqno));
4837                         bf->bf_next = NULL;
4838                         TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4839                 } else {
4840                         sc->sc_stats.ast_tx_aggr_fail++;
4841                         if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4842                                 drops++;
4843                                 bf->bf_next = NULL;
4844                                 TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4845                         }
4846                         nbad++;
4847                 }
4848                 bf = bf_next;
4849         }
4850
4851         /*
4852          * Now that the BAW updates have been done, unlock
4853          *
4854          * txseq is grabbed before the lock is released so we
4855          * have a consistent view of what -was- in the BAW.
4856          * Anything after this point will not yet have been
4857          * TXed.
4858          */
4859         txseq = tap->txa_start;
4860         ATH_TX_UNLOCK(sc);
4861
4862         if (nframes != nf)
4863                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4864                     "%s: num frames seen=%d; bf nframes=%d\n",
4865                     __func__, nframes, nf);
4866
4867         /*
4868          * Now we know how many frames were bad, call the rate
4869          * control code.
4870          */
4871         if (fail == 0)
4872                 ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4873                     nbad);
4874
4875         /*
4876          * send bar if we dropped any frames
4877          */
4878         if (drops) {
4879                 /* Suspend the TX queue and get ready to send the BAR */
4880                 ATH_TX_LOCK(sc);
4881                 ath_tx_tid_bar_suspend(sc, atid);
4882                 ATH_TX_UNLOCK(sc);
4883         }
4884
4885         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4886             "%s: txa_start now %d\n", __func__, tap->txa_start);
4887
4888         ATH_TX_LOCK(sc);
4889
4890         /* Prepend all frames to the beginning of the queue */
4891         while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4892                 TAILQ_REMOVE(&bf_q, bf, bf_list);
4893                 ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4894         }
4895
4896         /*
4897          * Reschedule to grab some further frames.
4898          */
4899         ath_tx_tid_sched(sc, atid);
4900
4901         /*
4902          * If the queue is filtered, re-schedule as required.
4903          *
4904          * This is required as there may be a subsequent TX descriptor
4905          * for this end-node that has CLRDMASK set, so it's quite possible
4906          * that a filtered frame will be followed by a non-filtered
4907          * (complete or otherwise) frame.
4908          *
4909          * XXX should we do this before we complete the frame?
4910          */
4911         if (atid->isfiltered)
4912                 ath_tx_tid_filt_comp_complete(sc, atid);
4913
4914 finish_send_bar:
4915
4916         /*
4917          * Send BAR if required
4918          */
4919         if (ath_tx_tid_bar_tx_ready(sc, atid))
4920                 ath_tx_tid_bar_tx(sc, atid);
4921
4922         ATH_TX_UNLOCK(sc);
4923
4924         /* Do deferred completion */
4925         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4926                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
4927                 ath_tx_default_comp(sc, bf, 0);
4928         }
4929 }
4930
4931 /*
4932  * Handle completion of unaggregated frames in an ADDBA
4933  * session.
4934  *
4935  * Fail is set to 1 if the entry is being freed via a call to
4936  * ath_tx_draintxq().
4937  */
4938 static void
4939 ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4940 {
4941         struct ieee80211_node *ni = bf->bf_node;
4942         struct ath_node *an = ATH_NODE(ni);
4943         int tid = bf->bf_state.bfs_tid;
4944         struct ath_tid *atid = &an->an_tid[tid];
4945         struct ath_tx_status ts;
4946         int drops = 0;
4947
4948         /*
4949          * Take a copy of this; filtering/cloning the frame may free the
4950          * bf pointer.
4951          */
4952         ts = bf->bf_status.ds_txstat;
4953
4954         /*
4955          * Update rate control status here, before we possibly
4956          * punt to retry or cleanup.
4957          *
4958          * Do it outside of the TXQ lock.
4959          */
4960         if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4961                 ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4962                     &bf->bf_status.ds_txstat,
4963                     bf->bf_state.bfs_pktlen,
4964                     1, (ts.ts_status == 0) ? 0 : 1);
4965
4966         /*
4967          * This is called early so atid->hwq_depth can be tracked.
4968          * This unfortunately means that it's released and regrabbed
4969          * during retry and cleanup. That's rather inefficient.
4970          */
4971         ATH_TX_LOCK(sc);
4972
4973         if (tid == IEEE80211_NONQOS_TID)
4974                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16!\n", __func__);
4975
4976         DPRINTF(sc, ATH_DEBUG_SW_TX,
4977             "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4978             __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4979             SEQNO(bf->bf_state.bfs_seqno));
4980
4981         atid->hwq_depth--;
4982         if (atid->hwq_depth < 0)
4983                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
4984                     __func__, atid->hwq_depth);
4985
4986         /*
4987          * If the TID is filtered, handle completing the filter
4988          * transition before potentially kicking it to the cleanup
4989          * function.
4990          */
4991         if (atid->isfiltered)
4992                 ath_tx_tid_filt_comp_complete(sc, atid);
4993
4994         /*
4995          * If a cleanup is in progress, punt to comp_cleanup;
4996          * rather than handling it here. It's thus their
4997          * responsibility to clean up, call the completion
4998          * function in net80211, etc.
4999          */
5000         if (atid->cleanup_inprogress) {
5001                 if (atid->isfiltered)
5002                         DPRINTF(sc, ATH_DEBUG_SW_TX,
5003                             "%s: isfiltered=1, normal_comp?\n",
5004                             __func__);
5005                 ATH_TX_UNLOCK(sc);
5006                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
5007                     __func__);
5008                 ath_tx_comp_cleanup_unaggr(sc, bf);
5009                 return;
5010         }
5011
5012         /*
5013          * XXX TODO: how does cleanup, BAR and filtered frame handling
5014          * overlap?
5015          *
5016          * If the frame is filtered OR if it's any failure but
5017          * the TID is filtered, the frame must be added to the
5018          * filtered frame list.
5019          *
5020          * However - a busy buffer can't be added to the filtered
5021          * list as it will end up being recycled without having
5022          * been made available for the hardware.
5023          */
5024         if ((ts.ts_status & HAL_TXERR_FILT) ||
5025             (ts.ts_status != 0 && atid->isfiltered)) {
5026                 int freeframe;
5027
5028                 if (fail != 0)
5029                         DPRINTF(sc, ATH_DEBUG_SW_TX,
5030                             "%s: isfiltered=1, fail=%d\n",
5031                             __func__, fail);
5032                 freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
5033                 if (freeframe) {
5034                         /* Remove from BAW */
5035                         if (bf->bf_state.bfs_addedbaw)
5036                                 drops++;
5037                         if (bf->bf_state.bfs_dobaw) {
5038                                 ath_tx_update_baw(sc, an, atid, bf);
5039                                 if (!bf->bf_state.bfs_addedbaw)
5040                                         DPRINTF(sc, ATH_DEBUG_SW_TX,
5041                                             "%s: wasn't added: seqno %d\n",
5042                                             __func__, SEQNO(bf->bf_state.bfs_seqno));
5043                         }
5044                         bf->bf_state.bfs_dobaw = 0;
5045                 }
5046
5047                 /*
5048                  * If the frame couldn't be filtered, treat it as a drop and
5049                  * prepare to send a BAR.
5050                  */
5051                 if (freeframe && drops)
5052                         ath_tx_tid_bar_suspend(sc, atid);
5053
5054                 /*
5055                  * Send BAR if required
5056                  */
5057                 if (ath_tx_tid_bar_tx_ready(sc, atid))
5058                         ath_tx_tid_bar_tx(sc, atid);
5059
5060                 ATH_TX_UNLOCK(sc);
5061                 /*
5062                  * If freeframe is set, then the frame couldn't be
5063                  * cloned and bf is still valid.  Just complete/free it.
5064                  */
5065                 if (freeframe)
5066                         ath_tx_default_comp(sc, bf, fail);
5067
5068
5069                 return;
5070         }
5071         /*
5072          * Don't bother with the retry check if all frames
5073          * are being failed (eg during queue deletion.)
5074          */
5075 #if 0
5076         if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
5077 #endif
5078         if (fail == 0 && ts.ts_status != 0) {
5079                 ATH_TX_UNLOCK(sc);
5080                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
5081                     __func__);
5082                 ath_tx_aggr_retry_unaggr(sc, bf);
5083                 return;
5084         }
5085
5086         /* Success? Complete */
5087         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
5088             __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
5089         if (bf->bf_state.bfs_dobaw) {
5090                 ath_tx_update_baw(sc, an, atid, bf);
5091                 bf->bf_state.bfs_dobaw = 0;
5092                 if (!bf->bf_state.bfs_addedbaw)
5093                         DPRINTF(sc, ATH_DEBUG_SW_TX,
5094                             "%s: wasn't added: seqno %d\n",
5095                             __func__, SEQNO(bf->bf_state.bfs_seqno));
5096         }
5097
5098         /*
5099          * If the queue is filtered, re-schedule as required.
5100          *
5101          * This is required as there may be a subsequent TX descriptor
5102          * for this end-node that has CLRDMASK set, so it's quite possible
5103          * that a filtered frame will be followed by a non-filtered
5104          * (complete or otherwise) frame.
5105          *
5106          * XXX should we do this before we complete the frame?
5107          */
5108         if (atid->isfiltered)
5109                 ath_tx_tid_filt_comp_complete(sc, atid);
5110
5111         /*
5112          * Send BAR if required
5113          */
5114         if (ath_tx_tid_bar_tx_ready(sc, atid))
5115                 ath_tx_tid_bar_tx(sc, atid);
5116
5117         ATH_TX_UNLOCK(sc);
5118
5119         ath_tx_default_comp(sc, bf, fail);
5120         /* bf is freed at this point */
5121 }
5122
5123 void
5124 ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
5125 {
5126         if (bf->bf_state.bfs_aggr)
5127                 ath_tx_aggr_comp_aggr(sc, bf, fail);
5128         else
5129                 ath_tx_aggr_comp_unaggr(sc, bf, fail);
5130 }
5131
5132 /*
5133  * Schedule some packets from the given node/TID to the hardware.
5134  *
5135  * This is the aggregate version.
5136  */
5137 void
5138 ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
5139     struct ath_tid *tid)
5140 {
5141         struct ath_buf *bf;
5142         struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5143         struct ieee80211_tx_ampdu *tap;
5144         ATH_AGGR_STATUS status;
5145         ath_bufhead bf_q;
5146
5147         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
5148         ATH_TX_LOCK_ASSERT(sc);
5149
5150         /*
5151          * XXX TODO: If we're called for a queue that we're leaking frames to,
5152          * ensure we only leak one.
5153          */
5154
5155         tap = ath_tx_get_tx_tid(an, tid->tid);
5156
5157         if (tid->tid == IEEE80211_NONQOS_TID)
5158                 DPRINTF(sc, ATH_DEBUG_SW_TX, 
5159                     "%s: called for TID=NONQOS_TID?\n", __func__);
5160
5161         for (;;) {
5162                 status = ATH_AGGR_DONE;
5163
5164                 /*
5165                  * If the upper layer has paused the TID, don't
5166                  * queue any further packets.
5167                  *
5168                  * This can also occur from the completion task because
5169                  * of packet loss; but as its serialised with this code,
5170                  * it won't "appear" half way through queuing packets.
5171                  */
5172                 if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5173                         break;
5174
5175                 bf = ATH_TID_FIRST(tid);
5176                 if (bf == NULL) {
5177                         break;
5178                 }
5179
5180                 /*
5181                  * If the packet doesn't fall within the BAW (eg a NULL
5182                  * data frame), schedule it directly; continue.
5183                  */
5184                 if (! bf->bf_state.bfs_dobaw) {
5185                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5186                             "%s: non-baw packet\n",
5187                             __func__);
5188                         ATH_TID_REMOVE(tid, bf, bf_list);
5189
5190                         if (bf->bf_state.bfs_nframes > 1)
5191                                 DPRINTF(sc, ATH_DEBUG_SW_TX, 
5192                                     "%s: aggr=%d, nframes=%d\n",
5193                                     __func__,
5194                                     bf->bf_state.bfs_aggr,
5195                                     bf->bf_state.bfs_nframes);
5196
5197                         /*
5198                          * This shouldn't happen - such frames shouldn't
5199                          * ever have been queued as an aggregate in the
5200                          * first place.  However, make sure the fields
5201                          * are correctly setup just to be totally sure.
5202                          */
5203                         bf->bf_state.bfs_aggr = 0;
5204                         bf->bf_state.bfs_nframes = 1;
5205
5206                         /* Update CLRDMASK just before this frame is queued */
5207                         ath_tx_update_clrdmask(sc, tid, bf);
5208
5209                         ath_tx_do_ratelookup(sc, bf);
5210                         ath_tx_calc_duration(sc, bf);
5211                         ath_tx_calc_protection(sc, bf);
5212                         ath_tx_set_rtscts(sc, bf);
5213                         ath_tx_rate_fill_rcflags(sc, bf);
5214                         ath_tx_setds(sc, bf);
5215                         ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5216
5217                         sc->sc_aggr_stats.aggr_nonbaw_pkt++;
5218
5219                         /* Queue the packet; continue */
5220                         goto queuepkt;
5221                 }
5222
5223                 TAILQ_INIT(&bf_q);
5224
5225                 /*
5226                  * Do a rate control lookup on the first frame in the
5227                  * list. The rate control code needs that to occur
5228                  * before it can determine whether to TX.
5229                  * It's inaccurate because the rate control code doesn't
5230                  * really "do" aggregate lookups, so it only considers
5231                  * the size of the first frame.
5232                  */
5233                 ath_tx_do_ratelookup(sc, bf);
5234                 bf->bf_state.bfs_rc[3].rix = 0;
5235                 bf->bf_state.bfs_rc[3].tries = 0;
5236
5237                 ath_tx_calc_duration(sc, bf);
5238                 ath_tx_calc_protection(sc, bf);
5239
5240                 ath_tx_set_rtscts(sc, bf);
5241                 ath_tx_rate_fill_rcflags(sc, bf);
5242
5243                 status = ath_tx_form_aggr(sc, an, tid, &bf_q);
5244
5245                 DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5246                     "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
5247
5248                 /*
5249                  * No frames to be picked up - out of BAW
5250                  */
5251                 if (TAILQ_EMPTY(&bf_q))
5252                         break;
5253
5254                 /*
5255                  * This assumes that the descriptor list in the ath_bufhead
5256                  * are already linked together via bf_next pointers.
5257                  */
5258                 bf = TAILQ_FIRST(&bf_q);
5259
5260                 if (status == ATH_AGGR_8K_LIMITED)
5261                         sc->sc_aggr_stats.aggr_rts_aggr_limited++;
5262
5263                 /*
5264                  * If it's the only frame send as non-aggregate
5265                  * assume that ath_tx_form_aggr() has checked
5266                  * whether it's in the BAW and added it appropriately.
5267                  */
5268                 if (bf->bf_state.bfs_nframes == 1) {
5269                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5270                             "%s: single-frame aggregate\n", __func__);
5271
5272                         /* Update CLRDMASK just before this frame is queued */
5273                         ath_tx_update_clrdmask(sc, tid, bf);
5274
5275                         bf->bf_state.bfs_aggr = 0;
5276                         bf->bf_state.bfs_ndelim = 0;
5277                         ath_tx_setds(sc, bf);
5278                         ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5279                         if (status == ATH_AGGR_BAW_CLOSED)
5280                                 sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
5281                         else
5282                                 sc->sc_aggr_stats.aggr_single_pkt++;
5283                 } else {
5284                         DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5285                             "%s: multi-frame aggregate: %d frames, "
5286                             "length %d\n",
5287                              __func__, bf->bf_state.bfs_nframes,
5288                             bf->bf_state.bfs_al);
5289                         bf->bf_state.bfs_aggr = 1;
5290                         sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
5291                         sc->sc_aggr_stats.aggr_aggr_pkt++;
5292
5293                         /* Update CLRDMASK just before this frame is queued */
5294                         ath_tx_update_clrdmask(sc, tid, bf);
5295
5296                         /*
5297                          * Calculate the duration/protection as required.
5298                          */
5299                         ath_tx_calc_duration(sc, bf);
5300                         ath_tx_calc_protection(sc, bf);
5301
5302                         /*
5303                          * Update the rate and rtscts information based on the
5304                          * rate decision made by the rate control code;
5305                          * the first frame in the aggregate needs it.
5306                          */
5307                         ath_tx_set_rtscts(sc, bf);
5308
5309                         /*
5310                          * Setup the relevant descriptor fields
5311                          * for aggregation. The first descriptor
5312                          * already points to the rest in the chain.
5313                          */
5314                         ath_tx_setds_11n(sc, bf);
5315
5316                 }
5317         queuepkt:
5318                 /* Set completion handler, multi-frame aggregate or not */
5319                 bf->bf_comp = ath_tx_aggr_comp;
5320
5321                 if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
5322                         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16?\n", __func__);
5323
5324                 /*
5325                  * Update leak count and frame config if were leaking frames.
5326                  *
5327                  * XXX TODO: it should update all frames in an aggregate
5328                  * correctly!
5329                  */
5330                 ath_tx_leak_count_update(sc, tid, bf);
5331
5332                 /* Punt to txq */
5333                 ath_tx_handoff(sc, txq, bf);
5334
5335                 /* Track outstanding buffer count to hardware */
5336                 /* aggregates are "one" buffer */
5337                 tid->hwq_depth++;
5338
5339                 /*
5340                  * Break out if ath_tx_form_aggr() indicated
5341                  * there can't be any further progress (eg BAW is full.)
5342                  * Checking for an empty txq is done above.
5343                  *
5344                  * XXX locking on txq here?
5345                  */
5346                 /* XXX TXQ locking */
5347                 if (txq->axq_aggr_depth >= sc->sc_hwq_limit_aggr ||
5348                     (status == ATH_AGGR_BAW_CLOSED ||
5349                      status == ATH_AGGR_LEAK_CLOSED))
5350                         break;
5351         }
5352 }
5353
5354 /*
5355  * Schedule some packets from the given node/TID to the hardware.
5356  *
5357  * XXX TODO: this routine doesn't enforce the maximum TXQ depth.
5358  * It just dumps frames into the TXQ.  We should limit how deep
5359  * the transmit queue can grow for frames dispatched to the given
5360  * TXQ.
5361  *
5362  * To avoid locking issues, either we need to own the TXQ lock
5363  * at this point, or we need to pass in the maximum frame count
5364  * from the caller.
5365  */
5366 void
5367 ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
5368     struct ath_tid *tid)
5369 {
5370         struct ath_buf *bf;
5371         struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5372
5373         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
5374             __func__, an, tid->tid);
5375
5376         ATH_TX_LOCK_ASSERT(sc);
5377
5378         /* Check - is AMPDU pending or running? then print out something */
5379         if (ath_tx_ampdu_pending(sc, an, tid->tid))
5380                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu pending?\n",
5381                     __func__, tid->tid);
5382         if (ath_tx_ampdu_running(sc, an, tid->tid))
5383                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu running?\n",
5384                     __func__, tid->tid);
5385
5386         for (;;) {
5387
5388                 /*
5389                  * If the upper layers have paused the TID, don't
5390                  * queue any further packets.
5391                  *
5392                  * XXX if we are leaking frames, make sure we decrement
5393                  * that counter _and_ we continue here.
5394                  */
5395                 if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5396                         break;
5397
5398                 bf = ATH_TID_FIRST(tid);
5399                 if (bf == NULL) {
5400                         break;
5401                 }
5402
5403                 ATH_TID_REMOVE(tid, bf, bf_list);
5404
5405                 /* Sanity check! */
5406                 if (tid->tid != bf->bf_state.bfs_tid) {
5407                         DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bfs_tid %d !="
5408                             " tid %d\n", __func__, bf->bf_state.bfs_tid,
5409                             tid->tid);
5410                 }
5411                 /* Normal completion handler */
5412                 bf->bf_comp = ath_tx_normal_comp;
5413
5414                 /*
5415                  * Override this for now, until the non-aggregate
5416                  * completion handler correctly handles software retransmits.
5417                  */
5418                 bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
5419
5420                 /* Update CLRDMASK just before this frame is queued */
5421                 ath_tx_update_clrdmask(sc, tid, bf);
5422
5423                 /* Program descriptors + rate control */
5424                 ath_tx_do_ratelookup(sc, bf);
5425                 ath_tx_calc_duration(sc, bf);
5426                 ath_tx_calc_protection(sc, bf);
5427                 ath_tx_set_rtscts(sc, bf);
5428                 ath_tx_rate_fill_rcflags(sc, bf);
5429                 ath_tx_setds(sc, bf);
5430
5431                 /*
5432                  * Update the current leak count if
5433                  * we're leaking frames; and set the
5434                  * MORE flag as appropriate.
5435                  */
5436                 ath_tx_leak_count_update(sc, tid, bf);
5437
5438                 /* Track outstanding buffer count to hardware */
5439                 /* aggregates are "one" buffer */
5440                 tid->hwq_depth++;
5441
5442                 /* Punt to hardware or software txq */
5443                 ath_tx_handoff(sc, txq, bf);
5444         }
5445 }
5446
5447 /*
5448  * Schedule some packets to the given hardware queue.
5449  *
5450  * This function walks the list of TIDs (ie, ath_node TIDs
5451  * with queued traffic) and attempts to schedule traffic
5452  * from them.
5453  *
5454  * TID scheduling is implemented as a FIFO, with TIDs being
5455  * added to the end of the queue after some frames have been
5456  * scheduled.
5457  */
5458 void
5459 ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
5460 {
5461         struct ath_tid *tid, *next, *last;
5462
5463         ATH_TX_LOCK_ASSERT(sc);
5464
5465         /*
5466          * Don't schedule if the hardware queue is busy.
5467          * This (hopefully) gives some more time to aggregate
5468          * some packets in the aggregation queue.
5469          *
5470          * XXX It doesn't stop a parallel sender from sneaking
5471          * in transmitting a frame!
5472          */
5473         /* XXX TXQ locking */
5474         if (txq->axq_aggr_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_aggr) {
5475                 sc->sc_aggr_stats.aggr_sched_nopkt++;
5476                 return;
5477         }
5478         if (txq->axq_depth >= sc->sc_hwq_limit_nonaggr) {
5479                 sc->sc_aggr_stats.aggr_sched_nopkt++;
5480                 return;
5481         }
5482
5483         last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
5484
5485         TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
5486                 /*
5487                  * Suspend paused queues here; they'll be resumed
5488                  * once the addba completes or times out.
5489                  */
5490                 DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
5491                     __func__, tid->tid, tid->paused);
5492                 ath_tx_tid_unsched(sc, tid);
5493                 /*
5494                  * This node may be in power-save and we're leaking
5495                  * a frame; be careful.
5496                  */
5497                 if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
5498                         continue;
5499                 }
5500                 if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
5501                         ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
5502                 else
5503                         ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5504
5505                 /* Not empty? Re-schedule */
5506                 if (tid->axq_depth != 0)
5507                         ath_tx_tid_sched(sc, tid);
5508
5509                 /*
5510                  * Give the software queue time to aggregate more
5511                  * packets.  If we aren't running aggregation then
5512                  * we should still limit the hardware queue depth.
5513                  */
5514                 /* XXX TXQ locking */
5515                 if (txq->axq_aggr_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_aggr) {
5516                         break;
5517                 }
5518                 if (txq->axq_depth >= sc->sc_hwq_limit_nonaggr) {
5519                         break;
5520                 }
5521
5522                 /*
5523                  * If this was the last entry on the original list, stop.
5524                  * Otherwise nodes that have been rescheduled onto the end
5525                  * of the TID FIFO list will just keep being rescheduled.
5526                  *
5527                  * XXX What should we do about nodes that were paused
5528                  * but are pending a leaking frame in response to a ps-poll?
5529                  * They'll be put at the front of the list; so they'll
5530                  * prematurely trigger this condition! Ew.
5531                  */
5532                 if (tid == last)
5533                         break;
5534         }
5535 }
5536
5537 /*
5538  * TX addba handling
5539  */
5540
5541 /*
5542  * Return net80211 TID struct pointer, or NULL for none
5543  */
5544 struct ieee80211_tx_ampdu *
5545 ath_tx_get_tx_tid(struct ath_node *an, int tid)
5546 {
5547         struct ieee80211_node *ni = &an->an_node;
5548         struct ieee80211_tx_ampdu *tap;
5549
5550         if (tid == IEEE80211_NONQOS_TID)
5551                 return NULL;
5552
5553         tap = &ni->ni_tx_ampdu[tid];
5554         return tap;
5555 }
5556
5557 /*
5558  * Is AMPDU-TX running?
5559  */
5560 static int
5561 ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5562 {
5563         struct ieee80211_tx_ampdu *tap;
5564
5565         if (tid == IEEE80211_NONQOS_TID)
5566                 return 0;
5567
5568         tap = ath_tx_get_tx_tid(an, tid);
5569         if (tap == NULL)
5570                 return 0;       /* Not valid; default to not running */
5571
5572         return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5573 }
5574
5575 /*
5576  * Is AMPDU-TX negotiation pending?
5577  */
5578 static int
5579 ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5580 {
5581         struct ieee80211_tx_ampdu *tap;
5582
5583         if (tid == IEEE80211_NONQOS_TID)
5584                 return 0;
5585
5586         tap = ath_tx_get_tx_tid(an, tid);
5587         if (tap == NULL)
5588                 return 0;       /* Not valid; default to not pending */
5589
5590         return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5591 }
5592
5593 /*
5594  * Is AMPDU-TX pending for the given TID?
5595  */
5596
5597
5598 /*
5599  * Method to handle sending an ADDBA request.
5600  *
5601  * We tap this so the relevant flags can be set to pause the TID
5602  * whilst waiting for the response.
5603  *
5604  * XXX there's no timeout handler we can override?
5605  */
5606 int
5607 ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5608     int dialogtoken, int baparamset, int batimeout)
5609 {
5610         struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5611         int tid = tap->txa_tid;
5612         struct ath_node *an = ATH_NODE(ni);
5613         struct ath_tid *atid = &an->an_tid[tid];
5614
5615         /*
5616          * XXX danger Will Robinson!
5617          *
5618          * Although the taskqueue may be running and scheduling some more
5619          * packets, these should all be _before_ the addba sequence number.
5620          * However, net80211 will keep self-assigning sequence numbers
5621          * until addba has been negotiated.
5622          *
5623          * In the past, these packets would be "paused" (which still works
5624          * fine, as they're being scheduled to the driver in the same
5625          * serialised method which is calling the addba request routine)
5626          * and when the aggregation session begins, they'll be dequeued
5627          * as aggregate packets and added to the BAW. However, now there's
5628          * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5629          * packets. Thus they never get included in the BAW tracking and
5630          * this can cause the initial burst of packets after the addba
5631          * negotiation to "hang", as they quickly fall outside the BAW.
5632          *
5633          * The "eventual" solution should be to tag these packets with
5634          * dobaw. Although net80211 has given us a sequence number,
5635          * it'll be "after" the left edge of the BAW and thus it'll
5636          * fall within it.
5637          */
5638         ATH_TX_LOCK(sc);
5639         /*
5640          * This is a bit annoying.  Until net80211 HT code inherits some
5641          * (any) locking, we may have this called in parallel BUT only
5642          * one response/timeout will be called.  Grr.
5643          */
5644         if (atid->addba_tx_pending == 0) {
5645                 ath_tx_tid_pause(sc, atid);
5646                 atid->addba_tx_pending = 1;
5647         }
5648         ATH_TX_UNLOCK(sc);
5649
5650         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5651             "%s: %6D: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
5652             __func__,
5653             ni->ni_macaddr,
5654             ":",
5655             dialogtoken, baparamset, batimeout);
5656         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5657             "%s: txa_start=%d, ni_txseqs=%d\n",
5658             __func__, tap->txa_start, ni->ni_txseqs[tid]);
5659
5660         return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5661             batimeout);
5662 }
5663
5664 /*
5665  * Handle an ADDBA response.
5666  *
5667  * We unpause the queue so TX'ing can resume.
5668  *
5669  * Any packets TX'ed from this point should be "aggregate" (whether
5670  * aggregate or not) so the BAW is updated.
5671  *
5672  * Note! net80211 keeps self-assigning sequence numbers until
5673  * ampdu is negotiated. This means the initially-negotiated BAW left
5674  * edge won't match the ni->ni_txseq.
5675  *
5676  * So, being very dirty, the BAW left edge is "slid" here to match
5677  * ni->ni_txseq.
5678  *
5679  * What likely SHOULD happen is that all packets subsequent to the
5680  * addba request should be tagged as aggregate and queued as non-aggregate
5681  * frames; thus updating the BAW. For now though, I'll just slide the
5682  * window.
5683  */
5684 int
5685 ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5686     int status, int code, int batimeout)
5687 {
5688         struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5689         int tid = tap->txa_tid;
5690         struct ath_node *an = ATH_NODE(ni);
5691         struct ath_tid *atid = &an->an_tid[tid];
5692         int r;
5693
5694         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5695             "%s: %6D: called; status=%d, code=%d, batimeout=%d\n", __func__,
5696             ni->ni_macaddr,
5697             ":",
5698             status, code, batimeout);
5699
5700         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5701             "%s: txa_start=%d, ni_txseqs=%d\n",
5702             __func__, tap->txa_start, ni->ni_txseqs[tid]);
5703
5704         /*
5705          * Call this first, so the interface flags get updated
5706          * before the TID is unpaused. Otherwise a race condition
5707          * exists where the unpaused TID still doesn't yet have
5708          * IEEE80211_AGGR_RUNNING set.
5709          */
5710         r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5711
5712         ATH_TX_LOCK(sc);
5713         atid->addba_tx_pending = 0;
5714         /*
5715          * XXX dirty!
5716          * Slide the BAW left edge to wherever net80211 left it for us.
5717          * Read above for more information.
5718          */
5719         tap->txa_start = ni->ni_txseqs[tid];
5720         ath_tx_tid_resume(sc, atid);
5721         ATH_TX_UNLOCK(sc);
5722         return r;
5723 }
5724
5725
5726 /*
5727  * Stop ADDBA on a queue.
5728  *
5729  * This can be called whilst BAR TX is currently active on the queue,
5730  * so make sure this is unblocked before continuing.
5731  */
5732 void
5733 ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5734 {
5735         struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5736         int tid = tap->txa_tid;
5737         struct ath_node *an = ATH_NODE(ni);
5738         struct ath_tid *atid = &an->an_tid[tid];
5739         ath_bufhead bf_cq;
5740         struct ath_buf *bf;
5741
5742         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: %6D: called\n",
5743             __func__,
5744             ni->ni_macaddr,
5745             ":");
5746
5747         /*
5748          * Pause TID traffic early, so there aren't any races
5749          * Unblock the pending BAR held traffic, if it's currently paused.
5750          */
5751         ATH_TX_LOCK(sc);
5752         ath_tx_tid_pause(sc, atid);
5753         if (atid->bar_wait) {
5754                 /*
5755                  * bar_unsuspend() expects bar_tx == 1, as it should be
5756                  * called from the TX completion path.  This quietens
5757                  * the warning.  It's cleared for us anyway.
5758                  */
5759                 atid->bar_tx = 1;
5760                 ath_tx_tid_bar_unsuspend(sc, atid);
5761         }
5762         ATH_TX_UNLOCK(sc);
5763
5764         /* There's no need to hold the TXQ lock here */
5765         sc->sc_addba_stop(ni, tap);
5766
5767         /*
5768          * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5769          * it'll set the cleanup flag, and it'll be unpaused once
5770          * things have been cleaned up.
5771          */
5772         TAILQ_INIT(&bf_cq);
5773         ATH_TX_LOCK(sc);
5774         ath_tx_tid_cleanup(sc, an, tid, &bf_cq);
5775         /*
5776          * Unpause the TID if no cleanup is required.
5777          */
5778         if (! atid->cleanup_inprogress)
5779                 ath_tx_tid_resume(sc, atid);
5780         ATH_TX_UNLOCK(sc);
5781
5782         /* Handle completing frames and fail them */
5783         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
5784                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
5785                 ath_tx_default_comp(sc, bf, 1);
5786         }
5787
5788 }
5789
5790 /*
5791  * Handle a node reassociation.
5792  *
5793  * We may have a bunch of frames queued to the hardware; those need
5794  * to be marked as cleanup.
5795  */
5796 void
5797 ath_tx_node_reassoc(struct ath_softc *sc, struct ath_node *an)
5798 {
5799         struct ath_tid *tid;
5800         int i;
5801         ath_bufhead bf_cq;
5802         struct ath_buf *bf;
5803
5804         TAILQ_INIT(&bf_cq);
5805
5806         ATH_TX_UNLOCK_ASSERT(sc);
5807
5808         ATH_TX_LOCK(sc);
5809         for (i = 0; i < IEEE80211_TID_SIZE; i++) {
5810                 tid = &an->an_tid[i];
5811                 if (tid->hwq_depth == 0)
5812                         continue;
5813                 ath_tx_tid_pause(sc, tid);
5814                 DPRINTF(sc, ATH_DEBUG_NODE,
5815                     "%s: %6D: TID %d: cleaning up TID\n",
5816                     __func__,
5817                     an->an_node.ni_macaddr,
5818                     ":",
5819                     i);
5820                 ath_tx_tid_cleanup(sc, an, i, &bf_cq);
5821                 /*
5822                  * Unpause the TID if no cleanup is required.
5823                  */
5824                 if (! tid->cleanup_inprogress)
5825                         ath_tx_tid_resume(sc, tid);
5826         }
5827         ATH_TX_UNLOCK(sc);
5828
5829         /* Handle completing frames and fail them */
5830         while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
5831                 TAILQ_REMOVE(&bf_cq, bf, bf_list);
5832                 ath_tx_default_comp(sc, bf, 1);
5833         }
5834 }
5835
5836 /*
5837  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5838  * it simply tears down the aggregation session. Ew.
5839  *
5840  * It however will call ieee80211_ampdu_stop() which will call
5841  * ic->ic_addba_stop().
5842  *
5843  * XXX This uses a hard-coded max BAR count value; the whole
5844  * XXX BAR TX success or failure should be better handled!
5845  */
5846 void
5847 ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5848     int status)
5849 {
5850         struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5851         int tid = tap->txa_tid;
5852         struct ath_node *an = ATH_NODE(ni);
5853         struct ath_tid *atid = &an->an_tid[tid];
5854         int attempts = tap->txa_attempts;
5855
5856         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5857             "%s: %6D: called; txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
5858             __func__,
5859             ni->ni_macaddr,
5860             ":",
5861             tap->txa_tid,
5862             atid->tid,
5863             status,
5864             attempts);
5865
5866         /* Note: This may update the BAW details */
5867         sc->sc_bar_response(ni, tap, status);
5868
5869         /* Unpause the TID */
5870         /*
5871          * XXX if this is attempt=50, the TID will be downgraded
5872          * XXX to a non-aggregate session. So we must unpause the
5873          * XXX TID here or it'll never be done.
5874          *
5875          * Also, don't call it if bar_tx/bar_wait are 0; something
5876          * has beaten us to the punch? (XXX figure out what?)
5877          */
5878         if (status == 0 || attempts == 50) {
5879                 ATH_TX_LOCK(sc);
5880                 if (atid->bar_tx == 0 || atid->bar_wait == 0)
5881                         DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5882                             "%s: huh? bar_tx=%d, bar_wait=%d\n",
5883                             __func__,
5884                             atid->bar_tx, atid->bar_wait);
5885                 else
5886                         ath_tx_tid_bar_unsuspend(sc, atid);
5887                 ATH_TX_UNLOCK(sc);
5888         }
5889 }
5890
5891 /*
5892  * This is called whenever the pending ADDBA request times out.
5893  * Unpause and reschedule the TID.
5894  */
5895 void
5896 ath_addba_response_timeout(struct ieee80211_node *ni,
5897     struct ieee80211_tx_ampdu *tap)
5898 {
5899         struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5900         int tid = tap->txa_tid;
5901         struct ath_node *an = ATH_NODE(ni);
5902         struct ath_tid *atid = &an->an_tid[tid];
5903
5904         DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5905             "%s: %6D: TID=%d, called; resuming\n",
5906             __func__,
5907             ni->ni_macaddr,
5908             ":",
5909             tid);
5910
5911         ATH_TX_LOCK(sc);
5912         atid->addba_tx_pending = 0;
5913         ATH_TX_UNLOCK(sc);
5914
5915         /* Note: This updates the aggregate state to (again) pending */
5916         sc->sc_addba_response_timeout(ni, tap);
5917
5918         /* Unpause the TID; which reschedules it */
5919         ATH_TX_LOCK(sc);
5920         ath_tx_tid_resume(sc, atid);
5921         ATH_TX_UNLOCK(sc);
5922 }
5923
5924 /*
5925  * Check if a node is asleep or not.
5926  */
5927 int
5928 ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
5929 {
5930
5931         ATH_TX_LOCK_ASSERT(sc);
5932
5933         return (an->an_is_powersave);
5934 }
5935
5936 /*
5937  * Mark a node as currently "in powersaving."
5938  * This suspends all traffic on the node.
5939  *
5940  * This must be called with the node/tx locks free.
5941  *
5942  * XXX TODO: the locking silliness below is due to how the node
5943  * locking currently works.  Right now, the node lock is grabbed
5944  * to do rate control lookups and these are done with the TX
5945  * queue lock held.  This means the node lock can't be grabbed
5946  * first here or a LOR will occur.
5947  *
5948  * Eventually (hopefully!) the TX path code will only grab
5949  * the TXQ lock when transmitting and the ath_node lock when
5950  * doing node/TID operations.  There are other complications -
5951  * the sched/unsched operations involve walking the per-txq
5952  * 'active tid' list and this requires both locks to be held.
5953  */
5954 void
5955 ath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
5956 {
5957         struct ath_tid *atid;
5958         struct ath_txq *txq;
5959         int tid;
5960
5961         ATH_TX_UNLOCK_ASSERT(sc);
5962
5963         /* Suspend all traffic on the node */
5964         ATH_TX_LOCK(sc);
5965
5966         if (an->an_is_powersave) {
5967                 DPRINTF(sc, ATH_DEBUG_XMIT,
5968                     "%s: %6D: node was already asleep!\n",
5969                     __func__, an->an_node.ni_macaddr, ":");
5970                 ATH_TX_UNLOCK(sc);
5971                 return;
5972         }
5973
5974         for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5975                 atid = &an->an_tid[tid];
5976                 txq = sc->sc_ac2q[atid->ac];
5977
5978                 ath_tx_tid_pause(sc, atid);
5979         }
5980
5981         /* Mark node as in powersaving */
5982         an->an_is_powersave = 1;
5983
5984         ATH_TX_UNLOCK(sc);
5985 }
5986
5987 /*
5988  * Mark a node as currently "awake."
5989  * This resumes all traffic to the node.
5990  */
5991 void
5992 ath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
5993 {
5994         struct ath_tid *atid;
5995         struct ath_txq *txq;
5996         int tid;
5997
5998         ATH_TX_UNLOCK_ASSERT(sc);
5999
6000         ATH_TX_LOCK(sc);
6001
6002         /* !? */
6003         if (an->an_is_powersave == 0) {
6004                 ATH_TX_UNLOCK(sc);
6005                 DPRINTF(sc, ATH_DEBUG_XMIT,
6006                     "%s: an=%p: node was already awake\n",
6007                     __func__, an);
6008                 return;
6009         }
6010
6011         /* Mark node as awake */
6012         an->an_is_powersave = 0;
6013         /*
6014          * Clear any pending leaked frame requests
6015          */
6016         an->an_leak_count = 0;
6017
6018         for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
6019                 atid = &an->an_tid[tid];
6020                 txq = sc->sc_ac2q[atid->ac];
6021
6022                 ath_tx_tid_resume(sc, atid);
6023         }
6024         ATH_TX_UNLOCK(sc);
6025 }
6026
6027 static int
6028 ath_legacy_dma_txsetup(struct ath_softc *sc)
6029 {
6030
6031         /* nothing new needed */
6032         return (0);
6033 }
6034
6035 static int
6036 ath_legacy_dma_txteardown(struct ath_softc *sc)
6037 {
6038
6039         /* nothing new needed */
6040         return (0);
6041 }
6042
6043 void
6044 ath_xmit_setup_legacy(struct ath_softc *sc)
6045 {
6046         /*
6047          * For now, just set the descriptor length to sizeof(ath_desc);
6048          * worry about extracting the real length out of the HAL later.
6049          */
6050         sc->sc_tx_desclen = sizeof(struct ath_desc);
6051         sc->sc_tx_statuslen = sizeof(struct ath_desc);
6052         sc->sc_tx_nmaps = 1;    /* only one buffer per TX desc */
6053
6054         sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
6055         sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
6056         sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
6057
6058         sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
6059         sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
6060
6061         sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
6062 }