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