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